Compute the angle in radians between two 3D vectors. First obtain their cosine:
c=∥v∥2∥w∥2v⋅wThen recover the angle:
θ=arccos(c)Clamp c to [−1,1] before applying arccos to protect against floating-point error. If either vector has zero norm, the angle is undefined, so return np.nan. Otherwise return a Python float in [0,π].
Input: v = [1, 0, 0], w = [0, 1, 0]
Output: 1.570796
Explanation: Orthogonal vectors have cosine zero and an angle of pi divided by two.
Input: v = [1, 2, 3], w = [2, 4, 6]
Output: 0
Use np.dot(v, w) for the numerator and squared sums for both norms.
Pass np.clip(cosine, -1.0, 1.0) to np.arccos.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Compute the angle in radians between two 3D vectors. First obtain their cosine:
c=∥v∥2∥w∥2v⋅wThen recover the angle:
θ=arccos(c)Clamp c to [−1,1] before applying arccos to protect against floating-point error. If either vector has zero norm, the angle is undefined, so return np.nan. Otherwise return a Python float in [0,π].
Input: v = [1, 0, 0], w = [0, 1, 0]
Output: 1.570796
Explanation: Orthogonal vectors have cosine zero and an angle of pi divided by two.
Input: v = [1, 2, 3], w = [2, 4, 6]
Output: 0
Use np.dot(v, w) for the numerator and squared sums for both norms.
Pass np.clip(cosine, -1.0, 1.0) to np.arccos.
Sign in to take notes on this problem
Accepts: array
Accepts: array