矩阵行列式 [英] Matrix Determinant

查看:119
本文介绍了矩阵行列式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在尝试对从文件中读取的一些矩阵执行矩阵运算.为了存储每个矩阵的尺寸,我使用了2d数组,并且还使用了multiD数组来指定特定矩阵的每个元素.我遇到问题的部分正在尝试计算矩阵之一的行列式.该文件将告诉我必须针对哪个矩阵执行此操作:类似于8D,其中8表示特定矩阵.一旦我的程序读取了D,就会调用我的行列式函数.这是此功能的代码:

  void 行列式( float  myArray [ 25 ] [ float  otherArray [ 25 ], int 矩阵;
     矩阵= c-1;
      float  sum =  0 ;
     浮动行列式[ 25 ] [ 25 ] ;
     如果(otherArray [matrix] [ 0 ]!= otherArray [matrix] [" < ;< " << endl;
         返回;
     }

      如果(otherArray [矩阵] [ 0 ] ==  2 )
      {
                    sum =(myArray [matrix] [ 0 ] [ 0 ] * myArray [matrix] [ 1 ] [ 1 ])-(myArray [matrix] [ 0 ] [ 1 ] * myArray [matrix] [ 1 ] [ for ( int  p =  0 ; p< ; otherArray [matrix] [ 0 ]; p ++)
      {
           int  h =  0 ;
           int  k =  0 ;
           for ( int  i =  1 ; i< ; otherArray [matrix] [ 0 ]; i ++)
          {
               for ( int  j =  0 ; j< ; otherArray [matrix] [ 0 ]; j ++)
              {
                  如果(j == p)
                    继续;
                    行列式[h] [k] = myArray [matrix] [i] [j];
                    k ++;
                  如果(k ==(otherArray [matrix] [ 0 ]-1))
                  {
                                     h ++;
                                     k =  0 ;
                  }
              }
          }
          sum + = myArray [matrix] [ 0 ] [p] * pow(-1. 0 ,p) *行列式(determinant,otherArray-1,matrix);
      }
      cout<< " << c<< " << sum<< endl;
      cout<< " << endl;
      返回;
} 




sum + =部分是我认为造成麻烦的部分.我起初声明了行列式[25] [25],所以不是行列式[h] [k] =
myArray [matrix] [i] [j]我的myArray等于行列式[matrix] [h] [k].这样,我的程序就可以编译,但是有时我的程序会崩溃,当我尝试对其进行调试时,它告诉我行列式数组存在问题.这就是为什么我尝试更改它的原因.即使第一种方法有时使我的程序无法工作,但它仍然可以正常工作,除了计算的数字应该是220.00,而应该是022000.由于明显的原因,当前编写代码的方式不起作用-行列式只是2D数组,但是在执行求和计算时,它在对行列式函数的调用中占据3D数组的位置.我有点困惑,也不知道该去哪里.这可能是一个快速修复,或者我有一些大的逻辑错误,不确定.

解决方案

您不应使用递归来计算行列式...无论如何,我建议使用LAPACK或ATLAS.

Okay I''m trying to perform matrix operations on a few matrices that I read in from a file. To store the dimensions of each matrix I used a 2d array, and I also used a multiD array to specify each element of a particular matrix. The part I''m having a problem with is trying to calculate the determinant of one of the matrices. The file will tell which matrix I must do this for: so something like 8D, where the 8 represents the specific matrix. Once my program reads the D is calls my determinant function. Here is the code I have for this function:

void Determinant(float myArray[25][25][25], float otherArray[25][25],int c) 
{
     int matrix;
     matrix=c-1;
     float sum=0;
     float determinant[25][25];
     if (otherArray[matrix][0]!=otherArray[matrix][1])
     {
         cout<<"The determinant could not be found because the matrix was not a square matrix."<<"\n"<<endl;
         return;
     }

      if(otherArray[matrix][0]==2)
      {
                    sum=(myArray[matrix][0][0]*myArray[matrix][1][1])-(myArray[matrix][0][1]*myArray[matrix][1][0]);
                    cout<<sum;
      }
      for (int p=0; p<otherArray[matrix][0]; p++)
      {
          int h=0;
          int k=0;
          for (int i=1; i<otherArray[matrix][0]; i++)
          {
              for (int j=0; j<otherArray[matrix][0]; j++)
              {
                  if (j==p)
                    continue;
                    determinant[h][k]=myArray[matrix][i][j];
                    k++;
                  if (k==(otherArray[matrix][0]-1))
                  {
                                     h++;
                                     k=0;
                  }
              }
          }
          sum+=myArray[matrix][0][p]*pow(-1.0,p)*Determinant(determinant,otherArray-1, matrix);
      }
      cout<<"The determinant of Matrix "<<c<<" is : "<<sum<<endl;
      cout<<"\n\n"<<endl;
      return;
}




the sum+= part is the part that I think is causes my trouble. I at first had declared determinant[25][25] so instead of determinant[h][k]=
myArray[matrix][i][j] I had the myArray equal to determinant[matrix][h][k]. With this, my program was able to compile, however sometimes my program would crash and when i tried to debug it it told me that there was problems with my determinant array. Which is why I tried to change it. Even though the first way made my program not work sometimes, it still worked other times except the number calculated should have been 220.00 and instead it was 022000. The way the code currently is written doesn''t work, for obvious reasons though -- determinant is only a 2D array and yet it takes the spot of a 3D array in the call to the determinant function while performing the sum calculation. I'' kinda confused, and don''t really know where to go. It might be a quick fix, or I have some big logical mistake, not really sure. ANY help would be greatly appreciated!

解决方案

You shouldn''t use recursion to compute a determinant... Anyway, I would recommend LAPACK or ATLAS.


这篇关于矩阵行列式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆