deepbox/linalg
Solvers
Functions for solving linear systems Ax = b, least-squares problems, and triangular systems.
solve
solve(A: Tensor, b: Tensor): Tensor
Solve the linear system Ax = b for x. A must be a square non-singular matrix. Uses LU decomposition internally.
Parameters:
A: Tensor - Coefficient matrix [n, n]b: Tensor - Right-hand side [n] or [n, k]lstsq
lstsq(A: Tensor, b: Tensor, rcond?: number): { x: Tensor; residuals: Tensor; rank: number; s: Tensor }
Solve the least-squares problem: minimize ‖Ax − b‖². Works for overdetermined (m > n) and underdetermined (m < n) systems. Uses SVD internally.
solveTriangular
solveTriangular(A: Tensor, b: Tensor, lower?: boolean): Tensor
Solve a triangular system Ax = b by forward or back substitution. Much faster than general solve() when A is already triangular.
Parameters:
lower: boolean - true if A is lower triangular, false if upper (default: true)Linear System
Ax = b → x = A⁻¹b
Where:
- x = Unknown vector
Least Squares
min ‖Ax − b‖²
Where:
- ‖·‖ = L2 (Euclidean) norm
solvers.ts
import { solve, lstsq, solveTriangular } from "deepbox/linalg";import { tensor } from "deepbox/ndarray";// Solve exact system: 2x + y = 5, x + 3y = 7const A = tensor([[2, 1], [1, 3]]);const b = tensor([5, 7]);const x = solve(A, b); // x ≈ [1.6, 1.8]// Least squares: overdetermined system (more equations than unknowns)const Aover = tensor([[1, 1], [1, 2], [1, 3]]);const bover = tensor([1, 2, 2]);const { x: solution, residuals } = lstsq(Aover, bover);// Triangular solve (fast)const L = tensor([[2, 0], [1, 3]]);const bL = tensor([4, 7]);const xL = solveTriangular(L, bL, true);