package chromem import "math" func cosine_similarities(a, b [][]float32) [][]float32 { result := make([][]float32, len(a)) for iX, x := range a { result[iX] = make([]float32, len(b)) for iY, y := range b { result[iX][iY] = cosine_similarity(x, y) } } return result } func cosine_similarity(x, y []float32) float32 { var sum, s1, s2 float64 for i := 0; i < len(x); i++ { sum += float64(x[i] * y[i]) s1 += math.Pow(float64(x[i]), 2) s2 += math.Pow(float64(y[i]), 2) } if s1 == 0 || s2 == 0 { return 0.0 } return float32(sum / (math.Sqrt(s1) * math.Sqrt(s2))) }