COPASI API  4.16.103
CEvaluationNodeNumber.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2014 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 #include <iomanip>
16 #include <sstream>
17 #include <stdlib.h>
18 #include <cmath>
19 
20 #include "copasi.h"
21 #include "CEvaluationNode.h"
22 #include "utilities/utility.h"
23 
24 #include "sbml/math/ASTNode.h"
25 
27  CEvaluationNode(CEvaluationNode::INVALID, "")
29 
31  const Data & data):
32  CEvaluationNode((Type)(CEvaluationNode::NUMBER | subType), data)
33 {
34  const char * end;
35  const char * str = mData.c_str();
36 
37  switch (subType)
38  {
39  case DOUBLE:
40  case INTEGER:
41  case ENOTATION:
42  {
43  //mValue = strToDouble(str, NULL);
44  std::istringstream in;
45  in.imbue(std::locale::classic());
46  in.str(str);
47  in >> mValue;
48  }
49  break;
50 
51  case RATIONALE:
52  str++; // Skip the '('
53  mValue = strToDouble(str, &end);
54  end++; // Skip the '/'
55  mValue /= strToDouble(end, NULL);
56  break;
57 
58  case INVALID:
59  fatalError();
60  break;
61  }
62 
64 }
65 
68 {
69  std::ostringstream Data;
70 
71  Data.imbue(std::locale::classic());
72  Data.precision(16);
73  Data << number;
74 
75  mData = Data.str();
77 }
78 
80  CEvaluationNode(src)
81 {}
82 
84 
85 // static
86 CEvaluationNode * CEvaluationNodeNumber::fromAST(const ASTNode * pASTNode, const std::vector< CEvaluationNode * > & children)
87 {
88  assert(pASTNode->getNumChildren() == children.size());
89 
90  std::stringstream ss;
92  std::string data = "";
93  CEvaluationNode* pNode = NULL;
94 
95  switch (pASTNode->getType())
96  {
97  case AST_INTEGER:
98  subType = INTEGER;
99 
100  if (pASTNode->getInteger() < 0)
101  {
103 
104  ss << abs(pASTNode->getInteger());
105  data = ss.str();
106  pNode->addChild(new CEvaluationNodeNumber(subType, data));
107  }
108  else
109  {
110  ss << pASTNode->getInteger();
111  data = ss.str();
112  pNode = new CEvaluationNodeNumber(subType, data);
113  }
114 
115  break;
116 
117  case AST_REAL:
118  subType = DOUBLE;
119 
120  if (pASTNode->getReal() == (std::numeric_limits<C_FLOAT64>::infinity()))
121  {
123  }
124  else if (pASTNode->getReal() == (-std::numeric_limits<C_FLOAT64>::infinity()))
125  {
128  }
129  else if (isnan(pASTNode->getReal()))
130  {
132  }
133  else if (pASTNode->getReal() < 0.0)
134  {
136 
137  ss << fabs(pASTNode->getReal());
138  data = ss.str();
139  pNode->addChild(new CEvaluationNodeNumber(subType, data));
140  }
141  else
142  {
143  ss << pASTNode->getReal();
144  data = ss.str();
145  pNode = new CEvaluationNodeNumber(subType, data);
146  }
147 
148  break;
149 
150  case AST_REAL_E:
151  subType = ENOTATION;
152 
153  if (pASTNode->getReal() == (std::numeric_limits<C_FLOAT64>::infinity()))
154  {
156  }
157  else if (pASTNode->getReal() == (-std::numeric_limits<C_FLOAT64>::infinity()))
158  {
161  }
162  else if (isnan(pASTNode->getReal()))
163  {
165  }
166  else if (pASTNode->getReal() < 0.0)
167  {
169 
170  ss << fabs(pASTNode->getReal());
171  data = ss.str();
172  pNode->addChild(new CEvaluationNodeNumber(subType, data));
173  }
174  else
175  {
176  ss << pASTNode->getReal();
177  data = ss.str();
178  pNode = new CEvaluationNodeNumber(subType, data);
179  }
180 
181  break;
182 
183  case AST_RATIONAL:
184  subType = RATIONALE;
185 
186  if (pASTNode->getReal() < 0.0) // getReal returns the value of the node
187  {
189 
190  ss << "(" << abs(pASTNode->getNumerator()) << "/" << abs(pASTNode->getDenominator()) << ")";
191  data = ss.str();
192  pNode->addChild(new CEvaluationNodeNumber(subType, data));
193  }
194  else
195  {
196  ss << "(" << pASTNode->getNumerator() << "/" << pASTNode->getDenominator() << ")";
197  data = ss.str();
198  pNode = new CEvaluationNodeNumber(subType, data);
199  }
200 
201  break;
202 
203  default:
204  subType = INVALID;
205  break;
206  }
207 
208  return pNode;
209 }
210 
211 ASTNode* CEvaluationNodeNumber::toAST(const CCopasiDataModel* /* pDataModel */) const
212 {
214  ASTNode* node = new ASTNode();
215  double num1;
216  double num2;
217  const char * end;
218  const char * str = mData.c_str();
219 
220  switch (subType)
221  {
222  case DOUBLE:
223  node->setType(AST_REAL);
224  node->setValue(this->getValue());
225  break;
226 
227  case INTEGER:
228  node->setType(AST_INTEGER);
229  node->setValue((long)this->getValue());
230  break;
231 
232  case ENOTATION:
233  node->setType(AST_REAL_E);
234  num2 = floor(log10(this->getValue()));
235  num1 = pow(10.0, log10(this->getValue()) - num2);
236  node->setValue(num1, (long)num2);
237  break;
238 
239  case RATIONALE:
240  node->setType(AST_RATIONAL);
241  str++; // Skip the '('
242  num1 = strToDouble(str, &end);
243  end++; // Skip the '/'
244  num2 = strToDouble(end, NULL);
245  node->setValue((long)num1, (long)num2);
246  break;
247 
248  case INVALID:
249  break;
250  }
251 
252  return node;
253 }
254 
255 #include "utilities/copasimathml.h"
256 
257 // virtual
258 std::string CEvaluationNodeNumber::getMMLString(const std::vector< std::string > & /* children */,
259  bool /* expand */,
260  const std::vector< std::vector< std::string > > & /* variables */) const
261 {
262  std::ostringstream out;
263 
264  //or use mValue instead?
265  out << "<mn>" << mData << "</mn>" << std::endl;
266 
267  return out.str();
268 }
269 
270 std::string CEvaluationNodeNumber::getCCodeString(const std::vector< std::string > & /*children*/) const
271 {
272  std::ostringstream out;
273  out << std::setprecision(std::numeric_limits<double>::digits10 + 2) << std::fixed << getValue();
274  return out.str();
275 }
static CEvaluationNode * fromAST(const ASTNode *pASTNode, const std::vector< CEvaluationNode * > &children)
const C_FLOAT64 & getValue() const
#define fatalError()
const Type & getType() const
virtual bool addChild(CCopasiNode< Data > *pChild, CCopasiNode< Data > *pAfter=NULL)
Definition: CCopasiNode.h:156
virtual std::string getCCodeString(const std::vector< std::string > &children) const
static Type subType(const Type &type)
class CEvaluationNode::CPrecedence mPrecedence
double strToDouble(const char *str, char const **pTail)
Definition: utility.cpp:325
#define C_FLOAT64
Definition: copasi.h:92
virtual std::string getMMLString(const std::vector< std::string > &children, bool expand, const std::vector< std::vector< std::string > > &variables) const
ASTNode * toAST(const CCopasiDataModel *pDataModel) const
#define abs(x)
Definition: f2c.h:173
#define PRECEDENCE_NUMBER