import numbers
import re
+from collections import OrderedDict
from fractions import Fraction, gcd
'_constant',
'_symbols',
'_dimension',
+ '_hash',
)
def __new__(cls, coefficients=None, constant=0):
raise TypeError('coefficients must be rational numbers '
'or Constant instances')
self._coefficients[symbol] = coefficient
+ self._coefficients = OrderedDict(sorted(self._coefficients.items()))
if isinstance(constant, Constant):
constant = constant.constant
if not isinstance(constant, numbers.Rational):
raise TypeError('constant must be a rational number '
'or a Constant instance')
self._constant = constant
- self._symbols = tuple(sorted(self._coefficients))
+ self._symbols = tuple(self._coefficients)
self._dimension = len(self._symbols)
+ self._hash = hash((tuple(self._coefficients.items()), self._constant))
return self
def coefficient(self, symbol):
__getitem__ = coefficient
def coefficients(self):
- for symbol in self.symbols:
- yield symbol, self.coefficient(symbol)
+ yield from self._coefficients.items()
@property
def constant(self):
def dimension(self):
return self._dimension
+ def __hash__(self):
+ return self._hash
+
def isconstant(self):
return False
from .polyhedra import Gt
return Gt(self, other)
- def __hash__(self):
- return hash((tuple(sorted(self._coefficients.items())), self._constant))
-
def _toint(self):
lcm = functools.reduce(lambda a, b: a*b // gcd(a, b),
[value.denominator for value in self.values()])
raise TypeError('name must be a string or a Symbol instance')
name = name.strip()
self = object().__new__(cls)
- self._coefficients = {name: 1}
+ 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
@property
self._constant = numerator.constant
else:
self._constant = Fraction(numerator, denominator)
- self._coefficients = {}
+ self._coefficients = OrderedDict()
self._symbols = ()
self._dimension = 0
+ self._hash = hash(self._constant)
return self
def isconstant(self):