# The 2F1 bites the dust

October 19, 2015

The Gauss hypergeometric function ${}_2F_1(a,b,c,z)$ is finally in Arb. This versatile function has many applications in physics, statistics, and other fields. Its special cases include:

• Legendre and Jacobi polynomials, and their generalizations to complex orders (e.g. the Legendre functions $P_{\lambda}^{\mu}(z)$, $Q_{\lambda}^{\mu}(z)$)
• The incomplete beta function $B(z;\,a,b) = \int_0^z t^{a-1}\,(1-t)^{b-1}\,dt = z^a{}_2F_1(a,1-b;a+1;z) / a$
• Inverse trigonometric functions and complete elliptic integrals (of course, these functions are best computed by less general methods, as Arb indeed does)

Previously, the Gauss hypergeometric function could mostly be computed using the tools available in Arb for generalized hypergeometric series, but you had to piece those tools together for different cases (and some corner cases require further work, as discussed below). My project this weekend was to put everything together. The code commits are: 1, 2, 3, 4, 5, 6.

## Algorithm

The Gauss hypergeometric function is the analytic continuation of the hypergeometric series $${}_2F_1(a,b,c,z) = \sum_{k=0}^{\infty} \frac{(a)_k (b)_k}{(c)_k} \frac{z^k}{k!}$$ which converges for $|z| < 1$ for generic values of the parameters $a, b, c$ (for special integer values of the parameters, the series might terminate after a finite number of terms, and it might not exist at all when $c = 0, -1, -2, \ldots$). Arb also implements the regularized hypergeometric function $\mathop{\mathbf{F}}(a,b,c,z) = {}_2F_1(a,b,c,z) / \Gamma(c)$, which exists even when $c$ is a nonpositive integer.

The hypergeometric series is effective for computation when $|z| < 0.75$, say. For larger $|z|$, one can try to apply a linear fractional transformation to the argument (section 15.8 in the DLMF). A ${}_2F_1$ of argument $z \not\in \{0,1,\infty\}$ can always be transformed into a ${}_2F_1$ or a sum of two ${}_2F_1$'s of argument $$\frac{z}{z-1}, \quad \frac{1}{z}, \quad \frac{1}{1-z}, \quad 1-z, \quad 1-\frac{1}{z}.$$ For example, the $z \to 1/z$ formula reads $$\frac{\sin(\pi(b-a))}{\pi} \mathbf{F}(a,b,c,z) = \frac{(-z)^{-a}}{\Gamma(b)\Gamma(c-a)} \mathbf{F}\!\left(a,a-c+1,a-b+1,\frac{1}{z}\right) - \frac{(-z)^{-b}}{\Gamma(a)\Gamma(c-b)} \mathbf{F}\!\left(b,b-c+1,b-a+1,\frac{1}{z}\right).$$ Generally speaking, the goal is to choose the linear fractional transformation that results in the smallest absolute value, but there are also other factors affecting efficiency; for example, when sticking to $z$ or $z/(z-1)$, there is just one series to evaluate instead of two, and no extra gamma function values are required. The following picture shows how Arb currently chooses between $z$ (green), $z/(z-1)$ (aquamarine), $1/z$ (red), $1/(1-z)$ (blue), $1-z$ (yellow), $1-1/z$ (orange). The unit circle and the real and imaginary axes are drawn in white.

The black regions are the difficult corner cases. All transformations leave the points $\exp(\pm \pi i / 3) = \tfrac{1}{2} (1 \pm \sqrt{3} i)$ fixed (or simply swap them). Near these points, the hypergeometric series converges too slowly to be used for numerical evaluation, regardless of which argument transformations have been applied. Macsyma, Mathematica and others (including mpmath) solve this problem by switching to an algorithm by Bill Gosper. However, I currently don't have rigorous error bounds for this method. It's also possible to use standard convergence acceleration methods. Again, I don't have error bounds for such methods, so I can't use them at the moment.

My solution in Arb is to analytically continue the function using the hypergeometric differential equation $$z(1-z) F''(z) + \left[c-(a+b+1)z \right] F'(z) - ab F(z) = 0,$$ starting from initial values obtained at a point where the hypergeometric series converges rapidly. For high-precision integration of this ODE, local Taylor expansions are effective. I use the Cauchy-Kovalevskaya majorant method to obtain error bounds. The Cauchy-Kovalevskaya method is actually a powerful tool for showing the existence of solutions of partial differential equations, but it allows determining explicit local bounds as a nice side effect (for a refined and more general version of this method to compute bounds, see the paper Effective bounds for P-recursive sequences by Marc Mezzarobba and Bruno Salvy). The error bounds can become quite bad when the parameters $a,b,c$ are large, but overall this works decently.

The analytic continuation code in Arb currently performs two integration steps ($0.375 \pm 0.625i \to 0.5 \pm 0.8125i \to z$), which is a very simplistic way to do it. The algorithm could be optimized for high precision and rational parameters by taking a sequence of decreasing steps (this leads to the asymptotically fast "bit-burst" algorithm when done correctly).

### Limit cases

When applying transformation formulas, a problem arises: one ends up dividing by a factor $\sin(\pi(b-a))$ or $\sin(\pi(c-a-b))$, which becomes zero when $b-a$ or $c-a-b$ is an integer. Thus, one has to compute the limit $$\lim_{\varepsilon \to 0} {}_2F_1(a + \varepsilon,b,c,z).$$

There are different ways to do this. In mpmath, I settled for computing the limit numerically, effectively by setting $\varepsilon$ to a finite but nonzero value (e.g. roughly $2^{-53}$ at 53-bit precision). I don't want to do this in Arb, for three reasons: it would defeat potential future high-precision optimizations, it would would give far too wide output intervals in some cases, and most importantly, I don't have rigorous error bounds for this method.

The more robust way is to compute the limit formally. In reference works, one can find ugly-looking formulas for the limiting cases of the ${}_2F_1$ involving infinite series of digamma functions. However, we don't need these series expansions explicitly (in particular, there's no need to do a separate error analysis for each such infinite series). Instead, we can implement the normal transformations for ${}_2F_1$ using arithmetic in $\mathbb{C}[[\varepsilon]] / \langle \varepsilon^2 \rangle$, that is, with dual numbers. Evaluating the convergent hypergeometric series with power series as parameters is already implemented in Arb. Now computing the limit $\lim_{\varepsilon \to 0} {}_2F_1(a + \varepsilon,b,c,z)$ is just an application of L'Hôpital's rule. No ugly special limit formulas required!

## The full picture

A simple test is to plot ${}_2F_1(0.2+0.3i, 0.4+0.5i, 0.5+0.6i, z)$ on $z \in [-4,4] + [-2,2]i$ at a resolution of 1024 times 512 pixels. For plotting, 64-bit precision is more than enough. The following plot takes 200 seconds to produce with Arb, which is six times faster than mpmath (1260 seconds) at the same precision (with the simpler parameters $1/4, 1/2, 1$ instead, Arb takes 60 seconds, which is 14 times faster than mpmath).

It's interesting to check how much accuracy we lose, considering that the input parameters are inexact intervals (for example, $0.2$ is parsed as $[3689348814741910323 \cdot 2^{-64} \pm 2^{-66}]$) and that the error bounds grow when doing a sequence of interval operations. The following plot uses brightness to indicate the number of accurate bits divided by 64; that is, white would represent full 64-bit accuracy in the output and black would represent complete loss of accuracy. The loss of accuracy generally turns out to be around 5-12 bits except near $\exp(\pm \pi i / 3)$ where up to 23 bits are lost. This is probably improvable, but it's not too shabby.

## High-precision benchmarks

I did a few tests to compare mpmath and Arb on my laptop which has a 1.90 GHz Intel i5-4300U CPU. I've also included timings for Mathematica 9.0 on a 3.07 GHz Intel Xeon X5675 CPU.

### Easy case

${}_2F_1(1/2,1/4,1,z)$ at $z = \sqrt{5} + \sqrt{7} i$. The parameters are simple numbers, the $1/z$ transformation gives rapid convergence, and no limit has to be computed. As a side note, with these parameters, the function is equivalent to an elliptic integral, which can be used as an independent check of correctness. I use 20 guard bits with Arb to get a result with full accuracy.

 Digits Mathematica mpmath Arb 10 0.00066 s 0.00090 s 0.00011 s 100 0.0039 s 0.0017 s 0.00075 s 1000 0.23 s 1.5 s 0.019 s 10000 42.6 s 98 s 1.2 s

### Corner case

${}_2F_1(1/2,1/4,1,z)$ at $z = \exp(\pi i / 3)$. It's clear that the algorithm in Arb could be optimized.

 Digits Mathematica mpmath Arb 10 0.00050 s 0.0048 s 0.00060 s 100 0.0056 s 0.031 s 0.0046 s 1000 0.17 s 0.46 s 0.16 s 10000 30 s 51 s 29 s

### Limit case

${}_2F_1(10+i,5+i,1,z)$ at $z = \sqrt{5} + \sqrt{7} i$. Here, a limit has to be computed when applying the $1/z$ transformation since $a-b$ is an integer, which slows things down.

 Digits Mathematica mpmath Arb 10 0.0025 s 0.0060 s 0.00066 s 100 0.016 s 0.10 s 0.0042 s 1000 0.46 s 27 s 0.26 s 10000 108 s - 56 s

### A precision challenge

We compute $\log(-\operatorname{Re}[{}_2F_1(500i, -500i, -500 + 10^{-500}i, 3/4)])$ accurately. Here's a friendly reminder that Mathematica's N[] cannot be trusted (to be fair, the same warning applies to any software).

Mathematica 9.0 for Linux x86 (64-bit)
Copyright 1988-2013 Wolfram Research, Inc.