source: git/kernel/kstd1.cc @ 9eafde

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