Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The Lambert W function

ProblemSolve for : the real branches and , every complex branch , and the related equation
Devised byJohann Heinrich Lambert (1758) and Leonhard Euler (1783); named, and its theory and computation systematised, by Robert Corless, Gaston Gonnet, D. E. G. Hare, David Jeffrey and Donald Knuth (1996)
AccuracyAbout relative, a few units of near the branch point
CostAn initial approximation and two or three Halley iterations
Read firstNewton’s method and the secant method
APIlambert_w0, lambert_w_m1, lambert_w, inverse_x_log_x, inverse_x_log_x_lower

Overview

The Lambert W function is the inverse of . Like the logarithm, which inverts , it turns a whole class of transcendental equations into closed forms: any equation in which the unknown appears both in and out of an exponential or logarithm, such as , or , can be solved with W. It appears in the solution of delay differential equations, in enzyme kinetics (Michaelis-Menten), in Wien’s displacement law, in the analysis of algorithms (the height of random trees), in combustion and in the current through a diode with a series resistance.

Where it is used

The Lambert W function solves , which turns up whenever a variable appears both in an exponent and outside.

  • Delay and population models. The stability of delay-differential equations in control and ecology is decided by the roots of equations solved by .
  • Electronics. The current of a diode with a series resistor, and the output of a photovoltaic cell model, involve .
  • Physics. Wien’s displacement law for blackbody radiation, , is solved with .
  • Inverting a cost. Solving for the largest problem size that fits a time budget gives .

History

Johann Heinrich Lambert studied the trinomial equation in 1758 and found a series solution; Euler (1783) transformed it into the form now associated with W. The function reappeared repeatedly over two centuries under different names, until Robert Corless, Gaston Gonnet, D. E. G. Hare, David Jeffrey and Donald Knuth, having implemented it in the computer algebra system Maple, wrote the 1996 paper “On the Lambert W function” that named it, collected its properties and applications, and specified its branches. It has since become a standard function in mathematical software.

The idea

The Lambert W function

The Lambert W function is the inverse of : is the number for which . To see what this inverse looks like, consider the graph of . Its derivative is negative for and positive for , so falls from 0 (as ) to a minimum of at , and then rises without bound. Three conclusions follow for real . There is no solution for . For there is exactly one. And for there are two, one on each side of . The inverse therefore has two real branches:

  • the principal branch , defined for , increasing, with and ;
  • the lower branch , defined for , decreasing to as .

The two meet at the branch point , , where behaves like a square root: with (and for the lower branch).

Figure 6.15. The real branches of the Lambert W function.

In the complex plane there are infinitely many branches , . For large they behave like with .

Computing W. There is no formula for in terms of elementary functions, so it is computed as the root of . Like any iterative root finder, this needs a good starting point and a fast iteration. The library starts from the branch-point series near , from the asymptotic expansion for large , and from simple approximations elsewhere. It then refines the estimate with Halley’s method, which extends Newton’s method by using the second derivative as well as the first; here both are cheap, since they involve only :

which converges cubically, so two or three iterations reach machine precision.

The inverse of x ln x

The equation becomes , so and . For there is one solution, ; for there are two, from the two real branches, one in and one in . inverse_x_log_x returns the one with , inverse_x_log_x_lower the other.

Accuracy

Table 6.10. Accuracy of the Lambert W function.

functiontest pointsmedian relative errorlargest relative errorat arguments
lambert_w02408e-16(-0.3678)
lambert_w_m1503e-15(-0.3678)
lambert_w (complex), real part1504e-16(0, -1, 1)
lambert_w (complex), imaginary part1100(1, 100000, 0)
inverse_x_log_x500(100)
inverse_x_log_x_lower32e-162e-16(-0.36)

Algorithm

lambert_w0(y), the principal branch for real :

  1. : return NaN. : return 0.
  2. Starting value. Near the branch point , use the series in , which is already accurate there (and where the iteration below would converge slowly, since the derivative is infinite at the branch point). For moderate use . For large use with .
  3. Halley iteration. Compute and update by the formula above. Stop when the change in is below rounding error; two or three iterations are enough.

lambert_w_m1(y), the lower branch, is defined only for . It follows the same three steps, with the branch-point series taken with and a starting value below elsewhere.

lambert_w(k, z), branch for complex : choose the starting value according to the branch and the region ( with away from the origin and the branch point, the branch-point series near for the branches that meet there), and run the same Halley iteration in complex arithmetic.

inverse_x_log_x(y): compute on the appropriate branch and return .

Implementation

/// The principal branch W₀(y) of the Lambert W function, for `y >= -1/e`.
pub fn lambert_w0(y: f64) -> f64 {
    if y.is_nan() || y < -INV_E {
        return f64::NAN;
    }
    if y == f64::INFINITY {
        return f64::INFINITY;
    }
    if y == 0.0 {
        return 0.0;
    }
    let guess = if y < -0.25 {
        // Series about the branch point.
        let p = (2.0 * (std::f64::consts::E * y + 1.0)).max(0.0).sqrt();
        -1.0 + p - p * p / 3.0 + 11.0 / 72.0 * p * p * p
    } else if y < 3.0 {
        let l = (1.0 + y).ln();
        l * (1.0 - l.ln_1p() / (2.0 + l))
    } else {
        let l = y.ln();
        l - l.ln() + l.ln() / l
    };
    halley(y, guess)
}
  • The complex branch cut of is ; is real on , matching lambert_w_m1.
  • For real arguments the real functions are faster than the complex one.

In practice

//! The Lambert W function.
//!
//! Run with `cargo run -p lib_jsl_special_functions --example special_lambert_w`.

use lib_jsl_special_functions::{inverse_x_log_x, lambert_w, lambert_w0, lambert_w_m1};
use num_complex::Complex;

fn main() {
    // The omega constant Ω = W(1), with Ω e^Ω = 1.
    let omega = lambert_w0(1.0);
    println!("Ω = W₀(1) = {omega:.15}; Ω e^Ω = {:.15}", omega * omega.exp());
    // Both real solutions of x eˣ = −0.2.
    println!("W₀(−0.2) = {:.15}, W₋₁(−0.2) = {:.15}", lambert_w0(-0.2), lambert_w_m1(-0.2));
    // Solving x ln x = 10, and 2ˣ = 5x (x = −W(−ln 2 / 5) / ln 2).
    println!("x ln x = 10 at x = {:.15}", inverse_x_log_x(10.0));
    let l = 2f64.ln();
    for branch in [lambert_w0, lambert_w_m1] {
        let x = -branch(-l / 5.0) / l;
        println!("2^x = 5x at x = {x:.12} (check: {:.3e})", 2f64.powf(x) - 5.0 * x);
    }
    // Complex branches.
    let w = lambert_w(1, Complex::new(1.0, 0.0));
    println!("W₁(1) = {:.12} {:+.12}i", w.re, w.im);
}

Strengths

  • Closed-form solutions to a wide class of transcendental equations.
  • Machine precision on every branch, including near the branch point.

Weaknesses

  • Solving an equation with W often needs some algebra to put it into the form .

Exercises

  1. Solve with W. (Hint: take logarithms.)
  2. Show that , and use it to derive Halley’s iteration.
  3. Wien’s displacement law requires solving . Show that and evaluate it.
  4. Find all real solutions of using both real branches, as in the example, and check them.
  5. Plot for at in the complex plane. How do the branches approach ?
Hints and answers
  1. . With , , so and .
  2. Differentiating gives , so . Halley’s method on uses and .
  3. Put : gives , i.e. , so . The value is (so ); the other root of the original equation is (the branch).
  4. has two real solutions, and , one from each real branch of (put and use and ).
  5. As the branch is approximately ; the difference from is , which becomes relatively small as grows (at the agreement is only rough).

Further reading

  • R. M. Corless, G. H. Gonnet, D. E. G. Hare, D. J. Jeffrey and D. E. Knuth, “On the Lambert W function”, Advances in Computational Mathematics 5 (1996) 329–359.
  • F. Johansson, “Computing the Lambert W function in arbitrary-precision complex interval arithmetic”, Numerical Algorithms 83 (2020) 221–242.
  • NIST Digital Library of Mathematical Functions, §4.13, https://dlmf.nist.gov/4.13.