Characteristic and minimal polynomials
- Cost
- ring operations for Berkowitz's algorithm, field operations for Danilevsky's algorithm, and field operations in typical cases (at most ) for the minimal polynomial of an matrix.
Let be an matrix over . Its characteristic polynomial can be computed with operations over a field by the algorithms of Keller-Gehrig [KG1985] and Pernet and Storjohann [PS2007], which reduce the problem to matrix multiplication; see [DPW2005] for a practical comparison. FLINT uses two simpler algorithms instead, Danilevsky's cubic reduction to companion form and Berkowitz's quartic division-free algorithm, chosen so that the result is correct for every modulus , prime or not. The minimal polynomial is computed with a Krylov method (Minimal polynomial).
Generic version#
Both characteristic polynomial algorithms are implemented once in gr_mat (_gr_mat_charpoly_berkowitz, _gr_mat_charpoly_danilevsky), together with the Faddeev-LeVerrier algorithm and its baby-step giant-step variant (which divide by and need characteristic zero or larger than ) and reduction to Hessenberg form. Berkowitz's algorithm, being division-free, is the default over any commutative ring. Rings override GR_METHOD_MAT_CHARPOLY to use Danilevsky's algorithm above a dimension: 8 for nmod, nmod8 and nmod32 and 15 for fmpz_mod, with Berkowitz's algorithm as the fallback when a pivot is not invertible, and 12 for the fq family; fmpz and fmpq use fmpz_mat_charpoly, which reduces modulo primes for large matrices (Characteristic and minimal polynomials). nmod_mat_charpoly uses its own implementations of the two algorithms, except in the case described in Danilevsky's algorithm. The minimal polynomial is computed by the same Krylov method over any field (gr_mat_minpoly_field), with the row reductions done by the ring method GR_METHOD_MAT_REDUCE_ROW, which fmpz_mod, mpn_mod and the fq and fq_nmod rings implement. Over exact real and complex numbers, where Danilevsky's pivots require zero tests, ca_mat_charpoly uses Berkowitz's algorithm by default (linear algebra over exact numbers).
Berkowitz's algorithm#
Berkowitz's algorithm [Ber1984] computes without any division, so it works over every commutative ring. For , let be the leading principal submatrix of and its characteristic polynomial, with . The algorithm computes in turn, each from the previous one and a few entries of powers of .
Lemma 1. Let , the bottom right entry of the -th power of , for . Then, as power series in ,
Proof. By Cramer's rule, the bottom right entry of is the cofactor divided by , that is . On the other hand, as a power series in . Comparing bottom right entries and substituting gives , and multiplying by gives the claim.
Since , the series is invertible without division, and the reversed polynomial is the product of the reversed and the inverse series, truncated to degree . Writing and , this is the triangular recurrence
with . The numbers come from the vectors : is column of , each further vector is a matrix-vector product with , and is the last entry of ; the last one, , only needs the last row of times .
Step costs about matrix-vector products of dimension , so the algorithm needs about multiply-adds in all. _nmod_mat_charpoly_berkowitz computes each entry of these products as a dot product with a single reduction (Dot products modulo a word-size integer). Despite its quartic cost it is the fastest method for , and nmod_mat_det uses it for . Berkowitz designed the algorithm for parallel computation: the matrix-vector products of all steps can be organised as products of matrices, giving depth .
Danilevsky's algorithm#
Danilevsky's method [Dan1937] transforms by similarity transforms into a block triangular matrix whose diagonal blocks are companion matrices, whose characteristic polynomials can be read off; see also [Coh1996] for the related Hessenberg method. nmod_mat_charpoly_danilevsky works on a copy of from the bottom row upwards. At step , the rows below already form the last rows of a companion matrix, and the step makes row equal to the unit vector :
- Let be the entry of row just left of the diagonal. If , look for a nonzero entry further left in the same row and move it into place by exchanging two rows and the corresponding two columns (a similarity transform by a permutation).
- If the row has no nonzero entry left of the diagonal, the trailing block of size is a companion matrix and is decoupled from the rest: multiply the result by its characteristic polynomial, read off its bottom row, and continue with the leading block of size .
- Otherwise, apply the similarity transform by the identity matrix with row replaced by row of , whose inverse involves . This turns row into and costs one rank-one update of the matrix and one row of dot products, operations.
When the leading block is a full companion matrix, its characteristic polynomial is multiplied into the result. There are steps of cost , so operations in all. Every dot product is computed with a single reduction.
Over a field the algorithm cannot fail. Over with composite , step 3 needs the inverse of ; if is a nonzero zero divisor, the function returns 0 and nmod_mat_charpoly falls back to Berkowitz's algorithm. A zero divisor can appear even when the result is well defined: the method depends on the arithmetic of a field, not just on the ring.
nmod_mat_charpoly uses Danilevsky's algorithm for . For prime and it runs it through the gr context for nmod8, on a copy of the matrix stored in bytes, which reduces the memory traffic of the rank-one updates.
Minimal polynomial#
The minimal polynomial of is the monic generator of the ideal of polynomials with . For a vector , the minimal polynomial of with respect to is the monic generator of the ideal of with ; its degree is the dimension of the Krylov space , and its coefficients express the first vector that is linearly dependent on the previous ones.
Theorem 2. Over a field, if is the whole space, then .
Proof. Each divides , since , so their least common multiple divides . Conversely, for all and , so vanishes on a spanning set and . Hence divides .
nmod_mat_minpoly applies this with unit vectors.
Input: .
Output: .
- , , .
While is not the whole space:
- compute , reducing each new vector against the previous ones (kept in echelon form, together with the combination that produced them), until one reduces to zero; the combination gives ;
- , computed as ;
- add the Krylov vectors to , also kept in echelon form, and let be the first index such that is not in .
- Return .
The rows are reduced with nmod_mat_reduce_row, which accumulates the updates of a row in up to three words and reduces each entry once. Each Krylov vector costs a matrix-vector product and a row reduction, operations. If already generates the space, which is often the case, there are vectors and the cost is . A matrix whose minimal polynomial has small degree, such as a scalar matrix, needs up to generators, and the number of Krylov vectors is bounded by in all cases. Algorithms with operations, also based on Krylov spaces, are described in [Sto2000].
Referenced from#
_acb_mat_charpoly,acb_mat_charpoly- Linear algebra over exact numbers (algorithm)
- Gaussian elimination and LU decomposition (algorithm)
- Multimodular and p-adic linear algebra over the integers (algorithm)
- Linear algebra over generic rings and its specialisations (overview)
- Optimal precision in p-adic linear algebra (algorithm)
_arb_mat_charpoly,arb_mat_charpoly- Characteristic and minimal polynomials
fq_nmod_mat_charpolyfq_mat_charpolyfq_zech_mat_charpoly- Determinants and permanents
_gr_mat_charpoly,gr_mat_charpoly,_gr_mat_charpoly_generic,gr_mat_charpoly_genericgr_mat_minpoly_field- Computing characteristic and minimal polynomials
- nmod_mat.h (module)
References#
- [KG1985] W. Keller-Gehrig, "Fast algorithms for the characteristic polynomial", Theoretical Computer Science 36 (1985), 309-317, https://doi.org/10.1016/0304-3975(85)90049-0
- [PS2007] C. Pernet and A. Storjohann, "Faster algorithms for the characteristic polynomial", Proceedings of the 2007 International Symposium on Symbolic and Algebraic Computation (ISSAC) (2007), 307-314
- [DPW2005] J.-G. Dumas, C. Pernet and Z. Wan, "Efficient computation of the characteristic polynomial", Proceedings of the 2005 International Symposium on Symbolic and Algebraic Computation (ISSAC) (2005), 140-147
- [Ber1984] S. J. Berkowitz, "On computing the determinant in small parallel time using a small number of processors", Information Processing Letters 18 (1984), 147-150
- [Dan1937] A. M. Danilevsky, "On the numerical solution of the secular equation" (in Russian), Matematicheskii Sbornik 2(44):1 (1937), 169-171
- [Coh1996] Cohen, Henri : A course in computational algebraic number theory, Springer, 1996
- [Sto2000] A. Storjohann, Algorithms for Matrix Canonical Forms, PhD thesis, ETH Zürich (2000), https://doi.org/10.3929/ethz-a-004141007
The FLINT team, “Characteristic and minimal polynomials,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/linear-algebra/charpoly.html (generated 2026-11-02).
@misc{flint-doc-algorithms-linear-algebra-charpoly,
author = {{The FLINT team}},
title = {{Characteristic and minimal polynomials}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/algorithms/linear-algebra/charpoly.html},
note = {Generated 2026-11-02}
}