20
Linear Algebra
Decompositions
Matrix Math
Linear Algebra Operations
Linear algebra operations underpin nearly every ML algorithm. This example demonstrates solving linear systems with solve() (Ax = b), computing matrix inverses with inv(), LU and QR decompositions for numerical stability, SVD (Singular Value Decomposition) for understanding data structure, eigenvalue decomposition with eig(), and computing determinants, matrix rank, norms, and condition numbers. You will see how lstsq() solves overdetermined systems (more equations than unknowns), and how the condition number warns about numerical instability. These are the same operations used internally by LinearRegression, PCA, and Ridge.
Deepbox Modules Used
deepbox/linalgdeepbox/ndarrayWhat You Will Learn
- solve() is more stable and faster than computing inv() explicitly
- SVD decomposes any matrix into U·S·Vᵀ — used by PCA and lstsq
- Eigenvalues reveal principal directions of variance in data
- Condition number indicates numerical sensitivity — high values mean trouble
- lstsq() handles overdetermined systems via SVD internally
Source Code
20-linear-algebra/index.ts
1import {2 solve, inv, lu, qr, svd, eig, det, norm,3 matrixRank, cond, lstsq, trace4} from "deepbox/linalg";5import { tensor, eye, dot } from "deepbox/ndarray";67console.log("=== Linear Algebra ===\n");89const A = tensor([[2, 1], [1, 3]]);10const b = tensor([5, 7]);1112// Solve Ax = b13const x = solve(A, b);14console.log("solve(A, b):", x.toString()); // [1.6, 1.8]1516// Matrix inverse17const Ainv = inv(A);18console.log("A⁻¹:", Ainv.toString());19console.log("A·A⁻¹ ≈ I:", dot(A, Ainv).toString());2021// Determinant and properties22console.log("\ndet(A):", det(A));23console.log("rank(A):", matrixRank(A));24console.log("trace(A):", trace(A));25console.log("cond(A):", cond(A).toFixed(2));26console.log("‖A‖₂:", norm(A).toFixed(4));2728// SVD: A = U·S·Vᵀ29const { U, S, Vt } = svd(A);30console.log("\nSVD singular values:", S.toString());3132// Eigenvalues33const { eigenvalues } = eig(A);34console.log("Eigenvalues:", eigenvalues.toString());3536// Least squares (overdetermined system)37const Aover = tensor([[1,1],[1,2],[1,3]]);38const bover = tensor([1, 2, 2]);39const { solution } = lstsq(Aover, bover);40console.log("\nLeast squares solution:", solution.toString());Console Output
$ npx tsx 20-linear-algebra/index.ts
=== Linear Algebra ===
solve(A, b): [1.6, 1.8]
A⁻¹: [[0.6, -0.2], [-0.2, 0.4]]
A·A⁻¹ ≈ I: [[1, 0], [0, 1]]
det(A): 5
rank(A): 2
trace(A): 5
cond(A): 2.62
‖A‖₂: 3.7321
SVD singular values: [3.449, 1.449]
Eigenvalues: [3.449, 1.449]
Least squares solution: [0.333, 0.500]