Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Rational reconstruction
This file is a Cython implementation of rational reconstruction, using direct MPIR calls.
AUTHORS:
- ??? (2006 or before)
- Jeroen Demeyer (2014-10-20): move this function from ``gmp.pxi``, simplify and fix some bugs, see :trac:`17180` """ #***************************************************************************** # Copyright (C) 2006 ??? # Copyright (C) 2014 Jeroen Demeyer <jdemeyer@cage.ugent.be> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # http://www.gnu.org/licenses/ #*****************************************************************************
from cysignals.signals cimport sig_on, sig_off
from sage.libs.gmp.mpz cimport * from sage.libs.gmp.mpq cimport *
cdef int mpq_rational_reconstruction(mpq_t answer, mpz_t a, mpz_t m) except -1: """ Set ``answer`` to a rational number which is `a` modulo `m` and such that the numerator and denominator of the result is bounded by sqrt(m/2).
If `m` is zero, raise ``ZeroDivisionError``. If the rational reconstruction does not exist, raise ``ValueError``.
We assume that ``mpq_init`` has been called on ``answer``.
For examples, see the ``rational_reconstruction`` method of :class:`Integer`.
TESTS::
sage: q = -113/355 sage: for p in range(2*355^2, 3*355^2): # long time ....: if is_prime(p): ....: assert Mod(q, p).rational_reconstruction() == q """ cdef mpz_t bound cdef mpz_t u1 cdef mpz_t u2 cdef mpz_t v1 cdef mpz_t v2 cdef mpz_t q
# Initialization: u1 = 0; v1 = 1
# The answer is v2/v1, but check the conditions first # Set answer to v2/v1 with correct sign else:
# Earlier versions used to put a string representation of # the input here. We don't do this, since some low-level code # needs to catch and handle this exception so the string # handling is just a waste of time. The actual user-facing # methods could catch ValueError and raise a better exception # instead. finally: |