Angle Between Vectors
Implement a function to calculate the angle between two vectors a and b in degrees. This task involves understanding vector operations and trigonometric relationships, particularly the dot product and its connection to the cosine of the angle between vectors.
The dot product of two vectors a and b is given by a⋅b=∑i=1naibi, where n is the dimension of the vectors. The magnitude of a vector a is denoted by ∣∣a∣∣=a⋅a. To find the angle θ between two vectors, we use the formula cos(θ)=∣∣a∣∣⋅∣∣b∣∣a⋅b.
Here are the steps to calculate the angle:
- Compute the dot product of vectors a and b.
- Calculate the magnitudes of vectors a and b.
- Use the formula to find cos(θ).
- Find the angle θ using the inverse cosine function.
This technique is widely used in computer vision for measuring similarity between feature vectors.
Example:
angle_between([1, 0], [0, 1])
90.00
Step-by-step calculation using the angle formula:
θ=arccos(∥a∥×∥b∥a⋅b)
-
Compute dot product: a⋅b=(1×0)+(0×1)=0
-
Compute magnitudes: ∥a∥=12+02=1 ∥b∥=02+12=1
-
Apply the formula: cos(θ)=1×10=0
-
Find the angle: θ=arccos(0)=90°
-
Result: 90.00°
The vectors [1,0] and [0,1] are the standard basis vectors pointing along the x and y axes respectively, which are perpendicular (90° apart).
Constraints:
- Both vectors have the same length n where 1 ≤ n ≤ 1000
- Neither vector is the zero vector
- Return the angle in degrees, rounded to 2 decimal places