Commit a67a20bf authored by ferhat's avatar ferhat
Browse files

- new Incidence_Matrix class

- lead and lag on exogenous variables
- corrections in dr1_sparse and dr11_sparse
- minor corrections in simulate

git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@2255 ac1d8469-bf42-47a9-8791-bf33cf982152
parent 01194422
...@@ -31,158 +31,346 @@ using namespace std; ...@@ -31,158 +31,346 @@ using namespace std;
#include "BlockTriangular.hh" #include "BlockTriangular.hh"
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
/*BlockTriangular::BlockTriangular(const SymbolTable &symbol_table_arg) :
symbol_table(symbol_table_arg),
normalization(symbol_table_arg)*/
BlockTriangular::BlockTriangular(const SymbolTable &symbol_table_arg) : BlockTriangular::BlockTriangular(const SymbolTable &symbol_table_arg) :
symbol_table(symbol_table_arg), symbol_table(symbol_table_arg),
normalization(symbol_table_arg) normalization(symbol_table_arg),
incidencematrix(symbol_table_arg)
{ {
bt_verbose = 0; bt_verbose = 0;
ModelBlock = NULL; ModelBlock = NULL;
Model_Max_Lead = 0;
Model_Max_Lag = 0;
periods = 0; periods = 0;
} }
IncidenceMatrix::IncidenceMatrix(const SymbolTable &symbol_table_arg) :
symbol_table(symbol_table_arg)
{
Model_Max_Lead = Model_Max_Lead_Endo = Model_Max_Lead_Exo = 0;
Model_Max_Lag = Model_Max_Lag_Endo = Model_Max_Lag_Exo = 0;
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
//For a lead or a lag build the Incidence Matrix structures //For a lead or a lag build the Incidence Matrix structures
List_IM* List_IM*
BlockTriangular::Build_IM(int lead_lag) IncidenceMatrix::Build_IM(int lead_lag, SymbolType type)
{ {
List_IM* pIM = new List_IM;
int i; int i;
Last_IM->pNext = pIM; List_IM* pIM = new List_IM;
pIM->IM = (bool*)malloc(endo_nbr * endo_nbr * sizeof(pIM->IM[0])); if(type==eEndogenous)
for(i = 0;i < endo_nbr*endo_nbr;i++)
pIM->IM[i] = 0;
pIM->lead_lag = lead_lag;
if(lead_lag > 0)
{ {
if(lead_lag > Model_Max_Lead) Last_IM->pNext = pIM;
Model_Max_Lead = lead_lag; pIM->IM = (bool*)malloc(symbol_table.endo_nbr * symbol_table.endo_nbr * sizeof(pIM->IM[0]));
for(i = 0;i < symbol_table.endo_nbr*symbol_table.endo_nbr;i++)
pIM->IM[i] = 0;
pIM->lead_lag = lead_lag;
if(lead_lag > 0)
{
if(lead_lag > Model_Max_Lead_Endo)
{
Model_Max_Lead_Endo = lead_lag;
if(lead_lag > Model_Max_Lead)
Model_Max_Lead = lead_lag;
}
}
else
{
if( -lead_lag > Model_Max_Lag_Endo)
{
Model_Max_Lag_Endo = -lead_lag;
if(-lead_lag > Model_Max_Lag)
Model_Max_Lag = -lead_lag;
}
}
pIM->pNext = NULL;
Last_IM = pIM;
} }
else else
{ { //eExogenous
if( -lead_lag > Model_Max_Lag) Last_IM_X->pNext = pIM;
Model_Max_Lag = -lead_lag; pIM->IM = (bool*)malloc(symbol_table.exo_nbr * symbol_table.endo_nbr * sizeof(pIM->IM[0]));
for(i = 0;i < symbol_table.exo_nbr*symbol_table.endo_nbr;i++)
pIM->IM[i] = 0;
pIM->lead_lag = lead_lag;
if(lead_lag > 0)
{
if(lead_lag > Model_Max_Lead_Exo)
{
Model_Max_Lead_Exo = lead_lag;
if(lead_lag > Model_Max_Lead)
Model_Max_Lead = lead_lag;
}
}
else
{
if( -lead_lag > Model_Max_Lag_Exo)
{
Model_Max_Lag_Exo = -lead_lag;
if(-lead_lag > Model_Max_Lag)
Model_Max_Lag = -lead_lag;
}
}
pIM->pNext = NULL;
Last_IM_X = pIM;
} }
pIM->pNext = NULL;
Last_IM = pIM;
return (pIM); return (pIM);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// initialize all the incidence matrix structures // initialize all the incidence matrix structures
void void
BlockTriangular::init_incidence_matrix(int nb_endo) IncidenceMatrix::init_incidence_matrix()
{ {
int i; int i;
endo_nbr = nb_endo;
First_IM = new List_IM; First_IM = new List_IM;
First_IM->IM = (bool*)malloc(nb_endo * nb_endo * sizeof(First_IM->IM[0])); First_IM->IM = (bool*)malloc(symbol_table.endo_nbr * symbol_table.endo_nbr * sizeof(First_IM->IM[0]));
for(i = 0;i < nb_endo*nb_endo;i++) for(i = 0;i < symbol_table.endo_nbr*symbol_table.endo_nbr;i++)
First_IM->IM[i] = 0; First_IM->IM[i] = 0;
First_IM->lead_lag = 0; First_IM->lead_lag = 0;
First_IM->pNext = NULL; First_IM->pNext = NULL;
Last_IM = First_IM; Last_IM = First_IM;
//cout << "init_incidence_matrix done \n";
First_IM_X = new List_IM;
First_IM_X->IM = (bool*)malloc(symbol_table.exo_nbr * symbol_table.endo_nbr * sizeof(First_IM_X->IM[0]));
for(i = 0;i < symbol_table.endo_nbr*symbol_table.exo_nbr;i++)
First_IM_X->IM[i] = 0;
First_IM_X->lead_lag = 0;
First_IM_X->pNext = NULL;
Last_IM_X = First_IM_X;
} }
void void
BlockTriangular::Free_IM(List_IM* First_IM) const IncidenceMatrix::Free_IM() const
{ {
#ifdef DEBUG
cout << "Free_IM\n";
#endif
List_IM *Cur_IM, *SFirst_IM; List_IM *Cur_IM, *SFirst_IM;
Cur_IM = SFirst_IM = First_IM; Cur_IM = SFirst_IM = First_IM;
while(Cur_IM) while(Cur_IM)
{ {
First_IM = Cur_IM->pNext; SFirst_IM = Cur_IM->pNext;
free(Cur_IM->IM); free(Cur_IM->IM);
delete Cur_IM; delete Cur_IM;
Cur_IM = First_IM; Cur_IM = SFirst_IM;
}
Cur_IM = SFirst_IM = First_IM_X;
while(Cur_IM)
{
SFirst_IM = Cur_IM->pNext;
free(Cur_IM->IM);
delete Cur_IM;
Cur_IM = SFirst_IM;
} }
//free(SFirst_IM);
//delete SFirst_IM;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Return the inceidence matrix related to a lead or a lag // Return the incidence matrix related to a lead or a lag
List_IM* List_IM*
BlockTriangular::Get_IM(int lead_lag) IncidenceMatrix::Get_IM(int lead_lag, SymbolType type) const
{ {
List_IM* Cur_IM; List_IM* Cur_IM;
Cur_IM = First_IM; if(type==eEndogenous)
Cur_IM = First_IM;
else
Cur_IM = First_IM_X;
while ((Cur_IM != NULL) && (Cur_IM->lead_lag != lead_lag)) while ((Cur_IM != NULL) && (Cur_IM->lead_lag != lead_lag))
Cur_IM = Cur_IM->pNext; Cur_IM = Cur_IM->pNext;
return (Cur_IM); return (Cur_IM);
} }
bool* bool*
BlockTriangular::bGet_IM(int lead_lag) const IncidenceMatrix::bGet_IM(int lead_lag, SymbolType type) const
{ {
List_IM* Cur_IM; List_IM* Cur_IM;
Cur_IM = First_IM; if(type==eEndogenous)
Cur_IM = First_IM;
else
Cur_IM = First_IM_X;
while ((Cur_IM != NULL) && (Cur_IM->lead_lag != lead_lag)) while ((Cur_IM != NULL) && (Cur_IM->lead_lag != lead_lag))
{ {
Cur_IM = Cur_IM->pNext; Cur_IM = Cur_IM->pNext;
} }
if((Cur_IM->lead_lag != lead_lag) || (Cur_IM==NULL)) if(Cur_IM)
{ return (Cur_IM->IM);
cout << "the incidence matrix with lag " << lead_lag << " does not exist !!"; else
exit(EXIT_FAILURE); return(0);
}
return (Cur_IM->IM);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Fill the incidence matrix related to a lead or a lag // Fill the incidence matrix related to a lead or a lag
void void
BlockTriangular::fill_IM(int equation, int variable_endo, int lead_lag) IncidenceMatrix::fill_IM(int equation, int variable, int lead_lag, SymbolType type)
{ {
List_IM* Cur_IM; List_IM* Cur_IM;
//cout << "equation=" << equation << " variable_endo=" << variable_endo << " lead_lag=" << lead_lag << "\n"; Cur_IM = Get_IM(lead_lag, type);
Cur_IM = Get_IM(lead_lag); if(equation >= symbol_table.endo_nbr)
if(equation >= endo_nbr)
{ {
cout << "Error : The model has more equations (at least " << equation + 1 << ") than declared endogenous variables (" << endo_nbr << ")\n"; cout << "Error : The model has more equations (at least " << equation + 1 << ") than declared endogenous variables (" << symbol_table.endo_nbr << ")\n";
system("PAUSE");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (!Cur_IM) if (!Cur_IM)
Cur_IM = Build_IM(lead_lag); Cur_IM = Build_IM(lead_lag, type);
Cur_IM->IM[equation*endo_nbr + variable_endo] = 1; if(type==eEndogenous)
Cur_IM->IM[equation*symbol_table.endo_nbr + variable] = 1;
else
Cur_IM->IM[equation*symbol_table.exo_nbr + variable] = 1;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// unFill the incidence matrix related to a lead or a lag // unFill the incidence matrix related to a lead or a lag
void void
BlockTriangular::unfill_IM(int equation, int variable_endo, int lead_lag) IncidenceMatrix::unfill_IM(int equation, int variable, int lead_lag, SymbolType type)
{ {
List_IM* Cur_IM; List_IM* Cur_IM;
//cout << "lead_lag=" << lead_lag << "\n"; Cur_IM = Get_IM(lead_lag, type);
Cur_IM = Get_IM(lead_lag);
/*if(equation >= endo_nbr)
{
cout << "Error : The model has more equations (at least " << equation + 1 << ") than declared endogenous variables (" << endo_nbr << ")\n";
system("PAUSE");
exit(EXIT_FAILURE);
}*/
if (!Cur_IM) if (!Cur_IM)
Cur_IM = Build_IM(lead_lag); Cur_IM = Build_IM(lead_lag, type);
Cur_IM->IM[equation*endo_nbr + variable_endo] = 0; if(type==eEndogenous)
/*system("pause");*/ Cur_IM->IM[equation*symbol_table.endo_nbr + variable] = 0;
else
Cur_IM->IM[equation*symbol_table.exo_nbr + variable] = 0;
}
List_IM*
IncidenceMatrix::Get_First(SymbolType type) const
{
if(type==eEndogenous)
return(First_IM);
else
return(First_IM_X);
} }
//------------------------------------------------------------------------------
//For a lead or a lag build the Incidence Matrix structures
/*List_IM*
IncidenceMatrix::Build_IM_X(int lead_lag)
{
List_IM* pIM_X = new List_IM;
int i;
Last_IM_X->pNext = pIM_X;
pIM_X->IM = (bool*)malloc(exo_nbr * endo_nbr * sizeof(pIM_X->IM[0]));
for(i = 0;i < exo_nbr*endo_nbr;i++)
pIM_X->IM[i] = 0;
pIM_X->lead_lag = lead_lag;
if(lead_lag > 0)
{
if(lead_lag > Model_Max_Lead_Exo)
{
Model_Max_Lead_Exo = lead_lag;
if(lead_lag > Model_Max_Lead)
Model_Max_Lead = lead_lag;
}
}
else
{
if( -lead_lag > Model_Max_Lag_Exo)
{
Model_Max_Lag_Exo = -lead_lag;
if(-lead_lag > Model_Max_Lag)
Model_Max_Lag = -lead_lag;
}
}
pIM_X->pNext = NULL;
Last_IM_X = pIM_X;
return (pIM_X);
}
//------------------------------------------------------------------------------
// initialize all the incidence matrix structures
void
IncidenceMatrix::init_incidence_matrix_X(int nb_exo)
{
int i;
//cout << "init_incidence_matrix_X nb_exo = " << nb_exo << " endo_nbr=" << endo_nbr << "\n";
exo_nbr = nb_exo;
First_IM_X = new List_IM;
First_IM_X->IM = (bool*)malloc(nb_exo * endo_nbr * sizeof(First_IM_X->IM[0]));
for(i = 0;i < endo_nbr*nb_exo;i++)
First_IM_X->IM[i] = 0;
First_IM_X->lead_lag = 0;
First_IM_X->pNext = NULL;
Last_IM_X = First_IM_X;
}
void
IncidenceMatrix::Free_IM_X(List_IM* First_IM_X) const
{
List_IM *Cur_IM_X, *SFirst_IM_X;
Cur_IM_X = SFirst_IM_X = First_IM_X;
while(Cur_IM_X)
{
First_IM_X = Cur_IM_X->pNext;
free(Cur_IM_X->IM);
delete Cur_IM_X;
Cur_IM_X = First_IM_X;
}
}
//------------------------------------------------------------------------------
// Return the inceidence matrix related to a lead or a lag
List_IM*
IncidenceMatrix::Get_IM_X(int lead_lag)
{
List_IM* Cur_IM_X;
Cur_IM_X = First_IM_X;
while ((Cur_IM_X != NULL) && (Cur_IM_X->lead_lag != lead_lag))
Cur_IM_X = Cur_IM_X->pNext;
return (Cur_IM_X);
}
bool*
IncidenceMatrix::bGet_IM_X(int lead_lag) const
{
List_IM* Cur_IM_X;
Cur_IM_X = First_IM_X;
while ((Cur_IM_X != NULL) && (Cur_IM_X->lead_lag != lead_lag))
{
Cur_IM_X = Cur_IM_X->pNext;
}
if(Cur_IM_X)
return (Cur_IM_X->IM);
else
return(0);
}
//------------------------------------------------------------------------------
// Fill the incidence matrix related to a lead or a lag
void
IncidenceMatrix::fill_IM_X(int equation, int variable_exo, int lead_lag)
{
List_IM* Cur_IM_X;
Cur_IM_X = Get_IM_X(lead_lag);
if(equation >= endo_nbr)
{
cout << "Error : The model has more equations (at least " << equation + 1 << ") than declared endogenous variables (" << endo_nbr << ")\n";
exit(EXIT_FAILURE);
}
if (!Cur_IM_X)
{
Cur_IM_X = Build_IM_X(lead_lag);
}
Cur_IM_X->IM[equation*exo_nbr + variable_exo] = true;
}
*/
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
//Print azn incidence matrix //Print azn incidence matrix
void void
BlockTriangular::Print_SIM(bool* IM, int n) const IncidenceMatrix::Print_SIM(bool* IM, SymbolType type) const
{ {
int i, j; int i, j, n;
for(i = 0;i < n;i++) if(type == eEndogenous)
n = symbol_table.endo_nbr;
else
n = symbol_table.exo_nbr;
for(i = 0;i < symbol_table.endo_nbr;i++)
{ {
cout << " "; cout << " ";
for(j = 0;j < n;j++) for(j = 0;j < n;j++)
...@@ -194,15 +382,18 @@ BlockTriangular::Print_SIM(bool* IM, int n) const ...@@ -194,15 +382,18 @@ BlockTriangular::Print_SIM(bool* IM, int n) const
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
//Print all incidence matrix //Print all incidence matrix
void void
BlockTriangular::Print_IM(int n) const IncidenceMatrix::Print_IM(SymbolType type) const
{ {
List_IM* Cur_IM; List_IM* Cur_IM;
Cur_IM = First_IM; if(type == eEndogenous)
Cur_IM = First_IM;
else
Cur_IM = First_IM_X;
cout << "-------------------------------------------------------------------\n"; cout << "-------------------------------------------------------------------\n";
while(Cur_IM) while(Cur_IM)
{ {
cout << "Incidence matrix for lead_lag = " << Cur_IM->lead_lag << "\n"; cout << "Incidence matrix for lead_lag = " << Cur_IM->lead_lag << "\n";
Print_SIM(Cur_IM->IM, n); Print_SIM(Cur_IM->IM, type);
Cur_IM = Cur_IM->pNext; Cur_IM = Cur_IM->pNext;
} }
} }
...@@ -211,7 +402,7 @@ BlockTriangular::Print_IM(int n) const ...@@ -211,7 +402,7 @@ BlockTriangular::Print_IM(int n) const
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Swap rows and columns of the incidence matrix // Swap rows and columns of the incidence matrix
void void
BlockTriangular::swap_IM_c(bool *SIM, int pos1, int pos2, int pos3, simple* Index_Var_IM, simple* Index_Equ_IM, int n) IncidenceMatrix::swap_IM_c(bool *SIM, int pos1, int pos2, int pos3, simple* Index_Var_IM, simple* Index_Equ_IM, int n) const
{ {
int tmp_i, j; int tmp_i, j;
bool tmp_b; bool tmp_b;
...@@ -269,15 +460,13 @@ BlockTriangular::Prologue_Epilogue(bool* IM, int* prologue, int* epilogue, int n ...@@ -269,15 +460,13 @@ BlockTriangular::Prologue_Epilogue(bool* IM, int* prologue, int* epilogue, int n
if ((k == 1) && IM0[Index_Equ_IM[i].index*n + Index_Var_IM[l].index]) if ((k == 1) && IM0[Index_Equ_IM[i].index*n + Index_Var_IM[l].index])
{ {
modifie = 1; modifie = 1;
swap_IM_c(IM, *prologue, i, l, Index_Var_IM, Index_Equ_IM, n); incidencematrix.swap_IM_c(IM, *prologue, i, l, Index_Var_IM, Index_Equ_IM, n);
(*prologue)++; (*prologue)++;
} }
} }
} }
*epilogue = 0; *epilogue = 0;
modifie = 1; modifie = 1;
/*print_SIM(IM,n);
print_SIM(IM*/
while(modifie) while(modifie)
{ {
modifie = 0; modifie = 0;
...@@ -295,7 +484,7 @@ BlockTriangular::Prologue_Epilogue(bool* IM, int* prologue, int* epilogue, int n ...@@ -295,7 +484,7 @@ BlockTriangular::Prologue_Epilogue(bool* IM, int* prologue, int* epilogue, int n
if ((k == 1) && IM0[Index_Equ_IM[l].index*n + Index_Var_IM[i].index]) if ((k == 1) && IM0[Index_Equ_IM[l].index*n + Index_Var_IM[i].index])
{ {
modifie = 1; modifie = 1;
swap_IM_c(IM, n - (1 + *epilogue), l, i, Index_Var_IM, Index_Equ_IM, n); incidencematrix.swap_IM_c(IM, n - (1 + *epilogue), l, i, Index_Var_IM, Index_Equ_IM, n);
(*epilogue)++; (*epilogue)++;
} }
} }
...@@ -306,11 +495,12 @@ BlockTriangular::Prologue_Epilogue(bool* IM, int* prologue, int* epilogue, int n ...@@ -306,11 +495,12 @@ BlockTriangular::Prologue_Epilogue(bool* IM, int* prologue, int* epilogue, int n
void void
BlockTriangular::Allocate_Block(int size, int *count_Equ, int *count_Block, BlockType type, Model_Block * ModelBlock) BlockTriangular::Allocate_Block(int size, int *count_Equ, int *count_Block, BlockType 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, first_count_equ, i1;
int *list_lead_var, *tmp_size, *tmp_var, *tmp_endo, nb_lead_lag_endo; int *tmp_size, *tmp_size_exo, *tmp_var, *tmp_endo, *tmp_exo, tmp_nb_exo, nb_lead_lag_endo;
List_IM *Cur_IM; List_IM *Cur_IM;
bool *IM, OK; bool *IM, OK;
ModelBlock->Periods = periods; ModelBlock->Periods = periods;
int Lag_Endo, Lead_Endo, Lag_Exo, Lead_Exo;
if ((type == PROLOGUE) || (type == EPILOGUE)) if ((type == PROLOGUE) || (type == EPILOGUE))
{ {
for(i = 0;i < size;i++) for(i = 0;i < size;i++)
...@@ -321,128 +511,221 @@ BlockTriangular::Allocate_Block(int size, int *count_Equ, int *count_Block, Bloc ...@@ -321,128 +511,221 @@ BlockTriangular::Allocate_Block(int size, int *count_Equ, int *count_Block, Bloc
ModelBlock->Block_List[*count_Block].Simulation_Type = UNKNOWN; ModelBlock->Block_List[*count_Block].Simulation_Type = UNKNOWN;
ModelBlock->Block_List[*count_Block].Temporary_terms=new temporary_terms_type (); ModelBlock->Block_List[*count_Block].Temporary_terms=new temporary_terms_type ();
ModelBlock->Block_List[*count_Block].Temporary_terms->clear(); ModelBlock->Block_List[*count_Block].Temporary_terms->clear();
list_lead_var = (int*)malloc(Model_Max_Lead * endo_nbr * sizeof(int)); tmp_endo = (int*)malloc((incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1) * sizeof(int));
size_list_lead_var = 0; tmp_size = (int*)malloc((incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1) * sizeof(int));
tmp_endo = (int*)malloc((Model_Max_Lead + Model_Max_Lag + 1) * sizeof(int));
tmp_size = (int*)malloc((Model_Max_Lead + Model_Max_Lag + 1) * sizeof(int));
tmp_var = (int*)malloc(sizeof(int)); tmp_var = (int*)malloc(sizeof(int));
memset(tmp_size, 0, (Model_Max_Lead + Model_Max_Lag + 1)*sizeof(int)); tmp_size_exo = (int*)malloc((incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1) * sizeof(int));
memset(tmp_endo, 0, (Model_Max_Lead + Model_Max_Lag + 1)*sizeof(int)); memset(tmp_size_exo, 0, (incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1)*sizeof(int));
memset(tmp_size, 0, (incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1)*sizeof(int));
memset(tmp_endo, 0, (incidencematrix.Model_Max_Lead + incidencematrix.Model_Max_Lag + 1)*sizeof(int));
nb_lead_lag_endo = Lead = Lag = 0; nb_lead_lag_endo = Lead = Lag = 0;
Cur_IM = First_IM; Lag_Endo = Lead_Endo = Lag_Exo = Lead_Exo = 0;
Cur_IM = incidencematrix.Get_First(eEndogenous);
while(Cur_IM) while(Cur_IM)
{ {
k = Cur_IM->lead_lag; k = Cur_IM->lead_lag;
i_1 = Index_Equ_IM[*count_Equ].index * endo_nbr; i_1 = Index_Equ_IM[*count_Equ].index * symbol_table.endo_nbr;
if(k > 0) if(k > 0)
{ {
if(Cur_IM->IM[