Skip to content

Embeddings vectormath l2norm

github-actions[bot] edited this page Aug 27, 2026 · 25 revisions

VectorMath.L2Norm

The Euclidean length of a vector.

public static float L2Norm(ReadOnlySpan<float> v)

Parametersv is the vector to measure. A float[] converts implicitly.

Returnsfloat, the square root of the sum of squares. Never negative; 0 only for the all-zero vector.

Exceptions — none for a well-formed vector.

Example — the 3-4-5 triangle, and a unit vector proving itself one.

using Lodestar.Embeddings.Search;

float length = VectorMath.L2Norm(new float[] { 3f, 4f });  // => 5
float unit = VectorMath.L2Norm(new float[] { 0f, 1f });  // => 1

Remarks — this is Dot(v, v) under a square root, and it is implemented as exactly that, so it inherits Dot's SIMD path and its accumulation order.

Dividing a vector by its norm is what makes it a unit vector, which is what makes VectorMath.Dot a cosine similarity. EmbeddingIndex does that for you on insertion unless told not to — there is rarely a reason to normalize by hand before adding.

A zero vector has norm 0, and dividing by it is undefined. The index handles this by leaving such a vector unnormalized rather than producing NaN; a caller normalizing by hand has to make the same decision.

Applies to — net10.0, netstandard2.0.

See alsoVectorMath.Dot, VectorMath, the search index.

Lodestar

Project

Clone this wiki locally