add UnicodeEngine (MultiEngineText and axis texters returning MultiEngineText), texte...
[PyX.git] / test / unit / test_mathutils.py
blobe5d9f043b5f45d706b150fd67edcad5cb4f0d72a
1 import sys
2 if sys.path[0] != "../..":
3 sys.path.insert(0, "../..")
5 import unittest
7 from pyx import mathutils
9 class PolynomTestCase(unittest.TestCase):
11 def makePolyRoots(self, *rs):
12 # creates a polynom with zeros rs
13 if len(rs) == 1:
14 return [1, -rs[0]]
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):
24 found.sort()
25 # we remove degeneracies in found, since we do *not* claim to properly handle degeneracies
26 i = 1
27 while len(found) > i:
28 if found[i] - found[i-1] < 1e-7:
29 del found[i]
30 else:
31 i += 1
32 self.assertEqual(len(found), len(should))
33 for r1, r2 in zip(found, should):
34 self.assertAlmostEqual(r1, r2)
36 def testConstant(self):
37 self.compareRoots(mathutils.realpolyroots(1), [])
38 self.compareRoots(mathutils.realpolyroots(0), [0])
40 def testLinear(self):
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])
61 def testCubic(self):
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__":
91 unittest.main()