问题描述
我已经做了一个程序来计算矩阵的行列式。我的程序工作,但问题是,它需要很长时间来计算,特别是对于大矩阵。你能告诉我如何执行我的程序,以便在最短的时间内计算行列式?
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;
}
推荐答案
您的算法定义公式的直接实现是在 O(n!)
。
Your algorithm, which looks like a straight-forward implementation of the definition formula, is in O(n!)
.
O(n ^ 3)
包括首先使用。一旦你这样做,行列式是对角元素的乘积。
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.
这篇关于矩阵37x37的行列式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!