NLA-lib Explorer

Modified Gram–Schmidt

Implementation of Gram–Schmidt orthogonalization

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.

Implementation guides

Algorithm

Definition (In-place modified Gram–Schmidt).

  1. Allocate two scalar scratch cells.
  2. For each column j=0,\ldots,m-1:
    1. Use the stored column as the current residual \mathbf{v}.
    2. For each earlier column i=0,\ldots,j-1:
      1. Compute the Hermitian coefficient r=\mathbf{q}_i^{\mathsf{H}}\mathbf{v}.
      2. Store the negated coefficient in scratch.
      3. Update the current column in place by \mathbf{v}\leftarrow\mathbf{v}-r\mathbf{q}_i.
    3. Compute t=\mathbf{v}^{\mathsf{H}}\mathbf{v}.
    4. Store a zero literal in the comparison cell.
    5. If t is nonzero, replace it by \sqrt t and divide the column by that value.
    6. Otherwise leave the zero residual unchanged.
  3. 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.

Lean statement and details
NLAlib.MGS.factorProgram {Op Tst : Type} [Ops.Has Ops.ScalarOp Op] [Ops.Has Ops.Blas1Op Op]
  [Ops.Has Ops.CmpTst Tst] : Ops.Program Op Tst 2 1

Full source on GitHub

Copy link

Model of arithmetic

All statements on this page are for

Exact arithmetic

Definitions of the arithmetic models · Other programs of this algorithm: Two-pass classical Gram–Schmidt basis extension (Exact arithmetic; Standard rounding · relative-error BLAS)

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

Full source on GitHub

Copy link

Accuracy

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.

Lean statement and details
NLAlib.MGS.factorRoutine_meets {𝕜 Op Tst : Type} [RCLike 𝕜] [Ops.Has Ops.ScalarOp Op]
  [Ops.Has Ops.Blas1Op Op] [Ops.Has Ops.CmpTst Tst] {I : Ops.Interp Op Tst 𝕜}
  (hS : Ops.IsScalar Ops.RCLikeBlas.machine I) (hB : Ops.RCLikeBlas.IsExact I)
  (hC : Ops.IsCmp (⇑RCLike.re) I) : MGS.factorRoutine.Meets I (MGS.factorSpec 𝕜)

Full source on GitHub

Copy link

Mathematical method