1 import unittest
, operator
3 from solve
import scalar
, vector
, matrix
, identitymatrix
, trafo
, identitytrafo
, Solver
5 class ScalarTestCase(unittest
.TestCase
):
8 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), scalar
, "")
9 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), 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}")
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((RuntimeError, AttributeError, TypeError), operator
.__div
__, 2, scalar())
30 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, scalar(), scalar())
31 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, vector(1), scalar())
32 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, scalar() + scalar(), scalar())
33 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, vector(1) + vector(1), scalar())
37 self
.failUnlessEqual(s
.is_set(), 0)
38 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), s
.get
)
39 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), float, s
)
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, AttributeError, TypeError), s
.set, 3)
45 self
.failUnlessEqual(s
.is_set(), 1)
46 self
.failUnlessAlmostEqual(s
.get(), 2.0)
47 self
.failUnlessAlmostEqual(float(s
), 2.0)
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, AttributeError, TypeError), 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
):
62 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), 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})}")
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")
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, AttributeError, TypeError), a
[1].get
)
85 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), float, a
[1])
86 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), a
.y
.get
)
87 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), 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, AttributeError, TypeError), 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, AttributeError, TypeError), a
[1].get
)
98 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), float, a
[1])
99 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), a
.y
.get
)
100 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), 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)
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, AttributeError, TypeError), 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}")
140 for i
in range(1, 5):
142 self
.failUnlessEqual(len(a
), i
)
143 self
.failUnlessEqual(str(a
), "unnamed_vector{=(" + ", ".join(["unnamed_vector[%i]" % j
for j
in range(i
)]) + ")}")
146 self
.failUnlessEqual(str(-vector(2, "a")), "unnamed_vector{=(unnamed_scalar{=-1.0} * a[0], unnamed_scalar{=-1.0} * a[1])}")
147 self
.failUnlessEqual(str(vector(2, "a") + vector(2, "b")), "unnamed_vector{=(a[0] + b[0], a[1] + b[1])}")
148 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, 1, vector(2))
149 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, vector(2), 1)
150 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, scalar(), vector(2))
151 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, vector(2), scalar())
152 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, scalar() + scalar(), vector(2))
153 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, vector(2), scalar() + scalar())
154 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, vector(2), vector(3))
155 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])}")
156 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, 1, vector(2))
157 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, vector(2), 1)
158 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, scalar(), vector(2))
159 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, vector(2), scalar())
160 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, scalar() + scalar(), vector(2))
161 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, vector(2), scalar() + scalar())
162 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, vector(2), vector(3))
163 self
.failUnlessEqual(str(2 * vector(2, "a")), "unnamed_vector{=(a[0] * unnamed_scalar{=2.0}, a[1] * unnamed_scalar{=2.0})}")
164 self
.failUnlessEqual(str(vector(2, "a") * 2), "unnamed_vector{=(a[0] * unnamed_scalar{=2.0}, a[1] * unnamed_scalar{=2.0})}")
165 self
.failUnlessEqual(str(scalar(name
="s") * vector(2, "a")), "unnamed_vector{=(a[0] * s, a[1] * s)}")
166 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)}")
167 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)}")
168 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)}")
169 self
.failUnlessEqual(str(vector(2, "a") * scalar(name
="s")), "unnamed_vector{=(a[0] * s, a[1] * s)}")
170 self
.failUnlessEqual(str(vector(2, "a") * vector(2, "b")), "a[0] * b[0] + a[1] * b[1]")
171 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__mul
__, vector(2, "a"), vector(3))
172 self
.failUnlessEqual(str(vector(2, "a") / 2.0), "unnamed_vector{=(unnamed_scalar{=0.5} * a[0], unnamed_scalar{=0.5} * a[1])}")
173 self
.failUnlessEqual(str(vector(2, "a") / 2), "unnamed_vector{=(unnamed_scalar{=0.0} * a[0], unnamed_scalar{=0.0} * a[1])}") # integer logic!
174 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, scalar(), vector(1))
175 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, scalar() + scalar(), vector(1))
176 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, vector(1), scalar())
177 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, vector(1), scalar() + scalar())
178 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, vector(1), vector(1))
181 class MatrixTestCase(unittest
.TestCase
):
184 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]))}")
185 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}))}")
186 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]))}")
187 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}))}")
188 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), matrix
, [[1, 2, 3], [4, 5]])
191 for rows
in range(1, 5):
192 for cols
in range(1, 5):
193 A
= matrix((rows
, cols
))
194 self
.failUnlessEqual(A
.getnumberofrows(), rows
)
195 self
.failUnlessEqual(A
.getnumberofcols(), cols
)
196 self
.failUnlessEqual(str(A
), "unnamed_matrix{=(" + ", ".join(["(" + ", ".join(["unnamed_matrix[%i, %i]" % (i
, j
) for j
in range(cols
)]) + ")" for i
in range(rows
)]) + ")}")
198 def testAccess(self
):
200 self
.failUnlessEqual(str(A
), "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]))}")
201 self
.failUnlessEqual(A
[0, 0].is_set(), 0)
202 self
.failUnlessEqual(A
[1, 0].is_set(), 0)
203 self
.failUnlessRaises(IndexError, operator
.__getitem
__, A
, (2, 0))
204 self
.failUnlessEqual(A
[0, 1].is_set(), 0)
205 self
.failUnlessEqual(A
[1, 1].is_set(), 0)
206 self
.failUnlessEqual(A
[0, 2].is_set(), 0)
207 self
.failUnlessEqual(A
[1, 2].is_set(), 0)
208 self
.failUnlessRaises(IndexError, operator
.__getitem
__, A
, (0, 3))
210 self
.failUnlessEqual(A
[0, 0].is_set(), 1)
211 self
.failUnlessEqual(A
[0, 1].is_set(), 0)
212 self
.failUnlessEqual(A
[1, 0].is_set(), 0)
213 self
.failUnlessAlmostEqual(A
[0, 0].get(), 1.0)
214 self
.failUnlessAlmostEqual(float(A
[0, 0]), 1.0)
217 self
.failUnlessEqual(str(-matrix([2, 3], "A")), "unnamed_matrix{=((unnamed_scalar{=-1.0} * A[0, 0], unnamed_scalar{=-1.0} * A[0, 1], unnamed_scalar{=-1.0} * A[0, 2]), (unnamed_scalar{=-1.0} * A[1, 0], unnamed_scalar{=-1.0} * A[1, 1], unnamed_scalar{=-1.0} * A[1, 2]))}")
218 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]))}")
219 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, matrix([2, 3]), 1)
220 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, matrix([2, 3]), scalar())
221 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, matrix([2, 3]), scalar() + scalar())
222 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, matrix([2, 3]), vector(2))
223 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, matrix([2, 3]), vector(3))
224 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, matrix([2, 3]), matrix([2, 4]))
225 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, matrix([2, 3]), matrix([3, 3]))
226 self
.failUnlessEqual(str(matrix([2, 3], "A") - matrix([2, 3], "B")), "unnamed_matrix{=((unnamed_scalar{=-1.0} * B[0, 0] + A[0, 0], unnamed_scalar{=-1.0} * B[0, 1] + A[0, 1], unnamed_scalar{=-1.0} * B[0, 2] + A[0, 2]), (unnamed_scalar{=-1.0} * B[1, 0] + A[1, 0], unnamed_scalar{=-1.0} * B[1, 1] + A[1, 1], unnamed_scalar{=-1.0} * B[1, 2] + A[1, 2]))}")
227 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, matrix([2, 3]), 1)
228 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, matrix([2, 3]), scalar())
229 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, matrix([2, 3]), scalar() + scalar())
230 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, matrix([2, 3]), vector(2))
231 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, matrix([2, 3]), vector(3))
232 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, matrix([2, 3]), matrix([2, 4]))
233 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, matrix([2, 3]), matrix([3, 3]))
234 self
.failUnlessEqual(str(2 * matrix([2, 3], "A")), "unnamed_matrix{=((A[0, 0] * unnamed_scalar{=2.0}, A[0, 1] * unnamed_scalar{=2.0}, A[0, 2] * unnamed_scalar{=2.0}), (A[1, 0] * unnamed_scalar{=2.0}, A[1, 1] * unnamed_scalar{=2.0}, A[1, 2] * unnamed_scalar{=2.0}))}")
235 self
.failUnlessEqual(str(matrix([2, 3], "A") * 2), "unnamed_matrix{=((A[0, 0] * unnamed_scalar{=2.0}, A[0, 1] * unnamed_scalar{=2.0}, A[0, 2] * unnamed_scalar{=2.0}), (A[1, 0] * unnamed_scalar{=2.0}, A[1, 1] * unnamed_scalar{=2.0}, A[1, 2] * unnamed_scalar{=2.0}))}")
236 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])}")
237 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__mul
__, vector(2, "a"), matrix([2, 3], "A"))
238 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]))}")
239 self
.failUnlessEqual(str(matrix([2, 3], "A") / 2.0), "unnamed_matrix{=((unnamed_scalar{=0.5} * A[0, 0], unnamed_scalar{=0.5} * A[0, 1], unnamed_scalar{=0.5} * A[0, 2]), (unnamed_scalar{=0.5} * A[1, 0], unnamed_scalar{=0.5} * A[1, 1], unnamed_scalar{=0.5} * A[1, 2]))}")
240 self
.failUnlessEqual(str(matrix([2, 3], "A") / 2), "unnamed_matrix{=((unnamed_scalar{=0.0} * A[0, 0], unnamed_scalar{=0.0} * A[0, 1], unnamed_scalar{=0.0} * A[0, 2]), (unnamed_scalar{=0.0} * A[1, 0], unnamed_scalar{=0.0} * A[1, 1], unnamed_scalar{=0.0} * A[1, 2]))}")
241 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, scalar(), matrix([2, 3]))
242 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, scalar() + scalar(), matrix([2, 3]))
243 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, vector(1), matrix([2, 3]))
244 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, matrix([2, 3]), scalar())
245 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, matrix([2, 3]), scalar() + scalar())
246 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, matrix([2, 3]), vector(1))
247 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, matrix([2, 3]), matrix([2, 3]))
250 class TrafoTestCase(unittest
.TestCase
):
253 self
.failUnlessEqual(str(trafo((2, 3))), "unnamed_trafo{=(matrix: unnamed_trafo_matrix{=((unnamed_trafo_matrix[0, 0], unnamed_trafo_matrix[0, 1], unnamed_trafo_matrix[0, 2]), (unnamed_trafo_matrix[1, 0], unnamed_trafo_matrix[1, 1], unnamed_trafo_matrix[1, 2]))}, vector: unnamed_trafo_vector{=(unnamed_trafo_vector[0], unnamed_trafo_vector[1])})}")
254 self
.failUnlessEqual(str(trafo((2, 3), "A")), "A{=(matrix: A_matrix{=((A_matrix[0, 0], A_matrix[0, 1], A_matrix[0, 2]), (A_matrix[1, 0], A_matrix[1, 1], A_matrix[1, 2]))}, vector: A_vector{=(A_vector[0], A_vector[1])})}")
255 self
.failUnlessEqual(str(trafo((matrix([2, 3], "B"), vector(2, "c")), "A")), "A{=(matrix: B{=((B[0, 0], B[0, 1], B[0, 2]), (B[1, 0], B[1, 1], B[1, 2]))}, vector: c{=(c[0], c[1])})}")
256 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), trafo
, (matrix([2, 3]), vector(3)))
258 def testAccess(self
):
262 self
.failUnlessEqual(t
.getmatrix(), m
)
263 self
.failUnlessEqual(t
.getvector(), v
)
266 self
.failUnlessEqual(str(-trafo([2, 3], "A")), str(trafo((-matrix([2, 3], "A_matrix"), -vector(2, "A_vector")))))
267 self
.failUnlessEqual(str(trafo([2, 3], "A") + trafo([2, 3], "B")), str(trafo((matrix([2, 3], "A_matrix") + matrix([2, 3], "B_matrix"), vector(2, "A_vector") + vector(2, "B_vector")))))
268 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, 1, trafo([2, 3]))
269 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, trafo([2, 3]), 1)
270 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, scalar(), trafo([2, 3]))
271 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, trafo([2, 3]), scalar())
272 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, scalar() + scalar(), trafo([2, 3]))
273 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, trafo([2, 3]), scalar() + scalar())
274 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, vector(2), trafo([2, 3]))
275 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, trafo([2, 3]), vector(2))
276 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, vector(3), trafo([2, 3]))
277 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, trafo([2, 3]), vector(3))
278 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, matrix([2, 3]), trafo([2, 3]))
279 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, trafo([2, 3]), matrix([2, 3]))
280 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, trafo([2, 3]), trafo([2, 4]))
281 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__add
__, trafo([2, 3]), trafo([3, 3]))
282 self
.failUnlessEqual(str(trafo([2, 3], "A") - trafo([2, 3], "B")), str(trafo((matrix([2, 3], "A_matrix") - matrix([2, 3], "B_matrix"), vector(2, "A_vector") - vector(2, "B_vector")))))
283 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, 1, trafo([2, 3]))
284 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, trafo([2, 3]), 1)
285 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, scalar(), trafo([2, 3]))
286 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, trafo([2, 3]), scalar())
287 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, scalar() + scalar(), trafo([2, 3]))
288 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, trafo([2, 3]), scalar() + scalar())
289 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, vector(2), trafo([2, 3]))
290 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, trafo([2, 3]), vector(2))
291 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, vector(3), trafo([2, 3]))
292 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, trafo([2, 3]), vector(3))
293 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, matrix([2, 3]), trafo([2, 3]))
294 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, trafo([2, 3]), matrix([2, 3]))
295 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, trafo([2, 3]), trafo([2, 4]))
296 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__sub
__, trafo([2, 3]), trafo([3, 3]))
297 self
.failUnlessEqual(str(trafo([2, 3], "A") * trafo([3, 4], "B")), str(trafo((matrix([2, 3], "A_matrix") * matrix([3, 4], "B_matrix"), vector(2, "A_vector") + matrix([2, 3], "A_matrix") * vector(3, "B_vector")))))
298 self
.failUnlessEqual(str(2 * trafo([2, 3], "A")), str(trafo((2 * matrix([2, 3], "A_matrix"), 2 * vector(2, "A_vector")))))
299 self
.failUnlessEqual(str(trafo([2, 3], "A") * 2), str(trafo((matrix([2, 3], "A_matrix") * 2, vector(2, "A_vector") * 2))))
300 self
.failUnlessEqual(str(scalar() * trafo([2, 3], "A")), str(trafo((scalar() * matrix([2, 3], "A_matrix"), scalar() * vector(2, "A_vector")))))
301 self
.failUnlessEqual(str(trafo([2, 3], "A") * scalar()), str(trafo((matrix([2, 3], "A_matrix") * scalar(), vector(2, "A_vector") * scalar()))))
302 self
.failUnlessEqual(str((scalar() + scalar()) * trafo([2, 3], "A")), str(trafo(((scalar() + scalar()) * matrix([2, 3], "A_matrix"), (scalar() + scalar()) * vector(2, "A_vector")))))
303 self
.failUnlessEqual(str(trafo([2, 3], "A") * (scalar() + scalar())), str(trafo((matrix([2, 3], "A_matrix") * (scalar() + scalar()), vector(2, "A_vector") * (scalar() + scalar())))))
304 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__mul
__, vector(2), trafo([2, 3]))
305 self
.failUnlessEqual(str(trafo([2, 3], "A") * vector(3, "a")), str(matrix([2, 3], "A_matrix") * vector(3, "a") + vector(2, "A_vector")))
306 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__mul
__, trafo([2, 3]), vector(2))
307 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__mul
__, matrix([3, 2]), trafo([2, 3]))
308 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__mul
__, trafo([2, 3]), matrix([3, 2]))
309 self
.failUnlessEqual(str(trafo([2, 3], "A") / 2.0), str(trafo((matrix([2, 3], "A_matrix") / 2.0, vector(2, "A_vector") / 2.0))))
310 self
.failUnlessEqual(str(trafo([2, 3], "A") / 2), str(trafo((matrix([2, 3], "A_matrix") / 2, vector(2, "A_vector") / 2))))
311 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, scalar(), trafo([2, 3]))
312 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, scalar() + scalar(), trafo([2, 3]))
313 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, vector(1), trafo([2, 3]))
314 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, matrix([2, 3]), trafo([2, 3]))
315 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, trafo([2, 3]), scalar())
316 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, trafo([2, 3]), scalar() + scalar())
317 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, trafo([2, 3]), vector(1))
318 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, trafo([2, 3]), matrix([2, 3]))
319 self
.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator
.__div
__, trafo([2, 3]), trafo([2, 3]))
321 class SolverTestCase(unittest
.TestCase
):
323 def testScalars(self
):
327 self
.failUnlessAlmostEqual(float(a
), 1)
328 self
.failUnlessRaises(RuntimeError, solver
.eq
, 2 + a
, 3) # the solver is broken after that
333 solver
.eq(2 + a
+ b
, 5)
334 self
.failUnlessAlmostEqual(float(a
), 1)
335 self
.failUnlessAlmostEqual(float(b
), 2)
339 self
.failUnlessRaises(Exception, solver
.eq
, a
, b
)
341 def testVector(self
):
344 solver
.eq(vector([1, 2]) + a
, vector([3, 3]))
345 self
.failUnlessAlmostEqual(float(a
.x
), 2)
346 self
.failUnlessAlmostEqual(float(a
.y
), 1)
348 solver
.eq(a
*b
) # => solver.eq(a*b, 0)
350 self
.failUnlessAlmostEqual(float(b
.x
), -1)
352 def testMatrix(self
):
355 solver
.eq(A
*vector([1, -1]), vector([2, 0]))
356 solver
.eq(A
*vector([1, 1]), vector([0, 2]))
357 self
.failUnlessAlmostEqual(float(A
[0, 0]), 1)
358 self
.failUnlessAlmostEqual(float(A
[0, 1]), -1)
359 self
.failUnlessAlmostEqual(float(A
[1, 0]), 1)
360 self
.failUnlessAlmostEqual(float(A
[1, 1]), 1)
362 solver
.eq(A
*B
, identitymatrix(2))
363 self
.failUnlessAlmostEqual(float(B
[0, 0]), 0.5)
364 self
.failUnlessAlmostEqual(float(B
[0, 1]), 0.5)
365 self
.failUnlessAlmostEqual(float(B
[1, 0]), -0.5)
366 self
.failUnlessAlmostEqual(float(B
[1, 1]), 0.5)
367 self
.failUnlessAlmostEqual(float((B
* vector([2, 0]))[0]), 1)
368 self
.failUnlessAlmostEqual(float((B
* vector([2, 0]))[1]), -1)
369 self
.failUnlessAlmostEqual(float((B
* vector([0, 2]))[0]), 1)
370 self
.failUnlessAlmostEqual(float((B
* vector([0, 2]))[1]), 1)
375 solver
.eq(A
.getmatrix()[0, 0], A
.getmatrix()[1, 1]) # restrict to rotation + scaling + translation
376 solver
.eq(A
.getmatrix()[0, 1], -A
.getmatrix()[1, 0])
377 solver
.eq(A
*vector([0, 1]), vector([-1, 2]))
378 solver
.eq(A
*vector([1, 0]), vector([1, 4]))
379 self
.failUnlessEqual(float((A
* vector([0, 0]))[0]), 1)
380 self
.failUnlessEqual(float((A
* vector([0, 0]))[1]), 2)
381 self
.failUnlessEqual(float(A
.getmatrix()[0, 0]), 0)
382 self
.failUnlessEqual(float(A
.getmatrix()[0, 1]), -2)
383 self
.failUnlessEqual(float(A
.getmatrix()[1, 0]), 2)
384 self
.failUnlessEqual(float(A
.getmatrix()[1, 1]), 0)
386 solver
.eq(A
*B
, identitytrafo(2))
387 self
.failUnlessEqual(float((B
* vector([0, 0]))[0]), -1)
388 self
.failUnlessEqual(float((B
* vector([0, 0]))[1]), 0.5)
389 self
.failUnlessEqual(float(B
.getmatrix()[0, 0]), 0)
390 self
.failUnlessEqual(float(B
.getmatrix()[0, 1]), 0.5)
391 self
.failUnlessEqual(float(B
.getmatrix()[1, 0]), -0.5)
392 self
.failUnlessEqual(float(B
.getmatrix()[1, 1]), 0)
395 if __name__
== "__main__":