The Matrix Logarithm
#include <mtl/operation/logm.hpp>
auto L = mtl::logm(A); // expm(L) = A, L's eigenvalues with |Im| < piA matrix X with exp(X) = A is a logarithm of A. When A has no
eigenvalue on the closed negative real axis, exactly one of them has all its
eigenvalues in the strip −π < Im z < π: the principal logarithm (Higham
2008, Theorem 1.31). That is the one logm returns, as MATLAB’s and SciPy’s do.
It is the inverse of expm on matrices whose eigenvalues lie in that strip, and
the logarithm map of a matrix Lie group.
logm is not mtl::log(A), which takes the scalar logarithm of each element.
What it returns
Section titled “What it returns”A | L |
|---|---|
| real, no eigenvalue on (−∞, 0] | real, also when A has complex eigenvalues |
| complex, no eigenvalue on (−∞, 0] | complex |
| an eigenvalue on (−∞, 0] | std::domain_error: no principal logarithm |
- A singular matrix has no logarithm at all, so
logmthrows for it. - A negative eigenvalue leaves no principal logarithm, real or complex, and
logmthrows for it, assqrtmdoes. Casting a real matrix to complex does not help:logmofdiag(1, −1)as a complex matrix throws as well. - A non-square matrix, or a NaN or infinite entry, throws
std::invalid_argument. - A logarithm too large to represent throws
std::overflow_error.
How it is computed
Section titled “How it is computed”Inverse scaling and squaring (Kenney & Laub 1989) uses
log(A) = 2^s log(A^{1/2^s}). Repeated square roots bring A close to I,
where a Padé approximant of log(1 + x) is accurate. MTL5 follows Al-Mohy &
Higham’s Algorithm 4.1 (2012), the method of SciPy’s logm, on the Schur form:
- Schur form.
A = Q T Qᴴbyschur:Tis quasi-triangular for a realAand triangular for a complex one. - Square roots of
T, bysqrtm’s Schur recurrence, in two stages:- first, until every eigenvalue
λ^{1/2^s}is within θ₇ of 1; - then for as long as Algorithm 4.1’s cost rule says another root lowers the
degree
menough to pay for itself. The rule comparesα_p(T − I) = max(‖(T − I)^p‖^{1/p}, ‖(T − I)^{p+1}‖^{1/(p+1)})with θ_m.α_pcan be far below‖T − I‖for a nonnormal matrix (Al-Mohy & Higham 2009). MTL5 computes the norms of the powers exactly, where the paper estimates them, and compares‖(T − I)^p‖ ≤ θ^pwithout taking roots.
- first, until every eigenvalue
X = T^{1/2^s} − I. Each 1×1 diagonal entry is formed as(a − 1) / Π_j (1 + a^{1/2^j})(Al-Mohy 2012), which avoids the cancellation ina^{1/2^s} − 1whensis large.- The first superdiagonal is kept. Al-Mohy & Higham recompute it here too.
- Between 1×1 blocks, the square-root recurrence already forms it without
cancellation, as
t / Π_i (λ₁^{1/2^i} + λ₂^{1/2^i}). Its error issrounding errors: measured at most 23u after 29 roots.
- The Padé approximant, in partial fractions:
r_m(X) = Σ_j w_j (I + x_j X)^{−1} X, with them-point Gauss–Legendre rule(x_j, w_j)on [0, 1]. The nodes are computed by Newton’s method in the element type, so they carry its precision. Each solve is a block back substitution on the quasi-triangularI + x_j X. ThenL = 2^s r_m(X). - The diagonal and first superdiagonal of
Lare recomputed fromT, since these are the entries where scaling loses the most accuracy:log(t_ii)for a 1×1 block;- a real 2×2 block
[a b; c a], with eigenvaluesa ± iμ, in closed form:log ρ · I + (θ/μ) [0 b; c 0], whereρ e^{iθ} = a + iμ; - an entry between two 1×1 blocks by Higham (2008, (11.28)), with
atanhand the unwinding number where the two eigenvalues are close.
- Back-transform:
log(A) = Q L Qᴴ.
Precision: θ_m follows the element type
Section titled “Precision: θ_m follows the element type”The Padé approximant has the backward error r_m(X) = log(I + X + ΔX), with
ΔX = h_{2m+1}(X) = e^{r_m(X)} − I − X (Al-Mohy & Higham 2012, Theorem 2.2).
θ_m is the largest θ with Σ_{k≥2m+1} |c_k| θ^{k−1} ≤ u, where c_k are the
coefficients of h_{2m+1}. tools/logm_theta.py (mpmath) solves this for
bfloat16, half, float, double, x87, double-double, binary128, quad-double and
binary256. logm uses the table with the largest u not above the type’s own,
as expm does.
- θ_m is capped at 0.9. Theorem 2.2 needs
ρ(r_m(X)) < π, which the paper shows holds forρ(X) < 0.91.- Where it binds: for half and bfloat16 from
m = 4or 5, and for float fromm = 12. - Rounding: the cap is rounded down like every other θ_m, to the double just below 0.9.
- Where it binds: for half and bfloat16 from
- The double values are below the paper’s Table 2.1. The table does not
follow from the paper’s own definition:
- for
m = 1,r_1(x) = 2x/(2 + x)andc_3 = −1/12exactly, soθ_1 = (12u)^{1/2} = 3.65·10⁻⁸, where Table 2.1 prints 1.59·10⁻⁵; - the difference narrows as
mgrows: for θ₇, 0.248 here against 0.288 there. MTL5 uses the values the definition gives. Smaller is the safe direction: it can cost one more square root or a higher degree, and the backward error stays within the stated bound.
- for
Element types. It is generic, as schur and sqrtm are: + − × ÷,
comparisons, and abs and sqrt of the real type. Step 5 also needs log of
the real type, and atan2 for a complex A or a real 2×2 block, found by ADL.
A type without them keeps the Padé values for those entries, which are still
within the backward error bound. When atanh is found, it sharpens the
superdiagonal entry between close eigenvalues.
Accumulator (leading template argument, default void) is forwarded to
schur, to the products behind the norms of the powers, and to Q L Qᴴ.
tests/unit/operation/test_logm.cpp covers:
- Round trips:
logm(expm(B)) = Bandexpm(logm(A)) = Afor real matrices with and without complex pairs, normal and nonnormal,n ≤ 12, through the default and the in-house paths, and for complex matrices. - Closed forms:
- the identity;
- rotations, whose log is the generator
[0 −θ; θ 0]; - Jordan blocks, whose log is
[log a, 1/a; 0, log a]; - close eigenvalues, against the
atanhform.
- Near the identity, against the series
log(I + E). - A spectrum from 10⁻⁶ to 10⁶, which needs many square roots.
- Element types:
float,long double, and class-typed reals with and withoutlog. - The θ table: its selection,
θ_1 = (12u)^{1/2}, and its order. - The errors.
References
Section titled “References”- A. H. Al-Mohy and N. J. Higham, “Improved inverse scaling and squaring algorithms for the matrix logarithm”, SIAM J. Sci. Comput. 34(4), C152–C169, 2012.
- A. H. Al-Mohy, “A more accurate Briggs method for the logarithm”, Numer. Algorithms 59(3), 393–402, 2012.
- C. S. Kenney and A. J. Laub, “Condition estimates for matrix functions”, SIAM J. Matrix Anal. Appl. 10(2), 191–209, 1989.
- N. J. Higham, Functions of Matrices: Theory and Computation, SIAM, 2008, chapter 11.