/* nag_quad_1d_inf_exp_wt (d01ubc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */
#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagd01.h>

#ifdef __cplusplus
extern "C"
{
#endif
  static void NAG_CALL fun(const double x[], double f[], const Integer n, 
                         Nag_Comm *comm, Integer *istop);
#ifdef __cplusplus
}
#endif

int main(void)
{
  static double ruser[2] = { -1.0, -1.0 };
  /* Scalars */
  Integer       exit_status = 0;
  double        ans;
  Integer       n;
  /* Nag Types */
  Nag_Comm      comm;
  NagError      fail;

  printf("nag_quad_1d_inf_exp_wt (d01ubc) Example Program Results\n\n");

  /* For communication with user-supplied functions: */
  comm.user = ruser;

  INIT_FAIL(fail);

  n = 10;

  /* Compute the one-dimensional integral, from zero to infinity,
   * of a function weighted by exp(-x*x), using
   * nag_quad_1d_inf_exp_wt (d01ubc).
   */
  nag_quad_1d_inf_exp_wt(fun, n, &ans, &comm, &fail);
  switch (fail.code) {
  case NE_NOERROR:
    {
      /* The definite integral has been estimated. */
      printf("Number of abscissae used  = %5ld\n", n);
      printf("approximation to integral = %10.5f\n", ans);
      break;
    }
  case NE_USER_STOP:
    {
      /* A requested exit was made in fun. */
      printf("A stop was requested in fun by setting istop < 0\n\n");
      printf("%s\n", fail.message);
      exit_status++;
      break;
    }
  default:
    {
      /* A solution could not be calculated due to an illegal parameter
       * or other failure.
       */
      printf("%s\n", fail.message);
      exit_status++;
    }
  }
  return exit_status;
}

static void NAG_CALL fun(const double x[], double f[], const Integer n,
                         Nag_Comm *comm, Integer *istop)
{
  Integer i;

  if (comm->user[0] == -1.0) {
    printf("(User-supplied callback fun, first invocation.)\n");
    comm->user[0] = 0.0;
  }
  for (i=0; i<n; i++) {
    f[i] = x[i];
  }
  *istop = 0;
}