def distributive_property(term1: Term) -> (Term, bool): """ x*x*(1+3) --> x*x*1 + x*x*3 """ for i, child in enumerate(term1.children): # look for sum, if found use distributive property if child.label == ADD: term1.children.pop(i) # remove that sum, whats left are the terms from product to be distributed return Term(ADD, [Term(MUL, children=[summand] + [Term.copy(child) for child in term1.children]) for summand in child.children]), True else: # not found a sum among children return term1, False
def tan_derivative(term1: Term, rules) -> Term: """rule for differentiating tangens: (tan(u))' = u' + u'*(tan(u))^2""" arg = Term.copy(term1.children[0]) return Term(ADD, [ calculate_derivative(arg, rules), Term(MUL, [calculate_derivative(arg, rules), Term(POW, [term1, Term(2)])]) ])
def get_children_copy(self): """ try_to_simplify acts on a copy of children, because usually it needs to reverse the changes. """ return [Term.copy(child) for child in self.term.children]