## no-std lambert_w

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

### 23 releases(4 breaking)

 new 0.5.8 Sep 7, 2024 Sep 6, 2024 Aug 9, 2024 Aug 2, 2024 Jul 28, 2024

#152 in Math

Used in 7 crates (3 directly)

MIT/Apache

99KB
2K SLoC

# lambert_w

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.

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 and one with 24 bits. 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 also evaluate the approximation with 24 bits of accuracy on 32-bit floats, even though it is defined on 64-bit floats in the 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.

The API of the crate is stable and the only reason it's not at version `1.0.0` is because its dependencies are not.

## 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));
``````