d01ar computes definite and indefinite integrals over a finite range to a specified relative or absolute accuracy, using the method described in Patterson (1968).

# Syntax

C#
```public static void d01ar(
double a,
double b,
D01..::..D01AR_FUN fun,
double relacc,
double absacc,
int maxrul,
int iparm,
out double acc,
out double ans,
out int n,
double[] alpha,
out int ifail
)```
Visual Basic
```Public Shared Sub d01ar ( _
a As Double, _
b As Double, _
fun As D01..::..D01AR_FUN, _
relacc As Double, _
absacc As Double, _
maxrul As Integer, _
iparm As Integer, _
<OutAttribute> ByRef acc As Double, _
<OutAttribute> ByRef ans As Double, _
<OutAttribute> ByRef n As Integer, _
alpha As Double(), _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void d01ar(
double a,
double b,
D01..::..D01AR_FUN^ fun,
double relacc,
double absacc,
int maxrul,
int iparm,
[OutAttribute] double% acc,
[OutAttribute] double% ans,
[OutAttribute] int% n,
array<double>^ alpha,
[OutAttribute] int% ifail
)```
F#
```static member d01ar :
a : float *
b : float *
fun : D01..::..D01AR_FUN *
relacc : float *
absacc : float *
maxrul : int *
iparm : int *
acc : float byref *
ans : float byref *
n : int byref *
alpha : float[] *
ifail : int byref -> unit
```

#### Parameters

a
Type: System..::..Double
On entry: $a$, the lower limit of integration.
b
Type: System..::..Double
On entry: $b$, the upper limit of integration. It is not necessary that $a.
fun
Type: NagLibrary..::..D01..::..D01AR_FUN
fun must return the value of the integrand $f$ at a specified point.

A delegate of type D01AR_FUN.

If ${\mathbf{iparm}}=2$, fun is not called.
relacc
Type: System..::..Double
On entry: the relative accuracy required. If convergence according to absolute accuracy is required, relacc should be set to zero (but see also [Accuracy]). If ${\mathbf{relacc}}<0.0$, its absolute value is used.
If ${\mathbf{iparm}}=2$, relacc is not used.
absacc
Type: System..::..Double
On entry: the absolute accuracy required. If convergence according to relative accuracy is required, absacc should be set to zero (but see also [Accuracy]). If ${\mathbf{absacc}}<0.0$, its absolute value is used.
If ${\mathbf{iparm}}=2$, absacc is not used.
maxrul
Type: System..::..Int32
On entry: the maximum number of successive rules that may be used.
Constraint: $1\le {\mathbf{maxrul}}\le 9$. If maxrul is outside these limits, the value $9$ is assumed.
If ${\mathbf{iparm}}=2$, maxrul is not used.
iparm
Type: System..::..Int32
On entry: indicates the task to be performed by the method.
${\mathbf{iparm}}=0$
Only the definite integral over $\left[a,b\right]$ is evaluated.
${\mathbf{iparm}}=1$
As well as the definite integral, the expansion of the integrand in Legendre polynomials over $\left[a,b\right]$ is calculated, using the same values of the integrand as used to compute the integral. The expansion coefficients, and some other quantities, are returned in alpha for later use in computing indefinite integrals.
${\mathbf{iparm}}=2$
$f\left(t\right)$ is integrated analytically over $\left[a,b\right]$ using the previously computed expansion, stored in alpha. No further evaluations of the integrand are required. The method must previously have been called with ${\mathbf{iparm}}=1$ and the interval $\left[a,b\right]$ must lie within that specified for the previous call. In this case only the arguments a, b, iparm, ans, alpha and ifail are used.
Constraint: ${\mathbf{iparm}}=0$, $1$ or $2$.
acc
Type: System..::..Double%
On exit: if ${\mathbf{iparm}}=0$ or $1$, acc contains the absolute value of the difference between the last two successive estimates of the integral. This may be used as a measure of the accuracy actually achieved.
If ${\mathbf{iparm}}=2$, acc is not used.
ans
Type: System..::..Double%
On exit: the estimated value of the integral.
n
Type: System..::..Int32%
On exit: when ${\mathbf{iparm}}=0$ or $1$, n contains the number of integrand evaluations used in the calculation of the integral.
If ${\mathbf{iparm}}=2$, n is not used.
alpha
Type: array<System..::..Double>[]()[][]
An array of size [$390$]
On entry: if ${\mathbf{iparm}}=2$, alpha must contain the coefficients of the Legendre expansions of the integrand, as returned by a previous call of d01ar with ${\mathbf{iparm}}=1$ and a range containing the present range.
If ${\mathbf{iparm}}=0$ or $1$, alpha need not be set on entry.
On exit: if ${\mathbf{iparm}}=1$, the first $m$ elements of alpha hold the coefficients of the Legendre expansion of the integrand, and the value of $m$ is stored in ${\mathbf{alpha}}\left[389\right]$. alpha must not be changed between a call with ${\mathbf{iparm}}=1$ and subsequent calls with ${\mathbf{iparm}}=2$.
If ${\mathbf{iparm}}=2$, the first $m$ elements of alpha are unchanged on exit.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# Description

d01ar evaluates definite and indefinite integrals of the form:
 $∫abftdt$
using the method described in Patterson (1968).

# Definite Integrals

In this case d01ar must be called with ${\mathbf{iparm}}=0$. By linear transformation the integral is changed to
 $I=∫-1+1Fxdx$
where
 $Fx=b-a2fb+a+b-ax2$
and is then approximated by an $n$-point quadrature rule
 $I=∑k=1nwkFxk$
where ${w}_{k}$ are the weights and ${x}_{k}$ are the abscissae.
The method uses a family of nine interlacing rules based on the optimal extension of the three-point Gauss rule. These rules use $1$, $3$, $7$, $15$, $31$, $63$, $127$, $255$ and $511$ points and have respective polynomial integrating degrees $1$, $5$, $11$, $23$, $47$, $95$, $191$, $383$ and $767$. Each rule has the property that the next in sequence includes all the points of its predecessor and has the greatest possible increase in integrating degree.
The integration method is based on the successive application of these rules until the absolute value of the difference of two successive results differs by not more than absacc, or relatively by not more than relacc. The result of the last rule used is taken as the value of the integral (ans), and the absolute difference of the results of the last two rules used is taken as an estimate of the absolute error (acc). Due to their interlacing form no integrand evaluations are wasted in passing from one rule to the next.

# Indefinite Integrals

Suppose the value of the integral
 $∫cdftdt$
is required for a number of sub-intervals $\left[c,d\right]$, all of which lie in an interval $\left[a,b\right]$.
In this case d01ar should first be called with the parameter ${\mathbf{iparm}}=1$ and the interval set to $\left[a,b\right]$. The method then calculates the integral over $\left[a,b\right]$ and the Legendre expansion of the integrand, using the same integrand values. If the method is subsequently called with ${\mathbf{iparm}}=2$ and the interval set to $\left[c,d\right]$, the integral over $\left[c,d\right]$ is calculated by analytical integration of the Legendre expansion, without further evaluations of the integrand.
For the interval $\left[-1,1\right]$ the expansion takes the form
 $Fx=∑i=0∞αiPix$
where ${P}_{i}\left(x\right)$ is the order $i$ Legendre polynomial. Assuming that the integral over the full range $\left[-1,1\right]$ was evaluated to the required accuracy using an $n$-point rule, then the coefficients
 $αi=122i-1∫-1+1PixFxdx, i=0,1,…,m$
are evaluated by that same rule, up to
 $m=3n-1/4.$
The accuracy for indefinite integration should be of the same order as that obtained for the definite integral over the full range. The indefinite integrals will be exact when $F\left(x\right)$ is a polynomial of degree $\text{}\le m$.

# References

Patterson T N L (1968) The Optimum addition of points to quadrature formulae Math. Comput. 22 847–856

# Error Indicators and Warnings

Note: d01ar may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the method:
${\mathbf{ifail}}=1$
If ${\mathbf{iparm}}=0$ or $1$, this indicates that all maxrul rules have been used and the integral has not converged to the accuracy requested. In this case ans contains the last approximation to the integral, and acc contains the difference between the last two approximations. To check this estimate of the integral, d01ar could be called again to evaluate
 $∫abftdt as ∫acftdt+∫cbftdt for some ​a
If ${\mathbf{iparm}}=2$, this indicates failure of convergence during the run with ${\mathbf{iparm}}=1$ in which the Legendre expansion was created.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{iparm}}\ne 0$, $1$ or $2$
${\mathbf{ifail}}=3$
The method is called with ${\mathbf{iparm}}=2$ but a previous call with ${\mathbf{iparm}}=1$ has been omitted or was invoked with an integration interval of length zero.
${\mathbf{ifail}}=4$
On entry, with ${\mathbf{iparm}}=2$, the interval for indefinite integration is not contained within the interval specified when d01ar was previously called with ${\mathbf{iparm}}=1$.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

The relative or absolute accuracy required is specified by you in the variables relacc or absacc. d01ar will terminate whenever either the relative accuracy specified by relacc or the absolute accuracy specified by absacc is reached. One or other of these criteria may be ‘forced’ by setting the parameter for the other to zero. If both relacc and absacc are specified as zero, then the method uses the value  for relacc.
If on exit ${\mathbf{ifail}}={0}$, then it is likely that the result is correct to one or other of these accuracies. If on exit ${\mathbf{ifail}}={1}$, then it is likely that neither of the requested accuracies has been reached.
When you have no prior idea of the magnitude of the integral, it is possible that an unreasonable accuracy may be requested, e.g., a relative accuracy for an integral which turns out to be zero, or a small absolute accuracy for an integral which turns out to be very large. Even if failure is reported in such a case, the value of the integral may still be satisfactory. The device of setting the other ‘unused’ accuracy parameter to a small positive value (e.g., ${10}^{-9}$ for an implementation of $11$-digit precision) rather than zero, may prevent excessive calculation in such a situation.
To avoid spurious convergence, it is recommended that relative accuracies larger than about ${10}^{-3}$ be avoided.

# Parallelism and Performance

None.

The time taken by d01ar depends on the complexity of the integrand and the accuracy required.
This method uses the Patterson method over the whole integration interval and should therefore be suitable for well behaved functions. However, for very irregular functions it would be more efficient to submit the differently behaved regions separately for integration.

# Example

This example evaluates the following integrals
(i) Definite integral only $\left({\mathbf{iparm}}=0\right)$ for
 $∫0141+x2dx absacc=10-5.$
(ii) Definite integral together with expansion coefficients $\left({\mathbf{iparm}}=1\right)$ for
 $∫12x8dx absacc=10-5.$
(iii) Indefinite integral using previous expansion $\left({\mathbf{iparm}}=2\right)$ for
 $∫1.21.8x8dx absacc=10-5.$

Example program (C#): d01are.cs

Example program results: d01are.r