|
13 | 13 | namespace internal { |
14 | 14 | Rcpp::Function ll_fun("ls"); |
15 | 15 | Rcpp::Function grad_fun("ls"); |
| 16 | + SEXP message_sym = Rf_install("message"); |
16 | 17 | } |
17 | 18 |
|
18 | 19 | template <typename F, typename T> |
@@ -42,11 +43,15 @@ double r_function(const T& v, |
42 | 43 | double lp = 0; |
43 | 44 | auto v_cons = stan::math::lub_constrain<jacobian__>(v, lower_bounds, upper_bounds, lp); |
44 | 45 | SEXP res = internal::ll_fun(v_cons); |
45 | | - SEXP msgSEXP = Rf_getAttrib(res, Rf_install("message")); |
46 | | - // If the result has a "message" attribute, it indicates an error in the user function |
47 | | - if (msgSEXP != R_NilValue) { |
48 | | - std::string msg = Rf_translateCharUTF8(STRING_ELT(msgSEXP, 0)); |
49 | | - throw std::domain_error("Error in user-defined function: " + msg); |
| 46 | + // Fast path: successful calls return a plain numeric scalar (REALSXP). |
| 47 | + // Only check attributes when the result is a list (VECSXP), which indicates |
| 48 | + // the user function returned a list (e.g., with a "message" on error). |
| 49 | + if (TYPEOF(res) == VECSXP) { |
| 50 | + SEXP msgSEXP = Rf_getAttrib(res, internal::message_sym); |
| 51 | + if (msgSEXP != R_NilValue) { |
| 52 | + std::string msg = Rf_translateCharUTF8(STRING_ELT(msgSEXP, 0)); |
| 53 | + throw std::domain_error("Error in user-defined function: " + msg); |
| 54 | + } |
50 | 55 | } |
51 | 56 | return Rcpp::as<double>(res) + lp; |
52 | 57 | } |
@@ -75,14 +80,24 @@ stan::math::var r_function(const T& v, |
75 | 80 | } else { |
76 | 81 | arena_v = stan::math::lub_constrain<jacobian__>(v, lower_bounds, upper_bounds, lp); |
77 | 82 | SEXP res = internal::grad_fun(arena_v.val()); |
78 | | - SEXP msgSEXP = Rf_getAttrib(res, Rf_install("message")); |
79 | | - // If the result has a "message" attribute, it indicates an error in the user function |
80 | | - if (msgSEXP != R_NilValue) { |
81 | | - std::string msg = Rf_translateCharUTF8(STRING_ELT(msgSEXP, 0)); |
82 | | - throw std::domain_error("Error in user-defined gradient function: " + msg); |
| 83 | + // Fast path: successful gradient calls return a plain numeric vector. |
| 84 | + if (TYPEOF(res) == VECSXP) { |
| 85 | + SEXP msgSEXP = Rf_getAttrib(res, internal::message_sym); |
| 86 | + if (msgSEXP != R_NilValue) { |
| 87 | + std::string msg = Rf_translateCharUTF8(STRING_ELT(msgSEXP, 0)); |
| 88 | + throw std::domain_error("Error in user-defined gradient function: " + msg); |
| 89 | + } |
83 | 90 | } |
84 | 91 | arena_grad = Rcpp::as<Eigen::VectorXd>(res); |
85 | | - rtn = Rcpp::as<double>(internal::ll_fun(arena_v.val())); |
| 92 | + SEXP ll_res = internal::ll_fun(arena_v.val()); |
| 93 | + if (TYPEOF(ll_res) == VECSXP) { |
| 94 | + SEXP msgSEXP = Rf_getAttrib(ll_res, internal::message_sym); |
| 95 | + if (msgSEXP != R_NilValue) { |
| 96 | + std::string msg = Rf_translateCharUTF8(STRING_ELT(msgSEXP, 0)); |
| 97 | + throw std::domain_error("Error in user-defined function: " + msg); |
| 98 | + } |
| 99 | + } |
| 100 | + rtn = Rcpp::as<double>(ll_res); |
86 | 101 | } |
87 | 102 | return make_callback_var( |
88 | 103 | rtn, |
|
0 commit comments