function nu = nu_measure ( dim_num, n, z, ns, sample_routine )

%*****************************************************************************80
%
%% nu_measure() determines the pointset quality measure NU.
%
%  Discussion:
%
%    The NU measure of dispersion for a set of N points in an
%    DIM_NUM-dimensional region is defined as follows:
%
%    For each element Z(I) of the pointset, let VOLUME(I) be the volume
%    of the corresponding Voronoi region, restricted to the region.
%
%    Then
%
%      NU = max ( 1 <= I <= N ) VOLUME(I) / min ( 1 <= I <= N ) VOLUME(I)
%
%    This quantity can be estimated by using a large number of sampling
%    points to estimate the Voronoi volumes.
%
%    For an ideally uniform pointset, the Voronoi volumes would be equal,
%    so that NU would be 1.  In any case, NU must be 1 or greater.  In
%    comparing two meshes, the one with the lower value of NU would be
%    preferred.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    08 July 2021
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Max Gunzburger and John Burkardt,
%    Uniformity Measures for Point Samples in Hypercubes.
%
%  Input:
%
%    integer DIM_NUM, the spatial dimension.
%
%    integer N, the number of points.
%
%    real Z(DIM_NUM,N), the points.
%
%    integer NS, the number of sample points.
%
%    character SAMPLE_ROUTINE, the name of the function to be used to
%    return sample points in the region.  The function must have the form
%      x = sample_routine ( dim_num, n )
%
%  Output:
%
%    real NU, the NU quality measure.
%
  hit(1:n) = 0;

  for k = 1 : ns

    x = feval ( sample_routine, 1, dim_num );

    closest = find_closest ( dim_num, n, 1, x, z );

    hit(closest(1)) = hit(closest(1)) + 1;

  end

  volume(1:n) = hit(1:n) / ns;

  if ( min ( volume(1:n) ) == 0.0 )
    nu = Inf;
  else
    nu = max ( volume(1:n) ) / min ( volume(1:n) );
  end

  return
end
