mathjs/test/function/algebra/solver/usolve.test.js
greenkeeper[bot] c5971b371a Update standard to the latest version 🚀 (#1226)
* chore(package): update standard to version 12.0.0

* update to new lint version with --fix

I believe this mainly adds whitespace to `{}`'s.

* Replace assert.equal with assert.strictEqual

This breaks a lot of tests which I will endevour to fix in the next
commits.

* Fix most errors due to assert.strictEquals

Some instances of `strictEquals` are replaced by `deepEquals`.
`toString` has been used to make some string comparisions explicit.
Tests will still fail untill #1236 and #1237 are fixed.

* Fix assertion erros due to -0

With node 10, assert.strictEqual no longer considers `0 === -0`.
I missed these first time round as I was using node 8.

* Put toString correct side of bracket

I was converting the constructor to a string rather
than the result of the computation. Oops.

* Fixed #1236: quantileSeq has inconsistant return

* Update package-lock

* Fixed #1237: norm sometimes returning a complex number instead of number

* Fix cli tests

* More changes for standardjs, and fixes in unit tests
2018-09-08 16:33:58 +02:00

121 lines
3.0 KiB
JavaScript

// test usolve
const assert = require('assert')
const approx = require('../../../../tools/approx')
const math = require('../../../../src/main')
describe('usolve', function () {
it('should solve linear system 4 x 4, arrays', function () {
const m =
[
[1, 1, 1, 1],
[0, 1, 1, 1],
[0, 0, 1, 1],
[0, 0, 0, 1]
]
const b = [1, 2, 3, 4]
const x = math.usolve(m, b)
approx.deepEqual(x, [[-1], [-1], [-1], [4]])
})
it('should solve linear system 4 x 4, array and column array', function () {
const m =
[
[1, 1, 1, 1],
[0, 1, 1, 1],
[0, 0, 1, 1],
[0, 0, 0, 1]
]
const b = [
[1],
[2],
[3],
[4]
]
const x = math.usolve(m, b)
approx.deepEqual(x, [[-1], [-1], [-1], [4]])
})
it('should solve linear system 4 x 4, matrices', function () {
const m = math.matrix(
[
[1, 1, 1, 1],
[0, 1, 1, 1],
[0, 0, 1, 1],
[0, 0, 0, 1]
])
const b = math.matrix([1, 2, 3, 4])
const x = math.usolve(m, b)
assert(x instanceof math.type.Matrix)
approx.deepEqual(x, math.matrix([[-1], [-1], [-1], [4]]))
})
it('should solve linear system 4 x 4, sparse matrices', function () {
const m = math.sparse(
[
[1, 1, 1, 1],
[0, 1, 1, 1],
[0, 0, 1, 1],
[0, 0, 0, 1]
])
const b = math.matrix([[1], [2], [3], [4]], 'sparse')
const x = math.usolve(m, b)
assert(x instanceof math.type.Matrix)
approx.deepEqual(x, math.matrix([[-1], [-1], [-1], [4]]))
})
it('should solve linear system 4 x 4, matrix and column matrix', function () {
const m = math.matrix(
[
[1, 1, 1, 1],
[0, 1, 1, 1],
[0, 0, 1, 1],
[0, 0, 0, 1]
])
const b = math.matrix([
[1],
[2],
[3],
[4]
])
const x = math.usolve(m, b)
assert(x instanceof math.type.Matrix)
approx.deepEqual(x, math.matrix([[-1], [-1], [-1], [4]]))
})
it('should solve linear system 4 x 4, sparse matrix and column matrix', function () {
const m = math.matrix(
[
[1, 1, 1, 1],
[0, 1, 1, 1],
[0, 0, 1, 1],
[0, 0, 0, 1]
], 'sparse')
const b = math.matrix([
[1],
[2],
[3],
[4]
], 'sparse')
const x = math.usolve(m, b)
assert(x instanceof math.type.Matrix)
approx.deepEqual(x, math.matrix([[-1], [-1], [-1], [4]]))
})
it('should throw exception when matrix is singular', function () {
assert.throws(function () { math.usolve([[1, 1], [0, 0]], [1, 1]) }, /Error: Linear system cannot be solved since matrix is singular/)
assert.throws(function () { math.usolve(math.matrix([[1, 1], [0, 0]], 'dense'), [1, 1]) }, /Error: Linear system cannot be solved since matrix is singular/)
assert.throws(function () { math.usolve(math.matrix([[1, 1], [0, 0]], 'sparse'), [1, 1]) }, /Error: Linear system cannot be solved since matrix is singular/)
})
})