Better implementation of symbols and constants
authorVivien Maisonneuve <v.maisonneuve@gmail.com>
Wed, 2 Jul 2014 04:53:14 +0000 (06:53 +0200)
committerVivien Maisonneuve <v.maisonneuve@gmail.com>
Wed, 2 Jul 2014 04:53:14 +0000 (06:53 +0200)
pypol/linexprs.py

index 3aef337..9449219 100644 (file)
@@ -83,7 +83,7 @@ class Expression:
 
     def coefficient(self, symbol):
         if isinstance(symbol, Symbol):
-            symbol = str(symbol)
+            symbol = symbol.name
         elif not isinstance(symbol, str):
             raise TypeError('symbol must be a string or a Symbol instance')
         try:
@@ -339,8 +339,9 @@ class Expression:
 
 class Symbol(Expression):
 
-    __slots__ = Expression.__slots__ + (
+    __slots__ = (
         '_name',
+        '_hash',
     )
 
     def __new__(cls, name):
@@ -350,11 +351,7 @@ class Symbol(Expression):
             raise TypeError('name must be a string or a Symbol instance')
         name = name.strip()
         self = object().__new__(cls)
-        self._coefficients = OrderedDict([(name, 1)])
-        self._constant = 0
-        self._symbols = tuple(name)
         self._name = name
-        self._dimension = 1
         self._hash = hash(self._name)
         return self
 
@@ -362,9 +359,40 @@ class Symbol(Expression):
     def name(self):
         return self._name
 
+    def __hash__(self):
+        return self._hash
+
+    def coefficient(self, symbol):
+        if isinstance(symbol, Symbol):
+            symbol = symbol.name
+        elif not isinstance(symbol, str):
+            raise TypeError('symbol must be a string or a Symbol instance')
+        if symbol == self.name:
+            return 1
+        else:
+            return 0
+
+    def coefficients(self):
+        yield self.name, 1
+
+    @property
+    def constant(self):
+        return 0
+
+    @property
+    def symbols(self):
+        return self.name,
+
+    @property
+    def dimension(self):
+        return 1
+
     def issymbol(self):
         return True
 
+    def __eq__(self, other):
+        return isinstance(other, Symbol) and self.name == other.name
+
     @classmethod
     def _fromast(cls, node):
         if isinstance(node, ast.Module) and len(node.body) == 1:
@@ -395,21 +423,48 @@ def symbols(names):
 
 class Constant(Expression):
 
+    __slots__ = (
+        '_constant',
+        '_hash',
+    )
+
     def __new__(cls, numerator=0, denominator=None):
         self = object().__new__(cls)
         if denominator is None and isinstance(numerator, Constant):
             self._constant = numerator.constant
         else:
             self._constant = Fraction(numerator, denominator)
-        self._coefficients = OrderedDict()
-        self._symbols = ()
-        self._dimension = 0
         self._hash = hash(self._constant)
         return self
 
+    def __hash__(self):
+        return self._hash
+
+    def coefficient(self, symbol):
+        if isinstance(symbol, Symbol):
+            symbol = symbol.name
+        elif not isinstance(symbol, str):
+            raise TypeError('symbol must be a string or a Symbol instance')
+        return 0
+
+    def coefficients(self):
+        yield from []
+
+    @property
+    def symbols(self):
+        return ()
+
+    @property
+    def dimension(self):
+        return 0
+
     def isconstant(self):
         return True
 
+    @_polymorphic
+    def __eq__(self, other):
+        return isinstance(other, Constant) and self.constant == other.constant
+
     def __bool__(self):
         return self.constant != 0