files <- dir(system.file("include", "stan", "math", "prim",
package = "StanHeaders"),
pattern = "hpp$", recursive = TRUE)
functions <- sub("\\.hpp$", "",
sort(unique(basename(files[dirname(files) != "."]))))
length(functions) # you could call most of these Stan functions
if (FALSE) {
log(sum(exp(exp(1)), exp(pi))) # true value
stanFunction("log_sum_exp", x = exp(1), y = pi)
args(log_sum_exp) # now exists in .GlobalEnv
log_sum_exp(x = pi, y = exp(1))
# but log_sum_exp() was not defined for a vector or matrix
x <- c(exp(1), pi)
try(log_sum_exp(x))
stanFunction("log_sum_exp", x = x) # now it is
# log_sum_exp() is now also defined for a matrix
log_sum_exp(as.matrix(x))
log_sum_exp(t(as.matrix(x)))
log_sum_exp(rbind(x, x))
# but log_sum_exp() was not defined for a list
try(log_sum_exp(as.list(x)))
stanFunction("log_sum_exp", x = as.list(x)) # now it is
# in rare cases, passing a nested list is needed
stanFunction("dims", x = list(list(1:3)))
# functions of complex arguments work
stanFunction("eigenvalues", # different ordering than base:eigen()
x = matrix(complex(real = 1:9, imaginary = pi),
nrow = 3, ncol = 3))
# nullary functions work but are not that interesting
stanFunction("negative_infinity")
# PRNG functions work by adding a seed argument
stanFunction("lkj_corr_rng", K = 3L, eta = 1)
args(lkj_corr_rng) # has a seed argument
}
Run the code above in your browser using DataLab