2008-01-11 14:42:14 +01:00
/*
2010-02-22 17:33:38 +01:00
* Copyright ( C ) 2007 - 2010 Dynare Team
2008-01-11 14:42:14 +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/>.
*/
2007-01-09 20:00:05 +01:00
# ifndef _EXPR_NODE_HH
# define _EXPR_NODE_HH
using namespace std ;
# include <set>
# include <map>
2007-03-09 18:27:46 +01:00
# include <vector>
2009-07-06 12:36:36 +02:00
# include <ostream>
2007-10-04 00:01:08 +02:00
2007-12-19 16:16:43 +01:00
# include "SymbolTable.hh"
2007-10-04 00:01:08 +02:00
# include "CodeInterpreter.hh"
2010-02-22 17:33:38 +01:00
# include "ExternalFunctionsTable.hh"
2007-01-09 20:00:05 +01:00
class DataTree ;
2009-09-30 17:10:31 +02:00
class VariableNode ;
class BinaryOpNode ;
2007-01-09 20:00:05 +01:00
typedef class ExprNode * NodeID ;
2008-06-28 13:20:45 +02:00
struct Model_Block ;
2007-02-22 00:28:16 +01:00
2007-01-09 20:00:05 +01:00
struct ExprNodeLess ;
//! Type for set of temporary terms
/*! They are ordered by index number thanks to ExprNodeLess */
typedef set < NodeID , ExprNodeLess > temporary_terms_type ;
2009-04-17 18:26:23 +02:00
2009-12-16 14:21:31 +01:00
//! set of temporary terms used in a block
typedef set < int > temporary_terms_inuse_type ;
2009-12-16 18:13:23 +01:00
typedef map < int , int > map_idx_type ;
2007-01-09 20:00:05 +01:00
2009-04-30 15:14:33 +02:00
//! Type for evaluation contexts
/*! The key is a symbol id. Lags are assumed to be null */
typedef map < int , double > eval_context_type ;
2010-03-04 16:40:07 +01:00
//! Type for tracking first/second derivative functions that have already been written as temporary terms
typedef map < pair < int , vector < NodeID > > , int > deriv_node_temp_terms_type ;
2007-03-06 18:14:35 +01:00
//! Possible types of output when writing ExprNode(s)
enum ExprNodeOutputType
{
2010-03-09 16:40:52 +01:00
oMatlabStaticModel , //!< Matlab code, static model declarations
oMatlabDynamicModel , //!< Matlab code, dynamic model declarations
oMatlabStaticModelSparse , //!< Matlab code, static block decomposed mode declaration
oMatlabDynamicModelSparse , //!< Matlab code, dynamic block decomposed mode declaration
oCDynamicModel , //!< C code, dynamic model declarations
oMatlabOutsideModel , //!< Matlab code, outside model block (for example in initval)
oLatexStaticModel , //!< LaTeX code, static model declarations
oLatexDynamicModel , //!< LaTeX code, dynamic model declarations
oLatexDynamicSteadyStateOperator , //!< LaTeX code, dynamic model steady state declarations
oMatlabDynamicSteadyStateOperator , //!< Matlab code, dynamic model steady state declarations
2009-12-16 18:13:23 +01:00
oMatlabDynamicModelSparseSteadyStateOperator , //!< Matlab code, dynamic block decomposed model steady state declarations
2010-04-23 18:39:07 +02:00
oMatlabDynamicModelSparseLocalTemporaryTerms , //!< Matlab code, dynamic block decomposed model local temporary_terms
oSteadyStateFile //!< Matlab code, in the generated steady state file
2007-03-06 18:14:35 +01:00
} ;
2009-04-30 15:14:33 +02:00
# define IS_MATLAB(output_type) ((output_type) == oMatlabStaticModel \
| | ( output_type ) = = oMatlabDynamicModel \
| | ( output_type ) = = oMatlabOutsideModel \
| | ( output_type ) = = oMatlabStaticModelSparse \
2009-09-10 22:09:16 +02:00
| | ( output_type ) = = oMatlabDynamicModelSparse \
2009-12-16 14:21:31 +01:00
| | ( output_type ) = = oMatlabDynamicModelSparseLocalTemporaryTerms \
2009-12-16 18:13:23 +01:00
| | ( output_type ) = = oMatlabDynamicSteadyStateOperator \
2010-04-23 18:39:07 +02:00
| | ( output_type ) = = oMatlabDynamicModelSparseSteadyStateOperator \
| | ( output_type ) = = oSteadyStateFile )
2009-04-30 15:14:33 +02:00
2009-07-07 16:20:48 +02:00
# define IS_C(output_type) ((output_type) == oCDynamicModel)
2009-04-30 15:14:33 +02:00
# define IS_LATEX(output_type) ((output_type) == oLatexStaticModel \
2009-12-16 18:13:23 +01:00
| | ( output_type ) = = oLatexDynamicModel \
| | ( output_type ) = = oLatexDynamicSteadyStateOperator )
2007-03-09 18:27:46 +01:00
2009-04-30 15:14:33 +02:00
/* Equal to 1 for Matlab langage, or to 0 for C language. Not defined for LaTeX.
2007-10-04 00:01:08 +02:00
In Matlab , array indexes begin at 1 , while they begin at 0 in C */
2009-04-30 15:14:33 +02:00
# define ARRAY_SUBSCRIPT_OFFSET(output_type) ((int) IS_MATLAB(output_type))
2007-03-06 18:14:35 +01:00
2009-04-30 15:14:33 +02:00
// Left and right array subscript delimiters: '(' and ')' for Matlab, '[' and ']' for C
# define LEFT_ARRAY_SUBSCRIPT(output_type) (IS_MATLAB(output_type) ? '(' : '[')
# define RIGHT_ARRAY_SUBSCRIPT(output_type) (IS_MATLAB(output_type) ? ')' : ']')
2007-03-06 18:14:35 +01:00
2009-04-30 15:14:33 +02:00
// Left and right parentheses
# define LEFT_PAR(output_type) (IS_LATEX(output_type) ? "\\left(" : "(")
# define RIGHT_PAR(output_type) (IS_LATEX(output_type) ? "\\right)" : ")")
2007-03-06 18:14:35 +01:00
// Computing cost above which a node can be declared a temporary term
# define MIN_COST_MATLAB (40*90)
# define MIN_COST_C (40*4)
2009-04-29 16:16:14 +02:00
# define MIN_COST(is_matlab) ((is_matlab) ? MIN_COST_MATLAB : MIN_COST_C)
2007-03-06 18:14:35 +01:00
2007-01-09 20:00:05 +01:00
//! Base class for expression nodes
class ExprNode
{
friend class DataTree ;
2009-04-14 16:39:53 +02:00
friend class DynamicModel ;
2009-12-16 14:21:31 +01:00
friend class StaticModel ;
2010-01-22 11:03:29 +01:00
friend class ModelTree ;
2007-01-09 20:00:05 +01:00
friend class ExprNodeLess ;
friend class NumConstNode ;
friend class VariableNode ;
friend class UnaryOpNode ;
friend class BinaryOpNode ;
2007-11-11 16:24:50 +01:00
friend class TrinaryOpNode ;
2010-02-22 17:33:38 +01:00
friend class ExternalFunctionNode ;
2007-01-09 20:00:05 +01:00
private :
2009-04-17 18:26:23 +02:00
//! Computes derivative w.r. to a derivation ID (but doesn't store it in derivatives map)
2007-01-09 20:00:05 +01:00
/*! You shoud use getDerivative() to get the benefit of symbolic a priori and of caching */
2009-04-17 18:26:23 +02:00
virtual NodeID computeDerivative ( int deriv_id ) = 0 ;
2007-01-09 20:00:05 +01:00
protected :
//! Reference to the enclosing DataTree
DataTree & datatree ;
//! Index number
int idx ;
2009-09-30 17:10:31 +02:00
//! Is the data member non_null_derivatives initialized ?
bool preparedForDerivation ;
2009-04-17 18:26:23 +02:00
//! Set of derivation IDs with respect to which the derivative is potentially non-null
2007-01-09 20:00:05 +01:00
set < int > non_null_derivatives ;
//! Used for caching of first order derivatives (when non-null)
map < int , NodeID > derivatives ;
//! Cost of computing current node
/*! Nodes included in temporary_terms are considered having a null cost */
2007-03-06 18:14:35 +01:00
virtual int cost ( const temporary_terms_type & temporary_terms , bool is_matlab ) const ;
2007-01-09 20:00:05 +01:00
public :
ExprNode ( DataTree & datatree_arg ) ;
virtual ~ ExprNode ( ) ;
2009-09-30 17:10:31 +02:00
//! Initializes data member non_null_derivatives
virtual void prepareForDerivation ( ) = 0 ;
2009-04-17 18:26:23 +02:00
//! Returns derivative w.r. to derivation ID
2007-01-09 20:00:05 +01:00
/*! Uses a symbolic a priori to pre-detect null derivatives, and caches the result for other derivatives (to avoid computing it several times)
2009-01-23 11:59:37 +01:00
For an equal node , returns the derivative of lhs minus rhs */
2009-04-17 18:26:23 +02:00
NodeID getDerivative ( int deriv_id ) ;
2007-01-09 20:00:05 +01:00
2009-07-06 11:34:21 +02:00
//! Computes derivatives by applying the chain rule for some variables
/*!
\ param deriv_id The derivation ID with respect to which we are derivating
\ param recursive_variables Contains the derivation ID for which chain rules must be applied . Keys are derivation IDs , values are equations of the form x = f ( y ) where x is the key variable and x doesn ' t appear in y
*/
virtual NodeID getChainRuleDerivative ( int deriv_id , const map < int , NodeID > & recursive_variables ) = 0 ;
2009-06-05 16:45:23 +02:00
2007-01-09 20:00:05 +01:00
//! Returns precedence of node
/*! Equals 100 for constants, variables, unary ops, and temporary terms */
2007-03-06 18:14:35 +01:00
virtual int precedence ( ExprNodeOutputType output_type , const temporary_terms_type & temporary_terms ) const ;
2007-01-09 20:00:05 +01:00
//! Fills temporary_terms set, using reference counts
/*! A node will be marked as a temporary term if it is referenced at least two times (i.e. has at least two parents), and has a computing cost (multiplied by reference count) greater to datatree.min_cost */
2007-03-06 18:14:35 +01:00
virtual void computeTemporaryTerms ( map < NodeID , int > & reference_count , temporary_terms_type & temporary_terms , bool is_matlab ) const ;
2007-01-09 20:00:05 +01:00
2010-03-09 12:16:32 +01:00
//! Writes output of node, using a Txxx notation for nodes in temporary_terms, and specifiying the set of already written external functions
/*!
\ param [ in ] output the output stream
\ param [ in ] output_type the type of output ( MATLAB , C , LaTeX . . . )
\ param [ in ] temporary_terms the nodes that are marked as temporary terms
\ param [ in , out ] tef_terms the set of already written external function nodes
*/
2010-03-04 16:40:07 +01:00
virtual void writeOutput ( ostream & output , ExprNodeOutputType output_type , const temporary_terms_type & temporary_terms , deriv_node_temp_terms_type & tef_terms ) const = 0 ;
2007-02-22 00:28:16 +01:00
2007-03-09 18:27:46 +01:00
//! Writes output of node (with no temporary terms and with "outside model" output type)
2010-03-09 12:16:32 +01:00
void writeOutput ( ostream & output ) const ;
2007-03-09 18:27:46 +01:00
2010-03-09 12:16:32 +01:00
//! Writes output of node (with no temporary terms)
void writeOutput ( ostream & output , ExprNodeOutputType output_type ) const ;
//! Writes output of node, using a Txxx notation for nodes in temporary_terms
void writeOutput ( ostream & output , ExprNodeOutputType output_type , const temporary_terms_type & temporary_terms ) const ;
2010-03-04 16:40:07 +01:00
//! Writes the output for an external function, ensuring that the external function is called as few times as possible using temporary terms
virtual void writeExternalFunctionOutput ( ostream & output , ExprNodeOutputType output_type ,
const temporary_terms_type & temporary_terms ,
deriv_node_temp_terms_type & tef_terms ) const ;
2009-07-10 18:42:08 +02:00
//! Computes the set of all variables of a given symbol type in the expression
/*!
Variables are stored as integer pairs of the form ( symb_id , lag ) .
They are added to the set given in argument .
Note that model local variables are substituted by their expression in the computation
( and added if type_arg = ModelLocalVariable ) .
*/
virtual void collectVariables ( SymbolType type_arg , set < pair < int , int > > & result ) const = 0 ;
2008-04-03 16:47:39 +02:00
//! Computes the set of endogenous variables in the expression
2009-04-27 19:15:14 +02:00
/*!
2009-07-10 16:22:40 +02:00
Endogenous are stored as integer pairs of the form ( type_specific_id , lag ) .
2009-04-27 19:15:14 +02:00
They are added to the set given in argument .
2009-07-10 18:42:08 +02:00
Note that model local variables are substituted by their expression in the computation .
2009-04-27 19:15:14 +02:00
*/
2009-07-10 18:42:08 +02:00
virtual void collectEndogenous ( set < pair < int , int > > & result ) const ;
2009-04-27 19:15:14 +02:00
//! Computes the set of exogenous variables in the expression
/*!
2009-07-10 16:22:40 +02:00
Exogenous are stored as integer pairs of the form ( type_specific_id , lag ) .
2009-04-27 19:15:14 +02:00
They are added to the set given in argument .
2009-07-10 18:42:08 +02:00
Note that model local variables are substituted by their expression in the computation .
2009-04-27 19:15:14 +02:00
*/
2009-07-10 18:42:08 +02:00
virtual void collectExogenous ( set < pair < int , int > > & result ) const ;
//! Computes the set of model local variables in the expression
/*!
Symbol IDs of these model local variables are added to the set given in argument .
Note that this method is called recursively on the expressions associated to the model local variables detected .
*/
virtual void collectModelLocalVariables ( set < int > & result ) const ;
2009-04-27 19:15:14 +02:00
2009-12-16 14:21:31 +01:00
virtual void collectTemporary_terms ( const temporary_terms_type & temporary_terms , temporary_terms_inuse_type & temporary_terms_inuse , int Curr_Block ) const = 0 ;
2007-02-22 00:28:16 +01:00
virtual void computeTemporaryTerms ( map < NodeID , int > & reference_count ,
temporary_terms_type & temporary_terms ,
2009-01-02 14:00:43 +01:00
map < NodeID , pair < int , int > > & first_occurence ,
2007-02-22 00:28:16 +01:00
int Curr_block ,
2009-12-16 14:21:31 +01:00
vector < vector < temporary_terms_type > > & v_temporary_terms ,
int equation ) const ;
2007-03-09 18:27:46 +01:00
class EvalException
2009-06-05 16:45:23 +02:00
2007-03-09 18:27:46 +01:00
{
} ;
virtual double eval ( const eval_context_type & eval_context ) const throw ( EvalException ) = 0 ;
2010-01-22 17:42:08 +01:00
virtual void compile ( ostream & CompileCode , bool lhs_rhs , const temporary_terms_type & temporary_terms , const map_idx_type & map_idx , bool dynamic , bool steady_dynamic ) const = 0 ;
2009-04-14 16:39:53 +02:00
//! Creates a static version of this node
/*!
This method duplicates the current node by creating a similar node from which all leads / lags have been stripped ,
adds the result in the static_datatree argument ( and not in the original datatree ) , and returns it .
*/
virtual NodeID toStatic ( DataTree & static_datatree ) const = 0 ;
2009-06-05 16:45:23 +02:00
//! Try to normalize an equation linear in its endogenous variable
2009-08-25 11:43:01 +02:00
virtual pair < int , NodeID > normalizeEquation ( int symb_id_endo , vector < pair < int , pair < NodeID , NodeID > > > & List_of_Op_RHS ) const = 0 ;
2009-09-30 17:10:31 +02:00
//! Returns the maximum lead of endogenous in this expression
/*! Always returns a non-negative value */
virtual int maxEndoLead ( ) const = 0 ;
2009-10-07 18:34:42 +02:00
//! Returns the maximum lead of exogenous in this expression
/*! Always returns a non-negative value */
virtual int maxExoLead ( ) const = 0 ;
2009-09-30 17:10:31 +02:00
//! Returns a new expression where all the leads/lags have been shifted backwards by the same amount
/*!
Only acts on endogenous , exogenous , exogenous det
\ param [ in ] n The number of lags by which to shift
\ return The same expression except that leads / lags have been shifted backwards
*/
virtual NodeID decreaseLeadsLags ( int n ) const = 0 ;
//! Type for the substitution map used in the process of creating auxiliary vars for leads >= 2
typedef map < const ExprNode * , const VariableNode * > subst_table_t ;
2009-10-07 16:07:13 +02:00
//! Creates auxiliary endo lead variables corresponding to this expression
2009-12-16 14:21:31 +01:00
/*!
2009-09-30 17:10:31 +02:00
If maximum endogenous lead > = 3 , this method will also create intermediary auxiliary var , and will add the equations of the form aux1 = aux2 ( + 1 ) to the substitution table .
\ pre This expression is assumed to have maximum endogenous lead > = 2
\ param [ in , out ] subst_table The table to which new auxiliary variables and their correspondance will be added
2009-10-07 18:34:42 +02:00
\ param [ out ] neweqs Equations to be added to the model to match the creation of auxiliary variables .
2009-09-30 17:10:31 +02:00
\ return The new variable node corresponding to the current expression
*/
2009-10-07 16:07:13 +02:00
VariableNode * createEndoLeadAuxiliaryVarForMyself ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
2009-09-30 17:10:31 +02:00
2009-10-07 18:34:42 +02:00
//! Creates auxiliary exo lead variables corresponding to this expression
2009-12-16 14:21:31 +01:00
/*!
2009-10-07 18:34:42 +02:00
If maximum exogenous lead > = 2 , this method will also create intermediary auxiliary var , and will add the equations of the form aux1 = aux2 ( + 1 ) to the substitution table .
\ pre This expression is assumed to have maximum exogenous lead > = 1
\ param [ in , out ] subst_table The table to which new auxiliary variables and their correspondance will be added
\ param [ out ] neweqs Equations to be added to the model to match the creation of auxiliary variables .
\ return The new variable node corresponding to the current expression
*/
VariableNode * createExoLeadAuxiliaryVarForMyself ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
2009-09-30 17:10:31 +02:00
//! Constructs a new expression where sub-expressions with max endo lead >= 2 have been replaced by auxiliary variables
/*!
\ param [ in , out ] subst_table Map used to store expressions that have already be substituted and their corresponding variable , in order to avoid creating two auxiliary variables for the same sub - expr .
\ param [ out ] neweqs Equations to be added to the model to match the creation of auxiliary variables .
If the method detects a sub - expr which needs to be substituted , two cases are possible :
- if this expr is in the table , then it will use the corresponding variable and return the substituted expression
- if this expr is not in the table , then it will create an auxiliary endogenous variable , add the substitution in the table and return the substituted expression
\ return A new equivalent expression where sub - expressions with max endo lead > = 2 have been replaced by auxiliary variables
2009-12-16 18:13:23 +01:00
*/
2009-10-07 16:07:13 +02:00
virtual NodeID substituteEndoLeadGreaterThanTwo ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const = 0 ;
2009-09-30 17:10:31 +02:00
//! Constructs a new expression where endo variables with max endo lag >= 2 have been replaced by auxiliary variables
/*!
\ param [ in , out ] subst_table Map used to store expressions that have already be substituted and their corresponding variable , in order to avoid creating two auxiliary variables for the same sub - expr .
\ param [ out ] neweqs Equations to be added to the model to match the creation of auxiliary variables .
*/
2009-10-07 16:07:13 +02:00
virtual NodeID substituteEndoLagGreaterThanTwo ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const = 0 ;
2009-10-07 18:34:42 +02:00
//! Constructs a new expression where exogenous variables with a lead have been replaced by auxiliary variables
/*!
\ param [ in , out ] subst_table Map used to store expressions that have already be substituted and their corresponding variable , in order to avoid creating two auxiliary variables for the same sub - expr .
\ param [ out ] neweqs Equations to be added to the model to match the creation of auxiliary variables .
*/
virtual NodeID substituteExoLead ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const = 0 ;
//! Constructs a new expression where exogenous variables with a lag have been replaced by auxiliary variables
2009-10-07 16:07:13 +02:00
/*!
\ param [ in , out ] subst_table Map used to store expressions that have already be substituted and their corresponding variable , in order to avoid creating two auxiliary variables for the same sub - expr .
\ param [ out ] neweqs Equations to be added to the model to match the creation of auxiliary variables .
*/
2009-10-07 18:34:42 +02:00
virtual NodeID substituteExoLag ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const = 0 ;
2009-10-29 18:16:10 +01:00
//! Constructs a new expression where the expectation operator has been replaced by auxiliary variables
/*!
\ param [ in , out ] subst_table Map used to store expressions that have already be substituted and their corresponding variable , in order to avoid creating two auxiliary variables for the same sub - expr .
\ param [ out ] neweqs Equations to be added to the model to match the creation of auxiliary variables .
2009-11-09 16:13:47 +01:00
\ param [ in ] partial_information_model Are we substituting in a partial information model ?
2009-10-29 18:16:10 +01:00
*/
virtual NodeID substituteExpectation ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs , bool partial_information_model ) const = 0 ;
2009-11-07 19:37:11 +01:00
2009-11-09 12:03:18 +01:00
virtual NodeID decreaseLeadsLagsPredeterminedVariables ( ) const = 0 ;
2009-11-07 19:37:11 +01:00
2010-04-16 16:54:55 +02:00
//! Return true if the nodeID is a numerical constant equal to value and false otherwise
/*!
\ param [ in ] value of the numerical constante
\ param [ out ] the boolean equal to true if NodeId is a constant equal to value
*/
virtual bool isNumConstNodeEqualTo ( double value ) const = 0 ;
//! Return true if the nodeID is a variable withe a type equal to type_arg, a specific variable id aqual to varfiable_id and a lag equal to lag_arg and false otherwise
/*!
\ param [ in ] the type ( type_arg ) , specifique variable id ( variable_id and the lag ( lag_arg )
\ param [ out ] the boolean equal to true if NodeId is the variable
*/
virtual bool isVariableNodeEqualTo ( SymbolType type_arg , int variable_id , int lag_arg ) const = 0 ;
2007-01-09 20:00:05 +01:00
} ;
//! Object used to compare two nodes (using their indexes)
struct ExprNodeLess
{
2009-12-16 18:13:23 +01:00
bool
operator ( ) ( NodeID arg1 , NodeID arg2 ) const
2007-01-09 20:00:05 +01:00
{
return arg1 - > idx < arg2 - > idx ;
}
} ;
//! Numerical constant node
2007-05-08 21:16:35 +02:00
/*! The constant is necessarily non-negative (this is enforced at the NumericalConstants class level) */
2007-01-09 20:00:05 +01:00
class NumConstNode : public ExprNode
{
private :
//! Id from numerical constants table
const int id ;
2009-04-17 18:26:23 +02:00
virtual NodeID computeDerivative ( int deriv_id ) ;
2007-01-09 20:00:05 +01:00
public :
NumConstNode ( DataTree & datatree_arg , int id_arg ) ;
2009-12-16 18:13:23 +01:00
int
get_id ( ) const
{
return id ;
} ;
2009-09-30 17:10:31 +02:00
virtual void prepareForDerivation ( ) ;
2010-03-04 16:40:07 +01:00
virtual void writeOutput ( ostream & output , ExprNodeOutputType output_type , const temporary_terms_type & temporary_terms , deriv_node_temp_terms_type & tef_terms ) const ;
2009-07-10 18:42:08 +02:00
virtual void collectVariables ( SymbolType type_arg , set < pair < int , int > > & result ) const ;
2009-12-16 14:21:31 +01:00
virtual void collectTemporary_terms ( const temporary_terms_type & temporary_terms , temporary_terms_inuse_type & temporary_terms_inuse , int Curr_Block ) const ;
2007-03-09 18:27:46 +01:00
virtual double eval ( const eval_context_type & eval_context ) const throw ( EvalException ) ;
2010-01-22 17:42:08 +01:00
virtual void compile ( ostream & CompileCode , bool lhs_rhs , const temporary_terms_type & temporary_terms , const map_idx_type & map_idx , bool dynamic , bool steady_dynamic ) const ;
2009-04-14 16:39:53 +02:00
virtual NodeID toStatic ( DataTree & static_datatree ) const ;
2009-08-25 11:43:01 +02:00
virtual pair < int , NodeID > normalizeEquation ( int symb_id_endo , vector < pair < int , pair < NodeID , NodeID > > > & List_of_Op_RHS ) const ;
2009-07-06 11:34:21 +02:00
virtual NodeID getChainRuleDerivative ( int deriv_id , const map < int , NodeID > & recursive_variables ) ;
2009-09-30 17:10:31 +02:00
virtual int maxEndoLead ( ) const ;
2009-10-07 18:34:42 +02:00
virtual int maxExoLead ( ) const ;
2009-09-30 17:10:31 +02:00
virtual NodeID decreaseLeadsLags ( int n ) const ;
2009-10-07 16:07:13 +02:00
virtual NodeID substituteEndoLeadGreaterThanTwo ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
virtual NodeID substituteEndoLagGreaterThanTwo ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
2009-10-07 18:34:42 +02:00
virtual NodeID substituteExoLead ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
virtual NodeID substituteExoLag ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
2009-10-29 18:16:10 +01:00
virtual NodeID substituteExpectation ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs , bool partial_information_model ) const ;
2009-11-09 12:03:18 +01:00
virtual NodeID decreaseLeadsLagsPredeterminedVariables ( ) const ;
2010-04-16 16:54:55 +02:00
virtual bool isNumConstNodeEqualTo ( double value ) const ;
virtual bool isVariableNodeEqualTo ( SymbolType type_arg , int variable_id , int lag_arg ) const ;
2007-01-09 20:00:05 +01:00
} ;
//! Symbol or variable node
class VariableNode : public ExprNode
{
private :
2007-03-09 18:27:46 +01:00
//! Id from the symbol table
const int symb_id ;
2008-10-17 12:20:19 +02:00
const SymbolType type ;
2007-03-09 18:27:46 +01:00
const int lag ;
2009-09-30 17:10:31 +02:00
virtual NodeID computeDerivative ( int deriv_id ) ;
2007-01-09 20:00:05 +01:00
public :
2009-09-30 17:10:31 +02:00
VariableNode ( DataTree & datatree_arg , int symb_id_arg , int lag_arg ) ;
virtual void prepareForDerivation ( ) ;
2010-03-04 16:40:07 +01:00
virtual void writeOutput ( ostream & output , ExprNodeOutputType output_type , const temporary_terms_type & temporary_terms , deriv_node_temp_terms_type & tef_terms ) const ;
2009-07-10 18:42:08 +02:00
virtual void collectVariables ( SymbolType type_arg , set < pair < int , int > > & result ) const ;
2009-01-02 14:00:43 +01:00
virtual void computeTemporaryTerms ( map < NodeID , int > & reference_count ,
2009-01-23 11:59:37 +01:00
temporary_terms_type & temporary_terms ,
map < NodeID , pair < int , int > > & first_occurence ,
int Curr_block ,
2009-12-16 14:21:31 +01:00
vector < vector < temporary_terms_type > > & v_temporary_terms ,
int equation ) const ;
virtual void collectTemporary_terms ( const temporary_terms_type & temporary_terms , temporary_terms_inuse_type & temporary_terms_inuse , int Curr_Block ) const ;
2007-03-09 18:27:46 +01:00
virtual double eval ( const eval_context_type & eval_context ) const throw ( EvalException ) ;
2010-01-22 17:42:08 +01:00
virtual void compile ( ostream & CompileCode , bool lhs_rhs , const temporary_terms_type & temporary_terms , const map_idx_type & map_idx , bool dynamic , bool steady_dynamic ) const ;
2009-04-14 16:39:53 +02:00
virtual NodeID toStatic ( DataTree & static_datatree ) const ;
2009-12-16 18:13:23 +01:00
int
get_symb_id ( ) const
{
return symb_id ;
} ;
2010-01-18 23:08:44 +01:00
int get_lag ( ) const { return lag ; } ;
2009-08-25 11:43:01 +02:00
virtual pair < int , NodeID > normalizeEquation ( int symb_id_endo , vector < pair < int , pair < NodeID , NodeID > > > & List_of_Op_RHS ) const ;
2009-07-06 11:34:21 +02:00
virtual NodeID getChainRuleDerivative ( int deriv_id , const map < int , NodeID > & recursive_variables ) ;
2009-09-30 17:10:31 +02:00
virtual int maxEndoLead ( ) const ;
2009-10-07 18:34:42 +02:00
virtual int maxExoLead ( ) const ;
2009-09-30 17:10:31 +02:00
virtual NodeID decreaseLeadsLags ( int n ) const ;
2009-10-07 16:07:13 +02:00
virtual NodeID substituteEndoLeadGreaterThanTwo ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
virtual NodeID substituteEndoLagGreaterThanTwo ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
2009-10-07 18:34:42 +02:00
virtual NodeID substituteExoLead ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
virtual NodeID substituteExoLag ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
2009-10-29 18:16:10 +01:00
virtual NodeID substituteExpectation ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs , bool partial_information_model ) const ;
2009-11-09 12:03:18 +01:00
virtual NodeID decreaseLeadsLagsPredeterminedVariables ( ) const ;
2010-04-16 16:54:55 +02:00
virtual bool isNumConstNodeEqualTo ( double value ) const ;
virtual bool isVariableNodeEqualTo ( SymbolType type_arg , int variable_id , int lag_arg ) const ;
2007-01-09 20:00:05 +01:00
} ;
//! Unary operator node
class UnaryOpNode : public ExprNode
{
private :
const NodeID arg ;
2009-11-06 19:19:52 +01:00
//! Stores the information set. Only used for expectation operator
const int expectation_information_set ;
2010-01-18 23:08:44 +01:00
//! Stores the information set name. Only used for expectation operator
const string expectation_information_set_name ;
2007-01-09 20:00:05 +01:00
const UnaryOpcode op_code ;
2009-04-17 18:26:23 +02:00
virtual NodeID computeDerivative ( int deriv_id ) ;
2007-03-09 18:27:46 +01:00
virtual int cost ( const temporary_terms_type & temporary_terms , bool is_matlab ) const ;
2009-07-06 11:34:21 +02:00
//! Returns the derivative of this node if darg is the derivative of the argument
NodeID composeDerivatives ( NodeID darg ) ;
2007-01-09 20:00:05 +01:00
public :
2010-01-18 23:08:44 +01:00
UnaryOpNode ( DataTree & datatree_arg , UnaryOpcode op_code_arg , const NodeID arg_arg , const int expectation_information_set_arg , const string & expectation_information_set_name_arg ) ;
2009-09-30 17:10:31 +02:00
virtual void prepareForDerivation ( ) ;
2007-03-06 18:14:35 +01:00
virtual void computeTemporaryTerms ( map < NodeID , int > & reference_count , temporary_terms_type & temporary_terms , bool is_matlab ) const ;
2010-03-04 16:40:07 +01:00
virtual void writeOutput ( ostream & output , ExprNodeOutputType output_type , const temporary_terms_type & temporary_terms , deriv_node_temp_terms_type & tef_terms ) const ;
virtual void writeExternalFunctionOutput ( ostream & output , ExprNodeOutputType output_type ,
const temporary_terms_type & temporary_terms ,
deriv_node_temp_terms_type & tef_terms ) const ;
2007-02-22 00:28:16 +01:00
virtual void computeTemporaryTerms ( map < NodeID , int > & reference_count ,
temporary_terms_type & temporary_terms ,
2009-01-02 14:00:43 +01:00
map < NodeID , pair < int , int > > & first_occurence ,
2007-02-22 00:28:16 +01:00
int Curr_block ,
2009-12-16 14:21:31 +01:00
vector < vector < temporary_terms_type > > & v_temporary_terms ,
int equation ) const ;
2009-07-10 18:42:08 +02:00
virtual void collectVariables ( SymbolType type_arg , set < pair < int , int > > & result ) const ;
2009-12-16 14:21:31 +01:00
virtual void collectTemporary_terms ( const temporary_terms_type & temporary_terms , temporary_terms_inuse_type & temporary_terms_inuse , int Curr_Block ) const ;
2007-05-08 21:16:35 +02:00
static double eval_opcode ( UnaryOpcode op_code , double v ) throw ( EvalException ) ;
2007-03-09 18:27:46 +01:00
virtual double eval ( const eval_context_type & eval_context ) const throw ( EvalException ) ;
2010-01-22 17:42:08 +01:00
virtual void compile ( ostream & CompileCode , bool lhs_rhs , const temporary_terms_type & temporary_terms , const map_idx_type & map_idx , bool dynamic , bool steady_dynamic ) const ;
2009-04-14 16:39:53 +02:00
//! Returns operand
2009-12-16 18:13:23 +01:00
NodeID
get_arg ( ) const
{
return ( arg ) ;
} ;
2009-04-14 16:39:53 +02:00
//! Returns op code
2009-12-16 18:13:23 +01:00
UnaryOpcode
get_op_code ( ) const
{
return ( op_code ) ;
} ;
2009-04-14 16:39:53 +02:00
virtual NodeID toStatic ( DataTree & static_datatree ) const ;
2009-08-25 11:43:01 +02:00
virtual pair < int , NodeID > normalizeEquation ( int symb_id_endo , vector < pair < int , pair < NodeID , NodeID > > > & List_of_Op_RHS ) const ;
2009-07-06 11:34:21 +02:00
virtual NodeID getChainRuleDerivative ( int deriv_id , const map < int , NodeID > & recursive_variables ) ;
2009-09-30 17:10:31 +02:00
virtual int maxEndoLead ( ) const ;
2009-10-07 18:34:42 +02:00
virtual int maxExoLead ( ) const ;
2009-09-30 17:10:31 +02:00
virtual NodeID decreaseLeadsLags ( int n ) const ;
2009-10-07 16:07:13 +02:00
virtual NodeID substituteEndoLeadGreaterThanTwo ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
2009-09-30 17:10:31 +02:00
//! Creates another UnaryOpNode with the same opcode, but with a possibly different datatree and argument
NodeID buildSimilarUnaryOpNode ( NodeID alt_arg , DataTree & alt_datatree ) const ;
2009-10-07 16:07:13 +02:00
virtual NodeID substituteEndoLagGreaterThanTwo ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
2009-10-07 18:34:42 +02:00
virtual NodeID substituteExoLead ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
virtual NodeID substituteExoLag ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
2009-10-29 18:16:10 +01:00
virtual NodeID substituteExpectation ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs , bool partial_information_model ) const ;
2009-11-09 12:03:18 +01:00
virtual NodeID decreaseLeadsLagsPredeterminedVariables ( ) const ;
2010-04-16 16:54:55 +02:00
virtual bool isNumConstNodeEqualTo ( double value ) const ;
virtual bool isVariableNodeEqualTo ( SymbolType type_arg , int variable_id , int lag_arg ) const ;
2007-01-09 20:00:05 +01:00
} ;
//! Binary operator node
class BinaryOpNode : public ExprNode
{
private :
const NodeID arg1 , arg2 ;
const BinaryOpcode op_code ;
2009-04-17 18:26:23 +02:00
virtual NodeID computeDerivative ( int deriv_id ) ;
2007-03-09 18:27:46 +01:00
virtual int cost ( const temporary_terms_type & temporary_terms , bool is_matlab ) const ;
2009-07-06 11:34:21 +02:00
//! Returns the derivative of this node if darg1 and darg2 are the derivatives of the arguments
NodeID composeDerivatives ( NodeID darg1 , NodeID darg2 ) ;
2007-01-09 20:00:05 +01:00
public :
BinaryOpNode ( DataTree & datatree_arg , const NodeID arg1_arg ,
BinaryOpcode op_code_arg , const NodeID arg2_arg ) ;
2009-09-30 17:10:31 +02:00
virtual void prepareForDerivation ( ) ;
2007-03-06 18:14:35 +01:00
virtual int precedence ( ExprNodeOutputType output_type , const temporary_terms_type & temporary_terms ) const ;
virtual void computeTemporaryTerms ( map < NodeID , int > & reference_count , temporary_terms_type & temporary_terms , bool is_matlab ) const ;
2010-03-04 16:40:07 +01:00
virtual void writeOutput ( ostream & output , ExprNodeOutputType output_type , const temporary_terms_type & temporary_terms , deriv_node_temp_terms_type & tef_terms ) const ;
virtual void writeExternalFunctionOutput ( ostream & output , ExprNodeOutputType output_type ,
const temporary_terms_type & temporary_terms ,
deriv_node_temp_terms_type & tef_terms ) const ;
2007-02-22 00:28:16 +01:00
virtual void computeTemporaryTerms ( map < NodeID , int > & reference_count ,
temporary_terms_type & temporary_terms ,
2009-01-02 14:00:43 +01:00
map < NodeID , pair < int , int > > & first_occurence ,
2007-02-22 00:28:16 +01:00
int Curr_block ,
2009-12-16 14:21:31 +01:00
vector < vector < temporary_terms_type > > & v_temporary_terms ,
int equation ) const ;
2009-07-10 18:42:08 +02:00
virtual void collectVariables ( SymbolType type_arg , set < pair < int , int > > & result ) const ;
2009-12-16 14:21:31 +01:00
virtual void collectTemporary_terms ( const temporary_terms_type & temporary_terms , temporary_terms_inuse_type & temporary_terms_inuse , int Curr_Block ) const ;
2007-05-08 21:16:35 +02:00
static double eval_opcode ( double v1 , BinaryOpcode op_code , double v2 ) throw ( EvalException ) ;
2007-03-09 18:27:46 +01:00
virtual double eval ( const eval_context_type & eval_context ) const throw ( EvalException ) ;
2010-01-22 17:42:08 +01:00
virtual void compile ( ostream & CompileCode , bool lhs_rhs , const temporary_terms_type & temporary_terms , const map_idx_type & map_idx , bool dynamic , bool steady_dynamic ) const ;
2009-08-25 11:43:01 +02:00
virtual NodeID Compute_RHS ( NodeID arg1 , NodeID arg2 , int op , int op_type ) const ;
2009-04-14 16:39:53 +02:00
//! Returns first operand
2009-12-16 18:13:23 +01:00
NodeID
get_arg1 ( ) const
{
return ( arg1 ) ;
} ;
2009-04-14 16:39:53 +02:00
//! Returns second operand
2009-12-16 18:13:23 +01:00
NodeID
get_arg2 ( ) const
{
return ( arg2 ) ;
} ;
2009-04-14 16:39:53 +02:00
//! Returns op code
2009-12-16 18:13:23 +01:00
BinaryOpcode
get_op_code ( ) const
{
return ( op_code ) ;
} ;
2009-04-14 16:39:53 +02:00
virtual NodeID toStatic ( DataTree & static_datatree ) const ;
2009-08-25 11:43:01 +02:00
virtual pair < int , NodeID > normalizeEquation ( int symb_id_endo , vector < pair < int , pair < NodeID , NodeID > > > & List_of_Op_RHS ) const ;
2009-07-06 11:34:21 +02:00
virtual NodeID getChainRuleDerivative ( int deriv_id , const map < int , NodeID > & recursive_variables ) ;
2009-09-30 17:10:31 +02:00
virtual int maxEndoLead ( ) const ;
2009-10-07 18:34:42 +02:00
virtual int maxExoLead ( ) const ;
2009-09-30 17:10:31 +02:00
virtual NodeID decreaseLeadsLags ( int n ) const ;
2009-10-07 16:07:13 +02:00
virtual NodeID substituteEndoLeadGreaterThanTwo ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
2009-09-30 17:10:31 +02:00
//! Creates another BinaryOpNode with the same opcode, but with a possibly different datatree and arguments
NodeID buildSimilarBinaryOpNode ( NodeID alt_arg1 , NodeID alt_arg2 , DataTree & alt_datatree ) const ;
2009-10-07 16:07:13 +02:00
virtual NodeID substituteEndoLagGreaterThanTwo ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
2009-10-07 18:34:42 +02:00
virtual NodeID substituteExoLead ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
virtual NodeID substituteExoLag ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
2009-10-29 18:16:10 +01:00
virtual NodeID substituteExpectation ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs , bool partial_information_model ) const ;
2009-11-09 12:03:18 +01:00
virtual NodeID decreaseLeadsLagsPredeterminedVariables ( ) const ;
2010-04-16 16:54:55 +02:00
virtual bool isNumConstNodeEqualTo ( double value ) const ;
virtual bool isVariableNodeEqualTo ( SymbolType type_arg , int variable_id , int lag_arg ) const ;
2007-11-11 16:24:50 +01:00
} ;
//! Trinary operator node
class TrinaryOpNode : public ExprNode
{
friend class ModelTree ;
private :
const NodeID arg1 , arg2 , arg3 ;
const TrinaryOpcode op_code ;
2009-04-17 18:26:23 +02:00
virtual NodeID computeDerivative ( int deriv_id ) ;
2007-11-11 16:24:50 +01:00
virtual int cost ( const temporary_terms_type & temporary_terms , bool is_matlab ) const ;
2009-07-06 11:34:21 +02:00
//! Returns the derivative of this node if darg1, darg2 and darg3 are the derivatives of the arguments
NodeID composeDerivatives ( NodeID darg1 , NodeID darg2 , NodeID darg3 ) ;
2007-11-11 16:24:50 +01:00
public :
TrinaryOpNode ( DataTree & datatree_arg , const NodeID arg1_arg ,
2009-12-16 18:13:23 +01:00
TrinaryOpcode op_code_arg , const NodeID arg2_arg , const NodeID arg3_arg ) ;
2009-09-30 17:10:31 +02:00
virtual void prepareForDerivation ( ) ;
2007-11-11 16:24:50 +01:00
virtual int precedence ( ExprNodeOutputType output_type , const temporary_terms_type & temporary_terms ) const ;
virtual void computeTemporaryTerms ( map < NodeID , int > & reference_count , temporary_terms_type & temporary_terms , bool is_matlab ) const ;
2010-03-04 16:40:07 +01:00
virtual void writeOutput ( ostream & output , ExprNodeOutputType output_type , const temporary_terms_type & temporary_terms , deriv_node_temp_terms_type & tef_terms ) const ;
virtual void writeExternalFunctionOutput ( ostream & output , ExprNodeOutputType output_type ,
const temporary_terms_type & temporary_terms ,
deriv_node_temp_terms_type & tef_terms ) const ;
2007-11-11 16:24:50 +01:00
virtual void computeTemporaryTerms ( map < NodeID , int > & reference_count ,
temporary_terms_type & temporary_terms ,
2009-01-02 14:00:43 +01:00
map < NodeID , pair < int , int > > & first_occurence ,
2007-11-11 16:24:50 +01:00
int Curr_block ,
2009-12-16 14:21:31 +01:00
vector < vector < temporary_terms_type > > & v_temporary_terms ,
int equation ) const ;
2009-07-10 18:42:08 +02:00
virtual void collectVariables ( SymbolType type_arg , set < pair < int , int > > & result ) const ;
2009-12-16 14:21:31 +01:00
virtual void collectTemporary_terms ( const temporary_terms_type & temporary_terms , temporary_terms_inuse_type & temporary_terms_inuse , int Curr_Block ) const ;
2007-11-11 16:24:50 +01:00
static double eval_opcode ( double v1 , TrinaryOpcode op_code , double v2 , double v3 ) throw ( EvalException ) ;
virtual double eval ( const eval_context_type & eval_context ) const throw ( EvalException ) ;
2010-01-22 17:42:08 +01:00
virtual void compile ( ostream & CompileCode , bool lhs_rhs , const temporary_terms_type & temporary_terms , const map_idx_type & map_idx , bool dynamic , bool steady_dynamic ) const ;
2009-04-14 16:39:53 +02:00
virtual NodeID toStatic ( DataTree & static_datatree ) const ;
2009-08-25 11:43:01 +02:00
virtual pair < int , NodeID > normalizeEquation ( int symb_id_endo , vector < pair < int , pair < NodeID , NodeID > > > & List_of_Op_RHS ) const ;
2009-07-06 11:34:21 +02:00
virtual NodeID getChainRuleDerivative ( int deriv_id , const map < int , NodeID > & recursive_variables ) ;
2009-09-30 17:10:31 +02:00
virtual int maxEndoLead ( ) const ;
2009-10-07 18:34:42 +02:00
virtual int maxExoLead ( ) const ;
2009-09-30 17:10:31 +02:00
virtual NodeID decreaseLeadsLags ( int n ) const ;
2009-10-07 16:07:13 +02:00
virtual NodeID substituteEndoLeadGreaterThanTwo ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
2009-09-30 17:10:31 +02:00
//! Creates another TrinaryOpNode with the same opcode, but with a possibly different datatree and arguments
NodeID buildSimilarTrinaryOpNode ( NodeID alt_arg1 , NodeID alt_arg2 , NodeID alt_arg3 , DataTree & alt_datatree ) const ;
2009-10-07 16:07:13 +02:00
virtual NodeID substituteEndoLagGreaterThanTwo ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
2009-10-07 18:34:42 +02:00
virtual NodeID substituteExoLead ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
virtual NodeID substituteExoLag ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
2009-10-29 18:16:10 +01:00
virtual NodeID substituteExpectation ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs , bool partial_information_model ) const ;
2009-11-09 12:03:18 +01:00
virtual NodeID decreaseLeadsLagsPredeterminedVariables ( ) const ;
2010-04-16 16:54:55 +02:00
virtual bool isNumConstNodeEqualTo ( double value ) const ;
virtual bool isVariableNodeEqualTo ( SymbolType type_arg , int variable_id , int lag_arg ) const ;
2007-03-09 18:27:46 +01:00
} ;
2010-02-22 17:33:38 +01:00
//! External function node
class ExternalFunctionNode : public ExprNode
2007-03-09 18:27:46 +01:00
{
private :
2010-02-22 17:33:38 +01:00
virtual NodeID computeDerivative ( int deriv_id ) ;
virtual NodeID composeDerivatives ( const vector < NodeID > & dargs ) ;
protected :
2010-03-04 16:40:07 +01:00
//! Thrown when trying to access an unknown entry in external_function_node_map
class UnknownFunctionNameAndArgs
{
} ;
2007-10-17 11:36:56 +02:00
const int symb_id ;
2007-03-09 18:27:46 +01:00
const vector < NodeID > arguments ;
2010-03-04 16:40:07 +01:00
//! Returns true if the given external function has been written as a temporary term
bool alreadyWrittenAsTefTerm ( int the_symb_id , deriv_node_temp_terms_type & tef_terms ) const ;
//! Returns the index in the tef_terms map of this external function
int getIndxInTefTerms ( int the_symb_id , deriv_node_temp_terms_type & tef_terms ) const throw ( UnknownFunctionNameAndArgs ) ;
//! Helper function to write output arguments of any given external function
void writeExternalFunctionArguments ( ostream & output , ExprNodeOutputType output_type , const temporary_terms_type & temporary_terms , deriv_node_temp_terms_type & tef_terms ) const ;
2007-03-09 18:27:46 +01:00
public :
2010-02-22 17:33:38 +01:00
ExternalFunctionNode ( DataTree & datatree_arg , int symb_id_arg ,
2007-03-09 18:27:46 +01:00
const vector < NodeID > & arguments_arg ) ;
2009-09-30 17:10:31 +02:00
virtual void prepareForDerivation ( ) ;
2007-03-09 18:27:46 +01:00
virtual void computeTemporaryTerms ( map < NodeID , int > & reference_count , temporary_terms_type & temporary_terms , bool is_matlab ) const ;
2010-03-04 16:40:07 +01:00
virtual void writeOutput ( ostream & output , ExprNodeOutputType output_type , const temporary_terms_type & temporary_terms , deriv_node_temp_terms_type & tef_terms ) const ;
virtual void writeExternalFunctionOutput ( ostream & output , ExprNodeOutputType output_type ,
const temporary_terms_type & temporary_terms ,
deriv_node_temp_terms_type & tef_terms ) const ;
2007-03-09 18:27:46 +01:00
virtual void computeTemporaryTerms ( map < NodeID , int > & reference_count ,
temporary_terms_type & temporary_terms ,
2009-01-02 14:00:43 +01:00
map < NodeID , pair < int , int > > & first_occurence ,
2007-03-09 18:27:46 +01:00
int Curr_block ,
2009-12-16 14:21:31 +01:00
vector < vector < temporary_terms_type > > & v_temporary_terms ,
int equation ) const ;
2009-07-10 18:42:08 +02:00
virtual void collectVariables ( SymbolType type_arg , set < pair < int , int > > & result ) const ;
2009-12-16 14:21:31 +01:00
virtual void collectTemporary_terms ( const temporary_terms_type & temporary_terms , temporary_terms_inuse_type & temporary_terms_inuse , int Curr_Block ) const ;
2007-03-09 18:27:46 +01:00
virtual double eval ( const eval_context_type & eval_context ) const throw ( EvalException ) ;
2010-01-22 17:42:08 +01:00
virtual void compile ( ostream & CompileCode , bool lhs_rhs , const temporary_terms_type & temporary_terms , const map_idx_type & map_idx , bool dynamic , bool steady_dynamic ) const ;
2009-04-14 16:39:53 +02:00
virtual NodeID toStatic ( DataTree & static_datatree ) const ;
2009-08-25 11:43:01 +02:00
virtual pair < int , NodeID > normalizeEquation ( int symb_id_endo , vector < pair < int , pair < NodeID , NodeID > > > & List_of_Op_RHS ) const ;
2009-07-06 11:34:21 +02:00
virtual NodeID getChainRuleDerivative ( int deriv_id , const map < int , NodeID > & recursive_variables ) ;
2009-09-30 17:10:31 +02:00
virtual int maxEndoLead ( ) const ;
2009-10-07 18:34:42 +02:00
virtual int maxExoLead ( ) const ;
2009-09-30 17:10:31 +02:00
virtual NodeID decreaseLeadsLags ( int n ) const ;
2009-10-07 16:07:13 +02:00
virtual NodeID substituteEndoLeadGreaterThanTwo ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
virtual NodeID substituteEndoLagGreaterThanTwo ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
2009-10-07 18:34:42 +02:00
virtual NodeID substituteExoLead ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
virtual NodeID substituteExoLag ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs ) const ;
2009-10-29 18:16:10 +01:00
virtual NodeID substituteExpectation ( subst_table_t & subst_table , vector < BinaryOpNode * > & neweqs , bool partial_information_model ) const ;
2010-02-22 17:33:38 +01:00
virtual NodeID buildSimilarExternalFunctionNode ( vector < NodeID > & alt_args , DataTree & alt_datatree ) const ;
2009-11-09 12:03:18 +01:00
virtual NodeID decreaseLeadsLagsPredeterminedVariables ( ) const ;
2010-04-16 16:54:55 +02:00
virtual bool isNumConstNodeEqualTo ( double value ) const ;
virtual bool isVariableNodeEqualTo ( SymbolType type_arg , int variable_id , int lag_arg ) const ;
2007-02-22 00:28:16 +01:00
} ;
2010-02-22 17:33:38 +01:00
class FirstDerivExternalFunctionNode : public ExternalFunctionNode
{
private :
const int inputIndex ;
virtual NodeID composeDerivatives ( const vector < NodeID > & dargs ) ;
public :
FirstDerivExternalFunctionNode ( DataTree & datatree_arg ,
int top_level_symb_id_arg ,
const vector < NodeID > & arguments_arg ,
int inputIndex_arg ) ;
2010-03-04 16:40:07 +01:00
virtual void computeTemporaryTerms ( map < NodeID , int > & reference_count , temporary_terms_type & temporary_terms , bool is_matlab ) const ;
virtual void computeTemporaryTerms ( map < NodeID , int > & reference_count ,
temporary_terms_type & temporary_terms ,
map < NodeID , pair < int , int > > & first_occurence ,
int Curr_block ,
vector < vector < temporary_terms_type > > & v_temporary_terms ,
int equation ) const ;
virtual void writeOutput ( ostream & output , ExprNodeOutputType output_type , const temporary_terms_type & temporary_terms , deriv_node_temp_terms_type & tef_terms ) const ;
virtual void writeExternalFunctionOutput ( ostream & output , ExprNodeOutputType output_type ,
const temporary_terms_type & temporary_terms ,
deriv_node_temp_terms_type & tef_terms ) const ;
2010-02-22 17:33:38 +01:00
} ;
class SecondDerivExternalFunctionNode : public ExternalFunctionNode
{
private :
const int inputIndex1 ;
const int inputIndex2 ;
virtual NodeID computeDerivative ( int deriv_id ) ;
public :
SecondDerivExternalFunctionNode ( DataTree & datatree_arg ,
int top_level_symb_id_arg ,
const vector < NodeID > & arguments_arg ,
int inputIndex1_arg ,
int inputIndex2_arg ) ;
2010-03-04 16:40:07 +01:00
virtual void computeTemporaryTerms ( map < NodeID , int > & reference_count , temporary_terms_type & temporary_terms , bool is_matlab ) const ;
virtual void computeTemporaryTerms ( map < NodeID , int > & reference_count ,
temporary_terms_type & temporary_terms ,
map < NodeID , pair < int , int > > & first_occurence ,
int Curr_block ,
vector < vector < temporary_terms_type > > & v_temporary_terms ,
int equation ) const ;
virtual void writeOutput ( ostream & output , ExprNodeOutputType output_type , const temporary_terms_type & temporary_terms , deriv_node_temp_terms_type & tef_terms ) const ;
virtual void writeExternalFunctionOutput ( ostream & output , ExprNodeOutputType output_type ,
const temporary_terms_type & temporary_terms ,
deriv_node_temp_terms_type & tef_terms ) const ;
2010-02-22 17:33:38 +01:00
} ;
2007-01-09 20:00:05 +01:00
# endif