Elliptic integrals and Jacobian elliptic functions
| Problem | Evaluate the complete and incomplete elliptic integrals of the first, second and third kinds, Carlson’s symmetric integrals, and the Jacobian elliptic functions , , |
| Devised by | Giulio Fagnano (1718) and Leonhard Euler (1750s); John Landen (1775), the transformation; Carl Friedrich Gauss (1799), the arithmetic-geometric mean; Adrien-Marie Legendre (1825), the three kinds; Niels Henrik Abel and Carl Gustav Jacobi (1827–1829), elliptic functions; Bille Carlson (1979), the symmetric forms and their duplication algorithm |
| Accuracy | About relative; for the first and third kinds very close to the logarithmic singularity at |
| Cost | Carlson’s duplication: about 6 to 10 iterations of a few square roots; Jacobian functions: an AGM of about 5 steps |
| Read first | Preliminaries: floating-point arithmetic |
| API | elliptic_integral, elliptic_integral_second, elliptic_integral_third, elliptic_f, elliptic_e, elliptic_pi, carlson_rf, carlson_rd, carlson_rj, carlson_rc, jacobian_elliptic |
Overview
Elliptic integrals are integrals of rational functions involving the square root of a cubic or quartic polynomial. They got their name from the arc length of an ellipse, and they give the period of a pendulum swinging through a large angle, the shape of a bent elastic rod, the inductance of coils, the field of a current loop, and the geodesics on an ellipsoid. Inverting them, as the sine inverts , gives the Jacobian elliptic functions , , , doubly periodic generalisations of the trigonometric functions that describe the motion of a pendulum and of a rigid body, nonlinear waves (cnoidal waves) and elliptic filters.
Where it is used
Elliptic integrals measure curved things exactly.
- Pendulums. The period of a pendulum swinging through a large angle is a complete elliptic integral.
- Geometry. The circumference of an ellipse, and arc lengths of many curves.
- Electromagnetics. The magnetic field and inductance of circular loops and coils.
- Filters. Elliptic (Cauer) filters are designed with Jacobian elliptic functions, giving the sharpest cutoff for a given order.
- Mechanics. Motion of a rigid body and large-amplitude oscillators.
History
Giulio Fagnano (1718) found that arcs of the lemniscate could be doubled by algebraic operations, and Euler (1750s) generalised this into the addition theorem for elliptic integrals. John Landen (1775) discovered the transformation that relates integrals of different moduli, and Gauss (1799) found that the arithmetic-geometric mean computes the complete integral of the first kind: , one of the fastest algorithms in all of mathematics. Legendre spent forty years on elliptic integrals and reduced them all to three standard kinds (1825-1828), just before Niels Henrik Abel and Carl Gustav Jacobi revolutionised the subject by inverting them, creating the theory of elliptic functions.
For computation, Bille Carlson (1979) replaced Legendre’s forms by symmetric integrals that can all be computed by one simple, provably convergent duplication algorithm. His method underlies the elliptic integrals of most modern libraries, including this one.
The idea
Elliptic integrals
Legendre’s forms. Legendre showed that every elliptic integral can be reduced to three standard ones. With modulus (some authors use the parameter instead) and amplitude , the integrals of the first, second and third kinds are
When the upper limit is they are called complete: , , . For example, the perimeter of an ellipse with semi-axes is with , and the period of a pendulum of length released from angle is .
At the square roots are 1 and both and reduce to . As the integrand of becomes , which is not integrable at : diverges logarithmically, , while .
Figure 6.8. Complete elliptic integrals.
Carlson’s symmetric forms. Legendre’s forms are traditional but awkward to compute: each needs its own method and its own special cases. Carlson proposed a different set of standard integrals,
and similarly (for the third kind) and the degenerate case , which is an elementary function. is symmetric: interchanging , and does not change it. Legendre’s integrals are simple combinations of Carlson’s, for example
The duplication theorem. The reward for symmetry is one algorithm that computes all of them. With ,
The value is unchanged, but the arguments are different. Adding the same to each leaves their differences as they were, and dividing by 4 then shrinks the differences by a factor of 4. Repeating the step drives the three arguments together.
Why does that help? Because when the three arguments are equal the integral is elementary: . Once the arguments nearly agree, is for their mean , times a short Taylor series in their small relative deviations from the mean. A handful of duplications followed by that series gives full precision. The same idea computes and .
Amplitudes beyond are reduced by periodicity of the integrand: , and similarly for and .
Jacobian elliptic functions
The inverse sine can be defined by an integral, , and the sine is then the inverse of that integral. Jacobi’s functions arise in the same way from the elliptic integral of the first kind. If , then is a function of , and the Jacobian elliptic functions are
with . They generalise the trigonometric functions, to which they reduce at (where ): , , . As increases by , increases by , so and have period in place of ; has period . At the other extreme, , the period is infinite and the functions become , and . The motion of a pendulum released at angle is with :
Figure 6.9. Jacobian elliptic functions.
They are computed by the descending Landen transformation, which relates elliptic functions of parameter to those of a smaller parameter. Applied repeatedly, it drives the parameter to zero, where the functions are trigonometric. In practice the iteration is the arithmetic-geometric mean: starting from and , replace the pair by its arithmetic mean and its geometric mean , and repeat. The two sequences converge to a common limit quadratically, in about five steps. The amplitude is then recovered by running back through the steps with the recursion , where , and finally .
Accuracy
Table 6.5. Accuracy of the elliptic integrals and functions.
| function | test points | median relative error | largest relative error | at arguments |
|---|---|---|---|---|
elliptic_integral | 6 | 1e-16 | 7e-13 | (0.999999) |
elliptic_integral_second | 6 | 2e-16 | 3e-15 | (0.999999) |
elliptic_integral_third | 18 | 2e-16 | 9e-13 | (0.9, 0.999999) |
elliptic_f | 30 | 1e-16 | 7e-13 | (3, 0.999999) |
elliptic_e | 30 | 1e-16 | 3e-15 | (3, 0.999999) |
elliptic_pi | 30 | 1e-16 | 7e-13 | (3, 0.3, 0.999999) |
jacobian_elliptic sn | 33 | 0 | 4e-15 | (5, 0.9) |
jacobian_elliptic cn | 30 | 0 | 8e-15 | (5, 0.999) |
jacobian_elliptic dn | 33 | 0 | 8e-16 | (-3, 0.999) |
The errors of at come from the logarithmic singularity: is formed with a relative error near , and depends on it logarithmically.
The table includes points at the quarter period , where the formula of Abramowitz and Stegun fails. Both factors vanish there, and the quotient is 0.2418 instead of 0.2236 for , giving spikes at every minimum of dn. The library uses the identity above, a sum of non-negative terms.
Algorithm
Carlson’s .
- Compute and replace each argument by .
- Compute the mean of the three arguments and their relative deviations from it.
- If the largest deviation exceeds about , go to 1.
- Return times a short polynomial in the deviations (the first terms of the Taylor series about equal arguments).
The neglected terms of the series are of the sixth order in the deviations, so a tolerance of gives an error near . Each pass divides the deviations by 4, so about a dozen passes suffice at most. , and follow the same loop, with their own series, and and also accumulate a sum during the duplications.
Legendre’s , and .
- Reduce the amplitude: write with .
- Evaluate the integral for as a combination of Carlson integrals, as in the formula for above.
- Add times the complete integral.
Jacobi’s , , .
- For within rounding of 0 or 1, use the short series about the trigonometric or hyperbolic limit.
- Otherwise run the arithmetic-geometric mean from , , storing and at each step, until is negligible.
- Set and recur backwards, , down to .
- Return , and .
Implementation
Carlson’s by duplication:
/// Carlson's elliptic integral of the first kind `R_F(x, y, z)`, for non-negative arguments with
/// at most one equal to zero.
pub fn carlson_rf(x: f64, y: f64, z: f64) -> f64 {
const ERRTOL: f64 = 0.001;
if x.is_nan() || y.is_nan() || z.is_nan() || x < 0.0 || y < 0.0 || z < 0.0 {
return f64::NAN;
}
if (x == 0.0) as u8 + (y == 0.0) as u8 + (z == 0.0) as u8 > 1 {
return f64::NAN;
}
let (mut xt, mut yt, mut zt) = (x, y, z);
let (mut delx, mut dely, mut delz, mut ave);
loop {
let (sx, sy, sz) = (xt.sqrt(), yt.sqrt(), zt.sqrt());
let alamb = sx * (sy + sz) + sy * sz;
xt = 0.25 * (xt + alamb);
yt = 0.25 * (yt + alamb);
zt = 0.25 * (zt + alamb);
ave = (xt + yt + zt) / 3.0;
delx = (ave - xt) / ave;
dely = (ave - yt) / ave;
delz = (ave - zt) / ave;
if delx.abs().max(dely.abs()).max(delz.abs()) <= ERRTOL {
break;
}
}
let e2 = delx * dely - delz * delz;
let e3 = delx * dely * delz;
(1.0 + (e2 / 24.0 - 0.1 - 3.0 * e3 / 44.0) * e2 + e3 / 14.0) / ave.sqrt()
}
The Jacobian elliptic functions:
/// The Jacobian elliptic functions `(sn(u | m), cn(u | m), dn(u | m))` with parameter `m = k²`
/// for `0 <= m <= 1`.
///
/// Computed with the descending Landen / arithmetic-geometric mean scheme (Abramowitz & Stegun
/// 16.4), with series fallbacks at the endpoints `m → 0` and `m → 1`. Returns `NaN`s for `m`
/// outside `[0, 1]`.
pub fn jacobian_elliptic(u: f64, m: f64) -> (f64, f64, f64) {
const EPS: f64 = 1e-16;
if u.is_nan() || m.is_nan() || !(0.0..=1.0).contains(&m) {
return (f64::NAN, f64::NAN, f64::NAN);
}
if m < 1e-9 {
let (s, c) = u.sin_cos();
let t = 0.25 * m * (u - s * c);
return (s - t * c, c + t * s, 1.0 - 0.5 * m * s * s);
}
if m > 1.0 - 1e-9 {
let m1 = 1.0 - m;
let (t, sech) = (u.tanh(), 1.0 / u.cosh());
let shch = u.sinh() * u.cosh();
return (
t + 0.25 * m1 * (shch - u) * sech * sech,
sech - 0.25 * m1 * (shch - u) * t * sech,
sech + 0.25 * m1 * (shch + u) * t * sech,
);
}
let mut a = vec![1.0];
let mut c = vec![m.sqrt()];
let mut b = (1.0 - m).sqrt();
while c.last().unwrap().abs() > EPS && a.len() < 40 {
let an = *a.last().unwrap();
let cn = 0.5 * (an - b);
let anext = 0.5 * (an + b);
b = (an * b).sqrt();
a.push(anext);
c.push(cn);
}
let n = a.len() - 1;
let mut phi = (1u64 << n.min(62)) as f64 * a[n] * u;
for i in (1..=n).rev() {
// t = 2 φ_{i-1} - φ_i satisfies sin t = (c_i / a_i) sin φ_i and |t| < π/2.
let t = (c[i] / a[i] * phi.sin()).asin();
phi = 0.5 * (phi + t);
}
let sn = phi.sin();
let cn = phi.cos();
// dn² = 1 − m sn² = (1 − m) + m cn², a sum of non-negative terms. (The A&S formula
// dn = cn / cos(φ₁ − φ₀) is 0/0 at u = K, where it gave 0.2418 instead of 0.2236 for m = 0.95.)
let dn = ((1.0 - m) + m * cn * cn).sqrt();
(sn, cn, dn)
}
- The Legendre forms take the modulus , the Jacobian functions the parameter , as in Abramowitz
and Stegun. Check the convention when comparing with other software: SciPy’s
ellipktakes . - Principal-value cases of the third kind ( changing sign) are not supported and give
NaN.
In practice
//! Elliptic integrals and Jacobian elliptic functions: the period and motion of a pendulum.
//!
//! Run with `cargo run -p lib_jsl_special_functions --example special_elliptic`.
use lib_jsl_special_functions::{carlson_rf, elliptic_e, elliptic_integral, elliptic_integral_second, jacobian_elliptic};
fn main() {
// A pendulum of length L released from angle θ₀ has period 4 √(L/g) K(sin(θ₀/2)).
let (l, g) = (1.0f64, 9.81f64);
for degrees in [5.0f64, 45.0, 90.0, 170.0] {
let k = (degrees.to_radians() / 2.0).sin();
let period = 4.0 * (l / g).sqrt() * elliptic_integral(k);
println!("θ₀ = {degrees:>5}°: period {period:.6} s (small-angle value {:.6} s)", 2.0 * std::f64::consts::PI * (l / g).sqrt());
}
// Its angle at time t: sin(θ/2) = k sn(K − t √(g/L) | k²).
let k = (90f64.to_radians() / 2.0).sin();
let quarter = elliptic_integral(k);
for t in [0.0, 0.25, 0.5] {
let (sn, _, _) = jacobian_elliptic(quarter - t * (g / l).sqrt(), k * k);
println!("t = {t}: θ = {:.4}°", (2.0 * (k * sn).asin()).to_degrees());
}
// The perimeter of an ellipse with semi-axes a ≥ b is 4a E(e), e the eccentricity.
let (a, b) = (2.0f64, 1.0f64);
let e = (1.0 - (b / a).powi(2)).sqrt();
println!("perimeter of the 2 × 1 ellipse: {:.12}", 4.0 * a * elliptic_integral_second(e));
println!("E(π/3, 0.5) = {:.15}, R_F(1, 2, 0) = {:.15}", elliptic_e(std::f64::consts::FRAC_PI_3, 0.5), carlson_rf(1.0, 2.0, 0.0));
}
Strengths
- One uniform, provably convergent algorithm for all three kinds.
- Fast: a handful of square roots per evaluation.
Weaknesses
- Real arguments only; no complex modulus or Weierstrass functions.
- The modulus/parameter conventions differ between sources and must be checked.
Exercises
- Show that the period of a pendulum of amplitude is , and expand it to second order in .
- Compute by Gauss’s AGM formula and compare with
elliptic_integral. How many AGM steps are needed for full precision? - Show that the perimeter of an ellipse with semi-axes is with , and compare with Ramanujan’s approximation .
- Verify and numerically, and that .
- Prove the duplication theorem for by a change of variable.
Hints and answers
- For the pendulum , energy conservation gives with after the substitution . Since and , .
- . The AGM converges quadratically, so five or six steps reach full double precision except very close to .
- Parametrise the ellipse as : the arc length element is , and integrating over the quarter turn gives . Ramanujan’s approximation is accurate to about relative or better for moderate eccentricity and degrades for very elongated ellipses.
- Compute , , at several and and check the identities to rounding; for the derivative use a central difference with .
- Since , the theorem reads with . Write , : then and . Substituting turns the right-hand side into , which is twice the integral over of the left-hand integrand’s half; so the claim is that and of are equal. Check that numerically first, then look for the substitution that exchanges the two intervals.
Further reading
- B. C. Carlson, “Computing elliptic integrals by duplication”, Numerische Mathematik 33 (1979) 1–16, and “Numerical computation of real or complex elliptic integrals”, Numerical Algorithms 10 (1995) 13–26.
- J. M. Borwein and P. B. Borwein, Pi and the AGM, Wiley, 1987.
- NIST Digital Library of Mathematical Functions, chapters 19 and 22, https://dlmf.nist.gov/19.
- M. Abramowitz and I. A. Stegun, Handbook of Mathematical Functions, chapters 16 and 17, 1964.