program main !*****************************************************************************80 ! !! vector_max_test() investigates how the MAX function is used in vector operations. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 02 February 2010 ! ! Author: ! ! John Burkardt ! implicit none call timestamp ( ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'vector_max_test' write ( *, '(a)' ) ' FORTRAN90 version' write ( *, '(a)' ) ' Investigate vectorized MAX operations.' call test01 ( ) ! ! Terminate. ! write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'vector_max_test' write ( *, '(a)' ) ' Normal end of execution.' write ( *, '(a)' ) ' ' call timestamp ( ) stop end subroutine test01 ( ) !*****************************************************************************80 ! !! TEST01 computes the vector of maximum values of two vectors. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 02 February 2010 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: n = 10 integer i integer x(n) integer y(n) integer z(n) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST01:' write ( *, '(a)' ) ' Does FORTRAN90''s MAX function support vector operations?' write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' If we want to compute a vector Z whose entries are the' write ( *, '(a)' ) ' maximums of the corresponding entries of X and Y, can' write ( *, '(a)' ) ' we invoke a vector form of the computation instead of' write ( *, '(a)' ) ' using a loop? And can we replace one vector by a ' write ( *, '(a)' ) ' scalar constant?' write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' 1) We execute the old style code with a loop:' call i4vec_uniform_ab ( n, 0, 10, x ) call i4vec_uniform_ab ( n, 0, 10, y ) do i = 1, n z(i) = max ( x(i), y(i) ) end do write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' I X(I) Y(I) Z(I)' write ( *, '(a)' ) ' ' do i = 1, n write ( *, '(2x,i8,2x,i8,2x,i8,2x,i8)' ) i, x(i), y(i), z(i) end do write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' 2) Try Z(1:N) = max ( X(1:N), Y(1:N) )' call i4vec_uniform_ab ( n, 0, 10, x ) call i4vec_uniform_ab ( n, 0, 10, y ) z(1:n) = max ( x(1:n), y(1:n) ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' I X(I) Y(I) Z(I)' write ( *, '(a)' ) ' ' do i = 1, n write ( *, '(2x,i8,2x,i8,2x,i8,2x,i8)' ) i, x(i), y(i), z(i) end do write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' 3) Try Z(1:N) = max ( 5, Y(1:N) )' call i4vec_uniform_ab ( n, 0, 10, x ) call i4vec_uniform_ab ( n, 0, 10, y ) z(1:n) = max ( 5, y(1:n) ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' I X(I) Y(I) Z(I)' write ( *, '(a)' ) ' ' do i = 1, n write ( *, '(2x,i8,2x,i8,2x,i8,2x,i8)' ) i, 5, y(i), z(i) end do return end subroutine i4vec_uniform_ab ( n, a, b, x ) !*****************************************************************************80 ! !! i4vec_uniform_ab() returns a scaled pseudorandom I4VEC. ! ! Discussion: ! ! An I4VEC is a vector of I4's. ! ! The pseudorandom numbers should be scaled to be uniformly distributed ! between A and B. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 15 April 2025 ! ! Author: ! ! John Burkardt ! ! Input: ! ! integer n: the dimension of the vector. ! ! integer a, b: the limits of the interval. ! ! Output: ! ! integer x(n): a vector of numbers between A and B. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n integer a integer b integer i real ( kind = rk ) r integer x(n) do i = 1, n call random_number ( harvest = r ) x(i) = a + int ( ( b + 1 - a ) * r ) end do return end subroutine timestamp ( ) !*****************************************************************************80 ! !! timestamp() prints the current YMDHMS date as a time stamp. ! ! Example: ! ! 31 May 2001 9:45:54.872 AM ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 06 August 2005 ! ! Author: ! ! John Burkardt ! implicit none character ( len = 8 ) ampm integer d integer h integer m integer mm character ( len = 9 ), parameter, dimension(12) :: month = (/ & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' /) integer n integer s integer values(8) integer y call date_and_time ( values = values ) y = values(1) m = values(2) d = values(3) h = values(5) n = values(6) s = values(7) mm = values(8) if ( h < 12 ) then ampm = 'AM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Noon' else ampm = 'PM' end if else h = h - 12 if ( h < 12 ) then ampm = 'PM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Midnight' else ampm = 'AM' end if end if end if write ( *, '(i2,1x,a,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & d, trim ( month(m) ), y, h, ':', n, ':', s, '.', mm, trim ( ampm ) return end