該程序的功能主要解決一些簡單矩陣計算問題。
主要功能有:
① 矩陣輸入
② 矩陣輸出
③ 輸出矩陣的轉置矩陣(可轉置任意行列的矩陣)
④ 求方陣的行列式(如果你輸入錯誤,程序將提示你錯誤,你可關閉程序,重新輸入行列相同的矩陣,再進行計算)
⑤ 求矩陣的秩
⑥ 求矩陣的逆矩陣(前提:行和列相等)
⑦ 求兩個矩陣的乘積矩陣(其中之一是你已經輸入的那一個矩陣,另一個你可自行輸入,可計算多次乘積)
- /*
- author:wangchangshuai0010 sdust wangchangshuai0010.iteye.com
- */
- #include<stdio.h>
- #include<math.h>
- #define N 10
- void output(double a[][N],int am,int an)
- {
- int i,j;
- printf("\nThe OriginalMatrix A is:\n");
- printf("**********************************************\n");
- for(i=0;i<am;i++)
- {
- for(j=0;j<an;j++)
- {
- printf("%10.4f",a[i][j]);
- }
- printf("\n");
- }
- }
- void inver(double b[][N],int am,int an)
- {
- int i,j;
- double a[N][N],c[N][N];
- for(i=0;i<am;i++)
- {
- for(j=0;j<an;j++)
- {
- a[i][j]=b[i][j];
- }
- }
- for(i=0;i<am;i++)
- {
- for(j=0;j<an;j++)
- {
- c[j][i]=a[i][j];
- }
- }
- for(i=0;i<an;i++)
- {
- for(j=0;j<am;j++)
- {
- printf("%10.4f",c[i][j]);
- }
- printf("\n");
- }
- }
- double getdet(double b[][N],int am,int an)
- {
- int i,j,k,l,nexti,count=0;
- double temp,a[N][N],detA=1.0,x;
- for(i=0;i<am;i++)
- {
- for(j=0;j<an;j++)
- {
- a[i][j]=b[i][j];
- }
- }
- if(am==an)
- {
- for(i=0,j=0;i<am-1;i++,j++)
- {
- nexti=i;
- while(a[i][j]==0)
- {
- nexti++;
- temp=a[i][j];
- a[i][j]=a[nexti][j];
- a[nexti][j]=temp;
- }
- for(k=j+1;k<am;k++)
- {
- temp=a[i][k];
- a[i][k]=a[nexti][k];
- a[nexti][k]=temp;
- }
- for(l=i+1;l<an;l++)
- {
- if(a[l][j]==0)
- {l++;}
- else
- {
- x=a[l][j]/a[i][j];
- for(k=j;k<am;k++)
- {
- a[l][k]=a[l][k]-(x)*a[i][k];
- }
- }
- }
- }
- for(i=0,j=0;i<am;i++,j++)
- {
- detA*=a[i][j];
- }
- }
- else
- {
- printf("error!\n");
- }
- return detA;
- }
- void getrank(double b[][N],int am,int an)
- {
- int i,s,j,k,l,nexti,t=0,count=0;
- double temp,x,a[N][N],detA=1.0;
- if(am>an)
- {
- s=am;
- am=an;
- an=s;
- }
- for(i=0;i<am;i++)
- {
- for(j=0;j<an;j++)
- {
- a[i][j]=b[i][j];
- }
- }
- for(i=0,j=0;i<am-1;i++,j++)
- {
- nexti=i;
- while(a[i][j]==0)
- {
- nexti=i+1;
- temp=a[i][j];
- a[i][j]=a[nexti][j];
- a[nexti][j]=temp;
- }
- for(k=j+1;k<am;k++)
- {
- temp=a[i][k];
- a[i][k]=a[nexti][k];
- a[nexti][k]=temp;
- }
- for(l=i+1;l<an;l++)
- {
- if(a[l][j]==0)
- {l++;}
- else
- {
- x=a[l][j]/a[i][j];
- for(k=j;k<am;k++)
- {
- a[l][k]=a[l][k]-(x)*a[i][k];
- }
- }
- }
- }
- for(i=an-1;i<an;i++)
- {
- for(j=am;j<an;j++)
- {
- if(a[i][j]!=0)
- {
- t++;
- }
- }
- }
- if(t==0)
- {
- for(i=0,j=0;a[i][j]!=0&&i<an;i++,j++)
- {count++;}
- printf("the rank is %d\n",count);
- }
- else
- {
- printf("**********************************************\n");
- printf("the rank is %d\n",am);
- }
- }
- void getnijuzhen(double b[][N],int am,int an)
- {
- int i,j,k,l,m,n;
- double a[N][N],c[N][N],d[N][N],x;
- if(an!=am)
- {
- printf("error!\n");
- }
- else{
- for(i=0;i<am;i++)
- {
- for(j=0;j<an;j++)
- {
- a[i][j]=b[i][j];
- }
- }
- for(i=0;i<am;i++)
- {
- for(j=0;j<an;j++)
- {
- m=i;
- n=j;
- for(k=0,m=0;k<am-1;k++,m++)
- {
- if(k==m)
- {m++;}
- for(l=0,n=0;l<am-1;l++,n++)
- {
- if(l==j)
- {n++;}
- c[k][l]=a[m][n];
- }
- }
- d[i][j]=getdet(c,an-1,an-1)*pow(-1,i+j);
- }
- }
- x=getdet(a,an,am);
- if(x==0)
- {
- printf("error!can't get nijuzhen!\n");
- }
- else
- {
- for(i=0;i<am;i++)
- {
- for(j=0;j<an;j++)
- {
- d[i][j]=d[i][j]/x;
- }
- }
- printf("the nijuzhen of the matrix is:\n");
- printf("**********************************************\n");
- inver(d,an,an);
- }
- }
- }
- void multiply(double c[N][N],int am,int an)
- {
- int i,j,k,bm,bn;
- double a[N][N],b[N][N],d[N][N];
- printf("please input the row and line of matrix B:\n");
- scanf("%d %d",&bm,&bn);
- if(an!=bm)
- {
- printf("error!\n");
- }
- else
- {
- printf("please input the matrix A:\n");
- for(i=0;i<bm;i++)
- {
- for(j=0;j<bn;j++)
- {
- scanf("%lf",&b[i][j]);
- }
- }
- for(i=0;i<am;i++)
- {
- for(j=0;j<an;j++)
- {
- a[i][j]=c[i][j];
- }
- }
- for(i=0;i<am;i++)
- {
- for(j=0;j<bn;j++)
- {
- d[i][j]=0;
- for(k=0;k<bn;k++)
- d[i][j]+=a[i][k]*b[k][j];
- }
- }
- printf("the multiply of A and B is:\n");
- printf("**********************************************\n");
- for(i=0;i<am;i++)
- {
- for(j=0;j<bn;j++)
- {
- printf("%10.4lf",d[i][j]);
- }
- printf("\n");
- }
- }
- }
- void main()
- {
- int m,n,i,j,select;
- double a[N][N],detA;
- printf("please input the row and line of matrix A:\n");
- scanf("%d %d",&m,&n);
- printf("please input the matrix A:\n");
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- scanf("%lf",&a[i][j]);
- }
- }
- do
- {
- printf("**********************************************\n");
- printf("1-->output\n2-->inver\n3-->get detmination\n4->get rank\n5-->get nijuzhen\n6->get multiply of two matrixs\n0-->exit!\n");
- printf("**********************************************\n");
- do
- {
- printf("Please input your choice:\n(Warming:Please input number from 0 to 6,or it will be error!)\n");
- scanf("%d",&select);
- if(select>6||select<0)
- {
- printf("Error!please input the number again!\n");
- }
- }while(select>6||select<0);
- switch(select)
- {
- case 1:
- output(a,m,n);
- break;
- case 2:
- printf("\nThe InverseMatrix is:\n");
- printf("**********************************************\n");
- inver(a,m,n);
- break;
- case 3:
- detA=getdet(a,m,n);
- printf("the detmination is %.4lf\n",detA);
- break;
- case 4:
- getrank(a,m,n);
- break;
- case 5:
- getnijuzhen(a,m,n);
- break;
- case 6:
- multiply(a,m,n);
- default:
- break;
- }
- }while(select!=0);
- }