From eb0ecd4cecf7a864c36efbabccb6ee94e8a544a7 Mon Sep 17 00:00:00 2001
From: Michel Juillard <michel.juillard@ens.fr>
Date: Fri, 10 Sep 2010 11:43:48 +0200
Subject: [PATCH] Preprocessor: corrected bug on HISTVAL with lags > 1
 (manually cherry-picked from a5b5f39987b32de65486acb7607d44e7512dbb9b)

---
 preprocessor/NumericalInitialization.cc | 22 +++++++++++++++++++---
 preprocessor/SymbolTable.cc             | 11 ++++++++++-
 preprocessor/SymbolTable.hh             |  9 ++++++++-
 3 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/preprocessor/NumericalInitialization.cc b/preprocessor/NumericalInitialization.cc
index 357469ead5..5dcfa899a5 100644
--- a/preprocessor/NumericalInitialization.cc
+++ b/preprocessor/NumericalInitialization.cc
@@ -175,16 +175,32 @@ HistValStatement::writeOutput(ostream &output, const string &basename) const
 {
   output << "%" << endl
          << "% HISTVAL instructions" << endl
-         << "%" << endl;
+         << "%" << endl
+         << "oo_.endo_simul = zeros(M_.endo_nbr,M_.maximum_lag);" << endl;
 
   for (hist_values_type::const_iterator it = hist_values.begin();
        it != hist_values.end(); it++)
     {
-      const int &symb_id = it->first.first;
-      const int &lag = it->first.second;
+      int symb_id = it->first.first;
+      int lag = it->first.second;
       const NodeID expression = it->second;
 
       SymbolType type = symbol_table.getType(symb_id);
+      if (type == eEndogenous && lag < 0)
+	{
+	  const int new_symb_id = symbol_table.searchAuxiliaryVars(symb_id,lag);
+	  if (new_symb_id != -1)
+	    {
+	      symb_id = new_symb_id;
+	      lag = 0;
+	    }
+	  else if (symbol_table.AuxVarsSize() > 0)
+	    {
+	      cerr << "Histval: this variable doesn't exist with such a lag in the model" << endl;
+	      exit(EXIT_FAILURE);
+	    }
+	    
+	}
       int tsid = symbol_table.getTypeSpecificID(symb_id) + 1;
 
       if (type == eEndogenous)
diff --git a/preprocessor/SymbolTable.cc b/preprocessor/SymbolTable.cc
index 74ae98165f..8c2a9b80eb 100644
--- a/preprocessor/SymbolTable.cc
+++ b/preprocessor/SymbolTable.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2009 Dynare Team
+ * Copyright (C) 2003-2010 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -343,6 +343,15 @@ SymbolTable::addExpectationAuxiliaryVar(int information_set, int index) throw (F
   return symb_id;
 }
 
+int 
+SymbolTable::searchAuxiliaryVars(int orig_symb_id, int orig_lead_lag) const
+{
+  for (int i=0; i < aux_vars.size();++i)
+    if ((aux_vars[i].orig_symb_id == orig_symb_id) && (aux_vars[i].orig_lead_lag == orig_lead_lag))
+      return aux_vars[i].symb_id;
+  return -1;
+}
+
 void
 SymbolTable::markPredetermined(int symb_id) throw (UnknownSymbolIDException, FrozenException)
 {
diff --git a/preprocessor/SymbolTable.hh b/preprocessor/SymbolTable.hh
index 7346e679bf..607677c370 100644
--- a/preprocessor/SymbolTable.hh
+++ b/preprocessor/SymbolTable.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2009 Dynare Team
+ * Copyright (C) 2003-2010 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -191,6 +191,13 @@ public:
     \return the symbol ID of the new symbol
   */
   int addExpectationAuxiliaryVar(int information_set, int index) throw (FrozenException);
+  //! Searches auxiliary variables by symbol_id and lead_lag
+  /*!
+    \return the symbol ID of the auxiliary variable and -1 if not found
+  */
+  int searchAuxiliaryVars(int orig_symb_id, int orig_lead_lag) const;
+  //! Returns the number of auxiliary variables
+  int AuxVarsSize() const {return aux_vars.size();};
   //! Tests if symbol already exists
   inline bool exists(const string &name) const;
   //! Get symbol name (by ID)
-- 
GitLab