如何使用递归函数计算 C 中矩阵的行列式
how to compute the determinant of a Matrix in C using recursive functions
你好,我正在尝试编写一个 C 程序来计算给定矩阵的行列式。我稍微完成了它,但是当我试图创建一个函数来查找给定矩阵和组件的子矩阵时,我被卡住了。我在每个部分都尽可能清楚地评论了,但我认为该程序的主要问题是最后一种方法 subMatrix.If 您可以帮助我修复它或提出替代解决方案,我将非常感激。
PS :我知道部分代码或注释可能不清楚,所以请随时在注释中问我。
#define MAX 10000
//here I was trying to make a "matrix" type to be able to return values in the function "subMatrix"
struct Matrix
{
double a[MAX][MAX];
};
struct Matrix subMatrix(int n, double m[n][n], int I, int J);
double determinant(int n, double M[n][n]);
int main()
{
int n, k = 0;
printf("how many rows does the matrix have");
scanf("%d", &n);
double Matrix[n][n];
double temp[n * n];
printf("enter the numbers in order with an enter after each one");
//gathering all the data from the user
for (int i = 0; i < n * n; i++)
{
scanf("%d", temp[i]);
}
//sorting the data into a matrix
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Matrix[i][j] = temp[k];
k++;
}
}
//prints the determinant
printf("%d",determinant(n,Matrix));
return 0;
}
//this recursive function calculates the determinant
double determinant(int n, double M[n][n])
{
double det = 0;
//the functions continues to call its self until n=2
if (n == 2)
{
det = M[0][0] *M[1][1]-M[0][1]*M[1][0];
}
else
{
for (int i = 0; i < n; i++)
{
det += M[0][i] * determinant(n - 1, subMatrix(n, M, 0, i));
}
}
return det;
}
//here I tried to make the subMatrix of a given matrix and one of its components
//by sub matrix I mean the matrix that doesn't include the row and columns that are in line with one of the matrix componants
struct Matrix subMatrix(int n, double m[n][n], int I, int J)
{
int i, a = 0, b = 0;
int j;
struct Matrix M[n - 1][n - 1];
for (i = 0; i < n; i++)
{
if (i != I)
{
for (j = 0; j < n; j++)
{
if (J != j)
{
M[a][b] = m[i][j];
b++;
}
}
a++;
}
}
return M;
}
您的代码存在多个问题,
subMatrix
正在返回 struct Matrix
的指针,但预计只是 struct Matrix
- 在
subMatrix
中,b
的值递增并且不会在新行上重置。
determinant
的参数期望 double M[n][n]
但在递归调用中传递 struct Matrix
- 局部变量的冗余使用
temp[n * n]
- 输入错误
n=1
从subMatrix
获取二维数组的简单解决方案是传递二维数组的引用并在函数中填充所需的值。
我试着把混乱简单化如下,
void subMatrix(int n, double m[n][n], int I, int J,double M[n-1][n-1])
{
int i, a = 0, b = 0;
int j;
for (i = 0; i < n; i++)
{
if (i == I)
{
continue;
}
b = 0;//in-order to start fresh for new row
for (j = 0; j < n; j++)
{
if (J == j)
{
continue;
}
M[a][b] = m[i][j];
b++;
}
a++;
}
}
//this recursive function calculates the determinant
double determinant(int n, double M[n][n])
{
double det = 0;
//the functions continues to call its self until n=2
if(n==1)
{
return M[0][0];
}
if (n == 2)
{
det = M[0][0] *M[1][1]-M[0][1]*M[1][0];
}
else
{
double subArray[n-1][n-1];
for (int i = 0; i < n; i++)
{
//subMatrix is filling the subArray
subMatrix(n,M,0,i,subArray);
det += M[0][i] * ((i&1)?-1:1)*determinant(n - 1,subArray);
}
}
return det;
}
int main()
{
int n, k = 0;
printf("how many rows does the matrix have");
scanf("%d", &n);
double Matrix[n][n];
printf("enter the numbers in order with an enter after each one");
//Taking user input for 2D array
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%lf", &Matrix[i][j]);
}
}
printf("%f",determinant(n,Matrix));
return 0;
}
这是我自己的解决方案,我没有早点 post 因为我找不到错误,但多亏了@Truthseeker,我发现我没有让 b=0 并且我明白了这个程序可以计算行列式,但求真者的答案在数学上是有缺陷的,因为他在行列式方法中忘记了 pow (-1, i)
#include <stdio.h>
#include <math.h>
void subMatrix(int n,double M[n-1][n-1], double m[n][n], int I, int J)
{
int i, a = 0, b = 0;
int j;
for (i = 0; i < n; i++)
{
if (i != I)
{
for (j = 0; j < n; j++)
{
if (J != j)
{
M[a][b] = m[i][j];
b++;
}
}
a++;
b=0;
}
}
}
//this recursive function calculates the determinant
double determinant (int n, double m[n][n])
{
double det = 0;
//the functions continues to call its self until n=2
if (n == 1)
{
return m[0][0];
}
if (n == 2)
{
det = m[0][0] * m[1][1] - m[0][1] * m[1][0];
}
else
{
double M[n - 1][n - 1];
for (int i = 0; i < n; i++)
{
subMatrix (n, M, m, 0, i);
det += m[0][i] * pow (-1, i) * determinant (n - 1, M);
}
}
return det;
}
int
main ()
{
int n, k = 0;
printf ("how many rows does the matrix have");
scanf ("%d", &n);
double Matrix[n][n];
printf ("enter the numbers in order with an enter after each one");
//Taking user input for 2D array
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
scanf ("%lf", &Matrix[i][j]);
}
}
//prints the determinant
printf ("the determinant is %lf\n", determinant (n, Matrix));
return 0;
}
你好,我正在尝试编写一个 C 程序来计算给定矩阵的行列式。我稍微完成了它,但是当我试图创建一个函数来查找给定矩阵和组件的子矩阵时,我被卡住了。我在每个部分都尽可能清楚地评论了,但我认为该程序的主要问题是最后一种方法 subMatrix.If 您可以帮助我修复它或提出替代解决方案,我将非常感激。
PS :我知道部分代码或注释可能不清楚,所以请随时在注释中问我。
#define MAX 10000
//here I was trying to make a "matrix" type to be able to return values in the function "subMatrix"
struct Matrix
{
double a[MAX][MAX];
};
struct Matrix subMatrix(int n, double m[n][n], int I, int J);
double determinant(int n, double M[n][n]);
int main()
{
int n, k = 0;
printf("how many rows does the matrix have");
scanf("%d", &n);
double Matrix[n][n];
double temp[n * n];
printf("enter the numbers in order with an enter after each one");
//gathering all the data from the user
for (int i = 0; i < n * n; i++)
{
scanf("%d", temp[i]);
}
//sorting the data into a matrix
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Matrix[i][j] = temp[k];
k++;
}
}
//prints the determinant
printf("%d",determinant(n,Matrix));
return 0;
}
//this recursive function calculates the determinant
double determinant(int n, double M[n][n])
{
double det = 0;
//the functions continues to call its self until n=2
if (n == 2)
{
det = M[0][0] *M[1][1]-M[0][1]*M[1][0];
}
else
{
for (int i = 0; i < n; i++)
{
det += M[0][i] * determinant(n - 1, subMatrix(n, M, 0, i));
}
}
return det;
}
//here I tried to make the subMatrix of a given matrix and one of its components
//by sub matrix I mean the matrix that doesn't include the row and columns that are in line with one of the matrix componants
struct Matrix subMatrix(int n, double m[n][n], int I, int J)
{
int i, a = 0, b = 0;
int j;
struct Matrix M[n - 1][n - 1];
for (i = 0; i < n; i++)
{
if (i != I)
{
for (j = 0; j < n; j++)
{
if (J != j)
{
M[a][b] = m[i][j];
b++;
}
}
a++;
}
}
return M;
}
您的代码存在多个问题,
subMatrix
正在返回struct Matrix
的指针,但预计只是struct Matrix
- 在
subMatrix
中,b
的值递增并且不会在新行上重置。 determinant
的参数期望double M[n][n]
但在递归调用中传递struct Matrix
- 局部变量的冗余使用
temp[n * n]
- 输入错误
n=1
从subMatrix
获取二维数组的简单解决方案是传递二维数组的引用并在函数中填充所需的值。
我试着把混乱简单化如下,
void subMatrix(int n, double m[n][n], int I, int J,double M[n-1][n-1])
{
int i, a = 0, b = 0;
int j;
for (i = 0; i < n; i++)
{
if (i == I)
{
continue;
}
b = 0;//in-order to start fresh for new row
for (j = 0; j < n; j++)
{
if (J == j)
{
continue;
}
M[a][b] = m[i][j];
b++;
}
a++;
}
}
//this recursive function calculates the determinant
double determinant(int n, double M[n][n])
{
double det = 0;
//the functions continues to call its self until n=2
if(n==1)
{
return M[0][0];
}
if (n == 2)
{
det = M[0][0] *M[1][1]-M[0][1]*M[1][0];
}
else
{
double subArray[n-1][n-1];
for (int i = 0; i < n; i++)
{
//subMatrix is filling the subArray
subMatrix(n,M,0,i,subArray);
det += M[0][i] * ((i&1)?-1:1)*determinant(n - 1,subArray);
}
}
return det;
}
int main()
{
int n, k = 0;
printf("how many rows does the matrix have");
scanf("%d", &n);
double Matrix[n][n];
printf("enter the numbers in order with an enter after each one");
//Taking user input for 2D array
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%lf", &Matrix[i][j]);
}
}
printf("%f",determinant(n,Matrix));
return 0;
}
这是我自己的解决方案,我没有早点 post 因为我找不到错误,但多亏了@Truthseeker,我发现我没有让 b=0 并且我明白了这个程序可以计算行列式,但求真者的答案在数学上是有缺陷的,因为他在行列式方法中忘记了 pow (-1, i)
#include <stdio.h>
#include <math.h>
void subMatrix(int n,double M[n-1][n-1], double m[n][n], int I, int J)
{
int i, a = 0, b = 0;
int j;
for (i = 0; i < n; i++)
{
if (i != I)
{
for (j = 0; j < n; j++)
{
if (J != j)
{
M[a][b] = m[i][j];
b++;
}
}
a++;
b=0;
}
}
}
//this recursive function calculates the determinant
double determinant (int n, double m[n][n])
{
double det = 0;
//the functions continues to call its self until n=2
if (n == 1)
{
return m[0][0];
}
if (n == 2)
{
det = m[0][0] * m[1][1] - m[0][1] * m[1][0];
}
else
{
double M[n - 1][n - 1];
for (int i = 0; i < n; i++)
{
subMatrix (n, M, m, 0, i);
det += m[0][i] * pow (-1, i) * determinant (n - 1, M);
}
}
return det;
}
int
main ()
{
int n, k = 0;
printf ("how many rows does the matrix have");
scanf ("%d", &n);
double Matrix[n][n];
printf ("enter the numbers in order with an enter after each one");
//Taking user input for 2D array
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
scanf ("%lf", &Matrix[i][j]);
}
}
//prints the determinant
printf ("the determinant is %lf\n", determinant (n, Matrix));
return 0;
}