Developer documentation

scalar_extended_rational.hpp
Go to the documentation of this file.
1 /********************************************************************
2  * This file is part of the source code of the realroot library.
3  * Author(s): P. Dobrowolski, Warsaw University of Technology, Poland
4  * $Id: scalar_extended_rational.hpp,v 1.1 2012/02/22 20:00:00 pdobrowolski Exp $
5  ********************************************************************/
6 #ifndef REALROOT_SCALAR_EXTENDED_RATIONAL_H_
7 #define REALROOT_SCALAR_EXTENDED_RATIONAL_H_
8 //----------------------------------------------------------------------
9 #include <string>
10 #include <iostream>
11 #include <realroot/extended.hpp>
14 #include <cassert>
15 //======================================================================
16 namespace mmx {
17 //----------------------------------------------------------------------
19 //----------------------------------------------------------------------
20 std::ostream& operator <<(std::ostream& os, const scalar<EMPQ>& b);
21 //----------------------------------------------------------------------
22 template<> inline
23 void scalar<EMPQ>::init() {}
24 //------------------- scalar<T>(...) -------------------------------------
25 template<> inline
26 scalar<EMPQ>::scalar() : data() {}
27 //----------------------------------------------------------------------
28 template<> inline
30 //----------------------------------------------------------------------
31 template<> inline
32 scalar<EMPQ>::scalar(int si) : data(scalar<MPQ>(si)) {}
33 //----------------------------------------------------------------------
34 template<> inline
35 scalar<EMPQ>::scalar(unsigned si) : data(scalar<MPQ>(si)) {}
36 //----------------------------------------------------------------------
37 template<> inline
38 scalar<EMPQ>::scalar(const char *string, unsigned int base) : data(scalar<MPQ>(string, base)) {}
39 //----------------------------------------------------------------------
40 template<> template<class U>
41 inline scalar<EMPQ>::scalar(const scalar<U> &a, const scalar<U> &b, const scalar<U> &c) : data(a, b, c) {}
42 //----------------------------------------------------------------------
43 template<> inline
44 scalar<EMPQ>::scalar(signed long int sl) : data(scalar<MPQ>(sl)) {}
45 //----------------------------------------------------------------------
46 template<> inline
47 scalar<EMPQ>::scalar(unsigned long int ul) : data(scalar<MPQ>(ul)) {}
48 //----------------------------------------------------------------------
49 template<> inline
50 scalar<EMPQ>::scalar(const scalar<EMPQ>& rhs) : data(rhs.rep()) {}
51 //----------------------------------------------------------------------
52 template<> inline
53 scalar<EMPQ>::scalar(double rhs) : data(scalar<MPQ>(rhs)) {}
54 //----------------------------------------------------------------------
55 template<> inline
56 scalar<EMPQ>& scalar<EMPQ>::operator =(const scalar<EMPQ>& rhs) { rep() = rhs.rep(); return *this; }
57 //----------------------------------------------------------------------
58 template<> inline
59 bool scalar<EMPQ>::operator ==(const scalar<EMPQ>& rhs) const { return rep() == rhs.rep(); }
60 
61 template<> inline
62 bool scalar<EMPQ>::operator ==(long sl) const { return rep() == EMPQ(sl); }
63 
64 template<> inline
65 bool scalar<EMPQ>::operator ==(int si) const { return rep() == EMPQ(si); }
66 
67 template<> inline
68 bool scalar<EMPQ>::operator ==(unsigned long ul) const { return rep() == EMPQ(ul); }
69 
70 // != ------------------------------------------------------------------
71 template<> inline
72 bool scalar<EMPQ>::operator !=(const scalar<EMPQ>& rhs) const { return rep() != rhs.rep(); }
73 
74 template<> inline
75 bool scalar<EMPQ>::operator !=(long sl) const { return rep() != EMPQ(sl); }
76 
77 template<> inline
78 bool scalar<EMPQ>::operator !=(int si) const { return rep() != EMPQ(si); }
79 
80 template<> inline
81 bool scalar<EMPQ>::operator !=(unsigned long ul) const { return rep() != EMPQ(ul); }
82 
83 // > -------------------------------------------------------------------
84 template<> inline
85 bool scalar<EMPQ>::operator >(const scalar<EMPQ>& rhs) const { return rep() > rhs.rep(); }
86 
87 template<> inline
88 bool scalar<EMPQ>::operator >(long sl) const { return rep() > EMPQ(sl); }
89 
90 template<> inline
91 bool scalar<EMPQ>::operator >(int si) const { return rep() > EMPQ(si); }
92 
93 template<> inline
94 bool scalar<EMPQ>::operator >(unsigned long ul) const { return rep() > EMPQ(ul); }
95 
96 // <= --------------------------------------------------------------------
97 template<> inline
98 bool scalar<EMPQ>::operator >=(const scalar<EMPQ>& rhs) const { return rep() >= rhs.rep(); }
99 
100 template<> inline
101 bool scalar<EMPQ>::operator >=(long sl) const { return rep() >= EMPQ(sl); }
102 
103 template<> inline
104 bool scalar<EMPQ>::operator >=(int si) const { return rep() >= EMPQ(si); }
105 
106 template<> inline
107 bool scalar<EMPQ>::operator >=(unsigned long ul) const { return rep() >= EMPQ(ul); }
108 
109 // < -------------------------------------------------------------------
110 template<> inline
111 bool scalar<EMPQ>::operator <(const scalar<EMPQ>& rhs) const { return rep() < rhs.rep(); }
112 
113 template<> inline
114 bool scalar<EMPQ>::operator <(long sl) const { return rep() < EMPQ(sl); }
115 
116 template<> inline
117 bool scalar<EMPQ>::operator <(int si) const { return rep() < EMPQ(si); }
118 
119 template<> inline
120 bool scalar<EMPQ>::operator <(unsigned long ul) const { return rep() < EMPQ(ul); }
121 
122 // <= ------------------------------------------------------------------
123 template<> inline
124 bool scalar<EMPQ>::operator <=(const scalar<EMPQ>& rhs) const { return rep() <= rhs.rep(); }
125 
126 template<> inline
127 bool scalar<EMPQ>::operator <=(long sl) const { return rep() <= EMPQ(sl); }
128 
129 template<> inline
130 bool scalar<EMPQ>::operator <=(int si) const { return rep() <= EMPQ(si); }
131 
132 template<> inline
133 bool scalar<EMPQ>::operator <=(unsigned long ul) const { return rep() <= EMPQ(ul); }
134 
135 // = -------------------------------------------------------------------
136 template<> inline
137 scalar<EMPQ>& scalar<EMPQ>::operator =(unsigned long ul) { rep() = EMPQ(ul); return *this;}
138 
139 template<> inline
140 scalar<EMPQ>& scalar<EMPQ>::operator =(long sl) { rep() = EMPQ(sl); return *this;}
141 
142 template<> inline
143 scalar<EMPQ>& scalar<EMPQ>::operator =(int si) { rep() = EMPQ(si); return *this;}
144 
145 // += -------------------------------------------------------------------
146 template<> inline
147 scalar<EMPQ>& scalar<EMPQ>::operator +=(const scalar<EMPQ>& rhs) { rep() += rhs.rep(); return *this; }
148 
149 template<> inline
150 scalar<EMPQ>& scalar<EMPQ>::operator +=(unsigned long ul) { rep() += EMPQ(ul); return *this;}
151 
152 template<> inline
153 scalar<EMPQ>& scalar<EMPQ>::operator +=(long sl) { rep() += EMPQ(sl); return *this;}
154 
155 template<> inline
156 scalar<EMPQ>& scalar<EMPQ>::operator +=(int si) { rep() += EMPQ(si); return *this;}
157 
158 // -= -------------------------------------------------------------------
159 template<> inline
160 scalar<EMPQ>& scalar<EMPQ>::operator -=(const scalar<EMPQ>& rhs) { rep() -= rhs.rep(); return *this; }
161 
162 template<> inline
163 scalar<EMPQ>& scalar<EMPQ>::operator -=(unsigned long ul) { rep() -= EMPQ(ul); return *this;}
164 
165 template<> inline
166 scalar<EMPQ>& scalar<EMPQ>::operator -=(long sl) { rep() -= EMPQ(sl); return *this;}
167 
168 template<> inline
169 scalar<EMPQ>& scalar<EMPQ>::operator -=(int si) { rep() -= EMPQ(si); return *this;}
170 
171 // *= -----------------------------------------------------------------
172 template<> inline
173 scalar<EMPQ>& scalar<EMPQ>::operator *=(const scalar<EMPQ>& rhs) { rep() *= rhs.rep(); return *this; }
174 
175 template<> inline
176 scalar<EMPQ>& scalar<EMPQ>::operator *=(unsigned long ul) { rep() *= EMPQ(ul); return *this;}
177 
178 template<> inline
179 scalar<EMPQ>& scalar<EMPQ>::operator *=(long sl) { rep() *= EMPQ(sl); return *this;}
180 
181 template<> inline
182 scalar<EMPQ>& scalar<EMPQ>::operator *=(int si) { rep() *= EMPQ(si); return *this;}
183 
184 // /= ------------------------------------------------------------------
185 template<> inline
186 scalar<EMPQ>& scalar<EMPQ>::operator /=(const scalar<EMPQ>& rhs) { rep() /= rhs.rep(); return *this; }
187 
188 template<> inline
189 scalar<EMPQ>& scalar<EMPQ>::operator /=(unsigned long ul) { rep() /= EMPQ(ul); return *this;}
190 
191 template<> inline
192 scalar<EMPQ>& scalar<EMPQ>::operator /=(long sl) { rep() /= EMPQ(sl); return *this;}
193 
194 template<> inline
195 scalar<EMPQ>& scalar<EMPQ>::operator /=(int si) { rep() /= EMPQ(si); return *this;}
196 
197 //======================================================================
198 // ARITHMETIC OPERATORS
199 //======================================================================
200 inline
202 {
203  scalar<EMPQ> result;
204  result.rep() = a1.rep() + a2.rep();
205  return result;
206 }
207 //----------------------------------------------------------------------
208 inline
210 {
211  scalar<EMPQ> result;
212  result.rep() = a1.rep() - a2.rep();
213  return result;
214 }
215 //----------------------------------------------------------------------
216 inline
218 {
219  scalar<EMPQ> result;
220  result.rep() = -a1.rep();
221  return result;
222 }
223 //----------------------------------------------------------------------
224 inline
226 {
227  scalar<EMPQ> result;
228  result.rep() = a1.rep() * a2.rep();
229  return result;
230 }
231 
232 inline
234 {
235  scalar<EMPQ> result;
236  EMPQ v(scalar<MPQ>(a2.rep().a), scalar<MPQ>(a2.rep().b), scalar<MPQ>(a2.rep().c));
237  result.rep() = a1.rep() * v;
238  return result;
239 }
240 
241 inline
243 {
244  scalar<EMPQ> result;
245  EMPQ v(scalar<MPQ>(a1.rep().a), scalar<MPQ>(a1.rep().b), scalar<MPQ>(a1.rep().c));
246  result.rep() = v * a2.rep();
247  return result;
248 }
249 
250 inline
252 {
253  scalar<EMPQ> result;
254  result.rep() = a1.rep() * EMPQ(a2);
255  return result;
256 }
257 
258 inline scalar<EMPQ> operator *(const scalar<MPQ>& a1, const scalar<EMPQ>& a2)
259 {
260  scalar<EMPQ> result;
261  result.rep() = EMPQ(a1) * a2.rep();
262  return result;
263 }
264 //--------------------------------------------------------------------
265 inline
267 {
268  return a2 * scalar<EMPQ>(a1);
269 }
270 
271 inline
273 {
274  scalar<EMPQ> result;
275  result.rep() = EMPQ(scalar<MPQ>(a1)) * a2.rep();
276  return result;
277 }
278 
279 inline
281 {
282  scalar<EMPQ> result;
283  result.rep() = a1.rep() / a2.rep();
284  return result;
285 }
286 
287 inline
289 {
290  scalar<EMPQ> result;
291  result.rep() = a1.rep() / EMPQ(a2);
292  return result;
293 }
294 
295 inline
297 {
298  scalar<EMPQ> result;
299  EMPQ v(scalar<MPQ>(a2.rep().a), scalar<MPQ>(a2.rep().b), scalar<MPQ>(a2.rep().c));
300  result.rep() = a1.rep() / v;
301  return result;
302 }
303 //======================================================================
305 template<> inline
307 {
308  ++rep().a;
309 }
310 //----------------------------------------------------------------------
312 template<> inline
314 {
315  --rep().a;
316 }
317 //======================================================================
318 // INPUT OUTPUT
319 //======================================================================
320 inline
321 std::ostream& operator <<(std::ostream& os, const scalar<EMPQ>& b)
322 {
323  os << b.rep().a << " + " << b.rep().b << " * sqrt( " << b.rep().c << " )";
324  return os;
325 }
326 
327 //----------------------------------------------------------------------
328 //inline
329 //std::istream& operator >>(std::istream& is, scalar<EMPQ>& b)
330 //{
331 // INVALID_CALL();
332 // return is;
333 //}
334 //======================================================================
335 // SPECIAL FUNCTIONS
336 //======================================================================
337 template<> inline void
339 {
340  if (*this < 0)
341  *this = -*this;
342 }
343 
344 template<> inline
346 {
347  *this = -*this;
348  return *this;
349 }
350 
351 //-------------------------- compare(...) ------------------------------------
352 inline
353 int compare(const scalar<EMPQ>& b1, const scalar<EMPQ>& b2) { return EMPQ::compare(b1.rep(), b2.rep()); }
354 
355 inline
356 int compare(const scalar<EMPQ>& b, unsigned long ul) { return EMPQ::compare(b.rep(), EMPQ(scalar<MPQ>(ul))); }
357 
358 inline
359 int compare(const scalar<EMPQ>& b, long sl) { return EMPQ::compare(b.rep(), EMPQ(scalar<MPQ>(sl))); }
360 
361 inline
362 int compare(const scalar<EMPQ>& b, int si) { return EMPQ::compare(b.rep(), EMPQ(scalar<MPQ>(si))); }
363 
364 inline
365 scalar<EMPQ> min(const scalar<EMPQ>& a1, const scalar<EMPQ>& a2) { return a1 < a2 ? a1 : a2; }
366 
367 inline
368 scalar<EMPQ> max(const scalar<EMPQ>& a1, const scalar<EMPQ>& a2) { return a1 > a2 ? a1 : a2; }
369 
370 //--------------------------------------------------------------------
371 namespace let
372 {
373 inline void assign(scalar<MPQ>& x, const scalar<EMPQ>& r) { assert(0); }
374 inline void assign(scalar<EMPQ>& x, const scalar<EMPZ>& r)
375 {
376  x.rep() = EMPQ(scalar<MPQ>(r.rep().a),
377  scalar<MPQ>(r.rep().b),
378  scalar<MPQ>(r.rep().c));
379 }
380 }
381 
382 template<typename T,typename F> struct as_helper;
383 
384 template<> struct as_helper<double,scalar<EMPQ> > {
385  static inline double cv(const scalar<EMPQ>& x) {double r; let::assign(r,x); return r;}
386 };
387 
388 
389 template<> struct as_helper<scalar<EMPQ>,scalar<EMPZ> > {
390  static inline scalar<EMPQ> cv(const scalar<EMPZ>& x) {
391  scalar<EMPQ> r;
392  let::assign(r,x);
393  return r;}
394 };
395 } //namespace mmx
396 //======================================================================
397 #endif // REALROOT_SCALAR_EXTENDED_RATIONAL_H_
bool operator>(const scalar< T > &rhs) const
scalar< T > & operator+=(const scalar< T > &rhs)
scalar()
Definition: scalar.hpp:37
const C & b
Definition: Interval_glue.hpp:25
scalar< T > & operator=(const scalar< T > &rhs)
bool operator>=(const scalar< T > &rhs) const
Definition: assign.hpp:48
Definition: extended.hpp:25
T & rep()
Definition: scalar.hpp:30
R & rep(R &r)
Definition: shared_object.hpp:180
extended< NT > operator-(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:132
extended< NT > operator/(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:111
scalar< T > & operator-=(const scalar< T > &rhs)
static scalar< EMPQ > cv(const scalar< EMPZ > &x)
Definition: scalar_extended_rational.hpp:390
int compare(const QQ &a, const QQ &b)
Definition: GMPXX.hpp:71
extended< MPQ > EMPQ
Definition: scalar_extended_rational.hpp:18
T c
Definition: extended.hpp:33
extended< NT > operator+(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:122
static int compare(const Self &lhs, const Self &rhs)
Definition: extended.hpp:63
MP_RAT MPQ
Definition: scalar_rational.hpp:17
bool operator<(const scalar< T > &rhs) const
bool operator<=(const scalar< T > &rhs) const
bool operator!=(const scalar< T > &rhs) const
Interval< T, r > min(const Interval< T, r > &a, const Interval< T, r > &b)
Definition: Interval_fcts.hpp:130
scalar< T > & operator*=(const scalar< T > &rhs)
scalar< T > & negate()
void operator++()
static double cv(const scalar< EMPQ > &x)
Definition: scalar_extended_rational.hpp:385
scalar< T > & operator/=(const scalar< T > &rhs)
bool operator==(const scalar< T > &rhs) const
void operator--()
const C & c
Definition: Interval_glue.hpp:45
T b
Definition: extended.hpp:33
T a
Definition: extended.hpp:33
void assign(A &a, const B &b)
Generic definition of the assignement function.
Definition: assign.hpp:97
Interval< T, r > max(const Interval< T, r > &a, const Interval< T, r > &b)
Definition: Interval_fcts.hpp:135
Definition: array.hpp:12
extended< NT > operator*(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:101
Definition: scalar.hpp:24
~scalar()
Definition: scalar.hpp:55
#define assert(expr, msg)
Definition: shared_object.hpp:57
Home