source: git/kernel/kstd1.cc @ f41347f

spielwiese
Last change on this file since f41347f was f41347f, checked in by Oliver Wienand <wienand@…>, 15 years ago
Fehler in lokalen Ordnungen und KoefRingen git-svn-id: file:///usr/local/Singular/svn/trunk@11215 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 58.2 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: kstd1.cc,v 1.41 2008-12-04 14:13:37 wienand Exp $ */
5/*
6* ABSTRACT:
7*/
8
9// define if buckets should be used
10#define MORA_USE_BUCKETS
11
12// define if tailrings should be used
13#define HAVE_TAIL_RING
14
15#include "mod2.h"
16#include "structs.h"
17#include "omalloc.h"
18#include "kutil.h"
19#include "kInline.cc"
20#include "polys.h"
21#include "febase.h"
22#include "kstd1.h"
23#include "khstd.h"
24#include "stairc.h"
25#include "weight.h"
26//#include "cntrlc.h"
27#include "intvec.h"
28#include "ideals.h"
29//#include "../Singular/ipid.h"
30#include "timer.h"
31
32//#include "ipprint.h"
33
34#ifdef HAVE_PLURAL
35#include "sca.h"
36#endif
37
38
39/* the list of all options which give a warning by test */
40BITSET kOptions=Sy_bit(OPT_PROT)           /*  0 */
41                |Sy_bit(OPT_REDSB)         /*  1 */
42                |Sy_bit(OPT_NOT_SUGAR)     /*  3 */
43                |Sy_bit(OPT_INTERRUPT)     /*  4 */
44                |Sy_bit(OPT_SUGARCRIT)     /*  5 */
45                |Sy_bit(OPT_REDTHROUGH)
46                |Sy_bit(OPT_OLDSTD)
47                |Sy_bit(OPT_FASTHC)        /* 10 */
48                |Sy_bit(OPT_KEEPVARS)      /* 21 */
49                |Sy_bit(OPT_INTSTRATEGY)   /* 26 */
50                |Sy_bit(OPT_INFREDTAIL)    /* 28 */
51                |Sy_bit(OPT_NOTREGULARITY) /* 30 */
52                |Sy_bit(OPT_WEIGHTM);      /* 31 */
53
54/* the list of all options which may be used by option and test */
55BITSET validOpts=Sy_bit(0)
56                |Sy_bit(1)
57                |Sy_bit(2) // obachman 10/00: replaced by notBucket
58                |Sy_bit(3)
59                |Sy_bit(4)
60                |Sy_bit(5)
61                |Sy_bit(6)
62//                |Sy_bit(7) obachman 11/00 tossed: 12/00 used for redThrough
63  |Sy_bit(OPT_REDTHROUGH)
64//                |Sy_bit(8) obachman 11/00 tossed
65                |Sy_bit(9)
66                |Sy_bit(10)
67                |Sy_bit(11)
68                |Sy_bit(12)
69                |Sy_bit(13)
70                |Sy_bit(14)
71                |Sy_bit(15)
72                |Sy_bit(16)
73                |Sy_bit(17)
74                |Sy_bit(18)
75                |Sy_bit(19)
76//                |Sy_bit(20) obachman 11/00 tossed: 12/00 used for redOldStd
77  |Sy_bit(OPT_OLDSTD)
78                |Sy_bit(21)
79                |Sy_bit(22)
80                /*|Sy_bit(23)*/
81                /*|Sy_bit(24)*/
82                |Sy_bit(OPT_REDTAIL)
83                |Sy_bit(OPT_INTSTRATEGY)
84                |Sy_bit(27)
85                |Sy_bit(28)
86                |Sy_bit(29)
87                |Sy_bit(30)
88                |Sy_bit(31);
89
90//static BOOLEAN posInLOldFlag;
91           /*FALSE, if posInL == posInL10*/
92// returns TRUE if mora should use buckets, false otherwise
93static BOOLEAN kMoraUseBucket(kStrategy strat);
94
95static void kOptimizeLDeg(pLDegProc ldeg, kStrategy strat)
96{
97//  if (strat->ak == 0 && !rIsSyzIndexRing(currRing))
98    strat->length_pLength = TRUE;
99//  else
100//    strat->length_pLength = FALSE;
101
102  if ((ldeg == pLDeg0c /*&& !rIsSyzIndexRing(currRing)*/) ||
103      (ldeg == pLDeg0 && strat->ak == 0))
104  {
105    strat->LDegLast = TRUE;
106  }
107  else
108  {
109    strat->LDegLast = FALSE;
110  }
111}
112
113
114static int doRed (LObject* h, TObject* with,BOOLEAN intoT,kStrategy strat)
115{
116  poly hp;
117  int ret;
118#if KDEBUG > 0
119  kTest_L(h);
120  kTest_T(with);
121#endif
122  // Hmmm ... why do we do this -- polys from T should already be normalized
123  if (!TEST_OPT_INTSTRATEGY)
124    with->pNorm();
125#ifdef KDEBUG
126  if (TEST_OPT_DEBUG)
127  {
128    PrintS("reduce ");h->wrp();PrintS(" with ");with->wrp();PrintLn();
129  }
130#endif
131  if (intoT)
132  {
133    // need to do it exacly like this: otherwise
134    // we might get errors
135    LObject L= *h;
136    L.Copy();
137    h->GetP();
138    h->SetLength(strat->length_pLength);
139    ret = ksReducePoly(&L, with, strat->kNoetherTail(), NULL, strat);
140    if (ret)
141    {
142      if (ret < 0) return ret;
143      if (h->tailRing != strat->tailRing)
144        h->ShallowCopyDelete(strat->tailRing,
145                             pGetShallowCopyDeleteProc(h->tailRing,
146                                                       strat->tailRing));
147    }
148    enterT(*h,strat);
149    *h = L;
150  }
151  else
152    ret = ksReducePoly(h, with, strat->kNoetherTail(), NULL, strat);
153#ifdef KDEBUG
154  if (TEST_OPT_DEBUG)
155  {
156    PrintS("to ");h->wrp();PrintLn();
157  }
158#endif
159  return ret;
160}
161
162int redEcart (LObject* h,kStrategy strat)
163{
164  poly pi;
165  int i,at,reddeg,d,ei,li,ii;
166  int j = 0;
167  int pass = 0;
168
169  d = h->GetpFDeg()+ h->ecart;
170  reddeg = strat->LazyDegree+d;
171  h->SetShortExpVector();
172  loop
173  {
174    j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
175    if (j < 0)
176    {
177      if (strat->honey) h->SetLength(strat->length_pLength);
178      return 1;
179    }
180
181    ei = strat->T[j].ecart;
182    ii = j;
183
184    if (ei > h->ecart && ii < strat->tl)
185    {
186      li = strat->T[j].length;
187      // the polynomial to reduce with (up to the moment) is;
188      // pi with ecart ei and length li
189      // look for one with smaller ecart
190      i = j;
191      loop
192      {
193        /*- takes the first possible with respect to ecart -*/
194        i++;
195#if 1
196        if (i > strat->tl) break;
197        if ((strat->T[i].ecart < ei || (strat->T[i].ecart == ei &&
198                                        strat->T[i].length < li))
199            &&
200            p_LmShortDivisibleBy(strat->T[i].GetLmTailRing(), strat->sevT[i], h->GetLmTailRing(), ~h->sev, strat->tailRing))
201#else
202          j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h, i);
203        if (j < 0) break;
204        i = j;
205        if (strat->T[i].ecart < ei || (strat->T[i].ecart == ei &&
206                                        strat->T[i].length < li))
207#endif
208        {
209          // the polynomial to reduce with is now
210          ii = i;
211          ei = strat->T[i].ecart;
212          if (ei <= h->ecart) break;
213          li = strat->T[i].length;
214        }
215      }
216    }
217
218    // end of search: have to reduce with pi
219    if (ei > h->ecart)
220    {
221      // It is not possible to reduce h with smaller ecart;
222      // if possible h goes to the lazy-set L,i.e
223      // if its position in L would be not the last one
224      strat->fromT = TRUE;
225      if (!K_TEST_OPT_REDTHROUGH && strat->Ll >= 0) /*- L is not empty -*/
226      {
227        h->SetLmCurrRing();
228        if (strat->honey && strat->posInLDependsOnLength)
229          h->SetLength(strat->length_pLength);
230        assume(h->FDeg == h->pFDeg());
231        at = strat->posInL(strat->L,strat->Ll,h,strat);
232        if (at <= strat->Ll)
233        {
234          /*- h will not become the next element to reduce -*/
235          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
236#ifdef KDEBUG
237          if (TEST_OPT_DEBUG) Print(" ecart too big; -> L%d\n",at);
238#endif
239          h->Clear();
240          strat->fromT = FALSE;
241          return -1;
242        }
243      }
244    }
245
246    // now we finally can reduce
247    doRed(h,&(strat->T[ii]),strat->fromT,strat);
248    strat->fromT=FALSE;
249
250    // are we done ???
251    if (h->IsNull())
252    {
253      if (h->lcm!=NULL) pLmFree(h->lcm);
254      h->Clear();
255      return 0;
256    }
257
258    // NO!
259    h->SetShortExpVector();
260    h->SetpFDeg();
261    if (strat->honey)
262    {
263      if (ei <= h->ecart)
264        h->ecart = d-h->GetpFDeg();
265      else
266        h->ecart = d-h->GetpFDeg()+ei-h->ecart;
267    }
268    else
269      // this has the side effect of setting h->length
270      h->ecart = h->pLDeg(strat->LDegLast) - h->GetpFDeg();
271#if 0
272    if (strat->syzComp!=0)
273    {
274      if ((strat->syzComp>0) && (h->Comp() > strat->syzComp))
275      {
276        assume(h->MinComp() > strat->syzComp);
277        if (strat->honey) h->SetLength();
278#ifdef KDEBUG
279        if (TEST_OPT_DEBUG) PrintS(" > syzComp\n");
280#endif
281        return -2;
282      }
283    }
284#endif
285    /*- try to reduce the s-polynomial -*/
286    pass++;
287    d = h->GetpFDeg()+h->ecart;
288    /*
289     *test whether the polynomial should go to the lazyset L
290     *-if the degree jumps
291     *-if the number of pre-defined reductions jumps
292     */
293    if (!K_TEST_OPT_REDTHROUGH && (strat->Ll >= 0)
294        && ((d >= reddeg) || (pass > strat->LazyPass)))
295    {
296      h->SetLmCurrRing();
297      if (strat->honey && strat->posInLDependsOnLength)
298        h->SetLength(strat->length_pLength);
299      assume(h->FDeg == h->pFDeg());
300      at = strat->posInL(strat->L,strat->Ll,h,strat);
301      if (at <= strat->Ll)
302      {
303        int dummy=strat->sl;
304        if (kFindDivisibleByInS(strat, &dummy, h) < 0)
305        {
306          if (strat->honey && !strat->posInLDependsOnLength)
307            h->SetLength(strat->length_pLength);
308          return 1;
309        }
310        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
311#ifdef KDEBUG
312        if (TEST_OPT_DEBUG) Print(" degree jumped; ->L%d\n",at);
313#endif
314        h->Clear();
315        return -1;
316      }
317    }
318    else if ((TEST_OPT_PROT) && (strat->Ll < 0) && (d >= reddeg))
319    {
320      Print(".%d",d);mflush();
321      reddeg = d+1;
322    }
323  }
324}
325
326/*2
327*reduces h with elements from T choosing  the first possible
328* element in t with respect to the given pDivisibleBy
329*/
330int redFirst (LObject* h,kStrategy strat)
331{
332  if (h->IsNull()) return 0;
333
334  int at, reddeg,d;
335  int pass = 0;
336  int j = 0;
337
338  if (! strat->homog)
339  {
340    d = h->GetpFDeg() + h->ecart;
341    reddeg = strat->LazyDegree+d;
342  }
343  h->SetShortExpVector();
344  loop
345  {
346    j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
347    if (j < 0)
348    {
349      h->SetDegStuffReturnLDeg(strat->LDegLast);
350      return 1;
351    }
352
353    if (!TEST_OPT_INTSTRATEGY)
354      strat->T[j].pNorm();
355#ifdef KDEBUG
356    if (TEST_OPT_DEBUG)
357    {
358      PrintS("reduce ");
359      h->wrp();
360      PrintS(" with ");
361      strat->T[j].wrp();
362    }
363#endif
364    ksReducePoly(h, &(strat->T[j]), strat->kNoetherTail(), NULL, strat);
365#ifdef KDEBUG
366    if (TEST_OPT_DEBUG)
367    {
368      PrintS(" to ");
369      wrp(h->p);
370      PrintLn();
371    }
372#endif
373    if (h->IsNull())
374    {
375      if (h->lcm!=NULL) pLmFree(h->lcm);
376      h->Clear();
377      return 0;
378    }
379    h->SetShortExpVector();
380
381#if 0
382    if ((strat->syzComp!=0) && !strat->honey)
383    {
384      if ((strat->syzComp>0) &&
385          (h->Comp() > strat->syzComp))
386      {
387        assume(h->MinComp() > strat->syzComp);
388#ifdef KDEBUG
389        if (TEST_OPT_DEBUG) PrintS(" > syzComp\n");
390#endif
391        if (strat->homog)
392          h->SetDegStuffReturnLDeg(strat->LDegLast);
393        return -2;
394      }
395    }
396#endif
397    if (!strat->homog)
398    {
399      if (!K_TEST_OPT_OLDSTD && strat->honey)
400      {
401        h->SetpFDeg();
402        if (strat->T[j].ecart <= h->ecart)
403          h->ecart = d - h->GetpFDeg();
404        else
405          h->ecart = d - h->GetpFDeg() + strat->T[j].ecart - h->ecart;
406
407        d = h->GetpFDeg() + h->ecart;
408      }
409      else
410        d = h->SetDegStuffReturnLDeg(strat->LDegLast);
411      /*- try to reduce the s-polynomial -*/
412      pass++;
413      /*
414       *test whether the polynomial should go to the lazyset L
415       *-if the degree jumps
416       *-if the number of pre-defined reductions jumps
417       */
418      if (!K_TEST_OPT_REDTHROUGH && (strat->Ll >= 0)
419          && ((d >= reddeg) || (pass > strat->LazyPass)))
420      {
421        h->SetLmCurrRing();
422        if (strat->posInLDependsOnLength)
423          h->SetLength(strat->length_pLength);
424        at = strat->posInL(strat->L,strat->Ll,h,strat);
425        if (at <= strat->Ll)
426        {
427          int dummy=strat->sl;
428          if (kFindDivisibleByInS(strat,&dummy, h) < 0)
429            return 1;
430          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
431#ifdef KDEBUG
432          if (TEST_OPT_DEBUG) Print(" degree jumped; ->L%d\n",at);
433#endif
434          h->Clear();
435          return -1;
436        }
437      }
438      if ((TEST_OPT_PROT) && (strat->Ll < 0) && (d >= reddeg))
439      {
440        reddeg = d+1;
441        Print(".%d",d);mflush();
442      }
443    }
444  }
445}
446
447/*2
448* reduces h with elements from T choosing first possible
449* element in T with respect to the given ecart
450* used for computing normal forms outside kStd
451*/
452static poly redMoraNF (poly h,kStrategy strat, int flag)
453{
454  LObject H;
455  H.p = h;
456  int j = 0;
457  int z = 10;
458  int o = H.SetpFDeg();
459  H.ecart = pLDeg(H.p,&H.length,currRing)-o;
460  if ((flag & 2) == 0) cancelunit(&H,TRUE);
461  H.sev = pGetShortExpVector(H.p);
462  unsigned long not_sev = ~ H.sev;
463  loop
464  {
465    if (j > strat->tl)
466    {
467      return H.p;
468    }
469    if (TEST_V_DEG_STOP)
470    {
471      if (kModDeg(H.p)>Kstd1_deg) pDeleteLm(&H.p);
472      if (H.p==NULL) return NULL;
473    }
474    if (p_LmShortDivisibleBy(strat->T[j].GetLmTailRing(), strat->sevT[j], H.GetLmTailRing(), not_sev, strat->tailRing))
475    {
476      //if (strat->interpt) test_int_std(strat->kIdeal);
477      /*- remember the found T-poly -*/
478      poly pi = strat->T[j].p;
479      int ei = strat->T[j].ecart;
480      int li = strat->T[j].length;
481      int ii = j;
482      /*
483      * the polynomial to reduce with (up to the moment) is;
484      * pi with ecart ei and length li
485      */
486      loop
487      {
488        /*- look for a better one with respect to ecart -*/
489        /*- stop, if the ecart is small enough (<=ecart(H)) -*/
490        j++;
491        if (j > strat->tl) break;
492        if (ei <= H.ecart) break;
493        if (((strat->T[j].ecart < ei)
494          || ((strat->T[j].ecart == ei)
495        && (strat->T[j].length < li)))
496        && pLmShortDivisibleBy(strat->T[j].p,strat->sevT[j], H.p, not_sev))
497        {
498          /*
499          * the polynomial to reduce with is now;
500          */
501          pi = strat->T[j].p;
502          ei = strat->T[j].ecart;
503          li = strat->T[j].length;
504          ii = j;
505        }
506      }
507      /*
508      * end of search: have to reduce with pi
509      */
510      z++;
511      if (z>10)
512      {
513        pNormalize(H.p);
514        z=0;
515      }
516      if ((ei > H.ecart) && (!strat->kHEdgeFound))
517      {
518        /*
519        * It is not possible to reduce h with smaller ecart;
520        * we have to reduce with bad ecart: H has to enter in T
521        */
522        doRed(&H,&(strat->T[ii]),TRUE,strat);
523        if (H.p == NULL)
524          return NULL;
525      }
526      else
527      {
528        /*
529        * we reduce with good ecart, h need not to be put to T
530        */
531        doRed(&H,&(strat->T[ii]),FALSE,strat);
532        if (H.p == NULL)
533          return NULL;
534      }
535      /*- try to reduce the s-polynomial -*/
536      o = H.SetpFDeg();
537      if ((flag &2 ) == 0) cancelunit(&H,TRUE);
538      H.ecart = pLDeg(H.p,&(H.length),currRing)-o;
539      j = 0;
540      H.sev = pGetShortExpVector(H.p);
541      not_sev = ~ H.sev;
542    }
543    else
544    {
545      j++;
546    }
547  }
548}
549
550/*2
551*reorders  L with respect to posInL
552*/
553void reorderL(kStrategy strat)
554{
555  int i,j,at;
556  LObject p;
557
558  for (i=1; i<=strat->Ll; i++)
559  {
560    at = strat->posInL(strat->L,i-1,&(strat->L[i]),strat);
561    if (at != i)
562    {
563      p = strat->L[i];
564      for (j=i-1; j>=at; j--) strat->L[j+1] = strat->L[j];
565      strat->L[at] = p;
566    }
567  }
568}
569
570/*2
571*reorders  T with respect to length
572*/
573void reorderT(kStrategy strat)
574{
575  int i,j,at;
576  TObject p;
577  unsigned long sev;
578
579
580  for (i=1; i<=strat->tl; i++)
581  {
582    if (strat->T[i-1].length > strat->T[i].length)
583    {
584      p = strat->T[i];
585      sev = strat->sevT[i];
586      at = i-1;
587      loop
588      {
589        at--;
590        if (at < 0) break;
591        if (strat->T[i].length > strat->T[at].length) break;
592      }
593      for (j = i-1; j>at; j--)
594      {
595        strat->T[j+1]=strat->T[j];
596        strat->sevT[j+1]=strat->sevT[j];
597        strat->R[strat->T[j+1].i_r] = &(strat->T[j+1]);
598      }
599      strat->T[at+1]=p;
600      strat->sevT[at+1] = sev;
601      strat->R[p.i_r] = &(strat->T[at+1]);
602    }
603  }
604}
605
606/*2
607*looks whether exactly pVariables-1 axis are used
608*returns last != 0 in this case
609*last is the (first) unused axis
610*/
611void missingAxis (int* last,kStrategy strat)
612{
613  int   i = 0;
614  int   k = 0;
615
616  *last = 0;
617  if (!currRing->MixedOrder)
618  {
619    loop
620    {
621      i++;
622      if (i > pVariables) break;
623      if (strat->NotUsedAxis[i])
624      {
625        *last = i;
626        k++;
627      }
628      if (k>1)
629      {
630        *last = 0;
631        break;
632      }
633    }
634  }
635}
636
637/*2
638*last is the only non used axis, it looks
639*for a monomial in p being a pure power of this
640*variable and returns TRUE in this case
641*(*length) gives the length between the pure power and the leading term
642*(should be minimal)
643*/
644BOOLEAN hasPurePower (const poly p,int last, int *length,kStrategy strat)
645{
646  poly h;
647  int i;
648
649  if (pNext(p) == strat->tail)
650    return FALSE;
651  pp_Test(p, currRing, strat->tailRing);
652  if (strat->ak <= 0 || p_MinComp(p, currRing, strat->tailRing) == strat->ak)
653  {
654    i = p_IsPurePower(p, currRing);
655    if (i == last)
656    {
657      *length = 0;
658      return TRUE;
659    }
660    *length = 1;
661    h = pNext(p);
662    while (h != NULL)
663    {
664      i = p_IsPurePower(h, strat->tailRing);
665      if (i==last) return TRUE;
666      (*length)++;
667      pIter(h);
668    }
669  }
670  return FALSE;
671}
672
673BOOLEAN hasPurePower (LObject *L,int last, int *length,kStrategy strat)
674{
675  if (L->bucket != NULL)
676  {
677    poly p = L->CanonicalizeP();
678    BOOLEAN ret = hasPurePower(p, last, length, strat);
679    pNext(p) = NULL;
680    return ret;
681  }
682  else
683  {
684    return hasPurePower(L->p, last, length, strat);
685  }
686}
687
688/*2
689* looks up the position of polynomial p in L
690* in the case of looking for the pure powers
691*/
692int posInL10 (const LSet set,const int length, LObject* p,const kStrategy strat)
693{
694  int j,dp,dL;
695
696  if (length<0) return 0;
697  if (hasPurePower(p,strat->lastAxis,&dp,strat))
698  {
699    int op= p->GetpFDeg() +p->ecart;
700    for (j=length; j>=0; j--)
701    {
702      if (!hasPurePower(&(set[j]),strat->lastAxis,&dL,strat))
703        return j+1;
704      if (dp < dL)
705        return j+1;
706      if ((dp == dL)
707          && (set[j].GetpFDeg()+set[j].ecart >= op))
708        return j+1;
709    }
710  }
711  j=length;
712  loop
713  {
714    if (j<0) break;
715    if (!hasPurePower(&(set[j]),strat->lastAxis,&dL,strat)) break;
716    j--;
717  }
718  return strat->posInLOld(set,j,p,strat);
719}
720
721
722/*2
723* computes the s-polynomials L[ ].p in L
724*/
725void updateL(kStrategy strat)
726{
727  LObject p;
728  int dL;
729  int j=strat->Ll;
730  loop
731  {
732    if (j<0) break;
733    if (hasPurePower(&(strat->L[j]),strat->lastAxis,&dL,strat))
734    {
735      p=strat->L[strat->Ll];
736      strat->L[strat->Ll]=strat->L[j];
737      strat->L[j]=p;
738      break;
739    }
740    j--;
741  }
742  if (j<0)
743  {
744    j=strat->Ll;
745    loop
746    {
747      if (j<0) break;
748      if (pNext(strat->L[j].p) == strat->tail)
749      {
750#ifdef HAVE_RINGS
751        if (rField_is_Ring(currRing))
752          pLmDelete(strat->L[j].p);    /*deletes the short spoly and computes*/
753        else
754#else
755          pLmFree(strat->L[j].p);    /*deletes the short spoly and computes*/
756#endif
757        strat->L[j].p = NULL;
758        poly m1 = NULL, m2 = NULL;
759        // check that spoly creation is ok
760        while (strat->tailRing != currRing &&
761               !kCheckSpolyCreation(&(strat->L[j]), strat, m1, m2))
762        {
763          assume(m1 == NULL && m2 == NULL);
764          // if not, change to a ring where exponents are at least
765          // large enough
766          kStratChangeTailRing(strat);
767        }
768        /* create the real one */
769        ksCreateSpoly(&(strat->L[j]), strat->kNoetherTail(), FALSE,
770                      strat->tailRing, m1, m2, strat->R);
771
772        strat->L[j].SetLmCurrRing();
773        if (!strat->honey)
774          strat->initEcart(&strat->L[j]);
775        else
776          strat->L[j].SetLength(strat->length_pLength);
777
778        BOOLEAN pp = hasPurePower(&(strat->L[j]),strat->lastAxis,&dL,strat);
779
780        if (strat->use_buckets) strat->L[j].PrepareRed(TRUE);
781
782        if (pp)
783        {
784          p=strat->L[strat->Ll];
785          strat->L[strat->Ll]=strat->L[j];
786          strat->L[j]=p;
787          break;
788        }
789      }
790      j--;
791    }
792  }
793}
794
795/*2
796* computes the s-polynomials L[ ].p in L and
797* cuts elements in L above noether
798*/
799void updateLHC(kStrategy strat)
800{
801  int i = 0;
802  kTest_TS(strat);
803  while (i <= strat->Ll)
804  {
805    if (pNext(strat->L[i].p) == strat->tail)
806    {
807       /*- deletes the int spoly and computes -*/
808      if (pLmCmp(strat->L[i].p,strat->kNoether) == -1)
809      {
810        pLmFree(strat->L[i].p);
811        strat->L[i].p = NULL;
812      }
813      else
814      {
815        pLmFree(strat->L[i].p);
816        strat->L[i].p = NULL;
817        poly m1 = NULL, m2 = NULL;
818        // check that spoly creation is ok
819        while (strat->tailRing != currRing &&
820               !kCheckSpolyCreation(&(strat->L[i]), strat, m1, m2))
821        {
822          assume(m1 == NULL && m2 == NULL);
823          // if not, change to a ring where exponents are at least
824          // large enough
825          kStratChangeTailRing(strat);
826        }
827        /* create the real one */
828        ksCreateSpoly(&(strat->L[i]), strat->kNoetherTail(), FALSE,
829                      strat->tailRing, m1, m2, strat->R);
830        if (! strat->L[i].IsNull())
831        {
832          strat->L[i].SetLmCurrRing();
833          strat->L[i].SetpFDeg();
834          strat->L[i].ecart
835            = strat->L[i].pLDeg(strat->LDegLast) - strat->L[i].GetpFDeg();
836          if (strat->use_buckets) strat->L[i].PrepareRed(TRUE);
837        }
838      }
839    }
840    else
841      deleteHC(&(strat->L[i]), strat);
842   if (strat->L[i].IsNull())
843      deleteInL(strat->L,&strat->Ll,i,strat);
844    else
845    {
846#ifdef KDEBUG
847      kTest_L(&(strat->L[i]), strat->tailRing, TRUE, i, strat->T, strat->tl);
848#endif
849      i++;
850    }
851  }
852  kTest_TS(strat);
853}
854
855/*2
856* cuts in T above strat->kNoether and tries to cancel a unit
857*/
858void updateT(kStrategy strat)
859{
860  int i = 0;
861  LObject p;
862
863  while (i <= strat->tl)
864  {
865    p = strat->T[i];
866    deleteHC(&p,strat, TRUE);
867    /*- tries to cancel a unit: -*/
868    cancelunit(&p);
869    if (p.p != strat->T[i].p)
870    {
871      strat->sevT[i] = pGetShortExpVector(p.p);
872      p.SetpFDeg();
873    }
874    strat->T[i] = p;
875    i++;
876  }
877}
878
879/*2
880* arranges red, pos and T if strat->kHEdgeFound (first time)
881*/
882void firstUpdate(kStrategy strat)
883{
884  if (strat->update)
885  {
886    kTest_TS(strat);
887    strat->update = (strat->tl == -1);
888    if (TEST_OPT_WEIGHTM)
889    {
890      pRestoreDegProcs(pFDegOld, pLDegOld);
891      if (strat->tailRing != currRing)
892      {
893        strat->tailRing->pFDeg = strat->pOrigFDeg_TailRing;
894        strat->tailRing->pLDeg = strat->pOrigLDeg_TailRing;
895      }
896      int i;
897      for (i=strat->Ll; i>=0; i--)
898      {
899        strat->L[i].SetpFDeg();
900      }
901      for (i=strat->tl; i>=0; i--)
902      {
903        strat->T[i].SetpFDeg();
904      }
905      if (ecartWeights)
906      {
907        omFreeSize((ADDRESS)ecartWeights,(pVariables+1)*sizeof(short));
908        ecartWeights=NULL;
909      }
910    }
911    if (TEST_OPT_FASTHC)
912    {
913      strat->posInL = strat->posInLOld;
914      strat->lastAxis = 0;
915    }
916    if (BTEST1(27))
917      return;
918    strat->red = redFirst;
919    strat->use_buckets = kMoraUseBucket(strat);
920    updateT(strat);
921    strat->posInT = posInT2;
922    reorderT(strat);
923  }
924  kTest_TS(strat);
925}
926
927/*2
928*-puts p to the standardbasis s at position at
929*-reduces the tail of p if TEST_OPT_REDTAIL
930*-tries to cancel a unit
931*-HEckeTest
932*  if TRUE
933*  - decides about reduction-strategies
934*  - computes noether
935*  - stops computation if BTEST1(27)
936*  - cuts the tails of the polynomials
937*    in s,t and the elements in L above noether
938*    and cancels units if possible
939*  - reorders s,L
940*/
941void enterSMora (LObject p,int atS,kStrategy strat, int atR = -1)
942{
943  int i;
944  enterSBba(p, atS, strat, atR);
945  #ifdef KDEBUG
946  if (TEST_OPT_DEBUG)
947  {
948    Print("new s%d:",atS);
949    wrp(p.p);
950    PrintLn();
951  }
952  #endif
953  return;
954  if ((!strat->kHEdgeFound) || (strat->kNoether!=NULL)) HEckeTest(p.p,strat);
955  if (strat->kHEdgeFound)
956  {
957    if (newHEdge(strat->S,strat))
958    {
959      firstUpdate(strat);
960      if (BTEST1(27))
961        return;
962      /*- cuts elements in L above noether and reorders L -*/
963      updateLHC(strat);
964      /*- reorders L with respect to posInL -*/
965      reorderL(strat);
966    }
967  }
968  else if (strat->kNoether!=NULL)
969    strat->kHEdgeFound = TRUE;
970  else if (TEST_OPT_FASTHC)
971  {
972    if (strat->posInLOldFlag)
973    {
974      missingAxis(&strat->lastAxis,strat);
975      if (strat->lastAxis)
976      {
977        strat->posInLOld = strat->posInL;
978        strat->posInLOldFlag = FALSE;
979        strat->posInL = posInL10;
980        strat->posInLDependsOnLength = TRUE;
981        updateL(strat);
982        reorderL(strat);
983      }
984    }
985    else if (strat->lastAxis)
986      updateL(strat);
987  }
988}
989
990/*2
991*-puts p to the standardbasis s at position at
992*-HEckeTest
993*  if TRUE
994*  - computes noether
995*/
996void enterSMoraNF (LObject p, int atS,kStrategy strat, int atR = -1)
997{
998  int i;
999
1000  enterSBba(p, atS, strat, atR);
1001  if ((!strat->kHEdgeFound) || (strat->kNoether!=NULL)) HEckeTest(p.p,strat);
1002  if (strat->kHEdgeFound)
1003    newHEdge(strat->S,strat);
1004  else if (strat->kNoether!=NULL)
1005    strat->kHEdgeFound = TRUE;
1006}
1007
1008void initBba(ideal F,kStrategy strat)
1009{
1010  int i;
1011  idhdl h;
1012 /* setting global variables ------------------- */
1013  strat->enterS = enterSBba;
1014    strat->red = redHoney;
1015  if (strat->honey)
1016    strat->red = redHoney;
1017  else if (pLexOrder && !strat->homog)
1018    strat->red = redLazy;
1019  else
1020  {
1021    strat->LazyPass *=4;
1022    strat->red = redHomog;
1023  }
1024#ifdef HAVE_RINGS  //TODO Oliver
1025  if (rField_is_Ring(currRing))
1026  {
1027    strat->red = redRing;
1028  }
1029#endif
1030  if (pLexOrder && strat->honey)
1031    strat->initEcart = initEcartNormal;
1032  else
1033    strat->initEcart = initEcartBBA;
1034  if (strat->honey)
1035    strat->initEcartPair = initEcartPairMora;
1036  else
1037    strat->initEcartPair = initEcartPairBba;
1038  strat->kIdeal = NULL;
1039  //if (strat->ak==0) strat->kIdeal->rtyp=IDEAL_CMD;
1040  //else              strat->kIdeal->rtyp=MODUL_CMD;
1041  //strat->kIdeal->data=(void *)strat->Shdl;
1042  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1043  {
1044    //interred  machen   Aenderung
1045    pFDegOld=pFDeg;
1046    pLDegOld=pLDeg;
1047    //h=ggetid("ecart");
1048    //if ((h!=NULL) /*&& (IDTYP(h)==INTVEC_CMD)*/)
1049    //{
1050    //  ecartWeights=iv2array(IDINTVEC(h));
1051    //}
1052    //else
1053    {
1054      ecartWeights=(short *)omAlloc((pVariables+1)*sizeof(short));
1055      /*uses automatic computation of the ecartWeights to set them*/
1056      kEcartWeights(F->m,IDELEMS(F)-1,ecartWeights);
1057    }
1058    pRestoreDegProcs(totaldegreeWecart, maxdegreeWecart);
1059    if (TEST_OPT_PROT)
1060    {
1061      for(i=1; i<=pVariables; i++)
1062        Print(" %d",ecartWeights[i]);
1063      PrintLn();
1064      mflush();
1065    }
1066  }
1067}
1068
1069void initMora(ideal F,kStrategy strat)
1070{
1071  int i,j;
1072  idhdl h;
1073
1074  strat->NotUsedAxis = (BOOLEAN *)omAlloc((pVariables+1)*sizeof(BOOLEAN));
1075  for (j=pVariables; j>0; j--) strat->NotUsedAxis[j] = TRUE;
1076  strat->enterS = enterSMora;
1077  strat->initEcartPair = initEcartPairMora; /*- ecart approximation -*/
1078  strat->posInLOld = strat->posInL;
1079  strat->posInLOldFlag = TRUE;
1080  strat->initEcart = initEcartNormal;
1081  strat->kHEdgeFound = ppNoether != NULL;
1082  if ( strat->kHEdgeFound )
1083     strat->kNoether = pCopy(ppNoether);
1084  else if (strat->kHEdgeFound || strat->homog)
1085    strat->red = redFirst;  /*take the first possible in T*/
1086  else
1087    strat->red = redEcart;/*take the first possible in under ecart-restriction*/
1088  if (strat->kHEdgeFound)
1089  {
1090    strat->HCord = pFDeg(ppNoether,currRing)+1;
1091    strat->posInT = posInT2;
1092  }
1093  else
1094  {
1095    strat->HCord = 32000;/*- very large -*/
1096  }
1097  /*reads the ecartWeights used for Graebes method from the
1098   *intvec ecart and set ecartWeights
1099   */
1100  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1101  {
1102    //interred  machen   Aenderung
1103    pFDegOld=pFDeg;
1104    pLDegOld=pLDeg;
1105    //h=ggetid("ecart");
1106    //if ((h!=NULL) /*&& (IDTYP(h)==INTVEC_CMD)*/)
1107    //{
1108    //  ecartWeights=iv2array(IDINTVEC(h));
1109    //}
1110    //else
1111    {
1112      ecartWeights=(short *)omAlloc((pVariables+1)*sizeof(short));
1113      /*uses automatic computation of the ecartWeights to set them*/
1114      kEcartWeights(F->m,IDELEMS(F)-1,ecartWeights);
1115    }
1116
1117    pSetDegProcs(totaldegreeWecart, maxdegreeWecart);
1118    if (TEST_OPT_PROT)
1119    {
1120      for(i=1; i<=pVariables; i++)
1121        Print(" %d",ecartWeights[i]);
1122      PrintLn();
1123      mflush();
1124    }
1125  }
1126  kOptimizeLDeg(pLDeg, strat);
1127}
1128
1129#ifdef HAVE_ASSUME
1130static int mora_count = 0;
1131static int mora_loop_count;
1132#endif
1133
1134ideal mora (ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat)
1135{
1136#ifdef HAVE_ASSUME
1137  mora_count++;
1138  mora_loop_count = 0;
1139#endif
1140#ifdef KDEBUG
1141  om_Opts.MinTrack = 5;
1142#endif
1143  int srmax;
1144  int lrmax = 0;
1145  int olddeg = 0;
1146  int reduc = 0;
1147  int red_result = 1;
1148  int hilbeledeg=1,hilbcount=0;
1149
1150  strat->update = TRUE;
1151  /*- setting global variables ------------------- -*/
1152  initBuchMoraCrit(strat);
1153  initHilbCrit(F,Q,&hilb,strat);
1154  initMora(F,strat);
1155  initBuchMoraPos(strat);
1156  /*Shdl=*/initBuchMora(F,Q,strat);
1157  if (TEST_OPT_FASTHC) missingAxis(&strat->lastAxis,strat);
1158  /*updateS in initBuchMora has Hecketest
1159  * and could have put strat->kHEdgdeFound FALSE*/
1160  if (ppNoether!=NULL)
1161  {
1162    strat->kHEdgeFound = TRUE;
1163  }
1164  if (strat->kHEdgeFound && strat->update)
1165  {
1166    firstUpdate(strat);
1167    updateLHC(strat);
1168    reorderL(strat);
1169  }
1170  if (TEST_OPT_FASTHC && (strat->lastAxis) && strat->posInLOldFlag)
1171  {
1172    strat->posInLOld = strat->posInL;
1173    strat->posInLOldFlag = FALSE;
1174    strat->posInL = posInL10;
1175    updateL(strat);
1176    reorderL(strat);
1177  }
1178  srmax = strat->sl;
1179  kTest_TS(strat);
1180  strat->use_buckets = kMoraUseBucket(strat);
1181  /*- compute-------------------------------------------*/
1182
1183#ifdef HAVE_TAIL_RING
1184//  if (strat->homog && strat->red == redFirst)
1185    kStratInitChangeTailRing(strat);
1186#endif
1187
1188  while (strat->Ll >= 0)
1189  {
1190#ifdef HAVE_ASSUME
1191    mora_loop_count++;
1192#endif
1193    if (lrmax< strat->Ll) lrmax=strat->Ll; /*stat*/
1194    //test_int_std(strat->kIdeal);
1195    #ifdef KDEBUG
1196    if (TEST_OPT_DEBUG) messageSets(strat);
1197    #endif
1198    if (TEST_OPT_DEGBOUND
1199    && (strat->L[strat->Ll].ecart+strat->L[strat->Ll].GetpFDeg()> Kstd1_deg))
1200    {
1201      /*
1202      * stops computation if
1203      * - 24 (degBound)
1204      *   && upper degree is bigger than Kstd1_deg
1205      */
1206      while ((strat->Ll >= 0)
1207        && (strat->L[strat->Ll].p1!=NULL) && (strat->L[strat->Ll].p2!=NULL)
1208        && (strat->L[strat->Ll].ecart+strat->L[strat->Ll].GetpFDeg()> Kstd1_deg)
1209      )
1210      {
1211        deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
1212        //if (TEST_OPT_PROT)
1213        //{
1214        //   PrintS("D"); mflush();
1215        //}
1216      }
1217      if (strat->Ll<0) break;
1218      else strat->noClearS=TRUE;
1219    }
1220    strat->P = strat->L[strat->Ll];/*- picks the last element from the lazyset L -*/
1221    if (strat->Ll==0) strat->interpt=TRUE;
1222    strat->Ll--;
1223
1224    // create the real Spoly
1225    if (pNext(strat->P.p) == strat->tail)
1226    {
1227      /*- deletes the short spoly and computes -*/
1228#ifdef HAVE_RINGS
1229      if (rField_is_Ring(currRing))
1230        pLmDelete(strat->P.p);
1231      else
1232#endif
1233      pLmFree(strat->P.p);
1234      strat->P.p = NULL;
1235      poly m1 = NULL, m2 = NULL;
1236      // check that spoly creation is ok
1237      while (strat->tailRing != currRing &&
1238             !kCheckSpolyCreation(&(strat->P), strat, m1, m2))
1239      {
1240        assume(m1 == NULL && m2 == NULL);
1241        // if not, change to a ring where exponents are large enough
1242        kStratChangeTailRing(strat);
1243      }
1244      /* create the real one */
1245      ksCreateSpoly(&(strat->P), strat->kNoetherTail(), strat->use_buckets,
1246                    strat->tailRing, m1, m2, strat->R);
1247      if (!strat->use_buckets)
1248        strat->P.SetLength(strat->length_pLength);
1249    }
1250    else if (strat->P.p1 == NULL)
1251    {
1252      // for input polys, prepare reduction (buckets !)
1253      strat->P.SetLength(strat->length_pLength);
1254      strat->P.PrepareRed(strat->use_buckets);
1255    }
1256
1257    if (!strat->P.IsNull())
1258    {
1259      // might be NULL from noether !!!
1260      if (TEST_OPT_PROT)
1261        message(strat->P.ecart+strat->P.GetpFDeg(),&olddeg,&reduc,strat, red_result);
1262      // reduce
1263      red_result = strat->red(&strat->P,strat);
1264    }
1265
1266    if (! strat->P.IsNull())
1267    {
1268      strat->P.GetP();
1269      // statistics
1270      if (TEST_OPT_PROT) PrintS("s");
1271      // normalization
1272      if (!TEST_OPT_INTSTRATEGY)
1273        strat->P.pNorm();
1274      // tailreduction
1275      strat->P.p = redtail(&(strat->P),strat->sl,strat);
1276      // set ecart -- might have changed because of tail reductions
1277      if ((!strat->noTailReduction) && (!strat->honey))
1278        strat->initEcart(&strat->P);
1279      // cancel unit
1280      cancelunit(&strat->P);
1281      // for char 0, clear denominators
1282      if (TEST_OPT_INTSTRATEGY)
1283        strat->P.pCleardenom();
1284
1285      // put in T
1286      enterT(strat->P,strat);
1287      // build new pairs
1288#ifdef HAVE_RINGS
1289      if (rField_is_Ring(currRing))
1290        superenterpairs(strat->P.p,strat->sl,strat->P.ecart,0,strat, strat->tl);
1291      else
1292#endif
1293      enterpairs(strat->P.p,strat->sl,strat->P.ecart,0,strat, strat->tl);
1294      // put in S
1295      strat->enterS(strat->P,
1296                    posInS(strat,strat->sl,strat->P.p, strat->P.ecart),
1297                    strat, strat->tl);
1298
1299      // apply hilbert criterion
1300      if (hilb!=NULL) khCheck(Q,w,hilb,hilbeledeg,hilbcount,strat);
1301
1302      // clear strat->P
1303      if (strat->P.lcm!=NULL) 
1304#ifdef HAVE_RINGS
1305        pLmDelete(strat->P.lcm);
1306#else
1307        pLmFree(strat->P.lcm);
1308#endif
1309      strat->P.lcm=NULL;
1310#ifdef KDEBUG
1311      // make sure kTest_TS does not complain about strat->P
1312      memset(&strat->P,0,sizeof(strat->P));
1313#endif
1314      if (strat->sl>srmax) srmax = strat->sl; /*stat.*/
1315      if (strat->Ll>lrmax) lrmax = strat->Ll;
1316    }
1317    if (strat->kHEdgeFound)
1318    {
1319      if ((BTEST1(27))
1320      || ((TEST_OPT_MULTBOUND) && (scMult0Int((strat->Shdl)) < mu)))
1321      {
1322        // obachman: is this still used ???
1323        /*
1324        * stops computation if strat->kHEdgeFound and
1325        * - 27 (finiteDeterminacyTest)
1326        * or
1327        * - 23
1328        *   (multBound)
1329        *   && multiplicity of the ideal is smaller then a predefined number mu
1330        */
1331        while (strat->Ll >= 0) deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
1332      }
1333    }
1334    kTest_TS(strat);
1335  }
1336  /*- complete reduction of the standard basis------------------------ -*/
1337  if (TEST_OPT_REDSB) completeReduce(strat);
1338  else if (TEST_OPT_PROT) PrintLn();
1339  /*- release temp data------------------------------- -*/
1340  exitBuchMora(strat);
1341  /*- polynomials used for HECKE: HC, noether -*/
1342  if (BTEST1(27))
1343  {
1344    if (strat->kHEdge!=NULL)
1345      Kstd1_mu=pFDeg(strat->kHEdge,currRing);
1346    else
1347      Kstd1_mu=-1;
1348  }
1349  pDelete(&strat->kHEdge);
1350  strat->update = TRUE; //???
1351  strat->lastAxis = 0; //???
1352  pDelete(&strat->kNoether);
1353  omFreeSize((ADDRESS)strat->NotUsedAxis,(pVariables+1)*sizeof(BOOLEAN));
1354  if (TEST_OPT_PROT) messageStat(srmax,lrmax,hilbcount,strat);
1355  if (TEST_OPT_WEIGHTM)
1356  {
1357    pRestoreDegProcs(pFDegOld, pLDegOld);
1358    if (ecartWeights)
1359    {
1360      omFreeSize((ADDRESS)ecartWeights,(pVariables+1)*sizeof(short));
1361      ecartWeights=NULL;
1362    }
1363  }
1364  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
1365  idTest(strat->Shdl);
1366  return (strat->Shdl);
1367}
1368
1369poly kNF1 (ideal F,ideal Q,poly q, kStrategy strat, int lazyReduce)
1370{
1371  poly   p;
1372  int   i;
1373  int   j;
1374  int   o;
1375  LObject   h;
1376  BITSET save_test=test;
1377
1378  if ((idIs0(F))&&(Q==NULL))
1379    return pCopy(q); /*F=0*/
1380  strat->ak = si_max(idRankFreeModule(F),pMaxComp(q));
1381  /*- creating temp data structures------------------- -*/
1382  strat->kHEdgeFound = ppNoether != NULL;
1383  strat->kNoether    = pCopy(ppNoether);
1384  test|=Sy_bit(OPT_REDTAIL);
1385  test&=~Sy_bit(OPT_INTSTRATEGY);
1386  if (TEST_OPT_STAIRCASEBOUND
1387  && (! TEST_V_DEG_STOP)
1388  && (0<Kstd1_deg)
1389  && ((!strat->kHEdgeFound)
1390    ||(TEST_OPT_DEGBOUND && (pWTotaldegree(strat->kNoether)<Kstd1_deg))))
1391  {
1392    pDelete(&strat->kNoether);
1393    strat->kNoether=pOne();
1394    pSetExp(strat->kNoether,1, Kstd1_deg+1);
1395    pSetm(strat->kNoether);
1396    strat->kHEdgeFound=TRUE;
1397  }
1398  initBuchMoraCrit(strat);
1399  initBuchMoraPos(strat);
1400  initMora(F,strat);
1401  strat->enterS = enterSMoraNF;
1402  /*- set T -*/
1403  strat->tl = -1;
1404  strat->tmax = setmaxT;
1405  strat->T = initT();
1406  strat->R = initR();
1407  strat->sevT = initsevT();
1408  /*- set S -*/
1409  strat->sl = -1;
1410  /*- init local data struct.-------------------------- -*/
1411  /*Shdl=*/initS(F,Q,strat);
1412  if ((strat->ak!=0)
1413  && (strat->kHEdgeFound))
1414  {
1415    if (strat->ak!=1)
1416    {
1417      pSetComp(strat->kNoether,1);
1418      pSetmComp(strat->kNoether);
1419      poly p=pHead(strat->kNoether);
1420      pSetComp(p,strat->ak);
1421      pSetmComp(p);
1422      p=pAdd(strat->kNoether,p);
1423      strat->kNoether=pNext(p);
1424      p_LmFree(p,currRing);
1425    }
1426  }
1427  if ((lazyReduce & KSTD_NF_LAZY)==0)
1428  {
1429    for (i=strat->sl; i>=0; i--)
1430      pNorm(strat->S[i]);
1431  }
1432  /*- puts the elements of S also to T -*/
1433  for (i=0; i<=strat->sl; i++)
1434  {
1435    h.p = strat->S[i];
1436    h.ecart = strat->ecartS[i];
1437    if (strat->sevS[i] == 0) strat->sevS[i] = pGetShortExpVector(h.p);
1438    else assume(strat->sevS[i] == pGetShortExpVector(h.p));
1439    h.length = pLength(h.p);
1440    h.sev = strat->sevS[i];
1441    h.SetpFDeg();
1442    enterT(h,strat);
1443  }
1444  /*- compute------------------------------------------- -*/
1445  p = pCopy(q);
1446  deleteHC(&p,&o,&j,strat);
1447  if (TEST_OPT_PROT) { PrintS("r"); mflush(); }
1448  if (p!=NULL) p = redMoraNF(p,strat, lazyReduce & KSTD_NF_ECART);
1449  if ((p!=NULL)&&((lazyReduce & KSTD_NF_LAZY)==0))
1450  {
1451    if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
1452    p = redtail(p,strat->sl,strat);
1453  }
1454  /*- release temp data------------------------------- -*/
1455  cleanT(strat);
1456  omFreeSize((ADDRESS)strat->T,strat->tmax*sizeof(TObject));
1457  omFreeSize((ADDRESS)strat->ecartS,IDELEMS(strat->Shdl)*sizeof(int));
1458  omFreeSize((ADDRESS)strat->sevS,IDELEMS(strat->Shdl)*sizeof(unsigned long));
1459  omFreeSize((ADDRESS)strat->NotUsedAxis,(pVariables+1)*sizeof(BOOLEAN));
1460  omfree(strat->sevT);
1461  omfree(strat->S_2_R);
1462  omfree(strat->R);
1463
1464  if ((Q!=NULL)&&(strat->fromQ!=NULL))
1465  {
1466    i=((IDELEMS(Q)+IDELEMS(F)+15)/16)*16;
1467    omFreeSize((ADDRESS)strat->fromQ,i*sizeof(int));
1468    strat->fromQ=NULL;
1469  }
1470  pDelete(&strat->kHEdge);
1471  pDelete(&strat->kNoether);
1472  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1473  {
1474    pRestoreDegProcs(pFDegOld, pLDegOld);
1475    if (ecartWeights)
1476    {
1477      omFreeSize((ADDRESS *)&ecartWeights,(pVariables+1)*sizeof(short));
1478      ecartWeights=NULL;
1479    }
1480  }
1481  idDelete(&strat->Shdl);
1482  test=save_test;
1483  if (TEST_OPT_PROT) PrintLn();
1484  return p;
1485}
1486
1487ideal kNF1 (ideal F,ideal Q,ideal q, kStrategy strat, int lazyReduce)
1488{
1489  poly   p;
1490  int   i;
1491  int   j;
1492  int   o;
1493  LObject   h;
1494  ideal res;
1495  BITSET save_test=test;
1496
1497  if (idIs0(q)) return idInit(IDELEMS(q),si_max(q->rank,F->rank));
1498  if ((idIs0(F))&&(Q==NULL))
1499    return idCopy(q); /*F=0*/
1500  strat->ak = si_max(idRankFreeModule(F),idRankFreeModule(q));
1501  /*- creating temp data structures------------------- -*/
1502  strat->kHEdgeFound = ppNoether != NULL;
1503  strat->kNoether=pCopy(ppNoether);
1504  test|=Sy_bit(OPT_REDTAIL);
1505  if (TEST_OPT_STAIRCASEBOUND
1506  && (0<Kstd1_deg)
1507  && ((!strat->kHEdgeFound)
1508    ||(TEST_OPT_DEGBOUND && (pWTotaldegree(strat->kNoether)<Kstd1_deg))))
1509  {
1510    pDelete(&strat->kNoether);
1511    strat->kNoether=pOne();
1512    pSetExp(strat->kNoether,1, Kstd1_deg+1);
1513    pSetm(strat->kNoether);
1514    strat->kHEdgeFound=TRUE;
1515  }
1516  initBuchMoraCrit(strat);
1517  initBuchMoraPos(strat);
1518  initMora(F,strat);
1519  strat->enterS = enterSMoraNF;
1520  /*- set T -*/
1521  strat->tl = -1;
1522  strat->tmax = setmaxT;
1523  strat->T = initT();
1524  strat->R = initR();
1525  strat->sevT = initsevT();
1526  /*- set S -*/
1527  strat->sl = -1;
1528  /*- init local data struct.-------------------------- -*/
1529  /*Shdl=*/initS(F,Q,strat);
1530  if ((strat->ak!=0)
1531  && (strat->kHEdgeFound))
1532  {
1533    if (strat->ak!=1)
1534    {
1535      pSetComp(strat->kNoether,1);
1536      pSetmComp(strat->kNoether);
1537      poly p=pHead(strat->kNoether);
1538      pSetComp(p,strat->ak);
1539      pSetmComp(p);
1540      p=pAdd(strat->kNoether,p);
1541      strat->kNoether=pNext(p);
1542      p_LmFree(p,currRing);
1543    }
1544  }
1545  if (TEST_OPT_INTSTRATEGY && ((lazyReduce & KSTD_NF_LAZY)==0))
1546  {
1547    for (i=strat->sl; i>=0; i--)
1548      pNorm(strat->S[i]);
1549  }
1550  /*- compute------------------------------------------- -*/
1551  res=idInit(IDELEMS(q),strat->ak);
1552  for (i=0; i<IDELEMS(q); i++)
1553  {
1554    if (q->m[i]!=NULL)
1555    {
1556      p = pCopy(q->m[i]);
1557      deleteHC(&p,&o,&j,strat);
1558      if (p!=NULL)
1559      {
1560        /*- puts the elements of S also to T -*/
1561        for (j=0; j<=strat->sl; j++)
1562        {
1563          h.p = strat->S[j];
1564          h.ecart = strat->ecartS[j];
1565          h.pLength = h.length = pLength(h.p);
1566          if (strat->sevS[j] == 0) strat->sevS[j] = pGetShortExpVector(h.p);
1567          else assume(strat->sevS[j] == pGetShortExpVector(h.p));
1568          h.sev = strat->sevS[j];
1569          h.SetpFDeg();
1570          enterT(h,strat);
1571        }
1572        if (TEST_OPT_PROT) { PrintS("r"); mflush(); }
1573        p = redMoraNF(p,strat, lazyReduce & KSTD_NF_ECART);
1574        if ((p!=NULL)&&((lazyReduce & KSTD_NF_LAZY)==0))
1575        {
1576          if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
1577          p = redtail(p,strat->sl,strat);
1578        }
1579        cleanT(strat);
1580      }
1581      res->m[i]=p;
1582    }
1583    //else
1584    //  res->m[i]=NULL;
1585  }
1586  /*- release temp data------------------------------- -*/
1587  omFreeSize((ADDRESS)strat->T,strat->tmax*sizeof(TObject));
1588  omFreeSize((ADDRESS)strat->ecartS,IDELEMS(strat->Shdl)*sizeof(int));
1589  omFreeSize((ADDRESS)strat->sevS,IDELEMS(strat->Shdl)*sizeof(unsigned long));
1590  omFreeSize((ADDRESS)strat->NotUsedAxis,(pVariables+1)*sizeof(BOOLEAN));
1591  omfree(strat->sevT);
1592  omfree(strat->S_2_R);
1593  omfree(strat->R);
1594  if ((Q!=NULL)&&(strat->fromQ!=NULL))
1595  {
1596    i=((IDELEMS(Q)+IDELEMS(F)+15)/16)*16;
1597    omFreeSize((ADDRESS)strat->fromQ,i*sizeof(int));
1598    strat->fromQ=NULL;
1599  }
1600  pDelete(&strat->kHEdge);
1601  pDelete(&strat->kNoether);
1602  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1603  {
1604    pFDeg=pFDegOld;
1605    pLDeg=pLDegOld;
1606    if (ecartWeights)
1607    {
1608      omFreeSize((ADDRESS *)&ecartWeights,(pVariables+1)*sizeof(short));
1609      ecartWeights=NULL;
1610    }
1611  }
1612  idDelete(&strat->Shdl);
1613  test=save_test;
1614  if (TEST_OPT_PROT) PrintLn();
1615  return res;
1616}
1617
1618pFDegProc pFDegOld;
1619pLDegProc pLDegOld;
1620intvec * kModW, * kHomW;
1621
1622long kModDeg(poly p, ring r)
1623{
1624  long o=pWDegree(p, r);
1625  long i=p_GetComp(p, r);
1626  if (i==0) return o;
1627  assume((i>0) && (i<=kModW->length()));
1628  return o+(*kModW)[i-1];
1629}
1630long kHomModDeg(poly p, ring r)
1631{
1632  int i;
1633  long j=0;
1634
1635  for (i=r->N;i>0;i--)
1636    j+=p_GetExp(p,i,r)*(*kHomW)[i-1];
1637  if (kModW == NULL) return j;
1638  i = p_GetComp(p,r);
1639  if (i==0) return j;
1640  return j+(*kModW)[i-1];
1641}
1642
1643ideal kStd(ideal F, ideal Q, tHomog h,intvec ** w, intvec *hilb,int syzComp,
1644          int newIdeal, intvec *vw)
1645{
1646  ideal r;
1647  BOOLEAN b=pLexOrder,toReset=FALSE;
1648  BOOLEAN delete_w=(w==NULL);
1649  kStrategy strat=new skStrategy;
1650
1651  if(!TEST_OPT_RETURN_SB)
1652    strat->syzComp = syzComp;
1653  if (TEST_OPT_SB_1)
1654    strat->newIdeal = newIdeal;
1655  if (rField_has_simple_inverse())
1656    strat->LazyPass=20;
1657  else
1658    strat->LazyPass=2;
1659  strat->LazyDegree = 1;
1660  strat->ak = idRankFreeModule(F);
1661  strat->kModW=kModW=NULL;
1662  strat->kHomW=kHomW=NULL;
1663  if (vw != NULL)
1664  {
1665    pLexOrder=FALSE;
1666    strat->kHomW=kHomW=vw;
1667    pFDegOld = pFDeg;
1668    pLDegOld = pLDeg;
1669    pSetDegProcs(kHomModDeg);
1670    toReset = TRUE;
1671  }
1672  if (h==testHomog)
1673  {
1674    if (strat->ak == 0)
1675    {
1676      h = (tHomog)idHomIdeal(F,Q);
1677      w=NULL;
1678    }
1679    else if (!TEST_OPT_DEGBOUND)
1680    {
1681      h = (tHomog)idHomModule(F,Q,w);
1682    }
1683  }
1684  pLexOrder=b;
1685  if (h==isHomog)
1686  {
1687    if (strat->ak > 0 && (w!=NULL) && (*w!=NULL))
1688    {
1689      strat->kModW = kModW = *w;
1690      if (vw == NULL)
1691      {
1692        pFDegOld = pFDeg;
1693        pLDegOld = pLDeg;
1694        pSetDegProcs(kModDeg);
1695        toReset = TRUE;
1696      }
1697    }
1698    pLexOrder = TRUE;
1699    if (hilb==NULL) strat->LazyPass*=2;
1700  }
1701  strat->homog=h;
1702#ifdef KDEBUG
1703  idTest(F);
1704#endif
1705#ifdef HAVE_PLURAL
1706  if (rIsPluralRing(currRing))
1707  {
1708    if (w!=NULL)
1709      r = nc_GB(F, Q, *w, hilb, strat);
1710    else
1711      r = nc_GB(F, Q, NULL, hilb, strat);
1712  }
1713  else
1714#endif
1715  {
1716    if (pOrdSgn==-1)
1717    {
1718      if (w!=NULL)
1719        r=mora(F,Q,*w,hilb,strat);
1720      else
1721        r=mora(F,Q,NULL,hilb,strat);
1722    }
1723    else
1724    {
1725      if (w!=NULL)
1726        r=bba(F,Q,*w,hilb,strat);
1727      else
1728        r=bba(F,Q,NULL,hilb,strat);
1729    }
1730  }
1731#ifdef KDEBUG
1732  idTest(r);
1733#endif
1734  if (toReset)
1735  {
1736    kModW = NULL;
1737    pRestoreDegProcs(pFDegOld, pLDegOld);
1738  }
1739  pLexOrder = b;
1740//Print("%d reductions canceled \n",strat->cel);
1741  HCord=strat->HCord;
1742  delete(strat);
1743  if ((delete_w)&&(w!=NULL)&&(*w!=NULL)) delete *w;
1744  return r;
1745}
1746
1747#ifdef HAVE_SHIFTBBA
1748ideal kStdShift(ideal F, ideal Q, tHomog h,intvec ** w, intvec *hilb,int syzComp,
1749                int newIdeal, intvec *vw, int uptodeg, int lV)
1750{
1751  ideal r;
1752  BOOLEAN b=pLexOrder,toReset=FALSE;
1753  BOOLEAN delete_w=(w==NULL);
1754  kStrategy strat=new skStrategy;
1755
1756  if(!TEST_OPT_RETURN_SB)
1757    strat->syzComp = syzComp;
1758  if (TEST_OPT_SB_1)
1759    strat->newIdeal = newIdeal;
1760  if (rField_has_simple_inverse())
1761    strat->LazyPass=20;
1762  else
1763    strat->LazyPass=2;
1764  strat->LazyDegree = 1;
1765  strat->ak = idRankFreeModule(F);
1766  strat->kModW=kModW=NULL;
1767  strat->kHomW=kHomW=NULL;
1768  if (vw != NULL)
1769  {
1770    pLexOrder=FALSE;
1771    strat->kHomW=kHomW=vw;
1772    pFDegOld = pFDeg;
1773    pLDegOld = pLDeg;
1774    pSetDegProcs(kHomModDeg);
1775    toReset = TRUE;
1776  }
1777  if (h==testHomog)
1778  {
1779    if (strat->ak == 0)
1780    {
1781      h = (tHomog)idHomIdeal(F,Q);
1782      w=NULL;
1783    }
1784    else if (!TEST_OPT_DEGBOUND)
1785    {
1786      h = (tHomog)idHomModule(F,Q,w);
1787    }
1788  }
1789  pLexOrder=b;
1790  if (h==isHomog)
1791  {
1792    if (strat->ak > 0 && (w!=NULL) && (*w!=NULL))
1793    {
1794      strat->kModW = kModW = *w;
1795      if (vw == NULL)
1796      {
1797        pFDegOld = pFDeg;
1798        pLDegOld = pLDeg;
1799        pSetDegProcs(kModDeg);
1800        toReset = TRUE;
1801      }
1802    }
1803    pLexOrder = TRUE;
1804    if (hilb==NULL) strat->LazyPass*=2;
1805  }
1806  strat->homog=h;
1807#ifdef KDEBUG
1808  idTest(F);
1809#endif
1810  if (pOrdSgn==-1)
1811  {
1812    /* error: no local ord yet with shifts */
1813    Print("No local ordering possible for shifts");
1814    return(NULL);
1815  }
1816  else
1817  {
1818    /* global ordering */
1819    if (w!=NULL)
1820      r=bbaShift(F,Q,*w,hilb,strat,uptodeg,lV);
1821    else
1822      r=bbaShift(F,Q,NULL,hilb,strat,uptodeg,lV);
1823  }
1824#ifdef KDEBUG
1825  idTest(r);
1826#endif
1827  if (toReset)
1828  {
1829    kModW = NULL;
1830    pRestoreDegProcs(pFDegOld, pLDegOld);
1831  }
1832  pLexOrder = b;
1833//Print("%d reductions canceled \n",strat->cel);
1834  HCord=strat->HCord;
1835  delete(strat);
1836  if ((delete_w)&&(w!=NULL)&&(*w!=NULL)) delete *w;
1837  return r;
1838}
1839#endif
1840
1841//##############################################################
1842//##############################################################
1843//##############################################################
1844//##############################################################
1845//##############################################################
1846
1847ideal kMin_std(ideal F, ideal Q, tHomog h,intvec ** w, ideal &M, intvec *hilb,
1848              int syzComp, int reduced)
1849{
1850  ideal r=NULL;
1851  int Kstd1_OldDeg = Kstd1_deg,i;
1852  intvec* temp_w=NULL;
1853  BOOLEAN b=pLexOrder,toReset=FALSE;
1854  BOOLEAN delete_w=(w==NULL);
1855  BOOLEAN oldDegBound=TEST_OPT_DEGBOUND;
1856  kStrategy strat=new skStrategy;
1857
1858  if(!TEST_OPT_RETURN_SB)
1859     strat->syzComp = syzComp;
1860  if (rField_has_simple_inverse())
1861    strat->LazyPass=20;
1862  else
1863    strat->LazyPass=2;
1864  strat->LazyDegree = 1;
1865  strat->minim=(reduced % 2)+1;
1866  strat->ak = idRankFreeModule(F);
1867  if (delete_w)
1868  {
1869    temp_w=new intvec((strat->ak)+1);
1870    w = &temp_w;
1871  }
1872  if ((h==testHomog)
1873  )
1874  {
1875    if (strat->ak == 0)
1876    {
1877      h = (tHomog)idHomIdeal(F,Q);
1878      w=NULL;
1879    }
1880    else
1881    {
1882      h = (tHomog)idHomModule(F,Q,w);
1883    }
1884  }
1885  if (h==isHomog)
1886  {
1887    if (strat->ak > 0 && (w!=NULL) && (*w!=NULL))
1888    {
1889      kModW = *w;
1890      strat->kModW = *w;
1891      assume(pFDeg != NULL && pLDeg != NULL);
1892      pFDegOld = pFDeg;
1893      pLDegOld = pLDeg;
1894      pSetDegProcs(kModDeg);
1895
1896      toReset = TRUE;
1897      if (reduced>1)
1898      {
1899        Kstd1_OldDeg=Kstd1_deg;
1900        Kstd1_deg = -1;
1901        for (i=IDELEMS(F)-1;i>=0;i--)
1902        {
1903          if ((F->m[i]!=NULL) && (pFDeg(F->m[i],currRing)>=Kstd1_deg))
1904            Kstd1_deg = pFDeg(F->m[i],currRing)+1;
1905        }
1906      }
1907    }
1908    pLexOrder = TRUE;
1909    strat->LazyPass*=2;
1910  }
1911  strat->homog=h;
1912  if (pOrdSgn==-1)
1913  {
1914    if (w!=NULL)
1915      r=mora(F,Q,*w,hilb,strat);
1916    else
1917      r=mora(F,Q,NULL,hilb,strat);
1918  }
1919  else
1920  {
1921    if (w!=NULL)
1922      r=bba(F,Q,*w,hilb,strat);
1923    else
1924      r=bba(F,Q,NULL,hilb,strat);
1925  }
1926#ifdef KDEBUG
1927  {
1928    int i;
1929    for (i=IDELEMS(r)-1; i>=0; i--) pTest(r->m[i]);
1930  }
1931#endif
1932  idSkipZeroes(r);
1933  if (toReset)
1934  {
1935    pRestoreDegProcs(pFDegOld, pLDegOld);
1936    kModW = NULL;
1937  }
1938  pLexOrder = b;
1939  HCord=strat->HCord;
1940  if ((delete_w)&&(temp_w!=NULL)) delete temp_w;
1941  if ((IDELEMS(r)==1) && (r->m[0]!=NULL) && pIsConstant(r->m[0]) && (strat->ak==0))
1942  {
1943    M=idInit(1,F->rank);
1944    M->m[0]=pOne();
1945    //if (strat->ak!=0) { pSetComp(M->m[0],strat->ak); pSetmComp(M->m[0]); }
1946    if (strat->M!=NULL) idDelete(&strat->M);
1947  }
1948  else if (strat->M==NULL)
1949  {
1950    M=idInit(1,F->rank);
1951    Warn("no minimal generating set computed");
1952  }
1953  else
1954  {
1955    idSkipZeroes(strat->M);
1956    M=strat->M;
1957  }
1958  delete(strat);
1959  if (reduced>2)
1960  {
1961    Kstd1_deg=Kstd1_OldDeg;
1962    if (!oldDegBound)
1963      test &= ~Sy_bit(OPT_DEGBOUND);
1964  }
1965  else
1966  {
1967    if (IDELEMS(M)>IDELEMS(r)) { idDelete(&M); M=idCopy(r); }
1968  }
1969  return r;
1970}
1971
1972poly kNF(ideal F, ideal Q, poly p,int syzComp, int lazyReduce)
1973{
1974  if (p==NULL)
1975     return NULL;
1976  kStrategy strat=new skStrategy;
1977  strat->syzComp = syzComp;
1978
1979  poly pp = p;
1980
1981#ifdef HAVE_PLURAL
1982  if(rIsSCA(currRing))
1983  {
1984    const unsigned int m_iFirstAltVar = scaFirstAltVar(currRing);
1985    const unsigned int m_iLastAltVar  = scaLastAltVar(currRing);
1986    pp = p_KillSquares(pp, m_iFirstAltVar, m_iLastAltVar, currRing);
1987
1988    if(Q == currQuotient)
1989      Q = SCAQuotient(currRing);
1990  }
1991#endif
1992
1993  poly res;
1994
1995  if (pOrdSgn==-1)
1996    res=kNF1(F,Q,pp,strat,lazyReduce);
1997  else
1998    res=kNF2(F,Q,pp,strat,lazyReduce);
1999  delete(strat);
2000
2001#ifdef HAVE_PLURAL
2002  if(pp != p)
2003    p_Delete(&pp, currRing);
2004#endif
2005
2006  return res;
2007}
2008
2009ideal kNF(ideal F, ideal Q, ideal p,int syzComp,int lazyReduce)
2010{
2011  ideal res;
2012  if (TEST_OPT_PROT)
2013  {
2014    Print("(S:%d)",IDELEMS(p));mflush();
2015  }
2016  kStrategy strat=new skStrategy;
2017  strat->syzComp = syzComp;
2018
2019  ideal pp = p;
2020#ifdef HAVE_PLURAL
2021  if(rIsSCA(currRing))
2022  {
2023    const unsigned int m_iFirstAltVar = scaFirstAltVar(currRing);
2024    const unsigned int m_iLastAltVar  = scaLastAltVar(currRing);
2025    pp = id_KillSquares(pp, m_iFirstAltVar, m_iLastAltVar, currRing);
2026
2027    if(Q == currQuotient)
2028      Q = SCAQuotient(currRing);
2029  }
2030#endif
2031
2032  if (pOrdSgn==-1)
2033    res=kNF1(F,Q,pp,strat,lazyReduce);
2034  else
2035    res=kNF2(F,Q,pp,strat,lazyReduce);
2036  delete(strat);
2037
2038#ifdef HAVE_PLURAL
2039  if(pp != p)
2040    id_Delete(&pp, currRing);
2041#endif
2042
2043  return res;
2044}
2045
2046/*2
2047*interreduces F
2048*/
2049// old version
2050ideal kInterRedOld (ideal F, ideal Q)
2051{
2052  int j;
2053  kStrategy strat = new skStrategy;
2054
2055  ideal tempF = F;
2056  ideal tempQ = Q;
2057
2058#ifdef HAVE_PLURAL
2059  if(rIsSCA(currRing))
2060  {
2061    const unsigned int m_iFirstAltVar = scaFirstAltVar(currRing);
2062    const unsigned int m_iLastAltVar  = scaLastAltVar(currRing);
2063    tempF = id_KillSquares(F, m_iFirstAltVar, m_iLastAltVar, currRing);
2064
2065    // this should be done on the upper level!!! :
2066    //    tempQ = SCAQuotient(currRing);
2067
2068    if(Q == currQuotient)
2069      tempQ = SCAQuotient(currRing);
2070  }
2071#endif
2072
2073//  if (TEST_OPT_PROT)
2074//  {
2075//    writeTime("start InterRed:");
2076//    mflush();
2077//  }
2078  //strat->syzComp     = 0;
2079  strat->kHEdgeFound = ppNoether != NULL;
2080  strat->kNoether=pCopy(ppNoether);
2081  strat->ak = idRankFreeModule(tempF);
2082  initBuchMoraCrit(strat);
2083  strat->NotUsedAxis = (BOOLEAN *)omAlloc((pVariables+1)*sizeof(BOOLEAN));
2084  for (j=pVariables; j>0; j--) strat->NotUsedAxis[j] = TRUE;
2085  strat->enterS      = enterSBba;
2086  strat->posInT      = posInT17;
2087  strat->initEcart   = initEcartNormal;
2088  strat->sl   = -1;
2089  strat->tl          = -1;
2090  strat->tmax        = setmaxT;
2091  strat->T           = initT();
2092  strat->R           = initR();
2093  strat->sevT        = initsevT();
2094  if (pOrdSgn == -1)   strat->honey = TRUE;
2095  initS(tempF, tempQ, strat);
2096  if (TEST_OPT_REDSB)
2097    strat->noTailReduction=FALSE;
2098  updateS(TRUE,strat);
2099  if (TEST_OPT_REDSB && TEST_OPT_INTSTRATEGY)
2100    completeReduce(strat);
2101  //else if (TEST_OPT_PROT) PrintLn();
2102  pDelete(&strat->kHEdge);
2103  omFreeSize((ADDRESS)strat->T,strat->tmax*sizeof(TObject));
2104  omFreeSize((ADDRESS)strat->ecartS,IDELEMS(strat->Shdl)*sizeof(int));
2105  omFreeSize((ADDRESS)strat->sevS,IDELEMS(strat->Shdl)*sizeof(unsigned long));
2106  omFreeSize((ADDRESS)strat->NotUsedAxis,(pVariables+1)*sizeof(BOOLEAN));
2107  omfree(strat->sevT);
2108  omfree(strat->S_2_R);
2109  omfree(strat->R);
2110
2111  if (strat->fromQ)
2112  {
2113    for (j=IDELEMS(strat->Shdl)-1;j>=0;j--)
2114    {
2115      if(strat->fromQ[j]) pDelete(&strat->Shdl->m[j]);
2116    }
2117    omFreeSize((ADDRESS)strat->fromQ,IDELEMS(strat->Shdl)*sizeof(int));
2118  }
2119//  if (TEST_OPT_PROT)
2120//  {
2121//    writeTime("end Interred:");
2122//    mflush();
2123//  }
2124  ideal shdl=strat->Shdl;
2125  idSkipZeroes(shdl);
2126  if (strat->fromQ)
2127  {
2128    strat->fromQ=NULL;
2129    ideal res=kInterRed(shdl,NULL);
2130    idDelete(&shdl);
2131    shdl=res;
2132  }
2133  delete(strat);
2134
2135#ifdef HAVE_PLURAL
2136  if( tempF != F )
2137    id_Delete( &tempF, currRing);
2138#endif
2139
2140  return shdl;
2141}
2142// new version
2143ideal kInterRedBba (ideal F, ideal Q, int &need_retry)
2144{
2145  need_retry=0;
2146  int   srmax,lrmax, red_result = 1;
2147  int   olddeg,reduc;
2148  BOOLEAN withT = FALSE;
2149  BOOLEAN b=pLexOrder;
2150  BOOLEAN toReset=FALSE;
2151  kStrategy strat=new skStrategy;
2152  tHomog h;
2153  intvec * w=NULL;
2154
2155  if (rField_has_simple_inverse())
2156    strat->LazyPass=20;
2157  else
2158    strat->LazyPass=2;
2159  strat->LazyDegree = 1;
2160  strat->ak = idRankFreeModule(F);
2161  strat->syzComp = strat->ak;
2162  strat->kModW=kModW=NULL;
2163  strat->kHomW=kHomW=NULL;
2164  if (strat->ak == 0)
2165  {
2166    h = (tHomog)idHomIdeal(F,Q);
2167    w=NULL;
2168  }
2169  else if (!TEST_OPT_DEGBOUND)
2170  {
2171    h = (tHomog)idHomModule(F,Q,&w);
2172  }
2173  pLexOrder=b;
2174  if (h==isHomog)
2175  {
2176    if (strat->ak > 0 && (w!=NULL) && (w!=NULL))
2177    {
2178      strat->kModW = kModW = w;
2179      pFDegOld = pFDeg;
2180      pLDegOld = pLDeg;
2181      pSetDegProcs(kModDeg);
2182      toReset = TRUE;
2183    }
2184    pLexOrder = TRUE;
2185    strat->LazyPass*=2;
2186  }
2187  strat->homog=h;
2188#ifdef KDEBUG
2189  idTest(F);
2190#endif
2191
2192  initBuchMoraCrit(strat); /*set Gebauer, honey, sugarCrit*/
2193  initBuchMoraPos(strat);
2194  initBba(F,strat);
2195  /*set enterS, spSpolyShort, reduce, red, initEcart, initEcartPair*/
2196  strat->posInL=posInL0; /* ord according pComp */
2197
2198  /*Shdl=*/initBuchMora(F, Q,strat);
2199  srmax = strat->sl;
2200  reduc = olddeg = lrmax = 0;
2201
2202#ifndef NO_BUCKETS
2203  if (!TEST_OPT_NOT_BUCKETS)
2204    strat->use_buckets = 1;
2205#endif
2206
2207  // redtailBBa against T for inhomogenous input
2208  if (!K_TEST_OPT_OLDSTD)
2209    withT = ! strat->homog;
2210
2211  // strat->posInT = posInT_pLength;
2212  kTest_TS(strat);
2213
2214#ifdef HAVE_TAIL_RING
2215  kStratInitChangeTailRing(strat);
2216#endif
2217
2218  /* compute------------------------------------------------------- */
2219  while (strat->Ll >= 0)
2220  {
2221    if (strat->Ll > lrmax) lrmax =strat->Ll;/*stat.*/
2222    #ifdef KDEBUG
2223      #ifdef HAVE_RINGS
2224        if (TEST_OPT_DEBUG) PrintS("--- next step ---\n");
2225      #endif
2226      if (TEST_OPT_DEBUG) messageSets(strat);
2227    #endif
2228    if (strat->Ll== 0) strat->interpt=TRUE;
2229    /* picks the last element from the lazyset L */
2230    strat->P = strat->L[strat->Ll];
2231    strat->Ll--;
2232
2233    if (strat->P.p1 == NULL)
2234    {
2235      // for input polys, prepare reduction
2236      strat->P.PrepareRed(strat->use_buckets);
2237    }
2238
2239    if (strat->P.p == NULL && strat->P.t_p == NULL)
2240    {
2241      red_result = 0;
2242    }
2243    else
2244    {
2245      int deg_before=olddeg;
2246      if (TEST_OPT_PROT)
2247        message(strat->P.pFDeg(),
2248                &olddeg,&reduc,strat, red_result);
2249
2250      /* reduction of the element choosen from L */
2251      red_result = strat->red(&strat->P,strat);
2252    }
2253
2254    // reduction to non-zero new poly
2255    if (red_result == 1)
2256    {
2257      /* statistic */
2258      if (TEST_OPT_PROT) PrintS("s");
2259
2260      // get the polynomial (canonicalize bucket, make sure P.p is set)
2261      strat->P.GetP(strat->lmBin);
2262
2263      int pos=posInS(strat,strat->sl,strat->P.p,strat->P.ecart);
2264
2265      // reduce the tail and normalize poly
2266      // in the ring case we cannot expect LC(f) = 1,
2267      // therefore we call pContent instead of pNorm
2268      if ((TEST_OPT_INTSTRATEGY) || (rField_is_Ring(currRing)))
2269      {
2270        strat->P.pCleardenom();
2271        if (0)
2272        //if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
2273        {
2274          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
2275          strat->P.pCleardenom();
2276        }
2277      }
2278      else
2279      {
2280        strat->P.pNorm();
2281        if (0)
2282        //if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
2283          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
2284      }
2285
2286#ifdef KDEBUG
2287      if (TEST_OPT_DEBUG){PrintS("new s:");strat->P.wrp();PrintLn();}
2288#endif
2289
2290      // enter into S, L, and T
2291      //if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
2292        enterT(strat->P, strat);
2293      // posInS only depends on the leading term
2294      strat->enterS(strat->P, pos, strat, strat->tl);
2295
2296      if (strat->P.lcm!=NULL)
2297#ifdef HAVE_RINGS
2298        pLmDelete(strat->P.lcm);
2299#else
2300        pLmFree(strat->P.lcm);
2301#endif
2302      if (strat->sl>srmax) srmax = strat->sl;
2303      if (pos<strat->sl)
2304        need_retry++;
2305    }
2306
2307#ifdef KDEBUG
2308    memset(&(strat->P), 0, sizeof(strat->P));
2309#endif
2310    kTest_TS(strat);
2311  }
2312#ifdef KDEBUG
2313  //if (TEST_OPT_DEBUG) messageSets(strat);
2314#endif
2315  /* complete reduction of the standard basis--------- */
2316
2317  if((need_retry==0) && (TEST_OPT_REDSB))
2318  {
2319    completeReduce(strat);
2320#ifdef HAVE_TAIL_RING
2321    if (strat->completeReduce_retry)
2322    {
2323      // completeReduce needed larger exponents, retry
2324      // to reduce with S (instead of T)
2325      // and in currRing (instead of strat->tailRing)
2326      cleanT(strat);strat->tailRing=currRing;
2327      int i;
2328      for(i=strat->sl;i>=0;i--) strat->S_2_R[i]=-1;
2329      completeReduce(strat);
2330    }
2331#endif
2332  }
2333  else if (TEST_OPT_PROT) PrintLn();
2334
2335  /* release temp data-------------------------------- */
2336  exitBuchMora(strat);
2337  if (TEST_OPT_WEIGHTM)
2338  {
2339    pRestoreDegProcs(pFDegOld, pLDegOld);
2340    if (ecartWeights)
2341    {
2342      omFreeSize((ADDRESS)ecartWeights,(pVariables+1)*sizeof(short));
2343      ecartWeights=NULL;
2344    }
2345  }
2346  //if (TEST_OPT_PROT) messageStat(srmax,lrmax,0/*hilbcount*/,strat);
2347  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
2348  ideal res=strat->Shdl;
2349  strat->Shdl=NULL;
2350  delete strat;
2351  if (w!=NULL) delete w;
2352  return res;
2353}
2354ideal kInterRed (ideal F, ideal Q)
2355{
2356#ifdef HAVE_PLURAL
2357  if(rIsPluralRing(currRing)) return kInterRedOld(F,Q);
2358#endif
2359  if(pOrdSgn==-1) return kInterRedOld(F,Q);
2360
2361  BITSET save=test;
2362  //test|=Sy_bit(OPT_NOT_SUGAR);
2363  test|=Sy_bit(OPT_REDTHROUGH);
2364  //test&= ~Sy_bit(OPT_REDTAIL);
2365  //test&= ~Sy_bit(OPT_REDSB);
2366  //extern char * showOption() ;
2367  //Print("%s\n",showOption());
2368
2369  int need_retry;
2370  int counter=3;
2371  int elems=idElem(F);
2372  ideal res=kInterRedBba(F,Q,need_retry);
2373  while (need_retry && (counter>0))
2374  {
2375    ideal res1=kInterRedBba(res,Q,need_retry);
2376    int new_elems=idElem(res1);
2377    counter -= (new_elems >= elems);
2378    elems = new_elems;
2379    idDelete(&res);
2380    res = res1;
2381  }
2382  test=save;
2383  idSkipZeroes(res);
2384  return res;
2385}
2386
2387
2388// returns TRUE if mora should use buckets, false otherwise
2389static BOOLEAN kMoraUseBucket(kStrategy strat)
2390{
2391#ifdef MORA_USE_BUCKETS
2392  if (TEST_OPT_NOT_BUCKETS)
2393    return FALSE;
2394  if (strat->red == redFirst)
2395  {
2396#ifdef NO_LDEG
2397    if (strat->syzComp==0)
2398      return TRUE;
2399#else
2400    if ((strat->homog || strat->honey) && (strat->syzComp==0))
2401      return TRUE;
2402#endif
2403  }
2404  else
2405  {
2406    assume(strat->red == redEcart);
2407    if (strat->honey && (strat->syzComp==0))
2408      return TRUE;
2409  }
2410#endif
2411  return FALSE;
2412}
Note: See TracBrowser for help on using the repository browser.