The checked program overwrites an n\times m input matrix with the mathematical Gram–Schmidt columns. Dependent columns are allowed: a zero residual stays zero. This endpoint returns the basis matrix, rather than storing a triangular coefficient matrix.
Use the stored column as the current residual \mathbf{v}.
For each earlier column i=0,\ldots,j-1:
Compute the Hermitian coefficient r=\mathbf{q}_i^{\mathsf{H}}\mathbf{v}.
Store the negated coefficient in scratch.
Update the current column in place by \mathbf{v}\leftarrow\mathbf{v}-r\mathbf{q}_i.
Compute t=\mathbf{v}^{\mathsf{H}}\mathbf{v}.
Store a zero literal in the comparison cell.
If t is nonzero, replace it by \sqrt t and divide the column by that value.
Otherwise leave the zero residual unchanged.
Release the two scratch cells and return the overwritten matrix.
Each projection coefficient uses the already updated residual. This is the modified update order; forming all coefficients before the updates would be a classical pass.
This page presents exact real or complex field arithmetic with Hermitian dot products, vector updates, division by a scalar, square roots, and real-part comparisons. The primitive contracts permit the in-place column views used by the program. The normalization guard handles dependent columns and zero dimensions.
A cell is one abstract field value. Translating a complex value to physical real/imaginary storage is a separate representation choice.
Operations and Storage
The column sweeps make m(m-1)/2 projection dot products, the same number of vector updates and scalar negations, and m additional self-dot products. There are m zero literals and equality tests, and at most m square roots and vector normalizations. For n,m\ge1, with linear-cost length-n vector primitives, numerical work is O(nm^2). The canonical certificate also covers zero dimensions and counts structural instructions.
Theorem (In-place scratch bound). If every primitive workspace is at most w cells, the complete matrix program uses at most 2+w scratch cells above the supplied nm-cell matrix. Thus resident capacity is at most
nm+2+w.
With zero primitive workspace, the scoped scratch allocation is exactly two cells. This bound requires no full-rank assumption and includes the zero-residual branch.
Lean statement and details
NLAlib.MGS.factor_peak_le {Op Tst : Type} [Ops.Has Ops.ScalarOp Op] [Ops.Has Ops.Blas1Op Op]
[Ops.Has Ops.CmpTst Tst] (W : Op → List ℕ → ℕ) (w : ℕ) (hW : ∀ (o : Op) (d : List ℕ), W o d ≤ w)
(idx : List ℕ) : Ops.stmtPeak W MGS.factor idx ≤ 2 + w
Theorem (Exact execution correspondence). Under the exact primitive contracts and the theorem's live, readable, writable matrix-buffer conditions, the program terminates and overwrites the matrix with exactly the mathematical gramSchmidtQ of the input. No full-rank or successful-execution premise is assumed. Rectangular matrices, dependent columns, and zero dimensions are admitted.
The shared mathematical method describes how the nonzero columns span the original range. Rounded orthogonality bounds for two classical passes concern a different implementation and are not substituted for this exact MGS statement.