【问题标题】:Dot multiply between a matrix and column vector in C#C#中矩阵和列向量之间的点乘
【发布时间】:2020-12-15 12:22:03
【问题描述】:

我正在尝试在 C# 中的矩阵 4x4 和向量 4x1 之间进行 dot 乘法。 如果我尝试使用using MathNet.Numerics.LinearAlgebra;

Matrix<double>.op_DotMultiply(matrixABuilder.DenseOfArray(A), matrixPBuilder.DenseOfArray(P));

其中A4x4P 的矩阵,定义为4x1,我收到下一个错误>
System.ArgumentException: 'Matrix dimensions must agree: op1 is 4x4, op2 is 4x1. (Parameter 'other')'

如果我从矩阵P 转换为向量然后计算(现在Pdouble[] 的向量)>

Matrix&lt;double&gt;.op_DotMultiply(matrixA.DenseOfArray(A),vectorPBuilder.Dense(P));

然后我得到错误

Cannot convert from 'MathNet.Numerics.LinearAlgebra.Vector&lt;double&gt;' to 'MathNet.Numerics.LinearAlgebra.Matrix&lt;double&gt;'

我必须如何获得这种乘法的结果,才能获得4x1 数据的数组? (在 Python 中 numpy.dot() 工作正常,在 C# 中则不行)。

【问题讨论】:

    标签: c#


    【解决方案1】:

    Dot 乘法需要大小相等的Matrixes 并计算单个双精度值。
    看来您实际上需要将矩阵乘以向量。
    P 转换为Vector 并简单地使用* 进行乘法运算:

    matrixABuilder.DenseOfArray(A) * vectorPBuilder.Dense(P)
    

    【讨论】:

    • 那么在这种情况下,当我们想将矩阵与向量相乘时,点积和简单的乘法一样吗?
    • 是的,如果你想 dot 在 4x1 上乘以 4x4 并得到 4x1 - 这只是向量乘法的矩阵。
    • 你知道当我有 4x4 和 4x2 的乘法时我应该怎么做吗?
    猜你喜欢
    • 1970-01-01
    • 2021-08-17
    • 2012-11-24
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 2021-01-20
    相关资源
    最近更新 更多