def __rsub__(self, other):
return -(self - other)
- @_polymorphic
def __mul__(self, other):
- if isinstance(other, Rational):
- return other.__rmul__(self)
+ if isinstance(other, numbers.Rational):
+ coefficients = dict(self._coefficients)
+ for symbol in coefficients:
+ coefficients[symbol] *= other
+ constant = self._constant * other
+ return Expression(coefficients, constant)
return NotImplemented
__rmul__ = __mul__
- @_polymorphic
def __truediv__(self, other):
- if isinstance(other, Rational):
- return other.__rtruediv__(self)
+ if isinstance(other, numbers.Rational):
+ coefficients = dict(self._coefficients)
+ for symbol in coefficients:
+ coefficients[symbol] /= other
+ constant = self._constant / other
+ # import pdb; pdb.set_trace()
+ return Expression(coefficients, constant)
return NotImplemented
- __rtruediv__ = __truediv__
-
@_polymorphic
def __eq__(self, other):
# "normal" equality
raise TypeError('name must be a string')
self = object().__new__(cls)
self._name = name.strip()
- self._coefficients = {self: 1}
- self._constant = 0
+ self._coefficients = {self: Fraction(1)}
+ self._constant = Fraction(0)
self._symbols = (self,)
self._dimension = 1
return self
self = object().__new__(cls)
self._index = Dummy._count
self._name = name.strip()
- self._coefficients = {self: 1}
- self._constant = 0
+ self._coefficients = {self: Fraction(1)}
+ self._constant = Fraction(0)
self._symbols = (self,)
self._dimension = 1
Dummy._count += 1
def __bool__(self):
return Fraction.__bool__(self)
- @_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):