source: git/kernel/GBEngine/kstd1.cc @ 3cc89c

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