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

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