diff --git a/DESCRIPTION b/DESCRIPTION index 053dda7a..3d55cb48 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,7 +29,7 @@ URL: https://mc-stan.org/cmdstanr/, https://discourse.mc-stan.org BugReports: https://github.com/stan-dev/cmdstanr/issues Encoding: UTF-8 LazyData: true -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.0 Roxygen: list(markdown = TRUE, r6 = FALSE) SystemRequirements: CmdStan (https://mc-stan.org/users/interfaces/cmdstan) Depends: @@ -50,6 +50,5 @@ Suggests: loo (>= 2.0.0), rmarkdown, testthat (>= 2.1.0), - Rcpp, - RcppEigen + Rcpp VignetteBuilder: knitr diff --git a/R/fit.R b/R/fit.R index 9dc1c324..554a79b3 100644 --- a/R/fit.R +++ b/R/fit.R @@ -312,8 +312,8 @@ CmdStanFit$set("public", name = "init", value = init) #' @description The `$init_model_methods()` method compiles and initializes the #' `log_prob`, `grad_log_prob`, `constrain_variables`, `unconstrain_variables` #' and `unconstrain_draws` functions. These are then available as methods of -#' the fitted model object. This requires the additional `Rcpp` and -#' `RcppEigen` packages, which are not required for fitting models using +#' the fitted model object. This requires the additional `Rcpp` package, +#' which are not required for fitting models using #' CmdStanR. #' #' Note: there may be many compiler warnings emitted during compilation but @@ -339,7 +339,6 @@ init_model_methods <- function(seed = 0, verbose = FALSE, hessian = FALSE) { call. = FALSE) } require_suggested_package("Rcpp") - require_suggested_package("RcppEigen") if (length(private$model_methods_env_$hpp_code_) == 0) { stop("Model methods cannot be used with a pre-compiled Stan executable, ", "the model must be compiled again", call. = FALSE) diff --git a/R/utils.R b/R/utils.R index 26d57fec..54c78e3e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -928,8 +928,7 @@ compile_functions <- function(env, verbose = FALSE, global = FALSE) { mod_stan_funs <- paste(c( env$hpp_code[1:(funs[1] - 1)], "#include ", - "#include ", - "// [[Rcpp::depends(RcppEigen)]]", + "#include ", stan_funs), collapse = "\n") if (global) { @@ -980,7 +979,6 @@ expose_stan_functions <- function(function_env, global = FALSE, verbose = FALSE) call. = FALSE) } require_suggested_package("Rcpp") - require_suggested_package("RcppEigen") if (function_env$compiled) { if (!global) { message("Functions already compiled, nothing to do!") diff --git a/inst/include/hessian.cpp b/inst/include/hessian.cpp index 003c722e..3883c130 100644 --- a/inst/include/hessian.cpp +++ b/inst/include/hessian.cpp @@ -1,8 +1,6 @@ -#include +#include #include -// [[Rcpp::depends(RcppEigen)]] - template struct hessian_wrapper { const M& model; diff --git a/inst/include/rcpp_eigen_interop.hpp b/inst/include/rcpp_eigen_interop.hpp new file mode 100644 index 00000000..f916fff9 --- /dev/null +++ b/inst/include/rcpp_eigen_interop.hpp @@ -0,0 +1,57 @@ +#ifndef CMDSTANR_RCPP_EIGEN_INTEROP_HPP +#define CMDSTANR_RCPP_EIGEN_INTEROP_HPP + +#include +#include + +namespace Rcpp { + namespace traits { + template + class Exporter> { + private: + SEXP object; + public: + Exporter(SEXP x) : object(x){} + ~Exporter(){} + + // Adapted from Rcpp/internal/Exporter.h + Eigen::Matrix get() { + if (R == 1) { + Eigen::Matrix result(Rf_length(object)); + Rcpp::internal::export_indexing, T>(object, result); + return result; + } else if (C == 1) { + Eigen::Matrix result(Rf_length(object)); + Rcpp::internal::export_indexing, T>(object, result); + return result; + } else { + Shield dims(Rf_getAttrib(object, R_DimSymbol)); + if (Rf_isNull(dims) || Rf_length(dims) != 2) { + throw Rcpp::not_a_matrix(); + } + int* dims_ = INTEGER(dims); + Eigen::Matrix result(dims_[0], dims_[1]); + T* result_data = result.data(); + Rcpp::internal::export_indexing(object, result_data); + return result; + } + } + }; + } // traits + + // Rcpp is hardcoded to assume that Eigen types will be wrapped using the + // eigen_wrap function in the RcppEigen package + namespace RcppEigen { + template + SEXP eigen_wrap(const T& x) { + const static int RTYPE = Rcpp::traits::r_sexptype_traits>::rtype; + Rcpp::Vector vec_rtn(Rcpp::wrap(stan::math::to_array_1d(x))); + if (!stan::is_eigen_col_vector::value) { + vec_rtn.attr("dim") = Rcpp::Dimension(x.rows(), x.cols()); + } + return vec_rtn; + } + } // RcppEigen +} // Rcpp + +#endif diff --git a/man/cmdstanr-package.Rd b/man/cmdstanr-package.Rd index 930105ee..c98b0b3a 100644 --- a/man/cmdstanr-package.Rd +++ b/man/cmdstanr-package.Rd @@ -198,4 +198,34 @@ The Stan and CmdStan documentation: \item Stan documentation: \href{https://mc-stan.org/users/documentation/}{mc-stan.org/users/documentation} \item CmdStan User’s Guide: \href{https://mc-stan.org/docs/cmdstan-guide/}{mc-stan.org/docs/cmdstan-guide} } + +Useful links: +\itemize{ + \item \url{https://mc-stan.org/cmdstanr/} + \item \url{https://discourse.mc-stan.org} + \item Report bugs at \url{https://github.com/stan-dev/cmdstanr/issues} +} + +} +\author{ +\strong{Maintainer}: Jonah Gabry \email{jsg2201@columbia.edu} + +Authors: +\itemize{ + \item Rok Češnovar \email{rok.cesnovar@fri.uni-lj.si} + \item Andrew Johnson (\href{https://orcid.org/0000-0001-7000-8065}{ORCID}) +} + +Other contributors: +\itemize{ + \item Ben Bales [contributor] + \item Mitzi Morris [contributor] + \item Mikhail Popov [contributor] + \item Mike Lawrence [contributor] + \item William Michael Landau \email{will.landau@gmail.com} (\href{https://orcid.org/0000-0003-1878-3253}{ORCID}) [contributor] + \item Jacob Socolar [contributor] + \item Martin Modrák [contributor] + \item Steve Bronder [contributor] +} + } diff --git a/man/fit-method-init_model_methods.Rd b/man/fit-method-init_model_methods.Rd index 0fd0c075..1a96cd59 100644 --- a/man/fit-method-init_model_methods.Rd +++ b/man/fit-method-init_model_methods.Rd @@ -19,8 +19,8 @@ init_model_methods(seed = 0, verbose = FALSE, hessian = FALSE) The \verb{$init_model_methods()} method compiles and initializes the \code{log_prob}, \code{grad_log_prob}, \code{constrain_variables}, \code{unconstrain_variables} and \code{unconstrain_draws} functions. These are then available as methods of -the fitted model object. This requires the additional \code{Rcpp} and -\code{RcppEigen} packages, which are not required for fitting models using +the fitted model object. This requires the additional \code{Rcpp} package, +which are not required for fitting models using CmdStanR. Note: there may be many compiler warnings emitted during compilation but