https://github.com/cran/pracma
Raw File
Tip revision: 708a2ad382a163d1eef5af0665e3ae2aad200ced authored by HwB on 21 March 2013, 00:00:00 UTC
version 1.4.5
Tip revision: 708a2ad
lambertW.R
##
##  l a m b e r t W . R  Lambert W Function
##


lambertWp <- function(z) {
    if (!is.numeric(z))
        stop("Argument 'z' must be a numeric (real) vector.")

    if (length(z) == 1) {
        if (z  < -1/exp(1)) return(NA)
        if (z == -1/exp(1)) return(-1)
        w0 <- 1
        w1 <- w0 - (w0*exp(w0) - z)/((w0+1)*exp(w0) - (w0+2)*(w0*exp(w0) - z)/(2*w0+2))
        n = 1
        while(abs(w1-w0) > 1e-12 && n <= 12) {
            w0 <- w1
            w1 <- w0 - (w0*exp(w0) - z)/((w0+1)*exp(w0) - (w0+2)*(w0*exp(w0) - z)/(2*w0+2))
            n = n + 1
        }
        if (n > 12)
            warning("Maximum number of iterations has been exceeded.")
        return(w1)
    } else {
        sapply(z, lambertWp)
    }
}
back to top