pcv.cc
Go to the documentation of this file.
1 /*****************************************
2 * Computer Algebra System SINGULAR *
3 *****************************************/
4 /*
5 * ABSTRACT: conversion between polys and coef vectors
6 */
7 
8 
9 
10 
11 #include <kernel/mod2.h>
12 
13 #ifdef HAVE_PCV
14 
15 #include <Singular/tok.h>
16 #include <Singular/ipid.h>
17 #include <coeffs/numbers.h>
18 #include <kernel/polys.h>
19 #include <kernel/ideals.h>
20 #include <Singular/lists.h>
21 #include <polys/matpol.h>
22 #include <Singular/ipshell.h>
23 #include <Singular/pcv.h>
24 
25 static int pcvMaxDegree;
26 static int pcvTableSize;
27 static int pcvIndexSize;
28 static unsigned* pcvTable=NULL;
29 static unsigned** pcvIndex=NULL;
30 
32 {
34  int i=l1->nr;
35  if(l1->nr<l2->nr) i=l2->nr;
36  l0->Init(i+1);
37  for(;i>=0;i--)
38  {
39  if(i<=l1->nr&&(l1->m[i].rtyp==POLY_CMD||l1->m[i].rtyp==VECTOR_CMD))
40  {
41  l0->m[i].rtyp=l1->m[i].rtyp;
42  l0->m[i].data=pCopy((poly)l1->m[i].data);
43  if(i<=l2->nr&&l2->m[i].rtyp==l1->m[i].rtyp)
44  l0->m[i].data=pAdd((poly)l0->m[i].data,pCopy((poly)l2->m[i].data));
45  }
46  else
47  if(i<=l2->nr&&(l2->m[i].rtyp==POLY_CMD||l2->m[i].rtyp==VECTOR_CMD))
48  {
49  l0->m[i].rtyp=l2->m[i].rtyp;
50  l0->m[i].data=pCopy((poly)l2->m[i].data);
51  }
52  }
53  return(l0);
54 }
55 
57 {
59  l0->Init(l1->nr+1);
60  for(int i=l1->nr;i>=0;i--)
61  {
62  if(l1->m[i].rtyp==POLY_CMD)
63  {
64  l0->m[i].rtyp=POLY_CMD;
65  l0->m[i].data=ppMult_qq(p,(poly)l1->m[i].data);
66  }
67  }
68  return(l0);
69 }
70 
72 {
73  short t[]={2,LIST_CMD,LIST_CMD};
74  if (iiCheckTypes(h,t,1))
75  {
76  lists l1=(lists)h->Data();
77  h=h->next;
78  lists l2=(lists)h->Data();
79  res->rtyp=LIST_CMD;
80  res->data=(void*)pcvLAddL(l1,l2);
81  return FALSE;
82  }
83  return TRUE;
84 }
85 
87 {
88  short t[]={2,POLY_CMD,LIST_CMD};
89  if (iiCheckTypes(h,t,1))
90  {
91  poly p=(poly)h->Data();
92  h=h->next;
93  lists l=(lists)h->Data();
94  res->rtyp=LIST_CMD;
95  res->data=(void*)pcvPMulL(p,l);
96  return FALSE;
97  }
98  return TRUE;
99 }
100 
102 {
103  int d=0;
104  for(int i=currRing->N;i>=1;i--) d+=pGetExp(p,i);
105  return d;
106 }
107 
109 {
110  if(!p) return -1;
111  int md=pcvDeg(p);
112  pIter(p);
113  while(p)
114  {
115  int d=pcvDeg(p);
116  if(d<md) md=d;
117  pIter(p);
118  }
119  return md;
120 }
121 
123 {
124  int i,j,d;
125  int md=-1;
126  for(i=1;i<=MATROWS(m);i++)
127  {
128  for(j=1;j<=MATCOLS(m);j++)
129  {
130  d=pcvMinDeg(MATELEM(m,i,j));
131  if((d>=0&&md>d)||md==-1) md=d;
132  }
133  }
134  return(md);
135 }
136 
138 {
139  if(h)
140  {
141  if(h->Typ()==POLY_CMD)
142  {
143  res->rtyp=INT_CMD;
144  res->data=(void*)(long)pcvMinDeg((poly)h->Data());
145  return FALSE;
146  }
147  else
148  if(h->Typ()==MATRIX_CMD)
149  {
150  res->rtyp=INT_CMD;
151  res->data=(void*)(long)pcvMinDeg((matrix)h->Data());
152  return FALSE;
153  }
154  }
155  WerrorS("<poly> expected");
156  return TRUE;
157 }
158 
159 void pcvInit(int d)
160 {
161  if(d<0) d=1;
162  pcvMaxDegree=d+1;
163  pcvTableSize=currRing->N*pcvMaxDegree*sizeof(unsigned);
164  pcvTable=(unsigned*)omAlloc0(pcvTableSize);
165  pcvIndexSize=currRing->N*sizeof(unsigned*);
166  pcvIndex=(unsigned**)omAlloc(pcvIndexSize);
167  for(int i=0;i<currRing->N;i++)
168  pcvIndex[i]=pcvTable+i*pcvMaxDegree;
169  for(int i=0;i<pcvMaxDegree;i++)
170  pcvIndex[0][i]=i;
171  unsigned k,l;
172  for(int i=1;i<currRing->N;i++)
173  {
174  k=0;
175  for(int j=0;j<pcvMaxDegree;j++)
176  {
177  l=pcvIndex[i-1][j];
178  if(l>unsigned(~0)-k)
179  {
180  j=pcvMaxDegree;
181  i=currRing->N;
182  WerrorS("unsigned overflow");
183  }
184  else pcvIndex[i][j]=k+=l;
185  }
186  }
187 }
188 
189 void pcvClean()
190 {
191  if(pcvTable)
192  {
194  pcvTable=NULL;
195  }
196  if(pcvIndex)
197  {
199  pcvIndex=NULL;
200  }
201 }
202 
204 {
205  unsigned n=0,dn,d=0;
206  for(int i=0;i<currRing->N;i++)
207  {
208  d+=pGetExp(m,i+1);
209  dn=pcvIndex[i][d];
210  if(dn>MAX_INT_VAL-n)
211  {
212  i=currRing->N;
213  WerrorS("component overflow");
214  }
215  else n+=dn;
216  }
217  return n+1;
218 }
219 
220 poly pcvN2M(int n)
221 {
222  n--;
223  poly m=pOne();
224  int i,j=0,k;
225  for(i=currRing->N-1;i>=0;i--)
226  {
227  k=j;
228  for(j=0; (j<pcvMaxDegree) && (pcvIndex[i][j]<=(unsigned)n); j++);
229  j--;
230  n-=pcvIndex[i][j];
231  if(i<currRing->N-1) pSetExp(m,i+2,k-j);
232  }
233  if(n==0)
234  {
235  pSetExp(m,1,j);
236  pSetm(m);
237  return m;
238  }
239  else
240  {
241  pLmDelete(&m);
242  return NULL;
243  }
244 }
245 
246 poly pcvP2CV(poly p,int d0,int d1)
247 {
248  poly cv=NULL;
249  while(p)
250  {
251  int d=pcvDeg(p);
252  if(d0<=d&&d<d1)
253  {
254  poly c=pNSet(nCopy(pGetCoeff(p)));
255  pSetComp(c,pcvM2N(p));
256  cv=pAdd(cv,c);
257  }
258  pIter(p);
259  }
260  return cv;
261 }
262 
263 poly pcvCV2P(poly cv,int d0,int d1)
264 {
265  poly p=NULL;
266  while(cv)
267  {
268  poly m=pcvN2M(pGetComp(cv));
269  if(m)
270  {
271  int d=pcvDeg(m);
272  if(d0<=d&&d<d1)
273  {
274  pSetCoeff(m,nCopy(pGetCoeff(cv)));
275  p=pAdd(p,m);
276  }
277  }
278  pIter(cv);
279  }
280  return p;
281 }
282 
283 lists pcvP2CV(lists pl,int d0,int d1)
284 {
286  cvl->Init(pl->nr+1);
287  pcvInit(d1);
288  for(int i=pl->nr;i>=0;i--)
289  {
290  if(pl->m[i].rtyp==POLY_CMD)
291  {
292  cvl->m[i].rtyp=VECTOR_CMD;
293  cvl->m[i].data=pcvP2CV((poly)pl->m[i].data,d0,d1);
294  }
295  }
296  pcvClean();
297  return cvl;
298 }
299 
300 lists pcvCV2P(lists cvl,int d0,int d1)
301 {
303  pl->Init(cvl->nr+1);
304  pcvInit(d1);
305  for(int i=cvl->nr;i>=0;i--)
306  {
307  if(cvl->m[i].rtyp==VECTOR_CMD)
308  {
309  pl->m[i].rtyp=POLY_CMD;
310  pl->m[i].data=pcvCV2P((poly)cvl->m[i].data,d0,d1);
311  }
312  }
313  pcvClean();
314  return pl;
315 }
316 
318 {
319  if(currRing)
320  {
321  short t[]={3,LIST_CMD,INT_CMD,INT_CMD};
322  if (iiCheckTypes(h,t,1))
323  {
324  lists p=(lists)h->Data();
325  h=h->next;
326  int d0=(int)(long)h->Data();
327  h=h->next;
328  int d1=(int)(long)h->Data();
329  res->rtyp=LIST_CMD;
330  res->data=(void*)pcvP2CV(p,d0,d1);
331  return FALSE;
332  }
333  return TRUE;
334  }
335  WerrorS("no ring active");
336  return TRUE;
337 }
338 
340 {
341  if(currRing)
342  {
343  short t[]={3,LIST_CMD,INT_CMD,INT_CMD};
344  if (iiCheckTypes(h,t,1))
345  {
346  lists pl=(lists)h->Data();
347  h=h->next;
348  int d0=(int)(long)h->Data();
349  h=h->next;
350  int d1=(int)(long)h->Data();
351  res->rtyp=LIST_CMD;
352  res->data=(void*)pcvCV2P(pl,d0,d1);
353  return FALSE;
354  }
355  return TRUE;
356  }
357  WerrorS("no ring active");
358  return TRUE;
359 }
360 
361 int pcvDim(int d0,int d1)
362 {
363  if(d0<0) d0=0;
364  if(d1<0) d1=0;
365  pcvInit(d1);
366  int d=pcvIndex[currRing->N-1][d1]-pcvIndex[currRing->N-1][d0];
367  pcvClean();
368  return d;
369 }
370 
372 {
373  if(currRing)
374  {
375  short t[]={2,INT_CMD,INT_CMD};
376  if (iiCheckTypes(h,t,1))
377  {
378  int d0=(int)(long)h->Data();
379  h=h->next;
380  int d1=(int)(long)h->Data();
381  res->rtyp=INT_CMD;
382  res->data=(void*)(long)pcvDim(d0,d1);
383  return FALSE;
384  }
385  return TRUE;
386  }
387  WerrorS("no ring active");
388  return TRUE;
389 }
390 
391 int pcvBasis(lists b,int i,poly m,int d,int n)
392 {
393  if(n<currRing->N)
394  {
395  for(int k=0,l=d;k<=l;k++,d--)
396  {
397  pSetExp(m,n,k);
398  i=pcvBasis(b,i,m,d,n+1);
399  }
400  }
401  else
402  {
403  pSetExp(m,n,d);
404  pSetm(m);
405  b->m[i].rtyp=POLY_CMD;
406  b->m[i++].data=pCopy(m);
407  }
408  return i;
409 }
410 
411 lists pcvBasis(int d0,int d1)
412 {
413  if(d0<0) d0=0;
414  if(d1<0) d1=0;
416  b->Init(pcvDim(d0,d1));
417  poly m=pOne();
418  for(int d=d0,i=0;d<d1;d++)
419  i=pcvBasis(b,i,m,d,1);
420  pLmDelete(&m);
421  return b;
422 }
423 
425 {
426  if(currRing)
427  {
428  short t[]={2,INT_CMD,INT_CMD};
429  if (iiCheckTypes(h,t,1))
430  {
431  int d0=(int)(long)h->Data();
432  h=h->next;
433  int d1=(int)(long)h->Data();
434  res->rtyp=LIST_CMD;
435  res->data=(void*)pcvBasis(d0,d1);
436  return FALSE;
437  }
438  return TRUE;
439  }
440  WerrorS("no ring active");
441  return TRUE;
442 }
443 
444 #endif /* HAVE_PCV */
#define omAllocBin(bin)
Definition: omAllocDecl.h:205
int pcvM2N(poly m)
Definition: pcv.cc:203
#define ppMult_qq(p, q)
Definition: polys.h:179
sleftv * m
Definition: lists.h:45
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
#define pSetm(p)
Definition: polys.h:241
Definition: tok.h:85
#define pAdd(p, q)
Definition: polys.h:174
#define pNSet(n)
Definition: polys.h:284
Definition: lists.h:22
static unsigned * pcvTable
Definition: pcv.cc:28
#define pSetExp(p, i, v)
Definition: polys.h:42
#define FALSE
Definition: auxiliary.h:140
Compatiblity layer for legacy polynomial operations (over currRing)
return P p
Definition: myNF.cc:203
lists pcvPMulL(poly p, lists l1)
Definition: pcv.cc:56
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
#define pLmDelete(p)
assume p != NULL, deletes Lm(p)->coef and Lm(p)
Definition: polys.h:76
#define TRUE
Definition: auxiliary.h:144
void WerrorS(const char *s)
Definition: feFopen.cc:24
int k
Definition: cfEzgcd.cc:93
static unsigned ** pcvIndex
Definition: pcv.cc:29
static number & pGetCoeff(poly p)
return an alias to the leading coefficient of p assumes that p != NULL NOTE: not copy ...
Definition: monomials.h:51
int Typ()
Definition: subexpr.cc:969
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define pGetComp(p)
Component.
Definition: polys.h:37
void * data
Definition: subexpr.h:89
#define pIter(p)
Definition: monomials.h:44
poly res
Definition: myNF.cc:322
ring currRing
Widely used global variable which specifies the current polynomial ring for Singular interpreter and ...
Definition: polys.cc:12
#define pGetExp(p, i)
Exponent.
Definition: polys.h:41
int pcvDeg(poly p)
Definition: pcv.cc:101
const CanonicalForm CFMap CFMap & N
Definition: cfEzgcd.cc:49
int pcvDim(int d0, int d1)
Definition: pcv.cc:361
void pcvInit(int d)
Definition: pcv.cc:159
int j
Definition: myNF.cc:70
void pcvClean()
Definition: pcv.cc:189
const int MAX_INT_VAL
Definition: mylimits.h:12
#define pSetComp(p, v)
Definition: polys.h:38
int m
Definition: cfEzgcd.cc:119
poly pcvCV2P(poly cv, int d0, int d1)
Definition: pcv.cc:263
int i
Definition: cfEzgcd.cc:123
lists pcvLAddL(lists l1, lists l2)
Definition: pcv.cc:31
#define pOne()
Definition: polys.h:286
int pcvBasis(lists b, int i, poly m, int d, int n)
Definition: pcv.cc:391
static int pcvIndexSize
Definition: pcv.cc:27
leftv next
Definition: subexpr.h:87
INLINE_THIS void Init(int l=0)
Definition: lists.h:66
int nr
Definition: lists.h:43
#define MATCOLS(i)
Definition: matpol.h:28
#define NULL
Definition: omList.c:10
slists * lists
Definition: mpr_numeric.h:146
static int pcvMaxDegree
Definition: pcv.cc:25
BOOLEAN iiCheckTypes(leftv args, const short *type_list, int report)
check a list of arguemys against a given field of types return TRUE if the types match return FALSE (...
Definition: ipshell.cc:6434
poly pcvN2M(int n)
Definition: pcv.cc:220
int rtyp
Definition: subexpr.h:92
#define nCopy(n)
Definition: numbers.h:15
void * Data()
Definition: subexpr.cc:1111
poly pcvP2CV(poly p, int d0, int d1)
Definition: pcv.cc:246
Definition: tok.h:96
omBin slists_bin
Definition: lists.cc:23
static int pcvTableSize
Definition: pcv.cc:26
#define MATROWS(i)
Definition: matpol.h:27
polyrec * poly
Definition: hilb.h:10
static Poly * h
Definition: janet.cc:978
int BOOLEAN
Definition: auxiliary.h:131
const poly b
Definition: syzextra.cc:213
#define pSetCoeff(p, n)
deletes old coeff before setting the new one
Definition: polys.h:31
int pcvMinDeg(poly p)
Definition: pcv.cc:108
#define omAlloc0(size)
Definition: omAllocDecl.h:211
int l
Definition: cfEzgcd.cc:94
#define pCopy(p)
return a copy of the poly
Definition: polys.h:156
#define MATELEM(mat, i, j)
Definition: matpol.h:29