找不到 n*n 矩阵的行列式

couldn't find the determinant of n*n matrix

本文关键字:行列式 找不到      更新时间:2023-10-16

我已经编写了以下代码来计算N*N矩阵的行列式。它完美地适用于矩阵4*4和5*5。但找不到40*40矩阵的行列式Z_ Z。本文给出了矩阵Z_ Z的元素。

#include <iostream>
int const N_M=40;
void getCofactor(double A[N_M][N_M], double A_rc[N_M][N_M], int r,int c, int n) 
{ int i = 0, j = 0; 
// Looping for each element of the matrix 
for (int row = 0; row < n; row++) 
{ 
for (int col = 0; col < n; col++) 
{ 
//  Copying into temporary matrix only those element 
//  which are not in given row and column 
if (row != r && col != c) 
{ 
A_rc[i][j] = A[row][col]; 
j=j+1;
// Row is filled, so increase row index and 
// reset col index 
if (j == n - 1) 
{ 
j = 0; 
i=i+1; 
} 
} 
} 
} 
}
double determinant(double A[N_M][N_M], int n) 
{ double D = 0.0; // Initialize result
//  Base case : if matrix contains single element
if (n==1) return A[0][0];
else if (n == 2) return (A[0][0]*A[1][1])-(A[0][1]*A[1][0]); 
else {
double sub_Matrix_A_0c[N_M][N_M]; // To store cofactors 
// Iterate for each element of first row 
for (int c = 0; c < n; c++) 
{ 
// Getting Cofactor of A[0][f] 
getCofactor(A, sub_Matrix_A_0c, 0, c, n); 
D += pow(-1.0,c) * A[0][c] * determinant(sub_Matrix_A_0c, n - 1); 

} 
return D;} 
}
int main () {
double Z_z[N_M][N_M]=

{{-64,16,-4,1,0,0,0,0.0,0,0,00,0,0,0,00,1,0,0,00,0,0,0,00,00,0,0,00,10,0,0,00,,0,0,0:0,0,0,00,0,0,0,7,0,0,0},{-43.7213019529827,12.4106746539480,-3.52287874528034,1,0,0,0,00,0,0,0,0.0,0,0,00,1,0,0,00,,0,0,0:0,0,0,0,4,0,0,00,0,0,0,{0,0,0,0,-43.7213019529827,12.4106746539480,-3.52287874528034,1,0,0,0,00,0,0,0,0.0,0,0,00,1,0,0,00,,0,0,0:0,0,0,0,4,0,0,00,0,0,0},{0,0,0,0,-27,9,-3,1,0,0,0,00,0,0,0,00,1,0,0,00,0,0,0,00,00,0,0,00,,0,0,0.0,0,0,0:0,0,0,00,0,0,0},{0,0,0,0,0,0,-27,9,-3,1,0,0,00,0,0,00,00,0,0,00,,0,0,00,0,0,0,00,1,0,0,0:0,0,0,00,10,0,0,0},{0,0,0,0,0,0,-16.579142269798,6.36491716338729,-2.5227874528034,1,0,0,00,0,0,00,,0,0,00,0,0,0,00,00,0,0,00,1,0,0,0.0,0,0,0:0,0,0,00,0,0,00,10,0,0,0.00,0,0,00},{0,0,0,0,0,0.0,0,0,0,-16.579142269798,6.36491716338729,-2.5227874528034,1,0,0,00,0,0,00,00,0,0,00,,0,0,00,1,0,0,00,0,,0,00,00,,0,00,0,0,0,00},{0,0,0,0,0,0.0,0,0,0,-8,4,-2,1,0,0,00,0,0,00,00,0,0,00,,0,0,00,1,0,0,0:0,0,0,00,0,0,0,00,0,0,00},{0,0,0,0,0,00,0,0,00,00,0,0,00,1,0,0,0,-8,4,2,0,0,00,,0,0,0.0,0,0,0:0,0,0,00,10,0,0,00,0,0,0,00,0,0,00},{0,0,0,0,0,0.0,0,0,00,0,0,00,00,0,0,00,,0,0,0,-3.5317987265895,2.31915967282662,-1.5227874528034,1,0,0,00,1,0,0,0:0,0,0,00,0,0,0,00},{0,0,0,0,0,0.0,0,0,00,0,0,0.00,0,0,00,00,0,0,00,,0,0,0,-3.5317987265895,2.31915967282662,-1.5227874528034,1,0,0,00,0,0,0,0:0,0,0,0},{0,0,0,0,0,00,0,0,00,00,0,0,00,,0,0,00,0,0,0,0,1,0,0,0.0,0,0,0:0,0,0,00,1,0,0,0,{0,0,0,0,0,00,0,0,00,00,0,0,0.0,0,0,0.00,0,0,0,1,0,0,00,,0,0,00,1,0,0,0:0,0,0,00},{0,0,0,0,0,00,0,0,00,00,0,0,0.0,0,0,0.00,0,0,0,-0.14295610020121,073402182265940,-0.522878745280338,1,0,0,0:0,0,0,00,1,0,0,00,0},{0,0,0,0,0,00,0,0,00,00,0,0,0.0,0,0,0.00,0,0,00,1,0,0,00,,0,0,0:0,0,0,00,1.142956190020121,0273402182265940,-0.522878745280338,1,0,0,0,4,0,0,0},{0,0,0,0,0,00,0,0,00,00,0,0,0.0,0,0,0.00,0,0,00,,0,0,00,0,0,0,00,1,0,0,0:0,0,0,0},{0,0,0,0,0,0.0,0,0,00,0,0,0.00,0,0,00,00,0,0,00,,0,0,00,0,0,0,00,1,0,0,00,10,0,0,00},{0,0,0,0,0,00,0,0,00,00,0,0,0.0,0,0,0.00,0,0,00,,0,0,00,1,1,0,0,00,0},{0,0,0,0,0,0.0,0,0,00,0,0,0.00,0,0,00,00,0,0,00,,0,0,00,0,0,0,00,1,0,0,00,1,1,1},{0,0,0,0,0,00,0,0,00,00,0,0,0.0,0,0,0.00,0,0,00,,0,0,00,0,0,0,00,1,0,0,0,2.20222621658426,1.69267904961742,1.3010299566398,1},{37.2320239618439,-7.0457549056068,1,0,-37.230239618439,7.04575749056068,-1,0,0,0,0,0,0.0,0,0,00,0,0,00,00,0,0,00,,0,0,00,1,0,0,00,0,0,0,0:0,0,0,00},{0,0,0,0,27,-6,1,0,-27,6,-1,0,0,0,0,0,00,0,0,00,00,0,0,00,,0,0,00,0,0,0,0.0,0,0,00,1,0,0,0:0,0,0,00},{0,0,0,0,0,0,19.0947514901619,-5.04575749056068,1,0,-19.94754901619,5.0475749056068,-1,0,0,0,00,0,0,0.0,0,0,00,00,0,0,00,,0,0,00,0,0,0,0:0,0,0,00},{0,0,0,0,0,00,0,0,0,12,-4,10,-12,4,-1,0,0,0,00,00,0,0,00,,0,0,00,0,0,0,00,1,0,0,00},{0,0,0,0,0,00,0,0,00,00,0,0,0.0,0,0,0,6.95747901847985,-3.04575749056068,1,0,-6.95747901840,7985,3.045775749056068,-1.0,0,0,0:0,0,0,00,,0,0,0.0},{0,0,0,0,0,0.0,0,0,00,0,0,00,00,0,0,00,,0,0,0,3,-2,0,-3,2,-1,,0,0,00,0,0,0,00,1,0,0,0},{0,0,0,0,0,00,0,0,00,00,0,0,0.0,0,0,0.820206546797821,-1.045775749056068,1,0,-0.8205467978211.04575749056068,-1.0,0,0,0.00,0,0,00,,0,0,0},{0,0,0,0,0,00,0,0,00,00,0,0,00,,0,0,0.0,0,0,00,1,0,0,00,0,0,0,0,1,0,0,0,-1,0,0,0,0},{0,0,0,0,0,00,0,0,00,00,0,0,0.0,0,0,0.00,0,0,00,,0,0,00,1,0,0,0,3,2,1,0,-3,-2,-1,0},{-21.1372724716820,2,0,0,21.1372724716820,-2,0,0,0,00,0,0,0,0.0,0,0,00,,0,0,0:0,0,0,00,1,0,0,0,7,0,0,0,4,0,0,0,{0,0,0,0,-18,2,0,0,18,-2,0,0,0,00,0,0,0,0.0,0,0,00,1,0,0,00,,0,0,0:0,0,0,00,0,0,0,4,0,0,0},{0,0,0,0,0,0,-15.3372724716820,2,0,0,15.1372724716820,-2,0,0,00,0,0,00,00,0,0,00,1,0,0,00,10,0,0,00,,0,0,00,0,0,0,0.0,0,0,0:0,0,0,00},{0,0,0,0,0,00,0,0,00,00,0,0,-12,2,0,0,12,-2,0,0,00,,0,0,00,1,0,0,0.0,0,0,0:0,0,0,00,0,0,0},{0,0,0,0,0,00,0,0,00,00,0,0,00,,0,0,00,0,0,0,0,-9.172727168203,2,0,0,9.13727247168203,-2,0,0,0.0,0,0,00,1,0,0,0:0,0,0,0,4,0,0,0},{0,0,0,0,0,00,0,0,00,00,0,0,00,,0,0,00,0,6,2,0,0,6,2,0,0,0.0,0,0,00,1,0,0,0:0,0,0,00,0,0,0},{0,0,0,0,0,00,0,0,00,00,0,0,0.0,0,0,00,,0,0,0,-31.3727247168203,2,0,0,3.13727247168203,-2,0,0,00,1,0,0,0:0,0,0,0},{0,0,0,0,0,00,0,0,00,00,0,0,00,,0,0,0.0,0,0,00,0,0,0,00,1,0,0,0,2,0,0,0,-2,0,0,0:0,0,0,0},{0,0,0,0,0,00,0,0,00,00,0,0,0.0,0,0,0.00,0,0,00,,0,0,00,0,0,0,00,1,0,0,0,6,2,0,0,-6,-2,0,0},{24,-2,0,0,0,00,0,0,0,0.0,0,0,00,1,0,0,00,0,0,0,00,00,0,0,00,,0,0,0,4,0,0,0:0,0,0,0},{0,0,0,0,0,00,0,0,00,00,0,0,0.0,0,0,0.00,0,0,00,,0,0,00,1,0,0,00,0,0,0,0,-7.80617997398389,-2,0,0}};

double det=determinant(Z_z, 40); 
cout<<det;
system ("pause");
return 0;}

在第一阶段递归调用determinant()函数n次,然后n次调用中的每一次调用n-1次,等等。因此,调用总数将接近n!(阶乘)。

当n=4或n=5时,呼叫次数仍然可以接受,但当n=40时,您尝试拨打40!=8159152832478977334345611269596115894272000000000个虚拟调用,更不用说这么多任何类型的操作了。我认为你找不到一台机器来处理这件事。