source: git/kernel/kstd2.cc @ 60eb1d

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