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

NAG CL Interface Introduction
Example description
/* nag_quad_dim1_fin_gonnet_vec (d01rgc) Example Program.
 *
 * Copyright 2023 Numerical Algorithms Group.
 *
 * Mark 29.2, 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) {
  static double ruser[1] = {-1.0};
  Integer exit_status = 0;
  double a, b, dinest, epsabs, epsrel, errest;
  Integer nevals;
  Integer exmode[3], exmode_old[3];

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

  INIT_FAIL(fail);

  printf("nag_quad_dim1_fin_gonnet_vec (d01rgc) Example Program Results\n\n");

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

  /* The example function can raise various exceptions - it contains
   * a division by zero and a log singularity - although its integral
   * is well behaved.
   */

  /* nag_ieee_get_exception_mode (x07cac).
   * Gets current behaviour of floating point exceptions.
   */
  nag_ieee_get_exception_mode(exmode_old);

  /* Turn exception halting mode off for the three common exceptions:
   * overflow, division-by-zero, and invalid operation.
   */
  exmode[0] = exmode[1] = exmode[2] = 0;
  /* nag_ieee_set_exception_mode (x07cbc). Sets behaviour of
     floating-point exceptions */
  nag_ieee_set_exception_mode(exmode);
  epsabs = 0.0;
  epsrel = 1.0e-04;
  a = -1.0;
  b = 1.0;

  /* Evaluate the integral using the vectorized One-dimensional adaptive
   * quadrature routine nag_quad_dim1_fin_gonnet_vec (d01rgc).
   */
  nag_quad_dim1_fin_gonnet_vec(a, b, f, epsabs, epsrel, &dinest, &errest,
                               &nevals, &comm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_quad_dim1_fin_gonnet_vec (d01rgc).\n%s\n",
           fail.message);
    exit_status = 1;
    if (fail.code != NE_ACCURACY)
      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("\n");
  printf("dinest -  approximation to the integral = %11.4f\n", dinest);
  printf("errest - estimate of the absolute error = %11.2e\n", errest);
  printf("nevals - number of function evaluations = %11" NAG_IFMT "\n", nevals);

END:
  /* Restore the original halting mode */
  nag_ieee_set_exception_mode(exmode_old);

  return exit_status;
}

static void NAG_CALL f(const double x[], Integer nx, double fv[],
                       Integer *iflag, Nag_Comm *comm) {
  Integer i;
  if (comm->user[0] == -1.0) {
    printf("(User-supplied callback f, first invocation.)\n");
    comm->user[0] = 0.0;
  }
  for (i = 0; i < nx; i++)
    fv[i] = sin(x[i]) / x[i] * log(10.0 * (1.0 - x[i]));
}