matrix class
[PyX/mjg.git] / test / experimental / test_solve.py
blob28a9873851539cb43d2d23cf8bf48abdd91c015e
1 import unittest, operator
3 from solve import scalar, vector, matrix, solver
5 class ScalarTestCase(unittest.TestCase):
7 def testInit(self):
8 self.failUnlessRaises(RuntimeError, scalar, "")
9 self.failUnlessRaises(RuntimeError, scalar, 1j)
10 self.failUnlessEqual(str(scalar()), "unnamed_scalar")
11 self.failUnlessEqual(str(scalar(name="s")), "s")
12 self.failUnlessEqual(str(scalar(1)), "unnamed_scalar{=1.0}")
13 self.failUnlessEqual(str(scalar(-1, name="s")), "s{=-1.0}")
15 def testMath(self):
16 self.failUnlessEqual(str(-scalar(name="s")), "unnamed_scalar{=-1.0} * s")
17 self.failUnlessEqual(str(scalar(name="s") + scalar(name="t")), "s + t")
18 self.failUnlessEqual(str(scalar(name="s") + 1), "s + unnamed_scalar{=1.0}")
19 self.failUnlessEqual(str(1 + scalar(name="s")), "s + unnamed_scalar{=1.0}")
20 self.failUnlessEqual(str(scalar(name="s") - scalar(name="t")), "unnamed_scalar{=-1.0} * t + s")
21 self.failUnlessEqual(str(1 - scalar(name="s")), "unnamed_scalar{=-1.0} * s + unnamed_scalar{=1.0}")
22 self.failUnlessEqual(str(2 * scalar(name="s")), "s * unnamed_scalar{=2.0}")
23 self.failUnlessEqual(str(scalar(name="s") * 2), "s * unnamed_scalar{=2.0}")
24 self.failUnlessEqual(str(scalar(name="s") * scalar(name="t")), "s * t")
25 self.failUnlessEqual(str((scalar(name="s") + scalar(name="t")) * 2), "s * unnamed_scalar{=2.0} + t * unnamed_scalar{=2.0}")
26 self.failUnlessEqual(str(scalar(name="s") / 2.0), "unnamed_scalar{=0.5} * s")
27 self.failUnlessEqual(str(scalar(name="s") / 2), "unnamed_scalar{=0.0} * s") # integer logic!
28 self.failUnlessEqual(str((scalar(name="s") + scalar(name="t")) / 2.0), "unnamed_scalar{=0.5} * s + unnamed_scalar{=0.5} * t")
29 self.failUnlessRaises(TypeError, lambda: 2 / scalar())
30 self.failUnlessRaises(TypeError, lambda: scalar() / scalar())
31 self.failUnlessRaises(TypeError, lambda: vector(1) / scalar())
32 self.failUnlessRaises(TypeError, lambda: ((scalar() + scalar()) / scalar()))
33 self.failUnlessRaises(TypeError, lambda: (vector(1) + vector(1)) / scalar())
35 def testAccess(self):
36 s = scalar()
37 self.failUnlessEqual(s.is_set(), 0)
38 self.failUnlessRaises(RuntimeError, s.get)
39 self.failUnlessRaises(RuntimeError, float, s)
40 s.set(2)
41 self.failUnlessEqual(s.is_set(), 1)
42 self.failUnlessAlmostEqual(s.get(), 2.0)
43 self.failUnlessAlmostEqual(float(s), 2.0)
44 self.failUnlessRaises(RuntimeError, s.set, 3)
45 self.failUnlessEqual(s.is_set(), 1)
46 self.failUnlessAlmostEqual(s.get(), 2.0)
47 self.failUnlessAlmostEqual(float(s), 2.0)
49 s = scalar(2)
50 self.failUnlessEqual(scalar(2).is_set(), 1)
51 self.failUnlessAlmostEqual(s.get(), 2.0)
52 self.failUnlessAlmostEqual(float(s), 2.0)
53 self.failUnlessRaises(RuntimeError, s.set, 3)
54 self.failUnlessEqual(s.is_set(), 1)
55 self.failUnlessAlmostEqual(s.get(), 2.0)
56 self.failUnlessAlmostEqual(float(s), 2.0)
59 class VectorTestCase(unittest.TestCase):
61 def testInit(self):
62 self.failUnlessRaises(RuntimeError, vector, 0, 0)
63 self.failUnlessEqual(str(vector(2)), "unnamed_vector{=(unnamed_vector[0], unnamed_vector[1])}")
64 self.failUnlessEqual(str(vector([1, 2])), "unnamed_vector{=(unnamed_vector[0]{=1.0}, unnamed_vector[1]{=2.0})}")
65 self.failUnlessEqual(str(vector(3, "a")), "a{=(a[0], a[1], a[2])}")
66 self.failUnlessEqual(str(vector([3, 2, 1], "a")), "a{=(a[0]{=3.0}, a[1]{=2.0}, a[2]{=1.0})}")
68 def testAccess(self):
69 a = vector(2)
70 self.failUnlessEqual(str(a), "unnamed_vector{=(unnamed_vector[0], unnamed_vector[1])}")
71 self.failUnlessEqual(a[0].is_set(), 0)
72 self.failUnlessEqual(a.x.is_set(), 0)
73 self.failUnlessEqual(a[1].is_set(), 0)
74 self.failUnlessEqual(a.y.is_set(), 0)
75 self.failUnlessRaises(IndexError, operator.__getitem__, a, 2)
76 self.failUnlessRaises(IndexError, getattr, a, "z")
78 a[0].set(2)
79 self.failUnlessEqual(str(a), "unnamed_vector{=(unnamed_vector[0]{=2.0}, unnamed_vector[1])}")
80 self.failUnlessAlmostEqual(a[0].get(), 2.0)
81 self.failUnlessAlmostEqual(float(a[0]), 2.0)
82 self.failUnlessAlmostEqual(a.x.get(), 2.0)
83 self.failUnlessAlmostEqual(float(a.x), 2.0)
84 self.failUnlessRaises(RuntimeError, a[1].get)
85 self.failUnlessRaises(RuntimeError, float, a[1])
86 self.failUnlessRaises(RuntimeError, a.y.get)
87 self.failUnlessRaises(RuntimeError, float, a.y)
88 self.failUnlessEqual(a[0].is_set(), 1)
89 self.failUnlessEqual(a.x.is_set(), 1)
90 self.failUnlessEqual(a[1].is_set(), 0)
91 self.failUnlessEqual(a.y.is_set(), 0)
92 self.failUnlessRaises(RuntimeError, a[0].set, 3)
93 self.failUnlessAlmostEqual(a[0].get(), 2.0)
94 self.failUnlessAlmostEqual(float(a[0]), 2.0)
95 self.failUnlessAlmostEqual(a.x.get(), 2.0)
96 self.failUnlessAlmostEqual(float(a.x), 2.0)
97 self.failUnlessRaises(RuntimeError, a[1].get)
98 self.failUnlessRaises(RuntimeError, float, a[1])
99 self.failUnlessRaises(RuntimeError, a.y.get)
100 self.failUnlessRaises(RuntimeError, float, a.y)
101 self.failUnlessEqual(a[0].is_set(), 1)
102 self.failUnlessEqual(a.x.is_set(), 1)
103 self.failUnlessEqual(a[1].is_set(), 0)
104 self.failUnlessEqual(a.y.is_set(), 0)
106 a[1].set(3)
107 self.failUnlessEqual(str(a), "unnamed_vector{=(unnamed_vector[0]{=2.0}, unnamed_vector[1]{=3.0})}")
108 self.failUnlessAlmostEqual(a[0].get(), 2.0)
109 self.failUnlessAlmostEqual(float(a[0]), 2.0)
110 self.failUnlessAlmostEqual(a.x.get(), 2.0)
111 self.failUnlessAlmostEqual(float(a.x), 2.0)
112 self.failUnlessAlmostEqual(a[1].get(), 3.0)
113 self.failUnlessAlmostEqual(float(a[1]), 3.0)
114 self.failUnlessAlmostEqual(a.y.get(), 3.0)
115 self.failUnlessAlmostEqual(float(a.y), 3.0)
116 self.failUnlessEqual(a[0].is_set(), 1)
117 self.failUnlessEqual(a.x.is_set(), 1)
118 self.failUnlessEqual(a[1].is_set(), 1)
119 self.failUnlessEqual(a.y.is_set(), 1)
120 self.failUnlessRaises(RuntimeError, a[0].set, 4)
121 self.failUnlessAlmostEqual(a[0].get(), 2.0)
122 self.failUnlessAlmostEqual(float(a[0]), 2.0)
123 self.failUnlessAlmostEqual(a.x.get(), 2.0)
124 self.failUnlessAlmostEqual(float(a.x), 2.0)
125 self.failUnlessAlmostEqual(a[1].get(), 3.0)
126 self.failUnlessAlmostEqual(float(a[1]), 3.0)
127 self.failUnlessAlmostEqual(a.y.get(), 3.0)
128 self.failUnlessAlmostEqual(float(a.y), 3.0)
129 self.failUnlessEqual(a[0].is_set(), 1)
130 self.failUnlessEqual(a.x.is_set(), 1)
131 self.failUnlessEqual(a[1].is_set(), 1)
132 self.failUnlessEqual(a.y.is_set(), 1)
134 a = vector([1, 2, 3])
135 self.failUnlessEqual(str(a.x), "unnamed_vector[0]{=1.0}")
136 self.failUnlessEqual(str(a.y), "unnamed_vector[1]{=2.0}")
137 self.failUnlessEqual(str(a.z), "unnamed_vector[2]{=3.0}")
139 def testLen(self):
140 for i in range(1, 10):
141 a = vector(i)
142 self.failUnlessEqual(len(a), i)
143 self.failUnlessEqual(str(a), "unnamed_vector{=(" + ", ".join(["unnamed_vector[%i]" % j for j in range(i)]) + ")}")
144 for i in range(1, 10):
145 a = -vector(i)
146 self.failUnlessEqual(len(a), i)
147 self.failUnlessEqual(str(a), "unnamed_vector{=(" + ", ".join(["unnamed_scalar{=-1.0} * unnamed_vector[%i]" % j for j in range(i)]) + ")}")
149 def testMath(self):
150 self.failUnlessEqual(str(-vector(2, "a")), "unnamed_vector{=(unnamed_scalar{=-1.0} * a[0], unnamed_scalar{=-1.0} * a[1])}")
151 self.failUnlessEqual(str(vector(2, "a") + vector(2, "b")), "unnamed_vector{=(a[0] + b[0], a[1] + b[1])}")
152 self.failUnlessRaises(AttributeError, operator.__add__, vector(2), scalar())
153 self.failUnlessRaises(RuntimeError, operator.__add__, vector(2), vector(3))
154 self.failUnlessEqual(str(vector(2, "a") - vector(2, "b")), "unnamed_vector{=(unnamed_scalar{=-1.0} * b[0] + a[0], unnamed_scalar{=-1.0} * b[1] + a[1])}")
155 self.failUnlessRaises(RuntimeError, operator.__sub__, vector(2), scalar())
156 self.failUnlessRaises(RuntimeError, operator.__sub__, vector(2), vector(3))
157 self.failUnlessEqual(str(2 * vector(2, "a")), "unnamed_vector{=(a[0] * unnamed_scalar{=2.0}, a[1] * unnamed_scalar{=2.0})}")
158 self.failUnlessEqual(str(vector(2, "a") * 2), "unnamed_vector{=(a[0] * unnamed_scalar{=2.0}, a[1] * unnamed_scalar{=2.0})}")
159 self.failUnlessEqual(str(scalar(name="s") * vector(2, "a")), "unnamed_vector{=(a[0] * s, a[1] * s)}")
160 self.failUnlessEqual(str(scalar(name="s") * (vector(2, "a") + vector(2, "b"))), "unnamed_vector{=(a[0] * s + b[0] * s, a[1] * s + b[1] * s)}")
161 self.failUnlessEqual(str((scalar(name="s") + scalar(name="t")) * vector(2, "a")), "unnamed_vector{=(a[0] * s + a[0] * t, a[1] * s + a[1] * t)}")
162 self.failUnlessEqual(str((scalar(name="s") + scalar(name="t")) * (vector(2, "a") + vector(2, "b"))), "unnamed_vector{=(a[0] * s + b[0] * s + a[0] * t + b[0] * t, a[1] * s + b[1] * s + a[1] * t + b[1] * t)}")
163 self.failUnlessEqual(str(vector(2, "a") * scalar(name="s")), "unnamed_vector{=(a[0] * s, a[1] * s)}")
164 self.failUnlessEqual(str(vector(2, "a") * vector(2, "b")), "a[0] * b[0] + a[1] * b[1]")
165 self.failUnlessRaises(RuntimeError, operator.__mul__, vector(2, "a"), vector(3))
166 self.failUnlessEqual(str(vector(2, "a") / 2.0), "unnamed_vector{=(unnamed_scalar{=0.5} * a[0], unnamed_scalar{=0.5} * a[1])}")
167 self.failUnlessEqual(str(vector(2, "a") / 2), "unnamed_vector{=(unnamed_scalar{=0.0} * a[0], unnamed_scalar{=0.0} * a[1])}") # integer logic!
168 self.failUnlessRaises(TypeError, lambda: scalar() / vector(1))
169 self.failUnlessRaises(TypeError, lambda: vector(1) / vector(1))
170 self.failUnlessRaises(TypeError, lambda: (scalar() + scalar()) / vector(1))
171 self.failUnlessRaises(TypeError, lambda: (vector(1) + vector(1)) / vector(1))
174 class MatrixTestCase(unittest.TestCase):
176 def testInit(self):
177 self.failUnlessEqual(str(matrix([2, 3])), "unnamed_matrix{=((unnamed_matrix[0, 0], unnamed_matrix[0, 1], unnamed_matrix[0, 2]), (unnamed_matrix[1, 0], unnamed_matrix[1, 1], unnamed_matrix[1, 2]))}")
178 self.failUnlessEqual(str(matrix([[1, 2, 3], [4, 5, 6]])), "unnamed_matrix{=((unnamed_matrix[0, 0]{=1.0}, unnamed_matrix[0, 1]{=2.0}, unnamed_matrix[0, 2]{=3.0}), (unnamed_matrix[1, 0]{=4.0}, unnamed_matrix[1, 1]{=5.0}, unnamed_matrix[1, 2]{=6.0}))}")
179 self.failUnlessEqual(str(matrix([2, 3], "a")), "a{=((a[0, 0], a[0, 1], a[0, 2]), (a[1, 0], a[1, 1], a[1, 2]))}")
180 self.failUnlessEqual(str(matrix([[1, 2, 3], [4, 5, 6]], "a")), "a{=((a[0, 0]{=1.0}, a[0, 1]{=2.0}, a[0, 2]{=3.0}), (a[1, 0]{=4.0}, a[1, 1]{=5.0}, a[1, 2]{=6.0}))}")
182 def testMath(self):
183 self.failUnlessEqual(str(-matrix([2, 2], "A")), "unnamed_matrix{=((unnamed_scalar{=-1.0} * A[0, 0], unnamed_scalar{=-1.0} * A[0, 1]), (unnamed_scalar{=-1.0} * A[1, 0], unnamed_scalar{=-1.0} * A[1, 1]))}")
184 self.failUnlessEqual(str(matrix([2, 3], "A") + matrix([2, 3], "B")), "unnamed_matrix{=((A[0, 0] + B[0, 0], A[0, 1] + B[0, 1], A[0, 2] + B[0, 2]), (A[1, 0] + B[1, 0], A[1, 1] + B[1, 1], A[1, 2] + B[1, 2]))}")
185 self.failUnlessEqual(str(matrix([2, 3], "A") * matrix([3, 2], "B")), "unnamed_matrix{=((A[0, 0] * B[0, 0] + A[0, 1] * B[1, 0] + A[0, 2] * B[2, 0], A[0, 0] * B[0, 1] + A[0, 1] * B[1, 1] + A[0, 2] * B[2, 1]), (A[1, 0] * B[0, 0] + A[1, 1] * B[1, 0] + A[1, 2] * B[2, 0], A[1, 0] * B[0, 1] + A[1, 1] * B[1, 1] + A[1, 2] * B[2, 1]))}")
186 self.failUnlessEqual(str(matrix([2, 3], "A") * vector(3, "a")), "unnamed_vector{=(A[0, 0] * a[0] + A[0, 1] * a[1] + A[0, 2] * a[2], A[1, 0] * a[0] + A[1, 1] * a[1] + A[1, 2] * a[2])}")
189 if __name__ == "__main__":
190 unittest.main()