duanqiao2225
2018-10-12 10:31
浏览 383
已采纳

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

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?

图片转代码服务由CSDN问答提供 功能建议

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douya2006 2018-10-12 11:18
    已采纳

    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)
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题