コード例 #1
0
    def test_solve_from_derivative_roots(self):
        epsilon = 0.00001
        n_digits = convert_from_epsilon_to_n_digit(epsilon)

        polynomial = parse_to_polynomial("x^3+x")
        derivative_roots = []
        roots = solve_from_derivative_roots(polynomial, epsilon,
                                            derivative_roots)
        expected_roots = [0]
        for index in range(len(roots)):
            self.assertEqual(round(roots[index], n_digits),
                             expected_roots[index])

        polynomial = parse_to_polynomial("x^2-6*x+1")
        derivative_roots = [3]
        roots = solve_from_derivative_roots(polynomial, epsilon,
                                            derivative_roots)
        expected_roots = [0.1716, 5.8284]
        for index in range(len(roots)):
            self.assertEqual(round(roots[index], n_digits),
                             expected_roots[index])

        polynomial = parse_to_polynomial("x^3-3*x^2+2*x-10")
        derivative_roots = [0.42, 1.57]
        roots = solve_from_derivative_roots(polynomial, epsilon,
                                            derivative_roots)
        expected_roots = [3.3089]
        for index in range(len(roots)):
            self.assertEqual(round(roots[index], n_digits),
                             expected_roots[index])
コード例 #2
0
 def test_get_upper_bound_with_opposite_sign(self):
     self.assertEqual(
         get_upper_bound_with_opposite_sign(parse_to_polynomial("x^3"), 1),
         None)
     self.assertEqual(
         get_upper_bound_with_opposite_sign(parse_to_polynomial("x^3"),
                                            -10), 5)
コード例 #3
0
 def test_get_lower_bound_with_opposite_sign(self):
     self.assertEqual(
         get_lower_bound_with_opposite_sign(parse_to_polynomial("x^3"), 1),
         0)
     self.assertEqual(
         get_lower_bound_with_opposite_sign(parse_to_polynomial("x^2+9"),
                                            -2), None)
コード例 #4
0
    def test_bisect(self):
        epsilon = 0.00001
        n_digits = convert_from_epsilon_to_n_digit(epsilon)

        polynomial = parse_to_polynomial("x^3/3-x")
        root = find_root_using_bisection(polynomial, epsilon, 1, 10)
        self.assertEqual(round(root, n_digits), 1.7321)

        polynomial = parse_to_polynomial("x^2-x-2")
        root = find_root_using_bisection(polynomial, epsilon, -100, 0)
        self.assertEqual(round(root, n_digits), -1)

        polynomial = parse_to_polynomial("x^2-x-2")
        root = find_root_using_bisection(polynomial, epsilon, -100, -10)
        self.assertEqual(root, None)
コード例 #5
0
def parse_and_solve_and_round(expression, epsilon):
    if expression.find("=") < 0:
        roots = solve_equation(parse_to_polynomial(expression), epsilon)
    else:
        if expression.endswith("=0"):
            roots = solve_equation(
                parse_to_polynomial(expression[0:len(expression) - 2]),
                epsilon)
        else:
            index_of_equal = expression.find("=")
            a = parse_to_polynomial(expression[0:index_of_equal])
            b = parse_to_polynomial(expression[index_of_equal + 1:])
            roots = solve_equation(a.minus(b), epsilon)

    if roots == ["Infinite roots"]:
        return roots
    n_digits = convert_from_epsilon_to_n_digit(epsilon)
    return list(map(lambda root: round(root, n_digits), roots))
コード例 #6
0
    def test_solve_equation(self):
        epsilon = 0.0001

        polynomial = parse_to_polynomial("0*x+6")
        roots = solve_equation(polynomial, epsilon)
        self.assertEqual(roots, [])

        polynomial = parse_to_polynomial("0*x+0")
        roots = solve_equation(polynomial, epsilon)
        self.assertEqual(roots, ["Infinite roots"])

        polynomial = parse_to_polynomial("11*x+6")
        roots = solve_equation(polynomial, epsilon)
        expected_roots = [-6 / 11]
        self.assertEqual(len(roots), len(expected_roots))
        for index in range(len(roots)):
            self.assertEqual(roots[index], expected_roots[index])

        polynomial = parse_to_polynomial("6*x^2+11*x+6")
        roots = solve_equation(polynomial, epsilon)
        expected_roots = []
        self.assertEqual(len(roots), len(expected_roots))
        for index in range(len(roots)):
            self.assertEqual(roots[index], expected_roots[index])

        polynomial = parse_to_polynomial("x^3+6*x^2+11*x+6")
        roots = solve_equation(polynomial, epsilon)
        expected_roots = [-3, -2, -1]
        self.assertEqual(len(roots), len(expected_roots))
        for index in range(len(roots)):
            self.assertEqual(roots[index], expected_roots[index])

        polynomial = parse_to_polynomial("x^2-1").multiply(
            parse_to_polynomial("x^2-4"))
        roots = solve_equation(polynomial, epsilon)
        expected_roots = [-2, -1, 1, 2]
        self.assertEqual(len(roots), len(expected_roots))
        for index in range(len(roots)):
            self.assertEqual(roots[index], expected_roots[index])