def test_single_variable_solver_handles_distribution(self): equation = Parser.parse_equation('p * -25 + (1 - p) * 5 = 0') result = Solver.single_variable(equation, 'p', print_out=True, max_iterations=20) verify(str(result), self.reporter)
def test_single_variable_solver_handles_same_variable(self): equation = Parser.parse_equation('x + x = 3') result = Solver.single_variable(equation, 'x', print_out=True, max_iterations=5) self.assertEqual(str(result), 'x = 1.5')
def test_single_variable_solver_handles_same_variable_complex(self): equation = Parser.parse_equation('2*x - 7 = 4*x + 5') result = Solver.single_variable(equation, 'x', print_out=True, max_iterations=20) self.assertEqual(str(result), 'x = -6.0')
def test_single_variable_solver_evaluates(self): equation = Parser.parse_equation('0 = 0.5*a + 3*4') result = Solver.single_variable(equation, 'a', print_out=True, max_iterations=5) verify(str(result), self.reporter)
def solve_equation(input): assert type(input) == str if Parser.is_equation(input): equation = Parser.parse_equation(input) variables = equation.get_variables() if len(variables) != 1: raise InputException( 'Equation should have exactly 1 variable but instead has {}: {}' .format(str(len(variables)), variables)) result = Solver.single_variable(equation, variables[0]) else: expression = Parser.parse_expression(input) variables = expression.get_variables() if len(variables) != 0: raise InputException( 'Expression should have no variables but instead has the following: {}' .format(variables)) result = expression.evaluate() print(result)
def test_single_variable_solver_simple(self): equation = Parser.parse_equation('y*0.5 - 2*x = 9') result = Solver.single_variable(equation, 'y', print_out=True) verify(str(result), self.reporter)
def test_single_variable_solver_flip(self): equation = Parser.parse_equation('-3.1415 = p') result = Solver.single_variable(equation, 'p') verify(str(result), self.reporter)
def test_single_variable_solver_trivial(self): equation = Equation(Operation(Variable('x')), Operation(Variable(2))) result = Solver.single_variable(equation, 'x') verify(str(result), self.reporter)