Michal Grňo ba4ff2f9c9
Add usolveAll and lsolveAll methods (#1916)
* refactor solveValidation

* refactor usolve

* usolve algorithm implemented (for square mat.)

* added lsolve, consistent return type, fixed tests

* fixed lusolve and its tests, fixed linting issues

* added tests for usolve&lsolve, try-catch in lusolve

* put changes into separate files (u-/lsolveAll), revert changes to u-, l- and lusolve

* made *solveAll return [] for non-solvable, implemented sparse algorithms

* improved documentation for *solve(All)

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
2020-08-29 13:11:16 +02:00

156 lines
4.0 KiB
JavaScript

// test lsolveAll
import assert from 'assert'
import approx from '../../../../../tools/approx'
import math from '../../../../../src/bundleAny'
describe('lsolveAll', function () {
it('should solve linear system 4 x 4, arrays', function () {
const m =
[
[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 1, 1, 0],
[1, 1, 1, 1]
]
const b = [1, 2, 3, 4]
const x = math.lsolveAll(m, b)
approx.deepEqual(x, [[[1], [1], [1], [1]]])
})
it('should solve linear system 4 x 4, array and column array', function () {
const m =
[
[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 1, 1, 0],
[1, 1, 1, 1]
]
const b = [
[1],
[2],
[3],
[4]
]
const x = math.lsolveAll(m, b)
approx.deepEqual(x, [[[1], [1], [1], [1]]])
})
it('should solve linear system 4 x 4, matrices', function () {
const m = math.matrix(
[
[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 1, 1, 0],
[1, 1, 1, 1]
])
const b = math.matrix([1, 2, 3, 4])
const x = math.lsolveAll(m, b)
assert(x[0] instanceof math.Matrix)
approx.deepEqual(x, [math.matrix([[1], [1], [1], [1]])])
})
it('should solve linear system 4 x 4, sparse matrices', function () {
const m = math.sparse(
[
[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 1, 1, 0],
[1, 1, 1, 1]
])
const b = math.matrix([[1], [2], [3], [4]], 'sparse')
const x = math.lsolveAll(m, b)
assert(x[0] instanceof math.Matrix)
approx.deepEqual(x, [math.matrix([[1], [1], [1], [1]])])
})
it('should solve linear system 4 x 4, matrix and column matrix', function () {
const m = math.matrix(
[
[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 1, 1, 0],
[1, 1, 1, 1]
])
const b = math.matrix([
[1],
[2],
[3],
[4]
])
const x = math.lsolveAll(m, b)
assert(x[0] instanceof math.Matrix)
approx.deepEqual(x, [math.matrix([[1], [1], [1], [1]])])
})
it('should solve linear system 4 x 4, sparse matrix and column matrix', function () {
const m = math.matrix(
[
[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 1, 1, 0],
[1, 1, 1, 1]
], 'sparse')
const b = math.matrix([
[1],
[2],
[3],
[4]
], 'sparse')
const x = math.lsolveAll(m, b)
assert(x[0] instanceof math.Matrix)
approx.deepEqual(x, [math.matrix([[1], [1], [1], [1]])])
})
it('should return an empty array when there is no solution', function () {
assert.deepStrictEqual([], math.lsolveAll([[1, 1], [0, 0]], [1, 1]))
assert.deepStrictEqual([], math.lsolveAll(math.matrix([[1, 1], [0, 0]], 'dense'), [1, 1]))
assert.deepStrictEqual([], math.lsolveAll(math.matrix([[1, 1], [0, 0]], 'sparse'), [1, 1]))
})
it('should solve systems with singular dense matrices', function () {
approx.deepEqual(
math.lsolveAll([[2, 0, 0], [1, 0, 0], [-1, 1, 1]], [4, 2, 1]),
[[[2], [0], [3]], [[2], [1], [2]]]
)
approx.deepEqual(
math.lsolveAll([[0, 0, 0], [1, 1, 0], [2, 1, 0]], [0, 2, 2]),
[[[0], [2], [0]], [[0], [2], [1]]]
)
approx.deepEqual(
math.lsolveAll([[0, 0, 0], [1, 1, 0], [1, 1, 0]], [0, 2, 2]),
[[[0], [2], [0]], [[1], [1], [0]], [[0], [2], [1]]]
)
})
it('should solve systems with singular sparse matrices', function () {
approx.deepEqual(
math.lsolveAll(math.matrix([[2, 0, 0], [1, 0, 0], [-1, 1, 1]], 'sparse'), [4, 2, 1]),
[math.matrix([[2], [0], [3]]), math.matrix([[2], [1], [2]])]
)
approx.deepEqual(
math.lsolveAll(math.matrix([[0, 0, 0], [1, 1, 0], [2, 1, 0]], 'sparse'), [0, 2, 2]),
[math.matrix([[0], [2], [0]]), math.matrix([[0], [2], [1]])]
)
approx.deepEqual(
math.lsolveAll(math.matrix([[0, 0, 0], [1, 1, 0], [1, 1, 0]], 'sparse'), [0, 2, 2]),
[math.matrix([[0], [2], [0]]), math.matrix([[1], [1], [0]]), math.matrix([[0], [2], [1]])]
)
})
})