PIXELBANKv9.1.0
Menu

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\mathbf{a} \cdot \mathbf{b} = \sum_{i=1}^{n} a_i b_i, where nn is the dimension of the vectors. The magnitude of a vector a is denoted by ∣∣a∣∣=a⋅a||\mathbf{a}|| = \sqrt{\mathbf{a} \cdot \mathbf{a}}. To find the angle θ\theta between two vectors, we use the formula cos⁡(θ)=a⋅b∣∣a∣∣⋅∣∣b∣∣\cos(\theta) = \frac{\mathbf{a} \cdot \mathbf{b}}{||\mathbf{a}|| \cdot ||\mathbf{b}||}.

Here are the steps to calculate the angle:

  1. Compute the dot product of vectors a and b.
  2. Calculate the magnitudes of vectors a and b.
  3. Use the formula to find cos⁡(θ)\cos(\theta).
  4. Find the angle θ\theta using the inverse cosine function.
cos⁡(θ)=a⋅b∣∣a∣∣⋅∣∣b∣∣\cos(\theta) = \frac{\mathbf{a} \cdot \mathbf{b}}{||\mathbf{a}|| \cdot ||\mathbf{b}||}

This technique is widely used in computer vision for measuring similarity between feature vectors.

Example:

Input:
angle_between([1, 0], [0, 1])
Output:
90.00
Reasoning:

Step-by-step calculation using the angle formula:

θ=arccos⁡(a⋅b∥a∥×∥b∥)\theta = \arccos\left(\frac{\mathbf{a} \cdot \mathbf{b}}{\|\mathbf{a}\| \times \|\mathbf{b}\|}\right)

  1. Compute dot product: a⋅b=(1×0)+(0×1)=0\mathbf{a} \cdot \mathbf{b} = (1 \times 0) + (0 \times 1) = 0

  2. Compute magnitudes: ∥a∥=12+02=1\|\mathbf{a}\| = \sqrt{1^2 + 0^2} = 1 ∥b∥=02+12=1\|\mathbf{b}\| = \sqrt{0^2 + 1^2} = 1

  3. Apply the formula: cos⁡(θ)=01×1=0\cos(\theta) = \frac{0}{1 \times 1} = 0

  4. Find the angle: θ=arccos⁡(0)=90°\theta = \arccos(0) = 90°

  5. Result: 90.00°90.00°

The vectors [1,0][1,0] and [0,1][0,1] are the standard basis vectors pointing along the x and y axes respectively, which are perpendicular (90°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
solution.py

Test Results

0/0
Run code to see test results.