source: git/kernel/kstd2.cc @ 0a8ee5

spielwiese
Last change on this file since 0a8ee5 was 690e21e, checked in by Hans Schönemann <hannes@…>, 14 years ago
moved option marcos to options.h git-svn-id: file:///usr/local/Singular/svn/trunk@12466 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 50.7 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 "options.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      strat->enterS(strat->P, pos, strat, strat->tl);
1196#if 0
1197      int pl=pLength(strat->P.p);
1198      if (pl==1)
1199      {
1200        //if (TEST_OPT_PROT)
1201        //PrintS("<1>");
1202      }
1203      else if (pl==2)
1204      {
1205        //if (TEST_OPT_PROT)
1206        //PrintS("<2>");
1207      }
1208#endif
1209      if (hilb!=NULL) khCheck(Q,w,hilb,hilbeledeg,hilbcount,strat);
1210//      Print("[%d]",hilbeledeg);
1211      if (strat->P.lcm!=NULL)
1212#ifdef HAVE_RINGS
1213        pLmDelete(strat->P.lcm);
1214#else
1215        pLmFree(strat->P.lcm);
1216#endif
1217      if (strat->sl>srmax) srmax = strat->sl;
1218    }
1219    else if (strat->P.p1 == NULL && strat->minim > 0)
1220    {
1221      p_Delete(&strat->P.p2, currRing, strat->tailRing);
1222    }
1223
1224#ifdef KDEBUG
1225    memset(&(strat->P), 0, sizeof(strat->P));
1226#endif /* KDEBUG */
1227    kTest_TS(strat);
1228  }
1229#ifdef KDEBUG
1230#if MYTEST
1231  PrintS("bba finish GB: currRing: "); rWrite(currRing);
1232#endif /* MYTEST */
1233  if (TEST_OPT_DEBUG) messageSets(strat);
1234#endif /* KDEBUG */
1235
1236  if (TEST_OPT_SB_1)
1237  {
1238    int k=1;
1239    int j;
1240    while(k<=strat->sl)
1241    {
1242      j=0;
1243      loop
1244      {
1245        if (j>=k) break;
1246        clearS(strat->S[j],strat->sevS[j],&k,&j,strat);
1247        j++;
1248      }
1249      k++;
1250    }
1251  }
1252
1253  /* complete reduction of the standard basis--------- */
1254  if (TEST_OPT_REDSB)
1255  {
1256    completeReduce(strat);
1257#ifdef HAVE_TAIL_RING
1258    if (strat->completeReduce_retry)
1259    {
1260      // completeReduce needed larger exponents, retry
1261      // to reduce with S (instead of T)
1262      // and in currRing (instead of strat->tailRing)
1263      cleanT(strat);strat->tailRing=currRing;
1264      int i;
1265      for(i=strat->sl;i>=0;i--) strat->S_2_R[i]=-1;
1266      completeReduce(strat);
1267    }
1268#endif
1269  }
1270  else if (TEST_OPT_PROT) PrintLn();
1271
1272  /* release temp data-------------------------------- */
1273  exitBuchMora(strat);
1274  if (TEST_OPT_WEIGHTM)
1275  {
1276    pRestoreDegProcs(pFDegOld, pLDegOld);
1277    if (ecartWeights)
1278    {
1279      omFreeSize((ADDRESS)ecartWeights,(pVariables+1)*sizeof(short));
1280      ecartWeights=NULL;
1281    }
1282  }
1283  if (TEST_OPT_PROT) messageStat(srmax,lrmax,hilbcount,strat);
1284  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
1285
1286#ifdef KDEBUG
1287#if MYTEST
1288  PrintS("bba_end: currRing: "); rWrite(currRing);
1289#endif /* MYTEST */
1290#endif /* KDEBUG */
1291  idTest(strat->Shdl);
1292
1293  return (strat->Shdl);
1294}
1295
1296poly kNF2 (ideal F,ideal Q,poly q,kStrategy strat, int lazyReduce)
1297{
1298  assume(q!=NULL);
1299  assume(!(idIs0(F)&&(Q==NULL))); // NF(q, std(0) in polynomial ring?
1300
1301// lazy_reduce flags: can be combined by |
1302//#define KSTD_NF_LAZY   1
1303  // do only a reduction of the leading term
1304//#define KSTD_NF_NONORM 4
1305  // only global: avoid normalization, return a multiply of NF
1306  poly   p;
1307  int   i;
1308
1309  //if ((idIs0(F))&&(Q==NULL))
1310  //  return pCopy(q); /*F=0*/
1311  //strat->ak = idRankFreeModule(F);
1312  /*- creating temp data structures------------------- -*/
1313  BITSET save_test=test;
1314  test|=Sy_bit(OPT_REDTAIL);
1315  initBuchMoraCrit(strat);
1316  strat->initEcart = initEcartBBA;
1317  strat->enterS = enterSBba;
1318#ifndef NO_BUCKETS
1319  strat->use_buckets = (!TEST_OPT_NOT_BUCKETS) && (!rIsPluralRing(currRing));
1320#endif
1321  /*- set S -*/
1322  strat->sl = -1;
1323  /*- init local data struct.---------------------------------------- -*/
1324  /*Shdl=*/initS(F,Q,strat);
1325  /*- compute------------------------------------------------------- -*/
1326  //if ((TEST_OPT_INTSTRATEGY)&&(lazyReduce==0))
1327  //{
1328  //  for (i=strat->sl;i>=0;i--)
1329  //    pNorm(strat->S[i]);
1330  //}
1331  kTest(strat);
1332  if (TEST_OPT_PROT) { PrintS("r"); mflush(); }
1333  int max_ind;
1334  p = redNF(pCopy(q),max_ind,lazyReduce & KSTD_NF_NONORM,strat);
1335  if ((p!=NULL)&&((lazyReduce & KSTD_NF_LAZY)==0))
1336  {
1337    if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
1338    #ifdef HAVE_RINGS
1339    if (rField_is_Ring())
1340    {
1341      p = redtailBba_Z(p,max_ind,strat);
1342    }
1343    else
1344    #endif
1345    {
1346      BITSET save=test;
1347      test &= ~Sy_bit(OPT_INTSTRATEGY);
1348      p = redtailBba(p,max_ind,strat,(lazyReduce & KSTD_NF_NONORM)==0);
1349      test=save;
1350    }
1351  }
1352  /*- release temp data------------------------------- -*/
1353  omfree(strat->sevS);
1354  omfree(strat->ecartS);
1355  omfree(strat->T);
1356  omfree(strat->sevT);
1357  omfree(strat->R);
1358  omfree(strat->S_2_R);
1359  omfree(strat->L);
1360  omfree(strat->B);
1361  omfree(strat->fromQ);
1362  idDelete(&strat->Shdl);
1363  test=save_test;
1364  if (TEST_OPT_PROT) PrintLn();
1365  return p;
1366}
1367
1368ideal kNF2 (ideal F,ideal Q,ideal q,kStrategy strat, int lazyReduce)
1369{
1370  assume(!idIs0(q));
1371  assume(!(idIs0(F)&&(Q==NULL)));
1372// lazy_reduce flags: can be combined by |
1373//#define KSTD_NF_LAZY   1
1374  // do only a reduction of the leading term
1375//#define KSTD_NF_NONORM 4
1376  // only global: avoid normalization, return a multiply of NF
1377  poly   p;
1378  int   i;
1379  ideal res;
1380  int max_ind;
1381
1382  //if (idIs0(q))
1383  //  return idInit(IDELEMS(q),si_max(q->rank,F->rank));
1384  //if ((idIs0(F))&&(Q==NULL))
1385  //  return idCopy(q); /*F=0*/
1386  //strat->ak = idRankFreeModule(F);
1387  /*- creating temp data structures------------------- -*/
1388  BITSET save_test=test;
1389  test|=Sy_bit(OPT_REDTAIL);
1390  initBuchMoraCrit(strat);
1391  strat->initEcart = initEcartBBA;
1392  strat->enterS = enterSBba;
1393  /*- set S -*/
1394  strat->sl = -1;
1395#ifndef NO_BUCKETS
1396  strat->use_buckets = (!TEST_OPT_NOT_BUCKETS) && (!rIsPluralRing(currRing));
1397#endif
1398  /*- init local data struct.---------------------------------------- -*/
1399  /*Shdl=*/initS(F,Q,strat);
1400  /*- compute------------------------------------------------------- -*/
1401  res=idInit(IDELEMS(q),si_max(q->rank,F->rank));
1402  BITSET save=test;
1403  test &= ~Sy_bit(OPT_INTSTRATEGY);
1404  for (i=IDELEMS(q)-1; i>=0; i--)
1405  {
1406    if (q->m[i]!=NULL)
1407    {
1408      if (TEST_OPT_PROT) { PrintS("r");mflush(); }
1409      p = redNF(pCopy(q->m[i]),max_ind,lazyReduce & KSTD_NF_NONORM,strat);
1410      if ((p!=NULL)&&((lazyReduce & KSTD_NF_LAZY)==0))
1411      {
1412        if (TEST_OPT_PROT) { PrintS("t"); mflush(); }
1413        #ifdef HAVE_RINGS
1414        if (rField_is_Ring())
1415        {
1416          p = redtailBba_Z(p,max_ind,strat);
1417        }
1418        else
1419        #endif
1420        {
1421          p = redtailBba(p,max_ind,strat,(lazyReduce & KSTD_NF_NONORM)==0);
1422        }
1423      }
1424      res->m[i]=p;
1425    }
1426    //else
1427    //  res->m[i]=NULL;
1428  }
1429  /*- release temp data------------------------------- -*/
1430  test=save;
1431  omfree(strat->sevS);
1432  omfree(strat->ecartS);
1433  omfree(strat->T);
1434  omfree(strat->sevT);
1435  omfree(strat->R);
1436  omfree(strat->S_2_R);
1437  omfree(strat->L);
1438  omfree(strat->B);
1439  omfree(strat->fromQ);
1440  idDelete(&strat->Shdl);
1441  test=save_test;
1442  if (TEST_OPT_PROT) PrintLn();
1443  return res;
1444}
1445
1446/* shiftgb stuff */
1447#ifdef HAVE_SHIFTBBA
1448
1449
1450ideal bbaShift(ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat, int uptodeg, int lV)
1451{
1452#ifdef KDEBUG
1453  bba_count++;
1454  int loop_count = 0;
1455#endif
1456  om_Opts.MinTrack = 5;
1457  int   srmax,lrmax, red_result = 1;
1458  int   olddeg,reduc;
1459  int hilbeledeg=1,hilbcount=0,minimcnt=0;
1460  BOOLEAN withT = TRUE; // very important for shifts
1461
1462  initBuchMoraCrit(strat); /*set Gebauer, honey, sugarCrit, NO CHANGES */
1463  initBuchMoraPos(strat); /*NO CHANGES YET: perhaps later*/
1464  initHilbCrit(F,Q,&hilb,strat); /*NO CHANGES*/
1465  initBbaShift(F,strat); /* DONE */
1466  /*set enterS, spSpolyShort, reduce, red, initEcart, initEcartPair*/
1467  /*Shdl=*/initBuchMoraShift(F, Q,strat); /* updateS with no toT, i.e. no init for T */
1468  updateSShift(strat,uptodeg,lV); /* initializes T */
1469
1470  if (strat->minim>0) strat->M=idInit(IDELEMS(F),F->rank);
1471  srmax = strat->sl;
1472  reduc = olddeg = lrmax = 0;
1473  strat->lV=lV;
1474
1475#ifndef NO_BUCKETS
1476  if (!TEST_OPT_NOT_BUCKETS)
1477    strat->use_buckets = 1;
1478#endif
1479
1480  // redtailBBa against T for inhomogenous input
1481  //  if (!K_TEST_OPT_OLDSTD)
1482  //    withT = ! strat->homog;
1483
1484  // strat->posInT = posInT_pLength;
1485  kTest_TS(strat);
1486
1487#ifdef HAVE_TAIL_RING
1488  kStratInitChangeTailRing(strat);
1489#endif
1490
1491  /* compute------------------------------------------------------- */
1492  while (strat->Ll >= 0)
1493  {
1494    if (strat->Ll > lrmax) lrmax =strat->Ll;/*stat.*/
1495#ifdef KDEBUG
1496    loop_count++;
1497    if (TEST_OPT_DEBUG) messageSets(strat);
1498#endif
1499    if (strat->Ll== 0) strat->interpt=TRUE;
1500    if (TEST_OPT_DEGBOUND
1501        && ((strat->honey && (strat->L[strat->Ll].ecart+pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
1502            || ((!strat->honey) && (pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))))
1503    {
1504      /*
1505       *stops computation if
1506       * 24 IN test and the degree +ecart of L[strat->Ll] is bigger then
1507       *a predefined number Kstd1_deg
1508       */
1509      while ((strat->Ll >= 0)
1510        && (strat->L[strat->Ll].p1!=NULL) && (strat->L[strat->Ll].p2!=NULL)
1511        && ((strat->honey && (strat->L[strat->Ll].ecart+pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg))
1512            || ((!strat->honey) && (pFDeg(strat->L[strat->Ll].p,currRing)>Kstd1_deg)))
1513        )
1514        deleteInL(strat->L,&strat->Ll,strat->Ll,strat);
1515      if (strat->Ll<0) break;
1516      else strat->noClearS=TRUE;
1517    }
1518    /* picks the last element from the lazyset L */
1519    strat->P = strat->L[strat->Ll];
1520    strat->Ll--;
1521
1522    if (pNext(strat->P.p) == strat->tail)
1523    {
1524      // deletes the short spoly
1525      pLmFree(strat->P.p);
1526      strat->P.p = NULL;
1527      poly m1 = NULL, m2 = NULL;
1528
1529      // check that spoly creation is ok
1530      while (strat->tailRing != currRing &&
1531             !kCheckSpolyCreation(&(strat->P), strat, m1, m2))
1532      {
1533        assume(m1 == NULL && m2 == NULL);
1534        // if not, change to a ring where exponents are at least
1535        // large enough
1536        kStratChangeTailRing(strat);
1537      }
1538      // create the real one
1539      ksCreateSpoly(&(strat->P), NULL, strat->use_buckets,
1540                    strat->tailRing, m1, m2, strat->R);
1541    }
1542    else if (strat->P.p1 == NULL)
1543    {
1544      if (strat->minim > 0)
1545        strat->P.p2=p_Copy(strat->P.p, currRing, strat->tailRing);
1546      // for input polys, prepare reduction
1547      strat->P.PrepareRed(strat->use_buckets);
1548    }
1549
1550    poly qq;
1551
1552    /* here in the nonhomog case we shrink the new spoly */
1553
1554    if ( ! strat->homog)
1555    {
1556      strat->P.GetP(strat->lmBin); // because shifts are counted with .p structure
1557      /* in the nonhomog case we have to shrink the polynomial */
1558      assume(strat->P.t_p!=NULL);
1559      qq = p_Shrink(strat->P.t_p, lV, strat->tailRing); // direct shrink
1560      if (qq != NULL)
1561      {
1562         /* we're here if Shrink is nonzero */
1563        //         strat->P.p =  NULL;
1564        //        strat->P.Delete(); /* deletes P.p and P.t_p */ //error
1565        strat->P.p   =  NULL; // is not set by Delete
1566        strat->P.t_p =  qq;
1567        strat->P.GetP(strat->lmBin);
1568        // update sev and length
1569        strat->initEcart(&(strat->P));
1570        strat->P.sev = pGetShortExpVector(strat->P.p);
1571//         strat->P.FDeg = strat->P.pFDeg();
1572//         strat->P.length = strat->P.pLDeg();
1573//         strat->P.pLength =strat->P.GetpLength(); //pLength(strat->P.p);
1574      }
1575      else
1576      {
1577         /* Shrink is zero, like y(1)*y(2) - y(1)*y(3)*/
1578#ifdef KDEBUG
1579         if (TEST_OPT_DEBUG){PrintS("nonzero s shrinks to 0");PrintLn();}
1580#endif
1581         //         strat->P.Delete();  // cause error
1582         strat->P.p = NULL;
1583         strat->P.t_p = NULL;
1584           //         strat->P.p = NULL; // or delete strat->P.p ?
1585       }
1586    }
1587      /* end shrinking poly in the nonhomog case */
1588
1589    if (strat->P.p == NULL && strat->P.t_p == NULL)
1590    {
1591      red_result = 0;
1592    }
1593    else
1594    {
1595      if (TEST_OPT_PROT)
1596        message((strat->honey ? strat->P.ecart : 0) + strat->P.pFDeg(),
1597                &olddeg,&reduc,strat, red_result);
1598
1599      /* reduction of the element choosen from L */
1600      red_result = strat->red(&strat->P,strat);
1601    }
1602
1603    // reduction to non-zero new poly
1604    if (red_result == 1)
1605    {
1606      /* statistic */
1607      if (TEST_OPT_PROT) PrintS("s");
1608
1609      // get the polynomial (canonicalize bucket, make sure P.p is set)
1610      strat->P.GetP(strat->lmBin);
1611
1612      int pos=posInS(strat,strat->sl,strat->P.p,strat->P.ecart);
1613
1614      // reduce the tail and normalize poly
1615      if (TEST_OPT_INTSTRATEGY)
1616      {
1617        strat->P.pCleardenom();
1618        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
1619        {
1620          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
1621          strat->P.pCleardenom();
1622        }
1623      }
1624      else
1625      {
1626        strat->P.pNorm();
1627        if ((TEST_OPT_REDSB)||(TEST_OPT_REDTAIL))
1628          strat->P.p = redtailBba(&(strat->P),pos-1,strat, withT);
1629      }
1630
1631      // here we must shrink again! and optionally reduce again
1632      // or build shrink into redtailBba!
1633
1634#ifdef KDEBUG
1635      if (TEST_OPT_DEBUG){PrintS("new s:");strat->P.wrp();PrintLn();}
1636#endif
1637
1638      // min_std stuff
1639      if ((strat->P.p1==NULL) && (strat->minim>0))
1640      {
1641        if (strat->minim==1)
1642        {
1643          strat->M->m[minimcnt]=p_Copy(strat->P.p,currRing,strat->tailRing);
1644          p_Delete(&strat->P.p2, currRing, strat->tailRing);
1645        }
1646        else
1647        {
1648          strat->M->m[minimcnt]=strat->P.p2;
1649          strat->P.p2=NULL;
1650        }
1651        if (strat->tailRing!=currRing && pNext(strat->M->m[minimcnt])!=NULL)
1652          pNext(strat->M->m[minimcnt])
1653            = strat->p_shallow_copy_delete(pNext(strat->M->m[minimcnt]),
1654                                           strat->tailRing, currRing,
1655                                           currRing->PolyBin);
1656        minimcnt++;
1657      }
1658
1659    /* here in the nonhomog case we shrink the reduced poly AGAIN */
1660
1661    if ( ! strat->homog)
1662    {
1663      strat->P.GetP(strat->lmBin); // because shifts are counted with .p structure
1664      /* assume strat->P.t_p != NULL */
1665      /* in the nonhomog case we have to shrink the polynomial */
1666      assume(strat->P.t_p!=NULL); // poly qq defined above
1667      qq = p_Shrink(strat->P.t_p, lV, strat->tailRing); // direct shrink
1668      if (qq != NULL)
1669      {
1670         /* we're here if Shrink is nonzero */
1671        //         strat->P.p =  NULL;
1672        //        strat->P.Delete(); /* deletes P.p and P.t_p */ //error
1673        strat->P.p   =  NULL; // is not set by Delete
1674        strat->P.t_p =  qq;
1675        strat->P.GetP(strat->lmBin);
1676        // update sev and length
1677        strat->initEcart(&(strat->P));
1678        strat->P.sev = pGetShortExpVector(strat->P.p);
1679      }
1680      else
1681      {
1682         /* Shrink is zero, like y(1)*y(2) - y(1)*y(3)*/
1683#ifdef PDEBUG
1684         if (TEST_OPT_DEBUG){PrintS("nonzero s shrinks to 0");PrintLn();}
1685#endif
1686         //         strat->P.Delete();  // cause error
1687         strat->P.p = NULL;
1688         strat->P.t_p = NULL;
1689           //         strat->P.p = NULL; // or delete strat->P.p ?
1690         goto     red_shrink2zero;
1691       }
1692    }
1693      /* end shrinking poly AGAIN in the nonhomog case */
1694
1695
1696      // enter into S, L, and T
1697      //if ((!TEST_OPT_IDLIFT) || (pGetComp(strat->P.p) <= strat->syzComp))
1698      //        enterT(strat->P, strat); // this was here before Shift stuff
1699      //enterTShift(LObject p, kStrategy strat, int atT, int uptodeg, int lV); // syntax
1700      // the default value for atT = -1 as in bba
1701      /*   strat->P.GetP(); */
1702      // because shifts are counted with .p structure // done before, but ?
1703      enterTShift(strat->P,strat,-1,uptodeg, lV);
1704      enterpairsShift(strat->P.p,strat->sl,strat->P.ecart,pos,strat, strat->tl,uptodeg,lV);
1705      //      enterpairsShift(vw,strat->sl,strat->P.ecart,pos,strat, strat->tl,uptodeg,lV);
1706      // posInS only depends on the leading term
1707      strat->enterS(strat->P, pos, strat, strat->tl);
1708
1709      if (hilb!=NULL) khCheck(Q,w,hilb,hilbeledeg,hilbcount,strat);
1710//      Print("[%d]",hilbeledeg);
1711      if (strat->P.lcm!=NULL) pLmFree(strat->P.lcm);
1712      if (strat->sl>srmax) srmax = strat->sl;
1713    }
1714    else
1715    {
1716    red_shrink2zero:
1717      if (strat->P.p1 == NULL && strat->minim > 0)
1718      {
1719        p_Delete(&strat->P.p2, currRing, strat->tailRing);
1720      }
1721    }
1722#ifdef KDEBUG
1723    memset(&(strat->P), 0, sizeof(strat->P));
1724#endif
1725    kTest_TS(strat);
1726  }
1727#ifdef KDEBUG
1728  if (TEST_OPT_DEBUG) messageSets(strat);
1729#endif
1730  /* complete reduction of the standard basis--------- */
1731  /*  shift case: look for elt's in S such that they are divisible by elt in T */
1732  //  if (TEST_OPT_SB_1)
1733  if (TEST_OPT_REDSB)
1734  {
1735    int k=0;
1736    int j=-1;
1737    while(k<=strat->sl)
1738    {
1739//       loop
1740//       {
1741//         if (j>=k) break;
1742//         clearS(strat->S[j],strat->sevS[j],&k,&j,strat);
1743//         j++;
1744//       }
1745      LObject Ln (strat->S[k],currRing, strat->tailRing);
1746      Ln.SetShortExpVector();
1747      j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, &Ln, j+1);
1748      if (j<0) {  k++; j=-1;}
1749      else
1750      {
1751        if ( pLmCmp(strat->S[k],strat->T[j].p) == 0)
1752        {
1753          j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, &Ln, j+1);
1754          if (j<0) {  k++; j=-1;}
1755          else
1756          {
1757            deleteInS(k,strat);
1758          }
1759        }
1760        else
1761        {
1762          deleteInS(k,strat);
1763        }
1764      }
1765    }
1766  }
1767
1768  if (TEST_OPT_REDSB)
1769  {    completeReduce(strat, TRUE); //shift: withT = TRUE
1770    if (strat->completeReduce_retry)
1771    {
1772      // completeReduce needed larger exponents, retry
1773      // to reduce with S (instead of T)
1774      // and in currRing (instead of strat->tailRing)
1775      cleanT(strat);strat->tailRing=currRing;
1776      int i;
1777      for(i=strat->sl;i>=0;i--) strat->S_2_R[i]=-1;
1778      completeReduce(strat, TRUE);
1779    }
1780  }
1781  else if (TEST_OPT_PROT) PrintLn();
1782
1783  /* release temp data-------------------------------- */
1784  exitBuchMora(strat);
1785  if (TEST_OPT_WEIGHTM)
1786  {
1787    pRestoreDegProcs(pFDegOld, pLDegOld);
1788    if (ecartWeights)
1789    {
1790      omFreeSize((ADDRESS)ecartWeights,(pVariables+1)*sizeof(short));
1791      ecartWeights=NULL;
1792    }
1793  }
1794  if (TEST_OPT_PROT) messageStat(srmax,lrmax,hilbcount,strat);
1795  if (Q!=NULL) updateResult(strat->Shdl,Q,strat);
1796  return (strat->Shdl);
1797}
1798
1799
1800ideal freegb(ideal I, int uptodeg, int lVblock)
1801{
1802  /* todo main call */
1803
1804  /* assume: ring is prepared, ideal is copied into shifted ring */
1805  /* uptodeg and lVblock are correct - test them! */
1806
1807  /* check whether the ideal is in V */
1808
1809//  if (0)
1810  if (! ideal_isInV(I,lVblock) )
1811  {
1812    WerrorS("The input ideal contains incorrectly encoded elements! ");
1813    return(NULL);
1814  }
1815
1816  //  kStrategy strat = new skStrategy;
1817  /* ideal bbaShift(ideal F, ideal Q,intvec *w,intvec *hilb,kStrategy strat, int uptodeg, int lV) */
1818  /* at the moment:
1819- no quotient (check)
1820- no *w, no *hilb
1821  */
1822  /* ideal F, ideal Q, tHomog h,intvec ** w, intvec *hilb,int syzComp,
1823     int newIdeal, intvec *vw) */
1824  ideal RS = kStdShift(I,NULL, testHomog, NULL,NULL,0,0,NULL, uptodeg, lVblock);
1825    //bbaShift(I,NULL, NULL, NULL, strat, uptodeg, lVblock);
1826  idSkipZeroes(RS);
1827  return(RS);
1828}
1829
1830/*2
1831*reduces h with elements from T choosing  the first possible
1832* element in t with respect to the given pDivisibleBy
1833*/
1834int redFirstShift (LObject* h,kStrategy strat)
1835{
1836  if (h->IsNull()) return 0;
1837
1838  int at, reddeg,d;
1839  int pass = 0;
1840  int j = 0;
1841
1842  if (! strat->homog)
1843  {
1844    d = h->GetpFDeg() + h->ecart;
1845    reddeg = strat->LazyDegree+d;
1846  }
1847  h->SetShortExpVector();
1848  loop
1849  {
1850    j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, h);
1851    if (j < 0)
1852    {
1853      h->SetDegStuffReturnLDeg(strat->LDegLast);
1854      return 1;
1855    }
1856
1857    if (!TEST_OPT_INTSTRATEGY)
1858      strat->T[j].pNorm();
1859#ifdef KDEBUG
1860    if (TEST_OPT_DEBUG)
1861    {
1862      PrintS("reduce ");
1863      h->wrp();
1864      PrintS(" with ");
1865      strat->T[j].wrp();
1866    }
1867#endif
1868    ksReducePoly(h, &(strat->T[j]), strat->kNoetherTail(), NULL, strat);
1869    if (!h->IsNull())
1870    {
1871      poly qq=p_Shrink(h->GetTP(),strat->lV,strat->tailRing);
1872      h->p=NULL;
1873      h->t_p=qq;
1874      if (qq!=NULL) h->GetP(strat->lmBin);
1875    }
1876
1877#ifdef KDEBUG
1878    if (TEST_OPT_DEBUG)
1879    {
1880      PrintS(" to ");
1881      wrp(h->p);
1882      PrintLn();
1883    }
1884#endif
1885    if (h->IsNull())
1886    {
1887      if (h->lcm!=NULL) pLmFree(h->lcm);
1888      h->Clear();
1889      return 0;
1890    }
1891    h->SetShortExpVector();
1892
1893#if 0
1894    if ((strat->syzComp!=0) && !strat->honey)
1895    {
1896      if ((strat->syzComp>0) &&
1897          (h->Comp() > strat->syzComp))
1898      {
1899        assume(h->MinComp() > strat->syzComp);
1900#ifdef KDEBUG
1901        if (TEST_OPT_DEBUG) PrintS(" > syzComp\n");
1902#endif
1903        if (strat->homog)
1904          h->SetDegStuffReturnLDeg(strat->LDegLast);
1905        return -2;
1906      }
1907    }
1908#endif
1909    if (!strat->homog)
1910    {
1911      if (!K_TEST_OPT_OLDSTD && strat->honey)
1912      {
1913        h->SetpFDeg();
1914        if (strat->T[j].ecart <= h->ecart)
1915          h->ecart = d - h->GetpFDeg();
1916        else
1917          h->ecart = d - h->GetpFDeg() + strat->T[j].ecart - h->ecart;
1918
1919        d = h->GetpFDeg() + h->ecart;
1920      }
1921      else
1922        d = h->SetDegStuffReturnLDeg(strat->LDegLast);
1923      /*- try to reduce the s-polynomial -*/
1924      pass++;
1925      /*
1926       *test whether the polynomial should go to the lazyset L
1927       *-if the degree jumps
1928       *-if the number of pre-defined reductions jumps
1929       */
1930      if (!K_TEST_OPT_REDTHROUGH && (strat->Ll >= 0)
1931          && ((d >= reddeg) || (pass > strat->LazyPass)))
1932      {
1933        h->SetLmCurrRing();
1934        if (strat->posInLDependsOnLength)
1935          h->SetLength(strat->length_pLength);
1936        at = strat->posInL(strat->L,strat->Ll,h,strat);
1937        if (at <= strat->Ll)
1938        {
1939          int dummy=strat->sl;
1940          /*          if (kFindDivisibleByInS(strat,&dummy, h) < 0) */
1941          if (kFindDivisibleByInT(strat->T,strat->sevT, dummy, h) < 0)
1942            return 1;
1943          enterL(&strat->L,&strat->Ll,&strat->Lmax,*h,at);
1944#ifdef KDEBUG
1945          if (TEST_OPT_DEBUG) Print(" degree jumped; ->L%d\n",at);
1946#endif
1947          h->Clear();
1948          return -1;
1949        }
1950      }
1951      if ((TEST_OPT_PROT) && (strat->Ll < 0) && (d >= reddeg))
1952      {
1953        reddeg = d+1;
1954        Print(".%d",d);mflush();
1955      }
1956    }
1957  }
1958}
1959
1960void initBbaShift(ideal F,kStrategy strat)
1961{
1962  int i;
1963  idhdl h;
1964 /* setting global variables ------------------- */
1965  strat->enterS = enterSBba; /* remains as is, we change enterT! */
1966
1967  strat->red = redFirstShift; /* no redHomog ! */
1968
1969  if (pLexOrder && strat->honey)
1970    strat->initEcart = initEcartNormal;
1971  else
1972    strat->initEcart = initEcartBBA;
1973  if (strat->honey)
1974    strat->initEcartPair = initEcartPairMora;
1975  else
1976    strat->initEcartPair = initEcartPairBba;
1977  strat->kIdeal = NULL;
1978  //if (strat->ak==0) strat->kIdeal->rtyp=IDEAL_CMD;
1979  //else              strat->kIdeal->rtyp=MODUL_CMD;
1980  //strat->kIdeal->data=(void *)strat->Shdl;
1981  if ((TEST_OPT_WEIGHTM)&&(F!=NULL))
1982  {
1983    //interred  machen   Aenderung
1984    pFDegOld=pFDeg;
1985    pLDegOld=pLDeg;
1986    //h=ggetid("ecart");
1987    //if ((h!=NULL) /*&& (IDTYP(h)==INTVEC_CMD)*/)
1988    //{
1989    //  ecartWeights=iv2array(IDINTVEC(h));
1990    //}
1991    //else
1992    {
1993      ecartWeights=(short *)omAlloc((pVariables+1)*sizeof(short));
1994      /*uses automatic computation of the ecartWeights to set them*/
1995      kEcartWeights(F->m,IDELEMS(F)-1,ecartWeights);
1996    }
1997    pRestoreDegProcs(totaldegreeWecart, maxdegreeWecart);
1998    if (TEST_OPT_PROT)
1999    {
2000      for(i=1; i<=pVariables; i++)
2001        Print(" %d",ecartWeights[i]);
2002      PrintLn();
2003      mflush();
2004    }
2005  }
2006}
2007#endif
Note: See TracBrowser for help on using the repository browser.