S Chapter Contents
S Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentS14AEF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

S14AEF returns the value of the $k$th derivative of the psi function $\psi \left(x\right)$ for real $x$ and $k=0,1,\dots ,6$, via the function name.

## 2  Specification

 FUNCTION S14AEF ( X, K, IFAIL)
 REAL (KIND=nag_wp) S14AEF
 INTEGER K, IFAIL REAL (KIND=nag_wp) X

## 3  Description

S14AEF evaluates an approximation to the $k$th derivative of the psi function $\psi \left(x\right)$ given by
 $ψ k x=dkdxk ψx=dkdxk ddx loge⁡Γx ,$
where $x$ is real with $x\ne 0,-1,-2,\dots \text{}$ and $k=0,1,\dots ,6$. For negative noninteger values of $x$, the recurrence relationship
 $ψ k x+1=ψ k x+dkdxk 1x$
is used. The value of $\frac{{\left(-1\right)}^{k+1}{\psi }^{\left(k\right)}\left(x\right)}{k!}$ is obtained by a call to S14ADF, which is based on the routine PSIFN in Amos (1983).
Note that ${\psi }^{\left(k\right)}\left(x\right)$ is also known as the polygamma function. Specifically, ${\psi }^{\left(0\right)}\left(x\right)$ is often referred to as the digamma function and ${\psi }^{\left(1\right)}\left(x\right)$ as the trigamma function in the literature. Further details can be found in Abramowitz and Stegun (1972).
Abramowitz M and Stegun I A (1972) Handbook of Mathematical Functions (3rd Edition) Dover Publications
Amos D E (1983) Algorithm 610: A portable FORTRAN subroutine for derivatives of the psi function ACM Trans. Math. Software 9 494–502

## 5  Parameters

1:     X – REAL (KIND=nag_wp)Input
On entry: the argument $x$ of the function.
Constraint: ${\mathbf{X}}$ must not be ‘too close’ (see Section 6) to a non-positive integer.
2:     K – INTEGERInput
On entry: the function ${\psi }^{\left(k\right)}\left(x\right)$ to be evaluated.
Constraint: $0\le {\mathbf{K}}\le 6$.
3:     IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction 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, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ 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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{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{K}}<0$, or ${\mathbf{K}}>6$, or X is ‘too close’ to a non-positive integer. That is, .
${\mathbf{IFAIL}}=2$
The evaluation has been abandoned due to the likelihood of underflow. The result is returned as zero.
${\mathbf{IFAIL}}=3$
The evaluation has been abandoned due to the likelihood of overflow. The result is returned as zero.

## 7  Accuracy

All constants in S14ADF are given to approximately $18$ digits of precision. If $t$ denotes the number of digits of precision in the floating point arithmetic being used, then clearly the maximum number in the results obtained is limited by $p=\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(t,18\right)$. Empirical tests by Amos (1983) have shown that the maximum relative error is a loss of approximately two decimal places of precision. Further tests with the function $-{\psi }^{\left(0\right)}\left(x\right)$ have shown somewhat improved accuracy, except at points near the positive zero of ${\psi }^{\left(0\right)}\left(x\right)$ at $x=1.46\dots \text{}$, where only absolute accuracy can be obtained.

None.

## 9  Example

This example evaluates ${\psi }^{\left(2\right)}\left(x\right)$ at $x=2.5$, and prints the results.

### 9.1  Program Text

Program Text (s14aefe.f90)

### 9.2  Program Data

Program Data (s14aefe.d)

### 9.3  Program Results

Program Results (s14aefe.r)