FLINT3.7.0-dev
Algorithm

Hermite and Smith normal forms

Cost
Polynomial in the dimension and the bit size for all algorithms except the classical one; computations modulo DD keep entries below DD and cost O(n3)O(n^3) operations on integers of log⁡2D\log_2 D bits for an n×nn \times n matrix.

The Hermite normal form (HNF) is to integer matrices what the reduced row echelon form is to matrices over a field: a canonical form under the row operations that are invertible over Z\mathbb{Z}. The Smith normal form (SNF) allows column operations as well and describes a matrix up to equivalence; it gives the structure of finitely generated abelian groups. Both are computed by elimination with gcds, and the main difficulty is the growth of the entries, which the algorithms below control in different ways. There are no generic gr_mat versions of these algorithms; they are specific to Z\mathbb{Z} (fmpz_mat). Over Z/NZ\mathbb{Z}/N\mathbb{Z} the Howell form plays the role of the HNF (Howell form).

Definitions#

A matrix U∈Zm×mU \in \mathbb{Z}^{m \times m} is unimodular if det⁡U=±1\det U = \pm 1, equivalently if U−1U^{-1} is an integer matrix. Multiplying AA on the left by UU performs a sequence of elementary row operations over Z\mathbb{Z}: exchanging rows, negating a row, adding an integer multiple of a row to another. The rows of AA and of UAUA generate the same lattice L(A)={xTA:x∈Zm}⊆ZnL(A) = \{x^T A : x \in \mathbb{Z}^m\} \subseteq \mathbb{Z}^n.

Definition 1. An m×nm \times n integer matrix HH of rank rr is in Hermite normal form if its first rr rows are nonzero and the others zero, the first nonzero entry of row i<ri < r is positive and lies in column jij_i with j0<j1<⋯<jr−1j_0 < j_1 < \cdots < j_{r-1}, and 0≤Hkji<Hiji0 \le H_{k j_i} < H_{i j_i} for all k<ik < i.

Theorem 2. For every integer matrix AA there is exactly one HH in Hermite normal form with H=UAH = UA for a unimodular UU. Two matrices of the same shape have the same Hermite normal form if and only if their rows generate the same lattice.

Proof. Existence follows from the algorithms below. For uniqueness, let HH and H′H' be in Hermite normal form with the same row lattice. The pivot columns are determined by the lattice (jij_i is the least jj such that the lattice vectors vanishing in columns before jj have rank r−ir - i), so they agree, and the pivots agree since HijiH_{i j_i} is the positive generator of the ideal of values in column jij_i of lattice vectors vanishing before jij_i. If H≠H′H \ne H', let ii be the last row in which they differ; then Hi−Hi′H_i - H'_i is a lattice vector vanishing up to column jij_i, so it is a combination of rows i+1,…i + 1, \ldots of HH; its first nonzero entry is in some pivot column jkj_k with k>ik > i and is then a multiple of HkjkH_{k j_k}, which contradicts the reduction 0≤Hijk,Hijk′<Hkjk0 \le H_{i j_k}, H'_{i j_k} < H_{k j_k}.

For a nonsingular n×nn \times n matrix, HH is upper triangular and ∏iHii=∣det⁡A∣=[Zn:L(A)]\prod_i H_{ii} = |\det A| = [\mathbb{Z}^n : L(A)].

Theorem 3 (Smith normal form). For every m×nm \times n integer matrix AA of rank rr there are unimodular UU, VV with UAV=diag⁡(d1,…,dr,0,…,0)UAV = \operatorname{diag}(d_1, \ldots, d_r, 0, \ldots, 0), di>0d_i > 0 and d1∣d2∣⋯∣drd_1 \mid d_2 \mid \cdots \mid d_r. The did_i are unique: d1d2⋯dkd_1 d_2 \cdots d_k is the gcd gkg_k of all k×kk \times k minors of AA.

Proof. Existence follows from the algorithms below. The gcd of the k×kk \times k minors is invariant under multiplication by unimodular matrices, since by the Cauchy–Binet formula the minors of UAUA are integer combinations of those of AA and conversely. For the diagonal form the gcd is d1⋯dkd_1 \cdots d_k because of the divisibility chain.

The did_i are the elementary divisors (or invariant factors) of AA, and Zn/L(A)≅⨁iZ/diZ⊕Zn−r\mathbb{Z}^n/L(A) \cong \bigoplus_i \mathbb{Z}/d_i\mathbb{Z} \oplus \mathbb{Z}^{n - r}. The largest one, drd_r, is the exponent of the torsion part.

Hermite normal form algorithms#

Classical elimination#

fmpz_mat_hnf_classical processes the columns from left to right. In the current column it repeatedly picks the nonzero entry of least absolute value at or below the current row, moves it into place and reduces the entries below it with integer division, until they are zero; then it makes the pivot positive and reduces the entries above it [Coh1996, Algorithm 2.4.4]. fmpz_mat_hnf_xgcd instead combines two rows with the cofactors u,vu, v of g=gcd⁡(a,b)=ua+vbg = \gcd(a, b) = ua + vb by the unimodular transformation

(uv−b/ga/g),\begin{pmatrix} u & v \\ -b/g & a/g \end{pmatrix},

which clears an entry in one step [Coh1996, Algorithm 2.4.5]. Both are fast for small matrices, but the entries of the rows not yet processed can grow exponentially with the dimension [HM1991].

Kannan–Bachem#

fmpz_mat_hnf_minors [KanBac1979] brings the leading principal k×kk \times k submatrix into Hermite normal form for k=1,2,…k = 1, 2, \ldots: row kk is eliminated against rows 0,…,k−10, \ldots, k - 1 with the gcd transformation above, and then the entries above the new diagonal are reduced modulo the pivots. Because all rows processed so far are kept reduced, Kannan and Bachem prove that every intermediate entry has a number of bits polynomial in nn and in the size of AA, which gives the first polynomial-time Hermite normal form algorithm. FLINT's version requires AA to have full column rank; it brings a zero diagonal entry into place by exchanging with a later row. fmpz_mat_hnf_minors_transform applies the same operations to an identity matrix to obtain UU.

Computing modulo a determinant#

The entries of an HNF are bounded by its pivots, so it is natural to compute modulo a multiple of them.

Lemma 4. Let L⊆ZnL \subseteq \mathbb{Z}^n be a lattice of rank nn with elementary divisors d1∣⋯∣dnd_1 \mid \cdots \mid d_n. Then dnZn⊆Ld_n \mathbb{Z}^n \subseteq L, and hence DZn⊆LD \mathbb{Z}^n \subseteq L for every multiple DD of dnd_n, in particular for D=det⁡L=d1⋯dnD = \det L = d_1 \cdots d_n.

Proof. The group Zn/L≅⨁iZ/diZ\mathbb{Z}^n / L \cong \bigoplus_i \mathbb{Z}/d_i\mathbb{Z} is annihilated by dnd_n.

Hence LL is generated by the rows of AA together with the rows of DInD I_n, and all row operations can be done modulo DD, which bounds the entries by DD. fmpz_mat_hnf_modular is the algorithm of Domich, Kannan and Trotter [DomKanTro1987; Coh1996, Algorithm 2.4.8]: it eliminates column kk with gcd transformations, reducing modulo the current RR, puts gcd⁡(Hkk,R)\gcd(H_{kk}, R) on the diagonal, and divides RR by the pivot for the next column, since the lattice generated by the remaining columns has determinant R/HkkR/H_{kk}. fmpz_mat_hnf_modular_eldiv uses the weaker requirement dn∣Dd_n \mid D: it computes the strong echelon form modulo DD (nmod_mat_strong_echelon_form or fmpz_mat_strong_echelon_form_mod), which by the result of Fieker and Hofmann [FieHof2014] is the Hermite normal form of the lattice L+DZn=LL + D\mathbb{Z}^n = L reduced modulo DD, and replaces zero diagonal entries, which stand for pivots equal to DD, by DD. Hafner and McCurley [HM1991] and Storjohann and Labahn [SL1996] give asymptotically faster modular algorithms based on matrix multiplication.

Pernet–Stein#

For a nonsingular square matrix, D=∣det⁡A∣D = |\det A| can be huge, but for random matrices the lattice generated by n−1n - 1 of the rows together with a combination of the last two has a small determinant. Pernet and Stein [PernetStein2010], after Micciancio and Warinschi [MW2001], exploit this. For an m×nm \times n matrix of rank rr, fmpz_mat_hnf_pernet_stein does the following.

  1. Find the pivot rows and columns modulo a random prime; let BB be the first r−2r - 2 pivot rows and cc, dd the next two, all restricted to the first r−1r - 1 pivot columns.
  2. Double determinant: compute d1=det⁡(Bc)d_1 = \det \binom{B}{c} and d2=det⁡(Bd)d_2 = \det \binom{B}{d}, both from one solution of a linear system over Q\mathbb{Q} (fmpq_mat_solve_fmpz_mat) and a few modular determinants. Let g=gcd⁡(d1,d2)=sd1+td2g = \gcd(d_1, d_2) = s d_1 + t d_2.
  3. Replace the pair c,dc, d by sc+tdsc + td; the resulting (r−1)×(r−1)(r - 1) \times (r - 1) matrix CC has determinant ±g\pm g, which is small with high probability for random input. Compute its HNF modulo gg (fmpz_mat_hnf_modular), or recursively if gg is large.
  4. Add columns: the HNF HCH_C of CC is UCUC with U=HCC−1U = H_C C^{-1} unimodular, so the HNF of the same r−1r - 1 rows on all columns is UU times these rows. The other columns are obtained by solving a linear system with CC.
  5. Add rows: insert the remaining rows of AA, including the original rows cc and dd, one at a time into the HNF by gcd elimination and reduction.
  6. If the pivot structure obtained differs from the one predicted modulo the prime, the prime was bad: restart.

The expected cost for random matrices is dominated by linear system solving, O∼(nω+1b)O^\sim(n^{\omega + 1} b) bit operations. FLINT falls back to fmpz_mat_hnf_minors when AA has full column rank and fewer than about 35 to 52 columns, and when the rank is too small for the decomposition. Faster algorithms that certify unimodularity and handle structured input better are given in [PauSto2013].

Choice and transformation#

fmpz_mat_hnf calls fmpz_mat_hnf_classical for matrices with few rows (fewer than 52 rows for entries of 1 or 2 bits, decreasing to 2 rows for entries above 512 bits) and fmpz_mat_hnf_pernet_stein otherwise. fmpz_mat_hnf_transform computes UU with fmpz_mat_hnf_minors_transform for matrices of full column rank, and otherwise as the right block of the HNF of (A∣Im)(A \mid I_m): that matrix has full row rank, so its HNF is U(A∣Im)=(UA∣U)U(A \mid I_m) = (UA \mid U) with UU unimodular.

Smith normal form algorithms#

Elimination#

fmpz_mat_snf_kannan_bachem [KanBac1979] alternately clears the first column with row gcd transformations and the first row with column gcd transformations until both are zero apart from the corner, and repeats if the corner does not divide the remaining entries (adding a row to the first row and continuing). Then it proceeds with the submatrix. fmpz_mat_snf_iliopoulos [Iliopoulos1989] does the same eliminations modulo a multiple DD of the largest elementary divisor of a nonsingular square matrix, which by Lemma 4 does not change the result and bounds the entries by DD; the diagonal is finally normalised by fmpz_mat_snf_diagonal, which replaces pairs of diagonal entries by their gcd and lcm.

Iterated Hermite forms#

fmpz_mat_snf uses a different approach: it alternates the row HNF of the matrix and the row HNF of its transpose (that is, the column HNF) until the matrix is diagonal. Each round is a unimodular transformation, and the leading entries can only become divisors of what they were; Kannan and Bachem show that the number of rounds is bounded by a polynomial in the dimension and the size of the entries [KanBac1979]. The diagonal is then turned into a divisibility chain by gcd and lcm steps. Since fmpz_mat_hnf is fast (Pernet–Stein for large matrices), this inherits its performance; two or three rounds suffice for typical matrices. fmpz_mat_snf_transform does the same with fmpz_mat_hnf_transform and accumulates UU and VV; the final gcd steps are done with extended gcds on pairs of rows and columns.

Elementary divisors by p-adic ranks#

When only the elementary divisors are needed and their prime factors are known, Lübeck's method [Lüb2002] computes the valuations from ranks modulo primes. Let RR be an r×nr \times n basis of the row lattice LL (the nonzero rows of the HNF), with elementary divisors e1,…,ere_1, \ldots, e_r.

Proposition 5. Let pp be a prime, let y1,…,yky_1, \ldots, y_k be a basis of the left kernel of RR modulo pp (so k=r−rank⁡pRk = r - \operatorname{rank}_p R), and let R′R' be obtained from RR by replacing, for each jj, a row iji_j in which yjy_j has entry 1 (and the other yly_l have entry 0) by yjR/py_j R / p. Then R′R' is a basis of L′=L+1p(L∩pZn)L' = L + \frac1p (L \cap p\mathbb{Z}^n), and the elementary divisors of L′L' are ei/pe_i/p for the kk indices with p∣eip \mid e_i and eie_i for the others.

Proof. Choose bases in which L=⨁ieiZfiL = \bigoplus_i e_i \mathbb{Z} f_i (Smith form). Then L∩pZn=⨁ilcm⁡(ei,p)ZfiL \cap p\mathbb{Z}^n = \bigoplus_i \operatorname{lcm}(e_i, p) \mathbb{Z} f_i and L′=⨁iei′ZfiL' = \bigoplus_i e_i' \mathbb{Z} f_i with ei′=ei/pe_i' = e_i/p if p∣eip \mid e_i and ei′=eie_i' = e_i otherwise; in particular rank⁡pR=#{i:p∤ei}\operatorname{rank}_p R = \#\{i : p \nmid e_i\}, so k=#{i:p∣ei}k = \#\{i : p \mid e_i\}. The vectors yjRy_j R span L∩pZnL \cap p\mathbb{Z}^n modulo pLpL, so the rows of R′R' generate L′L', and the replaced rows are recovered as p⋅(yjR/p)p \cdot (y_j R/p) minus the other terms of yjRy_j R, so nothing is lost; R′R' has rr rows, so it is a basis.

Iterating until RR has full rank modulo pp, the nullities k1≥k2≥⋯k_1 \ge k_2 \ge \cdots count the elementary divisors with vp(ei)≥1,2,…v_p(e_i) \ge 1, 2, \ldots, which determines all valuations. fmpz_mat_elementary_divisors computes the HNF, factors its pivots (whose product is divisible by every prime dividing an eie_i), applies this for each prime, and falls back to fmpz_mat_snf when a pivot is too large to factor or has a prime factor beyond a word. Faster Smith form algorithms based on linear system solving are given in [Gie1995; EGV2000].

Use in FLINT#

Referenced from#

References#

  • [Coh1996] Cohen, Henri : A course in computational algebraic number theory, Springer, 1996
  • [HM1991] J. L. Hafner and K. S. McCurley, "Asymptotically fast triangularization of matrices over rings", SIAM Journal on Computing 20:6 (1991), 1068–1083. https://doi.org/10.1137/0220067
  • [KanBac1979] Kannan, R. and Bachem, A. : Polynomial algorithms for computing and the Smith and Hermite normal forms of an integer matrix, SIAM J. Computation vol. 9 (1979) 499–507
  • [DomKanTro1987] Domich, P. D. and Kannan, R. and Trotter, L. E. Jr. : Hermite Normal Form Computation Using Modulo Determinant Arithmetic, Math. Operations Res. (12) 1987 50-59
  • [FieHof2014] Fieker C. and Hofmann T.: "Computing in quotients of rings of integers" LMS Journal of Computation and Mathematics, 17(A), 349-365
  • [SL1996] A. Storjohann and G. Labahn, "Asymptotically fast computation of Hermite normal forms of integer matrices", Proceedings of the 1996 International Symposium on Symbolic and Algebraic Computation (ISSAC 1996), 259–266. https://doi.org/10.1145/236869.237083
  • [PernetStein2010] Pernet, C. and Stein, W. : Fast computation of Hermite normal forms of random integer matrices ,J. Number Theory 130:17 (2010) 1675–1683
  • [MW2001] D. Micciancio and B. Warinschi, "A linear space algorithm for computing the Hermite normal form", Proceedings of the 2001 International Symposium on Symbolic and Algebraic Computation (ISSAC 2001), 231–236. https://doi.org/10.1145/384101.384133
  • [PauSto2013] C. Pauderis and A. Storjohann, "Computing the invariant structure of integer matrices: fast algorithms into practice", Proceedings of the 2013 International Symposium on Symbolic and Algebraic Computation (ISSAC 2013), 307–314. https://doi.org/10.1145/2465506.2465955
  • [Iliopoulos1989] Iliopoulos, C. S., Worst-Case Complexity Bounds on Algorithms for Computing the Canonical Structure of Finite Abelian Groups and the Hermite and Smith Normal Forms of an Integer Matrix : SIAM J. Computation 18:4 (1989) 658
  • [Lüb2002] F. Lübeck, "On the computation of elementary divisors of integer matrices", Journal of Symbolic Computation 33:1 (2002), 57–65. https://doi.org/10.1006/jsco.2001.0492
  • [Gie1995] M. Giesbrecht, "Fast computation of the Smith normal form of an integer matrix", Proceedings of the 1995 International Symposium on Symbolic and Algebraic Computation (ISSAC 1995), 110–118. https://doi.org/10.1145/220346.220361
  • [EGV2000] W. Eberly, M. Giesbrecht and G. Villard, "On computing the determinant and Smith form of an integer matrix", Proceedings of the 41st Annual Symposium on Foundations of Computer Science (FOCS 2000), 675–685. https://doi.org/10.1109/SFCS.2000.892335
Cite this page

The FLINT team, “Hermite and Smith normal forms,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/linear-algebra/hermite-smith.html (generated 2026-11-02).