for symbol in substitutions:
if not isinstance(symbol, Symbol):
raise TypeError('symbols must be Symbol instances')
for symbol in substitutions:
if not isinstance(symbol, Symbol):
raise TypeError('symbols must be Symbol instances')
for symbol, coefficient in self._coefficients.items():
expression = substitutions.get(symbol, symbol)
result += coefficient * expression
for symbol, coefficient in self._coefficients.items():
expression = substitutions.get(symbol, symbol)
result += coefficient * expression
# Add implicit multiplication operators, e.g. '5x' -> '5*x'.
string = LinExpr._RE_NUM_VAR.sub(r'\1*\2', string)
tree = ast.parse(string, 'eval')
# Add implicit multiplication operators, e.g. '5x' -> '5*x'.
string = LinExpr._RE_NUM_VAR.sub(r'\1*\2', string)
tree = ast.parse(string, 'eval')
- expr = cls._fromast(tree)
- if not isinstance(expr, cls):
+ expression = cls._fromast(tree)
+ if not isinstance(expression, cls):
"""
Create a linear expression from a SymPy expression. Raise TypeError is
the sympy expression is not linear.
"""
Create a linear expression from a SymPy expression. Raise TypeError is
the sympy expression is not linear.
coefficient = Fraction(coefficient.p, coefficient.q)
if symbol == sympy.S.One:
constant = coefficient
coefficient = Fraction(coefficient.p, coefficient.q)
if symbol == sympy.S.One:
constant = coefficient
symbol = Symbol(symbol.name)
coefficients.append((symbol, coefficient))
else:
symbol = Symbol(symbol.name)
coefficients.append((symbol, coefficient))
else:
- raise TypeError('non-linear expression: {!r}'.format(expr))
- expr = LinExpr(coefficients, constant)
- if not isinstance(expr, cls):
+ raise TypeError('non-linear expression: {!r}'.format(expression))
+ expression = LinExpr(coefficients, constant)
+ if not isinstance(expression, cls):
for symbol, coefficient in self.coefficients():
term = coefficient * sympy.Symbol(symbol.name)
for symbol, coefficient in self.coefficients():
term = coefficient * sympy.Symbol(symbol.name)