C语言编程:编写一个函数求逆矩阵#include stdio.h
#include stdlib.h
#include malloc.h
void MatrixOpp(double *A, int m, int n, double* invmat);
void MatrixInver(double *A, int m, int n, double* invmat);
double Surplus(double A[], int m, int n);
int matrix_inv(double* p, int num, double* invmat);
void MatrixOpp(double A[], int m, int n, double* invmat)
{
int i, j, x, y, k;
double *SP = NULL, *AB = NULL, *B = NULL, X;
SP = (double *) malloc(m * n * sizeof(double));
AB = (double *) malloc(m * n * sizeof(double));
B = (double *) malloc(m * n * sizeof(double));
X = Surplus(A, m, n);
X = 1 / X;
for (i = 0; im; i++)
【c语言构造逆矩阵函数 c语言构造矩阵的方法】for (j = 0; jn; j++)
{
for (k = 0; km * n; k++)
B[k] = A[k];
{
for (x = 0; xn; x++)
B[i * n + x] = 0;
for (y = 0; ym; y++)
B[m * y + j] = 0;
B[i * n + j] = 1;
SP[i * n + j] = Surplus(B, m, n);
AB[i * n + j] = X * SP[i * n + j];
}
}
MatrixInver(AB, m, n, invmat);
free(SP);
free(AB);
free(B);
}
void MatrixInver(double A[], int m, int n, double* invmat)
{
int i, j;
double *B = invmat;
for (i = 0; in; i++)
for (j = 0; jm; j++)
B[i * m + j] = A[j * n + i];
}
double Surplus(double A[], int m, int n)
{
int i, j, k, p, r;
double X, temp = 1, temp1 = 1, s = 0, s1 = 0;
if (n == 2)
{
for (i = 0; im; i++)
for (j = 0; jn; j++)
if ((i + j) % 2)
temp1 *= A[i * n + j];
else
temp *= A[i * n + j];
X = temp - temp1;
}
else
{
for (k = 0; kn; k++)
{
for (i = 0, j = k; im, jn; i++, j++)
temp *= A[i * n + j];
if (m - i)
{
for (p = m - i, r = m - 1; p0; p--, r--)
temp *= A[r * n + p - 1];
}
s += temp;
temp = 1;
}
for (k = n - 1; k = 0; k--)
{
for (i = 0, j = k; im, j = 0; i++, j--)
temp1 *= A[i * n + j];
if (m - i)
{
for (p = m - 1, r = i; rm; p--, r++)
temp1 *= A[r * n + p];
}
s1 += temp1;
temp1 = 1;
}
X = s - s1;
}
return X;
}
int matrix_inv(double* p, int num, double* invmat)
{
if (p == NULL || invmat == NULL)
{
return 1;
}
if (num10)
{
return 2;
}
MatrixOpp(p, num, num, invmat);
return 0;
}
int main()
{
int i, j;
int num;
double *arr=NULL;
double *result=NULL;
int flag;
printf("请输入矩阵维数c语言构造逆矩阵函数:\n");
scanf("%d",num);
arr=(double *)malloc(sizeof(double)*num*num);
result=(double *)malloc(sizeof(double)*num*num);
printf("请输入%d*%d矩阵c语言构造逆矩阵函数:\n", num, num);
for (i = 0; inum; i++)
{
for (j = 0; jnum; j++)
{
scanf("%lf", arr[i * num + j]);
}
}
flag = matrix_inv(arr, num, result);
if(flag==0)
{
printf("逆矩阵为c语言构造逆矩阵函数:\n");
for (i = 0; inum * num; i++)
{
printf("%lf\t ", *(result + i));
if (i % num == (num - 1))
printf("\n");
}
}
else if(flag==1)
{
printf("p/q为空\n");
}
else
{
printf("超过最大维数\n");
}
system("PAUSE");
free(arr);
free(result);
return 0;
}
c语言编写一个子函数求矩阵的逆矩阵#include stdlib.h
#include math.h
#include stdio.h
int brinv(double a[], int n)
{ int *is,*js,i,j,k,l,u,v;
double d,p;
is=malloc(n*sizeof(int));
js=malloc(n*sizeof(int));
推荐阅读
- 龙虾加工直播文案,龙虾加工直播文案范文
- 包含电力gis设备规范的词条
- 怎样关注宁波公运公众号,宁波公运网网上售票
- mysql怎么增加自增 mysql增加一个自增字段
- 如何提高电商的知识,如何提高电商服务质量
- 童年电脑桌面都有哪些游戏,童年电脑桌面都有哪些游戏
- 内存储存的ios是什么,ios内存几大区域
- 服务器交换机,服务器交换机存储连接
- 栗子直播录屏,栗子视频