我已经做了一个计算矩阵行列式的程序。我的程序可以工作,但问题是它需要很长的时间来计算,特别是对于大型矩阵。你能告诉我如何在最短的时间内执行我的程序来计算行列式吗?
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;
}发布于 2012-10-01 22:23:03
您的算法看起来像是定义公式的直接实现,它是在O(n!)中实现的。
O(n^3)中的一个标准算法是首先使用Gauss elimination将其转换为三角矩阵。一旦你这样做了,行列式就是对角线元素的乘积。
https://stackoverflow.com/questions/12674580
复制相似问题