source: git/Singular/kstd1.cc @ 19df79

spielwiese
Last change on this file since 19df79 was 19df79, checked in by Olaf Bachmann <obachman@…>, 23 years ago
* towards tailRings for local case git-svn-id: file:///usr/local/Singular/svn/trunk@4975 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 46.0 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: kstd1.cc,v 1.76 2000-12-21 16:37:49 obachman Exp $ */
5/*
6* ABSTRACT:
7*/
8
9// define if buckets should be used
10#define MORA_USE_BUCKETS
11
12// define if tailrings should be used
13// #define HAVE_TAIL_RING
14
15#include "mod2.h"
16#include "tok.h"
17#include "omalloc.h"
18#include "kutil.h"
19#include "kInline.cc"
20#include "polys.h"
21#include "febase.h"
22#include "kstd1.h"
23#include "khstd.h"
24#include "stairc.h"
25#include "weight.h"
26#include "cntrlc.h"
27#include "intvec.h"
28#include "ideals.h"
29#include "ipshell.h"
30#include "ipid.h"
31#include "timer.h"
32#include "lists.h"
33
34//#include "ipprint.h"
35
36/* the list of all options which give a warning by test */
37BITSET kOptions=Sy_bit(OPT_PROT)           /*  0 */
38                |Sy_bit(OPT_REDSB)         /*  1 */
39                |Sy_bit(OPT_NOT_SUGAR)     /*  3 */
40                |Sy_bit(OPT_INTERRUPT)     /*  4 */
41                |Sy_bit(OPT_SUGARCRIT)     /*  5 */
42                |Sy_bit(OPT_REDTHROUGH)
43                |Sy_bit(OPT_OLDSTD)
44                |Sy_bit(OPT_FASTHC)        /* 10 */
45                |Sy_bit(OPT_KEEPVARS)      /* 21 */
46                |Sy_bit(OPT_INTSTRATEGY)   /* 26 */
47                |Sy_bit(OPT_INFREDTAIL)    /* 28 */
48                |Sy_bit(OPT_NOTREGULARITY) /* 30 */
49                |Sy_bit(OPT_WEIGHTM);      /* 31 */
50
51/* the list of all options which may be used by option and test */
52BITSET validOpts=Sy_bit(0)
53                |Sy_bit(1)
54                |Sy_bit(2) // obachman 10/00: replaced by notBucket
55                |Sy_bit(3)
56                |Sy_bit(4)
57                |Sy_bit(5)
58                |Sy_bit(6)
59//                |Sy_bit(7) obachman 11/00 tossed: 12/00 used for redThrough
60  |Sy_bit(OPT_REDTHROUGH)
61//                |Sy_bit(8) obachman 11/00 tossed
62                |Sy_bit(9)
63                |Sy_bit(10)
64                |Sy_bit(11)
65                |Sy_bit(12)
66                |Sy_bit(13)
67                |Sy_bit(14)
68                |Sy_bit(15)
69                |Sy_bit(16)
70                |Sy_bit(17)
71                |Sy_bit(18)
72                |Sy_bit(19)
73//                |Sy_bit(20) obachman 11/00 tossed: 12/00 used for redOldStd
74  |Sy_bit(OPT_OLDSTD)
75                |Sy_bit(21)
76                |Sy_bit(22)
77                /*|Sy_bit(23)*/
78                /*|Sy_bit(24)*/
79                |Sy_bit(OPT_REDTAIL)
80                |Sy_bit(OPT_INTSTRATEGY)
81                |Sy_bit(27)
82                |Sy_bit(28)
83                |Sy_bit(29)
84                |Sy_bit(30)
85                |Sy_bit(31);
86
87//static BOOLEAN posInLOldFlag;
88           /*FALSE, if posInL == posInL10*/
89// returns TRUE if mora should use buckets, false otherwise
90static BOOLEAN kMoraUseBucket(kStrategy strat);
91
92static void kOptimizeLDeg(pLDegProc ldeg, kStrategy strat)
93{
94  if (strat->ak == 0 && !rIsSyzIndexRing(currRing))
95    strat->length_pLength = TRUE;
96  else
97    strat->length_pLength = FALSE;
98   
99  if ((ldeg == pLDeg0c && !rIsSyzIndexRing(currRing)) ||
100      (ldeg == pLDeg0 && strat->ak == 0))
101  {
102    strat->LDegLast = TRUE;
103  }
104  else
105  {
106    strat->LDegLast = FALSE;
107  }
108}
109
110 
111static int doRed (LObject* h, TObject* with,BOOLEAN intoT,kStrategy strat)
112{
113  poly hp;
114  int ret;
115#if KDEBUG > 0
116  kTest_L(h);
117  kTest_T(with);
118#endif
119  // Hmmm ... why do we do this -- polys from T should already be normalized
120  if (!TEST_OPT_INTSTRATEGY)
121    with->pNorm();
122#ifdef KDEBUG
123  if (TEST_OPT_DEBUG)
124  {
125    PrintS("reduce ");h->wrp();PrintS(" with ");with->wrp();PrintLn();
126  }
127#endif
128  if (intoT)
129  {
130    // need to do it exacly like this: otherwise
131    // we might get errors
132    LObject L= *h;
133    L.Copy();
134    h->GetP();
135    h->SetLength(strat->length_pLength);
136    ret = ksReducePoly(&L, with, strat->kNoetherTail(), NULL, strat);
137    if (ret)
138    {
139      if (ret < 0) return ret;
140      if (h->tailRing != strat->tailRing)
141        h->ShallowCopyDelete(strat->tailRing,
142                             pGetShallowCopyDeleteProc(h->tailRing,
143                                                       strat->tailRing));
144    }
145    enterT(*h,strat);
146    *h = L;
147  }
148  else
149    ret = ksReducePoly(h, with, strat->kNoetherTail(), NULL, strat);
150#ifdef KDEBUG
151  if (TEST_OPT_DEBUG)
152  {
153    PrintS("to ");h->wrp();PrintLn();
154  }
155#endif
156  return ret;
157}
158
159int redEcart (LObject* h,kStrategy strat)
160{
161  poly pi;
162  int i,at,reddeg,d,ei,li,ii;
163  int j = 0;
164  int pass = 0;
165
166  d = h->GetpFDeg()+ h->ecart;
167  reddeg = strat->LazyDegree+d;
168  h->SetShortExpVector();
169  while (1)
170  {
171    j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
172    if (j < 0)
173    {
174      if (strat->honey) h->SetLength(strat->length_pLength);
175      return 1;
176    }
177
178    ei = strat->T[j].ecart;
179    ii = j;
180
181    if (ei > h->ecart && ii < strat->tl)
182    {
183      li = strat->T[j].length;
184      // the polynomial to reduce with (up to the moment) is;
185      // pi with ecart ei and length li
186      // look for one with smaller ecart
187      i = j;
188      while (1)
189      {
190        /*- takes the first possible with respect to ecart -*/
191        i++;
192#if 1
193        if (i > strat->tl) break;
194        if ((strat->T[i].ecart < ei || (strat->T[i].ecart == ei &&
195                                        strat->T[i].length < li))
196            &&
197            p_LmShortDivisibleBy(strat->T[i].GetLmTailRing(), strat->sevT[i], h->GetLmTailRing(), ~h->sev, strat->tailRing))
198#else
199          j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h, i);
200        if (j < 0) break;
201        i = j;
202        if (strat->T[i].ecart < ei || (strat->T[i].ecart == ei &&
203                                        strat->T[i].length < li))
204#endif
205        {
206          // the polynomial to reduce with is now
207          ii = i;
208          ei = strat->T[i].ecart;
209          if (ei <= h->ecart) break;
210          li = strat->T[i].length;
211        }
212      }
213    }
214
215    // end of search: have to reduce with pi
216    if (ei > h->ecart)
217    {
218      // It is not possible to reduce h with smaller ecart;
219      // if possible h goes to the lazy-set L,i.e
220      // if its position in L would be not the last one
221      strat->fromT = TRUE;
222      if (!K_TEST_OPT_REDTHROUGH && strat->Ll >= 0) /*- L is not empty -*/
223      {
224        h->SetLmCurrRing();
225        if (strat->honey && strat->posInLDependsOnLength)
226          h->SetLength(strat->length_pLength);
227        assume(h->FDeg == h->pFDeg());
228        at = strat->posInL(strat->L,strat->Ll,h,strat);
229        if (at <= strat->Ll)
230        {
231          /*- h will not become the next element to reduce -*/
232          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
233#ifdef KDEBUG
234          if (TEST_OPT_DEBUG) Print(" ecart too big; -> L%d\n",at);
235#endif
236          h->Clear();
237          strat->fromT = FALSE;
238          return -1;
239        }
240      }
241    }
242
243    // now we finally can reduce
244    doRed(h,&(strat->T[ii]),strat->fromT,strat);
245    strat->fromT=FALSE;
246
247    // are we done ???
248    if (h->IsNull())
249    {
250      if (h->lcm!=NULL) pLmFree(h->lcm);
251      h->Clear();
252      return 0;
253    }
254
255    // NO!
256    h->SetShortExpVector();
257    h->SetpFDeg();
258    if (strat->honey)
259    {
260      if (ei <= h->ecart)
261        h->ecart = d-h->GetpFDeg();
262      else
263        h->ecart = d-h->GetpFDeg()+ei-h->ecart;
264    }
265    else
266      // this has the side effect of setting h->length
267      h->ecart = h->pLDeg(strat->LDegLast) - h->GetpFDeg();
268
269    if (strat->syzComp!=0)
270    {
271      if ((strat->syzComp>0) && (h->Comp() > strat->syzComp))
272      {
273        assume(h->MinComp() > strat->syzComp);
274        if (strat->honey) h->SetLength();
275#ifdef KDEBUG
276        if (TEST_OPT_DEBUG) PrintS(" > syzComp\n");
277#endif
278        return -2;
279      }
280    }
281    /*- try to reduce the s-polynomial -*/
282    pass++;
283    d = h->GetpFDeg()+h->ecart;
284    /*
285     *test whether the polynomial should go to the lazyset L
286     *-if the degree jumps
287     *-if the number of pre-defined reductions jumps
288     */
289    if (!K_TEST_OPT_REDTHROUGH && (strat->Ll >= 0)
290        && ((d >= reddeg) || (pass > strat->LazyPass)))
291    {
292      h->SetLmCurrRing();
293      if (strat->honey && strat->posInLDependsOnLength) 
294        h->SetLength(strat->length_pLength);
295      assume(h->FDeg == h->pFDeg());
296      at = strat->posInL(strat->L,strat->Ll,h,strat);
297      if (at <= strat->Ll)
298      {
299        if (kFindDivisibleByInS(strat->S, strat->sevS, strat->sl, h) < 0)
300        {
301          if (strat->honey && !strat->posInLDependsOnLength) 
302            h->SetLength(strat->length_pLength);
303          return 1;
304        }
305        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
306#ifdef KDEBUG
307        if (TEST_OPT_DEBUG) Print(" degree jumped; ->L%d\n",at);
308#endif
309        h->Clear();
310        return -1;
311      }
312    }
313    else if ((TEST_OPT_PROT) && (strat->Ll < 0) && (d >= reddeg))
314    {
315      Print(".%d",d);mflush();
316      reddeg = d+1;
317    }
318  }
319}
320
321/*2
322*reduces h with elements from T choosing  the first possible
323* element in t with respect to the given pDivisibleBy
324*/
325int redFirst (LObject* h,kStrategy strat)
326{
327  if (h->IsNull()) return 0;
328
329  int at, reddeg,d;
330  int pass = 0;
331  int j = 0;
332
333  if (! strat->homog)
334  {
335    d = h->GetpFDeg() + h->ecart;
336    reddeg = strat->LazyDegree+d;
337  }
338  h->SetShortExpVector();
339  while (1)
340  {
341    j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
342    if (j < 0)
343    {
344      h->SetDegStuffReturnLDeg(strat->LDegLast);
345      return 1;
346    }
347
348    if (!TEST_OPT_INTSTRATEGY)
349      strat->T[j].pNorm();
350#ifdef KDEBUG
351    if (TEST_OPT_DEBUG)
352    {
353      PrintS("reduce ");
354      h->wrp();
355      PrintS(" with ");
356      strat->T[j].wrp();
357    }
358#endif
359    ksReducePoly(h, &(strat->T[j]), strat->kNoetherTail(), NULL, strat);
360#ifdef KDEBUG
361    if (TEST_OPT_DEBUG)
362    {
363      PrintS(" to ");
364      wrp(h->p);
365      PrintLn();
366    }
367#endif
368    if (h->IsNull())
369    {
370      if (h->lcm!=NULL) pLmFree(h->lcm);
371      h->Clear();
372      return 0;
373    }
374    h->SetShortExpVector();
375
376    if ((strat->syzComp!=0) && !strat->honey)
377    {
378      if ((strat->syzComp>0) &&
379          (h->Comp() > strat->syzComp))
380      {
381        assume(h->MinComp() > strat->syzComp);
382#ifdef KDEBUG
383        if (TEST_OPT_DEBUG) PrintS(" > syzComp\n");
384#endif
385        if (strat->homog) 
386          h->SetDegStuffReturnLDeg(strat->LDegLast);
387        return -2;
388      }
389    }
390    if (!strat->homog)
391    {
392      if (!K_TEST_OPT_OLDSTD && strat->honey)
393      {
394        h->SetpFDeg();
395        if (strat->T[j].ecart <= h->ecart)
396          h->ecart = d - h->GetpFDeg();
397        else
398          h->ecart = d - h->GetpFDeg() + strat->T[j].ecart - h->ecart;
399     
400        d = h->GetpFDeg() + h->ecart;
401      }
402      else
403        d = h->SetDegStuffReturnLDeg(strat->LDegLast);
404      /*- try to reduce the s-polynomial -*/
405      pass++;
406      /*
407       *test whether the polynomial should go to the lazyset L
408       *-if the degree jumps
409       *-if the number of pre-defined reductions jumps
410       */
411      if (!K_TEST_OPT_REDTHROUGH && (strat->Ll >= 0)
412          && ((d >= reddeg) || (pass > strat->LazyPass)))
413      {
414        h->SetLmCurrRing();
415        if (strat->posInLDependsOnLength)
416          h->SetLength(strat->length_pLength);
417        at = strat->posInL(strat->L,strat->Ll,h,strat);
418        if (at <= strat->Ll)
419        {
420          if (kFindDivisibleByInS(strat->S, strat->sevS, strat->sl, h) < 0)
421            return 1;
422          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
423#ifdef KDEBUG
424          if (TEST_OPT_DEBUG) Print(" degree jumped; ->L%d\n",at);
425#endif
426          h->Clear();
427          return -1;
428        }
429      }
430      if ((TEST_OPT_PROT) && (strat->Ll < 0) && (d >= reddeg))
431      {
432        reddeg = d+1;
433        Print(".%d",d);mflush();
434      }
435    }
436  }
437}
438
439/*2
440* reduces h with elements from T choosing first possible
441* element in T with respect to the given ecart
442* used for computing normal forms outside kStd
443*/
444static poly redMoraNF (poly h,kStrategy strat, int flag)
445{
446  LObject H;
447  H.p = h;
448  int j = 0;
449  int z = 10;
450  int o = H.SetpFDeg();
451  H.ecart = pLDeg(H.p,&H.length)-o;
452  if (flag==0) cancelunit(&H);
453  H.sev = pGetShortExpVector(H.p);
454  unsigned long not_sev = ~ H.sev;
455  loop
456  {
457    if (j > strat->tl)
458    {
459      return H.p;
460    }
461    if (TEST_V_DEG_STOP)
462    {
463      if (kModDeg(H.p)>Kstd1_deg) pDeleteLm(&H.p);
464      if (H.p==NULL) return NULL;
465    }
466    if (p_LmShortDivisibleBy(strat->T[j].GetLmTailRing(), strat->sevT[j], H.GetLmTailRing(), not_sev, strat->tailRing))
467    {
468      //if (strat->interpt) test_int_std(strat->kIdeal);
469      /*- remember the found T-poly -*/
470      poly pi = strat->T[j].p;
471      int ei = strat->T[j].ecart;
472      int li = strat->T[j].length;
473      int ii = j;
474      /*
475      * the polynomial to reduce with (up to the moment) is;
476      * pi with ecart ei and length li
477      */
478      loop
479      {
480        /*- look for a better one with respect to ecart -*/
481        /*- stop, if the ecart is small enough (<=ecart(H)) -*/
482        j++;
483        if (j > strat->tl) break;
484        if (ei <= H.ecart) break;
485        if (((strat->T[j].ecart < ei)
486          || ((strat->T[j].ecart == ei)
487        && (strat->T[j].length < li)))
488        && pLmShortDivisibleBy(strat->T[j].p,strat->sevT[j], H.p, not_sev))
489        {
490          /*
491          * the polynomial to reduce with is now;
492          */
493          pi = strat->T[j].p;
494          ei = strat->T[j].ecart;
495          li = strat->T[j].length;
496          ii = j;
497        }
498      }
499      /*
500      * end of search: have to reduce with pi
501      */
502      z++;
503      if (z>10)
504      {
505        pNormalize(H.p);
506        z=0;
507      }
508      if ((ei > H.ecart) && (!strat->kHEdgeFound))
509      {
510        /*
511        * It is not possible to reduce h with smaller ecart;
512        * we have to reduce with bad ecart: H has to enter in T
513        */
514        doRed(&H,&(strat->T[ii]),TRUE,strat);
515        if (H.p == NULL)
516          return NULL;
517      }
518      else
519      {
520        /*
521        * we reduce with good ecart, h need not to be put to T
522        */
523        doRed(&H,&(strat->T[ii]),FALSE,strat);
524        if (H.p == NULL)
525          return NULL;
526      }
527      /*- try to reduce the s-polynomial -*/
528      o = H.SetpFDeg();
529      if (flag != 2) cancelunit(&H);
530      H.ecart = pLDeg(H.p,&(H.length))-o;
531      j = 0;
532      H.sev = pGetShortExpVector(H.p);
533      not_sev = ~ H.sev;
534    }
535    else
536    {
537      j++;
538    }
539  }
540}
541
542/*2
543*reorders  L with respect to posInL
544*/
545void reorderL(kStrategy strat)
546{
547  int i,j,at;
548  LObject p;
549
550  for (i=1; i<=strat->Ll; i++)
551  {
552    at = strat->posInL(strat->L,i-1,&(strat->L[i]),strat);
553    if (at != i)
554    {
555      p = strat->L[i];
556      for (j=i-1; j>=at; j--) strat->L[j+1] = strat->L[j];
557      strat->L[at] = p;
558    }
559  }
560}
561
562/*2
563*reorders  T with respect to length
564*/
565void reorderT(kStrategy strat)
566{
567  int i,j,at;
568  TObject p;
569  unsigned long sev;
570
571
572  for (i=1; i<=strat->tl; i++)
573  {
574    if (strat->T[i-1].length > strat->T[i].length)
575    {
576      p = strat->T[i];
577      sev = strat->sevT[i];
578      at = i-1;
579      loop
580      {
581        at--;
582        if (at < 0) break;
583        if (strat->T[i].length > strat->T[at].length) break;
584      }
585      for (j = i-1; j>at; j--)
586      {
587        strat->T[j+1]=strat->T[j];
588        strat->sevT[j+1]=strat->sevT[j];
589        strat->R[strat->T[j+1].i_r] = &(strat->T[j+1]);
590      }
591      strat->T[at+1]=p;
592      strat->sevT[at+1] = sev;
593      strat->R[p.i_r] = &(strat->T[at+1]);
594    }
595  }
596}
597
598/*2
599*looks whether exactly pVariables-1 axis are used
600*returns last != 0 in this case
601*last is the (first) unused axis
602*/
603void missingAxis (int* last,kStrategy strat)
604{
605  int   i = 0;
606  int   k = 0;
607
608  *last = 0;
609  loop
610  {
611    i++;
612    if (i > pVariables) break;
613    if (strat->NotUsedAxis[i])
614    {
615      *last = i;
616      k++;
617    }
618    if (k>1)
619    {
620      *last = 0;
621      break;
622    }
623  }
624}
625
626/*2
627*last is the only non used axis, it looks
628*for a monomial in p being a pure power of this
629*variable and returns TRUE in this case
630*(*length) gives the length between the pure power and the leading term
631*(should be minimal)
632*/
633BOOLEAN hasPurePower (const poly p,int last, int *length,kStrategy strat)
634{
635  poly h;
636  int i;
637
638  if (pNext(p) == strat->tail)
639    return FALSE;
640  pp_Test(p, currRing, strat->tailRing);
641  if (strat->ak <= 0 || p_MinComp(p, currRing, strat->tailRing) == strat->ak)
642  {
643    i = p_IsPurePower(p, currRing);
644    if (i == last) 
645    {
646      *length = 0;
647      return TRUE;
648    }
649    *length = 1;
650    h = pNext(p);
651    while (h != NULL)
652    {
653      i = p_IsPurePower(h, strat->tailRing);
654      if (i==last) return TRUE;
655      (*length)++;
656      pIter(h);
657    }
658  }
659  return FALSE;
660}
661
662BOOLEAN hasPurePower (LObject *L,int last, int *length,kStrategy strat)
663{
664  if (L->bucket != NULL)
665  {
666    poly p = L->CanonicalizeP();
667    BOOLEAN ret = hasPurePower(p, last, length, strat);
668    pNext(p) = NULL;
669    return ret;
670  }
671  else 
672  {
673    return hasPurePower(L->p, last, length, strat);
674  }
675}
676
677/*2
678* looks up the position of polynomial p in L
679* in the case of looking for the pure powers
680*/
681int posInL10 (LSet const set, int length, LObject* p,kStrategy const strat)
682{
683  int j,dp,dL;
684
685  if (length<0) return 0;
686  if (hasPurePower(p,strat->lastAxis,&dp,strat))
687  {
688    int op= p->GetpFDeg() +p->ecart;
689    for (j=length; j>=0; j--)
690    {
691      if (!hasPurePower(&(set[j]),strat->lastAxis,&dL,strat))
692        return j+1;
693      if (dp < dL)
694        return j+1;
695      if ((dp == dL)
696          && (set[j].GetpFDeg()+set[j].ecart >= op))
697        return j+1;
698    }
699  }
700  j=length;
701  loop
702  {
703    if (j<0) break;
704    if (!hasPurePower(&(set[j]),strat->lastAxis,&dL,strat)) break;
705    j--;
706  }
707  return strat->posInLOld(set,j,p,strat);
708}
709
710
711/*2
712* computes the s-polynomials L[ ].p in L
713*/
714void updateL(kStrategy strat)
715{
716  LObject p;
717  int dL;
718  int j=strat->Ll;
719  loop
720  {
721    if (j<0) break;
722    if (hasPurePower(&(strat->L[j]),strat->lastAxis,&dL,strat))
723    {
724      p=strat->L[strat->Ll];
725      strat->L[strat->Ll]=strat->L[j];
726      strat->L[j]=p;
727      break;
728    }
729    j--;
730  }
731  if (j<0)
732  {
733    j=strat->Ll;
734    loop
735    {
736      if (j<0) break;
737      if (pNext(strat->L[j].p) == strat->tail)
738      {
739        pLmFree(strat->L[j].p);    /*deletes the short spoly and computes*/
740        poly m1 = NULL, m2 = NULL;
741        // check that spoly creation is ok
742        while (strat->tailRing != currRing && 
743               !kCheckSpolyCreation(&(strat->L[j]), strat, m1, m2))
744        {
745          assume(m1 == NULL && m2 == NULL);
746          // if not, change to a ring where exponents are at least
747          // large enough
748          kStratChangeTailRing(strat);
749        }
750        /* create the real one */
751        ksCreateSpoly(&(strat->L[j]), strat->kNoetherTail(), FALSE, 
752                      strat->tailRing, m1, m2, strat->R);
753
754        if (!strat->honey)
755          strat->initEcart(&strat->L[j]);
756        else
757          strat->L[j].SetLength(strat->length_pLength);
758       
759        BOOLEAN pp = hasPurePower(&(strat->L[j]),strat->lastAxis,&dL,strat);
760       
761        if (strat->use_buckets) strat->L[j].PrepareRed(TRUE);
762       
763        if (pp)
764        {
765          p=strat->L[strat->Ll];
766          strat->L[strat->Ll]=strat->L[j];
767          strat->L[j]=p;
768          break;
769        }
770      }
771      j--;
772    }
773  }
774}
775
776/*2
777* computes the s-polynomials L[ ].p in L and
778* cuts elements in L above noether
779*/
780void updateLHC(kStrategy strat)
781{
782  int i = 0;
783  kTest_TS(strat);
784  while (i <= strat->Ll)
785  {
786    if (pNext(strat->L[i].p) == strat->tail)
787    {
788       /*- deletes the int spoly and computes -*/
789      if (pLmCmp(strat->L[i].p,strat->kNoether) == -1)
790      {
791        pLmFree(strat->L[i].p);
792        strat->L[i].p = NULL;
793      }
794      else
795      {
796        pLmFree(strat->L[i].p);
797        poly m1 = NULL, m2 = NULL;
798        // check that spoly creation is ok
799        while (strat->tailRing != currRing && 
800               !kCheckSpolyCreation(&(strat->L[i]), strat, m1, m2))
801        {
802          assume(m1 == NULL && m2 == NULL);
803          // if not, change to a ring where exponents are at least
804          // large enough
805          kStratChangeTailRing(strat);
806        }
807        /* create the real one */
808        ksCreateSpoly(&(strat->L[i]), strat->kNoetherTail(), FALSE, 
809                      strat->tailRing, m1, m2, strat->R);
810        if (! strat->L[i].IsNull())
811        {
812          strat->L[i].SetpFDeg();
813          strat->L[i].ecart
814            = strat->L[i].pLDeg(strat->LDegLast) - strat->L[i].GetpFDeg();
815          if (strat->use_buckets) strat->L[i].PrepareRed(TRUE);
816        }
817      }
818    }
819    else
820      deleteHC(&(strat->L[i]), strat);
821   if (strat->L[i].IsNull())
822      deleteInL(strat->L,&strat->Ll,i,strat);
823    else
824    {
825#ifdef KDEBUG
826      kTest_L(&(strat->L[i]), strat->tailRing, TRUE, i, strat->T, strat->tl);
827#endif
828      i++;
829    }
830  }
831  kTest_TS(strat);
832}
833
834/*2
835* cuts in T above strat->kNoether and tries to cancel a unit
836*/
837void updateT(kStrategy strat)
838{
839  int i = 0;
840  LObject p;
841
842  while (i <= strat->tl)
843  {
844    p = strat->T[i];
845    deleteHC(&p,strat, TRUE);
846    /*- tries to cancel a unit: -*/
847    cancelunit(&p);
848    if (p.p != strat->T[i].p)
849    {
850      strat->sevT[i] = pGetShortExpVector(p.p);
851      p.SetpFDeg();
852    }
853    strat->T[i] = p;
854    i++;
855  }
856}
857
858/*2
859* arranges red, pos and T if strat->kHEdgeFound (first time)
860*/
861void firstUpdate(kStrategy strat)
862{
863  if (strat->update)
864  {
865    kTest_TS(strat);
866    strat->update = (strat->tl == -1);
867    if (TEST_OPT_WEIGHTM)
868    {
869      pRestoreDegProcs(pFDegOld, pLDegOld);
870      if (strat->tailRing != currRing)
871      {
872        strat->tailRing->pFDeg = strat->pOrigFDeg_TailRing;
873        strat->tailRing->pLDeg = strat->pOrigLDeg_TailRing;
874      }
875      int i;
876      for (i =0; i<=strat->Ll; i++)
877      {
878        strat->L[i].SetpFDeg();
879      }
880      for (i=0; i<=strat->tl; i++)
881      {
882        strat->T[i].SetpFDeg();
883      }
884      if (ecartWeights)
885      {
886        omFreeSize((ADDRESS)ecartWeights,(pVariables+1)*sizeof(short));
887        ecartWeights=NULL;
888      }
889    }
890    if (TEST_OPT_FASTHC)
891    {
892      strat->posInL = strat->posInLOld;
893      strat->lastAxis = 0;
894    }
895    if (BTEST1(27))
896      return;
897    strat->red = redFirst;
898    strat->use_buckets = kMoraUseBucket(strat);
899    updateT(strat);
900    strat->posInT = posInT2;
901    reorderT(strat);
902  }
903  kTest_TS(strat);
904}
905
906/*2
907*-puts p to the standardbasis s at position at
908*-reduces the tail of p if TEST_OPT_REDTAIL
909*-tries to cancel a unit
910*-HEckeTest
911*  if TRUE
912*  - decides about reduction-strategies
913*  - computes noether
914*  - stops computation if BTEST1(27)
915*  - cuts the tails of the polynomials
916*    in s,t and the elements in L above noether
917*    and cancels units if possible
918*  - reorders s,L
919*/
920void enterSMora (LObject p,int atS,kStrategy strat, int atR = -1)
921{
922  int i;
923  enterSBba(p, atS, strat, atR);
924  if (TEST_OPT_DEBUG)
925  {
926    Print("new s%d:",atS);
927    wrp(p.p);
928    PrintLn();
929  }
930  if ((!strat->kHEdgeFound) || (strat->kNoether!=NULL)) HEckeTest(p.p,strat);
931  if (strat->kHEdgeFound)
932  {
933    if (newHEdge(strat->S,strat->ak,strat))
934    {
935      firstUpdate(strat);
936      if (BTEST1(27))
937        return;
938      /*- cuts elements in L above noether and reorders L -*/
939      updateLHC(strat);
940      /*- reorders L with respect to posInL -*/
941      reorderL(strat);
942    }
943  }
944  else if (strat->kNoether!=NULL)
945    strat->kHEdgeFound = TRUE;
946  else if (TEST_OPT_FASTHC)
947  {
948    if (strat->posInLOldFlag)
949    {
950      missingAxis(&strat->lastAxis,strat);
951      if (strat->lastAxis)
952      {
953        strat->posInLOld = strat->posInL;
954        strat->posInLOldFlag = FALSE;
955        strat->posInL = posInL10;
956        strat->posInLDependsOnLength = TRUE;
957        updateL(strat);
958        reorderL(strat);
959      }
960    }
961    else if (strat->lastAxis)
962      updateL(strat);
963  }
964}
965
966/*2
967*-puts p to the standardbasis s at position at
968*-HEckeTest
969*  if TRUE
970*  - computes noether
971*/
972void enterSMoraNF (LObject p, int atS,kStrategy strat, int atR = -1)
973{
974  int i;
975
976  enterSBba(p, atS, strat, atR);
977  if ((!strat->kHEdgeFound) || (strat->kNoether!=NULL)) HEckeTest(p.p,strat);
978  if (strat->kHEdgeFound)
979    newHEdge(strat->S,strat->ak,strat);
980  else if (strat->kNoether!=NULL)
981    strat->kHEdgeFound = TRUE;
982}
983
984
985void initMora(ideal F,kStrategy strat)
986{
987  int i,j;
988  idhdl h;
989
990  strat->NotUsedAxis = (BOOLEAN *)omAlloc((pVariables+1)*sizeof(BOOLEAN));
991  for (j=pVariables; j>0; j--) strat->NotUsedAxis[j] = TRUE;
992  strat->enterS = enterSMora;
993  strat->initEcartPair = initEcartPairMora; /*- ecart approximation -*/
994  strat->posInLOld = strat->posInL;
995  strat->posInLOldFlag = TRUE;
996  strat->initEcart = initEcartNormal;
997  strat->kHEdgeFound = ppNoether != NULL;
998  if ( strat->kHEdgeFound )
999     strat->kNoether = pCopy(ppNoether);
1000  else if (strat->kHEdgeFound || strat->homog)
1001    strat->red = redFirst;  /*take the first possible in T*/
1002  else
1003    strat->red = redEcart;/*take the first possible in under ecart-restriction*/
1004  if (strat->kHEdgeFound)
1005  {
1006    strat->HCord = pFDeg(ppNoether)+1;
1007    strat->posInT = posInT2;
1008  }
1009  else
1010  {
1011    strat->HCord = 32000;/*- very large -*/
1012  }
1013  /*reads the ecartWeights used for Graebes method from the
1014   *intvec ecart and set ecartWeights
1015   */
1016  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1017  {
1018    //interred  machen   Aenderung
1019    pFDegOld=pFDeg;
1020    pLDegOld=pLDeg;
1021    h=ggetid("ecart");
1022    if ((h!=NULL) && (IDTYP(h)==INTVEC_CMD))
1023    {
1024      ecartWeights=iv2array(IDINTVEC(h));
1025    }
1026    else
1027    {
1028      ecartWeights=(short *)omAlloc((pVariables+1)*sizeof(short));
1029      /*uses automatic computation of the ecartWeights to set them*/
1030      kEcartWeights(F->m,IDELEMS(F)-1,ecartWeights);
1031    }
1032   
1033    pSetDegProcs(totaldegreeWecart, maxdegreeWecart);
1034    if (TEST_OPT_PROT)
1035    {
1036      for(i=1; i<=pVariables; i++)
1037        Print(" %d",ecartWeights[i]);
1038      PrintLn();
1039      mflush();
1040    }
1041  }
1042  kOptimizeLDeg(pLDeg, strat);
1043}
1044
1045#ifdef HAVE_ASSUME
1046static int mora_count = 0;
1047static int mora_loop_count;
1048#endif
1049
1050ideal mora (ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat)
1051{
1052#ifdef HAVE_ASSUME
1053  mora_count++;
1054  mora_loop_count = 0;
1055#endif
1056#ifdef KDEBUG
1057  om_Opts.MinTrack = 5;
1058#endif
1059  int srmax;
1060  int lrmax = 0;
1061  int olddeg = 0;
1062  int reduc = 0;
1063  int red_result = 1;
1064  int hilbeledeg=1,hilbcount=0;
1065
1066  strat->update = TRUE;
1067  /*- setting global variables ------------------- -*/
1068  initBuchMoraCrit(strat);
1069  initHilbCrit(F,Q,&hilb,strat);
1070  initMora(F,strat);
1071  initBuchMoraPos(strat);
1072  /*Shdl=*/initBuchMora(F,Q,strat);
1073  if (TEST_OPT_FASTHC) missingAxis(&strat->lastAxis,strat);
1074  /*updateS in initBuchMora has Hecketest
1075  * and could have put strat->kHEdgdeFound FALSE*/
1076  if (ppNoether!=NULL)
1077  {
1078    strat->kHEdgeFound = TRUE;
1079  }
1080  if (strat->kHEdgeFound && strat->update)
1081  {
1082    firstUpdate(strat);
1083    updateLHC(strat);
1084    reorderL(strat);
1085  }
1086  if (TEST_OPT_FASTHC && (strat->lastAxis) && strat->posInLOldFlag)
1087  {
1088    strat->posInLOld = strat->posInL;
1089    strat->posInLOldFlag = FALSE;
1090    strat->posInL = posInL10;
1091    updateL(strat);
1092    reorderL(strat);
1093  }
1094  srmax = strat->sl;
1095  kTest_TS(strat);
1096  strat->use_buckets = kMoraUseBucket(strat);
1097  /*- compute-------------------------------------------*/
1098
1099#ifdef HAVE_TAIL_RING
1100//  if (strat->homog && strat->red == redFirst)
1101    kStratInitChangeTailRing(strat);
1102#endif 
1103 
1104  while (strat->Ll >= 0)
1105  {
1106#ifdef HAVE_ASSUME
1107    mora_loop_count++;
1108#endif
1109    if (lrmax< strat->Ll) lrmax=strat->Ll; /*stat*/
1110    //test_int_std(strat->kIdeal);
1111    if (TEST_OPT_DEBUG) messageSets(strat);
1112    if (TEST_OPT_DEGBOUND
1113    && (strat->L[strat->Ll].ecart+strat->L[strat->Ll].GetpFDeg()> Kstd1_deg))
1114    {
1115      /*
1116      * stops computation if
1117      * - 24 (degBound)
1118      *   && upper degree is bigger than Kstd1_deg
1119      */
1120      while ((strat->Ll >= 0)
1121        && (strat->L[strat->Ll].ecart+strat->L[strat->Ll].GetpFDeg()> Kstd1_deg)
1122        && (strat->L[strat->Ll].p1!=NULL) && (strat->L[strat->Ll].p2!=NULL))
1123      {
1124        deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
1125        //if (TEST_OPT_PROT)
1126        //{
1127        //   PrintS("D"); mflush();
1128        //}
1129      }
1130      if (strat->Ll<0) break;
1131    }
1132    strat->P = strat->L[strat->Ll];/*- picks the last element from the lazyset L -*/
1133    if (strat->Ll==0) strat->interpt=TRUE;
1134    strat->Ll--;
1135
1136    // create the real Spoly
1137    if (pNext(strat->P.p) == strat->tail)
1138    {
1139      /*- deletes the short spoly and computes -*/
1140      pLmFree(strat->P.p);
1141      strat->P.p = NULL;
1142      poly m1 = NULL, m2 = NULL;
1143      // check that spoly creation is ok
1144      while (strat->tailRing != currRing && 
1145             !kCheckSpolyCreation(&(strat->P), strat, m1, m2))
1146      {
1147        assume(m1 == NULL && m2 == NULL);
1148        // if not, change to a ring where exponents are large enough
1149        kStratChangeTailRing(strat);
1150      }
1151      /* create the real one */
1152      ksCreateSpoly(&(strat->P), strat->kNoetherTail(), strat->use_buckets, 
1153                    strat->tailRing, m1, m2, strat->R);
1154      if (!strat->use_buckets)
1155        strat->P.SetLength(strat->length_pLength);
1156    }
1157    else if (strat->P.p1 == NULL)
1158    {
1159      // for input polys, prepare reduction (buckets !)
1160      strat->P.SetLength(strat->length_pLength);
1161      strat->P.PrepareRed(strat->use_buckets);
1162    }
1163   
1164    if (!strat->P.IsNull())
1165    {
1166      // might be NULL from noether !!!
1167      if (TEST_OPT_PROT) 
1168        message(strat->P.ecart+strat->P.GetpFDeg(),&olddeg,&reduc,strat, red_result);
1169      // reduce
1170      red_result = strat->red(&strat->P,strat);
1171    }
1172
1173    if (! strat->P.IsNull())
1174    {
1175      strat->P.GetP();
1176      // statistics
1177      if (TEST_OPT_PROT) PrintS("s");
1178      // normalization
1179      if (!TEST_OPT_INTSTRATEGY)
1180        strat->P.pNorm();
1181      // tailreduction
1182      strat->P.p = redtail(&(strat->P),strat->sl,strat);
1183      // set ecart -- might have changed because of tail reductions
1184      if ((!strat->noTailReduction) && (!strat->honey))
1185        strat->initEcart(&strat->P);
1186      // for char 0, clear denominators
1187      if (TEST_OPT_INTSTRATEGY)
1188        strat->P.pCleardenom();
1189      // cancel unit
1190      cancelunit(&strat->P);
1191
1192      // put in T
1193      enterT(strat->P,strat);
1194      // build new pairs
1195      enterpairs(strat->P.p,strat->sl,strat->P.ecart,0,strat, strat->tl);
1196      // put in S
1197      strat->enterS(strat->P, posInS(strat->S,strat->sl,strat->P.p),
1198                    strat, strat->tl);
1199
1200      // apply hilbert criterion
1201      if (hilb!=NULL) khCheck(Q,w,hilb,hilbeledeg,hilbcount,strat);
1202
1203      // clear strat->P
1204      if (strat->P.lcm!=NULL) pLmFree(strat->P.lcm);
1205      strat->P.lcm=NULL;
1206#ifdef KDEBUG
1207      // make sure kTest_TS does not complain about strat->P
1208      memset(&strat->P,0,sizeof(strat->P));
1209#endif
1210      if (strat->sl>srmax) srmax = strat->sl; /*stat.*/
1211      if (strat->Ll>lrmax) lrmax = strat->Ll;
1212    }
1213    if (strat->kHEdgeFound)
1214    {
1215      if ((BTEST1(27))
1216      || ((TEST_OPT_MULTBOUND) && (scMult0Int((strat->Shdl)) < mu)))
1217      {
1218        // obachman: is this still used ???
1219        /*
1220        * stops computation if strat->kHEdgeFound and
1221        * - 27 (finiteDeterminacyTest)
1222        * or
1223        * - 23
1224        *   (multBound)
1225        *   && multiplicity of the ideal is smaller then a predefined number mu
1226        */
1227        while (strat->Ll >= 0) deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
1228      }
1229    }
1230    kTest_TS(strat);
1231  }
1232  /*- complete reduction of the standard basis------------------------ -*/
1233  if (TEST_OPT_REDSB) completeReduce(strat);
1234  /*- release temp data------------------------------- -*/
1235  exitBuchMora(strat);
1236  /*- polynomials used for HECKE: HC, noether -*/
1237  if (BTEST1(27))
1238  {
1239    if (strat->kHEdge!=NULL)
1240      Kstd1_mu=pFDeg(strat->kHEdge);
1241    else
1242      Kstd1_mu=-1;
1243  }
1244  pDelete(&strat->kHEdge);
1245  strat->update = TRUE; //???
1246  strat->lastAxis = 0; //???
1247  pDelete(&strat->kNoether);
1248  omFreeSize((ADDRESS)strat->NotUsedAxis,(pVariables+1)*sizeof(BOOLEAN));
1249  if (TEST_OPT_PROT) messageStat(srmax,lrmax,hilbcount,strat);
1250  if (TEST_OPT_WEIGHTM)
1251  {
1252    pRestoreDegProcs(pFDegOld, pLDegOld);
1253    if (ecartWeights)
1254    {
1255      omFreeSize((ADDRESS)ecartWeights,(pVariables+1)*sizeof(short));
1256      ecartWeights=NULL;
1257    }
1258  }
1259  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
1260  idTest(strat->Shdl);
1261  return (strat->Shdl);
1262}
1263
1264poly kNF1 (ideal F,ideal Q,poly q, kStrategy strat, int lazyReduce)
1265{
1266  poly   p;
1267  int   i;
1268  int   j;
1269  int   o;
1270  LObject   h;
1271  BITSET save_test=test;
1272
1273  if ((idIs0(F))&&(Q==NULL))
1274    return pCopy(q); /*F=0*/
1275  strat->ak = max(idRankFreeModule(F),pMaxComp(q));
1276  /*- creating temp data structures------------------- -*/
1277  strat->kHEdgeFound = ppNoether != NULL;
1278  strat->kNoether    = pCopy(ppNoether);
1279  test|=Sy_bit(OPT_REDTAIL);
1280  test&=~Sy_bit(OPT_INTSTRATEGY);
1281  if (TEST_OPT_STAIRCASEBOUND
1282  && (! TEST_V_DEG_STOP)
1283  && (0<Kstd1_deg)
1284  && ((!strat->kHEdgeFound)
1285    ||(TEST_OPT_DEGBOUND && (pWTotaldegree(strat->kNoether)<Kstd1_deg))))
1286  {
1287    pDelete(&strat->kNoether);
1288    strat->kNoether=pOne();
1289    pSetExp(strat->kNoether,1, Kstd1_deg+1);
1290    pSetm(strat->kNoether);
1291    strat->kHEdgeFound=TRUE;
1292  }
1293  initBuchMoraCrit(strat);
1294  initBuchMoraPos(strat);
1295  initMora(F,strat);
1296  strat->enterS = enterSMoraNF;
1297  /*- set T -*/
1298  strat->tl = -1;
1299  strat->tmax = setmax;
1300  strat->T = initT();
1301  strat->R = initR();
1302  strat->sevT = initsevT();
1303  /*- set S -*/
1304  strat->sl = -1;
1305  /*- init local data struct.-------------------------- -*/
1306  /*Shdl=*/initS(F,Q,strat);
1307  if ((strat->ak!=0)
1308  && (strat->kHEdgeFound))
1309  {
1310    if (strat->ak!=1)
1311    {
1312      pSetComp(strat->kNoether,1);
1313      pSetmComp(strat->kNoether);
1314      poly p=pHead(strat->kNoether);
1315      pSetComp(p,strat->ak);
1316      pSetmComp(p);
1317      p=pAdd(strat->kNoether,p);
1318      strat->kNoether=pNext(p);
1319      p_LmFree(p,currRing);
1320    }
1321  }
1322  if ((lazyReduce & 1)==0)
1323  {
1324    for (i=strat->sl; i>=0; i--)
1325      pNorm(strat->S[i]);
1326  }
1327  /*- puts the elements of S also to T -*/
1328  for (i=0; i<=strat->sl; i++)
1329  {
1330    h.p = strat->S[i];
1331    h.ecart = strat->ecartS[i];
1332    if (strat->sevS[i] == 0) strat->sevS[i] = pGetShortExpVector(h.p);
1333    else assume(strat->sevS[i] == pGetShortExpVector(h.p));
1334    h.length = pLength(h.p);
1335    h.sev = strat->sevS[i];
1336    h.SetpFDeg();
1337    enterT(h,strat);
1338  }
1339  /*- compute------------------------------------------- -*/
1340  p = pCopy(q);
1341  deleteHC(&p,&o,&j,strat);
1342  if (TEST_OPT_PROT) { PrintS("r"); mflush(); }
1343  if (p!=NULL) p = redMoraNF(p,strat, lazyReduce & 2);
1344  if ((p!=NULL)&&((lazyReduce & 1)==0))
1345  {
1346    if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
1347    p = redtail(p,strat->sl,strat);
1348  }
1349  /*- release temp data------------------------------- -*/
1350  cleanT(strat);
1351  omFreeSize((ADDRESS)strat->T,strat->tmax*sizeof(TObject));
1352  omFreeSize((ADDRESS)strat->ecartS,IDELEMS(strat->Shdl)*sizeof(int));
1353  omFreeSize((ADDRESS)strat->sevS,IDELEMS(strat->Shdl)*sizeof(unsigned long));
1354  omFreeSize((ADDRESS)strat->NotUsedAxis,(pVariables+1)*sizeof(BOOLEAN));
1355  omfree(strat->sevT);
1356  omfree(strat->S_2_R);
1357  omfree(strat->R);
1358
1359  if ((Q!=NULL)&&(strat->fromQ!=NULL))
1360  {
1361    i=((IDELEMS(Q)+IDELEMS(F)+15)/16)*16;
1362    omFreeSize((ADDRESS)strat->fromQ,i*sizeof(int));
1363    strat->fromQ=NULL;
1364  }
1365  pDelete(&strat->kHEdge);
1366  pDelete(&strat->kNoether);
1367  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1368  {
1369    pRestoreDegProcs(pFDegOld, pLDegOld);
1370    if (ecartWeights)
1371    {
1372      omFreeSize((ADDRESS *)&ecartWeights,(pVariables+1)*sizeof(short));
1373      ecartWeights=NULL;
1374    }
1375  }
1376  idDelete(&strat->Shdl);
1377  test=save_test;
1378  if (TEST_OPT_PROT) PrintLn();
1379  return p;
1380}
1381
1382ideal kNF1 (ideal F,ideal Q,ideal q, kStrategy strat, int lazyReduce)
1383{
1384  poly   p;
1385  int   i;
1386  int   j;
1387  int   o;
1388  LObject   h;
1389  ideal res;
1390  BITSET save_test=test;
1391
1392  if (idIs0(q)) return idInit(1,q->rank);
1393  if ((idIs0(F))&&(Q==NULL))
1394    return idCopy(q); /*F=0*/
1395  strat->ak = max(idRankFreeModule(F),idRankFreeModule(q));
1396  /*- creating temp data structures------------------- -*/
1397  strat->kHEdgeFound = ppNoether != NULL;
1398  strat->kNoether=pCopy(ppNoether);
1399  test|=Sy_bit(OPT_REDTAIL);
1400  if (TEST_OPT_STAIRCASEBOUND
1401  && (0<Kstd1_deg)
1402  && ((!strat->kHEdgeFound)
1403    ||(TEST_OPT_DEGBOUND && (pWTotaldegree(strat->kNoether)<Kstd1_deg))))
1404  {
1405    pDelete(&strat->kNoether);
1406    strat->kNoether=pOne();
1407    pSetExp(strat->kNoether,1, Kstd1_deg+1);
1408    pSetm(strat->kNoether);
1409    strat->kHEdgeFound=TRUE;
1410  }
1411  initBuchMoraCrit(strat);
1412  initBuchMoraPos(strat);
1413  initMora(F,strat);
1414  strat->enterS = enterSMoraNF;
1415  /*- set T -*/
1416  strat->tl = -1;
1417  strat->tmax = setmax;
1418  strat->T = initT();
1419  strat->R = initR();
1420  strat->sevT = initsevT();
1421  /*- set S -*/
1422  strat->sl = -1;
1423  /*- init local data struct.-------------------------- -*/
1424  /*Shdl=*/initS(F,Q,strat);
1425  if ((strat->ak!=0)
1426  && (strat->kHEdgeFound))
1427  {
1428    if (strat->ak!=1)
1429    {
1430      pSetComp(strat->kNoether,1);
1431      pSetmComp(strat->kNoether);
1432      poly p=pHead(strat->kNoether);
1433      pSetComp(p,strat->ak);
1434      pSetmComp(p);
1435      p=pAdd(strat->kNoether,p);
1436      strat->kNoether=pNext(p);
1437      p_LmFree(p,currRing);
1438    }
1439  }
1440  if (TEST_OPT_INTSTRATEGY && ((lazyReduce & 1)==0))
1441  {
1442    for (i=strat->sl; i>=0; i--)
1443      pNorm(strat->S[i]);
1444  }
1445  /*- compute------------------------------------------- -*/
1446  res=idInit(IDELEMS(q),q->rank);
1447  for (i=0; i<IDELEMS(q); i++)
1448  {
1449    if (q->m[i]!=NULL)
1450    {
1451      p = pCopy(q->m[i]);
1452      deleteHC(&p,&o,&j,strat);
1453      if (p!=NULL)
1454      {
1455        /*- puts the elements of S also to T -*/
1456        for (j=0; j<=strat->sl; j++)
1457        {
1458          h.p = strat->S[j];
1459          h.ecart = strat->ecartS[j];
1460          h.pLength = h.length = pLength(h.p);
1461          if (strat->sevS[j] == 0) strat->sevS[j] = pGetShortExpVector(h.p);
1462          else assume(strat->sevS[j] == pGetShortExpVector(h.p));
1463          h.sev = strat->sevS[j];
1464          h.SetpFDeg();
1465          enterT(h,strat);
1466        }
1467        if (TEST_OPT_PROT) { PrintS("r"); mflush(); }
1468        p = redMoraNF(p,strat, lazyReduce & 2);
1469        if ((p!=NULL)&&((lazyReduce & 1)==0))
1470        {
1471          if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
1472          p = redtail(p,strat->sl,strat);
1473        }
1474        cleanT(strat);
1475      }
1476      res->m[i]=p;
1477    }
1478    //else
1479    //  res->m[i]=NULL;
1480  }
1481  /*- release temp data------------------------------- -*/
1482  omFreeSize((ADDRESS)strat->T,strat->tmax*sizeof(TObject));
1483  omFreeSize((ADDRESS)strat->ecartS,IDELEMS(strat->Shdl)*sizeof(int));
1484  omFreeSize((ADDRESS)strat->sevS,IDELEMS(strat->Shdl)*sizeof(unsigned long));
1485  omFreeSize((ADDRESS)strat->NotUsedAxis,(pVariables+1)*sizeof(BOOLEAN));
1486  omfree(strat->sevT);
1487  omfree(strat->S_2_R);
1488  omfree(strat->R);
1489  if ((Q!=NULL)&&(strat->fromQ!=NULL))
1490  {
1491    i=((IDELEMS(Q)+IDELEMS(F)+15)/16)*16;
1492    omFreeSize((ADDRESS)strat->fromQ,i*sizeof(int));
1493    strat->fromQ=NULL;
1494  }
1495  pDelete(&strat->kHEdge);
1496  pDelete(&strat->kNoether);
1497  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1498  {
1499    pFDeg=pFDegOld;
1500    pLDeg=pLDegOld;
1501    if (ecartWeights)
1502    {
1503      omFreeSize((ADDRESS *)&ecartWeights,(pVariables+1)*sizeof(short));
1504      ecartWeights=NULL;
1505    }
1506  }
1507  idDelete(&strat->Shdl);
1508  test=save_test;
1509  if (TEST_OPT_PROT) PrintLn();
1510  return res;
1511}
1512
1513pFDegProc pFDegOld;
1514pLDegProc pLDegOld;
1515intvec * kModW, * kHomW;
1516
1517long kModDeg(poly p, ring r)
1518{
1519  long o=pWDegree(p, r);
1520  long i=p_GetComp(p, r);
1521  if (i==0) return o;
1522  return o+(*kModW)[i-1];
1523}
1524long kHomModDeg(poly p, ring r)
1525{
1526  int i;
1527  long j=0;
1528
1529  for (i=r->N;i>0;i--)
1530    j+=p_GetExp(p,i,r)*(*kHomW)[i-1];
1531  if (kModW == NULL) return j;
1532  i = p_GetComp(p,r);
1533  if (i==0) return j;
1534  return j+(*kModW)[i-1];
1535}
1536
1537ideal kStd(ideal F, ideal Q, tHomog h,intvec ** w, intvec *hilb,int syzComp,
1538          int newIdeal, intvec *vw)
1539{
1540  ideal r;
1541  BOOLEAN b=pLexOrder,toReset=FALSE;
1542  BOOLEAN delete_w=(w==NULL);
1543  kStrategy strat=new skStrategy;
1544
1545  if(!TEST_OPT_RETURN_SB)
1546    strat->syzComp = syzComp;
1547  if (TEST_OPT_SB_1)
1548    strat->newIdeal = newIdeal;
1549  if (rField_has_simple_inverse())
1550    strat->LazyPass=20;
1551  else
1552    strat->LazyPass=2;
1553  strat->LazyDegree = 1;
1554  strat->ak = idRankFreeModule(F);
1555  strat->kModW=kModW=NULL;
1556  strat->kHomW=kHomW=NULL;
1557  if (vw != NULL)
1558  {
1559    pLexOrder=FALSE;
1560    strat->kHomW=kHomW=vw;
1561    pFDegOld = pFDeg;
1562    pLDegOld = pLDeg;
1563    pSetDegProcs(kHomModDeg);
1564    toReset = TRUE;
1565  }
1566  if ((h==testHomog)
1567  )
1568  {
1569    if (strat->ak == 0)
1570    {
1571      h = (tHomog)idHomIdeal(F,Q);
1572      w=NULL;
1573    }
1574    else
1575    {
1576      h = (tHomog)idHomModule(F,Q,w);
1577    }
1578  }
1579  pLexOrder=b;
1580  if (h==isHomog)
1581  {
1582    if (strat->ak > 0 && (w!=NULL) && (*w!=NULL))
1583    {
1584      strat->kModW = kModW = *w;
1585      if (vw == NULL)
1586      {
1587        pFDegOld = pFDeg;
1588        pLDegOld = pLDeg;
1589        pSetDegProcs(kModDeg);
1590        toReset = TRUE;
1591      }
1592    }
1593    pLexOrder = TRUE;
1594    if (hilb==NULL) strat->LazyPass*=2;
1595  }
1596  strat->homog=h;
1597#ifdef KDEBUG
1598  idTest(F);
1599#endif
1600  if (pOrdSgn==-1)
1601  {
1602    if (w!=NULL)
1603      r=mora(F,Q,*w,hilb,strat);
1604    else
1605      r=mora(F,Q,NULL,hilb,strat);
1606  }
1607  else
1608  {
1609    if (w!=NULL)
1610      r=bba(F,Q,*w,hilb,strat);
1611    else
1612      r=bba(F,Q,NULL,hilb,strat);
1613  }
1614#ifdef KDEBUG
1615  idTest(r);
1616#endif
1617  if (toReset)
1618  {
1619    kModW = NULL;
1620    pRestoreDegProcs(pFDegOld, pLDegOld);
1621  }
1622  pLexOrder = b;
1623//Print("%d reductions canceled \n",strat->cel);
1624  HCord=strat->HCord;
1625  delete(strat);
1626  if ((delete_w)&&(w!=NULL)&&(*w!=NULL)) delete *w;
1627  return r;
1628}
1629
1630//##############################################################
1631//##############################################################
1632//##############################################################
1633//##############################################################
1634//##############################################################
1635
1636lists min_std(ideal F, ideal Q, tHomog h,intvec ** w, intvec *hilb,int syzComp,
1637              int reduced)
1638{
1639  ideal r=NULL;
1640  int Kstd1_OldDeg = Kstd1_deg,i;
1641  intvec* temp_w=NULL;
1642  BOOLEAN b=pLexOrder,toReset=FALSE;
1643  BOOLEAN delete_w=(w==NULL);
1644  BOOLEAN oldDegBound=TEST_OPT_DEGBOUND;
1645  kStrategy strat=new skStrategy;
1646
1647  if(!TEST_OPT_RETURN_SB)
1648     strat->syzComp = syzComp;
1649  if (rField_has_simple_inverse())
1650    strat->LazyPass=20;
1651  else
1652    strat->LazyPass=2;
1653  strat->LazyDegree = 1;
1654  strat->minim=(reduced % 2)+1;
1655  strat->ak = idRankFreeModule(F);
1656  if (delete_w)
1657  {
1658    temp_w=new intvec((strat->ak)+1);
1659    w = &temp_w;
1660  }
1661  if ((h==testHomog)
1662  )
1663  {
1664    if (strat->ak == 0)
1665    {
1666      h = (tHomog)idHomIdeal(F,Q);
1667      w=NULL;
1668    }
1669    else
1670    {
1671      h = (tHomog)idHomModule(F,Q,w);
1672    }
1673  }
1674  if (h==isHomog)
1675  {
1676    if (strat->ak > 0 && (w!=NULL) && (*w!=NULL))
1677    {
1678      kModW = *w;
1679      strat->kModW = *w;
1680      assume(pFDeg != NULL && pLDeg != NULL);
1681      pFDegOld = pFDeg;
1682      pLDegOld = pLDeg;
1683      pSetDegProcs(kModDeg);
1684     
1685      toReset = TRUE;
1686      if (reduced>1)
1687      {
1688        Kstd1_OldDeg=Kstd1_deg;
1689        Kstd1_deg = -1;
1690        for (i=IDELEMS(F)-1;i>=0;i--)
1691        {
1692          if ((F->m[i]!=NULL) && (pFDeg(F->m[i])>=Kstd1_deg))
1693            Kstd1_deg = pFDeg(F->m[i])+1;
1694        }
1695      }
1696    }
1697    pLexOrder = TRUE;
1698    strat->LazyPass*=2;
1699  }
1700  strat->homog=h;
1701  if (pOrdSgn==-1)
1702  {
1703    if (w!=NULL)
1704      r=mora(F,Q,*w,hilb,strat);
1705    else
1706      r=mora(F,Q,NULL,hilb,strat);
1707  }
1708  else
1709  {
1710    if (w!=NULL)
1711      r=bba(F,Q,*w,hilb,strat);
1712    else
1713      r=bba(F,Q,NULL,hilb,strat);
1714  }
1715#ifdef KDEBUG
1716  {
1717    int i;
1718    for (i=0; i<IDELEMS(r); i++) pTest(r->m[i]);
1719  }
1720#endif
1721  idSkipZeroes(r);
1722  if (toReset)
1723  {
1724    pRestoreDegProcs(pFDegOld, pLDegOld);
1725    kModW = NULL;
1726  }
1727  pLexOrder = b;
1728  HCord=strat->HCord;
1729  if ((delete_w)&&(temp_w!=NULL)) delete temp_w;
1730  lists l=(lists)omAllocBin(slists_bin);
1731  l->Init(2);
1732  if (strat->ak==0)
1733  {
1734    l->m[0].rtyp=IDEAL_CMD;
1735    l->m[1].rtyp=IDEAL_CMD;
1736  }
1737  else
1738  {
1739    l->m[0].rtyp=MODUL_CMD;
1740    l->m[1].rtyp=MODUL_CMD;
1741  }
1742  l->m[0].data=(void *)r;
1743  setFlag(&(l->m[0]),FLAG_STD);
1744  if (strat->M==NULL)
1745  {
1746    l->m[1].data=(void *)idInit(1,F->rank);
1747    Warn("no minimal generating set computed");
1748  }
1749  else
1750  {
1751    idSkipZeroes(strat->M);
1752    l->m[1].data=(void *)strat->M;
1753  }
1754  delete(strat);
1755  if (reduced>2)
1756  {
1757    Kstd1_deg=Kstd1_OldDeg;
1758    if (!oldDegBound)
1759      test &= ~Sy_bit(OPT_DEGBOUND);
1760  }
1761  return l;
1762}
1763
1764poly kNF(ideal F, ideal Q, poly p,int syzComp, int lazyReduce)
1765{
1766  if (p==NULL)
1767     return NULL;
1768  kStrategy strat=new skStrategy;
1769  strat->syzComp = syzComp;
1770  if (pOrdSgn==-1)
1771    p=kNF1(F,Q,p,strat,lazyReduce);
1772  else
1773    p=kNF2(F,Q,p,strat,lazyReduce);
1774  delete(strat);
1775  return p;
1776}
1777
1778ideal kNF(ideal F, ideal Q, ideal p,int syzComp,int lazyReduce)
1779{
1780  ideal res;
1781  if (TEST_OPT_PROT)
1782  {
1783    Print("(S:%d)",IDELEMS(p));mflush();
1784  }
1785  kStrategy strat=new skStrategy;
1786  strat->syzComp = syzComp;
1787  if (pOrdSgn==-1)
1788    res=kNF1(F,Q,p,strat,lazyReduce);
1789  else
1790    res=kNF2(F,Q,p,strat,lazyReduce);
1791  delete(strat);
1792  return res;
1793}
1794
1795/*2
1796*interreduces F
1797*/
1798ideal kInterRed (ideal F, ideal Q)
1799{
1800  int j;
1801  kStrategy strat = new skStrategy;
1802
1803//  if (TEST_OPT_PROT)
1804//  {
1805//    writeTime("start InterRed:");
1806//    mflush();
1807//  }
1808  //strat->syzComp     = 0;
1809  strat->kHEdgeFound = ppNoether != NULL;
1810  strat->kNoether=pCopy(ppNoether);
1811  strat->ak = idRankFreeModule(F);
1812  initBuchMoraCrit(strat);
1813  strat->NotUsedAxis = (BOOLEAN *)omAlloc((pVariables+1)*sizeof(BOOLEAN));
1814  for (j=pVariables; j>0; j--) strat->NotUsedAxis[j] = TRUE;
1815  strat->enterS      = enterSBba;
1816  strat->posInT      = posInT0;
1817  strat->initEcart   = initEcartNormal;
1818  strat->sl   = -1;
1819  strat->tl          = -1;
1820  strat->tmax        = setmax;
1821  strat->T           = initT();
1822  strat->R           = initR();
1823  strat->sevT        = initsevT();
1824  if (pOrdSgn == -1)   strat->honey = TRUE;
1825  initS(F,Q,strat);
1826  if (TEST_OPT_REDSB)
1827    strat->noTailReduction=FALSE;
1828  updateS(TRUE,strat);
1829  if (TEST_OPT_REDSB && TEST_OPT_INTSTRATEGY)
1830    completeReduce(strat);
1831  pDelete(&strat->kHEdge);
1832  omFreeSize((ADDRESS)strat->T,strat->tmax*sizeof(TObject));
1833  omFreeSize((ADDRESS)strat->ecartS,IDELEMS(strat->Shdl)*sizeof(int));
1834  omFreeSize((ADDRESS)strat->sevS,IDELEMS(strat->Shdl)*sizeof(unsigned long));
1835  omFreeSize((ADDRESS)strat->NotUsedAxis,(pVariables+1)*sizeof(BOOLEAN));
1836  omfree(strat->sevT);
1837  omfree(strat->S_2_R);
1838  omfree(strat->R);
1839
1840  if (strat->fromQ)
1841  {
1842    for (j=0;j<IDELEMS(strat->Shdl);j++)
1843    {
1844      if(strat->fromQ[j]) pDelete(&strat->Shdl->m[j]);
1845    }
1846    omFreeSize((ADDRESS)strat->fromQ,IDELEMS(strat->Shdl)*sizeof(int));
1847    strat->fromQ=NULL;
1848    idSkipZeroes(strat->Shdl);
1849  }
1850//  if (TEST_OPT_PROT)
1851//  {
1852//    writeTime("end Interred:");
1853//    mflush();
1854//  }
1855  ideal shdl=strat->Shdl;
1856  delete(strat);
1857  return shdl;
1858}
1859
1860// returns TRUE if mora should use buckets, false otherwise
1861static BOOLEAN kMoraUseBucket(kStrategy strat)
1862{
1863#ifdef MORA_USE_BUCKETS
1864  if (TEST_OPT_NOT_BUCKETS) 
1865    return FALSE;
1866  if (strat->red == redFirst)
1867  {
1868#ifdef NO_LDEG
1869    if (!strat->syzComp)
1870      return TRUE;
1871#else   
1872    if ((strat->homog || strat->honey) && !strat->syzComp) 
1873      return TRUE;
1874#endif
1875  }
1876  else
1877  {
1878    assume(strat->red == redEcart);
1879    if (strat->honey && !strat->syzComp)
1880      return TRUE;
1881  }
1882#endif
1883  return FALSE;
1884}
Note: See TracBrowser for help on using the repository browser.