示例#1
0
    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)
示例#2
0
    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')
示例#3
0
    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')
示例#4
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)
示例#5
0
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)
示例#6
0
    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)
示例#7
0
    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)
示例#8
0
    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)