Expose_stan_functions with stan/math library function

I am attempting to import my stan functions to R for testing. I used one of the C++ functions from stan/math library log_modified_bessel_first_kind and the code fails to compile successfully. Is there a way to expose my functions for testing? Below is my code:

model_code <- 
"
functions {
real log_modified_bessel_first_kind(real y, real z); 
real calclogC_up2const(real tau, int K) {
   real res;
  res = (K / 2.0 - 1) * log(tau) - log_modified_bessel_first_kind(K / 2.0 - 1, tau);
   return res;
}
real my_vMF_lpdf(vector y, real tau, vector mu, int K) {
  real res = calclogC_up2const(tau, K) + y' * mu * tau;
    return res;
  }
}
"
expose_stan_functions(stanc(model_code = model_code, allow_undefined = TRUE))

Thank you!

1 Like

Not currently

So you want to test the Stan-math internal function log_modified_bessel_first_kind in R? If so, then you must define a user-defined function for just that. So defined

exposeme_log_modified_bessel_first_kind

which just calls the log_modified_bessel_first_kind and then you are good to go and expose it as usual.

Not sure if that is what you intend to do.

1 Like

Thank you!

The log_modified_bessel_first_kind exists in Stan Math but was never exposed to the Stan language. So, that won’t work. It was intended that expose_stan_functions would work with user-defined functions that are declared in the functions block but defined in external C++ files, but that does not actually work either.

Yack.

On the other hand, if you are defining (or wrapping) a C++ function to use in R, then you might as well just use Rcpp::sourceCpp.

2 Likes

i.e.

Rcpp::cppFunction(
'double log_besselJ(double v, double z) {
  return stan::math::log_modified_bessel_first_kind(v, z);
}', depends = "StanHeaders", includes = "#include <stan/math.hpp>")

works

1 Like