您的位置:首页 > 其它

关于矩阵的一些操作(求转置矩阵、行列式、矩阵的秩、矩阵的逆矩阵、两个矩阵的乘积矩阵)

2012-03-13 12:56 573 查看
该程序的功能主要解决一些简单矩阵计算问题。

主要功能有:

① 矩阵输入

② 矩阵输出

③ 输出矩阵的转置矩阵(可转置任意行列的矩阵)

④ 求方阵的行列式(如果你输入错误,程序将提示你错误,你可关闭程序,重新输入行列相同的矩阵,再进行计算)

⑤ 求矩阵的秩

⑥ 求矩阵的逆矩阵(前提:行和列相等)

⑦ 求两个矩阵的乘积矩阵(其中之一是你已经输入的那一个矩阵,另一个你可自行输入,可计算多次乘积)

/*
author:wangchangshuai0010 sdust wangchangshuai0010.iteye.com
*/
#include<stdio.h>
#include<math.h>
#define N 10

void output(double a[]
,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[]
,int am,int an)
{
int i,j;
double a

,c

;
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[]
,int am,int an)
{
int i,j,k,l,nexti,count=0;
double temp,a

,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[]
,int am,int an)
{
int i,s,j,k,l,nexti,t=0,count=0;
double temp,x,a

,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[]
,int am,int an)
{
int i,j,k,l,m,n;
double a

,c

,d

,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]
;
}
}
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

,int am,int an)
{
int i,j,k,bm,bn;
double a

,b

,d

;
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

,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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: