File: BigRat.h

package info (click to toggle)
cgal 3.2.1-2
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 47,752 kB
  • ctags: 72,510
  • sloc: cpp: 397,707; ansic: 10,393; sh: 4,232; makefile: 3,713; perl: 394; sed: 9
file content (486 lines) | stat: -rw-r--r-- 12,410 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/****************************************************************************
 * Core Library Version 1.7, August 2004
 * Copyright (c) 1995-2004 Exact Computation Project
 * All rights reserved.
 *
 * This file is part of CORE (http://cs.nyu.edu/exact/core/); you may
 * redistribute it under the terms of the Q Public License version 1.0.
 * See the file LICENSE.QPL distributed with CORE.
 *
 * Licensees holding a valid commercial license may use this file in
 * accordance with the commercial license agreement provided with the
 * software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * File: BigRat.h
 * Synopsis: 
 * 		a wrapper class for mpq from GMP
 * 
 * Written by 
 *       Chee Yap <yap@cs.nyu.edu>
 *       Chen Li <chenli@cs.nyu.edu>
 *       Zilin Du <zilin@cs.nyu.edu>
 *
 * WWW URL: http://cs.nyu.edu/exact/
 * Email: exact@cs.nyu.edu
 *
 * $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.2-branch/Core/include/CORE/BigRat.h $
 * $Id: BigRat.h 29485 2006-03-14 11:52:49Z efif $
 ***************************************************************************/

#ifndef _CORE_BIGRAT_H_
#define _CORE_BIGRAT_H_

#include <CORE/BigInt.h>

CORE_BEGIN_NAMESPACE

class BigRatRep : public RCRepImpl<BigRatRep> {
public:
  BigRatRep() {
    mpq_init(mp);
  }
  BigRatRep(const BigRatRep& z) {
    mpq_init(mp);
    mpq_set(mp, z.mp);
  }
  BigRatRep(signed char c) {
    mpq_init(mp);
    mpq_set_si(mp, c, 1);
  }
  BigRatRep(unsigned char c) {
    mpq_init(mp);
    mpq_set_ui(mp, c, 1);
  }
  BigRatRep(signed int i) {
    mpq_init(mp);
    mpq_set_si(mp, i, 1);
  }
  BigRatRep(unsigned int i) {
    mpq_init(mp);
    mpq_set_ui(mp, i, 1);
  }
  BigRatRep(signed short int s) {
    mpq_init(mp);
    mpq_set_si(mp, s, 1);
  }
  BigRatRep(unsigned short int s) {
    mpq_init(mp);
    mpq_set_ui(mp, s, 1);
  }
  BigRatRep(signed long int l) {
    mpq_init(mp);
    mpq_set_si(mp, l, 1);
  }
  BigRatRep(unsigned long int l) {
    mpq_init(mp);
    mpq_set_ui(mp, l, 1);
  }
  BigRatRep(float f) {
    mpq_init(mp);
    mpq_set_d(mp, f);
  }
  BigRatRep(double d) {
    mpq_init(mp);
    mpq_set_d(mp, d);
  }
  BigRatRep(const char* s) {
    mpq_init(mp);
    mpq_set_str(mp, s, 0);
  }
  BigRatRep(const std::string& s) {
    mpq_init(mp);
    mpq_set_str(mp, s.c_str(), 0);
  }
  explicit BigRatRep(mpq_srcptr q) {
    mpq_init(mp);
    mpq_set(mp, q);
  }
  BigRatRep(mpz_srcptr z) {
    mpq_init(mp);
    mpq_set_z(mp, z);
  }
  BigRatRep(mpz_srcptr n, mpz_srcptr d) {
    mpq_init(mp);
    mpz_set(mpq_numref(mp), n);
    mpz_set(mpq_denref(mp), d);
    mpq_canonicalize(mp);
  }
  ~BigRatRep() {
    mpq_clear(mp);
  }

  CORE_MEMORY(BigRatRep)

  mpq_srcptr get_mp() const {
    return mp;
  }
  mpq_ptr get_mp() {
    return mp;
  }
private:
  mpq_t mp;
}; //BigRatRep

class BigFloat;

typedef RCImpl<BigRatRep> RCBigRat;
class BigRat : public RCBigRat {
public:
  /// \name Constructors
  //@{
  /// default constructor
  BigRat() : RCBigRat(new BigRatRep()) {}
  /// constructor for <tt>signed char</tt>
  BigRat(signed char x) : RCBigRat(new BigRatRep(x)) {}
  /// constructor for <tt>unsigned char</tt>
  BigRat(unsigned char x) : RCBigRat(new BigRatRep(x)) {}
  /// constructor for <tt>signed short int</tt>
  BigRat(signed short int x) : RCBigRat(new BigRatRep(x)) {}
  /// constructor for <tt>unsigned short int</tt>
  BigRat(unsigned short int x) : RCBigRat(new BigRatRep(x)) {}
  /// constructor for <tt>signed int</tt>
  BigRat(signed int x) : RCBigRat(new BigRatRep(x)) {}
  /// constructor for <tt>unsigned int</tt>
  BigRat(unsigned int x) : RCBigRat(new BigRatRep(x)) {}
  /// constructor for <tt>signed long int</tt>
  BigRat(signed long int x) : RCBigRat(new BigRatRep(x)) {}
  /// constructor for <tt>unsigned long int</tt>
  BigRat(unsigned long int x) : RCBigRat(new BigRatRep(x)) {}
  /// constructor for <tt>float</tt>
  BigRat(float x) : RCBigRat(new BigRatRep(x)) {}
  /// constructor for <tt>double</tt>
  BigRat(double x) : RCBigRat(new BigRatRep(x)) {}
  /// constructor for <tt>const char*</tt> with base
  BigRat(const char* s) : RCBigRat(new BigRatRep(s)) {}
  /// constructor for <tt>std::string</tt> with base
  BigRat(const std::string& s) : RCBigRat(new BigRatRep(s)) {}
  /// constructor for <tt>mpq_srcptr</tt>
  explicit BigRat(mpq_srcptr z) : RCBigRat(new BigRatRep(z)) {}
  /// constructor for <tt>BigInt</tt>
  BigRat(const BigInt& z) : RCBigRat(new BigRatRep(z.get_mp())) {}
  /// constructor for two <tt>BigInts</tt>
  BigRat(const BigInt& n, const BigInt& d)
      : RCBigRat(new BigRatRep(n.get_mp(), d.get_mp())) {}
  /// constructor for <tt>BigFloat</tt>
  BigRat(const BigFloat&);
  //@}

  /// \name Copy-Assignment-Destructor
  //@{
  /// copy constructor
  BigRat(const BigRat& rhs) : RCBigRat(rhs) {
    rep->incRef();
  }
  /// assignment operator
  BigRat& operator=(const BigRat& rhs) {
    if (this != &rhs) {
      rep->decRef();
      rep = rhs.rep;
      rep->incRef();
    }
    return *this;
  }
  /// destructor
  ~BigRat() {
    rep->decRef();
  }
  //@}

  /// \name Overloaded operators
  //@{
  BigRat& operator +=(const BigRat& rhs) {
    makeCopy();
    mpq_add(get_mp(), get_mp(), rhs.get_mp());
    return *this;
  }
  BigRat& operator -=(const BigRat& rhs) {
    makeCopy();
    mpq_sub(get_mp(), get_mp(), rhs.get_mp());
    return *this;
  }
  BigRat& operator *=(const BigRat& rhs) {
    makeCopy();
    mpq_mul(get_mp(), get_mp(), rhs.get_mp());
    return *this;
  }
  BigRat& operator /=(const BigRat& rhs) {
    makeCopy();
    mpq_div(get_mp(), get_mp(), rhs.get_mp());
    return *this;
  }
  BigRat& operator <<=(unsigned long ul) {
    makeCopy();
    mpq_mul_2exp(get_mp(), get_mp(), ul);
    return *this;
  }
  BigRat& operator >>=(unsigned long ul) {
    makeCopy();
    mpq_div_2exp(get_mp(), get_mp(), ul);
    return *this;
  }
  //@}

  /// \name div2, unary, increment, decrement operators
  //@{

  /// exact division by 2 (this method is provided for compatibility)
  BigRat div2() const {
    BigRat r; BigRat t(2);     // probably not most efficient way
    mpq_div(r.get_mp(), get_mp(), t.get_mp());
    return r;
  }
  BigRat operator+() const {
    return BigRat(*this);
  }
  BigRat operator-() const {
    BigRat r;
    mpq_neg(r.get_mp(), get_mp());
    return r;
  }
  BigRat& operator++() {
    makeCopy();
    mpz_add(get_num_mp(),get_num_mp(),get_den_mp());
    return *this;
  }
  BigRat& operator--() {
    makeCopy();
    mpz_sub(get_num_mp(),get_num_mp(),get_den_mp());
    return *this;
  }
  BigRat operator++(int) {
    BigRat r(*this);
    ++(*this);
    return r;
  }
  BigRat operator--(int) {
    BigRat r(*this);
    --(*this);
    return r;
  }
  //@}

  /// \name Helper Functions
  //@{
  /// Canonicalize
  void canonicalize() {
    makeCopy();
    mpq_canonicalize(get_mp());
  } 
  /// Has Exact Division
  static bool hasExactDivision() {
    return true;
  }

  /// return mpz pointer of numerator (const)
  mpz_srcptr get_num_mp() const {
    return mpq_numref(get_mp());
  }
  /// return mpz pointer of numerator
  mpz_ptr get_num_mp() {
    return mpq_numref(get_mp());
  }
  /// return mpz pointer of denominator
  mpz_srcptr get_den_mp() const {
    return mpq_denref(get_mp());
  }
  /// return mpz pointer of denominator
  mpz_ptr get_den_mp() {
    return mpq_denref(get_mp());
  }

  /// get mpq pointer (const)
  mpq_srcptr get_mp() const {
    return rep->get_mp();
  }
  /// get mpq pointer
  mpq_ptr get_mp() {
    return rep->get_mp();
  }
  //@}

  /// \name String Conversion Functions
  //@{
  /// set value from <tt>const char*</tt>
  int set_str(const char* s, int base = 0) {
    makeCopy();
    return mpq_set_str(get_mp(), s, base);
  }
  /// convert to <tt>std::string</tt>
  std::string get_str(int base = 10) const {
    int n = mpz_sizeinbase(mpq_numref(get_mp()), base) + mpz_sizeinbase(mpq_denref(get_mp()), base)+ 3;
    char *buffer = new char[n];
    mpq_get_str(buffer, base, get_mp());
    std::string result(buffer);
    delete [] buffer;
    return result;
  }
  //@}

  /// \name Conversion Functions
  //@{
  /// intValue
  int intValue() const {
    return static_cast<int>(doubleValue());
  }
  /// longValue
  long longValue() const {
    return static_cast<long>(doubleValue());
  }
  /// doubleValue
  double doubleValue() const {
    return mpq_get_d(get_mp());
  }
  /// BigIntValue
  BigInt BigIntValue() const {
    BigInt r;
    mpz_tdiv_q(r.get_mp(), get_num_mp(), get_den_mp());
    return r;
  }
  //@}
}; //BigRat class

inline BigRat operator+(const BigRat& a, const BigRat& b) {
  BigRat r;
  mpq_add(r.get_mp(), a.get_mp(), b.get_mp());
  return r;
}
inline BigRat operator-(const BigRat& a, const BigRat& b) {
  BigRat r;
  mpq_sub(r.get_mp(), a.get_mp(), b.get_mp());
  return r;
}
inline BigRat operator*(const BigRat& a, const BigRat& b) {
  BigRat r;
  mpq_mul(r.get_mp(), a.get_mp(), b.get_mp());
  return r;
}
inline BigRat operator/(const BigRat& a, const BigRat& b) {
  BigRat r;
  mpq_div(r.get_mp(), a.get_mp(), b.get_mp());
  return r;
}
// Chee (3/19/2004):
//   The following definitions of div_exact(x,y) and gcd(x,y)
//   ensures that in Polynomial<NT>
/// divisible(x,y) = "x | y"
inline BigRat div_exact(const BigRat& x, const BigRat& y) {
	BigRat z;
	mpq_div(z.get_mp(), x.get_mp(), y.get_mp());
	return z;
}
/// numerator
inline BigInt numerator(const BigRat& a) {
  return BigInt(a.get_num_mp());
}
/// denominator
inline BigInt denominator(const BigRat& a) {
  return BigInt(a.get_den_mp());
}

inline BigRat gcd(const BigRat& x, const BigRat& y) {
  //	return BigRat(1);  // Remark: we may want replace this by
			   // the definition of gcd of a quotient field
			   // of a UFD [Yap's book, Chap.3]
  //Here is one possible definition: gcd of x and y is just the
  //gcd of the numerators of x and y divided by the gcd of the
  //denominators of x and y.
  BigInt n = gcd(numerator(x), numerator(y));
  BigInt d = gcd(denominator(x), denominator(y));
  return BigRat(n,d);
		
}
// Chee: 8/8/2004: need isDivisible to compile Polynomial<BigRat>
// A trivial implementation is to return true always. But this
// caused tPolyRat to fail.
// So we follow the definition of
// Expr::isDivisible(e1, e2) which checks if e1/e2 is an integer.  
inline bool isInteger(const BigRat& x) {
  return BigInt(x.get_den_mp()) == 1;
}
inline bool isDivisible(const BigRat& x, const BigRat& y) {
  BigRat r;
  mpq_div(r.get_mp(), x.get_mp(), y.get_mp());
  return isInteger(r);
}
inline BigRat operator<<(const BigRat& a, unsigned long ul) {
  BigRat r;
  mpq_mul_2exp(r.get_mp(), a.get_mp(), ul);
  return r;
}
inline BigRat operator>>(const BigRat& a, unsigned long ul) {
  BigRat r;
  mpq_div_2exp(r.get_mp(), a.get_mp(), ul);
  return r;
}

inline int cmp(const BigRat& x, const BigRat& y) {
  return mpq_cmp(x.get_mp(), y.get_mp());
}
inline bool operator==(const BigRat& a, const BigRat& b) {
  return cmp(a, b) == 0;
}
inline bool operator!=(const BigRat& a, const BigRat& b) {
  return cmp(a, b) != 0;
}
inline bool operator>=(const BigRat& a, const BigRat& b) {
  return cmp(a, b) >= 0;
}
inline bool operator>(const BigRat& a, const BigRat& b) {
  return cmp(a, b) > 0;
}
inline bool operator<=(const BigRat& a, const BigRat& b) {
  return cmp(a, b) <= 0;
}
inline bool operator<(const BigRat& a, const BigRat& b) {
  return cmp(a, b) < 0;
}

inline std::ostream& operator<<(std::ostream& o, const BigRat& x) {
  return CORE::operator<<(o, x.get_mp());
}
inline std::istream& operator>>(std::istream& i, BigRat& x) {
  x.makeCopy();
  return CORE::operator>>(i, x.get_mp());
}

/// sign
inline int sign(const BigRat& a) {
  return mpq_sgn(a.get_mp());
}
/// abs
inline BigRat abs(const BigRat& a) {
  BigRat r;
  mpq_abs(r.get_mp(), a.get_mp());
  return r;
}
/// neg
inline BigRat neg(const BigRat& a) {
  BigRat r;
  mpq_neg(r.get_mp(), a.get_mp());
  return r;
}
/// div2
inline BigRat div2(const BigRat& a) {
  BigRat r(a);
  return r.div2();
}
/// longValue
inline long longValue(const BigRat& a) {
  return a.longValue();
}
/// doubleValue
inline double doubleValue(const BigRat& a) {
  return a.doubleValue();
}
/// return BigInt value
inline BigInt BigIntValue(const BigRat& a) {
  return a.BigIntValue();
}

CORE_END_NAMESPACE
#endif // _CORE_BIGRAT_H_