| ADoverload | R Documentation |
Enable extra RTMB convenience methods
ADoverload(x = c("[<-", "c", "diag<-", "if"))
x |
Name of primitive to overload |
Work around limitations in R's method dispatch system by overloading some selected primitives, currently:
Inplace replacement, so you can do x[i] <- y when x is numeric and y is AD.
Mixed combine, so you can do e.g. c(x, y) when x numeric and y is AD.
Diagonal assignment, so you can do diag(x) <- y when x is a numeric matrix and y is AD.
Simple if-else branching where the condition is parameter dependent, so you can tape the result of e.g. if (x<0) x else x*x.
In all cases, the result should be AD.
The methods (except if) are automatically temporarily attached to the search path (search()) when entering MakeTape or MakeADFun.
Alternatively, methods can be overloaded locally inside functions using e.g. "[<-" <- ADoverload("[<-"). This is only needed when using RTMB from a package.
Function representing the overload.
MakeTape(function(x) {print(search()); x}, numeric(0))
MakeTape(function(x) c(1,x), 1:3)
MakeTape(function(x) {y <- 1:3; y[2] <- x; y}, 1)
MakeTape(function(x) {y <- matrix(0,3,3); diag(y) <- x; y}, 1:3)
MakeTape(function(x) {"if" <- ADoverload("if"); if (x<0) x else x*x}, 1)
## Robust logspace_gamma(x) := lgamma(exp(x))
logspace_gamma <- Vectorize(function(x) {
"if" <- ADoverload("if")
if (x < -150) -x else lgamma(exp(x))
})
MakeTape(logspace_gamma, numeric(10))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.