If I update the file (called ‘external_test.hpp’) to
template <typename T>
Eigen::Matrix<T, Eigen::Dynamic, 1> schur_U(
const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& m, std::ostream *msgs) {
if (m.size() == 0) {
return {};
}
return Eigen::RealSchur<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> >(m)
.matrixU();
}
And then I run the following in R
library(rstan)
stancode <- 'functions {
// matrix schur_T(matrix A);
matrix schur_U(matrix A);
}
data {
matrix[3, 3] A;
}
parameters {
real b;
}
transformed parameters {
// matrix T = schur_T(A);
matrix[3, 3] U = schur_U(A);
}
model {
b ~ std_normal();
}
'
mod <- stan_model(model_code = stancode,
allow_undefined = TRUE,
includes = paste0('\n#include "',
file.path(getwd(), 'external_test.hpp'), '"\n')
)
try(readLines(stanc(model_code = stancode, allow_undefined = TRUE)$cppcode))
I get the error messages from stan_model(...)
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from <built-in>:1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Dense:1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Core:535:
/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
#pragma clang diagnostic pop
^
In file included from <built-in>:1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/4.
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file808778475ee3.cpp 2> file808778475ee3.cpp.err.txt' had status 1
Error in sink(type = "output") : invalid connection
and try(readLines(stanc(model_code = stancode, allow_undefined = TRUE)$cppcode))
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file '// Code generated by Stan version 2.19.1
#include <stan/model/model_header.hpp>
namespace model808715a6c6c6_stancode_namespace {
using std::istream;
using std::string;
using std::stringstream;
using std::vector;
using stan::io::dump;
using stan::math::lgamma;
using stan::model::prob_grad;
using namespace stan::math;
static int current_statement_begin__;
stan::io::program_reader prog_reader__() {
stan::io::program_reader reader;
reader.add_event(0, 0, "start", "model808715a6c6c6_stancode");
reader.add_event(19, 17, "end", "model808715a6c6c6_stancode");
return reader;
}
template <typename T0__>
Eigen::Matrix<typename boost::math::tools::promote_args<T0__>::type, Eigen::Dynamic, Eigen::Dynamic>
schur_U(const Eigen::Matrix<T0__, Eigen::Dynamic, Eigen::Dynamic>& A, std::ostream* pstream__);
class model808715a6c6c6_stancode : public prob_grad {
private:
matrix_d A;
public:
model808715a6c6c6_stancode(stan::io::var_context& context__,
[... truncated]