library(dual)
# Initilizing variables of the function
x <- dual(f = 1.5, grad = c(1, 0, 0))
y <- dual(f = 0.5, grad = c(0, 1, 0))
z <- dual(f = 1.0, grad = c(0, 0, 1))
# Computing the function and its gradient
exp(z - x) * sin(x)^y / x
# General use for computations with dual numbers
a <- dual(1.1, grad = c(1.2, 2.3, 3.4, 4.5, 5.6))
0.5 * a^2 - 0.1
# Johann Heinrich Lambert's W-function
lambertW <- function(x) {
w0 <- 1
w1 <- w0 - (w0*exp(w0)-x)/((w0+1)*exp(w0)-(w0+2)*(w0*exp(w0)-x)/(2*w0+2))
while(abs(w1-w0) > 1e-15) {
w0 <- w1
w1 <- w0 - (w0*exp(w0)-x)/((w0+1)*exp(w0)-(w0+2)*(w0*exp(w0)-x)/(2*w0+2))
}
return(w1)
}
lambertW(dual(1, 1))
Run the code above in your browser using DataLab