bigrational.h 7.56 KB
Newer Older
Antoine Lucas's avatar
import  
Antoine Lucas committed
1
2
3
4
5
6
7
8
9
/*! \file bigrational.h
 *  \brief Description of class bigrational
 *
 *  \date Created: 22/05/06
 *  \date Last modified: Time-stamp: <2009-10-27 17:28:06 antoine>
 *
 *  \author Antoine Lucas (adapted from biginteger class made by
 *                         Immanuel Scholz)
 *
10
 *  \note Licence: GPL (>= 2)
Antoine Lucas's avatar
import  
Antoine Lucas committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
 */

#ifndef BIGRATIONAL_HEADER_
#define BIGRATIONAL_HEADER_ 1

#include <string>

#include "biginteger.h"

/**
 * \brief Class for rational value.
 *
 * A big rational. Actually a wrapper for mpq_t to work with plus
 * some special stuff.
 *
 * The bigrational special state "NA" means, no value is assigned.
 * This does not mean, the internal state is not constructed, but
 * the value explicit is "not available".
 */
class bigrational
{

 private:
  /**
   * The rational = n/d .
   */
  mpq_t value ;

  /**
   * \brief a flag: true => NA value.
   */
  bool na;

 public:
  /**
   * Construct a "NA" bigrational.
   */
  bigrational() :
    value(),
    na(true) {mpq_init(value);}

  /**
   * Construct a bigrational from a raw expression.
   * in fact in raw: we will store an mpz_export of
   * either numerator, or denominator
   */
  bigrational(void* raw);

  /**
   * Create a bigrational from a value. Remember to free the
   * parameter's mpz_t if you allocated them by yourself -
   * biginteger will copy the value.
   */
  bigrational(const mpq_t& value_) :
    value(),
    na(false)
    {
      mpq_init(value);
      mpq_set(value, value_);
    }

  /**
   * \brief create a rational from an [big] integer
   */
  bigrational(const mpz_t& value_) :
    value(),
    na(false)
    {
      mpq_init(value);
      mpq_set_z(value, value_);
    }

  /**
   * Construct a bigrational from a long value.
   */
  bigrational(int value_) :
    value(),
    na(false) {
    mpq_init(value);
    if(value_ ==  NA_INTEGER)
      na = true  ;
    else
      mpq_set_si(value, value_,1);
  }

  /**
   * Construct a bigrational from a long value.
   */
  bigrational(int num_, int den_) :
    value(),
    na(false) {
    mpq_init(value);
    if((num_ ==  NA_INTEGER) || (den_ == NA_INTEGER) )
      na = true  ;
    else
      mpq_set_si(value, num_,den_);}

  /**
   * Construct a bigrational from a double value.
   */
  bigrational(double value_) :
    value(),
    na(false)
    {
      mpq_init(value);
      if(R_FINITE( value_ ) )
	mpq_set_d(value, value_);
      else // FIXME: consider  "1/0" and "(-1)/0" for  +- Inf
	na = true  ;
    }

  /**
   * Construct a bigrational from a string value. it can be "4343" or "2322/4343"
   */
  bigrational(const std::string& value_) :
    value(),
    na(false)
    {
      mpq_init(value);
      /* mpz_init.. return -1 when error, 0: ok */
      if(mpq_set_str(value, value_.c_str(), 0))
	na=true;
      /*	if(mpz_init_set_str(value, value_.c_str(), 0) == -1)
		Rf_error("Not a valid number");    */
    }

  /**
   *  Copy constructor (mpz_t aren't standard-copyable)
   */
  bigrational(const bigrational & rhs) :
    value(),
    na(rhs.na)
    {
      mpq_init(value);
      mpq_set(value, rhs.value);
    }

  /**
   * Free the owned mpz_t structs
   */
  virtual ~bigrational() {mpq_clear(value);}

  /** \brief overload affectation operator
   *
   */
  bigrational & operator= (const bigrational& rhs);

  /**
   * Set the bigrational to state "NA".
   */
  void setValue() {mpq_set_si(value, 0,1); na = true;}

  /**
   * Set the bigrational to a specific value.
   */
  void setValue(const mpq_t value_ ) {
    mpq_set(value, value_); na = false;
  }

  /** \brief Set Denominator: return value = value / value_ */
  void setDenValue(const mpq_t value_ ) {
    if(!na)
      mpq_div(value,value, value_);
  }

  /**
   * \brief set value from integer
   */
  void setValue(int value_) {
    if(value_ == NA_INTEGER)
      {mpq_set_ui(value, 0,1); na = true  ;}
    else
      {
	mpq_set_si(value, value_,1);
	na = false;
      }
  }

  /** \brief set value from unsigned int
   */
  void setValue(unsigned long int value_) {
    if((int)value_ == NA_INTEGER)
      {mpq_set_ui(value, 0,1); na = true  ;}
    else
      {mpq_set_ui(value, value_,1); na = false;}
  }

  /** \brief set value from float (double)
   */
  void setValue(double value_) {
    if( R_FINITE (value_ ) )
      {mpq_set_d(value, value_); na = false;}
    else
      {mpq_set_ui(value, 0,1); na = true  ;}
  }


  /**
   * \brief set value from big integer
   */
  void setValue(const mpz_t & value_) {
    mpq_set_z(value,value_);
    na = false;
  }

  /**
   * \brief set value from biginteger value
   */
  void setValue(const biginteger & value_)
    {
      mpq_set_z(value,value_.getValueTemp());
      na = value_.isNA();
    }

  /**
   * \brief set value from biginteger value
   */
  void setValue(const bigrational & value_)
    {
      mpq_set(value,value_.getValueTemp());
      na = value_.isNA();
    }

  /**
   * For const-purposes, return the value. Remember, that the return value
   * only lives as long as this class live, so do not call getValueTemp on
   * temporary objects.
   */
  const mpq_t& getValueTemp() const {return value;}

  /**
   * Return true, if the value is NA.
   */
  bool isNA() const {return na;}

  /**
   * set NA value
   */
  void NA(bool value_p)  {na = value_p;}

  /**
   * Return 1, if the value is > 0;  -1 when negative, 0 when 0.
   */
  int sgn() const {return mpq_sgn(value);}

  /**
   *  Convert the bigrational into a standard string.
   */
  std::string str(int b) const;

  /**
   * \brief Convert the bigrational into a double value
   * (you may loose precision)
   */
  double as_double() const {return mpq_get_d(value);}

  /**
   * Convert the bigrational to a raw memory block. Obtain the size needed
   * from biginteger_raw_size() first and make sure, the buffer provided is
   * large enough to hold the data.
   *
   * Also remember, that the modulus is not saved this way. To obtain a
   * modulus raw byte use get_modulus().as_raw(void*).
   *
   * @return number of bytes used (same as raw_size())
   */
  //  int as_raw(void* raw) const;

  /**
   * Return the number of bytes needed to store the numerator only in a
   * continous memory block.
   */
  size_t raw_size() const;

  /** \brief  simplify n/d (use of mpq_canonical)
   *
   */
  void  simplify ();

  /**
   * \brief  return 1/x
   */
  bigrational inv();


};


/** \brief mpq struct.
 *
 * Use this to clear mpq_t structs at end-of-function automatically
 */
struct mpq_t_sentry {
  /** the bigrational */
  mpq_t& value;
  /** initialisation */
  mpq_t_sentry(mpq_t& v): value(v) {}
  /** free all */
  ~mpq_t_sentry()
  {
    mpq_clear(value);
  }
};


/**
 * \brief set denominator to a bigrational (in fact divide
 * bigrational by value m.
 */
bigrational set_denominator(const bigrational& x, const bigrational& m);


/**
 * \brief Add two bigrationals together.
 *
 */
bigrational operator+(const bigrational& rhs, const bigrational& lhs);

/**
 * \brief Subtract two bigrationals.
 */
bigrational operator-(const bigrational& rhs, const bigrational& lhs);

/**
 * \brief Multiply two bigrationals.
 */
bigrational operator*(const bigrational& rhs, const bigrational& lhs);

/**
 *\brief  Divide two bigrationals.
 */
bigrational operator/(const bigrational& rhs, const bigrational& lhs);

/**
 *\brief  x^y = pow(x,y) :  bigrational ^ biginteger
 */
bigrational operator^(const bigrational& rhs, const biginteger& lhs);


/** \brief comparison operator
 */
bool operator> (const bigrational& rhs, const bigrational& lhs);

/** \brief comparison operator
 */
bool operator< (const bigrational& rhs, const bigrational& lhs);


/** \brief comparison operator
 */
bool operator!= (const bigrational& rhs, const bigrational& lhs);

#endif