subroutine monomial_value ( m, n, e, x, value ) !*****************************************************************************80 ! !! MONOMIAL_VALUE evaluates a monomial. ! ! Discussion: ! ! This routine evaluates a monomial of the form ! ! product ( 1 <= i <= m ) x(i)^e(i) ! ! where the exponents are nonnegative integers. Note that ! if the combination 0^0 is encountered, it should be treated ! as 1. ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 20 April 2014 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer ( kind = 4 ) M, the spatial dimension. ! ! Input, integer ( kind = 4 ) N, the number of points at which the ! monomial is to be evaluated. ! ! Input, integer ( kind = 4 ) E(M), the exponents. ! ! Input, real ( kind = 8 ) X(M,N), the point coordinates. ! ! Output, real ( kind = 8 ) VALUE(N), the value of the monomial. ! implicit none integer ( kind = 4 ) m integer ( kind = 4 ) n integer ( kind = 4 ) e(m) integer ( kind = 4 ) i real ( kind = 8 ) value(n) real ( kind = 8 ) x(m,n) value(1:n) = 1.0D+00 do i = 1, m if ( 0 /= e(i) ) then value(1:n) = value(1:n) * x(i,1:n) ** e(i) end if end do return end function polygon_area ( nv, v ) !*****************************************************************************80 ! !! POLYGON_AREA determines the area of a polygon. ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 06 May 2014 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer ( kind = 4 ) NV, the number of vertices of the polygon. ! ! Input, real ( kind = 8 ) V(2,NV), the vertex coordinates. ! ! Output, real ( kind = 8 ) POLYGON_AREA, the area of the polygon. ! implicit none integer ( kind = 4 ) nv real ( kind = 8 ) area integer ( kind = 4 ) e(2) real ( kind = 8 ) polygon_area real ( kind = 8 ) v(2,nv) e(1) = 0 e(2) = 0 call polygon_monomial_integral ( nv, v, e, area ) polygon_area = area return end subroutine polygon_monomial_integral ( nv, v, e, nu_pq ) !*****************************************************************************80 ! !! POLYGON_MONOMIAL_INTEGRAL integrates a monomial over a polygon. ! ! Discussion: ! ! Nu(P,Q) = Integral ( x, y in polygon ) x^e(1) y^e(2) dx dy ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 03 October 2012 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! Carsten Steger, ! On the calculation of arbitrary moments of polygons, ! Technical Report FGBV-96-05, ! Forschungsgruppe Bildverstehen, Informatik IX, ! Technische Universitaet Muenchen, October 1996. ! ! Parameters: ! ! Input, integer ( kind = 4 ) NV, the number of vertices of the polygon. ! ! Input, real ( kind = 8 ) V(2,NV), the vertex coordinates. ! ! Input, integer ( kind = 4 ) E(2), the exponents of the monomial. ! ! Output, real ( kind = 8 ) NU_PQ, the unnormalized moment Nu(P,Q). ! implicit none integer ( kind = 4 ) nv integer ( kind = 4 ) e(2) integer ( kind = 4 ) i integer ( kind = 4 ) k integer ( kind = 4 ) l real ( kind = 8 ) nu_pq integer ( kind = 4 ) p integer ( kind = 4 ) q real ( kind = 8 ) r8_choose real ( kind = 8 ) s_pq real ( kind = 8 ) v(2,nv) real ( kind = 8 ) xi real ( kind = 8 ) xj real ( kind = 8 ) yi real ( kind = 8 ) yj p = e(1) q = e(2) nu_pq = 0.0D+00 xj = v(1,nv) yj = v(2,nv) do i = 1, nv xi = v(1,i) yi = v(2,i) s_pq = 0.0D+00 do k = 0, p do l = 0, q s_pq = s_pq & + r8_choose ( k + l, l ) * r8_choose ( p + q - k - l, q - l ) & * xi ** k * xj ** ( p - k ) & * yi ** l * yj ** ( q - l ) end do end do nu_pq = nu_pq + ( xj * yi - xi * yj ) * s_pq xj = xi yj = yi end do nu_pq = nu_pq / real ( p + q + 2, kind = 8 ) & / real ( p + q + 1, kind = 8 ) & / r8_choose ( p + q, p ) return end subroutine polygon_sample ( nv, v, n, seed, s ) !*****************************************************************************80 ! !! POLYGON_SAMPLE uniformly samples a polygon. ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 05 August 2005 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer ( kind = 4 ) NV, the number of vertices. ! ! Input, real ( kind = 8 ) V(2,NV), the vertices of the polygon, listed in ! counterclockwise order. ! ! Input, integer ( kind = 4 ) N, the number of points to create. ! ! Input/output, integer ( kind = 4 ) SEED, a seed for the random ! number generator. ! ! Output, real ( kind = 8 ) S(2,N), the points. ! implicit none integer ( kind = 4 ) n integer ( kind = 4 ) nv real ( kind = 8 ) area_cumulative(nv-2) real ( kind = 8 ) area_polygon real ( kind = 8 ) area_relative(nv-2) real ( kind = 8 ) area_triangle(nv-2) real ( kind = 8 ) area_percent integer ( kind = 4 ) i integer ( kind = 4 ) ip1 integer ( kind = 4 ) j integer ( kind = 4 ) k real ( kind = 8 ) r(2) real ( kind = 8 ) r8_uniform_01 integer ( kind = 4 ) seed real ( kind = 8 ) triangle_area integer ( kind = 4 ) triangles(3,nv-2) real ( kind = 8 ) s(2,n) real ( kind = 8 ) v(2,nv) ! ! Triangulate the polygon. ! call polygon_triangulate ( nv, v(1,1:nv), v(2,1:nv), triangles ) ! ! Determine the areas of each triangle. ! do i = 1, nv - 2 area_triangle(i) = triangle_area ( & v(1,triangles(1,i)), v(2,triangles(1,i)), & v(1,triangles(2,i)), v(2,triangles(2,i)), & v(1,triangles(3,i)), v(2,triangles(3,i)) ) end do ! ! Normalize the areas. ! area_polygon = sum ( area_triangle(1:nv-2) ) area_relative(1:nv-2) = area_triangle(1:nv-2) / area_polygon ! ! Replace each area by the sum of itself and all previous ones. ! area_cumulative(1) = area_relative(1) do i = 2, nv - 2 area_cumulative(i) = area_relative(i) + area_cumulative(i-1) end do do j = 1, n ! ! Choose triangle I at random, based on areas. ! area_percent = r8_uniform_01 ( seed ) do k = 1, nv - 2 i = k if ( area_percent <= area_cumulative(k) ) then exit end if end do ! ! Now choose a point at random in triangle I. ! call r8vec_uniform_01 ( 2, seed, r ) if ( 1.0D+00 < sum ( r(1:2) ) ) then r(1:2) = 1.0D+00 - r(1:2) end if s(1:2,j) = ( 1.0D+00 - r(1) - r(2) ) * v(1:2,triangles(1,i)) & + r(1) * v(1:2,triangles(2,i)) & + r(2) * v(1:2,triangles(3,i)) end do return end function r8_choose ( n, k ) !*****************************************************************************80 ! !! R8_CHOOSE computes the binomial coefficient C(N,K) as an R8. ! ! Discussion: ! ! The value is calculated in such a way as to avoid overflow and ! roundoff. The calculation is done in R8 arithmetic. ! ! The formula used is: ! ! C(N,K) = N! / ( K! * (N-K)! ) ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 24 March 2008 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! ML Wolfson, HV Wright, ! Algorithm 160: ! Combinatorial of M Things Taken N at a Time, ! Communications of the ACM, ! Volume 6, Number 4, April 1963, page 161. ! ! Parameters: ! ! Input, integer ( kind = 4 ) N, K, are the values of N and K. ! ! Output, real ( kind = 8 ) R8_CHOOSE, the number of combinations of N ! things taken K at a time. ! implicit none integer ( kind = 4 ) i integer ( kind = 4 ) k integer ( kind = 4 ) mn integer ( kind = 4 ) mx integer ( kind = 4 ) n real ( kind = 8 ) r8_choose real ( kind = 8 ) value mn = min ( k, n - k ) if ( mn < 0 ) then value = 0.0D+00 else if ( mn == 0 ) then value = 1.0D+00 else mx = max ( k, n - k ) value = real ( mx + 1, kind = 8 ) do i = 2, mn value = ( value * real ( mx + i, kind = 8 ) ) / real ( i, kind = 8 ) end do end if r8_choose = value return end function r8_uniform_01 ( seed ) !*****************************************************************************80 ! !! R8_UNIFORM_01 returns a unit pseudorandom R8. ! ! Discussion: ! ! An R8 is a real ( kind = 8 ) value. ! ! For now, the input quantity SEED is an integer variable. ! ! This routine implements the recursion ! ! seed = 16807 * seed mod ( 2^31 - 1 ) ! r8_uniform_01 = seed / ( 2^31 - 1 ) ! ! The integer arithmetic never requires more than 32 bits, ! including a sign bit. ! ! If the initial seed is 12345, then the first three computations are ! ! Input Output R8_UNIFORM_01 ! SEED SEED ! ! 12345 207482415 0.096616 ! 207482415 1790989824 0.833995 ! 1790989824 2035175616 0.947702 ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 05 July 2006 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! Paul Bratley, Bennett Fox, Linus Schrage, ! A Guide to Simulation, ! Springer Verlag, pages 201-202, 1983. ! ! Pierre L'Ecuyer, ! Random Number Generation, ! in Handbook of Simulation, ! edited by Jerry Banks, ! Wiley Interscience, page 95, 1998. ! ! Bennett Fox, ! Algorithm 647: ! Implementation and Relative Efficiency of Quasirandom ! Sequence Generators, ! ACM Transactions on Mathematical Software, ! Volume 12, Number 4, pages 362-376, 1986. ! ! Peter Lewis, Allen Goodman, James Miller ! A Pseudo-Random Number Generator for the System/360, ! IBM Systems Journal, ! Volume 8, pages 136-143, 1969. ! ! Parameters: ! ! Input/output, integer ( kind = 4 ) SEED, the "seed" value, which should ! NOT be 0. On output, SEED has been updated. ! ! Output, real ( kind = 8 ) R8_UNIFORM_01, a new pseudorandom variate, ! strictly between 0 and 1. ! implicit none integer ( kind = 4 ), parameter :: i4_huge = 2147483647 integer ( kind = 4 ) k real ( kind = 8 ) r8_uniform_01 integer ( kind = 4 ) seed if ( seed == 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'R8_UNIFORM_01 - Fatal error!' write ( *, '(a)' ) ' Input value of SEED = 0.' stop 1 end if k = seed / 127773 seed = 16807 * ( seed - k * 127773 ) - k * 2836 if ( seed < 0 ) then seed = seed + i4_huge end if r8_uniform_01 = real ( seed, kind = 8 ) * 4.656612875D-10 return end subroutine r8vec_uniform_01 ( n, seed, r ) !*****************************************************************************80 ! !! R8VEC_UNIFORM_01 returns a unit pseudorandom R8VEC. ! ! Discussion: ! ! An R8VEC is a vector of R8's. ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 05 July 2006 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! Paul Bratley, Bennett Fox, Linus Schrage, ! A Guide to Simulation, ! Springer Verlag, pages 201-202, 1983. ! ! Bennett Fox, ! Algorithm 647: ! Implementation and Relative Efficiency of Quasirandom ! Sequence Generators, ! ACM Transactions on Mathematical Software, ! Volume 12, Number 4, pages 362-376, 1986. ! ! Peter Lewis, Allen Goodman, James Miller ! A Pseudo-Random Number Generator for the System/360, ! IBM Systems Journal, ! Volume 8, pages 136-143, 1969. ! ! Parameters: ! ! Input, integer ( kind = 4 ) N, the number of entries in the vector. ! ! Input/output, integer ( kind = 4 ) SEED, the "seed" value, which ! should NOT be 0. On output, SEED has been updated. ! ! Output, real ( kind = 8 ) R(N), the vector of pseudorandom values. ! implicit none integer ( kind = 4 ) n integer ( kind = 4 ) i integer ( kind = 4 ) k integer ( kind = 4 ) seed real ( kind = 8 ) r(n) if ( seed == 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'R8VEC_UNIFORM_01 - Fatal error!' write ( *, '(a)' ) ' Input value of SEED = 0.' stop 1 end if do i = 1, n k = seed / 127773 seed = 16807 * ( seed - k * 127773 ) - k * 2836 if ( seed < 0 ) then seed = seed + 2147483647 end if r(i) = real ( seed, kind = 8 ) * 4.656612875D-10 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: ! ! 18 May 2013 ! ! 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.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