source: git/kernel/kstd1.cc @ 92e0eb

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