@@ -105,13 +105,29 @@ def rq(A):
105105
106106
107107def vector_angle (u , v ):
108- '''
108+ """Angle between two vectors.
109+
110+ >>> u = [ 0.99500417, -0.33333333, -0.09983342]
111+ >>> v = [ 0.99500417, -0.33333333, -0.09983342]
112+ >>> vector_angle(u, v)
113+ 0.0
114+ """
115+ cos = np .dot (u , v ) / math .sqrt (np .dot (u , u ) * np .dot (v , v ))
116+ if cos >= 1.0 :
117+ return 0.0
118+ else :
119+ return math .acos (cos )
120+
121+
122+ def vector_angle_many (u , v ):
123+ """Angles between to lists of vectors.
124+
109125 >>> u = [[0.99500417, -0.33333333, -0.09983342], [0, -1, 0], [0, 1, 0]]
110126 >>> v = [[0.99500417, -0.33333333, -0.09983342], [0, +1, 0], [0, 0, 1]]
111- >>> angles = vector_angle (u, v)
127+ >>> angles = vector_angle_many (u, v)
112128 >>> np.allclose(angles, [0., 3.1416, 1.5708])
113129 True
114- '''
130+ """
115131 ua = np .array (u , dtype = np .float64 , copy = False ).reshape (- 1 , 3 )
116132 va = np .array (v , dtype = np .float64 , copy = False ).reshape (- 1 , 3 )
117133 return tf .angle_between_vectors (ua , va , axis = 1 )
0 commit comments