mirror of
https://github.com/Warky-Devs/vecna.git
synced 2026-05-05 01:26:58 +00:00
feat: 🎉 Vectors na Vectors, the begining
Translate 1536 <-> 768 , 3072 <-> 2048
This commit is contained in:
32
pkg/adapter/projection.go
Normal file
32
pkg/adapter/projection.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package adapter
|
||||
|
||||
import "fmt"
|
||||
|
||||
type projectionAdapter struct {
|
||||
sourceDim int
|
||||
targetDim int
|
||||
matrix [][]float32
|
||||
}
|
||||
|
||||
func (a *projectionAdapter) SourceDim() int { return a.sourceDim }
|
||||
func (a *projectionAdapter) TargetDim() int { return a.targetDim }
|
||||
|
||||
func (a *projectionAdapter) Adapt(vec []float32) ([]float32, error) {
|
||||
if len(vec) != a.sourceDim {
|
||||
return nil, fmt.Errorf("projection adapt: %w: got %d, want %d", ErrDimMismatch, len(vec), a.sourceDim)
|
||||
}
|
||||
return L2Norm(matVecMul(a.matrix, vec)), nil
|
||||
}
|
||||
|
||||
// matVecMul computes m·v where m is [rows][cols] and v has len cols.
|
||||
func matVecMul(m [][]float32, v []float32) []float32 {
|
||||
out := make([]float32, len(m))
|
||||
for i, row := range m {
|
||||
var sum float32
|
||||
for j, val := range row {
|
||||
sum += val * v[j]
|
||||
}
|
||||
out[i] = sum
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user