E1 + E2*x + E3*x**2
if type(expr) in (set, list, tuple):
renumbered = [constant_renumber(e, variables, newconstants) for e in expr]
return type(expr)(renumbered)
// Symbols in solution but not ODE are constants
if variables is not None:
variables = set(variables)
constantsymbols = list(expr.free_symbols - variables)
// Any Cn is a constant...
else:
variables = set()
isconstant = lambda s: s.startswith("C") and s[1:].isdigit()
constantsymbols = [sym for sym in expr.free_symbols if isconstant(sym.name)]
// Find new constants checking that they aren"t already in the ODE
if newconstants is None:
iter_constants = numbered_symbols(start=1, prefix="C", exclude=variables)
else:
iter_constants = (sym for sym in newconstants if sym not in variables)
// XXX: This global newstartnumber hack should be removed
global newstartnumber
newstartnumber = 1
endnumber = len(constantsymbols)
constants_found = [None]*(endnumber + 2)
// make a mapping to send all constantsymbols to S.One and use
// that to make sure that term ordering is not dependent on
After Change
else:
iter_constants = (sym for sym in newconstants if sym not in variables)
constants_found = []
// make a mapping to send all constantsymbols to S.One and use
// that to make sure that term ordering is not dependent on
// the indexed value of C
C_1 = [(ci, S.One) for ci in constantsymbols]
sort_key=lambda arg: default_sort_key(arg.subs(C_1))
def _constant_renumber(expr):
r
We need to have an internal recursive function
// For system of expressions
if isinstance(expr, Tuple):
renumbered = [_constant_renumber(e) for e in expr]
return Tuple(*renumbered)
if isinstance(expr, Equality):
return Eq(
_constant_renumber(expr.lhs),
_constant_renumber(expr.rhs))
if type(expr) not in (Mul, Add, Pow) and not expr.is_Function and \
not expr.has(*constantsymbols):
// Base case, as above. Hope there aren"t constants inside
// of some other class, because they won"t be renumbered.
return expr
elif expr.is_Piecewise:
return expr
elif expr in constantsymbols:
if expr not in constants_found:
constants_found.append(expr)
return expr
elif expr.is_Function or expr.is_Pow:
return expr.func(
*[_constant_renumber(x) for x in expr.args])
else:
sortedargs = list(expr.args)
sortedargs.sort(key=sort_key)
return expr.func(*[_constant_renumber(x) for x in sortedargs])
expr = _constant_renumber(expr)
// Don"t renumber symbols present in the ODE.
constants_found = [c for c in constants_found if c not in variables]
// Renumbering happens here
subs_dict = {var: cons for var, cons in zip(constants_found, iter_constants)}
expr = expr.subs(subs_dict, simultaneous=True)
return expr