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