source: git/kernel/kstd1.cc @ 36605f

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