1#!/usr/bin/env python
2
3import polypy
4import polypy_test
5
6polypy_test.init()
7
8def check_roots_isolate(p, expected):
9    roots = p.roots_isolate()
10    sorted = all(roots[i] < roots[i+1] for i in range(len(roots)-1))
11    count = len(roots)
12    if ((not sorted) or count != expected):
13        print("p = {0}".format(p))
14        print("sorted = {0}".format(sorted))
15        print("count = {0}".format(count))
16        print("expected = {0}".format(expected))
17        polypy_test.check(False)
18    else:
19        polypy_test.check(True)
20
21def check_roots_count(p, expected, lb = None, ub = None):
22    if (lb is None):
23        count = p.roots_count()
24    else:
25        count = p.roots_count(lb, ub)
26
27    if (count != expected):
28        polypy_test.check(False)
29        print("p = {0}".format(p))
30        print("lb = {0}".format(lb))
31        print("ub = {0}".format(ub))
32        print("count = {0}".format(count))
33        print("expected = {0}".format(expected))
34    else:
35        polypy_test.check(True)
36
37# polypy.trace_enable("roots")
38# polypy.trace_enable("algebraic_number")
39# polypy.trace_enable("division")
40# polypy.trace_enable("sturm_sequence_check")
41
42x = polypy.x
43
44polypy_test.start("Root isolation")
45
46p = x**2 - 1
47check_roots_isolate(p, 2)
48
49p = (x - 1)*(x + 1)*(x - 2)
50check_roots_isolate(p, 3)
51
52p = 1*x**5 + (-23*x**4) + (-57*x**3) + 85*x**2 + 64*x + (-63)
53check_roots_isolate(p, 5)
54
55p = 41*x**5 + (-79*x**4) + 44*x**3 + 56*x**2 + (-10*x) + 41
56check_roots_isolate(p, 1)
57
58p = 9*x**13 - 18*x**11 - 33*x**10 + 102*x**8 + 7*x**7 - 36*x**6 - 122*x**5 + 49*x**4 + 93*x**3 - 42*x**2 - 18*x** + 9
59check_roots_isolate(p, 6)
60
61polypy_test.start("Root counting")
62
63p = x + 1 - x
64check_roots_count(p, 0)
65check_roots_count(p, 0, -0.5, 0.5)
66check_roots_count(p, 0, -1, 1)
67check_roots_count(p, 0, -1.5, 1.5)
68check_roots_count(p, 0, -2, 2)
69check_roots_count(p, 0, -2.5, 2.5)
70check_roots_count(p, 0, -3, 3)
71
72p = (x - 1)
73check_roots_count(p, 1)
74check_roots_count(p, 0, -0.5, 0.5)
75check_roots_count(p, 0, -1, 1)
76check_roots_count(p, 1, -1.5, 1.5)
77check_roots_count(p, 1, -2, 2)
78check_roots_count(p, 1, -2.5, 2.5)
79check_roots_count(p, 1, -3, 3)
80
81p = (x - 1)*(x + 1)
82check_roots_count(p, 2)
83check_roots_count(p, 0, -0.5, 0.5)
84check_roots_count(p, 0, -1, 1)
85check_roots_count(p, 2, -1.5, 1.5)
86check_roots_count(p, 2, -2, 2)
87check_roots_count(p, 2, -2.5, 2.5)
88check_roots_count(p, 2, -3, 3)
89
90p = (x - 1)*(x + 1)*(x - 2)
91check_roots_count(p, 3)
92check_roots_count(p, 0, -0.5, 0.5)
93check_roots_count(p, 0, -1, 1)
94check_roots_count(p, 2, -1.5, 1.5)
95check_roots_count(p, 2, -2, 2)
96check_roots_count(p, 3, -2.5, 2.5)
97check_roots_count(p, 3, -3, 3)
98
99p = (x - 1)*(x + 1)*(x - 2)*(x + 2)
100check_roots_count(p, 4)
101check_roots_count(p, 0, -0.5, 0.5)
102check_roots_count(p, 0, -1, 1)
103check_roots_count(p, 2, -1.5, 1.5)
104check_roots_count(p, 2, -2, 2)
105check_roots_count(p, 4, -2.5, 2.5)
106check_roots_count(p, 4, -3, 3)
107
108p = (x - 1)**3 * (x + 1)** 2 * (x - 2)**2 * (x + 2)
109check_roots_count(p, 4)
110check_roots_count(p, 0, -0.5, 0.5)
111check_roots_count(p, 0, -1, 1)
112check_roots_count(p, 2, -1.5, 1.5)
113check_roots_count(p, 2, -2, 2)
114check_roots_count(p, 4, -2.5, 2.5)
115check_roots_count(p, 4, -3, 3)
116