|
| 1 | +# Matrix & Vector Norms |
| 2 | + |
| 3 | +## Overview & Motivation |
| 4 | + |
| 5 | +Linear algebra computations — solvers, Kalman filter covariance updates, regression — depend on the numerical health of the matrices involved. Norms formalise the notion of "size" for matrices and vectors and are the building blocks of every conditioning, stability, and error-bound estimate in the library. They are also the cheapest such quantities to compute: a single pass over the entries with no allocation, suitable for real-time embedded paths. |
| 6 | + |
| 7 | +Vector normalisation, closely related, produces the unit-length direction of a vector and is a recurring primitive in geometry, attitude estimation, and gradient methods. |
| 8 | + |
| 9 | +## Mathematical Theory |
| 10 | + |
| 11 | +### Vector Norm |
| 12 | + |
| 13 | +For a vector $\mathbf{v} \in \mathbb{R}^n$, the Euclidean (L2) norm is |
| 14 | + |
| 15 | +$$\|\mathbf{v}\|_2 = \sqrt{\sum_{i=1}^{n} v_i^2}$$ |
| 16 | + |
| 17 | +The unit vector $\hat{\mathbf{v}} = \mathbf{v} / \|\mathbf{v}\|_2$ satisfies $\|\hat{\mathbf{v}}\|_2 = 1$. Normalisation is undefined when $\|\mathbf{v}\|_2 = 0$ and must be guarded. |
| 18 | + |
| 19 | +### Matrix Norms |
| 20 | + |
| 21 | +**Frobenius norm** — treats the matrix as a flattened vector: |
| 22 | + |
| 23 | +$$\|A\|_F = \sqrt{\sum_{i=1}^{m}\sum_{j=1}^{n} a_{ij}^2}$$ |
| 24 | + |
| 25 | +It is rotationally invariant under unitary transformations and cheap to compute. |
| 26 | + |
| 27 | +**1-norm (maximum absolute column sum)**: |
| 28 | + |
| 29 | +$$\|A\|_1 = \max_{1 \le j \le n} \sum_{i=1}^{m} |a_{ij}|$$ |
| 30 | + |
| 31 | +**Infinity norm (maximum absolute row sum)**: |
| 32 | + |
| 33 | +$$\|A\|_\infty = \max_{1 \le i \le m} \sum_{j=1}^{n} |a_{ij}|$$ |
| 34 | + |
| 35 | +The 1-norm and infinity-norm are dual: $\|A\|_\infty = \|A^\top\|_1$. |
| 36 | + |
| 37 | +## Complexity Analysis |
| 38 | + |
| 39 | +| Operation | Time | Space | Notes | |
| 40 | +|---------------|---------|--------|------------------------------| |
| 41 | +| FrobeniusNorm | $O(mn)$ | $O(1)$ | Single pass, no allocation | |
| 42 | +| OneNorm | $O(mn)$ | $O(1)$ | Column-wise sum, running max | |
| 43 | +| InfinityNorm | $O(mn)$ | $O(1)$ | Row-wise sum, running max | |
| 44 | +| VectorNorm | $O(n)$ | $O(1)$ | Single pass | |
| 45 | +| Normalize | $O(n)$ | $O(n)$ | Output vector on stack | |
| 46 | + |
| 47 | +## Step-by-Step Walkthrough |
| 48 | + |
| 49 | +Matrix $A = \begin{bmatrix}3 & 1 \\ 1 & 2\end{bmatrix}$: |
| 50 | + |
| 51 | +1. **FrobeniusNorm**: $\sqrt{9 + 1 + 1 + 4} = \sqrt{15} \approx 3.873$ |
| 52 | +2. **OneNorm**: column 0 sum $= |3| + |1| = 4$; column 1 sum $= |1| + |2| = 3$; max $= 4$ |
| 53 | +3. **InfinityNorm**: row 0 sum $= |3| + |1| = 4$; row 1 sum $= |1| + |2| = 3$; max $= 4$ |
| 54 | + |
| 55 | +Vector $\mathbf{v} = [3,\, 4]^\top$: $\|\mathbf{v}\|_2 = 5$, and $\hat{\mathbf{v}} = [0.6,\, 0.8]^\top$. |
| 56 | + |
| 57 | +## Pitfalls & Edge Cases |
| 58 | + |
| 59 | +**Zero vector normalisation** — dividing by $\|\mathbf{v}\|_2 = 0$ is undefined. The implementation returns an empty optional for near-zero norms. |
| 60 | + |
| 61 | +**Fast-math semantics** — `#pragma GCC optimize("fast-math")` may reorder floating-point operations. The norms are sums of non-negative values, so reordering does not change the sign of the result, but catastrophic cancellation can still occur for near-zero off-diagonal entries. |
| 62 | + |
| 63 | +**Non-square matrices** — FrobeniusNorm, OneNorm, and InfinityNorm apply to any $m \times n$ matrix. |
| 64 | + |
| 65 | +## Variants & Generalizations |
| 66 | + |
| 67 | +The **spectral norm** (largest singular value, $\|A\|_2$) is the tightest but requires an SVD — $O(N^3)$ with a large constant, unsuitable for embedded real-time paths. The 1-norm and infinity-norm are cheap upper bounds used throughout the library instead. |
| 68 | + |
| 69 | +General **p-norms** and weighted norms generalise the vector case; the L2 norm is the only one currently exposed because it is the natural quantity for geometric and least-squares work. |
| 70 | + |
| 71 | +## Applications |
| 72 | + |
| 73 | +- **Conditioning estimates**: the 1-norm feeds the condition number (see `solvers::ConditionNumber`). |
| 74 | +- **Convergence tests**: iterative solvers and optimisers stop when a residual norm falls below tolerance. |
| 75 | +- **Attitude / geometry**: vector normalisation produces unit direction and rotation axes. |
| 76 | +- **Covariance sanity**: the Frobenius norm of a covariance matrix bounds its total variance. |
| 77 | + |
| 78 | +## Connections to Other Algorithms |
| 79 | + |
| 80 | +The 1-norm is the norm used by `solvers::ConditionNumber` for its $\|A\|\cdot\|A^{-1}\|$ estimate. The `math::Matrix` type provides the storage and transpose the norms operate on. Norm-based residual tests appear in `solvers` and `optimization`. |
| 81 | + |
| 82 | +## References & Further Reading |
| 83 | + |
| 84 | +- Golub, G. H. & Van Loan, C. F., "Matrix Computations", 4th ed., Chapter 2 (matrix norms) |
| 85 | +- Trefethen, L. N. & Bau, D., "Numerical Linear Algebra", Lecture 3 (norms) |
| 86 | +- Higham, N. J., "Accuracy and Stability of Numerical Algorithms", 2nd ed. |
0 commit comments