From 8c301448d62bddfe0e2d6042e5241500577e1789 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Thu, 5 Jul 2018 16:12:18 +0200
Subject: [PATCH] =?UTF-8?q?remove=20storage=20of=20diff=20and=20unary=20au?=
 =?UTF-8?q?x=20vars=20in=20diff=5Faux=5Fequations=20as=20it=E2=80=99s=20no?=
 =?UTF-8?q?=20longer=20necessary?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/DynamicModel.cc | 43 ++-----------------------------------------
 src/DynamicModel.hh |  3 ---
 src/ModFile.cc      |  2 --
 src/ModelTree.hh    |  6 ------
 4 files changed, 2 insertions(+), 52 deletions(-)

diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index bb25c2d2..b88d7d25 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -5264,39 +5264,6 @@ DynamicModel::substituteLeadLagInternal(aux_var_t type, bool deterministic_model
       aux_equation = substeq;
     }
 
- // Substitute in diff_aux_equations
-  // Without this loop, the auxiliary equations in equations
-  // will diverge from those in diff_aux_equations
-  for (auto & diff_aux_equation : diff_aux_equations)
-    {
-      expr_t subst;
-      switch (type)
-        {
-        case avEndoLead:
-          subst = diff_aux_equation->substituteEndoLeadGreaterThanTwo(subst_table,
-                                                                     neweqs, deterministic_model);
-          break;
-        case avEndoLag:
-          subst = diff_aux_equation->substituteEndoLagGreaterThanTwo(subst_table, neweqs);
-          break;
-        case avExoLead:
-          subst = diff_aux_equation->substituteExoLead(subst_table, neweqs, deterministic_model);
-          break;
-        case avExoLag:
-          subst = diff_aux_equation->substituteExoLag(subst_table, neweqs);
-          break;
-        case avDiffForward:
-          subst = diff_aux_equation->differentiateForwardVars(subset, subst_table, neweqs);
-          break;
-        default:
-          cerr << "DynamicModel::substituteLeadLagInternal: impossible case" << endl;
-          exit(EXIT_FAILURE);
-        }
-      auto *substeq = dynamic_cast<BinaryOpNode *>(subst);
-      assert(substeq != nullptr);
-      diff_aux_equation = substeq;
-    }
-
   // Add new equations
   for (auto & neweq : neweqs)
     addEquation(neweq, -1);
@@ -5425,7 +5392,7 @@ DynamicModel::substituteUnaryOps(StaticModel &static_model, vector<int> &eqnumbe
   for (auto & neweq : neweqs)
     addEquation(neweq, -1);
 
-  copy(neweqs.begin(), neweqs.end(), back_inserter(diff_aux_equations));
+  copy(neweqs.begin(), neweqs.end(), back_inserter(aux_equations));
 
   if (subst_table.size() > 0)
     cout << "Substitution of Unary Ops: added " << neweqs.size() << " auxiliary variables and equations." << endl;
@@ -5465,18 +5432,12 @@ DynamicModel::substituteDiff(StaticModel &static_model, ExprNode::subst_table_t
   for (auto & neweq : neweqs)
     addEquation(neweq, -1);
 
-  copy(neweqs.begin(), neweqs.end(), back_inserter(diff_aux_equations));
+  copy(neweqs.begin(), neweqs.end(), back_inserter(aux_equations));
 
   if (diff_subst_table.size() > 0)
     cout << "Substitution of Diff operator: added " << neweqs.size() << " auxiliary variables and equations." << endl;
 }
 
-void
-DynamicModel::combineDiffAuxEquations()
-{
-  copy(diff_aux_equations.begin(), diff_aux_equations.end(), back_inserter(aux_equations));
-}
-
 void
 DynamicModel::substituteExpectation(bool partial_information_model)
 {
diff --git a/src/DynamicModel.hh b/src/DynamicModel.hh
index 79c726ce..ee96cf9d 100644
--- a/src/DynamicModel.hh
+++ b/src/DynamicModel.hh
@@ -438,9 +438,6 @@ public:
   void getUndiffLHSForPac(vector<int> &lhs, vector<expr_t> &lhs_expr_t, vector<bool> &diff, vector<int> &orig_diff_var,
                           vector<int> &eqnumber, map<string, int> &undiff, ExprNode::subst_table_t &diff_subst_table);
 
-  //! Adds contents of diff_aux_equations to the back of aux_equations
-  void combineDiffAuxEquations();
-
   //! Fill var_expectation_functions_to_write
   void fillVarExpectationFunctionsToWrite();
 
diff --git a/src/ModFile.cc b/src/ModFile.cc
index ff08c4dc..18e657b3 100644
--- a/src/ModFile.cc
+++ b/src/ModFile.cc
@@ -512,8 +512,6 @@ ModFile::transformPass(bool nostrict, bool stochastic, bool compute_xrefs, const
       dynamic_model.substituteEndoLagGreaterThanTwo(true);
     }
 
-  dynamic_model.combineDiffAuxEquations();
-
   for (auto & statement : statements)
     {
       auto *vms = dynamic_cast<VarModelStatement *>(statement);
diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index 46cad5f2..682fe0e1 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -60,12 +60,6 @@ protected:
 
   //! Only stores generated auxiliary equations, in an order meaningful for evaluation
   deque<BinaryOpNode *> aux_equations;
-  //! Temporarily stores aux equations for diff operator
-  //! This is a hack to make diff aux vars work: they must be substituted before we consider VARs
-  //! But leads lags can't be substituted until after we consider VARs
-  //! The diff_aux_vars may depend on aux_vars created for leads lags, as in
-  //! diff(x(-1)) => x(-1) - x(-2) => x(-1) - aux_var(-1); aux_var = x(-1);
-  deque<BinaryOpNode *> diff_aux_equations;
 
   //! Stores equation tags
   vector<pair<int, pair<string, string>>> equation_tags;
-- 
GitLab