I'm trying to get an approximate answer to the following integral:

$\lim_{n \to \infty} \frac{n+1}{2} \int_1^{\ln(n)}xe^{-x}(1-\exp(-x - W(-xe^{-x})))^2 dx $

I'm currently just interested in if it converges to anything, so I want to just plug in a few values of $n$ and see what happens. My R 'code' looks like this:

>W <- function(x){(1001/2)*x*exp(-x)*(1-exp(-x-LambertW(-x*exp(-x))))^2}

>integrate(W, lower=1, upper=log(1000))

Unfortunately, R tells me that I have a non-finite function value. It's my understanding that LambertW calculates the principal branch of the function, which does not do anything strange for values between $-\frac 1e$ and $0$, which is where I am. I tried shifting the lower bound slightly up to avoid the branching point at $-\frac 1e$, but I got the same error message then.

  • $\begingroup$ Possibly a bug in the R code for LambertW? Can you try integrating on other intervals to see for what value of x the "non-finite" value is occurring? $\endgroup$ – Robert Israel Dec 13 '18 at 13:39
  • $\begingroup$ @RobertIsrael I figured it out. It seems there is a LambertW-function and a lambertW-function that are slightly different. $\endgroup$ – Johanna Dec 13 '18 at 14:32

It seems capital letters REALLY matter with R. The function LambertW only takes positive values, while the function lambertW takes values at least $-\frac 1e$. Switching to the correct Lambert W function-function solved the problem.

  • $\begingroup$ out of interest do these functions come with R as standard? Or are they loaded from additional packages, and if it is the latter how easy is it dig out the source code for these packages? For example in python using the scipy package you can clearly see the source code here along with the methods used to handle the boundary cases $\endgroup$ – Nadiels Dec 13 '18 at 14:36
  • $\begingroup$ @Nadiels No, they're loaded from the packages spatstat and VGAM, respectively. The source code for both should be available, but I haven't tried to find it. $\endgroup$ – Johanna Dec 13 '18 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.