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的行列式矩阵?有人能帮我吗?提前谢谢。
对于N>2,行列式通常以递归形式计算为SUM(Ai0*det(Xi*(-1(i(,其中Xi是通过移除第一列和第i行获得的子矩阵。在C语言中,它可以写成:
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;
}