def _merge_solutions(self): for variable_name in self._solution_method_recurse.get_solutions_name_set( ): variable_solution_equation = self._solution_method_recurse.get_solution( variable_name) self.set_solution(variable_name, variable_solution_equation) if variable_name in self._equation_to_resolve.get_name_set(): self._equation_to_resolve.apply_operation( Equation.EquationApplier(variable_name, variable_solution_equation)) self._equation_to_resolve.apply_operation( Equation.EquationSimplifyer())
def apply(self): equations_to_remove = [] for eq in self._equation_list: if eq.is_solution_equation(): solved_variable = eq.get_name_set().pop() eq.apply_operation(Equation.VariableIsolator(solved_variable)) self._found_solutions[solved_variable] = eq equations_to_remove.append(eq) for eq in equations_to_remove: self._equation_list.remove(eq) solutions_appplier = EquationListOperationApplier( Equation.SolutionEquationsApplier(self._found_solutions)) solutions_appplier.set_equation_list(self._equation_list) solutions_appplier.apply()
def testVariableIsolator(self): eq = EquationBuilder().left_constant(1).left_variable( 'x', 2, 1).right_variable('x', 1, 2).build() eq.apply_operation(Equation.VariableIsolator('x')) isolated_eq = EquationBuilder().left_variable( 'x', 1, 1).right_constant_fraction(-2, 3).build() self.assertEqual(eq, isolated_eq)
def testVariableIsolatorWithMoreVariables(self): eq = EquationBuilder().left_constant(1).left_variable( 'x', 2, 1).left_variable('y', 1, 1).right_variable('x', 1, 1).build() eq.apply_operation(Equation.VariableIsolator('x')) isolated_eq = EquationBuilder().left_variable( 'x', 1, 1).right_constant(-1).right_variable('y', -1, 1).build() self.assertEqual(eq, isolated_eq)
def testEquationSimplifyer(self): eq = EquationBuilder().left_constant(1).left_variable( 'x', 2, 1).left_variable('x', 1, 2).right_default_constant().build() eq.apply_operation(Equation.EquationSimplifyer()) simplified_eq = EquationBuilder().left_constant(1).left_variable( 'x', 5, 2).right_default_constant().build() self.assertEqual(eq, simplified_eq)
def _resolve_recursive(self): equation_system_to_recurse = self._equation_system.clon() equation_system_to_recurse.remove(self._equation_to_resolve) equation_system_to_recurse.apply_operation( EquationListOperationApplier( Equation.EquationApplier(self._variable_to_reduce, self._equation_to_resolve))) self._solution_method_recurse = SubstitutionMethod() self._solution_method_recurse.set(equation_system_to_recurse) self._solution_method_recurse.resolve()
def tesEquationApplier(self): # 1 + 2x + y = x eq = EquationBuilder().left_constant(1).left_variable( 'x', 2, 1).left_variable('y', 1, 1).right_variable('x', 1, 1).build() # 1 +x -y = y +1 -> x = y + y -> x = 2y eq2 = EquationBuilder().left_constant(1).left_variable( 'x', 1, 1).left_variable('y', -1, 1).right_variable( 'y', 1, 1).right_constant_fraction(1, 1).build() # eq: 1 + 4y + y = 2y eq.apply_operation(Equation.EquationApplier('x', eq2)) resultant_eq = EquationBuilder().left_constant(1).left_variable( 'y', 4, 1).left_variable('y', 1, 1).right_variable('y', 2, 1).build() self.assertEqual(eq, resultant_eq)
def _compute_solution(self): self._equation_to_resolve.apply_operation( Equation.VariableIsolator(self._variable_to_reduce)) self.set_solution(self._variable_to_reduce, self._equation_to_resolve)
def __init__(self): EquationListOperationApplier.__init__(self, Equation.EquationSimplifyer())
def _reduce_all_equations(self, equation_system_to_recurse): self._equation_to_reduce.multiply(-1.0) equation_system_to_recurse.apply_operation( EquationListOperationApplier( Equation.EquationAdder(self._equation_to_reduce)))
def _preprocess_input_equation_system(self): self._equation_system.apply_operation( EquationListOperationApplier( Equation.VariableMover(self._variable_to_reduce, Side.left))) self._equation_system.apply_operation(EquationListSimplify())
def build(self): return Equation(left_expression=self._left_expression, right_expression=self._right_expression)
def _apply_multiplication_values(self): for i in range(len(self._equation_list)): self._equation_list[i].apply_operation( Equation.ValueMultiplier(self._multiplication_values[i]))
def _normalize(self): normalizer = EquationListOperationApplier(Equation.Normalizer()) normalizer.set_equation_list(self._equation_list) normalizer.apply()