def is_a_valid_next_step(self, old_expression: Expression, new_expression: Expression, theorems: List[Theorem]) -> bool: logger.info("Starting transition validation") logger.info("Checking if a theorem can be applied") theorems_that_apply = self.theorems_service.possible_theorems_for( old_expression, theorems) logger.info("THEOREMS THAT APPLY:") logger.info(theorems_that_apply) for theorem in theorems_that_apply: theo_applied = theorem.apply_to(old_expression) for possibility in theo_applied: if possibility != None and possibility.simplify( ) == new_expression.simplify(): logger.info("Theorem can be applied") return True theo_applied_reverse = theorem.apply_reverse_to(new_expression) for possibility in theo_applied_reverse: if possibility != None and possibility.simplify( ) == old_expression.simplify(): logger.info("Theorem can be applied") return True # try with derivatives logger.info("Try with derivatives") if old_expression.solve_derivatives().simplify( ) == new_expression.simplify(): logger.info("Derivatives were applied") return True only_one_derivative = old_expression.derivatives_solving_possibilities( ) for derivative_applied in only_one_derivative: if derivative_applied.simplify() == new_expression.simplify(): return True # try simplifying the expression logger.info("Try simplifying") old_simplifications = old_expression.get_simplifications() for simplification in old_simplifications: new_simplifications = new_expression.get_simplifications() if simplification in new_simplifications: logger.info("Simplifications were applied") return True logger.info("Invalid next step: " + str(new_expression) + " - Old expression: " + str(old_expression)) return False
def solution_tree_for(self, expression: Expression, theorems: List[Theorem], applied_theorem: Theorem): already_seen = set() subtrees = self.subtrees(expression, theorems, already_seen) already_seen |= subtrees[1] tree = SolutionTreeNode(expression, applied_theorem.name, subtrees[0]) # TODO: check if this si necessary simplified_expression = expression.simplify() if simplified_expression.sympy_expr != expression.sympy_expr: if simplified_expression.to_string() in already_seen: tree.branches.append( SolutionTreeNode(simplified_expression, 'simplificacion', [])) else: tree.branches.append( SolutionTreeNode( simplified_expression, 'simplificacion', self.subtrees(simplified_expression, theorems, already_seen)[0])) return tree