source: git/kernel/kstd2.cc @ 5eb865

spielwiese
Last change on this file since 5eb865 was 5eb865, checked in by Hans Schönemann <hannes@…>, 15 years ago
*hannes: redtail for Z git-svn-id: file:///usr/local/Singular/svn/trunk@11891 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 47.3 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: kstd2.cc,v 1.88 2009-06-09 18:21:50 Singular 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_RINGS
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  poly h_p;
634  int i,j,at,pass,ei, ii, h_d;
635  unsigned long not_sev;
636  long reddeg,d;
637
638  pass = j = 0;
639  d = reddeg = h->GetpFDeg() + h->ecart;
640  h->SetShortExpVector();
641  int li;
642  h_p = h->GetLmTailRing();
643  not_sev = ~ h->sev;
644
645  h->PrepareRed(strat->use_buckets);
646  loop
647  {
648    j=kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
649    if (j < 0) return 1;
650
651    ei = strat->T[j].ecart;
652    li = strat->T[j].pLength;
653    ii = j;
654    /*
655     * the polynomial to reduce with (up to the moment) is;
656     * pi with ecart ei
657     */
658    i = j;
659    if (TEST_OPT_LENGTH)
660    loop
661    {
662      /*- takes the first possible with respect to ecart -*/
663      i++;
664      if (i > strat->tl)
665        break;
666      //if (ei < h->ecart)
667      //  break;
668      if (li<=1)
669        break;
670      if ((((strat->T[i].ecart < ei) && (ei> h->ecart))
671         || ((strat->T[i].ecart <= h->ecart) && (strat->T[i].pLength < li)))
672         &&
673          p_LmShortDivisibleBy(strat->T[i].GetLmTailRing(), strat->sevT[i],
674                               h_p, not_sev, strat->tailRing))
675      {
676        /*
677         * the polynomial to reduce with is now;
678         */
679        ei = strat->T[i].ecart;
680        li = strat->T[i].pLength;
681        ii = i;
682      }
683    }
684
685    /*
686     * end of search: have to reduce with pi
687     */
688    if (!K_TEST_OPT_REDTHROUGH && (pass!=0) && (ei > h->ecart))
689    {
690      h->GetTP(); // clears bucket
691      h->SetLmCurrRing();
692      /*
693       * It is not possible to reduce h with smaller ecart;
694       * if possible h goes to the lazy-set L,i.e
695       * if its position in L would be not the last one
696       */
697      if (strat->Ll >= 0) /* L is not empty */
698      {
699        at = strat->posInL(strat->L,strat->Ll,h,strat);
700        if(at <= strat->Ll)
701          /*- h will not become the next element to reduce -*/
702        {
703          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
704#ifdef KDEBUG
705          if (TEST_OPT_DEBUG) Print(" ecart too big: -> L%d\n",at);
706#endif
707          h->Clear();
708          return -1;
709        }
710      }
711    }
712#ifdef KDEBUG
713    if (TEST_OPT_DEBUG)
714    {
715      PrintS("red:");
716      h->wrp();
717      PrintS(" with ");
718      strat->T[ii].wrp();
719    }
720#endif
721    assume(strat->fromT == FALSE);
722
723    number coef;
724    ksReducePoly(h,&(strat->T[ii]),strat->kNoetherTail(),&coef,strat);
725#ifdef KDEBUG
726    if (TEST_OPT_DEBUG)
727    {
728      PrintS("\nto:");
729      h->wrp();
730      PrintLn();
731    }
732#endif
733    if(h->IsNull())
734    {
735      h->Clear();
736      if (h->lcm!=NULL) pLmFree(h->lcm);
737      #ifdef KDEBUG
738      h->lcm=NULL;
739      #endif
740      return 0;
741    }
742    h->SetShortExpVector();
743    not_sev = ~ h->sev;
744    h_d = h->SetpFDeg();
745    /* compute the ecart */
746    if (ei <= h->ecart)
747      h->ecart = d-h_d;
748    else
749      h->ecart = d-h_d+ei-h->ecart;
750
751    /*
752     * try to reduce the s-polynomial h
753     *test first whether h should go to the lazyset L
754     *-if the degree jumps
755     *-if the number of pre-defined reductions jumps
756     */
757    pass++;
758    d = h_d + h->ecart;
759    if (!K_TEST_OPT_REDTHROUGH && (strat->Ll >= 0) && ((d > reddeg) || (pass > strat->LazyPass)))
760    {
761      h->GetTP(); // clear bucket
762      h->SetLmCurrRing();
763      at = strat->posInL(strat->L,strat->Ll,h,strat);
764      if (at <= strat->Ll)
765      {
766        int dummy=strat->sl;
767        if (kFindDivisibleByInS(strat, &dummy, h) < 0)
768          return 1;
769        enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
770#ifdef KDEBUG
771        if (TEST_OPT_DEBUG)
772          Print(" degree jumped: -> L%d\n",at);
773#endif
774        h->Clear();
775        return -1;
776      }
777    }
778    else if (TEST_OPT_PROT && (strat->Ll < 0) && (d > reddeg))
779    {
780      //h->wrp(); Print("<%d>\n",h->GetpLength());
781      reddeg = d;
782      Print(".%ld",d); mflush();
783    }
784  }
785}
786/*2
787*  reduction procedure for the normal form
788*/
789
790poly redNF (poly h,int &max_ind,int nonorm,kStrategy strat)
791{
792  if (h==NULL) return NULL;
793  int j;
794  max_ind=strat->sl;
795
796  if (0 > strat->sl)
797  {
798    return h;
799  }
800  LObject P(h);
801  P.SetShortExpVector();
802  P.bucket = kBucketCreate(currRing);
803  kBucketInit(P.bucket,P.p,pLength(P.p));
804  kbTest(P.bucket);
805#ifdef HAVE_RINGS
806  BOOLEAN is_ring = rField_is_Ring(currRing);
807#endif
808  loop
809  {
810    j=kFindDivisibleByInS(strat,&max_ind,&P);
811    if (j>=0)
812    {
813#ifdef HAVE_RINGS
814      if (!is_ring)
815      {
816#endif
817        int sl=pSize(strat->S[j]);
818        int jj=j;
819        loop
820        {
821          int sll;
822          jj=kFindNextDivisibleByInS(strat,jj+1,max_ind,&P);
823          if (jj<0) break;
824          sll=pSize(strat->S[jj]);
825          if (sll<sl)
826          {
827            #ifdef KDEBUG
828            if (TEST_OPT_DEBUG) Print("better(S%d:%d -> S%d:%d)\n",j,sl,jj,sll);
829            #endif
830            //else if (TEST_OPT_PROT) { PrintS("b"); mflush(); }
831            j=jj;
832            sl=sll;
833          }
834        }
835        if ((nonorm==0) && (!nIsOne(pGetCoeff(strat->S[j]))))
836        {
837          pNorm(strat->S[j]);
838          //if (TEST_OPT_PROT) { PrintS("n"); mflush(); }
839        }
840#ifdef HAVE_RINGS
841      }
842#endif
843      nNormalize(pGetCoeff(P.p));
844#ifdef KDEBUG
845      if (TEST_OPT_DEBUG)
846      {
847        PrintS("red:");
848        wrp(h);
849        PrintS(" with ");
850        wrp(strat->S[j]);
851      }
852#endif
853#ifdef HAVE_PLURAL
854      if (rIsPluralRing(currRing))
855      {
856        number coef;
857        nc_kBucketPolyRed(P.bucket,strat->S[j],&coef);
858        nDelete(&coef);
859      }
860      else
861#endif
862      {
863        number coef;
864        coef=kBucketPolyRed(P.bucket,strat->S[j],pLength(strat->S[j]),strat->kNoether);
865        nDelete(&coef);
866      }
867      h = kBucketGetLm(P.bucket);   // FRAGE OLIVER
868      if (h==NULL)
869      {
870        kBucketDestroy(&P.bucket);
871        return NULL;
872      }
873      kbTest(P.bucket);
874      P.p=h;
875      P.t_p=NULL;
876      P.SetShortExpVector();
877#ifdef KDEBUG
878      if (TEST_OPT_DEBUG)
879      {
880        PrintS("\nto:");
881        wrp(h);
882        PrintLn();
883      }
884#endif
885    }
886    else
887    {
888      P.p=kBucketClear(P.bucket);
889      kBucketDestroy(&P.bucket);
890      pNormalize(P.p);
891      return P.p;
892    }
893  }
894}
895
896#ifdef KDEBUG
897static int bba_count = 0;
898#endif
899void kDebugPrint(kStrategy strat);
900
901ideal bba (ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat)
902{
903#ifdef KDEBUG
904  bba_count++;
905  int loop_count = 0;
906#endif
907  om_Opts.MinTrack = 5;
908  int   srmax,lrmax, red_result = 1;
909  int   olddeg,reduc;
910  int hilbeledeg=1,hilbcount=0,minimcnt=0;
911  BOOLEAN withT = FALSE;
912
913  initBuchMoraCrit(strat); /*set Gebauer, honey, sugarCrit*/
914  initBuchMoraPos(strat);
915  initHilbCrit(F,Q,&hilb,strat);
916  initBba(F,strat);
917  /*set enterS, spSpolyShort, reduce, red, initEcart, initEcartPair*/
918  /*Shdl=*/initBuchMora(F, Q,strat);
919  if (strat->minim>0) strat->M=idInit(IDELEMS(F),F->rank);
920  srmax = strat->sl;
921  reduc = olddeg = lrmax = 0;
922
923#ifndef NO_BUCKETS
924  if (!TEST_OPT_NOT_BUCKETS)
925    strat->use_buckets = 1;
926#endif
927
928  // redtailBBa against T for inhomogenous input
929  if (!K_TEST_OPT_OLDSTD)
930    withT = ! strat->homog;
931
932  // strat->posInT = posInT_pLength;
933  kTest_TS(strat);
934
935#ifdef HAVE_TAIL_RING
936  kStratInitChangeTailRing(strat);
937#endif
938  //kDebugPrint(strat);
939
940  /* compute------------------------------------------------------- */
941  while (strat->Ll >= 0)
942  {
943    if (strat->Ll > lrmax) lrmax =strat->Ll;/*stat.*/
944    #ifdef KDEBUG
945      loop_count++;
946      #ifdef HAVE_RINGS
947        if (TEST_OPT_DEBUG) PrintS("--- next step ---\n");
948      #endif
949      if (TEST_OPT_DEBUG) messageSets(strat);
950    #endif
951    if (strat->Ll== 0) strat->interpt=TRUE;
952    if (TEST_OPT_DEGBOUND
953        && ((strat->honey && (strat->L[strat->Ll].ecart+pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
954            || ((!strat->honey) && (pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))))
955    {
956      /*
957       *stops computation if
958       * 24 IN test and the degree +ecart of L[strat->Ll] is bigger then
959       *a predefined number Kstd1_deg
960       */
961      while ((strat->Ll >= 0)
962        && (strat->L[strat->Ll].p1!=NULL) && (strat->L[strat->Ll].p2!=NULL)
963        && ((strat->honey && (strat->L[strat->Ll].ecart+pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
964            || ((!strat->honey) && (pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg)))
965        )
966        deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
967      if (strat->Ll<0) break;
968      else strat->noClearS=TRUE;
969    }
970    /* picks the last element from the lazyset L */
971    strat->P = strat->L[strat->Ll];
972    strat->Ll--;
973
974    if (pNext(strat->P.p) == strat->tail)
975    {
976      // deletes the short spoly
977#ifdef HAVE_RINGS
978      if (rField_is_Ring(currRing))
979        pLmDelete(strat->P.p);
980      else
981#endif
982        pLmFree(strat->P.p);
983      strat->P.p = NULL;
984      poly m1 = NULL, m2 = NULL;
985
986      // check that spoly creation is ok
987      while (strat->tailRing != currRing &&
988             !kCheckSpolyCreation(&(strat->P), strat, m1, m2))
989      {
990        assume(m1 == NULL && m2 == NULL);
991        // if not, change to a ring where exponents are at least
992        // large enough
993        kStratChangeTailRing(strat);
994      }
995      // create the real one
996      ksCreateSpoly(&(strat->P), NULL, strat->use_buckets,
997                    strat->tailRing, m1, m2, strat->R);
998    }
999    else if (strat->P.p1 == NULL)
1000    {
1001      if (strat->minim > 0)
1002        strat->P.p2=p_Copy(strat->P.p, currRing, strat->tailRing);
1003      // for input polys, prepare reduction
1004      strat->P.PrepareRed(strat->use_buckets);
1005    }
1006
1007    if (strat->P.p == NULL && strat->P.t_p == NULL)
1008    {
1009      red_result = 0;
1010    }
1011    else
1012    {
1013      if (TEST_OPT_PROT)
1014        message((strat->honey ? strat->P.ecart : 0) + strat->P.pFDeg(),
1015                &olddeg,&reduc,strat, red_result);
1016
1017      /* reduction of the element choosen from L */
1018      red_result = strat->red(&strat->P,strat);
1019    }
1020
1021    // reduction to non-zero new poly
1022    if (red_result == 1)
1023    {
1024      /* statistic */
1025      if (TEST_OPT_PROT) PrintS("s");
1026
1027      // get the polynomial (canonicalize bucket, make sure P.p is set)
1028      strat->P.GetP(strat->lmBin);
1029
1030      int pos=posInS(strat,strat->sl,strat->P.p,strat->P.ecart);
1031
1032      // reduce the tail and normalize poly
1033      // in the ring case we cannot expect LC(f) = 1,
1034      // therefore we call pContent instead of pNorm
1035      if ((TEST_OPT_INTSTRATEGY) || (rField_is_Ring(currRing)))
1036      {
1037        strat->P.pCleardenom();
1038        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
1039        {
1040          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
1041          strat->P.pCleardenom();
1042        }
1043      }
1044      else
1045      {
1046        strat->P.pNorm();
1047        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
1048          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
1049      }
1050
1051#ifdef KDEBUG
1052      if (TEST_OPT_DEBUG){PrintS("new s:");strat->P.wrp();PrintLn();}
1053#endif
1054
1055      // min_std stuff
1056      if ((strat->P.p1==NULL) && (strat->minim>0))
1057      {
1058        if (strat->minim==1)
1059        {
1060          strat->M->m[minimcnt]=p_Copy(strat->P.p,currRing,strat->tailRing);
1061          p_Delete(&strat->P.p2, currRing, strat->tailRing);
1062        }
1063        else
1064        {
1065          strat->M->m[minimcnt]=strat->P.p2;
1066          strat->P.p2=NULL;
1067        }
1068        if (strat->tailRing!=currRing && pNext(strat->M->m[minimcnt])!=NULL)
1069          pNext(strat->M->m[minimcnt])
1070            = strat->p_shallow_copy_delete(pNext(strat->M->m[minimcnt]),
1071                                           strat->tailRing, currRing,
1072                                           currRing->PolyBin);
1073        minimcnt++;
1074      }
1075
1076      // enter into S, L, and T
1077      //if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
1078        enterT(strat->P, strat);
1079#ifdef HAVE_RINGS
1080      if (rField_is_Ring(currRing))
1081        superenterpairs(strat->P.p,strat->sl,strat->P.ecart,pos,strat, strat->tl);
1082      else
1083#endif
1084        enterpairs(strat->P.p,strat->sl,strat->P.ecart,pos,strat, strat->tl);
1085      // posInS only depends on the leading term
1086      if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
1087      {
1088      strat->enterS(strat->P, pos, strat, strat->tl);
1089      }
1090      else
1091      {
1092      //  strat->P.Delete(); // syzComp test: it is in T
1093      }
1094#if 0
1095      int pl=pLength(strat->P.p);
1096      if (pl==1)
1097      {
1098        //if (TEST_OPT_PROT)
1099        //PrintS("<1>");
1100      }
1101      else if (pl==2)
1102      {
1103        //if (TEST_OPT_PROT)
1104        //PrintS("<2>");
1105      }
1106#endif
1107      if (hilb!=NULL) khCheck(Q,w,hilb,hilbeledeg,hilbcount,strat);
1108//      Print("[%d]",hilbeledeg);
1109      if (strat->P.lcm!=NULL)
1110#ifdef HAVE_RINGS
1111        pLmDelete(strat->P.lcm);
1112#else
1113        pLmFree(strat->P.lcm);
1114#endif
1115      if (strat->sl>srmax) srmax = strat->sl;
1116    }
1117    else if (strat->P.p1 == NULL && strat->minim > 0)
1118    {
1119      p_Delete(&strat->P.p2, currRing, strat->tailRing);
1120    }
1121
1122#ifdef KDEBUG
1123    memset(&(strat->P), 0, sizeof(strat->P));
1124#endif
1125    kTest_TS(strat);
1126  }
1127#ifdef KDEBUG
1128  if (TEST_OPT_DEBUG) messageSets(strat);
1129#endif
1130  if (TEST_OPT_SB_1)
1131  {
1132    int k=1;
1133    int j;
1134    while(k<=strat->sl)
1135    {
1136      j=0;
1137      loop
1138      {
1139        if (j>=k) break;
1140        clearS(strat->S[j],strat->sevS[j],&k,&j,strat);
1141        j++;
1142      }
1143      k++;
1144    }
1145  }
1146
1147  /* complete reduction of the standard basis--------- */
1148  if (TEST_OPT_REDSB)
1149  {
1150    completeReduce(strat);
1151#ifdef HAVE_TAIL_RING
1152    if (strat->completeReduce_retry)
1153    {
1154      // completeReduce needed larger exponents, retry
1155      // to reduce with S (instead of T)
1156      // and in currRing (instead of strat->tailRing)
1157      cleanT(strat);strat->tailRing=currRing;
1158      int i;
1159      for(i=strat->sl;i>=0;i--) strat->S_2_R[i]=-1;
1160      completeReduce(strat);
1161    }
1162#endif
1163  }
1164  else if (TEST_OPT_PROT) PrintLn();
1165
1166  /* release temp data-------------------------------- */
1167  exitBuchMora(strat);
1168  if (TEST_OPT_WEIGHTM)
1169  {
1170    pRestoreDegProcs(pFDegOld, pLDegOld);
1171    if (ecartWeights)
1172    {
1173      omFreeSize((ADDRESS)ecartWeights,(pVariables+1)*sizeof(short));
1174      ecartWeights=NULL;
1175    }
1176  }
1177  if (TEST_OPT_PROT) messageStat(srmax,lrmax,hilbcount,strat);
1178  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
1179  return (strat->Shdl);
1180}
1181
1182poly kNF2 (ideal F,ideal Q,poly q,kStrategy strat, int lazyReduce)
1183{
1184  assume(q!=NULL);
1185  assume(!(idIs0(F)&&(Q==NULL)));
1186
1187// lazy_reduce flags: can be combined by |
1188//#define KSTD_NF_LAZY   1
1189  // do only a reduction of the leading term
1190//#define KSTD_NF_NONORM 4
1191  // only global: avoid normalization, return a multiply of NF
1192  poly   p;
1193  int   i;
1194
1195  //if ((idIs0(F))&&(Q==NULL))
1196  //  return pCopy(q); /*F=0*/
1197  //strat->ak = idRankFreeModule(F);
1198  /*- creating temp data structures------------------- -*/
1199  BITSET save_test=test;
1200  test|=Sy_bit(OPT_REDTAIL);
1201  initBuchMoraCrit(strat);
1202  strat->initEcart = initEcartBBA;
1203  strat->enterS = enterSBba;
1204#ifndef NO_BUCKETS
1205  strat->use_buckets = (!TEST_OPT_NOT_BUCKETS) && (!rIsPluralRing(currRing));
1206#endif
1207  /*- set S -*/
1208  strat->sl = -1;
1209  /*- init local data struct.---------------------------------------- -*/
1210  /*Shdl=*/initS(F,Q,strat);
1211  /*- compute------------------------------------------------------- -*/
1212  //if ((TEST_OPT_INTSTRATEGY)&&(lazyReduce==0))
1213  //{
1214  //  for (i=strat->sl;i>=0;i--)
1215  //    pNorm(strat->S[i]);
1216  //}
1217  kTest(strat);
1218  if (TEST_OPT_PROT) { PrintS("r"); mflush(); }
1219  int max_ind;
1220  p = redNF(pCopy(q),max_ind,lazyReduce & KSTD_NF_NONORM,strat);
1221  if ((p!=NULL)&&((lazyReduce & KSTD_NF_LAZY)==0))
1222  {
1223    if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
1224    #ifdef HAVE_RINGS
1225    if (rField_is_Ring())
1226    {
1227      p = redtailBba_Z(p,max_ind,strat);
1228    }
1229    else
1230    #endif
1231    {
1232      BITSET save=test;
1233      test &= ~Sy_bit(OPT_INTSTRATEGY);
1234      p = redtailBba(p,max_ind,strat,(lazyReduce & KSTD_NF_NONORM)==0);
1235      test=save;
1236    }
1237  }
1238  /*- release temp data------------------------------- -*/
1239  omfree(strat->sevS);
1240  omfree(strat->ecartS);
1241  omfree(strat->T);
1242  omfree(strat->sevT);
1243  omfree(strat->R);
1244  omfree(strat->S_2_R);
1245  omfree(strat->L);
1246  omfree(strat->B);
1247  omfree(strat->fromQ);
1248  idDelete(&strat->Shdl);
1249  test=save_test;
1250  if (TEST_OPT_PROT) PrintLn();
1251  return p;
1252}
1253
1254ideal kNF2 (ideal F,ideal Q,ideal q,kStrategy strat, int lazyReduce)
1255{
1256  assume(!idIs0(q));
1257  assume(!(idIs0(F)&&(Q==NULL)));
1258// lazy_reduce flags: can be combined by |
1259//#define KSTD_NF_LAZY   1
1260  // do only a reduction of the leading term
1261//#define KSTD_NF_NONORM 4
1262  // only global: avoid normalization, return a multiply of NF
1263  poly   p;
1264  int   i;
1265  ideal res;
1266  int max_ind;
1267
1268  //if (idIs0(q))
1269  //  return idInit(IDELEMS(q),si_max(q->rank,F->rank));
1270  //if ((idIs0(F))&&(Q==NULL))
1271  //  return idCopy(q); /*F=0*/
1272  //strat->ak = idRankFreeModule(F);
1273  /*- creating temp data structures------------------- -*/
1274  BITSET save_test=test;
1275  test|=Sy_bit(OPT_REDTAIL);
1276  initBuchMoraCrit(strat);
1277  strat->initEcart = initEcartBBA;
1278  strat->enterS = enterSBba;
1279  /*- set S -*/
1280  strat->sl = -1;
1281#ifndef NO_BUCKETS
1282  strat->use_buckets = (!TEST_OPT_NOT_BUCKETS) && (!rIsPluralRing(currRing));
1283#endif
1284  /*- init local data struct.---------------------------------------- -*/
1285  /*Shdl=*/initS(F,Q,strat);
1286  /*- compute------------------------------------------------------- -*/
1287  res=idInit(IDELEMS(q),si_max(q->rank,F->rank));
1288  BITSET save=test;
1289  test &= ~Sy_bit(OPT_INTSTRATEGY);
1290  for (i=IDELEMS(q)-1; i>=0; i--)
1291  {
1292    if (q->m[i]!=NULL)
1293    {
1294      if (TEST_OPT_PROT) { PrintS("r");mflush(); }
1295      p = redNF(pCopy(q->m[i]),max_ind,lazyReduce & KSTD_NF_NONORM,strat);
1296      if ((p!=NULL)&&((lazyReduce & KSTD_NF_LAZY)==0))
1297      {
1298        if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
1299        #ifdef HAVE_RINGS
1300        if (rField_is_Ring())
1301        {
1302          p = redtailBba_Z(p,max_ind,strat);
1303        }
1304        else
1305        #endif
1306        {
1307          p = redtailBba(p,max_ind,strat,(lazyReduce & KSTD_NF_NONORM)==0);
1308        }
1309      }
1310      res->m[i]=p;
1311    }
1312    //else
1313    //  res->m[i]=NULL;
1314  }
1315  /*- release temp data------------------------------- -*/
1316  test=save;
1317  omfree(strat->sevS);
1318  omfree(strat->ecartS);
1319  omfree(strat->T);
1320  omfree(strat->sevT);
1321  omfree(strat->R);
1322  omfree(strat->S_2_R);
1323  omfree(strat->L);
1324  omfree(strat->B);
1325  omfree(strat->fromQ);
1326  idDelete(&strat->Shdl);
1327  test=save_test;
1328  if (TEST_OPT_PROT) PrintLn();
1329  return res;
1330}
1331
1332/* shiftgb stuff */
1333#ifdef HAVE_SHIFTBBA
1334
1335
1336ideal bbaShift(ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat, int uptodeg, int lV)
1337{
1338#ifdef KDEBUG
1339  bba_count++;
1340  int loop_count = 0;
1341#endif
1342  om_Opts.MinTrack = 5;
1343  int   srmax,lrmax, red_result = 1;
1344  int   olddeg,reduc;
1345  int hilbeledeg=1,hilbcount=0,minimcnt=0;
1346  BOOLEAN withT = TRUE; // very important for shifts
1347
1348  initBuchMoraCrit(strat); /*set Gebauer, honey, sugarCrit, NO CHANGES */
1349  initBuchMoraPos(strat); /*NO CHANGES YET: perhaps later*/
1350  initHilbCrit(F,Q,&hilb,strat); /*NO CHANGES*/
1351  initBbaShift(F,strat); /* DONE */
1352  /*set enterS, spSpolyShort, reduce, red, initEcart, initEcartPair*/
1353  /*Shdl=*/initBuchMoraShift(F, Q,strat); /* updateS with no toT, i.e. no init for T */
1354  updateSShift(strat,uptodeg,lV); /* initializes T */
1355
1356  if (strat->minim>0) strat->M=idInit(IDELEMS(F),F->rank);
1357  srmax = strat->sl;
1358  reduc = olddeg = lrmax = 0;
1359
1360#ifndef NO_BUCKETS
1361  if (!TEST_OPT_NOT_BUCKETS)
1362    strat->use_buckets = 1;
1363#endif
1364
1365  // redtailBBa against T for inhomogenous input
1366  //  if (!K_TEST_OPT_OLDSTD)
1367  //    withT = ! strat->homog;
1368
1369  // strat->posInT = posInT_pLength;
1370  kTest_TS(strat);
1371
1372#ifdef HAVE_TAIL_RING
1373  kStratInitChangeTailRing(strat);
1374#endif
1375
1376  /* compute------------------------------------------------------- */
1377  while (strat->Ll >= 0)
1378  {
1379    if (strat->Ll > lrmax) lrmax =strat->Ll;/*stat.*/
1380#ifdef KDEBUG
1381    loop_count++;
1382    if (TEST_OPT_DEBUG) messageSets(strat);
1383#endif
1384    if (strat->Ll== 0) strat->interpt=TRUE;
1385    if (TEST_OPT_DEGBOUND
1386        && ((strat->honey && (strat->L[strat->Ll].ecart+pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
1387            || ((!strat->honey) && (pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))))
1388    {
1389      /*
1390       *stops computation if
1391       * 24 IN test and the degree +ecart of L[strat->Ll] is bigger then
1392       *a predefined number Kstd1_deg
1393       */
1394      while ((strat->Ll >= 0)
1395        && (strat->L[strat->Ll].p1!=NULL) && (strat->L[strat->Ll].p2!=NULL)
1396        && ((strat->honey && (strat->L[strat->Ll].ecart+pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
1397            || ((!strat->honey) && (pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg)))
1398        )
1399        deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
1400      if (strat->Ll<0) break;
1401      else strat->noClearS=TRUE;
1402    }
1403    /* picks the last element from the lazyset L */
1404    strat->P = strat->L[strat->Ll];
1405    strat->Ll--;
1406
1407    if (pNext(strat->P.p) == strat->tail)
1408    {
1409      // deletes the short spoly
1410      pLmFree(strat->P.p);
1411      strat->P.p = NULL;
1412      poly m1 = NULL, m2 = NULL;
1413
1414      // check that spoly creation is ok
1415      while (strat->tailRing != currRing &&
1416             !kCheckSpolyCreation(&(strat->P), strat, m1, m2))
1417      {
1418        assume(m1 == NULL && m2 == NULL);
1419        // if not, change to a ring where exponents are at least
1420        // large enough
1421        kStratChangeTailRing(strat);
1422      }
1423      // create the real one
1424      ksCreateSpoly(&(strat->P), NULL, strat->use_buckets,
1425                    strat->tailRing, m1, m2, strat->R);
1426    }
1427    else if (strat->P.p1 == NULL)
1428    {
1429      if (strat->minim > 0)
1430        strat->P.p2=p_Copy(strat->P.p, currRing, strat->tailRing);
1431      // for input polys, prepare reduction
1432      strat->P.PrepareRed(strat->use_buckets);
1433    }
1434
1435    poly qq;
1436
1437    /* here in the nonhomog case we shrink the new spoly */
1438
1439    if ( ! strat->homog)
1440    {
1441      strat->P.GetP(strat->lmBin); // because shifts are counted with .p structure
1442      /* in the nonhomog case we have to shrink the polynomial */
1443      assume(strat->P.t_p!=NULL);
1444      qq = p_Shrink(strat->P.t_p, lV, strat->tailRing); // direct shrink
1445      if (qq != NULL)
1446      {
1447         /* we're here if Shrink is nonzero */
1448        //         strat->P.p =  NULL;
1449        //        strat->P.Delete(); /* deletes P.p and P.t_p */ //error
1450        strat->P.p   =  NULL; // is not set by Delete
1451        strat->P.t_p =  qq;
1452        strat->P.GetP(strat->lmBin);
1453        // update sev and length
1454        strat->initEcart(&(strat->P));
1455        strat->P.sev = pGetShortExpVector(strat->P.p);
1456//         strat->P.FDeg = strat->P.pFDeg();
1457//         strat->P.length = strat->P.pLDeg();
1458//         strat->P.pLength =strat->P.GetpLength(); //pLength(strat->P.p);
1459      }
1460      else
1461      {
1462         /* Shrink is zero, like y(1)*y(2) - y(1)*y(3)*/
1463#ifdef KDEBUG
1464         if (TEST_OPT_DEBUG){PrintS("nonzero s shrinks to 0");PrintLn();}
1465#endif
1466         //         strat->P.Delete();  // cause error
1467         strat->P.p = NULL;
1468         strat->P.t_p = NULL;
1469           //         strat->P.p = NULL; // or delete strat->P.p ?
1470       }
1471    }
1472      /* end shrinking poly in the nonhomog case */
1473
1474    if (strat->P.p == NULL && strat->P.t_p == NULL)
1475    {
1476      red_result = 0;
1477    }
1478    else
1479    {
1480      if (TEST_OPT_PROT)
1481        message((strat->honey ? strat->P.ecart : 0) + strat->P.pFDeg(),
1482                &olddeg,&reduc,strat, red_result);
1483
1484      /* reduction of the element choosen from L */
1485      red_result = strat->red(&strat->P,strat);
1486    }
1487
1488    // reduction to non-zero new poly
1489    if (red_result == 1)
1490    {
1491      /* statistic */
1492      if (TEST_OPT_PROT) PrintS("s");
1493
1494      // get the polynomial (canonicalize bucket, make sure P.p is set)
1495      strat->P.GetP(strat->lmBin);
1496
1497      int pos=posInS(strat,strat->sl,strat->P.p,strat->P.ecart);
1498
1499      // reduce the tail and normalize poly
1500      if (TEST_OPT_INTSTRATEGY)
1501      {
1502        strat->P.pCleardenom();
1503        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
1504        {
1505          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
1506          strat->P.pCleardenom();
1507        }
1508      }
1509      else
1510      {
1511        strat->P.pNorm();
1512        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
1513          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
1514      }
1515
1516      // here we must shrink again! and optionally reduce again
1517      // or build shrink into redtailBba!
1518
1519#ifdef KDEBUG
1520      if (TEST_OPT_DEBUG){PrintS("new s:");strat->P.wrp();PrintLn();}
1521#endif
1522
1523      // min_std stuff
1524      if ((strat->P.p1==NULL) && (strat->minim>0))
1525      {
1526        if (strat->minim==1)
1527        {
1528          strat->M->m[minimcnt]=p_Copy(strat->P.p,currRing,strat->tailRing);
1529          p_Delete(&strat->P.p2, currRing, strat->tailRing);
1530        }
1531        else
1532        {
1533          strat->M->m[minimcnt]=strat->P.p2;
1534          strat->P.p2=NULL;
1535        }
1536        if (strat->tailRing!=currRing && pNext(strat->M->m[minimcnt])!=NULL)
1537          pNext(strat->M->m[minimcnt])
1538            = strat->p_shallow_copy_delete(pNext(strat->M->m[minimcnt]),
1539                                           strat->tailRing, currRing,
1540                                           currRing->PolyBin);
1541        minimcnt++;
1542      }
1543
1544    /* here in the nonhomog case we shrink the reduced poly AGAIN */
1545
1546    if ( ! strat->homog)
1547    {
1548      strat->P.GetP(strat->lmBin); // because shifts are counted with .p structure
1549      /* assume strat->P.t_p != NULL */
1550      /* in the nonhomog case we have to shrink the polynomial */
1551      assume(strat->P.t_p!=NULL); // poly qq defined above
1552      qq = p_Shrink(strat->P.t_p, lV, strat->tailRing); // direct shrink
1553      if (qq != NULL)
1554      {
1555         /* we're here if Shrink is nonzero */
1556        //         strat->P.p =  NULL;
1557        //        strat->P.Delete(); /* deletes P.p and P.t_p */ //error
1558        strat->P.p   =  NULL; // is not set by Delete
1559        strat->P.t_p =  qq;
1560        strat->P.GetP(strat->lmBin);
1561        // update sev and length
1562        strat->initEcart(&(strat->P));
1563        strat->P.sev = pGetShortExpVector(strat->P.p);
1564      }
1565      else
1566      {
1567         /* Shrink is zero, like y(1)*y(2) - y(1)*y(3)*/
1568#ifdef PDEBUG
1569         if (TEST_OPT_DEBUG){PrintS("nonzero s shrinks to 0");PrintLn();}
1570#endif
1571         //         strat->P.Delete();  // cause error
1572         strat->P.p = NULL;
1573         strat->P.t_p = NULL;
1574           //         strat->P.p = NULL; // or delete strat->P.p ?
1575         goto     red_shrink2zero;
1576       }
1577    }
1578      /* end shrinking poly AGAIN in the nonhomog case */
1579
1580
1581      // enter into S, L, and T
1582      //if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
1583      //        enterT(strat->P, strat); // this was here before Shift stuff
1584      //enterTShift(LObject p, kStrategy strat, int atT, int uptodeg, int lV); // syntax
1585      // the default value for atT = -1 as in bba
1586      /*   strat->P.GetP(); */
1587      // because shifts are counted with .p structure // done before, but ?
1588      enterTShift(strat->P,strat,-1,uptodeg, lV);
1589      enterpairsShift(strat->P.p,strat->sl,strat->P.ecart,pos,strat, strat->tl,uptodeg,lV);
1590      //      enterpairsShift(vw,strat->sl,strat->P.ecart,pos,strat, strat->tl,uptodeg,lV);
1591      // posInS only depends on the leading term
1592      if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
1593      {
1594        strat->enterS(strat->P, pos, strat, strat->tl);
1595      }
1596      else
1597      {
1598      //  strat->P.Delete(); // syzComp test: it is in T
1599      }
1600
1601      if (hilb!=NULL) khCheck(Q,w,hilb,hilbeledeg,hilbcount,strat);
1602//      Print("[%d]",hilbeledeg);
1603      if (strat->P.lcm!=NULL) pLmFree(strat->P.lcm);
1604      if (strat->sl>srmax) srmax = strat->sl;
1605    }
1606    else
1607    {
1608    red_shrink2zero:
1609      if (strat->P.p1 == NULL && strat->minim > 0)
1610      {
1611        p_Delete(&strat->P.p2, currRing, strat->tailRing);
1612      }
1613    }
1614#ifdef KDEBUG
1615    memset(&(strat->P), 0, sizeof(strat->P));
1616#endif
1617    kTest_TS(strat);
1618  }
1619#ifdef KDEBUG
1620  if (TEST_OPT_DEBUG) messageSets(strat);
1621#endif
1622  /* complete reduction of the standard basis--------- */
1623  if (TEST_OPT_SB_1)
1624  {
1625    int k=1;
1626    int j;
1627    while(k<=strat->sl)
1628    {
1629      j=0;
1630      loop
1631      {
1632        if (j>=k) break;
1633        clearS(strat->S[j],strat->sevS[j],&k,&j,strat);
1634        j++;
1635      }
1636      k++;
1637    }
1638  }
1639
1640  if (TEST_OPT_REDSB)
1641  {
1642    completeReduce(strat, TRUE); //shift: withT = TRUE
1643    if (strat->completeReduce_retry)
1644    {
1645      // completeReduce needed larger exponents, retry
1646      // to reduce with S (instead of T)
1647      // and in currRing (instead of strat->tailRing)
1648      cleanT(strat);strat->tailRing=currRing;
1649      int i;
1650      for(i=strat->sl;i>=0;i--) strat->S_2_R[i]=-1;
1651      completeReduce(strat, TRUE);
1652    }
1653  }
1654  else if (TEST_OPT_PROT) PrintLn();
1655
1656  /* release temp data-------------------------------- */
1657  exitBuchMora(strat);
1658  if (TEST_OPT_WEIGHTM)
1659  {
1660    pRestoreDegProcs(pFDegOld, pLDegOld);
1661    if (ecartWeights)
1662    {
1663      omFreeSize((ADDRESS)ecartWeights,(pVariables+1)*sizeof(short));
1664      ecartWeights=NULL;
1665    }
1666  }
1667  if (TEST_OPT_PROT) messageStat(srmax,lrmax,hilbcount,strat);
1668  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
1669  return (strat->Shdl);
1670}
1671
1672
1673ideal freegb(ideal I, int uptodeg, int lVblock)
1674{
1675  /* todo main call */
1676
1677  /* assume: ring is prepared, ideal is copied into shifted ring */
1678  /* uptodeg and lVblock are correct - test them! */
1679
1680  /* check whether the ideal is in V */
1681
1682//  if (0)
1683  if (! ideal_isInV(I,lVblock) )
1684  {
1685    WerrorS("The input ideal contains incorrectly encoded elements! ");
1686    return(NULL);
1687  }
1688
1689  //  kStrategy strat = new skStrategy;
1690  /* ideal bbaShift(ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat, int uptodeg, int lV) */
1691  /* at the moment:
1692- no quotient (check)
1693- no *w, no *hilb
1694  */
1695  /* ideal F, ideal Q, tHomog h,intvec ** w, intvec *hilb,int syzComp,
1696     int newIdeal, intvec *vw) */
1697  ideal RS = kStdShift(I,NULL, testHomog, NULL,NULL,0,0,NULL, uptodeg, lVblock);
1698    //bbaShift(I,NULL, NULL, NULL, strat, uptodeg, lVblock);
1699  idSkipZeroes(RS);
1700  return(RS);
1701}
1702
1703/*2
1704*reduces h with elements from T choosing  the first possible
1705* element in t with respect to the given pDivisibleBy
1706*/
1707int redFirstShift (LObject* h,kStrategy strat)
1708{
1709  if (h->IsNull()) return 0;
1710
1711  int at, reddeg,d;
1712  int pass = 0;
1713  int j = 0;
1714
1715  if (! strat->homog)
1716  {
1717    d = h->GetpFDeg() + h->ecart;
1718    reddeg = strat->LazyDegree+d;
1719  }
1720  h->SetShortExpVector();
1721  loop
1722  {
1723    j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
1724    if (j < 0)
1725    {
1726      h->SetDegStuffReturnLDeg(strat->LDegLast);
1727      return 1;
1728    }
1729
1730    if (!TEST_OPT_INTSTRATEGY)
1731      strat->T[j].pNorm();
1732#ifdef KDEBUG
1733    if (TEST_OPT_DEBUG)
1734    {
1735      PrintS("reduce ");
1736      h->wrp();
1737      PrintS(" with ");
1738      strat->T[j].wrp();
1739    }
1740#endif
1741    ksReducePoly(h, &(strat->T[j]), strat->kNoetherTail(), NULL, strat);
1742
1743#ifdef KDEBUG
1744    if (TEST_OPT_DEBUG)
1745    {
1746      PrintS(" to ");
1747      wrp(h->p);
1748      PrintLn();
1749    }
1750#endif
1751    if (h->IsNull())
1752    {
1753      if (h->lcm!=NULL) pLmFree(h->lcm);
1754      h->Clear();
1755      return 0;
1756    }
1757    h->SetShortExpVector();
1758
1759#if 0
1760    if ((strat->syzComp!=0) && !strat->honey)
1761    {
1762      if ((strat->syzComp>0) &&
1763          (h->Comp() > strat->syzComp))
1764      {
1765        assume(h->MinComp() > strat->syzComp);
1766#ifdef KDEBUG
1767        if (TEST_OPT_DEBUG) PrintS(" > syzComp\n");
1768#endif
1769        if (strat->homog)
1770          h->SetDegStuffReturnLDeg(strat->LDegLast);
1771        return -2;
1772      }
1773    }
1774#endif
1775    if (!strat->homog)
1776    {
1777      if (!K_TEST_OPT_OLDSTD && strat->honey)
1778      {
1779        h->SetpFDeg();
1780        if (strat->T[j].ecart <= h->ecart)
1781          h->ecart = d - h->GetpFDeg();
1782        else
1783          h->ecart = d - h->GetpFDeg() + strat->T[j].ecart - h->ecart;
1784
1785        d = h->GetpFDeg() + h->ecart;
1786      }
1787      else
1788        d = h->SetDegStuffReturnLDeg(strat->LDegLast);
1789      /*- try to reduce the s-polynomial -*/
1790      pass++;
1791      /*
1792       *test whether the polynomial should go to the lazyset L
1793       *-if the degree jumps
1794       *-if the number of pre-defined reductions jumps
1795       */
1796      if (!K_TEST_OPT_REDTHROUGH && (strat->Ll >= 0)
1797          && ((d >= reddeg) || (pass > strat->LazyPass)))
1798      {
1799        h->SetLmCurrRing();
1800        if (strat->posInLDependsOnLength)
1801          h->SetLength(strat->length_pLength);
1802        at = strat->posInL(strat->L,strat->Ll,h,strat);
1803        if (at <= strat->Ll)
1804        {
1805          int dummy=strat->sl;
1806          /*          if (kFindDivisibleByInS(strat,&dummy, h) < 0) */
1807          if (kFindDivisibleByInT(strat->T,strat->sevT, dummy, h) < 0)
1808            return 1;
1809          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
1810#ifdef KDEBUG
1811          if (TEST_OPT_DEBUG) Print(" degree jumped; ->L%d\n",at);
1812#endif
1813          h->Clear();
1814          return -1;
1815        }
1816      }
1817      if ((TEST_OPT_PROT) && (strat->Ll < 0) && (d >= reddeg))
1818      {
1819        reddeg = d+1;
1820        Print(".%d",d);mflush();
1821      }
1822    }
1823  }
1824}
1825
1826void initBbaShift(ideal F,kStrategy strat)
1827{
1828  int i;
1829  idhdl h;
1830 /* setting global variables ------------------- */
1831  strat->enterS = enterSBba; /* remains as is, we change enterT! */
1832
1833  strat->red = redFirstShift; /* no redHomog ! */
1834
1835  if (pLexOrder && strat->honey)
1836    strat->initEcart = initEcartNormal;
1837  else
1838    strat->initEcart = initEcartBBA;
1839  if (strat->honey)
1840    strat->initEcartPair = initEcartPairMora;
1841  else
1842    strat->initEcartPair = initEcartPairBba;
1843  strat->kIdeal = NULL;
1844  //if (strat->ak==0) strat->kIdeal->rtyp=IDEAL_CMD;
1845  //else              strat->kIdeal->rtyp=MODUL_CMD;
1846  //strat->kIdeal->data=(void *)strat->Shdl;
1847  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1848  {
1849    //interred  machen   Aenderung
1850    pFDegOld=pFDeg;
1851    pLDegOld=pLDeg;
1852    //h=ggetid("ecart");
1853    //if ((h!=NULL) /*&& (IDTYP(h)==INTVEC_CMD)*/)
1854    //{
1855    //  ecartWeights=iv2array(IDINTVEC(h));
1856    //}
1857    //else
1858    {
1859      ecartWeights=(short *)omAlloc((pVariables+1)*sizeof(short));
1860      /*uses automatic computation of the ecartWeights to set them*/
1861      kEcartWeights(F->m,IDELEMS(F)-1,ecartWeights);
1862    }
1863    pRestoreDegProcs(totaldegreeWecart, maxdegreeWecart);
1864    if (TEST_OPT_PROT)
1865    {
1866      for(i=1; i<=pVariables; i++)
1867        Print(" %d",ecartWeights[i]);
1868      PrintLn();
1869      mflush();
1870    }
1871  }
1872}
1873#endif
Note: See TracBrowser for help on using the repository browser.