I posted a VBA function to return The angle between two vectors, in 2D or 3D last year, and have just discovered that Python and Numpy are lacking this function. Since all the suggested code I found in a quick search used:
Cos θ = (a.b)/(|a||b|)
which gives inaccurate results for small angles, I have written my own, using the same procedure as the VBA version:
Tan θ = |(axb)|/ (a.b)
Here is the lengthy code:
import numpy as np import numpy.linalg as la @xl_func("numpy_row v1, numpy_row v2: float") def py_ang(v1, v2): """ Returns the angle in radians between vectors 'v1' and 'v2' """ cosang = np.dot(v1, v2) sinang = la.norm(np.cross(v1, v2)) return np.arctan2(sinang, cosang)
For details on how to link to Python functions from Excel, using Pyxll, see: Installing Python, Scipy and Pyxll ; also an updated Glob_to_Loc function, using the py_ang function, will appear within the next few days.