Skip to main content

To verify the identity trace AB = trace BA for two matrices


/* 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);
}

Comments

Top 3 Popular Posts of All Time

C Program to Add of Two Numbers

The following program performs the arithmetic operation of adding two numbers and prints their sum on the screen. In this example, the user must input two numbers. Declare the Variables: The first step in adding two numbers using C programming is to declare the variables that will store the values of the two numbers. In C programming, we can declare variables using the syntax:  data_type variable_name;  For example, to declare two variables that will store the values of the two numbers, we can use the following code: int num1, num2; Get Input from User: After declaring the variables, we need to get the input from the user. To do this, we can use the scanf() function. The scanf() function reads the input from the user and stores it in the variables. Here is the code to get the input from the user: printf("Enter the first number: "); scanf("%d", &num1); printf("Enter the second number: "); scanf("%d", &num2); Add the Numbers:   Now that we

How to Install CERN ROOT on Ubuntu: A Step-by-Step Guide

If you're involved in scientific research or data analysis, you've likely heard of CERN ROOT. Developed by CERN (the European Organization for Nuclear Research), ROOT is an essential toolkit for high-energy physics data analysis. In this guide, we'll walk you through the process of installing CERN ROOT on your Ubuntu machine.  Prerequisites Before we dive into the installation process, make sure you have the following prerequisites in place: A working Ubuntu machine (this guide is tested on Ubuntu 23.04, but it should work on other versions as well). Sudo privileges to install packages. Downloading CERN ROOT Start by downloading the latest version of CERN ROOT from the official website  or open your terminal and use the following command to download the source code: wget https://root.cern/download/root_vX.YY.Z.source.tar.gz Replace X.YY.Z with the version number you want to install (e.g., 6.22.08). Installing Dependencies CERN ROOT relies on several libraries and tools. To

Use of TClonesArray Class

void write { TClonesArray * arr = new TClonesArray ( " TVector3 " ); TClonesArray & ar = * arr ; TFile * file = new TFile ( " file.root " , " recreate " ); TTree * tree = new TTree ( " tcl " , " tcl " ); tree -> Branch ( " array " , & arr ); TRandom2 * rand = new TRandom2 ( 1 ); for ( int i = 0 ; i < 100 ; i ++) { arr -> Clear (); for ( int j = 0 ; j < 1000 ; j ++) { double x = rand -> Rndm (); double y = rand -> Rndm (); double z = rand -> Rndm (); new ( ar [ j ]) TVector3 ( x , y , z ); } tree -> Fill (); } file -> Write (); file -> Close (); } void read () { TFile * file = new TFile ( " file.root " ); TTree * tree = ( TTree *) file -> Get ( " tcl " ); TClonesArray * arr = new