From a4929167a6a51c33912caab8f7ae1561c836fe53 Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Fri, 17 Oct 2025 11:35:08 -0400 Subject: [PATCH 1/2] Add examples of codegen Needed because compiler elides them if it sees literals it knows will pass --- test/integration/good/code-gen/cpp.expected | 425 +++++++++++------- .../good/code-gen/stochastic_matrices.stan | 8 +- .../good/code-gen/sum_to_zero.stan | 10 +- 3 files changed, 269 insertions(+), 174 deletions(-) diff --git a/test/integration/good/code-gen/cpp.expected b/test/integration/good/code-gen/cpp.expected index 4b926e6d91..8ec77a4d47 100644 --- a/test/integration/good/code-gen/cpp.expected +++ b/test/integration/good/code-gen/cpp.expected @@ -52223,28 +52223,38 @@ namespace stochastic_matrices_model_namespace { using stan::model::model_base_crtp; using namespace stan::math; stan::math::profile_map profiles__; -static constexpr std::array locations_array__ = +static constexpr std::array locations_array__ = {" (found before start of program)", - " (in 'stochastic_matrices.stan', line 13, column 3 to column 42)", - " (in 'stochastic_matrices.stan', line 14, column 3 to column 39)", - " (in 'stochastic_matrices.stan', line 15, column 3 to column 52)", - " (in 'stochastic_matrices.stan', line 19, column 3 to column 51)", - " (in 'stochastic_matrices.stan', line 20, column 3 to column 48)", - " (in 'stochastic_matrices.stan', line 21, column 3 to column 62)", - " (in 'stochastic_matrices.stan', line 25, column 3 to column 52)", - " (in 'stochastic_matrices.stan', line 26, column 3 to column 49)", - " (in 'stochastic_matrices.stan', line 27, column 3 to column 63)", + " (in 'stochastic_matrices.stan', line 15, column 3 to column 46)", + " (in 'stochastic_matrices.stan', line 16, column 3 to column 43)", + " (in 'stochastic_matrices.stan', line 17, column 3 to column 56)", + " (in 'stochastic_matrices.stan', line 21, column 3 to column 51)", + " (in 'stochastic_matrices.stan', line 22, column 3 to column 48)", + " (in 'stochastic_matrices.stan', line 23, column 3 to column 62)", + " (in 'stochastic_matrices.stan', line 27, column 3 to column 52)", + " (in 'stochastic_matrices.stan', line 28, column 3 to column 49)", + " (in 'stochastic_matrices.stan', line 29, column 3 to column 63)", " (in 'stochastic_matrices.stan', line 2, column 3 to column 42)", " (in 'stochastic_matrices.stan', line 3, column 3 to column 39)", " (in 'stochastic_matrices.stan', line 4, column 3 to column 52)", - " (in 'stochastic_matrices.stan', line 8, column 3 to column 51)", - " (in 'stochastic_matrices.stan', line 9, column 3 to column 48)", - " (in 'stochastic_matrices.stan', line 10, column 3 to column 53)"}; + " (in 'stochastic_matrices.stan', line 8, column 3 to column 17)", + " (in 'stochastic_matrices.stan', line 9, column 3 to column 17)", + " (in 'stochastic_matrices.stan', line 10, column 3 to column 51)", + " (in 'stochastic_matrices.stan', line 11, column 3 to column 48)", + " (in 'stochastic_matrices.stan', line 12, column 3 to column 53)", + " (in 'stochastic_matrices.stan', line 15, column 28 to column 32)", + " (in 'stochastic_matrices.stan', line 15, column 34 to column 38)", + " (in 'stochastic_matrices.stan', line 16, column 25 to column 29)", + " (in 'stochastic_matrices.stan', line 16, column 31 to column 35)", + " (in 'stochastic_matrices.stan', line 17, column 37 to column 41)", + " (in 'stochastic_matrices.stan', line 17, column 43 to column 47)"}; class stochastic_matrices_model final : public model_base_crtp { private: Eigen::Matrix d_csm_data__; Eigen::Matrix d_rsm_data__; std::vector>> d_arsm; + int ROWS; + int COLS; Eigen::Matrix td_csm_data__; Eigen::Matrix td_rsm_data__; std::vector>> td_arsm; @@ -52352,35 +52362,55 @@ class stochastic_matrices_model final : public model_base_crtp::min(); + current_statement__ = 13; + ROWS = 10; + current_statement__ = 14; + COLS = std::numeric_limits::min(); + current_statement__ = 14; + COLS = 10; + current_statement__ = 15; td_csm_data__ = Eigen::Matrix::Constant(10, 10, std::numeric_limits::quiet_NaN()); new (&td_csm) Eigen::Map>(td_csm_data__.data(), 10, 10); - current_statement__ = 13; + current_statement__ = 15; stan::model::assign(td_csm, d_csm, "assigning variable td_csm"); - current_statement__ = 14; + current_statement__ = 16; td_rsm_data__ = Eigen::Matrix::Constant(10, 10, std::numeric_limits::quiet_NaN()); new (&td_rsm) Eigen::Map>(td_rsm_data__.data(), 10, 10); - current_statement__ = 14; + current_statement__ = 16; stan::model::assign(td_rsm, d_rsm, "assigning variable td_rsm"); - current_statement__ = 15; + current_statement__ = 17; td_arsm = std::vector>>(2, std::vector>(2, Eigen::Matrix::Constant(10, 10, std::numeric_limits::quiet_NaN()))); - current_statement__ = 13; + current_statement__ = 15; stan::math::check_stochastic_column(function__, "td_csm", td_csm); - current_statement__ = 14; + current_statement__ = 16; stan::math::check_stochastic_row(function__, "td_rsm", td_rsm); - current_statement__ = 15; + current_statement__ = 17; stan::math::check_stochastic_row(function__, "td_arsm", td_arsm); + current_statement__ = 18; + stan::math::validate_non_negative_index("p_csm", "ROWS", ROWS); + current_statement__ = 19; + stan::math::validate_non_negative_index("p_csm", "COLS", COLS); + current_statement__ = 20; + stan::math::validate_non_negative_index("p_rsm", "ROWS", ROWS); + current_statement__ = 21; + stan::math::validate_non_negative_index("p_rsm", "COLS", COLS); + current_statement__ = 22; + stan::math::validate_non_negative_index("p_arsm", "ROWS", ROWS); + current_statement__ = 23; + stan::math::validate_non_negative_index("p_arsm", "COLS", COLS); } catch (const std::exception& e) { stan::lang::rethrow_located(e, locations_array__[current_statement__]); } - num_params_r__ = ((10 - 1) * 10) + (10 * (10 - 1)) + (2 * (2 * (10 * (10 - - 1)))); + num_params_r__ = ((ROWS - 1) * COLS) + (ROWS * (COLS - 1)) + (2 * (2 * + (ROWS * (COLS - 1)))); } inline std::string model_name() const final { return "stochastic_matrices_model"; @@ -52416,16 +52446,18 @@ class stochastic_matrices_model final : public model_base_crtp, jacobian__>(lp__, 10, 10); + Eigen::Matrix, jacobian__>(lp__, ROWS, + COLS); current_statement__ = 2; auto p_rsm = in__.template read_constrain_stochastic_row< - Eigen::Matrix, jacobian__>(lp__, 10, 10); + Eigen::Matrix, jacobian__>(lp__, ROWS, + COLS); current_statement__ = 3; auto p_arsm = in__.template read_constrain_stochastic_row< std::vector>>, - jacobian__>(lp__, 2, 2, 10, 10); + jacobian__>(lp__, 2, 2, ROWS, COLS); Eigen::Matrix tp_csm = Eigen::Matrix::Constant(10, 10, DUMMY_VAR__); current_statement__ = 4; @@ -52481,16 +52513,18 @@ class stochastic_matrices_model final : public model_base_crtp, jacobian__>(lp__, 10, 10); + Eigen::Matrix, jacobian__>(lp__, ROWS, + COLS); current_statement__ = 2; auto p_rsm = in__.template read_constrain_stochastic_row< - Eigen::Matrix, jacobian__>(lp__, 10, 10); + Eigen::Matrix, jacobian__>(lp__, ROWS, + COLS); current_statement__ = 3; auto p_arsm = in__.template read_constrain_stochastic_row< std::vector>>, - jacobian__>(lp__, 2, 2, 10, 10); + jacobian__>(lp__, 2, 2, ROWS, COLS); Eigen::Matrix tp_csm = Eigen::Matrix::Constant(10, 10, DUMMY_VAR__); current_statement__ = 4; @@ -52557,16 +52591,18 @@ class stochastic_matrices_model final : public model_base_crtp, jacobian__>(lp__, 10, 10); + Eigen::Matrix, jacobian__>(lp__, ROWS, + COLS); current_statement__ = 2; auto p_rsm = in__.template read_constrain_stochastic_row< - Eigen::Matrix, jacobian__>(lp__, 10, 10); + Eigen::Matrix, jacobian__>(lp__, ROWS, + COLS); current_statement__ = 3; auto p_arsm = in__.template read_constrain_stochastic_row< std::vector>>, - jacobian__>(lp__, 2, 2, 10, 10); + jacobian__>(lp__, 2, 2, ROWS, COLS); Eigen::Matrix tp_csm = Eigen::Matrix::Constant(10, 10, std::numeric_limits::quiet_NaN()); @@ -52581,8 +52617,8 @@ class stochastic_matrices_model final : public model_base_crtp p_csm = - Eigen::Matrix::Constant(10, 10, DUMMY_VAR__); + Eigen::Matrix::Constant(ROWS, COLS, + DUMMY_VAR__); current_statement__ = 1; stan::model::assign(p_csm, - in__.read>(10, 10), + in__.read>(ROWS, COLS), "assigning variable p_csm"); out__.write_free_stochastic_column(p_csm); Eigen::Matrix p_rsm = - Eigen::Matrix::Constant(10, 10, DUMMY_VAR__); + Eigen::Matrix::Constant(ROWS, COLS, + DUMMY_VAR__); current_statement__ = 2; stan::model::assign(p_rsm, - in__.read>(10, 10), + in__.read>(ROWS, COLS), "assigning variable p_rsm"); out__.write_free_stochastic_row(p_rsm); std::vector>> p_arsm = std::vector>>(2, std::vector>(2, - Eigen::Matrix::Constant(10, 10, + Eigen::Matrix::Constant(ROWS, COLS, DUMMY_VAR__))); current_statement__ = 3; - for (int sym1__ = 1; sym1__ <= 10; ++sym1__) { - for (int sym2__ = 1; sym2__ <= 10; ++sym2__) { + for (int sym1__ = 1; sym1__ <= COLS; ++sym1__) { + for (int sym2__ = 1; sym2__ <= ROWS; ++sym2__) { for (int sym3__ = 1; sym3__ <= 2; ++sym3__) { for (int sym4__ = 1; sym4__ <= 2; ++sym4__) { current_statement__ = 3; @@ -52746,25 +52784,28 @@ class stochastic_matrices_model final : public model_base_crtp{static_cast(10), static_cast(10)}); + std::vector{static_cast(ROWS), + static_cast(COLS)}); current_statement__ = 2; context__.validate_dims("parameter initialization", "p_rsm", "double", - std::vector{static_cast(10), static_cast(10)}); + std::vector{static_cast(ROWS), + static_cast(COLS)}); current_statement__ = 3; context__.validate_dims("parameter initialization", "p_arsm", "double", std::vector{static_cast(2), static_cast(2), - static_cast(10), static_cast(10)}); + static_cast(ROWS), static_cast(COLS)}); int pos__ = std::numeric_limits::min(); pos__ = 1; Eigen::Matrix p_csm = - Eigen::Matrix::Constant(10, 10, DUMMY_VAR__); + Eigen::Matrix::Constant(ROWS, COLS, + DUMMY_VAR__); { std::vector p_csm_flat__; current_statement__ = 1; p_csm_flat__ = context__.vals_r("p_csm"); pos__ = 1; - for (int sym1__ = 1; sym1__ <= 10; ++sym1__) { - for (int sym2__ = 1; sym2__ <= 10; ++sym2__) { + for (int sym1__ = 1; sym1__ <= COLS; ++sym1__) { + for (int sym2__ = 1; sym2__ <= ROWS; ++sym2__) { stan::model::assign(p_csm, p_csm_flat__[(pos__ - 1)], "assigning variable p_csm", stan::model::index_uni(sym2__), stan::model::index_uni(sym1__)); @@ -52774,14 +52815,15 @@ class stochastic_matrices_model final : public model_base_crtp p_rsm = - Eigen::Matrix::Constant(10, 10, DUMMY_VAR__); + Eigen::Matrix::Constant(ROWS, COLS, + DUMMY_VAR__); { std::vector p_rsm_flat__; current_statement__ = 2; p_rsm_flat__ = context__.vals_r("p_rsm"); pos__ = 1; - for (int sym1__ = 1; sym1__ <= 10; ++sym1__) { - for (int sym2__ = 1; sym2__ <= 10; ++sym2__) { + for (int sym1__ = 1; sym1__ <= COLS; ++sym1__) { + for (int sym2__ = 1; sym2__ <= ROWS; ++sym2__) { stan::model::assign(p_rsm, p_rsm_flat__[(pos__ - 1)], "assigning variable p_rsm", stan::model::index_uni(sym2__), stan::model::index_uni(sym1__)); @@ -52794,15 +52836,15 @@ class stochastic_matrices_model final : public model_base_crtp>>(2, std::vector>(2, - Eigen::Matrix::Constant(10, 10, + Eigen::Matrix::Constant(ROWS, COLS, DUMMY_VAR__))); { std::vector p_arsm_flat__; current_statement__ = 3; p_arsm_flat__ = context__.vals_r("p_arsm"); pos__ = 1; - for (int sym1__ = 1; sym1__ <= 10; ++sym1__) { - for (int sym2__ = 1; sym2__ <= 10; ++sym2__) { + for (int sym1__ = 1; sym1__ <= COLS; ++sym1__) { + for (int sym2__ = 1; sym2__ <= ROWS; ++sym2__) { for (int sym3__ = 1; sym3__ <= 2; ++sym3__) { for (int sym4__ = 1; sym4__ <= 2; ++sym4__) { stan::model::assign(p_arsm, p_arsm_flat__[(pos__ - 1)], @@ -52844,13 +52886,13 @@ class stochastic_matrices_model final : public model_base_crtp>{std::vector{static_cast< size_t>( - 10), - static_cast(10)}, - std::vector{static_cast(10), - static_cast(10)}, + ROWS), + static_cast(COLS)}, + std::vector{static_cast(ROWS), + static_cast(COLS)}, std::vector{static_cast(2), - static_cast(2), static_cast(10), - static_cast(10)}}; + static_cast(2), static_cast(ROWS), + static_cast(COLS)}}; if (emit_transformed_parameters__) { std::vector> temp{std::vector{static_cast(10), @@ -52880,20 +52922,20 @@ class stochastic_matrices_model final : public model_base_crtp& param_names__, bool emit_transformed_parameters__ = true, bool emit_generated_quantities__ = true) const final { - for (int sym1__ = 1; sym1__ <= 10; ++sym1__) { - for (int sym2__ = 1; sym2__ <= 10; ++sym2__) { + for (int sym1__ = 1; sym1__ <= COLS; ++sym1__) { + for (int sym2__ = 1; sym2__ <= ROWS; ++sym2__) { param_names__.emplace_back(std::string() + "p_csm" + '.' + std::to_string(sym2__) + '.' + std::to_string(sym1__)); } } - for (int sym1__ = 1; sym1__ <= 10; ++sym1__) { - for (int sym2__ = 1; sym2__ <= 10; ++sym2__) { + for (int sym1__ = 1; sym1__ <= COLS; ++sym1__) { + for (int sym2__ = 1; sym2__ <= ROWS; ++sym2__) { param_names__.emplace_back(std::string() + "p_rsm" + '.' + std::to_string(sym2__) + '.' + std::to_string(sym1__)); } } - for (int sym1__ = 1; sym1__ <= 10; ++sym1__) { - for (int sym2__ = 1; sym2__ <= 10; ++sym2__) { + for (int sym1__ = 1; sym1__ <= COLS; ++sym1__) { + for (int sym2__ = 1; sym2__ <= ROWS; ++sym2__) { for (int sym3__ = 1; sym3__ <= 2; ++sym3__) { for (int sym4__ = 1; sym4__ <= 2; ++sym4__) { param_names__.emplace_back(std::string() + "p_arsm" + '.' + @@ -52958,20 +53000,20 @@ class stochastic_matrices_model final : public model_base_crtp& param_names__, bool emit_transformed_parameters__ = true, bool emit_generated_quantities__ = true) const final { - for (int sym1__ = 1; sym1__ <= 10; ++sym1__) { - for (int sym2__ = 1; sym2__ <= (10 - 1); ++sym2__) { + for (int sym1__ = 1; sym1__ <= COLS; ++sym1__) { + for (int sym2__ = 1; sym2__ <= (ROWS - 1); ++sym2__) { param_names__.emplace_back(std::string() + "p_csm" + '.' + std::to_string(sym2__) + '.' + std::to_string(sym1__)); } } - for (int sym1__ = 1; sym1__ <= (10 - 1); ++sym1__) { - for (int sym2__ = 1; sym2__ <= 10; ++sym2__) { + for (int sym1__ = 1; sym1__ <= (COLS - 1); ++sym1__) { + for (int sym2__ = 1; sym2__ <= ROWS; ++sym2__) { param_names__.emplace_back(std::string() + "p_rsm" + '.' + std::to_string(sym2__) + '.' + std::to_string(sym1__)); } } - for (int sym1__ = 1; sym1__ <= (10 - 1); ++sym1__) { - for (int sym2__ = 1; sym2__ <= 10; ++sym2__) { + for (int sym1__ = 1; sym1__ <= (COLS - 1); ++sym1__) { + for (int sym2__ = 1; sym2__ <= ROWS; ++sym2__) { for (int sym3__ = 1; sym3__ <= 2; ++sym3__) { for (int sym4__ = 1; sym4__ <= 2; ++sym4__) { param_names__.emplace_back(std::string() + "p_arsm" + '.' + @@ -53033,10 +53075,10 @@ class stochastic_matrices_model final : public model_base_crtp inline void @@ -53045,7 +53087,8 @@ class stochastic_matrices_model final : public model_base_crtp locations_array__ = +static constexpr std::array locations_array__ = {" (found before start of program)", - " (in 'sum_to_zero.stan', line 15, column 3 to column 33)", - " (in 'sum_to_zero.stan', line 16, column 3 to column 46)", - " (in 'sum_to_zero.stan', line 17, column 3 to column 34)", - " (in 'sum_to_zero.stan', line 18, column 3 to column 47)", - " (in 'sum_to_zero.stan', line 22, column 3 to column 43)", - " (in 'sum_to_zero.stan', line 23, column 3 to column 57)", - " (in 'sum_to_zero.stan', line 24, column 3 to column 44)", - " (in 'sum_to_zero.stan', line 25, column 3 to column 58)", - " (in 'sum_to_zero.stan', line 29, column 3 to column 44)", - " (in 'sum_to_zero.stan', line 30, column 3 to column 58)", - " (in 'sum_to_zero.stan', line 31, column 3 to column 45)", - " (in 'sum_to_zero.stan', line 32, column 3 to column 59)", + " (in 'sum_to_zero.stan', line 17, column 3 to column 32)", + " (in 'sum_to_zero.stan', line 18, column 3 to column 45)", + " (in 'sum_to_zero.stan', line 19, column 3 to column 36)", + " (in 'sum_to_zero.stan', line 20, column 3 to column 49)", + " (in 'sum_to_zero.stan', line 24, column 3 to column 43)", + " (in 'sum_to_zero.stan', line 25, column 3 to column 57)", + " (in 'sum_to_zero.stan', line 26, column 3 to column 44)", + " (in 'sum_to_zero.stan', line 27, column 3 to column 58)", + " (in 'sum_to_zero.stan', line 31, column 3 to column 44)", + " (in 'sum_to_zero.stan', line 32, column 3 to column 58)", + " (in 'sum_to_zero.stan', line 33, column 3 to column 45)", + " (in 'sum_to_zero.stan', line 34, column 3 to column 59)", " (in 'sum_to_zero.stan', line 2, column 3 to column 33)", " (in 'sum_to_zero.stan', line 3, column 3 to column 46)", " (in 'sum_to_zero.stan', line 4, column 3 to column 34)", " (in 'sum_to_zero.stan', line 5, column 3 to column 47)", - " (in 'sum_to_zero.stan', line 9, column 3 to column 43)", - " (in 'sum_to_zero.stan', line 10, column 3 to column 47)", - " (in 'sum_to_zero.stan', line 11, column 3 to column 44)", - " (in 'sum_to_zero.stan', line 12, column 3 to column 48)"}; + " (in 'sum_to_zero.stan', line 9, column 3 to column 14)", + " (in 'sum_to_zero.stan', line 10, column 3 to column 13)", + " (in 'sum_to_zero.stan', line 11, column 3 to column 43)", + " (in 'sum_to_zero.stan', line 12, column 3 to column 47)", + " (in 'sum_to_zero.stan', line 13, column 3 to column 44)", + " (in 'sum_to_zero.stan', line 14, column 3 to column 48)", + " (in 'sum_to_zero.stan', line 17, column 22 to column 23)", + " (in 'sum_to_zero.stan', line 18, column 34 to column 35)", + " (in 'sum_to_zero.stan', line 19, column 22 to column 23)", + " (in 'sum_to_zero.stan', line 19, column 24 to column 27)", + " (in 'sum_to_zero.stan', line 20, column 34 to column 35)", + " (in 'sum_to_zero.stan', line 20, column 36 to column 39)"}; class sum_to_zero_model final : public model_base_crtp { private: Eigen::Matrix d_stzv_data__; std::vector>> d_astzv; Eigen::Matrix d_stzm_data__; std::vector>> d_astzm; + int v; + int N; Eigen::Matrix td_stzv_data__; std::vector>> td_astzv; Eigen::Matrix td_stzm_data__; std::vector>> td_astzm; + int p_stzm_2dim__; + int p_astzm_4dim__; Eigen::Map> d_stzv{nullptr, 0}; Eigen::Map> d_stzm{nullptr, 0, 0}; Eigen::Map> td_stzv{nullptr, 0}; @@ -53306,42 +53362,72 @@ class sum_to_zero_model final : public model_base_crtp { current_statement__ = 16; stan::math::check_sum_to_zero(function__, "d_astzm", d_astzm); current_statement__ = 17; + v = std::numeric_limits::min(); + current_statement__ = 17; + v = 10; + current_statement__ = 18; + N = std::numeric_limits::min(); + current_statement__ = 18; + N = 4; + current_statement__ = 19; td_stzv_data__ = Eigen::Matrix::Constant(10, std::numeric_limits::quiet_NaN()); new (&td_stzv) Eigen::Map>(td_stzv_data__.data(), 10); - current_statement__ = 17; + current_statement__ = 19; stan::model::assign(td_stzv, d_stzv, "assigning variable td_stzv"); - current_statement__ = 18; + current_statement__ = 20; td_astzv = std::vector>>(2, std::vector>(3, Eigen::Matrix::Constant(10, std::numeric_limits::quiet_NaN()))); - current_statement__ = 19; + current_statement__ = 21; td_stzm_data__ = Eigen::Matrix::Constant(4, 5, std::numeric_limits::quiet_NaN()); new (&td_stzm) Eigen::Map>(td_stzm_data__.data(), 4, 5); - current_statement__ = 19; + current_statement__ = 21; stan::model::assign(td_stzm, d_stzm, "assigning variable td_stzm"); - current_statement__ = 20; + current_statement__ = 22; td_astzm = std::vector>>(2, std::vector>(3, Eigen::Matrix::Constant(4, 5, std::numeric_limits::quiet_NaN()))); - current_statement__ = 17; + current_statement__ = 19; stan::math::check_sum_to_zero(function__, "td_stzv", td_stzv); - current_statement__ = 18; + current_statement__ = 20; stan::math::check_sum_to_zero(function__, "td_astzv", td_astzv); - current_statement__ = 19; + current_statement__ = 21; stan::math::check_sum_to_zero(function__, "td_stzm", td_stzm); - current_statement__ = 20; + current_statement__ = 22; stan::math::check_sum_to_zero(function__, "td_astzm", td_astzm); + current_statement__ = 23; + stan::math::validate_non_negative_index("p_stzv", "v", v); + current_statement__ = 24; + stan::math::validate_non_negative_index("p_astzv", "v", v); + current_statement__ = 25; + stan::math::validate_non_negative_index("p_stzm", "N", N); + current_statement__ = 26; + p_stzm_2dim__ = std::numeric_limits::min(); + current_statement__ = 26; + p_stzm_2dim__ = (N + 1); + current_statement__ = 26; + stan::math::validate_non_negative_index("p_stzm", "N + 1", + p_stzm_2dim__); + current_statement__ = 27; + stan::math::validate_non_negative_index("p_astzm", "N", N); + current_statement__ = 28; + p_astzm_4dim__ = std::numeric_limits::min(); + current_statement__ = 28; + p_astzm_4dim__ = (N + 1); + current_statement__ = 28; + stan::math::validate_non_negative_index("p_astzm", "N + 1", + p_astzm_4dim__); } catch (const std::exception& e) { stan::lang::rethrow_located(e, locations_array__[current_statement__]); } - num_params_r__ = (10 - 1) + (2 * (3 * (10 - 1))) + ((4 - 1) * (5 - 1)) + - (2 * (3 * ((4 - 1) * (5 - 1)))); + num_params_r__ = (v - 1) + (2 * (3 * (v - 1))) + ((N - 1) * + (p_stzm_2dim__ - 1)) + (2 * (3 * ((N - 1) * (p_astzm_4dim__ - 1)))); } inline std::string model_name() const final { return "sum_to_zero_model"; @@ -53377,21 +53463,22 @@ class sum_to_zero_model final : public model_base_crtp { current_statement__ = 1; auto p_stzv = in__.template read_constrain_sum_to_zero< - Eigen::Matrix, jacobian__>(lp__, 10); + Eigen::Matrix, jacobian__>(lp__, v); current_statement__ = 2; auto p_astzv = in__.template read_constrain_sum_to_zero< std::vector>>, - jacobian__>(lp__, 2, 3, 10); + jacobian__>(lp__, 2, 3, v); current_statement__ = 3; auto p_stzm = in__.template read_constrain_sum_to_zero< - Eigen::Matrix, jacobian__>(lp__, 4, 5); + Eigen::Matrix, jacobian__>(lp__, N, + p_stzm_2dim__); current_statement__ = 4; auto p_astzm = in__.template read_constrain_sum_to_zero< std::vector>>, - jacobian__>(lp__, 2, 3, 4, 5); + jacobian__>(lp__, 2, 3, N, p_astzm_4dim__); Eigen::Matrix tp_stzv = Eigen::Matrix::Constant(10, DUMMY_VAR__); current_statement__ = 5; @@ -53456,21 +53543,22 @@ class sum_to_zero_model final : public model_base_crtp { current_statement__ = 1; auto p_stzv = in__.template read_constrain_sum_to_zero< - Eigen::Matrix, jacobian__>(lp__, 10); + Eigen::Matrix, jacobian__>(lp__, v); current_statement__ = 2; auto p_astzv = in__.template read_constrain_sum_to_zero< std::vector>>, - jacobian__>(lp__, 2, 3, 10); + jacobian__>(lp__, 2, 3, v); current_statement__ = 3; auto p_stzm = in__.template read_constrain_sum_to_zero< - Eigen::Matrix, jacobian__>(lp__, 4, 5); + Eigen::Matrix, jacobian__>(lp__, N, + p_stzm_2dim__); current_statement__ = 4; auto p_astzm = in__.template read_constrain_sum_to_zero< std::vector>>, - jacobian__>(lp__, 2, 3, 4, 5); + jacobian__>(lp__, 2, 3, N, p_astzm_4dim__); Eigen::Matrix tp_stzv = Eigen::Matrix::Constant(10, DUMMY_VAR__); current_statement__ = 5; @@ -53546,21 +53634,22 @@ class sum_to_zero_model final : public model_base_crtp { current_statement__ = 1; auto p_stzv = in__.template read_constrain_sum_to_zero< - Eigen::Matrix, jacobian__>(lp__, 10); + Eigen::Matrix, jacobian__>(lp__, v); current_statement__ = 2; auto p_astzv = in__.template read_constrain_sum_to_zero< std::vector>>, - jacobian__>(lp__, 2, 3, 10); + jacobian__>(lp__, 2, 3, v); current_statement__ = 3; auto p_stzm = in__.template read_constrain_sum_to_zero< - Eigen::Matrix, jacobian__>(lp__, 4, 5); + Eigen::Matrix, jacobian__>(lp__, N, + p_stzm_2dim__); current_statement__ = 4; auto p_astzm = in__.template read_constrain_sum_to_zero< std::vector>>, - jacobian__>(lp__, 2, 3, 4, 5); + jacobian__>(lp__, 2, 3, N, p_astzm_4dim__); Eigen::Matrix tp_stzv = Eigen::Matrix::Constant(10, std::numeric_limits::quiet_NaN()); @@ -53579,7 +53668,7 @@ class sum_to_zero_model final : public model_base_crtp { std::numeric_limits::quiet_NaN()))); out__.write(p_stzv); current_statement__ = 2; - for (int sym1__ = 1; sym1__ <= 10; ++sym1__) { + for (int sym1__ = 1; sym1__ <= v; ++sym1__) { for (int sym2__ = 1; sym2__ <= 3; ++sym2__) { for (int sym3__ = 1; sym3__ <= 2; ++sym3__) { out__.write(p_astzv[(sym3__ - 1)][(sym2__ - 1)][(sym1__ - 1)]); @@ -53588,8 +53677,8 @@ class sum_to_zero_model final : public model_base_crtp { } out__.write(p_stzm); current_statement__ = 4; - for (int sym1__ = 1; sym1__ <= 5; ++sym1__) { - for (int sym2__ = 1; sym2__ <= 4; ++sym2__) { + for (int sym1__ = 1; sym1__ <= p_astzm_4dim__; ++sym1__) { + for (int sym2__ = 1; sym2__ <= N; ++sym2__) { for (int sym3__ = 1; sym3__ <= 3; ++sym3__) { for (int sym4__ = 1; sym4__ <= 2; ++sym4__) { out__.write(stan::model::rvalue(p_astzm, "p_astzm", @@ -53728,19 +53817,19 @@ class sum_to_zero_model final : public model_base_crtp { (void) DUMMY_VAR__; try { Eigen::Matrix p_stzv = - Eigen::Matrix::Constant(10, DUMMY_VAR__); + Eigen::Matrix::Constant(v, DUMMY_VAR__); current_statement__ = 1; stan::model::assign(p_stzv, - in__.read>(10), + in__.read>(v), "assigning variable p_stzv"); out__.write_free_sum_to_zero(p_stzv); std::vector>> p_astzv = std::vector>>(2, std::vector>(3, - Eigen::Matrix::Constant(10, DUMMY_VAR__))); + Eigen::Matrix::Constant(v, DUMMY_VAR__))); current_statement__ = 2; - for (int sym1__ = 1; sym1__ <= 10; ++sym1__) { + for (int sym1__ = 1; sym1__ <= v; ++sym1__) { for (int sym2__ = 1; sym2__ <= 3; ++sym2__) { for (int sym3__ = 1; sym3__ <= 2; ++sym3__) { current_statement__ = 2; @@ -53752,21 +53841,22 @@ class sum_to_zero_model final : public model_base_crtp { } out__.write_free_sum_to_zero(p_astzv); Eigen::Matrix p_stzm = - Eigen::Matrix::Constant(4, 5, DUMMY_VAR__); + Eigen::Matrix::Constant(N, p_stzm_2dim__, + DUMMY_VAR__); current_statement__ = 3; stan::model::assign(p_stzm, - in__.read>(4, 5), + in__.read>(N, p_stzm_2dim__), "assigning variable p_stzm"); out__.write_free_sum_to_zero(p_stzm); std::vector>> p_astzm = std::vector>>(2, std::vector>(3, - Eigen::Matrix::Constant(4, 5, - DUMMY_VAR__))); + Eigen::Matrix::Constant(N, + p_astzm_4dim__, DUMMY_VAR__))); current_statement__ = 4; - for (int sym1__ = 1; sym1__ <= 5; ++sym1__) { - for (int sym2__ = 1; sym2__ <= 4; ++sym2__) { + for (int sym1__ = 1; sym1__ <= p_astzm_4dim__; ++sym1__) { + for (int sym2__ = 1; sym2__ <= N; ++sym2__) { for (int sym3__ = 1; sym3__ <= 3; ++sym3__) { for (int sym4__ = 1; sym4__ <= 2; ++sym4__) { current_statement__ = 4; @@ -53799,30 +53889,31 @@ class sum_to_zero_model final : public model_base_crtp { try { current_statement__ = 1; context__.validate_dims("parameter initialization", "p_stzv", "double", - std::vector{static_cast(10)}); + std::vector{static_cast(v)}); current_statement__ = 2; context__.validate_dims("parameter initialization", "p_astzv", "double", std::vector{static_cast(2), static_cast(3), - static_cast(10)}); + static_cast(v)}); current_statement__ = 3; context__.validate_dims("parameter initialization", "p_stzm", "double", - std::vector{static_cast(4), static_cast(5)}); + std::vector{static_cast(N), + static_cast(p_stzm_2dim__)}); current_statement__ = 4; context__.validate_dims("parameter initialization", "p_astzm", "double", std::vector{static_cast(2), static_cast(3), - static_cast(4), static_cast(5)}); + static_cast(N), static_cast(p_astzm_4dim__)}); int pos__ = std::numeric_limits::min(); pos__ = 1; Eigen::Matrix p_stzv = - Eigen::Matrix::Constant(10, DUMMY_VAR__); + Eigen::Matrix::Constant(v, DUMMY_VAR__); { std::vector p_stzv_flat__; current_statement__ = 1; p_stzv_flat__ = context__.vals_r("p_stzv"); pos__ = 1; - for (int sym1__ = 1; sym1__ <= 10; ++sym1__) { + for (int sym1__ = 1; sym1__ <= v; ++sym1__) { stan::model::assign(p_stzv, p_stzv_flat__[(pos__ - 1)], "assigning variable p_stzv", stan::model::index_uni(sym1__)); pos__ = (pos__ + 1); @@ -53833,13 +53924,13 @@ class sum_to_zero_model final : public model_base_crtp { p_astzv = std::vector>>(2, std::vector>(3, - Eigen::Matrix::Constant(10, DUMMY_VAR__))); + Eigen::Matrix::Constant(v, DUMMY_VAR__))); { std::vector p_astzv_flat__; current_statement__ = 2; p_astzv_flat__ = context__.vals_r("p_astzv"); pos__ = 1; - for (int sym1__ = 1; sym1__ <= 10; ++sym1__) { + for (int sym1__ = 1; sym1__ <= v; ++sym1__) { for (int sym2__ = 1; sym2__ <= 3; ++sym2__) { for (int sym3__ = 1; sym3__ <= 2; ++sym3__) { stan::model::assign(p_astzv, p_astzv_flat__[(pos__ - 1)], @@ -53853,14 +53944,15 @@ class sum_to_zero_model final : public model_base_crtp { } out__.write_free_sum_to_zero(p_astzv); Eigen::Matrix p_stzm = - Eigen::Matrix::Constant(4, 5, DUMMY_VAR__); + Eigen::Matrix::Constant(N, p_stzm_2dim__, + DUMMY_VAR__); { std::vector p_stzm_flat__; current_statement__ = 3; p_stzm_flat__ = context__.vals_r("p_stzm"); pos__ = 1; - for (int sym1__ = 1; sym1__ <= 5; ++sym1__) { - for (int sym2__ = 1; sym2__ <= 4; ++sym2__) { + for (int sym1__ = 1; sym1__ <= p_stzm_2dim__; ++sym1__) { + for (int sym2__ = 1; sym2__ <= N; ++sym2__) { stan::model::assign(p_stzm, p_stzm_flat__[(pos__ - 1)], "assigning variable p_stzm", stan::model::index_uni(sym2__), stan::model::index_uni(sym1__)); @@ -53873,15 +53965,15 @@ class sum_to_zero_model final : public model_base_crtp { p_astzm = std::vector>>(2, std::vector>(3, - Eigen::Matrix::Constant(4, 5, - DUMMY_VAR__))); + Eigen::Matrix::Constant(N, + p_astzm_4dim__, DUMMY_VAR__))); { std::vector p_astzm_flat__; current_statement__ = 4; p_astzm_flat__ = context__.vals_r("p_astzm"); pos__ = 1; - for (int sym1__ = 1; sym1__ <= 5; ++sym1__) { - for (int sym2__ = 1; sym2__ <= 4; ++sym2__) { + for (int sym1__ = 1; sym1__ <= p_astzm_4dim__; ++sym1__) { + for (int sym2__ = 1; sym2__ <= N; ++sym2__) { for (int sym3__ = 1; sym3__ <= 3; ++sym3__) { for (int sym4__ = 1; sym4__ <= 2; ++sym4__) { stan::model::assign(p_astzm, p_astzm_flat__[(pos__ - 1)], @@ -53925,15 +54017,14 @@ class sum_to_zero_model final : public model_base_crtp { emit_transformed_parameters__ = true, const bool emit_generated_quantities__ = true) const { dimss__ = std::vector>{std::vector{static_cast< - size_t>( - 10)}, + size_t>(v)}, std::vector{static_cast(2), - static_cast(3), static_cast(10)}, - std::vector{static_cast(4), - static_cast(5)}, + static_cast(3), static_cast(v)}, + std::vector{static_cast(N), + static_cast(p_stzm_2dim__)}, std::vector{static_cast(2), - static_cast(3), static_cast(4), - static_cast(5)}}; + static_cast(3), static_cast(N), + static_cast(p_astzm_4dim__)}}; if (emit_transformed_parameters__) { std::vector> temp{std::vector{static_cast(10)}, @@ -53965,11 +54056,11 @@ class sum_to_zero_model final : public model_base_crtp { constrained_param_names(std::vector& param_names__, bool emit_transformed_parameters__ = true, bool emit_generated_quantities__ = true) const final { - for (int sym1__ = 1; sym1__ <= 10; ++sym1__) { + for (int sym1__ = 1; sym1__ <= v; ++sym1__) { param_names__.emplace_back(std::string() + "p_stzv" + '.' + std::to_string(sym1__)); } - for (int sym1__ = 1; sym1__ <= 10; ++sym1__) { + for (int sym1__ = 1; sym1__ <= v; ++sym1__) { for (int sym2__ = 1; sym2__ <= 3; ++sym2__) { for (int sym3__ = 1; sym3__ <= 2; ++sym3__) { param_names__.emplace_back(std::string() + "p_astzv" + '.' + @@ -53978,14 +54069,14 @@ class sum_to_zero_model final : public model_base_crtp { } } } - for (int sym1__ = 1; sym1__ <= 5; ++sym1__) { - for (int sym2__ = 1; sym2__ <= 4; ++sym2__) { + for (int sym1__ = 1; sym1__ <= p_stzm_2dim__; ++sym1__) { + for (int sym2__ = 1; sym2__ <= N; ++sym2__) { param_names__.emplace_back(std::string() + "p_stzm" + '.' + std::to_string(sym2__) + '.' + std::to_string(sym1__)); } } - for (int sym1__ = 1; sym1__ <= 5; ++sym1__) { - for (int sym2__ = 1; sym2__ <= 4; ++sym2__) { + for (int sym1__ = 1; sym1__ <= p_astzm_4dim__; ++sym1__) { + for (int sym2__ = 1; sym2__ <= N; ++sym2__) { for (int sym3__ = 1; sym3__ <= 3; ++sym3__) { for (int sym4__ = 1; sym4__ <= 2; ++sym4__) { param_names__.emplace_back(std::string() + "p_astzm" + '.' + @@ -54064,11 +54155,11 @@ class sum_to_zero_model final : public model_base_crtp { unconstrained_param_names(std::vector& param_names__, bool emit_transformed_parameters__ = true, bool emit_generated_quantities__ = true) const final { - for (int sym1__ = 1; sym1__ <= (10 - 1); ++sym1__) { + for (int sym1__ = 1; sym1__ <= (v - 1); ++sym1__) { param_names__.emplace_back(std::string() + "p_stzv" + '.' + std::to_string(sym1__)); } - for (int sym1__ = 1; sym1__ <= (10 - 1); ++sym1__) { + for (int sym1__ = 1; sym1__ <= (v - 1); ++sym1__) { for (int sym2__ = 1; sym2__ <= 3; ++sym2__) { for (int sym3__ = 1; sym3__ <= 2; ++sym3__) { param_names__.emplace_back(std::string() + "p_astzv" + '.' + @@ -54077,14 +54168,14 @@ class sum_to_zero_model final : public model_base_crtp { } } } - for (int sym1__ = 1; sym1__ <= (5 - 1); ++sym1__) { - for (int sym2__ = 1; sym2__ <= (4 - 1); ++sym2__) { + for (int sym1__ = 1; sym1__ <= (p_stzm_2dim__ - 1); ++sym1__) { + for (int sym2__ = 1; sym2__ <= (N - 1); ++sym2__) { param_names__.emplace_back(std::string() + "p_stzm" + '.' + std::to_string(sym2__) + '.' + std::to_string(sym1__)); } } - for (int sym1__ = 1; sym1__ <= (5 - 1); ++sym1__) { - for (int sym2__ = 1; sym2__ <= (4 - 1); ++sym2__) { + for (int sym1__ = 1; sym1__ <= (p_astzm_4dim__ - 1); ++sym1__) { + for (int sym2__ = 1; sym2__ <= (N - 1); ++sym2__) { for (int sym3__ = 1; sym3__ <= 3; ++sym3__) { for (int sym4__ = 1; sym4__ <= 2; ++sym4__) { param_names__.emplace_back(std::string() + "p_astzm" + '.' + @@ -54160,10 +54251,10 @@ class sum_to_zero_model final : public model_base_crtp { } } inline std::string get_constrained_sizedtypes() const { - return std::string("[{\"name\":\"p_stzv\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(10) + "},\"block\":\"parameters\"},{\"name\":\"p_astzv\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"vector\",\"length\":" + std::to_string(10) + "}}},\"block\":\"parameters\"},{\"name\":\"p_stzm\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(4) + ",\"cols\":" + std::to_string(5) + "},\"block\":\"parameters\"},{\"name\":\"p_astzm\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(4) + ",\"cols\":" + std::to_string(5) + "}}},\"block\":\"parameters\"},{\"name\":\"tp_stzv\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(10) + "},\"block\":\"transformed_parameters\"},{\"name\":\"tp_astzv\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"vector\",\"length\":" + std::to_string(10) + "}}},\"block\":\"transformed_parameters\"},{\"name\":\"tp_stzm\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(4) + ",\"cols\":" + std::to_string(5) + "},\"block\":\"transformed_parameters\"},{\"name\":\"tp_astzm\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(4) + ",\"cols\":" + std::to_string(5) + "}}},\"block\":\"transformed_parameters\"},{\"name\":\"gq_stzv\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(10) + "},\"block\":\"generated_quantities\"},{\"name\":\"gq_astzv\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"vector\",\"length\":" + std::to_string(10) + "}}},\"block\":\"generated_quantities\"},{\"name\":\"gq_stzm\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(4) + ",\"cols\":" + std::to_string(5) + "},\"block\":\"generated_quantities\"},{\"name\":\"gq_astzm\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(4) + ",\"cols\":" + std::to_string(5) + "}}},\"block\":\"generated_quantities\"}]"); + return std::string("[{\"name\":\"p_stzv\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(v) + "},\"block\":\"parameters\"},{\"name\":\"p_astzv\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"vector\",\"length\":" + std::to_string(v) + "}}},\"block\":\"parameters\"},{\"name\":\"p_stzm\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(N) + ",\"cols\":" + std::to_string(p_stzm_2dim__) + "},\"block\":\"parameters\"},{\"name\":\"p_astzm\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(N) + ",\"cols\":" + std::to_string(p_astzm_4dim__) + "}}},\"block\":\"parameters\"},{\"name\":\"tp_stzv\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(10) + "},\"block\":\"transformed_parameters\"},{\"name\":\"tp_astzv\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"vector\",\"length\":" + std::to_string(10) + "}}},\"block\":\"transformed_parameters\"},{\"name\":\"tp_stzm\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(4) + ",\"cols\":" + std::to_string(5) + "},\"block\":\"transformed_parameters\"},{\"name\":\"tp_astzm\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(4) + ",\"cols\":" + std::to_string(5) + "}}},\"block\":\"transformed_parameters\"},{\"name\":\"gq_stzv\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(10) + "},\"block\":\"generated_quantities\"},{\"name\":\"gq_astzv\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"vector\",\"length\":" + std::to_string(10) + "}}},\"block\":\"generated_quantities\"},{\"name\":\"gq_stzm\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(4) + ",\"cols\":" + std::to_string(5) + "},\"block\":\"generated_quantities\"},{\"name\":\"gq_astzm\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(4) + ",\"cols\":" + std::to_string(5) + "}}},\"block\":\"generated_quantities\"}]"); } inline std::string get_unconstrained_sizedtypes() const { - return std::string("[{\"name\":\"p_stzv\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string((10 -1)) + "},\"block\":\"parameters\"},{\"name\":\"p_astzv\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"vector\",\"length\":" + std::to_string((10 -1)) + "}}},\"block\":\"parameters\"},{\"name\":\"p_stzm\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string((4 -1)) + ",\"cols\":" + std::to_string((5 -1)) + "},\"block\":\"parameters\"},{\"name\":\"p_astzm\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"matrix\",\"rows\":" + std::to_string((4 -1)) + ",\"cols\":" + std::to_string((5 -1)) + "}}},\"block\":\"parameters\"},{\"name\":\"tp_stzv\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string((10 -1)) + "},\"block\":\"transformed_parameters\"},{\"name\":\"tp_astzv\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"vector\",\"length\":" + std::to_string((10 -1)) + "}}},\"block\":\"transformed_parameters\"},{\"name\":\"tp_stzm\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string((4 -1)) + ",\"cols\":" + std::to_string((5 -1)) + "},\"block\":\"transformed_parameters\"},{\"name\":\"tp_astzm\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"matrix\",\"rows\":" + std::to_string((4 -1)) + ",\"cols\":" + std::to_string((5 -1)) + "}}},\"block\":\"transformed_parameters\"},{\"name\":\"gq_stzv\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string((10 -1)) + "},\"block\":\"generated_quantities\"},{\"name\":\"gq_astzv\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"vector\",\"length\":" + std::to_string((10 -1)) + "}}},\"block\":\"generated_quantities\"},{\"name\":\"gq_stzm\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string((4 -1)) + ",\"cols\":" + std::to_string((5 -1)) + "},\"block\":\"generated_quantities\"},{\"name\":\"gq_astzm\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"matrix\",\"rows\":" + std::to_string((4 -1)) + ",\"cols\":" + std::to_string((5 -1)) + "}}},\"block\":\"generated_quantities\"}]"); + return std::string("[{\"name\":\"p_stzv\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string((v -1)) + "},\"block\":\"parameters\"},{\"name\":\"p_astzv\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"vector\",\"length\":" + std::to_string((v -1)) + "}}},\"block\":\"parameters\"},{\"name\":\"p_stzm\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string((N -1)) + ",\"cols\":" + std::to_string((p_stzm_2dim__ -1)) + "},\"block\":\"parameters\"},{\"name\":\"p_astzm\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"matrix\",\"rows\":" + std::to_string((N -1)) + ",\"cols\":" + std::to_string((p_astzm_4dim__ -1)) + "}}},\"block\":\"parameters\"},{\"name\":\"tp_stzv\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string((10 -1)) + "},\"block\":\"transformed_parameters\"},{\"name\":\"tp_astzv\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"vector\",\"length\":" + std::to_string((10 -1)) + "}}},\"block\":\"transformed_parameters\"},{\"name\":\"tp_stzm\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string((4 -1)) + ",\"cols\":" + std::to_string((5 -1)) + "},\"block\":\"transformed_parameters\"},{\"name\":\"tp_astzm\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"matrix\",\"rows\":" + std::to_string((4 -1)) + ",\"cols\":" + std::to_string((5 -1)) + "}}},\"block\":\"transformed_parameters\"},{\"name\":\"gq_stzv\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string((10 -1)) + "},\"block\":\"generated_quantities\"},{\"name\":\"gq_astzv\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"vector\",\"length\":" + std::to_string((10 -1)) + "}}},\"block\":\"generated_quantities\"},{\"name\":\"gq_stzm\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string((4 -1)) + ",\"cols\":" + std::to_string((5 -1)) + "},\"block\":\"generated_quantities\"},{\"name\":\"gq_astzm\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(2) + ",\"element_type\":{\"name\":\"array\",\"length\":" + std::to_string(3) + ",\"element_type\":{\"name\":\"matrix\",\"rows\":" + std::to_string((4 -1)) + ",\"cols\":" + std::to_string((5 -1)) + "}}},\"block\":\"generated_quantities\"}]"); } // Begin method overload boilerplate template inline void @@ -54172,8 +54263,8 @@ class sum_to_zero_model final : public model_base_crtp { emit_transformed_parameters = true, const bool emit_generated_quantities = true, std::ostream* pstream = nullptr) const { - const size_t num_params__ = 10 + (2 * (3 * 10)) + (4 * 5) + (2 * (3 * (4 - * 5))); + const size_t num_params__ = v + (2 * (3 * v)) + (N * p_stzm_2dim__) + (2 + * (3 * (N * p_astzm_4dim__))); const size_t num_transformed = emit_transformed_parameters * (10 + (2 * (3 * 10)) + (4 * 5) + (2 * (3 * (4 * 5)))); const size_t num_gen_quantities = emit_generated_quantities * (10 + (2 * @@ -54192,8 +54283,8 @@ class sum_to_zero_model final : public model_base_crtp { emit_transformed_parameters = true, bool emit_generated_quantities = true, std::ostream* pstream = nullptr) const { - const size_t num_params__ = 10 + (2 * (3 * 10)) + (4 * 5) + (2 * (3 * (4 - * 5))); + const size_t num_params__ = v + (2 * (3 * v)) + (N * p_stzm_2dim__) + (2 + * (3 * (N * p_astzm_4dim__))); const size_t num_transformed = emit_transformed_parameters * (10 + (2 * (3 * 10)) + (4 * 5) + (2 * (3 * (4 * 5)))); const size_t num_gen_quantities = emit_generated_quantities * (10 + (2 * diff --git a/test/integration/good/code-gen/stochastic_matrices.stan b/test/integration/good/code-gen/stochastic_matrices.stan index b1f0e1dc16..27f7c66ddb 100644 --- a/test/integration/good/code-gen/stochastic_matrices.stan +++ b/test/integration/good/code-gen/stochastic_matrices.stan @@ -5,14 +5,16 @@ data { } transformed data { + int ROWS = 10; + int COLS = 10; column_stochastic_matrix[10, 10] td_csm = d_csm; row_stochastic_matrix[10, 10] td_rsm = d_rsm; array[2, 2] row_stochastic_matrix[10, 10] td_arsm; } parameters { - column_stochastic_matrix[10, 10] p_csm; - row_stochastic_matrix[10, 10] p_rsm; - array[2, 2] row_stochastic_matrix[10, 10] p_arsm; + column_stochastic_matrix[ROWS, COLS] p_csm; + row_stochastic_matrix[ROWS, COLS] p_rsm; + array[2, 2] row_stochastic_matrix[ROWS, COLS] p_arsm; } transformed parameters { diff --git a/test/integration/good/code-gen/sum_to_zero.stan b/test/integration/good/code-gen/sum_to_zero.stan index bc49a83000..f72458ec5d 100644 --- a/test/integration/good/code-gen/sum_to_zero.stan +++ b/test/integration/good/code-gen/sum_to_zero.stan @@ -6,16 +6,18 @@ data { } transformed data { + int v = 10; + int N = 4; sum_to_zero_vector[10] td_stzv = d_stzv; array[2, 3] sum_to_zero_vector[10] td_astzv; sum_to_zero_matrix[4,5] td_stzm = d_stzm; array[2, 3] sum_to_zero_matrix[4,5] td_astzm; } parameters { - sum_to_zero_vector[10] p_stzv; - array[2, 3] sum_to_zero_vector[10] p_astzv; - sum_to_zero_matrix[4,5] p_stzm; - array[2, 3] sum_to_zero_matrix[4,5] p_astzm; + sum_to_zero_vector[v] p_stzv; + array[2, 3] sum_to_zero_vector[v] p_astzv; + sum_to_zero_matrix[N,N+1] p_stzm; + array[2, 3] sum_to_zero_matrix[N,N+1] p_astzm; } transformed parameters { From 31e2bdefc4a4deb5b0e1a7f52726ea06da7e5adb Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Fri, 17 Oct 2025 12:23:19 -0400 Subject: [PATCH 2/2] Check for positive indices on stochastic matrices, sum to zero types --- src/frontend/Ast_to_Mir.ml | 17 ++++++++++++---- src/middle/Internal_fun.ml | 4 ++-- src/stan_math_backend/Lower_stmt.ml | 2 +- test/integration/good/code-gen/cpp.expected | 20 +++++++++---------- test/integration/good/code-gen/mir.expected | 6 +++--- .../good/code-gen/transformed_mir.expected | 6 +++--- 6 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/frontend/Ast_to_Mir.ml b/src/frontend/Ast_to_Mir.ml index c1b5e8d897..299e40364a 100644 --- a/src/frontend/Ast_to_Mir.ml +++ b/src/frontend/Ast_to_Mir.ml @@ -797,8 +797,8 @@ let rec trans_sizedtype_decl declc tr name st = | SVector (mem_pattern, s) -> let fn = match (declc.transform_action, tr) with - | Constrain, Transformation.Simplex -> - Internal_fun.FnValidateSizeSimplex + | Constrain, (Transformation.Simplex | SumToZero) -> + Internal_fun.FnValidateSizePositive | Constrain, UnitVector -> FnValidateSizeUnitVector | _ -> FnValidateSize in let l, s = grab_size fn n s in @@ -813,8 +813,17 @@ let rec trans_sizedtype_decl declc tr name st = let l, s = grab_size FnValidateSize n s in (l, SizedType.SComplexVector s) | SMatrix (mem_pattern, r, c) -> - let l1, r = grab_size FnValidateSize n r in - let l2, c = grab_size FnValidateSize (n + 1) c in + let fn1, fn2 = + match (declc.transform_action, tr) with + | Constrain, Transformation.SumToZero -> + ( Internal_fun.FnValidateSizePositive + , Internal_fun.FnValidateSizePositive ) + | Constrain, StochasticColumn -> + (FnValidateSizePositive, FnValidateSize) + | Constrain, StochasticRow -> (FnValidateSize, FnValidateSizePositive) + | _ -> (FnValidateSize, FnValidateSize) in + let l1, r = grab_size fn1 n r in + let l2, c = grab_size fn2 (n + 1) c in let cf_cov = match (declc.transform_action, tr) with | Constrain, CholeskyCov -> diff --git a/src/middle/Internal_fun.ml b/src/middle/Internal_fun.ml index 77683057e8..4c5439beca 100644 --- a/src/middle/Internal_fun.ml +++ b/src/middle/Internal_fun.ml @@ -18,7 +18,7 @@ type 'expr t = ; mem_pattern: Mem_pattern.t } | FnWriteParam of {unconstrain_opt: 'expr Transformation.t option; var: 'expr} | FnValidateSize - | FnValidateSizeSimplex + | FnValidateSizePositive | FnValidateSizeUnitVector | FnCheck of {trans: 'expr Transformation.t; var_name: string; var: 'expr} | FnPrint @@ -54,7 +54,7 @@ let pp (pp_expr : 'a Fmt.t) ppf internal = *) let can_side_effect = function | FnReadParam _ | FnReadData | FnReadDeserializer | FnWriteParam _ - |FnValidateSize | FnValidateSizeSimplex | FnValidateSizeUnitVector + |FnValidateSize | FnValidateSizePositive | FnValidateSizeUnitVector |FnReadWriteEventsOpenCL _ -> true | FnLength | FnMakeArray | FnMakeRowVec | FnNegInf | FnPrint | FnReject diff --git a/src/stan_math_backend/Lower_stmt.ml b/src/stan_math_backend/Lower_stmt.ml index 7331965c95..aa66cb3afc 100644 --- a/src/stan_math_backend/Lower_stmt.ml +++ b/src/stan_math_backend/Lower_stmt.ml @@ -24,7 +24,7 @@ let check_to_string = function let math_fn_translations = function | Internal_fun.FnValidateSize -> Some "stan::math::validate_non_negative_index" - | FnValidateSizeSimplex -> Some "stan::math::validate_positive_index" + | FnValidateSizePositive -> Some "stan::math::validate_positive_index" | FnValidateSizeUnitVector -> Some "stan::math::validate_unit_vector_index" | FnReadWriteEventsOpenCL x -> Some (x ^ ".wait_for_read_write_events") | _ -> None diff --git a/test/integration/good/code-gen/cpp.expected b/test/integration/good/code-gen/cpp.expected index 8ec77a4d47..23e0ea3aa9 100644 --- a/test/integration/good/code-gen/cpp.expected +++ b/test/integration/good/code-gen/cpp.expected @@ -52395,17 +52395,17 @@ class stochastic_matrices_model final : public model_base_crtp { current_statement__ = 22; stan::math::check_sum_to_zero(function__, "td_astzm", td_astzm); current_statement__ = 23; - stan::math::validate_non_negative_index("p_stzv", "v", v); + stan::math::validate_positive_index("p_stzv", "v", v); current_statement__ = 24; - stan::math::validate_non_negative_index("p_astzv", "v", v); + stan::math::validate_positive_index("p_astzv", "v", v); current_statement__ = 25; - stan::math::validate_non_negative_index("p_stzm", "N", N); + stan::math::validate_positive_index("p_stzm", "N", N); current_statement__ = 26; p_stzm_2dim__ = std::numeric_limits::min(); current_statement__ = 26; p_stzm_2dim__ = (N + 1); current_statement__ = 26; - stan::math::validate_non_negative_index("p_stzm", "N + 1", - p_stzm_2dim__); + stan::math::validate_positive_index("p_stzm", "N + 1", p_stzm_2dim__); current_statement__ = 27; - stan::math::validate_non_negative_index("p_astzm", "N", N); + stan::math::validate_positive_index("p_astzm", "N", N); current_statement__ = 28; p_astzm_4dim__ = std::numeric_limits::min(); current_statement__ = 28; p_astzm_4dim__ = (N + 1); current_statement__ = 28; - stan::math::validate_non_negative_index("p_astzm", "N + 1", - p_astzm_4dim__); + stan::math::validate_positive_index("p_astzm", "N + 1", p_astzm_4dim__); } catch (const std::exception& e) { stan::lang::rethrow_located(e, locations_array__[current_statement__]); } diff --git a/test/integration/good/code-gen/mir.expected b/test/integration/good/code-gen/mir.expected index cdb66cef76..13dd791fc5 100644 --- a/test/integration/good/code-gen/mir.expected +++ b/test/integration/good/code-gen/mir.expected @@ -6256,7 +6256,7 @@ ((pattern (Var N)) (meta ((type_ UInt) (loc ) (adlevel DataOnly))))))) (meta )) ((pattern - (NRFunApp (CompilerInternal FnValidateSizeSimplex) + (NRFunApp (CompilerInternal FnValidateSizePositive) (((pattern (Lit Str p_simplex)) (meta ((type_ UInt) (loc ) (adlevel DataOnly)))) ((pattern (Lit Str N)) (meta ((type_ UInt) (loc ) (adlevel DataOnly)))) @@ -6270,7 +6270,7 @@ ((pattern (Var N)) (meta ((type_ UInt) (loc ) (adlevel DataOnly))))))) (meta )) ((pattern - (NRFunApp (CompilerInternal FnValidateSizeSimplex) + (NRFunApp (CompilerInternal FnValidateSizePositive) (((pattern (Lit Str p_1d_simplex)) (meta ((type_ UInt) (loc ) (adlevel DataOnly)))) ((pattern (Lit Str N)) (meta ((type_ UInt) (loc ) (adlevel DataOnly)))) @@ -6298,7 +6298,7 @@ ((pattern (Var K)) (meta ((type_ UInt) (loc ) (adlevel DataOnly))))))) (meta )) ((pattern - (NRFunApp (CompilerInternal FnValidateSizeSimplex) + (NRFunApp (CompilerInternal FnValidateSizePositive) (((pattern (Lit Str p_3d_simplex)) (meta ((type_ UInt) (loc ) (adlevel DataOnly)))) ((pattern (Lit Str N)) (meta ((type_ UInt) (loc ) (adlevel DataOnly)))) diff --git a/test/integration/good/code-gen/transformed_mir.expected b/test/integration/good/code-gen/transformed_mir.expected index 5b30a43eb0..6ad4392551 100644 --- a/test/integration/good/code-gen/transformed_mir.expected +++ b/test/integration/good/code-gen/transformed_mir.expected @@ -9309,7 +9309,7 @@ ((pattern (Var N)) (meta ((type_ UInt) (loc ) (adlevel DataOnly))))))) (meta )) ((pattern - (NRFunApp (CompilerInternal FnValidateSizeSimplex) + (NRFunApp (CompilerInternal FnValidateSizePositive) (((pattern (Lit Str p_simplex)) (meta ((type_ UInt) (loc ) (adlevel DataOnly)))) ((pattern (Lit Str N)) (meta ((type_ UInt) (loc ) (adlevel DataOnly)))) @@ -9323,7 +9323,7 @@ ((pattern (Var N)) (meta ((type_ UInt) (loc ) (adlevel DataOnly))))))) (meta )) ((pattern - (NRFunApp (CompilerInternal FnValidateSizeSimplex) + (NRFunApp (CompilerInternal FnValidateSizePositive) (((pattern (Lit Str p_1d_simplex)) (meta ((type_ UInt) (loc ) (adlevel DataOnly)))) ((pattern (Lit Str N)) (meta ((type_ UInt) (loc ) (adlevel DataOnly)))) @@ -9351,7 +9351,7 @@ ((pattern (Var K)) (meta ((type_ UInt) (loc ) (adlevel DataOnly))))))) (meta )) ((pattern - (NRFunApp (CompilerInternal FnValidateSizeSimplex) + (NRFunApp (CompilerInternal FnValidateSizePositive) (((pattern (Lit Str p_3d_simplex)) (meta ((type_ UInt) (loc ) (adlevel DataOnly)))) ((pattern (Lit Str N)) (meta ((type_ UInt) (loc ) (adlevel DataOnly))))