simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

slu.js (3986B)


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