+ def subs(self, symbol, expression=None):
+ if expression is None:
+ if isinstance(symbol, Mapping):
+ symbol = symbol.items()
+ substitutions = symbol
+ else:
+ substitutions = [(symbol, expression)]
+ result = self
+ for symbol, expression in substitutions:
+ coefficients = [(othersymbol, coefficient)
+ for othersymbol, coefficient in result.coefficients()
+ if othersymbol != symbol]
+ coefficient = result.coefficient(symbol)
+ constant = result.constant
+ result = Expression(coefficients, constant) + coefficient*expression
+ return result
+