Skip to main content

SUBDEM.F95

	PROGRAM SUBDEM
!
!  Purpose: To demonstrate the operation of a subroutine.
!
	IMPLICIT NONE
 	INTEGER:: MAT1(2,2),MAT2(2,2)	! Initial matrices
!
	INTEGER:: MPROD(2,2)		! Used to store matrix product
	INTEGER:: N=2,I,J		! Working variables
!
!  Initialization of MAT1 and MAT2:
	MAT1(1,1)=1
	MAT1(2,1)=2
	MAT1(1,2)=3
	MAT1(2,2)=4
	MAT2(1,1)=5
	MAT2(2,1)=6
	MAT2(1,2)=7
	MAT2(2,2)=8
!
	CALL MULMAT(MAT1,MAT2,MPROD,N)	! Multiply matrices
	WRITE(*,10)((MPROD(I,J),J=1,N),I=1,N)
   10	FORMAT(2I6)
!
!  Test: use the intrinsic function MATMUL (see page 6 of the notes)
	MPROD=0				! Zero elements of the product
	MPROD=MATMUL(MAT1,MAT2)		! Multiply matrices
	WRITE(*,*)' '
	WRITE(*,10)((MPROD(I,J),J=1,N),I=1,N)
!
	STOP
	END PROGRAM SUBDEM
!
	SUBROUTINE MULMAT(M1,M2,M3,N)
!
!  Subroutine to calculate the integer matrix product M1*M2
!  and place the result in M3.
!
	IMPLICIT NONE
	INTEGER, INTENT(IN):: M1(N,N),M2(N,N)	! Input matrices
	INTEGER, INTENT(OUT):: M3(N,N)		! Product matrix
	INTEGER, INTENT(IN):: N			! Order of matrix
	INTEGER:: I,J,K				! Working variables
!
	M3=0			! Zero elements of product matrix
!
!  Compute matrix product of M1 and M2 and place result in M3
!
	DO I=1,N
	  DO J=1,N
	    DO K=1,N
	      M3(I,J)=M3(I,J)+M1(I,K)*M2(K,J)
	    END DO
	  END DO
	END DO
	RETURN
	END SUBROUTINE MULMAT