X-Git-Url: https://scm.cri.ensmp.fr/git/linpy.git/blobdiff_plain/3f3ec5755dc4b96250b8bd09be9bede967d7203d..7afcb0a1ded9e9a331b131689d68c085f712143f:/linpy/linexprs.py diff --git a/linpy/linexprs.py b/linpy/linexprs.py index bc36fda..8267be1 100644 --- a/linpy/linexprs.py +++ b/linpy/linexprs.py @@ -25,7 +25,7 @@ from fractions import Fraction, gcd __all__ = [ - 'Expression', + 'LinExpr', 'Symbol', 'Dummy', 'symbols', 'Rational', ] @@ -34,7 +34,7 @@ __all__ = [ def _polymorphic(func): @functools.wraps(func) def wrapper(left, right): - if isinstance(right, Expression): + if isinstance(right, LinExpr): return func(left, right) elif isinstance(right, numbers.Rational): right = Rational(right) @@ -43,7 +43,7 @@ def _polymorphic(func): return wrapper -class Expression: +class LinExpr: """ This class implements linear expressions. """ @@ -55,7 +55,7 @@ class Expression: if isinstance(coefficients, str): if constant != 0: raise TypeError('too many arguments') - return Expression.fromstring(coefficients) + return LinExpr.fromstring(coefficients) if coefficients is None: return Rational(constant) if isinstance(coefficients, Mapping): @@ -163,7 +163,7 @@ class Expression: for symbol, coefficient in other._coefficients.items(): coefficients[symbol] += coefficient constant = self._constant + other._constant - return Expression(coefficients, constant) + return LinExpr(coefficients, constant) __radd__ = __add__ @@ -176,7 +176,7 @@ class Expression: for symbol, coefficient in other._coefficients.items(): coefficients[symbol] -= coefficient constant = self._constant - other._constant - return Expression(coefficients, constant) + return LinExpr(coefficients, constant) @_polymorphic def __rsub__(self, other): @@ -190,7 +190,7 @@ class Expression: coefficients = ((symbol, coefficient * other) for symbol, coefficient in self._coefficients.items()) constant = self._constant * other - return Expression(coefficients, constant) + return LinExpr(coefficients, constant) return NotImplemented __rmul__ = __mul__ @@ -200,7 +200,7 @@ class Expression: coefficients = ((symbol, coefficient / other) for symbol, coefficient in self._coefficients.items()) constant = self._constant / other - return Expression(coefficients, constant) + return LinExpr(coefficients, constant) return NotImplemented @_polymorphic @@ -208,7 +208,7 @@ class Expression: """ Test whether two expressions are equal """ - return isinstance(other, Expression) and \ + return isinstance(other, LinExpr) and \ self._coefficients == other._coefficients and \ self._constant == other._constant @@ -256,7 +256,7 @@ class Expression: if othersymbol != symbol] coefficient = result._coefficients.get(symbol, 0) constant = result._constant - result = Expression(coefficients, constant) + coefficient*expression + result = LinExpr(coefficients, constant) + coefficient*expression return result @classmethod @@ -292,7 +292,7 @@ class Expression: Create an expression from a string. """ # add implicit multiplication operators, e.g. '5x' -> '5*x' - string = Expression._RE_NUM_VAR.sub(r'\1*\2', string) + string = LinExpr._RE_NUM_VAR.sub(r'\1*\2', string) tree = ast.parse(string, 'eval') return cls._fromast(tree) @@ -368,11 +368,11 @@ class Expression: coefficients.append((symbol, coefficient)) else: raise ValueError('non-linear expression: {!r}'.format(expr)) - return Expression(coefficients, constant) + return LinExpr(coefficients, constant) def tosympy(self): """ - Return an expression as a sympy object. + Return an expression as a sympy object. """ import sympy expr = 0 @@ -383,7 +383,7 @@ class Expression: return expr -class Symbol(Expression): +class Symbol(LinExpr): def __new__(cls, name): """ @@ -494,7 +494,7 @@ def symbols(names): return tuple(Symbol(name) for name in names) -class Rational(Expression, Fraction): +class Rational(LinExpr, Fraction): """ This class represents integers and rational numbers of any size. """