source: git/kernel/kstd2.cc @ 7245240

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