diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc index 1bae113b168b392b953c7eaf05cdfc93f1df3183..b32617c550ec5b6033397f9cbeeb0363b2a38560 100644 --- a/src/DynamicModel.cc +++ b/src/DynamicModel.cc @@ -6344,3 +6344,24 @@ DynamicModel::checkNoRemainingPacExpectation() const exit(EXIT_FAILURE); } } + +void +DynamicModel::simplifyEquations() +{ + size_t last_subst_table_size = 0; + map<VariableNode *, NumConstNode *> subst_table; + // Equations with “mcp” tag are excluded, see dynare#1697 + findConstantEquationsWithoutMcpTag(subst_table); + while (subst_table.size() != last_subst_table_size) + { + last_subst_table_size = subst_table.size(); + for (auto &[id, definition] : local_variables_table) + definition = definition->replaceVarsInEquation(subst_table); + for (auto &equation : equations) + equation = dynamic_cast<BinaryOpNode *>(equation->replaceVarsInEquation(subst_table)); + for (auto &equation : static_only_equations) + equation = dynamic_cast<BinaryOpNode *>(equation->replaceVarsInEquation(subst_table)); + subst_table.clear(); + findConstantEquationsWithoutMcpTag(subst_table); + } +} diff --git a/src/DynamicModel.hh b/src/DynamicModel.hh index 1ce54fb8a1310eaeabc629b5aea5dd97cdf3400b..09d4d90086d06f2aa3955715b6b274f7f6a9a64d 100644 --- a/src/DynamicModel.hh +++ b/src/DynamicModel.hh @@ -589,5 +589,9 @@ public: bool ParamUsedWithLeadLag() const; bool isChecksumMatching(const string &basename, bool block) const; + + //! Simplify model equations: if a variable is equal to a constant, replace that variable elsewhere in the model + /*! Equations with MCP tags are excluded, see dynare#1697 */ + void simplifyEquations(); }; #endif diff --git a/src/ExprNode.cc b/src/ExprNode.cc index ddd426992cc99a28cede9157b3086a5068da1c83..c2a35b0439dd39f2fecd3111e968ba742d020c5e 100644 --- a/src/ExprNode.cc +++ b/src/ExprNode.cc @@ -1938,7 +1938,7 @@ expr_t VariableNode::replaceVarsInEquation(map<VariableNode *, NumConstNode *> &table) const { /* Do not recurse into model-local variables definitions, since MLVs are - already handled by ModelTree::simplifyEquations(). + already handled by DynamicModel::simplifyEquations(). This is also necessary because of #65. */ for (auto &it : table) if (it.first->symb_id == symb_id) diff --git a/src/ModelTree.cc b/src/ModelTree.cc index c6216131ea7aea3e807ea84cda7d672eba80b6e0..da71df01332dd14b40c6ce45dcc588f658f9fcda 100644 --- a/src/ModelTree.cc +++ b/src/ModelTree.cc @@ -1629,25 +1629,6 @@ ModelTree::includeExcludeEquations(set<pair<string, string>> &eqs, bool exclude_ return excluded_vars; } -void -ModelTree::simplifyEquations() -{ - size_t last_subst_table_size = 0; - map<VariableNode *, NumConstNode *> subst_table; - // Equations with “mcp” tag are excluded, see dynare#1697 - findConstantEquationsWithoutMcpTag(subst_table); - while (subst_table.size() != last_subst_table_size) - { - last_subst_table_size = subst_table.size(); - for (auto &[id, definition] : local_variables_table) - definition = definition->replaceVarsInEquation(subst_table); - for (auto &equation : equations) - equation = dynamic_cast<BinaryOpNode *>(equation->replaceVarsInEquation(subst_table)); - subst_table.clear(); - findConstantEquationsWithoutMcpTag(subst_table); - } -} - void ModelTree::findConstantEquationsWithoutMcpTag(map<VariableNode *, NumConstNode *> &subst_table) const { diff --git a/src/ModelTree.hh b/src/ModelTree.hh index a5e3d1bde66a427d7eae477ba134855c23d39dab..4596df6600cdd7e99d222a3c654b581b5353db95 100644 --- a/src/ModelTree.hh +++ b/src/ModelTree.hh @@ -440,9 +440,6 @@ public: //! Is a given variable non-stationary? bool isNonstationary(int symb_id) const; void set_cutoff_to_zero(); - //! Simplify model equations: if a variable is equal to a constant, replace that variable elsewhere in the model - /*! Equations with MCP tags are excluded, see dynare#1697 */ - void simplifyEquations(); /*! Reorder auxiliary variables so that they appear in recursive order in set_auxiliary_variables.m and dynamic_set_auxiliary_series.m */ void reorderAuxiliaryEquations();