COPASI API  4.16.103
CEvaluationNodeFunction.h
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2015 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2005 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #ifndef COPASI_CEvaluationNodeFunction
16 #define COPASI_CEvaluationNodeFunction
17 
18 #include <limits>
19 #include <cmath>
20 
21 #ifdef WIN32 // These are not part of ANSI C and Visual C++ misses them.
22 static inline double asinh(double value)
23 {return log(value + sqrt(value * value + 1.0));}
24 
25 static inline double acosh(double value)
26 {return log(value + sqrt(value * value - 1.0));}
27 
28 static inline double atanh(double value)
29 {return (.5 * log((1.0 + value) / (1.0 - value)));}
30 
31 static inline double round(double value)
32 {return value < 0.0 ? -floor(-value + 0.5) : floor(value + 0.5);}
33 #endif // WIN32
34 
36 
37 class CRandom;
38 class CCopasiDataModel;
39 
40 /**
41  * This is the class for nodes presenting build-in functions used in an evaluation trees.
42  */
44 {
45 public:
46  /**
47  * Enumeration of possible node types.
48  */
49  enum SubType
50  {
51  INVALID = 0x00FFFFFF,
52  LOG = 0x00000000,
53  LOG10 = 0x00000001,
54  EXP = 0x00000002,
55  SIN = 0x00000003,
56  COS = 0x00000004,
57  TAN = 0x00000005,
58 #ifdef SEC
59 # undef SEC
60 #endif
61  SEC = 0x00000006,
62  CSC = 0x00000007,
63  COT = 0x00000008,
64  SINH = 0x00000009,
65  COSH = 0x0000000a,
66  TANH = 0x0000000b,
67  SECH = 0x0000000c,
68  CSCH = 0x0000000d,
69  COTH = 0x0000000e,
70  ARCSIN = 0x0000000f,
71  ARCCOS = 0x00000010,
72  ARCTAN = 0x00000011,
73  ARCSEC = 0x00000012,
74  ARCCSC = 0x00000013,
75  ARCCOT = 0x00000014,
76  ARCSINH = 0x00000015,
77  ARCCOSH = 0x00000016,
78  ARCTANH = 0x00000017,
79  ARCSECH = 0x00000018,
80  ARCCSCH = 0x00000019,
81  ARCCOTH = 0x0000001a,
82  SQRT = 0x0000001b,
83  ABS = 0x0000001c,
84  FLOOR = 0x0000001d,
85  CEIL = 0x0000001e,
86  FACTORIAL = 0x0000001f,
87  MINUS = 0x00000020,
88  PLUS = 0x00000021,
89  NOT = 0x00000022,
90  RUNIFORM = 0x00000023,
91  RNORMAL = 0x00000024,
92  MAX = 0x00000025,
93  MIN = 0x00000026,
94  RGAMMA = 0x00000027,
95  RPOISSON = 0x00000028
96  };
97 
98  // Operations
99 private:
100  /**
101  * Default constructor
102  */
104 
105 public:
106  /**
107  * Default constructor
108  * @param const SubType & subType
109  * @param const Data & data
110  */
112  const Data & data);
113 
114  /**
115  * Copy constructor
116  * @param const CEvaluationNodeFunction & src
117  */
119 
120  /**
121  * Destructor
122  */
123  virtual ~CEvaluationNodeFunction();
124 
125  /**
126  * Calculate the numerical result of the node. It is assumed that
127  * all child nodes are up to date.
128  */
129  virtual inline void calculate()
130  {
131  if (mpFunction)
132  {
133  mValue = (*mpFunction)(mpArg1->getValue());
134  }
135  else if (mpFunction2)
136  {
137  mValue = (*mpFunction2)(mpArg1->getValue(), mpArg2->getValue());
138  }
139  else if (mpFunction4)
140  {
141  mValue = (*mpFunction4)(mpArg1->getValue(), mpArg2->getValue(),
142  mpArg3->getValue(), mpArg4->getValue());
143  }
144  }
145 
146  /**
147  * Compile a node;
148  * @param const CEvaluationTree * pTree
149  * @return bool success;
150  */
151  virtual bool compile(const CEvaluationTree * pTree);
152 
153  /**
154  * Retrieve the infix value of the node and its eventual child nodes.
155  * @return const Data & value
156  */
157  virtual std::string getInfix(const std::vector< std::string > & children) const;
158 
159  /**
160  * Retrieve the display string of the node and its eventual child nodes.
161  * @return const Data & value
162  */
163  virtual std::string getDisplayString(const std::vector< std::string > & children) const;
164 
165  /**
166  * Retrieve the display string of the node and its eventual child nodes in C.
167  * @return const Data & value
168  */
169  virtual std::string getCCodeString(const std::vector< std::string > & children) const;
170 
171  /**
172  * Retrieve the display string of the node and its eventual child nodes
173  * in Berkeley Madonna format.
174  * @return const Data & value
175  */
176  virtual std::string getBerkeleyMadonnaString(const std::vector< std::string > & children) const;
177 
178  /**
179  ** Retrieve the display string of the node and its eventual child nodes
180  ** in XPPAUT format.
181  ** @return const Data & value
182  **/
183  virtual std::string getXPPString(const std::vector< std::string > & children) const;
184 
185  /**
186  * Creates a new CEvaluationNodeCall from an ASTNode and the given children
187  * @param const ASTNode* pNode
188  * @param const std::vector< CEvaluationNode * > & children
189  * @return CEvaluationNode * pCretedNode
190  */
191  static CEvaluationNode * fromAST(const ASTNode * pASTNode, const std::vector< CEvaluationNode * > & children);
192 
193  /**
194  * Check whether the result is Boolean
195  * @return bool isBoolean
196  */
197  virtual bool isBoolean() const;
198 
199  /**
200  * Create a new ASTNode corresponding to this FunctionNode.
201  * @return ASTNode* return a pointer to the newly created node;
202  */
203  virtual ASTNode* toAST(const CCopasiDataModel* pDataModel) const;
204 
205  /**
206  * Create a simplified node for an operatorNode with children from vector (if not exist, = NULL),
207  * and assign new children
208  * @return CEvaluationNode* return a pointer to the simplified node;
209  */
210  virtual CEvaluationNode* simplifyNode(const std::vector<CEvaluationNode*>& children) const;
211 
212  /**
213  * Build the MathML string
214  * @param const std::vector< std::string > & children
215  * @param bool expand = true
216  * @param const std::vector< std::vector< std::string > > & variables
217  * @return std::string MMLString
218  */
219  virtual std::string getMMLString(const std::vector< std::string > & children,
220  bool expand,
221  const std::vector< std::vector< std::string > > & variables) const;
222 
224  const CEvaluationNode * getLeft() const;
225 
226 private:
227  std::string handleSign(const std::string & str) const;
228 
229  std::string handleNot(const std::string & str) const;
230 
231  static inline C_FLOAT64 sec(C_FLOAT64 value)
232  {return 1.0 / cos(value);}
233 
234  static inline C_FLOAT64 csc(C_FLOAT64 value)
235  {return 1.0 / sin(value);}
236 
237  static inline C_FLOAT64 cot(C_FLOAT64 value)
238  {return 1.0 / tan(value);}
239 
240  static inline C_FLOAT64 sech(C_FLOAT64 value)
241  {return 1.0 / cosh(value);}
242 
243  static inline C_FLOAT64 csch(C_FLOAT64 value)
244  {return 1.0 / sinh(value);}
245 
246  static inline C_FLOAT64 coth(C_FLOAT64 value)
247  {return 1.0 / tanh(value);}
248 
249  static inline C_FLOAT64 arcsec(C_FLOAT64 value)
250  {return acos(1.0 / value);}
251 
252  static inline C_FLOAT64 arccsc(C_FLOAT64 value)
253  {return asin(1.0 / value);}
254 
255  static inline C_FLOAT64 arccot(C_FLOAT64 value)
256  {return atan(1.0 / value);}
257 
258  static inline C_FLOAT64 asech(C_FLOAT64 value)
259  {return acosh(1.0 / value);}
260 
261  static inline C_FLOAT64 acsch(C_FLOAT64 value)
262  {return asinh(1.0 / value);}
263 
264  static inline C_FLOAT64 acoth(C_FLOAT64 value)
265  {return atanh(1.0 / value);}
266 
267  static inline C_FLOAT64 factorial(C_FLOAT64 value)
268  {
269  if (value < 0.0 ||
270  value != ceil(value))
271  return std::numeric_limits<C_FLOAT64>::quiet_NaN();
272 
273  if (value > 170)
274  return std::numeric_limits<C_FLOAT64>::infinity();
275 
276  if (value == 0.0)
277  return 1.0;
278 
279  C_FLOAT64 Value = 1.0;
280  C_FLOAT64 Result = 1.0;
281 
282  while (Value < value) Result *= ++Value;
283 
284  return Result;
285  }
286 
287  static inline C_FLOAT64 minus(C_FLOAT64 value)
288  {return - value;}
289 
290  static inline C_FLOAT64 plus(C_FLOAT64 value)
291  {return value;}
292 
293  static inline C_FLOAT64 copasiNot(C_FLOAT64 value)
294  {return (value != 0.0) ? 0.0 : 1.0;}
295 
296  static C_FLOAT64 runiform(const C_FLOAT64 & lowerBound,
297  const C_FLOAT64 & upperBound);
298 
299  static C_FLOAT64 rnormal(const C_FLOAT64 & mean,
300  const C_FLOAT64 & sd);
301 
302  static C_FLOAT64 rgamma(const C_FLOAT64 & shape,
303  const C_FLOAT64 & scale);
304 
305  static C_FLOAT64 rpoisson(const C_FLOAT64 mu);
306 
307  static C_FLOAT64 max(const C_FLOAT64 & x1,
308  const C_FLOAT64 & x2);
309 
310  static C_FLOAT64 min(const C_FLOAT64 & x1,
311  const C_FLOAT64 & x2);
312 
313  // Attributes
314 private:
316 
318  const C_FLOAT64 & arg2);
319 
321  const C_FLOAT64 & arg2,
322  const C_FLOAT64 & arg3,
323  const C_FLOAT64 & arg4);
324 
329 
330  static CRandom * mpRandom;
331 };
332 
333 #endif // COPASI_CEvaluationNodeFunction
static C_FLOAT64 factorial(C_FLOAT64 value)
static C_FLOAT64 sech(C_FLOAT64 value)
static C_FLOAT64 min(const C_FLOAT64 &x1, const C_FLOAT64 &x2)
static C_FLOAT64 coth(C_FLOAT64 value)
std::string handleNot(const std::string &str) const
static C_FLOAT64 acsch(C_FLOAT64 value)
static C_FLOAT64 arccot(C_FLOAT64 value)
static C_FLOAT64 plus(C_FLOAT64 value)
C_FLOAT64(* mpFunction4)(const C_FLOAT64 &arg1, const C_FLOAT64 &arg2, const C_FLOAT64 &arg3, const C_FLOAT64 &arg4)
static C_FLOAT64 arcsec(C_FLOAT64 value)
const C_FLOAT64 & getValue() const
static C_FLOAT64 rnormal(const C_FLOAT64 &mean, const C_FLOAT64 &sd)
virtual bool compile(const CEvaluationTree *pTree)
virtual std::string getBerkeleyMadonnaString(const std::vector< std::string > &children) const
static CEvaluationNode * fromAST(const ASTNode *pASTNode, const std::vector< CEvaluationNode * > &children)
C_FLOAT64(* mpFunction2)(const C_FLOAT64 &arg1, const C_FLOAT64 &arg2)
static C_FLOAT64 csch(C_FLOAT64 value)
virtual std::string getDisplayString(const std::vector< std::string > &children) const
static C_FLOAT64 copasiNot(C_FLOAT64 value)
virtual std::string getCCodeString(const std::vector< std::string > &children) const
virtual std::string getXPPString(const std::vector< std::string > &children) const
static C_FLOAT64 asech(C_FLOAT64 value)
C_FLOAT64(* mpFunction)(C_FLOAT64 arg1)
static C_FLOAT64 sec(C_FLOAT64 value)
virtual CEvaluationNode * simplifyNode(const std::vector< CEvaluationNode * > &children) const
static C_FLOAT64 runiform(const C_FLOAT64 &lowerBound, const C_FLOAT64 &upperBound)
static C_FLOAT64 csc(C_FLOAT64 value)
static C_FLOAT64 rgamma(const C_FLOAT64 &shape, const C_FLOAT64 &scale)
static C_FLOAT64 rpoisson(const C_FLOAT64 mu)
static C_FLOAT64 arccsc(C_FLOAT64 value)
virtual ASTNode * toAST(const CCopasiDataModel *pDataModel) const
static Type subType(const Type &type)
static C_FLOAT64 max(const C_FLOAT64 &x1, const C_FLOAT64 &x2)
#define C_FLOAT64
Definition: copasi.h:92
static C_FLOAT64 cot(C_FLOAT64 value)
static C_FLOAT64 acoth(C_FLOAT64 value)
virtual std::string getInfix(const std::vector< std::string > &children) const
virtual std::string getMMLString(const std::vector< std::string > &children, bool expand, const std::vector< std::vector< std::string > > &variables) const
static C_FLOAT64 minus(C_FLOAT64 value)
std::string handleSign(const std::string &str) const