FORTRAN 90 Lecturer : Rafel Hekmat Hameed University of Babylon Subject : Fortran 90 College of Engineering Year : Second B.Sc. Mechanical Engineering Dep. S Sppeecciiaall FFoorrm mss ooff M Maattrriicceess A square matrix is said to be upper triangular when all the elements below the diagonal are zero. The matrix below is an upper triangular matrix. In an upper triangular matrix, not all elements above the diagonal need to be non-zero. A square matrix is said to be lower triangular, when all the elements above the diagonal are zero. The matrix below is a lower triangular matrix. In a lower triangular matrix, not all elements below the diagonal need to be non-zero. A square matrix is said to be diagonal, if all elements are zero, except those in the diagonal. The matrix below is a diagonal matrix. ϭ A diagonal matrix is called a scalar matrix, if a11 = a22 = a33 = … = ann = k where k is a scalar. The matrix D below is a scalar matrix with k=4. A scalar matrix with k=1 , is called an identity matrix . Shown below are 2 2, 33 , and 44 identity matrices. The transpose of a matrix A, denoted as AT, is the matrix that is obtained when the rows and columns of matrix A are interchanged. A symmetric matrix A , is one such that AT=A , that is, the transpose of a matrix A is the same as A . An example of a symmetric matrix is shown below. EXAMPLE Write a fortran 90 program to read two square matrices A & B with rank 2 and shape (/3,3/) by using data statement, then do the following: 1. Construct [C] = [A]T . By using subroutine program. 2. Construct [D] = [C] [B]. By using subroutine program. 3. Find the largest element in the minor diagonal of [D]. Use format statement to print your results in output file. Ͷ A =ʹ ͻ ͺ ͷ ͳ ʹ ൩ ͳ B =ͳͲ ͺ , Ϯ ͵ ͻ ʹ ൩ ͷ Ͷ implicit none integer,parameter::n=3 integer,dimension(n,n)::a ,b , c, d integer,dimension(n)::e integer::i,j,max data a/4,2,9,8,5,1,2,7,6/ data b/1,10,8,3,2,5,9,6,4/ ! construct C call trans (a,c) write(*,6)((c(i,j),j=1,n),i=1,n) PRINT*,"****************" ! construct D call multi (c,b,d) write(*,6)((d(i,j),j=1,n),i=1,n) PRINT*,"****************" ! larger element in the minor diagonal do i=1,n ; do j=1,n if (i+j==n+1) e(i)=d(i,j) enddo ; enddo max=e(1) do i=2,n if(e(i).gt.max)then max=e(i) ; endif enddo write(*,9) max 9 format(2x,"max =",i5) 6 format(3(2x,i3)) ; End subroutine trans (a,c) implicit none integer,parameter::n=3 integer,dimension(n,n)::a,c ϯ integer i,j do i=1,n ; do j=1,n c(i,j)= a(j,i) enddo ; enddo end subroutine multi (c,b,d) implicit none integer,parameter::n=3 integer,dimension(n,n)::b,c,d integer i,j,k do i=1,n do j=1,n d(i,j)=0 do k=1,n d(i,j)=d(i,j)+c(i,k)*b(k,j) enddo ; enddo ; enddo end EXAMPLE Two matrix A with rank 2 and shape (/4,2/) and matrix B with rank 2 and shape (/2,4/) read these matrices by using reading statement from input file and do the following 1. 2. 3. 4. Construct [C] = [A] [B]. By using subroutine program Transpose [C] Construct [D], which is a diagonal matrix of [C] Construct [E], which is an lower triangular matrix has the element of [C]T Use format statement to print all generated matrices. ͷ ʹ ͻ ͳ A= ͵ ͻ ͳ , B=ቂ ϰ ͺ ͳͲ ͷ ʹ ͳ ቃ ͵ Ͷ implicit none integer,parameter::n=4,m=2 integer,dimension(n,m)::a integer,dimension(m,n)::b integer,dimension(n,n)::c,d,e integer::i,j,z open(unit=10,file="rh.dat") read(10,*)((a(i,j),j=1,m),i=1,n) read(10,*)((b(i,j),j=1,n),i=1,m) ! construct C call multi (a,b,c) write(*,6)((c(i,j),j=1,n),i=1,n) PRINT*,"****************" ! Transpose C do i=1,n ; do j=i,n z=c(i,j) ; c(i,j)=c(j,i) enddo ; enddo ; ; c(j,i)=z write(*,6) ((c(i,j),j=1,n),i=1,n) PRINT*,"****************" ! construct D do i=1,n ; do j=1,n if(i==j) then d(i,j)=c(i,j) else d(i,j)=0 endif enddo ; enddo ; PRINT*,"****************" write(*,6)((d(i,j),j=1,n),i=1,n) ! construct E do i=1,n ; do j=1,n ; do j=i+1,n e(i,j)=c(i,j) enddo ; enddo do i=1,n-1 ϱ e(i,j)=0 ; enddo ; enddo write(*,6)((e(i,j),j=1,n),i=1,n) 6 format (4(2x,i3)) end subroutine multi (a,b,c) implicit none integer,parameter::n=4,m=2 integer,dimension(n,m)::a integer,dimension(m,n)::b integer,dimension(n,n)::c integer i,j,k do i=1,n do j=1,n c(i,j)=0 do k=1,m c(i,j)=c(i,j)+a(i,k)*b(k,j) enddo ; enddo ; enddo end ϲ