示例#1
0
    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)
示例#2
0
    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)
示例#3
0
    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
示例#4
0
    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
示例#5
0
    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)
示例#6
0
    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)
示例#7
0
    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)
示例#8
0
    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)
示例#9
0
    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)
示例#10
0
 def gcd(self, a, b):
     """Compute GCD of ``a`` and ``b``. """
     return gcd(a, b)