I have more than 5 Million pairs of two 3D Vectors and I need to calculate the angle between each pair. I tried with:
# calculate angle
def unit_vector(vector):
return vector / np.linalg.norm(vector)
def angle_between(v1, v2):
v1_u = unit_vector(v1)
v2_u = unit_vector(v2)
return np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0))
as in this post
E.g.:
a=[[1,1,1],[1,2,1],[6,4,5]]
b=[[1,0,0],[6,2,2],[1,9,2]]
anglevec=np.zeros(len(a))
for i in range(len(a)):
anglevec[i]=angle_between(a[i], b[i])
print(anglevec)
but the implementation with the loop is far too slow.
Can anyone help?