linsolve(): array | matrix
Solve a linear system of equations Ax = b.
Solves the linear system of equations Ax = b using LU factorization with row pivoting.
Solve a linear system with a vector
Solve a linear system with a vector
import { assertEquals, assertAlmostEquals } from "jsr:@std/assert"; import { eye } from "../../index.ts"; const actual = (linsolve([[1, 1, -1], [1, -2, 3], [2, 3, 1]], [5, 6, 3])) as number[]; const expected = [5.846153846153846, -2.3846153846153846, -1.5384615384615385]; assertEquals(actual.length, expected.length); actual.forEach((v, i) => assertAlmostEquals(v, expected[i], 1e-12));
Solve a linear system with a matrix
Solve a linear system with a matrix
import { assertEquals, assertAlmostEquals } from "jsr:@std/assert"; import { eye } from "../../index.ts"; const actual = (linsolve([[1, 1, -1], [1, -2, 3], [2, 3, 1]], [[4], [-6], [7]])) as number[][]; const expected = [[1], [2], [-0.9999999999999999]]; assertEquals(actual.map(row => row.length), expected.map(row => row.length)); actual.forEach((row, i) => row.forEach((v, j) => assertAlmostEquals(v, expected[i][j], 1e-12)));
Solve a linear system with an identity matrix
Solve a linear system with an identity matrix
import { assertEquals, assertAlmostEquals } from "jsr:@std/assert"; import { eye } from "../../index.ts"; const actual = (linsolve([[1, 1, -1], [1, -2, 3], [2, 3, 1]], eye(3, 3))) as number[][]; const expected = [[0.846153846153846, 0.3076923076923077, -0.07692307692307707], [-0.3846153846153846, -0.23076923076923078, 0.30769230769230776], [-0.5384615384615384, 0.07692307692307691, 0.23076923076923078]]; assertEquals(actual.map(row => row.length), expected.map(row => row.length)); actual.forEach((row, i) => row.forEach((v, j) => assertAlmostEquals(v, expected[i][j], 1e-12)));