1import polypy
2import polypy_test
3
4from polypy import x
5from itertools import combinations_with_replacement
6
7polypy_test.init()
8
9def check_comparison(x, expected, precision = 0.0000001):
10    diff = x.to_double() - expected
11    if (diff < -precision or diff > precision):
12        print("x = {0}".format(x))
13        print("expected = {0}".format(expected))
14        print("precision = {0}".format(precision))
15        polypy_test.check(False)
16    else:
17        polypy_test.check(True)
18
19polypy_test.start("Construction")
20
21zero_int = polypy.Value(0)
22one_int = polypy.Value(1)
23two_int = polypy.Value(2)
24three_int = polypy.Value(3)
25
26check_comparison(zero_int, 0)
27check_comparison(one_int, 1)
28check_comparison(two_int, 2)
29check_comparison(three_int, 3)
30
31p = x**2 - 2
32sqrt2_neg = polypy.Value(polypy.AlgebraicNumber(p, 0))
33sqrt2_pos = polypy.Value(polypy.AlgebraicNumber(p, 1))
34
35check_comparison(sqrt2_neg, -1.41421356237)
36check_comparison(sqrt2_pos, 1.41421356237)
37
38p = x**2 - 3
39sqrt3_neg = polypy.Value(polypy.AlgebraicNumber(p, 0))
40sqrt3_pos = polypy.Value(polypy.AlgebraicNumber(p, 1))
41
42check_comparison(sqrt3_neg, -1.73205080757)
43check_comparison(sqrt3_pos, 1.73205080757)
44
45half_rat = one_int / two_int
46third_rat = one_int / three_int
47
48check_comparison(half_rat, 0.5)
49check_comparison(third_rat, 0.333333333333)
50
51values = [zero_int, one_int, two_int, three_int, half_rat, third_rat, sqrt2_pos, sqrt2_neg, sqrt3_pos, sqrt3_neg]
52
53polypy_test.start("Addition")
54
55for v1, v2 in combinations_with_replacement(values, 2):
56    result_value = v1 + v2
57    result_float = v1.to_double() + v2.to_double()
58    check_comparison(result_value, result_float)
59
60polypy_test.start("Subtraction")
61
62for v1, v2 in combinations_with_replacement(values, 2):
63    result_value = v1 - v2
64    result_float = v1.to_double() - v2.to_double()
65    check_comparison(result_value, result_float)
66
67polypy_test.start("Multiplication")
68
69for v1, v2 in combinations_with_replacement(values, 2):
70    result_value = v1 * v2
71    result_float = v1.to_double() * v2.to_double()
72    check_comparison(result_value, result_float)
73
74polypy_test.start("Division")
75
76for v1, v2 in combinations_with_replacement(values, 2):
77    if v2 == zero_int:
78        continue
79    result_value = v1 / v2
80    result_float = v1.to_double() / v2.to_double()
81    check_comparison(result_value, result_float)
82
83polypy_test.start("Power")
84
85for v in values:
86    for p in range(0, 10):
87        result_value = v**p
88        result_float = v.to_double() ** p
89        check_comparison(result_value, result_float)
90
91