Matrix functions, subspaces and norms
| Problem | Compute the principal square root, logarithm and fractional powers of a matrix; the pseudoinverse, numerical rank and orthonormal bases of the range and null space; matrix norms and condition numbers |
| Devised by | Eliakim Moore (1920) and Roger Penrose (1955), the pseudoinverse; Åke Björck and Sven Hammarling (1983), the Schur square root; Charles Kenney and Alan Laub (1989), inverse scaling and squaring; Sheung Hun Cheng, Nicholas Higham, Kenney and Laub (2001), its Padé form; Gene Golub and William Kahan (1965), numerical rank from the SVD |
| Accuracy | Agrees with SciPy and NumPy to relative on well-conditioned matrices |
| Cost | sqrtm: a Schur form and operations; logm: a Schur form, triangular square roots and 8 triangular solves; the SVD-based functions: one SVD |
| Read first | The singular value decomposition, Matrix functions and matrix equations |
| API | sqrtm, logm, fractional_matrix_power, pinv, matrix_rank, null_space, orth, norm, cond |
Overview
Two groups of tools complete the dense linear algebra:
- Matrix functions beyond the exponential. The square root appears in covariance factorisations and in symmetric orthogonalisation, the logarithm in recovering a generator from a transition matrix , and fractional powers in interpolating between transformations.
- The four fundamental subspaces and the size of a matrix. The pseudoinverse solves least-squares problems of any rank. The rank and the bases of the range and null space describe what a matrix can and cannot reach. Norms and condition numbers measure its size and how much it can amplify errors.
The functions follow SciPy (sqrtm, logm, fractional_matrix_power, pinv, null_space, orth) and NumPy (matrix_rank, norm, cond).
Where it is used
Norms, condition numbers and subspaces tell you how much to trust a computed answer.
- Condition number. Predicts how much measurement error is amplified in solving a system, as in sensor calibration or tomography.
- Pseudoinverse and rank. Solving rank-deficient and underdetermined systems, and testing whether a set of measurements is redundant.
- Matrix functions. Square roots and logarithms of matrices appear in robotics (interpolating rotations) and in statistics (covariance matrices).
- Subspace angles. Comparing the signal subspaces estimated from two data sets.
History
Eliakim Moore (1920) defined a generalised inverse for singular matrices, and Roger Penrose (1955) characterised it by four equations. Gene Golub and William Kahan’s SVD algorithm (1965) made it computable, and made the numerical rank precise: the number of singular values above a tolerance. Åke Björck and Sven Hammarling (1983) computed matrix square roots from the Schur form, and Nicholas Higham (1987) adapted their method to real arithmetic. For the logarithm, Charles Kenney and Alan Laub (1989) proposed inverse scaling and squaring: take square roots until the matrix is close to the identity, then use a series. Sheung Hun Cheng, Higham, Kenney and Laub (2001) chose the Padé approximants for it, and Awad Al-Mohy and Higham (2012) refined the choice of the number of square roots.
The idea
Square roots and logarithms through the Schur form
Every square matrix has a complex Schur form with upper triangular and unitary, and every primary matrix function satisfies . Functions of triangular matrices are triangular, so they can be computed entry by entry.
For the square root, with upper triangular gives , the principal root with positive real part, and for
computed column by column. The denominators are non-zero as long as no two eigenvalues have square roots summing to zero, which the principal branch guarantees unless has a repeated zero eigenvalue.
For the logarithm, power series converge only near the identity. Inverse scaling and squaring uses . Take square roots of until , then evaluate by the Padé approximant. Written as 8-point Gauss-Legendre quadrature of , it is
The approximation error for is below . Fractional powers follow as .
The pseudoinverse and the fundamental subspaces
With the SVD and non-zero singular values:
- the range of is spanned by the first columns of (
orth); - the null space by the last columns of (
null_space); - the pseudoinverse is , with inverting the non-zero singular values.
is the least-squares solution of of smallest norm. It satisfies Penrose’s four equations: , , and both and are Hermitian.
In floating point no singular value of a rank-deficient matrix is exactly zero, so “non-zero” means “above a tolerance”, , with by default. The choice matters when the data are noisy. A rank-3 matrix perturbed by noise of size has seven singular values near , and inverting them amplifies the noise by :
Table 7.12. The pseudoinverse solution for several truncation tolerances.
| rtol | singular values kept | error of A⁺b against the minimum-norm solution |
|---|---|---|
| 1e-15 | 10 | 2.8e-1 |
| 1e-10 | 10 | 2.8e-1 |
| 1e-6 | 3 | 3.3e-9 |
| 1e-2 | 3 | 3.3e-9 |
With the tolerance above the noise, the pseudoinverse recovers the minimum-norm solution of the noise-free problem. Below it, the solution is ruined. Choosing the tolerance from the known accuracy of the data is a form of regularisation.
Norms and condition numbers
The norms of a matrix measure how much it can stretch a vector:
| norm | value | from |
|---|---|---|
| largest column sum of | the entries | |
| largest row sum | the entries | |
| , the largest singular value | the SVD | |
| the entries | ||
| nuclear | the SVD |
The condition number bounds the relative change of the solution of per relative change of the data. In the 2-norm it is , which also applies to rectangular matrices and least squares. Roughly, digits are lost when solving with , as the chapter introduction explains.
Algorithm
logm(A): Schur form ; while , set (triangular square root) and ; ;
with Gauss-Legendre nodes and weights on ; return .
pinv(A, rtol): the SVD; = the number of ; .
Implementation
/// The principal logarithm `L` with `e^L = A` and eigenvalues with imaginary parts in `(−π, π)`
/// (SciPy's `logm`), by the inverse scaling and squaring method on the Schur form: take square
/// roots of `T` until `‖T − I‖₁ ≤ 0.25` (`k` times), evaluate `log(I + X)` by the `[8/8]` Padé
/// approximant written as Gauss-Legendre quadrature, `Σ wⱼ X (I + xⱼ X)⁻¹`, and multiply by `2ᵏ`.
pub fn logm<T: Scalar, S: Data<Elem = T>>(a: &ArrayBase<S, Ix2>) -> Result<Array2<C>> {
let (mut t, z) = schur(a)?;
let n = t.rows;
if (0..n).any(|i| t.at(i, i).norm() == 0.0) {
return Err(LinalgError::Singular(0));
}
let eye = Mat::<C>::identity(n);
let one = C::new(1.0, 0.0);
let mut k = 0;
while norm1(&lincomb(one, &t, -one, &eye)) > 0.25 {
t = sqrt_triangular(&t)?;
k += 1;
if k > 100 {
return Err(LinalgError::NoConvergence("logm square roots"));
}
}
let x = lincomb(one, &t, -one, &eye);
let mut log = Mat::<C>::zeros(n, n);
for (node, weight) in gauss_legendre_01(8) {
// X (I + x_j X)⁻¹ = (I + x_j X)⁻¹ X, since the two commute.
let m = lincomb(one, &eye, C::new(node, 0.0), &x);
let lu = LuFactorization::new(m)?;
for col in 0..n {
let rhs: Vec<C> = (0..n).map(|r| x.at(r, col)).collect();
for (r, v) in lu.solve_vec(&rhs).into_iter().enumerate() {
*log.at_mut(r, col) += v * weight;
}
}
}
let scale = C::new(2f64.powi(k), 0.0);
log.data.iter_mut().for_each(|v| *v *= scale);
Ok(back(&z, &log))
}
/// The Moore-Penrose pseudoinverse `A⁺ = V Σ⁺ Uᴴ` (SciPy's `pinv`): singular values at most
/// `rtol · σ_max` are treated as zero, with `rtol = max(m, n) ε` by default.
pub fn pinv<T: Scalar, S: Data<Elem = T>>(a: &ArrayBase<S, Ix2>, rtol: Option<f64>) -> Result<Array2<T>> {
let (m, n) = a.dim();
let (u, sv, vt) = svd_full(a)?;
let rtol = rtol.unwrap_or(m.max(n) as f64 * f64::EPSILON);
let r = rank_of(&sv, rtol);
let mut out = Array2::<T>::from_elem((n, m), T::zero());
for k in 0..r {
let inv = 1.0 / sv[k];
for i in 0..n {
let vik = vt[(k, i)].conj().scale(inv);
for j in 0..m {
out[(i, j)] += vik * u[(j, k)].conj();
}
}
}
Ok(out)
}
- The matrix functions return complex matrices, even for real input whose result is real. Take the real parts in that case.
- Defaults follow SciPy and NumPy:
pinv,null_spaceandorthtruncate at ;matrix_rankat . condin the 1, ∞ and Frobenius norms inverts the matrix, and is infinite for a singular one.
In practice
Every function is compared with SciPy 1.17.1 and NumPy 2 (references from book/generator/data/make_subspaces_refs.py). The null
space and range are compared through their projectors , which do not depend on the choice of basis:
Table 7.13. Matrix functions, subspaces and norms against SciPy and NumPy.
| computation (SciPy / NumPy reference) | largest difference (relative) |
|---|---|
sqrtm, symmetric positive definite 12 × 12 | 2e-15 |
logm, symmetric positive definite 12 × 12 | 4e-15 |
fractional_matrix_power (p = 0.3), symmetric positive definite 12 × 12 | 8e-16 |
sqrtm, general 12 × 12 | 2e-15 |
logm, general 12 × 12 | 4e-15 |
fractional_matrix_power (p = 0.3), general 12 × 12 | 4e-15 |
sqrtm, complex 10 × 10 | 2e-15 |
logm, complex 10 × 10 | 2e-15 |
fractional_matrix_power (p = 0.3), complex 10 × 10 | 2e-15 |
pinv, 15 × 9 of rank 4 | 2e-15 |
matrix_rank, 15 × 9 of rank 4: 4 (NumPy 4) | 0 |
null_space: projector onto the 5-dimensional null space | 2e-15 |
orth: projector onto the 4-dimensional range | 7e-16 |
pinv, 20 × 7 of full rank | 4e-15 |
norm, all eight orders, gen | 2e-15 |
norm, all eight orders, lowrank | 6e-16 |
norm, all eight orders, tall | 1e-15 |
cond in the 2, −2, 1, ∞ and Frobenius norms, gen | 4e-15 |
cond in the 2, −2, 1, ∞ and Frobenius norms, spd | 2e-16 |
Strengths
- One method, the Schur form, for every matrix function, real or complex. The square root and logarithm agree with SciPy to rounding.
- SVD-based subspaces and pseudoinverses that are reliable for rank-deficient and rectangular matrices, with an explicit truncation tolerance.
Weaknesses
- No
funmfor general functions and no real-arithmetic Schur method, so real results are computed in complex arithmetic. logmtakes a fixed number of Padé terms. Al-Mohy and Higham’s algorithm chooses the number of square roots and the degree adaptively, and is faster.- The subspace functions cost a full SVD. For large sparse matrices, iterative methods are needed.
Exercises
- Show that for any polynomial , and hence for any function defined by a power series.
- Derive the recurrence for from .
- Show that , and why Gauss-Legendre quadrature of it gives a Padé approximant.
- Verify Penrose’s four equations for .
- Show that for a matrix of rank .
Hints and answers
- because , and sums and limits preserve the identity.
- ; solve for .
- Differentiate with respect to ; and commute. An -point Gauss rule applied to gives a rational function of degree that matches the series to order , which is the Padé approximant.
- and , and and are diagonal and real. The unitary factors carry these over.
- lies between and .
Further reading
- N. J. Higham, Functions of Matrices: Theory and Computation, SIAM, 2008, chapters 6 (square root) and 11 (logarithm).
- S. H. Cheng, N. J. Higham, C. S. Kenney and A. J. Laub, “Approximating the logarithm of a matrix to specified accuracy”, SIAM Journal on Matrix Analysis and Applications 22 (2001) 1112–1125.
- Å. Björck and S. Hammarling, “A Schur method for the square root of a matrix”, Linear Algebra and its Applications 52/53 (1983) 127–140.
- G. H. Golub and C. F. Van Loan, Matrix Computations, 4th ed., Johns Hopkins University Press, 2013, sections 2.3 (norms), 5.5 (pseudoinverse and rank).
- R. Penrose, “A generalized inverse for matrices”, Proceedings of the Cambridge Philosophical Society 51 (1955) 406–413.