I have a time series of spacecraft coordinates, which is of shape (t,3)
, and a time series of rotation matrices of shape (3,3,t)
, where t is the length of the time series. I want to find the dot product of the coordinates at each time t with the rotation matrix at each time t, such that I achieve an array of shape (t,3)
which is the rotated coordinates.
I can achieve this in a for loop by writing:
new_coords = np.zeros_like(input_coords)
for Ci, Vi in enumerate(input_coords):
new_coords[Ci,:] = np.tensordot(Vi, rotation[:,:,Ci], axes = 1)
How can I replace this for loop with a single line of Python? I've tried various permutations of np.tensordot
with no success.
You can use
np.einsum
-Shapes in generic format -
Two considerations were applied there -
rotation
with last ofinput_coords
.rotation
and first ofinput_coords
aligned. This is in correspondence with the wayCi
is used within the nested loop.