2008-02-03 11:28:36 +01:00
|
|
|
|
/*
|
2021-04-16 17:34:13 +02:00
|
|
|
|
* Copyright © 2003-2021 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
|
|
|
|
*/
|
|
|
|
|
|
2008-06-28 13:20:45 +02:00
|
|
|
|
#include <cstdlib>
|
2009-04-28 18:21:39 +02:00
|
|
|
|
#include <cassert>
|
2008-02-03 11:28:36 +01:00
|
|
|
|
#include <iostream>
|
2018-06-27 15:01:31 +02:00
|
|
|
|
#include <regex>
|
2021-04-22 18:03:47 +02:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <iterator>
|
2019-04-04 17:01:37 +02:00
|
|
|
|
#include <filesystem>
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
|
|
|
|
#include "DataTree.hh"
|
|
|
|
|
|
2021-07-13 14:35:10 +02:00
|
|
|
|
bool DataTree::no_commutativity = false;
|
|
|
|
|
|
2018-10-09 18:27:19 +02:00
|
|
|
|
void
|
|
|
|
|
DataTree::initConstants()
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2010-11-25 13:45:35 +01:00
|
|
|
|
Zero = AddNonNegativeConstant("0");
|
|
|
|
|
One = AddNonNegativeConstant("1");
|
|
|
|
|
Two = AddNonNegativeConstant("2");
|
2019-07-15 18:18:26 +02:00
|
|
|
|
Three = AddNonNegativeConstant("3");
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
|
|
|
|
MinusOne = AddUMinus(One);
|
2009-01-22 16:05:38 +01:00
|
|
|
|
|
2010-11-25 13:45:35 +01:00
|
|
|
|
NaN = AddNonNegativeConstant("NaN");
|
|
|
|
|
Infinity = AddNonNegativeConstant("Inf");
|
2009-01-22 16:05:38 +01:00
|
|
|
|
MinusInfinity = AddUMinus(Infinity);
|
2009-04-16 12:33:30 +02:00
|
|
|
|
|
2010-11-25 13:45:35 +01:00
|
|
|
|
Pi = AddNonNegativeConstant("3.141592653589793");
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-09 18:27:19 +02:00
|
|
|
|
DataTree::DataTree(SymbolTable &symbol_table_arg,
|
|
|
|
|
NumericalConstants &num_constants_arg,
|
|
|
|
|
ExternalFunctionsTable &external_functions_table_arg,
|
|
|
|
|
bool is_dynamic_arg) :
|
|
|
|
|
symbol_table{symbol_table_arg},
|
|
|
|
|
num_constants{num_constants_arg},
|
|
|
|
|
external_functions_table{external_functions_table_arg},
|
2019-12-20 16:59:30 +01:00
|
|
|
|
is_dynamic{is_dynamic_arg}
|
2018-10-09 18:27:19 +02:00
|
|
|
|
{
|
|
|
|
|
initConstants();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DataTree::DataTree(const DataTree &d) :
|
2019-12-20 16:59:30 +01:00
|
|
|
|
symbol_table{d.symbol_table},
|
|
|
|
|
num_constants{d.num_constants},
|
|
|
|
|
external_functions_table{d.external_functions_table},
|
|
|
|
|
is_dynamic{d.is_dynamic},
|
|
|
|
|
local_variables_vector{d.local_variables_vector}
|
2018-10-09 18:27:19 +02:00
|
|
|
|
{
|
|
|
|
|
// Constants must be initialized first because they are used in some Add* methods
|
|
|
|
|
initConstants();
|
|
|
|
|
|
2019-12-20 16:59:30 +01:00
|
|
|
|
for (const auto &it : d.node_list)
|
2018-10-10 13:07:25 +02:00
|
|
|
|
it->clone(*this);
|
2018-10-09 18:27:19 +02:00
|
|
|
|
|
|
|
|
|
assert(node_list.size() == d.node_list.size());
|
|
|
|
|
|
2019-12-20 16:59:30 +01:00
|
|
|
|
for (const auto &it : d.local_variables_table)
|
2018-10-10 13:07:25 +02:00
|
|
|
|
local_variables_table[it.first] = it.second->clone(*this);
|
2018-10-09 18:27:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DataTree &
|
|
|
|
|
DataTree::operator=(const DataTree &d)
|
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
assert(&symbol_table == &d.symbol_table);
|
|
|
|
|
assert(&num_constants == &d.num_constants);
|
|
|
|
|
assert(&external_functions_table == &d.external_functions_table);
|
|
|
|
|
assert(is_dynamic == d.is_dynamic);
|
2018-10-09 18:27:19 +02:00
|
|
|
|
|
|
|
|
|
num_const_node_map.clear();
|
|
|
|
|
variable_node_map.clear();
|
|
|
|
|
unary_op_node_map.clear();
|
|
|
|
|
binary_op_node_map.clear();
|
|
|
|
|
trinary_op_node_map.clear();
|
|
|
|
|
external_function_node_map.clear();
|
|
|
|
|
var_expectation_node_map.clear();
|
|
|
|
|
pac_expectation_node_map.clear();
|
|
|
|
|
first_deriv_external_function_node_map.clear();
|
|
|
|
|
second_deriv_external_function_node_map.clear();
|
|
|
|
|
|
|
|
|
|
node_list.clear();
|
|
|
|
|
|
|
|
|
|
// Constants must be initialized first because they are used in some Add* methods
|
|
|
|
|
initConstants();
|
|
|
|
|
|
2020-09-29 17:54:56 +02:00
|
|
|
|
/* Model local variables must be next, because they can be evaluated in Add*
|
2021-04-16 17:34:13 +02:00
|
|
|
|
methods when the model equations are added. They need to be cloned in
|
|
|
|
|
order of appearance in the model block (hence with
|
|
|
|
|
local_variables_vector), because if there is a model_local_variable statement
|
|
|
|
|
the symbol IDs ordering may not be the right one (see dynare#1782) */
|
|
|
|
|
for (const auto &id : d.local_variables_vector)
|
|
|
|
|
local_variables_table[id] = d.local_variables_table.at(id)->clone(*this);
|
2020-09-29 17:54:56 +02:00
|
|
|
|
|
2019-12-20 16:59:30 +01:00
|
|
|
|
for (const auto &it : d.node_list)
|
2018-10-10 13:07:25 +02:00
|
|
|
|
it->clone(*this);
|
2018-10-09 18:27:19 +02:00
|
|
|
|
|
|
|
|
|
assert(node_list.size() == d.node_list.size());
|
|
|
|
|
|
|
|
|
|
local_variables_vector = d.local_variables_vector;
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 12:08:18 +02:00
|
|
|
|
NumConstNode *
|
2012-07-12 12:46:22 +02:00
|
|
|
|
DataTree::AddNonNegativeConstant(const string &value)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2010-11-25 13:45:35 +01:00
|
|
|
|
int id = num_constants.AddNonNegativeConstant(value);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (auto it = num_const_node_map.find(id);
|
|
|
|
|
it != num_const_node_map.end())
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return it->second;
|
2018-09-05 16:41:33 +02:00
|
|
|
|
|
|
|
|
|
auto sp = make_unique<NumConstNode>(*this, node_list.size(), id);
|
|
|
|
|
auto p = sp.get();
|
|
|
|
|
node_list.push_back(move(sp));
|
|
|
|
|
num_const_node_map[id] = p;
|
|
|
|
|
return p;
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2009-09-30 17:10:31 +02:00
|
|
|
|
VariableNode *
|
2018-10-09 18:27:19 +02:00
|
|
|
|
DataTree::AddVariable(int symb_id, int lag)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2018-10-09 18:27:19 +02:00
|
|
|
|
if (lag != 0 && !is_dynamic)
|
|
|
|
|
{
|
|
|
|
|
cerr << "Leads/lags not authorized in this DataTree" << endl;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (auto it = variable_node_map.find({ symb_id, lag });
|
|
|
|
|
it != variable_node_map.end())
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return it->second;
|
2018-09-05 16:41:33 +02:00
|
|
|
|
|
|
|
|
|
auto sp = make_unique<VariableNode>(*this, node_list.size(), symb_id, lag);
|
|
|
|
|
auto p = sp.get();
|
|
|
|
|
node_list.push_back(move(sp));
|
|
|
|
|
variable_node_map[{ symb_id, lag }] = p;
|
|
|
|
|
return p;
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
2009-09-30 17:10:31 +02:00
|
|
|
|
|
2018-10-10 13:03:52 +02:00
|
|
|
|
VariableNode *
|
|
|
|
|
DataTree::getVariable(int symb_id, int lag) const
|
|
|
|
|
{
|
|
|
|
|
auto it = variable_node_map.find({ symb_id, lag });
|
|
|
|
|
if (it == variable_node_map.end())
|
|
|
|
|
{
|
|
|
|
|
cerr << "DataTree::getVariable: unknown variable node for symb_id=" << symb_id << " and lag=" << lag << endl;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
return it->second;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-30 14:40:03 +02:00
|
|
|
|
bool
|
|
|
|
|
DataTree::ParamUsedWithLeadLagInternal() const
|
|
|
|
|
{
|
2019-12-20 16:59:30 +01:00
|
|
|
|
for (const auto &it : variable_node_map)
|
2018-07-17 18:34:07 +02:00
|
|
|
|
if (symbol_table.getType(it.first.first) == SymbolType::parameter && it.first.second != 0)
|
2015-07-30 14:40:03 +02:00
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddPlus(expr_t iArg1, expr_t iArg2)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg2 == Zero)
|
|
|
|
|
return iArg1;
|
2019-12-12 13:10:55 +01:00
|
|
|
|
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg1 == Zero)
|
|
|
|
|
return iArg2;
|
2019-12-12 13:10:55 +01:00
|
|
|
|
|
2019-12-16 19:42:59 +01:00
|
|
|
|
// Simplify x+(-y) in x-y
|
|
|
|
|
if (auto uarg2 = dynamic_cast<UnaryOpNode *>(iArg2);
|
|
|
|
|
uarg2 && uarg2->op_code == UnaryOpcode::uminus)
|
|
|
|
|
return AddMinus(iArg1, uarg2->arg);
|
2018-11-05 12:30:42 +01:00
|
|
|
|
|
2019-12-16 19:42:59 +01:00
|
|
|
|
// Simplify (-x)+y in y-x
|
|
|
|
|
if (auto uarg1 = dynamic_cast<UnaryOpNode *>(iArg1);
|
|
|
|
|
uarg1 && uarg1->op_code == UnaryOpcode::uminus)
|
|
|
|
|
return AddMinus(iArg2, uarg1->arg);
|
|
|
|
|
|
|
|
|
|
// Simplify (x-y)+y in x
|
|
|
|
|
if (auto barg1 = dynamic_cast<BinaryOpNode *>(iArg1);
|
|
|
|
|
barg1 && barg1->op_code == BinaryOpcode::minus && barg1->arg2 == iArg2)
|
|
|
|
|
return barg1->arg1;
|
|
|
|
|
|
|
|
|
|
// Simplify y+(x-y) in x
|
|
|
|
|
if (auto barg2 = dynamic_cast<BinaryOpNode *>(iArg2);
|
|
|
|
|
barg2 && barg2->op_code == BinaryOpcode::minus && barg2->arg2 == iArg1)
|
|
|
|
|
return barg2->arg1;
|
|
|
|
|
|
|
|
|
|
// To treat commutativity of "+"
|
|
|
|
|
// Nodes iArg1 and iArg2 are sorted by index
|
2021-07-13 14:35:10 +02:00
|
|
|
|
if (iArg1->idx > iArg2->idx && !no_commutativity)
|
2019-12-16 19:42:59 +01:00
|
|
|
|
swap(iArg1, iArg2);
|
|
|
|
|
return AddBinaryOp(iArg1, BinaryOpcode::plus, iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddMinus(expr_t iArg1, expr_t iArg2)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2009-04-28 18:04:29 +02:00
|
|
|
|
if (iArg2 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return iArg1;
|
2009-04-28 18:04:29 +02:00
|
|
|
|
|
|
|
|
|
if (iArg1 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return AddUMinus(iArg2);
|
2009-04-28 18:04:29 +02:00
|
|
|
|
|
|
|
|
|
if (iArg1 == iArg2)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return Zero;
|
2009-04-28 18:04:29 +02:00
|
|
|
|
|
2019-12-11 19:08:01 +01:00
|
|
|
|
// Simplify x-(-y) in x+y
|
|
|
|
|
if (auto uarg2 = dynamic_cast<UnaryOpNode *>(iArg2);
|
|
|
|
|
uarg2 && uarg2->op_code == UnaryOpcode::uminus)
|
|
|
|
|
return AddPlus(iArg1, uarg2->arg);
|
|
|
|
|
|
2019-12-12 13:10:55 +01:00
|
|
|
|
// Simplify (x+y)-y and (y+x)-y in x
|
|
|
|
|
if (auto barg1 = dynamic_cast<BinaryOpNode *>(iArg1);
|
|
|
|
|
barg1 && barg1->op_code == BinaryOpcode::plus)
|
|
|
|
|
{
|
|
|
|
|
if (barg1->arg2 == iArg2)
|
|
|
|
|
return barg1->arg1;
|
|
|
|
|
if (barg1->arg1 == iArg2)
|
|
|
|
|
return barg1->arg2;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddBinaryOp(iArg1, BinaryOpcode::minus, iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddUMinus(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg1 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return Zero;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
// Simplify -(-x) in x
|
|
|
|
|
if (auto uarg = dynamic_cast<UnaryOpNode *>(iArg1);
|
|
|
|
|
uarg && uarg->op_code == UnaryOpcode::uminus)
|
|
|
|
|
return uarg->arg;
|
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::uminus, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddTimes(expr_t iArg1, expr_t iArg2)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg1 == Zero || iArg2 == Zero)
|
|
|
|
|
return Zero;
|
|
|
|
|
|
|
|
|
|
if (iArg1 == One)
|
|
|
|
|
return iArg2;
|
|
|
|
|
|
|
|
|
|
if (iArg2 == One)
|
|
|
|
|
return iArg1;
|
|
|
|
|
|
2008-02-03 11:28:36 +01:00
|
|
|
|
if (iArg1 == MinusOne)
|
|
|
|
|
return AddUMinus(iArg2);
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
if (iArg2 == MinusOne)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return AddUMinus(iArg1);
|
2019-12-12 13:06:50 +01:00
|
|
|
|
|
2019-12-16 19:42:59 +01:00
|
|
|
|
// Simplify (x/y)*y in x
|
|
|
|
|
if (auto barg1 = dynamic_cast<BinaryOpNode *>(iArg1);
|
|
|
|
|
barg1 && barg1->op_code == BinaryOpcode::divide && barg1->arg2 == iArg2)
|
|
|
|
|
return barg1->arg1;
|
2019-12-12 13:06:50 +01:00
|
|
|
|
|
2019-12-16 19:42:59 +01:00
|
|
|
|
// Simplify y*(x/y) in x
|
|
|
|
|
if (auto barg2 = dynamic_cast<BinaryOpNode *>(iArg2);
|
|
|
|
|
barg2 && barg2->op_code == BinaryOpcode::divide && barg2->arg2 == iArg1)
|
|
|
|
|
return barg2->arg1;
|
|
|
|
|
|
|
|
|
|
// To treat commutativity of "*"
|
|
|
|
|
// Nodes iArg1 and iArg2 are sorted by index
|
2021-07-13 14:35:10 +02:00
|
|
|
|
if (iArg1->idx > iArg2->idx && !no_commutativity)
|
2019-12-16 19:42:59 +01:00
|
|
|
|
swap(iArg1, iArg2);
|
|
|
|
|
return AddBinaryOp(iArg1, BinaryOpcode::times, iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
2018-06-04 12:50:53 +02:00
|
|
|
|
DataTree::AddDivide(expr_t iArg1, expr_t iArg2) noexcept(false)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2009-04-28 18:04:29 +02:00
|
|
|
|
if (iArg2 == One)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return iArg1;
|
2009-04-28 18:04:29 +02:00
|
|
|
|
|
|
|
|
|
// This test should be before the next two, otherwise 0/0 won't be rejected
|
|
|
|
|
if (iArg2 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2009-04-28 18:04:29 +02:00
|
|
|
|
cerr << "ERROR: Division by zero!" << endl;
|
2014-10-03 16:54:59 +02:00
|
|
|
|
throw DivisionByZeroException();
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
2009-04-28 18:04:29 +02:00
|
|
|
|
|
|
|
|
|
if (iArg1 == Zero)
|
|
|
|
|
return Zero;
|
|
|
|
|
|
|
|
|
|
if (iArg1 == iArg2)
|
|
|
|
|
return One;
|
|
|
|
|
|
2019-12-11 19:07:52 +01:00
|
|
|
|
// Simplify x/(1/y) in x*y
|
|
|
|
|
if (auto barg2 = dynamic_cast<BinaryOpNode *>(iArg2);
|
|
|
|
|
barg2 && barg2->op_code == BinaryOpcode::divide && barg2->arg1 == One)
|
|
|
|
|
return AddTimes(iArg1, barg2->arg2);
|
|
|
|
|
|
2019-12-12 13:06:50 +01:00
|
|
|
|
// Simplify (x*y)/y and (y*x)/y in x
|
|
|
|
|
if (auto barg1 = dynamic_cast<BinaryOpNode *>(iArg1);
|
|
|
|
|
barg1 && barg1->op_code == BinaryOpcode::times)
|
|
|
|
|
{
|
|
|
|
|
if (barg1->arg2 == iArg2)
|
|
|
|
|
return barg1->arg1;
|
|
|
|
|
if (barg1->arg1 == iArg2)
|
|
|
|
|
return barg1->arg2;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddBinaryOp(iArg1, BinaryOpcode::divide, iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddLess(expr_t iArg1, expr_t iArg2)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddBinaryOp(iArg1, BinaryOpcode::less, iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddGreater(expr_t iArg1, expr_t iArg2)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddBinaryOp(iArg1, BinaryOpcode::greater, iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddLessEqual(expr_t iArg1, expr_t iArg2)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddBinaryOp(iArg1, BinaryOpcode::lessEqual, iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddGreaterEqual(expr_t iArg1, expr_t iArg2)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddBinaryOp(iArg1, BinaryOpcode::greaterEqual, iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddEqualEqual(expr_t iArg1, expr_t iArg2)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddBinaryOp(iArg1, BinaryOpcode::equalEqual, iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddDifferent(expr_t iArg1, expr_t iArg2)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddBinaryOp(iArg1, BinaryOpcode::different, iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddPower(expr_t iArg1, expr_t iArg2)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
// This one comes first, because 0⁰=1
|
|
|
|
|
if (iArg2 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return One;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
if (iArg1 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return Zero;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
if (iArg1 == One)
|
|
|
|
|
return One;
|
|
|
|
|
|
|
|
|
|
if (iArg2 == One)
|
|
|
|
|
return iArg1;
|
|
|
|
|
|
|
|
|
|
return AddBinaryOp(iArg1, BinaryOpcode::power, iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-12-13 14:07:05 +01:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddPowerDeriv(expr_t iArg1, expr_t iArg2, int powerDerivOrder)
|
|
|
|
|
{
|
|
|
|
|
assert(powerDerivOrder > 0);
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddBinaryOp(iArg1, BinaryOpcode::powerDeriv, iArg2, powerDerivOrder);
|
2010-12-13 14:07:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-12 14:56:44 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddDiff(expr_t iArg1)
|
|
|
|
|
{
|
2019-03-05 12:42:37 +01:00
|
|
|
|
if (iArg1->maxLead() > 0)
|
2019-08-20 12:50:41 +02:00
|
|
|
|
// Issue preprocessor#21: always expand diffs with leads
|
2019-03-05 12:42:37 +01:00
|
|
|
|
return AddMinus(iArg1, iArg1->decreaseLeadsLags(1));
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddUnaryOp(UnaryOpcode::diff, iArg1);
|
2017-06-12 14:56:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-03 17:21:11 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddAdl(expr_t iArg1, const string &name, const vector<int> &lags)
|
|
|
|
|
{
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddUnaryOp(UnaryOpcode::adl, iArg1, 0, 0, 0, string(name), lags);
|
2017-07-03 17:21:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddExp(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg1 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return One;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::exp, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddLog(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-01-28 15:15:58 +01:00
|
|
|
|
if (iArg1 == One)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return Zero;
|
2019-01-28 15:15:58 +01:00
|
|
|
|
|
|
|
|
|
if (iArg1 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2009-04-28 18:04:29 +02:00
|
|
|
|
cerr << "ERROR: log(0) not defined!" << endl;
|
2008-10-29 16:33:16 +01:00
|
|
|
|
exit(EXIT_FAILURE);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
2019-01-28 15:15:58 +01:00
|
|
|
|
|
2019-12-16 19:42:59 +01:00
|
|
|
|
// Simplify log(1/x) in −log(x)
|
|
|
|
|
if (auto barg1 = dynamic_cast<BinaryOpNode *>(iArg1);
|
|
|
|
|
barg1 && barg1->op_code == BinaryOpcode::divide && barg1->arg1 == One)
|
2019-01-28 15:15:58 +01:00
|
|
|
|
return AddUMinus(AddLog(barg1->arg2));
|
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::log, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddLog10(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-01-28 15:15:58 +01:00
|
|
|
|
if (iArg1 == One)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return Zero;
|
2019-01-28 15:15:58 +01:00
|
|
|
|
|
|
|
|
|
if (iArg1 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2009-04-28 18:04:29 +02:00
|
|
|
|
cerr << "ERROR: log10(0) not defined!" << endl;
|
2008-10-29 16:33:16 +01:00
|
|
|
|
exit(EXIT_FAILURE);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
2019-01-28 15:15:58 +01:00
|
|
|
|
|
2019-12-16 19:42:59 +01:00
|
|
|
|
// Simplify log₁₀(1/x) in −log₁₀(x)
|
|
|
|
|
if (auto barg1 = dynamic_cast<BinaryOpNode *>(iArg1);
|
|
|
|
|
barg1 && barg1->op_code == BinaryOpcode::divide && barg1->arg1 == One)
|
2019-01-28 15:15:58 +01:00
|
|
|
|
return AddUMinus(AddLog10(barg1->arg2));
|
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::log10, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddCos(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg1 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return One;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::cos, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddSin(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg1 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return Zero;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::sin, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddTan(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg1 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return Zero;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::tan, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddAcos(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg1 == One)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return Zero;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::acos, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddAsin(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg1 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return Zero;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::asin, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddAtan(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg1 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return Zero;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::atan, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddCosh(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg1 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return One;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::cosh, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddSinh(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg1 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return Zero;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::sinh, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddTanh(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg1 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return Zero;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::tanh, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddAcosh(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg1 == One)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return Zero;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::acosh, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddAsinh(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg1 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return Zero;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::asinh, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddAtanh(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg1 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return Zero;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::atanh, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddSqrt(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg1 == Zero)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
return Zero;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
if (iArg1 == One)
|
|
|
|
|
return One;
|
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::sqrt, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-15 18:18:26 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddCbrt(expr_t iArg1)
|
|
|
|
|
{
|
|
|
|
|
if (iArg1 == Zero)
|
|
|
|
|
return Zero;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
if (iArg1 == One)
|
2019-07-15 18:18:26 +02:00
|
|
|
|
return One;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::cbrt, iArg1);
|
2019-07-15 18:18:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-08-12 13:20:53 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddAbs(expr_t iArg1)
|
|
|
|
|
{
|
|
|
|
|
if (iArg1 == Zero)
|
|
|
|
|
return Zero;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
2011-08-12 13:20:53 +02:00
|
|
|
|
if (iArg1 == One)
|
|
|
|
|
return One;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::abs, iArg1);
|
2011-08-12 13:20:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddSign(expr_t iArg1)
|
|
|
|
|
{
|
|
|
|
|
if (iArg1 == Zero)
|
|
|
|
|
return Zero;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
2011-08-12 13:20:53 +02:00
|
|
|
|
if (iArg1 == One)
|
|
|
|
|
return One;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::sign, iArg1);
|
2011-08-12 13:20:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddErf(expr_t iArg1)
|
2010-03-11 11:57:34 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (iArg1 == Zero)
|
2010-03-11 11:57:34 +01:00
|
|
|
|
return Zero;
|
2019-12-16 19:42:59 +01:00
|
|
|
|
|
|
|
|
|
return AddUnaryOp(UnaryOpcode::erf, iArg1);
|
2010-03-11 11:57:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddMax(expr_t iArg1, expr_t iArg2)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddBinaryOp(iArg1, BinaryOpcode::max, iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddMin(expr_t iArg1, expr_t iArg2)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddBinaryOp(iArg1, BinaryOpcode::min, iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddNormcdf(expr_t iArg1, expr_t iArg2, expr_t iArg3)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddTrinaryOp(iArg1, TrinaryOpcode::normcdf, iArg2, iArg3);
|
2010-03-11 09:43:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddNormpdf(expr_t iArg1, expr_t iArg2, expr_t iArg3)
|
2010-03-11 09:43:16 +01:00
|
|
|
|
{
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddTrinaryOp(iArg1, TrinaryOpcode::normpdf, iArg2, iArg3);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddSteadyState(expr_t iArg1)
|
2009-09-10 22:09:16 +02:00
|
|
|
|
{
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddUnaryOp(UnaryOpcode::steadyState, iArg1);
|
2009-09-10 22:09:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-01-13 18:08:26 +01:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddSteadyStateParamDeriv(expr_t iArg1, int param_symb_id)
|
|
|
|
|
{
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddUnaryOp(UnaryOpcode::steadyStateParamDeriv, iArg1, 0, param_symb_id);
|
2011-01-13 18:08:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddSteadyStateParam2ndDeriv(expr_t iArg1, int param1_symb_id, int param2_symb_id)
|
|
|
|
|
{
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddUnaryOp(UnaryOpcode::steadyStateParam2ndDeriv, iArg1, 0, param1_symb_id, param2_symb_id);
|
2011-01-13 18:08:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddExpectation(int iArg1, expr_t iArg2)
|
2009-10-30 06:21:54 +01:00
|
|
|
|
{
|
2018-07-18 16:18:26 +02:00
|
|
|
|
return AddUnaryOp(UnaryOpcode::expectation, iArg2, iArg1);
|
2009-10-30 06:21:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 17:08:06 +01:00
|
|
|
|
expr_t
|
2018-08-01 19:41:44 +02:00
|
|
|
|
DataTree::AddVarExpectation(const string &model_name)
|
2016-11-03 17:08:06 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (auto it = var_expectation_node_map.find(model_name);
|
|
|
|
|
it != var_expectation_node_map.end())
|
2016-11-18 16:52:13 +01:00
|
|
|
|
return it->second;
|
|
|
|
|
|
2018-09-05 16:41:33 +02:00
|
|
|
|
auto sp = make_unique<VarExpectationNode>(*this, node_list.size(), model_name);
|
|
|
|
|
auto p = sp.get();
|
|
|
|
|
node_list.push_back(move(sp));
|
|
|
|
|
var_expectation_node_map[model_name] = p;
|
|
|
|
|
return p;
|
2016-11-03 17:08:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-30 16:33:16 +01:00
|
|
|
|
expr_t
|
2018-03-28 18:46:15 +02:00
|
|
|
|
DataTree::AddPacExpectation(const string &model_name)
|
2018-01-30 16:33:16 +01:00
|
|
|
|
{
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (auto it = pac_expectation_node_map.find(model_name);
|
|
|
|
|
it != pac_expectation_node_map.end())
|
2018-01-30 16:33:16 +01:00
|
|
|
|
return it->second;
|
2018-02-07 16:52:11 +01:00
|
|
|
|
|
2018-09-05 16:41:33 +02:00
|
|
|
|
auto sp = make_unique<PacExpectationNode>(*this, node_list.size(), model_name);
|
|
|
|
|
auto p = sp.get();
|
|
|
|
|
node_list.push_back(move(sp));
|
|
|
|
|
pac_expectation_node_map[model_name] = p;
|
|
|
|
|
return p;
|
2018-01-30 16:33:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-02 19:10:59 +02:00
|
|
|
|
BinaryOpNode *
|
2010-09-16 19:18:45 +02:00
|
|
|
|
DataTree::AddEqual(expr_t iArg1, expr_t iArg2)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2020-04-02 19:10:59 +02:00
|
|
|
|
/* We know that we can safely cast to BinaryOpNode because
|
|
|
|
|
BinaryOpCode::equal can never be reduced to a constant. */
|
|
|
|
|
return dynamic_cast<BinaryOpNode *>(AddBinaryOp(iArg1, BinaryOpcode::equal, iArg2));
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2018-06-04 12:50:53 +02:00
|
|
|
|
DataTree::AddLocalVariable(int symb_id, expr_t value) noexcept(false)
|
2008-02-03 11:28:36 +01:00
|
|
|
|
{
|
2018-07-17 18:34:07 +02:00
|
|
|
|
assert(symbol_table.getType(symb_id) == SymbolType::modelLocalVariable);
|
2009-04-16 12:33:30 +02:00
|
|
|
|
|
2008-02-03 11:28:36 +01:00
|
|
|
|
// Throw an exception if symbol already declared
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (auto it = local_variables_table.find(symb_id);
|
|
|
|
|
it != local_variables_table.end())
|
2010-03-16 12:16:07 +01:00
|
|
|
|
throw LocalVariableException(symbol_table.getName(symb_id));
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
2010-03-16 12:16:07 +01:00
|
|
|
|
local_variables_table[symb_id] = value;
|
2017-08-28 15:14:11 +02:00
|
|
|
|
local_variables_vector.push_back(symb_id);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
|
|
|
|
DataTree::AddExternalFunction(int symb_id, const vector<expr_t> &arguments)
|
2010-02-22 17:33:38 +01:00
|
|
|
|
{
|
2018-07-17 18:34:07 +02:00
|
|
|
|
assert(symbol_table.getType(symb_id) == SymbolType::externalFunction);
|
2010-03-03 11:43:22 +01:00
|
|
|
|
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (auto it = external_function_node_map.find({ arguments, symb_id });
|
|
|
|
|
it != external_function_node_map.end())
|
2010-03-03 11:43:22 +01:00
|
|
|
|
return it->second;
|
|
|
|
|
|
2018-09-05 16:41:33 +02:00
|
|
|
|
auto sp = make_unique<ExternalFunctionNode>(*this, node_list.size(), symb_id, arguments);
|
|
|
|
|
auto p = sp.get();
|
|
|
|
|
node_list.push_back(move(sp));
|
|
|
|
|
external_function_node_map[{ arguments, symb_id }] = p;
|
|
|
|
|
return p;
|
2010-02-22 17:33:38 +01:00
|
|
|
|
}
|
2009-04-16 12:33:30 +02:00
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
2014-03-11 17:38:47 +01:00
|
|
|
|
DataTree::AddFirstDerivExternalFunction(int top_level_symb_id, const vector<expr_t> &arguments, int input_index)
|
2010-02-22 17:33:38 +01:00
|
|
|
|
{
|
2018-07-17 18:34:07 +02:00
|
|
|
|
assert(symbol_table.getType(top_level_symb_id) == SymbolType::externalFunction);
|
2010-03-03 11:43:22 +01:00
|
|
|
|
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (auto it = first_deriv_external_function_node_map.find({ arguments, input_index, top_level_symb_id });
|
|
|
|
|
it != first_deriv_external_function_node_map.end())
|
2010-03-03 11:43:22 +01:00
|
|
|
|
return it->second;
|
|
|
|
|
|
2018-09-05 16:41:33 +02:00
|
|
|
|
auto sp = make_unique<FirstDerivExternalFunctionNode>(*this, node_list.size(), top_level_symb_id, arguments, input_index);
|
|
|
|
|
auto p = sp.get();
|
|
|
|
|
node_list.push_back(move(sp));
|
|
|
|
|
first_deriv_external_function_node_map[{ arguments, input_index, top_level_symb_id }] = p;
|
|
|
|
|
return p;
|
2010-02-22 17:33:38 +01:00
|
|
|
|
}
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
|
expr_t
|
2014-03-11 17:38:47 +01:00
|
|
|
|
DataTree::AddSecondDerivExternalFunction(int top_level_symb_id, const vector<expr_t> &arguments, int input_index1, int input_index2)
|
2010-02-22 17:33:38 +01:00
|
|
|
|
{
|
2018-07-17 18:34:07 +02:00
|
|
|
|
assert(symbol_table.getType(top_level_symb_id) == SymbolType::externalFunction);
|
2010-03-03 11:43:22 +01:00
|
|
|
|
|
2019-12-16 19:42:59 +01:00
|
|
|
|
if (auto it = second_deriv_external_function_node_map.find({ arguments, input_index1, input_index2,
|
|
|
|
|
top_level_symb_id });
|
|
|
|
|
it != second_deriv_external_function_node_map.end())
|
2010-03-03 11:43:22 +01:00
|
|
|
|
return it->second;
|
|
|
|
|
|
2018-09-05 16:41:33 +02:00
|
|
|
|
auto sp = make_unique<SecondDerivExternalFunctionNode>(*this, node_list.size(), top_level_symb_id, arguments, input_index1, input_index2);
|
|
|
|
|
auto p = sp.get();
|
|
|
|
|
node_list.push_back(move(sp));
|
|
|
|
|
second_deriv_external_function_node_map[{ arguments, input_index1, input_index2, top_level_symb_id }] = p;
|
|
|
|
|
return p;
|
2008-02-03 11:28:36 +01:00
|
|
|
|
}
|
2009-02-27 13:19:25 +01:00
|
|
|
|
|
2009-03-11 12:43:18 +01:00
|
|
|
|
bool
|
|
|
|
|
DataTree::isSymbolUsed(int symb_id) const
|
|
|
|
|
{
|
2019-12-20 16:59:30 +01:00
|
|
|
|
for (const auto &it : variable_node_map)
|
2018-06-04 12:26:16 +02:00
|
|
|
|
if (it.first.first == symb_id)
|
2009-03-11 12:43:18 +01:00
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (local_variables_table.find(symb_id) != local_variables_table.end())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-17 18:26:23 +02:00
|
|
|
|
int
|
2018-06-04 12:50:53 +02:00
|
|
|
|
DataTree::getDerivID(int symb_id, int lag) const noexcept(false)
|
2009-04-17 18:26:23 +02:00
|
|
|
|
{
|
|
|
|
|
throw UnknownDerivIDException();
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-13 18:08:26 +01:00
|
|
|
|
SymbolType
|
2018-06-04 12:50:53 +02:00
|
|
|
|
DataTree::getTypeByDerivID(int deriv_id) const noexcept(false)
|
2011-01-13 18:08:26 +01:00
|
|
|
|
{
|
|
|
|
|
throw UnknownDerivIDException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2018-06-04 12:50:53 +02:00
|
|
|
|
DataTree::getLagByDerivID(int deriv_id) const noexcept(false)
|
2011-01-13 18:08:26 +01:00
|
|
|
|
{
|
|
|
|
|
throw UnknownDerivIDException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2018-06-04 12:50:53 +02:00
|
|
|
|
DataTree::getSymbIDByDerivID(int deriv_id) const noexcept(false)
|
2011-01-13 18:08:26 +01:00
|
|
|
|
{
|
|
|
|
|
throw UnknownDerivIDException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
DataTree::addAllParamDerivId(set<int> &deriv_id_set)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-17 18:26:23 +02:00
|
|
|
|
int
|
2018-06-04 12:50:53 +02:00
|
|
|
|
DataTree::getDynJacobianCol(int deriv_id) const noexcept(false)
|
2009-04-17 18:26:23 +02:00
|
|
|
|
{
|
|
|
|
|
throw UnknownDerivIDException();
|
|
|
|
|
}
|
2009-11-09 16:13:47 +01:00
|
|
|
|
|
|
|
|
|
bool
|
2010-02-23 16:45:56 +01:00
|
|
|
|
DataTree::isUnaryOpUsed(UnaryOpcode opcode) const
|
2009-11-09 16:13:47 +01:00
|
|
|
|
{
|
2019-12-20 16:59:30 +01:00
|
|
|
|
for (const auto &it : unary_op_node_map)
|
2018-06-04 17:35:02 +02:00
|
|
|
|
if (get<1>(it.first) == opcode)
|
2010-02-23 16:45:56 +01:00
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-10 12:37:11 +02:00
|
|
|
|
bool
|
|
|
|
|
DataTree::isUnaryOpUsedOnType(SymbolType type, UnaryOpcode opcode) const
|
|
|
|
|
{
|
|
|
|
|
set<int> var;
|
2019-12-20 16:59:30 +01:00
|
|
|
|
for (const auto &it : unary_op_node_map)
|
2019-09-10 12:37:11 +02:00
|
|
|
|
if (get<1>(it.first) == opcode)
|
|
|
|
|
{
|
|
|
|
|
it.second->collectVariables(type, var);
|
|
|
|
|
if (!var.empty())
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-23 16:45:56 +01:00
|
|
|
|
bool
|
|
|
|
|
DataTree::isBinaryOpUsed(BinaryOpcode opcode) const
|
|
|
|
|
{
|
2019-12-20 16:59:30 +01:00
|
|
|
|
for (const auto &it : binary_op_node_map)
|
2018-06-04 17:35:02 +02:00
|
|
|
|
if (get<2>(it.first) == opcode)
|
2010-02-23 16:45:56 +01:00
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-10 12:37:11 +02:00
|
|
|
|
bool
|
|
|
|
|
DataTree::isBinaryOpUsedOnType(SymbolType type, BinaryOpcode opcode) const
|
|
|
|
|
{
|
|
|
|
|
set<int> var;
|
2019-12-20 16:59:30 +01:00
|
|
|
|
for (const auto &it : binary_op_node_map)
|
2019-09-10 12:37:11 +02:00
|
|
|
|
if (get<2>(it.first) == opcode)
|
|
|
|
|
{
|
|
|
|
|
it.second->collectVariables(type, var);
|
|
|
|
|
if (!var.empty())
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-20 14:47:03 +02:00
|
|
|
|
int
|
|
|
|
|
DataTree::minLagForSymbol(int symb_id) const
|
|
|
|
|
{
|
|
|
|
|
int r = 0;
|
2019-12-20 16:59:30 +01:00
|
|
|
|
for (const auto &it : variable_node_map)
|
2018-06-04 12:26:16 +02:00
|
|
|
|
if (it.first.first == symb_id && it.first.second < r)
|
|
|
|
|
r = it.first.second;
|
2010-10-20 14:47:03 +02:00
|
|
|
|
return r;
|
|
|
|
|
}
|
2010-12-13 14:07:05 +01:00
|
|
|
|
|
|
|
|
|
void
|
2016-10-14 14:30:14 +02:00
|
|
|
|
DataTree::writePowerDeriv(ostream &output) const
|
2010-12-13 14:07:05 +01:00
|
|
|
|
{
|
2018-07-18 16:18:26 +02:00
|
|
|
|
if (isBinaryOpUsed(BinaryOpcode::powerDeriv))
|
2010-12-13 14:07:05 +01:00
|
|
|
|
output << "/*" << endl
|
|
|
|
|
<< " * The k-th derivative of x^p" << endl
|
|
|
|
|
<< " */" << endl
|
|
|
|
|
<< "double getPowerDeriv(double x, double p, int k)" << endl
|
|
|
|
|
<< "{" << endl
|
2020-06-23 16:00:40 +02:00
|
|
|
|
<< " if (fabs(x) < " << near_zero << " && p > 0 && k > p && fabs(p-nearbyint(p)) < " << near_zero << ')' << endl
|
2010-12-13 14:07:05 +01:00
|
|
|
|
<< " return 0.0;" << endl
|
|
|
|
|
<< " else" << endl
|
|
|
|
|
<< " {" << endl
|
|
|
|
|
<< " int i = 0;" << endl
|
|
|
|
|
<< " double dxp = pow(x, p-k);" << endl
|
|
|
|
|
<< " for (; i<k; i++)" << endl
|
|
|
|
|
<< " dxp *= p--;" << endl
|
|
|
|
|
<< " return dxp;" << endl
|
|
|
|
|
<< " }" << endl
|
|
|
|
|
<< "}" << endl;
|
|
|
|
|
}
|
2016-10-14 16:11:44 +02:00
|
|
|
|
|
2021-04-26 12:52:18 +02:00
|
|
|
|
void
|
|
|
|
|
DataTree::writePowerDerivJulia(ostream &output) const
|
|
|
|
|
{
|
|
|
|
|
if (isBinaryOpUsed(BinaryOpcode::powerDeriv))
|
|
|
|
|
output << "nearbyint(x::Float64) = (abs((x)-floor(x)) < abs((x)-ceil(x)) ? floor(x) : ceil(x))" << endl
|
|
|
|
|
<< endl
|
|
|
|
|
<< "function get_power_deriv(x::Float64, p::Float64, k::Int64)" << endl
|
|
|
|
|
<< " if (abs(x) < 1e-12 && p > 0 && k > p && abs(p-nearbyint(p)) < 1e-12 )" << endl
|
|
|
|
|
<< " return 0.0" << endl
|
|
|
|
|
<< " else" << endl
|
|
|
|
|
<< " dxp = x^(p-k)" << endl
|
|
|
|
|
<< " for i = 1:k" << endl
|
|
|
|
|
<< " dxp *= p" << endl
|
|
|
|
|
<< " p -= 1" << endl
|
|
|
|
|
<< " end" << endl
|
|
|
|
|
<< " return dxp" << endl
|
|
|
|
|
<< " end" << endl
|
|
|
|
|
<< "end" << endl;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 15:13:04 +02:00
|
|
|
|
void
|
|
|
|
|
DataTree::writePowerDerivHeader(ostream &output) const
|
|
|
|
|
{
|
|
|
|
|
if (isBinaryOpUsed(BinaryOpcode::powerDeriv))
|
|
|
|
|
output << "double getPowerDeriv(double x, double p, int k);" << endl;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-27 15:01:31 +02:00
|
|
|
|
string
|
|
|
|
|
DataTree::packageDir(const string &package)
|
|
|
|
|
{
|
2019-04-03 16:32:52 +02:00
|
|
|
|
regex pat{R"(\.)"};
|
2018-06-27 15:01:31 +02:00
|
|
|
|
string dirname = "+" + regex_replace(package, pat, "/+");
|
2019-04-04 17:01:37 +02:00
|
|
|
|
filesystem::create_directories(dirname);
|
2018-06-27 15:01:31 +02:00
|
|
|
|
return dirname;
|
|
|
|
|
}
|
2021-04-22 18:03:47 +02:00
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
DataTree::writeToFileIfModified(stringstream &new_contents, const string &filename)
|
|
|
|
|
{
|
|
|
|
|
ifstream old_file{filename, ios::in | ios::binary};
|
|
|
|
|
if (old_file.is_open()
|
|
|
|
|
&& equal(istreambuf_iterator<char>{old_file}, istreambuf_iterator<char>{},
|
|
|
|
|
istreambuf_iterator<char>{new_contents}, istreambuf_iterator<char>{}))
|
|
|
|
|
return;
|
|
|
|
|
old_file.close();
|
|
|
|
|
|
2021-04-26 14:10:04 +02:00
|
|
|
|
new_contents.seekg(0);
|
|
|
|
|
|
2021-04-22 18:03:47 +02:00
|
|
|
|
ofstream new_file{filename, ios::out | ios::binary};
|
|
|
|
|
if (!new_file.is_open())
|
|
|
|
|
{
|
|
|
|
|
cerr << "Error: Can't open file " << filename << " for writing" << endl;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
copy(istreambuf_iterator<char>{new_contents}, istreambuf_iterator<char>{},
|
|
|
|
|
ostreambuf_iterator<char>{new_file});
|
|
|
|
|
new_file.close();
|
|
|
|
|
}
|