Problems
Loading...
1 / 1

Normalize 3D Vectors

3D Geometry
Easy

Given 3D vector(s) vv, return the unit vector(s). Handle zero vectors by returning a zero vector (no division by zero).

Unit Vector Formula:

v^=vv\hat{v} = \frac{v}{\lVert v \rVert}

For zero vectors (∥v∥=0), return unchanged (all zeros)

Loading visualization...

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.ndarray of 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