9 from functools
import reduce
10 from math
import cos, pi, sin, sqrt
20 if isinstance(other, R3):
21 return other.multiplyByConstant(self.
value)
22 if isinstance(other, float):
23 return R(self.
value * other)
24 if isinstance(other, R):
25 return R(self.
value * other.value)
27 "cannot multiply instances of type R and type " + str(type(other))
31 return str(self.
value)
36 Vectors of real number of dimension 3.
41 if isinstance(args[0], R3):
46 self.
value = numpy.array(map(float, args[0]))
48 self.
value = numpy.array(map(float, args))
51 "constructor of R3 takes either three float,"
52 +
", an instance of R or a tuple of three float."
58 Check that instance is well defined.
60 if len(self.
value) != 3:
61 raise TypeError(
"R3 object should be of length 3")
65 Check that argument is of type R3
67 if not isinstance(other, R3):
69 "second argument of operator "
71 +
" should be an instance of R3."
80 cp.append(self.
value[1] * other.value[2] - self.
value[2] * other.value[1])
81 cp.append(self.
value[2] * other.value[0] - self.
value[0] * other.value[2])
82 cp.append(self.
value[0] * other.value[1] - self.
value[1] * other.value[0])
90 return R3(tuple(map(
lambda x: x[0] + x[1], zip(self, other))))
97 return R3(tuple(map(
lambda x: x[0] - x[1], zip(self, other))))
101 Operator *: inner product
104 return reduce(
lambda x, y: x + y[0] * y[1], zip(self, other), 0.0)
108 Operator * by a float
110 return R3(tuple(map(
lambda x: number * x, self)))
116 return "({0},{1},{2})".format(*self.
value)
122 return self.
value[index]
128 self.
value[index] = value
132 Multiply vector by a constant value
134 return R3(tuple(map(
lambda x: constant * x, self)))
140 return numpy.array(self.
value)
149 if isinstance(matrix, SO3):
157 "expecting a tuple of "
158 +
"3 tuples of 3 float, got a tuple of length %i." % len(matrix)
163 raise TypeError(
"expecting tuple of 3 float or instance of R3.")
164 m.append(
R3(r[0], r[1], r[2]))
169 Return the inverse of the matrix
175 Return the transpose of the matrix
177 return SO3(tuple(zip(*self)))
183 return "(%s,%s,%s)" % self.
value
189 return self.
value[index]
195 second argument is either
196 - an instance of SO3 or
199 if isinstance(other, SO3):
201 if isinstance(other, R3):
204 "Cannot multiply instance of SO3 by instance of " + str(type(other)) +
"."
208 return R3(self[0] * other, self[1] * other, self[2] * other)
212 reduce(
lambda m, col: m + (tuple(self * col),), other.transpose(), ())
220 - by rotation matrix and translation vector,
221 - by homogeneous matrix
222 - without argument: return identity matrix
231 self.
rotation =
SO3(tuple(map(
lambda row: row[:3], matrix[:3])))
232 self.
translation =
R3(tuple(map(
lambda row: row[3], matrix[:3])))
235 ((1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)),
239 raise ValueError(
"two many arguments.")
245 return "((%f,%f,%f,%f),(%f,%f,%f,%f),(%f,%f,%f,%f),(0.,0.,0.,1.))" % (
268 other argument is either
269 - an instance of SE3 or,
272 if isinstance(other, SE3):
274 if isinstance(other, R3):
277 "cannot multiply instance of SE3 by instance of " + str(type(other))
291 return (0.0, 0.0, 0.0, 1.0)
299 (r[0][0], r[0][1], r[0][2], t[0]),
300 (r[1][0], r[1][1], r[1][2], t[1]),
301 (r[2][0], r[2][1], r[2][2], t[2]),
302 (0.0, 0.0, 0.0, 1.0),
306 if __name__ ==
"__main__":
308 u =
R3(1.0, 2.0, 3.0)
309 v =
R3(2.0, 3.0, 4.0)
310 print(
"a = " + str(a))
311 print(
"u = " + str(u))
312 print(
"v = " + str(v))
313 print(
"a*a = " + str(a * a))
314 print(
"a*u = " + str(a * u))
315 print(
"u*u = " + str(u * u))
316 print(
"u-v = " + str(u - v))
317 print(
"u*v = " + str(u * v))
319 for i, x
in zip(range(3), u):
320 print(
"u[%i] = %f" % (i, x))
323 (cos(pi / 6), -sin(pi / 6), 0.0),
324 (sin(pi / 6), cos(pi / 6), 0.0),
328 print(
"m1 = " + str(m1))
331 (cos(pi / 6), 0.0, -sin(pi / 6)),
333 (sin(pi / 6), 0.0, cos(pi / 6)),
337 print(
"m2 = " + str(m2))
340 print(
"m1*m2 = " + str(m1 * m2))
342 "should be ((%f,%f,%f),(%f,%f,%f),(%f,%f,%f))"
357 print(
"m3 = " + str(m3))
362 print(
"m3[%i][%i] = %f" % (i, j, m3[i][j]))
366 print(
"m3.inverse() = " + str(m3inv))
369 print(
"m3inv * m3 = " + str(m3inv * m3))
371 print(
"m3 * m3inv = " + str(m3 * m3inv))