def simplify_polynomial(polynomial, monomial_substitutions):
if isinstance(polynomial, (int, float, complex)):<EOL><INDENT>return polynomial<EOL><DEDENT>polynomial = (<NUM_LIT:1.0> * polynomial).expand(mul=True,<EOL>multinomial=True)<EOL>if is_number_type(polynomial):<EOL><INDENT>return polynomial<EOL><DEDENT>if polynomial.is_Mul:<EOL><INDENT>elements = [polynomial]<EOL><DEDENT>else:<EOL><INDENT>elements = polynomial.as_coeff_mul()[<NUM_LIT:1>][<NUM_LIT:0>].as_coeff_add()[<NUM_LIT:1>]<EOL><DEDENT>new_polynomial = <NUM_LIT:0><EOL>for element in elements:<EOL><INDENT>monomial, coeff = separate_scalar_factor(element)<EOL>monomial = apply_substitutions(monomial, monomial_substitutions)<EOL>new_polynomial += coeff * monomial<EOL><DEDENT>return new_polynomial<EOL>
Simplify a polynomial for uniform handling later.