- @_polymorphic
- def __mul__(self, other):
- coefficients = dict(other._coefficients)
- for symbol in coefficients:
- coefficients[symbol] *= self._constant
- constant = other._constant * self._constant
- return Expression(coefficients, constant)
-
- __rmul__ = __mul__
-
- @_polymorphic
- def __rtruediv__(self, other):
- coefficients = dict(other._coefficients)
- for symbol in coefficients:
- coefficients[symbol] /= self._constant
- constant = other._constant / self._constant
- return Expression(coefficients, constant)
-
- @classmethod
- def fromstring(cls, string):
- if not isinstance(string, str):
- raise TypeError('string must be a string instance')
- return Rational(Fraction(string))
+ def __repr__(self):
+ if self.denominator == 1:
+ return '{!r}'.format(self.numerator)
+ else:
+ return '{!r}/{!r}'.format(self.numerator, self.denominator)
+
+ def _repr_latex_(self):
+ if self.denominator == 1:
+ return '$${}$$'.format(self.numerator)
+ elif self.numerator < 0:
+ return '$$-\\frac{{{}}}{{{}}}$$'.format(-self.numerator,
+ self.denominator)
+ else:
+ return '$$\\frac{{{}}}{{{}}}$$'.format(self.numerator,
+ self.denominator)