source: git/kernel/shiftgb.cc @ dabe365

spielwiese
Last change on this file since dabe365 was dabe365, checked in by Viktor Levandovskyy <levandov@…>, 16 years ago
*levandov: shiftbba prepared for release git-svn-id: file:///usr/local/Singular/svn/trunk@10968 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 13.4 KB
RevLine 
[3a67ea7]1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
[dabe365]4/* $Id: shiftgb.cc,v 1.10 2008-08-07 18:08:37 levandov Exp $ */
[3a67ea7]5/*
6* ABSTRACT: kernel: utils for shift GB and free GB
7*/
8
[cb0fbe]9#include "mod2.h"
[07625cb]10
[037df4]11#ifdef HAVE_SHIFTBBA
[cb0fbe]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))
[3a67ea7]40
[4d2ab5c]41
42/* TODO: write p* stuff as instances of p_* for all the functions */
[ad1c3b]43/* p_* functions are new, p* are old */
[4d2ab5c]44
45poly p_LPshiftT(poly p, int sh, int uptodeg, int lV, kStrategy strat, const ring r)
46{
47  /* assume shift takes place, shifts the poly p by sh */
48  /* p is like TObject: lm in currRing = r, tail in tailRing  */
49
50  if (p==NULL) return(p);
51
52  assume(p_LmCheckIsFromRing(p,r));
53  assume(p_CheckIsFromRing(pNext(p),strat->tailRing));
54
[ad1c3b]55  /* assume sh and uptodeg agree  TODO check */
[4d2ab5c]56
57  if (sh == 0) return(p); /* the zero shift */
58
59  poly q   = NULL;
60  poly s   = p_mLPshift(p, sh, uptodeg, lV, r); // lm in currRing
61  poly pp = pNext(p);
62 
63  while (pp != NULL)
64  {
65    q = p_Add_q(q, p_mLPshift(pp,sh,uptodeg,lV,strat->tailRing),strat->tailRing);
66    pIter(pp);
67  }
68  pNext(s) = q;
69  /* int version: returns TRUE if it was successful */
70  return(s);
71}
72
73
74poly p_LPshift(poly p, int sh, int uptodeg, int lV, const ring r)
75{
76  /* assume shift takes place */
[ad1c3b]77  /* shifts the poly p from the ring r by sh */
[4d2ab5c]78
[ad1c3b]79  /* assume sh and uptodeg agree TODO check */
[4d2ab5c]80
81  if (p==NULL) return(p);
82  if (sh == 0) return(p); /* the zero shift */
83
84  poly q  = NULL;
[ad1c3b]85  poly pp = p; // do not take copies
[4d2ab5c]86  while (pp!=NULL)
87  {
88    q = p_Add_q(q, p_mLPshift(pp,sh,uptodeg,lV,r),r);
89    pIter(pp);
90  }
91  return(q);
92}
93
94poly p_mLPshift(poly p, int sh, int uptodeg, int lV, const ring r)
95{
[ad1c3b]96  /* p is a monomial from the ring r */
[4d2ab5c]97
98  if (sh == 0) return(p); /* the zero shift */
99
100  if (sh < 0 )
101  {
102#ifdef PDEBUG
103    Print("pmLPshift: negative shift requested");
104#endif
105    return(NULL); /* violation, 2check */
106  }
107
108  int L = p_mLastVblock(p,lV,r);
109  if (L+sh-1 > uptodeg)
110  {
111#ifdef PDEBUG
112    Print("p_mLPshift: too big shift requested");
113#endif
114    return(NULL); /* violation, 2check */
115  }
116  int *e=(int *)omAlloc0((r->N+1)*sizeof(int));
117  int *s=(int *)omAlloc0((r->N+1)*sizeof(int));
118  p_GetExpV(p,e,r);
[ad1c3b]119
[4d2ab5c]120  int j;
121  //  for (j=1; j<=r->N; j++)
122  // L*lV gives the last position of the last block
123  for (j=1; j<= L*lV ; j++)
124  {
125    if (e[j]==1)
126    {
127      s[j + (sh*lV)] = e[j]; /* actually 1 */
[ad1c3b]128#ifdef PDEBUG
[4d2ab5c]129      omCheckAddr(s);
[ad1c3b]130#endif
[4d2ab5c]131    }
[ad1c3b]132#ifdef PDEBUG
[4d2ab5c]133    else 
134    {
135      if (e[j]!=0)
136      {
[ad1c3b]137        //         Print("p_mLPshift: ex[%d]=%d\n",j,e[j]);
[4d2ab5c]138      }
139    }
[ad1c3b]140#endif
[4d2ab5c]141  }
[b902246]142  poly m = p_One(r);
[4d2ab5c]143  p_SetExpV(m,s,r);
144  freeT(e, r->N);
145  freeT(s, r->N);
[ad1c3b]146  /*  pSetm(m); */ /* done in the pSetExpV */
147  /* think on the component and coefficient */
148  //  number c = pGetCoeff(p);
149  //  p_SetCoeff0(m,p_GetCoeff(p,r),r);
150  p_SetComp(m,p_GetComp(p,r),r); // component is preserved
151  p_SetCoeff0(m,p_GetCoeff(p,r),r);  // coeff is preserved
[4d2ab5c]152  return(m);
153}
154
[3a67ea7]155poly pLPshift(poly p, int sh, int uptodeg, int lV)
156{
157  /* assume shift takes place */
[cb0fbe]158  /* shifts the poly p by sh */
[37a4c3]159  /* deletes p */
[cb0fbe]160
161  /* assume sh and uptodeg agree */
[3a67ea7]162
163  if (sh == 0) return(p); /* the zero shift */
164
[cb0fbe]165  poly q  = NULL;
[37a4c3]166  poly pp = p; // pCopy(p);
[cb0fbe]167  while (pp!=NULL)
[3a67ea7]168  {
[cb0fbe]169    q = p_Add_q(q, pmLPshift(pp,sh,uptodeg,lV),currRing);
170    pIter(pp);
[3a67ea7]171  }
[cb0fbe]172  /* delete pp? */
[3a67ea7]173  /* int version: returns TRUE if it was successful */
[cb0fbe]174  return(q);
[3a67ea7]175}
176
177poly pmLPshift(poly p, int sh, int uptodeg, int lV)
178{
[4d2ab5c]179  /* TODO: use a shortcut with p_ version */
[3a67ea7]180  /* pm is a monomial */
181
182  if (sh == 0) return(p); /* the zero shift */
183
[cb0fbe]184  if (sh < 0 )
185  {
186#ifdef PDEBUG
187    Print("pmLPshift: negative shift requested");
188#endif
189    return(NULL); /* violation, 2check */
190  }
191
[3a67ea7]192  int L = pmLastVblock(p,lV);
[cb0fbe]193  if (L+sh-1 > uptodeg)
[3a67ea7]194  {
[cb0fbe]195#ifdef PDEBUG
196    Print("pmLPshift: too big shift requested");
197#endif
[3a67ea7]198    return(NULL); /* violation, 2check */
199  }
[cb0fbe]200  int *e=(int *)omAlloc0((currRing->N+1)*sizeof(int));
201  int *s=(int *)omAlloc0((currRing->N+1)*sizeof(int));
[3a67ea7]202  pGetExpV(p,e);
203  number c = pGetCoeff(p);
[cb0fbe]204  int j;
205  for (j=1; j<=currRing->N; j++)
[3a67ea7]206  {
[ad1c3b]207    if (e[j]==1)
[3a67ea7]208    {
[cb0fbe]209      s[j + (sh*lV)] = e[j]; /* actually 1 */
[3a67ea7]210    }
211  }
212  poly m = pOne();
213  pSetExpV(m,s);
[cb0fbe]214  /*  pSetm(m); */ /* done in the pSetExpV */
[37a4c3]215  /* think on the component */
[3a67ea7]216  pSetCoeff0(m,c);
217  freeT(e, currRing->N);
218  freeT(s, currRing->N);
219  return(m);
220}
221
222int pLastVblock(poly p, int lV)
223{
224  /* returns the number of maximal block */
225  /* appearing among the monomials of p */
[37a4c3]226  /* the 0th block is the 1st one */
[4d2ab5c]227  poly q = p; //p_Copy(p,currRing); /* need it ? */
[cb0fbe]228  int ans = 0; 
229  int ansnew = 0;
[3a67ea7]230  while (q!=NULL)
231  {
232    ansnew = pmLastVblock(q,lV);
[cb0fbe]233    ans    = si_max(ans,ansnew);
[3a67ea7]234    pIter(q);
235  }
[cb0fbe]236  /* do not need to delete q */
[3a67ea7]237  return(ans);
238}
239
240int pmLastVblock(poly p, int lV)
241{
242  /* for a monomial p, returns the number of the last block */
243  /* where a nonzero exponent is sitting */
[4d2ab5c]244  if (pIsConstantPoly(p))
245  {
246    return(int(0));
247  }
[cb0fbe]248  int *e=(int *)omAlloc0((currRing->N+1)*sizeof(int));
[3a67ea7]249  pGetExpV(p,e);
250  int j,b;
[cb0fbe]251  j = currRing->N;
[3a67ea7]252  while ( (!e[j]) && (j>=1) ) j--;
[cb0fbe]253  if (j==0) 
254  {
255#ifdef PDEBUG
[37a4c3]256    Print("pmLastVblock: unexpected zero exponent vector");
257    PrintLn();
[cb0fbe]258#endif   
259    return(j);
260  }
261  b = (int)(j/lV) + 1; /* the number of the block, >=1 */
[3a67ea7]262  return (b);
263}
264
[4d2ab5c]265int p_LastVblockT(poly p, int lV, kStrategy strat, const ring r)
266{
267  /* returns the number of maximal block */
268  /* appearing among the monomials of p */
269  /* the 0th block is the 1st one */
270
271  /* p is like TObject: lm in currRing = r, tail in tailRing  */
272  assume(p_LmCheckIsFromRing(p,r));
273  assume(p_CheckIsFromRing(pNext(p),strat->tailRing));
274
275  int ans = p_mLastVblock(p, lV, r); // Block of LM
276  poly q = pNext(p); 
277  int ansnew = 0;
278  while (q != NULL)
279  {
280    ansnew = p_mLastVblock(q, lV, strat->tailRing);
281    ans       = si_max(ans,ansnew);
282    pIter(q);
283  }
284  /* do not need to delete q */
285  return(ans);
286}
287
288int p_LastVblock(poly p, int lV, const ring r)
289{
290  /* returns the number of maximal block */
291  /* appearing among the monomials of p */
292  /* the 0th block is the 1st one */
293  poly q = p; //p_Copy(p,currRing); /* need it ? */
294  int ans = 0; 
295  int ansnew = 0;
296  while (q!=NULL)
297  {
298    ansnew = p_mLastVblock(q, lV, r);
299    ans    = si_max(ans,ansnew);
300    pIter(q);
301  }
302  /* do not need to delete q */
303  return(ans);
304}
305
306int p_mLastVblock(poly p, int lV, const ring r)
307{
308  /* for a monomial p, returns the number of the last block */
309  /* where a nonzero exponent is sitting */
310  if (p_LmIsConstant(p,r))
311  {
312    return(0);
313  }
314  int *e=(int *)omAlloc0((r->N+1)*sizeof(int));
315  p_GetExpV(p,e,r);
316  int j,b;
317  j = r->N;
318  while ( (!e[j]) && (j>=1) ) j--;
319  if (j==0) 
320  {
321#ifdef PDEBUG
322    Print("pmLastVblock: unexpected zero exponent vector");
323    PrintLn();
324#endif   
325    return(j);
326  }
327  b = (int)((j+lV-1)/lV); /* the number of the block, >=1 */
328  freeT(e,r->N);
329  return (b);
330}
331
332int pFirstVblock(poly p, int lV)
333{
334  /* returns the number of maximal block */
335  /* appearing among the monomials of p */
336  /* the 0th block is the 1st one */
337  poly q = p; //p_Copy(p,currRing); /* need it ? */
338  int ans = 0; 
339  int ansnew = 0;
340  while (q!=NULL)
341  {
342    ansnew = pmFirstVblock(q,lV);
343    ans    = si_min(ans,ansnew);
344    pIter(q);
345  }
346  /* do not need to delete q */
347  return(ans);
348}
349
350int pmFirstVblock(poly p, int lV)
351{
352  if (pIsConstantPoly(p))
353  {
354    return(int(0));
355  }
356  /* for a monomial p, returns the number of the first block */
357  /* where a nonzero exponent is sitting */
358  int *e=(int *)omAlloc0((currRing->N+1)*sizeof(int));
359  pGetExpV(p,e);
360  int j,b;
361  j = 1;
362  while ( (!e[j]) && (j<=currRing->N-1) ) j++;
363  if (j==currRing->N + 1) 
364  {
365#ifdef PDEBUG
366    Print("pmFirstVblock: unexpected zero exponent vector");
367    PrintLn();
368#endif   
369    return(j);
370  }
371  b = (int)(j/lV)+1; /* the number of the block, 1<= N <= currRing->N  */
372  return (b);
373}
374
[1c473f]375  /* there should be two routines: */
[ad1c3b]376  /* 1. test place-squarefreeness: in homog this suffices: isInV */
[1c473f]377  /* 2. test the presence of a hole -> in the tail??? */
[4d2ab5c]378
[3a67ea7]379int isInV(poly p, int lV)
380{
[ad1c3b]381  /* investigate only the leading monomial of p in currRing */
[dabe365]382  if ( pIsConstant(p) ) return(1);
[cb0fbe]383  if (lV <= 0) return(0);
[3a67ea7]384  /* returns 1 iff p is in V */
[cb0fbe]385  /* that is in each block up to a certain one there is only one nonzero exponent */
[3a67ea7]386  /* lV = the length of V = the number of orig vars */
[cb0fbe]387  int *e = (int *)omAlloc0((currRing->N+1)*sizeof(int));
[ad1c3b]388  int  b = (int)((currRing->N +lV-1)/lV); /* the number of blocks */
389  //int b  = (int)(currRing->N)/lV;
[cb0fbe]390  int *B = (int *)omAlloc0((b+1)*sizeof(int)); /* the num of elements in a block */
[3a67ea7]391  pGetExpV(p,e);
392  int i,j;
393  for (j=1; j<=b; j++)
394  {
395    /* we go through all the vars */
396    /* by blocks in lV vars */
397    for (i=(j-1)*lV + 1; i<= j*lV; i++)
398    {
[4d2ab5c]399      if (e[i]) B[j] = B[j]+1;
[3a67ea7]400    }
401  }
[ad1c3b]402  //  j = b;
[37a4c3]403  //  while ( (!B[j]) && (j>=1)) j--;
404  for (j=b; j>=1; j--)
405  {
406    if (B[j]!=0) break;
407  }
[ad1c3b]408  /* do not need e anymore */
409  freeT(e, currRing->N);
[37a4c3]410
[ad1c3b]411  if (j==0) goto ret_true;
412//   {
413//     /* it is a zero exp vector, which is in V */
414//     freeT(B, b);
415//     return(1);
416//   }
417  /* now B[j] != 0 and we test place-squarefreeness */
[3a67ea7]418  for (j; j>=1; j--)
419  {
420    if (B[j]!=1)
421    {
[ad1c3b]422      freeT(B, b);
[3a67ea7]423      return(0);
424    }
425  }
[ad1c3b]426 ret_true:
427  freeT(B, b);
[3a67ea7]428  return(1);
429}
430
[dabe365]431int poly_isInV(poly p, int lV)
432{
433  /* tests whether the whole polynomial p in in V */
434  poly q = p;
435  while (q!=NULL)
436  {
437    if ( !isInV(q,lV) )
438    {
439      return(0);
440    }
441    q = pNext(q);
442  }
443  return(1);
444}
445
446int ideal_isInV(ideal I, int lV)
447{
448  /* tests whether each polynomial of an ideal I lies in in V */
449  int i;
450  int s    = IDELEMS(I)-1;
451  for(i = 0; i <= s; i++)
452  {
453    if ( !poly_isInV(I->m[i],lV) )
454    {
455      return(0);
456    }
457  }
458  return(1);
459}
460
461
[4d2ab5c]462int itoInsert(poly p, int uptodeg, int lV, const ring r)
463{
464  /* for poly in lmCR/tailTR presentation */
[ad1c3b]465  /* the below situation (commented out) might happen! */
[4d2ab5c]466//   if (r == currRing)
467//   {
468//     "Current ring is not expected in toInsert";
469//     return(0);
470//   }
471  /* compute the number of insertions */
472  int i = p_mLastVblock(p, lV, currRing);
473  if (pNext(p) != NULL)
474  {
475    i = si_max(i, p_LastVblock(pNext(p), lV, r) );
476  }
477  //  i = uptodeg  - i +1;
478  i = uptodeg  - i; 
[1c473f]479  //  p_wrp(p,currRing,r); Print("----i:%d",i); PrintLn();
[4d2ab5c]480  return(i);
481}
482
[ad1c3b]483poly p_ShrinkT(poly p, int lV, kStrategy strat, const ring r)
484//poly p_Shrink(poly p, int uptodeg, int lV, kStrategy strat, const ring r)
485{
486  /* p is like TObject: lm in currRing = r, tail in tailRing  */
487  /* proc shrinks the poly p in ring r */
488  /* lV = the length of V = the number of orig vars */
489  /* check assumes/exceptions */
490  /* r->N is a multiple of lV */
491
492  if (p==NULL) return(p);
493
494  assume(p_LmCheckIsFromRing(p,r));
495  assume(p_CheckIsFromRing(pNext(p),strat->tailRing));
496
497  poly q   = NULL;
498  poly s   = p_mShrink(p, lV, r); // lm in currRing
499  poly pp = pNext(p);
500 
501  while (pp != NULL)
502  {
503    //    q = p_Add_q(q, p_mShrink(pp,uptodeg,lV,strat->tailRing),strat->tailRing);
504    q = p_Add_q(q, p_mShrink(pp,lV,strat->tailRing),strat->tailRing);
505    pIter(pp);
506  }
507  pNext(s) = q;
508  return(s);
509}
510
511poly p_Shrink(poly p, int lV, const ring r)
512{
513  /* proc shrinks the poly p in ring r */
514  /* lV = the length of V = the number of orig vars */
515  /* check assumes/exceptions */
516  /* r->N is a multiple of lV */
517
518  if (p==NULL) return(p);
519  assume(p_CheckIsFromRing(p,r));
520  poly q = NULL;
521  poly pp = p;
522 
523  while (pp != NULL)
524  {
525    q = p_Add_q(q, p_mShrink(pp,lV,r),r);
526    pIter(pp);
527  }
528  return(q);
529}
530
531poly p_mShrink(poly p, int lV, const ring r)
532{
533  /* shrinks the monomial p in ring r */
534  /* lV = the length of V = the number of orig vars */
535
536  /* check assumes/exceptions */
537  /* r->N is a multiple of lV */
538
539  int *e = (int *)omAlloc0((r->N+1)*sizeof(int));
540  int  b = (int)((r->N +lV-1)/lV); /* the number of blocks */
541  //  int *B = (int *)omAlloc0((b+1)*sizeof(int)); /* the num of elements in a block */
542  int *S = (int *)omAlloc0((r->N+1)*sizeof(int)); /* the shrinked exponent */
543  p_GetExpV(p,e,r);
544  int i,j; int cnt = 1; //counter for blocks in S
545  for (j=1; j<=b; j++)
546  {
547    /* we go through all the vars */
548    /* by blocks in lV vars */
549    for (i=(j-1)*lV + 1; i<= j*lV; i++)
550    {
551      if (e[i]==1) 
552      {
553        //      B[j] = B[j]+1; // for control in V?
554         S[(cnt-1)*lV + (i - (j-1)*lV)] = e[i];
555         /* assuming we are in V, can interrupt here */
556         cnt++;
557         //  break; //results in incomplete shrink!
558         i = j*lV; // manual break under assumption p is in V
559      }
560    }
561  }
562#ifdef PDEBUG
563  //  Print("p_mShrink: cnt = [%d], b = %d\n",cnt,b);
564#endif
565  // cnt -1 <= b  must hold!
566  //  freeT(B, b);
[b902246]567  poly s = p_One(r);
[ad1c3b]568  p_SetExpV(s,S,r);
569  freeT(e, r->N);
570  freeT(S, r->N);
571  /*  p_Setm(s,r); // done by p_SetExpV */
572  p_SetComp(s,p_GetComp(p,r),r); // component is preserved
573  p_SetCoeff(s,p_GetCoeff(p,r),r);  // coeff is preserved
574#ifdef PDEBUG
575  //  Print("p_mShrink: from "); p_wrp(p,r); Print(" to "); p_wrp(s,r); PrintLn();
576#endif
577  return(s);
578}
[4d2ab5c]579
[cb0fbe]580/* shiftgb stuff */
[3a67ea7]581
[37a4c3]582
583/* remarks: cleanT : just deletion
584enlargeT: just reallocation */
585
[07625cb]586#endif
Note: See TracBrowser for help on using the repository browser.