2008-02-03 11:28:36 +01:00
|
|
|
|
/*
|
2023-03-23 12:50:18 +01:00
|
|
|
|
* Copyright © 2003-2023 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
|
2021-06-09 16:52:20 +02:00
|
|
|
|
* along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2008-02-03 11:28:36 +01:00
|
|
|
|
*/
|
|
|
|
|
|
2023-12-01 15:39:01 +01:00
|
|
|
|
#ifndef DATA_TREE_HH
|
|
|
|
|
#define DATA_TREE_HH
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
2012-10-01 16:22:38 +02:00
|
|
|
|
#include <cmath>
|
2022-10-11 15:59:56 +02:00
|
|
|
|
#include <filesystem>
|
2023-11-30 15:28:57 +01:00
|
|
|
|
#include <iomanip>
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <string>
|
2022-10-11 15:59:56 +02:00
|
|
|
|
#include <string_view>
|
2023-11-30 15:28:57 +01:00
|
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
|
|
|
|
#include "ExprNode.hh"
|
2023-11-30 15:28:57 +01:00
|
|
|
|
#include "ExternalFunctionsTable.hh"
|
|
|
|
|
#include "NumericalConstants.hh"
|
2018-08-14 14:23:21 +02:00
|
|
|
|
#include "SubModel.hh"
|
2023-11-30 15:28:57 +01:00
|
|
|
|
#include "SymbolTable.hh"
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
2022-01-06 14:54:57 +01:00
|
|
|
|
using namespace std;
|
|
|
|
|
|
2008-02-03 11:28:36 +01:00
|
|
|
|
class DataTree
|
|
|
|
|
{
|
2018-09-05 17:41:11 +02:00
|
|
|
|
public:
|
2008-02-03 11:28:36 +01:00
|
|
|
|
//! A reference to the symbol table
|
2023-11-30 15:28:57 +01:00
|
|
|
|
SymbolTable& symbol_table;
|
2008-02-03 11:28:36 +01:00
|
|
|
|
//! Reference to numerical constants table
|
2023-11-30 15:28:57 +01:00
|
|
|
|
NumericalConstants& num_constants;
|
2010-02-22 17:33:38 +01:00
|
|
|
|
//! A reference to the external functions table
|
2023-11-30 15:28:57 +01:00
|
|
|
|
ExternalFunctionsTable& external_functions_table;
|
2018-10-09 18:27:19 +02:00
|
|
|
|
//! Is it possible to use leads/lags on variable nodes?
|
|
|
|
|
const bool is_dynamic;
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
2018-10-10 13:03:52 +02:00
|
|
|
|
private:
|
2018-06-04 17:35:02 +02:00
|
|
|
|
//! num_constant_id -> NumConstNode
|
2023-11-30 15:28:57 +01:00
|
|
|
|
using num_const_node_map_t = map<int, NumConstNode*>;
|
2010-09-16 19:00:48 +02:00
|
|
|
|
num_const_node_map_t num_const_node_map;
|
2018-06-04 17:35:02 +02:00
|
|
|
|
|
|
|
|
|
//! (symbol_id, lag) -> VariableNode
|
2023-11-30 15:28:57 +01:00
|
|
|
|
using variable_node_map_t = map<pair<int, int>, VariableNode*>;
|
2010-09-16 19:00:48 +02:00
|
|
|
|
variable_node_map_t variable_node_map;
|
2017-07-03 16:38:44 +02:00
|
|
|
|
|
2023-11-30 15:28:57 +01:00
|
|
|
|
//! (arg, op_code, arg_exp_info_set, param1_symb_id, param2_symb_id, adl_param_name, adl_lags) ->
|
|
|
|
|
//! UnaryOpNode
|
|
|
|
|
using unary_op_node_map_t
|
|
|
|
|
= map<tuple<expr_t, UnaryOpcode, int, int, int, string, vector<int>>, UnaryOpNode*>;
|
2010-09-16 19:00:48 +02:00
|
|
|
|
unary_op_node_map_t unary_op_node_map;
|
2018-06-04 17:35:02 +02:00
|
|
|
|
|
|
|
|
|
//! ( arg1, arg2, opCode, order of Power Derivative) -> BinaryOpNode
|
2023-11-30 15:28:57 +01:00
|
|
|
|
using binary_op_node_map_t = map<tuple<expr_t, expr_t, BinaryOpcode, int>, BinaryOpNode*>;
|
2010-09-16 19:00:48 +02:00
|
|
|
|
binary_op_node_map_t binary_op_node_map;
|
2018-06-04 17:35:02 +02:00
|
|
|
|
|
|
|
|
|
//! ( arg1, arg2, arg3, opCode) -> TrinaryOpNode
|
2023-11-30 15:28:57 +01:00
|
|
|
|
using trinary_op_node_map_t = map<tuple<expr_t, expr_t, expr_t, TrinaryOpcode>, TrinaryOpNode*>;
|
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
|
2023-11-30 15:28:57 +01:00
|
|
|
|
using external_function_node_map_t = map<pair<vector<expr_t>, int>, ExternalFunctionNode*>;
|
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
|
|
|
|
|
2018-06-04 17:35:02 +02:00
|
|
|
|
// (model_name, symb_id, forecast_horizon) -> VarExpectationNode
|
2023-11-30 15:28:57 +01:00
|
|
|
|
using var_expectation_node_map_t = map<string, VarExpectationNode*>;
|
2016-11-18 16:52:13 +01:00
|
|
|
|
var_expectation_node_map_t var_expectation_node_map;
|
|
|
|
|
|
2018-03-28 18:46:15 +02:00
|
|
|
|
// model_name -> PacExpectationNode
|
2023-11-30 15:28:57 +01:00
|
|
|
|
using pac_expectation_node_map_t = map<string, PacExpectationNode*>;
|
2018-01-30 16:33:16 +01:00
|
|
|
|
pac_expectation_node_map_t pac_expectation_node_map;
|
|
|
|
|
|
2021-10-26 18:06:26 +02:00
|
|
|
|
// model_name -> PacTargetNonstationaryNode
|
2023-11-30 15:28:57 +01:00
|
|
|
|
using pac_target_nonstationary_node_map_t = map<string, PacTargetNonstationaryNode*>;
|
2021-10-26 18:06:26 +02:00
|
|
|
|
pac_target_nonstationary_node_map_t pac_target_nonstationary_node_map;
|
|
|
|
|
|
2018-06-04 17:35:02 +02:00
|
|
|
|
// (arguments, deriv_idx, symb_id) -> FirstDerivExternalFunctionNode
|
2023-11-30 15:28:57 +01:00
|
|
|
|
using first_deriv_external_function_node_map_t
|
|
|
|
|
= map<tuple<vector<expr_t>, int, int>, FirstDerivExternalFunctionNode*>;
|
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
|
|
|
|
|
2018-06-04 17:35:02 +02:00
|
|
|
|
// (arguments, deriv_idx1, deriv_idx2, symb_id) -> SecondDerivExternalFunctionNode
|
2023-11-30 15:28:57 +01:00
|
|
|
|
using second_deriv_external_function_node_map_t
|
|
|
|
|
= map<tuple<vector<expr_t>, int, int, int>, SecondDerivExternalFunctionNode*>;
|
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
|
|
|
|
|
2021-07-13 14:35:10 +02:00
|
|
|
|
// Flag to disable simplifications related to commutativity of addition and multiplication
|
|
|
|
|
static bool no_commutativity;
|
|
|
|
|
|
2018-10-10 13:03:52 +02:00
|
|
|
|
protected:
|
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;
|
2023-11-30 15:28:57 +01:00
|
|
|
|
//! Stores the order of appearance of local variables in the model block. Needed following change
|
|
|
|
|
//! in #563
|
2017-08-28 15:14:11 +02:00
|
|
|
|
vector<int> local_variables_vector;
|
2009-04-16 12:33:30 +02:00
|
|
|
|
|
2015-07-30 14:40:03 +02:00
|
|
|
|
//! Internal implementation of ParamUsedWithLeadLag()
|
2023-12-01 14:02:38 +01:00
|
|
|
|
[[nodiscard]] bool ParamUsedWithLeadLagInternal() const;
|
2018-06-27 15:01:31 +02:00
|
|
|
|
|
2021-04-22 18:03:47 +02:00
|
|
|
|
/* Writes the contents of “new_contents” to the file “filename”. However, if
|
|
|
|
|
the file already exists and would not be modified by this operation, then do
|
|
|
|
|
nothing. */
|
2023-11-30 15:28:57 +01:00
|
|
|
|
static void writeToFileIfModified(stringstream& new_contents, const filesystem::path& filename);
|
2021-04-22 18:03:47 +02:00
|
|
|
|
|
2009-04-16 12:33:30 +02:00
|
|
|
|
private:
|
2023-11-30 15:28:57 +01:00
|
|
|
|
constexpr static int constants_precision {16};
|
2018-07-17 16:32:00 +02:00
|
|
|
|
|
2009-04-16 12:33:30 +02:00
|
|
|
|
//! The list of nodes
|
2018-09-05 16:41:33 +02:00
|
|
|
|
vector<unique_ptr<ExprNode>> node_list;
|
2009-04-16 12:33:30 +02:00
|
|
|
|
|
2023-11-30 15:28:57 +01: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,
|
|
|
|
|
const string& adl_param_name = "",
|
|
|
|
|
const vector<int>& adl_lags = vector<int>());
|
|
|
|
|
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
|
|
|
|
|
2018-10-09 18:27:19 +02:00
|
|
|
|
//! Initializes the predefined constants, used only from the constructors
|
|
|
|
|
void initConstants();
|
|
|
|
|
|
2008-02-03 11:28:36 +01:00
|
|
|
|
public:
|
2023-11-30 15:28:57 +01:00
|
|
|
|
DataTree(SymbolTable& symbol_table_arg, NumericalConstants& num_constants_arg,
|
|
|
|
|
ExternalFunctionsTable& external_functions_table_arg, bool is_static_args = false);
|
2018-09-14 17:04:06 +02:00
|
|
|
|
|
2018-10-11 17:08:44 +02:00
|
|
|
|
virtual ~DataTree() = default;
|
2009-04-17 18:26:23 +02:00
|
|
|
|
|
2023-11-30 15:28:57 +01:00
|
|
|
|
DataTree(const DataTree& d);
|
|
|
|
|
DataTree& operator=(const DataTree& d);
|
2018-10-04 17:57:56 +02:00
|
|
|
|
|
2009-04-16 12:33:30 +02:00
|
|
|
|
//! Some predefined constants
|
2021-07-16 12:08:18 +02:00
|
|
|
|
NumConstNode *Zero, *One, *Two, *Three, *NaN, *Infinity, *Pi;
|
|
|
|
|
expr_t MinusOne, MinusInfinity;
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
|
|
|
|
//! Raised when a local parameter is declared twice
|
2022-07-20 13:22:21 +02:00
|
|
|
|
struct LocalVariableException
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
|
|
|
|
string name;
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-03 16:54:59 +02:00
|
|
|
|
class DivisionByZeroException
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-05 17:41:11 +02:00
|
|
|
|
inline expr_t AddPossiblyNegativeConstant(double val);
|
2010-11-25 13:45:35 +01:00
|
|
|
|
//! Adds a non-negative numerical constant (possibly Inf or NaN)
|
2023-11-30 15:28:57 +01:00
|
|
|
|
NumConstNode* AddNonNegativeConstant(const string& value);
|
2009-04-16 12:33:30 +02:00
|
|
|
|
//! Adds a variable
|
2023-11-30 15:28:57 +01:00
|
|
|
|
VariableNode* AddVariable(int symb_id, int lag = 0);
|
2018-10-10 13:03:52 +02:00
|
|
|
|
//! Gets a variable
|
|
|
|
|
/*! Same as AddVariable, except that it fails if the variable node has not
|
2019-12-20 16:59:30 +01:00
|
|
|
|
already been created */
|
2023-12-01 14:02:38 +01:00
|
|
|
|
[[nodiscard]] VariableNode* getVariable(int symb_id, int lag = 0) const;
|
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
|
2018-06-04 12:50:53 +02:00
|
|
|
|
expr_t AddDivide(expr_t iArg1, expr_t iArg2) noexcept(false);
|
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);
|
2017-06-12 14:56:44 +02:00
|
|
|
|
//! Adds "diff(arg)" to model tree
|
|
|
|
|
expr_t AddDiff(expr_t iArg1);
|
2018-02-28 11:31:08 +01:00
|
|
|
|
//! Adds "adl(arg1, name, lag/lags)" to model tree
|
2023-11-30 15:28:57 +01:00
|
|
|
|
expr_t AddAdl(expr_t iArg1, const string& name, const vector<int>& lags);
|
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);
|
2019-07-15 18:18:26 +02:00
|
|
|
|
//! Adds "cbrt(arg)" to model tree
|
|
|
|
|
expr_t AddCbrt(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);
|
2021-12-07 15:19:40 +01:00
|
|
|
|
//! Adds "erfc(arg)" to model tree
|
|
|
|
|
expr_t AddErfc(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
|
2023-11-30 15:28:57 +01:00
|
|
|
|
BinaryOpNode* AddEqual(expr_t iArg1, expr_t iArg2);
|
2018-08-01 19:41:44 +02:00
|
|
|
|
//! Adds "var_expectation(model_name)" to model tree
|
2023-11-30 15:28:57 +01:00
|
|
|
|
expr_t AddVarExpectation(const string& model_name);
|
2018-01-30 16:33:16 +01:00
|
|
|
|
//! Adds pac_expectation command to model tree
|
2023-11-30 15:28:57 +01:00
|
|
|
|
expr_t AddPacExpectation(const string& model_name);
|
2021-10-26 18:06:26 +02:00
|
|
|
|
//! Adds a pac_target_nonstationary node to model tree
|
2023-11-30 15:28:57 +01:00
|
|
|
|
expr_t AddPacTargetNonstationary(const string& model_name);
|
2009-04-16 12:33:30 +02:00
|
|
|
|
//! Adds a model local variable with its value
|
2018-06-04 12:50:53 +02:00
|
|
|
|
void AddLocalVariable(int symb_id, expr_t value) noexcept(false);
|
2010-02-22 17:33:38 +01:00
|
|
|
|
//! Adds an external function node
|
2023-11-30 15:28:57 +01: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
|
2023-11-30 15:28:57 +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
|
2023-11-30 15:28:57 +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
|
2023-12-01 14:02:38 +01:00
|
|
|
|
[[nodiscard]] 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
|
2023-12-01 14:02:38 +01:00
|
|
|
|
[[nodiscard]] bool isUnaryOpUsed(UnaryOpcode opcode) const;
|
2019-09-10 12:37:11 +02:00
|
|
|
|
//! Checks if a given unary op is used somewhere in the data tree on an endogenous variable
|
2023-12-01 14:02:38 +01:00
|
|
|
|
[[nodiscard]] bool isUnaryOpUsedOnType(SymbolType type, UnaryOpcode opcode) const;
|
2010-02-23 16:45:56 +01:00
|
|
|
|
//! Checks if a given binary op is used somewhere in the data tree
|
2023-12-01 14:02:38 +01:00
|
|
|
|
[[nodiscard]] bool isBinaryOpUsed(BinaryOpcode opcode) const;
|
2019-09-10 12:37:11 +02:00
|
|
|
|
//! Checks if a given binary op is used somewhere in the data tree on an endogenous variable
|
2023-12-01 14:02:38 +01:00
|
|
|
|
[[nodiscard]] bool isBinaryOpUsedOnType(SymbolType type, BinaryOpcode opcode) const;
|
2023-11-30 15:28:57 +01:00
|
|
|
|
//! Returns the minimum lag (as a negative number) of the given symbol in the whole data tree (and
|
|
|
|
|
//! not only in the equations !!)
|
2010-10-20 14:47:03 +02:00
|
|
|
|
/*! Returns 0 if the symbol is not used */
|
2023-12-01 14:02:38 +01:00
|
|
|
|
[[nodiscard]] int minLagForSymbol(int symb_id) const;
|
2023-04-11 15:29:12 +02:00
|
|
|
|
/* Writes definitions of C function helpers (getPowerDeriv(), sign()) as
|
|
|
|
|
inline functions */
|
2023-11-30 15:28:57 +01:00
|
|
|
|
void writeCHelpersDefinition(ostream& output) const;
|
2023-04-11 15:29:12 +02:00
|
|
|
|
/* Writes declarations of C function helpers (getPowerDeriv(), sign()) as
|
|
|
|
|
extern inline (external definition). Those need to be included in exactly
|
|
|
|
|
one translation unit. That external definition will be used or not,
|
|
|
|
|
depending on the optimization decision by the compiler.
|
|
|
|
|
See https://en.cppreference.com/w/c/language/inline */
|
2023-11-30 15:28:57 +01:00
|
|
|
|
void writeCHelpersDeclaration(ostream& output) 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
|
2022-07-20 13:22:21 +02:00
|
|
|
|
struct TrendException
|
2010-10-15 19:05:16 +02:00
|
|
|
|
{
|
|
|
|
|
string name;
|
|
|
|
|
};
|
|
|
|
|
|
2022-07-12 15:28:52 +02:00
|
|
|
|
// Returns the derivation ID, or throws an exception if the derivation ID does not exist
|
2023-12-01 14:02:38 +01:00
|
|
|
|
[[nodiscard]] virtual int getDerivID(int symb_id, int lag) const noexcept(false);
|
2022-07-12 15:28:52 +02:00
|
|
|
|
// Get the type corresponding to a derivation ID
|
2023-12-01 14:02:38 +01:00
|
|
|
|
[[nodiscard]] virtual SymbolType getTypeByDerivID(int deriv_id) const noexcept(false);
|
2022-07-12 15:28:52 +02:00
|
|
|
|
// Get the lag corresponding to a derivation ID
|
2023-12-01 14:02:38 +01:00
|
|
|
|
[[nodiscard]] virtual int getLagByDerivID(int deriv_id) const noexcept(false);
|
2022-07-12 15:28:52 +02:00
|
|
|
|
// Get the symbol ID corresponding to a derivation ID
|
2023-12-01 14:02:38 +01:00
|
|
|
|
[[nodiscard]] virtual int getSymbIDByDerivID(int deriv_id) const noexcept(false);
|
2022-07-12 15:28:52 +02:00
|
|
|
|
// Get the type-specific ID corresponding to a derivation ID
|
2023-12-01 14:02:38 +01:00
|
|
|
|
[[nodiscard]] virtual int getTypeSpecificIDByDerivID(int deriv_id) const;
|
2022-07-12 15:28:52 +02:00
|
|
|
|
// Get the symbol name corresponding to a derivation ID
|
2023-12-01 14:02:38 +01:00
|
|
|
|
[[nodiscard]] string
|
2022-07-12 15:28:52 +02:00
|
|
|
|
getNameByDerivID(int deriv_id) const
|
|
|
|
|
{
|
|
|
|
|
return symbol_table.getName(getSymbIDByDerivID(deriv_id));
|
|
|
|
|
}
|
2022-07-11 17:33:09 +02:00
|
|
|
|
|
2022-09-14 17:07:08 +02:00
|
|
|
|
/* Returns the column of the Jacobian associated to a derivation ID.
|
|
|
|
|
The “sparse” argument selects between the legacy representation and the
|
|
|
|
|
sparse representation. */
|
2023-12-01 14:02:38 +01:00
|
|
|
|
[[nodiscard]] virtual int
|
2022-09-14 17:07:08 +02:00
|
|
|
|
getJacobianCol([[maybe_unused]] int deriv_id, [[maybe_unused]] bool sparse) const
|
2022-07-11 17:33:09 +02:00
|
|
|
|
{
|
|
|
|
|
throw UnknownDerivIDException();
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-14 17:07:08 +02:00
|
|
|
|
/* Returns the number of columns of the Jacobian
|
|
|
|
|
The “sparse” argument selects between the legacy representation and the
|
|
|
|
|
sparse representation. */
|
2023-12-01 14:02:38 +01:00
|
|
|
|
[[nodiscard]] virtual int
|
2022-09-14 17:07:08 +02:00
|
|
|
|
getJacobianColsNbr([[maybe_unused]] bool sparse) const
|
2022-07-11 17:33:09 +02:00
|
|
|
|
{
|
|
|
|
|
throw UnknownDerivIDException();
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-13 18:08:26 +01:00
|
|
|
|
//! Adds to the set all the deriv IDs corresponding to parameters
|
2023-11-30 15:28:57 +01:00
|
|
|
|
virtual void addAllParamDerivId(set<int>& deriv_id_set);
|
2009-10-02 20:36:07 +02:00
|
|
|
|
|
2023-11-30 15:28:57 +01:00
|
|
|
|
//! Returns bool indicating whether DataTree represents a Dynamic Model (returns true in
|
|
|
|
|
//! DynamicModel.hh)
|
2023-12-01 14:02:38 +01:00
|
|
|
|
[[nodiscard]] virtual bool
|
2009-12-16 18:13:23 +01:00
|
|
|
|
isDynamic() const
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
};
|
2018-09-05 17:41:11 +02:00
|
|
|
|
|
2022-07-20 13:22:21 +02:00
|
|
|
|
struct UnknownLocalVariableException
|
2018-09-05 17:41:11 +02:00
|
|
|
|
{
|
|
|
|
|
//! Symbol ID
|
|
|
|
|
int id;
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-01 14:02:38 +01:00
|
|
|
|
[[nodiscard]] expr_t
|
2019-12-20 16:59:30 +01:00
|
|
|
|
getLocalVariable(int symb_id) const
|
2018-09-05 17:41:11 +02:00
|
|
|
|
{
|
|
|
|
|
auto it = local_variables_table.find(symb_id);
|
|
|
|
|
if (it == local_variables_table.end())
|
2023-11-30 15:28:57 +01:00
|
|
|
|
throw UnknownLocalVariableException {symb_id};
|
2018-09-05 17:41:11 +02:00
|
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
|
}
|
2021-07-13 14:35:10 +02:00
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
setNoCommutativity()
|
|
|
|
|
{
|
|
|
|
|
no_commutativity = true;
|
|
|
|
|
}
|
2021-10-26 18:06:26 +02:00
|
|
|
|
|
2022-10-11 14:30:53 +02:00
|
|
|
|
/* Equivalent of MATLAB/Octave’s strsplit, except that it ignores empty
|
|
|
|
|
substring components (MATLAB/Octave adds them to the output); in
|
|
|
|
|
particular, returns an empty vector given an empty string. */
|
|
|
|
|
static vector<string> strsplit(string_view str, char delim);
|
|
|
|
|
|
2021-10-26 18:06:26 +02:00
|
|
|
|
/*! Takes a MATLAB/Octave package name (possibly with several levels nested using dots),
|
2022-10-11 15:59:56 +02:00
|
|
|
|
and returns the path to the corresponding filesystem directory.
|
2021-10-26 18:06:26 +02:00
|
|
|
|
In practice the package nesting is used for the planner_objective (stored
|
|
|
|
|
inside +objective subdir). */
|
2023-12-13 15:37:07 +01:00
|
|
|
|
static filesystem::path packageDir(const string_view& package);
|
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;
|
2018-07-17 16:32:00 +02:00
|
|
|
|
ost << setprecision(constants_precision) << v;
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
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
|
2023-11-30 15:28:57 +01:00
|
|
|
|
DataTree::AddUnaryOp(UnaryOpcode op_code, expr_t arg, int arg_exp_info_set, int param1_symb_id,
|
|
|
|
|
int param2_symb_id, const string& adl_param_name, const vector<int>& adl_lags)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
|
|
|
|
// If the node already exists in tree, share it
|
2023-11-30 15:28:57 +01:00
|
|
|
|
if (auto it = unary_op_node_map.find({arg, op_code, arg_exp_info_set, param1_symb_id,
|
|
|
|
|
param2_symb_id, adl_param_name, adl_lags});
|
2019-12-16 19:42:59 +01:00
|
|
|
|
it != unary_op_node_map.end())
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
|
|
// Try to reduce to a constant
|
2023-11-30 15:28:57 +01:00
|
|
|
|
// Case where arg is a constant and op_code == UnaryOpcode::uminus (i.e. we're adding a negative
|
|
|
|
|
// constant) is skipped
|
|
|
|
|
if (auto carg = dynamic_cast<NumConstNode*>(arg); op_code != UnaryOpcode::uminus || !carg)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-12-04 16:37:00 +01:00
|
|
|
|
double argval = arg->eval({}); // NOLINT(clang-analyzer-core.CallAndMessage)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
double val = UnaryOpNode::eval_opcode(op_code, argval);
|
|
|
|
|
return AddPossiblyNegativeConstant(val);
|
|
|
|
|
}
|
2023-11-30 15:28:57 +01:00
|
|
|
|
catch (ExprNode::EvalException& e)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-05 16:41:33 +02:00
|
|
|
|
|
2023-11-30 15:28:57 +01:00
|
|
|
|
auto sp = make_unique<UnaryOpNode>(*this, node_list.size(), op_code, arg, arg_exp_info_set,
|
|
|
|
|
param1_symb_id, param2_symb_id, adl_param_name, adl_lags);
|
2018-09-05 16:41:33 +02:00
|
|
|
|
auto p = sp.get();
|
|
|
|
|
node_list.push_back(move(sp));
|
2023-11-30 15:28:57 +01:00
|
|
|
|
unary_op_node_map.try_emplace(
|
|
|
|
|
{arg, op_code, arg_exp_info_set, param1_symb_id, param2_symb_id, adl_param_name, adl_lags},
|
|
|
|
|
p);
|
2018-09-05 16:41:33 +02:00
|
|
|
|
return p;
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
inline expr_t
|
2017-07-03 16:38:44 +02:00
|
|
|
|
DataTree::AddBinaryOp(expr_t arg1, BinaryOpcode op_code, expr_t arg2, int powerDerivOrder)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2023-11-30 15:28:57 +01:00
|
|
|
|
if (auto it = binary_op_node_map.find({arg1, arg2, op_code, powerDerivOrder});
|
2019-12-16 19:42:59 +01:00
|
|
|
|
it != binary_op_node_map.end())
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
|
|
// Try to reduce to a constant
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-12-04 16:37:00 +01:00
|
|
|
|
double argval1 = arg1->eval({}); // NOLINT(clang-analyzer-core.CallAndMessage)
|
|
|
|
|
double argval2 = arg2->eval({}); // NOLINT(clang-analyzer-core.CallAndMessage)
|
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);
|
|
|
|
|
}
|
2023-11-30 15:28:57 +01:00
|
|
|
|
catch (ExprNode::EvalException& e)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2018-09-05 16:41:33 +02:00
|
|
|
|
|
2023-11-30 15:28:57 +01:00
|
|
|
|
auto sp
|
|
|
|
|
= make_unique<BinaryOpNode>(*this, node_list.size(), arg1, op_code, arg2, powerDerivOrder);
|
2018-09-05 16:41:33 +02:00
|
|
|
|
auto p = sp.get();
|
|
|
|
|
node_list.push_back(move(sp));
|
2023-11-30 15:28:57 +01:00
|
|
|
|
binary_op_node_map.try_emplace({arg1, arg2, op_code, powerDerivOrder}, p);
|
2018-09-05 16:41:33 +02:00
|
|
|
|
return p;
|
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
|
|
|
|
{
|
2023-11-30 15:28:57 +01:00
|
|
|
|
if (auto it = trinary_op_node_map.find({arg1, arg2, arg3, op_code});
|
2019-12-16 19:42:59 +01:00
|
|
|
|
it != trinary_op_node_map.end())
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
|
|
// Try to reduce to a constant
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
double argval1 = arg1->eval({});
|
|
|
|
|
double argval2 = arg2->eval({});
|
|
|
|
|
double argval3 = arg3->eval({});
|
2008-02-03 11:28:36 +01:00
|
|
|
|
double val = TrinaryOpNode::eval_opcode(argval1, op_code, argval2, argval3);
|
|
|
|
|
return AddPossiblyNegativeConstant(val);
|
|
|
|
|
}
|
2023-11-30 15:28:57 +01:00
|
|
|
|
catch (ExprNode::EvalException& e)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2018-09-05 16:41:33 +02:00
|
|
|
|
|
|
|
|
|
auto sp = make_unique<TrinaryOpNode>(*this, node_list.size(), arg1, op_code, arg2, arg3);
|
|
|
|
|
auto p = sp.get();
|
|
|
|
|
node_list.push_back(move(sp));
|
2023-11-30 15:28:57 +01:00
|
|
|
|
trinary_op_node_map.try_emplace({arg1, arg2, arg3, op_code}, p);
|
2018-09-05 16:41:33 +02:00
|
|
|
|
return p;
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|