I'm trying to develop an algorithm to calculate the incomplete gamma function for complex input values to arbitrary precision.
For some ranges of input values, I use the continued fraction formula:
$$\Gamma(a, z)= \cfrac{z^a e^{-z}}{1+z-a+ \cfrac{a-1}{3+z-a+ \cfrac{2(a-2)}{5+z-a+ \cfrac{3(a-3)} {7+z-a+ \cfrac{4(a-4)}{9+z-a+ \ddots}}}}}$$
I'm calculating the continued fraction with the (modified) Lentz's algorithm.
For a wide range of input values this algorithm works fine (if $a$ and $z$ aren't too large in absolute value and $z$ is not too close to the negative real axis and $|a|$ is roughly the same or less than $|z|$).
However there are some relatively narrow ranges of $a$ and $z$ where the continued fraction does not converge well. It initially converges to a wrong value - possibly to a precision of 100 digits or more - until it diverges away from the wrong value and then finally converges to the correct (?) value.
For example:
$\Gamma(-1 \cdot 10^{-2} + 4 \cdot 10^2i, 1 \cdot 10^{-2} + 1 \cdot 10^2i)$
When I run the algorithm with my arbitrary precision software using 150 digits of precision the continued fraction (excluding the factor of $z^a e^{-z}$) converges as follows.
In the first 240 iterations it converges to approximately this value (to a precision of 101 digits): $-3.4812455650395559724083225442044685679172703381183511740233299928770701153447585747686976555814151526 \cdot 10^{-6} + 3.3333097415343713576386150215385070073982379130010751824025858800036447026117907322447199186033463044446\cdot 10^{-3}i$
It circles around that point for maybe 120 iterations more and then starts to diverge away from it.
At 1240 iterations (from the beginning) it has diverged fully away and starts to move to a completely different point. The value changes substantially.
At around 1300 iterations it starts to converge to a different point (which I guess is the correct one):
$-8.88 \cdot 10^{-2} + 8.39 \cdot 10^{-2}i$
It keeps converging and after 2647 iterations the algorithm stops as it thinks it has the value correctly to a precision of 150 digits.
Assuming that Mathematica returns the correct value, it is:
Gamma[-1*10^-2 + 4*10^2 I, 1*10^-2 + 1*10^2 I] // N
-1.05428*10^-274 - 1.20736*10^-274 I
I can also verify the convergence of the continued fraction with Mathematica, using exact values, so I'm fairly certain that the problem isn't just error propagation in the Lentz's algorithm:
z^a Exp[-z] ContinuedFractionK[If[n == 1, 1, (n - 1) (a - n + 1)], (2 n - 1 + z - a), {n, 1, 240}] /. a -> -1*10^-2 + 4*10^2 I /. z -> 1*10^-2 + 1*10^2 I // N
-4.36799*10^-276 - 1.7607*10^-277 I
z^a Exp[-z] ContinuedFractionK[If[n == 1, 1, (n - 1) (a - n + 1)], (2 n - 1 + z - a), {n, 1, 1240}] /. a -> -1*10^-2 + 4*10^2 I /. z -> 1*10^-2 + 1*10^2 I // N
-4.36917*10^-276 - 1.75839*10^-277 I
z^a Exp[-z] ContinuedFractionK[If[n == 1, 1, (n - 1) (a - n + 1)], (2 n - 1 + z - a), {n, 1, 1300}] /. a -> -1*10^-2 + 4*10^2 I /. z -> 1*10^-2 + 1*10^2 I // N
-1.05267*10^-274 - 1.21171*10^-274 I
z^a Exp[-z] ContinuedFractionK[If[n == 1, 1, (n - 1) (a - n + 1)], (2 n - 1 + z - a), {n, 1, 2647}] /. a -> -1*10^-2 + 4*10^2 I /. z -> 1*10^-2 + 1*10^2 I // N
-1.05428*10^-274 - 1.20736*10^-274 I
Problem: If I want to calculate the result e.g. to just 50 digits of precision then there's really no way to know that it initially converges to a wrong value (because it converges to the wrong value to a precision of 101 digits).
Questions:
- How can I know, when the algorithm initially converges to an incorrect value and will only converge to the correct value later with more iterations and higher precision?
- Is there any way to estimate what is the minimum precision needed to detect that it initially converges to an incorrect value?
- Is there a better algorithm (or set of algorithms) for the complex incomplete gamma function that converges for all possible input values (for $\gamma(a,z)$ I already have a different continued fraction and the sum formulae for small $|z|$ and for negative integer $a$ yet a different algorithm)?