19 #include "sbml/math/ASTNode.h"
33 switch (
mType & 0x00FFFFFF)
76 if (
mpLeft == NULL)
return false;
80 if (
mpRight == NULL)
return false;
88 if (const_cast<CEvaluationNodeOperator *>(
this)->
compile(NULL))
93 Infix =
"(" + children[0] +
")";
100 Infix +=
"(" + children[1] +
")";
102 Infix += children[1];
113 if (const_cast<CEvaluationNodeOperator *>(
this)->
compile(NULL))
118 DisplayString =
"(" + children[0] +
")";
120 DisplayString = children[0];
122 DisplayString +=
mData;
125 DisplayString +=
"(" + children[1] +
")";
127 DisplayString += children[1];
129 return DisplayString;
138 if (const_cast<CEvaluationNodeOperator *>(
this)->
compile(NULL))
143 if (subType ==
POWER)
144 DisplayString =
"pow(";
147 DisplayString =
"(int)";
150 DisplayString +=
"(" + children[0] +
")";
152 DisplayString += children[0];
157 DisplayString +=
",";
161 DisplayString +=
"%(int)";
165 DisplayString +=
mData;
170 DisplayString +=
"(" + children[1] +
")";
172 DisplayString += children[1];
174 if (subType ==
POWER)
175 DisplayString +=
")";
177 return DisplayString;
186 if (const_cast<CEvaluationNodeOperator *>(
this)->
compile(NULL))
188 std::string mdata =
"";
198 DisplayString =
"(" + children[0] +
")";
200 DisplayString = children[0];
202 DisplayString += mdata;
205 DisplayString +=
"(" + children[1] +
")";
207 DisplayString += children[1];
209 return DisplayString;
218 if (const_cast<CEvaluationNodeOperator *>(
this)->
compile(NULL))
224 DisplayString =
"mod(";
227 DisplayString +=
"(" + children[0] +
")";
229 DisplayString += children[0];
234 DisplayString +=
",";
238 DisplayString +=
mData;
243 DisplayString +=
"(" + children[1] +
")";
245 DisplayString += children[1];
248 DisplayString +=
")";
250 return DisplayString;
259 assert(pASTNode->getNumChildren() == children.size());
261 size_t i = 0, iMax = children.size();
263 int type = (int)pASTNode->getType();
266 std::string data =
"";
291 case AST_FUNCTION_POWER:
305 if (type == AST_MINUS)
328 else if (type == AST_DIVIDE || type == AST_POWER || type == AST_FUNCTION_POWER)
330 switch (pASTNode->getNumChildren())
345 else if (type == AST_PLUS || type == AST_TIMES)
351 if (type == AST_PLUS)
407 ASTNode* node =
new ASTNode();
412 node->setType(AST_POWER);
416 node->setType(AST_TIMES);
420 node->setType(AST_DIVIDE);
429 node->setType(AST_PLUS);
433 node->setType(AST_MINUS);
446 node->addChild(child2->
toAST(pDataModel));
464 assert(children.size() > 0);
468 if (children.size() > 1)
470 child2 = children[1];
482 std::stringstream tmp;
484 tmp << pow(child1->getValue(), child2->
getValue());
491 if (fabs(child2->
getValue() - 1.0) < 1.0E-100)
498 if (fabs(child2->
getValue()) < 1.0E-100)
514 std::stringstream tmp;
519 newnode->
addChild(newchild2, newchild1);
520 newchild2->
addChild(grandchild1, NULL);
521 newchild2->
addChild(grandchild2, grandchild1);
535 exp = (
int) floor(child2->
getValue());
537 exp = (int) floor(child2->
getValue()) + 1;
541 newpower->
addChild(newchild1, NULL);
542 newpower->
addChild(child2, newchild1);
545 if (pow(-1.0, exp) == 1.0)
554 if (child1->getData() ==
"^")
563 newnode->
addChild(newchild2, newchild1);
564 newchild2->
addChild(grandchild1, NULL);
565 newchild2->
addChild(grandchild2, grandchild1);
589 if (child1->getData() ==
"*")
600 newnode->
addChild(newchild2, newchild1);
601 newchild1->
addChild(grandchild1, NULL);
602 newchild1->
addChild(grandchild2, grandchild1);
603 newchild2->
addChild(grandchild3, NULL);
604 newchild2->
addChild(grandchild4, grandchild3);
609 if (child1->getData() ==
"/")
620 newnode->
addChild(newchild2, newchild1);
621 newchild1->
addChild(grandchild1, NULL);
622 newchild1->
addChild(grandchild2, grandchild1);
623 newchild2->
addChild(grandchild3, NULL);
624 newchild2->
addChild(grandchild4, grandchild3);
680 std::stringstream tmp;
682 tmp << child1->getValue() * child2->
getValue();
689 if (fabs(child1->getValue()) < 1.0E-100)
698 if (fabs(child1->getValue() - 1.0) < 1.0E-100)
705 if (child1->getValue() < 0.0)
712 std::stringstream tmp;
714 tmp << fabs(child1->getValue());
718 newchild1->
addChild(grandchild1, NULL);
719 newchild1->
addChild(grandchild2, grandchild1);
728 if (fabs(child2->
getValue()) < 1.0E-100)
737 if (fabs(child2->
getValue() - 1.0) < 1.0E-100)
749 std::stringstream tmp;
755 newchild1->
addChild(grandchild1, NULL);
756 newchild1->
addChild(grandchild2, grandchild1);
762 if (child1->getData() ==
"/")
776 newnode->
addChild(newchild2, newchild1);
777 newchild1->
addChild(grandchild1, NULL);
778 newchild1->
addChild(grandchild2, grandchild1);
779 newchild2->
addChild(grandchild3, NULL);
780 newchild2->
addChild(grandchild4, grandchild3);
793 newnode->
addChild(newchild2, newchild1);
794 newchild1->
addChild(grandchild1, NULL);
795 newchild1->
addChild(grandchild2, grandchild1);
809 newnode->
addChild(newchild2, newchild1);
810 newchild1->
addChild(grandchild1, NULL);
811 newchild1->
addChild(grandchild2, grandchild1);
825 newchild1->
addChild(grandchild1, NULL);
826 newchild1->
addChild(grandchild2, grandchild1);
841 newchild1->
addChild(grandchild1, NULL);
842 newchild1->
addChild(grandchild2, grandchild1);
847 if (child1->getData() ==
"+")
858 newnode->
addChild(newchild2, newchild1);
859 newchild1->
addChild(grandchild1, NULL);
860 newchild1->
addChild(grandchild2, grandchild1);
861 newchild2->
addChild(grandchild3, NULL);
862 newchild2->
addChild(grandchild4, grandchild3);
878 newnode->
addChild(newchild2, newchild1);
879 newchild1->
addChild(grandchild1, NULL);
880 newchild1->
addChild(grandchild2, grandchild1);
881 newchild2->
addChild(grandchild3, NULL);
882 newchild2->
addChild(grandchild4, grandchild3);
902 std::vector<CEvaluationNode*> children;
903 children.push_back(grandchild1);
904 children.push_back(grandchild2);
910 children.push_back(dynamic_cast<CEvaluationNode*>(newNode->
getChild())->
copyBranch());
911 children.push_back(dynamic_cast<CEvaluationNode*>(newNode->
getChild()->getSibling())->
copyBranch());
934 std::vector<CEvaluationNode*> children;
935 children.push_back(grandchild1);
936 children.push_back(grandchild2);
942 children.push_back(dynamic_cast<CEvaluationNode*>(newNode->
getChild())->
copyBranch());
943 children.push_back(dynamic_cast<CEvaluationNode*>(newNode->
getChild()->getSibling())->
copyBranch());
966 std::vector<CEvaluationNode*> children;
967 children.push_back(grandchild1);
968 children.push_back(grandchild2);
973 children.push_back(dynamic_cast<CEvaluationNode*>(newNode->
getChild())->
copyBranch());
974 children.push_back(dynamic_cast<CEvaluationNode*>(newNode->
getChild()->getSibling())->
copyBranch());
996 std::stringstream tmp;
998 tmp << child1->getValue() / child2->
getValue();
1005 if (fabs(child1->getValue()) < 1.0E-100)
1018 if (fabs(child2->
getValue()) < 1.0E-100)
1021 if (fabs(child2->
getValue() - 1.0) < 1.0E-100)
1029 if (child1->buildInfix() == child2->
buildInfix())
1038 if (child1->getData() ==
"/")
1051 newnode->
addChild(newchild1, NULL);
1052 newnode->
addChild(newchild2, newchild1);
1053 newchild1->
addChild(grandchild1, NULL);
1054 newchild1->
addChild(grandchild2, grandchild1);
1055 newchild2->
addChild(grandchild3, NULL);
1056 newchild2->
addChild(grandchild4, grandchild3);
1068 newnode->
addChild(newchild1, NULL);
1069 newnode->
addChild(newchild2, newchild1);
1070 newchild2->
addChild(grandchild1, NULL);
1071 newchild2->
addChild(grandchild2, grandchild1);
1084 newnode->
addChild(newchild1, NULL);
1085 newnode->
addChild(newchild2, newchild1);
1086 newchild1->
addChild(grandchild1, NULL);
1087 newchild1->
addChild(grandchild2, grandchild1);
1107 std::vector<CEvaluationNode*> children;
1108 children.push_back(grandchild1);
1109 children.push_back(grandchild2);
1115 children.push_back(dynamic_cast<CEvaluationNode*>(newNode->
getChild())->
copyBranch());
1116 children.push_back(dynamic_cast<CEvaluationNode*>(newNode->
getChild()->getSibling())->
copyBranch());
1139 std::vector<CEvaluationNode*> children;
1140 children.push_back(grandchild1);
1141 children.push_back(grandchild2);
1147 children.push_back(dynamic_cast<CEvaluationNode*>(newNode->
getChild())->
copyBranch());
1148 children.push_back(dynamic_cast<CEvaluationNode*>(newNode->
getChild()->getSibling())->
copyBranch());
1171 std::vector<CEvaluationNode*> children;
1172 children.push_back(grandchild1);
1173 children.push_back(grandchild2);
1178 children.push_back(dynamic_cast<CEvaluationNode*>(newNode->
getChild())->
copyBranch());
1179 children.push_back(dynamic_cast<CEvaluationNode*>(newNode->
getChild()->getSibling())->
copyBranch());
1201 std::stringstream tmp;
1203 tmp << child1->getValue() + child2->
getValue();
1210 if (fabs(child1->getValue()) < 1.0E-100)
1229 && (dynamic_cast<CEvaluationNode*>(child2->
getChild())->
buildInfix() == child1->buildInfix())))
1250 std::stringstream tmp;
1252 tmp << child1->getValue() - child2->
getValue();
1259 if (fabs(child1->getValue()) < 1.0E-100)
1276 if (child1->buildInfix() == child2->
buildInfix())
1289 newnode->
addChild(newchild2, child1);
1304 bool result =
false;
1320 pASTNode->setType(AST_FUNCTION_PIECEWISE);
1323 ASTNode* pASTNodeTrue =
new ASTNode();
1324 pASTNodeTrue->setType(AST_MINUS);
1325 ASTNode* tmpASTNode =
new ASTNode(AST_DIVIDE);
1326 tmpASTNode->addChild(x->
toAST(pDataModel));
1327 tmpASTNode->addChild(y->
toAST(pDataModel));
1328 ASTNode* tmpASTNode2 =
new ASTNode(AST_FUNCTION_CEILING);
1329 tmpASTNode2->addChild(tmpASTNode);
1330 tmpASTNode =
new ASTNode(AST_TIMES);
1331 tmpASTNode->addChild(y->
toAST(pDataModel));
1332 tmpASTNode->addChild(tmpASTNode2);
1333 pASTNodeTrue->addChild(x->
toAST(pDataModel));
1334 pASTNodeTrue->addChild(tmpASTNode);
1335 pASTNode->addChild(pASTNodeTrue);
1341 ASTNode* pASTNodeCondition =
new ASTNode();
1342 pASTNodeCondition->setType(AST_LOGICAL_XOR);
1345 tmpASTNode =
new ASTNode(AST_RELATIONAL_LT);
1347 tmpASTNode->addChild(x->
toAST(pDataModel));
1349 tmpASTNode2 =
new ASTNode(AST_INTEGER);
1350 tmpASTNode2->setValue(0);
1351 tmpASTNode->addChild(tmpASTNode2);
1352 pASTNodeCondition->addChild(tmpASTNode);
1355 tmpASTNode =
new ASTNode(AST_RELATIONAL_LT);
1357 tmpASTNode->addChild(y->
toAST(pDataModel));
1359 tmpASTNode2 =
new ASTNode(AST_INTEGER);
1360 tmpASTNode2->setValue(0);
1361 tmpASTNode->addChild(tmpASTNode2);
1362 pASTNodeCondition->addChild(tmpASTNode);
1363 pASTNode->addChild(pASTNodeCondition);
1366 ASTNode* pASTNodeFalse =
new ASTNode();
1367 pASTNodeFalse->setType(AST_MINUS);
1368 tmpASTNode =
new ASTNode(AST_DIVIDE);
1369 tmpASTNode->addChild(x->
toAST(pDataModel));
1370 tmpASTNode->addChild(y->
toAST(pDataModel));
1371 tmpASTNode2 =
new ASTNode(AST_FUNCTION_FLOOR);
1372 tmpASTNode2->addChild(tmpASTNode);
1373 tmpASTNode =
new ASTNode(AST_TIMES);
1374 tmpASTNode->addChild(y->
toAST(pDataModel));
1375 tmpASTNode->addChild(tmpASTNode2);
1376 pASTNodeFalse->addChild(x->
toAST(pDataModel));
1377 pASTNodeFalse->addChild(tmpASTNode);
1378 pASTNode->addChild(pASTNodeFalse);
1401 const std::vector< std::vector< std::string > > & )
const
1403 std::ostringstream out;
1407 switch (
mType & 0x00FFFFFF)
1411 out <<
"<mrow>" << std::endl;
1413 out <<
"<mo>" <<
"+" <<
"</mo>" << std::endl;
1415 out <<
"</mrow>" << std::endl;
1419 out <<
"<mrow>" << std::endl;
1421 out <<
"<mo>" <<
"-" <<
"</mo>" << std::endl;
1428 if (flag) out <<
"<mfenced>" << std::endl;
1432 if (flag) out <<
"</mfenced>" << std::endl;
1434 out <<
"</mrow>" << std::endl;
1438 out <<
"<mrow>" << std::endl;
1446 if (flag) out <<
"<mfenced>" << std::endl;
1450 if (flag) out <<
"</mfenced>" << std::endl;
1452 out <<
"<mo>" <<
"·" <<
"</mo>" << std::endl;
1459 if (flag) out <<
"<mfenced>" << std::endl;
1463 if (flag) out <<
"</mfenced>" << std::endl;
1465 out <<
"</mrow>" << std::endl;
1470 out <<
"<mfrac>" << std::endl;
1480 out <<
"</mfrac>" << std::endl;
1484 out <<
"<msup>" << std::endl;
1495 if (flag) out <<
"<mfenced>" << std::endl;
1499 if (flag) out <<
"</mfenced>" << std::endl;
1501 out <<
"<mrow>" << std::endl;
1503 out <<
"</mrow>" << std::endl;
1505 out <<
"</msup>" << std::endl;
1509 out <<
"<mrow>" << std::endl;
1514 if (flag) out <<
"<mfenced>" << std::endl;
1518 if (flag) out <<
"</mfenced>" << std::endl;
1520 out <<
"<mo>" <<
"%" <<
"</mo>" << std::endl;
1524 if (flag) out <<
"<mfenced>" << std::endl;
1528 if (flag) out <<
"</mfenced>" << std::endl;
1530 out <<
"</mrow>" << std::endl;
virtual std::string getDisplayString(const std::vector< std::string > &children) const
virtual std::string getBerkeleyMadonnaString(const std::vector< std::string > &children) const
virtual ASTNode * toAST(const CCopasiDataModel *pDataModel) const
CEvaluationNode * copyBranch() const
CCopasiNode< Data > * getSibling()
bool createModuloTree(const CEvaluationNodeOperator *pNode, ASTNode *pASTNode, const CCopasiDataModel *pDataModel) const
virtual std::string getMMLString(const std::vector< std::string > &children, bool expand, const std::vector< std::vector< std::string > > &variables) const
CEvaluationNode * copyNode(CEvaluationNode *child1, CEvaluationNode *child2) const
virtual bool compile(const CEvaluationTree *pTree)
const C_FLOAT64 & getValue() const
CEvaluationNode * getRight()
const Type & getType() const
virtual ASTNode * toAST(const CCopasiDataModel *pDataModel) const
static CEvaluationNode * create(const Type &type, const Data &data)
std::string buildInfix() const
CEvaluationNodeOperator()
virtual std::string getXPPString(const std::vector< std::string > &children) const
#define PRECEDENCE_OPERATOR_MODULUS
#define PRECEDENCE_OPERATOR_PLUS
static Type type(const Type &type)
CEvaluationNode * mpRight
virtual std::string getCCodeString(const std::vector< std::string > &children) const
#define PRECEDENCE_OPERATOR_DIVIDE
virtual bool addChild(CCopasiNode< Data > *pChild, CCopasiNode< Data > *pAfter=NULL)
virtual CEvaluationNode * simplifyNode(const std::vector< CEvaluationNode * > &children) const
static Type subType(const Type &type)
#define PRECEDENCE_OPERATOR_POWER
#define PRECEDENCE_OPERATOR_MINUS
CEvaluationNode * getLeft()
class CEvaluationNode::CPrecedence mPrecedence
virtual std::string getInfix(const std::vector< std::string > &children) const
virtual ~CEvaluationNodeOperator()
#define PRECEDENCE_OPERATOR_MULTIPLY
virtual CEvaluationNode * simplifyNode(const std::vector< CEvaluationNode * > &children) const
virtual const Data & getData() const
CCopasiNode< Data > * getChild()
static CEvaluationNode * fromAST(const ASTNode *pASTNode, const std::vector< CEvaluationNode * > &children)