Developer documentation

scalar_extended_integer.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_integer.hpp,v 1.1 2012/02/22 20:00:00 pdobrowolski Exp $
5  ********************************************************************/
6 #ifndef REALROOT_SCALAR_EXTENDED_INTEGER_H_
7 #define REALROOT_SCALAR_EXTENDED_INTEGER_H_
8 //----------------------------------------------------------------------
9 #include <string>
10 #include <iostream>
11 #include <realroot/extended.hpp>
13 #include <cassert>
14 //======================================================================
15 namespace mmx {
16 //----------------------------------------------------------------------
18 //----------------------------------------------------------------------
19 std::ostream& operator <<(std::ostream& os, const scalar<EMPZ>& b);
20 //----------------------------------------------------------------------
21 template<> inline
22 void scalar<EMPZ>::init() {}
23 //------------------- scalar<T>(...) -------------------------------------
24 template<> inline
25 scalar<EMPZ>::scalar() : data() {}
26 //----------------------------------------------------------------------
27 template<> inline
29 //----------------------------------------------------------------------
30 template<> inline
31 scalar<EMPZ>::scalar(int si) : data(scalar<MPZ>(si)) {}
32 //----------------------------------------------------------------------
33 template<> inline
34 scalar<EMPZ>::scalar(unsigned si) : data(scalar<MPZ>(si)) {}
35 //----------------------------------------------------------------------
36 template<> inline
37 scalar<EMPZ>::scalar(const char *string, unsigned int base) : data(scalar<MPZ>(string, base)) {}
38 //----------------------------------------------------------------------
39 template<> template<class U>
40 inline scalar<EMPZ>::scalar(const scalar<U> &a, const scalar<U> &b, const scalar<U> &c) : data(a, b, c) {}
41 //----------------------------------------------------------------------
42 template<> inline
43 scalar<EMPZ>::scalar(signed long int sl) : data(scalar<MPZ>(sl)) {}
44 //----------------------------------------------------------------------
45 template<> inline
46 scalar<EMPZ>::scalar(unsigned long int ul) : data(scalar<MPZ>(ul)) {}
47 //----------------------------------------------------------------------
48 template<> inline
49 scalar<EMPZ>::scalar(const scalar<EMPZ>& rhs) : data(rhs.rep()) {}
50 //----------------------------------------------------------------------
51 template<> inline
52 scalar<EMPZ>::scalar(double rhs) : data(scalar<MPZ>(rhs)) {}
53 //----------------------------------------------------------------------
54 template<> inline
55 scalar<EMPZ>& scalar<EMPZ>::operator =(const scalar<EMPZ>& rhs) { rep() = rhs.rep(); return *this; }
56 //----------------------------------------------------------------------
57 template<> inline
58 bool scalar<EMPZ>::operator ==(const scalar<EMPZ>& rhs) const { return rep() == rhs.rep(); }
59 
60 template<> inline
61 bool scalar<EMPZ>::operator ==(long sl) const { return rep() == EMPZ(sl); }
62 
63 template<> inline
64 bool scalar<EMPZ>::operator ==(int si) const { return rep() == EMPZ(si); }
65 
66 template<> inline
67 bool scalar<EMPZ>::operator ==(unsigned long ul) const { return rep() == EMPZ(ul); }
68 
69 // != ------------------------------------------------------------------
70 template<> inline
71 bool scalar<EMPZ>::operator !=(const scalar<EMPZ>& rhs) const { return rep() != rhs.rep(); }
72 
73 template<> inline
74 bool scalar<EMPZ>::operator !=(long sl) const { return rep() != EMPZ(sl); }
75 
76 template<> inline
77 bool scalar<EMPZ>::operator !=(int si) const { return rep() != EMPZ(si); }
78 
79 template<> inline
80 bool scalar<EMPZ>::operator !=(unsigned long ul) const { return rep() != EMPZ(ul); }
81 
82 // > -------------------------------------------------------------------
83 template<> inline
84 bool scalar<EMPZ>::operator >(const scalar<EMPZ>& rhs) const { return rep() > rhs.rep(); }
85 
86 template<> inline
87 bool scalar<EMPZ>::operator >(long sl) const { return rep() > EMPZ(sl); }
88 
89 template<> inline
90 bool scalar<EMPZ>::operator >(int si) const { return rep() > EMPZ(si); }
91 
92 template<> inline
93 bool scalar<EMPZ>::operator >(unsigned long ul) const { return rep() > EMPZ(ul); }
94 
95 // <= --------------------------------------------------------------------
96 template<> inline
97 bool scalar<EMPZ>::operator >=(const scalar<EMPZ>& rhs) const { return rep() >= rhs.rep(); }
98 
99 template<> inline
100 bool scalar<EMPZ>::operator >=(long sl) const { return rep() >= EMPZ(sl); }
101 
102 template<> inline
103 bool scalar<EMPZ>::operator >=(int si) const { return rep() >= EMPZ(si); }
104 
105 template<> inline
106 bool scalar<EMPZ>::operator >=(unsigned long ul) const { return rep() >= EMPZ(ul); }
107 
108 // < -------------------------------------------------------------------
109 template<> inline
110 bool scalar<EMPZ>::operator <(const scalar<EMPZ>& rhs) const { return rep() < rhs.rep(); }
111 
112 template<> inline
113 bool scalar<EMPZ>::operator <(long sl) const { return rep() < EMPZ(sl); }
114 
115 template<> inline
116 bool scalar<EMPZ>::operator <(int si) const { return rep() < EMPZ(si); }
117 
118 template<> inline
119 bool scalar<EMPZ>::operator <(unsigned long ul) const { return rep() < EMPZ(ul); }
120 
121 // <= ------------------------------------------------------------------
122 template<> inline
123 bool scalar<EMPZ>::operator <=(const scalar<EMPZ>& rhs) const { return rep() <= rhs.rep(); }
124 
125 template<> inline
126 bool scalar<EMPZ>::operator <=(long sl) const { return rep() <= EMPZ(sl); }
127 
128 template<> inline
129 bool scalar<EMPZ>::operator <=(int si) const { return rep() <= EMPZ(si); }
130 
131 template<> inline
132 bool scalar<EMPZ>::operator <=(unsigned long ul) const { return rep() <= EMPZ(ul); }
133 
134 // = -------------------------------------------------------------------
135 template<> inline
136 scalar<EMPZ>& scalar<EMPZ>::operator =(unsigned long ul) { rep() = EMPZ(ul); return *this;}
137 
138 template<> inline
139 scalar<EMPZ>& scalar<EMPZ>::operator =(long sl) { rep() = EMPZ(sl); return *this;}
140 
141 template<> inline
142 scalar<EMPZ>& scalar<EMPZ>::operator =(int si) { rep() = EMPZ(si); return *this;}
143 
144 // += -------------------------------------------------------------------
145 template<> inline
146 scalar<EMPZ>& scalar<EMPZ>::operator +=(const scalar<EMPZ>& rhs) { rep() += rhs.rep(); return *this; }
147 
148 template<> inline
149 scalar<EMPZ>& scalar<EMPZ>::operator +=(unsigned long ul) { rep() += EMPZ(ul); return *this;}
150 
151 template<> inline
152 scalar<EMPZ>& scalar<EMPZ>::operator +=(long sl) { rep() += EMPZ(sl); return *this;}
153 
154 template<> inline
155 scalar<EMPZ>& scalar<EMPZ>::operator +=(int si) { rep() += EMPZ(si); return *this;}
156 
157 // -= -------------------------------------------------------------------
158 template<> inline
159 scalar<EMPZ>& scalar<EMPZ>::operator -=(const scalar<EMPZ>& rhs) { rep() -= rhs.rep(); return *this; }
160 
161 template<> inline
162 scalar<EMPZ>& scalar<EMPZ>::operator -=(unsigned long ul) { rep() -= EMPZ(ul); return *this;}
163 
164 template<> inline
165 scalar<EMPZ>& scalar<EMPZ>::operator -=(long sl) { rep() -= EMPZ(sl); return *this;}
166 
167 template<> inline
168 scalar<EMPZ>& scalar<EMPZ>::operator -=(int si) { rep() -= EMPZ(si); return *this;}
169 
170 // *= -----------------------------------------------------------------
171 template<> inline
172 scalar<EMPZ>& scalar<EMPZ>::operator *=(const scalar<EMPZ>& rhs) { rep() *= rhs.rep(); return *this; }
173 
174 template<> inline
175 scalar<EMPZ>& scalar<EMPZ>::operator *=(unsigned long ul) { rep() *= EMPZ(ul); return *this;}
176 
177 template<> inline
178 scalar<EMPZ>& scalar<EMPZ>::operator *=(long sl) { rep() *= EMPZ(sl); return *this;}
179 
180 template<> inline
181 scalar<EMPZ>& scalar<EMPZ>::operator *=(int si) { rep() *= EMPZ(si); return *this;}
182 
183 // /= ------------------------------------------------------------------
184 template<> inline
185 scalar<EMPZ>& scalar<EMPZ>::operator /=(const scalar<EMPZ>& rhs) { rep() /= rhs.rep(); return *this; }
186 
187 template<> inline
188 scalar<EMPZ>& scalar<EMPZ>::operator /=(unsigned long ul) { rep() /= EMPZ(ul); return *this;}
189 
190 template<> inline
191 scalar<EMPZ>& scalar<EMPZ>::operator /=(long sl) { rep() /= EMPZ(sl); return *this;}
192 
193 template<> inline
194 scalar<EMPZ>& scalar<EMPZ>::operator /=(int si) { rep() /= EMPZ(si); return *this;}
195 
196 //======================================================================
197 // ARITHMETIC OPERATORS
198 //======================================================================
199 inline
201 {
202  scalar<EMPZ> result;
203  result.rep() = a1.rep() + a2.rep();
204  return result;
205 }
206 //----------------------------------------------------------------------
207 inline
209 {
210  scalar<EMPZ> result;
211  result.rep() = a1.rep() - a2.rep();
212  return result;
213 }
214 //----------------------------------------------------------------------
215 inline
217 {
218  scalar<EMPZ> result;
219  result.rep() = -a1.rep();
220  return result;
221 }
222 //----------------------------------------------------------------------
223 inline
225 {
226  scalar<EMPZ> result;
227  result.rep() = a1.rep() * a2.rep();
228  return result;
229 }
230 
231 inline
233 {
234  scalar<EMPZ> result;
235  result.rep() = a1.rep() * EMPZ(a2);
236  return result;
237 }
238 
239 inline scalar<EMPZ> operator *(const scalar<MPZ>& a1, const scalar<EMPZ>& a2)
240 {
241  scalar<EMPZ> result;
242  result.rep() = EMPZ(a1) * a2.rep();
243  return result;
244 }
245 //--------------------------------------------------------------------
246 inline
248 {
249  return a2 * scalar<EMPZ>(a1);
250 }
251 
252 inline
254 {
255  scalar<EMPZ> result;
256  result.rep() = EMPZ(scalar<MPZ>(a1)) * a2.rep();
257  return result;
258 }
259 
260 inline
262 {
263  scalar<EMPZ> result;
264  result.rep() = a1.rep() / a2.rep();
265  return result;
266 }
267 
268 inline
270 {
271  scalar<EMPZ> result;
272  result.rep() = a1.rep() / EMPZ(a2);
273  return result;
274 }
275 //======================================================================
277 template<> inline
279 {
280  ++rep().a;
281 }
282 //----------------------------------------------------------------------
284 template<> inline
286 {
287  --rep().a;
288 }
289 //======================================================================
290 // INPUT OUTPUT
291 //======================================================================
292 inline
293 std::ostream& operator <<(std::ostream& os, const scalar<EMPZ>& b)
294 {
295  os << b.rep().a << " + " << b.rep().b << " * sqrt( " << b.rep().c << " )";
296  return os;
297 }
298 
299 template<class OSTREAM> inline void
300 print(OSTREAM& os, const scalar<EMPZ>& b) {
301  os << b.rep().a << " + " << b.rep().b << " * sqrt( " << b.rep().c << " )";
302 }
303 
304 //----------------------------------------------------------------------
305 //inline
306 //std::istream& operator >>(std::istream& is, scalar<EMPZ>& b)
307 //{
308 // INVALID_CALL();
309 // return is;
310 //}
311 //======================================================================
312 // SPECIAL FUNCTIONS
313 //======================================================================
314 template<> inline void
316 {
317  if (*this < 0)
318  *this = -*this;
319 }
320 
321 template<> inline
323 {
324  *this = -*this;
325  return *this;
326 }
327 
328 inline
329 scalar<EMPZ> operator << (const scalar<EMPZ>& x, long int s) {
330  scalar<EMPZ> r;
331  r.rep().a <<= s;
332  r.rep().b <<= s;
333  return r;
334 }
335 //----------------------------------------------------------------------
336 inline
337 scalar<EMPZ>& operator <<= (scalar<EMPZ>& x, long int s)
338 {
339  x.rep().a <<= s;
340  x.rep().b <<= s;
341  return x;
342 }
343 
344 //-------------------------- compare(...) ------------------------------------
345 inline
346 int compare(const scalar<EMPZ>& b1, const scalar<EMPZ>& b2) { return EMPZ::compare(b1.rep(), b2.rep()); }
347 
348 inline
349 int compare(const scalar<EMPZ>& b, unsigned long ul) { return EMPZ::compare(b.rep(), EMPZ(scalar<MPZ>(ul))); }
350 
351 inline
352 int compare(const scalar<EMPZ>& b, long sl) { return EMPZ::compare(b.rep(), EMPZ(scalar<MPZ>(sl))); }
353 
354 inline
355 int compare(const scalar<EMPZ>& b, int si) { return EMPZ::compare(b.rep(), EMPZ(scalar<MPZ>(si))); }
356 
357 inline
358 scalar<EMPZ> min(const scalar<EMPZ>& a1, const scalar<EMPZ>& a2) { return a1 < a2 ? a1 : a2; }
359 
360 inline
361 scalar<EMPZ> max(const scalar<EMPZ>& a1, const scalar<EMPZ>& a2) { return a1 > a2 ? a1 : a2; }
362 
363 //--------------------------------------------------------------------
364 inline long int bit_size(const scalar<EMPZ>& z) { return std::max(bit_size(z.rep().a), bit_size(z.rep().b)); }
365 
366 namespace let
367 {
368 inline void assign(scalar<MPZ>& x, const scalar<EMPZ>& r) { assert(0); }
369 }
370 
371 template<typename T,typename F> struct as_helper;
372 
373 template<> struct as_helper<double,scalar<EMPZ> > {
374  static inline double cv(const scalar<EMPZ>& x) {double r; let::assign(r,x); return r;}
375 };
376 } //namespace mmx
377 //======================================================================
378 #endif // REALROOT_SCALAR_EXTENDED_INTEGER_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
scalar< EMPZ > max(const scalar< EMPZ > &a1, const scalar< EMPZ > &a2)
Definition: scalar_extended_integer.hpp:361
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)
int compare(const QQ &a, const QQ &b)
Definition: GMPXX.hpp:71
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
bool operator<(const scalar< T > &rhs) const
static double cv(const scalar< EMPZ > &x)
Definition: scalar_extended_integer.hpp:374
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)
long int bit_size(const ZZ &z)
Definition: GMPXX.hpp:32
scalar< T > & negate()
void operator++()
extended< MPZ > EMPZ
Definition: scalar_extended_integer.hpp:17
scalar< T > & operator/=(const scalar< T > &rhs)
void print(OSTREAM &os, const Interval< T, r > &a)
Definition: Interval.hpp:135
bool operator==(const scalar< T > &rhs) const
MP_INT MPZ
Definition: scalar_integer.hpp:18
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