Numerical Linear Algebra/Numerical Thinking and Matrix Computation

Lesson 8.11,562 words

Numerical Thinking and Matrix Computation

Numerical analysis builds efficient discrete algorithms for continuous problems, and its cost is dominated as much by memory traffic as by arithmetic. Block matrix calculus, flop counts, and the BLAS efficiency ratio fix the cost model; triangular and unitary matrices are the two computational building blocks every factorization rests on.

╌╌╌╌

Exact linear algebra assumes perfect arithmetic. Row reduction produces a solution, the characteristic polynomial has roots, and the Gram–Schmidt process returns an orthonormal basis, each on the premise that every operation is carried out without error. A computer honors none of that premise. It stores numbers to finite precision, spends far more time moving data than adding it, and cannot evaluate a limit. Numerical linear algebra studies what becomes of the exact theory once a machine rounds every operation and spends real time on every memory access.

Numerical analysis

Numerical analysis exists because the continuous and the discrete do not match. An analytical result is the value of a limit, and a limit needs infinite time and infinite memory. To compute anything at all, continuous quantities must be discretized into something finite. The three tools for that are machine numbers (finite-precision stand-ins for real numbers), iteration (a finite number of improving steps in place of a limit), and approximation (an answer that is deliberately, and controllably, wrong). The computed result is allowed to differ from the exact one, provided the error stays bounded and predictable. Numerical linear algebra is where this program is learned first, because the problems it treats — solving , factoring a matrix, finding eigenvalues — sit underneath nearly all of scientific computing.

Throughout, denotes either or , and the space of matrices over it. A column vector is an element of ; a co-vector (row vector) lives in . The adjoint is the transpose when and the conjugate transpose when ; the co-vector is the adjoint of the column vector .

Thinking in blocks

Manipulating a matrix as an array of sub-matrices rather than scalars is clearer on paper and maps onto how fast hardware runs, as the timing table below shows.

Start from the columns. Writing for the -th standard basis vector, the columns of are the images of the basis vectors,

using the colon shorthand for . Linearity then turns a matrix–vector product into a linear combination of columns, and a row-covector times a vector into an inner product:

The matrix product admits four equivalent readings, and choosing the right one is what makes an algorithm fast. Writing by columns / rows and by columns :

  • Column-wise. — each column of the result is applied to a column of .
  • Row-wise. The -th row of is .
  • Rank-one sum. — a sum of outer products, each an rank-one matrix.
  • Componentwise. , the familiar formula from a first course.

All four compute the same additions and multiplications in a different order. The componentwise formula, the one usually taught first, is the least useful here.

One computes with blocks as if they were scalars, with one caution: block factors do not commute, so the order matters and cannot be rearranged the way scalar entries can.2 The four product formulas above are all special cases of this one lemma, recovered by partitioning and into single columns, single rows, or single entries.

Block multiplication treats each as a scalar-like sum of products over the shared block index .

The cost of computation

The natural first cost model counts arithmetic. A flop is one real floating-point operation (, , , , ). Keeping only the leading order as dimensions grow:

OperationShapesFlop (leading order)
Inner product
Outer product
Matrix–vector
Matrix–matrix

If a flop took a fixed time and nothing else cost anything, runtime would just be , the peak performance ceiling. Real programs miss this ceiling because of data movement, not arithmetic.

The gap comes from the memory hierarchy. A processor has a few very fast, very small registers and caches, backed by progressively larger and slower memory, down to disk. Access speed spans two to three orders of magnitude across the levels.

The memory hierarchy: each level down is larger and roughly an order of magnitude slower, so an algorithm's speed depends on how well it reuses data already near the top.

Let count the input/output transfers to main memory and their cost. A better model of runtime is

where the machine constant says a memory access costs roughly thirty flops, and the algorithm-dependent efficiency ratio

is the quantity to maximize. An algorithm with of order one is memory-bound and idles near the ceiling; one with growing with the dimension keeps the arithmetic units busy.

The three product formulas differ precisely in . An inner or outer product has ; a matrix–vector product has ; a matrix–matrix product has , growing with the dimension. This is why the same arithmetic, reordered, runs an order of magnitude apart on real hardware.

OperationFlopMemory transfers
Inner product
Outer product
Matrix–vector
Matrix–matrix

The BLAS (Basic Linear Algebra Subprograms) library packages hardware-tuned kernels at three levels matched to this table: Level 1 for vector operations (one loop), Level 2 for matrix–vector operations (two loops), Level 3 for matrix–matrix operations (three loops). The design rule that follows is to phrase an algorithm at the highest BLAS level available, so it spends its time in arithmetic with traffic rather than the reverse.

One more hardware fact leaks into the mathematics. Fortran, and therefore BLAS, LAPACK, and MATLAB, store a matrix column by column (column-major order); C and Python store it row by row. Sweeping a matrix along its stored direction avoids index arithmetic and cache misses, which is why the column-oriented product formulas are the default in this subject.

Triangular matrices

Two families of matrices are the reusable pieces every factorization is built from. The first is triangular.

Two structural facts make them computationally convenient:

  • Determinant. By Laplace expansion the determinant of a triangular matrix is the product of its diagonal entries, . So a triangular matrix is invertible exactly when no diagonal entry is zero.
  • Closure. Invertible lower (upper) triangular matrices are closed under product and inverse; they form a subgroup of the general linear group .4

The payoff is that a triangular system is solved directly, with no elimination. For with lower triangular, partition off the last row at each step: the equation gives once the earlier components are known. Sweeping is forward substitution; sweeping an upper-triangular system from the bottom up is back substitution.

Forward substitution fills the solution top to bottom; solving row uses only the already-known components above it (shaded) against the lower triangle of .
Algorithm:ForwardSubstitution(L,b)\textsc{ForwardSubstitution}(L, b) — solve Lx=bLx = b for lower triangular LL
  1. 1
    for k=1k = 1 to mm do
  2. 2
    sβklk1xk1s \gets \beta_k - l_{k-1}^\ast x_{k-1}
    inner product over the solved part
  3. 3
    ξks/λk\xi_k \gets s / \lambda_k
  4. 4
    end for
  5. 5
    return xx

The inner product at row costs flops, so the whole solve costs flops — the same leading cost, and the same efficiency ratio , as a single triangular matrix–vector product. Both are standardized Level-2 BLAS routines. Because is never needed after is computed, the output can overwrite the input in place, saving a copy; such in situ execution is the BLAS norm.

Unitary matrices

The second building block preserves geometry rather than exploiting sparsity.

Three properties matter downstream:

  • Trivial inverse. Solving needs no factorization: , one matrix–vector product at flops, twice a triangular solve.
  • Length preservation. From , a unitary map is an isometry: it rotates or reflects without stretching. Length preservation keeps errors from being amplified, which is why unitary matrices anchor the stable algorithms of numerical linear algebra.
  • Group. Unitary matrices are closed under product and inverse, forming the subgroup ( in the real case) of .
A unitary map sends the unit circle to itself and an orthonormal frame to another orthonormal frame: lengths and right angles are preserved, only the orientation of the frame changes.

A permutation matrix , whose columns are the basis vectors in permuted order, is the simplest unitary matrix: it merely reorders rows or columns and satisfies . Permutations are the bookkeeping of pivoting.

Building blocks for the factorizations

Triangular and unitary matrices are the two shapes a hard matrix is reduced to. A triangular factorization (, or Cholesky's ) turns a linear system into two cheap triangular solves. The QR factorization reduces a matrix to a unitary times a triangular factor, trading a little extra cost for the stability that unitary maps guarantee. Each such algorithm is assembled from block products, forward and back substitution, and orthogonal transformations, phrased at the highest BLAS level the hardware rewards.

Footnotes

  1. Bornemann, Numerical Linear Algebra, §1 — What Is Numerical Analysis?: the definition in terms of efficient discrete algorithms for continuous problems, and the roles of machine numbers, iteration, and approximation.
  2. Bornemann, §2.10–§2.17 — Matrix Calculus: the four product formulas (column-wise, row-wise, rank-one sum, componentwise) and the block-multiplication lemma that subsumes them.
  3. Bornemann, §4 — Execution Times: the flop table, the runtime model , the efficiency ratio , and the three BLAS levels; §4.8 on column-major versus row-major storage.
  4. Bornemann, §5 — Triangular Matrices: the determinant and closure properties, forward and back substitution, and their cost; §6 — Unitary Matrices: , length preservation, and permutation matrices.

╌╌ END ╌╌