source: git/kernel/kstd2.cc @ 762407

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