def try_transform(self, expression, pattern=None): assert isinstance(expression, Operation) if pattern is None: pattern = SubstitutionPattern(dict()) assert isinstance(pattern, SubstitutionPattern) substituted_start = self._substitute(self._start, pattern) if not Operation.areEqual(substituted_start, expression): raise TransformException( '{} substituted with {} == {} != {}'.format( str(self._start), str(pattern), str(substituted_start), str(expression))) else: return substituted_start
def single_variable(equation, variable, print_out=False, max_iterations=1000): assert isinstance(equation, Equation) if type(variable) == str: variable = Variable(variable) assert isinstance(variable, Variable) assert type(print_out) == bool assert type(max_iterations) == int assert variable.symbol in equation.get_variables() expected_result = Operation(variable) condition = lambda x: ( (Operation.areEqual(x.lhs, expected_result) and variable.symbol not in x.rhs.get_variables())\ or x is None ) distributions = [ ExpressionSubstitution(Parser.parse_expression('a * (b + c)'), Parser.parse_expression('a * b + a * c')), ExpressionSubstitution(Parser.parse_expression('(a + b) * c'), Parser.parse_expression('a * c + b * c')), ExpressionSubstitution(Parser.parse_expression('(a + b) / c'), Parser.parse_expression('a / c + b / c')), ExpressionSubstitution(Parser.parse_expression('a * (b - c)'), Parser.parse_expression('a * b - a * c')), ExpressionSubstitution(Parser.parse_expression('(a - b) * c'), Parser.parse_expression('a * c - b * c')), ExpressionSubstitution(Parser.parse_expression('(a - b) / c'), Parser.parse_expression('a / c - b / c')), ] distributions = [ Transformation.apply_all_substitution_transformations(x) for x in distributions ] distribute = SolverStep( Transformation.each_transformation(distributions, False)) pre_solve = SolverStep( Transformation.collect_like_terms_transformation()) equation = distribute.execute_step(equation) equation = pre_solve.execute_step(equation) branches = [equation] executed_branches = set() iterations = 0 no_regrets_transformations = [ Transformation.apply_all_substitution_transformations(x)\ for x in Solver.no_regrets_substitutions ] solve_step = SolverStep(Transformation.evaluation_transformation(), terminate_on_repeat=True) solve_step_2 = SolverStep( Transformation.each_transformation(no_regrets_transformations, False)) solve_step_3 = SolverStep(Transformation.flip_transformation(variable)) solve_step_4 = SolverStep.cancellations() solve_step.next_step = solve_step_2 solve_step_2.next_step = solve_step_3 solve_step_3.next_step = solve_step_4 solve_step_4.next_step = solve_step while iterations < max_iterations: if len(branches) == 0: raise SolverException('Exhausted possible transformations.') branch = branches[0] branches = branches[1:] if print_out: print('Executing branch: {}'.format(str(branch))) result = solve_step.execute_until(branch, condition, print_out=print_out) if condition(result): final_execute_step = SolverStep( Transformation.evaluation_transformation()) return final_execute_step.execute_step(result) else: executed_branches.add(str(branch)) executed_branches.add( str(result)) # Executed already since steps terminated new_branches = dict() # We don't care about the outputs of flips or cancellations new_branch_strings = solve_step_3.previous_inputs - executed_branches for string in new_branch_strings: new_branches[string] = Parser.parse_equation(string) solve_step.clear_history() solve_step_2.clear_history() solve_step_3.clear_history() solve_step_4.clear_history() for substitution in Solver.substitutions: left_substitution_result = [ x[1] for x in substitution.get_all_substitutions(branch.lhs) ] right_substitution_result = [ x[1] for x in substitution.get_all_substitutions(branch.rhs) ] equations = [ Equation(x, branch.rhs) for x in left_substitution_result ] equations += [ Equation(branch.lhs, x) for x in right_substitution_result ] pairs = [(str(x), x) for x in equations if str(x) not in executed_branches] for k, v in pairs: new_branches[k] = v if print_out: print("New branches from {}:\n{}\n".format( str(branch), '\n'.join(new_branches.keys()))) branches += new_branches.values() branches.sort(key=lambda x: len(str(x))) iterations += 1 raise SolverException( 'Could not solve equation for a single variable. Final result: {}' .format(str(equation)))
def substitutes(self, expression, pattern): assert isinstance(expression, Operation) assert isinstance(pattern, SubstitutionPattern) substituted_start = self._substitute(self._start, pattern) return Operation.areEqual(substituted_start, expression)