# Matrice Recursive Determinant Calculation

• 10-31-2008
overlord21
Matrice Recursive Determinant Calculation
Hi i have problems calculating the determinant of more than a 2 x2 can anyone help

Code:

``` nt determinant(int matrix_temp[10][10],int size_matrice)  //Get the minor of the matrice {  int i,j,determinant_sum = 0;  int matrice[10][10];//matrice[maximum][maximum]  if(size_matrice==2)   {                                        //basic step         determinant_sum = matrix_temp[0][0]*matrix_temp[1][1] - matrix_temp[0][1]*matrix_temp[1][0];         return determinant_sum;   }  for(int count=0;count<size_matrice;count++)  {   int h = 0,k = 0;   for(i=1;i<size_matrice;i++)   {         for( j=0;j<size_matrice;j++)         {         if(j==count)                   matrice[h][k] = matrix_temp[i][j];         k++;         if(k == size_matrice-1)           {                 h++;                 k = 0;           }         }   } //recursive formula   determinant_sum = determinant_sum + matrix_temp[0][count]*pow(-1,count)*determinant(matrice,size_matrice-1);   //Force the sum to return an "int" result    }  return determinant_sum; }```
• 10-31-2008
esbo
It's not a maths forum.
• 10-31-2008
Thantos
• 10-31-2008
overlord21
This a math problem? but what the hell yu say i told that i have problems with the recursive formula as it gives strange results for 3 x 3 matrice and more
• 10-31-2008
esbo
Well good luck fixing it.
• 10-31-2008
cph
maybe like this (not tested for bigger matrix yet, sorry)
Code:

```#include <stdio.h> #define MY_FREE(x)  { free(x); x = NULL; } typedef struct {     unsigned int width;     unsigned int height;     float        *data; } matrix; /*  * new_matrix : creates new matrix  * w          : width (column)  * h          : height (row)  * returns    : newly allocated matrix (on success), NULL (otherwise)  */ matrix *new_matrix (unsigned int w, unsigned int h) {     matrix *result = (matrix *)malloc(sizeof(matrix));     if (result != NULL) {         result->width  = w;         result->height = h;         result->data  = (float *)calloc(w * h, sizeof(float));         if (result->data == NULL) {             fprintf(stderr, "new_matrix() : out of memory.\n");             MY_FREE(result);         }     }     return(result); } /*  * destroy_matrix : it's obvious  */ void destroy_matrix (matrix *mat) {     if (mat != NULL) {         MY_FREE(mat->data);         MY_FREE(mat);     } } /*  * determinant : get the determinant of a matrix  * mat    : matrix input  * retval  : 1 (success), 0 (failed)  * returns : determinant of matrix mat  */ float determinant (const matrix *mat, int *retval) {     float result;     if (mat->width != mat->height) {         fprintf(stderr, "not a square matrix!\n");         *retval = 0;        /* return false */     } else {         if (mat->width == 2) {             /* det = ad - bc */             result  = (mat->data[0] * mat->data[3]) - (mat->data[1] * mat->data[2]);             *retval = 1;         } else {             matrix *tmp_mat = new_matrix(mat->width - 1, mat->height - 1);             if (tmp_mat == NULL) {                 *retval = 0;             } else {                 const float sign[2] = { 1.0, -1.0 };                 int        col, rc, x, y, xx, yy;                 float      tmp_det;                 result = 0.0;                 for (col = 0; col < mat->width; ++col) {                     for (y = 1, yy = 0; y < mat->height; ++y, ++yy) {                         for (x = 0, xx = 0; x < mat->width; ++x) {                             if (x != col) {                                 tmp_mat->data[yy * tmp_mat->width + xx] = mat->data[y * mat->width + x];                                 ++xx;                             }                         }                     }                     tmp_det = determinant(tmp_mat, &rc);                     if (rc) {        /* success */                         result = result + (mat->data[col] * sign[col &#37; 2] * tmp_det);                     } else {        /* failed */                         *retval = 0;                         destroy_matrix(tmp_mat);                         break;                     }                 }                 *retval = 1;             }         }     }     return result; } /* test program */ int main (void) {     matrix *mat1 = new_matrix(3, 3);     int    x, y, rc;     float  det;     for (x = 0; x < mat1->width * mat1->height; ++x) {         mat1->data[x] = (float)1;     }     mat1->data[4] = 5.0;     mat1->data[6] = 13.0;     det = determinant(mat1, &rc);     if (rc) {         printf("determinant : %f\n", det);     }     for (y = 0; y < mat1->width; ++y) {         for (x = 0; x < mat1->height; ++x) {             printf("%5.2f", mat1->data[y * mat1->width + x]);         }         printf("\n");     }     destroy_matrix(mat1);     return(0); }```
• 11-01-2008
cyberfish
Don't write complete code for people even if you have the time to kill. They won't learn this way. Worse still if it's homework question.