My Project
Loading...
Searching...
No Matches
longrat.cc
Go to the documentation of this file.
1/****************************************
2* Computer Algebra System SINGULAR *
3****************************************/
4/*
5* ABSTRACT: computation with long rational numbers (Hubert Grassmann)
6*/
7
8#include "misc/auxiliary.h"
9
10#include "factory/factory.h"
11
12#include "misc/sirandom.h"
13#include "misc/prime.h"
14#include "reporter/reporter.h"
15
16#include "coeffs/coeffs.h"
17#include "coeffs/numbers.h"
18#include "coeffs/rmodulon.h" // ZnmInfo
19#include "coeffs/longrat.h"
20#include "coeffs/shortfl.h"
21#include "coeffs/modulop.h"
22#include "coeffs/mpr_complex.h"
23
24#include <string.h>
25#include <float.h>
26
27// allow inlining only from p_Numbers.h and if ! LDEBUG
28#if defined(DO_LINLINE) && defined(P_NUMBERS_H) && !defined(LDEBUG)
29#define LINLINE static FORCE_INLINE
30#else
31#define LINLINE
32#undef DO_LINLINE
33#endif // DO_LINLINE
34
36LINLINE number nlInit(long i, const coeffs r);
41LINLINE void nlDelete(number *a, const coeffs r);
46LINLINE void nlInpAdd(number &a, number b, const coeffs r);
47LINLINE void nlInpMult(number &a, number b, const coeffs r);
48
49number nlRInit (long i);
50
51
52// number nlInitMPZ(mpz_t m, const coeffs r);
53// void nlMPZ(mpz_t m, number &n, const coeffs r);
54
55void nlNormalize(number &x, const coeffs r);
56
57number nlGcd(number a, number b, const coeffs r);
59number nlNormalizeHelper(number a, number b, const coeffs r); /*special routine !*/
61BOOLEAN nlIsMOne(number a, const coeffs r);
62long nlInt(number &n, const coeffs r);
64
66number nlInvers(number a, const coeffs r);
67number nlDiv(number a, number b, const coeffs r);
69number nlIntDiv(number a, number b, const coeffs r);
70number nlIntMod(number a, number b, const coeffs r);
71void nlPower(number x, int exp, number *lu, const coeffs r);
72const char * nlRead (const char *s, number *a, const coeffs r);
73void nlWrite(number a, const coeffs r);
74
76
77#ifdef LDEBUG
78BOOLEAN nlDBTest(number a, const char *f, const int l);
79#endif
80
81nMapFunc nlSetMap(const coeffs src, const coeffs dst);
82
83// in-place operations
84void nlInpIntDiv(number &a, number b, const coeffs r);
85
86#ifdef LDEBUG
87#define nlTest(a, r) nlDBTest(a,__FILE__,__LINE__, r)
88BOOLEAN nlDBTest(number a, const char *f,int l, const coeffs r);
89#else
90#define nlTest(a, r) do {} while (0)
91#endif
92
93
94// 64 bit version:
95//#if SIZEOF_LONG == 8
96#if 0
97#define MAX_NUM_SIZE 60
98#define POW_2_28 (1L<<60)
99#define POW_2_28_32 (1L<<28)
100#define LONG long
101#else
102#define MAX_NUM_SIZE 28
103#define POW_2_28 (1L<<28)
104#define POW_2_28_32 (1L<<28)
105#define LONG int
106#endif
107
108
109static inline number nlShort3(number x) // assume x->s==3
110{
111 assume(x->s==3);
112 if (mpz_sgn1(x->z)==0)
113 {
114 mpz_clear(x->z);
116 return INT_TO_SR(0);
117 }
118 if (mpz_size1(x->z)<=MP_SMALL)
119 {
120 LONG ui=mpz_get_si(x->z);
121 if ((((ui<<3)>>3)==ui)
122 && (mpz_cmp_si(x->z,(long)ui)==0))
123 {
124 mpz_clear(x->z);
126 return INT_TO_SR(ui);
127 }
128 }
129 return x;
130}
131
132#ifndef LONGRAT_CC
133#define LONGRAT_CC
134
135#ifndef BYTES_PER_MP_LIMB
136#define BYTES_PER_MP_LIMB sizeof(mp_limb_t)
137#endif
138
139//#define SR_HDL(A) ((long)(A))
140/*#define SR_INT 1L*/
141/*#define INT_TO_SR(INT) ((number) (((long)INT << 2) + SR_INT))*/
142// #define SR_TO_INT(SR) (((long)SR) >> 2)
143
144#define MP_SMALL 1
145//#define mpz_isNeg(A) (mpz_sgn1(A)<0)
146#define mpz_isNeg(A) ((A)->_mp_size<0)
147#define mpz_limb_size(A) ((A)->_mp_size)
148#define mpz_limb_d(A) ((A)->_mp_d)
149
150void _nlDelete_NoImm(number *a);
151
152/***************************************************************
153 *
154 * Routines which are never inlined by p_Numbers.h
155 *
156 *******************************************************************/
157#ifndef P_NUMBERS_H
158
160{
161 return nlShort3(x);
162}
163
165{
166 number z = ALLOC_RNUMBER();
167 z->s = 3;
168 #ifdef LDEBUG
169 z->debug=123456;
170 #endif
171 mpz_init_set(z->z, m);
172 z=nlShort3(z);
173 return z;
174}
175
176#if (__GNU_MP_VERSION*10+__GNU_MP_VERSION_MINOR < 31)
177void mpz_mul_si (mpz_ptr r, mpz_srcptr s, long int si)
178{
179 if (si>=0)
180 mpz_mul_ui(r,s,si);
181 else
182 {
183 mpz_mul_ui(r,s,-si);
184 mpz_neg(r,r);
185 }
186}
187#endif
188
189static number nlMapP(number from, const coeffs src, const coeffs dst)
190{
191 assume( getCoeffType(src) == n_Zp );
192
193 number to = nlInit(npInt(from,src), dst); // FIXME? TODO? // extern long npInt (number &n, const coeffs r);
194
195 return to;
196}
197
198static number nlMapLongR(number from, const coeffs src, const coeffs dst);
199static number nlMapR(number from, const coeffs src, const coeffs dst);
200
201
202#ifdef HAVE_RINGS
203/*2
204* convert from a GMP integer
205*/
206static inline number nlMapGMP(number from, const coeffs /*src*/, const coeffs dst)
207{
208 return nlInitMPZ((mpz_ptr)from,dst);
209}
210
211number nlMapZ(number from, const coeffs /*src*/, const coeffs dst)
212{
213 if (SR_HDL(from) & SR_INT)
214 {
215 return from;
216 }
217 return nlInitMPZ((mpz_ptr)from,dst);
218}
219
220/*2
221* convert from an machine long
222*/
223number nlMapMachineInt(number from, const coeffs /*src*/, const coeffs /*dst*/)
224{
226#if defined(LDEBUG)
227 z->debug=123456;
228#endif
229 mpz_init_set_ui(z->z,(unsigned long) from);
230 z->s = 3;
231 z=nlShort3(z);
232 return z;
233}
234#endif
235
236
237#ifdef LDEBUG
238BOOLEAN nlDBTest(number a, const char *f,const int l, const coeffs /*r*/)
239{
240 if (a==NULL)
241 {
242 Print("!!longrat: NULL in %s:%d\n",f,l);
243 return FALSE;
244 }
245 //if ((int)a==1) Print("!! 0x1 as number ? %s %d\n",f,l);
246 if ((((long)a)&3L)==3L)
247 {
248 Print(" !!longrat:ptr(3) in %s:%d\n",f,l);
249 return FALSE;
250 }
251 if ((((long)a)&3L)==1L)
252 {
253 if (((((LONG)(long)a)<<1)>>1)!=((LONG)(long)a))
254 {
255 Print(" !!longrat:arith:%lx in %s:%d\n",(long)a, f,l);
256 return FALSE;
257 }
258 return TRUE;
259 }
260 /* TODO: If next line is active, then computations in algebraic field
261 extensions over Q will throw a lot of assume violations although
262 everything is computed correctly and no seg fault appears.
263 Maybe the test is not appropriate in this case. */
264 omCheckIf(omCheckAddrSize(a,sizeof(*a)), return FALSE);
265 if (a->debug!=123456)
266 {
267 Print("!!longrat:debug:%d in %s:%d\n",a->debug,f,l);
268 a->debug=123456;
269 return FALSE;
270 }
271 if ((a->s<0)||(a->s>4))
272 {
273 Print("!!longrat:s=%d in %s:%d\n",a->s,f,l);
274 return FALSE;
275 }
276 /* TODO: If next line is active, then computations in algebraic field
277 extensions over Q will throw a lot of assume violations although
278 everything is computed correctly and no seg fault appears.
279 Maybe the test is not appropriate in this case. */
280 //omCheckAddrSize(a->z[0]._mp_d,a->z[0]._mp_alloc*BYTES_PER_MP_LIMB);
281 if (a->z[0]._mp_alloc==0)
282 Print("!!longrat:z->alloc=0 in %s:%d\n",f,l);
283
284 if (a->s<2)
285 {
286 if ((a->n[0]._mp_d[0]==0)&&(a->n[0]._mp_alloc<=1))
287 {
288 Print("!!longrat: n==0 in %s:%d\n",f,l);
289 return FALSE;
290 }
291 /* TODO: If next line is active, then computations in algebraic field
292 extensions over Q will throw a lot of assume violations although
293 everything is computed correctly and no seg fault appears.
294 Maybe the test is not appropriate in this case. */
295 //omCheckIf(omCheckAddrSize(a->n[0]._mp_d,a->n[0]._mp_alloc*BYTES_PER_MP_LIMB), return FALSE);
296 if (a->z[0]._mp_alloc==0)
297 Print("!!longrat:n->alloc=0 in %s:%d\n",f,l);
298 if ((mpz_size1(a->n) ==1) && (mpz_cmp_si(a->n,1L)==0))
299 {
300 Print("!!longrat:integer as rational in %s:%d\n",f,l);
301 mpz_clear(a->n); a->s=3;
302 return FALSE;
303 }
304 else if (mpz_isNeg(a->n))
305 {
306 Print("!!longrat:div. by negative in %s:%d\n",f,l);
307 mpz_neg(a->z,a->z);
308 mpz_neg(a->n,a->n);
309 return FALSE;
310 }
311 return TRUE;
312 }
313 //if (a->s==2)
314 //{
315 // Print("!!longrat:s=2 in %s:%d\n",f,l);
316 // return FALSE;
317 //}
318 if (mpz_size1(a->z)>MP_SMALL) return TRUE;
319 LONG ui=(LONG)mpz_get_si(a->z);
320 if ((((ui<<3)>>3)==ui)
321 && (mpz_cmp_si(a->z,(long)ui)==0))
322 {
323 Print("!!longrat:im int %d in %s:%d\n",ui,f,l);
324 return FALSE;
325 }
326 return TRUE;
327}
328#endif
329
331{
332 if (setChar) setCharacteristic( 0 );
333
335 if ( SR_HDL(n) & SR_INT )
336 {
337 long nn=SR_TO_INT(n);
338 term = nn;
339 }
340 else
341 {
342 if ( n->s == 3 )
343 {
344 mpz_t dummy;
345 long lz=mpz_get_si(n->z);
346 if (mpz_cmp_si(n->z,lz)==0) term=lz;
347 else
348 {
349 mpz_init_set( dummy,n->z );
350 term = make_cf( dummy );
351 }
352 }
353 else
354 {
355 // assume s==0 or s==1
356 mpz_t num, den;
358 mpz_init_set( num, n->z );
359 mpz_init_set( den, n->n );
360 term = make_cf( num, den, ( n->s != 1 ));
361 }
362 }
363 return term;
364}
365
366number nlRInit (long i);
367
369{
370 if (f.isImm())
371 {
372 return nlInit(f.intval(),r);
373 }
374 else
375 {
376 number z = ALLOC_RNUMBER();
377#if defined(LDEBUG)
378 z->debug=123456;
379#endif
380 gmp_numerator( f, z->z );
381 if ( f.den().isOne() )
382 {
383 z->s = 3;
384 z=nlShort3(z);
385 }
386 else
387 {
388 gmp_denominator( f, z->n );
389 z->s = 1;
390 }
391 return z;
392 }
393}
394
395static number nlMapR(number from, const coeffs src, const coeffs dst)
396{
397 assume( getCoeffType(src) == n_R );
398
399 double f=nrFloat(from); // FIXME? TODO? // extern float nrFloat(number n);
400 if (f==0.0) return INT_TO_SR(0);
401 int f_sign=1;
402 if (f<0.0)
403 {
404 f_sign=-1;
405 f=-f;
406 }
407 int i=0;
408 mpz_t h1;
410 while((FLT_RADIX*f) < DBL_MAX && i<DBL_MANT_DIG)
411 {
412 f*=FLT_RADIX;
414 i++;
415 }
416 number re=nlRInit(1);
417 mpz_set_d(re->z,f);
418 memcpy(&(re->n),&h1,sizeof(h1));
419 re->s=0; /* not normalized */
420 if(f_sign==-1) re=nlNeg(re,dst);
422 return re;
423}
424
425static number nlMapR_BI(number from, const coeffs src, const coeffs dst)
426{
427 assume( getCoeffType(src) == n_R );
428
429 double f=nrFloat(from); // FIXME? TODO? // extern float nrFloat(number n);
430 if (f==0.0) return INT_TO_SR(0);
431 long l=long(f);
432 return nlInit(l,dst);
433}
434
435static number nlMapLongR(number from, const coeffs src, const coeffs dst)
436{
437 assume( getCoeffType(src) == n_long_R );
438
439 gmp_float *ff=(gmp_float*)from;
440 mpf_t *f=ff->_mpfp();
441 number res;
442 mpz_ptr dest,ndest;
443 int size, i,negative;
444 int e,al,bl;
445 mp_ptr qp,dd,nn;
446
447 size = (*f)[0]._mp_size;
448 if (size == 0)
449 return INT_TO_SR(0);
450 if(size<0)
451 {
452 negative = 1;
453 size = -size;
454 }
455 else
456 negative = 0;
457
458 qp = (*f)[0]._mp_d;
459 while(qp[0]==0)
460 {
461 qp++;
462 size--;
463 }
464
465 e=(*f)[0]._mp_exp-size;
466 res = ALLOC_RNUMBER();
467#if defined(LDEBUG)
468 res->debug=123456;
469#endif
470 dest = res->z;
471
472 void* (*allocfunc) (size_t);
474 if (e<0)
475 {
476 al = dest->_mp_size = size;
477 if (al<2) al = 2;
478 dd = (mp_ptr)allocfunc(sizeof(mp_limb_t)*al);
479 for (i=0;i<size;i++) dd[i] = qp[i];
480 bl = 1-e;
481 nn = (mp_ptr)allocfunc(sizeof(mp_limb_t)*bl);
482 memset(nn,0,sizeof(mp_limb_t)*bl);
483 nn[bl-1] = 1;
484 ndest = res->n;
485 ndest->_mp_d = nn;
486 ndest->_mp_alloc = ndest->_mp_size = bl;
487 res->s = 0;
488 }
489 else
490 {
491 al = dest->_mp_size = size+e;
492 if (al<2) al = 2;
493 dd = (mp_ptr)allocfunc(sizeof(mp_limb_t)*al);
494 memset(dd,0,sizeof(mp_limb_t)*al);
495 for (i=0;i<size;i++) dd[i+e] = qp[i];
496 for (i=0;i<e;i++) dd[i] = 0;
497 res->s = 3;
498 }
499
500 dest->_mp_d = dd;
501 dest->_mp_alloc = al;
502 if (negative) mpz_neg(dest,dest);
503
504 if (res->s==0)
506 else if (mpz_size1(res->z)<=MP_SMALL)
507 {
508 // res is new, res->ref is 1
510 }
511 nlTest(res, dst);
512 return res;
513}
514
515static number nlMapLongR_BI(number from, const coeffs src, const coeffs dst)
516{
517 assume( getCoeffType(src) == n_long_R );
518
519 gmp_float *ff=(gmp_float*)from;
520 if (mpf_fits_slong_p(ff->t))
521 {
522 long l=mpf_get_si(ff->t);
523 return nlInit(l,dst);
524 }
525 char *out=floatToStr(*(gmp_float*)from, src->float_len);
526 char *p=strchr(out,'.');
527 *p='\0';
528 number res;
529 res = ALLOC_RNUMBER();
530#if defined(LDEBUG)
531 res->debug=123456;
532#endif
533 res->s=3;
534 mpz_init(res->z);
535 if (out[0]=='-')
536 {
537 mpz_set_str(res->z,out+1,10);
538 res=nlNeg(res,dst);
539 }
540 else
541 {
542 mpz_set_str(res->z,out,10);
543 }
544 omFree( (void *)out );
545 return res;
546}
547
548static number nlMapC(number from, const coeffs src, const coeffs dst)
549{
550 assume( getCoeffType(src) == n_long_C );
551 if ( ! ((gmp_complex*)from)->imag().isZero() )
552 return INT_TO_SR(0);
553
554 if (dst->is_field==FALSE) /* ->ZZ */
555 {
556 char *s=floatToStr(((gmp_complex*)from)->real(),src->float_len);
557 mpz_t z;
558 mpz_init(z);
559 char *ss=nEatLong(s,z);
560 if (*ss=='\0')
561 {
562 omFree(s);
563 number n=nlInitMPZ(z,dst);
564 mpz_clear(z);
565 return n;
566 }
567 omFree(s);
568 mpz_clear(z);
569 WarnS("conversion problem in CC -> ZZ mapping");
570 return INT_TO_SR(0);
571 }
572
573 gmp_float gfl = ((gmp_complex*)from)->real();
574 mpf_t *f = gfl._mpfp();
575
576 number res;
577 mpz_ptr dest,ndest;
578 int size, i,negative;
579 int e,al,bl;
580 mp_ptr qp,dd,nn;
581
582 size = (*f)[0]._mp_size;
583 if (size == 0)
584 return INT_TO_SR(0);
585 if(size<0)
586 {
587 negative = 1;
588 size = -size;
589 }
590 else
591 negative = 0;
592
593 qp = (*f)[0]._mp_d;
594 while(qp[0]==0)
595 {
596 qp++;
597 size--;
598 }
599
600 e=(*f)[0]._mp_exp-size;
601 res = ALLOC_RNUMBER();
602#if defined(LDEBUG)
603 res->debug=123456;
604#endif
605 dest = res->z;
606
607 void* (*allocfunc) (size_t);
609 if (e<0)
610 {
611 al = dest->_mp_size = size;
612 if (al<2) al = 2;
613 dd = (mp_ptr)allocfunc(sizeof(mp_limb_t)*al);
614 for (i=0;i<size;i++) dd[i] = qp[i];
615 bl = 1-e;
616 nn = (mp_ptr)allocfunc(sizeof(mp_limb_t)*bl);
617 memset(nn,0,sizeof(mp_limb_t)*bl);
618 nn[bl-1] = 1;
619 ndest = res->n;
620 ndest->_mp_d = nn;
621 ndest->_mp_alloc = ndest->_mp_size = bl;
622 res->s = 0;
623 }
624 else
625 {
626 al = dest->_mp_size = size+e;
627 if (al<2) al = 2;
628 dd = (mp_ptr)allocfunc(sizeof(mp_limb_t)*al);
629 memset(dd,0,sizeof(mp_limb_t)*al);
630 for (i=0;i<size;i++) dd[i+e] = qp[i];
631 for (i=0;i<e;i++) dd[i] = 0;
632 res->s = 3;
633 }
634
635 dest->_mp_d = dd;
636 dest->_mp_alloc = al;
637 if (negative) mpz_neg(dest,dest);
638
639 if (res->s==0)
641 else if (mpz_size1(res->z)<=MP_SMALL)
642 {
643 // res is new, res->ref is 1
645 }
646 nlTest(res, dst);
647 return res;
648}
649
650//static number nlMapLongR(number from)
651//{
652// gmp_float *ff=(gmp_float*)from;
653// const mpf_t *f=ff->mpfp();
654// int f_size=ABS((*f)[0]._mp_size);
655// if (f_size==0)
656// return nlInit(0);
657// int f_sign=1;
658// number work=ngcCopy(from);
659// if (!ngcGreaterZero(work))
660// {
661// f_sign=-1;
662// work=ngcNeg(work);
663// }
664// int i=0;
665// mpz_t h1;
666// mpz_init_set_ui(h1,1);
667// while((FLT_RADIX*f) < DBL_MAX && i<DBL_MANT_DIG)
668// {
669// f*=FLT_RADIX;
670// mpz_mul_ui(h1,h1,FLT_RADIX);
671// i++;
672// }
673// number r=nlRInit(1);
674// mpz_set_d(&(r->z),f);
675// memcpy(&(r->n),&h1,sizeof(h1));
676// r->s=0; /* not normalized */
677// nlNormalize(r);
678// return r;
679//
680//
681// number r=nlRInit(1);
682// int f_shift=f_size+(*f)[0]._mp_exp;
683// if ( f_shift > 0)
684// {
685// r->s=0;
686// mpz_init(&r->n);
687// mpz_setbit(&r->n,f_shift*BYTES_PER_MP_LIMB*8);
688// mpz_setbit(&r->z,f_size*BYTES_PER_MP_LIMB*8-1);
689// // now r->z has enough space
690// memcpy(mpz_limb_d(&r->z),((*f)[0]._mp_d),f_size*BYTES_PER_MP_LIMB);
691// nlNormalize(r);
692// }
693// else
694// {
695// r->s=3;
696// if (f_shift==0)
697// {
698// mpz_setbit(&r->z,f_size*BYTES_PER_MP_LIMB*8-1);
699// // now r->z has enough space
700// memcpy(mpz_limb_d(&r->z),((*f)[0]._mp_d),f_size*BYTES_PER_MP_LIMB);
701// }
702// else /* f_shift < 0 */
703// {
704// mpz_setbit(&r->z,(f_size-f_shift)*BYTES_PER_MP_LIMB*8-1);
705// // now r->z has enough space
706// memcpy(mpz_limb_d(&r->z)-f_shift,((*f)[0]._mp_d),
707// f_size*BYTES_PER_MP_LIMB);
708// }
709// }
710// if ((*f)[0]._mp_size<0);
711// r=nlNeg(r);
712// return r;
713//}
714
715int nlSize(number a, const coeffs)
716{
717 if (a==INT_TO_SR(0))
718 return 0; /* rational 0*/
719 if (SR_HDL(a) & SR_INT)
720 return 1; /* immediate int */
721 int s=a->z[0]._mp_alloc;
722// while ((s>0) &&(a->z._mp_d[s]==0L)) s--;
723//#if SIZEOF_LONG == 8
724// if (a->z._mp_d[s] < (unsigned long)0x100000000L) s=s*2-1;
725// else s *=2;
726//#endif
727// s++;
728 if (a->s<2)
729 {
730 int d=a->n[0]._mp_alloc;
731// while ((d>0) && (a->n._mp_d[d]==0L)) d--;
732//#if SIZEOF_LONG == 8
733// if (a->n._mp_d[d] < (unsigned long)0x100000000L) d=d*2-1;
734// else d *=2;
735//#endif
736 s+=d;
737 }
738 return s;
739}
740
741/*2
742* convert number to int
743*/
744long nlInt(number &i, const coeffs r)
745{
746 nlTest(i, r);
747 nlNormalize(i,r);
748 if (SR_HDL(i) & SR_INT)
749 {
750 return SR_TO_INT(i);
751 }
752 if (i->s==3)
753 {
754 if(mpz_size1(i->z)>MP_SMALL) return 0;
755 long ul=mpz_get_si(i->z);
756 if (mpz_cmp_si(i->z,ul)!=0) return 0;
757 return ul;
758 }
759 mpz_t tmp;
760 long ul;
761 mpz_init(tmp);
762 mpz_tdiv_q(tmp,i->z,i->n);
763 if(mpz_size1(tmp)>MP_SMALL) ul=0;
764 else
765 {
767 if (mpz_cmp_si(tmp,ul)!=0) ul=0;
768 }
769 mpz_clear(tmp);
770 return ul;
771}
772
773/*2
774* convert number to bigint
775*/
777{
778 nlTest(i, r);
779 nlNormalize(i,r);
780 if (SR_HDL(i) & SR_INT) return (i);
781 if (i->s==3)
782 {
783 return nlCopy(i,r);
784 }
785 number tmp=nlRInit(1);
786 mpz_tdiv_q(tmp->z,i->z,i->n);
788 return tmp;
789}
790
791/*
792* 1/a
793*/
795{
796 nlTest(a, r);
797 number n;
798 if (SR_HDL(a) & SR_INT)
799 {
800 if ((a==INT_TO_SR(1L)) || (a==INT_TO_SR(-1L)))
801 {
802 return a;
803 }
804 if (nlIsZero(a,r))
805 {
807 return INT_TO_SR(0);
808 }
809 n=ALLOC_RNUMBER();
810#if defined(LDEBUG)
811 n->debug=123456;
812#endif
813 n->s=1;
814 if (((long)a)>0L)
815 {
816 mpz_init_set_ui(n->z,1L);
817 mpz_init_set_si(n->n,(long)SR_TO_INT(a));
818 }
819 else
820 {
821 mpz_init_set_si(n->z,-1L);
822 mpz_init_set_si(n->n,(long)-SR_TO_INT(a));
823 }
824 nlTest(n, r);
825 return n;
826 }
827 n=ALLOC_RNUMBER();
828#if defined(LDEBUG)
829 n->debug=123456;
830#endif
831 {
832 mpz_init_set(n->n,a->z);
833 switch (a->s)
834 {
835 case 0:
836 case 1:
837 n->s=a->s;
838 mpz_init_set(n->z,a->n);
839 if (mpz_isNeg(n->n)) /* && n->s<2*/
840 {
841 mpz_neg(n->z,n->z);
842 mpz_neg(n->n,n->n);
843 }
844 if (mpz_cmp_ui(n->n,1L)==0)
845 {
846 mpz_clear(n->n);
847 n->s=3;
848 n=nlShort3(n);
849 }
850 break;
851 case 3:
852 // i.e. |a| > 2^...
853 n->s=1;
854 if (mpz_isNeg(n->n)) /* && n->s<2*/
855 {
856 mpz_neg(n->n,n->n);
857 mpz_init_set_si(n->z,-1L);
858 }
859 else
860 {
861 mpz_init_set_ui(n->z,1L);
862 }
863 break;
864 }
865 }
866 nlTest(n, r);
867 return n;
868}
869
870
871/*2
872* u := a / b in Z, if b | a (else undefined)
873*/
875{
876 if (b==INT_TO_SR(0))
877 {
879 return INT_TO_SR(0);
880 }
881 number u;
882 if (SR_HDL(a) & SR_HDL(b) & SR_INT)
883 {
884 /* the small int -(1<<28) divided by -1 is the large int (1<<28) */
885 if ((a==INT_TO_SR(-(POW_2_28)))&&(b==INT_TO_SR(-1L)))
886 {
887 return nlRInit(POW_2_28);
888 }
889 long aa=SR_TO_INT(a);
890 long bb=SR_TO_INT(b);
891 return INT_TO_SR(aa/bb);
892 }
893 number aa=NULL;
894 number bb=NULL;
895 if (SR_HDL(a) & SR_INT)
896 {
897 aa=nlRInit(SR_TO_INT(a));
898 a=aa;
899 }
900 if (SR_HDL(b) & SR_INT)
901 {
903 b=bb;
904 }
905 u=ALLOC_RNUMBER();
906#if defined(LDEBUG)
907 u->debug=123456;
908#endif
909 mpz_init(u->z);
910 /* u=a/b */
911 u->s = 3;
912 assume(a->s==3);
913 assume(b->s==3);
914 mpz_divexact(u->z,a->z,b->z);
915 if (aa!=NULL)
916 {
917 mpz_clear(aa->z);
918#if defined(LDEBUG)
919 aa->debug=654324;
920#endif
921 FREE_RNUMBER(aa); // omFreeBin((void *)aa, rnumber_bin);
922 }
923 if (bb!=NULL)
924 {
925 mpz_clear(bb->z);
926#if defined(LDEBUG)
927 bb->debug=654324;
928#endif
929 FREE_RNUMBER(bb); // omFreeBin((void *)bb, rnumber_bin);
930 }
931 u=nlShort3(u);
932 nlTest(u, r);
933 return u;
934}
935
936/*2
937* u := a / b in Z
938*/
940{
941 if (b==INT_TO_SR(0))
942 {
944 return INT_TO_SR(0);
945 }
946 number u;
947 if (SR_HDL(a) & SR_HDL(b) & SR_INT)
948 {
949 /* the small int -(1<<28) divided by -1 is the large int (1<<28) */
950 if ((a==INT_TO_SR(-(POW_2_28)))&&(b==INT_TO_SR(-1L)))
951 {
952 return nlRInit(POW_2_28);
953 }
954 LONG aa=SR_TO_INT(a);
956 LONG rr=aa%bb;
957 if (rr<0) rr+=ABS(bb);
958 LONG cc=(aa-rr)/bb;
959 return INT_TO_SR(cc);
960 }
961 number aa=NULL;
962 if (SR_HDL(a) & SR_INT)
963 {
964 /* the small int -(1<<28) divided by 2^28 is 1 */
965 if (a==INT_TO_SR(-(POW_2_28)))
966 {
967 if(mpz_cmp_si(b->z,(POW_2_28))==0)
968 {
969 return INT_TO_SR(-1);
970 }
971 }
972 aa=nlRInit(SR_TO_INT(a));
973 a=aa;
974 }
975 number bb=NULL;
976 if (SR_HDL(b) & SR_INT)
977 {
979 b=bb;
980 }
981 u=ALLOC_RNUMBER();
982#if defined(LDEBUG)
983 u->debug=123456;
984#endif
985 assume(a->s==3);
986 assume(b->s==3);
987 /* u=u/b */
988 mpz_t rr;
989 mpz_init(rr);
990 mpz_mod(rr,a->z,b->z);
991 u->s = 3;
992 mpz_init(u->z);
993 mpz_sub(u->z,a->z,rr);
994 mpz_clear(rr);
995 mpz_divexact(u->z,u->z,b->z);
996 if (aa!=NULL)
997 {
998 mpz_clear(aa->z);
999#if defined(LDEBUG)
1000 aa->debug=654324;
1001#endif
1003 }
1004 if (bb!=NULL)
1005 {
1006 mpz_clear(bb->z);
1007#if defined(LDEBUG)
1008 bb->debug=654324;
1009#endif
1011 }
1012 u=nlShort3(u);
1013 nlTest(u,r);
1014 return u;
1015}
1016
1017/*2
1018* u := a mod b in Z, u>=0
1019*/
1021{
1022 if (b==INT_TO_SR(0))
1023 {
1025 return INT_TO_SR(0);
1026 }
1027 if (a==INT_TO_SR(0))
1028 return INT_TO_SR(0);
1029 number u;
1030 if (SR_HDL(a) & SR_HDL(b) & SR_INT)
1031 {
1032 LONG aa=SR_TO_INT(a);
1033 LONG bb=SR_TO_INT(b);
1034 LONG c=aa % bb;
1035 if (c<0) c+=ABS(bb);
1036 return INT_TO_SR(c);
1037 }
1038 if (SR_HDL(a) & SR_INT)
1039 {
1040 LONG ai=SR_TO_INT(a);
1041 mpz_t aa;
1043 u=ALLOC_RNUMBER();
1044#if defined(LDEBUG)
1045 u->debug=123456;
1046#endif
1047 u->s = 3;
1048 mpz_init(u->z);
1049 mpz_mod(u->z, aa, b->z);
1050 mpz_clear(aa);
1051 u=nlShort3(u);
1052 nlTest(u,r);
1053 return u;
1054 }
1055 number bb=NULL;
1056 if (SR_HDL(b) & SR_INT)
1057 {
1059 b=bb;
1060 }
1061 u=ALLOC_RNUMBER();
1062#if defined(LDEBUG)
1063 u->debug=123456;
1064#endif
1065 mpz_init(u->z);
1066 u->s = 3;
1067 mpz_mod(u->z, a->z, b->z);
1068 if (bb!=NULL)
1069 {
1070 mpz_clear(bb->z);
1071#if defined(LDEBUG)
1072 bb->debug=654324;
1073#endif
1075 }
1076 u=nlShort3(u);
1077 nlTest(u,r);
1078 return u;
1079}
1080
1082{
1083 if (SR_HDL(a) & SR_HDL(b) & SR_INT)
1084 {
1085 return ((SR_TO_INT(a) % SR_TO_INT(b))==0);
1086 }
1087 if (SR_HDL(b) & SR_INT)
1088 {
1089 return (mpz_divisible_ui_p(a->z,SR_TO_INT(b))!=0);
1090 }
1091 if (SR_HDL(a) & SR_INT) return FALSE;
1092 return mpz_divisible_p(a->z, b->z) != 0;
1093}
1094
1096{
1097 if (nlDivBy(a, b, r))
1098 {
1099 if (nlDivBy(b, a, r)) return 2;
1100 return -1;
1101 }
1102 if (nlDivBy(b, a, r)) return 1;
1103 return 0;
1104}
1105
1107{
1108 if (nlGreaterZero(n,cf)) return INT_TO_SR(1);
1109 else return INT_TO_SR(-1);
1110}
1111
1113{
1114 long ch = r->cfInt(c, r);
1115 int p=IsPrime(ch);
1116 coeffs rr=NULL;
1117 if (((long)p)==ch)
1118 {
1119 rr = nInitChar(n_Zp,(void*)ch);
1120 }
1121 #ifdef HAVE_RINGS
1122 else
1123 {
1124 mpz_t dummy;
1126 ZnmInfo info;
1127 info.base = dummy;
1128 info.exp = (unsigned long) 1;
1129 rr = nInitChar(n_Zn, (void*)&info);
1131 }
1132 #endif
1133 return(rr);
1134}
1135
1136
1138{
1139 return ((SR_HDL(a) & SR_INT) && (ABS(SR_TO_INT(a))==1));
1140}
1141
1142
1143/*2
1144* u := a / b
1145*/
1147{
1148 if (nlIsZero(b,r))
1149 {
1151 return INT_TO_SR(0);
1152 }
1153 number u;
1154// ---------- short / short ------------------------------------
1155 if (SR_HDL(a) & SR_HDL(b) & SR_INT)
1156 {
1157 LONG i=SR_TO_INT(a);
1158 LONG j=SR_TO_INT(b);
1159 if (j==1L) return a;
1160 if ((i==-POW_2_28) && (j== -1L))
1161 {
1162 return nlRInit(POW_2_28);
1163 }
1164 LONG r=i%j;
1165 if (r==0)
1166 {
1167 return INT_TO_SR(i/j);
1168 }
1169 u=ALLOC_RNUMBER();
1170 u->s=0;
1171 #if defined(LDEBUG)
1172 u->debug=123456;
1173 #endif
1174 mpz_init_set_si(u->z,(long)i);
1175 mpz_init_set_si(u->n,(long)j);
1176 }
1177 else
1178 {
1179 u=ALLOC_RNUMBER();
1180 u->s=0;
1181 #if defined(LDEBUG)
1182 u->debug=123456;
1183 #endif
1184 mpz_init(u->z);
1185// ---------- short / long ------------------------------------
1186 if (SR_HDL(a) & SR_INT)
1187 {
1188 // short a / (z/n) -> (a*n)/z
1189 if (b->s<2)
1190 {
1191 mpz_mul_si(u->z,b->n,SR_TO_INT(a));
1192 }
1193 else
1194 // short a / long z -> a/z
1195 {
1196 mpz_set_si(u->z,SR_TO_INT(a));
1197 }
1198 if (mpz_cmp(u->z,b->z)==0)
1199 {
1200 mpz_clear(u->z);
1201 FREE_RNUMBER(u);
1202 return INT_TO_SR(1);
1203 }
1204 mpz_init_set(u->n,b->z);
1205 }
1206// ---------- long / short ------------------------------------
1207 else if (SR_HDL(b) & SR_INT)
1208 {
1209 mpz_set(u->z,a->z);
1210 // (z/n) / b -> z/(n*b)
1211 if (a->s<2)
1212 {
1213 mpz_init_set(u->n,a->n);
1214 if (((long)b)>0L)
1215 mpz_mul_ui(u->n,u->n,SR_TO_INT(b));
1216 else
1217 {
1218 mpz_mul_ui(u->n,u->n,-SR_TO_INT(b));
1219 mpz_neg(u->z,u->z);
1220 }
1221 }
1222 else
1223 // long z / short b -> z/b
1224 {
1225 //mpz_set(u->z,a->z);
1227 }
1228 }
1229// ---------- long / long ------------------------------------
1230 else
1231 {
1232 mpz_set(u->z,a->z);
1233 mpz_init_set(u->n,b->z);
1234 if (a->s<2) mpz_mul(u->n,u->n,a->n);
1235 if (b->s<2) mpz_mul(u->z,u->z,b->n);
1236 }
1237 }
1238 if (mpz_isNeg(u->n))
1239 {
1240 mpz_neg(u->z,u->z);
1241 mpz_neg(u->n,u->n);
1242 }
1243 if (mpz_cmp_si(u->n,1L)==0)
1244 {
1245 mpz_clear(u->n);
1246 u->s=3;
1247 u=nlShort3(u);
1248 }
1249 nlTest(u, r);
1250 return u;
1251}
1252
1253/*2
1254* u:= x ^ exp
1255*/
1256void nlPower (number x,int exp,number * u, const coeffs r)
1257{
1258 *u = INT_TO_SR(0); // 0^e, e!=0
1259 if (exp==0)
1260 *u= INT_TO_SR(1);
1261 else if (!nlIsZero(x,r))
1262 {
1263 nlTest(x, r);
1264 number aa=NULL;
1265 if (SR_HDL(x) & SR_INT)
1266 {
1268 x=aa;
1269 }
1270 else if (x->s==0)
1271 nlNormalize(x,r);
1272 *u=ALLOC_RNUMBER();
1273#if defined(LDEBUG)
1274 (*u)->debug=123456;
1275#endif
1276 mpz_init((*u)->z);
1277 mpz_pow_ui((*u)->z,x->z,(unsigned long)exp);
1278 if (x->s<2)
1279 {
1280 if (mpz_cmp_si(x->n,1L)==0)
1281 {
1282 x->s=3;
1283 mpz_clear(x->n);
1284 }
1285 else
1286 {
1287 mpz_init((*u)->n);
1288 mpz_pow_ui((*u)->n,x->n,(unsigned long)exp);
1289 }
1290 }
1291 (*u)->s = x->s;
1292 if ((*u)->s==3) *u=nlShort3(*u);
1293 if (aa!=NULL)
1294 {
1295 mpz_clear(aa->z);
1297 }
1298 }
1299#ifdef LDEBUG
1300 if (exp<0) Print("nlPower: neg. exp. %d\n",exp);
1301 nlTest(*u, r);
1302#endif
1303}
1304
1305
1306/*2
1307* za >= 0 ?
1308*/
1310{
1311 nlTest(a, r);
1312 if (SR_HDL(a) & SR_INT) return SR_HDL(a)>1L /* represents number(0) */;
1313 return (!mpz_isNeg(a->z));
1314}
1315
1316/*2
1317* a > b ?
1318*/
1320{
1321 nlTest(a, r);
1322 nlTest(b, r);
1323 number re;
1324 BOOLEAN rr;
1325 re=nlSub(a,b,r);
1326 rr=(!nlIsZero(re,r)) && (nlGreaterZero(re,r));
1327 nlDelete(&re,r);
1328 return rr;
1329}
1330
1331/*2
1332* a == -1 ?
1333*/
1335{
1336#ifdef LDEBUG
1337 if (a==NULL) return FALSE;
1338 nlTest(a, r);
1339#endif
1340 return (a==INT_TO_SR(-1L));
1341}
1342
1343/*2
1344* result =gcd(a,b)
1345*/
1347{
1348 number result;
1349 nlTest(a, r);
1350 nlTest(b, r);
1351 //nlNormalize(a);
1352 //nlNormalize(b);
1353 if ((a==INT_TO_SR(1L))||(a==INT_TO_SR(-1L))
1354 || (b==INT_TO_SR(1L))||(b==INT_TO_SR(-1L)))
1355 return INT_TO_SR(1L);
1356 if (a==INT_TO_SR(0)) /* gcd(0,b) ->b */
1357 return nlCopy(b,r);
1358 if (b==INT_TO_SR(0)) /* gcd(a,0) -> a */
1359 return nlCopy(a,r);
1360 if (SR_HDL(a) & SR_HDL(b) & SR_INT)
1361 {
1362 long i=SR_TO_INT(a);
1363 long j=SR_TO_INT(b);
1364 long l;
1365 i=ABS(i);
1366 j=ABS(j);
1367 do
1368 {
1369 l=i%j;
1370 i=j;
1371 j=l;
1372 } while (l!=0L);
1373 if (i==POW_2_28)
1375 else
1377 nlTest(result,r);
1378 return result;
1379 }
1380 if (((!(SR_HDL(a) & SR_INT))&&(a->s<2))
1381 || ((!(SR_HDL(b) & SR_INT))&&(b->s<2))) return INT_TO_SR(1);
1382 if (SR_HDL(a) & SR_INT)
1383 {
1384 LONG aa=ABS(SR_TO_INT(a));
1385 unsigned long t=mpz_gcd_ui(NULL,b->z,(long)aa);
1386 if (t==POW_2_28)
1388 else
1389 result=INT_TO_SR(t);
1390 }
1391 else
1392 if (SR_HDL(b) & SR_INT)
1393 {
1394 LONG bb=ABS(SR_TO_INT(b));
1395 unsigned long t=mpz_gcd_ui(NULL,a->z,(long)bb);
1396 if (t==POW_2_28)
1398 else
1399 result=INT_TO_SR(t);
1400 }
1401 else
1402 {
1404 result->s = 3;
1405 #ifdef LDEBUG
1406 result->debug=123456;
1407 #endif
1408 mpz_init(result->z);
1409 mpz_gcd(result->z,a->z,b->z);
1411 }
1412 nlTest(result, r);
1413 return result;
1414}
1415
1416static int int_extgcd(int a, int b, int * u, int* x, int * v, int* y)
1417{
1418 int q, r;
1419 if (a==0)
1420 {
1421 *u = 0;
1422 *v = 1;
1423 *x = -1;
1424 *y = 0;
1425 return b;
1426 }
1427 if (b==0)
1428 {
1429 *u = 1;
1430 *v = 0;
1431 *x = 0;
1432 *y = 1;
1433 return a;
1434 }
1435 *u=1;
1436 *v=0;
1437 *x=0;
1438 *y=1;
1439 do
1440 {
1441 q = a/b;
1442 r = a%b;
1443 assume (q*b+r == a);
1444 a = b;
1445 b = r;
1446
1447 r = -(*v)*q+(*u);
1448 (*u) =(*v);
1449 (*v) = r;
1450
1451 r = -(*y)*q+(*x);
1452 (*x) = (*y);
1453 (*y) = r;
1454 } while (b);
1455
1456 return a;
1457}
1458
1459//number nlGcd_dummy(number a, number b, const coeffs r)
1460//{
1461// extern char my_yylinebuf[80];
1462// Print("nlGcd in >>%s<<\n",my_yylinebuf);
1463// return nlGcd(a,b,r);;
1464//}
1465
1466number nlShort1(number x) // assume x->s==0/1
1467{
1468 assume(x->s<2);
1469 if (mpz_sgn1(x->z)==0)
1470 {
1472 return INT_TO_SR(0);
1473 }
1474 if (x->s<2)
1475 {
1476 if (mpz_cmp(x->z,x->n)==0)
1477 {
1479 return INT_TO_SR(1);
1480 }
1481 }
1482 return x;
1483}
1484/*2
1485* simplify x
1486*/
1487void nlNormalize (number &x, const coeffs r)
1488{
1489 if ((SR_HDL(x) & SR_INT) ||(x==NULL))
1490 return;
1491 if (x->s==3)
1492 {
1494 nlTest(x,r);
1495 return;
1496 }
1497 else if (x->s==0)
1498 {
1499 if (mpz_cmp_si(x->n,1L)==0)
1500 {
1501 mpz_clear(x->n);
1502 x->s=3;
1503 x=nlShort3(x);
1504 }
1505 else
1506 {
1507 mpz_t gcd;
1508 mpz_init(gcd);
1509 mpz_gcd(gcd,x->z,x->n);
1510 x->s=1;
1511 if (mpz_cmp_si(gcd,1L)!=0)
1512 {
1513 mpz_divexact(x->z,x->z,gcd);
1514 mpz_divexact(x->n,x->n,gcd);
1515 if (mpz_cmp_si(x->n,1L)==0)
1516 {
1517 mpz_clear(x->n);
1518 x->s=3;
1520 }
1521 }
1522 mpz_clear(gcd);
1523 }
1524 }
1525 nlTest(x, r);
1526}
1527
1528/*2
1529* returns in result->z the lcm(a->z,b->n)
1530*/
1532{
1533 number result;
1534 nlTest(a, r);
1535 nlTest(b, r);
1536 if ((SR_HDL(b) & SR_INT)
1537 || (b->s==3))
1538 {
1539 // b is 1/(b->n) => b->n is 1 => result is a
1540 return nlCopy(a,r);
1541 }
1543#if defined(LDEBUG)
1544 result->debug=123456;
1545#endif
1546 result->s=3;
1547 mpz_t gcd;
1548 mpz_init(gcd);
1549 mpz_init(result->z);
1550 if (SR_HDL(a) & SR_INT)
1551 mpz_gcd_ui(gcd,b->n,ABS(SR_TO_INT(a)));
1552 else
1553 mpz_gcd(gcd,a->z,b->n);
1554 if (mpz_cmp_si(gcd,1L)!=0)
1555 {
1556 mpz_t bt;
1557 mpz_init(bt);
1558 mpz_divexact(bt,b->n,gcd);
1559 if (SR_HDL(a) & SR_INT)
1561 else
1562 mpz_mul(result->z,bt,a->z);
1563 mpz_clear(bt);
1564 }
1565 else
1566 if (SR_HDL(a) & SR_INT)
1567 mpz_mul_si(result->z,b->n,SR_TO_INT(a));
1568 else
1569 mpz_mul(result->z,b->n,a->z);
1570 mpz_clear(gcd);
1572 nlTest(result, r);
1573 return result;
1574}
1575
1576// Map q \in QQ or ZZ \to Zp or an extension of it
1577// src = Q or Z, dst = Zp (or an extension of Zp)
1578number nlModP(number q, const coeffs /*Q*/, const coeffs Zp)
1579{
1580 const int p = n_GetChar(Zp);
1581 assume( p > 0 );
1582
1583 const long P = p;
1584 assume( P > 0 );
1585
1586 // embedded long within q => only long numerator has to be converted
1587 // to int (modulo char.)
1588 if (SR_HDL(q) & SR_INT)
1589 {
1590 long i = SR_TO_INT(q);
1591 return n_Init( i, Zp );
1592 }
1593
1594 const unsigned long PP = p;
1595
1596 // numerator modulo char. should fit into int
1597 number z = n_Init( static_cast<long>(mpz_fdiv_ui(q->z, PP)), Zp );
1598
1599 // denominator != 1?
1600 if (q->s!=3)
1601 {
1602 // denominator modulo char. should fit into int
1603 number n = n_Init( static_cast<long>(mpz_fdiv_ui(q->n, PP)), Zp );
1604
1605 number res = n_Div( z, n, Zp );
1606
1607 n_Delete(&z, Zp);
1608 n_Delete(&n, Zp);
1609
1610 return res;
1611 }
1612
1613 return z;
1614}
1615
1616#ifdef HAVE_RINGS
1617/*2
1618* convert number i (from Q) to GMP and warn if denom != 1
1619*/
1620void nlGMP(number &i, mpz_t n, const coeffs r)
1621{
1622 // Hier brauche ich einfach die GMP Zahl
1623 nlTest(i, r);
1624 nlNormalize(i, r);
1625 if (SR_HDL(i) & SR_INT)
1626 {
1627 mpz_set_si(n, SR_TO_INT(i));
1628 return;
1629 }
1630 if (i->s!=3)
1631 {
1632 WarnS("Omitted denominator during coefficient mapping !");
1633 }
1634 mpz_set(n, i->z);
1635}
1636#endif
1637
1638/*2
1639* acces to denominator, other 1 for integers
1640*/
1642{
1643 if (!(SR_HDL(n) & SR_INT))
1644 {
1645 if (n->s==0)
1646 {
1647 nlNormalize(n,r);
1648 }
1649 if (!(SR_HDL(n) & SR_INT))
1650 {
1651 if (n->s!=3)
1652 {
1654 u->s=3;
1655#if defined(LDEBUG)
1656 u->debug=123456;
1657#endif
1658 mpz_init_set(u->z,n->n);
1659 u=nlShort3_noinline(u);
1660 return u;
1661 }
1662 }
1663 }
1664 return INT_TO_SR(1);
1665}
1666
1667/*2
1668* acces to Nominator, nlCopy(n) for integers
1669*/
1671{
1672 if (!(SR_HDL(n) & SR_INT))
1673 {
1674 if (n->s==0)
1675 {
1676 nlNormalize(n,r);
1677 }
1678 if (!(SR_HDL(n) & SR_INT))
1679 {
1681#if defined(LDEBUG)
1682 u->debug=123456;
1683#endif
1684 u->s=3;
1685 mpz_init_set(u->z,n->z);
1686 if (n->s!=3)
1687 {
1688 u=nlShort3_noinline(u);
1689 }
1690 return u;
1691 }
1692 }
1693 return n; // imm. int
1694}
1695
1696/***************************************************************
1697 *
1698 * routines which are needed by Inline(d) routines
1699 *
1700 *******************************************************************/
1702{
1703 assume(! (SR_HDL(a) & SR_HDL(b) & SR_INT));
1704// long - short
1705 BOOLEAN bo;
1706 if (SR_HDL(b) & SR_INT)
1707 {
1708 if (a->s!=0) return FALSE;
1709 number n=b; b=a; a=n;
1710 }
1711// short - long
1712 if (SR_HDL(a) & SR_INT)
1713 {
1714 if (b->s!=0)
1715 return FALSE;
1716 if ((((long)a) > 0L) && (mpz_isNeg(b->z)))
1717 return FALSE;
1718 if ((((long)a) < 0L) && (!mpz_isNeg(b->z)))
1719 return FALSE;
1720 mpz_t bb;
1721 mpz_init(bb);
1722 mpz_mul_si(bb,b->n,(long)SR_TO_INT(a));
1723 bo=(mpz_cmp(bb,b->z)==0);
1724 mpz_clear(bb);
1725 return bo;
1726 }
1727// long - long
1728 if (((a->s==1) && (b->s==3))
1729 || ((b->s==1) && (a->s==3)))
1730 return FALSE;
1731 if (mpz_isNeg(a->z)&&(!mpz_isNeg(b->z)))
1732 return FALSE;
1733 if (mpz_isNeg(b->z)&&(!mpz_isNeg(a->z)))
1734 return FALSE;
1735 mpz_t aa;
1736 mpz_t bb;
1737 mpz_init_set(aa,a->z);
1738 mpz_init_set(bb,b->z);
1739 if (a->s<2) mpz_mul(bb,bb,a->n);
1740 if (b->s<2) mpz_mul(aa,aa,b->n);
1741 bo=(mpz_cmp(aa,bb)==0);
1742 mpz_clear(aa);
1743 mpz_clear(bb);
1744 return bo;
1745}
1746
1747// copy not immediate number a
1749{
1750 assume(!(SR_HDL(a) & SR_INT));
1751 //nlTest(a, r);
1753#if defined(LDEBUG)
1754 b->debug=123456;
1755#endif
1756 switch (a->s)
1757 {
1758 case 0:
1759 case 1:
1760 mpz_init_set(b->n,a->n);
1761 /*no break*/
1762 case 3:
1763 mpz_init_set(b->z,a->z);
1764 break;
1765 }
1766 b->s = a->s;
1767 return b;
1768}
1769
1771{
1772 {
1773 switch ((*a)->s)
1774 {
1775 case 0:
1776 case 1:
1777 mpz_clear((*a)->n);
1778 /*no break*/
1779 case 3:
1780 mpz_clear((*a)->z);
1781 }
1782 #ifdef LDEBUG
1783 memset(*a,0,sizeof(**a));
1784 #endif
1785 FREE_RNUMBER(*a); // omFreeBin((void *) *a, rnumber_bin);
1786 }
1787}
1788
1790{
1791 mpz_neg(a->z,a->z);
1792 if (a->s==3)
1793 {
1794 a=nlShort3(a);
1795 }
1796 return a;
1797}
1798
1799// conditio to use nlNormalize_Gcd in intermediate computations:
1800#define GCD_NORM_COND(OLD,NEW) (mpz_size1(NEW->z)>mpz_size1(OLD->z))
1801
1803{
1804 mpz_t gcd;
1805 mpz_init(gcd);
1806 mpz_gcd(gcd,x->z,x->n);
1807 x->s=1;
1808 if (mpz_cmp_si(gcd,1L)!=0)
1809 {
1810 mpz_divexact(x->z,x->z,gcd);
1811 mpz_divexact(x->n,x->n,gcd);
1812 if (mpz_cmp_si(x->n,1L)==0)
1813 {
1814 mpz_clear(x->n);
1815 x->s=3;
1817 }
1818 }
1819 mpz_clear(gcd);
1820}
1821
1823{
1825#if defined(LDEBUG)
1826 u->debug=123456;
1827#endif
1828 mpz_init(u->z);
1829 if (SR_HDL(b) & SR_INT)
1830 {
1831 number x=a;
1832 a=b;
1833 b=x;
1834 }
1835 if (SR_HDL(a) & SR_INT)
1836 {
1837 switch (b->s)
1838 {
1839 case 0:
1840 case 1:/* a:short, b:1 */
1841 {
1842 mpz_t x;
1843 mpz_init(x);
1844 mpz_mul_si(x,b->n,SR_TO_INT(a));
1845 mpz_add(u->z,b->z,x);
1846 mpz_clear(x);
1847 if (mpz_sgn1(u->z)==0)
1848 {
1849 mpz_clear(u->z);
1850 FREE_RNUMBER(u);
1851 return INT_TO_SR(0);
1852 }
1853 if (mpz_cmp(u->z,b->n)==0)
1854 {
1855 mpz_clear(u->z);
1856 FREE_RNUMBER(u);
1857 return INT_TO_SR(1);
1858 }
1859 mpz_init_set(u->n,b->n);
1860 u->s = 0;
1861 if (GCD_NORM_COND(b,u)) { nlNormalize_Gcd(u); }
1862 break;
1863 }
1864 case 3:
1865 {
1866 if (((long)a)>0L)
1867 mpz_add_ui(u->z,b->z,SR_TO_INT(a));
1868 else
1869 mpz_sub_ui(u->z,b->z,-SR_TO_INT(a));
1870 u->s = 3;
1871 u=nlShort3(u);
1872 break;
1873 }
1874 }
1875 }
1876 else
1877 {
1878 switch (a->s)
1879 {
1880 case 0:
1881 case 1:
1882 {
1883 switch(b->s)
1884 {
1885 case 0:
1886 case 1:
1887 {
1888 mpz_t x;
1889 mpz_init(x);
1890
1891 mpz_mul(x,b->z,a->n);
1892 mpz_mul(u->z,a->z,b->n);
1893 mpz_add(u->z,u->z,x);
1894 mpz_clear(x);
1895
1896 if (mpz_sgn1(u->z)==0)
1897 {
1898 mpz_clear(u->z);
1899 FREE_RNUMBER(u);
1900 return INT_TO_SR(0);
1901 }
1902 mpz_init(u->n);
1903 mpz_mul(u->n,a->n,b->n);
1904 if (mpz_cmp(u->z,u->n)==0)
1905 {
1906 mpz_clear(u->z);
1907 mpz_clear(u->n);
1908 FREE_RNUMBER(u);
1909 return INT_TO_SR(1);
1910 }
1911 u->s = 0;
1912 if (GCD_NORM_COND(b,u)) { nlNormalize_Gcd(u); }
1913 break;
1914 }
1915 case 3: /* a:1 b:3 */
1916 {
1917 mpz_mul(u->z,b->z,a->n);
1918 mpz_add(u->z,u->z,a->z);
1919 if (mpz_sgn1(u->z)==0)
1920 {
1921 mpz_clear(u->z);
1922 FREE_RNUMBER(u);
1923 return INT_TO_SR(0);
1924 }
1925 if (mpz_cmp(u->z,a->n)==0)
1926 {
1927 mpz_clear(u->z);
1928 FREE_RNUMBER(u);
1929 return INT_TO_SR(1);
1930 }
1931 mpz_init_set(u->n,a->n);
1932 u->s = 0;
1933 if (GCD_NORM_COND(a,u)) { nlNormalize_Gcd(u); }
1934 break;
1935 }
1936 } /*switch (b->s) */
1937 break;
1938 }
1939 case 3:
1940 {
1941 switch(b->s)
1942 {
1943 case 0:
1944 case 1:/* a:3, b:1 */
1945 {
1946 mpz_mul(u->z,a->z,b->n);
1947 mpz_add(u->z,u->z,b->z);
1948 if (mpz_sgn1(u->z)==0)
1949 {
1950 mpz_clear(u->z);
1951 FREE_RNUMBER(u);
1952 return INT_TO_SR(0);
1953 }
1954 if (mpz_cmp(u->z,b->n)==0)
1955 {
1956 mpz_clear(u->z);
1957 FREE_RNUMBER(u);
1958 return INT_TO_SR(1);
1959 }
1960 mpz_init_set(u->n,b->n);
1961 u->s = 0;
1962 if (GCD_NORM_COND(b,u)) { nlNormalize_Gcd(u); }
1963 break;
1964 }
1965 case 3:
1966 {
1967 mpz_add(u->z,a->z,b->z);
1968 u->s = 3;
1969 u=nlShort3(u);
1970 break;
1971 }
1972 }
1973 break;
1974 }
1975 }
1976 }
1977 return u;
1978}
1979
1981{
1982 if (SR_HDL(b) & SR_INT)
1983 {
1984 switch (a->s)
1985 {
1986 case 0:
1987 case 1:/* b:short, a:1 */
1988 {
1989 mpz_t x;
1990 mpz_init(x);
1991 mpz_mul_si(x,a->n,SR_TO_INT(b));
1992 mpz_add(a->z,a->z,x);
1993 mpz_clear(x);
1994 nlNormalize_Gcd(a);
1995 break;
1996 }
1997 case 3:
1998 {
1999 if (((long)b)>0L)
2000 mpz_add_ui(a->z,a->z,SR_TO_INT(b));
2001 else
2002 mpz_sub_ui(a->z,a->z,-SR_TO_INT(b));
2003 a->s = 3;
2004 a=nlShort3_noinline(a);
2005 break;
2006 }
2007 }
2008 return;
2009 }
2010 else if (SR_HDL(a) & SR_INT)
2011 {
2013 #if defined(LDEBUG)
2014 u->debug=123456;
2015 #endif
2016 mpz_init(u->z);
2017 switch (b->s)
2018 {
2019 case 0:
2020 case 1:/* a:short, b:1 */
2021 {
2022 mpz_t x;
2023 mpz_init(x);
2024
2025 mpz_mul_si(x,b->n,SR_TO_INT(a));
2026 mpz_add(u->z,b->z,x);
2027 mpz_clear(x);
2028 // result cannot be 0, if coeffs are normalized
2029 mpz_init_set(u->n,b->n);
2030 u->s=0;
2031 if (GCD_NORM_COND(b,u)) { nlNormalize_Gcd(u); }
2032 else { u=nlShort1(u); }
2033 break;
2034 }
2035 case 3:
2036 {
2037 if (((long)a)>0L)
2038 mpz_add_ui(u->z,b->z,SR_TO_INT(a));
2039 else
2040 mpz_sub_ui(u->z,b->z,-SR_TO_INT(a));
2041 // result cannot be 0, if coeffs are normalized
2042 u->s = 3;
2043 u=nlShort3_noinline(u);
2044 break;
2045 }
2046 }
2047 a=u;
2048 }
2049 else
2050 {
2051 switch (a->s)
2052 {
2053 case 0:
2054 case 1:
2055 {
2056 switch(b->s)
2057 {
2058 case 0:
2059 case 1: /* a:1 b:1 */
2060 {
2061 mpz_t x;
2062 mpz_t y;
2063 mpz_init(x);
2064 mpz_init(y);
2065 mpz_mul(x,b->z,a->n);
2066 mpz_mul(y,a->z,b->n);
2067 mpz_add(a->z,x,y);
2068 mpz_clear(x);
2069 mpz_clear(y);
2070 mpz_mul(a->n,a->n,b->n);
2071 a->s=0;
2072 if (GCD_NORM_COND(b,a)) { nlNormalize_Gcd(a); }
2073 else { a=nlShort1(a);}
2074 break;
2075 }
2076 case 3: /* a:1 b:3 */
2077 {
2078 mpz_t x;
2079 mpz_init(x);
2080 mpz_mul(x,b->z,a->n);
2081 mpz_add(a->z,a->z,x);
2082 mpz_clear(x);
2083 a->s=0;
2084 if (GCD_NORM_COND(b,a)) { nlNormalize_Gcd(a); }
2085 else { a=nlShort1(a);}
2086 break;
2087 }
2088 } /*switch (b->s) */
2089 break;
2090 }
2091 case 3:
2092 {
2093 switch(b->s)
2094 {
2095 case 0:
2096 case 1:/* a:3, b:1 */
2097 {
2098 mpz_t x;
2099 mpz_init(x);
2100 mpz_mul(x,a->z,b->n);
2101 mpz_add(a->z,b->z,x);
2102 mpz_clear(x);
2103 mpz_init_set(a->n,b->n);
2104 a->s=0;
2105 if (GCD_NORM_COND(b,a)) { nlNormalize_Gcd(a); }
2106 else { a=nlShort1(a);}
2107 break;
2108 }
2109 case 3:
2110 {
2111 mpz_add(a->z,a->z,b->z);
2112 a->s = 3;
2113 a=nlShort3_noinline(a);
2114 break;
2115 }
2116 }
2117 break;
2118 }
2119 }
2120 }
2121}
2122
2124{
2126#if defined(LDEBUG)
2127 u->debug=123456;
2128#endif
2129 mpz_init(u->z);
2130 if (SR_HDL(a) & SR_INT)
2131 {
2132 switch (b->s)
2133 {
2134 case 0:
2135 case 1:/* a:short, b:1 */
2136 {
2137 mpz_t x;
2138 mpz_init(x);
2139 mpz_mul_si(x,b->n,SR_TO_INT(a));
2140 mpz_sub(u->z,x,b->z);
2141 mpz_clear(x);
2142 if (mpz_sgn1(u->z)==0)
2143 {
2144 mpz_clear(u->z);
2145 FREE_RNUMBER(u);
2146 return INT_TO_SR(0);
2147 }
2148 if (mpz_cmp(u->z,b->n)==0)
2149 {
2150 mpz_clear(u->z);
2151 FREE_RNUMBER(u);
2152 return INT_TO_SR(1);
2153 }
2154 mpz_init_set(u->n,b->n);
2155 u->s=0;
2156 if (GCD_NORM_COND(b,u)) { nlNormalize_Gcd(u); }
2157 break;
2158 }
2159 case 3:
2160 {
2161 if (((long)a)>0L)
2162 {
2163 mpz_sub_ui(u->z,b->z,SR_TO_INT(a));
2164 mpz_neg(u->z,u->z);
2165 }
2166 else
2167 {
2168 mpz_add_ui(u->z,b->z,-SR_TO_INT(a));
2169 mpz_neg(u->z,u->z);
2170 }
2171 u->s = 3;
2172 u=nlShort3(u);
2173 break;
2174 }
2175 }
2176 }
2177 else if (SR_HDL(b) & SR_INT)
2178 {
2179 switch (a->s)
2180 {
2181 case 0:
2182 case 1:/* b:short, a:1 */
2183 {
2184 mpz_t x;
2185 mpz_init(x);
2186 mpz_mul_si(x,a->n,SR_TO_INT(b));
2187 mpz_sub(u->z,a->z,x);
2188 mpz_clear(x);
2189 if (mpz_sgn1(u->z)==0)
2190 {
2191 mpz_clear(u->z);
2192 FREE_RNUMBER(u);
2193 return INT_TO_SR(0);
2194 }
2195 if (mpz_cmp(u->z,a->n)==0)
2196 {
2197 mpz_clear(u->z);
2198 FREE_RNUMBER(u);
2199 return INT_TO_SR(1);
2200 }
2201 mpz_init_set(u->n,a->n);
2202 u->s=0;
2203 if (GCD_NORM_COND(a,u)) { nlNormalize_Gcd(u); }
2204 break;
2205 }
2206 case 3:
2207 {
2208 if (((long)b)>0L)
2209 {
2210 mpz_sub_ui(u->z,a->z,SR_TO_INT(b));
2211 }
2212 else
2213 {
2214 mpz_add_ui(u->z,a->z,-SR_TO_INT(b));
2215 }
2216 u->s = 3;
2217 u=nlShort3(u);
2218 break;
2219 }
2220 }
2221 }
2222 else
2223 {
2224 switch (a->s)
2225 {
2226 case 0:
2227 case 1:
2228 {
2229 switch(b->s)
2230 {
2231 case 0:
2232 case 1:
2233 {
2234 mpz_t x;
2235 mpz_t y;
2236 mpz_init(x);
2237 mpz_init(y);
2238 mpz_mul(x,b->z,a->n);
2239 mpz_mul(y,a->z,b->n);
2240 mpz_sub(u->z,y,x);
2241 mpz_clear(x);
2242 mpz_clear(y);
2243 if (mpz_sgn1(u->z)==0)
2244 {
2245 mpz_clear(u->z);
2246 FREE_RNUMBER(u);
2247 return INT_TO_SR(0);
2248 }
2249 mpz_init(u->n);
2250 mpz_mul(u->n,a->n,b->n);
2251 if (mpz_cmp(u->z,u->n)==0)
2252 {
2253 mpz_clear(u->z);
2254 mpz_clear(u->n);
2255 FREE_RNUMBER(u);
2256 return INT_TO_SR(1);
2257 }
2258 u->s=0;
2259 if (GCD_NORM_COND(a,u)) { nlNormalize_Gcd(u); }
2260 break;
2261 }
2262 case 3: /* a:1, b:3 */
2263 {
2264 mpz_t x;
2265 mpz_init(x);
2266 mpz_mul(x,b->z,a->n);
2267 mpz_sub(u->z,a->z,x);
2268 mpz_clear(x);
2269 if (mpz_sgn1(u->z)==0)
2270 {
2271 mpz_clear(u->z);
2272 FREE_RNUMBER(u);
2273 return INT_TO_SR(0);
2274 }
2275 if (mpz_cmp(u->z,a->n)==0)
2276 {
2277 mpz_clear(u->z);
2278 FREE_RNUMBER(u);
2279 return INT_TO_SR(1);
2280 }
2281 mpz_init_set(u->n,a->n);
2282 u->s=0;
2283 if (GCD_NORM_COND(a,u)) { nlNormalize_Gcd(u); }
2284 break;
2285 }
2286 }
2287 break;
2288 }
2289 case 3:
2290 {
2291 switch(b->s)
2292 {
2293 case 0:
2294 case 1: /* a:3, b:1 */
2295 {
2296 mpz_t x;
2297 mpz_init(x);
2298 mpz_mul(x,a->z,b->n);
2299 mpz_sub(u->z,x,b->z);
2300 mpz_clear(x);
2301 if (mpz_sgn1(u->z)==0)
2302 {
2303 mpz_clear(u->z);
2304 FREE_RNUMBER(u);
2305 return INT_TO_SR(0);
2306 }
2307 if (mpz_cmp(u->z,b->n)==0)
2308 {
2309 mpz_clear(u->z);
2310 FREE_RNUMBER(u);
2311 return INT_TO_SR(1);
2312 }
2313 mpz_init_set(u->n,b->n);
2314 u->s=0;
2315 if (GCD_NORM_COND(b,u)) { nlNormalize_Gcd(u); }
2316 break;
2317 }
2318 case 3: /* a:3 , b:3 */
2319 {
2320 mpz_sub(u->z,a->z,b->z);
2321 u->s = 3;
2322 u=nlShort3(u);
2323 break;
2324 }
2325 }
2326 break;
2327 }
2328 }
2329 }
2330 return u;
2331}
2332
2333// a and b are intermediate, but a*b not
2335{
2337#if defined(LDEBUG)
2338 u->debug=123456;
2339#endif
2340 u->s=3;
2341 mpz_init_set_si(u->z,SR_TO_INT(a));
2342 mpz_mul_si(u->z,u->z,SR_TO_INT(b));
2343 return u;
2344}
2345
2346// a or b are not immediate
2348{
2349 assume(! (SR_HDL(a) & SR_HDL(b) & SR_INT));
2351#if defined(LDEBUG)
2352 u->debug=123456;
2353#endif
2354 mpz_init(u->z);
2355 if (SR_HDL(b) & SR_INT)
2356 {
2357 number x=a;
2358 a=b;
2359 b=x;
2360 }
2361 if (SR_HDL(a) & SR_INT)
2362 {
2363 u->s=b->s;
2364 if (u->s==1) u->s=0;
2365 if (((long)a)>0L)
2366 {
2367 mpz_mul_ui(u->z,b->z,(unsigned long)SR_TO_INT(a));
2368 }
2369 else
2370 {
2371 if (a==INT_TO_SR(-1))
2372 {
2373 mpz_set(u->z,b->z);
2374 mpz_neg(u->z,u->z);
2375 u->s=b->s;
2376 }
2377 else
2378 {
2379 mpz_mul_ui(u->z,b->z,(unsigned long)-SR_TO_INT(a));
2380 mpz_neg(u->z,u->z);
2381 }
2382 }
2383 if (u->s<2)
2384 {
2385 if (mpz_cmp(u->z,b->n)==0)
2386 {
2387 mpz_clear(u->z);
2388 FREE_RNUMBER(u);
2389 return INT_TO_SR(1);
2390 }
2391 mpz_init_set(u->n,b->n);
2392 if (GCD_NORM_COND(b,u)) { nlNormalize_Gcd(u); }
2393 }
2394 else //u->s==3
2395 {
2396 u=nlShort3(u);
2397 }
2398 }
2399 else
2400 {
2401 mpz_mul(u->z,a->z,b->z);
2402 u->s = 0;
2403 if(a->s==3)
2404 {
2405 if(b->s==3)
2406 {
2407 u->s = 3;
2408 }
2409 else
2410 {
2411 if (mpz_cmp(u->z,b->n)==0)
2412 {
2413 mpz_clear(u->z);
2414 FREE_RNUMBER(u);
2415 return INT_TO_SR(1);
2416 }
2417 mpz_init_set(u->n,b->n);
2418 if (GCD_NORM_COND(b,u)) { nlNormalize_Gcd(u); }
2419 }
2420 }
2421 else
2422 {
2423 if(b->s==3)
2424 {
2425 if (mpz_cmp(u->z,a->n)==0)
2426 {
2427 mpz_clear(u->z);
2428 FREE_RNUMBER(u);
2429 return INT_TO_SR(1);
2430 }
2431 mpz_init_set(u->n,a->n);
2432 if (GCD_NORM_COND(a,u)) { nlNormalize_Gcd(u); }
2433 }
2434 else
2435 {
2436 mpz_init(u->n);
2437 mpz_mul(u->n,a->n,b->n);
2438 if (mpz_cmp(u->z,u->n)==0)
2439 {
2440 mpz_clear(u->z);
2441 mpz_clear(u->n);
2442 FREE_RNUMBER(u);
2443 return INT_TO_SR(1);
2444 }
2445 if (GCD_NORM_COND(a,u)) { nlNormalize_Gcd(u); }
2446 }
2447 }
2448 }
2449 return u;
2450}
2451
2452/*2
2453* copy a to b for mapping
2454*/
2455number nlCopyMap(number a, const coeffs /*src*/, const coeffs /*dst*/)
2456{
2457 if ((SR_HDL(a) & SR_INT)||(a==NULL))
2458 {
2459 return a;
2460 }
2461 return _nlCopy_NoImm(a);
2462}
2463
2465{
2466 if ((SR_HDL(a) & SR_INT)||(a==NULL))
2467 {
2468 return a;
2469 }
2470 if (a->s==3) return _nlCopy_NoImm(a);
2471 number a0=a;
2472 BOOLEAN a1=FALSE;
2473 if (a->s==0) { a0=_nlCopy_NoImm(a); a1=TRUE; }
2475 number b2=nlGetDenom(a0,src);
2477 nlDelete(&b1,src);
2478 nlDelete(&b2,src);
2479 if (a1) _nlDelete_NoImm(&a0);
2480 return b;
2481}
2482
2484{
2485 if (src->rep==n_rep_gap_rat) /*Q, coeffs_BIGINT */
2486 {
2487 if ((src->is_field==dst->is_field) /* Q->Q, Z->Z*/
2488 || (src->is_field==FALSE)) /* Z->Q */
2489 return nlCopyMap;
2490 return nlMapQtoZ; /* Q->Z */
2491 }
2492 if ((src->rep==n_rep_int) && nCoeff_is_Zp(src))
2493 {
2494 return nlMapP;
2495 }
2496 if ((src->rep==n_rep_float) && nCoeff_is_R(src))
2497 {
2498 if (dst->is_field) /* R -> Q */
2499 return nlMapR;
2500 else
2501 return nlMapR_BI; /* R -> bigint */
2502 }
2503 if ((src->rep==n_rep_gmp_float) && nCoeff_is_long_R(src))
2504 {
2505 if (dst->is_field)
2506 return nlMapLongR; /* long R -> Q */
2507 else
2508 return nlMapLongR_BI;
2509 }
2510 if (nCoeff_is_long_C(src))
2511 {
2512 return nlMapC; /* C -> Q */
2513 }
2514#ifdef HAVE_RINGS
2515 if (src->rep==n_rep_gmp) // nCoeff_is_Z(src) || nCoeff_is_Ring_PtoM(src) || nCoeff_is_Zn(src))
2516 {
2517 return nlMapGMP;
2518 }
2519 if (src->rep==n_rep_gap_gmp)
2520 {
2521 return nlMapZ;
2522 }
2523 if ((src->rep==n_rep_int) && nCoeff_is_Ring_2toM(src))
2524 {
2525 return nlMapMachineInt;
2526 }
2527#endif
2528 return NULL;
2529}
2530/*2
2531* z := i
2532*/
2534{
2536#if defined(LDEBUG)
2537 z->debug=123456;
2538#endif
2539 mpz_init_set_si(z->z,i);
2540 z->s = 3;
2541 return z;
2542}
2543
2544/*2
2545* z := i/j
2546*/
2547number nlInit2 (int i, int j, const coeffs r)
2548{
2550#if defined(LDEBUG)
2551 z->debug=123456;
2552#endif
2553 mpz_init_set_si(z->z,(long)i);
2554 mpz_init_set_si(z->n,(long)j);
2555 z->s = 0;
2556 nlNormalize(z,r);
2557 return z;
2558}
2559
2561{
2563#if defined(LDEBUG)
2564 z->debug=123456;
2565#endif
2566 mpz_init_set(z->z,i);
2567 mpz_init_set(z->n,j);
2568 z->s = 0;
2569 nlNormalize(z,r);
2570 return z;
2571}
2572
2573#else // DO_LINLINE
2574
2575// declare immedate routines
2576number nlRInit (long i);
2585
2586#endif
2587
2588/***************************************************************
2589 *
2590 * Routines which might be inlined by p_Numbers.h
2591 *
2592 *******************************************************************/
2593#if defined(DO_LINLINE) || !defined(P_NUMBERS_H)
2594
2595// routines which are always inlined/static
2596
2597/*2
2598* a = b ?
2599*/
2601{
2602 nlTest(a, r);
2603 nlTest(b, r);
2604// short - short
2605 if (SR_HDL(a) & SR_HDL(b) & SR_INT) return a==b;
2606 return _nlEqual_aNoImm_OR_bNoImm(a, b);
2607}
2608
2610{
2611 number n;
2612 #if MAX_NUM_SIZE == 60
2613 if (((i << 3) >> 3) == i) n=INT_TO_SR(i);
2614 else n=nlRInit(i);
2615 #else
2616 LONG ii=(LONG)i;
2617 if ( ((((long)ii)==i) && ((ii << 3) >> 3) == ii )) n=INT_TO_SR(ii);
2618 else n=nlRInit(i);
2619 #endif
2620 nlTest(n, r);
2621 return n;
2622}
2623
2624/*2
2625* a == 1 ?
2626*/
2628{
2629#ifdef LDEBUG
2630 if (a==NULL) return FALSE;
2631 nlTest(a, r);
2632#endif
2633 return (a==INT_TO_SR(1));
2634}
2635
2637{
2638 #if 0
2639 if (a==INT_TO_SR(0)) return TRUE;
2640 if ((SR_HDL(a) & SR_INT)||(a==NULL)) return FALSE;
2641 if (mpz_cmp_si(a->z,0L)==0)
2642 {
2643 printf("gmp-0 in nlIsZero\n");
2644 dErrorBreak();
2645 return TRUE;
2646 }
2647 return FALSE;
2648 #else
2649 return (a==NULL)|| (a==INT_TO_SR(0));
2650 #endif
2651}
2652
2653/*2
2654* copy a to b
2655*/
2657{
2658 if (SR_HDL(a) & SR_INT)
2659 {
2660 return a;
2661 }
2662 return _nlCopy_NoImm(a);
2663}
2664
2665
2666/*2
2667* delete a
2668*/
2669LINLINE void nlDelete (number * a, const coeffs r)
2670{
2671 if (*a!=NULL)
2672 {
2673 nlTest(*a, r);
2674 if ((SR_HDL(*a) & SR_INT)==0)
2675 {
2676 _nlDelete_NoImm(a);
2677 }
2678 *a=NULL;
2679 }
2680}
2681
2682/*2
2683* za:= - za
2684*/
2686{
2687 nlTest(a, R);
2688 if(SR_HDL(a) &SR_INT)
2689 {
2690 LONG r=SR_TO_INT(a);
2691 if (r==(-(POW_2_28))) a=nlRInit(POW_2_28);
2692 else a=INT_TO_SR(-r);
2693 return a;
2694 }
2695 a = _nlNeg_NoImm(a);
2696 nlTest(a, R);
2697 return a;
2698
2699}
2700
2701/*2
2702* u:= a + b
2703*/
2705{
2706 if (SR_HDL(a) & SR_HDL(b) & SR_INT)
2707 {
2708 LONG r=SR_HDL(a)+SR_HDL(b)-1L;
2709 if ( ((r << 1) >> 1) == r )
2710 return (number)(long)r;
2711 else
2712 return nlRInit(SR_TO_INT(r));
2713 }
2715 nlTest(u, R);
2716 return u;
2717}
2718
2721
2723{
2724 // a=a+b
2725 if (SR_HDL(a) & SR_HDL(b) & SR_INT)
2726 {
2727 LONG r=SR_HDL(a)+SR_HDL(b)-1L;
2728 if ( ((r << 1) >> 1) == r )
2729 a=(number)(long)r;
2730 else
2731 a=nlRInit(SR_TO_INT(r));
2732 }
2733 else
2734 {
2736 nlTest(a,r);
2737 }
2738}
2739
2741{
2742 nlTest(a, R);
2743 nlTest(b, R);
2744 if (a==INT_TO_SR(0)) return INT_TO_SR(0);
2745 if (b==INT_TO_SR(0)) return INT_TO_SR(0);
2746 if (SR_HDL(a) & SR_HDL(b) & SR_INT)
2747 {
2748 LONG r=(LONG)((unsigned LONG)(SR_HDL(a)-1L))*((unsigned LONG)(SR_HDL(b)>>1));
2749 if ((r/(SR_HDL(b)>>1))==(SR_HDL(a)-1L))
2750 {
2751 number u=((number) ((r>>1)+SR_INT));
2752 if (((((LONG)SR_HDL(u))<<1)>>1)==SR_HDL(u)) return (u);
2753 return nlRInit(SR_HDL(u)>>2);
2754 }
2756 nlTest(u, R);
2757 return u;
2758
2759 }
2761 nlTest(u, R);
2762 return u;
2763
2764}
2765
2766
2767/*2
2768* u:= a - b
2769*/
2771{
2772 if (SR_HDL(a) & SR_HDL(b) & SR_INT)
2773 {
2774 LONG r=SR_HDL(a)-SR_HDL(b)+1;
2775 if ( ((r << 1) >> 1) == r )
2776 {
2777 return (number)(long)r;
2778 }
2779 else
2780 return nlRInit(SR_TO_INT(r));
2781 }
2783 nlTest(u, r);
2784 return u;
2785
2786}
2787
2789{
2790 number aa=a;
2791 if (((SR_HDL(b)|SR_HDL(aa))&SR_INT))
2792 {
2793 number n=nlMult(aa,b,r);
2794 nlDelete(&a,r);
2795 a=n;
2796 }
2797 else
2798 {
2799 mpz_mul(aa->z,a->z,b->z);
2800 if (aa->s==3)
2801 {
2802 if(b->s!=3)
2803 {
2804 mpz_init_set(a->n,b->n);
2805 a->s=0;
2806 }
2807 }
2808 else
2809 {
2810 if(b->s!=3)
2811 {
2812 mpz_mul(a->n,a->n,b->n);
2813 }
2814 a->s=0;
2815 }
2816 }
2817}
2818#endif // DO_LINLINE
2819
2820#ifndef P_NUMBERS_H
2821
2822void nlMPZ(mpz_t m, number &n, const coeffs r)
2823{
2824 nlTest(n, r);
2825 nlNormalize(n, r);
2826 if (SR_HDL(n) & SR_INT) mpz_init_set_si(m, SR_TO_INT(n)); /* n fits in an int */
2827 else mpz_init_set(m, (mpz_ptr)n->z);
2828}
2829
2830
2832{
2833 if (SR_HDL(a) & SR_HDL(b) & SR_INT)
2834 {
2835 int uu, vv, x, y;
2836 int g = int_extgcd(SR_TO_INT(a), SR_TO_INT(b), &uu, &vv, &x, &y);
2837 *s = INT_TO_SR(uu);
2838 *t = INT_TO_SR(vv);
2839 *u = INT_TO_SR(x);
2840 *v = INT_TO_SR(y);
2841 return INT_TO_SR(g);
2842 }
2843 else
2844 {
2845 mpz_t aa, bb;
2846 if (SR_HDL(a) & SR_INT)
2847 {
2849 }
2850 else
2851 {
2852 mpz_init_set(aa, a->z);
2853 }
2854 if (SR_HDL(b) & SR_INT)
2855 {
2857 }
2858 else
2859 {
2860 mpz_init_set(bb, b->z);
2861 }
2863 mpz_init(erg);
2864 mpz_init(bs);
2865 mpz_init(bt);
2866
2867 mpz_gcdext(erg, bs, bt, aa, bb);
2868
2869 mpz_div(aa, aa, erg);
2870 *u=nlInitMPZ(bb,r);
2871 *u=nlNeg(*u,r);
2872 *v=nlInitMPZ(aa,r);
2873
2874 mpz_clear(aa);
2875 mpz_clear(bb);
2876
2877 *s = nlInitMPZ(bs,r);
2878 *t = nlInitMPZ(bt,r);
2879 return nlInitMPZ(erg,r);
2880 }
2881}
2882
2884{
2885 assume(SR_TO_INT(b)!=0);
2886 if (SR_HDL(a) & SR_HDL(b) & SR_INT)
2887 {
2888 if (r!=NULL)
2889 *r = INT_TO_SR(SR_TO_INT(a) % SR_TO_INT(b));
2890 return INT_TO_SR(SR_TO_INT(a)/SR_TO_INT(b));
2891 }
2892 else if (SR_HDL(a) & SR_INT)
2893 {
2894 // -2^xx / 2^xx
2895 if ((a==INT_TO_SR(-(POW_2_28)))&&(b==INT_TO_SR(-1L)))
2896 {
2897 if (r!=NULL) *r=INT_TO_SR(0);
2898 return nlRInit(POW_2_28);
2899 }
2900 //a is small, b is not, so q=0, r=a
2901 if (r!=NULL)
2902 *r = a;
2903 return INT_TO_SR(0);
2904 }
2905 else if (SR_HDL(b) & SR_INT)
2906 {
2907 unsigned long rr;
2908 mpz_t qq;
2909 mpz_init(qq);
2910 mpz_t rrr;
2911 mpz_init(rrr);
2912 rr = mpz_divmod_ui(qq, rrr, a->z, (unsigned long)ABS(SR_TO_INT(b)));
2913 mpz_clear(rrr);
2914
2915 if (r!=NULL)
2916 *r = INT_TO_SR(rr);
2917 if (SR_TO_INT(b)<0)
2918 {
2919 mpz_neg(qq, qq);
2920 }
2921 return nlInitMPZ(qq,R);
2922 }
2923 mpz_t qq,rr;
2924 mpz_init(qq);
2925 mpz_init(rr);
2926 mpz_divmod(qq, rr, a->z, b->z);
2927 if (r!=NULL)
2928 *r = nlInitMPZ(rr,R);
2929 else
2930 {
2931 mpz_clear(rr);
2932 }
2933 return nlInitMPZ(qq,R);
2934}
2935
2936void nlInpGcd(number &a, number b, const coeffs r)
2937{
2938 if ((SR_HDL(b)|SR_HDL(a))&SR_INT)
2939 {
2940 number n=nlGcd(a,b,r);
2941 nlDelete(&a,r);
2942 a=n;
2943 }
2944 else
2945 {
2946 mpz_gcd(a->z,a->z,b->z);
2947 a=nlShort3_noinline(a);
2948 }
2949}
2950
2952{
2953 if ((SR_HDL(b)|SR_HDL(a))&SR_INT)
2954 {
2955 number n=nlIntDiv(a,b, r);
2956 nlDelete(&a,r);
2957 a=n;
2958 }
2959 else
2960 {
2961 mpz_t rr;
2962 mpz_init(rr);
2963 mpz_mod(rr,a->z,b->z);
2964 mpz_sub(a->z,a->z,rr);
2965 mpz_clear(rr);
2966 mpz_divexact(a->z,a->z,b->z);
2967 a=nlShort3_noinline(a);
2968 }
2969}
2970
2972{
2973 mpz_t A,B,C,D,E,N,P,tmp;
2975 else mpz_init_set(P,nP->z);
2976 const mp_bitcnt_t bits=2*(mpz_size1(P)+1)*GMP_LIMB_BITS;
2977 mpz_init2(N,bits);
2979 else mpz_set(N,nN->z);
2980 assume(!mpz_isNeg(P));
2981 if (mpz_isNeg(N)) mpz_add(N,N,P);
2982 mpz_init2(A,bits); mpz_set_ui(A,0L);
2983 mpz_init2(B,bits); mpz_set_ui(B,1L);
2984 mpz_init2(C,bits); mpz_set_ui(C,0L);
2985 mpz_init2(D,bits);
2986 mpz_init2(E,bits); mpz_set(E,P);
2988 number z=INT_TO_SR(0);
2989 while(mpz_sgn1(N)!=0)
2990 {
2991 mpz_mul(tmp,N,N);
2992 mpz_add(tmp,tmp,tmp);
2993 if (mpz_cmp(tmp,P)<0)
2994 {
2995 if (mpz_isNeg(B))
2996 {
2997 mpz_neg(B,B);
2998 mpz_neg(N,N);
2999 }
3000 // check for gcd(N,B)==1
3001 mpz_gcd(tmp,N,B);
3002 if (mpz_cmp_ui(tmp,1)==0)
3003 {
3004 // return N/B
3005 z=ALLOC_RNUMBER();
3006 #ifdef LDEBUG
3007 z->debug=123456;
3008 #endif
3009 memcpy(z->z,N,sizeof(mpz_t));
3010 memcpy(z->n,B,sizeof(mpz_t));
3011 z->s = 0;
3012 nlNormalize(z,r);
3013 }
3014 else
3015 {
3016 // return nN (the input) instead of "fail"
3017 z=nlCopy(nN,r);
3018 mpz_clear(B);
3019 mpz_clear(N);
3020 }
3021 break;
3022 }
3023 //mpz_mod(D,E,N);
3024 //mpz_div(tmp,E,N);
3025 mpz_divmod(tmp,D,E,N);
3026 mpz_mul(tmp,tmp,B);
3027 mpz_sub(C,A,tmp);
3028 mpz_set(E,N);
3029 mpz_set(N,D);
3030 mpz_set(A,B);
3031 mpz_set(B,C);
3032 }
3033 mpz_clear(tmp);
3034 mpz_clear(A);
3035 mpz_clear(C);
3036 mpz_clear(D);
3037 mpz_clear(E);
3038 mpz_clear(P);
3039 return z;
3040}
3041
3043{
3044 mpz_ptr aa,bb;
3045 *s=ALLOC_RNUMBER();
3046 mpz_init((*s)->z); (*s)->s=3;
3047 (*t)=ALLOC_RNUMBER();
3048 mpz_init((*t)->z); (*t)->s=3;
3050 mpz_init(g->z); g->s=3;
3051 #ifdef LDEBUG
3052 g->debug=123456;
3053 (*s)->debug=123456;
3054 (*t)->debug=123456;
3055 #endif
3056 if (SR_HDL(a) & SR_INT)
3057 {
3058 aa=(mpz_ptr)omAlloc(sizeof(mpz_t));
3060 }
3061 else
3062 {
3063 aa=a->z;
3064 }
3065 if (SR_HDL(b) & SR_INT)
3066 {
3067 bb=(mpz_ptr)omAlloc(sizeof(mpz_t));
3069 }
3070 else
3071 {
3072 bb=b->z;
3073 }
3074 mpz_gcdext(g->z,(*s)->z,(*t)->z,aa,bb);
3075 g=nlShort3(g);
3076 (*s)=nlShort3((*s));
3077 (*t)=nlShort3((*t));
3078 if (SR_HDL(a) & SR_INT)
3079 {
3080 mpz_clear(aa);
3081 omFreeSize(aa, sizeof(mpz_t));
3082 }
3083 if (SR_HDL(b) & SR_INT)
3084 {
3085 mpz_clear(bb);
3086 omFreeSize(bb, sizeof(mpz_t));
3087 }
3088 return g;
3089}
3090
3091//void nlCoeffWrite (const coeffs r, BOOLEAN /*details*/)
3092//{
3093// if (r->is_field) PrintS("QQ");
3094// else PrintS("ZZ");
3095//}
3096
3099// elemenst in the array are x[0..(rl-1)], q[0..(rl-1)]
3100{
3101 setCharacteristic( 0 ); // only in char 0
3103 CFArray X(rl), Q(rl);
3104 int i;
3105 for(i=rl-1;i>=0;i--)
3106 {
3107 X[i]=CF->convSingNFactoryN(x[i],FALSE,CF); // may be larger MAX_INT
3108 Q[i]=CF->convSingNFactoryN(q[i],FALSE,CF); // may be larger MAX_INT
3109 }
3111 if (n_SwitchChinRem)
3113 else
3115 number n=CF->convFactoryNSingN(xnew,CF);
3116 if (sym)
3117 {
3118 number p=CF->convFactoryNSingN(qnew,CF);
3119 number p2;
3120 if (getCoeffType(CF) == n_Q) p2=nlIntDiv(p,nlInit(2, CF),CF);
3121 else p2=CF->cfDiv(p,CF->cfInit(2, CF),CF);
3122 if (CF->cfGreater(n,p2,CF))
3123 {
3124 number n2=CF->cfSub(n,p,CF);
3125 CF->cfDelete(&n,CF);
3126 n=n2;
3127 }
3128 CF->cfDelete(&p2,CF);
3129 CF->cfDelete(&p,CF);
3130 }
3131 CF->cfNormalize(n,CF);
3132 return n;
3133}
3134#if 0
3135number nlChineseRemainder(number *x, number *q,int rl, const coeffs C)
3136{
3137 CFArray inv(rl);
3138 return nlChineseRemainderSym(x,q,rl,TRUE,inv,C);
3139}
3140#endif
3141
3143{
3144 assume(cf != NULL);
3145
3147
3148 if( !numberCollectionEnumerator.MoveNext() ) // empty zero polynomial?
3149 {
3150 c = nlInit(1, cf);
3151 return;
3152 }
3153
3154 // all coeffs are given by integers!!!
3155
3156 // part 1, find a small candidate for gcd
3158 int s1,s;
3159 s=2147483647; // max. int
3160
3162
3163 int normalcount = 0;
3164 do
3165 {
3166 number& n = numberCollectionEnumerator.Current();
3167 nlNormalize(n, cf); ++normalcount;
3168 cand1 = n;
3169
3170 if (SR_HDL(cand1)&SR_INT) { cand=cand1; break; }
3171 assume(cand1->s==3); // all coeffs should be integers // ==0?!! after printing
3172 s1=mpz_size1(cand1->z);
3173 if (s>s1)
3174 {
3175 cand=cand1;
3176 s=s1;
3177 }
3178 } while (numberCollectionEnumerator.MoveNext() );
3179
3180// assume( nlGreaterZero(cand,cf) ); // cand may be a negative integer!
3181
3182 cand=nlCopy(cand,cf);
3183 // part 2: compute gcd(cand,all coeffs)
3184
3186
3187 while (numberCollectionEnumerator.MoveNext() )
3188 {
3189 number& n = numberCollectionEnumerator.Current();
3190
3191 if( (--normalcount) <= 0)
3192 nlNormalize(n, cf);
3193
3194 nlInpGcd(cand, n, cf);
3196
3197 if(nlIsOne(cand,cf))
3198 {
3199 c = cand;
3200
3201 if(!lc_is_pos)
3202 {
3203 // make the leading coeff positive
3204 c = nlNeg(c, cf);
3206
3207 while (numberCollectionEnumerator.MoveNext() )
3208 {
3210 nn = nlNeg(nn, cf);
3211 }
3212 }
3213 return;
3214 }
3215 }
3216
3217 // part3: all coeffs = all coeffs / cand
3218 if (!lc_is_pos)
3219 cand = nlNeg(cand,cf);
3220
3221 c = cand;
3223
3224 while (numberCollectionEnumerator.MoveNext() )
3225 {
3226 number& n = numberCollectionEnumerator.Current();
3227 number t=nlExactDiv(n, cand, cf); // simple integer exact division, no ratios to remain
3228 nlDelete(&n, cf);
3229 n = t;
3230 }
3231}
3232
3234{
3235 assume(cf != NULL);
3236
3238
3239 if( !numberCollectionEnumerator.MoveNext() ) // empty zero polynomial?
3240 {
3241 c = nlInit(1, cf);
3242// assume( n_GreaterZero(c, cf) );
3243 return;
3244 }
3245
3246 // all coeffs are given by integers after returning from this routine
3247
3248 // part 1, collect product of all denominators /gcds
3249 number cand;
3251#if defined(LDEBUG)
3252 cand->debug=123456;
3253#endif
3254 cand->s=3;
3255
3256 int s=0;
3257
3259
3260 do
3261 {
3263
3264 if (!(SR_HDL(cand1)&SR_INT))
3265 {
3267 if ((!(SR_HDL(cand1)&SR_INT)) // not a short int
3268 && (cand1->s==1)) // and is a normalised rational
3269 {
3270 if (s==0) // first denom, we meet
3271 {
3272 mpz_init_set(cand->z, cand1->n); // cand->z = cand1->n
3273 s=1;
3274 }
3275 else // we have already something
3276 {
3277 mpz_lcm(cand->z, cand->z, cand1->n);
3278 }
3279 }
3280 }
3281 }
3282 while (numberCollectionEnumerator.MoveNext() );
3283
3284
3285 if (s==0) // nothing to do, all coeffs are already integers
3286 {
3287// mpz_clear(tmp);
3289 if (lc_is_pos)
3290 c=nlInit(1,cf);
3291 else
3292 {
3293 // make the leading coeff positive
3294 c=nlInit(-1,cf);
3295
3296 // TODO: incorporate the following into the loop below?
3298 while (numberCollectionEnumerator.MoveNext() )
3299 {
3300 number& n = numberCollectionEnumerator.Current();
3301 n = nlNeg(n, cf);
3302 }
3303 }
3304// assume( n_GreaterZero(c, cf) );
3305 return;
3306 }
3307
3308 cand = nlShort3(cand);
3309
3310 // part2: all coeffs = all coeffs * cand
3311 // make the lead coeff positive
3313
3314 if (!lc_is_pos)
3315 cand = nlNeg(cand, cf);
3316
3317 c = cand;
3318
3319 while (numberCollectionEnumerator.MoveNext() )
3320 {
3321 number &n = numberCollectionEnumerator.Current();
3322 nlInpMult(n, cand, cf);
3323 }
3324
3325}
3326
3327char * nlCoeffName(const coeffs r)
3328{
3329 if (r->cfDiv==nlDiv) return (char*)"QQ";
3330 else return (char*)"ZZ";
3331}
3332
3333void nlWriteFd(number n, const ssiInfo* d, const coeffs)
3334{
3335 if(SR_HDL(n) & SR_INT)
3336 {
3337 #if SIZEOF_LONG == 4
3338 fprintf(d->f_write,"4 %ld ",SR_TO_INT(n));
3339 #else
3340 long nn=SR_TO_INT(n);
3341 if ((nn<POW_2_28_32)&&(nn>= -POW_2_28_32))
3342 {
3343 int nnn=(int)nn;
3344 fprintf(d->f_write,"4 %d ",nnn);
3345 }
3346 else
3347 {
3348 mpz_t tmp;
3350 fputs("8 ",d->f_write);
3352 fputc(' ',d->f_write);
3353 mpz_clear(tmp);
3354 }
3355 #endif
3356 }
3357 else if (n->s<2)
3358 {
3359 //gmp_fprintf(f,"%d %Zd %Zd ",n->s,n->z,n->n);
3360 fprintf(d->f_write,"%d ",n->s+5);
3361 mpz_out_str (d->f_write,SSI_BASE, n->z);
3362 fputc(' ',d->f_write);
3363 mpz_out_str (d->f_write,SSI_BASE, n->n);
3364 fputc(' ',d->f_write);
3365
3366 //if (d->f_debug!=NULL) gmp_fprintf(d->f_debug,"number: s=%d gmp/gmp \"%Zd %Zd\" ",n->s,n->z,n->n);
3367 }
3368 else /*n->s==3*/
3369 {
3370 //gmp_fprintf(d->f_write,"3 %Zd ",n->z);
3371 fputs("8 ",d->f_write);
3372 mpz_out_str (d->f_write,SSI_BASE, n->z);
3373 fputc(' ',d->f_write);
3374
3375 //if (d->f_debug!=NULL) gmp_fprintf(d->f_debug,"number: gmp \"%Zd\" ",n->z);
3376 }
3377}
3378
3380{
3381 int sub_type=-1;
3383 switch(sub_type)
3384 {
3385 case 0:
3386 case 1:
3387 {// read mpz_t, mpz_t
3388 number n=nlRInit(0);
3389 mpz_init(n->n);
3390 s_readmpz(d->f_read,n->z);
3391 s_readmpz(d->f_read,n->n);
3392 n->s=sub_type;
3393 return n;
3394 }
3395
3396 case 3:
3397 {// read mpz_t
3398 number n=nlRInit(0);
3399 s_readmpz(d->f_read,n->z);
3400 n->s=3; /*sub_type*/
3401 #if SIZEOF_LONG == 8
3402 n=nlShort3(n);
3403 #endif
3404 return n;
3405 }
3406 case 4:
3407 {
3409 //#if SIZEOF_LONG == 8
3410 return INT_TO_SR(dd);
3411 //#else
3412 //return nlInit(dd,NULL);
3413 //#endif
3414 }
3415 case 5:
3416 case 6:
3417 {// read raw mpz_t, mpz_t
3418 number n=nlRInit(0);
3419 mpz_init(n->n);
3420 s_readmpz_base (d->f_read,n->z, SSI_BASE);
3421 s_readmpz_base (d->f_read,n->n, SSI_BASE);
3422 n->s=sub_type-5;
3423 return n;
3424 }
3425 case 8:
3426 {// read raw mpz_t
3427 number n=nlRInit(0);
3428 s_readmpz_base (d->f_read,n->z, SSI_BASE);
3429 n->s=sub_type=3; /*subtype-5*/
3430 #if SIZEOF_LONG == 8
3431 n=nlShort3(n);
3432 #endif
3433 return n;
3434 }
3435
3436 default: Werror("error in reading number: invalid subtype %d",sub_type);
3437 return NULL;
3438 }
3439 return NULL;
3440}
3441
3443{
3444 /* test, if r is an instance of nInitCoeffs(n,parameter) */
3445 /* if parameter is not needed */
3446 if (n==r->type)
3447 {
3448 if ((p==NULL)&&(r->cfDiv==nlDiv)) return TRUE;
3449 if ((p!=NULL)&&(r->cfDiv!=nlDiv)) return TRUE;
3450 }
3451 return FALSE;
3452}
3453
3455{
3456 number g=nlGcd(a,b,r);
3457 number n1=nlMult(a,b,r);
3458 number n2=nlExactDiv(n1,g,r);
3459 nlDelete(&g,r);
3460 nlDelete(&n1,r);
3461 return n2;
3462}
3463
3465{
3466 number a=nlInit(p(),cf);
3467 if (v2!=NULL)
3468 {
3469 number b=nlInit(p(),cf);
3470 number c=nlDiv(a,b,cf);
3471 nlDelete(&b,cf);
3472 nlDelete(&a,cf);
3473 a=c;
3474 }
3475 return a;
3476}
3477
3479{
3480 r->is_domain=TRUE;
3481 r->rep=n_rep_gap_rat;
3482
3483 r->nCoeffIsEqual=nlCoeffIsEqual;
3484 //r->cfKillChar = ndKillChar; /* dummy */
3485 //r->cfCoeffString=nlCoeffString;
3486 r->cfCoeffName=nlCoeffName;
3487
3488 r->cfInitMPZ = nlInitMPZ;
3489 r->cfMPZ = nlMPZ;
3490
3491 r->cfMult = nlMult;
3492 r->cfSub = nlSub;
3493 r->cfAdd = nlAdd;
3494 r->cfExactDiv= nlExactDiv;
3495 if (p==NULL) /* Q */
3496 {
3497 r->is_field=TRUE;
3498 r->cfDiv = nlDiv;
3499 //r->cfGcd = ndGcd_dummy;
3500 r->cfSubringGcd = nlGcd;
3501 }
3502 else /* Z: coeffs_BIGINT */
3503 {
3504 r->is_field=FALSE;
3505 r->cfDiv = nlIntDiv;
3506 r->cfIntMod= nlIntMod;
3507 r->cfGcd = nlGcd;
3508 r->cfDivBy=nlDivBy;
3509 r->cfDivComp = nlDivComp;
3510 r->cfIsUnit = nlIsUnit;
3511 r->cfGetUnit = nlGetUnit;
3512 r->cfQuot1 = nlQuot1;
3513 r->cfLcm = nlLcm;
3514 r->cfXExtGcd=nlXExtGcd;
3515 r->cfQuotRem=nlQuotRem;
3516 }
3517 r->cfInit = nlInit;
3518 r->cfSize = nlSize;
3519 r->cfInt = nlInt;
3520
3521 r->cfChineseRemainder=nlChineseRemainderSym;
3522 r->cfFarey=nlFarey;
3523 r->cfInpNeg = nlNeg;
3524 r->cfInvers= nlInvers;
3525 r->cfCopy = nlCopy;
3526 r->cfRePart = nlCopy;
3527 //r->cfImPart = ndReturn0;
3528 r->cfWriteLong = nlWrite;
3529 r->cfRead = nlRead;
3530 r->cfNormalize=nlNormalize;
3531 r->cfGreater = nlGreater;
3532 r->cfEqual = nlEqual;
3533 r->cfIsZero = nlIsZero;
3534 r->cfIsOne = nlIsOne;
3535 r->cfIsMOne = nlIsMOne;
3536 r->cfGreaterZero = nlGreaterZero;
3537 r->cfPower = nlPower;
3538 r->cfGetDenom = nlGetDenom;
3539 r->cfGetNumerator = nlGetNumerator;
3540 r->cfExtGcd = nlExtGcd; // only for ring stuff and Z
3541 r->cfNormalizeHelper = nlNormalizeHelper;
3542 r->cfDelete= nlDelete;
3543 r->cfSetMap = nlSetMap;
3544 //r->cfName = ndName;
3545 r->cfInpMult=nlInpMult;
3546 r->cfInpAdd=nlInpAdd;
3547 //r->cfCoeffWrite=nlCoeffWrite;
3548
3549 r->cfClearContent = nlClearContent;
3550 r->cfClearDenominators = nlClearDenominators;
3551
3552#ifdef LDEBUG
3553 // debug stuff
3554 r->cfDBTest=nlDBTest;
3555#endif
3556 r->convSingNFactoryN=nlConvSingNFactoryN;
3557 r->convFactoryNSingN=nlConvFactoryNSingN;
3558
3559 r->cfRandom=nlRandom;
3560
3561 // io via ssi
3562 r->cfWriteFd=nlWriteFd;
3563 r->cfReadFd=nlReadFd;
3564
3565 //r->type = n_Q;
3566 r->ch = 0;
3567 r->has_simple_Alloc=FALSE;
3568 r->has_simple_Inverse=FALSE;
3569
3570 // variables for this type of coeffs:
3571 // (none)
3572 return FALSE;
3573}
3574#if 0
3576{
3577 if (((SR_HDL(b)&SR_HDL(a))&SR_INT)
3578 {
3579 int bi=SR_TO_INT(b);
3580 int ai=SR_TO_INT(a);
3581 int bb=ABS(bi);
3582 int c=ai%bb;
3583 if (c<0) c+=bb;
3584 return (INT_TO_SR(c));
3585 }
3586 number al;
3587 number bl;
3588 if (SR_HDL(a))&SR_INT)
3589 al=nlRInit(SR_TO_INT(a));
3590 else
3591 al=nlCopy(a);
3592 if (SR_HDL(b))&SR_INT)
3594 else
3595 bl=nlCopy(b);
3596 number r=nlRInit(0);
3597 mpz_mod(r->z,al->z,bl->z);
3598 nlDelete(&al);
3599 nlDelete(&bl);
3600 if (mpz_size1(&r->z)<=MP_SMALL)
3601 {
3602 LONG ui=(int)mpz_get_si(&r->z);
3603 if ((((ui<<3)>>3)==ui)
3604 && (mpz_cmp_si(x->z,(long)ui)==0))
3605 {
3606 mpz_clear(&r->z);
3607 FREE_RNUMBER(r); // omFreeBin((void *)r, rnumber_bin);
3608 r=INT_TO_SR(ui);
3609 }
3610 }
3611 return r;
3612}
3613#endif
3614#endif // not P_NUMBERS_H
3615#endif // LONGRAT_CC
All the auxiliary stuff.
#define SSI_BASE
Definition auxiliary.h:135
static int ABS(int v)
Definition auxiliary.h:112
int BOOLEAN
Definition auxiliary.h:87
#define TRUE
Definition auxiliary.h:100
#define FALSE
Definition auxiliary.h:96
void On(int sw)
switches
void Off(int sw)
switches
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition cf_ops.cc:600
void FACTORY_PUBLIC setCharacteristic(int c)
Definition cf_char.cc:28
CanonicalForm num(const CanonicalForm &f)
CanonicalForm den(const CanonicalForm &f)
const CanonicalForm CFMap CFMap & N
Definition cfEzgcd.cc:56
int l
Definition cfEzgcd.cc:100
int m
Definition cfEzgcd.cc:128
int i
Definition cfEzgcd.cc:132
const CanonicalForm const CanonicalForm const CanonicalForm const CanonicalForm & cand
Definition cfModGcd.cc:70
Variable x
Definition cfModGcd.cc:4090
int p
Definition cfModGcd.cc:4086
g
Definition cfModGcd.cc:4098
CanonicalForm cf
Definition cfModGcd.cc:4091
CanonicalForm b
Definition cfModGcd.cc:4111
void FACTORY_PUBLIC chineseRemainder(const CanonicalForm &x1, const CanonicalForm &q1, const CanonicalForm &x2, const CanonicalForm &q2, CanonicalForm &xnew, CanonicalForm &qnew)
void chineseRemainder ( const CanonicalForm & x1, const CanonicalForm & q1, const CanonicalForm & x2,...
Definition cf_chinese.cc:57
void FACTORY_PUBLIC chineseRemainderCached(const CanonicalForm &x1, const CanonicalForm &q1, const CanonicalForm &x2, const CanonicalForm &q2, CanonicalForm &xnew, CanonicalForm &qnew, CFArray &inv)
static const int SW_RATIONAL
set to 1 for computations over Q
Definition cf_defs.h:31
FILE * f
Definition checklibs.c:9
factory's main class
gmp_complex numbers based on
Coefficient rings, fields and other domains suitable for Singular polynomials.
static FORCE_INLINE BOOLEAN nCoeff_is_long_R(const coeffs r)
Definition coeffs.h:895
n_coeffType
Definition coeffs.h:27
@ n_R
single prescision (6,6) real numbers
Definition coeffs.h:31
@ n_Q
rational (GMP) numbers
Definition coeffs.h:30
@ n_Zn
only used if HAVE_RINGS is defined
Definition coeffs.h:44
@ n_long_R
real floating point (GMP) numbers
Definition coeffs.h:33
@ n_Zp
\F{p < 2^31}
Definition coeffs.h:29
@ n_long_C
complex floating point (GMP) numbers
Definition coeffs.h:41
static FORCE_INLINE number n_Div(number a, number b, const coeffs r)
return the quotient of 'a' and 'b', i.e., a/b; raises an error if 'b' is not invertible in r exceptio...
Definition coeffs.h:619
coeffs nInitChar(n_coeffType t, void *parameter)
one-time initialisations for new coeffs in case of an error return NULL
Definition numbers.cc:419
#define ALLOC_RNUMBER()
Definition coeffs.h:94
static FORCE_INLINE n_coeffType getCoeffType(const coeffs r)
Returns the type of coeffs domain.
Definition coeffs.h:429
static FORCE_INLINE int n_GetChar(const coeffs r)
Return the characteristic of the coeff. domain.
Definition coeffs.h:448
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete 'p'
Definition coeffs.h:459
static FORCE_INLINE BOOLEAN nCoeff_is_Zp(const coeffs r)
Definition coeffs.h:804
static FORCE_INLINE number n_Init(long i, const coeffs r)
a number representing i in the given coeff field/ring r
Definition coeffs.h:542
static FORCE_INLINE BOOLEAN nCoeff_is_Ring_2toM(const coeffs r)
Definition coeffs.h:728
#define FREE_RNUMBER(x)
Definition coeffs.h:93
@ n_rep_gap_rat
(number), see longrat.h
Definition coeffs.h:118
@ n_rep_gap_gmp
(), see rinteger.h, new impl.
Definition coeffs.h:119
@ n_rep_float
(float), see shortfl.h
Definition coeffs.h:123
@ n_rep_int
(int), see modulop.h
Definition coeffs.h:117
@ n_rep_gmp_float
(gmp_float), see
Definition coeffs.h:124
@ n_rep_gmp
(mpz_ptr), see rmodulon,h
Definition coeffs.h:122
#define ALLOC0_RNUMBER()
Definition coeffs.h:95
number(* nMapFunc)(number a, const coeffs src, const coeffs dst)
maps "a", which lives in src, into dst
Definition coeffs.h:80
static FORCE_INLINE BOOLEAN nCoeff_is_R(const coeffs r)
Definition coeffs.h:840
static FORCE_INLINE BOOLEAN nCoeff_is_long_C(const coeffs r)
Definition coeffs.h:898
#define Print
Definition emacs.cc:80
#define WarnS
Definition emacs.cc:78
return result
const CanonicalForm int s
Definition facAbsFact.cc:51
const CanonicalForm int const CFList const Variable & y
Definition facAbsFact.cc:53
CanonicalForm res
Definition facAbsFact.cc:60
REvaluation E(1, terms.length(), IntRandom(25))
b *CanonicalForm B
Definition facBivar.cc:52
const Variable & v
< [in] a sqrfree bivariate poly
Definition facBivar.h:39
int j
Definition facHensel.cc:110
bool isZero(const CFArray &A)
checks if entries of A are zero
‘factory.h’ is the user interface to Factory.
CanonicalForm FACTORY_PUBLIC make_cf(const mpz_ptr n)
Definition singext.cc:66
void FACTORY_PUBLIC gmp_numerator(const CanonicalForm &f, mpz_ptr result)
Definition singext.cc:20
void FACTORY_PUBLIC gmp_denominator(const CanonicalForm &f, mpz_ptr result)
Definition singext.cc:40
void WerrorS(const char *s)
Definition feFopen.cc:24
#define D(A)
Definition gentable.cc:131
#define VAR
Definition globaldefs.h:5
#define info
Definition libparse.cc:1256
static number nlMapP(number from, const coeffs src, const coeffs dst)
Definition longrat.cc:189
#define nlTest(a, r)
Definition longrat.cc:87
void nlWriteFd(number n, const ssiInfo *d, const coeffs)
Definition longrat.cc:3333
LINLINE void nlInpMult(number &a, number b, const coeffs r)
Definition longrat.cc:2788
LINLINE BOOLEAN nlEqual(number a, number b, const coeffs r)
Definition longrat.cc:2600
LINLINE number nlAdd(number la, number li, const coeffs r)
Definition longrat.cc:2704
number nlMapZ(number from, const coeffs, const coeffs dst)
Definition longrat.cc:211
long nlInt(number &n, const coeffs r)
Definition longrat.cc:744
static number nlLcm(number a, number b, const coeffs r)
Definition longrat.cc:3454
static number nlMapLongR_BI(number from, const coeffs src, const coeffs dst)
Definition longrat.cc:515
number nlInit2(int i, int j, const coeffs r)
create a rational i/j (implicitly) over Q NOTE: make sure to use correct Q in debug mode
Definition longrat.cc:2547
#define POW_2_28
Definition longrat.cc:103
LINLINE number nl_Copy(number a, const coeffs r)
number nlInit2gmp(mpz_t i, mpz_t j, const coeffs r)
create a rational i/j (implicitly) over Q NOTE: make sure to use correct Q in debug mode
Definition longrat.cc:2560
void _nlInpAdd_aNoImm_OR_bNoImm(number &a, number b)
Definition longrat.cc:1980
LINLINE number nlSub(number la, number li, const coeffs r)
Definition longrat.cc:2770
number nlIntMod(number a, number b, const coeffs r)
Definition longrat.cc:1020
number _nlCopy_NoImm(number a)
Definition longrat.cc:1748
number _nlSub_aNoImm_OR_bNoImm(number a, number b)
Definition longrat.cc:2123
LINLINE number nlCopy(number a, const coeffs r)
Definition longrat.cc:2656
LINLINE number nlNeg(number za, const coeffs r)
Definition longrat.cc:2685
number nlXExtGcd(number a, number b, number *s, number *t, number *u, number *v, const coeffs r)
Definition longrat.cc:2831
void nlPower(number x, int exp, number *lu, const coeffs r)
Definition longrat.cc:1256
number nlQuotRem(number a, number b, number *r, const coeffs R)
Definition longrat.cc:2883
number nlFarey(number nN, number nP, const coeffs CF)
Definition longrat.cc:2971
LINLINE BOOLEAN nlIsOne(number a, const coeffs r)
Definition longrat.cc:2627
#define mpz_isNeg(A)
Definition longrat.cc:146
static number nlMapC(number from, const coeffs src, const coeffs dst)
Definition longrat.cc:548
number nlNormalizeHelper(number a, number b, const coeffs r)
Definition longrat.cc:1531
LINLINE void nlDelete(number *a, const coeffs r)
Definition longrat.cc:2669
BOOLEAN nlGreaterZero(number za, const coeffs r)
Definition longrat.cc:1309
number _nlNeg_NoImm(number a)
Definition longrat.cc:1789
number nlModP(number q, const coeffs, const coeffs Zp)
Definition longrat.cc:1578
LINLINE void nlInpAdd(number &a, number b, const coeffs r)
Definition longrat.cc:2722
number nlExactDiv(number a, number b, const coeffs r)
Definition longrat.cc:874
void mpz_mul_si(mpz_ptr r, mpz_srcptr s, long int si)
Definition longrat.cc:177
VAR int n_SwitchChinRem
Definition longrat.cc:3097
const char * nlRead(const char *s, number *a, const coeffs r)
Definition longrat0.cc:31
void nlMPZ(mpz_t m, number &n, const coeffs r)
Definition longrat.cc:2822
number nlInvers(number a, const coeffs r)
Definition longrat.cc:794
BOOLEAN nlIsUnit(number a, const coeffs)
Definition longrat.cc:1137
void nlInpIntDiv(number &a, number b, const coeffs r)
Definition longrat.cc:2951
static void nlNormalize_Gcd(number &x)
Definition longrat.cc:1802
static number nlConvFactoryNSingN(const CanonicalForm f, const coeffs r)
Definition longrat.cc:368
number nlChineseRemainderSym(number *x, number *q, int rl, BOOLEAN sym, CFArray &inv_cache, const coeffs CF)
Definition longrat.cc:3098
int nlDivComp(number a, number b, const coeffs r)
Definition longrat.cc:1095
void _nlDelete_NoImm(number *a)
Definition longrat.cc:1770
#define LINLINE
Definition longrat.cc:31
char * nlCoeffName(const coeffs r)
Definition longrat.cc:3327
#define POW_2_28_32
Definition longrat.cc:104
BOOLEAN nlInitChar(coeffs r, void *p)
Definition longrat.cc:3478
number nlCopyMap(number a, const coeffs, const coeffs)
Definition longrat.cc:2455
number nlExtGcd(number a, number b, number *s, number *t, const coeffs)
Definition longrat.cc:3042
static number nlMapGMP(number from, const coeffs, const coeffs dst)
Definition longrat.cc:206
LINLINE number nlMult(number a, number b, const coeffs r)
Definition longrat.cc:2740
static number nlInitMPZ(mpz_t m, const coeffs)
Definition longrat.cc:164
number nlIntDiv(number a, number b, const coeffs r)
Definition longrat.cc:939
static void nlClearDenominators(ICoeffsEnumerator &numberCollectionEnumerator, number &c, const coeffs cf)
Definition longrat.cc:3233
static number nlMapLongR(number from, const coeffs src, const coeffs dst)
Definition longrat.cc:435
LINLINE BOOLEAN nlIsZero(number za, const coeffs r)
Definition longrat.cc:2636
number nlGetDenom(number &n, const coeffs r)
Definition longrat.cc:1641
number nlGcd(number a, number b, const coeffs r)
Definition longrat.cc:1346
number _nlMult_aImm_bImm_rNoImm(number a, number b)
Definition longrat.cc:2334
number nlReadFd(const ssiInfo *d, const coeffs)
Definition longrat.cc:3379
int nlSize(number a, const coeffs)
Definition longrat.cc:715
number nlMapMachineInt(number from, const coeffs, const coeffs)
Definition longrat.cc:223
nMapFunc nlSetMap(const coeffs src, const coeffs dst)
Definition longrat.cc:2483
number nlBigInt(number &n)
static number nlShort3(number x)
Definition longrat.cc:109
#define GCD_NORM_COND(OLD, NEW)
Definition longrat.cc:1800
BOOLEAN nlDBTest(number a, const char *f, const int l)
number nlDiv(number a, number b, const coeffs r)
Definition longrat.cc:1146
number nlRInit(long i)
Definition longrat.cc:2533
BOOLEAN nlIsMOne(number a, const coeffs r)
Definition longrat.cc:1334
static void nlClearContent(ICoeffsEnumerator &numberCollectionEnumerator, number &c, const coeffs cf)
Definition longrat.cc:3142
number _nlMult_aNoImm_OR_bNoImm(number a, number b)
Definition longrat.cc:2347
LINLINE number nlInit(long i, const coeffs r)
Definition longrat.cc:2609
number nlShort3_noinline(number x)
Definition longrat.cc:159
number nlGetNumerator(number &n, const coeffs r)
Definition longrat.cc:1670
number _nlAdd_aNoImm_OR_bNoImm(number a, number b)
Definition longrat.cc:1822
#define LONG
Definition longrat.cc:105
BOOLEAN nlCoeffIsEqual(const coeffs r, n_coeffType n, void *p)
Definition longrat.cc:3442
static CanonicalForm nlConvSingNFactoryN(number n, const BOOLEAN setChar, const coeffs)
Definition longrat.cc:330
static number nlMapR(number from, const coeffs src, const coeffs dst)
Definition longrat.cc:395
number nlGetUnit(number n, const coeffs cf)
Definition longrat.cc:1106
coeffs nlQuot1(number c, const coeffs r)
Definition longrat.cc:1112
BOOLEAN _nlEqual_aNoImm_OR_bNoImm(number a, number b)
Definition longrat.cc:1701
number nlShort1(number x)
Definition longrat.cc:1466
#define MP_SMALL
Definition longrat.cc:144
BOOLEAN nlGreater(number a, number b, const coeffs r)
Definition longrat.cc:1319
static number nlMapR_BI(number from, const coeffs src, const coeffs dst)
Definition longrat.cc:425
void nlGMP(number &i, mpz_t n, const coeffs r)
Definition longrat.cc:1620
void nlNormalize(number &x, const coeffs r)
Definition longrat.cc:1487
BOOLEAN nlDivBy(number a, number b, const coeffs)
Definition longrat.cc:1081
static int int_extgcd(int a, int b, int *u, int *x, int *v, int *y)
Definition longrat.cc:1416
void nlWrite(number a, const coeffs r)
Definition longrat0.cc:90
void nlInpGcd(number &a, number b, const coeffs r)
Definition longrat.cc:2936
static number nlRandom(siRandProc p, number v2, number, const coeffs cf)
Definition longrat.cc:3464
number nlMapQtoZ(number a, const coeffs src, const coeffs dst)
Definition longrat.cc:2464
#define SR_INT
Definition longrat.h:67
#define INT_TO_SR(INT)
Definition longrat.h:68
#define SR_TO_INT(SR)
Definition longrat.h:69
void dErrorBreak(void)
Definition dError.cc:140
#define assume(x)
Definition mod2.h:387
long npInt(number &n, const coeffs r)
Definition modulop.cc:83
char * floatToStr(const gmp_float &r, const unsigned int oprec)
gmp_float exp(const gmp_float &a)
The main handler for Singular numbers which are suitable for Singular polynomials.
char * nEatLong(char *s, mpz_ptr i)
extracts a long integer from s, returns the rest
Definition numbers.cc:724
const char *const nDivBy0
Definition numbers.h:89
#define omFreeSize(addr, size)
#define omAlloc(size)
#define omCheckIf(cond, test)
#define omCheckAddrSize(addr, size)
#define omFree(addr)
#define NULL
Definition omList.c:12
int IsPrime(int p)
Definition prime.cc:61
void Werror(const char *fmt,...)
Definition reporter.cc:189
void s_readmpz(s_buff F, mpz_t a)
Definition s_buff.cc:184
void s_readmpz_base(s_buff F, mpz_ptr a, int base)
Definition s_buff.cc:209
int s_readint(s_buff F)
Definition s_buff.cc:112
long s_readlong(s_buff F)
Definition s_buff.cc:140
s_buff f_read
Definition s_buff.h:22
FILE * f_write
Definition s_buff.h:23
SI_FLOAT nrFloat(number n)
Converts a n_R number into a float. Needed by Maps.
Definition shortfl.cc:48
#define mpz_size1(A)
Definition si_gmp.h:17
#define mpz_sgn1(A)
Definition si_gmp.h:18
#define R
Definition sirandom.c:27
#define A
Definition sirandom.c:24
#define Q
Definition sirandom.c:26
int(* siRandProc)(void)
Definition sirandom.h:9
#define SR_HDL(A)
Definition tgb.cc:35
int gcd(int a, int b)