Dot product of a time series of co-ordinates with a time series of rotation matrices


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.

Show source
| numpy   | python   | matrix   | matrix-multiplication   | dot-product   2016-12-16 12:12 1 Answers

Answers ( 1 )

  1. 2016-12-16 12:12

    You can use np.einsum -

    np.einsum('ijk,ki->kj',rotation, input_coords)

    Shapes in generic format -

    rotation     : 3 x 3 x N
    input_coords : N x 3

    Two considerations were applied there -

    • Sum-reduction of first (axes) of rotation with last of input_coords.
    • Keeping the last of rotation and first of input_coords aligned. This is in correspondence with the way Ci is used within the nested loop.
◀ Go back