1 /****************************************
2 *  Computer Algebra System SINGULAR     *
3 ****************************************/
4 /*
5 * ABSTRACT: numbers (integers)
6 */
7 
8 #include "misc/auxiliary.h"
9 
10 #include "factory/factory.h"
11 
12 #include "misc/mylimits.h"
13 #include "reporter/reporter.h"
14 
15 #include "coeffs/coeffs.h"
16 #include "coeffs/longrat.h"
17 #include "coeffs/numbers.h"
18 
19 #include "coeffs/si_gmp.h"
20 
21 #include "coeffs/generics.h"
22 
23 #include "coeffs/rintegers.h"
24 
25 #include <string.h>
26 
27 struct generic_pair
28 {
29   number a1;
30   number a2;
31 };
32 typedef struct generic_pair *gcp;
33 VAR coeffs coeffs1,coeffs2;
34 
gCoeffWrite(const coeffs r,BOOLEAN b)35 static void gCoeffWrite(const coeffs r, BOOLEAN b)
36 {
37   printf("debug: ");
38   coeffs1->cfCoeffWrite(coeffs1,b);
39 }
40 
gCoeffName(const coeffs r)41 char* gCoeffName(const coeffs r)
42 {
43   return coeffs1->cfCoeffName(coeffs1);
44 }
gKillChar(coeffs r)45 void gKillChar(coeffs r)
46 {
47   coeffs1->cfKillChar(coeffs1);
48   coeffs2->cfKillChar(coeffs2);
49 }
gSetChar(coeffs r)50 void gSetChar(coeffs r)
51 {
52   coeffs1->cfSetChar(coeffs1);
53   coeffs2->cfSetChar(coeffs2);
54 }
55 
gMult(number a,number b,const coeffs cf)56 static number gMult (number a, number b, const coeffs cf)
57 {
58   gcp aa=(gcp)a;
59   gcp bb=(gcp)b;
60   gcp cc=(gcp)omalloc(sizeof(*cc));
61   cc->a1=coeffs1->cfMult(aa->a1,bb->a1,coeffs1);
62   cc->a2=coeffs2->cfMult(aa->a2,bb->a2,coeffs2);
63   n_Test((number)cc,cf);
64   return (number)cc;
65 }
gSub(number a,number b,const coeffs cf)66 static number gSub (number a, number b, const coeffs cf)
67 {
68   gcp aa=(gcp)a;
69   gcp bb=(gcp)b;
70   gcp cc=(gcp)omalloc(sizeof(*cc));
71   cc->a1=coeffs1->cfSub(aa->a1,bb->a1,coeffs1);
72   cc->a2=coeffs2->cfSub(aa->a2,bb->a2,coeffs2);
73   n_Test((number)cc,cf);
74   return (number)cc;
75 }
gAdd(number a,number b,const coeffs cf)76 static number gAdd (number a, number b, const coeffs cf)
77 {
78   gcp aa=(gcp)a;
79   gcp bb=(gcp)b;
80   gcp cc=(gcp)omalloc(sizeof(*cc));
81   cc->a1=coeffs1->cfAdd(aa->a1,bb->a1,coeffs1);
82   cc->a2=coeffs2->cfAdd(aa->a2,bb->a2,coeffs2);
83   n_Test((number)cc,cf);
84   return (number)cc;
85 }
gDiv(number a,number b,const coeffs cf)86 static number gDiv (number a, number b, const coeffs cf)
87 {
88   gcp aa=(gcp)a;
89   gcp bb=(gcp)b;
90   gcp cc=(gcp)omalloc(sizeof(*cc));
91   cc->a1=coeffs1->cfDiv(aa->a1,bb->a1,coeffs1);
92   cc->a2=coeffs2->cfDiv(aa->a2,bb->a2,coeffs2);
93   n_Test((number)cc,cf);
94   return (number)cc;
95 }
gIntMod(number a,number b,const coeffs cf)96 static number gIntMod (number a, number b, const coeffs cf)
97 {
98   gcp aa=(gcp)a;
99   gcp bb=(gcp)b;
100   gcp cc=(gcp)omalloc(sizeof(*cc));
101   cc->a1=coeffs1->cfIntMod(aa->a1,bb->a1,coeffs1);
102   cc->a2=coeffs2->cfIntMod(aa->a2,bb->a2,coeffs2);
103   n_Test((number)cc,cf);
104   return (number)cc;
105 }
gExactDiv(number a,number b,const coeffs cf)106 static number gExactDiv (number a, number b, const coeffs cf)
107 {
108   gcp aa=(gcp)a;
109   gcp bb=(gcp)b;
110   gcp cc=(gcp)omalloc(sizeof(*cc));
111   cc->a1=coeffs1->cfExactDiv(aa->a1,bb->a1,coeffs1);
112   cc->a2=coeffs2->cfExactDiv(aa->a2,bb->a2,coeffs2);
113   n_Test((number)cc,cf);
114   return (number)cc;
115 }
gInit(long i,const coeffs cf)116 static number gInit (long i, const coeffs cf)
117 {
118   gcp cc=(gcp)omalloc(sizeof(*cc));
119   cc->a1=coeffs1->cfInit(i,coeffs1);
120   cc->a2=coeffs2->cfInit(i,coeffs2);
121   n_Test((number)cc,cf);
122   return (number)cc;
123 }
gInitMPZ(mpz_t i,const coeffs cf)124 static number gInitMPZ (mpz_t i, const coeffs cf)
125 {
126   gcp cc=(gcp)omalloc(sizeof(*cc));
127   cc->a1=coeffs1->cfInitMPZ(i,coeffs1);
128   cc->a2=coeffs2->cfInitMPZ(i,coeffs2);
129   n_Test((number)cc,cf);
130   return (number)cc;
131 }
gSize(number a,const coeffs)132 static int gSize (number a, const coeffs)
133 {
134   gcp aa=(gcp)a;
135   int s1=coeffs1->cfSize(aa->a1,coeffs1);
136   int s2=coeffs2->cfSize(aa->a2,coeffs2);
137   if (s1!=s2)
138   {
139     printf("gSize: %d, %d\n",s1,s2);
140   }
141   return s1;
142 }
gInt(number & a,const coeffs)143 static long gInt (number &a, const coeffs)
144 {
145   gcp aa=(gcp)a;
146   long s1=coeffs1->cfInt(aa->a1,coeffs1);
147   long s2=coeffs2->cfInt(aa->a2,coeffs2);
148   if (s1!=s2)
149   {
150     printf("gInt: %ld, %ld\n",s1,s2);
151   }
152   return s1;
153 }
gMPZ(mpz_t result,number & n,const coeffs r)154 static void gMPZ(mpz_t result, number &n, const coeffs r)
155 {
156   coeffs1->cfMPZ(result,n,coeffs1);
157   mpz_t r2;
158   coeffs2->cfMPZ(r2,n,coeffs2);
159   if(mpz_cmp(result,r2)!=0)
160   {
161     printf("gMPZ\n");
162   }
163   mpz_clear(r2);
164 }
gInpNeg(number a,const coeffs r)165 static number  gInpNeg(number a, const coeffs r)
166 {
167   gcp aa=(gcp)a;
168   aa->a1=coeffs1->cfInpNeg(aa->a1,coeffs1);
169   aa->a2=coeffs2->cfInpNeg(aa->a2,coeffs2);
170   return (number)aa;
171 }
gInvers(number a,const coeffs r)172 static number  gInvers(number a, const coeffs r)
173 {
174   gcp aa=(gcp)a;
175   gcp cc=(gcp)omalloc(sizeof(*cc));
176   cc->a1=coeffs1->cfInvers(aa->a1,coeffs1);
177   cc->a2=coeffs2->cfInvers(aa->a2,coeffs2);
178   return (number)cc;
179 }
gCopy(number a,const coeffs r)180 static number  gCopy(number a, const coeffs r)
181 {
182   gcp aa=(gcp)a;
183   gcp cc=(gcp)omalloc(sizeof(*cc));
184   cc->a1=coeffs1->cfCopy(aa->a1,coeffs1);
185   cc->a2=coeffs2->cfCopy(aa->a2,coeffs2);
186   return (number)cc;
187 }
gRePart(number a,const coeffs r)188 static number  gRePart(number a, const coeffs r)
189 {
190   gcp aa=(gcp)a;
191   gcp cc=(gcp)omalloc(sizeof(*cc));
192   cc->a1=coeffs1->cfRePart(aa->a1,coeffs1);
193   cc->a2=coeffs2->cfRePart(aa->a2,coeffs2);
194   return (number)cc;
195 }
gImPart(number a,const coeffs r)196 static number  gImPart(number a, const coeffs r)
197 {
198   gcp aa=(gcp)a;
199   gcp cc=(gcp)omalloc(sizeof(*cc));
200   cc->a1=coeffs1->cfRePart(aa->a1,coeffs1);
201   cc->a2=coeffs2->cfRePart(aa->a2,coeffs2);
202   return (number)cc;
203 }
gWriteLong(number a,const coeffs r)204 static void  gWriteLong(number a, const coeffs r)
205 {
206   gcp aa=(gcp)a;
207   coeffs1->cfWriteLong(aa->a1,coeffs1);
208 }
gWriteShort(number a,const coeffs r)209 static void  gWriteShort(number a, const coeffs r)
210 {
211   gcp aa=(gcp)a;
212   coeffs1->cfWriteShort(aa->a1,coeffs1);
213 }
gRead(const char * s,number * a,const coeffs r)214 static const char *gRead(const char * s, number * a, const coeffs r)
215 {
216   gcp cc=(gcp)omalloc(sizeof(*cc));
217   const char* ss=coeffs1->cfRead(s,&(cc->a1),coeffs1);
218   number tmp=coeffs2->cfInit(1,coeffs2);
219   mpz_ptr tt=(mpz_ptr)tmp;
220   coeffs1->cfMPZ(tt,cc->a1,coeffs1);
221   cc->a2=(number)tt;
222   *a=(number)cc;
223   return ss;
224 }
gNormalize(number & a,const coeffs r)225 static void    gNormalize(number &a, const coeffs r)
226 {
227   gcp aa=(gcp)a;
228   coeffs1->cfNormalize(aa->a1,coeffs1);
229   coeffs2->cfNormalize(aa->a2,coeffs2);
230 }
gGreater(number a,number b,const coeffs r)231 static  BOOLEAN gGreater(number a,number b, const coeffs r)
232 {
233   gcp aa=(gcp)a;
234   gcp bb=(gcp)b;
235   BOOLEAN b1=coeffs1->cfGreater(aa->a1,bb->a1,coeffs1);
236   BOOLEAN b2=coeffs2->cfGreater(aa->a2,bb->a2,coeffs2);
237   if (b1!=b2)
238   {
239     printf("gGreater\n");
240   }
241   return b1;
242 }
gEqual(number a,number b,const coeffs r)243 static  BOOLEAN gEqual(number a,number b, const coeffs r)
244 {
245   gcp aa=(gcp)a;
246   gcp bb=(gcp)b;
247   BOOLEAN b1=coeffs1->cfEqual(aa->a1,bb->a1,coeffs1);
248   BOOLEAN b2=coeffs2->cfEqual(aa->a2,bb->a2,coeffs2);
249   if (b1!=b2)
250   {
251     printf("gEqual\n");
252   }
253   return b1;
254 }
gIsZero(number a,const coeffs r)255 static  BOOLEAN gIsZero(number a, const coeffs r)
256 {
257   if (a==NULL) return TRUE;
258   gcp aa=(gcp)a;
259   BOOLEAN b1=coeffs1->cfIsZero(aa->a1,coeffs1);
260   BOOLEAN b2=coeffs2->cfIsZero(aa->a2,coeffs2);
261   if (b1!=b2)
262   {
263     printf("gIsZero\n");
264   }
265   return b1;
266 }
gIsOne(number a,const coeffs r)267 static  BOOLEAN gIsOne(number a, const coeffs r)
268 {
269   gcp aa=(gcp)a;
270   BOOLEAN b1=coeffs1->cfIsOne(aa->a1,coeffs1);
271   BOOLEAN b2=coeffs2->cfIsOne(aa->a2,coeffs2);
272   if (b1!=b2)
273   {
274     printf("gIsOne\n");
275   }
276   return b1;
277 }
gIsMOne(number a,const coeffs r)278 static  BOOLEAN gIsMOne(number a, const coeffs r)
279 {
280   gcp aa=(gcp)a;
281   BOOLEAN b1=coeffs1->cfIsMOne(aa->a1,coeffs1);
282   BOOLEAN b2=coeffs2->cfIsMOne(aa->a2,coeffs2);
283   if (b1!=b2)
284   {
285     printf("gIsMOne\n");
286   }
287   return b1;
288 }
gGreaterZero(number a,const coeffs r)289 static  BOOLEAN gGreaterZero(number a, const coeffs r)
290 {
291   gcp aa=(gcp)a;
292   BOOLEAN b1=coeffs1->cfGreaterZero(aa->a1,coeffs1);
293   BOOLEAN b2=coeffs2->cfGreaterZero(aa->a2,coeffs2);
294   if (b1!=b2)
295   {
296     printf("gGreaterZero\n");
297   }
298   return b1;
299 }
gPower(number a,int i,number * result,const coeffs r)300 static void gPower(number a, int i, number * result, const coeffs r)
301 {
302   gcp aa=(gcp)a;
303   gcp cc=(gcp)omalloc(sizeof(*cc));
304   coeffs1->cfPower(aa->a1,i,&cc->a1,coeffs1);
305   coeffs2->cfPower(aa->a2,i,&cc->a2,coeffs2);
306   *result=(number)cc;
307 }
gGcd(number a,number b,const coeffs)308 static number gGcd (number a, number b, const coeffs)
309 {
310   gcp aa=(gcp)a;
311   gcp bb=(gcp)b;
312   gcp cc=(gcp)omalloc(sizeof(*cc));
313   cc->a1=coeffs1->cfGcd(aa->a1,bb->a1,coeffs1);
314   cc->a2=coeffs2->cfGcd(aa->a2,bb->a2,coeffs2);
315   return (number)cc;
316 }
gSubringGcd(number a,number b,const coeffs)317 static number gSubringGcd (number a, number b, const coeffs)
318 {
319   gcp aa=(gcp)a;
320   gcp bb=(gcp)b;
321   gcp cc=(gcp)omalloc(sizeof(*cc));
322   cc->a1=coeffs1->cfSubringGcd(aa->a1,bb->a1,coeffs1);
323   cc->a2=coeffs2->cfSubringGcd(aa->a2,bb->a2,coeffs2);
324   return (number)cc;
325 }
gGetDenom(number & a,const coeffs)326 static number gGetDenom (number &a, const coeffs)
327 {
328   gcp aa=(gcp)a;
329   gcp cc=(gcp)omalloc(sizeof(*cc));
330   cc->a1=coeffs1->cfGetDenom(aa->a1,coeffs1);
331   cc->a2=coeffs2->cfGetDenom(aa->a2,coeffs2);
332   return (number)cc;
333 }
gGetNumerator(number & a,const coeffs)334 static number gGetNumerator (number &a, const coeffs)
335 {
336   gcp aa=(gcp)a;
337   gcp cc=(gcp)omalloc(sizeof(*cc));
338   cc->a1=coeffs1->cfGetNumerator(aa->a1,coeffs1);
339   cc->a2=coeffs2->cfGetNumerator(aa->a2,coeffs2);
340   return (number)cc;
341 }
gQuotRem(number a,number b,number * rem,const coeffs r)342 static number  gQuotRem(number a, number b, number *rem, const coeffs r)
343 {
344  printf("gQuotRem\n");
345  return NULL;
346 }
gLcm(number a,number b,const coeffs r)347 static number  gLcm(number a, number b, const coeffs r)
348 {
349   gcp aa=(gcp)a;
350   gcp bb=(gcp)b;
351   gcp cc=(gcp)omalloc(sizeof(*cc));
352   cc->a1=coeffs1->cfLcm(aa->a1,bb->a1,coeffs1);
353   cc->a2=coeffs2->cfLcm(aa->a2,bb->a2,coeffs2);
354   return (number)cc;
355 }
gNormalizeHelper(number a,number b,const coeffs r)356 static number  gNormalizeHelper(number a, number b, const coeffs r)
357 {
358   gcp aa=(gcp)a;
359   gcp bb=(gcp)b;
360   gcp cc=(gcp)omalloc(sizeof(*cc));
361   cc->a1=coeffs1->cfNormalizeHelper(aa->a1,bb->a1,coeffs1);
362   cc->a2=coeffs2->cfNormalizeHelper(aa->a2,bb->a2,coeffs2);
363   return (number)cc;
364 }
gDelete(number * a,const coeffs r)365 static void  gDelete(number * a, const coeffs r)
366 {
367   if (*a!=NULL)
368   {
369     gcp aa=(gcp)*a;
370     coeffs1->cfDelete(&aa->a1,coeffs1);
371     coeffs2->cfDelete(&aa->a2,coeffs2);
372     omFree(aa);
373     *a=NULL;
374   }
375 }
gSetMap(const coeffs src,const coeffs dst)376 static nMapFunc gSetMap(const coeffs src, const coeffs dst)
377 {
378   printf("gSetMap\n");
379   return NULL;
380 }
gWriteFd(number a,FILE * f,const coeffs r)381 static void gWriteFd(number a, FILE *f, const coeffs r)
382 {
383   printf("gWriteFd\n");
384 }
gReadFd(s_buff f,const coeffs r)385 static number gReadFd( s_buff f, const coeffs r)
386 {
387   printf("gReadFd\n");
388   return NULL;
389 }
gFarey(number p,number n,const coeffs)390 static number  gFarey(number p, number n, const coeffs)
391 {
392   gcp aa=(gcp)p;
393   gcp cc=(gcp)omalloc(sizeof(*cc));
394   cc->a1=coeffs1->cfFarey(aa->a1,n,coeffs1);
395   cc->a2=coeffs2->cfFarey(aa->a2,n,coeffs2);
396   return (number)cc;
397 }
gChineseRemainder(number * x,number * q,int rl,BOOLEAN sym,CFArray & inv_cache,const coeffs)398 static number gChineseRemainder(number *x, number *q,int rl, BOOLEAN sym,CFArray &inv_cache,const coeffs)
399 {
400   printf("gChineseREmainder\n");
401   return NULL;
402 }
gRandom(siRandProc p,number p1,number p2,const coeffs cf)403 static  number gRandom(siRandProc p, number p1, number p2, const coeffs cf)
404 {
405   printf("gRandom\n");
406   return NULL;
407 }
gDivBy(number a,number b,const coeffs)408 static BOOLEAN gDivBy (number a,number b, const coeffs)
409 {
410   gcp aa=(gcp)a;
411   gcp bb=(gcp)b;
412   BOOLEAN b1=coeffs1->cfDivBy(aa->a1,bb->a1,coeffs1);
413   BOOLEAN b2=coeffs2->cfDivBy(aa->a2,bb->a2,coeffs2);
414   if (b1!=b2)
415   {
416     printf("gDivBy:%d,%d\n",b1,b2);
417   }
418   return b1;
419 }
gExtGcd(number a,number b,number * s,number * t,const coeffs)420 static number  gExtGcd (number a, number b, number *s, number *t, const coeffs)
421 {
422   gcp aa=(gcp)a;
423   gcp bb=(gcp)b;
424   gcp cc=(gcp)omalloc(sizeof(*cc));
425   gcp ss=(gcp)omalloc(sizeof(*ss));
426   gcp tt=(gcp)omalloc(sizeof(*ss));
427   cc->a1=coeffs1->cfExtGcd(aa->a1,bb->a1,&ss->a1,&tt->a1,coeffs1);
428   cc->a2=coeffs2->cfExtGcd(aa->a2,bb->a2,&ss->a2,&tt->a2,coeffs2);
429   return (number)cc;
430 }
gGetUnit(number n,const coeffs r)431 static number  gGetUnit (number n, const coeffs r)
432 {
433   gcp aa=(gcp)n;
434   gcp cc=(gcp)omalloc(sizeof(*cc));
435   cc->a1=coeffs1->cfGetUnit(aa->a1,coeffs1);
436   cc->a2=coeffs2->cfGetUnit(aa->a2,coeffs2);
437   return (number)cc;
438 }
gIsUnit(number a,const coeffs)439 static BOOLEAN gIsUnit (number a, const coeffs)
440 {
441   gcp aa=(gcp)a;
442   BOOLEAN b1=coeffs1->cfIsUnit(aa->a1,coeffs1);
443   BOOLEAN b2=coeffs2->cfIsUnit(aa->a2,coeffs2);
444   if (b1!=b2)
445   {
446     printf("gIsUnit:%d,%d\n",b1,b2);
447   }
448   return b1;
449 }
gDivComp(number a,number b,const coeffs r)450 static int gDivComp(number a, number b, const coeffs r)
451 {
452   gcp aa=(gcp)a;
453   gcp bb=(gcp)b;
454   int i1=coeffs1->cfDivComp(aa->a1,bb->a1,coeffs1);
455   int i2=coeffs2->cfDivComp(aa->a2,bb->a2,coeffs2);
456   if (i1!=i2)
457   {
458     printf("gDivComp:%d,%d\n",i1,i2);
459   }
460   return i1;
461 }
gDBTest(number a,const char * f,const int l,const coeffs r)462 static BOOLEAN gDBTest(number a, const char *f, const int l, const coeffs r)
463 {
464  if (a==NULL)
465    printf("NULL in %s:%d\n",f,l);
466  return TRUE;
467 }
gInitChar(coeffs r,void * p)468 BOOLEAN gInitChar(coeffs r, void* p)
469 {
470   coeffs1=nInitChar(n_Z_1,(void*)1);
471   coeffs2=nInitChar(n_Z_2,(void*)1);
472   r->is_field=FALSE;
473   r->is_domain=TRUE;
474   r->rep=n_rep_unknown;
475   r->ch = 0;
476   r->cfKillChar=gKillChar;
477   //r->nCoeffIsEqual=gCoeffsEqual;
478   r->cfCoeffName=gCoeffName;
479   r->cfCoeffWrite=gCoeffWrite;
480 
481   r->cfMult  = gMult;
482   r->cfSub   = gSub;
483   r->cfAdd   = gAdd;
484   r->cfDiv   = gDiv;
485   r->cfInit = gInit;
486   r->cfSize  = gSize;
487   r->cfInt  = gInt;
488   #ifdef HAVE_RINGS
489   r->cfDivComp = gDivComp;
490   r->cfIsUnit = gIsUnit;
491   r->cfGetUnit = gGetUnit;
492   r->cfExtGcd = gExtGcd;
493    r->cfDivBy = gDivBy;
494   #endif
495   r->cfInpNeg   = gInpNeg;
496   r->cfInvers= gInvers;
497   r->cfCopy  = gCopy;
498   r->cfRePart = gCopy;
499   //r->cfImPart = ndReturn0;
500   r->cfWriteLong = gWriteLong;
501   r->cfWriteShort = gWriteShort;
502   r->cfRead = gRead;
503   r->cfNormalize=gNormalize;
504   r->cfGreater = gGreater;
505   r->cfEqual = gEqual;
506   r->cfIsZero = gIsZero;
507   r->cfIsOne = gIsOne;
508   r->cfIsMOne = gIsMOne;
509   r->cfGreaterZero = gGreaterZero;
510   r->cfPower = gPower;
511   r->cfGetDenom = gGetDenom;
512   r->cfGetNumerator = gGetNumerator;
513   r->cfGcd  = gGcd;
514   r->cfLcm  = gGcd;
515   r->cfDelete= gDelete;
516   r->cfSetMap = gSetMap;
517   //r->cfInpMult=ndInpMult;
518   r->cfRandom=gRandom;
519   r->cfWriteFd=gWriteFd;
520   r->cfReadFd=gReadFd;
521   r->type=n_Z;
522   #ifdef LDEBUG
523   r->cfDBTest=gDBTest;
524   #endif
525   return FALSE;
526 }
527 
528