vector

package
v0.25.2 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const MaxDim = 16000

MaxDim is the maximum number of dimensions a vector can have.

Variables

View Source
var (
	ErrInvalidLengthVector        = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowVector          = fmt.Errorf("proto: integer overflow")
	ErrUnexpectedEndOfGroupVector = fmt.Errorf("proto: unexpected end of group")
)

Functions

func CosDistance

func CosDistance(t T, t2 T) (float64, error)

CosDistance returns the cosine distance between t and t2. This represents the similarity between the two vectors, ranging from 0 (most similar) to 2 (least similar). Only the angle between the vectors matters; the norms (magnitudes) are irrelevant.

func Encode

func Encode(appendTo []byte, t T) ([]byte, error)

Encode encodes the vector as a byte array suitable for storing in KV.

func InnerProduct

func InnerProduct(t T, t2 T) (float64, error)

InnerProduct returns the inner product of t1 and t2.

func L1Distance

func L1Distance(t T, t2 T) (float64, error)

L1Distance returns the L1 (Manhattan) distance between t and t2.

func L2Distance

func L2Distance(t T, t2 T) (float64, error)

L2Distance returns the Euclidean distance between t and t2.

func NegInnerProduct

func NegInnerProduct(t T, t2 T) (float64, error)

NegInnerProduct returns the negative inner product of t1 and t2.

func Norm

func Norm(t T) float64

Norm returns the L2 norm of t.

Types

type Set

type Set struct {
	// Dims is the number of dimensions of each vector in the set.
	Dims int `protobuf:"varint,1,opt,name=dims,proto3,casttype=int" json:"dims,omitempty"`
	// Count is the number of vectors in the set.
	Count int `protobuf:"varint,2,opt,name=count,proto3,casttype=int" json:"count,omitempty"`
	// Data is a float32 slice that contains all vectors, laid out contiguously in
	// row-wise order in memory.
	// NB: Avoid using this field directly, instead preferring to use the At
	// function to access individual vectors.
	Data []float32 `protobuf:"fixed32,3,rep,packed,name=data,proto3" json:"data,omitempty"`
}

Set is a set of float32 vectors of equal dimension. Vectors in the set are stored contiguously in a slice, in row-wise order. They are assumed to be unordered; some methods do not preserve ordering.

func MakeSet

func MakeSet(dims int) Set

MakeSet constructs a new empty vector set with the given number of dimensions. New vectors can be added via the Add or AddSet methods.

func MakeSetFromRawData

func MakeSetFromRawData(data []float32, dims int) Set

MakeSetFromRawData constructs a new vector set from a raw slice of vectors. The vectors in the slice have the given number of dimensions and are laid out contiguously in row-wise order. NB: The data slice is directly used rather than copied; do not use it outside the context of this vector set after this point.

func (*Set) Add

func (vs *Set) Add(v T)

Add appends a new vector to the set.

func (*Set) AddSet

func (vs *Set) AddSet(vectors Set)

AddSet appends all vectors from the given set to this set.

func (*Set) AddUndefined

func (vs *Set) AddUndefined(count int)

AddUndefined adds the given number of vectors to this set. The vectors should be set to defined values before use.

func (*Set) AsMatrix

func (vs *Set) AsMatrix() num32.Matrix

AsMatrix returns this set as a matrix, to be used with the num32 package. NB: The underlying float32 slice is shared by the resulting matrix, so any changes will affect both the vector set and matrix.

func (*Set) At

func (vs *Set) At(offset int) T

At returns the vector at the given offset in the set. The returned vector is intended for transient use, since mutations to the vector set can invalidate the reference.

func (*Set) Centroid

func (vs *Set) Centroid(centroid T) T

Centroid calculates the mean of each dimension across all vectors in the set and writes the resulting averages to the provided centroid slice. The slice must be pre-allocated by the caller, with its length set to the number of dimensions (Dims field) of the vectors in the set.

func (*Set) Clear

func (vs *Set) Clear()

Clear empties the set so that it has zero vectors.

func (*Set) Clone

func (vs *Set) Clone() Set

Clone performs a deep copy of the vector set. Changes to the original or clone will not affect the other.

func (*Set) Descriptor

func (*Set) Descriptor() ([]byte, []int)

func (*Set) EnsureCapacity

func (vs *Set) EnsureCapacity(capacity int)

EnsureCapacity grows the underlying data slice if needed to ensure the requested capacity. This is useful to prevent unnecessary resizing when it's known up-front how big the vector set will need to get.

func (*Set) Equal

func (vs *Set) Equal(other *Set) bool

Equal returns true if this set is equal to the other set. Two sets are equal if they have the same number of dimensions, the same number of vectors, and the same values in the same order.

func (*Set) Marshal

func (m *Set) Marshal() (dAtA []byte, err error)

func (*Set) MarshalTo

func (m *Set) MarshalTo(dAtA []byte) (int, error)

func (*Set) MarshalToSizedBuffer

func (m *Set) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (*Set) ProtoMessage

func (*Set) ProtoMessage()

func (*Set) ReplaceWithLast

func (vs *Set) ReplaceWithLast(offset int)

ReplaceWithLast removes the vector at the given offset from the set, replacing it with the last vector in the set. The modified set has one less element and the last vector's position changes.

func (*Set) Reset

func (m *Set) Reset()

func (*Set) Size

func (m *Set) Size() (n int)

func (*Set) Slice

func (vs *Set) Slice(offset, count int) Set

Slice returns a vector set that contains a subset of "count" vectors, starting at the given offset.

NOTE: Slice returns a set that references the same memory as this set. Modifications to one set may be visible in the other set, so callers should typically ensure that both sets are immutable after calling Slice.

func (*Set) SplitAt

func (vs *Set) SplitAt(offset int) Set

SplitAt divides the vector set into two subsets at the given offset. This vector set is updated to contain only the vectors before the split point, and the returned set contains only the vectors on or after the split point.

func (*Set) String

func (m *Set) String() string

func (*Set) Unmarshal

func (m *Set) Unmarshal(dAtA []byte) error

func (*Set) XXX_DiscardUnknown

func (m *Set) XXX_DiscardUnknown()

func (*Set) XXX_Marshal

func (m *Set) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Set) XXX_Merge

func (m *Set) XXX_Merge(src proto.Message)

func (*Set) XXX_Size

func (m *Set) XXX_Size() int

func (*Set) XXX_Unmarshal

func (m *Set) XXX_Unmarshal(b []byte) error

type T

type T []float32

T is the type of a PGVector-like vector.

func Add

func Add(t T, t2 T) (T, error)

Add returns t+t2, pointwise.

func Decode

func Decode(b []byte) (remaining []byte, ret T, err error)

Decode decodes the byte array into a vector and returns any remaining bytes.

func Minus

func Minus(t T, t2 T) (T, error)

Minus returns t-t2, pointwise.

func Mult

func Mult(t T, t2 T) (T, error)

Mult returns t*t2, pointwise.

func ParseVector

func ParseVector(input string) (T, error)

ParseVector parses the Postgres string representation of a vector.

func Random

func Random(rng *rand.Rand, maxDim int) T

Random returns a random vector with the number of dimensions in [1, maxDim] range.

func (T) AsSet

func (v T) AsSet() Set

AsSet returns this vector a set of one vector.

func (T) Compare

func (v T) Compare(v2 T) (int, error)

Compare returns -1 if v < v2, 1 if v > v2, and 0 if v == v2.

func (T) Size

func (v T) Size() uintptr

Size returns the size of the vector in bytes.

func (T) String

func (v T) String() string

String implements the fmt.Stringer interface.

Jump to

Keyboard shortcuts

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