source: git/kernel/GBEngine/kstd1.cc @ 35a23c9

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