def __div__(self, other): from sympy.polys.domains.groundtypes import python_gcd as gcd if isinstance(other, PythonRational): ap, aq, bp, bq = self.p, self.q, other.p, other.q x1 = gcd(ap, bp) x2 = gcd(bq, aq) p, q = ((ap // x1) * (bq // x2), (aq // x2) * (bp // x1)) elif isinstance(other, int): x = gcd(other, self.p) p = self.p // x q = self.q * (other // x) else: return NotImplemented return self.__class__(p, q, _gcd=False)
def __div__(self, other): from sympy.polys.domains.groundtypes import python_gcd as gcd if isinstance(other, PythonRational): ap, aq, bp, bq = self.p, self.q, other.p, other.q x1 = gcd(ap, bp) x2 = gcd(bq, aq) p, q = ((ap//x1)*(bq//x2), (aq//x2)*(bp//x1)) elif isinstance(other, integer_types): x = gcd(other, self.p) p = self.p//x q = self.q*(other//x) else: return NotImplemented return self.__class__(p, q, _gcd=False)
def __init__(self, p, q=1, _gcd=True): from sympy.polys.domains.groundtypes import python_gcd as gcd if isinstance(p, Integer): p = p.p elif isinstance(p, Rational): p, q = p.p, p.q if not q: raise ZeroDivisionError('rational number') elif q < 0: p, q = -p, -q if not p: self.p = 0 self.q = 1 elif p == 1 or q == 1: self.p = p self.q = q else: if _gcd: x = gcd(p, q) if x != 1: p //= x q //= x self.p = p self.q = q
def __init__(self, p, q=1, _gcd=True): from sympy.polys.domains.groundtypes import python_gcd as gcd if isinstance(p, Integer): p = p.p elif isinstance(p, Rational): p, q = p.p, p.q if not q: raise ZeroDivisionError('rational number') elif q < 0: p, q = -p, -q if not p: self.p = 0 self.q = 1 elif p == 1 or q == 1: self.p = p self.q = q else: if _gcd: x = gcd(p, q) if x != 1: p //= x q //= x self.p = p self.q = q
def __rdiv__(self, other): from sympy.polys.domains.groundtypes import python_gcd as gcd if not isinstance(other, int): return NotImplemented x = gcd(self.p, other) p = self.q * (other // x) q = self.p // x return self.__class__(p, q)
def __rmul__(self, other): from sympy.polys.domains.groundtypes import python_gcd as gcd if not isinstance(other, integer_types): return NotImplemented x = gcd(self.q, other) p = self.p * (other // x) q = self.q // x return self.__class__(p, q, _gcd=False)
def __rdiv__(self, other): from sympy.polys.domains.groundtypes import python_gcd as gcd if not isinstance(other, integer_types): return NotImplemented x = gcd(self.p, other) p = self.q*(other//x) q = self.p//x return self.__class__(p, q)
def __sub__(self, other): from sympy.polys.domains.groundtypes import python_gcd as gcd if isinstance(other, PythonRational): ap, aq, bp, bq = self.p, self.q, other.p, other.q g = gcd(aq, bq) if g == 1: p = ap * bq - aq * bp q = bq * aq else: q1, q2 = aq // g, bq // g p, q = ap * q2 - bp * q1, q1 * q2 g2 = gcd(p, g) p, q = (p // g2), q * (g // g2) elif isinstance(other, int): p = self.p - self.q * other q = self.q else: return NotImplemented return self.__class__(p, q, _gcd=False)
def __sub__(self, other): from sympy.polys.domains.groundtypes import python_gcd as gcd if isinstance(other, PythonRational): ap, aq, bp, bq = self.p, self.q, other.p, other.q g = gcd(aq, bq) if g == 1: p = ap*bq - aq*bp q = bq*aq else: q1, q2 = aq//g, bq//g p, q = ap*q2 - bp*q1, q1*q2 g2 = gcd(p, g) p, q = (p // g2), q * (g // g2) elif isinstance(other, integer_types): p = self.p - self.q*other q = self.q else: return NotImplemented return self.__class__(p, q, _gcd=False)
def gcd(self, a, b): """Compute GCD of ``a`` and ``b``. """ return gcd(a, b)