function tau = trig_lagrange ( xi, xd, j )

%*****************************************************************************80
%
%% trig_lagrange() evaluates a trigonometric Lagrange polynomial.
%
%  Discussion:
%
%    The trigonometric Lagrange polynomials are similar to a Lagrange
%    polynomial basis, in that each function vanishes at all but one
%    data node.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    28 August 2019
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Anthony Austin, Lloyd Trefethen,
%    Trigonometric interpolation and quadrature at perturbed points,
%    SIAM Journal on Numerical Analysis,
%    Volume 55, Number 5, pages 2113-2122, 2017.
%
%  Input:
%
%    real XI(NI): interpolation points.
%
%    real XD(ND): the data nodes.
%    ND should be odd.
%
%    integer J: the index of the desired polynomial.
%    1 <= J <= ND.
%
%  Output:
%
%    real TAU(NI): the value of the trigonometric Lagrange polynomial 
%    associated with the J-th node at the interpolation nodes.
%
  ni = length ( xi );
  nd = length ( xd );

  xi = xi(:);
  tau = ones ( ni, 1 );
  for k = 1 : nd
    if ( k ~= j )
      tau(1:ni) = tau(1:ni) .* sin ( 0.5 * ( xi(1:ni) - xd(k) ) ) ...
                             / sin ( 0.5 * ( xd(j)    - xd(k) ) );
    end 
  end

  return
end

