galois.crt(remainders: Sequence[int], moduli: Sequence[int]) int
galois.crt(remainders: Sequence[Poly], moduli: Sequence[Poly]) Poly

Solves the simultaneous system of congruences for \(x\).

Parameters:
remainders: Sequence[int]
remainders: Sequence[Poly]

The integer or polynomial remainders \(a_i\).

moduli: Sequence[int]
moduli: Sequence[Poly]

The integer or polynomial moduli \(m_i\).

Returns:

The simultaneous solution \(x\) to the system of congruences.

Notes

This function implements the Chinese Remainder Theorem.

\[\begin{split} x &\equiv a_1\ (\textrm{mod}\ m_1) \\ x &\equiv a_2\ (\textrm{mod}\ m_2) \\ x &\equiv \ldots \\ x &\equiv a_n\ (\textrm{mod}\ m_n) \end{split}\]

References

Examples

Define a system of integer congruences.

In [1]: a = [0, 3, 4]

In [2]: m = [3, 4, 5]

Solve the system of congruences.

In [3]: x = galois.crt(a, m); x
Out[3]: 39

Show that the solution satisfies each congruence.

In [4]: for i in range(len(a)):
   ...:     ai = x % m[i]
   ...:     print(ai, ai == a[i])
   ...: 
0 True
3 True
4 True

Define a system of polynomial congruences over \(\mathrm{GF}(7)\).

In [5]: GF = galois.GF(7)

In [6]: x_truth = galois.Poly.Random(6, field=GF); x_truth
Out[6]: Poly(x^6 + 2x^5 + 4x^4 + x^3 + x + 1, GF(7))

In [7]: m3 = galois.Poly.Random(3, field=GF)

In [8]: m4 = galois.Poly.Random(4, field=GF)

In [9]: m5 = galois.Poly.Random(5, field=GF)

In [10]: m = [m3, m4, m5]; m
Out[10]: 
[Poly(6x^3 + 6x^2 + 5x + 5, GF(7)),
 Poly(6x^4 + 2x^3 + 6x + 3, GF(7)),
 Poly(6x^5 + 6x^4 + x^2 + 3x + 5, GF(7))]

In [11]: a = [x_truth % m3, x_truth % m4, x_truth % m5]; a
Out[11]: 
[Poly(2, GF(7)),
 Poly(3x^3 + 6x^2 + x + 2, GF(7)),
 Poly(3x^4 + 2x^3 + 4x^2 + 2x + 6, GF(7))]

Solve the system of congruences.

In [12]: x = galois.crt(a, m); x
Out[12]: Poly(x^6 + 2x^5 + 4x^4 + x^3 + x + 1, GF(7))

Show that the solution satisfies each congruence.

In [13]: for i in range(len(a)):
   ....:     ai = x % m[i]
   ....:     print(ai, ai == a[i])
   ....: 
2 True
3x^3 + 6x^2 + x + 2 True
3x^4 + 2x^3 + 4x^2 + 2x + 6 True