如何找到行列式
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行列式矩阵?有人能帮我吗?提前致谢。
对于 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;
}
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;
}