2 if sys
.path
[0] != "../..":
3 sys
.path
.insert(0, "../..")
7 from pyx
import mathutils
9 class PolynomTestCase(unittest
.TestCase
):
11 def makePolyRoots(self
, *rs
):
12 # creates a polynom with zeros rs
15 cs
= self
.makePolyRoots(*rs
[1:])
16 return [cim1
-rs
[0]*ci
for cim1
, ci
in zip(cs
+[0], [0]+cs
)]
18 def makePolyNonroots(self
, cs
, a
, b
):
19 # adds a factor ((x-a)**2 + b) to the polynom with coefficients cs
20 # which can be used (for b > 0) to construct polynoms with complex roots
21 return [cim2
-2*a
*cim1
+(a
*a
+b
)*ci
for cim2
, cim1
, ci
in zip(cs
+[0, 0], [0]+cs
+[0], [0, 0]+cs
)]
23 def compareRoots(self
, found
, should
):
25 # we remove degeneracies in found, since we do *not* claim to properly handle degeneracies
28 if found
[i
] - found
[i
-1] < 1e-7:
32 self
.failUnlessEqual(len(found
), len(should
))
33 for r1
, r2
in zip(found
, should
):
34 self
.failUnlessAlmostEqual(r1
, r2
)
36 def testConstant(self
):
37 self
.compareRoots(mathutils
.realpolyroots(1), [])
38 self
.compareRoots(mathutils
.realpolyroots(0), [0])
41 self
.compareRoots(mathutils
.realpolyroots(0, 1), [])
42 self
.compareRoots(mathutils
.realpolyroots(0, 0), [0])
43 self
.compareRoots(mathutils
.realpolyroots(1, 1), [-1])
44 self
.compareRoots(mathutils
.realpolyroots(2, 0), [0])
45 self
.compareRoots(mathutils
.realpolyroots(4, -1), [0.25])
46 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyRoots(1.7)), [1.7])
47 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyRoots(-42)), [-42])
49 def testQuadratic(self
):
50 self
.compareRoots(mathutils
.realpolyroots(0, 1, 1), [-1])
51 self
.compareRoots(mathutils
.realpolyroots(1, -4, 3), [1, 3])
52 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyRoots(1.7, 2.3)), [1.7, 2.3])
53 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyRoots(2.3, 1.7)), [1.7, 2.3])
54 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyRoots(1.7, -2.3)), [-2.3, 1.7])
55 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyRoots(-2.3, 1.7)), [-2.3, 1.7])
56 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyRoots(1.7, 1.7)), [1.7])
57 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyNonroots([1], 1.7, 1)), [])
58 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyNonroots([1], 1.7, -1)), [0.7, 2.7])
59 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyNonroots([1], 1.7, -4)), [-0.3, 3.7])
62 self
.compareRoots(mathutils
.realpolyroots(0, 0, 0, 1), [])
63 self
.compareRoots(mathutils
.realpolyroots(0, 0, 1, 1), [-1])
64 self
.compareRoots(mathutils
.realpolyroots(0, 1, -4, 3), [1, 3])
65 self
.compareRoots(mathutils
.realpolyroots(1, -9, 23, -15), [1, 3, 5])
66 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyRoots(1.7, 2.3, 4.2)), [1.7, 2.3, 4.2])
67 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyRoots(1.7, -2.3, 4.2)), [-2.3, 1.7, 4.2])
68 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyRoots(1.7, -2.3, -4.2)), [-4.2, -2.3, 1.7])
69 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyNonroots(self
.makePolyRoots(1.7), 2.3, 1)), [1.7])
70 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyNonroots(self
.makePolyRoots(1.7), 2.3, -1)), [1.3, 1.7, 3.3])
71 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyNonroots(self
.makePolyRoots(1.7), 2.3, -4)), [0.3, 1.7, 4.3])
73 def testQuartic(self
):
74 self
.compareRoots(mathutils
.realpolyroots(0, 0, 0, 0, 1), [])
75 self
.compareRoots(mathutils
.realpolyroots(0, 0, 0, 1, 1), [-1])
76 self
.compareRoots(mathutils
.realpolyroots(0, 1, -9, 23, -15), [1, 3, 5])
77 self
.compareRoots(mathutils
.realpolyroots(1, -16, 86, -176, 105), [1, 3, 5, 7])
78 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyRoots(1.7, 2.3, 4.2, 13)), [1.7, 2.3, 4.2, 13])
79 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyRoots(1.7, -2.3, 4.2, 13)), [-2.3, 1.7, 4.2, 13])
80 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyRoots(1.7, -2.3, -4.2, 13)), [-4.2, -2.3, 1.7, 13])
81 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyNonroots(self
.makePolyRoots(1.7, 2.3), 4.2, 1)), [1.7, 2.3])
82 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyNonroots(self
.makePolyRoots(1.7, 2.3), 4.2, -1)), [1.7, 2.3, 3.2, 5.2])
83 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyNonroots(self
.makePolyRoots(1.7, 2.3), 4.2, -4)), [1.7, 2.2, 2.3, 6.2])
84 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyNonroots(self
.makePolyRoots(1.7, -2.3), 4.2, -1)), [-2.3, 1.7, 3.2, 5.2])
85 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyNonroots(self
.makePolyRoots(1.7, -2.3), 4.2, -4)), [-2.3, 1.7, 2.2, 6.2])
86 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyNonroots(self
.makePolyNonroots([1], 1.7, 1), 2.3, 1)), [])
87 self
.compareRoots(mathutils
.realpolyroots(*self
.makePolyNonroots(self
.makePolyNonroots([1], 1.7, 1), 2.3, -1)), [1.3, 3.3])
90 if __name__
== "__main__":