5

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

TylerH
  • 20,799
  • 66
  • 75
  • 101
J. McCabe
  • 115
  • 2
  • 9
  • It seems to work fine for me. My guess is that your `Ybus` is not what you think it is. Since you do not show how you are populating `Ybus` or how you determined that it is what you claim it is it is hard to say what the problem is exactly. – Jason Boyd Nov 15 '16 at 19:37
  • Thanks. I guess I got enough reputation to post an image, so I just posted a screenshot of the console print of the elements of the array. Strange...the only difference between the two arrays is the Inverse operation. Unless there's something strange under the hood of my input array.. – J. McCabe Nov 15 '16 at 19:48
  • And btw, the same method/function is used in both cases to print the array elements, so it's not like there's an issue with the printing format or something... I think – J. McCabe Nov 15 '16 at 19:51
  • Oooppppss...sorry everyone. I did a stupid. My array sizes were 1 row/column too big, and due to the row of zeros the inverse blew up. My apologies. All is well :) – J. McCabe Nov 15 '16 at 20:07

1 Answers1

3

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)
Christoph Rüegg
  • 4,626
  • 1
  • 20
  • 34
  • 1
    Thanks Christoph. Sorry for the false alarm. My array size was too big and the row of zeros caused the inverse to fail. Oh, and the bad rounding was me being too lazy to type 8 digits for each number :) By the way, thanks so much for this wonderful MathNet library. It's such a huge help for us engineers who don't want to write the detailed code for stuff like matrix inversion. – J. McCabe Nov 15 '16 at 20:22