You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to gonum-dev
Hello,
Im trying to find a more efficient way to calculate cosine distance between a vector and multiple vectors in a matrix.
My current implementation simply loops over the vec and the matrix vectors as below. I'm wondering whether this can be implemented more efficiently by vectorizing this calculations (I did something similar in Python but not sure how to go about this here)
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to gonum-dev
Hi,
If representing the input data as slices is enough for you, then you don't need the 'mat' package for this small calculation and you can use 'floats' instead:
func main() { // Vectors are stored in the rows of the "matrix" A. a := [][]float64{ {2, 2, -1}, {-1, 3, 1}, {0, 1, 3}, } x := []float64{3, 1, 3} normx := floats.Norm(x, 2) for _, ai := range a { normai := floats.Norm(ai, 2) cosDist := 1 - floats.Dot(ai, x)/normai/normx fmt.Println(cosDist) } }
You could "vectorize" the code a bit more by using the matrix-vector product A*x instead of 'floats.Dot' but for that you'd have to change how A is stored and switch to using the mat package throughout. At this moment without further requirements/information I don't think it's worth it.