mathlib — Linear algebra

Vectors, matrix multiply, Cholesky, SVD, LU solve. Optional WebGPU comparison for matmul. ← Back to hub

Vector CPU vs GPU

0 Vector add, dot, norm (f32)

Run add, dot, and norm on CPU vs GPU (f32). When WebGPU is available, click Init GPU in the Matrix section first, then run the benchmark.

Vector size:

—
Storage

1 Column vs Row Major

Column-major: elements in same column are consecutive. mathlib uses column-major (like BLAS, Fortran, MATLAB). In matrix multiply and matvec, rows of A are used as dot-product vectors with columns of B or with v; columns of A are the vectors that get scaled and summed in A×v. Storage order does not change the math, only how the same numbers are laid out in a flat array.

Same 3×3 matrix; colors show memory layout
Vectors

2 Vector add, dot, norm

Two vectors added in WASM. Dot product and norm (GPU-accelerated on native after init).

Loading…
Matrices

2 Matrix 2D (A × B)

Two matrices; C = A × B. Column-major storage.

—

Large matrix (CPU vs GPU)

Compare CPU (f64), CPU (f32), and GPU (f32) when WebGPU is available. Each run uses a warmup then reports the median of several runs. Thresholds for when to use GPU are configured in Rust (ExecutorThresholds).

Size:

—
Basic f32 ops

3 Add, scale, dot, matvec (f32)

Element-wise add, scale, dot product, and matrix×vector using f32. When WebGPU is initialized, these use the same backend as the large matmul above.

—

Basic f32 CPU vs GPU

Vector size: Matvec: 256×256.

At small sizes CPU is often faster due to GPU upload/readback cost. Try vector size 1,000,000 or 2,000,000 to see GPU win.

—
Matrices

3a Matrix × Vector (A × v)

y = A × v. Vectors are column vectors.

—
Result vector components
Solvers

3b Solve Ax = b (general)

Square matrix A; solve Ax = b via LU (using WasmMatrix.solve(b)).

—
Solution x
Solvers

4 Cholesky solve

SPD matrix A, solve Ax = b via Cholesky; show L and solution x.

—
Solution x
Decompositions

5 SVD

Economical SVD; singular values σ and U, V dimensions.

—
Singular values σ
Solvers

6 LU solve

General square matrix A; solve Ax = b via LU decomposition.

—
Solution x
Solvers

6a Damped least squares

Rectangular A (m×n); minimize ‖Ax − b‖² + λ²‖x‖². Use the slider to change λ².

λ²: 1

—
Solution x
Decompositions

6b QR decomposition

A = Q R where Q has orthonormal columns and R is upper triangular. For least squares min ‖Ax − b‖.

—
Solution x
Sparse

7 Sparse formats (CSR, CCS, COO)

For matrices with few non-zeros, sparse storage saves memory and can speed up operations. CSR (Compressed Sparse Row) stores row_ptr (start index per row), col_ind (column index of each entry), and values. CCS is column-wise; COO is a list of (row, col, value). mathlib provides CRS and other formats.

Example 3×3 sparse matrix and its CSR representation (row_ptr, col_ind, values)