source: git/kernel/GBEngine/kstd1.cc @ cdb1115

spielwiese
Last change on this file since cdb1115 was cdb1115, checked in by Hans Schoenemann <hannes@…>, 6 years ago
fix: memory leak in kInterRedOld
  • Property mode set to 100644
File size: 92.9 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT:
6*/
7
8// TODO: why the following is here instead of mod2.h???
9
10
11// define if buckets should be used
12#define MORA_USE_BUCKETS
13
14//#define ADIDEBUG 0
15//#define ADIDEBUG_NF 0
16#define PRE_INTEGER_CHECK 0
17
18#include "kernel/mod2.h"
19
20#include "omalloc/omalloc.h"
21
22#include "misc/options.h"
23#include "misc/intvec.h"
24
25#include "polys/weight.h"
26#include "kernel/polys.h"
27
28#include "kernel/GBEngine/kutil.h"
29#include "kernel/GBEngine/kstd1.h"
30#include "kernel/GBEngine/khstd.h"
31#include "kernel/combinatorics/stairc.h"
32#include "kernel/ideals.h"
33
34//#include "ipprint.h"
35
36#ifdef HAVE_PLURAL
37#include "polys/nc/nc.h"
38#include "polys/nc/sca.h"
39#include "kernel/GBEngine/nc.h"
40#endif
41
42#include "kernel/GBEngine/kInline.h"
43
44
45/* the list of all options which give a warning by test */
46BITSET kOptions=Sy_bit(OPT_PROT)           /*  0 */
47                |Sy_bit(OPT_REDSB)         /*  1 */
48                |Sy_bit(OPT_NOT_SUGAR)     /*  3 */
49                |Sy_bit(OPT_INTERRUPT)     /*  4 */
50                |Sy_bit(OPT_SUGARCRIT)     /*  5 */
51                |Sy_bit(OPT_REDTHROUGH)
52                |Sy_bit(OPT_OLDSTD)
53                |Sy_bit(OPT_FASTHC)        /* 10 */
54                |Sy_bit(OPT_INTSTRATEGY)   /* 26 */
55                |Sy_bit(OPT_INFREDTAIL)    /* 28 */
56                |Sy_bit(OPT_NOTREGULARITY) /* 30 */
57                |Sy_bit(OPT_WEIGHTM);      /* 31 */
58
59/* the list of all options which may be used by option and test */
60/* defintion of ALL options: libpolys/misc/options.h */
61BITSET validOpts=Sy_bit(0)
62                |Sy_bit(1)
63                |Sy_bit(2) // obachman 10/00: replaced by notBucket
64                |Sy_bit(3)
65                |Sy_bit(4)
66                |Sy_bit(5)
67                |Sy_bit(6)
68//                |Sy_bit(7) obachman 11/00 tossed: 12/00 used for redThrough
69                |Sy_bit(7) // OPT_REDTHROUGH
70                |Sy_bit(8) // obachman 11/00 tossed -> motsak 2011 experimental: OPT_NO_SYZ_MINIM
71                |Sy_bit(9)
72                |Sy_bit(10)
73                |Sy_bit(11)
74                |Sy_bit(12)
75                |Sy_bit(13)
76                |Sy_bit(14)
77                |Sy_bit(15)
78                |Sy_bit(16)
79                |Sy_bit(17)
80                |Sy_bit(18)
81                |Sy_bit(19)
82//                |Sy_bit(20) obachman 11/00 tossed: 12/00 used for redOldStd
83                |Sy_bit(OPT_OLDSTD)
84                |Sy_bit(21)
85                |Sy_bit(22)
86                /*|Sy_bit(23)*/
87                /*|Sy_bit(24)*/
88                |Sy_bit(OPT_REDTAIL)
89                |Sy_bit(OPT_INTSTRATEGY)
90                |Sy_bit(27)
91                |Sy_bit(28)
92                |Sy_bit(29)
93                |Sy_bit(30)
94                |Sy_bit(31);
95
96//static BOOLEAN posInLOldFlag;
97           /*FALSE, if posInL == posInL10*/
98// returns TRUE if mora should use buckets, false otherwise
99static BOOLEAN kMoraUseBucket(kStrategy strat);
100
101static void kOptimizeLDeg(pLDegProc ldeg, kStrategy strat)
102{
103//  if (strat->ak == 0 && !rIsSyzIndexRing(currRing))
104    strat->length_pLength = TRUE;
105//  else
106//    strat->length_pLength = FALSE;
107
108  if ((ldeg == pLDeg0c /*&& !rIsSyzIndexRing(currRing)*/) ||
109      (ldeg == pLDeg0 && strat->ak == 0))
110  {
111    strat->LDegLast = TRUE;
112  }
113  else
114  {
115    strat->LDegLast = FALSE;
116  }
117}
118
119
120static int doRed (LObject* h, TObject* with,BOOLEAN intoT,kStrategy strat, bool redMoraNF)
121{
122  int ret;
123#if KDEBUG > 0
124  kTest_L(h);
125  kTest_T(with);
126#endif
127  // Hmmm ... why do we do this -- polys from T should already be normalized
128  if (!TEST_OPT_INTSTRATEGY)
129    with->pNorm();
130#ifdef KDEBUG
131  if (TEST_OPT_DEBUG)
132  {
133    PrintS("reduce ");h->wrp();PrintS(" with ");with->wrp();PrintLn();
134  }
135#endif
136  if (intoT)
137  {
138    // need to do it exacly like this: otherwise
139    // we might get errors
140    LObject L= *h;
141    L.Copy();
142    h->GetP();
143    h->length=h->pLength=pLength(h->p);
144    ret = ksReducePoly(&L, with, strat->kNoetherTail(), NULL, strat);
145    if (ret)
146    {
147      if (ret < 0) return ret;
148      if (h->tailRing != strat->tailRing)
149        h->ShallowCopyDelete(strat->tailRing,
150                             pGetShallowCopyDeleteProc(h->tailRing,
151                                                       strat->tailRing));
152    }
153    if(redMoraNF && (rField_is_Ring(currRing)))
154      enterT_strong(*h,strat);
155    else
156      enterT(*h,strat);
157    *h = L;
158  }
159  else
160    ret = ksReducePoly(h, with, strat->kNoetherTail(), NULL, strat);
161#ifdef KDEBUG
162  if (TEST_OPT_DEBUG)
163  {
164    PrintS("to ");h->wrp();PrintLn();
165  }
166#endif
167  return ret;
168}
169
170int redEcart (LObject* h,kStrategy strat)
171{
172  int i,at,ei,li,ii;
173  int j = 0;
174  int pass = 0;
175  long d,reddeg;
176
177  d = h->GetpFDeg()+ h->ecart;
178  reddeg = strat->LazyDegree+d;
179  h->SetShortExpVector();
180  loop
181  {
182    j = kFindDivisibleByInT(strat, h);
183    if (j < 0)
184    {
185      if (strat->honey) h->SetLength(strat->length_pLength);
186      return 1;
187    }
188
189    ei = strat->T[j].ecart;
190    ii = j;
191
192    if (ei > h->ecart && ii < strat->tl)
193    {
194      li = strat->T[j].length;
195      // the polynomial to reduce with (up to the moment) is;
196      // pi with ecart ei and length li
197      // look for one with smaller ecart
198      i = j;
199      loop
200      {
201        /*- takes the first possible with respect to ecart -*/
202        i++;
203#if 1
204        if (i > strat->tl) break;
205        if ((strat->T[i].ecart < ei || (strat->T[i].ecart == ei &&
206                                        strat->T[i].length < li))
207            &&
208            p_LmShortDivisibleBy(strat->T[i].GetLmTailRing(), strat->sevT[i], h->GetLmTailRing(), ~h->sev, strat->tailRing))
209#else
210          j = kFindDivisibleByInT(strat, h, i);
211        if (j < 0) break;
212        i = j;
213        if (strat->T[i].ecart < ei || (strat->T[i].ecart == ei &&
214                                        strat->T[i].length < li))
215#endif
216        {
217          // the polynomial to reduce with is now
218          ii = i;
219          ei = strat->T[i].ecart;
220          if (ei <= h->ecart) break;
221          li = strat->T[i].length;
222        }
223      }
224    }
225
226    // end of search: have to reduce with pi
227    if (ei > h->ecart)
228    {
229      // It is not possible to reduce h with smaller ecart;
230      // if possible h goes to the lazy-set L,i.e
231      // if its position in L would be not the last one
232      strat->fromT = TRUE;
233      if (!TEST_OPT_REDTHROUGH && strat->Ll >= 0) /*- L is not empty -*/
234      {
235        h->SetLmCurrRing();
236        if (strat->honey && strat->posInLDependsOnLength)
237          h->SetLength(strat->length_pLength);
238        assume(h->FDeg == h->pFDeg());
239        at = strat->posInL(strat->L,strat->Ll,h,strat);
240        if (at <= strat->Ll)
241        {
242          /*- h will not become the next element to reduce -*/
243          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
244#ifdef KDEBUG
245          if (TEST_OPT_DEBUG) Print(" ecart too big; -> L%d\n",at);
246#endif
247          h->Clear();
248          strat->fromT = FALSE;
249          return -1;
250        }
251      }
252    }
253
254    // now we finally can reduce
255    doRed(h,&(strat->T[ii]),strat->fromT,strat,FALSE);
256    strat->fromT=FALSE;
257
258    // are we done ???
259    if (h->IsNull())
260    {
261      assume(!rField_is_Ring(currRing));
262      if (h->lcm!=NULL) pLmFree(h->lcm);
263      h->Clear();
264      return 0;
265    }
266
267    // NO!
268    h->SetShortExpVector();
269    h->SetpFDeg();
270    if (strat->honey)
271    {
272      if (ei <= h->ecart)
273        h->ecart = d-h->GetpFDeg();
274      else
275        h->ecart = d-h->GetpFDeg()+ei-h->ecart;
276    }
277    else
278      // this has the side effect of setting h->length
279      h->ecart = h->pLDeg(strat->LDegLast) - h->GetpFDeg();
280#if 0
281    if (strat->syzComp!=0)
282    {
283      if ((strat->syzComp>0) && (h->Comp() > strat->syzComp))
284      {
285        assume(h->MinComp() > strat->syzComp);
286        if (strat->honey) h->SetLength();
287#ifdef KDEBUG
288        if (TEST_OPT_DEBUG) PrintS(" > syzComp\n");
289#endif
290        return -2;
291      }
292    }
293#endif
294    /*- try to reduce the s-polynomial -*/
295    pass++;
296    d = h->GetpFDeg()+h->ecart;
297    /*
298     *test whether the polynomial should go to the lazyset L
299     *-if the degree jumps
300     *-if the number of pre-defined reductions jumps
301     */
302    if (!TEST_OPT_REDTHROUGH && (strat->Ll >= 0)
303        && ((d >= reddeg) || (pass > strat->LazyPass)))
304    {
305      h->SetLmCurrRing();
306      if (strat->honey && strat->posInLDependsOnLength)
307        h->SetLength(strat->length_pLength);
308      assume(h->FDeg == h->pFDeg());
309      at = strat->posInL(strat->L,strat->Ll,h,strat);
310      if (at <= strat->Ll)
311      {
312        int dummy=strat->sl;
313        if (kFindDivisibleByInS(strat, &dummy, h) < 0)
314        {
315          if (strat->honey && !strat->posInLDependsOnLength)
316            h->SetLength(strat->length_pLength);
317          return 1;
318        }
319        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
320#ifdef KDEBUG
321        if (TEST_OPT_DEBUG) Print(" degree jumped; ->L%d\n",at);
322#endif
323        h->Clear();
324        return -1;
325      }
326    }
327    else if ((TEST_OPT_PROT) && (strat->Ll < 0) && (d >= reddeg))
328    {
329      Print(".%ld",d);mflush();
330      reddeg = d+1;
331      if (h->pTotalDeg()+h->ecart >= (int)strat->tailRing->bitmask)
332      {
333        strat->overflow=TRUE;
334        //Print("OVERFLOW in redEcart d=%ld, max=%ld",d,strat->tailRing->bitmask);
335        h->GetP();
336        at = strat->posInL(strat->L,strat->Ll,h,strat);
337        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
338        h->Clear();
339        return -1;
340      }
341    }
342  }
343}
344
345#ifdef HAVE_RINGS
346int redRiloc (LObject* h,kStrategy strat)
347{
348  int i,at,ei,li,ii;
349  int j = 0;
350  int pass = 0;
351  long d,reddeg;
352
353
354#ifdef ADIDEBUG_NF
355  int iii;
356  PrintS("\n---------------------------- NEW REDRILOC COMPUTATION ----------------------------\n");
357  PrintS("    The pair h :\n");
358  PrintS("\n      p1 = "); p_Write(h->p1,strat->tailRing);
359  PrintS("\n      p2 = "); p_Write(h->p2,strat->tailRing);
360  PrintS("\n      p  = "); p_Write(h->p,strat->tailRing);
361  PrintS("\n    The actual reducer T is: ");
362  if(strat->tl<0)
363    {PrintS(" Empty.\n");}
364  else
365  {
366    for (iii=0;iii<=strat->tl;iii++)
367    {
368      Print("\n      T[%i] = ",iii);p_Write(strat->T[iii].p,strat->tailRing);
369    }
370  }
371#endif /* ADIDEBUG_NF */
372
373  d = h->GetpFDeg()+ h->ecart;
374  reddeg = strat->LazyDegree+d;
375  h->SetShortExpVector();
376#ifdef ADIDEBUG_NF
377  Print("\n  Searching for a poly in T that divides h (of ecart %i) ...\n",h->ecart);
378#endif
379  loop
380  {
381    j = kFindDivisibleByInT(strat, h);
382#ifdef ADIDEBUG_NF
383    if(j != -1)
384    {
385      ei = strat->T[j].ecart;
386      Print("\n    Found one: T[%i] of ecart %i: ",j,ei);
387      p_Write(strat->T[j].p,strat->tailRing);
388      PrintS("\n    Try to find another with smaller ecart:\n");
389    }
390    else
391    {
392      PrintS("\n    No poly in T divides h.\n");
393    }
394    //getchar();
395#endif
396    if (j < 0)
397    {
398      // over ZZ: cleanup coefficients by complete reduction with monomials
399      postReduceByMon(h, strat);
400      if(h->p == NULL)
401      {
402        if (h->lcm!=NULL) pLmDelete(h->lcm);
403        h->Clear();
404        return 0;
405      }
406      if (strat->honey) h->SetLength(strat->length_pLength);
407      if(strat->tl >= 0)
408          h->i_r1 = strat->tl;
409      else
410          h->i_r1 = -1;
411      if (h->GetLmTailRing() == NULL)
412      {
413        if (h->lcm!=NULL) pLmDelete(h->lcm);
414        h->Clear();
415        return 0;
416      }
417      return 1;
418    }
419
420    ei = strat->T[j].ecart;
421    ii = j;
422#ifdef ADIDEBUG_NF
423    iii=ii;
424#endif
425    if (ei > h->ecart && ii < strat->tl)
426    {
427      li = strat->T[j].length;
428      // the polynomial to reduce with (up to the moment) is;
429      // pi with ecart ei and length li
430      // look for one with smaller ecart
431      i = j;
432      loop
433      {
434        /*- takes the first possible with respect to ecart -*/
435        i++;
436#if 1
437        if (i > strat->tl) break;
438        if ((strat->T[i].ecart < ei || (strat->T[i].ecart == ei &&
439                                        strat->T[i].length < li))
440            &&
441            p_LmShortDivisibleBy(strat->T[i].GetLmTailRing(), strat->sevT[i], h->GetLmTailRing(), ~h->sev, strat->tailRing)
442            &&
443            n_DivBy(h->p->coef,strat->T[i].p->coef,strat->tailRing->cf))
444#else
445          j = kFindDivisibleByInT(strat, h, i);
446        if (j < 0) break;
447        i = j;
448        if (strat->T[i].ecart < ei || (strat->T[i].ecart == ei &&
449                                        strat->T[i].length < li))
450#endif
451        {
452          // the polynomial to reduce with is now
453          #ifdef ADIDEBUG_NF
454          printf("\n    Intermidiate one, h.ecart = %i < ecart = %i < ei = %i: ",h->ecart,strat->T[i].ecart, ei);
455          pWrite(strat->T[i].p);
456          #endif
457          ii = i;
458          ei = strat->T[i].ecart;
459          if (ei <= h->ecart) break;
460          li = strat->T[i].length;
461        }
462      }
463
464#ifdef ADIDEBUG_NF
465      if(iii == ii)
466      {
467        PrintS("\n    None was found.\n");
468      }
469      else
470      {
471        Print("\n    A better one (ecart = %i): T[%i] = ",ei,ii);
472        p_Write(strat->T[ii].p,strat->tailRing);
473        PrintLn();
474      }
475#endif
476    }
477
478    // end of search: have to reduce with pi
479    if (ei > h->ecart)
480    {
481      #ifdef ADIDEBUG_NF
482      printf("\nHAD TO REDUCE WITH BIGGER ECART!!!\n");
483      #endif
484      // It is not possible to reduce h with smaller ecart;
485      // if possible h goes to the lazy-set L,i.e
486      // if its position in L would be not the last one
487      strat->fromT = TRUE;
488      if (!TEST_OPT_REDTHROUGH && strat->Ll >= 0) /*- L is not empty -*/
489      {
490        h->SetLmCurrRing();
491        if (strat->honey && strat->posInLDependsOnLength)
492          h->SetLength(strat->length_pLength);
493        assume(h->FDeg == h->pFDeg());
494        at = strat->posInL(strat->L,strat->Ll,h,strat);
495        if (at <= strat->Ll && pLmCmp(h->p, strat->L[strat->Ll].p) != 0 && !nEqual(h->p->coef, strat->L[strat->Ll].p->coef))
496        {
497          /*- h will not become the next element to reduce -*/
498          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
499          #ifdef KDEBUG
500          if (TEST_OPT_DEBUG) Print(" ecart too big; -> L%d\n",at);
501          #endif
502          h->Clear();
503          strat->fromT = FALSE;
504          return -1;
505        }
506      }
507      doRed(h,&(strat->T[ii]),strat->fromT,strat,TRUE);
508    }
509    else
510    {
511      // now we finally can reduce
512      doRed(h,&(strat->T[ii]),strat->fromT,strat,FALSE);
513    }
514    strat->fromT=FALSE;
515    // are we done ???
516    if (h->IsNull())
517    {
518      #ifdef ADIDEBUG_NF
519      printf("\nReduced to 0. Exit\n");
520      #endif
521      if (h->lcm!=NULL) pLmDelete(h->lcm);
522      h->Clear();
523      return 0;
524    }
525
526    // NO!
527    h->SetShortExpVector();
528    h->SetpFDeg();
529    if (strat->honey)
530    {
531      if (ei <= h->ecart)
532        h->ecart = d-h->GetpFDeg();
533      else
534        h->ecart = d-h->GetpFDeg()+ei-h->ecart;
535    }
536    else
537      // this has the side effect of setting h->length
538      h->ecart = h->pLDeg(strat->LDegLast) - h->GetpFDeg();
539    #ifdef ADIDEBUG_NF
540    printf("\n  Partial Reduced (ecart %i) h = ",h->ecart);p_Write(h->p,strat->tailRing);
541    PrintLn();
542    #endif
543    /*- try to reduce the s-polynomial -*/
544    pass++;
545    d = h->GetpFDeg()+h->ecart;
546    /*
547     *test whether the polynomial should go to the lazyset L
548     *-if the degree jumps
549     *-if the number of pre-defined reductions jumps
550     */
551    if (!TEST_OPT_REDTHROUGH && (strat->Ll >= 0)
552        && ((d >= reddeg) || (pass > strat->LazyPass)))
553    {
554      h->SetLmCurrRing();
555      if (strat->honey && strat->posInLDependsOnLength)
556        h->SetLength(strat->length_pLength);
557      assume(h->FDeg == h->pFDeg());
558      at = strat->posInL(strat->L,strat->Ll,h,strat);
559      if (at <= strat->Ll)
560      {
561        int dummy=strat->sl;
562        if (kFindDivisibleByInS(strat, &dummy, h) < 0)
563        {
564          if (strat->honey && !strat->posInLDependsOnLength)
565            h->SetLength(strat->length_pLength);
566          return 1;
567        }
568        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
569#ifdef KDEBUG
570        if (TEST_OPT_DEBUG) Print(" degree jumped; ->L%d\n",at);
571#endif
572        h->Clear();
573        return -1;
574      }
575    }
576    else if ((TEST_OPT_PROT) && (strat->Ll < 0) && (d >= reddeg))
577    {
578      Print(".%ld",d);mflush();
579      reddeg = d+1;
580      if (h->pTotalDeg()+h->ecart >= (int)strat->tailRing->bitmask)
581      {
582        strat->overflow=TRUE;
583        //Print("OVERFLOW in redEcart d=%ld, max=%ld",d,strat->tailRing->bitmask);
584        h->GetP();
585        at = strat->posInL(strat->L,strat->Ll,h,strat);
586        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
587        h->Clear();
588        return -1;
589      }
590    }
591  }
592}
593#endif
594
595/*2
596*reduces h with elements from T choosing  the first possible
597* element in t with respect to the given pDivisibleBy
598*/
599int redFirst (LObject* h,kStrategy strat)
600{
601  if (h->IsNull()) return 0;
602
603  int at;
604  long reddeg,d;
605  int pass = 0;
606  int j = 0;
607
608  if (! strat->homog)
609  {
610    d = h->GetpFDeg() + h->ecart;
611    reddeg = strat->LazyDegree+d;
612  }
613  h->SetShortExpVector();
614  loop
615  {
616    j = kFindDivisibleByInT(strat, h);
617    if (j < 0)
618    {
619      h->SetDegStuffReturnLDeg(strat->LDegLast);
620      return 1;
621    }
622
623    if (!TEST_OPT_INTSTRATEGY)
624      strat->T[j].pNorm();
625#ifdef KDEBUG
626    if (TEST_OPT_DEBUG)
627    {
628      PrintS("reduce ");
629      h->wrp();
630      PrintS(" with ");
631      strat->T[j].wrp();
632    }
633#endif
634    ksReducePoly(h, &(strat->T[j]), strat->kNoetherTail(), NULL, strat);
635#ifdef KDEBUG
636    if (TEST_OPT_DEBUG)
637    {
638      PrintS(" to ");
639      wrp(h->p);
640      PrintLn();
641    }
642#endif
643    if (h->IsNull())
644    {
645      assume(!rField_is_Ring(currRing));
646      if (h->lcm!=NULL) pLmFree(h->lcm);
647      h->Clear();
648      return 0;
649    }
650    h->SetShortExpVector();
651
652#if 0
653    if ((strat->syzComp!=0) && !strat->honey)
654    {
655      if ((strat->syzComp>0) &&
656          (h->Comp() > strat->syzComp))
657      {
658        assume(h->MinComp() > strat->syzComp);
659#ifdef KDEBUG
660        if (TEST_OPT_DEBUG) PrintS(" > syzComp\n");
661#endif
662        if (strat->homog)
663          h->SetDegStuffReturnLDeg(strat->LDegLast);
664        return -2;
665      }
666    }
667#endif
668    if (!strat->homog)
669    {
670      if (!TEST_OPT_OLDSTD && strat->honey)
671      {
672        h->SetpFDeg();
673        if (strat->T[j].ecart <= h->ecart)
674          h->ecart = d - h->GetpFDeg();
675        else
676          h->ecart = d - h->GetpFDeg() + strat->T[j].ecart - h->ecart;
677
678        d = h->GetpFDeg() + h->ecart;
679      }
680      else
681        d = h->SetDegStuffReturnLDeg(strat->LDegLast);
682      /*- try to reduce the s-polynomial -*/
683      pass++;
684      /*
685       *test whether the polynomial should go to the lazyset L
686       *-if the degree jumps
687       *-if the number of pre-defined reductions jumps
688       */
689      if (!TEST_OPT_REDTHROUGH && (strat->Ll >= 0)
690          && ((d >= reddeg) || (pass > strat->LazyPass)))
691      {
692        h->SetLmCurrRing();
693        if (strat->posInLDependsOnLength)
694          h->SetLength(strat->length_pLength);
695        at = strat->posInL(strat->L,strat->Ll,h,strat);
696        if (at <= strat->Ll)
697        {
698          int dummy=strat->sl;
699          if (kFindDivisibleByInS(strat,&dummy, h) < 0)
700            return 1;
701          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
702#ifdef KDEBUG
703          if (TEST_OPT_DEBUG) Print(" degree jumped; ->L%d\n",at);
704#endif
705          h->Clear();
706          return -1;
707        }
708      }
709      if ((TEST_OPT_PROT) && (strat->Ll < 0) && (d >= reddeg))
710      {
711        reddeg = d+1;
712        Print(".%ld",d);mflush();
713        if (h->pTotalDeg()+h->ecart >= (int)strat->tailRing->bitmask)
714        {
715          strat->overflow=TRUE;
716          //Print("OVERFLOW in redFirst d=%ld, max=%ld",d,strat->tailRing->bitmask);
717          h->GetP();
718          at = strat->posInL(strat->L,strat->Ll,h,strat);
719          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
720          h->Clear();
721          return -1;
722        }
723      }
724    }
725  }
726}
727
728/*2
729* reduces h with elements from T choosing first possible
730* element in T with respect to the given ecart
731* used for computing normal forms outside kStd
732*/
733static poly redMoraNF (poly h,kStrategy strat, int flag)
734{
735  LObject H;
736  H.p = h;
737  int j = 0;
738  int z = 10;
739  int o = H.SetpFDeg();
740  H.ecart = currRing->pLDeg(H.p,&H.length,currRing)-o;
741  if ((flag & 2) == 0) cancelunit(&H,TRUE);
742  H.sev = pGetShortExpVector(H.p);
743  unsigned long not_sev = ~ H.sev;
744  loop
745  {
746    if (j > strat->tl)
747    {
748      return H.p;
749    }
750    if (TEST_V_DEG_STOP)
751    {
752      if (kModDeg(H.p)>Kstd1_deg) pLmDelete(&H.p);
753      if (H.p==NULL) return NULL;
754    }
755    if (p_LmShortDivisibleBy(strat->T[j].GetLmTailRing(), strat->sevT[j], H.GetLmTailRing(), not_sev, strat->tailRing)
756        )
757    {
758      /*- remember the found T-poly -*/
759      // poly pi = strat->T[j].p;
760      int ei = strat->T[j].ecart;
761      int li = strat->T[j].length;
762      int ii = j;
763      /*
764      * the polynomial to reduce with (up to the moment) is;
765      * pi with ecart ei and length li
766      */
767      loop
768      {
769        /*- look for a better one with respect to ecart -*/
770        /*- stop, if the ecart is small enough (<=ecart(H)) -*/
771        j++;
772        if (j > strat->tl) break;
773        if (ei <= H.ecart) break;
774        if (((strat->T[j].ecart < ei)
775          || ((strat->T[j].ecart == ei)
776        && (strat->T[j].length < li)))
777        && pLmShortDivisibleBy(strat->T[j].p,strat->sevT[j], H.p, not_sev)
778        )
779        {
780          /*
781          * the polynomial to reduce with is now;
782          */
783          // pi = strat->T[j].p;
784          ei = strat->T[j].ecart;
785          li = strat->T[j].length;
786          ii = j;
787        }
788      }
789      /*
790      * end of search: have to reduce with pi
791      */
792      z++;
793      if (z>10)
794      {
795        pNormalize(H.p);
796        z=0;
797      }
798      if ((ei > H.ecart) && (!strat->kHEdgeFound))
799      {
800        /*
801        * It is not possible to reduce h with smaller ecart;
802        * we have to reduce with bad ecart: H has to enter in T
803        */
804        doRed(&H,&(strat->T[ii]),TRUE,strat,TRUE);
805        if (H.p == NULL)
806          return NULL;
807      }
808      else
809      {
810        /*
811        * we reduce with good ecart, h need not to be put to T
812        */
813        doRed(&H,&(strat->T[ii]),FALSE,strat,TRUE);
814        if (H.p == NULL)
815          return NULL;
816      }
817      /*- try to reduce the s-polynomial -*/
818      o = H.SetpFDeg();
819      if ((flag &2 ) == 0) cancelunit(&H,TRUE);
820      H.ecart = currRing->pLDeg(H.p,&(H.length),currRing)-o;
821      j = 0;
822      H.sev = pGetShortExpVector(H.p);
823      not_sev = ~ H.sev;
824    }
825    else
826    {
827      j++;
828    }
829  }
830}
831
832#ifdef HAVE_RINGS
833static poly redMoraNFRing (poly h,kStrategy strat, int flag)
834{
835  LObject H;
836  H.p = h;
837  int j = 0;
838  int z = 10;
839  int o = H.SetpFDeg();
840  H.ecart = currRing->pLDeg(H.p,&H.length,currRing)-o;
841  if ((flag & 2) == 0) cancelunit(&H,TRUE);
842  H.sev = pGetShortExpVector(H.p);
843  unsigned long not_sev = ~ H.sev;
844  loop
845  {
846    if (j > strat->tl)
847    {
848      return H.p;
849    }
850    if (TEST_V_DEG_STOP)
851    {
852      if (kModDeg(H.p)>Kstd1_deg) pLmDelete(&H.p);
853      if (H.p==NULL) return NULL;
854    }
855    #ifdef ADIDEBUG_NF
856    printf("\nSearching for a reducer...\n");
857    #endif
858    if (p_LmShortDivisibleBy(strat->T[j].GetLmTailRing(), strat->sevT[j], H.GetLmTailRing(), not_sev, strat->tailRing)
859        && (n_DivBy(H.p->coef, strat->T[j].p->coef,strat->tailRing->cf))
860        )
861    {
862      /*- remember the found T-poly -*/
863      // poly pi = strat->T[j].p;
864      int ei = strat->T[j].ecart;
865      int li = strat->T[j].length;
866      int ii = j;
867      #ifdef ADIDEBUG_NF
868      printf("\nFound: j = %i, ecart = %i\nTrying to find a better one...\n",j,ei);pWrite(strat->T[j].p);
869      #endif
870      /*
871      * the polynomial to reduce with (up to the moment) is;
872      * pi with ecart ei and length li
873      */
874      loop
875      {
876        /*- look for a better one with respect to ecart -*/
877        /*- stop, if the ecart is small enough (<=ecart(H)) -*/
878        j++;
879        if (j > strat->tl) break;
880        if (ei <= H.ecart) break;
881        if (((strat->T[j].ecart < ei)
882          || ((strat->T[j].ecart == ei)
883        && (strat->T[j].length < li)))
884        && pLmShortDivisibleBy(strat->T[j].p,strat->sevT[j], H.p, not_sev)
885        && (n_DivBy(H.p->coef, strat->T[j].p->coef,strat->tailRing->cf))
886        )
887        {
888          /*
889          * the polynomial to reduce with is now;
890          */
891          // pi = strat->T[j].p;
892          ei = strat->T[j].ecart;
893          li = strat->T[j].length;
894          ii = j;
895          #ifdef ADIDEBUG_NF
896          printf("\nFound a better one: j = %i, ecart = %i\nTrying to find a better one...\n",j,ei);
897          pWrite(strat->T[j].p);
898          #endif
899        }
900      }
901      /*
902      * end of search: have to reduce with pi
903      */
904      z++;
905      if (z>10)
906      {
907        pNormalize(H.p);
908        z=0;
909      }
910      if ((ei > H.ecart) && (!strat->kHEdgeFound))
911      {
912        /*
913        * It is not possible to reduce h with smaller ecart;
914        * we have to reduce with bad ecart: H has to enter in T
915        */
916        #ifdef ADIDEBUG_NF
917        printf("\nHAVE TO REDUCE IT WITH BIGGER ECART\n");
918        #endif
919        doRed(&H,&(strat->T[ii]),TRUE,strat,TRUE);
920        if (H.p == NULL)
921          return NULL;
922      }
923      else
924      {
925        /*
926        * we reduce with good ecart, h need not to be put to T
927        */
928        doRed(&H,&(strat->T[ii]),FALSE,strat,TRUE);
929        if (H.p == NULL)
930          return NULL;
931      }
932      #ifdef ADIDEBUG_NF
933      printf("\nAfter the small reduction it looks like this:\n");pWrite(H.p);
934      getchar();
935      #endif
936      /*- try to reduce the s-polynomial -*/
937      o = H.SetpFDeg();
938      if ((flag &2 ) == 0) cancelunit(&H,TRUE);
939      H.ecart = currRing->pLDeg(H.p,&(H.length),currRing)-o;
940      j = 0;
941      H.sev = pGetShortExpVector(H.p);
942      not_sev = ~ H.sev;
943    }
944    else
945    {
946      j++;
947    }
948  }
949}
950#endif
951
952/*2
953*reorders  L with respect to posInL
954*/
955void reorderL(kStrategy strat)
956{
957  int i,j,at;
958  LObject p;
959
960  for (i=1; i<=strat->Ll; i++)
961  {
962    at = strat->posInL(strat->L,i-1,&(strat->L[i]),strat);
963    if (at != i)
964    {
965      p = strat->L[i];
966      for (j=i-1; j>=at; j--) strat->L[j+1] = strat->L[j];
967      strat->L[at] = p;
968    }
969  }
970}
971
972/*2
973*reorders  T with respect to length
974*/
975void reorderT(kStrategy strat)
976{
977  int i,j,at;
978  TObject p;
979  unsigned long sev;
980
981
982  for (i=1; i<=strat->tl; i++)
983  {
984    if (strat->T[i-1].length > strat->T[i].length)
985    {
986      p = strat->T[i];
987      sev = strat->sevT[i];
988      at = i-1;
989      loop
990      {
991        at--;
992        if (at < 0) break;
993        if (strat->T[i].length > strat->T[at].length) break;
994      }
995      for (j = i-1; j>at; j--)
996      {
997        strat->T[j+1]=strat->T[j];
998        strat->sevT[j+1]=strat->sevT[j];
999        strat->R[strat->T[j+1].i_r] = &(strat->T[j+1]);
1000      }
1001      strat->T[at+1]=p;
1002      strat->sevT[at+1] = sev;
1003      strat->R[p.i_r] = &(strat->T[at+1]);
1004    }
1005  }
1006}
1007
1008/*2
1009*looks whether exactly (currRing->N)-1 axis are used
1010*returns last != 0 in this case
1011*last is the (first) unused axis
1012*/
1013void missingAxis (int* last,kStrategy strat)
1014{
1015  int   i = 0;
1016  int   k = 0;
1017
1018  *last = 0;
1019  if (!rHasMixedOrdering(currRing))
1020  {
1021    loop
1022    {
1023      i++;
1024      if (i > (currRing->N)) break;
1025      if (strat->NotUsedAxis[i])
1026      {
1027        *last = i;
1028        k++;
1029      }
1030      if (k>1)
1031      {
1032        *last = 0;
1033        break;
1034      }
1035    }
1036  }
1037}
1038
1039/*2
1040*last is the only non used axis, it looks
1041*for a monomial in p being a pure power of this
1042*variable and returns TRUE in this case
1043*(*length) gives the length between the pure power and the leading term
1044*(should be minimal)
1045*/
1046BOOLEAN hasPurePower (const poly p,int last, int *length,kStrategy strat)
1047{
1048  poly h;
1049  int i;
1050
1051  if (pNext(p) == strat->tail)
1052    return FALSE;
1053  pp_Test(p, currRing, strat->tailRing);
1054  if (strat->ak <= 0 || p_MinComp(p, currRing, strat->tailRing) == strat->ak)
1055  {
1056    i = p_IsPurePower(p, currRing);
1057    if (rField_is_Ring(currRing) && (!n_IsUnit(pGetCoeff(p), currRing->cf))) i=0;
1058    if (i == last)
1059    {
1060      *length = 0;
1061      return TRUE;
1062    }
1063    *length = 1;
1064    h = pNext(p);
1065    while (h != NULL)
1066    {
1067      i = p_IsPurePower(h, strat->tailRing);
1068      if (rField_is_Ring(currRing) && (!n_IsUnit(pGetCoeff(h), currRing->cf))) i=0;
1069      if (i==last) return TRUE;
1070      (*length)++;
1071      pIter(h);
1072    }
1073  }
1074  return FALSE;
1075}
1076
1077BOOLEAN hasPurePower (LObject *L,int last, int *length,kStrategy strat)
1078{
1079  if (L->bucket != NULL)
1080  {
1081    poly p = L->CanonicalizeP();
1082    BOOLEAN ret = hasPurePower(p, last, length, strat);
1083    pNext(p) = NULL;
1084    return ret;
1085  }
1086  else
1087  {
1088    return hasPurePower(L->p, last, length, strat);
1089  }
1090}
1091
1092/*2
1093* looks up the position of polynomial p in L
1094* in the case of looking for the pure powers
1095*/
1096int posInL10 (const LSet set,const int length, LObject* p,const kStrategy strat)
1097{
1098  int j,dp,dL;
1099
1100  if (length<0) return 0;
1101  if (hasPurePower(p,strat->lastAxis,&dp,strat))
1102  {
1103    int op= p->GetpFDeg() +p->ecart;
1104    for (j=length; j>=0; j--)
1105    {
1106      if (!hasPurePower(&(set[j]),strat->lastAxis,&dL,strat))
1107        return j+1;
1108      if (dp < dL)
1109        return j+1;
1110      if ((dp == dL)
1111          && (set[j].GetpFDeg()+set[j].ecart >= op))
1112        return j+1;
1113    }
1114  }
1115  j=length;
1116  loop
1117  {
1118    if (j<0) break;
1119    if (!hasPurePower(&(set[j]),strat->lastAxis,&dL,strat)) break;
1120    j--;
1121  }
1122  return strat->posInLOld(set,j,p,strat);
1123}
1124
1125
1126/*2
1127* computes the s-polynomials L[ ].p in L
1128*/
1129void updateL(kStrategy strat)
1130{
1131  LObject p;
1132  int dL;
1133  int j=strat->Ll;
1134  loop
1135  {
1136    if (j<0) break;
1137    if (hasPurePower(&(strat->L[j]),strat->lastAxis,&dL,strat))
1138    {
1139      p=strat->L[strat->Ll];
1140      strat->L[strat->Ll]=strat->L[j];
1141      strat->L[j]=p;
1142      break;
1143    }
1144    j--;
1145  }
1146  if (j<0)
1147  {
1148    j=strat->Ll;
1149    loop
1150    {
1151      if (j<0) break;
1152      if (pNext(strat->L[j].p) == strat->tail)
1153      {
1154        if (rField_is_Ring(currRing))
1155          pLmDelete(strat->L[j].p);    /*deletes the short spoly and computes*/
1156        else
1157          pLmFree(strat->L[j].p);    /*deletes the short spoly and computes*/
1158        strat->L[j].p = NULL;
1159        poly m1 = NULL, m2 = NULL;
1160        // check that spoly creation is ok
1161        while (strat->tailRing != currRing &&
1162               !kCheckSpolyCreation(&(strat->L[j]), strat, m1, m2))
1163        {
1164          assume(m1 == NULL && m2 == NULL);
1165          // if not, change to a ring where exponents are at least
1166          // large enough
1167          kStratChangeTailRing(strat);
1168        }
1169        /* create the real one */
1170        ksCreateSpoly(&(strat->L[j]), strat->kNoetherTail(), FALSE,
1171                      strat->tailRing, m1, m2, strat->R);
1172
1173        strat->L[j].SetLmCurrRing();
1174        if (!strat->honey)
1175          strat->initEcart(&strat->L[j]);
1176        else
1177          strat->L[j].SetLength(strat->length_pLength);
1178
1179        BOOLEAN pp = hasPurePower(&(strat->L[j]),strat->lastAxis,&dL,strat);
1180
1181        if (strat->use_buckets) strat->L[j].PrepareRed(TRUE);
1182
1183        if (pp)
1184        {
1185          p=strat->L[strat->Ll];
1186          strat->L[strat->Ll]=strat->L[j];
1187          strat->L[j]=p;
1188          break;
1189        }
1190      }
1191      j--;
1192    }
1193  }
1194}
1195
1196/*2
1197* computes the s-polynomials L[ ].p in L and
1198* cuts elements in L above noether
1199*/
1200void updateLHC(kStrategy strat)
1201{
1202
1203  int i = 0;
1204  kTest_TS(strat);
1205  while (i <= strat->Ll)
1206  {
1207    if (pNext(strat->L[i].p) == strat->tail)
1208    {
1209       /*- deletes the int spoly and computes -*/
1210      if (pLmCmp(strat->L[i].p,strat->kNoether) == -1)
1211      {
1212        if (rField_is_Ring(currRing))
1213          pLmDelete(strat->L[i].p);
1214        else
1215          pLmFree(strat->L[i].p);
1216        strat->L[i].p = NULL;
1217      }
1218      else
1219      {
1220        if (rField_is_Ring(currRing))
1221          pLmDelete(strat->L[i].p);
1222        else
1223          pLmFree(strat->L[i].p);
1224        strat->L[i].p = NULL;
1225        poly m1 = NULL, m2 = NULL;
1226        // check that spoly creation is ok
1227        while (strat->tailRing != currRing &&
1228               !kCheckSpolyCreation(&(strat->L[i]), strat, m1, m2))
1229        {
1230          assume(m1 == NULL && m2 == NULL);
1231          // if not, change to a ring where exponents are at least
1232          // large enough
1233          kStratChangeTailRing(strat);
1234        }
1235        /* create the real one */
1236        ksCreateSpoly(&(strat->L[i]), strat->kNoetherTail(), FALSE,
1237                      strat->tailRing, m1, m2, strat->R);
1238        if (! strat->L[i].IsNull())
1239        {
1240          strat->L[i].SetLmCurrRing();
1241          strat->L[i].SetpFDeg();
1242          strat->L[i].ecart
1243            = strat->L[i].pLDeg(strat->LDegLast) - strat->L[i].GetpFDeg();
1244          if (strat->use_buckets) strat->L[i].PrepareRed(TRUE);
1245        }
1246      }
1247    }
1248    else
1249      deleteHC(&(strat->L[i]), strat);
1250    if (strat->L[i].IsNull())
1251      deleteInL(strat->L,&strat->Ll,i,strat);
1252    else
1253    {
1254#ifdef KDEBUG
1255      kTest_L(&(strat->L[i]), strat->tailRing, TRUE, i, strat->T, strat->tl);
1256#endif
1257      i++;
1258    }
1259  }
1260  kTest_TS(strat);
1261}
1262
1263/*2
1264* cuts in T above strat->kNoether and tries to cancel a unit
1265* changes also S as S is a subset of T
1266*/
1267void updateT(kStrategy strat)
1268{
1269  int i = 0;
1270  LObject p;
1271
1272  while (i <= strat->tl)
1273  {
1274    p = strat->T[i];
1275    deleteHC(&p,strat, TRUE);
1276    /*- tries to cancel a unit: -*/
1277    cancelunit(&p);
1278    if (TEST_OPT_INTSTRATEGY) /* deleteHC and/or cancelunit may have changed p*/
1279      p.pCleardenom();
1280    if (p.p != strat->T[i].p)
1281    {
1282      strat->sevT[i] = pGetShortExpVector(p.p);
1283      p.SetpFDeg();
1284    }
1285    strat->T[i] = p;
1286    i++;
1287  }
1288}
1289
1290/*2
1291* arranges red, pos and T if strat->kHEdgeFound (first time)
1292*/
1293void firstUpdate(kStrategy strat)
1294{
1295  if (strat->update)
1296  {
1297    kTest_TS(strat);
1298    strat->update = (strat->tl == -1);
1299    if (TEST_OPT_WEIGHTM)
1300    {
1301      pRestoreDegProcs(currRing,strat->pOrigFDeg, strat->pOrigLDeg);
1302      if (strat->tailRing != currRing)
1303      {
1304        strat->tailRing->pFDeg = strat->pOrigFDeg_TailRing;
1305        strat->tailRing->pLDeg = strat->pOrigLDeg_TailRing;
1306      }
1307      int i;
1308      for (i=strat->Ll; i>=0; i--)
1309      {
1310        strat->L[i].SetpFDeg();
1311      }
1312      for (i=strat->tl; i>=0; i--)
1313      {
1314        strat->T[i].SetpFDeg();
1315      }
1316      if (ecartWeights)
1317      {
1318        omFreeSize((ADDRESS)ecartWeights,(rVar(currRing)+1)*sizeof(short));
1319        ecartWeights=NULL;
1320      }
1321    }
1322    if (TEST_OPT_FASTHC)
1323    {
1324      strat->posInL = strat->posInLOld;
1325      strat->lastAxis = 0;
1326    }
1327    if (TEST_OPT_FINDET)
1328      return;
1329
1330    if ( (!rField_is_Ring(currRing)) || (rHasGlobalOrdering(currRing)))
1331    {
1332      strat->red = redFirst;
1333      strat->use_buckets = kMoraUseBucket(strat);
1334    }
1335    updateT(strat);
1336
1337    if ( (!rField_is_Ring(currRing)) || (rHasGlobalOrdering(currRing)))
1338    {
1339      strat->posInT = posInT2;
1340      reorderT(strat);
1341    }
1342  }
1343  kTest_TS(strat);
1344}
1345
1346/*2
1347*-puts p to the standardbasis s at position at
1348*-reduces the tail of p if TEST_OPT_REDTAIL
1349*-tries to cancel a unit
1350*-HEckeTest
1351*  if TRUE
1352*  - decides about reduction-strategies
1353*  - computes noether
1354*  - stops computation if TEST_OPT_FINDET
1355*  - cuts the tails of the polynomials
1356*    in s,t and the elements in L above noether
1357*    and cancels units if possible
1358*  - reorders s,L
1359*/
1360void enterSMora (LObject &p,int atS,kStrategy strat, int atR = -1)
1361{
1362  enterSBba(p, atS, strat, atR);
1363  #ifdef KDEBUG
1364  if (TEST_OPT_DEBUG)
1365  {
1366    Print("new s%d:",atS);
1367    p_wrp(p.p,currRing,strat->tailRing);
1368    PrintLn();
1369  }
1370  #endif
1371  if ((!strat->kHEdgeFound) || (strat->kNoether!=NULL)) HEckeTest(p.p,strat);
1372  if (strat->kHEdgeFound)
1373  {
1374    if (newHEdge(strat))
1375    {
1376      firstUpdate(strat);
1377      if (TEST_OPT_FINDET)
1378        return;
1379
1380      /*- cuts elements in L above noether and reorders L -*/
1381      updateLHC(strat);
1382      /*- reorders L with respect to posInL -*/
1383      reorderL(strat);
1384    }
1385  }
1386  else if (strat->kNoether!=NULL)
1387    strat->kHEdgeFound = TRUE;
1388  else if (TEST_OPT_FASTHC)
1389  {
1390    if (strat->posInLOldFlag)
1391    {
1392      missingAxis(&strat->lastAxis,strat);
1393      if (strat->lastAxis)
1394      {
1395        strat->posInLOld = strat->posInL;
1396        strat->posInLOldFlag = FALSE;
1397        strat->posInL = posInL10;
1398        strat->posInLDependsOnLength = TRUE;
1399        updateL(strat);
1400        reorderL(strat);
1401      }
1402    }
1403    else if (strat->lastAxis)
1404      updateL(strat);
1405  }
1406}
1407
1408/*2
1409*-puts p to the standardbasis s at position at
1410*-HEckeTest
1411*  if TRUE
1412*  - computes noether
1413*/
1414void enterSMoraNF (LObject &p, int atS,kStrategy strat, int atR = -1)
1415{
1416  enterSBba(p, atS, strat, atR);
1417  if ((!strat->kHEdgeFound) || (strat->kNoether!=NULL)) HEckeTest(p.p,strat);
1418  if (strat->kHEdgeFound)
1419    newHEdge(strat);
1420  else if (strat->kNoether!=NULL)
1421    strat->kHEdgeFound = TRUE;
1422}
1423
1424void initBba(kStrategy strat)
1425{
1426 /* setting global variables ------------------- */
1427  strat->enterS = enterSBba;
1428    strat->red = redHoney;
1429  if (strat->honey)
1430    strat->red = redHoney;
1431  else if (currRing->pLexOrder && !strat->homog)
1432    strat->red = redLazy;
1433  else
1434  {
1435    strat->LazyPass *=4;
1436    strat->red = redHomog;
1437  }
1438  if (rField_is_Ring(currRing))
1439  {
1440    strat->red = redRing;
1441  }
1442  if (currRing->pLexOrder && strat->honey)
1443    strat->initEcart = initEcartNormal;
1444  else
1445    strat->initEcart = initEcartBBA;
1446  if (strat->honey)
1447    strat->initEcartPair = initEcartPairMora;
1448  else
1449    strat->initEcartPair = initEcartPairBba;
1450//  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1451//  {
1452//    //interred  machen   Aenderung
1453//    strat->pOrigFDeg=pFDeg;
1454//    strat->pOrigLDeg=pLDeg;
1455//    //h=ggetid("ecart");
1456//    //if ((h!=NULL) /*&& (IDTYP(h)==INTVEC_CMD)*/)
1457//    //{
1458//    //  ecartWeights=iv2array(IDINTVEC(h));
1459//    //}
1460//    //else
1461//    {
1462//      ecartWeights=(short *)omAlloc(((currRing->N)+1)*sizeof(short));
1463//      /*uses automatic computation of the ecartWeights to set them*/
1464//      kEcartWeights(F->m,IDELEMS(F)-1,ecartWeights);
1465//    }
1466//    pRestoreDegProcs(currRing,totaldegreeWecart, maxdegreeWecart);
1467//    if (TEST_OPT_PROT)
1468//    {
1469//      for(i=1; i<=(currRing->N); i++)
1470//        Print(" %d",ecartWeights[i]);
1471//      PrintLn();
1472//      mflush();
1473//    }
1474//  }
1475}
1476
1477void initSba(ideal F,kStrategy strat)
1478{
1479  int i;
1480  //idhdl h;
1481 /* setting global variables ------------------- */
1482  strat->enterS = enterSSba;
1483    strat->red2 = redHoney;
1484  if (strat->honey)
1485    strat->red2 = redHoney;
1486  else if (currRing->pLexOrder && !strat->homog)
1487    strat->red2 = redLazy;
1488  else
1489  {
1490    strat->LazyPass *=4;
1491    strat->red2 = redHomog;
1492  }
1493  if (rField_is_Ring(currRing))
1494  {
1495    if(rHasLocalOrMixedOrdering(currRing))
1496      {strat->red2 = redRiloc;}
1497    else
1498      {strat->red2 = redRing;}
1499  }
1500  if (currRing->pLexOrder && strat->honey)
1501    strat->initEcart = initEcartNormal;
1502  else
1503    strat->initEcart = initEcartBBA;
1504  if (strat->honey)
1505    strat->initEcartPair = initEcartPairMora;
1506  else
1507    strat->initEcartPair = initEcartPairBba;
1508  //strat->kIdeal = NULL;
1509  //if (strat->ak==0) strat->kIdeal->rtyp=IDEAL_CMD;
1510  //else              strat->kIdeal->rtyp=MODUL_CMD;
1511  //strat->kIdeal->data=(void *)strat->Shdl;
1512  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1513  {
1514    //interred  machen   Aenderung
1515    strat->pOrigFDeg  = currRing->pFDeg;
1516    strat->pOrigLDeg  = currRing->pLDeg;
1517    //h=ggetid("ecart");
1518    //if ((h!=NULL) /*&& (IDTYP(h)==INTVEC_CMD)*/)
1519    //{
1520    //  ecartWeights=iv2array(IDINTVEC(h));
1521    //}
1522    //else
1523    {
1524      ecartWeights=(short *)omAlloc(((currRing->N)+1)*sizeof(short));
1525      /*uses automatic computation of the ecartWeights to set them*/
1526      kEcartWeights(F->m,IDELEMS(F)-1,ecartWeights, currRing);
1527    }
1528    pRestoreDegProcs(currRing, totaldegreeWecart, maxdegreeWecart);
1529    if (TEST_OPT_PROT)
1530    {
1531      for(i=1; i<=(currRing->N); i++)
1532        Print(" %d",ecartWeights[i]);
1533      PrintLn();
1534      mflush();
1535    }
1536  }
1537  // for sig-safe reductions in signature-based
1538  // standard basis computations
1539  if(rField_is_Ring(currRing))
1540    strat->red = redSigRing;
1541  else
1542    strat->red        = redSig;
1543  //strat->sbaOrder  = 1;
1544  strat->currIdx      = 1;
1545}
1546
1547void initMora(ideal F,kStrategy strat)
1548{
1549  int i,j;
1550
1551  strat->NotUsedAxis = (BOOLEAN *)omAlloc(((currRing->N)+1)*sizeof(BOOLEAN));
1552  for (j=(currRing->N); j>0; j--) strat->NotUsedAxis[j] = TRUE;
1553  strat->enterS = enterSMora;
1554  strat->initEcartPair = initEcartPairMora; /*- ecart approximation -*/
1555  strat->posInLOld = strat->posInL;
1556  strat->posInLOldFlag = TRUE;
1557  strat->initEcart = initEcartNormal;
1558  strat->kHEdgeFound = (currRing->ppNoether) != NULL;
1559  if ( strat->kHEdgeFound )
1560     strat->kNoether = pCopy((currRing->ppNoether));
1561  else if (strat->kHEdgeFound || strat->homog)
1562    strat->red = redFirst;  /*take the first possible in T*/
1563  else
1564    strat->red = redEcart;/*take the first possible in under ecart-restriction*/
1565  if (strat->kHEdgeFound)
1566  {
1567    strat->HCord = currRing->pFDeg((currRing->ppNoether),currRing)+1;
1568    strat->posInT = posInT2;
1569  }
1570  else
1571  {
1572    strat->HCord = 32000;/*- very large -*/
1573  }
1574
1575  if (rField_is_Ring(currRing))
1576    strat->red = redRiloc;
1577
1578  /*reads the ecartWeights used for Graebes method from the
1579   *intvec ecart and set ecartWeights
1580   */
1581  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1582  {
1583    //interred  machen   Aenderung
1584    strat->pOrigFDeg=currRing->pFDeg;
1585    strat->pOrigLDeg=currRing->pLDeg;
1586    ecartWeights=(short *)omAlloc(((currRing->N)+1)*sizeof(short));
1587    /*uses automatic computation of the ecartWeights to set them*/
1588    kEcartWeights(F->m,IDELEMS(F)-1,ecartWeights,currRing);
1589
1590    pSetDegProcs(currRing,totaldegreeWecart, maxdegreeWecart);
1591    if (TEST_OPT_PROT)
1592    {
1593      for(i=1; i<=(currRing->N); i++)
1594        Print(" %d",ecartWeights[i]);
1595      PrintLn();
1596      mflush();
1597    }
1598  }
1599  kOptimizeLDeg(currRing->pLDeg, strat);
1600}
1601
1602void kDebugPrint(kStrategy strat);
1603
1604ideal mora (ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat)
1605{
1606#ifdef HAVE_RINGS
1607#ifdef ADIDEBUG
1608int loop_count;
1609loop_count = 1;
1610#endif
1611#endif
1612  int olddeg = 0;
1613  int reduc = 0;
1614  int red_result = 1;
1615  int hilbeledeg=1,hilbcount=0;
1616  BITSET save1;
1617  SI_SAVE_OPT1(save1);
1618  if (rHasMixedOrdering(currRing))
1619  {
1620    si_opt_1 &= ~Sy_bit(OPT_REDSB);
1621    si_opt_1 &= ~Sy_bit(OPT_REDTAIL);
1622  }
1623
1624  strat->update = TRUE;
1625  /*- setting global variables ------------------- -*/
1626  initBuchMoraCrit(strat);
1627  initHilbCrit(F,Q,&hilb,strat);
1628  initMora(F,strat);
1629  if(rField_is_Ring(currRing))
1630    initBuchMoraPosRing(strat);
1631  else
1632    initBuchMoraPos(strat);
1633  /*Shdl=*/initBuchMora(F,Q,strat);
1634  if (TEST_OPT_FASTHC) missingAxis(&strat->lastAxis,strat);
1635  /*updateS in initBuchMora has Hecketest
1636  * and could have put strat->kHEdgdeFound FALSE*/
1637  if ((currRing->ppNoether)!=NULL)
1638  {
1639    strat->kHEdgeFound = TRUE;
1640  }
1641  if (strat->kHEdgeFound && strat->update)
1642  {
1643    firstUpdate(strat);
1644    updateLHC(strat);
1645    reorderL(strat);
1646  }
1647  if (TEST_OPT_FASTHC && (strat->lastAxis) && strat->posInLOldFlag)
1648  {
1649    strat->posInLOld = strat->posInL;
1650    strat->posInLOldFlag = FALSE;
1651    strat->posInL = posInL10;
1652    updateL(strat);
1653    reorderL(strat);
1654  }
1655  kTest_TS(strat);
1656  strat->use_buckets = kMoraUseBucket(strat);
1657
1658#ifdef HAVE_TAIL_RING
1659  if (strat->homog && strat->red == redFirst)
1660    if(!idIs0(F) &&(!rField_is_Ring(currRing)))
1661      kStratInitChangeTailRing(strat);
1662#endif
1663
1664  if (BVERBOSE(23))
1665  {
1666    kDebugPrint(strat);
1667  }
1668//deleteInL(strat->L,&strat->Ll,1,strat);
1669//deleteInL(strat->L,&strat->Ll,0,strat);
1670
1671  /*- compute-------------------------------------------*/
1672  while (strat->Ll >= 0)
1673  {
1674    #ifdef ADIDEBUG
1675    printf("\n      ------------------------NEW LOOP\n");
1676    printf("\nShdl = \n");
1677    for(int iii = 0; iii<= strat->sl; iii++)
1678    {
1679        printf("S[%i]:",iii);
1680        p_Write(strat->S[iii], strat->tailRing);
1681    }
1682    printf("\n   list   L has %i\n", strat->Ll);
1683    int iii;
1684    #ifdef ADIDEBUG
1685    for(iii = 0; iii<= strat->Ll; iii++)
1686    {
1687        printf("L[%i]:",iii);
1688        pWrite(strat->L[iii].p);
1689        pWrite(strat->L[iii].p1);
1690        pWrite(strat->L[iii].p2);
1691    }
1692    #endif
1693    getchar();
1694    #endif
1695    #ifdef KDEBUG
1696    if (TEST_OPT_DEBUG) messageSets(strat);
1697    #endif
1698    if (TEST_OPT_DEGBOUND
1699    && (strat->L[strat->Ll].ecart+strat->L[strat->Ll].GetpFDeg()> Kstd1_deg))
1700    {
1701      /*
1702      * stops computation if
1703      * - 24 (degBound)
1704      *   && upper degree is bigger than Kstd1_deg
1705      */
1706      while ((strat->Ll >= 0)
1707        && (strat->L[strat->Ll].p1!=NULL) && (strat->L[strat->Ll].p2!=NULL)
1708        && (strat->L[strat->Ll].ecart+strat->L[strat->Ll].GetpFDeg()> Kstd1_deg)
1709      )
1710      {
1711        deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
1712        //if (TEST_OPT_PROT)
1713        //{
1714        //   PrintS("D"); mflush();
1715        //}
1716      }
1717      if (strat->Ll<0) break;
1718      else strat->noClearS=TRUE;
1719    }
1720    strat->P = strat->L[strat->Ll];/*- picks the last element from the lazyset L -*/
1721    if (strat->Ll==0) strat->interpt=TRUE;
1722    strat->Ll--;
1723    // create the real Spoly
1724    if (pNext(strat->P.p) == strat->tail)
1725    {
1726      /*- deletes the short spoly and computes -*/
1727      if (rField_is_Ring(currRing))
1728        pLmDelete(strat->P.p);
1729      else
1730        pLmFree(strat->P.p);
1731      strat->P.p = NULL;
1732      poly m1 = NULL, m2 = NULL;
1733      // check that spoly creation is ok
1734      while (strat->tailRing != currRing &&
1735             !kCheckSpolyCreation(&(strat->P), strat, m1, m2))
1736      {
1737        assume(m1 == NULL && m2 == NULL);
1738        // if not, change to a ring where exponents are large enough
1739        kStratChangeTailRing(strat);
1740      }
1741      /* create the real one */
1742      ksCreateSpoly(&(strat->P), strat->kNoetherTail(), strat->use_buckets,
1743                    strat->tailRing, m1, m2, strat->R);
1744      if (!strat->use_buckets)
1745        strat->P.SetLength(strat->length_pLength);
1746    }
1747    else if (strat->P.p1 == NULL)
1748    {
1749      // for input polys, prepare reduction (buckets !)
1750      strat->P.SetLength(strat->length_pLength);
1751      strat->P.PrepareRed(strat->use_buckets);
1752    }
1753
1754    // the s-poly
1755    if (!strat->P.IsNull())
1756    {
1757      // might be NULL from noether !!!
1758      if (TEST_OPT_PROT)
1759        message(strat->P.ecart+strat->P.GetpFDeg(),&olddeg,&reduc,strat, red_result);
1760      // reduce
1761      #ifdef ADIDEBUG
1762      printf("\nThis is P vor red:\n");p_Write(strat->P.p,strat->tailRing);p_Write(strat->P.p1,strat->tailRing);p_Write(strat->P.p2,strat->tailRing);
1763      printf("\nBefore Ll = %i\n", strat->Ll);
1764      #endif
1765      red_result = strat->red(&strat->P,strat);
1766      #ifdef ADIDEBUG
1767      printf("\nThis is P nach red:\n");p_Write(strat->P.p,strat->tailRing);p_Write(strat->P.p1,strat->tailRing);p_Write(strat->P.p2,strat->tailRing);
1768      printf("\nAfter Ll = %i\n", strat->Ll);
1769      #endif
1770    }
1771
1772    // the reduced s-poly
1773    if (! strat->P.IsNull())
1774    {
1775      strat->P.GetP();
1776      // statistics
1777      if (TEST_OPT_PROT) PrintS("s");
1778      // normalization
1779      if (TEST_OPT_INTSTRATEGY)
1780        strat->P.pCleardenom();
1781      else
1782        strat->P.pNorm();
1783      // tailreduction
1784      strat->P.p = redtail(&(strat->P),strat->sl,strat);
1785      if (strat->P.p==NULL)
1786      {
1787        WerrorS("expoent overflow - wrong ordering");
1788        return(idInit(1,1));
1789      }
1790      // set ecart -- might have changed because of tail reductions
1791      if ((!strat->noTailReduction) && (!strat->honey))
1792        strat->initEcart(&strat->P);
1793      // cancel unit
1794      cancelunit(&strat->P);
1795      // for char 0, clear denominators
1796      if ((strat->P.p->next==NULL) /* i.e. cancelunit did something*/
1797      && TEST_OPT_INTSTRATEGY)
1798        strat->P.pCleardenom();
1799
1800      enterT(strat->P,strat);
1801      // build new pairs
1802      if (rField_is_Ring(currRing))
1803        superenterpairs(strat->P.p,strat->sl,strat->P.ecart,0,strat, strat->tl);
1804      else
1805        enterpairs(strat->P.p,strat->sl,strat->P.ecart,0,strat, strat->tl);
1806      // put in S
1807
1808        #ifdef ADIDEBUG
1809        Print("\n    The new pair list L -- after superenterpairs in loop %d -- is:\n",loop_count);
1810        for(int iii=0;iii<=strat->Ll;iii++)
1811        {
1812          printf("\n    L[%d]:\n",iii);
1813          PrintS("         ");p_Write(strat->L[iii].p,strat->tailRing);
1814          PrintS("         ");p_Write(strat->L[iii].p1,strat->tailRing);
1815          PrintS("         ");p_Write(strat->L[iii].p2,strat->tailRing);
1816        }
1817        #endif
1818      strat->enterS(strat->P,
1819                    posInS(strat,strat->sl,strat->P.p, strat->P.ecart),
1820                    strat, strat->tl);
1821      #ifdef ADIDEBUG
1822      printf("\nThis pair has been added to S:\n");
1823      pWrite(strat->P.p);
1824      pWrite(strat->P.p1);
1825      pWrite(strat->P.p2);
1826      #endif
1827
1828      // apply hilbert criterion
1829      if (hilb!=NULL)
1830      {
1831        if (strat->homog==isHomog)
1832          khCheck(Q,w,hilb,hilbeledeg,hilbcount,strat);
1833        else
1834          khCheckLocInhom(Q,w,hilb,hilbcount,strat);
1835      }
1836
1837      // clear strat->P
1838      if (strat->P.lcm!=NULL)
1839      {
1840        if (rField_is_Ring(currRing))
1841          pLmDelete(strat->P.lcm);
1842        else
1843          pLmFree(strat->P.lcm);
1844        strat->P.lcm=NULL;
1845      }
1846
1847#ifdef KDEBUG
1848      // make sure kTest_TS does not complain about strat->P
1849      memset(&strat->P,0,sizeof(strat->P));
1850#endif
1851    }
1852    if (strat->kHEdgeFound)
1853    {
1854      if ((TEST_OPT_FINDET)
1855      || ((TEST_OPT_MULTBOUND) && (scMult0Int(strat->Shdl,NULL,strat->tailRing) < Kstd1_mu)))
1856      {
1857        // obachman: is this still used ???
1858        /*
1859        * stops computation if strat->kHEdgeFound and
1860        * - 27 (finiteDeterminacyTest)
1861        * or
1862        * - 23
1863        *   (multBound)
1864        *   && multiplicity of the ideal is smaller then a predefined number mu
1865        */
1866        while (strat->Ll >= 0) deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
1867      }
1868    }
1869    kTest_TS(strat);
1870  }
1871  /*- complete reduction of the standard basis------------------------ -*/
1872  if (TEST_OPT_REDSB) completeReduce(strat);
1873  else if (TEST_OPT_PROT) PrintLn();
1874  /*- release temp data------------------------------- -*/
1875  exitBuchMora(strat);
1876  /*- polynomials used for HECKE: HC, noether -*/
1877  if (TEST_OPT_FINDET)
1878  {
1879    if (strat->kHEdge!=NULL)
1880      Kstd1_mu=currRing->pFDeg(strat->kHEdge,currRing);
1881    else
1882      Kstd1_mu=-1;
1883  }
1884  if (strat->kHEdge!=NULL) pLmFree(&strat->kHEdge);
1885  strat->update = TRUE; //???
1886  strat->lastAxis = 0; //???
1887  if (strat->kNoether!=NULL) pLmDelete(&strat->kNoether);
1888  omFreeSize((ADDRESS)strat->NotUsedAxis,((currRing->N)+1)*sizeof(BOOLEAN));
1889  if ((TEST_OPT_PROT)||(TEST_OPT_DEBUG))  messageStat(hilbcount,strat);
1890//  if (TEST_OPT_WEIGHTM)
1891//  {
1892//    pRestoreDegProcs(currRing,strat->pOrigFDeg, strat->pOrigLDeg);
1893//    if (ecartWeights)
1894//    {
1895//      omFreeSize((ADDRESS)ecartWeights,((currRing->N)+1)*sizeof(short));
1896//      ecartWeights=NULL;
1897//    }
1898//  }
1899  if(nCoeff_is_Ring_Z(currRing->cf))
1900    finalReduceByMon(strat);
1901  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
1902  SI_RESTORE_OPT1(save1);
1903  idTest(strat->Shdl);
1904  return (strat->Shdl);
1905}
1906
1907poly kNF1 (ideal F,ideal Q,poly q, kStrategy strat, int lazyReduce)
1908{
1909  assume(q!=NULL);
1910  assume(!(idIs0(F)&&(Q==NULL)));
1911
1912// lazy_reduce flags: can be combined by |
1913//#define KSTD_NF_LAZY   1
1914  // do only a reduction of the leading term
1915//#define KSTD_NF_ECART  2
1916  // only local: recude even with bad ecart
1917  poly   p;
1918  int   i;
1919  int   j;
1920  int   o;
1921  LObject   h;
1922  BITSET save1;
1923  SI_SAVE_OPT1(save1);
1924
1925  //if ((idIs0(F))&&(Q==NULL))
1926  //  return pCopy(q); /*F=0*/
1927  //strat->ak = si_max(idRankFreeModule(F),pMaxComp(q));
1928  /*- creating temp data structures------------------- -*/
1929  strat->kHEdgeFound = (currRing->ppNoether) != NULL;
1930  strat->kNoether    = pCopy((currRing->ppNoether));
1931  si_opt_1|=Sy_bit(OPT_REDTAIL);
1932  si_opt_1&=~Sy_bit(OPT_INTSTRATEGY);
1933  if (TEST_OPT_STAIRCASEBOUND
1934  && (! TEST_V_DEG_STOP)
1935  && (0<Kstd1_deg)
1936  && ((!strat->kHEdgeFound)
1937    ||(TEST_OPT_DEGBOUND && (pWTotaldegree(strat->kNoether)<Kstd1_deg))))
1938  {
1939    pLmDelete(&strat->kNoether);
1940    strat->kNoether=pOne();
1941    pSetExp(strat->kNoether,1, Kstd1_deg+1);
1942    pSetm(strat->kNoether);
1943    strat->kHEdgeFound=TRUE;
1944  }
1945  initBuchMoraCrit(strat);
1946  if(rField_is_Ring(currRing))
1947    initBuchMoraPosRing(strat);
1948  else
1949    initBuchMoraPos(strat);
1950  initMora(F,strat);
1951  strat->enterS = enterSMoraNF;
1952  /*- set T -*/
1953  strat->tl = -1;
1954  strat->tmax = setmaxT;
1955  strat->T = initT();
1956  strat->R = initR();
1957  strat->sevT = initsevT();
1958  /*- set S -*/
1959  strat->sl = -1;
1960  /*- init local data struct.-------------------------- -*/
1961  /*Shdl=*/initS(F,Q,strat);
1962  if ((strat->ak!=0)
1963  && (strat->kHEdgeFound))
1964  {
1965    if (strat->ak!=1)
1966    {
1967      pSetComp(strat->kNoether,1);
1968      pSetmComp(strat->kNoether);
1969      poly p=pHead(strat->kNoether);
1970      pSetComp(p,strat->ak);
1971      pSetmComp(p);
1972      p=pAdd(strat->kNoether,p);
1973      strat->kNoether=pNext(p);
1974      p_LmDelete(p,currRing);
1975    }
1976  }
1977  if ((lazyReduce & KSTD_NF_LAZY)==0)
1978  {
1979    for (i=strat->sl; i>=0; i--)
1980      pNorm(strat->S[i]);
1981  }
1982  /*- puts the elements of S also to T -*/
1983  for (i=0; i<=strat->sl; i++)
1984  {
1985    h.p = strat->S[i];
1986    h.ecart = strat->ecartS[i];
1987    if (strat->sevS[i] == 0) strat->sevS[i] = pGetShortExpVector(h.p);
1988    else assume(strat->sevS[i] == pGetShortExpVector(h.p));
1989    h.length = pLength(h.p);
1990    h.sev = strat->sevS[i];
1991    h.SetpFDeg();
1992    enterT(h,strat);
1993  }
1994#ifdef KDEBUG
1995//  kDebugPrint(strat);
1996#endif
1997  /*- compute------------------------------------------- -*/
1998  p = pCopy(q);
1999  deleteHC(&p,&o,&j,strat);
2000  kTest(strat);
2001  if (TEST_OPT_PROT) { PrintS("r"); mflush(); }
2002  if (BVERBOSE(23)) kDebugPrint(strat);
2003  if(rField_is_Ring(currRing))
2004  {
2005    if (p!=NULL) p = redMoraNFRing(p,strat, lazyReduce & KSTD_NF_ECART);
2006  }
2007  else
2008  {
2009    if (p!=NULL) p = redMoraNF(p,strat, lazyReduce & KSTD_NF_ECART);
2010  }
2011  if ((p!=NULL)&&((lazyReduce & KSTD_NF_LAZY)==0))
2012  {
2013    if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
2014    p = redtail(p,strat->sl,strat);
2015  }
2016  /*- release temp data------------------------------- -*/
2017  cleanT(strat);
2018  assume(strat->L==NULL); /*strat->L unsed */
2019  assume(strat->B==NULL); /*strat->B unused */
2020  omFreeSize((ADDRESS)strat->T,strat->tmax*sizeof(TObject));
2021  omFreeSize((ADDRESS)strat->ecartS,IDELEMS(strat->Shdl)*sizeof(int));
2022  omFreeSize((ADDRESS)strat->sevS,IDELEMS(strat->Shdl)*sizeof(unsigned long));
2023  omFreeSize((ADDRESS)strat->NotUsedAxis,((currRing->N)+1)*sizeof(BOOLEAN));
2024  omFree(strat->sevT);
2025  omFree(strat->S_2_R);
2026  omFree(strat->R);
2027
2028  if ((Q!=NULL)&&(strat->fromQ!=NULL))
2029  {
2030    i=((IDELEMS(Q)+IDELEMS(F)+15)/16)*16;
2031    omFreeSize((ADDRESS)strat->fromQ,i*sizeof(int));
2032    strat->fromQ=NULL;
2033  }
2034  if (strat->kHEdge!=NULL) pLmFree(&strat->kHEdge);
2035  if (strat->kNoether!=NULL) pLmDelete(&strat->kNoether);
2036//  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
2037//  {
2038//    pRestoreDegProcs(currRing,strat->pOrigFDeg, strat->pOrigLDeg);
2039//    if (ecartWeights)
2040//    {
2041//      omFreeSize((ADDRESS *)&ecartWeights,((currRing->N)+1)*sizeof(short));
2042//      ecartWeights=NULL;
2043//    }
2044//  }
2045  idDelete(&strat->Shdl);
2046  SI_RESTORE_OPT1(save1);
2047  if (TEST_OPT_PROT) PrintLn();
2048  return p;
2049}
2050
2051ideal kNF1 (ideal F,ideal Q,ideal q, kStrategy strat, int lazyReduce)
2052{
2053  assume(!idIs0(q));
2054  assume(!(idIs0(F)&&(Q==NULL)));
2055
2056// lazy_reduce flags: can be combined by |
2057//#define KSTD_NF_LAZY   1
2058  // do only a reduction of the leading term
2059//#define KSTD_NF_ECART  2
2060  // only local: recude even with bad ecart
2061  poly   p;
2062  int   i;
2063  int   j;
2064  int   o;
2065  LObject   h;
2066  ideal res;
2067  BITSET save1;
2068  SI_SAVE_OPT1(save1);
2069
2070  //if (idIs0(q)) return idInit(IDELEMS(q),si_max(q->rank,F->rank));
2071  //if ((idIs0(F))&&(Q==NULL))
2072  //  return idCopy(q); /*F=0*/
2073  //strat->ak = si_max(idRankFreeModule(F),idRankFreeModule(q));
2074  /*- creating temp data structures------------------- -*/
2075  strat->kHEdgeFound = (currRing->ppNoether) != NULL;
2076  strat->kNoether=pCopy((currRing->ppNoether));
2077  si_opt_1|=Sy_bit(OPT_REDTAIL);
2078  if (TEST_OPT_STAIRCASEBOUND
2079  && (0<Kstd1_deg)
2080  && ((!strat->kHEdgeFound)
2081    ||(TEST_OPT_DEGBOUND && (pWTotaldegree(strat->kNoether)<Kstd1_deg))))
2082  {
2083    pLmDelete(&strat->kNoether);
2084    strat->kNoether=pOne();
2085    pSetExp(strat->kNoether,1, Kstd1_deg+1);
2086    pSetm(strat->kNoether);
2087    strat->kHEdgeFound=TRUE;
2088  }
2089  initBuchMoraCrit(strat);
2090  if(rField_is_Ring(currRing))
2091    initBuchMoraPosRing(strat);
2092  else
2093    initBuchMoraPos(strat);
2094  initMora(F,strat);
2095  strat->enterS = enterSMoraNF;
2096  /*- set T -*/
2097  strat->tl = -1;
2098  strat->tmax = setmaxT;
2099  strat->T = initT();
2100  strat->R = initR();
2101  strat->sevT = initsevT();
2102  /*- set S -*/
2103  strat->sl = -1;
2104  /*- init local data struct.-------------------------- -*/
2105  /*Shdl=*/initS(F,Q,strat);
2106  if ((strat->ak!=0)
2107  && (strat->kHEdgeFound))
2108  {
2109    if (strat->ak!=1)
2110    {
2111      pSetComp(strat->kNoether,1);
2112      pSetmComp(strat->kNoether);
2113      poly p=pHead(strat->kNoether);
2114      pSetComp(p,strat->ak);
2115      pSetmComp(p);
2116      p=pAdd(strat->kNoether,p);
2117      strat->kNoether=pNext(p);
2118      p_LmDelete(p,currRing);
2119    }
2120  }
2121  if (TEST_OPT_INTSTRATEGY && ((lazyReduce & KSTD_NF_LAZY)==0))
2122  {
2123    for (i=strat->sl; i>=0; i--)
2124      pNorm(strat->S[i]);
2125  }
2126  /*- compute------------------------------------------- -*/
2127  res=idInit(IDELEMS(q),strat->ak);
2128  for (i=0; i<IDELEMS(q); i++)
2129  {
2130    if (q->m[i]!=NULL)
2131    {
2132      p = pCopy(q->m[i]);
2133      deleteHC(&p,&o,&j,strat);
2134      if (p!=NULL)
2135      {
2136        /*- puts the elements of S also to T -*/
2137        for (j=0; j<=strat->sl; j++)
2138        {
2139          h.p = strat->S[j];
2140          h.ecart = strat->ecartS[j];
2141          h.pLength = h.length = pLength(h.p);
2142          if (strat->sevS[j] == 0) strat->sevS[j] = pGetShortExpVector(h.p);
2143          else assume(strat->sevS[j] == pGetShortExpVector(h.p));
2144          h.sev = strat->sevS[j];
2145          h.SetpFDeg();
2146          if(rField_is_Ring(currRing) && rHasLocalOrMixedOrdering(currRing))
2147            enterT_strong(h,strat);
2148          else
2149            enterT(h,strat);
2150        }
2151        if (TEST_OPT_PROT) { PrintS("r"); mflush(); }
2152        if(rField_is_Ring(currRing))
2153        {
2154          p = redMoraNFRing(p,strat, lazyReduce & KSTD_NF_ECART);
2155        }
2156        else
2157          p = redMoraNF(p,strat, lazyReduce & KSTD_NF_ECART);
2158        if ((p!=NULL)&&((lazyReduce & KSTD_NF_LAZY)==0))
2159        {
2160          if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
2161          p = redtail(p,strat->sl,strat);
2162        }
2163        cleanT(strat);
2164      }
2165      res->m[i]=p;
2166    }
2167    //else
2168    //  res->m[i]=NULL;
2169  }
2170  /*- release temp data------------------------------- -*/
2171  assume(strat->L==NULL); /*strat->L unsed */
2172  assume(strat->B==NULL); /*strat->B unused */
2173  omFreeSize((ADDRESS)strat->T,strat->tmax*sizeof(TObject));
2174  omFreeSize((ADDRESS)strat->ecartS,IDELEMS(strat->Shdl)*sizeof(int));
2175  omFreeSize((ADDRESS)strat->sevS,IDELEMS(strat->Shdl)*sizeof(unsigned long));
2176  omFreeSize((ADDRESS)strat->NotUsedAxis,((currRing->N)+1)*sizeof(BOOLEAN));
2177  omFree(strat->sevT);
2178  omFree(strat->S_2_R);
2179  omFree(strat->R);
2180  if ((Q!=NULL)&&(strat->fromQ!=NULL))
2181  {
2182    i=((IDELEMS(F)+IDELEMS(Q)+(setmaxTinc-1))/setmaxTinc)*setmaxTinc;
2183    omFreeSize((ADDRESS)strat->fromQ,i*sizeof(int));
2184    strat->fromQ=NULL;
2185  }
2186  if (strat->kHEdge!=NULL) pLmFree(&strat->kHEdge);
2187  if (strat->kNoether!=NULL) pLmDelete(&strat->kNoether);
2188//  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
2189//  {
2190//    pFDeg=strat->pOrigFDeg;
2191//    pLDeg=strat->pOrigLDeg;
2192//    if (ecartWeights)
2193//    {
2194//      omFreeSize((ADDRESS *)&ecartWeights,((currRing->N)+1)*sizeof(short));
2195//      ecartWeights=NULL;
2196//    }
2197//  }
2198  idDelete(&strat->Shdl);
2199  SI_RESTORE_OPT1(save1);
2200  if (TEST_OPT_PROT) PrintLn();
2201  return res;
2202}
2203
2204intvec * kModW, * kHomW;
2205
2206long kModDeg(poly p, ring r)
2207{
2208  long o=p_WDegree(p, r);
2209  long i=__p_GetComp(p, r);
2210  if (i==0) return o;
2211  //assume((i>0) && (i<=kModW->length()));
2212  if (i<=kModW->length())
2213    return o+(*kModW)[i-1];
2214  return o;
2215}
2216long kHomModDeg(poly p, ring r)
2217{
2218  int i;
2219  long j=0;
2220
2221  for (i=r->N;i>0;i--)
2222    j+=p_GetExp(p,i,r)*(*kHomW)[i-1];
2223  if (kModW == NULL) return j;
2224  i = __p_GetComp(p,r);
2225  if (i==0) return j;
2226  return j+(*kModW)[i-1];
2227}
2228
2229ideal kStd(ideal F, ideal Q, tHomog h,intvec ** w, intvec *hilb,int syzComp,
2230          int newIdeal, intvec *vw, s_poly_proc_t sp)
2231{
2232  if(idIs0(F))
2233    return idInit(1,F->rank);
2234
2235  ideal r;
2236  BOOLEAN b=currRing->pLexOrder,toReset=FALSE;
2237  BOOLEAN delete_w=(w==NULL);
2238  kStrategy strat=new skStrategy;
2239
2240  strat->s_poly=sp;
2241  if(!TEST_OPT_RETURN_SB)
2242    strat->syzComp = syzComp;
2243  if (TEST_OPT_SB_1
2244    &&(!rField_is_Ring(currRing))
2245    )
2246    strat->newIdeal = newIdeal;
2247  if (rField_has_simple_inverse(currRing))
2248    strat->LazyPass=20;
2249  else
2250    strat->LazyPass=2;
2251  strat->LazyDegree = 1;
2252  strat->ak = id_RankFreeModule(F,currRing);
2253  strat->kModW=kModW=NULL;
2254  strat->kHomW=kHomW=NULL;
2255  if (vw != NULL)
2256  {
2257    currRing->pLexOrder=FALSE;
2258    strat->kHomW=kHomW=vw;
2259    strat->pOrigFDeg = currRing->pFDeg;
2260    strat->pOrigLDeg = currRing->pLDeg;
2261    pSetDegProcs(currRing,kHomModDeg);
2262    toReset = TRUE;
2263  }
2264  if (h==testHomog)
2265  {
2266    if (strat->ak == 0)
2267    {
2268      h = (tHomog)idHomIdeal(F,Q);
2269      w=NULL;
2270    }
2271    else if (!TEST_OPT_DEGBOUND)
2272    {
2273      if (w!=NULL)
2274        h = (tHomog)idHomModule(F,Q,w);
2275      else
2276        h = (tHomog)idHomIdeal(F,Q);
2277    }
2278  }
2279  currRing->pLexOrder=b;
2280  if (h==isHomog)
2281  {
2282    if (strat->ak > 0 && (w!=NULL) && (*w!=NULL))
2283    {
2284      strat->kModW = kModW = *w;
2285      if (vw == NULL)
2286      {
2287        strat->pOrigFDeg = currRing->pFDeg;
2288        strat->pOrigLDeg = currRing->pLDeg;
2289        pSetDegProcs(currRing,kModDeg);
2290        toReset = TRUE;
2291      }
2292    }
2293    currRing->pLexOrder = TRUE;
2294    if (hilb==NULL) strat->LazyPass*=2;
2295  }
2296  strat->homog=h;
2297#ifdef KDEBUG
2298  idTest(F);
2299  if (Q!=NULL) idTest(Q);
2300#endif
2301#ifdef HAVE_PLURAL
2302  if (rIsPluralRing(currRing))
2303  {
2304    const BOOLEAN bIsSCA  = rIsSCA(currRing) && strat->z2homog; // for Z_2 prod-crit
2305    strat->no_prod_crit   = ! bIsSCA;
2306    if (w!=NULL)
2307      r = nc_GB(F, Q, *w, hilb, strat, currRing);
2308    else
2309      r = nc_GB(F, Q, NULL, hilb, strat, currRing);
2310  }
2311  else
2312#endif
2313  {
2314    #if PRE_INTEGER_CHECK
2315    //the preinteger check strategy is not for modules
2316    if(rField_is_Ring(currRing) && nCoeff_is_Ring_Z(currRing->cf) && strat->ak <= 0)
2317    {
2318      ideal FCopy = idCopy(F);
2319      poly pFmon = preIntegerCheck(FCopy, Q);
2320      if(pFmon != NULL)
2321      {
2322        idInsertPoly(FCopy, pFmon);
2323        #ifdef ADIDEBUG
2324        printf("\nPreintegerCheck found this constant:\n");pWrite(pFmon);
2325        #endif
2326
2327        strat->kModW=kModW=NULL;
2328        if (h==testHomog)
2329        {
2330            if (strat->ak == 0)
2331            {
2332              h = (tHomog)idHomIdeal(FCopy,Q);
2333              w=NULL;
2334            }
2335            else if (!TEST_OPT_DEGBOUND)
2336            {
2337                h = (tHomog)idHomModule(FCopy,Q,w);
2338            }
2339        }
2340        currRing->pLexOrder=b;
2341        if (h==isHomog)
2342        {
2343          if (strat->ak > 0 && (w!=NULL) && (*w!=NULL))
2344          {
2345            strat->kModW = kModW = *w;
2346            if (vw == NULL)
2347            {
2348              strat->pOrigFDeg = currRing->pFDeg;
2349              strat->pOrigLDeg = currRing->pLDeg;
2350              pSetDegProcs(currRing,kModDeg);
2351              toReset = TRUE;
2352            }
2353          }
2354          currRing->pLexOrder = TRUE;
2355          if (hilb==NULL) strat->LazyPass*=2;
2356        }
2357        strat->homog=h;
2358      }
2359      #ifdef ADIDEBUG
2360      else
2361      {
2362        printf("\npreIntegerCheck didn't found any new information\n");
2363      }
2364      #endif
2365      omTestMemory(1);
2366      if(w == NULL)
2367      {
2368        if(rHasLocalOrMixedOrdering(currRing))
2369            r=mora(FCopy,Q,NULL,hilb,strat);
2370        else
2371            r=bba(FCopy,Q,NULL,hilb,strat);
2372      }
2373      else
2374      {
2375        if(rHasLocalOrMixedOrdering(currRing))
2376            r=mora(FCopy,Q,*w,hilb,strat);
2377        else
2378            r=bba(FCopy,Q,*w,hilb,strat);
2379      }
2380      idDelete(&FCopy);
2381    }
2382    else
2383    #endif
2384    {
2385      if(w==NULL)
2386      {
2387        if(rHasLocalOrMixedOrdering(currRing))
2388          r=mora(F,Q,NULL,hilb,strat);
2389        else
2390          r=bba(F,Q,NULL,hilb,strat);
2391      }
2392      else
2393      {
2394        if(rHasLocalOrMixedOrdering(currRing))
2395          r=mora(F,Q,*w,hilb,strat);
2396        else
2397          r=bba(F,Q,*w,hilb,strat);
2398      }
2399    }
2400  }
2401#ifdef KDEBUG
2402  idTest(r);
2403#endif
2404  if (toReset)
2405  {
2406    kModW = NULL;
2407    pRestoreDegProcs(currRing,strat->pOrigFDeg, strat->pOrigLDeg);
2408  }
2409  currRing->pLexOrder = b;
2410//Print("%d reductions canceled \n",strat->cel);
2411  HCord=strat->HCord;
2412  delete(strat);
2413  if ((delete_w)&&(w!=NULL)&&(*w!=NULL)) delete *w;
2414  return r;
2415}
2416
2417ideal kSba(ideal F, ideal Q, tHomog h,intvec ** w, int sbaOrder, int arri, intvec *hilb,int syzComp,
2418          int newIdeal, intvec *vw)
2419{
2420  if(idIs0(F))
2421    return idInit(1,F->rank);
2422  if(!rField_is_Ring(currRing))
2423  {
2424    ideal r;
2425    BOOLEAN b=currRing->pLexOrder,toReset=FALSE;
2426    BOOLEAN delete_w=(w==NULL);
2427    kStrategy strat=new skStrategy;
2428    strat->sbaOrder = sbaOrder;
2429    if (arri!=0)
2430    {
2431      strat->rewCrit1 = arriRewDummy;
2432      strat->rewCrit2 = arriRewCriterion;
2433      strat->rewCrit3 = arriRewCriterionPre;
2434    }
2435    else
2436    {
2437      strat->rewCrit1 = faugereRewCriterion;
2438      strat->rewCrit2 = faugereRewCriterion;
2439      strat->rewCrit3 = faugereRewCriterion;
2440    }
2441
2442    if(!TEST_OPT_RETURN_SB)
2443      strat->syzComp = syzComp;
2444    if (TEST_OPT_SB_1)
2445      //if(!rField_is_Ring(currRing)) // always true here
2446        strat->newIdeal = newIdeal;
2447    if (rField_has_simple_inverse(currRing))
2448      strat->LazyPass=20;
2449    else
2450      strat->LazyPass=2;
2451    strat->LazyDegree = 1;
2452    strat->enterOnePair=enterOnePairNormal;
2453    strat->chainCrit=chainCritNormal;
2454    if (TEST_OPT_SB_1) strat->chainCrit=chainCritOpt_1;
2455    strat->ak = id_RankFreeModule(F,currRing);
2456    strat->kModW=kModW=NULL;
2457    strat->kHomW=kHomW=NULL;
2458    if (vw != NULL)
2459    {
2460      currRing->pLexOrder=FALSE;
2461      strat->kHomW=kHomW=vw;
2462      strat->pOrigFDeg = currRing->pFDeg;
2463      strat->pOrigLDeg = currRing->pLDeg;
2464      pSetDegProcs(currRing,kHomModDeg);
2465      toReset = TRUE;
2466    }
2467    if (h==testHomog)
2468    {
2469      if (strat->ak == 0)
2470      {
2471        h = (tHomog)idHomIdeal(F,Q);
2472        w=NULL;
2473      }
2474      else if (!TEST_OPT_DEGBOUND)
2475      {
2476        h = (tHomog)idHomModule(F,Q,w);
2477      }
2478    }
2479    currRing->pLexOrder=b;
2480    if (h==isHomog)
2481    {
2482      if (strat->ak > 0 && (w!=NULL) && (*w!=NULL))
2483      {
2484        strat->kModW = kModW = *w;
2485        if (vw == NULL)
2486        {
2487          strat->pOrigFDeg = currRing->pFDeg;
2488          strat->pOrigLDeg = currRing->pLDeg;
2489          pSetDegProcs(currRing,kModDeg);
2490          toReset = TRUE;
2491        }
2492      }
2493      currRing->pLexOrder = TRUE;
2494      if (hilb==NULL) strat->LazyPass*=2;
2495    }
2496    strat->homog=h;
2497  #ifdef KDEBUG
2498    idTest(F);
2499    if(Q != NULL)
2500      idTest(Q);
2501  #endif
2502  #ifdef HAVE_PLURAL
2503    if (rIsPluralRing(currRing))
2504    {
2505      const BOOLEAN bIsSCA  = rIsSCA(currRing) && strat->z2homog; // for Z_2 prod-crit
2506      strat->no_prod_crit   = ! bIsSCA;
2507      if (w!=NULL)
2508        r = nc_GB(F, Q, *w, hilb, strat, currRing);
2509      else
2510        r = nc_GB(F, Q, NULL, hilb, strat, currRing);
2511    }
2512    else
2513  #endif
2514    {
2515      if (rHasLocalOrMixedOrdering(currRing))
2516      {
2517        if (w!=NULL)
2518          r=mora(F,Q,*w,hilb,strat);
2519        else
2520          r=mora(F,Q,NULL,hilb,strat);
2521      }
2522      else
2523      {
2524        strat->sigdrop = FALSE;
2525        if (w!=NULL)
2526          r=sba(F,Q,*w,hilb,strat);
2527        else
2528          r=sba(F,Q,NULL,hilb,strat);
2529      }
2530    }
2531  #ifdef KDEBUG
2532    idTest(r);
2533  #endif
2534    if (toReset)
2535    {
2536      kModW = NULL;
2537      pRestoreDegProcs(currRing,strat->pOrigFDeg, strat->pOrigLDeg);
2538    }
2539    currRing->pLexOrder = b;
2540  //Print("%d reductions canceled \n",strat->cel);
2541    HCord=strat->HCord;
2542    //delete(strat);
2543    if ((delete_w)&&(w!=NULL)&&(*w!=NULL)) delete *w;
2544    return r;
2545  }
2546  else
2547  {
2548    //--------------------------RING CASE-------------------------
2549    assume(sbaOrder == 1);
2550    assume(arri == 0);
2551    ideal r;
2552    r = idCopy(F);
2553    int sbaEnterS = -1;
2554    bool sigdrop = TRUE;
2555    #ifdef ADIDEBUG
2556    printf("\nEnter the nice kSba loop\n");
2557    #endif
2558    //This is how we set the SBA algorithm;
2559    int totalsbaruns = 1,blockedreductions = 20,blockred = 0,loops = 0;
2560    while(sigdrop && (loops < totalsbaruns || totalsbaruns == -1)
2561                  && (blockred <= blockedreductions))
2562    {
2563      loops++;
2564      if(loops == 1)
2565        sigdrop = FALSE;
2566      BOOLEAN b=currRing->pLexOrder,toReset=FALSE;
2567      BOOLEAN delete_w=(w==NULL);
2568      kStrategy strat=new skStrategy;
2569      strat->sbaEnterS = sbaEnterS;
2570      strat->sigdrop = sigdrop;
2571      #if 0
2572      strat->blockred = blockred;
2573      #else
2574      strat->blockred = 0;
2575      #endif
2576      strat->blockredmax = blockedreductions;
2577      //printf("\nsbaEnterS beginning = %i\n",strat->sbaEnterS);
2578      //printf("\nsigdrop beginning = %i\n",strat->sigdrop);
2579      strat->sbaOrder = sbaOrder;
2580      if (arri!=0)
2581      {
2582        strat->rewCrit1 = arriRewDummy;
2583        strat->rewCrit2 = arriRewCriterion;
2584        strat->rewCrit3 = arriRewCriterionPre;
2585      }
2586      else
2587      {
2588        strat->rewCrit1 = faugereRewCriterion;
2589        strat->rewCrit2 = faugereRewCriterion;
2590        strat->rewCrit3 = faugereRewCriterion;
2591      }
2592
2593      if(!TEST_OPT_RETURN_SB)
2594        strat->syzComp = syzComp;
2595      if (TEST_OPT_SB_1)
2596        if(!rField_is_Ring(currRing))
2597          strat->newIdeal = newIdeal;
2598      if (rField_has_simple_inverse(currRing))
2599        strat->LazyPass=20;
2600      else
2601        strat->LazyPass=2;
2602      strat->LazyDegree = 1;
2603      strat->enterOnePair=enterOnePairNormal;
2604      strat->chainCrit=chainCritNormal;
2605      if (TEST_OPT_SB_1) strat->chainCrit=chainCritOpt_1;
2606      strat->ak = id_RankFreeModule(F,currRing);
2607      strat->kModW=kModW=NULL;
2608      strat->kHomW=kHomW=NULL;
2609      if (vw != NULL)
2610      {
2611        currRing->pLexOrder=FALSE;
2612        strat->kHomW=kHomW=vw;
2613        strat->pOrigFDeg = currRing->pFDeg;
2614        strat->pOrigLDeg = currRing->pLDeg;
2615        pSetDegProcs(currRing,kHomModDeg);
2616        toReset = TRUE;
2617      }
2618      if (h==testHomog)
2619      {
2620        if (strat->ak == 0)
2621        {
2622          h = (tHomog)idHomIdeal(F,Q);
2623          w=NULL;
2624        }
2625        else if (!TEST_OPT_DEGBOUND)
2626        {
2627          h = (tHomog)idHomModule(F,Q,w);
2628        }
2629      }
2630      currRing->pLexOrder=b;
2631      if (h==isHomog)
2632      {
2633        if (strat->ak > 0 && (w!=NULL) && (*w!=NULL))
2634        {
2635          strat->kModW = kModW = *w;
2636          if (vw == NULL)
2637          {
2638            strat->pOrigFDeg = currRing->pFDeg;
2639            strat->pOrigLDeg = currRing->pLDeg;
2640            pSetDegProcs(currRing,kModDeg);
2641            toReset = TRUE;
2642          }
2643        }
2644        currRing->pLexOrder = TRUE;
2645        if (hilb==NULL) strat->LazyPass*=2;
2646      }
2647      strat->homog=h;
2648    #ifdef KDEBUG
2649      idTest(F);
2650      if(Q != NULL)
2651        idTest(Q);
2652    #endif
2653    #ifdef HAVE_PLURAL
2654      if (rIsPluralRing(currRing))
2655      {
2656        const BOOLEAN bIsSCA  = rIsSCA(currRing) && strat->z2homog; // for Z_2 prod-crit
2657        strat->no_prod_crit   = ! bIsSCA;
2658        if (w!=NULL)
2659          r = nc_GB(F, Q, *w, hilb, strat, currRing);
2660        else
2661          r = nc_GB(F, Q, NULL, hilb, strat, currRing);
2662      }
2663      else
2664    #endif
2665      {
2666        if (rHasLocalOrMixedOrdering(currRing))
2667        {
2668          if (w!=NULL)
2669            r=mora(F,Q,*w,hilb,strat);
2670          else
2671            r=mora(F,Q,NULL,hilb,strat);
2672        }
2673        else
2674        {
2675          if (w!=NULL)
2676            r=sba(r,Q,*w,hilb,strat);
2677          else
2678          {
2679            r=sba(r,Q,NULL,hilb,strat);
2680          }
2681          #ifdef ADIDEBUG
2682          printf("\nSBA Run %i: %i elements (syzCrit = %i,rewCrit = %i)\n",loops,IDELEMS(r),strat->nrsyzcrit,strat->nrrewcrit);
2683          idPrint(r);
2684          //getchar();
2685          #endif
2686        }
2687      }
2688    #ifdef KDEBUG
2689      idTest(r);
2690    #endif
2691      if (toReset)
2692      {
2693        kModW = NULL;
2694        pRestoreDegProcs(currRing,strat->pOrigFDeg, strat->pOrigLDeg);
2695      }
2696      currRing->pLexOrder = b;
2697    //Print("%d reductions canceled \n",strat->cel);
2698      HCord=strat->HCord;
2699      sigdrop = strat->sigdrop;
2700      sbaEnterS = strat->sbaEnterS;
2701      blockred = strat->blockred;
2702      #ifdef ADIDEBUG
2703      printf("\nsbaEnterS = %i\n",sbaEnterS);
2704      #endif
2705      delete(strat);
2706      if ((delete_w)&&(w!=NULL)&&(*w!=NULL)) delete *w;
2707    }
2708    // Go to std
2709    if(sigdrop || blockred > blockedreductions)
2710    {
2711      #ifdef ADIDEBUG
2712      printf("\nWent to std\n");
2713      idPrint(r);
2714      getchar();
2715      #endif
2716      r = kStd(r, Q, h, w, hilb, syzComp, newIdeal, vw);
2717    }
2718    return r;
2719  }
2720}
2721
2722#ifdef HAVE_SHIFTBBA
2723ideal kStdShift(ideal F, ideal Q, tHomog h,intvec ** w, intvec *hilb,int syzComp,
2724                int newIdeal, intvec *vw, int uptodeg, int lV)
2725{
2726  ideal r;
2727  BOOLEAN b=currRing->pLexOrder,toReset=FALSE;
2728  BOOLEAN delete_w=(w==NULL);
2729  kStrategy strat=new skStrategy;
2730
2731  if(!TEST_OPT_RETURN_SB)
2732    strat->syzComp = syzComp;
2733  if (TEST_OPT_SB_1)
2734    if(!rField_is_Ring(currRing))
2735      strat->newIdeal = newIdeal;
2736  if (rField_has_simple_inverse(currRing))
2737    strat->LazyPass=20;
2738  else
2739    strat->LazyPass=2;
2740  strat->LazyDegree = 1;
2741  strat->ak = id_RankFreeModule(F,currRing);
2742  strat->kModW=kModW=NULL;
2743  strat->kHomW=kHomW=NULL;
2744  if (vw != NULL)
2745  {
2746    currRing->pLexOrder=FALSE;
2747    strat->kHomW=kHomW=vw;
2748    strat->pOrigFDeg = currRing->pFDeg;
2749    strat->pOrigLDeg = currRing->pLDeg;
2750    pSetDegProcs(currRing,kHomModDeg);
2751    toReset = TRUE;
2752  }
2753  if (h==testHomog)
2754  {
2755    if (strat->ak == 0)
2756    {
2757      h = (tHomog)idHomIdeal(F,Q);
2758      w=NULL;
2759    }
2760    else if (!TEST_OPT_DEGBOUND)
2761    {
2762      h = (tHomog)idHomModule(F,Q,w);
2763    }
2764  }
2765  currRing->pLexOrder=b;
2766  if (h==isHomog)
2767  {
2768    if (strat->ak > 0 && (w!=NULL) && (*w!=NULL))
2769    {
2770      strat->kModW = kModW = *w;
2771      if (vw == NULL)
2772      {
2773        strat->pOrigFDeg = currRing->pFDeg;
2774        strat->pOrigLDeg = currRing->pLDeg;
2775        pSetDegProcs(currRing,kModDeg);
2776        toReset = TRUE;
2777      }
2778    }
2779    currRing->pLexOrder = TRUE;
2780    if (hilb==NULL) strat->LazyPass*=2;
2781  }
2782  strat->homog=h;
2783#ifdef KDEBUG
2784  idTest(F);
2785#endif
2786  if (rHasLocalOrMixedOrdering(currRing))
2787  {
2788    /* error: no local ord yet with shifts */
2789    PrintS("No local ordering possible for shifts");
2790    return(NULL);
2791  }
2792  else
2793  {
2794    /* global ordering */
2795    if (w!=NULL)
2796      r=bbaShift(F,Q,*w,hilb,strat,uptodeg,lV);
2797    else
2798      r=bbaShift(F,Q,NULL,hilb,strat,uptodeg,lV);
2799  }
2800#ifdef KDEBUG
2801  idTest(r);
2802#endif
2803  if (toReset)
2804  {
2805    kModW = NULL;
2806    pRestoreDegProcs(currRing,strat->pOrigFDeg, strat->pOrigLDeg);
2807  }
2808  currRing->pLexOrder = b;
2809//Print("%d reductions canceled \n",strat->cel);
2810  HCord=strat->HCord;
2811  delete(strat);
2812  if ((delete_w)&&(w!=NULL)&&(*w!=NULL)) delete *w;
2813  return r;
2814}
2815#endif
2816
2817//##############################################################
2818//##############################################################
2819//##############################################################
2820//##############################################################
2821//##############################################################
2822
2823ideal kMin_std(ideal F, ideal Q, tHomog h,intvec ** w, ideal &M, intvec *hilb,
2824              int syzComp, int reduced)
2825{
2826  if(idIs0(F))
2827  {
2828    M=idInit(1,F->rank);
2829    return idInit(1,F->rank);
2830  }
2831  if(rField_is_Ring(currRing))
2832  {
2833    ideal sb;
2834    sb = kStd(F, Q, h, w, hilb);
2835    idSkipZeroes(sb);
2836    if(IDELEMS(sb) <= IDELEMS(F))
2837    {
2838        M = idCopy(sb);
2839        idSkipZeroes(M);
2840        return(sb);
2841    }
2842    else
2843    {
2844        M = idCopy(F);
2845        idSkipZeroes(M);
2846        return(sb);
2847    }
2848  }
2849  ideal r=NULL;
2850  int Kstd1_OldDeg = Kstd1_deg,i;
2851  intvec* temp_w=NULL;
2852  BOOLEAN b=currRing->pLexOrder,toReset=FALSE;
2853  BOOLEAN delete_w=(w==NULL);
2854  BOOLEAN oldDegBound=TEST_OPT_DEGBOUND;
2855  kStrategy strat=new skStrategy;
2856
2857  if(!TEST_OPT_RETURN_SB)
2858     strat->syzComp = syzComp;
2859  if (rField_has_simple_inverse(currRing))
2860    strat->LazyPass=20;
2861  else
2862    strat->LazyPass=2;
2863  strat->LazyDegree = 1;
2864  strat->minim=(reduced % 2)+1;
2865  strat->ak = id_RankFreeModule(F,currRing);
2866  if (delete_w)
2867  {
2868    temp_w=new intvec((strat->ak)+1);
2869    w = &temp_w;
2870  }
2871  if (h==testHomog)
2872  {
2873    if (strat->ak == 0)
2874    {
2875      h = (tHomog)idHomIdeal(F,Q);
2876      w=NULL;
2877    }
2878    else
2879    {
2880      h = (tHomog)idHomModule(F,Q,w);
2881    }
2882  }
2883  if (h==isHomog)
2884  {
2885    if (strat->ak > 0 && (w!=NULL) && (*w!=NULL))
2886    {
2887      kModW = *w;
2888      strat->kModW = *w;
2889      assume(currRing->pFDeg != NULL && currRing->pLDeg != NULL);
2890      strat->pOrigFDeg = currRing->pFDeg;
2891      strat->pOrigLDeg = currRing->pLDeg;
2892      pSetDegProcs(currRing,kModDeg);
2893
2894      toReset = TRUE;
2895      if (reduced>1)
2896      {
2897        Kstd1_OldDeg=Kstd1_deg;
2898        Kstd1_deg = -1;
2899        for (i=IDELEMS(F)-1;i>=0;i--)
2900        {
2901          if ((F->m[i]!=NULL) && (currRing->pFDeg(F->m[i],currRing)>=Kstd1_deg))
2902            Kstd1_deg = currRing->pFDeg(F->m[i],currRing)+1;
2903        }
2904      }
2905    }
2906    currRing->pLexOrder = TRUE;
2907    strat->LazyPass*=2;
2908  }
2909  strat->homog=h;
2910  if (rHasLocalOrMixedOrdering(currRing))
2911  {
2912    if (w!=NULL)
2913      r=mora(F,Q,*w,hilb,strat);
2914    else
2915      r=mora(F,Q,NULL,hilb,strat);
2916  }
2917  else
2918  {
2919    if (w!=NULL)
2920      r=bba(F,Q,*w,hilb,strat);
2921    else
2922      r=bba(F,Q,NULL,hilb,strat);
2923  }
2924#ifdef KDEBUG
2925  {
2926    int i;
2927    for (i=IDELEMS(r)-1; i>=0; i--) pTest(r->m[i]);
2928  }
2929#endif
2930  idSkipZeroes(r);
2931  if (toReset)
2932  {
2933    pRestoreDegProcs(currRing,strat->pOrigFDeg, strat->pOrigLDeg);
2934    kModW = NULL;
2935  }
2936  currRing->pLexOrder = b;
2937  HCord=strat->HCord;
2938  if ((delete_w)&&(temp_w!=NULL)) delete temp_w;
2939  if ((IDELEMS(r)==1) && (r->m[0]!=NULL) && pIsConstant(r->m[0]) && (strat->ak==0))
2940  {
2941    M=idInit(1,F->rank);
2942    M->m[0]=pOne();
2943    //if (strat->ak!=0) { pSetComp(M->m[0],strat->ak); pSetmComp(M->m[0]); }
2944    if (strat->M!=NULL) idDelete(&strat->M);
2945  }
2946  else if (strat->M==NULL)
2947  {
2948    M=idInit(1,F->rank);
2949    Warn("no minimal generating set computed");
2950  }
2951  else
2952  {
2953    idSkipZeroes(strat->M);
2954    M=strat->M;
2955  }
2956  delete(strat);
2957  if (reduced>2)
2958  {
2959    Kstd1_deg=Kstd1_OldDeg;
2960    if (!oldDegBound)
2961      si_opt_1 &= ~Sy_bit(OPT_DEGBOUND);
2962  }
2963  else
2964  {
2965    if (IDELEMS(M)>IDELEMS(r)) {
2966       idDelete(&M);
2967       M=idCopy(r); }
2968  }
2969  return r;
2970}
2971
2972poly kNF(ideal F, ideal Q, poly p,int syzComp, int lazyReduce)
2973{
2974  if (p==NULL)
2975     return NULL;
2976
2977  poly pp = p;
2978
2979#ifdef HAVE_PLURAL
2980  if(rIsSCA(currRing))
2981  {
2982    const unsigned int m_iFirstAltVar = scaFirstAltVar(currRing);
2983    const unsigned int m_iLastAltVar  = scaLastAltVar(currRing);
2984    pp = p_KillSquares(pp, m_iFirstAltVar, m_iLastAltVar, currRing);
2985
2986    if(Q == currRing->qideal)
2987      Q = SCAQuotient(currRing);
2988  }
2989#endif
2990
2991  if ((idIs0(F))&&(Q==NULL))
2992  {
2993#ifdef HAVE_PLURAL
2994    if(p != pp)
2995      return pp;
2996#endif
2997    return pCopy(p); /*F+Q=0*/
2998  }
2999
3000  kStrategy strat=new skStrategy;
3001  strat->syzComp = syzComp;
3002  strat->ak = si_max(id_RankFreeModule(F,currRing),pMaxComp(p));
3003  poly res;
3004
3005  if (rHasLocalOrMixedOrdering(currRing))
3006    res=kNF1(F,Q,pp,strat,lazyReduce);
3007  else
3008    res=kNF2(F,Q,pp,strat,lazyReduce);
3009  delete(strat);
3010
3011#ifdef HAVE_PLURAL
3012  if(pp != p)
3013    p_Delete(&pp, currRing);
3014#endif
3015  return res;
3016}
3017
3018poly kNFBound(ideal F, ideal Q, poly p,int bound,int syzComp, int lazyReduce)
3019{
3020  if (p==NULL)
3021     return NULL;
3022
3023  poly pp = p;
3024
3025#ifdef HAVE_PLURAL
3026  if(rIsSCA(currRing))
3027  {
3028    const unsigned int m_iFirstAltVar = scaFirstAltVar(currRing);
3029    const unsigned int m_iLastAltVar  = scaLastAltVar(currRing);
3030    pp = p_KillSquares(pp, m_iFirstAltVar, m_iLastAltVar, currRing);
3031
3032    if(Q == currRing->qideal)
3033      Q = SCAQuotient(currRing);
3034  }
3035#endif
3036
3037  if ((idIs0(F))&&(Q==NULL))
3038  {
3039#ifdef HAVE_PLURAL
3040    if(p != pp)
3041      return pp;
3042#endif
3043    return pCopy(p); /*F+Q=0*/
3044  }
3045
3046  kStrategy strat=new skStrategy;
3047  strat->syzComp = syzComp;
3048  strat->ak = si_max(id_RankFreeModule(F,currRing),pMaxComp(p));
3049  poly res;
3050  res=kNF2Bound(F,Q,pp,bound,strat,lazyReduce);
3051  delete(strat);
3052
3053#ifdef HAVE_PLURAL
3054  if(pp != p)
3055    p_Delete(&pp, currRing);
3056#endif
3057  return res;
3058}
3059
3060ideal kNF(ideal F, ideal Q, ideal p,int syzComp,int lazyReduce)
3061{
3062  ideal res;
3063  if (TEST_OPT_PROT)
3064  {
3065    Print("(S:%d)",IDELEMS(p));mflush();
3066  }
3067  if (idIs0(p))
3068    return idInit(IDELEMS(p),si_max(p->rank,F->rank));
3069
3070  ideal pp = p;
3071#ifdef HAVE_PLURAL
3072  if(rIsSCA(currRing))
3073  {
3074    const unsigned int m_iFirstAltVar = scaFirstAltVar(currRing);
3075    const unsigned int m_iLastAltVar  = scaLastAltVar(currRing);
3076    pp = id_KillSquares(pp, m_iFirstAltVar, m_iLastAltVar, currRing, false);
3077
3078    if(Q == currRing->qideal)
3079      Q = SCAQuotient(currRing);
3080  }
3081#endif
3082
3083  if ((idIs0(F))&&(Q==NULL))
3084  {
3085#ifdef HAVE_PLURAL
3086    if(p != pp)
3087      return pp;
3088#endif
3089    return idCopy(p); /*F+Q=0*/
3090  }
3091
3092  kStrategy strat=new skStrategy;
3093  strat->syzComp = syzComp;
3094  strat->ak = si_max(id_RankFreeModule(F,currRing),id_RankFreeModule(p,currRing));
3095  if (strat->ak>0) // only for module case, see Tst/Short/bug_reduce.tst
3096  {
3097    strat->ak = si_max(strat->ak,(int)F->rank);
3098  }
3099
3100  if (rHasLocalOrMixedOrdering(currRing))
3101    res=kNF1(F,Q,pp,strat,lazyReduce);
3102  else
3103    res=kNF2(F,Q,pp,strat,lazyReduce);
3104  delete(strat);
3105
3106#ifdef HAVE_PLURAL
3107  if(pp != p)
3108    id_Delete(&pp, currRing);
3109#endif
3110
3111  return res;
3112}
3113
3114ideal kNFBound(ideal F, ideal Q, ideal p,int bound,int syzComp,int lazyReduce)
3115{
3116  ideal res;
3117  if (TEST_OPT_PROT)
3118  {
3119    Print("(S:%d)",IDELEMS(p));mflush();
3120  }
3121  if (idIs0(p))
3122    return idInit(IDELEMS(p),si_max(p->rank,F->rank));
3123
3124  ideal pp = p;
3125#ifdef HAVE_PLURAL
3126  if(rIsSCA(currRing))
3127  {
3128    const unsigned int m_iFirstAltVar = scaFirstAltVar(currRing);
3129    const unsigned int m_iLastAltVar  = scaLastAltVar(currRing);
3130    pp = id_KillSquares(pp, m_iFirstAltVar, m_iLastAltVar, currRing, false);
3131
3132    if(Q == currRing->qideal)
3133      Q = SCAQuotient(currRing);
3134  }
3135#endif
3136
3137  if ((idIs0(F))&&(Q==NULL))
3138  {
3139#ifdef HAVE_PLURAL
3140    if(p != pp)
3141      return pp;
3142#endif
3143    return idCopy(p); /*F+Q=0*/
3144  }
3145
3146  kStrategy strat=new skStrategy;
3147  strat->syzComp = syzComp;
3148  strat->ak = si_max(id_RankFreeModule(F,currRing),id_RankFreeModule(p,currRing));
3149  if (strat->ak>0) // only for module case, see Tst/Short/bug_reduce.tst
3150  {
3151    strat->ak = si_max(strat->ak,(int)F->rank);
3152  }
3153
3154  res=kNF2Bound(F,Q,pp,bound,strat,lazyReduce);
3155  delete(strat);
3156
3157#ifdef HAVE_PLURAL
3158  if(pp != p)
3159    id_Delete(&pp, currRing);
3160#endif
3161
3162  return res;
3163}
3164
3165poly k_NF (ideal F, ideal Q, poly p,int syzComp, int lazyReduce, const ring _currRing)
3166{
3167  const ring save = currRing;
3168  if( currRing != _currRing ) rChangeCurrRing(_currRing);
3169  poly ret = kNF(F, Q, p, syzComp, lazyReduce);
3170  if( currRing != save )     rChangeCurrRing(save);
3171  return ret;
3172}
3173
3174/*2
3175*interreduces F
3176*/
3177// old version
3178ideal kInterRedOld (ideal F, ideal Q)
3179{
3180  int j;
3181  kStrategy strat = new skStrategy;
3182
3183  ideal tempF = F;
3184  ideal tempQ = Q;
3185
3186#ifdef HAVE_PLURAL
3187  if(rIsSCA(currRing))
3188  {
3189    const unsigned int m_iFirstAltVar = scaFirstAltVar(currRing);
3190    const unsigned int m_iLastAltVar  = scaLastAltVar(currRing);
3191    tempF = id_KillSquares(F, m_iFirstAltVar, m_iLastAltVar, currRing);
3192
3193    // this should be done on the upper level!!! :
3194    //    tempQ = SCAQuotient(currRing);
3195
3196    if(Q == currRing->qideal)
3197      tempQ = SCAQuotient(currRing);
3198  }
3199#endif
3200
3201//  if (TEST_OPT_PROT)
3202//  {
3203//    writeTime("start InterRed:");
3204//    mflush();
3205//  }
3206  //strat->syzComp     = 0;
3207  strat->kHEdgeFound = (currRing->ppNoether) != NULL;
3208  strat->kNoether=pCopy((currRing->ppNoether));
3209  strat->ak = id_RankFreeModule(tempF,currRing);
3210  initBuchMoraCrit(strat);
3211  strat->NotUsedAxis = (BOOLEAN *)omAlloc(((currRing->N)+1)*sizeof(BOOLEAN));
3212  for (j=(currRing->N); j>0; j--) strat->NotUsedAxis[j] = TRUE;
3213  strat->enterS      = enterSBba;
3214  strat->posInT      = posInT17;
3215  strat->initEcart   = initEcartNormal;
3216  strat->sl   = -1;
3217  strat->tl          = -1;
3218  strat->tmax        = setmaxT;
3219  strat->T           = initT();
3220  strat->R           = initR();
3221  strat->sevT        = initsevT();
3222  if (rHasLocalOrMixedOrdering(currRing))   strat->honey = TRUE;
3223  initS(tempF, tempQ, strat);
3224  if (TEST_OPT_REDSB)
3225    strat->noTailReduction=FALSE;
3226  updateS(TRUE,strat);
3227  if (TEST_OPT_REDSB && TEST_OPT_INTSTRATEGY)
3228    completeReduce(strat);
3229  //else if (TEST_OPT_PROT) PrintLn();
3230  cleanT(strat);
3231  if (strat->kHEdge!=NULL) pLmFree(&strat->kHEdge);
3232  omFreeSize((ADDRESS)strat->T,strat->tmax*sizeof(TObject));
3233  omFreeSize((ADDRESS)strat->ecartS,IDELEMS(strat->Shdl)*sizeof(int));
3234  omFreeSize((ADDRESS)strat->sevS,IDELEMS(strat->Shdl)*sizeof(unsigned long));
3235  omFreeSize((ADDRESS)strat->NotUsedAxis,((currRing->N)+1)*sizeof(BOOLEAN));
3236  omfree(strat->sevT);
3237  omfree(strat->S_2_R);
3238  omfree(strat->R);
3239
3240  if (strat->fromQ)
3241  {
3242    for (j=IDELEMS(strat->Shdl)-1;j>=0;j--)
3243    {
3244      if(strat->fromQ[j]) pDelete(&strat->Shdl->m[j]);
3245    }
3246    omFreeSize((ADDRESS)strat->fromQ,IDELEMS(strat->Shdl)*sizeof(int));
3247  }
3248//  if (TEST_OPT_PROT)
3249//  {
3250//    writeTime("end Interred:");
3251//    mflush();
3252//  }
3253  ideal shdl=strat->Shdl;
3254  idSkipZeroes(shdl);
3255  if (strat->fromQ)
3256  {
3257    strat->fromQ=NULL;
3258    ideal res=kInterRed(shdl,NULL);
3259    idDelete(&shdl);
3260    shdl=res;
3261  }
3262  delete(strat);
3263#ifdef HAVE_PLURAL
3264  if( tempF != F )
3265    id_Delete( &tempF, currRing);
3266#endif
3267  return shdl;
3268}
3269// new version
3270ideal kInterRedBba (ideal F, ideal Q, int &need_retry)
3271{
3272  need_retry=0;
3273  int   red_result = 1;
3274  int   olddeg,reduc;
3275  BOOLEAN withT = FALSE;
3276  // BOOLEAN toReset=FALSE;
3277  kStrategy strat=new skStrategy;
3278  tHomog h;
3279  intvec * w=NULL;
3280
3281  if (rField_has_simple_inverse(currRing))
3282    strat->LazyPass=20;
3283  else
3284    strat->LazyPass=2;
3285  strat->LazyDegree = 1;
3286  strat->ak = id_RankFreeModule(F,currRing);
3287  strat->syzComp = strat->ak;
3288  strat->kModW=kModW=NULL;
3289  strat->kHomW=kHomW=NULL;
3290  if (strat->ak == 0)
3291  {
3292    h = (tHomog)idHomIdeal(F,Q);
3293    w=NULL;
3294  }
3295  else if (!TEST_OPT_DEGBOUND)
3296  {
3297    h = (tHomog)idHomModule(F,Q,&w);
3298  }
3299  if (h==isHomog)
3300  {
3301    if (strat->ak > 0 && (w!=NULL) && (w!=NULL))
3302    {
3303      strat->kModW = kModW = w;
3304      strat->pOrigFDeg = currRing->pFDeg;
3305      strat->pOrigLDeg = currRing->pLDeg;
3306      pSetDegProcs(currRing,kModDeg);
3307      // toReset = TRUE;
3308    }
3309    strat->LazyPass*=2;
3310  }
3311  strat->homog=h;
3312#ifdef KDEBUG
3313  idTest(F);
3314#endif
3315
3316  initBuchMoraCrit(strat); /*set Gebauer, honey, sugarCrit*/
3317  if(rField_is_Ring(currRing))
3318    initBuchMoraPosRing(strat);
3319  else
3320    initBuchMoraPos(strat);
3321  initBba(strat);
3322  /*set enterS, spSpolyShort, reduce, red, initEcart, initEcartPair*/
3323  strat->posInL=posInL0; /* ord according pComp */
3324
3325  /*Shdl=*/initBuchMora(F, Q, strat);
3326  reduc = olddeg = 0;
3327
3328#ifndef NO_BUCKETS
3329  if (!TEST_OPT_NOT_BUCKETS)
3330    strat->use_buckets = 1;
3331#endif
3332
3333  // redtailBBa against T for inhomogenous input
3334  if (!TEST_OPT_OLDSTD)
3335    withT = ! strat->homog;
3336
3337  // strat->posInT = posInT_pLength;
3338  kTest_TS(strat);
3339
3340#ifdef HAVE_TAIL_RING
3341  kStratInitChangeTailRing(strat);
3342#endif
3343
3344  /* compute------------------------------------------------------- */
3345  while (strat->Ll >= 0)
3346  {
3347    #ifdef KDEBUG
3348      if (TEST_OPT_DEBUG) messageSets(strat);
3349    #endif
3350    if (strat->Ll== 0) strat->interpt=TRUE;
3351    /* picks the last element from the lazyset L */
3352    strat->P = strat->L[strat->Ll];
3353    strat->Ll--;
3354
3355    if (strat->P.p1 == NULL)
3356    {
3357      // for input polys, prepare reduction
3358      strat->P.PrepareRed(strat->use_buckets);
3359    }
3360
3361    if (strat->P.p == NULL && strat->P.t_p == NULL)
3362    {
3363      red_result = 0;
3364    }
3365    else
3366    {
3367      if (TEST_OPT_PROT)
3368        message(strat->P.pFDeg(),
3369                &olddeg,&reduc,strat, red_result);
3370
3371      /* reduction of the element chosen from L */
3372      red_result = strat->red(&strat->P,strat);
3373    }
3374
3375    // reduction to non-zero new poly
3376    if (red_result == 1)
3377    {
3378      /* statistic */
3379      if (TEST_OPT_PROT) PrintS("s");
3380
3381      // get the polynomial (canonicalize bucket, make sure P.p is set)
3382      strat->P.GetP(strat->lmBin);
3383
3384      int pos=posInS(strat,strat->sl,strat->P.p,strat->P.ecart);
3385
3386      // reduce the tail and normalize poly
3387      // in the ring case we cannot expect LC(f) = 1,
3388      // therefore we call pContent instead of pNorm
3389      if ((TEST_OPT_INTSTRATEGY) || (rField_is_Ring(currRing)))
3390      {
3391        strat->P.pCleardenom();
3392        if (0)
3393        //if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
3394        {
3395          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
3396          strat->P.pCleardenom();
3397        }
3398      }
3399      else
3400      {
3401        strat->P.pNorm();
3402        if (0)
3403        //if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
3404          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
3405      }
3406
3407#ifdef KDEBUG
3408      if (TEST_OPT_DEBUG){PrintS("new s:");strat->P.wrp();PrintLn();}
3409#endif
3410
3411      // enter into S, L, and T
3412      if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
3413      {
3414        enterT(strat->P, strat);
3415        // posInS only depends on the leading term
3416        strat->enterS(strat->P, pos, strat, strat->tl);
3417
3418        if (pos<strat->sl)
3419        {
3420          need_retry++;
3421          // move all "larger" elements fromS to L
3422          // remove them from T
3423          int ii=pos+1;
3424          for(;ii<=strat->sl;ii++)
3425          {
3426            LObject h;
3427            memset(&h,0,sizeof(h));
3428            h.tailRing=strat->tailRing;
3429            h.p=strat->S[ii]; strat->S[ii]=NULL;
3430            strat->initEcart(&h);
3431            h.sev=strat->sevS[ii];
3432            int jj=strat->tl;
3433            while (jj>=0)
3434            {
3435              if (strat->T[jj].p==h.p)
3436              {
3437                strat->T[jj].p=NULL;
3438                if (jj<strat->tl)
3439                {
3440                  memmove(&(strat->T[jj]),&(strat->T[jj+1]),
3441                          (strat->tl-jj)*sizeof(strat->T[jj]));
3442                  memmove(&(strat->sevT[jj]),&(strat->sevT[jj+1]),
3443                          (strat->tl-jj)*sizeof(strat->sevT[jj]));
3444                }
3445                strat->tl--;
3446                break;
3447              }
3448              jj--;
3449            }
3450            int lpos=strat->posInL(strat->L,strat->Ll,&h,strat);
3451            enterL(&strat->L,&strat->Ll,&strat->Lmax,h,lpos);
3452            #ifdef KDEBUG
3453            if (TEST_OPT_DEBUG)
3454            {
3455              Print("move S[%d] -> L[%d]: ",ii,pos);
3456              p_wrp(h.p,currRing, strat->tailRing);
3457              PrintLn();
3458            }
3459            #endif
3460          }
3461          if (strat->fromQ!=NULL)
3462          {
3463            for(ii=pos+1;ii<=strat->sl;ii++) strat->fromQ[ii]=0;
3464          }
3465          strat->sl=pos;
3466        }
3467      }
3468      else
3469      {
3470        // clean P
3471      }
3472      if (strat->P.lcm!=NULL)
3473#ifdef HAVE_RINGS
3474        pLmDelete(strat->P.lcm);
3475#else
3476        pLmFree(strat->P.lcm);
3477#endif
3478    }
3479
3480#ifdef KDEBUG
3481    if (TEST_OPT_DEBUG)
3482    {
3483      messageSets(strat);
3484    }
3485    memset(&(strat->P), 0, sizeof(strat->P));
3486#endif
3487    //kTest_TS(strat);: i_r out of sync in kInterRedBba, but not used!
3488  }
3489#ifdef KDEBUG
3490  //if (TEST_OPT_DEBUG) messageSets(strat);
3491#endif
3492  /* complete reduction of the standard basis--------- */
3493
3494  if((need_retry<=0) && (TEST_OPT_REDSB))
3495  {
3496    completeReduce(strat);
3497    if (strat->completeReduce_retry)
3498    {
3499      // completeReduce needed larger exponents, retry
3500      // hopefully: kStratChangeTailRing already provided a larger tailRing
3501      //    (otherwise: it will fail again)
3502      strat->completeReduce_retry=FALSE;
3503      completeReduce(strat);
3504      if (strat->completeReduce_retry)
3505      {
3506#ifdef HAVE_TAIL_RING
3507        if(currRing->bitmask>strat->tailRing->bitmask)
3508        {
3509          // retry without T
3510          strat->completeReduce_retry=FALSE;
3511          cleanT(strat);strat->tailRing=currRing;
3512          int i;
3513          for(i=strat->sl;i>=0;i--) strat->S_2_R[i]=-1;
3514          completeReduce(strat);
3515        }
3516        if (strat->completeReduce_retry)
3517#endif
3518          Werror("exponent bound is %ld",currRing->bitmask);
3519      }
3520    }
3521  }
3522  else if (TEST_OPT_PROT) PrintLn();
3523
3524
3525  /* release temp data-------------------------------- */
3526  exitBuchMora(strat);
3527//  if (TEST_OPT_WEIGHTM)
3528//  {
3529//    pRestoreDegProcs(currRing,strat->pOrigFDeg, strat->pOrigLDeg);
3530//    if (ecartWeights)
3531//    {
3532//      omFreeSize((ADDRESS)ecartWeights,((currRing->N)+1)*sizeof(short));
3533//      ecartWeights=NULL;
3534//    }
3535//  }
3536  //if (TEST_OPT_PROT) messageStat(0/*hilbcount*/,strat);
3537  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
3538  ideal res=strat->Shdl;
3539  strat->Shdl=NULL;
3540  delete strat;
3541  if (w!=NULL) delete w;
3542  return res;
3543}
3544ideal kInterRed (ideal F, ideal Q)
3545{
3546#ifdef HAVE_PLURAL
3547  if(rIsPluralRing(currRing)) return kInterRedOld(F,Q);
3548#endif
3549  if ((rHasLocalOrMixedOrdering(currRing))|| (rField_is_numeric(currRing))
3550  ||(rField_is_Ring(currRing))
3551  )
3552    return kInterRedOld(F,Q);
3553
3554    //return kInterRedOld(F,Q);
3555
3556  BITSET save1;
3557  SI_SAVE_OPT1(save1);
3558  //si_opt_1|=Sy_bit(OPT_NOT_SUGAR);
3559  si_opt_1|=Sy_bit(OPT_REDTHROUGH);
3560  //si_opt_1&= ~Sy_bit(OPT_REDTAIL);
3561  //si_opt_1&= ~Sy_bit(OPT_REDSB);
3562  //extern char * showOption() ;
3563  //Print("%s\n",showOption());
3564
3565  int need_retry;
3566  int counter=3;
3567  ideal res, res1;
3568  int elems;
3569  ideal null=NULL;
3570  if ((Q==NULL) || (!TEST_OPT_REDSB))
3571  {
3572    elems=idElem(F);
3573    res=kInterRedBba(F,Q,need_retry);
3574  }
3575  else
3576  {
3577    ideal FF=idSimpleAdd(F,Q);
3578    res=kInterRedBba(FF,NULL,need_retry);
3579    idDelete(&FF);
3580    null=idInit(1,1);
3581    if (need_retry)
3582      res1=kNF(null,Q,res,0,KSTD_NF_LAZY);
3583    else
3584      res1=kNF(null,Q,res);
3585    idDelete(&res);
3586    res=res1;
3587    need_retry=1;
3588  }
3589  if (idElem(res)<=1) need_retry=0;
3590  while (need_retry && (counter>0))
3591  {
3592    #ifdef KDEBUG
3593    if (TEST_OPT_DEBUG) { Print("retry counter %d\n",counter); }
3594    #endif
3595    res1=kInterRedBba(res,Q,need_retry);
3596    int new_elems=idElem(res1);
3597    counter -= (new_elems >= elems);
3598    elems = new_elems;
3599    idDelete(&res);
3600    if (idElem(res1)<=1) need_retry=0;
3601    if ((Q!=NULL) && (TEST_OPT_REDSB))
3602    {
3603      if (need_retry)
3604        res=kNF(null,Q,res1,0,KSTD_NF_LAZY);
3605      else
3606        res=kNF(null,Q,res1);
3607      idDelete(&res1);
3608    }
3609    else
3610      res = res1;
3611    if (idElem(res)<=1) need_retry=0;
3612  }
3613  if (null!=NULL) idDelete(&null);
3614  SI_RESTORE_OPT1(save1);
3615  idSkipZeroes(res);
3616  return res;
3617}
3618
3619// returns TRUE if mora should use buckets, false otherwise
3620static BOOLEAN kMoraUseBucket(kStrategy strat)
3621{
3622#ifdef MORA_USE_BUCKETS
3623  if (TEST_OPT_NOT_BUCKETS)
3624    return FALSE;
3625  if (strat->red == redFirst)
3626  {
3627#ifdef NO_LDEG
3628    if (strat->syzComp==0)
3629      return TRUE;
3630#else
3631    if ((strat->homog || strat->honey) && (strat->syzComp==0))
3632      return TRUE;
3633#endif
3634  }
3635  else
3636  {
3637    #ifdef HAVE_RINGS
3638    assume(strat->red == redEcart || strat->red == redRiloc);
3639    #else
3640    assume(strat->red == redEcart);
3641    #endif
3642    if (strat->honey && (strat->syzComp==0))
3643      return TRUE;
3644  }
3645#endif
3646  return FALSE;
3647}
Note: See TracBrowser for help on using the repository browser.