2016-05-19 12 views
1

Ich muss einige Python (numpy) Code in C# (MathNet) portieren. Ich kann in Python schreiben:Was ist das MathNet-Äquivalent von numpy Array [0 ,:] * = 1.23

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

Wie kann ich dies tun in MathNet? Gibt es eine bessere (schnellere) Lösung als:

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

?

Antwort

1

Es gibt einige Möglichkeiten, für sauberer als for. Beginnend mit Matrix voller 1.

Matrix<double> mtx = Matrix<double>.Build.Dense(2, 3, 1); 

mtx.SetRow(0, mtx.Row(0).Multiply(1.23)); 

Console.WriteLine(mtx); 

kehrt

DenseMatrix 2x3-Doppel

1,23 1,23 1,23

2

Der Vollständigkeit halber: Math.NET Numerics selbst unterstützt tatsächlich eine Notation, die Ihrem NumPy-Beispiel nahekommt. C# unterstützt es nicht, aber auch andere leistungsfähigere .Net-Sprachen wie F # tun:

let mtx = matrix [[0.;1.;2.];[3.;4.;5.]] 
mtx.[0,*] <- 1.23 * mtx.[0,*] 
+0

Glauben Sie, dass, wenn ich die Bibliothek oft verwenden und intensiver, ich soll die F # Sprache bevorzugen? – ThWin

+1

@ThWin, denke ich, ja. –