From the relation below, the Lambert W can be calculated by using eW — the exponent of Lambert W function.
eW0(x) — the exponent of the principal branch of the Lambert W function W0(x) — can be calculated by using Newton-Raphson method,
or Halley's method shown below.![]()
When finding eW using an iterative method such as Newton's Rapson method,
giving a bad initial value, especially when it is very close to the singularity x = -1 / e,
can be terrible in terms of convergence.
Therefore, it is desirable that the initial value asymptotically approaches eW at the singularity.
For that reason, I chose the following formula as an approximate expression that gives the initial value.
This equation replaces the coefficient 1 / 3 of the third term of the Puiseux series expansion of
e W0
at x = -1 / e
with 0.3 and ignores the fourth and subsequent terms.
Initial approximation :
Series expansion of eW at z = -1 / e (Puiseux series) :
Instead of the coefficient of 0.3 in the formula above, it can be (e - √2 - 1) which make it zero at x = 0,
or "1 / 3" from Puiseux series, but 0.3 is quite good enough for this purpose.
It's nice to be able to handle complex numbers easily with HP Prime, and thanks to sqrt √ in this equation,
HP Prime can automatically get the complex y0 from x less than -1 / e.
Note that, complex output from real input must be allowed by selecting the check box of 'Complex' in 'Home Setting'.
Calculation results by WolframAlphaDownload 'eW.hpprgm' for HP Prime.
eW (for HP Prime) eW(x) returns the requested exponent of the principal branch of the Lambert W function. Recurrence formula (Halleys method) :
Initial approximation :
![]()
#CAS //eW : exponent of the Lambert W0 function //Rev.1.52 (Dec. 4, 2020) (c) Takayuki HOSODA (aka Lyuka) // http://www.finetune.co.jp/~lyuka/technote/lambertw/ //Acknowledgments: Thanks to Albert Chan for his informative suggestions. eW(x):= BEGIN LOCAL y, p, s, t, u, v; LOCAL q, r, m; HComplex := 1; // Enable complex result from a real input. r := 1.0 / e; q := e - sqrt(2) - 1; s := x + r; IF s == 0 THEN return r; END; m := MAXREAL / 1024.0; IF abs(RE(x)) <= m AND abs(IM(x)) <= m THEN m := 1.0; y := r + sqrt(2.0 * r * s) + q * s; // approximation near x=-1/e ELSE m := 1.0 / 1024.0; x := x * m; s := x * m; y := r * m + sqrt(r * (s * 64.0)) + q * s; y := y * 1024.0; END; r := MAXREAL; REPEAT q := r; p := y; t := ln(y); v := 1 + t; s := 0.5 * (x * inv(y) - t * m) * inv(v) + v * m; //s <> 0, y <> 0, v <> 0 t := (x - y * (t * m)); y := y + t * inv(s); // Halley's method r := cabs(y - p); // correction radius UNTIL 0 == r OR q <= r; // convergence check return p; END; // This is a workaround for the dynamic range problem of complex division and abs() in CAS. // Conforms to HP Prime software version 2.1.14433 (2020 01 21). // cabs(z) -- returns the magnitude of the complex number z. cabs(z):= BEGIN LOCAL x, y, t, s; x := RE(z); y := IM(z); IF x < 0 THEN x := -x; END; IF y < 0 THEN y := -y; END; IF x == 0 THEN return y; END; IF y == 0 THEN return x; END; IF (y > x) THEN t := x; x := y; y := t; END; t := x - y; IF t > y THEN t := x / y; t := t + sqrt(1 + t * t); ELSE t := t / y; s := t * (t + 2); t := t + s / (sqrt(2) + sqrt(2 + s)); t := t + sqrt(2) + 1; END; return(x + y / t); END; #END
It is necessary to consider the singularity, but since eW does not become zero, it was relatively easy to find eW by Newton-Raphson method or Harrey's method. However, when trying to find W0 - Lambert's W of the primary branch by such an iterative method, since W0 becomes zero when x is zero, so it is necessary to consider zero as well as the singularity. In other words, it is strongly desired that the approximation formula of the initial value of the iterative method has an asymptotic to zero error at the singular point and zero. Also, since W0 is, from the singularity, a monotonically increasing function, its approximation must also be a monotonically increasing function. To meet these requirements, the initial value is calculated using the following approximation function.
Since W0 has a large derivative around the singularity, Newton's method can cause catastrophic behavior near the singularity if the initial approximation is poor or due to computational errors. Therefore, we will use the Halley method or Householder's method of order 4, which is more stable than the Newton method, to obtain W0.
In addition to the Halley's method of cubic convergence, the Householder's method of order 4 can also be used to calculate W0. However, special care is required because the calculation is complicated and it is easy for digits to overflow during the calculation. Householder's method was only a few percent faster than Halley's method for calculating up to 16 decimal places.
Newton-Raphson method :Halley's method :![]()
Householder's method of order 4 :![]()
The convergence test by Urabe's theorem is used to determine the convergence. It is a method to judge that the calculation limit has been reached and terminate the iteration when the correction value does not change or increases.
W0 calculation using Halley's methodCalculation example of LW0CDownload 'LW0C.hpprgm' for HP Prime.
LW0C (for HP Prime CAS) LW0C(x) returns the requested value of the principal branch of the Lambert W function. Recurrence formula (Halley's method) :
Initial approximation :
![]()
#CAS //LW0C - Principal branch of the Lambert W function //Rev.1.52 (Dec. 4, 2020) (c) Takayuki HOSODA (aka Lyuka) //Acknowledgments: Thanks to Albert Chan for his informative suggestions. LW0C(x):= BEGIN LOCAL y, p, s, t; LOCAL q, r, m; HComplex := 1; // Enable complex result from a real input. r := 1.0 / e; q := e - sqrt(2.0) - 1.0; m := MAXREAL / 1024.0; IF abs(RE(x)) <= m AND abs(IM(x)) <= m THEN m := 1.0; s := x + r; IF s == 0 THEN return -1; END; y := ln(r + sqrt(2.0 * r * s) + q * s); // approximation near x=-1/e ELSE m := 1.0 / 1024.0; x := x * m; s := x * m; y := ln(r * m + sqrt(r * (s * 64.0)) + q * s); y := y + 6.931471805599453094; // + ln(2) * 10 END; r := MAXREAL; //1.79769313486e308 REPEAT q := r; p := y; t := exp(y) * m; s := y * t - x; t := (0.5 * y) * inv(y + 1.0) * s + t + x; // t <> 0 y := y - s * inv(t); // Halley's method r := abs(y - p); // correction radius; UNTIL 0 == r OR q <= r; // convergence check return p; END; #END
Calculation results by WolframAlpha
W0 by Using Householder's method of order 4Calculation speed comparison, eW (Rev.1.46) V.S. LW0C (Rev.1.46) V.S. LW0H (Rev.1.46)
Download 'LW0H.hpprgm' for HP Prime.
LW0H (for HP Prime) LW0H(x) returns the requested value of the principal branch of the Lambert W function. Recurrence formula (Householder's method of order 4) :
Initial approximation :
![]()
#CAS //LW0H - Principal branch of the Lambert W function //Rev.1.46 (Nov. 28, 2020) (c) Takayuki HOSODA (aka Lyuka) //Acknowledgments: Thanks to Albert Chan for his informative suggestions. LW0H(x):= BEGIN LOCAL y, p, s, t, u, v; LOCAL q, r; HComplex := 1; // Enable complex result from a real input. r := sqrt(MAXREAL) / 1e4; // Limit x range IF abs(RE(x)) > r OR abs(IM(x)) > r THEN return "LW0H: x out of range"; END; r := 1.0 / e; s := x + r; IF s == 0 THEN return -1; END; q := e - sqrt(2.0) - 1.0; y := 1.0 * ln(r + sqrt(2.0 * r * s) + q * s); // approximation near x=-1/e r := MAXREAL; REPEAT q := r; p := y; s := exp(y); t := s + x; u := y * s; v := u - x; v := (6.0 * ((y + 1.0) * u * x + t * (t + u)) + v * (3.0 * (u + x) + y * v)); IF (0 == v) THEN break; END; t := 3.0 * (u - x) * ((u + x) * (y + 2.0) + 2.0 * s); y := y - t * inv(v); // Householder's method of order 4 r := abs(y - p); // correction radius UNTIL 0 == r OR q <= r; // convergence check by Urabe's theorem return p; END; #END
From some screen captures of HP Prime for reference![]()
Download 'scLW.hpprgm' used to make the comparison above -- invoked as scLW(3) or scLW(50).
Reference