stan-dev / stan

Stan development repository. The master branch contains the current release. The develop branch contains the latest stable development. See the Developer Process Wiki for details.
https://mc-stan.org
BSD 3-Clause "New" or "Revised" License
2.61k stars 370 forks source link

conditional operator parses but fails to compile #2101

Closed bob-carpenter closed 7 years ago

bob-carpenter commented 8 years ago

Summary:

Example of ternary operator that translates to C++ but fails to compile.

Reproducible Steps:

functions{

  row_vector vecpower(row_vector vec, real power){
    row_vector[num_elements(vec)] out;
    for(i in 1:num_elements(vec)){
      out[i] = vec[i]^power;
    }
    return out;
  }

  vector vecsqrt(vector vec){
    vector[num_elements(vec)] out;
    for(i in 1:num_elements(vec)){
      out[i]=sqrt(vec[i]);
    }
    return out;
  }

  matrix sdpcorr2cov_lp(matrix mat,int chol, real eta){ //converts from lower pcor and diag sd to chol or cov matrix
    int ndim;
    matrix[rows(mat),cols(mat)] mscale;
    matrix[rows(mat),cols(mat)] mcholcor;
    matrix[rows(mat),cols(mat)] out;
    real tempsum;
    real betad;

    ndim = cols(mat);
    mcholcor=diag_matrix(rep_vector(0,ndim));
    mscale = diag_matrix(diagonal(mat));
    betad = eta + (ndim-1)/2;

    if(ndim > 1){
      for(coli in 1:(ndim-1)){
        betad = betad - 1/2;
        for(rowi in coli:ndim){

          if(rowi > coli){
            mat[rowi,coli]/2+.5 ~ beta(betad,betad);
            mcholcor[rowi,coli] =  mat[rowi,coli]; 
            mcholcor[coli,rowi] = 0;
          }
          if(coli > 1){
            mcholcor[rowi,coli] = mcholcor[rowi,coli] * (rowi==coli ? 0 : mat[rowi,coli-1]) * sqrt(1-square(mcholcor[rowi,coli-1]));
          }

        }
      }
    }

    //out = mscale * mcholcor; //cholesky

    //if(chol == 0) out=multiply_lower_tri_self_transpose(out); //cov
    return mcholcor;
  } //end sdpcorr2cov

}

data{
  int ndim;
  real eta;
}

parameters{
  vector<lower=-1,upper=1>[(ndim*ndim-ndim)/2] pcorrvec;
  real dummy;
}

transformed parameters{
  vector[ndim] scalevec;
  matrix[ndim,ndim] sdpcorrmat;
  matrix[ndim,ndim] cholcorrmat;
  scalevec=rep_vector(1,ndim);

  {
    int counter;
    counter=0;
    sdpcorrmat=diag_matrix(scalevec);

    for(rowi in 2:ndim){ // fill lower tri of sdpcorrmat
      for(coli in 1:(rowi-1)){
        counter=counter+1;
        sdpcorrmat[rowi,coli]=pcorrvec[counter];
      }
    }
  }

  cholcorrmat = sdpcorr2cov_lp(sdpcorrmat,1,eta); //convert sd and lower tri partial cor matrix to cholesky factor cov

}
model{
  dummy~normal(0,1); //just putting something here...
  //pcorrvec~normal(0,1);
}
generated quantities{
  matrix[ndim,ndim] corrmat_out;
  corrmat_out = cholcorrmat * cholcorrmat'; //the matrix of actual interest
}

Current Output:

> stan_model("bug.stan")
DIAGNOSTIC(S) FROM PARSER:
Warning: integer division implicitly rounds to integer. Found int division: (ndim - 1) / 2
 Positive values rounded down, negative values rounded up or down in platform-dependent way.
Warning: integer division implicitly rounds to integer. Found int division: 1 / 2
 Positive values rounded down, negative values rounded up or down in platform-dependent way.
Warning: integer division implicitly rounds to integer. Found int division: ((ndim * ndim) - ndim) / 2
 Positive values rounded down, negative values rounded up or down in platform-dependent way.

In file included from file174be6fb66fea.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:9:
In file included from /Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/stan/math/prim/mat.hpp:133:
In file included from /Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/promote_scalar.hpp:4:
/Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/stan/math/prim/scal/fun/promote_scalar.hpp:32:16: error: cannot convert 'stan::math::var' to 'double' without a conversion operator
        return T(x);
               ^~~
/Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/stan/math/prim/scal/fun/promote_scalar.hpp:68:43: note: in instantiation of member function 'stan::math::promote_scalar_struct<double, stan::math::var>::apply' requested here
      return promote_scalar_struct<T, S>::apply(x);
                                          ^
file174be6fb66fea.cpp:165:220: note: in instantiation of function template specialization 'stan::math::promote_scalar<double, stan::math::var>' requested here
                            stan::math::assign(get_base1_lhs(mcholcor,rowi,coli,"mcholcor",1), ((get_base1(mcholcor,rowi,coli,"mcholcor",1) * (logical_eq(rowi,coli) ? stan::math::promote_scalar<double>(0) : stan::math::promote_scalar<double>(get_base1(mat,rowi,(coli - 1),"mat",1)) )) * sqrt((1 - square(get_base1(mcholcor,rowi,(coli - 1),"mcholcor",1))))));
                                                                                                                                                                                                                           ^
file174be6fb66fea.cpp:380:45: note: in instantiation of function template specialization 'model174be4c85f95d_bug_namespace::sdpcorr2cov_lp<stan::math::var, double, stan::math::var, stan::math::accumulator<stan::math::var> >' requested here
            stan::math::assign(cholcorrmat, sdpcorr2cov_lp(sdpcorrmat,1,eta, lp__, lp_accum__, pstream__));
                                            ^
file174be6fb66fea.cpp:440:14: note: in instantiation of function template specialization 'model174be4c85f95d_bug_namespace::model174be4c85f95d_bug::log_prob<true, true, stan::math::var>' requested here
      return log_prob<propto,jacobian,T_>(vec_params_r, vec_params_i, pstream);
             ^
/Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/src/stan/model/util.hpp:402:31: note: in instantiation of function template specialization 'model174be4c85f95d_bug_namespace::model174be4c85f95d_bug::log_prob<true, true, stan::math::var>' requested here
        return model.template log_prob<true, true, T>(x, o);
                              ^
/Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/stan/math/rev/mat/functor/gradient.hpp:51:22: note: (skipping 3 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)
        var fx_var = f(x_var);
                     ^
/Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/src/stan/services/init/initialize_state.hpp:177:16: note: in instantiation of function template specialization 'stan::services::init::initialize_state_values<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug>' requested here
        return initialize_state_values(cont_params, model, writer);
               ^
/Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/src/stan/services/init/initialize_state.hpp:429:20: note: in instantiation of function template specialization 'stan::services::init::initialize_state_zero<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug>' requested here
            return initialize_state_zero(cont_params, model, writer);
                   ^
/Library/Frameworks/R.framework/Versions/3.2/Resources/library/rstan/include/rstan/stan_fit.hpp:700:36: note: in instantiation of function template specialization 'stan::services::init::initialize_state<rstan::io::rlist_ref_var_context_factory, model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>, boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399> > >' requested here
        if (!stan::services::init::initialize_state(init,
                                   ^
/Library/Frameworks/R.framework/Versions/3.2/Resources/library/rstan/include/rstan/stan_fit.hpp:1496:13: note: in instantiation of function template specialization 'rstan::(anonymous namespace)::sampler_command<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>, boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399> > >' requested here
      ret = sampler_command(args, model_, holder, names_oi_tidx_,
            ^
file174be6fb66fea.cpp:720:116: note: in instantiation of member function 'rstan::stan_fit<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>, boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399> > >::call_sampler' requested here
            &rstan::stan_fit<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::ecuyer1988>::call_sampler)
                                                                                                                   ^
1 error generated.
make: *** [file174be6fb66fea.o] Error 1

ERROR(s) during compilation: source code errors or compiler configuration errors!

Program source:
  1: 
  2: // includes from the plugin
  3: 
  4: 
  5: // user includes
  6: #define STAN__SERVICES__COMMAND_HPP// Code generated by Stan version 2.12
  7: 
  8: #include <stan/model/model_header.hpp>
  9: 
 10: namespace model174be4c85f95d_bug_namespace {
 11: 
 12: using std::istream;
 13: using std::string;
 14: using std::stringstream;
 15: using std::vector;
 16: using stan::io::dump;
 17: using stan::math::lgamma;
 18: using stan::model::prob_grad;
 19: using namespace stan::math;
 20: 
 21: typedef Eigen::Matrix<double,Eigen::Dynamic,1> vector_d;
 22: typedef Eigen::Matrix<double,1,Eigen::Dynamic> row_vector_d;
 23: typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> matrix_d;
 24: 
 25: static int current_statement_begin__;
 26: 
 27: template <typename T0__, typename T1__>
 28: inline
 29: Eigen::Matrix<typename boost::math::tools::promote_args<T0__, T1__>::type, 1,Eigen::Dynamic>
 30: vecpower(const Eigen::Matrix<T0__, 1,Eigen::Dynamic>& vec,
 31:              const T1__& power, std::ostream* pstream__) {
 32:     typedef typename boost::math::tools::promote_args<T0__, T1__>::type fun_scalar_t__;
 33:     typedef fun_scalar_t__ fun_return_scalar_t__;
 34:     const static bool propto__ = true;
 35:     (void) propto__;
 36:     int current_statement_begin__ = -1;
 37:     try {
 38:         {
 39:             Eigen::Matrix<fun_scalar_t__,1,Eigen::Dynamic>  out(static_cast<Eigen::VectorXd::Index>(num_elements(vec)));
 40:             (void) out;  // dummy to suppress unused var warning
 41:             stan::math::initialize(out, std::numeric_limits<double>::quiet_NaN());
 42:             current_statement_begin__ = 5;
 43:             for (int i = 1; i <= num_elements(vec); ++i) {
 44:                 current_statement_begin__ = 6;
 45:                 stan::math::assign(get_base1_lhs(out,i,"out",1), pow(get_base1(vec,i,"vec",1),power));
 46:             }
 47:             current_statement_begin__ = 8;
 48:             return stan::math::promote_scalar<fun_return_scalar_t__>(out);
 49:         }
 50:     } catch (const std::exception& e) {
 51:         stan::lang::rethrow_located(e,current_statement_begin__);
 52:         // Next line prevents compiler griping about no return
 53:         throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
 54:     }
 55: }
 56: 
 57: 
 58: struct vecpower_functor__ {
 59:     template <typename T0__, typename T1__>
 60:     inline
 61:     Eigen::Matrix<typename boost::math::tools::promote_args<T0__, T1__>::type, 1,Eigen::Dynamic>
 62:     operator()(const Eigen::Matrix<T0__, 1,Eigen::Dynamic>& vec,
 63:              const T1__& power, std::ostream* pstream__) const {
 64:         return vecpower(vec, power, pstream__);
 65:     }
 66: };
 67: 
 68: template <typename T0__>
 69: inline
 70: Eigen::Matrix<typename boost::math::tools::promote_args<T0__>::type, Eigen::Dynamic,1>
 71: vecsqrt(const Eigen::Matrix<T0__, Eigen::Dynamic,1>& vec, std::ostream* pstream__) {
 72:     typedef typename boost::math::tools::promote_args<T0__>::type fun_scalar_t__;
 73:     typedef fun_scalar_t__ fun_return_scalar_t__;
 74:     const static bool propto__ = true;
 75:     (void) propto__;
 76:     int current_statement_begin__ = -1;
 77:     try {
 78:         {
 79:             Eigen::Matrix<fun_scalar_t__,Eigen::Dynamic,1>  out(static_cast<Eigen::VectorXd::Index>(num_elements(vec)));
 80:             (void) out;  // dummy to suppress unused var warning
 81:             stan::math::initialize(out, std::numeric_limits<double>::quiet_NaN());
 82:             current_statement_begin__ = 13;
 83:             for (int i = 1; i <= num_elements(vec); ++i) {
 84:                 current_statement_begin__ = 14;
 85:                 stan::math::assign(get_base1_lhs(out,i,"out",1), sqrt(get_base1(vec,i,"vec",1)));
 86:             }
 87:             current_statement_begin__ = 16;
 88:             return stan::math::promote_scalar<fun_return_scalar_t__>(out);
 89:         }
 90:     } catch (const std::exception& e) {
 91:         stan::lang::rethrow_located(e,current_statement_begin__);
 92:         // Next line prevents compiler griping about no return
 93:         throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
 94:     }
 95: }
 96: 
 97: 
 98: struct vecsqrt_functor__ {
 99:     template <typename T0__>
100:     inline
101:     Eigen::Matrix<typename boost::math::tools::promote_args<T0__>::type, Eigen::Dynamic,1>
102:     operator()(const Eigen::Matrix<T0__, Eigen::Dynamic,1>& vec, std::ostream* pstream__) const {
103:         return vecsqrt(vec, pstream__);
104:     }
105: };
106: 
107: template <typename T0__, typename T2__, typename T_lp__, typename T_lp_accum__>
108: inline
109: Eigen::Matrix<typename boost::math::tools::promote_args<T0__, T2__, T_lp__>::type, Eigen::Dynamic,Eigen::Dynamic>
110: sdpcorr2cov_lp(const Eigen::Matrix<T0__, Eigen::Dynamic,Eigen::Dynamic>& mat,
111:                    const int& chol,
112:                    const T2__& eta, T_lp__& lp__, T_lp_accum__& lp_accum__, std::ostream* pstream__) {
113:     typedef typename boost::math::tools::promote_args<T0__, T2__, T_lp__>::type fun_scalar_t__;
114:     typedef fun_scalar_t__ fun_return_scalar_t__;
115:     const static bool propto__ = true;
116:     (void) propto__;
117:     int current_statement_begin__ = -1;
118:     try {
119:         {
120:             int ndim(0);
121:             (void) ndim;  // dummy to suppress unused var warning
122:             Eigen::Matrix<fun_scalar_t__,Eigen::Dynamic,Eigen::Dynamic>  mscale(static_cast<Eigen::VectorXd::Index>(rows(mat)),static_cast<Eigen::VectorXd::Index>(cols(mat)));
123:             (void) mscale;  // dummy to suppress unused var warning
124:             Eigen::Matrix<fun_scalar_t__,Eigen::Dynamic,Eigen::Dynamic>  mcholcor(static_cast<Eigen::VectorXd::Index>(rows(mat)),static_cast<Eigen::VectorXd::Index>(cols(mat)));
125:             (void) mcholcor;  // dummy to suppress unused var warning
126:             Eigen::Matrix<fun_scalar_t__,Eigen::Dynamic,Eigen::Dynamic>  out(static_cast<Eigen::VectorXd::Index>(rows(mat)),static_cast<Eigen::VectorXd::Index>(cols(mat)));
127:             (void) out;  // dummy to suppress unused var warning
128:             fun_scalar_t__ tempsum;
129:             (void) tempsum;  // dummy to suppress unused var warning
130:             fun_scalar_t__ betad;
131:             (void) betad;  // dummy to suppress unused var warning
132:             stan::math::initialize(mscale, std::numeric_limits<double>::quiet_NaN());
133:             stan::math::initialize(mcholcor, std::numeric_limits<double>::quiet_NaN());
134:             stan::math::initialize(out, std::numeric_limits<double>::quiet_NaN());
135:             stan::math::initialize(tempsum, std::numeric_limits<double>::quiet_NaN());
136:             stan::math::initialize(betad, std::numeric_limits<double>::quiet_NaN());
137:             current_statement_begin__ = 27;
138:             stan::math::assign(ndim, cols(mat));
139:             current_statement_begin__ = 28;
140:             stan::math::assign(mcholcor, diag_matrix(rep_vector(0,ndim)));
141:             current_statement_begin__ = 29;
142:             stan::math::assign(mscale, diag_matrix(diagonal(mat)));
143:             current_statement_begin__ = 30;
144:             stan::math::assign(betad, (eta + divide((ndim - 1),2)));
145:             current_statement_begin__ = 32;
146:             if (as_bool(logical_gt(ndim,1))) {
147:                 current_statement_begin__ = 33;
148:                 for (int coli = 1; coli <= (ndim - 1); ++coli) {
149:                     current_statement_begin__ = 34;
150:                     stan::math::assign(betad, (betad - divide(1,2)));
151:                     current_statement_begin__ = 35;
152:                     for (int rowi = coli; rowi <= ndim; ++rowi) {
153:                         current_statement_begin__ = 37;
154:                         if (as_bool(logical_gt(rowi,coli))) {
155:                             current_statement_begin__ = 38;
156:                             lp_accum__.add(beta_log<propto__>(((get_base1(mat,rowi,coli,"mat",1) / 2) + 0.5), betad, betad));
157:                             current_statement_begin__ = 39;
158:                             stan::math::assign(get_base1_lhs(mcholcor,rowi,coli,"mcholcor",1), get_base1(mat,rowi,coli,"mat",1));
159:                             current_statement_begin__ = 40;
160:                             stan::math::assign(get_base1_lhs(mcholcor,coli,rowi,"mcholcor",1), 0);
161:                         }
162:                         current_statement_begin__ = 42;
163:                         if (as_bool(logical_gt(coli,1))) {
164:                             current_statement_begin__ = 43;
165:                             stan::math::assign(get_base1_lhs(mcholcor,rowi,coli,"mcholcor",1), ((get_base1(mcholcor,rowi,coli,"mcholcor",1) * (logical_eq(rowi,coli) ? stan::math::promote_scalar<double>(0) : stan::math::promote_scalar<double>(get_base1(mat,rowi,(coli - 1),"mat",1)) )) * sqrt((1 - square(get_base1(mcholcor,rowi,(coli - 1),"mcholcor",1))))));
166:                         }
167:                     }
168:                 }
169:             }
170:             current_statement_begin__ = 55;
171:             return stan::math::promote_scalar<fun_return_scalar_t__>(mcholcor);
172:         }
173:     } catch (const std::exception& e) {
174:         stan::lang::rethrow_located(e,current_statement_begin__);
175:         // Next line prevents compiler griping about no return
176:         throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
177:     }
178: }
179: 
180: 
181: struct sdpcorr2cov_lp_functor__ {
182:     template <typename T0__, typename T2__, typename T_lp__, typename T_lp_accum__>
183:     inline
184:     Eigen::Matrix<typename boost::math::tools::promote_args<T0__, T2__, T_lp__>::type, Eigen::Dynamic,Eigen::Dynamic>
185:     operator()(const Eigen::Matrix<T0__, Eigen::Dynamic,Eigen::Dynamic>& mat,
186:                    const int& chol,
187:                    const T2__& eta, T_lp__& lp__, T_lp_accum__& lp_accum__, std::ostream* pstream__) const {
188:         return sdpcorr2cov_lp(mat, chol, eta, lp__, lp_accum__, pstream__);
189:     }
190: };
191: 
192: class model174be4c85f95d_bug : public prob_grad {
193: private:
194:     int ndim;
195:     double eta;
196: public:
197:     model174be4c85f95d_bug(stan::io::var_context& context__,
198:         std::ostream* pstream__ = 0)
199:         : prob_grad(0) {
200:         typedef boost::ecuyer1988 rng_t;
201:         rng_t base_rng(0);  // 0 seed default
202:         ctor_body(context__, base_rng, pstream__);
203:     }
204: 
205:     template <class RNG>
206:     model174be4c85f95d_bug(stan::io::var_context& context__,
207:         RNG& base_rng__,
208:         std::ostream* pstream__ = 0)
209:         : prob_grad(0) {
210:         ctor_body(context__, base_rng__, pstream__);
211:     }
212: 
213:     template <class RNG>
214:     void ctor_body(stan::io::var_context& context__,
215:                    RNG& base_rng__,
216:                    std::ostream* pstream__) {
217:         current_statement_begin__ = -1;
218: 
219:         static const char* function__ = "model174be4c85f95d_bug_namespace::model174be4c85f95d_bug";
220:         (void) function__; // dummy call to supress warning
221:         size_t pos__;
222:         (void) pos__; // dummy call to supress warning
223:         std::vector<int> vals_i__;
224:         std::vector<double> vals_r__;
225:         context__.validate_dims("data initialization", "ndim", "int", context__.to_vec());
226:         ndim = int(0);
227:         vals_i__ = context__.vals_i("ndim");
228:         pos__ = 0;
229:         ndim = vals_i__[pos__++];
230:         context__.validate_dims("data initialization", "eta", "double", context__.to_vec());
231:         eta = double(0);
232:         vals_r__ = context__.vals_r("eta");
233:         pos__ = 0;
234:         eta = vals_r__[pos__++];
235: 
236:         // validate data
237: 
238:         double DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
239:         (void) DUMMY_VAR__;  // suppress unused var warning
240: 
241: 
242:         // initialize transformed variables to avoid seg fault on val access
243: 
244:         try {
245:         } catch (const std::exception& e) {
246:             stan::lang::rethrow_located(e,current_statement_begin__);
247:             // Next line prevents compiler griping about no return
248:             throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
249:         }
250: 
251:         // validate transformed data
252: 
253:         // set parameter ranges
254:         num_params_r__ = 0U;
255:         param_ranges_i__.clear();
256:         num_params_r__ += divide(((ndim * ndim) - ndim),2);
257:         ++num_params_r__;
258:     }
259: 
260:     ~model174be4c85f95d_bug() { }
261: 
262: 
263:     void transform_inits(const stan::io::var_context& context__,
264:                          std::vector<int>& params_i__,
265:                          std::vector<double>& params_r__,
266:                          std::ostream* pstream__) const {
267:         stan::io::writer<double> writer__(params_r__,params_i__);
268:         size_t pos__;
269:         (void) pos__; // dummy call to supress warning
270:         std::vector<double> vals_r__;
271:         std::vector<int> vals_i__;
272: 
273:         if (!(context__.contains_r("pcorrvec")))
274:             throw std::runtime_error("variable pcorrvec missing");
275:         vals_r__ = context__.vals_r("pcorrvec");
276:         pos__ = 0U;
277:         context__.validate_dims("initialization", "pcorrvec", "vector_d", context__.to_vec(divide(((ndim * ndim) - ndim),2)));
278:         vector_d pcorrvec(static_cast<Eigen::VectorXd::Index>(divide(((ndim * ndim) - ndim),2)));
279:         for (int j1__ = 0U; j1__ < divide(((ndim * ndim) - ndim),2); ++j1__)
280:             pcorrvec(j1__) = vals_r__[pos__++];
281:         try {
282:             writer__.vector_lub_unconstrain(-(1),1,pcorrvec);
283:         } catch (const std::exception& e) { 
284:             throw std::runtime_error(std::string("Error transforming variable pcorrvec: ") + e.what());
285:         }
286: 
287:         if (!(context__.contains_r("dummy")))
288:             throw std::runtime_error("variable dummy missing");
289:         vals_r__ = context__.vals_r("dummy");
290:         pos__ = 0U;
291:         context__.validate_dims("initialization", "dummy", "double", context__.to_vec());
292:         double dummy(0);
293:         dummy = vals_r__[pos__++];
294:         try {
295:             writer__.scalar_unconstrain(dummy);
296:         } catch (const std::exception& e) { 
297:             throw std::runtime_error(std::string("Error transforming variable dummy: ") + e.what());
298:         }
299: 
300:         params_r__ = writer__.data_r();
301:         params_i__ = writer__.data_i();
302:     }
303: 
304:     void transform_inits(const stan::io::var_context& context,
305:                          Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
306:                          std::ostream* pstream__) const {
307:       std::vector<double> params_r_vec;
308:       std::vector<int> params_i_vec;
309:       transform_inits(context, params_i_vec, params_r_vec, pstream__);
310:       params_r.resize(params_r_vec.size());
311:       for (int i = 0; i < params_r.size(); ++i)
312:         params_r(i) = params_r_vec[i];
313:     }
314: 
315: 
316:     template <bool propto__, bool jacobian__, typename T__>
317:     T__ log_prob(vector<T__>& params_r__,
318:                  vector<int>& params_i__,
319:                  std::ostream* pstream__ = 0) const {
320: 
321:         T__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
322:         (void) DUMMY_VAR__;  // suppress unused var warning
323: 
324:         T__ lp__(0.0);
325:         stan::math::accumulator<T__> lp_accum__;
326: 
327:         // model parameters
328:         stan::io::reader<T__> in__(params_r__,params_i__);
329: 
330:         Eigen::Matrix<T__,Eigen::Dynamic,1>  pcorrvec;
331:         (void) pcorrvec;  // dummy to suppress unused var warning
332:         if (jacobian__)
333:             pcorrvec = in__.vector_lub_constrain(-(1),1,divide(((ndim * ndim) - ndim),2),lp__);
334:         else
335:             pcorrvec = in__.vector_lub_constrain(-(1),1,divide(((ndim * ndim) - ndim),2));
336: 
337:         T__ dummy;
338:         (void) dummy;  // dummy to suppress unused var warning
339:         if (jacobian__)
340:             dummy = in__.scalar_constrain(lp__);
341:         else
342:             dummy = in__.scalar_constrain();
343: 
344: 
345:         // transformed parameters
346:         Eigen::Matrix<T__,Eigen::Dynamic,1>  scalevec(static_cast<Eigen::VectorXd::Index>(ndim));
347:         (void) scalevec;  // dummy to suppress unused var warning
348:         Eigen::Matrix<T__,Eigen::Dynamic,Eigen::Dynamic>  sdpcorrmat(static_cast<Eigen::VectorXd::Index>(ndim),static_cast<Eigen::VectorXd::Index>(ndim));
349:         (void) sdpcorrmat;  // dummy to suppress unused var warning
350:         Eigen::Matrix<T__,Eigen::Dynamic,Eigen::Dynamic>  cholcorrmat(static_cast<Eigen::VectorXd::Index>(ndim),static_cast<Eigen::VectorXd::Index>(ndim));
351:         (void) cholcorrmat;  // dummy to suppress unused var warning
352: 
353:         // initialize transformed variables to avoid seg fault on val access
354:         stan::math::fill(scalevec,DUMMY_VAR__);
355:         stan::math::fill(sdpcorrmat,DUMMY_VAR__);
356:         stan::math::fill(cholcorrmat,DUMMY_VAR__);
357: 
358:         try {
359:             current_statement_begin__ = 74;
360:             stan::math::assign(scalevec, rep_vector(1,ndim));
361:             {
362:                 int counter(0);
363:                 (void) counter;  // dummy to suppress unused var warning
364:                 current_statement_begin__ = 78;
365:                 stan::math::assign(counter, 0);
366:                 current_statement_begin__ = 79;
367:                 stan::math::assign(sdpcorrmat, diag_matrix(scalevec));
368:                 current_statement_begin__ = 81;
369:                 for (int rowi = 2; rowi <= ndim; ++rowi) {
370:                     current_statement_begin__ = 82;
371:                     for (int coli = 1; coli <= (rowi - 1); ++coli) {
372:                         current_statement_begin__ = 83;
373:                         stan::math::assign(counter, (counter + 1));
374:                         current_statement_begin__ = 84;
375:                         stan::math::assign(get_base1_lhs(sdpcorrmat,rowi,coli,"sdpcorrmat",1), get_base1(pcorrvec,counter,"pcorrvec",1));
376:                     }
377:                 }
378:             }
379:             current_statement_begin__ = 89;
380:             stan::math::assign(cholcorrmat, sdpcorr2cov_lp(sdpcorrmat,1,eta, lp__, lp_accum__, pstream__));
381:         } catch (const std::exception& e) {
382:             stan::lang::rethrow_located(e,current_statement_begin__);
383:             // Next line prevents compiler griping about no return
384:             throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
385:         }
386: 
387:         // validate transformed parameters
388:         for (int i0__ = 0; i0__ < ndim; ++i0__) {
389:             if (stan::math::is_uninitialized(scalevec(i0__))) {
390:                 std::stringstream msg__;
391:                 msg__ << "Undefined transformed parameter: scalevec" << '[' << i0__ << ']';
392:                 throw std::runtime_error(msg__.str());
393:             }
394:         }
395:         for (int i0__ = 0; i0__ < ndim; ++i0__) {
396:             for (int i1__ = 0; i1__ < ndim; ++i1__) {
397:                 if (stan::math::is_uninitialized(sdpcorrmat(i0__,i1__))) {
398:                     std::stringstream msg__;
399:                     msg__ << "Undefined transformed parameter: sdpcorrmat" << '[' << i0__ << ']' << '[' << i1__ << ']';
400:                     throw std::runtime_error(msg__.str());
401:                 }
402:             }
403:         }
404:         for (int i0__ = 0; i0__ < ndim; ++i0__) {
405:             for (int i1__ = 0; i1__ < ndim; ++i1__) {
406:                 if (stan::math::is_uninitialized(cholcorrmat(i0__,i1__))) {
407:                     std::stringstream msg__;
408:                     msg__ << "Undefined transformed parameter: cholcorrmat" << '[' << i0__ << ']' << '[' << i1__ << ']';
409:                     throw std::runtime_error(msg__.str());
410:                 }
411:             }
412:         }
413: 
414:         const char* function__ = "validate transformed params";
415:         (void) function__;  // dummy to suppress unused var warning
416: 
417:         // model body
418:         try {
419:             current_statement_begin__ = 94;
420:             lp_accum__.add(normal_log<propto__>(dummy, 0, 1));
421:         } catch (const std::exception& e) {
422:             stan::lang::rethrow_located(e,current_statement_begin__);
423:             // Next line prevents compiler griping about no return
424:             throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
425:         }
426: 
427:         lp_accum__.add(lp__);
428:         return lp_accum__.sum();
429: 
430:     } // log_prob()
431: 
432:     template <bool propto, bool jacobian, typename T_>
433:     T_ log_prob(Eigen::Matrix<T_,Eigen::Dynamic,1>& params_r,
434:                std::ostream* pstream = 0) const {
435:       std::vector<T_> vec_params_r;
436:       vec_params_r.reserve(params_r.size());
437:       for (int i = 0; i < params_r.size(); ++i)
438:         vec_params_r.push_back(params_r(i));
439:       std::vector<int> vec_params_i;
440:       return log_prob<propto,jacobian,T_>(vec_params_r, vec_params_i, pstream);
441:     }
442: 
443: 
444:     void get_param_names(std::vector<std::string>& names__) const {
445:         names__.resize(0);
446:         names__.push_back("pcorrvec");
447:         names__.push_back("dummy");
448:         names__.push_back("scalevec");
449:         names__.push_back("sdpcorrmat");
450:         names__.push_back("cholcorrmat");
451:         names__.push_back("corrmat_out");
452:     }
453: 
454: 
455:     void get_dims(std::vector<std::vector<size_t> >& dimss__) const {
456:         dimss__.resize(0);
457:         std::vector<size_t> dims__;
458:         dims__.resize(0);
459:         dims__.push_back(divide(((ndim * ndim) - ndim),2));
460:         dimss__.push_back(dims__);
461:         dims__.resize(0);
462:         dimss__.push_back(dims__);
463:         dims__.resize(0);
464:         dims__.push_back(ndim);
465:         dimss__.push_back(dims__);
466:         dims__.resize(0);
467:         dims__.push_back(ndim);
468:         dims__.push_back(ndim);
469:         dimss__.push_back(dims__);
470:         dims__.resize(0);
471:         dims__.push_back(ndim);
472:         dims__.push_back(ndim);
473:         dimss__.push_back(dims__);
474:         dims__.resize(0);
475:         dims__.push_back(ndim);
476:         dims__.push_back(ndim);
477:         dimss__.push_back(dims__);
478:     }
479: 
480:     template <typename RNG>
481:     void write_array(RNG& base_rng__,
482:                      std::vector<double>& params_r__,
483:                      std::vector<int>& params_i__,
484:                      std::vector<double>& vars__,
485:                      bool include_tparams__ = true,
486:                      bool include_gqs__ = true,
487:                      std::ostream* pstream__ = 0) const {
488:         vars__.resize(0);
489:         stan::io::reader<double> in__(params_r__,params_i__);
490:         static const char* function__ = "model174be4c85f95d_bug_namespace::write_array";
491:         (void) function__; // dummy call to supress warning
492:         // read-transform, write parameters
493:         vector_d pcorrvec = in__.vector_lub_constrain(-(1),1,divide(((ndim * ndim) - ndim),2));
494:         double dummy = in__.scalar_constrain();
495:         for (int k_0__ = 0; k_0__ < divide(((ndim * ndim) - ndim),2); ++k_0__) {
496:             vars__.push_back(pcorrvec[k_0__]);
497:         }
498:         vars__.push_back(dummy);
499: 
500:         if (!include_tparams__) return;
501:         // declare and define transformed parameters
502:         double lp__ = 0.0;
503:         (void) lp__; // dummy call to supress warning
504:         stan::math::accumulator<double> lp_accum__;
505: 
506:         vector_d scalevec(static_cast<Eigen::VectorXd::Index>(ndim));
507:         (void) scalevec;  // dummy to suppress unused var warning
508:         matrix_d sdpcorrmat(static_cast<Eigen::VectorXd::Index>(ndim),static_cast<Eigen::VectorXd::Index>(ndim));
509:         (void) sdpcorrmat;  // dummy to suppress unused var warning
510:         matrix_d cholcorrmat(static_cast<Eigen::VectorXd::Index>(ndim),static_cast<Eigen::VectorXd::Index>(ndim));
511:         (void) cholcorrmat;  // dummy to suppress unused var warning
512: 
513:         try {
514:             current_statement_begin__ = 74;
515:             stan::math::assign(scalevec, rep_vector(1,ndim));
516:             {
517:                 int counter(0);
518:                 (void) counter;  // dummy to suppress unused var warning
519:                 current_statement_begin__ = 78;
520:                 stan::math::assign(counter, 0);
521:                 current_statement_begin__ = 79;
522:                 stan::math::assign(sdpcorrmat, diag_matrix(scalevec));
523:                 current_statement_begin__ = 81;
524:                 for (int rowi = 2; rowi <= ndim; ++rowi) {
525:                     current_statement_begin__ = 82;
526:                     for (int coli = 1; coli <= (rowi - 1); ++coli) {
527:                         current_statement_begin__ = 83;
528:                         stan::math::assign(counter, (counter + 1));
529:                         current_statement_begin__ = 84;
530:                         stan::math::assign(get_base1_lhs(sdpcorrmat,rowi,coli,"sdpcorrmat",1), get_base1(pcorrvec,counter,"pcorrvec",1));
531:                     }
532:                 }
533:             }
534:             current_statement_begin__ = 89;
535:             stan::math::assign(cholcorrmat, sdpcorr2cov_lp(sdpcorrmat,1,eta, lp__, lp_accum__, pstream__));
536:         } catch (const std::exception& e) {
537:             stan::lang::rethrow_located(e,current_statement_begin__);
538:             // Next line prevents compiler griping about no return
539:             throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
540:         }
541: 
542:         // validate transformed parameters
543: 
544:         // write transformed parameters
545:         for (int k_0__ = 0; k_0__ < ndim; ++k_0__) {
546:             vars__.push_back(scalevec[k_0__]);
547:         }
548:         for (int k_1__ = 0; k_1__ < ndim; ++k_1__) {
549:             for (int k_0__ = 0; k_0__ < ndim; ++k_0__) {
550:                 vars__.push_back(sdpcorrmat(k_0__, k_1__));
551:             }
552:         }
553:         for (int k_1__ = 0; k_1__ < ndim; ++k_1__) {
554:             for (int k_0__ = 0; k_0__ < ndim; ++k_0__) {
555:                 vars__.push_back(cholcorrmat(k_0__, k_1__));
556:             }
557:         }
558: 
559:         if (!include_gqs__) return;
560:         // declare and define generated quantities
561:         matrix_d corrmat_out(static_cast<Eigen::VectorXd::Index>(ndim),static_cast<Eigen::VectorXd::Index>(ndim));
562:         (void) corrmat_out;  // dummy to suppress unused var warning
563: 
564:         double DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
565:         (void) DUMMY_VAR__;  // suppress unused var warning
566: 
567: 
568:         // initialize transformed variables to avoid seg fault on val access
569:         stan::math::fill(corrmat_out,DUMMY_VAR__);
570: 
571:         try {
572:             current_statement_begin__ = 99;
573:             stan::math::assign(corrmat_out, multiply(cholcorrmat,transpose(cholcorrmat)));
574:         } catch (const std::exception& e) {
575:             stan::lang::rethrow_located(e,current_statement_begin__);
576:             // Next line prevents compiler griping about no return
577:             throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
578:         }
579: 
580:         // validate generated quantities
581: 
582:         // write generated quantities
583:         for (int k_1__ = 0; k_1__ < ndim; ++k_1__) {
584:             for (int k_0__ = 0; k_0__ < ndim; ++k_0__) {
585:                 vars__.push_back(corrmat_out(k_0__, k_1__));
586:             }
587:         }
588: 
589:     }
590: 
591:     template <typename RNG>
592:     void write_array(RNG& base_rng,
593:                      Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
594:                      Eigen::Matrix<double,Eigen::Dynamic,1>& vars,
595:                      bool include_tparams = true,
596:                      bool include_gqs = true,
597:                      std::ostream* pstream = 0) const {
598:       std::vector<double> params_r_vec(params_r.size());
599:       for (int i = 0; i < params_r.size(); ++i)
600:         params_r_vec[i] = params_r(i);
601:       std::vector<double> vars_vec;
602:       std::vector<int> params_i_vec;
603:       write_array(base_rng,params_r_vec,params_i_vec,vars_vec,include_tparams,include_gqs,pstream);
604:       vars.resize(vars_vec.size());
605:       for (int i = 0; i < vars.size(); ++i)
606:         vars(i) = vars_vec[i];
607:     }
608: 
609:     static std::string model_name() {
610:         return "model174be4c85f95d_bug";
611:     }
612: 
613: 
614:     void constrained_param_names(std::vector<std::string>& param_names__,
615:                                  bool include_tparams__ = true,
616:                                  bool include_gqs__ = true) const {
617:         std::stringstream param_name_stream__;
618:         for (int k_0__ = 1; k_0__ <= divide(((ndim * ndim) - ndim),2); ++k_0__) {
619:             param_name_stream__.str(std::string());
620:             param_name_stream__ << "pcorrvec" << '.' << k_0__;
621:             param_names__.push_back(param_name_stream__.str());
622:         }
623:         param_name_stream__.str(std::string());
624:         param_name_stream__ << "dummy";
625:         param_names__.push_back(param_name_stream__.str());
626: 
627:         if (!include_gqs__ && !include_tparams__) return;
628:         for (int k_0__ = 1; k_0__ <= ndim; ++k_0__) {
629:             param_name_stream__.str(std::string());
630:             param_name_stream__ << "scalevec" << '.' << k_0__;
631:             param_names__.push_back(param_name_stream__.str());
632:         }
633:         for (int k_1__ = 1; k_1__ <= ndim; ++k_1__) {
634:             for (int k_0__ = 1; k_0__ <= ndim; ++k_0__) {
635:                 param_name_stream__.str(std::string());
636:                 param_name_stream__ << "sdpcorrmat" << '.' << k_0__ << '.' << k_1__;
637:                 param_names__.push_back(param_name_stream__.str());
638:             }
639:         }
640:         for (int k_1__ = 1; k_1__ <= ndim; ++k_1__) {
641:             for (int k_0__ = 1; k_0__ <= ndim; ++k_0__) {
642:                 param_name_stream__.str(std::string());
643:                 param_name_stream__ << "cholcorrmat" << '.' << k_0__ << '.' << k_1__;
644:                 param_names__.push_back(param_name_stream__.str());
645:             }
646:         }
647: 
648:         if (!include_gqs__) return;
649:         for (int k_1__ = 1; k_1__ <= ndim; ++k_1__) {
650:             for (int k_0__ = 1; k_0__ <= ndim; ++k_0__) {
651:                 param_name_stream__.str(std::string());
652:                 param_name_stream__ << "corrmat_out" << '.' << k_0__ << '.' << k_1__;
653:                 param_names__.push_back(param_name_stream__.str());
654:             }
655:         }
656:     }
657: 
658: 
659:     void unconstrained_param_names(std::vector<std::string>& param_names__,
660:                                    bool include_tparams__ = true,
661:                                    bool include_gqs__ = true) const {
662:         std::stringstream param_name_stream__;
663:         for (int k_0__ = 1; k_0__ <= divide(((ndim * ndim) - ndim),2); ++k_0__) {
664:             param_name_stream__.str(std::string());
665:             param_name_stream__ << "pcorrvec" << '.' << k_0__;
666:             param_names__.push_back(param_name_stream__.str());
667:         }
668:         param_name_stream__.str(std::string());
669:         param_name_stream__ << "dummy";
670:         param_names__.push_back(param_name_stream__.str());
671: 
672:         if (!include_gqs__ && !include_tparams__) return;
673:         for (int k_0__ = 1; k_0__ <= ndim; ++k_0__) {
674:             param_name_stream__.str(std::string());
675:             param_name_stream__ << "scalevec" << '.' << k_0__;
676:             param_names__.push_back(param_name_stream__.str());
677:         }
678:         for (int k_1__ = 1; k_1__ <= ndim; ++k_1__) {
679:             for (int k_0__ = 1; k_0__ <= ndim; ++k_0__) {
680:                 param_name_stream__.str(std::string());
681:                 param_name_stream__ << "sdpcorrmat" << '.' << k_0__ << '.' << k_1__;
682:                 param_names__.push_back(param_name_stream__.str());
683:             }
684:         }
685:         for (int k_1__ = 1; k_1__ <= ndim; ++k_1__) {
686:             for (int k_0__ = 1; k_0__ <= ndim; ++k_0__) {
687:                 param_name_stream__.str(std::string());
688:                 param_name_stream__ << "cholcorrmat" << '.' << k_0__ << '.' << k_1__;
689:                 param_names__.push_back(param_name_stream__.str());
690:             }
691:         }
692: 
693:         if (!include_gqs__) return;
694:         for (int k_1__ = 1; k_1__ <= ndim; ++k_1__) {
695:             for (int k_0__ = 1; k_0__ <= ndim; ++k_0__) {
696:                 param_name_stream__.str(std::string());
697:                 param_name_stream__ << "corrmat_out" << '.' << k_0__ << '.' << k_1__;
698:                 param_names__.push_back(param_name_stream__.str());
699:             }
700:         }
701:     }
702: 
703: }; // model
704: 
705: } // namespace
706: 
707: typedef model174be4c85f95d_bug_namespace::model174be4c85f95d_bug stan_model;
708: 
709: #include <rstan/rstaninc.hpp>
710: /**
711:  * Define Rcpp Module to expose stan_fit's functions to R.
712:  */
713: RCPP_MODULE(stan_fit4model174be4c85f95d_bug_mod){
714:   Rcpp::class_<rstan::stan_fit<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug,
715:                boost::random::ecuyer1988> >("stan_fit4model174be4c85f95d_bug")
716:     // .constructor<Rcpp::List>()
717:     .constructor<SEXP, SEXP>()
718:     // .constructor<SEXP, SEXP>()
719:     .method("call_sampler",
720:             &rstan::stan_fit<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::ecuyer1988>::call_sampler)
721:     .method("param_names",
722:             &rstan::stan_fit<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::ecuyer1988>::param_names)
723:     .method("param_names_oi",
724:             &rstan::stan_fit<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::ecuyer1988>::param_names_oi)
725:     .method("param_fnames_oi",
726:             &rstan::stan_fit<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::ecuyer1988>::param_fnames_oi)
727:     .method("param_dims",
728:             &rstan::stan_fit<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::ecuyer1988>::param_dims)
729:     .method("param_dims_oi",
730:             &rstan::stan_fit<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::ecuyer1988>::param_dims_oi)
731:     .method("update_param_oi",
732:             &rstan::stan_fit<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::ecuyer1988>::update_param_oi)
733:     .method("param_oi_tidx",
734:             &rstan::stan_fit<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::ecuyer1988>::param_oi_tidx)
735:     .method("grad_log_prob",
736:             &rstan::stan_fit<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::ecuyer1988>::grad_log_prob)
737:     .method("log_prob",
738:             &rstan::stan_fit<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::ecuyer1988>::log_prob)
739:     .method("unconstrain_pars",
740:             &rstan::stan_fit<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::ecuyer1988>::unconstrain_pars)
741:     .method("constrain_pars",
742:             &rstan::stan_fit<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::ecuyer1988>::constrain_pars)
743:     .method("num_pars_unconstrained",
744:             &rstan::stan_fit<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::ecuyer1988>::num_pars_unconstrained)
745:     .method("unconstrained_param_names",
746:             &rstan::stan_fit<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::ecuyer1988>::unconstrained_param_names)
747:     .method("constrained_param_names",
748:             &rstan::stan_fit<model174be4c85f95d_bug_namespace::model174be4c85f95d_bug, boost::random::ecuyer1988>::constrained_param_names)
749:     ;
750: }
751: 
752: // declarations
753: extern "C" {
754: SEXP file174be6fb66fea( ) ;
755: }
756: 
757: // definition
758: 
759: SEXP file174be6fb66fea(  ){
760:  return Rcpp::wrap("bug");
761: }
762: 
763: 
Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! In file included from file174be6fb66fea.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:9:
In file included from /Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/stan/math/prim/mat.hpp:133:
In file included from /Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/promote_scalar.hpp:4:
/Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/stan/math/prim/scal/fun/promote_scalar.hpp:32:16: error: cannot convert 'stan::math::var' to 'double' without a conversion operator
In addition: Warning message:
running command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file174be6fb66fea.cpp 2> file174be6fb66fea.cpp.err.txt' had status 1 
> 

Expected Output:

It to compile or fail to parse with a warning message.

Current Version:

v2.12.0

For the example, I ran on my mac with

rstan (Version 2.12.1, packaged: 2016-09-11 13:07:50 UTC, GitRev: 85f7a56811da)

and

$ clang++ --version
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
bob-carpenter commented 8 years ago

Fixed with pull request #2104

adamwespiser commented 7 years ago

I'm still getting this problem: ternary op not working, but if_else does...was this fix merged into stan 2.16.2?

I ran into the same problem.


compile_error_code <- '
data {
int N;
int y_max;
int<lower=0> y[N];
int<lower=0,upper=1> group[N];
} 
parameters {
vector<lower=0,upper=1>[2] param;

}
model {
to_vector(param) ~ uniform(0,1);
for (i in 1:N){
  y[i] ~ binomial(y_max, group[i] == 0 ? param[1] : param[2]);
}
}
'

compile_warning_code <- '
data {
int N;
int y_max;
int<lower=0> y[N];
int<lower=0,upper=1> group[N];
} 
parameters {
vector<lower=0,upper=1>[2] param;

}
model {
to_vector(param) ~ uniform(0,1);
for (i in 1:N){
y[i] ~ binomial(y_max, if_else(group[i] == 0,param[1],param[2]));
}
}
'

ce_data=list(y=sample(1:10, size = 40,replace = TRUE),
     N=40,
     y_max = 10,
     group=sample(c(0,1),size=40,replace=TRUE))

model <- stan(model_code=compile_warning_code, data=ce_data,seed=42, chains=1)
compile_err_model <- stan(model_code=compile_error_code, data=ce_data,seed=42, chains=1)
> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X Yosemite 10.10.5

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rstan_2.16.2         StanHeaders_2.16.0-1 ggplot2_2.2.1       

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.12     codetools_0.2-15 grid_3.3.3       plyr_1.8.4       gtable_0.2.0     stats4_3.3.3     scales_0.4.1    
 [8] rlang_0.1.1      lazyeval_0.2.0   tools_3.3.3      munsell_0.4.3    parallel_3.3.3   inline_0.3.14    colorspace_1.3-2
[15] gridExtra_2.2.1  tibble_1.3.3    
> model <- stan(model_code=compile_warning_code, 
                data=ce_data,seed=42, chains=1)
DIAGNOSTIC(S) FROM PARSER:
Warning (non-fatal): the if_else() function is deprecated.  Use the conditional operator '?:' instead.

In file included from file5030a0e0305.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core/var.hpp:7:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/math/tools/config.hpp:13:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/config.hpp:39:
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/config/compiler/clang.hpp:196:11: warning: 'BOOST_NO_CXX11_RVALUE_REFERENCES' macro redefined [-Wmacro-redefined]
#  define BOOST_NO_CXX11_RVALUE_REFERENCES
          ^
<command line>:6:9: note: previous definition is here
#define BOOST_NO_CXX11_RVALUE_REFERENCES 1
        ^
In file included from file5030a0e0305.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:42:
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints.hpp:14:17: warning: unused function 'set_zero_all_adjoints' [-Wunused-function]
    static void set_zero_all_adjoints() {
                ^
In file included from file5030a0e0305.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:43:
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints_nested.hpp:17:17: warning: 'static' function 'set_zero_all_adjoints_nested' declared in header file should be declared 'static inline' [-Wunneeded-internal-declaration]
    static void set_zero_all_adjoints_nested() {
                ^
In file included from file5030a0e0305.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/prim/mat.hpp:58:
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/autocorrelation.hpp:17:14: warning: function 'fft_next_good_size' is not needed and will not be emitted [-Wunneeded-internal-declaration]
      size_t fft_next_good_size(size_t N) {
             ^
In file included from file5030a0e0305.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/prim/mat.hpp:298:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/prim/arr.hpp:38:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/prim/arr/functor/integrate_ode_rk45.hpp:13:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/numeric/odeint.hpp:61:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/numeric/odeint/util/multi_array_adaption.hpp:29:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/multi_array.hpp:21:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/multi_array/base.hpp:28:
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/multi_array/concept_checks.hpp:42:43: warning: unused typedef 'index_range' [-Wunused-local-typedef]
      typedef typename Array::index_range index_range;
                                          ^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/multi_array/concept_checks.hpp:43:37: warning: unused typedef 'index' [-Wunused-local-typedef]
      typedef typename Array::index index;
                                    ^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/multi_array/concept_checks.hpp:53:43: warning: unused typedef 'index_range' [-Wunused-local-typedef]
      typedef typename Array::index_range index_range;
                                          ^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/multi_array/concept_checks.hpp:54:37: warning: unused typedef 'index' [-Wunused-local-typedef]
      typedef typename Array::index index;
                                    ^
8 warnings generated.

SAMPLING FOR MODEL 'e021365baf0c9dc2d11d141ee17adbb4' NOW (CHAIN 1).

Gradient evaluation took 3e-05 seconds
1000 transitions using 10 leapfrog steps per transition would take 0.3 seconds.
Adjust your expectations accordingly!

Iteration:    1 / 2000 [  0%]  (Warmup)
Iteration:  200 / 2000 [ 10%]  (Warmup)
Iteration:  400 / 2000 [ 20%]  (Warmup)
Iteration:  600 / 2000 [ 30%]  (Warmup)
Iteration:  800 / 2000 [ 40%]  (Warmup)
Iteration: 1000 / 2000 [ 50%]  (Warmup)
Iteration: 1001 / 2000 [ 50%]  (Sampling)
Iteration: 1200 / 2000 [ 60%]  (Sampling)
Iteration: 1400 / 2000 [ 70%]  (Sampling)
Iteration: 1600 / 2000 [ 80%]  (Sampling)
Iteration: 1800 / 2000 [ 90%]  (Sampling)
Iteration: 2000 / 2000 [100%]  (Sampling)

 Elapsed Time: 0.069626 seconds (Warm-up)
               0.057414 seconds (Sampling)
               0.12704 seconds (Total)

Now for the model with the ternary operator that doesn't compile


> compile_err_model <- stan(model_code=compile_error_code,
                            data=ce_data,seed=42, chains=1)
In file included from file5030604e93d7.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core/var.hpp:7:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/math/tools/config.hpp:13:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/config.hpp:39:
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/config/compiler/clang.hpp:196:11: warning: 'BOOST_NO_CXX11_RVALUE_REFERENCES' macro redefined [-Wmacro-redefined]
#  define BOOST_NO_CXX11_RVALUE_REFERENCES
          ^
<command line>:6:9: note: previous definition is here
#define BOOST_NO_CXX11_RVALUE_REFERENCES 1
        ^
In file included from file5030604e93d7.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/prim/mat.hpp:174:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/promote_scalar.hpp:4:
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/prim/scal/fun/promote_scalar.hpp:32:16: error: cannot convert 'stan::math::var' to 'double' without a conversion operator
        return T(x);
               ^~~
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/prim/scal/fun/promote_scalar.hpp:68:43: note: in instantiation of member function 'stan::math::promote_scalar_struct<double, stan::math::var>::apply' requested here
      return promote_scalar_struct<T, S>::apply(x);
                                          ^
file5030604e93d7.cpp:221:142: note: in instantiation of function template specialization 'stan::math::promote_scalar<double, stan::math::var>' requested here
                lp_accum__.add(binomial_log<propto__>(get_base1(y,i,"y",1), y_max, (logical_eq(get_base1(group,i,"group",1),0) ? stan::math::promote_scalar<double>(get_base1(param,1,"param",1)) : stan::math::promote_scalar<double>(get_base1(param,2,"param",1)) )));
                                                                                                                                             ^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/log_prob_grad.hpp:44:28: note: in instantiation of function template specialization 'model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b::log_prob<true, true, stan::math::var>' requested here
          = model.template log_prob<propto, jacobian_adjust_transform>
                           ^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/services/util/initialize.hpp:147:37: note: in instantiation of function template specialization 'stan::model::log_prob_grad<true, true, model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b>' requested here
            log_prob = stan::model::log_prob_grad<true, true>
                                    ^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/services/diagnose/diagnose.hpp:54:19: note: in instantiation of function template specialization 'stan::services::util::initialize<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>, boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399> > >' requested here
          = util::initialize(model, init, rng, init_radius,
                  ^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/rstan/include/rstan/stan_fit.hpp:459:45: note: in instantiation of function template specialization 'stan::services::diagnose::diagnose<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b>' requested here
    return_code = stan::services::diagnose::diagnose(model,
                                            ^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/rstan/include/rstan/stan_fit.hpp:1196:11: note: in instantiation of function template specialization 'rstan::(anonymous namespace)::command<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>, boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399> > >' requested here
    ret = command(args, model_, holder, names_oi_tidx_,
          ^
file5030604e93d7.cpp:387:172: note: in instantiation of member function 'rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>, boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399> > >::call_sampler' requested here
            &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::call_sampler)
                                                                                                                                                                           ^
1 warning and 1 error generated.
make: *** [file5030604e93d7.o] Error 1

ERROR(s) during compilation: source code errors or compiler configuration errors!

Program source:
  1: 
  2: // includes from the plugin
  3: 
  4: 
  5: // user includes
  6: #define STAN__SERVICES__COMMAND_HPP// Code generated by Stan version 2.16.0
  7: 
  8: #include <stan/model/model_header.hpp>
  9: 
 10: namespace model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace {
 11: 
 12: using std::istream;
 13: using std::string;
 14: using std::stringstream;
 15: using std::vector;
 16: using stan::io::dump;
 17: using stan::math::lgamma;
 18: using stan::model::prob_grad;
 19: using namespace stan::math;
 20: 
 21: typedef Eigen::Matrix<double,Eigen::Dynamic,1> vector_d;
 22: typedef Eigen::Matrix<double,1,Eigen::Dynamic> row_vector_d;
 23: typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> matrix_d;
 24: 
 25: static int current_statement_begin__;
 26: 
 27: stan::io::program_reader prog_reader__() {
 28:     stan::io::program_reader reader;
 29:     reader.add_event(0, 0, "start", "model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b");
 30:     reader.add_event(17, 17, "end", "model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b");
 31:     return reader;
 32: }
 33: 
 34: class model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b : public prob_grad {
 35: private:
 36:     int N;
 37:     int y_max;
 38:     vector<int> y;
 39:     vector<int> group;
 40: public:
 41:     model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b(stan::io::var_context& context__,
 42:         std::ostream* pstream__ = 0)
 43:         : prob_grad(0) {
 44:         ctor_body(context__, 0, pstream__);
 45:     }
 46: 
 47:     model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b(stan::io::var_context& context__,
 48:         unsigned int random_seed__,
 49:         std::ostream* pstream__ = 0)
 50:         : prob_grad(0) {
 51:         ctor_body(context__, random_seed__, pstream__);
 52:     }
 53: 
 54:     void ctor_body(stan::io::var_context& context__,
 55:                    unsigned int random_seed__,
 56:                    std::ostream* pstream__) {
 57:         boost::ecuyer1988 base_rng__ =
 58:           stan::services::util::create_rng(random_seed__, 0);
 59:         (void) base_rng__;  // suppress unused var warning
 60: 
 61:         current_statement_begin__ = -1;
 62: 
 63:         static const char* function__ = "model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b";
 64:         (void) function__;  // dummy to suppress unused var warning
 65:         size_t pos__;
 66:         (void) pos__;  // dummy to suppress unused var warning
 67:         std::vector<int> vals_i__;
 68:         std::vector<double> vals_r__;
 69:         double DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
 70:         (void) DUMMY_VAR__;  // suppress unused var warning
 71: 
 72:         // initialize member variables
 73:         context__.validate_dims("data initialization", "N", "int", context__.to_vec());
 74:         N = int(0);
 75:         vals_i__ = context__.vals_i("N");
 76:         pos__ = 0;
 77:         N = vals_i__[pos__++];
 78:         context__.validate_dims("data initialization", "y_max", "int", context__.to_vec());
 79:         y_max = int(0);
 80:         vals_i__ = context__.vals_i("y_max");
 81:         pos__ = 0;
 82:         y_max = vals_i__[pos__++];
 83:         validate_non_negative_index("y", "N", N);
 84:         context__.validate_dims("data initialization", "y", "int", context__.to_vec(N));
 85:         validate_non_negative_index("y", "N", N);
 86:         y = std::vector<int>(N,int(0));
 87:         vals_i__ = context__.vals_i("y");
 88:         pos__ = 0;
 89:         size_t y_limit_0__ = N;
 90:         for (size_t i_0__ = 0; i_0__ < y_limit_0__; ++i_0__) {
 91:             y[i_0__] = vals_i__[pos__++];
 92:         }
 93:         validate_non_negative_index("group", "N", N);
 94:         context__.validate_dims("data initialization", "group", "int", context__.to_vec(N));
 95:         validate_non_negative_index("group", "N", N);
 96:         group = std::vector<int>(N,int(0));
 97:         vals_i__ = context__.vals_i("group");
 98:         pos__ = 0;
 99:         size_t group_limit_0__ = N;
100:         for (size_t i_0__ = 0; i_0__ < group_limit_0__; ++i_0__) {
101:             group[i_0__] = vals_i__[pos__++];
102:         }
103: 
104:         // validate, data variables
105:         for (int k0__ = 0; k0__ < N; ++k0__) {
106:             check_greater_or_equal(function__,"y[k0__]",y[k0__],0);
107:         }
108:         for (int k0__ = 0; k0__ < N; ++k0__) {
109:             check_greater_or_equal(function__,"group[k0__]",group[k0__],0);
110:             check_less_or_equal(function__,"group[k0__]",group[k0__],1);
111:         }
112:         // initialize data variables
113: 
114:         try {
115:         } catch (const std::exception& e) {
116:             stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
117:             // Next line prevents compiler griping about no return
118:             throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
119:         }
120: 
121:         // validate transformed data
122: 
123:         // validate, set parameter ranges
124:         num_params_r__ = 0U;
125:         param_ranges_i__.clear();
126:         validate_non_negative_index("param", "2", 2);
127:         num_params_r__ += 2;
128:     }
129: 
130:     ~model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b() { }
131: 
132: 
133:     void transform_inits(const stan::io::var_context& context__,
134:                          std::vector<int>& params_i__,
135:                          std::vector<double>& params_r__,
136:                          std::ostream* pstream__) const {
137:         stan::io::writer<double> writer__(params_r__,params_i__);
138:         size_t pos__;
139:         (void) pos__; // dummy call to supress warning
140:         std::vector<double> vals_r__;
141:         std::vector<int> vals_i__;
142: 
143:         if (!(context__.contains_r("param")))
144:             throw std::runtime_error("variable param missing");
145:         vals_r__ = context__.vals_r("param");
146:         pos__ = 0U;
147:         validate_non_negative_index("param", "2", 2);
148:         context__.validate_dims("initialization", "param", "vector_d", context__.to_vec(2));
149:         // generate_declaration param
150:         vector_d param(static_cast<Eigen::VectorXd::Index>(2));
151:         for (int j1__ = 0U; j1__ < 2; ++j1__)
152:             param(j1__) = vals_r__[pos__++];
153:         try {
154:             writer__.vector_lub_unconstrain(0,1,param);
155:         } catch (const std::exception& e) { 
156:             throw std::runtime_error(std::string("Error transforming variable param: ") + e.what());
157:         }
158: 
159:         params_r__ = writer__.data_r();
160:         params_i__ = writer__.data_i();
161:     }
162: 
163:     void transform_inits(const stan::io::var_context& context,
164:                          Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
165:                          std::ostream* pstream__) const {
166:       std::vector<double> params_r_vec;
167:       std::vector<int> params_i_vec;
168:       transform_inits(context, params_i_vec, params_r_vec, pstream__);
169:       params_r.resize(params_r_vec.size());
170:       for (int i = 0; i < params_r.size(); ++i)
171:         params_r(i) = params_r_vec[i];
172:     }
173: 
174: 
175:     template <bool propto__, bool jacobian__, typename T__>
176:     T__ log_prob(vector<T__>& params_r__,
177:                  vector<int>& params_i__,
178:                  std::ostream* pstream__ = 0) const {
179: 
180:         T__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
181:         (void) DUMMY_VAR__;  // suppress unused var warning
182: 
183:         T__ lp__(0.0);
184:         stan::math::accumulator<T__> lp_accum__;
185: 
186:         // model parameters
187:         stan::io::reader<T__> in__(params_r__,params_i__);
188: 
189:         Eigen::Matrix<T__,Eigen::Dynamic,1>  param;
190:         (void) param;  // dummy to suppress unused var warning
191:         if (jacobian__)
192:             param = in__.vector_lub_constrain(0,1,2,lp__);
193:         else
194:             param = in__.vector_lub_constrain(0,1,2);
195: 
196: 
197:         // transformed parameters
198: 
199: 
200:         try {
201:         } catch (const std::exception& e) {
202:             stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
203:             // Next line prevents compiler griping about no return
204:             throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
205:         }
206: 
207:         // validate transformed parameters
208: 
209:         const char* function__ = "validate transformed params";
210:         (void) function__;  // dummy to suppress unused var warning
211: 
212:         // model body
213:         try {
214: 
215:             current_statement_begin__ = 13;
216:             lp_accum__.add(uniform_log<propto__>(to_vector(param), 0, 1));
217:             current_statement_begin__ = 14;
218:             for (int i = 1; i <= N; ++i) {
219: 
220:                 current_statement_begin__ = 15;
221:                 lp_accum__.add(binomial_log<propto__>(get_base1(y,i,"y",1), y_max, (logical_eq(get_base1(group,i,"group",1),0) ? stan::math::promote_scalar<double>(get_base1(param,1,"param",1)) : stan::math::promote_scalar<double>(get_base1(param,2,"param",1)) )));
222:             }
223:         } catch (const std::exception& e) {
224:             stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
225:             // Next line prevents compiler griping about no return
226:             throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
227:         }
228: 
229:         lp_accum__.add(lp__);
230:         return lp_accum__.sum();
231: 
232:     } // log_prob()
233: 
234:     template <bool propto, bool jacobian, typename T_>
235:     T_ log_prob(Eigen::Matrix<T_,Eigen::Dynamic,1>& params_r,
236:                std::ostream* pstream = 0) const {
237:       std::vector<T_> vec_params_r;
238:       vec_params_r.reserve(params_r.size());
239:       for (int i = 0; i < params_r.size(); ++i)
240:         vec_params_r.push_back(params_r(i));
241:       std::vector<int> vec_params_i;
242:       return log_prob<propto,jacobian,T_>(vec_params_r, vec_params_i, pstream);
243:     }
244: 
245: 
246:     void get_param_names(std::vector<std::string>& names__) const {
247:         names__.resize(0);
248:         names__.push_back("param");
249:     }
250: 
251: 
252:     void get_dims(std::vector<std::vector<size_t> >& dimss__) const {
253:         dimss__.resize(0);
254:         std::vector<size_t> dims__;
255:         dims__.resize(0);
256:         dims__.push_back(2);
257:         dimss__.push_back(dims__);
258:     }
259: 
260:     template <typename RNG>
261:     void write_array(RNG& base_rng__,
262:                      std::vector<double>& params_r__,
263:                      std::vector<int>& params_i__,
264:                      std::vector<double>& vars__,
265:                      bool include_tparams__ = true,
266:                      bool include_gqs__ = true,
267:                      std::ostream* pstream__ = 0) const {
268:         vars__.resize(0);
269:         stan::io::reader<double> in__(params_r__,params_i__);
270:         static const char* function__ = "model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::write_array";
271:         (void) function__;  // dummy to suppress unused var warning
272:         // read-transform, write parameters
273:         vector_d param = in__.vector_lub_constrain(0,1,2);
274:         for (int k_0__ = 0; k_0__ < 2; ++k_0__) {
275:             vars__.push_back(param[k_0__]);
276:         }
277: 
278:         if (!include_tparams__) return;
279:         // declare and define transformed parameters
280:         double lp__ = 0.0;
281:         (void) lp__;  // dummy to suppress unused var warning
282:         stan::math::accumulator<double> lp_accum__;
283: 
284:         double DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
285:         (void) DUMMY_VAR__;  // suppress unused var warning
286: 
287: 
288: 
289:         try {
290:         } catch (const std::exception& e) {
291:             stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
292:             // Next line prevents compiler griping about no return
293:             throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
294:         }
295: 
296:         // validate transformed parameters
297: 
298:         // write transformed parameters
299: 
300:         if (!include_gqs__) return;
301:         // declare and define generated quantities
302: 
303: 
304:         try {
305:         } catch (const std::exception& e) {
306:             stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
307:             // Next line prevents compiler griping about no return
308:             throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
309:         }
310: 
311:         // validate generated quantities
312: 
313:         // write generated quantities
314:     }
315: 
316:     template <typename RNG>
317:     void write_array(RNG& base_rng,
318:                      Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
319:                      Eigen::Matrix<double,Eigen::Dynamic,1>& vars,
320:                      bool include_tparams = true,
321:                      bool include_gqs = true,
322:                      std::ostream* pstream = 0) const {
323:       std::vector<double> params_r_vec(params_r.size());
324:       for (int i = 0; i < params_r.size(); ++i)
325:         params_r_vec[i] = params_r(i);
326:       std::vector<double> vars_vec;
327:       std::vector<int> params_i_vec;
328:       write_array(base_rng,params_r_vec,params_i_vec,vars_vec,include_tparams,include_gqs,pstream);
329:       vars.resize(vars_vec.size());
330:       for (int i = 0; i < vars.size(); ++i)
331:         vars(i) = vars_vec[i];
332:     }
333: 
334:     static std::string model_name() {
335:         return "model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b";
336:     }
337: 
338: 
339:     void constrained_param_names(std::vector<std::string>& param_names__,
340:                                  bool include_tparams__ = true,
341:                                  bool include_gqs__ = true) const {
342:         std::stringstream param_name_stream__;
343:         for (int k_0__ = 1; k_0__ <= 2; ++k_0__) {
344:             param_name_stream__.str(std::string());
345:             param_name_stream__ << "param" << '.' << k_0__;
346:             param_names__.push_back(param_name_stream__.str());
347:         }
348: 
349:         if (!include_gqs__ && !include_tparams__) return;
350: 
351:         if (!include_gqs__) return;
352:     }
353: 
354: 
355:     void unconstrained_param_names(std::vector<std::string>& param_names__,
356:                                    bool include_tparams__ = true,
357:                                    bool include_gqs__ = true) const {
358:         std::stringstream param_name_stream__;
359:         for (int k_0__ = 1; k_0__ <= 2; ++k_0__) {
360:             param_name_stream__.str(std::string());
361:             param_name_stream__ << "param" << '.' << k_0__;
362:             param_names__.push_back(param_name_stream__.str());
363:         }
364: 
365:         if (!include_gqs__ && !include_tparams__) return;
366: 
367:         if (!include_gqs__) return;
368:     }
369: 
370: }; // model
371: 
372: }
373: 
374: typedef model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b stan_model;
375: 
376: #include <rstan/rstaninc.hpp>
377: /**
378:  * Define Rcpp Module to expose stan_fit's functions to R.
379:  */
380: RCPP_MODULE(stan_fit4model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_mod){
381:   Rcpp::class_<rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b,
382:                boost::random::ecuyer1988> >("stan_fit4model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b")
383:     // .constructor<Rcpp::List>()
384:     .constructor<SEXP, SEXP, SEXP>()
385:     // .constructor<SEXP, SEXP>()
386:     .method("call_sampler",
387:             &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::call_sampler)
388:     .method("param_names",
389:             &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::param_names)
390:     .method("param_names_oi",
391:             &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::param_names_oi)
392:     .method("param_fnames_oi",
393:             &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::param_fnames_oi)
394:     .method("param_dims",
395:             &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::param_dims)
396:     .method("param_dims_oi",
397:             &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::param_dims_oi)
398:     .method("update_param_oi",
399:             &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::update_param_oi)
400:     .method("param_oi_tidx",
401:             &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::param_oi_tidx)
402:     .method("grad_log_prob",
403:             &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::grad_log_prob)
404:     .method("log_prob",
405:             &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::log_prob)
406:     .method("unconstrain_pars",
407:             &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::unconstrain_pars)
408:     .method("constrain_pars",
409:             &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::constrain_pars)
410:     .method("num_pars_unconstrained",
411:             &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::num_pars_unconstrained)
412:     .method("unconstrained_param_names",
413:             &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::unconstrained_param_names)
414:     .method("constrained_param_names",
415:             &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::constrained_param_names)
416:     ;
417: }
418: 
419: // declarations
420: extern "C" {
421: SEXP file5030604e93d7( ) ;
422: }
423: 
424: // definition
425: 
426: SEXP file5030604e93d7(  ){
427:  return Rcpp::wrap("917b48a33f8f03dac0f7b778c2b6092b");
428: }
429: 
430: 
Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! In file included from file5030604e93d7.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core/var.hpp:7:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/B
In addition: Warning message:
running command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file5030604e93d7.cpp 2> file5030604e93d7.cpp.err.txt' had status 1 
bob-carpenter commented 7 years ago

I just verified it's still an issue in RStan 2.16.2 and also still an issue in develop. I'll reopen:

bin/stanc  /Users/carp/temp2/badcomp.stan --o=/Users/carp/temp2/badcomp.hpp
Model name=badcomp_model
Input file=/Users/carp/temp2/badcomp.stan
Output file=/Users/carp/temp2/badcomp.hpp
src/cmdstan/diagnose.cpp:81:30: warning: comparison of integers of different signs: 'size_t'
      (aka 'unsigned long') and 'Index' (aka 'long') [-Wsign-compare]
        for (size_t n = 0; n < t_samples.size(); ++n)
                           ~ ^ ~~~~~~~~~~~~~~~~
src/cmdstan/diagnose.cpp:122:28: warning: comparison of integers of different signs: 'size_t'
      (aka 'unsigned long') and 'Index' (aka 'long') [-Wsign-compare]
      for (size_t n = 1; n < e_samples.size(); ++n) {
                         ~ ^ ~~~~~~~~~~~~~~~~
src/cmdstan/diagnose.cpp:163:23: warning: comparison of integers of different signs: 'int' and
      'unsigned long' [-Wsign-compare]
    for (int n = 0; n < bad_n_eff_names.size() - 1; ++n)
                    ~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~
src/cmdstan/diagnose.cpp:177:23: warning: comparison of integers of different signs: 'int' and
      'unsigned long' [-Wsign-compare]
    for (int n = 0; n < bad_rhat_names.size() - 1; ++n)
                    ~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~
clang++ -Wall -I . -isystem stan/lib/stan_math/lib/eigen_3.3.3 -isystem stan/lib/stan_math/lib/boost_1.64.0 -isystem stan/lib/stan_math/lib/cvodes_2.9.0/include -std=c++1y -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I src -I stan/src -isystem stan/lib/stan_math/ -DEIGEN_NO_DEBUG -DFUSION_MAX_VECTOR_SIZE=12 -stdlib=libc++ -Wno-unknown-warning-option -Wno-unused-function -Wno-tautological-compare -Wsign-compare -DNO_FPRINTF_OUTPUT -pipe   -O0 -o bin/stansummary bin/cmdstan/stansummary.o
4 warnings generated.
clang++ -Wall -I . -isystem stan/lib/stan_math/lib/eigen_3.3.3 -isystem stan/lib/stan_math/lib/boost_1.64.0 -isystem stan/lib/stan_math/lib/cvodes_2.9.0/include -std=c++1y -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I src -I stan/src -isystem stan/lib/stan_math/ -DEIGEN_NO_DEBUG -DFUSION_MAX_VECTOR_SIZE=12 -stdlib=libc++ -Wno-unknown-warning-option -Wno-unused-function -Wno-tautological-compare -Wsign-compare -DNO_FPRINTF_OUTPUT -pipe   -O0 -o bin/diagnose bin/cmdstan/diagnose.o

--- Linking C++ model ---
clang++ -Wall -I . -isystem stan/lib/stan_math/lib/eigen_3.3.3 -isystem stan/lib/stan_math/lib/boost_1.64.0 -isystem stan/lib/stan_math/lib/cvodes_2.9.0/include -std=c++1y -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I src -I stan/src -isystem stan/lib/stan_math/ -DEIGEN_NO_DEBUG -DFUSION_MAX_VECTOR_SIZE=12 -stdlib=libc++ -Wno-unknown-warning-option -Wno-unused-function -Wno-tautological-compare -Wsign-compare -DNO_FPRINTF_OUTPUT -pipe   -O0 -o /Users/carp/temp2/badcomp src/cmdstan/main.cpp -include /Users/carp/temp2/badcomp.hpp stan/lib/stan_math/lib/cvodes_2.9.0/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/cvodes_2.9.0/lib/libsundials_cvodes.a
In file included from <built-in>:360:
In file included from <command line>:8:
In file included from /Users/carp/temp2/badcomp.hpp:3:
In file included from stan/src/stan/model/model_header.hpp:4:
In file included from stan/lib/stan_math/stan/math.hpp:4:
In file included from stan/lib/stan_math/stan/math/rev/mat.hpp:12:
In file included from stan/lib/stan_math/stan/math/prim/mat.hpp:174:
In file included from stan/lib/stan_math/stan/math/prim/mat/fun/promote_scalar.hpp:4:
stan/lib/stan_math/stan/math/prim/scal/fun/promote_scalar.hpp:32:16: error: cannot convert 'stan::math::var'
      to 'double' without a conversion operator
        return T(x);
               ^~~
stan/lib/stan_math/stan/math/prim/scal/fun/promote_scalar.hpp:68:43: note: in instantiation of member
      function 'stan::math::promote_scalar_struct<double, stan::math::var>::apply' requested here
      return promote_scalar_struct<T, S>::apply(x);
                                          ^
/Users/carp/temp2/badcomp.hpp:218:142: note: in instantiation of function template specialization
      'stan::math::promote_scalar<double, stan::math::var>' requested here
  ...y_max, (logical_eq(get_base1(group,i,"group",1),0) ? stan::math::promote_scalar<double>(get_base1(pa...
                                                                      ^
stan/src/stan/model/log_prob_grad.hpp:44:28: note: in instantiation of function template specialization
      'badcomp_model_namespace::badcomp_model::log_prob<true, true, stan::math::var>' requested here
          = model.template log_prob<propto, jacobian_adjust_transform>
                           ^
stan/src/stan/services/util/initialize.hpp:147:37: note: in instantiation of function template
      specialization 'stan::model::log_prob_grad<true, true, badcomp_model_namespace::badcomp_model>'
      requested here
            log_prob = stan::model::log_prob_grad<true, true>
                                    ^
stan/src/stan/services/diagnose/diagnose.hpp:54:19: note: in instantiation of function template
      specialization 'stan::services::util::initialize<badcomp_model_namespace::badcomp_model,
      boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014,
      0, 2147483563>, boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399> > >'
      requested here
          = util::initialize(model, init, rng, init_radius,
                  ^
src/cmdstan/command.hpp:135:49: note: in instantiation of function template specialization
      'stan::services::diagnose::diagnose<badcomp_model_namespace::badcomp_model>' requested here
        return_code = stan::services::diagnose::diagnose(model,
                                                ^
src/cmdstan/main.cpp:8:21: note: in instantiation of function template specialization
      'cmdstan::command<badcomp_model_namespace::badcomp_model>' requested here
    return cmdstan::command<stan_model>(argc,argv);
                    ^
1 error generated.

(This also highlights a coding error in that we shouldn't get getting that warning, either.)

bob-carpenter commented 7 years ago

Meant to reopen with the last comment.

bob-carpenter commented 7 years ago

Simple program for replication from https://github.com/stan-dev/stan/issues/2101#issuecomment-329994130 (without R and with indenting):

data {
  int N;
  int y_max;
  int<lower=0> y[N];
  int<lower=0,upper=1> group[N];
}
parameters {
  vector<lower=0,upper=1>[2] param;

}
model {
  to_vector(param) ~ uniform(0,1);
  for (i in 1:N){
    y[i] ~ binomial(y_max, group[i] == 0 ? param[1] : param[2]);
  }
}
bgoodri commented 7 years ago

It will work if you do your loop like

for (i in 1:N) { real p = group[i] == 0 ? param[1] : param[2]; y[i] ~ binomial(y_max, p); }

mitzimorris commented 7 years ago

the underlying problem is that the function stan::lang::generate_expression, (file src/stan/lang/generator/generate_expression.hpp) has 2- and 3-arg variants which fail to keep track of flag is_var_context which is true when in parameters and model block. the fix to is always call the 4-arg version of this function. testing this change now.

mitzimorris commented 7 years ago

better fix: