# NAG Library Routine Document

## 1Purpose

s18dcf returns a sequence of values for the modified Bessel functions ${K}_{\nu +n}\left(z\right)$ for complex $z$, non-negative $\nu$ and $n=0,1,\dots ,N-1$, with an option for exponential scaling.

## 2Specification

Fortran Interface
 Subroutine s18dcf ( fnu, z, n, scal, cy, nz,
 Integer, Intent (In) :: n Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: nz Real (Kind=nag_wp), Intent (In) :: fnu Complex (Kind=nag_wp), Intent (In) :: z Complex (Kind=nag_wp), Intent (Out) :: cy(n) Character (1), Intent (In) :: scal
#include <nagmk26.h>
 void s18dcf_ (const double *fnu, const Complex *z, const Integer *n, const char *scal, Complex cy[], Integer *nz, Integer *ifail, const Charlen length_scal)

## 3Description

s18dcf evaluates a sequence of values for the modified Bessel function ${K}_{\nu }\left(z\right)$, where $z$ is complex, $-\pi <\mathrm{arg}z\le \pi$, and $\nu$ is the real, non-negative order. The $N$-member sequence is generated for orders $\nu$, $\nu +1,\dots ,\nu +N-1$. Optionally, the sequence is scaled by the factor ${e}^{z}$.
The routine is derived from the routine CBESK in Amos (1986).
Note:  although the routine may not be called with $\nu$ less than zero, for negative orders the formula ${K}_{-\nu }\left(z\right)={K}_{\nu }\left(z\right)$ may be used.
When $N$ is greater than $1$, extra values of ${K}_{\nu }\left(z\right)$ are computed using recurrence relations.
For very large $\left|z\right|$ or $\left(\nu +N-1\right)$, argument reduction will cause total loss of accuracy, and so no computation is performed. For slightly smaller $\left|z\right|$ or $\left(\nu +N-1\right)$, the computation is performed but results are accurate to less than half of machine precision. If $\left|z\right|$ is very small, near the machine underflow threshold, or $\left(\nu +N-1\right)$ is too large, there is a risk of overflow and so no computation is performed. In all the above cases, a warning is given by the routine.

## 4References

NIST Digital Library of Mathematical Functions
Amos D E (1986) Algorithm 644: A portable package for Bessel functions of a complex argument and non-negative order ACM Trans. Math. Software 12 265–273

## 5Arguments

1:     $\mathbf{fnu}$ – Real (Kind=nag_wp)Input
On entry: $\nu$, the order of the first member of the sequence of functions.
Constraint: ${\mathbf{fnu}}\ge 0.0$.
2:     $\mathbf{z}$ – Complex (Kind=nag_wp)Input
On entry: the argument $z$ of the functions.
Constraint: ${\mathbf{z}}\ne \left(0.0,0.0\right)$.
3:     $\mathbf{n}$ – IntegerInput
On entry: $N$, the number of members required in the sequence ${K}_{\nu }\left(z\right),{K}_{\nu +1}\left(z\right),\dots ,{K}_{\nu +N-1}\left(z\right)$.
Constraint: ${\mathbf{n}}\ge 1$.
4:     $\mathbf{scal}$ – Character(1)Input
On entry: the scaling option.
${\mathbf{scal}}=\text{'U'}$
The results are returned unscaled.
${\mathbf{scal}}=\text{'S'}$
The results are returned scaled by the factor ${e}^{z}$.
Constraint: ${\mathbf{scal}}=\text{'U'}$ or $\text{'S'}$.
5:     $\mathbf{cy}\left({\mathbf{n}}\right)$ – Complex (Kind=nag_wp) arrayOutput
On exit: the $N$ required function values: ${\mathbf{cy}}\left(i\right)$ contains ${K}_{\nu +i-1}\left(z\right)$, for $\mathit{i}=1,2,\dots ,N$.
6:     $\mathbf{nz}$ – IntegerOutput
On exit: the number of components of cy that are set to zero due to underflow. If ${\mathbf{nz}}>0$ and $\mathrm{Re}\left(z\right)\ge 0.0$, elements ${\mathbf{cy}}\left(1\right),{\mathbf{cy}}\left(2\right),\dots ,{\mathbf{cy}}\left({\mathbf{nz}}\right)$ are set to zero. If $\mathrm{Re}\left(z\right)<0.0$, nz simply states the number of underflows, and not which elements they are.
7:     $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, . 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  is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value  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:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{fnu}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{fnu}}\ge 0.0$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 1$.
On entry, scal has an illegal value: ${\mathbf{scal}}=〈\mathit{\text{value}}〉$.
On entry, ${\mathbf{z}}=\left(0.0,0.0\right)$.
${\mathbf{ifail}}=2$
No computation because $\left|{\mathbf{z}}\right|=〈\mathit{\text{value}}〉<〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=3$
No computation because ${\mathbf{fnu}}+{\mathbf{n}}-1=〈\mathit{\text{value}}〉$ is too large.
${\mathbf{ifail}}=4$
Results lack precision because $\left|{\mathbf{z}}\right|=〈\mathit{\text{value}}〉>〈\mathit{\text{value}}〉$.
Results lack precision because ${\mathbf{fnu}}+{\mathbf{n}}-1=〈\mathit{\text{value}}〉>〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=5$
No computation because $\left|{\mathbf{z}}\right|=〈\mathit{\text{value}}〉>〈\mathit{\text{value}}〉$.
No computation because ${\mathbf{fnu}}+{\mathbf{n}}-1=〈\mathit{\text{value}}〉>〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=6$
No computation – algorithm termination condition not met.
${\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

All constants in s18dcf are given to approximately $18$ digits of precision. Calling the number of digits of precision in the floating-point arithmetic being used $t$, then clearly the maximum number of correct digits in the results obtained is limited by $p=\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(t,18\right)$. Because of errors in argument reduction when computing elementary functions inside s18dcf, the actual number of correct digits is limited, in general, by $p-s$, where $s\approx \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,\left|{\mathrm{log}}_{10}\left|z\right|\right|,\left|{\mathrm{log}}_{10}\nu \right|\right)$ represents the number of digits lost due to the argument reduction. Thus the larger the values of $\left|z\right|$ and $\nu$, the less the precision in the result. If s18dcf is called with ${\mathbf{n}}>1$, then computation of function values via recurrence may lead to some further small loss of accuracy.
If function values which should nominally be identical are computed by calls to s18dcf with different base values of $\nu$ and different n, the computed values may not agree exactly. Empirical tests with modest values of $\nu$ and $z$ have shown that the discrepancy is limited to the least significant $3$ – $4$ digits of precision.

## 8Parallelism and Performance

s18dcf is not threaded in any implementation.

The time taken for a call of s18dcf is approximately proportional to the value of n, plus a constant. In general it is much cheaper to call s18dcf with n greater than $1$, rather than to make $N$ separate calls to s18dcf.
Paradoxically, for some values of $z$ and $\nu$, it is cheaper to call s18dcf with a larger value of n than is required, and then discard the extra function values returned. However, it is not possible to state the precise circumstances in which this is likely to occur. It is due to the fact that the base value used to start recurrence may be calculated in different regions for different n, and the costs in each region may differ greatly.
Note that if the function required is ${K}_{0}\left(x\right)$ or ${K}_{1}\left(x\right)$, i.e., $\nu =0.0$ or $1.0$, where $x$ is real and positive, and only a single function value is required, then it may be much cheaper to call s18acf, s18adf, s18ccf or s18cdf, depending on whether a scaled result is required or not.

## 10Example

The example program prints a caption and then proceeds to read sets of data from the input data stream. The first datum is a value for the order fnu, the second is a complex value for the argument, z, and the third is a character value to set the argument scal. The program calls the routine with ${\mathbf{n}}=2$ to evaluate the function for orders fnu and ${\mathbf{fnu}}+1$, and it prints the results. The process is repeated until the end of the input data stream is encountered.

### 10.1Program Text

Program Text (s18dcfe.f90)

### 10.2Program Data

Program Data (s18dcfe.d)

### 10.3Program Results

Program Results (s18dcfe.r)