Linear Algebra
# gaussianElimination function
packages/math/src/linalg.ts:40gaussianElimination(input: number[][], order: number): number[]Solves the linear system Ax = b via Gaussian elimination with partial
pivoting.
The augmented matrix [A | b] is passed as input: an n × (n+1) matrix
where the first n columns represent A and the last column represents
b. The function operates on a deep copy and does not mutate its input.
Partial pivoting (swapping rows so the largest absolute value in the current column is the pivot) reduces numerical instability caused by small pivots.
Returns an array of NaN values if the system is singular (i.e. A has
no unique solution).
Usage in @statili/stats — polynomial and multilinear regression both
construct a normal-equations matrix (XᵀX | Xᵀy) and call
gaussianElimination to recover the coefficient vector. This function is
also called internally by logistic regression for weight initialisation
helpers.
Parameters
| Name | Type | Description |
|---|---|---|
| input | number[][] | Augmented matrix [A | b] in row-major form: n rows, each
with n + 1 elements. The caller is responsible for passing a
well-formed matrix. |
| order | number | Number of unknowns n (equals the number of rows). |
Returns
number[]Solution vector
x of length order, or Array(order).fill(NaN)
if the matrix is singular.Throws
- If
inputis not a rectangular array with the expected dimensions. (Runtime invariant — intended for developer debugging.)
Example
// Solve: 2x + y = 5, x + 3y = 10 → x ≈ 1, y ≈ 3
gaussianElimination([[2, 1, 5], [1, 3, 10]], 2) // [1, 3]
// Singular system — no unique solution
gaussianElimination([[1, 2, 3], [2, 4, 6]], 2) // [NaN, NaN]