Matrice Recursive Determinant Calculation

This is a discussion on Matrice Recursive Determinant Calculation within the C Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    72

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

  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    It's not a maths forum.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    72
    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

  5. #5
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Well good luck fixing it.

  6. #6
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    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);
    }
    Last edited by cph; 11-01-2008 at 12:33 AM.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,146
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrice Recursive Determinant Calculation
    By overlord21 in forum C Programming
    Replies: 1
    Last Post: 10-31-2008, 10:37 AM
  2. Recursive algorithm to find the determinant of a matrix
    By mahesh.mach in forum C Programming
    Replies: 3
    Last Post: 06-07-2007, 09:13 AM
  3. difference between recursive and iterative
    By Micko in forum C Programming
    Replies: 33
    Last Post: 07-06-2004, 09:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Determinant calculation
    By Sang-drax in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2002, 01:32 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21