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 | |
---|
17 | namespace gfan{ |
---|
18 | class Rational |
---|
19 | { |
---|
20 | mpq_t value; |
---|
21 | public: |
---|
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_init_set_si(mpq_numref(value), value_); |
---|
34 | // mpz_init_set_ui(mpq_denref(value), 1); |
---|
35 | mpz_set_si(mpq_numref(value), value_); |
---|
36 | mpz_set_ui(mpq_denref(value), 1); |
---|
37 | mpq_canonicalize(value); |
---|
38 | } |
---|
39 | Rational(Rational const & value_) |
---|
40 | { |
---|
41 | mpq_init(value); |
---|
42 | mpq_set(value,value_.value); |
---|
43 | } |
---|
44 | Rational(mpq_t value_) |
---|
45 | { |
---|
46 | mpq_init(value); |
---|
47 | mpq_set(value,value_); |
---|
48 | } |
---|
49 | explicit Rational(Integer const & value_) |
---|
50 | { |
---|
51 | mpq_init(value); |
---|
52 | // mpz_init_set(mpq_numref(value), value_.value); |
---|
53 | // mpz_init_set_ui(mpq_denref(value), 1); |
---|
54 | mpz_set(mpq_numref(value), value_.value); |
---|
55 | mpz_set_ui(mpq_denref(value), 1); |
---|
56 | mpq_canonicalize(value); |
---|
57 | } |
---|
58 | ~Rational() |
---|
59 | { |
---|
60 | mpq_clear(value); |
---|
61 | } |
---|
62 | Rational& operator=(const Rational& a) |
---|
63 | { |
---|
64 | const Rational *A=(const Rational*)&a; |
---|
65 | if (this != A) { |
---|
66 | mpq_clear(value); |
---|
67 | mpq_init(value); |
---|
68 | mpq_set(value,a.value); |
---|
69 | } |
---|
70 | return *this; |
---|
71 | } |
---|
72 | bool isZero()const{ |
---|
73 | return mpz_sgn(mpq_numref(value))==0; |
---|
74 | } |
---|
75 | friend std::ostream &operator<<(std::ostream &f, Rational const &a) |
---|
76 | { |
---|
77 | void (*freefunc)(void *, size_t); |
---|
78 | mp_get_memory_functions(0,0,&freefunc); |
---|
79 | char *str=mpq_get_str(0,10,a.value); |
---|
80 | f<<str; |
---|
81 | freefunc(str,strlen(str)+1); |
---|
82 | return f; |
---|
83 | } |
---|
84 | Rational& operator+=(const Rational& a) |
---|
85 | { |
---|
86 | mpq_add(value,value,a.value); |
---|
87 | return *this; |
---|
88 | } |
---|
89 | Rational& operator-=(const Rational& a) |
---|
90 | { |
---|
91 | mpq_sub(value,value,a.value); |
---|
92 | return *this; |
---|
93 | } |
---|
94 | Rational& operator*=(const Rational& a) |
---|
95 | { |
---|
96 | mpq_mul(value,value,a.value); |
---|
97 | return *this; |
---|
98 | } |
---|
99 | Rational& operator/=(const Rational& a) |
---|
100 | { |
---|
101 | assert(!a.isZero()); |
---|
102 | mpq_div(value,value,a.value); |
---|
103 | return *this; |
---|
104 | } |
---|
105 | friend Rational operator-(const Rational &b) |
---|
106 | { |
---|
107 | Rational ret; |
---|
108 | ret-=b; |
---|
109 | return ret; |
---|
110 | } |
---|
111 | Rational operator+(const Rational &a)const |
---|
112 | { |
---|
113 | Rational ret(*this); |
---|
114 | ret+=a; |
---|
115 | return ret; |
---|
116 | } |
---|
117 | Rational operator-(const Rational &a)const |
---|
118 | { |
---|
119 | Rational ret(*this); |
---|
120 | ret-=a; |
---|
121 | return ret; |
---|
122 | } |
---|
123 | Rational operator*(const Rational &a)const |
---|
124 | { |
---|
125 | Rational ret(*this); |
---|
126 | ret*=a; |
---|
127 | return ret; |
---|
128 | } |
---|
129 | Rational operator/(const Rational &a)const |
---|
130 | { |
---|
131 | Rational ret(*this); |
---|
132 | ret/=a; |
---|
133 | return ret; |
---|
134 | } |
---|
135 | void madd(const Rational &a,const Rational &b) |
---|
136 | { |
---|
137 | mpq_t temp; |
---|
138 | mpq_init(temp); |
---|
139 | mpq_mul(temp,a.value,b.value); |
---|
140 | mpq_add(value,value,temp); |
---|
141 | mpq_clear(temp); |
---|
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 | bool operator!=(const Rational &a)const |
---|
152 | { |
---|
153 | return mpq_cmp(value,a.value)!=0; |
---|
154 | } |
---|
155 | /** |
---|
156 | * Returns +1,-1, or 0. |
---|
157 | */ |
---|
158 | int sign()const |
---|
159 | { |
---|
160 | return mpq_sgn(value); |
---|
161 | } |
---|
162 | static Rational gcd(Rational const &a, Rational const &b, Rational &s, Rational &t) |
---|
163 | { |
---|
164 | /* mpz_t r; |
---|
165 | mpz_init(r); |
---|
166 | mpz_gcdext(r,s.value,t.value,a.value,b.value); |
---|
167 | Integer ret(r); |
---|
168 | mpz_clear(r);*/ |
---|
169 | assert(0); |
---|
170 | return a; |
---|
171 | } |
---|
172 | /** |
---|
173 | * Assigns the value to q. q must have been initialized as a gmp variable. |
---|
174 | */ |
---|
175 | void setGmp(mpq_t q)const |
---|
176 | { |
---|
177 | mpq_set(q,value); |
---|
178 | } |
---|
179 | }; |
---|
180 | |
---|
181 | |
---|
182 | } |
---|
183 | |
---|
184 | #endif /* LIB_Q_H_ */ |
---|