library(RTMB)
TapeConfig(comparison = "tape")
f <- function(x) CondExpLt(x, 3, 0, x^2)
g <- MakeTape(f, numeric(1))
x <- seq(0, 5)
# Does not work!
f2 <- function(x) if (x < 3) 0 else x^2
g2 <- MakeTape(f2, numeric(1))
# Compare the real answer (deriv) with various values returned by RTMB
data.frame(
x = x,
deriv = ifelse(x < 3, 0, 2 * x),
deriv_f = sapply(x, g$jacobian),
deriv_f2 = sapply(x, g2$jacobian)
)
Run the code above in your browser using DataLab