2

I have to port some python (numpy) code in c# (MathNet). I can write in python:

mtx = np.array([[0,1,2],[3,4,5]])
mtx[0,:] *= 1.23    #multiply all elements in row 0 by 1.23

How can I do this in MathNet? Is there better (faster) solution than:

 Matrix<double> mtx = Matrix<double>.Build.Dense(2,3);
 //...
 for(int i = 0; i < mtx.ColumnCount; i++)
    mtx[0,i] *= 1.23;

?

TylerH
  • 20,799
  • 66
  • 75
  • 101
ThWin
  • 33
  • 4

2 Answers2

5

For completeness: Math.NET Numerics itself actually does support a notation which is somewhat close to your NumPy example. C# does not support it, but other more powerful .Net languages like F# do:

let mtx = matrix [[0.;1.;2.];[3.;4.;5.]]
mtx.[0,*] <- 1.23 * mtx.[0,*]
Christoph Rüegg
  • 4,626
  • 1
  • 20
  • 34
2

There are few ways, for sure cleaner than for. Starting with matrix full of 1.

 Matrix<double> mtx = Matrix<double>.Build.Dense(2, 3, 1);
 
 mtx.SetRow(0, mtx.Row(0).Multiply(1.23));

 Console.WriteLine(mtx);

returns

DenseMatrix 2x3-Double

1,23 1,23 1,23

1 1 1

Community
  • 1
  • 1
Piotr Leniartek
  • 1,177
  • 2
  • 14
  • 33