24 # pragma warning (disable: 4786)
25 # pragma warning (disable: 4243)
27 # pragma warning (disable: 4355)
73 std::vector<CEvaluationNode*> children;
79 children.push_back(newchild);
137 std::stringstream tmp;
168 std::stringstream tmp;
172 assert(pTmpNode != NULL);
174 assert(pFraction != NULL);
201 std::vector<CEvaluationNode*> children;
204 while (pChild != NULL)
207 children.push_back(pNewChild);
213 assert(children.size() == 2);
214 std::vector<const CEvaluationNode*> summands;
218 std::vector<CEvaluationNode*> numeratorNodes;
219 std::vector<CEvaluationNode*> denominatorNodes;
220 std::vector<const CEvaluationNode*>::iterator it = summands.begin(), endit = summands.end();
225 pPowerNode->
addChild(children[0]->copyBranch());
229 pPowerNode->
addChild(dynamic_cast<const CEvaluationNode*>((*it)->getChild())->copyBranch());
230 denominatorNodes.push_back(pPowerNode);
234 std::ostringstream os;
236 os << fabs(dynamic_cast<const CEvaluationNodeNumber*>(*it)->getValue());
238 denominatorNodes.push_back(pPowerNode);
242 pPowerNode->
addChild((*it)->copyBranch());
243 numeratorNodes.push_back(pPowerNode);
253 if (numeratorNodes.empty())
262 assert(pResult != NULL);
266 if (!denominatorNodes.empty())
278 std::vector<CEvaluationNode*>::iterator it = children.begin(), endit = children.end();
302 if (nodes.size() == 0)
306 else if (nodes.size() == 1)
308 pResult = nodes[0]->copyBranch();
313 std::vector<const CEvaluationNode*>::const_reverse_iterator it = nodes.rbegin(), endit = nodes.rend();
326 pOperator->
addChild((*it)->copyBranch());
352 assert(pChild1 != NULL);
357 assert(pChild2 != NULL);
369 summands.push_back(pChild1);
378 summands.push_back(pChild2);
385 summands.push_back(pRoot);
415 if (pTmp != pResult)
delete pResult;
503 bool finished =
false;
507 std::string infix2 = infix;
509 unsigned int counter = 0;
629 while (pChild != NULL)
632 assert(pNewChild != NULL);
634 if (pNewChild != pChild)
640 pOrig->
addChild(pNewChild, pLastChild);
714 assert(pChild != NULL);
720 assert(pChild != NULL);
725 std::ostringstream os;
748 while (pChild != NULL)
793 assert(pChild1 != NULL);
795 assert(pChild2 != NULL);
802 assert(pNumberNode != NULL);
803 double value = pNumberNode->
getValue();
805 if (fabs(value) <
ZERO)
815 double value = pNumberNode2->
getValue();
819 if (fabs(value) <
ZERO || value < 0.0)
826 if (pResult == pPowerNode)
831 else if (fabs(value - 1.0) <
ZERO)
869 assert(pNumberNode2 != NULL);
870 double value = pNumberNode2->
getValue();
873 if (fabs(value) <
ZERO)
878 else if (value > 0.0)
907 assert(pNumberNode2 != NULL);
908 double value = pNumberNode2->
getValue();
911 if (fabs(value) <
ZERO)
916 else if (value > 0.0)
938 assert(pNumberNode2 != NULL);
939 double value = pNumberNode2->
getValue();
942 if (fabs(value) <
ZERO)
946 else if (fabs(value - 1.0) <
ZERO)
993 assert(pChild1 != NULL);
995 assert(pChild2 != NULL);
1019 assert(pNumberNode != NULL);
1020 double value = pNumberNode->
getValue();
1022 if (fabs(value) <
ZERO)
1026 else if (fabs(value - 1.0) <
ZERO)
1066 assert(pChild1 != NULL);
1068 assert(pChild2 != NULL);
1081 fabs(dynamic_cast<const CEvaluationNodeNumber*>(pChild1)->getValue()) <
ZERO) ||
1083 fabs(dynamic_cast<const CEvaluationNodeNumber*>(pChild2)->getValue()) <
ZERO))
1089 fabs(dynamic_cast<const CEvaluationNodeNumber*>(pChild1)->getValue() - 1.0) <
ZERO))
1094 fabs(dynamic_cast<const CEvaluationNodeNumber*>(pChild2)->getValue() - 1.0) <
ZERO))
1122 assert(pChild1 != NULL);
1124 assert(pChild2 != NULL);
1139 fabs(dynamic_cast<const CEvaluationNodeNumber*>(pChild2)->getValue()) <
ZERO))
1145 fabs(dynamic_cast<const CEvaluationNodeNumber*>(pChild1)->getValue()) <
ZERO))
1150 else if (base1->toString() == base2->
toString())
1156 fabs(dynamic_cast<const CEvaluationNodeNumber*>(pChild2)->getValue() - 1.0) <
ZERO))
1182 assert(pChild1 != NULL);
1184 assert(pChild2 != NULL);
1197 fabs(dynamic_cast<const CEvaluationNodeNumber*>(pChild2)->getValue()) <
ZERO))
1203 fabs(dynamic_cast<const CEvaluationNodeNumber*>(pChild1)->getValue()) <
ZERO))
1228 assert(pChild1 != NULL);
1230 assert(pChild2 != NULL);
1245 else if (base1->toString() == base2->
toString())
1251 fabs(dynamic_cast<const CEvaluationNodeNumber*>(pChild2)->getValue()) <
ZERO))
1257 fabs(dynamic_cast<const CEvaluationNodeNumber*>(pChild1)->getValue()) <
ZERO))
1762 bool modified =
false;
1765 assert(pResult != NULL);
1778 assert(pChild1 != NULL);
1780 assert(pChild2 != NULL);
1789 std::ostringstream os;
1791 assert(pNumberNode1 != NULL);
1793 assert(pNumberNode1 != NULL);
1801 dfi->
getParent()->addChild(pTmpOrig, *dfi);
1809 assert(*dfi == pResult);
1824 std::ostringstream os;
1826 assert(pNumberNode1 != NULL);
1828 assert(pNumberNode2 != NULL);
1836 dfi->
getParent()->addChild(pTmpOrig, *dfi);
1844 assert(*dfi == pResult);
1865 std::vector<const CEvaluationNode*> multiplications, divisions;
1869 std::set<CEvaluationNode*> multiplicationNumberNodes;
1870 unsigned int i, iMax = multiplications.size();
1873 for (i = 0; i < iMax; ++i)
1879 multiplicationNumberNodes.insert(pNode);
1883 std::set<CEvaluationNode*> divisionNumberNodes;
1884 iMax = divisions.size();
1886 for (i = 0; i < iMax; ++i)
1892 divisionNumberNodes.insert(pNode);
1896 if ((multiplicationNumberNodes.size() + divisionNumberNodes.size()) > 1)
1901 std::set<CEvaluationNode*>::iterator it = multiplicationNumberNodes.begin(), endit = multiplicationNumberNodes.end();
1905 value *= (*it)->getValue();
1909 it = divisionNumberNodes.begin();
1910 endit = divisionNumberNodes.end();
1914 value /= (*it)->getValue();
1918 std::vector<CEvaluationNode*> newMultiplications, newDivisions;
1920 if (fabs((value - 1.0)) >=
ZERO)
1922 std::ostringstream os;
1925 if (fabs(value) < 1.0)
1929 newDivisions.push_back(pEvaluated);
1935 newMultiplications.push_back(pEvaluated);
1939 std::vector<const CEvaluationNode*>::iterator it2 = multiplications.begin(), endit2 = multiplications.end();
1941 while (it2 != endit2)
1945 it = multiplicationNumberNodes.find(const_cast<CEvaluationNode*>(*it2));
1947 if (it == multiplicationNumberNodes.end())
1955 pTmpNode->
getParent()->removeChild(pTmpNode);
1958 newMultiplications.push_back(pTmpNode);
1969 it2 = divisions.begin();
1970 endit2 = divisions.end();
1972 while (it2 != endit2)
1976 it = divisionNumberNodes.find(const_cast<CEvaluationNode*>(*it2));
1978 if (it == divisionNumberNodes.end())
1985 pTmpNode->
getParent()->removeChild(pTmpNode);
1988 newDivisions.push_back(const_cast<CEvaluationNode*>(pTmpNode));
2001 if (newMultiplications.empty())
2010 if (!newDivisions.empty())
2014 pTmpOrig = pTmpNode;
2023 dfi->
getParent()->addChild(pTmpOrig, *dfi);
2031 assert(*dfi == pResult);
2052 std::vector<CEvaluationNode*> additions, subtractions;
2056 std::set<CEvaluationNode*> additionNumberNodes;
2057 unsigned int i, iMax = additions.size();
2060 for (i = 0; i < iMax; ++i)
2066 additionNumberNodes.insert(pNode);
2070 std::set<CEvaluationNode*> subtractionNumberNodes;
2071 iMax = subtractions.size();
2073 for (i = 0; i < iMax; ++i)
2079 subtractionNumberNodes.insert(pNode);
2083 if ((additionNumberNodes.size() + subtractionNumberNodes.size()) > 1)
2088 std::set<CEvaluationNode*>::const_iterator it = additionNumberNodes.begin(), endit = additionNumberNodes.end();
2092 value += (*it)->getValue();
2096 it = subtractionNumberNodes.begin();
2097 endit = subtractionNumberNodes.end();
2101 value -= (*it)->getValue();
2105 std::vector<CEvaluationNode*> newAdditions, newSubtractions;
2107 if (fabs(value) >=
ZERO)
2109 std::ostringstream os;
2116 newSubtractions.push_back(pEvaluated);
2122 newAdditions.push_back(pEvaluated);
2127 std::vector<CEvaluationNode*>::const_iterator it2 = additions.begin(), endit2 = additions.end();
2129 while (it2 != endit2)
2133 it = additionNumberNodes.find(*it2);
2135 if (it == additionNumberNodes.end())
2137 newAdditions.push_back(*it2);
2148 it2 = subtractions.begin();
2149 endit2 = subtractions.end();
2151 while (it2 != endit2)
2155 it = subtractionNumberNodes.find(*it2);
2157 if (it == subtractionNumberNodes.end())
2159 newSubtractions.push_back(*it2);
2172 if (newAdditions.empty())
2174 if (newSubtractions.empty())
2185 if (!newSubtractions.empty())
2187 if (newAdditions.empty())
2191 std::ostringstream os;
2193 os << -1.0 * newSubtractions[0]->getValue();
2196 delete newSubtractions[0];
2205 pTmpOrig = pTmpNode2;
2212 pTmpOrig = pTmpNode;
2222 dfi->
getParent()->addChild(pTmpOrig, *dfi);
2230 assert(*dfi == pResult);
2242 unsigned int i, iMax = additions.size();
2244 for (i = 0; i < iMax; ++i)
2246 delete additions[i];
2249 iMax = subtractions.size();
2251 for (i = 0; i < iMax; ++i)
2253 delete subtractions[i];
2272 if (modified ==
false)
2289 std::vector<CEvaluationNode*> children;
2292 bool childrenChanged =
false;
2295 while (pChild != NULL)
2299 if (pNewChild != NULL)
2301 childrenChanged =
true;
2304 children.push_back(pNewChild);
2308 if (childrenChanged ==
true)
2310 std::vector<CEvaluationNode*>::iterator it = children.begin(), endit = children.end();
2324 assert(pChild == NULL);
2325 pResult = pTmpOrig->
copyNode(children);
2334 assert(pChild1 != NULL);
2336 assert(pChild2 != NULL);
2344 assert(pChild != NULL);
2348 assert(pChild != NULL);
2351 if (pResult == NULL)
2357 assert(pResult == pTmpOrig);
2358 pResult->
removeChild(const_cast<CEvaluationNode*>(pChild2));
2359 pMult->
addChild(const_cast<CEvaluationNode*>(pChild2));
2380 assert(pChild1 != NULL);
2382 assert(pChild2 != NULL);
2395 if (division ==
false)
2397 multiplications.push_back(pChild1);
2401 divisions.push_back(pChild1);
2413 if (division ==
false)
2415 multiplications.push_back(pChild2);
2419 divisions.push_back(pChild2);
2433 if (division ==
false)
2435 multiplications.push_back(pChild1);
2439 divisions.push_back(pChild1);
2451 if (division ==
false)
2453 divisions.push_back(pChild2);
2457 multiplications.push_back(pChild2);
2464 multiplications.push_back(pRoot);
2482 assert(pChild1 != NULL);
2484 assert(pChild2 != NULL);
2499 additions.push_back(pChild1);
2503 subtractions.push_back(pChild1);
2517 additions.push_back(pChild2);
2521 subtractions.push_back(pChild2);
2537 additions.push_back(pChild1);
2541 subtractions.push_back(pChild1);
2555 subtractions.push_back(pChild2);
2559 additions.push_back(pChild2);
2566 additions.push_back(pRoot);
2575 if (pRoot == NULL)
return;
2577 orderList.push_back(pRoot);
2580 while (pChild != NULL)
2594 std::vector<const CEvaluationNode*> tmpAdditions, tmpSubtractions;
2596 std::set<const CEvaluationNode*> tmpAdditionsS, tmpSubtractionsS;
2597 tmpAdditionsS.insert(tmpAdditions.begin(), tmpAdditions.end());
2598 tmpSubtractionsS.insert(tmpSubtractions.begin(), tmpSubtractions.end());
2599 std::list<const CEvaluationNode*> orderList;
2615 std::list<const CEvaluationNode*>::const_iterator orderIt = orderList.begin(), orderEndit = orderList.end();
2617 bool isMinus =
false;
2619 while (orderIt != orderEndit)
2621 pCurrentNode = NULL;
2624 if (tmpAdditionsS.find(*orderIt) == tmpAdditionsS.end())
2626 if (tmpSubtractionsS.find(*orderIt) != tmpSubtractionsS.end())
2629 pCurrentNode = *orderIt;
2630 assert(pCurrentNode != NULL);
2635 pCurrentNode = *orderIt;
2636 assert(pCurrentNode != NULL);
2639 if (pCurrentNode != NULL)
2644 std::ostringstream os;
2646 os << pCurrentNode->
getValue() * -1.0;
2649 if (isMinus ==
true)
2651 additions.push_back(pTmpNumber);
2655 subtractions.push_back(pTmpNumber);
2664 if (fabs(dynamic_cast<const CEvaluationNodeNumber*>(pCurrentNode->
getChild())->getValue()) - 1.0 <
ZERO)
2667 assert(pTmpNode != NULL);
2669 if (isMinus ==
true)
2671 additions.push_back(pTmpNode);
2675 subtractions.push_back(pTmpNode);
2681 std::ostringstream os;
2683 os << static_cast<const CEvaluationNodeNumber*>(pCurrentNode->
getChild())->getValue() * -1.0;
2685 pTmp->
addChild(dynamic_cast<const CEvaluationNode*>(pCurrentNode->
getChild()->getSibling())->copyBranch());
2687 if (isMinus ==
true)
2689 additions.push_back(pTmp);
2693 subtractions.push_back(pTmp);
2699 if (fabs(dynamic_cast<const CEvaluationNodeNumber*>(pCurrentNode->
getChild()->getSibling())->getValue()) - 1.0 <
ZERO)
2702 assert(pTmpNode != NULL);
2704 if (isMinus ==
true)
2706 additions.push_back(pTmpNode);
2710 subtractions.push_back(pTmpNode);
2716 pTmp->
addChild(dynamic_cast<const CEvaluationNode*>(pCurrentNode->
getChild())->copyBranch());
2717 std::ostringstream os;
2719 os << static_cast<const CEvaluationNodeNumber*>(pCurrentNode->
getChild()->getSibling())->getValue() * -1.0;
2722 if (isMinus ==
true)
2724 additions.push_back(pTmp);
2728 subtractions.push_back(pTmp);
2734 if (isMinus ==
true)
2736 subtractions.push_back(pCurrentNode->
copyBranch());
2740 additions.push_back(pCurrentNode->
copyBranch());
2746 if (isMinus ==
true)
2748 subtractions.push_back(pCurrentNode->
copyBranch());
2752 additions.push_back(pCurrentNode->
copyBranch());
2769 std::vector<CEvaluationNode*> children;
2773 bool childrenChanged =
false;
2775 while (pChild != NULL)
2779 if (pNewChild != NULL)
2781 childrenChanged =
true;
2784 children.push_back(pNewChild);
2788 if (childrenChanged ==
true)
2791 std::vector<CEvaluationNode*>::iterator it = children.begin(), endit = children.end();
2804 pResult = pTmpOrig->
copyNode(children);
2813 assert(pChild1 != NULL);
2815 assert(pChild2 != NULL);
2818 std::vector<CEvaluationNode*> additions, subtractions;
2825 if (!additions.empty() || !subtractions.empty())
2830 unsigned int i, iMax = additions.size();
2832 for (i = 0; i < iMax; ++i)
2838 delete additions[i];
2848 additions[i] = pTmpNode;
2851 additions[i] = pTmpNode;
2854 iMax = subtractions.size();
2856 for (i = 0; i < iMax; ++i)
2863 delete subtractions[i];
2871 pTmpNode->
addChild(subtractions[i]);
2874 subtractions[i] = pTmpNode;
2881 if (additions.empty())
2883 pTmpResult = CNormalTranslation::ONE_NODE.
copyBranch();
2891 assert(pTmpResult != NULL);
2893 if (!subtractions.empty())
2897 pTmpResult = pTmpNode;
2899 assert(pTmpNode != NULL);
2901 subtractions.clear();
2904 if (pResult != NULL)
2909 pResult = pTmpResult;
2925 std::map<const CEvaluationNode*, product_match> matchMap;
2926 std::map<std::string, const CEvaluationNode*> infixMap;
2927 std::map<const CEvaluationNode*, product_match>::iterator matchPos;
2928 std::map<std::string, const CEvaluationNode*>::iterator infixPos;
2930 std::vector<const CEvaluationNode*>::const_iterator vit = multiplications.begin(), vendit = multiplications.end();
2933 std::string base2String;
2935 unsigned int index = 0;
2940 std::list<const CEvaluationNode*> orderList;
2942 while (vit != vendit)
2971 infixPos = infixMap.find(base2String);
2973 if (infixPos != infixMap.end())
2975 matchPos = matchMap.find(infixPos->second);
2976 assert(matchPos != matchMap.end());
2978 if (matchPos != matchMap.end())
2980 if (pExponent != NULL)
2982 if (matchPos->second.pExponentNode != NULL)
2985 pAddNode->addChild(matchPos->second.pExponentNode);
2986 pAddNode->addChild(pExponent);
2987 matchPos->second.pExponentNode = pAddNode;
2988 matchPos->second.addition_indices.insert(index);
2992 matchPos->second.pExponentNode = pExponent;
2997 matchPos->second.factor += 1.0;
3005 if (pExponent == NULL)
3018 infixMap.insert(std::pair<std::string, const CEvaluationNode*>(base2String, pBase));
3019 matchMap.insert(std::pair<const CEvaluationNode*, product_match>(pBase, match));
3020 orderList.push_back(pBase);
3027 vit = divisions.begin(), vendit = divisions.end();
3031 while (vit != vendit)
3061 infixPos = infixMap.find(base2String);
3063 if (infixPos != infixMap.end())
3065 matchPos = matchMap.find(infixPos->second);
3066 assert(matchPos != matchMap.end());
3068 if (matchPos != matchMap.end())
3070 if (pExponent != NULL)
3072 if (matchPos->second.pExponentNode != NULL)
3075 pAddNode->addChild(matchPos->second.pExponentNode);
3076 pAddNode->addChild(pExponent);
3077 matchPos->second.pExponentNode = pAddNode;
3078 matchPos->second.subtraction_indices.insert(index);
3085 pAddNode->addChild(pExponent);
3086 matchPos->second.pExponentNode = pAddNode;
3092 matchPos->second.factor -= 1.0;
3100 if (pExponent == NULL)
3111 pAddNode->addChild(pExponent);
3118 infixMap.insert(std::pair<std::string, const CEvaluationNode*>(base2String, pBase));
3119 matchMap.insert(std::pair<const CEvaluationNode*, product_match>(pBase, match));
3120 orderList.push_back(pBase);
3128 std::vector<product_match> result;
3130 std::ostringstream os;
3132 std::list<const CEvaluationNode*>::const_iterator orderIt = orderList.begin(), orderEndit = orderList.end();
3134 while (orderIt != orderEndit)
3136 matchPos = matchMap.find(*orderIt);
3137 assert(matchPos != matchMap.end());
3139 if (matchPos != matchMap.end())
3143 if (matchPos->second.pExponentNode != NULL)
3148 matchPos->second.factor += matchPos->second.pExponentNode->getValue();
3150 delete matchPos->second.pExponentNode;
3151 matchPos->second.pExponentNode = NULL;
3157 if (pTmpNode != NULL)
3162 matchPos->second.factor += pTmpNode->
getValue();
3164 delete matchPos->second.pExponentNode;
3165 matchPos->second.pExponentNode = NULL;
3170 delete matchPos->second.pExponentNode;
3171 matchPos->second.pExponentNode = pTmpNode;
3178 if (matchPos->second.factor != 0.0)
3182 os << matchPos->second.factor;
3186 pAddNode->
addChild(matchPos->second.pExponentNode);
3187 matchPos->second.pExponentNode = pAddNode;
3188 matchPos->second.factor = 0.0;
3195 result.push_back(matchPos->second);
3218 std::map<const CEvaluationNode*, summ_match> matchMap;
3219 std::map<const CEvaluationNode*, summ_match>::iterator matchPos;
3220 std::map<std::string, const CEvaluationNode*> infixMap;
3221 std::map<std::string, const CEvaluationNode*>::iterator infixPos;
3224 std::vector<const CEvaluationNode*>::const_iterator vit = additions.begin(), vendit = additions.end();
3225 const CEvaluationNode *pNode = NULL, *pChild1 = NULL, *pChild2 = NULL;
3226 double factor = 0.0;
3228 std::string base2String;
3229 unsigned int index = 0;
3233 std::list<const CEvaluationNode*> orderList;
3235 while (vit != vendit)
3243 assert(pChild1 != NULL);
3244 pChild2 =
dynamic_cast<const CEvaluationNode*
>(pChild1->getSibling());
3245 assert(pChild2 != NULL);
3246 assert(pChild2->getSibling() == NULL);
3275 infixPos = infixMap.find(base2String);
3277 if (infixPos != infixMap.end())
3279 matchPos = matchMap.find(infixPos->second);
3280 assert(matchPos != matchMap.end());
3282 if (matchPos != matchMap.end())
3284 matchPos->second.factor += factor;
3285 matchPos->second.addition_indices.insert(index);
3295 infixMap.insert(std::pair<std::string, const CEvaluationNode*>(base2String, pNode));
3296 matchMap.insert(std::pair<const CEvaluationNode*, summ_match>(pNode, match));
3297 orderList.push_back(pNode);
3306 vit = subtractions.begin(), vendit = subtractions.end();
3308 while (vit != vendit)
3316 assert(pChild1 != NULL);
3317 pChild2 =
dynamic_cast<const CEvaluationNode*
>(pChild1->getSibling());
3318 assert(pChild2 != NULL);
3319 assert(pChild2->getSibling() == NULL);
3348 infixPos = infixMap.find(base2String);
3350 if (infixPos != infixMap.end())
3352 matchPos = matchMap.find(infixPos->second);
3353 assert(matchPos != matchMap.end());
3355 if (matchPos != matchMap.end())
3357 matchPos->second.factor -= factor;
3358 matchPos->second.subtraction_indices.insert(index);
3368 infixMap.insert(std::pair<std::string, const CEvaluationNode*>(base2String, pNode));
3369 matchMap.insert(std::pair<const CEvaluationNode*, summ_match>(pNode, match));
3370 orderList.push_back(pNode);
3378 std::vector<summ_match> result;
3380 std::list<const CEvaluationNode*>::const_iterator orderIt = orderList.begin(), orderEndit = orderList.end();
3382 while (orderIt != orderEndit)
3384 matchPos = matchMap.find(*orderIt);
3385 assert(matchPos != matchMap.end());
3387 if (matchPos != matchMap.end())
3389 result.push_back(matchPos->second);
3587 std::vector<std::pair<CEvaluationNode*, CEvaluationNode*> >
CNormalTranslation::matchSummands(
const std::vector<CEvaluationNode*>& additions,
const std::vector<CEvaluationNode*>& subtractions)
3595 std::vector<std::pair<std::pair<const CEvaluationNode*, std::string>, std::vector<CEvaluationNode*> > > matchMap;
3597 std::vector<CEvaluationNode*>::const_iterator vit = additions.begin(), vendit = additions.end();
3599 while (vit != vendit)
3607 assert(pChild1 != NULL);
3609 assert(pChild2 != NULL);
3635 std::string base2String;
3649 std::vector<std::pair<std::pair<const CEvaluationNode*, std::string>, std::vector<CEvaluationNode*> > >::iterator mapIt = matchMap.begin(), mapEndit = matchMap.end();
3651 while (mapIt != mapEndit)
3653 if (mapIt->first.second == base2String)
3655 mapIt->second.push_back(pFactor);
3663 if (mapIt == mapEndit)
3665 std::vector<CEvaluationNode*> v;
3666 v.push_back(pFactor);
3667 matchMap.push_back(std::make_pair(std::pair<const CEvaluationNode*, std::string>(pNode, base2String), v));
3673 std::vector<std::pair<std::pair<const CEvaluationNode*, std::string>, std::vector<CEvaluationNode*> > > matchMap2;
3674 vit = subtractions.begin(), vendit = subtractions.end();
3676 while (vit != vendit)
3684 assert(pChild1 != NULL);
3686 assert(pChild2 != NULL);
3712 std::vector<std::pair<std::pair<const CEvaluationNode*, std::string>, std::vector<CEvaluationNode*> > >::iterator mapIt = matchMap2.begin(), mapEndit = matchMap2.end();
3713 std::string base2String;
3726 while (mapIt != mapEndit)
3728 if (mapIt->first.second == base2String)
3730 mapIt->second.push_back(pFactor);
3738 if (mapIt == mapEndit)
3740 std::vector<CEvaluationNode*> v;
3741 v.push_back(pFactor);
3742 matchMap2.push_back(std::make_pair(std::pair<const CEvaluationNode*, std::string>(pNode, base2String), v));
3749 std::vector<std::pair<CEvaluationNode*, std::pair<CEvaluationNode*, std::string> > > result;
3750 std::vector<std::pair<std::pair<const CEvaluationNode*, std::string>, std::vector<CEvaluationNode*> > >::iterator mapIt = matchMap.begin(), mapEndit = matchMap.end();
3752 while (mapIt != mapEndit)
3755 assert(pNode != NULL);
3756 result.push_back(std::make_pair(pNode, std::pair<CEvaluationNode*, std::string>(mapIt->first.first->copyBranch(), mapIt->first.second)));
3760 mapIt = matchMap2.begin(), mapEndit = matchMap2.end();
3762 while (mapIt != mapEndit)
3764 std::vector<CEvaluationNode*> constVect;
3765 constVect.insert(constVect.begin(), mapIt->second.begin(), mapIt->second.end());
3769 unsigned int i, iMax = result.size();
3771 for (i = 0; i < iMax; ++i)
3773 if (result[i].second.second == mapIt->first.second)
3776 pTmpNode->
addChild(result[i].first);
3778 result[i] = std::make_pair(pTmpNode, result[i].second);
3789 std::ostringstream os;
3791 os << dynamic_cast<const CEvaluationNodeNumber*>(pNode)->getValue() * -1.0;
3802 result.push_back(std::make_pair(pTmpNode, std::pair<CEvaluationNode*, std::string>(mapIt->first.first->copyBranch(), mapIt->first.second)));
3806 iMax = mapIt->second.size();
3808 for (i = 0; i < iMax; ++i)
3810 delete mapIt->second[i];
3816 std::vector<std::pair<CEvaluationNode*, CEvaluationNode*> > tmp;
3818 unsigned int i, iMax = result.size();
3823 for (i = 0; i < iMax; ++i)
3825 tmp.push_back(std::pair<CEvaluationNode*, CEvaluationNode*>(result[i].first, result[i].second.first));
3844 std::vector<const CEvaluationNode*> multiplications, divisions;
3846 unsigned int i, iMax = multiplications.size();
3849 for (i = 0; i < iMax; ++i)
3851 if (pResult == NULL)
3861 pResult = pTmpResult;
3865 if (!divisions.empty())
3868 iMax = divisions.size();
3870 for (i = 0; i < iMax; ++i)
3872 if (pDenominator == NULL)
3880 delete pDenominator;
3882 pDenominator = pTmpResult;
3890 pTmpResult->
addChild(pDenominator);
3891 pResult = pTmpResult;
3897 std::vector<CEvaluationNode*> children;
3899 while (pChild != NULL)
3908 assert(children.size() == 2);
3915 if (pResult == NULL)
3917 pResult = pOrig->
copyNode(children);
3931 std::vector<const CEvaluationNode*> additions1, subtractions1;
3933 std::vector<const CEvaluationNode*> additions2, subtractions2;
3941 std::vector<CEvaluationNode*> tmp;
3942 unsigned int i, iMax = additions1.size();
3944 for (i = 0; i < iMax; ++i)
3946 unsigned int j, jMax = additions2.size();
3948 for (j = 0; j < jMax; ++j)
3951 pMult->
addChild(additions1[i]->copyBranch());
3952 pMult->
addChild(additions2[j]->copyBranch());
3953 tmp.push_back(pMult);
3957 iMax = subtractions1.size();
3959 for (i = 0; i < iMax; ++i)
3961 unsigned int j, jMax = subtractions2.size();
3963 for (j = 0; j < jMax; ++j)
3966 pMult->
addChild(subtractions1[i]->copyBranch());
3967 pMult->
addChild(subtractions2[j]->copyBranch());
3968 tmp.push_back(pMult);
3975 assert(pResult != NULL);
3979 iMax = additions1.size();
3981 for (i = 0; i < iMax; ++i)
3983 unsigned int j, jMax = subtractions2.size();
3985 for (j = 0; j < jMax; ++j)
3988 pMult->
addChild(additions1[i]->copyBranch());
3989 pMult->
addChild(subtractions2[j]->copyBranch());
3990 tmp.push_back(pMult);
3994 iMax = subtractions1.size();
3996 for (i = 0; i < iMax; ++i)
3998 unsigned int j, jMax = additions2.size();
4000 for (j = 0; j < jMax; ++j)
4003 pMult->
addChild(subtractions1[i]->copyBranch());
4004 pMult->
addChild(additions2[j]->copyBranch());
4005 tmp.push_back(pMult);
4011 if (pResult != NULL)
4017 assert(pTmpNode != NULL);
4024 std::ostringstream os;
4026 os << tmp[0]->getValue() * -1.0;
4036 assert(pTmpNode != NULL);
4059 bool modified =
false;
4062 assert(pResult != NULL);
4066 std::ostringstream os;
4075 assert(pChild1 != NULL);
4077 assert(pChild2 != NULL);
4091 std::vector<const CEvaluationNode*> multiplications, divisions;
4104 if (collected.size() != (multiplications.size() + divisions.size()))
4106 std::vector<CEvaluationNode*> numeratorChain;
4107 std::vector<CEvaluationNode*> denominatorChain;
4108 unsigned int iMax = collected.size();
4110 for (
unsigned int i = 0; i < iMax; ++i)
4124 else if (match.
factor > 0.0)
4128 numeratorChain.push_back(match.
pNode);
4138 numeratorChain.push_back(pPower);
4145 denominatorChain.push_back(match.
pNode);
4153 os << fabs(match.
factor);
4155 denominatorChain.push_back(pPower);
4170 numeratorChain.push_back(pPower);
4175 if (numeratorChain.empty())
4177 pTmpOrig = CNormalTranslation::ONE_NODE.
copyBranch();
4184 assert(pTmpOrig != NULL);
4186 if (!denominatorChain.empty())
4190 pTmpOrig = pTmpNode;
4192 assert(pTmpNode != NULL);
4196 assert(pTmpOrig != NULL);
4202 dfi->
getParent()->addChild(pTmpOrig, *dfi);
4210 assert(*dfi == pResult);
4222 std::vector<product_match>::iterator it2 = collected.begin(), endit2 = collected.end();
4224 while (it2 != endit2)
4226 if ((*it2).pNode != NULL)
4230 if ((*it2).pExponentNode != NULL)
4232 delete(*it2).pExponentNode;
4252 std::vector<const CEvaluationNode*> additions, subtractions;
4254 std::vector<CEvaluationNode*> negAdditions, negSubtractions;
4257 additions.insert(additions.end(), negSubtractions.begin(), negSubtractions.end());
4258 subtractions.insert(subtractions.end(), negAdditions.begin(), negAdditions.end());
4266 std::vector<CEvaluationNode*>::iterator it = negAdditions.begin(), endit = negAdditions.end();
4274 it = negSubtractions.begin(), endit = negSubtractions.end();
4284 if (collected.size() != (additions.size() + subtractions.size()))
4286 std::vector<CEvaluationNode*> chain;
4287 unsigned int iMax = collected.size();
4294 for (
unsigned int i = 0; i < iMax; ++i)
4305 chain.push_back(match.
pNode);
4314 assert(pMult != NULL);
4315 assert(match.
pNode != NULL);
4317 chain.push_back(pMult);
4333 assert(pTmpOrig != NULL);
4339 dfi->
getParent()->addChild(pTmpOrig, *dfi);
4347 assert(*dfi == pResult);
4359 std::vector<summ_match>::iterator it2 = collected.begin(), endit2 = collected.end();
4361 while (it2 != endit2)
4363 if ((*it2).pNode != NULL)
4374 subtractions.clear();
4389 if (modified ==
false)
5123 if (pOrig == NULL)
return NULL;
5126 std::vector<CEvaluationNode*> children;
5130 bool childrenChanged =
false;
5132 while (pChild != NULL)
5136 if (pNewChild != NULL)
5138 childrenChanged =
true;
5141 children.push_back(pNewChild);
5145 if (childrenChanged ==
true)
5147 std::vector<CEvaluationNode*>::iterator it = children.begin(), endit = children.end();
5161 assert(pChild == NULL);
5163 pResult = pTmpOrig->
copyNode(children);
5172 assert(pChild1 != NULL);
5174 assert(pChild2 != NULL);
5184 pTmp->
addChild(dynamic_cast<const CEvaluationNode*>(pChild1->
getChild())->copyBranch());
5185 pTmp->
addChild(dynamic_cast<const CEvaluationNode*>(pChild2->
getChild()->getSibling())->copyBranch());
5188 pTmp->
addChild(dynamic_cast<const CEvaluationNode*>(pChild1->
getChild()->getSibling())->copyBranch());
5189 pTmp->
addChild(dynamic_cast<const CEvaluationNode*>(pChild2->
getChild())->copyBranch());
5192 if (pResult != NULL)
5197 pResult = pTmpResult;
5203 pTmpResult->
addChild(dynamic_cast<const CEvaluationNode*>(pChild1->
getChild())->copyBranch());
5205 pTmp->
addChild(dynamic_cast<const CEvaluationNode*>(pChild1->
getChild()->getSibling())->copyBranch());
5207 if (pResult == NULL)
5213 assert(pResult == pTmpOrig);
5214 pResult->
removeChild(const_cast<CEvaluationNode*>(pChild2));
5215 pTmp->
addChild(const_cast<CEvaluationNode*>(pChild2));
5220 pResult = pTmpResult;
5229 if (pResult != NULL)
5231 assert(pTmpOrig == pResult);
5232 pResult->
removeChild(const_cast<CEvaluationNode*>(pChild1));
5233 pTmp->
addChild(const_cast<CEvaluationNode*>(pChild1));
5240 pTmp->
addChild(dynamic_cast<const CEvaluationNode*>(pChild2->
getChild()->getSibling())->copyBranch());
5242 pTmpResult->
addChild(dynamic_cast<const CEvaluationNode*>(pChild2->
getChild())->copyBranch());
5244 if (pResult != NULL)
5249 pResult = pTmpResult;
5265 if (pOrig == NULL)
return NULL;
5269 std::vector<CEvaluationNode*> children;
5273 bool childrenChanged =
false;
5275 while (pChild != NULL)
5279 if (pNewChild != NULL)
5281 childrenChanged =
true;
5284 children.push_back(pNewChild);
5288 if (childrenChanged ==
true)
5291 std::vector<CEvaluationNode*>::iterator it = children.begin(), endit = children.end();
5304 assert(pChild == NULL);
5305 pResult = pTmpOrig->
copyNode(children);
5312 assert(pChild1 != NULL);
5314 assert(pChild2 != NULL);
5323 pTmp->
addChild(dynamic_cast<const CEvaluationNode*>(pChild1->
getChild())->copyBranch());
5327 pTmp->
addChild(dynamic_cast<const CEvaluationNode*>(pChild1->
getChild()->getSibling())->copyBranch());
5329 if (pResult == NULL)
5336 pResult->
removeChild(const_cast<CEvaluationNode*>(pChild2));
5337 pTmp->
addChild(const_cast<CEvaluationNode*>(pChild2));
5342 pResult = pTmpResult;
5359 std::vector<CEvaluationNode*> children;
5362 while (pChild != NULL)
5374 assert(children.size() == 2);
5378 pNumerator1 =
dynamic_cast<CEvaluationNode*
>(children[0]->getChild());
5379 pDenominator1 =
dynamic_cast<CEvaluationNode*
>(children[0]->getChild()->getSibling());
5383 pNumerator1 = children[0];
5388 pNumerator2 =
dynamic_cast<CEvaluationNode*
>(children[1]->getChild());
5389 pDenominator2 =
dynamic_cast<CEvaluationNode*
>(children[1]->getChild()->getSibling());
5393 pNumerator2 = children[1];
5396 if (pDenominator1 || pDenominator2)
5404 if (pDenominator1 && pDenominator2)
5411 else if (pDenominator1)
5425 pResult = pOrig->
copyNode(children);
5430 pResult = pOrig->
copyNode(children);
5447 std::vector<CEvaluationNode*> children;
5451 bool childrenChanged =
false;
5453 while (pChild != NULL)
5457 if (pNewChild != NULL)
5459 childrenChanged =
true;
5462 children.push_back(pNewChild);
5466 if (childrenChanged ==
true)
5468 std::vector<CEvaluationNode*>::iterator it = children.begin(), endit = children.end();
5482 assert(pChild == NULL);
5485 pResult = pRoot->
copyNode(children);
5494 assert(pBase != NULL);
5496 assert(pExp != NULL);
5501 std::vector<const CEvaluationNode*> multiplications, divisions;
5502 std::vector<CEvaluationNode*> numeratorNodes, denominatorNodes;
5505 std::vector<const CEvaluationNode*>::const_iterator it = multiplications.begin(), endit = multiplications.end();
5512 if (pResult == NULL)
5514 pPower->
addChild((*it)->copyBranch());
5520 if ((*it)->getParent() != NULL)
5525 pPower->
addChild(const_cast<CEvaluationNode*>(*it));
5529 numeratorNodes.push_back(pPower);
5534 assert(pTmp != NULL);
5536 if (!divisions.empty())
5538 it = divisions.begin(), endit = divisions.end();
5544 if (pResult == NULL)
5546 pPower->
addChild((*it)->copyBranch());
5552 if ((*it)->getParent() != NULL)
5557 pPower->
addChild(const_cast<CEvaluationNode*>(*it));
5561 denominatorNodes.push_back(pPower);
5568 assert(pTmp != NULL);
5573 if (pResult != NULL)
5582 std::vector<CEvaluationNode*> additions, subtractions;
5586 unsigned int i, iMax = additions.size();
5588 for (i = 0; i < iMax; ++i)
5590 delete additions[i];
5594 iMax = subtractions.size();
5596 for (i = 0; i < iMax; ++i)
5598 delete subtractions[i];
5601 subtractions.clear();
5603 if (resultPair.first != NULL)
5607 pTmpNode->
addChild(resultPair.first);
5611 pTmpNode->
addChild(resultPair.second);
5615 if (pResult != NULL)
5632 std::pair<CEvaluationNode*, CEvaluationNode*>
CNormalTranslation::factorize(
const std::vector<CEvaluationNode*>& additions,
const std::vector<CEvaluationNode*>& subtractions)
5634 std::vector<const CEvaluationNode*> commonMultiplications;
5635 std::vector<const CEvaluationNode*> commonDivisions;
5637 assert(additions.size() > 0);
5639 std::vector<const CEvaluationNode*> multiplications, divisions;
5640 unsigned int i, iMax = additions.size();
5641 unsigned int iiMax = iMax + subtractions.size();
5642 std::vector<std::vector<const CEvaluationNode*> > multiplicationVectors, divisionVectors;
5644 for (i = 0; i < iiMax; ++i)
5646 const CEvaluationNode* pTmpNode = (i < iMax) ? additions[i] : subtractions[i - iMax];
5659 multiplications.push_back(pTmpNode);
5664 multiplications.push_back(pTmpNode);
5667 multiplicationVectors.push_back(multiplications);
5668 divisionVectors.push_back(divisions);
5669 multiplications.clear();
5674 multiplications = multiplicationVectors[0];
5675 std::vector<const CEvaluationNode*>::const_iterator it = multiplications.begin(), endit = multiplications.end();
5679 bool everywhere =
true;
5680 std::vector<std::vector<const CEvaluationNode*> >::iterator innerIt = multiplicationVectors.begin(), innerEndit = multiplicationVectors.end();
5683 std::string infix = (*it)->buildInfix();
5686 while (innerIt != innerEndit)
5689 std::vector<const CEvaluationNode*>::iterator innerIt2 = (*innerIt).begin(), innerEndit2 = (*innerIt).end();
5691 while (innerIt2 != innerEndit2)
5693 if ((*innerIt2)->buildInfix() == infix)
5716 commonMultiplications.push_back(*it);
5717 std::vector<std::vector<const CEvaluationNode*> >::iterator innerIt = multiplicationVectors.begin();
5718 std::vector<std::vector<const CEvaluationNode*> >::iterator innerEndit = multiplicationVectors.end();
5720 while (innerIt != innerEndit)
5722 std::vector<const CEvaluationNode*>::iterator innerIt2 = (*innerIt).begin();
5723 std::vector<const CEvaluationNode*>::iterator innerEndit2 = (*innerIt).end();
5725 while (innerIt2 != innerEndit2)
5727 if ((*innerIt2)->buildInfix() == infix)
5729 innerIt->erase(innerIt2);
5744 divisions = divisionVectors[0];
5746 if (!divisions.empty())
5748 it = divisions.begin(), endit = divisions.end();
5752 bool everywhere =
true;
5753 std::vector<std::vector<const CEvaluationNode*> >::iterator innerIt = divisionVectors.begin(), innerEndit = divisionVectors.end();
5756 std::string infix = (*it)->buildInfix();
5759 while (innerIt != innerEndit)
5762 std::vector<const CEvaluationNode*>::iterator innerIt2 = (*innerIt).begin(), innerEndit2 = (*innerIt).end();
5764 while (innerIt2 != innerEndit2)
5766 if ((*innerIt2)->buildInfix() == infix)
5789 commonDivisions.push_back(*it);
5790 innerIt = divisionVectors.begin();
5791 innerEndit = divisionVectors.end();
5793 while (innerIt != innerEndit)
5795 std::vector<const CEvaluationNode*>::iterator innerIt2 = (*innerIt).begin();
5796 std::vector<const CEvaluationNode*>::iterator innerEndit2 = (*innerIt).end();
5798 while (innerIt2 != innerEndit2)
5800 if ((*innerIt2)->buildInfix() == infix)
5802 innerIt->erase(innerIt2);
5826 if (!(commonMultiplications.empty() && commonDivisions.empty()))
5828 unsigned int i, iMax = additions.size();
5829 unsigned int iiMax = iMax + subtractions.size();
5830 std::vector<CEvaluationNode*> newAddition