source: git/kernel/kstd2.cc @ 0179d5

spielwiese
Last change on this file since 0179d5 was 7ae94b, checked in by Hans Schönemann <hannes@…>, 15 years ago
*hannes: OVERFLOW stuff git-svn-id: file:///usr/local/Singular/svn/trunk@11978 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 49.8 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: kstd2.cc,v 1.97 2009-07-13 16:36:35 Singular Exp $ */
5/*
6*  ABSTRACT -  Kernel: alg. of Buchberger
7*/
8
9// #define PDEBUG 2
10// define to enable tailRings
11#define HAVE_TAIL_RING
12// define if no buckets should be used
13// #define NO_BUCKETS
14
15#include "mod2.h"
16#ifdef HAVE_PLURAL
17#define PLURAL_INTERNAL_DECLARATIONS 1
18#endif
19#include "kutil.h"
20#include "structs.h"
21#include "omalloc.h"
22#include "polys.h"
23#include "ideals.h"
24#include "febase.h"
25#include "kstd1.h"
26#include "khstd.h"
27#include "kbuckets.h"
28//#include "cntrlc.h"
29#include "weight.h"
30#include "intvec.h"
31#ifdef HAVE_PLURAL
32#include "gring.h"
33#endif
34// #include "timer.h"
35
36/* shiftgb stuff */
37#include "shiftgb.h"
38
39  int (*test_PosInT)(const TSet T,const int tl,LObject &h);
40  int (*test_PosInL)(const LSet set, const int length,
41                LObject* L,const kStrategy strat);
42
43// return -1 if no divisor is found
44//        number of first divisor, otherwise
45int kFindDivisibleByInT(const TSet &T, const unsigned long* sevT,
46                        const int tl, const LObject* L, const int start)
47{
48  unsigned long not_sev = ~L->sev;
49  int j = start;
50  poly p=L->p;
51  ring r=currRing;
52  if (p==NULL)  { r=L->tailRing; p=L->t_p; }
53  L->GetLm(p, r);
54
55  pAssume(~not_sev == p_GetShortExpVector(p, r));
56
57  if (r == currRing)
58  {
59    loop
60    {
61      if (j > tl) return -1;
62#if defined(PDEBUG) || defined(PDIV_DEBUG)
63      if (p_LmShortDivisibleBy(T[j].p, sevT[j],
64                               p, not_sev, r))
65        return j;
66#else
67      if (!(sevT[j] & not_sev) &&
68          p_LmDivisibleBy(T[j].p, p, r))
69        return j;
70#endif
71      j++;
72    }
73  }
74  else
75  {
76    loop
77    {
78      if (j > tl) return -1;
79#if defined(PDEBUG) || defined(PDIV_DEBUG)
80      if (p_LmShortDivisibleBy(T[j].t_p, sevT[j],
81                               p, not_sev, r))
82        return j;
83#else
84      if (!(sevT[j] & not_sev) &&
85          p_LmDivisibleBy(T[j].t_p, p, r))
86        return j;
87#endif
88      j++;
89    }
90  }
91}
92
93// same as above, only with set S
94int kFindDivisibleByInS(const kStrategy strat, int* max_ind, LObject* L)
95{
96  unsigned long not_sev = ~L->sev;
97  poly p = L->GetLmCurrRing();
98  int j = 0;
99
100  pAssume(~not_sev == p_GetShortExpVector(p, currRing));
101#if 1
102  int ende;
103  if (strat->ak>0) ende=strat->sl;
104  else ende=posInS(strat,*max_ind,p,0)+1;
105  if (ende>(*max_ind)) ende=(*max_ind);
106#else
107  int ende=strat->sl;
108#endif
109  (*max_ind)=ende;
110  loop
111  {
112    if (j > ende) return -1;
113#if defined(PDEBUG) || defined(PDIV_DEBUG)
114    if (p_LmShortDivisibleBy(strat->S[j], strat->sevS[j],
115                             p, not_sev, currRing))
116        return j;
117#else
118    if ( !(strat->sevS[j] & not_sev) &&
119         p_LmDivisibleBy(strat->S[j], p, currRing))
120      return j;
121#endif
122    j++;
123  }
124}
125
126int kFindNextDivisibleByInS(const kStrategy strat, int start,int max_ind, LObject* L)
127{
128  unsigned long not_sev = ~L->sev;
129  poly p = L->GetLmCurrRing();
130  int j = start;
131
132  pAssume(~not_sev == p_GetShortExpVector(p, currRing));
133#if 1
134  int ende=max_ind;
135#else
136  int ende=strat->sl;
137#endif
138  loop
139  {
140    if (j > ende) return -1;
141#if defined(PDEBUG) || defined(PDIV_DEBUG)
142    if (p_LmShortDivisibleBy(strat->S[j], strat->sevS[j],
143                             p, not_sev, currRing))
144        return j;
145#else
146    if ( !(strat->sevS[j] & not_sev) &&
147         p_LmDivisibleBy(strat->S[j], p, currRing))
148      return j;
149#endif
150    j++;
151  }
152}
153
154#ifdef HAVE_RINGS
155static NATNUMBER factorial(NATNUMBER arg)
156{
157   NATNUMBER tmp = 1; arg++;
158   for (int i = 2; i < arg; i++)
159   {
160     tmp *= i;
161   }
162   return tmp;
163}
164
165poly kFindZeroPoly(poly input_p, ring leadRing, ring tailRing)
166{
167  // m = currRing->ch
168
169  if (input_p == NULL) return NULL;
170
171  poly p = input_p;
172  poly zeroPoly = NULL;
173  NATNUMBER a = (NATNUMBER) pGetCoeff(p);
174
175  int k_ind2 = 0;
176  int a_ind2 = ind2(a);
177
178  NATNUMBER k = 1;
179  // of interest is only k_ind2, special routine for improvement ... TODO OLIVER
180  for (int i = 1; i <= leadRing->N; i++)
181  {
182    k_ind2 = k_ind2 + ind_fact_2(p_GetExp(p, i, leadRing));
183  }
184
185  a = (NATNUMBER) pGetCoeff(p);
186
187  number tmp1;
188  poly tmp2, tmp3;
189  poly lead_mult = p_ISet(1, tailRing);
190  if (leadRing->ch <= k_ind2 + a_ind2)
191  {
192    int too_much = k_ind2 + a_ind2 - leadRing->ch;
193    int s_exp;
194    zeroPoly = p_ISet(a, tailRing);
195    for (int i = 1; i <= leadRing->N; i++)
196    {
197      s_exp = p_GetExp(p, i,leadRing);
198      if (s_exp % 2 != 0)
199      {
200        s_exp = s_exp - 1;
201      }
202      while ( (0 < ind2(s_exp)) && (ind2(s_exp) <= too_much) )
203      {
204        too_much = too_much - ind2(s_exp);
205        s_exp = s_exp - 2;
206      }
207      p_SetExp(lead_mult, i, p_GetExp(p, i,leadRing) - s_exp, tailRing);
208      for (NATNUMBER j = 1; j <= s_exp; j++)
209      {
210        tmp1 = nInit(j);
211        tmp2 = p_ISet(1, tailRing);
212        p_SetExp(tmp2, i, 1, tailRing);
213        p_Setm(tmp2, tailRing);
214        if (nIsZero(tmp1))
215        { // should nowbe obsolet, test ! TODO OLIVER
216          zeroPoly = p_Mult_q(zeroPoly, tmp2, tailRing);
217        }
218        else
219        {
220          tmp3 = p_NSet(nCopy(tmp1), tailRing);
221          zeroPoly = p_Mult_q(zeroPoly, p_Add_q(tmp3, tmp2, tailRing), tailRing);
222        }
223      }
224    }
225    p_Setm(lead_mult, tailRing);
226    zeroPoly = p_Mult_mm(zeroPoly, lead_mult, tailRing);
227    tmp2 = p_NSet(nCopy(pGetCoeff(zeroPoly)), leadRing);
228    for (int i = 1; i <= leadRing->N; i++)
229    {
230      pSetExp(tmp2, i, p_GetExp(zeroPoly, i, tailRing));
231    }
232    p_Setm(tmp2, leadRing);
233    zeroPoly = p_LmDeleteAndNext(zeroPoly, tailRing);
234    pNext(tmp2) = zeroPoly;
235    return tmp2;
236  }
237/*  NATNUMBER alpha_k = twoPow(leadRing->ch - k_ind2);
238  if (1 == 0 && alpha_k <= a)
239  {  // Temporarly disabled, reducing coefficients not compatible with std TODO Oliver
240    zeroPoly = p_ISet((a / alpha_k)*alpha_k, tailRing);
241    for (int i = 1; i <= leadRing->N; i++)
242    {
243      for (NATNUMBER j = 1; j <= p_GetExp(p, i, leadRing); j++)
244      {
245        tmp1 = nInit(j);
246        tmp2 = p_ISet(1, tailRing);
247        p_SetExp(tmp2, i, 1, tailRing);
248        p_Setm(tmp2, tailRing);
249        if (nIsZero(tmp1))
250        {
251          zeroPoly = p_Mult_q(zeroPoly, tmp2, tailRing);
252        }
253        else
254        {
255          tmp3 = p_ISet((NATNUMBER) tmp1, tailRing);
256          zeroPoly = p_Mult_q(zeroPoly, p_Add_q(tmp2, tmp3, tailRing), tailRing);
257        }
258      }
259    }
260    tmp2 = p_ISet((NATNUMBER) pGetCoeff(zeroPoly), leadRing);
261    for (int i = 1; i <= leadRing->N; i++)
262    {
263      pSetExp(tmp2, i, p_GetExp(zeroPoly, i, tailRing));
264    }
265    p_Setm(tmp2, leadRing);
266    zeroPoly = p_LmDeleteAndNext(zeroPoly, tailRing);
267    pNext(tmp2) = zeroPoly;
268    return tmp2;
269  } */
270  return NULL;
271}
272
273poly kFindDivisibleByZeroPoly(LObject* h)
274{
275  return kFindZeroPoly(h->GetLmCurrRing(), currRing, h->tailRing);
276}
277#endif
278
279
280#ifdef HAVE_RINGS
281/*2
282*  reduction procedure for the ring Z/2^m
283*/
284int redRing (LObject* h,kStrategy strat)
285{
286  if (h->p == NULL && h->t_p == NULL) return 0; // spoly is zero (can only occure with zero divisors)
287
288//  if (strat->tl<0) return 1;
289  int at,i;
290  long d;
291  int j = 0;
292  int pass = 0;
293  poly zeroPoly = NULL;
294
295// TODO warum SetpFDeg notwendig?
296  h->SetpFDeg();
297  assume(h->pFDeg() == h->FDeg);
298  long reddeg = h->GetpFDeg();
299
300  h->SetShortExpVector();
301  loop
302  {
303      j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
304      if (j < 0) return 1;
305#ifdef KDEBUG
306      if (TEST_OPT_DEBUG)
307      {
308        PrintS("T red:");
309      }
310#endif
311#ifdef KDEBUG
312    if (TEST_OPT_DEBUG)
313    {
314      h->wrp();
315      PrintS(" with ");
316      strat->T[j].wrp();
317    }
318#endif
319
320    ksReducePoly(h, &(strat->T[j]), NULL, NULL, strat);
321
322#ifdef KDEBUG
323    if (TEST_OPT_DEBUG)
324    {
325      PrintS("\nto ");
326      h->wrp();
327      PrintLn();
328    }
329#endif
330
331    if (h->GetLmTailRing() == NULL)
332    {
333      if (h->lcm!=NULL) pLmDelete(h->lcm);
334#ifdef KDEBUG
335      h->lcm=NULL;
336#endif
337      return 0;
338    }
339    h->SetShortExpVector();
340    d = h->SetpFDeg();
341    /*- try to reduce the s-polynomial -*/
342    pass++;
343    if (!K_TEST_OPT_REDTHROUGH &&
344        (strat->Ll >= 0) && ((d > reddeg) || (pass > strat->LazyPass)))
345    {
346      h->SetLmCurrRing();
347      at = strat->posInL(strat->L,strat->Ll,h,strat);
348      if (at <= strat->Ll)
349      {
350#ifdef KDEBUG
351        if (TEST_OPT_DEBUG) Print(" ->L[%d]\n",at);
352#endif
353        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);     // NOT RING CHECKED OLIVER
354        h->Clear();
355        return -1;
356      }
357    }
358    else if (d != reddeg)
359    {
360      if (d >= strat->tailRing->bitmask)
361      {
362        if (h->pTotalDeg() >= strat->tailRing->bitmask)
363        {
364          strat->overflow=TRUE;
365          //Print("OVERFLOW in redRing d=%ld, max=%ld\n",d,strat->tailRing->bitmask);
366          h->GetP();
367          at = strat->posInL(strat->L,strat->Ll,h,strat);
368          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
369          h->Clear();
370          return -1;
371        }
372      }
373      else if ((TEST_OPT_PROT) && (strat->Ll < 0))
374      {
375        Print(".%d",d);mflush();
376        reddeg = d;
377      }
378    }
379  }
380}
381#endif
382
383/*2
384*  reduction procedure for the homogeneous case
385*  and the case of a degree-ordering
386*/
387int redHomog (LObject* h,kStrategy strat)
388{
389  if (strat->tl<0) return 1;
390  //if (h->GetLmTailRing()==NULL) return 0; // HS: SHOULD NOT BE NEEDED!
391  assume(h->FDeg == h->pFDeg());
392
393  poly h_p;
394  int i,j,at,pass, ii;
395  unsigned long not_sev;
396  long reddeg,d;
397
398  pass = j = 0;
399  d = reddeg = h->GetpFDeg();
400  h->SetShortExpVector();
401  int li;
402  h_p = h->GetLmTailRing();
403  not_sev = ~ h->sev;
404  loop
405  {
406    j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
407    if (j < 0) return 1;
408
409    li = strat->T[j].pLength;
410    ii = j;
411    /*
412     * the polynomial to reduce with (up to the moment) is;
413     * pi with length li
414     */
415    i = j;
416#if 1
417    if (TEST_OPT_LENGTH)
418    loop
419    {
420      /*- search the shortest possible with respect to length -*/
421      i++;
422      if (i > strat->tl)
423        break;
424      if (li<=1)
425        break;
426      if ((strat->T[i].pLength < li)
427         &&
428          p_LmShortDivisibleBy(strat->T[i].GetLmTailRing(), strat->sevT[i],
429                               h_p, not_sev, strat->tailRing))
430      {
431        /*
432         * the polynomial to reduce with is now;
433         */
434        li = strat->T[i].pLength;
435        ii = i;
436      }
437    }
438#endif
439
440    /*
441     * end of search: have to reduce with pi
442     */
443#ifdef KDEBUG
444    if (TEST_OPT_DEBUG)
445    {
446      PrintS("red:");
447      h->wrp();
448      PrintS(" with ");
449      strat->T[ii].wrp();
450    }
451#endif
452    assume(strat->fromT == FALSE);
453
454    ksReducePoly(h, &(strat->T[ii]), NULL, NULL, strat);
455
456#ifdef KDEBUG
457    if (TEST_OPT_DEBUG)
458    {
459      PrintS("\nto ");
460      h->wrp();
461      PrintLn();
462    }
463#endif
464
465    h_p = h->GetLmTailRing();
466    if (h_p == NULL)
467    {
468      if (h->lcm!=NULL) pLmFree(h->lcm);
469#ifdef KDEBUG
470      h->lcm=NULL;
471#endif
472      return 0;
473    }
474    h->SetShortExpVector();
475    not_sev = ~ h->sev;
476    /*
477     * try to reduce the s-polynomial h
478     *test first whether h should go to the lazyset L
479     *-if the degree jumps
480     *-if the number of pre-defined reductions jumps
481     */
482    pass++;
483    if (!K_TEST_OPT_REDTHROUGH && (strat->Ll >= 0) && (pass > strat->LazyPass))
484    {
485      h->SetLmCurrRing();
486      at = strat->posInL(strat->L,strat->Ll,h,strat);
487      if (at <= strat->Ll)
488      {
489        int dummy=strat->sl;
490        if (kFindDivisibleByInS(strat, &dummy, h) < 0)
491          return 1;
492        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
493#ifdef KDEBUG
494        if (TEST_OPT_DEBUG)
495          Print(" lazy: -> L%d\n",at);
496#endif
497        h->Clear();
498        return -1;
499      }
500    }
501  }
502}
503
504/*2
505*  reduction procedure for the inhomogeneous case
506*  and not a degree-ordering
507*/
508int redLazy (LObject* h,kStrategy strat)
509{
510  if (strat->tl<0) return 1;
511  int at,i,ii,li;
512  int j = 0;
513  int pass = 0;
514  assume(h->pFDeg() == h->FDeg);
515  long reddeg = h->GetpFDeg();
516  long d;
517  unsigned long not_sev;
518
519  h->SetShortExpVector();
520  poly h_p = h->GetLmTailRing();
521  not_sev = ~ h->sev;
522  loop
523  {
524    j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
525    if (j < 0) return 1;
526
527    li = strat->T[j].pLength;
528    #if 0
529    if (li==0)
530    {
531      li=strat->T[j].pLength=pLength(strat->T[j].p);
532    }
533    #endif
534    ii = j;
535    /*
536     * the polynomial to reduce with (up to the moment) is;
537     * pi with length li
538     */
539
540    i = j;
541#if 1
542    if (TEST_OPT_LENGTH)
543    loop
544    {
545      /*- search the shortest possible with respect to length -*/
546      i++;
547      if (i > strat->tl)
548        break;
549      if (li<=1)
550        break;
551    #if 0
552      if (strat->T[i].pLength==0)
553      {
554        PrintS("!");
555        strat->T[i].pLength=pLength(strat->T[i].p);
556      }
557   #endif
558      if ((strat->T[i].pLength < li)
559         &&
560          p_LmShortDivisibleBy(strat->T[i].GetLmTailRing(), strat->sevT[i],
561                               h_p, not_sev, strat->tailRing))
562      {
563        /*
564         * the polynomial to reduce with is now;
565         */
566        PrintS("+");
567        li = strat->T[i].pLength;
568        ii = i;
569      }
570    }
571#endif
572
573    /*
574     * end of search: have to reduce with pi
575     */
576
577
578#ifdef KDEBUG
579    if (TEST_OPT_DEBUG)
580    {
581      PrintS("red:");
582      h->wrp();
583      PrintS(" with ");
584      strat->T[ii].wrp();
585    }
586#endif
587
588    ksReducePoly(h, &(strat->T[ii]), NULL, NULL, strat);
589
590#ifdef KDEBUG
591    if (TEST_OPT_DEBUG)
592    {
593      PrintS("\nto ");
594      h->wrp();
595      PrintLn();
596    }
597#endif
598
599    h_p=h->GetLmTailRing();
600
601    if (h_p == NULL)
602    {
603      if (h->lcm!=NULL) pLmFree(h->lcm);
604#ifdef KDEBUG
605      h->lcm=NULL;
606#endif
607      return 0;
608    }
609    h->SetShortExpVector();
610    not_sev = ~ h->sev;
611    d = h->SetpFDeg();
612    /*- try to reduce the s-polynomial -*/
613    pass++;
614    if (//!K_TEST_OPT_REDTHROUGH &&
615        (strat->Ll >= 0) && ((d > reddeg) || (pass > strat->LazyPass)))
616    {
617      h->SetLmCurrRing();
618      at = strat->posInL(strat->L,strat->Ll,h,strat);
619      if (at <= strat->Ll)
620      {
621#if 1
622        int dummy=strat->sl;
623        if (kFindDivisibleByInS(strat, &dummy, h) < 0)
624          return 1;
625#endif
626#ifdef KDEBUG
627        if (TEST_OPT_DEBUG) Print(" ->L[%d]\n",at);
628#endif
629        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
630        h->Clear();
631        return -1;
632      }
633    }
634    else if (d != reddeg)
635    {
636      if (d>=strat->tailRing->bitmask)
637      {
638        if (h->pTotalDeg() >= strat->tailRing->bitmask)
639        {
640          strat->overflow=TRUE;
641          //Print("OVERFLOW in redLazy d=%ld, max=%ld\n",d,strat->tailRing->bitmask);
642          h->GetP();
643          at = strat->posInL(strat->L,strat->Ll,h,strat);
644          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
645          h->Clear();
646          return -1;
647        }
648      }
649      else if ((TEST_OPT_PROT) && (strat->Ll < 0))
650      {
651        Print(".%d",d);mflush();
652        reddeg = d;
653      }
654    }
655  }
656}
657/*2
658*  reduction procedure for the sugar-strategy (honey)
659* reduces h with elements from T choosing first possible
660* element in T with respect to the given ecart
661*/
662int redHoney (LObject* h, kStrategy strat)
663{
664  if (strat->tl<0) return 1;
665  //if (h->GetLmTailRing()==NULL) return 0; // HS: SHOULD NOT BE NEEDED!
666  assume(h->FDeg == h->pFDeg());
667  poly h_p;
668  int i,j,at,pass,ei, ii, h_d;
669  unsigned long not_sev;
670  long reddeg,d;
671
672  pass = j = 0;
673  d = reddeg = h->GetpFDeg() + h->ecart;
674  h->SetShortExpVector();
675  int li;
676  h_p = h->GetLmTailRing();
677  not_sev = ~ h->sev;
678
679  h->PrepareRed(strat->use_buckets);
680  loop
681  {
682    j=kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
683    if (j < 0) return 1;
684
685    ei = strat->T[j].ecart;
686    li = strat->T[j].pLength;
687    ii = j;
688    /*
689     * the polynomial to reduce with (up to the moment) is;
690     * pi with ecart ei
691     */
692    i = j;
693    if (TEST_OPT_LENGTH)
694    loop
695    {
696      /*- takes the first possible with respect to ecart -*/
697      i++;
698      if (i > strat->tl)
699        break;
700      //if (ei < h->ecart)
701      //  break;
702      if (li<=1)
703        break;
704      if ((((strat->T[i].ecart < ei) && (ei> h->ecart))
705         || ((strat->T[i].ecart <= h->ecart) && (strat->T[i].pLength < li)))
706         &&
707          p_LmShortDivisibleBy(strat->T[i].GetLmTailRing(), strat->sevT[i],
708                               h_p, not_sev, strat->tailRing))
709      {
710        /*
711         * the polynomial to reduce with is now;
712         */
713        ei = strat->T[i].ecart;
714        li = strat->T[i].pLength;
715        ii = i;
716      }
717    }
718
719    /*
720     * end of search: have to reduce with pi
721     */
722    if (!K_TEST_OPT_REDTHROUGH && (pass!=0) && (ei > h->ecart))
723    {
724      h->GetTP(); // clears bucket
725      h->SetLmCurrRing();
726      /*
727       * It is not possible to reduce h with smaller ecart;
728       * if possible h goes to the lazy-set L,i.e
729       * if its position in L would be not the last one
730       */
731      if (strat->Ll >= 0) /* L is not empty */
732      {
733        at = strat->posInL(strat->L,strat->Ll,h,strat);
734        if(at <= strat->Ll)
735          /*- h will not become the next element to reduce -*/
736        {
737          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
738#ifdef KDEBUG
739          if (TEST_OPT_DEBUG) Print(" ecart too big: -> L%d\n",at);
740#endif
741          h->Clear();
742          return -1;
743        }
744      }
745    }
746#ifdef KDEBUG
747    if (TEST_OPT_DEBUG)
748    {
749      PrintS("red:");
750      h->wrp();
751      PrintS(" with ");
752      strat->T[ii].wrp();
753    }
754#endif
755    assume(strat->fromT == FALSE);
756
757    number coef;
758    ksReducePoly(h,&(strat->T[ii]),strat->kNoetherTail(),&coef,strat);
759#ifdef KDEBUG
760    if (TEST_OPT_DEBUG)
761    {
762      PrintS("\nto:");
763      h->wrp();
764      PrintLn();
765    }
766#endif
767    if(h->IsNull())
768    {
769      h->Clear();
770      if (h->lcm!=NULL) pLmFree(h->lcm);
771      #ifdef KDEBUG
772      h->lcm=NULL;
773      #endif
774      return 0;
775    }
776    h->SetShortExpVector();
777    not_sev = ~ h->sev;
778    h_d = h->SetpFDeg();
779    /* compute the ecart */
780    if (ei <= h->ecart)
781      h->ecart = d-h_d;
782    else
783      h->ecart = d-h_d+ei-h->ecart;
784
785    /*
786     * try to reduce the s-polynomial h
787     *test first whether h should go to the lazyset L
788     *-if the degree jumps
789     *-if the number of pre-defined reductions jumps
790     */
791    pass++;
792    d = h_d + h->ecart;
793    if (!K_TEST_OPT_REDTHROUGH && (strat->Ll >= 0) && ((d > reddeg) || (pass > strat->LazyPass)))
794    {
795      h->GetTP(); // clear bucket
796      h->SetLmCurrRing();
797      at = strat->posInL(strat->L,strat->Ll,h,strat);
798      if (at <= strat->Ll)
799      {
800        int dummy=strat->sl;
801        if (kFindDivisibleByInS(strat, &dummy, h) < 0)
802          return 1;
803        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
804#ifdef KDEBUG
805        if (TEST_OPT_DEBUG)
806          Print(" degree jumped: -> L%d\n",at);
807#endif
808        h->Clear();
809        return -1;
810      }
811    }
812    else if (d > reddeg)
813    {
814      if (d>=strat->tailRing->bitmask)
815      {
816        if (h->pTotalDeg()+h->ecart >= strat->tailRing->bitmask)
817        {
818          strat->overflow=TRUE;
819          //Print("OVERFLOW in redHoney d=%ld, max=%ld\n",d,strat->tailRing->bitmask);
820          h->GetP();
821          at = strat->posInL(strat->L,strat->Ll,h,strat);
822          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
823          h->Clear();
824          return -1;
825        }
826      }
827      else if (TEST_OPT_PROT && (strat->Ll < 0) )
828      {
829        //h->wrp(); Print("<%d>\n",h->GetpLength());
830        reddeg = d;
831        Print(".%ld",d); mflush();
832      }
833    }
834  }
835}
836/*2
837*  reduction procedure for the normal form
838*/
839
840poly redNF (poly h,int &max_ind,int nonorm,kStrategy strat)
841{
842  if (h==NULL) return NULL;
843  int j;
844  max_ind=strat->sl;
845
846  if (0 > strat->sl)
847  {
848    return h;
849  }
850  LObject P(h);
851  P.SetShortExpVector();
852  P.bucket = kBucketCreate(currRing);
853  kBucketInit(P.bucket,P.p,pLength(P.p));
854  kbTest(P.bucket);
855#ifdef HAVE_RINGS
856  BOOLEAN is_ring = rField_is_Ring(currRing);
857#endif
858  loop
859  {
860    j=kFindDivisibleByInS(strat,&max_ind,&P);
861    if (j>=0)
862    {
863#ifdef HAVE_RINGS
864      if (!is_ring)
865      {
866#endif
867        int sl=pSize(strat->S[j]);
868        int jj=j;
869        loop
870        {
871          int sll;
872          jj=kFindNextDivisibleByInS(strat,jj+1,max_ind,&P);
873          if (jj<0) break;
874          sll=pSize(strat->S[jj]);
875          if (sll<sl)
876          {
877            #ifdef KDEBUG
878            if (TEST_OPT_DEBUG) Print("better(S%d:%d -> S%d:%d)\n",j,sl,jj,sll);
879            #endif
880            //else if (TEST_OPT_PROT) { PrintS("b"); mflush(); }
881            j=jj;
882            sl=sll;
883          }
884        }
885        if ((nonorm==0) && (!nIsOne(pGetCoeff(strat->S[j]))))
886        {
887          pNorm(strat->S[j]);
888          //if (TEST_OPT_PROT) { PrintS("n"); mflush(); }
889        }
890#ifdef HAVE_RINGS
891      }
892#endif
893      nNormalize(pGetCoeff(P.p));
894#ifdef KDEBUG
895      if (TEST_OPT_DEBUG)
896      {
897        PrintS("red:");
898        wrp(h);
899        PrintS(" with ");
900        wrp(strat->S[j]);
901      }
902#endif
903#ifdef HAVE_PLURAL
904      if (rIsPluralRing(currRing))
905      {
906        number coef;
907        nc_kBucketPolyRed(P.bucket,strat->S[j],&coef);
908        nDelete(&coef);
909      }
910      else
911#endif
912      {
913        number coef;
914        coef=kBucketPolyRed(P.bucket,strat->S[j],pLength(strat->S[j]),strat->kNoether);
915        nDelete(&coef);
916      }
917      h = kBucketGetLm(P.bucket);   // FRAGE OLIVER
918      if (h==NULL)
919      {
920        kBucketDestroy(&P.bucket);
921        return NULL;
922      }
923      kbTest(P.bucket);
924      P.p=h;
925      P.t_p=NULL;
926      P.SetShortExpVector();
927#ifdef KDEBUG
928      if (TEST_OPT_DEBUG)
929      {
930        PrintS("\nto:");
931        wrp(h);
932        PrintLn();
933      }
934#endif
935    }
936    else
937    {
938      P.p=kBucketClear(P.bucket);
939      kBucketDestroy(&P.bucket);
940      pNormalize(P.p);
941      return P.p;
942    }
943  }
944}
945
946#ifdef KDEBUG
947static int bba_count = 0;
948#endif
949void kDebugPrint(kStrategy strat);
950
951ideal bba (ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat)
952{
953#ifdef KDEBUG
954  bba_count++;
955  int loop_count = 0;
956#endif
957  om_Opts.MinTrack = 5;
958  int   srmax,lrmax, red_result = 1;
959  int   olddeg,reduc;
960  int hilbeledeg=1,hilbcount=0,minimcnt=0;
961  BOOLEAN withT = FALSE;
962
963  initBuchMoraCrit(strat); /*set Gebauer, honey, sugarCrit*/
964  initBuchMoraPos(strat);
965  initHilbCrit(F,Q,&hilb,strat);
966  initBba(F,strat);
967  /*set enterS, spSpolyShort, reduce, red, initEcart, initEcartPair*/
968  /*Shdl=*/initBuchMora(F, Q,strat);
969  if (strat->minim>0) strat->M=idInit(IDELEMS(F),F->rank);
970  srmax = strat->sl;
971  reduc = olddeg = lrmax = 0;
972
973#ifndef NO_BUCKETS
974  if (!TEST_OPT_NOT_BUCKETS)
975    strat->use_buckets = 1;
976#endif
977
978  // redtailBBa against T for inhomogenous input
979  if (!K_TEST_OPT_OLDSTD)
980    withT = ! strat->homog;
981
982  // strat->posInT = posInT_pLength;
983  kTest_TS(strat);
984
985#ifdef HAVE_TAIL_RING
986  kStratInitChangeTailRing(strat);
987#endif
988  if (BVERBOSE(23))
989  {
990    if (test_PosInT!=NULL) strat->posInT=test_PosInT;
991    if (test_PosInL!=NULL) strat->posInL=test_PosInL;
992    kDebugPrint(strat);
993  }
994
995
996  /* compute------------------------------------------------------- */
997  while (strat->Ll >= 0)
998  {
999    if (strat->Ll > lrmax) lrmax =strat->Ll;/*stat.*/
1000    #ifdef KDEBUG
1001      loop_count++;
1002      #ifdef HAVE_RINGS
1003        if (TEST_OPT_DEBUG) PrintS("--- next step ---\n");
1004      #endif
1005      if (TEST_OPT_DEBUG) messageSets(strat);
1006    #endif
1007    if (strat->Ll== 0) strat->interpt=TRUE;
1008    if (TEST_OPT_DEGBOUND
1009        && ((strat->honey && (strat->L[strat->Ll].ecart+pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
1010            || ((!strat->honey) && (pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))))
1011    {
1012      /*
1013       *stops computation if
1014       * 24 IN test and the degree +ecart of L[strat->Ll] is bigger then
1015       *a predefined number Kstd1_deg
1016       */
1017      while ((strat->Ll >= 0)
1018        && (strat->L[strat->Ll].p1!=NULL) && (strat->L[strat->Ll].p2!=NULL)
1019        && ((strat->honey && (strat->L[strat->Ll].ecart+pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
1020            || ((!strat->honey) && (pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg)))
1021        )
1022        deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
1023      if (strat->Ll<0) break;
1024      else strat->noClearS=TRUE;
1025    }
1026    /* picks the last element from the lazyset L */
1027    strat->P = strat->L[strat->Ll];
1028    strat->Ll--;
1029
1030    if (pNext(strat->P.p) == strat->tail)
1031    {
1032      // deletes the short spoly
1033#ifdef HAVE_RINGS
1034      if (rField_is_Ring(currRing))
1035        pLmDelete(strat->P.p);
1036      else
1037#endif
1038        pLmFree(strat->P.p);
1039      strat->P.p = NULL;
1040      poly m1 = NULL, m2 = NULL;
1041
1042      // check that spoly creation is ok
1043      while (strat->tailRing != currRing &&
1044             !kCheckSpolyCreation(&(strat->P), strat, m1, m2))
1045      {
1046        assume(m1 == NULL && m2 == NULL);
1047        // if not, change to a ring where exponents are at least
1048        // large enough
1049        if (!kStratChangeTailRing(strat))
1050        {
1051          WerrorS("OVERFLOW..."); 
1052          break;
1053        }
1054      }
1055      // create the real one
1056      ksCreateSpoly(&(strat->P), NULL, strat->use_buckets,
1057                    strat->tailRing, m1, m2, strat->R);
1058    }
1059    else if (strat->P.p1 == NULL)
1060    {
1061      if (strat->minim > 0)
1062        strat->P.p2=p_Copy(strat->P.p, currRing, strat->tailRing);
1063      // for input polys, prepare reduction
1064      strat->P.PrepareRed(strat->use_buckets);
1065    }
1066
1067    if (strat->P.p == NULL && strat->P.t_p == NULL)
1068    {
1069      red_result = 0;
1070    }
1071    else
1072    {
1073      if (TEST_OPT_PROT)
1074        message((strat->honey ? strat->P.ecart : 0) + strat->P.pFDeg(),
1075                &olddeg,&reduc,strat, red_result);
1076
1077      /* reduction of the element choosen from L */
1078      red_result = strat->red(&strat->P,strat);
1079      if (errorreported)  break;
1080    }
1081
1082    if (strat->overflow)
1083    {
1084        if (!kStratChangeTailRing(strat)) { Werror("OVERFLOW.."); break;}
1085    }
1086
1087    // reduction to non-zero new poly
1088    if (red_result == 1)
1089    {
1090      // get the polynomial (canonicalize bucket, make sure P.p is set)
1091      strat->P.GetP(strat->lmBin);
1092
1093      /* statistic */
1094      if (TEST_OPT_PROT) PrintS("s");
1095
1096      int pos=posInS(strat,strat->sl,strat->P.p,strat->P.ecart);
1097
1098      // reduce the tail and normalize poly
1099      // in the ring case we cannot expect LC(f) = 1,
1100      // therefore we call pContent instead of pNorm
1101      if ((TEST_OPT_INTSTRATEGY) || (rField_is_Ring(currRing)))
1102      {
1103        strat->P.pCleardenom();
1104        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
1105        {
1106          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
1107          strat->P.pCleardenom();
1108        }
1109      }
1110      else
1111      {
1112        strat->P.pNorm();
1113        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
1114          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
1115      }
1116
1117#ifdef KDEBUG
1118      if (TEST_OPT_DEBUG){PrintS("new s:");strat->P.wrp();PrintLn();}
1119#endif
1120
1121      // min_std stuff
1122      if ((strat->P.p1==NULL) && (strat->minim>0))
1123      {
1124        if (strat->minim==1)
1125        {
1126          strat->M->m[minimcnt]=p_Copy(strat->P.p,currRing,strat->tailRing);
1127          p_Delete(&strat->P.p2, currRing, strat->tailRing);
1128        }
1129        else
1130        {
1131          strat->M->m[minimcnt]=strat->P.p2;
1132          strat->P.p2=NULL;
1133        }
1134        if (strat->tailRing!=currRing && pNext(strat->M->m[minimcnt])!=NULL)
1135          pNext(strat->M->m[minimcnt])
1136            = strat->p_shallow_copy_delete(pNext(strat->M->m[minimcnt]),
1137                                           strat->tailRing, currRing,
1138                                           currRing->PolyBin);
1139        minimcnt++;
1140      }
1141
1142      // enter into S, L, and T
1143      //if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
1144        enterT(strat->P, strat);
1145#ifdef HAVE_RINGS
1146      if (rField_is_Ring(currRing))
1147        superenterpairs(strat->P.p,strat->sl,strat->P.ecart,pos,strat, strat->tl);
1148      else
1149#endif
1150        enterpairs(strat->P.p,strat->sl,strat->P.ecart,pos,strat, strat->tl);
1151      // posInS only depends on the leading term
1152      if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
1153      {
1154      strat->enterS(strat->P, pos, strat, strat->tl);
1155      }
1156      else
1157      {
1158      //  strat->P.Delete(); // syzComp test: it is in T
1159      }
1160#if 0
1161      int pl=pLength(strat->P.p);
1162      if (pl==1)
1163      {
1164        //if (TEST_OPT_PROT)
1165        //PrintS("<1>");
1166      }
1167      else if (pl==2)
1168      {
1169        //if (TEST_OPT_PROT)
1170        //PrintS("<2>");
1171      }
1172#endif
1173      if (hilb!=NULL) khCheck(Q,w,hilb,hilbeledeg,hilbcount,strat);
1174//      Print("[%d]",hilbeledeg);
1175      if (strat->P.lcm!=NULL)
1176#ifdef HAVE_RINGS
1177        pLmDelete(strat->P.lcm);
1178#else
1179        pLmFree(strat->P.lcm);
1180#endif
1181      if (strat->sl>srmax) srmax = strat->sl;
1182    }
1183    else if (strat->P.p1 == NULL && strat->minim > 0)
1184    {
1185      p_Delete(&strat->P.p2, currRing, strat->tailRing);
1186    }
1187
1188#ifdef KDEBUG
1189    memset(&(strat->P), 0, sizeof(strat->P));
1190#endif
1191    kTest_TS(strat);
1192  }
1193#ifdef KDEBUG
1194  if (TEST_OPT_DEBUG) messageSets(strat);
1195#endif
1196  if (TEST_OPT_SB_1)
1197  {
1198    int k=1;
1199    int j;
1200    while(k<=strat->sl)
1201    {
1202      j=0;
1203      loop
1204      {
1205        if (j>=k) break;
1206        clearS(strat->S[j],strat->sevS[j],&k,&j,strat);
1207        j++;
1208      }
1209      k++;
1210    }
1211  }
1212
1213  /* complete reduction of the standard basis--------- */
1214  if (TEST_OPT_REDSB)
1215  {
1216    completeReduce(strat);
1217#ifdef HAVE_TAIL_RING
1218    if (strat->completeReduce_retry)
1219    {
1220      // completeReduce needed larger exponents, retry
1221      // to reduce with S (instead of T)
1222      // and in currRing (instead of strat->tailRing)
1223      cleanT(strat);strat->tailRing=currRing;
1224      int i;
1225      for(i=strat->sl;i>=0;i--) strat->S_2_R[i]=-1;
1226      completeReduce(strat);
1227    }
1228#endif
1229  }
1230  else if (TEST_OPT_PROT) PrintLn();
1231
1232  /* release temp data-------------------------------- */
1233  exitBuchMora(strat);
1234  if (TEST_OPT_WEIGHTM)
1235  {
1236    pRestoreDegProcs(pFDegOld, pLDegOld);
1237    if (ecartWeights)
1238    {
1239      omFreeSize((ADDRESS)ecartWeights,(pVariables+1)*sizeof(short));
1240      ecartWeights=NULL;
1241    }
1242  }
1243  if (TEST_OPT_PROT) messageStat(srmax,lrmax,hilbcount,strat);
1244  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
1245  return (strat->Shdl);
1246}
1247
1248poly kNF2 (ideal F,ideal Q,poly q,kStrategy strat, int lazyReduce)
1249{
1250  assume(q!=NULL);
1251  assume(!(idIs0(F)&&(Q==NULL)));
1252
1253// lazy_reduce flags: can be combined by |
1254//#define KSTD_NF_LAZY   1
1255  // do only a reduction of the leading term
1256//#define KSTD_NF_NONORM 4
1257  // only global: avoid normalization, return a multiply of NF
1258  poly   p;
1259  int   i;
1260
1261  //if ((idIs0(F))&&(Q==NULL))
1262  //  return pCopy(q); /*F=0*/
1263  //strat->ak = idRankFreeModule(F);
1264  /*- creating temp data structures------------------- -*/
1265  BITSET save_test=test;
1266  test|=Sy_bit(OPT_REDTAIL);
1267  initBuchMoraCrit(strat);
1268  strat->initEcart = initEcartBBA;
1269  strat->enterS = enterSBba;
1270#ifndef NO_BUCKETS
1271  strat->use_buckets = (!TEST_OPT_NOT_BUCKETS) && (!rIsPluralRing(currRing));
1272#endif
1273  /*- set S -*/
1274  strat->sl = -1;
1275  /*- init local data struct.---------------------------------------- -*/
1276  /*Shdl=*/initS(F,Q,strat);
1277  /*- compute------------------------------------------------------- -*/
1278  //if ((TEST_OPT_INTSTRATEGY)&&(lazyReduce==0))
1279  //{
1280  //  for (i=strat->sl;i>=0;i--)
1281  //    pNorm(strat->S[i]);
1282  //}
1283  kTest(strat);
1284  if (TEST_OPT_PROT) { PrintS("r"); mflush(); }
1285  int max_ind;
1286  p = redNF(pCopy(q),max_ind,lazyReduce & KSTD_NF_NONORM,strat);
1287  if ((p!=NULL)&&((lazyReduce & KSTD_NF_LAZY)==0))
1288  {
1289    if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
1290    #ifdef HAVE_RINGS
1291    if (rField_is_Ring())
1292    {
1293      p = redtailBba_Z(p,max_ind,strat);
1294    }
1295    else
1296    #endif
1297    {
1298      BITSET save=test;
1299      test &= ~Sy_bit(OPT_INTSTRATEGY);
1300      p = redtailBba(p,max_ind,strat,(lazyReduce & KSTD_NF_NONORM)==0);
1301      test=save;
1302    }
1303  }
1304  /*- release temp data------------------------------- -*/
1305  omfree(strat->sevS);
1306  omfree(strat->ecartS);
1307  omfree(strat->T);
1308  omfree(strat->sevT);
1309  omfree(strat->R);
1310  omfree(strat->S_2_R);
1311  omfree(strat->L);
1312  omfree(strat->B);
1313  omfree(strat->fromQ);
1314  idDelete(&strat->Shdl);
1315  test=save_test;
1316  if (TEST_OPT_PROT) PrintLn();
1317  return p;
1318}
1319
1320ideal kNF2 (ideal F,ideal Q,ideal q,kStrategy strat, int lazyReduce)
1321{
1322  assume(!idIs0(q));
1323  assume(!(idIs0(F)&&(Q==NULL)));
1324// lazy_reduce flags: can be combined by |
1325//#define KSTD_NF_LAZY   1
1326  // do only a reduction of the leading term
1327//#define KSTD_NF_NONORM 4
1328  // only global: avoid normalization, return a multiply of NF
1329  poly   p;
1330  int   i;
1331  ideal res;
1332  int max_ind;
1333
1334  //if (idIs0(q))
1335  //  return idInit(IDELEMS(q),si_max(q->rank,F->rank));
1336  //if ((idIs0(F))&&(Q==NULL))
1337  //  return idCopy(q); /*F=0*/
1338  //strat->ak = idRankFreeModule(F);
1339  /*- creating temp data structures------------------- -*/
1340  BITSET save_test=test;
1341  test|=Sy_bit(OPT_REDTAIL);
1342  initBuchMoraCrit(strat);
1343  strat->initEcart = initEcartBBA;
1344  strat->enterS = enterSBba;
1345  /*- set S -*/
1346  strat->sl = -1;
1347#ifndef NO_BUCKETS
1348  strat->use_buckets = (!TEST_OPT_NOT_BUCKETS) && (!rIsPluralRing(currRing));
1349#endif
1350  /*- init local data struct.---------------------------------------- -*/
1351  /*Shdl=*/initS(F,Q,strat);
1352  /*- compute------------------------------------------------------- -*/
1353  res=idInit(IDELEMS(q),si_max(q->rank,F->rank));
1354  BITSET save=test;
1355  test &= ~Sy_bit(OPT_INTSTRATEGY);
1356  for (i=IDELEMS(q)-1; i>=0; i--)
1357  {
1358    if (q->m[i]!=NULL)
1359    {
1360      if (TEST_OPT_PROT) { PrintS("r");mflush(); }
1361      p = redNF(pCopy(q->m[i]),max_ind,lazyReduce & KSTD_NF_NONORM,strat);
1362      if ((p!=NULL)&&((lazyReduce & KSTD_NF_LAZY)==0))
1363      {
1364        if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
1365        #ifdef HAVE_RINGS
1366        if (rField_is_Ring())
1367        {
1368          p = redtailBba_Z(p,max_ind,strat);
1369        }
1370        else
1371        #endif
1372        {
1373          p = redtailBba(p,max_ind,strat,(lazyReduce & KSTD_NF_NONORM)==0);
1374        }
1375      }
1376      res->m[i]=p;
1377    }
1378    //else
1379    //  res->m[i]=NULL;
1380  }
1381  /*- release temp data------------------------------- -*/
1382  test=save;
1383  omfree(strat->sevS);
1384  omfree(strat->ecartS);
1385  omfree(strat->T);
1386  omfree(strat->sevT);
1387  omfree(strat->R);
1388  omfree(strat->S_2_R);
1389  omfree(strat->L);
1390  omfree(strat->B);
1391  omfree(strat->fromQ);
1392  idDelete(&strat->Shdl);
1393  test=save_test;
1394  if (TEST_OPT_PROT) PrintLn();
1395  return res;
1396}
1397
1398/* shiftgb stuff */
1399#ifdef HAVE_SHIFTBBA
1400
1401
1402ideal bbaShift(ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat, int uptodeg, int lV)
1403{
1404#ifdef KDEBUG
1405  bba_count++;
1406  int loop_count = 0;
1407#endif
1408  om_Opts.MinTrack = 5;
1409  int   srmax,lrmax, red_result = 1;
1410  int   olddeg,reduc;
1411  int hilbeledeg=1,hilbcount=0,minimcnt=0;
1412  BOOLEAN withT = TRUE; // very important for shifts
1413
1414  initBuchMoraCrit(strat); /*set Gebauer, honey, sugarCrit, NO CHANGES */
1415  initBuchMoraPos(strat); /*NO CHANGES YET: perhaps later*/
1416  initHilbCrit(F,Q,&hilb,strat); /*NO CHANGES*/
1417  initBbaShift(F,strat); /* DONE */
1418  /*set enterS, spSpolyShort, reduce, red, initEcart, initEcartPair*/
1419  /*Shdl=*/initBuchMoraShift(F, Q,strat); /* updateS with no toT, i.e. no init for T */
1420  updateSShift(strat,uptodeg,lV); /* initializes T */
1421
1422  if (strat->minim>0) strat->M=idInit(IDELEMS(F),F->rank);
1423  srmax = strat->sl;
1424  reduc = olddeg = lrmax = 0;
1425  strat->lV=lV;
1426
1427#ifndef NO_BUCKETS
1428  if (!TEST_OPT_NOT_BUCKETS)
1429    strat->use_buckets = 1;
1430#endif
1431
1432  // redtailBBa against T for inhomogenous input
1433  //  if (!K_TEST_OPT_OLDSTD)
1434  //    withT = ! strat->homog;
1435
1436  // strat->posInT = posInT_pLength;
1437  kTest_TS(strat);
1438
1439#ifdef HAVE_TAIL_RING
1440  kStratInitChangeTailRing(strat);
1441#endif
1442
1443  /* compute------------------------------------------------------- */
1444  while (strat->Ll >= 0)
1445  {
1446    if (strat->Ll > lrmax) lrmax =strat->Ll;/*stat.*/
1447#ifdef KDEBUG
1448    loop_count++;
1449    if (TEST_OPT_DEBUG) messageSets(strat);
1450#endif
1451    if (strat->Ll== 0) strat->interpt=TRUE;
1452    if (TEST_OPT_DEGBOUND
1453        && ((strat->honey && (strat->L[strat->Ll].ecart+pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
1454            || ((!strat->honey) && (pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))))
1455    {
1456      /*
1457       *stops computation if
1458       * 24 IN test and the degree +ecart of L[strat->Ll] is bigger then
1459       *a predefined number Kstd1_deg
1460       */
1461      while ((strat->Ll >= 0)
1462        && (strat->L[strat->Ll].p1!=NULL) && (strat->L[strat->Ll].p2!=NULL)
1463        && ((strat->honey && (strat->L[strat->Ll].ecart+pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
1464            || ((!strat->honey) && (pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg)))
1465        )
1466        deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
1467      if (strat->Ll<0) break;
1468      else strat->noClearS=TRUE;
1469    }
1470    /* picks the last element from the lazyset L */
1471    strat->P = strat->L[strat->Ll];
1472    strat->Ll--;
1473
1474    if (pNext(strat->P.p) == strat->tail)
1475    {
1476      // deletes the short spoly
1477      pLmFree(strat->P.p);
1478      strat->P.p = NULL;
1479      poly m1 = NULL, m2 = NULL;
1480
1481      // check that spoly creation is ok
1482      while (strat->tailRing != currRing &&
1483             !kCheckSpolyCreation(&(strat->P), strat, m1, m2))
1484      {
1485        assume(m1 == NULL && m2 == NULL);
1486        // if not, change to a ring where exponents are at least
1487        // large enough
1488        kStratChangeTailRing(strat);
1489      }
1490      // create the real one
1491      ksCreateSpoly(&(strat->P), NULL, strat->use_buckets,
1492                    strat->tailRing, m1, m2, strat->R);
1493    }
1494    else if (strat->P.p1 == NULL)
1495    {
1496      if (strat->minim > 0)
1497        strat->P.p2=p_Copy(strat->P.p, currRing, strat->tailRing);
1498      // for input polys, prepare reduction
1499      strat->P.PrepareRed(strat->use_buckets);
1500    }
1501
1502    poly qq;
1503
1504    /* here in the nonhomog case we shrink the new spoly */
1505
1506    if ( ! strat->homog)
1507    {
1508      strat->P.GetP(strat->lmBin); // because shifts are counted with .p structure
1509      /* in the nonhomog case we have to shrink the polynomial */
1510      assume(strat->P.t_p!=NULL);
1511      qq = p_Shrink(strat->P.t_p, lV, strat->tailRing); // direct shrink
1512      if (qq != NULL)
1513      {
1514         /* we're here if Shrink is nonzero */
1515        //         strat->P.p =  NULL;
1516        //        strat->P.Delete(); /* deletes P.p and P.t_p */ //error
1517        strat->P.p   =  NULL; // is not set by Delete
1518        strat->P.t_p =  qq;
1519        strat->P.GetP(strat->lmBin);
1520        // update sev and length
1521        strat->initEcart(&(strat->P));
1522        strat->P.sev = pGetShortExpVector(strat->P.p);
1523//         strat->P.FDeg = strat->P.pFDeg();
1524//         strat->P.length = strat->P.pLDeg();
1525//         strat->P.pLength =strat->P.GetpLength(); //pLength(strat->P.p);
1526      }
1527      else
1528      {
1529         /* Shrink is zero, like y(1)*y(2) - y(1)*y(3)*/
1530#ifdef KDEBUG
1531         if (TEST_OPT_DEBUG){PrintS("nonzero s shrinks to 0");PrintLn();}
1532#endif
1533         //         strat->P.Delete();  // cause error
1534         strat->P.p = NULL;
1535         strat->P.t_p = NULL;
1536           //         strat->P.p = NULL; // or delete strat->P.p ?
1537       }
1538    }
1539      /* end shrinking poly in the nonhomog case */
1540
1541    if (strat->P.p == NULL && strat->P.t_p == NULL)
1542    {
1543      red_result = 0;
1544    }
1545    else
1546    {
1547      if (TEST_OPT_PROT)
1548        message((strat->honey ? strat->P.ecart : 0) + strat->P.pFDeg(),
1549                &olddeg,&reduc,strat, red_result);
1550
1551      /* reduction of the element choosen from L */
1552      red_result = strat->red(&strat->P,strat);
1553    }
1554
1555    // reduction to non-zero new poly
1556    if (red_result == 1)
1557    {
1558      /* statistic */
1559      if (TEST_OPT_PROT) PrintS("s");
1560
1561      // get the polynomial (canonicalize bucket, make sure P.p is set)
1562      strat->P.GetP(strat->lmBin);
1563
1564      int pos=posInS(strat,strat->sl,strat->P.p,strat->P.ecart);
1565
1566      // reduce the tail and normalize poly
1567      if (TEST_OPT_INTSTRATEGY)
1568      {
1569        strat->P.pCleardenom();
1570        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
1571        {
1572          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
1573          strat->P.pCleardenom();
1574        }
1575      }
1576      else
1577      {
1578        strat->P.pNorm();
1579        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
1580          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
1581      }
1582
1583      // here we must shrink again! and optionally reduce again
1584      // or build shrink into redtailBba!
1585
1586#ifdef KDEBUG
1587      if (TEST_OPT_DEBUG){PrintS("new s:");strat->P.wrp();PrintLn();}
1588#endif
1589
1590      // min_std stuff
1591      if ((strat->P.p1==NULL) && (strat->minim>0))
1592      {
1593        if (strat->minim==1)
1594        {
1595          strat->M->m[minimcnt]=p_Copy(strat->P.p,currRing,strat->tailRing);
1596          p_Delete(&strat->P.p2, currRing, strat->tailRing);
1597        }
1598        else
1599        {
1600          strat->M->m[minimcnt]=strat->P.p2;
1601          strat->P.p2=NULL;
1602        }
1603        if (strat->tailRing!=currRing && pNext(strat->M->m[minimcnt])!=NULL)
1604          pNext(strat->M->m[minimcnt])
1605            = strat->p_shallow_copy_delete(pNext(strat->M->m[minimcnt]),
1606                                           strat->tailRing, currRing,
1607                                           currRing->PolyBin);
1608        minimcnt++;
1609      }
1610
1611    /* here in the nonhomog case we shrink the reduced poly AGAIN */
1612
1613    if ( ! strat->homog)
1614    {
1615      strat->P.GetP(strat->lmBin); // because shifts are counted with .p structure
1616      /* assume strat->P.t_p != NULL */
1617      /* in the nonhomog case we have to shrink the polynomial */
1618      assume(strat->P.t_p!=NULL); // poly qq defined above
1619      qq = p_Shrink(strat->P.t_p, lV, strat->tailRing); // direct shrink
1620      if (qq != NULL)
1621      {
1622         /* we're here if Shrink is nonzero */
1623        //         strat->P.p =  NULL;
1624        //        strat->P.Delete(); /* deletes P.p and P.t_p */ //error
1625        strat->P.p   =  NULL; // is not set by Delete
1626        strat->P.t_p =  qq;
1627        strat->P.GetP(strat->lmBin);
1628        // update sev and length
1629        strat->initEcart(&(strat->P));
1630        strat->P.sev = pGetShortExpVector(strat->P.p);
1631      }
1632      else
1633      {
1634         /* Shrink is zero, like y(1)*y(2) - y(1)*y(3)*/
1635#ifdef PDEBUG
1636         if (TEST_OPT_DEBUG){PrintS("nonzero s shrinks to 0");PrintLn();}
1637#endif
1638         //         strat->P.Delete();  // cause error
1639         strat->P.p = NULL;
1640         strat->P.t_p = NULL;
1641           //         strat->P.p = NULL; // or delete strat->P.p ?
1642         goto     red_shrink2zero;
1643       }
1644    }
1645      /* end shrinking poly AGAIN in the nonhomog case */
1646
1647
1648      // enter into S, L, and T
1649      //if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
1650      //        enterT(strat->P, strat); // this was here before Shift stuff
1651      //enterTShift(LObject p, kStrategy strat, int atT, int uptodeg, int lV); // syntax
1652      // the default value for atT = -1 as in bba
1653      /*   strat->P.GetP(); */
1654      // because shifts are counted with .p structure // done before, but ?
1655      enterTShift(strat->P,strat,-1,uptodeg, lV);
1656      enterpairsShift(strat->P.p,strat->sl,strat->P.ecart,pos,strat, strat->tl,uptodeg,lV);
1657      //      enterpairsShift(vw,strat->sl,strat->P.ecart,pos,strat, strat->tl,uptodeg,lV);
1658      // posInS only depends on the leading term
1659      if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
1660      {
1661        strat->enterS(strat->P, pos, strat, strat->tl);
1662      }
1663      else
1664      {
1665      //  strat->P.Delete(); // syzComp test: it is in T
1666      }
1667
1668      if (hilb!=NULL) khCheck(Q,w,hilb,hilbeledeg,hilbcount,strat);
1669//      Print("[%d]",hilbeledeg);
1670      if (strat->P.lcm!=NULL) pLmFree(strat->P.lcm);
1671      if (strat->sl>srmax) srmax = strat->sl;
1672    }
1673    else
1674    {
1675    red_shrink2zero:
1676      if (strat->P.p1 == NULL && strat->minim > 0)
1677      {
1678        p_Delete(&strat->P.p2, currRing, strat->tailRing);
1679      }
1680    }
1681#ifdef KDEBUG
1682    memset(&(strat->P), 0, sizeof(strat->P));
1683#endif
1684    kTest_TS(strat);
1685  }
1686#ifdef KDEBUG
1687  if (TEST_OPT_DEBUG) messageSets(strat);
1688#endif
1689  /* complete reduction of the standard basis--------- */
1690  /*  shift case: look for elt's in S such that they are divisible by elt in T */
1691  //  if (TEST_OPT_SB_1)
1692  if (TEST_OPT_REDSB)
1693  {
1694    int k=0;
1695    int j=-1;
1696    while(k<=strat->sl)
1697    {
1698//       loop
1699//       {
1700//         if (j>=k) break;
1701//         clearS(strat->S[j],strat->sevS[j],&k,&j,strat);
1702//         j++;
1703//       }
1704      LObject Ln (strat->S[k],currRing, strat->tailRing);
1705      Ln.SetShortExpVector();
1706      j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, &Ln, j+1);
1707      if (j<0) {  k++; j=-1;}
1708      else
1709      {
1710        if ( pLmCmp(strat->S[k],strat->T[j].p) == 0)
1711        {
1712          j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, &Ln, j+1);
1713          if (j<0) {  k++; j=-1;}
1714          else
1715          {
1716            deleteInS(k,strat);
1717          }
1718        }
1719        else
1720        {
1721          deleteInS(k,strat);
1722        }
1723      }
1724    }
1725  }
1726
1727  if (TEST_OPT_REDSB)
1728  {    completeReduce(strat, TRUE); //shift: withT = TRUE
1729    if (strat->completeReduce_retry)
1730    {
1731      // completeReduce needed larger exponents, retry
1732      // to reduce with S (instead of T)
1733      // and in currRing (instead of strat->tailRing)
1734      cleanT(strat);strat->tailRing=currRing;
1735      int i;
1736      for(i=strat->sl;i>=0;i--) strat->S_2_R[i]=-1;
1737      completeReduce(strat, TRUE);
1738    }
1739  }
1740  else if (TEST_OPT_PROT) PrintLn();
1741
1742  /* release temp data-------------------------------- */
1743  exitBuchMora(strat);
1744  if (TEST_OPT_WEIGHTM)
1745  {
1746    pRestoreDegProcs(pFDegOld, pLDegOld);
1747    if (ecartWeights)
1748    {
1749      omFreeSize((ADDRESS)ecartWeights,(pVariables+1)*sizeof(short));
1750      ecartWeights=NULL;
1751    }
1752  }
1753  if (TEST_OPT_PROT) messageStat(srmax,lrmax,hilbcount,strat);
1754  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
1755  return (strat->Shdl);
1756}
1757
1758
1759ideal freegb(ideal I, int uptodeg, int lVblock)
1760{
1761  /* todo main call */
1762
1763  /* assume: ring is prepared, ideal is copied into shifted ring */
1764  /* uptodeg and lVblock are correct - test them! */
1765
1766  /* check whether the ideal is in V */
1767
1768//  if (0)
1769  if (! ideal_isInV(I,lVblock) )
1770  {
1771    WerrorS("The input ideal contains incorrectly encoded elements! ");
1772    return(NULL);
1773  }
1774
1775  //  kStrategy strat = new skStrategy;
1776  /* ideal bbaShift(ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat, int uptodeg, int lV) */
1777  /* at the moment:
1778- no quotient (check)
1779- no *w, no *hilb
1780  */
1781  /* ideal F, ideal Q, tHomog h,intvec ** w, intvec *hilb,int syzComp,
1782     int newIdeal, intvec *vw) */
1783  ideal RS = kStdShift(I,NULL, testHomog, NULL,NULL,0,0,NULL, uptodeg, lVblock);
1784    //bbaShift(I,NULL, NULL, NULL, strat, uptodeg, lVblock);
1785  idSkipZeroes(RS);
1786  return(RS);
1787}
1788
1789/*2
1790*reduces h with elements from T choosing  the first possible
1791* element in t with respect to the given pDivisibleBy
1792*/
1793int redFirstShift (LObject* h,kStrategy strat)
1794{
1795  if (h->IsNull()) return 0;
1796
1797  int at, reddeg,d;
1798  int pass = 0;
1799  int j = 0;
1800
1801  if (! strat->homog)
1802  {
1803    d = h->GetpFDeg() + h->ecart;
1804    reddeg = strat->LazyDegree+d;
1805  }
1806  h->SetShortExpVector();
1807  loop
1808  {
1809    j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
1810    if (j < 0)
1811    {
1812      h->SetDegStuffReturnLDeg(strat->LDegLast);
1813      return 1;
1814    }
1815
1816    if (!TEST_OPT_INTSTRATEGY)
1817      strat->T[j].pNorm();
1818#ifdef KDEBUG
1819    if (TEST_OPT_DEBUG)
1820    {
1821      PrintS("reduce ");
1822      h->wrp();
1823      PrintS(" with ");
1824      strat->T[j].wrp();
1825    }
1826#endif
1827    ksReducePoly(h, &(strat->T[j]), strat->kNoetherTail(), NULL, strat);
1828    if (!h->IsNull())
1829    {
1830      poly qq=p_Shrink(h->GetTP(),strat->lV,strat->tailRing);
1831      h->p=NULL;
1832      h->t_p=qq;
1833      if (qq!=NULL) h->GetP(strat->lmBin);
1834    }
1835
1836#ifdef KDEBUG
1837    if (TEST_OPT_DEBUG)
1838    {
1839      PrintS(" to ");
1840      wrp(h->p);
1841      PrintLn();
1842    }
1843#endif
1844    if (h->IsNull())
1845    {
1846      if (h->lcm!=NULL) pLmFree(h->lcm);
1847      h->Clear();
1848      return 0;
1849    }
1850    h->SetShortExpVector();
1851
1852#if 0
1853    if ((strat->syzComp!=0) && !strat->honey)
1854    {
1855      if ((strat->syzComp>0) &&
1856          (h->Comp() > strat->syzComp))
1857      {
1858        assume(h->MinComp() > strat->syzComp);
1859#ifdef KDEBUG
1860        if (TEST_OPT_DEBUG) PrintS(" > syzComp\n");
1861#endif
1862        if (strat->homog)
1863          h->SetDegStuffReturnLDeg(strat->LDegLast);
1864        return -2;
1865      }
1866    }
1867#endif
1868    if (!strat->homog)
1869    {
1870      if (!K_TEST_OPT_OLDSTD && strat->honey)
1871      {
1872        h->SetpFDeg();
1873        if (strat->T[j].ecart <= h->ecart)
1874          h->ecart = d - h->GetpFDeg();
1875        else
1876          h->ecart = d - h->GetpFDeg() + strat->T[j].ecart - h->ecart;
1877
1878        d = h->GetpFDeg() + h->ecart;
1879      }
1880      else
1881        d = h->SetDegStuffReturnLDeg(strat->LDegLast);
1882      /*- try to reduce the s-polynomial -*/
1883      pass++;
1884      /*
1885       *test whether the polynomial should go to the lazyset L
1886       *-if the degree jumps
1887       *-if the number of pre-defined reductions jumps
1888       */
1889      if (!K_TEST_OPT_REDTHROUGH && (strat->Ll >= 0)
1890          && ((d >= reddeg) || (pass > strat->LazyPass)))
1891      {
1892        h->SetLmCurrRing();
1893        if (strat->posInLDependsOnLength)
1894          h->SetLength(strat->length_pLength);
1895        at = strat->posInL(strat->L,strat->Ll,h,strat);
1896        if (at <= strat->Ll)
1897        {
1898          int dummy=strat->sl;
1899          /*          if (kFindDivisibleByInS(strat,&dummy, h) < 0) */
1900          if (kFindDivisibleByInT(strat->T,strat->sevT, dummy, h) < 0)
1901            return 1;
1902          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
1903#ifdef KDEBUG
1904          if (TEST_OPT_DEBUG) Print(" degree jumped; ->L%d\n",at);
1905#endif
1906          h->Clear();
1907          return -1;
1908        }
1909      }
1910      if ((TEST_OPT_PROT) && (strat->Ll < 0) && (d >= reddeg))
1911      {
1912        reddeg = d+1;
1913        Print(".%d",d);mflush();
1914      }
1915    }
1916  }
1917}
1918
1919void initBbaShift(ideal F,kStrategy strat)
1920{
1921  int i;
1922  idhdl h;
1923 /* setting global variables ------------------- */
1924  strat->enterS = enterSBba; /* remains as is, we change enterT! */
1925
1926  strat->red = redFirstShift; /* no redHomog ! */
1927
1928  if (pLexOrder && strat->honey)
1929    strat->initEcart = initEcartNormal;
1930  else
1931    strat->initEcart = initEcartBBA;
1932  if (strat->honey)
1933    strat->initEcartPair = initEcartPairMora;
1934  else
1935    strat->initEcartPair = initEcartPairBba;
1936  strat->kIdeal = NULL;
1937  //if (strat->ak==0) strat->kIdeal->rtyp=IDEAL_CMD;
1938  //else              strat->kIdeal->rtyp=MODUL_CMD;
1939  //strat->kIdeal->data=(void *)strat->Shdl;
1940  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1941  {
1942    //interred  machen   Aenderung
1943    pFDegOld=pFDeg;
1944    pLDegOld=pLDeg;
1945    //h=ggetid("ecart");
1946    //if ((h!=NULL) /*&& (IDTYP(h)==INTVEC_CMD)*/)
1947    //{
1948    //  ecartWeights=iv2array(IDINTVEC(h));
1949    //}
1950    //else
1951    {
1952      ecartWeights=(short *)omAlloc((pVariables+1)*sizeof(short));
1953      /*uses automatic computation of the ecartWeights to set them*/
1954      kEcartWeights(F->m,IDELEMS(F)-1,ecartWeights);
1955    }
1956    pRestoreDegProcs(totaldegreeWecart, maxdegreeWecart);
1957    if (TEST_OPT_PROT)
1958    {
1959      for(i=1; i<=pVariables; i++)
1960        Print(" %d",ecartWeights[i]);
1961      PrintLn();
1962      mflush();
1963    }
1964  }
1965}
1966#endif
Note: See TracBrowser for help on using the repository browser.