TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Problems
Loading...
1 / 1

Normalize 3D Vectors

3D Geometry
Easy

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

Unit Vector Formula:

v^=v∥v∥\hat{v} = \frac{v}{\lVert v \rVert} v^=∥v∥v​

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
Vector Norm 3dAngle Between 3dDot ProductRotate Around ZHomogeneous Transform

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.
PrevNext

Normalize 3D Vectors

3D Geometry
Easy

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

Unit Vector Formula:

v^=v∥v∥\hat{v} = \frac{v}{\lVert v \rVert} v^=∥v∥v​

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
Vector Norm 3dAngle Between 3dDot ProductRotate Around ZHomogeneous Transform

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.