number2.cc
Go to the documentation of this file.
1 #include "kernel/mod2.h" // general settings/macros
2 #include <reporter/reporter.h> // for Print, WerrorS
3 #include <coeffs/numbers.h> // nRegister, coeffs.h
4 #include <coeffs/rmodulon.h> // ZnmInfo
5 #include <coeffs/bigintmat.h> // bigintmat
6 #include <coeffs/longrat.h> // BIGINTs: nlGMP
7 #include <polys/ext_fields/algext.h> // AlgExtInfo
8 #include <misc/prime.h> // IsPrime
9 #include <Singular/blackbox.h> // blackbox type
10 #include <Singular/ipshell.h> // IsPrime
11 #include <Singular/ipconv.h> // iiConvert etc.
12 
13 #include <Singular/ipid.h> // for SModulFunctions, leftv
14 
15 #include <Singular/number2.h>
16 
17 char *crString(coeffs c)
18 {
19  if (c==NULL)
20  {
21  return omStrDup("oo");
22  }
23  return omStrDup(nCoeffName(c));
24 }
25 void crPrint(coeffs c)
26 {
27  char *s=crString(c);
28  PrintS(s);
29  omFree(s);
30 }
31 
32 // -----------------------------------------------------------
33 // interpreter stuff for cring/coeffs
34 // -----------------------------------------------------------
36 {
37  coeffs c1=(coeffs)a->Data();
38  int i2=(int)(long)b->Data();
39  if (c1->type==n_Z)
40  {
41  if (i2==IsPrime(i2))
42  {
43  res->data=(void *)nInitChar(n_Zp,(void*)(long)i2);
44  }
45  else
46  {
47  ZnmInfo info;
48  mpz_ptr modBase= (mpz_ptr) omAlloc(sizeof(mpz_t));
49  mpz_init_set_ui(modBase,i2);
50  info.base= modBase;
51  info.exp= 1;
52  res->data=(void *)nInitChar(n_Zn,&info);
53  }
54  return FALSE;
55  }
56  return TRUE;
57 }
59 {
60  coeffs c1=(coeffs)a->Data();
61  number i2=(number)b->Data();
62  if (c1->type==n_Z)
63  {
64  ZnmInfo info;
65  number modBase= (number) omAlloc(sizeof(mpz_t));
66  nlGMP(i2,modBase,coeffs_BIGINT); // FIXME? TODO? // extern void nlGMP(number &i, number n, const coeffs r); // to be replaced with n_MPZ(modBase,i2,coeffs_BIGINT); // ?
67  info.base= (mpz_ptr)modBase;
68  info.exp= 1;
69  res->data=(void *)nInitChar(n_Zn,&info);
70  return FALSE;
71  }
72  return TRUE;
73 }
74 
76 {
77  coeffs a2=(coeffs)a->Data();
78  coeffs b2=(coeffs)b->Data();
79  res->data=(void*)(long)(a2==b2);
80  return FALSE;
81 }
82 
83 #ifdef SINGULAR_4_2
84 // -----------------------------------------------------------
85 // interpreter stuff for Number/number2
86 // -----------------------------------------------------------
87 BOOLEAN jjNUMBER2_POW(leftv res, leftv a, leftv b)
88 {
89  number2 a2=(number2)a->Data();
90  if (a2->cf==NULL) return TRUE;
91  number2 r=(number2)omAlloc0(sizeof(*r));
92  r->cf=a2->cf;
93  n_Power(a2->n,(int)(long)b->Data(),&(r->n),r->cf);
94  return FALSE;
95 }
96 BOOLEAN jjNUMBER2_OP2(leftv res, leftv a, leftv b)
97 {
98  int op=iiOp;
99  // binary operations for number2
100  number2 a2=NULL;
101  number aa=NULL;
102  number2 b2=NULL;
103  number bb=NULL;
106  int ai,bi;
107  int at=a->Typ();
108  int bt=b->Typ();
109  if ((ai=iiTestConvert(at,CNUMBER_CMD,dConvertTypes))!=0)
110  {
111  if ((bi=iiTestConvert(bt,CNUMBER_CMD,dConvertTypes))!=0)
112  {
113  iiConvert(at,CNUMBER_CMD,ai,a,an);
114  iiConvert(bt,CNUMBER_CMD,bi,b,bn);
115  a2=(number2)an->Data();
116  b2=(number2)bn->Data();
117  if (((a2!=NULL) && (b2!=NULL) && (a2->cf!=b2->cf))
118  || (a2==NULL)
119  || (b2==NULL))
120  {
121  an->CleanUp();
122  bn->CleanUp();
125  WerrorS("Number not compatible");
126  return TRUE;
127  }
128  aa=a2->n;
129  bb=b2->n;
130  number2 r=(number2)omAlloc0(sizeof(*r));
131  r->cf=a2->cf;
132  if (r->cf==NULL) op=0; // force error
133  switch(op)
134  {
135  case '+': r->n=n_Add(aa,bb,r->cf);break;
136  case '-': r->n=n_Sub(aa,bb,r->cf);break;
137  case '*': r->n=n_Mult(aa,bb,r->cf);break;
138  case '/': r->n=n_Div(aa,bb,r->cf);break;
139  case '%': r->n=n_IntMod(aa,bb,r->cf);break;
140  default: Werror("unknown binary operation %s(%d)",Tok2Cmdname(op),op);
141  omFree(r);
142  an->CleanUp();
143  bn->CleanUp();
146  return TRUE;
147  }
148  res->data=(void*)r;
149  r->cf->ref++;
150  return FALSE;
151  }
152  else
153  {
154  an->CleanUp();
156  Werror("cannot convert second operand (%s) to Number",b->Name());
157  return TRUE;
158  }
159  }
160  else
161  {
162  Werror("cannot convert first operand (%s) to Number",a->Name());
163  return TRUE;
164  }
165 }
166 BOOLEAN jjNUMBER2_OP1(leftv res, leftv a)
167 {
168  int op=iiOp;
169  // unary operations for number2
170  number2 a2=(number2)a->Data();
171  number2 r=(number2)omAlloc(sizeof(*r));
172  r->cf=a2->cf;
173  if (a2->cf==NULL) op=0; // force error
174  switch(op)
175  {
176  case '-': r->n=n_Copy(a2->n,a2->cf);r->n=n_InpNeg(r->n,a2->cf);break;
177  default: Werror("unknown unary operation %s(%d)",Tok2Cmdname(op),op);
178  omFree(r);
179  return TRUE;
180  }
181  res->data=(void*)r;
182  r->cf->ref++;
183  return FALSE;
184 }
185 
186 BOOLEAN jjPOLY2_POW(leftv res, leftv a, leftv b)
187 {
188  poly2 a2=(poly2)a->Data();
189  if (a2->cf==NULL) return TRUE;
190  poly2 r=(poly2)omAlloc0(sizeof(*r));
191  r->cf=a2->cf;
192  r->n=p_Power(p_Copy(a2->n,r->cf),(int)(long)b->Data(),r->cf);
193  return FALSE;
194 }
195 BOOLEAN jjPOLY2_OP2(leftv res, leftv a, leftv b)
196 {
197  int op=iiOp;
198  // binary operations for poly2
199  poly2 a2=NULL;
200  poly aa=NULL;
201  poly2 b2=NULL;
202  poly bb=NULL;
205  int ai,bi;
206  int at=a->Typ();
207  int bt=b->Typ();
208  if ((ai=iiTestConvert(at,CPOLY_CMD,dConvertTypes))!=0)
209  {
210  if ((bi=iiTestConvert(bt,CPOLY_CMD,dConvertTypes))!=0)
211  {
212  iiConvert(at,CPOLY_CMD,ai,a,an);
213  iiConvert(bt,CPOLY_CMD,bi,b,bn);
214  a2=(poly2)an->Data();
215  b2=(poly2)bn->Data();
216  if (((a2!=NULL) && (b2!=NULL) && (a2->cf!=b2->cf))
217  || (a2==NULL)
218  || (b2==NULL))
219  {
220  an->CleanUp();
221  bn->CleanUp();
224  WerrorS("Poly not compatible");
225  return TRUE;
226  }
227  aa=a2->n;
228  bb=b2->n;
229  poly2 r=(poly2)omAlloc0(sizeof(*r));
230  r->cf=a2->cf;
231  if (r->cf==NULL) op=0; // force error
232  switch(op)
233  {
234  case '+': r->n=p_Add_q(p_Copy(aa,r->cf),p_Copy(bb,r->cf),r->cf);break;
235  case '-': r->n=p_Sub(p_Copy(aa,r->cf),p_Copy(bb,r->cf),r->cf);break;
236  case '*': r->n=pp_Mult_qq(aa,bb,r->cf);break;
237  //case '/': r->n=n_Div(aa,bb,r->cf);break;
238  //case '%': r->n=n_IntMod(aa,bb,r->cf);break;
239  default: Werror("unknown binary operation %s(%d)",Tok2Cmdname(op),op);
240  omFree(r);
241  an->CleanUp();
242  bn->CleanUp();
245  return TRUE;
246  }
247  res->data=(void*)r;
248  r->cf->ref++;
249  return FALSE;
250  }
251  else
252  {
253  an->CleanUp();
255  Werror("cannot convert second operand (%s) to Poly",b->Name());
256  return TRUE;
257  }
258  }
259  else
260  {
261  Werror("cannot convert first operand (%s) to Poly",a->Name());
262  return TRUE;
263  }
264 }
265 BOOLEAN jjPOLY2_OP1(leftv res, leftv a)
266 {
267  int op=iiOp;
268  // unary operations for poly2
269  poly2 a2=(poly2)a->Data();
270  poly2 r=(poly2)omAlloc(sizeof(*r));
271  r->cf=a2->cf;
272  if (a2->cf==NULL) op=0; // force error
273  switch(op)
274  {
275  case '-': r->n=p_Copy(a2->n,a2->cf);r->n=p_Neg(r->n,a2->cf);break;
276  default: Werror("unknown unary operation %s(%d)",Tok2Cmdname(op),op);
277  omFree(r);
278  return TRUE;
279  }
280  res->data=(void*)r;
281  r->cf->ref++;
282  return FALSE;
283 }
284 
285 BOOLEAN jjNUMBER2CR(leftv res, leftv a, leftv b)
286 {
287  number2 r=(number2)omAlloc(sizeof(*r));
288  r->cf=(coeffs)b->CopyD();
289  BOOLEAN bo=FALSE;
290  switch(a->Typ())
291  {
292  case INT_CMD:
293  r->n=n_Init((long)a->Data(),r->cf); break;
294  case BIGINT_CMD:
295  {
296  nMapFunc nMap=n_SetMap(coeffs_BIGINT,r->cf);
297  r->n=nMap((number)a->Data(),coeffs_BIGINT,r->cf); break;
298  }
299  case NUMBER_CMD:
300  {
301  nMapFunc nMap=n_SetMap(currRing->cf,r->cf);
302  if (nMap!=NULL)
303  r->n=nMap((number)a->Data(),currRing->cf,r->cf);
304  else
305  bo=TRUE;
306  break;
307  }
308  case CNUMBER_CMD:
309  {
310  number2 a2=(number2)a->Data();
311  if (a2->cf==NULL) bo=TRUE;
312  else
313  {
314  nMapFunc nMap=n_SetMap(a2->cf,r->cf);
315  if (nMap!=NULL)
316  r->n=nMap(a2->n,a2->cf,r->cf);
317  else
318  bo=TRUE;
319  }
320  break;
321  }
322  default: bo=TRUE; break;
323  }
324  if (bo)
325  {
326  Werror("no conversion to Number from %s",Tok2Cmdname(a->Typ()));
327  omFreeSize(r,sizeof(*r));
328  }
329  else
330  res->data=(void*)r;
331  return bo;
332 }
333 
334 BOOLEAN jjN2_CR(leftv res, leftv a) // number2 ->cring
335 {
336  number2 n=(number2)a->Data();
337  n->cf->ref++;
338  res->data=(void*)n->cf;
339  return FALSE;
340 }
341 
342 BOOLEAN jjP2_R(leftv res, leftv a) // poly2 ->ring
343 {
344  poly2 n=(poly2)a->Data();
345  n->cf->ref++;
346  res->data=(void*)n->cf;
347  return FALSE;
348 }
349 
350 BOOLEAN jjCM_CR(leftv res, leftv a) // cmatrix ->cring
351 {
352  bigintmat *b=(bigintmat*)a->Data();
353  coeffs cf=b->basecoeffs();
354  if (cf!=NULL)
355  {
356  cf->ref++;
357  }
358  res->data=(void*)cf;
359  return FALSE;
360 }
361 
362 BOOLEAN jjCMATRIX_3(leftv res, leftv r, leftv c,leftv cf)
363 {
364  bigintmat *b=new bigintmat((int)(long)r->Data(),
365  (int)(long)c->Data(),
366  (coeffs)cf->Data());
367  res->data=(char*)b;
368  return FALSE;
369 }
370 
371 BOOLEAN jjN2_N(leftv res, leftv a) // number2 ->number
372 {
373  number2 n2=(number2)a->Data();
374  BOOLEAN bo=TRUE;
375  if (currRing!=NULL)
376  {
377  nMapFunc nMap=n_SetMap(n2->cf,currRing->cf);
378  if (nMap!=NULL)
379  {
380  res->data=(void*)nMap(n2->n,n2->cf,currRing->cf);
381  bo=FALSE;
382  }
383  }
384  return bo;
385 }
386 
387 // -----------------------------------------------------------
388 // operations with Number/number2
389 // -----------------------------------------------------------
390 number2 n2Copy(const number2 d)
391 {
392  number2 r=NULL;
393  if ((d!=NULL)&&(d->cf!=NULL))
394  {
395  r=(number2)omAlloc(sizeof(*r));
396  d->cf->ref++;
397  r->cf=d->cf;
398  if (d->cf!=NULL)
399  r->n=n_Copy(d->n,d->cf);
400  else
401  r->n=NULL;
402  }
403  return r;
404 }
405 void n2Delete(number2 &d)
406 {
407  if (d!=NULL)
408  {
409  if (d->cf!=NULL)
410  {
411  n_Delete(&d->n,d->cf);
412  nKillChar(d->cf);
413  }
414  omFreeSize(d,sizeof(*d));
415  d=NULL;
416  }
417 }
418 char *n2String(number2 d, BOOLEAN typed)
419 {
420  StringSetS("");
421  if ((d!=NULL) && (d->cf!=NULL))
422  {
423  if (typed) StringAppendS("Number(");
424  n_Write(d->n,d->cf);
425  if (typed) StringAppendS(")");
426  }
427  else StringAppendS("oo");
428  return StringEndS();
429 }
430 
431 void n2Print(number2 d)
432 {
433  char *s=n2String(d,FALSE);
434  PrintS(s);
435  omFree(s);
436 }
437 
438 // -----------------------------------------------------------
439 // operations with Poly/poly2
440 // -----------------------------------------------------------
441 
442 poly2 p2Copy(const poly2 d)
443 {
444  poly2 r=NULL;
445  if ((d!=NULL)&&(d->cf!=NULL))
446  {
447  r=(poly2)omAlloc(sizeof(*r));
448  d->cf->ref++;
449  r->cf=d->cf;
450  if (d->cf!=NULL)
451  r->n=p_Copy(d->n,d->cf);
452  else
453  r->n=NULL;
454  }
455  return r;
456 }
457 void p2Delete(poly2 &d)
458 {
459  if (d!=NULL)
460  {
461  if (d->cf!=NULL)
462  {
463  p_Delete(&d->n,d->cf);
464  rKill(d->cf);
465  }
466  omFreeSize(d,sizeof(*d));
467  d=NULL;
468  }
469 }
470 char *p2String(poly2 d, BOOLEAN typed)
471 {
472  StringSetS("");
473  if ((d!=NULL) && (d->cf!=NULL))
474  {
475  if (typed) StringAppendS("Poly(");
476  p_Write0(d->n,d->cf);
477  if (typed) StringAppendS(")");
478  }
479  else StringAppendS("oo");
480  return StringEndS();
481 }
482 
483 void p2Print(poly2 d)
484 {
485  char *s=p2String(d,FALSE);
486  PrintS(s);
487  omFree(s);
488 }
489 
490 // ---------------------------------------------------------------------
491 #include <coeffs/bigintmat.h>
492 BOOLEAN jjBIM2_CR(leftv res, leftv a) // bigintmat ->cring
493 {
494  bigintmat *b=(bigintmat*)a->Data();
495  coeffs cf=b->basecoeffs();
496  if (cf==NULL) return TRUE;
497  cf->ref++;
498  res->data=(void*)cf;
499  return FALSE;
500 }
501 
502 BOOLEAN jjR2_CR(leftv res, leftv a) // ring ->cring
503 {
504  ring r=(ring)a->Data();
505  AlgExtInfo extParam;
506  extParam.r = r;
507  coeffs cf=nInitChar(n_polyExt,&extParam);
508  res->data=(void*)cf;
509  return FALSE;
510 }
511 
512 #endif
mpz_ptr base
Definition: rmodulon.h:19
static FORCE_INLINE number n_Sub(number a, number b, const coeffs r)
return the difference of &#39;a&#39; and &#39;b&#39;, i.e., a-b
Definition: coeffs.h:673
static FORCE_INLINE number n_IntMod(number a, number b, const coeffs r)
for r a field, return n_Init(0,r) always: n_Div(a,b,r)*b+n_IntMod(a,b,r)==a n_IntMod(a,b,r) >=0
Definition: coeffs.h:632
const CanonicalForm int s
Definition: facAbsFact.cc:55
int iiTestConvert(int inputType, int outputType)
Definition: gentable.cc:293
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
const poly a
Definition: syzextra.cc:212
Definition: tok.h:48
only used if HAVE_RINGS is defined
Definition: coeffs.h:44
Definition: tok.h:95
ring r
Definition: algext.h:40
BOOLEAN jjCRING_Zm(leftv res, leftv a, leftv b)
Definition: number2.cc:58
const struct sConvertTypes dConvertTypes[]
Definition: table.h:1178
#define FALSE
Definition: auxiliary.h:94
BOOLEAN iiConvert(int inputType, int outputType, int index, leftv input, leftv output, const struct sConvertTypes *dConvertTypes)
Definition: ipconv.cc:401
Definition: tok.h:38
Matrices of numbers.
Definition: bigintmat.h:51
static FORCE_INLINE number n_Init(long i, const coeffs r)
a number representing i in the given coeff field/ring r
Definition: coeffs.h:542
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
{p < 2^31}
Definition: coeffs.h:30
#define TRUE
Definition: auxiliary.h:98
BOOLEAN jjEQUAL_CR(leftv res, leftv a, leftv b)
Definition: number2.cc:75
void * ADDRESS
Definition: auxiliary.h:115
sleftv * leftv
Definition: structs.h:60
void WerrorS(const char *s)
Definition: feFopen.cc:24
char * StringEndS()
Definition: reporter.cc:151
void nlGMP(number &i, number n, const coeffs r)
Definition: longrat.cc:1467
coeffs coeffs_BIGINT
Definition: ipid.cc:54
int Typ()
Definition: subexpr.cc:996
#define omAlloc(size)
Definition: omAllocDecl.h:210
poly p_Sub(poly p1, poly p2, const ring r)
Definition: p_polys.cc:1911
const char * Name()
Definition: subexpr.h:121
static poly p_Copy(poly p, const ring r)
returns a copy of p
Definition: p_polys.h:804
char * crString(coeffs c)
Definition: number2.cc:17
void * data
Definition: subexpr.h:89
poly res
Definition: myNF.cc:322
static FORCE_INLINE number n_Mult(number a, number b, const coeffs r)
return the product of &#39;a&#39; and &#39;b&#39;, i.e., a*b
Definition: coeffs.h:640
ring currRing
Widely used global variable which specifies the current polynomial ring for Singular interpreter and ...
Definition: polys.cc:10
const ring r
Definition: syzextra.cc:208
void rKill(ring r)
Definition: ipshell.cc:6026
#define omFree(addr)
Definition: omAllocDecl.h:261
The main handler for Singular numbers which are suitable for Singular polynomials.
static FORCE_INLINE number n_Add(number a, number b, const coeffs r)
return the sum of &#39;a&#39; and &#39;b&#39;, i.e., a+b
Definition: coeffs.h:660
void StringSetS(const char *st)
Definition: reporter.cc:128
static poly pp_Mult_qq(poly p, poly q, const ring r)
Definition: p_polys.h:1070
void StringAppendS(const char *st)
Definition: reporter.cc:107
const ExtensionInfo & info
< [in] sqrfree poly
number(* nMapFunc)(number a, const coeffs src, const coeffs dst)
maps "a", which lives in src, into dst
Definition: coeffs.h:73
static FORCE_INLINE void n_Write(number n, const coeffs r, const BOOLEAN bShortOut=TRUE)
Definition: coeffs.h:595
static FORCE_INLINE char * nCoeffName(const coeffs cf)
Definition: coeffs.h:980
static FORCE_INLINE number n_InpNeg(number n, const coeffs r)
in-place negation of n MUST BE USED: n = n_InpNeg(n) (no copy is returned)
Definition: coeffs.h:561
only used if HAVE_RINGS is defined
Definition: coeffs.h:43
unsigned long exp
Definition: rmodulon.h:19
omBin sleftv_bin
Definition: subexpr.cc:50
void PrintS(const char *s)
Definition: reporter.cc:284
int IsPrime(int p)
Definition: prime.cc:61
static FORCE_INLINE nMapFunc n_SetMap(const coeffs src, const coeffs dst)
set the mapping function pointers for translating numbers from src to dst
Definition: coeffs.h:725
void p_Write0(poly p, ring lmRing, ring tailRing)
Definition: polys0.cc:196
static void p_Delete(poly *p, const ring r)
Definition: p_polys.h:843
#define omAlloc0Bin(bin)
Definition: omAllocDecl.h:206
static FORCE_INLINE void n_Power(number a, int b, number *res, const coeffs r)
fill res with the power a^b
Definition: coeffs.h:636
CanonicalForm cf
Definition: cfModGcd.cc:4024
#define NULL
Definition: omList.c:10
static FORCE_INLINE number n_Copy(number n, const coeffs r)
return a copy of &#39;n&#39;
Definition: coeffs.h:455
struct for passing initialization parameters to naInitChar
Definition: algext.h:40
const char * Tok2Cmdname(int tok)
Definition: gentable.cc:132
static FORCE_INLINE number n_Div(number a, number b, const coeffs r)
return the quotient of &#39;a&#39; and &#39;b&#39;, i.e., a/b; raises an error if &#39;b&#39; is not invertible in r exceptio...
Definition: coeffs.h:619
coeffs basecoeffs() const
Definition: bigintmat.h:147
BOOLEAN jjCRING_Zp(leftv res, leftv a, leftv b)
Definition: number2.cc:35
void CleanUp(ring r=currRing)
Definition: subexpr.cc:333
void * Data()
Definition: subexpr.cc:1138
static poly p_Neg(poly p, const ring r)
Definition: p_polys.h:1013
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete &#39;p&#39;
Definition: coeffs.h:459
polyrec * poly
Definition: hilb.h:10
static poly p_Add_q(poly p, poly q, const ring r)
Definition: p_polys.h:877
#define omFreeBin(addr, bin)
Definition: omAllocDecl.h:259
int iiOp
Definition: iparith.cc:225
int BOOLEAN
Definition: auxiliary.h:85
const poly b
Definition: syzextra.cc:213
void nKillChar(coeffs r)
undo all initialisations
Definition: numbers.cc:496
poly p_Power(poly p, int i, const ring r)
Definition: p_polys.cc:2118
void Werror(const char *fmt,...)
Definition: reporter.cc:189
void * CopyD(int t)
Definition: subexpr.cc:708
#define omAlloc0(size)
Definition: omAllocDecl.h:211
used to represent polys as coeffcients
Definition: coeffs.h:35
void crPrint(coeffs c)
Definition: number2.cc:25
coeffs nInitChar(n_coeffType t, void *parameter)
one-time initialisations for new coeffs in case of an error return NULL
Definition: numbers.cc:334
#define omStrDup(s)
Definition: omAllocDecl.h:263