source: git/Singular/pcv.cc @ dd668f

fieker-DuValspielwiese
Last change on this file since dd668f was 762407, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
config.h is for sources files only FIX: config.h should only be used by source (not from inside kernel/mod2.h!) NOTE: each source file should better include mod2.h right after config.h, while headers should better not include mod2.h.
  • Property mode set to 100644
File size: 8.4 KB
RevLine 
[b7e7b6]1/*****************************************
2*  Computer Algebra System SINGULAR      *
3*****************************************/
[341696]4/* $Id$ */
[b7e7b6]5/*
[ab1f18]6* ABSTRACT: conversion between polys and coef vectors
[b7e7b6]7*/
8
[762407]9#include "config.h"
[b1dfaf]10#include <kernel/mod2.h>
[4d9dc5]11
[740e1f7]12#ifdef HAVE_PCV
[79102a]13//#if !defined(HAVE_DYNAMIC_LOADING) || defined(BUILD_MODULE)
[740e1f7]14
[599326]15#include <Singular/tok.h>
16#include <Singular/ipid.h>
[0fb34ba]17#include <coeffs/numbers.h>
[737a68]18#include <kernel/polys.h>
[599326]19#include <kernel/ideals.h>
20#include <Singular/lists.h>
[0fb34ba]21#include <polys/matpol.h>
[599326]22#include <kernel/febase.h>
23#include <Singular/pcv.h>
[b7e7b6]24
[4d9dc5]25static int pcvMaxDegree;
[b7e7b6]26static int pcvTableSize;
27static int pcvIndexSize;
28static unsigned* pcvTable=NULL;
29static unsigned** pcvIndex=NULL;
30
[b6484c]31lists pcvLAddL(lists l1,lists l2)
[4d9dc5]32{
[c232af]33  lists l0=(lists)omAllocBin(slists_bin);
[b6484c]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;
[32820d]42      l0->m[i].data=pCopy((poly)l1->m[i].data);
[b6484c]43      if(i<=l2->nr&&l2->m[i].rtyp==l1->m[i].rtyp)
[e858e7]44        l0->m[i].data=pAdd((poly)l0->m[i].data,pCopy((poly)l2->m[i].data));
[b6484c]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;
[32820d]50      l0->m[i].data=pCopy((poly)l2->m[i].data);
[b6484c]51    }
52  }
53  return(l0);
[4d9dc5]54}
[740e1f7]55
[b6484c]56lists pcvPMulL(poly p,lists l1)
[740e1f7]57{
[c232af]58  lists l0=(lists)omAllocBin(slists_bin);
[b6484c]59  l0->Init(l1->nr+1);
60  for(int i=l1->nr;i>=0;i--)
[740e1f7]61  {
[b6484c]62    if(l1->m[i].rtyp==POLY_CMD)
63    {
64      l0->m[i].rtyp=POLY_CMD;
[a6a239]65      l0->m[i].data=ppMult_qq(p,(poly)l1->m[i].data);
[b6484c]66    }
[740e1f7]67  }
[b6484c]68  return(l0);
[740e1f7]69}
70
[b6484c]71BOOLEAN pcvLAddL(leftv res,leftv h)
[740e1f7]72{
[b6484c]73  if(h&&h->Typ()==LIST_CMD)
[740e1f7]74  {
[b6484c]75    lists l1=(lists)h->Data();
76    h=h->next;
77    if(h&&h->Typ()==LIST_CMD)
[740e1f7]78    {
[b6484c]79      lists l2=(lists)h->Data();
80      res->rtyp=LIST_CMD;
81      res->data=(void*)pcvLAddL(l1,l2);
82      return FALSE;
[740e1f7]83    }
84  }
[b6484c]85  WerrorS("<list>,<list> expected");
86  return TRUE;
[740e1f7]87}
88
[b6484c]89BOOLEAN pcvPMulL(leftv res,leftv h)
90{
91  if(h&&h->Typ()==POLY_CMD)
92  {
93    poly p=(poly)h->Data();
94    h=h->next;
95    if(h&&h->Typ()==LIST_CMD)
96    {
97      lists l=(lists)h->Data();
98      res->rtyp=LIST_CMD;
99      res->data=(void*)pcvPMulL(p,l);
100      return FALSE;
101    }
102  }
103  WerrorS("<poly>,<list> expected");
104  return TRUE;
105}
106
107int pcvDeg(poly p)
108{
109  int d=0;
[e5739a]110  for(int i=currRing->N;i>=1;i--) d+=pGetExp(p,i);
[b6484c]111  return d;
112}
113
114int pcvMinDeg(poly p)
[740e1f7]115{
[4d9dc5]116  if(!p) return -1;
117  int md=pcvDeg(p);
118  pIter(p);
119  while(p)
[740e1f7]120  {
[4d9dc5]121    int d=pcvDeg(p);
[b6484c]122    if(d<md) md=d;
[4d9dc5]123    pIter(p);
[740e1f7]124  }
[4d9dc5]125  return md;
[740e1f7]126}
127
[b6484c]128int pcvMinDeg(matrix m)
[740e1f7]129{
[4d9dc5]130  int i,j,d;
131  int md=-1;
132  for(i=1;i<=MATROWS(m);i++)
[740e1f7]133  {
[4d9dc5]134    for(j=1;j<=MATCOLS(m);j++)
[740e1f7]135    {
[4d9dc5]136      d=pcvMinDeg(MATELEM(m,i,j));
[b6484c]137      if((d>=0&&md>d)||md==-1) md=d;
[740e1f7]138    }
139  }
[4d9dc5]140  return(md);
[740e1f7]141}
142
[4d9dc5]143BOOLEAN pcvMinDeg(leftv res,leftv h)
[740e1f7]144{
[4d9dc5]145  if(h)
[740e1f7]146  {
[4d9dc5]147    if(h->Typ()==POLY_CMD)
[740e1f7]148    {
[4d9dc5]149      res->rtyp=INT_CMD;
150      res->data=(void*)pcvMinDeg((poly)h->Data());
151      return FALSE;
152    }
153    else
154    if(h->Typ()==MATRIX_CMD)
155    {
156      res->rtyp=INT_CMD;
[a3bc95e]157      res->data=(void*)pcvMinDeg((matrix)h->Data());
[4d9dc5]158      return FALSE;
[740e1f7]159    }
160  }
[b6484c]161  WerrorS("<poly> expected");
[740e1f7]162  return TRUE;
163}
164
[5bb2fe]165void pcvInit(int d)
166{
[307e0a]167  if(d<0) d=1;
168  pcvMaxDegree=d+1;
[e5739a]169  pcvTableSize=currRing->N*pcvMaxDegree*sizeof(unsigned);
[c232af]170  pcvTable=(unsigned*)omAlloc0(pcvTableSize);
[e5739a]171  pcvIndexSize=currRing->N*sizeof(unsigned*);
[c232af]172  pcvIndex=(unsigned**)omAlloc(pcvIndexSize);
[e5739a]173  for(int i=0;i<currRing->N;i++)
[4d9dc5]174    pcvIndex[i]=pcvTable+i*pcvMaxDegree;
175  for(int i=0;i<pcvMaxDegree;i++)
[5bb2fe]176    pcvIndex[0][i]=i;
[0e64fdd]177  unsigned k,l;
[e5739a]178  for(int i=1;i<currRing->N;i++)
[b7e7b6]179  {
[0e64fdd]180    k=0;
[4d9dc5]181    for(int j=0;j<pcvMaxDegree;j++)
[b7e7b6]182    {
[0e64fdd]183      l=pcvIndex[i-1][j];
184      if(l>unsigned(~0)-k)
[307e0a]185      {
186        j=pcvMaxDegree;
[e5739a]187        i=currRing->N;
[2f9ffd]188        WerrorS("unsigned overflow");
[307e0a]189      }
[0e64fdd]190      else pcvIndex[i][j]=k+=l;
[b7e7b6]191    }
192  }
193}
194
195void pcvClean()
196{
197  if(pcvTable)
198  {
[c232af]199    omFreeSize(pcvTable,pcvTableSize);
[b7e7b6]200    pcvTable=NULL;
201  }
202  if(pcvIndex)
203  {
[c232af]204    omFreeSize(pcvIndex,pcvIndexSize);
[b7e7b6]205    pcvIndex=NULL;
206  }
207}
208
[eb3c4de]209int pcvM2N(poly m)
[b7e7b6]210{
[0e64fdd]211  unsigned n=0,dn,d=0;
[e5739a]212  for(int i=0;i<currRing->N;i++)
[b7e7b6]213  {
214    d+=pGetExp(m,i+1);
[0e64fdd]215    dn=pcvIndex[i][d];
[e554162]216    if(dn>MAX_INT_VAL-n)
[0e64fdd]217    {
[e5739a]218      i=currRing->N;
[0e64fdd]219      WerrorS("component overflow");
220    }
221    else n+=dn;
[b7e7b6]222  }
223  return n+1;
224}
225
[eb3c4de]226poly pcvN2M(int n)
[b7e7b6]227{
228  n--;
229  poly m=pOne();
[458653]230  int i,j=0,k;
[e5739a]231  for(i=currRing->N-1;i>=0;i--)
[b7e7b6]232  {
233    k=j;
[e858e7]234    for(j=0; (j<pcvMaxDegree) && (pcvIndex[i][j]<=(unsigned)n); j++);
[b7e7b6]235    j--;
236    n-=pcvIndex[i][j];
[e5739a]237    if(i<currRing->N-1) pSetExp(m,i+2,k-j);
[b7e7b6]238  }
239  if(n==0)
240  {
241    pSetExp(m,1,j);
242    pSetm(m);
243    return m;
244  }
245  else
246  {
[fb82895]247    pLmDelete(&m);
[b7e7b6]248    return NULL;
249  }
250}
251
[7cbc5c]252poly pcvP2CV(poly p,int d0,int d1)
[b7e7b6]253{
[5bb2fe]254  poly cv=NULL;
[b7e7b6]255  while(p)
256  {
[5bb2fe]257    int d=pcvDeg(p);
[b7e7b6]258    if(d0<=d&&d<d1)
259    {
[0aed0d]260      poly c=pNSet(nCopy(pGetCoeff(p)));
[eb3c4de]261      pSetComp(c,pcvM2N(p));
[5bb2fe]262      cv=pAdd(cv,c);
[b7e7b6]263    }
264    pIter(p);
265  }
[5bb2fe]266  return cv;
[b7e7b6]267}
268
[7cbc5c]269poly pcvCV2P(poly cv,int d0,int d1)
[b7e7b6]270{
[5bb2fe]271  poly p=NULL;
272  while(cv)
[b7e7b6]273  {
[eb3c4de]274    poly m=pcvN2M(pGetComp(cv));
[5bb2fe]275    if(m)
[b7e7b6]276    {
[5bb2fe]277      int d=pcvDeg(m);
[b7e7b6]278      if(d0<=d&&d<d1)
279      {
[5bb2fe]280        pSetCoeff(m,nCopy(pGetCoeff(cv)));
281        p=pAdd(p,m);
[b7e7b6]282      }
283    }
[5bb2fe]284    pIter(cv);
[b7e7b6]285  }
[5bb2fe]286  return p;
[b7e7b6]287}
288
[7cbc5c]289lists pcvP2CV(lists pl,int d0,int d1)
[b7e7b6]290{
[c232af]291  lists cvl=(lists)omAllocBin(slists_bin);
[5bb2fe]292  cvl->Init(pl->nr+1);
293  pcvInit(d1);
294  for(int i=pl->nr;i>=0;i--)
295  {
296    if(pl->m[i].rtyp==POLY_CMD)
297    {
298      cvl->m[i].rtyp=VECTOR_CMD;
[7cbc5c]299      cvl->m[i].data=pcvP2CV((poly)pl->m[i].data,d0,d1);
[5bb2fe]300    }
301  }
[b7e7b6]302  pcvClean();
[5bb2fe]303  return cvl;
[b7e7b6]304}
305
[7cbc5c]306lists pcvCV2P(lists cvl,int d0,int d1)
[b7e7b6]307{
[c232af]308  lists pl=(lists)omAllocBin(slists_bin);
[5bb2fe]309  pl->Init(cvl->nr+1);
310  pcvInit(d1);
311  for(int i=cvl->nr;i>=0;i--)
[b7e7b6]312  {
[5bb2fe]313    if(cvl->m[i].rtyp==VECTOR_CMD)
[b7e7b6]314    {
[5bb2fe]315      pl->m[i].rtyp=POLY_CMD;
[7cbc5c]316      pl->m[i].data=pcvCV2P((poly)cvl->m[i].data,d0,d1);
[b7e7b6]317    }
318  }
[5bb2fe]319  pcvClean();
320  return pl;
[b7e7b6]321}
[19fc394]322
[4d9dc5]323BOOLEAN pcvP2CV(leftv res,leftv h)
324{
[7d58b6]325  if(currRing)
[4d9dc5]326  {
327    if(h&&h->Typ()==LIST_CMD)
328    {
[b6484c]329      lists p=(lists)h->Data();
[4d9dc5]330      h=h->next;
331      if(h&&h->Typ()==INT_CMD)
332      {
[7447d8]333        int d0=(int)(long)h->Data();
[4d9dc5]334        h=h->next;
335        if(h&&h->Typ()==INT_CMD)
336        {
[7447d8]337          int d1=(int)(long)h->Data();
[4d9dc5]338          res->rtyp=LIST_CMD;
[b6484c]339          res->data=(void*)pcvP2CV(p,d0,d1);
[4d9dc5]340          return FALSE;
341        }
342      }
343    }
344    WerrorS("<list>,<int>,<int> expected");
345    return TRUE;
346  }
347  WerrorS("no ring active");
348  return TRUE;
349}
350
351BOOLEAN pcvCV2P(leftv res,leftv h)
352{
[7d58b6]353  if(currRing)
[4d9dc5]354  {
355    if(h&&h->Typ()==LIST_CMD)
356    {
357      lists pl=(lists)h->Data();
358      h=h->next;
359      if(h&&h->Typ()==INT_CMD)
360      {
[7447d8]361        int d0=(int)(long)h->Data();
[4d9dc5]362        h=h->next;
363        if(h&&h->Typ()==INT_CMD)
364        {
[7447d8]365          int d1=(int)(long)h->Data();
[4d9dc5]366          res->rtyp=LIST_CMD;
[b6484c]367          res->data=(void*)pcvCV2P(pl,d0,d1);
[4d9dc5]368          return FALSE;
369        }
370      }
371    }
372    WerrorS("<list>,<int>,<int> expected");
373    return TRUE;
374  }
375  WerrorS("no ring active");
376  return TRUE;
377}
378
[5bb2fe]379int pcvDim(int d0,int d1)
[19fc394]380{
[5bb2fe]381  if(d0<0) d0=0;
382  if(d1<0) d1=0;
[307e0a]383  pcvInit(d1);
[e5739a]384  int d=pcvIndex[currRing->N-1][d1]-pcvIndex[currRing->N-1][d0];
[5bb2fe]385  pcvClean();
386  return d;
387}
388
[4d9dc5]389BOOLEAN pcvDim(leftv res,leftv h)
390{
[7d58b6]391  if(currRing)
[4d9dc5]392  {
393    if(h&&h->Typ()==INT_CMD)
394    {
[7447d8]395      int d0=(int)(long)h->Data();
[4d9dc5]396      h=h->next;
397      if(h&&h->Typ()==INT_CMD)
398      {
[7447d8]399        int d1=(int)(long)h->Data();
[4d9dc5]400        res->rtyp=INT_CMD;
401        res->data=(void*)pcvDim(d0,d1);
402        return FALSE;
403      }
404    }
405    WerrorS("<int>,<int> expected");
406    return TRUE;
407  }
408  WerrorS("no ring active");
409  return TRUE;
410}
411
[5bb2fe]412int pcvBasis(lists b,int i,poly m,int d,int n)
413{
[e5739a]414  if(n<currRing->N)
[8f9d57]415  {
[5bb2fe]416    for(int k=0,l=d;k<=l;k++,d--)
417    {
418      pSetExp(m,n,k);
419      i=pcvBasis(b,i,m,d,n+1);
420    }
[8f9d57]421  }
[5bb2fe]422  else
[19fc394]423  {
[5bb2fe]424    pSetExp(m,n,d);
425    pSetm(m);
426    b->m[i].rtyp=POLY_CMD;
427    b->m[i++].data=pCopy(m);
[19fc394]428  }
[5bb2fe]429  return i;
[19fc394]430}
431
[5bb2fe]432lists pcvBasis(int d0,int d1)
[19fc394]433{
[5bb2fe]434  if(d0<0) d0=0;
435  if(d1<0) d1=0;
[c232af]436  lists b=(lists)omAllocBin(slists_bin);
[5bb2fe]437  b->Init(pcvDim(d0,d1));
438  poly m=pOne();
439  for(int d=d0,i=0;d<d1;d++)
440    i=pcvBasis(b,i,m,d,1);
[fb82895]441  pLmDelete(&m);
[5bb2fe]442  return b;
443}
444
[4d9dc5]445BOOLEAN pcvBasis(leftv res,leftv h)
446{
[7d58b6]447  if(currRing)
[4d9dc5]448  {
449    if(h&&h->Typ()==INT_CMD)
450    {
[7447d8]451      int d0=(int)(long)h->Data();
[4d9dc5]452      h=h->next;
453      if(h&&h->Typ()==INT_CMD)
454      {
[7447d8]455        int d1=(int)(long)h->Data();
[4d9dc5]456        res->rtyp=LIST_CMD;
[b6484c]457        res->data=(void*)pcvBasis(d0,d1);
[4d9dc5]458        return FALSE;
459      }
460    }
461    WerrorS("<int>,<int> expected");
462    return TRUE;
463  }
464  WerrorS("no ring active");
465  return TRUE;
466}
467
[79102a]468//#endif /* !defined(HAVE_DYNAMIC_LOADING) || defined(BUILD_MODULE) */
[740e1f7]469#endif /* HAVE_PCV */
Note: See TracBrowser for help on using the repository browser.