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 BOOLEAN jjR2_CR(leftv res, leftv a) // ring ->cring
84 {
85  ring r=(ring)a->Data();
86  AlgExtInfo extParam;
87  extParam.r = r;
88  coeffs cf=nInitChar(n_transExt,&extParam);
89  res->data=(void*)cf;
90  return FALSE;
91 }
92 
93 #ifdef SINGULAR_4_2
94 // -----------------------------------------------------------
95 // interpreter stuff for Number/number2
96 // -----------------------------------------------------------
97 BOOLEAN jjNUMBER2_POW(leftv res, leftv a, leftv b)
98 {
99  number2 a2=(number2)a->Data();
100  if (a2->cf==NULL) return TRUE;
101  number2 r=(number2)omAlloc0(sizeof(*r));
102  r->cf=a2->cf;
103  n_Power(a2->n,(int)(long)b->Data(),&(r->n),r->cf);
104  return FALSE;
105 }
106 BOOLEAN jjNUMBER2_OP2(leftv res, leftv a, leftv b)
107 {
108  int op=iiOp;
109  // binary operations for number2
110  number2 a2=NULL;
111  number aa=NULL;
112  number2 b2=NULL;
113  number bb=NULL;
116  int ai,bi;
117  int at=a->Typ();
118  int bt=b->Typ();
119  if ((ai=iiTestConvert(at,CNUMBER_CMD,dConvertTypes))!=0)
120  {
121  if ((bi=iiTestConvert(bt,CNUMBER_CMD,dConvertTypes))!=0)
122  {
123  iiConvert(at,CNUMBER_CMD,ai,a,an);
124  iiConvert(bt,CNUMBER_CMD,bi,b,bn);
125  a2=(number2)an->Data();
126  b2=(number2)bn->Data();
127  if (((a2!=NULL) && (b2!=NULL) && (a2->cf!=b2->cf))
128  || (a2==NULL)
129  || (b2==NULL))
130  {
131  an->CleanUp();
132  bn->CleanUp();
135  WerrorS("Number not compatible");
136  return TRUE;
137  }
138  aa=a2->n;
139  bb=b2->n;
140  number2 r=(number2)omAlloc0(sizeof(*r));
141  r->cf=a2->cf;
142  if (r->cf==NULL) op=0; // force error
143  switch(op)
144  {
145  case '+': r->n=n_Add(aa,bb,r->cf);break;
146  case '-': r->n=n_Sub(aa,bb,r->cf);break;
147  case '*': r->n=n_Mult(aa,bb,r->cf);break;
148  case '/': r->n=n_Div(aa,bb,r->cf);break;
149  case '%': r->n=n_IntMod(aa,bb,r->cf);break;
150  default: Werror("unknown binary operation %s(%d)",Tok2Cmdname(op),op);
151  omFree(r);
152  an->CleanUp();
153  bn->CleanUp();
156  return TRUE;
157  }
158  res->data=(void*)r;
159  r->cf->ref++;
160  return FALSE;
161  }
162  else
163  {
164  an->CleanUp();
166  Werror("cannot convert second operand (%s) to Number",b->Name());
167  return TRUE;
168  }
169  }
170  else
171  {
172  Werror("cannot convert first operand (%s) to Number",a->Name());
173  return TRUE;
174  }
175 }
176 BOOLEAN jjNUMBER2_OP1(leftv res, leftv a)
177 {
178  int op=iiOp;
179  // unary operations for number2
180  number2 a2=(number2)a->Data();
181  number2 r=(number2)omAlloc(sizeof(*r));
182  r->cf=a2->cf;
183  if (a2->cf==NULL) op=0; // force error
184  switch(op)
185  {
186  case '-': r->n=n_Copy(a2->n,a2->cf);r->n=n_InpNeg(r->n,a2->cf);break;
187  default: Werror("unknown unary operation %s(%d)",Tok2Cmdname(op),op);
188  omFree(r);
189  return TRUE;
190  }
191  res->data=(void*)r;
192  r->cf->ref++;
193  return FALSE;
194 }
195 
196 BOOLEAN jjPOLY2_POW(leftv res, leftv a, leftv b)
197 {
198  poly2 a2=(poly2)a->Data();
199  if (a2->cf==NULL) return TRUE;
200  poly2 r=(poly2)omAlloc0(sizeof(*r));
201  r->cf=a2->cf;
202  r->n=p_Power(p_Copy(a2->n,r->cf),(int)(long)b->Data(),r->cf);
203  return FALSE;
204 }
205 BOOLEAN jjPOLY2_OP2(leftv res, leftv a, leftv b)
206 {
207  int op=iiOp;
208  // binary operations for poly2
209  poly2 a2=NULL;
210  poly aa=NULL;
211  poly2 b2=NULL;
212  poly bb=NULL;
215  int ai,bi;
216  int at=a->Typ();
217  int bt=b->Typ();
218  if ((ai=iiTestConvert(at,CPOLY_CMD,dConvertTypes))!=0)
219  {
220  if ((bi=iiTestConvert(bt,CPOLY_CMD,dConvertTypes))!=0)
221  {
222  iiConvert(at,CPOLY_CMD,ai,a,an);
223  iiConvert(bt,CPOLY_CMD,bi,b,bn);
224  a2=(poly2)an->Data();
225  b2=(poly2)bn->Data();
226  if (((a2!=NULL) && (b2!=NULL) && (a2->cf!=b2->cf))
227  || (a2==NULL)
228  || (b2==NULL))
229  {
230  an->CleanUp();
231  bn->CleanUp();
234  WerrorS("Poly not compatible");
235  return TRUE;
236  }
237  aa=a2->n;
238  bb=b2->n;
239  poly2 r=(poly2)omAlloc0(sizeof(*r));
240  r->cf=a2->cf;
241  if (r->cf==NULL) op=0; // force error
242  switch(op)
243  {
244  case '+': r->n=p_Add_q(p_Copy(aa,r->cf),p_Copy(bb,r->cf),r->cf);break;
245  case '-': r->n=p_Sub(p_Copy(aa,r->cf),p_Copy(bb,r->cf),r->cf);break;
246  case '*': r->n=pp_Mult_qq(aa,bb,r->cf);break;
247  //case '/': r->n=n_Div(aa,bb,r->cf);break;
248  //case '%': r->n=n_IntMod(aa,bb,r->cf);break;
249  default: Werror("unknown binary operation %s(%d)",Tok2Cmdname(op),op);
250  omFree(r);
251  an->CleanUp();
252  bn->CleanUp();
255  return TRUE;
256  }
257  res->data=(void*)r;
258  r->cf->ref++;
259  return FALSE;
260  }
261  else
262  {
263  an->CleanUp();
265  Werror("cannot convert second operand (%s) to Poly",b->Name());
266  return TRUE;
267  }
268  }
269  else
270  {
271  Werror("cannot convert first operand (%s) to Poly",a->Name());
272  return TRUE;
273  }
274 }
275 BOOLEAN jjPOLY2_OP1(leftv res, leftv a)
276 {
277  int op=iiOp;
278  // unary operations for poly2
279  poly2 a2=(poly2)a->Data();
280  poly2 r=(poly2)omAlloc(sizeof(*r));
281  r->cf=a2->cf;
282  if (a2->cf==NULL) op=0; // force error
283  switch(op)
284  {
285  case '-': r->n=p_Copy(a2->n,a2->cf);r->n=p_Neg(r->n,a2->cf);break;
286  default: Werror("unknown unary operation %s(%d)",Tok2Cmdname(op),op);
287  omFree(r);
288  return TRUE;
289  }
290  res->data=(void*)r;
291  r->cf->ref++;
292  return FALSE;
293 }
294 
295 BOOLEAN jjNUMBER2CR(leftv res, leftv a, leftv b)
296 {
297  number2 r=(number2)omAlloc(sizeof(*r));
298  r->cf=(coeffs)b->CopyD();
299  BOOLEAN bo=FALSE;
300  switch(a->Typ())
301  {
302  case INT_CMD:
303  r->n=n_Init((long)a->Data(),r->cf); break;
304  case BIGINT_CMD:
305  {
306  nMapFunc nMap=n_SetMap(coeffs_BIGINT,r->cf);
307  r->n=nMap((number)a->Data(),coeffs_BIGINT,r->cf); break;
308  }
309  case NUMBER_CMD:
310  {
311  nMapFunc nMap=n_SetMap(currRing->cf,r->cf);
312  if (nMap!=NULL)
313  r->n=nMap((number)a->Data(),currRing->cf,r->cf);
314  else
315  bo=TRUE;
316  break;
317  }
318  case CNUMBER_CMD:
319  {
320  number2 a2=(number2)a->Data();
321  if (a2->cf==NULL) bo=TRUE;
322  else
323  {
324  nMapFunc nMap=n_SetMap(a2->cf,r->cf);
325  if (nMap!=NULL)
326  r->n=nMap(a2->n,a2->cf,r->cf);
327  else
328  bo=TRUE;
329  }
330  break;
331  }
332  default: bo=TRUE; break;
333  }
334  if (bo)
335  {
336  Werror("no conversion to Number from %s",Tok2Cmdname(a->Typ()));
337  omFreeSize(r,sizeof(*r));
338  }
339  else
340  res->data=(void*)r;
341  return bo;
342 }
343 
344 BOOLEAN jjN2_CR(leftv res, leftv a) // number2 ->cring
345 {
346  number2 n=(number2)a->Data();
347  n->cf->ref++;
348  res->data=(void*)n->cf;
349  return FALSE;
350 }
351 
352 BOOLEAN jjP2_R(leftv res, leftv a) // poly2 ->ring
353 {
354  poly2 n=(poly2)a->Data();
355  n->cf->ref++;
356  res->data=(void*)n->cf;
357  return FALSE;
358 }
359 
360 BOOLEAN jjCM_CR(leftv res, leftv a) // cmatrix ->cring
361 {
362  bigintmat *b=(bigintmat*)a->Data();
363  coeffs cf=b->basecoeffs();
364  if (cf!=NULL)
365  {
366  cf->ref++;
367  }
368  res->data=(void*)cf;
369  return FALSE;
370 }
371 
372 BOOLEAN jjCMATRIX_3(leftv res, leftv r, leftv c,leftv cf)
373 {
374  bigintmat *b=new bigintmat((int)(long)r->Data(),
375  (int)(long)c->Data(),
376  (coeffs)cf->Data());
377  res->data=(char*)b;
378  return FALSE;
379 }
380 
381 BOOLEAN jjN2_N(leftv res, leftv a) // number2 ->number
382 {
383  number2 n2=(number2)a->Data();
384  BOOLEAN bo=TRUE;
385  if (currRing!=NULL)
386  {
387  nMapFunc nMap=n_SetMap(n2->cf,currRing->cf);
388  if (nMap!=NULL)
389  {
390  res->data=(void*)nMap(n2->n,n2->cf,currRing->cf);
391  bo=FALSE;
392  }
393  }
394  return bo;
395 }
396 
397 // -----------------------------------------------------------
398 // operations with Number/number2
399 // -----------------------------------------------------------
400 number2 n2Copy(const number2 d)
401 {
402  number2 r=NULL;
403  if ((d!=NULL)&&(d->cf!=NULL))
404  {
405  r=(number2)omAlloc(sizeof(*r));
406  d->cf->ref++;
407  r->cf=d->cf;
408  if (d->cf!=NULL)
409  r->n=n_Copy(d->n,d->cf);
410  else
411  r->n=NULL;
412  }
413  return r;
414 }
415 void n2Delete(number2 &d)
416 {
417  if (d!=NULL)
418  {
419  if (d->cf!=NULL)
420  {
421  n_Delete(&d->n,d->cf);
422  nKillChar(d->cf);
423  }
424  omFreeSize(d,sizeof(*d));
425  d=NULL;
426  }
427 }
428 char *n2String(number2 d, BOOLEAN typed)
429 {
430  StringSetS("");
431  if ((d!=NULL) && (d->cf!=NULL))
432  {
433  if (typed) StringAppendS("Number(");
434  n_Write(d->n,d->cf);
435  if (typed) StringAppendS(")");
436  }
437  else StringAppendS("oo");
438  return StringEndS();
439 }
440 
441 void n2Print(number2 d)
442 {
443  char *s=n2String(d,FALSE);
444  PrintS(s);
445  omFree(s);
446 }
447 
448 // -----------------------------------------------------------
449 // operations with Poly/poly2
450 // -----------------------------------------------------------
451 
452 poly2 p2Copy(const poly2 d)
453 {
454  poly2 r=NULL;
455  if ((d!=NULL)&&(d->cf!=NULL))
456  {
457  r=(poly2)omAlloc(sizeof(*r));
458  d->cf->ref++;
459  r->cf=d->cf;
460  if (d->cf!=NULL)
461  r->n=p_Copy(d->n,d->cf);
462  else
463  r->n=NULL;
464  }
465  return r;
466 }
467 void p2Delete(poly2 &d)
468 {
469  if (d!=NULL)
470  {
471  if (d->cf!=NULL)
472  {
473  p_Delete(&d->n,d->cf);
474  rKill(d->cf);
475  }
476  omFreeSize(d,sizeof(*d));
477  d=NULL;
478  }
479 }
480 char *p2String(poly2 d, BOOLEAN typed)
481 {
482  StringSetS("");
483  if ((d!=NULL) && (d->cf!=NULL))
484  {
485  if (typed) StringAppendS("Poly(");
486  p_Write0(d->n,d->cf);
487  if (typed) StringAppendS(")");
488  }
489  else StringAppendS("oo");
490  return StringEndS();
491 }
492 
493 void p2Print(poly2 d)
494 {
495  char *s=p2String(d,FALSE);
496  PrintS(s);
497  omFree(s);
498 }
499 
500 // ---------------------------------------------------------------------
501 #include <coeffs/bigintmat.h>
502 BOOLEAN jjBIM2_CR(leftv res, leftv a) // bigintmat ->cring
503 {
504  bigintmat *b=(bigintmat*)a->Data();
505  coeffs cf=b->basecoeffs();
506  if (cf==NULL) return TRUE;
507  cf->ref++;
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
used for all transcendental extensions, i.e., the top-most extension in an extension tower is transce...
Definition: coeffs.h:39
const struct sConvertTypes dConvertTypes[]
Definition: table.h:1177
#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:1482
coeffs coeffs_BIGINT
Definition: ipid.cc:54
int Typ()
Definition: subexpr.cc:995
#define omAlloc(size)
Definition: omAllocDecl.h:210
poly p_Sub(poly p1, poly p2, const ring r)
Definition: p_polys.cc:1943
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:6056
#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
BOOLEAN jjR2_CR(leftv res, leftv a)
Definition: number2.cc:83
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:332
void * Data()
Definition: subexpr.cc:1137
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:224
int BOOLEAN
Definition: auxiliary.h:85
const poly b
Definition: syzextra.cc:213
void nKillChar(coeffs r)
undo all initialisations
Definition: numbers.cc:504
poly p_Power(poly p, int i, const ring r)
Definition: p_polys.cc:2150
void Werror(const char *fmt,...)
Definition: reporter.cc:189
void * CopyD(int t)
Definition: subexpr.cc:707
#define omAlloc0(size)
Definition: omAllocDecl.h:211
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:341
#define omStrDup(s)
Definition: omAllocDecl.h:263