// Generated by rstantools.  Do not edit by hand.

/*
    multinma is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    multinma is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with multinma.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MODELS_HPP
#define MODELS_HPP
#define STAN__SERVICES__COMMAND_HPP
#ifndef USE_STANC3
#define USE_STANC3
#endif
#include <rstan/rstaninc.hpp>
// Code generated by stanc v2.32.2
#include <stan/model/model_header.hpp>
namespace model_predict_delta_new_namespace {
using stan::model::model_base_crtp;
using namespace stan::math;
stan::math::profile_map profiles__;
static constexpr std::array<const char*, 11> locations_array__ =
  {" (found before start of program)",
  " (in 'string', line 9, column 2 to column 19)",
  " (in 'string', line 10, column 2 to column 22)",
  " (in 'string', line 14, column 2 to column 70)",
  " (in 'string', line 2, column 2 to column 18)",
  " (in 'string', line 3, column 14 to column 20)",
  " (in 'string', line 3, column 2 to column 29)",
  " (in 'string', line 6, column 23 to column 29)",
  " (in 'string', line 6, column 2 to column 65)",
  " (in 'string', line 9, column 9 to column 15)",
  " (in 'string', line 14, column 9 to column 15)"};
#include <stan_meta_header.hpp>
class model_predict_delta_new final : public model_base_crtp<model_predict_delta_new> {
private:
  int nt;
  Eigen::Matrix<double,-1,-1> RE_cor_data__;
  Eigen::Matrix<double,-1,-1> RE_L_data__;
  int d_1dim__;
  int delta_new_1dim__;
  Eigen::Map<Eigen::Matrix<double,-1,-1>> RE_cor{nullptr, 0, 0};
  Eigen::Map<Eigen::Matrix<double,-1,-1>> RE_L{nullptr, 0, 0};
public:
  ~model_predict_delta_new() {}
  model_predict_delta_new(stan::io::var_context& context__, unsigned int
                          random_seed__ = 0, std::ostream*
                          pstream__ = nullptr) : model_base_crtp(0) {
    int current_statement__ = 0;
    using local_scalar_t__ = double;
    boost::ecuyer1988 base_rng__ =
      stan::services::util::create_rng(random_seed__, 0);
    // suppress unused var warning
    (void) base_rng__;
    static constexpr const char* function__ =
      "model_predict_delta_new_namespace::model_predict_delta_new";
    // suppress unused var warning
    (void) function__;
    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    // suppress unused var warning
    (void) DUMMY_VAR__;
    try {
      int pos__ = std::numeric_limits<int>::min();
      pos__ = 1;
      current_statement__ = 4;
      context__.validate_dims("data initialization", "nt", "int",
        std::vector<size_t>{});
      nt = std::numeric_limits<int>::min();
      current_statement__ = 4;
      nt = context__.vals_i("nt")[(1 - 1)];
      current_statement__ = 4;
      stan::math::check_greater_or_equal(function__, "nt", nt, 1);
      current_statement__ = 5;
      stan::math::validate_non_negative_index("RE_cor", "nt - 1", (nt - 1));
      current_statement__ = 5;
      stan::math::validate_non_negative_index("RE_cor", "nt - 1", (nt - 1));
      current_statement__ = 6;
      context__.validate_dims("data initialization", "RE_cor", "double",
        std::vector<size_t>{static_cast<size_t>((nt - 1)),
          static_cast<size_t>((nt - 1))});
      RE_cor_data__ = Eigen::Matrix<double,-1,-1>::Constant((nt - 1), (nt -
                        1), std::numeric_limits<double>::quiet_NaN());
      new (&RE_cor)
        Eigen::Map<Eigen::Matrix<double,-1,-1>>(RE_cor_data__.data(), (nt -
        1), (nt - 1));
      {
        std::vector<local_scalar_t__> RE_cor_flat__;
        current_statement__ = 6;
        RE_cor_flat__ = context__.vals_r("RE_cor");
        current_statement__ = 6;
        pos__ = 1;
        current_statement__ = 6;
        for (int sym1__ = 1; sym1__ <= (nt - 1); ++sym1__) {
          current_statement__ = 6;
          for (int sym2__ = 1; sym2__ <= (nt - 1); ++sym2__) {
            current_statement__ = 6;
            stan::model::assign(RE_cor, RE_cor_flat__[(pos__ - 1)],
              "assigning variable RE_cor", stan::model::index_uni(sym2__),
              stan::model::index_uni(sym1__));
            current_statement__ = 6;
            pos__ = (pos__ + 1);
          }
        }
      }
      current_statement__ = 6;
      stan::math::check_corr_matrix(function__, "RE_cor", RE_cor);
      current_statement__ = 7;
      stan::math::validate_non_negative_index("RE_L", "nt - 1", (nt - 1));
      current_statement__ = 7;
      stan::math::validate_non_negative_index("RE_L", "nt - 1", (nt - 1));
      current_statement__ = 8;
      RE_L_data__ = Eigen::Matrix<double,-1,-1>::Constant((nt - 1), (nt - 1),
                      std::numeric_limits<double>::quiet_NaN());
      new (&RE_L) Eigen::Map<Eigen::Matrix<double,-1,-1>>(RE_L_data__.data(),
        (nt - 1), (nt - 1));
      current_statement__ = 8;
      stan::model::assign(RE_L, stan::math::cholesky_decompose(RE_cor),
        "assigning variable RE_L");
      current_statement__ = 8;
      stan::math::check_cholesky_factor_corr(function__, "RE_L", RE_L);
      current_statement__ = 9;
      d_1dim__ = std::numeric_limits<int>::min();
      current_statement__ = 9;
      d_1dim__ = (nt - 1);
      current_statement__ = 9;
      stan::math::validate_non_negative_index("d", "nt - 1", d_1dim__);
      current_statement__ = 10;
      delta_new_1dim__ = std::numeric_limits<int>::min();
      current_statement__ = 10;
      delta_new_1dim__ = (nt - 1);
      current_statement__ = 10;
      stan::math::validate_non_negative_index("delta_new", "nt - 1",
        delta_new_1dim__);
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
    num_params_r__ = d_1dim__ + 1;
  }
  inline std::string model_name() const final {
    return "model_predict_delta_new";
  }
  inline std::vector<std::string> model_compile_info() const noexcept {
    return std::vector<std::string>{"stanc_version = stanc3 v2.32.2",
             "stancflags = --allow-undefined"};
  }
  template <bool propto__, bool jacobian__, typename VecR, typename VecI,
            stan::require_vector_like_t<VecR>* = nullptr,
            stan::require_vector_like_vt<std::is_integral, VecI>* = nullptr>
  inline stan::scalar_type_t<VecR>
  log_prob_impl(VecR& params_r__, VecI& params_i__, std::ostream*
                pstream__ = nullptr) const {
    using T__ = stan::scalar_type_t<VecR>;
    using local_scalar_t__ = T__;
    T__ lp__(0.0);
    stan::math::accumulator<T__> lp_accum__;
    stan::io::deserializer<local_scalar_t__> in__(params_r__, params_i__);
    int current_statement__ = 0;
    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    // suppress unused var warning
    (void) DUMMY_VAR__;
    static constexpr const char* function__ =
      "model_predict_delta_new_namespace::log_prob";
    // suppress unused var warning
    (void) function__;
    try {
      Eigen::Matrix<local_scalar_t__,-1,1> d =
        Eigen::Matrix<local_scalar_t__,-1,1>::Constant(d_1dim__, DUMMY_VAR__);
      current_statement__ = 1;
      d = in__.template read<Eigen::Matrix<local_scalar_t__,-1,1>>(d_1dim__);
      local_scalar_t__ tau = DUMMY_VAR__;
      current_statement__ = 2;
      tau = in__.template read_constrain_lb<local_scalar_t__, jacobian__>(0,
              lp__);
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
    lp_accum__.add(lp__);
    return lp_accum__.sum();
  }
  template <typename RNG, typename VecR, typename VecI, typename VecVar,
            stan::require_vector_like_vt<std::is_floating_point,
            VecR>* = nullptr, stan::require_vector_like_vt<std::is_integral,
            VecI>* = nullptr, stan::require_vector_vt<std::is_floating_point,
            VecVar>* = nullptr>
  inline void
  write_array_impl(RNG& base_rng__, VecR& params_r__, VecI& params_i__,
                   VecVar& vars__, const bool
                   emit_transformed_parameters__ = true, const bool
                   emit_generated_quantities__ = true, std::ostream*
                   pstream__ = nullptr) const {
    using local_scalar_t__ = double;
    stan::io::deserializer<local_scalar_t__> in__(params_r__, params_i__);
    stan::io::serializer<local_scalar_t__> out__(vars__);
    static constexpr bool propto__ = true;
    // suppress unused var warning
    (void) propto__;
    double lp__ = 0.0;
    // suppress unused var warning
    (void) lp__;
    int current_statement__ = 0;
    stan::math::accumulator<double> lp_accum__;
    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    // suppress unused var warning
    (void) DUMMY_VAR__;
    constexpr bool jacobian__ = false;
    static constexpr const char* function__ =
      "model_predict_delta_new_namespace::write_array";
    // suppress unused var warning
    (void) function__;
    try {
      Eigen::Matrix<double,-1,1> d =
        Eigen::Matrix<double,-1,1>::Constant(d_1dim__,
          std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 1;
      d = in__.template read<Eigen::Matrix<local_scalar_t__,-1,1>>(d_1dim__);
      double tau = std::numeric_limits<double>::quiet_NaN();
      current_statement__ = 2;
      tau = in__.template read_constrain_lb<local_scalar_t__, jacobian__>(0,
              lp__);
      out__.write(d);
      out__.write(tau);
      if (stan::math::logical_negation(
            (stan::math::primitive_value(emit_transformed_parameters__) ||
            stan::math::primitive_value(emit_generated_quantities__)))) {
        return ;
      }
      if (stan::math::logical_negation(emit_generated_quantities__)) {
        return ;
      }
      Eigen::Matrix<double,-1,1> delta_new =
        Eigen::Matrix<double,-1,1>::Constant(delta_new_1dim__,
          std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 3;
      stan::model::assign(delta_new,
        stan::math::multi_normal_cholesky_rng(d,
          stan::math::multiply(tau, RE_L), base_rng__),
        "assigning variable delta_new");
      out__.write(delta_new);
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
  }
  template <typename VecVar, typename VecI,
            stan::require_vector_t<VecVar>* = nullptr,
            stan::require_vector_like_vt<std::is_integral, VecI>* = nullptr>
  inline void
  unconstrain_array_impl(const VecVar& params_r__, const VecI& params_i__,
                         VecVar& vars__, std::ostream* pstream__ = nullptr) const {
    using local_scalar_t__ = double;
    stan::io::deserializer<local_scalar_t__> in__(params_r__, params_i__);
    stan::io::serializer<local_scalar_t__> out__(vars__);
    int current_statement__ = 0;
    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    // suppress unused var warning
    (void) DUMMY_VAR__;
    try {
      int pos__ = std::numeric_limits<int>::min();
      pos__ = 1;
      Eigen::Matrix<local_scalar_t__,-1,1> d =
        Eigen::Matrix<local_scalar_t__,-1,1>::Constant(d_1dim__, DUMMY_VAR__);
      current_statement__ = 1;
      stan::model::assign(d,
        in__.read<Eigen::Matrix<local_scalar_t__,-1,1>>(d_1dim__),
        "assigning variable d");
      out__.write(d);
      local_scalar_t__ tau = DUMMY_VAR__;
      current_statement__ = 2;
      tau = in__.read<local_scalar_t__>();
      out__.write_free_lb(0, tau);
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
  }
  template <typename VecVar, stan::require_vector_t<VecVar>* = nullptr>
  inline void
  transform_inits_impl(const stan::io::var_context& context__, VecVar&
                       vars__, std::ostream* pstream__ = nullptr) const {
    using local_scalar_t__ = double;
    stan::io::serializer<local_scalar_t__> out__(vars__);
    int current_statement__ = 0;
    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    // suppress unused var warning
    (void) DUMMY_VAR__;
    try {
      current_statement__ = 1;
      context__.validate_dims("parameter initialization", "d", "double",
        std::vector<size_t>{static_cast<size_t>(d_1dim__)});
      current_statement__ = 2;
      context__.validate_dims("parameter initialization", "tau", "double",
        std::vector<size_t>{});
      int pos__ = std::numeric_limits<int>::min();
      pos__ = 1;
      Eigen::Matrix<local_scalar_t__,-1,1> d =
        Eigen::Matrix<local_scalar_t__,-1,1>::Constant(d_1dim__, DUMMY_VAR__);
      {
        std::vector<local_scalar_t__> d_flat__;
        current_statement__ = 1;
        d_flat__ = context__.vals_r("d");
        current_statement__ = 1;
        pos__ = 1;
        current_statement__ = 1;
        for (int sym1__ = 1; sym1__ <= d_1dim__; ++sym1__) {
          current_statement__ = 1;
          stan::model::assign(d, d_flat__[(pos__ - 1)],
            "assigning variable d", stan::model::index_uni(sym1__));
          current_statement__ = 1;
          pos__ = (pos__ + 1);
        }
      }
      out__.write(d);
      local_scalar_t__ tau = DUMMY_VAR__;
      current_statement__ = 2;
      tau = context__.vals_r("tau")[(1 - 1)];
      out__.write_free_lb(0, tau);
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
  }
  inline void
  get_param_names(std::vector<std::string>& names__, const bool
                  emit_transformed_parameters__ = true, const bool
                  emit_generated_quantities__ = true) const {
    names__ = std::vector<std::string>{"d", "tau"};
    if (emit_transformed_parameters__) {}
    if (emit_generated_quantities__) {
      std::vector<std::string> temp{"delta_new"};
      names__.reserve(names__.size() + temp.size());
      names__.insert(names__.end(), temp.begin(), temp.end());
    }
  }
  inline void
  get_dims(std::vector<std::vector<size_t>>& dimss__, const bool
           emit_transformed_parameters__ = true, const bool
           emit_generated_quantities__ = true) const {
    dimss__ = std::vector<std::vector<size_t>>{std::vector<size_t>{static_cast<
                                                                    size_t>(
                                                                    d_1dim__)},
                std::vector<size_t>{}};
    if (emit_transformed_parameters__) {}
    if (emit_generated_quantities__) {
      std::vector<std::vector<size_t>>
        temp{std::vector<size_t>{static_cast<size_t>(delta_new_1dim__)}};
      dimss__.reserve(dimss__.size() + temp.size());
      dimss__.insert(dimss__.end(), temp.begin(), temp.end());
    }
  }
  inline void
  constrained_param_names(std::vector<std::string>& param_names__, bool
                          emit_transformed_parameters__ = true, bool
                          emit_generated_quantities__ = true) const final {
    for (int sym1__ = 1; sym1__ <= d_1dim__; ++sym1__) {
      param_names__.emplace_back(std::string() + "d" + '.' +
        std::to_string(sym1__));
    }
    param_names__.emplace_back(std::string() + "tau");
    if (emit_transformed_parameters__) {}
    if (emit_generated_quantities__) {
      for (int sym1__ = 1; sym1__ <= delta_new_1dim__; ++sym1__) {
        param_names__.emplace_back(std::string() + "delta_new" + '.' +
          std::to_string(sym1__));
      }
    }
  }
  inline void
  unconstrained_param_names(std::vector<std::string>& param_names__, bool
                            emit_transformed_parameters__ = true, bool
                            emit_generated_quantities__ = true) const final {
    for (int sym1__ = 1; sym1__ <= d_1dim__; ++sym1__) {
      param_names__.emplace_back(std::string() + "d" + '.' +
        std::to_string(sym1__));
    }
    param_names__.emplace_back(std::string() + "tau");
    if (emit_transformed_parameters__) {}
    if (emit_generated_quantities__) {
      for (int sym1__ = 1; sym1__ <= delta_new_1dim__; ++sym1__) {
        param_names__.emplace_back(std::string() + "delta_new" + '.' +
          std::to_string(sym1__));
      }
    }
  }
  inline std::string get_constrained_sizedtypes() const {
    return std::string("[{\"name\":\"d\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(d_1dim__) + "},\"block\":\"parameters\"},{\"name\":\"tau\",\"type\":{\"name\":\"real\"},\"block\":\"parameters\"},{\"name\":\"delta_new\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(delta_new_1dim__) + "},\"block\":\"generated_quantities\"}]");
  }
  inline std::string get_unconstrained_sizedtypes() const {
    return std::string("[{\"name\":\"d\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(d_1dim__) + "},\"block\":\"parameters\"},{\"name\":\"tau\",\"type\":{\"name\":\"real\"},\"block\":\"parameters\"},{\"name\":\"delta_new\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(delta_new_1dim__) + "},\"block\":\"generated_quantities\"}]");
  }
  // Begin method overload boilerplate
  template <typename RNG> inline void
  write_array(RNG& base_rng, Eigen::Matrix<double,-1,1>& params_r,
              Eigen::Matrix<double,-1,1>& vars, const bool
              emit_transformed_parameters = true, const bool
              emit_generated_quantities = true, std::ostream*
              pstream = nullptr) const {
    const size_t num_params__ = (d_1dim__ + 1);
    const size_t num_transformed = emit_transformed_parameters * (0);
    const size_t num_gen_quantities = emit_generated_quantities *
      (delta_new_1dim__);
    const size_t num_to_write = num_params__ + num_transformed +
      num_gen_quantities;
    std::vector<int> params_i;
    vars = Eigen::Matrix<double,-1,1>::Constant(num_to_write,
             std::numeric_limits<double>::quiet_NaN());
    write_array_impl(base_rng, params_r, params_i, vars,
      emit_transformed_parameters, emit_generated_quantities, pstream);
  }
  template <typename RNG> inline void
  write_array(RNG& base_rng, std::vector<double>& params_r, std::vector<int>&
              params_i, std::vector<double>& vars, bool
              emit_transformed_parameters = true, bool
              emit_generated_quantities = true, std::ostream*
              pstream = nullptr) const {
    const size_t num_params__ = (d_1dim__ + 1);
    const size_t num_transformed = emit_transformed_parameters * (0);
    const size_t num_gen_quantities = emit_generated_quantities *
      (delta_new_1dim__);
    const size_t num_to_write = num_params__ + num_transformed +
      num_gen_quantities;
    vars = std::vector<double>(num_to_write,
             std::numeric_limits<double>::quiet_NaN());
    write_array_impl(base_rng, params_r, params_i, vars,
      emit_transformed_parameters, emit_generated_quantities, pstream);
  }
  template <bool propto__, bool jacobian__, typename T_> inline T_
  log_prob(Eigen::Matrix<T_,-1,1>& params_r, std::ostream* pstream = nullptr) const {
    Eigen::Matrix<int,-1,1> params_i;
    return log_prob_impl<propto__, jacobian__>(params_r, params_i, pstream);
  }
  template <bool propto__, bool jacobian__, typename T_> inline T_
  log_prob(std::vector<T_>& params_r, std::vector<int>& params_i,
           std::ostream* pstream = nullptr) const {
    return log_prob_impl<propto__, jacobian__>(params_r, params_i, pstream);
  }
  inline void
  transform_inits(const stan::io::var_context& context,
                  Eigen::Matrix<double,-1,1>& params_r, std::ostream*
                  pstream = nullptr) const final {
    std::vector<double> params_r_vec(params_r.size());
    std::vector<int> params_i;
    transform_inits(context, params_i, params_r_vec, pstream);
    params_r = Eigen::Map<Eigen::Matrix<double,-1,1>>(params_r_vec.data(),
                 params_r_vec.size());
  }
  inline void
  transform_inits(const stan::io::var_context& context, std::vector<int>&
                  params_i, std::vector<double>& vars, std::ostream*
                  pstream__ = nullptr) const {
    vars.resize(num_params_r__);
    transform_inits_impl(context, vars, pstream__);
  }
  inline void
  unconstrain_array(const std::vector<double>& params_constrained,
                    std::vector<double>& params_unconstrained, std::ostream*
                    pstream = nullptr) const {
    const std::vector<int> params_i;
    params_unconstrained = std::vector<double>(num_params_r__,
                             std::numeric_limits<double>::quiet_NaN());
    unconstrain_array_impl(params_constrained, params_i,
      params_unconstrained, pstream);
  }
  inline void
  unconstrain_array(const Eigen::Matrix<double,-1,1>& params_constrained,
                    Eigen::Matrix<double,-1,1>& params_unconstrained,
                    std::ostream* pstream = nullptr) const {
    const std::vector<int> params_i;
    params_unconstrained = Eigen::Matrix<double,-1,1>::Constant(num_params_r__,
                             std::numeric_limits<double>::quiet_NaN());
    unconstrain_array_impl(params_constrained, params_i,
      params_unconstrained, pstream);
  }
};
}
using stan_model = model_predict_delta_new_namespace::model_predict_delta_new;
#ifndef USING_R
// Boilerplate
stan::model::model_base&
new_model(stan::io::var_context& data_context, unsigned int seed,
          std::ostream* msg_stream) {
  stan_model* m = new stan_model(data_context, seed, msg_stream);
  return *m;
}
stan::math::profile_map& get_stan_profile_data() {
  return model_predict_delta_new_namespace::profiles__;
}
#endif
#endif
