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

NAG CL Interface Introduction
Example description
/* nag_quad_dim1_quad_inf_wt_trig_1 (d01ssc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.0, 2024.
 *
 */

#include <math.h>
#include <nag.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif
static double NAG_CALL g(double x, Nag_User *comm);
#ifdef __cplusplus
}
#endif

int main(void) {
  static Integer use_comm[1] = {1};
  Integer exit_status = 0;
  double a;
  double omega;
  double epsabs, abserr;
  Nag_TrigTransform wt_func;
  double result;
  Nag_QuadSubProgress qpsub;
  Integer maxintervals, maxsubint_per_int;
  NagError fail;
  Nag_User comm;

  INIT_FAIL(fail);

  printf("nag_quad_dim1_quad_inf_wt_trig_1 (d01ssc) Example Program Results\n");

  /* For communication with user-supplied functions: */
  comm.p = (Pointer)&use_comm;

  epsabs = 0.001;
  a = 0.0;
  /* nag_math_pi (x01aac).
   * pi
   */
  omega = nag_math_pi * 0.5;
  wt_func = Nag_Cosine;
  maxintervals = 50;
  maxsubint_per_int = 500;

  /* nag_quad_dim1_quad_inf_wt_trig_1 (d01ssc).
   * One-dimensional adaptive quadrature, semi-infinite
   * interval, sine or cosine weight function, thread-safe
   */
  nag_quad_dim1_quad_inf_wt_trig_1(g, a, omega, wt_func, maxintervals,
                                   maxsubint_per_int, epsabs, &result, &abserr,
                                   &qpsub, &comm, &fail);
  printf("a      - lower limit of integration = %10.4f\n", a);
  printf("b      - upper limit of integration = infinity\n");
  printf("epsabs - absolute accuracy requested = %11.2e\n\n", epsabs);
  if (fail.code != NE_NOERROR)
    printf("Error from nag_quad_dim1_quad_inf_wt_trig_1 (d01ssc) %s\n",
           fail.message);
  if (fail.code != NE_INT_ARG_LT && fail.code != NE_BAD_PARAM &&
      fail.code != NE_ALLOC_FAIL && fail.code != NE_NO_LICENCE) {
    printf("result - approximation to the integral = %9.5f\n", result);
    printf("abserr - estimate of the absolute error = %11.2e\n", abserr);
    printf("qpsub.fun_count  - number of function evaluations ="
           " %4" NAG_IFMT "\n",
           qpsub.fun_count);
    printf("qpsub.intervals  - number of intervals used = %4" NAG_IFMT "\n",
           qpsub.intervals);
    printf("qpsub.subints_per_interval  - \n"
           "maximum number of subintervals used in any one interval ="
           " %4" NAG_IFMT "\n",
           qpsub.subints_per_interval);
    /* Free memory used by qpsub */
    NAG_FREE(qpsub.interval_error);
    NAG_FREE(qpsub.interval_result);
    NAG_FREE(qpsub.interval_flag);
  } else {
    exit_status = 1;
    goto END;
  }

END:
  return exit_status;
}

static double NAG_CALL g(double x, Nag_User *comm) {
  Integer *use_comm = (Integer *)comm->p;

  if (use_comm[0]) {
    printf("(User-supplied callback g, first invocation.)\n");
    use_comm[0] = 0;
  }

  return (x > 0.0) ? 1.0 / sqrt(x) : 0.0;
}