 # Cannot find a Hadamard operation between a vector and a matrix that return a matrix using C#

I can do the following using numpy:
a = np.array([[1,2],[3,4],[5,6]]) # a 3x2 matrix
b = np.array([1, 2, 3]) # a 1x3 array
c = np.multiply(a.T,b)

[[ 1 2]
[ 6 8]
[15 18]]

Why cant I do the same Hadamard operation between a vector and a matrix using Math.Net with C#?

So far as I know, you have to do it column by column:

``````        var M = Matrix<double>.Build;
var V = Vector<double>.Build;

double[,] x = {{ 1.0, 2.0 },
{ 3.0, 4.0 },
{ 5.0, 6.0 } };
var m = M.DenseOfArray(x);

double[] y = { 1.0, 2.0, 3.0 };
var v = V.DenseOfArray(y);

for (int ic=0;ic<m.ColumnCount;ic++) m.SetColumn(ic, m.Column(ic).PointwiseMultiply(v));

Console.WriteLine(m);``````

We do support the Hadamard product: `PointwiseMultiply`.

For your example where the dimensions don’t match exactly, the Hadamard product is actually undefined. I guess it is a convenience feature of numpy to allow it anyway, assuming the single-row or single-column matrix to be repeated. Currently the matrix-`PointwiseMultiply` requires dimensions to match exactly and thus does not support this.

I guess we could extend it to allow them to either match exactly or be exactly 1, in which case it would assume them to be repeated. If one of them is a 1x1 matrix it could do a scalar multiplication. This way you could write this in C# as `m.PointwiseMultiply(v.ToColumnMatrix())`, and in F# as `m .* (v.ToColumnMatrix())`.

We still need to convert to a column or row matrix since the vector is undirected.