/* nag_quad_1d_fin_smooth (d01bdc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

#include <stdio.h>
#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagd01.h>
#include <nagx01.h>

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

int main(void)
{
  static double ruser[1] = { -1.0 };
  Integer exit_status = 0;
  double a, abserr, b, epsabs, epsrel, result;
  Nag_Comm comm;

  printf("nag_quad_1d_fin_smooth (d01bdc) Example Program Results\n");

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

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  /* Input arguments */
  scanf("%lf %lf", &epsabs, &epsrel);
  scanf("%lf %lf", &a, &b);

  /* nag_quad_1d_fin_smooth (d01bdc).
   * One-dimensional quadrature, non-adaptive, finite interval.
   */
  nag_quad_1d_fin_smooth(f, a, b, epsabs, epsrel, &result, &abserr, &comm);

  printf("\na      - lower limit of integration = %10.4f"
         "\nb      - upper limit of integration = %10.4f"
         "\nepsabs - absolute accuracy requested = %9.2e"
         "\nepsrel - relative accuracy requested = %9.2e\n"
         "\nresult - approximation to the integral = %9.5f"
         "\nabserr - estimate to the absolute error = %9.2e\n\n",
         a, b, epsabs, epsrel, result, abserr);
  if (abserr > MAX(epsabs, epsrel * fabs(result)))
    printf("Warning - requested accuracy may not have been achieved.\n");

  return exit_status;
}

static double NAG_CALL f(double x, Nag_Comm *comm)
{
  if (comm->user[0] == -1.0) {
    printf("(User-supplied callback f, first invocation.)\n");
    comm->user[0] = 0.0;
  }
  return (pow(x, 2)) * sin(10.0 * nag_pi * x);
}