v4 parser: refactored VariableTable class

git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1585 ac1d8469-bf42-47a9-8791-bf33cf982152
time-shift
sebastien 2008-01-11 16:52:13 +00:00
parent 04684d52a0
commit 9b0dbc3322
8 changed files with 168 additions and 261 deletions

View File

@ -384,7 +384,7 @@ BlockTriangular::getMax_Lead_Lag_B(int size, int* Equation, int *Variable, int *
void
BlockTriangular::Allocate_Block(int size, int *count_Equ, int *count_Block, int type, Model_Block * ModelBlock, int* Table, int TableSize)
BlockTriangular::Allocate_Block(int size, int *count_Equ, int *count_Block, int type, Model_Block * ModelBlock)
{
int i, j, k, l, ls, m, i_1, Lead, Lag, size_list_lead_var, first_count_equ, i1;
int *list_lead_var, *tmp_size, *tmp_var, *tmp_endo, nb_lead_lag_endo;
@ -882,16 +882,16 @@ BlockTriangular::Normalize_and_BlockDecompose(bool* IM, Model_Block* ModelBlock,
Free_IM(p_First_IM);
count_Equ = count_Block = 0;
if (*prologue)
Allocate_Block(*prologue, &count_Equ, &count_Block, PROLOGUE, ModelBlock, Table, TableSize);
Allocate_Block(*prologue, &count_Equ, &count_Block, PROLOGUE, ModelBlock);
for(j = 0;j < res->n_sets;j++)
{
if(res->sets_f[res->ordered[j]] == res->sets_s[res->ordered[j]])
Allocate_Block(res->sets_f[res->ordered[j]] - res->sets_s[res->ordered[j]] + 1, &count_Equ, &count_Block, PROLOGUE, ModelBlock, Table, TableSize);
Allocate_Block(res->sets_f[res->ordered[j]] - res->sets_s[res->ordered[j]] + 1, &count_Equ, &count_Block, PROLOGUE, ModelBlock);
else
Allocate_Block(res->sets_f[res->ordered[j]] - res->sets_s[res->ordered[j]] + 1, &count_Equ, &count_Block, SIMULTANS, ModelBlock, Table, TableSize);
Allocate_Block(res->sets_f[res->ordered[j]] - res->sets_s[res->ordered[j]] + 1, &count_Equ, &count_Block, SIMULTANS, ModelBlock);
}
if (*epilogue)
Allocate_Block(*epilogue, &count_Equ, &count_Block, EPILOGUE, ModelBlock, Table, TableSize);
Allocate_Block(*epilogue, &count_Equ, &count_Block, EPILOGUE, ModelBlock);
return 0;
}
@ -984,16 +984,16 @@ BlockTriangular::Normalize_and_BlockDecompose(bool* IM, Model_Block* ModelBlock,
Free_IM(p_First_IM);
count_Equ = count_Block = 0;
if (*prologue)
Allocate_Block(*prologue, &count_Equ, &count_Block, PROLOGUE, ModelBlock, Table, TableSize);
Allocate_Block(*prologue, &count_Equ, &count_Block, PROLOGUE, ModelBlock);
for(j = 0;j < res->n_sets;j++)
{
if(res->sets_f[res->ordered[j]] == res->sets_s[res->ordered[j]])
Allocate_Block(res->sets_f[res->ordered[j]] - res->sets_s[res->ordered[j]] + 1, &count_Equ, &count_Block, PROLOGUE, ModelBlock, Table, TableSize);
Allocate_Block(res->sets_f[res->ordered[j]] - res->sets_s[res->ordered[j]] + 1, &count_Equ, &count_Block, PROLOGUE, ModelBlock);
else
Allocate_Block(res->sets_f[res->ordered[j]] - res->sets_s[res->ordered[j]] + 1, &count_Equ, &count_Block, SIMULTANS, ModelBlock, Table, TableSize);
Allocate_Block(res->sets_f[res->ordered[j]] - res->sets_s[res->ordered[j]] + 1, &count_Equ, &count_Block, SIMULTANS, ModelBlock);
}
if (*epilogue)
Allocate_Block(*epilogue, &count_Equ, &count_Block, EPILOGUE, ModelBlock, Table, TableSize);
Allocate_Block(*epilogue, &count_Equ, &count_Block, EPILOGUE, ModelBlock);
return 0;
}
@ -1188,11 +1188,3 @@ BlockTriangular::Normalize_and_BlockDecompose_Static_0_Model(const jacob_map &j_
for(i = 0;i < endo_nbr;i++)
cout << "Block=" << Index_Equ_IM[i].block << " Equ=" << Index_Equ_IM[i].index << " Var= " << Index_Var_IM[i].index << " " << symbol_table.getNameByID(eEndogenous, Index_Var_IM[i].index) << "\n";
}
void
BlockTriangular::SetVariableTable(int *Table, int Size, int HSize)
{
BlockTriangular::Table = Table;
TableSize = Size;
}

View File

@ -190,7 +190,7 @@ VariableNode::VariableNode(DataTree &datatree_arg, int symb_id_arg, Type type_ar
|| type == eExogenousDet
|| type == eExogenous
|| type == eRecursiveVariable)
var_id = datatree.variable_table.AddVariable(datatree.symbol_table.getNameByID(type, symb_id), lag);
var_id = datatree.variable_table.addVariable(type, symb_id, lag);
else
var_id = -1;
@ -288,7 +288,7 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
{
case oMatlabDynamicModel:
case oCDynamicModel:
i = datatree.variable_table.getPrintIndex(var_id) + OFFSET(output_type);
i = datatree.variable_table.getSortID(var_id) + OFFSET(output_type);
output << "y" << LPAR(output_type) << i << RPAR(output_type);
break;
case oMatlabStaticModel:

View File

@ -52,10 +52,11 @@ ModelTree::equation_number() const
}
void
ModelTree::writeDerivative(ostream &output, int eq, int var, int lag, ExprNodeOutputType output_type,
const temporary_terms_type &temporary_terms) const
ModelTree::writeDerivative(ostream &output, int eq, int symb_id, int lag,
ExprNodeOutputType output_type,
const temporary_terms_type &temporary_terms) const
{
first_derivatives_type::const_iterator it = first_derivatives.find(make_pair(eq, variable_table.getmVariableSelector(var, lag)));
first_derivatives_type::const_iterator it = first_derivatives.find(make_pair(eq, variable_table.getID(eEndogenous, symb_id, lag)));
if (it != first_derivatives.end())
(it->second)->writeOutput(output, output_type, temporary_terms);
else
@ -63,9 +64,9 @@ ModelTree::writeDerivative(ostream &output, int eq, int var, int lag, ExprNodeOu
}
void
ModelTree::compileDerivative(ofstream &code_file, int eq, int var, int lag, ExprNodeOutputType output_type, map_idx_type map_idx) const
ModelTree::compileDerivative(ofstream &code_file, int eq, int symb_id, int lag, ExprNodeOutputType output_type, map_idx_type map_idx) const
{
first_derivatives_type::const_iterator it = first_derivatives.find(make_pair(eq, variable_table.getmVariableSelector(var, lag)));
first_derivatives_type::const_iterator it = first_derivatives.find(make_pair(eq, variable_table.getID(eEndogenous, symb_id, lag)));
if (it != first_derivatives.end())
{
/*NodeID Id = it->second;*/
@ -305,7 +306,7 @@ ModelTree::computeTemporaryTermsOrdered(int order, Model_Block *ModelBlock)
{
eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_Index[i];
var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index[i];
it=first_derivatives.find(make_pair(eq,variable_table.getmVariableSelector(var,lag)));
it=first_derivatives.find(make_pair(eq,variable_table.getID(eEndogenous, var,lag)));
it->second->computeTemporaryTerms(reference_count, temporary_terms, first_occurence, j, ModelBlock, map_idx);
}
}
@ -318,7 +319,7 @@ ModelTree::computeTemporaryTermsOrdered(int order, Model_Block *ModelBlock)
{
eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_Index[i];
var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index[i];
it=first_derivatives.find(make_pair(eq,variable_table.getmVariableSelector(var,0)));
it=first_derivatives.find(make_pair(eq,variable_table.getID(eEndogenous,var,0)));
it->second->computeTemporaryTerms(reference_count, temporary_terms, first_occurence, j, ModelBlock, map_idx);
}
}
@ -326,7 +327,7 @@ ModelTree::computeTemporaryTermsOrdered(int order, Model_Block *ModelBlock)
{
eq=ModelBlock->Block_List[j].Equation[0];
var=ModelBlock->Block_List[j].Variable[0];
it=first_derivatives.find(make_pair(eq,variable_table.getmVariableSelector(var,0)));
it=first_derivatives.find(make_pair(eq,variable_table.getID(eEndogenous,var,0)));
it->second->computeTemporaryTerms(reference_count, temporary_terms, first_occurence, j, ModelBlock, map_idx);
}
}
@ -347,7 +348,7 @@ void
ModelTree::writeModelEquationsOrdered_C(ostream &output, Model_Block *ModelBlock) const
{
int i,j,k,m;
string sModel, tmp_s;
string tmp_s;
ostringstream tmp_output;
NodeID lhs=NULL, rhs=NULL;
BinaryOpNode *eq_node;
@ -435,8 +436,8 @@ ModelTree::writeModelEquationsOrdered_C(ostream &output, Model_Block *ModelBlock
// The equations
for(i = 0;i < ModelBlock->Block_List[j].Size;i++)
{
sModel = symbol_table.getNameByID(eEndogenous, ModelBlock->Block_List[j].Variable[i]) ;
ModelBlock->Block_List[j].Variable_Sorted[i] = variable_table.getID(sModel, 0);
ModelBlock->Block_List[j].Variable_Sorted[i] = variable_table.getID(eEndogenous, ModelBlock->Block_List[j].Variable[i], 0);
string sModel = symbol_table.getNameByID(eEndogenous, ModelBlock->Block_List[j].Variable[i]) ;
output << " //equation " << ModelBlock->Block_List[j].Equation[i] << " variable : " <<
sModel << " (" << ModelBlock->Block_List[j].Variable[i] << ")\n";
if (!lhs_rhs_done)
@ -585,7 +586,7 @@ void
ModelTree::writeModelEquationsOrdered_M(ostream &output, Model_Block *ModelBlock, const string &dynamic_basename) const
{
int i,j,k,m;
string sModel, tmp_s, sps;
string tmp_s, sps;
ostringstream tmp_output, global_output;
NodeID lhs=NULL, rhs=NULL;
BinaryOpNode *eq_node;
@ -703,8 +704,8 @@ ModelTree::writeModelEquationsOrdered_M(ostream &output, Model_Block *ModelBlock
// The equations
for(i = 0;i < ModelBlock->Block_List[j].Size;i++)
{
sModel = symbol_table.getNameByID(eEndogenous, ModelBlock->Block_List[j].Variable[i]) ;
ModelBlock->Block_List[j].Variable_Sorted[i] = variable_table.getID(sModel, 0);
ModelBlock->Block_List[j].Variable_Sorted[i] = variable_table.getID(eEndogenous, ModelBlock->Block_List[j].Variable[i], 0);
string sModel = symbol_table.getNameByID(eEndogenous, ModelBlock->Block_List[j].Variable[i]) ;
output << sps << " " << interfaces::comment() << "equation " << ModelBlock->Block_List[j].Equation[i] << " variable : " <<
sModel << " (" << ModelBlock->Block_List[j].Variable[i] << ")\n";
if (!lhs_rhs_done)
@ -865,7 +866,7 @@ void
ModelTree::writeModelStaticEquationsOrdered_M(ostream &output, Model_Block *ModelBlock, const string &static_basename) const
{
int i,j,k,m, var, eq;
string sModel, tmp_s, sps;
string tmp_s, sps;
ostringstream tmp_output, global_output;
NodeID lhs=NULL, rhs=NULL;
BinaryOpNode *eq_node;
@ -993,8 +994,8 @@ ModelTree::writeModelStaticEquationsOrdered_M(ostream &output, Model_Block *Mode
// The equations
for(i = 0;i < ModelBlock->Block_List[j].Size;i++)
{
sModel = symbol_table.getNameByID(eEndogenous, ModelBlock->Block_List[j].Variable[i]) ;
ModelBlock->Block_List[j].Variable_Sorted[i] = variable_table.getID(sModel, 0);
ModelBlock->Block_List[j].Variable_Sorted[i] = variable_table.getID(eEndogenous, ModelBlock->Block_List[j].Variable[i], 0);
string sModel = symbol_table.getNameByID(eEndogenous, ModelBlock->Block_List[j].Variable[i]) ;
output << sps << " " << interfaces::comment() << "equation " << ModelBlock->Block_List[j].Equation[i] << " variable : " <<
sModel << " (" << ModelBlock->Block_List[j].Variable[i] << ")\n";
if (!lhs_rhs_done)
@ -1157,7 +1158,7 @@ ModelTree::writeModelEquationsCodeOrdered(const string file_name, const Model_Bl
};
int i,j,k,m, v, ModelBlock_Aggregated_Count, k0, k1;
string sModel, tmp_s;
string tmp_s;
ostringstream tmp_output;
ofstream code_file;
NodeID lhs=NULL, rhs=NULL;
@ -1298,8 +1299,7 @@ ModelTree::writeModelEquationsCodeOrdered(const string file_name, const Model_Bl
// The equations
for(i = 0;i < ModelBlock->Block_List[j].Size;i++)
{
sModel = symbol_table.getNameByID(eEndogenous, ModelBlock->Block_List[j].Variable[i]) ;
ModelBlock->Block_List[j].Variable_Sorted[i] = variable_table.getID(sModel, 0);
ModelBlock->Block_List[j].Variable_Sorted[i] = variable_table.getID(eEndogenous, ModelBlock->Block_List[j].Variable[i], 0);
if (!lhs_rhs_done)
{
eq_node = equations[ModelBlock->Block_List[j].Equation[i]];
@ -3517,14 +3517,16 @@ ModelTree::writeOutput(ostream &output) const
// Loop on periods
for(int lag = -variable_table.max_endo_lag; lag <= variable_table.max_endo_lead; lag++)
{
// Getting name of symbol
string name = symbol_table.getNameByID(eEndogenous, endoID);
// and its variableID if exists with current period
int varID = variable_table.getID(name, lag);
if (varID >= 0)
output << " " << variable_table.getPrintIndex(varID) + 1;
else
output << " 0";
// Print variableID if exists with current period, otherwise print 0
try
{
int varID = variable_table.getID(eEndogenous, endoID, lag);
output << " " << variable_table.getSortID(varID) + 1;
}
catch(VariableTable::UnknownVariableKeyException &e)
{
output << " 0";
}
}
output << ";";
}
@ -3633,7 +3635,7 @@ ModelTree::BlockLinear(Model_Block *ModelBlock)
{
int eq=ModelBlock->Block_List[j].IM_lead_lag[ll].Equ_Index[i];
int var=ModelBlock->Block_List[j].IM_lead_lag[ll].Var_Index[i];
first_derivatives_type::const_iterator it=first_derivatives.find(make_pair(eq,variable_table.getmVariableSelector(var,0)));
first_derivatives_type::const_iterator it=first_derivatives.find(make_pair(eq,variable_table.getID(eEndogenous,var,0)));
if (it!= first_derivatives.end())
{
NodeID Id = it->second;
@ -3661,7 +3663,7 @@ ModelTree::BlockLinear(Model_Block *ModelBlock)
{
int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_Index[i];
int var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index[i];
first_derivatives_type::const_iterator it=first_derivatives.find(make_pair(eq,variable_table.getmVariableSelector(var,k1)));
first_derivatives_type::const_iterator it=first_derivatives.find(make_pair(eq,variable_table.getID(eEndogenous,var,k1)));
NodeID Id = it->second;
if (it!= first_derivatives.end())
{
@ -3692,9 +3694,7 @@ ModelTree::computingPass(const eval_context_type &eval_context)
cout << equations.size() << " equation(s) found" << endl;
// Sorting variable table
variable_table.Sort();
variable_table.setmVariableSelector();
variable_table.sort();
// Determine derivation order
int order = 1;
@ -3709,10 +3709,6 @@ ModelTree::computingPass(const eval_context_type &eval_context)
if (mode == eSparseDLLMode || mode == eSparseMode)
{
jacob_map j_m;
int Size;
int HSize;
int *Table=variable_table.GetVariableTable(&Size,&HSize);
evaluateJacobian(eval_context, &j_m);
@ -3721,7 +3717,6 @@ ModelTree::computingPass(const eval_context_type &eval_context)
cout << "The gross incidence matrix \n";
block_triangular.Print_IM( symbol_table.endo_nbr);
}
block_triangular.SetVariableTable(Table, Size, HSize);
block_triangular.Normalize_and_BlockDecompose_Static_0_Model(j_m);
BlockLinear(block_triangular.ModelBlock);

View File

@ -17,9 +17,6 @@
* along with Dynare. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <algorithm>
#include "VariableTable.hh"
VariableTable::VariableTable(const SymbolTable &symbol_table_arg) :
@ -34,22 +31,23 @@ VariableTable::VariableTable(const SymbolTable &symbol_table_arg) :
}
int
VariableTable::AddVariable(const string &iName, int iLag)
VariableTable::addVariable(Type type, int symb_id, int lag)
{
// Testing if variable exists in VariableTable
int lVariableID = getID(iName,iLag);
if (lVariableID != -1)
return lVariableID;
if (sorted_ids_table.size() != 0)
throw AlreadySortedException();
lVariableID = mVariableIndex.size();
// Making variable struct and key
varKey key = make_pair(iName, iLag);
// Pushing variable on VariableTable
mVariableTable[key] = lVariableID;
mVariableIndex.push_back(key);
var_key_type key = make_pair(make_pair(type, lag), symb_id);
// Testing if variable already exists
variable_table_type::const_iterator it = variable_table.find(key);
if (it != variable_table.end())
return it->second;
int var_id = size();
variable_table[key] = var_id;
inv_variable_table[var_id] = key;
// Setting dynamic variables numbers
Type type = getType(lVariableID);
if (type == eEndogenous)
var_endo_nbr++;
if (type == eExogenous)
@ -58,132 +56,53 @@ VariableTable::AddVariable(const string &iName, int iLag)
var_exo_det_nbr++;
// Setting maximum and minimum lags
if (max_lead < iLag)
max_lead = iLag;
else if (-max_lag > iLag)
max_lag = -iLag;
if (max_lead < lag)
max_lead = lag;
else if (-max_lag > lag)
max_lag = -lag;
switch(type)
{
case eEndogenous:
if (max_endo_lead < iLag)
max_endo_lead = iLag;
else if (-max_endo_lag > iLag)
max_endo_lag = -iLag;
if (max_endo_lead < lag)
max_endo_lead = lag;
else if (-max_endo_lag > lag)
max_endo_lag = -lag;
break;
case eExogenous:
if (max_exo_lead < iLag)
max_exo_lead = iLag;
else if (-max_exo_lag > iLag)
max_exo_lag = -iLag;
if (max_exo_lead < lag)
max_exo_lead = lag;
else if (-max_exo_lag > lag)
max_exo_lag = -lag;
break;
case eExogenousDet:
if (max_exo_det_lead < iLag)
max_exo_det_lead = iLag;
else if (-max_exo_det_lag > iLag)
max_exo_det_lag = -iLag;
if (max_exo_det_lead < lag)
max_exo_det_lead = lag;
else if (-max_exo_det_lag > lag)
max_exo_det_lag = -lag;
break;
case eRecursiveVariable:
if (max_recur_lead < iLag)
max_recur_lead = iLag;
else if (-max_recur_lag > iLag)
max_recur_lag = -iLag;
if (max_recur_lead < lag)
max_recur_lead = lag;
else if (-max_recur_lag > lag)
max_recur_lag = -lag;
break;
default:
;
}
return lVariableID;
return var_id;
}
void
VariableTable::Sort()
VariableTable::sort()
{
// Trivial case where no ordering is necessary
if (mVariableIndex.size() == 1)
{
mSortedVariableID.push_back(0);
mPrintFormatIndex.push_back(0);
return;
}
if (sorted_ids_table.size() != 0)
throw AlreadySortedException();
/* The type of key for lexicographic ordering over variables:
the key is equal to (type, lag, symbol_id) */
typedef pair<Type, pair<int, int> > lexicographic_key_type;
int sorted_id = 0;
sorted_ids_table.resize(size());
// Construct the vector matching keys to their varIDs
vector<pair<lexicographic_key_type, int> > VarToSort;
for (unsigned int varID = 0; varID < mVariableIndex.size(); varID++)
{
Type type = getType(varID);
int symbolID = getSymbolID(varID);
int lag = mVariableIndex[varID].second;
VarToSort.push_back(make_pair(make_pair(type, make_pair(lag, symbolID)), varID));
}
// Sort variables using the lexicographic ordering
sort(VarToSort.begin(), VarToSort.end());
// Fill mSortedVariableID and mPrintFormatIndex
mSortedVariableID.resize(VarToSort.size());
mPrintFormatIndex.resize(VarToSort.size());
Type type = getType(VarToSort[0].second);
int index = 0;
for (unsigned int sortedID = 0; sortedID < VarToSort.size(); sortedID++)
{
int varID = VarToSort[sortedID].second;
mSortedVariableID[varID] = sortedID;
if (type == getType(varID))
{
mPrintFormatIndex[varID] = index;
index++;
}
else
{
mPrintFormatIndex[varID] = 0;
type = getType(varID);
index = 1;
}
}
}
int*
VariableTable::GetVariableTable(int* Size, int* HSize)
{
int* Table;
varKey key;
int variable,id, ind;
(*Size)=0;
for (id=0; id < (int) mVariableIndex.size(); id++)
{
key = mVariableIndex[id];
variable = mVariableTable[key];
if(getType(variable)==eEndogenous)
(*Size)++;
}
(*HSize)=4;
Table=(int*)malloc((*Size)*(*HSize)*sizeof(*Table));
ind=0;
for (id=0; id < (int) mVariableIndex.size(); id++)
{
key = mVariableIndex[id];
variable = mVariableTable[key];
if (getType(variable)==eEndogenous)
{
Table[ind*(*HSize)]= getSymbolID(id);
Table[ind*(*HSize)+1]= key.second;
Table[ind*(*HSize)+2]= mPrintFormatIndex[id];
Table[ind*(*HSize)+3]= mSortedVariableID[id];
ind++;
}
}
return(Table);
}
int
VariableTable::getIDS(int id, int lead_lag) const
{
varKey key;
key=mVariableIndex[id];
map<varKey, int>::const_iterator it = mVariableTable.find(key);
return(it->second);
for(variable_table_type::const_iterator it = variable_table.begin();
it != variable_table.end(); it++)
sorted_ids_table[it->second] = sorted_id++;
}

View File

@ -83,9 +83,8 @@ public:
void getMax_Lead_Lag(int var, int equ, int *lead, int *lag);
void getMax_Lead_Lag_B(int size, int* Equation, int *Variable, int *lead, int *lag);
void swap_IM_c(bool *SIM, int pos1, int pos2, int pos3, simple* Index_Var_IM, simple* Index_Equ_IM, int n);
void Allocate_Block(int size, int *count_Equ, int *count_Block, int type, Model_Block * ModelBlock, int* Table, int TableSize);
void Allocate_Block(int size, int *count_Equ, int *count_Block, int type, Model_Block * ModelBlock);
void Free_Block(Model_Block* ModelBlock);
void SetVariableTable(int *Table,int Size,int HSize);
string getnamebyID(Type type, int id);
List_IM *First_IM ;
List_IM *Last_IM ;
@ -94,8 +93,7 @@ public:
int prologue, epilogue;
int Model_Max_Lead, Model_Max_Lag, periods;
bool bt_verbose;
int endo_nbr, TableSize;
int* Table;
int endo_nbr;
Model_Block* ModelBlock;
inline static std::string BlockType0(int type)
{

View File

@ -87,9 +87,9 @@ private:
//! Computes derivatives of ModelTree
void derive(int order);
//! Write derivative of an equation w.r. to a variable
void writeDerivative(ostream &output, int eq, int var, int lag, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms) const;
void writeDerivative(ostream &output, int eq, int symb_id, int lag, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms) const;
//! Write derivative code of an equation w.r. to a variable
void compileDerivative(ofstream &code_file, int eq, int var, int lag, ExprNodeOutputType output_type, map_idx_type map_idx) const;
void compileDerivative(ofstream &code_file, int eq, int symb_id, int lag, ExprNodeOutputType output_type, map_idx_type map_idx) const;
//! Computes temporary terms
void computeTemporaryTerms(int order);
void computeTemporaryTermsOrdered(int order, Model_Block *ModelBlock);

View File

@ -29,7 +29,7 @@ using namespace std;
#include <iostream>
//! Enumeration of possible symbol types
/*! Be careful not to change the order of the enumeration, it matters for VariableTable (at least up to eParameter) */
/*! Warning: do not to change the order of the enumeration, it matters for VariableTable (at least ensure that eEndogenous is the first one) */
enum Type
{
eEndogenous = 0, //!< Endogenous
@ -56,14 +56,11 @@ private:
//! A symbol is represented by a pair (type, id)
typedef pair<Type, int> symbol_type;
//! Type for map: symbol_name -> (type, id)
typedef map<string, symbol_type> symbol_table_type;
//! Maps strings to pairs (type,id)
symbol_table_type symbol_table;
//! Type for map: (type, id) -> symbol_name
typedef map<symbol_type, string> inv_symbol_table_type;
//! Maps pairs (type, id) to names
inv_symbol_table_type name_table;
//! Maps pairs (type, id) to TeX names
@ -128,7 +125,6 @@ public:
inline int getID(const string &name) const throw (UnknownSymbolNameException);
//! Write output of this class
void writeOutput(ostream &output) const;
};
inline bool

View File

@ -23,34 +23,30 @@
using namespace std;
#include <map>
#include <string>
#include <vector>
#include "SymbolTable.hh"
//! This class is used to store variables as they appear in the model (with their lead or lag)
/*! \todo Raise exceptions when requesting ordered IDs before calling to Sort() */
//! Used to keep track of variables in the sense of the models, i.e. pairs (symbol, lead/lag)
/*! Warning: some methods access variables through the tuple (type, symbol_id, lag), but internally the class uses a lexicographic order over (type, lag, symbol_id) */
class VariableTable
{
private:
//! A reference to the symbol table
const SymbolTable &symbol_table;
//! Variable key type to acced variable table elements
typedef pair<string, int> varKey;
//! Maps a pair (symbol, lag) to an ID
map<varKey, int> mVariableTable;
//! Maps an ID to a pair (symbol, lag)
/*! It is the reverse map of mVariableTable */
vector<varKey> mVariableIndex;
//! Variable IDs of sorted variable table
vector<int> mSortedVariableID;
//! For each variable, gives its index number among variables of the same type
/*! It is the index used in the output file:
- in the lead/lag matrix
- in the right hand side of equations (such as y(index))
*/
vector<int> mPrintFormatIndex;
map<pair<int, int>, int> mVariableSelector;
//! A variable is a tuple (type, lag, symbol_id)
/*! Warning: don't change the order of elements in the tuple, since this determines the lexicographic ordering in sort() */
typedef pair<pair<Type, int>, int> var_key_type;
typedef map<var_key_type, int> variable_table_type;
//! Maps a tuple (type, lag, symbol_id) to a variable ID
variable_table_type variable_table;
typedef map<int, var_key_type> inv_variable_table_type;
//! Maps a variable ID to a tuple (type, lag, symbol_id)
inv_variable_table_type inv_variable_table;
//! Contains the sorted IDs (indexed by variable IDs)
vector<int> sorted_ids_table;
public:
VariableTable(const SymbolTable &symbol_table_arg);
//! Number of dynamic endogenous variables inside the model block
@ -79,100 +75,111 @@ public:
int max_recur_lag;
//! Maximum lead over recursive variables
int max_recur_lead;
//! Adds a variable in the table, and returns its (newly allocated) varID
//! Adds a variable in the table, and returns its (newly allocated) variable ID
/*! Also works if the variable already exists */
int AddVariable(const string &iName, int iLag);
int addVariable(Type type, int symb_id, int lag);
//! Return variable ID
inline int getID(const string &iName, int iLag) const;
inline int getID(Type type, int symb_id, int lag) const;
//! Return lag of variable
inline int getLag(int ivarID) const;
//! Return symbol ID of variable
inline int getSymbolID(int ivarID) const;
//! Get variable type
inline Type getType(int ivarID) const;
//! Get number of variables in mVariableTable
//! Get number of variables
inline int size() const;
//! Get variable ID of sorted variable table
/*! In practice, only used for endogenous variables */
inline int getSortID(int iVarID) const;
//! Return variable index to print in format : y(index) or oo_.y_simul(index) ...
inline int getPrintIndex(int iVarID) const;
//! Sorts variable table
/*! The order used is a lexicographic order over the tuple (type, lag, symbolID) */
void Sort();
/*! The order used is a lexicographic order over the tuple (type, lag, symbol_id) */
void sort();
//! Get the number of dynamic variables
inline int get_dyn_var_nbr(void) const;
int* GetVariableTable(int* Size, int* HSize);
int getIDS(int id, int lead_lag) const;
void setmVariableSelector();
int getmVariableSelector(int var, int lag) const;
inline int get_dyn_var_nbr() const;
//! Thrown when trying to access an unknown variable by (type, symb_id, lag)
class UnknownVariableKeyException
{
public:
Type type;
int symb_id, lag;
UnknownVariableKeyException(Type type_arg, int symb_id_arg, int lag_arg) : type(type_arg), symb_id(symb_id_arg), lag(lag_arg) {}
};
//! Thrown when trying to access an unknown variable by var_id
class UnknownVariableIDException
{
public:
//! Variable ID
int id;
UnknownVariableIDException(int id_arg) : id(id_arg) {}
};
//! Thrown when getSortID() called before sort()
class NotYetSortedException
{
};
//! Thrown when sort() or addVariable() called after sort()
class AlreadySortedException
{
};
};
inline void
VariableTable::setmVariableSelector()
{
for(int var = 0; var < (int) mVariableTable.size(); var++)
{
if(getType(var)==eEndogenous)
mVariableSelector[make_pair(getSymbolID(var),mVariableIndex[var].second)]=var;
}
}
inline int
VariableTable::getmVariableSelector(int var, int lag) const
{
return(mVariableSelector.find(make_pair(var,lag))->second);
}
inline int
VariableTable::getSortID(int iVarID) const
{
return mSortedVariableID[iVarID];
if (sorted_ids_table.size() == 0)
throw NotYetSortedException();
return sorted_ids_table[iVarID];
}
inline int
VariableTable::getPrintIndex(int iVarID) const
VariableTable::getID(Type type, int symb_id, int lag) const
{
return mPrintFormatIndex[iVarID];
}
inline int
VariableTable::getID(const string &iName, int iLag) const
{
map<varKey, int>::const_iterator it = mVariableTable.find(make_pair(iName, iLag));
if (it == mVariableTable.end())
return -1;
variable_table_type::const_iterator it = variable_table.find(make_pair(make_pair(type, lag), symb_id));
if (it == variable_table.end())
throw UnknownVariableKeyException(type, symb_id, lag);
else
return it->second;
}
inline Type
VariableTable::getType(int ivarID) const
VariableTable::getType(int var_id) const
{
varKey key = mVariableIndex[ivarID];
return symbol_table.getType(key.first);
inv_variable_table_type::const_iterator it = inv_variable_table.find(var_id);
if (it != inv_variable_table.end())
return it->second.first.first;
else
throw UnknownVariableIDException(var_id);
}
inline int
VariableTable::getSymbolID(int ivarID) const
VariableTable::getSymbolID(int var_id) const
{
varKey key = mVariableIndex[ivarID];
return symbol_table.getID(key.first);
inv_variable_table_type::const_iterator it = inv_variable_table.find(var_id);
if (it != inv_variable_table.end())
return it->second.second;
else
throw UnknownVariableIDException(var_id);
}
inline int
VariableTable::getLag(int ivarID) const
VariableTable::getLag(int var_id) const
{
return mVariableIndex[ivarID].second;
inv_variable_table_type::const_iterator it = inv_variable_table.find(var_id);
if (it != inv_variable_table.end())
return it->second.first.second;
else
throw UnknownVariableIDException(var_id);
}
inline int
VariableTable::size() const
{
return mVariableTable.size();
return variable_table.size();
}
inline int
VariableTable::get_dyn_var_nbr(void) const
VariableTable::get_dyn_var_nbr() const
{
return var_endo_nbr + symbol_table.exo_nbr + symbol_table.exo_det_nbr;
}