mutable design

Design goal

mutable is the execution-oriented half of the repository. It stores a matrix in one flat row-major array, lets callers update it in place and work through live views, and implements the floating-point numerical routines: determinant, inverse, rank, row reduction, Cholesky factorization, symmetric eigenvalues and the power method. The public API still reads like value operations wherever possible; mutation is confined to methods that return Unit and to the views, so callers can tell from a signature whether a call changes its receiver.

Constraints

  • The package must run on all four MoonBit targets (native, js, wasm, wasm-gc) with one public specification.
  • It may depend only on error, internal and the upstream scalar packages, not on the experimental algebra and container layers.
  • Element types are generic. The numerical routines can only assume the luna-generic structure traits (Field, Num) plus Compare, Sqrt and this package’s Tolerance; they cannot inspect the floating-point format.
  • Its names, argument order and checked/unchecked conventions follow immut wherever both packages offer an operation.

Design decisions

Flat row-major storage

Options. An array of row arrays, a persistent structure, or one flat array. Decision. One Array[T] with entry (i,j)(i, j) at ic+ji c + j, shared by all four targets. Why. It gives O(1)O(1) access with one bounds check per coordinate, contiguous rows for the inner loops of elimination and products, and zero-cost row and column views. from_array adopts the caller’s array so that large inputs need not be copied; the price is aliasing, which the API documents.

Views instead of copies

row_view, col_view and to_transpose return live views in O(1)O(1). A view is a pair (matrix, index) or a wrapper, so writes go to the shared storage and no synchronization is needed. Materializing is always explicit (to_vector, materialize, transpose).

Target-specific kernels with shared semantics

The package has one source file per target for the matrix, LU, view and transpose code. They differ only in loop structure (unrolling, packing, avoidance of division in index computations) chosen for each backend; the public semantics, including bounds and error behaviour, are identical, and the tests run on all four targets. Floating-point results may differ in the last bits because summation orders differ.

Tolerance-based decisions

Elimination must decide when a computed quantity “is zero”. The package uses one absolute threshold τ\tau = Tolerance::tolerance() = 10−1110^{-11} for Double and Float, applied as follows:

RoutineTest
LU (determinant for n≥5n \ge 5, inverse, is_invertible)pivot ∣ukk∣<τ\lvert u_{kk}\rvert < \tau means singular
inverse of a diagonal matrix∣aii∣<τ\lvert a_{ii}\rvert < \tau means singular
ranklargest remaining ∣aik∣<τ\lvert a_{ik}\rvert < \tau means no pivot; a multiplier <τ< \tau is dropped
reduce_row_eliminationpivot candidate ≤τ\le \tau is set to zero and skipped; column entries ≤τ\le \tau are set to zero
cholesky_decompositionradicand ≤τ\le \tau means not positive definite
is_symmetric (also the precondition of eigen)∣aij−aji∣≤τ\lvert a_{ij} - a_{ji}\rvert \le \tau counts as equal
power_methodresidual ∥Ax−λx∥∞≤τ\lVert Ax - \lambda x\rVert_\infty \le \tau accepts; ∥Ax∥∞≤τ\lVert Ax\rVert_\infty \le \tau or xTx≤τx^{\mathsf T}x \le \tau gives None

The structural fast paths and the internal decisions of eigen do not use τ\tau: the fast paths require the structure exactly, and eigen deflates relative to the entries (see below). An absolute threshold is simple and predictable, but it is not scale invariant. Scaling AA by 10−1210^{-12} makes every pivot fall below τ\tau, so a perfectly conditioned matrix is reported singular; scaling by 101210^{12} lets a numerically singular matrix pass. Scale data to order one before calling these routines. For Float, τ=10−11\tau = 10^{-11} is far below the unit roundoff ≈6×10−8\approx 6 \times 10^{-8}, so for data of order one the tests effectively check for exact zeros, and nearly singular Float matrices are not detected. The trait is closed (pub, not pub(open)), so these two instances are the only ones; a scale-aware tolerance policy is future work and would be a breaking change.

Fast paths

inverse recognizes the identity (returns a copy), diagonal matrices (inverts the diagonal) and permutation matrices (returns the transpose, since PTP=IP^{\mathsf T} P = I for a matrix whose columns are distinct coordinate vectors). cholesky_decomposition recognizes the identity and diagonal matrices, and determinant recognizes triangular matrices for n≥5n \ge 5 and multiplies the diagonal. Each test costs O(n2)O(n^2) and saves an O(n3)O(n^3) factorization. The tests are exact (entries compared with 00 and 11), so a shortcut is taken only when it returns what the general algorithm would; a matrix that is merely close to the structure goes through the general path.

Checked forms without changing the kernels

Every checked method validates (squareness, exponent sign, non-emptiness, lengths) and then calls its unchecked partner, which keeps the original aborting or Option behaviour. Matrix multiplication also provides matmul, which returns a DimensionMismatch error before entering the same unchecked kernel. The * operator keeps its aborting behavior, and unchecked_matmul remains available when callers establish compatible shapes.

Symmetric eigenvalues only

General real matrices can have complex eigenvalues, which a function returning Vector[T] cannot represent for real T. eigen therefore accepts only symmetric matrices, for which all eigenvalues are real and an orthonormal eigenbasis exists, and aborts otherwise.

Which traits the routines require, and why

BoundNeeded for
AddMonoid + Mulproducts, mul_vec, dot, trace: sums of products starting from 0
Semiringpow: needs 1 for A0=IA^0 = I and distributivity for associativity of the product
Fieldelimination divides by pivots; mean divides by NN
Numabs for pivot selection and every tolerance test
Comparecomparing magnitudes
SqrtCholesky diagonal, Householder norms, Givens rotations, ∥⋅∥F\lVert\cdot\rVert_F, std_dev
Tolerancethe zero decisions above

Field is commutative in luna-generic, which the derivations below use (for example det⁡(LU)=det⁡Ldet⁡U\det(LU) = \det L \det U and (AB)T=BTAT(AB)^{\mathsf T} = B^{\mathsf T} A^{\mathsf T}).

Mathematical background

Throughout, uu is the unit roundoff (2−532^{-53} for Double, 2−242^{-24} for Float), fl(x∘y)=(x∘y)(1+δ)\mathrm{fl}(x \circ y) = (x \circ y)(1 + \delta) with ∣δ∣≤u|\delta| \le u, and

γn=nu1−nu,∏k=1n(1+δk)±1=1+θn,∣θn∣≤γn(nu<1).\gamma_n = \frac{n u}{1 - n u}, \qquad \prod_{k=1}^{n} (1 + \delta_k)^{\pm 1} = 1 + \theta_n,\quad |\theta_n| \le \gamma_n \quad (n u < 1) .

The second fact11 Higham, Lemma 3.1. The proof is an induction on nn using ∣θn+1∣≤∣θn∣+u+u∣θn∣|\theta_{n+1}| \le |\theta_n| + u + u|\theta_n|. is what turns “each operation is rounded” into bounds for whole algorithms. Inequalities between matrices hold entry-wise, and ∣A∣|A| is the matrix of absolute values. A flop is one floating-point addition, subtraction, multiplication or division.

Matrix product

(AB)ik=∑jaijbjk(AB)_{ik} = \sum_j a_{ij} b_{jk} costs rcnrcn multiply-adds, 2rcn2rcn flops. The kernels sum in different orders (four partial products per step in the unrolled kernels; a packed copy of the columns of BB for products of at least 4×16×164 \times 16 \times 16), but in any order each product aijbjka_{ij} b_{jk} passes through one multiplication and at most n−1n - 1 additions, so it carries a factor 1+θn1 + \theta_n and

∣fl(AB)−AB∣≤γn∣A∣ ∣B∣.\big|\mathrm{fl}(AB) - AB\big| \le \gamma_n |A|\,|B| .

Results on different targets therefore agree to that accuracy, not bit for bit.

LU factorization with partial pivoting

For square AA, Gaussian elimination with partial pivoting computes a permutation PP, a unit lower triangular LL and an upper triangular UU with

PA=LU.PA = LU .

Derivation. Write A(0)=AA^{(0)} = A. At step kk the algorithm chooses the row p≥kp \ge k with the largest ∣apk(k)∣|a^{(k)}_{pk}| and swaps it into position kk (a transposition PkP_k). It then subtracts multiples of row kk from the rows below, which is multiplication by the Gauss transform

Mk=I−ℓkekT,ℓk=(0,…,0,lk+1,k,…,ln−1,k)T,lik=aik(k)/akk(k),M_k = I - \ell_k e_k^{\mathsf T}, \qquad \ell_k = (0, \dots, 0, l_{k+1,k}, \dots, l_{n-1,k})^{\mathsf T}, \qquad l_{ik} = a^{(k)}_{ik} / a^{(k)}_{kk},

so that aij(k+1)=aij(k)−likakj(k)a^{(k+1)}_{ij} = a^{(k)}_{ij} - l_{ik} a^{(k)}_{kj} and column kk below the diagonal becomes zero. After n−1n - 1 steps

U=Mn−2Pn−2⋯M0P0 A.U = M_{n-2} P_{n-2} \cdots M_0 P_0 \, A .

Since ekTℓk=0e_k^{\mathsf T} \ell_k = 0, Mk−1=I+ℓkekTM_k^{-1} = I + \ell_k e_k^{\mathsf T}, and a later transposition PjP_j (j>kj > k) only exchanges entries of ℓk\ell_k: PjMk=M~kPjP_j M_k = \tilde M_k P_j with M~k=I−(Pjℓk)ekT\tilde M_k = I - (P_j \ell_k) e_k^{\mathsf T}. Moving all permutations to the right gives U=M~n−2⋯M~0PAU = \tilde M_{n-2} \cdots \tilde M_0 P A with P=Pn−2⋯P0P = P_{n-2} \cdots P_0, hence PA=LUPA = LU with

L=M~0−1⋯M~n−2−1=I+∑kℓ~kekT.L = \tilde M_0^{-1} \cdots \tilde M_{n-2}^{-1} = I + \sum_k \tilde\ell_k e_k^{\mathsf T} .

This is why the code can store the multipliers in place of the eliminated entries and swap whole rows, multipliers included: the stored lower triangle is exactly LL. The pivot choice gives ∣lik∣≤1|l_{ik}| \le 1.

Cost. Step kk updates an (n−k−1)×(n−k−1)(n-k-1) \times (n-k-1) block with one multiply and one subtract per entry, so

∑k=0n−12(n−k−1)2=(n−1)n(2n−1)3≈23n3\sum_{k=0}^{n-1} 2 (n - k - 1)^2 = \frac{(n-1) n (2n-1)}{3} \approx \tfrac23 n^3

flops.

Determinant. Taking determinants of PA=LUPA = LU with det⁡L=1\det L = 1 and det⁡P=(−1)s\det P = (-1)^{s} for ss exchanges,

det⁡A=(−1)s∏kukk.\det A = (-1)^{s} \prod_{k} u_{kk} .

Solving and the inverse. Ax=bAx = b becomes Ly=PbL y = P b (forward substitution, yi=(Pb)i−∑k<ilikyky_i = (Pb)_i - \sum_{k<i} l_{ik} y_k) and Ux=yU x = y (back substitution, xi=(yi−∑k>iuikxk)/uiix_i = (y_i - \sum_{k>i} u_{ik} x_k) / u_{ii}). Each costs ∑i2i≈n2\sum_i 2i \approx n^2 flops per right-hand side. The inverse is the solution for the nn columns of II; the code does not exploit the zeros of eje_j, so the total is 23n3+n⋅2n2=83n3\tfrac23 n^3 + n \cdot 2n^2 = \tfrac83 n^3 flops.

Stability. The computed factors satisfy the backward error bound (Wilkinson; Higham, Theorem 9.3)22 N. J. Higham, Accuracy and Stability of Numerical Algorithms, 2nd ed., SIAM, 2002, chapters 3 (basics), 8 (triangular systems), 9 (LU) and 10 (Cholesky).

L^U^=P(A+ΔA),∣ΔA∣≤γn∣L^∣ ∣U^∣.\hat L \hat U = P(A + \Delta A), \qquad |\Delta A| \le \gamma_n |\hat L|\,|\hat U| .

To turn this into a norm bound, let ρn=max⁡i,j,k∣aij(k)∣/max⁡i,j∣aij∣\rho_n = \max_{i,j,k} |a^{(k)}_{ij}| / \max_{i,j} |a_{ij}| be the growth factor. Every row of L^\hat L has at most nn entries of magnitude at most 11, so ∥L^∥∞≤n\lVert \hat L \rVert_\infty \le n, and every row of U^\hat U has at most nn entries bounded by ρnmax⁡∣aij∣≤ρn∥A∥∞\rho_n \max |a_{ij}| \le \rho_n \lVert A \rVert_\infty, so

∥ΔA∥∞≤γn∥L^∥∞∥U^∥∞≤n2γnρn∥A∥∞.\lVert \Delta A \rVert_\infty \le \gamma_n \lVert \hat L \rVert_\infty \lVert \hat U \rVert_\infty \le n^2 \gamma_n \rho_n \lVert A \rVert_\infty .

Partial pivoting bounds the growth: since ∣lik∣≤1|l_{ik}| \le 1, ∣aij(k+1)∣≤∣aij(k)∣+∣akj(k)∣≤2max⁡∣a(k)∣|a^{(k+1)}_{ij}| \le |a^{(k)}_{ij}| + |a^{(k)}_{kj}| \le 2 \max |a^{(k)}|, so ρn≤2n−1\rho_n \le 2^{n-1}. The bound is attained only by contrived matrices; in practice ρn\rho_n is small and the method is backward stable. Including the two triangular solves, each computed solution satisfies (A+ΔA)x^=b(A + \Delta A)\hat x = b with ∥ΔA∥∞≤2n2γnρn∥A∥∞\lVert \Delta A \rVert_\infty \le 2 n^2 \gamma_n \rho_n \lVert A \rVert_\infty (Higham, Theorem 9.5), and the forward error is governed by the condition number κ∞(A)=∥A∥∞∥A−1∥∞\kappa_\infty(A) = \lVert A \rVert_\infty \lVert A^{-1} \rVert_\infty:

∥x^−x∥∞∥x∥∞≤κ∞(A) ϵ1−κ∞(A) ϵ,ϵ=2n2γnρn.\frac{\lVert \hat x - x \rVert_\infty}{\lVert x \rVert_\infty} \le \frac{\kappa_\infty(A)\, \epsilon}{1 - \kappa_\infty(A)\, \epsilon}, \qquad \epsilon = 2 n^2 \gamma_n \rho_n .

Each column of the computed inverse obeys this bound.

Closed forms for small determinants

For n≤4n \le 4 the determinant is evaluated by formulas instead of LU: the rule ad−bcad - bc, cofactor expansion along the first row for n=3n = 3, and for n=4n = 4 Laplace’s expansion along the first two rows into six products of complementary 2×22 \times 2 minors,

det⁡A=∑p<q(−1)(0+1)+(p+q)det⁡A{0,1},{p,q}det⁡A{2,3},{p,q}‾,\det A = \sum_{p<q} (-1)^{(0 + 1) + (p + q)} \det A_{\{0,1\},\{p,q\}} \det A_{\{2,3\},\overline{\{p,q\}}} ,

where {p,q}‾\overline{\{p,q\}} is the complementary pair of columns. The sign is (−1)1+p+q(-1)^{1+p+q}: ++ for {0,1},{0,3},{1,2},{2,3}\{0,1\}, \{0,3\}, \{1,2\}, \{2,3\} and −- for {0,2},{1,3}\{0,2\}, \{1,3\}, which is the pattern s01c23−s02c13+s03c12+s12c03−s13c02+s23c01s_{01} c_{23} - s_{02} c_{13} + s_{03} c_{12} + s_{12} c_{03} - s_{13} c_{02} + s_{23} c_{01} in the code.

They use no division and no pivoting decision, which avoids the tolerance test for tiny matrices. Their rounding error is easy to bound: every one of the n!n! monomials ±a0σ(0)⋯an−1,σ(n−1)\pm a_{0\sigma(0)} \cdots a_{n-1,\sigma(n-1)} passes through at most mm roundings, with m=2,5,10m = 2, 5, 10 for n=2,3,4n = 2, 3, 4 (for n=4n = 4: two in each 2×22 \times 2 minor, one for their product, five for the sum of six terms), so

∣fl(det⁡A)−det⁡A∣≤γmper⁡(∣A∣),per⁡(∣A∣)=∑σ∏i∣aiσ(i)∣.\big|\mathrm{fl}(\det A) - \det A\big| \le \gamma_m \operatorname{per}(|A|), \qquad \operatorname{per}(|A|) = \sum_{\sigma} \prod_i |a_{i\sigma(i)}| .

The permanent is at least ∣det⁡A∣|\det A|, and much larger when the monomials cancel; then the relative accuracy is lost, exactly as the determinant itself is ill-conditioned there. These formulas are not backward stable in the LU sense.

Rank and reduced row echelon form

rank runs elimination with partial pivoting on a copy and counts pivots whose magnitude is at least the tolerance, moving to the next column when the largest remaining entry is below it. This is the numerical rank with an absolute threshold τ\tau. It is exact for matrices whose pivots are well separated from τ\tau and otherwise depends on scaling. The singular value decomposition gives the reliable numerical rank, #{σi>τ}\#\{\sigma_i > \tau\}, because by the Eckart–Young theorem σk+1\sigma_{k+1} is the 2-norm distance to the nearest matrix of rank kk; elimination pivots have no such meaning, and a matrix with all pivots of order one can be within 2−n2^{-n} of a singular one.33 Kahan’s example: the upper triangular matrix with 11 on the diagonal and −1-1 above it has all pivots equal to 11, while its smallest singular value decreases like 2−n2^{-n}. The SVD is not implemented.

reduce_row_elimination is Gauss–Jordan elimination: each pivot row is scaled to make the pivot one, and the pivot column is cleared in every other row. For each of at most min⁡(r,c)\min(r, c) pivots it updates all rr rows over all cc columns, about 2rcmin⁡(r,c)2 r c \min(r, c) flops, and works in place. Unlike LU it produces the reduced form, in which every pivot column is a coordinate vector, so the non-pivot columns of the result express the dependent columns of AA in terms of the pivot columns.

Cholesky factorization

A symmetric positive definite (SPD) matrix has a unique factorization A=LLTA = L L^{\mathsf T} with LL lower triangular and ljj>0l_{jj} > 0. Comparing entries of A=LLTA = L L^{\mathsf T} for i≥ji \ge j,

aij=∑k=0jlikljk⟹ljj=ajj−∑k<jljk2,lij=1ljj(aij−∑k<jlikljk).a_{ij} = \sum_{k=0}^{j} l_{ik} l_{jk} \quad\Longrightarrow\quad l_{jj} = \sqrt{a_{jj} - \sum_{k<j} l_{jk}^2}, \qquad l_{ij} = \frac{1}{l_{jj}} \Big(a_{ij} - \sum_{k<j} l_{ik} l_{jk}\Big) .

Each equation determines one new entry from earlier ones, which proves uniqueness once the radicands are positive. The code evaluates them row by row (the Cholesky–Banachiewicz order) and reads only the lower triangle of AA; the cost is ∑j2j(n−j)≈13n3\sum_j 2 j (n - j) \approx \tfrac13 n^3 flops, half of LU, because symmetry halves the work.

Why success characterizes SPD. Write D=diag⁡(ljj)D = \operatorname{diag}(l_{jj}). Then A=(LD−1)(DLT)A = (L D^{-1})(D L^{\mathsf T}) is an LU factorization without pivoting, whose pivots are ujj=ljj2u_{jj} = l_{jj}^2, the radicands. The product of the first j+1j + 1 pivots is the leading principal minor det⁡Mj+1\det M_{j+1}, so the radicand at step jj equals det⁡Mj+1/det⁡Mj\det M_{j+1} / \det M_j. By Sylvester’s criterion AA is SPD exactly when all leading principal minors are positive, so in exact arithmetic the factorization succeeds exactly for SPD matrices. With the tolerance, cholesky_decomposition succeeds exactly when every ratio det⁡Mj+1/det⁡Mj\det M_{j+1} / \det M_j exceeds τ\tau (up to rounding), and is_positive_definite is implemented by attempting it.

Stability. Cholesky needs no pivoting: from ajj=∑kljk2a_{jj} = \sum_k l_{jk}^2 every ∣ljk∣≤ajj|l_{jk}| \le \sqrt{a_{jj}}, so entries cannot grow, and the computed factor satisfies L^L^T=A+ΔA\hat L \hat L^{\mathsf T} = A + \Delta A with ∣ΔA∣≤γn+1∣L^∣ ∣L^T∣|\Delta A| \le \gamma_{n+1} |\hat L|\,|\hat L^{\mathsf T}| (Higham, Theorem 10.3). Since (∣L^∣ ∣L^T∣)ij≤aiiajj(|\hat L|\,|\hat L^{\mathsf T}|)_{ij} \le \sqrt{a_{ii} a_{jj}} up to O(u)O(u) by the Cauchy–Schwarz inequality, the backward error is small relative to the diagonal of AA.

Symmetric eigenvalue problem

For real symmetric AA the spectral theorem gives A=QΛQTA = Q \Lambda Q^{\mathsf T} with QQ orthogonal and Λ\Lambda real diagonal. eigen computes it in two phases for n≠2n \ne 2, and by a closed formula for n=2n = 2.

Householder tridiagonalization. For x∈Rmx \in \mathbb{R}^m, x≠0x \ne 0, let α=−sgn⁡(xm−1)∥x∥2\alpha = -\operatorname{sgn}(x_{m-1}) \lVert x \rVert_2 and v=x−αem−1v = x - \alpha e_{m-1}. The reflector H=I−2vvT/vTvH = I - 2 v v^{\mathsf T} / v^{\mathsf T} v is symmetric and orthogonal, and it maps xx to αem−1\alpha e_{m-1}:

vTv=∥x∥2−2αxm−1+α2=2(∥x∥2−αxm−1)=2 vTx,Hx=x−2 vTxvTv v=x−v=αem−1.\begin{aligned} v^{\mathsf T} v &= \lVert x \rVert^2 - 2 \alpha x_{m-1} + \alpha^2 = 2\big(\lVert x \rVert^2 - \alpha x_{m-1}\big) = 2\, v^{\mathsf T} x, \\ H x &= x - \frac{2\, v^{\mathsf T} x}{v^{\mathsf T} v}\, v = x - v = \alpha e_{m-1} . \end{aligned}

The sign of α\alpha is opposite to xm−1x_{m-1}, so xm−1−αx_{m-1} - \alpha adds two numbers of the same sign and cannot cancel. The code (the tred2 scheme) works from the last row upwards: for row ii it reflects x=(ai0,…,ai,i−1)x = (a_{i0}, \dots, a_{i,i-1}) onto its last coordinate, applies HH from both sides, and so zeroes row and column ii outside the tridiagonal band. After n−2n - 2 such steps

Q1TAQ1=T,Q1=Hn−1Hn−2⋯H2,Q_1^{\mathsf T} A Q_1 = T, \qquad Q_1 = H_{n-1} H_{n-2} \cdots H_2 ,

where TT is symmetric tridiagonal with diagonal dd and off-diagonal ee. The two-sided update costs 43n3\tfrac43 n^3 flops and accumulating Q1Q_1 explicitly another 43n3\tfrac43 n^3. Before forming each reflector the row is divided by ∑k∣xk∣\sum_k |x_k|, which avoids overflow and underflow in ∥x∥2\lVert x \rVert_2; only an exactly zero row is left unreflected.

Implicit QL with Wilkinson shifts. The tridiagonal TT is diagonalized by plane rotations. Before each sweep on the unreduced block starting at ll, the shift σ\sigma is the eigenvalue of the leading 2×22 \times 2 block (dleleldl+1)\begin{pmatrix} d_l & e_l \\ e_l & d_{l+1} \end{pmatrix} closer to dld_l. With g=(dl+1−dl)/(2el)g = (d_{l+1} - d_l)/(2 e_l), its eigenvalues are

λ±=dl+dl+12±(dl+1−dl2)2+el2=dl+el(g±g2+1),\lambda_\pm = \frac{d_l + d_{l+1}}{2} \pm \sqrt{\Big(\frac{d_{l+1} - d_l}{2}\Big)^2 + e_l^2} = d_l + e_l \big(g \pm \sqrt{g^2 + 1}\big),

because 12(dl+dl+1)=dl+elg\tfrac12(d_l + d_{l+1}) = d_l + e_l g and the square root equals ∣el∣g2+1|e_l| \sqrt{g^2 + 1} (the sign of ele_l is absorbed by ±\pm). The one closer to dld_l makes ∣g±g2+1∣|g \pm \sqrt{g^2+1}| small, that is, takes the sign opposite to gg:

σ=dl+el(g−sgn⁡(g)g2+1)=dl−elg+sgn⁡(g)g2+1,\sigma = d_l + e_l\big(g - \operatorname{sgn}(g)\sqrt{g^2+1}\big) = d_l - \frac{e_l}{g + \operatorname{sgn}(g)\sqrt{g^2 + 1}} ,

where the second form follows from (g−sg2+1)(g+sg2+1)=g2−(g2+1)=−1(g - s\sqrt{g^2+1})(g + s\sqrt{g^2+1}) = g^2 - (g^2 + 1) = -1 for s=sgn⁡(g)s = \operatorname{sgn}(g), and avoids the cancellation of the first. The code uses it with sgn⁡(0)=1\operatorname{sgn}(0) = 1. A sweep starts from dm−σd_m - \sigma and applies Givens rotations (cs−sc)\begin{pmatrix} c & s \\ -s & c \end{pmatrix}, c2+s2=1c^2 + s^2 = 1, from the bottom of the block upwards, each one chasing the bulge created by the previous; QQ is updated with the same rotations, 6n6n flops each. The rotation parameters are computed as c=g/fc = g/f, r=c2+1r = \sqrt{c^2 + 1}, s=1/rs = 1/r, c=csc = c s (or the symmetric form), dividing by the larger of ∣f∣,∣g∣|f|, |g| so that no square of a large number is formed.

An off-diagonal entry is treated as zero when it is negligible next to its diagonal neighbours in the working precision,

fl((∣dm∣+∣dm+1∣)+∣em∣)=∣dm∣+∣dm+1∣,\mathrm{fl}\big((|d_m| + |d_{m+1}|) + |e_m|\big) = |d_m| + |d_{m+1}| ,

that is, ∣em∣≲u (∣dm∣+∣dm+1∣)|e_m| \lesssim u\,(|d_m| + |d_{m+1}|), and the block then splits. The test needs no knowledge of uu, so it works for Float and Double alike, and it is invariant under scaling of AA. For symmetric tridiagonal matrices the Wilkinson shift converges for every input in exact arithmetic (Wilkinson, 1968), and typically cubically; about two sweeps per eigenvalue is usual. The code nevertheless aborts after 60 sweeps for one starting index ll. With accumulation of QQ the whole computation takes about 9n39 n^3 flops (Golub and Van Loan’s estimate for the symmetric QR algorithm), 83n3\tfrac83 n^3 of them in the reduction.

Accuracy. Every transformation is orthogonal, so in exact arithmetic the spectrum never changes, and rounding contributes a backward error of O(nu)∥A∥2O(n u) \lVert A \rVert_2; setting a negligible eme_m to zero perturbs TT by at most u(∣dm∣+∣dm+1∣)≤2u∥A∥2u(|d_m| + |d_{m+1}|) \le 2u \lVert A \rVert_2, which is of the same order. Weyl’s inequality, ∣λi(A+E)−λi(A)∣≤∥E∥2|\lambda_i(A + E) - \lambda_i(A)| \le \lVert E \rVert_2 for symmetric AA and EE, then gives for the computed eigenvalues

∣λ^i−λi∣≤cn u ∥A∥2|\hat\lambda_i - \lambda_i| \le c_n\, u\, \lVert A \rVert_2

with a modest cnc_n, independent of the scale of AA. Eigenvectors are accurate in proportion to u∥A∥2/gapu \lVert A \rVert_2/\text{gap}, where the gap is the distance to the nearest other eigenvalue (the Davis–Kahan theorem). The only absolute test left is the symmetry check ∣aij−aji∣≤τ|a_{ij} - a_{ji}| \le \tau before the computation; the algorithm then reads the lower triangle.

The 2×22 \times 2 case. The code works with the symmetric part (assd)\begin{pmatrix} a & s \\ s & d \end{pmatrix}, s=12(a01+a10)s = \tfrac12(a_{01} + a_{10}). Its characteristic polynomial λ2−(a+d)λ+(ad−s2)\lambda^2 - (a + d)\lambda + (ad - s^2) has the discriminant

(a+d2)2−(ad−s2)=(a−d2)2+s2=r2,\Big(\frac{a + d}{2}\Big)^2 - (ad - s^2) = \Big(\frac{a - d}{2}\Big)^2 + s^2 = r^2 ,

a sum of squares, so with m=12(a+d)m = \tfrac12(a + d) and h=12(a−d)h = \tfrac12(a - d)

λ1,2=m±r,r=h2+s2≥0.\lambda_{1,2} = m \pm r, \qquad r = \sqrt{h^2 + s^2} \ge 0 .

Computing r2r^2 as m2−det⁡Am^2 - \det A instead would subtract two numbers of size m2m^2 and lose about u m2u\,m^2 in absolute terms, which for m≈108m \approx 10^8 is about 11: the computed discriminant could even be negative for a diagonal matrix. The code evaluates rr as β(h/β)2+(s/β)2\beta\sqrt{(h/\beta)^2 + (s/\beta)^2} with β=max⁡(∣h∣,∣s∣)\beta = \max(|h|, |s|), which cannot overflow, and never thresholds it, so eigenvalues as close as rounding allows stay distinct. The eigenvalue of larger magnitude, m+sgn⁡(m) rm + \operatorname{sgn}(m)\, r, is a sum of two numbers of the same sign and is computed directly; the other follows from λ1λ2=det⁡A=ad−s2\lambda_1 \lambda_2 = \det A = ad - s^2, which avoids the cancellation in m−rm - r when ∣λ2∣≪∣λ1∣|\lambda_2| \ll |\lambda_1|. When s=0s = 0 the matrix is diagonal and the code returns aa and dd exactly, in decreasing order, with the coordinate vectors.

For the eigenvector of λ1\lambda_1, both

(λ1−ds)=(h+rs)and(sλ1−a)=(sr−h)\begin{pmatrix} \lambda_1 - d \\ s \end{pmatrix} = \begin{pmatrix} h + r \\ s \end{pmatrix} \quad\text{and}\quad \begin{pmatrix} s \\ \lambda_1 - a \end{pmatrix} = \begin{pmatrix} s \\ r - h \end{pmatrix}

solve (A−λ1I)v=0(A - \lambda_1 I)v = 0: for the first, (a−λ1)(h+r)+s2=(h−r)(h+r)+s2=h2−r2+s2=0(a - \lambda_1)(h + r) + s^2 = (h - r)(h + r) + s^2 = h^2 - r^2 + s^2 = 0, and the second row is s(h+r)+(d−λ1)s=s(h+r−h−r)=0s(h + r) + (d - \lambda_1)s = s(h + r - h - r) = 0; the second vector is checked the same way. The code takes the first when h≥0h \ge 0 and the second when h<0h < 0, so that h+rh + r or r−hr - h adds numbers of the same sign and cannot cancel. Since AA is symmetric, the eigenvector of λ2\lambda_2 is orthogonal to it, (vy,−vx)(v_y, -v_x). Each column is scaled so that its entry of largest magnitude is 11; the columns are therefore never zero, never parallel, and paired with the right eigenvalues.

Power method

The code starts from x0=(1,…,1)x_0 = (1, \dots, 1), or, if ∥Ax0∥∞≤τ\lVert A x_0 \rVert_\infty \le \tau, from the first coordinate vector eke_k with ∥Aek∥∞>τ\lVert A e_k \rVert_\infty > \tau; if there is none it returns None. It then iterates

y=Axk,xk+1=y/∥y∥∞,λk+1=xk+1TAxk+1xk+1Txk+1,y = A x_k, \qquad x_{k+1} = y / \lVert y \rVert_\infty, \qquad \lambda_{k+1} = \frac{x_{k+1}^{\mathsf T} A x_{k+1}}{x_{k+1}^{\mathsf T} x_{k+1}} ,

and stops when ∥Axk−λkxk∥∞≤τ\lVert A x_k - \lambda_k x_k \rVert_\infty \le \tau; the normalization makes ∥xk∥∞=1\lVert x_k \rVert_\infty = 1.

Convergence. If AA is diagonalizable with eigenvalues ∣λ1∣>∣λ2∣≥…|\lambda_1| > |\lambda_2| \ge \dots and eigenvectors viv_i, and x0x_0 has a component c1≠0c_1 \ne 0 along v1v_1, then

Akx0=λ1k(c1v1+∑i≥2ci(λiλ1)kvi),A^{k} x_0 = \lambda_1^{k}\Big(c_1 v_1 + \sum_{i \ge 2} c_i \big(\tfrac{\lambda_i}{\lambda_1}\big)^{k} v_i\Big),

so the direction of xkx_k converges to v1v_1 linearly with ratio r=∣λ2/λ1∣r = |\lambda_2 / \lambda_1|. For negative λ1\lambda_1 the sign of xkx_k alternates, which the Rayleigh quotient does not notice. For symmetric AA with orthonormal viv_i, xk∝∑ici(λi/λ1)kvix_k \propto \sum_i c_i (\lambda_i/\lambda_1)^k v_i gives

λk=∑ici2λi(λi/λ1)2k∑ici2(λi/λ1)2k=λ1+O(r2k),\lambda_k = \frac{\sum_i c_i^2 \lambda_i (\lambda_i/\lambda_1)^{2k}}{\sum_i c_i^2 (\lambda_i/\lambda_1)^{2k}} = \lambda_1 + O(r^{2k}),

so the eigenvalue converges twice as fast as the vector. When ∣λ1∣=∣λ2∣|\lambda_1| = |\lambda_2| with λ1≠λ2\lambda_1 \ne \lambda_2 (for example ±1\pm 1) the direction oscillates and the residual test never passes, and when AA is nilpotent the iterate reaches zero; both cases return None.

What the stopping test guarantees. For symmetric AA, a residual r=Ax−λxr = Ax - \lambda x bounds the distance to the spectrum: expanding xx in the orthonormal eigenbasis, ∥r∥22=∑ici2(λi−λ)2≥min⁡i(λi−λ)2∥x∥22\lVert r \rVert_2^2 = \sum_i c_i^2 (\lambda_i - \lambda)^2 \ge \min_i (\lambda_i - \lambda)^2 \lVert x \rVert_2^2. With ∥r∥∞≤τ\lVert r \rVert_\infty \le \tau and ∥x∥∞=1\lVert x \rVert_\infty = 1 we have ∥r∥2≤n τ\lVert r \rVert_2 \le \sqrt n\, \tau and ∥x∥2≥1\lVert x \rVert_2 \ge 1, so some eigenvalue lies within n τ\sqrt n\, \tau of the returned λ\lambda. The test is absolute, so for ∥A∥≫1\lVert A \rVert \gg 1 it may be unreachable in floating point (the residual cannot drop below about u∥A∥u \lVert A \rVert), and for ∥A∥≪1\lVert A \rVert \ll 1 it passes before xx is accurate.

Statistics

variance is the population variance computed in two passes: first the mean aˉ\bar a, then 1N∑(ai−aˉ)2\tfrac1N \sum (a_i - \bar a)^2. The one-pass formula 1N∑ai2−aˉ2\tfrac1N \sum a_i^2 - \bar a^2 subtracts two nearly equal numbers when the data have a large mean and a small spread, and can even return a negative value. With S=∑(ai−aˉ)2S = \sum (a_i - \bar a)^2 and the condition number κ=∥a∥2/S\kappa = \lVert a \rVert_2 / \sqrt S of the data, Chan, Golub and LeVeque (1983) show that the relative error of SS is bounded by about Nκ2uN \kappa^2 u for the one-pass formula and by about Nu+N2κ2u2N u + N^2 \kappa^2 u^2 for the two-pass one, which is O(Nu)O(Nu) unless κ\kappa is near u−1/2u^{-1/2}. The count NN is accumulated as a sum of ones in T, exact up to 2532^{53} entries for Double and 2242^{24} for Float; beyond that the count stops growing and the mean is wrong.

Transpose views and products

Let UU and VV be the matrices wrapped by the two transpose views. Their logical product C=UTVTC = U^{\mathsf T}V^{\mathsf T} is defined when Urows=VcolsU_{\text{rows}}=V_{\text{cols}}. Its shape is Ucols×VrowsU_{\text{cols}} \times V_{\text{rows}}, and the matrix product definition gives, for 0≤i<Ucols0 \le i < U_{\text{cols}} and 0≤k<Vrows0 \le k < V_{\text{rows}},

Cik=∑j=0Urows−1UjiVkj.C_{ik} = \sum_{j=0}^{U_{\text{rows}}-1} U_{ji} V_{kj} .

The implementation evaluates this sum in the displayed factor order and stores CTC^{\mathsf T} in the returned Transpose wrapper. It requires only AddMonoid + Mul: Zero supplies the empty sum when the inner dimension is zero, and no commutativity or distributivity law for scalar multiplication is used. The previous shortcut computed ∑jVkjUji\sum_j V_{kj}U_{ji}, which is generally different when scalar multiplication is non-commutative. The direct loop takes O(UcolsVrowsUrows)O(U_{\text{cols}}V_{\text{rows}}U_{\text{rows}}) scalar multiply-adds and allocates the output matrix, without copying either input view.

Correctness and invariants

  • Storage. data.length() == row * col at all times; every public accessor checks the row and column separately.
  • Value-returning methods do not mutate. Only Unit-returning methods, view writes and reduce_row_elimination change a matrix.
  • Checked/unchecked law. x.f() == Ok(x.unchecked_f()) whenever the precondition holds; inverse returns Err(SingularMatrix) exactly when unchecked_inverse returns None.
  • Determinant consistency. For n≥5n \ge 5 and a matrix that is not triangular, determinant returns exactly 0 when the LU factorization reports a pivot below τ\tau, which is also when is_invertible returns false and inverse fails; otherwise it returns a product of pivots that are at least τ\tau in magnitude, which can still underflow. For n≤4n \le 4 the closed formulas are used for determinant, while is_invertible still uses LU, so a matrix with a tiny non-zero determinant can be “not invertible” with a non-zero determinant.
  • Residual guarantees. cholesky_decomposition returns a factor with backward error O(nu)O(nu) relative to ∣L^∣ ∣L^T∣|\hat L|\,|\hat L^{\mathsf T}|; eigen returns eigenpairs with backward error O(nu)∥A∥O(nu)\lVert A \rVert, for every scale of AA; power_method returns only pairs with residual at most τ\tau.
  • Complexity. *: 2rcn2rcn flops; determinant: 23n3\tfrac23 n^3; inverse: 83n3\tfrac83 n^3; rank, reduce_row_elimination: O(rcmin⁡(r,c))O(rc\min(r, c)); cholesky_decomposition: 13n3\tfrac13 n^3; eigen: about 9n39n^3; power_method: two matrix-vector products, 4n24n^2 flops, per iteration; statistics: O(rc)O(rc).

Alternatives rejected

  • A relative or norm-scaled tolerance. More robust, but it would change the results of existing callers; deferred until a tolerance policy can be passed explicitly.
  • Returning complex eigenvalues. Would make this package depend on a complex number type and change the signature for real symmetric input.
  • Copy-on-write views. Would hide the cost of writes and break the in-place contract that views exist for.
  • A single portable kernel. Measured to be slower on some targets; the per-target files trade code size for speed while sharing one specification.

Boundaries

mutable does not provide linear solves for a right-hand side as a public method (only the inverse), QR, SVD or least-squares solvers, eigenvalues of non-symmetric matrices, sparse storage, condition number estimates, or scale-aware tolerances. It does not implement the algebra traits itself; backends/default wraps it for that. Domain workflows such as regression or optimization belong in downstream packages.

Footnotes

  1. Higham, Lemma 3.1. The proof is an induction on nn using ∣θn+1∣≤∣θn∣+u+u∣θn∣|\theta_{n+1}| \le |\theta_n| + u + u|\theta_n|. ↩

  2. N. J. Higham, Accuracy and Stability of Numerical Algorithms, 2nd ed., SIAM, 2002, chapters 3 (basics), 8 (triangular systems), 9 (LU) and 10 (Cholesky). ↩

  3. Kahan’s example: the upper triangular matrix with 11 on the diagonal and −1-1 above it has all pivots equal to 11, while its smallest singular value decreases like 2−n2^{-n}. ↩