NxN矩阵行列式递归问题

时间:2012-11-17 10:51:18

标签: c math recursion matrix

我目前正在尝试编写一个程序来查找NxN矩阵的行列式但是我有一个N大于2的递归问题。基本上我可以告诉它,它没有这样做,它只是运行函数一旦使用我的调试选项显示该函数贯穿列,但顺序永远不会下降,然后它给我的决定因素为零,无论如何。香港专业教育学院曾试图寻找任何想法,因为我做错了但我似乎无法找到任何答案,我甚至找到了与我基本相同的例子和使用它们给我零,无论如何,所以我非常困惑:(。如果有人可以快速查看我的代码,并告诉我哪里是一个白痴,我将非常感激!(抱歉格式化,它在我的编辑器中看起来不错但我似乎无法得到它在这里)

代码:

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


double det(double **mat, int order);


int main (int argc, char* argv[])
{  


   FILE* input; 
   int row,column,N;
   double **matrix;
   N=3;
   matrix=(double**)malloc(N*sizeof(double));

   input=fopen("matrix.dat", "r");
   if(input !=(FILE*) NULL)
   {

      for(row=0; row<N; row++) 
      { 
     matrix[row]=(double*)malloc(N*sizeof(double));
  }
  for(row=0; row<N; row++) 
  {
        printf("| ");
        for(column=0; column<N; column++)   
        {  
          fscanf(input,"%lf ", &matrix[row][column]); 
      printf("%g ", matrix[row][column]);
    }
     if(row != (N/2))
         { 
       printf("|\n");
         }
         else
         {

           printf("|= %lf \n", det(matrix, N) );
     }
 }
 return(EXIT_SUCCESS);
 }

 else
 { 
  printf("*********************ERROR*********************\n");
  printf("** Cannot open input file 'matrix.dat' make  **\n");
  printf("** sure file is present in working directory **\n");
  printf("***********************************************\n");

  return(EXIT_FAILURE);
 }

}

double det(double **mat, int order)
{
 int debug;
 double cofact[order], determinant, **temp;
 determinant = 0;
 debug=0;

 if(order==1)
 {
  determinant=mat[0][0];

  if(debug==1)
  {
    printf("order 1 if\n");
  }
 }
  else if(order==2)
 {
 determinant= ((mat[0][0]*mat[1][1])-(mat[0][1]*mat[1][0]));

 if(debug==1)
 {
   printf("order 2 if\n");
     }
  }
 else
 {

  int column, rowtemp, coltemp, colread;
  for (column=0; column<order; column++) 
  {
  /* Now create an array of size N-1 to store temporary data used for calculating minors */
     temp= malloc((order-1)*sizeof(*temp)); 
    for(rowtemp=0; rowtemp<(order-1); rowtemp++)
  {
    /* Now asign each element in the array temp as an array of size N-1 itself */
    temp[rowtemp]=malloc((order-1)*sizeof(double));
  }
  for(rowtemp=1; rowtemp<order; rowtemp++)
  {
    /* We now have our empty array, and will now fill it by assinging row and collumn values    from the original mat with the aprroriate elements excluded */
       coltemp=0;
      for(colread=0; colread<order; colread++)
      {
        /* When the collumn of temp is equal to the collumn of the matrix, this indicates this row should be exlcuded and is skiped over */
        if(colread==column)
        {
          continue;
        }
        temp[rowtemp-1][coltemp] = mat[rowtemp][colread];
        coltemp++;
      }

   }
  if(debug==1)
  {
    printf("column =%d, order=%d\n", column, order);
  }
  determinant+=(mat[0][column]*(1 - 2*(column & 1))*det(temp, order-1));
}   

 }  
 return(determinant);   

 }

1 个答案:

答案 0 :(得分:2)

temp= (double **)malloc((order-1)*sizeof(double));

这不会导致崩溃只要sizeof(double*) <= sizeof(double),这在通常的32位或64位系统上就是这种情况,但它在概念上是错误的。您正在为double*数组分配空间,因此该因子应为sizeof(double*),或者更好,因为它在类型更改时不变,sizeof *temp

temp = malloc((order-1) * sizeof *temp);

(而且你不需要在C中投射malloc的结果,最好不要这样做,因为演员可能会隐藏错误,比如忘记#include <stdlib.h>。)

同样适用于分配

matrix=(double**)malloc(N*sizeof(double));
main中的

在行列式的计算中,

         for(coltemp=0; coltemp<order; coltemp++)
         {
           for(colread=0; colread<order; colread++)
               {
               /* When the collumn of temp is equal to the collumn of   the matrix, this indicates this row should be exlcuded and is skiped over */
               if(colread==column)
               {
                continue;
               }
          temp[rowtemp-1][coltemp] = mat[rowtemp][colread];
          coltemp++;
            }
        }

您在列中循环两次,一次用于coltemp == 0,然后在内循环中,coltemp递增order-1次,因此内循环第二次运行{ {1}}一开始。然后coltemp == order-1在循环中再次递增多次,并且您正在写出已分配内存的界限。

应删除外部循环,coltemp并且内部循环是您需要的。

coltemp = 0;

不是确定标志的好方法。

pow(-1,column))

比调用(1 - 2*(column & 1)) 要快。

最后,在pow

main

您正在第for(row=0; row<N; row++) { printf("| "); for(column=0; column<N; column++) { fscanf(input,"%lf ", &matrix[row][column]); printf("%g ", matrix[row][column]); } if(row != (N/2)) { printf("|\n"); } else { printf("|= %lf \n", det(matrix, N) ); } } 行打印行列式,这看起来很不错,但此时您还没有在整个矩阵中进行扫描,因此行N/2N/2 + 1包含未初始化的行数据,不太可能全为零。如果将N-1更改为if (row != N/2),它将起作用,但是,处理它的正确方法是将矩阵的扫描与计算和打印分开。这些都是独立的操作,应该在各自独立的功能中处理。