From 1e6b15f0425b5abc98d1a89c537e89b1c1f61b59 Mon Sep 17 00:00:00 2001 From: sebastien Date: Thu, 3 Apr 2008 14:47:39 +0000 Subject: [PATCH] v4 preprocessor: rewrote ExprNode::collectEndogenous() git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1778 ac1d8469-bf42-47a9-8791-bf33cf982152 --- preprocessor/ExprNode.cc | 43 ++++++++++++-------------------- preprocessor/ModelTree.cc | 14 ++++++----- preprocessor/include/ExprNode.hh | 24 ++++++++---------- 3 files changed, 34 insertions(+), 47 deletions(-) diff --git a/preprocessor/ExprNode.cc b/preprocessor/ExprNode.cc index 06a8a6912..eb4b6364b 100644 --- a/preprocessor/ExprNode.cc +++ b/preprocessor/ExprNode.cc @@ -73,18 +73,6 @@ ExprNode::cost(const temporary_terms_type &temporary_terms, bool is_matlab) cons return 0; } -int -ExprNode::present_endogenous_size() const -{ - return(present_endogenous.size()); -} - -int -ExprNode::present_endogenous_find(int var, int lag) const -{ - return(present_endogenous.find(make_pair(var,lag))!=present_endogenous.end()); -} - void ExprNode::computeTemporaryTerms(map &reference_count, temporary_terms_type &temporary_terms, @@ -172,7 +160,7 @@ NumConstNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType ou void -NumConstNode::collectEndogenous(NodeID &Id) +NumConstNode::collectEndogenous(set > &result) const { } @@ -479,10 +467,10 @@ VariableNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType ou } void -VariableNode::collectEndogenous(NodeID &Id) +VariableNode::collectEndogenous(set > &result) const { if (type == eEndogenous) - Id->present_endogenous.insert(make_pair(symb_id, lag)); + result.insert(make_pair(symb_id, lag)); } UnaryOpNode::UnaryOpNode(DataTree &datatree_arg, UnaryOpcode op_code_arg, const NodeID arg_arg) : @@ -871,9 +859,9 @@ UnaryOpNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType out } void -UnaryOpNode::collectEndogenous(NodeID &Id) +UnaryOpNode::collectEndogenous(set > &result) const { - arg->collectEndogenous(Id); + arg->collectEndogenous(result); } BinaryOpNode::BinaryOpNode(DataTree &datatree_arg, const NodeID arg1_arg, @@ -1328,10 +1316,10 @@ BinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type, } void -BinaryOpNode::collectEndogenous(NodeID &Id) +BinaryOpNode::collectEndogenous(set > &result) const { - arg1->collectEndogenous(Id); - arg2->collectEndogenous(Id); + arg1->collectEndogenous(result); + arg2->collectEndogenous(result); } TrinaryOpNode::TrinaryOpNode(DataTree &datatree_arg, const NodeID arg1_arg, @@ -1595,11 +1583,11 @@ TrinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type, } void -TrinaryOpNode::collectEndogenous(NodeID &Id) +TrinaryOpNode::collectEndogenous(set > &result) const { - arg1->collectEndogenous(Id); - arg2->collectEndogenous(Id); - arg3->collectEndogenous(Id); + arg1->collectEndogenous(result); + arg2->collectEndogenous(result); + arg3->collectEndogenous(result); } UnknownFunctionNode::UnknownFunctionNode(DataTree &datatree_arg, @@ -1655,10 +1643,11 @@ UnknownFunctionNode::computeTemporaryTerms(map &reference_count, } void -UnknownFunctionNode::collectEndogenous(NodeID &Id) +UnknownFunctionNode::collectEndogenous(set > &result) const { - cerr << "UnknownFunctionNode::collectEndogenous: not implemented" << endl; - exit(-1); + for(vector::const_iterator it = arguments.begin(); + it != arguments.end(); it++) + (*it)->collectEndogenous(result); } double diff --git a/preprocessor/ModelTree.cc b/preprocessor/ModelTree.cc index 08a96a871..8c76a171b 100644 --- a/preprocessor/ModelTree.cc +++ b/preprocessor/ModelTree.cc @@ -3647,12 +3647,13 @@ ModelTree::BlockLinear(Model_Block *ModelBlock) if (it!= first_derivatives.end()) { NodeID Id = it->second; - Id->collectEndogenous(Id); - if (Id->present_endogenous_size()>0) + set > endogenous; + Id->collectEndogenous(endogenous); + if (endogenous.size() > 0) { for(l=0;lBlock_List[j].Size;l++) { - if (Id->present_endogenous_find(ModelBlock->Block_List[j].Variable[l],0)) + if (endogenous.find(make_pair(ModelBlock->Block_List[j].Variable[l], 0)) != endogenous.end()) { ModelBlock->Block_List[j].is_linear=false; goto follow; @@ -3675,12 +3676,13 @@ ModelTree::BlockLinear(Model_Block *ModelBlock) NodeID Id = it->second; if (it!= first_derivatives.end()) { - Id->collectEndogenous(Id); - if (Id->present_endogenous_size()>0) + set > endogenous; + Id->collectEndogenous(endogenous); + if (endogenous.size() > 0) { for(l=0;lBlock_List[j].Size;l++) { - if (Id->present_endogenous_find(ModelBlock->Block_List[j].Variable[l],k1)) + if (endogenous.find(make_pair(ModelBlock->Block_List[j].Variable[l], k1)) != endogenous.end()) { ModelBlock->Block_List[j].is_linear=false; goto follow; diff --git a/preprocessor/include/ExprNode.hh b/preprocessor/include/ExprNode.hh index c3779c4e9..2e8fb3bb5 100644 --- a/preprocessor/include/ExprNode.hh +++ b/preprocessor/include/ExprNode.hh @@ -116,10 +116,6 @@ protected: /*! Nodes included in temporary_terms are considered having a null cost */ virtual int cost(const temporary_terms_type &temporary_terms, bool is_matlab) const; - //! set of endogenous variables in the current expression - //! - set< pair > present_endogenous; - public: ExprNode(DataTree &datatree_arg); virtual ~ExprNode(); @@ -143,16 +139,16 @@ public: //! Writes output of node (with no temporary terms and with "outside model" output type) void writeOutput(ostream &output); - //! Collects the Endogenous in a expression - virtual void collectEndogenous(NodeID &Id) = 0; + //! 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 */ + virtual void collectEndogenous(set > &result) const = 0; virtual void computeTemporaryTerms(map &reference_count, temporary_terms_type &temporary_terms, map &first_occurence, int Curr_block, Model_Block *ModelBlock, map_idx_type &map_idx) const; - int present_endogenous_size() const; - int present_endogenous_find(int var, int lag) const; class EvalException { @@ -182,7 +178,7 @@ private: public: NumConstNode(DataTree &datatree_arg, int id_arg); virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms) const; - virtual void collectEndogenous(NodeID &Id); + virtual void collectEndogenous(set > &result) 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; }; @@ -201,7 +197,7 @@ private: public: VariableNode(DataTree &datatree_arg, int symb_id_arg, Type type_arg, int lag_arg); virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms = temporary_terms_type()) const; - virtual void collectEndogenous(NodeID &Id); + virtual void collectEndogenous(set > &result) 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; }; @@ -226,7 +222,7 @@ public: int Curr_block, Model_Block *ModelBlock, map_idx_type &map_idx) const; - virtual void collectEndogenous(NodeID &Id); + virtual void collectEndogenous(set > &result) const; static double eval_opcode(UnaryOpcode op_code, double v) throw (EvalException); 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; @@ -253,7 +249,7 @@ public: int Curr_block, Model_Block *ModelBlock, map_idx_type &map_idx) const; - virtual void collectEndogenous(NodeID &Id); + virtual void collectEndogenous(set > &result) const; 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 void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const; @@ -285,7 +281,7 @@ public: int Curr_block, Model_Block *ModelBlock, map_idx_type &map_idx) const; - virtual void collectEndogenous(NodeID &Id); + virtual void collectEndogenous(set > &result) const; 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 void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const; @@ -310,7 +306,7 @@ public: int Curr_block, Model_Block *ModelBlock, map_idx_type &map_idx) const; - virtual void collectEndogenous(NodeID &Id); + virtual void collectEndogenous(set > &result) 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; };