MODULE const INTEGER, PARAMETER :: dp = KIND(1.0d0), sp = KIND(1.0e0) REAL(KIND=dp), PARAMETER :: dpi = 3.141592653589793d0 END MODULE const PROGRAM prog USE const IMPLICIT NONE ! ! Program to compute various norms of a vector ! INTEGER,PARAMETER :: NMAX = 6000 INTEGER :: i,n,flag REAL(kind=dp) :: outnorm REAL(kind=dp) :: x(NMAX) ! Value of n obtained from the short line counting program n = 5432 ! Read data from file (NOTE: This only needs to be done ! once since we are not changing any of the x values.) DO i = 1,n READ(*,*) x(i) ENDDO ! Test all 3 norms flag = 1 CALL getnorms(n,x,flag,outnorm) WRITE(*,*) '1 norm = ',outnorm flag = 2 CALL getnorms(n,x,flag,outnorm) WRITE(*,*) '2 norm = ',outnorm flag = 3 CALL getnorms(n,x,flag,outnorm) WRITE(*,*) 'inf norm = ',outnorm END PROGRAM prog !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SUBROUTINE getnorms(n,vec,flag,norm) USE const IMPLICIT NONE ! ! Dummy declarations INTEGER,INTENT(IN) :: n,flag REAL(kind=dp),INTENT(IN) :: vec(n) REAL(kind=dp),INTENT(OUT) :: norm ! Local declarations INTEGER :: i norm = 0 IF(flag == 1) THEN ! Get 1 norm DO i = 1,n norm = norm + ABS(vec(i)) ENDDO ELSEIF(flag == 2) THEN ! Get 2 norm DO i = 1,n norm = norm + vec(i)**2 ENDDO norm = SQRT(norm) ELSE norm = ABS(vec(1)) DO i = 2,n IF(ABS(vec(i)) > norm) THEN norm = ABS(vec(i)) ENDIF ENDDO ENDIF RETURN END SUBROUTINE getnorms ! ! 1 norm = 24194.589651576633 ! 2 norm = 400.24498909113049 ! inf norm = 14.238625554646266