FLINT3.7.0-dev

Smith normal form and elementary divisors

Synopsis#include <flint/fmpz_mat.h>
void fmpz_mat_snf(fmpz_mat_t S, const fmpz_mat_t A)source
void fmpz_mat_snf_kannan_bachem(fmpz_mat_t S, const fmpz_mat_t A)source
void fmpz_mat_snf_iliopoulos(fmpz_mat_t S, const fmpz_mat_t A, const fmpz_t mod)source

Sets SS, of the same dimensions as AA, to the Smith normal form of AA. SS may be AA. fmpz_mat_snf and fmpz_mat_snf_kannan_bachem accept any matrix; fmpz_mat_snf_iliopoulos works modulo mod and requires AA to be square and nonsingular with mod a positive multiple of its largest elementary divisor, for example of ∣det⁡(A)∣|\det(A)|.

fmpz_mat_snf returns at once for a diagonal matrix (fmpz_mat_snf_diagonal) and otherwise alternates row and column Hermite normal forms (fmpz_mat_hnf of XX and of XTX^T) until the matrix is diagonal, then fixes the divisibility of the diagonal with gcd and lcm operations.

void fmpz_mat_snf_transform(fmpz_mat_t S, fmpz_mat_t U, fmpz_mat_t V, const fmpz_mat_t A)source

Sets SS to the Smith normal form of the m×nm \times n matrix AA and UU (m×mm \times m) and VV (n×nn \times n) to unimodular matrices with S=UAVS = UAV. UU or VV may be NULL if it is not wanted. SS may be AA; UU and VV must be distinct from AA, SS and each other.

slong fmpz_mat_elementary_divisors(fmpz * ed, const fmpz_mat_t A)source

Sets ed to the nonzero elementary divisors d1∣⋯∣drd_1 \mid \cdots \mid d_r of AA, where rr is the rank, and returns rr. ed must have room for min⁡(m,n)\min(m, n) entries.

The function computes the Hermite normal form of AA, factors its pivots and, if all their prime factors fit in a word, determines the pp-adic valuations of the elementary divisors for each such prime pp with Lübeck's algorithm [Lüb2002], which needs only ranks and nullspaces modulo pp. If a pivot has more than 2β2\beta bits or cannot be factored cheaply, it falls back to fmpz_mat_snf.

The Smith normal form describes an integer matrix up to unimodular transformations on both sides. If the rows of the m×nm \times n matrix AA generate a subgroup LL of Zn\mathbb{Z}^n, the diagonal entries d1∣d2∣⋯∣drd_1 \mid d_2 \mid \cdots \mid d_r of its Smith form give the structure of the quotient,

Zn/L≅Z/d1Z×⋯×Z/drZ×Zn−r,\mathbb{Z}^n / L \cong \mathbb{Z}/d_1\mathbb{Z} \times \cdots \times \mathbb{Z}/d_r\mathbb{Z} \times \mathbb{Z}^{n - r},

which is how finitely presented abelian groups, class groups and homology groups are computed. FLINT computes the Smith form by alternating Hermite normal forms, optionally with the transformation matrices, and the elementary divisors alone by a pp-adic method.

AlgorithmHermite and Smith normal formsThe Hermite and Smith normal forms of integer matrices and the algorithms FLINT implements for them — classical and gcd-based elimination, Kannan–Bachem, Domich–Kannan–Trotter and other computations modulo a determinant, the Pernet–Stein algorithm, Iliopoulos' algorithm, iterated Hermite forms, and Lübeck's pp-adic method for elementary divisors — with the choices made by fmpz_mat_hnf and fmpz_mat_snf.

Definition#

For every m×nm \times n integer matrix AA of rank rr there are unimodular matrices UU (m×mm \times m) and VV (n×nn \times n) such that S=UAVS = UAV is diagonal with diagonal entries d1,…,dr,0,…,0d_1, \ldots, d_r, 0, \ldots, 0, where di>0d_i > 0 and di∣di+1d_i \mid d_{i+1}. The did_i, the elementary divisors or invariant factors of AA, are unique: d1d2⋯dkd_1 d_2 \cdots d_k is the gcd of the k×kk \times k minors of AA. SS is the Smith normal form of AA; UU and VV are not unique. fmpz_mat_is_in_snf checks this definition.

Usage#

  • fmpz_mat_snf(S, A) sets SS, of the dimensions of AA, to its Smith form. SS may be AA.
  • fmpz_mat_snf_transform(S, U, V, A) also computes UU and VV; either may be NULL. UU and VV must be distinct from each other and from AA and SS.
  • fmpz_mat_elementary_divisors(ed, A) writes d1,…,drd_1, \ldots, d_r to the vector ed, which needs room for min⁡(m,n)\min(m, n) entries, and returns rr.
  • fmpz_mat_snf_kannan_bachem and fmpz_mat_snf_iliopoulos are alternative algorithms; the latter needs a nonsingular square matrix and a multiple of its largest elementary divisor, for example ∣det⁡A∣|\det A|, and computes modulo that number.
  • fmpz_mat_snf_diagonal computes the Smith form of a diagonal matrix.

Algorithms#

fmpz_mat_snf returns at once with fmpz_mat_snf_diagonal if AA is diagonal, which repeatedly replaces pairs of diagonal entries by their gcd and lcm. Otherwise it alternates row-style Hermite normal forms of the matrix and of its transpose (fmpz_mat_hnf, hence the Pernet–Stein algorithm for large matrices) until the matrix is diagonal, then makes the diagonal a divisibility chain by gcd and lcm steps and makes it nonnegative. Each Hermite form is upper triangular and the next one of the transpose clears the entries above the diagonal while possibly creating new ones below; the number of rounds is small in practice, and Kannan and Bachem showed that it is polynomial [KanBac1979]. A defensive bound (_fmpz_mat_snf_iter_bound) raises an exception if it is ever exceeded.

fmpz_mat_snf_transform runs the same iteration with fmpz_mat_hnf_transform, accumulating the transforms in UU and VV. The divisibility chain is then fixed with extended gcds: for a pair of diagonal entries (a,b)(a, b) with g=gcd⁡(a,b)=sa+tbg = \gcd(a, b) = sa + tb, a few row and column operations, applied to UU and VV as well, turn diag⁡(a,b)\operatorname{diag} (a, b) into diag⁡(g,ab/g)\operatorname{diag}(g, ab/g). The construction follows the implementation by Jean Kieffer in acb_theta.

fmpz_mat_snf_kannan_bachem eliminates the first row and column alternately with extended gcd operations until the leading entry divides all others [KanBac1979]. fmpz_mat_snf_iliopoulos does the same eliminations modulo a multiple of the largest elementary divisor, which bounds the entries [Iliopoulos1989].

fmpz_mat_elementary_divisors computes the Hermite normal form HH of AA and factors its pivots: the product of the pivots is a multiple of d1⋯drd_1 \cdots d_r, so the elementary divisors are products of powers of the primes dividing the pivots. For each such prime pp, the valuations vp(di)v_p(d_i) follow from the ranks modulo pp of a sequence of lattices obtained by Lübeck's algorithm [Lüb2002]: as long as the rr nonzero rows RR of HH have rank less than rr modulo pp, each vector yy of a basis of the left nullspace of RR modulo pp gives the integral vector yR/pyR/p, which replaces a row of RR; the nullities at the successive steps count the did_i with vp(di)≥1,2,…v_p(d_i) \ge 1, 2, \ldots. If a pivot has more than 2β2\beta bits or a prime factor that does not fit in a word, the function computes the full Smith form instead.

Example#

The quotient of Z3\mathbb{Z}^3 by the lattice of Example, and the same computation with transformation matrices:

Examplefmpz_mat_snf_group.cchecked by make checkdoc
#include <flint/flint.h>
#include <flint/fmpz.h>
#include <flint/fmpz_vec.h>
#include <flint/fmpz_mat.h>

int main(void)
{
    fmpz_mat_t A, S, U, V, T;
    fmpz * ed;
    slong i, j, r;
    slong rows[3][3] = { { 4, 6, 10 }, { 2, -2, 8 }, { 6, 14, -6 } };

    fmpz_mat_init(A, 3, 3);
    fmpz_mat_init(S, 3, 3);
    fmpz_mat_init(U, 3, 3);
    fmpz_mat_init(V, 3, 3);
    fmpz_mat_init(T, 3, 3);
    ed = _fmpz_vec_init(3);

    for (i = 0; i < 3; i++)
        for (j = 0; j < 3; j++)
            fmpz_set_si(fmpz_mat_entry(A, i, j), rows[i][j]);

    fmpz_mat_snf(S, A);
    fmpz_mat_print_pretty(S);
    flint_printf("\nin SNF: %d\n", fmpz_mat_is_in_snf(S));

    r = fmpz_mat_elementary_divisors(ed, A);
    flint_printf("Z^3 / L = ");
    for (i = 0; i < r; i++)
        flint_printf("%sZ/%{fmpz}", i ? " x " : "", ed + i);
    flint_printf("\n");

    fmpz_mat_snf_transform(T, U, V, A);
    fmpz_mat_mul(T, U, A);
    fmpz_mat_mul(T, T, V);
    flint_printf("U A V = S: %d\n", fmpz_mat_equal(T, S));

    fmpz_mat_clear(A);
    fmpz_mat_clear(S);
    fmpz_mat_clear(U);
    fmpz_mat_clear(V);
    fmpz_mat_clear(T);
    _fmpz_vec_clear(ed, 3);
    return 0;
}
Output
[[2 0 0]
[0 2 0]
[0 0 90]
]
in SNF: 1
Z^3 / L = Z/2 x Z/2 x Z/90
U A V = S: 1

Guides#

References#

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

The FLINT team, “Smith normal form and elementary divisors (fmpz_mat.h),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/fmpz_mat/snf.html (generated 2026-11-02).