slu.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createSlu = void 0;
  6. var _number = require("../../../utils/number.js");
  7. var _factory = require("../../../utils/factory.js");
  8. var _csSqr = require("../sparse/csSqr.js");
  9. var _csLu = require("../sparse/csLu.js");
  10. var name = 'slu';
  11. var dependencies = ['typed', 'abs', 'add', 'multiply', 'transpose', 'divideScalar', 'subtract', 'larger', 'largerEq', 'SparseMatrix'];
  12. var createSlu = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  13. var typed = _ref.typed,
  14. abs = _ref.abs,
  15. add = _ref.add,
  16. multiply = _ref.multiply,
  17. transpose = _ref.transpose,
  18. divideScalar = _ref.divideScalar,
  19. subtract = _ref.subtract,
  20. larger = _ref.larger,
  21. largerEq = _ref.largerEq,
  22. SparseMatrix = _ref.SparseMatrix;
  23. var csSqr = (0, _csSqr.createCsSqr)({
  24. add: add,
  25. multiply: multiply,
  26. transpose: transpose
  27. });
  28. var csLu = (0, _csLu.createCsLu)({
  29. abs: abs,
  30. divideScalar: divideScalar,
  31. multiply: multiply,
  32. subtract: subtract,
  33. larger: larger,
  34. largerEq: largerEq,
  35. SparseMatrix: SparseMatrix
  36. });
  37. /**
  38. * Calculate the Sparse Matrix LU decomposition with full pivoting. Sparse Matrix `A` is decomposed in two matrices (`L`, `U`) and two permutation vectors (`pinv`, `q`) where
  39. *
  40. * `P * A * Q = L * U`
  41. *
  42. * Syntax:
  43. *
  44. * math.slu(A, order, threshold)
  45. *
  46. * Examples:
  47. *
  48. * const A = math.sparse([[4,3], [6, 3]])
  49. * math.slu(A, 1, 0.001)
  50. * // returns:
  51. * // {
  52. * // L: [[1, 0], [1.5, 1]]
  53. * // U: [[4, 3], [0, -1.5]]
  54. * // p: [0, 1]
  55. * // q: [0, 1]
  56. * // }
  57. *
  58. * See also:
  59. *
  60. * lup, lsolve, usolve, lusolve
  61. *
  62. * @param {SparseMatrix} A A two dimensional sparse matrix for which to get the LU decomposition.
  63. * @param {Number} order The Symbolic Ordering and Analysis order:
  64. * 0 - Natural ordering, no permutation vector q is returned
  65. * 1 - Matrix must be square, symbolic ordering and analisis is performed on M = A + A'
  66. * 2 - Symbolic ordering and analisis is performed on M = A' * A. Dense columns from A' are dropped, A recreated from A'.
  67. * This is appropriatefor LU factorization of unsymmetric matrices.
  68. * 3 - Symbolic ordering and analisis is performed on M = A' * A. This is best used for LU factorization is matrix M has no dense rows.
  69. * A dense row is a row with more than 10*sqr(columns) entries.
  70. * @param {Number} threshold Partial pivoting threshold (1 for partial pivoting)
  71. *
  72. * @return {Object} The lower triangular matrix, the upper triangular matrix and the permutation vectors.
  73. */
  74. return typed(name, {
  75. 'SparseMatrix, number, number': function SparseMatrixNumberNumber(a, order, threshold) {
  76. // verify order
  77. if (!(0, _number.isInteger)(order) || order < 0 || order > 3) {
  78. throw new Error('Symbolic Ordering and Analysis order must be an integer number in the interval [0, 3]');
  79. }
  80. // verify threshold
  81. if (threshold < 0 || threshold > 1) {
  82. throw new Error('Partial pivoting threshold must be a number from 0 to 1');
  83. }
  84. // perform symbolic ordering and analysis
  85. var s = csSqr(order, a, false);
  86. // perform lu decomposition
  87. var f = csLu(a, s, threshold);
  88. // return decomposition
  89. return {
  90. L: f.L,
  91. U: f.U,
  92. p: f.pinv,
  93. q: s.q,
  94. toString: function toString() {
  95. return 'L: ' + this.L.toString() + '\nU: ' + this.U.toString() + '\np: ' + this.p.toString() + (this.q ? '\nq: ' + this.q.toString() : '') + '\n';
  96. }
  97. };
  98. }
  99. });
  100. });
  101. exports.createSlu = createSlu;