如何找到行列式 [英] how to find determinant
本文介绍了如何找到行列式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
double Determinant(double *X, int N){
/*Solution*/
}
int main(void)
{
double X[] = {3, 1, 2,
7, 9, 2,
4, 6, 9};
if (Determinant(X,3) == 164)
{
printf("✓");
}
}
如何找到一维数组NxN行列式矩阵?有人能帮我吗?提前致谢。
how to find one dimensional array NxN determinant matrix? Can someone help me? thanks in advance.
推荐答案
对于N> 2,行列式通常以递归形式计算为SUM(a i0 * det(X i *(-1) i )其中X i 是通过删除第一列和第i个而获得的子矩阵第C行,在C语言中,可以写成:
A determinant is commonly computed in a recursive form for N > 2 as SUM(ai0 * det(Xi * (-1)i) where Xi is the sub-matrix obtained by removing the first column and the i-th line. In C language, it can be written as:
double Determinant(double *X, int N) {
if (N == 2) { // trivial for a 2-2 matrix
return X[0] * X[3] - X[1] * X[2];
}
// allocate a sequential array for the sub-matrix
double *Y = malloc((N - 1) * (N - 1) * sizeof(double));
// recursively compute the determinant
double det = 0.;
for (int k = 0, s = 1; k < N; k++) {
// build the submatrix
for (int i = 0, l=0; i < N; i++) {
if (i == k) continue;
for (int j = 1; j < N; j++) {
Y[l++] = X[j + i * N];
}
}
det += X[k * N] * Determinant(Y, N - 1) * s;
s = -s;
}
free(Y); // do not forget to de-alloc...
return det;
}
这篇关于如何找到行列式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文