source: git/gfanlib/gfanlib_q.h @ ba5e9e

spielwiese
Last change on this file since ba5e9e was 26b713, checked in by Yue Ren <ren@…>, 11 years ago
chg: updated gfanlib package to newest version in master
  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * lib_q.h
3 *
4 *  Created on: Sep 29, 2010
5 *      Author: anders
6 */
7
8#ifndef LIB_Q_H_
9#define LIB_Q_H_
10
11#include <string.h>
12#include <ostream>
13#include "gmp.h"
14
15#include "gfanlib_z.h"
16
17namespace gfan{
18class Rational
19{
20  mpq_t value;
21public:
22  static bool isField()
23  {
24    return true;
25  }
26  Rational()
27  {
28    mpq_init(value);
29  }
30  Rational(signed long int value_)
31  {
32    mpq_init(value);
33    mpz_set_si(mpq_numref(value), value_);
34    mpz_set_ui(mpq_denref(value), 1);
35    mpq_canonicalize(value);
36  }
37  Rational(Rational const & value_)
38  {
39    mpq_init(value);
40    mpq_set(value,value_.value);
41  }
42  Rational(mpq_t value_)
43  {
44    mpq_init(value);
45    mpq_set(value,value_);
46  }
47  explicit Rational(Integer const & value_)
48  {
49    mpq_init(value);
50    mpz_set(mpq_numref(value), value_.value);
51    mpz_set_ui(mpq_denref(value), 1);
52    mpq_canonicalize(value);
53  }
54  ~Rational()
55  {
56    mpq_clear(value);
57  }
58  Rational& operator=(const Rational& a)
59    {
60      const Rational *A=(const Rational*)&a;
61      if (this != A) {
62        mpq_clear(value);
63        mpq_init(value);
64        mpq_set(value,a.value);
65      }
66      return *this;
67    }
68  bool isZero()const{
69    return mpz_sgn(mpq_numref(value))==0;
70  }
71  friend std::ostream &operator<<(std::ostream &f, Rational const &a)
72  {
73    void (*freefunc)(void *, size_t);
74    mp_get_memory_functions(0,0,&freefunc);
75    char *str=mpq_get_str(0,10,a.value);
76    f<<str;
77    freefunc(str,strlen(str)+1);
78    return f;
79  }
80  Rational& operator+=(const Rational& a)
81    {
82      mpq_add(value,value,a.value);
83      return *this;
84    }
85  Rational& operator-=(const Rational& a)
86    {
87      mpq_sub(value,value,a.value);
88      return *this;
89    }
90  Rational& operator*=(const Rational& a)
91    {
92      mpq_mul(value,value,a.value);
93      return *this;
94    }
95  Rational& operator/=(const Rational& a)
96    {
97      assert(!a.isZero());
98      mpq_div(value,value,a.value);
99      return *this;
100    }
101  friend Rational operator-(const Rational &b)
102  {
103    Rational ret;
104    ret-=b;
105    return ret;
106  }
107  Rational operator+(const Rational &a)const
108  {
109    Rational ret(*this);
110    ret+=a;
111    return ret;
112  }
113  Rational operator-(const Rational &a)const
114  {
115    Rational ret(*this);
116    ret-=a;
117    return ret;
118  }
119  Rational operator*(const Rational &a)const
120  {
121    Rational ret(*this);
122    ret*=a;
123    return ret;
124  }
125  Rational operator/(const Rational &a)const
126  {
127    Rational ret(*this);
128    ret/=a;
129    return ret;
130  }
131  void madd(const Rational &a,const Rational &b)
132    {
133      mpq_t temp;
134      mpq_init(temp);
135      mpq_mul(temp,a.value,b.value);
136      mpq_add(value,value,temp);
137      mpq_clear(temp);
138    }
139  bool operator<(const Rational &a)const
140  {
141    return mpq_cmp(value,a.value)<0;
142  }
143  bool operator==(const Rational &a)const
144  {
145    return mpq_cmp(value,a.value)==0;
146  }
147  bool operator!=(const Rational &a)const
148  {
149    return mpq_cmp(value,a.value)!=0;
150  }
151  /**
152   * Returns +1,-1, or 0.
153   */
154  int sign()const
155  {
156    return mpq_sgn(value);
157  }
158  static Rational gcd(Rational const &a, Rational const &b, Rational &s, Rational &t)
159  {
160/*    mpz_t r;
161    mpz_init(r);
162    mpz_gcdext(r,s.value,t.value,a.value,b.value);
163    Integer ret(r);
164    mpz_clear(r);*/
165    assert(0);
166    return a;
167  }
168  /**
169   * Assigns the value to q. q must have been initialized as a gmp variable.
170   */
171  void setGmp(mpq_t q)const
172  {
173    mpq_set(q,value);
174  }
175};
176
177
178}
179
180#endif /* LIB_Q_H_ */
Note: See TracBrowser for help on using the repository browser.