source: git/kernel/shiftgb.cc @ 4d2ab5c

spielwiese
Last change on this file since 4d2ab5c was 4d2ab5c, checked in by Viktor Levandovskyy <levandov@…>, 16 years ago
*levandov: major update in freegb impl git-svn-id: file:///usr/local/Singular/svn/trunk@10602 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 9.6 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: shiftgb.cc,v 1.5 2008-02-23 20:12:53 levandov Exp $ */
5/*
6* ABSTRACT: kernel: utils for shift GB and free GB
7*/
8
9#include "mod2.h"
10
11#ifdef HAVE_PLURAL
12#include "febase.h"
13#include "ring.h"
14#include "polys.h"
15#include "numbers.h"
16#include "ideals.h"
17#include "matpol.h"
18#include "kbuckets.h"
19#include "kstd1.h"
20#include "sbuckets.h"
21#include "p_Mult_q.h"
22#include "kutil.h"
23#include "structs.h"
24#include "omalloc.h"
25#include "khstd.h"
26#include "kbuckets.h"
27#include "weight.h"
28#include "intvec.h"
29#include "structs.h"
30#include "kInline.cc"
31#include "stairc.h"
32#include "weight.h"
33#include "intvec.h"
34#include "timer.h"
35#include "shiftgb.h"
36#include "sca.h"
37
38
39#define freeT(A,v) omFreeSize((ADDRESS)A,(v+1)*sizeof(int))
40
41
42/* TODO: write p* stuff as instances of p_* for all the functions */
43
44poly p_LPshiftT(poly p, int sh, int uptodeg, int lV, kStrategy strat, const ring r)
45{
46  /* assume shift takes place, shifts the poly p by sh */
47  /* p is like TObject: lm in currRing = r, tail in tailRing  */
48
49  if (p==NULL) return(p);
50
51  assume(p_LmCheckIsFromRing(p,r));
52  assume(p_CheckIsFromRing(pNext(p),strat->tailRing));
53
54  /* assume sh and uptodeg agree */
55
56  if (sh == 0) return(p); /* the zero shift */
57
58  poly q   = NULL;
59  poly s   = p_mLPshift(p, sh, uptodeg, lV, r); // lm in currRing
60  poly pp = pNext(p);
61 
62  while (pp != NULL)
63  {
64    q = p_Add_q(q, p_mLPshift(pp,sh,uptodeg,lV,strat->tailRing),strat->tailRing);
65    pIter(pp);
66  }
67  pNext(s) = q;
68  /* int version: returns TRUE if it was successful */
69  return(s);
70}
71
72
73poly p_LPshift(poly p, int sh, int uptodeg, int lV, const ring r)
74{
75  /* assume shift takes place */
76  /* shifts the poly p by sh */
77  /* deletes p */
78
79  /* assume sh and uptodeg agree */
80
81  if (p==NULL) return(p);
82  if (sh == 0) return(p); /* the zero shift */
83
84  poly q  = NULL;
85  poly pp = p; // pCopy(p);
86  while (pp!=NULL)
87  {
88    q = p_Add_q(q, p_mLPshift(pp,sh,uptodeg,lV,r),r);
89    pIter(pp);
90  }
91  /* delete pp? */
92  /* int version: returns TRUE if it was successful */
93  return(q);
94}
95
96poly p_mLPshift(poly p, int sh, int uptodeg, int lV, const ring r)
97{
98  /* pm is a monomial */
99
100  if (sh == 0) return(p); /* the zero shift */
101
102  if (sh < 0 )
103  {
104#ifdef PDEBUG
105    Print("pmLPshift: negative shift requested");
106#endif
107    return(NULL); /* violation, 2check */
108  }
109
110  int L = p_mLastVblock(p,lV,r);
111  if (L+sh-1 > uptodeg)
112  {
113#ifdef PDEBUG
114    Print("p_mLPshift: too big shift requested");
115#endif
116    return(NULL); /* violation, 2check */
117  }
118  int *e=(int *)omAlloc0((r->N+1)*sizeof(int));
119  int *s=(int *)omAlloc0((r->N+1)*sizeof(int));
120  p_GetExpV(p,e,r);
121  number c = pGetCoeff(p);
122  int j;
123  //  for (j=1; j<=r->N; j++)
124  // L*lV gives the last position of the last block
125  for (j=1; j<= L*lV ; j++)
126  {
127    if (e[j]==1)
128    {
129      s[j + (sh*lV)] = e[j]; /* actually 1 */
130      omCheckAddr(s);
131    }
132    else 
133    {
134      if (e[j]!=0)
135      {
136#ifdef PDEBUG
137         Print("p_mLPshift: ex[%d]=%d\n",j,e[j]);
138#endif
139      }
140    }
141  }
142  poly m = p_ISet(1,r);
143  p_SetExpV(m,s,r);
144  /*  pSetm(m); */ /* done in the pSetExpV */
145  /* think on the component */
146  pSetCoeff0(m,c);
147  freeT(e, r->N);
148  freeT(s, r->N);
149  return(m);
150}
151
152poly pLPshift(poly p, int sh, int uptodeg, int lV)
153{
154  /* assume shift takes place */
155  /* shifts the poly p by sh */
156  /* deletes p */
157
158  /* assume sh and uptodeg agree */
159
160  if (sh == 0) return(p); /* the zero shift */
161
162  poly q  = NULL;
163  poly pp = p; // pCopy(p);
164  while (pp!=NULL)
165  {
166    q = p_Add_q(q, pmLPshift(pp,sh,uptodeg,lV),currRing);
167    pIter(pp);
168  }
169  /* delete pp? */
170  /* int version: returns TRUE if it was successful */
171  return(q);
172}
173
174poly pmLPshift(poly p, int sh, int uptodeg, int lV)
175{
176  /* TODO: use a shortcut with p_ version */
177  /* pm is a monomial */
178
179  if (sh == 0) return(p); /* the zero shift */
180
181  if (sh < 0 )
182  {
183#ifdef PDEBUG
184    Print("pmLPshift: negative shift requested");
185#endif
186    return(NULL); /* violation, 2check */
187  }
188
189  int L = pmLastVblock(p,lV);
190  if (L+sh-1 > uptodeg)
191  {
192#ifdef PDEBUG
193    Print("pmLPshift: too big shift requested");
194#endif
195    return(NULL); /* violation, 2check */
196  }
197  int *e=(int *)omAlloc0((currRing->N+1)*sizeof(int));
198  int *s=(int *)omAlloc0((currRing->N+1)*sizeof(int));
199  pGetExpV(p,e);
200  number c = pGetCoeff(p);
201  int j;
202  for (j=1; j<=currRing->N; j++)
203  {
204    if (e[j])
205    {
206      s[j + (sh*lV)] = e[j]; /* actually 1 */
207    }
208  }
209  poly m = pOne();
210  pSetExpV(m,s);
211  /*  pSetm(m); */ /* done in the pSetExpV */
212  /* think on the component */
213  pSetCoeff0(m,c);
214  freeT(e, currRing->N);
215  freeT(s, currRing->N);
216  return(m);
217}
218
219int pLastVblock(poly p, int lV)
220{
221  /* returns the number of maximal block */
222  /* appearing among the monomials of p */
223  /* the 0th block is the 1st one */
224  poly q = p; //p_Copy(p,currRing); /* need it ? */
225  int ans = 0; 
226  int ansnew = 0;
227  while (q!=NULL)
228  {
229    ansnew = pmLastVblock(q,lV);
230    ans    = si_max(ans,ansnew);
231    pIter(q);
232  }
233  /* do not need to delete q */
234  return(ans);
235}
236
237int pmLastVblock(poly p, int lV)
238{
239  /* for a monomial p, returns the number of the last block */
240  /* where a nonzero exponent is sitting */
241  if (pIsConstantPoly(p))
242  {
243    return(int(0));
244  }
245  int *e=(int *)omAlloc0((currRing->N+1)*sizeof(int));
246  pGetExpV(p,e);
247  int j,b;
248  j = currRing->N;
249  while ( (!e[j]) && (j>=1) ) j--;
250  if (j==0) 
251  {
252#ifdef PDEBUG
253    Print("pmLastVblock: unexpected zero exponent vector");
254    PrintLn();
255#endif   
256    return(j);
257  }
258  b = (int)(j/lV) + 1; /* the number of the block, >=1 */
259  return (b);
260}
261
262int p_LastVblockT(poly p, int lV, kStrategy strat, const ring r)
263{
264  /* returns the number of maximal block */
265  /* appearing among the monomials of p */
266  /* the 0th block is the 1st one */
267
268  /* p is like TObject: lm in currRing = r, tail in tailRing  */
269  assume(p_LmCheckIsFromRing(p,r));
270  assume(p_CheckIsFromRing(pNext(p),strat->tailRing));
271
272  int ans = p_mLastVblock(p, lV, r); // Block of LM
273  poly q = pNext(p); 
274  int ansnew = 0;
275  while (q != NULL)
276  {
277    ansnew = p_mLastVblock(q, lV, strat->tailRing);
278    ans       = si_max(ans,ansnew);
279    pIter(q);
280  }
281  /* do not need to delete q */
282  return(ans);
283}
284
285int p_LastVblock(poly p, int lV, const ring r)
286{
287  /* returns the number of maximal block */
288  /* appearing among the monomials of p */
289  /* the 0th block is the 1st one */
290  poly q = p; //p_Copy(p,currRing); /* need it ? */
291  int ans = 0; 
292  int ansnew = 0;
293  while (q!=NULL)
294  {
295    ansnew = p_mLastVblock(q, lV, r);
296    ans    = si_max(ans,ansnew);
297    pIter(q);
298  }
299  /* do not need to delete q */
300  return(ans);
301}
302
303int p_mLastVblock(poly p, int lV, const ring r)
304{
305  /* for a monomial p, returns the number of the last block */
306  /* where a nonzero exponent is sitting */
307  if (p_LmIsConstant(p,r))
308  {
309    return(0);
310  }
311  int *e=(int *)omAlloc0((r->N+1)*sizeof(int));
312  p_GetExpV(p,e,r);
313  int j,b;
314  j = r->N;
315  while ( (!e[j]) && (j>=1) ) j--;
316  if (j==0) 
317  {
318#ifdef PDEBUG
319    Print("pmLastVblock: unexpected zero exponent vector");
320    PrintLn();
321#endif   
322    return(j);
323  }
324  b = (int)((j+lV-1)/lV); /* the number of the block, >=1 */
325  freeT(e,r->N);
326  return (b);
327}
328
329int pFirstVblock(poly p, int lV)
330{
331  /* returns the number of maximal block */
332  /* appearing among the monomials of p */
333  /* the 0th block is the 1st one */
334  poly q = p; //p_Copy(p,currRing); /* need it ? */
335  int ans = 0; 
336  int ansnew = 0;
337  while (q!=NULL)
338  {
339    ansnew = pmFirstVblock(q,lV);
340    ans    = si_min(ans,ansnew);
341    pIter(q);
342  }
343  /* do not need to delete q */
344  return(ans);
345}
346
347int pmFirstVblock(poly p, int lV)
348{
349  if (pIsConstantPoly(p))
350  {
351    return(int(0));
352  }
353  /* for a monomial p, returns the number of the first block */
354  /* where a nonzero exponent is sitting */
355  int *e=(int *)omAlloc0((currRing->N+1)*sizeof(int));
356  pGetExpV(p,e);
357  int j,b;
358  j = 1;
359  while ( (!e[j]) && (j<=currRing->N-1) ) j++;
360  if (j==currRing->N + 1) 
361  {
362#ifdef PDEBUG
363    Print("pmFirstVblock: unexpected zero exponent vector");
364    PrintLn();
365#endif   
366    return(j);
367  }
368  b = (int)(j/lV)+1; /* the number of the block, 1<= N <= currRing->N  */
369  return (b);
370}
371
372
373int isInV(poly p, int lV)
374{
375  if (lV <= 0) return(0);
376  /* returns 1 iff p is in V */
377  /* that is in each block up to a certain one there is only one nonzero exponent */
378  /* lV = the length of V = the number of orig vars */
379  int *e = (int *)omAlloc0((currRing->N+1)*sizeof(int));
380  int  b = (int)(currRing->N)/lV; /* the number of blocks */
381  int *B = (int *)omAlloc0((b+1)*sizeof(int)); /* the num of elements in a block */
382  pGetExpV(p,e);
383  int i,j;
384  for (j=1; j<=b; j++)
385  {
386    /* we go through all the vars */
387    /* by blocks in lV vars */
388    for (i=(j-1)*lV + 1; i<= j*lV; i++)
389    {
390      if (e[i]) B[j] = B[j]+1;
391    }
392  }
393  j = b;
394  //  while ( (!B[j]) && (j>=1)) j--;
395  for (j=b; j>=1; j--)
396  {
397    if (B[j]!=0) break;
398  }
399
400  if (j==0)
401  {
402    /* it is a zero exp vector, which is in V */
403    return(1);
404  }
405  /* now B[j] != 0 */
406  for (j; j>=1; j--)
407  {
408    if (B[j]!=1)
409    {
410      return(0);
411    }
412  }
413  return(1);
414}
415
416int itoInsert(poly p, int uptodeg, int lV, const ring r)
417{
418  /* for poly in lmCR/tailTR presentation */
419  /* the below situation might happen! */
420//   if (r == currRing)
421//   {
422//     "Current ring is not expected in toInsert";
423//     return(0);
424//   }
425  /* compute the number of insertions */
426  int i = p_mLastVblock(p, lV, currRing);
427  if (pNext(p) != NULL)
428  {
429    i = si_max(i, p_LastVblock(pNext(p), lV, r) );
430  }
431  //  i = uptodeg  - i +1;
432  i = uptodeg  - i; 
433  p_wrp(p,currRing,r); Print("----i:%d",i); PrintLn();
434  return(i);
435}
436
437
438/* shiftgb stuff */
439
440
441/* remarks: cleanT : just deletion
442enlargeT: just reallocation */
443
444#endif
Note: See TracBrowser for help on using the repository browser.