Preprocessor:

* new ExprNode method for computing set of ModelLocalVariables of an expression
* factorized code with collectEndogenous() and collectExogenous() methods


git-svn-id: https://www.dynare.org/svn/dynare/trunk@2839 ac1d8469-bf42-47a9-8791-bf33cf982152
issue#70
sebastien 2009-07-10 16:42:08 +00:00
parent 97f73257f6
commit daae864666
3 changed files with 89 additions and 90 deletions

View File

@ -21,6 +21,8 @@
#include <iterator> #include <iterator>
#include <algorithm> #include <algorithm>
#include <ext/functional>
#include <cassert> #include <cassert>
#include <cmath> #include <cmath>
@ -75,6 +77,35 @@ ExprNode::cost(const temporary_terms_type &temporary_terms, bool is_matlab) cons
return 0; return 0;
} }
void
ExprNode::collectEndogenous(set<pair<int, int> > &result) const
{
set<pair<int, int> > symb_ids;
collectVariables(eEndogenous, symb_ids);
for(set<pair<int, int> >::const_iterator it = symb_ids.begin();
it != symb_ids.end(); it++)
result.insert(make_pair(datatree.symbol_table.getTypeSpecificID(it->first), it->second));
}
void
ExprNode::collectExogenous(set<pair<int, int> > &result) const
{
set<pair<int, int> > symb_ids;
collectVariables(eExogenous, symb_ids);
for(set<pair<int, int> >::const_iterator it = symb_ids.begin();
it != symb_ids.end(); it++)
result.insert(make_pair(datatree.symbol_table.getTypeSpecificID(it->first), it->second));
}
void
ExprNode::collectModelLocalVariables(set<int> &result) const
{
set<pair<int, int> > symb_ids;
collectVariables(eModelLocalVariable, symb_ids);
transform(symb_ids.begin(), symb_ids.end(), inserter(result, result.begin()),
__gnu_cxx::select1st<pair<int, int> >());
}
void void
ExprNode::computeTemporaryTerms(map<NodeID, int> &reference_count, ExprNode::computeTemporaryTerms(map<NodeID, int> &reference_count,
temporary_terms_type &temporary_terms, temporary_terms_type &temporary_terms,
@ -160,14 +191,9 @@ NumConstNode::compile(ostream &CompileCode, bool lhs_rhs, const temporary_terms_
} }
void void
NumConstNode::collectEndogenous(set<pair<int, int> > &result) const NumConstNode::collectVariables(SymbolType type_arg, set<pair<int, int> > &result) const
{ {
} }
void
NumConstNode::collectExogenous(set<pair<int, int> > &result) const
{
}
pair<bool, NodeID> pair<bool, NodeID>
NumConstNode::normalizeLinearInEndoEquation(int var_endo, NodeID Derivative) const NumConstNode::normalizeLinearInEndoEquation(int var_endo, NodeID Derivative) const
@ -507,22 +533,13 @@ VariableNode::computeTemporaryTerms(map<NodeID, int> &reference_count,
} }
void void
VariableNode::collectEndogenous(set<pair<int, int> > &result) const VariableNode::collectVariables(SymbolType type_arg, set<pair<int, int> > &result) const
{ {
if (type == eEndogenous) if (type == type_arg)
result.insert(make_pair(datatree.symbol_table.getTypeSpecificID(symb_id), lag)); result.insert(make_pair(symb_id, lag));
else if (type == eModelLocalVariable) if (type == eModelLocalVariable)
datatree.local_variables_table[symb_id]->collectEndogenous(result); datatree.local_variables_table[symb_id]->collectVariables(type_arg, result);
} }
void
VariableNode::collectExogenous(set<pair<int, int> > &result) const
{
if (type == eExogenous)
result.insert(make_pair(datatree.symbol_table.getTypeSpecificID(symb_id), lag));
else if (type == eModelLocalVariable)
datatree.local_variables_table[symb_id]->collectExogenous(result);
}
pair<bool, NodeID> pair<bool, NodeID>
VariableNode::normalizeLinearInEndoEquation(int var_endo, NodeID Derivative) const VariableNode::normalizeLinearInEndoEquation(int var_endo, NodeID Derivative) const
@ -993,16 +1010,10 @@ UnaryOpNode::compile(ostream &CompileCode, bool lhs_rhs, const temporary_terms_t
} }
void void
UnaryOpNode::collectEndogenous(set<pair<int, int> > &result) const UnaryOpNode::collectVariables(SymbolType type_arg, set<pair<int, int> > &result) const
{ {
arg->collectEndogenous(result); arg->collectVariables(type_arg, result);
} }
void
UnaryOpNode::collectExogenous(set<pair<int, int> > &result) const
{
arg->collectExogenous(result);
}
pair<bool, NodeID> pair<bool, NodeID>
UnaryOpNode::normalizeLinearInEndoEquation(int var_endo, NodeID Derivative) const UnaryOpNode::normalizeLinearInEndoEquation(int var_endo, NodeID Derivative) const
@ -1615,18 +1626,11 @@ BinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
} }
void void
BinaryOpNode::collectEndogenous(set<pair<int, int> > &result) const BinaryOpNode::collectVariables(SymbolType type_arg, set<pair<int, int> > &result) const
{ {
arg1->collectEndogenous(result); arg1->collectVariables(type_arg, result);
arg2->collectEndogenous(result); arg2->collectVariables(type_arg, result);
} }
void
BinaryOpNode::collectExogenous(set<pair<int, int> > &result) const
{
arg1->collectExogenous(result);
arg2->collectExogenous(result);
}
pair<bool, NodeID> pair<bool, NodeID>
BinaryOpNode::normalizeLinearInEndoEquation(int var_endo, NodeID Derivative) const BinaryOpNode::normalizeLinearInEndoEquation(int var_endo, NodeID Derivative) const
@ -2079,20 +2083,12 @@ TrinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
} }
void void
TrinaryOpNode::collectEndogenous(set<pair<int, int> > &result) const TrinaryOpNode::collectVariables(SymbolType type_arg, set<pair<int, int> > &result) const
{ {
arg1->collectEndogenous(result); arg1->collectVariables(type_arg, result);
arg2->collectEndogenous(result); arg2->collectVariables(type_arg, result);
arg3->collectEndogenous(result); arg3->collectVariables(type_arg, result);
} }
void
TrinaryOpNode::collectExogenous(set<pair<int, int> > &result) const
{
arg1->collectExogenous(result);
arg2->collectExogenous(result);
arg3->collectExogenous(result);
}
pair<bool, NodeID> pair<bool, NodeID>
TrinaryOpNode::normalizeLinearInEndoEquation(int var_endo, NodeID Derivative) const TrinaryOpNode::normalizeLinearInEndoEquation(int var_endo, NodeID Derivative) const
@ -2199,20 +2195,12 @@ UnknownFunctionNode::computeTemporaryTerms(map<NodeID, int> &reference_count,
} }
void void
UnknownFunctionNode::collectEndogenous(set<pair<int, int> > &result) const UnknownFunctionNode::collectVariables(SymbolType type_arg, set<pair<int, int> > &result) const
{ {
for (vector<NodeID>::const_iterator it = arguments.begin(); for (vector<NodeID>::const_iterator it = arguments.begin();
it != arguments.end(); it++) it != arguments.end(); it++)
(*it)->collectEndogenous(result); (*it)->collectVariables(type_arg, result);
} }
void
UnknownFunctionNode::collectExogenous(set<pair<int, int> > &result) const
{
for (vector<NodeID>::const_iterator it = arguments.begin();
it != arguments.end(); it++)
(*it)->collectExogenous(result);
}
void void
UnknownFunctionNode::collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const UnknownFunctionNode::collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const

View File

@ -153,19 +153,37 @@ public:
//! Writes output of node (with no temporary terms and with "outside model" output type) //! Writes output of node (with no temporary terms and with "outside model" output type)
void writeOutput(ostream &output); void writeOutput(ostream &output);
//! 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;
//! Computes the set of endogenous variables in the expression //! Computes the set of endogenous variables in the expression
/*! /*!
Endogenous are stored as integer pairs of the form (type_specific_id, lag). Endogenous are stored as integer pairs of the form (type_specific_id, lag).
They are added to the set given in argument. They are added to the set given in argument.
Note that model local variables are substituted by their expression in the computation.
*/ */
virtual void collectEndogenous(set<pair<int, int> > &result) const = 0; virtual void collectEndogenous(set<pair<int, int> > &result) const;
//! Computes the set of exogenous variables in the expression //! Computes the set of exogenous variables in the expression
/*! /*!
Exogenous are stored as integer pairs of the form (type_specific_id, lag). Exogenous are stored as integer pairs of the form (type_specific_id, lag).
They are added to the set given in argument. They are added to the set given in argument.
Note that model local variables are substituted by their expression in the computation.
*/ */
virtual void collectExogenous(set<pair<int, int> > &result) const = 0; 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;
virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const = 0; virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const = 0;
virtual void computeTemporaryTerms(map<NodeID, int> &reference_count, virtual void computeTemporaryTerms(map<NodeID, int> &reference_count,
@ -213,8 +231,7 @@ private:
public: public:
NumConstNode(DataTree &datatree_arg, int id_arg); NumConstNode(DataTree &datatree_arg, int id_arg);
virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms) const; virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms) const;
virtual void collectEndogenous(set<pair<int, int> > &result) const; virtual void collectVariables(SymbolType type_arg, set<pair<int, int> > &result) const;
virtual void collectExogenous(set<pair<int, int> > &result) const;
virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) 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 double eval(const eval_context_type &eval_context) const throw (EvalException);
virtual void compile(ostream &CompileCode, bool lhs_rhs, const temporary_terms_type &temporary_terms, map_idx_type &map_idx) const; virtual void compile(ostream &CompileCode, bool lhs_rhs, const temporary_terms_type &temporary_terms, map_idx_type &map_idx) const;
@ -237,8 +254,7 @@ private:
public: public:
VariableNode(DataTree &datatree_arg, int symb_id_arg, int lag_arg, int deriv_id_arg); VariableNode(DataTree &datatree_arg, int symb_id_arg, int lag_arg, int deriv_id_arg);
virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms) const; virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms) const;
virtual void collectEndogenous(set<pair<int, int> > &result) const; virtual void collectVariables(SymbolType type_arg, set<pair<int, int> > &result) const;
virtual void collectExogenous(set<pair<int, int> > &result) const;
virtual void computeTemporaryTerms(map<NodeID, int> &reference_count, virtual void computeTemporaryTerms(map<NodeID, int> &reference_count,
temporary_terms_type &temporary_terms, temporary_terms_type &temporary_terms,
map<NodeID, pair<int, int> > &first_occurence, map<NodeID, pair<int, int> > &first_occurence,
@ -276,8 +292,7 @@ public:
Model_Block *ModelBlock, Model_Block *ModelBlock,
int equation, int equation,
map_idx_type &map_idx) const; map_idx_type &map_idx) const;
virtual void collectEndogenous(set<pair<int, int> > &result) const; virtual void collectVariables(SymbolType type_arg, set<pair<int, int> > &result) const;
virtual void collectExogenous(set<pair<int, int> > &result) const;
virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const; virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const;
static double eval_opcode(UnaryOpcode op_code, double v) throw (EvalException); static double eval_opcode(UnaryOpcode op_code, double v) throw (EvalException);
virtual double eval(const eval_context_type &eval_context) const throw (EvalException); virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
@ -314,8 +329,7 @@ public:
Model_Block *ModelBlock, Model_Block *ModelBlock,
int equation, int equation,
map_idx_type &map_idx) const; map_idx_type &map_idx) const;
virtual void collectEndogenous(set<pair<int, int> > &result) const; virtual void collectVariables(SymbolType type_arg, set<pair<int, int> > &result) const;
virtual void collectExogenous(set<pair<int, int> > &result) const;
virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const; virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const;
static double eval_opcode(double v1, BinaryOpcode op_code, double v2) throw (EvalException); static double eval_opcode(double v1, BinaryOpcode op_code, double v2) throw (EvalException);
virtual double eval(const eval_context_type &eval_context) const throw (EvalException); virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
@ -355,8 +369,7 @@ public:
Model_Block *ModelBlock, Model_Block *ModelBlock,
int equation, int equation,
map_idx_type &map_idx) const; map_idx_type &map_idx) const;
virtual void collectEndogenous(set<pair<int, int> > &result) const; virtual void collectVariables(SymbolType type_arg, set<pair<int, int> > &result) const;
virtual void collectExogenous(set<pair<int, int> > &result) const;
virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const; virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) const;
static double eval_opcode(double v1, TrinaryOpcode op_code, double v2, double v3) throw (EvalException); 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); virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
@ -385,8 +398,7 @@ public:
Model_Block *ModelBlock, Model_Block *ModelBlock,
int equation, int equation,
map_idx_type &map_idx) const; map_idx_type &map_idx) const;
virtual void collectEndogenous(set<pair<int, int> > &result) const; virtual void collectVariables(SymbolType type_arg, set<pair<int, int> > &result) const;
virtual void collectExogenous(set<pair<int, int> > &result) const;
virtual void collectTemporary_terms(const temporary_terms_type &temporary_terms, Model_Block *ModelBlock, int Curr_Block) 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 double eval(const eval_context_type &eval_context) const throw (EvalException);
virtual void compile(ostream &CompileCode, bool lhs_rhs, const temporary_terms_type &temporary_terms, map_idx_type &map_idx) const; virtual void compile(ostream &CompileCode, bool lhs_rhs, const temporary_terms_type &temporary_terms, map_idx_type &map_idx) const;

View File

@ -27,7 +27,6 @@
#ifdef DEBUG #ifdef DEBUG
# include <ext/functional> # include <ext/functional>
using namespace __gnu_cxx;
#endif #endif
#include <boost/graph/adjacency_list.hpp> #include <boost/graph/adjacency_list.hpp>
@ -321,7 +320,7 @@ StaticModel::computeNormalization()
n1++; n1++;
pair<multimap<int, int>::const_iterator, multimap<int, int>::const_iterator> x = natural_endo2eqs.equal_range(i); pair<multimap<int, int>::const_iterator, multimap<int, int>::const_iterator> x = natural_endo2eqs.equal_range(i);
if (find_if(x.first, x.second, compose1(bind2nd(equal_to<int>(), endo2eq[i]), select2nd<multimap<int, int>::value_type>())) == x.second) if (find_if(x.first, x.second, compose1(bind2nd(equal_to<int>(), endo2eq[i]), __gnu_cxx::select2nd<multimap<int, int>::value_type>())) == x.second)
cout << "Natural normalization of variable " << symbol_table.getName(symbol_table.getID(eEndogenous, i)) cout << "Natural normalization of variable " << symbol_table.getName(symbol_table.getID(eEndogenous, i))
<< " not used." << endl; << " not used." << endl;
else else