Skip to content
Advertisement

Fastest way to get dot product of a matrix and each point in the given array

I have an array Nx3 of N points, each one has X, Y and Z coordinate. I need to rotate each point, so i have the rotation matrix 3×3. To do this, i need to get dot product of the rotation matrix and each point. The problem is the array of points is quite massive (~1_000_000x3) and therefore it takes noticeable amount of time to compute the rotated points.

The only solution i come up with so far is simple for loop iterating over array of points. Here is the example with a piece of data:

JavaScript

I dont feel confident using numpy as i quite new to it, so i’m sure there is at least more elegant way to do. I’ve heard about np.einsum() but i don’t understand yet how to implement it here and i’m not sure it will make it quicker. And the main problem is still the computation time, so i want to now how to make it work faster?

Thank you very much in advance!

Advertisement

Answer

You can write the code using numba parallelization in no python mode as:

JavaScript

It must be better than ko3 answer due to parallelization and signatures and using algebra instead np.dot. I have tested similar code (that was applied just on upper triangle of an array) instead dot product in another answer which showed that was at least 2 times faster (as far as I can remember).

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement