UNPKG

@rayyamhk/matrix

Version:

A professional, comprehensive and high-performance library for you to manipulate matrices.

151 lines (113 loc) 4.79 kB
"use strict"; function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } var _require = require('../../Error'), INVALID_MATRIX = _require.INVALID_MATRIX; /** * Calculates the LUP decomposition of the Matrix, * where L is lower triangular matrix which diagonal entries are always 1, * U is upper triangular matrix, and P is permutation matrix.<br><br> * * It is implemented using Gaussian Elimination with Partial Pivoting in order to * reduce the error caused by floating-point arithmetic.<br><br> * * Note that if optimized is true, P is a Permutation Array and both L and U are merged * into one matrix in order to improve performance. * @memberof Matrix * @static * @param {Matrix} A - Any matrix * @param {boolean} [optimized=false] - Returns [P, LU] if it is true, [P, L, U] if it is false * @returns {Matrix[]} The LUP decomposition of Matrix */ function LU(A) { var optimized = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; if (!(A instanceof this)) { throw new Error(INVALID_MATRIX); } var _A$size = A.size(), _A$size2 = _slicedToArray(_A$size, 2), row = _A$size2[0], col = _A$size2[1]; var size = Math.min(row, col); var EPSILON = 1 / (Math.pow(10, A._digit) * 2); var permutation = initPermutation(row); var copy = this.clone(A)._matrix; for (var i = 0; i < row - 1; i++) { var currentCol = Math.min(i, col); // apply Partial Pivoting PartialPivoting(copy, permutation, currentCol, row, col); var ith = permutation[i]; var pivot = copy[ith][currentCol]; if (Math.abs(pivot) < EPSILON) { continue; } for (var j = i + 1; j < row; j++) { var jth = permutation[j]; var entry = copy[jth][currentCol]; if (Math.abs(entry) >= EPSILON) { var factor = entry / pivot; for (var k = currentCol; k < col; k++) { copy[jth][k] -= factor * copy[ith][k]; } copy[jth][currentCol] = factor; } } } var result = new Array(row); for (var _i2 = 0; _i2 < row; _i2++) { result[_i2] = copy[permutation[_i2]]; } if (optimized) { return [permutation, new this(result)]; } var P = this.generate(row, row, function (i, j) { var idx = permutation[i]; if (j === idx) { return 1; } return 0; }); var L = this.generate(row, size, function (i, j) { if (i === j) { return 1; } if (i < j) { return 0; } return result[i][j]; }); var U = this.generate(size, col, function (i, j) { if (i > j) { return 0; } return result[i][j]; }); return [P, L, U]; } ; function initPermutation(size) { var permutation = new Array(size); for (var i = 0; i < size; i++) { permutation[i] = i; } return permutation; } function PartialPivoting(matrix, permutation, pos, row, col) { var currentCol = Math.min(pos, col); var maxIdx = pos; var max = Math.abs(matrix[permutation[pos]][currentCol]); for (var i = pos + 1; i < row; i++) { var value = Math.abs(matrix[permutation[i]][currentCol]); if (value > max) { maxIdx = i; max = value; } } var t = permutation[pos]; permutation[pos] = permutation[maxIdx]; permutation[maxIdx] = t; } module.exports = LU;