如何在 gonum 中将矩阵与向量相乘?

我想将mat.Dense矩阵与mat.VecDense向量相乘,但显然mat.Dense 也mat.VecDens没有实现矩阵接口或定义将矩阵与向量相乘的方法。我该怎么做?



茅侃侃
浏览 82回答 1
1回答

BIG阳

解决了。mat.NewVecDense(...)返回一个*mat.VecDense,它实现了一个方法func MulVec(a mat.Matrix, b mat.Vector)这是验证功能的测试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)}
打开App,查看更多内容
随时随地看视频慕课网APP