DataTree.cc 9.76 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2009 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
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

#include "DataTree.hh"

DataTree::DataTree(SymbolTable &symbol_table_arg, NumericalConstants &num_constants_arg) :
  symbol_table(symbol_table_arg),
  num_constants(num_constants_arg),
  node_counter(0),
  variable_table(symbol_table_arg)
{
  Zero = AddNumConstant("0");
  One = AddNumConstant("1");
sebastien's avatar
sebastien committed
33
  Two = AddNumConstant("2");
34
35

  MinusOne = AddUMinus(One);
36
37
38
39

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

  Pi = AddNumConstant("3.141592653589793");
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
}

DataTree::~DataTree()
{
  for (node_list_type::iterator it = node_list.begin(); it != node_list.end(); it++)
    delete *it;
}

NodeID
DataTree::AddNumConstant(const string &value)
{
  int id = num_constants.AddConstant(value);

  num_const_node_map_type::iterator it = num_const_node_map.find(id);
  if (it != num_const_node_map.end())
    return it->second;
  else
    return new NumConstNode(*this, id);
}

NodeID
sebastien's avatar
sebastien committed
63
DataTree::AddVariableInternal(const string &name, int lag)
64
65
66
{
  int symb_id = symbol_table.getID(name);

sebastien's avatar
sebastien committed
67
  variable_node_map_type::iterator it = variable_node_map.find(make_pair(symb_id, lag));
68
69
70
  if (it != variable_node_map.end())
    return it->second;
  else
sebastien's avatar
sebastien committed
71
    return new VariableNode(*this, symb_id, lag);
72
73
}

sebastien's avatar
sebastien committed
74
75
76
77
78
79
80
81
82
83
84
NodeID
DataTree::AddVariable(const string &name, int lag)
{
  if (lag != 0)
    {
      cerr << "DataTree::AddVariable: a non-zero lag is forbidden here!" << endl;
      exit(EXIT_FAILURE);
    }
  return AddVariableInternal(name, lag);
}

85
86
87
88
89
90
91
NodeID
DataTree::AddPlus(NodeID iArg1, NodeID iArg2)
{
  if (iArg1 != Zero && iArg2 != Zero)
    {
      // Simplify x+(-y) in x-y
      UnaryOpNode *uarg2 = dynamic_cast<UnaryOpNode *>(iArg2);
sebastien's avatar
sebastien committed
92
93
      if (uarg2 != NULL && uarg2->get_op_code() == oUminus)
        return AddMinus(iArg1, uarg2->get_arg());
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

      // To treat commutativity of "+"
      // Nodes iArg1 and iArg2 are sorted by index
      if (iArg1->idx > iArg2->idx)
        {
          NodeID tmp = iArg1;
          iArg1 = iArg2;
          iArg2 = tmp;
        }
      return AddBinaryOp(iArg1, oPlus, iArg2);
    }
  else if (iArg1 != Zero)
    return iArg1;
  else if (iArg2 != Zero)
    return iArg2;
  else
    return Zero;
}

NodeID
DataTree::AddMinus(NodeID iArg1, NodeID iArg2)
{
  if (iArg1 != Zero && iArg2 != Zero)
    return AddBinaryOp(iArg1, oMinus, iArg2);
  else if (iArg1 != Zero)
    return iArg1;
  else if (iArg2 != Zero)
    return AddUMinus(iArg2);
  else
    return Zero;
}

NodeID
DataTree::AddUMinus(NodeID iArg1)
{
  if (iArg1 != Zero)
    {
      // Simplify -(-x) in x
      UnaryOpNode *uarg = dynamic_cast<UnaryOpNode *>(iArg1);
sebastien's avatar
sebastien committed
133
134
      if (uarg != NULL && uarg->get_op_code() == oUminus)
        return uarg->get_arg();
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

      return AddUnaryOp(oUminus, iArg1);
    }
  else
    return Zero;
}

NodeID
DataTree::AddTimes(NodeID iArg1, NodeID iArg2)
{
  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)
        {
          NodeID tmp = iArg1;
          iArg1 = iArg2;
          iArg2 = tmp;
        }
      return AddBinaryOp(iArg1, oTimes, iArg2);
    }
  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;
}

NodeID
DataTree::AddDivide(NodeID iArg1, NodeID iArg2)
{
  if (iArg1 != Zero && iArg2 != Zero && iArg2 != One)
    return AddBinaryOp(iArg1, oDivide, iArg2);
  else if (iArg2 == One)
    return iArg1;
  else if (iArg1 == Zero && iArg2 != Zero)
    return Zero;
  else
    {
      cerr << "Division by zero!" << endl;
183
      exit(EXIT_FAILURE);
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
    }
}

NodeID
DataTree::AddLess(NodeID iArg1, NodeID iArg2)
{
  return AddBinaryOp(iArg1, oLess, iArg2);
}

NodeID
DataTree::AddGreater(NodeID iArg1, NodeID iArg2)
{
  return AddBinaryOp(iArg1, oGreater, iArg2);
}

NodeID
DataTree::AddLessEqual(NodeID iArg1, NodeID iArg2)
{
  return AddBinaryOp(iArg1, oLessEqual, iArg2);
}

NodeID
DataTree::AddGreaterEqual(NodeID iArg1, NodeID iArg2)
{
  return AddBinaryOp(iArg1, oGreaterEqual, iArg2);
}

NodeID
DataTree::AddEqualEqual(NodeID iArg1, NodeID iArg2)
{
  return AddBinaryOp(iArg1, oEqualEqual, iArg2);
}

NodeID
DataTree::AddDifferent(NodeID iArg1, NodeID iArg2)
{
  return AddBinaryOp(iArg1, oDifferent, iArg2);
}

NodeID
DataTree::AddPower(NodeID iArg1, NodeID iArg2)
{
  if (iArg1 != Zero && iArg2 != Zero && iArg2 != One)
    return AddBinaryOp(iArg1, oPower, iArg2);
  else if (iArg2 == One)
    return iArg1;
  else if (iArg2 == Zero)
    return One;
  else
    return Zero;
}

NodeID
DataTree::AddExp(NodeID iArg1)
{
  if (iArg1 != Zero)
    return AddUnaryOp(oExp, iArg1);
  else
    return One;
}

NodeID
DataTree::AddLog(NodeID iArg1)
{
  if (iArg1 != Zero && iArg1 != One)
    return AddUnaryOp(oLog, iArg1);
  else if (iArg1 == One)
    return Zero;
  else
    {
      cerr << "log(0) isn't available" << endl;
255
      exit(EXIT_FAILURE);
256
257
258
259
260
261
262
263
264
265
266
267
    }
}

NodeID DataTree::AddLog10(NodeID iArg1)
{
  if (iArg1 != Zero && iArg1 != One)
    return AddUnaryOp(oLog10, iArg1);
  else if (iArg1 == One)
    return Zero;
  else
    {
      cerr << "log10(0) isn't available" << endl;
268
      exit(EXIT_FAILURE);
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
    }
}

NodeID
DataTree::AddCos(NodeID iArg1)
{
  if (iArg1 != Zero)
    return AddUnaryOp(oCos, iArg1);
  else
    return One;
}

NodeID
DataTree::AddSin(NodeID iArg1)
{
  if (iArg1 != Zero)
    return AddUnaryOp(oSin, iArg1);
  else
    return Zero;
}

NodeID
DataTree::AddTan(NodeID iArg1)
{
  if (iArg1 != Zero)
    return AddUnaryOp(oTan, iArg1);
  else
    return Zero;
}

NodeID
sebastien's avatar
sebastien committed
300
DataTree::AddAcos(NodeID iArg1)
301
302
303
304
305
306
307
308
{
  if (iArg1 != One)
    return AddUnaryOp(oAcos, iArg1);
  else
    return Zero;
}

NodeID
sebastien's avatar
sebastien committed
309
DataTree::AddAsin(NodeID iArg1)
310
311
312
313
314
315
316
317
{
  if (iArg1 != Zero)
    return AddUnaryOp(oAsin, iArg1);
  else
    return Zero;
}

NodeID
sebastien's avatar
sebastien committed
318
DataTree::AddAtan(NodeID iArg1)
319
320
321
322
323
324
325
326
{
  if (iArg1 != Zero)
    return AddUnaryOp(oAtan, iArg1);
  else
    return Zero;
}

NodeID
sebastien's avatar
sebastien committed
327
DataTree::AddCosh(NodeID iArg1)
328
329
330
331
332
333
334
335
{
  if (iArg1 != Zero)
    return AddUnaryOp(oCosh, iArg1);
  else
    return One;
}

NodeID
sebastien's avatar
sebastien committed
336
DataTree::AddSinh(NodeID iArg1)
337
338
339
340
341
342
343
344
{
  if (iArg1 != Zero)
    return AddUnaryOp(oSinh, iArg1);
  else
    return Zero;
}

NodeID
sebastien's avatar
sebastien committed
345
DataTree::AddTanh(NodeID iArg1)
346
347
348
349
350
351
352
353
{
  if (iArg1 != Zero)
    return AddUnaryOp(oTanh, iArg1);
  else
    return Zero;
}

NodeID
sebastien's avatar
sebastien committed
354
DataTree::AddAcosh(NodeID iArg1)
355
356
357
358
359
360
361
362
{
  if (iArg1 != One)
    return AddUnaryOp(oAcosh, iArg1);
  else
    return Zero;
}

NodeID
sebastien's avatar
sebastien committed
363
DataTree::AddAsinh(NodeID iArg1)
364
365
366
367
368
369
370
371
{
  if (iArg1 != Zero)
    return AddUnaryOp(oAsinh, iArg1);
  else
    return Zero;
}

NodeID
sebastien's avatar
sebastien committed
372
DataTree::AddAtanh(NodeID iArg1)
373
374
375
376
377
378
379
380
{
  if (iArg1 != Zero)
    return AddUnaryOp(oAtanh, iArg1);
  else
    return Zero;
}

NodeID
sebastien's avatar
sebastien committed
381
DataTree::AddSqrt(NodeID iArg1)
382
383
384
385
386
387
388
389
{
  if (iArg1 != Zero)
    return AddUnaryOp(oSqrt, iArg1);
  else
    return Zero;
}

NodeID
sebastien's avatar
sebastien committed
390
DataTree::AddMax(NodeID iArg1, NodeID iArg2)
391
{
392
  return AddBinaryOp(iArg1, oMax, iArg2);
393
394
395
396
397
}

NodeID
DataTree::AddMin(NodeID iArg1, NodeID iArg2)
{
398
  return AddBinaryOp(iArg1, oMin, iArg2);
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
}

NodeID
DataTree::AddNormcdf(NodeID iArg1, NodeID iArg2, NodeID iArg3)
{
  return AddTrinaryOp(iArg1, oNormcdf, iArg2, iArg3);
}

NodeID
DataTree::AddEqual(NodeID iArg1, NodeID iArg2)
{
  return AddBinaryOp(iArg1, oEqual, iArg2);
}

void
sebastien's avatar
sebastien committed
414
DataTree::AddLocalVariable(const string &name, NodeID value) throw (LocalVariableException)
415
416
417
{
  int id = symbol_table.getID(name);

sebastien's avatar
sebastien committed
418
419
420
421
422
423
  if (symbol_table.getType(id) != eModelLocalVariable)
    {
      cerr << "Symbol " << name << " is not a model local variable!" << endl;
      exit(EXIT_FAILURE);
    }

424
425
426
  // Throw an exception if symbol already declared
  map<int, NodeID>::iterator it = local_variables_table.find(id);
  if (it != local_variables_table.end())
sebastien's avatar
sebastien committed
427
    throw LocalVariableException(name);
428
429
430
431
432
433
434

  local_variables_table[id] = value;
}

NodeID
DataTree::AddUnknownFunction(const string &function_name, const vector<NodeID> &arguments)
{
sebastien's avatar
sebastien committed
435
436
437
  int id = symbol_table.getID(function_name);

  if (symbol_table.getType(id) != eUnknownFunction)
438
    {
sebastien's avatar
sebastien committed
439
      cerr << "Symbol " << function_name << " is not a function name!" << endl;
440
      exit(EXIT_FAILURE);
441
442
443
444
    }

  return new UnknownFunctionNode(*this, id, arguments);
}
sebastien's avatar
sebastien committed
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463

void
DataTree::fillEvalContext(eval_context_type &eval_context) const
{
  for(map<int, NodeID>::const_iterator it = local_variables_table.begin();
      it != local_variables_table.end(); it++)
    {
      try
        {
          const NodeID expression = it->second;
          double val = expression->eval(eval_context);
          eval_context[it->first] = val;
        }
      catch(ExprNode::EvalException &e)
        {
          // Do nothing
        }
    }
}
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478

bool
DataTree::isSymbolUsed(int symb_id) const
{
  for(variable_node_map_type::const_iterator it = variable_node_map.begin();
      it != variable_node_map.end(); it++)
    if (it->first.first == symb_id)
      return true;

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

  return false;
}