2008-02-03 11:28:36 +01:00
|
|
|
/*
|
2018-01-30 16:33:16 +01:00
|
|
|
* Copyright (C) 2003-2018 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/>.
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
|
|
|
|
#include <boost/filesystem.hpp>
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
|
|
#include "DataTree.hh"
|
|
|
|
|
2010-02-22 17:33:38 +01:00
|
|
|
DataTree::DataTree(SymbolTable &symbol_table_arg,
|
|
|
|
NumericalConstants &num_constants_arg,
|
|
|
|
ExternalFunctionsTable &external_functions_table_arg) :
|
2008-02-03 11:28:36 +01:00
|
|
|
symbol_table(symbol_table_arg),
|
|
|
|
num_constants(num_constants_arg),
|
2010-02-22 17:33:38 +01:00
|
|
|
external_functions_table(external_functions_table_arg),
|
2009-04-17 18:26:23 +02:00
|
|
|
node_counter(0)
|
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");
|
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
|
|
|
}
|
|
|
|
|
|
|
|
DataTree::~DataTree()
|
|
|
|
{
|
2018-06-04 12:26:16 +02:00
|
|
|
for (auto & it : node_list)
|
|
|
|
delete it;
|
2008-02-03 11:28:36 +01:00
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t
|
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
|
|
|
|
2018-06-04 15:03:26 +02:00
|
|
|
auto it = num_const_node_map.find(id);
|
2008-02-03 11:28:36 +01:00
|
|
|
if (it != num_const_node_map.end())
|
|
|
|
return it->second;
|
|
|
|
else
|
|
|
|
return new NumConstNode(*this, id);
|
|
|
|
}
|
|
|
|
|
2009-09-30 17:10:31 +02:00
|
|
|
VariableNode *
|
|
|
|
DataTree::AddVariableInternal(int symb_id, int lag)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
2018-06-04 16:36:46 +02:00
|
|
|
auto it = variable_node_map.find({ symb_id, lag });
|
2008-02-03 11:28:36 +01:00
|
|
|
if (it != variable_node_map.end())
|
|
|
|
return it->second;
|
|
|
|
else
|
2009-09-30 17:10:31 +02:00
|
|
|
return new VariableNode(*this, symb_id, lag);
|
2008-02-03 11:28:36 +01:00
|
|
|
}
|
2009-09-30 17:10:31 +02:00
|
|
|
|
2015-07-30 14:40:03 +02:00
|
|
|
bool
|
|
|
|
DataTree::ParamUsedWithLeadLagInternal() const
|
|
|
|
{
|
2018-06-04 12:26:16 +02: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;
|
|
|
|
}
|
|
|
|
|
2009-09-30 17:10:31 +02:00
|
|
|
VariableNode *
|
|
|
|
DataTree::AddVariable(int symb_id, int lag)
|
2009-04-16 12:33:30 +02:00
|
|
|
{
|
2009-04-28 18:21:39 +02:00
|
|
|
assert(lag == 0);
|
2009-09-30 17:10:31 +02:00
|
|
|
return AddVariableInternal(symb_id, lag);
|
2009-04-16 12:33:30 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (iArg1 != Zero && iArg2 != Zero)
|
|
|
|
{
|
|
|
|
// Simplify x+(-y) in x-y
|
2018-06-04 15:03:26 +02:00
|
|
|
auto *uarg2 = dynamic_cast<UnaryOpNode *>(iArg2);
|
2018-07-18 16:18:26 +02:00
|
|
|
if (uarg2 != nullptr && uarg2->get_op_code() == UnaryOpcode::uminus)
|
2009-04-14 16:39:53 +02:00
|
|
|
return AddMinus(iArg1, uarg2->get_arg());
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
|
|
// To treat commutativity of "+"
|
|
|
|
// Nodes iArg1 and iArg2 are sorted by index
|
|
|
|
if (iArg1->idx > iArg2->idx)
|
|
|
|
{
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t tmp = iArg1;
|
2008-02-03 11:28:36 +01:00
|
|
|
iArg1 = iArg2;
|
|
|
|
iArg2 = tmp;
|
|
|
|
}
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddBinaryOp(iArg1, BinaryOpcode::plus, iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
}
|
|
|
|
else if (iArg1 != Zero)
|
|
|
|
return iArg1;
|
|
|
|
else if (iArg2 != Zero)
|
|
|
|
return iArg2;
|
|
|
|
else
|
|
|
|
return Zero;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
{
|
|
|
|
if (iArg1 != Zero)
|
|
|
|
{
|
|
|
|
// Simplify -(-x) in x
|
2018-06-04 15:03:26 +02:00
|
|
|
auto *uarg = dynamic_cast<UnaryOpNode *>(iArg1);
|
2018-07-18 16:18:26 +02:00
|
|
|
if (uarg != nullptr && uarg->get_op_code() == UnaryOpcode::uminus)
|
2009-04-14 16:39:53 +02:00
|
|
|
return uarg->get_arg();
|
2008-02-03 11:28:36 +01:00
|
|
|
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddUnaryOp(UnaryOpcode::uminus, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return Zero;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (iArg1 == MinusOne)
|
|
|
|
return AddUMinus(iArg2);
|
|
|
|
else if (iArg2 == MinusOne)
|
|
|
|
return AddUMinus(iArg1);
|
|
|
|
else if (iArg1 != Zero && iArg1 != One && iArg2 != Zero && iArg2 != One)
|
|
|
|
{
|
|
|
|
// To treat commutativity of "*"
|
|
|
|
// Nodes iArg1 and iArg2 are sorted by index
|
|
|
|
if (iArg1->idx > iArg2->idx)
|
|
|
|
{
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t tmp = iArg1;
|
2008-02-03 11:28:36 +01:00
|
|
|
iArg1 = iArg2;
|
|
|
|
iArg2 = tmp;
|
|
|
|
}
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddBinaryOp(iArg1, BinaryOpcode::times, iArg2);
|
2008-02-03 11:28:36 +01:00
|
|
|
}
|
|
|
|
else if (iArg1 != Zero && iArg1 != One && iArg2 == One)
|
|
|
|
return iArg1;
|
|
|
|
else if (iArg2 != Zero && iArg2 != One && iArg1 == One)
|
|
|
|
return iArg2;
|
|
|
|
else if (iArg2 == One && iArg1 == One)
|
|
|
|
return One;
|
|
|
|
else
|
|
|
|
return Zero;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
{
|
2010-10-12 16:17:00 +02:00
|
|
|
if (iArg1 != Zero && iArg2 != Zero && iArg1 != One && iArg2 != One)
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddBinaryOp(iArg1, BinaryOpcode::power, iArg2);
|
2010-10-12 16:17:00 +02:00
|
|
|
else if (iArg1 == One)
|
|
|
|
return One;
|
2008-02-03 11:28:36 +01:00
|
|
|
else if (iArg2 == One)
|
|
|
|
return iArg1;
|
|
|
|
else if (iArg2 == Zero)
|
|
|
|
return One;
|
|
|
|
else
|
|
|
|
return Zero;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
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
|
|
|
{
|
|
|
|
if (iArg1 != Zero)
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddUnaryOp(UnaryOpcode::exp, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
else
|
|
|
|
return One;
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t
|
|
|
|
DataTree::AddLog(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
if (iArg1 != Zero && iArg1 != One)
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddUnaryOp(UnaryOpcode::log, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
else if (iArg1 == One)
|
|
|
|
return Zero;
|
|
|
|
else
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t
|
|
|
|
DataTree::AddLog10(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
if (iArg1 != Zero && iArg1 != One)
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddUnaryOp(UnaryOpcode::log10, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
else if (iArg1 == One)
|
|
|
|
return Zero;
|
|
|
|
else
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t
|
|
|
|
DataTree::AddCos(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
if (iArg1 != Zero)
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddUnaryOp(UnaryOpcode::cos, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
else
|
|
|
|
return One;
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t
|
|
|
|
DataTree::AddSin(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
if (iArg1 != Zero)
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddUnaryOp(UnaryOpcode::sin, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
else
|
|
|
|
return Zero;
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t
|
|
|
|
DataTree::AddTan(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
if (iArg1 != Zero)
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddUnaryOp(UnaryOpcode::tan, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
else
|
|
|
|
return Zero;
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t
|
|
|
|
DataTree::AddAcos(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
if (iArg1 != One)
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddUnaryOp(UnaryOpcode::acos, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
else
|
|
|
|
return Zero;
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t
|
|
|
|
DataTree::AddAsin(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
if (iArg1 != Zero)
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddUnaryOp(UnaryOpcode::asin, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
else
|
|
|
|
return Zero;
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t
|
|
|
|
DataTree::AddAtan(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
if (iArg1 != Zero)
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddUnaryOp(UnaryOpcode::atan, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
else
|
|
|
|
return Zero;
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t
|
|
|
|
DataTree::AddCosh(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
if (iArg1 != Zero)
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddUnaryOp(UnaryOpcode::cosh, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
else
|
|
|
|
return One;
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t
|
|
|
|
DataTree::AddSinh(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
if (iArg1 != Zero)
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddUnaryOp(UnaryOpcode::sinh, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
else
|
|
|
|
return Zero;
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t
|
|
|
|
DataTree::AddTanh(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
if (iArg1 != Zero)
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddUnaryOp(UnaryOpcode::tanh, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
else
|
|
|
|
return Zero;
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t
|
|
|
|
DataTree::AddAcosh(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
if (iArg1 != One)
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddUnaryOp(UnaryOpcode::acosh, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
else
|
|
|
|
return Zero;
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t
|
|
|
|
DataTree::AddAsinh(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
if (iArg1 != Zero)
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddUnaryOp(UnaryOpcode::asinh, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
else
|
|
|
|
return Zero;
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t
|
|
|
|
DataTree::AddAtanh(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
if (iArg1 != Zero)
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddUnaryOp(UnaryOpcode::atanh, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
else
|
|
|
|
return Zero;
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t
|
|
|
|
DataTree::AddSqrt(expr_t iArg1)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
if (iArg1 != Zero)
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddUnaryOp(UnaryOpcode::sqrt, iArg1);
|
2008-02-03 11:28:36 +01:00
|
|
|
else
|
|
|
|
return Zero;
|
|
|
|
}
|
|
|
|
|
2011-08-12 13:20:53 +02:00
|
|
|
expr_t
|
|
|
|
DataTree::AddAbs(expr_t iArg1)
|
|
|
|
{
|
|
|
|
if (iArg1 == Zero)
|
|
|
|
return Zero;
|
|
|
|
if (iArg1 == One)
|
|
|
|
return One;
|
|
|
|
else
|
2018-07-18 16:18:26 +02: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;
|
|
|
|
if (iArg1 == One)
|
|
|
|
return One;
|
|
|
|
else
|
2018-07-18 16:18:26 +02: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
|
|
|
{
|
|
|
|
if (iArg1 != Zero)
|
2018-07-18 16:18:26 +02:00
|
|
|
return AddUnaryOp(UnaryOpcode::erf, iArg1);
|
2010-03-11 11:57:34 +01:00
|
|
|
else
|
|
|
|
return Zero;
|
|
|
|
}
|
|
|
|
|
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
|
2016-11-18 16:52:13 +01:00
|
|
|
DataTree::AddVarExpectation(const int symb_id, const int forecast_horizon, const string &model_name)
|
2016-11-03 17:08:06 +01:00
|
|
|
{
|
2018-07-17 18:34:07 +02:00
|
|
|
assert(symbol_table.getType(symb_id) == SymbolType::endogenous);
|
2016-11-18 16:52:13 +01:00
|
|
|
|
2018-06-04 17:35:02 +02:00
|
|
|
auto it = var_expectation_node_map.find({ model_name, symb_id, forecast_horizon });
|
2016-11-18 16:52:13 +01:00
|
|
|
if (it != var_expectation_node_map.end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
return new VarExpectationNode(*this, symb_id, forecast_horizon, model_name);
|
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
|
|
|
{
|
2018-06-04 15:03:26 +02:00
|
|
|
auto it = pac_expectation_node_map.find(model_name);
|
2018-01-30 16:33:16 +01:00
|
|
|
if (it != pac_expectation_node_map.end())
|
|
|
|
return it->second;
|
2018-02-07 16:52:11 +01:00
|
|
|
|
2018-03-28 18:46:15 +02:00
|
|
|
return new PacExpectationNode(*this, model_name);
|
2018-01-30 16:33:16 +01:00
|
|
|
}
|
|
|
|
|
2010-09-16 19:18:45 +02:00
|
|
|
expr_t
|
|
|
|
DataTree::AddEqual(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::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
|
2018-06-04 15:03:26 +02:00
|
|
|
auto it = local_variables_table.find(symb_id);
|
2008-02-03 11:28:36 +01:00
|
|
|
if (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
|
|
|
|
2018-06-04 16:36:46 +02:00
|
|
|
auto it = external_function_node_map.find({ arguments, symb_id });
|
2010-03-03 11:43:22 +01:00
|
|
|
if (it != external_function_node_map.end())
|
|
|
|
return it->second;
|
|
|
|
|
2010-02-22 17:33:38 +01:00
|
|
|
return new ExternalFunctionNode(*this, symb_id, arguments);
|
|
|
|
}
|
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
|
|
|
|
2018-06-04 15:03:26 +02:00
|
|
|
auto it
|
2018-06-04 17:35:02 +02:00
|
|
|
= first_deriv_external_function_node_map.find({ arguments, input_index, top_level_symb_id });
|
2010-03-03 11:43:22 +01:00
|
|
|
if (it != first_deriv_external_function_node_map.end())
|
|
|
|
return it->second;
|
|
|
|
|
2010-02-22 17:33:38 +01:00
|
|
|
return new FirstDerivExternalFunctionNode(*this, top_level_symb_id, arguments, input_index);
|
|
|
|
}
|
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
|
|
|
|
2018-06-04 15:03:26 +02:00
|
|
|
auto it
|
2018-06-04 17:35:02 +02:00
|
|
|
= second_deriv_external_function_node_map.find({ arguments, input_index1, input_index2,
|
2018-06-04 16:36:46 +02:00
|
|
|
top_level_symb_id });
|
2010-03-03 11:43:22 +01:00
|
|
|
if (it != second_deriv_external_function_node_map.end())
|
|
|
|
return it->second;
|
|
|
|
|
2010-02-22 17:33:38 +01:00
|
|
|
return new SecondDerivExternalFunctionNode(*this, top_level_symb_id, arguments, input_index1, input_index2);
|
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
|
|
|
|
{
|
2018-06-04 12:26:16 +02:00
|
|
|
for (const auto & it : variable_node_map)
|
|
|
|
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
|
|
|
{
|
2018-06-04 12:26:16 +02: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DataTree::isBinaryOpUsed(BinaryOpcode opcode) const
|
|
|
|
{
|
2018-06-04 12:26:16 +02: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DataTree::isTrinaryOpUsed(TrinaryOpcode opcode) const
|
|
|
|
{
|
2018-06-04 12:26:16 +02:00
|
|
|
for (const auto & it : trinary_op_node_map)
|
2018-06-04 17:35:02 +02:00
|
|
|
if (get<3>(it.first) == opcode)
|
2009-11-09 16:13:47 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2010-03-03 11:43:22 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
DataTree::isExternalFunctionUsed(int symb_id) const
|
|
|
|
{
|
2018-06-04 12:26:16 +02:00
|
|
|
for (const auto & it : external_function_node_map)
|
|
|
|
if (it.first.second == symb_id)
|
2010-03-03 11:43:22 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DataTree::isFirstDerivExternalFunctionUsed(int symb_id) const
|
|
|
|
{
|
2018-06-04 12:26:16 +02:00
|
|
|
for (const auto & it : first_deriv_external_function_node_map)
|
2018-06-04 17:35:02 +02:00
|
|
|
if (get<2>(it.first) == symb_id)
|
2010-03-03 11:43:22 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DataTree::isSecondDerivExternalFunctionUsed(int symb_id) const
|
|
|
|
{
|
2018-06-04 12:26:16 +02:00
|
|
|
for (const auto & it : second_deriv_external_function_node_map)
|
2018-06-04 17:35:02 +02:00
|
|
|
if (get<3>(it.first) == symb_id)
|
2010-03-03 11:43:22 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-20 14:47:03 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
DataTree::minLagForSymbol(int symb_id) const
|
|
|
|
{
|
|
|
|
int r = 0;
|
2018-06-04 12:26:16 +02:00
|
|
|
for (const auto & it : variable_node_map)
|
|
|
|
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
|
|
|
|
DataTree::writePowerDerivCHeader(ostream &output) const
|
|
|
|
{
|
2018-07-18 16:18:26 +02:00
|
|
|
if (isBinaryOpUsed(BinaryOpcode::powerDeriv))
|
2010-12-13 14:07:05 +01:00
|
|
|
output << "double getPowerDeriv(double, double, int);" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2010-12-22 13:50:13 +01:00
|
|
|
<< "#ifdef _MSC_VER" << endl
|
|
|
|
<< "# define nearbyint(x) (fabs((x)-floor(x)) < fabs((x)-ceil(x)) ? floor(x) : ceil(x))" << endl
|
|
|
|
<< "#endif" << endl
|
2018-07-17 16:32:00 +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
|
|
|
|
|
|
|
void
|
|
|
|
DataTree::writeNormcdfCHeader(ostream &output) const
|
|
|
|
{
|
|
|
|
#if defined(_WIN32) || defined(__CYGWIN32__) || defined(__MINGW32__)
|
2018-07-18 16:18:26 +02:00
|
|
|
if (isTrinaryOpUsed(TrinaryOpcode::normcdf))
|
2016-10-14 16:11:44 +02:00
|
|
|
output << "#ifdef _MSC_VER" << endl
|
|
|
|
<< "double normcdf(double);" << endl
|
|
|
|
<< "#endif" << endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataTree::writeNormcdf(ostream &output) const
|
|
|
|
{
|
|
|
|
#if defined(_WIN32) || defined(__CYGWIN32__) || defined(__MINGW32__)
|
2018-07-18 16:18:26 +02:00
|
|
|
if (isTrinaryOpUsed(TrinaryOpcode::normcdf))
|
2016-10-14 16:11:44 +02:00
|
|
|
output << endl
|
|
|
|
<< "#ifdef _MSC_VER" << endl
|
|
|
|
<< "/*" << endl
|
|
|
|
<< " * Define normcdf for MSVC compiler" << endl
|
|
|
|
<< " */" << endl
|
|
|
|
<< "double normcdf(double x)" << endl
|
|
|
|
<< "{" << endl
|
|
|
|
<< "#if _MSC_VER >= 1700" << endl
|
|
|
|
<< " return 0.5 * erfc(-x * M_SQRT1_2);" << endl
|
|
|
|
<< "#else" << endl
|
|
|
|
<< " // From http://www.johndcook.com/blog/cpp_phi" << endl
|
|
|
|
<< " double a1 = 0.254829592;" << endl
|
|
|
|
<< " double a2 = -0.284496736;" << endl
|
|
|
|
<< " double a3 = 1.421413741;" << endl
|
|
|
|
<< " double a4 = -1.453152027;" << endl
|
|
|
|
<< " double a5 = 1.061405429;" << endl
|
|
|
|
<< " double p = 0.3275911;" << endl
|
|
|
|
<< " int sign = (x < 0) ? -1 : 1;" << endl
|
|
|
|
<< " x = fabs(x)/sqrt(2.0);" << endl
|
|
|
|
<< " // From the Handbook of Mathematical Functions by Abramowitz and Stegun, formula 7.1.26" << endl
|
|
|
|
<< " double t = 1.0/(1.0 + p*x);" << endl
|
|
|
|
<< " double y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*exp(-x*x);" << endl
|
|
|
|
<< " return 0.5*(1.0 + sign*y);" << endl
|
|
|
|
<< "#endif" << endl
|
|
|
|
<< "}" << endl
|
|
|
|
<< "#endif" << endl;
|
|
|
|
#endif
|
|
|
|
}
|
2018-06-27 15:01:31 +02:00
|
|
|
|
|
|
|
string
|
|
|
|
DataTree::packageDir(const string &package)
|
|
|
|
{
|
|
|
|
regex pat{"\\."};
|
|
|
|
string dirname = "+" + regex_replace(package, pat, "/+");
|
|
|
|
boost::filesystem::create_directories(dirname);
|
|
|
|
return dirname;
|
|
|
|
}
|