-
-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trac #28945: clean _cmp_ in rational.pyx
URL: https://trac.sagemath.org/28945 Reported by: chapoton Ticket author(s): Frédéric Chapoton Reviewer(s): Travis Scrimshaw
- Loading branch information
Showing
1 changed file
with
34 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,23 +40,22 @@ TESTS:: | |
True | ||
""" | ||
|
||
#***************************************************************************** | ||
# **************************************************************************** | ||
# Copyright (C) 2004, 2006 William Stein <[email protected]> | ||
# Copyright (C) 2017 Vincent Delecroix <[email protected]> | ||
# | ||
# Distributed under the terms of the GNU General Public License (GPL) | ||
# 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/ | ||
#***************************************************************************** | ||
# https://www.gnu.org/licenses/ | ||
# **************************************************************************** | ||
|
||
cimport cython | ||
from cpython cimport * | ||
from cpython.object cimport Py_EQ, Py_NE | ||
|
||
from cysignals.signals cimport sig_on, sig_off | ||
|
||
import sys | ||
import operator | ||
import fractions | ||
|
||
|
@@ -822,7 +821,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
l = self.continued_fraction_list() | ||
return ContinuedFraction_periodic(l) | ||
|
||
def __richcmp__(left, right, int op): | ||
cpdef _richcmp_(left, right, int op): | ||
""" | ||
Compare two rational numbers. | ||
|
@@ -859,43 +858,14 @@ cdef class Rational(sage.structure.element.FieldElement): | |
....: assert (one1 >= one2) is True | ||
""" | ||
cdef int c | ||
cdef mpz_t mpz_tmp | ||
|
||
assert isinstance(left, Rational) | ||
|
||
if isinstance(right, Rational): | ||
if op == Py_EQ: | ||
return <bint> mpq_equal((<Rational>left).value, (<Rational>right).value) | ||
elif op == Py_NE: | ||
return not mpq_equal((<Rational>left).value, (<Rational>right).value) | ||
else: | ||
c = mpq_cmp((<Rational>left).value, (<Rational>right).value) | ||
elif isinstance(right, Integer): | ||
c = mpq_cmp_z((<Rational>left).value, (<Integer>right).value) | ||
elif isinstance(right, long): | ||
mpz_init(mpz_tmp) | ||
mpz_set_pylong(mpz_tmp, right) | ||
c = mpq_cmp_z((<Rational>left).value, mpz_tmp) | ||
mpz_clear(mpz_tmp) | ||
elif isinstance(right, int): | ||
c = mpq_cmp_si((<Rational>left).value, PyInt_AS_LONG(right), 1) | ||
else: | ||
return coercion_model.richcmp(left, right, op) | ||
|
||
return rich_to_bool_sgn(op, c) | ||
|
||
cpdef int _cmp_(left, right) except -2: | ||
r""" | ||
TESTS:: | ||
sage: (2/3)._cmp_(3/4) | ||
-1 | ||
sage: (1/2)._cmp_(1/2) | ||
0 | ||
""" | ||
cdef int c | ||
if op == Py_EQ: | ||
return <bint> mpq_equal((<Rational>left).value, | ||
(<Rational>right).value) | ||
elif op == Py_NE: | ||
return not mpq_equal((<Rational>left).value, | ||
(<Rational>right).value) | ||
c = mpq_cmp((<Rational>left).value, (<Rational>right).value) | ||
return (c > 0) - (c < 0) | ||
return rich_to_bool_sgn(op, c) | ||
|
||
def __copy__(self): | ||
""" | ||
|
@@ -1189,7 +1159,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def local_height(self, p, prec=None): | ||
r""" | ||
Returns the local height of this rational number at the prime `p`. | ||
Return the local height of this rational number at the prime `p`. | ||
INPUT: | ||
|
@@ -1227,7 +1197,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def local_height_arch(self, prec=None): | ||
r""" | ||
Returns the Archimedean local height of this rational number at the | ||
Return the Archimedean local height of this rational number at the | ||
infinite place. | ||
INPUT: | ||
|
@@ -1263,7 +1233,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def global_height_non_arch(self, prec=None): | ||
r""" | ||
Returns the total non-archimedean component of the height of this | ||
Return the total non-archimedean component of the height of this | ||
rational number. | ||
INPUT: | ||
|
@@ -1306,7 +1276,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def global_height_arch(self, prec=None): | ||
r""" | ||
Returns the total archimedean component of the height of this rational | ||
Return the total archimedean component of the height of this rational | ||
number. | ||
INPUT: | ||
|
@@ -1339,7 +1309,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def global_height(self, prec=None): | ||
r""" | ||
Returns the absolute logarithmic height of this rational number. | ||
Return the absolute logarithmic height of this rational number. | ||
INPUT: | ||
|
@@ -1506,7 +1476,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def _bnfisnorm(self, K, proof=True, extra_primes=0): | ||
r""" | ||
This gives the output of the PARI function :pari:`bnfisnorm`. | ||
Return the output of the PARI function :pari:`bnfisnorm`. | ||
Tries to tell whether the rational number ``self`` is the norm of some | ||
element `y` in ``K``. Returns a pair `(a, b)` where | ||
|
@@ -1566,7 +1536,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def is_perfect_power(self, expected_value=False): | ||
r""" | ||
Returns ``True`` if ``self`` is a perfect power. | ||
Return ``True`` if ``self`` is a perfect power. | ||
INPUT: | ||
|
@@ -1770,7 +1740,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def val_unit(self, p): | ||
r""" | ||
Returns a pair: the `p`-adic valuation of ``self``, and the `p`-adic | ||
Return a pair: the `p`-adic valuation of ``self``, and the `p`-adic | ||
unit of ``self``, as a :class:`Rational`. | ||
We do not require the `p` be prime, but it must be at least 2. For | ||
|
@@ -1836,7 +1806,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def prime_to_S_part(self, S=[]): | ||
r""" | ||
Returns ``self`` with all powers of all primes in ``S`` removed. | ||
Return ``self`` with all powers of all primes in ``S`` removed. | ||
INPUT: | ||
|
@@ -2086,7 +2056,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def is_nth_power(self, int n): | ||
r""" | ||
Returns ``True`` if self is an `n`-th power, else ``False``. | ||
Return ``True`` if self is an `n`-th power, else ``False``. | ||
INPUT: | ||
|
@@ -2739,7 +2709,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def sign(self): | ||
""" | ||
Returns the sign of this rational number, which is -1, 0, or 1 | ||
Return the sign of this rational number, which is -1, 0, or 1 | ||
depending on whether this number is negative, zero, or positive | ||
respectively. | ||
|
@@ -2825,7 +2795,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def norm(self): | ||
r""" | ||
Returns the norm from `\QQ` to `\QQ` of `x` (which is just `x`). This | ||
Return the norm from `\QQ` to `\QQ` of `x` (which is just `x`). This | ||
was added for compatibility with :class:`NumberFields`. | ||
OUTPUT: | ||
|
@@ -2845,7 +2815,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def relative_norm(self): | ||
""" | ||
Returns the norm from Q to Q of x (which is just x). This was added for compatibility with NumberFields | ||
Return the norm from Q to Q of x (which is just x). This was added for compatibility with NumberFields | ||
EXAMPLES:: | ||
|
@@ -2859,7 +2829,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def absolute_norm(self): | ||
""" | ||
Returns the norm from Q to Q of x (which is just x). This was added for compatibility with NumberFields | ||
Return the norm from Q to Q of x (which is just x). This was added for compatibility with NumberFields | ||
EXAMPLES:: | ||
|
@@ -2873,7 +2843,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def trace(self): | ||
r""" | ||
Returns the trace from `\QQ` to `\QQ` of `x` (which is just `x`). This | ||
Return the trace from `\QQ` to `\QQ` of `x` (which is just `x`). This | ||
was added for compatibility with :class:`NumberFields`. | ||
OUTPUT: | ||
|
@@ -3022,7 +2992,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def denominator(self): | ||
""" | ||
Returns the denominator of this rational number. | ||
Return the denominator of this rational number. | ||
denom is an alias of denominator. | ||
EXAMPLES:: | ||
|
@@ -3345,7 +3315,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def round(Rational self, mode="away"): | ||
""" | ||
Returns the nearest integer to ``self``, rounding away from 0 by | ||
Return the nearest integer to ``self``, rounding away from 0 by | ||
default, for consistency with the builtin Python round. | ||
INPUT: | ||
|
@@ -3411,7 +3381,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def real(self): | ||
""" | ||
Returns the real part of ``self``, which is ``self``. | ||
Return the real part of ``self``, which is ``self``. | ||
EXAMPLES:: | ||
|
@@ -3422,7 +3392,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def imag(self): | ||
""" | ||
Returns the imaginary part of ``self``, which is zero. | ||
Return the imaginary part of ``self``, which is zero. | ||
EXAMPLES:: | ||
|
@@ -3466,7 +3436,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def _lcm(self, Rational other): | ||
""" | ||
Returns the least common multiple, in the rational numbers, of ``self`` | ||
Return the least common multiple, in the rational numbers, of ``self`` | ||
and ``other``. This function returns either 0 or 1 (as a rational | ||
number). | ||
|
@@ -3667,7 +3637,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
sig_off() | ||
return x | ||
|
||
def __lshift__(x,y): | ||
def __lshift__(x, y): | ||
""" | ||
Left shift operator ``x << y``. | ||
|
@@ -3715,7 +3685,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
sig_off() | ||
return x | ||
|
||
def __rshift__(x,y): | ||
def __rshift__(x, y): | ||
""" | ||
Right shift operator ``x >> y``. | ||
|
@@ -3768,7 +3738,7 @@ cdef class Rational(sage.structure.element.FieldElement): | |
|
||
def __pari__(self): | ||
""" | ||
Returns the PARI version of this rational number. | ||
Return the PARI version of this rational number. | ||
EXAMPLES:: | ||
|