INNER CODE UNIT · Go
BestDotProduct
takara-ai/go-attention · attention/attention.go:33
func BestDotProduct(v1, v2 Vector) (float64, error) {
return DotProduct(v1, v2)
}
// bestDotProduct is the single, canonical implementation
func bestDotProduct(v1, v2 Vector) (float64, error) {
if len(v1) != len(v2) {
return 0, fmt.Errorf("vector dimensions mismatch: %d != %d", len(v1), len(v2))
}
// Loop unrolling for performance
sum := 0.0
n := len(v1)
for i := 0; i < n-7; i += 8 {
sum += v1[i]*v2[i] + v1[i+1]*v2[i+1] + v1[i+2]*v2[i+2] + v1[i+3]*v2[i+3] +
v1[i+4]*v2[i+4] + v1[i+5]*v2[i+5] + v1[i+6]*v2[i+6] + v1[i+7]*v2[i+7]
}
for i := (n/8)*8; i < n; i++ {
sum += v1[i] * v2[i]