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

NAG CL Interface Introduction
Example description
/* nag_quad_dim1_fin_brkpts (d01rlc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.1, 2024.
 */
#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;
  Integer lrinfo, liinfo, maxsub, npts;
  Integer *iinfo = 0;
  double *rinfo = 0;
  double points[1];

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

  INIT_FAIL(fail);

  printf("nag_quad_dim1_fin_brkpts (d01rlc) Example Program Results\n\n");
  epsabs = 0.0;
  epsrel = 1.0e-04;
  a = 0.0;
  b = 1.0;
  maxsub = 20;
  npts = 1;
  points[0] = 1.0 / 7.0;
  lrinfo = 4 * (MAX(maxsub, npts) + 1) + npts + 2;
  liinfo = 2 * (MAX(maxsub, npts) + 1) + npts + 2;
  npts = 1;

  /* 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("points   -             given break points = %11.2e\n", points[0]);
  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_brkpts (d01rlc).
   */
  nag_quad_dim1_fin_brkpts(f, a, b, npts, points, epsabs, epsrel, maxsub,
                           &result, &abserr, rinfo, iinfo, &comm, &fail);
  if (fail.code != NE_NOERROR)
    printf("Error or warning from nag_quad_dim1_fin_brkpts (d01rlc) %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] = ABS(x[i] - 1.0 / 7.0);
    if (fv[i] == 0.0) {
      /* A singular point will be hit. */
      /* Record offending abscissae and abort computation. */
      *iflag = *iflag + 1;
      comm->user[*iflag] = x[i];
    }
  }
  if (*iflag != 0) {
    /* store value of iflag in IUSER */
    comm->iuser[0] = *iflag;
    /* signal abort by setting iflag<0 */
    *iflag = -*iflag;
  } else {
    /* Safe to evaluate */
    for (i = 0; i < nx; i++)
      fv[i] = 1.0 / sqrt(fv[i]);
  }
}