FLINT3.7.0-dev
Algorithm

Characteristic and minimal polynomials

Cost
O(r4)O(r^4) ring operations for Berkowitz's algorithm, O(r3)O(r^3) field operations for Danilevsky's algorithm, and O(r3)O(r^3) field operations in typical cases (at most O(r4)O(r^4)) for the minimal polynomial of an r×rr \times r matrix.

Let MM be an r×rr \times r matrix over Z/NZ\mathbb{Z}/N\mathbb{Z}. Its characteristic polynomial χM(x)=det⁡(xI−M)\chi_M(x) = \det(xI - M) can be computed with O(rω)O(r^\omega) 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 NN, 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 1,…,r1, \ldots, r and need characteristic zero or larger than rr) 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 χM\chi_M without any division, so it works over every commutative ring. For 0≤t≤r0 \le t \le r, let MtM_t be the leading principal t×tt \times t submatrix of MM and χt\chi_t its characteristic polynomial, with χ0=1\chi_0 = 1. The algorithm computes χ1,χ2,…,χr\chi_1, \chi_2, \ldots, \chi_r in turn, each from the previous one and a few entries of powers of MtM_t.

Lemma 1. Let sk=(Mt+1k)t,ts_k = (M_{t+1}^k)_{t,t}, the bottom right entry of the kk-th power of Mt+1M_{t+1}, for k≥0k \ge 0. Then, as power series in yy,

ytχt(1/y)=yt+1χt+1(1/y)⋅∑k≥0skyk.y^t \chi_t(1/y) = y^{t+1} \chi_{t+1}(1/y) \cdot \sum_{k \ge 0} s_k y^k .

Proof. By Cramer's rule, the bottom right entry of (xI−Mt+1)−1(xI - M_{t+1})^{-1} is the cofactor det⁡(xI−Mt)\det(xI - M_t) divided by det⁡(xI−Mt+1)\det(xI - M_{t+1}), that is χt(x)/χt+1(x)\chi_t(x)/\chi_{t+1}(x). On the other hand, (xI−Mt+1)−1=∑k≥0Mt+1kx−k−1(xI - M_{t+1})^{-1} = \sum_{k \ge 0} M_{t+1}^k x^{-k-1} as a power series in 1/x1/x. Comparing bottom right entries and substituting x=1/yx = 1/y gives χt(1/y)=χt+1(1/y) y∑kskyk\chi_t(1/y) = \chi_{t+1}(1/y) \, y \sum_k s_k y^k, and multiplying by yty^t gives the claim.

Since s0=1s_0 = 1, the series ∑kskyk\sum_k s_k y^k is invertible without division, and the reversed polynomial χ~t+1(y)=yt+1χt+1(1/y)\tilde\chi_{t+1}(y) = y^{t+1} \chi_{t+1}(1/y) is the product of the reversed χ~t\tilde\chi_t and the inverse series, truncated to degree t+1t + 1. Writing χ~t=∑ibiyi\tilde\chi_t = \sum_i b_i y^i and χ~t+1=∑iciyi\tilde\chi_{t+1} = \sum_i c_i y^i, this is the triangular recurrence

c0=1,ck=bk−∑j=1ksj ck−j(1≤k≤t+1),c_0 = 1, \qquad c_k = b_k - \sum_{j=1}^{k} s_j \, c_{k-j} \quad (1 \le k \le t + 1),

with bt+1=0b_{t+1} = 0. The numbers s1,…,st+1s_1, \ldots, s_{t+1} come from the vectors uk=Mt+1ketu_k = M_{t+1}^k e_t: u1u_1 is column tt of Mt+1M_{t+1}, each further vector is a matrix-vector product with Mt+1M_{t+1}, and sks_k is the last entry of uku_k; the last one, st+1s_{t+1}, only needs the last row of Mt+1M_{t+1} times utu_t.

Step tt costs about tt matrix-vector products of dimension t+1t + 1, so the algorithm needs about r4/4r^4/4 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 r≤8r \le 8, and nmod_mat_det uses it for 5≤r≤85 \le r \le 8. Berkowitz designed the algorithm for parallel computation: the matrix-vector products of all steps can be organised as products of matrices, giving depth O(log⁡2r)O(\log^2 r).

Danilevsky's algorithm#

Danilevsky's method [Dan1937] transforms MM 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 MM from the bottom row upwards. At step ii, the rows below r−ir - i already form the last rows of a companion matrix, and the step makes row r−ir - i equal to the unit vector er−i−1e_{r-i-1}:

  1. Let hh be the entry of row r−ir - i just left of the diagonal. If h=0h = 0, 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).
  2. If the row has no nonzero entry left of the diagonal, the trailing block of size ii 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 r−ir - i.
  3. Otherwise, apply the similarity transform by the identity matrix with row r−i−1r - i - 1 replaced by row r−ir - i of MM, whose inverse involves h−1h^{-1}. This turns row r−ir - i into er−i−1e_{r-i-1} and costs one rank-one update of the matrix and one row of dot products, O(r2)O(r^2) operations.

When the leading block is a full companion matrix, its characteristic polynomial is multiplied into the result. There are r−1r - 1 steps of cost O(r2)O(r^2), so O(r3)O(r^3) operations in all. Every dot product is computed with a single reduction.

Over a field the algorithm cannot fail. Over Z/NZ\mathbb{Z}/N\mathbb{Z} with composite NN, step 3 needs the inverse of hh; if hh 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 r>8r > 8. For prime N≤255N \le 255 and r≥32r \ge 32 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 μM\mu_M of MM is the monic generator of the ideal of polynomials ff with f(M)=0f(M) = 0. For a vector vv, the minimal polynomial μM,v\mu_{M,v} of vv with respect to MM is the monic generator of the ideal of ff with f(M)v=0f(M) v = 0; its degree is the dimension of the Krylov space K(v)=span⁡{v,Mv,M2v,…}K(v) = \operatorname{span}\{v, Mv, M^2 v, \ldots\}, and its coefficients express the first vector MdvM^d v that is linearly dependent on the previous ones.

Theorem 2. Over a field, if K(v1)+⋯+K(vs)K(v_1) + \cdots + K(v_s) is the whole space, then μM=lcm⁡(μM,v1,…,μM,vs)\mu_M = \operatorname{lcm}(\mu_{M, v_1}, \ldots, \mu_{M, v_s}).

Proof. Each μM,vj\mu_{M, v_j} divides μM\mu_M, since μM(M)vj=0\mu_M(M) v_j = 0, so their least common multiple LL divides μM\mu_M. Conversely, L(M)Mivj=MiL(M)vj=0L(M) M^i v_j = M^i L(M) v_j = 0 for all ii and jj, so L(M)L(M) vanishes on a spanning set and L(M)=0L(M) = 0. Hence μM\mu_M divides LL.

nmod_mat_minpoly applies this with unit vectors.

Algorithm 1: Minimal polynomial

Input: M∈(Z/pZ)r×rM \in (\mathbb{Z}/p\mathbb{Z})^{r \times r}.

Output: μM\mu_M.

  1. μ←1\mu \gets 1, W←{0}W \gets \{0\}, j←0j \gets 0.
  2. While WW is not the whole space:

    1. compute v=ej,Mv,M2v,…v = e_j, Mv, M^2 v, \ldots, 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 μM,ej\mu_{M, e_j};
    2. μ←lcm⁡(μ,μM,ej)\mu \gets \operatorname{lcm}(\mu, \mu_{M, e_j}), computed as μμM,ej/gcd⁡(μ,μM,ej)\mu \mu_{M,e_j} / \gcd(\mu, \mu_{M,e_j});
    3. add the Krylov vectors to WW, also kept in echelon form, and let jj be the first index such that eje_j is not in WW.
  3. Return μ\mu.

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, O(r2)O(r^2) operations. If e0e_0 already generates the space, which is often the case, there are r+1r + 1 vectors and the cost is O(r3)O(r^3). A matrix whose minimal polynomial has small degree, such as a scalar matrix, needs up to rr generators, and the number of Krylov vectors is bounded by r(r+1)r (r + 1) in all cases. Algorithms with O(rω)O(r^\omega) operations, also based on Krylov spaces, are described in [Sto2000].

Referenced from#

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
Cite this page

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