1

Possible Duplicate:
Calculating an NxN matrix determinant in C#

i want to find determinant of 4x4 matrix in c#

    int ss = 4; int count = 0;
    int[,] matrix=new int[ss,ss];
    ArrayList al = new ArrayList() {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
    for (int i = 0; i < ss; i++)
    {
        for (int j = 0; j < ss; j++)
        {
            matrix[i, j] =Convert.ToInt32( al[count]);
            ++count;
            Response.Write(matrix[i, j] + " ");
        }
        Response.Write("<br/>");
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
user347640
  • 213
  • 1
  • 4
  • 11
  • Whilst this question does appear to be identical to that @vj4u posted elsewhere, in this instance it's specific to 4x4 matrices, something common in graphics calculations. I hit this question via Google and, even though it's a poorly phrased and somewhat desperate question, I would have liked to have seen a few answers here, and ultimately to have added my own. I added it here: http://stackoverflow.com/questions/2922690/calculating-an-nxn-matrix-determinant-in-c/2980966#2980966 – Drew Noakes Jun 05 '10 at 15:47

4 Answers4

14

If you're fixed to 4x4, the simplest solution would be to just hardcode the formula.

   // assumes matrix indices start from 0 (0,1,2 and 3)
   public double determinant(int[,] m) {
      return
         m[0,3] * m[1,2] * m[2,1] * m[3,0] - m[0,2] * m[1,3] * m[2,1] * m[3,0] -
         m[0,3] * m[1,1] * m[2,2] * m[3,0] + m[0,1] * m[1,3] * m[2,2] * m[3,0] +
         m[0,2] * m[1,1] * m[2,3] * m[3,0] - m[0,1] * m[1,2] * m[2,3] * m[3,0] -
         m[0,3] * m[1,2] * m[2,0] * m[3,1] + m[0,2] * m[1,3] * m[2,0] * m[3,1] +
         m[0,3] * m[1,0] * m[2,2] * m[3,1] - m[0,0] * m[1,3] * m[2,2] * m[3,1] -
         m[0,2] * m[1,0] * m[2,3] * m[3,1] + m[0,0] * m[1,2] * m[2,3] * m[3,1] +
         m[0,3] * m[1,1] * m[2,0] * m[3,2] - m[0,1] * m[1,3] * m[2,0] * m[3,2] -
         m[0,3] * m[1,0] * m[2,1] * m[3,2] + m[0,0] * m[1,3] * m[2,1] * m[3,2] +
         m[0,1] * m[1,0] * m[2,3] * m[3,2] - m[0,0] * m[1,1] * m[2,3] * m[3,2] -
         m[0,2] * m[1,1] * m[2,0] * m[3,3] + m[0,1] * m[1,2] * m[2,0] * m[3,3] +
         m[0,2] * m[1,0] * m[2,1] * m[3,3] - m[0,0] * m[1,2] * m[2,1] * m[3,3] -
         m[0,1] * m[1,0] * m[2,2] * m[3,3] + m[0,0] * m[1,1] * m[2,2] * m[3,3];
   }

References

polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • im sorry im not fixed i want to calculate for nxn – user347640 May 30 '10 at 13:39
  • If you are using a flat array, rather than a rectangular one, then check my adaptation of this answer here: http://stackoverflow.com/questions/2922690/calculating-an-nxn-matrix-determinant-in-c/2980966#2980966 – Drew Noakes Jun 05 '10 at 15:50
3

You might look at The Answer given the last time you posted this exact same question.

Community
  • 1
  • 1
Rusty
  • 3,228
  • 19
  • 23
  • hehe all i could thought when i went and read the other question was "buuuuuurnnneeed" xD – Francisco Noriega May 31 '10 at 20:33
  • 1
    @bangoker I usually avoid the use of violent sarcasm as not being constructive. However, my self control breaks down when the poster has 1 rep and the question takes the form of "How do I ... ?" and includes the likes of urgent, important, post code answer, etc. I just can't help myself. – Rusty Jun 01 '10 at 18:37
  • and I support you for breaking down :D I mean, people should at least TRY to do something about it, whether its a google/SO search or some code.. or at least a nice text explaining the doubts.. – Francisco Noriega Jun 01 '10 at 21:27
  • @bangoker Agreed. There is always help for the ignorant. We can only hope that Darwin finds the stupid. – Rusty Jun 01 '10 at 23:16
2

I'm not a big fan of doing what appears to be someone else's homework, so instead I'll summarise some thoughts on your problem, which will hopefully be more enlightening than simply posting the solution.

You've probably done 3x3 determinants before, and noticed that the method relies on using the individual 2x2 determinants left over from crossing out a row and a column. You then multiply by the doubly crossed number, and +/- alternately.

So, for a 4x4 matrix, you would simply extend this algorithm. This would then require you to find the determinants of the remaining 3x3 matrices after crossing out a col+row.

Basically, you need a recursive program that does this.

Also, it sounds this question is part of a larger algorithm design course, where you're meant to appreciate that this algorithm isn't very scalable, ie for larger matrices it takes a huge number of calculations.

From what I remember, LU-decomposition is a good alternative that allows matrix inversion with good scaling properties.

Carlos
  • 5,991
  • 6
  • 43
  • 82
2

What you need is implementation of LU-decomposition.

It decomposes matrix into two triangular matrices L and U such that A = L*U. L is lower triangular matrix and U is upper triangular matrix.

Since A = L*U, then det(A) = det(L)*det(U). Now the fact that determinant of a triangular matrix is equal to product od elements on the diagonal allows to compute det(L) and det(U) easy.

det(L) = diag_prod(L) same for U

so

det(A) = diag_prod(L) * diag_prod(U)

as for actual algorithm for LU-Decomposition I recommend Doolittle algorithm. It is easy to understand and wikipedia has a description.

http://en.wikipedia.org/wiki/LU_decomposition

stmi
  • 733
  • 4
  • 13