COPASI API  4.16.103
test_simplify.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/compareExpressions/unittests/test_simplify.cpp,v $
3 // $Revision: 1.3 $
4 // $Name: $
5 // $Author: gauges $
6 // $Date: 2009/07/02 17:57:53 $
7 // End CVS Header
8 
9 // Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
11 // and The University of Manchester.
12 // All rights reserved.
13 
14 #include "test_simplify.h"
15 
16 #include "../CNormalFraction.h"
17 #include "../CNormalSum.h"
18 #include "../CNormalProduct.h"
19 #include "../CNormalItemPower.h"
20 #include "../CNormalGeneralPower.h"
21 #include "../CNormalItem.h"
23 
25 {
26  CCopasiRootContainer::init(false, 0, NULL);
27 }
28 
30 {
32 }
33 
35 {
36  CNormalItem* pItemA = new CNormalItem();
38  pItemA->setName("A");
39 
40  CNormalItem* pItemB = new CNormalItem();
42  pItemB->setName("B");
43 
44  CNormalItem* pItemC = new CNormalItem();
46  pItemC->setName("C");
47 
48  CNormalItem* pItemD = new CNormalItem();
50  pItemD->setName("D");
51 
52  CNormalItem* pItemE = new CNormalItem();
54  pItemE->setName("E");
55 
56  CNormalItem* pItemF = new CNormalItem();
58  pItemF->setName("F");
59 
60  // Z=(((-1) * E + F + 4 * D * E))^2
61  // -1.0 * E + F + 4 * D * E
62  CNormalFraction* pFraction = new CNormalFraction();
63  CNormalSum* pSum = &pFraction->getNumerator();
64  // -1 * E
65  CNormalProduct* pProduct = new CNormalProduct();
66  pProduct->multiply(-1.0);
67  pProduct->multiply(*pItemE);
68  pSum->add(*pProduct);
69  delete pProduct;
70  // F
71  pProduct = new CNormalProduct();
72  pProduct->multiply(*pItemF);
73  pSum->add(*pProduct);
74  delete pProduct;
75  // 4 * D * E
76  pProduct = new CNormalProduct();
77  pProduct->multiply(4.0);
78  pProduct->multiply(*pItemD);
79  pProduct->multiply(*pItemE);
80  pSum->add(*pProduct);
81  delete pProduct;
82  pProduct = new CNormalProduct();
83  pFraction->getDenominator().add(*pProduct);
84  delete pProduct;
86  CNormalGeneralPower* pGeneralPower = new CNormalGeneralPower();
87  pGeneralPower->setType(CNormalGeneralPower::POWER);
88  pGeneralPower->setLeft(*pFraction);
89  delete pFraction;
91  pGeneralPower->setRight(*pFraction);
92  delete pFraction;
93  pZ->setExp(2.0);
94  pZ->setItem(*pGeneralPower);
95  delete pGeneralPower;
96 
97  // Y=(((-16) * D^2 * E^2 + Z + 4 * D * E^2))
98  CNormalFraction* pY = new CNormalFraction();
99  pProduct = new CNormalProduct();
100  pProduct->multiply(-16.0);
101  // D^2
102  CNormalItemPower* pItemPower = new CNormalItemPower();
103  pItemPower->setExp(2.0);
104  pItemPower->setItem(*pItemD);
105  pProduct->multiply(*pItemPower);
106  delete pItemPower;
107  // E^2
108  pItemPower = new CNormalItemPower();
109  pItemPower->setExp(2.0);
110  pItemPower->setItem(*pItemE);
111  pProduct->multiply(*pItemPower);
112  delete pItemPower;
113  pSum = &pY->getNumerator();
114  pSum->add(*pProduct);
115  delete pProduct;
116  pProduct = new CNormalProduct();
117  pProduct->multiply(*pZ);
118  pZ->simplify();
119  delete pZ;
120  pSum->add(*pProduct);
121  delete pProduct;
122  // 4 * D * E^2
123  pProduct = new CNormalProduct();
124  pProduct->multiply(4.0);
125  pProduct->multiply(*pItemD);
126  pItemPower = new CNormalItemPower();
127  pItemPower->setExp(2.0);
128  pItemPower->setItem(*pItemE);
129  pProduct->multiply(*pItemPower);
130  delete pItemPower;
131  pSum->add(*pProduct);
132  delete pProduct;
133  // set the denominator of pY to 1
134  pProduct = new CNormalProduct();
135  pY->getDenominator().add(*pProduct);
136  delete pProduct;
137 
138  // X=((-32) * B * C * D^2 * E + (-8) * B * C * D * F + 8 * B * C * D * E + 8 * B * C * D * Y^0.5)/((-4) * D + 1)
139  CNormalFraction* pX = new CNormalFraction();
140  // denominator -4 * D + 1
141  pProduct = new CNormalProduct();
142  pProduct->multiply(-4.0);
143  pProduct->multiply(*pItemD);
144  pX->getDenominator().add(*pProduct);
145  delete pProduct;
146  pProduct = new CNormalProduct();
147  pX->getDenominator().add(*pProduct);
148  delete pProduct;
149  // numerator ((-32) * B * C * D^2 * E + (-8) * B * C * D * F + 8 * B * C * D * E + 8 * B * C * D * Y^0.5)
150  pSum = &pX->getNumerator();
151  // (-32) * B * C * D^2 * E
152  pProduct = new CNormalProduct();
153  pProduct->multiply(-32.0);
154  pProduct->multiply(*pItemB);
155  pProduct->multiply(*pItemC);
156  pItemPower = new CNormalItemPower();
157  pItemPower->setExp(2.0);
158  pItemPower->setItem(*pItemD);
159  pProduct->multiply(*pItemPower);
160  delete pItemPower;
161  pProduct->multiply(*pItemE);
162  pSum->add(*pProduct);
163  delete pProduct;
164  // (-8) * B * C * D * F
165  pProduct = new CNormalProduct();
166  pProduct->multiply(-8.0);
167  pProduct->multiply(*pItemB);
168  pProduct->multiply(*pItemC);
169  pProduct->multiply(*pItemD);
170  pProduct->multiply(*pItemF);
171  pSum->add(*pProduct);
172  delete pProduct;
173  // 8 * B * C * D * E
174  pProduct = new CNormalProduct();
175  pProduct->multiply(8.0);
176  pProduct->multiply(*pItemB);
177  pProduct->multiply(*pItemC);
178  pProduct->multiply(*pItemD);
179  pProduct->multiply(*pItemE);
180  pSum->add(*pProduct);
181  delete pProduct;
182  // 8 * B * C * D * Y^0.5)
183  pProduct = new CNormalProduct();
184  pProduct->multiply(8.0);
185  pProduct->multiply(*pItemB);
186  pProduct->multiply(*pItemC);
187  pProduct->multiply(*pItemD);
188  pItemPower = new CNormalItemPower();
189  pItemPower->setExp(0.5);
190  pGeneralPower = new CNormalGeneralPower();
191  pGeneralPower->setType(CNormalGeneralPower::POWER);
192  pGeneralPower->setLeft(*pY);
193  pY->simplify();
194  delete pY;
196  pGeneralPower->setRight(*pFraction);
197  delete pFraction;
198  pItemPower->setItem(*pGeneralPower);
199  delete pGeneralPower;
200  pProduct->multiply(*pItemPower);
201  pSum->add(*pProduct);
202  delete pProduct;
203 
204  // W=((-8) * B * C * D * E + X)/(A)
205  CNormalFraction* pW = new CNormalFraction();
206  // denominator A
207  pSum = &pW->getDenominator();
208  pProduct = new CNormalProduct();
209  pProduct->multiply(*pItemA);
210  pSum->add(*pProduct);
211  delete pProduct;
212  // numerator ((-8) * B * C * D * E + X)
213  pSum = &pW->getNumerator();
214  // (-8) * B * C * D * E
215  pProduct = new CNormalProduct();
216  pProduct->multiply(-8.0);
217  pProduct->multiply(*pItemB);
218  pProduct->multiply(*pItemC);
219  pProduct->multiply(*pItemD);
220  pProduct->multiply(*pItemE);
221  pSum->add(*pProduct);
222  delete pProduct;
223 
224  pSum->add(*pX);
225  pX->simplify();
226  delete pX;
227  delete pItemA;
228  delete pItemB;
229  delete pItemC;
230  delete pItemD;
231  delete pItemE;
232  delete pItemF;
233  // test what simplify does to this
234  pProduct = new CNormalProduct();
235  pItemPower = new CNormalItemPower();
236  pItemPower->setExp(1.0);
237  pGeneralPower = new CNormalGeneralPower();
238  pGeneralPower->setType(CNormalGeneralPower::POWER);
240  pGeneralPower->setRight(*pFraction);
241  delete pFraction;
242  pGeneralPower->setLeft(*pW);
243  pItemPower->setItem(*pGeneralPower);
244  pGeneralPower->simplify();
245  delete pGeneralPower;
246  pProduct->multiply(*pItemPower);
247  pItemPower->simplify();
248  delete pItemPower;
249  pW->simplify();
250  pProduct->simplify();
251  delete pProduct;
252  delete pW;
253 }
virtual bool simplify()
CNormalSum & getNumerator()
bool setType(const Type &type)
Definition: CNormalItem.cpp:72
virtual bool simplify()
bool setName(const std::string &name)
Definition: CNormalItem.cpp:62
virtual bool simplify()
void test_simplify_1()
bool add(const CNormalProduct &product)
Definition: CNormalSum.cpp:156
static CNormalFraction * createUnitFraction()
bool setItem(const CNormalBase &item)
CNormalSum & getDenominator()
void setRight(const CNormalFraction &right)
void setLeft(const CNormalFraction &left)
static void init(int argc, char *argv[], const bool &withGui=false)
bool multiply(const C_FLOAT64 &number)
bool setExp(const C_FLOAT64 &number)