2008-02-03 11:28:36 +01:00
|
|
|
/*
|
2015-07-30 14:40:03 +02:00
|
|
|
* Copyright (C) 2003-2015 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 _DATATREE_HH
|
|
|
|
#define _DATATREE_HH
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <list>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
2012-10-01 16:22:38 +02:00
|
|
|
#include <cmath>
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
|
|
#include "SymbolTable.hh"
|
|
|
|
#include "NumericalConstants.hh"
|
2010-02-22 17:33:38 +01:00
|
|
|
#include "ExternalFunctionsTable.hh"
|
2008-02-03 11:28:36 +01:00
|
|
|
#include "ExprNode.hh"
|
|
|
|
|
|
|
|
#define CONSTANTS_PRECISION 16
|
|
|
|
|
|
|
|
class DataTree
|
|
|
|
{
|
|
|
|
friend class ExprNode;
|
|
|
|
friend class NumConstNode;
|
|
|
|
friend class VariableNode;
|
|
|
|
friend class UnaryOpNode;
|
|
|
|
friend class BinaryOpNode;
|
|
|
|
friend class TrinaryOpNode;
|
2014-03-13 11:22:00 +01:00
|
|
|
friend class AbstractExternalFunctionNode;
|
2010-02-22 17:33:38 +01:00
|
|
|
friend class ExternalFunctionNode;
|
|
|
|
friend class FirstDerivExternalFunctionNode;
|
|
|
|
friend class SecondDerivExternalFunctionNode;
|
2008-02-03 11:28:36 +01:00
|
|
|
protected:
|
|
|
|
//! A reference to the symbol table
|
|
|
|
SymbolTable &symbol_table;
|
|
|
|
//! Reference to numerical constants table
|
|
|
|
NumericalConstants &num_constants;
|
2010-02-22 17:33:38 +01:00
|
|
|
//! A reference to the external functions table
|
|
|
|
ExternalFunctionsTable &external_functions_table;
|
2008-02-03 11:28:36 +01:00
|
|
|
|
2010-09-16 19:00:48 +02:00
|
|
|
typedef map<int, NumConstNode *> num_const_node_map_t;
|
|
|
|
num_const_node_map_t num_const_node_map;
|
2009-02-27 13:19:25 +01:00
|
|
|
//! Pair (symbol_id, lag) used as key
|
2010-09-16 19:00:48 +02:00
|
|
|
typedef map<pair<int, int>, VariableNode *> variable_node_map_t;
|
|
|
|
variable_node_map_t variable_node_map;
|
2011-10-25 16:46:12 +02:00
|
|
|
//! Pair( Pair(arg1, UnaryOpCode), Pair( Expectation Info Set, Pair(param1_symb_id, param2_symb_id)) ))
|
|
|
|
typedef map<pair<pair<expr_t, UnaryOpcode>, pair<int, pair<int, int> > >, UnaryOpNode *> unary_op_node_map_t;
|
2010-09-16 19:00:48 +02:00
|
|
|
unary_op_node_map_t unary_op_node_map;
|
2010-12-13 14:07:05 +01:00
|
|
|
//! Pair( Pair( Pair(arg1, arg2), order of Power Derivative), opCode)
|
|
|
|
typedef map<pair<pair<pair<expr_t, expr_t>, int>, BinaryOpcode>, BinaryOpNode *> binary_op_node_map_t;
|
2010-09-16 19:00:48 +02:00
|
|
|
binary_op_node_map_t binary_op_node_map;
|
2010-09-16 19:18:45 +02:00
|
|
|
typedef map<pair<pair<pair<expr_t, expr_t>, expr_t>, TrinaryOpcode>, TrinaryOpNode *> trinary_op_node_map_t;
|
2010-09-16 19:00:48 +02:00
|
|
|
trinary_op_node_map_t trinary_op_node_map;
|
2014-02-24 15:37:55 +01:00
|
|
|
|
|
|
|
// (arguments, symb_id) -> ExternalFunctionNode
|
2010-09-16 19:18:45 +02:00
|
|
|
typedef map<pair<vector<expr_t>, int>, ExternalFunctionNode *> external_function_node_map_t;
|
2010-09-16 19:00:48 +02:00
|
|
|
external_function_node_map_t external_function_node_map;
|
2014-02-24 15:37:55 +01:00
|
|
|
|
|
|
|
// ((arguments, deriv_idx), symb_id) -> FirstDerivExternalFunctionNode
|
2010-09-16 19:18:45 +02:00
|
|
|
typedef map<pair<pair<vector<expr_t>, int>, int>, FirstDerivExternalFunctionNode *> first_deriv_external_function_node_map_t;
|
2010-09-16 19:00:48 +02:00
|
|
|
first_deriv_external_function_node_map_t first_deriv_external_function_node_map;
|
2014-02-24 15:37:55 +01:00
|
|
|
|
|
|
|
// ((arguments, (deriv_idx1, deriv_idx2)), symb_id) -> SecondDerivExternalFunctionNode
|
2010-09-16 19:18:45 +02:00
|
|
|
typedef map<pair<pair<vector<expr_t>, pair<int, int> >, int>, SecondDerivExternalFunctionNode *> second_deriv_external_function_node_map_t;
|
2010-09-16 19:00:48 +02:00
|
|
|
second_deriv_external_function_node_map_t second_deriv_external_function_node_map;
|
2008-02-03 11:28:36 +01:00
|
|
|
|
2009-04-16 12:33:30 +02:00
|
|
|
//! Stores local variables value (maps symbol ID to corresponding node)
|
2010-09-16 19:18:45 +02:00
|
|
|
map<int, expr_t> local_variables_table;
|
2009-04-16 12:33:30 +02:00
|
|
|
|
|
|
|
//! 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
|
|
|
|
2015-07-30 14:40:03 +02:00
|
|
|
//! Internal implementation of ParamUsedWithLeadLag()
|
|
|
|
bool ParamUsedWithLeadLagInternal() const;
|
2009-04-16 12:33:30 +02:00
|
|
|
private:
|
2010-09-16 19:18:45 +02:00
|
|
|
typedef list<expr_t> node_list_t;
|
2009-04-16 12:33:30 +02:00
|
|
|
//! The list of nodes
|
2010-09-16 19:00:48 +02:00
|
|
|
node_list_t node_list;
|
2009-04-16 12:33:30 +02:00
|
|
|
//! A counter for filling ExprNode's idx field
|
|
|
|
int node_counter;
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
inline expr_t AddPossiblyNegativeConstant(double val);
|
2011-10-25 16:46:12 +02:00
|
|
|
inline expr_t AddUnaryOp(UnaryOpcode op_code, expr_t arg, int arg_exp_info_set = 0, int param1_symb_id = 0, int param2_symb_id = 0);
|
2010-12-13 14:07:05 +01:00
|
|
|
inline expr_t AddBinaryOp(expr_t arg1, BinaryOpcode op_code, expr_t arg2, int powerDerivOrder = 0);
|
2010-09-16 19:18:45 +02:00
|
|
|
inline expr_t AddTrinaryOp(expr_t arg1, TrinaryOpcode op_code, expr_t arg2, expr_t arg3);
|
2009-02-27 13:19:25 +01:00
|
|
|
|
2008-02-03 11:28:36 +01:00
|
|
|
public:
|
2010-02-22 17:33:38 +01:00
|
|
|
DataTree(SymbolTable &symbol_table_arg, NumericalConstants &num_constants_arg, ExternalFunctionsTable &external_functions_table_arg);
|
2008-02-03 11:28:36 +01:00
|
|
|
virtual ~DataTree();
|
2009-04-17 18:26:23 +02:00
|
|
|
|
2009-04-16 12:33:30 +02:00
|
|
|
//! Some predefined constants
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t Zero, One, Two, MinusOne, NaN, Infinity, MinusInfinity, Pi;
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
|
|
//! Raised when a local parameter is declared twice
|
2009-04-16 12:33:30 +02:00
|
|
|
class LocalVariableException
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
string name;
|
2009-12-16 18:13:23 +01:00
|
|
|
LocalVariableException(const string &name_arg) : name(name_arg)
|
|
|
|
{
|
|
|
|
}
|
2008-02-03 11:28:36 +01:00
|
|
|
};
|
|
|
|
|
2014-10-03 16:54:59 +02:00
|
|
|
class DivisionByZeroException
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2010-11-25 13:45:35 +01:00
|
|
|
//! Adds a non-negative numerical constant (possibly Inf or NaN)
|
2012-07-12 12:46:22 +02:00
|
|
|
expr_t AddNonNegativeConstant(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);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "arg1+arg2" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddPlus(expr_t iArg1, expr_t iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "arg1-arg2" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddMinus(expr_t iArg1, expr_t iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "-arg" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddUMinus(expr_t iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "arg1*arg2" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddTimes(expr_t iArg1, expr_t iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "arg1/arg2" to model tree
|
2014-10-03 16:54:59 +02:00
|
|
|
expr_t AddDivide(expr_t iArg1, expr_t iArg2) throw (DivisionByZeroException);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "arg1<arg2" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddLess(expr_t iArg1, expr_t iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "arg1>arg2" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddGreater(expr_t iArg1, expr_t iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "arg1<=arg2" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddLessEqual(expr_t iArg1, expr_t iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "arg1>=arg2" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddGreaterEqual(expr_t iArg1, expr_t iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "arg1==arg2" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddEqualEqual(expr_t iArg1, expr_t iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "arg1!=arg2" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddDifferent(expr_t iArg1, expr_t iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "arg1^arg2" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddPower(expr_t iArg1, expr_t iArg2);
|
2010-12-13 14:07:05 +01:00
|
|
|
//! Adds "getPowerDeriv(arg1, arg2, powerDerivOrder)" to model tree
|
|
|
|
expr_t AddPowerDeriv(expr_t iArg1, expr_t iArg2, int powerDerivOrder);
|
2009-10-29 18:16:10 +01:00
|
|
|
//! Adds "E(arg1)(arg2)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddExpectation(int iArg1, expr_t iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "exp(arg)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddExp(expr_t iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "log(arg)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddLog(expr_t iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "log10(arg)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddLog10(expr_t iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "cos(arg)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddCos(expr_t iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "sin(arg)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddSin(expr_t iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "tan(arg)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddTan(expr_t iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "acos(arg)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddAcos(expr_t iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "asin(arg)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddAsin(expr_t iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "atan(arg)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddAtan(expr_t iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "cosh(arg)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddCosh(expr_t iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "sinh(arg)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddSinh(expr_t iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "tanh(arg)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddTanh(expr_t iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "acosh(arg)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddAcosh(expr_t iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "asinh(arg)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddAsinh(expr_t iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "atanh(args)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddAtanh(expr_t iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "sqrt(arg)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddSqrt(expr_t iArg1);
|
2011-08-12 13:20:53 +02:00
|
|
|
//! Adds "abs(arg)" to model tree
|
|
|
|
expr_t AddAbs(expr_t iArg1);
|
|
|
|
//! Adds "sign(arg)" to model tree
|
|
|
|
expr_t AddSign(expr_t iArg1);
|
2010-03-11 11:57:34 +01:00
|
|
|
//! Adds "erf(arg)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddErf(expr_t iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "max(arg1,arg2)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddMax(expr_t iArg1, expr_t iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "min(arg1,arg2)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddMin(expr_t iArg1, expr_t iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "normcdf(arg1,arg2,arg3)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddNormcdf(expr_t iArg1, expr_t iArg2, expr_t iArg3);
|
2010-03-11 09:43:16 +01:00
|
|
|
//! Adds "normpdf(arg1,arg2,arg3)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddNormpdf(expr_t iArg1, expr_t iArg2, expr_t iArg3);
|
2009-09-10 22:09:16 +02:00
|
|
|
//! Adds "steadyState(arg)" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddSteadyState(expr_t iArg1);
|
2011-01-13 18:08:26 +01:00
|
|
|
//! Add derivative of steady state w.r.t. parameter to model tree
|
|
|
|
expr_t AddSteadyStateParamDeriv(expr_t iArg1, int param_symb_id);
|
|
|
|
//! Add 2nd derivative of steady state w.r.t. parameter to model tree
|
|
|
|
expr_t AddSteadyStateParam2ndDeriv(expr_t iArg1, int param1_symb_id, int param2_symb_id);
|
2008-02-03 11:28:36 +01:00
|
|
|
//! Adds "arg1=arg2" to model tree
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddEqual(expr_t iArg1, expr_t iArg2);
|
2009-04-16 12:33:30 +02:00
|
|
|
//! Adds a model local variable with its value
|
2010-09-16 19:18:45 +02:00
|
|
|
void AddLocalVariable(int symb_id, expr_t value) throw (LocalVariableException);
|
2010-02-22 17:33:38 +01:00
|
|
|
//! Adds an external function node
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t AddExternalFunction(int symb_id, const vector<expr_t> &arguments);
|
2010-02-22 17:33:38 +01:00
|
|
|
//! Adds an external function node for the first derivative of an external function
|
2014-03-11 17:38:47 +01:00
|
|
|
expr_t AddFirstDerivExternalFunction(int top_level_symb_id, const vector<expr_t> &arguments, int input_index);
|
2010-02-22 17:33:38 +01:00
|
|
|
//! Adds an external function node for the second derivative of an external function
|
2014-03-11 17:38:47 +01:00
|
|
|
expr_t AddSecondDerivExternalFunction(int top_level_symb_id, const vector<expr_t> &arguments, int input_index1, int input_index2);
|
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;
|
2010-02-23 16:45:56 +01:00
|
|
|
//! Checks if a given unary op is used somewhere in the data tree
|
|
|
|
bool isUnaryOpUsed(UnaryOpcode opcode) const;
|
|
|
|
//! Checks if a given binary op is used somewhere in the data tree
|
|
|
|
bool isBinaryOpUsed(BinaryOpcode opcode) const;
|
|
|
|
//! Checks if a given trinary op is used somewhere in the data tree
|
|
|
|
bool isTrinaryOpUsed(TrinaryOpcode opcode) const;
|
2010-03-03 11:43:22 +01:00
|
|
|
//! Checks if a given external function is used somewhere in the data tree
|
|
|
|
bool isExternalFunctionUsed(int symb_id) const;
|
|
|
|
//! Checks if a given first derivative external function is used somewhere in the data tree
|
|
|
|
bool isFirstDerivExternalFunctionUsed(int symb_id) const;
|
|
|
|
//! Checks if a given second derivative external function is used somewhere in the data tree
|
|
|
|
bool isSecondDerivExternalFunctionUsed(int symb_id) const;
|
2010-10-20 14:47:03 +02:00
|
|
|
//! Returns the minimum lag (as a negative number) of the given symbol in the whole data tree (and not only in the equations !!)
|
|
|
|
/*! Returns 0 if the symbol is not used */
|
|
|
|
int minLagForSymbol(int symb_id) const;
|
2010-12-13 14:07:05 +01:00
|
|
|
//! Write the Header for getPowerDeriv when use_dll is used
|
|
|
|
void writePowerDerivCHeader(ostream &output) const;
|
|
|
|
//! Write getPowerDeriv
|
|
|
|
void writePowerDeriv(ostream &output, bool use_dll) const;
|
2009-04-17 18:26:23 +02:00
|
|
|
//! Thrown when trying to access an unknown variable by deriv_id
|
|
|
|
class UnknownDerivIDException
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2010-10-15 19:05:16 +02:00
|
|
|
//! Raised when a trend is declared twice
|
|
|
|
class TrendException
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
string name;
|
|
|
|
TrendException(const string &name_arg) : name(name_arg)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-04-17 18:26:23 +02:00
|
|
|
//! 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);
|
2011-01-13 18:08:26 +01:00
|
|
|
virtual SymbolType getTypeByDerivID(int deriv_id) const throw (UnknownDerivIDException);
|
|
|
|
virtual int getLagByDerivID(int deriv_id) const throw (UnknownDerivIDException);
|
|
|
|
virtual int getSymbIDByDerivID(int deriv_id) const throw (UnknownDerivIDException);
|
2009-04-17 18:26:23 +02:00
|
|
|
//! Returns the column of the dynamic Jacobian associated to a derivation ID
|
|
|
|
virtual int getDynJacobianCol(int deriv_id) const throw (UnknownDerivIDException);
|
2011-01-13 18:08:26 +01:00
|
|
|
//! Adds to the set all the deriv IDs corresponding to parameters
|
|
|
|
virtual void addAllParamDerivId(set<int> &deriv_id_set);
|
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;
|
|
|
|
};
|
2008-02-03 11:28:36 +01:00
|
|
|
};
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
inline expr_t
|
2008-02-03 11:28:36 +01:00
|
|
|
DataTree::AddPossiblyNegativeConstant(double v)
|
|
|
|
{
|
2012-10-01 16:22:38 +02:00
|
|
|
/* Treat NaN and Inf separately. In particular, under Windows, converting
|
|
|
|
them to a string does not work as expected */
|
|
|
|
if (isnan(v))
|
|
|
|
return NaN;
|
|
|
|
if (isinf(v))
|
|
|
|
return (v < 0 ? MinusInfinity : Infinity);
|
|
|
|
|
2008-02-03 11:28:36 +01:00
|
|
|
bool neg = false;
|
|
|
|
if (v < 0)
|
|
|
|
{
|
|
|
|
v = -v;
|
|
|
|
neg = true;
|
|
|
|
}
|
|
|
|
ostringstream ost;
|
|
|
|
ost << setprecision(CONSTANTS_PRECISION) << v;
|
|
|
|
|
2010-11-25 13:45:35 +01:00
|
|
|
expr_t cnode = AddNonNegativeConstant(ost.str());
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
|
|
if (neg)
|
|
|
|
return AddUMinus(cnode);
|
|
|
|
else
|
|
|
|
return cnode;
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
inline expr_t
|
2011-10-25 16:46:12 +02:00
|
|
|
DataTree::AddUnaryOp(UnaryOpcode op_code, expr_t arg, int arg_exp_info_set, int param1_symb_id, int param2_symb_id)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
// If the node already exists in tree, share it
|
2011-10-25 16:46:12 +02:00
|
|
|
unary_op_node_map_t::iterator it = unary_op_node_map.find(make_pair(make_pair(arg, op_code), make_pair(arg_exp_info_set, make_pair(param1_symb_id, param2_symb_id))));
|
2008-02-03 11:28:36 +01:00
|
|
|
if (it != unary_op_node_map.end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
{
|
2010-09-16 19:00:48 +02:00
|
|
|
double argval = arg->eval(eval_context_t());
|
2008-02-03 11:28:36 +01:00
|
|
|
double val = UnaryOpNode::eval_opcode(op_code, argval);
|
|
|
|
return AddPossiblyNegativeConstant(val);
|
|
|
|
}
|
2009-12-16 18:13:23 +01:00
|
|
|
catch (ExprNode::EvalException &e)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2011-10-25 16:46:12 +02:00
|
|
|
return new UnaryOpNode(*this, op_code, arg, arg_exp_info_set, param1_symb_id, param2_symb_id);
|
2008-02-03 11:28:36 +01:00
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
inline expr_t
|
2010-12-13 14:07:05 +01:00
|
|
|
DataTree::AddBinaryOp(expr_t arg1, BinaryOpcode op_code, expr_t arg2, int powerDerivOrder)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
2010-12-13 14:07:05 +01:00
|
|
|
binary_op_node_map_t::iterator it = binary_op_node_map.find(make_pair(make_pair(make_pair(arg1, arg2), powerDerivOrder), op_code));
|
2008-02-03 11:28:36 +01:00
|
|
|
if (it != binary_op_node_map.end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
// Try to reduce to a constant
|
|
|
|
try
|
|
|
|
{
|
2010-09-16 19:00:48 +02:00
|
|
|
double argval1 = arg1->eval(eval_context_t());
|
|
|
|
double argval2 = arg2->eval(eval_context_t());
|
2010-12-13 14:07:05 +01:00
|
|
|
double val = BinaryOpNode::eval_opcode(argval1, op_code, argval2, powerDerivOrder);
|
2008-02-03 11:28:36 +01:00
|
|
|
return AddPossiblyNegativeConstant(val);
|
|
|
|
}
|
2009-12-16 18:13:23 +01:00
|
|
|
catch (ExprNode::EvalException &e)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
}
|
2010-12-13 14:07:05 +01:00
|
|
|
return new BinaryOpNode(*this, arg1, op_code, arg2, powerDerivOrder);
|
2008-02-03 11:28:36 +01:00
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
inline expr_t
|
|
|
|
DataTree::AddTrinaryOp(expr_t arg1, TrinaryOpcode op_code, expr_t arg2, expr_t arg3)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
2010-09-16 19:00:48 +02:00
|
|
|
trinary_op_node_map_t::iterator it = trinary_op_node_map.find(make_pair(make_pair(make_pair(arg1, arg2), arg3), op_code));
|
2008-02-03 11:28:36 +01:00
|
|
|
if (it != trinary_op_node_map.end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
// Try to reduce to a constant
|
|
|
|
try
|
|
|
|
{
|
2010-09-16 19:00:48 +02:00
|
|
|
double argval1 = arg1->eval(eval_context_t());
|
|
|
|
double argval2 = arg2->eval(eval_context_t());
|
|
|
|
double argval3 = arg3->eval(eval_context_t());
|
2008-02-03 11:28:36 +01:00
|
|
|
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)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
return new TrinaryOpNode(*this, arg1, op_code, arg2, arg3);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|