I want to multiply a mat.Dense
Matrix with a mat.VecDense
Vector, but obviously mat.Dense
nor mat.VecDens
do not implement the Matrix interface or define a method to multiply a matrix with a vector. How would I do that?
Asked
Active
Viewed 2,803 times
1 Answers
6
Solved it.
mat.NewVecDense(...)
returns a *mat.VecDense
, that implements a method func MulVec(a mat.Matrix, b mat.Vector)
Here is a test to validate the functionality
func TestMatrixVectorMul(t *testing.T) {
a := mat.NewDense(3, 3, []float64{
1, 2, 3, 4, 5, 6, 7, 8, 9,
})
b := mat.NewVecDense(3, []float64{
1, 2, 3,
})
actual := make([]float64, 3)
c := mat.NewVecDense(3, actual)
// this was the method, I was looking for.
c.MulVec(a, b)
expected := []float64{14, 32, 50}
assert.Equal(t, expected, actual)
}