From 141cff07613dd5440a746103eb195ad05424ad01 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Fri, 13 Dec 2019 17:27:50 +0100
Subject: [PATCH] New field M_.endo_trends
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

For each endogenous variable, gives the deflator and the growth factor (as well
as the “log” versions).

Ref. dynare#1648
---
 src/DynamicModel.cc | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index 5fe95c94..5e288be4 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -3697,6 +3697,33 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
   output << modstruct << "params = " << (julia ? "fill(NaN, " : "NaN(")
          << symbol_table.param_nbr() << (julia ? ")" : ", 1);") << endl;
 
+  // FIXME: implement this for Julia
+  if (!julia)
+    {
+      string empty_cell = "cell(" + to_string(symbol_table.endo_nbr()) + ", 1)";
+      output << modstruct << "endo_trends = struct('deflator', " << empty_cell
+             << ", 'log_deflator', " << empty_cell << ", 'growth_factor', " << empty_cell
+             << ", 'log_growth_factor', " << empty_cell << ");" << endl;
+      for (int i = 0; i < symbol_table.endo_nbr(); i++)
+        {
+          int symb_id = symbol_table.getID(SymbolType::endogenous, i);
+          if (auto it = nonstationary_symbols_map.find(symb_id); it != nonstationary_symbols_map.end())
+            {
+              auto [is_log, deflator] = it->second;
+              output << modstruct << "endo_trends(" << i << ")."
+                     << (is_log ? "log_deflator" : "deflator") << " = '";
+              deflator->writeJsonOutput(output, {}, {});
+              output << "';" << endl;
+
+              auto growth_factor = const_cast<DynamicModel *>(this)->AddDivide(deflator, deflator->decreaseLeadsLags(1))->removeTrendLeadLag(trend_symbols_map)->replaceTrendVar();
+              output << modstruct << "endo_trends(" << i << ")."
+                     << (is_log ? "log_growth_factor" : "growth_factor") << " = '";
+              growth_factor->writeJsonOutput(output, {}, {});
+              output << "';" << endl;
+            }
+        }
+    }
+
   if (compute_xrefs)
     writeXrefs(output);
 
-- 
GitLab