source: git/kernel/kstd2.cc @ 31b00db

fieker-DuValspielwiese
Last change on this file since 31b00db was 9408d9, checked in by Christian Eder, 11 years ago
fixes prod_crit check in sba
  • Property mode set to 100644
File size: 91.2 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5*  ABSTRACT -  Kernel: alg. of Buchberger
6*/
7
8// #define PDEBUG 2
9
10// TODO: why the following is here instead of mod2.h???
11
12// define to enable tailRings
13#define HAVE_TAIL_RING
14
15#ifdef HAVE_CONFIG_H
16#include "singularconfig.h"
17#endif /* HAVE_CONFIG_H */
18#include <kernel/mod2.h>
19
20#ifndef NDEBUG
21# define MYTEST 0
22#else /* ifndef NDEBUG */
23# define MYTEST 0
24#endif /* ifndef NDEBUG */
25
26#if MYTEST
27# ifdef HAVE_TAIL_RING
28#  undef HAVE_TAIL_RING
29# endif // ifdef HAVE_TAIL_RING
30#endif
31
32// define if no buckets should be used
33// #define NO_BUCKETS
34
35#ifdef HAVE_PLURAL
36#define PLURAL_INTERNAL_DECLARATIONS 1
37#endif
38
39/***********************************************
40 * SBA stuff -- start
41***********************************************/
42#define DEBUGF50  0
43#define DEBUGF51  0
44
45#ifdef DEBUGF5
46#undef DEBUGF5
47//#define DEBUGF5 1
48#endif
49
50#define F5C       1
51#if F5C
52  #define F5CTAILRED 1
53#endif
54
55#define SBA_INTERRED_START                  0
56#define SBA_TAIL_RED                        1
57#define SBA_PRODUCT_CRITERION               0
58#define SBA_PRINT_ZERO_REDUCTIONS           0
59#define SBA_PRINT_REDUCTION_STEPS           0
60#define SBA_PRINT_OPERATIONS                0
61#define SBA_PRINT_SIZE_G                    0
62#define SBA_PRINT_SIZE_SYZ                  0
63#define SBA_PRINT_PRODUCT_CRITERION         0
64
65// counts sba's reduction steps
66#if SBA_PRINT_REDUCTION_STEPS
67long sba_reduction_steps;
68long sba_interreduction_steps;
69#endif
70#if SBA_PRINT_OPERATIONS
71long sba_operations;
72long sba_interreduction_operations;
73#endif
74
75/***********************************************
76 * SBA stuff -- done
77***********************************************/
78
79#include <kernel/kutil.h>
80#include <misc/options.h>
81#include <omalloc/omalloc.h>
82#include <kernel/polys.h>
83#include <kernel/ideals.h>
84#include <kernel/febase.h>
85#include <kernel/kstd1.h>
86#include <kernel/khstd.h>
87#include <polys/kbuckets.h>
88#include <polys/prCopy.h>
89//#include "cntrlc.h"
90#include <polys/weight.h>
91#include <misc/intvec.h>
92#ifdef HAVE_PLURAL
93#include <polys/nc/nc.h>
94#endif
95// #include "timer.h"
96
97/* shiftgb stuff */
98#include <kernel/shiftgb.h>
99
100  int (*test_PosInT)(const TSet T,const int tl,LObject &h);
101  int (*test_PosInL)(const LSet set, const int length,
102                LObject* L,const kStrategy strat);
103
104// return -1 if no divisor is found
105//        number of first divisor, otherwise
106int kFindDivisibleByInT(const TSet &T, const unsigned long* sevT,
107                        const int tl, const LObject* L, const int start)
108{
109  unsigned long not_sev = ~L->sev;
110  int j = start;
111  poly p=L->p;
112  ring r=currRing;
113  L->GetLm(p, r);
114
115  pAssume(~not_sev == p_GetShortExpVector(p, r));
116
117  if (r == currRing)
118  {
119    loop
120    {
121      if (j > tl) return -1;
122#if defined(PDEBUG) || defined(PDIV_DEBUG)
123      if (p_LmShortDivisibleBy(T[j].p, sevT[j],
124                               p, not_sev, r))
125        return j;
126#else
127      if (!(sevT[j] & not_sev) &&
128          p_LmDivisibleBy(T[j].p, p, r))
129        return j;
130#endif
131      j++;
132    }
133  }
134  else
135  {
136    loop
137    {
138      if (j > tl) return -1;
139#if defined(PDEBUG) || defined(PDIV_DEBUG)
140      if (p_LmShortDivisibleBy(T[j].t_p, sevT[j],
141                               p, not_sev, r))
142        return j;
143#else
144      if (!(sevT[j] & not_sev) &&
145          p_LmDivisibleBy(T[j].t_p, p, r))
146        return j;
147#endif
148      j++;
149    }
150  }
151}
152
153// same as above, only with set S
154int kFindDivisibleByInS(const kStrategy strat, int* max_ind, LObject* L)
155{
156  unsigned long not_sev = ~L->sev;
157  poly p = L->GetLmCurrRing();
158  int j = 0;
159
160  pAssume(~not_sev == p_GetShortExpVector(p, currRing));
161#if 1
162  int ende;
163  if ((strat->ak>0) || currRing->pLexOrder) ende=strat->sl;
164  else ende=posInS(strat,*max_ind,p,0)+1;
165  if (ende>(*max_ind)) ende=(*max_ind);
166#else
167  int ende=strat->sl;
168#endif
169  (*max_ind)=ende;
170  loop
171  {
172    if (j > ende) return -1;
173#if defined(PDEBUG) || defined(PDIV_DEBUG)
174    if (p_LmShortDivisibleBy(strat->S[j], strat->sevS[j],
175                             p, not_sev, currRing))
176        return j;
177#else
178    if ( !(strat->sevS[j] & not_sev) &&
179         p_LmDivisibleBy(strat->S[j], p, currRing))
180      return j;
181#endif
182    j++;
183  }
184}
185
186int kFindNextDivisibleByInS(const kStrategy strat, int start,int max_ind, LObject* L)
187{
188  unsigned long not_sev = ~L->sev;
189  poly p = L->GetLmCurrRing();
190  int j = start;
191
192  pAssume(~not_sev == p_GetShortExpVector(p, currRing));
193#if 1
194  int ende=max_ind;
195#else
196  int ende=strat->sl;
197#endif
198  loop
199  {
200    if (j > ende) return -1;
201#if defined(PDEBUG) || defined(PDIV_DEBUG)
202    if (p_LmShortDivisibleBy(strat->S[j], strat->sevS[j],
203                             p, not_sev, currRing))
204        return j;
205#else
206    if ( !(strat->sevS[j] & not_sev) &&
207         p_LmDivisibleBy(strat->S[j], p, currRing))
208      return j;
209#endif
210    j++;
211  }
212}
213
214#ifdef HAVE_RINGS
215poly kFindZeroPoly(poly input_p, ring leadRing, ring tailRing)
216{
217  // m = currRing->ch
218
219  if (input_p == NULL) return NULL;
220
221  poly p = input_p;
222  poly zeroPoly = NULL;
223  NATNUMBER a = (NATNUMBER) pGetCoeff(p);
224
225  int k_ind2 = 0;
226  int a_ind2 = ind2(a);
227
228  // NATNUMBER k = 1;
229  // of interest is only k_ind2, special routine for improvement ... TODO OLIVER
230  for (int i = 1; i <= leadRing->N; i++)
231  {
232    k_ind2 = k_ind2 + ind_fact_2(p_GetExp(p, i, leadRing));
233  }
234
235  a = (NATNUMBER) pGetCoeff(p);
236
237  number tmp1;
238  poly tmp2, tmp3;
239  poly lead_mult = p_ISet(1, tailRing);
240  if (n_GetChar(leadRing->cf) <= k_ind2 + a_ind2)
241  {
242    int too_much = k_ind2 + a_ind2 - n_GetChar(leadRing->cf);
243    int s_exp;
244    zeroPoly = p_ISet(a, tailRing);
245    for (int i = 1; i <= leadRing->N; i++)
246    {
247      s_exp = p_GetExp(p, i,leadRing);
248      if (s_exp % 2 != 0)
249      {
250        s_exp = s_exp - 1;
251      }
252      while ( (0 < ind2(s_exp)) && (ind2(s_exp) <= too_much) )
253      {
254        too_much = too_much - ind2(s_exp);
255        s_exp = s_exp - 2;
256      }
257      p_SetExp(lead_mult, i, p_GetExp(p, i,leadRing) - s_exp, tailRing);
258      for (NATNUMBER j = 1; j <= s_exp; j++)
259      {
260        tmp1 = nInit(j);
261        tmp2 = p_ISet(1, tailRing);
262        p_SetExp(tmp2, i, 1, tailRing);
263        p_Setm(tmp2, tailRing);
264        if (nIsZero(tmp1))
265        { // should nowbe obsolet, test ! TODO OLIVER
266          zeroPoly = p_Mult_q(zeroPoly, tmp2, tailRing);
267        }
268        else
269        {
270          tmp3 = p_NSet(nCopy(tmp1), tailRing);
271          zeroPoly = p_Mult_q(zeroPoly, p_Add_q(tmp3, tmp2, tailRing), tailRing);
272        }
273      }
274    }
275    p_Setm(lead_mult, tailRing);
276    zeroPoly = p_Mult_mm(zeroPoly, lead_mult, tailRing);
277    tmp2 = p_NSet(nCopy(pGetCoeff(zeroPoly)), leadRing);
278    for (int i = 1; i <= leadRing->N; i++)
279    {
280      pSetExp(tmp2, i, p_GetExp(zeroPoly, i, tailRing));
281    }
282    p_Setm(tmp2, leadRing);
283    zeroPoly = p_LmDeleteAndNext(zeroPoly, tailRing);
284    pNext(tmp2) = zeroPoly;
285    return tmp2;
286  }
287/*  NATNUMBER alpha_k = twoPow(leadRing->ch - k_ind2);
288  if (1 == 0 && alpha_k <= a)
289  {  // Temporarly disabled, reducing coefficients not compatible with std TODO Oliver
290    zeroPoly = p_ISet((a / alpha_k)*alpha_k, tailRing);
291    for (int i = 1; i <= leadRing->N; i++)
292    {
293      for (NATNUMBER j = 1; j <= p_GetExp(p, i, leadRing); j++)
294      {
295        tmp1 = nInit(j);
296        tmp2 = p_ISet(1, tailRing);
297        p_SetExp(tmp2, i, 1, tailRing);
298        p_Setm(tmp2, tailRing);
299        if (nIsZero(tmp1))
300        {
301          zeroPoly = p_Mult_q(zeroPoly, tmp2, tailRing);
302        }
303        else
304        {
305          tmp3 = p_ISet((NATNUMBER) tmp1, tailRing);
306          zeroPoly = p_Mult_q(zeroPoly, p_Add_q(tmp2, tmp3, tailRing), tailRing);
307        }
308      }
309    }
310    tmp2 = p_ISet((NATNUMBER) pGetCoeff(zeroPoly), leadRing);
311    for (int i = 1; i <= leadRing->N; i++)
312    {
313      pSetExp(tmp2, i, p_GetExp(zeroPoly, i, tailRing));
314    }
315    p_Setm(tmp2, leadRing);
316    zeroPoly = p_LmDeleteAndNext(zeroPoly, tailRing);
317    pNext(tmp2) = zeroPoly;
318    return tmp2;
319  } */
320  return NULL;
321}
322#endif
323
324
325#ifdef HAVE_RINGS
326/*2
327*  reduction procedure for the ring Z/2^m
328*/
329int redRing (LObject* h,kStrategy strat)
330{
331  if (h->IsNull()) return 0; // spoly is zero (can only occure with zero divisors)
332  if (strat->tl<0) return 1;
333
334  int at/*,i*/;
335  long d;
336  int j = 0;
337  int pass = 0;
338  // poly zeroPoly = NULL;
339
340// TODO warum SetpFDeg notwendig?
341  h->SetpFDeg();
342  assume(h->pFDeg() == h->FDeg);
343  long reddeg = h->GetpFDeg();
344
345  h->SetShortExpVector();
346  loop
347  {
348    j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
349    if (j < 0) return 1;
350
351    ksReducePoly(h, &(strat->T[j]), NULL, NULL, strat); // with debug output
352
353    if (h->GetLmTailRing() == NULL)
354    {
355      if (h->lcm!=NULL) pLmDelete(h->lcm);
356#ifdef KDEBUG
357      h->lcm=NULL;
358#endif
359      h->Clear();
360      return 0;
361    }
362    h->SetShortExpVector();
363    d = h->SetpFDeg();
364    /*- try to reduce the s-polynomial -*/
365    pass++;
366    if (!TEST_OPT_REDTHROUGH &&
367        (strat->Ll >= 0) && ((d > reddeg) || (pass > strat->LazyPass)))
368    {
369      h->SetLmCurrRing();
370      if (strat->posInLDependsOnLength)
371        h->SetLength(strat->length_pLength);
372      at = strat->posInL(strat->L,strat->Ll,h,strat);
373      if (at <= strat->Ll)
374      {
375#ifdef KDEBUG
376        if (TEST_OPT_DEBUG) Print(" ->L[%d]\n",at);
377#endif
378        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);     // NOT RING CHECKED OLIVER
379        h->Clear();
380        return -1;
381      }
382    }
383    if (d != reddeg)
384    {
385      if (d >= strat->tailRing->bitmask)
386      {
387        if (h->pTotalDeg() >= strat->tailRing->bitmask)
388        {
389          strat->overflow=TRUE;
390          //Print("OVERFLOW in redRing d=%ld, max=%ld\n",d,strat->tailRing->bitmask);
391          h->GetP();
392          at = strat->posInL(strat->L,strat->Ll,h,strat);
393          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
394          h->Clear();
395          return -1;
396        }
397      }
398      else if ((TEST_OPT_PROT) && (strat->Ll < 0))
399      {
400        Print(".%ld",d);mflush();
401        reddeg = d;
402      }
403    }
404  }
405}
406#endif
407
408/*2
409*  reduction procedure for the homogeneous case
410*  and the case of a degree-ordering
411*/
412int redHomog (LObject* h,kStrategy strat)
413{
414  if (strat->tl<0) return 1;
415  //if (h->GetLmTailRing()==NULL) return 0; // HS: SHOULD NOT BE NEEDED!
416  assume(h->FDeg == h->pFDeg());
417
418  poly h_p;
419  int i,j,at,pass, ii;
420  unsigned long not_sev;
421  // long reddeg,d;
422
423  pass = j = 0;
424  // d = reddeg = h->GetpFDeg();
425  h->SetShortExpVector();
426  int li;
427  h_p = h->GetLmTailRing();
428  not_sev = ~ h->sev;
429  loop
430  {
431    j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
432    if (j < 0) return 1;
433
434    li = strat->T[j].pLength;
435    ii = j;
436    /*
437     * the polynomial to reduce with (up to the moment) is;
438     * pi with length li
439     */
440    i = j;
441#if 1
442    if (TEST_OPT_LENGTH)
443    loop
444    {
445      /*- search the shortest possible with respect to length -*/
446      i++;
447      if (i > strat->tl)
448        break;
449      if (li<=1)
450        break;
451      if ((strat->T[i].pLength < li)
452         &&
453          p_LmShortDivisibleBy(strat->T[i].GetLmTailRing(), strat->sevT[i],
454                               h_p, not_sev, strat->tailRing))
455      {
456        /*
457         * the polynomial to reduce with is now;
458         */
459        li = strat->T[i].pLength;
460        ii = i;
461      }
462    }
463#endif
464
465    /*
466     * end of search: have to reduce with pi
467     */
468#ifdef KDEBUG
469    if (TEST_OPT_DEBUG)
470    {
471      PrintS("red:");
472      h->wrp();
473      PrintS(" with ");
474      strat->T[ii].wrp();
475    }
476#endif
477    assume(strat->fromT == FALSE);
478
479    ksReducePoly(h, &(strat->T[ii]), NULL, NULL, strat);
480#if SBA_PRINT_REDUCTION_STEPS
481    sba_interreduction_steps++;
482#endif
483#if SBA_PRINT_OPERATIONS
484    sba_interreduction_operations  +=  pLength(strat->T[ii].p);
485#endif
486
487#ifdef KDEBUG
488    if (TEST_OPT_DEBUG)
489    {
490      PrintS("\nto ");
491      h->wrp();
492      PrintLn();
493    }
494#endif
495
496    h_p = h->GetLmTailRing();
497    if (h_p == NULL)
498    {
499      if (h->lcm!=NULL) pLmFree(h->lcm);
500#ifdef KDEBUG
501      h->lcm=NULL;
502#endif
503      return 0;
504    }
505    h->SetShortExpVector();
506    not_sev = ~ h->sev;
507    /*
508     * try to reduce the s-polynomial h
509     *test first whether h should go to the lazyset L
510     *-if the degree jumps
511     *-if the number of pre-defined reductions jumps
512     */
513    pass++;
514    if (!TEST_OPT_REDTHROUGH && (strat->Ll >= 0) && (pass > strat->LazyPass))
515    {
516      h->SetLmCurrRing();
517      at = strat->posInL(strat->L,strat->Ll,h,strat);
518      if (at <= strat->Ll)
519      {
520        int dummy=strat->sl;
521        if (kFindDivisibleByInS(strat, &dummy, h) < 0)
522          return 1;
523        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
524#ifdef KDEBUG
525        if (TEST_OPT_DEBUG)
526          Print(" lazy: -> L%d\n",at);
527#endif
528        h->Clear();
529        return -1;
530      }
531    }
532  }
533}
534
535KINLINE int ksReducePolyTailSig(LObject* PR, TObject* PW, LObject* Red)
536{
537  BOOLEAN ret;
538  number coef;
539
540  assume(PR->GetLmCurrRing() != PW->GetLmCurrRing());
541  Red->HeadNormalize();
542  /*
543  printf("------------------------\n");
544  pWrite(Red->GetLmCurrRing());
545  */
546  ret = ksReducePolySig(Red, PW, 1, NULL, &coef);
547
548
549  if (!ret)
550  {
551    if (! n_IsOne(coef, currRing->cf))
552    {
553      PR->Mult_nn(coef);
554      // HANNES: mark for Normalize
555    }
556    n_Delete(&coef, currRing->cf);
557  }
558  return ret;
559}
560
561/*2
562*  reduction procedure for signature-based standard
563*  basis algorithms:
564*  all reductions have to be sig-safe!
565*
566*  2 is returned if and only if the pair is rejected by the rewritten criterion
567*  at exactly this point of the computations. This is the last possible point
568*  such a check can be done => checks with the biggest set of available
569*  signatures
570*/
571int redSig (LObject* h,kStrategy strat)
572{
573  if (strat->tl<0) return 1;
574  //if (h->GetLmTailRing()==NULL) return 0; // HS: SHOULD NOT BE NEEDED!
575  //printf("FDEGS: %ld -- %ld\n",h->FDeg, h->pFDeg());
576  assume(h->FDeg == h->pFDeg());
577//#if 1
578#ifdef DEBUGF5
579  Print("------- IN REDSIG -------\n");
580  Print("p: ");
581  pWrite(pHead(h->p));
582  Print("p1: ");
583  pWrite(pHead(h->p1));
584  Print("p2: ");
585  pWrite(pHead(h->p2));
586  Print("---------------------------\n");
587#endif
588  poly h_p;
589  int i,j,at,pass, ii;
590  int start=0;
591  int sigSafe;
592  unsigned long not_sev;
593  // long reddeg,d;
594
595  pass = j = 0;
596  // d = reddeg = h->GetpFDeg();
597  h->SetShortExpVector();
598  int li;
599  h_p = h->GetLmTailRing();
600  not_sev = ~ h->sev;
601  loop
602  {
603    j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h, start);
604    if (j < 0)
605    {
606      return 1;
607    }
608
609    li = strat->T[j].pLength;
610    ii = j;
611    /*
612     * the polynomial to reduce with (up to the moment) is;
613     * pi with length li
614     */
615    i = j;
616#if 1
617    if (TEST_OPT_LENGTH)
618    loop
619    {
620      /*- search the shortest possible with respect to length -*/
621      i++;
622      if (i > strat->tl)
623        break;
624      if (li<=1)
625        break;
626      if ((strat->T[i].pLength < li)
627         &&
628          p_LmShortDivisibleBy(strat->T[i].GetLmTailRing(), strat->sevT[i],
629                               h_p, not_sev, strat->tailRing))
630      {
631        /*
632         * the polynomial to reduce with is now;
633         */
634        li = strat->T[i].pLength;
635        ii = i;
636      }
637    }
638    start = ii+1;
639#endif
640
641    /*
642     * end of search: have to reduce with pi
643     */
644#ifdef KDEBUG
645    if (TEST_OPT_DEBUG)
646    {
647      PrintS("red:");
648      h->wrp();
649      PrintS(" with ");
650      strat->T[ii].wrp();
651    }
652#endif
653    assume(strat->fromT == FALSE);
654//#if 1
655#ifdef DEBUGF5
656    Print("BEFORE REDUCTION WITH %d:\n",ii);
657    Print("--------------------------------\n");
658    pWrite(h->sig);
659    pWrite(strat->T[ii].sig);
660    pWrite(h->GetLmCurrRing());
661    pWrite(pHead(h->p1));
662    pWrite(pHead(h->p2));
663    pWrite(pHead(strat->T[ii].p));
664    Print("--------------------------------\n");
665    printf("INDEX OF REDUCER T: %d\n",ii);
666#endif
667    sigSafe = ksReducePolySig(h, &(strat->T[ii]), strat->S_2_R[ii], NULL, NULL, strat);
668#if SBA_PRINT_REDUCTION_STEPS
669    if (sigSafe != 3)
670      sba_reduction_steps++;
671#endif
672#if SBA_PRINT_OPERATIONS
673    if (sigSafe != 3)
674      sba_operations  +=  pLength(strat->T[ii].p);
675#endif
676    // if reduction has taken place, i.e. the reduction was sig-safe
677    // otherwise start is already at the next position and the loop
678    // searching reducers in T goes on from index start
679//#if 1
680#ifdef DEBUGF5
681    Print("SigSAFE: %d\n",sigSafe);
682#endif
683    if (sigSafe != 3)
684    {
685      // start the next search for reducers in T from the beginning
686      start = 0;
687#ifdef KDEBUG
688      if (TEST_OPT_DEBUG)
689      {
690        PrintS("\nto ");
691        h->wrp();
692        PrintLn();
693      }
694#endif
695
696      h_p = h->GetLmTailRing();
697      if (h_p == NULL)
698      {
699        if (h->lcm!=NULL) pLmFree(h->lcm);
700#ifdef KDEBUG
701        h->lcm=NULL;
702#endif
703        return 0;
704      }
705      h->SetShortExpVector();
706      not_sev = ~ h->sev;
707      /*
708      * try to reduce the s-polynomial h
709      *test first whether h should go to the lazyset L
710      *-if the degree jumps
711      *-if the number of pre-defined reductions jumps
712      */
713      pass++;
714      if (!TEST_OPT_REDTHROUGH && (strat->Ll >= 0) && (pass > strat->LazyPass))
715      {
716        h->SetLmCurrRing();
717        at = strat->posInL(strat->L,strat->Ll,h,strat);
718        if (at <= strat->Ll)
719        {
720          int dummy=strat->sl;
721          if (kFindDivisibleByInS(strat, &dummy, h) < 0)
722          {
723            return 1;
724          }
725          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
726#ifdef KDEBUG
727          if (TEST_OPT_DEBUG)
728            Print(" lazy: -> L%d\n",at);
729#endif
730          h->Clear();
731          return -1;
732        }
733      }
734    }
735  }
736}
737
738// tail reduction for SBA
739poly redtailSba (LObject* L, int pos, kStrategy strat, BOOLEAN withT, BOOLEAN normalize)
740{
741#define REDTAIL_CANONICALIZE 100
742  strat->redTailChange=FALSE;
743  if (strat->noTailReduction) return L->GetLmCurrRing();
744  poly h, p;
745  p = h = L->GetLmTailRing();
746  if ((h==NULL) || (pNext(h)==NULL))
747    return L->GetLmCurrRing();
748
749  TObject* With;
750  // placeholder in case strat->tl < 0
751  TObject  With_s(strat->tailRing);
752
753  LObject Ln(pNext(h), strat->tailRing);
754  Ln.sig      = L->sig;
755  Ln.sevSig   = L->sevSig;
756  Ln.pLength  = L->GetpLength() - 1;
757
758  pNext(h) = NULL;
759  if (L->p != NULL) pNext(L->p) = NULL;
760  L->pLength = 1;
761
762  Ln.PrepareRed(strat->use_buckets);
763
764  int cnt=REDTAIL_CANONICALIZE;
765  while(!Ln.IsNull())
766  {
767    loop
768    {
769      Ln.SetShortExpVector();
770      if (withT)
771      {
772        int j;
773        j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, &Ln);
774        if (j < 0) break;
775        With = &(strat->T[j]);
776      }
777      else
778      {
779        With = kFindDivisibleByInS(strat, pos, &Ln, &With_s);
780        if (With == NULL) break;
781      }
782      cnt--;
783      if (cnt==0)
784      {
785        cnt=REDTAIL_CANONICALIZE;
786        /*poly tmp=*/Ln.CanonicalizeP();
787        if (normalize)
788        {
789          Ln.Normalize();
790          //pNormalize(tmp);
791          //if (TEST_OPT_PROT) { PrintS("n"); mflush(); }
792        }
793      }
794      if (normalize && (!TEST_OPT_INTSTRATEGY) && (!nIsOne(pGetCoeff(With->p))))
795      {
796        With->pNorm();
797      }
798      strat->redTailChange=TRUE;
799      int ret = ksReducePolyTailSig(L, With, &Ln);
800#if SBA_PRINT_REDUCTION_STEPS
801      if (ret != 3)
802        sba_reduction_steps++;
803#endif
804#if SBA_PRINT_OPERATIONS
805      if (ret != 3)
806        sba_operations  +=  pLength(With->p);
807#endif
808      if (ret)
809      {
810        // reducing the tail would violate the exp bound
811        //  set a flag and hope for a retry (in bba)
812        strat->completeReduce_retry=TRUE;
813        if ((Ln.p != NULL) && (Ln.t_p != NULL)) Ln.p=NULL;
814        do
815        {
816          pNext(h) = Ln.LmExtractAndIter();
817          pIter(h);
818          L->pLength++;
819        } while (!Ln.IsNull());
820        goto all_done;
821      }
822      if (Ln.IsNull()) goto all_done;
823      if (! withT) With_s.Init(currRing);
824    }
825    pNext(h) = Ln.LmExtractAndIter();
826    pIter(h);
827    pNormalize(h);
828    L->pLength++;
829  }
830
831  all_done:
832  Ln.Delete();
833  if (L->p != NULL) pNext(L->p) = pNext(p);
834
835  if (strat->redTailChange)
836  {
837    L->length = 0;
838  }
839
840  //if (TEST_OPT_PROT) { PrintS("N"); mflush(); }
841  //L->Normalize(); // HANNES: should have a test
842  assume(kTest_L(L));
843  return L->GetLmCurrRing();
844}
845
846/*2
847*  reduction procedure for the inhomogeneous case
848*  and not a degree-ordering
849*/
850int redLazy (LObject* h,kStrategy strat)
851{
852  if (strat->tl<0) return 1;
853  int at,i,ii,li;
854  int j = 0;
855  int pass = 0;
856  assume(h->pFDeg() == h->FDeg);
857  long reddeg = h->GetpFDeg();
858  long d;
859  unsigned long not_sev;
860
861  h->SetShortExpVector();
862  poly h_p = h->GetLmTailRing();
863  not_sev = ~ h->sev;
864  loop
865  {
866    j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
867    if (j < 0) return 1;
868
869    li = strat->T[j].pLength;
870    #if 0
871    if (li==0)
872    {
873      li=strat->T[j].pLength=pLength(strat->T[j].p);
874    }
875    #endif
876    ii = j;
877    /*
878     * the polynomial to reduce with (up to the moment) is;
879     * pi with length li
880     */
881
882    i = j;
883#if 1
884    if (TEST_OPT_LENGTH)
885    loop
886    {
887      /*- search the shortest possible with respect to length -*/
888      i++;
889      if (i > strat->tl)
890        break;
891      if (li<=1)
892        break;
893    #if 0
894      if (strat->T[i].pLength==0)
895      {
896        PrintS("!");
897        strat->T[i].pLength=pLength(strat->T[i].p);
898      }
899   #endif
900      if ((strat->T[i].pLength < li)
901         &&
902          p_LmShortDivisibleBy(strat->T[i].GetLmTailRing(), strat->sevT[i],
903                               h_p, not_sev, strat->tailRing))
904      {
905        /*
906         * the polynomial to reduce with is now;
907         */
908        PrintS("+");
909        li = strat->T[i].pLength;
910        ii = i;
911      }
912    }
913#endif
914
915    /*
916     * end of search: have to reduce with pi
917     */
918
919
920#ifdef KDEBUG
921    if (TEST_OPT_DEBUG)
922    {
923      PrintS("red:");
924      h->wrp();
925      PrintS(" with ");
926      strat->T[ii].wrp();
927    }
928#endif
929
930    ksReducePoly(h, &(strat->T[ii]), NULL, NULL, strat);
931#if SBA_PRINT_REDUCTION_STEPS
932    sba_interreduction_steps++;
933#endif
934#if SBA_PRINT_OPERATIONS
935    sba_interreduction_operations  +=  pLength(strat->T[ii].p);
936#endif
937
938#ifdef KDEBUG
939    if (TEST_OPT_DEBUG)
940    {
941      PrintS("\nto ");
942      h->wrp();
943      PrintLn();
944    }
945#endif
946
947    h_p=h->GetLmTailRing();
948
949    if (h_p == NULL)
950    {
951      if (h->lcm!=NULL) pLmFree(h->lcm);
952#ifdef KDEBUG
953      h->lcm=NULL;
954#endif
955      return 0;
956    }
957    h->SetShortExpVector();
958    not_sev = ~ h->sev;
959    d = h->SetpFDeg();
960    /*- try to reduce the s-polynomial -*/
961    pass++;
962    if (//!TEST_OPT_REDTHROUGH &&
963        (strat->Ll >= 0) && ((d > reddeg) || (pass > strat->LazyPass)))
964    {
965      h->SetLmCurrRing();
966      at = strat->posInL(strat->L,strat->Ll,h,strat);
967      if (at <= strat->Ll)
968      {
969#if 1
970        int dummy=strat->sl;
971        if (kFindDivisibleByInS(strat, &dummy, h) < 0)
972          return 1;
973#endif
974#ifdef KDEBUG
975        if (TEST_OPT_DEBUG) Print(" ->L[%d]\n",at);
976#endif
977        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
978        h->Clear();
979        return -1;
980      }
981    }
982    else if (d != reddeg)
983    {
984      if (d>=strat->tailRing->bitmask)
985      {
986        if (h->pTotalDeg() >= strat->tailRing->bitmask)
987        {
988          strat->overflow=TRUE;
989          //Print("OVERFLOW in redLazy d=%ld, max=%ld\n",d,strat->tailRing->bitmask);
990          h->GetP();
991          at = strat->posInL(strat->L,strat->Ll,h,strat);
992          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
993          h->Clear();
994          return -1;
995        }
996      }
997      else if ((TEST_OPT_PROT) && (strat->Ll < 0))
998      {
999        Print(".%ld",d);mflush();
1000        reddeg = d;
1001      }
1002    }
1003  }
1004}
1005/*2
1006*  reduction procedure for the sugar-strategy (honey)
1007* reduces h with elements from T choosing first possible
1008* element in T with respect to the given ecart
1009*/
1010int redHoney (LObject* h, kStrategy strat)
1011{
1012  if (strat->tl<0) return 1;
1013  //if (h->GetLmTailRing()==NULL) return 0; // HS: SHOULD NOT BE NEEDED!
1014  assume(h->FDeg == h->pFDeg());
1015  poly h_p;
1016  int i,j,at,pass,ei, ii, h_d;
1017  unsigned long not_sev;
1018  long reddeg,d;
1019
1020  pass = j = 0;
1021  d = reddeg = h->GetpFDeg() + h->ecart;
1022  h->SetShortExpVector();
1023  int li;
1024  h_p = h->GetLmTailRing();
1025  not_sev = ~ h->sev;
1026
1027  h->PrepareRed(strat->use_buckets);
1028  loop
1029  {
1030    j=kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
1031    if (j < 0) return 1;
1032
1033    ei = strat->T[j].ecart;
1034    li = strat->T[j].pLength;
1035    ii = j;
1036    /*
1037     * the polynomial to reduce with (up to the moment) is;
1038     * pi with ecart ei
1039     */
1040    i = j;
1041    if (TEST_OPT_LENGTH)
1042    loop
1043    {
1044      /*- takes the first possible with respect to ecart -*/
1045      i++;
1046      if (i > strat->tl)
1047        break;
1048      //if (ei < h->ecart)
1049      //  break;
1050      if (li<=1)
1051        break;
1052      if ((((strat->T[i].ecart < ei) && (ei> h->ecart))
1053         || ((strat->T[i].ecart <= h->ecart) && (strat->T[i].pLength < li)))
1054         &&
1055          p_LmShortDivisibleBy(strat->T[i].GetLmTailRing(), strat->sevT[i],
1056                               h_p, not_sev, strat->tailRing))
1057      {
1058        /*
1059         * the polynomial to reduce with is now;
1060         */
1061        ei = strat->T[i].ecart;
1062        li = strat->T[i].pLength;
1063        ii = i;
1064      }
1065    }
1066
1067    /*
1068     * end of search: have to reduce with pi
1069     */
1070    if (!TEST_OPT_REDTHROUGH && (pass!=0) && (ei > h->ecart))
1071    {
1072      h->GetTP(); // clears bucket
1073      h->SetLmCurrRing();
1074      /*
1075       * It is not possible to reduce h with smaller ecart;
1076       * if possible h goes to the lazy-set L,i.e
1077       * if its position in L would be not the last one
1078       */
1079      if (strat->Ll >= 0) /* L is not empty */
1080      {
1081        at = strat->posInL(strat->L,strat->Ll,h,strat);
1082        if(at <= strat->Ll)
1083          /*- h will not become the next element to reduce -*/
1084        {
1085          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
1086#ifdef KDEBUG
1087          if (TEST_OPT_DEBUG) Print(" ecart too big: -> L%d\n",at);
1088#endif
1089          h->Clear();
1090          return -1;
1091        }
1092      }
1093    }
1094#ifdef KDEBUG
1095    if (TEST_OPT_DEBUG)
1096    {
1097      PrintS("red:");
1098      h->wrp();
1099      PrintS(" with ");
1100      strat->T[ii].wrp();
1101    }
1102#endif
1103    assume(strat->fromT == FALSE);
1104
1105    number coef;
1106    ksReducePoly(h,&(strat->T[ii]),strat->kNoetherTail(),&coef,strat);
1107#if SBA_PRINT_REDUCTION_STEPS
1108    sba_interreduction_steps++;
1109#endif
1110#if SBA_PRINT_OPERATIONS
1111    sba_interreduction_operations  +=  pLength(strat->T[ii].p);
1112#endif
1113#ifdef KDEBUG
1114    if (TEST_OPT_DEBUG)
1115    {
1116      PrintS("\nto:");
1117      h->wrp();
1118      PrintLn();
1119    }
1120#endif
1121    if(h->IsNull())
1122    {
1123      h->Clear();
1124      if (h->lcm!=NULL) pLmFree(h->lcm);
1125      #ifdef KDEBUG
1126      h->lcm=NULL;
1127      #endif
1128      return 0;
1129    }
1130    h->SetShortExpVector();
1131    not_sev = ~ h->sev;
1132    h_d = h->SetpFDeg();
1133    /* compute the ecart */
1134    if (ei <= h->ecart)
1135      h->ecart = d-h_d;
1136    else
1137      h->ecart = d-h_d+ei-h->ecart;
1138
1139    /*
1140     * try to reduce the s-polynomial h
1141     *test first whether h should go to the lazyset L
1142     *-if the degree jumps
1143     *-if the number of pre-defined reductions jumps
1144     */
1145    pass++;
1146    d = h_d + h->ecart;
1147    if (!TEST_OPT_REDTHROUGH && (strat->Ll >= 0) && ((d > reddeg) || (pass > strat->LazyPass)))
1148    {
1149      h->GetTP(); // clear bucket
1150      h->SetLmCurrRing();
1151      at = strat->posInL(strat->L,strat->Ll,h,strat);
1152      if (at <= strat->Ll)
1153      {
1154        int dummy=strat->sl;
1155        if (kFindDivisibleByInS(strat, &dummy, h) < 0)
1156          return 1;
1157        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
1158#ifdef KDEBUG
1159        if (TEST_OPT_DEBUG)
1160          Print(" degree jumped: -> L%d\n",at);
1161#endif
1162        h->Clear();
1163        return -1;
1164      }
1165    }
1166    else if (d > reddeg)
1167    {
1168      if (d>=strat->tailRing->bitmask)
1169      {
1170        if (h->pTotalDeg()+h->ecart >= strat->tailRing->bitmask)
1171        {
1172          strat->overflow=TRUE;
1173          //Print("OVERFLOW in redHoney d=%ld, max=%ld\n",d,strat->tailRing->bitmask);
1174          h->GetP();
1175          at = strat->posInL(strat->L,strat->Ll,h,strat);
1176          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
1177          h->Clear();
1178          return -1;
1179        }
1180      }
1181      else if (TEST_OPT_PROT && (strat->Ll < 0) )
1182      {
1183        //h->wrp(); Print("<%d>\n",h->GetpLength());
1184        reddeg = d;
1185        Print(".%ld",d); mflush();
1186      }
1187    }
1188  }
1189}
1190
1191/*2
1192*  reduction procedure for the normal form
1193*/
1194
1195poly redNF (poly h,int &max_ind,int nonorm,kStrategy strat)
1196{
1197  if (h==NULL) return NULL;
1198  int j;
1199  max_ind=strat->sl;
1200
1201  if (0 > strat->sl)
1202  {
1203    return h;
1204  }
1205  LObject P(h);
1206  P.SetShortExpVector();
1207  P.bucket = kBucketCreate(currRing);
1208  kBucketInit(P.bucket,P.p,pLength(P.p));
1209  kbTest(P.bucket);
1210#ifdef HAVE_RINGS
1211  BOOLEAN is_ring = rField_is_Ring(currRing);
1212#endif
1213#ifdef KDEBUG
1214  if (TEST_OPT_DEBUG)
1215  {
1216    PrintS("redNF: starting S: ");
1217    for( j = 0; j <= max_ind; j++ )
1218    {
1219      Print("S[%d] (of size: %d): ", j, pSize(strat->S[j]));
1220      pWrite(strat->S[j]);
1221    }
1222  };
1223#endif
1224
1225  loop
1226  {
1227    j=kFindDivisibleByInS(strat,&max_ind,&P);
1228    if (j>=0)
1229    {
1230#ifdef HAVE_RINGS
1231      if (!is_ring)
1232      {
1233#endif
1234        int sl=pSize(strat->S[j]);
1235        int jj=j;
1236        loop
1237        {
1238          int sll;
1239          jj=kFindNextDivisibleByInS(strat,jj+1,max_ind,&P);
1240          if (jj<0) break;
1241          sll=pSize(strat->S[jj]);
1242          if (sll<sl)
1243          {
1244            #ifdef KDEBUG
1245            if (TEST_OPT_DEBUG) Print("better(S%d:%d -> S%d:%d)\n",j,sl,jj,sll);
1246            #endif
1247            //else if (TEST_OPT_PROT) { PrintS("b"); mflush(); }
1248            j=jj;
1249            sl=sll;
1250          }
1251        }
1252        if ((nonorm==0) && (!nIsOne(pGetCoeff(strat->S[j]))))
1253        {
1254          pNorm(strat->S[j]);
1255          //if (TEST_OPT_PROT) { PrintS("n"); mflush(); }
1256        }
1257#ifdef HAVE_RINGS
1258      }
1259#endif
1260      nNormalize(pGetCoeff(P.p));
1261#ifdef KDEBUG
1262      if (TEST_OPT_DEBUG)
1263      {
1264        PrintS("red:");
1265        wrp(h);
1266        PrintS(" with ");
1267        wrp(strat->S[j]);
1268      }
1269#endif
1270#ifdef HAVE_PLURAL
1271      if (rIsPluralRing(currRing))
1272      {
1273        number coef;
1274        nc_kBucketPolyRed(P.bucket,strat->S[j],&coef);
1275        nDelete(&coef);
1276      }
1277      else
1278#endif
1279      {
1280        number coef;
1281        coef=kBucketPolyRed(P.bucket,strat->S[j],pLength(strat->S[j]),strat->kNoether);
1282        nDelete(&coef);
1283      }
1284      h = kBucketGetLm(P.bucket);   // FRAGE OLIVER
1285      if (h==NULL)
1286      {
1287        kBucketDestroy(&P.bucket);
1288
1289#ifdef KDEBUG
1290        if (TEST_OPT_DEBUG)
1291        {
1292          PrintS("redNF: starting S: ");
1293          for( j = 0; j <= max_ind; j++ )
1294          {
1295            Print("S[%d] (of size: %d): ", j, pSize(strat->S[j]));
1296            pWrite(strat->S[j]);
1297          }
1298        };
1299#endif
1300
1301        return NULL;
1302      }
1303      kbTest(P.bucket);
1304      P.p=h;
1305      P.t_p=NULL;
1306      P.SetShortExpVector();
1307#ifdef KDEBUG
1308      if (TEST_OPT_DEBUG)
1309      {
1310        PrintS("\nto:");
1311        wrp(h);
1312        PrintLn();
1313      }
1314#endif
1315    }
1316    else
1317    {
1318      P.p=kBucketClear(P.bucket);
1319      kBucketDestroy(&P.bucket);
1320      pNormalize(P.p);
1321
1322#ifdef KDEBUG
1323      if (TEST_OPT_DEBUG)
1324      {
1325        PrintS("redNF: starting S: ");
1326        for( j = 0; j <= max_ind; j++ )
1327        {
1328          Print("S[%d] (of size: %d): ", j, pSize(strat->S[j]));
1329          pWrite(strat->S[j]);
1330        }
1331      };
1332#endif
1333
1334      return P.p;
1335    }
1336  }
1337}
1338
1339#ifdef KDEBUG
1340static int bba_count = 0;
1341#endif /* KDEBUG */
1342void kDebugPrint(kStrategy strat);
1343
1344ideal bba (ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat)
1345{
1346#ifdef KDEBUG
1347  bba_count++;
1348  int loop_count = 0;
1349#endif /* KDEBUG */
1350  int   red_result = 1;
1351  int   olddeg,reduc;
1352  int hilbeledeg=1,hilbcount=0,minimcnt=0;
1353  BOOLEAN withT = FALSE;
1354  BITSET save;
1355  SI_SAVE_OPT1(save);
1356
1357  initBuchMoraCrit(strat); /*set Gebauer, honey, sugarCrit*/
1358  initBuchMoraPos(strat);
1359  initHilbCrit(F,Q,&hilb,strat);
1360  initBba(F,strat);
1361  /*set enterS, spSpolyShort, reduce, red, initEcart, initEcartPair*/
1362  /*Shdl=*/initBuchMora(F, Q,strat);
1363  if (strat->minim>0) strat->M=idInit(IDELEMS(F),F->rank);
1364  reduc = olddeg = 0;
1365
1366#ifndef NO_BUCKETS
1367  if (!TEST_OPT_NOT_BUCKETS)
1368    strat->use_buckets = 1;
1369#endif
1370
1371  // redtailBBa against T for inhomogenous input
1372  if (!TEST_OPT_OLDSTD)
1373    withT = ! strat->homog;
1374
1375  // strat->posInT = posInT_pLength;
1376  assume(kTest_TS(strat));
1377
1378#ifdef KDEBUG
1379#if MYTEST
1380  if (TEST_OPT_DEBUG)
1381  {
1382    PrintS("bba start GB: currRing: ");
1383    // rWrite(currRing);PrintLn();
1384    rDebugPrint(currRing);
1385    PrintLn();
1386  }
1387#endif /* MYTEST */
1388#endif /* KDEBUG */
1389
1390#ifdef HAVE_TAIL_RING
1391  if(!idIs0(F) &&(!rField_is_Ring(currRing)))  // create strong gcd poly computes with tailring and S[i] ->to be fixed
1392    kStratInitChangeTailRing(strat);
1393#endif
1394  if (BVERBOSE(23))
1395  {
1396    if (test_PosInT!=NULL) strat->posInT=test_PosInT;
1397    if (test_PosInL!=NULL) strat->posInL=test_PosInL;
1398    kDebugPrint(strat);
1399  }
1400
1401
1402#ifdef KDEBUG
1403  //kDebugPrint(strat);
1404#endif
1405  /* compute------------------------------------------------------- */
1406  while (strat->Ll >= 0)
1407  {
1408    #ifdef KDEBUG
1409      loop_count++;
1410      if (TEST_OPT_DEBUG) messageSets(strat);
1411    #endif
1412    if (strat->Ll== 0) strat->interpt=TRUE;
1413    if (TEST_OPT_DEGBOUND
1414        && ((strat->honey && (strat->L[strat->Ll].ecart+currRing->pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
1415            || ((!strat->honey) && (currRing->pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))))
1416    {
1417      /*
1418       *stops computation if
1419       * 24 IN test and the degree +ecart of L[strat->Ll] is bigger then
1420       *a predefined number Kstd1_deg
1421       */
1422      while ((strat->Ll >= 0)
1423        && (strat->L[strat->Ll].p1!=NULL) && (strat->L[strat->Ll].p2!=NULL)
1424        && ((strat->honey && (strat->L[strat->Ll].ecart+currRing->pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
1425            || ((!strat->honey) && (currRing->pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg)))
1426        )
1427        deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
1428      if (strat->Ll<0) break;
1429      else strat->noClearS=TRUE;
1430    }
1431    /* picks the last element from the lazyset L */
1432    strat->P = strat->L[strat->Ll];
1433    strat->Ll--;
1434
1435    if (pNext(strat->P.p) == strat->tail)
1436    {
1437      // deletes the short spoly
1438#ifdef HAVE_RINGS
1439      if (rField_is_Ring(currRing))
1440        pLmDelete(strat->P.p);
1441      else
1442#endif
1443        pLmFree(strat->P.p);
1444      strat->P.p = NULL;
1445      poly m1 = NULL, m2 = NULL;
1446
1447      // check that spoly creation is ok
1448      while (strat->tailRing != currRing &&
1449             !kCheckSpolyCreation(&(strat->P), strat, m1, m2))
1450      {
1451        assume(m1 == NULL && m2 == NULL);
1452        // if not, change to a ring where exponents are at least
1453        // large enough
1454        if (!kStratChangeTailRing(strat))
1455        {
1456          WerrorS("OVERFLOW...");
1457          break;
1458        }
1459      }
1460      // create the real one
1461      ksCreateSpoly(&(strat->P), NULL, strat->use_buckets,
1462                    strat->tailRing, m1, m2, strat->R);
1463    }
1464    else if (strat->P.p1 == NULL)
1465    {
1466      if (strat->minim > 0)
1467        strat->P.p2=p_Copy(strat->P.p, currRing, strat->tailRing);
1468      // for input polys, prepare reduction
1469      strat->P.PrepareRed(strat->use_buckets);
1470    }
1471
1472    if (strat->P.p == NULL && strat->P.t_p == NULL)
1473    {
1474      red_result = 0;
1475    }
1476    else
1477    {
1478      if (TEST_OPT_PROT)
1479        message((strat->honey ? strat->P.ecart : 0) + strat->P.pFDeg(),
1480                &olddeg,&reduc,strat, red_result);
1481
1482      /* reduction of the element choosen from L */
1483      red_result = strat->red(&strat->P,strat);
1484      if (errorreported)  break;
1485    }
1486
1487    if (strat->overflow)
1488    {
1489        if (!kStratChangeTailRing(strat)) { Werror("OVERFLOW.."); break;}
1490    }
1491
1492    // reduction to non-zero new poly
1493    if (red_result == 1)
1494    {
1495      // get the polynomial (canonicalize bucket, make sure P.p is set)
1496      strat->P.GetP(strat->lmBin);
1497      // in the homogeneous case FDeg >= pFDeg (sugar/honey)
1498      // but now, for entering S, T, we reset it
1499      // in the inhomogeneous case: FDeg == pFDeg
1500      if (strat->homog) strat->initEcart(&(strat->P));
1501
1502      /* statistic */
1503      if (TEST_OPT_PROT) PrintS("s");
1504
1505      int pos=posInS(strat,strat->sl,strat->P.p,strat->P.ecart);
1506
1507#ifdef KDEBUG
1508#if MYTEST
1509      PrintS("New S: "); p_DebugPrint(strat->P.p, currRing); PrintLn();
1510#endif /* MYTEST */
1511#endif /* KDEBUG */
1512
1513      // reduce the tail and normalize poly
1514      // in the ring case we cannot expect LC(f) = 1,
1515      // therefore we call pContent instead of pNorm
1516      if ((TEST_OPT_INTSTRATEGY) || (rField_is_Ring(currRing)))
1517      {
1518        strat->P.pCleardenom();
1519        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
1520        {
1521          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
1522          strat->P.pCleardenom();
1523        }
1524      }
1525      else
1526      {
1527        strat->P.pNorm();
1528        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
1529          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
1530      }
1531
1532#ifdef KDEBUG
1533      if (TEST_OPT_DEBUG){PrintS("new s:");strat->P.wrp();PrintLn();}
1534#if MYTEST
1535      PrintS("New (reduced) S: "); p_DebugPrint(strat->P.p, currRing); PrintLn();
1536#endif /* MYTEST */
1537#endif /* KDEBUG */
1538
1539      // min_std stuff
1540      if ((strat->P.p1==NULL) && (strat->minim>0))
1541      {
1542        if (strat->minim==1)
1543        {
1544          strat->M->m[minimcnt]=p_Copy(strat->P.p,currRing,strat->tailRing);
1545          p_Delete(&strat->P.p2, currRing, strat->tailRing);
1546        }
1547        else
1548        {
1549          strat->M->m[minimcnt]=strat->P.p2;
1550          strat->P.p2=NULL;
1551        }
1552        if (strat->tailRing!=currRing && pNext(strat->M->m[minimcnt])!=NULL)
1553          pNext(strat->M->m[minimcnt])
1554            = strat->p_shallow_copy_delete(pNext(strat->M->m[minimcnt]),
1555                                           strat->tailRing, currRing,
1556                                           currRing->PolyBin);
1557        minimcnt++;
1558      }
1559
1560      // enter into S, L, and T
1561      //if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
1562        enterT(strat->P, strat);
1563#ifdef HAVE_RINGS
1564      if (rField_is_Ring(currRing))
1565        superenterpairs(strat->P.p,strat->sl,strat->P.ecart,pos,strat, strat->tl);
1566      else
1567#endif
1568        enterpairs(strat->P.p,strat->sl,strat->P.ecart,pos,strat, strat->tl);
1569      // posInS only depends on the leading term
1570      strat->enterS(strat->P, pos, strat, strat->tl);
1571#if 0
1572      int pl=pLength(strat->P.p);
1573      if (pl==1)
1574      {
1575        //if (TEST_OPT_PROT)
1576        //PrintS("<1>");
1577      }
1578      else if (pl==2)
1579      {
1580        //if (TEST_OPT_PROT)
1581        //PrintS("<2>");
1582      }
1583#endif
1584      if (hilb!=NULL) khCheck(Q,w,hilb,hilbeledeg,hilbcount,strat);
1585//      Print("[%d]",hilbeledeg);
1586      if (strat->P.lcm!=NULL)
1587#ifdef HAVE_RINGS
1588        pLmDelete(strat->P.lcm);
1589#else
1590        pLmFree(strat->P.lcm);
1591#endif
1592    }
1593    else if (strat->P.p1 == NULL && strat->minim > 0)
1594    {
1595      p_Delete(&strat->P.p2, currRing, strat->tailRing);
1596    }
1597
1598#ifdef KDEBUG
1599    memset(&(strat->P), 0, sizeof(strat->P));
1600#endif /* KDEBUG */
1601    assume(kTest_TS(strat));
1602  }
1603#ifdef KDEBUG
1604#if MYTEST
1605  PrintS("bba finish GB: currRing: "); rWrite(currRing);
1606#endif /* MYTEST */
1607  if (TEST_OPT_DEBUG) messageSets(strat);
1608#endif /* KDEBUG */
1609
1610  if (TEST_OPT_SB_1)
1611  {
1612    int k=1;
1613    int j;
1614    while(k<=strat->sl)
1615    {
1616      j=0;
1617      loop
1618      {
1619        if (j>=k) break;
1620        clearS(strat->S[j],strat->sevS[j],&k,&j,strat);
1621        j++;
1622      }
1623      k++;
1624    }
1625  }
1626
1627  /* complete reduction of the standard basis--------- */
1628  if (TEST_OPT_REDSB)
1629  {
1630    completeReduce(strat);
1631#ifdef HAVE_TAIL_RING
1632    if (strat->completeReduce_retry)
1633    {
1634      // completeReduce needed larger exponents, retry
1635      // to reduce with S (instead of T)
1636      // and in currRing (instead of strat->tailRing)
1637      cleanT(strat);strat->tailRing=currRing;
1638      int i;
1639      for(i=strat->sl;i>=0;i--) strat->S_2_R[i]=-1;
1640      completeReduce(strat);
1641    }
1642#endif
1643  }
1644  else if (TEST_OPT_PROT) PrintLn();
1645
1646  /* release temp data-------------------------------- */
1647  exitBuchMora(strat);
1648//  if (TEST_OPT_WEIGHTM)
1649//  {
1650//    pRestoreDegProcs(currRing,pFDegOld, pLDegOld);
1651//    if (ecartWeights)
1652//    {
1653//      omFreeSize((ADDRESS)ecartWeights,((currRing->N)+1)*sizeof(short));
1654//      ecartWeights=NULL;
1655//    }
1656//  }
1657  if (TEST_OPT_PROT) messageStat(hilbcount,strat);
1658  SI_RESTORE_OPT1(save);
1659  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
1660
1661#ifdef KDEBUG
1662#if MYTEST
1663  PrintS("bba_end: currRing: "); rWrite(currRing);
1664#endif /* MYTEST */
1665#endif /* KDEBUG */
1666  idTest(strat->Shdl);
1667
1668  return (strat->Shdl);
1669}
1670ideal sba (ideal F0, ideal Q,intvec *w,intvec *hilb,kStrategy strat)
1671{
1672  // ring order stuff:
1673  // in sba we have (until now) two possibilities:
1674  // 1. an incremental computation w.r.t. (C,monomial order)
1675  // 2. a (possibly non-incremental) computation w.r.t. the
1676  //    induced Schreyer order.
1677  // The corresponding orders are computed in sbaRing(), depending
1678  // on the flag strat->sbaOrder
1679#if SBA_PRINT_ZERO_REDUCTIONS
1680  long zeroreductions           = 0;
1681#endif
1682#if SBA_PRINT_PRODUCT_CRITERION
1683  long product_criterion        = 0;
1684#endif
1685#if SBA_PRINT_SIZE_G
1686  long size_g                   = 0;
1687#endif
1688#if SBA_PRINT_SIZE_SYZ
1689  long size_syz                 = 0;
1690#endif
1691  // global variable
1692#if SBA_PRINT_REDUCTION_STEPS
1693  sba_reduction_steps           = 0;
1694  sba_interreduction_steps      = 0;
1695#endif
1696#if SBA_PRINT_OPERATIONS
1697  sba_operations                = 0;
1698  sba_interreduction_operations = 0;
1699#endif
1700
1701  ideal F1 = F0;
1702  ring sRing, currRingOld;
1703  currRingOld  = currRing;
1704  if (strat->sbaOrder == 1 || strat->sbaOrder == 3)
1705  {
1706    sRing = sbaRing(strat);
1707    if (sRing!=currRingOld)
1708    {
1709      rChangeCurrRing (sRing);
1710      F1 = idrMoveR (F0, currRingOld, currRing);
1711    }
1712  }
1713  // sort ideal F
1714  ideal F       = idInit(IDELEMS(F1),F1->rank);
1715  intvec *sort  = idSort(F1);
1716  for (int i=0; i<sort->length();++i)
1717    F->m[i] = F1->m[(*sort)[i]-1];
1718#if SBA_INTERRED_START
1719  F = kInterRed(F,NULL);
1720#endif
1721#if F5DEBUG
1722  printf("SBA COMPUTATIONS DONE IN THE FOLLOWING RING:\n");
1723  rWrite (currRing);
1724  printf("ordSgn = %d\n",currRing->OrdSgn);
1725  printf("\n");
1726#endif
1727#ifdef KDEBUG
1728  bba_count++;
1729  int loop_count = 0;
1730#endif /* KDEBUG */
1731  int   srmax,lrmax, red_result = 1;
1732  int   olddeg,reduc;
1733  int hilbeledeg=1,hilbcount=0,minimcnt=0;
1734  LObject L;
1735  BOOLEAN withT     = TRUE;
1736  strat->max_lower_index = 0;
1737
1738  //initBuchMoraCrit(strat); /*set Gebauer, honey, sugarCrit*/
1739  initSbaCrit(strat); /*set Gebauer, honey, sugarCrit*/
1740  initSbaPos(strat);
1741  //initBuchMoraPos(strat);
1742  initHilbCrit(F,Q,&hilb,strat);
1743  initSba(F,strat);
1744  /*set enterS, spSpolyShort, reduce, red, initEcart, initEcartPair*/
1745  /*Shdl=*/initSbaBuchMora(F, Q,strat);
1746  if (strat->minim>0) strat->M=idInit(IDELEMS(F),F->rank);
1747  srmax = strat->sl;
1748  reduc = olddeg = lrmax = 0;
1749
1750#ifndef NO_BUCKETS
1751  if (!TEST_OPT_NOT_BUCKETS)
1752    strat->use_buckets = 1;
1753#endif
1754
1755  // redtailBBa against T for inhomogenous input
1756  // if (!TEST_OPT_OLDSTD)
1757  //   withT = ! strat->homog;
1758
1759  // strat->posInT = posInT_pLength;
1760  assume(kTest_TS(strat));
1761
1762#ifdef KDEBUG
1763#if MYTEST
1764  if (TEST_OPT_DEBUG)
1765  {
1766    PrintS("bba start GB: currRing: ");
1767    // rWrite(currRing);PrintLn();
1768    rDebugPrint(currRing);
1769    PrintLn();
1770  }
1771#endif /* MYTEST */
1772#endif /* KDEBUG */
1773
1774#ifdef HAVE_TAIL_RING
1775  if(!idIs0(F) &&(!rField_is_Ring(currRing)))  // create strong gcd poly computes with tailring and S[i] ->to be fixed
1776    kStratInitChangeTailRing(strat);
1777#endif
1778  if (BVERBOSE(23))
1779  {
1780    if (test_PosInT!=NULL) strat->posInT=test_PosInT;
1781    if (test_PosInL!=NULL) strat->posInL=test_PosInL;
1782    kDebugPrint(strat);
1783  }
1784
1785
1786#ifdef KDEBUG
1787  //kDebugPrint(strat);
1788#endif
1789  /* compute------------------------------------------------------- */
1790  while (strat->Ll >= 0)
1791  {
1792    if (strat->Ll > lrmax) lrmax =strat->Ll;/*stat.*/
1793    #ifdef KDEBUG
1794      loop_count++;
1795      if (TEST_OPT_DEBUG) messageSets(strat);
1796    #endif
1797    if (strat->Ll== 0) strat->interpt=TRUE;
1798    /*
1799    if (TEST_OPT_DEGBOUND
1800        && ((strat->honey && (strat->L[strat->Ll].ecart+currRing->pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
1801            || ((!strat->honey) && (currRing->pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))))
1802    {
1803     
1804       //stops computation if
1805       // 24 IN test and the degree +ecart of L[strat->Ll] is bigger then
1806       //a predefined number Kstd1_deg
1807      while ((strat->Ll >= 0)
1808        && (strat->L[strat->Ll].p1!=NULL) && (strat->L[strat->Ll].p2!=NULL)
1809        && ((strat->honey && (strat->L[strat->Ll].ecart+currRing->pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
1810            || ((!strat->honey) && (currRing->pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg)))
1811        )
1812        deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
1813      if (strat->Ll<0) break;
1814      else strat->noClearS=TRUE;
1815    }
1816    */
1817    if (strat->sbaOrder == 1 && pGetComp(strat->L[strat->Ll].sig) != strat->currIdx)
1818    {
1819      strat->currIdx  = pGetComp(strat->L[strat->Ll].sig);
1820#if F5C
1821      // 1. interreduction of the current standard basis
1822      // 2. generation of new principal syzygy rules for syzCriterion
1823      f5c ( strat, olddeg, minimcnt, hilbeledeg, hilbcount, srmax,
1824          lrmax, reduc, Q, w, hilb );
1825#endif
1826      // initialize new syzygy rules for the next iteration step
1827      initSyzRules(strat);
1828
1829    }
1830    /*********************************************************************
1831      * interrreduction step is done, we can go on with the next iteration
1832      * step of the signature-based algorithm
1833      ********************************************************************/
1834    /* picks the last element from the lazyset L */
1835    strat->P = strat->L[strat->Ll];
1836    strat->Ll--;
1837    /* reduction of the element choosen from L */
1838
1839    if (!strat->rewCrit2(strat->P.sig, ~strat->P.sevSig, strat->P.GetLmCurrRing(), strat, strat->P.checked+1)) {
1840      //#if 1
1841#ifdef DEBUGF5
1842      Print("SIG OF NEXT PAIR TO HANDLE IN SIG-BASED ALGORITHM\n");
1843      Print("-------------------------------------------------\n");
1844      pWrite(strat->P.sig);
1845      pWrite(pHead(strat->P.p));
1846      pWrite(pHead(strat->P.p1));
1847      pWrite(pHead(strat->P.p2));
1848      Print("-------------------------------------------------\n");
1849#endif
1850      if (pNext(strat->P.p) == strat->tail)
1851      {
1852        // deletes the short spoly
1853        /*
1854#ifdef HAVE_RINGS
1855        if (rField_is_Ring(currRing))
1856          pLmDelete(strat->P.p);
1857        else
1858#endif
1859          pLmFree(strat->P.p);
1860*/
1861          // TODO: needs some masking
1862          // TODO: masking needs to vanish once the signature
1863          //       sutff is completely implemented
1864          strat->P.p = NULL;
1865        poly m1 = NULL, m2 = NULL;
1866
1867        // check that spoly creation is ok
1868        while (strat->tailRing != currRing &&
1869            !kCheckSpolyCreation(&(strat->P), strat, m1, m2))
1870        {
1871          assume(m1 == NULL && m2 == NULL);
1872          // if not, change to a ring where exponents are at least
1873          // large enough
1874          if (!kStratChangeTailRing(strat))
1875          {
1876            WerrorS("OVERFLOW...");
1877            break;
1878          }
1879        }
1880        // create the real one
1881        ksCreateSpoly(&(strat->P), NULL, strat->use_buckets,
1882            strat->tailRing, m1, m2, strat->R);
1883
1884      }
1885      else if (strat->P.p1 == NULL)
1886      {
1887        if (strat->minim > 0)
1888          strat->P.p2=p_Copy(strat->P.p, currRing, strat->tailRing);
1889        // for input polys, prepare reduction
1890        strat->P.PrepareRed(strat->use_buckets);
1891      }
1892      if (strat->P.p == NULL && strat->P.t_p == NULL)
1893      {
1894        red_result = 0;
1895      }
1896      else
1897      {
1898        //#if 1
1899#ifdef DEBUGF5
1900        Print("Poly before red: ");
1901        pWrite(pHead(strat->P.p));
1902        pWrite(strat->P.sig);
1903#endif
1904#if SBA_PRODUCT_CRITERION
1905        if (strat->P.prod_crit) {
1906#if SBA_PRINT_PRODUCT_CRITERION
1907          product_criterion++;
1908#endif
1909          int pos = posInSyz(strat, strat->P.sig);
1910          enterSyz(strat->P, strat, pos);
1911          if (strat->P.lcm!=NULL)
1912            pLmFree(strat->P.lcm);
1913          red_result = 2;
1914        } else {
1915          red_result = strat->red(&strat->P,strat);
1916        }
1917#else
1918        red_result = strat->red(&strat->P,strat);
1919#endif
1920      }
1921    } else {
1922      /*
1923      if (strat->P.lcm != NULL)
1924        pLmFree(strat->P.lcm);
1925        */
1926      red_result = 2;
1927    }
1928    if (errorreported)  break;
1929
1930//#if 1
1931#ifdef DEBUGF5
1932    if (red_result != 0) {
1933        Print("Poly after red: ");
1934        pWrite(pHead(strat->P.p));
1935        pWrite(strat->P.GetLmCurrRing());
1936        pWrite(strat->P.sig);
1937        printf("%d\n",red_result);
1938    }
1939#endif
1940
1941    if (strat->overflow)
1942    {
1943        if (!kStratChangeTailRing(strat)) { Werror("OVERFLOW.."); break;}
1944    }
1945
1946    // reduction to non-zero new poly
1947    if (red_result == 1)
1948    {
1949      // get the polynomial (canonicalize bucket, make sure P.p is set)
1950      strat->P.GetP(strat->lmBin);
1951
1952      // sig-safe computations may lead to wrong FDeg computation, thus we need
1953      // to recompute it to make sure everything is alright
1954      (strat->P).FDeg = (strat->P).pFDeg();
1955      // in the homogeneous case FDeg >= pFDeg (sugar/honey)
1956      // but now, for entering S, T, we reset it
1957      // in the inhomogeneous case: FDeg == pFDeg
1958      if (strat->homog) strat->initEcart(&(strat->P));
1959
1960      /* statistic */
1961      if (TEST_OPT_PROT) PrintS("s");
1962
1963      //int pos=posInS(strat,strat->sl,strat->P.p,strat->P.ecart);
1964      // in F5E we know that the last reduced element is already the
1965      // the one with highest signature
1966      int pos = strat->sl+1;
1967
1968#ifdef KDEBUG
1969#if MYTEST
1970      PrintS("New S: "); pDebugPrint(strat->P.p); PrintLn();
1971#endif /* MYTEST */
1972#endif /* KDEBUG */
1973
1974      // reduce the tail and normalize poly
1975      // in the ring case we cannot expect LC(f) = 1,
1976      // therefore we call pContent instead of pNorm
1977#if SBA_TAIL_RED
1978      if (strat->sbaOrder != 2) {
1979        if ((TEST_OPT_INTSTRATEGY) || (rField_is_Ring(currRing)))
1980        {
1981          strat->P.pCleardenom();
1982          if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
1983          {
1984            strat->P.p = redtailSba(&(strat->P),pos-1,strat, withT);
1985            strat->P.pCleardenom();
1986          }
1987        }
1988        else
1989        {
1990          strat->P.pNorm();
1991          if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
1992            strat->P.p = redtailSba(&(strat->P),pos-1,strat, withT);
1993        }
1994      }
1995#endif
1996
1997    // remove sigsafe label since it is no longer valid for the next element to
1998    // be reduced
1999    if (strat->sbaOrder == 1)
2000    {
2001      for (int jj = 0; jj<strat->tl+1; jj++)
2002      {
2003        if (pGetComp(strat->T[jj].sig) == strat->currIdx)
2004        {
2005          strat->T[jj].is_sigsafe = FALSE;
2006        }
2007      }
2008    }
2009    else
2010    {
2011      for (int jj = 0; jj<strat->tl+1; jj++)
2012      {
2013        strat->T[jj].is_sigsafe = FALSE;
2014      }
2015    }
2016#ifdef KDEBUG
2017      if (TEST_OPT_DEBUG){PrintS("new s:");strat->P.wrp();PrintLn();}
2018#if MYTEST
2019//#if 1
2020      PrintS("New (reduced) S: "); pDebugPrint(strat->P.p); PrintLn();
2021#endif /* MYTEST */
2022#endif /* KDEBUG */
2023
2024      // min_std stuff
2025      if ((strat->P.p1==NULL) && (strat->minim>0))
2026      {
2027        if (strat->minim==1)
2028        {
2029          strat->M->m[minimcnt]=p_Copy(strat->P.p,currRing,strat->tailRing);
2030          p_Delete(&strat->P.p2, currRing, strat->tailRing);
2031        }
2032        else
2033        {
2034          strat->M->m[minimcnt]=strat->P.p2;
2035          strat->P.p2=NULL;
2036        }
2037        if (strat->tailRing!=currRing && pNext(strat->M->m[minimcnt])!=NULL)
2038          pNext(strat->M->m[minimcnt])
2039            = strat->p_shallow_copy_delete(pNext(strat->M->m[minimcnt]),
2040                                           strat->tailRing, currRing,
2041                                           currRing->PolyBin);
2042        minimcnt++;
2043      }
2044
2045      // enter into S, L, and T
2046      //if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
2047      enterT(strat->P, strat);
2048      strat->T[strat->tl].is_sigsafe = FALSE;
2049      /*
2050      printf("hier\n");
2051      pWrite(strat->P.GetLmCurrRing());
2052      pWrite(strat->P.sig);
2053      */
2054#ifdef HAVE_RINGS
2055      if (rField_is_Ring(currRing))
2056        superenterpairs(strat->P.p,strat->sl,strat->P.ecart,pos,strat, strat->tl);
2057      else
2058#endif
2059        enterpairsSig(strat->P.p,strat->P.sig,strat->sl+1,strat->sl,strat->P.ecart,pos,strat, strat->tl);
2060      // posInS only depends on the leading term
2061      strat->enterS(strat->P, pos, strat, strat->tl);
2062      if(strat->sbaOrder != 1)
2063      {
2064        BOOLEAN overwrite = FALSE;
2065        for (int tk=0; tk<strat->sl+1; tk++)
2066        {
2067          if (pGetComp(strat->sig[tk]) == pGetComp(strat->P.sig))
2068          {
2069            //printf("TK %d / %d\n",tk,strat->sl);
2070            overwrite = FALSE;
2071            break;
2072          }
2073        }
2074        //printf("OVERWRITE %d\n",overwrite);
2075        if (overwrite)
2076        {
2077          int cmp = pGetComp(strat->P.sig);
2078          int* vv = (int*)omAlloc((currRing->N+1)*sizeof(int));
2079          pGetExpV (strat->P.p,vv);
2080          pSetExpV (strat->P.sig, vv);
2081          pSetComp (strat->P.sig,cmp);
2082
2083          strat->P.sevSig = pGetShortExpVector (strat->P.sig);
2084          int i;
2085          LObject Q;
2086          for(int ps=0;ps<strat->sl+1;ps++)
2087          {
2088
2089            strat->newt = TRUE;
2090            if (strat->syzl == strat->syzmax)
2091            {
2092              pEnlargeSet(&strat->syz,strat->syzmax,setmaxTinc);
2093              strat->sevSyz = (unsigned long*) omRealloc0Size(strat->sevSyz,
2094                  (strat->syzmax)*sizeof(unsigned long),
2095                  ((strat->syzmax)+setmaxTinc)
2096                  *sizeof(unsigned long));
2097              strat->syzmax += setmaxTinc;
2098            }
2099            Q.sig = pCopy(strat->P.sig);
2100            // add LM(F->m[i]) to the signature to get a Schreyer order
2101            // without changing the underlying polynomial ring at all
2102            if (strat->sbaOrder == 0)
2103              p_ExpVectorAdd (Q.sig,strat->S[ps],currRing);
2104            // since p_Add_q() destroys all input
2105            // data we need to recreate help
2106            // each time
2107            // ----------------------------------------------------------
2108            // in the Schreyer order we always know that the multiplied
2109            // module monomial strat->P.sig gives the leading monomial of
2110            // the corresponding principal syzygy
2111            // => we do not need to compute the "real" syzygy completely
2112            poly help = p_Copy(strat->sig[ps],currRing);
2113            p_ExpVectorAdd (help,strat->P.p,currRing);
2114            Q.sig = p_Add_q(Q.sig,help,currRing);
2115            //printf("%d. SYZ  ",i+1);
2116            //pWrite(strat->syz[i]);
2117            Q.sevSig = p_GetShortExpVector(Q.sig,currRing);
2118            i = posInSyz(strat, Q.sig);
2119            enterSyz(Q, strat, i);
2120          }
2121        }
2122      }
2123      // deg - idx - lp/rp
2124      // => we need to add syzygies with indices > pGetComp(strat->P.sig)
2125      if(strat->sbaOrder == 0 || strat->sbaOrder == 3)
2126      {
2127        int cmp     = pGetComp(strat->P.sig);
2128        int max_cmp = IDELEMS(F);
2129        int* vv = (int*)omAlloc((currRing->N+1)*sizeof(int));
2130        pGetExpV (strat->P.p,vv);
2131        LObject Q;
2132        int pos;
2133        int idx = p_GetComp(strat->P.sig,currRing);
2134        //printf("++ -- adding syzygies -- ++\n");
2135        // if new element is the first one in this index
2136        if (strat->currIdx < idx) {
2137          for (int i=0; i<strat->sl; ++i) {
2138            Q.sig = p_Copy(strat->P.sig,currRing);
2139            p_ExpVectorAdd(Q.sig,strat->S[i],currRing);
2140            poly help = p_Copy(strat->sig[i],currRing);
2141            p_ExpVectorAdd(help,strat->P.p,currRing);
2142            Q.sig = p_Add_q(Q.sig,help,currRing);
2143            //pWrite(Q.sig);
2144            pos = posInSyz(strat, Q.sig);
2145            enterSyz(Q, strat, pos);
2146          }
2147          strat->currIdx = idx;
2148        } else {
2149          // if the element is not the first one in the given index we build all
2150          // possible syzygies with elements of higher index
2151          for (int i=cmp+1; i<=max_cmp; ++i) {
2152            pos = -1;
2153            for (int j=0; j<strat->sl; ++j) {
2154              if (p_GetComp(strat->sig[j],currRing) == i) {
2155                pos = j;
2156                break;
2157              }
2158            }
2159            if (pos != -1) {
2160              Q.sig = p_One(currRing);
2161              p_SetExpV(Q.sig, vv, currRing);
2162              // F->m[i-1] corresponds to index i
2163              p_ExpVectorAdd(Q.sig,F->m[i-1],currRing);
2164              p_SetComp(Q.sig, i, currRing);
2165              poly help = p_Copy(strat->P.sig,currRing);
2166              p_ExpVectorAdd(help,strat->S[pos],currRing);
2167              Q.sig = p_Add_q(Q.sig,help,currRing);
2168              if (strat->sbaOrder == 0) {
2169                if (p_LmCmp(Q.sig,strat->syz[strat->syzl-1],currRing) == -currRing->OrdSgn) {
2170                  pos = posInSyz(strat, Q.sig);
2171                  enterSyz(Q, strat, pos);
2172                }
2173              } else {
2174                pos = posInSyz(strat, Q.sig);
2175                enterSyz(Q, strat, pos);
2176              }
2177            }
2178          }
2179          //printf("++ -- done adding syzygies -- ++\n");
2180        }
2181      }
2182//#if 1
2183#if DEBUGF50
2184    printf("---------------------------\n");
2185    Print(" %d. ELEMENT ADDED TO GCURR:\n",strat->sl+1);
2186    Print("LEAD POLY:  "); pWrite(pHead(strat->S[strat->sl]));
2187    Print("SIGNATURE:  "); pWrite(strat->sig[strat->sl]);
2188#endif
2189      /*
2190      if (newrules)
2191      {
2192        newrules  = FALSE;
2193      }
2194      */
2195#if 0
2196      int pl=pLength(strat->P.p);
2197      if (pl==1)
2198      {
2199        //if (TEST_OPT_PROT)
2200        //PrintS("<1>");
2201      }
2202      else if (pl==2)
2203      {
2204        //if (TEST_OPT_PROT)
2205        //PrintS("<2>");
2206      }
2207#endif
2208      if (hilb!=NULL) khCheck(Q,w,hilb,hilbeledeg,hilbcount,strat);
2209//      Print("[%d]",hilbeledeg);
2210      if (strat->P.lcm!=NULL)
2211#ifdef HAVE_RINGS
2212        pLmDelete(strat->P.lcm);
2213#else
2214        pLmFree(strat->P.lcm);
2215#endif
2216      if (strat->sl>srmax) srmax = strat->sl;
2217    }
2218    else
2219    {
2220      // adds signature of the zero reduction to
2221      // strat->syz. This is the leading term of
2222      // syzygy and can be used in syzCriterion()
2223      // the signature is added if and only if the
2224      // pair was not detected by the rewritten criterion in strat->red = redSig
2225      if (red_result!=2) {
2226#if SBA_PRINT_ZERO_REDUCTIONS
2227        zeroreductions++;
2228#endif
2229        int pos = posInSyz(strat, strat->P.sig);
2230        enterSyz(strat->P, strat, pos);
2231//#if 1
2232#ifdef DEBUGF5
2233        Print("ADDING STUFF TO SYZ :  ");
2234        //pWrite(strat->P.p);
2235        pWrite(strat->P.sig);
2236#endif
2237      }
2238      if (strat->P.p1 == NULL && strat->minim > 0)
2239      {
2240        p_Delete(&strat->P.p2, currRing, strat->tailRing);
2241      }
2242    }
2243
2244#ifdef KDEBUG
2245    memset(&(strat->P), 0, sizeof(strat->P));
2246#endif /* KDEBUG */
2247    assume(kTest_TS(strat));
2248  }
2249#ifdef KDEBUG
2250#if MYTEST
2251  PrintS("bba finish GB: currRing: "); rWrite(currRing);
2252#endif /* MYTEST */
2253  if (TEST_OPT_DEBUG) messageSets(strat);
2254#endif /* KDEBUG */
2255
2256  if (TEST_OPT_SB_1)
2257  {
2258    int k=1;
2259    int j;
2260    while(k<=strat->sl)
2261    {
2262      j=0;
2263      loop
2264      {
2265        if (j>=k) break;
2266        clearS(strat->S[j],strat->sevS[j],&k,&j,strat);
2267        j++;
2268      }
2269      k++;
2270    }
2271  }
2272
2273  /* complete reduction of the standard basis--------- */
2274  if (TEST_OPT_REDSB)
2275  {
2276    completeReduce(strat);
2277#ifdef HAVE_TAIL_RING
2278    if (strat->completeReduce_retry)
2279    {
2280      // completeReduce needed larger exponents, retry
2281      // to reduce with S (instead of T)
2282      // and in currRing (instead of strat->tailRing)
2283      cleanT(strat);strat->tailRing=currRing;
2284      int i;
2285      for(i=strat->sl;i>=0;i--) strat->S_2_R[i]=-1;
2286      completeReduce(strat);
2287    }
2288#endif
2289  }
2290  else if (TEST_OPT_PROT) PrintLn();
2291
2292#if SBA_PRINT_SIZE_G
2293  size_g   = strat->sl+1;
2294#endif
2295#if SBA_PRINT_SIZE_SYZ
2296  // that is correct, syzl is counting one too far
2297  size_syz = strat->syzl;
2298#endif
2299  exitSba(strat);
2300//  if (TEST_OPT_WEIGHTM)
2301//  {
2302//    pRestoreDegProcs(pFDegOld, pLDegOld);
2303//    if (ecartWeights)
2304//    {
2305//      omFreeSize((ADDRESS)ecartWeights,(pVariables+1)*sizeof(short));
2306//      ecartWeights=NULL;
2307//    }
2308//  }
2309  if (TEST_OPT_PROT) messageStat(hilbcount,strat);
2310  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
2311
2312#ifdef KDEBUG
2313#if MYTEST
2314  PrintS("bba_end: currRing: "); rWrite(currRing);
2315#endif /* MYTEST */
2316#endif /* KDEBUG */
2317  // using F5C it is possible that there is some data stored in the last
2318  // entries of strat->Shdl which are dirty, i.e. not correct, but also not NULL
2319  // => we need to delete them before return the ideal
2320#if F5C
2321  for(int i=strat->sl+1;i<IDELEMS(strat->Shdl);i++)
2322  {
2323    //pDelete (&strat->Shdl->m[i]);
2324    strat->Shdl->m[i] = NULL;
2325  }
2326#endif
2327  if ((strat->sbaOrder == 1 || strat->sbaOrder == 3) && sRing!=currRingOld)
2328  {
2329    rChangeCurrRing (currRingOld);
2330    F0          = idrMoveR (F1, sRing, currRing);
2331    strat->Shdl = idrMoveR_NoSort (strat->Shdl, sRing, currRing);
2332    rDelete (sRing);
2333  }
2334  idTest(strat->Shdl);
2335
2336#ifdef DEBUGF5
2337  printf("SIZE OF SHDL: %d\n",IDELEMS(strat->Shdl));
2338  int oo = 0;
2339  while (oo<IDELEMS(strat->Shdl))
2340  {
2341    printf(" %d.   ",oo+1);
2342    pWrite(pHead(strat->Shdl->m[oo]));
2343    oo++;
2344  }
2345#endif
2346#if SBA_PRINT_ZERO_REDUCTIONS
2347  printf("----------------------------------------------------------\n");
2348  printf("ZERO REDUCTIONS:            %ld\n",zeroreductions);
2349  zeroreductions  = 0;
2350#endif
2351#if SBA_PRINT_REDUCTION_STEPS
2352  printf("----------------------------------------------------------\n");
2353  printf("S-REDUCTIONS:               %ld\n",sba_reduction_steps);
2354#endif
2355#if SBA_PRINT_OPERATIONS
2356  printf("OPERATIONS:                 %ld\n",sba_operations);
2357#endif
2358#if SBA_PRINT_REDUCTION_STEPS
2359  printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n");
2360  printf("INTERREDUCTIONS:            %ld\n",sba_interreduction_steps);
2361#endif
2362#if SBA_PRINT_OPERATIONS
2363  printf("INTERREDUCTION OPERATIONS:  %ld\n",sba_interreduction_operations);
2364#endif
2365#if SBA_PRINT_REDUCTION_STEPS
2366  printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n");
2367  printf("ALL REDUCTIONS:             %ld\n",sba_reduction_steps+sba_interreduction_steps);
2368  sba_interreduction_steps  = 0;
2369  sba_reduction_steps       = 0;
2370#endif
2371#if SBA_PRINT_OPERATIONS
2372  printf("ALL OPERATIONS:             %ld\n",sba_operations+sba_interreduction_operations);
2373  sba_interreduction_operations = 0;
2374  sba_operations                = 0;
2375#endif
2376#if SBA_PRINT_SIZE_G
2377  printf("----------------------------------------------------------\n");
2378  printf("SIZE OF G:                  %ld\n",size_g);
2379  size_g  = 0;
2380#endif
2381#if SBA_PRINT_SIZE_SYZ
2382  printf("SIZE OF SYZ:                %ld\n",size_syz);
2383  printf("----------------------------------------------------------\n");
2384  size_syz  = 0;
2385#endif
2386#if SBA_PRINT_PRODUCT_CRITERION
2387  printf("PRODUCT CRITERIA:           %ld\n",product_criterion);
2388  product_criterion = 0;
2389#endif
2390  return (strat->Shdl);
2391}
2392
2393poly kNF2 (ideal F,ideal Q,poly q,kStrategy strat, int lazyReduce)
2394{
2395  assume(q!=NULL);
2396  assume(!(idIs0(F)&&(Q==NULL))); // NF(q, std(0) in polynomial ring?
2397
2398// lazy_reduce flags: can be combined by |
2399//#define KSTD_NF_LAZY   1
2400  // do only a reduction of the leading term
2401//#define KSTD_NF_NONORM 4
2402  // only global: avoid normalization, return a multiply of NF
2403  poly   p;
2404
2405  //if ((idIs0(F))&&(Q==NULL))
2406  //  return pCopy(q); /*F=0*/
2407  //strat->ak = idRankFreeModule(F);
2408  /*- creating temp data structures------------------- -*/
2409  BITSET save1;
2410  SI_SAVE_OPT1(save1);
2411  si_opt_1|=Sy_bit(OPT_REDTAIL);
2412  initBuchMoraCrit(strat);
2413  strat->initEcart = initEcartBBA;
2414  strat->enterS = enterSBba;
2415#ifndef NO_BUCKETS
2416  strat->use_buckets = (!TEST_OPT_NOT_BUCKETS) && (!rIsPluralRing(currRing));
2417#endif
2418  /*- set S -*/
2419  strat->sl = -1;
2420  /*- init local data struct.---------------------------------------- -*/
2421  /*Shdl=*/initS(F,Q,strat);
2422  /*- compute------------------------------------------------------- -*/
2423  //if ((TEST_OPT_INTSTRATEGY)&&(lazyReduce==0))
2424  //{
2425  //  for (i=strat->sl;i>=0;i--)
2426  //    pNorm(strat->S[i]);
2427  //}
2428  assume(kTest(strat));
2429  if (TEST_OPT_PROT) { PrintS("r"); mflush(); }
2430  if (BVERBOSE(23)) kDebugPrint(strat);
2431  int max_ind;
2432  p = redNF(pCopy(q),max_ind,lazyReduce & KSTD_NF_NONORM,strat);
2433  if ((p!=NULL)&&((lazyReduce & KSTD_NF_LAZY)==0))
2434  {
2435    if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
2436    #ifdef HAVE_RINGS
2437    if (rField_is_Ring(currRing))
2438    {
2439      p = redtailBba_Z(p,max_ind,strat);
2440    }
2441    else
2442    #endif
2443    {
2444      si_opt_1 &= ~Sy_bit(OPT_INTSTRATEGY);
2445      p = redtailBba(p,max_ind,strat,(lazyReduce & KSTD_NF_NONORM)==0);
2446    }
2447  }
2448  /*- release temp data------------------------------- -*/
2449  assume(strat->L==NULL); /* strat->L unused */
2450  assume(strat->B==NULL); /* strat->B unused */
2451  omFree(strat->sevS);
2452  omFree(strat->ecartS);
2453  assume(strat->T==NULL);//omfree(strat->T);
2454  assume(strat->sevT==NULL);//omfree(strat->sevT);
2455  assume(strat->R==NULL);//omfree(strat->R);
2456  omfree(strat->S_2_R);
2457  omfree(strat->fromQ);
2458  idDelete(&strat->Shdl);
2459  SI_RESTORE_OPT1(save1);
2460  if (TEST_OPT_PROT) PrintLn();
2461  return p;
2462}
2463
2464ideal kNF2 (ideal F,ideal Q,ideal q,kStrategy strat, int lazyReduce)
2465{
2466  assume(!idIs0(q));
2467  assume(!(idIs0(F)&&(Q==NULL)));
2468// lazy_reduce flags: can be combined by |
2469//#define KSTD_NF_LAZY   1
2470  // do only a reduction of the leading term
2471//#define KSTD_NF_NONORM 4
2472  // only global: avoid normalization, return a multiply of NF
2473  poly   p;
2474  int   i;
2475  ideal res;
2476  int max_ind;
2477
2478  //if (idIs0(q))
2479  //  return idInit(IDELEMS(q),si_max(q->rank,F->rank));
2480  //if ((idIs0(F))&&(Q==NULL))
2481  //  return idCopy(q); /*F=0*/
2482  //strat->ak = idRankFreeModule(F);
2483  /*- creating temp data structures------------------- -*/
2484  BITSET save1;
2485  SI_SAVE_OPT1(save1);
2486  si_opt_1|=Sy_bit(OPT_REDTAIL);
2487  initBuchMoraCrit(strat);
2488  strat->initEcart = initEcartBBA;
2489  strat->enterS = enterSBba;
2490  /*- set S -*/
2491  strat->sl = -1;
2492#ifndef NO_BUCKETS
2493  strat->use_buckets = (!TEST_OPT_NOT_BUCKETS) && (!rIsPluralRing(currRing));
2494#endif
2495  /*- init local data struct.---------------------------------------- -*/
2496  /*Shdl=*/initS(F,Q,strat);
2497  /*- compute------------------------------------------------------- -*/
2498  res=idInit(IDELEMS(q),si_max(q->rank,F->rank));
2499  si_opt_1 &= ~Sy_bit(OPT_INTSTRATEGY);
2500  for (i=IDELEMS(q)-1; i>=0; i--)
2501  {
2502    if (q->m[i]!=NULL)
2503    {
2504      if (TEST_OPT_PROT) { PrintS("r");mflush(); }
2505      p = redNF(pCopy(q->m[i]),max_ind,lazyReduce & KSTD_NF_NONORM,strat);
2506      if ((p!=NULL)&&((lazyReduce & KSTD_NF_LAZY)==0))
2507      {
2508        if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
2509        #ifdef HAVE_RINGS
2510        if (rField_is_Ring(currRing))
2511        {
2512          p = redtailBba_Z(p,max_ind,strat);
2513        }
2514        else
2515        #endif
2516        {
2517          p = redtailBba(p,max_ind,strat,(lazyReduce & KSTD_NF_NONORM)==0);
2518        }
2519      }
2520      res->m[i]=p;
2521    }
2522    //else
2523    //  res->m[i]=NULL;
2524  }
2525  /*- release temp data------------------------------- -*/
2526  assume(strat->L==NULL); /* strat->L unused */
2527  assume(strat->B==NULL); /* strat->B unused */
2528  omFree(strat->sevS);
2529  omFree(strat->ecartS);
2530  assume(strat->T==NULL);//omfree(strat->T);
2531  assume(strat->sevT==NULL);//omfree(strat->sevT);
2532  assume(strat->R==NULL);//omfree(strat->R);
2533  omfree(strat->S_2_R);
2534  omfree(strat->fromQ);
2535  idDelete(&strat->Shdl);
2536  SI_RESTORE_OPT1(save1);
2537  if (TEST_OPT_PROT) PrintLn();
2538  return res;
2539}
2540
2541#if F5C
2542/*********************************************************************
2543* interrreduction step of the signature-based algorithm:
2544* 1. all strat->S are interpreted as new critical pairs
2545* 2. those pairs need to be completely reduced by the usual (non sig-
2546*    safe) reduction process (including tail reductions)
2547* 3. strat->S and strat->T are completely new computed in these steps
2548********************************************************************/
2549void f5c (kStrategy strat, int& olddeg, int& minimcnt, int& hilbeledeg,
2550          int& hilbcount, int& srmax, int& lrmax, int& reduc, ideal Q,
2551          intvec *w,intvec *hilb )
2552{
2553  int Ll_old, red_result = 1;
2554  int pos  = 0;
2555  hilbeledeg=1;
2556  hilbcount=0;
2557  minimcnt=0;
2558  srmax = 0; // strat->sl is 0 at this point
2559  reduc = olddeg = lrmax = 0;
2560  // we cannot use strat->T anymore
2561  //cleanT(strat);
2562  //strat->tl = -1;
2563  Ll_old    = strat->Ll;
2564  while (strat->tl >= 0)
2565  {
2566    if(!strat->T[strat->tl].is_redundant)
2567    {
2568      LObject h;
2569      h.p = strat->T[strat->tl].p;
2570      h.tailRing = strat->T[strat->tl].tailRing;
2571      h.t_p = strat->T[strat->tl].t_p;
2572      if (h.p!=NULL)
2573      {
2574        if (currRing->OrdSgn==-1)
2575        {
2576          cancelunit(&h);
2577          deleteHC(&h, strat);
2578        }
2579        if (h.p!=NULL)
2580        {
2581          if (TEST_OPT_INTSTRATEGY)
2582          {
2583            //pContent(h.p);
2584            h.pCleardenom(); // also does a pContent
2585          }
2586          else
2587          {
2588            h.pNorm();
2589          }
2590          strat->initEcart(&h);
2591          pos = strat->Ll+1;
2592          h.sev = pGetShortExpVector(h.p);
2593          enterL(&strat->L,&strat->Ll,&strat->Lmax,h,pos);
2594        }
2595      }
2596    }
2597    strat->tl--;
2598  }
2599  strat->sl = -1;
2600#if 0
2601//#ifdef HAVE_TAIL_RING
2602  if(!rField_is_Ring())  // create strong gcd poly computes with tailring and S[i] ->to be fixed
2603    kStratInitChangeTailRing(strat);
2604#endif
2605  //enterpairs(pOne(),0,0,-1,strat,strat->tl);
2606  //strat->sl = -1;
2607  /* picks the last element from the lazyset L */
2608  while (strat->Ll>Ll_old)
2609  {
2610    strat->P = strat->L[strat->Ll];
2611    strat->Ll--;
2612//#if 1
2613#ifdef DEBUGF5
2614    Print("NEXT PAIR TO HANDLE IN INTERRED ALGORITHM\n");
2615    Print("-------------------------------------------------\n");
2616    pWrite(pHead(strat->P.p));
2617    pWrite(pHead(strat->P.p1));
2618    pWrite(pHead(strat->P.p2));
2619    printf("%d\n",strat->tl);
2620    Print("-------------------------------------------------\n");
2621#endif
2622    if (pNext(strat->P.p) == strat->tail)
2623    {
2624      // deletes the short spoly
2625#ifdef HAVE_RINGS
2626      if (rField_is_Ring(currRing))
2627        pLmDelete(strat->P.p);
2628      else
2629#endif
2630        pLmFree(strat->P.p);
2631
2632      // TODO: needs some masking
2633      // TODO: masking needs to vanish once the signature
2634      //       sutff is completely implemented
2635      strat->P.p = NULL;
2636      poly m1 = NULL, m2 = NULL;
2637
2638      // check that spoly creation is ok
2639      while (strat->tailRing != currRing &&
2640          !kCheckSpolyCreation(&(strat->P), strat, m1, m2))
2641      {
2642        assume(m1 == NULL && m2 == NULL);
2643        // if not, change to a ring where exponents are at least
2644        // large enough
2645        if (!kStratChangeTailRing(strat))
2646        {
2647          WerrorS("OVERFLOW...");
2648          break;
2649        }
2650      }
2651      // create the real one
2652      ksCreateSpoly(&(strat->P), NULL, strat->use_buckets,
2653          strat->tailRing, m1, m2, strat->R);
2654    }
2655    else if (strat->P.p1 == NULL)
2656    {
2657      if (strat->minim > 0)
2658        strat->P.p2=p_Copy(strat->P.p, currRing, strat->tailRing);
2659      // for input polys, prepare reduction
2660      strat->P.PrepareRed(strat->use_buckets);
2661    }
2662
2663    if (strat->P.p == NULL && strat->P.t_p == NULL)
2664    {
2665      red_result = 0;
2666    }
2667    else
2668    {
2669      if (TEST_OPT_PROT)
2670        message((strat->honey ? strat->P.ecart : 0) + strat->P.pFDeg(),
2671            &olddeg,&reduc,strat, red_result);
2672
2673#ifdef DEBUGF5
2674      Print("Poly before red: ");
2675      pWrite(strat->P.p);
2676#endif
2677      /* complete reduction of the element choosen from L */
2678      red_result = strat->red2(&strat->P,strat);
2679      if (errorreported)  break;
2680    }
2681
2682    if (strat->overflow)
2683    {
2684      if (!kStratChangeTailRing(strat)) { Werror("OVERFLOW.."); break;}
2685    }
2686
2687    // reduction to non-zero new poly
2688    if (red_result == 1)
2689    {
2690      // get the polynomial (canonicalize bucket, make sure P.p is set)
2691      strat->P.GetP(strat->lmBin);
2692      // in the homogeneous case FDeg >= pFDeg (sugar/honey)
2693      // but now, for entering S, T, we reset it
2694      // in the inhomogeneous case: FDeg == pFDeg
2695      if (strat->homog) strat->initEcart(&(strat->P));
2696
2697      /* statistic */
2698      if (TEST_OPT_PROT) PrintS("s");
2699
2700      int pos=posInS(strat,strat->sl,strat->P.p,strat->P.ecart);
2701
2702#ifdef KDEBUG
2703#if MYTEST
2704      PrintS("New S: "); pDebugPrint(strat->P.p); PrintLn();
2705#endif /* MYTEST */
2706#endif /* KDEBUG */
2707
2708      // reduce the tail and normalize poly
2709      // in the ring case we cannot expect LC(f) = 1,
2710      // therefore we call pContent instead of pNorm
2711#if F5CTAILRED
2712      BOOLEAN withT = TRUE;
2713      if ((TEST_OPT_INTSTRATEGY) || (rField_is_Ring(currRing)))
2714      {
2715        strat->P.pCleardenom();
2716        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
2717        {
2718          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
2719          strat->P.pCleardenom();
2720        }
2721      }
2722      else
2723      {
2724        strat->P.pNorm();
2725        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
2726          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
2727      }
2728#endif
2729#ifdef KDEBUG
2730      if (TEST_OPT_DEBUG){PrintS("new s:");strat->P.wrp();PrintLn();}
2731#if MYTEST
2732//#if 1
2733      PrintS("New (reduced) S: "); pDebugPrint(strat->P.p); PrintLn();
2734#endif /* MYTEST */
2735#endif /* KDEBUG */
2736
2737      // min_std stuff
2738      if ((strat->P.p1==NULL) && (strat->minim>0))
2739      {
2740        if (strat->minim==1)
2741        {
2742          strat->M->m[minimcnt]=p_Copy(strat->P.p,currRing,strat->tailRing);
2743          p_Delete(&strat->P.p2, currRing, strat->tailRing);
2744        }
2745        else
2746        {
2747          strat->M->m[minimcnt]=strat->P.p2;
2748          strat->P.p2=NULL;
2749        }
2750        if (strat->tailRing!=currRing && pNext(strat->M->m[minimcnt])!=NULL)
2751          pNext(strat->M->m[minimcnt])
2752            = strat->p_shallow_copy_delete(pNext(strat->M->m[minimcnt]),
2753                strat->tailRing, currRing,
2754                currRing->PolyBin);
2755        minimcnt++;
2756      }
2757
2758      // enter into S, L, and T
2759      // here we need to recompute new signatures, but those are trivial ones
2760      //if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
2761      enterT(strat->P, strat);
2762      // posInS only depends on the leading term
2763      strat->enterS(strat->P, pos, strat, strat->tl);
2764//#if 1
2765#ifdef DEBUGF5
2766      Print("ELEMENT ADDED TO GCURR DURING INTERRED: ");
2767      pWrite(pHead(strat->S[strat->sl]));
2768      pWrite(strat->sig[strat->sl]);
2769#endif
2770      if (hilb!=NULL) khCheck(Q,w,hilb,hilbeledeg,hilbcount,strat);
2771      //      Print("[%d]",hilbeledeg);
2772      if (strat->P.lcm!=NULL)
2773#ifdef HAVE_RINGS
2774        pLmDelete(strat->P.lcm);
2775#else
2776      pLmFree(strat->P.lcm);
2777#endif
2778      if (strat->sl>srmax) srmax = strat->sl;
2779    }
2780    else
2781    {
2782      // adds signature of the zero reduction to
2783      // strat->syz. This is the leading term of
2784      // syzygy and can be used in syzCriterion()
2785      // the signature is added if and only if the
2786      // pair was not detected by the rewritten criterion in strat->red = redSig
2787      if (strat->P.p1 == NULL && strat->minim > 0)
2788      {
2789        p_Delete(&strat->P.p2, currRing, strat->tailRing);
2790      }
2791    }
2792
2793#ifdef KDEBUG
2794    memset(&(strat->P), 0, sizeof(strat->P));
2795#endif /* KDEBUG */
2796  }
2797  int cc = 0;
2798  while (cc<strat->tl+1)
2799  {
2800    strat->T[cc].sig        = pOne();
2801    p_SetComp(strat->T[cc].sig,cc+1,currRing);
2802    strat->T[cc].sevSig     = pGetShortExpVector(strat->T[cc].sig);
2803    strat->sig[cc]          = strat->T[cc].sig;
2804    strat->sevSig[cc]       = strat->T[cc].sevSig;
2805    strat->T[cc].is_sigsafe = TRUE;
2806    cc++;
2807  }
2808  strat->max_lower_index = strat->tl;
2809  // set current signature index of upcoming iteration step
2810  // NOTE:  this needs to be set here, as otherwise initSyzRules cannot compute
2811  //        the corresponding syzygy rules correctly
2812  strat->currIdx = cc+1;
2813  for (int cd=strat->Ll; cd>=0; cd--)
2814  {
2815    p_SetComp(strat->L[cd].sig,cc+1,currRing);
2816    cc++;
2817  }
2818//#if 1
2819#if DEBUGF5
2820  Print("------------------- STRAT S ---------------------\n");
2821  cc = 0;
2822  while (cc<strat->tl+1)
2823  {
2824    pWrite(pHead(strat->S[cc]));
2825    pWrite(strat->sig[cc]);
2826    printf("- - - - - -\n");
2827    cc++;
2828  }
2829  Print("-------------------------------------------------\n");
2830  Print("------------------- STRAT T ---------------------\n");
2831  cc = 0;
2832  while (cc<strat->tl+1)
2833  {
2834    pWrite(pHead(strat->T[cc].p));
2835    pWrite(strat->T[cc].sig);
2836    printf("- - - - - -\n");
2837    cc++;
2838  }
2839  Print("-------------------------------------------------\n");
2840  Print("------------------- STRAT L ---------------------\n");
2841  cc = 0;
2842  while (cc<strat->Ll+1)
2843  {
2844    pWrite(pHead(strat->L[cc].p));
2845    pWrite(pHead(strat->L[cc].p1));
2846    pWrite(pHead(strat->L[cc].p2));
2847    pWrite(strat->L[cc].sig);
2848    printf("- - - - - -\n");
2849    cc++;
2850  }
2851  Print("-------------------------------------------------\n");
2852  printf("F5C DONE\nSTRAT SL: %d -- %d\n",strat->sl, strat->currIdx);
2853#endif
2854
2855}
2856#endif
2857
2858/* shiftgb stuff */
2859#ifdef HAVE_SHIFTBBA
2860
2861
2862ideal bbaShift(ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat, int uptodeg, int lV)
2863{
2864#ifdef KDEBUG
2865  bba_count++;
2866  int loop_count = 0;
2867#endif
2868  int   red_result = 1;
2869  int   olddeg,reduc;
2870  int hilbeledeg=1,hilbcount=0,minimcnt=0;
2871  BOOLEAN withT = TRUE; // very important for shifts
2872
2873  initBuchMoraCrit(strat); /*set Gebauer, honey, sugarCrit, NO CHANGES */
2874  initBuchMoraPos(strat); /*NO CHANGES YET: perhaps later*/
2875  initHilbCrit(F,Q,&hilb,strat); /*NO CHANGES*/
2876  initBbaShift(F,strat); /* DONE */
2877  /*set enterS, spSpolyShort, reduce, red, initEcart, initEcartPair*/
2878  /*Shdl=*/initBuchMoraShift(F, Q,strat); /* updateS with no toT, i.e. no init for T */
2879  updateSShift(strat,uptodeg,lV); /* initializes T */
2880
2881  if (strat->minim>0) strat->M=idInit(IDELEMS(F),F->rank);
2882  reduc = olddeg = 0;
2883  strat->lV=lV;
2884
2885#ifndef NO_BUCKETS
2886  if (!TEST_OPT_NOT_BUCKETS)
2887    strat->use_buckets = 1;
2888#endif
2889
2890  // redtailBBa against T for inhomogenous input
2891  //  if (!TEST_OPT_OLDSTD)
2892  //    withT = ! strat->homog;
2893
2894  // strat->posInT = posInT_pLength;
2895  assume(kTest_TS(strat));
2896
2897#ifdef HAVE_TAIL_RING
2898  kStratInitChangeTailRing(strat);
2899#endif
2900
2901  /* compute------------------------------------------------------- */
2902  while (strat->Ll >= 0)
2903  {
2904#ifdef KDEBUG
2905    loop_count++;
2906    if (TEST_OPT_DEBUG) messageSets(strat);
2907#endif
2908    if (strat->Ll== 0) strat->interpt=TRUE;
2909    if (TEST_OPT_DEGBOUND
2910        && ((strat->honey && (strat->L[strat->Ll].ecart+currRing->pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
2911            || ((!strat->honey) && (currRing->pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))))
2912    {
2913      /*
2914       *stops computation if
2915       * 24 IN test and the degree +ecart of L[strat->Ll] is bigger then
2916       *a predefined number Kstd1_deg
2917       */
2918      while ((strat->Ll >= 0)
2919        && (strat->L[strat->Ll].p1!=NULL) && (strat->L[strat->Ll].p2!=NULL)
2920        && ((strat->honey && (strat->L[strat->Ll].ecart+currRing->pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
2921            || ((!strat->honey) && (currRing->pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg)))
2922        )
2923        deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
2924      if (strat->Ll<0) break;
2925      else strat->noClearS=TRUE;
2926    }
2927    /* picks the last element from the lazyset L */
2928    strat->P = strat->L[strat->Ll];
2929    strat->Ll--;
2930
2931    if (pNext(strat->P.p) == strat->tail)
2932    {
2933      // deletes the short spoly
2934      pLmFree(strat->P.p);
2935      strat->P.p = NULL;
2936      poly m1 = NULL, m2 = NULL;
2937
2938      // check that spoly creation is ok
2939      while (strat->tailRing != currRing &&
2940             !kCheckSpolyCreation(&(strat->P), strat, m1, m2))
2941      {
2942        assume(m1 == NULL && m2 == NULL);
2943        // if not, change to a ring where exponents are at least
2944        // large enough
2945        kStratChangeTailRing(strat);
2946      }
2947      // create the real one
2948      ksCreateSpoly(&(strat->P), NULL, strat->use_buckets,
2949                    strat->tailRing, m1, m2, strat->R);
2950    }
2951    else if (strat->P.p1 == NULL)
2952    {
2953      if (strat->minim > 0)
2954        strat->P.p2=p_Copy(strat->P.p, currRing, strat->tailRing);
2955      // for input polys, prepare reduction
2956      strat->P.PrepareRed(strat->use_buckets);
2957    }
2958
2959    poly qq;
2960
2961    /* here in the nonhomog case we shrink the new spoly */
2962
2963    if ( ! strat->homog)
2964    {
2965      strat->P.GetP(strat->lmBin); // because shifts are counted with .p structure
2966      /* in the nonhomog case we have to shrink the polynomial */
2967      assume(strat->P.t_p!=NULL);
2968      qq = p_Shrink(strat->P.t_p, lV, strat->tailRing); // direct shrink
2969      if (qq != NULL)
2970      {
2971         /* we're here if Shrink is nonzero */
2972        //         strat->P.p =  NULL;
2973        //        strat->P.Delete(); /* deletes P.p and P.t_p */ //error
2974        strat->P.p   =  NULL; // is not set by Delete
2975        strat->P.t_p =  qq;
2976        strat->P.GetP(strat->lmBin);
2977        // update sev and length
2978        strat->initEcart(&(strat->P));
2979        strat->P.sev = pGetShortExpVector(strat->P.p);
2980//         strat->P.FDeg = strat->P.pFDeg();
2981//         strat->P.length = strat->P.pLDeg();
2982//         strat->P.pLength =strat->P.GetpLength(); //pLength(strat->P.p);
2983      }
2984      else
2985      {
2986         /* Shrink is zero, like y(1)*y(2) - y(1)*y(3)*/
2987#ifdef KDEBUG
2988         if (TEST_OPT_DEBUG){PrintS("nonzero s shrinks to 0");PrintLn();}
2989#endif
2990         //         strat->P.Delete();  // cause error
2991         strat->P.p = NULL;
2992         strat->P.t_p = NULL;
2993           //         strat->P.p = NULL; // or delete strat->P.p ?
2994       }
2995    }
2996      /* end shrinking poly in the nonhomog case */
2997
2998    if (strat->P.p == NULL && strat->P.t_p == NULL)
2999    {
3000      red_result = 0;
3001    }
3002    else
3003    {
3004      if (TEST_OPT_PROT)
3005        message((strat->honey ? strat->P.ecart : 0) + strat->P.pFDeg(),
3006                &olddeg,&reduc,strat, red_result);
3007
3008      /* reduction of the element choosen from L */
3009      red_result = strat->red(&strat->P,strat);
3010    }
3011
3012    // reduction to non-zero new poly
3013    if (red_result == 1)
3014    {
3015      /* statistic */
3016      if (TEST_OPT_PROT) PrintS("s");
3017
3018      // get the polynomial (canonicalize bucket, make sure P.p is set)
3019      strat->P.GetP(strat->lmBin);
3020
3021      int pos=posInS(strat,strat->sl,strat->P.p,strat->P.ecart);
3022
3023      // reduce the tail and normalize poly
3024      if (TEST_OPT_INTSTRATEGY)
3025      {
3026        strat->P.pCleardenom();
3027        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
3028        {
3029          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
3030          strat->P.pCleardenom();
3031        }
3032      }
3033      else
3034      {
3035        strat->P.pNorm();
3036        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
3037          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
3038      }
3039
3040      // here we must shrink again! and optionally reduce again
3041      // or build shrink into redtailBba!
3042
3043#ifdef KDEBUG
3044      if (TEST_OPT_DEBUG){PrintS("new s:");strat->P.wrp();PrintLn();}
3045#endif
3046
3047      // min_std stuff
3048      if ((strat->P.p1==NULL) && (strat->minim>0))
3049      {
3050        if (strat->minim==1)
3051        {
3052          strat->M->m[minimcnt]=p_Copy(strat->P.p,currRing,strat->tailRing);
3053          p_Delete(&strat->P.p2, currRing, strat->tailRing);
3054        }
3055        else
3056        {
3057          strat->M->m[minimcnt]=strat->P.p2;
3058          strat->P.p2=NULL;
3059        }
3060        if (strat->tailRing!=currRing && pNext(strat->M->m[minimcnt])!=NULL)
3061          pNext(strat->M->m[minimcnt])
3062            = strat->p_shallow_copy_delete(pNext(strat->M->m[minimcnt]),
3063                                           strat->tailRing, currRing,
3064                                           currRing->PolyBin);
3065        minimcnt++;
3066      }
3067
3068    /* here in the nonhomog case we shrink the reduced poly AGAIN */
3069
3070    if ( ! strat->homog)
3071    {
3072      strat->P.GetP(strat->lmBin); // because shifts are counted with .p structure
3073      /* assume strat->P.t_p != NULL */
3074      /* in the nonhomog case we have to shrink the polynomial */
3075      assume(strat->P.t_p!=NULL); // poly qq defined above
3076      qq = p_Shrink(strat->P.t_p, lV, strat->tailRing); // direct shrink
3077      if (qq != NULL)
3078      {
3079         /* we're here if Shrink is nonzero */
3080        //         strat->P.p =  NULL;
3081        //        strat->P.Delete(); /* deletes P.p and P.t_p */ //error
3082        strat->P.p   =  NULL; // is not set by Delete
3083        strat->P.t_p =  qq;
3084        strat->P.GetP(strat->lmBin);
3085        // update sev and length
3086        strat->initEcart(&(strat->P));
3087        strat->P.sev = pGetShortExpVector(strat->P.p);
3088      }
3089      else
3090      {
3091         /* Shrink is zero, like y(1)*y(2) - y(1)*y(3)*/
3092#ifdef PDEBUG
3093         if (TEST_OPT_DEBUG){PrintS("nonzero s shrinks to 0");PrintLn();}
3094#endif
3095         //         strat->P.Delete();  // cause error
3096         strat->P.p = NULL;
3097         strat->P.t_p = NULL;
3098           //         strat->P.p = NULL; // or delete strat->P.p ?
3099         goto     red_shrink2zero;
3100       }
3101    }
3102      /* end shrinking poly AGAIN in the nonhomog case */
3103
3104
3105      // enter into S, L, and T
3106      //if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
3107      //        enterT(strat->P, strat); // this was here before Shift stuff
3108      //enterTShift(LObject p, kStrategy strat, int atT, int uptodeg, int lV); // syntax
3109      // the default value for atT = -1 as in bba
3110      /*   strat->P.GetP(); */
3111      // because shifts are counted with .p structure // done before, but ?
3112      enterTShift(strat->P,strat,-1,uptodeg, lV);
3113      enterpairsShift(strat->P.p,strat->sl,strat->P.ecart,pos,strat, strat->tl,uptodeg,lV);
3114      //      enterpairsShift(vw,strat->sl,strat->P.ecart,pos,strat, strat->tl,uptodeg,lV);
3115      // posInS only depends on the leading term
3116      strat->enterS(strat->P, pos, strat, strat->tl);
3117
3118      if (hilb!=NULL) khCheck(Q,w,hilb,hilbeledeg,hilbcount,strat);
3119//      Print("[%d]",hilbeledeg);
3120      if (strat->P.lcm!=NULL) pLmFree(strat->P.lcm);
3121    }
3122    else
3123    {
3124    red_shrink2zero:
3125      if (strat->P.p1 == NULL && strat->minim > 0)
3126      {
3127        p_Delete(&strat->P.p2, currRing, strat->tailRing);
3128      }
3129    }
3130#ifdef KDEBUG
3131    memset(&(strat->P), 0, sizeof(strat->P));
3132#endif
3133    assume(kTest_TS(strat));
3134  }
3135#ifdef KDEBUG
3136  if (TEST_OPT_DEBUG) messageSets(strat);
3137#endif
3138  /* complete reduction of the standard basis--------- */
3139  /*  shift case: look for elt's in S such that they are divisible by elt in T */
3140  //  if (TEST_OPT_SB_1)
3141  if (TEST_OPT_REDSB)
3142  {
3143    int k=0;
3144    int j=-1;
3145    while(k<=strat->sl)
3146    {
3147//       loop
3148//       {
3149//         if (j>=k) break;
3150//         clearS(strat->S[j],strat->sevS[j],&k,&j,strat);
3151//         j++;
3152//       }
3153      LObject Ln (strat->S[k],currRing, strat->tailRing);
3154      Ln.SetShortExpVector();
3155      j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, &Ln, j+1);
3156      if (j<0) {  k++; j=-1;}
3157      else
3158      {
3159        if ( pLmCmp(strat->S[k],strat->T[j].p) == 0)
3160        {
3161          j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, &Ln, j+1);
3162          if (j<0) {  k++; j=-1;}
3163          else
3164          {
3165            deleteInS(k,strat);
3166          }
3167        }
3168        else
3169        {
3170          deleteInS(k,strat);
3171        }
3172      }
3173    }
3174  }
3175
3176  if (TEST_OPT_REDSB)
3177  {    completeReduce(strat, TRUE); //shift: withT = TRUE
3178    if (strat->completeReduce_retry)
3179    {
3180      // completeReduce needed larger exponents, retry
3181      // to reduce with S (instead of T)
3182      // and in currRing (instead of strat->tailRing)
3183      cleanT(strat);strat->tailRing=currRing;
3184      int i;
3185      for(i=strat->sl;i>=0;i--) strat->S_2_R[i]=-1;
3186      completeReduce(strat, TRUE);
3187    }
3188  }
3189  else if (TEST_OPT_PROT) PrintLn();
3190
3191  /* release temp data-------------------------------- */
3192  exitBuchMora(strat);
3193//  if (TEST_OPT_WEIGHTM)
3194//  {
3195//    pRestoreDegProcs(currRing,pFDegOld, pLDegOld);
3196//    if (ecartWeights)
3197//    {
3198//      omFreeSize((ADDRESS)ecartWeights,((currRing->N)+1)*sizeof(short));
3199//      ecartWeights=NULL;
3200//    }
3201//  }
3202  if (TEST_OPT_PROT) messageStat(hilbcount,strat);
3203  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
3204  return (strat->Shdl);
3205}
3206
3207
3208ideal freegb(ideal I, int uptodeg, int lVblock)
3209{
3210  /* todo main call */
3211
3212  /* assume: ring is prepared, ideal is copied into shifted ring */
3213  /* uptodeg and lVblock are correct - test them! */
3214
3215  /* check whether the ideal is in V */
3216
3217//  if (0)
3218  if (! ideal_isInV(I,lVblock) )
3219  {
3220    WerrorS("The input ideal contains incorrectly encoded elements! ");
3221    return(NULL);
3222  }
3223
3224  //  kStrategy strat = new skStrategy;
3225  /* ideal bbaShift(ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat, int uptodeg, int lV) */
3226  /* at the moment:
3227- no quotient (check)
3228- no *w, no *hilb
3229  */
3230  /* ideal F, ideal Q, tHomog h,intvec ** w, intvec *hilb,int syzComp,
3231     int newIdeal, intvec *vw) */
3232  ideal RS = kStdShift(I,NULL, testHomog, NULL,NULL,0,0,NULL, uptodeg, lVblock);
3233    //bbaShift(I,NULL, NULL, NULL, strat, uptodeg, lVblock);
3234  idSkipZeroes(RS);
3235  return(RS);
3236}
3237
3238/*2
3239*reduces h with elements from T choosing  the first possible
3240* element in t with respect to the given pDivisibleBy
3241*/
3242int redFirstShift (LObject* h,kStrategy strat)
3243{
3244  if (h->IsNull()) return 0;
3245
3246  int at, reddeg,d;
3247  int pass = 0;
3248  int j = 0;
3249
3250  if (! strat->homog)
3251  {
3252    d = h->GetpFDeg() + h->ecart;
3253    reddeg = strat->LazyDegree+d;
3254  }
3255  h->SetShortExpVector();
3256  loop
3257  {
3258    j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
3259    if (j < 0)
3260    {
3261      h->SetDegStuffReturnLDeg(strat->LDegLast);
3262      return 1;
3263    }
3264
3265    if (!TEST_OPT_INTSTRATEGY)
3266      strat->T[j].pNorm();
3267#ifdef KDEBUG
3268    if (TEST_OPT_DEBUG)
3269    {
3270      PrintS("reduce ");
3271      h->wrp();
3272      PrintS(" with ");
3273      strat->T[j].wrp();
3274    }
3275#endif
3276    ksReducePoly(h, &(strat->T[j]), strat->kNoetherTail(), NULL, strat);
3277    if (!h->IsNull())
3278    {
3279      poly qq=p_Shrink(h->GetTP(),strat->lV,strat->tailRing);
3280      h->p=NULL;
3281      h->t_p=qq;
3282      if (qq!=NULL) h->GetP(strat->lmBin);
3283    }
3284
3285#ifdef KDEBUG
3286    if (TEST_OPT_DEBUG)
3287    {
3288      PrintS(" to ");
3289      wrp(h->p);
3290      PrintLn();
3291    }
3292#endif
3293    if (h->IsNull())
3294    {
3295      if (h->lcm!=NULL) pLmFree(h->lcm);
3296      h->Clear();
3297      return 0;
3298    }
3299    h->SetShortExpVector();
3300
3301#if 0
3302    if ((strat->syzComp!=0) && !strat->honey)
3303    {
3304      if ((strat->syzComp>0) &&
3305          (h->Comp() > strat->syzComp))
3306      {
3307        assume(h->MinComp() > strat->syzComp);
3308#ifdef KDEBUG
3309        if (TEST_OPT_DEBUG) PrintS(" > syzComp\n");
3310#endif
3311        if (strat->homog)
3312          h->SetDegStuffReturnLDeg(strat->LDegLast);
3313        return -2;
3314      }
3315    }
3316#endif
3317    if (!strat->homog)
3318    {
3319      if (!TEST_OPT_OLDSTD && strat->honey)
3320      {
3321        h->SetpFDeg();
3322        if (strat->T[j].ecart <= h->ecart)
3323          h->ecart = d - h->GetpFDeg();
3324        else
3325          h->ecart = d - h->GetpFDeg() + strat->T[j].ecart - h->ecart;
3326
3327        d = h->GetpFDeg() + h->ecart;
3328      }
3329      else
3330        d = h->SetDegStuffReturnLDeg(strat->LDegLast);
3331      /*- try to reduce the s-polynomial -*/
3332      pass++;
3333      /*
3334       *test whether the polynomial should go to the lazyset L
3335       *-if the degree jumps
3336       *-if the number of pre-defined reductions jumps
3337       */
3338      if (!TEST_OPT_REDTHROUGH && (strat->Ll >= 0)
3339          && ((d >= reddeg) || (pass > strat->LazyPass)))
3340      {
3341        h->SetLmCurrRing();
3342        if (strat->posInLDependsOnLength)
3343          h->SetLength(strat->length_pLength);
3344        at = strat->posInL(strat->L,strat->Ll,h,strat);
3345        if (at <= strat->Ll)
3346        {
3347          int dummy=strat->sl;
3348          /*          if (kFindDivisibleByInS(strat,&dummy, h) < 0) */
3349          if (kFindDivisibleByInT(strat->T,strat->sevT, dummy, h) < 0)
3350            return 1;
3351          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
3352#ifdef KDEBUG
3353          if (TEST_OPT_DEBUG) Print(" degree jumped; ->L%d\n",at);
3354#endif
3355          h->Clear();
3356          return -1;
3357        }
3358      }
3359      if ((TEST_OPT_PROT) && (strat->Ll < 0) && (d >= reddeg))
3360      {
3361        reddeg = d+1;
3362        Print(".%d",d);mflush();
3363      }
3364    }
3365  }
3366}
3367
3368void initBbaShift(ideal /*F*/,kStrategy strat)
3369{
3370 /* setting global variables ------------------- */
3371  strat->enterS = enterSBba; /* remains as is, we change enterT! */
3372
3373  strat->red = redFirstShift; /* no redHomog ! */
3374
3375  if (currRing->pLexOrder && strat->honey)
3376    strat->initEcart = initEcartNormal;
3377  else
3378    strat->initEcart = initEcartBBA;
3379  if (strat->honey)
3380    strat->initEcartPair = initEcartPairMora;
3381  else
3382    strat->initEcartPair = initEcartPairBba;
3383//  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
3384//  {
3385//    //interred  machen   Aenderung
3386//    pFDegOld=currRing->pFDeg;
3387//    pLDegOld=pLDeg;
3388//    //h=ggetid("ecart");
3389//    //if ((h!=NULL) /*&& (IDTYP(h)==INTVEC_CMD)*/)
3390//    //{
3391//    //  ecartWeights=iv2array(IDINTVEC(h));
3392//    //}
3393//    //else
3394//    {
3395//      ecartWeights=(short *)omAlloc(((currRing->N)+1)*sizeof(short));
3396//      /*uses automatic computation of the ecartWeights to set them*/
3397//      kEcartWeights(F->m,IDELEMS(F)-1,ecartWeights,currRing);
3398//    }
3399//    pRestoreDegProcs(currRing,totaldegreeWecart, maxdegreeWecart);
3400//    if (TEST_OPT_PROT)
3401//    {
3402//      for(int i=1; i<=rVar(currRing); i++)
3403//        Print(" %d",ecartWeights[i]);
3404//      PrintLn();
3405//      mflush();
3406//    }
3407//  }
3408}
3409#endif
Note: See TracBrowser for help on using the repository browser.