NAG Library Manual, Mark 29.3
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_quad_dim1_fin_general (d01rjc) Example Program.
 *
 * Copyright 2023 Numerical Algorithms Group.
 *
 * Mark 29.3, 2023.
 */
#include <math.h>
#include <nag.h>
#include <stdio.h>

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

int main(void) {
  Integer exit_status = 0;
  double a, b, result, epsabs, epsrel, abserr, pi;
  Integer lrinfo, liinfo, maxsub;
  Integer *iinfo = 0;
  double *rinfo = 0;
  double ruser[1];

  /* Nag Types */
  NagError fail;
  Nag_Comm comm;

  INIT_FAIL(fail);

  printf("nag_quad_dim1_fin_general (d01rjc) Example Program Results\n\n");
  pi = nag_math_pi;
  epsabs = 0.0;
  epsrel = 1.0e-04;
  a = 0.0;
  b = 2.0 * pi;
  maxsub = 20;
  lrinfo = 4 * maxsub;
  liinfo = MAX(maxsub, 4);

  /* Allocate memory */
  if (!(rinfo = NAG_ALLOC(lrinfo, double)) ||
      !(iinfo = NAG_ALLOC(liinfo, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* pass constant to f through ruser */
  ruser[0] = 4.0 * pi * pi;
  comm.user = ruser;

  printf("a        -     lower limit of integration = %11.4f\n", a);
  printf("b        -     upper limit of integration = %11.4f\n", b);
  printf("epsabs   -    absolute accuracy requested = %11.2e\n", epsabs);
  printf("epsrel   -    relative accuracy requested = %11.2e\n", epsrel);
  printf("maxsub   - maximum number of subintervals = %11" NAG_IFMT "\n",
         maxsub);
  printf("\n");

  /* Evaluate the integral using the vectorized One-dimensional adaptive
   * quadrature routine nag_quad_dim1_fin_general (d01rjc).
   */
  nag_quad_dim1_fin_general(f, a, b, epsabs, epsrel, maxsub, &result, &abserr,
                            rinfo, iinfo, &comm, &fail);
  if (fail.code != NE_NOERROR)
    printf("Error or warning from nag_quad_dim1_fin_general (d01rjc) %s\n",
           fail.message);
  if (fail.code != NE_INT_ARG_LT && fail.code != NE_ALLOC_FAIL &&
      fail.code != NE_NO_LICENCE && fail.code != NE_USER_STOP) {
    printf("result   -  approximation to the integral = %11.4f\n", result);
    printf("abserr   - estimate of the absolute error = %11.2e\n", abserr);
    printf("iinfo(0) -    number of subintervals used = %11" NAG_IFMT "\n",
           iinfo[0]);
  } else if (fail.code == NE_USER_STOP) {
    printf("Exit requested from f with iflag = %" NAG_IFMT "\n", comm.iuser[0]);
  } else {
    exit_status = 1;
    goto END;
  }

END:

  NAG_FREE(rinfo);
  NAG_FREE(iinfo);

  return exit_status;
}

static void NAG_CALL f(const double x[], Integer nx, double fv[],
                       Integer *iflag, Nag_Comm *comm) {
  Integer i;

  /* Set iflag negative to terminate execution for any reason. */
  *iflag = 0;

  for (i = 0; i < nx; i++) {
    if (x[i] == 1.0) {
      *iflag = -1;
      /* Store chosen value of iflag in iuser */
      comm->iuser[0] = *iflag;
    }
  }
  if (*iflag == 0) {
    for (i = 0; i < nx; i++)
      fv[i] =
          x[i] * sin(30.0 * x[i]) / sqrt(1.0 - (x[i] * x[i]) / comm->user[0]);
  }
}