Borderbasix

BC.hpp
Go to the documentation of this file.
1 /*********************************************************************
2 * This file is part of the source code of BORDERBASIX software. *
3 * (C) B. Mourrain, INRIA *
4 **********************************************************************
5 History:
6 $Id: BC.hpp,v 1.1.1.1 2006/10/06 08:01:40 trebuche Exp $
7 **********************************************************************/
8 #ifndef _BIGINT_H_
9 #ifndef NDEBUG
10 #include <assert.h>
11 #endif //
12 
13 //========================== end of GMP custom allocation =====================
14 //#include "Basic.H"
15 #include "gmp.h"
16 
17 #ifdef win32
18 #define const
19 #endif //
20 
21 
22 
23 template <class T> struct Scl<T>
24 #ifdef PL_CMM
25 : public GcObject
26 #endif //
27 {
28 public:
29  Scl<T> ( );
30  Scl<T> (const Scl<T>& b);
31  Scl<T> (signed long sl);
32  Scl<T> (unsigned long ul);
33  Scl<T> (int si);
34  Scl<T> (const char* string, int base = 10);
35  Scl<T> ( MP_INT new_z ) { z = new_z; }
36 
37  ~Scl<T> ();
38  friend ostream& operator << (ostream& os, const Scl<T>& b);
39  friend istream& operator >> (istream& is, Scl<T>& b);
40 
41  friend unsigned long BigIntToUL (const Scl<T>& b);
42  friend signed long BigIntToSL (const Scl<T>& b);
43 
44  friend size_t log (const Scl<T>& b);
45  friend size_t log (const Scl<T>& b, int base);
46 
47  friend int sign (const Scl<T>& b);
48 
49  friend int compare (const Scl<T>& b1, const Scl<T>& b2);
50  friend int compare (const Scl<T>& b, unsigned long ul);
51  friend int compare (const Scl<T>& b, long sl);
52 
53  friend bool IsPositive (const Scl<T>& b);
54  friend bool IsNegative (const Scl<T>& b);
55  friend bool IsZero (const Scl<T>& b);
56  friend bool IsOne (const Scl<T>& b);
57  friend bool IsMinusOne (const Scl<T>& b);
58  friend bool IsOdd (const Scl<T>& b);
59  friend bool IsEven (const Scl<T>& b);
60  friend bool IsPerfectSquare (const Scl<T>& b);
61  friend bool IsProbablyPrime (const Scl<T>& b, int reps = 25);
62 
63  bool operator == (const Scl<T>& rhs) const;
64  bool operator != (const Scl<T>& rhs) const;
65  bool operator > (const Scl<T>& rhs) const;
66  bool operator >= (const Scl<T>& rhs) const;
67  bool operator < (const Scl<T>& rhs) const;
68  bool operator <= (const Scl<T>& rhs) const;
69 
70  bool operator == (long sl) const;
71  bool operator != (long sl) const;
72  bool operator > (long sl) const;
73  bool operator >= (long sl) const;
74  bool operator < (long sl) const;
75  bool operator <= (long sl) const;
76 
77  bool operator == (int si) const;
78  bool operator != (int si) const;
79  bool operator > (int si) const;
80  bool operator >= (int si) const;
81  bool operator < (int si) const;
82  bool operator <= (int si) const;
83 
84  bool operator == (unsigned long ul) const;
85  bool operator != (unsigned long ul) const;
86  bool operator > (unsigned long ul) const;
87  bool operator >= (unsigned long ul) const;
88  bool operator < (unsigned long ul) const;
89  bool operator <= (unsigned long ul) const;
90 
91  //
92  // -------- functions that modify at least one argument or `*this' ----------
93  //
94 
95  void operator = (const Scl<T>& rhs);
96  void operator += (const Scl<T>& rhs);
97  void operator -= (const Scl<T>& rhs);
98  void operator *= (const Scl<T>& rhs);
99  void operator /= (const Scl<T>& rhs);
100  void operator %= (const Scl<T>& rhs);
101 
102  void operator = (unsigned long rhs);
103  void operator += (unsigned long rhs);
104  void operator -= (unsigned long rhs);
105  void operator *= (unsigned long rhs);
106  void operator /= (unsigned long rhs);
107  void operator %= (unsigned long rhs);
108 
109  void operator = (long rhs);
110  void operator += (long rhs);
111  void operator -= (long rhs);
112  void operator *= (long rhs);
113  void operator /= (long rhs);
114  void operator %= (long rhs);
115 
116  void operator ++ ( );
117  void operator -- ( );
118 
119  void Div2Exp (unsigned long exponent_of_2);
120  void GCD (const Scl<T>& b2);
121  void Mod2Exp (unsigned long exponent_of_2);
122  void Mul2Exp (unsigned long exponent_of_2);
123  void PowMod (const Scl<T>& exp, const Scl<T>& m);
124  void PowMod (unsigned long exp, const Scl<T>& m);
125  void abs ( );
126  void factorial (unsigned long n);
127  void negate ( );
128  void pow (unsigned long exp);
129  void pow (unsigned long base, unsigned long exp);
130  void quo (const Scl<T>& divisor);
131  void quo (unsigned long divisor);
132  void rem (const Scl<T>& divisor);
133  unsigned long rem (unsigned long divisor);
134  void sqrt ( );
135 
136  friend void SqrtRem (Scl<T>& sqrt, Scl<T>& rem, const Scl<T>& b);
137 
138  friend void QuoRem (Scl<T>& q, Scl<T>& r,
139  const Scl<T>& divdend, const Scl<T>& divisor);
140 
141  friend unsigned long QuoRem (Scl<T>& q, Scl<T>& r,
142  const Scl<T>& divdend, unsigned long divisor);
143 
144  friend void DivMod (Scl<T>& q, Scl<T>& r,
145  const Scl<T>& divdend, const Scl<T>& divisor);
146 
147  friend void DivMod (Scl<T>& q, Scl<T>& r, const Scl<T>& divdend,
148  unsigned long divisor);
149 
150  friend void ExtGCD (Scl<T>& gcd, Scl<T>& a, Scl<T>& b,
151  const Scl<T>& x, const Scl<T>& y);
152 
153  friend void HalfExtGCD (Scl<T>& gcd, Scl<T>& a,
154  const Scl<T>& x, const Scl<T>& y);
155 
156 
157 #ifdef PL_CMM
158  void traverse();
159 #endif //
160  mpz z;
161 };
162 
163 
164 //
165 // -------- the corresponding `const' functions -----------------------------
166 //
167 
168 Scl<T>& operator - (const Scl<T>& b);
169 
170 Scl<T> operator + (const Scl<T>& b1, const Scl<T>& b2);
171 Scl<T>& operator - (const Scl<T>& b1, const Scl<T>& b2);
172 Scl<T>& operator * (const Scl<T>& b1, const Scl<T>& b2);
173 Scl<T>& operator / (const Scl<T>& b1, const Scl<T>& b2);
174 Scl<T>& operator % (const Scl<T>& b1, const Scl<T>& b2);
175 
176 Scl<T>& operator + (const Scl<T>& b, unsigned long ul);
177 Scl<T>& operator - (const Scl<T>& b, unsigned long ul);
178 Scl<T>& operator * (const Scl<T>& b, unsigned long ul);
179 Scl<T>& operator / (const Scl<T>& b, unsigned long ul);
180 Scl<T>& operator % (const Scl<T>& b, unsigned long ul);
181 
182 Scl<T>& operator + (unsigned long ul, const Scl<T>& b);
183 Scl<T>& operator * (unsigned long ul, const Scl<T>& b);
184 
185 Scl<T>& operator + (const Scl<T>& b, long sl);
186 Scl<T>& operator - (const Scl<T>& b, long sl);
187 Scl<T>& operator * (const Scl<T>& b, long sl);
188 Scl<T>& operator / (const Scl<T>& b, long sl);
189 Scl<T>& operator % (const Scl<T>& b, long sl);
190 
191 Scl<T>& operator + (long sl, const Scl<T>& b);
192 Scl<T>& operator * (long sl, const Scl<T>& b);
193 
194 Scl<T>& Div2Exp (const Scl<T>& b, unsigned long exponent_of_2);
195 Scl<T>& GCD (const Scl<T>& b1, const Scl<T>& b2);
196 Scl<T>& gcd (const Scl<T>& b1, const Scl<T>& b2);
197 Scl<T>& Mod2Exp (const Scl<T>& b, unsigned long exponent_of_2);
198 Scl<T>& Mul2Exp (const Scl<T>& b, unsigned long exponent_of_2);
199 Scl<T>& PowMod (const Scl<T>& b, const Scl<T>& exp, const Scl<T>& m);
200 Scl<T>& PowMod (const Scl<T>& b, unsigned long exp, const Scl<T>& m);
201 Scl<T>& abs (const Scl<T>& b);
202 // Scl<T>& negate (const Scl<T>& b);
203 Scl<T>& pow (const Scl<T>& base, unsigned long exp);
204 Scl<T>& quo (const Scl<T>& dividend, const Scl<T>& divisor);
205 Scl<T>& quo (const Scl<T>& dividend, unsigned long divisor);
206 Scl<T>& rem (const Scl<T>& dividend, const Scl<T>& divisor);
207 Scl<T>& sqrt (const Scl<T>& b);
208 unsigned long rem (const Scl<T>& b, unsigned long divisor);
209 
210 Scl<T>& BigIntFactorial (unsigned long n);
211 
212 
213 //-------------------------- Scl<T> (...) -------------------------------------
214 
215 inline
217 {
218  mpz_init(&z);
219 }
220 
221 inline
222 Scl<T>::Scl<T> (const Scl<T>& b)
223 {
224  mpz_init_set(&z, &b.z);
225 }
226 
227 inline
228 Scl<T>::Scl<T> (signed long int sl)
229 {
230  mpz_init_set_si(&z, sl);
231 }
232 
233 inline
234 Scl<T>::Scl<T> (unsigned long int ul)
235 {
236  mpz_init_set_ui(&z, ul);
237 }
238 
239 inline
240 Scl<T>::Scl<T> (int si)
241 {
242  mpz_init_set_si(&z, (long) si);
243 }
244 
245 inline
246 Scl<T>::Scl<T> (const char *string, int base)
247 {
248  if (mpz_init_set_str(&z, string, base))
249  cerr << "Scl<T>::Scl<T>: The string " << string
250  << " is not a valid number in base " << base << endl;
251 }
252 
253 
254 //-------------------------- Scl<T>::~Scl<T> () -------------------------------
255 
256 inline
257 Scl<T>::~Scl<T> ( )
258 {
259 #if defined(PL_CMM) // || defined(PL_BARTLETT)
260 // PL_CMM || Bartlett
261 // It wouldn't be wrong to call `mpz_clear' but since it only calls
262 // `_PossoGMPDealloc', which does nothing for Bartlett and CMM, this
263 // is a small optimization.
264 // PL_CMM || Bartlett end
265 #else
266  mpz_clear(&z);
267 #endif //
268 }
269 
270 
271 //-------------------------- BigIntToUL ---------------------------------------
272 
273 inline unsigned long
274 BigIntToUL (const Scl<T>& b)
275 {
276  return mpz_get_ui(&b.z);
277 }
278 
279 
280 //-------------------------- BigIntToSL ---------------------------------------
281 
282 inline signed long
283 BigIntToSL (const Scl<T>& b)
284 {
285  return mpz_get_si(&b.z);
286 }
287 
288 
289 //-------------------------- log(...) -----------------------------------------
290 
291 inline size_t
292 log (const Scl<T>& b)
293 {
294  return mpz_size(&b.z);
295 }
296 
297 inline size_t
298 log (const Scl<T>& b, int base)
299 {
300  assert(base >= 2 && base <= 36);
301  return mpz_sizeinbase(&b.z, base);
302 }
303 
304 //-------------------------- sign ---------------------------------------------
305 
306 inline int
307 sign (const Scl<T>& b)
308 {
309  if (b.z._mp_size == 0)
310  return 0;
311  else
312  return b.z._mp_size > 0 ? 1 : -1;
313 }
314 
315 
316 //-------------------------- compare (...) ------------------------------------
317 
318 inline int
319 compare (const Scl<T>& b1, const Scl<T>& b2)
320 {
321  return mpz_cmp(&b1.z, &b2.z);
322 }
323 
324 inline int
325 compare (const Scl<T>& b, unsigned long ul)
326 {
327  return mpz_cmp_ui(&b.z, ul);
328 }
329 
330 inline int
331 compare (const Scl<T>& b, long sl)
332 {
333  return mpz_cmp_si(&b.z, sl);
334 }
335 
336 
337 //-------------------------- IsPositive etc. ----------------------------------
338 
339 inline bool
340 IsPositive (const Scl<T>& b)
341 {
342  return b.z._mp_size > 0;
343 }
344 
345 inline bool
346 IsNegative (const Scl<T>& b)
347 {
348  return b.z._mp_size < 0;
349 }
350 
351 inline bool
352 IsZero (const Scl<T>& b)
353 {
354  return b.z._mp_size == 0;
355 }
356 
357 inline bool
358 IsOne(const Scl<T>& b)
359 {
360  return b.z._mp_size == 1 && b.z._mp_d[0] == 1;
361 }
362 
363 inline bool
364 IsMinusOne (const Scl<T>& b)
365 {
366  return b.z._mp_size == -1 && b.z._mp_d[0] == 1;
367 }
368 
369 inline bool
370 IsOdd (const Scl<T>& b)
371 {
372  return b.z._mp_size != 0 && (b.z._mp_d[0] & ((mp_limb_t) 1));
373 }
374 
375 inline bool
376 IsEven (const Scl<T>& b)
377 {
378  return b.z._mp_size == 0 || (b.z._mp_d[0] ^ ((mp_limb_t) 0));
379 }
380 
381 inline bool
383 {
384  return mpz_perfect_square_p(&b.z);
385 }
386 
387 inline bool
388 IsProbablyPrime (const Scl<T>& b, int reps /* = 25 */)
389 {
390  return mpz_probab_prime_p(&b.z, reps);
391 }
392 
393 
394 //-------------------------- Scl<T> ==, !=, >, >=, <, <= ----------------------
395 
396 inline bool
397 Scl<T>::operator == (const Scl<T>& rhs) const
398 {
399  return mpz_cmp(&z, &rhs.z) == 0;
400 }
401 
402 inline bool
403 Scl<T>::operator != (const Scl<T>& rhs) const
404 {
405  return mpz_cmp(&z, &rhs.z) != 0;
406 }
407 
408 inline bool
409 Scl<T>::operator > (const Scl<T>& rhs) const
410 {
411  return mpz_cmp(&z, &rhs.z) > 0;
412 }
413 
414 inline bool
415 Scl<T>::operator >= (const Scl<T>& rhs) const
416 {
417  return mpz_cmp(&z, &rhs.z) >= 0;
418 }
419 
420 inline bool
421 Scl<T>::operator < (const Scl<T>& rhs) const
422 {
423  return mpz_cmp(&z, &rhs.z) < 0;
424 }
425 
426 inline bool
427 Scl<T>::operator <= (const Scl<T>& rhs) const
428 {
429  return mpz_cmp(&z, &rhs.z) <= 0;
430 }
431 
432 
433 //-------------------------- long ==, !=, >, >=, <, <= ------------------------
434 
435 inline bool
437 {
438  return mpz_cmp_si(&z, sl) == 0;
439 }
440 
441 inline bool
443 {
444  return mpz_cmp_si(&z, sl) != 0;
445 }
446 
447 inline bool
449 {
450  return mpz_cmp_si(&z, sl) > 0;
451 }
452 
453 inline bool
455 {
456  return mpz_cmp_si(&z, sl) >= 0;
457 }
458 
459 inline bool
461 {
462  return mpz_cmp_si(&z, sl) < 0;
463 }
464 
465 inline bool
467 {
468  return mpz_cmp_si(&z, sl) <= 0;
469 }
470 
471 //-------------------------- int ==, !=, >, >=, <, <= ---------------
472 
473 
474 inline bool
475 Scl<T>::operator == (int si) const
476 {
477  return mpz_cmp_si(&z, (long) si) == 0;
478 }
479 
480 inline bool
481 Scl<T>::operator != (int si) const
482 {
483  return mpz_cmp_si(&z, (long) si) != 0;
484 }
485 
486 inline bool
487 Scl<T>::operator > (int si) const
488 {
489  return mpz_cmp_si(&z, (long) si) > 0;
490 }
491 
492 inline bool
493 Scl<T>::operator >= (int si) const
494 {
495  return mpz_cmp_si(&z, (long) si) >= 0;
496 }
497 
498 inline bool
499 Scl<T>::operator < (int si) const
500 {
501  return mpz_cmp_si(&z, (long) si) < 0;
502 }
503 
504 inline bool
505 Scl<T>::operator <= (int si) const
506 {
507  return mpz_cmp_si(&z, (long) si) <= 0;
508 }
509 
510 
511 //-------------------------- unsigned long ==, !=, >, >=, <, <= ---------------
512 
513 
514 inline bool
515 Scl<T>::operator == (unsigned long ul) const
516 {
517  return mpz_cmp_ui(&z, ul) == 0;
518 }
519 
520 inline bool
521 Scl<T>::operator != (unsigned long ul) const
522 {
523  return mpz_cmp_ui(&z, ul) != 0;
524 }
525 
526 inline bool
527 Scl<T>::operator > (unsigned long ul) const
528 {
529  return mpz_cmp_ui(&z, ul) > 0;
530 }
531 
532 inline bool
533 Scl<T>::operator >= (unsigned long ul) const
534 {
535  return mpz_cmp_ui(&z, ul) >= 0;
536 }
537 
538 inline bool
539 Scl<T>::operator < (unsigned long ul) const
540 {
541  return mpz_cmp_ui(&z, ul) < 0;
542 }
543 
544 inline bool
545 Scl<T>::operator <= (unsigned long ul) const
546 {
547  return mpz_cmp_ui(&z, ul) <= 0;
548 }
549 
550 //-------------------------- Scl<T> =, +=, -=, *=, /=, %= ---------------------
551 
552 inline void
554 {
555  if (this != &rhs)
556  mpz_set(&z, &rhs.z);
557 }
558 
559 inline void
561 {
562  mpz_add(&z, &z, &rhs.z);
563 }
564 
565 inline void
567 {
568  mpz_sub(&z, &z, &rhs.z);
569 }
570 
571 inline void
573 {
574  mpz_mul(&z, &z, &rhs.z);
575 }
576 
577 inline void
579 {
580 // mpz_div(&z, &z, &rhs.z);
581  mpz_divexact(&z, &z, &rhs.z);
582 }
583 
584 inline void
586 {
587  mpz_mod(&z, &z, &rhs.z);
588 }
589 
590 
591 //-------------------------- unsigned long =, +=, -=, *=, /=, %= --------------
592 
593 inline void
594 Scl<T>::operator = (unsigned long ul)
595 {
596  mpz_set_ui(&z, ul);
597 }
598 
599 inline void
600 Scl<T>::operator += (unsigned long ul)
601 {
602  mpz_add_ui(&z, &z, ul);
603 }
604 
605 inline void
606 Scl<T>::operator -= (unsigned long ul)
607 {
608  mpz_sub_ui(&z, &z, ul);
609 }
610 
611 inline void
612 Scl<T>::operator *= (unsigned long ul)
613 {
614  mpz_mul_ui(&z, &z, ul);
615 }
616 
617 inline void
618 Scl<T>::operator /= (unsigned long ul)
619 {
620  mpz_div_ui(&z, &z, ul);
621 }
622 
623 inline void
624 Scl<T>::operator %= (unsigned long ul)
625 {
626  mpz_mod_ui(&z, &z, ul);
627 }
628 
629 
630 //-------------------------- long =, +=, -=, *=, /=, %= -----------------------
631 
632 inline void
634 {
635  mpz_set_si(&z, sl);
636 }
637 
638 inline void
640 {
641  if (sl >= 0)
642  mpz_add_ui(&z, &z, (unsigned long) sl);
643  else
644  mpz_sub_ui(&z, &z, ((unsigned long) -sl));
645 }
646 
647 inline void
649 {
650  if (sl >= 0)
651  mpz_sub_ui(&z, &z, (unsigned long) sl);
652  else
653  mpz_add_ui(&z, &z, (unsigned long) sl);
654 }
655 
656 inline void
658 {
659  if (sl >= 0)
660  mpz_mul_ui(&z, &z, (unsigned long) sl);
661  else
662  {
663  z._mp_size = -z._mp_size;
664  mpz_mul_ui(&z, &z, ((unsigned long) -sl));
665  }
666 }
667 
668 inline void
670 {
671  if (sl >= 0)
672  mpz_div_ui(&z, &z, (unsigned long) sl);
673  else
674  {
675  z._mp_size = -z._mp_size;
676  mpz_div_ui(&z, &z, ((unsigned long) -sl));
677  }
678 }
679 
680 // inline void
681 // Scl<T>::operator %= (long sl)
682 // {
683 // mpz_mod_si(&z, &z, sl);
684 // }
685 
686 
687 //-------------------------- prefix ++, and -- --------------------------------
688 
689 inline void
691 {
692  mpz_add_ui(&z, &z, 1);
693 }
694 
695 inline void
697 {
698  mpz_sub_ui(&z, &z, 1);
699 }
700 
701 
702 //--------------------------
703 
704 inline void
705 Scl<T>::Div2Exp (unsigned long exponent_of_2)
706 {
707  mpz_div_2exp(&z, &z, exponent_of_2);
708 }
709 
710 inline void
711 Scl<T>::GCD (const Scl<T>& b2)
712 {
713  mpz_gcd (&z, &z, &b2.z);
714 }
715 
716 
717 inline void
718 Scl<T>::Mod2Exp (unsigned long exponent_of_2)
719 {
720  mpz_mod_2exp(&z, &z, exponent_of_2);
721 }
722 
723 inline void
724 Scl<T>::Mul2Exp (unsigned long exponent_of_2)
725 {
726  mpz_mul_2exp(&z, &z, exponent_of_2);
727 }
728 
729 inline void
730 Scl<T>::PowMod (const Scl<T>& exp, const Scl<T>& m)
731 {
732  mpz_powm(&z, &z, &exp.z, &m.z);
733 }
734 
735 inline void
736 Scl<T>::PowMod (unsigned long exp, const Scl<T>& m)
737 {
738  mpz_powm_ui(&z, &z, exp, &m.z);
739 }
740 
741 inline void
743 {
744  if (z._mp_size < 0)
745  z._mp_size = - z._mp_size;
746 }
747 
748 inline void
749 Scl<T>::factorial (unsigned long n)
750 {
751  mpz_fac_ui(&z, n);
752 }
753 
754 inline void
756 {
757  z._mp_size = - z._mp_size;
758 }
759 
760 inline void
761 Scl<T>::pow (unsigned long exp)
762 {
763  mpz_pow_ui(&z, &z, exp);
764 }
765 
766 inline void
767 Scl<T>::pow (unsigned long base, unsigned long exp)
768 {
769  mpz_ui_pow_ui(&z, base, exp);
770 }
771 
772 inline void
773 Scl<T>::quo (const Scl<T>& divisor)
774 {
775  mpz_mdiv (&z, &z, &divisor.z);
776 }
777 
778 inline void
779 Scl<T>::quo (unsigned long divisor)
780 {
781  mpz_mdiv_ui(&z, &z, divisor);
782 }
783 
784 inline void
785 Scl<T>::rem (const Scl<T>& divisor)
786 {
787  mpz_mmod(&z, &z, &divisor.z);
788 }
789 
790 inline unsigned long
791 Scl<T>::rem (unsigned long divisor)
792 {
793  return mpz_mmod_ui(&z, &z, divisor);
794 }
795 
796 inline void
798 {
799  mpz_sqrt(&z, &z);
800 }
801 
802 inline void
804 {
805  mpz_sqrtrem(&sqrt.z, &rem.z, &b.z);
806 }
807 
808 inline void
809 QuoRem (Scl<T>& q, Scl<T>& r, const Scl<T>& divdend, const Scl<T>& divisor)
810 {
811  mpz_mdivmod(&q.z, &r.z, &divdend.z, &divisor.z);
812 }
813 
814 inline unsigned long
815 QuoRem (Scl<T>& q, Scl<T>& r, const Scl<T>& divdend, unsigned long divisor)
816 {
817  return mpz_mdivmod_ui(&q.z, &r.z, &divdend.z, divisor);
818 }
819 
820 inline void
821 DivMod (Scl<T>& q, Scl<T>& r, const Scl<T>& divdend, const Scl<T>& divisor)
822 {
823  mpz_divmod(&q.z, &r.z, &divdend.z, &divisor.z);
824 }
825 
826 inline void
827 DivMod (Scl<T>& q, Scl<T>& r, const Scl<T>& divdend, unsigned long divisor)
828 {
829  mpz_divmod_ui(&q.z, &r.z, &divdend.z, divisor);
830 }
831 
832 inline void
833 ExtGCD (Scl<T>& gcd, Scl<T>& a, Scl<T>& b, const Scl<T>& x, const Scl<T>& y)
834 {
835  mpz_gcdext(&gcd.z, &a.z, &b.z, &x.z, &y.z);
836 }
837 
838 inline void
839 HalfExtGCD (Scl<T>& gcd, Scl<T>& a, const Scl<T>& x, const Scl<T>& y)
840 {
841  mpz_gcdext(&gcd.z, &a.z, 0, &x.z, &y.z);
842 }
843 
844 
845 //
846 // -------- the corresponding `const' functions -----------------------------
847 //
848 
849 //#define NO_NRV 1
850 #if defined(__GNUG__) && !defined(NO_NRV)
851 #define NRVAL(name) return name
852 #define NRCODE(code) code
853 #define NONRCODE(code)
854 #else
855 #define NRVAL(name)
856 #define NRCODE(code)
857 #define NONRCODE(code) code
858 #endif //
859 
860 
861 inline Scl<T>
862 operator - (const Scl<T>& b) NRVAL(r(b))
863 {
864  NRCODE(r.negate();)
865  NONRCODE(Scl<T> r(b); r.negate(); return r;)
866 }
867 
868 inline Scl<T>
869 operator + (const Scl<T>& b1, const Scl<T>& b2) NRVAL(r(b1))
870 {
871  NRCODE(r += b2;)
872  NONRCODE(Scl<T> r(b1); r += b2; return r;)
873 }
874 
875 inline Scl<T>
876 operator - (const Scl<T>& b1, const Scl<T>& b2) NRVAL(r(b1))
877 {
878  NRCODE(r -= b2;)
879  NONRCODE(Scl<T> r(b1); r -= b2; return r;)
880 }
881 
882 inline Scl<T>
883 operator * (const Scl<T>& b1, const Scl<T>& b2) NRVAL(r(b1))
884 {
885  NRCODE(r *= b2;)
886  NONRCODE(Scl<T> r(b1); r *= b2; return r;)
887 }
888 
889 inline Scl<T>
890 operator / (const Scl<T>& b1, const Scl<T>& b2) NRVAL(r(b1))
891 {
892  NRCODE(r /= b2;)
893  NONRCODE(Scl<T> r(b1); r /= b2; return r;)
894 }
895 
896 inline Scl<T>
897 operator % (const Scl<T>& b1, const Scl<T>& b2) NRVAL(r(b1))
898 {
899  NRCODE(r %= b2;)
900  NONRCODE(Scl<T> r(b1); r %= b2; return r;)
901 }
902 
903 //--------------------------
904 
905 inline Scl<T>
906 operator + (const Scl<T>& b, unsigned long ul) NRVAL(r(b))
907 {
908  NRCODE(r += ul;)
909  NONRCODE(Scl<T> r(b); r += ul; return r;)
910 }
911 
912 inline Scl<T>
913 operator - (const Scl<T>& b, unsigned long ul) NRVAL(r(b))
914 {
915  NRCODE(r -= ul;)
916  NONRCODE(Scl<T> r(b); r -= ul; return r;)
917 }
918 
919 inline Scl<T>
920 operator * (const Scl<T>& b, unsigned long ul) NRVAL(r(b))
921 {
922  NRCODE(r *= ul;)
923  NONRCODE(Scl<T> r(b); r *= ul; return r;)
924 }
925 
926 inline Scl<T>
927 operator / (const Scl<T>& b, unsigned long ul) NRVAL(r(b))
928 {
929  NRCODE(r /= ul;)
930  NONRCODE(Scl<T> r(b); r /= ul; return r;)
931 }
932 
933 inline Scl<T>
934 operator % (const Scl<T>& b, unsigned long ul) NRVAL(r(b))
935 {
936  NRCODE(r %= ul;)
937  NONRCODE(Scl<T> r(b); r %= ul; return r;)
938 }
939 
940 //--------------------------
941 
942 inline Scl<T>
943 operator + (unsigned long ul, const Scl<T>& b)
944 {
945  return b + ul;
946 }
947 
948 inline Scl<T>
949 operator * (unsigned long ul, const Scl<T>& b)
950 {
951  return b * ul;
952 }
953 
954 //--------------------------
955 
956 inline Scl<T>
957 operator + (const Scl<T>& b, long sl) NRVAL(r(b))
958 {
959  NRCODE(r += sl;)
960  NONRCODE(Scl<T> r(b); r += sl; return r;)
961 }
962 
963 
964 inline Scl<T>
965 operator - (const Scl<T>& b, long sl) NRVAL(r(b))
966 {
967  NRCODE(r -= sl;)
968  NONRCODE(Scl<T> r(b); r -= sl; return r;)
969 }
970 
971 
972 inline Scl<T>
973 operator * (const Scl<T>& b, long sl) NRVAL(r(b))
974 {
975  NRCODE(r *= sl;)
976  NONRCODE(Scl<T> r(b); r *= sl; return r;)
977 }
978 
979 
980 inline Scl<T>
981 operator / (const Scl<T>& b, long sl) NRVAL(r(b))
982 {
983  NRCODE(r /= sl;)
984  NONRCODE(Scl<T> r(b); r /= sl; return r;)
985 }
986 
987 
988 inline Scl<T>
989 operator % (const Scl<T>& b, long sl) NRVAL(r(b))
990 {
991  NRCODE(r %= sl;)
992  NONRCODE(Scl<T> r(b); r %= sl; return r;)
993 }
994 
995 //--------------------------
996 
997 inline Scl<T>
998 operator + (long sl, const Scl<T>& b)
999 {
1000  return b + sl;
1001 }
1002 
1003 inline Scl<T>
1004 operator * (long sl, const Scl<T>& b)
1005 {
1006  return b * sl;
1007 }
1008 
1009 //--------------------------
1010 
1011 inline Scl<T>
1012 Div2Exp (const Scl<T>& b, unsigned long exponent_of_2) NRVAL(r(b))
1013 {
1014  NRCODE(r.Div2Exp(exponent_of_2);)
1015  NONRCODE(Scl<T> r(b); r.Div2Exp(exponent_of_2); return r;)
1016 }
1017 
1018 inline Scl<T>
1019 GCD (const Scl<T>& b1, const Scl<T>& b2) NRVAL(r(b1))
1020 {
1021  NRCODE(r.GCD(b2);)
1022  NONRCODE(Scl<T> r(b1); r.GCD(b2); return r;)
1023 }
1024 
1025 inline Scl<T>
1026 gcd (const Scl<T>& b1, const Scl<T>& b2) NRVAL(r(b1))
1027 {
1028  NRCODE(r.GCD(b2);)
1029  NONRCODE(Scl<T> r(b1); r.GCD(b2); return r;)
1030 }
1031 
1032 inline Scl<T>
1033 Mod2Exp (const Scl<T>& b, unsigned long exponent_of_2) NRVAL(r(b))
1034 {
1035  NRCODE(r.Mod2Exp(exponent_of_2);)
1036  NONRCODE(Scl<T> r(b); r.Mod2Exp(exponent_of_2); return r;)
1037 }
1038 
1039 inline Scl<T>
1040 Mul2Exp (const Scl<T>& b, unsigned long exponent_of_2) NRVAL(r(b))
1041 {
1042  NRCODE(r.Mul2Exp(exponent_of_2);)
1043  NONRCODE(Scl<T> r(b); r.Mul2Exp(exponent_of_2); return r;)
1044 }
1045 
1046 inline Scl<T>
1047 PowMod (const Scl<T>& b, const Scl<T>& exp, const Scl<T>& m) NRVAL(r(b))
1048 {
1049  NRCODE(r.PowMod(exp, m);)
1050  NONRCODE(Scl<T> r(b); r.PowMod(exp,m); return r;)
1051 }
1052 
1053 inline Scl<T>
1054 PowMod (const Scl<T>& b, unsigned long exp, const Scl<T>& m) NRVAL(r(b))
1055 {
1056  NRCODE(r.PowMod(exp, m);)
1057  NONRCODE(Scl<T> r(b); r.PowMod(exp,m); return r;)
1058 }
1059 
1060 inline Scl<T>
1061 abs (const Scl<T>& b) NRVAL(r(b))
1062 {
1063  NRCODE(r.abs();)
1064  NONRCODE(Scl<T> r(b); r.abs(); return r;)
1065 }
1066 
1067 // inline Scl<T>
1068 // negate (const Scl<T>& b) NRVAL(r(b))
1069 // {
1070 // NRCODE(r.negate();)
1071 // NONRCODE(Scl<T> r(b); r.negate(); return r;)
1072 // }
1073 
1074 inline Scl<T>
1075 pow (const Scl<T>& base, unsigned long exp) NRVAL(r(base))
1076 {
1077  NRCODE(r.pow(exp);)
1078  NONRCODE(Scl<T> r(base); r.pow(exp); return r;)
1079 }
1080 
1081 inline Scl<T>
1082 quo (const Scl<T>& dividend, const Scl<T>& divisor) NRVAL(r(dividend))
1083 {
1084  NRCODE(r.quo(divisor);)
1085  NONRCODE(Scl<T> r(dividend); r.quo(divisor); return r;)
1086 }
1087 
1088 inline Scl<T>
1089 quo (const Scl<T>& dividend, unsigned long divisor) NRVAL(r(dividend))
1090 {
1091  NRCODE(r.quo(divisor);)
1092  NONRCODE(Scl<T> r(dividend); r.quo(divisor); return r;)
1093 }
1094 
1095 inline Scl<T>
1096 rem (const Scl<T>& dividend, const Scl<T>& divisor) NRVAL(r(dividend))
1097 {
1098  NRCODE(r.rem(divisor);)
1099  NONRCODE(Scl<T> r(dividend); r.rem(divisor); return r;)
1100 }
1101 
1102 inline Scl<T>
1103 sqrt (const Scl<T>& b) NRVAL(r(b))
1104 {
1105  NRCODE(r.sqrt();)
1106  NONRCODE(Scl<T> r(b); r.sqrt(); return r;)
1107 }
1108 
1109 inline unsigned long
1110 rem (const Scl<T>& b, unsigned long divisor)
1111 {
1112  Scl<T> r(b);
1113  return r.rem(divisor);
1114 }
1115 
1116 inline Scl<T>
1117 BigIntFactorial (unsigned long n) NRVAL(r)
1118 {
1119  NRCODE(r.factorial(n);)
1120  NONRCODE(Scl<T> r; r.factorial(n); return r;)
1121 }
1122 
1123 
1124 inline Scl<T>
1125 BigIntPow (unsigned long base, unsigned long exp) NRVAL(r)
1126 {
1127  NRCODE(r.pow(base, exp);)
1128  NONRCODE(Scl<T> r; r.pow(base, exp); return r;)
1129 }
1130 
1131 #undef NRVAL
1132 #undef NRCODE
1133 #undef NONRCODE
1134 
1135 #endif // // _BIGINT_H_
1136 
std::istream & operator>>(std::istream &is, Scl< MPF > &b)
Definition: Scl_mpf.hpp:617
unsigned long BigIntToUL(const Scl< T > &b)
Definition: BC.hpp:274
void PowMod(const Scl< T > &exp, const Scl< T > &m)
Scl< T > BigIntPow(unsigned long base, unsigned long exp) NRVAL(r)
Definition: BC.hpp:1125
mpz z
Definition: BC.hpp:160
#define NRVAL(name)
Definition: BC.hpp:855
void SqrtRem(Scl< T > &sqrt, Scl< T > &rem, const Scl< T > &b)
Definition: BC.hpp:803
Scl< T > & negate()
void pow(unsigned long exp)
Definition: BC.hpp:761
void sqrt()
Scl< T > & BigIntFactorial(unsigned long n)
Definition: BC.hpp:1117
void Div2Exp(unsigned long exponent_of_2)
signed long BigIntToSL(const Scl< T > &b)
Definition: BC.hpp:283
void HalfExtGCD(Scl< T > &gcd, Scl< T > &a, const Scl< T > &x, const Scl< T > &y)
Definition: BC.hpp:839
Scl< T > operator-=(const Scl< T > &rhs)
MSKaccmodee MSKint32t MSKsoltypee MSKstakeye MSKrealt MSKrealt * sl
Definition: mosek.h:3209
Scl< T > operator+(const Scl< T > &b1, const Scl< T > &b2)
Definition: BC.hpp:869
Scl< T > operator+=(const Scl< T > &rhs)
Scl< T > & operator/(const Scl< T > &b1, const Scl< T > &b2)
Definition: BC.hpp:890
Scl< T > & PowMod(const Scl< T > &b, const Scl< T > &exp, const Scl< T > &m)
Definition: BC.hpp:1047
bool IsOdd(const Scl< T > &b)
Definition: BC.hpp:370
MSKcallbackcodee MSKsoltypee MSKprostae MSKsolstae MSKstakeye MSKstakeye MSKstakeye MSKrealt MSKrealt MSKrealt * y
Definition: mosek.h:2689
bool operator>=(const Scl< T > &rhs) const
Scl< T > & operator*(const Scl< T > &b1, const Scl< T > &b2)
Definition: BC.hpp:883
void operator--()
MSKaccmodee MSKint32t MSKsoltypee MSKstakeye MSKrealt * x
Definition: mosek.h:3209
Scl< T > & pow(const Scl< T > &base, unsigned long exp)
Definition: BC.hpp:1075
int sign(const Scl< T > &b)
Definition: BC.hpp:307
void Mod2Exp(unsigned long exponent_of_2)
Scl< T > & operator-(const Scl< T > &b)
Definition: BC.hpp:862
void ExtGCD(Scl< T > &gcd, Scl< T > &a, Scl< T > &b, const Scl< T > &x, const Scl< T > &y)
Definition: BC.hpp:833
void quo(const Scl< T > &divisor)
Scl< T > & GCD(const Scl< T > &b1, const Scl< T > &b2)
Definition: BC.hpp:1019
Scl< T > operator*=(const Scl< T > &rhs)
void operator++()
bool operator==(const Scl< T > &rhs) const
void rem(const Scl< T > &divisor)
Scl< T > & rem(const Scl< T > &dividend, const Scl< T > &divisor)
Definition: BC.hpp:1096
bool IsNegative(const Scl< T > &b)
Definition: BC.hpp:346
Definition: BC.hpp:23
void QuoRem(Scl< T > &q, Scl< T > &r, const Scl< T > &divdend, const Scl< T > &divisor)
Definition: BC.hpp:809
bool operator<=(const Scl< T > &rhs) const
Scl< T > & pow(int exp)
Scl< T > operator/=(const Scl< T > &rhs)
int compare(const Scl< T > &b1, const Scl< T > &b2)
Definition: BC.hpp:319
Definition: Scl.hpp:26
void Mul2Exp(unsigned long exponent_of_2)
Scl< T > & sqrt(const Scl< T > &b)
Definition: BC.hpp:1103
Scl< T > & abs(const Scl< T > &b)
Definition: BC.hpp:1061
void rem(const Scl< T > &divisor)
Definition: BC.hpp:785
Scl< T > operator=(const Scl< T > &rhs)
Scl< T > & Mul2Exp(const Scl< T > &b, unsigned long exponent_of_2)
Definition: BC.hpp:1040
MSKrescodee r
Definition: mosek.h:2321
Scl< T > operator%=(const Scl< T > &rhs)
bool IsOne(const Scl< T > &b)
Definition: BC.hpp:358
Scl< T > & operator%(const Scl< T > &b1, const Scl< T > &b2)
Definition: BC.hpp:897
#define NONRCODE(code)
Definition: BC.hpp:857
bool operator>(const Scl< T > &rhs) const
MSKstreamtypee MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t a
Definition: mosek.h:3833
bool IsEven(const Scl< T > &b)
Definition: BC.hpp:376
void DivMod(Scl< T > &q, Scl< T > &r, const Scl< T > &divdend, const Scl< T > &divisor)
Definition: BC.hpp:821
bool IsPerfectSquare(const Scl< T > &b)
Definition: BC.hpp:382
Scl< T > & Div2Exp(const Scl< T > &b, unsigned long exponent_of_2)
Definition: BC.hpp:1012
void abs()
#define NRCODE(code)
Definition: BC.hpp:856
Scl< T > & quo(const Scl< T > &dividend, const Scl< T > &divisor)
Definition: BC.hpp:1082
bool IsProbablyPrime(const Scl< T > &b, int reps)
Definition: BC.hpp:388
void GCD(const Scl< T > &b2)
bool IsPositive(const Scl< T > &b)
Definition: BC.hpp:340
void factorial(unsigned long n)
Scl< T > & Mod2Exp(const Scl< T > &b, unsigned long exponent_of_2)
Definition: BC.hpp:1033
bool operator!=(const Scl< T > &rhs) const
bool IsZero(const Scl< T > &b)
Definition: BC.hpp:352
bool operator<(const Scl< T > &rhs) const
size_t log(const Scl< T > &b)
Definition: BC.hpp:292
Scl< T > & gcd(const Scl< T > &b1, const Scl< T > &b2)
Definition: BC.hpp:1026
void factorial(unsigned long n)
Definition: BC.hpp:749
bool IsMinusOne(const Scl< T > &b)
Definition: BC.hpp:364
Home  |  Download & InstallContributions