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