program main !*****************************************************************************80 ! !! MAIN is the main program for VECTOR_MAX. ! ! Discussion: ! ! This program investigates how the MAX function can be used in ! vector operations. ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 02 February 2010 ! ! Author: ! ! John Burkardt ! implicit none call timestamp ( ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'VECTOR_MAX' write ( *, '(a)' ) ' Investigate vectorized MAX operations.' call test01 ( ) ! ! Terminate. ! write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'VECTOR_MAX' 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 GNU LGPL license. ! ! Modified: ! ! 02 February 2010 ! ! Author: ! ! John Burkardt ! implicit none integer ( kind = 4 ), parameter :: n = 10 integer ( kind = 4 ) i integer ( kind = 4 ) seed integer ( kind = 4 ) x(n) integer ( kind = 4 ) y(n) integer ( kind = 4 ) 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:' seed = 123456789 call i4vec_uniform ( n, 0, 10, seed, x ) call i4vec_uniform ( n, 0, 10, seed, 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) )' seed = 123456789 call i4vec_uniform ( n, 0, 10, seed, x ) call i4vec_uniform ( n, 0, 10, seed, 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) )' seed = 123456789 call i4vec_uniform ( n, 0, 10, seed, x ) call i4vec_uniform ( n, 0, 10, seed, 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 ( n, a, b, seed, x ) !*****************************************************************************80 ! !! I4VEC_UNIFORM 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 GNU LGPL license. ! ! Modified: ! ! 31 May 2007 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! Paul Bratley, Bennett Fox, Linus Schrage, ! A Guide to Simulation, ! Second Edition, ! Springer, 1987, ! ISBN: 0387964673, ! LC: QA76.9.C65.B73. ! ! Bennett Fox, ! Algorithm 647: ! Implementation and Relative Efficiency of Quasirandom ! Sequence Generators, ! ACM Transactions on Mathematical Software, ! Volume 12, Number 4, December 1986, pages 362-376. ! ! Pierre L'Ecuyer, ! Random Number Generation, ! in Handbook of Simulation, ! edited by Jerry Banks, ! Wiley, 1998, ! ISBN: 0471134031, ! LC: T57.62.H37. ! ! Peter Lewis, Allen Goodman, James Miller, ! A Pseudo-Random Number Generator for the System/360, ! IBM Systems Journal, ! Volume 8, Number 2, 1969, pages 136-143. ! ! Parameters: ! ! Input, integer ( kind = 4 ) N, the dimension of the vector. ! ! Input, integer ( kind = 4 ) A, B, the limits of the interval. ! ! Input/output, integer ( kind = 4 ) SEED, the "seed" value, which ! should NOT be 0. On output, SEED has been updated. ! ! Output, integer ( kind = 4 ) X(N), a vector of numbers between A and B. ! implicit none integer ( kind = 4 ) n integer ( kind = 4 ) a integer ( kind = 4 ) b integer ( kind = 4 ) i integer ( kind = 4 ), parameter :: i4_huge = 2147483647 integer ( kind = 4 ) k real ( kind = 4 ) r integer ( kind = 4 ) seed integer ( kind = 4 ) value integer ( kind = 4 ) x(n) if ( seed == 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'I4VEC_UNIFORM - Fatal error!' write ( *, '(a)' ) ' Input value of SEED = 0.' stop end if do i = 1, n k = seed / 127773 seed = 16807 * ( seed - k * 127773 ) - k * 2836 if ( seed < 0 ) then seed = seed + i4_huge end if r = real ( seed, kind = 4 ) * 4.656612875E-10 ! ! Scale R to lie between A-0.5 and B+0.5. ! r = ( 1.0E+00 - r ) * ( real ( min ( a, b ), kind = 4 ) - 0.5E+00 ) & + r * ( real ( max ( a, b ), kind = 4 ) + 0.5E+00 ) ! ! Use rounding to convert R to an integer between A and B. ! value = nint ( r, kind = 4 ) value = max ( value, min ( a, b ) ) value = min ( value, max ( a, b ) ) x(i) = value 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 GNU LGPL license. ! ! Modified: ! ! 06 August 2005 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! None ! implicit none character ( len = 8 ) ampm integer ( kind = 4 ) d integer ( kind = 4 ) h integer ( kind = 4 ) m integer ( kind = 4 ) mm character ( len = 9 ), parameter, dimension(12) :: month = (/ & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' /) integer ( kind = 4 ) n integer ( kind = 4 ) s integer ( kind = 4 ) values(8) integer ( kind = 4 ) 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