# NAG Library Routine Document

## 1Purpose

d01apf is an adaptive integrator which calculates an approximation to the integral of a function $g\left(x\right)w\left(x\right)$ over a finite interval $\left[a,b\right]$:
 $I= ∫ab gx wx dx$
where the weight function $w$ has end point singularities of algebraico-logarithmic type.

## 2Specification

Fortran Interface
 Subroutine d01apf ( g, a, b, alfa, beta, key, w, lw, iw, liw,
 Integer, Intent (In) :: key, lw, liw Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: iw(liw) Real (Kind=nag_wp), External :: g Real (Kind=nag_wp), Intent (In) :: a, b, alfa, beta, epsabs, epsrel Real (Kind=nag_wp), Intent (Out) :: result, abserr, w(lw)
#include nagmk26.h
 void d01apf_ (double (NAG_CALL *g)(const double *x),const double *a, const double *b, const double *alfa, const double *beta, const Integer *key, const double *epsabs, const double *epsrel, double *result, double *abserr, double w[], const Integer *lw, Integer iw[], const Integer *liw, Integer *ifail)

## 3Description

d01apf is based on the QUADPACK routine QAWSE (see Piessens et al. (1983)) and integrates a function of the form $g\left(x\right)w\left(x\right)$, where the weight function $w\left(x\right)$ may have algebraico-logarithmic singularities at the end points $a$ and/or $b$. The strategy is a modification of that in d01akf. We start by bisecting the original interval and applying modified Clenshaw–Curtis integration of orders $12$ and $24$ to both halves. Clenshaw–Curtis integration is then used on all sub-intervals which have $a$ or $b$ as one of their end points (see Piessens et al. (1974)). On the other sub-intervals Gauss–Kronrod ($7$$15$ point) integration is carried out.
A ‘global’ acceptance criterion (as defined by Malcolm and Simpson (1976)) is used. The local error estimation control is described in Piessens et al. (1983).

## 4References

Malcolm M A and Simpson R B (1976) Local versus global strategies for adaptive quadrature ACM Trans. Math. Software 1 129–146
Piessens R, de Doncker–Kapenga E, Überhuber C and Kahaner D (1983) QUADPACK, A Subroutine Package for Automatic Integration Springer–Verlag
Piessens R, Mertens I and Branders M (1974) Integration of functions having end-point singularities Angew. Inf. 16 65–68

## 5Arguments

1:     $\mathbf{g}$ – real (Kind=nag_wp) Function, supplied by the user.External Procedure
g must return the value of the function $g$ at a given point x.
The specification of g is:
Fortran Interface
 Function g ( x)
 Real (Kind=nag_wp) :: g Real (Kind=nag_wp), Intent (In) :: x
#include nagmk26.h
 double g (const double *x)
1:     $\mathbf{x}$ – Real (Kind=nag_wp)Input
On entry: the point at which the function $g$ must be evaluated.
g must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which d01apf is called. Arguments denoted as Input must not be changed by this procedure.
Note: g should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by d01apf. If your code inadvertently does return any NaNs or infinities, d01apf is likely to produce unexpected results.
2:     $\mathbf{a}$ – Real (Kind=nag_wp)Input
On entry: $a$, the lower limit of integration.
3:     $\mathbf{b}$ – Real (Kind=nag_wp)Input
On entry: $b$, the upper limit of integration.
Constraint: ${\mathbf{b}}>{\mathbf{a}}$.
4:     $\mathbf{alfa}$ – Real (Kind=nag_wp)Input
On entry: the argument $\alpha$ in the weight function.
Constraint: ${\mathbf{alfa}}>-1.0$.
5:     $\mathbf{beta}$ – Real (Kind=nag_wp)Input
On entry: the argument $\beta$ in the weight function.
Constraint: ${\mathbf{beta}}>-1.0$.
6:     $\mathbf{key}$ – IntegerInput
On entry: indicates which weight function is to be used.
${\mathbf{key}}=1$
$w\left(x\right)={\left(x-a\right)}^{\alpha }{\left(b-x\right)}^{\beta }$.
${\mathbf{key}}=2$
$w\left(x\right)={\left(x-a\right)}^{\alpha }{\left(b-x\right)}^{\beta }\mathrm{ln}\left(x-a\right)$.
${\mathbf{key}}=3$
$w\left(x\right)={\left(x-a\right)}^{\alpha }{\left(b-x\right)}^{\beta }\mathrm{ln}\left(b-x\right)$.
${\mathbf{key}}=4$
$w\left(x\right)={\left(x-a\right)}^{\alpha }{\left(b-x\right)}^{\beta }\mathrm{ln}\left(x-a\right)\mathrm{ln}\left(b-x\right)$.
Constraint: ${\mathbf{key}}=1$, $2$, $3$ or $4$.
7:     $\mathbf{epsabs}$ – Real (Kind=nag_wp)Input
On entry: the absolute accuracy required. If epsabs is negative, the absolute value is used. See Section 7.
8:     $\mathbf{epsrel}$ – Real (Kind=nag_wp)Input
On entry: the relative accuracy required. If epsrel is negative, the absolute value is used. See Section 7.
9:     $\mathbf{result}$ – Real (Kind=nag_wp)Output
On exit: the approximation to the integral $I$.
10:   $\mathbf{abserr}$ – Real (Kind=nag_wp)Output
On exit: an estimate of the modulus of the absolute error, which should be an upper bound for $\left|I-{\mathbf{result}}\right|$.
11:   $\mathbf{w}\left({\mathbf{lw}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: details of the computation see Section 9 for more information.
12:   $\mathbf{lw}$ – IntegerInput
On entry: the dimension of the array w as declared in the (sub)program from which d01apf is called. The value of lw (together with that of liw) imposes a bound on the number of sub-intervals into which the interval of integration may be divided by the routine. The number of sub-intervals cannot exceed ${\mathbf{lw}}/4$. The more difficult the integrand, the larger lw should be.
Suggested value: ${\mathbf{lw}}=800$ to $2000$ is adequate for most problems.
Constraint: ${\mathbf{lw}}\ge 8$.
13:   $\mathbf{iw}\left({\mathbf{liw}}\right)$ – Integer arrayOutput
On exit: ${\mathbf{iw}}\left(1\right)$ contains the actual number of sub-intervals used. The rest of the array is used as workspace.
14:   $\mathbf{liw}$ – IntegerInput
On entry: the dimension of the array iw as declared in the (sub)program from which d01apf is called. The number of sub-intervals into which the interval of integration may be divided cannot exceed liw.
Suggested value: ${\mathbf{liw}}={\mathbf{lw}}/4$.
Constraint: ${\mathbf{liw}}\ge 2$.
15:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, because for this routine the values of the output arguments may be useful even if ${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit, the recommended value is $-1$. When the value $-\mathbf{1}\text{​ or ​}1$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Note: d01apf may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
The maximum number of subdivisions allowed with the given workspace has been reached without the accuracy requirements being achieved. Look at the integrand in order to determine the integration difficulties. If the position of a discontinuity or a singularity of algebraico-logarithmic type within the interval can be determined, the interval must be split up at this point and the integrator called on the subranges. If necessary, another integrator, which is designed for handling the type of difficulty involved, must be used. Alternatively, consider relaxing the accuracy requirements specified by epsabs and epsrel, or increasing the amount of workspace.
${\mathbf{ifail}}=2$
Round-off error prevents the requested tolerance from being achieved. Consider requesting less accuracy.
${\mathbf{ifail}}=3$
Extremely bad local integrand behaviour causes a very strong subdivision around one (or more) points of the interval. The same advice applies as in the case of ${\mathbf{ifail}}={\mathbf{1}}$.
${\mathbf{ifail}}=4$
 On entry, ${\mathbf{b}}\le {\mathbf{a}}$, or ${\mathbf{alfa}}\le -1.0$, or ${\mathbf{beta}}\le -1.0$, or ${\mathbf{key}}\ne 1$, $2$, $3$ or $4$.
${\mathbf{ifail}}=5$
 On entry, ${\mathbf{lw}}<8$, or ${\mathbf{liw}}<2$.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

d01apf cannot guarantee, but in practice usually achieves, the following accuracy:
 $I-result≤tol,$
where
 $tol=maxepsabs,epsrel×I ,$
and epsabs and epsrel are user-specified absolute and relative error tolerances. Moreover, it returns the quantity abserr which, in normal circumstances, satisfies
 $I-result≤abserr≤tol.$

## 8Parallelism and Performance

d01apf is not threaded in any implementation.

The time taken by d01apf depends on the integrand and the accuracy required.
If ${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit, then you may wish to examine the contents of the array w, which contains the end points of the sub-intervals used by d01apf along with the integral contributions and error estimates over these sub-intervals.
Specifically, for $i=1,2,\dots ,n$, let ${r}_{i}$ denote the approximation to the value of the integral over the sub-interval $\left[{a}_{i},{b}_{i}\right]$ in the partition of $\left[a,b\right]$ and ${e}_{i}$ be the corresponding absolute error estimate. Then, $\underset{{a}_{i}}{\overset{{b}_{i}}{\int }}f\left(x\right)w\left(x\right)dx\simeq {r}_{i}$ and ${\mathbf{result}}=\sum _{i=1}^{n}{r}_{i}$. The value of $n$ is returned in ${\mathbf{iw}}\left(1\right)$, and the values ${a}_{i}$, ${b}_{i}$, ${e}_{i}$ and ${r}_{i}$ are stored consecutively in the array w, that is:
• ${a}_{i}={\mathbf{w}}\left(i\right)$,
• ${b}_{i}={\mathbf{w}}\left(n+i\right)$,
• ${e}_{i}={\mathbf{w}}\left(2n+i\right)$ and
• ${r}_{i}={\mathbf{w}}\left(3n+i\right)$.

## 10Example

This example computes
 $∫ 0 1 ln⁡x cos10πx dx and ∫01 sin10x x1-x dx .$

### 10.1Program Text

Program Text (d01apfe.f90)

None.

### 10.3Program Results

Program Results (d01apfe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017