From 4d30f17e0b61488188f883c574839a9a4bd5fcdf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Mon, 30 Mar 2020 14:51:53 +0200
Subject: [PATCH] Block decomposition: move
 collectFirstOrderDerivativesEndogenous in ModelTree

By the way, use camel case for the function name.
---
 src/DynamicModel.cc | 19 ++-----------------
 src/DynamicModel.hh |  2 --
 src/ModelTree.cc    | 15 +++++++++++++++
 src/ModelTree.hh    |  4 ++++
 src/StaticModel.cc  | 19 +------------------
 src/StaticModel.hh  |  2 --
 6 files changed, 22 insertions(+), 39 deletions(-)

diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index b04be558..2092b5db 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -3813,21 +3813,6 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
     }
 }
 
-map<tuple<int, int, int>, expr_t>
-DynamicModel::collect_first_order_derivatives_endogenous()
-{
-  map<tuple<int, int, int>, expr_t> endo_derivatives;
-  for (auto & [indices, d1] : derivatives[1])
-    if (getTypeByDerivID(indices[1]) == SymbolType::endogenous)
-      {
-        int eq = indices[0];
-        int var = symbol_table.getTypeSpecificID(getSymbIDByDerivID(indices[1]));
-        int lag = getLagByDerivID(indices[1]);
-        endo_derivatives[{ eq, var, lag }] = d1;
-      }
-  return endo_derivatives;
-}
-
 void
 DynamicModel::runTrendTest(const eval_context_t &eval_context)
 {
@@ -4816,7 +4801,7 @@ DynamicModel::computingPass(bool jacobianExo, int derivsOrder, int paramsDerivsO
 
   if (linear_decomposition)
     {
-      first_order_endo_derivatives = collect_first_order_derivatives_endogenous();
+      first_order_endo_derivatives = collectFirstOrderDerivativesEndogenous();
       is_equation_linear = equationLinear(first_order_endo_derivatives);
 
       tie(contemporaneous_jacobian, static_jacobian) = evaluateAndReduceJacobian(eval_context, cutoff, false);
@@ -4856,7 +4841,7 @@ DynamicModel::computingPass(bool jacobianExo, int derivsOrder, int paramsDerivsO
 
       computePrologueAndEpilogue(static_jacobian);
 
-      first_order_endo_derivatives = collect_first_order_derivatives_endogenous();
+      first_order_endo_derivatives = collectFirstOrderDerivativesEndogenous();
 
       equationTypeDetermination(first_order_endo_derivatives, mfs);
 
diff --git a/src/DynamicModel.hh b/src/DynamicModel.hh
index b834310d..6e8efb6b 100644
--- a/src/DynamicModel.hh
+++ b/src/DynamicModel.hh
@@ -162,8 +162,6 @@ private:
   void computeDynJacobianCols(bool jacobianExo);
   //! Computes derivatives of the Jacobian w.r. to trend vars and tests that they are equal to zero
   void testTrendDerivativesEqualToZero(const eval_context_t &eval_context);
-  //! Collect only the first derivatives
-  map<tuple<int, int, int>, expr_t> collect_first_order_derivatives_endogenous();
 
   //! Allocates the derivation IDs for all dynamic variables of the model
   /*! Also computes max_{endo,exo}_{lead_lag}, and initializes dynJacobianColsNbr to the number of dynamic endos */
diff --git a/src/ModelTree.cc b/src/ModelTree.cc
index e39f190a..266e965a 100644
--- a/src/ModelTree.cc
+++ b/src/ModelTree.cc
@@ -2413,3 +2413,18 @@ ModelTree::reorderAuxiliaryEquations()
   for (int i = 0; i < n; i++)
     aux_equations[i] = aux_equations_old[index[ordered[i]]];
 }
+
+map<tuple<int, int, int>, expr_t>
+ModelTree::collectFirstOrderDerivativesEndogenous()
+{
+  map<tuple<int, int, int>, expr_t> endo_derivatives;
+  for (auto &[indices, d1] : derivatives[1])
+    if (getTypeByDerivID(indices[1]) == SymbolType::endogenous)
+      {
+        int eq = indices[0];
+        int var = symbol_table.getTypeSpecificID(getSymbIDByDerivID(indices[1]));
+        int lag = getLagByDerivID(indices[1]);
+        endo_derivatives[{ eq, var, lag }] = d1;
+      }
+  return endo_derivatives;
+}
diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index 8ae42108..734069a2 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -365,6 +365,10 @@ protected:
   virtual int getBlockInitialOtherEndogenousID(int block_number, int variable_number) const = 0;
   //! Initialize equation_reordered & variable_reordered
   void initializeVariablesAndEquations();
+  //! Returns the 1st derivatives w.r.t. endogenous in a different format
+  /*! Returns a map (equation, type-specific ID, lag) → derivative.
+      Assumes that derivatives have already been computed. */
+  map<tuple<int, int, int>, expr_t> collectFirstOrderDerivativesEndogenous();
 
 private:
   //! Internal helper for the copy constructor and assignment operator
diff --git a/src/StaticModel.cc b/src/StaticModel.cc
index c8f47773..45ee4c5d 100644
--- a/src/StaticModel.cc
+++ b/src/StaticModel.cc
@@ -1087,23 +1087,6 @@ StaticModel::Write_Inf_To_Bin_File_Block(const string &basename, int num,
   SaveCode.close();
 }
 
-map<tuple<int, int, int>, expr_t>
-StaticModel::collect_first_order_derivatives_endogenous()
-{
-  map<tuple<int, int, int>, expr_t> endo_derivatives;
-  for (auto & [indices, d1] : derivatives[1])
-    {
-      if (getTypeByDerivID(indices[1]) == SymbolType::endogenous)
-        {
-          int eq = indices[0];
-          int var = symbol_table.getTypeSpecificID(getSymbIDByDerivID(indices[1]));
-          int lag = 0;
-          endo_derivatives[{ eq, var, lag }] = d1;
-        }
-    }
-  return endo_derivatives;
-}
-
 void
 StaticModel::computingPass(int derivsOrder, int paramsDerivsOrder, const eval_context_t &eval_context, bool no_tmp_terms, bool block, bool bytecode)
 {
@@ -1153,7 +1136,7 @@ StaticModel::computingPass(int derivsOrder, int paramsDerivsOrder, const eval_co
 
       computePrologueAndEpilogue(static_jacobian);
 
-      auto first_order_endo_derivatives = collect_first_order_derivatives_endogenous();
+      auto first_order_endo_derivatives = collectFirstOrderDerivativesEndogenous();
 
       equationTypeDetermination(first_order_endo_derivatives, mfs);
 
diff --git a/src/StaticModel.hh b/src/StaticModel.hh
index d44febc4..6fd760e9 100644
--- a/src/StaticModel.hh
+++ b/src/StaticModel.hh
@@ -91,8 +91,6 @@ private:
   map<tuple<int, int, int, int, int>, int> get_Derivatives(int block);
   //! Computes chain rule derivatives of the Jacobian w.r. to endogenous variables
   void computeChainRuleJacobian();
-  //! Collect only the first derivatives
-  map<tuple<int, int, int>, expr_t> collect_first_order_derivatives_endogenous();
 
   //! Collecte the derivatives w.r. to endogenous of the block, to endogenous of previouys blocks and to exogenous
   void collect_block_first_order_derivatives();
-- 
GitLab