source: git/kernel/shiftgb.cc @ d90109

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