X-Git-Url: https://svn.cri.ensmp.fr/git/linpy.git/blobdiff_plain/29ed88d1a15d283ea6f3340a4dd97e8cc7c2d2d4..9f4798446bbb3ac43f7b8291ddae095af3818299:/pypol/linexprs.py diff --git a/pypol/linexprs.py b/pypol/linexprs.py index ccd1564..73c6b0e 100644 --- a/pypol/linexprs.py +++ b/pypol/linexprs.py @@ -10,7 +10,7 @@ from fractions import Fraction, gcd __all__ = [ 'Expression', 'Symbol', 'symbols', - 'Constant', + 'Rational', ] @@ -20,7 +20,7 @@ def _polymorphic(func): if isinstance(right, Expression): return func(left, right) elif isinstance(right, numbers.Rational): - right = Constant(right) + right = Rational(right) return func(left, right) return NotImplemented return wrapper @@ -44,7 +44,7 @@ class Expression: raise TypeError('too many arguments') return Expression.fromstring(coefficients) if coefficients is None: - return Constant(constant) + return Rational(constant) if isinstance(coefficients, dict): coefficients = coefficients.items() for symbol, coefficient in coefficients: @@ -53,7 +53,7 @@ class Expression: coefficients = [(symbol, coefficient) for symbol, coefficient in coefficients if coefficient != 0] if len(coefficients) == 0: - return Constant(constant) + return Rational(constant) if len(coefficients) == 1 and constant == 0: symbol, coefficient = coefficients[0] if coefficient == 1: @@ -61,18 +61,18 @@ class Expression: self = object().__new__(cls) self._coefficients = OrderedDict() for symbol, coefficient in sorted(coefficients, - key=lambda item: item[0].name): - if isinstance(coefficient, Constant): + key=lambda item: item[0].sortkey()): + if isinstance(coefficient, Rational): coefficient = coefficient.constant if not isinstance(coefficient, numbers.Rational): raise TypeError('coefficients must be rational numbers ' - 'or Constant instances') + 'or Rational instances') self._coefficients[symbol] = coefficient - if isinstance(constant, Constant): + if isinstance(constant, Rational): constant = constant.constant if not isinstance(constant, numbers.Rational): raise TypeError('constant must be a rational number ' - 'or a Constant instance') + 'or a Rational instance') self._constant = constant self._symbols = tuple(self._coefficients) self._dimension = len(self._symbols) @@ -127,7 +127,7 @@ class Expression: @_polymorphic def __add__(self, other): - coefficients = defaultdict(Constant, self.coefficients()) + coefficients = defaultdict(Rational, self.coefficients()) for symbol, coefficient in other.coefficients(): coefficients[symbol] += coefficient constant = self.constant + other.constant @@ -137,7 +137,7 @@ class Expression: @_polymorphic def __sub__(self, other): - coefficients = defaultdict(Constant, self.coefficients()) + coefficients = defaultdict(Rational, self.coefficients()) for symbol, coefficient in other.coefficients(): coefficients[symbol] -= coefficient constant = self.constant - other.constant @@ -166,8 +166,8 @@ class Expression: if other.isconstant(): coefficients = dict(self.coefficients()) for symbol in coefficients: - coefficients[symbol] = Constant(coefficients[symbol], other.constant) - constant = Constant(self.constant, other.constant) + coefficients[symbol] = Rational(coefficients[symbol], other.constant) + constant = Rational(self.constant, other.constant) return Expression(coefficients, constant) if isinstance(other, Expression): raise ValueError('non-linear expression: ' @@ -177,7 +177,7 @@ class Expression: def __rtruediv__(self, other): if isinstance(other, self): if self.isconstant(): - return Constant(other, self.constant) + return Rational(other, self.constant) else: raise ValueError('non-linear expression: ' '{} / {}'.format(other._parenstr(), self._parenstr())) @@ -242,7 +242,7 @@ class Expression: elif isinstance(node, ast.Name): return Symbol(node.id) elif isinstance(node, ast.Num): - return Constant(node.n) + return Rational(node.n) elif isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.USub): return -cls._fromast(node.operand) elif isinstance(node, ast.BinOp): @@ -380,6 +380,9 @@ class Symbol(Expression): def dimension(self): return 1 + def sortkey(self): + return self.name, + def issymbol(self): return True @@ -414,7 +417,7 @@ def symbols(names): return tuple(Symbol(name) for name in names) -class Constant(Expression): +class Rational(Expression): __slots__ = ( '_constant', @@ -422,7 +425,7 @@ class Constant(Expression): def __new__(cls, numerator=0, denominator=None): self = object().__new__(cls) - if denominator is None and isinstance(numerator, Constant): + if denominator is None and isinstance(numerator, Rational): self._constant = numerator.constant else: self._constant = Fraction(numerator, denominator) @@ -455,7 +458,7 @@ class Constant(Expression): @_polymorphic def __eq__(self, other): - return isinstance(other, Constant) and self.constant == other.constant + return isinstance(other, Rational) and self.constant == other.constant def __bool__(self): return self.constant != 0 @@ -464,14 +467,14 @@ class Constant(Expression): def fromstring(cls, string): if not isinstance(string, str): raise TypeError('string must be a string instance') - return Constant(Fraction(string)) + return Rational(Fraction(string)) @classmethod def fromsympy(cls, expr): import sympy if isinstance(expr, sympy.Rational): - return Constant(expr.p, expr.q) + return Rational(expr.p, expr.q) elif isinstance(expr, numbers.Rational): - return Constant(expr) + return Rational(expr) else: raise TypeError('expr must be a sympy.Rational instance')