From 797cc91b388f47e299734f9096c8728735ab8f3c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Wed, 6 May 2020 18:09:44 +0200
Subject: [PATCH] =?UTF-8?q?Block=20decomposition:=20no=20longer=20compute?=
 =?UTF-8?q?=20static=20Jacobian,=20it=E2=80=99s=20not=20used?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Also remove a message about elements below the cutoff that was no longer
correct (elements below the cutoff have no impact on the incidence matrix
outside of normalization).
---
 src/DynamicModel.cc |  8 ++++----
 src/ModelTree.cc    | 39 +++++++--------------------------------
 src/ModelTree.hh    |  6 +++---
 src/StaticModel.cc  |  4 ++--
 4 files changed, 16 insertions(+), 41 deletions(-)

diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index 53cb39ad..fd8a505c 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -4508,10 +4508,10 @@ DynamicModel::computingPass(bool jacobianExo, int derivsOrder, int paramsDerivsO
       auto first_order_endo_derivatives = collectFirstOrderDerivativesEndogenous();
       equationLinear(first_order_endo_derivatives);
 
-      auto [contemporaneous_jacobian, static_jacobian] = evaluateAndReduceJacobian(eval_context, cutoff, false);
+      auto contemporaneous_jacobian = evaluateAndReduceJacobian(eval_context);
 
       if (!computeNaturalNormalization())
-        computeNonSingularNormalization(contemporaneous_jacobian, cutoff, static_jacobian);
+        computeNonSingularNormalization(contemporaneous_jacobian);
 
       select_non_linear_equations_and_variables();
 
@@ -4531,9 +4531,9 @@ DynamicModel::computingPass(bool jacobianExo, int derivsOrder, int paramsDerivsO
     }
   else if (block)
     {
-      auto [contemporaneous_jacobian, static_jacobian] = evaluateAndReduceJacobian(eval_context, cutoff, false);
+      auto contemporaneous_jacobian = evaluateAndReduceJacobian(eval_context);
 
-      computeNonSingularNormalization(contemporaneous_jacobian, cutoff, static_jacobian);
+      computeNonSingularNormalization(contemporaneous_jacobian);
 
       auto [prologue, epilogue] = computePrologueAndEpilogue();
 
diff --git a/src/ModelTree.cc b/src/ModelTree.cc
index dbad7541..5d102b2b 100644
--- a/src/ModelTree.cc
+++ b/src/ModelTree.cc
@@ -255,7 +255,7 @@ ModelTree::computeNormalization(const jacob_map_t &contemporaneous_jacobian, boo
 }
 
 void
-ModelTree::computeNonSingularNormalization(const jacob_map_t &contemporaneous_jacobian, double cutoff, const jacob_map_t &static_jacobian)
+ModelTree::computeNonSingularNormalization(const jacob_map_t &contemporaneous_jacobian)
 {
   cout << "Normalizing the model..." << endl;
 
@@ -317,12 +317,10 @@ ModelTree::computeNonSingularNormalization(const jacob_map_t &contemporaneous_ja
     }
 }
 
-pair<ModelTree::jacob_map_t, ModelTree::jacob_map_t>
-ModelTree::evaluateAndReduceJacobian(const eval_context_t &eval_context, double cutoff, bool verbose) const
+ModelTree::jacob_map_t
+ModelTree::evaluateAndReduceJacobian(const eval_context_t &eval_context) const
 {
-  jacob_map_t contemporaneous_jacobian, static_jacobian;
-  int nb_elements_contemporaneous_jacobian = 0;
-  set<vector<int>> jacobian_elements_to_delete;
+  jacob_map_t contemporaneous_jacobian;
   for (const auto &[indices, d1] : derivatives[1])
     {
       int deriv_id = indices[1];
@@ -348,35 +346,12 @@ ModelTree::evaluateAndReduceJacobian(const eval_context_t &eval_context, double
               cerr << endl;
               exit(EXIT_FAILURE);
             }
-          if (fabs(val) < cutoff)
-            {
-              if (verbose)
-                cout << "The coefficient related to variable " << var << " with lag " << lag << " in equation " << eq << " is equal to " << val << " and is set to 0 in the incidence matrix (size=" << symbol_table.endo_nbr() << ")." << endl;
-              jacobian_elements_to_delete.insert({ eq, deriv_id });
-            }
-          else
-            {
-              if (lag == 0)
-                {
-                  nb_elements_contemporaneous_jacobian++;
-                  contemporaneous_jacobian[{ eq, var }] = val;
-                }
-
-              if (static_jacobian.find({ eq, var }) != static_jacobian.end())
-                static_jacobian[{ eq, var }] += val;
-              else
-                static_jacobian[{ eq, var }] = val;
-            }
+          if (lag == 0)
+            contemporaneous_jacobian[{ eq, var }] = val;
         }
     }
 
-  if (jacobian_elements_to_delete.size() > 0)
-    {
-      cout << jacobian_elements_to_delete.size() << " elements among " << derivatives[1].size() << " in the incidence matrices are below the cutoff (" << cutoff << ") and are discarded." << endl
-           << "The contemporaneous incidence matrix has " << nb_elements_contemporaneous_jacobian << " elements." << endl;
-    }
-
-  return { contemporaneous_jacobian, static_jacobian };
+  return contemporaneous_jacobian;
 }
 
 void
diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index d2131d49..47d5c794 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -279,13 +279,13 @@ protected:
     If no matching is found with a zero cutoff, an error message is printed.
     The resulting normalization is stored in endo2eq.
   */
-  void computeNonSingularNormalization(const jacob_map_t &contemporaneous_jacobian, double cutoff, const jacob_map_t &static_jacobian);
+  void computeNonSingularNormalization(const jacob_map_t &contemporaneous_jacobian);
   //! Try to find a natural normalization if all equations are matched to an endogenous variable on the LHS
   bool computeNaturalNormalization();
   //! Evaluate the jacobian (w.r.t. endogenous) and suppress all the elements below the cutoff
-  /*! Returns a pair (contemporaneous_jacobian, static_jacobian).
+  /*! Returns the contemporaneous_jacobian.
       Elements below the cutoff are discarded. External functions are evaluated to 1. */
-  pair<jacob_map_t, jacob_map_t> evaluateAndReduceJacobian(const eval_context_t &eval_context, double cutoff, bool verbose) const;
+  jacob_map_t evaluateAndReduceJacobian(const eval_context_t &eval_context) const;
   //! Select and reorder the non linear equations of the model
   void select_non_linear_equations_and_variables();
   /* Search the equations and variables belonging to the prologue and the
diff --git a/src/StaticModel.cc b/src/StaticModel.cc
index d7a07050..e22a137d 100644
--- a/src/StaticModel.cc
+++ b/src/StaticModel.cc
@@ -1064,9 +1064,9 @@ StaticModel::computingPass(int derivsOrder, int paramsDerivsOrder, const eval_co
 
   if (block)
     {
-      auto [contemporaneous_jacobian, static_jacobian] = evaluateAndReduceJacobian(eval_context, cutoff, false);
+      auto contemporaneous_jacobian = evaluateAndReduceJacobian(eval_context);
 
-      computeNonSingularNormalization(contemporaneous_jacobian, cutoff, static_jacobian);
+      computeNonSingularNormalization(contemporaneous_jacobian);
 
       auto [prologue, epilogue] = computePrologueAndEpilogue();
 
-- 
GitLab