source: git/Singular/pcv.cc @ 5476e83

spielwiese
Last change on this file since 5476e83 was a4b31c, checked in by Hans Schoenemann <hannes@…>, 7 years ago
use include ".." for singular related .h, p4
  • Property mode set to 100644
File size: 7.9 KB
Line 
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
25static int pcvMaxDegree;
26static int pcvTableSize;
27static int pcvIndexSize;
28static unsigned* pcvTable=NULL;
29static unsigned** pcvIndex=NULL;
30
31lists pcvLAddL(lists l1,lists l2)
32{
33  lists l0=(lists)omAllocBin(slists_bin);
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
56lists pcvPMulL(poly p,lists l1)
57{
58  lists l0=(lists)omAllocBin(slists_bin);
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
71BOOLEAN pcvLAddL(leftv res,leftv h)
72{
73  const 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
86BOOLEAN pcvPMulL(leftv res,leftv h)
87{
88  const 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
101int pcvDeg(poly p)
102{
103  int d=0;
104  for(int i=currRing->N;i>=1;i--) d+=pGetExp(p,i);
105  return d;
106}
107
108int pcvMinDeg(poly p)
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
122int pcvMinDeg(matrix m)
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
137BOOLEAN pcvMinDeg(leftv res,leftv h)
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
159void 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
189void pcvClean()
190{
191  if(pcvTable)
192  {
193    omFreeSize(pcvTable,pcvTableSize);
194    pcvTable=NULL;
195  }
196  if(pcvIndex)
197  {
198    omFreeSize(pcvIndex,pcvIndexSize);
199    pcvIndex=NULL;
200  }
201}
202
203int pcvM2N(poly m)
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
220poly 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
246poly 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
263poly 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
283lists pcvP2CV(lists pl,int d0,int d1)
284{
285  lists cvl=(lists)omAllocBin(slists_bin);
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
300lists pcvCV2P(lists cvl,int d0,int d1)
301{
302  lists pl=(lists)omAllocBin(slists_bin);
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
317BOOLEAN pcvP2CV(leftv res,leftv h)
318{
319  if(currRing)
320  {
321    const 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
339BOOLEAN pcvCV2P(leftv res,leftv h)
340{
341  if(currRing)
342  {
343    const 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
361int 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
371BOOLEAN pcvDim(leftv res,leftv h)
372{
373  if(currRing)
374  {
375    const 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
391int 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
411lists pcvBasis(int d0,int d1)
412{
413  if(d0<0) d0=0;
414  if(d1<0) d1=0;
415  lists b=(lists)omAllocBin(slists_bin);
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
424BOOLEAN pcvBasis(leftv res,leftv h)
425{
426  if(currRing)
427  {
428    const 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 */
Note: See TracBrowser for help on using the repository browser.