vector

package
v1.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 3 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package vector provides vector math operations for NornicDB.

This package consolidates all vector similarity and distance calculations used throughout the codebase. Use these functions instead of implementing your own to ensure consistency and correctness.

Main Functions:

  • CosineSimilarity: Standard similarity for float32 vectors (most common)
  • CosineSimilarityFloat64: High-precision similarity for float64 vectors
  • CosineSimilaritySIMD: SIMD-accelerated similarity for high-throughput
  • DotProduct: Dot product for float32 vectors
  • EuclideanSimilarity: Distance-based similarity
  • Normalize: Returns normalized copy of vector
  • NormalizeInPlace: Normalizes vector in-place (modifies input)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CosineSimilarity

func CosineSimilarity(a, b []float32) float64

CosineSimilarity calculates cosine similarity between two float32 vectors. Returns value in range [-1, 1] where 1 = identical, 0 = orthogonal, -1 = opposite.

This is the STANDARD implementation for all non-GPU code. Uses float64 accumulation for high precision, even with float32 inputs.

Example:

a := []float32{1.0, 2.0, 3.0}
b := []float32{4.0, 5.0, 6.0}
sim := CosineSimilarity(a, b)  // Returns 0.9746318461970762

func CosineSimilarityFloat64

func CosineSimilarityFloat64(a, b []float64) float64

CosineSimilarityFloat64 calculates cosine similarity between two float64 vectors. Returns value in range [-1, 1] where 1 = identical, 0 = orthogonal, -1 = opposite.

Use this when working with float64 vectors directly.

Example:

a := []float64{1.0, 2.0, 3.0}
b := []float64{4.0, 5.0, 6.0}
sim := CosineSimilarityFloat64(a, b)  // Returns 0.9746318461970762

func CosineSimilarityGPU

func CosineSimilarityGPU(a, b []float32) float32

CosineSimilarityGPU calculates cosine similarity optimized for GPU operations. Returns float32 for GPU compatibility.

Uses SIMD-accelerated implementation for maximum throughput. Slightly less accurate than CosineSimilarity() due to float32 accumulation.

Use this for high-throughput batch operations.

func CosineSimilaritySIMD

func CosineSimilaritySIMD(a, b []float32) float32

CosineSimilaritySIMD calculates cosine similarity using SIMD acceleration. Returns float32 for compatibility with embedding operations.

This is the fastest implementation for high-throughput similarity searches. Uses platform-specific SIMD instructions (AVX2 on x86, NEON on ARM).

On Apple Silicon: ~4-8x faster than scalar loop On x86 with AVX2: ~10x faster than scalar loop

Example:

query := []float32{0.1, 0.2, 0.3, ...}  // 1536-dim embedding
doc := []float32{0.4, 0.5, 0.6, ...}
sim := CosineSimilaritySIMD(query, doc)  // Fast similarity calculation

func DotProduct

func DotProduct(a, b []float32) float64

DotProduct calculates the dot product of two float32 vectors. Returns float64 for API compatibility.

Uses SIMD acceleration internally for maximum throughput. For normalized vectors, dot product equals cosine similarity.

Example:

a := []float32{1.0, 2.0, 3.0}
b := []float32{4.0, 5.0, 6.0}
dot := DotProduct(a, b)  // Returns 32.0

func DotProductSIMD

func DotProductSIMD(a, b []float32) float32

DotProductSIMD calculates the dot product using SIMD acceleration. Returns float32 for maximum performance in hot paths.

This is the fastest implementation for normalized vector comparisons. Use this in tight loops where float32 precision is acceptable.

Example:

query := []float32{0.1, 0.2, 0.3, ...}
doc := []float32{0.4, 0.5, 0.6, ...}
sim := DotProductSIMD(query, doc)

func EuclideanDistanceSIMD

func EuclideanDistanceSIMD(a, b []float32) float32

EuclideanDistanceSIMD calculates Euclidean distance using SIMD acceleration. Returns float32 for maximum performance in hot paths.

Example:

a := []float32{0, 0}
b := []float32{3, 4}
dist := EuclideanDistanceSIMD(a, b)  // Returns 5.0

func EuclideanSimilarity

func EuclideanSimilarity(a, b []float32) float64

EuclideanSimilarity calculates similarity based on Euclidean distance. Returns value in range [0, 1] where 1 = identical, 0 = very different.

Uses SIMD acceleration internally for the distance calculation. Formula: 1 / (1 + distance)

Example:

a := []float32{1.0, 2.0, 3.0}
b := []float32{4.0, 5.0, 6.0}
sim := EuclideanSimilarity(a, b)  // Returns ~0.161

func EuclideanSimilarityFloat64

func EuclideanSimilarityFloat64(a, b []float64) float64

EuclideanSimilarityFloat64 calculates similarity for float64 vectors.

func Neo4jCosineSimilarity

func Neo4jCosineSimilarity[T float32 | float64](a, b []T) (float64, bool)

Neo4jCosineSimilarity is Neo4j's COSINE similarity of a and b, which must have the same length; ok is false when either is invalid.

func Neo4jCosineVectorValid

func Neo4jCosineVectorValid[T float32 | float64](v []T) bool

Neo4jCosineVectorValid reports whether v is a valid vector for Neo4j's COSINE similarity: not empty, finite coordinates, a positive and finite L2 norm.

func Neo4jEuclideanSimilarity

func Neo4jEuclideanSimilarity[T float32 | float64](a, b []T) (float64, bool)

Neo4jEuclideanSimilarity is Neo4j's EUCLIDEAN similarity of a and b, which must have the same length; ok is false when either is invalid.

func Neo4jEuclideanVectorValid

func Neo4jEuclideanVectorValid[T float32 | float64](v []T) bool

Neo4jEuclideanVectorValid reports whether v is a valid vector for Neo4j's EUCLIDEAN similarity: not empty, every coordinate finite as a float32.

func Normalize

func Normalize(vec []float32) []float32

Normalize returns a normalized copy of the vector. The input vector is not modified (immutable operation).

Uses SIMD acceleration for the norm calculation.

Example:

original := []float32{3.0, 4.0}
normalized := Normalize(original)  // Returns [0.6, 0.8]
// original is unchanged

func NormalizeInPlace

func NormalizeInPlace(v []float32)

NormalizeInPlace normalizes a vector in-place (modifies the input). After normalization, the vector has unit length (magnitude = 1).

Uses SIMD acceleration for the norm calculation. WARNING: Modifies the input slice. Use Normalize() to preserve original.

Example:

v := []float32{3.0, 4.0}
NormalizeInPlace(v)  // v is now [0.6, 0.8]

Types

type Neo4jCosineQuery

type Neo4jCosineQuery struct {
	// contains filtered or unexported fields
}

Neo4jCosineQuery is a query vector prepared for Neo4j's COSINE similarity against many candidates: L2-normalized once (in float64, then as float32 coordinates), so each candidate costs only its own normalization and the dot product. Scores are Neo4jCosineSimilarity's, bit for bit.

func NewNeo4jCosineQuery

func NewNeo4jCosineQuery[T float32 | float64](q []T) (Neo4jCosineQuery, bool)

NewNeo4jCosineQuery prepares q; ok is false when q isn't a valid COSINE vector.

func (Neo4jCosineQuery) Similarity

func (q Neo4jCosineQuery) Similarity(candidate []float32) (float64, bool)

Similarity is the COSINE similarity of the query and candidate; ok is false when candidate has another length or isn't a valid COSINE vector.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL