Skip to content

The Matrix Square Root

#include <mtl/operation/sqrtm.hpp>
auto X = mtl::sqrtm(A); // X X = A, X's eigenvalues in the right half-plane

A square matrix has many square roots, or none. When A has no eigenvalue on the closed negative real axis, exactly one of them has all its eigenvalues in the open right half-plane: the principal square root (Higham 2008, Theorem 1.29). That is the one sqrtm returns, as MATLAB’s and SciPy’s do. It is also the one the polar decomposition, matrix means and logm use.

sqrtm is not mtl::sqrt(A), which takes the scalar square root of each element.

AX
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 square root
  • Zero is on the closed axis. A singular matrix has no principal square root, and sqrtm throws for it.
  • A negative eigenvalue leaves no principal root, real or complex, and sqrtm throws for it. Casting a real matrix to complex does not help: the eigenvalue stays on the axis, and sqrtm of diag(−4, −9) as a complex matrix throws as well.
  • A non-square matrix, or a NaN or infinite entry, throws std::invalid_argument.
  • A root too large to represent throws std::overflow_error.
  • A pivot of the triangular recurrence below the underflow threshold throws std::runtime_error. The recurrence’s equations are never singular for a principal root, so a small pivot is otherwise used as it is.

The Schur method: reduce A to Schur form, take the square root of the triangular factor by a recurrence, and transform back.

  1. A = Q T Qᴴ by schur. That is the real Schur form for a real A, and the complex one for a complex A.
  2. Compute R with R R = T. R has T’s (quasi-)triangular shape:
    • Complex A, Björck & Hammarling (1983):
      • r_ii = sqrt(t_ii), the principal scalar root;
      • r_ij = (t_ij − Σ_{k=i+1}^{j−1} r_ik r_kj) / (r_ii + r_jj), column by column, each from the diagonal up.
      • The denominator is nonzero, because both roots have positive real parts.
    • Real A, Higham (1987), in real arithmetic. T’s 1×1 blocks hold the real eigenvalues, and its 2×2 blocks [a b; c a] the complex pairs a ± iμ:
      • A 1×1 block t gives sqrt(t), which needs t > 0.
      • A 2×2 block gives α I + (T_jj − a I) / (2α), where α + iβ = sqrt(a + iμ) (Higham 1987). α is formed without cancellation for either sign of a, and μ = sqrt|b| · sqrt|c| without overflow in b c.
      • An off-diagonal block solves R_ii X + X R_jj = T_ij − Σ_k R_ik R_kj, a Sylvester equation of at most four unknowns. sylvester’s block solver (complete pivoting, as LAPACK dlasy2) does this.
  3. X = Q R Qᴴ.

The real method is the reason a real matrix with complex eigenvalues gets a real root: the 2×2 blocks never leave real arithmetic. A rotation by θ (|θ| < π) returns the rotation by θ/2.

Accuracy. The Schur method is backward stable in the sense of Higham (2008, §6.2): the computed X satisfies ‖X² − A‖ ≲ γ_n ‖X‖². The tests check ‖X X − A‖_F / ‖X‖_F² ≤ 10 n·ε across real matrices (with and without complex pairs, normal and nonnormal), complex matrices, float, long double and a class-typed real. When ‖X‖² is much larger than ‖A‖, as for a matrix near the negative real axis, that bound allows a larger error relative to A. It is the square root’s own conditioning, not the method.

Element types. It is generic, as schur is: + − × ÷, comparisons, and abs and sqrt of the real type by ADL. The complex square roots and divisions are computed from real and imaginary parts. For a complex A over a user-defined real type, the limits of schur’s complex path apply.

Accumulator (leading template argument, default void) is forwarded to schur and to the two products that form Q R Qᴴ. void keeps their LAPACK/BLAS dispatch. The triangular recurrence uses plain arithmetic.

  • Björck & Hammarling (1983) gave the Schur method for complex triangular T. It is the method of MATLAB’s sqrtm.
  • Higham (1987) gave the real version: square roots of 2×2 blocks, and block Sylvester equations for the rest. A real matrix stays in real arithmetic.
  • Deadman, Higham & Ralha (2013) later blocked the recurrence for speed on large matrices. MTL5 uses the point recurrence.
  • Å. Björck and S. Hammarling, “A Schur method for the square root of a matrix”, Linear Algebra Appl. 52/53, 127–140, 1983.
  • N. J. Higham, “Computing real square roots of a real matrix”, Linear Algebra Appl. 88/89, 405–430, 1987.
  • N. J. Higham, Functions of Matrices: Theory and Computation, SIAM, 2008, chapter 6.
  • E. Deadman, N. J. Higham and R. Ralha, “Blocked Schur algorithms for computing the matrix square root”, PARA 2012, LNCS 7782, 171–182, 2013.