Skip to content

Commit

Permalink
Support gmpy2.mpz ground type for Rational
Browse files Browse the repository at this point in the history
  • Loading branch information
skirpichev committed Oct 23, 2018
1 parent 33bca63 commit c74dc73
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
46 changes: 22 additions & 24 deletions diofant/core/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from ..utilities import filldedent
from .cache import cacheit, clear_cache
from .compatibility import DIOFANT_INTS, HAS_GMPY, as_int
from .compatibility import DIOFANT_INTS, GROUND_TYPES, HAS_GMPY, as_int, gmpy
from .containers import Tuple
from .decorators import _sympifyit
from .expr import AtomicExpr, Expr
Expand Down Expand Up @@ -1017,6 +1017,10 @@ def __format__(self, format_spec):
converter[float] = converter[decimal.Decimal] = Float


# Ground type for components of Rational
_int_dtype = gmpy.mpz if GROUND_TYPES == 'gmpy' else int


class Rational(Number):
"""Represents integers and rational numbers (p/q) of any size.
Expand Down Expand Up @@ -1134,8 +1138,9 @@ def __new__(cls, p, q=1):
return S.Half

obj = Expr.__new__(cls)
obj._numerator = int(p)
obj._denominator = int(q)

obj._numerator = _int_dtype(p)
obj._denominator = _int_dtype(q)

return obj

Expand Down Expand Up @@ -1273,8 +1278,8 @@ def __abs__(self):
def __int__(self):
p, q = self.numerator, self.denominator
if p < 0:
return -(-p//q)
return p//q
return -int(-p//q)
return int(p//q)

@_sympifyit('other', NotImplemented)
def __eq__(self, other):
Expand Down Expand Up @@ -1438,7 +1443,7 @@ def denominator(self):

class Integer(Rational):

_denominator = 1
_denominator = _int_dtype(1)
is_integer = True
is_number = True

Expand Down Expand Up @@ -1476,7 +1481,7 @@ def __new__(cls, i):
obj = S.NegativeOne
else:
obj = Expr.__new__(cls)
obj._numerator = ival
obj._numerator = _int_dtype(ival)

return obj

Expand All @@ -1487,7 +1492,7 @@ def __hash__(self):
return hash(self.numerator)

def __index__(self):
return self.numerator
return int(self.numerator)

def __eq__(self, other):
if isinstance(other, int):
Expand Down Expand Up @@ -1664,8 +1669,8 @@ class Zero(IntegerConstant, metaclass=Singleton):
.. [1] https//en.wikipedia.org/wiki/Zero
"""

_numerator = 0
_denominator = 1
_numerator = _int_dtype(0)
_denominator = _int_dtype(1)

is_positive = False
is_negative = False
Expand Down Expand Up @@ -1709,8 +1714,8 @@ class One(IntegerConstant, metaclass=Singleton):

is_number = True

_numerator = 1
_denominator = 1
_numerator = _int_dtype(1)
_denominator = _int_dtype(1)


class NegativeOne(IntegerConstant, metaclass=Singleton):
Expand All @@ -1737,8 +1742,8 @@ class NegativeOne(IntegerConstant, metaclass=Singleton):

is_number = True

_numerator = -1
_denominator = 1
_numerator = _int_dtype(-1)
_denominator = _int_dtype(1)

def _eval_power(self, expt):
if isinstance(expt, Number):
Expand Down Expand Up @@ -1787,8 +1792,8 @@ class Half(RationalConstant, metaclass=Singleton):

is_number = True

_numerator = 1
_denominator = 2
_numerator = _int_dtype(1)
_denominator = _int_dtype(2)


class Infinity(Number, metaclass=Singleton):
Expand Down Expand Up @@ -2786,12 +2791,7 @@ def sympify_fractions(f):
converter[fractions.Fraction] = sympify_fractions


try:
if HAS_GMPY:
import gmpy2 as gmpy
else:
raise ImportError

if HAS_GMPY:
def sympify_mpz(x):
return Integer(int(x))

Expand All @@ -2800,8 +2800,6 @@ def sympify_mpq(x):

converter[gmpy.mpz] = sympify_mpz
converter[gmpy.mpq] = sympify_mpq
except ImportError:
pass


def sympify_mpmath(x):
Expand Down
2 changes: 1 addition & 1 deletion diofant/functions/special/error_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,7 @@ class Li(Function):
def eval(cls, z):
if z is oo:
return oo
elif z is 2*S.One:
elif z == 2:
return S.Zero

def fdiff(self, argindex=1):
Expand Down
5 changes: 3 additions & 2 deletions diofant/printing/repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _print_NumberSymbol(self, expr):
return str(expr)

def _print_Integer(self, expr):
return 'Integer(%i)' % expr.numerator
return 'Integer(%i)' % int(expr.numerator)

def _print_list(self, expr):
return "[%s]" % self.reprify(expr, ", ")
Expand Down Expand Up @@ -116,7 +116,8 @@ def _print_Mul(self, expr, order=None):
return "Mul(%s)" % ", ".join(args)

def _print_Rational(self, expr):
return 'Rational(%s, %s)' % (self._print(expr.numerator), self._print(expr.denominator))
return 'Rational(%s, %s)' % (self._print(int(expr.numerator)),
self._print(int(expr.denominator)))

def _print_Float(self, expr):
dps = prec_to_dps(expr._prec)
Expand Down
1 change: 1 addition & 0 deletions docs/release/notes-0.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Minor changes
* Support detection of imaginary roots in :class:`~diofant.polys.rootoftools.RootOf`, see :pull:`625`.
* Mutable matrices support indexed deletion with :meth:`~object.__delitem__`, see :pull:`688`.
* Integer powers of :class:`~diofant.polys.rootoftools.RootOf` instances are automatically reduced, according to their minimal polynomial, see :pull:`691`.
* Support gmpy2.mpz ground type for numerator/denominator of :class:`~diofant.core.numbers.Rational`, see :pull:`694`.

Developer changes
=================
Expand Down

0 comments on commit c74dc73

Please sign in to comment.