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 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 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; 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); Free_IM(p_First_IM);
count_Equ = count_Block = 0; count_Equ = count_Block = 0;
if (*prologue) 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++) for(j = 0;j < res->n_sets;j++)
{ {
if(res->sets_f[res->ordered[j]] == res->sets_s[res->ordered[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 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) if (*epilogue)
Allocate_Block(*epilogue, &count_Equ, &count_Block, EPILOGUE, ModelBlock, Table, TableSize); Allocate_Block(*epilogue, &count_Equ, &count_Block, EPILOGUE, ModelBlock);
return 0; return 0;
} }
@ -984,16 +984,16 @@ BlockTriangular::Normalize_and_BlockDecompose(bool* IM, Model_Block* ModelBlock,
Free_IM(p_First_IM); Free_IM(p_First_IM);
count_Equ = count_Block = 0; count_Equ = count_Block = 0;
if (*prologue) 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++) for(j = 0;j < res->n_sets;j++)
{ {
if(res->sets_f[res->ordered[j]] == res->sets_s[res->ordered[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 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) if (*epilogue)
Allocate_Block(*epilogue, &count_Equ, &count_Block, EPILOGUE, ModelBlock, Table, TableSize); Allocate_Block(*epilogue, &count_Equ, &count_Block, EPILOGUE, ModelBlock);
return 0; return 0;
} }
@ -1188,11 +1188,3 @@ BlockTriangular::Normalize_and_BlockDecompose_Static_0_Model(const jacob_map &j_
for(i = 0;i < endo_nbr;i++) 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"; 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 == eExogenousDet
|| type == eExogenous || type == eExogenous
|| type == eRecursiveVariable) || 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 else
var_id = -1; var_id = -1;
@ -288,7 +288,7 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
{ {
case oMatlabDynamicModel: case oMatlabDynamicModel:
case oCDynamicModel: 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); output << "y" << LPAR(output_type) << i << RPAR(output_type);
break; break;
case oMatlabStaticModel: case oMatlabStaticModel:

View File

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

View File

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

View File

@ -83,9 +83,8 @@ public:
void getMax_Lead_Lag(int var, int equ, int *lead, int *lag); 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 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 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 Free_Block(Model_Block* ModelBlock);
void SetVariableTable(int *Table,int Size,int HSize);
string getnamebyID(Type type, int id); string getnamebyID(Type type, int id);
List_IM *First_IM ; List_IM *First_IM ;
List_IM *Last_IM ; List_IM *Last_IM ;
@ -94,8 +93,7 @@ public:
int prologue, epilogue; int prologue, epilogue;
int Model_Max_Lead, Model_Max_Lag, periods; int Model_Max_Lead, Model_Max_Lag, periods;
bool bt_verbose; bool bt_verbose;
int endo_nbr, TableSize; int endo_nbr;
int* Table;
Model_Block* ModelBlock; Model_Block* ModelBlock;
inline static std::string BlockType0(int type) inline static std::string BlockType0(int type)
{ {

View File

@ -87,9 +87,9 @@ private:
//! Computes derivatives of ModelTree //! Computes derivatives of ModelTree
void derive(int order); void derive(int order);
//! Write derivative of an equation w.r. to a variable //! 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 //! 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 //! Computes temporary terms
void computeTemporaryTerms(int order); void computeTemporaryTerms(int order);
void computeTemporaryTermsOrdered(int order, Model_Block *ModelBlock); void computeTemporaryTermsOrdered(int order, Model_Block *ModelBlock);

View File

@ -29,7 +29,7 @@ using namespace std;
#include <iostream> #include <iostream>
//! Enumeration of possible symbol types //! 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 enum Type
{ {
eEndogenous = 0, //!< Endogenous eEndogenous = 0, //!< Endogenous
@ -56,14 +56,11 @@ private:
//! A symbol is represented by a pair (type, id) //! A symbol is represented by a pair (type, id)
typedef pair<Type, int> symbol_type; typedef pair<Type, int> symbol_type;
//! Type for map: symbol_name -> (type, id)
typedef map<string, symbol_type> symbol_table_type; typedef map<string, symbol_type> symbol_table_type;
//! Maps strings to pairs (type,id) //! Maps strings to pairs (type,id)
symbol_table_type symbol_table; symbol_table_type symbol_table;
//! Type for map: (type, id) -> symbol_name
typedef map<symbol_type, string> inv_symbol_table_type; typedef map<symbol_type, string> inv_symbol_table_type;
//! Maps pairs (type, id) to names //! Maps pairs (type, id) to names
inv_symbol_table_type name_table; inv_symbol_table_type name_table;
//! Maps pairs (type, id) to TeX names //! Maps pairs (type, id) to TeX names
@ -128,7 +125,6 @@ public:
inline int getID(const string &name) const throw (UnknownSymbolNameException); inline int getID(const string &name) const throw (UnknownSymbolNameException);
//! Write output of this class //! Write output of this class
void writeOutput(ostream &output) const; void writeOutput(ostream &output) const;
}; };
inline bool inline bool

View File

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