Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_specfun_2f1_real (s22be)

## Purpose

nag_specfun_2f1_real (s22be) returns a value for the Gauss hypergeometric function ${}_{2}F_{1}\left(a,b;c;x\right)$ for real parameters $a,b$ and $c$, and real argument $x$.

## Syntax

[result, ifail] = s22be(a, b, c, x)
[result, ifail] = nag_specfun_2f1_real(a, b, c, x)

## Description

nag_specfun_2f1_real (s22be) returns a value for the Gauss hypergeometric function ${}_{2}F_{1}\left(a,b;c;x\right)$ for real parameters $a$, $b$ and $c$, and for real argument $x$.
The associated function nag_specfun_2f1_real_scaled (s22bf) performs the same operations, but returns ${}_{2}F_{1}\left(a,b;c;x\right)$ in the scaled form ${}_{2}F_{1}\left(a,b;c;x\right)={f}_{\mathrm{fr}}×{2}^{{f}_{\mathrm{sc}}}$ to allow calculations to be performed when ${}_{2}F_{1}\left(a,b;c;x\right)$ is not representable as a single working precision number. It also accepts the parameters $a$, $b$ and $c$ as summations of an integer and a decimal fraction, giving higher accuracy when any are close to an integer.
The Gauss hypergeometric function is a solution to the hypergeometric differential equation,
 $x1-x d2 f dx2 + c-a+b+1x d f dx - a b f = 0 .$ (1)
For $\left|x\right|<1$, it may be defined by the Gauss series,
 $F1 2 a,b;c;x = ∑ s=0 ∞ as bs cs s! xs = 1+ ab c x + aa+1 bb+1 cc+1 2! x2 + ⋯ ,$ (2)
where ${\left(a\right)}_{s}=1\left(a\right)\left(a+1\right)\left(a+2\right)\dots \left(a+s-1\right)$ is the rising factorial of $a$. ${}_{2}F_{1}\left(a,b;c;x\right)$ is undefined for $c=0$ or $c$ a negative integer.
For $\left|x\right|<1$, the series is absolutely convergent and ${}_{2}F_{1}\left(a,b;c;x\right)$ is finite.
For $x<1$, linear transformations of the form,
 $F1 2 a,b;c;x = C1 a1,b1,c1,x1 F1 2 a1, b1 ;c1;x1 + C2 a2,b2,c2,x2 F1 2 a2, b2 ;c2;x2$ (3)
exist, where ${x}_{1}$, ${x}_{2}\in \left(0,1\right]$. ${C}_{1}$ and ${C}_{2}$ are real valued functions of the parameters and argument, typically involving products of gamma functions. When these are degenerate, finite limiting cases exist. Hence for $x<0$, ${}_{2}F_{1}\left(a,b;c;x\right)$ is defined by analytic continuation, and for $x<1$, ${}_{2}F_{1}\left(a,b;c;x\right)$ is real and finite.
For $x=1$, the following apply:
• If $c>a+b$, ${}_{2}F_{1}\left(a,b;c;1\right)=\frac{\Gamma \left(c\right)\Gamma \left(c-a-b\right)}{\Gamma \left(c-a\right)\Gamma \left(c-b\right)}$, and hence is finite. Solutions also exist for the degenerate cases where $c-a$ or $c-b$ are negative integers or zero.
• If $c\le a+b$, ${}_{2}F_{1}\left(a,b;c;1\right)$ is infinite, and the sign of ${}_{2}F_{1}\left(a,b;c;1\right)$ is determinable as $x$ approaches $1$ from below.
In the complex plane, the principal branch of ${}_{2}F_{1}\left(a,b;c;z\right)$ is taken along the real axis from $x=1.0$ increasing. ${}_{2}F_{1}\left(a,b;c;z\right)$ is multivalued along this branch, and for real parameters $a,b$ and $c$ is typically not real valued. As such, this function will not compute a solution when $x>1$.
The solution strategy used by this function is primarily dependent upon the value of the argument $x$. Once trivial cases and the case $x=1.0$ are eliminated, this proceeds as follows.
For $0, sets of safe parameters $\left\{{\alpha }_{i,j};{\beta }_{i,j};{\zeta }_{i,j};{\chi }_{j}\left|1\le j\le 2\right|;1\le i\le 4\right\}$ are determined, such that the values of ${}_{2}F_{1}\left({a}_{j},{b}_{j};{c}_{j};{x}_{j}\right)$ required for an appropriate transformation of the type (3) may be calculated either directly or using recurrence relations from the solutions of ${}_{2}F_{1}\left({\alpha }_{i,j},{\beta }_{i,j};{\zeta }_{i,j};{\chi }_{j}\right)$. If $c$ is positive, then only transformations with ${C}_{2}=0.0$ will be used, implying only ${}_{2}F_{1}\left({a}_{1},{b}_{1};{c}_{1};{x}_{1}\right)$ will be required, with the transformed argument ${x}_{1}=x$. If $c$ is negative, in some cases a transformation with ${C}_{2}\ne 0.0$ will be used, with the argument ${x}_{2}=1.0-x$. The function then cycles through these sets until acceptable solutions are generated. If no computation produces an accurate answer, the least inaccurate answer is selected to complete the computation. See Accuracy.
For $0.5, an identical approach is first used with the argument $x$. Should this fail, a linear transformation resulting in both transformed arguments satisfying ${x}_{j}=1.0-x$ is employed, and the above strategy for $0 is utilized on both components. Further transformations in these sub-computations are however limited to single terms with no argument transformation.
For $x<0$, a linear transformation mapping the argument $x$ to the interval $\left(0,0.5\right]$ is first employed. The strategy for $0 is then used on each component, including possible further two term transforms. To avoid some degenerate cases, a transform mapping the argument $x$ to $\left[0.5,1\right)$ may also be used.
In addition to the above restrictions on $c$ and $x$, an artificial bound, arbnd, is placed on the magnitudes of $a,b,c$ and $x$ to minimize the occurrence of overflow in internal calculations, particularly those involving real to integer conversions. $\mathit{arbnd}=0.0001×{I}_{\mathrm{max}}$, where ${I}_{\mathrm{max}}$ is the largest machine integer (see nag_machine_integer_max (x02bb)). It should however not be assumed that this function will produce accurate answers for all values of $a,b,c$ and $x$ satisfying this criterion.
This function also tests for non-finite values of the parameters and argument on entry, and assigns non-finite values upon completion if appropriate. See Further Comments.
Please consult the NIST Digital Library of Mathematical Functions or the companion (2010) for a detailed discussion of the Gauss hypergeometric function including special cases, transformations, relations and asymptotic approximations.

## References

NIST Handbook of Mathematical Functions (2010) (eds F W J Olver, D W Lozier, R F Boisvert, C W Clark) Cambridge University Press
Pearson J (2009) Computation of hypergeometric functions MSc Dissertation, Mathematical Institute, University of Oxford

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{a}$ – double scalar
The parameter $a$.
Constraint: $\left|{\mathbf{a}}\right|\le \mathit{arbnd}$.
2:     $\mathrm{b}$ – double scalar
The parameter $b$.
Constraint: $\left|{\mathbf{b}}\right|\le \mathit{arbnd}$.
3:     $\mathrm{c}$ – double scalar
The parameter $c$.
Constraints:
• $\left|{\mathbf{c}}\right|\le \mathit{arbnd}$;
• ${\mathbf{c}}\ne 0,-1,-2,\dots$.
4:     $\mathrm{x}$ – double scalar
The argument $x$.
Constraint: $-\mathit{arbnd}<{\mathbf{x}}\le 1$.

None.

### Output Parameters

1:     $\mathrm{result}$ – double scalar
2:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
Underflow occurred during the evaluation of ${}_{2}F_{1}\left(a,b;c;x\right)$. The returned value may be inaccurate.
${\mathbf{ifail}}=2$
All approximations have completed, and the final residual estimate indicates some precision may have been lost.
${\mathbf{ifail}}=3$
All approximations have completed, and the final residual estimate indicates no accuracy can be guaranteed.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{x}}=_$, $c=_$, $a+b=_$.
${}_{2}F_{1}\left(a,b;c;1\right)$ is infinite in the case $c\le a+b$.
${\mathbf{ifail}}=5$
On completion, overflow occurred in the evaluation of ${}_{2}F_{1}\left(a,b;c;x\right)$.
${\mathbf{ifail}}=6$
Overflow occurred in a subcalculation of ${}_{2}F_{1}\left(a,b;c;x\right)$. The result may or may not be infinite.
${\mathbf{ifail}}=9$
An internal calculation has resulted in an undefined result.
${\mathbf{ifail}}=11$
On entry, a does not satisfy $\left|{\mathbf{a}}\right|\le \mathit{arbnd}=_$.
${\mathbf{ifail}}=21$
On entry, b does not satisfy $\left|{\mathbf{b}}\right|\le \mathit{arbnd}=_$.
${\mathbf{ifail}}=31$
On entry, c does not satisfy $\left|{\mathbf{c}}\right|\le \mathit{arbnd}=_$.
${\mathbf{ifail}}=32$
On entry.
${}_{2}F_{1}\left(a,b;c;x\right)$ is undefined when $c$ is zero or a negative integer.
${\mathbf{ifail}}=41$
On entry, x does not satisfy $\left|{\mathbf{x}}\right|\le \mathit{arbnd}=_$.
${\mathbf{ifail}}=42$
On entry.
In general, ${}_{2}F_{1}\left(a,b;c;x\right)$ is not real valued when $x>1$.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

In general, if ${\mathbf{ifail}}={\mathbf{0}}$, the value of ${}_{2}F_{1}\left(a,b;c;x\right)$ may be assumed accurate, with the possible loss of one or two decimal places. Assuming the result does not under or overflow, an error estimate $\mathit{res}$ is made internally using equation (1). If the magnitude of $\mathit{res}$ is sufficiently large, a nonzero ifail will be returned. Specifically,
 ${\mathbf{ifail}}={\mathbf{0}}$ or ${\mathbf{1}}$ $\mathit{res}\le 1000\epsilon$ ${\mathbf{ifail}}={\mathbf{2}}$ $1000\epsilon <\mathit{res}\le 0.1$ ${\mathbf{ifail}}={\mathbf{3}}$ $\mathit{res}>0.1$
where $\epsilon$ is the machine precision as returned by nag_machine_precision (x02aj).
A further estimate of the residual can be constructed using equation (1), and the differential identity,
 $d F 1 2 a,b; c;x dx = ab c F 1 2 a+1,b+1; c+1;x d2 F 1 2 a,b; c;x dx2 = aa+1 bb+1 cc+1 F 1 2 a+2,b+2; c+2;x$ (4)
This estimate is however dependent upon the error involved in approximating ${}_{2}F_{1}\left(a+1,b+1;c+1;x\right)$ and ${}_{2}F_{1}\left(a+2,b+2;c+2;x\right)$.
Furthermore, the accuracy of the solution, and the error estimate, can be dependent upon the accuracy of the decimal fraction of the input parameters $a$ and $b$. For example, if $c={c}_{i}+{c}_{r}=100+\text{1.0e−6}$, then on a machine with $16$ decimal digits of precision, the internal calculation of ${c}_{r}$ will only be accurate to $8$ decimal places. This can subsequently pollute the final solution by several decimal places without affecting the residual estimate as greatly. Should you require higher accuracy in such regions, then you should use nag_specfun_2f1_real_scaled (s22bf), which requires you to supply the correct decimal fraction.

nag_specfun_2f1_real (s22be) returns non-finite values when appropriate.
Should a non-finite value be returned, this will be indicated in the value of ifail, as detailed in the following cases.
If ${\mathbf{ifail}}={\mathbf{0}}$, or ${\mathbf{ifail}}={\mathbf{1}}$, ${\mathbf{2}}$ or ${\mathbf{3}}$, a finite value will have been returned with an approximate accuracy as detailed in Accuracy.
If ${\mathbf{ifail}}={\mathbf{4}}$ then ${}_{2}F_{1}\left(a,b;c;x\right)$ is infinite, and a signed infinity will have been returned. The sign of the infinity should be correct when taking the limit as $x$ approaches $1$ from below.
If ${\mathbf{ifail}}={\mathbf{5}}$ then upon completion, $\left|{}_{2}F_{1}\left(a,b;c;x\right)\right|>{R}_{\mathrm{max}}$, where ${R}_{\mathrm{max}}$ is the largest machine number given by nag_machine_real_largest (x02al), and hence is too large to be representable. The result will be returned as a signed infinity. The sign should be correct.
If ${\mathbf{ifail}}={\mathbf{6}}$ then overflow occurred during a subcalculation of ${}_{2}F_{1}\left(a,b;c;x\right)$. A signed infinity will have been returned, however there is no guarantee that this is representative of either the magnitude or the sign of ${}_{2}F_{1}\left(a,b;c;x\right)$.
For all other error exits, nag_specfun_2f1_real (s22be) will return a signalling NaN
If ${\mathbf{ifail}}={\mathbf{9}}$ then an internal computation produced an undefined result. This may occur when two terms overflow with opposite signs, and the result is dependent upon their summation for example.
If ${\mathbf{ifail}}={\mathbf{32}}$ then $c$ is too close to a negative integer or zero on entry, and ${}_{2}F_{1}\left(a,b;c;x\right)$ is considered undefined. Note, this will also be the case when $c$ is a negative integer, and a (possibly trivial) linear transformation of the form (3) would result in either:
 (i) all ${c}_{j}$ not being negative integers, (ii) for any ${c}_{j}$ which remain as negative integers, one of the corresponding parameters ${a}_{j}$ or ${b}_{j}$ is a negative integer of magnitude less than ${c}_{j}$.
In the first case, the transformation coefficients ${C}_{j}\left({a}_{j},{b}_{j},{c}_{j},{x}_{j}\right)$ are typically either infinite or undefined, preventing a solution being constructed. In the second case, the series (2) will terminate before the degenerate term, resulting in a polynomial of fixed degree, and hence potentially a finite solution.
If ${\mathbf{ifail}}={\mathbf{11}}$, ${\mathbf{21}}$, ${\mathbf{31}}$ or ${\mathbf{41}}$ then no computation will have been performed. The actual solution may however be finite.
${\mathbf{ifail}}={\mathbf{42}}$ indicates $x>1$. Hence the requested solution is on the boundary of the principal branch of ${}_{2}F_{1}\left(a,b;c;x\right)$, and hence is multivalued, typically with a non-zero imaginary component. It is however strictly finite.

## Example

This example evaluates ${}_{2}F_{1}\left(a,b;c;x\right)$ at a fixed set of parameters $a,b$ and $c$, and for several values for the argument $x$.
```function s22be_example

fprintf('s22be example results\n\n');

% Evaluate 2F1(a,b;c;x) for fixed a,b,c

a =  1.2;
b = -2.6;
c =  3.5;
x = [-4:0.25:1];
f = x;

for i = 1:21
[f(i), ifail] = s22be(a, b, c, x(i));
end

fprintf(' a = %4.1f, b = %4.1f, c = %4.1f\n\n',a,b,c);
disp('    x     2F1(a,b;c;x)');
fprintf('%7.2f%12.4f\n',[x;f]);

fig1 = figure;
plot(x,f);
ttext = sprintf('Gausian Hypergeometric for a=%4.1f, b=%4.1f, c=%4.1f',a,b,c);
title(ttext);
xlabel('x');
ylabel('_2F_1(a,b;c;x)');

```
```s22be example results

a =  1.2, b = -2.6, c =  3.5

x     2F1(a,b;c;x)
-4.00     12.3289
-3.75     11.0602
-3.50      9.8783
-3.25      8.7806
-3.00      7.7649
-2.75      6.8286
-2.50      5.9692
-2.25      5.1841
-2.00      4.4707
-1.75      3.8263
-1.50      3.2480
-1.25      2.7330
-1.00      2.2784
-0.75      1.8811
-0.50      1.5378
-0.25      1.2453
0.00      1.0000
0.25      0.7983
0.50      0.6362
0.75      0.5094
1.00      0.3659
``` 