I am modeling a system of ODE that describes the kinetics of a compartmental model. I have successfully used the matrix exponential function in Stan to solve the system for both optimization and sampling. I am now trying to use eigenvalues/eigenvectors to solve the system, extracting the real parts of the complex values returned by the new Stan functions eigenvalues and eigenvectors. Optimization and sampling from the prior work fine with the new method, but I get the following error when I try to sample from the posterior:

Chain 1 iodineModel_2: stan/lib/stan_math/lib/eigen_3.3.9/Eigen/src/Eigenvalues/EigenSolver.h:348: Eigen::EigenSolver<_MatrixType>::EigenvectorsType Eigen::EigenSolver<_MatrixType>::eigenvectors() const [with _MatrixType = Eigen::Matrix<stan::math::var_value, -1, -1, 1, -1, -1>; Eigen::EigenSolver<_MatrixType>::EigenvectorsType = Eigen::Matrix<std::complex<stan::math::var_value >, -1, -1, 1, -1, -1>; typename Eigen::NumTraits::Real = stan::math::var_value]: Assertion `m_eigenvectorsOk && “The eigenvectors have not been computed together with the eigenvalues.”’ failed.
Warning: Chain 1 finished unexpectedly!

There are some rather old non-Stan discussions about this message on the internet that speak to some part of the Eigen library not being implemented yet. Can anyone shed some light on the meaning of this error? Thanks.

Since no one appears to know off the cuff what causes this error, I worked up a simple example to illustrate the problem, but unfortunately it still somewhat busy. It attempts to find the estimates of the rate constants and intake of iodine given measurements of iodine in the thyroid and the biokinetic model shown in Figure 2 of Riggs Iodine Model. The MCMC example throws the error mentioned above, but the really interesting thing is that optimization works with a given version of the Stan code but fails if print statements are inserted to aid debugging.

The attached R and Stan code was run in the following environment:

cmdstan_version()
[1] “2.31.0”
sessionInfo()
R version 4.2.2 Patched (2022-11-10 r83330)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 21.1

I cannot reproduce the C++ error from above. If I manually run the cmdstan executable with those specified settings it actually samples “fine”, it seems (in the sense that the program terminates normally). Perhaps it is a cmdstanr specific issue?