Lambert W function (principal branch)
lambertW.RdSolves \(W(x) e^{W(x)} = x\) for the principal branch \(W_0\).
Details
This implementation allows for automatic differentiation with RTMB.
The value is obtained by Halley iteration, which converges to machine
precision in a handful of steps over the whole domain. For AD, the function
is registered as an atomic operation via ADjoint with
the analytic derivative
$$W'(x) = \frac{1}{e^{W(x)} (1 + W(x))},$$
expressed through the returned value rather than through \(x\). Written
this way the derivative is itself an AD-able expression, so derivatives of
every order are available; in particular the third-order derivatives that
the gradient of a Laplace approximation requires.
The principal branch is defined for \(x \ge -1/e\), with
\(W_0(-1/e) = -1\) and \(W_0(x) \ge -1\) throughout. Values below
\(-1/e\) return NaN with a warning. The derivative is infinite at
the branch point \(x = -1/e\).