function discrete_pdf_sample_2d_test02 ( n )

%*****************************************************************************80
%
%% discrete_pdf_sample_2d_test02() looks at a 12x8 region.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    15 December 2012
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N, the number of sample points to be generated.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'discrete_pdf_sample_2d_test02()\n' );
  fprintf ( 1, '  Consider data suggested by the shape and density of Iowa.\n' );
  fprintf ( 1, '  Generate %d samples\n', n );
%
%  Get the dimensions of the PDF data.
%
  [ n1, n2 ] = get_discrete_pdf_size2 ( );
  fprintf ( 1, '  PDF data is on a %d by %d grid.', n1, n2 );
%
%  Construct a PDF from the data.
%
  pdf = get_discrete_pdf_data2 ( n1, n2 );
%
%  "Integrate" the data over rows and columns of the region to get the CDF.
%
  cdf = set_discrete_cdf ( n1, n2, pdf );
%
%  Choose N CDF values at random.
%
  u = rand ( n, 1 );
%
%  Find the cell corresponding to each CDF value,
%  and choose a random point in that cell.
%
  xy = discrete_cdf_to_xy ( n1, n2, cdf, n, u );
%
%  Write data to a file for examination, plotting, or analysis.
%
  filename = 'discrete_pdf_sample_2d_test02.txt';
  r8mat_write ( filename, 2, n, xy );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Wrote sample data to file "%s".\n', filename );

  return
end
function pdf = get_discrete_pdf_data2 ( n1, n2 )

%*****************************************************************************80
%
%% get_discrete_pdf_data2() returns the discrete PDF data array.
%
%  Discussion:
%
%    Cell (I,J) extends from 
%
%      (I-1) * H < Y < I * H
%      (J-1) * H < X < J * H
%
%    We have data for each cell, representing the integral of some PDF
%    over that cell.  The function pdf(x,y) must be nonnegative.  However,
%    we don't impose any other conditions on it.
%
%    The array PDF(:,:) contains the integral of pdf(x,y) over each cell,
%    or, almost as good, simply a sample or average value.
%
%    We load the array PDF, and then we normalize it so that the sum of
%    all the entries is 1.  
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    15 Decdember 2012
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N1, N2, the number of rows and columns of PDF data.
%
%  Output:
%
%    real PDF(N1,N2).  PDF(I,J) is the discrete PDF 
%    for the cell (I,J), normalized so that the sum over all cells is 1.
%
  pdf = [ ...
     10.0,  20.0,  10.0,  10.0,  20.0,  10.0, ...
     30.0,  10.0,  10.0,  10.0,  10.0,  50.0; ...
     25.0,  30.0,  10.0,  25.0,  30.0,  40.0, ...
     30.0,  20.0,  10.0,  20.0,  30.0,  40.0; ...
     25.0,  30.0,  20.0,  10.0,  40.0, 200.0, ...
     50.0,  40.0,  10.0,  30.0,  60.0,  40.0; ...
     20.0,  30.0,  40.0,  10.0,  75.0, 100.0, ...
    100.0,  30.0,  25.0,  25.0,  90.0,  30.0; ...
     75.0,  15.0,  20.0,  10.0,  75.0,  50.0, ...
     40.0,  10.0, 100.0,  25.0,  25.0,  80.0; ...
     25.0,  50.0,  50.0,  10.0,  25.0,  25.0, ...
     15.0,  10.0,  25.0,  25.0,  10.0,  10.0; ...
    100.0,  50.0,  50.0,  10.0,  10.0,  10.0, ...
      5.0, 100.0,  50.0,  50.0,  10.0,  10.0; ...
     10.0,  10.0,  25.0,  50.0,  10.0,  50.0, ...
     10.0,  50.0,  25.0,  25.0,  25.0,  10.0 ]';
%
%  Normalize to get an integral of 1.
%
  total = sum ( sum ( pdf(1:n1,1:n2) ) );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  PDF data sums to %g\n', total );

  pdf(1:n1,1:n2) = pdf(1:n1,1:n2) / total;

  return
end
function [ n1, n2 ] = get_discrete_pdf_size2 ( )

%*****************************************************************************80
%
%% get_discrete_pdf_size2() returns the dimension of the discrete PDF data.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    15 December 2012
%
%  Author:
%
%    John Burkardt
%
%  Output:
%
%    integer N1, N2, the number of rows and columns of data.
%
  n1 = 12;
  n2 = 8;

  return
end
