Inverse Matrix Using MathNet.Numerics

I am writing C # code that uses MathNet.Numerics.LinearAlgebra and trying to match the results with a sample tutorial. One part of the code inverts the complex32 "Ybus" array and stores it in another "Zbus" array:

Matrix<Complex32> Ybus = Matrix<Complex32>.Build.Dense(numBuses, numBuses); Matrix<Complex32> Zbus = Matrix<Complex32>.Build.Dense(numBuses, numBuses); 

My Ybus matches exactly the example in the book.

 Ybus = j[ -13 5 4 0 5 -13.5 2.5 2 4 2.5 -9 2.5 0 2 2.5 -4.5] 

But when I do the inverse

 Zbus = Ybus.Inverse(); 

Zbus results - all NaN

while the correct result from the book is as follows:

 Zbus = j[ .15 .09 .12 .11 .09 .15 .12 .13 .12 .12 .25 .19 .11 .13 .19 .39] 

Anyone have any ideas what could be the problem? Maybe inverting a complex matrix has some problems?

enter image description here

Lesson learned: do not make your arrays too large so that they have lines 0 or vice versa explode :)) Here is the correct answer:

enter image description here

+5
source share
1 answer

As Jason mentioned, this seems to work fine. For instance:

 var y = Complex32.ImaginaryOne * CreateMatrix.Dense(4, 4, new Complex32[] {-13f,5f,4f,0f,5f,-13.5f,2.5f,2f,4f,2.5f,-9f,2.5f,0f,2f,2.5f,-4.5f}); y.ToString("F3"); y.Inverse().ToString("F3"); 

Provides the following result that matches your book result (except for poor rounding in the book):

 DenseMatrix 4x4-Complex32 (0.000, -13.000) (0.000, 5.000) (0.000, 4.000) (0.000, 0.000) (0.000, 5.000) (0.000, -13.500) (0.000, 2.500) (0.000, 2.000) (0.000, 4.000) (0.000, 2.500) (0.000, -9.000) (0.000, 2.500) (0.000, 0.000) (0.000, 2.000) (0.000, 2.500) (0.000, -4.500) DenseMatrix 4x4-Complex32 (0.000, 0.153) (0.000, 0.097) (0.000, 0.126) (0.000, 0.113) (0.000, 0.097) (0.000, 0.153) (0.000, 0.124) (0.000, 0.137) (0.000, 0.126) (0.000, 0.124) (0.000, 0.256) (0.000, 0.197) (0.000, 0.113) (0.000, 0.137) (0.000, 0.197) (0.000, 0.393) 
+2
source

Source: https://habr.com/ru/post/1259782/


All Articles