DataTree.cc 21 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2018 Dynare Team
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * 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/>.
 */

20
#include <cstdlib>
21
#include <cassert>
22
#include <iostream>
23
24
25
#include <regex>

#include <boost/filesystem.hpp>
26
27
28

#include "DataTree.hh"

29
30
void
DataTree::initConstants()
31
{
32
33
34
  Zero = AddNonNegativeConstant("0");
  One = AddNonNegativeConstant("1");
  Two = AddNonNegativeConstant("2");
35
36

  MinusOne = AddUMinus(One);
37

38
39
  NaN = AddNonNegativeConstant("NaN");
  Infinity = AddNonNegativeConstant("Inf");
40
  MinusInfinity = AddUMinus(Infinity);
sebastien's avatar
sebastien committed
41

42
  Pi = AddNonNegativeConstant("3.141592653589793");
43
44
}

45
46
47
48
49
50
51
52
53
54
55
56
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},
  is_dynamic {is_dynamic_arg}
{
  initConstants();
}

57
DataTree::~DataTree() = default;
58

59
60
61
62
63
64
65
66
67
68
69
DataTree::DataTree(const DataTree &d) :
  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}
{
  // Constants must be initialized first because they are used in some Add* methods
  initConstants();

  for (const auto & it : d.node_list)
70
    it->clone(*this);
71
72
73
74

  assert(node_list.size() == d.node_list.size());

  for (const auto & it : d.local_variables_table)
75
    local_variables_table[it.first] = it.second->clone(*this);
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
}

DataTree &
DataTree::operator=(const DataTree &d)
{
  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);

  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();

  for (const auto & it : d.node_list)
103
    it->clone(*this);
104
105
106
107
108
109

  assert(node_list.size() == d.node_list.size());

  local_variables_vector = d.local_variables_vector;

  for (const auto & it : d.local_variables_table)
110
    local_variables_table[it.first] = it.second->clone(*this);
111
112
113
114

  return *this;
}

115
expr_t
116
DataTree::AddNonNegativeConstant(const string &value)
117
{
118
  int id = num_constants.AddNonNegativeConstant(value);
119

120
  auto it = num_const_node_map.find(id);
121
122
  if (it != num_const_node_map.end())
    return it->second;
123
124
125
126
127
128

  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;
129
130
}

sebastien's avatar
sebastien committed
131
VariableNode *
132
DataTree::AddVariable(int symb_id, int lag)
133
{
134
135
136
137
138
139
  if (lag != 0 && !is_dynamic)
    {
      cerr << "Leads/lags not authorized in this DataTree" << endl;
      exit(EXIT_FAILURE);
    }

140
  auto it = variable_node_map.find({ symb_id, lag });
141
142
  if (it != variable_node_map.end())
    return it->second;
143
144
145
146
147
148

  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;
149
}
sebastien's avatar
sebastien committed
150

151
152
153
154
155
156
157
158
159
160
161
162
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;
}

163
164
165
bool
DataTree::ParamUsedWithLeadLagInternal() const
{
166
  for (const auto & it : variable_node_map)
167
    if (symbol_table.getType(it.first.first) == SymbolType::parameter && it.first.second != 0)
168
169
170
171
      return true;
  return false;
}

172
173
expr_t
DataTree::AddPlus(expr_t iArg1, expr_t iArg2)
174
175
176
177
{
  if (iArg1 != Zero && iArg2 != Zero)
    {
      // Simplify x+(-y) in x-y
178
      auto *uarg2 = dynamic_cast<UnaryOpNode *>(iArg2);
179
      if (uarg2 != nullptr && uarg2->get_op_code() == UnaryOpcode::uminus)
sebastien's avatar
sebastien committed
180
        return AddMinus(iArg1, uarg2->get_arg());
181
182
183
184
185

      // To treat commutativity of "+"
      // Nodes iArg1 and iArg2 are sorted by index
      if (iArg1->idx > iArg2->idx)
        {
186
          expr_t tmp = iArg1;
187
188
189
          iArg1 = iArg2;
          iArg2 = tmp;
        }
190
      return AddBinaryOp(iArg1, BinaryOpcode::plus, iArg2);
191
192
193
194
195
196
197
198
199
    }
  else if (iArg1 != Zero)
    return iArg1;
  else if (iArg2 != Zero)
    return iArg2;
  else
    return Zero;
}

200
201
expr_t
DataTree::AddMinus(expr_t iArg1, expr_t iArg2)
202
{
203
  if (iArg2 == Zero)
204
    return iArg1;
205
206

  if (iArg1 == Zero)
207
    return AddUMinus(iArg2);
208
209

  if (iArg1 == iArg2)
210
    return Zero;
211

212
  return AddBinaryOp(iArg1, BinaryOpcode::minus, iArg2);
213
214
}

215
216
expr_t
DataTree::AddUMinus(expr_t iArg1)
217
218
219
220
{
  if (iArg1 != Zero)
    {
      // Simplify -(-x) in x
221
      auto *uarg = dynamic_cast<UnaryOpNode *>(iArg1);
222
      if (uarg != nullptr && uarg->get_op_code() == UnaryOpcode::uminus)
sebastien's avatar
sebastien committed
223
        return uarg->get_arg();
224

225
      return AddUnaryOp(UnaryOpcode::uminus, iArg1);
226
227
228
229
230
    }
  else
    return Zero;
}

231
232
expr_t
DataTree::AddTimes(expr_t iArg1, expr_t iArg2)
233
234
235
236
237
238
239
240
241
242
243
{
  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)
        {
244
          expr_t tmp = iArg1;
245
246
247
          iArg1 = iArg2;
          iArg2 = tmp;
        }
248
      return AddBinaryOp(iArg1, BinaryOpcode::times, iArg2);
249
250
251
252
253
254
255
256
257
258
259
    }
  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;
}

260
expr_t
261
DataTree::AddDivide(expr_t iArg1, expr_t iArg2) noexcept(false)
262
{
263
  if (iArg2 == One)
264
    return iArg1;
265
266
267

  // This test should be before the next two, otherwise 0/0 won't be rejected
  if (iArg2 == Zero)
268
    {
269
      cerr << "ERROR: Division by zero!" << endl;
270
      throw DivisionByZeroException();
271
    }
272
273
274
275
276
277
278

  if (iArg1 == Zero)
    return Zero;

  if (iArg1 == iArg2)
    return One;

279
  return AddBinaryOp(iArg1, BinaryOpcode::divide, iArg2);
280
281
}

282
283
expr_t
DataTree::AddLess(expr_t iArg1, expr_t iArg2)
284
{
285
  return AddBinaryOp(iArg1, BinaryOpcode::less, iArg2);
286
287
}

288
289
expr_t
DataTree::AddGreater(expr_t iArg1, expr_t iArg2)
290
{
291
  return AddBinaryOp(iArg1, BinaryOpcode::greater, iArg2);
292
293
}

294
295
expr_t
DataTree::AddLessEqual(expr_t iArg1, expr_t iArg2)
296
{
297
  return AddBinaryOp(iArg1, BinaryOpcode::lessEqual, iArg2);
298
299
}

300
301
expr_t
DataTree::AddGreaterEqual(expr_t iArg1, expr_t iArg2)
302
{
303
  return AddBinaryOp(iArg1, BinaryOpcode::greaterEqual, iArg2);
304
305
}

306
307
expr_t
DataTree::AddEqualEqual(expr_t iArg1, expr_t iArg2)
308
{
309
  return AddBinaryOp(iArg1, BinaryOpcode::equalEqual, iArg2);
310
311
}

312
313
expr_t
DataTree::AddDifferent(expr_t iArg1, expr_t iArg2)
314
{
315
  return AddBinaryOp(iArg1, BinaryOpcode::different, iArg2);
316
317
}

318
319
expr_t
DataTree::AddPower(expr_t iArg1, expr_t iArg2)
320
{
321
  if (iArg1 != Zero && iArg2 != Zero && iArg1 != One && iArg2 != One)
322
    return AddBinaryOp(iArg1, BinaryOpcode::power, iArg2);
323
324
  else if (iArg1 == One)
    return One;
325
326
327
328
329
330
331
332
  else if (iArg2 == One)
    return iArg1;
  else if (iArg2 == Zero)
    return One;
  else
    return Zero;
}

333
334
335
336
expr_t
DataTree::AddPowerDeriv(expr_t iArg1, expr_t iArg2, int powerDerivOrder)
{
  assert(powerDerivOrder > 0);
337
  return AddBinaryOp(iArg1, BinaryOpcode::powerDeriv, iArg2, powerDerivOrder);
338
339
}

340
341
342
expr_t
DataTree::AddDiff(expr_t iArg1)
{
343
  return AddUnaryOp(UnaryOpcode::diff, iArg1);
344
345
}

346
347
348
expr_t
DataTree::AddAdl(expr_t iArg1, const string &name, const vector<int> &lags)
{
349
  return AddUnaryOp(UnaryOpcode::adl, iArg1, 0, 0, 0, string(name), lags);
350
351
}

352
353
expr_t
DataTree::AddExp(expr_t iArg1)
354
355
{
  if (iArg1 != Zero)
356
    return AddUnaryOp(UnaryOpcode::exp, iArg1);
357
358
359
360
  else
    return One;
}

361
362
expr_t
DataTree::AddLog(expr_t iArg1)
363
364
{
  if (iArg1 != Zero && iArg1 != One)
365
    return AddUnaryOp(UnaryOpcode::log, iArg1);
366
367
368
369
  else if (iArg1 == One)
    return Zero;
  else
    {
370
      cerr << "ERROR: log(0) not defined!" << endl;
371
      exit(EXIT_FAILURE);
372
373
374
    }
}

375
376
expr_t
DataTree::AddLog10(expr_t iArg1)
377
378
{
  if (iArg1 != Zero && iArg1 != One)
379
    return AddUnaryOp(UnaryOpcode::log10, iArg1);
380
381
382
383
  else if (iArg1 == One)
    return Zero;
  else
    {
384
      cerr << "ERROR: log10(0) not defined!" << endl;
385
      exit(EXIT_FAILURE);
386
387
388
    }
}

389
390
expr_t
DataTree::AddCos(expr_t iArg1)
391
392
{
  if (iArg1 != Zero)
393
    return AddUnaryOp(UnaryOpcode::cos, iArg1);
394
395
396
397
  else
    return One;
}

398
399
expr_t
DataTree::AddSin(expr_t iArg1)
400
401
{
  if (iArg1 != Zero)
402
    return AddUnaryOp(UnaryOpcode::sin, iArg1);
403
404
405
406
  else
    return Zero;
}

407
408
expr_t
DataTree::AddTan(expr_t iArg1)
409
410
{
  if (iArg1 != Zero)
411
    return AddUnaryOp(UnaryOpcode::tan, iArg1);
412
413
414
415
  else
    return Zero;
}

416
417
expr_t
DataTree::AddAcos(expr_t iArg1)
418
419
{
  if (iArg1 != One)
420
    return AddUnaryOp(UnaryOpcode::acos, iArg1);
421
422
423
424
  else
    return Zero;
}

425
426
expr_t
DataTree::AddAsin(expr_t iArg1)
427
428
{
  if (iArg1 != Zero)
429
    return AddUnaryOp(UnaryOpcode::asin, iArg1);
430
431
432
433
  else
    return Zero;
}

434
435
expr_t
DataTree::AddAtan(expr_t iArg1)
436
437
{
  if (iArg1 != Zero)
438
    return AddUnaryOp(UnaryOpcode::atan, iArg1);
439
440
441
442
  else
    return Zero;
}

443
444
expr_t
DataTree::AddCosh(expr_t iArg1)
445
446
{
  if (iArg1 != Zero)
447
    return AddUnaryOp(UnaryOpcode::cosh, iArg1);
448
449
450
451
  else
    return One;
}

452
453
expr_t
DataTree::AddSinh(expr_t iArg1)
454
455
{
  if (iArg1 != Zero)
456
    return AddUnaryOp(UnaryOpcode::sinh, iArg1);
457
458
459
460
  else
    return Zero;
}

461
462
expr_t
DataTree::AddTanh(expr_t iArg1)
463
464
{
  if (iArg1 != Zero)
465
    return AddUnaryOp(UnaryOpcode::tanh, iArg1);
466
467
468
469
  else
    return Zero;
}

470
471
expr_t
DataTree::AddAcosh(expr_t iArg1)
472
473
{
  if (iArg1 != One)
474
    return AddUnaryOp(UnaryOpcode::acosh, iArg1);
475
476
477
478
  else
    return Zero;
}

479
480
expr_t
DataTree::AddAsinh(expr_t iArg1)
481
482
{
  if (iArg1 != Zero)
483
    return AddUnaryOp(UnaryOpcode::asinh, iArg1);
484
485
486
487
  else
    return Zero;
}

488
489
expr_t
DataTree::AddAtanh(expr_t iArg1)
490
491
{
  if (iArg1 != Zero)
492
    return AddUnaryOp(UnaryOpcode::atanh, iArg1);
493
494
495
496
  else
    return Zero;
}

497
498
expr_t
DataTree::AddSqrt(expr_t iArg1)
499
500
{
  if (iArg1 != Zero)
501
    return AddUnaryOp(UnaryOpcode::sqrt, iArg1);
502
503
504
505
  else
    return Zero;
}

506
507
508
509
510
511
512
513
expr_t
DataTree::AddAbs(expr_t iArg1)
{
  if (iArg1 == Zero)
    return Zero;
  if (iArg1 == One)
    return One;
  else
514
    return AddUnaryOp(UnaryOpcode::abs, iArg1);
515
516
517
518
519
520
521
522
523
524
}

expr_t
DataTree::AddSign(expr_t iArg1)
{
  if (iArg1 == Zero)
    return Zero;
  if (iArg1 == One)
    return One;
  else
525
    return AddUnaryOp(UnaryOpcode::sign, iArg1);
526
527
}

528
529
expr_t
DataTree::AddErf(expr_t iArg1)
530
531
{
  if (iArg1 != Zero)
532
    return AddUnaryOp(UnaryOpcode::erf, iArg1);
533
534
535
536
  else
    return Zero;
}

537
538
expr_t
DataTree::AddMax(expr_t iArg1, expr_t iArg2)
539
{
540
  return AddBinaryOp(iArg1, BinaryOpcode::max, iArg2);
541
542
}

543
544
expr_t
DataTree::AddMin(expr_t iArg1, expr_t iArg2)
545
{
546
  return AddBinaryOp(iArg1, BinaryOpcode::min, iArg2);
547
548
}

549
550
expr_t
DataTree::AddNormcdf(expr_t iArg1, expr_t iArg2, expr_t iArg3)
551
{
552
  return AddTrinaryOp(iArg1, TrinaryOpcode::normcdf, iArg2, iArg3);
553
554
}

555
556
expr_t
DataTree::AddNormpdf(expr_t iArg1, expr_t iArg2, expr_t iArg3)
557
{
558
  return AddTrinaryOp(iArg1, TrinaryOpcode::normpdf, iArg2, iArg3);
559
560
}

561
562
expr_t
DataTree::AddSteadyState(expr_t iArg1)
563
{
564
  return AddUnaryOp(UnaryOpcode::steadyState, iArg1);
565
566
}

567
568
569
expr_t
DataTree::AddSteadyStateParamDeriv(expr_t iArg1, int param_symb_id)
{
570
  return AddUnaryOp(UnaryOpcode::steadyStateParamDeriv, iArg1, 0, param_symb_id);
571
572
573
574
575
}

expr_t
DataTree::AddSteadyStateParam2ndDeriv(expr_t iArg1, int param1_symb_id, int param2_symb_id)
{
576
  return AddUnaryOp(UnaryOpcode::steadyStateParam2ndDeriv, iArg1, 0, param1_symb_id, param2_symb_id);
577
578
}

579
580
expr_t
DataTree::AddExpectation(int iArg1, expr_t iArg2)
581
{
582
  return AddUnaryOp(UnaryOpcode::expectation, iArg2, iArg1);
583
584
}

Houtan Bastani's avatar
Houtan Bastani committed
585
expr_t
586
DataTree::AddVarExpectation(const string &model_name)
Houtan Bastani's avatar
Houtan Bastani committed
587
{
588
  auto it = var_expectation_node_map.find(model_name);
589
590
591
  if (it != var_expectation_node_map.end())
    return it->second;

592
593
594
595
596
  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;
Houtan Bastani's avatar
Houtan Bastani committed
597
598
}

599
expr_t
Houtan Bastani's avatar
Houtan Bastani committed
600
DataTree::AddPacExpectation(const string &model_name)
601
{
602
  auto it = pac_expectation_node_map.find(model_name);
603
604
  if (it != pac_expectation_node_map.end())
    return it->second;
605

606
607
608
609
610
  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;
611
612
}

613
614
expr_t
DataTree::AddEqual(expr_t iArg1, expr_t iArg2)
615
{
616
  return AddBinaryOp(iArg1, BinaryOpcode::equal, iArg2);
617
618
619
}

void
620
DataTree::AddLocalVariable(int symb_id, expr_t value) noexcept(false)
621
{
622
  assert(symbol_table.getType(symb_id) == SymbolType::modelLocalVariable);
sebastien's avatar
sebastien committed
623

624
  // Throw an exception if symbol already declared
625
  auto it = local_variables_table.find(symb_id);
626
  if (it != local_variables_table.end())
627
    throw LocalVariableException(symbol_table.getName(symb_id));
628

629
  local_variables_table[symb_id] = value;
630
  local_variables_vector.push_back(symb_id);
631
632
}

633
634
expr_t
DataTree::AddExternalFunction(int symb_id, const vector<expr_t> &arguments)
635
{
636
  assert(symbol_table.getType(symb_id) == SymbolType::externalFunction);
637

638
  auto it = external_function_node_map.find({ arguments, symb_id });
639
640
641
  if (it != external_function_node_map.end())
    return it->second;

642
643
644
645
646
  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;
647
}
sebastien's avatar
sebastien committed
648

649
expr_t
650
DataTree::AddFirstDerivExternalFunction(int top_level_symb_id, const vector<expr_t> &arguments, int input_index)
651
{
652
  assert(symbol_table.getType(top_level_symb_id) == SymbolType::externalFunction);
653

654
  auto it
655
    = first_deriv_external_function_node_map.find({ arguments, input_index, top_level_symb_id });
656
657
658
  if (it != first_deriv_external_function_node_map.end())
    return it->second;

659
660
661
662
663
  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;
664
}
665

666
expr_t
667
DataTree::AddSecondDerivExternalFunction(int top_level_symb_id, const vector<expr_t> &arguments, int input_index1, int input_index2)
668
{
669
  assert(symbol_table.getType(top_level_symb_id) == SymbolType::externalFunction);
670

671
  auto it
672
    = second_deriv_external_function_node_map.find({ arguments, input_index1, input_index2,
673
          top_level_symb_id });
674
675
676
  if (it != second_deriv_external_function_node_map.end())
    return it->second;

677
678
679
680
681
  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;
682
}
sebastien's avatar
sebastien committed
683

684
685
686
bool
DataTree::isSymbolUsed(int symb_id) const
{
687
688
  for (const auto & it : variable_node_map)
    if (it.first.first == symb_id)
689
690
691
692
693
694
695
696
      return true;

  if (local_variables_table.find(symb_id) != local_variables_table.end())
    return true;

  return false;
}

697
int
698
DataTree::getDerivID(int symb_id, int lag) const noexcept(false)
699
700
701
702
{
  throw UnknownDerivIDException();
}

703
SymbolType
704
DataTree::getTypeByDerivID(int deriv_id) const noexcept(false)
705
706
707
708
709
{
  throw UnknownDerivIDException();
}

int
710
DataTree::getLagByDerivID(int deriv_id) const noexcept(false)
711
712
713
714
715
{
  throw UnknownDerivIDException();
}

int
716
DataTree::getSymbIDByDerivID(int deriv_id) const noexcept(false)
717
718
719
720
721
722
723
724
725
{
  throw UnknownDerivIDException();
}

void
DataTree::addAllParamDerivId(set<int> &deriv_id_set)
{
}

726
int
727
DataTree::getDynJacobianCol(int deriv_id) const noexcept(false)
728
729
730
{
  throw UnknownDerivIDException();
}
sebastien's avatar
sebastien committed
731
732

bool
Sébastien Villemot's avatar
Sébastien Villemot committed
733
DataTree::isUnaryOpUsed(UnaryOpcode opcode) const
sebastien's avatar
sebastien committed
734
{
735
  for (const auto & it : unary_op_node_map)
736
    if (get<1>(it.first) == opcode)
Sébastien Villemot's avatar
Sébastien Villemot committed
737
738
739
740
741
742
743
744
      return true;

  return false;
}

bool
DataTree::isBinaryOpUsed(BinaryOpcode opcode) const
{
745
  for (const auto & it : binary_op_node_map)
746
    if (get<2>(it.first) == opcode)
Sébastien Villemot's avatar
Sébastien Villemot committed
747
748
749
750
751
752
753
754
      return true;

  return false;
}

bool
DataTree::isTrinaryOpUsed(TrinaryOpcode opcode) const
{
755
  for (const auto & it : trinary_op_node_map)
756
    if (get<3>(it.first) == opcode)
sebastien's avatar
sebastien committed
757
758
759
760
      return true;

  return false;
}
761
762
763
764

bool
DataTree::isExternalFunctionUsed(int symb_id) const
{
765
766
  for (const auto & it : external_function_node_map)
    if (it.first.second == symb_id)
767
768
769
770
771
772
773
774
      return true;

  return false;
}

bool
DataTree::isFirstDerivExternalFunctionUsed(int symb_id) const
{
775
  for (const auto & it : first_deriv_external_function_node_map)
776
    if (get<2>(it.first) == symb_id)
777
778
779
780
781
782
783
784
      return true;

  return false;
}

bool
DataTree::isSecondDerivExternalFunctionUsed(int symb_id) const
{
785
  for (const auto & it : second_deriv_external_function_node_map)
786
    if (get<3>(it.first) == symb_id)
787
788
789
790
      return true;

  return false;
}
Sébastien Villemot's avatar
Sébastien Villemot committed
791
792
793
794
795

int
DataTree::minLagForSymbol(int symb_id) const
{
  int r = 0;
796
797
798
  for (const auto & it : variable_node_map)
    if (it.first.first == symb_id && it.first.second < r)
      r = it.first.second;
Sébastien Villemot's avatar
Sébastien Villemot committed
799
800
  return r;
}
801
802
803
804

void
DataTree::writePowerDerivCHeader(ostream &output) const
{
805
  if (isBinaryOpUsed(BinaryOpcode::powerDeriv))
806
807
808
809
    output << "double getPowerDeriv(double, double, int);" << endl;
}

void
810
DataTree::writePowerDeriv(ostream &output) const
811
{
812
  if (isBinaryOpUsed(BinaryOpcode::powerDeriv))
813
814
815
816
817
    output << "/*" << endl
           << " * The k-th derivative of x^p" << endl
           << " */" << endl
           << "double getPowerDeriv(double x, double p, int k)" << endl
           << "{" << endl
818
819
820
           << "#ifdef _MSC_VER" << endl
           << "# define nearbyint(x) (fabs((x)-floor(x)) < fabs((x)-ceil(x)) ? floor(x) : ceil(x))" << endl
           << "#endif" << endl
821
           << "  if ( fabs(x) < " << near_zero << " && p > 0 && k > p && fabs(p-nearbyint(p)) < " << near_zero << " )" << endl
822
823
824
825
826
827
828
829
830
831
832
           << "    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;
}
833
834
835
836

void
DataTree::writeNormcdfCHeader(ostream &output) const
{
837
#if defined(_WIN32) || defined(__CYGWIN32__)
838
  if (isTrinaryOpUsed(TrinaryOpcode::normcdf))
839
840
841
842
843
844
845
846
847
    output << "#ifdef _MSC_VER" << endl
           << "double normcdf(double);" << endl
           << "#endif" << endl;
#endif
}

void
DataTree::writeNormcdf(ostream &output) const
{
848
#if defined(_WIN32) || defined(__CYGWIN32__)
849
  if (isTrinaryOpUsed(TrinaryOpcode::normcdf))
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
    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
}
878
879
880
881
882
883
884
885
886

string
DataTree::packageDir(const string &package)
{
  regex pat{"\\."};
  string dirname = "+" + regex_replace(package, pat, "/+");
  boost::filesystem::create_directories(dirname);
  return dirname;
}