# NAG CL Interfaces14apc (gamma_​log_​real_​vector)

Settings help

CL Name Style:

## 1Purpose

s14apc returns an array of values of the logarithm of the gamma function, $\mathrm{ln}\Gamma \left(x\right)$.

## 2Specification

 #include
 void s14apc (Integer n, const double x[], double f[], Integer ivalid[], NagError *fail)
The function may be called by the names: s14apc, nag_specfun_gamma_log_real_vector or nag_log_gamma_vector.

## 3Description

s14apc calculates an approximate value for $\mathrm{ln}\Gamma \left(x\right)$ for an array of arguments ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$. It is based on rational Chebyshev expansions.
Denote by ${R}_{n,m}^{i}\left(x\right)={P}_{n}^{i}\left(x\right)/{Q}_{m}^{i}\left(x\right)$ a ratio of polynomials of degree $n$ in the numerator and $m$ in the denominator. Then:
• for $0,
 $ln⁡Γ(x) ≈ -ln(x) + x R n,m 1 (x+1) ;$
• for $1/2,
 $ln⁡Γ(x) ≈ (x-1) R n,m 1 (x) ;$
• for $3/2,
 $ln⁡Γ(x) ≈ (x-2) R n,m 2 (x);$
• for $4,
 $ln⁡Γ(x) ≈ R n,m 3 (x) ;$
• and for $12,
 $ln⁡Γ(x) ≈ (x-12) ln(x) - x + ln(2π) + 1x R n,m 4 (1/x2) .$ (1)
For each expansion, the specific values of $n$ and $m$ are selected to be minimal such that the maximum relative error in the expansion is of the order ${10}^{-d}$, where $d$ is the maximum number of decimal digits that can be accurately represented for the particular implementation (see X02BEC).
Let $\epsilon$ denote machine precision and let ${x}_{\mathrm{huge}}$ denote the largest positive model number (see X02ALC). For $x<0.0$ the value $\mathrm{ln}\Gamma \left(x\right)$ is not defined; s14apc returns zero and exits with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NW_IVALID. It also exits with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NW_IVALID when $x=0.0$, and in this case the value ${x}_{\mathrm{huge}}$ is returned. For $x$ in the interval $\left(0.0,\epsilon \right]$, the function $\mathrm{ln}\Gamma \left(x\right)=-\mathrm{ln}\left(x\right)$ to machine accuracy.
Now denote by ${x}_{\mathrm{big}}$ the largest allowable argument for $\mathrm{ln}\Gamma \left(x\right)$ on the machine. For ${\left({x}_{\mathrm{big}}\right)}^{1/4} the ${R}_{n,m}^{4}\left(1/{x}^{2}\right)$ term in Equation (1) is negligible. For $x>{x}_{\mathrm{big}}$ there is a danger of setting overflow, and so s14apc exits with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_INT and returns ${x}_{\mathrm{huge}}$. The value of ${x}_{\mathrm{big}}$ is given in the Users' Note for your implementation.

## 4References

NIST Digital Library of Mathematical Functions
Cody W J and Hillstrom K E (1967) Chebyshev approximations for the natural logarithm of the gamma function Math.Comp. 21 198–203

## 5Arguments

1: $\mathbf{n}$Integer Input
On entry: $n$, the number of points.
Constraint: ${\mathbf{n}}\ge 0$.
2: $\mathbf{x}\left[{\mathbf{n}}\right]$const double Input
On entry: the argument ${x}_{\mathit{i}}$ of the function, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
Constraint: ${\mathbf{x}}\left[\mathit{i}-1\right]>0$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
3: $\mathbf{f}\left[{\mathbf{n}}\right]$double Output
On exit: $\mathrm{ln}\Gamma \left({x}_{i}\right)$, the function values.
4: $\mathbf{ivalid}\left[{\mathbf{n}}\right]$Integer Output
On exit: ${\mathbf{ivalid}}\left[\mathit{i}-1\right]$ contains the error code for ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
${\mathbf{ivalid}}\left[i-1\right]=0$
No error.
${\mathbf{ivalid}}\left[i-1\right]=1$
${x}_{i}\le 0$.
${\mathbf{ivalid}}\left[i-1\right]=2$
${x}_{i}$ is too large and positive. The threshold value is the same as for ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_REAL_ARG_GT in s14abc , as defined in the Users' Note for your implementation.
5: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
On entry, argument $⟨\mathit{\text{value}}⟩$ had an illegal value.
NE_INT
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 0$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NW_IVALID
On entry, at least one value of x was invalid.

## 7Accuracy

Let $\delta$ and $\epsilon$ be the relative errors in the argument and result respectively, and $E$ be the absolute error in the result.
If $\delta$ is somewhat larger than machine precision, then
 $E≃ |x×Ψ(x)| δ and ε≃ | x×Ψ(x) ln⁡Γ (x) | δ$
where $\Psi \left(x\right)$ is the digamma function $\frac{{\Gamma }^{\prime }\left(x\right)}{\Gamma \left(x\right)}$. Figure 1 and Figure 2 show the behaviour of these error amplification factors.
These show that relative error can be controlled, since except near $x=1$ or $2$ relative error is attenuated by the function or at least is not greatly amplified.
For large $x$, $\epsilon \simeq \left(1+\frac{1}{\mathrm{ln}x}\right)\delta$ and for small $x$, $\epsilon \simeq \frac{1}{\mathrm{ln}x}\delta$.
The function $\mathrm{ln}\Gamma \left(x\right)$ has zeros at $x=1$ and $2$ and hence relative accuracy is not maintainable near those points. However, absolute accuracy can still be provided near those zeros as is shown above.
If however, $\delta$ is of the order of machine precision, then rounding errors in the function's internal arithmetic may result in errors which are slightly larger than those predicted by the equalities. It should be noted that even in areas where strong attenuation of errors is predicted the relative precision is bounded by the effective machine precision.

## 8Parallelism and Performance

s14apc is not threaded in any implementation.

None.

## 10Example

This example reads values of x from a file, evaluates the function at each value of ${x}_{i}$ and prints the results.

### 10.1Program Text

Program Text (s14apce.c)

### 10.2Program Data

Program Data (s14apce.d)

### 10.3Program Results

Program Results (s14apce.r)