Rational reconstruction
- Cost
- bit operations with the Euclidean algorithm, with the half-GCD algorithm.
Multimodular and -adic algorithms compute in , where a rational number with is represented by the residue . When the answer of a computation over — the solution of a linear system, the coefficients of a polynomial — has been found modulo a large enough , rational reconstruction recovers it from . The method goes back to Wang [WGD1982]; see also [CollinsEncarnacion1995], [GG2003, §5.10] and [Sho2009, §4.6].
Throughout, , , and are bounds with .
Uniqueness#
Lemma 1. There is at most one fraction with , and , up to the representation of the fraction.
Proof. If and both satisfy the conditions, then , and , so .
The condition cannot be weakened much: the number of fractions with and in lowest terms is about , so their residues cannot all be distinct if is much larger than . With the balanced choice , about of all residues are reconstructible.
The extended Euclidean algorithm#
Apply the extended Euclidean algorithm to and : with the quotients ,
starting from and , until . Then , so for every . We use the following standard facts [GG2003, §3.2, §4.6].
- The alternate in sign and (with the only possible equality).
- , so .
- for .
- The convergents of the continued fraction of are the fractions , .
Fact 3 follows from and the alternating signs of the .
Input: , , with .
Output: the fraction in lowest terms with , and , or failure if none exists.
- If , return . If , return .
- Run the extended Euclidean algorithm on , computing only the and , up to the first index with .
- If and , return . Otherwise fail.
Theorem 2. Algorithm 1 is correct.
Proof. A returned fraction satisfies the conditions: in step 1 trivially, and in step 3 because , , and . By Lemma 1 it is the only one.
Conversely, suppose that in lowest terms satisfies the conditions and that step 1 does not apply, so and . Write . Then divides and , so , and
By Legendre's theorem [HarWri2008, Theorem 184], is a convergent of , so by fact 4 and fact 2, for some , and then . Since , we have . If , then by facts 3 and 1,
a contradiction. Hence , , and step 3 returns .
The remainders shrink and the cofactors grow as the algorithm proceeds, with ; the bound decides where the algorithm stops, trading the size of the numerator against the size of the denominator. Monagan's maximal quotient variant [Mon2004] does not fix in advance but stops after the largest quotient , which succeeds with a modulus only slightly larger than with high probability; FLINT implements the bounded version, which is a proof when the bounds are known.
Complexity#
The Euclidean algorithm on -bit numbers costs bit operations. Lehmer's method [Leh1938] simulates several steps with word-size leading parts of and , so that most of the work is done with single-word matrices. The half-GCD algorithm [Sch1971], [ThullYap1990] computes the quotient sequence up to a given size recursively from the leading halves and costs . The stopping rule is a partial GCD, which the half-GCD algorithm handles naturally: it reduces to a pair straddling a given size, then continues with Lehmer steps.
Several entries with a common denominator#
When a vector or matrix of rationals is reconstructed, the entries often share most of their denominator. Multiplying each residue by the denominator of the entries found so far, the next entry is usually an integer in the symmetric range , recognised without any GCD; only otherwise is the entry reconstructed, with the denominator bound , and multiplied by the new denominator.
Use in FLINT#
_fmpq_reconstruct_fmpz_2 implements Algorithm 1. After the quick integer tests of step 1, it chooses by the size of :
- for of one or two limbs, a Euclidean algorithm in one or two words, with a binary GCD for the final coprimality test;
- up to 12 limbs, Lehmer's method on stack arrays of limbs;
- for larger , Lehmer's method on
fmpz, switching to the half-GCD algorithm_fmpq_hgcdof the continued fraction code (Continued fractions of rational numbers) when has at leastFMPQ_RECONSTRUCT_HGCD_CUTOFF(500) more limbs than , and to plain Euclidean steps when they differ by fewer than 3 limbs.
_fmpq_reconstruct_fmpz_2_naive is the plain algorithm on fmpz, used to check the fast version in tests. fmpq_reconstruct_fmpz uses the balanced bounds .
The reconstruction returns rational results in fmpq_mat_set_fmpz_mat_mod_fmpz, fmpz_mat_det_divisor and the Dixon solver fmpz_mat_solve_dixon_den (through _fmpz_mat_reconstruct_matwise), which all use the common denominator technique above, and in fmpq_poly_interpolate_multi_mod and fmpz_poly_roots_fmpq. How the multimodular and -adic linear algebra algorithms choose the bounds and decide when to stop is described in multimodular and -adic linear algebra.
int fmpq_reconstruct_fmpz(fmpq_t res, const fmpz_t a, const fmpz_t m)int fmpq_reconstruct_fmpz_2(fmpq_t res, const fmpz_t a, const fmpz_t m, const fmpz_t N, const fmpz_t D)int fmpq_mod_fmpz(fmpz_t res, const fmpq_t x, const fmpz_t mod)Referenced from#
- Integer GCD (algorithm)
- Continued fractions of rational numbers (algorithm)
- fmpq.h (module)
- Rational reconstruction
fmpq_mat_set_fmpz_mat_mod_fmpz_fmpq_poly_interpolate_barycentric,fmpq_poly_interpolate_barycentric,_fmpq_poly_interpolate_multi_mod,fmpq_poly_interpolate_multi_mod,_fmpq_poly_interpolate_fmpq_vec,fmpq_poly_interpolate_fmpz_vec,fmpq_poly_interpolate_fmpz_fmpq_vec,fmpq_poly_interpolate_fmpq_vecfmpz_mat_det_divisorfmpz_mat_solve_dixon
References#
- [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
- [CollinsEncarnacion1995] G. E. Collins and M. J. Encarnación, "Efficient rational number reconstruction", Journal of Symbolic Computation 20:3 (1995), 287–297. https://doi.org/10.1006/jsco.1995.1051
- [GG2003] J. von zur Gathen and J. Gerhard, Modern Computer Algebra, second edition, Cambridge University Press (2003)
- [Sho2009] V. Shoup. A Computational Introduction to Number Theory and Algebra. Second edition, Cambridge University Press, 2009. https://shoup.net/ntb/
- [HarWri2008] G. H. Hardy and E. M. Wright, An Introduction to the Theory of Numbers, sixth edition, revised by D. R. Heath-Brown and J. H. Silverman, Oxford University Press, 2008.
- [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
- [Leh1938] D. H. Lehmer, "Euclid's algorithm for large numbers", American Mathematical Monthly 45:4 (1938), 227–233.
- [Sch1971] A. Schönhage, "Schnelle Berechnung von Kettenbruchentwicklungen", Acta Informatica 1 (1971), 139–144
- [ThullYap1990] Thull, K. and Yap, C. : A Unified Approach to HGCD Algorithms for Polynomials and Integers, (1990)
The FLINT team, “Rational reconstruction,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/number-theory/rational-reconstruction.html (generated 2026-11-02).
@misc{flint-doc-algorithms-number-theory-rational-reconstruction,
author = {{The FLINT team}},
title = {{Rational reconstruction}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/algorithms/number-theory/rational-reconstruction.html},
note = {Generated 2026-11-02}
}