Matrix functions
A function with a power series can be applied to a square matrix by putting the matrix into the series. The exponential is the first example. This page covers the rest of the family: sine and cosine, the hyperbolic pair, the square root, the logarithm and real powers. Entries are rarely rational, so each exact stage returns an interval for every entry that certainly contains the true value, as expm does.
Sine and cosine
cos(A) = I − A²/2! + A⁴/4! − … and sin(A) = A − A³/3! + …. They solve the second-order equation x″ = −A²x, the way exp solves x′ = Ax. The method is the one used for exp: divide A by 2ˢ, sum the series with a proven bound on what is left out, then undo the scaling with the double-angle rules cos 2X = 2cos²X − I and sin 2X = 2 sin X cos X. The identity sin²A + cos²A = I holds for every square matrix.
The square root
X² = A can have many solutions, or none. The principal square root is the one whose eigenvalues all have positive real part. It exists, and is unique, when A has no eigenvalue on the negative real axis or at 0. The library checks that condition exactly from the characteristic polynomial and refuses otherwise.
The proof has three steps. A float iteration (Denman–Beavers) gives a guess. The Krawczyk test on the n² equations X² − A = 0 then proves that a small box around the guess holds exactly one root. Last, a Lyapunov test proves that every matrix in the box has eigenvalues with positive real part: it finds a positive definite P with XP + PXᵀ positive definite. So the root in the box is the principal one. When the root turns out to be rational, the answer is checked by squaring it and returned as single points.
The logarithm
The principal logarithm is the X with exp(X) = A whose eigenvalues have imaginary part strictly between −π and π. It exists under the same condition as the square root. The method runs scaling and squaring backwards: take proven square roots until the matrix is within 1/4 of I, sum the series log(I + X) = X − X²/2 + X³/3 − … with a bound on the tail, and double the result once for each square root taken.
Real powers
powm(A, t) is exp(t·log A) for a rational t, so powm(A, "1/3") is a cube root. For an integer exponent use power, which is exact and works for every square matrix.
In code
import { sinm, cosm, sqrtm, logm, powm } from "@ratmath/linalg";
const s = sqrtm([[2, 1], [1, 3]]);
s.quick.value; // [[1.3763…, 0.3249…], [0.3249…, 1.7013…]], floats at once
s.exact()[0][0]; // a RationalInterval certain to contain the entry
sqrtm([[1, 1], [0, 1]]).exact()[0][1].low.toString(); // "1/2": this root is rational, so it is exact
logm([[1, 1], [0, 1]], { bits: 80 }).exact(); // encloses [[0, 1], [0, 0]]
powm([[8, 0], [0, 27]], "1/3").exact(); // encloses [[2, 0], [0, 3]]
cosm([[0, 1], [0, 0]]).exact(); // encloses the identity
sqrtm([[-1, 0], [0, 2]]).exact(); // RangeError: an eigenvalue is −1
@ratmath/linalg and Core's Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.