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