Noob Question: Solving a Matrix

Apr 9, 2013 at 3:13 PM
Edited Apr 9, 2013 at 3:39 PM
So, I'm trying to "solve" a matrix. If I were doing this with a pencil and paper, it'd be Gauss Jordan. Basically, what I want is to take the matrix for variables w, x, y, and z as follows:

1 0 -2 0
2 0 0 -1
0 2 -3 -2

and solve for w, x, y, and z. Again, with Gauss Jordan, it'd be:

1 0 0 -1/2
0 1 0 -11/8
0 0 1 -1/4

So w = 1/2, x = 11/8 and y = 1/4

I've made a DenseMatrix for this, but I can't for the life of me figure out how to solve it. I feel like an idiot. :\ Linear Algebra is right on the edge of my mathematical ability, and I'm thinking Math.NET uses a more efficient means than Gauss-Jordan I'm not familiar with. Can anyone point me in the right direction?

EDIT: Am I asking Math.NET to do too much? I thought I'd just be able to plug in a matrix and then call a method to tell it to put it into identity form for me. Am I wrong?
Apr 9, 2013 at 4:19 PM

Not entirely sure where your 4th variable comes in, but assuming you really want to solve the linear system A*x=y to x, or with actual numbers:
 1*u + 0*v - 2*w = 0
 2*u + 0*v + 0*w = 1
 0*u + 2*v -3*w = 2
You can solve it using the LU decomposition (since the matrix is square, else you could use QR) with Math.NET as follows:
 var A = new DenseMatrix(3,3,new double[] { 1,2,0,0,0,2,-2,0,-3 });
 var y = new DenseVector(new double[] {0,1,2});
 var x = A.LU().Solve(y);
Which will return vector x as (u,v,w) = (0.5; 1.375; 0.25)

Does that help?

Apr 9, 2013 at 10:48 PM
Edited Apr 10, 2013 at 2:52 PM
Thanks, this is definitely helpful, but I'm still finding myself a bit stuck. Here's what I'm trying to do. I have an arbitrary number of variables in equations, such as:

w = 2y
2w = z
2x = 3y + 2z

So in this example, I get the following matrix:

1 0 -2 0 = 0
2 0 0 -1 = 0
0 2 -3 -2 = 0


1 0 -2 = 0
2 0 0 = 1
0 2 -3 = 2

I can use Gauss-Jordan by hand (the only way I know how) to convert to solve for w, x, and y in a square matrix, giving me:

w = 1/2
x = 11/8
y = 1/4

I then find the Least Common Denominator to get my answer for z (which in this case is 8) and then convert my fractions into integers. In the end, my answer is:

w = 4
x = 11
y = 2
z = 8

Having them as integers is very important for my purposes. My method will only be fed integers and should only return them.

The problem I'm having now is, I don't know how I'd find the Least Common Denominator of a double (I should have thought of this before...). And I'm not sure the best way to convert my solution set (.5, 1.37, .25) into integers (2, 11, 4). I didn't see any overloaded versions of LU() or Solve() that would allow me to specify I want integers back, and while I'm sure I can brute force it, I thought you might know an easier way already baked in. :)
Apr 10, 2013 at 1:39 AM
I figured it out. I just need to do something like:

var z = x.QR().Determinent;
x *= z;

Thanks again for the help!
Apr 10, 2013 at 1:58 AM
Edited Apr 10, 2013 at 3:41 PM
One more question! So, Determinants are only valid for square matrices, so what I did earlier doesn't really work for some of the other matrices I'm working with. Can anyone give me a push in the right direction for determining the "z" answer in my above example for a non-square matrix?

EDIT: So, I don't know if Math.NET has a "better" way of doing this, but I'm just finding my z value by plugging it into my earlier equations. No problem. I'm still ending up with a bunch of decimals, though, when I have to have them in integer form. I'm really stuck on this part, though.