L2 Normalize

EasyMachine LearningMath

Description

Given a vector, scale it to unit L2 norm by dividing each component by the Euclidean norm of the vector. If the vector is all zeros, return all zeros. Round each result to 4 decimal places.

Examples

Input:[3,4]
Output:[0.6,0.8]
Explanation:

Dividing every component by the Euclidean norm rescales the vector to length one while keeping its direction.

Input:[1,0,0]
Output:[1,0,0]
Explanation:

Dividing every component by the Euclidean norm rescales the vector to length one while keeping its direction.

Input:[1,1,1,1]
Output:[0.5,0.5,0.5,0.5]
Explanation:

Dividing every component by the Euclidean norm rescales the vector to length one while keeping its direction.

Constraints

  • 1 ≤ length ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.