Fortran Program for transpose of matrix and check whether it is symmetric or not

Symmetric Matrix : Any square matrix is said to be symmetric if its transpose is equal to its matrix itself.

let A be the square matrix of 3X3 order and AT be its transpose matrix, then the condition for matrix to be symmetric is:

AT = A

If that condition is failed, then the matrix A is said to be anti-symmetric matrix.

Transpose of Matrix: The transpose of matrix A is defined as AT and can be formed by interchanging the row and column of that matrix.

So, If A = \begin{pmatrix}1 & 2 & 3\\4 & 5 & 6\\ 7 & 8 & 9  \end{pmatrix} \text{then} A^T = \begin{pmatrix}1 & 4 & 7\\2 & 5 & 8 \\ 3 & 6 & 9\end{pmatrix}

Here is some example of 3\times 3 symmetric matrix:

\begin{pmatrix}0 & -6 & 4\\-6 & 0 & 7\\ 4&7&0\end{pmatrix}, \begin{pmatrix}1 & 3 & 8\\3 & 8 & -4\\ 8&-4&6\end{pmatrix}, \begin{pmatrix}1 & 0 & 0\\0 & 1 & 0\\ 0&0&1\end{pmatrix}

Fortran Program

! fortran program to check the symmetricity of matrix
program symmetric
 implicit none
 integer, dimension(3,3) :: matA, matB	! checking for 3X3 matrix
  integer :: i, j
	character(len=2) :: ans			! variable ans is created

! asking the elements of matrix A
	do i = 1,3
		do j = 1, 3
			print*, "Enter"
			read(*,*) matA(i,j)
		end do
	end do

!b = transpose(a)			! intrinsic function to create matrix

! interchanging row and columns to find transpose 
do i = 1, 3
	do j=1,3
		matB(j,i) = matA(i, j)
	end do
end do

!-----------------------------------------
! you can use intrinsic function transpose() to create transpose on the go
! matB = transpose(matA)
!----------------------------------------


!-----------printing both matrices[optional]------------
print*,"----------------matrixA---------------"			! 
do i = 1,3												! 
	print*, (matA(i, j), j=1,3)							!  
end do													! 
														! 
print*,"--------transpose of matrix A---------"			! 
do i = 1,3												!  
	print*, (matB(i, j), j=1,3)							!   
end do													! 
!-------------------------------------------------------


! checking symmetric or not
do i = 1, 3
	do j = 1, 3
		if(matA(i,j) /= matB(i,j)) then
			ans="n"
			exit		! exit terminates these loop if condition failed
		end if
	end do
end do


! printing the final ans
if (ans == "n") then
	 print*, "Given matrix is not equal to its transpose"
	print*, "Therefore, the matrix is antisymmetric"
 else
	 print*, "Given matrix is equal to its transpose"
	 print*,"Therfore, the matrix is symmetric"
end if

end program

Want to Download?? Link Below, Password: 123

Share this post:

Leave a Comment

Your email address will not be published. Required fields are marked *