source: git/Singular/pcv.cc @ dcd92d

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