/* To verify trace AB = trace BA */
#include<stdio.h>
#include<math.h>
int main()
{
int i,j,n,m,p,k,a[20][20],b[20][20],e[20][20],sum=0,sum1=0,g[20][20];
printf("Enter the order of the matrix A:\n");
scanf("%d%d", &m ,&n);
printf("Enter the order of the matrix B:\n");
scanf("%d%d", &n,&p);
if(m==p)
{
printf("Enter the elements of the matrix A:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d", &a[i][j]);
printf("\n");
}
printf("Enter the elements of the matrix B:\n");
for(i=0;i<n;i++)
{
for(j=0;j<p;j++)
scanf("%d", &b[i][j]);
printf("\n");
}
/*matrix multiplication AB*/
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
e[i][j]=0;
for(k=0;k<n;k++)
e[i][j]=e[i][j]+a[i][k]*b[k][j];
}
}
printf("Product matrix AB is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
printf("%d\t",e[i][j]);
printf("\n");
}
/*To determine Trace of AB*/
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
if(i==j)
sum=sum+e[i][j];
}
printf("\nTrace of the matix AB is=%d",sum);
/*Matrix multiplication BA*/
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(m==p)
g[i][j]=0;
for(k=0;k<m;k++)
g[i][j]=g[i][j]+b[i][k]*a[k][j];
}
}
printf("\nProduct matrix BA is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d\t",g[i][j]);
printf("\n");
}
/*To determine Trace of BA*/
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(i==j)
sum1=sum1+g[i][j];
}
printf("\nTrace of BA matrix is=%d",sum1);
/*Checking two traces for equalities*/
if (sum==sum1)
printf("\nTherefore, Trace(AB) is equal to Trace(BA).\n");
else
printf("\nTherefore, Trace(AB) is not equal to Trace (BA).\n");
}
else
printf("Enter correct matrices.\n");
return(0);
}
#include<stdio.h>
#include<math.h>
int main()
{
int i,j,n,m,p,k,a[20][20],b[20][20],e[20][20],sum=0,sum1=0,g[20][20];
printf("Enter the order of the matrix A:\n");
scanf("%d%d", &m ,&n);
printf("Enter the order of the matrix B:\n");
scanf("%d%d", &n,&p);
if(m==p)
{
printf("Enter the elements of the matrix A:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d", &a[i][j]);
printf("\n");
}
printf("Enter the elements of the matrix B:\n");
for(i=0;i<n;i++)
{
for(j=0;j<p;j++)
scanf("%d", &b[i][j]);
printf("\n");
}
/*matrix multiplication AB*/
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
e[i][j]=0;
for(k=0;k<n;k++)
e[i][j]=e[i][j]+a[i][k]*b[k][j];
}
}
printf("Product matrix AB is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
printf("%d\t",e[i][j]);
printf("\n");
}
/*To determine Trace of AB*/
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
if(i==j)
sum=sum+e[i][j];
}
printf("\nTrace of the matix AB is=%d",sum);
/*Matrix multiplication BA*/
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(m==p)
g[i][j]=0;
for(k=0;k<m;k++)
g[i][j]=g[i][j]+b[i][k]*a[k][j];
}
}
printf("\nProduct matrix BA is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d\t",g[i][j]);
printf("\n");
}
/*To determine Trace of BA*/
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(i==j)
sum1=sum1+g[i][j];
}
printf("\nTrace of BA matrix is=%d",sum1);
/*Checking two traces for equalities*/
if (sum==sum1)
printf("\nTherefore, Trace(AB) is equal to Trace(BA).\n");
else
printf("\nTherefore, Trace(AB) is not equal to Trace (BA).\n");
}
else
printf("Enter correct matrices.\n");
return(0);
}
Comments
Post a Comment