Problems
Loading...
1 / 1

Compute 3D Vector Norm

3D Geometry
Easy

Given a 3D vector v=(x,y,z)v=(x,y,z), compute its Euclidean norm. You'll write a function that works on single 3D vectors or a batch of 3D vectors with shape (N,3)(N,3).

Euclidean Norm Formula:

v=x2+y2+z2\lVert v \rVert = \sqrt{x^{2} + y^{2} + z^{2}}
Loading visualization...

Examples

Input: v = [3, 4, 12]

Output: 13.0

Input: v = [[1, 0, 0], [0, 3, 4]]

Output: [1.0, 5.0] (shape = (2,))

Hint 1

Use np.asarray() to convert input and check v.ndim to distinguish single vector from batch.

Hint 2

For batch: use axis=1 with np.sum(v**2, axis=1) to compute norm for each vector.

Requirements

  • Accept single vector: shape (3,)(3,) or list [x,y,z][x,y,z]
  • Accept batch of vectors: shape (N,3)(N,3)
  • Return scalar float for single vector
  • Return NumPy array of shape (N,)(N,) for batch
  • Must be vectorized (no Python loops over N)

Constraints

  • Batch size N≤10⁵
  • NumPy only; time limit: 200ms
Try Similar Problems