def extended_euclidean(q, r): """Return a tuple *(p, a, b)* such that :math:`p = aq + br`, where *p* is the greatest common divisor of *q* and *r*. See also the `Wikipedia article on the Euclidean algorithm <https://en.wikipedia.org/wiki/Euclidean_algorithm>`_. """ import pymbolic.traits as traits # see [Davenport], Appendix, p. 214 t = traits.common_traits(q, r) if t.norm(q) < t.norm(r): p, a, b = extended_euclidean(r, q) return p, b, a Q = 1, 0 # noqa R = 0, 1 # noqa while r: quot, t = divmod(q, r) T = Q[0] - quot * R[0], Q[1] - quot * R[1] # noqa q, r = r, t Q, R = R, T return q, Q[0], Q[1]
def extended_euclidean(q, r): """Return a tuple *(p, a, b)* such that :math:`p = aq + br`, where *p* is the greatest common divisor of *q* and *r*. See also the `Wikipedia article on the Euclidean algorithm <https://en.wikipedia.org/wiki/Euclidean_algorithm>`_. """ import pymbolic.traits as traits # see [Davenport], Appendix, p. 214 t = traits.common_traits(q, r) if t.norm(q) < t.norm(r): p, a, b = extended_euclidean(r, q) return p, b, a Q = 1, 0 # noqa R = 0, 1 # noqa while r: quot, t = divmod(q, r) T = Q[0] - quot*R[0], Q[1] - quot*R[1] # noqa q, r = r, t Q, R = R, T return q, Q[0], Q[1]
def __add__(self, other): if not isinstance(other, Rational): newother = Rational(other) else: newother = other try: t = traits.common_traits(self.Denominator, newother.Denominator) newden = t.lcm(self.Denominator, newother.Denominator) newnum = self.Numerator * newden/self.Denominator + \ newother.Numerator * newden/newother.Denominator gcd = t.gcd(newden, newnum) return primitives.quotient(newnum/gcd, newden/gcd) except traits.NoTraitsError: return primitives.Expression.__add__(self, other) except traits.NoCommonTraitsError: return primitives.Expression.__add__(self, other)
def quotient(numerator, denominator): if not (denominator - 1): return numerator import pymbolic.rational as rat if isinstance(numerator, rat.Rational) and \ isinstance(denominator, rat.Rational): return numerator * denominator.reciprocal() try: c_traits = traits.common_traits(numerator, denominator) if isinstance(c_traits, traits.EuclideanRingTraits): return rat.Rational(numerator, denominator) except traits.NoCommonTraitsError: pass except traits.NoTraitsError: pass return Quotient(numerator, denominator)
def quotient(numerator, denominator): if not (denominator-1): return numerator import pymbolic.rational as rat if isinstance(numerator, rat.Rational) and \ isinstance(denominator, rat.Rational): return numerator * denominator.reciprocal() try: c_traits = traits.common_traits(numerator, denominator) if isinstance(c_traits, traits.EuclideanRingTraits): return rat.Rational(numerator, denominator) except traits.NoCommonTraitsError: pass except traits.NoTraitsError: pass return Quotient(numerator, denominator)
def __mul__(self, other): if not isinstance(other, Rational): newother = Rational(other) try: t = traits.common_traits(self.Numerator, newother.Numerator, self.Denominator, newother. Denominator) gcd_1 = t.gcd(self.Numerator, newother.Denominator) gcd_2 = t.gcd(newother.Numerator, self.Denominator) new_num = self.Numerator/gcd_1 * newother.Numerator/gcd_2 new_denom = self.Denominator/gcd_2 * newother.Denominator/gcd_1 if not (new_denom-1): return new_num return Rational(new_num, new_denom) except traits.NoTraitsError: return primitives.Expression.__mul__(self, other) except traits.NoCommonTraitsError: return primitives.Expression.__mul__(self, other)
def extended_euclidean(q, r): """Return a tuple (p, a, b) such that p = aq + br, where p is the greatest common divisor. """ import pymbolic.traits as traits # see [Davenport], Appendix, p. 214 t = traits.common_traits(q, r) if t.norm(q) < t.norm(r): p, a, b = extended_euclidean(r, q) return p, b, a Q = 1, 0 R = 0, 1 while r: quot, t = divmod(q, r) T = Q[0] - quot * R[0], Q[1] - quot * R[1] q, r = r, t Q, R = R, T return q, Q[0], Q[1]
def extended_euclidean(q, r): """Return a tuple (p, a, b) such that p = aq + br, where p is the greatest common divisor. """ import pymbolic.traits as traits # see [Davenport], Appendix, p. 214 t = traits.common_traits(q, r) if t.norm(q) < t.norm(r): p, a, b = extended_euclidean(r, q) return p, b, a Q = 1, 0 R = 0, 1 while r: quot, t = divmod(q, r) T = Q[0] - quot*R[0], Q[1] - quot*R[1] q, r = r, t Q, R = R, T return q, Q[0], Q[1]