X-Git-Url: https://svn.cri.ensmp.fr/git/linpy.git/blobdiff_plain/5b661514c1bbabd8205fdbd22a0ba0f6b1ac6305..1695c920d030869b3a842736fe5bcf963f2ffc52:/pypol/linexprs.py diff --git a/pypol/linexprs.py b/pypol/linexprs.py index c8745b5..b74628b 100644 --- a/pypol/linexprs.py +++ b/pypol/linexprs.py @@ -134,22 +134,27 @@ class Expression: 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 @@ -324,8 +329,8 @@ class Symbol(Expression): 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 @@ -385,8 +390,8 @@ class Dummy(Symbol): 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 @@ -437,24 +442,6 @@ class Rational(Expression, Fraction): 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):