source: git/kernel/kstd1.cc @ 87beab7

spielwiese
Last change on this file since 87beab7 was 18ef40, checked in by Oliver Wienand <wienand@…>, 15 years ago
mea culpa git-svn-id: file:///usr/local/Singular/svn/trunk@11217 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.42 2008-12-08 10:00:44 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  if ((!strat->kHEdgeFound) || (strat->kNoether!=NULL)) HEckeTest(p.p,strat);
954  if (strat->kHEdgeFound)
955  {
956    if (newHEdge(strat->S,strat))
957    {
958      firstUpdate(strat);
959      if (BTEST1(27))
960        return;
961      /*- cuts elements in L above noether and reorders L -*/
962      updateLHC(strat);
963      /*- reorders L with respect to posInL -*/
964      reorderL(strat);
965    }
966  }
967  else if (strat->kNoether!=NULL)
968    strat->kHEdgeFound = TRUE;
969  else if (TEST_OPT_FASTHC)
970  {
971    if (strat->posInLOldFlag)
972    {
973      missingAxis(&strat->lastAxis,strat);
974      if (strat->lastAxis)
975      {
976        strat->posInLOld = strat->posInL;
977        strat->posInLOldFlag = FALSE;
978        strat->posInL = posInL10;
979        strat->posInLDependsOnLength = TRUE;
980        updateL(strat);
981        reorderL(strat);
982      }
983    }
984    else if (strat->lastAxis)
985      updateL(strat);
986  }
987}
988
989/*2
990*-puts p to the standardbasis s at position at
991*-HEckeTest
992*  if TRUE
993*  - computes noether
994*/
995void enterSMoraNF (LObject p, int atS,kStrategy strat, int atR = -1)
996{
997  int i;
998
999  enterSBba(p, atS, strat, atR);
1000  if ((!strat->kHEdgeFound) || (strat->kNoether!=NULL)) HEckeTest(p.p,strat);
1001  if (strat->kHEdgeFound)
1002    newHEdge(strat->S,strat);
1003  else if (strat->kNoether!=NULL)
1004    strat->kHEdgeFound = TRUE;
1005}
1006
1007void initBba(ideal F,kStrategy strat)
1008{
1009  int i;
1010  idhdl h;
1011 /* setting global variables ------------------- */
1012  strat->enterS = enterSBba;
1013    strat->red = redHoney;
1014  if (strat->honey)
1015    strat->red = redHoney;
1016  else if (pLexOrder && !strat->homog)
1017    strat->red = redLazy;
1018  else
1019  {
1020    strat->LazyPass *=4;
1021    strat->red = redHomog;
1022  }
1023#ifdef HAVE_RINGS  //TODO Oliver
1024  if (rField_is_Ring(currRing))
1025  {
1026    strat->red = redRing;
1027  }
1028#endif
1029  if (pLexOrder && strat->honey)
1030    strat->initEcart = initEcartNormal;
1031  else
1032    strat->initEcart = initEcartBBA;
1033  if (strat->honey)
1034    strat->initEcartPair = initEcartPairMora;
1035  else
1036    strat->initEcartPair = initEcartPairBba;
1037  strat->kIdeal = NULL;
1038  //if (strat->ak==0) strat->kIdeal->rtyp=IDEAL_CMD;
1039  //else              strat->kIdeal->rtyp=MODUL_CMD;
1040  //strat->kIdeal->data=(void *)strat->Shdl;
1041  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1042  {
1043    //interred  machen   Aenderung
1044    pFDegOld=pFDeg;
1045    pLDegOld=pLDeg;
1046    //h=ggetid("ecart");
1047    //if ((h!=NULL) /*&& (IDTYP(h)==INTVEC_CMD)*/)
1048    //{
1049    //  ecartWeights=iv2array(IDINTVEC(h));
1050    //}
1051    //else
1052    {
1053      ecartWeights=(short *)omAlloc((pVariables+1)*sizeof(short));
1054      /*uses automatic computation of the ecartWeights to set them*/
1055      kEcartWeights(F->m,IDELEMS(F)-1,ecartWeights);
1056    }
1057    pRestoreDegProcs(totaldegreeWecart, maxdegreeWecart);
1058    if (TEST_OPT_PROT)
1059    {
1060      for(i=1; i<=pVariables; i++)
1061        Print(" %d",ecartWeights[i]);
1062      PrintLn();
1063      mflush();
1064    }
1065  }
1066}
1067
1068void initMora(ideal F,kStrategy strat)
1069{
1070  int i,j;
1071  idhdl h;
1072
1073  strat->NotUsedAxis = (BOOLEAN *)omAlloc((pVariables+1)*sizeof(BOOLEAN));
1074  for (j=pVariables; j>0; j--) strat->NotUsedAxis[j] = TRUE;
1075  strat->enterS = enterSMora;
1076  strat->initEcartPair = initEcartPairMora; /*- ecart approximation -*/
1077  strat->posInLOld = strat->posInL;
1078  strat->posInLOldFlag = TRUE;
1079  strat->initEcart = initEcartNormal;
1080  strat->kHEdgeFound = ppNoether != NULL;
1081  if ( strat->kHEdgeFound )
1082     strat->kNoether = pCopy(ppNoether);
1083  else if (strat->kHEdgeFound || strat->homog)
1084    strat->red = redFirst;  /*take the first possible in T*/
1085  else
1086    strat->red = redEcart;/*take the first possible in under ecart-restriction*/
1087  if (strat->kHEdgeFound)
1088  {
1089    strat->HCord = pFDeg(ppNoether,currRing)+1;
1090    strat->posInT = posInT2;
1091  }
1092  else
1093  {
1094    strat->HCord = 32000;/*- very large -*/
1095  }
1096  /*reads the ecartWeights used for Graebes method from the
1097   *intvec ecart and set ecartWeights
1098   */
1099  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1100  {
1101    //interred  machen   Aenderung
1102    pFDegOld=pFDeg;
1103    pLDegOld=pLDeg;
1104    //h=ggetid("ecart");
1105    //if ((h!=NULL) /*&& (IDTYP(h)==INTVEC_CMD)*/)
1106    //{
1107    //  ecartWeights=iv2array(IDINTVEC(h));
1108    //}
1109    //else
1110    {
1111      ecartWeights=(short *)omAlloc((pVariables+1)*sizeof(short));
1112      /*uses automatic computation of the ecartWeights to set them*/
1113      kEcartWeights(F->m,IDELEMS(F)-1,ecartWeights);
1114    }
1115
1116    pSetDegProcs(totaldegreeWecart, maxdegreeWecart);
1117    if (TEST_OPT_PROT)
1118    {
1119      for(i=1; i<=pVariables; i++)
1120        Print(" %d",ecartWeights[i]);
1121      PrintLn();
1122      mflush();
1123    }
1124  }
1125  kOptimizeLDeg(pLDeg, strat);
1126}
1127
1128#ifdef HAVE_ASSUME
1129static int mora_count = 0;
1130static int mora_loop_count;
1131#endif
1132
1133ideal mora (ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat)
1134{
1135#ifdef HAVE_ASSUME
1136  mora_count++;
1137  mora_loop_count = 0;
1138#endif
1139#ifdef KDEBUG
1140  om_Opts.MinTrack = 5;
1141#endif
1142  int srmax;
1143  int lrmax = 0;
1144  int olddeg = 0;
1145  int reduc = 0;
1146  int red_result = 1;
1147  int hilbeledeg=1,hilbcount=0;
1148
1149  strat->update = TRUE;
1150  /*- setting global variables ------------------- -*/
1151  initBuchMoraCrit(strat);
1152  initHilbCrit(F,Q,&hilb,strat);
1153  initMora(F,strat);
1154  initBuchMoraPos(strat);
1155  /*Shdl=*/initBuchMora(F,Q,strat);
1156  if (TEST_OPT_FASTHC) missingAxis(&strat->lastAxis,strat);
1157  /*updateS in initBuchMora has Hecketest
1158  * and could have put strat->kHEdgdeFound FALSE*/
1159  if (ppNoether!=NULL)
1160  {
1161    strat->kHEdgeFound = TRUE;
1162  }
1163  if (strat->kHEdgeFound && strat->update)
1164  {
1165    firstUpdate(strat);
1166    updateLHC(strat);
1167    reorderL(strat);
1168  }
1169  if (TEST_OPT_FASTHC && (strat->lastAxis) && strat->posInLOldFlag)
1170  {
1171    strat->posInLOld = strat->posInL;
1172    strat->posInLOldFlag = FALSE;
1173    strat->posInL = posInL10;
1174    updateL(strat);
1175    reorderL(strat);
1176  }
1177  srmax = strat->sl;
1178  kTest_TS(strat);
1179  strat->use_buckets = kMoraUseBucket(strat);
1180  /*- compute-------------------------------------------*/
1181
1182#ifdef HAVE_TAIL_RING
1183//  if (strat->homog && strat->red == redFirst)
1184    kStratInitChangeTailRing(strat);
1185#endif
1186
1187  while (strat->Ll >= 0)
1188  {
1189#ifdef HAVE_ASSUME
1190    mora_loop_count++;
1191#endif
1192    if (lrmax< strat->Ll) lrmax=strat->Ll; /*stat*/
1193    //test_int_std(strat->kIdeal);
1194    #ifdef KDEBUG
1195    if (TEST_OPT_DEBUG) messageSets(strat);
1196    #endif
1197    if (TEST_OPT_DEGBOUND
1198    && (strat->L[strat->Ll].ecart+strat->L[strat->Ll].GetpFDeg()> Kstd1_deg))
1199    {
1200      /*
1201      * stops computation if
1202      * - 24 (degBound)
1203      *   && upper degree is bigger than Kstd1_deg
1204      */
1205      while ((strat->Ll >= 0)
1206        && (strat->L[strat->Ll].p1!=NULL) && (strat->L[strat->Ll].p2!=NULL)
1207        && (strat->L[strat->Ll].ecart+strat->L[strat->Ll].GetpFDeg()> Kstd1_deg)
1208      )
1209      {
1210        deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
1211        //if (TEST_OPT_PROT)
1212        //{
1213        //   PrintS("D"); mflush();
1214        //}
1215      }
1216      if (strat->Ll<0) break;
1217      else strat->noClearS=TRUE;
1218    }
1219    strat->P = strat->L[strat->Ll];/*- picks the last element from the lazyset L -*/
1220    if (strat->Ll==0) strat->interpt=TRUE;
1221    strat->Ll--;
1222
1223    // create the real Spoly
1224    if (pNext(strat->P.p) == strat->tail)
1225    {
1226      /*- deletes the short spoly and computes -*/
1227#ifdef HAVE_RINGS_LOC
1228      if (rField_is_Ring(currRing))
1229        pLmDelete(strat->P.p);
1230      else
1231#endif
1232      pLmFree(strat->P.p);
1233      strat->P.p = NULL;
1234      poly m1 = NULL, m2 = NULL;
1235      // check that spoly creation is ok
1236      while (strat->tailRing != currRing &&
1237             !kCheckSpolyCreation(&(strat->P), strat, m1, m2))
1238      {
1239        assume(m1 == NULL && m2 == NULL);
1240        // if not, change to a ring where exponents are large enough
1241        kStratChangeTailRing(strat);
1242      }
1243      /* create the real one */
1244      ksCreateSpoly(&(strat->P), strat->kNoetherTail(), strat->use_buckets,
1245                    strat->tailRing, m1, m2, strat->R);
1246      if (!strat->use_buckets)
1247        strat->P.SetLength(strat->length_pLength);
1248    }
1249    else if (strat->P.p1 == NULL)
1250    {
1251      // for input polys, prepare reduction (buckets !)
1252      strat->P.SetLength(strat->length_pLength);
1253      strat->P.PrepareRed(strat->use_buckets);
1254    }
1255
1256    if (!strat->P.IsNull())
1257    {
1258      // might be NULL from noether !!!
1259      if (TEST_OPT_PROT)
1260        message(strat->P.ecart+strat->P.GetpFDeg(),&olddeg,&reduc,strat, red_result);
1261      // reduce
1262      red_result = strat->red(&strat->P,strat);
1263    }
1264
1265    if (! strat->P.IsNull())
1266    {
1267      strat->P.GetP();
1268      // statistics
1269      if (TEST_OPT_PROT) PrintS("s");
1270      // normalization
1271      if (!TEST_OPT_INTSTRATEGY)
1272        strat->P.pNorm();
1273      // tailreduction
1274      strat->P.p = redtail(&(strat->P),strat->sl,strat);
1275      // set ecart -- might have changed because of tail reductions
1276      if ((!strat->noTailReduction) && (!strat->honey))
1277        strat->initEcart(&strat->P);
1278      // cancel unit
1279      cancelunit(&strat->P);
1280      // for char 0, clear denominators
1281      if (TEST_OPT_INTSTRATEGY)
1282        strat->P.pCleardenom();
1283
1284      // put in T
1285      enterT(strat->P,strat);
1286      // build new pairs
1287#ifdef HAVE_RINGS_LOC
1288      if (rField_is_Ring(currRing))
1289        superenterpairs(strat->P.p,strat->sl,strat->P.ecart,0,strat, strat->tl);
1290      else
1291#endif
1292      enterpairs(strat->P.p,strat->sl,strat->P.ecart,0,strat, strat->tl);
1293      // put in S
1294      strat->enterS(strat->P,
1295                    posInS(strat,strat->sl,strat->P.p, strat->P.ecart),
1296                    strat, strat->tl);
1297
1298      // apply hilbert criterion
1299      if (hilb!=NULL) khCheck(Q,w,hilb,hilbeledeg,hilbcount,strat);
1300
1301      // clear strat->P
1302      if (strat->P.lcm!=NULL) 
1303#ifdef HAVE_RINGS_LOC
1304        pLmDelete(strat->P.lcm);
1305#else
1306        pLmFree(strat->P.lcm);
1307#endif
1308      strat->P.lcm=NULL;
1309#ifdef KDEBUG
1310      // make sure kTest_TS does not complain about strat->P
1311      memset(&strat->P,0,sizeof(strat->P));
1312#endif
1313      if (strat->sl>srmax) srmax = strat->sl; /*stat.*/
1314      if (strat->Ll>lrmax) lrmax = strat->Ll;
1315    }
1316    if (strat->kHEdgeFound)
1317    {
1318      if ((BTEST1(27))
1319      || ((TEST_OPT_MULTBOUND) && (scMult0Int((strat->Shdl)) < mu)))
1320      {
1321        // obachman: is this still used ???
1322        /*
1323        * stops computation if strat->kHEdgeFound and
1324        * - 27 (finiteDeterminacyTest)
1325        * or
1326        * - 23
1327        *   (multBound)
1328        *   && multiplicity of the ideal is smaller then a predefined number mu
1329        */
1330        while (strat->Ll >= 0) deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
1331      }
1332    }
1333    kTest_TS(strat);
1334  }
1335  /*- complete reduction of the standard basis------------------------ -*/
1336  if (TEST_OPT_REDSB) completeReduce(strat);
1337  else if (TEST_OPT_PROT) PrintLn();
1338  /*- release temp data------------------------------- -*/
1339  exitBuchMora(strat);
1340  /*- polynomials used for HECKE: HC, noether -*/
1341  if (BTEST1(27))
1342  {
1343    if (strat->kHEdge!=NULL)
1344      Kstd1_mu=pFDeg(strat->kHEdge,currRing);
1345    else
1346      Kstd1_mu=-1;
1347  }
1348  pDelete(&strat->kHEdge);
1349  strat->update = TRUE; //???
1350  strat->lastAxis = 0; //???
1351  pDelete(&strat->kNoether);
1352  omFreeSize((ADDRESS)strat->NotUsedAxis,(pVariables+1)*sizeof(BOOLEAN));
1353  if (TEST_OPT_PROT) messageStat(srmax,lrmax,hilbcount,strat);
1354  if (TEST_OPT_WEIGHTM)
1355  {
1356    pRestoreDegProcs(pFDegOld, pLDegOld);
1357    if (ecartWeights)
1358    {
1359      omFreeSize((ADDRESS)ecartWeights,(pVariables+1)*sizeof(short));
1360      ecartWeights=NULL;
1361    }
1362  }
1363  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
1364  idTest(strat->Shdl);
1365  return (strat->Shdl);
1366}
1367
1368poly kNF1 (ideal F,ideal Q,poly q, kStrategy strat, int lazyReduce)
1369{
1370  poly   p;
1371  int   i;
1372  int   j;
1373  int   o;
1374  LObject   h;
1375  BITSET save_test=test;
1376
1377  if ((idIs0(F))&&(Q==NULL))
1378    return pCopy(q); /*F=0*/
1379  strat->ak = si_max(idRankFreeModule(F),pMaxComp(q));
1380  /*- creating temp data structures------------------- -*/
1381  strat->kHEdgeFound = ppNoether != NULL;
1382  strat->kNoether    = pCopy(ppNoether);
1383  test|=Sy_bit(OPT_REDTAIL);
1384  test&=~Sy_bit(OPT_INTSTRATEGY);
1385  if (TEST_OPT_STAIRCASEBOUND
1386  && (! TEST_V_DEG_STOP)
1387  && (0<Kstd1_deg)
1388  && ((!strat->kHEdgeFound)
1389    ||(TEST_OPT_DEGBOUND && (pWTotaldegree(strat->kNoether)<Kstd1_deg))))
1390  {
1391    pDelete(&strat->kNoether);
1392    strat->kNoether=pOne();
1393    pSetExp(strat->kNoether,1, Kstd1_deg+1);
1394    pSetm(strat->kNoether);
1395    strat->kHEdgeFound=TRUE;
1396  }
1397  initBuchMoraCrit(strat);
1398  initBuchMoraPos(strat);
1399  initMora(F,strat);
1400  strat->enterS = enterSMoraNF;
1401  /*- set T -*/
1402  strat->tl = -1;
1403  strat->tmax = setmaxT;
1404  strat->T = initT();
1405  strat->R = initR();
1406  strat->sevT = initsevT();
1407  /*- set S -*/
1408  strat->sl = -1;
1409  /*- init local data struct.-------------------------- -*/
1410  /*Shdl=*/initS(F,Q,strat);
1411  if ((strat->ak!=0)
1412  && (strat->kHEdgeFound))
1413  {
1414    if (strat->ak!=1)
1415    {
1416      pSetComp(strat->kNoether,1);
1417      pSetmComp(strat->kNoether);
1418      poly p=pHead(strat->kNoether);
1419      pSetComp(p,strat->ak);
1420      pSetmComp(p);
1421      p=pAdd(strat->kNoether,p);
1422      strat->kNoether=pNext(p);
1423      p_LmFree(p,currRing);
1424    }
1425  }
1426  if ((lazyReduce & KSTD_NF_LAZY)==0)
1427  {
1428    for (i=strat->sl; i>=0; i--)
1429      pNorm(strat->S[i]);
1430  }
1431  /*- puts the elements of S also to T -*/
1432  for (i=0; i<=strat->sl; i++)
1433  {
1434    h.p = strat->S[i];
1435    h.ecart = strat->ecartS[i];
1436    if (strat->sevS[i] == 0) strat->sevS[i] = pGetShortExpVector(h.p);
1437    else assume(strat->sevS[i] == pGetShortExpVector(h.p));
1438    h.length = pLength(h.p);
1439    h.sev = strat->sevS[i];
1440    h.SetpFDeg();
1441    enterT(h,strat);
1442  }
1443  /*- compute------------------------------------------- -*/
1444  p = pCopy(q);
1445  deleteHC(&p,&o,&j,strat);
1446  if (TEST_OPT_PROT) { PrintS("r"); mflush(); }
1447  if (p!=NULL) p = redMoraNF(p,strat, lazyReduce & KSTD_NF_ECART);
1448  if ((p!=NULL)&&((lazyReduce & KSTD_NF_LAZY)==0))
1449  {
1450    if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
1451    p = redtail(p,strat->sl,strat);
1452  }
1453  /*- release temp data------------------------------- -*/
1454  cleanT(strat);
1455  omFreeSize((ADDRESS)strat->T,strat->tmax*sizeof(TObject));
1456  omFreeSize((ADDRESS)strat->ecartS,IDELEMS(strat->Shdl)*sizeof(int));
1457  omFreeSize((ADDRESS)strat->sevS,IDELEMS(strat->Shdl)*sizeof(unsigned long));
1458  omFreeSize((ADDRESS)strat->NotUsedAxis,(pVariables+1)*sizeof(BOOLEAN));
1459  omfree(strat->sevT);
1460  omfree(strat->S_2_R);
1461  omfree(strat->R);
1462
1463  if ((Q!=NULL)&&(strat->fromQ!=NULL))
1464  {
1465    i=((IDELEMS(Q)+IDELEMS(F)+15)/16)*16;
1466    omFreeSize((ADDRESS)strat->fromQ,i*sizeof(int));
1467    strat->fromQ=NULL;
1468  }
1469  pDelete(&strat->kHEdge);
1470  pDelete(&strat->kNoether);
1471  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1472  {
1473    pRestoreDegProcs(pFDegOld, pLDegOld);
1474    if (ecartWeights)
1475    {
1476      omFreeSize((ADDRESS *)&ecartWeights,(pVariables+1)*sizeof(short));
1477      ecartWeights=NULL;
1478    }
1479  }
1480  idDelete(&strat->Shdl);
1481  test=save_test;
1482  if (TEST_OPT_PROT) PrintLn();
1483  return p;
1484}
1485
1486ideal kNF1 (ideal F,ideal Q,ideal q, kStrategy strat, int lazyReduce)
1487{
1488  poly   p;
1489  int   i;
1490  int   j;
1491  int   o;
1492  LObject   h;
1493  ideal res;
1494  BITSET save_test=test;
1495
1496  if (idIs0(q)) return idInit(IDELEMS(q),si_max(q->rank,F->rank));
1497  if ((idIs0(F))&&(Q==NULL))
1498    return idCopy(q); /*F=0*/
1499  strat->ak = si_max(idRankFreeModule(F),idRankFreeModule(q));
1500  /*- creating temp data structures------------------- -*/
1501  strat->kHEdgeFound = ppNoether != NULL;
1502  strat->kNoether=pCopy(ppNoether);
1503  test|=Sy_bit(OPT_REDTAIL);
1504  if (TEST_OPT_STAIRCASEBOUND
1505  && (0<Kstd1_deg)
1506  && ((!strat->kHEdgeFound)
1507    ||(TEST_OPT_DEGBOUND && (pWTotaldegree(strat->kNoether)<Kstd1_deg))))
1508  {
1509    pDelete(&strat->kNoether);
1510    strat->kNoether=pOne();
1511    pSetExp(strat->kNoether,1, Kstd1_deg+1);
1512    pSetm(strat->kNoether);
1513    strat->kHEdgeFound=TRUE;
1514  }
1515  initBuchMoraCrit(strat);
1516  initBuchMoraPos(strat);
1517  initMora(F,strat);
1518  strat->enterS = enterSMoraNF;
1519  /*- set T -*/
1520  strat->tl = -1;
1521  strat->tmax = setmaxT;
1522  strat->T = initT();
1523  strat->R = initR();
1524  strat->sevT = initsevT();
1525  /*- set S -*/
1526  strat->sl = -1;
1527  /*- init local data struct.-------------------------- -*/
1528  /*Shdl=*/initS(F,Q,strat);
1529  if ((strat->ak!=0)
1530  && (strat->kHEdgeFound))
1531  {
1532    if (strat->ak!=1)
1533    {
1534      pSetComp(strat->kNoether,1);
1535      pSetmComp(strat->kNoether);
1536      poly p=pHead(strat->kNoether);
1537      pSetComp(p,strat->ak);
1538      pSetmComp(p);
1539      p=pAdd(strat->kNoether,p);
1540      strat->kNoether=pNext(p);
1541      p_LmFree(p,currRing);
1542    }
1543  }
1544  if (TEST_OPT_INTSTRATEGY && ((lazyReduce & KSTD_NF_LAZY)==0))
1545  {
1546    for (i=strat->sl; i>=0; i--)
1547      pNorm(strat->S[i]);
1548  }
1549  /*- compute------------------------------------------- -*/
1550  res=idInit(IDELEMS(q),strat->ak);
1551  for (i=0; i<IDELEMS(q); i++)
1552  {
1553    if (q->m[i]!=NULL)
1554    {
1555      p = pCopy(q->m[i]);
1556      deleteHC(&p,&o,&j,strat);
1557      if (p!=NULL)
1558      {
1559        /*- puts the elements of S also to T -*/
1560        for (j=0; j<=strat->sl; j++)
1561        {
1562          h.p = strat->S[j];
1563          h.ecart = strat->ecartS[j];
1564          h.pLength = h.length = pLength(h.p);
1565          if (strat->sevS[j] == 0) strat->sevS[j] = pGetShortExpVector(h.p);
1566          else assume(strat->sevS[j] == pGetShortExpVector(h.p));
1567          h.sev = strat->sevS[j];
1568          h.SetpFDeg();
1569          enterT(h,strat);
1570        }
1571        if (TEST_OPT_PROT) { PrintS("r"); mflush(); }
1572        p = redMoraNF(p,strat, lazyReduce & KSTD_NF_ECART);
1573        if ((p!=NULL)&&((lazyReduce & KSTD_NF_LAZY)==0))
1574        {
1575          if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
1576          p = redtail(p,strat->sl,strat);
1577        }
1578        cleanT(strat);
1579      }
1580      res->m[i]=p;
1581    }
1582    //else
1583    //  res->m[i]=NULL;
1584  }
1585  /*- release temp data------------------------------- -*/
1586  omFreeSize((ADDRESS)strat->T,strat->tmax*sizeof(TObject));
1587  omFreeSize((ADDRESS)strat->ecartS,IDELEMS(strat->Shdl)*sizeof(int));
1588  omFreeSize((ADDRESS)strat->sevS,IDELEMS(strat->Shdl)*sizeof(unsigned long));
1589  omFreeSize((ADDRESS)strat->NotUsedAxis,(pVariables+1)*sizeof(BOOLEAN));
1590  omfree(strat->sevT);
1591  omfree(strat->S_2_R);
1592  omfree(strat->R);
1593  if ((Q!=NULL)&&(strat->fromQ!=NULL))
1594  {
1595    i=((IDELEMS(Q)+IDELEMS(F)+15)/16)*16;
1596    omFreeSize((ADDRESS)strat->fromQ,i*sizeof(int));
1597    strat->fromQ=NULL;
1598  }
1599  pDelete(&strat->kHEdge);
1600  pDelete(&strat->kNoether);
1601  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1602  {
1603    pFDeg=pFDegOld;
1604    pLDeg=pLDegOld;
1605    if (ecartWeights)
1606    {
1607      omFreeSize((ADDRESS *)&ecartWeights,(pVariables+1)*sizeof(short));
1608      ecartWeights=NULL;
1609    }
1610  }
1611  idDelete(&strat->Shdl);
1612  test=save_test;
1613  if (TEST_OPT_PROT) PrintLn();
1614  return res;
1615}
1616
1617pFDegProc pFDegOld;
1618pLDegProc pLDegOld;
1619intvec * kModW, * kHomW;
1620
1621long kModDeg(poly p, ring r)
1622{
1623  long o=pWDegree(p, r);
1624  long i=p_GetComp(p, r);
1625  if (i==0) return o;
1626  assume((i>0) && (i<=kModW->length()));
1627  return o+(*kModW)[i-1];
1628}
1629long kHomModDeg(poly p, ring r)
1630{
1631  int i;
1632  long j=0;
1633
1634  for (i=r->N;i>0;i--)
1635    j+=p_GetExp(p,i,r)*(*kHomW)[i-1];
1636  if (kModW == NULL) return j;
1637  i = p_GetComp(p,r);
1638  if (i==0) return j;
1639  return j+(*kModW)[i-1];
1640}
1641
1642ideal kStd(ideal F, ideal Q, tHomog h,intvec ** w, intvec *hilb,int syzComp,
1643          int newIdeal, intvec *vw)
1644{
1645  ideal r;
1646  BOOLEAN b=pLexOrder,toReset=FALSE;
1647  BOOLEAN delete_w=(w==NULL);
1648  kStrategy strat=new skStrategy;
1649
1650  if(!TEST_OPT_RETURN_SB)
1651    strat->syzComp = syzComp;
1652  if (TEST_OPT_SB_1)
1653    strat->newIdeal = newIdeal;
1654  if (rField_has_simple_inverse())
1655    strat->LazyPass=20;
1656  else
1657    strat->LazyPass=2;
1658  strat->LazyDegree = 1;
1659  strat->ak = idRankFreeModule(F);
1660  strat->kModW=kModW=NULL;
1661  strat->kHomW=kHomW=NULL;
1662  if (vw != NULL)
1663  {
1664    pLexOrder=FALSE;
1665    strat->kHomW=kHomW=vw;
1666    pFDegOld = pFDeg;
1667    pLDegOld = pLDeg;
1668    pSetDegProcs(kHomModDeg);
1669    toReset = TRUE;
1670  }
1671  if (h==testHomog)
1672  {
1673    if (strat->ak == 0)
1674    {
1675      h = (tHomog)idHomIdeal(F,Q);
1676      w=NULL;
1677    }
1678    else if (!TEST_OPT_DEGBOUND)
1679    {
1680      h = (tHomog)idHomModule(F,Q,w);
1681    }
1682  }
1683  pLexOrder=b;
1684  if (h==isHomog)
1685  {
1686    if (strat->ak > 0 && (w!=NULL) && (*w!=NULL))
1687    {
1688      strat->kModW = kModW = *w;
1689      if (vw == NULL)
1690      {
1691        pFDegOld = pFDeg;
1692        pLDegOld = pLDeg;
1693        pSetDegProcs(kModDeg);
1694        toReset = TRUE;
1695      }
1696    }
1697    pLexOrder = TRUE;
1698    if (hilb==NULL) strat->LazyPass*=2;
1699  }
1700  strat->homog=h;
1701#ifdef KDEBUG
1702  idTest(F);
1703#endif
1704#ifdef HAVE_PLURAL
1705  if (rIsPluralRing(currRing))
1706  {
1707    if (w!=NULL)
1708      r = nc_GB(F, Q, *w, hilb, strat);
1709    else
1710      r = nc_GB(F, Q, NULL, hilb, strat);
1711  }
1712  else
1713#endif
1714  {
1715    if (pOrdSgn==-1)
1716    {
1717      if (w!=NULL)
1718        r=mora(F,Q,*w,hilb,strat);
1719      else
1720        r=mora(F,Q,NULL,hilb,strat);
1721    }
1722    else
1723    {
1724      if (w!=NULL)
1725        r=bba(F,Q,*w,hilb,strat);
1726      else
1727        r=bba(F,Q,NULL,hilb,strat);
1728    }
1729  }
1730#ifdef KDEBUG
1731  idTest(r);
1732#endif
1733  if (toReset)
1734  {
1735    kModW = NULL;
1736    pRestoreDegProcs(pFDegOld, pLDegOld);
1737  }
1738  pLexOrder = b;
1739//Print("%d reductions canceled \n",strat->cel);
1740  HCord=strat->HCord;
1741  delete(strat);
1742  if ((delete_w)&&(w!=NULL)&&(*w!=NULL)) delete *w;
1743  return r;
1744}
1745
1746#ifdef HAVE_SHIFTBBA
1747ideal kStdShift(ideal F, ideal Q, tHomog h,intvec ** w, intvec *hilb,int syzComp,
1748                int newIdeal, intvec *vw, int uptodeg, int lV)
1749{
1750  ideal r;
1751  BOOLEAN b=pLexOrder,toReset=FALSE;
1752  BOOLEAN delete_w=(w==NULL);
1753  kStrategy strat=new skStrategy;
1754
1755  if(!TEST_OPT_RETURN_SB)
1756    strat->syzComp = syzComp;
1757  if (TEST_OPT_SB_1)
1758    strat->newIdeal = newIdeal;
1759  if (rField_has_simple_inverse())
1760    strat->LazyPass=20;
1761  else
1762    strat->LazyPass=2;
1763  strat->LazyDegree = 1;
1764  strat->ak = idRankFreeModule(F);
1765  strat->kModW=kModW=NULL;
1766  strat->kHomW=kHomW=NULL;
1767  if (vw != NULL)
1768  {
1769    pLexOrder=FALSE;
1770    strat->kHomW=kHomW=vw;
1771    pFDegOld = pFDeg;
1772    pLDegOld = pLDeg;
1773    pSetDegProcs(kHomModDeg);
1774    toReset = TRUE;
1775  }
1776  if (h==testHomog)
1777  {
1778    if (strat->ak == 0)
1779    {
1780      h = (tHomog)idHomIdeal(F,Q);
1781      w=NULL;
1782    }
1783    else if (!TEST_OPT_DEGBOUND)
1784    {
1785      h = (tHomog)idHomModule(F,Q,w);
1786    }
1787  }
1788  pLexOrder=b;
1789  if (h==isHomog)
1790  {
1791    if (strat->ak > 0 && (w!=NULL) && (*w!=NULL))
1792    {
1793      strat->kModW = kModW = *w;
1794      if (vw == NULL)
1795      {
1796        pFDegOld = pFDeg;
1797        pLDegOld = pLDeg;
1798        pSetDegProcs(kModDeg);
1799        toReset = TRUE;
1800      }
1801    }
1802    pLexOrder = TRUE;
1803    if (hilb==NULL) strat->LazyPass*=2;
1804  }
1805  strat->homog=h;
1806#ifdef KDEBUG
1807  idTest(F);
1808#endif
1809  if (pOrdSgn==-1)
1810  {
1811    /* error: no local ord yet with shifts */
1812    Print("No local ordering possible for shifts");
1813    return(NULL);
1814  }
1815  else
1816  {
1817    /* global ordering */
1818    if (w!=NULL)
1819      r=bbaShift(F,Q,*w,hilb,strat,uptodeg,lV);
1820    else
1821      r=bbaShift(F,Q,NULL,hilb,strat,uptodeg,lV);
1822  }
1823#ifdef KDEBUG
1824  idTest(r);
1825#endif
1826  if (toReset)
1827  {
1828    kModW = NULL;
1829    pRestoreDegProcs(pFDegOld, pLDegOld);
1830  }
1831  pLexOrder = b;
1832//Print("%d reductions canceled \n",strat->cel);
1833  HCord=strat->HCord;
1834  delete(strat);
1835  if ((delete_w)&&(w!=NULL)&&(*w!=NULL)) delete *w;
1836  return r;
1837}
1838#endif
1839
1840//##############################################################
1841//##############################################################
1842//##############################################################
1843//##############################################################
1844//##############################################################
1845
1846ideal kMin_std(ideal F, ideal Q, tHomog h,intvec ** w, ideal &M, intvec *hilb,
1847              int syzComp, int reduced)
1848{
1849  ideal r=NULL;
1850  int Kstd1_OldDeg = Kstd1_deg,i;
1851  intvec* temp_w=NULL;
1852  BOOLEAN b=pLexOrder,toReset=FALSE;
1853  BOOLEAN delete_w=(w==NULL);
1854  BOOLEAN oldDegBound=TEST_OPT_DEGBOUND;
1855  kStrategy strat=new skStrategy;
1856
1857  if(!TEST_OPT_RETURN_SB)
1858     strat->syzComp = syzComp;
1859  if (rField_has_simple_inverse())
1860    strat->LazyPass=20;
1861  else
1862    strat->LazyPass=2;
1863  strat->LazyDegree = 1;
1864  strat->minim=(reduced % 2)+1;
1865  strat->ak = idRankFreeModule(F);
1866  if (delete_w)
1867  {
1868    temp_w=new intvec((strat->ak)+1);
1869    w = &temp_w;
1870  }
1871  if ((h==testHomog)
1872  )
1873  {
1874    if (strat->ak == 0)
1875    {
1876      h = (tHomog)idHomIdeal(F,Q);
1877      w=NULL;
1878    }
1879    else
1880    {
1881      h = (tHomog)idHomModule(F,Q,w);
1882    }
1883  }
1884  if (h==isHomog)
1885  {
1886    if (strat->ak > 0 && (w!=NULL) && (*w!=NULL))
1887    {
1888      kModW = *w;
1889      strat->kModW = *w;
1890      assume(pFDeg != NULL && pLDeg != NULL);
1891      pFDegOld = pFDeg;
1892      pLDegOld = pLDeg;
1893      pSetDegProcs(kModDeg);
1894
1895      toReset = TRUE;
1896      if (reduced>1)
1897      {
1898        Kstd1_OldDeg=Kstd1_deg;
1899        Kstd1_deg = -1;
1900        for (i=IDELEMS(F)-1;i>=0;i--)
1901        {
1902          if ((F->m[i]!=NULL) && (pFDeg(F->m[i],currRing)>=Kstd1_deg))
1903            Kstd1_deg = pFDeg(F->m[i],currRing)+1;
1904        }
1905      }
1906    }
1907    pLexOrder = TRUE;
1908    strat->LazyPass*=2;
1909  }
1910  strat->homog=h;
1911  if (pOrdSgn==-1)
1912  {
1913    if (w!=NULL)
1914      r=mora(F,Q,*w,hilb,strat);
1915    else
1916      r=mora(F,Q,NULL,hilb,strat);
1917  }
1918  else
1919  {
1920    if (w!=NULL)
1921      r=bba(F,Q,*w,hilb,strat);
1922    else
1923      r=bba(F,Q,NULL,hilb,strat);
1924  }
1925#ifdef KDEBUG
1926  {
1927    int i;
1928    for (i=IDELEMS(r)-1; i>=0; i--) pTest(r->m[i]);
1929  }
1930#endif
1931  idSkipZeroes(r);
1932  if (toReset)
1933  {
1934    pRestoreDegProcs(pFDegOld, pLDegOld);
1935    kModW = NULL;
1936  }
1937  pLexOrder = b;
1938  HCord=strat->HCord;
1939  if ((delete_w)&&(temp_w!=NULL)) delete temp_w;
1940  if ((IDELEMS(r)==1) && (r->m[0]!=NULL) && pIsConstant(r->m[0]) && (strat->ak==0))
1941  {
1942    M=idInit(1,F->rank);
1943    M->m[0]=pOne();
1944    //if (strat->ak!=0) { pSetComp(M->m[0],strat->ak); pSetmComp(M->m[0]); }
1945    if (strat->M!=NULL) idDelete(&strat->M);
1946  }
1947  else if (strat->M==NULL)
1948  {
1949    M=idInit(1,F->rank);
1950    Warn("no minimal generating set computed");
1951  }
1952  else
1953  {
1954    idSkipZeroes(strat->M);
1955    M=strat->M;
1956  }
1957  delete(strat);
1958  if (reduced>2)
1959  {
1960    Kstd1_deg=Kstd1_OldDeg;
1961    if (!oldDegBound)
1962      test &= ~Sy_bit(OPT_DEGBOUND);
1963  }
1964  else
1965  {
1966    if (IDELEMS(M)>IDELEMS(r)) { idDelete(&M); M=idCopy(r); }
1967  }
1968  return r;
1969}
1970
1971poly kNF(ideal F, ideal Q, poly p,int syzComp, int lazyReduce)
1972{
1973  if (p==NULL)
1974     return NULL;
1975  kStrategy strat=new skStrategy;
1976  strat->syzComp = syzComp;
1977
1978  poly pp = p;
1979
1980#ifdef HAVE_PLURAL
1981  if(rIsSCA(currRing))
1982  {
1983    const unsigned int m_iFirstAltVar = scaFirstAltVar(currRing);
1984    const unsigned int m_iLastAltVar  = scaLastAltVar(currRing);
1985    pp = p_KillSquares(pp, m_iFirstAltVar, m_iLastAltVar, currRing);
1986
1987    if(Q == currQuotient)
1988      Q = SCAQuotient(currRing);
1989  }
1990#endif
1991
1992  poly res;
1993
1994  if (pOrdSgn==-1)
1995    res=kNF1(F,Q,pp,strat,lazyReduce);
1996  else
1997    res=kNF2(F,Q,pp,strat,lazyReduce);
1998  delete(strat);
1999
2000#ifdef HAVE_PLURAL
2001  if(pp != p)
2002    p_Delete(&pp, currRing);
2003#endif
2004
2005  return res;
2006}
2007
2008ideal kNF(ideal F, ideal Q, ideal p,int syzComp,int lazyReduce)
2009{
2010  ideal res;
2011  if (TEST_OPT_PROT)
2012  {
2013    Print("(S:%d)",IDELEMS(p));mflush();
2014  }
2015  kStrategy strat=new skStrategy;
2016  strat->syzComp = syzComp;
2017
2018  ideal pp = p;
2019#ifdef HAVE_PLURAL
2020  if(rIsSCA(currRing))
2021  {
2022    const unsigned int m_iFirstAltVar = scaFirstAltVar(currRing);
2023    const unsigned int m_iLastAltVar  = scaLastAltVar(currRing);
2024    pp = id_KillSquares(pp, m_iFirstAltVar, m_iLastAltVar, currRing);
2025
2026    if(Q == currQuotient)
2027      Q = SCAQuotient(currRing);
2028  }
2029#endif
2030
2031  if (pOrdSgn==-1)
2032    res=kNF1(F,Q,pp,strat,lazyReduce);
2033  else
2034    res=kNF2(F,Q,pp,strat,lazyReduce);
2035  delete(strat);
2036
2037#ifdef HAVE_PLURAL
2038  if(pp != p)
2039    id_Delete(&pp, currRing);
2040#endif
2041
2042  return res;
2043}
2044
2045/*2
2046*interreduces F
2047*/
2048// old version
2049ideal kInterRedOld (ideal F, ideal Q)
2050{
2051  int j;
2052  kStrategy strat = new skStrategy;
2053
2054  ideal tempF = F;
2055  ideal tempQ = Q;
2056
2057#ifdef HAVE_PLURAL
2058  if(rIsSCA(currRing))
2059  {
2060    const unsigned int m_iFirstAltVar = scaFirstAltVar(currRing);
2061    const unsigned int m_iLastAltVar  = scaLastAltVar(currRing);
2062    tempF = id_KillSquares(F, m_iFirstAltVar, m_iLastAltVar, currRing);
2063
2064    // this should be done on the upper level!!! :
2065    //    tempQ = SCAQuotient(currRing);
2066
2067    if(Q == currQuotient)
2068      tempQ = SCAQuotient(currRing);
2069  }
2070#endif
2071
2072//  if (TEST_OPT_PROT)
2073//  {
2074//    writeTime("start InterRed:");
2075//    mflush();
2076//  }
2077  //strat->syzComp     = 0;
2078  strat->kHEdgeFound = ppNoether != NULL;
2079  strat->kNoether=pCopy(ppNoether);
2080  strat->ak = idRankFreeModule(tempF);
2081  initBuchMoraCrit(strat);
2082  strat->NotUsedAxis = (BOOLEAN *)omAlloc((pVariables+1)*sizeof(BOOLEAN));
2083  for (j=pVariables; j>0; j--) strat->NotUsedAxis[j] = TRUE;
2084  strat->enterS      = enterSBba;
2085  strat->posInT      = posInT17;
2086  strat->initEcart   = initEcartNormal;
2087  strat->sl   = -1;
2088  strat->tl          = -1;
2089  strat->tmax        = setmaxT;
2090  strat->T           = initT();
2091  strat->R           = initR();
2092  strat->sevT        = initsevT();
2093  if (pOrdSgn == -1)   strat->honey = TRUE;
2094  initS(tempF, tempQ, strat);
2095  if (TEST_OPT_REDSB)
2096    strat->noTailReduction=FALSE;
2097  updateS(TRUE,strat);
2098  if (TEST_OPT_REDSB && TEST_OPT_INTSTRATEGY)
2099    completeReduce(strat);
2100  //else if (TEST_OPT_PROT) PrintLn();
2101  pDelete(&strat->kHEdge);
2102  omFreeSize((ADDRESS)strat->T,strat->tmax*sizeof(TObject));
2103  omFreeSize((ADDRESS)strat->ecartS,IDELEMS(strat->Shdl)*sizeof(int));
2104  omFreeSize((ADDRESS)strat->sevS,IDELEMS(strat->Shdl)*sizeof(unsigned long));
2105  omFreeSize((ADDRESS)strat->NotUsedAxis,(pVariables+1)*sizeof(BOOLEAN));
2106  omfree(strat->sevT);
2107  omfree(strat->S_2_R);
2108  omfree(strat->R);
2109
2110  if (strat->fromQ)
2111  {
2112    for (j=IDELEMS(strat->Shdl)-1;j>=0;j--)
2113    {
2114      if(strat->fromQ[j]) pDelete(&strat->Shdl->m[j]);
2115    }
2116    omFreeSize((ADDRESS)strat->fromQ,IDELEMS(strat->Shdl)*sizeof(int));
2117  }
2118//  if (TEST_OPT_PROT)
2119//  {
2120//    writeTime("end Interred:");
2121//    mflush();
2122//  }
2123  ideal shdl=strat->Shdl;
2124  idSkipZeroes(shdl);
2125  if (strat->fromQ)
2126  {
2127    strat->fromQ=NULL;
2128    ideal res=kInterRed(shdl,NULL);
2129    idDelete(&shdl);
2130    shdl=res;
2131  }
2132  delete(strat);
2133
2134#ifdef HAVE_PLURAL
2135  if( tempF != F )
2136    id_Delete( &tempF, currRing);
2137#endif
2138
2139  return shdl;
2140}
2141// new version
2142ideal kInterRedBba (ideal F, ideal Q, int &need_retry)
2143{
2144  need_retry=0;
2145  int   srmax,lrmax, red_result = 1;
2146  int   olddeg,reduc;
2147  BOOLEAN withT = FALSE;
2148  BOOLEAN b=pLexOrder;
2149  BOOLEAN toReset=FALSE;
2150  kStrategy strat=new skStrategy;
2151  tHomog h;
2152  intvec * w=NULL;
2153
2154  if (rField_has_simple_inverse())
2155    strat->LazyPass=20;
2156  else
2157    strat->LazyPass=2;
2158  strat->LazyDegree = 1;
2159  strat->ak = idRankFreeModule(F);
2160  strat->syzComp = strat->ak;
2161  strat->kModW=kModW=NULL;
2162  strat->kHomW=kHomW=NULL;
2163  if (strat->ak == 0)
2164  {
2165    h = (tHomog)idHomIdeal(F,Q);
2166    w=NULL;
2167  }
2168  else if (!TEST_OPT_DEGBOUND)
2169  {
2170    h = (tHomog)idHomModule(F,Q,&w);
2171  }
2172  pLexOrder=b;
2173  if (h==isHomog)
2174  {
2175    if (strat->ak > 0 && (w!=NULL) && (w!=NULL))
2176    {
2177      strat->kModW = kModW = w;
2178      pFDegOld = pFDeg;
2179      pLDegOld = pLDeg;
2180      pSetDegProcs(kModDeg);
2181      toReset = TRUE;
2182    }
2183    pLexOrder = TRUE;
2184    strat->LazyPass*=2;
2185  }
2186  strat->homog=h;
2187#ifdef KDEBUG
2188  idTest(F);
2189#endif
2190
2191  initBuchMoraCrit(strat); /*set Gebauer, honey, sugarCrit*/
2192  initBuchMoraPos(strat);
2193  initBba(F,strat);
2194  /*set enterS, spSpolyShort, reduce, red, initEcart, initEcartPair*/
2195  strat->posInL=posInL0; /* ord according pComp */
2196
2197  /*Shdl=*/initBuchMora(F, Q,strat);
2198  srmax = strat->sl;
2199  reduc = olddeg = lrmax = 0;
2200
2201#ifndef NO_BUCKETS
2202  if (!TEST_OPT_NOT_BUCKETS)
2203    strat->use_buckets = 1;
2204#endif
2205
2206  // redtailBBa against T for inhomogenous input
2207  if (!K_TEST_OPT_OLDSTD)
2208    withT = ! strat->homog;
2209
2210  // strat->posInT = posInT_pLength;
2211  kTest_TS(strat);
2212
2213#ifdef HAVE_TAIL_RING
2214  kStratInitChangeTailRing(strat);
2215#endif
2216
2217  /* compute------------------------------------------------------- */
2218  while (strat->Ll >= 0)
2219  {
2220    if (strat->Ll > lrmax) lrmax =strat->Ll;/*stat.*/
2221    #ifdef KDEBUG
2222      #ifdef HAVE_RINGS
2223        if (TEST_OPT_DEBUG) PrintS("--- next step ---\n");
2224      #endif
2225      if (TEST_OPT_DEBUG) messageSets(strat);
2226    #endif
2227    if (strat->Ll== 0) strat->interpt=TRUE;
2228    /* picks the last element from the lazyset L */
2229    strat->P = strat->L[strat->Ll];
2230    strat->Ll--;
2231
2232    if (strat->P.p1 == NULL)
2233    {
2234      // for input polys, prepare reduction
2235      strat->P.PrepareRed(strat->use_buckets);
2236    }
2237
2238    if (strat->P.p == NULL && strat->P.t_p == NULL)
2239    {
2240      red_result = 0;
2241    }
2242    else
2243    {
2244      int deg_before=olddeg;
2245      if (TEST_OPT_PROT)
2246        message(strat->P.pFDeg(),
2247                &olddeg,&reduc,strat, red_result);
2248
2249      /* reduction of the element choosen from L */
2250      red_result = strat->red(&strat->P,strat);
2251    }
2252
2253    // reduction to non-zero new poly
2254    if (red_result == 1)
2255    {
2256      /* statistic */
2257      if (TEST_OPT_PROT) PrintS("s");
2258
2259      // get the polynomial (canonicalize bucket, make sure P.p is set)
2260      strat->P.GetP(strat->lmBin);
2261
2262      int pos=posInS(strat,strat->sl,strat->P.p,strat->P.ecart);
2263
2264      // reduce the tail and normalize poly
2265      // in the ring case we cannot expect LC(f) = 1,
2266      // therefore we call pContent instead of pNorm
2267      if ((TEST_OPT_INTSTRATEGY) || (rField_is_Ring(currRing)))
2268      {
2269        strat->P.pCleardenom();
2270        if (0)
2271        //if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
2272        {
2273          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
2274          strat->P.pCleardenom();
2275        }
2276      }
2277      else
2278      {
2279        strat->P.pNorm();
2280        if (0)
2281        //if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
2282          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
2283      }
2284
2285#ifdef KDEBUG
2286      if (TEST_OPT_DEBUG){PrintS("new s:");strat->P.wrp();PrintLn();}
2287#endif
2288
2289      // enter into S, L, and T
2290      //if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
2291        enterT(strat->P, strat);
2292      // posInS only depends on the leading term
2293      strat->enterS(strat->P, pos, strat, strat->tl);
2294
2295      if (strat->P.lcm!=NULL)
2296#ifdef HAVE_RINGS
2297        pLmDelete(strat->P.lcm);
2298#else
2299        pLmFree(strat->P.lcm);
2300#endif
2301      if (strat->sl>srmax) srmax = strat->sl;
2302      if (pos<strat->sl)
2303        need_retry++;
2304    }
2305
2306#ifdef KDEBUG
2307    memset(&(strat->P), 0, sizeof(strat->P));
2308#endif
2309    kTest_TS(strat);
2310  }
2311#ifdef KDEBUG
2312  //if (TEST_OPT_DEBUG) messageSets(strat);
2313#endif
2314  /* complete reduction of the standard basis--------- */
2315
2316  if((need_retry==0) && (TEST_OPT_REDSB))
2317  {
2318    completeReduce(strat);
2319#ifdef HAVE_TAIL_RING
2320    if (strat->completeReduce_retry)
2321    {
2322      // completeReduce needed larger exponents, retry
2323      // to reduce with S (instead of T)
2324      // and in currRing (instead of strat->tailRing)
2325      cleanT(strat);strat->tailRing=currRing;
2326      int i;
2327      for(i=strat->sl;i>=0;i--) strat->S_2_R[i]=-1;
2328      completeReduce(strat);
2329    }
2330#endif
2331  }
2332  else if (TEST_OPT_PROT) PrintLn();
2333
2334  /* release temp data-------------------------------- */
2335  exitBuchMora(strat);
2336  if (TEST_OPT_WEIGHTM)
2337  {
2338    pRestoreDegProcs(pFDegOld, pLDegOld);
2339    if (ecartWeights)
2340    {
2341      omFreeSize((ADDRESS)ecartWeights,(pVariables+1)*sizeof(short));
2342      ecartWeights=NULL;
2343    }
2344  }
2345  //if (TEST_OPT_PROT) messageStat(srmax,lrmax,0/*hilbcount*/,strat);
2346  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
2347  ideal res=strat->Shdl;
2348  strat->Shdl=NULL;
2349  delete strat;
2350  if (w!=NULL) delete w;
2351  return res;
2352}
2353ideal kInterRed (ideal F, ideal Q)
2354{
2355#ifdef HAVE_PLURAL
2356  if(rIsPluralRing(currRing)) return kInterRedOld(F,Q);
2357#endif
2358  if(pOrdSgn==-1) return kInterRedOld(F,Q);
2359
2360  BITSET save=test;
2361  //test|=Sy_bit(OPT_NOT_SUGAR);
2362  test|=Sy_bit(OPT_REDTHROUGH);
2363  //test&= ~Sy_bit(OPT_REDTAIL);
2364  //test&= ~Sy_bit(OPT_REDSB);
2365  //extern char * showOption() ;
2366  //Print("%s\n",showOption());
2367
2368  int need_retry;
2369  int counter=3;
2370  int elems=idElem(F);
2371  ideal res=kInterRedBba(F,Q,need_retry);
2372  while (need_retry && (counter>0))
2373  {
2374    ideal res1=kInterRedBba(res,Q,need_retry);
2375    int new_elems=idElem(res1);
2376    counter -= (new_elems >= elems);
2377    elems = new_elems;
2378    idDelete(&res);
2379    res = res1;
2380  }
2381  test=save;
2382  idSkipZeroes(res);
2383  return res;
2384}
2385
2386
2387// returns TRUE if mora should use buckets, false otherwise
2388static BOOLEAN kMoraUseBucket(kStrategy strat)
2389{
2390#ifdef MORA_USE_BUCKETS
2391  if (TEST_OPT_NOT_BUCKETS)
2392    return FALSE;
2393  if (strat->red == redFirst)
2394  {
2395#ifdef NO_LDEG
2396    if (strat->syzComp==0)
2397      return TRUE;
2398#else
2399    if ((strat->homog || strat->honey) && (strat->syzComp==0))
2400      return TRUE;
2401#endif
2402  }
2403  else
2404  {
2405    assume(strat->red == redEcart);
2406    if (strat->honey && (strat->syzComp==0))
2407      return TRUE;
2408  }
2409#endif
2410  return FALSE;
2411}
Note: See TracBrowser for help on using the repository browser.