vec

package
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 3 Imported by: ? modules, 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 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