SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
Numeric.cpp
Go to the documentation of this file.
1 /**
2  * @file Numeric.cpp
3  * @author Florian Corzilius <corzilius@cs.rwth-aachen.de>
4  *
5  * @version 2013-04-15
6  * Created on April 15th, 2013
7  */
8 
9 #include "Numeric.h"
10 
11 namespace smtrat
12 {
13  namespace lra
14  {
15  /**
16  * Default constructor.
17  */
19  mContent( new Rational( 0 ) )
20  {}
21 
22  /**
23  * Constructing from a Rational.
24  * @param The Rational.
25  */
26  Numeric::Numeric( const Rational& _value ):
27  mContent( new Rational( _value ) )
28  {}
29 
30  /**
31  * Constructing from an integer.
32  * @param _value The integer.
33  */
34  Numeric::Numeric( int _value ):
35  mContent( new Rational( _value ) )
36  {}
37 
38  /**
39  * Constructing from an unsigned integer.
40  * @param _value The unsigned integer
41  */
42  Numeric::Numeric( unsigned int _value ):
43  mContent( new Rational( _value ) )
44  {}
45 
46  /**
47  * Constructing from a long integer.
48  * @param _value The unsigned long integer.
49  */
50  Numeric::Numeric( long _value ):
51  mContent( new Rational( _value ) )
52  {}
53 
54  /**
55  * Constructing from an unsigned long integer.
56  * @param _value The unsigned long integer.
57  */
58  Numeric::Numeric( unsigned long _value ):
59  mContent( new Rational( _value ) )
60  {}
61 
62  /**
63  * Constructing from a char array.
64  * @param _value The char array.
65  */
66  Numeric::Numeric( const char* _value ):
67  mContent( new Rational( _value ) )
68  {}
69 
70  /**
71  * Copy constructor.
72  * @param _value The Numeric to copy.
73  */
74  Numeric::Numeric( const Numeric& _value ):
75  mContent( new Rational( *_value.mContent ) )
76  {}
77 
79  {
80  delete mContent;
81  }
82 
83  /**
84  * Cast from an integer.
85  * @param _value The integer.
86  * @return The corresponding Numeric.
87  */
88  Numeric& Numeric::operator=( int _value )
89  {
90  return operator=( Numeric( _value ) );
91  }
92 
93  /**
94  * Cast from an unsigned inteeger.
95  * @param _value The unsigned integer.
96  * @return The corresponding Numeric.
97  */
98  Numeric& Numeric::operator=( unsigned int _value )
99  {
100  return operator=( Numeric( _value ) );
101  }
102 
103  /**
104  * Cast from a long integer.
105  * @param _value The long integer.
106  * @return The corresponding Numeric.
107  */
108  Numeric& Numeric::operator=( long _value )
109  {
110  return operator=( Numeric( _value ) );
111  }
112 
113  /**
114  * Cast from an unsigned long integer.
115  * @param _value The unsigned long integer.
116  * @return The corresponding Numeric.
117  */
118  Numeric& Numeric::operator=( unsigned long _value )
119  {
120  return operator=( Numeric( _value ) );
121  }
122 
123  /**
124  * Cast from a char array.
125  * @param _value The char array.
126  * @return The corresponding Numeric.
127  */
128  Numeric& Numeric::operator=( const char* _value )
129  {
130  return operator=( Numeric( _value ) );
131  }
132 
133  /**
134  * Cast from a char array.
135  * @param _value The char array.
136  * @return The corresponding Numeric.
137  */
139  {
140  *mContent = _value.content();
141  return *this;
142  }
143 
144  /**
145  * Compares whether this Numeric and the given one are equal.
146  * @param _value The Numeric to compare with.
147  * @return True, if the two Numerics are equal;
148  * False, otherwise.
149  */
150  bool Numeric::operator==( const Numeric& _value ) const
151  {
152  return this->content() == _value.content();
153  }
154 
155  /**
156  * Compares whether this Numeric and the given one are not equal.
157  * @param _value The Numeric to compare with.
158  * @return True, if the two Numerics are not equal;
159  * False, otherwise.
160  */
161  bool Numeric::operator!=( const Numeric& _value ) const
162  {
163  return this->content() != _value.content();
164  }
165 
166  /**
167  * Compares whether this Numeric is less than the given one.
168  * @param _value The Numeric to compare with.
169  * @return True, if this Numeric is less than the given one;
170  * False, otherwise.
171  */
172  bool Numeric::operator<( const Numeric& _value ) const
173  {
174  return this->content() < _value.content();
175  }
176 
177  /**
178  * Compares whether this Numeric is less or equal than the given one.
179  * @param _value The Numeric to compare with.
180  * @return True, if this Numeric is less or equal than the given one;
181  * False, otherwise.
182  */
183  bool Numeric::operator<=( const Numeric& _value ) const
184  {
185  return this->content() <= _value.content();
186  }
187 
188  /**
189  * Compares whether this Numeric is greater than the given one.
190  * @param _value The Numeric to compare with.
191  * @return True, if this Numeric is greater than the given one;
192  * False, otherwise.
193  */
194  bool Numeric::operator>( const Numeric& _value ) const
195  {
196  return this->content() > _value.content();
197  }
198 
199  /**
200  * Compares whether this Numeric is greater or equal than the given one.
201  * @param _value The Numeric to compare with.
202  * @return True, if this Numeric is greater or equal than the given one;
203  * False, otherwise.
204  */
205  bool Numeric::operator>=( const Numeric& _value ) const
206  {
207  return this->content() >= _value.content();
208  }
209 
210  /**
211  * @return The enumerator of this Numeric.
212  */
214  {
215  return Numeric( carl::get_num( this->content() ) );
216  }
217 
218  /**
219  * @return The denominator of this Numeric.
220  */
222  {
223  return Numeric( carl::get_denom( this->content() ) );
224  }
225 
226  /**
227  * @return The next smaller integer to this Numeric.
228  */
230  {
231  return Numeric( carl::floor( this->content() ) );
232  }
233 
234  /**
235  * Checks whether this Numeric corresponds to a positive rational number.
236  * @return True, if this Numeric corresponds to a positive rational number;
237  * False, otherwise.
238  */
239  bool Numeric::is_positive() const
240  {
241  return ( this->content() > 0 );
242  }
243 
244  /**
245  * @return true, if this Numeric corresponds to a positive rational number;
246  * false, otherwise.
247  */
248  bool Numeric::is_negative() const
249  {
250  return ( this->content() < 0 );
251  }
252 
253  /**
254  * @return true, if this Numeric corresponds to zero;
255  * false, otherwise.
256  */
257  bool Numeric::is_zero() const
258  {
259  return ( this->content() == 0 );
260  }
261 
262  /**
263  * @return true, if this Numeric is integer;
264  * false, otherwise.
265  */
266  bool Numeric::is_integer() const
267  {
268  return ( this->denom() == 1 );
269  }
270 
271  /**
272  * Calculates the absolute value of the given Numeric.
273  * @param _value The Numeric to calculate the Numeric for.
274  * @return The absolute value of the given Numeric.
275  */
276  Numeric abs( const Numeric& _value )
277  {
278  return Numeric( carl::abs( _value.content() ) );
279  }
280 
281  /**
282  * Calculates the result of the first argument modulo the second argument.
283  * Note, that this method can only be applied to integers.
284  * @param _valueA An integer.
285  * @param _valueB An integer != 0.
286  * @return The first argument modulo the second argument.
287  */
288  Numeric mod( const Numeric& _valueA, const Numeric& _valueB )
289  {
290  assert( _valueA.is_integer() && _valueB.is_integer() );
291  assert( !_valueB.is_zero() );
292  return Numeric( carl::mod( carl::get_num( _valueA.content() ), carl::get_num( _valueB.content() ) ) );
293  }
294 
295  /**
296  * Calculates the least common multiple of the two arguments.
297  * Note, that this method can only be applied to integers.
298  * @param _valueA An integer.
299  * @param _valueB An integer.
300  * @return The least common multiple of the two arguments.
301  */
302  Numeric lcm( const Numeric& _valueA, const Numeric& _valueB )
303  {
304  assert( _valueA.is_integer() && _valueB.is_integer() );
305  if( _valueA.is_zero() || _valueB.is_zero() )
306  return Numeric( 0 );
307  return Numeric( carl::lcm( carl::get_num( _valueA.content() ), carl::get_num( _valueB.content() ) ) );
308  }
309 
310  /**
311  * Calculates the greatest common divisor of the two arguments.
312  * Note, that this method can only be applied to integers.
313  * @param _valueA An integer.
314  * @param _valueB An integer.
315  * @return The least common divisor of the two arguments.
316  */
317  Numeric gcd( const Numeric& _valueA, const Numeric& _valueB )
318  {
319  assert( _valueA.is_integer() && _valueB.is_integer() );
320  if( _valueA.is_zero() || _valueB.is_zero() )
321  return Numeric( 0 );
322  return Numeric( carl::gcd( carl::get_num( _valueA.content() ), carl::get_num( _valueB.content() ) ) );
323  }
324 
325  /**
326  * Calculates the sum of the two given Numerics.
327  * @param _valueA The first summand.
328  * @param _valueB The second summand.
329  * @return The sum of the two given Numerics.
330  */
331  Numeric operator+( const Numeric& _valueA, const Numeric& _valueB )
332  {
333  return Numeric( _valueA.content() + _valueB.content() );
334  }
335 
336  /**
337  * Calculates the difference between the two given Numerics.
338  * @param _valueA The minuend.
339  * @param _valueB The subtrahend.
340  * @return The difference between the two given Numerics.
341  */
342  Numeric operator-( const Numeric& _valueA, const Numeric& _valueB )
343  {
344  return Numeric( _valueA.content() - _valueB.content() );
345  }
346 
347  /**
348  * Calculates the product of the two given Numerics.
349  * @param _valueA The first factor.
350  * @param _valueB The second factor.
351  * @return The product of the two given Numerics.
352  */
353  Numeric operator*( const Numeric& _valueA, const Numeric& _valueB )
354  {
355  return Numeric( _valueA.content() * _valueB.content() );
356  }
357 
358  /**
359  * Calculates the division of the two given Numerics.
360  * @param _valueA The dividend.
361  * @param _valueB The divisor.
362  * @return The difference of the two given Numerics.
363  */
364  Numeric operator/( const Numeric& _valueA, const Numeric& _valueB )
365  {
366  return Numeric( _valueA.content() / _valueB.content() );
367  }
368 
369  /**
370  * Adds the value of the second given Numeric to the second given Numeric.
371  * @param _valueA The Numeric to add to.
372  * @param _valueB The Numeric to add.
373  * @return The first given Numeric increased by the second given Numeric.
374  */
375  Numeric& operator+=( Numeric& _valueA, const Numeric& _valueB )
376  {
377  _valueA.rContent() += _valueB.content();
378  return _valueA;
379  }
380 
381  /**
382  * Subtracts the second given Numeric to the first given Numeric.
383  * @param _valueA The Numeric to subtract from.
384  * @param _valueB The Numeric to subtract.
385  * @return The first given Numeric subtracted by the second given Numeric.
386  */
387  Numeric& operator-=( Numeric& _valueA, const Numeric& _valueB )
388  {
389  _valueA.rContent() -= _valueB.content();
390  return _valueA;
391  }
392 
393  /**
394  * Multiplies the second given Numeric to the first given Numeric.
395  * @param _valueA The Numeric to multiply.
396  * @param _valueB The Numeric to multiply by.
397  * @return The first given Numeric multiplied by the second given Numeric.
398  */
399  Numeric& operator*=( Numeric& _valueA, const Numeric& _valueB )
400  {
401  _valueA.rContent() *= _valueB.content();
402  return _valueA;
403  }
404 
405  /**
406  * Divides the first given Numeric by the second given Numeric.
407  * @param _valueA The Numeric to divide.
408  * @param _valueB The Numeric to divide by.
409  * @return The first given Numeric divided by the second given Numeric.
410  */
411  Numeric& operator/=( Numeric& _valueA, const Numeric& _valueB )
412  {
413  _valueA.rContent() /= _valueB.content();
414  return _valueA;
415  }
416 
417  /**
418  * Calculates the additive inverse of the given Numeric.
419  * @param _value The Numeric to calculate the additive inverse for.
420  * @return The additive inverse of the given Numeric.
421  */
422  Numeric operator-( const Numeric& _value )
423  {
424  return Numeric( -_value.content() );
425  }
426 
427  /**
428  * Increments the given Numeric by one.
429  * @param _value The Numeric to increment.
430  * @return The given Numeric incremented by one.
431  */
433  {
434  _value.rContent()++;
435  return _value;
436  }
437 
438  /**
439  * Decrements the given Numeric by one.
440  * @param _value The Numeric to decrement.
441  * @return The given Numeric decremented by one.
442  */
444  {
445  _value.rContent()--;
446  return _value;
447  }
448 
449  /**
450  * Prints the given Numerics representation on the given output stream.
451  * @param _out The output stream to print on.
452  * @param _value The Numeric to print.
453  * @return The output stream after printing the given Numerics representation on it.
454  */
455  std::ostream& operator <<( std::ostream& _out, const Numeric& _value )
456  {
457  _out << _value.content();
458  return _out;
459  }
460  } // end namespace lra
461 } // end namespace smtrat
const Rational & content() const
Definition: Numeric.h:40
Numeric floor() const
Definition: Numeric.cpp:229
bool is_integer() const
Definition: Numeric.cpp:266
bool is_zero() const
Definition: Numeric.cpp:257
bool operator>=(const Numeric &) const
Compares whether this Numeric is greater or equal than the given one.
Definition: Numeric.cpp:205
Numeric numer() const
Definition: Numeric.cpp:213
bool operator<=(const Numeric &) const
Compares whether this Numeric is less or equal than the given one.
Definition: Numeric.cpp:183
Numeric & operator=(int)
Cast from an integer.
Definition: Numeric.cpp:88
Rational & rContent()
Definition: Numeric.h:45
bool is_negative() const
Definition: Numeric.cpp:248
bool operator>(const Numeric &) const
Compares whether this Numeric is greater than the given one.
Definition: Numeric.cpp:194
bool operator<(const Numeric &) const
Compares whether this Numeric is less than the given one.
Definition: Numeric.cpp:172
bool is_positive() const
Checks whether this Numeric corresponds to a positive rational number.
Definition: Numeric.cpp:239
Numeric()
Default constructor.
Definition: Numeric.cpp:18
bool operator==(const Numeric &) const
Compares whether this Numeric and the given one are equal.
Definition: Numeric.cpp:150
Rational * mContent
Definition: Numeric.h:25
bool operator!=(const Numeric &) const
Compares whether this Numeric and the given one are not equal.
Definition: Numeric.cpp:161
Numeric denom() const
Definition: Numeric.cpp:221
Numeric & operator+=(Numeric &_valueA, const Numeric &_valueB)
Adds the value of the second given Numeric to the second given Numeric.
Definition: Numeric.cpp:375
Numeric lcm(const Numeric &_valueA, const Numeric &_valueB)
Calculates the least common multiple of the two arguments.
Definition: Numeric.cpp:302
std::ostream & operator<<(std::ostream &_out, const Numeric &_value)
Prints the given Numerics representation on the given output stream.
Definition: Numeric.cpp:455
Numeric & operator++(Numeric &_value)
Increments the given Numeric by one.
Definition: Numeric.cpp:432
Numeric operator-(const Numeric &_valueA, const Numeric &_valueB)
Calculates the difference between the two given Numerics.
Definition: Numeric.cpp:342
Numeric abs(const Numeric &_value)
Calculates the absolute value of the given Numeric.
Definition: Numeric.cpp:276
Numeric & operator*=(Numeric &_valueA, const Numeric &_valueB)
Multiplies the second given Numeric to the first given Numeric.
Definition: Numeric.cpp:399
Numeric mod(const Numeric &_valueA, const Numeric &_valueB)
Calculates the result of the first argument modulo the second argument.
Definition: Numeric.cpp:288
Numeric operator*(const Numeric &_valueA, const Numeric &_valueB)
Calculates the product of the two given Numerics.
Definition: Numeric.cpp:353
Numeric operator/(const Numeric &_valueA, const Numeric &_valueB)
Calculates the division of the two given Numerics.
Definition: Numeric.cpp:364
Numeric & operator--(Numeric &_value)
Decrements the given Numeric by one.
Definition: Numeric.cpp:443
Numeric operator+(const Numeric &_valueA, const Numeric &_valueB)
Calculates the sum of the two given Numerics.
Definition: Numeric.cpp:331
Numeric & operator-=(Numeric &_valueA, const Numeric &_valueB)
Subtracts the second given Numeric to the first given Numeric.
Definition: Numeric.cpp:387
Numeric gcd(const Numeric &_valueA, const Numeric &_valueB)
Calculates the greatest common divisor of the two arguments.
Definition: Numeric.cpp:317
Numeric & operator/=(Numeric &_valueA, const Numeric &_valueB)
Divides the first given Numeric by the second given Numeric.
Definition: Numeric.cpp:411
Class to create the formulas for axioms.
mpq_class Rational
Definition: types.h:19