# 有理重构

**有理重构**是计算机代数中的一个算法问题：给定整数 `a` 作为模整数 `m` 的剩余，恢复产生它的有理数 n/d，其中 n/d 模 m 的约化等于 a，且分子与分母满足大小界。它是取模 m 剩余的部分逆运算——该映射只对分母与 m 互素的有理数有定义——它使模算法能够在小素数域上计算，然后把结果提升回有理数域 ℚ。<sup>[1](https://magma.maths.usyd.edu.au/magma/handbook/text/205)</sup>

| 关键事实 | 细节 |
|---|---|
| 问题陈述 | 给定 a 与模数 m，求 n、d，使得 a·d ≡ n (mod m)，gcd(n, d) = 1，且 2\|n\|·\|d\| < m<sup>[2](https://charleshzh.github.io/oCAS/latest/en/math/number-theory-algorithms.html)</sup> |
| 唯一性 | 当 2\|n\|·\|d\| < m 时，满足条件的对 (n, d) 是唯一的<sup>[2](https://charleshzh.github.io/oCAS/latest/en/math/number-theory-algorithms.html)</sup> |
| 标准算法 | 王/扩展欧几里得算法，追踪序列 (rᵢ, tᵢ)；需要 O(log m) 步 GCD<sup>[2](https://charleshzh.github.io/oCAS/latest/en/math/number-theory-algorithms.html)</sup> |
| 起源 | 解法最早由保罗·S·王于 1981 年给出，最初是在部分分式分解的背景下<sup>[3](https://www.sigsam.org/issac/2004/abstracts/2.html)</sup><sup> • </sup><sup>[4](https://citeseerx.ist.psu.edu/document?doi=6eab8ef552196480b154fde506d5a6c6c663d2e3&repid=rep1&type=pdf)</sup> |
| 规模保证 | 当 m > 2M² 时，王的算法输出 n/d，其中 M = max(\|n\|, d)<sup>[3](https://www.sigsam.org/issac/2004/abstracts/2.html)</sup> |
| 近最优变体 | Monagan 的最大商算法（ISSAC 2004）在 m 仅比 2\|n\|d 多几个比特时，以可控的高概率成功<sup>[3](https://www.sigsam.org/issac/2004/abstracts/2.html)</sup> |
| 软件 | SageMath、Maple（`iratrecon`）和 Magma 都提供有理重构例程<sup>[5](https://sagemath.gitlab.io/documentation/pdf/en/reference/rings_standard/rings_standard.pdf)</sup><sup> • </sup><sup>[6](https://maplesoft.com/support/help/maple/view.aspx?path=iratrecon)</sup><sup> • </sup><sup>[1](https://magma.maths.usyd.edu.au/magma/handbook/text/205)</sup> |

## 问题

模算术用单个剩余表示有理数 n/d：计算 d⁻¹ mod m，取 a = n·d⁻¹ mod m，这要求 gcd(d, m) = 1。许多剩余对应同一个有理数，因此没有额外条件时逆映射不是良定义的。有理重构加入了大小界：它寻找唯一的既约 n/d，使 \|n\| 与 d 都不超过 √(m/2)，或者更一般地满足 2\|n\|·\|d\| < m。<sup>[2](https://charleshzh.github.io/oCAS/latest/en/math/number-theory-algorithms.html)</sup><sup> • </sup><sup>[1](https://magma.maths.usyd.edu.au/magma/handbook/text/205)</sup>

大小条件起两个作用。它使答案唯一，并保证重构结果是与该剩余一致的最“简单”有理表示。<sup>[2](https://charleshzh.github.io/oCAS/latest/en/math/number-theory-algorithms.html)</sup> 一个剩余未必有任何重构：SageMath 的 `rational_reconstruction(400, 1000)` 会报错，指出 400 模 1000 不存在重构。<sup>[5](https://sagemath.gitlab.io/documentation/pdf/en/reference/rings_standard/rings_standard.pdf)</sup>

一个算例展示了其机制。要表示 3/7 模 101，计算 7⁻¹ mod 101 = 29，于是像为 a = 3 · 29 mod 101 = 87；对 87 模 101 做有理重构返回 3/7。<sup>[7](https://charleshzh.github.io/oCAS/latest/en/algorithms/rational-functions.html)</sup> 类似地，SageMath 把 11323 模 100000 重构为 119/53，把 204977 模 292393 重构为 45/97。<sup>[5](https://sagemath.gitlab.io/documentation/pdf/en/reference/rings_standard/rings_standard.pdf)</sup>

## 扩展欧几里得算法

经典解法对 (m, a) 运行扩展欧几里得算法，同时追踪一个额外的系数序列。把算法状态写成对 (rᵢ, tᵢ)，初始化 (r₀, r₁) = (m, a) 和 (t₀, t₁) = (0, 1)，然后反复令 q = ⌊r₀/r₁⌋，并按 (r₀, r₁) ← (r₁, r₀ − q·r₁) 与 (t₀, t₁) ← (t₁, t₀ − q·t₁) 更新两个对。每一步都保持不变量 m·sᵢ + a·tᵢ = rᵢ，因此令 n = rᵢ、d = tᵢ 就得到 n = a·d + m·sᵢ，即 n ≡ a·d (mod m)。<sup>[18](https://fredrikj.net/fdoc/html/algorithms/number-theory/rational-reconstruction.html)</sup> 一旦 \|r₁\| ≤ √(m/2) 且 \|t₁\| ≤ √(m/2)，迭代就停止；随后用同余式与大小条件验证候选 (n, d)，若验证失败或 t₁ = 0，算法报告失败。<sup>[2](https://charleshzh.github.io/oCAS/latest/en/math/number-theory-algorithms.html)</sup><sup> • </sup><sup>[8](https://cocalc.com/share/download/5d54f9d642cd3ef1affd88397ab0db616c17e5e0/www/msri06/stein/main.pdf)</sup>

步数为 O(log m)，与欧几里得算法本身同阶。<sup>[2](https://charleshzh.github.io/oCAS/latest/en/math/number-theory-algorithms.html)</sup> 斯坦福大学荣休教授、《计算机程序设计艺术》的作者唐纳德·克努特（Donald Knuth）将该算法描述为第 2 卷第 379 页习题 51 的解答，并将其归功于王、Kornerup 和 Gregory，时间约在 1983 年。<sup>[8](https://cocalc.com/share/download/5d54f9d642cd3ef1affd88397ab0db616c17e5e0/www/msri06/stein/main.pdf)</sup>

## 历史

肯特州立大学的计算机科学家保罗·S·王（Paul S. Wang）于 1981 年引入了一种 p 进算法来构造部分分式分解；与通常用于因式分解或最大公因数的 p 进算法不同，它用 p 进像来重构有理数而非整数。<sup>[4](https://citeseerx.ist.psu.edu/document?doi=6eab8ef552196480b154fde506d5a6c6c663d2e3&repid=rep1&type=pdf)</sup><sup> • </sup><sup>[9](https://doi.org/10.1145%2F1089292.1089293)</sup> 王、剑桥大学的 M. J. T. Guy 与剑桥大学伊曼纽尔学院的 J. H. Davenport 于 1982 年在《ACM SIGSAM 公报》上发表了后续论文《有理数的 p 进重构》。<sup>[9](https://doi.org/10.1145%2F1089292.1089293)</sup>

后来的工作针对多精度整数上底层扩展欧几里得计算的开销。George E. Collins 与 Mark Encarnación 于 1995 年在《符号计算杂志》上发表了一种基于 Lehmer 多精度扩展欧几里得算法双位版本的算法；渐近复杂度仍是输入长度的二次，但实验显示新算法对小输入比常用算法快 3 倍以上，对 100 个字长的输入快 7 倍以上。<sup>[10](https://www.microsoft.com/en-us/research/publication/efficient-rational-number-reconstruction/)</sup> Xinmao Wang 与 Victor Y. Pan 在《SIAM 计算杂志》（2003）上把整数扩展欧几里得算法某一选定项的计算——进而把模与数值有理数重构问题——从二次时间加速到近线性时间，与已知的整数 gcd 复杂度界相匹配。<sup>[11](https://epubs.siam.org/doi/abs/10.1137/S0097539702408636)</sup>

## 变体

**最大商重构。** 王的算法要求 m > 2M²，其中 M = max(\|n\|, d)，这可能迫使模算法使用比信息论下界多得多的素数。Michael Monagan 的最大商有理重构在 ISSAC 2004 上提出，在以下意义下几乎最优：以可控的高概率，当 m 仅比 2\|n\|d 多几个比特时它输出 n/d，而当 m < 2\|n\|d 时它以高概率失败。在 m 为素数乘积的模算法中，这意味着只需比最小值多约一个素数；当 \|n\| ≪ d 或 d ≪ \|n\| 时，新算法最多可节省一半的素数。该算法返回欧几里得算法中最大商所对应的有理数，前提是该商超过某个界 Q。<sup>[3](https://www.sigsam.org/issac/2004/abstracts/2.html)</sup><sup> • </sup><sup>[6](https://maplesoft.com/support/help/maple/view.aspx?path=iratrecon)</sup>

**容错。** 经典方法假设每个模像都是正确的。在 ℚ 上计算 Gröbner 基等应用中，有些素数是“坏”的，会给出错误的像，而且可能没有高效的方法预先识别好素数。Janko Boehm、Wolfram Decker、Claus Fieker 与 Gerhard Pfister 在 2015 年发表于《计算数学》的工作中，引入了一种通过高斯约化的容错有理重构算法，作为处理坏素数的模框架的一部分。<sup>[12](https://arxiv.org/abs/1207.1651)</sup> John Abbott 于 2017 年在《符号计算杂志》上提出了两种高效的容错方法，用于从若干剩余-模数对（其中一些可能有误）重构有理数；一种推广了王、Guy 与 Davenport 的方法，另一种是 Monagan 最大商算法的启发式推广，当待重构的有理数不平衡时更为可取。<sup>[13](https://www.sciencedirect.com/science/article/pii/S0747717116300773)</sup>

**多项式类比。** 同样的问题对单变量多项式也存在：给定模多项式 M(x) 的像，恢复满足次数界的有理函数 n(x)/d(x)。这种有理函数重构经典上用半 gcd 算法以拟线性复杂度求解，Vincent Neiger、Mohab Safey El Din 与 Kevin Tran 在 2026 年的一项分析将其与 Padé 逼近和 Cauchy 插值联系起来，推导出带有显式首项常数的精细复杂度界。<sup>[14](https://arxiv.org/abs/2609.21051)</sup>

## 应用

有理重构是模算法的提升步骤，模算法通过在若干素数下计算并合并结果，避免 ℚ 上的中间系数增长。在模 GCD 算法中，用中国剩余定理把来自若干素数域 𝔽ₚ 的 GCD 系数合并后，模 M 的合并结果必须重构回 ℚ 上的有理数。<sup>[2](https://charleshzh.github.io/oCAS/latest/en/math/number-theory-algorithms.html)</sup> Gröbner 基以及零维理想伴随素理想的模算法遵循同样的三段式模式：在若干素数下求解问题，通过中国剩余或有理重构把结果提升到 ℚ，然后验证。<sup>[15](https://arxiv.org/abs/1005.5663)</sup> 一个建立在计算机代数系统 Singular 上的大规模并行框架使用容错有理重构，以确保只要对固定计算而言坏素数只有有限多个，计算就能终止。<sup>[16](https://arxiv.org/abs/2401.11606)</sup>

ℚ 上的线性代数使用同样的提升思想。SageMath 用于简化行阶梯形的多模算法在相继的素数下计算阶梯形，用中国剩余定理合并，并应用有理重构恢复有理元素，若重构失败则用更多素数重复。<sup>[8](https://cocalc.com/share/download/5d54f9d642cd3ef1affd88397ab0db616c17e5e0/www/msri06/stein/main.pdf)</sup> 该操作在多个层面上提供接口：SageMath 在整数上、在 ℤ/nℤ 与有限域的元素上、以及在视为模 N 的整数矩阵上提供 `rational_reconstruction`，在可能时把整个矩阵提升为有理数矩阵。<sup>[5](https://sagemath.gitlab.io/documentation/pdf/en/reference/rings_standard/rings_standard.pdf)</sup><sup> • </sup><sup>[17](https://doc.sagemath.org/html/en/reference/matrices/sage/matrix/matrix_integer_sparse.html)</sup> Maple 的 `iratrecon` 递归作用于多项式的系数以及列表、向量和矩阵的元素，并提供一个 `scaled` 选项，把所有有理数重构到公分母上，这更快且适合模算法。<sup>[6](https://maplesoft.com/support/help/maple/view.aspx?path=iratrecon)</sup>

## References

1. [Magma Handbook: Rational Reconstruction, Computational Algebra Group, University of Sydney](https://magma.maths.usyd.edu.au/magma/handbook/text/205)
2. [oCAS documentation: Number Theory Algorithms, Rational Reconstruction](https://charleshzh.github.io/oCAS/latest/en/math/number-theory-algorithms.html)
3. [M. Monagan (2004). Maximal Quotient Rational Reconstruction: An Almost Optimal Algorithm for Rational Reconstruction. ISSAC 2004 abstract.](https://www.sigsam.org/issac/2004/abstracts/2.html)
4. [Fault-Tolerant Modular Reconstruction of Rational Numbers (CiteSeerX copy)](https://citeseerx.ist.psu.edu/document?doi=6eab8ef552196480b154fde506d5a6c6c663d2e3&repid=rep1&type=pdf)
5. [Sage 9.4 Reference Manual: Standard Commutative Rings, `rational_reconstruction`](https://sagemath.gitlab.io/documentation/pdf/en/reference/rings_standard/rings_standard.pdf)
6. [Maple Help: iratrecon (rational reconstruction)](https://maplesoft.com/support/help/maple/view.aspx?path=iratrecon)
7. [oCAS documentation: Rational Functions & Resultants](https://charleshzh.github.io/oCAS/latest/en/algorithms/rational-functions.html)
8. [William Stein, A Brief Tour of (Sage) Linear Algebra, Section 7.2 Rational Reconstruction (MSRI 2006 notes)](https://cocalc.com/share/download/5d54f9d642cd3ef1affd88397ab0db616c17e5e0/www/msri06/stein/main.pdf)
9. [Paul S. Wang, M. J. T. Guy, J. H. Davenport (1982). P-adic reconstruction of rational numbers. ACM SIGSAM Bulletin 16(2), 2–3.](https://doi.org/10.1145%2F1089292.1089293)
10. [George E. Collins, Mark Encarnación (1995). Efficient Rational Number Reconstruction. Journal of Symbolic Computation 20(3), 287–297. Microsoft Research page.](https://www.microsoft.com/en-us/research/publication/efficient-rational-number-reconstruction/)
11. [Xinmao Wang, Victor Y. Pan (2003). Acceleration of Euclidean Algorithm and Rational Number Reconstruction. SIAM J. Comput. 32(2), 548–556.](https://epubs.siam.org/doi/abs/10.1137/S0097539702408636)
12. [Janko Boehm, Wolfram Decker, Claus Fieker, Gerhard Pfister. The use of Bad Primes in Rational Reconstruction. arXiv:1207.1651; Math. Comp. 84 (2015), 3013–3027.](https://arxiv.org/abs/1207.1651)
13. [John Abbott (2017). Fault-tolerant modular reconstruction of rational numbers. Journal of Symbolic Computation 80, 707–718.](https://www.sciencedirect.com/science/article/pii/S0747717116300773)
14. [Vincent Neiger, Mohab Safey El Din, Kevin Tran (2026). Refined complexity bounds for rational reconstruction and XGCD through Padé approximants and Cauchy interpolants. arXiv:2609.21051.](https://arxiv.org/abs/2609.21051)
15. [Nazeran Idrees, Gerhard Pfister, Stefan Steidel (2011). Parallelization of Modular Algorithms. arXiv:1005.5663; J. Symbolic Computation 46, 672–684.](https://arxiv.org/abs/1005.5663)
16. [Dirk Basson, Janko Boehm, Magdaleen S. Marais, Mirko Rahn, Hobihasina P. Rakotoarisoa (2024). Massively Parallel Modular Methods in Commutative Algebra and Algebraic Geometry. arXiv:2401.11606.](https://arxiv.org/abs/2401.11606)
17. [SageMath documentation: Sparse integer matrices, `rational_reconstruction(N)`](https://doc.sagemath.org/html/en/reference/matrices/sage/matrix/matrix_integer_sparse.html)
18. [Rational reconstruction, FLINT](https://fredrikj.net/fdoc/html/algorithms/number-theory/rational-reconstruction.html)

---
*Initially written Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Computational and symbolic algebra › Symbolic and algebraic algorithms › Exact and arbitrary-precision arithmetic

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/rational-reconstruction

English original: https://www.edgepedia.org/rational-reconstruction
