source: git/kernel/GBEngine/kstd2.cc @ 11416e

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