source: git/kernel/shiftgb.cc @ 762407

spielwiese
Last change on this file since 762407 was 762407, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
config.h is for sources files only FIX: config.h should only be used by source (not from inside kernel/mod2.h!) NOTE: each source file should better include mod2.h right after config.h, while headers should better not include mod2.h.
  • Property mode set to 100644
File size: 14.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id$ */
5/*
6* ABSTRACT: kernel: utils for shift GB and free GB
7*/
8
9#include "config.h"
10#include <kernel/mod2.h>
11
12#ifdef HAVE_SHIFTBBA
13#include <kernel/febase.h>
14#include <polys/monomials/ring.h>
15#include <kernel/polys.h>
16#include <coeffs/numbers.h>
17#include <kernel/ideals.h>
18#include <polys/matpol.h>
19#include <polys/kbuckets.h>
20#include <kernel/kstd1.h>
21#include <polys/sbuckets.h>
22#include <polys/operations/p_Mult_q.h>
23#include <kernel/kutil.h>
24#include <kernel/structs.h>
25#include <omalloc/omalloc.h>
26#include <kernel/khstd.h>
27#include <polys/kbuckets.h>
28#include <polys/weight.h>
29#include <misc/intvec.h>
30#include <kernel/structs.h>
31#include <kernel/kInline.h>
32#include <kernel/stairc.h>
33#include <polys/weight.h>
34#include <misc/intvec.h>
35#include <kernel/timer.h>
36#include <kernel/shiftgb.h>
37#include <polys/nc/sca.h>
38
39
40#define freeT(A,v) omFreeSize((ADDRESS)A,(v+1)*sizeof(int))
41
42
43/* TODO: write p* stuff as instances of p_* for all the functions */
44/* p_* functions are new, p* are old */
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
56  /* assume sh and uptodeg agree  TODO check */
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);
63
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 */
78  /* shifts the poly p from the ring r by sh */
79
80  /* assume sh and uptodeg agree TODO check */
81
82  if (p==NULL) return(p);
83  if (sh == 0) return(p); /* the zero shift */
84
85  poly q  = NULL;
86  poly pp = p; // do not take copies
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{
97  /* p is a monomial from the ring r */
98
99  if (sh == 0) return(p); /* the zero shift */
100
101  if (sh < 0 )
102  {
103#ifdef PDEBUG
104    PrintS("pmLPshift: negative shift requested\n");
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
113    PrintS("p_mLPshift: too big shift requested\n");
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);
120
121  int j;
122  //  for (j=1; j<=r->N; j++)
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 */
129#ifdef PDEBUG
130      omCheckAddr(s);
131#endif
132    }
133#ifdef PDEBUG
134    else
135    {
136      if (e[j]!=0)
137      {
138         //         Print("p_mLPshift: ex[%d]=%d\n",j,e[j]);
139      }
140    }
141#endif
142  }
143  poly m = p_One(r);
144  p_SetExpV(m,s,r);
145  freeT(e, r->N);
146  freeT(s, r->N);
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
152  p_SetCoeff0(m,p_GetCoeff(p,r),r);  // coeff is preserved
153  return(m);
154}
155
156poly pLPshift(poly p, int sh, int uptodeg, int lV)
157{
158  /* assume shift takes place */
159  /* shifts the poly p by sh */
160  /* deletes p */
161
162  /* assume sh and uptodeg agree */
163
164  if (sh == 0) return(p); /* the zero shift */
165
166  poly q  = NULL;
167  poly pp = p; // pCopy(p);
168  while (pp!=NULL)
169  {
170    q = p_Add_q(q, pmLPshift(pp,sh,uptodeg,lV),currRing);
171    pIter(pp);
172  }
173  /* delete pp? */
174  /* int version: returns TRUE if it was successful */
175  return(q);
176}
177
178poly pmLPshift(poly p, int sh, int uptodeg, int lV)
179{
180  /* TODO: use a shortcut with p_ version */
181  /* pm is a monomial */
182
183  if (sh == 0) return(p); /* the zero shift */
184
185  if (sh < 0 )
186  {
187#ifdef PDEBUG
188    PrintS("pmLPshift: negative shift requested\n");
189#endif
190    return(NULL); /* violation, 2check */
191  }
192
193  int L = pmLastVblock(p,lV);
194  if (L+sh-1 > uptodeg)
195  {
196#ifdef PDEBUG
197    PrintS("pmLPshift: too big shift requested\n");
198#endif
199    return(NULL); /* violation, 2check */
200  }
201  int *e=(int *)omAlloc0((currRing->N+1)*sizeof(int));
202  int *s=(int *)omAlloc0((currRing->N+1)*sizeof(int));
203  pGetExpV(p,e);
204  number c = pGetCoeff(p);
205  int j;
206  for (j=1; j<=currRing->N; j++)
207  {
208    if (e[j]==1)
209    {
210      s[j + (sh*lV)] = e[j]; /* actually 1 */
211    }
212  }
213  poly m = pOne();
214  pSetExpV(m,s);
215  /*  pSetm(m); */ /* done in the pSetExpV */
216  /* think on the component */
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 */
227  /* the 0th block is the 1st one */
228  poly q = p; //p_Copy(p,currRing); /* need it ? */
229  int ans = 0;
230  int ansnew = 0;
231  while (q!=NULL)
232  {
233    ansnew = pmLastVblock(q,lV);
234    ans    = si_max(ans,ansnew);
235    pIter(q);
236  }
237  /* do not need to delete q */
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 */
245  if (pIsConstantPoly(p))
246  {
247    return(int(0));
248  }
249  int *e=(int *)omAlloc0((currRing->N+1)*sizeof(int));
250  pGetExpV(p,e);
251  int j,b;
252  j = currRing->N;
253  while ( (!e[j]) && (j>=1) ) j--;
254  if (j==0)
255  {
256#ifdef PDEBUG
257    PrintS("pmLastVblock: unexpected zero exponent vector\n");
258#endif
259    return(j);
260  }
261  b = (int)(j/lV) + 1; /* the number of the block, >=1 */
262  return (b);
263}
264
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    PrintS("pmLastVblock: unexpected zero exponent vector\n");
323#endif
324    return(j);
325  }
326  b = (int)((j+lV-1)/lV); /* the number of the block, >=1 */
327  freeT(e,r->N);
328  return (b);
329}
330
331int pFirstVblock(poly p, int lV)
332{
333  /* returns the number of maximal block */
334  /* appearing among the monomials of p */
335  /* the 0th block is the 1st one */
336  poly q = p; //p_Copy(p,currRing); /* need it ? */
337  int ans = 0;
338  int ansnew = 0;
339  while (q!=NULL)
340  {
341    ansnew = pmFirstVblock(q,lV);
342    ans    = si_min(ans,ansnew);
343    pIter(q);
344  }
345  /* do not need to delete q */
346  return(ans);
347}
348
349int pmFirstVblock(poly p, int lV)
350{
351  if (pIsConstantPoly(p))
352  {
353    return(int(0));
354  }
355  /* for a monomial p, returns the number of the first block */
356  /* where a nonzero exponent is sitting */
357  int *e=(int *)omAlloc0((currRing->N+1)*sizeof(int));
358  pGetExpV(p,e);
359  int j,b;
360  j = 1;
361  while ( (!e[j]) && (j<=currRing->N-1) ) j++;
362  if (j==currRing->N + 1)
363  {
364#ifdef PDEBUG
365    PrintS("pmFirstVblock: unexpected zero exponent vector\n");
366#endif
367    return(j);
368  }
369  b = (int)(j/lV)+1; /* the number of the block, 1<= N <= currRing->N  */
370  return (b);
371}
372
373  /* there should be two routines: */
374  /* 1. test place-squarefreeness: in homog this suffices: isInV */
375  /* 2. test the presence of a hole -> in the tail??? */
376
377int isInV(poly p, int lV)
378{
379  /* investigate only the leading monomial of p in currRing */
380  if ( pIsConstant(p) ) return(1);
381  if (lV <= 0) return(0);
382  /* returns 1 iff p is in V */
383  /* that is in each block up to a certain one there is only one nonzero exponent */
384  /* lV = the length of V = the number of orig vars */
385  int *e = (int *)omAlloc0((currRing->N+1)*sizeof(int));
386  int  b = (int)((currRing->N +lV-1)/lV); /* the number of blocks */
387  //int b  = (int)(currRing->N)/lV;
388  int *B = (int *)omAlloc0((b+1)*sizeof(int)); /* the num of elements in a block */
389  pGetExpV(p,e);
390  int i,j;
391  for (j=1; j<=b; j++)
392  {
393    /* we go through all the vars */
394    /* by blocks in lV vars */
395    for (i=(j-1)*lV + 1; i<= j*lV; i++)
396    {
397      if (e[i]) B[j] = B[j]+1;
398    }
399  }
400  //  j = b;
401  //  while ( (!B[j]) && (j>=1)) j--;
402  for (j=b; j>=1; j--)
403  {
404    if (B[j]!=0) break;
405  }
406  /* do not need e anymore */
407  freeT(e, currRing->N);
408
409  if (j==0) goto ret_true;
410//   {
411//     /* it is a zero exp vector, which is in V */
412//     freeT(B, b);
413//     return(1);
414//   }
415  /* now B[j] != 0 and we test place-squarefreeness */
416  for (; j>=1; j--)
417  {
418    if (B[j]!=1)
419    {
420      freeT(B, b);
421      return(0);
422    }
423  }
424 ret_true:
425  freeT(B, b);
426  return(1);
427}
428
429int poly_isInV(poly p, int lV)
430{
431  /* tests whether the whole polynomial p in in V */
432  poly q = p;
433  while (q!=NULL)
434  {
435    if ( !isInV(q,lV) )
436    {
437      return(0);
438    }
439    q = pNext(q);
440  }
441  return(1);
442}
443
444int ideal_isInV(ideal I, int lV)
445{
446  /* tests whether each polynomial of an ideal I lies in in V */
447  int i;
448  int s    = IDELEMS(I)-1;
449  for(i = 0; i <= s; i++)
450  {
451    if ( !poly_isInV(I->m[i],lV) )
452    {
453      return(0);
454    }
455  }
456  return(1);
457}
458
459
460int itoInsert(poly p, int uptodeg, int lV, const ring r)
461{
462  /* for poly in lmCR/tailTR presentation */
463  /* the below situation (commented out) might happen! */
464//   if (r == currRing)
465//   {
466//     "Current ring is not expected in toInsert";
467//     return(0);
468//   }
469  /* compute the number of insertions */
470  int i = p_mLastVblock(p, lV, currRing);
471  if (pNext(p) != NULL)
472  {
473    i = si_max(i, p_LastVblock(pNext(p), lV, r) );
474  }
475  //  i = uptodeg  - i +1;
476  i = uptodeg  - i;
477  //  p_wrp(p,currRing,r); Print("----i:%d",i); PrintLn();
478  return(i);
479}
480
481poly p_ShrinkT(poly p, int lV, kStrategy strat, const ring r)
482//poly p_Shrink(poly p, int uptodeg, int lV, kStrategy strat, const ring r)
483{
484  /* p is like TObject: lm in currRing = r, tail in tailRing  */
485  /* proc shrinks the poly p in ring r */
486  /* lV = the length of V = the number of orig vars */
487  /* check assumes/exceptions */
488  /* r->N is a multiple of lV */
489
490  if (p==NULL) return(p);
491
492  assume(p_LmCheckIsFromRing(p,r));
493  assume(p_CheckIsFromRing(pNext(p),strat->tailRing));
494
495  poly q   = NULL;
496  poly s   = p_mShrink(p, lV, r); // lm in currRing
497  poly pp = pNext(p);
498
499  while (pp != NULL)
500  {
501    //    q = p_Add_q(q, p_mShrink(pp,uptodeg,lV,strat->tailRing),strat->tailRing);
502    q = p_Add_q(q, p_mShrink(pp,lV,strat->tailRing),strat->tailRing);
503    pIter(pp);
504  }
505  pNext(s) = q;
506  return(s);
507}
508
509poly p_Shrink(poly p, int lV, const ring r)
510{
511  /* proc shrinks the poly p in ring r */
512  /* lV = the length of V = the number of orig vars */
513  /* check assumes/exceptions */
514  /* r->N is a multiple of lV */
515
516  if (p==NULL) return(p);
517  assume(p_CheckIsFromRing(p,r));
518  poly q = NULL;
519  poly pp = p;
520
521  while (pp != NULL)
522  {
523    q = p_Add_q(q, p_mShrink(pp,lV,r),r);
524    pIter(pp);
525  }
526  return(q);
527}
528
529poly p_mShrink(poly p, int lV, const ring r)
530{
531  /* shrinks the monomial p in ring r */
532  /* lV = the length of V = the number of orig vars */
533
534  /* check assumes/exceptions */
535  /* r->N is a multiple of lV */
536
537  int *e = (int *)omAlloc0((r->N+1)*sizeof(int));
538  int  b = (int)((r->N +lV-1)/lV); /* the number of blocks */
539  //  int *B = (int *)omAlloc0((b+1)*sizeof(int)); /* the num of elements in a block */
540  int *S = (int *)omAlloc0((r->N+1)*sizeof(int)); /* the shrinked exponent */
541  p_GetExpV(p,e,r);
542  int i,j; int cnt = 1; //counter for blocks in S
543  for (j=1; j<=b; j++)
544  {
545    /* we go through all the vars */
546    /* by blocks in lV vars */
547    for (i=(j-1)*lV + 1; i<= j*lV; i++)
548    {
549      if (e[i]==1)
550      {
551         //      B[j] = B[j]+1; // for control in V?
552         S[(cnt-1)*lV + (i - (j-1)*lV)] = e[i];
553         /* assuming we are in V, can interrupt here */
554         cnt++;
555         //  break; //results in incomplete shrink!
556         i = j*lV; // manual break under assumption p is in V
557      }
558    }
559  }
560#ifdef PDEBUG
561  //  Print("p_mShrink: cnt = [%d], b = %d\n",cnt,b);
562#endif
563  // cnt -1 <= b  must hold!
564  //  freeT(B, b);
565  poly s = p_One(r);
566  p_SetExpV(s,S,r);
567  freeT(e, r->N);
568  freeT(S, r->N);
569  /*  p_Setm(s,r); // done by p_SetExpV */
570  p_SetComp(s,p_GetComp(p,r),r); // component is preserved
571  p_SetCoeff(s,p_GetCoeff(p,r),r);  // coeff is preserved
572#ifdef PDEBUG
573  //  Print("p_mShrink: from "); p_wrp(p,r); Print(" to "); p_wrp(s,r); PrintLn();
574#endif
575  return(s);
576}
577
578/* shiftgb stuff */
579
580
581/*2
582 *if the leading term of p
583 *divides the leading term of some T[i] it will be canceled
584 */
585// static inline void clearSShift (poly p, unsigned long p_sev,int l, int* at, int* k,
586//                            kStrategy strat)
587// {
588//   assume(p_sev == pGetShortExpVector(p));
589//   if (!pLmShortDivisibleBy(p,p_sev, strat->T[*at].p, ~ strat->sevT[*at])) return;
590//   //  if (l>=strat->lenS[*at]) return;
591//   if (TEST_OPT_PROT)
592//     PrintS("!");
593//   mflush();
594//   //pDelete(&strat->S[*at]);
595//   deleteInS((*at),strat);
596//   (*at)--;
597//   (*k)--;
598// //  assume(lenS_correct(strat));
599// }
600
601/* remarks: cleanT : just deletion
602enlargeT: just reallocation */
603
604#endif
Note: See TracBrowser for help on using the repository browser.