2

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.

I NN_
  • 139
  • 6
  • 1
    I'd think you would want to simplify matrixes using an elimination algorithm, We then know that the determinate of a triangular matrix is the product of the diagonal elements. See https://math.stackexchange.com/questions/1298536/what-operations-can-i-do-to-simplify-calculations-of-determinant – Michael Dorgan May 06 '20 at 21:42
  • 1
    And other code: https://www.geeksforgeeks.org/program-for-gauss-jordan-elimination-method/ – Michael Dorgan May 06 '20 at 21:59

0 Answers0