The Lambert W function W(z)=u is defined as the inverse of (see xexp)
[REMOVE_ME]uexp(u)=z,[REMOVEME2]
i.e., it satisfies W(z)exp(W(z))=z.
W evaluates the Lambert W function (W), its first derivative (deriv_W), and its logarithm (log_W). All of them have a principal (branch = 0 (default)) and non-principal branch (branch = -1) solution.
W is a wrapper for lambertW0 and lambertWm1 in the lamW package.
z: a numeric vector of real values; note that W(Inf, branch = 0) = Inf.
branch: either 0 or -1 for the principal or non-principal branch solution.
W.z: Lambert W function evaluated at z; see Details below for why this is useful.
Returns
numeric; same dimensions/size as z.
W returns numeric, Inf (for z = Inf), or NA if z<−1/e.
Note that W handles NaN differently to lambertW0 / lambertWm1 in the lamW package; it returns NA.
Description
The Lambert W function W(z)=u is defined as the inverse of (see xexp)
uexp(u)=z,
i.e., it satisfies W(z)exp(W(z))=z.
W evaluates the Lambert W function (W), its first derivative (deriv_W), and its logarithm (log_W). All of them have a principal (branch = 0 (default)) and non-principal branch (branch = -1) solution.
W is a wrapper for lambertW0 and lambertWm1 in the lamW package.
Details
Depending on the argument z of W(z) one can distinguish 3 cases:
z≥0: solution is unique W(z) = W(z, branch = 0)
−1/e≤z<0: two solutions: the principal (W(z, branch = 0)) and non-principal (W(z, branch = -1)) branch;
z<−1/e: no solution exists in the reals.
log_W computes the natural logarithm of W(z). This can be done efficiently since logW(z)=logz−W(z). Similarly, the derivative can be expressed as a function of W(z):
W′(z)=(1+W(z))exp(W(z))1=z(1+W(z))W(z).
Note that W′(0)=1 and W′(−1/e)=∞.
Moreover, by taking logs on both sides we can even simplify further to
logW′(z)=logW(z)−logz−log(1+W(z))
which, since logW(z)=logz−W(z), simplifies to
logW′(z)=−W(z)−log(1+W(z)).
For this reason it is numerically faster to pass the value of W(z) as an argument to deriv_W since W(z) often has already been evaluated in a previous step.
Examples
W(-0.25)# "reasonable" input event W(-0.25, branch =-1)# "extreme" input eventcurve(W(x, branch =-1),-1,2, type ="l", col =2, lwd =2)curve(W(x),-1,2, type ="l", add =TRUE, lty =2)abline(v =-1/ exp(1))# For lower values, the principal branch gives the 'wrong' solution; # the non-principal must be used.xexp(-10)W(xexp(-10), branch =0)W(xexp(-10), branch =-1)curve(log(x),0.1,5, lty =2, col =1, ylab ="")curve(W(x),0,5, add =TRUE, col ="red")curve(log_W(x),0.1,5, add =TRUE, col ="blue")grid()legend("bottomright", c("log(x)","W(x)","log(W(x))"), col = c("black","red","blue"), lty = c(2,1,1))
References
Corless, R. M., G. H. Gonnet, D. E. G. Hare, D. J. Jeffrey and D. E. Knuth (1996). On the Lambert W function . Advances in Computational Mathematics, pp. 329-359.