tags:

views:

103

answers:

4

suppose there is given two dimensional array

int a[][]=new int[4][4];

i am trying to find determinant of matrices please help i know how find it mathematical but i am trying to find it in programaticaly i am using language java and c# but in this case i think c++ will be also helpfull

+2  A: 

If you know how to do it mathematically, then apply this knowledge and write code that does exactly the same as you would do if you had to calculate the determinant by hand (on a paper). As Ignacio told you in his comment, please tell us what have you tried and maybe then you will get better answers. I will gladly edit my answer and help you out.

EDIT:

As it seems the problem here is not the formula itself, but understanding how to work with arrays, i would suggest something like this tutorial (i assume you use C#): how to: arrays in C#

PeterK
i have not done yet anything beacause i need sure how walk in array and touch elements which are located left to right bottom to up and so on
+5  A: 

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

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];
}

For a general NxN, the problem is considerably harder, with various algorithms in the order of O(N!), O(N^3), etc.

References

Related questions

polygenelubricants
that's horrible
isola009
@isola: Yes. I +1 love it. Can we do a 5x5 matrix now?
Charles Stewart
You are very crazy
isola009
at least for 'small' matrices, that's not as cazy as one might think, especially if you consider code generators; for 'large' matrices, one of the more sophisticated algorithms should be used as the asymptotic time complexity becomes more important
Christoph
It's worth mentioning that the `O(n^3)` algorithm is called [Gaussian elimination](http://en.wikipedia.org/wiki/Gaussian_elimination)
BlueRaja - Danny Pflughoeft
+1  A: 

Generate all the permuatations of integers 1..N, and for each such sequence s_1..s_N, calculate the product of the values of the cells M(i,s_i) multiplied by a sign value p(s_1..s_i), which is 1 if i-s_1 is even, and -1 otherwise. Sum all these products.

Postscript

As polygene says, there are inefficient algorithms, and this one is O(N!), since it keeps recalculating shared subproducts. But it's intuitive and space efficient, if done lazily.

Oh, and the sign function above is wrong: P(s_1..s_i) is +1, if s_i has odd index in the sequence 1..N with s_1..s_{i-1} removed, and -1 for even index.

Charles Stewart
+2  A: 

You can check the following link: Determinant of matrix in Java

isola009