Dot multiply between a matrix and column vector in C#

Viewed 236

I'm trying to do a dot multiplication between a matrix 4x4 and a vector 4x1 in C#. If I try with using MathNet.Numerics.LinearAlgebra;

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

Where A is a matrix of 4x4 and P defined as 4x1, I'm getting the next error>
System.ArgumentException: 'Matrix dimensions must agree: op1 is 4x4, op2 is 4x1. (Parameter 'other')'

If I convert from matrix P to a vector and then calculate (now P is a vector of double[])>

Matrix<double>.op_DotMultiply(matrixA.DenseOfArray(A),vectorPBuilder.Dense(P));

then I get the error that

Cannot convert from 'MathNet.Numerics.LinearAlgebra.Vector<double>' to 'MathNet.Numerics.LinearAlgebra.Matrix<double>'

How I must get an result for this type of multiplication, to get as result an array of 4x1 data? (In Python the numpy.dot() is working fine, in C# not).

1 Answers

Dot multiplication needs equal sized Matrixes and computes single double value.
It seems that you need actually multiplication of matrix to vector.
Convert P to Vector and simply use * for multiplication:

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