Solving a system of linear congruences with possibly differing moduli, using the Smith normal form (SNF) of an integer matrix

We solve the system of linear congruences:

a11x1 + ⋯ + a1nxn ≡ b1 (mod q1)
.
.
.
am1x1 + ⋯ + amnxn ≡ bm (mod qm)

Let q = lcm(q1,...,qm), q = qjrj, bij = aijri and cj = bjrj. Then we have the equivalent system of congruences

b11x1 + ⋯ + b1nxn ≡ c1 (mod q)
.
.
.
bm1x1 + ⋯ + bmnxn ≡ cm (mod q)

We use the Smith normal form of the integer matrix B.

P and Q are unimodular matrices such that PBQ = diag(d1,...,dr,0,...,0), where r = rank(B) and d1,...,dr are positive integers such that di divides di+1 for 1 ≤ i ≤ r-1.
Write X = QY and K = PC. Then we have the equivalent system of congrences

d1y1 ≡ k1 (mod q)
.
.
.
dryr ≡ kr (mod q)
0 ≡ kr+1 (mod q)
.
.
.
0 ≡ kn (mod q).

Assuming that the first r congruences are soluble (mod q) with solution arrays Y1,...,Yr of lengths e1,...,er, where ei = gcd(di,q) and that the last n-r congruences are satisfied, we get the complete solution of the form
X = QY, where Y = [y1,...,yr,yr+1,...,yn]t, where yi = Yi,zi and yr+1,...,yn are arbitrary (mod q).

If r = n, we get e1···er solutions (mod q), otherwise we get e1···erqn-r solutions (mod q).

The augmented matrix [A|B] can be entered either
(i) as a string of m(n+1) integers separated by spaces, or
(ii) cut and pasted from a text file, with entries separated by spaces and each row ended by a newline.

The author is grateful to Alan Offer for programming assistance with the recursive construction of the cartesian product.

    Enter m, the number of equations (≤ 50):
    Enter n, the number of unknowns (≤ 50):
    Enter the m moduli (separated by spaces):
    Enter the m × (n+1) augmented matrix :

Last modified 13th June 2015
Return to main page