class_docstr: Rational number implementation that is intended to be compatible with
  gmpy2's mpq.
  
  Also slightly faster than fractions.Fraction.
  
  PythonMPQ should be treated as immutable although no effort is made to
  prevent mutation (since that might slow down calculations).
class_docstr_tok: ["Rational","number","implementation","that","is","intended","to","be","compatible","with","gmpy2","'s","mpq",".","Also","slightly","faster","than","fractions.Fraction",".","PythonMPQ","should","be","treated","as","immutable","although","no","effort","is","made","to","prevent","mutation","(","since","that","might","slow","down","calculations",")","."]
docstr: q1 + q2
docstr_tok: ["q1","+","q2"]
code: def __add__(self, other):
      """q1 + q2"""
      if isinstance(other, PythonMPQ):
          #
          # This is much faster than the naive method used in the stdlib
          # fractions module. Not sure where this method comes from
          # though...
          #
          # Compare timings for something like:
          #   nums = range(1000)
          #   rats = [PythonMPQ(n, d) for n, d in zip(nums[:-5], nums[5:])]
          #   sum(rats) # <-- time this
          #
          ap, aq = self.numerator, self.denominator
          bp, bq = other.numerator, other.denominator
          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.numerator + self.denominator * other
          q = self.denominator
      else:
          return NotImplemented
  
      return self._new(p, q)
  
imports: import operator
  from math import gcd
  from decimal import Decimal
  from fractions import Fraction
  import sys
  from typing import Tuple, Type
label_desc: Use image node_id 20 for calling the PythonMPQ obj's underlying member method code with example usage: obj.__add__(other) and returns: self, NotImplemented