__all__ = [
- 'Expression',
+ 'LinExpr',
'Symbol', 'Dummy', 'symbols',
'Rational',
]
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)
return wrapper
-class Expression:
+class LinExpr:
"""
This class implements linear expressions.
"""
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):
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__
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):
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__
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
"""
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
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
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)
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
return expr
-class Symbol(Expression):
+class Symbol(LinExpr):
def __new__(cls, name):
"""
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.
"""