In my Matrix class I was able to write my own code for matrices up to 3X3
class Matrix
{
public float Determinant()
{
if (!isSquare())
return 0;
else if (_rows == 1)
return this[0, 0];
else if (_rows == 2)
{
/* |a b|
|c d|*/
float a = this[0, 0];
float b = this[0, 1];
float c = this[1, 0];
float d = this[1, 1];
return (a * d) - (b * c);
}
else
{
float sum = 0;
int i = 0;
for (int j = 0; j < _cols; j++)
{
//finding cofactor
float a = (float)Math.Pow(-1, i + j);
float b = (j % 2 == 0) ? - a * this[i,j] : a * this[i,j];
Matrix m = subMatrix(i, j);
//getting determinant by recursion
float d = m.Determinant();
sum += b * d;
}
return sum;
}
}
}
This code stops working for matrices bigger than 3X3. I've read some similar posted by other people but those don't really help me. I don't need spoon-fed code, just some explanation or maybe an article which describes what I need to do.