feat:node-modules
This commit is contained in:
163
node_modules/mathjs/lib/cjs/function/algebra/solver/lsolve.js
generated
vendored
Normal file
163
node_modules/mathjs/lib/cjs/function/algebra/solver/lsolve.js
generated
vendored
Normal file
@@ -0,0 +1,163 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createLsolve = void 0;
|
||||
var _factory = require("../../../utils/factory.js");
|
||||
var _solveValidation = require("./utils/solveValidation.js");
|
||||
const name = 'lsolve';
|
||||
const dependencies = ['typed', 'matrix', 'divideScalar', 'multiplyScalar', 'subtractScalar', 'equalScalar', 'DenseMatrix'];
|
||||
const createLsolve = exports.createLsolve = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
matrix,
|
||||
divideScalar,
|
||||
multiplyScalar,
|
||||
subtractScalar,
|
||||
equalScalar,
|
||||
DenseMatrix
|
||||
} = _ref;
|
||||
const solveValidation = (0, _solveValidation.createSolveValidation)({
|
||||
DenseMatrix
|
||||
});
|
||||
|
||||
/**
|
||||
* Finds one solution of a linear equation system by forwards substitution. Matrix must be a lower triangular matrix. Throws an error if there's no solution.
|
||||
*
|
||||
* `L * x = b`
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.lsolve(L, b)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* const a = [[-2, 3], [2, 1]]
|
||||
* const b = [11, 9]
|
||||
* const x = lsolve(a, b) // [[-5.5], [20]]
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* lsolveAll, lup, slu, usolve, lusolve
|
||||
*
|
||||
* @param {Matrix, Array} L A N x N matrix or array (L)
|
||||
* @param {Matrix, Array} b A column vector with the b values
|
||||
*
|
||||
* @return {DenseMatrix | Array} A column vector with the linear system solution (x)
|
||||
*/
|
||||
return typed(name, {
|
||||
'SparseMatrix, Array | Matrix': function (m, b) {
|
||||
return _sparseForwardSubstitution(m, b);
|
||||
},
|
||||
'DenseMatrix, Array | Matrix': function (m, b) {
|
||||
return _denseForwardSubstitution(m, b);
|
||||
},
|
||||
'Array, Array | Matrix': function (a, b) {
|
||||
const m = matrix(a);
|
||||
const r = _denseForwardSubstitution(m, b);
|
||||
return r.valueOf();
|
||||
}
|
||||
});
|
||||
function _denseForwardSubstitution(m, b) {
|
||||
// validate matrix and vector, return copy of column vector b
|
||||
b = solveValidation(m, b, true);
|
||||
const bdata = b._data;
|
||||
const rows = m._size[0];
|
||||
const columns = m._size[1];
|
||||
|
||||
// result
|
||||
const x = [];
|
||||
const mdata = m._data;
|
||||
|
||||
// loop columns
|
||||
for (let j = 0; j < columns; j++) {
|
||||
const bj = bdata[j][0] || 0;
|
||||
let xj;
|
||||
if (!equalScalar(bj, 0)) {
|
||||
// non-degenerate row, find solution
|
||||
|
||||
const vjj = mdata[j][j];
|
||||
if (equalScalar(vjj, 0)) {
|
||||
throw new Error('Linear system cannot be solved since matrix is singular');
|
||||
}
|
||||
xj = divideScalar(bj, vjj);
|
||||
|
||||
// loop rows
|
||||
for (let i = j + 1; i < rows; i++) {
|
||||
bdata[i] = [subtractScalar(bdata[i][0] || 0, multiplyScalar(xj, mdata[i][j]))];
|
||||
}
|
||||
} else {
|
||||
// degenerate row, we can choose any value
|
||||
xj = 0;
|
||||
}
|
||||
x[j] = [xj];
|
||||
}
|
||||
return new DenseMatrix({
|
||||
data: x,
|
||||
size: [rows, 1]
|
||||
});
|
||||
}
|
||||
function _sparseForwardSubstitution(m, b) {
|
||||
// validate matrix and vector, return copy of column vector b
|
||||
b = solveValidation(m, b, true);
|
||||
const bdata = b._data;
|
||||
const rows = m._size[0];
|
||||
const columns = m._size[1];
|
||||
const values = m._values;
|
||||
const index = m._index;
|
||||
const ptr = m._ptr;
|
||||
|
||||
// result
|
||||
const x = [];
|
||||
|
||||
// loop columns
|
||||
for (let j = 0; j < columns; j++) {
|
||||
const bj = bdata[j][0] || 0;
|
||||
if (!equalScalar(bj, 0)) {
|
||||
// non-degenerate row, find solution
|
||||
|
||||
let vjj = 0;
|
||||
// matrix values & indices (column j)
|
||||
const jValues = [];
|
||||
const jIndices = [];
|
||||
|
||||
// first and last index in the column
|
||||
const firstIndex = ptr[j];
|
||||
const lastIndex = ptr[j + 1];
|
||||
|
||||
// values in column, find value at [j, j]
|
||||
for (let k = firstIndex; k < lastIndex; k++) {
|
||||
const i = index[k];
|
||||
|
||||
// check row (rows are not sorted!)
|
||||
if (i === j) {
|
||||
vjj = values[k];
|
||||
} else if (i > j) {
|
||||
// store lower triangular
|
||||
jValues.push(values[k]);
|
||||
jIndices.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
// at this point we must have a value in vjj
|
||||
if (equalScalar(vjj, 0)) {
|
||||
throw new Error('Linear system cannot be solved since matrix is singular');
|
||||
}
|
||||
const xj = divideScalar(bj, vjj);
|
||||
for (let k = 0, l = jIndices.length; k < l; k++) {
|
||||
const i = jIndices[k];
|
||||
bdata[i] = [subtractScalar(bdata[i][0] || 0, multiplyScalar(xj, jValues[k]))];
|
||||
}
|
||||
x[j] = [xj];
|
||||
} else {
|
||||
// degenerate row, we can choose any value
|
||||
x[j] = [0];
|
||||
}
|
||||
}
|
||||
return new DenseMatrix({
|
||||
data: x,
|
||||
size: [rows, 1]
|
||||
});
|
||||
}
|
||||
});
|
||||
192
node_modules/mathjs/lib/cjs/function/algebra/solver/lsolveAll.js
generated
vendored
Normal file
192
node_modules/mathjs/lib/cjs/function/algebra/solver/lsolveAll.js
generated
vendored
Normal file
@@ -0,0 +1,192 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createLsolveAll = void 0;
|
||||
var _factory = require("../../../utils/factory.js");
|
||||
var _solveValidation = require("./utils/solveValidation.js");
|
||||
const name = 'lsolveAll';
|
||||
const dependencies = ['typed', 'matrix', 'divideScalar', 'multiplyScalar', 'subtractScalar', 'equalScalar', 'DenseMatrix'];
|
||||
const createLsolveAll = exports.createLsolveAll = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
matrix,
|
||||
divideScalar,
|
||||
multiplyScalar,
|
||||
subtractScalar,
|
||||
equalScalar,
|
||||
DenseMatrix
|
||||
} = _ref;
|
||||
const solveValidation = (0, _solveValidation.createSolveValidation)({
|
||||
DenseMatrix
|
||||
});
|
||||
|
||||
/**
|
||||
* Finds all solutions of a linear equation system by forwards substitution. Matrix must be a lower triangular matrix.
|
||||
*
|
||||
* `L * x = b`
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.lsolveAll(L, b)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* const a = [[-2, 3], [2, 1]]
|
||||
* const b = [11, 9]
|
||||
* const x = lsolveAll(a, b) // [ [[-5.5], [20]] ]
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* lsolve, lup, slu, usolve, lusolve
|
||||
*
|
||||
* @param {Matrix, Array} L A N x N matrix or array (L)
|
||||
* @param {Matrix, Array} b A column vector with the b values
|
||||
*
|
||||
* @return {DenseMatrix[] | Array[]} An array of affine-independent column vectors (x) that solve the linear system
|
||||
*/
|
||||
return typed(name, {
|
||||
'SparseMatrix, Array | Matrix': function (m, b) {
|
||||
return _sparseForwardSubstitution(m, b);
|
||||
},
|
||||
'DenseMatrix, Array | Matrix': function (m, b) {
|
||||
return _denseForwardSubstitution(m, b);
|
||||
},
|
||||
'Array, Array | Matrix': function (a, b) {
|
||||
const m = matrix(a);
|
||||
const R = _denseForwardSubstitution(m, b);
|
||||
return R.map(r => r.valueOf());
|
||||
}
|
||||
});
|
||||
function _denseForwardSubstitution(m, b_) {
|
||||
// the algorithm is derived from
|
||||
// https://www.overleaf.com/read/csvgqdxggyjv
|
||||
|
||||
// array of right-hand sides
|
||||
const B = [solveValidation(m, b_, true)._data.map(e => e[0])];
|
||||
const M = m._data;
|
||||
const rows = m._size[0];
|
||||
const columns = m._size[1];
|
||||
|
||||
// loop columns
|
||||
for (let i = 0; i < columns; i++) {
|
||||
let L = B.length;
|
||||
|
||||
// loop right-hand sides
|
||||
for (let k = 0; k < L; k++) {
|
||||
const b = B[k];
|
||||
if (!equalScalar(M[i][i], 0)) {
|
||||
// non-singular row
|
||||
|
||||
b[i] = divideScalar(b[i], M[i][i]);
|
||||
for (let j = i + 1; j < columns; j++) {
|
||||
// b[j] -= b[i] * M[j,i]
|
||||
b[j] = subtractScalar(b[j], multiplyScalar(b[i], M[j][i]));
|
||||
}
|
||||
} else if (!equalScalar(b[i], 0)) {
|
||||
// singular row, nonzero RHS
|
||||
|
||||
if (k === 0) {
|
||||
// There is no valid solution
|
||||
return [];
|
||||
} else {
|
||||
// This RHS is invalid but other solutions may still exist
|
||||
B.splice(k, 1);
|
||||
k -= 1;
|
||||
L -= 1;
|
||||
}
|
||||
} else if (k === 0) {
|
||||
// singular row, RHS is zero
|
||||
|
||||
const bNew = [...b];
|
||||
bNew[i] = 1;
|
||||
for (let j = i + 1; j < columns; j++) {
|
||||
bNew[j] = subtractScalar(bNew[j], M[j][i]);
|
||||
}
|
||||
B.push(bNew);
|
||||
}
|
||||
}
|
||||
}
|
||||
return B.map(x => new DenseMatrix({
|
||||
data: x.map(e => [e]),
|
||||
size: [rows, 1]
|
||||
}));
|
||||
}
|
||||
function _sparseForwardSubstitution(m, b_) {
|
||||
// array of right-hand sides
|
||||
const B = [solveValidation(m, b_, true)._data.map(e => e[0])];
|
||||
const rows = m._size[0];
|
||||
const columns = m._size[1];
|
||||
const values = m._values;
|
||||
const index = m._index;
|
||||
const ptr = m._ptr;
|
||||
|
||||
// loop columns
|
||||
for (let i = 0; i < columns; i++) {
|
||||
let L = B.length;
|
||||
|
||||
// loop right-hand sides
|
||||
for (let k = 0; k < L; k++) {
|
||||
const b = B[k];
|
||||
|
||||
// values & indices (column i)
|
||||
const iValues = [];
|
||||
const iIndices = [];
|
||||
|
||||
// first & last indeces in column
|
||||
const firstIndex = ptr[i];
|
||||
const lastIndex = ptr[i + 1];
|
||||
|
||||
// find the value at [i, i]
|
||||
let Mii = 0;
|
||||
for (let j = firstIndex; j < lastIndex; j++) {
|
||||
const J = index[j];
|
||||
// check row
|
||||
if (J === i) {
|
||||
Mii = values[j];
|
||||
} else if (J > i) {
|
||||
// store lower triangular
|
||||
iValues.push(values[j]);
|
||||
iIndices.push(J);
|
||||
}
|
||||
}
|
||||
if (!equalScalar(Mii, 0)) {
|
||||
// non-singular row
|
||||
|
||||
b[i] = divideScalar(b[i], Mii);
|
||||
for (let j = 0, lastIndex = iIndices.length; j < lastIndex; j++) {
|
||||
const J = iIndices[j];
|
||||
b[J] = subtractScalar(b[J], multiplyScalar(b[i], iValues[j]));
|
||||
}
|
||||
} else if (!equalScalar(b[i], 0)) {
|
||||
// singular row, nonzero RHS
|
||||
|
||||
if (k === 0) {
|
||||
// There is no valid solution
|
||||
return [];
|
||||
} else {
|
||||
// This RHS is invalid but other solutions may still exist
|
||||
B.splice(k, 1);
|
||||
k -= 1;
|
||||
L -= 1;
|
||||
}
|
||||
} else if (k === 0) {
|
||||
// singular row, RHS is zero
|
||||
|
||||
const bNew = [...b];
|
||||
bNew[i] = 1;
|
||||
for (let j = 0, lastIndex = iIndices.length; j < lastIndex; j++) {
|
||||
const J = iIndices[j];
|
||||
bNew[J] = subtractScalar(bNew[J], iValues[j]);
|
||||
}
|
||||
B.push(bNew);
|
||||
}
|
||||
}
|
||||
}
|
||||
return B.map(x => new DenseMatrix({
|
||||
data: x.map(e => [e]),
|
||||
size: [rows, 1]
|
||||
}));
|
||||
}
|
||||
});
|
||||
114
node_modules/mathjs/lib/cjs/function/algebra/solver/lusolve.js
generated
vendored
Normal file
114
node_modules/mathjs/lib/cjs/function/algebra/solver/lusolve.js
generated
vendored
Normal file
@@ -0,0 +1,114 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createLusolve = void 0;
|
||||
var _is = require("../../../utils/is.js");
|
||||
var _factory = require("../../../utils/factory.js");
|
||||
var _solveValidation = require("./utils/solveValidation.js");
|
||||
var _csIpvec = require("../sparse/csIpvec.js");
|
||||
const name = 'lusolve';
|
||||
const dependencies = ['typed', 'matrix', 'lup', 'slu', 'usolve', 'lsolve', 'DenseMatrix'];
|
||||
const createLusolve = exports.createLusolve = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
matrix,
|
||||
lup,
|
||||
slu,
|
||||
usolve,
|
||||
lsolve,
|
||||
DenseMatrix
|
||||
} = _ref;
|
||||
const solveValidation = (0, _solveValidation.createSolveValidation)({
|
||||
DenseMatrix
|
||||
});
|
||||
|
||||
/**
|
||||
* Solves the linear system `A * x = b` where `A` is an [n x n] matrix and `b` is a [n] column vector.
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.lusolve(A, b) // returns column vector with the solution to the linear system A * x = b
|
||||
* math.lusolve(lup, b) // returns column vector with the solution to the linear system A * x = b, lup = math.lup(A)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* const m = [[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]]
|
||||
*
|
||||
* const x = math.lusolve(m, [-1, -1, -1, -1]) // x = [[-1], [-0.5], [-1/3], [-0.25]]
|
||||
*
|
||||
* const f = math.lup(m)
|
||||
* const x1 = math.lusolve(f, [-1, -1, -1, -1]) // x1 = [[-1], [-0.5], [-1/3], [-0.25]]
|
||||
* const x2 = math.lusolve(f, [1, 2, 1, -1]) // x2 = [[1], [1], [1/3], [-0.25]]
|
||||
*
|
||||
* const a = [[-2, 3], [2, 1]]
|
||||
* const b = [11, 9]
|
||||
* const x = math.lusolve(a, b) // [[2], [5]]
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* lup, slu, lsolve, usolve
|
||||
*
|
||||
* @param {Matrix | Array | Object} A Invertible Matrix or the Matrix LU decomposition
|
||||
* @param {Matrix | Array} b Column Vector
|
||||
* @param {number} [order] The Symbolic Ordering and Analysis order, see slu for details. Matrix must be a SparseMatrix
|
||||
* @param {Number} [threshold] Partial pivoting threshold (1 for partial pivoting), see slu for details. Matrix must be a SparseMatrix.
|
||||
*
|
||||
* @return {DenseMatrix | Array} Column vector with the solution to the linear system A * x = b
|
||||
*/
|
||||
return typed(name, {
|
||||
'Array, Array | Matrix': function (a, b) {
|
||||
a = matrix(a);
|
||||
const d = lup(a);
|
||||
const x = _lusolve(d.L, d.U, d.p, null, b);
|
||||
return x.valueOf();
|
||||
},
|
||||
'DenseMatrix, Array | Matrix': function (a, b) {
|
||||
const d = lup(a);
|
||||
return _lusolve(d.L, d.U, d.p, null, b);
|
||||
},
|
||||
'SparseMatrix, Array | Matrix': function (a, b) {
|
||||
const d = lup(a);
|
||||
return _lusolve(d.L, d.U, d.p, null, b);
|
||||
},
|
||||
'SparseMatrix, Array | Matrix, number, number': function (a, b, order, threshold) {
|
||||
const d = slu(a, order, threshold);
|
||||
return _lusolve(d.L, d.U, d.p, d.q, b);
|
||||
},
|
||||
'Object, Array | Matrix': function (d, b) {
|
||||
return _lusolve(d.L, d.U, d.p, d.q, b);
|
||||
}
|
||||
});
|
||||
function _toMatrix(a) {
|
||||
if ((0, _is.isMatrix)(a)) {
|
||||
return a;
|
||||
}
|
||||
if ((0, _is.isArray)(a)) {
|
||||
return matrix(a);
|
||||
}
|
||||
throw new TypeError('Invalid Matrix LU decomposition');
|
||||
}
|
||||
function _lusolve(l, u, p, q, b) {
|
||||
// verify decomposition
|
||||
l = _toMatrix(l);
|
||||
u = _toMatrix(u);
|
||||
|
||||
// apply row permutations if needed (b is a DenseMatrix)
|
||||
if (p) {
|
||||
b = solveValidation(l, b, true);
|
||||
b._data = (0, _csIpvec.csIpvec)(p, b._data);
|
||||
}
|
||||
|
||||
// use forward substitution to resolve L * y = b
|
||||
const y = lsolve(l, b);
|
||||
// use backward substitution to resolve U * x = y
|
||||
const x = usolve(u, y);
|
||||
|
||||
// apply column permutations if needed (x is a DenseMatrix)
|
||||
if (q) {
|
||||
x._data = (0, _csIpvec.csIpvec)(q, x._data);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
});
|
||||
167
node_modules/mathjs/lib/cjs/function/algebra/solver/usolve.js
generated
vendored
Normal file
167
node_modules/mathjs/lib/cjs/function/algebra/solver/usolve.js
generated
vendored
Normal file
@@ -0,0 +1,167 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createUsolve = void 0;
|
||||
var _factory = require("../../../utils/factory.js");
|
||||
var _solveValidation = require("./utils/solveValidation.js");
|
||||
const name = 'usolve';
|
||||
const dependencies = ['typed', 'matrix', 'divideScalar', 'multiplyScalar', 'subtractScalar', 'equalScalar', 'DenseMatrix'];
|
||||
const createUsolve = exports.createUsolve = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
matrix,
|
||||
divideScalar,
|
||||
multiplyScalar,
|
||||
subtractScalar,
|
||||
equalScalar,
|
||||
DenseMatrix
|
||||
} = _ref;
|
||||
const solveValidation = (0, _solveValidation.createSolveValidation)({
|
||||
DenseMatrix
|
||||
});
|
||||
|
||||
/**
|
||||
* Finds one solution of a linear equation system by backward substitution. Matrix must be an upper triangular matrix. Throws an error if there's no solution.
|
||||
*
|
||||
* `U * x = b`
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.usolve(U, b)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* const a = [[-2, 3], [2, 1]]
|
||||
* const b = [11, 9]
|
||||
* const x = usolve(a, b) // [[8], [9]]
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* usolveAll, lup, slu, usolve, lusolve
|
||||
*
|
||||
* @param {Matrix, Array} U A N x N matrix or array (U)
|
||||
* @param {Matrix, Array} b A column vector with the b values
|
||||
*
|
||||
* @return {DenseMatrix | Array} A column vector with the linear system solution (x)
|
||||
*/
|
||||
return typed(name, {
|
||||
'SparseMatrix, Array | Matrix': function (m, b) {
|
||||
return _sparseBackwardSubstitution(m, b);
|
||||
},
|
||||
'DenseMatrix, Array | Matrix': function (m, b) {
|
||||
return _denseBackwardSubstitution(m, b);
|
||||
},
|
||||
'Array, Array | Matrix': function (a, b) {
|
||||
const m = matrix(a);
|
||||
const r = _denseBackwardSubstitution(m, b);
|
||||
return r.valueOf();
|
||||
}
|
||||
});
|
||||
function _denseBackwardSubstitution(m, b) {
|
||||
// make b into a column vector
|
||||
b = solveValidation(m, b, true);
|
||||
const bdata = b._data;
|
||||
const rows = m._size[0];
|
||||
const columns = m._size[1];
|
||||
|
||||
// result
|
||||
const x = [];
|
||||
const mdata = m._data;
|
||||
// loop columns backwards
|
||||
for (let j = columns - 1; j >= 0; j--) {
|
||||
// b[j]
|
||||
const bj = bdata[j][0] || 0;
|
||||
// x[j]
|
||||
let xj;
|
||||
if (!equalScalar(bj, 0)) {
|
||||
// value at [j, j]
|
||||
const vjj = mdata[j][j];
|
||||
if (equalScalar(vjj, 0)) {
|
||||
// system cannot be solved
|
||||
throw new Error('Linear system cannot be solved since matrix is singular');
|
||||
}
|
||||
xj = divideScalar(bj, vjj);
|
||||
|
||||
// loop rows
|
||||
for (let i = j - 1; i >= 0; i--) {
|
||||
// update copy of b
|
||||
bdata[i] = [subtractScalar(bdata[i][0] || 0, multiplyScalar(xj, mdata[i][j]))];
|
||||
}
|
||||
} else {
|
||||
// zero value at j
|
||||
xj = 0;
|
||||
}
|
||||
// update x
|
||||
x[j] = [xj];
|
||||
}
|
||||
return new DenseMatrix({
|
||||
data: x,
|
||||
size: [rows, 1]
|
||||
});
|
||||
}
|
||||
function _sparseBackwardSubstitution(m, b) {
|
||||
// make b into a column vector
|
||||
b = solveValidation(m, b, true);
|
||||
const bdata = b._data;
|
||||
const rows = m._size[0];
|
||||
const columns = m._size[1];
|
||||
const values = m._values;
|
||||
const index = m._index;
|
||||
const ptr = m._ptr;
|
||||
|
||||
// result
|
||||
const x = [];
|
||||
|
||||
// loop columns backwards
|
||||
for (let j = columns - 1; j >= 0; j--) {
|
||||
const bj = bdata[j][0] || 0;
|
||||
if (!equalScalar(bj, 0)) {
|
||||
// non-degenerate row, find solution
|
||||
|
||||
let vjj = 0;
|
||||
|
||||
// upper triangular matrix values & index (column j)
|
||||
const jValues = [];
|
||||
const jIndices = [];
|
||||
|
||||
// first & last indeces in column
|
||||
const firstIndex = ptr[j];
|
||||
const lastIndex = ptr[j + 1];
|
||||
|
||||
// values in column, find value at [j, j], loop backwards
|
||||
for (let k = lastIndex - 1; k >= firstIndex; k--) {
|
||||
const i = index[k];
|
||||
|
||||
// check row (rows are not sorted!)
|
||||
if (i === j) {
|
||||
vjj = values[k];
|
||||
} else if (i < j) {
|
||||
// store upper triangular
|
||||
jValues.push(values[k]);
|
||||
jIndices.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
// at this point we must have a value in vjj
|
||||
if (equalScalar(vjj, 0)) {
|
||||
throw new Error('Linear system cannot be solved since matrix is singular');
|
||||
}
|
||||
const xj = divideScalar(bj, vjj);
|
||||
for (let k = 0, lastIndex = jIndices.length; k < lastIndex; k++) {
|
||||
const i = jIndices[k];
|
||||
bdata[i] = [subtractScalar(bdata[i][0], multiplyScalar(xj, jValues[k]))];
|
||||
}
|
||||
x[j] = [xj];
|
||||
} else {
|
||||
// degenerate row, we can choose any value
|
||||
x[j] = [0];
|
||||
}
|
||||
}
|
||||
return new DenseMatrix({
|
||||
data: x,
|
||||
size: [rows, 1]
|
||||
});
|
||||
}
|
||||
});
|
||||
196
node_modules/mathjs/lib/cjs/function/algebra/solver/usolveAll.js
generated
vendored
Normal file
196
node_modules/mathjs/lib/cjs/function/algebra/solver/usolveAll.js
generated
vendored
Normal file
@@ -0,0 +1,196 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createUsolveAll = void 0;
|
||||
var _factory = require("../../../utils/factory.js");
|
||||
var _solveValidation = require("./utils/solveValidation.js");
|
||||
const name = 'usolveAll';
|
||||
const dependencies = ['typed', 'matrix', 'divideScalar', 'multiplyScalar', 'subtractScalar', 'equalScalar', 'DenseMatrix'];
|
||||
const createUsolveAll = exports.createUsolveAll = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
matrix,
|
||||
divideScalar,
|
||||
multiplyScalar,
|
||||
subtractScalar,
|
||||
equalScalar,
|
||||
DenseMatrix
|
||||
} = _ref;
|
||||
const solveValidation = (0, _solveValidation.createSolveValidation)({
|
||||
DenseMatrix
|
||||
});
|
||||
|
||||
/**
|
||||
* Finds all solutions of a linear equation system by backward substitution. Matrix must be an upper triangular matrix.
|
||||
*
|
||||
* `U * x = b`
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.usolveAll(U, b)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* const a = [[-2, 3], [2, 1]]
|
||||
* const b = [11, 9]
|
||||
* const x = usolveAll(a, b) // [ [[8], [9]] ]
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* usolve, lup, slu, usolve, lusolve
|
||||
*
|
||||
* @param {Matrix, Array} U A N x N matrix or array (U)
|
||||
* @param {Matrix, Array} b A column vector with the b values
|
||||
*
|
||||
* @return {DenseMatrix[] | Array[]} An array of affine-independent column vectors (x) that solve the linear system
|
||||
*/
|
||||
return typed(name, {
|
||||
'SparseMatrix, Array | Matrix': function (m, b) {
|
||||
return _sparseBackwardSubstitution(m, b);
|
||||
},
|
||||
'DenseMatrix, Array | Matrix': function (m, b) {
|
||||
return _denseBackwardSubstitution(m, b);
|
||||
},
|
||||
'Array, Array | Matrix': function (a, b) {
|
||||
const m = matrix(a);
|
||||
const R = _denseBackwardSubstitution(m, b);
|
||||
return R.map(r => r.valueOf());
|
||||
}
|
||||
});
|
||||
function _denseBackwardSubstitution(m, b_) {
|
||||
// the algorithm is derived from
|
||||
// https://www.overleaf.com/read/csvgqdxggyjv
|
||||
|
||||
// array of right-hand sides
|
||||
const B = [solveValidation(m, b_, true)._data.map(e => e[0])];
|
||||
const M = m._data;
|
||||
const rows = m._size[0];
|
||||
const columns = m._size[1];
|
||||
|
||||
// loop columns backwards
|
||||
for (let i = columns - 1; i >= 0; i--) {
|
||||
let L = B.length;
|
||||
|
||||
// loop right-hand sides
|
||||
for (let k = 0; k < L; k++) {
|
||||
const b = B[k];
|
||||
if (!equalScalar(M[i][i], 0)) {
|
||||
// non-singular row
|
||||
|
||||
b[i] = divideScalar(b[i], M[i][i]);
|
||||
for (let j = i - 1; j >= 0; j--) {
|
||||
// b[j] -= b[i] * M[j,i]
|
||||
b[j] = subtractScalar(b[j], multiplyScalar(b[i], M[j][i]));
|
||||
}
|
||||
} else if (!equalScalar(b[i], 0)) {
|
||||
// singular row, nonzero RHS
|
||||
|
||||
if (k === 0) {
|
||||
// There is no valid solution
|
||||
return [];
|
||||
} else {
|
||||
// This RHS is invalid but other solutions may still exist
|
||||
B.splice(k, 1);
|
||||
k -= 1;
|
||||
L -= 1;
|
||||
}
|
||||
} else if (k === 0) {
|
||||
// singular row, RHS is zero
|
||||
|
||||
const bNew = [...b];
|
||||
bNew[i] = 1;
|
||||
for (let j = i - 1; j >= 0; j--) {
|
||||
bNew[j] = subtractScalar(bNew[j], M[j][i]);
|
||||
}
|
||||
B.push(bNew);
|
||||
}
|
||||
}
|
||||
}
|
||||
return B.map(x => new DenseMatrix({
|
||||
data: x.map(e => [e]),
|
||||
size: [rows, 1]
|
||||
}));
|
||||
}
|
||||
function _sparseBackwardSubstitution(m, b_) {
|
||||
// array of right-hand sides
|
||||
const B = [solveValidation(m, b_, true)._data.map(e => e[0])];
|
||||
const rows = m._size[0];
|
||||
const columns = m._size[1];
|
||||
const values = m._values;
|
||||
const index = m._index;
|
||||
const ptr = m._ptr;
|
||||
|
||||
// loop columns backwards
|
||||
for (let i = columns - 1; i >= 0; i--) {
|
||||
let L = B.length;
|
||||
|
||||
// loop right-hand sides
|
||||
for (let k = 0; k < L; k++) {
|
||||
const b = B[k];
|
||||
|
||||
// values & indices (column i)
|
||||
const iValues = [];
|
||||
const iIndices = [];
|
||||
|
||||
// first & last indeces in column
|
||||
const firstIndex = ptr[i];
|
||||
const lastIndex = ptr[i + 1];
|
||||
|
||||
// find the value at [i, i]
|
||||
let Mii = 0;
|
||||
for (let j = lastIndex - 1; j >= firstIndex; j--) {
|
||||
const J = index[j];
|
||||
// check row
|
||||
if (J === i) {
|
||||
Mii = values[j];
|
||||
} else if (J < i) {
|
||||
// store upper triangular
|
||||
iValues.push(values[j]);
|
||||
iIndices.push(J);
|
||||
}
|
||||
}
|
||||
if (!equalScalar(Mii, 0)) {
|
||||
// non-singular row
|
||||
|
||||
b[i] = divideScalar(b[i], Mii);
|
||||
|
||||
// loop upper triangular
|
||||
for (let j = 0, lastIndex = iIndices.length; j < lastIndex; j++) {
|
||||
const J = iIndices[j];
|
||||
b[J] = subtractScalar(b[J], multiplyScalar(b[i], iValues[j]));
|
||||
}
|
||||
} else if (!equalScalar(b[i], 0)) {
|
||||
// singular row, nonzero RHS
|
||||
|
||||
if (k === 0) {
|
||||
// There is no valid solution
|
||||
return [];
|
||||
} else {
|
||||
// This RHS is invalid but other solutions may still exist
|
||||
B.splice(k, 1);
|
||||
k -= 1;
|
||||
L -= 1;
|
||||
}
|
||||
} else if (k === 0) {
|
||||
// singular row, RHS is zero
|
||||
|
||||
const bNew = [...b];
|
||||
bNew[i] = 1;
|
||||
|
||||
// loop upper triangular
|
||||
for (let j = 0, lastIndex = iIndices.length; j < lastIndex; j++) {
|
||||
const J = iIndices[j];
|
||||
bNew[J] = subtractScalar(bNew[J], iValues[j]);
|
||||
}
|
||||
B.push(bNew);
|
||||
}
|
||||
}
|
||||
}
|
||||
return B.map(x => new DenseMatrix({
|
||||
data: x.map(e => [e]),
|
||||
size: [rows, 1]
|
||||
}));
|
||||
}
|
||||
});
|
||||
121
node_modules/mathjs/lib/cjs/function/algebra/solver/utils/solveValidation.js
generated
vendored
Normal file
121
node_modules/mathjs/lib/cjs/function/algebra/solver/utils/solveValidation.js
generated
vendored
Normal file
@@ -0,0 +1,121 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createSolveValidation = createSolveValidation;
|
||||
var _is = require("../../../../utils/is.js");
|
||||
var _array = require("../../../../utils/array.js");
|
||||
var _string = require("../../../../utils/string.js");
|
||||
function createSolveValidation(_ref) {
|
||||
let {
|
||||
DenseMatrix
|
||||
} = _ref;
|
||||
/**
|
||||
* Validates matrix and column vector b for backward/forward substitution algorithms.
|
||||
*
|
||||
* @param {Matrix} m An N x N matrix
|
||||
* @param {Array | Matrix} b A column vector
|
||||
* @param {Boolean} copy Return a copy of vector b
|
||||
*
|
||||
* @return {DenseMatrix} Dense column vector b
|
||||
*/
|
||||
return function solveValidation(m, b, copy) {
|
||||
const mSize = m.size();
|
||||
if (mSize.length !== 2) {
|
||||
throw new RangeError('Matrix must be two dimensional (size: ' + (0, _string.format)(mSize) + ')');
|
||||
}
|
||||
const rows = mSize[0];
|
||||
const columns = mSize[1];
|
||||
if (rows !== columns) {
|
||||
throw new RangeError('Matrix must be square (size: ' + (0, _string.format)(mSize) + ')');
|
||||
}
|
||||
let data = [];
|
||||
if ((0, _is.isMatrix)(b)) {
|
||||
const bSize = b.size();
|
||||
const bdata = b._data;
|
||||
|
||||
// 1-dim vector
|
||||
if (bSize.length === 1) {
|
||||
if (bSize[0] !== rows) {
|
||||
throw new RangeError('Dimension mismatch. Matrix columns must match vector length.');
|
||||
}
|
||||
for (let i = 0; i < rows; i++) {
|
||||
data[i] = [bdata[i]];
|
||||
}
|
||||
return new DenseMatrix({
|
||||
data,
|
||||
size: [rows, 1],
|
||||
datatype: b._datatype
|
||||
});
|
||||
}
|
||||
|
||||
// 2-dim column
|
||||
if (bSize.length === 2) {
|
||||
if (bSize[0] !== rows || bSize[1] !== 1) {
|
||||
throw new RangeError('Dimension mismatch. Matrix columns must match vector length.');
|
||||
}
|
||||
if ((0, _is.isDenseMatrix)(b)) {
|
||||
if (copy) {
|
||||
data = [];
|
||||
for (let i = 0; i < rows; i++) {
|
||||
data[i] = [bdata[i][0]];
|
||||
}
|
||||
return new DenseMatrix({
|
||||
data,
|
||||
size: [rows, 1],
|
||||
datatype: b._datatype
|
||||
});
|
||||
}
|
||||
return b;
|
||||
}
|
||||
if ((0, _is.isSparseMatrix)(b)) {
|
||||
for (let i = 0; i < rows; i++) {
|
||||
data[i] = [0];
|
||||
}
|
||||
const values = b._values;
|
||||
const index = b._index;
|
||||
const ptr = b._ptr;
|
||||
for (let k1 = ptr[1], k = ptr[0]; k < k1; k++) {
|
||||
const i = index[k];
|
||||
data[i][0] = values[k];
|
||||
}
|
||||
return new DenseMatrix({
|
||||
data,
|
||||
size: [rows, 1],
|
||||
datatype: b._datatype
|
||||
});
|
||||
}
|
||||
}
|
||||
throw new RangeError('Dimension mismatch. The right side has to be either 1- or 2-dimensional vector.');
|
||||
}
|
||||
if ((0, _is.isArray)(b)) {
|
||||
const bsize = (0, _array.arraySize)(b);
|
||||
if (bsize.length === 1) {
|
||||
if (bsize[0] !== rows) {
|
||||
throw new RangeError('Dimension mismatch. Matrix columns must match vector length.');
|
||||
}
|
||||
for (let i = 0; i < rows; i++) {
|
||||
data[i] = [b[i]];
|
||||
}
|
||||
return new DenseMatrix({
|
||||
data,
|
||||
size: [rows, 1]
|
||||
});
|
||||
}
|
||||
if (bsize.length === 2) {
|
||||
if (bsize[0] !== rows || bsize[1] !== 1) {
|
||||
throw new RangeError('Dimension mismatch. Matrix columns must match vector length.');
|
||||
}
|
||||
for (let i = 0; i < rows; i++) {
|
||||
data[i] = [b[i][0]];
|
||||
}
|
||||
return new DenseMatrix({
|
||||
data,
|
||||
size: [rows, 1]
|
||||
});
|
||||
}
|
||||
throw new RangeError('Dimension mismatch. The right side has to be either 1- or 2-dimensional vector.');
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user