mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
106 lines
3.1 KiB
JavaScript
106 lines
3.1 KiB
JavaScript
import assert from 'assert'
|
|
import approx from '../../../../../tools/approx'
|
|
import math from '../../../../../src/bundleAny'
|
|
import { csPermute } from '../../../../../src/function/algebra/sparse/csPermute'
|
|
import { createCsLu } from '../../../../../src/function/algebra/sparse/csLu'
|
|
import { createCsSqr } from '../../../../../src/function/algebra/sparse/csSqr'
|
|
|
|
const { abs, add, divideScalar, multiply, subtract, larger, largerEq, transpose, SparseMatrix } = math
|
|
|
|
const csLu = createCsLu({ abs, divideScalar, multiply, subtract, larger, largerEq, SparseMatrix })
|
|
const csSqr = createCsSqr({ add, multiply, transpose })
|
|
|
|
describe('csLu', function () {
|
|
it('should decompose matrix, 2 x 2, no symbolic ordering and analysis, partial pivoting', function () {
|
|
const m = math.sparse([[2, 1], [1, 4]])
|
|
|
|
// partial pivoting
|
|
const r = csLu(m, null, 1)
|
|
|
|
// L
|
|
assert.deepStrictEqual(r.L.valueOf(), [[1, 0], [0.5, 1]])
|
|
// U
|
|
assert.deepStrictEqual(r.U.valueOf(), [[2, 1], [0, 3.5]])
|
|
// P
|
|
assert.deepStrictEqual(r.pinv, [0, 1])
|
|
// verify
|
|
approx.deepEqual(csPermute(m, r.pinv, null, true), math.multiply(r.L, r.U))
|
|
})
|
|
|
|
it('should decompose matrix, 4 x 4, natural ordering (order=0), partial pivoting', function () {
|
|
const m = math.sparse(
|
|
[
|
|
[4.5, 0, 3.2, 0],
|
|
[3.1, 2.9, 0, 0.9],
|
|
[0, 1.7, 3, 0],
|
|
[3.5, 0.4, 0, 1]
|
|
])
|
|
|
|
// symbolic ordering and analysis, order = 0
|
|
const s = csSqr(0, m, false)
|
|
|
|
// partial pivoting
|
|
const r = csLu(m, s, 1)
|
|
|
|
// verify
|
|
approx.deepEqual(csPermute(m, r.pinv, s.q, true).valueOf(), math.multiply(r.L, r.U).valueOf())
|
|
})
|
|
|
|
it('should decompose matrix, 4 x 4, amd(A+A\') (order=1), partial pivoting', function () {
|
|
const m = math.sparse(
|
|
[
|
|
[4.5, 0, 3.2, 0],
|
|
[3.1, 2.9, 0, 0.9],
|
|
[0, 1.7, 3, 0],
|
|
[3.5, 0.4, 0, 1]
|
|
])
|
|
|
|
// symbolic ordering and analysis, order = 1
|
|
const s = csSqr(1, m, false)
|
|
|
|
// partial pivoting
|
|
const r = csLu(m, s, 1)
|
|
|
|
// verify
|
|
approx.deepEqual(csPermute(m, r.pinv, s.q, true).valueOf(), math.multiply(r.L, r.U).valueOf())
|
|
})
|
|
|
|
it('should decompose matrix, 4 x 4, amd(A\'*A) (order=2), partial pivoting', function () {
|
|
const m = math.sparse(
|
|
[
|
|
[4.5, 0, 3.2, 0],
|
|
[3.1, 2.9, 0, 0.9],
|
|
[0, 1.7, 3, 0],
|
|
[3.5, 0.4, 0, 1]
|
|
])
|
|
|
|
// symbolic ordering and analysis, order = 2
|
|
const s = csSqr(2, m, false)
|
|
|
|
// partial pivoting
|
|
const r = csLu(m, s, 1)
|
|
|
|
// verify
|
|
approx.deepEqual(csPermute(m, r.pinv, s.q, true).valueOf(), math.multiply(r.L, r.U).valueOf())
|
|
})
|
|
|
|
it('should decompose matrix, 4 x 4, amd(A\'*A) (order=3), partial pivoting', function () {
|
|
const m = math.sparse(
|
|
[
|
|
[4.5, 0, 3.2, 0],
|
|
[3.1, 2.9, 0, 0.9],
|
|
[0, 1.7, 3, 0],
|
|
[3.5, 0.4, 0, 1]
|
|
])
|
|
|
|
// symbolic ordering and analysis, order = 3
|
|
const s = csSqr(3, m, false)
|
|
|
|
// partial pivoting
|
|
const r = csLu(m, s, 1)
|
|
|
|
// verify
|
|
approx.deepEqual(csPermute(m, r.pinv, s.q, true).valueOf(), math.multiply(r.L, r.U).valueOf())
|
|
})
|
|
})
|