Multimodular and p-adic linear algebra over the integers
- Cost
- For an matrix with -bit entries and -bit words, word operations for the determinant and for multimodular solving, and for -adic solving with one right-hand side; the output-sensitive solvers replace by the actual size of the solution.
Exact linear algebra over suffers from the growth of intermediate results: Gaussian elimination over creates fractions whose sizes grow quickly, and even fraction-free elimination (Fraction-free elimination) works with entries as large as the final result in each of its operations. Modular methods avoid this. They solve the problem modulo word-size primes, where nmod_mat runs at the speed of floating-point arithmetic (Multiplying matrices over Z/nZ), and recover the integer or rational result from its residues. FLINT does this for determinants, linear systems, ranks and echelon forms, characteristic polynomials and products, and makes every answer rigorous.
The methods are specific to and . There is no generic multimodular framework in gr_mat; instead, the generic functions dispatch to these ones for the ring of integers: gr_mat_mul, gr_mat_det, gr_mat_charpoly and gr_mat_permanent call the corresponding fmpz_mat functions.
Throughout, is an (or ) integer matrix with entries bounded by in absolute value, is the maximum norm of a matrix, the Euclidean norm of a row or column, and is the word size.
Bounds#
Theorem 1 (Hadamard's inequality). For a square matrix with rows and columns ,
Proof. The Gram–Schmidt orthogonalisation of the rows satisfies and [Had1893]. Apply this to and to .
With entries below , this gives , a number of about bits. fmpz_mat_det_bound evaluates the bound with upward-rounded mag_t arithmetic. fmpz_mat_det_bound_submatrix omits zero rows and columns from the products, which bounds every minor of a (possibly rectangular) matrix: the rows of a submatrix are no longer than the corresponding rows of , and the same holds for columns.
Lemma 2 (Solution bound). Let be nonsingular with columns and let have columns . Every entry of is at most
in absolute value.
Proof. By Cramer's rule entry is , where is with column replaced by . By Theorem 1 applied to columns it is at most , since the nonzero integer column has .
fmpz_mat_solve_bound returns and the bound of Theorem 1 for . The row product cannot replace the column product in : it bounds but not the cofactors along a column.
Chinese remaindering#
If satisfies and are pairwise coprime with , then is the unique integer in congruent to its residues. For matrices, fmpz_mat_multi_mod_ui reduces all entries modulo primes and fmpz_mat_multi_CRT_ui reconstructs them, both through the subproduct tree of fmpz_comb_t: reducing a -bit integer modulo primes of bits by a tree of remainders costs word operations instead of the of reducing modulo each prime separately, and the same holds for reconstruction [GG2003, §10.3]. Both functions distribute the entries over threads. FLINT uses primes of 59 to 63 bits, the size for which nmod_mat has its fastest kernels (NMOD_MAT_OPTIMAL_MODULUS_BITS = 59).
Definition 3. A prime is bad for a computation if the reduction modulo of a quantity the algorithm relies on behaves differently from the integer quantity: for example if divides , so that is singular modulo , or if the rank of modulo is less than its rank over .
The rank of modulo is at most its rank over , with equality unless divides all minors of , in particular some fixed nonzero minor . Since is at most the bound of fmpz_mat_det_bound_submatrix, the product of the distinct bad primes is at most , so a set of distinct primes whose product exceeds contains a good prime. FLINT uses this both ways: random large primes are good with overwhelming probability, and when all primes of such a set show a rank drop, the rank drop is real.
Determinants#
Input: , ; a positive integer dividing .
Output: .
- , , .
While : take the next prime not dividing and compute an LU decomposition of modulo .
- If the rank modulo is less than and can be certified by Proposition 5, return 0.
- , the sign being that of the row permutation.
- the integer in congruent to modulo and to modulo ; .
- Return .
This is fmpz_mat_det_modular_given_divisor; fmpz_mat_det_modular uses . Since at the end, the result is correct. The number of primes is about , and each costs a reduction of and an LU decomposition modulo , so the total is word operations, against with classical integer arithmetic for fraction-free elimination.
Early termination#
With proved = 0, the loop stops as soon as the reconstructed value has not changed while primes with more than 100 bits in total were added. If is much smaller than the Hadamard bound, which is typical for structured input, this saves most of the primes. A value that stays the same while further primes are added is wrong only if all primes divide the difference between the true and the reconstructed value. For random primes this is very unlikely [Kal2002], but it is not a proof, and fmpz_mat_det does not use it.
A large divisor#
The number of primes depends on , so a large known divisor saves work.
Lemma 4. Let be nonsingular with Smith normal form . Then is integral. Hence for every integer vector the least common denominator of divides , which divides .
Proof. With for unimodular and , , and is integral since for all .
fmpz_mat_det_divisor solves for by -adic lifting (p-adic lifting) and returns the least common multiple of the denominators of , a divisor of . For random matrices, is up to a small factor and with high probability [AbbottBronsteinMulders1999; Pan1988; EGV2000], so that needs only one or two primes and the -adic solve, which costs about as much as primes, dominates. fmpz_mat_det_modular_accelerated uses this, and fmpz_mat_det calls it for when is at least the bit size of the entries. For matrices whose determinant is spread over many invariant factors, is small and nothing is gained. Asymptotically faster determinant algorithms exist [EGV2000; KV2004; Sto2005] but are not implemented.
Rank and singularity#
Full rank modulo a prime proves full rank over . A rank deficiency modulo is either real or caused by a bad prime, and is certified as follows.
Proposition 5 (Kernel vector certificate). Let be , let be a prime and let be the rank of modulo , with pivot rows and pivot columns , so that the submatrix is nonsingular modulo . Let be a non-pivot column and let solve . Define by , and otherwise. If , then has rank less than over . If the rank of over is , then .
Proof. is nonsingular modulo , hence over , so exists, and since ; this proves the first claim. If has rank , its rows are combinations of the rows , which annihilate by construction.
_fmpz_mat_certify_singular_lu_mod_p computes exactly (by Cramer's rule, fraction-free elimination or -adic solving with the same prime) and checks the other rows. fmpz_mat_is_singular (with primes of 17 bits), the determinant and the nonsingular solvers use it. If the certificate fails, another prime is tried, and once the product of the primes with a rank drop exceeds the Hadamard bound, is singular by the argument of Chinese remaindering.
For the rank itself and the reduced row echelon form, the analogous certificate checks that all non-pivot rows lie in the span of the pivot rows.
Proposition 6 (Echelon form certificate). In the situation of Proposition 5, let be the matrix , which has an identity matrix in the pivot columns, and let be the matrix whose columns are the nullspace vectors defined by , one per non-pivot column. If is in reduced row echelon form and , where consists of the non-pivot rows of , then has rank and its reduced row echelon form is followed by zero rows.
Proof. The rows of span the same space as the pivot rows of , and the columns of span its orthogonal complement. puts the rows of in the orthogonal complement of that complement, which is . So the row space of is , of dimension , and is its reduced echelon basis.
fmpz_mat_rref_mul and fmpz_mat_rank_certify_lu_mod_p implement this with primes of 17 bits, following [Stein2007]; is computed with fmpz_mat_solve, so that is obtained as an integer matrix and a denominator.
Solving linear systems#
p-adic lifting#
Input: nonsingular , ; , ; a prime not dividing ; a number of steps .
Output: .
- Compute , or an LU decomposition of modulo ; ; .
For :
- , with entries in ;
- ;
- , an exact division.
- Return .
Lemma 7. After step , ; in particular . The entries of stay bounded by .
Proof. By induction on : , so the division is exact, and . For the bound, .
The rational solution has entries with by Lemma 2, so after steps with it is determined by rational reconstruction (Rational reconstruction). Each step costs a product with (or two triangular solves) modulo and a product of with an matrix of word-size entries, about word operations. With steps, the total for is word operations plus one LU decomposition modulo [Dix1982; MC1979]. For one right-hand side this beats the of the multimodular method. Storjohann's high-order lifting reaches bit operations [Sto2003; Sto2005] but is not implemented.
fmpz_mat_solve_dixon is the plain algorithm: it lifts to the worst-case bound and returns modulo without reconstruction. fmpz_mat_solve_dixon_den refines it:
- It starts with a prime near , for which the LU decomposition and the triangular solves are about twice as fast as with a 59-bit prime, at the same cost per lifted bit.
- The products are computed with
fmpz_mat_mul, with the classical algorithm (one multiplication of a multiword integer by a word per entry), or multimodularly from precomputed residues of , depending on , and . - When the solution turns out to be long, the digit is increased from to : the inverse is obtained by Newton iteration, modulo increasing powers of , and the products and are computed multimodularly with tables of residues of and . The digit size is doubled while the lifting still to be done is estimated to cost at least twice as much as the change, which bounds the overhead by a factor 1.5 whatever the size of the solution.
- The digits are accumulated in a binary counter of partial sums, so that forming modulo costs per entry rather than .
- Termination is output-sensitive (Output-sensitive termination).
Chinese remaindering#
fmpz_mat_solve_multi_mod_den solves modulo primes with nmod_mat_solve, in batches processed in parallel, and combines the residues of modulo the product of the primes. The residues are combined only when a reconstruction is attempted, so the total cost of the Chinese remaindering is that of a few full reconstructions. Each prime costs word operations and a reduction of and , so the total is for . When this beats -adic lifting, whose steps cost each, and fmpz_mat_solve uses it then.
Rational reconstruction#
Theorem 8. Let , with , and . There is at most one fraction with , , and . If it exists, it is found by the extended Euclidean algorithm applied to and , stopped at the first remainder at most .
Proof. If and both qualify, then and , so . For the statement about the Euclidean algorithm see [WGD1982] and [GG2003, §5.10].
Reconstructing every entry separately would need for each. FLINT reconstructs a whole matrix with a running common denominator (_fmpz_mat_reconstruct_matwise): each entry is first multiplied by the denominator found so far, and if in the symmetric range is at most , it is accepted as the numerator; otherwise the entry is reconstructed with denominator bound , and the new factor of the denominator is multiplied into and into the entries done so far. Most entries then cost one multiplication and one reduction [CheSto2005].
Output-sensitive termination#
The bounds and are often far larger than the actual solution. The solvers therefore stop as soon as the solution is determined, using a certificate that needs no a priori bound.
Theorem 9 (Size certificate). Let be an integer matrix and a nonzero integer with . If
then , so that is the solution of .
Proof. Each entry of is a sum of products bounded by and one term bounded by , so it is less than in absolute value. It is divisible by , hence zero.
This criterion goes back to Cabay [Cab1971] and is used in [CheSto2005; Stef2010]. To apply it, the solvers must find a candidate early, without knowing the size of the solution:
- Probe. A random combination of the entries of the current solution modulo , with weights , is maintained incrementally at negligible cost. It is the residue of the rational number , whose denominator is the common denominator of the solution with high probability, and whose numerator has about the size of the largest numerator.
- Probe reconstruction (
_fmpz_mat_solve_probe_reconstruct). First and , for the denominator found by the previous probe, are tested for being small in the symmetric range, with a margin of 30 bits. Otherwise the continued fraction expansion of is computed. The true value is the convergent preceding a large partial quotient as soon as , whatever the balance between and , which halves the precision that a fixed split would need [Mon2004]. A partial quotient of at least 30 bits is required, so a spurious result has probability about . - Attempt. When the probe has succeeded, and either the certificate can hold at the current modulus or an explicit check is estimated to cost less than the lifting steps still missing, the whole solution is reconstructed with a denominator bound times the probe denominator (the margin doubles after failures) and checked with Theorem 9 or by multiplication. During the first eight steps attempts are made without a probe, since they are cheap and the probe is not yet informative.
- Fallback. When , reconstruction with the global bounds is unique and cannot fail.
A spurious probe costs time but never produces a wrong result, since every accepted solution satisfies Theorem 9 or has been verified by a multiplication. Finally the gcd of the denominator and the entries is divided out, so that the denominator is the least common denominator.
Consistency of general systems#
For an matrix of rank , the system is solved on the pivot rows and columns found modulo a prime, and the remaining equations are checked exactly. If they hold, the solution is correct. If one fails, either the system is inconsistent or the prime is bad.
Proposition 10 (Inconsistency certificate). If there is a vector with and , then has no solution.
Proof. for any solution .
Conversely, by the Fredholm alternative such a exists whenever the system is inconsistent. fmpz_mat_can_solve_multi_mod_den constructs a candidate from a failing equation : has a denominator in position and minus the solution of the transposed pivot system in the pivot rows, so that vanishes on the pivot columns by construction, and is then checked exactly on the other columns [GieLobSau1998; MulSto2004]. If the check fails, the prime was bad and another one is tried; exact work is redone only when the rank increases. Once the product of the primes exceeds the bound on all minors of , some prime was good (Chinese remaindering) and its verdict, "inconsistent", is final.
Characteristic and minimal polynomials#
fmpz_mat_charpoly_modular uses a fixed number of primes determined by the coefficient bound of fmpz_mat_charpoly_bound, computes the characteristic polynomials modulo the primes in parallel (Characteristic and minimal polynomials), and reconstructs them. There are no bad primes, since the characteristic polynomial commutes with reduction.
The minimal polynomial does not: modulo a bad prime its degree drops. fmpz_mat_minpoly_modular keeps the primes of largest degree and, when the reconstruction stabilises, checks the candidate exactly: for the unit vectors used as Krylov generators modulo the primes. Their Krylov spaces span , hence , so and ; and because the minimal polynomial modulo divides the reduction of . Hence . The number of primes is capped by a bound of Dumas [Dum2007] on the coefficients of .
Matrix multiplication#
The product of integer matrices is determined by its residues modulo numbers whose product exceeds , and there are no bad primes. fmpz_mat_mul_multi_mod and fmpz_mat_mul_blas use this; see Multiplying matrices over the integers.
Use in FLINT#
| Task | Functions |
|---|---|
| Determinant | fmpz_mat_det_modular, fmpz_mat_det_modular_accelerated, fmpz_mat_det_modular_given_divisor, fmpz_mat_det_divisor |
| Singularity, rank | fmpz_mat_is_singular, fmpz_mat_rank, fmpz_mat_rank_certify_lu_mod_p, _fmpz_mat_certify_singular_lu_mod_p |
| Echelon form | fmpz_mat_rref_mul, fmpz_mat_rref_upper_certify_lu_mod_p |
| Nonsingular systems | fmpz_mat_solve_dixon, fmpz_mat_solve_dixon_den, fmpz_mat_solve_multi_mod_den |
| General systems | fmpz_mat_can_solve_dixon_den, fmpz_mat_can_solve_multi_mod_den, fmpz_mat_can_solve |
| Rational matrices | fmpq_mat_solve_dixon, fmpq_mat_solve_multi_mod, fmpq_mat_can_solve and variants |
| Polynomials | fmpz_mat_charpoly_modular, fmpz_mat_minpoly_modular |
| Products | fmpz_mat_mul_multi_mod, fmpz_mat_mul_blas |
| Building blocks | fmpz_mat_multi_mod_ui, fmpz_mat_multi_CRT_ui, fmpz_mat_CRT_ui, fmpq_mat_set_fmpz_mat_mod_fmpz |
Referenced from#
- Linear algebra over exact numbers (algorithm)
- Multimodular reduction and Chinese remaindering (algorithm)
- Characteristic and minimal polynomials (algorithm)
- Fraction-free elimination (algorithm)
- Multiplying matrices over the integers (overview)
- Linear algebra over generic rings and its specialisations (overview)
- Rational reconstruction (algorithm)
- fmpq_mat.h (module)
- Solving rational linear systems
fmpz_mat_det_modular,fmpz_mat_det_modular_accelerated,fmpz_mat_det_modular_given_divisor- Computing determinants
- fmpz_mat.h (module)
_fmpz_mat_solve_reconstruct_attempt- Exact linear algebra over the integers
- Solving linear systems over the rationals
- Determinants of polynomial matrices
- Matrix multiplication over Z/nZ for multiword n
- Combining modules: a multimodular determinant (tutorial)
References#
- [Had1893] J. Hadamard. Résolution d'une question relative aux déterminants. Bulletin des Sciences Mathématiques 17 (1893), 240–246.
- [GG2003] J. von zur Gathen and J. Gerhard, Modern Computer Algebra, second edition, Cambridge University Press (2003)
- [Kal2002] E. Kaltofen, "An output-sensitive variant of the baby steps/giant steps determinant algorithm", Proceedings of the 2002 International Symposium on Symbolic and Algebraic Computation (ISSAC 2002), 138–144. https://doi.org/10.1145/780506.780524
- [AbbottBronsteinMulders1999] Fast deterministic computation of determinants of dense matrices, ACM International Symposium on Symbolic and Algebraic Computation (1999)
- [Pan1988] V. Pan, "Computing the determinant and the characteristic polynomial of a matrix via solving linear systems of equations", Information Processing Letters 28:2 (1988), 71–75. https://doi.org/10.1016/0020-0190(88)90166-4
- [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
- [KV2004] E. Kaltofen and G. Villard, "On the complexity of computing determinants", Computational Complexity 13 (2004), 91–130. https://doi.org/10.1007/s00037-004-0185-3
- [Sto2005] A. Storjohann, "The shifted number system for fast linear algebra on integer matrices", Journal of Complexity 21:4 (2005), 609–650. https://doi.org/10.1016/j.jco.2005.04.002
- [Stein2007] Stein, William A.: Modular forms, a computational approach. American Mathematical Society. 2007
- [Dix1982] J. D. Dixon, "Exact solution of linear equations using p-adic expansions", Numerische Mathematik 40 (1982) 137-141
- [MC1979] R. T. Moenck and J. H. Carter, "Approximate algorithms to derive exact solutions to systems of linear equations", Symbolic and Algebraic Computation (EUROSAM 1979), Lecture Notes in Computer Science 72 (1979), 65–73. https://doi.org/10.1007/3-540-09519-5_60
- [Sto2003] A. Storjohann, "High-order lifting and integrality certification", Journal of Symbolic Computation 36:3–4 (2003), 613–648. https://doi.org/10.1016/S0747-7171(03)00097-X
- [WGD1982] P. S. Wang, M. J. T. Guy and J. H. Davenport, "P-adic reconstruction of rational numbers", ACM SIGSAM Bulletin 16:2 (1982), 2–3. https://doi.org/10.1145/1089292.1089293
- [CheSto2005] Z. Chen and A. Storjohann, "A BLAS based C library for exact linear algebra on integer matrices", Proceedings of the 2005 International Symposium on Symbolic and Algebraic Computation (ISSAC) (2005) 92-99
- [Cab1971] S. Cabay, "Exact solution of linear equations", Proceedings of the Second ACM Symposium on Symbolic and Algebraic Manipulation (1971) 392-398
- [Stef2010] D. E. Steffy, "Exact solutions to linear systems of equations using output sensitive lifting", ACM Communications in Computer Algebra 44:3/4 (2010) 160-182
- [Mon2004] M. Monagan, "Maximal quotient rational reconstruction: an almost optimal algorithm for rational reconstruction", Proceedings of the 2004 International Symposium on Symbolic and Algebraic Computation (ISSAC 2004), 243–249. https://doi.org/10.1145/1005285.1005321
- [GieLobSau1998] M. Giesbrecht, A. Lobo and B. D. Saunders, "Certifying inconsistency of sparse linear systems", Proceedings of the 1998 International Symposium on Symbolic and Algebraic Computation (ISSAC) (1998) 113-119
- [MulSto2004] T. Mulders and A. Storjohann, "Certified dense linear system solving", Journal of Symbolic Computation 37:4 (2004) 485-510
- [Dum2007] J.-G. Dumas, "Bounds on the coefficients of the characteristic and minimal polynomials", Journal of Inequalities in Pure and Applied Mathematics 8:2 (2007), article 31.
The FLINT team, “Multimodular and p-adic linear algebra over the integers,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/linear-algebra/multimodular.html (generated 2026-11-02).
@misc{flint-doc-algorithms-linear-algebra-multimodular,
author = {{The FLINT team}},
title = {{Multimodular and p-adic linear algebra over the integers}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/algorithms/linear-algebra/multimodular.html},
note = {Generated 2026-11-02}
}