COPASI API  4.16.103
CDerive.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/function/CDerive.cpp,v $
3 // $Revision: 1.4 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2010/07/16 18:59:37 $
7 // End CVS Header
8 
9 // Copyright (C) 2010 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 // Copyright (C) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
15 // Properties, Inc. and EML Research, gGmbH.
16 // All rights reserved.
17 
18 #include "CDerive.h"
19 
20 #include "CEvaluationNode.h"
21 
22 CEvaluationNode* CDerive::deriveBranch(const CEvaluationNode* node, unsigned C_INT32 variableIndex, const CCopasiObject * pObject)
23 {
24  CEvaluationNode * newNode = NULL;
25 
26  const CEvaluationNodeOperator * pENO = dynamic_cast<const CEvaluationNodeOperator*>(node);
27 
28  if (pENO)
29  {
30  if (!pENO->getLeft() || !pENO->getRight()) return NULL;
31 
32  CEvaluationNode * pLeftDeriv = deriveBranch(pENO->getLeft(), variableIndex, pObject);
33 
34  if (!pLeftDeriv) return NULL;
35 
36  CEvaluationNode * pRightDeriv = deriveBranch(pENO->getRight(), variableIndex, pObject);
37 
38  if (!pRightDeriv) {delete pLeftDeriv; return NULL;}
39 
40  // we now know that derivations of the left and right branch exist
41 
43  {
45  {
46  CEvaluationNode * pLeftCopy = pENO->getLeft()->copyBranch();
47  CEvaluationNode * pRightCopy = pENO->getRight()->copyBranch();
48 
50 
51  CEvaluationNodeOperator * tmpNode;
53  tmpNode->addChild(pRightCopy);
54  tmpNode->addChild(pLeftDeriv);
55  //tmpNode->compile(NULL);
56  newNode->addChild(tmpNode);
57 
59  tmpNode->addChild(pRightDeriv);
60  tmpNode->addChild(pLeftCopy);
61  //tmpNode->compile(NULL);
62  newNode->addChild(tmpNode);
63 
64  //if (newNode) newNode->compile(NULL);
65  return newNode;
66  }
67  break;
68 
70  {
71  CEvaluationNode * pLeftCopy = pENO->getLeft()->copyBranch();
72  CEvaluationNode * pRightCopy = pENO->getRight()->copyBranch();
73 
74  //numerator
76 
77  CEvaluationNodeOperator * tmpNode;
79  tmpNode->addChild(pRightCopy);
80  tmpNode->addChild(pLeftDeriv);
81  //tmpNode->compile(NULL);
82  minusNode->addChild(tmpNode);
83 
85  tmpNode->addChild(pRightDeriv);
86  tmpNode->addChild(pLeftCopy);
87  //tmpNode->compile(NULL);
88  minusNode->addChild(tmpNode);
89 
90  minusNode->compile(NULL);
91 
92  //denominator
94  pRightCopy = pENO->getRight()->copyBranch(); //new copy
95  powerNode->addChild(pRightCopy);
97  //powerNode->compile(NULL);
98 
100  newNode->addChild(minusNode);
101  newNode->addChild(powerNode);
102  //if (newNode) newNode->compile(NULL);
103  return newNode;
104  }
105  break;
106 
109  newNode->addChild(pLeftDeriv);
110  newNode->addChild(pRightDeriv);
111  //TODO check for zeros
112 
113  //if (newNode) newNode->compile(NULL);
114  return newNode;
115  break;
116 
119  newNode->addChild(pLeftDeriv);
120  newNode->addChild(pRightDeriv);
121  //TODO check for zeros
122 
123  //if (newNode) newNode->compile(NULL);
124  return newNode;
125  break;
126 
128  {
129  CEvaluationNode * pLeftCopy = pENO->getLeft()->copyBranch();
130  CEvaluationNode * pRightCopy = pENO->getRight()->copyBranch();
131 
132  // a^(b-1)
134  powerNode->addChild(pLeftCopy); // add a
135 
136  CEvaluationNodeOperator * tmpNode;
138  tmpNode->addChild(pRightCopy); // add b
140  powerNode->addChild(tmpNode); // add b-1
141 
142  // b*a´ + a*b´* ln a
144 
146  pRightCopy = pENO->getRight()->copyBranch(); //new copy of b
147  tmpNode->addChild(pRightCopy); // add b
148  tmpNode->addChild(pLeftDeriv); // add a´
149  plusNode->addChild(tmpNode); // add b*a´
150 
152  pLeftCopy = pENO->getLeft()->copyBranch(); //new copy of a
153  tmpNode->addChild(pLeftCopy); // add a
154 
156  tmptmpNode->addChild(pRightDeriv); // add b´
158  pLeftCopy = pENO->getLeft()->copyBranch(); //new copy of a
159  funcNode->addChild(pLeftCopy); // add a
160  tmptmpNode->addChild(funcNode); // add ln a
161 
162  tmpNode->addChild(tmptmpNode); // add b´ * ln a
163  plusNode->addChild(tmpNode); // add a * b´ * ln a
164 
165  // a^(b-1)*(b*a´ + a*b´ * ln a)
167  newNode->addChild(powerNode);
168  newNode->addChild(plusNode);
169  return newNode;
170  }
171  break;
172 
173  default:
174  break;
175  }
176  }
177 
178  const CEvaluationNodeVariable * pENV = dynamic_cast<const CEvaluationNodeVariable*>(node);
179 
180  if (pENV)
181  {
182  if (pObject) return NULL; // if a variable node occurs, we are differentiating a function
183 
184  if (variableIndex == pENV->getIndex())
186  else
188 
189  return newNode;
190  }
191 
192  const CEvaluationNodeNumber * pENN = dynamic_cast<const CEvaluationNodeNumber*>(node);
193 
194  if (pENN)
195  {
197  return newNode;
198  }
199 
200  return newNode;
201 }
202 
203 //static
205 {
206  if (!node) return;
207 
208  node->compile(pTree);
209 
210  CEvaluationNode* child = dynamic_cast<CEvaluationNode*>(node->getChild());
211 
212  while (child != NULL)
213  {
214  compileTree(child, pTree);
215  child = dynamic_cast<CEvaluationNode*>(child->getSibling());
216  }
217 }
CEvaluationNode * copyBranch() const
CCopasiNode< Data > * getSibling()
Definition: CCopasiNode.h:353
virtual bool compile(const CEvaluationTree *pTree)
static void compileTree(CEvaluationNode *node, const CEvaluationTree *pTree)
Definition: CDerive.cpp:204
const Type & getType() const
static CEvaluationNode * deriveBranch(const CEvaluationNode *node, unsigned C_INT32 variableIndex, const CCopasiObject *pObject=NULL)
Definition: CDerive.cpp:22
#define C_INT32
Definition: copasi.h:90
virtual bool addChild(CCopasiNode< Data > *pChild, CCopasiNode< Data > *pAfter=NULL)
Definition: CCopasiNode.h:156
static Type subType(const Type &type)
virtual bool compile(const CEvaluationTree *pTree)
CCopasiNode< Data > * getChild()
Definition: CCopasiNode.h:210