From 7b8fc8edb79376678f2bb2722ece237ce244b329 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Mon, 6 Dec 2021 12:53:35 +0100
Subject: [PATCH] Symbolic detrending engine: correctly handle equations marked
 [static]

Incidentally, this also fixes a detrending bug in the Occbin engine (since the
latter internally generates a [static] equation).

Ref. dynare#1827

By the way, perform a small code simplification.
---
 src/DynamicModel.cc | 38 ++++++++++++++++++++++++++------------
 1 file changed, 26 insertions(+), 12 deletions(-)

diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index 192e436b..345c6e1d 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -5546,18 +5546,28 @@ DynamicModel::detrendEquations()
   // We go backwards in the list of trend_vars, to deal correctly with I(2) processes
   for (auto it = nonstationary_symbols_map.crbegin();
        it != nonstationary_symbols_map.crend(); ++it)
-    for (auto &equation : equations)
-      {
-        auto substeq = dynamic_cast<BinaryOpNode *>(equation->detrend(it->first, it->second.first, it->second.second));
-        assert(substeq);
-        equation = dynamic_cast<BinaryOpNode *>(substeq);
-      }
+    {
+      for (auto &equation : equations)
+        {
+          equation = dynamic_cast<BinaryOpNode *>(equation->detrend(it->first, it->second.first, it->second.second));
+          assert(equation);
+        }
+      for (auto &equation : static_only_equations)
+        {
+          equation = dynamic_cast<BinaryOpNode *>(equation->detrend(it->first, it->second.first, it->second.second));
+          assert(equation);
+        }
+    }
 
   for (auto &equation : equations)
     {
-      auto substeq = dynamic_cast<BinaryOpNode *>(equation->removeTrendLeadLag(trend_symbols_map));
-      assert(substeq);
-      equation = dynamic_cast<BinaryOpNode *>(substeq);
+      equation = dynamic_cast<BinaryOpNode *>(equation->removeTrendLeadLag(trend_symbols_map));
+      assert(equation);
+    }
+  for (auto &equation : static_only_equations)
+    {
+      equation = dynamic_cast<BinaryOpNode *>(equation->removeTrendLeadLag(trend_symbols_map));
+      assert(equation);
     }
 }
 
@@ -5566,9 +5576,13 @@ DynamicModel::removeTrendVariableFromEquations()
 {
   for (auto &equation : equations)
     {
-      auto substeq = dynamic_cast<BinaryOpNode *>(equation->replaceTrendVar());
-      assert(substeq);
-      equation = dynamic_cast<BinaryOpNode *>(substeq);
+      equation = dynamic_cast<BinaryOpNode *>(equation->replaceTrendVar());
+      assert(equation);
+    }
+  for (auto &equation : static_only_equations)
+    {
+      equation = dynamic_cast<BinaryOpNode *>(equation->replaceTrendVar());
+      assert(equation);
     }
 }
 
-- 
GitLab