source: git/kernel/kstd2.cc @ 24ed65

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