stan-dev / math

The Stan Math Library is a C++ template library for automatic differentiation of any order using forward, reverse, and mixed modes. It includes a range of built-in functions for probabilistic modeling, linear algebra, and equation solving.
https://mc-stan.org
BSD 3-Clause "New" or "Revised" License
751 stars 188 forks source link

Compilation error when including Eigen and Stan together #2879

Closed MessyShen closed 1 year ago

MessyShen commented 1 year ago

Description

When Eigen and Stan are included in the same file, g++ reports compilation errors like ‘const class stan::math::arena_matrix<Eigen::Matrix<stan::math::var_value<double>, -1, 1> >’ has no member named ‘val’; did you mean ‘eval’?

Example

I just add #include <Eigen/Dense> to the hello world program provided in the official installation guide.

foo.cpp:

#include <Eigen/Dense> // ! Compilation error if this line is added
#include <stan/math.hpp>
#include <iostream>

int main() {
  std::cout << "log normal(1 | 2, 3)="
            << stan::math::normal_lpdf(1, 2, 3)
            << std::endl;
}

It's ok when I built the stan math library

make -j4 -f ../math/make/standalone math-lib

but after running the following line:

make -f ../math/make/standalone foo

I got errors like:

make -f ../math/make/standalone foo
g++ -std=c++1y -pthread -D_REENTRANT -Wno-sign-compare -Wno-ignored-attributes      -I /mnt/hgfs/Shared/StanMathLib/math/lib/tbb_2020.3/include    -O3  -I /mnt/hgfs/Shared/StanMathLib/math/ -I /mnt/hgfs/Shared/StanMathLib/math/lib/eigen_3.3.9 -I /mnt/hgfs/Shared/StanMathLib/math/lib/boost_1.78.0 -I /mnt/hgfs/Shared/StanMathLib/math/lib/sundials_6.1.1/include -I /mnt/hgfs/Shared/StanMathLib/math/lib/sundials_6.1.1/src/sundials    -DBOOST_DISABLE_ASSERTS   -DTBB_INTERFACE_NEW  -DTBB_INTERFACE_NEW          -Wl,-L,"/mnt/hgfs/Shared/StanMathLib/math/lib/tbb" -Wl,-rpath,"/mnt/hgfs/Shared/StanMathLib/math/lib/tbb"        foo.cpp        -Wl,-L,"/mnt/hgfs/Shared/StanMathLib/math/lib/tbb" -Wl,-rpath,"/mnt/hgfs/Shared/StanMathLib/math/lib/tbb"     /mnt/hgfs/Shared/StanMathLib/math/lib/sundials_6.1.1/lib/libsundials_nvecserial.a /mnt/hgfs/Shared/StanMathLib/math/lib/sundials_6.1.1/lib/libsundials_cvodes.a /mnt/hgfs/Shared/StanMathLib/math/lib/sundials_6.1.1/lib/libsundials_idas.a /mnt/hgfs/Shared/StanMathLib/math/lib/sundials_6.1.1/lib/libsundials_kinsol.a  /mnt/hgfs/Shared/StanMathLib/math/lib/tbb/libtbb.so.2 -o foo
In file included from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/cov_exp_quad.hpp:7,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun.hpp:41,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev.hpp:10,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math.hpp:19,
                 from foo.cpp:2:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/gp_exp_quad_cov.hpp: In lambda function:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/gp_exp_quad_cov.hpp:94:33: error: ‘const class stan::math::arena_matrix<Eigen::Matrix<stan::math::var_value<double>, -1, 1> >’ has no member named ‘val’; did you mean ‘eval’?
   94 |           adjsigma += (cov_diag.val().array() * cov_diag.adj().array()).sum();
      |                                 ^~~
      |                                 eval
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/gp_exp_quad_cov.hpp:94:58: error: ‘const class stan::math::arena_matrix<Eigen::Matrix<stan::math::var_value<double>, -1, 1> >’ has no member named ‘adj’
   94 |           adjsigma += (cov_diag.val().array() * cov_diag.adj().array()).sum();
      |                                                          ^~~
In file included from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun.hpp:75,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev.hpp:10,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math.hpp:19,
                 from foo.cpp:2:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/gp_periodic_cov.hpp: In lambda function:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/gp_periodic_cov.hpp:118:29: error: ‘const class stan::math::arena_matrix<Eigen::Matrix<stan::math::var_value<double>, -1, 1> >’ has no member named ‘val’; did you mean ‘eval’?
  118 |       adjsigma += (cov_diag.val().array() * cov_diag.adj().array()).sum();
      |                             ^~~
      |                             eval
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/gp_periodic_cov.hpp:118:54: error: ‘const class stan::math::arena_matrix<Eigen::Matrix<stan::math::var_value<double>, -1, 1> >’ has no member named ‘adj’
  118 |       adjsigma += (cov_diag.val().array() * cov_diag.adj().array()).sum();
      |                                                      ^~~
In file included from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun.hpp:76,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev.hpp:10,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math.hpp:19,
                 from foo.cpp:2:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/grad.hpp: In function ‘void stan::math::grad(stan::math::var&, Eigen::Matrix<stan::math::var_value<double>, -1, 1>&, Eigen::VectorXd&)’:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/grad.hpp:27:9: error: ‘class Eigen::Matrix<stan::math::var_value<double>, -1, 1>’ has no member named ‘adj’
   27 |   g = x.adj();
      |         ^~~
In file included from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun.hpp:131,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev.hpp:10,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math.hpp:19,
                 from foo.cpp:2:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/mdivide_left_spd.hpp: In member function ‘virtual void stan::math::internal::mdivide_left_spd_vv_vari<R1, C1, R2, C2>::chain()’:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/mdivide_left_spd.hpp:63:62: error: ‘class Eigen::Map<Eigen::Matrix<stan::math::vari_value<double>*, -1, -1> >’ has no member named ‘adj’
   63 |     matrix_d adjB = Eigen::Map<matrix_vi>(variRefC_, M_, N_).adj();
      |                                                              ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/mdivide_left_spd.hpp:65:46: error: ‘class Eigen::Map<Eigen::Matrix<stan::math::vari_value<double>*, -1, -1> >’ has no member named ‘adj’
   65 |     Eigen::Map<matrix_vi>(variRefA_, M_, M_).adj()
      |                                              ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/mdivide_left_spd.hpp:67:46: error: ‘class Eigen::Map<Eigen::Matrix<stan::math::vari_value<double>*, -1, -1> >’ has no member named ‘adj’
   67 |     Eigen::Map<matrix_vi>(variRefB_, M_, N_).adj() += adjB;
      |                                              ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/mdivide_left_spd.hpp: In member function ‘virtual void stan::math::internal::mdivide_left_spd_dv_vari<R1, C1, R2, C2>::chain()’:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/mdivide_left_spd.hpp:103:62: error: ‘class Eigen::Map<Eigen::Matrix<stan::math::vari_value<double>*, -1, -1> >’ has no member named ‘adj’
  103 |     matrix_d adjB = Eigen::Map<matrix_vi>(variRefC_, M_, N_).adj();
      |                                                              ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/mdivide_left_spd.hpp:105:46: error: ‘class Eigen::Map<Eigen::Matrix<stan::math::vari_value<double>*, -1, -1> >’ has no member named ‘adj’
  105 |     Eigen::Map<matrix_vi>(variRefB_, M_, N_).adj() += adjB;
      |                                              ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/mdivide_left_spd.hpp: In member function ‘virtual void stan::math::internal::mdivide_left_spd_vd_vari<R1, C1, R2, C2>::chain()’:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/mdivide_left_spd.hpp:140:62: error: ‘class Eigen::Map<Eigen::Matrix<stan::math::vari_value<double>*, -1, -1> >’ has no member named ‘adj’
  140 |     matrix_d adjC = Eigen::Map<matrix_vi>(variRefC_, M_, N_).adj();
      |                                                              ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/mdivide_left_spd.hpp:141:46: error: ‘class Eigen::Map<Eigen::Matrix<stan::math::vari_value<double>*, -1, -1> >’ has no member named ‘adj’
  141 |     Eigen::Map<matrix_vi>(variRefA_, M_, M_).adj()
      |                                              ^~~
In file included from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun.hpp:132,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev.hpp:10,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math.hpp:19,
                 from foo.cpp:2:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/mdivide_left_tri.hpp: In member function ‘virtual void stan::math::internal::mdivide_left_tri_vv_vari<TriView, R1, C1, R2, C2>::chain()’:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/mdivide_left_tri.hpp:84:57: error: ‘class Eigen::Map<Eigen::Matrix<stan::math::vari_value<double>*, -1, -1> >’ has no member named ‘adj’
   84 |                .solve(Map<matrix_vi>(variRefC_, M_, N_).adj());
      |                                                         ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/mdivide_left_tri.hpp:101:39: error: ‘class Eigen::Map<Eigen::Matrix<stan::math::vari_value<double>*, -1, -1> >’ has no member named ‘adj’
  101 |     Map<matrix_vi>(variRefB_, M_, N_).adj() += adjB;
      |                                       ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/mdivide_left_tri.hpp: In member function ‘virtual void stan::math::internal::mdivide_left_tri_dv_vari<TriView, R1, C1, R2, C2>::chain()’:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/mdivide_left_tri.hpp:150:39: error: ‘class Eigen::Map<Eigen::Matrix<stan::math::vari_value<double>*, -1, -1> >’ has no member named ‘adj’
  150 |     Map<matrix_vi>(variRefB_, M_, N_).adj()
      |                                       ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/mdivide_left_tri.hpp:154:57: error: ‘class Eigen::Map<Eigen::Matrix<stan::math::vari_value<double>*, -1, -1> >’ has no member named ‘adj’
  154 |                .solve(Map<matrix_vi>(variRefC_, M_, N_).adj());
      |                                                         ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/mdivide_left_tri.hpp: In member function ‘virtual void stan::math::internal::mdivide_left_tri_vd_vari<TriView, R1, C1, R2, C2>::chain()’:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/mdivide_left_tri.hpp:218:46: error: ‘class Eigen::Map<Eigen::Matrix<stan::math::vari_value<double>*, -1, -1> >’ has no member named ‘adj’
  218 |     adjC = Map<matrix_vi>(variRefC_, M_, N_).adj();
      |                                              ^~~
In file included from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun.hpp:171,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev.hpp:10,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math.hpp:19,
                 from foo.cpp:2:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/squared_distance.hpp: In member function ‘virtual void stan::math::internal::squared_distance_vv_vari::chain()’:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/squared_distance.hpp:79:38: error: ‘class Eigen::Map<Eigen::Matrix<stan::math::vari_value<double>*, -1, 1> >’ has no member named ‘val’; did you mean ‘eval’?
   79 |     vector_d di = 2 * adj_ * (v1_map.val() - v2_map.val());
      |                                      ^~~
      |                                      eval
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/squared_distance.hpp:79:53: error: ‘class Eigen::Map<Eigen::Matrix<stan::math::vari_value<double>*, -1, 1> >’ has no member named ‘val’; did you mean ‘eval’?
   79 |     vector_d di = 2 * adj_ * (v1_map.val() - v2_map.val());
      |                                                     ^~~
      |                                                     eval
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/squared_distance.hpp:80:12: error: ‘class Eigen::Map<Eigen::Matrix<stan::math::vari_value<double>*, -1, 1> >’ has no member named ‘adj’
   80 |     v1_map.adj() += di;
      |            ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/squared_distance.hpp:81:12: error: ‘class Eigen::Map<Eigen::Matrix<stan::math::vari_value<double>*, -1, 1> >’ has no member named ‘adj’
   81 |     v2_map.adj() -= di;
      |            ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/squared_distance.hpp: In member function ‘virtual void stan::math::internal::squared_distance_vd_vari::chain()’:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/squared_distance.hpp:110:12: error: ‘class Eigen::Map<Eigen::Matrix<stan::math::vari_value<double>*, -1, 1> >’ has no member named ‘adj’
  110 |     v1_map.adj()
      |            ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/squared_distance.hpp:111:31: error: ‘class Eigen::Map<Eigen::Matrix<stan::math::vari_value<double>*, -1, 1> >’ has no member named ‘val’; did you mean ‘eval’?
  111 |         += 2 * adj_ * (v1_map.val() - Eigen::Map<vector_d>(v2_, length_));
      |                               ^~~
      |                               eval
In file included from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun.hpp:194,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev.hpp:10,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math.hpp:19,
                 from foo.cpp:2:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/variance.hpp: In function ‘stan::math::var stan::math::internal::calc_variance(size_t, const var*)’:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/variance.hpp:22:49: error: ‘class Eigen::Map<const Eigen::Matrix<stan::math::var_value<double>, -1, 1>, 0, Eigen::Stride<0, 0> >’ has no member named ‘vi’
   22 |   Eigen::Map<vector_vi>(varis, size) = dtrs_map.vi();
      |                                                 ^~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/fun/variance.hpp:23:33: error: ‘class Eigen::Map<const Eigen::Matrix<stan::math::var_value<double>, -1, 1>, 0, Eigen::Stride<0, 0> >’ has no member named ‘val’; did you mean ‘eval’?
   23 |   vector_d dtrs_vals = dtrs_map.val();
      |                                 ^~~
      |                                 eval
In file included from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/algebra_system.hpp:5,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/algebra_solver_fp.hpp:7,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor.hpp:4,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev.hpp:11,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math.hpp:19,
                 from foo.cpp:2:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/jacobian.hpp: In function ‘void stan::math::jacobian(const F&, const Eigen::Matrix<double, -1, 1>&, Eigen::Matrix<double, -1, 1>&, Eigen::Matrix<double, -1, -1>&)’:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/jacobian.hpp:26:15: error: ‘class Eigen::Matrix<stan::math::var_value<double>, -1, 1>’ has no member named ‘val’; did you mean ‘eval’?
   26 |   fx = fx_var.val();
      |               ^~~
      |               eval
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/jacobian.hpp:28:20: error: ‘class Eigen::Matrix<stan::math::var_value<double>, -1, 1>’ has no member named ‘adj’
   28 |   J.col(0) = x_var.adj();
      |                    ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/jacobian.hpp:32:22: error: ‘class Eigen::Matrix<stan::math::var_value<double>, -1, 1>’ has no member named ‘adj’
   32 |     J.col(i) = x_var.adj();
      |                      ^~~
In file included from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor.hpp:5,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev.hpp:11,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math.hpp:19,
                 from foo.cpp:2:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/solve_powell.hpp: In lambda function:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/solve_powell.hpp:369:53: error: ‘stan::arena_t<Eigen::Matrix<stan::math::var_value<double>, -1, -1> >’ {aka ‘class stan::math::arena_matrix<Eigen::Matrix<stan::math::var_value<double>, -1, -1> >’} has no member named ‘adj’
  369 |     Eigen::VectorXd eta = -Jf_x_T_lu_ptr->solve(ret.adj().eval());
      |                                                     ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/solve_powell.hpp:375:37: error: ‘stan::arena_t<Eigen::Matrix<stan::math::var_value<double>, -1, -1> >’ {aka ‘class stan::math::arena_matrix<Eigen::Matrix<stan::math::var_value<double>, -1, -1> >’} has no member named ‘val’; did you mean ‘eval’?
  375 |       Eigen::VectorXd ret_val = ret.val();
      |                                     ^~~
      |                                     eval
In file included from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor.hpp:6,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev.hpp:11,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math.hpp:19,
                 from foo.cpp:2:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/solve_newton.hpp: In lambda function:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/solve_newton.hpp:182:57: error: ‘stan::arena_t<Eigen::Matrix<stan::math::var_value<double>, -1, -1> >’ {aka ‘class stan::math::arena_matrix<Eigen::Matrix<stan::math::var_value<double>, -1, -1> >’} has no member named ‘adj’
  182 |         Eigen::VectorXd eta = -Jf_x_T_lu_ptr->solve(ret.adj().eval());
      |                                                         ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/solve_newton.hpp:189:41: error: ‘stan::arena_t<Eigen::Matrix<stan::math::var_value<double>, -1, -1> >’ {aka ‘class stan::math::arena_matrix<Eigen::Matrix<stan::math::var_value<double>, -1, -1> >’} has no member named ‘val’; did you mean ‘eval’?
  189 |           Eigen::VectorXd ret_val = ret.val();
      |                                         ^~~
      |                                         eval
In file included from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor.hpp:11,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev.hpp:11,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math.hpp:19,
                 from foo.cpp:2:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/coupled_ode_system.hpp: In member function ‘void stan::math::coupled_ode_system_impl<false, F, T_y0, Args ...>::operator()(const std::vector<double, std::allocator<double> >&, std::vector<double, std::allocator<double> >&, double)’:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/coupled_ode_system.hpp:140:28: error: ‘class Eigen::Matrix<stan::math::var_value<double>, -1, 1>’ has no member named ‘adj’
  140 |       y_adjoints_ = y_vars.adj();
      |                            ^~~
In file included from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor.hpp:14,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev.hpp:11,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math.hpp:19,
                 from foo.cpp:2:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/gradient.hpp: In function ‘void stan::math::gradient(const F&, const Eigen::Matrix<double, -1, 1>&, double&, Eigen::Matrix<double, -1, 1>&)’:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/gradient.hpp:55:19: error: ‘class Eigen::Matrix<stan::math::var_value<double>, -1, 1>’ has no member named ‘adj’
   55 |   grad_fx = x_var.adj();
      |                   ^~~
In file included from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/ode_adjoint.hpp:6,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor.hpp:21,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math/rev.hpp:11,
                 from /mnt/hgfs/Shared/StanMathLib/math/stan/math.hpp:19,
                 from foo.cpp:2:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/cvodes_integrator_adjoint.hpp: In member function ‘int stan::math::cvodes_integrator_adjoint_vari<F, T_y0, T_t0, T_ts, T_Args>::rhs_adj(double, N_Vector, N_Vector, N_Vector) const’:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/cvodes_integrator_adjoint.hpp:604:16: error: ‘class Eigen::Matrix<stan::math::var_value<double>, -1, 1>’ has no member named ‘adj’
  604 |     f_y_t_vars.adj() = -Eigen::Map<Eigen::VectorXd>(NV_DATA_S(yB), N_);
      |                ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/cvodes_integrator_adjoint.hpp:606:64: error: ‘class Eigen::Matrix<stan::math::var_value<double>, -1, 1>’ has no member named ‘adj’
  606 |     Eigen::Map<Eigen::VectorXd>(NV_DATA_S(yBdot), N_) = y_vars.adj();
      |                                                                ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/cvodes_integrator_adjoint.hpp: In member function ‘int stan::math::cvodes_integrator_adjoint_vari<F, T_y0, T_t0, T_ts, T_Args>::quad_rhs_adj(double, N_Vector, N_Vector, N_Vector)’:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/cvodes_integrator_adjoint.hpp:643:16: error: ‘class Eigen::Matrix<stan::math::var_value<double>, -1, 1>’ has no member named ‘adj’
  643 |     f_y_t_vars.adj() = -Eigen::Map<Eigen::VectorXd>(NV_DATA_S(yB), N_);
      |                ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/cvodes_integrator_adjoint.hpp: In member function ‘int stan::math::cvodes_integrator_adjoint_vari<F, T_y0, T_t0, T_ts, T_Args>::jacobian_rhs_states(double, N_Vector, SUNMatrix) const’:
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/cvodes_integrator_adjoint.hpp:680:24: error: ‘class Eigen::Matrix<stan::math::var_value<double>, -1, 1>’ has no member named ‘adj’
  680 |     Jfy.col(0) = y_var.adj();
      |                        ^~~
/mnt/hgfs/Shared/StanMathLib/math/stan/math/rev/functor/cvodes_integrator_adjoint.hpp:684:26: error: ‘class Eigen::Matrix<stan::math::var_value<double>, -1, 1>’ has no member named ‘adj’
  684 |       Jfy.col(i) = y_var.adj();
      |                          ^~~
make: *** [<builtin>: foo] Error 1

Expected Output

See the example above.

I'm building a program where there are some header files that does not rely on stan but must include eigen. If these header files are included, same error generates.

Current Version:

v4.5.0 gcc version 11.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04)

Thanks!

rok-cesnovar commented 1 year ago

How about if you reorder the includes to

include <stan/math.hpp>

include <Eigen/Dense> // ! Compilation error if this line is added

include

Also, Eigen/Dense should already be included if you include stan/math.hpp

MessyShen commented 1 year ago

How about if you reorder the includes to

include <stan/math.hpp> #include <Eigen/Dense> // ! Compilation error if this line is added #include

Also, Eigen/Dense should already be included if you include stan/math.hpp

Thanks! By changing the order this example compiles correctly. But I don't know why the order matters here and I still have this issue for a more complex program if I put #include<stan/math.hpp> on top of some certain .hpp file. I'll try to provide a minimal example for this ASAP. It would be nice if you (or someone else) can explain this or fix the order issue. Thanks again for replying ;-)

wds15 commented 1 year ago

Stan-math uses Eigen plugins and is thereby a bit difficult wrt to header inclusion with Eigen. It's better to include first Stan-math (and then you anyway got Eigen included).

rok-cesnovar commented 1 year ago

The reason is that Stan Math adds some things to the Eigen namespace https://github.com/stan-dev/math/blob/develop/stan/math/prim/fun/Eigen.hpp So that has to be included first no matter what.

The most general workaround for this, if you do not want to change the order of the includes, is to include that specific file I linked (stan/math/prim/fun/Eigen.hpp) manually in the compile command with a -include ...

MessyShen commented 1 year ago

Thanks! It solves my problem! Thanks for your quick reply.