Skip to main content

normalize

Function normalize 

Source
pub fn normalize(vector: &mut [f32])
Expand description

Stable: external consumers may depend on this; breaking changes require a SemVer bump.

Normalize a vector to unit length (L2 normalization).

Modifies the vector in place. After normalization, the vector will have magnitude 1.0.

ยงExample

use lattice_embed::utils::normalize;

let mut v = vec![3.0, 4.0];
normalize(&mut v);

let magnitude: f32 = v.iter().map(|x| x * x).sum::<f32>().sqrt();
assert!((magnitude - 1.0).abs() < 0.0001);