COPASI API  4.16.103
Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
CNormalFraction Class Reference

#include <CNormalFraction.h>

Inheritance diagram for CNormalFraction:
Inheritance graph
[legend]
Collaboration diagram for CNormalFraction:
Collaboration graph
[legend]

Public Member Functions

bool cancel ()
 
bool checkDenominatorOne () const
 
bool checkForFractions () const
 
bool checkIsOne () const
 
bool checkIsZero () const
 
bool checkNumeratorOne () const
 
 CNormalFraction ()
 
 CNormalFraction (const CNormalFraction &src)
 
virtual CNormalBasecopy () const
 
bool expand (const CNormalLcm &lcm)
 
const CNormalLcmfindLcm () const
 
CNormalSumgetDenominator ()
 
const CNormalSumgetDenominator () const
 
CNormalSumgetNumerator ()
 
const CNormalSumgetNumerator () const
 
bool multiply (const C_FLOAT64 &number)
 
bool multiply (const CNormalItemPower &itemPower)
 
const CNormalSummultiply (CNormalLcm lcm)
 
bool operator< (const CNormalFraction &src) const
 
CNormalFractionoperator= (const CNormalFraction &src)
 
bool operator== (const CNormalFraction &rhs) const
 
bool setDenominator (const CNormalSum &denominator)
 
bool setDenominatorOne ()
 
bool setNumerator (const CNormalSum &numerator)
 
virtual bool simplify ()
 
virtual std::string toString () const
 
virtual ~CNormalFraction ()
 
- Public Member Functions inherited from CNormalBase
virtual bool areEqual (const CNormalBase &rhs) const
 
virtual ~CNormalBase ()
 

Static Public Member Functions

static CNormalFractioncreateUnitFraction ()
 

Private Attributes

CNormalSummpDenominator
 
CNormalSummpNumerator
 

Detailed Description

Definition at line 32 of file CNormalFraction.h.

Constructor & Destructor Documentation

CNormalFraction::CNormalFraction ( )

Default constructor

Definition at line 47 of file CNormalFraction.cpp.

Referenced by copy(), and createUnitFraction().

CNormalFraction::CNormalFraction ( const CNormalFraction src)

Copy contructor

Definition at line 52 of file CNormalFraction.cpp.

52  : CNormalBase(src),
CNormalSum * mpNumerator
CNormalSum * mpDenominator
CNormalFraction::~CNormalFraction ( )
virtual

Dtor.

Definition at line 70 of file CNormalFraction.cpp.

References mpDenominator, and mpNumerator.

71 {
72  if (mpNumerator != NULL) delete mpNumerator;
73 
74  if (mpDenominator != NULL) delete mpDenominator;
75 }
CNormalSum * mpNumerator
CNormalSum * mpDenominator

Member Function Documentation

bool CNormalFraction::cancel ( )

Cancel common items of mpNumerator and mpDenominator.

Returns
true.

Definition at line 211 of file CNormalFraction.cpp.

References C_FLOAT64, CNormalSum::checkFactor(), checkForFractions(), CNormalSum::divide(), CNormalSum::getFractions(), CNormalSum::getProducts(), mpDenominator, mpNumerator, CNormalSum::multiply(), setDenominatorOne(), and setNumerator().

Referenced by createFraction(), and simplify().

212 {
213  if (mpNumerator->getProducts().size() != 0 || mpNumerator->getFractions().size() != 0 || mpDenominator->getProducts().size() != 0 || mpDenominator->getFractions().size() != 0)
214  {
215  if (*mpDenominator == *mpNumerator)
216  {
219  return true;
220  }
221 
222  if (mpDenominator->getProducts().size() != 0)
223  {
224  C_FLOAT64 factor = (*mpDenominator->getProducts().begin())->getFactor();
225 
226  if (fabs(factor) < 1.0E-100)
227  return false;
228  else
229  {
230  mpNumerator->multiply(1.0 / factor); //factor != 0 as checked earlier
231  mpDenominator->multiply(1.0 / factor);
232  }
233  }
234 
235  if (checkForFractions() == false)
236  {
237  if (mpDenominator->getProducts().size() != 0 && (*mpDenominator->getProducts().begin())->getItemPowers().size() != 0)
238  {
239  std::set<CNormalItemPower*, compareItemPowers >::const_iterator it = (*mpDenominator->getProducts().begin())->getItemPowers().begin();
240  std::set<CNormalItemPower*, compareItemPowers >::const_iterator itEnd = (*mpDenominator->getProducts().begin())->getItemPowers().end();
241  std::vector<CNormalItemPower*> tmpV;
242 
243  while (it != itEnd)
244  {//runs through all item powers in the first product of the denominator
245  C_FLOAT64 exp = mpNumerator->checkFactor(**it);
246 
247  if (fabs(exp) >= 1.0E-100)
248  {
249  exp = mpDenominator->checkFactor(**it) < exp ? mpDenominator->checkFactor(**it) : exp;
250 
251  if (fabs(exp) >= 1.0E-100)
252  {
253  CNormalItemPower* itemPower = new CNormalItemPower((*it)->getItem(), exp);
254  tmpV.push_back(itemPower);
255  }
256  }
257 
258  ++it;
259  }
260 
261  std::vector<CNormalItemPower*>::iterator it2 = tmpV.begin();
262  std::vector<CNormalItemPower*>::iterator itEnd2 = tmpV.end();
263 
264  while (it2 != itEnd2)
265  {
266  mpNumerator->divide(**it2);
267  mpDenominator->divide(**it2);
268  delete *it2;
269  ++it2;
270  }
271  }
272  }
273  }
274 
275  return true;
276 }
bool checkForFractions() const
C_FLOAT64 checkFactor(const CNormalItemPower &itemPower) const
Definition: CNormalSum.cpp:382
const std::set< CNormalFraction * > & getFractions() const
Definition: CNormalSum.cpp:425
CNormalSum * mpNumerator
bool divide(const CNormalItemPower &itemPower)
Definition: CNormalSum.cpp:371
CNormalSum * mpDenominator
bool setNumerator(const CNormalSum &numerator)
#define C_FLOAT64
Definition: copasi.h:92
const std::set< CNormalProduct *, compareProducts > & getProducts() const
Definition: CNormalSum.cpp:416
bool multiply(const C_FLOAT64 &number)
Definition: CNormalSum.cpp:249
bool CNormalFraction::checkDenominatorOne ( ) const

Check if the denominator of this fraction equals 1.

Returns
bool

Definition at line 124 of file CNormalFraction.cpp.

References CNormalSum::checkIsOne(), and mpDenominator.

Referenced by convertToCEvaluationNode(), CNormalProduct::getDenominator(), is_mass_action(), normalize_variable_names(), CNormalGeneralPower::toString(), and toString().

125 {
126  return this->mpDenominator->checkIsOne();
127 }
CNormalSum * mpDenominator
bool checkIsOne() const
Definition: CNormalSum.cpp:850
bool CNormalFraction::checkForFractions ( ) const

Check if this fraction contains further fractions.

Returns
bool.

Definition at line 184 of file CNormalFraction.cpp.

References CNormalSum::getFractions(), mpDenominator, and mpNumerator.

Referenced by cancel().

185 {
186  if (mpNumerator->getFractions().size() + mpDenominator->getFractions().size() == 0)
187  return false;
188 
189  return true;
190 }
const std::set< CNormalFraction * > & getFractions() const
Definition: CNormalSum.cpp:425
CNormalSum * mpNumerator
CNormalSum * mpDenominator
bool CNormalFraction::checkIsOne ( ) const

Definition at line 518 of file CNormalFraction.cpp.

References mpDenominator, and mpNumerator.

Referenced by CNormalGeneralPower::checkIsOne(), convertToCEvaluationNode(), and CNormalSum::simplify().

519 {
520  // the fraction is one if numerator and denominator are equal
521  return ((*mpNumerator) == (*mpDenominator));
522 }
CNormalSum * mpNumerator
CNormalSum * mpDenominator
bool CNormalFraction::checkIsZero ( ) const

Definition at line 524 of file CNormalFraction.cpp.

References CNormalSum::checkIsZero(), and mpNumerator.

Referenced by CNormalGeneralPower::checkIsOne().

525 {
526  // the fraction is 0.0 if the numerator is 0.0
527  return this->mpNumerator->checkIsZero();
528 }
CNormalSum * mpNumerator
bool checkIsZero() const
Definition: CNormalSum.cpp:874
bool CNormalFraction::checkNumeratorOne ( ) const

Check if the numerator of this fraction equals 1.

Returns
bool

Check if the denominator of this fraction equals 1.

Returns
bool

Definition at line 115 of file CNormalFraction.cpp.

References CNormalSum::checkIsOne(), and mpNumerator.

116 {
117  return this->mpNumerator->checkIsOne();
118 }
CNormalSum * mpNumerator
bool checkIsOne() const
Definition: CNormalSum.cpp:850
CNormalBase * CNormalFraction::copy ( ) const
virtual

Implements CNormalBase.

Definition at line 434 of file CNormalFraction.cpp.

References CNormalFraction().

435 {
436  return new CNormalFraction(*this);
437 }
CNormalFraction * CNormalFraction::createUnitFraction ( )
static

Returns a fraction that is 1/1. It creates a new fraction and sets the numerator and the denominator to a unit sum.

Definition at line 530 of file CNormalFraction.cpp.

References CNormalFraction(), CNormalSum::createUnitSum(), mpDenominator, and mpNumerator.

Referenced by CNormalGeneralPower::createUnitGeneralPower(), CNormalGeneralPower::multiply(), CNormalSum::simplify(), and test_simplify::test_simplify_1().

531 {
532  CNormalFraction* pFraction = new CNormalFraction();
533  delete pFraction->mpNumerator;
534  delete pFraction->mpDenominator;
535  pFraction->mpNumerator = CNormalSum::createUnitSum();
537  return pFraction;
538 }
CNormalSum * mpNumerator
CNormalSum * mpDenominator
static CNormalSum * createUnitSum()
Definition: CNormalSum.cpp:889
bool CNormalFraction::expand ( const CNormalLcm lcm)

Expand this fraction by a lcm.

Returns
true.

Definition at line 196 of file CNormalFraction.cpp.

References CNormalSum::getSize(), mpDenominator, mpNumerator, and CNormalSum::multiply().

Referenced by simplify().

197 {
198  mpNumerator->multiply(lcm);
199  mpDenominator->multiply(lcm);
200 
201  if (mpDenominator->getSize() == 0)
202  return false;
203 
204  return true;
205 }
int getSize() const
Definition: CNormalSum.cpp:147
CNormalSum * mpNumerator
CNormalSum * mpDenominator
bool multiply(const C_FLOAT64 &number)
Definition: CNormalSum.cpp:249
const CNormalLcm * CNormalFraction::findLcm ( ) const

Find lcm of denominators of respective fractions contained in this fraction.

Returns
pointer to lcm.

Definition at line 329 of file CNormalFraction.cpp.

References CNormalLcm::add(), CNormalSum::getFractions(), mpDenominator, and mpNumerator.

Referenced by simplify().

330 {
331  CNormalLcm* lcm = new CNormalLcm();
332 
333  std::set<CNormalFraction*>::const_iterator it;
334  std::set<CNormalFraction*>::const_iterator itEnd = mpNumerator->getFractions().end();
335 
336  for (it = mpNumerator->getFractions().begin(); it != itEnd; ++it)
337  {
338  lcm->add((*it)->getDenominator());
339  }
340 
341  std::set<CNormalFraction*>::const_iterator it2;
342  std::set<CNormalFraction*>::const_iterator it2End = mpDenominator->getFractions().end();
343 
344  for (it2 = mpDenominator->getFractions().begin(); it2 != it2End; ++it2)
345  {
346  lcm->add((*it2)->getDenominator());
347  }
348 
349  return lcm;
350 }
bool add(const CNormalItemPower &itemPower)
Definition: CNormalLcm.cpp:105
const std::set< CNormalFraction * > & getFractions() const
Definition: CNormalSum.cpp:425
CNormalSum * mpNumerator
CNormalSum * mpDenominator
CNormalSum & CNormalFraction::getDenominator ( )

Retrieve the denominator of this fraction.

Returns
mpDenominator.

Definition at line 157 of file CNormalFraction.cpp.

References mpDenominator.

Referenced by convertToCEvaluationNode(), CNormalGeneralPower::multiply(), normalize_variable_names(), CNormalSum::simplify(), test_simplify::test_simplify_1(), and CNormalGeneralPower::toString().

158 {
159  return *mpDenominator;
160 }
CNormalSum * mpDenominator
const CNormalSum & CNormalFraction::getDenominator ( ) const

Retrieve the denominator of this fraction.

Returns
mpDenominator.

Definition at line 175 of file CNormalFraction.cpp.

References mpDenominator.

176 {
177  return *mpDenominator;
178 }
CNormalSum * mpDenominator
CNormalSum & CNormalFraction::getNumerator ( )

Retrieve the numerator of this fraction.

Returns
mpNumerator.

Definition at line 148 of file CNormalFraction.cpp.

References mpNumerator.

Referenced by CNormalSum::add(), convertToCEvaluationNode(), is_mass_action(), CNormalGeneralPower::multiply(), normalize_variable_names(), test_simplify::test_simplify_1(), and CNormalGeneralPower::toString().

149 {
150  return *mpNumerator;
151 }
CNormalSum * mpNumerator
const CNormalSum & CNormalFraction::getNumerator ( ) const

Retrieve the numerator of this fraction.

Returns
mpNumerator.

Definition at line 166 of file CNormalFraction.cpp.

References mpNumerator.

167 {
168  return *mpNumerator;
169 }
CNormalSum * mpNumerator
bool CNormalFraction::multiply ( const C_FLOAT64 number)

Multiply this fraction with a number.

Returns
true.

Definition at line 282 of file CNormalFraction.cpp.

References CNormalSum::getSize(), mpNumerator, CNormalSum::multiply(), and setDenominatorOne().

283 {
284  mpNumerator->multiply(number);
285 
286  if (mpNumerator->getSize() == 0)
288 
289  return true;
290 }
int getSize() const
Definition: CNormalSum.cpp:147
CNormalSum * mpNumerator
bool multiply(const C_FLOAT64 &number)
Definition: CNormalSum.cpp:249
bool CNormalFraction::multiply ( const CNormalItemPower itemPower)

Multiply a fraction with an itempower.

Returns
true.

Definition at line 296 of file CNormalFraction.cpp.

References C_FLOAT64, CNormalSum::checkFactor(), CNormalSum::divide(), CNormalItemPower::getExp(), CNormalItemPower::getItem(), mpDenominator, mpNumerator, and CNormalSum::multiply().

297 {
298  C_FLOAT64 exp = mpDenominator->checkFactor(itemPower);
299  C_FLOAT64 tmpExp;
300 
301  if (fabs(exp) >= 1.0E-100)
302  {
303  CNormalItemPower* pTmp1 = new CNormalItemPower(itemPower.getItem(), exp);
304  mpDenominator->divide(*pTmp1);
305  delete pTmp1;
306 
307  if ((tmpExp = fabs(itemPower.getExp() - exp)) >= 1.0E-100)
308  {
309  // :TODO: Memory leak?
310  CNormalItemPower * pTmp2 =
311  new CNormalItemPower(itemPower.getItem(), tmpExp);
312  mpNumerator->multiply(*pTmp2);
313  delete pTmp2;
314  return true;
315  }
316 
317  return true;
318  }
319  else
320  mpNumerator->multiply(itemPower);
321 
322  return true;
323 }
C_FLOAT64 checkFactor(const CNormalItemPower &itemPower) const
Definition: CNormalSum.cpp:382
CNormalSum * mpNumerator
bool divide(const CNormalItemPower &itemPower)
Definition: CNormalSum.cpp:371
const C_FLOAT64 & getExp() const
CNormalSum * mpDenominator
#define C_FLOAT64
Definition: copasi.h:92
CNormalBase & getItem()
bool multiply(const C_FLOAT64 &number)
Definition: CNormalSum.cpp:249
const CNormalSum * CNormalFraction::multiply ( CNormalLcm  lcm)

Multiply this fraction by a lcm.

Returns
CNormalSum*, pointer to result of multiplication ie. a normal sum. This fraction is useless now.

Definition at line 357 of file CNormalFraction.cpp.

References CNormalSum::getFractions(), mpDenominator, mpNumerator, CNormalSum::multiply(), and CNormalLcm::remove().

358 {
359  if (mpDenominator->getFractions().size() != 0)
360  {
361  return NULL;
362  }
363 
364  if (lcm.remove(*mpDenominator) == false)
365  {
366  return NULL;
367  }
368 
369  mpNumerator->multiply(lcm);
370  CNormalSum * sum = new CNormalSum(*mpNumerator);
371  return sum;
372 }
bool remove(const CNormalItemPower &itemPower)
Definition: CNormalLcm.cpp:176
const std::set< CNormalFraction * > & getFractions() const
Definition: CNormalSum.cpp:425
CNormalSum * mpNumerator
CNormalSum * mpDenominator
bool multiply(const C_FLOAT64 &number)
Definition: CNormalSum.cpp:249
bool CNormalFraction::operator< ( const CNormalFraction src) const

smaller operator

Definition at line 439 of file CNormalFraction.cpp.

References mpDenominator, and mpNumerator.

440 {
441  bool result = false;
442 
443  if ((*this->mpNumerator) < (*src.mpNumerator))
444  {
445  result = true;
446  }
447  else if ((*this->mpNumerator) == (*src.mpNumerator))
448  {
449  if ((*this->mpDenominator) < (*src.mpDenominator))
450  {
451  result = true;
452  }
453  }
454 
455  return result;
456 }
CNormalSum * mpNumerator
CNormalSum * mpDenominator
CNormalFraction & CNormalFraction::operator= ( const CNormalFraction src)

Assignment operator

Definition at line 59 of file CNormalFraction.cpp.

References mpDenominator, and mpNumerator.

60 {
61  if (this->mpNumerator != NULL) delete this->mpNumerator;
62 
63  if (this->mpDenominator != NULL) delete this->mpDenominator;
64 
65  this->mpNumerator = new CNormalSum(*src.mpNumerator);
66  this->mpDenominator = new CNormalSum(*src.mpDenominator);
67  return *this;
68 }
CNormalSum * mpNumerator
CNormalSum * mpDenominator
bool CNormalFraction::operator== ( const CNormalFraction rhs) const

Definition at line 404 of file CNormalFraction.cpp.

References mpDenominator, and mpNumerator.

405 {
406  if ((*mpNumerator == *rhs.mpNumerator) && (*mpDenominator == *rhs.mpDenominator))
407  return true;
408  else
409  return false;
410 }
CNormalSum * mpNumerator
CNormalSum * mpDenominator
bool CNormalFraction::setDenominator ( const CNormalSum denominator)

Set denominator of this fraction.

Returns
true.

Definition at line 96 of file CNormalFraction.cpp.

References CNormalSum::getSize(), and mpDenominator.

Referenced by createFraction(), CNormalProduct::getDenominator(), setDenominatorOne(), CNormalProduct::setDenominatorsOne(), and CNormalSum::simplify().

97 {
98  if (mpDenominator != NULL)
99  {
100  delete mpDenominator;
101  }
102 
103  mpDenominator = new CNormalSum(denominator);
104 
105  if (mpDenominator->getSize() == 0)
106  return false;
107 
108  return true;
109 }
int getSize() const
Definition: CNormalSum.cpp:147
CNormalSum * mpDenominator
bool CNormalFraction::setDenominatorOne ( )

Set the denominator of this fraction equal 1.

Returns
true.

Definition at line 133 of file CNormalFraction.cpp.

References CNormalSum::add(), and setDenominator().

Referenced by cancel(), and multiply().

134 {
135  CNormalProduct* product = new CNormalProduct();
136  CNormalSum* sum = new CNormalSum();
137  sum->add(*product);
138  delete product;
139  setDenominator(*sum);
140  delete sum;
141  return true;
142 }
bool add(const CNormalProduct &product)
Definition: CNormalSum.cpp:156
bool setDenominator(const CNormalSum &denominator)
bool CNormalFraction::setNumerator ( const CNormalSum numerator)

Set numerator of this fraction

Returns
true.

Definition at line 81 of file CNormalFraction.cpp.

References mpNumerator.

Referenced by cancel(), createFraction(), CNormalProduct::getDenominator(), CNormalGeneralPower::multiply(), and CNormalSum::simplify().

82 {
83  if (mpNumerator != NULL)
84  {
85  delete mpNumerator;
86  }
87 
88  mpNumerator = new CNormalSum(numerator);
89  return true;
90 }
CNormalSum * mpNumerator
bool CNormalFraction::simplify ( )
virtual

Check if numerator and denominator contain fractions, find lcm of respective denominators, expand this fraction by lcm and cancel.

Returns
true

Implements CNormalBase.

Definition at line 379 of file CNormalFraction.cpp.

References cancel(), expand(), findLcm(), CNormalSum::getFractions(), mpDenominator, mpNumerator, and CNormalSum::simplify().

Referenced by CNormalTranslation::normAndSimplify(), CNormalLogicalItem::simplify(), CNormalGeneralPower::simplify(), CNormalChoice::simplify(), CNormalFunction::simplify(), CNormalSum::simplify(), and test_simplify::test_simplify_1().

380 {
381  bool result = true;
382  result = this->mpNumerator->simplify();
383 
384  if (result == true)
385  {
386  result = this->mpDenominator->simplify();
387  }
388 
389  if (result == true)
390  {
391  if (mpNumerator->getFractions().size() + mpDenominator->getFractions().size() > 0)
392  {
393  // TODO the following code does not work if there are fractions left.
394  const CNormalLcm* lcm = findLcm();
395  assert(expand(*lcm) == true);
396  delete lcm;
397  }
398  }
399 
400  cancel();
401  return result;
402 }
virtual bool simplify()
Definition: CNormalSum.cpp:623
const std::set< CNormalFraction * > & getFractions() const
Definition: CNormalSum.cpp:425
CNormalSum * mpNumerator
CNormalSum * mpDenominator
const CNormalLcm * findLcm() const
bool expand(const CNormalLcm &lcm)
std::string CNormalFraction::toString ( ) const
virtual

Implements CNormalBase.

Definition at line 418 of file CNormalFraction.cpp.

References checkDenominatorOne(), mpDenominator, and mpNumerator.

Referenced by CNormalTranslation::elementaryEliminationDivide(), CNormalTranslation::elementaryEliminationMinus(), CNormalTranslation::elementaryEliminationModulus(), CNormalTranslation::matchPowerBases(), CNormalTranslation::matchSummands(), CNormalTranslation::normAndSimplifyReptdly(), operator<<(), test_biomodels217_expression::test_term_num1(), and CNormalChoice::toString().

419 {
420  std::ostringstream os;
421 
422  if (this->checkDenominatorOne() == true)
423  {
424  os << *this->mpNumerator;
425  }
426  else
427  {
428  os << "(" << *this->mpNumerator << ")/(" << *this->mpDenominator << ")";
429  }
430 
431  return os.str();
432 }
CNormalSum * mpNumerator
bool checkDenominatorOne() const
CNormalSum * mpDenominator

Member Data Documentation

CNormalSum* CNormalFraction::mpDenominator
private
CNormalSum* CNormalFraction::mpNumerator
private

The documentation for this class was generated from the following files: