source: git/Singular/pcv.cc @ 32820d

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