source: git/Singular/pcv.cc @ 6993c83

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