#square-root #no-alloc #product-log #omega-function

no-std lambert_w

Fast and accurate evaluation of the Lambert W function by the method of T. Fukushima.

30 releases (6 stable)

new 1.0.5 Dec 4, 2024
1.0.4 Nov 23, 2024
1.0.3 Oct 28, 2024
0.5.9 Oct 5, 2024
0.4.0 Jul 31, 2024

#83 in Math

Download history 66/week @ 2024-08-17 103/week @ 2024-08-24 596/week @ 2024-08-31 426/week @ 2024-09-07 288/week @ 2024-09-14 258/week @ 2024-09-21 150/week @ 2024-09-28 496/week @ 2024-10-05 415/week @ 2024-10-12 398/week @ 2024-10-19 575/week @ 2024-10-26 349/week @ 2024-11-02 438/week @ 2024-11-09 480/week @ 2024-11-16 806/week @ 2024-11-23 492/week @ 2024-11-30

2,281 downloads per month
Used in 12 crates (3 directly)

MIT/Apache

94KB
2K SLoC

lambert_w

Crates.io Version docs.rs Static Badge GitHub Actions Workflow Status codecov

Fast and accurate evaluation of the real valued parts of the principal and secondary branches of the Lambert W function with the method of Toshio Fukushima [1].

This method works by splitting the domain of the function into subdomains, and on each subdomain it uses a rational function evaluated on a simple transformation of the input to describe the function.
It is implemented in code as conditional switches on the input value followed by either a square root (and possibly a division) or a logarithm and then a series of multiplications and additions by fixed constants and finished with a division.

The crate provides two approximations of each branch, one with 50 bits of accuracy (implemented on 64-bit floats) and one with 24 bits (implemented on 32- and 64-bit floats). The one with 50 bits of accuracy uses higher degree polynomials in the rational functions compared to the one with only 24 bits, and thus more of the multiplications and additions by constants.

This crate can evaluate the approximation with 24 bits of accuracy on 32-bit floats, even though it is defined on 64-bit floats in Fukushima's paper. This may result in a reduction in the accuracy to less than 24 bits, but this reduction has not been quantified by the author of this crate.

The crate is no_std compatible, but can optionally depend on the standard library through features for a potential performance gain.

Examples

Compute the value of the omega constant with the principal branch of the Lambert W function:

use lambert_w::lambert_w0;

let Ω = lambert_w0(1.0);

assert_abs_diff_eq!(Ω, 0.5671432904097839);

Evaluate the secondary branch of the Lambert W function at -ln(2)/2:

use lambert_w::lambert_wm1;

let mln4 = lambert_wm1(-f64::ln(2.0) / 2.0);

assert_abs_diff_eq!(mln4, -f64::ln(4.0));

Do it on 32-bit floats:

use lambert_w::{lambert_w0f, lambert_wm1f};

let Ω = lambert_w0f(1.0);
let mln4 = lambert_wm1f(-f32::ln(2.0) / 2.0);

assert_abs_diff_eq!(Ω, 0.56714329);
assert_abs_diff_eq!(mln4, -f32::ln(4.0));

The implementation can handle extreme inputs just as well:

use lambert_w::{lambert_w0, lambert_wm1};

let big = lambert_w0(f64::MAX);
let tiny = lambert_wm1(-1e-308);

assert_relative_eq!(big, 703.2270331047702, max_relative = 4e-16);
assert_relative_eq!(tiny, -715.7695669234213, max_relative = 4e-16);

Importing the LambertW trait lets you call the functions with postfix notation:

use lambert_w::LambertW;

let z = 2.0 * f64::ln(2.0);

assert_abs_diff_eq!(z.lambert_w0(), f64::ln(2.0));

Features

One of the below features must be enabled:

libm (enabled by default): if the std feature is disabled, this feature uses the libm crate to compute square roots and logarithms during function evaluation instead of the standard library.

std: use the standard library to compute square roots and logarithms for a potential performance gain. When this feature is disabled the crate is no_std compatible.

References

[1]: Toshio Fukushima. Precise and fast computation of Lambert W function by piecewise minimax rational function approximation with variable transformation. DOI: 10.13140/RG.2.2.30264.37128. November 2020.

⬆️ Back to top.


License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~390KB