def apply_substitutions(monomial, monomial_substitutions, pure=False):
if is_number_type(monomial):<EOL><INDENT>return monomial<EOL><DEDENT>original_monomial = monomial<EOL>changed = True<EOL>if not pure:<EOL><INDENT>substitutions = monomial_substitutions<EOL><DEDENT>else:<EOL><INDENT>substitutions = {}<EOL>for lhs, rhs in monomial_substitutions.items():<EOL><INDENT>irrelevant = False<EOL>for atom in lhs.atoms():<EOL><INDENT>if atom.is_Number:<EOL><INDENT>continue<EOL><DEDENT>if not monomial.has(atom):<EOL><INDENT>irrelevant = True<EOL>break<EOL><DEDENT><DEDENT>if not irrelevant:<EOL><INDENT>substitutions[lhs] = rhs<EOL><DEDENT><DEDENT><DEDENT>while changed:<EOL><INDENT>for lhs, rhs in substitutions.items():<EOL><INDENT>monomial = fast_substitute(monomial, lhs, rhs)<EOL><DEDENT>if original_monomial == monomial:<EOL><INDENT>changed = False<EOL><DEDENT>original_monomial = monomial<EOL><DEDENT>return monomial<EOL>
Helper function to remove monomials from the basis.