source: git/kernel/kstd2.cc @ 80ca3c

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