Simplify Expression.__mul__(), Expression.__truediv__()
authorVivien Maisonneuve <v.maisonneuve@gmail.com>
Sun, 13 Jul 2014 14:23:43 +0000 (16:23 +0200)
committerVivien Maisonneuve <v.maisonneuve@gmail.com>
Sun, 13 Jul 2014 14:23:43 +0000 (16:23 +0200)
pypol/linexprs.py

index c8745b5..b74628b 100644 (file)
@@ -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):