The pgvector package provides types for different vector formats supported by PostgreSQL.
Standard Vectors
Create a standard vector from a []float32:
vec := pgvector.NewVector([]float32{1, 2, 3})
Half Vectors
Create a half vector (16-bit floats) from a []float32:
vec := pgvector.NewHalfVector([]float32{1, 2, 3})
Sparse Vectors
Create a sparse vector using a slice or a map of non-zero elements. Note that indices start at 0.
- From slice:
pgvector.NewSparseVector([]float32{1, 0, 2, 0, 3, 0}) - From map:
pgvector.NewSparseVectorFromMap(map[int32]float32{0: 1, 2: 2, 4: 3}, 6)
Common Methods
vec.Slice(): Returns the underlying slice.vec.Dimensions(): Returns the number of dimensions.vec.Indices(): Returns the indices of non-zero elements (for sparse vectors).vec.Values(): Returns the values of non-zero elements (for sparse vectors).
// Standard Vector
vec := pgvector.NewVector([]float32{1, 2, 3})
slice := vec.Slice()
// Half Vector
hvec := pgvector.NewHalfVector([]float32{1, 2, 3})
// Sparse Vector
elements := map[int32]float32{0: 1, 2: 2, 4: 3}
svec := pgvector.NewSparseVectorFromMap(elements, 6)
indices := svec.Indices()
values := svec.Values()