Purpose: Compute the characteristic polynomial of an matrix in using only matrix multiplications and traces — no symbolic determinant expansion, no divisions except by small integers.
The Recurrence
Let . Set and iterate for :
At the end , which is a free correctness check (and is exactly the Cayley-Hamilton theorem).
Code
// characteristic polynomial coefficients, highest degree first: [1, c_{n-1}, ..., c_0]
vector<double> faddeevLeVerrier(vector<vector<double>> A) {
int n = A.size();
vector<double> c(n + 1, 0);
c[0] = 1;
vector<vector<double>> M(n, vector<double>(n, 0));
for (int i = 0; i < n; i++) M[i][i] = 1; // M = I
for (int k = 1; k <= n; k++) {
auto AM = matmul(A, M);
double tr = 0;
for (int i = 0; i < n; i++) tr += AM[i][i];
c[k] = -tr / k;
M = AM;
for (int i = 0; i < n; i++) M[i][i] += c[k]; // M = A*M + c_k I
}
return c;
}Complexity
- Time: — iterations, each one matrix multiplication
- With fast matrix multiplication:
- Space:
Faster alternatives exist: reduction to Hessenberg form followed by a recurrence gives , and is what numerical libraries use.
Why it works
The identity behind it is Newton’s relation between power sums and elementary symmetric polynomials. The traces are the power sums of the eigenvalues, and the coefficients are (up to sign) the elementary symmetric polynomials of the eigenvalues. Newton’s identities convert between them, and the recurrence is a compact way to accumulate exactly those quantities.
Division by
Over a field of characteristic , dividing by can fail. In particular this algorithm breaks modulo a small prime. Use the Hessenberg method or interpolation instead when working mod with .
Free byproducts
- Matrix inverse. From Cayley-Hamilton, (when ).
- Cayley-Hamilton check. verifies the computation.
- Determinant. .
- Trace of every power appears along the way — useful for counting closed walks of each length in a graph.
Contest use: linear recurrences
The characteristic polynomial is the bridge between matrix exponentiation and Kitamasa/Bostan-Mori. Given a transition matrix , its characteristic polynomial gives a length- linear recurrence for the sequence, after which can be computed in or instead of . For a transition matrix and , that is the difference between too slow and comfortable.
Variants / Use Cases
- Berlekamp-Massey — recover the recurrence from the sequence directly, usually easier than computing the characteristic polynomial
- Wiedemann — the sparse-matrix analogue,
- Determinant and Matrices — the topic pages
- Bareiss — for exact integer determinants