# Calculating “element-wise” the angles between two lists of vectors

Let's say you have two lists of vectors:

v1s = [a, b, c]


v2s = [d, e, f]


I am interested in generating the following result:

angles = [angleBetween(a, d), angleBetween(b, e), angleBetween(c, f)]


Here is my function to do this (it uses vector dot products in order to calculate the angles):

import numpy as np

def findAnglesBetweenTwoVectors(v1s, v2s):
dot_v1_v2 = np.einsum('ij,ij->i', v1s, v2s)
dot_v1_v1 = np.einsum('ij,ij->i', v1s, v1s)
dot_v2_v2 = np.einsum('ij,ij->i', v2s, v2s)

return np.arccos(dot_v1_v2/(np.sqrt(dot_v1_v1)*np.sqrt(dot_v2_v2)))


I call this function a lot, so optimizing it would be very helpful for me. Is this possible?

• Is the middle term angleBetween(b, d) or angleBetween(b, e)? – hpaulj Jun 16 '14 at 7:11
• @hpaulj It should be angleBetween(b, e)! Fixed now. – user89 Jun 16 '14 at 7:19
• You can spare yourself half of your square roots, which are expensive operations, by multiplying before taking the root, i.e. return np.arccos(dot_v1_v2/np.sqrt(dot_v1_v1*dot_v2_v2)). – Jaime Jun 16 '14 at 15:17

Here's a way of packing the calculation into on call to einsum.

def findAnglesBetweenTwoVectors1(v1s, v2s):
dot = np.einsum('ijk,ijk->ij',[v1s,v1s,v2s],[v2s,v1s,v2s])
return np.arccos(dot[0,:]/(np.sqrt(dot[1,:])*np.sqrt(dot[2,:])))


for random vectors of length 10, the speed for this function and yours is basically the same. But with 10000, (corrected times)

In : timeit findAnglesBetweenTwoVectors0(v1s,v2s)
100 loops, best of 3: 2.26 ms per loop

In : timeit findAnglesBetweenTwoVectors1(v1s,v2s)
100 loops, best of 3: 4.24 ms per loop


Yours is clearly faster.

We could try to figure out why the more complex einsum is slower (assuming the issue is there rather than the indexing in the last line). But this result is consistent with other einsum testing that I've seen and done. einsum on simple 2D cases is as fast as np.dot, and in some cases faster. But in more complex cases (3 or more indexes) it is slower than the equivalent constructed from multiple calls to np.dot or einsum.

I tried tried factoring out the array construction, and got a speed improvement.

In : V1=np.array(v1s)  # shape (3,10000,3)
In : V2=np.array(v2s)
In : timeit findAnglesBetweenTwoVectors2(V1,V2)
100 loops, best of 3: 2.63 ms per loop


I've corrected the times, using v1s.shape==(10000,3)

• Pretty interesting. I am setting up things on a new computer where I will do my testing, so I hadn't gotten to testing that out yet myself. I can now cross it off of my list. My next idea was to perhaps execute the three einsum in parallel using multiprocessing.pool. I will get back to you with my results from that experiment. – user89 Jun 16 '14 at 18:00
• I am also considering stealing this code here (there is a function to compute the angle between two vectors), and using PyOpenCL to then implement it. Not sure though. – user89 Jun 16 '14 at 18:21
• Update on the multiprocessing test: its much slower: the normal version is over 9000 (serious, no pun intended) times faster. – user89 Jun 16 '14 at 20:30
• By the way, in my current construction, I am always supplying numpy array objects to the function. The shape should be (10000,). – user89 Jun 16 '14 at 20:36
• I changed the inputs so the shape is correct. The 3 einsum calculations is still better, though most of the extra in the single einsum case is due to the work of assembling the 3d arrays. – hpaulj Jun 16 '14 at 21:43