vec

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package vec encodes stored vectors and computes exact similarity scores.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(data []byte) ([]float32, error)

Decode strictly validates the header, exact payload length, and finite values. The returned vector owns its memory.

func Encode

func Encode(vector []float32) ([]byte, error)

Encode serializes finite, nonempty float32 vectors without metric normalization.

func Score

func Score(query, vector []float32, metric Metric) (float64, error)

Score computes cosine similarity, dot product, or negative squared L2 distance. Arithmetic is float64, including multiplication and subtraction, to avoid float32 overflow and underflow for finite float32 inputs.

func Validate

func Validate(vector []float32, metric Metric) error

Validate rejects unsupported metrics, empty or nonfinite vectors, and zero vectors for cosine similarity. Zero vectors are valid for dot product and L2.

Types

type Match

type Match struct {
	Number uint64
	Score  float64
}

Match identifies a segment-local document and its similarity score.

type Metric

type Metric string

Metric selects a similarity function. All scores are higher-is-better.

const (
	Cosine     Metric = "cosine"
	DotProduct Metric = "dot_product"
	L2         Metric = "l2"
)

Jump to

Keyboard shortcuts

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