Exponential, sine, cosine and Fresnel integrals
| Problem | Evaluate the exponential integrals , and , the sine and cosine integrals and , and the Fresnel integrals and |
| Devised by | Exponential and logarithmic integrals: Leonhard Euler (1768), Johann von Soldner (1809); sine and cosine integrals: Euler, Lorenzo Mascheroni (1790); Fresnel integrals: Euler (1744), Augustin-Jean Fresnel (1818); the Cornu spiral: Marie Alfred Cornu (1874) |
| Accuracy | About relative, for the Fresnel integrals at very large arguments |
| Cost | A power series for small arguments, a (complex) continued fraction or asymptotic series for large ones |
| Read first | Incomplete gamma and beta functions and the error function |
| API | exponential_integral, exponential_integral_en, exponential_integral_ei, sine_integral, sine_integral_shifted, cosine_integral, fresnel_integrals |
Overview
Integrating an exponential, sine or cosine divided by its argument, or of the square of the argument, gives functions that cannot be written in closed form but appear constantly. The exponential integrals arise in radiative transfer, groundwater flow (the Theis well function), and number theory, where the logarithmic integral counts the primes. The sine and cosine integrals describe the Gibbs phenomenon, the radiation of antennas and the response of ideal filters. The Fresnel integrals describe the diffraction of light at an edge, and the clothoid curves of road and railway design.
All of them are related through the exponential integral of a complex argument, which is how the library computes them.
Where it is used
These integrals appear when exponentials are divided by powers.
- Radiative transfer and heat conduction. The exponential integrals occur in attenuation of radiation through slabs and in transient conduction (the Theis well function in hydrology is ).
- Diffraction and optics. Fresnel integrals give the intensity pattern of light and radio waves near the edge of an obstacle.
- Road and rail design. Euler spirals (clothoids) for smooth transitions use Fresnel integrals.
- Signal processing. The sine integral gives the Gibbs overshoot of a truncated Fourier series.
History
Euler introduced the logarithmic and exponential integrals in his Institutiones calculi integralis (1768). Johann von Soldner, a Bavarian astronomer, tabulated the logarithmic integral in 1809 (it is sometimes called Soldner’s integral), and Gauss and Legendre conjectured that it approximates the number of primes below , which became the prime number theorem (1896). Lorenzo Mascheroni studied the sine and cosine integrals in 1790, computing Euler’s constant to 32 digits on the way (19 of them correct).
Euler met the Fresnel integrals in 1744 while studying elastic curves. Augustin-Jean Fresnel used them in 1818 to explain diffraction by the wave theory of light, and Marie Alfred Cornu (1874) drew the spiral that bears his name, an indispensable graphical tool for diffraction calculations before computers.
The idea
Exponential integrals
The integrals , and cannot be expressed through elementary functions, yet they arise constantly, so they are given names and treated as functions in their own right. For the exponential integrals are
The first two are related ( with is , after the substitution ). The third is defined for all real ; “PV” denotes a principal value, which means that for the singularity of the integrand at is approached symmetrically from both sides, so that the infinite contributions cancel. For negative arguments it reduces to the first: .
The computational pattern is the same as for the incomplete gamma function: a power series for small arguments, and a continued fraction or an asymptotic series for large ones.
Small . Expanding in its Taylor series and integrating term by term gives
with the Euler-Mascheroni constant. The term describes how grows without bound as . The series converges for every , but that does not make it usable for every . For large the true value is tiny, , while the individual terms are large and alternate in sign. The small result emerges only through cancellation of large numbers, and the rounding errors of those numbers swamp it.
Large . For the library uses the continued fraction
evaluated by the modified Lentz algorithm. The factor carries the smallness explicitly, so there is no cancellation.
for positive . Its power series is . All the terms are positive, so there is no cancellation and the series can be used for moderate . For large it needs too many terms, and the library switches to the asymptotic series (see Stirling’s series for what “asymptotic” means).
Sine and cosine integrals
The integrand is finite at , so is defined directly. The integrand is not, which is why is defined with the divergent part separated out and the well-behaved remainder integrated. is odd and tends to as . tends to 0, oscillating, as , and to logarithmically as .
Both are the exponential integral in disguise. Since , an exponential integral with an imaginary argument has a cosine integral as its real part and a sine integral as its imaginary part:
So no new method is needed. For large the library evaluates the continued fraction for in complex arithmetic and separates the real and imaginary parts, and for small it uses the power series. The value is the size of the overshoot of a Fourier series at a jump, the Gibbs phenomenon.
Figure 6.4. Sine, cosine and exponential integrals.
Fresnel integrals
Both tend to as , oscillating with decreasing amplitude. Plotting gives the Cornu spiral, or clothoid, a curve whose curvature grows linearly with arc length, which is why it joins straight track to circular curves smoothly:
Figure 6.5. The Cornu spiral.
is an error function of a complex argument, . The library uses the power series for small and, for large , a complex continued fraction for .
Accuracy
Table 6.3. Accuracy of the exponential, sine, cosine and Fresnel integrals.
| function | test points | median relative error | largest relative error | at arguments |
|---|---|---|---|---|
exponential_integral | 19 | 2e-16 | 2e-15 | (2.5) |
exponential_integral_en | 30 | 1e-16 | 2e-15 | (2, 5) |
exponential_integral_ei | 28 | 2e-16 | 1e-15 | (-1.5) |
sine_integral | 28 | 0 | 2e-16 | (-5.5) |
cosine_integral | 19 | 5e-16 | 6e-15 | (3.7) |
fresnel_integrals | 28 | 2e-16 | 6e-15 | (170.5) |
fresnel_integrals | 28 | 1e-16 | 4e-14 | (500) |
Algorithm
Every function chooses between a series, used for small arguments, and a continued fraction or asymptotic series, used for large ones.
, :
- : return .
- : evaluate the continued fraction by the modified Lentz algorithm and multiply by .
- Otherwise: sum the power series (for , the series with shown above) until a term is below times the sum.
:
- : return .
- : sum the power series until a term is below times the sum.
- Larger : sum the asymptotic series , stopping when a term is below , or at the smallest term if the terms begin to grow first. Beyond the switching point the smallest term, about , is already below rounding error, so the asymptotic series gives full precision there.
and : below a threshold, sum their power series; above it, evaluate the continued fraction for in complex arithmetic and take and . is extended to negative as an odd function.
Fresnel and : below a threshold, the power series; above it, a complex continued fraction for , from which both follow as real and imaginary parts. Both are odd functions.
Implementation
/// The generalized exponential integral Eₙ(x) = ∫₁^∞ e^(-xt)/tⁿ dt, for `x >= 0`.
pub fn exponential_integral_en(n: u32, x: f64) -> f64 {
if x.is_nan() || x < 0.0 {
return f64::NAN;
}
if n == 0 {
return (-x).exp() / x;
}
let nm1 = (n - 1) as i64;
if x == 0.0 {
return if nm1 > 0 { 1.0 / nm1 as f64 } else { f64::INFINITY };
}
if x > 1.0 {
// Continued fraction (modified Lentz).
let mut b = x + n as f64;
let mut c = 1.0 / FPMIN;
let mut d = 1.0 / b;
let mut h = d;
for i in 1..=10_000 {
let a = -(i as f64) * (nm1 as f64 + i as f64);
b += 2.0;
d = 1.0 / (a * d + b);
c = b + a / c;
let del = c * d;
h *= del;
if (del - 1.0).abs() < EPS {
break;
}
}
h * (-x).exp()
} else {
// Power series.
let mut ans = if nm1 != 0 { 1.0 / nm1 as f64 } else { -x.ln() - EULER };
let mut fact = 1.0;
for i in 1..=1000i64 {
fact *= -x / i as f64;
let del = if i != nm1 {
-fact / (i - nm1) as f64
} else {
let mut psi = -EULER;
for ii in 1..=nm1 {
psi += 1.0 / ii as f64;
}
fact * (-x.ln() + psi)
};
ans += del;
if del.abs() < ans.abs() * EPS {
break;
}
}
ans
}
}
sine_integral_shiftedreturns , accurate for large where itself is close to .fresnel_integrals(x)returns the pair(S(x), C(x)), with the convention.
In practice
//! Exponential, sine, cosine and Fresnel integrals.
//!
//! Run with `cargo run -p lib_jsl_special_functions --example special_exponential_integrals`.
use lib_jsl_special_functions::{cosine_integral, exponential_integral, exponential_integral_ei, exponential_integral_en, fresnel_integrals, sine_integral};
fn main() {
println!("E₁(1) = {:.15}, E₃(2) = {:.15}", exponential_integral(1.0), exponential_integral_en(3, 2.0));
// The logarithmic integral li(x) = Ei(ln x) approximates the number of primes below x.
let x: f64 = 1e6;
println!("li(10⁶) = Ei(ln 10⁶) = {:.1} (78498 primes below 10⁶)", exponential_integral_ei(x.ln()));
// The Gibbs overshoot of a Fourier series: (2/π) Si(π) = 1.1790.
println!("(2/π) Si(π) = {:.10}", 2.0 / std::f64::consts::PI * sine_integral(std::f64::consts::PI));
println!("Ci(1) = {:.15}", cosine_integral(1.0));
// Fresnel integrals: the diffraction pattern at a straight edge, at t = 1.
let (s, c) = fresnel_integrals(1.0);
println!("S(1) = {s:.15}, C(1) = {c:.15}");
}
Strengths
- Full accuracy across the whole real line, from one continued-fraction engine.
Weaknesses
- Real arguments only (the complex exponential integral is computed internally but not exposed).
Exercises
- Show that , and explain why this recurrence is unstable upward for .
- Derive the asymptotic series of by repeated integration by parts, and find the term at which it should be truncated for .
- Show that , and compare with the number of primes below for .
- Prove that the curvature of the Cornu spiral at parameter is .
- Compute the Gibbs overshoot by summing the Fourier series of a square wave to terms, and compare its maximum with .
Hints and answers
- Integrate by parts to get . Each upward step multiplies the error in by , which exceeds 1 when , so errors grow.
- Repeated integration by parts gives . The terms decrease until , so truncate near for ; the smallest term, , is the accuracy limit.
- Substitute in to get . Compare with : the ratio tends to 1 slowly and is always slightly larger in this range.
- For the Cornu spiral , the speed is and the tangent angle is , so the curvature is .
- The partial sums of the square-wave series overshoot near the jump by about (an overshoot of about 9% of the jump height 2), independent of .
Further reading
- W. Gautschi and W. F. Cahill, “Exponential integral and related functions”, in M. Abramowitz and I. A. Stegun (eds.), Handbook of Mathematical Functions, chapter 5, 1964.
- NIST Digital Library of Mathematical Functions, chapters 6 and 7, https://dlmf.nist.gov/6.
- W. H. Press, S. A. Teukolsky, W. T. Vetterling and B. P. Flannery, Numerical Recipes, 3rd ed., Cambridge University Press, 2007, §6.3 and §6.8–6.9.