1#!/usr/bin/env python
2
3import polypy
4import polypy_test
5
6import random
7import itertools
8import functools
9
10from polypy import x
11
12def check_comparison(a1, a2, cmp, result, expected):
13    if (result != expected):
14        print("a1 = {0}".format(a1))
15        print("a2 = {0}".format(a2))
16        print("cmp = {0}".format(cmp))
17        print("result = {0}".format(result))
18        print("expected = {0}".format(expected))
19        polypy_test.check(False)
20    else:
21        polypy_test.check(True)
22
23polypy_test.init()
24polypy_test.start("Construction and order")
25
26a1 = polypy.AlgebraicNumber((x**2 - 2)*(x - 10), 1)
27a2 = polypy.AlgebraicNumber((x**2 - 2), 1)
28
29result = (a1 < a2)
30check_comparison(a1, a2, "<", result, False)
31
32result = (a1 == a2)
33check_comparison(a1, a2, "==", result, True)
34
35p = x**2
36zero = polypy.AlgebraicNumber(p, 0)
37result = zero.to_double() == 0.0
38check_comparison(zero, 0, ".to_double ==", result, True)
39
40p = x**2 - 1
41one_neg = polypy.AlgebraicNumber(p, 0)
42one_pos = polypy.AlgebraicNumber(p, 1)
43result = (one_neg < one_pos)
44check_comparison(one_neg, one_pos, "<", result, True)
45
46p = x**2 - 2
47sqrt2_neg = polypy.AlgebraicNumber(p, 0)
48sqrt2_pos = polypy.AlgebraicNumber(p, 1)
49result = (sqrt2_neg < sqrt2_pos)
50check_comparison(sqrt2_neg, sqrt2_pos, "<", result, True)
51
52p = x**2 - 3
53sqrt3_neg = polypy.AlgebraicNumber(p, 0)
54sqrt3_pos = polypy.AlgebraicNumber(p, 1)
55result = (sqrt3_neg < sqrt3_pos)
56check_comparison(sqrt3_neg, sqrt3_pos, "<", result, True)
57
58a = [sqrt3_pos, zero, sqrt3_neg, one_neg, sqrt2_pos, one_pos, sqrt2_neg]
59a_sorted = [sqrt3_neg, sqrt2_neg, one_neg, zero, one_pos, sqrt2_pos, sqrt3_pos]
60a.sort()
61
62result = (a == a_sorted)
63check_comparison(a, a_sorted, "==", result, True)
64
65a = []
66a_sorted = []
67for n in range(1, 10):
68    p = x**2 - n
69    neg = polypy.AlgebraicNumber(p, 0)
70    pos = polypy.AlgebraicNumber(p, 1)
71    a.append(pos)
72    a.append(neg)
73    a_sorted.append(pos)
74    a_sorted.insert(0, neg)
75a.sort()
76check_comparison(a, a_sorted, "==", result, True)
77
78p1 = (x**2 - 2)*x        # -sqrt(2), 0, sqrt(2)
79p2 = (x**2 - 2)*(x-1)    # -sqrt(2), 1, sqrt(2)
80p3 = (x**2 - 2)*(x+2)    # -sqrt(2), -1, sqrt(2)
81
82a1 = polypy.AlgebraicNumber(p1, 2)
83a2 = polypy.AlgebraicNumber(p2, 2)
84a3 = polypy.AlgebraicNumber(p3, 2)
85
86check_comparison(a1, a2, "==", (a1 == a2), True)
87check_comparison(a1, a3, "==", (a1 == a3), True)
88check_comparison(a2, a3, "==", (a2 == a3), True)
89
90polypy_test.start("Arithmetic")
91
92# polypy.trace_enable("coefficient")
93# polypy.trace_enable("coefficient::resultant")
94# polypy.trace_enable("algebraic_number")
95
96x = polypy.x
97
98a = polypy.AlgebraicNumber(x**3 - 3, 0)
99a_neg = -a
100check_comparison(a_neg, a, "a", (a_neg < a), True)
101sum = a + a_neg
102check_comparison(sum, zero, "==", (sum == zero), True)
103
104a = polypy.AlgebraicNumber(x**2 - 2, 1)
105a2 = polypy.AlgebraicNumber(x**2 - 4, 1)
106
107mul = a * a
108check_comparison(mul, a2, "==", (mul == a2), True)
109
110a_pow = a**2
111check_comparison(a_pow, a2, "==", (a_pow == a2), True)
112
113b = polypy.AlgebraicNumber(x**2 - 3, 1)
114ab = polypy.AlgebraicNumber(x**2 - 6, 1)
115mul = a * b
116check_comparison(mul, ab, "==", (mul == ab), True)
117
118a = polypy.AlgebraicNumber(x**2 - 2, 0)
119b = polypy.AlgebraicNumber(x**2 - 2, 1)
120add = a + b
121check_comparison(add, zero, "==", (add == zero), True)
122
123a = polypy.AlgebraicNumber(x**2 - 2, 1)
124b = polypy.AlgebraicNumber(x**2 - 3, 1)
125ab = polypy.AlgebraicNumber(x**4 + -10*x**2 + 1, 3)
126add = a + b
127check_comparison(add, ab, "==", (add == ab), True)
128
129polypy_test.start("Division")
130
131one = polypy.AlgebraicNumber(x-1, 0)
132
133a_list = [sqrt3_pos, sqrt3_neg, sqrt2_pos, sqrt2_neg]
134z_list = [polypy.AlgebraicNumber(x - k, 0) for k in [-3, -1, 1, 3]]
135q_list = [polypy.AlgebraicNumber(2*x - k, 0) for k in [-3, -1, 1, 3]]
136numbers = a_list + z_list + q_list
137sum_list = [a + b for (a, b) in itertools.product(numbers, numbers)]
138todo = [a for a in (numbers + sum_list) if a != zero]
139
140for k in range(1, 500):
141    sample = random.sample(todo, 3)
142    random.shuffle(sample)
143    p = functools.reduce(lambda x, y: x*y, sample, one)
144    random.shuffle(sample)
145    p = functools.reduce(lambda x, y: x/y, sample, p)
146    polypy_test.check(p == 1)
147