determinant of a matrix 37x37

喜夏-厌秋 提交于 2020-01-05 06:09:07

问题


I have done a program to calculate a determinant of a matrix. My program works, but the problem is that it takes long time to calculate, especially for big matrix. Could you tell me how can a perform my program in order to calculate the determinant in the shortest possible time?

    double Matrice::Determinant(int n)
{
   cout<<"n = "<<n<<endl;
   int i,j,j1,j2;
   double det = 0;
   Matrice tmp(n,n);
   if (n < 1) 
   {
   } 
   else if (n == 1) 
   { 
      det = this->get_el(0,0);
   } else if (n == 2) {
      det = this->get_el(0,0) * this->get_el(1,1) - this->get_el(1,0) * this->get_el(0,1);
   } else {
      det = 0;
      for (j1=0;j1<n;j1++) {
         for (i=1;i<n;i++) {
            j2 = 0;
            for (j=0;j<n;j++) {
               if (j == j1)
                  continue;
               tmp.set_el(i-1,j2,get_el(i,j));
               j2++;
            }
         }
         det += pow(-1.0,1.0+j1+1.0) * get_el(0,j1) * tmp.Determinant(n-1);
      }
   }
   return det;
}

回答1:


Your algorithm, which looks like a straight-forward implementation of the definition formula, is in O(n!).

A standard algorithm in O(n^3) consists in first transforming it into a triangular matrix using Gauss elimination. Once you have done this, the determinant is the product of the diagonal elements.



来源:https://stackoverflow.com/questions/12674580/determinant-of-a-matrix-37x37

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!