2008-02-03 11:28:36 +01:00
/*
2009-01-21 15:39:24 +01:00
* Copyright ( C ) 2003 - 2009 Dynare Team
2008-02-03 11:28:36 +01:00
*
* This file is part of Dynare .
*
* Dynare is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* Dynare is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with Dynare . If not , see < http : //www.gnu.org/licenses/>.
*/
# ifndef _MODELTREE_HH
# define _MODELTREE_HH
using namespace std ;
# include <string>
# include <vector>
2009-09-30 17:10:31 +02:00
# include <deque>
2008-02-03 11:28:36 +01:00
# include <map>
# include <ostream>
# include "DataTree.hh"
2009-04-14 16:39:53 +02:00
//! Shared code for static and dynamic models
2008-02-03 11:28:36 +01:00
class ModelTree : public DataTree
{
2009-04-14 16:39:53 +02:00
protected :
2009-09-30 17:10:31 +02:00
//! Stores declared and generated auxiliary equations
2008-02-03 11:28:36 +01:00
vector < BinaryOpNode * > equations ;
2009-09-30 17:10:31 +02:00
//! Only stores generated auxiliary equations, in an order meaningful for evaluation
deque < BinaryOpNode * > aux_equations ;
2009-09-02 16:37:59 +02:00
//! Stores equation tags
vector < pair < int , pair < string , string > > > equation_tags ;
2009-05-12 22:32:27 +02:00
//! Number of non-zero derivatives
int NNZDerivatives [ 3 ] ;
2008-02-03 11:28:36 +01:00
typedef map < pair < int , int > , NodeID > first_derivatives_type ;
//! First order derivatives
/*! First index is equation number, second is variable w.r. to which is computed the derivative.
Only non - null derivatives are stored in the map .
2009-04-17 18:26:23 +02:00
Variable indices are those of the getDerivID ( ) method .
2008-02-03 11:28:36 +01:00
*/
first_derivatives_type first_derivatives ;
typedef map < pair < int , pair < int , int > > , NodeID > second_derivatives_type ;
//! Second order derivatives
/*! First index is equation number, second and third are variables w.r. to which is computed the derivative.
Only non - null derivatives are stored in the map .
Contains only second order derivatives where var1 > = var2 ( for obvious symmetry reasons ) .
2009-04-17 18:26:23 +02:00
Variable indices are those of the getDerivID ( ) method .
2008-02-03 11:28:36 +01:00
*/
second_derivatives_type second_derivatives ;
typedef map < pair < int , pair < int , pair < int , int > > > , NodeID > third_derivatives_type ;
//! Third order derivatives
/*! First index is equation number, second, third and fourth are variables w.r. to which is computed the derivative.
Only non - null derivatives are stored in the map .
Contains only third order derivatives where var1 > = var2 > = var3 ( for obvious symmetry reasons ) .
2009-04-17 18:26:23 +02:00
Variable indices are those of the getDerivID ( ) method .
2008-02-03 11:28:36 +01:00
*/
third_derivatives_type third_derivatives ;
//! Temporary terms (those which will be noted Txxxx)
temporary_terms_type temporary_terms ;
2009-04-20 12:48:54 +02:00
//! Computes 1st derivatives
/*! \param vars the derivation IDs w.r. to which compute the derivatives */
void computeJacobian ( const set < int > & vars ) ;
//! Computes 2nd derivatives
/*! \param vars the derivation IDs w.r. to which derive the 1st derivatives */
void computeHessian ( const set < int > & vars ) ;
//! Computes 3rd derivatives
/*! \param vars the derivation IDs w.r. to which derive the 2nd derivatives */
void computeThirdDerivatives ( const set < int > & vars ) ;
2008-02-03 11:28:36 +01:00
//! Write derivative of an equation w.r. to a variable
2009-02-27 13:19:25 +01:00
void writeDerivative ( ostream & output , int eq , int symb_id , int lag , ExprNodeOutputType output_type , const temporary_terms_type & temporary_terms ) const ;
2009-04-20 12:48:54 +02:00
//! Computes temporary terms (for all equations and derivatives)
2009-07-07 16:20:48 +02:00
void computeTemporaryTerms ( bool is_matlab ) ;
2008-02-03 11:28:36 +01:00
//! Writes temporary terms
2009-04-20 15:58:15 +02:00
void writeTemporaryTerms ( const temporary_terms_type & tt , ostream & output , ExprNodeOutputType output_type ) const ;
2008-02-03 11:28:36 +01:00
//! Writes model local variables
/*! No temporary term is used in the output, so that local parameters declarations can be safely put before temporary terms declaration in the output files */
void writeModelLocalVariables ( ostream & output , ExprNodeOutputType output_type ) const ;
//! Writes model equations
void writeModelEquations ( ostream & output , ExprNodeOutputType output_type ) const ;
2009-04-30 15:14:33 +02:00
//! Writes LaTeX model file
void writeLatexModelFile ( const string & filename , ExprNodeOutputType output_type ) const ;
2008-02-03 11:28:36 +01:00
public :
ModelTree ( SymbolTable & symbol_table_arg , NumericalConstants & num_constants ) ;
//! Declare a node as an equation of the model
void addEquation ( NodeID eq ) ;
2009-09-02 16:37:59 +02:00
//! Adds tags to equation number i
void addEquationTags ( int i , const string & key , const string & value ) ;
2009-09-30 17:10:31 +02:00
//! Declare a node as an auxiliary equation of the model, adding it at the end of the list of auxiliary equations
void addAuxEquation ( NodeID eq ) ;
2008-10-17 19:21:22 +02:00
//! Returns the number of equations in the model
2008-02-03 11:28:36 +01:00
int equation_number ( ) const ;
} ;
# endif