program main !*****************************************************************************80 ! !! lagrange_test() tests lagrange(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 05 August 2025 ! ! Author: ! ! John Burkardt ! implicit none call timestamp ( ) write ( *, '(a)' ) '' write ( *, '(a)' ) 'lagrange_test():' write ( *, '(a)' ) ' Fortran90 version' write ( *, '(a)' ) ' Test lagrange()' call lagrange_basis_antideriv_test ( ) call lagrange_basis_coef_test ( ) call lagrange_basis_deriv_test ( ) call lagrange_basis_deriv2_test ( ) call lagrange_basis_value_test ( ) ! ! Terminate. ! write ( *, '(a)' ) '' write ( *, '(a)' ) 'lagrange_test():' write ( *, '(a)' ) ' Normal end of execution.' write ( *, '(a)' ) '' call timestamp ( ) return end subroutine lagrange_basis_antideriv_test ( ) !*****************************************************************************80 ! !! lagrange_basis_antideriv_test() tests lagrange_basis_antideriv(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 05 August 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) character ( len = 255 ) command_filename integer command_unit character ( len = 255 ) data_filename integer data_unit character ( len = 255 ) data2_filename integer data2_unit integer i integer ipol integer nplot integer npol character ( len = 255 ) output_filename character ( len = 255 ) prefix character ( len = 255 ) prefix2 real ( kind = rk8 ), allocatable :: pcof_antideriv(:) real ( kind = rk8 ) r8poly_value real ( kind = rk8 ), allocatable :: xpol(:) real ( kind = rk8 ), allocatable :: xplot(:) real ( kind = rk8 ), allocatable :: yplot(:) write ( *, '(a)' ) '' write ( *, '(a)' ) 'lagrange_basis_antideriv_test():' write ( *, '(a)' ) ' lagrange_basis_antideriv() evaluates the antiderivative' write ( *, '(a)' ) ' of a Lagrange basis polynomial.' npol = 7 ipol = 2 allocate ( xpol(1:npol) ) call r8vec_linspace ( npol, -3.0D+00, +3.0D+00, xpol ) ! ! Get coefficients of antiderivative. ! allocate ( pcof_antideriv(1:npol+1) ) call lagrange_basis_antideriv ( npol, ipol, xpol, pcof_antideriv ) nplot = 101 allocate ( xplot(1:nplot) ) allocate ( yplot(1:nplot) ) call r8vec_linspace ( nplot, -3.3D+00, +3.5D+00, xplot ) do i = 1, nplot yplot(i) = r8poly_value ( npol, pcof_antideriv, xplot(i) ) end do prefix = 'lagrange_basis_antideriv' ! ! Create the data file. ! data_filename = trim ( prefix ) // '_data.txt' call get_unit ( data_unit ) open ( unit = data_unit, file = data_filename, status = 'replace' ) do i = 1, nplot write ( data_unit, '(2x,g14.6,2x,g14.6)' ) xplot(i), yplot(i) end do close ( unit = data_unit ) write ( *, '(a)' ) '' write ( *, '(a)' ) & ' Created graphics data file "' // trim ( data_filename ) // '".' ! ! Create the data2 file. ! data2_filename = trim ( prefix ) // '_data2.txt' call get_unit ( data2_unit ) open ( unit = data2_unit, file = data2_filename, status = 'replace' ) do i = 1, npol write ( data2_unit, '(2x,g14.6,2x,g14.6)' ) xpol(i), 0.0D+00 end do close ( unit = data2_unit ) write ( *, '(a)' ) '' write ( *, '(a)' ) & ' Created graphics data file "' // trim ( data2_filename ) // '".' ! ! Plot the selected data. ! command_filename = trim ( prefix ) // '_commands.txt' call get_unit ( command_unit ) open ( unit = command_unit, file = command_filename, status = 'replace' ) write ( command_unit, '(a)' ) '# ' // trim ( command_filename ) write ( command_unit, '(a)' ) '#' write ( command_unit, '(a)' ) '# Usage:' write ( command_unit, '(a)' ) '# gnuplot < ' // trim ( command_filename ) write ( command_unit, '(a)' ) '#' write ( command_unit, '(a)' ) 'set term png' write ( command_unit, '(a)' ) 'set nokey' output_filename = trim ( prefix ) // '.png' write ( command_unit, '(a)' ) & 'set output "' // trim ( output_filename ) // '"' write ( command_unit, '(a)' ) 'set xlabel "<---X--->"' write ( command_unit, '(a)' ) 'set ylabel "<---Antideriv(L(i)(X))--->"' call s_escape_tex2 ( prefix, prefix2 ) write ( command_unit, '(a)' ) 'set title "' // trim ( prefix2 ) // '"' write ( command_unit, '(a)' ) 'set grid' write ( command_unit, '(a)' ) 'set style data lines' write ( command_unit, '(a)' ) '$AXIS << EOL' write ( command_unit, '(a)' ) '-3.3 0.0' write ( command_unit, '(a)' ) ' 3.3 0.0' write ( command_unit, '(a)' ) 'EOL' write ( command_unit, '(a)' ) 'plot "' // trim ( data_filename ) & // '" using 1:2 lw 3 linecolor rgb "blue", \' write ( command_unit, '(a)' ) ' "' // trim ( data2_filename ) & // '" using 1:2 with points pt 7 ps 2 lc rgb "red", \' write ( command_unit, '(a)' ) ' $AXIS using 1:2 with lines lw 3 linecolor rgb "black"' close ( unit = command_unit ) write ( *, '(a)' ) & ' Created graphics command file "' // trim ( command_filename ) // '".' ! ! Free memory. ! deallocate ( pcof_antideriv ) deallocate ( xplot ) deallocate ( xpol ) deallocate ( yplot ) return end subroutine lagrange_basis_coef_test ( ) !*****************************************************************************80 ! !! lagrange_basis_coef_test() tests lagrange_basis_coef(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 27 September 2014 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer ipol integer npol real ( kind = rk8 ), allocatable :: pcof(:) character ( len = 15 ) s real ( kind = rk8 ), allocatable :: xpol(:) write ( *, '(a)' ) '' write ( *, '(a)' ) 'lagrange_basis_coef_test():' write ( *, '(a)' ) ' lagrange_basis_coef() returns the coefficients' write ( *, '(a)' ) ' for a Lagrange basis polynomial.' npol = 7 allocate ( xpol(1:npol) ) call r8vec_linspace ( npol, -3.0D+00, +3.0D+00, xpol ) call r8vec_print ( npol, xpol, ' Abscissas:' ) allocate ( pcof(1:npol+1) ) do ipol = 1, npol call lagrange_basis_coef ( npol, ipol, xpol, pcof ) write ( s, '(a,i1,a)' ) 'L(', ipol, ')(x)' call r8poly_print ( npol, pcof, s ) end do ! ! Free memory. ! deallocate ( pcof ) deallocate ( xpol ) return end subroutine lagrange_basis_deriv_test ( ) !*****************************************************************************80 ! !! lagrange_basis_deriv_test() tests lagrange_basis_deriv(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 05 August 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) character ( len = 255 ) command_filename integer command_unit character ( len = 255 ) data_filename integer data_unit character ( len = 255 ) data2_filename integer data2_unit integer i integer ipol integer nplot integer npol character ( len = 255 ) output_filename character ( len = 255 ) prefix character ( len = 255 ) prefix2 real ( kind = rk8 ), allocatable :: xpol(:) real ( kind = rk8 ), allocatable :: xplot(:) real ( kind = rk8 ), allocatable :: yplot(:) write ( *, '(a)' ) '' write ( *, '(a)' ) 'lagrange_basis_deriv_test():' write ( *, '(a)' ) ' lagrange_basis_deriv() evaluates the derivative' write ( *, '(a)' ) ' of a Lagrange basis polynomial.' npol = 7 ipol = 2 allocate ( xpol(1:npol) ) call r8vec_linspace ( npol, -3.0D+00, +3.0D+00, xpol ) nplot = 101 allocate ( xplot(1:nplot) ) allocate ( yplot(1:nplot) ) call r8vec_linspace ( nplot, -3.3D+00, +3.5D+00, xplot ) do i = 1, nplot call lagrange_basis_deriv ( npol, ipol, xpol, xplot(i), yplot(i) ) end do prefix = 'lagrange_basis_deriv' ! ! Create the data file. ! data_filename = trim ( prefix ) // '_data.txt' call get_unit ( data_unit ) open ( unit = data_unit, file = data_filename, status = 'replace' ) do i = 1, nplot write ( data_unit, '(2x,g14.6,2x,g14.6)' ) xplot(i), yplot(i) end do close ( unit = data_unit ) write ( *, '(a)' ) '' write ( *, '(a)' ) & ' Created graphics data file "' // trim ( data_filename ) // '".' ! ! Create the data2 file. ! data2_filename = trim ( prefix ) // '_data2.txt' call get_unit ( data2_unit ) open ( unit = data2_unit, file = data2_filename, status = 'replace' ) do i = 1, npol write ( data2_unit, '(2x,g14.6,2x,g14.6)' ) xpol(i), 0.0D+00 end do close ( unit = data2_unit ) write ( *, '(a)' ) '' write ( *, '(a)' ) & ' Created graphics data file "' // trim ( data2_filename ) // '".' ! ! Plot the selected data. ! command_filename = trim ( prefix ) // '_commands.txt' call get_unit ( command_unit ) open ( unit = command_unit, file = command_filename, status = 'replace' ) write ( command_unit, '(a)' ) '# ' // trim ( command_filename ) write ( command_unit, '(a)' ) '#' write ( command_unit, '(a)' ) '# Usage:' write ( command_unit, '(a)' ) '# gnuplot < ' // trim ( command_filename ) write ( command_unit, '(a)' ) '#' write ( command_unit, '(a)' ) 'set term png' write ( command_unit, '(a)' ) 'set nokey' output_filename = trim ( prefix ) // '.png' write ( command_unit, '(a)' ) & 'set output "' // trim ( output_filename ) // '"' write ( command_unit, '(a)' ) 'set xlabel "<---X--->"' write ( command_unit, '(a)' ) 'set ylabel "<---dL(i)(X)/dx)--->"' call s_escape_tex2 ( prefix, prefix2 ) write ( command_unit, '(a)' ) 'set title "' // trim ( prefix2 ) // '"' write ( command_unit, '(a)' ) 'set grid' write ( command_unit, '(a)' ) 'set style data lines' write ( command_unit, '(a)' ) '$AXIS << EOL' write ( command_unit, '(a)' ) '-3.3 0.0' write ( command_unit, '(a)' ) ' 3.3 0.0' write ( command_unit, '(a)' ) 'EOL' write ( command_unit, '(a)' ) 'plot "' // trim ( data_filename ) & // '" using 1:2 lw 3 linecolor rgb "blue", \' write ( command_unit, '(a)' ) ' "' // trim ( data2_filename ) & // '" using 1:2 with points pt 7 ps 2 lc rgb "red", \' write ( command_unit, '(a)' ) ' $AXIS using 1:2 with lines lw 3 linecolor rgb "black"' close ( unit = command_unit ) write ( *, '(a)' ) & ' Created graphics command file "' // trim ( command_filename ) // '".' ! ! Free memory. ! deallocate ( xplot ) deallocate ( xpol ) deallocate ( yplot ) return end subroutine lagrange_basis_deriv2_test ( ) !*****************************************************************************80 ! !! lagrange_basis_deriv2_test() tests lagrange_basis_deriv2(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 05 August 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) character ( len = 255 ) command_filename integer command_unit character ( len = 255 ) data_filename integer data_unit character ( len = 255 ) data2_filename integer data2_unit integer i integer ipol integer nplot integer npol character ( len = 255 ) output_filename character ( len = 255 ) prefix character ( len = 255 ) prefix2 real ( kind = rk8 ), allocatable :: xpol(:) real ( kind = rk8 ), allocatable :: xplot(:) real ( kind = rk8 ), allocatable :: yplot(:) write ( *, '(a)' ) '' write ( *, '(a)' ) 'lagrange_basis_deriv2_test():' write ( *, '(a)' ) ' lagrange_basis_deriv2() evaluates the second derivative' write ( *, '(a)' ) ' of a Lagrange basis polynomial.' npol = 7 ipol = 2 allocate ( xpol(1:npol) ) call r8vec_linspace ( npol, -3.0D+00, +3.0D+00, xpol ) nplot = 101 allocate ( xplot(1:nplot) ) allocate ( yplot(1:nplot) ) call r8vec_linspace ( nplot, -3.3D+00, +3.5D+00, xplot ) do i = 1, nplot call lagrange_basis_deriv2 ( npol, ipol, xpol, xplot(i), yplot(i) ) end do prefix = 'lagrange_basis_deriv2' ! ! Create the data file. ! data_filename = trim ( prefix ) // '_data.txt' call get_unit ( data_unit ) open ( unit = data_unit, file = data_filename, status = 'replace' ) do i = 1, nplot write ( data_unit, '(2x,g14.6,2x,g14.6)' ) xplot(i), yplot(i) end do close ( unit = data_unit ) write ( *, '(a)' ) '' write ( *, '(a)' ) & ' Created graphics data file "' // trim ( data_filename ) // '".' ! ! Create the data2 file. ! data2_filename = trim ( prefix ) // '_data2.txt' call get_unit ( data2_unit ) open ( unit = data2_unit, file = data2_filename, status = 'replace' ) do i = 1, npol write ( data2_unit, '(2x,g14.6,2x,g14.6)' ) xpol(i), 0.0D+00 end do close ( unit = data2_unit ) write ( *, '(a)' ) '' write ( *, '(a)' ) & ' Created graphics data file "' // trim ( data2_filename ) // '".' ! ! Plot the selected data. ! command_filename = trim ( prefix ) // '_commands.txt' call get_unit ( command_unit ) open ( unit = command_unit, file = command_filename, status = 'replace' ) write ( command_unit, '(a)' ) '# ' // trim ( command_filename ) write ( command_unit, '(a)' ) '#' write ( command_unit, '(a)' ) '# Usage:' write ( command_unit, '(a)' ) '# gnuplot < ' // trim ( command_filename ) write ( command_unit, '(a)' ) '#' write ( command_unit, '(a)' ) 'set term png' write ( command_unit, '(a)' ) 'set nokey' output_filename = trim ( prefix ) // '.png' write ( command_unit, '(a)' ) & 'set output "' // trim ( output_filename ) // '"' write ( command_unit, '(a)' ) 'set xlabel "<---X--->"' write ( command_unit, '(a)' ) 'set ylabel "<---d2L(i)(X)/dx2)--->"' call s_escape_tex2 ( prefix, prefix2 ) write ( command_unit, '(a)' ) 'set title "' // trim ( prefix2 ) // '"' write ( command_unit, '(a)' ) 'set grid' write ( command_unit, '(a)' ) 'set style data lines' write ( command_unit, '(a)' ) '$AXIS << EOL' write ( command_unit, '(a)' ) '-3.3 0.0' write ( command_unit, '(a)' ) ' 3.3 0.0' write ( command_unit, '(a)' ) 'EOL' write ( command_unit, '(a)' ) 'plot "' // trim ( data_filename ) & // '" using 1:2 lw 3 linecolor rgb "blue", \' write ( command_unit, '(a)' ) ' "' // trim ( data2_filename ) & // '" using 1:2 with points pt 7 ps 2 lc rgb "red", \' write ( command_unit, '(a)' ) ' $AXIS using 1:2 with lines lw 3 linecolor rgb "black"' close ( unit = command_unit ) write ( *, '(a)' ) & ' Created graphics command file "' // trim ( command_filename ) // '".' ! ! Free memory. ! deallocate ( xplot ) deallocate ( xpol ) deallocate ( yplot ) return end subroutine lagrange_basis_value_test ( ) !*****************************************************************************80 ! !! lagrange_basis_value_test() tests lagrange_basis_value(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 05 August 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) character ( len = 255 ) command_filename integer command_unit character ( len = 255 ) data_filename integer data_unit character ( len = 255 ) data2_filename integer data2_unit real ( kind = rk8 ) dpdx integer i integer ipol integer nplot integer npol character ( len = 255 ) output_filename character ( len = 255 ) prefix character ( len = 255 ) prefix2 real ( kind = rk8 ), allocatable :: xpol(:) real ( kind = rk8 ), allocatable :: xplot(:) real ( kind = rk8 ), allocatable :: yplot(:) write ( *, '(a)' ) '' write ( *, '(a)' ) 'lagrange_basis_value_test():' write ( *, '(a)' ) ' lagrange_basis_value() evaluates ' write ( *, '(a)' ) ' a Lagrange basis polynomial.' npol = 7 ipol = 2 allocate ( xpol(1:npol) ) call r8vec_linspace ( npol, -3.0D+00, +3.0D+00, xpol ) nplot = 101 allocate ( xplot(1:nplot) ) allocate ( yplot(1:nplot) ) call r8vec_linspace ( nplot, -3.3D+00, +3.5D+00, xplot ) do i = 1, nplot call lagrange_basis_value ( npol, ipol, xpol, xplot(i), yplot(i), dpdx ) end do prefix = 'lagrange_basis_value' ! ! Create the data file. ! data_filename = trim ( prefix ) // '_data.txt' call get_unit ( data_unit ) open ( unit = data_unit, file = data_filename, status = 'replace' ) do i = 1, nplot write ( data_unit, '(2x,g14.6,2x,g14.6)' ) xplot(i), yplot(i) end do close ( unit = data_unit ) write ( *, '(a)' ) '' write ( *, '(a)' ) & ' Created graphics data file "' // trim ( data_filename ) // '".' ! ! Create the data2 file. ! data2_filename = trim ( prefix ) // '_data2.txt' call get_unit ( data2_unit ) open ( unit = data2_unit, file = data2_filename, status = 'replace' ) do i = 1, npol write ( data2_unit, '(2x,g14.6,2x,g14.6)' ) xpol(i), 0.0D+00 end do close ( unit = data2_unit ) write ( *, '(a)' ) '' write ( *, '(a)' ) & ' Created graphics data file "' // trim ( data2_filename ) // '".' ! ! Plot the selected data. ! command_filename = trim ( prefix ) // '_commands.txt' call get_unit ( command_unit ) open ( unit = command_unit, file = command_filename, status = 'replace' ) write ( command_unit, '(a)' ) '# ' // trim ( command_filename ) write ( command_unit, '(a)' ) '#' write ( command_unit, '(a)' ) '# Usage:' write ( command_unit, '(a)' ) '# gnuplot < ' // trim ( command_filename ) write ( command_unit, '(a)' ) '#' write ( command_unit, '(a)' ) 'set term png' write ( command_unit, '(a)' ) 'set nokey' output_filename = trim ( prefix ) // '.png' write ( command_unit, '(a)' ) & 'set output "' // trim ( output_filename ) // '"' write ( command_unit, '(a)' ) 'set xlabel "<---X--->"' write ( command_unit, '(a)' ) 'set ylabel "<---L(i)(X)--->"' call s_escape_tex2 ( prefix, prefix2 ) write ( command_unit, '(a)' ) 'set title "' // trim ( prefix2 ) // '"' write ( command_unit, '(a)' ) 'set grid' write ( command_unit, '(a)' ) 'set style data lines' write ( command_unit, '(a)' ) '$AXIS << EOL' write ( command_unit, '(a)' ) '-3.3 0.0' write ( command_unit, '(a)' ) ' 3.3 0.0' write ( command_unit, '(a)' ) 'EOL' write ( command_unit, '(a)' ) '$BLIP << EOL' write ( command_unit, '(a)' ) '-2.0 0.0' write ( command_unit, '(a)' ) '-2.0 1.0' write ( command_unit, '(a)' ) 'EOL' write ( command_unit, '(a)' ) 'plot "' // trim ( data_filename ) & // '" using 1:2 lw 3 linecolor rgb "blue", \' write ( command_unit, '(a)' ) ' "' // trim ( data2_filename ) & // '" using 1:2 with points pt 7 ps 2 lc rgb "red", \' write ( command_unit, '(a)' ) ' $AXIS using 1:2 with lines lw 3 linecolor rgb "black", \' write ( command_unit, '(a)' ) ' $BLIP using 1:2 with lines lw 3 linecolor rgb "red"' close ( unit = command_unit ) write ( *, '(a)' ) & ' Created graphics command file "' // trim ( command_filename ) // '".' ! ! Free memory. ! deallocate ( xplot ) deallocate ( xpol ) deallocate ( yplot ) return end subroutine get_unit ( iunit ) !*****************************************************************************80 ! !! get_unit() returns a free Fortran unit number. ! ! Discussion: ! ! A "free" Fortran unit number is a value between 1 and 99 which ! is not currently associated with an I/O device. A free Fortran unit ! number is needed in order to open a file with the OPEN command. ! ! If IUNIT = 0, then no free Fortran unit could be found, although ! all 99 units were checked (except for units 5, 6 and 9, which ! are commonly reserved for console I/O). ! ! Otherwise, IUNIT is a value between 1 and 99, representing a ! free Fortran unit. The code assumes that units 5 and 6 ! are special, and will never return those values. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 26 October 2008 ! ! Author: ! ! John Burkardt ! ! Output: ! ! integer IUNIT, the free unit number. ! implicit none integer i integer ios integer iunit logical lopen iunit = 0 do i = 1, 99 if ( i /= 5 .and. i /= 6 .and. i /= 9 ) then inquire ( unit = i, opened = lopen, iostat = ios ) if ( ios == 0 ) then if ( .not. lopen ) then iunit = i return end if end if end if end do return end subroutine r8vec_linspace ( n, a, b, x ) !*****************************************************************************80 ! !! r8vec_linspace() creates a vector of linearly spaced values. ! ! Discussion: ! ! An R8VEC is a vector of R8's. ! ! 4 points evenly spaced between 0 and 12 will yield 0, 4, 8, 12. ! ! In other words, the interval is divided into N-1 even subintervals, ! and the endpoints of intervals are used as the points. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 14 March 2011 ! ! Author: ! ! John Burkardt ! ! Input: ! ! integer N, the number of entries in the vector. ! ! real ( kind = rk8 ) A, B, the first and last entries. ! ! Output: ! ! real ( kind = rk8 ) X(N), a vector of linearly spaced data. ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer n real ( kind = rk8 ) a real ( kind = rk8 ) b integer i real ( kind = rk8 ) x(n) if ( n == 1 ) then x(1) = ( a + b ) / 2.0D+00 else do i = 1, n x(i) = ( real ( n - i, kind = rk8 ) * a & + real ( i - 1, kind = rk8 ) * b ) & / real ( n - 1, kind = rk8 ) end do end if return end subroutine s_escape_tex2 ( s1, s2 ) !*****************************************************************************80 ! !! s_escape_tex2() de-escapes TeX escape sequences. ! ! Discussion: ! ! In particular, every occurrence of the characters '\', '_', ! '^', '{' and '}' will be replaced by '\\', '\_', '\^', ! '\{' and '\}'. A TeX interpreter, on seeing these character ! strings, is then likely to return the original characters. ! ! In some cases, it seems that TWO backslashes are needed. ! This version of the function provides them. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 19 January 2007 ! ! Author: ! ! John Burkardt ! ! Input: ! ! character ( len = * ) S1, the string to be de-escaped. ! ! Output: ! ! character ( len = * ) S2, a copy of the string, ! modified to avoid TeX escapes. ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) character ch character ( len = * ) s1 integer s1_length integer s1_pos character ( len = * ) s2 integer s2_pos s1_length = len_trim ( s1 ) s1_pos = 0 s2_pos = 0 s2 = ' ' do while ( s1_pos < s1_length ) s1_pos = s1_pos + 1 ch = s1(s1_pos:s1_pos) if ( ch == '\' .or. & ch == '_' .or. & ch == '^' .or. & ch == '{' .or. & ch == '}' ) then s2_pos = s2_pos + 1 s2(s2_pos:s2_pos) = '\' s2_pos = s2_pos + 1 s2(s2_pos:s2_pos) = '\' end if s2_pos = s2_pos + 1 s2(s2_pos:s2_pos) = ch 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: ! ! 15 August 2021 ! ! 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.2,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