+ 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:
+ if not isinstance(symbol, Symbol):
+ raise TypeError('symbols must be Symbol instances')
+ coefficients = [(othersymbol, coefficient)
+ for othersymbol, coefficient in result._coefficients.items()
+ if othersymbol != symbol]
+ coefficient = result._coefficients.get(symbol, 0)
+ constant = result._constant
+ result = Expression(coefficients, constant) + coefficient*expression
+ return result
+