source: git/Singular/pcv.cc @ b7b08c

spielwiese
Last change on this file since b7b08c was b7b08c, checked in by Olaf Bachmann <obachman@…>, 25 years ago
* us short exponent vectors in divisbility tests * AllocSizeOf * garbage collection * scanner/gmp: allow .0 and 1. input of reals git-svn-id: file:///usr/local/Singular/svn/trunk@3711 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 8.3 KB
Line 
1/*****************************************
2*  Computer Algebra System SINGULAR      *
3*****************************************/
4/* $Id: pcv.cc,v 1.27 1999-10-14 14:27:27 obachman 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)AllocSizeOf(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((poly)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)AllocSizeOf(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  unsigned k,l;
177  for(int i=1;i<pVariables;i++)
178  {
179    k=0;
180    for(int j=0;j<pcvMaxDegree;j++)
181    {
182      l=pcvIndex[i-1][j];
183      if(l>unsigned(~0)-k)
184      {
185        j=pcvMaxDegree;
186        i=pVariables;
187        WerrorS("unsigned overflow");
188      }
189      else pcvIndex[i][j]=k+=l;
190    }
191  }
192}
193
194void pcvClean()
195{
196  if(pcvTable)
197  {
198    Free(pcvTable,pcvTableSize);
199    pcvTable=NULL;
200  }
201  if(pcvIndex)
202  {
203    Free(pcvIndex,pcvIndexSize);
204    pcvIndex=NULL;
205  }
206}
207
208int pcvM2N(poly m)
209{
210  unsigned n=0,dn,d=0;
211  for(int i=0;i<pVariables;i++)
212  {
213    d+=pGetExp(m,i+1);
214    dn=pcvIndex[i][d];
215    if(dn>MAX_COMPONENT-n)
216    {
217      i=pVariables;
218      WerrorS("component overflow");
219    }
220    else n+=dn;
221  }
222  return n+1;
223}
224
225poly pcvN2M(int n)
226{
227  n--;
228  poly m=pOne();
229  int i,j,k;
230  for(i=pVariables-1;i>=0;i--)
231  {
232    k=j;
233    for(j=0; (j<pcvMaxDegree) && (pcvIndex[i][j]<=(unsigned)n); j++);
234    j--;
235    n-=pcvIndex[i][j];
236    if(i<pVariables-1) pSetExp(m,i+2,k-j);
237  }
238  if(n==0)
239  {
240    pSetExp(m,1,j);
241    pSetm(m);
242    return m;
243  }
244  else
245  {
246    pDelete1(&m);
247    return NULL;
248  }
249}
250
251poly pcvP2CV(poly p,int d0,int d1)
252{
253  poly cv=NULL;
254  while(p)
255  {
256    int d=pcvDeg(p);
257    if(d0<=d&&d<d1)
258    {
259      poly c=pOne();
260      pSetComp(c,pcvM2N(p));
261      pSetCoeff(c,nCopy(pGetCoeff(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)AllocSizeOf(slists);
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)AllocSizeOf(slists);
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(currRingHdl)
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)h->Data();
334        h=h->next;
335        if(h&&h->Typ()==INT_CMD)
336        {
337          int d1=(int)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(currRingHdl)
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)h->Data();
362        h=h->next;
363        if(h&&h->Typ()==INT_CMD)
364        {
365          int d1=(int)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[pVariables-1][d1]-pcvIndex[pVariables-1][d0];
385  pcvClean();
386  return d;
387}
388
389BOOLEAN pcvDim(leftv res,leftv h)
390{
391  if(currRingHdl)
392  {
393    if(h&&h->Typ()==INT_CMD)
394    {
395      int d0=(int)h->Data();
396      h=h->next;
397      if(h&&h->Typ()==INT_CMD)
398      {
399        int d1=(int)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<pVariables)
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)AllocSizeOf(slists);
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  pDelete1(&m);
442  return b;
443}
444
445BOOLEAN pcvBasis(leftv res,leftv h)
446{
447  if(currRingHdl)
448  {
449    if(h&&h->Typ()==INT_CMD)
450    {
451      int d0=(int)h->Data();
452      h=h->next;
453      if(h&&h->Typ()==INT_CMD)
454      {
455        int d1=(int)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.