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 S, of the same dimensions as A, to the Smith normal form of A. S may be A. fmpz_mat_snf and fmpz_mat_snf_kannan_bachem accept any matrix; fmpz_mat_snf_iliopoulos works modulo mod and requires A to be square and nonsingular with mod a positive multiple of its largest elementary divisor, for example of |\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 X and of X^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 S to the Smith normal form of the m \times n matrix A and U (m \times m) and V (n \times n) to unimodular matrices with S = UAV. U or V may be NULL if it is not wanted. S may be A; U and V must be distinct from A, S and each other.

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

Sets ed to the nonzero elementary divisors d_1 \mid \cdots \mid d_r of A, where r is the rank, and returns r. ed must have room for \min(m, n) entries.

The function computes the Hermite normal form of A, factors its pivots and, if all their prime factors fit in a word, determines the p-adic valuations of the elementary divisors for each such prime p with Lübeck's algorithm [Lüb2002], which needs only ranks and nullspaces modulo p. If a pivot has more than 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 \times n matrix A generate a subgroup L of \mathbb{Z}^n, the diagonal entries d_1 \mid d_2 \mid \cdots \mid d_r of its Smith form give the structure of the quotient,

\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 p-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 p-adic method for elementary divisors — with the choices made by fmpz_mat_hnf and fmpz_mat_snf.

Definition#

For every m \times n integer matrix A of rank r there are unimodular matrices U (m \times m) and V (n \times n) such that S = UAV is diagonal with diagonal entries d_1, \ldots, d_r, 0, \ldots, 0, where d_i > 0 and d_i \mid d_{i+1}. The d_i, the elementary divisors or invariant factors of A, are unique: d_1 d_2 \cdots d_k is the gcd of the k \times k minors of A. S is the Smith normal form of A; U and V are not unique. fmpz_mat_is_in_snf checks this definition.

Usage#

  • fmpz_mat_snf(S, A) sets S, of the dimensions of A, to its Smith form. S may be A.
  • fmpz_mat_snf_transform(S, U, V, A) also computes U and V; either may be NULL. U and V must be distinct from each other and from A and S.
  • fmpz_mat_elementary_divisors(ed, A) writes d_1, \ldots, d_r to the vector ed, which needs room for \min(m, n) entries, and returns r.
  • 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|, 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 A 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 U and V. The divisibility chain is then fixed with extended gcds: for a pair of diagonal entries (a, b) with g = \gcd(a, b) = sa + tb, a few row and column operations, applied to U and V as well, turn \operatorname{diag} (a, b) into \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 H of A and factors its pivots: the product of the pivots is a multiple of d_1 \cdots d_r, so the elementary divisors are products of powers of the primes dividing the pivots. For each such prime p, the valuations v_p(d_i) follow from the ranks modulo p of a sequence of lattices obtained by Lübeck's algorithm [Lüb2002]: as long as the r nonzero rows R of H have rank less than r modulo p, each vector y of a basis of the left nullspace of R modulo p gives the integral vector yR/p, which replaces a row of R; the nullities at the successive steps count the d_i with v_p(d_i) \ge 1, 2, \ldots. If a pivot has more than 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 \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-10-20).