Embeddings 0.6.0 vectormath dot - CyrilB1531/lodestar GitHub Wiki
Lodestar.Embeddings 0.6.0. This page is frozen at that release. Read the current documentation for what
mainsays now. A link to a decision or a migration page followsmain, and leaves the archive.
The dot product of two equal-length vectors.
public static float Dot(ReadOnlySpan<float> a, ReadOnlySpan<float> b)Parameters — a and b are the two vectors. They must be the same length; a float[]
converts implicitly, so nothing is allocated to pass one.
Returns — float, the sum of the element-wise products. Order of the arguments does not
change it.
Exceptions — ArgumentException when a and b differ in length. There is no silent
truncation to the shorter of the two.
Example — the textbook product, and the same call standing in for cosine similarity on unit vectors.
using Lodestar.Embeddings.Search;
float plain = VectorMath.Dot(new float[] { 1f, 2f, 3f }, new float[] { 4f, 5f, 6f }); // => 32
float orthogonal = VectorMath.Dot(new float[] { 1f, 0f }, new float[] { 0f, 1f }); // => 0Remarks — on two L2-normalized vectors this is cosine similarity: 1 for the same
direction, 0 for perpendicular, -1 for opposite. That identity is the whole reason
EmbeddingIndex normalizes on insertion — it turns a similarity metric into
one multiply-accumulate loop.
On unnormalized vectors it is not a similarity, because length contributes. A long vector scores high against everything.
The accumulation order differs between the two target frameworks, which
VectorMath explains: the SIMD path on net10.0 sums lane-wise and the
netstandard2.0 scalar loop sums left to right, so long vectors can disagree in the last bits.
Applies to — net10.0, netstandard2.0.
See also — VectorMath.L2Norm,
EmbeddingIndex.Search, the search index.