source: git/kernel/GBEngine/kstd1.cc @ 109e385

spielwiese
Last change on this file since 109e385 was 109e385, checked in by Adi Popescu <adi_popescum@…>, 8 years ago
fix: redMoraNF
  • Property mode set to 100644
File size: 79.9 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT:
6*/
7
8// TODO: why the following is here instead of mod2.h???
9
10
11// define if buckets should be used
12#define MORA_USE_BUCKETS
13
14#define MYTEST 0
15
16#define ADIDEBUG 0
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 KDEBUG
1258  if (TEST_OPT_DEBUG)
1259  {
1260    Print("new s%d:",atS);
1261    p_wrp(p.p,currRing,strat->tailRing);
1262    PrintLn();
1263  }
1264  #endif
1265  if ((!strat->kHEdgeFound) || (strat->kNoether!=NULL)) HEckeTest(p.p,strat);
1266  if (strat->kHEdgeFound)
1267  {
1268    if (newHEdge(strat))
1269    {
1270      firstUpdate(strat);
1271      if (TEST_OPT_FINDET)
1272        return;
1273
1274      /*- cuts elements in L above noether and reorders L -*/
1275      updateLHC(strat);
1276      /*- reorders L with respect to posInL -*/
1277      reorderL(strat);
1278    }
1279  }
1280  else if (strat->kNoether!=NULL)
1281    strat->kHEdgeFound = TRUE;
1282  else if (TEST_OPT_FASTHC)
1283  {
1284    if (strat->posInLOldFlag)
1285    {
1286      missingAxis(&strat->lastAxis,strat);
1287      if (strat->lastAxis)
1288      {
1289        strat->posInLOld = strat->posInL;
1290        strat->posInLOldFlag = FALSE;
1291        strat->posInL = posInL10;
1292        strat->posInLDependsOnLength = TRUE;
1293        updateL(strat);
1294        reorderL(strat);
1295      }
1296    }
1297    else if (strat->lastAxis)
1298      updateL(strat);
1299  }
1300}
1301
1302/*2
1303*-puts p to the standardbasis s at position at
1304*-HEckeTest
1305*  if TRUE
1306*  - computes noether
1307*/
1308void enterSMoraNF (LObject &p, int atS,kStrategy strat, int atR = -1)
1309{
1310  enterSBba(p, atS, strat, atR);
1311  if ((!strat->kHEdgeFound) || (strat->kNoether!=NULL)) HEckeTest(p.p,strat);
1312  if (strat->kHEdgeFound)
1313    newHEdge(strat);
1314  else if (strat->kNoether!=NULL)
1315    strat->kHEdgeFound = TRUE;
1316}
1317
1318void initBba(ideal /*F*/,kStrategy strat)
1319{
1320 /* setting global variables ------------------- */
1321  strat->enterS = enterSBba;
1322    strat->red = redHoney;
1323  if (strat->honey)
1324    strat->red = redHoney;
1325  else if (currRing->pLexOrder && !strat->homog)
1326    strat->red = redLazy;
1327  else
1328  {
1329    strat->LazyPass *=4;
1330    strat->red = redHomog;
1331  }
1332#ifdef HAVE_RINGS  //TODO Oliver
1333  if (rField_is_Ring(currRing))
1334  {
1335    strat->red = redRing;
1336  }
1337#endif
1338  if (currRing->pLexOrder && strat->honey)
1339    strat->initEcart = initEcartNormal;
1340  else
1341    strat->initEcart = initEcartBBA;
1342  if (strat->honey)
1343    strat->initEcartPair = initEcartPairMora;
1344  else
1345    strat->initEcartPair = initEcartPairBba;
1346//  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1347//  {
1348//    //interred  machen   Aenderung
1349//    strat->pOrigFDeg=pFDeg;
1350//    strat->pOrigLDeg=pLDeg;
1351//    //h=ggetid("ecart");
1352//    //if ((h!=NULL) /*&& (IDTYP(h)==INTVEC_CMD)*/)
1353//    //{
1354//    //  ecartWeights=iv2array(IDINTVEC(h));
1355//    //}
1356//    //else
1357//    {
1358//      ecartWeights=(short *)omAlloc(((currRing->N)+1)*sizeof(short));
1359//      /*uses automatic computation of the ecartWeights to set them*/
1360//      kEcartWeights(F->m,IDELEMS(F)-1,ecartWeights);
1361//    }
1362//    pRestoreDegProcs(currRing,totaldegreeWecart, maxdegreeWecart);
1363//    if (TEST_OPT_PROT)
1364//    {
1365//      for(i=1; i<=(currRing->N); i++)
1366//        Print(" %d",ecartWeights[i]);
1367//      PrintLn();
1368//      mflush();
1369//    }
1370//  }
1371}
1372
1373void initSba(ideal F,kStrategy strat)
1374{
1375  int i;
1376  //idhdl h;
1377 /* setting global variables ------------------- */
1378  strat->enterS = enterSSba;
1379    strat->red2 = redHoney;
1380  if (strat->honey)
1381    strat->red2 = redHoney;
1382  else if (currRing->pLexOrder && !strat->homog)
1383    strat->red2 = redLazy;
1384  else
1385  {
1386    strat->LazyPass *=4;
1387    strat->red2 = redHomog;
1388  }
1389#if defined(HAVE_RINGS)
1390  if (rField_is_Ring(currRing))
1391  {
1392    if(rHasLocalOrMixedOrdering(currRing))
1393      {strat->red = redRiloc;}
1394    else
1395      {strat->red2 = redRing;}
1396  }
1397#endif
1398  if (currRing->pLexOrder && strat->honey)
1399    strat->initEcart = initEcartNormal;
1400  else
1401    strat->initEcart = initEcartBBA;
1402  if (strat->honey)
1403    strat->initEcartPair = initEcartPairMora;
1404  else
1405    strat->initEcartPair = initEcartPairBba;
1406  //strat->kIdeal = NULL;
1407  //if (strat->ak==0) strat->kIdeal->rtyp=IDEAL_CMD;
1408  //else              strat->kIdeal->rtyp=MODUL_CMD;
1409  //strat->kIdeal->data=(void *)strat->Shdl;
1410  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1411  {
1412    //interred  machen   Aenderung
1413    strat->pOrigFDeg  = currRing->pFDeg;
1414    strat->pOrigLDeg  = currRing->pLDeg;
1415    //h=ggetid("ecart");
1416    //if ((h!=NULL) /*&& (IDTYP(h)==INTVEC_CMD)*/)
1417    //{
1418    //  ecartWeights=iv2array(IDINTVEC(h));
1419    //}
1420    //else
1421    {
1422      ecartWeights=(short *)omAlloc(((currRing->N)+1)*sizeof(short));
1423      /*uses automatic computation of the ecartWeights to set them*/
1424      kEcartWeights(F->m,IDELEMS(F)-1,ecartWeights, currRing);
1425    }
1426    pRestoreDegProcs(currRing, totaldegreeWecart, maxdegreeWecart);
1427    if (TEST_OPT_PROT)
1428    {
1429      for(i=1; i<=(currRing->N); i++)
1430        Print(" %d",ecartWeights[i]);
1431      PrintLn();
1432      mflush();
1433    }
1434  }
1435  // for sig-safe reductions in signature-based
1436  // standard basis computations
1437  strat->red          = redSig;
1438  //strat->sbaOrder  = 1;
1439  strat->currIdx      = 1;
1440}
1441
1442void initMora(ideal F,kStrategy strat)
1443{
1444  int i,j;
1445
1446  strat->NotUsedAxis = (BOOLEAN *)omAlloc(((currRing->N)+1)*sizeof(BOOLEAN));
1447  for (j=(currRing->N); j>0; j--) strat->NotUsedAxis[j] = TRUE;
1448  strat->enterS = enterSMora;
1449  strat->initEcartPair = initEcartPairMora; /*- ecart approximation -*/
1450  strat->posInLOld = strat->posInL;
1451  strat->posInLOldFlag = TRUE;
1452  strat->initEcart = initEcartNormal;
1453  strat->kHEdgeFound = (currRing->ppNoether) != NULL;
1454  if ( strat->kHEdgeFound )
1455     strat->kNoether = pCopy((currRing->ppNoether));
1456  else if (strat->kHEdgeFound || strat->homog)
1457    strat->red = redFirst;  /*take the first possible in T*/
1458  else
1459    strat->red = redEcart;/*take the first possible in under ecart-restriction*/
1460  if (strat->kHEdgeFound)
1461  {
1462    strat->HCord = currRing->pFDeg((currRing->ppNoether),currRing)+1;
1463    strat->posInT = posInT2;
1464  }
1465  else
1466  {
1467    strat->HCord = 32000;/*- very large -*/
1468  }
1469
1470#ifdef HAVE_RINGS
1471  if (rField_is_Ring(currRing))
1472    strat->red = redRiloc;
1473#endif
1474
1475  /*reads the ecartWeights used for Graebes method from the
1476   *intvec ecart and set ecartWeights
1477   */
1478  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1479  {
1480    //interred  machen   Aenderung
1481    strat->pOrigFDeg=currRing->pFDeg;
1482    strat->pOrigLDeg=currRing->pLDeg;
1483    ecartWeights=(short *)omAlloc(((currRing->N)+1)*sizeof(short));
1484    /*uses automatic computation of the ecartWeights to set them*/
1485    kEcartWeights(F->m,IDELEMS(F)-1,ecartWeights,currRing);
1486
1487    pSetDegProcs(currRing,totaldegreeWecart, maxdegreeWecart);
1488    if (TEST_OPT_PROT)
1489    {
1490      for(i=1; i<=(currRing->N); i++)
1491        Print(" %d",ecartWeights[i]);
1492      PrintLn();
1493      mflush();
1494    }
1495  }
1496  kOptimizeLDeg(currRing->pLDeg, strat);
1497}
1498
1499void kDebugPrint(kStrategy strat);
1500
1501ideal mora (ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat)
1502{
1503#ifdef HAVE_RINGS
1504#if ADIDEBUG
1505int loop_count;
1506loop_count = 1;
1507#endif
1508#endif
1509  int olddeg = 0;
1510  int reduc = 0;
1511  int red_result = 1;
1512  int hilbeledeg=1,hilbcount=0;
1513  BITSET save1;
1514  SI_SAVE_OPT1(save1);
1515  if (currRing->MixedOrder)
1516  {
1517    si_opt_1 &= ~Sy_bit(OPT_REDSB);
1518    si_opt_1 &= ~Sy_bit(OPT_REDTAIL);
1519  }
1520
1521  strat->update = TRUE;
1522  /*- setting global variables ------------------- -*/
1523  initBuchMoraCrit(strat);
1524  initHilbCrit(F,Q,&hilb,strat);
1525  initMora(F,strat);
1526  initBuchMoraPos(strat);
1527  /*Shdl=*/initBuchMora(F,Q,strat);
1528  if (TEST_OPT_FASTHC) missingAxis(&strat->lastAxis,strat);
1529  /*updateS in initBuchMora has Hecketest
1530  * and could have put strat->kHEdgdeFound FALSE*/
1531  if ((currRing->ppNoether)!=NULL)
1532  {
1533    strat->kHEdgeFound = TRUE;
1534  }
1535  if (strat->kHEdgeFound && strat->update)
1536  {
1537    firstUpdate(strat);
1538    updateLHC(strat);
1539    reorderL(strat);
1540  }
1541  if (TEST_OPT_FASTHC && (strat->lastAxis) && strat->posInLOldFlag)
1542  {
1543    strat->posInLOld = strat->posInL;
1544    strat->posInLOldFlag = FALSE;
1545    strat->posInL = posInL10;
1546    updateL(strat);
1547    reorderL(strat);
1548  }
1549  kTest_TS(strat);
1550  strat->use_buckets = kMoraUseBucket(strat);
1551  /*- compute-------------------------------------------*/
1552
1553#ifdef HAVE_TAIL_RING
1554//  if (strat->homog && strat->red == redFirst)
1555  kStratInitChangeTailRing(strat);
1556#endif
1557
1558  if (BVERBOSE(23))
1559  {
1560    kDebugPrint(strat);
1561  }
1562
1563  while (strat->Ll >= 0)
1564  {
1565    #ifdef KDEBUG
1566    if (TEST_OPT_DEBUG) messageSets(strat);
1567    #endif
1568    if (TEST_OPT_DEGBOUND
1569    && (strat->L[strat->Ll].ecart+strat->L[strat->Ll].GetpFDeg()> Kstd1_deg))
1570    {
1571      /*
1572      * stops computation if
1573      * - 24 (degBound)
1574      *   && upper degree is bigger than Kstd1_deg
1575      */
1576      while ((strat->Ll >= 0)
1577        && (strat->L[strat->Ll].p1!=NULL) && (strat->L[strat->Ll].p2!=NULL)
1578        && (strat->L[strat->Ll].ecart+strat->L[strat->Ll].GetpFDeg()> Kstd1_deg)
1579      )
1580      {
1581        deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
1582        //if (TEST_OPT_PROT)
1583        //{
1584        //   PrintS("D"); mflush();
1585        //}
1586      }
1587      if (strat->Ll<0) break;
1588      else strat->noClearS=TRUE;
1589    }
1590
1591#if ADIDEBUG
1592#ifdef KDEBUG
1593    Print("\n-------------------------------- LOOP %d ---------------------------------------\n",loop_count);
1594    //print the list L: (p1,p2,p)
1595    Print("\n    The pair list L -- in loop %d  -- is:\n",loop_count);
1596    for(int iii=0;iii<=strat->Ll;iii++)
1597    {
1598      Print("\n    L[%d]:\n",iii);
1599      PrintS("        ");p_Write(strat->L[iii].p1,strat->tailRing);
1600      PrintS("        ");p_Write(strat->L[iii].p2,strat->tailRing);
1601      PrintS("        ");p_Write(strat->L[iii].p,strat->tailRing);
1602    }
1603    PrintLn();
1604#endif
1605#endif
1606
1607    strat->P = strat->L[strat->Ll];/*- picks the last element from the lazyset L -*/
1608    if (strat->Ll==0) strat->interpt=TRUE;
1609    strat->Ll--;
1610
1611#if ADIDEBUG
1612#ifdef KDEBUG
1613    PrintS("    My new pair P = (p1,p2,p) is:\n");
1614    PrintS("      p1 = "); p_Write(strat->P.p1,strat->tailRing);PrintLn();
1615    PrintS("      p2 = "); p_Write(strat->P.p2,strat->tailRing);PrintLn();
1616    PrintS("      p = "); p_Write(strat->P.p,strat->tailRing); PrintLn();
1617    Print("\n    The old reducer list T -- at the beg of loop %d -- is :",loop_count);
1618    if(strat->tl<0)
1619    {PrintS(" Empty.\n");}
1620    else
1621    for(int iii=0;iii<=strat->tl;iii++)
1622    {
1623      Print("\n    T[%d]:",iii);
1624      p_Write(strat->T[iii].p,strat->T->tailRing);
1625    }
1626    PrintLn();
1627#endif /* ADIDEBUG */
1628#endif
1629
1630    // create the real Spoly
1631    if (pNext(strat->P.p) == strat->tail)
1632    {
1633      /*- deletes the short spoly and computes -*/
1634#ifdef HAVE_RINGS
1635      if (rField_is_Ring(currRing))
1636        pLmDelete(strat->P.p);
1637      else
1638#endif
1639      pLmFree(strat->P.p);
1640      strat->P.p = NULL;
1641      poly m1 = NULL, m2 = NULL;
1642      // check that spoly creation is ok
1643      while (strat->tailRing != currRing &&
1644             !kCheckSpolyCreation(&(strat->P), strat, m1, m2))
1645      {
1646        assume(m1 == NULL && m2 == NULL);
1647        // if not, change to a ring where exponents are large enough
1648        kStratChangeTailRing(strat);
1649      }
1650      /* create the real one */
1651      ksCreateSpoly(&(strat->P), strat->kNoetherTail(), strat->use_buckets,
1652                    strat->tailRing, m1, m2, strat->R);
1653      if (!strat->use_buckets)
1654        strat->P.SetLength(strat->length_pLength);
1655    }
1656    else if (strat->P.p1 == NULL)
1657    {
1658      // for input polys, prepare reduction (buckets !)
1659      strat->P.SetLength(strat->length_pLength);
1660      strat->P.PrepareRed(strat->use_buckets);
1661    }
1662
1663    if (!strat->P.IsNull())
1664    {
1665      // might be NULL from noether !!!
1666      if (TEST_OPT_PROT)
1667        message(strat->P.ecart+strat->P.GetpFDeg(),&olddeg,&reduc,strat, red_result);
1668      // reduce
1669      red_result = strat->red(&strat->P,strat);
1670    }
1671
1672    if (! strat->P.IsNull())
1673    {
1674      strat->P.GetP();
1675      // statistics
1676      if (TEST_OPT_PROT) PrintS("s");
1677      // normalization
1678      if (!TEST_OPT_INTSTRATEGY)
1679        strat->P.pNorm();
1680      // tailreduction
1681      strat->P.p = redtail(&(strat->P),strat->sl,strat);
1682      if (strat->P.p==NULL)
1683      {
1684        WerrorS("expoent overflow - wrong ordering");
1685        return(idInit(1,1));
1686      }
1687      // set ecart -- might have changed because of tail reductions
1688      if ((!strat->noTailReduction) && (!strat->honey))
1689        strat->initEcart(&strat->P);
1690      // cancel unit
1691      cancelunit(&strat->P);
1692      // for char 0, clear denominators
1693      if (TEST_OPT_INTSTRATEGY)
1694        strat->P.pCleardenom();
1695
1696      // put in T
1697      enterT(strat->P,strat);
1698      // build new pairs
1699#ifdef HAVE_RINGS
1700      if (rField_is_Ring(currRing))
1701      {
1702        superenterpairs(strat->P.p,strat->sl,strat->P.ecart,0,strat, strat->tl);
1703
1704#if ADIDEBUG
1705        Print("\n    The new pair list L -- after superenterpairs in loop %d -- is:\n",loop_count);
1706        for(int iii=0;iii<=strat->Ll;iii++)
1707        {
1708          PrintS("\n    L[%d]:\n",iii);
1709          PrintS("         ");p_Write(strat->L[iii].p1,strat->tailRing);
1710          PrintS("         ");p_Write(strat->L[iii].p2,strat->tailRing);
1711          PrintS("         ");p_Write(strat->L[iii].p,strat->tailRing);
1712        }
1713#endif
1714      }
1715      else
1716#endif
1717      enterpairs(strat->P.p,strat->sl,strat->P.ecart,0,strat, strat->tl);
1718      // put in S
1719      strat->enterS(strat->P,
1720                    posInS(strat,strat->sl,strat->P.p, strat->P.ecart),
1721                    strat, strat->tl);
1722
1723      // apply hilbert criterion
1724      if (hilb!=NULL)
1725      {
1726        if (strat->homog==isHomog)
1727          khCheck(Q,w,hilb,hilbeledeg,hilbcount,strat);
1728        else
1729          khCheckLocInhom(Q,w,hilb,hilbcount,strat);
1730      }
1731
1732      // clear strat->P
1733      if (strat->P.lcm!=NULL)
1734#if defined(HAVE_RINGS)
1735        pLmDelete(strat->P.lcm);
1736#else
1737        pLmFree(strat->P.lcm);
1738#endif
1739      strat->P.lcm=NULL;
1740#ifdef KDEBUG
1741      // make sure kTest_TS does not complain about strat->P
1742      memset(&strat->P,0,sizeof(strat->P));
1743#endif
1744    }
1745    if (strat->kHEdgeFound)
1746    {
1747      if ((TEST_OPT_FINDET)
1748      || ((TEST_OPT_MULTBOUND) && (scMult0Int(strat->Shdl,NULL,strat->tailRing) < Kstd1_mu)))
1749      {
1750        // obachman: is this still used ???
1751        /*
1752        * stops computation if strat->kHEdgeFound and
1753        * - 27 (finiteDeterminacyTest)
1754        * or
1755        * - 23
1756        *   (multBound)
1757        *   && multiplicity of the ideal is smaller then a predefined number mu
1758        */
1759        while (strat->Ll >= 0) deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
1760      }
1761    }
1762    kTest_TS(strat);
1763
1764#if ADIDEBUG
1765    Print("\n    The new reducer list T -- at the end of loop %d -- is\n",loop_count);
1766    for(int iii=0;iii<=strat->tl;iii++)
1767    {
1768      PrintS("\n    T[%d]:",iii);
1769      p_Write(strat->T[iii].p,strat->tailRing);
1770    }
1771    PrintLn();
1772
1773    loop_count++;
1774#endif /* ADIDEBUG */
1775  }
1776  /*- complete reduction of the standard basis------------------------ -*/
1777  if (TEST_OPT_REDSB) completeReduce(strat);
1778  else if (TEST_OPT_PROT) PrintLn();
1779  /*- release temp data------------------------------- -*/
1780  exitBuchMora(strat);
1781  /*- polynomials used for HECKE: HC, noether -*/
1782  if (TEST_OPT_FINDET)
1783  {
1784    if (strat->kHEdge!=NULL)
1785      Kstd1_mu=currRing->pFDeg(strat->kHEdge,currRing);
1786    else
1787      Kstd1_mu=-1;
1788  }
1789  pDelete(&strat->kHEdge);
1790  strat->update = TRUE; //???
1791  strat->lastAxis = 0; //???
1792  pDelete(&strat->kNoether);
1793  omFreeSize((ADDRESS)strat->NotUsedAxis,((currRing->N)+1)*sizeof(BOOLEAN));
1794  if (TEST_OPT_PROT) messageStat(hilbcount,strat);
1795//  if (TEST_OPT_WEIGHTM)
1796//  {
1797//    pRestoreDegProcs(currRing,strat->pOrigFDeg, strat->pOrigLDeg);
1798//    if (ecartWeights)
1799//    {
1800//      omFreeSize((ADDRESS)ecartWeights,((currRing->N)+1)*sizeof(short));
1801//      ecartWeights=NULL;
1802//    }
1803//  }
1804  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
1805  SI_RESTORE_OPT1(save1);
1806  idTest(strat->Shdl);
1807  return (strat->Shdl);
1808}
1809
1810poly kNF1 (ideal F,ideal Q,poly q, kStrategy strat, int lazyReduce)
1811{
1812  assume(q!=NULL);
1813  assume(!(idIs0(F)&&(Q==NULL)));
1814
1815// lazy_reduce flags: can be combined by |
1816//#define KSTD_NF_LAZY   1
1817  // do only a reduction of the leading term
1818//#define KSTD_NF_ECART  2
1819  // only local: recude even with bad ecart
1820  poly   p;
1821  int   i;
1822  int   j;
1823  int   o;
1824  LObject   h;
1825  BITSET save1;
1826  SI_SAVE_OPT1(save1);
1827
1828  //if ((idIs0(F))&&(Q==NULL))
1829  //  return pCopy(q); /*F=0*/
1830  //strat->ak = si_max(idRankFreeModule(F),pMaxComp(q));
1831  /*- creating temp data structures------------------- -*/
1832  strat->kHEdgeFound = (currRing->ppNoether) != NULL;
1833  strat->kNoether    = pCopy((currRing->ppNoether));
1834  si_opt_1|=Sy_bit(OPT_REDTAIL);
1835  si_opt_1&=~Sy_bit(OPT_INTSTRATEGY);
1836  if (TEST_OPT_STAIRCASEBOUND
1837  && (! TEST_V_DEG_STOP)
1838  && (0<Kstd1_deg)
1839  && ((!strat->kHEdgeFound)
1840    ||(TEST_OPT_DEGBOUND && (pWTotaldegree(strat->kNoether)<Kstd1_deg))))
1841  {
1842    pDelete(&strat->kNoether);
1843    strat->kNoether=pOne();
1844    pSetExp(strat->kNoether,1, Kstd1_deg+1);
1845    pSetm(strat->kNoether);
1846    strat->kHEdgeFound=TRUE;
1847  }
1848  initBuchMoraCrit(strat);
1849  initBuchMoraPos(strat);
1850  initMora(F,strat);
1851  strat->enterS = enterSMoraNF;
1852  /*- set T -*/
1853  strat->tl = -1;
1854  strat->tmax = setmaxT;
1855  strat->T = initT();
1856  strat->R = initR();
1857  strat->sevT = initsevT();
1858  /*- set S -*/
1859  strat->sl = -1;
1860  /*- init local data struct.-------------------------- -*/
1861  /*Shdl=*/initS(F,Q,strat);
1862  if ((strat->ak!=0)
1863  && (strat->kHEdgeFound))
1864  {
1865    if (strat->ak!=1)
1866    {
1867      pSetComp(strat->kNoether,1);
1868      pSetmComp(strat->kNoether);
1869      poly p=pHead(strat->kNoether);
1870      pSetComp(p,strat->ak);
1871      pSetmComp(p);
1872      p=pAdd(strat->kNoether,p);
1873      strat->kNoether=pNext(p);
1874      p_LmFree(p,currRing);
1875    }
1876  }
1877  if ((lazyReduce & KSTD_NF_LAZY)==0)
1878  {
1879    for (i=strat->sl; i>=0; i--)
1880      pNorm(strat->S[i]);
1881  }
1882  /*- puts the elements of S also to T -*/
1883  for (i=0; i<=strat->sl; i++)
1884  {
1885    h.p = strat->S[i];
1886    h.ecart = strat->ecartS[i];
1887    if (strat->sevS[i] == 0) strat->sevS[i] = pGetShortExpVector(h.p);
1888    else assume(strat->sevS[i] == pGetShortExpVector(h.p));
1889    h.length = pLength(h.p);
1890    h.sev = strat->sevS[i];
1891    h.SetpFDeg();
1892    enterT(h,strat);
1893  }
1894#ifdef KDEBUG
1895//  kDebugPrint(strat);
1896#endif
1897  /*- compute------------------------------------------- -*/
1898  p = pCopy(q);
1899  deleteHC(&p,&o,&j,strat);
1900  kTest(strat);
1901  if (TEST_OPT_PROT) { PrintS("r"); mflush(); }
1902  if (BVERBOSE(23)) kDebugPrint(strat);
1903  if (p!=NULL) p = redMoraNF(p,strat, lazyReduce & KSTD_NF_ECART);
1904  if ((p!=NULL)&&((lazyReduce & KSTD_NF_LAZY)==0))
1905  {
1906    if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
1907    p = redtail(p,strat->sl,strat);
1908  }
1909  /*- release temp data------------------------------- -*/
1910  cleanT(strat);
1911  assume(strat->L==NULL); /*strat->L unsed */
1912  assume(strat->B==NULL); /*strat->B unused */
1913  omFreeSize((ADDRESS)strat->T,strat->tmax*sizeof(TObject));
1914  omFreeSize((ADDRESS)strat->ecartS,IDELEMS(strat->Shdl)*sizeof(int));
1915  omFreeSize((ADDRESS)strat->sevS,IDELEMS(strat->Shdl)*sizeof(unsigned long));
1916  omFreeSize((ADDRESS)strat->NotUsedAxis,((currRing->N)+1)*sizeof(BOOLEAN));
1917  omFree(strat->sevT);
1918  omFree(strat->S_2_R);
1919  omFree(strat->R);
1920
1921  if ((Q!=NULL)&&(strat->fromQ!=NULL))
1922  {
1923    i=((IDELEMS(Q)+IDELEMS(F)+15)/16)*16;
1924    omFreeSize((ADDRESS)strat->fromQ,i*sizeof(int));
1925    strat->fromQ=NULL;
1926  }
1927  pDelete(&strat->kHEdge);
1928  pDelete(&strat->kNoether);
1929//  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1930//  {
1931//    pRestoreDegProcs(currRing,strat->pOrigFDeg, strat->pOrigLDeg);
1932//    if (ecartWeights)
1933//    {
1934//      omFreeSize((ADDRESS *)&ecartWeights,((currRing->N)+1)*sizeof(short));
1935//      ecartWeights=NULL;
1936//    }
1937//  }
1938  idDelete(&strat->Shdl);
1939  SI_RESTORE_OPT1(save1);
1940  if (TEST_OPT_PROT) PrintLn();
1941  return p;
1942}
1943
1944ideal kNF1 (ideal F,ideal Q,ideal q, kStrategy strat, int lazyReduce)
1945{
1946  assume(!idIs0(q));
1947  assume(!(idIs0(F)&&(Q==NULL)));
1948
1949// lazy_reduce flags: can be combined by |
1950//#define KSTD_NF_LAZY   1
1951  // do only a reduction of the leading term
1952//#define KSTD_NF_ECART  2
1953  // only local: recude even with bad ecart
1954  poly   p;
1955  int   i;
1956  int   j;
1957  int   o;
1958  LObject   h;
1959  ideal res;
1960  BITSET save1;
1961  SI_SAVE_OPT1(save1);
1962
1963  //if (idIs0(q)) return idInit(IDELEMS(q),si_max(q->rank,F->rank));
1964  //if ((idIs0(F))&&(Q==NULL))
1965  //  return idCopy(q); /*F=0*/
1966  //strat->ak = si_max(idRankFreeModule(F),idRankFreeModule(q));
1967  /*- creating temp data structures------------------- -*/
1968  strat->kHEdgeFound = (currRing->ppNoether) != NULL;
1969  strat->kNoether=pCopy((currRing->ppNoether));
1970  si_opt_1|=Sy_bit(OPT_REDTAIL);
1971  if (TEST_OPT_STAIRCASEBOUND
1972  && (0<Kstd1_deg)
1973  && ((!strat->kHEdgeFound)
1974    ||(TEST_OPT_DEGBOUND && (pWTotaldegree(strat->kNoether)<Kstd1_deg))))
1975  {
1976    pDelete(&strat->kNoether);
1977    strat->kNoether=pOne();
1978    pSetExp(strat->kNoether,1, Kstd1_deg+1);
1979    pSetm(strat->kNoether);
1980    strat->kHEdgeFound=TRUE;
1981  }
1982  initBuchMoraCrit(strat);
1983  initBuchMoraPos(strat);
1984  initMora(F,strat);
1985  strat->enterS = enterSMoraNF;
1986  /*- set T -*/
1987  strat->tl = -1;
1988  strat->tmax = setmaxT;
1989  strat->T = initT();
1990  strat->R = initR();
1991  strat->sevT = initsevT();
1992  /*- set S -*/
1993  strat->sl = -1;
1994  /*- init local data struct.-------------------------- -*/
1995  /*Shdl=*/initS(F,Q,strat);
1996  if ((strat->ak!=0)
1997  && (strat->kHEdgeFound))
1998  {
1999    if (strat->ak!=1)
2000    {
2001      pSetComp(strat->kNoether,1);
2002      pSetmComp(strat->kNoether);
2003      poly p=pHead(strat->kNoether);
2004      pSetComp(p,strat->ak);
2005      pSetmComp(p);
2006      p=pAdd(strat->kNoether,p);
2007      strat->kNoether=pNext(p);
2008      p_LmFree(p,currRing);
2009    }
2010  }
2011  if (TEST_OPT_INTSTRATEGY && ((lazyReduce & KSTD_NF_LAZY)==0))
2012  {
2013    for (i=strat->sl; i>=0; i--)
2014      pNorm(strat->S[i]);
2015  }
2016  /*- compute------------------------------------------- -*/
2017  res=idInit(IDELEMS(q),strat->ak);
2018  for (i=0; i<IDELEMS(q); i++)
2019  {
2020    if (q->m[i]!=NULL)
2021    {
2022      p = pCopy(q->m[i]);
2023      deleteHC(&p,&o,&j,strat);
2024      if (p!=NULL)
2025      {
2026        /*- puts the elements of S also to T -*/
2027        for (j=0; j<=strat->sl; j++)
2028        {
2029          h.p = strat->S[j];
2030          h.ecart = strat->ecartS[j];
2031          h.pLength = h.length = pLength(h.p);
2032          if (strat->sevS[j] == 0) strat->sevS[j] = pGetShortExpVector(h.p);
2033          else assume(strat->sevS[j] == pGetShortExpVector(h.p));
2034          h.sev = strat->sevS[j];
2035          h.SetpFDeg();
2036          enterT(h,strat);
2037        }
2038        if (TEST_OPT_PROT) { PrintS("r"); mflush(); }
2039        p = redMoraNF(p,strat, lazyReduce & KSTD_NF_ECART);
2040        if ((p!=NULL)&&((lazyReduce & KSTD_NF_LAZY)==0))
2041        {
2042          if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
2043          p = redtail(p,strat->sl,strat);
2044        }
2045        cleanT(strat);
2046      }
2047      res->m[i]=p;
2048    }
2049    //else
2050    //  res->m[i]=NULL;
2051  }
2052  /*- release temp data------------------------------- -*/
2053  assume(strat->L==NULL); /*strat->L unsed */
2054  assume(strat->B==NULL); /*strat->B unused */
2055  omFreeSize((ADDRESS)strat->T,strat->tmax*sizeof(TObject));
2056  omFreeSize((ADDRESS)strat->ecartS,IDELEMS(strat->Shdl)*sizeof(int));
2057  omFreeSize((ADDRESS)strat->sevS,IDELEMS(strat->Shdl)*sizeof(unsigned long));
2058  omFreeSize((ADDRESS)strat->NotUsedAxis,((currRing->N)+1)*sizeof(BOOLEAN));
2059  omFree(strat->sevT);
2060  omFree(strat->S_2_R);
2061  omFree(strat->R);
2062  if ((Q!=NULL)&&(strat->fromQ!=NULL))
2063  {
2064    i=((IDELEMS(Q)+IDELEMS(F)+15)/16)*16;
2065    omFreeSize((ADDRESS)strat->fromQ,i*sizeof(int));
2066    strat->fromQ=NULL;
2067  }
2068  pDelete(&strat->kHEdge);
2069  pDelete(&strat->kNoether);
2070//  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
2071//  {
2072//    pFDeg=strat->pOrigFDeg;
2073//    pLDeg=strat->pOrigLDeg;
2074//    if (ecartWeights)
2075//    {
2076//      omFreeSize((ADDRESS *)&ecartWeights,((currRing->N)+1)*sizeof(short));
2077//      ecartWeights=NULL;
2078//    }
2079//  }
2080  idDelete(&strat->Shdl);
2081  SI_RESTORE_OPT1(save1);
2082  if (TEST_OPT_PROT) PrintLn();
2083  return res;
2084}
2085
2086intvec * kModW, * kHomW;
2087
2088long kModDeg(poly p, ring r)
2089{
2090  long o=p_WDegree(p, r);
2091  long i=p_GetComp(p, r);
2092  if (i==0) return o;
2093  //assume((i>0) && (i<=kModW->length()));
2094  if (i<=kModW->length())
2095    return o+(*kModW)[i-1];
2096  return o;
2097}
2098long kHomModDeg(poly p, ring r)
2099{
2100  int i;
2101  long j=0;
2102
2103  for (i=r->N;i>0;i--)
2104    j+=p_GetExp(p,i,r)*(*kHomW)[i-1];
2105  if (kModW == NULL) return j;
2106  i = p_GetComp(p,r);
2107  if (i==0) return j;
2108  return j+(*kModW)[i-1];
2109}
2110
2111ideal kStd(ideal F, ideal Q, tHomog h,intvec ** w, intvec *hilb,int syzComp,
2112          int newIdeal, intvec *vw, s_poly_proc_t sp)
2113{
2114  if(idIs0(F))
2115    return idInit(1,F->rank);
2116
2117  ideal r;
2118  BOOLEAN b=currRing->pLexOrder,toReset=FALSE;
2119  BOOLEAN delete_w=(w==NULL);
2120  kStrategy strat=new skStrategy;
2121
2122  strat->s_poly=sp;
2123  if(!TEST_OPT_RETURN_SB)
2124    strat->syzComp = syzComp;
2125  if (TEST_OPT_SB_1
2126    #ifdef HAVE_RINGS
2127    &&(!rField_is_Ring(currRing))
2128    #endif
2129    )
2130    strat->newIdeal = newIdeal;
2131  if (rField_has_simple_inverse(currRing))
2132    strat->LazyPass=20;
2133  else
2134    strat->LazyPass=2;
2135  strat->LazyDegree = 1;
2136  strat->ak = id_RankFreeModule(F,currRing);
2137  strat->kModW=kModW=NULL;
2138  strat->kHomW=kHomW=NULL;
2139  if (vw != NULL)
2140  {
2141    currRing->pLexOrder=FALSE;
2142    strat->kHomW=kHomW=vw;
2143    strat->pOrigFDeg = currRing->pFDeg;
2144    strat->pOrigLDeg = currRing->pLDeg;
2145    pSetDegProcs(currRing,kHomModDeg);
2146    toReset = TRUE;
2147  }
2148  if (h==testHomog)
2149  {
2150    if (strat->ak == 0)
2151    {
2152      h = (tHomog)idHomIdeal(F,Q);
2153      w=NULL;
2154    }
2155    else if (!TEST_OPT_DEGBOUND)
2156    {
2157      h = (tHomog)idHomModule(F,Q,w);
2158    }
2159  }
2160  currRing->pLexOrder=b;
2161  if (h==isHomog)
2162  {
2163    if (strat->ak > 0 && (w!=NULL) && (*w!=NULL))
2164    {
2165      strat->kModW = kModW = *w;
2166      if (vw == NULL)
2167      {
2168        strat->pOrigFDeg = currRing->pFDeg;
2169        strat->pOrigLDeg = currRing->pLDeg;
2170        pSetDegProcs(currRing,kModDeg);
2171        toReset = TRUE;
2172      }
2173    }
2174    currRing->pLexOrder = TRUE;
2175    if (hilb==NULL) strat->LazyPass*=2;
2176  }
2177  strat->homog=h;
2178#ifdef KDEBUG
2179  idTest(F);
2180  if (Q!=NULL) idTest(Q);
2181
2182#if MYTEST
2183  if (TEST_OPT_DEBUG)
2184  {
2185    PrintS("// kSTD: currRing: ");
2186    rWrite(currRing);
2187  }
2188#endif
2189
2190#endif
2191#ifdef HAVE_PLURAL
2192  if (rIsPluralRing(currRing))
2193  {
2194    const BOOLEAN bIsSCA  = rIsSCA(currRing) && strat->z2homog; // for Z_2 prod-crit
2195    strat->no_prod_crit   = ! bIsSCA;
2196    if (w!=NULL)
2197      r = nc_GB(F, Q, *w, hilb, strat, currRing);
2198    else
2199      r = nc_GB(F, Q, NULL, hilb, strat, currRing);
2200  }
2201  else
2202#endif
2203#ifdef HAVE_RINGS
2204  if (rField_is_Ring(currRing))
2205  {
2206    if(rHasLocalOrMixedOrdering(currRing))
2207      r=mora(F,Q,NULL,hilb,strat);
2208    else
2209      r=bba(F,Q,NULL,hilb,strat);
2210  }
2211  else
2212#endif
2213  {
2214    if (rHasLocalOrMixedOrdering(currRing))
2215    {
2216      if (w!=NULL)
2217        r=mora(F,Q,*w,hilb,strat);
2218      else
2219        r=mora(F,Q,NULL,hilb,strat);
2220    }
2221    else
2222    {
2223      if (w!=NULL)
2224        r=bba(F,Q,*w,hilb,strat);
2225      else
2226        r=bba(F,Q,NULL,hilb,strat);
2227    }
2228  }
2229#ifdef KDEBUG
2230  idTest(r);
2231#endif
2232  if (toReset)
2233  {
2234    kModW = NULL;
2235    pRestoreDegProcs(currRing,strat->pOrigFDeg, strat->pOrigLDeg);
2236  }
2237  currRing->pLexOrder = b;
2238//Print("%d reductions canceled \n",strat->cel);
2239  HCord=strat->HCord;
2240  delete(strat);
2241  if ((delete_w)&&(w!=NULL)&&(*w!=NULL)) delete *w;
2242  return r;
2243}
2244
2245ideal kSba(ideal F, ideal Q, tHomog h,intvec ** w, int sbaOrder, int arri, intvec *hilb,int syzComp,
2246          int newIdeal, intvec *vw)
2247{
2248  if(idIs0(F))
2249    return idInit(1,F->rank);
2250
2251  ideal r;
2252  BOOLEAN b=currRing->pLexOrder,toReset=FALSE;
2253  BOOLEAN delete_w=(w==NULL);
2254  kStrategy strat=new skStrategy;
2255  strat->sbaOrder = sbaOrder;
2256  if (arri!=0)
2257  {
2258    strat->rewCrit1 = arriRewDummy;
2259    strat->rewCrit2 = arriRewCriterion;
2260    strat->rewCrit3 = arriRewCriterionPre;
2261  }
2262  else
2263  {
2264    strat->rewCrit1 = faugereRewCriterion;
2265    strat->rewCrit2 = faugereRewCriterion;
2266    strat->rewCrit3 = faugereRewCriterion;
2267  }
2268
2269  if(!TEST_OPT_RETURN_SB)
2270    strat->syzComp = syzComp;
2271  if (TEST_OPT_SB_1)
2272    #ifdef HAVE_RINGS
2273    if(!rField_is_Ring(currRing))
2274    #endif
2275    strat->newIdeal = newIdeal;
2276  if (rField_has_simple_inverse(currRing))
2277    strat->LazyPass=20;
2278  else
2279    strat->LazyPass=2;
2280  strat->LazyDegree = 1;
2281  strat->enterOnePair=enterOnePairNormal;
2282  strat->chainCrit=chainCritNormal;
2283  strat->ak = id_RankFreeModule(F,currRing);
2284  strat->kModW=kModW=NULL;
2285  strat->kHomW=kHomW=NULL;
2286  if (vw != NULL)
2287  {
2288    currRing->pLexOrder=FALSE;
2289    strat->kHomW=kHomW=vw;
2290    strat->pOrigFDeg = currRing->pFDeg;
2291    strat->pOrigLDeg = currRing->pLDeg;
2292    pSetDegProcs(currRing,kHomModDeg);
2293    toReset = TRUE;
2294  }
2295  if (h==testHomog)
2296  {
2297    if (strat->ak == 0)
2298    {
2299      h = (tHomog)idHomIdeal(F,Q);
2300      w=NULL;
2301    }
2302    else if (!TEST_OPT_DEGBOUND)
2303    {
2304      h = (tHomog)idHomModule(F,Q,w);
2305    }
2306  }
2307  currRing->pLexOrder=b;
2308  if (h==isHomog)
2309  {
2310    if (strat->ak > 0 && (w!=NULL) && (*w!=NULL))
2311    {
2312      strat->kModW = kModW = *w;
2313      if (vw == NULL)
2314      {
2315        strat->pOrigFDeg = currRing->pFDeg;
2316        strat->pOrigLDeg = currRing->pLDeg;
2317        pSetDegProcs(currRing,kModDeg);
2318        toReset = TRUE;
2319      }
2320    }
2321    currRing->pLexOrder = TRUE;
2322    if (hilb==NULL) strat->LazyPass*=2;
2323  }
2324  strat->homog=h;
2325#ifdef KDEBUG
2326  idTest(F);
2327  idTest(Q);
2328
2329#if MYTEST
2330  if (TEST_OPT_DEBUG)
2331  {
2332    PrintS("// kSTD: currRing: ");
2333    rWrite(currRing);
2334  }
2335#endif
2336
2337#endif
2338#ifdef HAVE_PLURAL
2339  if (rIsPluralRing(currRing))
2340  {
2341    const BOOLEAN bIsSCA  = rIsSCA(currRing) && strat->z2homog; // for Z_2 prod-crit
2342    strat->no_prod_crit   = ! bIsSCA;
2343    if (w!=NULL)
2344      r = nc_GB(F, Q, *w, hilb, strat, currRing);
2345    else
2346      r = nc_GB(F, Q, NULL, hilb, strat, currRing);
2347  }
2348  else
2349#endif
2350#ifdef HAVE_RINGS
2351  if (rField_is_Ring(currRing))
2352    r=bba(F,Q,NULL,hilb,strat);
2353  else
2354#endif
2355  {
2356    if (rHasLocalOrMixedOrdering(currRing))
2357    {
2358      if (w!=NULL)
2359        r=mora(F,Q,*w,hilb,strat);
2360      else
2361        r=mora(F,Q,NULL,hilb,strat);
2362    }
2363    else
2364    {
2365      if (w!=NULL)
2366        r=sba(F,Q,*w,hilb,strat);
2367      else
2368        r=sba(F,Q,NULL,hilb,strat);
2369    }
2370  }
2371#ifdef KDEBUG
2372  idTest(r);
2373#endif
2374  if (toReset)
2375  {
2376    kModW = NULL;
2377    pRestoreDegProcs(currRing,strat->pOrigFDeg, strat->pOrigLDeg);
2378  }
2379  currRing->pLexOrder = b;
2380//Print("%d reductions canceled \n",strat->cel);
2381  HCord=strat->HCord;
2382  delete(strat);
2383  if ((delete_w)&&(w!=NULL)&&(*w!=NULL)) delete *w;
2384  return r;
2385}
2386
2387#ifdef HAVE_SHIFTBBA
2388ideal kStdShift(ideal F, ideal Q, tHomog h,intvec ** w, intvec *hilb,int syzComp,
2389                int newIdeal, intvec *vw, int uptodeg, int lV)
2390{
2391  ideal r;
2392  BOOLEAN b=currRing->pLexOrder,toReset=FALSE;
2393  BOOLEAN delete_w=(w==NULL);
2394  kStrategy strat=new skStrategy;
2395
2396  if(!TEST_OPT_RETURN_SB)
2397    strat->syzComp = syzComp;
2398  if (TEST_OPT_SB_1)
2399    #ifdef HAVE_RINGS
2400    if(!rField_is_Ring(currRing))
2401    #endif
2402    strat->newIdeal = newIdeal;
2403  if (rField_has_simple_inverse(currRing))
2404    strat->LazyPass=20;
2405  else
2406    strat->LazyPass=2;
2407  strat->LazyDegree = 1;
2408  strat->ak = id_RankFreeModule(F,currRing);
2409  strat->kModW=kModW=NULL;
2410  strat->kHomW=kHomW=NULL;
2411  if (vw != NULL)
2412  {
2413    currRing->pLexOrder=FALSE;
2414    strat->kHomW=kHomW=vw;
2415    strat->pOrigFDeg = currRing->pFDeg;
2416    strat->pOrigLDeg = currRing->pLDeg;
2417    pSetDegProcs(currRing,kHomModDeg);
2418    toReset = TRUE;
2419  }
2420  if (h==testHomog)
2421  {
2422    if (strat->ak == 0)
2423    {
2424      h = (tHomog)idHomIdeal(F,Q);
2425      w=NULL;
2426    }
2427    else if (!TEST_OPT_DEGBOUND)
2428    {
2429      h = (tHomog)idHomModule(F,Q,w);
2430    }
2431  }
2432  currRing->pLexOrder=b;
2433  if (h==isHomog)
2434  {
2435    if (strat->ak > 0 && (w!=NULL) && (*w!=NULL))
2436    {
2437      strat->kModW = kModW = *w;
2438      if (vw == NULL)
2439      {
2440        strat->pOrigFDeg = currRing->pFDeg;
2441        strat->pOrigLDeg = currRing->pLDeg;
2442        pSetDegProcs(currRing,kModDeg);
2443        toReset = TRUE;
2444      }
2445    }
2446    currRing->pLexOrder = TRUE;
2447    if (hilb==NULL) strat->LazyPass*=2;
2448  }
2449  strat->homog=h;
2450#ifdef KDEBUG
2451  idTest(F);
2452#endif
2453  if (rHasLocalOrMixedOrdering(currRing))
2454  {
2455    /* error: no local ord yet with shifts */
2456    Print("No local ordering possible for shifts");
2457    return(NULL);
2458  }
2459  else
2460  {
2461    /* global ordering */
2462    if (w!=NULL)
2463      r=bbaShift(F,Q,*w,hilb,strat,uptodeg,lV);
2464    else
2465      r=bbaShift(F,Q,NULL,hilb,strat,uptodeg,lV);
2466  }
2467#ifdef KDEBUG
2468  idTest(r);
2469#endif
2470  if (toReset)
2471  {
2472    kModW = NULL;
2473    pRestoreDegProcs(currRing,strat->pOrigFDeg, strat->pOrigLDeg);
2474  }
2475  currRing->pLexOrder = b;
2476//Print("%d reductions canceled \n",strat->cel);
2477  HCord=strat->HCord;
2478  delete(strat);
2479  if ((delete_w)&&(w!=NULL)&&(*w!=NULL)) delete *w;
2480  return r;
2481}
2482#endif
2483
2484//##############################################################
2485//##############################################################
2486//##############################################################
2487//##############################################################
2488//##############################################################
2489
2490ideal kMin_std(ideal F, ideal Q, tHomog h,intvec ** w, ideal &M, intvec *hilb,
2491              int syzComp, int reduced)
2492{
2493  if(idIs0(F))
2494  {
2495    M=idInit(1,F->rank);
2496    return idInit(1,F->rank);
2497  }
2498  #ifdef HAVE_RINGS
2499  if(rField_is_Ring(currRing))
2500  {
2501    ideal sb;
2502    sb = kStd(F, Q, h, w, hilb);
2503    idSkipZeroes(sb);
2504    if(IDELEMS(sb) <= IDELEMS(F))
2505    {
2506        M = idCopy(sb);
2507        idSkipZeroes(M);
2508        return(sb);
2509    }
2510    else
2511    {
2512        M = idCopy(F);
2513        idSkipZeroes(M);
2514        return(sb);
2515    }
2516  }
2517  #endif
2518  ideal r=NULL;
2519  int Kstd1_OldDeg = Kstd1_deg,i;
2520  intvec* temp_w=NULL;
2521  BOOLEAN b=currRing->pLexOrder,toReset=FALSE;
2522  BOOLEAN delete_w=(w==NULL);
2523  BOOLEAN oldDegBound=TEST_OPT_DEGBOUND;
2524  kStrategy strat=new skStrategy;
2525
2526  if(!TEST_OPT_RETURN_SB)
2527     strat->syzComp = syzComp;
2528  if (rField_has_simple_inverse(currRing))
2529    strat->LazyPass=20;
2530  else
2531    strat->LazyPass=2;
2532  strat->LazyDegree = 1;
2533  strat->minim=(reduced % 2)+1;
2534  strat->ak = id_RankFreeModule(F,currRing);
2535  if (delete_w)
2536  {
2537    temp_w=new intvec((strat->ak)+1);
2538    w = &temp_w;
2539  }
2540  if (h==testHomog)
2541  {
2542    if (strat->ak == 0)
2543    {
2544      h = (tHomog)idHomIdeal(F,Q);
2545      w=NULL;
2546    }
2547    else
2548    {
2549      h = (tHomog)idHomModule(F,Q,w);
2550    }
2551  }
2552  if (h==isHomog)
2553  {
2554    if (strat->ak > 0 && (w!=NULL) && (*w!=NULL))
2555    {
2556      kModW = *w;
2557      strat->kModW = *w;
2558      assume(currRing->pFDeg != NULL && currRing->pLDeg != NULL);
2559      strat->pOrigFDeg = currRing->pFDeg;
2560      strat->pOrigLDeg = currRing->pLDeg;
2561      pSetDegProcs(currRing,kModDeg);
2562
2563      toReset = TRUE;
2564      if (reduced>1)
2565      {
2566        Kstd1_OldDeg=Kstd1_deg;
2567        Kstd1_deg = -1;
2568        for (i=IDELEMS(F)-1;i>=0;i--)
2569        {
2570          if ((F->m[i]!=NULL) && (currRing->pFDeg(F->m[i],currRing)>=Kstd1_deg))
2571            Kstd1_deg = currRing->pFDeg(F->m[i],currRing)+1;
2572        }
2573      }
2574    }
2575    currRing->pLexOrder = TRUE;
2576    strat->LazyPass*=2;
2577  }
2578  strat->homog=h;
2579  if (rHasLocalOrMixedOrdering(currRing))
2580  {
2581    if (w!=NULL)
2582      r=mora(F,Q,*w,hilb,strat);
2583    else
2584      r=mora(F,Q,NULL,hilb,strat);
2585  }
2586  else
2587  {
2588    if (w!=NULL)
2589      r=bba(F,Q,*w,hilb,strat);
2590    else
2591      r=bba(F,Q,NULL,hilb,strat);
2592  }
2593#ifdef KDEBUG
2594  {
2595    int i;
2596    for (i=IDELEMS(r)-1; i>=0; i--) pTest(r->m[i]);
2597  }
2598#endif
2599  idSkipZeroes(r);
2600  if (toReset)
2601  {
2602    pRestoreDegProcs(currRing,strat->pOrigFDeg, strat->pOrigLDeg);
2603    kModW = NULL;
2604  }
2605  currRing->pLexOrder = b;
2606  HCord=strat->HCord;
2607  if ((delete_w)&&(temp_w!=NULL)) delete temp_w;
2608  if ((IDELEMS(r)==1) && (r->m[0]!=NULL) && pIsConstant(r->m[0]) && (strat->ak==0))
2609  {
2610    M=idInit(1,F->rank);
2611    M->m[0]=pOne();
2612    //if (strat->ak!=0) { pSetComp(M->m[0],strat->ak); pSetmComp(M->m[0]); }
2613    if (strat->M!=NULL) idDelete(&strat->M);
2614  }
2615  else if (strat->M==NULL)
2616  {
2617    M=idInit(1,F->rank);
2618    Warn("no minimal generating set computed");
2619  }
2620  else
2621  {
2622    idSkipZeroes(strat->M);
2623    M=strat->M;
2624  }
2625  delete(strat);
2626  if (reduced>2)
2627  {
2628    Kstd1_deg=Kstd1_OldDeg;
2629    if (!oldDegBound)
2630      si_opt_1 &= ~Sy_bit(OPT_DEGBOUND);
2631  }
2632  else
2633  {
2634    if (IDELEMS(M)>IDELEMS(r)) {
2635       idDelete(&M);
2636       M=idCopy(r); }
2637  }
2638  return r;
2639}
2640
2641poly kNF(ideal F, ideal Q, poly p,int syzComp, int lazyReduce)
2642{
2643  if (p==NULL)
2644     return NULL;
2645
2646  poly pp = p;
2647
2648#ifdef HAVE_PLURAL
2649  if(rIsSCA(currRing))
2650  {
2651    const unsigned int m_iFirstAltVar = scaFirstAltVar(currRing);
2652    const unsigned int m_iLastAltVar  = scaLastAltVar(currRing);
2653    pp = p_KillSquares(pp, m_iFirstAltVar, m_iLastAltVar, currRing);
2654
2655    if(Q == currRing->qideal)
2656      Q = SCAQuotient(currRing);
2657  }
2658#endif
2659
2660  if ((idIs0(F))&&(Q==NULL))
2661  {
2662#ifdef HAVE_PLURAL
2663    if(p != pp)
2664      return pp;
2665#endif
2666    return pCopy(p); /*F+Q=0*/
2667  }
2668
2669  kStrategy strat=new skStrategy;
2670  strat->syzComp = syzComp;
2671  strat->ak = si_max(id_RankFreeModule(F,currRing),pMaxComp(p));
2672  poly res;
2673
2674  if (rHasLocalOrMixedOrdering(currRing))
2675    res=kNF1(F,Q,pp,strat,lazyReduce);
2676  else
2677    res=kNF2(F,Q,pp,strat,lazyReduce);
2678  delete(strat);
2679
2680#ifdef HAVE_PLURAL
2681  if(pp != p)
2682    p_Delete(&pp, currRing);
2683#endif
2684  return res;
2685}
2686
2687ideal kNF(ideal F, ideal Q, ideal p,int syzComp,int lazyReduce)
2688{
2689  ideal res;
2690  if (TEST_OPT_PROT)
2691  {
2692    Print("(S:%d)",IDELEMS(p));mflush();
2693  }
2694  if (idIs0(p))
2695    return idInit(IDELEMS(p),si_max(p->rank,F->rank));
2696
2697  ideal pp = p;
2698#ifdef HAVE_PLURAL
2699  if(rIsSCA(currRing))
2700  {
2701    const unsigned int m_iFirstAltVar = scaFirstAltVar(currRing);
2702    const unsigned int m_iLastAltVar  = scaLastAltVar(currRing);
2703    pp = id_KillSquares(pp, m_iFirstAltVar, m_iLastAltVar, currRing, false);
2704
2705    if(Q == currRing->qideal)
2706      Q = SCAQuotient(currRing);
2707  }
2708#endif
2709
2710  if ((idIs0(F))&&(Q==NULL))
2711  {
2712#ifdef HAVE_PLURAL
2713    if(p != pp)
2714      return pp;
2715#endif
2716    return idCopy(p); /*F+Q=0*/
2717  }
2718
2719  kStrategy strat=new skStrategy;
2720  strat->syzComp = syzComp;
2721  strat->ak = si_max(id_RankFreeModule(F,currRing),id_RankFreeModule(p,currRing));
2722  if (strat->ak>0) // only for module case, see Tst/Short/bug_reduce.tst
2723  {
2724    strat->ak = si_max(strat->ak,(int)F->rank);
2725  }
2726
2727  if (rHasLocalOrMixedOrdering(currRing))
2728    res=kNF1(F,Q,pp,strat,lazyReduce);
2729  else
2730    res=kNF2(F,Q,pp,strat,lazyReduce);
2731  delete(strat);
2732
2733#ifdef HAVE_PLURAL
2734  if(pp != p)
2735    id_Delete(&pp, currRing);
2736#endif
2737
2738  return res;
2739}
2740
2741poly k_NF (ideal F, ideal Q, poly p,int syzComp, int lazyReduce, const ring _currRing)
2742{
2743  const ring save = currRing;
2744  if( currRing != _currRing ) rChangeCurrRing(_currRing);
2745  poly ret = kNF(F, Q, p, syzComp, lazyReduce);
2746  if( currRing != save )     rChangeCurrRing(save);
2747  return ret;
2748}
2749
2750/*2
2751*interreduces F
2752*/
2753// old version
2754ideal kInterRedOld (ideal F, ideal Q)
2755{
2756  int j;
2757  kStrategy strat = new skStrategy;
2758
2759  ideal tempF = F;
2760  ideal tempQ = Q;
2761
2762#ifdef HAVE_PLURAL
2763  if(rIsSCA(currRing))
2764  {
2765    const unsigned int m_iFirstAltVar = scaFirstAltVar(currRing);
2766    const unsigned int m_iLastAltVar  = scaLastAltVar(currRing);
2767    tempF = id_KillSquares(F, m_iFirstAltVar, m_iLastAltVar, currRing);
2768
2769    // this should be done on the upper level!!! :
2770    //    tempQ = SCAQuotient(currRing);
2771
2772    if(Q == currRing->qideal)
2773      tempQ = SCAQuotient(currRing);
2774  }
2775#endif
2776
2777//  if (TEST_OPT_PROT)
2778//  {
2779//    writeTime("start InterRed:");
2780//    mflush();
2781//  }
2782  //strat->syzComp     = 0;
2783  strat->kHEdgeFound = (currRing->ppNoether) != NULL;
2784  strat->kNoether=pCopy((currRing->ppNoether));
2785  strat->ak = id_RankFreeModule(tempF,currRing);
2786  initBuchMoraCrit(strat);
2787  strat->NotUsedAxis = (BOOLEAN *)omAlloc(((currRing->N)+1)*sizeof(BOOLEAN));
2788  for (j=(currRing->N); j>0; j--) strat->NotUsedAxis[j] = TRUE;
2789  strat->enterS      = enterSBba;
2790  strat->posInT      = posInT17;
2791  strat->initEcart   = initEcartNormal;
2792  strat->sl   = -1;
2793  strat->tl          = -1;
2794  strat->tmax        = setmaxT;
2795  strat->T           = initT();
2796  strat->R           = initR();
2797  strat->sevT        = initsevT();
2798  if (rHasLocalOrMixedOrdering(currRing))   strat->honey = TRUE;
2799  initS(tempF, tempQ, strat);
2800  if (TEST_OPT_REDSB)
2801    strat->noTailReduction=FALSE;
2802  updateS(TRUE,strat);
2803  if (TEST_OPT_REDSB && TEST_OPT_INTSTRATEGY)
2804    completeReduce(strat);
2805  //else if (TEST_OPT_PROT) PrintLn();
2806  pDelete(&strat->kHEdge);
2807  omFreeSize((ADDRESS)strat->T,strat->tmax*sizeof(TObject));
2808  omFreeSize((ADDRESS)strat->ecartS,IDELEMS(strat->Shdl)*sizeof(int));
2809  omFreeSize((ADDRESS)strat->sevS,IDELEMS(strat->Shdl)*sizeof(unsigned long));
2810  omFreeSize((ADDRESS)strat->NotUsedAxis,((currRing->N)+1)*sizeof(BOOLEAN));
2811  omfree(strat->sevT);
2812  omfree(strat->S_2_R);
2813  omfree(strat->R);
2814
2815  if (strat->fromQ)
2816  {
2817    for (j=IDELEMS(strat->Shdl)-1;j>=0;j--)
2818    {
2819      if(strat->fromQ[j]) pDelete(&strat->Shdl->m[j]);
2820    }
2821    omFreeSize((ADDRESS)strat->fromQ,IDELEMS(strat->Shdl)*sizeof(int));
2822  }
2823//  if (TEST_OPT_PROT)
2824//  {
2825//    writeTime("end Interred:");
2826//    mflush();
2827//  }
2828  ideal shdl=strat->Shdl;
2829  idSkipZeroes(shdl);
2830  if (strat->fromQ)
2831  {
2832    strat->fromQ=NULL;
2833    ideal res=kInterRed(shdl,NULL);
2834    idDelete(&shdl);
2835    shdl=res;
2836  }
2837  delete(strat);
2838#ifdef HAVE_PLURAL
2839  if( tempF != F )
2840    id_Delete( &tempF, currRing);
2841#endif
2842  return shdl;
2843}
2844// new version
2845ideal kInterRedBba (ideal F, ideal Q, int &need_retry)
2846{
2847  need_retry=0;
2848  int   red_result = 1;
2849  int   olddeg,reduc;
2850  BOOLEAN withT = FALSE;
2851  // BOOLEAN toReset=FALSE;
2852  kStrategy strat=new skStrategy;
2853  tHomog h;
2854  intvec * w=NULL;
2855
2856  if (rField_has_simple_inverse(currRing))
2857    strat->LazyPass=20;
2858  else
2859    strat->LazyPass=2;
2860  strat->LazyDegree = 1;
2861  strat->ak = id_RankFreeModule(F,currRing);
2862  strat->syzComp = strat->ak;
2863  strat->kModW=kModW=NULL;
2864  strat->kHomW=kHomW=NULL;
2865  if (strat->ak == 0)
2866  {
2867    h = (tHomog)idHomIdeal(F,Q);
2868    w=NULL;
2869  }
2870  else if (!TEST_OPT_DEGBOUND)
2871  {
2872    h = (tHomog)idHomModule(F,Q,&w);
2873  }
2874  if (h==isHomog)
2875  {
2876    if (strat->ak > 0 && (w!=NULL) && (w!=NULL))
2877    {
2878      strat->kModW = kModW = w;
2879      strat->pOrigFDeg = currRing->pFDeg;
2880      strat->pOrigLDeg = currRing->pLDeg;
2881      pSetDegProcs(currRing,kModDeg);
2882      // toReset = TRUE;
2883    }
2884    strat->LazyPass*=2;
2885  }
2886  strat->homog=h;
2887#ifdef KDEBUG
2888  idTest(F);
2889#endif
2890
2891  initBuchMoraCrit(strat); /*set Gebauer, honey, sugarCrit*/
2892  initBuchMoraPos(strat);
2893  initBba(F,strat);
2894  /*set enterS, spSpolyShort, reduce, red, initEcart, initEcartPair*/
2895  strat->posInL=posInL0; /* ord according pComp */
2896
2897  /*Shdl=*/initBuchMora(F, Q, strat);
2898  reduc = olddeg = 0;
2899
2900#ifndef NO_BUCKETS
2901  if (!TEST_OPT_NOT_BUCKETS)
2902    strat->use_buckets = 1;
2903#endif
2904
2905  // redtailBBa against T for inhomogenous input
2906  if (!TEST_OPT_OLDSTD)
2907    withT = ! strat->homog;
2908
2909  // strat->posInT = posInT_pLength;
2910  kTest_TS(strat);
2911
2912#ifdef HAVE_TAIL_RING
2913  kStratInitChangeTailRing(strat);
2914#endif
2915
2916  /* compute------------------------------------------------------- */
2917  while (strat->Ll >= 0)
2918  {
2919    #ifdef KDEBUG
2920      if (TEST_OPT_DEBUG) messageSets(strat);
2921    #endif
2922    if (strat->Ll== 0) strat->interpt=TRUE;
2923    /* picks the last element from the lazyset L */
2924    strat->P = strat->L[strat->Ll];
2925    strat->Ll--;
2926
2927    if (strat->P.p1 == NULL)
2928    {
2929      // for input polys, prepare reduction
2930      strat->P.PrepareRed(strat->use_buckets);
2931    }
2932
2933    if (strat->P.p == NULL && strat->P.t_p == NULL)
2934    {
2935      red_result = 0;
2936    }
2937    else
2938    {
2939      if (TEST_OPT_PROT)
2940        message(strat->P.pFDeg(),
2941                &olddeg,&reduc,strat, red_result);
2942
2943      /* reduction of the element chosen from L */
2944      red_result = strat->red(&strat->P,strat);
2945    }
2946
2947    // reduction to non-zero new poly
2948    if (red_result == 1)
2949    {
2950      /* statistic */
2951      if (TEST_OPT_PROT) PrintS("s");
2952
2953      // get the polynomial (canonicalize bucket, make sure P.p is set)
2954      strat->P.GetP(strat->lmBin);
2955
2956      int pos=posInS(strat,strat->sl,strat->P.p,strat->P.ecart);
2957
2958      // reduce the tail and normalize poly
2959      // in the ring case we cannot expect LC(f) = 1,
2960      // therefore we call pContent instead of pNorm
2961      if ((TEST_OPT_INTSTRATEGY) || (rField_is_Ring(currRing)))
2962      {
2963        strat->P.pCleardenom();
2964        if (0)
2965        //if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
2966        {
2967          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
2968          strat->P.pCleardenom();
2969        }
2970      }
2971      else
2972      {
2973        strat->P.pNorm();
2974        if (0)
2975        //if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
2976          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
2977      }
2978
2979#ifdef KDEBUG
2980      if (TEST_OPT_DEBUG){PrintS("new s:");strat->P.wrp();PrintLn();}
2981#endif
2982
2983      // enter into S, L, and T
2984      if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
2985      {
2986        enterT(strat->P, strat);
2987        // posInS only depends on the leading term
2988        strat->enterS(strat->P, pos, strat, strat->tl);
2989
2990        if (pos<strat->sl)
2991        {
2992          need_retry++;
2993          // move all "larger" elements fromS to L
2994          // remove them from T
2995          int ii=pos+1;
2996          for(;ii<=strat->sl;ii++)
2997          {
2998            LObject h;
2999            memset(&h,0,sizeof(h));
3000            h.tailRing=strat->tailRing;
3001            h.p=strat->S[ii]; strat->S[ii]=NULL;
3002            strat->initEcart(&h);
3003            h.sev=strat->sevS[ii];
3004            int jj=strat->tl;
3005            while (jj>=0)
3006            {
3007              if (strat->T[jj].p==h.p)
3008              {
3009                strat->T[jj].p=NULL;
3010                if (jj<strat->tl)
3011                {
3012                  memmove(&(strat->T[jj]),&(strat->T[jj+1]),
3013                          (strat->tl-jj)*sizeof(strat->T[jj]));
3014                  memmove(&(strat->sevT[jj]),&(strat->sevT[jj+1]),
3015                          (strat->tl-jj)*sizeof(strat->sevT[jj]));
3016                }
3017                strat->tl--;
3018                break;
3019              }
3020              jj--;
3021            }
3022            int lpos=strat->posInL(strat->L,strat->Ll,&h,strat);
3023            enterL(&strat->L,&strat->Ll,&strat->Lmax,h,lpos);
3024            #ifdef KDEBUG
3025            if (TEST_OPT_DEBUG)
3026            {
3027              Print("move S[%d] -> L[%d]: ",ii,pos);
3028              p_wrp(h.p,currRing, strat->tailRing);
3029              PrintLn();
3030            }
3031            #endif
3032          }
3033          if (strat->fromQ!=NULL)
3034          {
3035            for(ii=pos+1;ii<=strat->sl;ii++) strat->fromQ[ii]=0;
3036          }
3037          strat->sl=pos;
3038        }
3039      }
3040      else
3041      {
3042        // clean P
3043      }
3044      if (strat->P.lcm!=NULL)
3045#ifdef HAVE_RINGS
3046        pLmDelete(strat->P.lcm);
3047#else
3048        pLmFree(strat->P.lcm);
3049#endif
3050    }
3051
3052#ifdef KDEBUG
3053    if (TEST_OPT_DEBUG)
3054    {
3055      messageSets(strat);
3056    }
3057    memset(&(strat->P), 0, sizeof(strat->P));
3058#endif
3059    //kTest_TS(strat);: i_r out of sync in kInterRedBba, but not used!
3060  }
3061#ifdef KDEBUG
3062  //if (TEST_OPT_DEBUG) messageSets(strat);
3063#endif
3064  /* complete reduction of the standard basis--------- */
3065
3066  if((need_retry<=0) && (TEST_OPT_REDSB))
3067  {
3068    completeReduce(strat);
3069#ifdef HAVE_TAIL_RING
3070    if (strat->completeReduce_retry)
3071    {
3072      // completeReduce needed larger exponents, retry
3073      // to reduce with S (instead of T)
3074      // and in currRing (instead of strat->tailRing)
3075      cleanT(strat);strat->tailRing=currRing;
3076      int i;
3077      for(i=strat->sl;i>=0;i--) strat->S_2_R[i]=-1;
3078      completeReduce(strat);
3079    }
3080#endif
3081  }
3082  else if (TEST_OPT_PROT) PrintLn();
3083
3084  /* release temp data-------------------------------- */
3085  exitBuchMora(strat);
3086//  if (TEST_OPT_WEIGHTM)
3087//  {
3088//    pRestoreDegProcs(currRing,strat->pOrigFDeg, strat->pOrigLDeg);
3089//    if (ecartWeights)
3090//    {
3091//      omFreeSize((ADDRESS)ecartWeights,((currRing->N)+1)*sizeof(short));
3092//      ecartWeights=NULL;
3093//    }
3094//  }
3095  //if (TEST_OPT_PROT) messageStat(0/*hilbcount*/,strat);
3096  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
3097  ideal res=strat->Shdl;
3098  strat->Shdl=NULL;
3099  delete strat;
3100  if (w!=NULL) delete w;
3101  return res;
3102}
3103ideal kInterRed (ideal F, ideal Q)
3104{
3105#ifdef HAVE_PLURAL
3106  if(rIsPluralRing(currRing)) return kInterRedOld(F,Q);
3107#endif
3108  if ((rHasLocalOrMixedOrdering(currRing))|| (rField_is_numeric(currRing))
3109  #ifdef HAVE_RINGS
3110  ||(rField_is_Ring(currRing))
3111  #endif
3112  )
3113    return kInterRedOld(F,Q);
3114
3115    //return kInterRedOld(F,Q);
3116
3117  BITSET save1;
3118  SI_SAVE_OPT1(save1);
3119  //si_opt_1|=Sy_bit(OPT_NOT_SUGAR);
3120  si_opt_1|=Sy_bit(OPT_REDTHROUGH);
3121  //si_opt_1&= ~Sy_bit(OPT_REDTAIL);
3122  //si_opt_1&= ~Sy_bit(OPT_REDSB);
3123  //extern char * showOption() ;
3124  //Print("%s\n",showOption());
3125
3126  int need_retry;
3127  int counter=3;
3128  ideal res, res1;
3129  int elems;
3130  ideal null=NULL;
3131  if ((Q==NULL) || (!TEST_OPT_REDSB))
3132  {
3133    elems=idElem(F);
3134    res=kInterRedBba(F,Q,need_retry);
3135  }
3136  else
3137  {
3138    ideal FF=idSimpleAdd(F,Q);
3139    res=kInterRedBba(FF,NULL,need_retry);
3140    idDelete(&FF);
3141    null=idInit(1,1);
3142    if (need_retry)
3143      res1=kNF(null,Q,res,0,KSTD_NF_LAZY);
3144    else
3145      res1=kNF(null,Q,res);
3146    idDelete(&res);
3147    res=res1;
3148    need_retry=1;
3149  }
3150  if (idElem(res)<=1) need_retry=0;
3151  while (need_retry && (counter>0))
3152  {
3153    #ifdef KDEBUG
3154    if (TEST_OPT_DEBUG) { Print("retry counter %d\n",counter); }
3155    #endif
3156    res1=kInterRedBba(res,Q,need_retry);
3157    int new_elems=idElem(res1);
3158    counter -= (new_elems >= elems);
3159    elems = new_elems;
3160    idDelete(&res);
3161    if (idElem(res1)<=1) need_retry=0;
3162    if ((Q!=NULL) && (TEST_OPT_REDSB))
3163    {
3164      if (need_retry)
3165        res=kNF(null,Q,res1,0,KSTD_NF_LAZY);
3166      else
3167        res=kNF(null,Q,res1);
3168      idDelete(&res1);
3169    }
3170    else
3171      res = res1;
3172    if (idElem(res)<=1) need_retry=0;
3173  }
3174  if (null!=NULL) idDelete(&null);
3175  SI_RESTORE_OPT1(save1);
3176  idSkipZeroes(res);
3177  return res;
3178}
3179
3180// returns TRUE if mora should use buckets, false otherwise
3181static BOOLEAN kMoraUseBucket(kStrategy strat)
3182{
3183#ifdef MORA_USE_BUCKETS
3184  if (TEST_OPT_NOT_BUCKETS)
3185    return FALSE;
3186  if (strat->red == redFirst)
3187  {
3188#ifdef NO_LDEG
3189    if (strat->syzComp==0)
3190      return TRUE;
3191#else
3192    if ((strat->homog || strat->honey) && (strat->syzComp==0))
3193      return TRUE;
3194#endif
3195  }
3196  else
3197  {
3198    assume(strat->red == redEcart || strat->red == redRiloc);
3199    if (strat->honey && (strat->syzComp==0))
3200      return TRUE;
3201  }
3202#endif
3203  return FALSE;
3204}
Note: See TracBrowser for help on using the repository browser.