source: git/Singular/pcv.cc @ 06c0b3

spielwiese
Last change on this file since 06c0b3 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
Line 
1/*****************************************
2*  Computer Algebra System SINGULAR      *
3*****************************************/
4/* $Id$ */
5/*
6* ABSTRACT: conversion between polys and coef vectors
7*/
8
9#include "config.h"
10#include <kernel/mod2.h>
11
12#ifdef HAVE_PCV
13//#if !defined(HAVE_DYNAMIC_LOADING) || defined(BUILD_MODULE)
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 <kernel/febase.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  if(h&&h->Typ()==LIST_CMD)
74  {
75    lists l1=(lists)h->Data();
76    h=h->next;
77    if(h&&h->Typ()==LIST_CMD)
78    {
79      lists l2=(lists)h->Data();
80      res->rtyp=LIST_CMD;
81      res->data=(void*)pcvLAddL(l1,l2);
82      return FALSE;
83    }
84  }
85  WerrorS("<list>,<list> expected");
86  return TRUE;
87}
88
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;
110  for(int i=currRing->N;i>=1;i--) d+=pGetExp(p,i);
111  return d;
112}
113
114int pcvMinDeg(poly p)
115{
116  if(!p) return -1;
117  int md=pcvDeg(p);
118  pIter(p);
119  while(p)
120  {
121    int d=pcvDeg(p);
122    if(d<md) md=d;
123    pIter(p);
124  }
125  return md;
126}
127
128int pcvMinDeg(matrix m)
129{
130  int i,j,d;
131  int md=-1;
132  for(i=1;i<=MATROWS(m);i++)
133  {
134    for(j=1;j<=MATCOLS(m);j++)
135    {
136      d=pcvMinDeg(MATELEM(m,i,j));
137      if((d>=0&&md>d)||md==-1) md=d;
138    }
139  }
140  return(md);
141}
142
143BOOLEAN pcvMinDeg(leftv res,leftv h)
144{
145  if(h)
146  {
147    if(h->Typ()==POLY_CMD)
148    {
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;
157      res->data=(void*)pcvMinDeg((matrix)h->Data());
158      return FALSE;
159    }
160  }
161  WerrorS("<poly> expected");
162  return TRUE;
163}
164
165void pcvInit(int d)
166{
167  if(d<0) d=1;
168  pcvMaxDegree=d+1;
169  pcvTableSize=currRing->N*pcvMaxDegree*sizeof(unsigned);
170  pcvTable=(unsigned*)omAlloc0(pcvTableSize);
171  pcvIndexSize=currRing->N*sizeof(unsigned*);
172  pcvIndex=(unsigned**)omAlloc(pcvIndexSize);
173  for(int i=0;i<currRing->N;i++)
174    pcvIndex[i]=pcvTable+i*pcvMaxDegree;
175  for(int i=0;i<pcvMaxDegree;i++)
176    pcvIndex[0][i]=i;
177  unsigned k,l;
178  for(int i=1;i<currRing->N;i++)
179  {
180    k=0;
181    for(int j=0;j<pcvMaxDegree;j++)
182    {
183      l=pcvIndex[i-1][j];
184      if(l>unsigned(~0)-k)
185      {
186        j=pcvMaxDegree;
187        i=currRing->N;
188        WerrorS("unsigned overflow");
189      }
190      else pcvIndex[i][j]=k+=l;
191    }
192  }
193}
194
195void pcvClean()
196{
197  if(pcvTable)
198  {
199    omFreeSize(pcvTable,pcvTableSize);
200    pcvTable=NULL;
201  }
202  if(pcvIndex)
203  {
204    omFreeSize(pcvIndex,pcvIndexSize);
205    pcvIndex=NULL;
206  }
207}
208
209int pcvM2N(poly m)
210{
211  unsigned n=0,dn,d=0;
212  for(int i=0;i<currRing->N;i++)
213  {
214    d+=pGetExp(m,i+1);
215    dn=pcvIndex[i][d];
216    if(dn>MAX_INT_VAL-n)
217    {
218      i=currRing->N;
219      WerrorS("component overflow");
220    }
221    else n+=dn;
222  }
223  return n+1;
224}
225
226poly pcvN2M(int n)
227{
228  n--;
229  poly m=pOne();
230  int i,j=0,k;
231  for(i=currRing->N-1;i>=0;i--)
232  {
233    k=j;
234    for(j=0; (j<pcvMaxDegree) && (pcvIndex[i][j]<=(unsigned)n); j++);
235    j--;
236    n-=pcvIndex[i][j];
237    if(i<currRing->N-1) pSetExp(m,i+2,k-j);
238  }
239  if(n==0)
240  {
241    pSetExp(m,1,j);
242    pSetm(m);
243    return m;
244  }
245  else
246  {
247    pLmDelete(&m);
248    return NULL;
249  }
250}
251
252poly pcvP2CV(poly p,int d0,int d1)
253{
254  poly cv=NULL;
255  while(p)
256  {
257    int d=pcvDeg(p);
258    if(d0<=d&&d<d1)
259    {
260      poly c=pNSet(nCopy(pGetCoeff(p)));
261      pSetComp(c,pcvM2N(p));
262      cv=pAdd(cv,c);
263    }
264    pIter(p);
265  }
266  return cv;
267}
268
269poly pcvCV2P(poly cv,int d0,int d1)
270{
271  poly p=NULL;
272  while(cv)
273  {
274    poly m=pcvN2M(pGetComp(cv));
275    if(m)
276    {
277      int d=pcvDeg(m);
278      if(d0<=d&&d<d1)
279      {
280        pSetCoeff(m,nCopy(pGetCoeff(cv)));
281        p=pAdd(p,m);
282      }
283    }
284    pIter(cv);
285  }
286  return p;
287}
288
289lists pcvP2CV(lists pl,int d0,int d1)
290{
291  lists cvl=(lists)omAllocBin(slists_bin);
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;
299      cvl->m[i].data=pcvP2CV((poly)pl->m[i].data,d0,d1);
300    }
301  }
302  pcvClean();
303  return cvl;
304}
305
306lists pcvCV2P(lists cvl,int d0,int d1)
307{
308  lists pl=(lists)omAllocBin(slists_bin);
309  pl->Init(cvl->nr+1);
310  pcvInit(d1);
311  for(int i=cvl->nr;i>=0;i--)
312  {
313    if(cvl->m[i].rtyp==VECTOR_CMD)
314    {
315      pl->m[i].rtyp=POLY_CMD;
316      pl->m[i].data=pcvCV2P((poly)cvl->m[i].data,d0,d1);
317    }
318  }
319  pcvClean();
320  return pl;
321}
322
323BOOLEAN pcvP2CV(leftv res,leftv h)
324{
325  if(currRing)
326  {
327    if(h&&h->Typ()==LIST_CMD)
328    {
329      lists p=(lists)h->Data();
330      h=h->next;
331      if(h&&h->Typ()==INT_CMD)
332      {
333        int d0=(int)(long)h->Data();
334        h=h->next;
335        if(h&&h->Typ()==INT_CMD)
336        {
337          int d1=(int)(long)h->Data();
338          res->rtyp=LIST_CMD;
339          res->data=(void*)pcvP2CV(p,d0,d1);
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{
353  if(currRing)
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      {
361        int d0=(int)(long)h->Data();
362        h=h->next;
363        if(h&&h->Typ()==INT_CMD)
364        {
365          int d1=(int)(long)h->Data();
366          res->rtyp=LIST_CMD;
367          res->data=(void*)pcvCV2P(pl,d0,d1);
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
379int pcvDim(int d0,int d1)
380{
381  if(d0<0) d0=0;
382  if(d1<0) d1=0;
383  pcvInit(d1);
384  int d=pcvIndex[currRing->N-1][d1]-pcvIndex[currRing->N-1][d0];
385  pcvClean();
386  return d;
387}
388
389BOOLEAN pcvDim(leftv res,leftv h)
390{
391  if(currRing)
392  {
393    if(h&&h->Typ()==INT_CMD)
394    {
395      int d0=(int)(long)h->Data();
396      h=h->next;
397      if(h&&h->Typ()==INT_CMD)
398      {
399        int d1=(int)(long)h->Data();
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
412int pcvBasis(lists b,int i,poly m,int d,int n)
413{
414  if(n<currRing->N)
415  {
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    }
421  }
422  else
423  {
424    pSetExp(m,n,d);
425    pSetm(m);
426    b->m[i].rtyp=POLY_CMD;
427    b->m[i++].data=pCopy(m);
428  }
429  return i;
430}
431
432lists pcvBasis(int d0,int d1)
433{
434  if(d0<0) d0=0;
435  if(d1<0) d1=0;
436  lists b=(lists)omAllocBin(slists_bin);
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);
441  pLmDelete(&m);
442  return b;
443}
444
445BOOLEAN pcvBasis(leftv res,leftv h)
446{
447  if(currRing)
448  {
449    if(h&&h->Typ()==INT_CMD)
450    {
451      int d0=(int)(long)h->Data();
452      h=h->next;
453      if(h&&h->Typ()==INT_CMD)
454      {
455        int d1=(int)(long)h->Data();
456        res->rtyp=LIST_CMD;
457        res->data=(void*)pcvBasis(d0,d1);
458        return FALSE;
459      }
460    }
461    WerrorS("<int>,<int> expected");
462    return TRUE;
463  }
464  WerrorS("no ring active");
465  return TRUE;
466}
467
468//#endif /* !defined(HAVE_DYNAMIC_LOADING) || defined(BUILD_MODULE) */
469#endif /* HAVE_PCV */
Note: See TracBrowser for help on using the repository browser.