2008-01-11 14:42:14 +01:00
|
|
|
/*
|
2009-01-22 16:05:38 +01:00
|
|
|
* Copyright (C) 2003-2009 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/>.
|
|
|
|
*/
|
|
|
|
|
2006-11-05 00:31:17 +01:00
|
|
|
#ifndef _DATATREE_HH
|
|
|
|
#define _DATATREE_HH
|
2007-01-09 20:00:05 +01:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2006-11-05 00:31:17 +01:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
2007-01-09 20:00:05 +01:00
|
|
|
#include <list>
|
2007-05-08 21:16:35 +02:00
|
|
|
#include <sstream>
|
2007-11-11 16:24:50 +01:00
|
|
|
#include <iomanip>
|
2007-01-09 20:00:05 +01:00
|
|
|
|
2006-11-05 00:31:17 +01:00
|
|
|
#include "SymbolTable.hh"
|
|
|
|
#include "NumericalConstants.hh"
|
2007-01-09 20:00:05 +01:00
|
|
|
#include "ExprNode.hh"
|
2006-11-05 00:31:17 +01:00
|
|
|
|
2007-12-07 17:02:55 +01:00
|
|
|
#define CONSTANTS_PRECISION 16
|
|
|
|
|
2006-11-05 00:31:17 +01:00
|
|
|
class DataTree
|
|
|
|
{
|
2007-01-09 20:00:05 +01:00
|
|
|
friend class ExprNode;
|
|
|
|
friend class NumConstNode;
|
|
|
|
friend class VariableNode;
|
|
|
|
friend class UnaryOpNode;
|
|
|
|
friend class BinaryOpNode;
|
2007-11-11 16:24:50 +01:00
|
|
|
friend class TrinaryOpNode;
|
2007-10-17 11:36:56 +02:00
|
|
|
friend class UnknownFunctionNode;
|
2007-01-09 20:00:05 +01:00
|
|
|
protected:
|
2006-11-28 12:56:02 +01:00
|
|
|
//! A reference to the symbol table
|
|
|
|
SymbolTable &symbol_table;
|
2007-01-09 20:00:05 +01:00
|
|
|
//! Reference to numerical constants table
|
|
|
|
NumericalConstants &num_constants;
|
2007-06-01 13:43:49 +02:00
|
|
|
|
2009-09-30 17:10:31 +02:00
|
|
|
typedef map<int, NumConstNode *> num_const_node_map_type;
|
2007-01-09 20:00:05 +01:00
|
|
|
num_const_node_map_type num_const_node_map;
|
2009-02-27 13:19:25 +01:00
|
|
|
//! Pair (symbol_id, lag) used as key
|
2009-09-30 17:10:31 +02:00
|
|
|
typedef map<pair<int, int>, VariableNode *> variable_node_map_type;
|
2007-01-09 20:00:05 +01:00
|
|
|
variable_node_map_type variable_node_map;
|
2009-11-09 16:13:47 +01:00
|
|
|
typedef map<pair<NodeID, UnaryOpcode>, UnaryOpNode *> unary_op_node_map_type;
|
2007-01-09 20:00:05 +01:00
|
|
|
unary_op_node_map_type unary_op_node_map;
|
2009-11-09 16:13:47 +01:00
|
|
|
typedef map<pair<pair<NodeID, NodeID>, BinaryOpcode>, BinaryOpNode *> binary_op_node_map_type;
|
2007-01-09 20:00:05 +01:00
|
|
|
binary_op_node_map_type binary_op_node_map;
|
2009-12-16 18:13:23 +01:00
|
|
|
typedef map<pair<pair<pair<NodeID, NodeID>, NodeID>, TrinaryOpcode>, TrinaryOpNode *> trinary_op_node_map_type;
|
2007-11-11 16:24:50 +01:00
|
|
|
trinary_op_node_map_type trinary_op_node_map;
|
2007-01-09 20:00:05 +01:00
|
|
|
|
2009-04-16 12:33:30 +02:00
|
|
|
//! Stores local variables value (maps symbol ID to corresponding node)
|
|
|
|
map<int, NodeID> local_variables_table;
|
|
|
|
|
|
|
|
//! Internal implementation of AddVariable(), without the check on the lag
|
2009-09-30 17:10:31 +02:00
|
|
|
VariableNode *AddVariableInternal(int symb_id, int lag);
|
2009-04-16 12:33:30 +02:00
|
|
|
|
2009-11-09 16:13:47 +01:00
|
|
|
//! Is there a steady state operator in the tree?
|
|
|
|
bool containsSteadyStateOperator() const;
|
|
|
|
|
2009-04-16 12:33:30 +02:00
|
|
|
private:
|
|
|
|
typedef list<NodeID> node_list_type;
|
|
|
|
//! The list of nodes
|
|
|
|
node_list_type node_list;
|
|
|
|
//! A counter for filling ExprNode's idx field
|
|
|
|
int node_counter;
|
|
|
|
|
2007-05-08 21:16:35 +02:00
|
|
|
inline NodeID AddPossiblyNegativeConstant(double val);
|
2010-01-18 23:08:44 +01:00
|
|
|
inline NodeID AddUnaryOp(UnaryOpcode op_code, NodeID arg, int arg_exp_info_set = 0, const string &arg_exp_info_set_name="");
|
2007-01-09 20:00:05 +01:00
|
|
|
inline NodeID AddBinaryOp(NodeID arg1, BinaryOpcode op_code, NodeID arg2);
|
2007-11-11 16:24:50 +01:00
|
|
|
inline NodeID AddTrinaryOp(NodeID arg1, TrinaryOpcode op_code, NodeID arg2, NodeID arg3);
|
2009-02-27 13:19:25 +01:00
|
|
|
|
2007-01-09 20:00:05 +01:00
|
|
|
public:
|
|
|
|
DataTree(SymbolTable &symbol_table_arg, NumericalConstants &num_constants_arg);
|
|
|
|
virtual ~DataTree();
|
2009-04-17 18:26:23 +02:00
|
|
|
|
2009-04-16 12:33:30 +02:00
|
|
|
//! Some predefined constants
|
|
|
|
NodeID Zero, One, Two, MinusOne, NaN, Infinity, MinusInfinity, Pi;
|
2007-02-22 00:28:16 +01:00
|
|
|
|
2007-01-09 20:00:05 +01:00
|
|
|
//! Raised when a local parameter is declared twice
|
2009-04-16 12:33:30 +02:00
|
|
|
class LocalVariableException
|
2007-01-09 20:00:05 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
string name;
|
2009-12-16 18:13:23 +01:00
|
|
|
LocalVariableException(const string &name_arg) : name(name_arg)
|
|
|
|
{
|
|
|
|
}
|
2007-01-09 20:00:05 +01:00
|
|
|
};
|
|
|
|
|
2009-04-16 12:33:30 +02:00
|
|
|
//! Adds a numerical constant
|
2007-01-09 20:00:05 +01:00
|
|
|
NodeID AddNumConstant(const string &value);
|
2009-04-16 12:33:30 +02:00
|
|
|
//! Adds a variable
|
|
|
|
/*! The default implementation of the method refuses any lag != 0 */
|
2009-09-30 17:10:31 +02:00
|
|
|
virtual VariableNode *AddVariable(int symb_id, int lag = 0);
|
|
|
|
//! Adds a variable, using its symbol name
|
|
|
|
VariableNode *AddVariable(const string &name, int lag = 0);
|
2007-01-09 20:00:05 +01:00
|
|
|
//! Adds "arg1+arg2" to model tree
|
|
|
|
NodeID AddPlus(NodeID iArg1, NodeID iArg2);
|
|
|
|
//! Adds "arg1-arg2" to model tree
|
|
|
|
NodeID AddMinus(NodeID iArg1, NodeID iArg2);
|
|
|
|
//! Adds "-arg" to model tree
|
|
|
|
NodeID AddUMinus(NodeID iArg1);
|
|
|
|
//! Adds "arg1*arg2" to model tree
|
|
|
|
NodeID AddTimes(NodeID iArg1, NodeID iArg2);
|
|
|
|
//! Adds "arg1/arg2" to model tree
|
|
|
|
NodeID AddDivide(NodeID iArg1, NodeID iArg2);
|
2007-10-09 00:52:57 +02:00
|
|
|
//! Adds "arg1<arg2" to model tree
|
|
|
|
NodeID AddLess(NodeID iArg1, NodeID iArg2);
|
|
|
|
//! Adds "arg1>arg2" to model tree
|
|
|
|
NodeID AddGreater(NodeID iArg1, NodeID iArg2);
|
|
|
|
//! Adds "arg1<=arg2" to model tree
|
|
|
|
NodeID AddLessEqual(NodeID iArg1, NodeID iArg2);
|
|
|
|
//! Adds "arg1>=arg2" to model tree
|
|
|
|
NodeID AddGreaterEqual(NodeID iArg1, NodeID iArg2);
|
|
|
|
//! Adds "arg1==arg2" to model tree
|
|
|
|
NodeID AddEqualEqual(NodeID iArg1, NodeID iArg2);
|
|
|
|
//! Adds "arg1!=arg2" to model tree
|
|
|
|
NodeID AddDifferent(NodeID iArg1, NodeID iArg2);
|
2007-01-09 20:00:05 +01:00
|
|
|
//! Adds "arg1^arg2" to model tree
|
|
|
|
NodeID AddPower(NodeID iArg1, NodeID iArg2);
|
2009-10-29 18:16:10 +01:00
|
|
|
//! Adds "E(arg1)(arg2)" to model tree
|
|
|
|
NodeID AddExpectation(int iArg1, NodeID iArg2);
|
2010-01-18 23:08:44 +01:00
|
|
|
//! Adds "E(arg1)(arg2)" to model tree
|
|
|
|
NodeID AddExpectation(string *iArg1, NodeID iArg2);
|
2007-01-09 20:00:05 +01:00
|
|
|
//! Adds "exp(arg)" to model tree
|
|
|
|
NodeID AddExp(NodeID iArg1);
|
|
|
|
//! Adds "log(arg)" to model tree
|
|
|
|
NodeID AddLog(NodeID iArg1);
|
|
|
|
//! Adds "log10(arg)" to model tree
|
|
|
|
NodeID AddLog10(NodeID iArg1);
|
|
|
|
//! Adds "cos(arg)" to model tree
|
|
|
|
NodeID AddCos(NodeID iArg1);
|
|
|
|
//! Adds "sin(arg)" to model tree
|
|
|
|
NodeID AddSin(NodeID iArg1);
|
|
|
|
//! Adds "tan(arg)" to model tree
|
|
|
|
NodeID AddTan(NodeID iArg1);
|
|
|
|
//! Adds "acos(arg)" to model tree
|
2009-04-16 12:33:30 +02:00
|
|
|
NodeID AddAcos(NodeID iArg1);
|
2007-01-09 20:00:05 +01:00
|
|
|
//! Adds "asin(arg)" to model tree
|
2009-04-16 12:33:30 +02:00
|
|
|
NodeID AddAsin(NodeID iArg1);
|
2007-01-09 20:00:05 +01:00
|
|
|
//! Adds "atan(arg)" to model tree
|
2009-04-16 12:33:30 +02:00
|
|
|
NodeID AddAtan(NodeID iArg1);
|
2007-01-09 20:00:05 +01:00
|
|
|
//! Adds "cosh(arg)" to model tree
|
2009-04-16 12:33:30 +02:00
|
|
|
NodeID AddCosh(NodeID iArg1);
|
2007-01-09 20:00:05 +01:00
|
|
|
//! Adds "sinh(arg)" to model tree
|
2009-04-16 12:33:30 +02:00
|
|
|
NodeID AddSinh(NodeID iArg1);
|
2007-01-09 20:00:05 +01:00
|
|
|
//! Adds "tanh(arg)" to model tree
|
2009-04-16 12:33:30 +02:00
|
|
|
NodeID AddTanh(NodeID iArg1);
|
2007-01-09 20:00:05 +01:00
|
|
|
//! Adds "acosh(arg)" to model tree
|
2009-04-16 12:33:30 +02:00
|
|
|
NodeID AddAcosh(NodeID iArg1);
|
2007-01-09 20:00:05 +01:00
|
|
|
//! Adds "asinh(arg)" to model tree
|
2009-04-16 12:33:30 +02:00
|
|
|
NodeID AddAsinh(NodeID iArg1);
|
2007-01-09 20:00:05 +01:00
|
|
|
//! Adds "atanh(args)" to model tree
|
2009-04-16 12:33:30 +02:00
|
|
|
NodeID AddAtanh(NodeID iArg1);
|
2007-01-09 20:00:05 +01:00
|
|
|
//! Adds "sqrt(arg)" to model tree
|
2009-04-16 12:33:30 +02:00
|
|
|
NodeID AddSqrt(NodeID iArg1);
|
2007-10-05 21:47:27 +02:00
|
|
|
//! Adds "max(arg1,arg2)" to model tree
|
2009-04-16 12:33:30 +02:00
|
|
|
NodeID AddMax(NodeID iArg1, NodeID iArg2);
|
2007-10-05 21:47:27 +02:00
|
|
|
//! Adds "min(arg1,arg2)" to model tree
|
|
|
|
NodeID AddMin(NodeID iArg1, NodeID iArg2);
|
2007-11-11 16:24:50 +01:00
|
|
|
//! Adds "normcdf(arg1,arg2,arg3)" to model tree
|
|
|
|
NodeID AddNormcdf(NodeID iArg1, NodeID iArg2, NodeID iArg3);
|
2009-09-10 22:09:16 +02:00
|
|
|
//! Adds "steadyState(arg)" to model tree
|
|
|
|
NodeID AddSteadyState(NodeID iArg1);
|
2007-01-09 20:00:05 +01:00
|
|
|
//! Adds "arg1=arg2" to model tree
|
|
|
|
NodeID AddEqual(NodeID iArg1, NodeID iArg2);
|
2009-04-16 12:33:30 +02:00
|
|
|
//! Adds a model local variable with its value
|
|
|
|
void AddLocalVariable(const string &name, NodeID value) throw (LocalVariableException);
|
2007-03-09 18:27:46 +01:00
|
|
|
//! Adds an unknown function node
|
|
|
|
/*! \todo Use a map to share identical nodes */
|
|
|
|
NodeID AddUnknownFunction(const string &function_name, const vector<NodeID> &arguments);
|
2009-03-11 12:43:18 +01:00
|
|
|
//! Checks if a given symbol is used somewhere in the data tree
|
|
|
|
bool isSymbolUsed(int symb_id) const;
|
2009-04-17 18:26:23 +02:00
|
|
|
//! Thrown when trying to access an unknown variable by deriv_id
|
|
|
|
class UnknownDerivIDException
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
//! Returns the derivation ID, or throws an exception if the derivation ID does not exist
|
|
|
|
virtual int getDerivID(int symb_id, int lag) const throw (UnknownDerivIDException);
|
|
|
|
//! Returns the column of the dynamic Jacobian associated to a derivation ID
|
|
|
|
virtual int getDynJacobianCol(int deriv_id) const throw (UnknownDerivIDException);
|
2009-10-02 20:36:07 +02:00
|
|
|
|
2009-09-10 22:09:16 +02:00
|
|
|
//! Returns bool indicating whether DataTree represents a Dynamic Model (returns true in DynamicModel.hh)
|
2009-12-16 18:13:23 +01:00
|
|
|
virtual bool
|
|
|
|
isDynamic() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
};
|
2007-01-09 20:00:05 +01:00
|
|
|
};
|
2006-11-05 00:31:17 +01:00
|
|
|
|
2007-05-08 21:16:35 +02:00
|
|
|
inline NodeID
|
|
|
|
DataTree::AddPossiblyNegativeConstant(double v)
|
|
|
|
{
|
|
|
|
bool neg = false;
|
|
|
|
if (v < 0)
|
|
|
|
{
|
|
|
|
v = -v;
|
|
|
|
neg = true;
|
|
|
|
}
|
|
|
|
ostringstream ost;
|
2007-12-07 17:02:55 +01:00
|
|
|
ost << setprecision(CONSTANTS_PRECISION) << v;
|
2007-06-01 13:43:49 +02:00
|
|
|
|
2007-05-08 21:16:35 +02:00
|
|
|
NodeID cnode = AddNumConstant(ost.str());
|
|
|
|
|
|
|
|
if (neg)
|
|
|
|
return AddUMinus(cnode);
|
|
|
|
else
|
|
|
|
return cnode;
|
|
|
|
}
|
|
|
|
|
2007-01-09 20:00:05 +01:00
|
|
|
inline NodeID
|
2010-01-18 23:08:44 +01:00
|
|
|
DataTree::AddUnaryOp(UnaryOpcode op_code, NodeID arg, int arg_exp_info_set, const string &arg_exp_info_set_name)
|
2006-11-05 00:31:17 +01:00
|
|
|
{
|
2007-05-08 21:16:35 +02:00
|
|
|
// If the node already exists in tree, share it
|
2007-01-09 20:00:05 +01:00
|
|
|
unary_op_node_map_type::iterator it = unary_op_node_map.find(make_pair(arg, op_code));
|
|
|
|
if (it != unary_op_node_map.end())
|
|
|
|
return it->second;
|
2007-05-08 21:16:35 +02:00
|
|
|
|
|
|
|
// Try to reduce to a constant
|
|
|
|
// Case where arg is a constant and op_code == oUminus (i.e. we're adding a negative constant) is skipped
|
|
|
|
NumConstNode *carg = dynamic_cast<NumConstNode *>(arg);
|
|
|
|
if (op_code != oUminus || carg == NULL)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2007-06-04 00:35:30 +02:00
|
|
|
double argval = arg->eval(eval_context_type());
|
2007-05-08 21:16:35 +02:00
|
|
|
double val = UnaryOpNode::eval_opcode(op_code, argval);
|
|
|
|
return AddPossiblyNegativeConstant(val);
|
|
|
|
}
|
2009-12-16 18:13:23 +01:00
|
|
|
catch (ExprNode::EvalException &e)
|
2007-05-08 21:16:35 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2010-01-18 23:08:44 +01:00
|
|
|
return new UnaryOpNode(*this, op_code, arg, arg_exp_info_set, arg_exp_info_set_name);
|
2006-11-05 00:31:17 +01:00
|
|
|
}
|
|
|
|
|
2007-01-09 20:00:05 +01:00
|
|
|
inline NodeID
|
|
|
|
DataTree::AddBinaryOp(NodeID arg1, BinaryOpcode op_code, NodeID arg2)
|
2006-11-05 00:31:17 +01:00
|
|
|
{
|
2007-01-09 20:00:05 +01:00
|
|
|
binary_op_node_map_type::iterator it = binary_op_node_map.find(make_pair(make_pair(arg1, arg2), op_code));
|
|
|
|
if (it != binary_op_node_map.end())
|
|
|
|
return it->second;
|
2007-05-08 21:16:35 +02:00
|
|
|
|
|
|
|
// Try to reduce to a constant
|
|
|
|
try
|
|
|
|
{
|
2007-06-04 00:35:30 +02:00
|
|
|
double argval1 = arg1->eval(eval_context_type());
|
|
|
|
double argval2 = arg2->eval(eval_context_type());
|
2007-05-08 21:16:35 +02:00
|
|
|
double val = BinaryOpNode::eval_opcode(argval1, op_code, argval2);
|
|
|
|
return AddPossiblyNegativeConstant(val);
|
|
|
|
}
|
2009-12-16 18:13:23 +01:00
|
|
|
catch (ExprNode::EvalException &e)
|
2007-05-08 21:16:35 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
return new BinaryOpNode(*this, arg1, op_code, arg2);
|
2006-11-05 00:31:17 +01:00
|
|
|
}
|
|
|
|
|
2007-11-11 16:24:50 +01:00
|
|
|
inline NodeID
|
|
|
|
DataTree::AddTrinaryOp(NodeID arg1, TrinaryOpcode op_code, NodeID arg2, NodeID arg3)
|
|
|
|
{
|
|
|
|
trinary_op_node_map_type::iterator it = trinary_op_node_map.find(make_pair(make_pair(make_pair(arg1, arg2), arg3), op_code));
|
|
|
|
if (it != trinary_op_node_map.end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
// Try to reduce to a constant
|
|
|
|
try
|
|
|
|
{
|
|
|
|
double argval1 = arg1->eval(eval_context_type());
|
|
|
|
double argval2 = arg2->eval(eval_context_type());
|
|
|
|
double argval3 = arg3->eval(eval_context_type());
|
|
|
|
double val = TrinaryOpNode::eval_opcode(argval1, op_code, argval2, argval3);
|
|
|
|
return AddPossiblyNegativeConstant(val);
|
|
|
|
}
|
2009-12-16 18:13:23 +01:00
|
|
|
catch (ExprNode::EvalException &e)
|
2007-11-11 16:24:50 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
return new TrinaryOpNode(*this, arg1, op_code, arg2, arg3);
|
|
|
|
}
|
|
|
|
|
2006-11-05 00:31:17 +01:00
|
|
|
#endif
|