source: git/kernel/kstd1.cc @ 1d138c

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