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

 

该程序的功能主要解决一些简单矩阵计算问题。

主要功能有:

① 矩阵输入

② 矩阵输出

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

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

⑤ 求矩阵的秩

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

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

下面看代码:

 

/*
author:wangchangshuai0010 sdust 
*/
#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);
}


   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值