X-Git-Url: https://svn.cri.ensmp.fr/git/linpy.git/blobdiff_plain/ce545ffab9494e0c8250a8880859b6b097ed544d..53cfc921440668ddabd7d8192fada69348486a7f:/pypol/linexprs.py diff --git a/pypol/linexprs.py b/pypol/linexprs.py index 73c6b0e..ef5d90b 100644 --- a/pypol/linexprs.py +++ b/pypol/linexprs.py @@ -9,7 +9,7 @@ from fractions import Fraction, gcd __all__ = [ 'Expression', - 'Symbol', 'symbols', + 'Symbol', 'Dummy', 'symbols', 'Rational', ] @@ -355,7 +355,7 @@ class Symbol(Expression): return self._name def __hash__(self): - return hash(self._name) + return hash(self.sortkey()) def coefficient(self, symbol): if not isinstance(symbol, Symbol): @@ -390,7 +390,11 @@ class Symbol(Expression): yield 1 def __eq__(self, other): - return isinstance(other, Symbol) and self.name == other.name + return not isinstance(other, Dummy) and isinstance(other, Symbol) \ + and self.name == other.name + + def asdummy(self): + return Dummy(self.name) @classmethod def _fromast(cls, node): @@ -411,6 +415,34 @@ class Symbol(Expression): raise TypeError('expr must be a sympy.Symbol instance') +class Dummy(Symbol): + + __slots__ = ( + '_name', + '_index', + ) + + _count = 0 + + def __new__(cls, name=None): + if name is None: + name = 'Dummy_{}'.format(Dummy._count) + self = object().__new__(cls) + self._name = name.strip() + self._index = Dummy._count + Dummy._count += 1 + return self + + def __hash__(self): + return hash(self.sortkey()) + + def sortkey(self): + return self._name, self._index + + def __eq__(self, other): + return isinstance(other, Dummy) and self._index == other._index + + def symbols(names): if isinstance(names, str): names = names.replace(',', ' ').split()