# Efficiently calculating the logarithmic integral with complex argument

My number theory library of choice doesn't implement the logarithmic integral for complex values. I thought that I might take a crack at coding it, but I thought I'd ask here first for algorithmic advice and/or references. I'm sure there are better methods than naively calculating the integral.

-

For this answer, I'm assuming the definition

$$\mathrm{li}(z):=\mathrm{PV}\int_0^z\frac{\mathrm{d}u}{\ln\;u}$$

where we assume the Cauchy principal value (the more common definition in number theory that has a lower limit of 2 differs merely by a constant).

Well, the first thing you have to note is the identity

$$\mathrm{li}(z)=\mathrm{Ei}(\ln\;z)$$

where $\mathrm{Ei}(z)$ is an exponential integral. (Again, I repeat my advice to people who encounter strange functions: you would really do well to check the DLMF first for identities and references.)

Now, $\mathrm{Ei}(z)$ is a slightly more tractable beastie to numerically evaluate, since the singularity at $z=0$ can be confined to a logarithmic part; to wit:

$\mathrm{Ei}(z)=\gamma+\ln\;z+\int_0^z \frac{\exp(u)-1}{u}\mathrm{d}u$

where the last portion is an entire function.

Now, depending on which of the left or right half-planes should the exponential integral be evaluated, your strategy will differ (it is a common fact that most special function routines are polyalgorithms, since their behavior can markedly differ in different regions of the complex plane). I will be vague for the rest of this answer since you did not clarify your region of interest. Suffice it to say that one usually uses a continued fraction for arguments in the left half-plane, a power series for small to medium-sized arguments, and asymptotic expansions for large arguments.

If implementing it yourself is starting to sound daunting (because it is), I will have to point out this paper and the corresponding FORTRAN subroutine.

Hope this helps a bit.

-
Excellent answer, very complete! Yes, I expected to be evaluating Ei, that's how the library handles Li now (though again, only for real arguments). –  Charles Nov 4 '10 at 12:49
@Charles: For estimating the prime-counting function, you don't need it for complex arguments; for evaluating the Riemann function, maybe... if you don't mind my asking, what exactly do you want to do with the logarithmic integral? An especially-tailored solution might be better than this general one I gave, certainly if speed is of the essence (Evaluating special functions at complex arguments is always expensive). –  J. M. is back. Nov 4 '10 at 13:44
@J. M.: I want to add it to a library (Pari), so covering all possible ranges is important. My immediate need is relatively unimportant -- just some exploratory work with Riemann's explicit formula. Since this (if successful) will be part of a library, speed is important. –  Charles Nov 4 '10 at 14:10
@Charles: in that case, may I suggest that you maintain separate routines for the real argument and complex argument cases, and have a top-level routine that will call whichever of the two is appropriate. The routine by Donald Amos serves well in the complex case, and as for the real case, there are a number of submissions to ACM's Transactions on Mathematical Software to do that job. –  J. M. is back. Nov 4 '10 at 14:15
I forgot to add: for medium sized (real or complex) arguments, I have found from a few numerical experiments that formula 15 in here, due to Ramanujan, might have a slight edge over the usual Maclaurin series. You will have to of course check with your environment to be certain. –  J. M. is back. Nov 4 '10 at 14:22