// The easiest way to pull in all necessary definitions is to include oct.h
#include <octave/oct.h>
// Overwrite type names duplicated in Octave and NAG header files
#define Complex NagComplex
#define MatrixType NagMatrixType
// and include the appropriate NAG header files
#include <nag.h>
#include <nags.h>

/* The third parameter (nargout) is not used, so it is
   omitted from the list of arguments to DEFUN_DLD in order to avoid
   the warning from gcc about an unused function parameter. */
DEFUN_DLD (nag_lgamma, args, ,
  "Calls nag_log_gamma (s14abc), which returns\n\
the value of the logarithm of the Gamma function, ln Gamma(x)\n\
via the function name.\n")
{
  // Variable to store function output values
  octave_value_list retval;
  int nargin = args.length ();
  if (nargin != 1)
    {
      error ("Insufficient input arguments.");
    }
  else
    {
      // Retrieve input arguments from args
      if (!args(0).is_double_type())
        error ("nag_lgamma expected double x");
      else
        {
          double x = args(0).double_value();
          // Declare local variables
          double y;
          NagError fail;
          INIT_FAIL(fail);
          // Call NAG routine like in C
          /* nag_log_gamma (s14abc).
           * Log Gamma function ln Gamma(x)
           */
          y = s14abc(x,&fail);
          if (fail.code!=NE_NOERROR)
            {
              error ("nag_log_gamma (s14abc).\n%s\n",fail.message);
              goto END;
            }
          // Assign output arguments to retval      
          retval(0) = y;
          retval(1) = fail.code;
        }
    }

 END:
  return retval;
}
