function mu = mu_measure ( dim_num, n, z, ns, sample_routine )

%*****************************************************************************80
%
%% mu_measure() determines the pointset quality measure MU.
%
%  Discussion:
%
%    The MU measure of dispersion for a set of N points in an
%    DIM_NUM-dimensional region takes the ratio of the largest and
%    smallest half-diameters of the Voronoi cells defined by a pointset.
%
%    To compute this quantity exactly, for every point X in the region,
%    find the nearest element Z of the point set and compute the distance.
%
%    Then, for each element Z(I) of the point set, define H(I) to be the
%    maximum of these distances.
%
%    MU is then the ratio of the maximum and minimum values of H.
%
%    To ESTIMATE this quantity, carry out the same process, but only for
%    NS sample points in the region.
%
%    In an ideally regular mesh, MU would be 1.  MU must be at least 1.
%    Under this measure, the mesh with the smaller value of MU is to be
%    preferred.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    08 July 2021
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Max Gunzburger, 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 MU, the MU quality measure.
%
  h(1:n) = 0.0;

  for k = 1 : ns

    x = feval ( sample_routine, 1, dim_num );

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

    dist = sum ( ( x(1,1:dim_num) - z(closest(1),1:dim_num) ).^2 );

    h(closest(1)) = max ( h(closest(1)), dist );

  end

  h_max = sqrt ( max ( h(1:n) ) );
  h_min = sqrt ( min ( h(1:n) ) );

  if ( h_min == 0.0 )
    mu = Inf;
  else
    mu = h_max / h_min;
  end

  return
end
