Reputation: 448
Rotation about the origin is a matrix product that can be done with numpy's dot function,
import numpy as np
points = np.random.rand(100,3) # 100 X, Y, Z tuples. shape = (100,3)
rotation = np.identity(3) # null rotation for example
out = np.empty(points.shape)
for idx, point in enumerate(points):
out[idx,:] = np.dot(rotation, point)
This involves a for loop, or numpy tile could be used to vectorize. I think there is an implementation involving np.tensordot, but the function is witchcraft to me. Is this possible?
Upvotes: 2
Views: 454
Reputation: 59731
There are several ways you can do that. With np.matmul
you can do:
out = np.matmul(rotation, points[:, :, np.newaxis])[:, :, 0]
Or, equivalently, if you are using Python 3.5 or later:
out = (rotation @ points[:, :, np.newaxis])[:, :, 0]
Another way is with np.einsum
:
out = np.einsum('ij,nj->ni', rotation, points)
Finally, as you suggested, you can also use np.tensordot
:
out = np.tensordot(points, rotation, axes=[1, 1])
Note that in this case points
is the first argument and rotation
the second, otherwise the dimensions at the output would be reversed.
Upvotes: 3