Normalize 3D Vectors
Normalize 3D Vectors
Given 3D vector(s) v, return the unit vector(s). Handle zero vectors by returning a zero vector (no division by zero).
Unit Vector Formula:
v^=∥v∥vFor zero vectors (∥v∥=0), return unchanged (all zeros)
Examples
Input: v = [3, 4, 0]
Output: [0.6, 0.8, 0.0]
Input: v = [[0, 0, 0], [1, 2, 2]]
Output: [[0, 0, 0], [0.333, 0.667, 0.667]] (approx)
Hint 1
Compute norms and check if norm >10-10 before dividing to avoid division by zero.
Hint 2
For batch, use keepdims=True when computing norms to maintain shape for broadcasting.
Requirements
- Accept single vector: (3,) or list of length 3
- Accept batch: (N,3)
- Return same shape as input, always
np.ndarrayof dtype float - For non-zero vectors, output should have norm ≈1.0
- For zero vectors, return zeros (unchanged)
- Must be vectorized (no Python loops over N)
Constraints
- Norm comparisons within tolerance 10−6
- NumPy only; time limit: 200ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Normalize 3D Vectors
Normalize 3D Vectors
Given 3D vector(s) v, return the unit vector(s). Handle zero vectors by returning a zero vector (no division by zero).
Unit Vector Formula:
v^=∥v∥vFor zero vectors (∥v∥=0), return unchanged (all zeros)
Examples
Input: v = [3, 4, 0]
Output: [0.6, 0.8, 0.0]
Input: v = [[0, 0, 0], [1, 2, 2]]
Output: [[0, 0, 0], [0.333, 0.667, 0.667]] (approx)
Hint 1
Compute norms and check if norm >10-10 before dividing to avoid division by zero.
Hint 2
For batch, use keepdims=True when computing norms to maintain shape for broadcasting.
Requirements
- Accept single vector: (3,) or list of length 3
- Accept batch: (N,3)
- Return same shape as input, always
np.ndarrayof dtype float - For non-zero vectors, output should have norm ≈1.0
- For zero vectors, return zeros (unchanged)
- Must be vectorized (no Python loops over N)
Constraints
- Norm comparisons within tolerance 10−6
- NumPy only; time limit: 200ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array