number2.cc
Go to the documentation of this file.
1 #include "kernel/mod2.h" // general settings/macros
2 
3 #ifdef SINGULAR_4_1
4 #include <reporter/reporter.h> // for Print, WerrorS
5 #include <coeffs/numbers.h> // nRegister, coeffs.h
6 #include <coeffs/rmodulon.h> // ZnmInfo
7 #include <coeffs/bigintmat.h> // bigintmat
8 #include <coeffs/longrat.h> // BIGINTs: nlGMP
9 #include <misc/prime.h> // IsPrime
10 #include <Singular/blackbox.h> // blackbox type
11 #include <Singular/ipshell.h> // IsPrime
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 
75 // -----------------------------------------------------------
76 // interpreter stuff for Number/number2
77 // -----------------------------------------------------------
79 {
80  int op=iiOp;
81  // binary operations for number2
82  number2 a2=NULL;
83  number aa=NULL;
84  number2 b2=NULL;
85  number bb=NULL;
86  if (a->Typ()==CNUMBER_CMD)
87  {
88  a2=(number2)a->Data();
89  aa=a2->n;
90  }
91  if (b->Typ()==CNUMBER_CMD)
92  {
93  b2=(number2)b->Data();
94  if ((a2!=NULL) && (a2->cf!=b2->cf))
95  {
96  WerrorS("Number not compatible");
97  return TRUE;
98  }
99  bb=b2->n;
100  }
101  number2 r=(number2)omAlloc(sizeof(*r));
102  if (a2!=NULL) r->cf=a2->cf;
103  else r->cf=b2->cf;
104  if (r->cf==NULL) op=0; // force error
105  else
106  if (a2==NULL)
107  {
108  if (a->Typ()==INT_CMD) aa=n_Init((long)a->Data(),r->cf);
109  else if (a->Typ()==BIGINT_CMD)
110  {
111  //aa=n_Init_bigint((number)a->Data(),coeffs_BIGINT,r->cf);
112  nMapFunc nMap=n_SetMap(coeffs_BIGINT,r->cf);
113  aa=nMap((number)a->Data(),coeffs_BIGINT,r->cf);
114  }
115  else op=0;
116  }
117  if ((b2==NULL) &&(op!='^') &&(op!=0))
118  {
119  if (b->Typ()==INT_CMD) bb=n_Init((long)b->Data(),r->cf);
120  else if (b->Typ()==BIGINT_CMD)
121  {
122  //bb=n_Init_bigint((number)b->Data(),coeffs_BIGINT,r->cf);
123  nMapFunc nMap=n_SetMap(coeffs_BIGINT,r->cf);
124  bb=nMap((number)b->Data(),coeffs_BIGINT,r->cf);
125  }
126  else op=0;
127  }
128  switch(op)
129  {
130  case '+': r->n=n_Add(aa,bb,r->cf);break;
131  case '-': r->n=n_Sub(aa,bb,r->cf);break;
132  case '*': r->n=n_Mult(aa,bb,r->cf);break;
133  case '/': r->n=n_Div(aa,bb,r->cf);break;
134  case '%': r->n=n_IntMod(aa,bb,r->cf);break;
135 
136  case '^': n_Power(aa,(int)(long)b->Data(),&(r->n),r->cf); break;
137 
138  default: Werror("unknown binary operation %s(%d)",Tok2Cmdname(op),op);
139  omFree(r);
140  return TRUE;
141  }
142  res->data=(void*)r;
143  r->cf->ref++;
144  return FALSE;
145 }
147 {
148  int op=iiOp;
149  // unary operations for number2
150  number2 a2=(number2)a->Data();
151  number2 r=(number2)omAlloc(sizeof(*r));
152  r->cf=a2->cf;
153  if (a2->cf==NULL) op=0; // force error
154  switch(op)
155  {
156  case '-': r->n=n_Copy(a2->n,a2->cf);r->n=n_InpNeg(r->n,a2->cf);break;
157  default: Werror("unknown unary operation %s(%d)",Tok2Cmdname(op),op);
158  omFree(r);
159  return TRUE;
160  }
161  res->data=(void*)r;
162  r->cf->ref++;
163  return FALSE;
164 }
165 
167 {
168  number2 r=(number2)omAlloc(sizeof(*r));
169  r->cf=(coeffs)b->CopyD();
170  BOOLEAN bo=FALSE;
171  switch(a->Typ())
172  {
173  case INT_CMD:
174  r->n=n_Init((long)a->Data(),r->cf); break;
175  case BIGINT_CMD:
176  {
177  nMapFunc nMap=n_SetMap(coeffs_BIGINT,r->cf);
178  r->n=nMap((number)a->Data(),coeffs_BIGINT,r->cf); break;
179  }
180  case NUMBER_CMD:
181  {
182  nMapFunc nMap=n_SetMap(currRing->cf,r->cf);
183  if (nMap!=NULL)
184  r->n=nMap((number)a->Data(),currRing->cf,r->cf);
185  else
186  bo=TRUE;
187  break;
188  }
189  case CNUMBER_CMD:
190  {
191  number2 a2=(number2)a->Data();
192  if (a2->cf==NULL) bo=TRUE;
193  else
194  {
195  nMapFunc nMap=n_SetMap(a2->cf,r->cf);
196  if (nMap!=NULL)
197  r->n=nMap(a2->n,a2->cf,r->cf);
198  else
199  bo=TRUE;
200  }
201  break;
202  }
203  default: bo=TRUE; break;
204  }
205  if (bo)
206  {
207  Werror("no conversion to Number from %s",Tok2Cmdname(a->Typ()));
208  omFreeSize(r,sizeof(*r));
209  }
210  else
211  res->data=(void*)r;
212  return bo;
213 }
214 
215 BOOLEAN jjN2_CR(leftv res, leftv a) // number2 ->cring
216 {
217  number2 n=(number2)a->Data();
218  n->cf->ref++;
219  res->data=(void*)n->cf;
220  return FALSE;
221 }
222 
223 BOOLEAN jjCM_CR(leftv res, leftv a) // cmatrix ->cring
224 {
225  bigintmat *b=(bigintmat*)a->Data();
226  coeffs cf=b->basecoeffs();
227  if (cf!=NULL)
228  {
229  cf->ref++;
230  }
231  res->data=(void*)cf;
232  return FALSE;
233 }
234 
236 {
237  bigintmat *b=new bigintmat((int)(long)r->Data(),
238  (int)(long)c->Data(),
239  (coeffs)cf->Data());
240  res->data=(char*)b;
241  return FALSE;
242 }
243 
244 BOOLEAN jjN2_N(leftv res, leftv a) // number2 ->number
245 {
246  number2 n2=(number2)a->Data();
247  BOOLEAN bo=TRUE;
248  if (currRing!=NULL)
249  {
250  nMapFunc nMap=n_SetMap(n2->cf,currRing->cf);
251  if (nMap!=NULL)
252  {
253  res->data=(void*)nMap(n2->n,n2->cf,currRing->cf);
254  bo=FALSE;
255  }
256  }
257  return bo;
258 }
259 
261 {
262  coeffs a2=(coeffs)a->Data();
263  coeffs b2=(coeffs)b->Data();
264  res->data=(void*)(long)(a2==b2);
265  return FALSE;
266 }
267 // -----------------------------------------------------------
268 // operations with Number/number2
269 // -----------------------------------------------------------
270 number2 n2Copy(const number2 d)
271 {
272  number2 r=NULL;
273  if ((d!=NULL)&&(d->cf!=NULL))
274  {
275  r=(number2)omAlloc(sizeof(*r));
276  d->cf->ref++;
277  r->cf=d->cf;
278  if (d->cf!=NULL)
279  r->n=n_Copy(d->n,d->cf);
280  else
281  r->n=NULL;
282  }
283  return r;
284 }
285 void n2Delete(number2 &d)
286 {
287  if (d!=NULL)
288  {
289  if (d->cf!=NULL)
290  {
291  n_Delete(&d->n,d->cf);
292  nKillChar(d->cf);
293  }
294  omFreeSize(d,sizeof(*d));
295  d=NULL;
296  }
297 }
298 char *n2String(number2 d, BOOLEAN typed)
299 {
300  StringSetS("");
301  if ((d!=NULL) && (d->cf!=NULL))
302  {
303  if (typed) StringAppendS("Number(");
304  n_Write(d->n,d->cf);
305  if (typed) StringAppendS(")");
306  }
307  else StringAppendS("oo");
308  return StringEndS();
309 }
310 
311 void n2Print(number2 d)
312 {
313  char *s=n2String(d,FALSE);
314  PrintS(s);
315  omFree(s);
316 }
317 
318 #include <coeffs/bigintmat.h>
319 BOOLEAN jjBIM2_CR(leftv res, leftv a) // bigintmat ->cring
320 {
321  bigintmat *b=(bigintmat*)a->Data();
322  coeffs cf=b->basecoeffs();
323  cf->ref++;
324  res->data=(void*)cf;
325  return FALSE;
326 }
327 
328 BOOLEAN jjR2_CR(leftv res, leftv a) // ring ->cring
329 {
330  ring r=(ring)a->Data();
331  coeffs cf=r->cf;
332  cf->ref++;
333  res->data=(void*)cf;
334  return FALSE;
335 }
336 
337 #endif
void n2Print(number2 d)
Definition: number2.cc:311
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
BOOLEAN jjNUMBER2_OP2(leftv res, leftv a, leftv b)
Definition: number2.cc:78
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
BOOLEAN jjN2_CR(leftv res, leftv a)
Definition: number2.cc:215
Class used for (list of) interpreter objects.
Definition: subexpr.h:84
const poly a
Definition: syzextra.cc:212
only used if HAVE_RINGS is defined
Definition: coeffs.h:44
Definition: tok.h:94
BOOLEAN jjCMATRIX_3(leftv res, leftv r, leftv c, leftv cf)
Definition: number2.cc:235
BOOLEAN jjCRING_Zm(leftv res, leftv a, leftv b)
Definition: number2.cc:58
#define FALSE
Definition: auxiliary.h:97
Definition: tok.h:38
Matrices of numbers.
Definition: bigintmat.h:51
void n2Delete(number2 &d)
Definition: number2.cc:285
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:101
BOOLEAN jjCM_CR(leftv res, leftv a)
Definition: number2.cc:223
BOOLEAN jjEQUAL_CR(leftv res, leftv a, leftv b)
Definition: number2.cc:260
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:1465
coeffs coeffs_BIGINT
Definition: ipid.cc:54
int Typ()
Definition: subexpr.cc:979
#define omAlloc(size)
Definition: omAllocDecl.h:210
char * crString(coeffs c)
Definition: number2.cc:17
void * data
Definition: subexpr.h:90
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
BOOLEAN jjN2_N(leftv res, leftv a)
Definition: number2.cc:244
#define omFree(addr)
Definition: omAllocDecl.h:261
BOOLEAN jjNUMBER2CR(leftv res, leftv a, leftv b)
Definition: number2.cc:166
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
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 char * nCoeffName(const coeffs cf)
Definition: coeffs.h:977
static FORCE_INLINE void n_Write(number &n, const coeffs r, const BOOLEAN bShortOut=TRUE)
Definition: coeffs.h:595
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
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:328
number2 n2Copy(const number2 d)
Definition: number2.cc:270
BOOLEAN jjNUMBER2_OP1(leftv res, leftv a)
Definition: number2.cc:146
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
const char * Tok2Cmdname(int tok)
Definition: gentable.cc:128
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 * Data()
Definition: subexpr.cc:1121
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete &#39;p&#39;
Definition: coeffs.h:459
int iiOp
Definition: iparith.cc:227
int BOOLEAN
Definition: auxiliary.h:88
const poly b
Definition: syzextra.cc:213
BOOLEAN jjBIM2_CR(leftv res, leftv a)
Definition: number2.cc:319
void nKillChar(coeffs r)
undo all initialisations
Definition: numbers.cc:490
void Werror(const char *fmt,...)
Definition: reporter.cc:189
void * CopyD(int t)
Definition: subexpr.cc:679
char * n2String(number2 d, BOOLEAN typed)
Definition: number2.cc:298
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:329
#define omStrDup(s)
Definition: omAllocDecl.h:263