tdma.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview The Tridiagonal matrix algorithm solver solves a special
  16. * version of a sparse linear system Ax = b where A is tridiagonal.
  17. *
  18. * See http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm
  19. *
  20. */
  21. goog.provide('goog.math.tdma');
  22. /**
  23. * Solves a linear system where the matrix is square tri-diagonal. That is,
  24. * given a system of equations:
  25. *
  26. * A * result = vecRight,
  27. *
  28. * this class computes result = inv(A) * vecRight, where A has the special form
  29. * of a tri-diagonal matrix:
  30. *
  31. * |dia(0) sup(0) 0 0 ... 0|
  32. * |sub(0) dia(1) sup(1) 0 ... 0|
  33. * A =| ... |
  34. * |0 ... 0 sub(n-2) dia(n-1) sup(n-1)|
  35. * |0 ... 0 0 sub(n-1) dia(n)|
  36. *
  37. * @param {!Array<number>} subDiag The sub diagonal of the matrix.
  38. * @param {!Array<number>} mainDiag The main diagonal of the matrix.
  39. * @param {!Array<number>} supDiag The super diagonal of the matrix.
  40. * @param {!Array<number>} vecRight The right vector of the system
  41. * of equations.
  42. * @param {Array<number>=} opt_result The optional array to store the result.
  43. * @return {!Array<number>} The vector that is the solution to the system.
  44. */
  45. goog.math.tdma.solve = function(
  46. subDiag, mainDiag, supDiag, vecRight, opt_result) {
  47. // Make a local copy of the main diagonal and the right vector.
  48. mainDiag = mainDiag.slice();
  49. vecRight = vecRight.slice();
  50. // The dimension of the matrix.
  51. var nDim = mainDiag.length;
  52. // Construct a modified linear system of equations with the same solution
  53. // as the input one.
  54. for (var i = 1; i < nDim; ++i) {
  55. var m = subDiag[i - 1] / mainDiag[i - 1];
  56. mainDiag[i] = mainDiag[i] - m * supDiag[i - 1];
  57. vecRight[i] = vecRight[i] - m * vecRight[i - 1];
  58. }
  59. // Solve the new system of equations by simple back-substitution.
  60. var result = opt_result || new Array(vecRight.length);
  61. result[nDim - 1] = vecRight[nDim - 1] / mainDiag[nDim - 1];
  62. for (i = nDim - 2; i >= 0; --i) {
  63. result[i] = (vecRight[i] - supDiag[i] * result[i + 1]) / mainDiag[i];
  64. }
  65. return result;
  66. };