Vectors

object Vectors

Vector maths shared by SemanticCache and by CacheStore implementations.

kmemo works exclusively with unit-normalized vectors: once a vector has length 1, cosine similarity collapses into a plain dot product, which removes two square roots from every comparison in the search loop. CacheEntry normalizes on construction, so store implementations can safely use dot instead of cosineSimilarity.

Functions

Link copied to clipboard

Cosine similarity of a and b, in [-1.0, 1.0]. Returns 0.0 when either vector has zero magnitude, since an undefined angle is better reported as "unrelated" than as an exception in the middle of a search loop.

Link copied to clipboard

Dot product of a and b. Equals the cosine similarity only when both vectors are unit-normalized; use cosineSimilarity when that is not guaranteed.

Link copied to clipboard
fun isNormalized(vector: FloatArray, tolerance: Double = 1.0E-4): Boolean

Whether vector already has unit length, within tolerance.

Link copied to clipboard

Euclidean length of vector, accumulated in Double to limit drift on large dimensions.

Link copied to clipboard

Returns a copy of vector scaled to unit length.