I know this is an old question, but I would like to give a small comparison regarding efficiency when evaluating the Boys function $F_n(x)$. Below are some implementations (in Julia) with simple benchmarks.
In the end, I also give some useful approximations to the Boys function, both for small and large values of $x$.
For benchmarking, I'll use BenchmarkTools
to measure the average time it takes for a particular implementation to calculate $F_1(x)$ $\forall x \in \hat{x}$, where $\tilde{x}$ is the 10000-vector below:
using BenchmarkTools
n = 1
x̃ = range(1e-15, 15, length=10000)
Implementations are in order, from fastest to slowest. (Of course, benchmarks should be compared with each other; different computers will produce different results.)
Evaluations
Using the incomplete gamma function
As mentioned by Eric above, $F_n(x)$ can be written in terms of the incomplete gamma function as
$$\frac{1}{2 x^{\frac{1}{2} + n}} \Gamma \left( \frac{1}{2} + n \right) \gamma \left( \frac{1}{2} + n, x \right)$$
using SpecialFunctions
# Incomplete gamma function implementation.
boys2(n, x) = gamma(0.5 + n) * gamma_inc(0.5 + n, x, 0)[1] / (2x^(0.5 + n))
@benchmark boys2.(n, x̃)
BenchmarkTools.Trial:
memory estimate: 406.52 KiB
allocs estimate: 1005
--------------
minimum time: 1.317 ms (0.00% GC)
median time: 1.345 ms (0.00% GC)
mean time: 1.380 ms (0.33% GC)
maximum time: 2.880 ms (0.00% GC)
--------------
samples: 3614
evals/sample: 1
Using quadratures
The simplest option is a quadrature of
$$\int_0^1 t^{2n} e^{-x t^2} dt$$
using QuadGK
# Adaptive Gauss-Kronrod quadrature implementation.
boys1(n, x) = quadgk(t -> t^(2n) * exp(-x * t^2), 0.0, 1.0)[1]
@benchmark boys1.(n, x̃)
BenchmarkTools.Trial:
memory estimate: 8.32 MiB
allocs estimate: 326405
--------------
minimum time: 23.747 ms (0.00% GC)
median time: 24.629 ms (0.00% GC)
mean time: 25.839 ms (3.78% GC)
maximum time: 45.680 ms (0.00% GC)
--------------
samples: 194
evals/sample: 1
Using the confluent hypergeometric function
Yet another relation, mentioned by Joshua above, uses the Kummer confluent hypergeometric function in
$$F_n(x) = \frac{{_1}F_1 \left( \frac{1}{2} + n, \frac{3}{2} + n, -x \right)}{2n + 1}$$
using using HypergeometricFunctions
# Confluent hypergeometric function implementation.
boys3(n, x) = pFq([0.5 + n], [1.5 + n], -x) / (2n + 1)
@benchmark boys3.(n, x̃)
BenchmarkTools.Trial:
memory estimate: 18.03 MiB
allocs estimate: 162806
--------------
minimum time: 58.511 ms (0.00% GC)
median time: 59.524 ms (0.00% GC)
mean time: 60.007 ms (1.19% GC)
maximum time: 65.223 ms (0.00% GC)
--------------
samples: 84
evals/sample: 1
Approximations
Small values of $x$
We can expand the integrand $t^{2n} e^{-x t^2}$ as a series around $x = 0$ and obtain successive polynomial approximations to the Boys function. Below is a systematic way of doing that (using SymPy
in Julia):
using SymPy
I = t^(2n) * exp(-x * t^2)
order = 2 # The order of the final polynomial.
I_taylor = series(I, x, 0, order)
B = SymPy.integrate(I_taylor, (t, 0, 1)) |> simplify
$$B^2_n(x) = \frac{3 + 2n - (2n + 1) x}{(2n + 1) (2n + 3)} + O(x^2)$$
Such approximations can be used for small values of $x$ (they are safer for $x \approx 0$ than some implementations above). An approximation of order 10 is compared below with a quadrature of the Boys function.
Large values $x$
Since the Boys function is decreasing, we could, for large $x$, approximate
$$\int_0^1 t^{2n} e^{-x t^2} \approx \int_0^\infty t^{2n} e^{-x t^2}
= \frac{1}{2x^{\frac{1}{2} + n}} \Gamma \left( \frac{1}{2} + n \right)
= \frac{(2n - 1)!!}{2^{n + 1}} \sqrt{\frac{\pi}{x^{2n + 1}}}$$
(I learned this trick here.) Compare this to the Boys function below.
Further reading
The above are obviously toy examples. Here are some nice reads: