From dcabcb6b0121a6592e1be1e9f6547f5642e0c0d4 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Mon, 16 Feb 2015 08:31:30 +0100
Subject: [PATCH] preprocessor: new command write_latex_original_model. closes
 #657

---
 doc/dynare.texi                    | 31 ++++++++++++++++++++++++++----
 preprocessor/ComputingTasks.cc     | 11 +++++++++++
 preprocessor/ComputingTasks.hh     | 11 ++++++++++-
 preprocessor/DynamicModel.cc       |  6 ++++++
 preprocessor/DynamicModel.hh       |  5 ++++-
 preprocessor/DynareBison.yy        |  7 ++++++-
 preprocessor/DynareFlex.ll         |  1 +
 preprocessor/ExprNode.cc           | 18 ++++++++++++++---
 preprocessor/ModFile.cc            |  6 +++++-
 preprocessor/ModFile.hh            |  4 +++-
 preprocessor/ModelTree.cc          |  3 ++-
 preprocessor/ParsingDriver.cc      |  8 +++++++-
 preprocessor/ParsingDriver.hh      |  4 +++-
 tests/expectations/expectation.mod |  2 ++
 14 files changed, 102 insertions(+), 15 deletions(-)

diff --git a/doc/dynare.texi b/doc/dynare.texi
index 867fbf475c..e635d3b0ed 100644
--- a/doc/dynare.texi
+++ b/doc/dynare.texi
@@ -1875,10 +1875,33 @@ end;
 
 @end deffn
 
-Dynare has the ability to output the list of model equations to a
-@LaTeX{} file, using the @code{write_latex_dynamic_model} command. The
-static model can also be written with the
-@code{write_latex_static_model} command.
+Dynare has the ability to output the original list of model equations
+to a @LaTeX{} file, using the @code{write_latex_original_model}
+command, the list of transformed model equations using the
+@code{write_latex_dynamic_model} command, and the list of static model
+equations using the @code{write_latex_static_model} command.
+
+@anchor{write_latex_original_model}
+
+@deffn Command write_latex_original_model ;
+
+@descriptionhead
+
+This command creates a @LaTeX{} file containing the model, as defined
+in the model block.
+
+If your @file{.mod} file is @file{@var{FILENAME}.mod}, then Dynare
+will create a file called @file{@var{FILENAME}_dynamic.tex},
+containing the list of all the dynamic model equations.
+
+If @LaTeX{} names were given for variables and parameters
+(@pxref{Variable declarations}), then those will be used; otherwise,
+the plain text names will be used.
+
+Time subscripts (@code{t}, @code{t+1}, @code{t-1}, @dots{}) will be
+appended to the variable names, as @LaTeX{} subscripts.
+
+@end deffn
 
 @anchor{write_latex_dynamic_model}
 
diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc
index 76cea6dc39..2c0c5d2978 100644
--- a/preprocessor/ComputingTasks.cc
+++ b/preprocessor/ComputingTasks.cc
@@ -1431,6 +1431,17 @@ WriteLatexStaticModelStatement::writeOutput(ostream &output, const string &basen
   static_model.writeLatexFile(basename);
 }
 
+WriteLatexOriginalModelStatement::WriteLatexOriginalModelStatement(const DynamicModel &original_model_arg) :
+  original_model(original_model_arg)
+{
+}
+
+void
+WriteLatexOriginalModelStatement::writeOutput(ostream &output, const string &basename) const
+{
+  original_model.writeLatexOriginalFile(basename);
+}
+
 ShockDecompositionStatement::ShockDecompositionStatement(const SymbolList &symbol_list_arg,
                                                          const OptionsList &options_list_arg) :
   symbol_list(symbol_list_arg),
diff --git a/preprocessor/ComputingTasks.hh b/preprocessor/ComputingTasks.hh
index b1a009ffac..36732cc249 100644
--- a/preprocessor/ComputingTasks.hh
+++ b/preprocessor/ComputingTasks.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2014 Dynare Team
+ * Copyright (C) 2003-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -509,6 +509,15 @@ public:
   virtual void writeOutput(ostream &output, const string &basename) const;
 };
 
+class WriteLatexOriginalModelStatement : public Statement
+{
+private:
+  const DynamicModel &original_model;
+public:
+  WriteLatexOriginalModelStatement(const DynamicModel &original_model_arg);
+  virtual void writeOutput(ostream &output, const string &basename) const;
+};
+
 class ShockDecompositionStatement : public Statement
 {
 private:
diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc
index fab1f84a9f..c261f178ab 100644
--- a/preprocessor/DynamicModel.cc
+++ b/preprocessor/DynamicModel.cc
@@ -3997,6 +3997,12 @@ DynamicModel::writeLatexFile(const string &basename) const
   writeLatexModelFile(basename + "_dynamic.tex", oLatexDynamicModel);
 }
 
+void
+DynamicModel::writeLatexOriginalFile(const string &basename) const
+{
+  writeLatexModelFile(basename + "_original.tex", oLatexDynamicModel);
+}
+
 void
 DynamicModel::substituteEndoLeadGreaterThanTwo(bool deterministic_model)
 {
diff --git a/preprocessor/DynamicModel.hh b/preprocessor/DynamicModel.hh
index 7f019f260f..8efbc5d3c6 100644
--- a/preprocessor/DynamicModel.hh
+++ b/preprocessor/DynamicModel.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2014 Dynare Team
+ * Copyright (C) 2003-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -252,6 +252,9 @@ public:
   //! Writes LaTeX file with the equations of the dynamic model
   void writeLatexFile(const string &basename) const;
 
+  //! Writes LaTeX file with the equations of the dynamic model (for the original model)
+  void writeLatexOriginalFile(const string &basename) const;
+
   virtual int getDerivID(int symb_id, int lag) const throw (UnknownDerivIDException);
   virtual int getDynJacobianCol(int deriv_id) const throw (UnknownDerivIDException);
   virtual void addAllParamDerivId(set<int> &deriv_id_set);
diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy
index 737696d4cb..3760a46ede 100644
--- a/preprocessor/DynareBison.yy
+++ b/preprocessor/DynareBison.yy
@@ -126,7 +126,7 @@ class ParsingDriver;
 %token <string_val> TEX_NAME
 %token UNIFORM_PDF UNIT_ROOT_VARS USE_DLL USEAUTOCORR GSA_SAMPLE_FILE USE_UNIVARIATE_FILTERS_IF_SINGULARITY_IS_DETECTED
 %token VALUES VAR VAREXO VAREXO_DET VAROBS PREDETERMINED_VARIABLES
-%token WRITE_LATEX_DYNAMIC_MODEL WRITE_LATEX_STATIC_MODEL
+%token WRITE_LATEX_DYNAMIC_MODEL WRITE_LATEX_STATIC_MODEL WRITE_LATEX_ORIGINAL_MODEL
 %token XLS_SHEET XLS_RANGE LONG_NAME
 %left COMMA
 %left EQUAL_EQUAL EXCLAMATION_EQUAL
@@ -250,6 +250,7 @@ statement : parameters
           | identification
           | write_latex_dynamic_model
           | write_latex_static_model
+          | write_latex_original_model
           | shock_decomposition
           | conditional_forecast
           | conditional_forecast_paths
@@ -1906,6 +1907,10 @@ write_latex_static_model : WRITE_LATEX_STATIC_MODEL ';'
                            { driver.write_latex_static_model(); }
                          ;
 
+write_latex_original_model : WRITE_LATEX_ORIGINAL_MODEL ';'
+                           { driver.write_latex_original_model(); }
+                         ;
+
 shock_decomposition : SHOCK_DECOMPOSITION ';'
                       {driver.shock_decomposition(); }
                     | SHOCK_DECOMPOSITION '(' shock_decomposition_options_list ')' ';'
diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll
index 040ecf8223..cf65ff855c 100644
--- a/preprocessor/DynareFlex.ll
+++ b/preprocessor/DynareFlex.ll
@@ -129,6 +129,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2
 <INITIAL>save_params_and_steady_state  {BEGIN DYNARE_STATEMENT; return token::SAVE_PARAMS_AND_STEADY_STATE;}
 <INITIAL>write_latex_dynamic_model  {BEGIN DYNARE_STATEMENT; return token::WRITE_LATEX_DYNAMIC_MODEL;}
 <INITIAL>write_latex_static_model  {BEGIN DYNARE_STATEMENT; return token::WRITE_LATEX_STATIC_MODEL;}
+<INITIAL>write_latex_original_model  {BEGIN DYNARE_STATEMENT; return token::WRITE_LATEX_ORIGINAL_MODEL;}
 
 <INITIAL>steady {BEGIN DYNARE_STATEMENT; return token::STEADY;}
 <INITIAL>check {BEGIN DYNARE_STATEMENT; return token::CHECK;}
diff --git a/preprocessor/ExprNode.cc b/preprocessor/ExprNode.cc
index d4121b7292..d61ebb56f4 100644
--- a/preprocessor/ExprNode.cc
+++ b/preprocessor/ExprNode.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2014 Dynare Team
+ * Copyright (C) 2007-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -1851,8 +1851,20 @@ UnaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
       }
       return;
     case oExpectation:
-      cerr << "UnaryOpNode::writeOutput: not implemented on oExpectation" << endl;
-      exit(EXIT_FAILURE);
+      if (!IS_LATEX(output_type))
+        {
+          cerr << "UnaryOpNode::writeOutput: not implemented on oExpectation" << endl;
+          exit(EXIT_FAILURE);
+        }
+      output << "\\mathbb{E}_{t";
+      if (expectation_information_set != 0)
+        {
+          if (expectation_information_set > 0)
+            output << "+";
+          output << expectation_information_set;
+        }
+      output << "}";
+      break;
     case oErf:
       output << "erf";
       break;
diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc
index 809255973d..e21cf81e82 100644
--- a/preprocessor/ModFile.cc
+++ b/preprocessor/ModFile.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2014 Dynare Team
+ * Copyright (C) 2006-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -32,6 +32,7 @@
 
 ModFile::ModFile(WarningConsolidation &warnings_arg)
   : expressions_tree(symbol_table, num_constants, external_functions_table),
+    original_model(symbol_table, num_constants, external_functions_table),
     dynamic_model(symbol_table, num_constants, external_functions_table),
     trend_dynamic_model(symbol_table, num_constants, external_functions_table),
     ramsey_FOC_equations_dynamic_model(symbol_table, num_constants, external_functions_table),
@@ -306,6 +307,9 @@ ModFile::checkPass()
 void
 ModFile::transformPass(bool nostrict)
 {
+  // Save the original model (must be done before any model transformations by preprocessor)
+  dynamic_model.cloneDynamic(original_model);
+
   if (nostrict)
     {
       set<int> unusedEndogs = dynamic_model.findUnusedEndogenous();
diff --git a/preprocessor/ModFile.hh b/preprocessor/ModFile.hh
index 04aa1b5e4a..722ea53419 100644
--- a/preprocessor/ModFile.hh
+++ b/preprocessor/ModFile.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2014 Dynare Team
+ * Copyright (C) 2006-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -51,6 +51,8 @@ public:
   NumericalConstants num_constants;
   //! Expressions outside model block
   DataTree expressions_tree;
+  //! Original model, as declared in the "model" block, that won't be modified by the preprocessor
+  DynamicModel original_model;
   //! Dynamic model, as declared in the "model" block
   DynamicModel dynamic_model;
   //! A copy of Dynamic model, for testing trends declared by user
diff --git a/preprocessor/ModelTree.cc b/preprocessor/ModelTree.cc
index 7bdd78aed9..688d4f292d 100644
--- a/preprocessor/ModelTree.cc
+++ b/preprocessor/ModelTree.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2014 Dynare Team
+ * Copyright (C) 2003-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -1352,6 +1352,7 @@ ModelTree::writeLatexModelFile(const string &filename, ExprNodeOutputType output
   output << "\\documentclass[10pt,a4paper]{article}" << endl
          << "\\usepackage[landscape]{geometry}" << endl
          << "\\usepackage{fullpage}" << endl
+         << "\\usepackage{amsfonts}" << endl
          << "\\usepackage{breqn}" << endl
          << "\\begin{document}" << endl
          << "\\footnotesize" << endl;
diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc
index c7f6b2574e..acf0c8a914 100644
--- a/preprocessor/ParsingDriver.cc
+++ b/preprocessor/ParsingDriver.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2014 Dynare Team
+ * Copyright (C) 2003-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -1820,6 +1820,12 @@ ParsingDriver::write_latex_static_model()
   mod_file->addStatement(new WriteLatexStaticModelStatement(mod_file->static_model));
 }
 
+void
+ParsingDriver::write_latex_original_model()
+{
+  mod_file->addStatement(new WriteLatexOriginalModelStatement(mod_file->original_model));
+}
+
 void
 ParsingDriver::bvar_density(string *maxnlags)
 {
diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh
index a747e896ca..ace9d1223d 100644
--- a/preprocessor/ParsingDriver.hh
+++ b/preprocessor/ParsingDriver.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2014 Dynare Team
+ * Copyright (C) 2003-2015 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -503,6 +503,8 @@ public:
   void write_latex_dynamic_model();
   //! Adds a write_latex_static_model statement
   void write_latex_static_model();
+  //! Adds a write_latex_original_model statement
+  void write_latex_original_model();
   //! BVAR marginal density
   void bvar_density(string *maxnlags);
   //! BVAR forecast
diff --git a/tests/expectations/expectation.mod b/tests/expectations/expectation.mod
index c05d7528d1..23a36a0e67 100644
--- a/tests/expectations/expectation.mod
+++ b/tests/expectations/expectation.mod
@@ -35,6 +35,8 @@ e = 0;
 u = 0;
 end;
 
+write_latex_original_model;
+
 shocks;
 var e; stderr 0.009;
 var u; stderr 0.009;
-- 
GitLab