Compute 3D Vector Norm
Compute 3D Vector Norm
Given a 3D vector 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).
Euclidean Norm Formula:
∥v∥=x2+y2+z2Examples
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]
- Accept batch of vectors: shape (N,3)
- Return scalar float for single vector
- Return NumPy array of shape (N,) for batch
- Must be vectorized (no Python loops over N)
Constraints
- Batch size N≤10⁵
- NumPy only; time limit: 200ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Compute 3D Vector Norm
Compute 3D Vector Norm
Given a 3D vector 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).
Euclidean Norm Formula:
∥v∥=x2+y2+z2Examples
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]
- Accept batch of vectors: shape (N,3)
- Return scalar float for single vector
- Return NumPy array of shape (N,) for batch
- Must be vectorized (no Python loops over N)
Constraints
- Batch size N≤10⁵
- NumPy only; time limit: 200ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array