c语言矩阵的逆函数,求逆矩阵c语言
C语言 矩阵的逆
下面是实现Gauss-Jordan法实矩阵求逆。
成都创新互联-专业网站定制、快速模板网站建设、高性价比云岩网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式云岩网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖云岩地区。费用合理售后完善,十多年实体公司更值得信赖。
#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));
for (k=0; k=n-1; k++)
{ d=0.0;
for (i=k; i=n-1; i++)
for (j=k; j=n-1; j++)
{ l=i*n+j; p=fabs(a[l]);
if (pd) { d=p; is[k]=i; js[k]=j;}
}
if (d+1.0==1.0)
{ free(is); free(js); printf("err**not inv\n");
return(0);
}
if (is[k]!=k)
for (j=0; j=n-1; j++)
{ u=k*n+j; v=is[k]*n+j;
p=a[u]; a[u]=a[v]; a[v]=p;
}
if (js[k]!=k)
for (i=0; i=n-1; i++)
{ u=i*n+k; v=i*n+js[k];
p=a[u]; a[u]=a[v]; a[v]=p;
}
l=k*n+k;
a[l]=1.0/a[l];
for (j=0; j=n-1; j++)
if (j!=k)
{ u=k*n+j; a[u]=a[u]*a[l];}
for (i=0; i=n-1; i++)
if (i!=k)
for (j=0; j=n-1; j++)
if (j!=k)
{ u=i*n+j;
a[u]=a[u]-a[i*n+k]*a[k*n+j];
}
for (i=0; i=n-1; i++)
if (i!=k)
{ u=i*n+k; a[u]=-a[u]*a[l];}
}
for (k=n-1; k=0; k--)
{ if (js[k]!=k)
for (j=0; j=n-1; j++)
{ u=k*n+j; v=js[k]*n+j;
p=a[u]; a[u]=a[v]; a[v]=p;
}
if (is[k]!=k)
for (i=0; i=n-1; i++)
{ u=i*n+k; v=i*n+is[k];
p=a[u]; a[u]=a[v]; a[v]=p;
}
}
free(is); free(js);
return(1);
}
void brmul(double a[], double b[],int m,int n,int k,double c[])
{ int i,j,l,u;
for (i=0; i=m-1; i++)
for (j=0; j=k-1; j++)
{ u=i*k+j; c[u]=0.0;
for (l=0; l=n-1; l++)
c[u]=c[u]+a[i*n+l]*b[l*k+j];
}
return;
}
int main()
{ int i,j;
static double a[4][4]={ {0.2368,0.2471,0.2568,1.2671},
{1.1161,0.1254,0.1397,0.1490},
{0.1582,1.1675,0.1768,0.1871},
{0.1968,0.2071,1.2168,0.2271}};
static double b[4][4],c[4][4];
for (i=0; i=3; i++)
for (j=0; j=3; j++)
b[i][j]=a[i][j];
i=brinv(a,4);
if (i!=0)
{ printf("MAT A IS:\n");
for (i=0; i=3; i++)
{ for (j=0; j=3; j++)
printf("%13.7e ",b[i][j]);
printf("\n");
}
printf("\n");
printf("MAT A- IS:\n");
for (i=0; i=3; i++)
{ for (j=0; j=3; j++)
printf("%13.7e ",a[i][j]);
printf("\n");
}
printf("\n");
printf("MAT AA- IS:\n");
brmul(b,a,4,4,4,c);
for (i=0; i=3; i++)
{ for (j=0; j=3; j++)
printf("%13.7e ",c[i][j]);
printf("\n");
}
}
} 收起
参考资料:C常用算法程序集-徐士良
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));
for (k=0; k=n-1; k++)
{ d=0.0;
for (i=k; i=n-1; i++)
for (j=k; j=n-1; j++)
{ l=i*n+j; p=fabs(a[l]);
if (pd) { d=p; is[k]=i; js[k]=j;}
}
if (d+1.0==1.0)
{ free(is); free(js); printf("err**not inv\n");
return(0);
}
if (is[k]!=k)
for (j=0; j=n-1; j++)
{ u=k*n+j; v=is[k]*n+j;
p=a[u]; a[u]=a[v]; a[v]=p;
}
if (js[k]!=k)
for (i=0; i=n-1; i++)
{ u=i*n+k; v=i*n+js[k];
p=a[u]; a[u]=a[v]; a[v]=p;
}
l=k*n+k;
a[l]=1.0/a[l];
for (j=0; j=n-1; j++)
if (j!=k)
{ u=k*n+j; a[u]=a[u]*a[l];}
for (i=0; i=n-1; i++)
if (i!=k)
for (j=0; j=n-1; j++)
if (j!=k)
{ u=i*n+j;
a[u]=a[u]-a[i*n+k]*a[k*n+j];
}
for (i=0; i=n-1; i++)
if (i!=k)
{ u=i*n+k; a[u]=-a[u]*a[l];}
}
for (k=n-1; k=0; k--)
{ if (js[k]!=k)
for (j=0; j=n-1; j++)
{ u=k*n+j; v=js[k]*n+j;
p=a[u]; a[u]=a[v]; a[v]=p;
}
if (is[k]!=k)
for (i=0; i=n-1; i++)
{ u=i*n+k; v=i*n+is[k];
p=a[u]; a[u]=a[v]; a[v]=p;
}
}
free(is); free(js);
return(1);
}
void brmul(double a[], double b[],int m,int n,int k,double c[])
{ int i,j,l,u;
for (i=0; i=m-1; i++)
for (j=0; j=k-1; j++)
{ u=i*k+j; c[u]=0.0;
for (l=0; l=n-1; l++)
c[u]=c[u]+a[i*n+l]*b[l*k+j];
}
return;
}
int main()
{ int i,j;
static double a[4][4]={ {0.2368,0.2471,0.2568,1.2671},
{1.1161,0.1254,0.1397,0.1490},
{0.1582,1.1675,0.1768,0.1871},
{0.1968,0.2071,1.2168,0.2271}};
static double b[4][4],c[4][4];
for (i=0; i=3; i++)
for (j=0; j=3; j++)
b[i][j]=a[i][j];
i=brinv(a,4);
if (i!=0)
{ printf("MAT A IS:\n");
for (i=0; i=3; i++)
{ for (j=0; j=3; j++)
printf("%13.7e ",b[i][j]);
printf("\n");
}
printf("\n");
printf("MAT A- IS:\n");
for (i=0; i=3; i++)
{ for (j=0; j=3; j++)
printf("%13.7e ",a[i][j]);
printf("\n");
}
printf("\n");
printf("MAT AA- IS:\n");
brmul(b,a,4,4,4,c);
for (i=0; i=3; i++)
{ for (j=0; j=3; j++)
printf("%13.7e ",c[i][j]);
printf("\n");
}
}
}
用c语言怎么编写输入一个矩阵求其逆矩阵的程序?
这是我编的一个简易矩阵计算器,C++语言,非常容易理解的,你可以参考求行列式和逆部分
#include iostream
#include iomanip
#include conio.h
#include "windows.h"
#include string
using namespace std;
void gotoxy(int x,int y) // 列x: 0~79 行y: 0~24
{ HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
COORD coordScreen={x,y};
SetConsoleCursorPosition(hConsole,coordScreen);
return;
}
void setcolor(unsigned short ForeColor,unsigned short BackColor)
// 0--黑 1--暗蓝 2--暗绿 3--暗青 4--暗红 5--暗紫 6--蟹黄 7--暗白
// 8--灰 9--亮蓝 10-亮绿 11-亮青 12-亮红 13-亮紫 14-黄 15-亮白
{ HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,(ForeColor % 16)|(BackColor % 16 * 16));
};
int main()
{
void plu();
void sub();
void amo();
void mul();
void ran();
void ord();
char sel='1';
while(sel != '0')
{ int i;
system("cls"); // 清屏
setcolor(15,0); // 下面显示黑底亮青字
gotoxy(8,1); cout"┌───────────────────────────┐";
for(i=2;i20;i++)
{gotoxy(8,i);cout"│";gotoxy(64,i);cout"│";}
setcolor(15,6); // 下面显示红底白字
gotoxy(10,3); cout" ";
gotoxy(10,4); cout" 简 易 矩 阵 计 算 器 ";
gotoxy(10,5); cout" ";
setcolor(15,0); // 下面显示黑底亮青字
gotoxy(10,7); cout" 1 ---- 矩阵加法 2 ---- 矩阵减法 ";
gotoxy(10,9); cout" 3 ---- 矩阵数乘 4 ---- 矩阵乘法 ";
gotoxy(10,11); cout" 5 ---- 矩阵行列式 6 ---- 矩阵的逆 ";
gotoxy(10,13); cout" 0 ---- 退出 ";
gotoxy(10,15); cout" 请选择(0--6):";
gotoxy(8,20); cout"└───────────────────────────┘";
do
{ gotoxy(28,15); sel=getche( );}
while ( sel!='1' sel!='2' sel!='3' sel!='4' sel!='5' sel!='6' sel!='0');
switch(sel)
{
case '1':plu(); break;
case '2':sub(); break;
case '3':amo(); break;
case '4':mul(); break;
case '5':ran(); break;
case '6':ord(); break;
case '0': break;
}
}
system("cls");
gotoxy(25,10);
cout"谢 谢 使 用 系 统 !"endl;
return 0;
}
void plu()//加法
{ char l;
system("cls"); // 清屏
setcolor(14,0); // 下面用黑底黄字
int a,b,i,j;
gotoxy(0,0);cout" 矩阵加法 ";
gotoxy(0,2);cout"请输入矩阵的行数:";
cina;
coutendl;
cout"请输入矩阵的列数:";
cinb;
coutendl;
double m[10][10],n[10][10];
cout"请输入第一个矩阵:"endl;
for(i=0;ia;i++)
for(j=0;jb;j++)
{gotoxy(6*j+20,2*i+6); cinm[i][j];}
coutendlendl"请输入第二个矩阵:"endl;
for(i=0;ia;i++)
for(j=0;jb;j++)
{gotoxy(6*j+20,2*a+2*i+7);cinn[i][j];}
coutendl""endl"矩阵加法结果为:";
for(i=0;ia;i++)
for(j=0;jb;j++)
{gotoxy(6*j+20,4*a+2*i+8);coutm[i][j]+n[i][j];}
gotoxy(0,6*a+9);
cout"按任意键退出:";
l=getche();
}
void sub()//减法
{ char l;
system("cls"); // 清屏
setcolor(14,0); // 下面用黑底黄字
int a,b,i,j;
gotoxy(0,0);cout"矩阵减法";
gotoxy(0,2);cout"请输入矩阵的行数:";
cina;
coutendl;
cout"请输入矩阵的列数:";
cinb;
coutendl;
double m[10][10],n[10][10];
cout"请输入第一个矩阵:"endl;
for(i=0;ia;i++)
for(j=0;jb;j++)
{gotoxy(6*j+20,2*i+6); cinm[i][j];}
coutendlendl"请输入第二个矩阵:"endl;
for(i=0;ia;i++)
for(j=0;jb;j++)
{gotoxy(6*j+20,2*a+2*i+7);cinn[i][j];}
coutendl""endl"矩阵减法结果为:";
for(i=0;ia;i++)
for(j=0;jb;j++)
{gotoxy(6*j+20,4*a+2*i+8);coutm[i][j]-n[i][j];}
gotoxy(0,6*a+9);
cout"按任意键退出:";
l=getche();
}
void amo()//数乘
{ char h;
system("cls"); // 清屏
setcolor(14,0); // 下面用黑底黄字
int a,b,i,j;
gotoxy(0,0);cout"矩阵数乘";
gotoxy(0,2);cout"请输入矩阵的行数:";
cina;
coutendl;
cout"请输入矩阵的列数:";
cinb;
coutendl;
double m[10][10],c;
cout"请输入矩阵:"endl;
for(i=0;ia;i++)
for(j=0;jb;j++)
{gotoxy(6*j+20,2*i+6);cinm[i][j];}
coutendl"请输入与矩阵相乘的实数:";
cinc;
coutendlendl"矩阵数乘结果为:";
for(i=0;ia;i++)
for(j=0;jb;j++)
{gotoxy(8*j+20,2*a+2*i+9);coutm[i][j]*c;}
gotoxy(0,4*a+12);
cout"按任意键退出:";h=getche();
}
void mul()//乘法
{
char k;
system("cls"); // 清屏
setcolor(14,0); // 下面用黑底黄字
int a,b,c,i,j,q;
gotoxy(0,0);cout"矩阵乘法";
gotoxy(0,2);cout"请输入第一个矩阵的行数:";
cina;
coutendl"请输入第一个矩阵的列数:";
cinb;
coutendl"则第二个矩阵的行数也为:"b;
coutendlendl"请输入第二个矩阵的列数:";
cinc;
coutendl;
double m[10][10],n[10][10],p[10][10]={0};
cout"请输入第一个矩阵:"endl;
for(i=0;ia;i++)
for(j=0;jb;j++)
{gotoxy(6*j+18,2*i+10); cinm[i][j];}
coutendlendl"请输入第二个矩阵:";
for(i=0;ib;i++)
for(j=0;jc;j++)
{gotoxy(6*j+18,2*a+2*i+11);cinn[i][j];}
coutendl""endl"矩阵相乘结果为: ";
for(i=0;ia;i++)
for(j=0;jc;j++)
for(q=0;qb;q++) p[i][j]=p[i][j]+m[i][q]*n[q][j];
for(i=0;ia;i++)
for(j=0;jc;j++)
{gotoxy(10*j+18,2*a+2*b+2*i+12);coutp[i][j];}
gotoxy(16,2*a+2*b+2*i+15);
cout"按任意键退出:";k=getche();
}
//===================================================行列式
float Fun(int n1,float a1[10][10]);
void ran()
{
system("cls"); // 清屏
setcolor(15,0); // 下面用黑底黄字
char k;
int n,i,j;
cout"矩阵行列式"endlendl"请输入矩阵阶数: ";
cinn;
coutendl"请输入矩阵:"endl;
float a[10][10];
for(i=0;in;i++)
for(j=0;jn;j++)
{gotoxy(6*j+12,2*i+4);cina[i][j];}
coutendl"行列式为: "Fun(n,a)endlendl;
cout"按任意键退出:";
k=getche();
}
float Fun(int n1,float a1[10][10])//求行列式的递归函数
{
int i_1,j_1,c;//c为数组b的行
float b[10][10];
int p=0,q=0;
float sum=0;
if(n1==1) return a1[0][0];
for(i_1=0;i_1n1;i_1++)
{
for(c=0;cn1-1;c++)
{if(ci_1) p=0;
else p=1;
for(j_1=0;j_1n1-1;j_1++)
{b[c][j_1]=a1[c+p][j_1+1];}
}
if(i_1%2==0)
q=1;
else q=(-1);
sum=sum+a1[i_1][0]*q*Fun(n1-1,b);
}return sum;
}
//================================================================
void ord()
{
char g;
system("cls"); // 清屏
setcolor(15,0); // 下面用黑底黄字
int i,j,n;
gotoxy(0,0);cout"矩阵的逆";
gotoxy(0,2);cout"请输入矩阵的阶数:";
cinn;
coutendl;
cout"请输入矩阵:";
float l[10][10],m[10][10],p;
for(i=0;in;i++)
for(j=0;jn;j++)
{gotoxy(4*j+12,2*i+4); cinl[i][j];}
if(Fun(n,l)==0) coutendl"该矩阵无逆!!!"endl;
else
{p=Fun(n,l);
coutendl"矩阵的逆为: ";
for(i=0;in;i++)
for(j=0;jn;j++)
{{float f[10][10];
int r,w,e,d;//e为数组f的行数
for(int j_1=0,e=0;j_1n-1,en-1;j_1++,e++)
for(int i_1=0,d=0;i_1n-1,dn-1;i_1++,d++)
{if(ei) r=0;else r=1;
if(dj) w=0;else w=1;
f[i_1][j_1]=l[i_1+w][j_1+r];};
if((i+j)%2==0) m[i][j]=Fun(n-1,f)/p;
else m[i][j]=-Fun(n-1,f)/p;
};
gotoxy(9*j+12,2*n+2*i+4);coutm[i][j];};};
coutendlendl"按任意键退出:";g=getche();
}
C语言 求矩阵的逆
//源程序如下#includestdio.h
#includeconio.h
#includestring.h
#includeiostream.h
#includestdlib.h
#includemath.h
#define max 100void inputstyle(int *); //输入函数
void input(int **,int); //输入函数
long danx(int **,int);
int sgnx(int);
void martx(int **,int);int main(void)
{
int style=0,i=0;
int matrix[max][max],*p[max];
for(i=0;imax;i++)*(p+i)=matrix[i]; //*(p+i)是指针,指向第i个字符串
char exit1=' ';
while(exit1!='E' exit1!='e'){ printf("求n阶矩阵的逆\n"); inputstyle(style);
input(p,style);
printf("原矩阵为:\n");
for(i=0;istyle;i++){
for(int j=0;jstyle;j++){
printf("%4d",matrix[i][j]);
}
printf("\n");
}
martx(p,style);
printf("\n");
printf("Exit=e Continue=Press any key\n");
cinexit1;
fflush(stdin);
printf("\n\n"); }
return(0);
} void input(int **p,int n){
for(int i=0;in;i++){
for(int j=0;jn;j++){
printf("输入矩阵(%d行,%d列)元素:",j+1,i+1);
*(*(p+j)+i)=0;
scanf("%d",*(p+j)+i);
fflush(stdin);
}
}
}void inputstyle(int *style){
do{
printf("输入矩阵n*n阶数n(0n%d):",max);
fflush(stdin);
scanf("%d",style);
fflush(stdin);
}while(*style=0 *stylemax);
}long danx(int **p,int n){
int i=0,j1=0,k1=0,j2=0,k2=0;
long sum=0;
int operate[max][max],*po[max];
for(i=0;imax;i++)*(po+i)=operate[i]; if(n==1)return *(*(p+0)+0);
else{
for(i=0;in;i++){
for(j1=1,j2=0;j1n;j1++,j2++){
k1=-1;k2=-1;
while(k2n-1){
k1++;
k2++;
if(k1==i)k1++;
*(*(po+j2)+k2)=*(*(p+j1)+k1);
}
}
/*for(int i1=0;i1n-1;i1++){
for(int h1=0;h1n-1;h1++){
printf("(%d,%d)%d ",i1,h1,*(*(po+h1)+i1));
}
printf("\n");
}*/
sum+=*(*(p+0)+i) * sgnx(1+i+1) * danx(po,n-1);
}
return sum;
}
}int sgnx(int i){
if(i%2==0)return(1);
else return(-1);
}void martx(int **p,int n){
int i=0,j=0,j1=0,k1=0,j2=0,k2=0,num=0;
int tramform[max][max];
int operate[max][max],*po[max];
for(i=0;imax;i++)*(po+i)=operate[i];
num=danx(p,n);
if(num==0)printf("矩阵不可逆\n");
else{
if(n==1)printf("矩阵的逆为: 1/%d\n",num);
else{
printf("矩阵的逆为: 系数 1/%d *\n",num);
for(i=0;in;i++){
for(j=0;jn;j++){
j1=-1;j2=-1;
while(j2n-1){
j1++;j2++;
if(j1==j)j1++; k1=-1;k2=-1;
while(k2n-1){
k1++;
k2++;
if(k1==i)k1++;
*(*(po+j2)+k2)=*(*(p+j1)+k1);
}
}
tramform[i][j]=sgnx(2+i+j) * danx(po,n-1);
}
}
for(i=0;in;i++){
for(j=0;jn;j++){
printf("%4d",tramform[i][j]);
}
printf("\n");
}
}
}
}
//运行结果//希望对你有帮助
矩阵求逆c语言
Gauss Jordan Elimination Algorithm (高斯消除法)
int InverseMatrix_GaussianJordan(const float** fMat, float **invMat)
{
int k, l, m, n;
int iTemp;
float dTemp;
for (l = 0; l 6; l++)
{
for (m = 0; m 6; m++)
{
if (l == m)
invMat[l][m] = 1;
else
invMat[l][m] = 0;
}
}
for (l = 0; l 6; l++)
{
//Find pivot (maximum lth column element) in the rest (6-l) rows
iTemp = l;
for (m = l + 1; m 6; m++)
{
if (fMat[m][l] fMat[iTemp][l])
{
iTemp = m;
}
}
if (fabs(fMat[iTemp][l]) == 0)
{
return 1;
}
// Swap the row which has maximum lth column element
if (iTemp != l)
{
for (k = 0; k 6; k++)
{
dTemp = fMat[l][k];
fMat[l][k] = fMat[iTemp][k];
fMat[iTemp][k] = dTemp;
dTemp = invMat[l][k];
invMat[l][k] = invMat[iTemp][k];
invMat[iTemp][k] = dTemp;
}
}
// Perform row operation to form required identity matrix out of the Hessian matrix
for (m = 0; m 6; m++)
{
dTemp = fMat[m][l];
if (m != l)
{
for (n = 0; n 6; n++)
{
invMat[m][n] -= invMat[l][n] * dTemp / fMat[l][l];
fMat[m][n] -= fMat[l][n] * dTemp / fMat[l][l];
}
}
else
{
for (n = 0; n 6; n++)
{
invMat[m][n] /= dTemp;
fMat[m][n] /= dTemp;
}
}
}
}
return 0;
}
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; i m; i++)
for (j = 0; j n; j++)
{
for (k = 0; k m * n; k++)
B[k] = A[k];
{
for (x = 0; x n; x++)
B[i * n + x] = 0;
for (y = 0; y m; 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; i n; i++)
for (j = 0; j m; 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; i m; i++)
for (j = 0; j n; j++)
if ((i + j) % 2)
temp1 *= A[i * n + j];
else
temp *= A[i * n + j];
X = temp - temp1;
}
else
{
for (k = 0; k n; k++)
{
for (i = 0, j = k; i m, j n; i++, j++)
temp *= A[i * n + j];
if (m - i)
{
for (p = m - i, r = m - 1; p 0; p--, r--)
temp *= A[r * n + p - 1];
}
s += temp;
temp = 1;
}
for (k = n - 1; k = 0; k--)
{
for (i = 0, j = k; i m, j = 0; i++, j--)
temp1 *= A[i * n + j];
if (m - i)
{
for (p = m - 1, r = i; r m; 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 (num 10)
{
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("请输入矩阵维数:\n");
scanf("%d",num);
arr=(double *)malloc(sizeof(double)*num*num);
result=(double *)malloc(sizeof(double)*num*num);
printf("请输入%d*%d矩阵:\n", num, num);
for (i = 0; i num; i++)
{
for (j = 0; j num; j++)
{
scanf("%lf", arr[i * num + j]);
}
}
flag = matrix_inv(arr, num, result);
if(flag==0)
{
printf("逆矩阵为:\n");
for (i = 0; i num * 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语言矩阵的逆函数,求逆矩阵c语言
当前地址:http://pwwzsj.com/article/hddsjd.html