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

NAG CL Interface Introduction
Example description
/* nag_quad_dim1_inf_general (d01rmc) 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 bound, result, epsabs, epsrel, abserr, pi;
  Integer inf, 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_inf_general (d01rmc) Example Program Results\n\n");
  pi = nag_math_pi;
  epsabs = 0.0;
  epsrel = 1.0e-04;
  bound = 0.0;
  inf = 1;
  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", bound);
  printf("b        -     upper limit of integration =    infinity\n");
  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_inf_general (d01rmc).
   */
  nag_quad_dim1_inf_general(f, bound, inf, epsabs, epsrel, maxsub, &result,
                            &abserr, rinfo, iinfo, &comm, &fail);
  if (fail.code != NE_NOERROR)
    printf("Error or warning from nag_quad_dim1_inf_general (d01rmc) %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] = 1.0 / ((x[i] + 1.0) * sqrt(x[i]));
}