Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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/>");
    }
like image 962
user347640 Avatar asked Dec 31 '25 07:12

user347640


2 Answers

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

  • Wikipedia/Determinant
  • EuclidianSpace.com/Determinant 4x4 formula
like image 57
polygenelubricants Avatar answered Jan 01 '26 23:01

polygenelubricants


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

like image 45
Rusty Avatar answered Jan 01 '26 22:01

Rusty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!