Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matrix Inverse using MathNet.Numerics

I'm writing some C# code that uses MathNet.Numerics.LinearAlgebra, and trying to match results with a textbook example. One part of the code does an inversion of a complex32 array "Ybus", and stores it in another array "Zbus":

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 an inversion

Zbus = Ybus.Inverse();

the results of Zbus are all NaN

while the correct result from the book looks like this:

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

Anyone have any ideas what the issue might be? Maybe inversion of a complex matrix has some issues?

enter image description here

Lesson learned: don't make your arrays too big so that they have rows of 0's or the inverse will blow up :) ... Here's the right answer:

enter image description here

like image 814
J. McCabe Avatar asked Oct 30 '22 16:10

J. McCabe


1 Answers

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

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 output, matching your book result (except for the bad 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)
like image 166
Christoph Rüegg Avatar answered Nov 15 '22 06:11

Christoph Rüegg