Tell me more ×
Computational Science Stack Exchange is a question and answer site for scientists using computers to solve scientific problems. It's 100% free, no registration required.

What is the state of the art way of implementing double precision special functions? I need the following integral: $$ F_m(t) = \int_0^1 u^{2m} e^{-tu^2} d u = {\gamma(m+{1\over 2}, t)\over 2 t^{m+{1\over 2}}} $$ for $m=0, 1, 2, ...$ and $t>0$, which can be written in terms of the lower incomplete gamma function. Here is my Fortran and C implementation:

https://gist.github.com/3764427

which uses series expansion, sums up the terms until the given accuracy, and then uses recursion relations to efficiently obtain values for lower $m$. I tested it well and I get 1e-15 accuracy for all values of parameters that I need, see the Fortran version's comments for details.

Is there a better way to implement it? Here is a gamma function implementation in gfortran:

https://github.com/mirrors/gcc/blob/master/libgfortran/intrinsics/c99_functions.c#L1781

it is using rational function approximation instead of summing up some infinite series that I am doing. I think that's a better approach, because one should obtain uniform accuracy. Is there some canonical way to approach these things, or does one have to figure out a special algorithm for each special function?

Update 1:

Based on the comments, here is the implementation using SLATEC:

https://gist.github.com/3767621

it reproduces values from my own function, roughly on the level of 1e-15 accuracy. However, I noticed a problem that for t=1e-6 and m=50, the $t^{m+{1\over2}}$ term gets equal to 1e-303 and for higher "m" it simply starts giving incorrect answers. My function doesn't have this problem, because I use a series expansion/recurrence relations directly for $F_m$. Here is an example of a correct value:

$F_{100}$(1e-6)=4.97511945200351715E-003,

but I can't get this using SLATEC because the denominator blows up. As you can see, the actual value of $F_m$ is nice and small.

Update 2:

To avoid the above problem, one can use the function dgamit (Tricomi's incomplete Gamma function), then F(m, t) = dgamit(m+0.5_dp, t) * gamma(m+0.5_dp) / 2, so there is no problem with $t$ anymore, but unfortunately the gamma(m+0.5_dp) blows up for $m\approx 172$. This however might be high enough $m$ for my purposes.

share|improve this question
2  
Why code your own function? GSL, cephes, and SLATEC all implement it. – Geoff Oxberry Sep 22 '12 at 10:55
I've updated the question why I don't use SLATEC. – Ondřej Čertík Sep 22 '12 at 20:06
@OndřejČertík You have apperantly discovered a bug! Upvoted your question! – Ali Sep 22 '12 at 20:22
Ali --- it's not a bug in SLATEC, but in the fact, that I actually need to divide the $\gamma(z, x)$ by $t^{m+{1\over2}}$ in order to obtain a value for $F_m(t)$. So the numerical method that works for $\gamma(z, x)$ might not work so well for $F_m(t)$. – Ondřej Čertík Sep 22 '12 at 21:14
@OndřejČertík OK, sorry, my mistake, I didn't check your code before making my comment. – Ali Sep 23 '12 at 9:12

3 Answers

up vote 3 down vote accepted

I'd take a look at Abramowicz & Stegun's book, or the newer revision that NIST has published a couple of years ago and that's available online I believe. They also discuss ways to implement things in a stable way.

share|improve this answer
I was using this: dlmf.nist.gov/8, when implementing it, but that's probably another resource. The chapter 5 in Numerical Recipes also has interesting info, but only applicable to functions of one variable. – Ondřej Čertík Sep 22 '12 at 0:56
I don't think you'll find anything much more recent than their 2001 reference; SLATEC will be older than that. – Geoff Oxberry Sep 22 '12 at 10:40

You might take a look at Numerical Methods for Special Functions by Amparo Gil, Javier Segura, and Nico M. Temme.

share|improve this answer
This is a great book, thanks for the tip! – Ondřej Čertík Sep 22 '12 at 18:33

It doesn't seem to be state-of-the-art but SLATEC at Netlib offers "1400 general purpose mathematical and statistical routines." The incomplete Gamma is available under the special functions here.

Implementing such functions is time consuming and error prone so I wouldn't do it myself unless absolutely necessary. SLATEC has been around for quite a while now and is widely used, at least based on the download counts, so I would expect the implementation to be mature.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.