https://github.com/cran/pracma
Raw File
Tip revision: c79a04b5074656b36e591191eb8137b70a349932 authored by Hans W. Borchers on 30 June 2014, 00:00:00 UTC
version 1.7.0
Tip revision: c79a04b
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-15 && n <= 20) {
            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 > 20)
            warning("Maximum number of iterations has been exceeded.")
        return(w1)
    } else {
        sapply(z, lambertWp)
    }
}

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

    if (length(z) == 1) {
        zw <- lambertWp(z)
        return( 1 / (1+zw) / exp(zw) )
    } else {
        sapply(z, D_lambertWp)
    }
}
back to top