Angle Between 3D Vectors
Angle Between 3D Vectors
Compute the angle (in radians) between two 3D vectors v and w. Return θ∈[0,π] Handle zero vectors gracefully.
Angle Formula:
cosθ=∥v∥∥w∥v⋅wθ=arccos(cosθ)Examples
Input: v = [1, 0, 0], w = [0, 1, 0]
Output: ≈ 1.571 (π/2)
Input: v = [1, 2, 3], w = [2, 4, 6]
Output: ≈ 0.0
Hint 1
Check if either norm <10−10, if so return np.nan.
Hint 2
Use np.clip() before np.arccos() to avoid numerical errors.
Requirements
- Inputs: v,w as lists or arrays of shape (3,)
- Return scalar float: angle in radians
- Use dot product and norms
- Clamp cosine value to [−1,1] before
arccos - If either vector has zero norm: return
np.nan
Constraints
- Absolute error tolerance: 10−6
- NumPy only; time limit: 200ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: array
Angle Between 3D Vectors
Angle Between 3D Vectors
Compute the angle (in radians) between two 3D vectors v and w. Return θ∈[0,π] Handle zero vectors gracefully.
Angle Formula:
cosθ=∥v∥∥w∥v⋅wθ=arccos(cosθ)Examples
Input: v = [1, 0, 0], w = [0, 1, 0]
Output: ≈ 1.571 (π/2)
Input: v = [1, 2, 3], w = [2, 4, 6]
Output: ≈ 0.0
Hint 1
Check if either norm <10−10, if so return np.nan.
Hint 2
Use np.clip() before np.arccos() to avoid numerical errors.
Requirements
- Inputs: v,w as lists or arrays of shape (3,)
- Return scalar float: angle in radians
- Use dot product and norms
- Clamp cosine value to [−1,1] before
arccos - If either vector has zero norm: return
np.nan
Constraints
- Absolute error tolerance: 10−6
- NumPy only; time limit: 200ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: array