OSExpressionTree.cpp
Go to the documentation of this file.
1 /* $Id: OSExpressionTree.cpp 5284 2017-12-08 13:52:50Z stefan $ */
15 #include "OSOutput.h"
16 #include "OSExpressionTree.h"
17 #include <vector>
18 
19 using std::endl;
20 
22 // m_treeRoot( NULL),
23 // m_bIsVectorValued(false),
24  mapVarIdx( NULL),
25  m_bIndexMapGenerated( false),
26  bADMustReTape( false),
27  bDestroyNlNodes( true)
28 {
29 }//end OSExpressionTree
30 
31 
33 {
34 #ifndef NDEBUG
36  "Inside the OSExpressionTree Destructor");
37 #endif
38 // if( bDestroyNlNodes == true)
39 // {
40 // if(m_treeRoot != NULL) delete m_treeRoot;
41 // m_treeRoot = NULL;
42 // }
43  if(mapVarIdx != NULL)
44  {
45  delete mapVarIdx;
46  mapVarIdx = NULL;
47  }
48 }//end ~OSExpressionTree
49 
50 #if 0
51 std::vector<ExprNode*> OSExpressionTree::getPrefixFromExpressionTree()
52 {
53  return m_treeRoot->getPrefixFromExpressionTree();
54 }//getPrefixFromExpressionTree
55 
56 
57 std::vector<ExprNode*> OSExpressionTree::getPostfixFromExpressionTree()
58 {
59  return m_treeRoot->getPostfixFromExpressionTree();
60 }//getPostfixFromExpressionTree
61 #endif
62 
63 #if 0
64 double OSExpressionTree::calculateFunction( double *x, bool new_x)
65 {
66  //calculateFunctionAD( x, functionEvaluated);
67  if( new_x == false)
68  {
69  return m_dTreeRootValue;
70  }
71  else
72  {
73  m_dTreeRootValue = m_treeRoot->calculateFunction( x);
74  return m_dTreeRootValue;
75  }
76 }//calculateFunction
77 #endif
78 
80 {
81 #ifndef NDEBUG
82  osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_trace, "Start comparing in OSExpressionTree");
83 #endif
84  if (this == NULL)
85  {
86  if (that == NULL)
87  return true;
88  else
89  {
90 #ifndef NDEBUG
91  osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_trace, "First object is NULL, second is not");
92 #endif
93  return false;
94  }
95  }
96  else
97  {
98  if (that == NULL)
99  {
100 #ifndef NDEBUG
101  osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_trace, "Second object is NULL, first is not");
102 #endif
103  return false;
104  }
105  else
106  {
107  if (this->m_bIndexMapGenerated != that->m_bIndexMapGenerated)
108  return false;
109  if (this->bADMustReTape != that->bADMustReTape)
110  return false;
111  if (this->bDestroyNlNodes != that->bDestroyNlNodes)
112  return false;
113  return true;
114  }
115  }
116 }//OSExpressionTree::IsEqual
117 
118 
120  m_treeRoot( NULL)
121 {
122 #ifndef NDEBUG
124  "Inside the ScalarExpressionTree Constructor");
125 #endif
126 }//end ScalarExpressionTree
127 
128 
130 {
131 #ifndef NDEBUG
133  "Inside the ScalarExpressionTree Destructor");
134 #endif
135  if( bDestroyNlNodes == true)
136  {
137  if(m_treeRoot != NULL) delete m_treeRoot;
138  m_treeRoot = NULL;
139  }
140 }//end ~ScalarExpressionTree
141 
143 {
145 }//getPrefixFromExpressionTree
146 
147 
149 {
151 }//getPostfixFromExpressionTree
152 
153 
155 {
156  if( m_bIndexMapGenerated == true) return mapVarIdx;
157  mapVarIdx = new std::map<int, int>();
158  std::map<int, int>::iterator m_mPosVarIdx;
160  m_bIndexMapGenerated = true;
161  return mapVarIdx;
162 }//getVariableIndicesMap
163 
164 
165 double ScalarExpressionTree::calculateFunction( double *x, bool new_x)
166 {
167  //calculateFunctionAD( x, functionEvaluated);
168  if( new_x == false)
169  {
170  return m_dTreeRootValue;
171  }
172  else
173  {
175  return m_dTreeRootValue;
176  }
177 }//calculateFunction
178 
179 
181 {
182 #ifndef NDEBUG
183  osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_trace, "Start comparing in ScalarExpressionTree");
184 #endif
185  if (this == NULL)
186  {
187  if (that == NULL)
188  return true;
189  else
190  {
191 #ifndef NDEBUG
192  osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_trace, "First object is NULL, second is not");
193 #endif
194  return false;
195  }
196  }
197  else
198  {
199  if (that == NULL)
200  {
201 #ifndef NDEBUG
202  osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_trace, "Second object is NULL, first is not");
203 #endif
204  return false;
205  }
206  else
207  {
208  if (!this->m_treeRoot->IsEqual(that->m_treeRoot))
209  return false;
210 
211  return this->OSExpressionTree::IsEqual(that);
212  }
213  }
214 }//ScalarExpressionTree::IsEqual
215 
216 
218  m_treeRoot( NULL)
219 {
220 #ifndef NDEBUG
222  "Inside the MatrixExpressionTree Constructor");
223 #endif
224 }//end MatrixExpressionTree
225 
226 
228 {
229 #ifndef NDEBUG
231  "Inside the MatrixExpressionTree Destructor");
232 #endif
233  if( bDestroyNlNodes == true)
234  {
235  if(m_treeRoot != NULL) delete m_treeRoot;
236  m_treeRoot = NULL;
237  }
238 }//end ~MatrixExpressionTree
239 
240 
242 {
243  return m_treeRoot->/*OSnLMNode::*/getPrefixFromExpressionTree();
244 }//getPrefixFromExpressionTree
245 
246 
248 {
249  return m_treeRoot->/*OSnLMNode::*/getPostfixFromExpressionTree();
250 }//getPostfixFromExpressionTree
251 
252 
254 {
255 #ifndef NDEBUG
256  osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_trace, "Start comparing in MatrixExpressionTree");
257 #endif
258  if (this == NULL)
259  {
260  if (that == NULL)
261  return true;
262  else
263  {
264 #ifndef NDEBUG
265  osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_trace, "First object is NULL, second is not");
266 #endif
267  return false;
268  }
269  }
270  else
271  {
272  if (that == NULL)
273  {
274 #ifndef NDEBUG
275  osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_trace, "Second object is NULL, first is not");
276 #endif
277  return false;
278  }
279  else
280  {
281  if (!this->m_treeRoot->IsEqual(that->m_treeRoot))
282  return false;
283 
284  return this->OSExpressionTree::IsEqual(that);
285  }
286  }
287 }//MatrixExpressionTree::IsEqual
288 
std::vector< ExprNode * > getPostfixFromExpressionTree()
Get a vector of pointers to ExprNodes that correspond to a scalar-valued OSExpressionTree in postfix ...
const OSSmartPtr< OSOutput > osoutput
Definition: OSOutput.cpp:39
bool bDestroyNlNodes
m_bDestroyNlNodes is true if the destructor deletes the nodes in the Expression tree ...
virtual std::vector< ExprNode * > getPostfixFromExpressionTree()
Get a vector of pointers to ExprNodes that correspond to the expression tree in postfix format...
Definition: OSnLNode.cpp:468
virtual void getVariableIndexMap(std::map< int, int > *varIdx)
varIdx is a map where the key is the index of an OSnLNodeVariable and (*varIdx)[ idx] is the kth vari...
Definition: OSnLNode.cpp:493
std::vector< ExprNode * > getPostfixFromExpressionTree()
Get a vector of pointers to ExprNodes that correspond to a scalar-valued OSExpressionTree in postfix ...
~OSExpressionTree()
default destructor.
bool IsEqual(OSExpressionTree *that)
A function to check for the equality of two objects.
std::map< int, int > * getVariableIndicesMap()
Retrieve a map of the indices of the variables that are in the expression tree.
OSExpressionTree()
default constructor.
bool m_bIndexMapGenerated
Retrieve a map of the indices of the variables that are in the expression tree.
~ScalarExpressionTree()
default destructor.
double calculateFunction(double *x, bool new_x)
Calculate the expression tree function value given the current variable values using the calculateFun...
Used to hold part of the instance in memory.
std::vector< ExprNode * > getPrefixFromExpressionTree()
Get a vector of pointers to ExprNodes that correspond to a scalar-valued OSExpressionTree in prefix f...
bool IsEqual(MatrixExpressionTree *that)
A function to check for the equality of two objects.
double m_dTreeRootValue
A function to make a deep copy of an instance of this class.
bool bADMustReTape
is true if an AD Expression Tree has an expression that can change depending on the value of the inpu...
ScalarExpressionTree()
default constructor.
bool IsEqual(ScalarExpressionTree *that)
A function to check for the equality of two objects.
~MatrixExpressionTree()
default destructor.
OSnLNode * m_treeRoot
m_treeRoot holds the root node (of OSnLNode type) of the expression tree.
std::vector< ExprNode * > getPrefixFromExpressionTree()
Get a vector of pointers to OSnLNodes and OSnLMNodes that correspond to the (matrix-valued) expressio...
Definition: OSnLNode.cpp:2542
OSnLMNode * m_treeRoot
m_treeRoot holds the root node (of OSnLMNode type) of the expression tree.
std::vector< ExprNode * > getPrefixFromExpressionTree()
Get a vector of pointers to ExprNodes that correspond to a scalar-valued OSExpressionTree in prefix f...
bool IsEqual(OSnLMNode *that)
A function to check for the equality of two objects.
Definition: OSnLNode.cpp:2760
MatrixExpressionTree()
default constructor.
bool IsEqual(OSnLNode *that)
A function to check for the equality of two objects.
Definition: OSnLNode.cpp:541
std::vector< ExprNode * > getPostfixFromExpressionTree()
Get a vector of pointers to ExprNodes that correspond to the expression tree in postfix format...
Definition: OSnLNode.cpp:2564
Used to hold the instance in memory.
virtual std::vector< ExprNode * > getPrefixFromExpressionTree()
Get a vector of pointers to OSnLNodes and OSnLMNodes that correspond to the (scalar-valued or matrix-...
Definition: OSnLNode.cpp:446
std::map< int, int > * mapVarIdx
m_mapVarIdx is a map used to generate the infix expression for AD the key is idx, a variable number; ...
Used to hold the instance in memory.
virtual double calculateFunction(double *x)=0
Calculate the function value given the current variable values.
void fint fint fint real fint real * x