Friday, August 19, 2016

Transpose

Write a program  to  read  in  2  square  matrices  ( of  any  size ) . Show  that the matrices  follow  the  rule  (AB)T = BT AT where  AT  is   the  transpose  of  matrix  A .

Program Coding

Program Transpose
 implicit none
 integer :: i, j, k
 integer, parameter :: n=3
 integer :: A(n,n), B(n,n), At(n,n), Bt(n,n),                          AB(n,n),AB_t(n,n),BtAt(n,n)
 open(1, file="input.dat")
 open(2, file="output.dat")
 Do i=1,n
      read(1,7) (A(i, j), j=1,n) ! Input A Matrix
      7 format(/,3(i3,1x))
 end do
 Do i=1,n
      read(1,8) (B(i, j), j=1,n) ! Input B Matrix
     8 format(/,3(i3,1x))
 end do
 call trn(A, At)               ! Transpose of A matrix At
 call trn(B, Bt)               ! Transpose of B matrix Bt
 call prod(A, B, AB)     ! Product of A and B matrix AB
 call trn(AB, AB_t)       !Transpose of AB matrix AB_t
 call prod(Bt, At, BtAt) !Product of B transpose (Bt) and A
transpose (At)
 write(2,9) "Transpose of AB Matrix ="
 9 format(A24)
Do i=1, n
     Write(2,10) (AB_t(i, j), j=1,n)
     10 format(3(i4, 1x))
 end do
 write(2,11) "Product of B transpose and A transpose ="
 11 format(A40)
 Do i=1, n
     Write(2,12) (BtAt(i, j), j=1,n)
    12 format(3(i4, 1x))
 end do
 end program

 subroutine prod(A,B,AB)      !Product of A and B
 integer, parameter :: n=3
 integer :: i,j,k, A(n,n), B(n,n), AB(n,n)
   Do i=1,n
      Do j=1,n
          AB(i, j)=0.0
            Do k=1, n
            AB(i,j)=AB(i,j) + A(i,k)*B(k, j)
           end do
     end do
 end do
 end subroutine

 subroutine trn(A, At)         !Transpose of a matrix
 integer,parameter :: n=3
 integer :: i,j, A(n,n), At(n,n)
 Do i=1,n
    Do j=1,n
       At(i,j)= A(j, i)
     end do
 end do
 end subroutine



No comments:

Post a Comment