feat:node-modules
This commit is contained in:
382
node_modules/mathjs/lib/cjs/function/algebra/decomposition/lup.js
generated
vendored
Normal file
382
node_modules/mathjs/lib/cjs/function/algebra/decomposition/lup.js
generated
vendored
Normal file
@@ -0,0 +1,382 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createLup = void 0;
|
||||
var _object = require("../../../utils/object.js");
|
||||
var _factory = require("../../../utils/factory.js");
|
||||
const name = 'lup';
|
||||
const dependencies = ['typed', 'matrix', 'abs', 'addScalar', 'divideScalar', 'multiplyScalar', 'subtractScalar', 'larger', 'equalScalar', 'unaryMinus', 'DenseMatrix', 'SparseMatrix', 'Spa'];
|
||||
const createLup = exports.createLup = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
matrix,
|
||||
abs,
|
||||
addScalar,
|
||||
divideScalar,
|
||||
multiplyScalar,
|
||||
subtractScalar,
|
||||
larger,
|
||||
equalScalar,
|
||||
unaryMinus,
|
||||
DenseMatrix,
|
||||
SparseMatrix,
|
||||
Spa
|
||||
} = _ref;
|
||||
/**
|
||||
* Calculate the Matrix LU decomposition with partial pivoting. Matrix `A` is decomposed in two matrices (`L`, `U`) and a
|
||||
* row permutation vector `p` where `A[p,:] = L * U`
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.lup(A)
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* const m = [[2, 1], [1, 4]]
|
||||
* const r = math.lup(m)
|
||||
* // r = {
|
||||
* // L: [[1, 0], [0.5, 1]],
|
||||
* // U: [[2, 1], [0, 3.5]],
|
||||
* // P: [0, 1]
|
||||
* // }
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* slu, lsolve, lusolve, usolve
|
||||
*
|
||||
* @param {Matrix | Array} A A two dimensional matrix or array for which to get the LUP decomposition.
|
||||
*
|
||||
* @return {{L: Array | Matrix, U: Array | Matrix, P: Array.<number>}} The lower triangular matrix, the upper triangular matrix and the permutation matrix.
|
||||
*/
|
||||
return typed(name, {
|
||||
DenseMatrix: function (m) {
|
||||
return _denseLUP(m);
|
||||
},
|
||||
SparseMatrix: function (m) {
|
||||
return _sparseLUP(m);
|
||||
},
|
||||
Array: function (a) {
|
||||
// create dense matrix from array
|
||||
const m = matrix(a);
|
||||
// lup, use matrix implementation
|
||||
const r = _denseLUP(m);
|
||||
// result
|
||||
return {
|
||||
L: r.L.valueOf(),
|
||||
U: r.U.valueOf(),
|
||||
p: r.p
|
||||
};
|
||||
}
|
||||
});
|
||||
function _denseLUP(m) {
|
||||
// rows & columns
|
||||
const rows = m._size[0];
|
||||
const columns = m._size[1];
|
||||
// minimum rows and columns
|
||||
let n = Math.min(rows, columns);
|
||||
// matrix array, clone original data
|
||||
const data = (0, _object.clone)(m._data);
|
||||
// l matrix arrays
|
||||
const ldata = [];
|
||||
const lsize = [rows, n];
|
||||
// u matrix arrays
|
||||
const udata = [];
|
||||
const usize = [n, columns];
|
||||
// vars
|
||||
let i, j, k;
|
||||
// permutation vector
|
||||
const p = [];
|
||||
for (i = 0; i < rows; i++) {
|
||||
p[i] = i;
|
||||
}
|
||||
// loop columns
|
||||
for (j = 0; j < columns; j++) {
|
||||
// skip first column in upper triangular matrix
|
||||
if (j > 0) {
|
||||
// loop rows
|
||||
for (i = 0; i < rows; i++) {
|
||||
// min i,j
|
||||
const min = Math.min(i, j);
|
||||
// v[i, j]
|
||||
let s = 0;
|
||||
// loop up to min
|
||||
for (k = 0; k < min; k++) {
|
||||
// s = l[i, k] - data[k, j]
|
||||
s = addScalar(s, multiplyScalar(data[i][k], data[k][j]));
|
||||
}
|
||||
data[i][j] = subtractScalar(data[i][j], s);
|
||||
}
|
||||
}
|
||||
// row with larger value in cvector, row >= j
|
||||
let pi = j;
|
||||
let pabsv = 0;
|
||||
let vjj = 0;
|
||||
// loop rows
|
||||
for (i = j; i < rows; i++) {
|
||||
// data @ i, j
|
||||
const v = data[i][j];
|
||||
// absolute value
|
||||
const absv = abs(v);
|
||||
// value is greater than pivote value
|
||||
if (larger(absv, pabsv)) {
|
||||
// store row
|
||||
pi = i;
|
||||
// update max value
|
||||
pabsv = absv;
|
||||
// value @ [j, j]
|
||||
vjj = v;
|
||||
}
|
||||
}
|
||||
// swap rows (j <-> pi)
|
||||
if (j !== pi) {
|
||||
// swap values j <-> pi in p
|
||||
p[j] = [p[pi], p[pi] = p[j]][0];
|
||||
// swap j <-> pi in data
|
||||
DenseMatrix._swapRows(j, pi, data);
|
||||
}
|
||||
// check column is in lower triangular matrix
|
||||
if (j < rows) {
|
||||
// loop rows (lower triangular matrix)
|
||||
for (i = j + 1; i < rows; i++) {
|
||||
// value @ i, j
|
||||
const vij = data[i][j];
|
||||
if (!equalScalar(vij, 0)) {
|
||||
// update data
|
||||
data[i][j] = divideScalar(data[i][j], vjj);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// loop columns
|
||||
for (j = 0; j < columns; j++) {
|
||||
// loop rows
|
||||
for (i = 0; i < rows; i++) {
|
||||
// initialize row in arrays
|
||||
if (j === 0) {
|
||||
// check row exists in upper triangular matrix
|
||||
if (i < columns) {
|
||||
// U
|
||||
udata[i] = [];
|
||||
}
|
||||
// L
|
||||
ldata[i] = [];
|
||||
}
|
||||
// check we are in the upper triangular matrix
|
||||
if (i < j) {
|
||||
// check row exists in upper triangular matrix
|
||||
if (i < columns) {
|
||||
// U
|
||||
udata[i][j] = data[i][j];
|
||||
}
|
||||
// check column exists in lower triangular matrix
|
||||
if (j < rows) {
|
||||
// L
|
||||
ldata[i][j] = 0;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// diagonal value
|
||||
if (i === j) {
|
||||
// check row exists in upper triangular matrix
|
||||
if (i < columns) {
|
||||
// U
|
||||
udata[i][j] = data[i][j];
|
||||
}
|
||||
// check column exists in lower triangular matrix
|
||||
if (j < rows) {
|
||||
// L
|
||||
ldata[i][j] = 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// check row exists in upper triangular matrix
|
||||
if (i < columns) {
|
||||
// U
|
||||
udata[i][j] = 0;
|
||||
}
|
||||
// check column exists in lower triangular matrix
|
||||
if (j < rows) {
|
||||
// L
|
||||
ldata[i][j] = data[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
// l matrix
|
||||
const l = new DenseMatrix({
|
||||
data: ldata,
|
||||
size: lsize
|
||||
});
|
||||
// u matrix
|
||||
const u = new DenseMatrix({
|
||||
data: udata,
|
||||
size: usize
|
||||
});
|
||||
// p vector
|
||||
const pv = [];
|
||||
for (i = 0, n = p.length; i < n; i++) {
|
||||
pv[p[i]] = i;
|
||||
}
|
||||
// return matrices
|
||||
return {
|
||||
L: l,
|
||||
U: u,
|
||||
p: pv,
|
||||
toString: function () {
|
||||
return 'L: ' + this.L.toString() + '\nU: ' + this.U.toString() + '\nP: ' + this.p;
|
||||
}
|
||||
};
|
||||
}
|
||||
function _sparseLUP(m) {
|
||||
// rows & columns
|
||||
const rows = m._size[0];
|
||||
const columns = m._size[1];
|
||||
// minimum rows and columns
|
||||
const n = Math.min(rows, columns);
|
||||
// matrix arrays (will not be modified, thanks to permutation vector)
|
||||
const values = m._values;
|
||||
const index = m._index;
|
||||
const ptr = m._ptr;
|
||||
// l matrix arrays
|
||||
const lvalues = [];
|
||||
const lindex = [];
|
||||
const lptr = [];
|
||||
const lsize = [rows, n];
|
||||
// u matrix arrays
|
||||
const uvalues = [];
|
||||
const uindex = [];
|
||||
const uptr = [];
|
||||
const usize = [n, columns];
|
||||
// vars
|
||||
let i, j, k;
|
||||
// permutation vectors, (current index -> original index) and (original index -> current index)
|
||||
const pvCo = [];
|
||||
const pvOc = [];
|
||||
for (i = 0; i < rows; i++) {
|
||||
pvCo[i] = i;
|
||||
pvOc[i] = i;
|
||||
}
|
||||
// swap indices in permutation vectors (condition x < y)!
|
||||
const swapIndeces = function (x, y) {
|
||||
// find pv indeces getting data from x and y
|
||||
const kx = pvOc[x];
|
||||
const ky = pvOc[y];
|
||||
// update permutation vector current -> original
|
||||
pvCo[kx] = y;
|
||||
pvCo[ky] = x;
|
||||
// update permutation vector original -> current
|
||||
pvOc[x] = ky;
|
||||
pvOc[y] = kx;
|
||||
};
|
||||
// loop columns
|
||||
for (j = 0; j < columns; j++) {
|
||||
// sparse accumulator
|
||||
const spa = new Spa();
|
||||
// check lower triangular matrix has a value @ column j
|
||||
if (j < rows) {
|
||||
// update ptr
|
||||
lptr.push(lvalues.length);
|
||||
// first value in j column for lower triangular matrix
|
||||
lvalues.push(1);
|
||||
lindex.push(j);
|
||||
}
|
||||
// update ptr
|
||||
uptr.push(uvalues.length);
|
||||
// k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1]
|
||||
const k0 = ptr[j];
|
||||
const k1 = ptr[j + 1];
|
||||
// copy column j into sparse accumulator
|
||||
for (k = k0; k < k1; k++) {
|
||||
// row
|
||||
i = index[k];
|
||||
// copy column values into sparse accumulator (use permutation vector)
|
||||
spa.set(pvCo[i], values[k]);
|
||||
}
|
||||
// skip first column in upper triangular matrix
|
||||
if (j > 0) {
|
||||
// loop rows in column j (above diagonal)
|
||||
spa.forEach(0, j - 1, function (k, vkj) {
|
||||
// loop rows in column k (L)
|
||||
SparseMatrix._forEachRow(k, lvalues, lindex, lptr, function (i, vik) {
|
||||
// check row is below k
|
||||
if (i > k) {
|
||||
// update spa value
|
||||
spa.accumulate(i, unaryMinus(multiplyScalar(vik, vkj)));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
// row with larger value in spa, row >= j
|
||||
let pi = j;
|
||||
let vjj = spa.get(j);
|
||||
let pabsv = abs(vjj);
|
||||
// loop values in spa (order by row, below diagonal)
|
||||
spa.forEach(j + 1, rows - 1, function (x, v) {
|
||||
// absolute value
|
||||
const absv = abs(v);
|
||||
// value is greater than pivote value
|
||||
if (larger(absv, pabsv)) {
|
||||
// store row
|
||||
pi = x;
|
||||
// update max value
|
||||
pabsv = absv;
|
||||
// value @ [j, j]
|
||||
vjj = v;
|
||||
}
|
||||
});
|
||||
// swap rows (j <-> pi)
|
||||
if (j !== pi) {
|
||||
// swap values j <-> pi in L
|
||||
SparseMatrix._swapRows(j, pi, lsize[1], lvalues, lindex, lptr);
|
||||
// swap values j <-> pi in U
|
||||
SparseMatrix._swapRows(j, pi, usize[1], uvalues, uindex, uptr);
|
||||
// swap values in spa
|
||||
spa.swap(j, pi);
|
||||
// update permutation vector (swap values @ j, pi)
|
||||
swapIndeces(j, pi);
|
||||
}
|
||||
// loop values in spa (order by row)
|
||||
spa.forEach(0, rows - 1, function (x, v) {
|
||||
// check we are above diagonal
|
||||
if (x <= j) {
|
||||
// update upper triangular matrix
|
||||
uvalues.push(v);
|
||||
uindex.push(x);
|
||||
} else {
|
||||
// update value
|
||||
v = divideScalar(v, vjj);
|
||||
// check value is non zero
|
||||
if (!equalScalar(v, 0)) {
|
||||
// update lower triangular matrix
|
||||
lvalues.push(v);
|
||||
lindex.push(x);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
// update ptrs
|
||||
uptr.push(uvalues.length);
|
||||
lptr.push(lvalues.length);
|
||||
|
||||
// return matrices
|
||||
return {
|
||||
L: new SparseMatrix({
|
||||
values: lvalues,
|
||||
index: lindex,
|
||||
ptr: lptr,
|
||||
size: lsize
|
||||
}),
|
||||
U: new SparseMatrix({
|
||||
values: uvalues,
|
||||
index: uindex,
|
||||
ptr: uptr,
|
||||
size: usize
|
||||
}),
|
||||
p: pvCo,
|
||||
toString: function () {
|
||||
return 'L: ' + this.L.toString() + '\nU: ' + this.U.toString() + '\nP: ' + this.p;
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
229
node_modules/mathjs/lib/cjs/function/algebra/decomposition/qr.js
generated
vendored
Normal file
229
node_modules/mathjs/lib/cjs/function/algebra/decomposition/qr.js
generated
vendored
Normal file
@@ -0,0 +1,229 @@
|
||||
"use strict";
|
||||
|
||||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createQr = void 0;
|
||||
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
||||
var _factory = require("../../../utils/factory.js");
|
||||
const name = 'qr';
|
||||
const dependencies = ['typed', 'matrix', 'zeros', 'identity', 'isZero', 'equal', 'sign', 'sqrt', 'conj', 'unaryMinus', 'addScalar', 'divideScalar', 'multiplyScalar', 'subtractScalar', 'complex'];
|
||||
const createQr = exports.createQr = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
matrix,
|
||||
zeros,
|
||||
identity,
|
||||
isZero,
|
||||
equal,
|
||||
sign,
|
||||
sqrt,
|
||||
conj,
|
||||
unaryMinus,
|
||||
addScalar,
|
||||
divideScalar,
|
||||
multiplyScalar,
|
||||
subtractScalar,
|
||||
complex
|
||||
} = _ref;
|
||||
/**
|
||||
* Calculate the Matrix QR decomposition. Matrix `A` is decomposed in
|
||||
* two matrices (`Q`, `R`) where `Q` is an
|
||||
* orthogonal matrix and `R` is an upper triangular matrix.
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.qr(A)
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* const m = [
|
||||
* [1, -1, 4],
|
||||
* [1, 4, -2],
|
||||
* [1, 4, 2],
|
||||
* [1, -1, 0]
|
||||
* ]
|
||||
* const result = math.qr(m)
|
||||
* // r = {
|
||||
* // Q: [
|
||||
* // [0.5, -0.5, 0.5],
|
||||
* // [0.5, 0.5, -0.5],
|
||||
* // [0.5, 0.5, 0.5],
|
||||
* // [0.5, -0.5, -0.5],
|
||||
* // ],
|
||||
* // R: [
|
||||
* // [2, 3, 2],
|
||||
* // [0, 5, -2],
|
||||
* // [0, 0, 4],
|
||||
* // [0, 0, 0]
|
||||
* // ]
|
||||
* // }
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* lup, lusolve
|
||||
*
|
||||
* @param {Matrix | Array} A A two dimensional matrix or array
|
||||
* for which to get the QR decomposition.
|
||||
*
|
||||
* @return {{Q: Array | Matrix, R: Array | Matrix}} Q: the orthogonal
|
||||
* matrix and R: the upper triangular matrix
|
||||
*/
|
||||
return (0, _extends2.default)(typed(name, {
|
||||
DenseMatrix: function (m) {
|
||||
return _denseQR(m);
|
||||
},
|
||||
SparseMatrix: function (m) {
|
||||
return _sparseQR(m);
|
||||
},
|
||||
Array: function (a) {
|
||||
// create dense matrix from array
|
||||
const m = matrix(a);
|
||||
// lup, use matrix implementation
|
||||
const r = _denseQR(m);
|
||||
// result
|
||||
return {
|
||||
Q: r.Q.valueOf(),
|
||||
R: r.R.valueOf()
|
||||
};
|
||||
}
|
||||
}), {
|
||||
_denseQRimpl
|
||||
});
|
||||
function _denseQRimpl(m) {
|
||||
// rows & columns (m x n)
|
||||
const rows = m._size[0]; // m
|
||||
const cols = m._size[1]; // n
|
||||
|
||||
const Q = identity([rows], 'dense');
|
||||
const Qdata = Q._data;
|
||||
const R = m.clone();
|
||||
const Rdata = R._data;
|
||||
|
||||
// vars
|
||||
let i, j, k;
|
||||
const w = zeros([rows], '');
|
||||
for (k = 0; k < Math.min(cols, rows); ++k) {
|
||||
/*
|
||||
* **k-th Household matrix**
|
||||
*
|
||||
* The matrix I - 2*v*transpose(v)
|
||||
* x = first column of A
|
||||
* x1 = first element of x
|
||||
* alpha = x1 / |x1| * |x|
|
||||
* e1 = tranpose([1, 0, 0, ...])
|
||||
* u = x - alpha * e1
|
||||
* v = u / |u|
|
||||
*
|
||||
* Household matrix = I - 2 * v * tranpose(v)
|
||||
*
|
||||
* * Initially Q = I and R = A.
|
||||
* * Household matrix is a reflection in a plane normal to v which
|
||||
* will zero out all but the top right element in R.
|
||||
* * Appplying reflection to both Q and R will not change product.
|
||||
* * Repeat this process on the (1,1) minor to get R as an upper
|
||||
* triangular matrix.
|
||||
* * Reflections leave the magnitude of the columns of Q unchanged
|
||||
* so Q remains othoganal.
|
||||
*
|
||||
*/
|
||||
|
||||
const pivot = Rdata[k][k];
|
||||
const sgn = unaryMinus(equal(pivot, 0) ? 1 : sign(pivot));
|
||||
const conjSgn = conj(sgn);
|
||||
let alphaSquared = 0;
|
||||
for (i = k; i < rows; i++) {
|
||||
alphaSquared = addScalar(alphaSquared, multiplyScalar(Rdata[i][k], conj(Rdata[i][k])));
|
||||
}
|
||||
const alpha = multiplyScalar(sgn, sqrt(alphaSquared));
|
||||
if (!isZero(alpha)) {
|
||||
// first element in vector u
|
||||
const u1 = subtractScalar(pivot, alpha);
|
||||
|
||||
// w = v * u1 / |u| (only elements k to (rows-1) are used)
|
||||
w[k] = 1;
|
||||
for (i = k + 1; i < rows; i++) {
|
||||
w[i] = divideScalar(Rdata[i][k], u1);
|
||||
}
|
||||
|
||||
// tau = - conj(u1 / alpha)
|
||||
const tau = unaryMinus(conj(divideScalar(u1, alpha)));
|
||||
let s;
|
||||
|
||||
/*
|
||||
* tau and w have been choosen so that
|
||||
*
|
||||
* 2 * v * tranpose(v) = tau * w * tranpose(w)
|
||||
*/
|
||||
|
||||
/*
|
||||
* -- calculate R = R - tau * w * tranpose(w) * R --
|
||||
* Only do calculation with rows k to (rows-1)
|
||||
* Additionally columns 0 to (k-1) will not be changed by this
|
||||
* multiplication so do not bother recalculating them
|
||||
*/
|
||||
for (j = k; j < cols; j++) {
|
||||
s = 0.0;
|
||||
|
||||
// calculate jth element of [tranpose(w) * R]
|
||||
for (i = k; i < rows; i++) {
|
||||
s = addScalar(s, multiplyScalar(conj(w[i]), Rdata[i][j]));
|
||||
}
|
||||
|
||||
// calculate the jth element of [tau * transpose(w) * R]
|
||||
s = multiplyScalar(s, tau);
|
||||
for (i = k; i < rows; i++) {
|
||||
Rdata[i][j] = multiplyScalar(subtractScalar(Rdata[i][j], multiplyScalar(w[i], s)), conjSgn);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* -- calculate Q = Q - tau * Q * w * transpose(w) --
|
||||
* Q is a square matrix (rows x rows)
|
||||
* Only do calculation with columns k to (rows-1)
|
||||
* Additionally rows 0 to (k-1) will not be changed by this
|
||||
* multiplication so do not bother recalculating them
|
||||
*/
|
||||
for (i = 0; i < rows; i++) {
|
||||
s = 0.0;
|
||||
|
||||
// calculate ith element of [Q * w]
|
||||
for (j = k; j < rows; j++) {
|
||||
s = addScalar(s, multiplyScalar(Qdata[i][j], w[j]));
|
||||
}
|
||||
|
||||
// calculate the ith element of [tau * Q * w]
|
||||
s = multiplyScalar(s, tau);
|
||||
for (j = k; j < rows; ++j) {
|
||||
Qdata[i][j] = divideScalar(subtractScalar(Qdata[i][j], multiplyScalar(s, conj(w[j]))), conjSgn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// return matrices
|
||||
return {
|
||||
Q,
|
||||
R,
|
||||
toString: function () {
|
||||
return 'Q: ' + this.Q.toString() + '\nR: ' + this.R.toString();
|
||||
}
|
||||
};
|
||||
}
|
||||
function _denseQR(m) {
|
||||
const ret = _denseQRimpl(m);
|
||||
const Rdata = ret.R._data;
|
||||
if (m._data.length > 0) {
|
||||
const zero = Rdata[0][0].type === 'Complex' ? complex(0) : 0;
|
||||
for (let i = 0; i < Rdata.length; ++i) {
|
||||
for (let j = 0; j < i && j < (Rdata[0] || []).length; ++j) {
|
||||
Rdata[i][j] = zero;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
function _sparseQR(m) {
|
||||
throw new Error('qr not implemented for sparse matrices yet');
|
||||
}
|
||||
});
|
||||
76
node_modules/mathjs/lib/cjs/function/algebra/decomposition/schur.js
generated
vendored
Normal file
76
node_modules/mathjs/lib/cjs/function/algebra/decomposition/schur.js
generated
vendored
Normal file
@@ -0,0 +1,76 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createSchur = void 0;
|
||||
var _factory = require("../../../utils/factory.js");
|
||||
const name = 'schur';
|
||||
const dependencies = ['typed', 'matrix', 'identity', 'multiply', 'qr', 'norm', 'subtract'];
|
||||
const createSchur = exports.createSchur = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
matrix,
|
||||
identity,
|
||||
multiply,
|
||||
qr,
|
||||
norm,
|
||||
subtract
|
||||
} = _ref;
|
||||
/**
|
||||
*
|
||||
* Performs a real Schur decomposition of the real matrix A = UTU' where U is orthogonal
|
||||
* and T is upper quasi-triangular.
|
||||
* https://en.wikipedia.org/wiki/Schur_decomposition
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.schur(A)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* const A = [[1, 0], [-4, 3]]
|
||||
* math.schur(A) // returns {T: [[3, 4], [0, 1]], R: [[0, 1], [-1, 0]]}
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* sylvester, lyap, qr
|
||||
*
|
||||
* @param {Array | Matrix} A Matrix A
|
||||
* @return {{U: Array | Matrix, T: Array | Matrix}} Object containing both matrix U and T of the Schur Decomposition A=UTU'
|
||||
*/
|
||||
return typed(name, {
|
||||
Array: function (X) {
|
||||
const r = _schur(matrix(X));
|
||||
return {
|
||||
U: r.U.valueOf(),
|
||||
T: r.T.valueOf()
|
||||
};
|
||||
},
|
||||
Matrix: function (X) {
|
||||
return _schur(X);
|
||||
}
|
||||
});
|
||||
function _schur(X) {
|
||||
const n = X.size()[0];
|
||||
let A = X;
|
||||
let U = identity(n);
|
||||
let k = 0;
|
||||
let A0;
|
||||
do {
|
||||
A0 = A;
|
||||
const QR = qr(A);
|
||||
const Q = QR.Q;
|
||||
const R = QR.R;
|
||||
A = multiply(R, Q);
|
||||
U = multiply(U, Q);
|
||||
if (k++ > 100) {
|
||||
break;
|
||||
}
|
||||
} while (norm(subtract(A, A0)) > 1e-4);
|
||||
return {
|
||||
U,
|
||||
T: A
|
||||
};
|
||||
}
|
||||
});
|
||||
107
node_modules/mathjs/lib/cjs/function/algebra/decomposition/slu.js
generated
vendored
Normal file
107
node_modules/mathjs/lib/cjs/function/algebra/decomposition/slu.js
generated
vendored
Normal file
@@ -0,0 +1,107 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.createSlu = void 0;
|
||||
var _number = require("../../../utils/number.js");
|
||||
var _factory = require("../../../utils/factory.js");
|
||||
var _csSqr = require("../sparse/csSqr.js");
|
||||
var _csLu = require("../sparse/csLu.js");
|
||||
const name = 'slu';
|
||||
const dependencies = ['typed', 'abs', 'add', 'multiply', 'transpose', 'divideScalar', 'subtract', 'larger', 'largerEq', 'SparseMatrix'];
|
||||
const createSlu = exports.createSlu = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
||||
let {
|
||||
typed,
|
||||
abs,
|
||||
add,
|
||||
multiply,
|
||||
transpose,
|
||||
divideScalar,
|
||||
subtract,
|
||||
larger,
|
||||
largerEq,
|
||||
SparseMatrix
|
||||
} = _ref;
|
||||
const csSqr = (0, _csSqr.createCsSqr)({
|
||||
add,
|
||||
multiply,
|
||||
transpose
|
||||
});
|
||||
const csLu = (0, _csLu.createCsLu)({
|
||||
abs,
|
||||
divideScalar,
|
||||
multiply,
|
||||
subtract,
|
||||
larger,
|
||||
largerEq,
|
||||
SparseMatrix
|
||||
});
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* `P * A * Q = L * U`
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.slu(A, order, threshold)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* const A = math.sparse([[4,3], [6, 3]])
|
||||
* math.slu(A, 1, 0.001)
|
||||
* // returns:
|
||||
* // {
|
||||
* // L: [[1, 0], [1.5, 1]]
|
||||
* // U: [[4, 3], [0, -1.5]]
|
||||
* // p: [0, 1]
|
||||
* // q: [0, 1]
|
||||
* // }
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* lup, lsolve, usolve, lusolve
|
||||
*
|
||||
* @param {SparseMatrix} A A two dimensional sparse matrix for which to get the LU decomposition.
|
||||
* @param {Number} order The Symbolic Ordering and Analysis order:
|
||||
* 0 - Natural ordering, no permutation vector q is returned
|
||||
* 1 - Matrix must be square, symbolic ordering and analisis is performed on M = A + A'
|
||||
* 2 - Symbolic ordering and analisis is performed on M = A' * A. Dense columns from A' are dropped, A recreated from A'.
|
||||
* This is appropriatefor LU factorization of unsymmetric matrices.
|
||||
* 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.
|
||||
* A dense row is a row with more than 10*sqr(columns) entries.
|
||||
* @param {Number} threshold Partial pivoting threshold (1 for partial pivoting)
|
||||
*
|
||||
* @return {Object} The lower triangular matrix, the upper triangular matrix and the permutation vectors.
|
||||
*/
|
||||
return typed(name, {
|
||||
'SparseMatrix, number, number': function (a, order, threshold) {
|
||||
// verify order
|
||||
if (!(0, _number.isInteger)(order) || order < 0 || order > 3) {
|
||||
throw new Error('Symbolic Ordering and Analysis order must be an integer number in the interval [0, 3]');
|
||||
}
|
||||
// verify threshold
|
||||
if (threshold < 0 || threshold > 1) {
|
||||
throw new Error('Partial pivoting threshold must be a number from 0 to 1');
|
||||
}
|
||||
|
||||
// perform symbolic ordering and analysis
|
||||
const s = csSqr(order, a, false);
|
||||
|
||||
// perform lu decomposition
|
||||
const f = csLu(a, s, threshold);
|
||||
|
||||
// return decomposition
|
||||
return {
|
||||
L: f.L,
|
||||
U: f.U,
|
||||
p: f.pinv,
|
||||
q: s.q,
|
||||
toString: function () {
|
||||
return 'L: ' + this.L.toString() + '\nU: ' + this.U.toString() + '\np: ' + this.p.toString() + (this.q ? '\nq: ' + this.q.toString() : '') + '\n';
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user