From 1740884d1d480ff4fe05313d63285f369729b3a6 Mon Sep 17 00:00:00 2001 From: sebastien Date: Fri, 23 Jan 2009 10:59:37 +0000 Subject: [PATCH] trunk preprocessor: automatic reindentation of all code with emacs git-svn-id: https://www.dynare.org/svn/dynare/trunk@2372 ac1d8469-bf42-47a9-8791-bf33cf982152 --- BlockTriangular.cc | 96 +- ComputingTasks.cc | 2 +- DataTree.cc | 4 +- ExprNode.cc | 38 +- IncidenceMatrix.cc | 2 +- MatlabFile.cc | 310 +-- ModFile.cc | 6 +- ModelNormalization.cc | 8 +- ModelTree.cc | 5012 ++++++++++++++++++------------------ ParsingDriver.cc | 22 +- Shocks.cc | 12 +- include/CodeInterpreter.hh | 22 +- include/ComputingTasks.hh | 2 +- include/ExprNode.hh | 24 +- include/MatlabFile.hh | 196 +- include/ModelTree.hh | 2 +- include/ParsingDriver.hh | 2 +- include/SymbolTable.hh | 6 +- macro/MacroDriver.hh | 6 +- macro/MacroValue.hh | 8 +- 20 files changed, 2890 insertions(+), 2890 deletions(-) diff --git a/BlockTriangular.cc b/BlockTriangular.cc index 036ab9bf..f9968399 100644 --- a/BlockTriangular.cc +++ b/BlockTriangular.cc @@ -31,9 +31,9 @@ using namespace std; //------------------------------------------------------------------------------ BlockTriangular::BlockTriangular(const SymbolTable &symbol_table_arg) : - symbol_table(symbol_table_arg), - normalization(symbol_table_arg), - incidencematrix(symbol_table_arg) + symbol_table(symbol_table_arg), + normalization(symbol_table_arg), + incidencematrix(symbol_table_arg) { bt_verbose = 0; ModelBlock = NULL; @@ -418,52 +418,52 @@ BlockTriangular::Allocate_Block(int size, int *count_Equ, int count_Block, Block void BlockTriangular::Free_Block(Model_Block* ModelBlock) const - { - int blk, i; - for (blk = 0;blk < ModelBlock->Size;blk++) - { +{ + int blk, i; + for (blk = 0;blk < ModelBlock->Size;blk++) + { - free(ModelBlock->Block_List[blk].Equation); - free(ModelBlock->Block_List[blk].Variable); - free(ModelBlock->Block_List[blk].Exogenous); - free(ModelBlock->Block_List[blk].Own_Derivative); - free(ModelBlock->Block_List[blk].Other_Endogenous); - for (i = 0;i < ModelBlock->Block_List[blk].Max_Lag + ModelBlock->Block_List[blk].Max_Lead + 1;i++) - { - if (incidencematrix.Model_Max_Lag_Endo-ModelBlock->Block_List[blk].Max_Lag+i>=0 /*&& ModelBlock->Block_List[blk].IM_lead_lag[i].size*/) - { - free(ModelBlock->Block_List[blk].IM_lead_lag[i].u); - free(ModelBlock->Block_List[blk].IM_lead_lag[i].us); - free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ); - free(ModelBlock->Block_List[blk].IM_lead_lag[i].Var); - free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_Index); - free(ModelBlock->Block_List[blk].IM_lead_lag[i].Var_Index); - free(ModelBlock->Block_List[blk].IM_lead_lag[i].u_other_endo); - free(ModelBlock->Block_List[blk].IM_lead_lag[i].Var_other_endo); - free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_other_endo); - free(ModelBlock->Block_List[blk].IM_lead_lag[i].Var_Index_other_endo); - free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_Index_other_endo); - } - if (incidencematrix.Model_Max_Lag_Exo-ModelBlock->Block_List[blk].Max_Lag+i>=0 /*&& ModelBlock->Block_List[blk].IM_lead_lag[i].size_exo*/) - { - free(ModelBlock->Block_List[blk].IM_lead_lag[i].Exogenous); - free(ModelBlock->Block_List[blk].IM_lead_lag[i].Exogenous_Index); - free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_X_Index); - free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_X); - } - } - free(ModelBlock->Block_List[blk].IM_lead_lag); - for(i=0; iBlock_List[blk].Size; i++) - delete ModelBlock->Block_List[blk].Temporary_Terms_in_Equation[i]; - free(ModelBlock->Block_List[blk].Temporary_Terms_in_Equation); - delete(ModelBlock->Block_List[blk].Temporary_InUse); - } - free(ModelBlock->Block_List); - free(ModelBlock); - free(Index_Equ_IM); - free(Index_Var_IM); - } + free(ModelBlock->Block_List[blk].Equation); + free(ModelBlock->Block_List[blk].Variable); + free(ModelBlock->Block_List[blk].Exogenous); + free(ModelBlock->Block_List[blk].Own_Derivative); + free(ModelBlock->Block_List[blk].Other_Endogenous); + for (i = 0;i < ModelBlock->Block_List[blk].Max_Lag + ModelBlock->Block_List[blk].Max_Lead + 1;i++) + { + if (incidencematrix.Model_Max_Lag_Endo-ModelBlock->Block_List[blk].Max_Lag+i>=0 /*&& ModelBlock->Block_List[blk].IM_lead_lag[i].size*/) + { + free(ModelBlock->Block_List[blk].IM_lead_lag[i].u); + free(ModelBlock->Block_List[blk].IM_lead_lag[i].us); + free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ); + free(ModelBlock->Block_List[blk].IM_lead_lag[i].Var); + free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_Index); + free(ModelBlock->Block_List[blk].IM_lead_lag[i].Var_Index); + free(ModelBlock->Block_List[blk].IM_lead_lag[i].u_other_endo); + free(ModelBlock->Block_List[blk].IM_lead_lag[i].Var_other_endo); + free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_other_endo); + free(ModelBlock->Block_List[blk].IM_lead_lag[i].Var_Index_other_endo); + free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_Index_other_endo); + } + if (incidencematrix.Model_Max_Lag_Exo-ModelBlock->Block_List[blk].Max_Lag+i>=0 /*&& ModelBlock->Block_List[blk].IM_lead_lag[i].size_exo*/) + { + free(ModelBlock->Block_List[blk].IM_lead_lag[i].Exogenous); + free(ModelBlock->Block_List[blk].IM_lead_lag[i].Exogenous_Index); + free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_X_Index); + free(ModelBlock->Block_List[blk].IM_lead_lag[i].Equ_X); + } + } + free(ModelBlock->Block_List[blk].IM_lead_lag); + for(i=0; iBlock_List[blk].Size; i++) + delete ModelBlock->Block_List[blk].Temporary_Terms_in_Equation[i]; + free(ModelBlock->Block_List[blk].Temporary_Terms_in_Equation); + delete(ModelBlock->Block_List[blk].Temporary_InUse); + } + free(ModelBlock->Block_List); + free(ModelBlock); + free(Index_Equ_IM); + free(Index_Var_IM); +} t_type @@ -571,7 +571,7 @@ BlockTriangular::Reduce_Blocks_and_type_determination(int prologue, int epilogue { if ( ((prev_Type == EVALUATE_FORWARD_R || prev_Type == EVALUATE_FORWARD) && (Simulation_Type == EVALUATE_FORWARD_R || Simulation_Type == EVALUATE_FORWARD)) || ((prev_Type == EVALUATE_BACKWARD_R || prev_Type == EVALUATE_BACKWARD) && (Simulation_Type == EVALUATE_BACKWARD_R || Simulation_Type == EVALUATE_BACKWARD)) - ) + ) { BlockSimulationType c_Type = (Type[Type.size()-1]).first; int c_Size = (Type[Type.size()-1]).second; diff --git a/ComputingTasks.cc b/ComputingTasks.cc index b8b9b6a9..661ec6c5 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -177,7 +177,7 @@ StochSimulStatement::writeOutput(ostream &output, const string &basename) const } ForecastStatement::ForecastStatement(const SymbolList &symbol_list_arg, - const OptionsList &options_list_arg) : + const OptionsList &options_list_arg) : symbol_list(symbol_list_arg), options_list(options_list_arg) { diff --git a/DataTree.cc b/DataTree.cc index 7fd99f07..6c6405a8 100644 --- a/DataTree.cc +++ b/DataTree.cc @@ -376,13 +376,13 @@ DataTree::AddSqRt(NodeID iArg1) NodeID DataTree::AddMaX(NodeID iArg1, NodeID iArg2) { - return AddBinaryOp(iArg1, oMax, iArg2); + return AddBinaryOp(iArg1, oMax, iArg2); } NodeID DataTree::AddMin(NodeID iArg1, NodeID iArg2) { - return AddBinaryOp(iArg1, oMin, iArg2); + return AddBinaryOp(iArg1, oMin, iArg2); } NodeID diff --git a/ExprNode.cc b/ExprNode.cc index c22a1035..359ded42 100644 --- a/ExprNode.cc +++ b/ExprNode.cc @@ -480,12 +480,12 @@ VariableNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType ou void VariableNode::computeTemporaryTerms(map &reference_count, - temporary_terms_type &temporary_terms, - map > &first_occurence, - int Curr_block, - Model_Block *ModelBlock, - int equation, - map_idx_type &map_idx) const + temporary_terms_type &temporary_terms, + map > &first_occurence, + int Curr_block, + Model_Block *ModelBlock, + int equation, + map_idx_type &map_idx) const { if(type== eModelLocalVariable) datatree.local_variables_table[symb_id]->computeTemporaryTerms(reference_count, temporary_terms, first_occurence, Curr_block, ModelBlock, equation, map_idx); @@ -1080,7 +1080,7 @@ BinaryOpNode::cost(const temporary_terms_type &temporary_terms, bool is_matlab) return cost + 90; case oMax: case oMin: - return cost + 110; + return cost + 110; case oDivide: return cost + 990; case oPower: @@ -1278,7 +1278,7 @@ BinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type, if ((op_code == oPower && !OFFSET(output_type)) || op_code == oMax || op_code == oMin ) { switch (op_code) - { + { case oPower: output << "pow("; break; @@ -1290,7 +1290,7 @@ BinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type, break; default: ; - } + } arg1->writeOutput(output, output_type, temporary_terms); output << ","; arg2->writeOutput(output, output_type, temporary_terms); @@ -1404,7 +1404,7 @@ BinaryOpNode::collectExogenous(set > &result) const } TrinaryOpNode::TrinaryOpNode(DataTree &datatree_arg, const NodeID arg1_arg, - TrinaryOpcode op_code_arg, const NodeID arg2_arg, const NodeID arg3_arg) : + TrinaryOpcode op_code_arg, const NodeID arg2_arg, const NodeID arg3_arg) : ExprNode(datatree_arg), arg1(arg1_arg), arg2(arg2_arg), @@ -1531,8 +1531,8 @@ TrinaryOpNode::cost(const temporary_terms_type &temporary_terms, bool is_matlab) void TrinaryOpNode::computeTemporaryTerms(map &reference_count, - temporary_terms_type &temporary_terms, - bool is_matlab) const + temporary_terms_type &temporary_terms, + bool is_matlab) const { NodeID this2 = const_cast(this); map::iterator it = reference_count.find(this2); @@ -1557,12 +1557,12 @@ TrinaryOpNode::computeTemporaryTerms(map &reference_count, void TrinaryOpNode::computeTemporaryTerms(map &reference_count, - temporary_terms_type &temporary_terms, - map > &first_occurence, - int Curr_block, - Model_Block *ModelBlock, - int equation, - map_idx_type &map_idx) const + temporary_terms_type &temporary_terms, + map > &first_occurence, + int Curr_block, + Model_Block *ModelBlock, + int equation, + map_idx_type &map_idx) const { NodeID this2 = const_cast(this); map::iterator it = reference_count.find(this2); @@ -1646,7 +1646,7 @@ TrinaryOpNode::collectTemporary_terms(const temporary_terms_type &temporary_term void TrinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type, - const temporary_terms_type &temporary_terms) const + const temporary_terms_type &temporary_terms) const { if (!OFFSET(output_type)) { diff --git a/IncidenceMatrix.cc b/IncidenceMatrix.cc index 3839a1cc..bf660ddc 100644 --- a/IncidenceMatrix.cc +++ b/IncidenceMatrix.cc @@ -114,7 +114,7 @@ IncidenceMatrix::Get_IM(int lead_lag, SymbolType type) const } else //eExogenous { - it = List_IM_X.find(lead_lag); + it = List_IM_X.find(lead_lag); if(it!=List_IM_X.end()) return(it->second); else diff --git a/MatlabFile.cc b/MatlabFile.cc index d5ba2d21..76c1fe76 100644 --- a/MatlabFile.cc +++ b/MatlabFile.cc @@ -18,10 +18,10 @@ */ /* -Useful documentation: Matlab 7 Mat-File Format ------------------------------------------------ -revision: October 2008 PDF only Rereleased for Version 7.7 (Release 2008b) -available at: http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/matfile_format.pdf + Useful documentation: Matlab 7 Mat-File Format + ----------------------------------------------- + revision: October 2008 PDF only Rereleased for Version 7.7 (Release 2008b) + available at: http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/matfile_format.pdf */ #include "MatlabFile.hh" @@ -183,69 +183,69 @@ SimpleElem::Get_Data_Class(Data_Header_t data_header) const returned_ReadData_t ret; switch(data_header.DataType) { - case miINT8: //8 bit, signed - case miUINT8: //8 bit, unsigned - ret.Type = Numerical; - ret.Simple = new INT8; - return(ret); - break; - case miINT16: //16-bit, signed - case miUINT16: //16-bit, unsigned - ret.Type = Numerical; - ret.Simple = new INT16; - return(ret); - break; - case miINT32: //32-bit, signed - case miUINT32: //32-bit, unsigned - ret.Type = Numerical; - ret.Simple = new INT32; - return(ret); - break; - case miINT64: //64-bit, signed - case miUINT64: //64-bit, unsigned - ret.Type = Numerical; - ret.Simple = new INT64; - return(ret); - break; - case miSINGLE: //IEEEŽ 754 single format - ret.Type = Numerical; - ret.Simple = new Single; - return(ret); - break; - case miDOUBLE: //IEEE 754 double format - ret.Type = Numerical; - ret.Simple = new Double; - return(ret); - break; - case miMATRIX: //MATLAB array - ret.Type = Matrix; - ret.Simple = NULL; - return(ret); - break; - case miCOMPRESSED: //Compressed Data - ret.Type = Compressed; - ret.Simple = NULL; - return(ret); - break; - case miUTF8: //Unicode UTF-8 Encoded Character Data - ret.Type = AlphaNumeric; - ret.Simple = new UTF8; - return(ret); - break; - case miUTF16: //Unicode UTF-16 Encoded Character Data - ret.Type = AlphaNumeric; - ret.Simple = new UTF16; - return(ret); - break; - case miUTF32: //Unicode UTF-32 Encoded Character Data - ret.Type = AlphaNumeric; - ret.Simple = new UTF32; - return(ret); - break; - default: - ret.Type = Unknown; - ret.Simple = NULL; - return(ret); + case miINT8: //8 bit, signed + case miUINT8: //8 bit, unsigned + ret.Type = Numerical; + ret.Simple = new INT8; + return(ret); + break; + case miINT16: //16-bit, signed + case miUINT16: //16-bit, unsigned + ret.Type = Numerical; + ret.Simple = new INT16; + return(ret); + break; + case miINT32: //32-bit, signed + case miUINT32: //32-bit, unsigned + ret.Type = Numerical; + ret.Simple = new INT32; + return(ret); + break; + case miINT64: //64-bit, signed + case miUINT64: //64-bit, unsigned + ret.Type = Numerical; + ret.Simple = new INT64; + return(ret); + break; + case miSINGLE: //IEEEŽ 754 single format + ret.Type = Numerical; + ret.Simple = new Single; + return(ret); + break; + case miDOUBLE: //IEEE 754 double format + ret.Type = Numerical; + ret.Simple = new Double; + return(ret); + break; + case miMATRIX: //MATLAB array + ret.Type = Matrix; + ret.Simple = NULL; + return(ret); + break; + case miCOMPRESSED: //Compressed Data + ret.Type = Compressed; + ret.Simple = NULL; + return(ret); + break; + case miUTF8: //Unicode UTF-8 Encoded Character Data + ret.Type = AlphaNumeric; + ret.Simple = new UTF8; + return(ret); + break; + case miUTF16: //Unicode UTF-16 Encoded Character Data + ret.Type = AlphaNumeric; + ret.Simple = new UTF16; + return(ret); + break; + case miUTF32: //Unicode UTF-32 Encoded Character Data + ret.Type = AlphaNumeric; + ret.Simple = new UTF32; + return(ret); + break; + default: + ret.Type = Unknown; + ret.Simple = NULL; + return(ret); } } @@ -259,36 +259,36 @@ SimpleElem::DataProceed(Data_Header data_header, char* InBuff, int* pBuff, FlagS double tmpv; switch(ret.Type) { - case Numerical: - if(data_header.Number_of_Bytes/ret.Simple->size()) - for(unsigned int i=0;isize();i++) - { - tmpv=ret.Simple->ReadNum(InBuff, pBuff); - VNumeric.push_back(tmpv); - } - //to align pBuff on a 4 Bytes - if(*pBuff % 4) - *pBuff += 4-(*pBuff % 4); - delete ret.Simple; - break; - case AlphaNumeric: + case Numerical: + if(data_header.Number_of_Bytes/ret.Simple->size()) for(unsigned int i=0;isize();i++) - Vstr.push_back(ret.Simple->ReadAlph(InBuff, pBuff, data_header.Number_of_Bytes)); - //to align pBuff on a 4 Bytes - if(*pBuff % 4) - *pBuff += 4-(*pBuff % 4); - delete ret.Simple; - break; - case Matrix: - array_elem = matrix.ReadArray_class(InBuff, pBuff); - array_elem->ReadArray(InBuff, pBuff, flag); - break; - case Compressed: - cerr << "Error: Compressed data in Mat-file not implemnted yet!\n set option -v6 when saving to Mat-file\n"; - exit(EXIT_FAILURE); - case Unknown: - cerr << "Error: Mat-file format use incomptible format with Matlab 7 specification!\n set option -v6 when saving to Mat-file\n"; - exit(EXIT_FAILURE); + { + tmpv=ret.Simple->ReadNum(InBuff, pBuff); + VNumeric.push_back(tmpv); + } + //to align pBuff on a 4 Bytes + if(*pBuff % 4) + *pBuff += 4-(*pBuff % 4); + delete ret.Simple; + break; + case AlphaNumeric: + for(unsigned int i=0;isize();i++) + Vstr.push_back(ret.Simple->ReadAlph(InBuff, pBuff, data_header.Number_of_Bytes)); + //to align pBuff on a 4 Bytes + if(*pBuff % 4) + *pBuff += 4-(*pBuff % 4); + delete ret.Simple; + break; + case Matrix: + array_elem = matrix.ReadArray_class(InBuff, pBuff); + array_elem->ReadArray(InBuff, pBuff, flag); + break; + case Compressed: + cerr << "Error: Compressed data in Mat-file not implemnted yet!\n set option -v6 when saving to Mat-file\n"; + exit(EXIT_FAILURE); + case Unknown: + cerr << "Error: Mat-file format use incomptible format with Matlab 7 specification!\n set option -v6 when saving to Mat-file\n"; + exit(EXIT_FAILURE); } } @@ -319,7 +319,7 @@ SimpleElem::Collect(const string &name, bool found, CollectStruct &collect_struc { array_elem->Collect(name, found, collect_struct); } - } + } } @@ -438,47 +438,47 @@ ArrayElem::ReadArray_class(char* InBuff, int* pBuff) const array_flag=array_elem.ReadArrayFlag(InBuff, pBuff); switch(array_flag.classe) { - case Cell_array: - return(new CellArray); - break; - case Structure_: - return(new Structure); - break; - case Object_: - return(new Object); - break; - case Character_array: - return(new CharacterArray); - break; - case Sparse_array: - return(new SparseArray); - break; - case Double_precision_array: - return(new DoublePrecisionArray); - break; - case Single_precision_array: - return(new SinglePrecisionArray); - break; - case Signed_integer_8_bit: - return(new Bit8SignedInteger); - break; - case Unsigned_integer_8_bit: - return(new Bit8UnsignedInteger); - break; - case Signed_integer_16_bit: - return(new Bit16SignedInteger); - break; - case Unsigned_integer_16_bit: - return(new Bit16UnsignedInteger); - break; - case Signed_integer_32_bit: - return(new Bit32SignedInteger); - break; - case Unsigned_integer_32_bit: - return(new Bit32UnsignedInteger); - break; - default: - return(NULL); + case Cell_array: + return(new CellArray); + break; + case Structure_: + return(new Structure); + break; + case Object_: + return(new Object); + break; + case Character_array: + return(new CharacterArray); + break; + case Sparse_array: + return(new SparseArray); + break; + case Double_precision_array: + return(new DoublePrecisionArray); + break; + case Single_precision_array: + return(new SinglePrecisionArray); + break; + case Signed_integer_8_bit: + return(new Bit8SignedInteger); + break; + case Unsigned_integer_8_bit: + return(new Bit8UnsignedInteger); + break; + case Signed_integer_16_bit: + return(new Bit16SignedInteger); + break; + case Unsigned_integer_16_bit: + return(new Bit16UnsignedInteger); + break; + case Signed_integer_32_bit: + return(new Bit32SignedInteger); + break; + case Unsigned_integer_32_bit: + return(new Bit32UnsignedInteger); + break; + default: + return(NULL); } return(NULL); } @@ -1024,7 +1024,7 @@ bool MatlabFile::Collect(const string &name, CollectStruct &collect_struct) const { for(vector::const_iterator it=VSimpl.begin(); it!=VSimpl.end(); it++) - (*it)->Collect(name, false, collect_struct); + (*it)->Collect(name, false, collect_struct); return(!(collect_struct.variable_double_name.empty() and collect_struct.variable_string_name.empty())); } @@ -1033,7 +1033,7 @@ void MatlabFile::MatFilePrint() { for(vector::iterator it=VSimpl.begin(); it!=VSimpl.end(); it++) - (*it)->Print(); + (*it)->Print(); } @@ -1048,9 +1048,9 @@ MatlabFile::Delete() } /* -int -main(int argc, char** argv) -{ + int + main(int argc, char** argv) + { CollectStruct collect_struct; MatlabFile matlab_file; matlab_file.MatFileRead("gimf_steady.mat"); @@ -1060,18 +1060,18 @@ main(int argc, char** argv) //string tmp_s("stored_values"); tmp_b=matlab_file.Collect("stored_values", collect_struct); if(tmp_b) - { - int i=0; - for(map >::iterator it2=collect_struct.variable_double_name.begin();it2!=collect_struct.variable_double_name.end();it2++) - { - i++; - cout << i << " " << it2->first.c_str() << " : "; - for(vector::iterator it=it2->second.begin();it!=it2->second.end();it++) - cout << *it; - cout << "\n"; - } - } -} + { + int i=0; + for(map >::iterator it2=collect_struct.variable_double_name.begin();it2!=collect_struct.variable_double_name.end();it2++) + { + i++; + cout << i << " " << it2->first.c_str() << " : "; + for(vector::iterator it=it2->second.begin();it!=it2->second.end();it++) + cout << *it; + cout << "\n"; + } + } + } */ diff --git a/ModFile.cc b/ModFile.cc index 0d488733..1a72b23a 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -77,9 +77,9 @@ ModFile::evalAllExpressions() j++; } catch(ExprNode::EvalException &e) - { - cout << "error in evaluation of param\n"; - } + { + cout << "error in evaluation of param\n"; + } } } if(mod_file_struct.load_params_and_steady_state_present && j!=symbol_table.parameter_nbr) diff --git a/ModelNormalization.cc b/ModelNormalization.cc index 039fd2cf..4a3dda7d 100644 --- a/ModelNormalization.cc +++ b/ModelNormalization.cc @@ -552,9 +552,9 @@ Normalization::Normalize(int n, int prologue, int epilogue, bool* IM, simple* In #ifdef DEBUG cout << "end of Normalize\n"; #endif - if(matchingSize < effective_n ) - return(0); - else - return(1); + if(matchingSize < effective_n ) + return(0); + else + return(1); } diff --git a/ModelTree.cc b/ModelTree.cc index 38401703..9b91c98b 100644 --- a/ModelTree.cc +++ b/ModelTree.cc @@ -39,48 +39,48 @@ ModelTree::ModelTree(SymbolTable &symbol_table_arg, NumericalConstants &num_constants_arg) : - DataTree(symbol_table_arg, num_constants_arg), - mode(eStandardMode), - cutoff(1e-15), - markowitz(0.7), - new_SGE(true), - computeJacobian(false), - computeJacobianExo(false), - computeHessian(false), - computeStaticHessian(false), - computeThirdDerivatives(false), - block_triangular(symbol_table_arg) + DataTree(symbol_table_arg, num_constants_arg), + mode(eStandardMode), + cutoff(1e-15), + markowitz(0.7), + new_SGE(true), + computeJacobian(false), + computeJacobianExo(false), + computeHessian(false), + computeStaticHessian(false), + computeThirdDerivatives(false), + block_triangular(symbol_table_arg) { } int ModelTree::equation_number() const - { - return(equations.size()); - } +{ + return(equations.size()); +} void ModelTree::writeDerivative(ostream &output, int eq, int symb_id, int lag, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, SymbolType type) const - { - first_derivatives_type::const_iterator it = first_derivatives.find(make_pair(eq, variable_table.getID(type, symb_id, lag))); - if (it != first_derivatives.end()) - (it->second)->writeOutput(output, output_type, temporary_terms); - else - output << 0; - } +{ + first_derivatives_type::const_iterator it = first_derivatives.find(make_pair(eq, variable_table.getID(type, symb_id, lag))); + if (it != first_derivatives.end()) + (it->second)->writeOutput(output, output_type, temporary_terms); + else + output << 0; +} void 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.getID(eEndogenous, symb_id, lag))); - if (it != first_derivatives.end()) - (it->second)->compile(code_file,false, output_type, temporary_terms, map_idx); - else - code_file.write(&FLDZ, sizeof(FLDZ)); - } +{ + 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)->compile(code_file,false, output_type, temporary_terms, map_idx); + else + code_file.write(&FLDZ, sizeof(FLDZ)); +} void @@ -177,52 +177,52 @@ ModelTree::computeTemporaryTerms(int order) void ModelTree::writeTemporaryTerms(ostream &output, ExprNodeOutputType output_type) const - { - // A copy of temporary terms - temporary_terms_type tt2; +{ + // A copy of temporary terms + temporary_terms_type tt2; - if (temporary_terms.size() > 0 && (!OFFSET(output_type))) - output << "double\n"; + if (temporary_terms.size() > 0 && (!OFFSET(output_type))) + output << "double\n"; - for (temporary_terms_type::const_iterator it = temporary_terms.begin(); - it != temporary_terms.end(); it++) - { - if (!OFFSET(output_type) && it != temporary_terms.begin()) - output << "," << endl; + for (temporary_terms_type::const_iterator it = temporary_terms.begin(); + it != temporary_terms.end(); it++) + { + if (!OFFSET(output_type) && it != temporary_terms.begin()) + output << "," << endl; - (*it)->writeOutput(output, output_type, temporary_terms); - output << " = "; + (*it)->writeOutput(output, output_type, temporary_terms); + output << " = "; - (*it)->writeOutput(output, output_type, tt2); + (*it)->writeOutput(output, output_type, tt2); - // Insert current node into tt2 - tt2.insert(*it); + // Insert current node into tt2 + tt2.insert(*it); - if (OFFSET(output_type)) - output << ";" << endl; - } - if (!OFFSET(output_type)) - output << ";" << endl; - } + if (OFFSET(output_type)) + output << ";" << endl; + } + if (!OFFSET(output_type)) + output << ";" << endl; +} void ModelTree::writeModelLocalVariables(ostream &output, ExprNodeOutputType output_type) const - { - for (map::const_iterator it = local_variables_table.begin(); - it != local_variables_table.end(); it++) - { - int id = it->first; - NodeID value = it->second; +{ + for (map::const_iterator it = local_variables_table.begin(); + it != local_variables_table.end(); it++) + { + int id = it->first; + NodeID value = it->second; - if (!OFFSET(output_type)) - output << "double "; + if (!OFFSET(output_type)) + output << "double "; - output << symbol_table.getNameByID(eModelLocalVariable, id) << " = "; - // Use an empty set for the temporary terms - value->writeOutput(output, output_type, temporary_terms_type()); - output << ";" << endl; - } - } + output << symbol_table.getNameByID(eModelLocalVariable, id) << " = "; + // Use an empty set for the temporary terms + value->writeOutput(output, output_type, temporary_terms_type()); + output << ";" << endl; + } +} void @@ -255,24 +255,24 @@ ModelTree::BuildIncidenceMatrix() void ModelTree::writeModelEquations(ostream &output, ExprNodeOutputType output_type) const - { - for (int eq = 0; eq < (int) equations.size(); eq++) - { - BinaryOpNode *eq_node = equations[eq]; +{ + for (int eq = 0; eq < (int) equations.size(); eq++) + { + BinaryOpNode *eq_node = equations[eq]; - NodeID lhs = eq_node->arg1; - output << "lhs ="; - lhs->writeOutput(output, output_type, temporary_terms); - output << ";" << endl; + NodeID lhs = eq_node->arg1; + output << "lhs ="; + lhs->writeOutput(output, output_type, temporary_terms); + output << ";" << endl; - NodeID rhs = eq_node->arg2; - output << "rhs ="; - rhs->writeOutput(output, output_type, temporary_terms); - output << ";" << endl; + NodeID rhs = eq_node->arg2; + output << "rhs ="; + rhs->writeOutput(output, output_type, temporary_terms); + output << ";" << endl; - output << "residual" << LPAR(output_type) << eq + OFFSET(output_type) << RPAR(output_type) << "= lhs-rhs;" << endl; - } - } + output << "residual" << LPAR(output_type) << eq + OFFSET(output_type) << RPAR(output_type) << "= lhs-rhs;" << endl; + } +} void ModelTree::computeTemporaryTermsOrdered(int order, Model_Block *ModelBlock) @@ -389,1298 +389,2096 @@ ModelTree::computeTemporaryTermsOrdered(int order, Model_Block *ModelBlock) void ModelTree::writeModelEquationsOrdered_M( Model_Block *ModelBlock, const string &dynamic_basename) const - { - int i,j,k,m; - string tmp_s, sps; - ostringstream tmp_output, tmp1_output, global_output; - NodeID lhs=NULL, rhs=NULL; - BinaryOpNode *eq_node; - ostringstream Uf[symbol_table.endo_nbr]; - map reference_count; - int prev_Simulation_Type=-1, count_derivates=0; - int jacobian_max_endo_col; - ofstream output; - temporary_terms_type::const_iterator it_temp=temporary_terms.begin(); - int nze, nze_exo, nze_other_endo; - //---------------------------------------------------------------------- - //For each block - for (j = 0;j < ModelBlock->Size;j++) - { - //For a block composed of a single equation determines wether we have to evaluate or to solve the equation - nze = nze_exo = nze_other_endo =0; - for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) - nze+=ModelBlock->Block_List[j].IM_lead_lag[m].size; - for (m=0;m<=ModelBlock->Block_List[j].Max_Lead_Exo+ModelBlock->Block_List[j].Max_Lag_Exo;m++) - nze_exo+=ModelBlock->Block_List[j].IM_lead_lag[m].size_exo; - for (m=0;m<=ModelBlock->Block_List[j].Max_Lead_Other_Endo+ModelBlock->Block_List[j].Max_Lag_Other_Endo;m++) - nze_other_endo+=ModelBlock->Block_List[j].IM_lead_lag[m].size_other_endo; - tmp1_output.str(""); - tmp1_output << dynamic_basename << "_" << j+1 << ".m"; - output.open(tmp1_output.str().c_str(), ios::out | ios::binary); - output << "%\n"; - output << "% " << tmp1_output.str() << " : Computes dynamic model for Dynare\n"; - output << "%\n"; - output << "% Warning : this file is generated automatically by Dynare\n"; - output << "% from model file (.mod)\n\n"; - output << "%/\n"; - if (ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD - ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD - ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD_R - ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD_R) - { - output << "function [y, g1, g2, g3, varargout] = " << dynamic_basename << "_" << j+1 << "(y, x, params, jacobian_eval, y_kmin, periods)\n"; - } - else if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_FORWARD_COMPLETE - || ModelBlock->Block_List[j].Simulation_Type==SOLVE_BACKWARD_COMPLETE) - output << "function [residual, g1, g2, g3, varargout] = " << dynamic_basename << "_" << j+1 << "(y, x, params, it_, jacobian_eval)\n"; - else if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_BACKWARD_SIMPLE - || ModelBlock->Block_List[j].Simulation_Type==SOLVE_FORWARD_SIMPLE) - output << "function [residual, g1, g2, g3, varargout] = " << dynamic_basename << "_" << j+1 << "(y, x, params, it_, jacobian_eval)\n"; - else - output << "function [residual, g1, g2, g3, b, varargout] = " << dynamic_basename << "_" << j+1 << "(y, x, params, periods, jacobian_eval, y_kmin, y_size)\n"; - output << " % ////////////////////////////////////////////////////////////////////////" << endl - << " % //" << string(" Block ").substr(int(log10(j + 1))) << j + 1 << " " << BlockTriangular::BlockType0(ModelBlock->Block_List[j].Type) - << " //" << endl - << " % // Simulation type " - << BlockTriangular::BlockSim(ModelBlock->Block_List[j].Simulation_Type) << " //" << endl - << " % ////////////////////////////////////////////////////////////////////////" << endl; - //The Temporary terms - if (ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD - ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD - ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD_R - ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD_R) - { - output << " if(jacobian_eval)\n"; - output << " g1 = spalloc(" << ModelBlock->Block_List[j].Size << ", " << ModelBlock->Block_List[j].Size*(1+ModelBlock->Block_List[j].Max_Lag_Endo+ModelBlock->Block_List[j].Max_Lead_Endo) << ", " << nze << ");\n"; - output << " g1_x=spalloc(" << ModelBlock->Block_List[j].Size << ", " << (ModelBlock->Block_List[j].nb_exo + ModelBlock->Block_List[j].nb_exo_det)*(1+ModelBlock->Block_List[j].Max_Lag_Exo+ModelBlock->Block_List[j].Max_Lead_Exo) << ", " << nze_exo << ");\n"; - output << " g1_o=spalloc(" << ModelBlock->Block_List[j].Size << ", " << ModelBlock->Block_List[j].nb_other_endo*(1+ModelBlock->Block_List[j].Max_Lag_Other_Endo+ModelBlock->Block_List[j].Max_Lead_Other_Endo) << ", " << nze_other_endo << ");\n"; - output << " end;\n"; - } - else - { - output << " if(jacobian_eval)\n"; - output << " g1 = spalloc(" << ModelBlock->Block_List[j].Size << ", " << ModelBlock->Block_List[j].Size*(1+ModelBlock->Block_List[j].Max_Lag_Endo+ModelBlock->Block_List[j].Max_Lead_Endo) << ", " << nze << ");\n"; - output << " g1_x=spalloc(" << ModelBlock->Block_List[j].Size << ", " << (ModelBlock->Block_List[j].nb_exo + ModelBlock->Block_List[j].nb_exo_det)*(1+ModelBlock->Block_List[j].Max_Lag_Exo+ModelBlock->Block_List[j].Max_Lead_Exo) << ", " << nze_exo << ");\n"; - output << " g1_o=spalloc(" << ModelBlock->Block_List[j].Size << ", " << ModelBlock->Block_List[j].nb_other_endo*(1+ModelBlock->Block_List[j].Max_Lag_Other_Endo+ModelBlock->Block_List[j].Max_Lead_Other_Endo) << ", " << nze_other_endo << ");\n"; - output << " else\n"; - if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE) - output << " g1 = spalloc(" << ModelBlock->Block_List[j].Size*ModelBlock->Periods << ", " << ModelBlock->Block_List[j].Size*(ModelBlock->Periods+ModelBlock->Block_List[j].Max_Lag+ModelBlock->Block_List[j].Max_Lead) << ", " << nze*ModelBlock->Periods << ");\n"; - else - output << " g1 = spalloc(" << ModelBlock->Block_List[j].Size << ", " << ModelBlock->Block_List[j].Size << ", " << nze << ");\n"; - output << " end;\n"; - } - - output << " g2=0;g3=0;\n"; - if(ModelBlock->Block_List[j].Temporary_InUse->size()) - { - tmp_output.str(""); - for (temporary_terms_inuse_type::const_iterator it = ModelBlock->Block_List[j].Temporary_InUse->begin(); - it != ModelBlock->Block_List[j].Temporary_InUse->end(); it++) - tmp_output << " T" << *it; - output << " global" << tmp_output.str() << ";\n"; - } - output << " residual=zeros(" << ModelBlock->Block_List[j].Size << ",1);\n"; - if (ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD - ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD - ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD_R - ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD_R) - output << " for it_ = y_kmin+1:(y_kmin+periods)\n"; - - - if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE) - { - output << " b = [];\n"; - output << " for it_ = y_kmin+1:(periods+y_kmin)\n"; - output << " Per_y_=it_*y_size;\n"; - output << " Per_J_=(it_-y_kmin-1)*y_size;\n"; - output << " Per_K_=(it_-1)*y_size;\n"; - sps=" "; - } - else - if(ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD || ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD || - ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD_R || ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD_R) - sps = " "; +{ + int i,j,k,m; + string tmp_s, sps; + ostringstream tmp_output, tmp1_output, global_output; + NodeID lhs=NULL, rhs=NULL; + BinaryOpNode *eq_node; + ostringstream Uf[symbol_table.endo_nbr]; + map reference_count; + int prev_Simulation_Type=-1, count_derivates=0; + int jacobian_max_endo_col; + ofstream output; + temporary_terms_type::const_iterator it_temp=temporary_terms.begin(); + int nze, nze_exo, nze_other_endo; + //---------------------------------------------------------------------- + //For each block + for (j = 0;j < ModelBlock->Size;j++) + { + //For a block composed of a single equation determines wether we have to evaluate or to solve the equation + nze = nze_exo = nze_other_endo =0; + for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) + nze+=ModelBlock->Block_List[j].IM_lead_lag[m].size; + for (m=0;m<=ModelBlock->Block_List[j].Max_Lead_Exo+ModelBlock->Block_List[j].Max_Lag_Exo;m++) + nze_exo+=ModelBlock->Block_List[j].IM_lead_lag[m].size_exo; + for (m=0;m<=ModelBlock->Block_List[j].Max_Lead_Other_Endo+ModelBlock->Block_List[j].Max_Lag_Other_Endo;m++) + nze_other_endo+=ModelBlock->Block_List[j].IM_lead_lag[m].size_other_endo; + tmp1_output.str(""); + tmp1_output << dynamic_basename << "_" << j+1 << ".m"; + output.open(tmp1_output.str().c_str(), ios::out | ios::binary); + output << "%\n"; + output << "% " << tmp1_output.str() << " : Computes dynamic model for Dynare\n"; + output << "%\n"; + output << "% Warning : this file is generated automatically by Dynare\n"; + output << "% from model file (.mod)\n\n"; + output << "%/\n"; + if (ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD + ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD + ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD_R + ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD_R) + { + output << "function [y, g1, g2, g3, varargout] = " << dynamic_basename << "_" << j+1 << "(y, x, params, jacobian_eval, y_kmin, periods)\n"; + } + else if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_FORWARD_COMPLETE + || ModelBlock->Block_List[j].Simulation_Type==SOLVE_BACKWARD_COMPLETE) + output << "function [residual, g1, g2, g3, varargout] = " << dynamic_basename << "_" << j+1 << "(y, x, params, it_, jacobian_eval)\n"; + else if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_BACKWARD_SIMPLE + || ModelBlock->Block_List[j].Simulation_Type==SOLVE_FORWARD_SIMPLE) + output << "function [residual, g1, g2, g3, varargout] = " << dynamic_basename << "_" << j+1 << "(y, x, params, it_, jacobian_eval)\n"; + else + output << "function [residual, g1, g2, g3, b, varargout] = " << dynamic_basename << "_" << j+1 << "(y, x, params, periods, jacobian_eval, y_kmin, y_size)\n"; + output << " % ////////////////////////////////////////////////////////////////////////" << endl + << " % //" << string(" Block ").substr(int(log10(j + 1))) << j + 1 << " " << BlockTriangular::BlockType0(ModelBlock->Block_List[j].Type) + << " //" << endl + << " % // Simulation type " + << BlockTriangular::BlockSim(ModelBlock->Block_List[j].Simulation_Type) << " //" << endl + << " % ////////////////////////////////////////////////////////////////////////" << endl; + //The Temporary terms + if (ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD + ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD + ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD_R + ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD_R) + { + output << " if(jacobian_eval)\n"; + output << " g1 = spalloc(" << ModelBlock->Block_List[j].Size << ", " << ModelBlock->Block_List[j].Size*(1+ModelBlock->Block_List[j].Max_Lag_Endo+ModelBlock->Block_List[j].Max_Lead_Endo) << ", " << nze << ");\n"; + output << " g1_x=spalloc(" << ModelBlock->Block_List[j].Size << ", " << (ModelBlock->Block_List[j].nb_exo + ModelBlock->Block_List[j].nb_exo_det)*(1+ModelBlock->Block_List[j].Max_Lag_Exo+ModelBlock->Block_List[j].Max_Lead_Exo) << ", " << nze_exo << ");\n"; + output << " g1_o=spalloc(" << ModelBlock->Block_List[j].Size << ", " << ModelBlock->Block_List[j].nb_other_endo*(1+ModelBlock->Block_List[j].Max_Lag_Other_Endo+ModelBlock->Block_List[j].Max_Lead_Other_Endo) << ", " << nze_other_endo << ");\n"; + output << " end;\n"; + } + else + { + output << " if(jacobian_eval)\n"; + output << " g1 = spalloc(" << ModelBlock->Block_List[j].Size << ", " << ModelBlock->Block_List[j].Size*(1+ModelBlock->Block_List[j].Max_Lag_Endo+ModelBlock->Block_List[j].Max_Lead_Endo) << ", " << nze << ");\n"; + output << " g1_x=spalloc(" << ModelBlock->Block_List[j].Size << ", " << (ModelBlock->Block_List[j].nb_exo + ModelBlock->Block_List[j].nb_exo_det)*(1+ModelBlock->Block_List[j].Max_Lag_Exo+ModelBlock->Block_List[j].Max_Lead_Exo) << ", " << nze_exo << ");\n"; + output << " g1_o=spalloc(" << ModelBlock->Block_List[j].Size << ", " << ModelBlock->Block_List[j].nb_other_endo*(1+ModelBlock->Block_List[j].Max_Lag_Other_Endo+ModelBlock->Block_List[j].Max_Lead_Other_Endo) << ", " << nze_other_endo << ");\n"; + output << " else\n"; + if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE) + output << " g1 = spalloc(" << ModelBlock->Block_List[j].Size*ModelBlock->Periods << ", " << ModelBlock->Block_List[j].Size*(ModelBlock->Periods+ModelBlock->Block_List[j].Max_Lag+ModelBlock->Block_List[j].Max_Lead) << ", " << nze*ModelBlock->Periods << ");\n"; else - sps=""; - // The equations - for (i = 0;i < ModelBlock->Block_List[j].Size;i++) - { - temporary_terms_type tt2; - tt2.clear(); - if (ModelBlock->Block_List[j].Temporary_Terms_in_Equation[i]->size()) - output << " " << sps << "% //Temporary variables" << endl; - for (temporary_terms_type::const_iterator it = ModelBlock->Block_List[j].Temporary_Terms_in_Equation[i]->begin(); - it != ModelBlock->Block_List[j].Temporary_Terms_in_Equation[i]->end(); it++) - { - output << " " << sps; - (*it)->writeOutput(output, oMatlabDynamicModelSparse, temporary_terms); - output << " = "; - (*it)->writeOutput(output, oMatlabDynamicModelSparse, tt2); - // Insert current node into tt2 - tt2.insert(*it); - output << ";" << endl; - } - string sModel = symbol_table.getNameByID(eEndogenous, ModelBlock->Block_List[j].Variable[i]) ; - eq_node = equations[ModelBlock->Block_List[j].Equation[i]]; - lhs = eq_node->arg1; - rhs = eq_node->arg2; - tmp_output.str(""); - lhs->writeOutput(tmp_output, oMatlabDynamicModelSparse, temporary_terms); - switch (ModelBlock->Block_List[j].Simulation_Type) - { - case EVALUATE_BACKWARD: - case EVALUATE_FORWARD: - output << " % equation " << ModelBlock->Block_List[j].Equation[i]+1 << " variable : " << sModel - << " (" << ModelBlock->Block_List[j].Variable[i]+1 << ")" << endl; - output << " "; - output << tmp_output.str(); - output << " = "; - rhs->writeOutput(output, oMatlabDynamicModelSparse, temporary_terms); - output << ";\n"; - break; - case EVALUATE_BACKWARD_R: - case EVALUATE_FORWARD_R: - output << " % equation " << ModelBlock->Block_List[j].Equation[i]+1 << " variable : " << sModel - << " (" << ModelBlock->Block_List[j].Variable[i]+1 << ")" << endl; - output << " "; - rhs->writeOutput(output, oMatlabDynamicModelSparse, temporary_terms); - output << " = "; - lhs->writeOutput(output, oMatlabDynamicModelSparse, temporary_terms); - output << ";\n"; - break; - case SOLVE_BACKWARD_SIMPLE: - case SOLVE_FORWARD_SIMPLE: - case SOLVE_BACKWARD_COMPLETE: - case SOLVE_FORWARD_COMPLETE: - output << " % equation " << ModelBlock->Block_List[j].Equation[i]+1 << " variable : " << sModel - << " (" << ModelBlock->Block_List[j].Variable[i]+1 << ")" << endl; - output << " " << "residual(" << i+1 << ") = ("; - goto end; - case SOLVE_TWO_BOUNDARIES_COMPLETE: - case SOLVE_TWO_BOUNDARIES_SIMPLE: - output << " % equation " << ModelBlock->Block_List[j].Equation[i]+1 << " variable : " << sModel - << " (" << ModelBlock->Block_List[j].Variable[i]+1 << ")" << endl; - Uf[ModelBlock->Block_List[j].Equation[i]] << " b(" << i+1 << "+Per_J_) = -residual(" << i+1 << ", it_)"; - output << " residual(" << i+1 << ", it_) = ("; - goto end; - default: -end: - output << tmp_output.str(); - output << ") - ("; - rhs->writeOutput(output, oMatlabDynamicModelSparse, temporary_terms); - output << ");\n"; -#ifdef CONDITION - if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE) - output << " condition(" << i+1 << ")=0;\n"; -#endif - } - } - // The Jacobian if we have to solve the block - if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE - || ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE) - output << " " << sps << "% Jacobian " << endl; + output << " g1 = spalloc(" << ModelBlock->Block_List[j].Size << ", " << ModelBlock->Block_List[j].Size << ", " << nze << ");\n"; + output << " end;\n"; + } + + output << " g2=0;g3=0;\n"; + if(ModelBlock->Block_List[j].Temporary_InUse->size()) + { + tmp_output.str(""); + for (temporary_terms_inuse_type::const_iterator it = ModelBlock->Block_List[j].Temporary_InUse->begin(); + it != ModelBlock->Block_List[j].Temporary_InUse->end(); it++) + tmp_output << " T" << *it; + output << " global" << tmp_output.str() << ";\n"; + } + output << " residual=zeros(" << ModelBlock->Block_List[j].Size << ",1);\n"; + if (ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD + ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD + ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD_R + ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD_R) + output << " for it_ = y_kmin+1:(y_kmin+periods)\n"; + + + if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE) + { + output << " b = [];\n"; + output << " for it_ = y_kmin+1:(periods+y_kmin)\n"; + output << " Per_y_=it_*y_size;\n"; + output << " Per_J_=(it_-y_kmin-1)*y_size;\n"; + output << " Per_K_=(it_-1)*y_size;\n"; + sps=" "; + } + else + if(ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD || ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD || + ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD_R || ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD_R) + sps = " "; else - if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_BACKWARD_SIMPLE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_FORWARD_SIMPLE || - ModelBlock->Block_List[j].Simulation_Type==SOLVE_BACKWARD_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_FORWARD_COMPLETE) - output << " % Jacobian " << endl << " if jacobian_eval" << endl; - else - output << " % Jacobian " << endl << " if jacobian_eval" << endl; - switch (ModelBlock->Block_List[j].Simulation_Type) - { - case EVALUATE_BACKWARD: - case EVALUATE_FORWARD: - case EVALUATE_BACKWARD_R: - case EVALUATE_FORWARD_R: - count_derivates++; - for (m=0;mBlock_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag+1;m++) - { - k=m-ModelBlock->Block_List[j].Max_Lag; - for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; - int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var[i]; - output << " g1(" << eqr+1 << ", " << /*varr+1+(m+variable_table.max_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.endo_nbr*/ + sps=""; + // The equations + for (i = 0;i < ModelBlock->Block_List[j].Size;i++) + { + temporary_terms_type tt2; + tt2.clear(); + if (ModelBlock->Block_List[j].Temporary_Terms_in_Equation[i]->size()) + output << " " << sps << "% //Temporary variables" << endl; + for (temporary_terms_type::const_iterator it = ModelBlock->Block_List[j].Temporary_Terms_in_Equation[i]->begin(); + it != ModelBlock->Block_List[j].Temporary_Terms_in_Equation[i]->end(); it++) + { + output << " " << sps; + (*it)->writeOutput(output, oMatlabDynamicModelSparse, temporary_terms); + output << " = "; + (*it)->writeOutput(output, oMatlabDynamicModelSparse, tt2); + // Insert current node into tt2 + tt2.insert(*it); + output << ";" << endl; + } + string sModel = symbol_table.getNameByID(eEndogenous, ModelBlock->Block_List[j].Variable[i]) ; + eq_node = equations[ModelBlock->Block_List[j].Equation[i]]; + lhs = eq_node->arg1; + rhs = eq_node->arg2; + tmp_output.str(""); + lhs->writeOutput(tmp_output, oMatlabDynamicModelSparse, temporary_terms); + switch (ModelBlock->Block_List[j].Simulation_Type) + { + case EVALUATE_BACKWARD: + case EVALUATE_FORWARD: + output << " % equation " << ModelBlock->Block_List[j].Equation[i]+1 << " variable : " << sModel + << " (" << ModelBlock->Block_List[j].Variable[i]+1 << ")" << endl; + output << " "; + output << tmp_output.str(); + output << " = "; + rhs->writeOutput(output, oMatlabDynamicModelSparse, temporary_terms); + output << ";\n"; + break; + case EVALUATE_BACKWARD_R: + case EVALUATE_FORWARD_R: + output << " % equation " << ModelBlock->Block_List[j].Equation[i]+1 << " variable : " << sModel + << " (" << ModelBlock->Block_List[j].Variable[i]+1 << ")" << endl; + output << " "; + rhs->writeOutput(output, oMatlabDynamicModelSparse, temporary_terms); + output << " = "; + lhs->writeOutput(output, oMatlabDynamicModelSparse, temporary_terms); + output << ";\n"; + break; + case SOLVE_BACKWARD_SIMPLE: + case SOLVE_FORWARD_SIMPLE: + case SOLVE_BACKWARD_COMPLETE: + case SOLVE_FORWARD_COMPLETE: + output << " % equation " << ModelBlock->Block_List[j].Equation[i]+1 << " variable : " << sModel + << " (" << ModelBlock->Block_List[j].Variable[i]+1 << ")" << endl; + output << " " << "residual(" << i+1 << ") = ("; + goto end; + case SOLVE_TWO_BOUNDARIES_COMPLETE: + case SOLVE_TWO_BOUNDARIES_SIMPLE: + output << " % equation " << ModelBlock->Block_List[j].Equation[i]+1 << " variable : " << sModel + << " (" << ModelBlock->Block_List[j].Variable[i]+1 << ")" << endl; + Uf[ModelBlock->Block_List[j].Equation[i]] << " b(" << i+1 << "+Per_J_) = -residual(" << i+1 << ", it_)"; + output << " residual(" << i+1 << ", it_) = ("; + goto end; + default: + end: + output << tmp_output.str(); + output << ") - ("; + rhs->writeOutput(output, oMatlabDynamicModelSparse, temporary_terms); + output << ");\n"; +#ifdef CONDITION + if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE) + output << " condition(" << i+1 << ")=0;\n"; +#endif + } + } + // The Jacobian if we have to solve the block + if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE + || ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE) + output << " " << sps << "% Jacobian " << endl; + else + if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_BACKWARD_SIMPLE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_FORWARD_SIMPLE || + ModelBlock->Block_List[j].Simulation_Type==SOLVE_BACKWARD_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_FORWARD_COMPLETE) + output << " % Jacobian " << endl << " if jacobian_eval" << endl; + else + output << " % Jacobian " << endl << " if jacobian_eval" << endl; + switch (ModelBlock->Block_List[j].Simulation_Type) + { + case EVALUATE_BACKWARD: + case EVALUATE_FORWARD: + case EVALUATE_BACKWARD_R: + case EVALUATE_FORWARD_R: + count_derivates++; + for (m=0;mBlock_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag+1;m++) + { + k=m-ModelBlock->Block_List[j].Max_Lag; + for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; + int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var[i]; + output << " g1(" << eqr+1 << ", " << /*varr+1+(m+variable_table.max_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.endo_nbr*/ varr+1+m*ModelBlock->Block_List[j].Size << ") = "; - writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous); - output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var) - << "(" << k//variable_table.getLag(variable_table.getSymbolID(ModelBlock->Block_List[j].Variable[0])) - << ") " << var+1 - << ", equation=" << eq+1 << endl; - } - } - //jacobian_max_endo_col=(variable_table.max_endo_lag+variable_table.max_endo_lead+1)*symbol_table.endo_nbr; - for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) - { - k=m-ModelBlock->Block_List[j].Max_Lag; - for (i=0;iBlock_List[j].IM_lead_lag[m].size_exo;i++) - { - int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X_Index[i]; - int var=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous_Index[i]; - int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X[i]; - int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous[i]; - output << " g1_x(" << eqr+1 << ", " - << varr+1+(m+variable_table.max_exo_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.exo_nbr << ") = "; - writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eExogenous); - output << "; % variable=" << symbol_table.getNameByID(eExogenous, var) - << "(" << k << ") " << var+1 - << ", equation=" << eq+1 << endl; - } - } - for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) - { - k=m-ModelBlock->Block_List[j].Max_Lag; - if (block_triangular.incidencematrix.Model_Max_Lag_Endo - ModelBlock->Block_List[j].Max_Lag +m >=0) - { - for (i=0;iBlock_List[j].IM_lead_lag[m].size_other_endo;i++) - { - int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_Index_other_endo[i]; - int var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index_other_endo[i]; - int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_other_endo[i]; - int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var_other_endo[i]; - output << " g1_o(" << eqr+1 << ", " - << varr+1+(m+variable_table.max_endo_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.endo_nbr << ") = "; - writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous); - output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var) - << "(" << k << ") " << var+1 - << ", equation=" << eq+1 << endl; - } - } - } - output << " varargout{1}=g1_x;\n"; - output << " varargout{2}=g1_o;\n"; - output << " end;" << endl; - output << " end;" << endl; - break; - case SOLVE_BACKWARD_SIMPLE: - case SOLVE_FORWARD_SIMPLE: - case SOLVE_BACKWARD_COMPLETE: - case SOLVE_FORWARD_COMPLETE: - count_derivates++; - for (m=0;mBlock_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag+1;m++) - { - k=m-ModelBlock->Block_List[j].Max_Lag; - for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; - int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var[i]; - output << " g1(" << eqr+1 << ", " << /*varr+1+(m+variable_table.max_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.endo_nbr*/ + writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous); + output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var) + << "(" << k//variable_table.getLag(variable_table.getSymbolID(ModelBlock->Block_List[j].Variable[0])) + << ") " << var+1 + << ", equation=" << eq+1 << endl; + } + } + //jacobian_max_endo_col=(variable_table.max_endo_lag+variable_table.max_endo_lead+1)*symbol_table.endo_nbr; + for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) + { + k=m-ModelBlock->Block_List[j].Max_Lag; + for (i=0;iBlock_List[j].IM_lead_lag[m].size_exo;i++) + { + int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X_Index[i]; + int var=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous_Index[i]; + int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X[i]; + int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous[i]; + output << " g1_x(" << eqr+1 << ", " + << varr+1+(m+variable_table.max_exo_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.exo_nbr << ") = "; + writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eExogenous); + output << "; % variable=" << symbol_table.getNameByID(eExogenous, var) + << "(" << k << ") " << var+1 + << ", equation=" << eq+1 << endl; + } + } + for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) + { + k=m-ModelBlock->Block_List[j].Max_Lag; + if (block_triangular.incidencematrix.Model_Max_Lag_Endo - ModelBlock->Block_List[j].Max_Lag +m >=0) + { + for (i=0;iBlock_List[j].IM_lead_lag[m].size_other_endo;i++) + { + int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_Index_other_endo[i]; + int var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index_other_endo[i]; + int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_other_endo[i]; + int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var_other_endo[i]; + output << " g1_o(" << eqr+1 << ", " + << varr+1+(m+variable_table.max_endo_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.endo_nbr << ") = "; + writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous); + output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var) + << "(" << k << ") " << var+1 + << ", equation=" << eq+1 << endl; + } + } + } + output << " varargout{1}=g1_x;\n"; + output << " varargout{2}=g1_o;\n"; + output << " end;" << endl; + output << " end;" << endl; + break; + case SOLVE_BACKWARD_SIMPLE: + case SOLVE_FORWARD_SIMPLE: + case SOLVE_BACKWARD_COMPLETE: + case SOLVE_FORWARD_COMPLETE: + count_derivates++; + for (m=0;mBlock_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag+1;m++) + { + k=m-ModelBlock->Block_List[j].Max_Lag; + for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; + int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var[i]; + output << " g1(" << eqr+1 << ", " << /*varr+1+(m+variable_table.max_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.endo_nbr*/ varr+1+m*ModelBlock->Block_List[j].Size << ") = "; - writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous); - output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var) - << "(" << k - << ") " << var+1 - << ", equation=" << eq+1 << endl; - } - } - for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) - { - k=m-ModelBlock->Block_List[j].Max_Lag; - for (i=0;iBlock_List[j].IM_lead_lag[m].size_exo;i++) - { - int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X_Index[i]; - int var=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous_Index[i]; - int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X[i]; - int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous[i]; - output << " g1_x(" << eqr+1 << ", " << varr+1+(m+variable_table.max_exo_lag-ModelBlock->Block_List[j].Max_Lag)*ModelBlock->Block_List[j].nb_exo << ") = "; - writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eExogenous); - output << "; % variable=" << symbol_table.getNameByID(eExogenous, var) - << "(" << k << ") " << var+1 - << ", equation=" << eq+1 << endl; - } - } - for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) - { - k=m-ModelBlock->Block_List[j].Max_Lag; - if (block_triangular.incidencematrix.Model_Max_Lag_Endo - ModelBlock->Block_List[j].Max_Lag +m >=0) - { - for (i=0;iBlock_List[j].IM_lead_lag[m].size_other_endo;i++) - { - int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_Index_other_endo[i]; - int var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index_other_endo[i]; - int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_other_endo[i]; - int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var_other_endo[i]; - output << " g1_o(" << eqr+1 << ", " - << varr+1+(m+variable_table.max_endo_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.endo_nbr << ") = "; - writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous); - output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var) - << "(" << k << ") " << var+1 - << ", equation=" << eq+1 << endl; - } - } - } - output << " varargout{1}=g1_x;\n"; - output << " varargout{2}=g1_o;\n"; - output << " else" << endl; + writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous); + output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var) + << "(" << k + << ") " << var+1 + << ", equation=" << eq+1 << endl; + } + } + for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) + { + k=m-ModelBlock->Block_List[j].Max_Lag; + for (i=0;iBlock_List[j].IM_lead_lag[m].size_exo;i++) + { + int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X_Index[i]; + int var=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous_Index[i]; + int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X[i]; + int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous[i]; + output << " g1_x(" << eqr+1 << ", " << varr+1+(m+variable_table.max_exo_lag-ModelBlock->Block_List[j].Max_Lag)*ModelBlock->Block_List[j].nb_exo << ") = "; + writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eExogenous); + output << "; % variable=" << symbol_table.getNameByID(eExogenous, var) + << "(" << k << ") " << var+1 + << ", equation=" << eq+1 << endl; + } + } + for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) + { + k=m-ModelBlock->Block_List[j].Max_Lag; + if (block_triangular.incidencematrix.Model_Max_Lag_Endo - ModelBlock->Block_List[j].Max_Lag +m >=0) + { + for (i=0;iBlock_List[j].IM_lead_lag[m].size_other_endo;i++) + { + int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_Index_other_endo[i]; + int var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index_other_endo[i]; + int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_other_endo[i]; + int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var_other_endo[i]; + output << " g1_o(" << eqr+1 << ", " + << varr+1+(m+variable_table.max_endo_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.endo_nbr << ") = "; + writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous); + output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var) + << "(" << k << ") " << var+1 + << ", equation=" << eq+1 << endl; + } + } + } + output << " varargout{1}=g1_x;\n"; + output << " varargout{2}=g1_o;\n"; + output << " else" << endl; - m=ModelBlock->Block_List[j].Max_Lag; - for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; - int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var[i]; - output << " g1(" << eqr+1 << ", " << varr+1 << ") = "; - writeDerivative(output, eq, var, 0, oMatlabDynamicModelSparse, temporary_terms, eEndogenous); - output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var) - << "(" << variable_table.getLag(variable_table.getSymbolID(var)) << ") " << var+1 - << ", equation=" << eq+1 << endl; - } - output << " end;\n"; - break; - case SOLVE_TWO_BOUNDARIES_SIMPLE: - case SOLVE_TWO_BOUNDARIES_COMPLETE: - output << " if ~jacobian_eval" << endl; - for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) - { - k=m-ModelBlock->Block_List[j].Max_Lag; - for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; - int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var[i]; - if (k==0) - Uf[ModelBlock->Block_List[j].Equation[eqr]] << "+g1(" << eqr+1 << "+Per_J_, " << varr+1 << "+Per_K_)*y(it_, " << var+1 << ")"; - else if (k==1) - Uf[ModelBlock->Block_List[j].Equation[eqr]] << "+g1(" << eqr+1 << "+Per_J_, " << varr+1 << "+Per_y_)*y(it_+1, " << var+1 << ")"; - else if (k>0) - Uf[ModelBlock->Block_List[j].Equation[eqr]] << "+g1(" << eqr+1 << "+Per_J_, " << varr+1 << "+y_size*(it_+" << k-1 << "))*y(it_+" << k << ", " << var+1 << ")"; - else if (k<0) - Uf[ModelBlock->Block_List[j].Equation[eqr]] << "+g1(" << eqr+1 << "+Per_J_, " << varr+1 << "+y_size*(it_" << k-1 << "))*y(it_" << k << ", " << var+1 << ")"; - if (k==0) - output << " g1(" << eqr+1 << "+Per_J_, " << varr+1 << "+Per_K_) = "; - else if (k==1) - output << " g1(" << eqr+1 << "+Per_J_, " << varr+1 << "+Per_y_) = "; - else if (k>0) - output << " g1(" << eqr+1 << "+Per_J_, " << varr+1 << "+y_size*(it_+" << k-1 << ")) = "; - else if (k<0) - output << " g1(" << eqr+1 << "+Per_J_, " << varr+1 << "+y_size*(it_" << k-1 << ")) = "; - writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous); - output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var) - << "(" << k << ") " << var+1 - << ", equation=" << eq+1 << endl; + m=ModelBlock->Block_List[j].Max_Lag; + for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; + int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var[i]; + output << " g1(" << eqr+1 << ", " << varr+1 << ") = "; + writeDerivative(output, eq, var, 0, oMatlabDynamicModelSparse, temporary_terms, eEndogenous); + output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var) + << "(" << variable_table.getLag(variable_table.getSymbolID(var)) << ") " << var+1 + << ", equation=" << eq+1 << endl; + } + output << " end;\n"; + break; + case SOLVE_TWO_BOUNDARIES_SIMPLE: + case SOLVE_TWO_BOUNDARIES_COMPLETE: + output << " if ~jacobian_eval" << endl; + for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) + { + k=m-ModelBlock->Block_List[j].Max_Lag; + for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; + int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var[i]; + if (k==0) + Uf[ModelBlock->Block_List[j].Equation[eqr]] << "+g1(" << eqr+1 << "+Per_J_, " << varr+1 << "+Per_K_)*y(it_, " << var+1 << ")"; + else if (k==1) + Uf[ModelBlock->Block_List[j].Equation[eqr]] << "+g1(" << eqr+1 << "+Per_J_, " << varr+1 << "+Per_y_)*y(it_+1, " << var+1 << ")"; + else if (k>0) + Uf[ModelBlock->Block_List[j].Equation[eqr]] << "+g1(" << eqr+1 << "+Per_J_, " << varr+1 << "+y_size*(it_+" << k-1 << "))*y(it_+" << k << ", " << var+1 << ")"; + else if (k<0) + Uf[ModelBlock->Block_List[j].Equation[eqr]] << "+g1(" << eqr+1 << "+Per_J_, " << varr+1 << "+y_size*(it_" << k-1 << "))*y(it_" << k << ", " << var+1 << ")"; + if (k==0) + output << " g1(" << eqr+1 << "+Per_J_, " << varr+1 << "+Per_K_) = "; + else if (k==1) + output << " g1(" << eqr+1 << "+Per_J_, " << varr+1 << "+Per_y_) = "; + else if (k>0) + output << " g1(" << eqr+1 << "+Per_J_, " << varr+1 << "+y_size*(it_+" << k-1 << ")) = "; + else if (k<0) + output << " g1(" << eqr+1 << "+Per_J_, " << varr+1 << "+y_size*(it_" << k-1 << ")) = "; + writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous); + output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var) + << "(" << k << ") " << var+1 + << ", equation=" << eq+1 << endl; #ifdef CONDITION - output << " if (fabs(condition[" << eqr << "])Block_List[j].Size;i++) - { - output << " " << Uf[ModelBlock->Block_List[j].Equation[i]].str() << ";\n"; + } + } + for (i = 0;i < ModelBlock->Block_List[j].Size;i++) + { + output << " " << Uf[ModelBlock->Block_List[j].Equation[i]].str() << ";\n"; #ifdef CONDITION - output << " if (fabs(condition(" << i+1 << "))Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) - { - k=m-ModelBlock->Block_List[j].Max_Lag; - for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 u=ModelBlock->Block_List[j].IM_lead_lag[m].u[i]; - int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; - output << " u(" << u+1 << "+Per_u_) = u(" << u+1 << "+Per_u_) / condition(" << eqr+1 << ");\n"; - } - } - for (i = 0;i < ModelBlock->Block_List[j].Size;i++) - output << " u(" << i+1 << "+Per_u_) = u(" << i+1 << "+Per_u_) / condition(" << i+1 << ");\n"; + for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) + { + k=m-ModelBlock->Block_List[j].Max_Lag; + for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 u=ModelBlock->Block_List[j].IM_lead_lag[m].u[i]; + int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; + output << " u(" << u+1 << "+Per_u_) = u(" << u+1 << "+Per_u_) / condition(" << eqr+1 << ");\n"; + } + } + for (i = 0;i < ModelBlock->Block_List[j].Size;i++) + output << " u(" << i+1 << "+Per_u_) = u(" << i+1 << "+Per_u_) / condition(" << i+1 << ");\n"; #endif - output << " else" << endl; - for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) - { - k=m-ModelBlock->Block_List[j].Max_Lag; - for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; - int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var[i]; - output << " g1(" << eqr+1 << ", " << varr+1+(m-ModelBlock->Block_List[j].Max_Lag+ModelBlock->Block_List[j].Max_Lag_Endo)*ModelBlock->Block_List[j].Size << ") = "; - writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous); - output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var) - << "(" << k << ") " << var+1 - << ", equation=" << eq+1 << endl; - } - } - jacobian_max_endo_col=(ModelBlock->Block_List[j].Max_Lead_Endo+ModelBlock->Block_List[j].Max_Lag_Endo+1)*ModelBlock->Block_List[j].Size; - for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) - { - k=m-ModelBlock->Block_List[j].Max_Lag; - for (i=0;iBlock_List[j].IM_lead_lag[m].size_exo;i++) - { - int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X_Index[i]; - int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X[i]; - int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous[i]; - int var=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous_Index[i]; - output << " g1_x(" << eqr+1 << ", " - << jacobian_max_endo_col+(m-(ModelBlock->Block_List[j].Max_Lag-ModelBlock->Block_List[j].Max_Lag_Exo))*ModelBlock->Block_List[j].nb_exo+varr+1 << ") = "; - writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eExogenous); - output << "; % variable (exogenous)=" << symbol_table.getNameByID(eExogenous, var) - << "(" << k << ") " << var+1 << " " << varr+1 - << ", equation=" << eq+1 << endl; - } - } - for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) - { - k=m-ModelBlock->Block_List[j].Max_Lag; - if (block_triangular.incidencematrix.Model_Max_Lag_Endo - ModelBlock->Block_List[j].Max_Lag +m >=0) - { - for (i=0;iBlock_List[j].IM_lead_lag[m].size_other_endo;i++) - { - int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_Index_other_endo[i]; - int var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index_other_endo[i]; - int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_other_endo[i]; - int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var_other_endo[i]; - output << " g1_o(" << eqr+1 << ", " - << varr+1+(m+variable_table.max_endo_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.endo_nbr << ") = "; - writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous); - output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var) - << "(" << k << ") " << var+1 - << ", equation=" << eq+1 << endl; - } - } - } - output << " varargout{1}=g1_x;\n"; - output << " varargout{2}=g1_o;\n"; - output << " end;\n"; - output << " end;\n"; - break; - default: - break; - } - prev_Simulation_Type=ModelBlock->Block_List[j].Simulation_Type; - output.close(); - } - } + output << " else" << endl; + for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) + { + k=m-ModelBlock->Block_List[j].Max_Lag; + for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; + int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var[i]; + output << " g1(" << eqr+1 << ", " << varr+1+(m-ModelBlock->Block_List[j].Max_Lag+ModelBlock->Block_List[j].Max_Lag_Endo)*ModelBlock->Block_List[j].Size << ") = "; + writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous); + output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var) + << "(" << k << ") " << var+1 + << ", equation=" << eq+1 << endl; + } + } + jacobian_max_endo_col=(ModelBlock->Block_List[j].Max_Lead_Endo+ModelBlock->Block_List[j].Max_Lag_Endo+1)*ModelBlock->Block_List[j].Size; + for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) + { + k=m-ModelBlock->Block_List[j].Max_Lag; + for (i=0;iBlock_List[j].IM_lead_lag[m].size_exo;i++) + { + int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X_Index[i]; + int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_X[i]; + int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous[i]; + int var=ModelBlock->Block_List[j].IM_lead_lag[m].Exogenous_Index[i]; + output << " g1_x(" << eqr+1 << ", " + << jacobian_max_endo_col+(m-(ModelBlock->Block_List[j].Max_Lag-ModelBlock->Block_List[j].Max_Lag_Exo))*ModelBlock->Block_List[j].nb_exo+varr+1 << ") = "; + writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eExogenous); + output << "; % variable (exogenous)=" << symbol_table.getNameByID(eExogenous, var) + << "(" << k << ") " << var+1 << " " << varr+1 + << ", equation=" << eq+1 << endl; + } + } + for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) + { + k=m-ModelBlock->Block_List[j].Max_Lag; + if (block_triangular.incidencematrix.Model_Max_Lag_Endo - ModelBlock->Block_List[j].Max_Lag +m >=0) + { + for (i=0;iBlock_List[j].IM_lead_lag[m].size_other_endo;i++) + { + int eq=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_Index_other_endo[i]; + int var=ModelBlock->Block_List[j].IM_lead_lag[m].Var_Index_other_endo[i]; + int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ_other_endo[i]; + int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var_other_endo[i]; + output << " g1_o(" << eqr+1 << ", " + << varr+1+(m+variable_table.max_endo_lag-ModelBlock->Block_List[j].Max_Lag)*symbol_table.endo_nbr << ") = "; + writeDerivative(output, eq, var, k, oMatlabDynamicModelSparse, temporary_terms, eEndogenous); + output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var) + << "(" << k << ") " << var+1 + << ", equation=" << eq+1 << endl; + } + } + } + output << " varargout{1}=g1_x;\n"; + output << " varargout{2}=g1_o;\n"; + output << " end;\n"; + output << " end;\n"; + break; + default: + break; + } + prev_Simulation_Type=ModelBlock->Block_List[j].Simulation_Type; + output.close(); + } +} void ModelTree::writeModelStaticEquationsOrdered_M(Model_Block *ModelBlock, const string &static_basename) const - { - int i,j,k,m, var, eq, g1_index = 1; - string tmp_s, sps; - ostringstream tmp_output, tmp1_output, global_output; - NodeID lhs=NULL, rhs=NULL; - BinaryOpNode *eq_node; - map reference_count; - int prev_Simulation_Type=-1; - int nze=0; - bool *IM, *IMl; - ofstream output; - temporary_terms_type::const_iterator it_temp=temporary_terms.begin(); - //---------------------------------------------------------------------- - for (j = 0;j < ModelBlock->Size;j++) - { - //For a block composed of a single equation determines wether we have to evaluate or to solve the equation - tmp1_output.str(""); - tmp1_output << static_basename << "_" << j+1 << ".m"; - output.open(tmp1_output.str().c_str(), ios::out | ios::binary); - output << "%\n"; - output << "% " << tmp1_output.str() << " : Computes static model for Dynare\n"; - output << "%\n"; - output << "% Warning : this file is generated automatically by Dynare\n"; - output << "% from model file (.mod)\n\n"; - output << "%/\n"; - if (ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD - ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD - ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD_R - ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD_R ) - output << "function [y, g1] = " << static_basename << "_" << j+1 << "(y, x, params, jacobian_eval)\n"; - else - output << "function [residual, g1, g2, g3] = " << static_basename << "_" << j+1 << "(y, x, params, jacobian_eval)\n"; - output << " % ////////////////////////////////////////////////////////////////////////" << endl - << " % //" << string(" Block ").substr(int(log10(j + 1))) << j + 1 << " " - << BlockTriangular::BlockType0(ModelBlock->Block_List[j].Type) << " //" << endl - << " % // Simulation type "; - output << BlockTriangular::BlockSim(ModelBlock->Block_List[j].Simulation_Type) << " //" << endl - << " % ////////////////////////////////////////////////////////////////////////" << endl; - //The Temporary terms - //output << global_output.str(); - if(ModelBlock->Block_List[j].Temporary_InUse->size()) - { - tmp_output.str(""); - for (temporary_terms_inuse_type::const_iterator it = ModelBlock->Block_List[j].Temporary_InUse->begin(); - it != ModelBlock->Block_List[j].Temporary_InUse->end(); it++) - tmp_output << " T" << *it; - output << " global" << tmp_output.str() << ";\n"; - } +{ + int i,j,k,m, var, eq, g1_index = 1; + string tmp_s, sps; + ostringstream tmp_output, tmp1_output, global_output; + NodeID lhs=NULL, rhs=NULL; + BinaryOpNode *eq_node; + map reference_count; + int prev_Simulation_Type=-1; + int nze=0; + bool *IM, *IMl; + ofstream output; + temporary_terms_type::const_iterator it_temp=temporary_terms.begin(); + //---------------------------------------------------------------------- + for (j = 0;j < ModelBlock->Size;j++) + { + //For a block composed of a single equation determines wether we have to evaluate or to solve the equation + tmp1_output.str(""); + tmp1_output << static_basename << "_" << j+1 << ".m"; + output.open(tmp1_output.str().c_str(), ios::out | ios::binary); + output << "%\n"; + output << "% " << tmp1_output.str() << " : Computes static model for Dynare\n"; + output << "%\n"; + output << "% Warning : this file is generated automatically by Dynare\n"; + output << "% from model file (.mod)\n\n"; + output << "%/\n"; + if (ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD + ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD + ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD_R + ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD_R ) + output << "function [y, g1] = " << static_basename << "_" << j+1 << "(y, x, params, jacobian_eval)\n"; + else + output << "function [residual, g1, g2, g3] = " << static_basename << "_" << j+1 << "(y, x, params, jacobian_eval)\n"; + output << " % ////////////////////////////////////////////////////////////////////////" << endl + << " % //" << string(" Block ").substr(int(log10(j + 1))) << j + 1 << " " + << BlockTriangular::BlockType0(ModelBlock->Block_List[j].Type) << " //" << endl + << " % // Simulation type "; + output << BlockTriangular::BlockSim(ModelBlock->Block_List[j].Simulation_Type) << " //" << endl + << " % ////////////////////////////////////////////////////////////////////////" << endl; + //The Temporary terms + //output << global_output.str(); + if(ModelBlock->Block_List[j].Temporary_InUse->size()) + { + tmp_output.str(""); + for (temporary_terms_inuse_type::const_iterator it = ModelBlock->Block_List[j].Temporary_InUse->begin(); + it != ModelBlock->Block_List[j].Temporary_InUse->end(); it++) + tmp_output << " T" << *it; + output << " global" << tmp_output.str() << ";\n"; + } - int n=ModelBlock->Block_List[j].Size; - int n1=symbol_table.endo_nbr; - IM=(bool*)malloc(n*n*sizeof(bool)); - memset(IM, 0, n*n*sizeof(bool)); - for (m=-ModelBlock->Block_List[j].Max_Lag;m<=ModelBlock->Block_List[j].Max_Lead;m++) - { - IMl=block_triangular.incidencematrix.Get_IM(m, eEndogenous); - if (IMl) - { - for (i=0;iBlock_List[j].Equation[i]; - for (k=0;kBlock_List[j].Variable[k]; - IM[i*n+k]=IM[i*n+k] || IMl[eq*n1+var]; - } - } - } - } - for (nze=0, i=0;iBlock_List[j].Simulation_Type!=EVALUATE_BACKWARD && ModelBlock->Block_List[j].Simulation_Type!=EVALUATE_FORWARD - && ModelBlock->Block_List[j].Simulation_Type!=EVALUATE_BACKWARD_R && ModelBlock->Block_List[j].Simulation_Type!=EVALUATE_FORWARD_R) - { - output << " g1=spalloc(" << ModelBlock->Block_List[j].Size << ", " << ModelBlock->Block_List[j].Size << ", " << nze << ");\n"; - output << " residual=zeros(" << ModelBlock->Block_List[j].Size << ",1);\n"; - } - sps=""; - // The equations - for (i = 0;i < ModelBlock->Block_List[j].Size;i++) - { - temporary_terms_type tt2; - tt2.clear(); - if (ModelBlock->Block_List[j].Temporary_Terms_in_Equation[i]->size()) - output << " " << sps << "% //Temporary variables" << endl; - for (temporary_terms_type::const_iterator it = ModelBlock->Block_List[j].Temporary_Terms_in_Equation[i]->begin(); - it != ModelBlock->Block_List[j].Temporary_Terms_in_Equation[i]->end(); it++) - { - output << " " << sps; - (*it)->writeOutput(output, oMatlabStaticModelSparse, temporary_terms); - output << " = "; - (*it)->writeOutput(output, oMatlabStaticModelSparse, tt2); - // Insert current node into tt2 - tt2.insert(*it); - output << ";" << endl; - } - string sModel = symbol_table.getNameByID(eEndogenous, ModelBlock->Block_List[j].Variable[i]) ; - output << sps << " % equation " << ModelBlock->Block_List[j].Equation[i]+1 << " variable : " - << sModel << " (" << ModelBlock->Block_List[j].Variable[i]+1 << ")" << endl; - eq_node = equations[ModelBlock->Block_List[j].Equation[i]]; - lhs = eq_node->arg1; - rhs = eq_node->arg2; - tmp_output.str(""); - lhs->writeOutput(tmp_output, oMatlabStaticModelSparse, temporary_terms); - output << " "; - switch (ModelBlock->Block_List[j].Simulation_Type) - { - case EVALUATE_BACKWARD: - case EVALUATE_FORWARD: - output << tmp_output.str(); - output << " = "; - rhs->writeOutput(output, oMatlabStaticModelSparse, temporary_terms); - output << ";\n"; - break; - case EVALUATE_BACKWARD_R: - case EVALUATE_FORWARD_R: - rhs->writeOutput(output, oMatlabStaticModelSparse, temporary_terms); - output << " = "; - lhs->writeOutput(output, oMatlabStaticModelSparse, temporary_terms); - output << ";\n"; - break; - case SOLVE_BACKWARD_SIMPLE: - case SOLVE_FORWARD_SIMPLE: - case SOLVE_BACKWARD_COMPLETE: - case SOLVE_FORWARD_COMPLETE: - case SOLVE_TWO_BOUNDARIES_COMPLETE: - case SOLVE_TWO_BOUNDARIES_SIMPLE: - goto end; - default: -end: - output << sps << "residual(" << i+1 << ") = ("; - output << tmp_output.str(); - output << ") - ("; - rhs->writeOutput(output, oMatlabStaticModelSparse, temporary_terms); - output << ");\n"; + int n=ModelBlock->Block_List[j].Size; + int n1=symbol_table.endo_nbr; + IM=(bool*)malloc(n*n*sizeof(bool)); + memset(IM, 0, n*n*sizeof(bool)); + for (m=-ModelBlock->Block_List[j].Max_Lag;m<=ModelBlock->Block_List[j].Max_Lead;m++) + { + IMl=block_triangular.incidencematrix.Get_IM(m, eEndogenous); + if (IMl) + { + for (i=0;iBlock_List[j].Equation[i]; + for (k=0;kBlock_List[j].Variable[k]; + IM[i*n+k]=IM[i*n+k] || IMl[eq*n1+var]; + } + } + } + } + for (nze=0, i=0;iBlock_List[j].Simulation_Type!=EVALUATE_BACKWARD && ModelBlock->Block_List[j].Simulation_Type!=EVALUATE_FORWARD + && ModelBlock->Block_List[j].Simulation_Type!=EVALUATE_BACKWARD_R && ModelBlock->Block_List[j].Simulation_Type!=EVALUATE_FORWARD_R) + { + output << " g1=spalloc(" << ModelBlock->Block_List[j].Size << ", " << ModelBlock->Block_List[j].Size << ", " << nze << ");\n"; + output << " residual=zeros(" << ModelBlock->Block_List[j].Size << ",1);\n"; + } + sps=""; + // The equations + for (i = 0;i < ModelBlock->Block_List[j].Size;i++) + { + temporary_terms_type tt2; + tt2.clear(); + if (ModelBlock->Block_List[j].Temporary_Terms_in_Equation[i]->size()) + output << " " << sps << "% //Temporary variables" << endl; + for (temporary_terms_type::const_iterator it = ModelBlock->Block_List[j].Temporary_Terms_in_Equation[i]->begin(); + it != ModelBlock->Block_List[j].Temporary_Terms_in_Equation[i]->end(); it++) + { + output << " " << sps; + (*it)->writeOutput(output, oMatlabStaticModelSparse, temporary_terms); + output << " = "; + (*it)->writeOutput(output, oMatlabStaticModelSparse, tt2); + // Insert current node into tt2 + tt2.insert(*it); + output << ";" << endl; + } + string sModel = symbol_table.getNameByID(eEndogenous, ModelBlock->Block_List[j].Variable[i]) ; + output << sps << " % equation " << ModelBlock->Block_List[j].Equation[i]+1 << " variable : " + << sModel << " (" << ModelBlock->Block_List[j].Variable[i]+1 << ")" << endl; + eq_node = equations[ModelBlock->Block_List[j].Equation[i]]; + lhs = eq_node->arg1; + rhs = eq_node->arg2; + tmp_output.str(""); + lhs->writeOutput(tmp_output, oMatlabStaticModelSparse, temporary_terms); + output << " "; + switch (ModelBlock->Block_List[j].Simulation_Type) + { + case EVALUATE_BACKWARD: + case EVALUATE_FORWARD: + output << tmp_output.str(); + output << " = "; + rhs->writeOutput(output, oMatlabStaticModelSparse, temporary_terms); + output << ";\n"; + break; + case EVALUATE_BACKWARD_R: + case EVALUATE_FORWARD_R: + rhs->writeOutput(output, oMatlabStaticModelSparse, temporary_terms); + output << " = "; + lhs->writeOutput(output, oMatlabStaticModelSparse, temporary_terms); + output << ";\n"; + break; + case SOLVE_BACKWARD_SIMPLE: + case SOLVE_FORWARD_SIMPLE: + case SOLVE_BACKWARD_COMPLETE: + case SOLVE_FORWARD_COMPLETE: + case SOLVE_TWO_BOUNDARIES_COMPLETE: + case SOLVE_TWO_BOUNDARIES_SIMPLE: + goto end; + default: + end: + output << sps << "residual(" << i+1 << ") = ("; + output << tmp_output.str(); + output << ") - ("; + rhs->writeOutput(output, oMatlabStaticModelSparse, temporary_terms); + output << ");\n"; #ifdef CONDITION - if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE) - output << " condition(" << i+1 << ")=0;\n"; + if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE) + output << " condition(" << i+1 << ")=0;\n"; #endif - } - } - // The Jacobian if we have to solve the block - output << " " << sps << "% Jacobian " << endl; - switch (ModelBlock->Block_List[j].Simulation_Type) - { - case EVALUATE_BACKWARD: - case EVALUATE_FORWARD: - case EVALUATE_BACKWARD_R: - case EVALUATE_FORWARD_R: - output << " if(jacobian_eval)\n"; - output << " g1( " << g1_index << ", " << g1_index << ")="; - writeDerivative(output, ModelBlock->Block_List[j].Equation[0], ModelBlock->Block_List[j].Variable[0], 0, oMatlabStaticModelSparse, temporary_terms, eEndogenous); - output << "; % variable=" << symbol_table.getNameByID(eEndogenous, ModelBlock->Block_List[j].Variable[0]) - << "(" << variable_table.getLag(variable_table.getSymbolID(ModelBlock->Block_List[j].Variable[0])) - << ") " << ModelBlock->Block_List[j].Variable[0]+1 - << ", equation=" << ModelBlock->Block_List[j].Equation[0]+1 << endl; - output << " end\n"; - break; - case SOLVE_BACKWARD_SIMPLE: - case SOLVE_FORWARD_SIMPLE: - case SOLVE_BACKWARD_COMPLETE: - case SOLVE_FORWARD_COMPLETE: - case SOLVE_TWO_BOUNDARIES_COMPLETE: - case SOLVE_TWO_BOUNDARIES_SIMPLE: - output << " g2=0;g3=0;\n"; - for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) - { - k=m-ModelBlock->Block_List[j].Max_Lag; - for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; - int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var[i]; - output << " g1(" << eqr+1 << ", " << varr+1 << ") = g1(" << eqr+1 << ", " << varr+1 << ") + "; - writeDerivative(output, eq, var, k, oMatlabStaticModelSparse, temporary_terms, eEndogenous); - output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var) - << "(" << k << ") " << var+1 - << ", equation=" << eq+1 << endl; + } + } + // The Jacobian if we have to solve the block + output << " " << sps << "% Jacobian " << endl; + switch (ModelBlock->Block_List[j].Simulation_Type) + { + case EVALUATE_BACKWARD: + case EVALUATE_FORWARD: + case EVALUATE_BACKWARD_R: + case EVALUATE_FORWARD_R: + output << " if(jacobian_eval)\n"; + output << " g1( " << g1_index << ", " << g1_index << ")="; + writeDerivative(output, ModelBlock->Block_List[j].Equation[0], ModelBlock->Block_List[j].Variable[0], 0, oMatlabStaticModelSparse, temporary_terms, eEndogenous); + output << "; % variable=" << symbol_table.getNameByID(eEndogenous, ModelBlock->Block_List[j].Variable[0]) + << "(" << variable_table.getLag(variable_table.getSymbolID(ModelBlock->Block_List[j].Variable[0])) + << ") " << ModelBlock->Block_List[j].Variable[0]+1 + << ", equation=" << ModelBlock->Block_List[j].Equation[0]+1 << endl; + output << " end\n"; + break; + case SOLVE_BACKWARD_SIMPLE: + case SOLVE_FORWARD_SIMPLE: + case SOLVE_BACKWARD_COMPLETE: + case SOLVE_FORWARD_COMPLETE: + case SOLVE_TWO_BOUNDARIES_COMPLETE: + case SOLVE_TWO_BOUNDARIES_SIMPLE: + output << " g2=0;g3=0;\n"; + for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) + { + k=m-ModelBlock->Block_List[j].Max_Lag; + for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; + int varr=ModelBlock->Block_List[j].IM_lead_lag[m].Var[i]; + output << " g1(" << eqr+1 << ", " << varr+1 << ") = g1(" << eqr+1 << ", " << varr+1 << ") + "; + writeDerivative(output, eq, var, k, oMatlabStaticModelSparse, temporary_terms, eEndogenous); + output << "; % variable=" << symbol_table.getNameByID(eEndogenous, var) + << "(" << k << ") " << var+1 + << ", equation=" << eq+1 << endl; #ifdef CONDITION - output << " if (fabs(condition[" << eqr << "])Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) - { - k=m-ModelBlock->Block_List[j].Max_Lag; - for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 u=ModelBlock->Block_List[j].IM_lead_lag[m].u[i]; - int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; - output << " u(" << u+1 << "+Per_u_) = u(" << u+1 << "+Per_u_) / condition(" << eqr+1 << ");\n"; - } - } - for (i = 0;i < ModelBlock->Block_List[j].Size;i++) - output << " u(" << i+1 << "+Per_u_) = u(" << i+1 << "+Per_u_) / condition(" << i+1 << ");\n"; + for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) + { + k=m-ModelBlock->Block_List[j].Max_Lag; + for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 u=ModelBlock->Block_List[j].IM_lead_lag[m].u[i]; + int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; + output << " u(" << u+1 << "+Per_u_) = u(" << u+1 << "+Per_u_) / condition(" << eqr+1 << ");\n"; + } + } + for (i = 0;i < ModelBlock->Block_List[j].Size;i++) + output << " u(" << i+1 << "+Per_u_) = u(" << i+1 << "+Per_u_) / condition(" << i+1 << ");\n"; #endif - break; - default: - break; - } - prev_Simulation_Type=ModelBlock->Block_List[j].Simulation_Type; - free(IM); - output << "return;\n"; - output.close(); - } - //output << "return;\n\n\n"; - } + break; + default: + break; + } + prev_Simulation_Type=ModelBlock->Block_List[j].Simulation_Type; + free(IM); + output << "return;\n"; + output.close(); + } + //output << "return;\n\n\n"; +} void ModelTree::writeModelEquationsCodeOrdered(const string file_name, const Model_Block *ModelBlock, const string bin_basename, ExprNodeOutputType output_type, map_idx_type map_idx) const +{ + struct Uff_l { - struct Uff_l - { - int u, var, lag; - Uff_l *pNext; - }; + int u, var, lag; + Uff_l *pNext; + }; - struct Uff - { - Uff_l *Ufl, *Ufl_First; - int eqr; - }; + struct Uff + { + Uff_l *Ufl, *Ufl_First; + int eqr; + }; - int i,j,k,m, v, ModelBlock_Aggregated_Count, k0, k1; - string tmp_s; - ostringstream tmp_output; - ofstream code_file; - NodeID lhs=NULL, rhs=NULL; - BinaryOpNode *eq_node; - bool lhs_rhs_done; - Uff Uf[symbol_table.endo_nbr]; - map reference_count; - map ModelBlock_Aggregated_Size, ModelBlock_Aggregated_Number; - int prev_Simulation_Type=-1; - //SymbolicGaussElimination SGE; - bool file_open=false; - temporary_terms_type::const_iterator it_temp=temporary_terms.begin(); - //---------------------------------------------------------------------- - string main_name=file_name; - main_name+=".cod"; - code_file.open(main_name.c_str(), ios::out | ios::binary | ios::ate ); - if (!code_file.is_open()) - { - cout << "Error : Can't open file \"" << main_name << "\" for writing\n"; - exit(EXIT_FAILURE); - } - //Temporary variables declaration - code_file.write(&FDIMT, sizeof(FDIMT)); - k=temporary_terms.size(); - code_file.write(reinterpret_cast(&k),sizeof(k)); - //search for successive and identical blocks - i=k=k0=0; - ModelBlock_Aggregated_Count=-1; - for (j = 0;j < ModelBlock->Size;j++) - { - if (BlockTriangular::BlockSim(prev_Simulation_Type)==BlockTriangular::BlockSim(ModelBlock->Block_List[j].Simulation_Type) - && (ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD - ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD - ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD_R - ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD_R )) - { - } - else - { - k=k0=0; - ModelBlock_Aggregated_Count++; - } - k0+=ModelBlock->Block_List[j].Size; - ModelBlock_Aggregated_Number[ModelBlock_Aggregated_Count]=k0; - ModelBlock_Aggregated_Size[ModelBlock_Aggregated_Count]=++k; - prev_Simulation_Type=ModelBlock->Block_List[j].Simulation_Type; - } - ModelBlock_Aggregated_Count++; - //For each block - j=0; - for (k0 = 0;k0 < ModelBlock_Aggregated_Count;k0++) - { - k1=j; - if (k0>0) - code_file.write(&FENDBLOCK, sizeof(FENDBLOCK)); - code_file.write(&FBEGINBLOCK, sizeof(FBEGINBLOCK)); - v=ModelBlock_Aggregated_Number[k0]; - code_file.write(reinterpret_cast(&v),sizeof(v)); - v=ModelBlock->Block_List[j].Simulation_Type; - code_file.write(reinterpret_cast(&v),sizeof(v)); - for (k=0; kBlock_List[j].Size;i++) - { - code_file.write(reinterpret_cast(&ModelBlock->Block_List[j].Variable[i]),sizeof(ModelBlock->Block_List[j].Variable[i])); - code_file.write(reinterpret_cast(&ModelBlock->Block_List[j].Equation[i]),sizeof(ModelBlock->Block_List[j].Equation[i])); - code_file.write(reinterpret_cast(&ModelBlock->Block_List[j].Own_Derivative[i]),sizeof(ModelBlock->Block_List[j].Own_Derivative[i])); - } - j++; - } - j=k1; - if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE || - ModelBlock->Block_List[j].Simulation_Type==SOLVE_BACKWARD_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_FORWARD_COMPLETE) - { - code_file.write(reinterpret_cast(&ModelBlock->Block_List[j].is_linear),sizeof(ModelBlock->Block_List[j].is_linear)); - v=block_triangular.ModelBlock->Block_List[j].IM_lead_lag[block_triangular.ModelBlock->Block_List[j].Max_Lag + block_triangular.ModelBlock->Block_List[j].Max_Lead].u_finish + 1; - code_file.write(reinterpret_cast(&v),sizeof(v)); - v=symbol_table.endo_nbr; - code_file.write(reinterpret_cast(&v),sizeof(v)); - v=block_triangular.ModelBlock->Block_List[j].Max_Lag; - code_file.write(reinterpret_cast(&v),sizeof(v)); - v=block_triangular.ModelBlock->Block_List[j].Max_Lead; - code_file.write(reinterpret_cast(&v),sizeof(v)); - //if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE) - //{ - int u_count_int=0; - Write_Inf_To_Bin_File(file_name, bin_basename, j, u_count_int,file_open, - ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE); - v=u_count_int; - code_file.write(reinterpret_cast(&v),sizeof(v)); - file_open=true; - //} - } - for (k1 = 0; k1 < ModelBlock_Aggregated_Size[k0]; k1++) - { - //For a block composed of a single equation determines whether we have to evaluate or to solve the equation - if (ModelBlock->Block_List[j].Size==1) - { - lhs_rhs_done=true; - eq_node = equations[ModelBlock->Block_List[j].Equation[0]]; - lhs = eq_node->arg1; - rhs = eq_node->arg2; - } - else - lhs_rhs_done=false; - // The equations - for (i = 0;i < ModelBlock->Block_List[j].Size;i++) - { - //ModelBlock->Block_List[j].Variable_Sorted[i] = variable_table.getID(eEndogenous, ModelBlock->Block_List[j].Variable[i], 0); - //The Temporary terms - temporary_terms_type tt2; + int i,j,k,m, v, ModelBlock_Aggregated_Count, k0, k1; + string tmp_s; + ostringstream tmp_output; + ofstream code_file; + NodeID lhs=NULL, rhs=NULL; + BinaryOpNode *eq_node; + bool lhs_rhs_done; + Uff Uf[symbol_table.endo_nbr]; + map reference_count; + map ModelBlock_Aggregated_Size, ModelBlock_Aggregated_Number; + int prev_Simulation_Type=-1; + //SymbolicGaussElimination SGE; + bool file_open=false; + temporary_terms_type::const_iterator it_temp=temporary_terms.begin(); + //---------------------------------------------------------------------- + string main_name=file_name; + main_name+=".cod"; + code_file.open(main_name.c_str(), ios::out | ios::binary | ios::ate ); + if (!code_file.is_open()) + { + cout << "Error : Can't open file \"" << main_name << "\" for writing\n"; + exit(EXIT_FAILURE); + } + //Temporary variables declaration + code_file.write(&FDIMT, sizeof(FDIMT)); + k=temporary_terms.size(); + code_file.write(reinterpret_cast(&k),sizeof(k)); + //search for successive and identical blocks + i=k=k0=0; + ModelBlock_Aggregated_Count=-1; + for (j = 0;j < ModelBlock->Size;j++) + { + if (BlockTriangular::BlockSim(prev_Simulation_Type)==BlockTriangular::BlockSim(ModelBlock->Block_List[j].Simulation_Type) + && (ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD + ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD + ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_BACKWARD_R + ||ModelBlock->Block_List[j].Simulation_Type==EVALUATE_FORWARD_R )) + { + } + else + { + k=k0=0; + ModelBlock_Aggregated_Count++; + } + k0+=ModelBlock->Block_List[j].Size; + ModelBlock_Aggregated_Number[ModelBlock_Aggregated_Count]=k0; + ModelBlock_Aggregated_Size[ModelBlock_Aggregated_Count]=++k; + prev_Simulation_Type=ModelBlock->Block_List[j].Simulation_Type; + } + ModelBlock_Aggregated_Count++; + //For each block + j=0; + for (k0 = 0;k0 < ModelBlock_Aggregated_Count;k0++) + { + k1=j; + if (k0>0) + code_file.write(&FENDBLOCK, sizeof(FENDBLOCK)); + code_file.write(&FBEGINBLOCK, sizeof(FBEGINBLOCK)); + v=ModelBlock_Aggregated_Number[k0]; + code_file.write(reinterpret_cast(&v),sizeof(v)); + v=ModelBlock->Block_List[j].Simulation_Type; + code_file.write(reinterpret_cast(&v),sizeof(v)); + for (k=0; kBlock_List[j].Size;i++) + { + code_file.write(reinterpret_cast(&ModelBlock->Block_List[j].Variable[i]),sizeof(ModelBlock->Block_List[j].Variable[i])); + code_file.write(reinterpret_cast(&ModelBlock->Block_List[j].Equation[i]),sizeof(ModelBlock->Block_List[j].Equation[i])); + code_file.write(reinterpret_cast(&ModelBlock->Block_List[j].Own_Derivative[i]),sizeof(ModelBlock->Block_List[j].Own_Derivative[i])); + } + j++; + } + j=k1; + if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE || + ModelBlock->Block_List[j].Simulation_Type==SOLVE_BACKWARD_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_FORWARD_COMPLETE) + { + code_file.write(reinterpret_cast(&ModelBlock->Block_List[j].is_linear),sizeof(ModelBlock->Block_List[j].is_linear)); + v=block_triangular.ModelBlock->Block_List[j].IM_lead_lag[block_triangular.ModelBlock->Block_List[j].Max_Lag + block_triangular.ModelBlock->Block_List[j].Max_Lead].u_finish + 1; + code_file.write(reinterpret_cast(&v),sizeof(v)); + v=symbol_table.endo_nbr; + code_file.write(reinterpret_cast(&v),sizeof(v)); + v=block_triangular.ModelBlock->Block_List[j].Max_Lag; + code_file.write(reinterpret_cast(&v),sizeof(v)); + v=block_triangular.ModelBlock->Block_List[j].Max_Lead; + code_file.write(reinterpret_cast(&v),sizeof(v)); + //if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE) + //{ + int u_count_int=0; + Write_Inf_To_Bin_File(file_name, bin_basename, j, u_count_int,file_open, + ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE || ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_SIMPLE); + v=u_count_int; + code_file.write(reinterpret_cast(&v),sizeof(v)); + file_open=true; + //} + } + for (k1 = 0; k1 < ModelBlock_Aggregated_Size[k0]; k1++) + { + //For a block composed of a single equation determines whether we have to evaluate or to solve the equation + if (ModelBlock->Block_List[j].Size==1) + { + lhs_rhs_done=true; + eq_node = equations[ModelBlock->Block_List[j].Equation[0]]; + lhs = eq_node->arg1; + rhs = eq_node->arg2; + } + else + lhs_rhs_done=false; + // The equations + for (i = 0;i < ModelBlock->Block_List[j].Size;i++) + { + //ModelBlock->Block_List[j].Variable_Sorted[i] = variable_table.getID(eEndogenous, ModelBlock->Block_List[j].Variable[i], 0); + //The Temporary terms + temporary_terms_type tt2; #ifdef DEBUGC - k=0; + k=0; #endif - for (temporary_terms_type::const_iterator it = ModelBlock->Block_List[j].Temporary_Terms_in_Equation[i]->begin(); - it != ModelBlock->Block_List[j].Temporary_Terms_in_Equation[i]->end(); it++) - { - (*it)->compile(code_file,false, output_type, tt2, map_idx); - code_file.write(&FSTPT, sizeof(FSTPT)); - map_idx_type::const_iterator ii=map_idx.find((*it)->idx); - v=(int)ii->second; - code_file.write(reinterpret_cast(&v), sizeof(v)); - // Insert current node into tt2 - tt2.insert(*it); + for (temporary_terms_type::const_iterator it = ModelBlock->Block_List[j].Temporary_Terms_in_Equation[i]->begin(); + it != ModelBlock->Block_List[j].Temporary_Terms_in_Equation[i]->end(); it++) + { + (*it)->compile(code_file,false, output_type, tt2, map_idx); + code_file.write(&FSTPT, sizeof(FSTPT)); + map_idx_type::const_iterator ii=map_idx.find((*it)->idx); + v=(int)ii->second; + code_file.write(reinterpret_cast(&v), sizeof(v)); + // Insert current node into tt2 + tt2.insert(*it); #ifdef DEBUGC - cout << "FSTPT " << v << "\n"; - code_file.write(&FOK, sizeof(FOK)); - code_file.write(reinterpret_cast(&k), sizeof(k)); - ki++; + cout << "FSTPT " << v << "\n"; + code_file.write(&FOK, sizeof(FOK)); + code_file.write(reinterpret_cast(&k), sizeof(k)); + ki++; #endif - } + } #ifdef DEBUGC - for (temporary_terms_type::const_iterator it = ModelBlock->Block_List[j].Temporary_terms->begin(); - it != ModelBlock->Block_List[j].Temporary_terms->end(); it++) - { - map_idx_type::const_iterator ii=map_idx.find((*it)->idx); - cout << "map_idx[" << (*it)->idx <<"]=" << ii->second << "\n"; - } + for (temporary_terms_type::const_iterator it = ModelBlock->Block_List[j].Temporary_terms->begin(); + it != ModelBlock->Block_List[j].Temporary_terms->end(); it++) + { + map_idx_type::const_iterator ii=map_idx.find((*it)->idx); + cout << "map_idx[" << (*it)->idx <<"]=" << ii->second << "\n"; + } #endif - if (!lhs_rhs_done) - { - eq_node = equations[ModelBlock->Block_List[j].Equation[i]]; - lhs = eq_node->arg1; - rhs = eq_node->arg2; - } - switch (ModelBlock->Block_List[j].Simulation_Type) - { - case EVALUATE_BACKWARD: - case EVALUATE_FORWARD: - rhs->compile(code_file,false, output_type, temporary_terms, map_idx); - lhs->compile(code_file,true, output_type, temporary_terms, map_idx); - break; - case EVALUATE_BACKWARD_R: - case EVALUATE_FORWARD_R: - lhs->compile(code_file,false, output_type, temporary_terms, map_idx); - rhs->compile(code_file,true, output_type, temporary_terms, map_idx); - break; - case SOLVE_BACKWARD_COMPLETE: - case SOLVE_FORWARD_COMPLETE: - v=ModelBlock->Block_List[j].Equation[i]; - Uf[v].eqr=i; - Uf[v].Ufl=NULL; - goto end; - case SOLVE_TWO_BOUNDARIES_COMPLETE: - case SOLVE_TWO_BOUNDARIES_SIMPLE: - v=ModelBlock->Block_List[j].Equation[i]; - Uf[v].eqr=i; - Uf[v].Ufl=NULL; - goto end; - default: -end: - lhs->compile(code_file,false, output_type, temporary_terms, map_idx); - rhs->compile(code_file,false, output_type, temporary_terms, map_idx); - code_file.write(&FBINARY, sizeof(FBINARY)); - int v=oMinus; - code_file.write(reinterpret_cast(&v),sizeof(v)); - code_file.write(&FSTPR, sizeof(FSTPR)); - code_file.write(reinterpret_cast(&i), sizeof(i)); + if (!lhs_rhs_done) + { + eq_node = equations[ModelBlock->Block_List[j].Equation[i]]; + lhs = eq_node->arg1; + rhs = eq_node->arg2; + } + switch (ModelBlock->Block_List[j].Simulation_Type) + { + case EVALUATE_BACKWARD: + case EVALUATE_FORWARD: + rhs->compile(code_file,false, output_type, temporary_terms, map_idx); + lhs->compile(code_file,true, output_type, temporary_terms, map_idx); + break; + case EVALUATE_BACKWARD_R: + case EVALUATE_FORWARD_R: + lhs->compile(code_file,false, output_type, temporary_terms, map_idx); + rhs->compile(code_file,true, output_type, temporary_terms, map_idx); + break; + case SOLVE_BACKWARD_COMPLETE: + case SOLVE_FORWARD_COMPLETE: + v=ModelBlock->Block_List[j].Equation[i]; + Uf[v].eqr=i; + Uf[v].Ufl=NULL; + goto end; + case SOLVE_TWO_BOUNDARIES_COMPLETE: + case SOLVE_TWO_BOUNDARIES_SIMPLE: + v=ModelBlock->Block_List[j].Equation[i]; + Uf[v].eqr=i; + Uf[v].Ufl=NULL; + goto end; + default: + end: + lhs->compile(code_file,false, output_type, temporary_terms, map_idx); + rhs->compile(code_file,false, output_type, temporary_terms, map_idx); + code_file.write(&FBINARY, sizeof(FBINARY)); + int v=oMinus; + code_file.write(reinterpret_cast(&v),sizeof(v)); + code_file.write(&FSTPR, sizeof(FSTPR)); + code_file.write(reinterpret_cast(&i), sizeof(i)); #ifdef CONDITION - if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE) - output << " condition[" << i << "]=0;\n"; + if (ModelBlock->Block_List[j].Simulation_Type==SOLVE_TWO_BOUNDARIES_COMPLETE) + output << " condition[" << i << "]=0;\n"; #endif - } - } - code_file.write(&FENDEQU, sizeof(FENDEQU)); - // The Jacobian if we have to solve the block - if (ModelBlock->Block_List[j].Simulation_Type!=EVALUATE_BACKWARD - && ModelBlock->Block_List[j].Simulation_Type!=EVALUATE_FORWARD - && ModelBlock->Block_List[j].Simulation_Type!=EVALUATE_BACKWARD_R - && ModelBlock->Block_List[j].Simulation_Type!=EVALUATE_FORWARD_R) - { - switch (ModelBlock->Block_List[j].Simulation_Type) - { - case SOLVE_BACKWARD_SIMPLE: - case SOLVE_FORWARD_SIMPLE: - compileDerivative(code_file, ModelBlock->Block_List[j].Equation[0], ModelBlock->Block_List[j].Variable[0], 0, output_type, map_idx); - code_file.write(&FSTPG, sizeof(FSTPG)); - v=0; - code_file.write(reinterpret_cast(&v), sizeof(v)); - break; - case SOLVE_BACKWARD_COMPLETE: - case SOLVE_FORWARD_COMPLETE: - m=ModelBlock->Block_List[j].Max_Lag; - for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 u=ModelBlock->Block_List[j].IM_lead_lag[m].us[i]; - int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; - int v=ModelBlock->Block_List[j].Equation[eqr]; - if (!Uf[v].Ufl) - { - Uf[v].Ufl=(Uff_l*)malloc(sizeof(Uff_l)); - Uf[v].Ufl_First=Uf[v].Ufl; - } - else - { - Uf[v].Ufl->pNext=(Uff_l*)malloc(sizeof(Uff_l)); - Uf[v].Ufl=Uf[v].Ufl->pNext; - } - Uf[v].Ufl->pNext=NULL; - Uf[v].Ufl->u=u; - Uf[v].Ufl->var=var; - compileDerivative(code_file, eq, var, 0, output_type, map_idx); - code_file.write(&FSTPU, sizeof(FSTPU)); - code_file.write(reinterpret_cast(&u), sizeof(u)); - } - for (i = 0;i < ModelBlock->Block_List[j].Size;i++) - { - code_file.write(&FLDR, sizeof(FLDR)); - code_file.write(reinterpret_cast(&i), sizeof(i)); - code_file.write(&FLDZ, sizeof(FLDZ)); - int v=ModelBlock->Block_List[j].Equation[i]; - for (Uf[v].Ufl=Uf[v].Ufl_First;Uf[v].Ufl;Uf[v].Ufl=Uf[v].Ufl->pNext) - { - code_file.write(&FLDU, sizeof(FLDU)); - code_file.write(reinterpret_cast(&Uf[v].Ufl->u), sizeof(Uf[v].Ufl->u)); - code_file.write(&FLDV, sizeof(FLDV)); - char vc=eEndogenous; - code_file.write(reinterpret_cast(&vc), sizeof(vc)); - code_file.write(reinterpret_cast(&Uf[v].Ufl->var), sizeof(Uf[v].Ufl->var)); - int v1=0; - code_file.write(reinterpret_cast(&v1), sizeof(v1)); - code_file.write(&FBINARY, sizeof(FBINARY)); - v1=oTimes; - code_file.write(reinterpret_cast(&v1), sizeof(v1)); - code_file.write(&FCUML, sizeof(FCUML)); - } - Uf[v].Ufl=Uf[v].Ufl_First; - while (Uf[v].Ufl) - { - Uf[v].Ufl_First=Uf[v].Ufl->pNext; - free(Uf[v].Ufl); - Uf[v].Ufl=Uf[v].Ufl_First; - } - code_file.write(&FBINARY, sizeof(FBINARY)); - v=oMinus; - code_file.write(reinterpret_cast(&v), sizeof(v)); - code_file.write(&FSTPU, sizeof(FSTPU)); - code_file.write(reinterpret_cast(&i), sizeof(i)); - } - break; - case SOLVE_TWO_BOUNDARIES_COMPLETE: - case SOLVE_TWO_BOUNDARIES_SIMPLE: - for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) - { - k=m-ModelBlock->Block_List[j].Max_Lag; - for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 u=ModelBlock->Block_List[j].IM_lead_lag[m].u[i]; - int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; - int v=ModelBlock->Block_List[j].Equation[eqr]; - if (!Uf[v].Ufl) - { - Uf[v].Ufl=(Uff_l*)malloc(sizeof(Uff_l)); - Uf[v].Ufl_First=Uf[v].Ufl; - } - else - { - Uf[v].Ufl->pNext=(Uff_l*)malloc(sizeof(Uff_l)); - Uf[v].Ufl=Uf[v].Ufl->pNext; - } - Uf[v].Ufl->pNext=NULL; - Uf[v].Ufl->u=u; - Uf[v].Ufl->var=var; - Uf[v].Ufl->lag=k; - compileDerivative(code_file, eq, var, k, output_type, map_idx); - code_file.write(&FSTPU, sizeof(FSTPU)); - code_file.write(reinterpret_cast(&u), sizeof(u)); + } + } + code_file.write(&FENDEQU, sizeof(FENDEQU)); + // The Jacobian if we have to solve the block + if (ModelBlock->Block_List[j].Simulation_Type!=EVALUATE_BACKWARD + && ModelBlock->Block_List[j].Simulation_Type!=EVALUATE_FORWARD + && ModelBlock->Block_List[j].Simulation_Type!=EVALUATE_BACKWARD_R + && ModelBlock->Block_List[j].Simulation_Type!=EVALUATE_FORWARD_R) + { + switch (ModelBlock->Block_List[j].Simulation_Type) + { + case SOLVE_BACKWARD_SIMPLE: + case SOLVE_FORWARD_SIMPLE: + compileDerivative(code_file, ModelBlock->Block_List[j].Equation[0], ModelBlock->Block_List[j].Variable[0], 0, output_type, map_idx); + code_file.write(&FSTPG, sizeof(FSTPG)); + v=0; + code_file.write(reinterpret_cast(&v), sizeof(v)); + break; + case SOLVE_BACKWARD_COMPLETE: + case SOLVE_FORWARD_COMPLETE: + m=ModelBlock->Block_List[j].Max_Lag; + for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 u=ModelBlock->Block_List[j].IM_lead_lag[m].us[i]; + int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; + int v=ModelBlock->Block_List[j].Equation[eqr]; + if (!Uf[v].Ufl) + { + Uf[v].Ufl=(Uff_l*)malloc(sizeof(Uff_l)); + Uf[v].Ufl_First=Uf[v].Ufl; + } + else + { + Uf[v].Ufl->pNext=(Uff_l*)malloc(sizeof(Uff_l)); + Uf[v].Ufl=Uf[v].Ufl->pNext; + } + Uf[v].Ufl->pNext=NULL; + Uf[v].Ufl->u=u; + Uf[v].Ufl->var=var; + compileDerivative(code_file, eq, var, 0, output_type, map_idx); + code_file.write(&FSTPU, sizeof(FSTPU)); + code_file.write(reinterpret_cast(&u), sizeof(u)); + } + for (i = 0;i < ModelBlock->Block_List[j].Size;i++) + { + code_file.write(&FLDR, sizeof(FLDR)); + code_file.write(reinterpret_cast(&i), sizeof(i)); + code_file.write(&FLDZ, sizeof(FLDZ)); + int v=ModelBlock->Block_List[j].Equation[i]; + for (Uf[v].Ufl=Uf[v].Ufl_First;Uf[v].Ufl;Uf[v].Ufl=Uf[v].Ufl->pNext) + { + code_file.write(&FLDU, sizeof(FLDU)); + code_file.write(reinterpret_cast(&Uf[v].Ufl->u), sizeof(Uf[v].Ufl->u)); + code_file.write(&FLDV, sizeof(FLDV)); + char vc=eEndogenous; + code_file.write(reinterpret_cast(&vc), sizeof(vc)); + code_file.write(reinterpret_cast(&Uf[v].Ufl->var), sizeof(Uf[v].Ufl->var)); + int v1=0; + code_file.write(reinterpret_cast(&v1), sizeof(v1)); + code_file.write(&FBINARY, sizeof(FBINARY)); + v1=oTimes; + code_file.write(reinterpret_cast(&v1), sizeof(v1)); + code_file.write(&FCUML, sizeof(FCUML)); + } + Uf[v].Ufl=Uf[v].Ufl_First; + while (Uf[v].Ufl) + { + Uf[v].Ufl_First=Uf[v].Ufl->pNext; + free(Uf[v].Ufl); + Uf[v].Ufl=Uf[v].Ufl_First; + } + code_file.write(&FBINARY, sizeof(FBINARY)); + v=oMinus; + code_file.write(reinterpret_cast(&v), sizeof(v)); + code_file.write(&FSTPU, sizeof(FSTPU)); + code_file.write(reinterpret_cast(&i), sizeof(i)); + } + break; + case SOLVE_TWO_BOUNDARIES_COMPLETE: + case SOLVE_TWO_BOUNDARIES_SIMPLE: + for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) + { + k=m-ModelBlock->Block_List[j].Max_Lag; + for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 u=ModelBlock->Block_List[j].IM_lead_lag[m].u[i]; + int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; + int v=ModelBlock->Block_List[j].Equation[eqr]; + if (!Uf[v].Ufl) + { + Uf[v].Ufl=(Uff_l*)malloc(sizeof(Uff_l)); + Uf[v].Ufl_First=Uf[v].Ufl; + } + else + { + Uf[v].Ufl->pNext=(Uff_l*)malloc(sizeof(Uff_l)); + Uf[v].Ufl=Uf[v].Ufl->pNext; + } + Uf[v].Ufl->pNext=NULL; + Uf[v].Ufl->u=u; + Uf[v].Ufl->var=var; + Uf[v].Ufl->lag=k; + compileDerivative(code_file, eq, var, k, output_type, map_idx); + code_file.write(&FSTPU, sizeof(FSTPU)); + code_file.write(reinterpret_cast(&u), sizeof(u)); #ifdef CONDITION - output << " if (fabs(condition[" << eqr << "])Block_List[j].Size;i++) - { - code_file.write(&FLDR, sizeof(FLDR)); - code_file.write(reinterpret_cast(&i), sizeof(i)); - code_file.write(&FLDZ, sizeof(FLDZ)); - int v=ModelBlock->Block_List[j].Equation[i]; - for (Uf[v].Ufl=Uf[v].Ufl_First;Uf[v].Ufl;Uf[v].Ufl=Uf[v].Ufl->pNext) - { - code_file.write(&FLDU, sizeof(FLDU)); - code_file.write(reinterpret_cast(&Uf[v].Ufl->u), sizeof(Uf[v].Ufl->u)); - code_file.write(&FLDV, sizeof(FLDV)); - char vc=eEndogenous; - code_file.write(reinterpret_cast(&vc), sizeof(vc)); - int v1=Uf[v].Ufl->var; - code_file.write(reinterpret_cast(&v1), sizeof(v1)); - v1=Uf[v].Ufl->lag; - code_file.write(reinterpret_cast(&v1), sizeof(v1)); - code_file.write(&FBINARY, sizeof(FBINARY)); - v1=oTimes; - code_file.write(reinterpret_cast(&v1), sizeof(v1)); - code_file.write(&FCUML, sizeof(FCUML)); - } - Uf[v].Ufl=Uf[v].Ufl_First; - while (Uf[v].Ufl) - { - Uf[v].Ufl_First=Uf[v].Ufl->pNext; - free(Uf[v].Ufl); - Uf[v].Ufl=Uf[v].Ufl_First; - } - code_file.write(&FBINARY, sizeof(FBINARY)); - v=oMinus; - code_file.write(reinterpret_cast(&v), sizeof(v)); - code_file.write(&FSTPU, sizeof(FSTPU)); - code_file.write(reinterpret_cast(&i), sizeof(i)); + } + } + for (i = 0;i < ModelBlock->Block_List[j].Size;i++) + { + code_file.write(&FLDR, sizeof(FLDR)); + code_file.write(reinterpret_cast(&i), sizeof(i)); + code_file.write(&FLDZ, sizeof(FLDZ)); + int v=ModelBlock->Block_List[j].Equation[i]; + for (Uf[v].Ufl=Uf[v].Ufl_First;Uf[v].Ufl;Uf[v].Ufl=Uf[v].Ufl->pNext) + { + code_file.write(&FLDU, sizeof(FLDU)); + code_file.write(reinterpret_cast(&Uf[v].Ufl->u), sizeof(Uf[v].Ufl->u)); + code_file.write(&FLDV, sizeof(FLDV)); + char vc=eEndogenous; + code_file.write(reinterpret_cast(&vc), sizeof(vc)); + int v1=Uf[v].Ufl->var; + code_file.write(reinterpret_cast(&v1), sizeof(v1)); + v1=Uf[v].Ufl->lag; + code_file.write(reinterpret_cast(&v1), sizeof(v1)); + code_file.write(&FBINARY, sizeof(FBINARY)); + v1=oTimes; + code_file.write(reinterpret_cast(&v1), sizeof(v1)); + code_file.write(&FCUML, sizeof(FCUML)); + } + Uf[v].Ufl=Uf[v].Ufl_First; + while (Uf[v].Ufl) + { + Uf[v].Ufl_First=Uf[v].Ufl->pNext; + free(Uf[v].Ufl); + Uf[v].Ufl=Uf[v].Ufl_First; + } + code_file.write(&FBINARY, sizeof(FBINARY)); + v=oMinus; + code_file.write(reinterpret_cast(&v), sizeof(v)); + code_file.write(&FSTPU, sizeof(FSTPU)); + code_file.write(reinterpret_cast(&i), sizeof(i)); #ifdef CONDITION - output << " if (fabs(condition[" << i << "])Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) - { - k=m-ModelBlock->Block_List[j].Max_Lag; - for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 u=ModelBlock->Block_List[j].IM_lead_lag[m].u[i]; - int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; - output << " u[" << u << "+Per_u_] /= condition[" << eqr << "];\n"; - } - } - for (i = 0;i < ModelBlock->Block_List[j].Size;i++) - output << " u[" << i << "+Per_u_] /= condition[" << i << "];\n"; + for (m=0;m<=ModelBlock->Block_List[j].Max_Lead+ModelBlock->Block_List[j].Max_Lag;m++) + { + k=m-ModelBlock->Block_List[j].Max_Lag; + for (i=0;iBlock_List[j].IM_lead_lag[m].size;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 u=ModelBlock->Block_List[j].IM_lead_lag[m].u[i]; + int eqr=ModelBlock->Block_List[j].IM_lead_lag[m].Equ[i]; + output << " u[" << u << "+Per_u_] /= condition[" << eqr << "];\n"; + } + } + for (i = 0;i < ModelBlock->Block_List[j].Size;i++) + output << " u[" << i << "+Per_u_] /= condition[" << i << "];\n"; #endif - break; - default: - break; - } + break; + default: + break; + } - prev_Simulation_Type=ModelBlock->Block_List[j].Simulation_Type; - } - j++; - } - } - code_file.write(&FENDBLOCK, sizeof(FENDBLOCK)); - code_file.write(&FEND, sizeof(FEND)); - code_file.close(); - } + prev_Simulation_Type=ModelBlock->Block_List[j].Simulation_Type; + } + j++; + } + } + code_file.write(&FENDBLOCK, sizeof(FENDBLOCK)); + code_file.write(&FEND, sizeof(FEND)); + code_file.close(); +} void ModelTree::writeStaticMFile(const string &static_basename) const - { - string filename = static_basename + ".m"; +{ + string filename = static_basename + ".m"; - ofstream mStaticModelFile; - mStaticModelFile.open(filename.c_str(), ios::out | ios::binary); - if (!mStaticModelFile.is_open()) - { - cerr << "Error: Can't open file " << filename << " for writing" << endl; - exit(EXIT_FAILURE); - } - // Writing comments and function definition command - mStaticModelFile << "function [residual, g1, g2] = " << static_basename << "(y, x, params)" << endl - << "%" << endl - << "% Status : Computes static model for Dynare" << endl - << "%" << endl - << "% Warning : this file is generated automatically by Dynare" << endl - << "% from model file (.mod)" << endl << endl; + ofstream mStaticModelFile; + mStaticModelFile.open(filename.c_str(), ios::out | ios::binary); + if (!mStaticModelFile.is_open()) + { + cerr << "Error: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + // Writing comments and function definition command + mStaticModelFile << "function [residual, g1, g2] = " << static_basename << "(y, x, params)" << endl + << "%" << endl + << "% Status : Computes static model for Dynare" << endl + << "%" << endl + << "% Warning : this file is generated automatically by Dynare" << endl + << "% from model file (.mod)" << endl << endl; - writeStaticModel(mStaticModelFile); + writeStaticModel(mStaticModelFile); - mStaticModelFile.close(); - } + mStaticModelFile.close(); +} void ModelTree::writeDynamicMFile(const string &dynamic_basename) const - { - string filename = dynamic_basename + ".m"; +{ + string filename = dynamic_basename + ".m"; - ofstream mDynamicModelFile; - mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); - if (!mDynamicModelFile.is_open()) - { - cerr << "Error: Can't open file " << filename << " for writing" << endl; - exit(EXIT_FAILURE); - } - mDynamicModelFile << "function [residual, g1, g2, g3] = " << dynamic_basename << "(y, x, params, it_)" << endl - << "%" << endl - << "% Status : Computes dynamic model for Dynare" << endl - << "%" << endl - << "% Warning : this file is generated automatically by Dynare" << endl - << "% from model file (.mod)" << endl << endl; + ofstream mDynamicModelFile; + mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); + if (!mDynamicModelFile.is_open()) + { + cerr << "Error: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + mDynamicModelFile << "function [residual, g1, g2, g3] = " << dynamic_basename << "(y, x, params, it_)" << endl + << "%" << endl + << "% Status : Computes dynamic model for Dynare" << endl + << "%" << endl + << "% Warning : this file is generated automatically by Dynare" << endl + << "% from model file (.mod)" << endl << endl; - writeDynamicModel(mDynamicModelFile); + writeDynamicModel(mDynamicModelFile); - mDynamicModelFile.close(); - } + mDynamicModelFile.close(); +} void ModelTree::writeStaticCFile(const string &static_basename) const - { - string filename = static_basename + ".c"; +{ + string filename = static_basename + ".c"; - ofstream mStaticModelFile; - mStaticModelFile.open(filename.c_str(), ios::out | ios::binary); - if (!mStaticModelFile.is_open()) - { - cerr << "Error: Can't open file " << filename << " for writing" << endl; - exit(EXIT_FAILURE); - } - mStaticModelFile << "/*" << endl - << " * " << filename << " : Computes static model for Dynare" << endl - << " * Warning : this file is generated automatically by Dynare" << endl - << " * from model file (.mod)" << endl - << endl - << " */" << endl - << "#include " << endl - << "#include \"mex.h\"" << endl; + ofstream mStaticModelFile; + mStaticModelFile.open(filename.c_str(), ios::out | ios::binary); + if (!mStaticModelFile.is_open()) + { + cerr << "Error: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + mStaticModelFile << "/*" << endl + << " * " << filename << " : Computes static model for Dynare" << endl + << " * Warning : this file is generated automatically by Dynare" << endl + << " * from model file (.mod)" << endl + << endl + << " */" << endl + << "#include " << endl + << "#include \"mex.h\"" << endl; - // Writing the function Static - writeStaticModel(mStaticModelFile); + // Writing the function Static + writeStaticModel(mStaticModelFile); - // Writing the gateway routine - mStaticModelFile << "/* The gateway routine */" << endl - << "void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])" << endl - << "{" << endl - << " double *y, *x, *params;" << endl - << " double *residual, *g1;" << endl - << endl - << " /* Create a pointer to the input matrix y. */" << endl - << " y = mxGetPr(prhs[0]);" << endl - << endl - << " /* Create a pointer to the input matrix x. */" << endl - << " x = mxGetPr(prhs[1]);" << endl - << endl - << " /* Create a pointer to the input matrix params. */" << endl - << " params = mxGetPr(prhs[2]);" << endl - << endl - << " residual = NULL;" << endl - << " if (nlhs >= 1)" << endl - << " {" << endl - << " /* Set the output pointer to the output matrix residual. */" << endl - << " plhs[0] = mxCreateDoubleMatrix(" << equations.size() << ",1, mxREAL);" << endl - << " /* Create a C pointer to a copy of the output matrix residual. */" << endl - << " residual = mxGetPr(plhs[0]);" << endl - << " }" << endl - << endl - << " g1 = NULL;" << endl - << " if (nlhs >= 2)" << endl - << " {" << endl - << " /* Set the output pointer to the output matrix g1. */" << endl - << " plhs[1] = mxCreateDoubleMatrix(" << equations.size() << ", " << symbol_table.endo_nbr << ", mxREAL);" << endl - << " /* Create a C pointer to a copy of the output matrix g1. */" << endl - << " g1 = mxGetPr(plhs[1]);" << endl - << " }" << endl - << endl - << " /* Call the C Static. */" << endl - << " Static(y, x, params, residual, g1);" << endl - << "}" << endl; + // Writing the gateway routine + mStaticModelFile << "/* The gateway routine */" << endl + << "void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])" << endl + << "{" << endl + << " double *y, *x, *params;" << endl + << " double *residual, *g1;" << endl + << endl + << " /* Create a pointer to the input matrix y. */" << endl + << " y = mxGetPr(prhs[0]);" << endl + << endl + << " /* Create a pointer to the input matrix x. */" << endl + << " x = mxGetPr(prhs[1]);" << endl + << endl + << " /* Create a pointer to the input matrix params. */" << endl + << " params = mxGetPr(prhs[2]);" << endl + << endl + << " residual = NULL;" << endl + << " if (nlhs >= 1)" << endl + << " {" << endl + << " /* Set the output pointer to the output matrix residual. */" << endl + << " plhs[0] = mxCreateDoubleMatrix(" << equations.size() << ",1, mxREAL);" << endl + << " /* Create a C pointer to a copy of the output matrix residual. */" << endl + << " residual = mxGetPr(plhs[0]);" << endl + << " }" << endl + << endl + << " g1 = NULL;" << endl + << " if (nlhs >= 2)" << endl + << " {" << endl + << " /* Set the output pointer to the output matrix g1. */" << endl + << " plhs[1] = mxCreateDoubleMatrix(" << equations.size() << ", " << symbol_table.endo_nbr << ", mxREAL);" << endl + << " /* Create a C pointer to a copy of the output matrix g1. */" << endl + << " g1 = mxGetPr(plhs[1]);" << endl + << " }" << endl + << endl + << " /* Call the C Static. */" << endl + << " Static(y, x, params, residual, g1);" << endl + << "}" << endl; - mStaticModelFile.close(); - } + mStaticModelFile.close(); +} void ModelTree::writeDynamicCFile(const string &dynamic_basename) const - { - string filename = dynamic_basename + ".c"; - ofstream mDynamicModelFile; +{ + string filename = dynamic_basename + ".c"; + ofstream mDynamicModelFile; - mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); - if (!mDynamicModelFile.is_open()) - { - cerr << "Error: Can't open file " << filename << " for writing" << endl; - exit(EXIT_FAILURE); - } - mDynamicModelFile << "/*" << endl - << " * " << filename << " : Computes dynamic model for Dynare" << endl - << " *" << endl - << " * Warning : this file is generated automatically by Dynare" << endl - << " * from model file (.mod)" << endl - << endl - << " */" << endl - << "#include " << endl - << "#include \"mex.h\"" << endl; + mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); + if (!mDynamicModelFile.is_open()) + { + cerr << "Error: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + mDynamicModelFile << "/*" << endl + << " * " << filename << " : Computes dynamic model for Dynare" << endl + << " *" << endl + << " * Warning : this file is generated automatically by Dynare" << endl + << " * from model file (.mod)" << endl + << endl + << " */" << endl + << "#include " << endl + << "#include \"mex.h\"" << endl; - // Writing the function body - writeDynamicModel(mDynamicModelFile); + // Writing the function body + writeDynamicModel(mDynamicModelFile); - // Writing the gateway routine - mDynamicModelFile << "/* The gateway routine */" << endl - << "void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])" << endl - << "{" << endl - << " double *y, *x, *params;" << endl - << " double *residual, *g1, *g2;" << endl - << " int nb_row_x, it_;" << endl - << endl - << " /* Create a pointer to the input matrix y. */" << endl - << " y = mxGetPr(prhs[0]);" << endl - << endl - << " /* Create a pointer to the input matrix x. */" << endl - << " x = mxGetPr(prhs[1]);" << endl - << endl - << " /* Create a pointer to the input matrix params. */" << endl - << " params = mxGetPr(prhs[2]);" << endl - << endl - << " /* Fetch time index */" << endl - << " it_ = (int) mxGetScalar(prhs[3]) - 1;" << endl - << endl - << " /* Gets number of rows of matrix x. */" << endl - << " nb_row_x = mxGetM(prhs[1]);" << endl - << endl - << " residual = NULL;" << endl - << " if (nlhs >= 1)" << endl - << " {" << endl - << " /* Set the output pointer to the output matrix residual. */" << endl - << " plhs[0] = mxCreateDoubleMatrix(" << equations.size() << ",1, mxREAL);" << endl - << " /* Create a C pointer to a copy of the output matrix residual. */" << endl - << " residual = mxGetPr(plhs[0]);" << endl - << " }" << endl - << endl - << " g1 = NULL;" << endl - << " if (nlhs >= 2)" << endl - << " {" << endl - << " /* Set the output pointer to the output matrix g1. */" << endl + // Writing the gateway routine + mDynamicModelFile << "/* The gateway routine */" << endl + << "void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])" << endl + << "{" << endl + << " double *y, *x, *params;" << endl + << " double *residual, *g1, *g2;" << endl + << " int nb_row_x, it_;" << endl + << endl + << " /* Create a pointer to the input matrix y. */" << endl + << " y = mxGetPr(prhs[0]);" << endl + << endl + << " /* Create a pointer to the input matrix x. */" << endl + << " x = mxGetPr(prhs[1]);" << endl + << endl + << " /* Create a pointer to the input matrix params. */" << endl + << " params = mxGetPr(prhs[2]);" << endl + << endl + << " /* Fetch time index */" << endl + << " it_ = (int) mxGetScalar(prhs[3]) - 1;" << endl + << endl + << " /* Gets number of rows of matrix x. */" << endl + << " nb_row_x = mxGetM(prhs[1]);" << endl + << endl + << " residual = NULL;" << endl + << " if (nlhs >= 1)" << endl + << " {" << endl + << " /* Set the output pointer to the output matrix residual. */" << endl + << " plhs[0] = mxCreateDoubleMatrix(" << equations.size() << ",1, mxREAL);" << endl + << " /* Create a C pointer to a copy of the output matrix residual. */" << endl + << " residual = mxGetPr(plhs[0]);" << endl + << " }" << endl + << endl + << " g1 = NULL;" << endl + << " if (nlhs >= 2)" << endl + << " {" << endl + << " /* Set the output pointer to the output matrix g1. */" << endl - << " plhs[1] = mxCreateDoubleMatrix(" << equations.size() << ", " << variable_table.getDynJacobianColsNbr(computeJacobianExo) << ", mxREAL);" << endl - << " /* Create a C pointer to a copy of the output matrix g1. */" << endl - << " g1 = mxGetPr(plhs[1]);" << endl - << " }" << endl - << endl - << " g2 = NULL;" << endl - << " if (nlhs >= 3)" << endl - << " {" << endl - << " /* Set the output pointer to the output matrix g2. */" << endl - << " plhs[2] = mxCreateDoubleMatrix(" << equations.size() << ", " << variable_table.getDynJacobianColsNbr(computeJacobianExo)*variable_table.getDynJacobianColsNbr(computeJacobianExo) << ", mxREAL);" << endl - << " /* Create a C pointer to a copy of the output matrix g1. */" << endl - << " g2 = mxGetPr(plhs[2]);" << endl - << " }" << endl - << endl - << " /* Call the C subroutines. */" << endl - << " Dynamic(y, x, nb_row_x, params, it_, residual, g1, g2);" << endl - << "}" << endl; - mDynamicModelFile.close(); - } + << " plhs[1] = mxCreateDoubleMatrix(" << equations.size() << ", " << variable_table.getDynJacobianColsNbr(computeJacobianExo) << ", mxREAL);" << endl + << " /* Create a C pointer to a copy of the output matrix g1. */" << endl + << " g1 = mxGetPr(plhs[1]);" << endl + << " }" << endl + << endl + << " g2 = NULL;" << endl + << " if (nlhs >= 3)" << endl + << " {" << endl + << " /* Set the output pointer to the output matrix g2. */" << endl + << " plhs[2] = mxCreateDoubleMatrix(" << equations.size() << ", " << variable_table.getDynJacobianColsNbr(computeJacobianExo)*variable_table.getDynJacobianColsNbr(computeJacobianExo) << ", mxREAL);" << endl + << " /* Create a C pointer to a copy of the output matrix g1. */" << endl + << " g2 = mxGetPr(plhs[2]);" << endl + << " }" << endl + << endl + << " /* Call the C subroutines. */" << endl + << " Dynamic(y, x, nb_row_x, params, it_, residual, g1, g2);" << endl + << "}" << endl; + mDynamicModelFile.close(); +} void ModelTree::writeStaticModel(ostream &StaticOutput) const - { - ostringstream model_output; // Used for storing model equations - ostringstream jacobian_output; // Used for storing jacobian equations - ostringstream hessian_output; - ostringstream lsymetric; // For symmetric elements in hessian +{ + ostringstream model_output; // Used for storing model equations + ostringstream jacobian_output; // Used for storing jacobian equations + ostringstream hessian_output; + ostringstream lsymetric; // For symmetric elements in hessian - ExprNodeOutputType output_type = (mode == eDLLMode ? oCStaticModel : oMatlabStaticModel); + ExprNodeOutputType output_type = (mode == eDLLMode ? oCStaticModel : oMatlabStaticModel); - writeModelLocalVariables(model_output, output_type); + writeModelLocalVariables(model_output, output_type); - writeTemporaryTerms(model_output, output_type); + writeTemporaryTerms(model_output, output_type); - writeModelEquations(model_output, output_type); + writeModelEquations(model_output, output_type); - // Write Jacobian w.r. to endogenous only + // Write Jacobian w.r. to endogenous only + for (first_derivatives_type::const_iterator it = first_derivatives.begin(); + it != first_derivatives.end(); it++) + { + int eq = it->first.first; + int var = it->first.second; + NodeID d1 = it->second; + + if (variable_table.getType(var) == eEndogenous) + { + ostringstream g1; + g1 << " g1"; + matrixHelper(g1, eq, variable_table.getSymbolID(var), output_type); + + jacobian_output << g1.str() << "=" << g1.str() << "+"; + d1->writeOutput(jacobian_output, output_type, temporary_terms); + jacobian_output << ";" << endl; + } + } + + // Write Hessian w.r. to endogenous only + if (computeStaticHessian) + for (second_derivatives_type::const_iterator it = second_derivatives.begin(); + it != second_derivatives.end(); it++) + { + int eq = it->first.first; + int var1 = it->first.second.first; + int var2 = it->first.second.second; + NodeID d2 = it->second; + + // Keep only derivatives w.r. to endogenous variables + if (variable_table.getType(var1) == eEndogenous + && variable_table.getType(var2) == eEndogenous) + { + int id1 = variable_table.getSymbolID(var1); + int id2 = variable_table.getSymbolID(var2); + + int col_nb = id1*symbol_table.endo_nbr+id2; + int col_nb_sym = id2*symbol_table.endo_nbr+id1; + + hessian_output << " g2"; + matrixHelper(hessian_output, eq, col_nb, output_type); + hessian_output << " = "; + d2->writeOutput(hessian_output, output_type, temporary_terms); + hessian_output << ";" << endl; + + // Treating symetric elements + if (var1 != var2) + { + lsymetric << " g2"; + matrixHelper(lsymetric, eq, col_nb_sym, output_type); + lsymetric << " = " << "g2"; + matrixHelper(lsymetric, eq, col_nb, output_type); + lsymetric << ";" << endl; + } + } + } + + // Writing ouputs + if (mode != eDLLMode) + { + StaticOutput << "residual = zeros( " << equations.size() << ", 1);" << endl << endl + << "%" << endl + << "% Model equations" << endl + << "%" << endl + << endl + << model_output.str() + << "if ~isreal(residual)" << endl + << " residual = real(residual)+imag(residual).^2;" << endl + << "end" << endl + << "if nargout >= 2," << endl + << " g1 = zeros(" << equations.size() << ", " << symbol_table.endo_nbr << ");" << endl + << endl + << "%" << endl + << "% Jacobian matrix" << endl + << "%" << endl + << endl + << jacobian_output.str() + << " if ~isreal(g1)" << endl + << " g1 = real(g1)+2*imag(g1);" << endl + << " end" << endl + << "end" << endl; + if (computeStaticHessian) + { + StaticOutput << "if nargout >= 3,\n"; + // Writing initialization instruction for matrix g2 + int ncols = symbol_table.endo_nbr * symbol_table.endo_nbr; + StaticOutput << " g2 = sparse([],[],[], " << equations.size() << ", " << ncols << ", " << 5*ncols << ");" << endl + << endl + << "%" << endl + << "% Hessian matrix" << endl + << "%" << endl + << endl + << hessian_output.str() + << lsymetric.str() + << "end;" << endl; + } + } + else + { + StaticOutput << "void Static(double *y, double *x, double *params, double *residual, double *g1)" << endl + << "{" << endl + << " double lhs, rhs;" << endl + // Writing residual equations + << " /* Residual equations */" << endl + << " if (residual == NULL)" << endl + << " return;" << endl + << " else" << endl + << " {" << endl + << model_output.str() + // Writing Jacobian + << " /* Jacobian for endogenous variables without lag */" << endl + << " if (g1 == NULL)" << endl + << " return;" << endl + << " else" << endl + << " {" << endl + << jacobian_output.str() + << " }" << endl + << " }" << endl + << "}" << endl << endl; + } +} + +string +ModelTree::reform(const string name1) const +{ + string name=name1; + int pos = name.find("\\", 0); + while (pos >= 0) + { + if (name.substr(pos + 1, 1) != "\\") + { + name = name.insert(pos, "\\"); + pos++; + } + pos++; + pos = name.find("\\", pos); + } + return (name); +} + +void +ModelTree::Write_Inf_To_Bin_File(const string &dynamic_basename, const string &bin_basename, const int &num, + int &u_count_int, bool &file_open, bool is_two_boundaries) const +{ + int j; + std::ofstream SaveCode; + if (file_open) + SaveCode.open((bin_basename + ".bin").c_str(), ios::out | ios::in | ios::binary | ios ::ate ); + else + SaveCode.open((bin_basename + ".bin").c_str(), ios::out | ios::binary); + if (!SaveCode.is_open()) + { + cout << "Error : Can't open file \"" << bin_basename << ".bin\" for writing\n"; + exit(EXIT_FAILURE); + } + u_count_int=0; + for (int m=0;m<=block_triangular.ModelBlock->Block_List[num].Max_Lead+block_triangular.ModelBlock->Block_List[num].Max_Lag;m++) + { + int k1=m-block_triangular.ModelBlock->Block_List[num].Max_Lag; + for (j=0;jBlock_List[num].IM_lead_lag[m].size;j++) + { + int varr=block_triangular.ModelBlock->Block_List[num].IM_lead_lag[m].Var[j]+k1*block_triangular.ModelBlock->Block_List[num].Size; + int u=block_triangular.ModelBlock->Block_List[num].IM_lead_lag[m].u[j]; + int eqr1=block_triangular.ModelBlock->Block_List[num].IM_lead_lag[m].Equ[j]; + SaveCode.write(reinterpret_cast(&eqr1), sizeof(eqr1)); + SaveCode.write(reinterpret_cast(&varr), sizeof(varr)); + SaveCode.write(reinterpret_cast(&k1), sizeof(k1)); + SaveCode.write(reinterpret_cast(&u), sizeof(u)); + u_count_int++; + } + } + if(is_two_boundaries) + { + for (j=0;jBlock_List[num].Size;j++) + { + int eqr1=j; + int varr=block_triangular.ModelBlock->Block_List[num].Size*(block_triangular.periods + +block_triangular.incidencematrix.Model_Max_Lead_Endo); + int k1=0; + SaveCode.write(reinterpret_cast(&eqr1), sizeof(eqr1)); + SaveCode.write(reinterpret_cast(&varr), sizeof(varr)); + SaveCode.write(reinterpret_cast(&k1), sizeof(k1)); + SaveCode.write(reinterpret_cast(&eqr1), sizeof(eqr1)); + u_count_int++; + } + } + //cout << "u_count_int=" << u_count_int << "\n"; + for (j=0;jBlock_List[num].Size;j++) + { + int varr=block_triangular.ModelBlock->Block_List[num].Variable[j]; + SaveCode.write(reinterpret_cast(&varr), sizeof(varr)); + } + for (j=0;jBlock_List[num].Size;j++) + { + int eqr1=block_triangular.ModelBlock->Block_List[num].Equation[j]; + SaveCode.write(reinterpret_cast(&eqr1), sizeof(eqr1)); + } + SaveCode.close(); +} + +void +ModelTree::writeSparseStaticMFile(const string &static_basename, const string &basename, const int mode) const +{ + string filename; + ofstream mStaticModelFile; + ostringstream tmp, tmp1, tmp_eq; + int i, k, prev_Simulation_Type, ga_index = 1; + bool skip_head, open_par=false; + + chdir(basename.c_str()); + filename = static_basename + ".m"; + mStaticModelFile.open(filename.c_str(), ios::out | ios::binary); + if (!mStaticModelFile.is_open()) + { + cerr << "Error: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + mStaticModelFile << "%\n"; + mStaticModelFile << "% " << filename << " : Computes static model for Dynare\n"; + mStaticModelFile << "%\n"; + mStaticModelFile << "% Warning : this file is generated automatically by Dynare\n"; + mStaticModelFile << "% from model file (.mod)\n\n"; + mStaticModelFile << "%/\n"; + mStaticModelFile << "function [varargout] = " << static_basename << "(varargin)\n"; + mStaticModelFile << " global oo_ M_ options_ ys0_ ;\n"; + bool OK=true; + ostringstream tmp_output; + for (temporary_terms_type::const_iterator it = temporary_terms.begin(); + it != temporary_terms.end(); it++) + { + if (OK) + OK=false; + else + tmp_output << " "; + (*it)->writeOutput(tmp_output, oMatlabDynamicModel, temporary_terms); + } + if (tmp_output.str().length()>0) + mStaticModelFile << " global " << tmp_output.str() << " M_ ;\n"; + mStaticModelFile << " T_init=0;\n"; + tmp_output.str(""); + for (temporary_terms_type::const_iterator it = temporary_terms.begin(); + it != temporary_terms.end(); it++) + { + tmp_output << " "; + (*it)->writeOutput(tmp_output, oMatlabDynamicModel, temporary_terms); + tmp_output << "=T_init;\n"; + } + if (tmp_output.str().length()>0) + mStaticModelFile << tmp_output.str(); + + mStaticModelFile << " y_kmin=M_.maximum_lag;\n"; + mStaticModelFile << " y_kmax=M_.maximum_lead;\n"; + mStaticModelFile << " y_size=M_.endo_nbr;\n"; + + + mStaticModelFile << " if(length(varargin)>0)\n"; + mStaticModelFile << " %A simple evaluation of the static model\n"; + mStaticModelFile << " y=varargin{1}(:);\n"; + mStaticModelFile << " ys=y;\n"; + mStaticModelFile << " g1=[];\n"; + mStaticModelFile << " x=varargin{2}(:);\n"; + mStaticModelFile << " params=varargin{3}(:);\n"; + mStaticModelFile << " residual=zeros(1, " << symbol_table.endo_nbr << ");\n"; + prev_Simulation_Type=-1; + tmp.str(""); + tmp_eq.str(""); + for (i=0;iSize;i++) + { + k=block_triangular.ModelBlock->Block_List[i].Simulation_Type; + if ((BlockTriangular::BlockSim(prev_Simulation_Type)!=BlockTriangular::BlockSim(k)) && + ((prev_Simulation_Type==EVALUATE_FORWARD || prev_Simulation_Type==EVALUATE_BACKWARD || prev_Simulation_Type==EVALUATE_FORWARD_R || prev_Simulation_Type==EVALUATE_BACKWARD_R) + || (k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R))) + { + mStaticModelFile << " y_index_eq=[" << tmp_eq.str() << "];\n"; + tmp_eq.str(""); + mStaticModelFile << " y_index=[" << tmp.str() << "];\n"; + tmp.str(""); + mStaticModelFile << tmp1.str(); + tmp1.str(""); + } + for (int ik=0;ikBlock_List[i].Size;ik++) + { + tmp << " " << block_triangular.ModelBlock->Block_List[i].Variable[ik]+1; + tmp_eq << " " << block_triangular.ModelBlock->Block_List[i].Equation[ik]+1; + } + if (k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R) + { + if (i==block_triangular.ModelBlock->Size-1) + { + mStaticModelFile << " y_index_eq=[" << tmp_eq.str() << "];\n"; + tmp_eq.str(""); + mStaticModelFile << " y_index=[" << tmp.str() << "];\n"; + tmp.str(""); + mStaticModelFile << tmp1.str(); + tmp1.str(""); + } + } + + if (BlockTriangular::BlockSim(prev_Simulation_Type)==BlockTriangular::BlockSim(k) && + (k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R)) + skip_head=true; + else + skip_head=false; + + switch (k) + { + case EVALUATE_FORWARD: + case EVALUATE_BACKWARD: + case EVALUATE_FORWARD_R: + case EVALUATE_BACKWARD_R: + if (!skip_head) + { + ga_index = 1; + tmp1 << " [y, ga]=" << static_basename << "_" << i + 1 << "(y, x, params, 1);\n"; + tmp1 << " residual(y_index)=ys(y_index)-y(y_index);\n"; + tmp1 << " g1(y_index_eq, y_index) = ga;\n"; + } + else + ga_index++; + break; + case SOLVE_FORWARD_COMPLETE: + case SOLVE_BACKWARD_COMPLETE: + case SOLVE_FORWARD_SIMPLE: + case SOLVE_BACKWARD_SIMPLE: + case SOLVE_TWO_BOUNDARIES_COMPLETE: + case SOLVE_TWO_BOUNDARIES_SIMPLE: + mStaticModelFile << " y_index_eq = [" << tmp_eq.str() << "];\n"; + mStaticModelFile << " y_index = ["; + mStaticModelFile << tmp.str(); + mStaticModelFile << " ];\n"; + tmp.str(""); + tmp_eq.str(""); + mStaticModelFile << " [r, ga]=" << static_basename << "_" << i + 1 << "(y, x, params, 1);\n"; + mStaticModelFile << " g1(y_index_eq, y_index) = ga;\n"; + mStaticModelFile << " residual(y_index)=r;\n"; + break; + } + prev_Simulation_Type=k; + } + mStaticModelFile << " varargout{1}=residual';\n"; + mStaticModelFile << " varargout{2}=g1;\n"; + mStaticModelFile << " return;\n"; + mStaticModelFile << " end;\n"; + mStaticModelFile << " %The deterministic simulation of the block decomposed static model\n"; + mStaticModelFile << " periods=options_.periods;\n"; + mStaticModelFile << " maxit_=options_.maxit_;\n"; + mStaticModelFile << " solve_tolf=options_.solve_tolf;\n"; + mStaticModelFile << " y=oo_.steady_state;\n"; + mStaticModelFile << " x=oo_.exo_steady_state;\n"; + mStaticModelFile << " params=M_.params;\n"; + mStaticModelFile << " varargout{2}=1;\n"; + prev_Simulation_Type=-1; + int Blck_Num = 0; + for (i = 0;i < block_triangular.ModelBlock->Size;i++) + { + k = block_triangular.ModelBlock->Block_List[i].Simulation_Type; + if (BlockTriangular::BlockSim(prev_Simulation_Type)==BlockTriangular::BlockSim(k) && + (k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R)) + skip_head=true; + else + { + skip_head=false; + Blck_Num++; + } + if ((k == EVALUATE_FORWARD || k == EVALUATE_FORWARD_R || k == EVALUATE_BACKWARD || k == EVALUATE_BACKWARD_R) && (block_triangular.ModelBlock->Block_List[i].Size)) + { + if (!skip_head) + { + if (open_par) + { + mStaticModelFile << " end\n"; + } + mStaticModelFile << " y=" << static_basename << "_" << i + 1 << "(y, x, params, 0);\n"; + } + open_par=false; + } + else if ((k == SOLVE_FORWARD_SIMPLE || k == SOLVE_BACKWARD_SIMPLE || k == SOLVE_FORWARD_COMPLETE || k == SOLVE_BACKWARD_COMPLETE || k == SOLVE_TWO_BOUNDARIES_COMPLETE || k == SOLVE_TWO_BOUNDARIES_SIMPLE) && (block_triangular.ModelBlock->Block_List[i].Size)) + { + if (open_par) + { + mStaticModelFile << "end\n"; + } + open_par=false; + mStaticModelFile << " y_index=["; + for (int ik=0;ikBlock_List[i].Size;ik++) + { + mStaticModelFile << " " << block_triangular.ModelBlock->Block_List[i].Variable[ik]+1; + } + mStaticModelFile << " ];\n"; + + + mStaticModelFile << " g1=0;g2=0;g3=0;\n"; + + int nze, m; + for (nze=0,m=0;m<=block_triangular.ModelBlock->Block_List[i].Max_Lead+block_triangular.ModelBlock->Block_List[i].Max_Lag;m++) + nze+=block_triangular.ModelBlock->Block_List[i].IM_lead_lag[m].size; + mStaticModelFile << " y = solve_one_boundary('" << static_basename << "_" << i + 1 << "'" << + ", y, x, params, y_index, " << nze << + ", 1, " << block_triangular.ModelBlock->Block_List[i].is_linear << + ", " << Blck_Num << ", y_kmin, options_.maxit_, options_.solve_tolf, options_.slowc, options_.cutoff, options_.simulation_method, 1, 0, 0);\n"; + + } + prev_Simulation_Type=k; + } + if (open_par) + mStaticModelFile << " end;\n"; + mStaticModelFile << " oo_.steady_state = y;\n"; + mStaticModelFile << " if isempty(ys0_)\n"; + mStaticModelFile << " oo_.endo_simul(:,1:M_.maximum_lag) = oo_.steady_state * ones(1,M_.maximum_lag);\n"; + mStaticModelFile << " end;\n"; + mStaticModelFile << " disp('Steady State value');\n"; + mStaticModelFile << " disp([strcat(M_.endo_names,' : ') num2str(oo_.steady_state,'%f')]);\n"; + mStaticModelFile << " varargout{2}=0;\n"; + mStaticModelFile << " varargout{1}=oo_.steady_state;\n"; + mStaticModelFile << "return;\n"; + writeModelStaticEquationsOrdered_M(block_triangular.ModelBlock, static_basename); + mStaticModelFile.close(); + chdir(".."); +} + +void +ModelTree::writeSparseDynamicMFile(const string &dynamic_basename, const string &basename, const int mode) const +{ + string sp; + ofstream mDynamicModelFile; + ostringstream tmp, tmp1, tmp_eq; + int prev_Simulation_Type, tmp_i; + //SymbolicGaussElimination SGE; + bool OK; + chdir(basename.c_str()); + string filename = dynamic_basename + ".m"; + mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); + if (!mDynamicModelFile.is_open()) + { + cerr << "Error: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + mDynamicModelFile << "%\n"; + mDynamicModelFile << "% " << filename << " : Computes dynamic model for Dynare\n"; + mDynamicModelFile << "%\n"; + mDynamicModelFile << "% Warning : this file is generated automatically by Dynare\n"; + mDynamicModelFile << "% from model file (.mod)\n\n"; + mDynamicModelFile << "%/\n"; + + int i, k, Nb_SGE=0; + bool skip_head, open_par=false; + if (computeJacobian || computeJacobianExo || computeHessian) + { + mDynamicModelFile << "function [varargout] = " << dynamic_basename << "(varargin)\n"; + mDynamicModelFile << " global oo_ options_ M_ ;\n"; + mDynamicModelFile << " g2=[];g3=[];\n"; + //Temporary variables declaration + OK=true; + ostringstream tmp_output; + for (temporary_terms_type::const_iterator it = temporary_terms.begin(); + it != temporary_terms.end(); it++) + { + if (OK) + OK=false; + else + tmp_output << " "; + (*it)->writeOutput(tmp_output, oMatlabStaticModelSparse, temporary_terms); + } + if (tmp_output.str().length()>0) + mDynamicModelFile << " global " << tmp_output.str() << " M_ ;\n"; + + mDynamicModelFile << " T_init=zeros(1,options_.periods+M_.maximum_lag+M_.maximum_lead);\n"; + tmp_output.str(""); + for (temporary_terms_type::const_iterator it = temporary_terms.begin(); + it != temporary_terms.end(); it++) + { + tmp_output << " "; + (*it)->writeOutput(tmp_output, oMatlabDynamicModel, temporary_terms); + tmp_output << "=T_init;\n"; + } + if (tmp_output.str().length()>0) + mDynamicModelFile << tmp_output.str(); + + mDynamicModelFile << " y_kmin=M_.maximum_lag;\n"; + mDynamicModelFile << " y_kmax=M_.maximum_lead;\n"; + mDynamicModelFile << " y_size=M_.endo_nbr;\n"; + mDynamicModelFile << " if(length(varargin)>0)\n"; + mDynamicModelFile << " %it is a simple evaluation of the dynamic model for time _it\n"; + mDynamicModelFile << " params=varargin{3};\n"; + mDynamicModelFile << " it_=varargin{4};\n"; + /*i = symbol_table.endo_nbr*(variable_table.max_endo_lag+variable_table.max_endo_lead+1)+ + symbol_table.exo_nbr*(variable_table.max_exo_lag+variable_table.max_exo_lead+1); + mDynamicModelFile << " g1=spalloc(" << symbol_table.endo_nbr << ", " << i << ", " << i*symbol_table.endo_nbr << ");\n";*/ + mDynamicModelFile << " Per_u_=0;\n"; + mDynamicModelFile << " Per_y_=it_*y_size;\n"; + mDynamicModelFile << " y=varargin{1};\n"; + mDynamicModelFile << " ys=y(it_,:);\n"; + mDynamicModelFile << " x=varargin{2};\n"; + prev_Simulation_Type=-1; + tmp.str(""); + tmp_eq.str(""); + for (int count_call=1, i = 0;i < block_triangular.ModelBlock->Size;i++, count_call++) + { + k=block_triangular.ModelBlock->Block_List[i].Simulation_Type; + if ((BlockTriangular::BlockSim(prev_Simulation_Type)!=BlockTriangular::BlockSim(k)) && + ((prev_Simulation_Type==EVALUATE_FORWARD || prev_Simulation_Type==EVALUATE_BACKWARD || prev_Simulation_Type==EVALUATE_FORWARD_R || prev_Simulation_Type==EVALUATE_BACKWARD_R) + || (k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R))) + { + mDynamicModelFile << " y_index_eq=[" << tmp_eq.str() << "];\n"; + tmp_eq.str(""); + mDynamicModelFile << " y_index=[" << tmp.str() << "];\n"; + tmp.str(""); + mDynamicModelFile << tmp1.str(); + tmp1.str(""); + } + for (int ik=0;ikBlock_List[i].Size;ik++) + { + tmp << " " << block_triangular.ModelBlock->Block_List[i].Variable[ik]+1; + tmp_eq << " " << block_triangular.ModelBlock->Block_List[i].Equation[ik]+1; + } + if (k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R) + { + if (i==block_triangular.ModelBlock->Size-1) + { + mDynamicModelFile << " y_index_eq=[" << tmp_eq.str() << "];\n"; + tmp_eq.str(""); + mDynamicModelFile << " y_index=[" << tmp.str() << "];\n"; + tmp.str(""); + mDynamicModelFile << tmp1.str(); + tmp1.str(""); + } + } + if (BlockTriangular::BlockSim(prev_Simulation_Type)==BlockTriangular::BlockSim(k) && + (k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R)) + skip_head=true; + else + skip_head=false; + switch (k) + { + case EVALUATE_FORWARD: + case EVALUATE_BACKWARD: + case EVALUATE_FORWARD_R: + case EVALUATE_BACKWARD_R: + if (!skip_head) + { + tmp1 << " [y, dr(" << count_call << ").g1, dr(" << count_call << ").g2, dr(" << count_call << ").g3, dr(" << count_call << ").g1_x, dr(" << count_call << ").g1_o]=" << dynamic_basename << "_" << i + 1 << "(y, x, params, 1, it_-1, 1);\n"; + tmp1 << " residual(y_index_eq)=ys(y_index)-y(it_, y_index);\n"; + } + break; + case SOLVE_FORWARD_SIMPLE: + case SOLVE_BACKWARD_SIMPLE: + mDynamicModelFile << " y_index_eq = " << block_triangular.ModelBlock->Block_List[i].Equation[0]+1 << ";\n"; + mDynamicModelFile << " [r, dr(" << count_call << ").g1, dr(" << count_call << ").g2, dr(" << count_call << ").g3, dr(" << count_call << ").g1_x, dr(" << count_call << ").g1_o]=" << dynamic_basename << "_" << i + 1 << "(y, x, params, it_, 1);\n"; + mDynamicModelFile << " residual(y_index_eq)=r;\n"; + tmp_eq.str(""); + tmp.str(""); + break; + case SOLVE_FORWARD_COMPLETE: + case SOLVE_BACKWARD_COMPLETE: + mDynamicModelFile << " y_index_eq = [" << tmp_eq.str() << "];\n"; + mDynamicModelFile << " [r, dr(" << count_call << ").g1, dr(" << count_call << ").g2, dr(" << count_call << ").g3, dr(" << count_call << ").g1_x, dr(" << count_call << ").g1_o]=" << dynamic_basename << "_" << i + 1 << "(y, x, params, it_, 1);\n"; + mDynamicModelFile << " residual(y_index_eq)=r;\n"; + break; + case SOLVE_TWO_BOUNDARIES_COMPLETE: + case SOLVE_TWO_BOUNDARIES_SIMPLE: + int j; + mDynamicModelFile << " y_index_eq = [" << tmp_eq.str() << "];\n"; + tmp_i=block_triangular.ModelBlock->Block_List[i].Max_Lag_Endo+block_triangular.ModelBlock->Block_List[i].Max_Lead_Endo+1; + mDynamicModelFile << " y_index = ["; + for (j=0;jBlock_List[i].Size;ik++) + { + mDynamicModelFile << " " << block_triangular.ModelBlock->Block_List[i].Variable[ik]+1+j*symbol_table.endo_nbr; + } + int tmp_ix=block_triangular.ModelBlock->Block_List[i].Max_Lag_Exo+block_triangular.ModelBlock->Block_List[i].Max_Lead_Exo+1; + for (j=0;jBlock_List[i].nb_exo;ik++) + mDynamicModelFile << " " << block_triangular.ModelBlock->Block_List[i].Exogenous[ik]+1+j*symbol_table.exo_nbr+symbol_table.endo_nbr*tmp_i; + mDynamicModelFile << " ];\n"; + tmp.str(""); + tmp_eq.str(""); + //mDynamicModelFile << " ga = [];\n"; + j = block_triangular.ModelBlock->Block_List[i].Size*(block_triangular.ModelBlock->Block_List[i].Max_Lag_Endo+block_triangular.ModelBlock->Block_List[i].Max_Lead_Endo+1) + + block_triangular.ModelBlock->Block_List[i].nb_exo*(block_triangular.ModelBlock->Block_List[i].Max_Lag_Exo+block_triangular.ModelBlock->Block_List[i].Max_Lead_Exo+1); + /*mDynamicModelFile << " ga=spalloc(" << block_triangular.ModelBlock->Block_List[i].Size << ", " << j << ", " << + block_triangular.ModelBlock->Block_List[i].Size*j << ");\n";*/ + tmp_i=block_triangular.ModelBlock->Block_List[i].Max_Lag_Endo+block_triangular.ModelBlock->Block_List[i].Max_Lead_Endo+1; + mDynamicModelFile << " [r, dr(" << count_call << ").g1, dr(" << count_call << ").g2, dr(" << count_call << ").g3, b, dr(" << count_call << ").g1_x, dr(" << count_call << ").g1_o]=" << dynamic_basename << "_" << i + 1 << "(y, x, params, it_-" << variable_table.max_lag << ", 1, " << variable_table.max_lag << ", " << block_triangular.ModelBlock->Block_List[i].Size << ");\n"; + /*if(block_triangular.ModelBlock->Block_List[i].Max_Lag==variable_table.max_lag && block_triangular.ModelBlock->Block_List[i].Max_Lead==variable_table.max_lead) + mDynamicModelFile << " g1(y_index_eq,y_index) = ga;\n"; + else + mDynamicModelFile << " g1(y_index_eq,y_index) = ga(:," << 1+(variable_table.max_lag-block_triangular.ModelBlock->Block_List[i].Max_Lag)*block_triangular.ModelBlock->Block_List[i].Size << ":" << (variable_table.max_lag+1+block_triangular.ModelBlock->Block_List[i].Max_Lead)*block_triangular.ModelBlock->Block_List[i].Size << ");\n";*/ + mDynamicModelFile << " residual(y_index_eq)=r(:,M_.maximum_lag+1);\n"; + break; + } + prev_Simulation_Type=k; + } + if (tmp1.str().length()) + { + mDynamicModelFile << tmp1.str(); + tmp1.str(""); + } + mDynamicModelFile << " varargout{1}=residual;\n"; + mDynamicModelFile << " varargout{2}=dr;\n"; + mDynamicModelFile << " return;\n"; + mDynamicModelFile << " end;\n"; + mDynamicModelFile << " %it is the deterministic simulation of the block decomposed dynamic model\n"; + mDynamicModelFile << " if(options_.simulation_method==0)\n"; + mDynamicModelFile << " mthd='Sparse LU';\n"; + mDynamicModelFile << " elseif(options_.simulation_method==2)\n"; + mDynamicModelFile << " mthd='GMRES';\n"; + mDynamicModelFile << " elseif(options_.simulation_method==3)\n"; + mDynamicModelFile << " mthd='BICGSTAB';\n"; + mDynamicModelFile << " else\n"; + mDynamicModelFile << " mthd='UNKNOWN';\n"; + mDynamicModelFile << " end;\n"; + mDynamicModelFile << " disp (['-----------------------------------------------------']) ;\n"; + mDynamicModelFile << " disp (['MODEL SIMULATION: (method=' mthd ')']) ;\n"; + mDynamicModelFile << " fprintf('\\n') ;\n"; + mDynamicModelFile << " periods=options_.periods;\n"; + mDynamicModelFile << " maxit_=options_.maxit_;\n"; + mDynamicModelFile << " solve_tolf=options_.solve_tolf;\n"; + mDynamicModelFile << " y=oo_.endo_simul';\n"; + mDynamicModelFile << " x=oo_.exo_simul;\n"; + } + prev_Simulation_Type=-1; + mDynamicModelFile << " params=M_.params;\n"; + mDynamicModelFile << " oo_.deterministic_simulation.status = 0;\n"; + for (i = 0;i < block_triangular.ModelBlock->Size;i++) + { + k = block_triangular.ModelBlock->Block_List[i].Simulation_Type; + if (BlockTriangular::BlockSim(prev_Simulation_Type)==BlockTriangular::BlockSim(k) && + (k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R)) + skip_head=true; + else + skip_head=false; + if ((k == EVALUATE_FORWARD || k == EVALUATE_FORWARD_R) && (block_triangular.ModelBlock->Block_List[i].Size)) + { + if (!skip_head) + { + if (open_par) + { + mDynamicModelFile << " end\n"; + } + mDynamicModelFile << " oo_.deterministic_simulation.status = 1;\n"; + mDynamicModelFile << " oo_.deterministic_simulation.error = 0;\n"; + mDynamicModelFile << " oo_.deterministic_simulation.iterations = 0;\n"; + mDynamicModelFile << " if(isfield(oo_.deterministic_simulation,'block'))\n"; + mDynamicModelFile << " blck_num = length(oo_.deterministic_simulation.block)+1;\n"; + mDynamicModelFile << " else\n"; + mDynamicModelFile << " blck_num = 1;\n"; + mDynamicModelFile << " end;\n"; + mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).status = 1;\n"; + mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).error = 0;\n"; + mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).iterations = 0;\n"; + mDynamicModelFile << " g1=[];g2=[];g3=[];\n"; + //mDynamicModelFile << " for it_ = y_kmin+1:(periods+y_kmin)\n"; + mDynamicModelFile << " y=" << dynamic_basename << "_" << i + 1 << "(y, x, params, 0, y_kmin, periods);\n"; + } + //open_par=true; + } + else if ((k == EVALUATE_BACKWARD || k == EVALUATE_BACKWARD_R) && (block_triangular.ModelBlock->Block_List[i].Size)) + { + if (!skip_head) + { + if (open_par) + { + mDynamicModelFile << " end\n"; + } + mDynamicModelFile << " oo_.deterministic_simulation.status = 1;\n"; + mDynamicModelFile << " oo_.deterministic_simulation.error = 0;\n"; + mDynamicModelFile << " oo_.deterministic_simulation.iterations = 0;\n"; + mDynamicModelFile << " if(isfield(oo_.deterministic_simulation,'block'))\n"; + mDynamicModelFile << " blck_num = length(oo_.deterministic_simulation.block)+1;\n"; + mDynamicModelFile << " else\n"; + mDynamicModelFile << " blck_num = 1;\n"; + mDynamicModelFile << " end;\n"; + mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).status = 1;\n"; + mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).error = 0;\n"; + mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).iterations = 0;\n"; + mDynamicModelFile << " g1=[];g2=[];g3=[];\n"; + mDynamicModelFile << " " << dynamic_basename << "_" << i + 1 << "(y, x, params, 0, y_kmin, periods);\n"; + } + } + else if ((k == SOLVE_FORWARD_COMPLETE || k == SOLVE_FORWARD_SIMPLE) && (block_triangular.ModelBlock->Block_List[i].Size)) + { + if (open_par) + mDynamicModelFile << " end\n"; + open_par=false; + mDynamicModelFile << " g1=0;\n"; + mDynamicModelFile << " r=0;\n"; + tmp.str(""); + for (int ik=0;ikBlock_List[i].Size;ik++) + { + tmp << " " << block_triangular.ModelBlock->Block_List[i].Variable[ik]+1; + } + mDynamicModelFile << " y_index = [" << tmp.str() << "];\n"; + int nze, m; + for (nze=0,m=0;m<=block_triangular.ModelBlock->Block_List[i].Max_Lead+block_triangular.ModelBlock->Block_List[i].Max_Lag;m++) + nze+=block_triangular.ModelBlock->Block_List[i].IM_lead_lag[m].size; + mDynamicModelFile << " if(isfield(oo_.deterministic_simulation,'block'))\n"; + mDynamicModelFile << " blck_num = length(oo_.deterministic_simulation.block)+1;\n"; + mDynamicModelFile << " else\n"; + mDynamicModelFile << " blck_num = 1;\n"; + mDynamicModelFile << " end;\n"; + mDynamicModelFile << " y = solve_one_boundary('" << dynamic_basename << "_" << i + 1 << "'" << + ", y, x, params, y_index, " << nze << + ", options_.periods, " << block_triangular.ModelBlock->Block_List[i].is_linear << + ", blck_num, y_kmin, options_.maxit_, options_.solve_tolf, options_.slowc, options_.cutoff, options_.simulation_method, 1, 1, 0);\n"; + + } + else if ((k == SOLVE_BACKWARD_COMPLETE || k == SOLVE_BACKWARD_SIMPLE) && (block_triangular.ModelBlock->Block_List[i].Size)) + { + if (open_par) + mDynamicModelFile << " end\n"; + open_par=false; + mDynamicModelFile << " g1=0;\n"; + mDynamicModelFile << " r=0;\n"; + tmp.str(""); + for (int ik=0;ikBlock_List[i].Size;ik++) + { + tmp << " " << block_triangular.ModelBlock->Block_List[i].Variable[ik]+1; + } + mDynamicModelFile << " y_index = [" << tmp.str() << "];\n"; + int nze, m; + for (nze=0,m=0;m<=block_triangular.ModelBlock->Block_List[i].Max_Lead+block_triangular.ModelBlock->Block_List[i].Max_Lag;m++) + nze+=block_triangular.ModelBlock->Block_List[i].IM_lead_lag[m].size; + mDynamicModelFile << " if(isfield(oo_.deterministic_simulation,'block'))\n"; + mDynamicModelFile << " blck_num = length(oo_.deterministic_simulation.block)+1;\n"; + mDynamicModelFile << " else\n"; + mDynamicModelFile << " blck_num = 1;\n"; + mDynamicModelFile << " end;\n"; + mDynamicModelFile << " y = solve_one_boundary('" << dynamic_basename << "_" << i + 1 << "'" << + ", y, x, params, y_index, " << nze << + ", options_.periods, " << block_triangular.ModelBlock->Block_List[i].is_linear << + ", blck_num, y_kmin, options_.maxit_, options_.solve_tolf, options_.slowc, options_.cutoff, options_.simulation_method, 1, 1, 0);\n"; + } + else if ((k == SOLVE_TWO_BOUNDARIES_COMPLETE || k == SOLVE_TWO_BOUNDARIES_SIMPLE) && (block_triangular.ModelBlock->Block_List[i].Size)) + { + if (open_par) + mDynamicModelFile << " end\n"; + open_par=false; + Nb_SGE++; + int nze, m; + for (nze=0,m=0;m<=block_triangular.ModelBlock->Block_List[i].Max_Lead+block_triangular.ModelBlock->Block_List[i].Max_Lag;m++) + nze+=block_triangular.ModelBlock->Block_List[i].IM_lead_lag[m].size; + mDynamicModelFile << " y_index=["; + for (int ik=0;ikBlock_List[i].Size;ik++) + { + mDynamicModelFile << " " << block_triangular.ModelBlock->Block_List[i].Variable[ik]+1; + } + mDynamicModelFile << " ];\n"; + mDynamicModelFile << " if(isfield(oo_.deterministic_simulation,'block'))\n"; + mDynamicModelFile << " blck_num = length(oo_.deterministic_simulation.block)+1;\n"; + mDynamicModelFile << " else\n"; + mDynamicModelFile << " blck_num = 1;\n"; + mDynamicModelFile << " end;\n"; + mDynamicModelFile << " y = solve_two_boundaries('" << dynamic_basename << "_" << i + 1 << "'" << + ", y, x, params, y_index, " << nze << + ", options_.periods, " << block_triangular.ModelBlock->Block_List[i].Max_Lag << + ", " << block_triangular.ModelBlock->Block_List[i].Max_Lead << + ", " << block_triangular.ModelBlock->Block_List[i].is_linear << + ", blck_num, y_kmin, options_.maxit_, options_.solve_tolf, options_.slowc, options_.cutoff, options_.simulation_method);\n"; + + } + prev_Simulation_Type=k; + } + if (open_par) + mDynamicModelFile << " end;\n"; + open_par=false; + mDynamicModelFile << " oo_.endo_simul = y';\n"; + mDynamicModelFile << "return;\n"; + + mDynamicModelFile.close(); + + writeModelEquationsOrdered_M( block_triangular.ModelBlock, dynamic_basename); + + chdir(".."); +} + +void +ModelTree::writeDynamicModel(ostream &DynamicOutput) const +{ + ostringstream lsymetric; // Used when writing symetric elements in Hessian + ostringstream model_output; // Used for storing model equations + ostringstream jacobian_output; // Used for storing jacobian equations + ostringstream hessian_output; // Used for storing Hessian equations + ostringstream third_derivatives_output; + + ExprNodeOutputType output_type = (mode == eStandardMode || mode==eSparseMode ? oMatlabDynamicModel : oCDynamicModel); + + writeModelLocalVariables(model_output, output_type); + + writeTemporaryTerms(model_output, output_type); + + writeModelEquations(model_output, output_type); + + int nrows = equations.size(); + int nvars = variable_table.getDynJacobianColsNbr(computeJacobianExo); + int nvars_sq = nvars * nvars; + + // Writing Jacobian + if (computeJacobian || computeJacobianExo) for (first_derivatives_type::const_iterator it = first_derivatives.begin(); it != first_derivatives.end(); it++) { @@ -1688,11 +2486,11 @@ ModelTree::writeStaticModel(ostream &StaticOutput) const int var = it->first.second; NodeID d1 = it->second; - if (variable_table.getType(var) == eEndogenous) + if (computeJacobianExo || variable_table.getType(var) == eEndogenous) { ostringstream g1; g1 << " g1"; - matrixHelper(g1, eq, variable_table.getSymbolID(var), output_type); + matrixHelper(g1, eq, variable_table.getDynJacobianCol(var), output_type); jacobian_output << g1.str() << "=" << g1.str() << "+"; d1->writeOutput(jacobian_output, output_type, temporary_terms); @@ -1700,1192 +2498,394 @@ ModelTree::writeStaticModel(ostream &StaticOutput) const } } - // Write Hessian w.r. to endogenous only - if (computeStaticHessian) - for (second_derivatives_type::const_iterator it = second_derivatives.begin(); - it != second_derivatives.end(); it++) - { - int eq = it->first.first; - int var1 = it->first.second.first; - int var2 = it->first.second.second; - NodeID d2 = it->second; + // Writing Hessian + if (computeHessian) + for (second_derivatives_type::const_iterator it = second_derivatives.begin(); + it != second_derivatives.end(); it++) + { + int eq = it->first.first; + int var1 = it->first.second.first; + int var2 = it->first.second.second; + NodeID d2 = it->second; - // Keep only derivatives w.r. to endogenous variables - if (variable_table.getType(var1) == eEndogenous - && variable_table.getType(var2) == eEndogenous) + int id1 = variable_table.getDynJacobianCol(var1); + int id2 = variable_table.getDynJacobianCol(var2); + + int col_nb = id1*nvars+id2; + int col_nb_sym = id2*nvars+id1; + + hessian_output << " g2"; + matrixHelper(hessian_output, eq, col_nb, output_type); + hessian_output << " = "; + d2->writeOutput(hessian_output, output_type, temporary_terms); + hessian_output << ";" << endl; + + // Treating symetric elements + if (id1 != id2) + { + lsymetric << " g2"; + matrixHelper(lsymetric, eq, col_nb_sym, output_type); + lsymetric << " = " << "g2"; + matrixHelper(lsymetric, eq, col_nb, output_type); + lsymetric << ";" << endl; + } + } + + // Writing third derivatives + if (computeThirdDerivatives) + for (third_derivatives_type::const_iterator it = third_derivatives.begin(); + it != third_derivatives.end(); it++) + { + int eq = it->first.first; + int var1 = it->first.second.first; + int var2 = it->first.second.second.first; + int var3 = it->first.second.second.second; + NodeID d3 = it->second; + + int id1 = variable_table.getDynJacobianCol(var1); + int id2 = variable_table.getDynJacobianCol(var2); + int id3 = variable_table.getDynJacobianCol(var3); + + // Reference column number for the g3 matrix + int ref_col = id1 * nvars_sq + id2 * nvars + id3; + + third_derivatives_output << " g3"; + matrixHelper(third_derivatives_output, eq, ref_col, output_type); + third_derivatives_output << " = "; + d3->writeOutput(third_derivatives_output, output_type, temporary_terms); + third_derivatives_output << ";" << endl; + + // Compute the column numbers for the 5 other permutations of (id1,id2,id3) and store them in a set (to avoid duplicates if two indexes are equal) + set cols; + cols.insert(id1 * nvars_sq + id3 * nvars + id2); + cols.insert(id2 * nvars_sq + id1 * nvars + id3); + cols.insert(id2 * nvars_sq + id3 * nvars + id1); + cols.insert(id3 * nvars_sq + id1 * nvars + id2); + cols.insert(id3 * nvars_sq + id2 * nvars + id1); + + for (set::iterator it2 = cols.begin(); it2 != cols.end(); it2++) + if (*it2 != ref_col) { - int id1 = variable_table.getSymbolID(var1); - int id2 = variable_table.getSymbolID(var2); - - int col_nb = id1*symbol_table.endo_nbr+id2; - int col_nb_sym = id2*symbol_table.endo_nbr+id1; - - hessian_output << " g2"; - matrixHelper(hessian_output, eq, col_nb, output_type); - hessian_output << " = "; - d2->writeOutput(hessian_output, output_type, temporary_terms); - hessian_output << ";" << endl; - - // Treating symetric elements - if (var1 != var2) - { - lsymetric << " g2"; - matrixHelper(lsymetric, eq, col_nb_sym, output_type); - lsymetric << " = " << "g2"; - matrixHelper(lsymetric, eq, col_nb, output_type); - lsymetric << ";" << endl; - } + third_derivatives_output << " g3"; + matrixHelper(third_derivatives_output, eq, *it2, output_type); + third_derivatives_output << " = " << "g3"; + matrixHelper(third_derivatives_output, eq, ref_col, output_type); + third_derivatives_output << ";" << endl; } - } - - // Writing ouputs - if (mode != eDLLMode) - { - StaticOutput << "residual = zeros( " << equations.size() << ", 1);" << endl << endl - << "%" << endl - << "% Model equations" << endl - << "%" << endl - << endl - << model_output.str() - << "if ~isreal(residual)" << endl - << " residual = real(residual)+imag(residual).^2;" << endl - << "end" << endl - << "if nargout >= 2," << endl - << " g1 = zeros(" << equations.size() << ", " << symbol_table.endo_nbr << ");" << endl - << endl - << "%" << endl - << "% Jacobian matrix" << endl - << "%" << endl - << endl - << jacobian_output.str() - << " if ~isreal(g1)" << endl - << " g1 = real(g1)+2*imag(g1);" << endl - << " end" << endl - << "end" << endl; - if (computeStaticHessian) - { - StaticOutput << "if nargout >= 3,\n"; - // Writing initialization instruction for matrix g2 - int ncols = symbol_table.endo_nbr * symbol_table.endo_nbr; - StaticOutput << " g2 = sparse([],[],[], " << equations.size() << ", " << ncols << ", " << 5*ncols << ");" << endl - << endl - << "%" << endl - << "% Hessian matrix" << endl - << "%" << endl - << endl - << hessian_output.str() - << lsymetric.str() - << "end;" << endl; - } } - else - { - StaticOutput << "void Static(double *y, double *x, double *params, double *residual, double *g1)" << endl - << "{" << endl - << " double lhs, rhs;" << endl - // Writing residual equations - << " /* Residual equations */" << endl - << " if (residual == NULL)" << endl - << " return;" << endl - << " else" << endl - << " {" << endl - << model_output.str() - // Writing Jacobian - << " /* Jacobian for endogenous variables without lag */" << endl - << " if (g1 == NULL)" << endl - << " return;" << endl - << " else" << endl - << " {" << endl - << jacobian_output.str() - << " }" << endl - << " }" << endl - << "}" << endl << endl; - } - } -string -ModelTree::reform(const string name1) const - { - string name=name1; - int pos = name.find("\\", 0); - while (pos >= 0) - { - if (name.substr(pos + 1, 1) != "\\") - { - name = name.insert(pos, "\\"); - pos++; - } - pos++; - pos = name.find("\\", pos); - } - return (name); - } + if (mode == eStandardMode) + { + DynamicOutput << "%" << endl + << "% Model equations" << endl + << "%" << endl + << endl + << "residual = zeros(" << nrows << ", 1);" << endl + << model_output.str(); -void -ModelTree::Write_Inf_To_Bin_File(const string &dynamic_basename, const string &bin_basename, const int &num, - int &u_count_int, bool &file_open, bool is_two_boundaries) const - { - int j; - std::ofstream SaveCode; - if (file_open) - SaveCode.open((bin_basename + ".bin").c_str(), ios::out | ios::in | ios::binary | ios ::ate ); - else - SaveCode.open((bin_basename + ".bin").c_str(), ios::out | ios::binary); - if (!SaveCode.is_open()) - { - cout << "Error : Can't open file \"" << bin_basename << ".bin\" for writing\n"; - exit(EXIT_FAILURE); - } - u_count_int=0; - for (int m=0;m<=block_triangular.ModelBlock->Block_List[num].Max_Lead+block_triangular.ModelBlock->Block_List[num].Max_Lag;m++) - { - int k1=m-block_triangular.ModelBlock->Block_List[num].Max_Lag; - for (j=0;jBlock_List[num].IM_lead_lag[m].size;j++) - { - int varr=block_triangular.ModelBlock->Block_List[num].IM_lead_lag[m].Var[j]+k1*block_triangular.ModelBlock->Block_List[num].Size; - int u=block_triangular.ModelBlock->Block_List[num].IM_lead_lag[m].u[j]; - int eqr1=block_triangular.ModelBlock->Block_List[num].IM_lead_lag[m].Equ[j]; - SaveCode.write(reinterpret_cast(&eqr1), sizeof(eqr1)); - SaveCode.write(reinterpret_cast(&varr), sizeof(varr)); - SaveCode.write(reinterpret_cast(&k1), sizeof(k1)); - SaveCode.write(reinterpret_cast(&u), sizeof(u)); - u_count_int++; - } - } - if(is_two_boundaries) - { - for (j=0;jBlock_List[num].Size;j++) - { - int eqr1=j; - int varr=block_triangular.ModelBlock->Block_List[num].Size*(block_triangular.periods - +block_triangular.incidencematrix.Model_Max_Lead_Endo); - int k1=0; - SaveCode.write(reinterpret_cast(&eqr1), sizeof(eqr1)); - SaveCode.write(reinterpret_cast(&varr), sizeof(varr)); - SaveCode.write(reinterpret_cast(&k1), sizeof(k1)); - SaveCode.write(reinterpret_cast(&eqr1), sizeof(eqr1)); - u_count_int++; - } - } - //cout << "u_count_int=" << u_count_int << "\n"; - for (j=0;jBlock_List[num].Size;j++) - { - int varr=block_triangular.ModelBlock->Block_List[num].Variable[j]; - SaveCode.write(reinterpret_cast(&varr), sizeof(varr)); - } - for (j=0;jBlock_List[num].Size;j++) - { - int eqr1=block_triangular.ModelBlock->Block_List[num].Equation[j]; - SaveCode.write(reinterpret_cast(&eqr1), sizeof(eqr1)); - } - SaveCode.close(); - } - -void -ModelTree::writeSparseStaticMFile(const string &static_basename, const string &basename, const int mode) const - { - string filename; - ofstream mStaticModelFile; - ostringstream tmp, tmp1, tmp_eq; - int i, k, prev_Simulation_Type, ga_index = 1; - bool skip_head, open_par=false; - - chdir(basename.c_str()); - filename = static_basename + ".m"; - mStaticModelFile.open(filename.c_str(), ios::out | ios::binary); - if (!mStaticModelFile.is_open()) - { - cerr << "Error: Can't open file " << filename << " for writing" << endl; - exit(EXIT_FAILURE); - } - mStaticModelFile << "%\n"; - mStaticModelFile << "% " << filename << " : Computes static model for Dynare\n"; - mStaticModelFile << "%\n"; - mStaticModelFile << "% Warning : this file is generated automatically by Dynare\n"; - mStaticModelFile << "% from model file (.mod)\n\n"; - mStaticModelFile << "%/\n"; - mStaticModelFile << "function [varargout] = " << static_basename << "(varargin)\n"; - mStaticModelFile << " global oo_ M_ options_ ys0_ ;\n"; - bool OK=true; - ostringstream tmp_output; - for (temporary_terms_type::const_iterator it = temporary_terms.begin(); - it != temporary_terms.end(); it++) - { - if (OK) - OK=false; - else - tmp_output << " "; - (*it)->writeOutput(tmp_output, oMatlabDynamicModel, temporary_terms); - } - if (tmp_output.str().length()>0) - mStaticModelFile << " global " << tmp_output.str() << " M_ ;\n"; - mStaticModelFile << " T_init=0;\n"; - tmp_output.str(""); - for (temporary_terms_type::const_iterator it = temporary_terms.begin(); - it != temporary_terms.end(); it++) - { - tmp_output << " "; - (*it)->writeOutput(tmp_output, oMatlabDynamicModel, temporary_terms); - tmp_output << "=T_init;\n"; - } - if (tmp_output.str().length()>0) - mStaticModelFile << tmp_output.str(); - - mStaticModelFile << " y_kmin=M_.maximum_lag;\n"; - mStaticModelFile << " y_kmax=M_.maximum_lead;\n"; - mStaticModelFile << " y_size=M_.endo_nbr;\n"; - - - mStaticModelFile << " if(length(varargin)>0)\n"; - mStaticModelFile << " %A simple evaluation of the static model\n"; - mStaticModelFile << " y=varargin{1}(:);\n"; - mStaticModelFile << " ys=y;\n"; - mStaticModelFile << " g1=[];\n"; - mStaticModelFile << " x=varargin{2}(:);\n"; - mStaticModelFile << " params=varargin{3}(:);\n"; - mStaticModelFile << " residual=zeros(1, " << symbol_table.endo_nbr << ");\n"; - prev_Simulation_Type=-1; - tmp.str(""); - tmp_eq.str(""); - for (i=0;iSize;i++) - { - k=block_triangular.ModelBlock->Block_List[i].Simulation_Type; - if ((BlockTriangular::BlockSim(prev_Simulation_Type)!=BlockTriangular::BlockSim(k)) && - ((prev_Simulation_Type==EVALUATE_FORWARD || prev_Simulation_Type==EVALUATE_BACKWARD || prev_Simulation_Type==EVALUATE_FORWARD_R || prev_Simulation_Type==EVALUATE_BACKWARD_R) - || (k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R))) - { - mStaticModelFile << " y_index_eq=[" << tmp_eq.str() << "];\n"; - tmp_eq.str(""); - mStaticModelFile << " y_index=[" << tmp.str() << "];\n"; - tmp.str(""); - mStaticModelFile << tmp1.str(); - tmp1.str(""); - } - for (int ik=0;ikBlock_List[i].Size;ik++) - { - tmp << " " << block_triangular.ModelBlock->Block_List[i].Variable[ik]+1; - tmp_eq << " " << block_triangular.ModelBlock->Block_List[i].Equation[ik]+1; - } - if (k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R) - { - if (i==block_triangular.ModelBlock->Size-1) - { - mStaticModelFile << " y_index_eq=[" << tmp_eq.str() << "];\n"; - tmp_eq.str(""); - mStaticModelFile << " y_index=[" << tmp.str() << "];\n"; - tmp.str(""); - mStaticModelFile << tmp1.str(); - tmp1.str(""); - } - } - - if (BlockTriangular::BlockSim(prev_Simulation_Type)==BlockTriangular::BlockSim(k) && - (k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R)) - skip_head=true; - else - skip_head=false; - - switch (k) - { - case EVALUATE_FORWARD: - case EVALUATE_BACKWARD: - case EVALUATE_FORWARD_R: - case EVALUATE_BACKWARD_R: - if (!skip_head) - { - ga_index = 1; - tmp1 << " [y, ga]=" << static_basename << "_" << i + 1 << "(y, x, params, 1);\n"; - tmp1 << " residual(y_index)=ys(y_index)-y(y_index);\n"; - tmp1 << " g1(y_index_eq, y_index) = ga;\n"; - } - else - ga_index++; - break; - case SOLVE_FORWARD_COMPLETE: - case SOLVE_BACKWARD_COMPLETE: - case SOLVE_FORWARD_SIMPLE: - case SOLVE_BACKWARD_SIMPLE: - case SOLVE_TWO_BOUNDARIES_COMPLETE: - case SOLVE_TWO_BOUNDARIES_SIMPLE: - mStaticModelFile << " y_index_eq = [" << tmp_eq.str() << "];\n"; - mStaticModelFile << " y_index = ["; - mStaticModelFile << tmp.str(); - mStaticModelFile << " ];\n"; - tmp.str(""); - tmp_eq.str(""); - mStaticModelFile << " [r, ga]=" << static_basename << "_" << i + 1 << "(y, x, params, 1);\n"; - mStaticModelFile << " g1(y_index_eq, y_index) = ga;\n"; - mStaticModelFile << " residual(y_index)=r;\n"; - break; - } - prev_Simulation_Type=k; - } - mStaticModelFile << " varargout{1}=residual';\n"; - mStaticModelFile << " varargout{2}=g1;\n"; - mStaticModelFile << " return;\n"; - mStaticModelFile << " end;\n"; - mStaticModelFile << " %The deterministic simulation of the block decomposed static model\n"; - mStaticModelFile << " periods=options_.periods;\n"; - mStaticModelFile << " maxit_=options_.maxit_;\n"; - mStaticModelFile << " solve_tolf=options_.solve_tolf;\n"; - mStaticModelFile << " y=oo_.steady_state;\n"; - mStaticModelFile << " x=oo_.exo_steady_state;\n"; - mStaticModelFile << " params=M_.params;\n"; - mStaticModelFile << " varargout{2}=1;\n"; - prev_Simulation_Type=-1; - int Blck_Num = 0; - for (i = 0;i < block_triangular.ModelBlock->Size;i++) - { - k = block_triangular.ModelBlock->Block_List[i].Simulation_Type; - if (BlockTriangular::BlockSim(prev_Simulation_Type)==BlockTriangular::BlockSim(k) && - (k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R)) - skip_head=true; - else - { - skip_head=false; - Blck_Num++; - } - if ((k == EVALUATE_FORWARD || k == EVALUATE_FORWARD_R || k == EVALUATE_BACKWARD || k == EVALUATE_BACKWARD_R) && (block_triangular.ModelBlock->Block_List[i].Size)) - { - if (!skip_head) - { - if (open_par) - { - mStaticModelFile << " end\n"; - } - mStaticModelFile << " y=" << static_basename << "_" << i + 1 << "(y, x, params, 0);\n"; - } - open_par=false; - } - else if ((k == SOLVE_FORWARD_SIMPLE || k == SOLVE_BACKWARD_SIMPLE || k == SOLVE_FORWARD_COMPLETE || k == SOLVE_BACKWARD_COMPLETE || k == SOLVE_TWO_BOUNDARIES_COMPLETE || k == SOLVE_TWO_BOUNDARIES_SIMPLE) && (block_triangular.ModelBlock->Block_List[i].Size)) - { - if (open_par) - { - mStaticModelFile << "end\n"; - } - open_par=false; - mStaticModelFile << " y_index=["; - for (int ik=0;ikBlock_List[i].Size;ik++) - { - mStaticModelFile << " " << block_triangular.ModelBlock->Block_List[i].Variable[ik]+1; - } - mStaticModelFile << " ];\n"; - - - mStaticModelFile << " g1=0;g2=0;g3=0;\n"; - - int nze, m; - for (nze=0,m=0;m<=block_triangular.ModelBlock->Block_List[i].Max_Lead+block_triangular.ModelBlock->Block_List[i].Max_Lag;m++) - nze+=block_triangular.ModelBlock->Block_List[i].IM_lead_lag[m].size; - mStaticModelFile << " y = solve_one_boundary('" << static_basename << "_" << i + 1 << "'" << - ", y, x, params, y_index, " << nze << - ", 1, " << block_triangular.ModelBlock->Block_List[i].is_linear << - ", " << Blck_Num << ", y_kmin, options_.maxit_, options_.solve_tolf, options_.slowc, options_.cutoff, options_.simulation_method, 1, 0, 0);\n"; - - } - prev_Simulation_Type=k; - } - if (open_par) - mStaticModelFile << " end;\n"; - mStaticModelFile << " oo_.steady_state = y;\n"; - mStaticModelFile << " if isempty(ys0_)\n"; - mStaticModelFile << " oo_.endo_simul(:,1:M_.maximum_lag) = oo_.steady_state * ones(1,M_.maximum_lag);\n"; - mStaticModelFile << " end;\n"; - mStaticModelFile << " disp('Steady State value');\n"; - mStaticModelFile << " disp([strcat(M_.endo_names,' : ') num2str(oo_.steady_state,'%f')]);\n"; - mStaticModelFile << " varargout{2}=0;\n"; - mStaticModelFile << " varargout{1}=oo_.steady_state;\n"; - mStaticModelFile << "return;\n"; - writeModelStaticEquationsOrdered_M(block_triangular.ModelBlock, static_basename); - mStaticModelFile.close(); - chdir(".."); - } - -void -ModelTree::writeSparseDynamicMFile(const string &dynamic_basename, const string &basename, const int mode) const - { - string sp; - ofstream mDynamicModelFile; - ostringstream tmp, tmp1, tmp_eq; - int prev_Simulation_Type, tmp_i; - //SymbolicGaussElimination SGE; - bool OK; - chdir(basename.c_str()); - string filename = dynamic_basename + ".m"; - mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); - if (!mDynamicModelFile.is_open()) - { - cerr << "Error: Can't open file " << filename << " for writing" << endl; - exit(EXIT_FAILURE); - } - mDynamicModelFile << "%\n"; - mDynamicModelFile << "% " << filename << " : Computes dynamic model for Dynare\n"; - mDynamicModelFile << "%\n"; - mDynamicModelFile << "% Warning : this file is generated automatically by Dynare\n"; - mDynamicModelFile << "% from model file (.mod)\n\n"; - mDynamicModelFile << "%/\n"; - - int i, k, Nb_SGE=0; - bool skip_head, open_par=false; - if (computeJacobian || computeJacobianExo || computeHessian) - { - mDynamicModelFile << "function [varargout] = " << dynamic_basename << "(varargin)\n"; - mDynamicModelFile << " global oo_ options_ M_ ;\n"; - mDynamicModelFile << " g2=[];g3=[];\n"; - //Temporary variables declaration - OK=true; - ostringstream tmp_output; - for (temporary_terms_type::const_iterator it = temporary_terms.begin(); - it != temporary_terms.end(); it++) - { - if (OK) - OK=false; - else - tmp_output << " "; - (*it)->writeOutput(tmp_output, oMatlabStaticModelSparse, temporary_terms); - } - if (tmp_output.str().length()>0) - mDynamicModelFile << " global " << tmp_output.str() << " M_ ;\n"; - - mDynamicModelFile << " T_init=zeros(1,options_.periods+M_.maximum_lag+M_.maximum_lead);\n"; - tmp_output.str(""); - for (temporary_terms_type::const_iterator it = temporary_terms.begin(); - it != temporary_terms.end(); it++) - { - tmp_output << " "; - (*it)->writeOutput(tmp_output, oMatlabDynamicModel, temporary_terms); - tmp_output << "=T_init;\n"; - } - if (tmp_output.str().length()>0) - mDynamicModelFile << tmp_output.str(); - - mDynamicModelFile << " y_kmin=M_.maximum_lag;\n"; - mDynamicModelFile << " y_kmax=M_.maximum_lead;\n"; - mDynamicModelFile << " y_size=M_.endo_nbr;\n"; - mDynamicModelFile << " if(length(varargin)>0)\n"; - mDynamicModelFile << " %it is a simple evaluation of the dynamic model for time _it\n"; - mDynamicModelFile << " params=varargin{3};\n"; - mDynamicModelFile << " it_=varargin{4};\n"; - /*i = symbol_table.endo_nbr*(variable_table.max_endo_lag+variable_table.max_endo_lead+1)+ - symbol_table.exo_nbr*(variable_table.max_exo_lag+variable_table.max_exo_lead+1); - mDynamicModelFile << " g1=spalloc(" << symbol_table.endo_nbr << ", " << i << ", " << i*symbol_table.endo_nbr << ");\n";*/ - mDynamicModelFile << " Per_u_=0;\n"; - mDynamicModelFile << " Per_y_=it_*y_size;\n"; - mDynamicModelFile << " y=varargin{1};\n"; - mDynamicModelFile << " ys=y(it_,:);\n"; - mDynamicModelFile << " x=varargin{2};\n"; - prev_Simulation_Type=-1; - tmp.str(""); - tmp_eq.str(""); - for (int count_call=1, i = 0;i < block_triangular.ModelBlock->Size;i++, count_call++) - { - k=block_triangular.ModelBlock->Block_List[i].Simulation_Type; - if ((BlockTriangular::BlockSim(prev_Simulation_Type)!=BlockTriangular::BlockSim(k)) && - ((prev_Simulation_Type==EVALUATE_FORWARD || prev_Simulation_Type==EVALUATE_BACKWARD || prev_Simulation_Type==EVALUATE_FORWARD_R || prev_Simulation_Type==EVALUATE_BACKWARD_R) - || (k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R))) - { - mDynamicModelFile << " y_index_eq=[" << tmp_eq.str() << "];\n"; - tmp_eq.str(""); - mDynamicModelFile << " y_index=[" << tmp.str() << "];\n"; - tmp.str(""); - mDynamicModelFile << tmp1.str(); - tmp1.str(""); - } - for (int ik=0;ikBlock_List[i].Size;ik++) - { - tmp << " " << block_triangular.ModelBlock->Block_List[i].Variable[ik]+1; - tmp_eq << " " << block_triangular.ModelBlock->Block_List[i].Equation[ik]+1; - } - if (k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R) - { - if (i==block_triangular.ModelBlock->Size-1) - { - mDynamicModelFile << " y_index_eq=[" << tmp_eq.str() << "];\n"; - tmp_eq.str(""); - mDynamicModelFile << " y_index=[" << tmp.str() << "];\n"; - tmp.str(""); - mDynamicModelFile << tmp1.str(); - tmp1.str(""); - } - } - if (BlockTriangular::BlockSim(prev_Simulation_Type)==BlockTriangular::BlockSim(k) && - (k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R)) - skip_head=true; - else - skip_head=false; - switch (k) - { - case EVALUATE_FORWARD: - case EVALUATE_BACKWARD: - case EVALUATE_FORWARD_R: - case EVALUATE_BACKWARD_R: - if (!skip_head) - { - tmp1 << " [y, dr(" << count_call << ").g1, dr(" << count_call << ").g2, dr(" << count_call << ").g3, dr(" << count_call << ").g1_x, dr(" << count_call << ").g1_o]=" << dynamic_basename << "_" << i + 1 << "(y, x, params, 1, it_-1, 1);\n"; - tmp1 << " residual(y_index_eq)=ys(y_index)-y(it_, y_index);\n"; - } - break; - case SOLVE_FORWARD_SIMPLE: - case SOLVE_BACKWARD_SIMPLE: - mDynamicModelFile << " y_index_eq = " << block_triangular.ModelBlock->Block_List[i].Equation[0]+1 << ";\n"; - mDynamicModelFile << " [r, dr(" << count_call << ").g1, dr(" << count_call << ").g2, dr(" << count_call << ").g3, dr(" << count_call << ").g1_x, dr(" << count_call << ").g1_o]=" << dynamic_basename << "_" << i + 1 << "(y, x, params, it_, 1);\n"; - mDynamicModelFile << " residual(y_index_eq)=r;\n"; - tmp_eq.str(""); - tmp.str(""); - break; - case SOLVE_FORWARD_COMPLETE: - case SOLVE_BACKWARD_COMPLETE: - mDynamicModelFile << " y_index_eq = [" << tmp_eq.str() << "];\n"; - mDynamicModelFile << " [r, dr(" << count_call << ").g1, dr(" << count_call << ").g2, dr(" << count_call << ").g3, dr(" << count_call << ").g1_x, dr(" << count_call << ").g1_o]=" << dynamic_basename << "_" << i + 1 << "(y, x, params, it_, 1);\n"; - mDynamicModelFile << " residual(y_index_eq)=r;\n"; - break; - case SOLVE_TWO_BOUNDARIES_COMPLETE: - case SOLVE_TWO_BOUNDARIES_SIMPLE: - int j; - mDynamicModelFile << " y_index_eq = [" << tmp_eq.str() << "];\n"; - tmp_i=block_triangular.ModelBlock->Block_List[i].Max_Lag_Endo+block_triangular.ModelBlock->Block_List[i].Max_Lead_Endo+1; - mDynamicModelFile << " y_index = ["; - for (j=0;jBlock_List[i].Size;ik++) - { - mDynamicModelFile << " " << block_triangular.ModelBlock->Block_List[i].Variable[ik]+1+j*symbol_table.endo_nbr; - } - int tmp_ix=block_triangular.ModelBlock->Block_List[i].Max_Lag_Exo+block_triangular.ModelBlock->Block_List[i].Max_Lead_Exo+1; - for (j=0;jBlock_List[i].nb_exo;ik++) - mDynamicModelFile << " " << block_triangular.ModelBlock->Block_List[i].Exogenous[ik]+1+j*symbol_table.exo_nbr+symbol_table.endo_nbr*tmp_i; - mDynamicModelFile << " ];\n"; - tmp.str(""); - tmp_eq.str(""); - //mDynamicModelFile << " ga = [];\n"; - j = block_triangular.ModelBlock->Block_List[i].Size*(block_triangular.ModelBlock->Block_List[i].Max_Lag_Endo+block_triangular.ModelBlock->Block_List[i].Max_Lead_Endo+1) - + block_triangular.ModelBlock->Block_List[i].nb_exo*(block_triangular.ModelBlock->Block_List[i].Max_Lag_Exo+block_triangular.ModelBlock->Block_List[i].Max_Lead_Exo+1); - /*mDynamicModelFile << " ga=spalloc(" << block_triangular.ModelBlock->Block_List[i].Size << ", " << j << ", " << - block_triangular.ModelBlock->Block_List[i].Size*j << ");\n";*/ - tmp_i=block_triangular.ModelBlock->Block_List[i].Max_Lag_Endo+block_triangular.ModelBlock->Block_List[i].Max_Lead_Endo+1; - mDynamicModelFile << " [r, dr(" << count_call << ").g1, dr(" << count_call << ").g2, dr(" << count_call << ").g3, b, dr(" << count_call << ").g1_x, dr(" << count_call << ").g1_o]=" << dynamic_basename << "_" << i + 1 << "(y, x, params, it_-" << variable_table.max_lag << ", 1, " << variable_table.max_lag << ", " << block_triangular.ModelBlock->Block_List[i].Size << ");\n"; - /*if(block_triangular.ModelBlock->Block_List[i].Max_Lag==variable_table.max_lag && block_triangular.ModelBlock->Block_List[i].Max_Lead==variable_table.max_lead) - mDynamicModelFile << " g1(y_index_eq,y_index) = ga;\n"; - else - mDynamicModelFile << " g1(y_index_eq,y_index) = ga(:," << 1+(variable_table.max_lag-block_triangular.ModelBlock->Block_List[i].Max_Lag)*block_triangular.ModelBlock->Block_List[i].Size << ":" << (variable_table.max_lag+1+block_triangular.ModelBlock->Block_List[i].Max_Lead)*block_triangular.ModelBlock->Block_List[i].Size << ");\n";*/ - mDynamicModelFile << " residual(y_index_eq)=r(:,M_.maximum_lag+1);\n"; - break; - } - prev_Simulation_Type=k; - } - if (tmp1.str().length()) - { - mDynamicModelFile << tmp1.str(); - tmp1.str(""); - } - mDynamicModelFile << " varargout{1}=residual;\n"; - mDynamicModelFile << " varargout{2}=dr;\n"; - mDynamicModelFile << " return;\n"; - mDynamicModelFile << " end;\n"; - mDynamicModelFile << " %it is the deterministic simulation of the block decomposed dynamic model\n"; - mDynamicModelFile << " if(options_.simulation_method==0)\n"; - mDynamicModelFile << " mthd='Sparse LU';\n"; - mDynamicModelFile << " elseif(options_.simulation_method==2)\n"; - mDynamicModelFile << " mthd='GMRES';\n"; - mDynamicModelFile << " elseif(options_.simulation_method==3)\n"; - mDynamicModelFile << " mthd='BICGSTAB';\n"; - mDynamicModelFile << " else\n"; - mDynamicModelFile << " mthd='UNKNOWN';\n"; - mDynamicModelFile << " end;\n"; - mDynamicModelFile << " disp (['-----------------------------------------------------']) ;\n"; - mDynamicModelFile << " disp (['MODEL SIMULATION: (method=' mthd ')']) ;\n"; - mDynamicModelFile << " fprintf('\\n') ;\n"; - mDynamicModelFile << " periods=options_.periods;\n"; - mDynamicModelFile << " maxit_=options_.maxit_;\n"; - mDynamicModelFile << " solve_tolf=options_.solve_tolf;\n"; - mDynamicModelFile << " y=oo_.endo_simul';\n"; - mDynamicModelFile << " x=oo_.exo_simul;\n"; - } - prev_Simulation_Type=-1; - mDynamicModelFile << " params=M_.params;\n"; - mDynamicModelFile << " oo_.deterministic_simulation.status = 0;\n"; - for (i = 0;i < block_triangular.ModelBlock->Size;i++) - { - k = block_triangular.ModelBlock->Block_List[i].Simulation_Type; - if (BlockTriangular::BlockSim(prev_Simulation_Type)==BlockTriangular::BlockSim(k) && - (k==EVALUATE_FORWARD || k==EVALUATE_BACKWARD || k==EVALUATE_FORWARD_R || k==EVALUATE_BACKWARD_R)) - skip_head=true; - else - skip_head=false; - if ((k == EVALUATE_FORWARD || k == EVALUATE_FORWARD_R) && (block_triangular.ModelBlock->Block_List[i].Size)) - { - if (!skip_head) - { - if (open_par) - { - mDynamicModelFile << " end\n"; - } - mDynamicModelFile << " oo_.deterministic_simulation.status = 1;\n"; - mDynamicModelFile << " oo_.deterministic_simulation.error = 0;\n"; - mDynamicModelFile << " oo_.deterministic_simulation.iterations = 0;\n"; - mDynamicModelFile << " if(isfield(oo_.deterministic_simulation,'block'))\n"; - mDynamicModelFile << " blck_num = length(oo_.deterministic_simulation.block)+1;\n"; - mDynamicModelFile << " else\n"; - mDynamicModelFile << " blck_num = 1;\n"; - mDynamicModelFile << " end;\n"; - mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).status = 1;\n"; - mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).error = 0;\n"; - mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).iterations = 0;\n"; - mDynamicModelFile << " g1=[];g2=[];g3=[];\n"; - //mDynamicModelFile << " for it_ = y_kmin+1:(periods+y_kmin)\n"; - mDynamicModelFile << " y=" << dynamic_basename << "_" << i + 1 << "(y, x, params, 0, y_kmin, periods);\n"; - } - //open_par=true; - } - else if ((k == EVALUATE_BACKWARD || k == EVALUATE_BACKWARD_R) && (block_triangular.ModelBlock->Block_List[i].Size)) - { - if (!skip_head) - { - if (open_par) - { - mDynamicModelFile << " end\n"; - } - mDynamicModelFile << " oo_.deterministic_simulation.status = 1;\n"; - mDynamicModelFile << " oo_.deterministic_simulation.error = 0;\n"; - mDynamicModelFile << " oo_.deterministic_simulation.iterations = 0;\n"; - mDynamicModelFile << " if(isfield(oo_.deterministic_simulation,'block'))\n"; - mDynamicModelFile << " blck_num = length(oo_.deterministic_simulation.block)+1;\n"; - mDynamicModelFile << " else\n"; - mDynamicModelFile << " blck_num = 1;\n"; - mDynamicModelFile << " end;\n"; - mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).status = 1;\n"; - mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).error = 0;\n"; - mDynamicModelFile << " oo_.deterministic_simulation.block(blck_num).iterations = 0;\n"; - mDynamicModelFile << " g1=[];g2=[];g3=[];\n"; - mDynamicModelFile << " " << dynamic_basename << "_" << i + 1 << "(y, x, params, 0, y_kmin, periods);\n"; - } - } - else if ((k == SOLVE_FORWARD_COMPLETE || k == SOLVE_FORWARD_SIMPLE) && (block_triangular.ModelBlock->Block_List[i].Size)) - { - if (open_par) - mDynamicModelFile << " end\n"; - open_par=false; - mDynamicModelFile << " g1=0;\n"; - mDynamicModelFile << " r=0;\n"; - tmp.str(""); - for (int ik=0;ikBlock_List[i].Size;ik++) - { - tmp << " " << block_triangular.ModelBlock->Block_List[i].Variable[ik]+1; - } - mDynamicModelFile << " y_index = [" << tmp.str() << "];\n"; - int nze, m; - for (nze=0,m=0;m<=block_triangular.ModelBlock->Block_List[i].Max_Lead+block_triangular.ModelBlock->Block_List[i].Max_Lag;m++) - nze+=block_triangular.ModelBlock->Block_List[i].IM_lead_lag[m].size; - mDynamicModelFile << " if(isfield(oo_.deterministic_simulation,'block'))\n"; - mDynamicModelFile << " blck_num = length(oo_.deterministic_simulation.block)+1;\n"; - mDynamicModelFile << " else\n"; - mDynamicModelFile << " blck_num = 1;\n"; - mDynamicModelFile << " end;\n"; - mDynamicModelFile << " y = solve_one_boundary('" << dynamic_basename << "_" << i + 1 << "'" << - ", y, x, params, y_index, " << nze << - ", options_.periods, " << block_triangular.ModelBlock->Block_List[i].is_linear << - ", blck_num, y_kmin, options_.maxit_, options_.solve_tolf, options_.slowc, options_.cutoff, options_.simulation_method, 1, 1, 0);\n"; - - } - else if ((k == SOLVE_BACKWARD_COMPLETE || k == SOLVE_BACKWARD_SIMPLE) && (block_triangular.ModelBlock->Block_List[i].Size)) - { - if (open_par) - mDynamicModelFile << " end\n"; - open_par=false; - mDynamicModelFile << " g1=0;\n"; - mDynamicModelFile << " r=0;\n"; - tmp.str(""); - for (int ik=0;ikBlock_List[i].Size;ik++) - { - tmp << " " << block_triangular.ModelBlock->Block_List[i].Variable[ik]+1; - } - mDynamicModelFile << " y_index = [" << tmp.str() << "];\n"; - int nze, m; - for (nze=0,m=0;m<=block_triangular.ModelBlock->Block_List[i].Max_Lead+block_triangular.ModelBlock->Block_List[i].Max_Lag;m++) - nze+=block_triangular.ModelBlock->Block_List[i].IM_lead_lag[m].size; - mDynamicModelFile << " if(isfield(oo_.deterministic_simulation,'block'))\n"; - mDynamicModelFile << " blck_num = length(oo_.deterministic_simulation.block)+1;\n"; - mDynamicModelFile << " else\n"; - mDynamicModelFile << " blck_num = 1;\n"; - mDynamicModelFile << " end;\n"; - mDynamicModelFile << " y = solve_one_boundary('" << dynamic_basename << "_" << i + 1 << "'" << - ", y, x, params, y_index, " << nze << - ", options_.periods, " << block_triangular.ModelBlock->Block_List[i].is_linear << - ", blck_num, y_kmin, options_.maxit_, options_.solve_tolf, options_.slowc, options_.cutoff, options_.simulation_method, 1, 1, 0);\n"; - } - else if ((k == SOLVE_TWO_BOUNDARIES_COMPLETE || k == SOLVE_TWO_BOUNDARIES_SIMPLE) && (block_triangular.ModelBlock->Block_List[i].Size)) - { - if (open_par) - mDynamicModelFile << " end\n"; - open_par=false; - Nb_SGE++; - int nze, m; - for (nze=0,m=0;m<=block_triangular.ModelBlock->Block_List[i].Max_Lead+block_triangular.ModelBlock->Block_List[i].Max_Lag;m++) - nze+=block_triangular.ModelBlock->Block_List[i].IM_lead_lag[m].size; - mDynamicModelFile << " y_index=["; - for (int ik=0;ikBlock_List[i].Size;ik++) - { - mDynamicModelFile << " " << block_triangular.ModelBlock->Block_List[i].Variable[ik]+1; - } - mDynamicModelFile << " ];\n"; - mDynamicModelFile << " if(isfield(oo_.deterministic_simulation,'block'))\n"; - mDynamicModelFile << " blck_num = length(oo_.deterministic_simulation.block)+1;\n"; - mDynamicModelFile << " else\n"; - mDynamicModelFile << " blck_num = 1;\n"; - mDynamicModelFile << " end;\n"; - mDynamicModelFile << " y = solve_two_boundaries('" << dynamic_basename << "_" << i + 1 << "'" << - ", y, x, params, y_index, " << nze << - ", options_.periods, " << block_triangular.ModelBlock->Block_List[i].Max_Lag << - ", " << block_triangular.ModelBlock->Block_List[i].Max_Lead << - ", " << block_triangular.ModelBlock->Block_List[i].is_linear << - ", blck_num, y_kmin, options_.maxit_, options_.solve_tolf, options_.slowc, options_.cutoff, options_.simulation_method);\n"; - - } - prev_Simulation_Type=k; - } - if (open_par) - mDynamicModelFile << " end;\n"; - open_par=false; - mDynamicModelFile << " oo_.endo_simul = y';\n"; - mDynamicModelFile << "return;\n"; - - mDynamicModelFile.close(); - - writeModelEquationsOrdered_M( block_triangular.ModelBlock, dynamic_basename); - - chdir(".."); - } - -void -ModelTree::writeDynamicModel(ostream &DynamicOutput) const - { - ostringstream lsymetric; // Used when writing symetric elements in Hessian - ostringstream model_output; // Used for storing model equations - ostringstream jacobian_output; // Used for storing jacobian equations - ostringstream hessian_output; // Used for storing Hessian equations - ostringstream third_derivatives_output; - - ExprNodeOutputType output_type = (mode == eStandardMode || mode==eSparseMode ? oMatlabDynamicModel : oCDynamicModel); - - writeModelLocalVariables(model_output, output_type); - - writeTemporaryTerms(model_output, output_type); - - writeModelEquations(model_output, output_type); - - int nrows = equations.size(); - int nvars = variable_table.getDynJacobianColsNbr(computeJacobianExo); - int nvars_sq = nvars * nvars; - - // Writing Jacobian - if (computeJacobian || computeJacobianExo) - for (first_derivatives_type::const_iterator it = first_derivatives.begin(); - it != first_derivatives.end(); it++) + if (computeJacobian || computeJacobianExo) { - int eq = it->first.first; - int var = it->first.second; - NodeID d1 = it->second; - - if (computeJacobianExo || variable_table.getType(var) == eEndogenous) - { - ostringstream g1; - g1 << " g1"; - matrixHelper(g1, eq, variable_table.getDynJacobianCol(var), output_type); - - jacobian_output << g1.str() << "=" << g1.str() << "+"; - d1->writeOutput(jacobian_output, output_type, temporary_terms); - jacobian_output << ";" << endl; - } + // Writing initialization instruction for matrix g1 + DynamicOutput << "if nargout >= 2," << endl + << " g1 = zeros(" << nrows << ", " << nvars << ");" << endl + << endl + << "%" << endl + << "% Jacobian matrix" << endl + << "%" << endl + << endl + << jacobian_output.str() + << "end" << endl; } - - // Writing Hessian - if (computeHessian) - for (second_derivatives_type::const_iterator it = second_derivatives.begin(); - it != second_derivatives.end(); it++) + if (computeHessian) { - int eq = it->first.first; - int var1 = it->first.second.first; - int var2 = it->first.second.second; - NodeID d2 = it->second; - - int id1 = variable_table.getDynJacobianCol(var1); - int id2 = variable_table.getDynJacobianCol(var2); - - int col_nb = id1*nvars+id2; - int col_nb_sym = id2*nvars+id1; - - hessian_output << " g2"; - matrixHelper(hessian_output, eq, col_nb, output_type); - hessian_output << " = "; - d2->writeOutput(hessian_output, output_type, temporary_terms); - hessian_output << ";" << endl; - - // Treating symetric elements - if (id1 != id2) - { - lsymetric << " g2"; - matrixHelper(lsymetric, eq, col_nb_sym, output_type); - lsymetric << " = " << "g2"; - matrixHelper(lsymetric, eq, col_nb, output_type); - lsymetric << ";" << endl; - } + // Writing initialization instruction for matrix g2 + int ncols = nvars_sq; + DynamicOutput << "if nargout >= 3," << endl + << " g2 = sparse([],[],[], " << nrows << ", " << ncols << ", " << 5*ncols << ");" << endl + << endl + << "%" << endl + << "% Hessian matrix" << endl + << "%" << endl + << endl + << hessian_output.str() + << lsymetric.str() + << "end;" << endl; } - - // Writing third derivatives - if (computeThirdDerivatives) - for (third_derivatives_type::const_iterator it = third_derivatives.begin(); - it != third_derivatives.end(); it++) + if (computeThirdDerivatives) { - int eq = it->first.first; - int var1 = it->first.second.first; - int var2 = it->first.second.second.first; - int var3 = it->first.second.second.second; - NodeID d3 = it->second; - - int id1 = variable_table.getDynJacobianCol(var1); - int id2 = variable_table.getDynJacobianCol(var2); - int id3 = variable_table.getDynJacobianCol(var3); - - // Reference column number for the g3 matrix - int ref_col = id1 * nvars_sq + id2 * nvars + id3; - - third_derivatives_output << " g3"; - matrixHelper(third_derivatives_output, eq, ref_col, output_type); - third_derivatives_output << " = "; - d3->writeOutput(third_derivatives_output, output_type, temporary_terms); - third_derivatives_output << ";" << endl; - - // Compute the column numbers for the 5 other permutations of (id1,id2,id3) and store them in a set (to avoid duplicates if two indexes are equal) - set cols; - cols.insert(id1 * nvars_sq + id3 * nvars + id2); - cols.insert(id2 * nvars_sq + id1 * nvars + id3); - cols.insert(id2 * nvars_sq + id3 * nvars + id1); - cols.insert(id3 * nvars_sq + id1 * nvars + id2); - cols.insert(id3 * nvars_sq + id2 * nvars + id1); - - for (set::iterator it2 = cols.begin(); it2 != cols.end(); it2++) - if (*it2 != ref_col) - { - third_derivatives_output << " g3"; - matrixHelper(third_derivatives_output, eq, *it2, output_type); - third_derivatives_output << " = " << "g3"; - matrixHelper(third_derivatives_output, eq, ref_col, output_type); - third_derivatives_output << ";" << endl; - } + int ncols = nvars_sq * nvars; + DynamicOutput << "if nargout >= 4," << endl + << " g3 = sparse([],[],[], " << nrows << ", " << ncols << ", " << 5*ncols << ");" << endl + << endl + << "%" << endl + << "% Third order derivatives" << endl + << "%" << endl + << endl + << third_derivatives_output.str() + << "end;" << endl; } + } + else + { + DynamicOutput << "void Dynamic(double *y, double *x, int nb_row_x, double *params, int it_, double *residual, double *g1, double *g2)" << endl + << "{" << endl + << " double lhs, rhs;" << endl + << endl + << " /* Residual equations */" << endl + << model_output.str(); - if (mode == eStandardMode) - { - DynamicOutput << "%" << endl - << "% Model equations" << endl - << "%" << endl - << endl - << "residual = zeros(" << nrows << ", 1);" << endl - << model_output.str(); - - if (computeJacobian || computeJacobianExo) - { - // Writing initialization instruction for matrix g1 - DynamicOutput << "if nargout >= 2," << endl - << " g1 = zeros(" << nrows << ", " << nvars << ");" << endl - << endl - << "%" << endl - << "% Jacobian matrix" << endl - << "%" << endl - << endl - << jacobian_output.str() - << "end" << endl; - } - if (computeHessian) - { - // Writing initialization instruction for matrix g2 - int ncols = nvars_sq; - DynamicOutput << "if nargout >= 3," << endl - << " g2 = sparse([],[],[], " << nrows << ", " << ncols << ", " << 5*ncols << ");" << endl - << endl - << "%" << endl - << "% Hessian matrix" << endl - << "%" << endl - << endl - << hessian_output.str() - << lsymetric.str() - << "end;" << endl; - } - if (computeThirdDerivatives) - { - int ncols = nvars_sq * nvars; - DynamicOutput << "if nargout >= 4," << endl - << " g3 = sparse([],[],[], " << nrows << ", " << ncols << ", " << 5*ncols << ");" << endl - << endl - << "%" << endl - << "% Third order derivatives" << endl - << "%" << endl - << endl - << third_derivatives_output.str() - << "end;" << endl; - } - } - else - { - DynamicOutput << "void Dynamic(double *y, double *x, int nb_row_x, double *params, int it_, double *residual, double *g1, double *g2)" << endl - << "{" << endl - << " double lhs, rhs;" << endl - << endl - << " /* Residual equations */" << endl - << model_output.str(); - - if (computeJacobian || computeJacobianExo) - { - DynamicOutput << " /* Jacobian */" << endl - << " if (g1 == NULL)" << endl - << " return;" << endl - << " else" << endl - << " {" << endl - << jacobian_output.str() - << " }" << endl; - } - if (computeHessian) - { - DynamicOutput << " /* Hessian for endogenous and exogenous variables */" << endl - << " if (g2 == NULL)" << endl - << " return;" << endl - << " else" << endl - << " {" << endl - << hessian_output.str() - << lsymetric.str() - << " }" << endl; - } - DynamicOutput << "}" << endl << endl; - } - } + if (computeJacobian || computeJacobianExo) + { + DynamicOutput << " /* Jacobian */" << endl + << " if (g1 == NULL)" << endl + << " return;" << endl + << " else" << endl + << " {" << endl + << jacobian_output.str() + << " }" << endl; + } + if (computeHessian) + { + DynamicOutput << " /* Hessian for endogenous and exogenous variables */" << endl + << " if (g2 == NULL)" << endl + << " return;" << endl + << " else" << endl + << " {" << endl + << hessian_output.str() + << lsymetric.str() + << " }" << endl; + } + DynamicOutput << "}" << endl << endl; + } +} void ModelTree::writeOutput(ostream &output) const - { - /* Writing initialisation for M_.lead_lag_incidence matrix - M_.lead_lag_incidence is a matrix with as many columns as there are - endogenous variables and as many rows as there are periods in the - models (nbr of rows = M_.max_lag+M_.max_lead+1) +{ + /* Writing initialisation for M_.lead_lag_incidence matrix + M_.lead_lag_incidence is a matrix with as many columns as there are + endogenous variables and as many rows as there are periods in the + models (nbr of rows = M_.max_lag+M_.max_lead+1) - The matrix elements are equal to zero if a variable isn't present in the - model at a given period. - */ - output << "M_.lead_lag_incidence = ["; - // Loop on endogenous variables - int lag = 0; - for (int endoID = 0; endoID < symbol_table.endo_nbr; endoID++) - { - output << "\n\t"; - // Loop on periods - for (lag = -variable_table.max_endo_lag; lag <= variable_table.max_endo_lead; lag++) - { - // Print variableID if exists with current period, otherwise print 0 - try - { - int varID = variable_table.getID(eEndogenous, endoID, lag); - output << " " << variable_table.getDynJacobianCol(varID) + 1; - } - catch (VariableTable::UnknownVariableKeyException &e) - { - output << " 0"; - } - } - output << ";"; - } - output << "]';\n"; - //In case of sparse model, writes the block structure of the model + The matrix elements are equal to zero if a variable isn't present in the + model at a given period. + */ + output << "M_.lead_lag_incidence = ["; + // Loop on endogenous variables + int lag = 0; + for (int endoID = 0; endoID < symbol_table.endo_nbr; endoID++) + { + output << "\n\t"; + // Loop on periods + for (lag = -variable_table.max_endo_lag; lag <= variable_table.max_endo_lead; lag++) + { + // Print variableID if exists with current period, otherwise print 0 + try + { + int varID = variable_table.getID(eEndogenous, endoID, lag); + output << " " << variable_table.getDynJacobianCol(varID) + 1; + } + catch (VariableTable::UnknownVariableKeyException &e) + { + output << " 0"; + } + } + output << ";"; + } + output << "]';\n"; + //In case of sparse model, writes the block structure of the model - if (mode==eSparseMode || mode==eSparseDLLMode) - { - //int prev_Simulation_Type=-1; - //bool skip_the_head; - int k=0; - int count_lead_lag_incidence = 0; - int max_lead, max_lag, max_lag_endo, max_lead_endo, max_lag_exo, max_lead_exo; - for (int j = 0;j < block_triangular.ModelBlock->Size;j++) - { - //For a block composed of a single equation determines wether we have to evaluate or to solve the equation - //skip_the_head=false; - k++; - count_lead_lag_incidence = 0; - int Block_size=block_triangular.ModelBlock->Block_List[j].Size; - max_lag =block_triangular.ModelBlock->Block_List[j].Max_Lag ; - max_lead=block_triangular.ModelBlock->Block_List[j].Max_Lead; - max_lag_endo =block_triangular.ModelBlock->Block_List[j].Max_Lag_Endo ; - max_lead_endo=block_triangular.ModelBlock->Block_List[j].Max_Lead_Endo; - max_lag_exo =block_triangular.ModelBlock->Block_List[j].Max_Lag_Exo ; - max_lead_exo=block_triangular.ModelBlock->Block_List[j].Max_Lead_Exo; - bool evaluate=false; - vector exogenous; - vector::iterator it_exogenous; - exogenous.clear(); - ostringstream tmp_s, tmp_s_eq; - tmp_s.str(""); - tmp_s_eq.str(""); - for (int i=0;iBlock_List[j].Size;i++) + if (mode==eSparseMode || mode==eSparseDLLMode) + { + //int prev_Simulation_Type=-1; + //bool skip_the_head; + int k=0; + int count_lead_lag_incidence = 0; + int max_lead, max_lag, max_lag_endo, max_lead_endo, max_lag_exo, max_lead_exo; + for (int j = 0;j < block_triangular.ModelBlock->Size;j++) + { + //For a block composed of a single equation determines wether we have to evaluate or to solve the equation + //skip_the_head=false; + k++; + count_lead_lag_incidence = 0; + int Block_size=block_triangular.ModelBlock->Block_List[j].Size; + max_lag =block_triangular.ModelBlock->Block_List[j].Max_Lag ; + max_lead=block_triangular.ModelBlock->Block_List[j].Max_Lead; + max_lag_endo =block_triangular.ModelBlock->Block_List[j].Max_Lag_Endo ; + max_lead_endo=block_triangular.ModelBlock->Block_List[j].Max_Lead_Endo; + max_lag_exo =block_triangular.ModelBlock->Block_List[j].Max_Lag_Exo ; + max_lead_exo=block_triangular.ModelBlock->Block_List[j].Max_Lead_Exo; + bool evaluate=false; + vector exogenous; + vector::iterator it_exogenous; + exogenous.clear(); + ostringstream tmp_s, tmp_s_eq; + tmp_s.str(""); + tmp_s_eq.str(""); + for (int i=0;iBlock_List[j].Size;i++) + { + tmp_s << " " << block_triangular.ModelBlock->Block_List[j].Variable[i]+1; + tmp_s_eq << " " << block_triangular.ModelBlock->Block_List[j].Equation[i]+1; + } + for (int i=0;iBlock_List[j].nb_exo;i++) + { + int ii=block_triangular.ModelBlock->Block_List[j].Exogenous[i]; + for (it_exogenous=exogenous.begin();it_exogenous!=exogenous.end() && *it_exogenous!=ii;it_exogenous++) /*cout << "*it_exogenous=" << *it_exogenous << "\n"*/; + if (it_exogenous==exogenous.end() || exogenous.begin()==exogenous.end()) + exogenous.push_back(ii); + } + output << "M_.block_structure.block(" << k << ").num = " << j+1 << ";\n"; + output << "M_.block_structure.block(" << k << ").Simulation_Type = " << block_triangular.ModelBlock->Block_List[j].Simulation_Type << ";\n"; + output << "M_.block_structure.block(" << k << ").maximum_lag = " << max_lag << ";\n"; + output << "M_.block_structure.block(" << k << ").maximum_lead = " << max_lead << ";\n"; + output << "M_.block_structure.block(" << k << ").maximum_endo_lag = " << max_lag_endo << ";\n"; + output << "M_.block_structure.block(" << k << ").maximum_endo_lead = " << max_lead_endo << ";\n"; + output << "M_.block_structure.block(" << k << ").maximum_exo_lag = " << max_lag_exo << ";\n"; + output << "M_.block_structure.block(" << k << ").maximum_exo_lead = " << max_lead_exo << ";\n"; + output << "M_.block_structure.block(" << k << ").endo_nbr = " << Block_size << ";\n"; + output << "M_.block_structure.block(" << k << ").equation = [" << tmp_s_eq.str() << "];\n"; + output << "M_.block_structure.block(" << k << ").variable = [" << tmp_s.str() << "];\n"; + output << "M_.block_structure.block(" << k << ").exogenous = ["; + int i=0; + for (it_exogenous=exogenous.begin();it_exogenous!=exogenous.end();it_exogenous++) + if (*it_exogenous>=0) { - tmp_s << " " << block_triangular.ModelBlock->Block_List[j].Variable[i]+1; - tmp_s_eq << " " << block_triangular.ModelBlock->Block_List[j].Equation[i]+1; + output << " " << *it_exogenous+1; + i++; } - for (int i=0;iBlock_List[j].nb_exo;i++) - { - int ii=block_triangular.ModelBlock->Block_List[j].Exogenous[i]; - for (it_exogenous=exogenous.begin();it_exogenous!=exogenous.end() && *it_exogenous!=ii;it_exogenous++) /*cout << "*it_exogenous=" << *it_exogenous << "\n"*/; - if (it_exogenous==exogenous.end() || exogenous.begin()==exogenous.end()) - exogenous.push_back(ii); - } - output << "M_.block_structure.block(" << k << ").num = " << j+1 << ";\n"; - output << "M_.block_structure.block(" << k << ").Simulation_Type = " << block_triangular.ModelBlock->Block_List[j].Simulation_Type << ";\n"; - output << "M_.block_structure.block(" << k << ").maximum_lag = " << max_lag << ";\n"; - output << "M_.block_structure.block(" << k << ").maximum_lead = " << max_lead << ";\n"; - output << "M_.block_structure.block(" << k << ").maximum_endo_lag = " << max_lag_endo << ";\n"; - output << "M_.block_structure.block(" << k << ").maximum_endo_lead = " << max_lead_endo << ";\n"; - output << "M_.block_structure.block(" << k << ").maximum_exo_lag = " << max_lag_exo << ";\n"; - output << "M_.block_structure.block(" << k << ").maximum_exo_lead = " << max_lead_exo << ";\n"; - output << "M_.block_structure.block(" << k << ").endo_nbr = " << Block_size << ";\n"; - output << "M_.block_structure.block(" << k << ").equation = [" << tmp_s_eq.str() << "];\n"; - output << "M_.block_structure.block(" << k << ").variable = [" << tmp_s.str() << "];\n"; - output << "M_.block_structure.block(" << k << ").exogenous = ["; - int i=0; - for (it_exogenous=exogenous.begin();it_exogenous!=exogenous.end();it_exogenous++) - if (*it_exogenous>=0) + output << "];\n"; + output << "M_.block_structure.block(" << k << ").exo_nbr = " << i << ";\n"; + + output << "M_.block_structure.block(" << k << ").exo_det_nbr = " << block_triangular.ModelBlock->Block_List[j].nb_exo_det << ";\n"; + + tmp_s.str(""); + + bool done_IM=false; + if (!evaluate) + { + output << "M_.block_structure.block(" << k << ").lead_lag_incidence = [];\n"; + for (int l=-max_lag_endo;lBlock_List[j].Size;l_var++) - { - for (int l_equ=0;l_equBlock_List[j].Size;l_equ++) - if (tmp_IM[block_triangular.ModelBlock->Block_List[j].Equation[l_equ]*symbol_table.endo_nbr+block_triangular.ModelBlock->Block_List[j].Variable[l_var]]) - { - count_lead_lag_incidence++; - if (tmp_s.str().length()) - tmp_s << " "; - tmp_s << count_lead_lag_incidence; - done_IM=true; - break; - } - if (!done_IM) - tmp_s << " 0"; - done_IM=false; - } - output << "M_.block_structure.block(" << k << ").lead_lag_incidence = [ M_.block_structure.block(" << k << ").lead_lag_incidence; " << tmp_s.str() << "];\n"; - tmp_s.str(""); - } - } - } - else - { - bool done_some_where; - output << "M_.block_structure.block(" << k << ").lead_lag_incidence = [\n"; - for (int l=-max_lag_endo;lBlock_List[ii].Size;l_var++) + bool *tmp_IM; + tmp_IM=block_triangular.incidencematrix.Get_IM(l, eEndogenous); + if (tmp_IM) + { + for (int l_var=0;l_varBlock_List[j].Size;l_var++) + { + for (int l_equ=0;l_equBlock_List[j].Size;l_equ++) + if (tmp_IM[block_triangular.ModelBlock->Block_List[j].Equation[l_equ]*symbol_table.endo_nbr+block_triangular.ModelBlock->Block_List[j].Variable[l_var]]) { - for (int l_equ=0;l_equBlock_List[ii].Size;l_equ++) - if (tmp_IM[block_triangular.ModelBlock->Block_List[ii].Equation[l_equ]*symbol_table.endo_nbr+block_triangular.ModelBlock->Block_List[ii].Variable[l_var]]) - { - //if(not_increm && l==-max_lag) - count_lead_lag_incidence++; - not_increm=false; - if (tmp_s.str().length()) - tmp_s << " "; - //tmp_s << count_lead_lag_incidence+(l+max_lag)*Block_size; - tmp_s << count_lead_lag_incidence; - done_IM=true; - break; - } - if (!done_IM) - tmp_s << " 0"; - else - done_some_where = true; - done_IM=false; + count_lead_lag_incidence++; + if (tmp_s.str().length()) + tmp_s << " "; + tmp_s << count_lead_lag_incidence; + done_IM=true; + break; } - ii++; - } - output << tmp_s.str() << "\n"; - tmp_s.str(""); - } - } - output << "];\n"; - } + if (!done_IM) + tmp_s << " 0"; + done_IM=false; + } + output << "M_.block_structure.block(" << k << ").lead_lag_incidence = [ M_.block_structure.block(" << k << ").lead_lag_incidence; " << tmp_s.str() << "];\n"; + tmp_s.str(""); + } + } + } + else + { + bool done_some_where; + output << "M_.block_structure.block(" << k << ").lead_lag_incidence = [\n"; + for (int l=-max_lag_endo;lBlock_List[ii].Size;l_var++) + { + for (int l_equ=0;l_equBlock_List[ii].Size;l_equ++) + if (tmp_IM[block_triangular.ModelBlock->Block_List[ii].Equation[l_equ]*symbol_table.endo_nbr+block_triangular.ModelBlock->Block_List[ii].Variable[l_var]]) + { + //if(not_increm && l==-max_lag) + count_lead_lag_incidence++; + not_increm=false; + if (tmp_s.str().length()) + tmp_s << " "; + //tmp_s << count_lead_lag_incidence+(l+max_lag)*Block_size; + tmp_s << count_lead_lag_incidence; + done_IM=true; + break; + } + if (!done_IM) + tmp_s << " 0"; + else + done_some_where = true; + done_IM=false; + } + ii++; + } + output << tmp_s.str() << "\n"; + tmp_s.str(""); + } + } + output << "];\n"; + } - } - for (int j=-block_triangular.incidencematrix.Model_Max_Lag_Endo;j<=block_triangular.incidencematrix.Model_Max_Lead_Endo;j++) - { - bool* IM = block_triangular.incidencematrix.Get_IM(j, eEndogenous); - if (IM) - { - bool new_entry=true; - output << "M_.block_structure.incidence(" << block_triangular.incidencematrix.Model_Max_Lag_Endo+j+1 << ").lead_lag = " << j << ";\n"; - output << "M_.block_structure.incidence(" << block_triangular.incidencematrix.Model_Max_Lag_Endo+j+1 << ").sparse_IM = ["; - for (int i=0;ieval(mod_file->global_eval_context); - int symb_id = mod_file->symbol_table.getID(*name); - mod_file->global_eval_context[make_pair(symb_id, eParameter)] = val; - } - catch(ExprNode::EvalException &e) + double val = rhs->eval(mod_file->global_eval_context); + int symb_id = mod_file->symbol_table.getID(*name); + mod_file->global_eval_context[make_pair(symb_id, eParameter)] = val; + } + catch(ExprNode::EvalException &e) { } */ @@ -296,11 +296,11 @@ ParsingDriver::init_val(string *name, NodeID rhs) // Update global evaluation context /*try { - double val = rhs->eval(mod_file->global_eval_context); - int symb_id = mod_file->symbol_table.getID(*name); - mod_file->global_eval_context[make_pair(symb_id, type)] = val; + double val = rhs->eval(mod_file->global_eval_context); + int symb_id = mod_file->symbol_table.getID(*name); + mod_file->global_eval_context[make_pair(symb_id, type)] = val; } - catch(ExprNode::EvalException &e) + catch(ExprNode::EvalException &e) { } */ @@ -372,7 +372,7 @@ ParsingDriver::sparse_dll() { model_tree->mode = eSparseDLLMode; /*model_tree->block_triangular.init_incidence_matrix(mod_file->symbol_table.endo_nbr); - model_tree->block_triangular.init_incidence_matrix_X(mod_file->symbol_table.exo_nbr);*/ + model_tree->block_triangular.init_incidence_matrix_X(mod_file->symbol_table.exo_nbr);*/ } void @@ -380,7 +380,7 @@ ParsingDriver::sparse() { model_tree->mode = eSparseMode; /*model_tree->block_triangular.init_incidence_matrix(mod_file->symbol_table.endo_nbr); - model_tree->block_triangular.init_incidence_matrix_X(mod_file->symbol_table.exo_nbr);*/ + model_tree->block_triangular.init_incidence_matrix_X(mod_file->symbol_table.exo_nbr);*/ } void diff --git a/Shocks.cc b/Shocks.cc index 3961ccad..e7618adc 100644 --- a/Shocks.cc +++ b/Shocks.cc @@ -24,12 +24,12 @@ using namespace std; #include "Shocks.hh" AbstractShocksStatement::AbstractShocksStatement(bool mshocks_arg, - const det_shocks_type &det_shocks_arg, - const var_and_std_shocks_type &var_shocks_arg, - const var_and_std_shocks_type &std_shocks_arg, - const covar_and_corr_shocks_type &covar_shocks_arg, - const covar_and_corr_shocks_type &corr_shocks_arg, - const SymbolTable &symbol_table_arg) : + const det_shocks_type &det_shocks_arg, + const var_and_std_shocks_type &var_shocks_arg, + const var_and_std_shocks_type &std_shocks_arg, + const covar_and_corr_shocks_type &covar_shocks_arg, + const covar_and_corr_shocks_type &corr_shocks_arg, + const SymbolTable &symbol_table_arg) : mshocks(mshocks_arg), det_shocks(det_shocks_arg), var_shocks(var_shocks_arg), diff --git a/include/CodeInterpreter.hh b/include/CodeInterpreter.hh index 2786ca41..27e9edee 100644 --- a/include/CodeInterpreter.hh +++ b/include/CodeInterpreter.hh @@ -51,17 +51,17 @@ enum BlockType /*enum BlockSimulationType { - UNKNOWN = -1, //!< Unknown simulation type - EVALUATE_FORWARD = 0, //!< Simple evaluation, normalized variable on left-hand side, forward - EVALUATE_BACKWARD = 1, //!< Simple evaluation, normalized variable on left-hand side, backward - SOLVE_FORWARD_SIMPLE = 2, //!< Block of one equation, newton solver needed, forward - SOLVE_BACKWARD_SIMPLE = 3, //!< Block of one equation, newton solver needed, backward - SOLVE_TWO_BOUNDARIES_SIMPLE = 4, //!< Block of one equation, newton solver needed, forward & ackward - SOLVE_FORWARD_COMPLETE = 5, //!< Block of several equations, newton solver needed, forward - SOLVE_BACKWARD_COMPLETE = 6, //!< Block of several equations, newton solver needed, backward - SOLVE_TWO_BOUNDARIES_COMPLETE = 7, //!< Block of several equations, newton solver needed, forward and backwar - EVALUATE_FORWARD_R = 8, //!< Simple evaluation, normalized variable on right-hand side, forward - EVALUATE_BACKWARD_R = 9 //!< Simple evaluation, normalized variable on right-hand side, backward + UNKNOWN = -1, //!< Unknown simulation type + EVALUATE_FORWARD = 0, //!< Simple evaluation, normalized variable on left-hand side, forward + EVALUATE_BACKWARD = 1, //!< Simple evaluation, normalized variable on left-hand side, backward + SOLVE_FORWARD_SIMPLE = 2, //!< Block of one equation, newton solver needed, forward + SOLVE_BACKWARD_SIMPLE = 3, //!< Block of one equation, newton solver needed, backward + SOLVE_TWO_BOUNDARIES_SIMPLE = 4, //!< Block of one equation, newton solver needed, forward & ackward + SOLVE_FORWARD_COMPLETE = 5, //!< Block of several equations, newton solver needed, forward + SOLVE_BACKWARD_COMPLETE = 6, //!< Block of several equations, newton solver needed, backward + SOLVE_TWO_BOUNDARIES_COMPLETE = 7, //!< Block of several equations, newton solver needed, forward and backwar + EVALUATE_FORWARD_R = 8, //!< Simple evaluation, normalized variable on right-hand side, forward + EVALUATE_BACKWARD_R = 9 //!< Simple evaluation, normalized variable on right-hand side, backward }; */ enum BlockSimulationType diff --git a/include/ComputingTasks.hh b/include/ComputingTasks.hh index 0699bcae..7cb3d9ce 100644 --- a/include/ComputingTasks.hh +++ b/include/ComputingTasks.hh @@ -105,7 +105,7 @@ private: const OptionsList options_list; public: ForecastStatement(const SymbolList &symbol_list_arg, - const OptionsList &options_list_arg); + const OptionsList &options_list_arg); virtual void checkPass(ModFileStructure &mod_file_struct); virtual void writeOutput(ostream &output, const string &basename) const; }; diff --git a/include/ExprNode.hh b/include/ExprNode.hh index 762cb624..7f714a6c 100644 --- a/include/ExprNode.hh +++ b/include/ExprNode.hh @@ -66,10 +66,10 @@ typedef map, double> eval_context_type; /* Equal to 1 for Matlab langage, or to 0 for C language In Matlab, array indexes begin at 1, while they begin at 0 in C */ -#define OFFSET(output_type) ((output_type == oMatlabStaticModel) \ - || (output_type == oMatlabDynamicModel) \ - || (output_type == oMatlabOutsideModel) \ - || (output_type == oMatlabStaticModelSparse) \ +#define OFFSET(output_type) ((output_type == oMatlabStaticModel) \ + || (output_type == oMatlabDynamicModel) \ + || (output_type == oMatlabOutsideModel) \ + || (output_type == oMatlabStaticModelSparse) \ || (output_type == oMatlabDynamicModelSparse)) // Left parenthesis: '(' for Matlab, '[' for C @@ -123,7 +123,7 @@ public: //! Returns derivative w.r. to variable varID /*! Uses a symbolic a priori to pre-detect null derivatives, and caches the result for other derivatives (to avoid computing it several times) - For an equal node, returns the derivative of lhs minus rhs */ + For an equal node, returns the derivative of lhs minus rhs */ NodeID getDerivative(int varID); //! Returns precedence of node @@ -142,7 +142,7 @@ public: //! Computes the set of endogenous variables in the expression /*! Endogenous are stored as integer pairs of the form (symb_id, lag) - They are added to the set given in argument */ + They are added to the set given in argument */ virtual void collectEndogenous(set > &result) const = 0; virtual void collectExogenous(set > &result) const = 0; virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const = 0; @@ -206,12 +206,12 @@ public: virtual void collectEndogenous(set > &result) const; virtual void collectExogenous(set > &result) const; virtual void computeTemporaryTerms(map &reference_count, - temporary_terms_type &temporary_terms, - map > &first_occurence, - int Curr_block, - Model_Block *ModelBlock, - int equation, - map_idx_type &map_idx) const; + temporary_terms_type &temporary_terms, + map > &first_occurence, + int Curr_block, + Model_Block *ModelBlock, + int equation, + map_idx_type &map_idx) const; virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const; virtual double eval(const eval_context_type &eval_context) const throw (EvalException); virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type &map_idx) const; diff --git a/include/MatlabFile.hh b/include/MatlabFile.hh index d2975e3e..fcc15d77 100644 --- a/include/MatlabFile.hh +++ b/include/MatlabFile.hh @@ -17,10 +17,10 @@ * along with Dynare. If not, see . */ /* -Usefull documentation: Matlab 7 Mat-File Format ------------------------------------------------ -revision: October 2008 PDF only Rereleased for Version 7.7 (Release 2008b) -available at: http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/matfile_format.pdf + Usefull documentation: Matlab 7 Mat-File Format + ----------------------------------------------- + revision: October 2008 PDF only Rereleased for Version 7.7 (Release 2008b) + available at: http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/matfile_format.pdf */ #ifndef _MAT_FILE_HH @@ -42,50 +42,50 @@ using namespace std; enum Data_Type -{ - miINT8 = 1, //8 bit, signed - miUINT8 = 2, //8 bit, unsigned - miINT16 = 3, //16-bit, signed - miUINT16 = 4, //16-bit, unsigned - miINT32 = 5, //32-bit, signed - miUINT32 = 6, //32-bit, unsigned - miSINGLE = 7, //IEEEŽ 754 single format - miDOUBLE = 9, //IEEE 754 double format - miINT64 = 12, //64-bit, signed - miUINT64 = 13, //64-bit, unsigned - miMATRIX = 14, //MATLAB array - miCOMPRESSED = 15, //Compressed Data - miUTF8 = 16, //Unicode UTF-8 Encoded Character Data - miUTF16 = 17, //Unicode UTF-16 Encoded Character Data - miUTF32 = 18 //Unicode UTF-32 Encoded Character Data -}; + { + miINT8 = 1, //8 bit, signed + miUINT8 = 2, //8 bit, unsigned + miINT16 = 3, //16-bit, signed + miUINT16 = 4, //16-bit, unsigned + miINT32 = 5, //32-bit, signed + miUINT32 = 6, //32-bit, unsigned + miSINGLE = 7, //IEEEŽ 754 single format + miDOUBLE = 9, //IEEE 754 double format + miINT64 = 12, //64-bit, signed + miUINT64 = 13, //64-bit, unsigned + miMATRIX = 14, //MATLAB array + miCOMPRESSED = 15, //Compressed Data + miUTF8 = 16, //Unicode UTF-8 Encoded Character Data + miUTF16 = 17, //Unicode UTF-16 Encoded Character Data + miUTF32 = 18 //Unicode UTF-32 Encoded Character Data + }; enum Array_Type -{ - Cell_array = 1, - Structure_ = 2, - Object_ = 3, - Character_array = 4, - Sparse_array = 5, - Double_precision_array = 6, - Single_precision_array = 7, - Signed_integer_8_bit = 8, - Unsigned_integer_8_bit = 9, - Signed_integer_16_bit = 10, - Unsigned_integer_16_bit = 11, - Signed_integer_32_bit = 12, - Unsigned_integer_32_bit = 13 -}; + { + Cell_array = 1, + Structure_ = 2, + Object_ = 3, + Character_array = 4, + Sparse_array = 5, + Double_precision_array = 6, + Single_precision_array = 7, + Signed_integer_8_bit = 8, + Unsigned_integer_8_bit = 9, + Signed_integer_16_bit = 10, + Unsigned_integer_16_bit = 11, + Signed_integer_32_bit = 12, + Unsigned_integer_32_bit = 13 + }; enum Function_Returned_Type -{ - Numerical =1, - AlphaNumeric =2, - Matrix =3, - Compressed =4, - Unknown =5 -}; + { + Numerical =1, + AlphaNumeric =2, + Matrix =3, + Compressed =4, + Unknown =5 + }; class ArrayElem; class SimpleElem; @@ -106,7 +106,7 @@ typedef struct Header short int Version; char Edian_Indicator[2]; } -Header_t; + Header_t; typedef struct Data_Header { @@ -114,7 +114,7 @@ typedef struct Data_Header ShortInt DataType; uLongInt Number_of_Bytes; } -Data_Header_t; + Data_Header_t; typedef struct Array_Flag { @@ -124,7 +124,7 @@ typedef struct Array_Flag char undef1[2]; uLongInt nzmax; } -Array_Flag_t; + Array_Flag_t; typedef struct returned_ReadData @@ -143,13 +143,13 @@ typedef struct FlagStructure typedef struct CollectStruct { /*vector variable_name; - vector< vector > variable_double; - vector< vector > variable_string;*/ + vector< vector > variable_double; + vector< vector > variable_string;*/ string tmp_name; map > variable_string_name; map > variable_double_name; } -CollectStruct; + CollectStruct; typedef vector Array_Dimensions_t; @@ -161,23 +161,23 @@ typedef vector Array_Dimensions_t; class SimpleElem { public: - bool verbose; - vector VNumeric; - vector Vstr; - ArrayElem *array_elem; - int Type; - SimpleElem(); - virtual ~SimpleElem(); - virtual double ReadNum(char* InBuff, int* pBuff) const{return(0);}; - virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const{return(NULL);}; - virtual Data_Header_t ReadDataHeader(char* InBuff, int* pBuff) const; - virtual int size() const{cout << "oups\n";return(0);}; - void Print() const; - void Delete() const; - void Collect(const string &name, bool found, CollectStruct &collect_struct) const; - returned_ReadData_t ReadData(char* InBuff, int* pBuff) const; - returned_ReadData_t Get_Data_Class(Data_Header_t data_header) const; - void DataProceed(Data_Header data_header, char* InBuff, int* pBuff, FlagStructure flag); + bool verbose; + vector VNumeric; + vector Vstr; + ArrayElem *array_elem; + int Type; + SimpleElem(); + virtual ~SimpleElem(); + virtual double ReadNum(char* InBuff, int* pBuff) const{return(0);}; + virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const{return(NULL);}; + virtual Data_Header_t ReadDataHeader(char* InBuff, int* pBuff) const; + virtual int size() const{cout << "oups\n";return(0);}; + void Print() const; + void Delete() const; + void Collect(const string &name, bool found, CollectStruct &collect_struct) const; + returned_ReadData_t ReadData(char* InBuff, int* pBuff) const; + returned_ReadData_t Get_Data_Class(Data_Header_t data_header) const; + void DataProceed(Data_Header data_header, char* InBuff, int* pBuff, FlagStructure flag); }; @@ -208,53 +208,53 @@ public: class INT8 : public SimpleElem { public: - virtual int size() const {return(1);}; - virtual double ReadNum(char* InBuff, int* pBuff) const; - virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const {return(NULL);}; + virtual int size() const {return(1);}; + virtual double ReadNum(char* InBuff, int* pBuff) const; + virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const {return(NULL);}; }; class INT16 : public SimpleElem { public: - virtual int size() const {return(2);}; - virtual double ReadNum(char* InBuff, int* pBuff) const; - virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const {return(NULL);}; + virtual int size() const {return(2);}; + virtual double ReadNum(char* InBuff, int* pBuff) const; + virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const {return(NULL);}; }; class INT32 : public SimpleElem { public: - virtual int size() const {return(4);}; - virtual double ReadNum(char* InBuff, int* pBuff) const; - virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const {return(NULL);}; + virtual int size() const {return(4);}; + virtual double ReadNum(char* InBuff, int* pBuff) const; + virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const {return(NULL);}; }; class INT64 : public SimpleElem { public: - virtual int size() const {return(8);}; - virtual double ReadNum(char* InBuff, int* pBuff) const; - virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const {return(NULL);}; + virtual int size() const {return(8);}; + virtual double ReadNum(char* InBuff, int* pBuff) const; + virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const {return(NULL);}; }; class Single : public SimpleElem { public: - virtual int size() const {return(4);}; - virtual double ReadNum(char* InBuff, int* pBuff) const; - virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const {return(NULL);}; + virtual int size() const {return(4);}; + virtual double ReadNum(char* InBuff, int* pBuff) const; + virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const {return(NULL);}; }; class Double : public SimpleElem { public: - virtual int size() const {return(8);}; - virtual double ReadNum(char* InBuff, int* pBuff) const; - virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const {return(NULL);}; + virtual int size() const {return(8);}; + virtual double ReadNum(char* InBuff, int* pBuff) const; + virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const {return(NULL);}; }; //! Base class for Array Element in Mat-File @@ -294,7 +294,7 @@ public: class CellArray : public ArrayElem { - public: +public: virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; virtual void Print() const; virtual void Delete() const; @@ -303,7 +303,7 @@ class CellArray : public ArrayElem class Structure : public ArrayElem { - public: +public: virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; virtual void Print() const; virtual void Delete() const; @@ -312,7 +312,7 @@ class Structure : public ArrayElem class Object : public ArrayElem { - public: +public: virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; virtual void Print() const; virtual void Delete() const; @@ -321,7 +321,7 @@ class Object : public ArrayElem class CharacterArray : public ArrayElem { - public: +public: virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; virtual void Print() const; virtual void Delete() const; @@ -330,7 +330,7 @@ class CharacterArray : public ArrayElem class SparseArray : public ArrayElem { - public: +public: virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; virtual void Print() const; virtual void Delete() const; @@ -339,7 +339,7 @@ class SparseArray : public ArrayElem class DoublePrecisionArray : public ArrayElem { - public: +public: virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; virtual void Print() const; virtual void Delete() const; @@ -348,7 +348,7 @@ class DoublePrecisionArray : public ArrayElem class SinglePrecisionArray : public ArrayElem { - public: +public: virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; virtual void Print() const; virtual void Delete() const; @@ -357,7 +357,7 @@ class SinglePrecisionArray : public ArrayElem class Bit8SignedInteger : public ArrayElem { - public: +public: virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; virtual void Print() const; virtual void Delete() const; @@ -366,7 +366,7 @@ class Bit8SignedInteger : public ArrayElem class Bit8UnsignedInteger : public ArrayElem { - public: +public: virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; virtual void Print() const; virtual void Delete() const; @@ -375,7 +375,7 @@ class Bit8UnsignedInteger : public ArrayElem class Bit16SignedInteger : public ArrayElem { - public: +public: virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; virtual void Print() const; virtual void Delete() const; @@ -384,7 +384,7 @@ class Bit16SignedInteger : public ArrayElem class Bit16UnsignedInteger : public ArrayElem { - public: +public: virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; virtual void Print() const; virtual void Delete() const; @@ -393,7 +393,7 @@ class Bit16UnsignedInteger : public ArrayElem class Bit32SignedInteger : public ArrayElem { - public: +public: virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; virtual void Print() const; virtual void Delete() const; @@ -402,7 +402,7 @@ class Bit32SignedInteger : public ArrayElem class Bit32UnsignedInteger : public ArrayElem { - public: +public: virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; virtual void Print() const; virtual void Delete() const; @@ -412,7 +412,7 @@ class Bit32UnsignedInteger : public ArrayElem class MatlabFile { - public: +public: Header_t header; vector VSimpl; MatlabFile(); diff --git a/include/ModelTree.hh b/include/ModelTree.hh index 1409e8dd..b6bbc15d 100644 --- a/include/ModelTree.hh +++ b/include/ModelTree.hh @@ -160,7 +160,7 @@ public: bool computeThirdDerivatives; //! Execute computations (variable sorting + derivation) /*! You must set computeJacobian, computeJacobianExo, computeHessian, computeStaticHessian and computeThirdDerivatives to correct values before calling this function - \param no_tmp_terms if true, no temporary terms will be computed in the static and dynamic files */ + \param no_tmp_terms if true, no temporary terms will be computed in the static and dynamic files */ void computingPass(const eval_context_type &eval_context, bool no_tmp_terms); //! Writes model initialization and lead/lag incidence matrix to output void writeOutput(ostream &output) const; diff --git a/include/ParsingDriver.hh b/include/ParsingDriver.hh index d99875b8..97f790c4 100644 --- a/include/ParsingDriver.hh +++ b/include/ParsingDriver.hh @@ -49,7 +49,7 @@ using namespace std; //! The lexer class /*! Actually it was necessary to subclass the DynareFlexLexer class generated by Flex, - since the prototype for DynareFlexLexer::yylex() was not convenient. + since the prototype for DynareFlexLexer::yylex() was not convenient. */ class DynareFlex : public DynareFlexLexer { diff --git a/include/SymbolTable.hh b/include/SymbolTable.hh index 48554b39..62226104 100644 --- a/include/SymbolTable.hh +++ b/include/SymbolTable.hh @@ -31,11 +31,11 @@ using namespace std; //! Stores the symbol table /*! - A symbol is given by its name, and is internally represented by a pair (type, id). + A symbol is given by its name, and is internally represented by a pair (type, id). - There is a distinct sequence of ids for each type, so two symbol of different types can have the same id. + There is a distinct sequence of ids for each type, so two symbol of different types can have the same id. - Also manages a TeX name for each symbol, which by default is an empty string. + Also manages a TeX name for each symbol, which by default is an empty string. */ class SymbolTable { diff --git a/macro/MacroDriver.hh b/macro/MacroDriver.hh index 7d1e0fee..549c6573 100644 --- a/macro/MacroDriver.hh +++ b/macro/MacroDriver.hh @@ -44,7 +44,7 @@ using namespace std; //! The lexer class /*! Actually it was necessary to subclass the MacroFlexLexer class generated by Flex, - since the prototype for MacroFlexLexer::yylex() was not convenient. + since the prototype for MacroFlexLexer::yylex() was not convenient. */ class MacroFlex : public MacroFlexLexer { @@ -128,8 +128,8 @@ private: //! Iterates over the loop body /*! If loop is terminated, return false and do nothing. - Otherwise, set loop variable to its new value (through driver.iter_loop()), - and initialise a new scanning context with the loop body */ + Otherwise, set loop variable to its new value (through driver.iter_loop()), + and initialise a new scanning context with the loop body */ bool iter_loop(MacroDriver &driver, Macro::parser::location_type *yylloc); public: MacroFlex(istream* in, ostream* out, bool no_line_macro_arg); diff --git a/macro/MacroValue.hh b/macro/MacroValue.hh index da2df145..11eba487 100644 --- a/macro/MacroValue.hh +++ b/macro/MacroValue.hh @@ -150,8 +150,8 @@ public: virtual const MacroValue *in(const MacroValue *array) const throw (TypeError); //! Creates a integer range /*! Arguments must be of type IntMV. - Returns an integer array containing all integers between mv1 and mv2. - If mv2 < mv1, constructs the range in decreasing order. + Returns an integer array containing all integers between mv1 and mv2. + If mv2 < mv1, constructs the range in decreasing order. */ static const MacroValue *new_range(MacroDriver &driver, const MacroValue *mv1, const MacroValue *mv2) throw (TypeError); }; @@ -209,8 +209,8 @@ public: virtual const MacroValue *operator!=(const MacroValue &mv) const throw (TypeError); //! Subscripting operator /*! Argument must be an ArrayMV. Indexes begin at 1. - If argument is a one-element array, returns an IntMV or StringMV. - Otherwise returns an array. */ + If argument is a one-element array, returns an IntMV or StringMV. + Otherwise returns an array. */ virtual const MacroValue *operator[](const MacroValue &mv) const throw (TypeError, OutOfBoundsError); //! Returns a string containing the concatenation of string representations of elements virtual string toString() const;