source: git/kernel/kstd2.cc @ 40d3462

spielwiese
Last change on this file since 40d3462 was 40d3462, checked in by Oliver Wienand <wienand@…>, 16 years ago
kstd2.cc: keine Größenvergleich bei redNF für Ringe, ebenso keine Normierung. git-svn-id: file:///usr/local/Singular/svn/trunk@10993 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 49.5 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: kstd2.cc,v 1.75 2008-08-14 13:00:34 wienand Exp $ */
5/*
6*  ABSTRACT -  Kernel: alg. of Buchberger
7*/
8
9// #define PDEBUG 2
10// define to enable tailRings
11#define HAVE_TAIL_RING
12// define if no buckets should be used
13// #define NO_BUCKETS
14
15#include "mod2.h"
16#ifdef HAVE_PLURAL
17#define PLURAL_INTERNAL_DECLARATIONS 1
18#endif
19#include "kutil.h"
20#include "structs.h"
21#include "omalloc.h"
22#include "polys.h"
23#include "ideals.h"
24#include "febase.h"
25#include "kstd1.h"
26#include "khstd.h"
27#include "kbuckets.h"
28//#include "cntrlc.h"
29#include "weight.h"
30#include "intvec.h"
31#ifdef HAVE_PLURAL
32#include "gring.h"
33#endif
34// #include "timer.h"
35
36/* shiftgb stuff */
37#include "shiftgb.h"
38
39// return -1 if no divisor is found
40//        number of first divisor, otherwise
41int kFindDivisibleByInT(const TSet &T, const unsigned long* sevT,
42                        const int tl, const LObject* L, const int start)
43{
44  unsigned long not_sev = ~L->sev;
45  int j = start;
46  poly p=L->p;
47  ring r=currRing;
48  if (p==NULL)  { r=L->tailRing; p=L->t_p; }
49  L->GetLm(p, r);
50
51  pAssume(~not_sev == p_GetShortExpVector(p, r));
52
53  if (r == currRing)
54  {
55    loop
56    {
57      if (j > tl) return -1;
58#if defined(PDEBUG) || defined(PDIV_DEBUG)
59      if (p_LmShortDivisibleBy(T[j].p, sevT[j],
60                               p, not_sev, r))
61        return j;
62#else
63      if (!(sevT[j] & not_sev) &&
64          p_LmDivisibleBy(T[j].p, p, r))
65        return j;
66#endif
67      j++;
68    }
69  }
70  else
71  {
72    loop
73    {
74      if (j > tl) return -1;
75#if defined(PDEBUG) || defined(PDIV_DEBUG)
76      if (p_LmShortDivisibleBy(T[j].t_p, sevT[j],
77                               p, not_sev, r))
78        return j;
79#else
80      if (!(sevT[j] & not_sev) &&
81          p_LmDivisibleBy(T[j].t_p, p, r))
82        return j;
83#endif
84      j++;
85    }
86  }
87}
88
89// same as above, only with set S
90int kFindDivisibleByInS(const kStrategy strat, int* max_ind, LObject* L)
91{
92  unsigned long not_sev = ~L->sev;
93  poly p = L->GetLmCurrRing();
94  int j = 0;
95
96  pAssume(~not_sev == p_GetShortExpVector(p, currRing));
97#if 1
98  int ende;
99  if (strat->ak>0) ende=strat->sl;
100  else ende=posInS(strat,*max_ind,p,0)+1;
101  if (ende>(*max_ind)) ende=(*max_ind);
102#else
103  int ende=strat->sl;
104#endif
105  (*max_ind)=ende;
106  loop
107  {
108    if (j > ende) return -1;
109#if defined(PDEBUG) || defined(PDIV_DEBUG)
110    if (p_LmShortDivisibleBy(strat->S[j], strat->sevS[j],
111                             p, not_sev, currRing))
112        return j;
113#else
114    if ( !(strat->sevS[j] & not_sev) &&
115         p_LmDivisibleBy(strat->S[j], p, currRing))
116      return j;
117#endif
118    j++;
119  }
120}
121
122int kFindNextDivisibleByInS(const kStrategy strat, int start,int max_ind, LObject* L)
123{
124  unsigned long not_sev = ~L->sev;
125  poly p = L->GetLmCurrRing();
126  int j = start;
127
128  pAssume(~not_sev == p_GetShortExpVector(p, currRing));
129#if 1
130  int ende=max_ind;
131#else
132  int ende=strat->sl;
133#endif
134  loop
135  {
136    if (j > ende) return -1;
137#if defined(PDEBUG) || defined(PDIV_DEBUG)
138    if (p_LmShortDivisibleBy(strat->S[j], strat->sevS[j],
139                             p, not_sev, currRing))
140        return j;
141#else
142    if ( !(strat->sevS[j] & not_sev) &&
143         p_LmDivisibleBy(strat->S[j], p, currRing))
144      return j;
145#endif
146    j++;
147  }
148}
149
150#ifdef HAVE_RING2TOM
151NATNUMBER factorial(NATNUMBER arg)
152{
153   NATNUMBER tmp = 1; arg++;
154   for (int i = 2; i < arg; i++)
155   {
156     tmp *= i;
157   }
158   return tmp;
159}
160
161poly kFindZeroPoly(poly input_p, ring leadRing, ring tailRing)
162{
163  // m = currRing->ch
164
165  if (input_p == NULL) return NULL;
166
167  poly p = input_p;
168  poly zeroPoly = NULL;
169  NATNUMBER a = (NATNUMBER) pGetCoeff(p);
170
171  int k_ind2 = 0;
172  int a_ind2 = ind2(a);
173
174  NATNUMBER k = 1;
175  // of interest is only k_ind2, special routine for improvement ... TODO OLIVER
176  for (int i = 1; i <= leadRing->N; i++)
177  {
178    k_ind2 = k_ind2 + ind_fact_2(p_GetExp(p, i, leadRing));
179  }
180
181  a = (NATNUMBER) pGetCoeff(p);
182
183  number tmp1;
184  poly tmp2, tmp3;
185  poly lead_mult = p_ISet(1, tailRing);
186  if (leadRing->ch <= k_ind2 + a_ind2)
187  {
188    int too_much = k_ind2 + a_ind2 - leadRing->ch;
189    int s_exp;
190    zeroPoly = p_ISet(a, tailRing);
191    for (int i = 1; i <= leadRing->N; i++)
192    {
193      s_exp = p_GetExp(p, i,leadRing);
194      if (s_exp % 2 != 0)
195      {
196        s_exp = s_exp - 1;
197      }
198      while ( (0 < ind2(s_exp)) && (ind2(s_exp) <= too_much) )
199      {
200        too_much = too_much - ind2(s_exp);
201        s_exp = s_exp - 2;
202      }
203      p_SetExp(lead_mult, i, p_GetExp(p, i,leadRing) - s_exp, tailRing);
204      for (NATNUMBER j = 1; j <= s_exp; j++)
205      {
206        tmp1 = nInit(j);
207        tmp2 = p_ISet(1, tailRing);
208        p_SetExp(tmp2, i, 1, tailRing);
209        p_Setm(tmp2, tailRing);
210        if (nIsZero(tmp1))
211        { // should nowbe obsolet, test ! TODO OLIVER
212          zeroPoly = p_Mult_q(zeroPoly, tmp2, tailRing);
213        }
214        else
215        {
216          tmp3 = p_NSet(nCopy(tmp1), tailRing);
217          zeroPoly = p_Mult_q(zeroPoly, p_Add_q(tmp3, tmp2, tailRing), tailRing);
218        }
219      }
220    }
221    p_Setm(lead_mult, tailRing);
222    zeroPoly = p_Mult_mm(zeroPoly, lead_mult, tailRing);
223    tmp2 = p_NSet(nCopy(pGetCoeff(zeroPoly)), leadRing);
224    for (int i = 1; i <= leadRing->N; i++)
225    {
226      pSetExp(tmp2, i, p_GetExp(zeroPoly, i, tailRing));
227    }
228    p_Setm(tmp2, leadRing);
229    zeroPoly = p_LmDeleteAndNext(zeroPoly, tailRing);
230    pNext(tmp2) = zeroPoly;
231    return tmp2;
232  }
233/*  NATNUMBER alpha_k = twoPow(leadRing->ch - k_ind2);
234  if (1 == 0 && alpha_k <= a)
235  {  // Temporarly disabled, reducing coefficients not compatible with std TODO Oliver
236    zeroPoly = p_ISet((a / alpha_k)*alpha_k, tailRing);
237    for (int i = 1; i <= leadRing->N; i++)
238    {
239      for (NATNUMBER j = 1; j <= p_GetExp(p, i, leadRing); j++)
240      {
241        tmp1 = nInit(j);
242        tmp2 = p_ISet(1, tailRing);
243        p_SetExp(tmp2, i, 1, tailRing);
244        p_Setm(tmp2, tailRing);
245        if (nIsZero(tmp1))
246        {
247          zeroPoly = p_Mult_q(zeroPoly, tmp2, tailRing);
248        }
249        else
250        {
251          tmp3 = p_ISet((NATNUMBER) tmp1, tailRing);
252          zeroPoly = p_Mult_q(zeroPoly, p_Add_q(tmp2, tmp3, tailRing), tailRing);
253        }
254      }
255    }
256    tmp2 = p_ISet((NATNUMBER) pGetCoeff(zeroPoly), leadRing);
257    for (int i = 1; i <= leadRing->N; i++)
258    {
259      pSetExp(tmp2, i, p_GetExp(zeroPoly, i, tailRing));
260    }
261    p_Setm(tmp2, leadRing);
262    zeroPoly = p_LmDeleteAndNext(zeroPoly, tailRing);
263    pNext(tmp2) = zeroPoly;
264    return tmp2;
265  } */
266  return NULL;
267}
268
269poly kFindDivisibleByZeroPoly(LObject* h)
270{
271  return kFindZeroPoly(h->GetLmCurrRing(), currRing, h->tailRing);
272}
273#endif
274
275
276#ifdef HAVE_RINGS
277/*2
278*  reduction procedure for the ring Z/2^m
279*/
280int redRing (LObject* h,kStrategy strat)
281{
282  if (h->p == NULL && h->t_p == NULL) return 0; // spoly is zero (can only occure with zero divisors)
283
284//  if (strat->tl<0) return 1;
285  int at,d,i;
286  int j = 0;
287  int pass = 0;
288  poly zeroPoly = NULL;
289
290// TODO warum SetpFDeg notwendig?
291  h->SetpFDeg();
292  assume(h->pFDeg() == h->FDeg);
293//  if (h->pFDeg() != h->FDeg)
294//  {
295//    Print("h->pFDeg()=%d =!= h->FDeg=%d\n", h->pFDeg(), h->FDeg);
296//  }
297  long reddeg = h->GetpFDeg();
298
299  h->SetShortExpVector();
300  loop
301  {
302      j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
303      if (j < 0) return 1;
304#ifdef KDEBUG
305      if (TEST_OPT_DEBUG)
306      {
307        PrintS("T red:");
308      }
309#endif
310#ifdef KDEBUG
311    if (TEST_OPT_DEBUG)
312    {
313      h->wrp();
314      PrintS(" with ");
315      strat->T[j].wrp();
316    }
317#endif
318
319    ksReducePoly(h, &(strat->T[j]), NULL, NULL, strat);
320
321#ifdef KDEBUG
322    if (TEST_OPT_DEBUG)
323    {
324      PrintS("\nto ");
325      h->wrp();
326      PrintLn();
327    }
328#endif
329
330    if (h->GetLmTailRing() == NULL)
331    {
332      if (h->lcm!=NULL) pLmDelete(h->lcm);
333#ifdef KDEBUG
334      h->lcm=NULL;
335#endif
336      return 0;
337    }
338    h->SetShortExpVector();
339    d = h->SetpFDeg();
340    /*- try to reduce the s-polynomial -*/
341    pass++;
342    if (!K_TEST_OPT_REDTHROUGH &&
343        (strat->Ll >= 0) && ((d > reddeg) || (pass > strat->LazyPass)))
344    {
345      h->SetLmCurrRing();
346      at = strat->posInL(strat->L,strat->Ll,h,strat);
347      if (at <= strat->Ll)
348      {
349#ifdef KDEBUG
350        if (TEST_OPT_DEBUG) Print(" ->L[%d]\n",at);
351#endif
352        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);     // NOT RING CHECKED OLIVER
353        h->Clear();
354        return -1;
355      }
356    }
357    else if ((TEST_OPT_PROT) && (strat->Ll < 0) && (d != reddeg))
358    {
359      Print(".%d",d);mflush();
360      reddeg = d;
361    }
362  }
363}
364#endif
365
366/*2
367*  reduction procedure for the homogeneous case
368*  and the case of a degree-ordering
369*/
370int redHomog (LObject* h,kStrategy strat)
371{
372  if (strat->tl<0) return 1;
373  //if (h->GetLmTailRing()==NULL) return 0; // HS: SHOULD NOT BE NEEDED!
374  assume(h->FDeg == h->pFDeg());
375
376  poly h_p;
377  int i,j,at,pass, ii;
378  unsigned long not_sev;
379  long reddeg,d;
380
381  pass = j = 0;
382  d = reddeg = h->GetpFDeg();
383  h->SetShortExpVector();
384  int li;
385  h_p = h->GetLmTailRing();
386  not_sev = ~ h->sev;
387  loop
388  {
389    j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
390    if (j < 0) return 1;
391
392    li = strat->T[j].pLength;
393    ii = j;
394    /*
395     * the polynomial to reduce with (up to the moment) is;
396     * pi with length li
397     */
398    i = j;
399#if 1
400    if (TEST_OPT_LENGTH)
401    loop
402    {
403      /*- search the shortest possible with respect to length -*/
404      i++;
405      if (i > strat->tl)
406        break;
407      if (li<=1)
408        break;
409      if ((strat->T[i].pLength < li)
410         &&
411          p_LmShortDivisibleBy(strat->T[i].GetLmTailRing(), strat->sevT[i],
412                               h_p, not_sev, strat->tailRing))
413      {
414        /*
415         * the polynomial to reduce with is now;
416         */
417        li = strat->T[i].pLength;
418        ii = i;
419      }
420    }
421#endif
422
423    /*
424     * end of search: have to reduce with pi
425     */
426#ifdef KDEBUG
427    if (TEST_OPT_DEBUG)
428    {
429      PrintS("red:");
430      h->wrp();
431      PrintS(" with ");
432      strat->T[ii].wrp();
433    }
434#endif
435    assume(strat->fromT == FALSE);
436
437    ksReducePoly(h, &(strat->T[ii]), NULL, NULL, strat);
438
439#ifdef KDEBUG
440    if (TEST_OPT_DEBUG)
441    {
442      PrintS("\nto ");
443      h->wrp();
444      PrintLn();
445    }
446#endif
447
448    h_p = h->GetLmTailRing();
449    if (h_p == NULL)
450    {
451      if (h->lcm!=NULL) pLmFree(h->lcm);
452#ifdef KDEBUG
453      h->lcm=NULL;
454#endif
455      return 0;
456    }
457    h->SetShortExpVector();
458    not_sev = ~ h->sev;
459    /*
460     * try to reduce the s-polynomial h
461     *test first whether h should go to the lazyset L
462     *-if the degree jumps
463     *-if the number of pre-defined reductions jumps
464     */
465    pass++;
466    if (!K_TEST_OPT_REDTHROUGH && (strat->Ll >= 0) && (pass > strat->LazyPass))
467    {
468      h->SetLmCurrRing();
469      at = strat->posInL(strat->L,strat->Ll,h,strat);
470      if (at <= strat->Ll)
471      {
472        int dummy=strat->sl;
473        if (kFindDivisibleByInS(strat, &dummy, h) < 0)
474          return 1;
475        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
476#ifdef KDEBUG
477        if (TEST_OPT_DEBUG)
478          Print(" lazy: -> L%d\n",at);
479#endif
480        h->Clear();
481        return -1;
482      }
483    }
484  }
485}
486
487/*2
488*  reduction procedure for the inhomogeneous case
489*  and not a degree-ordering
490*/
491int redLazy (LObject* h,kStrategy strat)
492{
493  if (strat->tl<0) return 1;
494  int at,d,i,ii,li;
495  int j = 0;
496  int pass = 0;
497  assume(h->pFDeg() == h->FDeg);
498  long reddeg = h->GetpFDeg();
499  unsigned long not_sev;
500
501  h->SetShortExpVector();
502  poly h_p = h->GetLmTailRing();
503  not_sev = ~ h->sev;
504  loop
505  {
506    j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
507    if (j < 0) return 1;
508
509    li = strat->T[j].pLength;
510    #if 0
511    if (li==0)
512    {
513      li=strat->T[j].pLength=pLength(strat->T[j].p);
514    }
515    #endif
516    ii = j;
517    /*
518     * the polynomial to reduce with (up to the moment) is;
519     * pi with length li
520     */
521
522    i = j;
523#if 1
524    if (TEST_OPT_LENGTH)
525    loop
526    {
527      /*- search the shortest possible with respect to length -*/
528      i++;
529      if (i > strat->tl)
530        break;
531      if (li<=1)
532        break;
533    #if 0
534      if (strat->T[i].pLength==0)
535      {
536        PrintS("!");
537        strat->T[i].pLength=pLength(strat->T[i].p);
538      }
539   #endif
540      if ((strat->T[i].pLength < li)
541         &&
542          p_LmShortDivisibleBy(strat->T[i].GetLmTailRing(), strat->sevT[i],
543                               h_p, not_sev, strat->tailRing))
544      {
545        /*
546         * the polynomial to reduce with is now;
547         */
548        PrintS("+");
549        li = strat->T[i].pLength;
550        ii = i;
551      }
552    }
553#endif
554
555    /*
556     * end of search: have to reduce with pi
557     */
558
559
560#ifdef KDEBUG
561    if (TEST_OPT_DEBUG)
562    {
563      PrintS("red:");
564      h->wrp();
565      PrintS(" with ");
566      strat->T[ii].wrp();
567    }
568#endif
569
570    ksReducePoly(h, &(strat->T[ii]), NULL, NULL, strat);
571
572#ifdef KDEBUG
573    if (TEST_OPT_DEBUG)
574    {
575      PrintS("\nto ");
576      h->wrp();
577      PrintLn();
578    }
579#endif
580
581    h_p=h->GetLmTailRing();
582
583    if (h_p == NULL)
584    {
585      if (h->lcm!=NULL) pLmFree(h->lcm);
586#ifdef KDEBUG
587      h->lcm=NULL;
588#endif
589      return 0;
590    }
591    h->SetShortExpVector();
592    not_sev = ~ h->sev;
593    d = h->SetpFDeg();
594    /*- try to reduce the s-polynomial -*/
595    pass++;
596    if (//!K_TEST_OPT_REDTHROUGH &&
597        (strat->Ll >= 0) && ((d > reddeg) || (pass > strat->LazyPass)))
598    {
599      h->SetLmCurrRing();
600      at = strat->posInL(strat->L,strat->Ll,h,strat);
601      if (at <= strat->Ll)
602      {
603#if 1
604        int dummy=strat->sl;
605        if (kFindDivisibleByInS(strat, &dummy, h) < 0)
606          return 1;
607#endif
608#ifdef KDEBUG
609        if (TEST_OPT_DEBUG) Print(" ->L[%d]\n",at);
610#endif
611        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
612        h->Clear();
613        return -1;
614      }
615    }
616    else if ((TEST_OPT_PROT) && (strat->Ll < 0) && (d != reddeg))
617    {
618      Print(".%d",d);mflush();
619      reddeg = d;
620    }
621  }
622}
623/*2
624*  reduction procedure for the sugar-strategy (honey)
625* reduces h with elements from T choosing first possible
626* element in T with respect to the given ecart
627*/
628int redHoney (LObject* h, kStrategy strat)
629{
630  if (strat->tl<0) return 1;
631  //if (h->GetLmTailRing()==NULL) return 0; // HS: SHOULD NOT BE NEEDED!
632  assume(h->FDeg == h->pFDeg());
633
634  poly h_p;
635  int i,j,at,pass,ei, ii, h_d;
636  unsigned long not_sev;
637  long reddeg,d;
638
639  pass = j = 0;
640  d = reddeg = h->GetpFDeg() + h->ecart;
641  h->SetShortExpVector();
642  int li;
643  h_p = h->GetLmTailRing();
644  not_sev = ~ h->sev;
645  loop
646  {
647    j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
648    if (j < 0) return 1;
649
650    ei = strat->T[j].ecart;
651    li = strat->T[j].pLength;
652    #if 0
653    if (li==0)
654    {
655       //PrintS("!");
656       li=strat->T[j].pLength=pLength(strat->T[j].p);
657    }
658    #endif
659    ii = j;
660    /*
661     * the polynomial to reduce with (up to the moment) is;
662     * pi with ecart ei
663     */
664    i = j;
665    if (TEST_OPT_LENGTH)
666    loop
667    {
668      /*- takes the first possible with respect to ecart -*/
669      i++;
670      if (i > strat->tl)
671        break;
672      //if (ei < h->ecart)
673      //  break;
674      if (li<=1)
675        break;
676      if ((((strat->T[i].ecart < ei) && (ei> h->ecart))
677         || ((strat->T[i].ecart <= h->ecart) && (strat->T[i].pLength < li)))
678         &&
679          p_LmShortDivisibleBy(strat->T[i].GetLmTailRing(), strat->sevT[i],
680                               h_p, not_sev, strat->tailRing))
681      {
682        /*
683         * the polynomial to reduce with is now;
684         */
685        ei = strat->T[i].ecart;
686        li = strat->T[i].pLength;
687        ii = i;
688      }
689    }
690
691    /*
692     * end of search: have to reduce with pi
693     */
694    if (!K_TEST_OPT_REDTHROUGH && (pass!=0) && (ei > h->ecart))
695    {
696      h->SetLmCurrRing();
697      /*
698       * It is not possible to reduce h with smaller ecart;
699       * if possible h goes to the lazy-set L,i.e
700       * if its position in L would be not the last one
701       */
702      if (strat->Ll >= 0) /* L is not empty */
703      {
704        at = strat->posInL(strat->L,strat->Ll,h,strat);
705        if(at <= strat->Ll)
706          /*- h will not become the next element to reduce -*/
707        {
708          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
709#ifdef KDEBUG
710          if (TEST_OPT_DEBUG) Print(" ecart too big: -> L%d\n",at);
711#endif
712          h->Clear();
713          return -1;
714        }
715      }
716    }
717#ifdef KDEBUG
718    if (TEST_OPT_DEBUG)
719    {
720      PrintS("red:");
721      h->wrp();
722      PrintS(" with ");
723      strat->T[ii].wrp();
724    }
725#endif
726    assume(strat->fromT == FALSE);
727
728#if 0 // test poly exchange
729    if (strat->inStdFac==0)
730    {
731      int ll;
732      poly t_p;
733      if (strat->tailRing==currRing)
734        t_p=strat->T[ii].p;
735      else
736        t_p=strat->T[ii].t_p;
737      if ((p_LmCmp(h_p,t_p,strat->tailRing)==0)
738      && ((ll=h->GuessLength()) < strat->T[ii].pLength))
739      {
740        h->GetP();
741        if ((h->pLength=h->GetpLength()) < strat->T[ii].pLength)
742        {
743          if (TEST_OPT_PROT)  PrintS("e");
744          h->GetP();
745          if (h->p!=NULL)
746          {
747            if (strat->T[ii].p!=NULL)
748            {
749              poly swap;
750              omTypeAlloc0Bin(poly,swap,currRing->PolyBin);
751              memcpy(swap,h->p,currRing->PolyBin->sizeW*sizeof(long));
752              memcpy(h->p,strat->T[ii].p,currRing->PolyBin->sizeW*sizeof(long));
753              memcpy(strat->T[ii].p,swap,currRing->PolyBin->sizeW*sizeof(long));
754              omFreeBinAddr(swap);
755            }
756            else
757            {
758              strat->T[ii].p=h->p;
759              h->p=NULL;
760            }
761          }
762          else
763          {
764            if (strat->T[ii].p!=NULL)
765            {
766              h->p=strat->T[ii].p;
767              strat->T[ii].p=NULL;
768            }
769            // else: all NULL
770          }
771          if (h->t_p!=NULL)
772          {
773            if (strat->T[ii].t_p!=NULL)
774            {
775              poly swap;
776              omTypeAlloc0Bin(poly,swap,strat->tailRing->PolyBin);
777              memcpy(swap,h->t_p,strat->tailRing->PolyBin->sizeW*sizeof(long));
778              memcpy(h->t_p,strat->T[ii].t_p,strat->tailRing->PolyBin->sizeW*sizeof(long));
779              memcpy(strat->T[ii].t_p,swap,strat->tailRing->PolyBin->sizeW*sizeof(long));
780              omFreeBinAddr(swap);
781            }
782            else
783            {
784              strat->T[ii].t_p=h->t_p;
785              h->t_p=NULL;
786            }
787          }
788          else
789          {
790            if (strat->T[ii].t_p!=NULL)
791            {
792              h->t_p=strat->T[ii].t_p;
793              strat->T[ii].t_p=NULL;
794            }
795            // else: all NULL
796          }
797          if (strat->tailRing != currRing && (strat->T[ii].p != NULL)
798          && pNext(strat->T[ii].p) != NULL)
799             strat->T[ii].max =p_GetMaxExpP(pNext(strat->T[ii].p), strat->tailRing);
800          else
801             strat->T[ii].max = NULL;
802          h->length=h->pLength=pLength(h->p);
803          strat->T[ii].length=strat->T[ii].pLength=pLength(strat->T[ii].p);
804          if (strat->T[ii].is_normalized)
805          {
806            strat->T[ii].is_normalized=0;
807            strat->T[ii].pNorm();
808          }
809          else
810          {
811            if (TEST_OPT_INTSTRATEGY)
812              strat->T[ii].pCleardenom();
813          }
814          h->PrepareRed(strat->use_buckets);
815        }
816      }
817    }
818#endif // test poly exchange
819    ksReducePoly(h, &(strat->T[ii]), NULL, NULL, strat);
820
821#ifdef KDEBUG
822    if (TEST_OPT_DEBUG)
823    {
824      PrintS("\nto ");
825      h->wrp();
826      PrintLn();
827    }
828#endif
829
830    h_p = h->GetLmTailRing();
831    if (h_p == NULL)
832    {
833      if (h->lcm!=NULL) pLmFree(h->lcm);
834#ifdef KDEBUG
835      h->lcm=NULL;
836#endif
837      return 0;
838    }
839    h->SetShortExpVector();
840    not_sev = ~ h->sev;
841    h_d = h->SetpFDeg();
842    /* compute the ecart */
843    if (ei <= h->ecart)
844      h->ecart = d-h_d;
845    else
846      h->ecart = d-h_d+ei-h->ecart;
847    /*
848     * try to reduce the s-polynomial h
849     *test first whether h should go to the lazyset L
850     *-if the degree jumps
851     *-if the number of pre-defined reductions jumps
852     */
853    pass++;
854    d = h_d + h->ecart;
855    if (!K_TEST_OPT_REDTHROUGH && (strat->Ll >= 0) && ((d > reddeg) || (pass > strat->LazyPass)))
856    {
857      h->SetLmCurrRing();
858      at = strat->posInL(strat->L,strat->Ll,h,strat);
859      if (at <= strat->Ll)
860      {
861        int dummy=strat->sl;
862        if (kFindDivisibleByInS(strat, &dummy, h) < 0)
863          return 1;
864        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
865#ifdef KDEBUG
866        if (TEST_OPT_DEBUG)
867          Print(" degree jumped: -> L%d\n",at);
868#endif
869        h->Clear();
870        return -1;
871      }
872    }
873    else if (TEST_OPT_PROT && (strat->Ll < 0) && (d > reddeg))
874    {
875      reddeg = d;
876      Print(".%d",d); mflush();
877    }
878  }
879}
880/*2
881*  reduction procedure for the normal form
882*/
883
884poly redNF (poly h,int &max_ind,kStrategy strat)
885{
886  if (h==NULL) return NULL;
887  int j;
888  max_ind=strat->sl;
889
890  if (0 > strat->sl)
891  {
892    return h;
893  }
894  LObject P(h);
895  P.SetShortExpVector();
896  P.bucket = kBucketCreate(currRing);
897  kBucketInit(P.bucket,P.p,pLength(P.p));
898  kbTest(P.bucket);
899#ifdef HAVE_RINGS
900  BOOLEAN is_ring = rField_is_Ring(currRing);
901#endif
902  loop
903  {
904    j=kFindDivisibleByInS(strat,&max_ind,&P);
905    if (j>=0)
906    {
907#ifdef HAVE_RINGS
908      if (!is_ring)
909      {
910#endif
911      int sl=pSize(strat->S[j]);
912      int jj=j;
913      loop
914      {
915        int sll;
916        jj=kFindNextDivisibleByInS(strat,jj+1,max_ind,&P);
917        if (jj<0) break;
918        sll=pSize(strat->S[jj]);
919        if (sll<sl)
920        {
921          if (!nIsOne(pGetCoeff(strat->S[j])))
922          {
923            pNorm(strat->S[j]);
924            //if (TEST_OPT_PROT) { PrintS("n"); mflush(); }
925            sl=pSize(strat->S[j]);
926          }
927          if (!nIsOne(pGetCoeff(strat->S[jj])))
928          {
929            pNorm(strat->S[jj]);
930            //if (TEST_OPT_PROT) { PrintS("n"); mflush(); }
931            sll=pSize(strat->S[jj]);
932          }
933          if (sll<sl)
934          {
935            #ifdef KDEBUG
936            if (TEST_OPT_DEBUG) Print("better(S%d:%d -> S%d:%d)\n",j,sl,jj,sll);
937            #endif
938            //else if (TEST_OPT_PROT) { PrintS("b"); mflush(); }
939            j=jj;
940            sl=sll;
941          }
942        }
943      }
944      if (!nIsOne(pGetCoeff(strat->S[j])))
945      {
946        pNorm(strat->S[j]);
947        //if (TEST_OPT_PROT) { PrintS("n"); mflush(); }
948      }
949#ifdef HAVE_RINGS
950      }
951#endif
952      nNormalize(pGetCoeff(P.p));
953#ifdef KDEBUG
954      if (TEST_OPT_DEBUG)
955      {
956        PrintS("red:");
957        wrp(h);
958        PrintS(" with ");
959        wrp(strat->S[j]);
960      }
961#endif
962#ifdef HAVE_PLURAL
963      if (rIsPluralRing(currRing))
964      {
965        number coef;
966        nc_kBucketPolyRed(P.bucket,strat->S[j],&coef);
967        nDelete(&coef);
968      }
969      else
970#endif
971      {
972        number coef;
973        coef=kBucketPolyRed(P.bucket,strat->S[j],pLength(strat->S[j]),strat->kNoether);
974        nDelete(&coef);
975      }
976      h = kBucketGetLm(P.bucket);   // FRAGE OLIVER
977      if (h==NULL)
978      {
979        kBucketDestroy(&P.bucket);
980        return NULL;
981      }
982      kbTest(P.bucket);
983      P.p=h;
984      P.t_p=NULL;
985      P.SetShortExpVector();
986#ifdef KDEBUG
987      if (TEST_OPT_DEBUG)
988      {
989        PrintS("\nto:");
990        wrp(h);
991        PrintLn();
992      }
993#endif
994    }
995    else
996    {
997      P.p=kBucketClear(P.bucket);
998      kBucketDestroy(&P.bucket);
999      pNormalize(P.p);
1000      return P.p;
1001    }
1002  }
1003}
1004
1005#ifdef KDEBUG
1006static int bba_count = 0;
1007#endif
1008
1009ideal bba (ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat)
1010{
1011#ifdef KDEBUG
1012  bba_count++;
1013  int loop_count = 0;
1014#endif
1015  om_Opts.MinTrack = 5;
1016  int   srmax,lrmax, red_result = 1;
1017  int   olddeg,reduc;
1018  int hilbeledeg=1,hilbcount=0,minimcnt=0;
1019  BOOLEAN withT = FALSE;
1020
1021  initBuchMoraCrit(strat); /*set Gebauer, honey, sugarCrit*/
1022  initBuchMoraPos(strat);
1023  initHilbCrit(F,Q,&hilb,strat);
1024  initBba(F,strat);
1025  /*set enterS, spSpolyShort, reduce, red, initEcart, initEcartPair*/
1026  /*Shdl=*/initBuchMora(F, Q,strat);
1027  if (strat->minim>0) strat->M=idInit(IDELEMS(F),F->rank);
1028  srmax = strat->sl;
1029  reduc = olddeg = lrmax = 0;
1030
1031#ifndef NO_BUCKETS
1032  if (!TEST_OPT_NOT_BUCKETS)
1033    strat->use_buckets = 1;
1034#endif
1035
1036  // redtailBBa against T for inhomogenous input
1037  if (!K_TEST_OPT_OLDSTD)
1038    withT = ! strat->homog;
1039
1040  // strat->posInT = posInT_pLength;
1041  kTest_TS(strat);
1042
1043#ifdef HAVE_TAIL_RING
1044  kStratInitChangeTailRing(strat);
1045#endif
1046
1047  /* compute------------------------------------------------------- */
1048  while (strat->Ll >= 0)
1049  {
1050    if (strat->Ll > lrmax) lrmax =strat->Ll;/*stat.*/
1051    #ifdef KDEBUG
1052      loop_count++;
1053      #ifdef HAVE_RINGS
1054        if (TEST_OPT_DEBUG) PrintS("--- next step ---\n");
1055      #endif
1056      if (TEST_OPT_DEBUG) messageSets(strat);
1057    #endif
1058    if (strat->Ll== 0) strat->interpt=TRUE;
1059    if (TEST_OPT_DEGBOUND
1060        && ((strat->honey && (strat->L[strat->Ll].ecart+pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
1061            || ((!strat->honey) && (pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))))
1062    {
1063      /*
1064       *stops computation if
1065       * 24 IN test and the degree +ecart of L[strat->Ll] is bigger then
1066       *a predefined number Kstd1_deg
1067       */
1068      while ((strat->Ll >= 0)
1069        && (strat->L[strat->Ll].p1!=NULL) && (strat->L[strat->Ll].p2!=NULL)
1070        && ((strat->honey && (strat->L[strat->Ll].ecart+pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
1071            || ((!strat->honey) && (pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg)))
1072        )
1073        deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
1074      if (strat->Ll<0) break;
1075      else strat->noClearS=TRUE;
1076    }
1077    /* picks the last element from the lazyset L */
1078    strat->P = strat->L[strat->Ll];
1079    strat->Ll--;
1080
1081    if (pNext(strat->P.p) == strat->tail)
1082    {
1083      // deletes the short spoly
1084#ifdef HAVE_RINGS
1085      if (rField_is_Ring(currRing))
1086        pLmDelete(strat->P.p);
1087      else
1088#endif
1089        pLmFree(strat->P.p);
1090      strat->P.p = NULL;
1091      poly m1 = NULL, m2 = NULL;
1092
1093      // check that spoly creation is ok
1094      while (strat->tailRing != currRing &&
1095             !kCheckSpolyCreation(&(strat->P), strat, m1, m2))
1096      {
1097        assume(m1 == NULL && m2 == NULL);
1098        // if not, change to a ring where exponents are at least
1099        // large enough
1100        kStratChangeTailRing(strat);
1101      }
1102      // create the real one
1103      ksCreateSpoly(&(strat->P), NULL, strat->use_buckets,
1104                    strat->tailRing, m1, m2, strat->R);
1105    }
1106    else if (strat->P.p1 == NULL)
1107    {
1108      if (strat->minim > 0)
1109        strat->P.p2=p_Copy(strat->P.p, currRing, strat->tailRing);
1110      // for input polys, prepare reduction
1111      strat->P.PrepareRed(strat->use_buckets);
1112    }
1113
1114    if (strat->P.p == NULL && strat->P.t_p == NULL)
1115    {
1116      red_result = 0;
1117    }
1118    else
1119    {
1120      if (TEST_OPT_PROT)
1121        message((strat->honey ? strat->P.ecart : 0) + strat->P.pFDeg(),
1122                &olddeg,&reduc,strat, red_result);
1123
1124      /* reduction of the element choosen from L */
1125      red_result = strat->red(&strat->P,strat);
1126    }
1127
1128    // reduction to non-zero new poly
1129    if (red_result == 1)
1130    {
1131      /* statistic */
1132      if (TEST_OPT_PROT) PrintS("s");
1133
1134      // get the polynomial (canonicalize bucket, make sure P.p is set)
1135      strat->P.GetP(strat->lmBin);
1136
1137      int pos=posInS(strat,strat->sl,strat->P.p,strat->P.ecart);
1138
1139      // reduce the tail and normalize poly
1140      // in the ring case we cannot expect LC(f) = 1,
1141      // therefore we call pContent instead of pNorm
1142      if ((TEST_OPT_INTSTRATEGY) || (rField_is_Ring(currRing)))
1143      {
1144        strat->P.pCleardenom();
1145        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
1146        {
1147          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
1148          strat->P.pCleardenom();
1149        }
1150      }
1151      else
1152      {
1153        strat->P.pNorm();
1154        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
1155          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
1156      }
1157
1158#ifdef KDEBUG
1159      if (TEST_OPT_DEBUG){PrintS("new s:");strat->P.wrp();PrintLn();}
1160#endif
1161
1162      // min_std stuff
1163      if ((strat->P.p1==NULL) && (strat->minim>0))
1164      {
1165        if (strat->minim==1)
1166        {
1167          strat->M->m[minimcnt]=p_Copy(strat->P.p,currRing,strat->tailRing);
1168          p_Delete(&strat->P.p2, currRing, strat->tailRing);
1169        }
1170        else
1171        {
1172          strat->M->m[minimcnt]=strat->P.p2;
1173          strat->P.p2=NULL;
1174        }
1175        if (strat->tailRing!=currRing && pNext(strat->M->m[minimcnt])!=NULL)
1176          pNext(strat->M->m[minimcnt])
1177            = strat->p_shallow_copy_delete(pNext(strat->M->m[minimcnt]),
1178                                           strat->tailRing, currRing,
1179                                           currRing->PolyBin);
1180        minimcnt++;
1181      }
1182
1183      // enter into S, L, and T
1184      //if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
1185        enterT(strat->P, strat);
1186#ifdef HAVE_RINGS
1187      if (rField_is_Ring(currRing))
1188        superenterpairs(strat->P.p,strat->sl,strat->P.ecart,pos,strat, strat->tl);
1189      else
1190#endif
1191        enterpairs(strat->P.p,strat->sl,strat->P.ecart,pos,strat, strat->tl);
1192      // posInS only depends on the leading term
1193      if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
1194      {
1195      strat->enterS(strat->P, pos, strat, strat->tl);
1196      }
1197      else
1198      {
1199      //  strat->P.Delete(); // syzComp test: it is in T
1200      }
1201#if 0
1202      int pl=pLength(strat->P.p);
1203      if (pl==1)
1204      {
1205        //if (TEST_OPT_PROT)
1206        //PrintS("<1>");
1207      }
1208      else if (pl==2)
1209      {
1210        //if (TEST_OPT_PROT)
1211        //PrintS("<2>");
1212      }
1213#endif
1214      if (hilb!=NULL) khCheck(Q,w,hilb,hilbeledeg,hilbcount,strat);
1215//      Print("[%d]",hilbeledeg);
1216      if (strat->P.lcm!=NULL)
1217#ifdef HAVE_RINGS
1218        pLmDelete(strat->P.lcm);
1219#else
1220        pLmFree(strat->P.lcm);
1221#endif
1222      if (strat->sl>srmax) srmax = strat->sl;
1223    }
1224    else if (strat->P.p1 == NULL && strat->minim > 0)
1225    {
1226      p_Delete(&strat->P.p2, currRing, strat->tailRing);
1227    }
1228
1229#ifdef KDEBUG
1230    memset(&(strat->P), 0, sizeof(strat->P));
1231#endif
1232    kTest_TS(strat);
1233  }
1234#ifdef KDEBUG
1235  if (TEST_OPT_DEBUG) messageSets(strat);
1236#endif
1237  /* complete reduction of the standard basis--------- */
1238  if (TEST_OPT_SB_1)
1239  {
1240    int k=1;
1241    int j;
1242    while(k<=strat->sl)
1243    {
1244      j=0;
1245      loop
1246      {
1247        if (j>=k) break;
1248        clearS(strat->S[j],strat->sevS[j],&k,&j,strat);
1249        j++;
1250      }
1251      k++;
1252    }
1253  }
1254
1255  if (TEST_OPT_REDSB)
1256  {
1257    completeReduce(strat);
1258#ifdef HAVE_TAIL_RING
1259    if (strat->completeReduce_retry)
1260    {
1261      // completeReduce needed larger exponents, retry
1262      // to reduce with S (instead of T)
1263      // and in currRing (instead of strat->tailRing)
1264      cleanT(strat);strat->tailRing=currRing;
1265      int i;
1266      for(i=strat->sl;i>=0;i--) strat->S_2_R[i]=-1;
1267      completeReduce(strat);
1268    }
1269#endif
1270  }
1271  else if (TEST_OPT_PROT) PrintLn();
1272
1273  /* release temp data-------------------------------- */
1274  exitBuchMora(strat);
1275  if (TEST_OPT_WEIGHTM)
1276  {
1277    pRestoreDegProcs(pFDegOld, pLDegOld);
1278    if (ecartWeights)
1279    {
1280      omFreeSize((ADDRESS)ecartWeights,(pVariables+1)*sizeof(short));
1281      ecartWeights=NULL;
1282    }
1283  }
1284  if (TEST_OPT_PROT) messageStat(srmax,lrmax,hilbcount,strat);
1285  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
1286  return (strat->Shdl);
1287}
1288
1289poly kNF2 (ideal F,ideal Q,poly q,kStrategy strat, int lazyReduce)
1290{
1291  poly   p;
1292  int   i;
1293
1294  if ((idIs0(F))&&(Q==NULL))
1295    return pCopy(q); /*F=0*/
1296  strat->ak = idRankFreeModule(F);
1297  /*- creating temp data structures------------------- -*/
1298  BITSET save_test=test;
1299  test|=Sy_bit(OPT_REDTAIL);
1300  initBuchMoraCrit(strat);
1301  strat->initEcart = initEcartBBA;
1302  strat->enterS = enterSBba;
1303#ifndef NO_BUCKETS
1304  strat->use_buckets = (!TEST_OPT_NOT_BUCKETS) && (!rIsPluralRing(currRing));
1305#endif
1306  /*- set S -*/
1307  strat->sl = -1;
1308  /*- init local data struct.---------------------------------------- -*/
1309  /*Shdl=*/initS(F,Q,strat);
1310  /*- compute------------------------------------------------------- -*/
1311  //if ((TEST_OPT_INTSTRATEGY)&&(lazyReduce==0))
1312  //{
1313  //  for (i=strat->sl;i>=0;i--)
1314  //    pNorm(strat->S[i]);
1315  //}
1316  kTest(strat);
1317  if (TEST_OPT_PROT) { PrintS("r"); mflush(); }
1318  int max_ind;
1319  p = redNF(pCopy(q),max_ind,strat);
1320  if ((p!=NULL)&&((lazyReduce & KSTD_NF_LAZY)==0))
1321  {
1322    BITSET save=test;
1323    test &= ~Sy_bit(OPT_INTSTRATEGY);
1324    if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
1325    p = redtailBba(p,max_ind,strat,(lazyReduce & KSTD_NF_NONORM)==0);
1326    test=save;
1327  }
1328  /*- release temp data------------------------------- -*/
1329  omfree(strat->sevS);
1330  omfree(strat->ecartS);
1331  omfree(strat->T);
1332  omfree(strat->sevT);
1333  omfree(strat->R);
1334  omfree(strat->S_2_R);
1335  omfree(strat->L);
1336  omfree(strat->B);
1337  omfree(strat->fromQ);
1338  idDelete(&strat->Shdl);
1339  test=save_test;
1340  if (TEST_OPT_PROT) PrintLn();
1341  return p;
1342}
1343
1344ideal kNF2 (ideal F,ideal Q,ideal q,kStrategy strat, int lazyReduce)
1345{
1346  poly   p;
1347  int   i;
1348  ideal res;
1349  int max_ind;
1350
1351  if (idIs0(q))
1352    return idInit(IDELEMS(q),si_max(q->rank,F->rank));
1353  if ((idIs0(F))&&(Q==NULL))
1354    return idCopy(q); /*F=0*/
1355  strat->ak = idRankFreeModule(F);
1356  /*- creating temp data structures------------------- -*/
1357  BITSET save_test=test;
1358  test|=Sy_bit(OPT_REDTAIL);
1359  initBuchMoraCrit(strat);
1360  strat->initEcart = initEcartBBA;
1361  strat->enterS = enterSBba;
1362  /*- set S -*/
1363  strat->sl = -1;
1364#ifndef NO_BUCKETS
1365  strat->use_buckets = (!TEST_OPT_NOT_BUCKETS) && (!rIsPluralRing(currRing));
1366#endif
1367  /*- init local data struct.---------------------------------------- -*/
1368  /*Shdl=*/initS(F,Q,strat);
1369  /*- compute------------------------------------------------------- -*/
1370  res=idInit(IDELEMS(q),si_max(q->rank,F->rank));
1371  for (i=IDELEMS(q)-1; i>=0; i--)
1372  {
1373    if (q->m[i]!=NULL)
1374    {
1375      if (TEST_OPT_PROT) { PrintS("r");mflush(); }
1376      p = redNF(pCopy(q->m[i]),max_ind,strat);
1377      if ((p!=NULL)&&((lazyReduce & KSTD_NF_LAZY)==0))
1378      {
1379        BITSET save=test;
1380        test &= ~Sy_bit(OPT_INTSTRATEGY);
1381        if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
1382        p = redtailBba(p,max_ind,strat,(lazyReduce & KSTD_NF_NONORM)==0);
1383        test=save;
1384      }
1385      res->m[i]=p;
1386    }
1387    //else
1388    //  res->m[i]=NULL;
1389  }
1390  /*- release temp data------------------------------- -*/
1391  omfree(strat->sevS);
1392  omfree(strat->ecartS);
1393  omfree(strat->T);
1394  omfree(strat->sevT);
1395  omfree(strat->R);
1396  omfree(strat->S_2_R);
1397  omfree(strat->L);
1398  omfree(strat->B);
1399  omfree(strat->fromQ);
1400  idDelete(&strat->Shdl);
1401  test=save_test;
1402  if (TEST_OPT_PROT) PrintLn();
1403  return res;
1404}
1405
1406/* shiftgb stuff */
1407#ifdef HAVE_SHIFTBBA
1408
1409
1410ideal bbaShift(ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat, int uptodeg, int lV)
1411{
1412#ifdef KDEBUG
1413  bba_count++;
1414  int loop_count = 0;
1415#endif
1416  om_Opts.MinTrack = 5;
1417  int   srmax,lrmax, red_result = 1;
1418  int   olddeg,reduc;
1419  int hilbeledeg=1,hilbcount=0,minimcnt=0;
1420  BOOLEAN withT = TRUE; // very important for shifts
1421
1422  initBuchMoraCrit(strat); /*set Gebauer, honey, sugarCrit, NO CHANGES */
1423  initBuchMoraPos(strat); /*NO CHANGES YET: perhaps later*/
1424  initHilbCrit(F,Q,&hilb,strat); /*NO CHANGES*/
1425  initBbaShift(F,strat); /* DONE */
1426  /*set enterS, spSpolyShort, reduce, red, initEcart, initEcartPair*/
1427  /*Shdl=*/initBuchMoraShift(F, Q,strat); /* updateS with no toT, i.e. no init for T */
1428  updateSShift(strat,uptodeg,lV); /* initializes T */
1429
1430  if (strat->minim>0) strat->M=idInit(IDELEMS(F),F->rank);
1431  srmax = strat->sl;
1432  reduc = olddeg = lrmax = 0;
1433
1434#ifndef NO_BUCKETS
1435  if (!TEST_OPT_NOT_BUCKETS)
1436    strat->use_buckets = 1;
1437#endif
1438
1439  // redtailBBa against T for inhomogenous input
1440  //  if (!K_TEST_OPT_OLDSTD)
1441  //    withT = ! strat->homog;
1442
1443  // strat->posInT = posInT_pLength;
1444  kTest_TS(strat);
1445
1446#ifdef HAVE_TAIL_RING
1447  kStratInitChangeTailRing(strat);
1448#endif
1449
1450  /* compute------------------------------------------------------- */
1451  while (strat->Ll >= 0)
1452  {
1453    if (strat->Ll > lrmax) lrmax =strat->Ll;/*stat.*/
1454#ifdef KDEBUG
1455    loop_count++;
1456    if (TEST_OPT_DEBUG) messageSets(strat);
1457#endif
1458    if (strat->Ll== 0) strat->interpt=TRUE;
1459    if (TEST_OPT_DEGBOUND
1460        && ((strat->honey && (strat->L[strat->Ll].ecart+pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
1461            || ((!strat->honey) && (pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))))
1462    {
1463      /*
1464       *stops computation if
1465       * 24 IN test and the degree +ecart of L[strat->Ll] is bigger then
1466       *a predefined number Kstd1_deg
1467       */
1468      while ((strat->Ll >= 0)
1469        && (strat->L[strat->Ll].p1!=NULL) && (strat->L[strat->Ll].p2!=NULL)
1470        && ((strat->honey && (strat->L[strat->Ll].ecart+pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
1471            || ((!strat->honey) && (pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg)))
1472        )
1473        deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
1474      if (strat->Ll<0) break;
1475      else strat->noClearS=TRUE;
1476    }
1477    /* picks the last element from the lazyset L */
1478    strat->P = strat->L[strat->Ll];
1479    strat->Ll--;
1480
1481    if (pNext(strat->P.p) == strat->tail)
1482    {
1483      // deletes the short spoly
1484      pLmFree(strat->P.p);
1485      strat->P.p = NULL;
1486      poly m1 = NULL, m2 = NULL;
1487
1488      // check that spoly creation is ok
1489      while (strat->tailRing != currRing &&
1490             !kCheckSpolyCreation(&(strat->P), strat, m1, m2))
1491      {
1492        assume(m1 == NULL && m2 == NULL);
1493        // if not, change to a ring where exponents are at least
1494        // large enough
1495        kStratChangeTailRing(strat);
1496      }
1497      // create the real one
1498      ksCreateSpoly(&(strat->P), NULL, strat->use_buckets,
1499                    strat->tailRing, m1, m2, strat->R);
1500    }
1501    else if (strat->P.p1 == NULL)
1502    {
1503      if (strat->minim > 0)
1504        strat->P.p2=p_Copy(strat->P.p, currRing, strat->tailRing);
1505      // for input polys, prepare reduction
1506      strat->P.PrepareRed(strat->use_buckets);
1507    }
1508
1509    poly qq;
1510
1511    /* here in the nonhomog case we shrink the new spoly */
1512
1513    if ( ! strat->homog)
1514    {
1515      strat->P.GetP(strat->lmBin); // because shifts are counted with .p structure
1516      /* assume strat->P.t_p != NULL */
1517      /* in the nonhomog case we have to shrink the polynomial */
1518      assume(strat->P.t_p!=NULL);
1519      qq = p_Shrink(strat->P.t_p, lV, strat->tailRing); // direct shrink
1520      if (qq != NULL)
1521      {
1522         /* we're here if Shrink is nonzero */
1523        //         strat->P.p =  NULL;
1524        //        strat->P.Delete(); /* deletes P.p and P.t_p */ //error
1525        strat->P.p   =  NULL; // is not set by Delete
1526        strat->P.t_p =  qq;
1527        strat->P.GetP(strat->lmBin);
1528        // update sev and length
1529        strat->initEcart(&(strat->P));
1530        strat->P.sev = pGetShortExpVector(strat->P.p);
1531//         strat->P.FDeg = strat->P.pFDeg();
1532//         strat->P.length = strat->P.pLDeg();
1533//         strat->P.pLength =strat->P.GetpLength(); //pLength(strat->P.p);
1534      }
1535      else
1536      {
1537         /* Shrink is zero, like y(1)*y(2) - y(1)*y(3)*/
1538#ifdef KDEBUG
1539         if (TEST_OPT_DEBUG){PrintS("nonzero s shrinks to 0");PrintLn();}
1540#endif
1541         //         strat->P.Delete();  // cause error
1542         strat->P.p = NULL;
1543         strat->P.t_p = NULL;
1544           //         strat->P.p = NULL; // or delete strat->P.p ?
1545       }
1546    }
1547      /* end shrinking poly in the nonhomog case */
1548
1549    if (strat->P.p == NULL && strat->P.t_p == NULL)
1550    {
1551      red_result = 0;
1552    }
1553    else
1554    {
1555      if (TEST_OPT_PROT)
1556        message((strat->honey ? strat->P.ecart : 0) + strat->P.pFDeg(),
1557                &olddeg,&reduc,strat, red_result);
1558
1559      /* reduction of the element choosen from L */
1560      red_result = strat->red(&strat->P,strat);
1561    }
1562
1563    // reduction to non-zero new poly
1564    if (red_result == 1)
1565    {
1566      /* statistic */
1567      if (TEST_OPT_PROT) PrintS("s");
1568
1569      // get the polynomial (canonicalize bucket, make sure P.p is set)
1570      strat->P.GetP(strat->lmBin);
1571
1572      int pos=posInS(strat,strat->sl,strat->P.p,strat->P.ecart);
1573
1574      // reduce the tail and normalize poly
1575      if (TEST_OPT_INTSTRATEGY)
1576      {
1577        strat->P.pCleardenom();
1578        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
1579        {
1580          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
1581          strat->P.pCleardenom();
1582        }
1583      }
1584      else
1585      {
1586        strat->P.pNorm();
1587        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
1588          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
1589      }
1590
1591      // here we must shrink again! and optionally reduce again
1592      // or build shrink into redtailBba!
1593
1594#ifdef KDEBUG
1595      if (TEST_OPT_DEBUG){PrintS("new s:");strat->P.wrp();PrintLn();}
1596#endif
1597
1598      // min_std stuff
1599      if ((strat->P.p1==NULL) && (strat->minim>0))
1600      {
1601        if (strat->minim==1)
1602        {
1603          strat->M->m[minimcnt]=p_Copy(strat->P.p,currRing,strat->tailRing);
1604          p_Delete(&strat->P.p2, currRing, strat->tailRing);
1605        }
1606        else
1607        {
1608          strat->M->m[minimcnt]=strat->P.p2;
1609          strat->P.p2=NULL;
1610        }
1611        if (strat->tailRing!=currRing && pNext(strat->M->m[minimcnt])!=NULL)
1612          pNext(strat->M->m[minimcnt])
1613            = strat->p_shallow_copy_delete(pNext(strat->M->m[minimcnt]),
1614                                           strat->tailRing, currRing,
1615                                           currRing->PolyBin);
1616        minimcnt++;
1617      }
1618
1619    /* here in the nonhomog case we shrink the reduced poly AGAIN */
1620
1621    if ( ! strat->homog)
1622    {
1623      strat->P.GetP(strat->lmBin); // because shifts are counted with .p structure
1624      /* assume strat->P.t_p != NULL */
1625      /* in the nonhomog case we have to shrink the polynomial */
1626      assume(strat->P.t_p!=NULL); // poly qq defined above
1627      qq = p_Shrink(strat->P.t_p, lV, strat->tailRing); // direct shrink
1628      if (qq != NULL)
1629      {
1630         /* we're here if Shrink is nonzero */
1631        //         strat->P.p =  NULL;
1632        //        strat->P.Delete(); /* deletes P.p and P.t_p */ //error
1633        strat->P.p   =  NULL; // is not set by Delete
1634        strat->P.t_p =  qq;
1635        strat->P.GetP(strat->lmBin);
1636        // update sev and length
1637        strat->initEcart(&(strat->P));
1638        strat->P.sev = pGetShortExpVector(strat->P.p);
1639      }
1640      else
1641      {
1642         /* Shrink is zero, like y(1)*y(2) - y(1)*y(3)*/
1643#ifdef PDEBUG
1644         if (TEST_OPT_DEBUG){PrintS("nonzero s shrinks to 0");PrintLn();}
1645#endif
1646         //         strat->P.Delete();  // cause error
1647         strat->P.p = NULL;
1648         strat->P.t_p = NULL;
1649           //         strat->P.p = NULL; // or delete strat->P.p ?
1650         goto     red_shrink2zero;
1651       }
1652    }
1653      /* end shrinking poly AGAIN in the nonhomog case */
1654
1655
1656      // enter into S, L, and T
1657      //if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
1658      //        enterT(strat->P, strat); // this was here before Shift stuff
1659      //enterTShift(LObject p, kStrategy strat, int atT, int uptodeg, int lV); // syntax
1660      // the default value for atT = -1 as in bba
1661      /*   strat->P.GetP(); */
1662      // because shifts are counted with .p structure // done before, but ?
1663      enterTShift(strat->P,strat,-1,uptodeg, lV);
1664      enterpairsShift(strat->P.p,strat->sl,strat->P.ecart,pos,strat, strat->tl,uptodeg,lV);
1665      //      enterpairsShift(vw,strat->sl,strat->P.ecart,pos,strat, strat->tl,uptodeg,lV);
1666      // posInS only depends on the leading term
1667      if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
1668      {
1669        strat->enterS(strat->P, pos, strat, strat->tl);
1670      }
1671      else
1672      {
1673      //  strat->P.Delete(); // syzComp test: it is in T
1674      }
1675
1676      if (hilb!=NULL) khCheck(Q,w,hilb,hilbeledeg,hilbcount,strat);
1677//      Print("[%d]",hilbeledeg);
1678      if (strat->P.lcm!=NULL) pLmFree(strat->P.lcm);
1679      if (strat->sl>srmax) srmax = strat->sl;
1680    }
1681    else
1682    {
1683    red_shrink2zero:
1684      if (strat->P.p1 == NULL && strat->minim > 0)
1685      {
1686        p_Delete(&strat->P.p2, currRing, strat->tailRing);
1687      }
1688    }
1689#ifdef KDEBUG
1690    memset(&(strat->P), 0, sizeof(strat->P));
1691#endif
1692    kTest_TS(strat);
1693  }
1694#ifdef KDEBUG
1695  if (TEST_OPT_DEBUG) messageSets(strat);
1696#endif
1697  /* complete reduction of the standard basis--------- */
1698  if (TEST_OPT_SB_1)
1699  {
1700    int k=1;
1701    int j;
1702    while(k<=strat->sl)
1703    {
1704      j=0;
1705      loop
1706      {
1707        if (j>=k) break;
1708        clearS(strat->S[j],strat->sevS[j],&k,&j,strat);
1709        j++;
1710      }
1711      k++;
1712    }
1713  }
1714
1715  if (TEST_OPT_REDSB)
1716  {
1717    completeReduce(strat, TRUE); //shift: withT = TRUE
1718    if (strat->completeReduce_retry)
1719    {
1720      // completeReduce needed larger exponents, retry
1721      // to reduce with S (instead of T)
1722      // and in currRing (instead of strat->tailRing)
1723      cleanT(strat);strat->tailRing=currRing;
1724      int i;
1725      for(i=strat->sl;i>=0;i--) strat->S_2_R[i]=-1;
1726      completeReduce(strat, TRUE);
1727    }
1728  }
1729  else if (TEST_OPT_PROT) PrintLn();
1730
1731  /* release temp data-------------------------------- */
1732  exitBuchMora(strat);
1733  if (TEST_OPT_WEIGHTM)
1734  {
1735    pRestoreDegProcs(pFDegOld, pLDegOld);
1736    if (ecartWeights)
1737    {
1738      omFreeSize((ADDRESS)ecartWeights,(pVariables+1)*sizeof(short));
1739      ecartWeights=NULL;
1740    }
1741  }
1742  if (TEST_OPT_PROT) messageStat(srmax,lrmax,hilbcount,strat);
1743  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
1744  return (strat->Shdl);
1745}
1746
1747
1748ideal freegb(ideal I, int uptodeg, int lVblock)
1749{
1750  /* todo main call */
1751
1752  /* assume: ring is prepared, ideal is copied into shifted ring */
1753  /* uptodeg and lVblock are correct - test them! */
1754
1755  /* check whether the ideal is in V */
1756  if (! ideal_isInV(I,lVblock) )
1757  {
1758    PrintS("ERROR: The input ideal contains incorrectly encoded elements! ");
1759    return(NULL);
1760  }
1761
1762  //  kStrategy strat = new skStrategy;
1763  /* ideal bbaShift(ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat, int uptodeg, int lV) */
1764  /* at the moment:
1765- no quotient (check)
1766- no *w, no *hilb
1767  */
1768  /* ideal F, ideal Q, tHomog h,intvec ** w, intvec *hilb,int syzComp,
1769     int newIdeal, intvec *vw) */
1770  ideal RS = kStdShift(I,NULL, testHomog, NULL,NULL,0,0,NULL, uptodeg, lVblock);
1771    //bbaShift(I,NULL, NULL, NULL, strat, uptodeg, lVblock);
1772  idSkipZeroes(RS);
1773  return(RS);
1774}
1775
1776/*2
1777*reduces h with elements from T choosing  the first possible
1778* element in t with respect to the given pDivisibleBy
1779*/
1780int redFirstShift (LObject* h,kStrategy strat)
1781{
1782  if (h->IsNull()) return 0;
1783
1784  int at, reddeg,d;
1785  int pass = 0;
1786  int j = 0;
1787
1788  if (! strat->homog)
1789  {
1790    d = h->GetpFDeg() + h->ecart;
1791    reddeg = strat->LazyDegree+d;
1792  }
1793  h->SetShortExpVector();
1794  loop
1795  {
1796    j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
1797    if (j < 0)
1798    {
1799      h->SetDegStuffReturnLDeg(strat->LDegLast);
1800      return 1;
1801    }
1802
1803    if (!TEST_OPT_INTSTRATEGY)
1804      strat->T[j].pNorm();
1805#ifdef KDEBUG
1806    if (TEST_OPT_DEBUG)
1807    {
1808      PrintS("reduce ");
1809      h->wrp();
1810      PrintS(" with ");
1811      strat->T[j].wrp();
1812    }
1813#endif
1814    ksReducePoly(h, &(strat->T[j]), strat->kNoetherTail(), NULL, strat);
1815#ifdef KDEBUG
1816    if (TEST_OPT_DEBUG)
1817    {
1818      PrintS(" to ");
1819      wrp(h->p);
1820      PrintLn();
1821    }
1822#endif
1823    if (h->IsNull())
1824    {
1825      if (h->lcm!=NULL) pLmFree(h->lcm);
1826      h->Clear();
1827      return 0;
1828    }
1829    h->SetShortExpVector();
1830
1831#if 0
1832    if ((strat->syzComp!=0) && !strat->honey)
1833    {
1834      if ((strat->syzComp>0) &&
1835          (h->Comp() > strat->syzComp))
1836      {
1837        assume(h->MinComp() > strat->syzComp);
1838#ifdef KDEBUG
1839        if (TEST_OPT_DEBUG) PrintS(" > syzComp\n");
1840#endif
1841        if (strat->homog)
1842          h->SetDegStuffReturnLDeg(strat->LDegLast);
1843        return -2;
1844      }
1845    }
1846#endif
1847    if (!strat->homog)
1848    {
1849      if (!K_TEST_OPT_OLDSTD && strat->honey)
1850      {
1851        h->SetpFDeg();
1852        if (strat->T[j].ecart <= h->ecart)
1853          h->ecart = d - h->GetpFDeg();
1854        else
1855          h->ecart = d - h->GetpFDeg() + strat->T[j].ecart - h->ecart;
1856
1857        d = h->GetpFDeg() + h->ecart;
1858      }
1859      else
1860        d = h->SetDegStuffReturnLDeg(strat->LDegLast);
1861      /*- try to reduce the s-polynomial -*/
1862      pass++;
1863      /*
1864       *test whether the polynomial should go to the lazyset L
1865       *-if the degree jumps
1866       *-if the number of pre-defined reductions jumps
1867       */
1868      if (!K_TEST_OPT_REDTHROUGH && (strat->Ll >= 0)
1869          && ((d >= reddeg) || (pass > strat->LazyPass)))
1870      {
1871        h->SetLmCurrRing();
1872        if (strat->posInLDependsOnLength)
1873          h->SetLength(strat->length_pLength);
1874        at = strat->posInL(strat->L,strat->Ll,h,strat);
1875        if (at <= strat->Ll)
1876        {
1877          int dummy=strat->sl;
1878          /*          if (kFindDivisibleByInS(strat,&dummy, h) < 0) */
1879          if (kFindDivisibleByInT(strat->T,strat->sevT, dummy, h) < 0)
1880            return 1;
1881          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
1882#ifdef KDEBUG
1883          if (TEST_OPT_DEBUG) Print(" degree jumped; ->L%d\n",at);
1884#endif
1885          h->Clear();
1886          return -1;
1887        }
1888      }
1889      if ((TEST_OPT_PROT) && (strat->Ll < 0) && (d >= reddeg))
1890      {
1891        reddeg = d+1;
1892        Print(".%d",d);mflush();
1893      }
1894    }
1895  }
1896}
1897
1898void initBbaShift(ideal F,kStrategy strat)
1899{
1900  int i;
1901  idhdl h;
1902 /* setting global variables ------------------- */
1903  strat->enterS = enterSBba; /* remains as is, we change enterT! */
1904
1905  strat->red = redFirstShift; /* no redHomog ! */
1906
1907  if (pLexOrder && strat->honey)
1908    strat->initEcart = initEcartNormal;
1909  else
1910    strat->initEcart = initEcartBBA;
1911  if (strat->honey)
1912    strat->initEcartPair = initEcartPairMora;
1913  else
1914    strat->initEcartPair = initEcartPairBba;
1915  strat->kIdeal = NULL;
1916  //if (strat->ak==0) strat->kIdeal->rtyp=IDEAL_CMD;
1917  //else              strat->kIdeal->rtyp=MODUL_CMD;
1918  //strat->kIdeal->data=(void *)strat->Shdl;
1919  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1920  {
1921    //interred  machen   Aenderung
1922    pFDegOld=pFDeg;
1923    pLDegOld=pLDeg;
1924    //h=ggetid("ecart");
1925    //if ((h!=NULL) /*&& (IDTYP(h)==INTVEC_CMD)*/)
1926    //{
1927    //  ecartWeights=iv2array(IDINTVEC(h));
1928    //}
1929    //else
1930    {
1931      ecartWeights=(short *)omAlloc((pVariables+1)*sizeof(short));
1932      /*uses automatic computation of the ecartWeights to set them*/
1933      kEcartWeights(F->m,IDELEMS(F)-1,ecartWeights);
1934    }
1935    pRestoreDegProcs(totaldegreeWecart, maxdegreeWecart);
1936    if (TEST_OPT_PROT)
1937    {
1938      for(i=1; i<=pVariables; i++)
1939        Print(" %d",ecartWeights[i]);
1940      PrintLn();
1941      mflush();
1942    }
1943  }
1944}
1945#endif
Note: See TracBrowser for help on using the repository browser.