source: git/kernel/kstd1.cc @ ea095d

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