## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

d01jaf attempts to evaluate an integral over an $n$-dimensional sphere ($n=2$, $3$, or $4$), to a user-specified absolute or relative accuracy, by means of a modified Sag–Szekeres method. The routine can handle singularities on the surface or at the centre of the sphere, and returns an error estimate.

## 2Specification

Fortran Interface
 Subroutine d01jaf ( f, ndim, epsa, epsr,
 Integer, Intent (In) :: ndim, method, icoord Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: nevals Real (Kind=nag_wp), External :: f Real (Kind=nag_wp), Intent (In) :: radius, epsa, epsr Real (Kind=nag_wp), Intent (Out) :: result, esterr
#include <nag.h>
 void d01jaf_ (double (NAG_CALL *f)(const Integer *ndim, const double x[]),const Integer *ndim, const double *radius, const double *epsa, const double *epsr, const Integer *method, const Integer *icoord, double *result, double *esterr, Integer *nevals, Integer *ifail)

## 3Description

d01jaf calculates an approximation to the $n$-dimensional integral
 $I=∫⋯∫SF(x1,…,xn)dx1⋯dxn, 2≤n≤4,$
where $S$ is the hypersphere
 $(x12+⋯+xn2)≤α<∞$
(the integrand function may also be defined in spherical coordinates). The algorithm is based on the Sag–Szekeres method (see Sag and Szekeres (1964)), applying the product trapezoidal formula after a suitable radial transformation. An improved transformation technique is developed: depending on the behaviour of the function and on the required accuracy, different transformations can be used, some of which are ‘double exponential’, as defined by Takahasi and Mori (1974). The resulting technique allows the routine to deal with integrand singularities on the surface or at the centre of the sphere. When the estimated error of the approximation with mesh size $h$ is larger than the tolerated error, the trapezoidal formula with mesh size $h/2$ is calculated. A drawback of this method is the exponential growth of the number of function evaluations in the successive approximations (this number grows with a factor $\text{}\approx {2}^{n}$). This introduces the restriction $n\le 4$. Because the convergence rate of the successive approximations is normally better than linear, the error estimate is based on the linear extrapolation of the difference between the successive approximations (see Robinson and de Doncker (1981) and Roose and de Doncker (1981)). For further details of the algorithm, see Roose and de Doncker (1981).

## 4References

Robinson I and de Doncker E (1981) Automatic computation of improper integrals over a bounded or unbounded planar region Computing 27 89–284
Roose D and de Doncker E (1981) Automatic integration over a sphere J. Comput. Appl. Math. 7 203–224
Sag T W and Szekeres G (1964) Numerical evaluation of high-dimensional integrals Math. Comput. 18 245–253
Takahasi H and Mori M (1974) Double Exponential Formulas for Numerical Integration 9 Publ. RIMS, Kyoto University 721–741

## 5Arguments

1: $\mathbf{f}$real (Kind=nag_wp) Function, supplied by the user. External Procedure
f must return the value of the integrand $f$ at a given point.
The specification of f is:
Fortran Interface
 Function f ( ndim, x)
 Real (Kind=nag_wp) :: f Integer, Intent (In) :: ndim Real (Kind=nag_wp), Intent (In) :: x(ndim)
 double f (const Integer *ndim, const double x[])
1: $\mathbf{ndim}$Integer Input
On entry: $n$, the number of dimensions of the integral.
2: $\mathbf{x}\left({\mathbf{ndim}}\right)$Real (Kind=nag_wp) array Input
On entry: the coordinates of the point at which the integrand $f$ must be evaluated. These coordinates are given in Cartesian or spherical polar form according to the value of icoord.
f must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which d01jaf is called. Arguments denoted as Input must not be changed by this procedure.
Note: f should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by d01jaf. If your code inadvertently does return any NaNs or infinities, d01jaf is likely to produce unexpected results.
2: $\mathbf{ndim}$Integer Input
On entry: $n$, the dimension of the sphere.
Constraint: $2\le {\mathbf{ndim}}\le 4$.
3: $\mathbf{radius}$Real (Kind=nag_wp) Input
On entry: $\alpha$, the radius of the sphere.
Constraint: ${\mathbf{radius}}\ge 0.0$.
4: $\mathbf{epsa}$Real (Kind=nag_wp) Input
On entry: the requested absolute tolerance. If ${\mathbf{epsa}}<0.0$, its absolute value is used. See Section 7.
5: $\mathbf{epsr}$Real (Kind=nag_wp) Input
On entry: the requested relative tolerance.
${\mathbf{epsr}}<0.0$
Its absolute value is used.
The latter value is used as epsr by the routine. See Section 7.
6: $\mathbf{method}$Integer Input
On entry: must specify the transformation to be used by the routine. The choice depends on the behaviour of the integrand and on the required accuracy.
For well-behaved functions and functions with mild singularities on the surface of the sphere only:
${\mathbf{method}}=1$
Low accuracy required.
${\mathbf{method}}=2$
High accuracy required.
For functions with severe singularities on the surface of the sphere only:
${\mathbf{method}}=3$
Low accuracy required.
${\mathbf{method}}=4$
High accuracy required.
(in this case icoord must be set to ${\mathbf{icoord}}=2$, and the function defined in special spherical coordinates).
For functions with a singularity at the centre of the sphere (and possibly with singularities on the surface as well):
${\mathbf{method}}=5$
Low accuracy required.
${\mathbf{method}}=6$
High accuracy required.
${\mathbf{method}}=0$ can be used as a default value and is equivalent to:
• ${\mathbf{method}}=1$ if ${\mathbf{epsr}}>{10}^{-6}$, and
• ${\mathbf{method}}=2$ if ${\mathbf{epsr}}\le {10}^{-6}$.
The distinction between low and high required accuracies, as mentioned above, depends also on the behaviour of the function. Roughly one may assume the critical value of epsa and epsr to be ${10}^{-6}$, but the critical value will be smaller for a well-behaved integrand and larger for an integrand with severe singularities.
Suggested value: ${\mathbf{method}}=0$.
Constraint: ${\mathbf{method}}=0$, $1$, $2$, $3$, $4$, $5$ or $6$.
If ${\mathbf{icoord}}=2$, ${\mathbf{method}}=3$ or $4$
7: $\mathbf{icoord}$Integer Input
On entry: must specify which kind of coordinates are used in f.
${\mathbf{icoord}}=0$
Cartesian coordinates ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
${\mathbf{icoord}}=1$
Spherical coordinates (see Section 9.2): ${\mathbf{x}}\left(1\right)=\rho$; ${\mathbf{x}}\left(\mathit{i}\right)={\theta }_{\mathit{i}-1}$, for $\mathit{i}=2,3,\dots ,n$.
${\mathbf{icoord}}=2$,
Special spherical polar coordinates (see Section 9.3), with the additional transformation $\rho =\alpha -\lambda$: ${\mathbf{x}}\left(1\right)=\lambda =\alpha -\rho$; ${\mathbf{x}}\left(\mathit{i}\right)={\theta }_{\mathit{i}-1}$, for $\mathit{i}=2,3,\dots ,n$.
Constraint: ${\mathbf{icoord}}=0$, $1$ or $2$.
If ${\mathbf{method}}=3$ or $4$, ${\mathbf{icoord}}=2$
8: $\mathbf{result}$Real (Kind=nag_wp) Output
On exit: the approximation to the integral $I$.
9: $\mathbf{esterr}$Real (Kind=nag_wp) Output
On exit: an estimate of the modulus of the absolute error.
10: $\mathbf{nevals}$Integer Output
On exit: the number of function evaluations used.
11: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $-1$ is recommended since useful values can be provided in some output arguments even when ${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit. When the value $-\mathbf{1}$ or $\mathbf{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).
Errors or warnings detected by the routine:
Note: in some cases d01jaf may return useful information.
${\mathbf{ifail}}=1$
The required accuracy could not be achieved.
${\mathbf{ifail}}=2$
The required accuracy could not be achieved.
${\mathbf{ifail}}=3$
The required accuracy could not be achieved.
If ${\mathbf{method}}=0$, $1$ or $2$, setting ${\mathbf{method}}=3$ or $4$ may help.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{icoord}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{icoord}}\le 2$.
On entry, ${\mathbf{icoord}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{icoord}}\ge 0$.
On entry, ${\mathbf{icoord}}=2$ and ${\mathbf{method}}=⟨\mathit{\text{value}}⟩$.
Constraint: when ${\mathbf{icoord}}=2$, ${\mathbf{method}}=3$ or $4$.
On entry, ${\mathbf{method}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{method}}\le 6$.
On entry, ${\mathbf{method}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{method}}\ge 0$.
On entry, ${\mathbf{method}}=3$ and ${\mathbf{icoord}}=⟨\mathit{\text{value}}⟩$.
Constraint: when ${\mathbf{method}}=3$, ${\mathbf{icoord}}=2$.
On entry, ${\mathbf{method}}=4$ and ${\mathbf{icoord}}=⟨\mathit{\text{value}}⟩$.
Constraint: when ${\mathbf{method}}=4$, ${\mathbf{icoord}}=2$.
On entry, ${\mathbf{ndim}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ndim}}\le 4$.
On entry, ${\mathbf{ndim}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ndim}}\ge 2$.
On entry, ${\mathbf{radius}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{radius}}\ge 0.0$.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

You can specify an absolute and/or a relative tolerance, setting epsa and epsr. The routine attempts to calculate an approximation result such that
 $|I-result|≤max{epsa,epsr×|I|}.$
If $0\le {\mathbf{ifail}}\le 3$, esterr returns an estimate of, but not necessarily a bound for, $|I-{\mathbf{result}}|$.

## 8Parallelism and Performance

d01jaf is not threaded in any implementation.

### 9.1Timing

Timing depends on the integrand and the accuracy required.

### 9.2Spherical Polar Coordinates

Cartesian coordinates are related to the spherical polar coordinates by:
 $x1 = ρ.sin⁡θ1⋯sin⁡θn-2.sin⁡θn-1 x2 = ρ.sin⁡θ1⋯sin⁡θn-2.cos⁡θn-1 x3 = ρ.sin⁡θ1⋯cos⁡θn-2 ⋮ xn = ρ.cos⁡θ1$
where $0<{\theta }_{\mathit{i}}<\pi$, for $\mathit{i}=1,2,\dots ,n-2$ and $0<{\theta }_{n-1}<2\pi$.

### 9.3Machine Dependencies

As a consequence of the transformation technique, the severity of the singularities which can be handled by d01jaf depends on the precision and range of real numbers on the machine. ${\mathbf{method}}=3$ or $4$ must be used when the singularity on the surface is ‘severe’ in view of the requested accuracy and machine precision. In practice one has to set ${\mathbf{method}}=3$ or $4$ if d01jaf terminates with ${\mathbf{ifail}}={\mathbf{3}}$ when called with ${\mathbf{method}}=0$, $1$ or $2$.
When integrating a function with a severe singular behaviour on the surface of the sphere, the additional transformation $\rho =\alpha -\lambda$ helps to avoid the loss of significant figures due to round-off error in the calculation of the integration nodes which are very close to the surface. For these points, the value of $\lambda$ can be computed more accurately than the value of $\rho$. Naturally, care must be taken that the function subprogram does not contain expressions of the form $\alpha -\lambda$, which could cause a large round-off error in the calculation of the integrand at the boundary of the sphere.
Care should be taken to avoid underflow and/or overflow problems in the function subprogram, because some of the integration nodes used by d01jaf may be very close to the surface or to the centre of the sphere.
Example:
• suppose the function
 $f(ρ)=(1-ρ2)-0.7$
is to be integrated over the unit sphere, with ${\mathbf{method}}=3$ or $4$. Then icoord should be set to 2; the transformation $\rho =1-\lambda$ gives $f\left(\rho \right)={\left(2\lambda -{\lambda }^{2}\right)}^{-0.7}$; and f could be coded thus:
```f = 1.0
a = x(1)
If (a.gt.0.0) f = 1.0/(a*(2.0-a))**0.7
Return```
Note that d01jaf ensures that $\lambda ={\mathbf{x}}\left(1\right)>{\mathbf{x02amf}}$, but underflow could occur in the computation of ${\lambda }^{2}$.

## 10Example

This example evaluates the integrals
 $∫⋯∫S11-ρ2dx1⋯dxn$
where $\rho =\sqrt{\sum _{i=1}^{n}{x}_{i}^{2}}$, and $S$ is the unit sphere of dimension $n=2$ or $4$.
The exact values (to $12$ decimal places) are $6.28318530718$ and $13.1594725348$.

### 10.1Program Text

Program Text (d01jafe.f90)

None.

### 10.3Program Results

Program Results (d01jafe.r)