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

NAG CL Interface Introduction
Example description
/* nag_quad_dim1_fin_osc_fn (d01rkc) Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 *
 * Mark 28.6, 2022.
 */
#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 key, lrinfo, liinfo, maxsub;
  Integer *iinfo = 0;
  double *rinfo = 0;

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

  INIT_FAIL(fail);

  printf("nag_quad_dim1_fin_osc_fn (d01rkc) Example Program Results\n\n");
  key = 6;
  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;
  }

  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_osc_fn (d01rkc).
   */
  nag_quad_dim1_fin_osc_fn(f, key, 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_osc_fn (d01rkc) %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\n");
  } 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++)
    fv[i] = x[i] * (sin(30.0 * x[i])) * cos(x[i]);
}