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 number type based on Python integers. """
from __future__ import print_function, division
import operator
from sympy.polys.polyutils import PicklableWithSlots from sympy.polys.domains.domainelement import DomainElement
from sympy.core.compatibility import integer_types from sympy.core.sympify import converter from sympy.core.numbers import Rational from sympy.printing.defaults import DefaultPrinting from sympy.utilities import public
@public class PythonRational(DefaultPrinting, PicklableWithSlots, DomainElement): """ Rational number type based on Python integers.
This was supposed to be needed for compatibility with older Python versions which don't support Fraction. However, Fraction is very slow so we don't use it anyway.
Examples ========
>>> from sympy.polys.domains import PythonRational
>>> PythonRational(1) 1 >>> PythonRational(2, 3) 2/3 >>> PythonRational(14, 10) 7/5
"""
__slots__ = ['p', 'q']
def parent(self):
def __init__(self, p, q=1): raise ZeroDivisionError('rational number')
else:
@classmethod def new(cls, p, q):
def __hash__(self): else:
def __int__(self): return -(-p//q)
def __float__(self): return float(self.p)/self.q
def __abs__(self):
def __pos__(self): return self.new(+self.p, self.q)
def __neg__(self):
def __add__(self, other): elif isinstance(other, integer_types): p = self.p + self.q*other q = self.q else: return NotImplemented
def __radd__(self, other): return NotImplemented
def __sub__(self, other): else:
def __rsub__(self, other): if not isinstance(other, integer_types): return NotImplemented
p = self.q*other - self.p q = self.q
return self.__class__(p, q)
def __mul__(self, other): else: return NotImplemented
def __rmul__(self, other): return NotImplemented
def __div__(self, other): else: return NotImplemented
__truediv__ = __div__
def __rdiv__(self, other): return NotImplemented
__rtruediv__ = __rdiv__
def __mod__(self, other): return self.__class__(0)
def __divmod__(self, other): return (self//other, self % other)
def __pow__(self, exp):
def __nonzero__(self):
__bool__ = __nonzero__
def __eq__(self, other): else:
def __ne__(self, other):
def _cmp(self, other, op): except TypeError: return NotImplemented else:
def __lt__(self, other):
def __le__(self, other):
def __gt__(self, other):
def __ge__(self, other):
@property def numer(self):
@property def denom(self):
numerator = numer denominator = denom
def sympify_pythonrational(arg): converter[PythonRational] = sympify_pythonrational |