找不到 n*n 矩阵的行列式
couldn't find the determinant of n*n matrix
我已经编写了以下代码来计算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个虚拟调用,更不用说这么多任何类型的操作了。我认为你找不到一台机器来处理这件事。
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 找不到QtResource文件中的文件
- VC++本机单元测试,找不到调试符号
- RegGetValue在当前用户下找不到名称
- 带有 -stdlib=libc++ 的 clang++ 9.0.1 找不到<optional>
- c++找不到具有相同哈希的无序集合元素
- 找不到以下加速库:boost_fiber
- 找不到 FLTK(缺少:FLTK_INCLUDE_DIR)
- 设置 Visual Studio for MPI: 找不到标识符错误
- "assimp/config.h"找不到,但它在文件夹中
- 链接器找不到在虚拟类 c++ 中访问的静态字段的符号
- C++系统找不到指定的文件错误
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- vcpkg 添加新库,但找不到
- Qt 5.11.2 (Clang 8.0 (Apple), 64 位), 找不到 QJSEngine 文件
- 找不到 n*n 矩阵的行列式