ebuko5
ebuko5

Reputation: 1

Problem with finding determinant of matrix 3x3 or larger in C

I have run this with 1x1 and 2x2 matrices which has worked, but for larger ones I have not gotten the correct answer. I have a sample 5x5 matrix in the code which should have a determinant of -30024.

I am running this code but it will not give me the correct answer:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define rMax 5

double a[rMax][rMax] = {{-1,2,3,-6,7}, {-2,0,-1,4,2}, {1,-9,8,2,0}, {9,3,5,7,9}, {-5,3,2,-2,2}};
//determinant should be -30024

double matrix_determinant(size_t rowMax, const double z[][rMax])
{
    double final = 0;
    double w[rowMax][rowMax];

    for(size_t row = 0; row < rowMax; row++) //copies z into a new editable matrix
    {
        for(size_t column = 0; column < rowMax; column++)
            w[row][column] = z[row][column];
    }

    if(rowMax > 2) //checks for larger matrix
    {
        for(size_t mat = 0; mat < rowMax; mat++) //loops equal to the max width of the matrix
        {
            double new[rowMax - 1][rowMax - 1]; //new matrix is 1x1 smaller

            for(size_t cRow = 0; cRow < (rowMax - 1); cRow++) //initializing the new matrix
            {
                for(size_t cCol = 0; cCol < (rowMax - 1); cCol++)
                {
                    new[cRow][cCol] = w[cRow + 1][((cCol + mat) % (rowMax - 1)) + 1];
                }           
            }
            if(0 == (mat % 2)) //alternates adding and subtracting
                final += (w[0][mat] * matrix_determinant((rowMax - 1), new));
            else
                final += ((-1) * w[0][mat] * matrix_determinant((rowMax - 1), new));
        }

        return final;
    }

    if(rowMax == 1) //checks for 1x1 matrix
    {
        return w[0][0];
    }

    else //computes final 2x2 matrix (base case)
    {
        final = (w[0][0] * w[1][1]) - (w[0][1] * w[1][0]);
        return final;
    }
}

int main ( void )
{
    size_t s = rMax;
    double det = matrix_determinant(s, a);
    printf("The determinant of matrix a is: %lf\n", det);
}

Upvotes: 0

Views: 174

Answers (1)

chux
chux

Reputation: 153457

2 changes.

Wrong type

Wrong to pass double new[rowMax - 1][rowMax - 1]; to matrix_determinant(size_t rowMax, const double z[][rMax]). The final [rowMax - 1] does not certainly match [rMax].

For various reasons (which is a side issue), I also took out the const.

// double matrix_determinant(size_t rowMax,   double z[][rMax])
double matrix_determinant(size_t rowMax,    double z[rowMax][rowMax])

Wrong computation

Reformed new[cRow][cCol] calculation. OP's code did not skip the right column in w[][].

            size_t offset = 0;
            for(size_t cCol = 0; cCol < (rowMax - 1); cCol++) {
                //new[cRow][cCol] = w[cRow + 1][((cCol + mat) % (rowMax - 1)) + 1];
                if (cCol == mat) offset = 1;
                new[cRow][cCol] = w[cRow + 1][cCol + offset];
            }

Result

The determinant of matrix a is: -30024.000000

Upvotes: 2

Related Questions