function value = elliptic_pik ( n, k )

%*****************************************************************************80
%
%% ELLIPTIC_PIK evaluates the complete elliptic integral Pi(N,K).
%
%  Discussion:
%
%    This is one form of what is sometimes called the complete elliptic
%    integral of the third kind.
%
%    The function is defined by the formula:
%
%      Pi(N,K) = integral ( 0 <= T <= PI/2 )
%        dT / (1 - N sin^2(T) ) sqrt ( 1 - k^2 * sin ( T )^2 )
%
%    In MATLAB, the function can be evaluated by:
%
%      ellipticPi(n,k^2)
%
%    The value is computed using Carlson elliptic integrals:
%
%      Pi(n,k) = RF ( 0, 1 - k^2, 1 ) + 1/3 n RJ ( 0, 1 - k^2, 1, 1 - n )
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    22 June 2018
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real N, K, the arguments.
%
%    Output, real VALUE, the function value.
%
  x = 0.0;
  y = ( 1.0 - k ) * ( 1.0 + k );
  z = 1.0;
  p = 1.0 - n;
  errtol = 1.0E-03;

  [ value1, ierr ] = rf ( x, y, z, errtol );
  if ( ierr ~= 0 )
    fprintf ( 1, 'ELLIPTIC_PIK - Fatal error!\n' );
    fprintf ( 1, '  RF returned IERR = %d\n', ierr );
    error ( 'ELLIPTIC_PIK - Fatal error!' );
    return
  end

  [ value2, ierr ] = rj ( x, y, z, p, errtol );
  if ( ierr ~= 0 )
    fprintf ( 1, 'ELLIPTIC_PIK - Fatal error!\n' );
    fprintf ( 1, '  RJ returned IERR = %d\n', ierr );
    error ( 'ELLIPTIC_PIK - Fatal error!' );
    return
  end

  value = value1 + n * value2 / 3.0;

  return
end
