Numpy Notes

Compute dot product of multidimensional matrix with vector

Supposed we have an (N, N, N, 3) matrix representing the (X, Y, Z) coordinates of all voxels in a rectangular 3D grid.

In order to compute the dot product of each voxel coordinate with a vector, we can do:

vector = np.array((1, 0, 0))
np.tensordot(grid, vector, axes=1)