diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc index 6d035dc0..318559ec 100644 --- a/src/DynamicModel.cc +++ b/src/DynamicModel.cc @@ -4170,11 +4170,11 @@ DynamicModel::fillTrendComponentModelTable() const trend_component_model_table.setVals(eqnums, trend_eqnums, lhsr, lhs_expr_tr); } -void -DynamicModel::fillErrorComponentMatrix(map, expr_t>> &A0r, - map, expr_t>> &A0starr, - ExprNode::subst_table_t &diff_subst_table) const +pair, expr_t>>, map, expr_t>>> +DynamicModel::fillErrorComponentMatrix(const ExprNode::subst_table_t &diff_subst_table) const { + map, expr_t>> A0r, A0starr; + for (const auto & it : trend_component_model_table.getEqNums()) { int i = 0; @@ -4198,6 +4198,8 @@ DynamicModel::fillErrorComponentMatrix(map, exp A0r[it.first] = A0; A0starr[it.first] = A0star; } + + return { A0r, A0starr }; } void @@ -4268,12 +4270,12 @@ DynamicModel::fillTrendComponentModelTableFromOrigModel(StaticModel &static_mode } void -DynamicModel::fillTrendComponentmodelTableAREC(ExprNode::subst_table_t &diff_subst_table) const +DynamicModel::fillTrendComponentmodelTableAREC(const ExprNode::subst_table_t &diff_subst_table) const { - map, expr_t>> ARr, A0r, A0starr; - ARr = fillAutoregressiveMatrix(false); + auto ARr = fillAutoregressiveMatrix(false); trend_component_model_table.setAR(ARr); - fillErrorComponentMatrix(A0r, A0starr, diff_subst_table); + map, expr_t>> A0r, A0starr; + tie(A0r, A0starr) = fillErrorComponentMatrix(diff_subst_table); trend_component_model_table.setA0(A0r, A0starr); } @@ -4337,7 +4339,7 @@ DynamicModel::addEquationsForVar() vector DynamicModel::getUndiffLHSForPac(const string &aux_model_name, - ExprNode::subst_table_t &diff_subst_table) const + const ExprNode::subst_table_t &diff_subst_table) const { vector lhs_expr_t = trend_component_model_table.getLhsExprT(aux_model_name); vector lhs = trend_component_model_table.getLhs(aux_model_name); @@ -4404,9 +4406,11 @@ DynamicModel::getUndiffLHSForPac(const string &aux_model_name, return lhs; } -void -DynamicModel::walkPacParameters(const string &name, map, pair> &eqtag_and_lag) +map, pair> +DynamicModel::walkPacParameters(const string &name) { + map, pair> eqtag_and_lag; + int i = 0; for (auto & equation : equations) { @@ -4507,6 +4511,7 @@ DynamicModel::walkPacParameters(const string &name, map, pa eqtag_and_lag[{name, eqtag}] = {eq, 0}; } } + return eqtag_and_lag; } void @@ -6212,9 +6217,10 @@ DynamicModel::substituteAdl() equation = dynamic_cast(equation->substituteAdl()); } -void -DynamicModel::getEquationNumbersFromTags(vector &eqnumbers, set &eqtags) const +vector +DynamicModel::getEquationNumbersFromTags(const set &eqtags) const { + vector eqnumbers; for (auto & eqtag : eqtags) for (const auto & equation_tag : equation_tags) if (equation_tag.second.first == "name" @@ -6223,6 +6229,7 @@ DynamicModel::getEquationNumbersFromTags(vector &eqnumbers, set &eq eqnumbers.push_back(equation_tag.first); break; } + return eqnumbers; } void @@ -6238,26 +6245,28 @@ DynamicModel::findPacExpectationEquationNumbers(vector &eqnumbers) const } } -void -DynamicModel::substituteUnaryOps(StaticModel &static_model, diff_table_t &nodes, ExprNode::subst_table_t &subst_table) +pair +DynamicModel::substituteUnaryOps(StaticModel &static_model) { vector eqnumbers(equations.size()); iota(eqnumbers.begin(), eqnumbers.end(), 0); - substituteUnaryOps(static_model, nodes, subst_table, eqnumbers); + return substituteUnaryOps(static_model, eqnumbers); } -void -DynamicModel::substituteUnaryOps(StaticModel &static_model, diff_table_t &nodes, ExprNode::subst_table_t &subst_table, set &var_model_eqtags) +pair +DynamicModel::substituteUnaryOps(StaticModel &static_model, const set &var_model_eqtags) { - vector eqnumbers; - getEquationNumbersFromTags(eqnumbers, var_model_eqtags); + vector eqnumbers = getEquationNumbersFromTags(var_model_eqtags); findPacExpectationEquationNumbers(eqnumbers); - substituteUnaryOps(static_model, nodes, subst_table, eqnumbers); + return substituteUnaryOps(static_model, eqnumbers); } -void -DynamicModel::substituteUnaryOps(StaticModel &static_model, diff_table_t &nodes, ExprNode::subst_table_t &subst_table, vector &eqnumbers) +pair +DynamicModel::substituteUnaryOps(StaticModel &static_model, const vector &eqnumbers) { + diff_table_t nodes; + ExprNode::subst_table_t subst_table; + // Find matching unary ops that may be outside of diffs (i.e., those with different lags) set used_local_vars; for (int eqnumber : eqnumbers) @@ -6293,11 +6302,16 @@ DynamicModel::substituteUnaryOps(StaticModel &static_model, diff_table_t &nodes, if (subst_table.size() > 0) cout << "Substitution of Unary Ops: added " << neweqs.size() << " auxiliary variables and equations." << endl; + + return { nodes, subst_table }; } -void -DynamicModel::substituteDiff(StaticModel &static_model, diff_table_t &diff_table, ExprNode::subst_table_t &diff_subst_table, vector &pac_growth) +pair +DynamicModel::substituteDiff(StaticModel &static_model, vector &pac_growth) { + diff_table_t diff_table; + ExprNode::subst_table_t diff_subst_table; + set used_local_vars; for (const auto & equation : equations) equation->collectVariables(SymbolType::modelLocalVariable, used_local_vars); @@ -6359,6 +6373,8 @@ DynamicModel::substituteDiff(StaticModel &static_model, diff_table_t &diff_table if (diff_subst_table.size() > 0) cout << "Substitution of Diff operator: added " << neweqs.size() << " auxiliary variables and equations." << endl; + + return { diff_table, diff_subst_table }; } void diff --git a/src/DynamicModel.hh b/src/DynamicModel.hh index 482e8c2b..01dc26a0 100644 --- a/src/DynamicModel.hh +++ b/src/DynamicModel.hh @@ -251,7 +251,7 @@ private: //! Create a legacy *_dynamic.m file for Matlab/Octave not yet using the temporary terms array interface void writeDynamicMatlabCompatLayer(const string &basename) const; - void getEquationNumbersFromTags(vector &eqnumber, set &eqtags) const; + vector getEquationNumbersFromTags(const set &eqtags) const; void findPacExpectationEquationNumbers(vector &eqnumber) const; @@ -323,12 +323,13 @@ public: map, expr_t>> fillAutoregressiveMatrix(bool is_var) const; //! Fill Error Component Matrix for trend_component_model - void fillErrorComponentMatrix(map, expr_t>> &A0r, map, expr_t>> &A0starr, ExprNode::subst_table_t &diff_subst_table) const; + /*! Returns a pair (A0r, A0starr) */ + pair, expr_t>>, map, expr_t>>> fillErrorComponentMatrix(const ExprNode::subst_table_t &diff_subst_table) const; //! Fill the Trend Component Model Table void fillTrendComponentModelTable() const; void fillTrendComponentModelTableFromOrigModel(StaticModel &static_model) const; - void fillTrendComponentmodelTableAREC(ExprNode::subst_table_t &diff_subst_table) const; + void fillTrendComponentmodelTableAREC(const ExprNode::subst_table_t &diff_subst_table) const; //! Fill the Var Model Table void fillVarModelTable() const; @@ -344,7 +345,7 @@ public: void addEquationsForVar(); //! Get Pac equation parameter info - void walkPacParameters(const string &name, map, pair> &eqtag_and_lag); + map, pair> walkPacParameters(const string &name); //! Add var_model info to pac_expectation nodes void fillPacModelInfo(const string &pac_model_name, vector lhs, @@ -442,16 +443,16 @@ public: void substituteAdl(); //! Creates aux vars for all unary operators - void substituteUnaryOps(StaticModel &static_model, diff_table_t &nodes, ExprNode::subst_table_t &subst_table); + pair substituteUnaryOps(StaticModel &static_model); //! Creates aux vars for certain unary operators: originally implemented for support of VARs - void substituteUnaryOps(StaticModel &static_model, diff_table_t &nodes, ExprNode::subst_table_t &subst_table, set &eq_tags); + pair substituteUnaryOps(StaticModel &static_model, const set &eq_tags); //! Creates aux vars for certain unary operators: originally implemented for support of VARs - void substituteUnaryOps(StaticModel &static_model, diff_table_t &nodes, ExprNode::subst_table_t &subst_table, vector &eqnumbers); + pair substituteUnaryOps(StaticModel &static_model, const vector &eqnumbers); //! Substitutes diff operator - void substituteDiff(StaticModel &static_model, diff_table_t &diff_table, ExprNode::subst_table_t &diff_subst_table, vector &pac_growth); + pair substituteDiff(StaticModel &static_model, vector &pac_growth); //! Substitute VarExpectation operators void substituteVarExpectation(const map &subst_table); @@ -498,7 +499,7 @@ public: //! Table to undiff LHS variables for pac vector z vector getUndiffLHSForPac(const string &aux_model_name, - ExprNode::subst_table_t &diff_subst_table) const; + const ExprNode::subst_table_t &diff_subst_table) const; //! Transforms the model by replacing trend variables with a 1 void removeTrendVariableFromEquations(); diff --git a/src/ModFile.cc b/src/ModFile.cc index ba62367c..cdde909b 100644 --- a/src/ModFile.cc +++ b/src/ModFile.cc @@ -409,15 +409,15 @@ ModFile::transformPass(bool nostrict, bool stochastic, bool compute_xrefs, const diff_table_t unary_ops_nodes; ExprNode::subst_table_t unary_ops_subst_table; if (transform_unary_ops) - dynamic_model.substituteUnaryOps(diff_static_model, unary_ops_nodes, unary_ops_subst_table); + tie(unary_ops_nodes, unary_ops_subst_table) = dynamic_model.substituteUnaryOps(diff_static_model); else // substitute only those unary ops that appear in auxiliary model equations - dynamic_model.substituteUnaryOps(diff_static_model, unary_ops_nodes, unary_ops_subst_table, eqtags); + tie(unary_ops_nodes, unary_ops_subst_table) = dynamic_model.substituteUnaryOps(diff_static_model, eqtags); // Create auxiliary variable and equations for Diff operators diff_table_t diff_table; ExprNode::subst_table_t diff_subst_table; - dynamic_model.substituteDiff(diff_static_model, diff_table, diff_subst_table, pac_growth); + tie(diff_table, diff_subst_table) = dynamic_model.substituteDiff(diff_static_model, pac_growth); // Fill Trend Component Model Table dynamic_model.fillTrendComponentModelTable(); @@ -463,8 +463,7 @@ ModFile::transformPass(bool nostrict, bool stochastic, bool compute_xrefs, const cerr << "Error: aux_model_name not recognized as VAR model or Trend Component model" << endl; exit(EXIT_FAILURE); } - map, pair> eqtag_and_lag; - dynamic_model.walkPacParameters(pms->name, eqtag_and_lag); + auto eqtag_and_lag = dynamic_model.walkPacParameters(pms->name); original_model.getPacMaxLag(pms->name, eqtag_and_lag); if (pms->aux_model_name == "") dynamic_model.addPacModelConsistentExpectationEquation(pms->name, symbol_table.getID(pms->discount),