APRONXX  0.9.12
/home/mine/apron/apronxx/apxx_coeff_inline.hh
Go to the documentation of this file.
1 /* -*- C++ -*-
2  * apxx_coeff_inline.hh
3  *
4  * APRON Library / C++ inline functions
5  *
6  * DO NOT INCLUDE THIS FILE DIRECTLY
7  *
8  * Copyright (C) Antoine Mine' 2007
9  *
10  */
11 /* This file is part of the APRON Library, released under LGPL license
12  with an exception allowing the redistribution of statically linked
13  executables.
14 
15  Please read the COPYING file packaged in the distribution.
16 */
17 
18 
19 /* ================================= */
20 /* coeff */
21 /* ================================= */
22 
23 
24 /* constructors */
25 /* ============ */
26 
27 inline coeff::coeff()
28 {
29  ap_coeff_init(&c, AP_COEFF_SCALAR);
30 }
31 
32 inline coeff::coeff(const coeff& x)
33 {
34  ap_coeff_init(&c, x.c.discr);
35  ap_coeff_set(&c, const_cast<ap_coeff_t*>(x.get_ap_coeff_t()));
36 }
37 
38 
39 /* scalars */
40 
41 inline coeff::coeff(const scalar& x)
42 {
43  ap_coeff_init(&c, AP_COEFF_SCALAR);
44  ap_coeff_set_scalar(&c, const_cast<ap_scalar_t*>(x.get_ap_scalar_t()));
45 }
46 
47 inline coeff::coeff(int x)
48 {
49  ap_coeff_init(&c, AP_COEFF_SCALAR);
50  ap_coeff_set_scalar_int(&c, x);
51 }
52 
53 inline coeff::coeff(long x)
54 {
55  ap_coeff_init(&c, AP_COEFF_SCALAR);
56  ap_coeff_set_scalar_int(&c, x);
57 }
58 
59 inline coeff::coeff(double x)
60 {
61  ap_coeff_init(&c, AP_COEFF_SCALAR);
62  ap_coeff_set_scalar_double(&c, x);
63 }
64 
65 inline coeff::coeff(const frac& x)
66 {
67  ap_coeff_init(&c, AP_COEFF_SCALAR);
68  ap_coeff_set_scalar_frac(&c, x.num, x.den);
69 }
70 
71 inline coeff::coeff(const mpq_class& x)
72 {
73  ap_coeff_init(&c, AP_COEFF_SCALAR);
74  ap_coeff_set_scalar_mpq(&c, const_cast<mpq_class&>(x).get_mpq_t());
75 }
76 
77 inline coeff::coeff(mpfr_t x)
78 {
79  ap_coeff_init(&c, AP_COEFF_SCALAR);
80  ap_coeff_set_scalar_mpfr(&c,x);
81 }
82 
83 
84 /* intervals */
85 
86 inline coeff::coeff(const interval& x)
87 {
88  ap_coeff_init(&c, AP_COEFF_INTERVAL);
89  ap_coeff_set_interval(&c,
90  const_cast<ap_interval_t*>(x.get_ap_interval_t()));
91 }
92 
93 inline coeff::coeff(const scalar& inf, const scalar& sup)
94 {
95  ap_coeff_init(&c, AP_COEFF_INTERVAL);
96  ap_coeff_set_interval_scalar(&c,
97  const_cast<ap_scalar_t*>(inf.get_ap_scalar_t()),
98  const_cast<ap_scalar_t*>(sup.get_ap_scalar_t()));
99 }
100 
101 inline coeff::coeff(int inf, int sup)
102 {
103  ap_coeff_init(&c, AP_COEFF_INTERVAL);
104  ap_coeff_set_interval_int(&c, inf, sup);
105 }
106 
107 inline coeff::coeff(long inf, long sup)
108 {
109  ap_coeff_init(&c, AP_COEFF_INTERVAL);
110  ap_coeff_set_interval_int(&c, inf, sup);
111 }
112 
113 inline coeff::coeff(double inf, double sup)
114 {
115  ap_coeff_init(&c, AP_COEFF_INTERVAL);
116  ap_coeff_set_interval_double(&c, inf, sup);
117 }
118 
119 inline coeff::coeff(const frac& inf, const frac& sup)
120 {
121  ap_coeff_init(&c, AP_COEFF_INTERVAL);
122  ap_coeff_set_interval_frac(&c, inf.num, inf.den, sup.num, sup.den);
123 }
124 
125 inline coeff::coeff(const mpq_class& inf, const mpq_class& sup)
126 {
127  ap_coeff_init(&c, AP_COEFF_INTERVAL);
128  ap_coeff_set_interval_mpq(&c,
129  const_cast<mpq_class&>(inf).get_mpq_t(),
130  const_cast<mpq_class&>(sup).get_mpq_t());
131 }
132 
133 inline coeff::coeff(mpfr_t inf, mpfr_t sup)
134 {
135  ap_coeff_init(&c, AP_COEFF_INTERVAL);
136  ap_coeff_set_interval_mpfr(&c,inf,sup);
137 }
138 
139 inline coeff::coeff(top t)
140 {
141  ap_coeff_init(&c, AP_COEFF_INTERVAL);
142  ap_coeff_set_interval_top(&c);
143 }
144 
145 inline coeff::coeff(bottom t)
146 {
147  ap_coeff_init(&c, AP_COEFF_INTERVAL);
148  ap_coeff_set_interval_int(&c, 1, -1);
149 }
150 
151 
152 /* destructor */
153 /* ========== */
154 
155 inline coeff::~coeff()
156 {
157  ap_coeff_clear(&c);
158 }
159 
160 
161 /* assignments */
162 /* =========== */
163 
164 inline coeff& coeff::operator= (const coeff& x)
165 {
166  ap_coeff_set(&c, const_cast<ap_coeff_t*>(x.get_ap_coeff_t()));
167  return *this;
168 }
169 
170 inline coeff& coeff::operator= (const scalar& x)
171 {
172  ap_coeff_set_scalar(&c, const_cast<ap_scalar_t*>(x.get_ap_scalar_t()));
173  return *this;
174 }
175 
176 inline coeff& coeff::operator= (int x)
177 {
178  ap_coeff_set_scalar_int(&c, x);
179  return *this;
180 }
181 
182 inline coeff& coeff::operator= (long x)
183 {
184  ap_coeff_set_scalar_int(&c, x);
185  return *this;
186 }
187 
188 inline coeff& coeff::operator= (double x)
189 {
190  ap_coeff_set_scalar_double(&c, x);
191  return *this;
192 }
193 
194 inline coeff& coeff::operator= (const frac& x)
195 {
196  ap_coeff_set_scalar_frac(&c, x.num, x.den);
197  return *this;
198 }
199 
200 inline coeff& coeff::operator= (const mpq_class& x)
201 {
202  ap_coeff_set_scalar_mpq(&c, const_cast<mpq_class&>(x).get_mpq_t());
203  return *this;
204 }
205 
206 inline coeff& coeff::operator= (mpfr_t x)
207 {
208  ap_coeff_set_scalar_mpfr(&c, x);
209  return *this;
210 }
211 
212 inline coeff& coeff::operator= (const interval& x)
213 {
214  ap_coeff_set_interval(&c, const_cast<ap_interval_t*>(x.get_ap_interval_t()));
215  return *this;
216 }
217 
218 inline coeff& coeff::operator= (top t)
219 {
220  ap_coeff_set_interval_top(&c);
221  return *this;
222 }
223 
224 inline coeff& coeff::operator= (bottom t)
225 {
226  ap_coeff_set_interval_int(&c, 1, -1);
227  return *this;
228 }
229 
230 
231 
232 /* access */
233 /* ====== */
234 
235 inline ap_coeff_discr_t coeff::get_discr() const
236 {
237  return c.discr;
238 }
239 
240 inline scalar& coeff::get_scalar()
241 {
242  if (c.discr!=AP_COEFF_SCALAR) throw(bad_discriminant("coeff::get_scalar"));
243  return reinterpret_cast<scalar&>(*c.val.scalar);
244 }
245 
246 inline const scalar& coeff::get_scalar() const
247 {
248  if (c.discr!=AP_COEFF_SCALAR) throw(bad_discriminant("coeff::get_scalar"));
249  return reinterpret_cast<const scalar&>(*c.val.scalar);
250 }
251 
252 inline interval& coeff::get_interval()
253 {
254  if (c.discr!=AP_COEFF_INTERVAL) throw(bad_discriminant("coeff::get_interval"));
255  return reinterpret_cast<interval&>(*c.val.interval);
256 }
257 
258 inline const interval& coeff::get_interval() const
259 {
260  if (c.discr!=AP_COEFF_INTERVAL) throw(bad_discriminant("coeff::get_interval"));
261  return reinterpret_cast<const interval&>(*c.val.interval);
262 }
263 
264 
265 inline coeff&coeff::set(const coeff& x)
266 {
267  ap_coeff_set(&c, const_cast<ap_coeff_t*>(x.get_ap_coeff_t()));
268  return *this;
269 }
270 
271 inline coeff&coeff::set(const scalar& x)
272 {
273  ap_coeff_set_scalar(&c, const_cast<ap_scalar_t*>(x.get_ap_scalar_t()));
274  return *this;
275 }
276 
277 inline coeff&coeff::set(int x)
278 {
279  ap_coeff_set_scalar_int(&c, x);
280  return *this;
281 }
282 
283 inline coeff&coeff::set(long x)
284 {
285  ap_coeff_set_scalar_int(&c, x);
286  return *this;
287 }
288 
289 inline coeff&coeff::set(double x)
290 {
291  ap_coeff_set_scalar_double(&c, x);
292  return *this;
293 }
294 
295 inline coeff&coeff::set(const frac& x)
296 {
297  ap_coeff_set_scalar_frac(&c, x.num, x.den);
298  return *this;
299 }
300 
301 inline coeff&coeff::set(const mpq_class& x)
302 {
303  ap_coeff_set_scalar_mpq(&c, const_cast<mpq_class&>(x).get_mpq_t());
304  return *this;
305 }
306 
307 inline coeff&coeff::set(mpfr_t x)
308 {
309  ap_coeff_set_scalar_mpfr(&c, x);
310  return *this;
311 }
312 
313 
314 inline coeff&coeff::set(const interval& x)
315 {
316  ap_coeff_set_interval(&c, const_cast<ap_interval_t*>(x.get_ap_interval_t()));
317  return *this;
318 }
319 
320 inline coeff&coeff::set(const scalar& inf, const scalar& sup)
321 {
322  ap_coeff_set_interval_scalar(&c,
323  const_cast<ap_scalar_t*>(inf.get_ap_scalar_t()),
324  const_cast<ap_scalar_t*>(sup.get_ap_scalar_t()));
325  return *this;
326 }
327 
328 inline coeff&coeff::set(int inf, int sup)
329 {
330  ap_coeff_set_interval_int(&c, inf, sup);
331  return *this;
332 }
333 
334 inline coeff&coeff::set(long inf, long sup)
335 {
336  ap_coeff_set_interval_int(&c, inf, sup);
337  return *this;
338 }
339 
340 inline coeff&coeff::set(double inf, double sup)
341 {
342  ap_coeff_set_interval_double(&c, inf, sup);
343  return *this;
344 }
345 
346 inline coeff&coeff::set(const frac& inf, const frac& sup)
347 {
348  ap_coeff_set_interval_frac(&c, inf.num, inf.den, sup.num, sup.den);
349  return *this;
350 }
351 
352 inline coeff&coeff::set(const mpq_class& inf, const mpq_class& sup)
353 {
354  ap_coeff_set_interval_mpq(&c,
355  const_cast<mpq_class&>(inf).get_mpq_t(),
356  const_cast<mpq_class&>(sup).get_mpq_t());
357  return *this;
358 }
359 
360 inline coeff&coeff::set(mpfr_t inf, mpfr_t sup)
361 {
362  ap_coeff_set_interval_mpfr(&c, inf, sup);
363  return *this;
364 }
365 
366 inline coeff&coeff::set(top t)
367 {
368  ap_coeff_set_interval_top(&c);
369  return *this;
370 }
371 
372 inline coeff&coeff::set(bottom t)
373 {
374  ap_coeff_set_interval_int(&c, 1, -1);
375  return *this;
376 }
377 
378 
379 /* swap */
380 
381 inline void swap(coeff& a, coeff &b)
382 {
383  ap_coeff_swap(&a.c, &b.c);
384 }
385 
386 
387 
388 /* print */
389 /* ===== */
390 
391 inline std::ostream& operator<< (std::ostream& os, const coeff& s)
392 {
393  switch (s.c.discr) {
394  case AP_COEFF_SCALAR: return os << *(reinterpret_cast<scalar*>(s.c.val.scalar));
395  case AP_COEFF_INTERVAL: return os << *(reinterpret_cast<interval*>(s.c.val.interval));
396  default: return os;
397  }
398 }
399 
400 inline void coeff::print(FILE* stream) const
401 {
402  ap_coeff_fprint(stream, const_cast<ap_coeff_t*>(&c));
403 }
404 
405 
406 /* tests */
407 /* ===== */
408 
409 inline bool coeff::is_zero() const
410 {
411  return ap_coeff_zero(const_cast<ap_coeff_t*>(&c));
412 }
413 
414 inline int cmp(const coeff& a, const coeff& b)
415 {
416  return ap_coeff_cmp(const_cast<ap_coeff_t*>(&a.c), const_cast<ap_coeff_t*>(&b.c));
417 }
418 
419 /*
420 inline bool operator<= (const coeff& a, const coeff& b)
421 {
422  int x = ap_coeff_cmp(const_cast<ap_coeff_t*>(&a.c), const_cast<ap_coeff_t*>(&b.c));
423  printf("%i\n",x);
424  return x>=-1 && x<=0;
425 }
426 
427 inline bool operator>= (const coeff& a, const coeff& b)
428 {
429  int x = ap_coeff_cmp(const_cast<ap_coeff_t*>(&a.c), const_cast<ap_coeff_t*>(&b.c));
430  return x>=0 && x<=1;
431 }
432 
433 inline bool operator< (const coeff& a, const coeff& b)
434 {
435  return ap_coeff_cmp(const_cast<ap_coeff_t*>(&a.c), const_cast<ap_coeff_t*>(&b.c)) == -1;
436 }
437 
438 inline bool operator> (const coeff& a, const coeff& b)
439 {
440  return ap_coeff_cmp(const_cast<ap_coeff_t*>(&a.c), const_cast<ap_coeff_t*>(&b.c)) == 1;
441 }
442 */
443 
444 inline bool operator== (const coeff& a, const coeff& b)
445 {
446  return ap_coeff_equal(const_cast<ap_coeff_t*>(&a.c), const_cast<ap_coeff_t*>(&b.c));
447 }
448 
449 inline bool operator!= (const coeff& a, const coeff& b)
450 {
451  return !ap_coeff_equal(const_cast<ap_coeff_t*>(&a.c), const_cast<ap_coeff_t*>(&b.c));
452 }
453 
454 
455 
456 
457 /* other operators */
458 /* =============== */
459 
460 inline void coeff::reduce()
461 {
462  ap_coeff_reduce(&c);
463 }
464 
465 inline void coeff::neg()
466 {
467  ap_coeff_neg(&c, &c);
468 }
469 
470 inline coeff coeff::operator- () const
471 {
472  coeff r = *this; r.neg(); return r;
473 }
474 
475 inline long coeff::hash() const
476 {
477  return ap_coeff_hash(const_cast<ap_coeff_t*>(&c));
478 }
479 
480 
481 /* C-level compatibility */
482 
483 inline const ap_coeff_t* coeff::get_ap_coeff_t() const
484 {
485  return &c;
486 }
487 
488 inline ap_coeff_t* coeff::get_ap_coeff_t()
489 {
490  return &c;
491 }
texpr0::builder neg(const texpr0::builder &a, ap_texpr_rtype_t rtype=AP_RTYPE_REAL, ap_texpr_rdir_t rdir=AP_RDIR_NEAREST)
Definition: apxx_texpr0.hh:814
std::ostream & operator<<(std::ostream &os, const coeff &s)
Definition: apxx_coeff_inline.hh:391
texpr0::builder operator-(const texpr0::builder &a)
Definition: apxx_texpr0.hh:854
friend builder neg(const builder &a, ap_texpr_rtype_t rtype, ap_texpr_rdir_t rdir)
Makes an AP_TEXPR_NEG expression node.
Definition: apxx_texpr0.hh:814
bool operator==(const coeff &a, const coeff &b)
Definition: apxx_coeff_inline.hh:444
bool operator!=(const coeff &a, const coeff &b)
Definition: apxx_coeff_inline.hh:449
void swap(coeff &a, coeff &b)
Definition: apxx_coeff_inline.hh:381
int cmp(const coeff &a, const coeff &b)
Definition: apxx_coeff_inline.hh:414