Skip to content

The Matrix Logarithm

#include <mtl/operation/logm.hpp>
auto L = mtl::logm(A); // expm(L) = A, L's eigenvalues with |Im| < pi

A 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.

AL
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 logm throws for it.
  • A negative eigenvalue leaves no principal logarithm, real or complex, and logm throws for it, as sqrtm does. Casting a real matrix to complex does not help: logm of diag(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.

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:

  1. Schur form. A = Q T Qᴴ by schur: T is quasi-triangular for a real A and triangular for a complex one.
  2. Square roots of T, by sqrtm’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 m enough 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. α_p can 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‖ ≤ θ^p without taking roots.
  3. 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 in a^{1/2^s} − 1 when s is 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 is s rounding errors: measured at most 23u after 29 roots.
  4. The Padé approximant, in partial fractions: r_m(X) = Σ_j w_j (I + x_j X)^{−1} X, with the m-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-triangular I + x_j X. Then L = 2^s r_m(X).
  5. The diagonal and first superdiagonal of L are recomputed from T, 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 eigenvalues a ± 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 atanh and the unwinding number where the two eigenvalues are close.
  6. Back-transform: log(A) = Q L Qᴴ.

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 = 4 or 5, and for float from m = 12.
    • Rounding: the cap is rounded down like every other θ_m, to the double just below 0.9.
  • 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) and c_3 = −1/12 exactly, so θ_1 = (12u)^{1/2} = 3.65·10⁻⁸, where Table 2.1 prints 1.59·10⁻⁵;
    • the difference narrows as m grows: 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.

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)) = B and expm(logm(A)) = A for 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 atanh form.
  • 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 without log.
  • The θ table: its selection, θ_1 = (12u)^{1/2}, and its order.
  • The errors.
  • 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.