Simplify Expression.__mul__(), Expression.__truediv__()
[linpy.git] / pypol / linexprs.py
index c8745b5..b74628b 100644 (file)
@@ -134,22 +134,27 @@ class Expression:
     def __rsub__(self, other):
         return -(self - other)
 
     def __rsub__(self, other):
         return -(self - other)
 
-    @_polymorphic
     def __mul__(self, other):
     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__
 
         return NotImplemented
 
     __rmul__ = __mul__
 
-    @_polymorphic
     def __truediv__(self, other):
     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
 
         return NotImplemented
 
-    __rtruediv__ = __truediv__
-
     @_polymorphic
     def __eq__(self, other):
         # "normal" equality
     @_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()
             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._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 = 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
         self._symbols = (self,)
         self._dimension = 1
         Dummy._count += 1
@@ -437,24 +442,6 @@ class Rational(Expression, Fraction):
     def __bool__(self):
         return Fraction.__bool__(self)
 
     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):
     @classmethod
     def fromstring(cls, string):
         if not isinstance(string, str):