function bk = shepard_basis_1d ( nd, xd, p, ni, xi )

%*****************************************************************************80
%
%% SHEPARD_BASIS_1D evaluates a 1D Shepard basis function.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    02 July 2015
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Donald Shepard,
%    A two-dimensional interpolation function for irregularly spaced data,
%    ACM '68: Proceedings of the 1968 23rd ACM National Conference,
%    ACM, pages 517-524, 1969.
%
%  Parameters:
%
%    Input, integer ND, the number of data points.
%
%    Input, real XD(ND,1), the data points.
%
%    Input, integer K, the index of the desired basis function,
%    1 <= K <= ND.
%
%    Input, real P, the power.
%
%    Input, integer NI, the number of interpolation points.
%
%    Input, real XI(NI,1), the interpolation points.
%
%    Output, real BK(NI,ND), the basis function at the interpolation points.
% 
  bk = zeros ( ni, nd );

  for i = 1 : ni

    if ( p == 0.0 )

      w(1,1:nd) = 1.0 / nd;

    else

      w = zeros ( 1, nd );

      z = -1;
      for j = 1 : nd
        w(1,j) = abs ( xi(i) - xd(j) );
        if ( w(1,j) == 0.0 )
          z = j;
          break
        end
      end

      if ( z ~= -1 )
        w = zeros ( 1, nd );
        w(1, z) = 1.0;
      else
        w(1, 1:nd) = 1.0 ./ w(1,1:nd) .^ p;
        s = sum ( w );
        w(1,1:nd) = w(1,1:nd) / s;
      end

    end

    bk(i,1:nd) = w(1,1:nd);

  end

  return
end
