source: git/Singular/kutil.cc @ 2800f6

spielwiese
Last change on this file since 2800f6 was 2800f6, checked in by Hans Schönemann <hannes@…>, 27 years ago
* hannes/siebert: added new posInL17_c, posInT17_c for (c,..)/(C,..) (kutil.cc) git-svn-id: file:///usr/local/Singular/svn/trunk@413 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 82.0 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: kutil.cc,v 1.8 1997-06-20 09:56:55 Singular Exp $ */
5/*
6* ABSTRACT: kernel: utils for std
7*/
8
9#include <stdlib.h>
10#include <string.h>
11#include "mod2.h"
12#include "tok.h"
13#include "febase.h"
14#include "spolys.h"
15#include "mmemory.h"
16#include "numbers.h"
17#include "polys.h"
18#include "ipid.h"
19#include "ring.h"
20#include "ideals.h"
21#include "timer.h"
22#include "cntrlc.h"
23#include "stairc.h"
24#include "subexpr.h"
25#include "kstd1.h"
26#include "kutil.h"
27//#include "longrat.h"
28
29static poly redMora (poly h,int maxIndex,kStrategy strat);
30static poly redBba (poly h,int maxIndex,kStrategy strat);
31
32//int     cp, c3;
33//BOOLEAN interpt;
34//BOOLEAN news=FALSE;
35//static BOOLEAN newt=FALSE;/*used for messageSets*/
36BITSET  test=(BITSET)0;
37//int     ak;
38//int     syzComp=0;
39int     HCord;
40//LObject P;
41//poly    tail;
42//BOOLEAN *pairtest;/*used for enterOnePair*/
43//int    // LazyPass=100,
44//        LazyDegree=1;
45int        Kstd1_deg;
46int        mu=32000;
47
48//polyset S;
49//int     sl;
50//ideal   Shdl;
51//intset  ecartS;
52//intset  fromQ;
53
54//TSet    T;
55//int     tl;
56//int     tmax;
57
58//LSet    L;
59//int     Ll;
60//int     Lmax;
61
62//LSet    B;
63//int     Bl;
64//int     Bmax;
65
66//BOOLEAN kActive=FALSE;
67//BOOLEAN fromT = FALSE;
68//BOOLEAN honey, sugarCrit;/*option 5*/
69//BOOLEAN Gebauer;
70//BOOLEAN * NotUsedAxis;
71//BOOLEAN homog;
72//BOOLEAN noTailReduction = FALSE;
73
74/*0 implementation*/
75/*
76*static void deleteHCn(poly* p)
77*{
78*  poly p1;
79*  if (pHEdgeFound && (*p))
80*  {
81*    if (pComp0(*p,pNoether) == -1)
82*    {
83*      pDelete(p);
84*      return;
85*    }
86*    p1 = *p;
87*    while (pNext(p1))
88*    {
89*      if (pComp0(pNext(p1), pNoether) == -1) pDelete(&pNext(p1));
90*      else                                  pIter(p1);
91*    }
92*  }
93*}
94*/
95
96/*2
97*deletes higher monomial of p, re-compute ecart and length
98*works only for orderings with ecart =pFDeg(end)-pFDeg(start)
99*/
100void deleteHC(poly* p, int* e, int* l,kStrategy strat)
101{
102  poly p1;
103
104  if (strat->kHEdgeFound)
105  {
106    if (pComp(*p,strat->kNoether) == -1)
107    {
108      pDelete(p);
109      *l = 0;
110      *e = -1;
111      return;
112    }
113    p1 = *p;
114    while (pNext(p1))
115    {
116      if (pComp0(pNext(p1), strat->kNoether) == -1)
117        pDelete(&pNext(p1));
118      else
119        pIter(p1);
120      *e = pLDeg(*p,l)-pFDeg(*p);
121    }
122  }
123}
124
125/*2
126*tests if p.p=monomial*unit and cancels the unit
127*/
128void cancelunit (LObject* p)
129{
130  int  i;
131  poly h;
132
133  if(!pIsVector((*p).p) && ((*p).ecart != 0))
134  {
135    h = pNext(((*p).p));
136    loop
137    {
138      if (!h)
139      {
140        pDelete(&(pNext((*p).p)));
141        (*p).ecart = 0;
142        (*p).length = 1;
143        return;
144      }
145      i = 0;
146      loop
147      {
148        i++;
149        if (pGetExp((*p).p,i) > pGetExp(h,i)) return ;
150        if (i == pVariables) break;
151      }
152      pIter(h);
153    }
154  }
155}
156
157/*2
158*pp is the new element in s
159*returns TRUE (in strat->kHEdgeFound) if
160*-HEcke is allowed
161*-we are in the last componente of the vector
162*-on all axis are monomials (all elements in NotUsedAxis are FALSE)
163*returns FALSE for pLexOrderings,
164*assumes in module case an ordering of type c* !!
165* HEckeTest is only called with strat->kHEdgeFound==FALSE !
166*/
167void HEckeTest (poly pp,kStrategy strat)
168{
169  int   j,k,p;
170
171  strat->kHEdgeFound=FALSE;
172  if (pLexOrder)
173  {
174    return;
175  }
176  if (strat->ak > 1)           /*we are in the module case*/
177  {
178    return; // until ....
179    //if (!pVectorOut)     /*pVectorOut <=> order = c,* */
180    //  return FALSE;
181    //if (pGetComp(pp) < strat->ak) /* ak is the number of the last component */
182    //  return FALSE;
183  }
184  k = 0;
185  p=pIsPurePower(pp);
186  if (p!=0) strat->NotUsedAxis[p] = FALSE;
187  /*- the leading term of pp is a power of the p-th variable -*/
188  for (j=pVariables;j>0; j--)
189  {
190    if (strat->NotUsedAxis[j])
191    {
192      return;
193    }
194  }
195  strat->kHEdgeFound=TRUE;
196}
197
198/*2
199*utilities for TSet, LSet
200*/
201inline intset initec (int maxnr)
202{
203  return (intset)Alloc(maxnr*sizeof(int));
204}
205
206void enlargeT (TSet* T,int* length,int incr)
207{
208  TSet h;
209
210  h = (TSet)Alloc(((*length)+incr)* sizeof(TObject));
211  /*for (i=0; i<=(*length)-1; i++) h[i] = (*T)[i];*/
212  memcpy(h, *T, (*length) * sizeof(TObject));
213  Free((ADDRESS)*T,((*length)*sizeof(TObject)));
214  *T = h;
215  (*length) += incr;
216}
217
218void cleanT (kStrategy strat)
219{
220  int i,j;
221  poly  p;
222
223  for (j=0; j<=strat->tl; j++)
224  {
225    p = strat->T[j].p;
226//    pTest(p);
227    strat->T[j].p=NULL;
228    i = -1;
229    loop
230    {
231      i++;
232      if (i>strat->sl)
233      {
234        pDelete(&p);
235        break;
236      }
237      if (p == strat->S[i])
238      {
239        break;
240      }
241    }
242  }
243  strat->tl=-1;
244}
245
246LSet initL ()
247{
248  return (LSet)Alloc(setmax*sizeof(LObject));
249}
250
251void enlargeL (LSet* L,int* length,int incr)
252{
253  LSet h;
254
255  h = (LSet)Alloc(((*length)+incr)*sizeof(LObject));
256  /*for (i=0; i<(*length); i++) h[i] = (*L)[i];*/
257  memcpy(h, *L, (*length) * sizeof(LObject));
258  Free((ADDRESS)*L,((*length)*sizeof(LObject)));
259  *L = h;
260  (*length) += incr;
261}
262
263void initPairtest(kStrategy strat)
264{
265  strat->pairtest = (BOOLEAN *)Alloc0((strat->sl+2)*sizeof(BOOLEAN));
266}
267
268/*2
269*test whether (p1,p2) or (p2,p1) is in L up position length
270*it returns TRUE if yes and the position k
271*/
272BOOLEAN isInPairsetL(int length,poly p1,poly p2,int*  k,kStrategy strat)
273{
274  LObject *p=&(strat->L[length]);
275
276  *k = length;
277  loop
278  {
279    if ((*k) < 0) return FALSE;
280    if (((p1 == (*p).p1) && (p2 == (*p).p2))
281    ||  ((p1 == (*p).p2) && (p2 == (*p).p1)))
282      return TRUE;
283    (*k)--;
284    p--;
285  }
286}
287
288/*2
289*in B all pairs have the same element p on the right
290*it tests whether (q,p) is in B and returns TRUE if yes
291*and the position k
292*/
293BOOLEAN isInPairsetB(poly q,int*  k,kStrategy strat)
294{
295  LObject *p=&(strat->B[strat->Bl]);
296
297  *k = strat->Bl;
298  loop
299  {
300    if ((*k) < 0) return FALSE;
301    if (q == (*p).p1)
302      return TRUE;
303    (*k)--;
304    p--;
305  }
306}
307
308#ifdef KDEBUG
309void K_Test (char *f, int l,kStrategy strat)
310{
311/*2
312*tests, if there is some wrong (deleted) in the set s or in pairs from L
313*/
314  int i;
315  /*- test pair P -*/
316  pDBTest(strat->P.p1,f,l);
317  pDBTest(strat->P.p2,f,l);
318  // pDBTest(strat->P.lcm,f,l);
319  /*- test set s -*/
320  if ((strat->S!=NULL) && (strat->sl>=0))
321  {
322    for (i=0; i<=strat->sl; i++)
323    {
324      pDBTest(strat->S[i],f,l);
325//      if(currQuotient==NULL)
326//      {
327//        if ((strat->ak!=0)
328//        && (pGetComp(strat->S[i])==0))
329//        {
330//          Print("wrong comp. S (0) in %s:%d\n",f,l);
331//        }
332//        else
333//        {
334//          if (pGetComp(strat->S[i])!=0)
335//            Print("wrong comp. S (%d) in %s:%d\n",pGetComp(strat->S[i]),f,l);
336//        }
337//      }
338    }
339  }
340  /*- test set L -*/
341  if ((strat->L!=NULL) && (strat->Ll>=0))
342  {
343    for (i=0; i<=strat->Ll; i++)
344    {
345      pDBTest(strat->L[i].p1,f,l);
346      pDBTest(strat->L[i].p2,f,l);
347      int j,f=0;
348      for(j=0;j<=strat->tl;j++)
349      {
350
351        if((strat->L[i].p1==NULL)
352        ||(strat->L[i].p1==strat->T[j].p))
353        {
354          f=1;
355          break;
356        }
357      }
358      if((f==0)&&(strat->L[i].p1!=NULL))
359        Print("L[%d].p1(%x) not in T ?\n",i,strat->L[i].p1);
360      f=0;
361      for(j=0;j<=strat->tl;j++)
362      {
363        if((strat->L[i].p2==NULL)
364        ||(strat->L[i].p2==strat->T[j].p))
365        {
366          f=1;
367          break;
368        }
369      }
370      if((f==0)&&(strat->L[i].p2!=NULL))
371        Print("L[%d].p2(%x) not in T ?\n",i,strat->L[i].p2);
372      //pDBTest(strat->L[i].lcm,f,l);
373    }
374  }
375/*
376*  #*- test set B -*#
377*  for (i=0; i<=strat->Bl; i++)
378*  {
379*    if (strat->B[i].p1 == (*p)) return ;
380*    if (strat->B[i].p2 == (*p)) return ;
381*  }
382*/
383  /*- test set T -*/
384  if ((strat->T!=NULL) && (strat->tl>=0))
385  {
386    for (i=0; i<=strat->tl; i++)
387    {
388      pDBTest(strat->T[i].p,f,l);
389//      if(currQuotient==NULL)
390//      {
391//        if ((strat->ak!=0)
392//        && (pGetComp(strat->T[i].p)==0))
393//        {
394//          Print("wrong comp. T (0) in %s:%d\n",f,l);
395//        }
396//        else
397//        {
398//          if (pGetComp(strat->T[i].p)!=0)
399//            Print("wrong comp. T (%d) in %s:%d\n",pGetComp(strat->T[i].p),f,l);
400//        }
401//      }
402    }
403  }
404#ifdef PDEBUG
405  idDBTest(strat->Shdl,f,l);
406  if (strat->D!=NULL) idDBTest(strat->D,f,l);
407#endif
408}
409#endif
410
411/*2
412*cancels the i-th polynomial in the standardbase s
413*/
414void deleteInS (int i,kStrategy strat)
415{
416  int j;
417
418  for (j=i; j<strat->sl; j++)
419  {
420    strat->S[j] = strat->S[j+1];
421    strat->ecartS[j] = strat->ecartS[j+1];
422  }
423  if (strat->fromQ!=NULL)
424  {
425    for (j=i; j<strat->sl; j++)
426    {
427      strat->fromQ[j] = strat->fromQ[j+1];
428    }
429  }
430  strat->S[strat->sl] = NULL;
431  strat->sl--;
432}
433
434/*2
435*cancels the i-th polynomial in the set
436*/
437void deleteInL (LSet set, int *length, int j,kStrategy strat)
438{
439  int i;
440
441  if (set[j].lcm!=NULL)
442    pFree1(set[j].lcm);
443  if (set[j].p!=NULL)
444  {
445    if (pNext(set[j].p) == strat->tail)
446    {
447      pFree1(set[j].p);
448      /*- tail belongs to several int spolys -*/
449    }
450    else pDelete(&(set[j].p));
451  }
452  if ((*length)>0)
453  {
454    for (i=j; i < (*length); i++)
455      set[i] = set[i+1];
456  }
457#ifdef KDEBUG
458  memset(&(set[*length]),0,sizeof(LObject));
459#endif
460  (*length)--;
461}
462
463/*2
464*is used after updating the pairset,if the leading term of p
465*devides the leading term of some S[i] it will be canceled
466*/
467void clearS (poly p, int* at, int* k,kStrategy strat)
468{
469  if (!pDivisibleBy(p,strat->S[*at])) return;
470  deleteInS((*at),strat);
471  (*at)--;
472  (*k)--;
473}
474
475/*2
476*enters p at position at in L
477*/
478void enterL (LSet *set,int *length, int *LSetmax, LObject p,int at)
479{
480  int i;
481
482  if ((*length)>=0)
483  {
484    if ((*length) == (*LSetmax)-1) enlargeL(set,LSetmax,setmax);
485    for (i=(*length)+1; i>=at+1; i--) (*set)[i] = (*set)[i-1];
486  }
487  else at = 0;
488  (*set)[at] = p;
489  (*length)++;
490}
491
492/*2
493* computes the normal ecart;
494* used in mora case and if pLexOrder & sugar in bba case
495*/
496void initEcartNormal (LObject* h)
497{
498  h->ecart = pLDeg(h->p,&(h->length))-pFDeg(h->p);
499}
500
501void initEcartBBA (LObject* h)
502{
503  (*h).ecart = 0;
504//#ifdef KDEBUG
505  (*h).length = 0;
506//#endif
507}
508
509void initEcartPairBba (LObject* Lp,poly f,poly g,int ecartF,int ecartG)
510{
511//#ifdef KDEBUG
512  (*Lp).ecart = 0;
513  (*Lp).length = 0;
514//#endif
515}
516
517void initEcartPairMora (LObject* Lp,poly f,poly g,int ecartF,int ecartG)
518{
519  (*Lp).ecart = max(ecartF,ecartG);
520  (*Lp).ecart = (*Lp).ecart-(pFDeg((*Lp).p)-pFDeg((*Lp).lcm));
521//#ifdef KDEBUG
522  (*Lp).length = 0;
523//#endif
524}
525
526/*2
527*if ecart1<=ecart2 it returns TRUE
528*/
529BOOLEAN sugarDivisibleBy(int ecart1, int ecart2)
530{
531  return (ecart1 <= ecart2);
532}
533
534/*2
535* put the pair (s[i],p)  into the set B, ecart=ecart(p)
536*/
537void enterOnePair (int i,poly p,int ecart, int isFromQ,kStrategy strat)
538{
539  int      l,j,compare;
540  LObject  Lp;
541
542#ifdef KDEBUG
543  Lp.ecart=0; Lp.length=0;
544#endif
545  /*- computes the lcm(s[i],p) -*/
546  Lp.lcm = pInit();
547  pLcm(p,strat->S[i],Lp.lcm);
548  pSetm(Lp.lcm);
549#ifdef DRING
550//  if ((pDRING) && (pdDFlag(Lp.lcm)==0))
551//  {
552//    for(j=1;j<=pdN;j++)
553//    {
554//      if((Lp.lcm->exp[pdX(j)]>0)
555//      &&(Lp.lcm->exp[pdIX(j)]>0))
556//      {
557//        pFree1(Lp.lcm);
558//        Lp.lcm=NULL;
559//        return;
560//      }
561//    }
562//    /*delete pairs with lcm contains x_j^a*x_j^(-b)*/
563//  }
564#endif
565  if (strat->sugarCrit)
566  {
567    if(
568#ifdef SDRING
569    (!pSDRING) &&
570#endif
571    (!((strat->ecartS[i]>0)&&(ecart>0)))
572    && pHasNotCF(p,strat->S[i]))
573    {
574    /*
575    *the product criterion has applied for (s,p),
576    *i.e. lcm(s,p)=product of the leading terms of s and p.
577    *Suppose (s,r) is in L and the leading term
578    *of p devides lcm(s,r)
579    *(==> the leading term of p devides the leading term of r)
580    *but the leading term of s does not devide the leading term of r
581    *(notice that tis condition is automatically satisfied if r is still
582    *in S), then (s,r) can be canceled.
583    *This should be done here because the
584    *case lcm(s,r)=lcm(s,p) is not covered by chainCrit.
585    */
586      strat->cp++;
587      pFree1(Lp.lcm);
588      Lp.lcm=NULL;
589      return;
590    }
591    else
592      Lp.ecart = max(ecart,strat->ecartS[i]);
593    if (strat->fromT && (strat->ecartS[i]>ecart))
594    {
595      pFree1(Lp.lcm);
596      Lp.lcm=NULL;
597      return;
598      /*the pair is (s[i],t[.]), discard it if the ecart is too big*/
599    }
600    /*
601    *the set B collects the pairs of type (S[j],p)
602    *suppose (r,p) is in B and (s,p) is the new pair and lcm(s,p)#lcm(r,p)
603    *if the leading term of s devides lcm(r,p) then (r,p) will be canceled
604    *if the leading term of r devides lcm(s,p) then (s,p) will not enter B
605    */
606#ifdef SDRING
607    if (!pSDRING)
608#endif
609    {
610      j = strat->Bl;
611      loop
612      {
613        if (j < 0)  break;
614        compare=pDivComp(strat->B[j].lcm,Lp.lcm);
615        if ((compare==1)
616        &&(sugarDivisibleBy(strat->B[j].ecart,Lp.ecart)))
617        {
618          strat->c3++;
619          if ((strat->fromQ==NULL) || (isFromQ==0) || (strat->fromQ[i]==0))
620          {
621            pFree1(Lp.lcm);
622            return;
623          }
624          break;
625        }
626        else
627        if ((compare ==-1)
628        && sugarDivisibleBy(Lp.ecart,strat->B[j].ecart))
629        {
630          deleteInL(strat->B,&strat->Bl,j,strat);
631          strat->c3++;
632        }
633        j--;
634      }
635    }
636  }
637  else /*sugarcrit*/
638  {
639    if(/*(strat->ak==0) && productCrit(p,strat->S[i])*/
640#ifdef SDRING
641    (!pSDRING) &&
642#endif
643    pHasNotCF(p,strat->S[i]))
644    {
645    /*
646    *the product criterion has applied for (s,p),
647    *i.e. lcm(s,p)=product of the leading terms of s and p.
648    *Suppose (s,r) is in L and the leading term
649    *of p devides lcm(s,r)
650    *(==> the leading term of p devides the leading term of r)
651    *but the leading term of s does not devide the leading term of r
652    *(notice that tis condition is automatically satisfied if r is still
653    *in S), then (s,r) can be canceled.
654    *This should be done here because the
655    *case lcm(s,r)=lcm(s,p) is not covered by chainCrit.
656    */
657      strat->cp++;
658      pFree1(Lp.lcm);
659      Lp.lcm=NULL;
660      return;
661    }
662    if (strat->fromT && (strat->ecartS[i]>ecart))
663    {
664      pFree1(Lp.lcm);
665      Lp.lcm=NULL;
666      return;
667      /*the pair is (s[i],t[.]), discard it if the ecart is too big*/
668    }
669    /*
670    *the set B collects the pairs of type (S[j],p)
671    *suppose (r,p) is in B and (s,p) is the new pair and lcm(s,p)#lcm(r,p)
672    *if the leading term of s devides lcm(r,p) then (r,p) will be canceled
673    *if the leading term of r devides lcm(s,p) then (s,p) will not enter B
674    */
675#ifdef SDRING
676    if (!pSDRING)
677#endif
678    for(j = strat->Bl;j>=0;j--)
679    {
680      compare=pDivComp(strat->B[j].lcm,Lp.lcm);
681      if (compare==1)
682      {
683        strat->c3++;
684        if ((strat->fromQ==NULL) || (isFromQ==0) || (strat->fromQ[i]==0))
685        {
686          pFree1(Lp.lcm);
687          return;
688        }
689        break;
690      }
691      else
692      if (compare ==-1)
693      {
694        deleteInL(strat->B,&strat->Bl,j,strat);
695        strat->c3++;
696      }
697    }
698  }
699  /*
700  *the pair (S[i],p) enters B if the spoly != 0
701  */
702  /*-  compute the short s-polynomial -*/
703  if (strat->fromT && !TEST_OPT_INTSTRATEGY)
704    pNorm(p);
705  if ((strat->S[i]==NULL) || (p==NULL))
706    return;
707  if ((strat->fromQ!=NULL) && (isFromQ!=0) && (strat->fromQ[i]!=0))
708    Lp.p=NULL;
709  else
710#ifdef SDRING
711  // spSpolyShortBba will not work for SDRING
712  if (pSDRING)
713  {
714    Lp.p=spSpolyCreate(strat->S[i],p,strat->kNoether);
715    if (Lp.p!=NULL)
716      pDelete(&pNext(Lp.p));
717  }
718  else
719#endif
720  {
721#ifdef KDEBUG
722    pTest(strat->S[i]);
723    pTest(p);
724#endif
725    Lp.p = spSpolyShortBba(strat->S[i],p);
726  }
727  if (Lp.p == NULL)
728  {
729    /*- the case that the s-poly is 0 -*/
730    if (strat->pairtest==NULL) initPairtest(strat);
731    strat->pairtest[i] = TRUE;/*- hint for spoly(S^[i],p)=0 -*/
732    strat->pairtest[strat->sl+1] = TRUE;
733    /*hint for spoly(S[i],p) == 0 for some i,0 <= i <= sl*/
734    /*
735    *suppose we have (s,r),(r,p),(s,p) and spoly(s,p) == 0 and (r,p) is
736    *still in B (i.e. lcm(r,p) == lcm(s,p) or the leading term of s does not
737    *devide lcm(r,p)). In the last case (s,r) can be canceled if the leading
738    *term of p devides the lcm(s,r)
739    *(this canceling should be done here because
740    *the case lcm(s,p) == lcm(s,r) is not covered in chainCrit)
741    *the first case is handeled in chainCrit
742    */
743    if (Lp.lcm!=NULL) pFree1(Lp.lcm);
744  }
745  else
746  {
747    /*- the pair (S[i],p) enters B -*/
748    Lp.p1 = strat->S[i];
749    Lp.p2 = p;
750    pNext(Lp.p) = strat->tail;
751    strat->initEcartPair(&Lp,strat->S[i],p,strat->ecartS[i],ecart);
752    if (TEST_OPT_INTSTRATEGY)
753    {
754      nDelete(&(Lp.p->coef));
755    }
756    l = strat->posInL(strat->B,strat->Bl,Lp,strat);
757    enterL(&strat->B,&strat->Bl,&strat->Bmax,Lp,l);
758  }
759#ifdef DRING
760  if ((pDRING) && (pdDFlag(strat->S[i])==0) && (pdDFlag(p)==0))
761  {
762    Lp.lcm=pOne();
763    pdLcm(strat->S[i],p,Lp.lcm);
764    Lp.p1 = strat->S[i];
765    Lp.p2 = p;
766#ifdef KDEBUG
767    pTest(p);
768    pTest(strat->S[i]);
769#endif
770    Lp.p = pdSpolyCreate(strat->S[i],p);
771    if (Lp.p!=NULL)
772    {
773#ifdef KDEBUG
774      pTest(p);
775      pTest(strat->S[i]);
776#endif
777      /*spoly of p and S[i] bzgl Lp.lcm*/
778      strat->initEcartPair(&Lp,strat->S[i],p,strat->ecartS[i],ecart);
779      l = strat->posInL(strat->B,strat->Bl,Lp,strat);
780      enterL(&strat->B,&strat->Bl,&strat->Bmax,Lp,l);
781    }
782  }
783#endif
784}
785
786/*2
787* put the pair (s[i],p) into the set L, ecart=ecart(p)
788* in the case that s forms a SB of (s)
789*/
790void enterOnePairSpecial (int i,poly p,int ecart,kStrategy strat)
791{
792  int      l,j,compare;
793  LObject  Lp;
794
795  Lp.lcm = pInit();
796  pLcm(p,strat->S[i],Lp.lcm);
797  pSetm(Lp.lcm);
798  if(
799#ifdef SDRING
800  (!pSDRING) &&
801#endif
802  (pHasNotCF(p,strat->S[i])))
803  {
804    strat->cp++;
805    pFree1(Lp.lcm);
806    Lp.lcm=NULL;
807    return;
808  }
809  for(j = strat->Ll;j>=0;j--)
810  {
811    compare=pDivComp(strat->L[j].lcm,Lp.lcm);
812    if ((compare==1) || (pEqual(strat->L[j].lcm,Lp.lcm)))
813    {
814      strat->c3++;
815      pFree1(Lp.lcm);
816      return;
817    }
818    else if (compare ==-1)
819    {
820      deleteInL(strat->L,&strat->Ll,j,strat);
821      strat->c3++;
822    }
823  }
824  /*-  compute the short s-polynomial -*/
825
826#ifdef SDRING
827  // spSpolyShortBba will not work for SDRING
828  if (pSDRING)
829  {
830    Lp.p=spSpolyCreate(strat->S[i],p,strat->kNoether);
831    if (Lp.p!=NULL)
832      pDelete(&pNext(Lp.p));
833  }
834  else
835#endif
836  {
837    Lp.p = spSpolyShortBba(strat->S[i],p);
838  }
839  if (Lp.p == NULL)
840  {
841     pFree1(Lp.lcm);
842  }
843  else
844  {
845    /*- the pair (S[i],p) enters B -*/
846    Lp.p1 = strat->S[i];
847    Lp.p2 = p;
848    pNext(Lp.p) = strat->tail;
849    strat->initEcartPair(&Lp,strat->S[i],p,strat->ecartS[i],ecart);
850    if (TEST_OPT_INTSTRATEGY)
851    {
852      nDelete(&(Lp.p->coef));
853    }
854    l = strat->posInL(strat->L,strat->Ll,Lp,strat);
855    enterL(&strat->L,&strat->Ll,&strat->Lmax,Lp,l);
856  }
857#ifdef DRING
858  if ((pDRING) && (pdDFlag(strat->S[i])==0) && (pdDFlag(p)==0))
859  {
860    Lp.lcm=pOne();
861    pdLcm(strat->S[i],p,Lp.lcm);
862    Lp.p1 = strat->S[i];
863    Lp.p2 = p;
864#ifdef KDEBUG
865    pTest(p);
866    pTest(strat->S[i]);
867#endif
868    Lp.p = pdSpolyCreate(strat->S[i],p);
869    if (Lp.p!=NULL)
870    {
871#ifdef KDEBUG
872      pTest(p);
873      pTest(strat->S[i]);
874#endif
875      /*spoly of p and S[i] bzgl Lp.lcm*/
876      strat->initEcartPair(&Lp,strat->S[i],p,strat->ecartS[i],ecart);
877      l = strat->posInL(strat->B,strat->Bl,Lp,strat);
878      enterL(&strat->B,&strat->Bl,&strat->Bmax,Lp,l);
879    }
880  }
881#endif
882}
883
884/*2
885*the pairset B of pairs of type (s[i],p) is complete now. It will be updated
886*using the chain-criterion in B and L and enters B to L
887*/
888void chainCrit (poly p,int ecart,kStrategy strat)
889{
890  int i,j,l;
891
892  /*
893  *pairtest[i] is TRUE if spoly(S[i],p) == 0.
894  *In this case all elements in B such
895  *that their lcm is divisible by the leading term of S[i] can be canceled
896  */
897  if (strat->pairtest!=NULL)
898  {
899#ifdef SDRING
900    if (!pSDRING)
901#endif
902    {
903      /*- i.e. there is an i with pairtest[i]==TRUE -*/
904      for (j=0; j<=strat->sl; j++)
905      {
906        if (strat->pairtest[j])
907        {
908          for (i=strat->Bl; i>=0; i--)
909          {
910            if (pDivisibleBy(strat->S[j],strat->B[i].lcm))
911            {
912              deleteInL(strat->B,&strat->Bl,i,strat);
913              strat->c3++;
914            }
915          }
916        }
917      }
918    }
919    Free((ADDRESS)strat->pairtest,(strat->sl+2)*sizeof(BOOLEAN));
920    strat->pairtest=NULL;
921  }
922  if ((strat->Gebauer || strat->fromT)
923#ifdef SDRING
924  && (!pSDRING)
925#endif
926  )
927  {
928    if (strat->sugarCrit)
929    {
930    /*
931    *suppose L[j] == (s,r) and p/lcm(s,r)
932    *and lcm(s,r)#lcm(s,p) and lcm(s,r)#lcm(r,p)
933    *and in case the sugar is o.k. then L[j] can be canceled
934    */
935      for (j=strat->Ll; j>=0; j--)
936      {
937        if (sugarDivisibleBy(ecart,strat->L[j].ecart)
938        && ((pNext(strat->L[j].p) == strat->tail) || (pOrdSgn==1))
939        && pCompareChain(p,strat->L[j].p1,strat->L[j].p2,strat->L[j].lcm))
940        {
941          if (strat->L[j].p == strat->tail)
942          {
943            deleteInL(strat->L,&strat->Ll,j,strat);
944            strat->c3++;
945          }
946        }
947      }
948      /*
949      *this is GEBAUER-MOELLER:
950      *in B all elements with the same lcm except the "best"
951      *(i.e. the last one in B with this property) will be canceled
952      */
953      j = strat->Bl;
954      loop /*cannot be changed into a for !!! */
955      {
956        if (j <= 0) break;
957        i = j-1;
958        loop
959        {
960          if (i <  0) break;
961          if (pEqual(strat->B[j].lcm,strat->B[i].lcm))
962          {
963            strat->c3++;
964            if (sugarDivisibleBy(strat->B[j].ecart,strat->B[i].ecart))
965            {
966              deleteInL(strat->B,&strat->Bl,i,strat);
967              j--;
968            }
969            else
970            {
971              deleteInL(strat->B,&strat->Bl,j,strat);
972              break;
973            }
974          }
975          i--;
976        }
977        j--;
978      }
979    }
980    else /*sugarCrit*/
981    {
982      /*
983      *suppose L[j] == (s,r) and p/lcm(s,r)
984      *and lcm(s,r)#lcm(s,p) and lcm(s,r)#lcm(r,p)
985      *and in case the sugar is o.k. then L[j] can be canceled
986      */
987      for (j=strat->Ll; j>=0; j--)
988      {
989        if (pCompareChain(p,strat->L[j].p1,strat->L[j].p2,strat->L[j].lcm))
990        {
991          if ((pNext(strat->L[j].p) == strat->tail)||(pOrdSgn==1))
992          {
993            deleteInL(strat->L,&strat->Ll,j,strat);
994            strat->c3++;
995          }
996        }
997      }
998      /*
999      *this is GEBAUER-MOELLER:
1000      *in B all elements with the same lcm except the "best"
1001      *(i.e. the last one in B with this property) will be canceled
1002      */
1003      j = strat->Bl;
1004      loop   /*cannot be changed into a for !!! */
1005      {
1006        if (j <= 0) break;
1007        for(i=j-1; i>=0; i--)
1008        {
1009          if (pEqual(strat->B[j].lcm,strat->B[i].lcm))
1010          {
1011            strat->c3++;
1012            deleteInL(strat->B,&strat->Bl,i,strat);
1013            j--;
1014          }
1015        }
1016        j--;
1017      }
1018    }
1019    /*
1020    *the elements of B enter L/their order with respect to B is kept
1021    *j = posInL(L,j,B[i]) would permutate the order
1022    *if once B is ordered different from L
1023    *then one should use j = posInL(L,Ll,B[i])
1024    */
1025    j = strat->Ll+1;
1026    for (i=strat->Bl; i>=0; i--)
1027    {
1028      j = strat->posInL(strat->L,j-1,strat->B[i],strat);
1029      enterL(&strat->L,&strat->Ll,&strat->Lmax,strat->B[i],j);
1030    }
1031    strat->Bl = -1;
1032  }
1033  else
1034  {
1035#ifdef SDRING
1036    if (!pSDRING)
1037#endif
1038    for (j=strat->Ll; j>=0; j--)
1039    {
1040      if (pCompareChain(p,strat->L[j].p1,strat->L[j].p2,strat->L[j].lcm))
1041      {
1042        if ((pNext(strat->L[j].p) == strat->tail)||(pOrdSgn==1))
1043        {
1044          deleteInL(strat->L,&strat->Ll,j,strat);
1045          strat->c3++;
1046        }
1047      }
1048    }
1049    /*
1050    *this is our MODIFICATION of GEBAUER-MOELLER:
1051    *First the elements of B enter L,
1052    *then we fix a lcm and the "best" element in L
1053    *(i.e the last in L with this lcm and of type (s,p))
1054    *and cancel all the other elements of type (r,p) with this lcm
1055    *except the case the element (s,r) has also the same lcm
1056    *and is on the worst position with respect to (s,p) and (r,p)
1057    */
1058    /*
1059    *B enters to L/their order with respect to B is permutated for elements
1060    *B[i].p with the same leading term
1061    */
1062    j = strat->Ll;
1063    for (i=strat->Bl; i>=0; i--)
1064    {
1065      j = strat->posInL(strat->L,j,strat->B[i],strat);
1066      enterL(&strat->L,&strat->Ll,&strat->Lmax,strat->B[i],j);
1067    }
1068    strat->Bl = -1;
1069    j = strat->Ll;
1070#ifdef SDRING
1071    if (!pSDRING)
1072#endif
1073    loop  /*cannot be changed into a for !!! */
1074    {
1075      if (j <= 0)
1076      {
1077        /*now L[0] cannot be canceled any more and the tail can be removed*/
1078        if (strat->L[0].p2 == strat->tail) strat->L[0].p2 = p;
1079        break;
1080      }
1081      if (strat->L[j].p2 == p)
1082      {
1083        i = j-1;
1084        loop
1085        {
1086          if (i < 0)  break;
1087          if ((strat->L[i].p2 == p) && pEqual(strat->L[j].lcm,strat->L[i].lcm))
1088          {
1089            /*L[i] could be canceled but we search for a better one to cancel*/
1090            strat->c3++;
1091            if (isInPairsetL(i-1,strat->L[j].p1,strat->L[i].p1,&l,strat)
1092            && (pNext(strat->L[l].p) == strat->tail)
1093            && (!pEqual(strat->L[i].p,strat->L[l].p))
1094            && pDivisibleBy(p,strat->L[l].lcm))
1095            {
1096              /*
1097              *"NOT equal(...)" because in case of "equal" the element L[l]
1098              *is "older" and has to be from theoretical point of view behind
1099              *L[i], but we do not want to reorder L
1100              */
1101              strat->L[i].p2 = strat->tail;
1102              /*
1103              *L[l] will be canceled, we cannot cancel L[i] later on,
1104              *so we mark it with "tail"
1105              */
1106              deleteInL(strat->L,&strat->Ll,l,strat);
1107              i--;
1108            }
1109            else
1110            {
1111              deleteInL(strat->L,&strat->Ll,i,strat);
1112            }
1113            j--;
1114          }
1115          i--;
1116        }
1117      }
1118      else if (strat->L[j].p2 == strat->tail)
1119      {
1120        /*now L[j] cannot be canceled any more and the tail can be removed*/
1121        strat->L[j].p2 = p;
1122      }
1123      j--;
1124    }
1125  }
1126}
1127
1128/*2
1129*(s[0],h),...,(s[k],h) will be put to the pairset L
1130*/
1131void initenterpairs (poly h,int k,int ecart,int isFromQ,kStrategy strat)
1132{
1133
1134  if ((strat->syzComp==0)
1135  || (pGetComp(h)<=strat->syzComp))
1136  {
1137    int j;
1138    BOOLEAN new_pair=FALSE;
1139
1140    if (pGetComp(h)==0)
1141    {
1142      /* for Q!=NULL: build pairs (f,q),(f1,f2), but not (q1,q2)*/
1143      if ((isFromQ)&&(strat->fromQ!=NULL))
1144      {
1145        for (j=0; j<=k; j++)
1146        {
1147          if (!strat->fromQ[j])
1148          {
1149            new_pair=TRUE;
1150            enterOnePair(j,h,ecart,isFromQ,strat);
1151          //Print("j:%d, Ll:%d\n",j,strat->Ll);
1152          }
1153        }
1154      }
1155      else
1156      {
1157        new_pair=TRUE;
1158        for (j=0; j<=k; j++)
1159        {
1160          enterOnePair(j,h,ecart,isFromQ,strat);
1161          //Print("j:%d, Ll:%d\n",j,strat->Ll);
1162        }
1163      }
1164    }
1165    else
1166    {
1167      for (j=0; j<=k; j++)
1168      {
1169        if ((pGetComp(h)==pGetComp(strat->S[j]))
1170        || (pGetComp(strat->S[j])==0))
1171        {
1172          new_pair=TRUE;
1173          enterOnePair(j,h,ecart,isFromQ,strat);
1174        //Print("j:%d, Ll:%d\n",j,strat->Ll);
1175        }
1176      }
1177    }
1178    if (new_pair) chainCrit(h,ecart,strat);
1179  }
1180}
1181
1182/*2
1183*(s[0],h),...,(s[k],h) will be put to the pairset L(via initenterpairs)
1184*superfluous elements in S will be deleted
1185*/
1186void enterpairs (poly h,int k,int ecart,int pos,kStrategy strat)
1187{
1188  int j=pos;
1189
1190  initenterpairs(h,k,ecart,0,strat);
1191  if ((!strat->fromT)
1192  && ((strat->syzComp==0)
1193    ||(pGetComp(h)<=strat->syzComp)))
1194  {
1195    //Print("start clearS k=%d, pos=%d, sl=%d\n",k,pos,strat->sl);
1196    loop
1197    {
1198      if (j > k) break;
1199      clearS(h,&j,&k,strat);
1200      j++;
1201    }
1202    //Print("end clearS sl=%d\n",strat->sl);
1203  }
1204 // PrintS("end enterpairs\n");
1205}
1206
1207/*2
1208*(s[0],h),...,(s[k],h) will be put to the pairset L(via initenterpairs)
1209*superfluous elements in S will be deleted
1210*/
1211void enterpairsSpecial (poly h,int k,int ecart,int pos,kStrategy strat)
1212{
1213  int j;
1214
1215  for (j=0; j<=k; j++)
1216  {
1217    if ((pGetComp(h)==pGetComp(strat->S[j]))
1218    || (0==pGetComp(strat->S[j])))
1219    {
1220      enterOnePairSpecial(j,h,ecart,strat);
1221    }
1222  }
1223  j=pos;
1224  loop
1225  {
1226    if (j > k) break;
1227    clearS(h,&j,&k,strat);
1228    j++;
1229  }
1230}
1231
1232/*2
1233*constructs the pairset at the beginning
1234*of the buchberger/mora algorithm
1235*/
1236void pairs (kStrategy strat)
1237{
1238  int j,i;
1239//  Print("pairs:sl=%d\n",strat->sl);
1240//  for (i=0; i<=strat->sl; i++)
1241//  {
1242//    Print("s%d:",i);pWrite(strat->S[i]);
1243//  }
1244  if (strat->fromQ!=NULL)
1245  {
1246    for (i=1; i<=strat->sl; i++)
1247    {
1248      initenterpairs(strat->S[i],i-1,strat->ecartS[i],strat->fromQ[i],strat);
1249    }
1250  }
1251  else
1252  {
1253    for (i=1; i<=strat->sl; i++)
1254    {
1255      initenterpairs(strat->S[i],i-1,strat->ecartS[i],0,strat);
1256    }
1257  }
1258  /*deletes superfluous elements in S*/
1259  i = -1;
1260  loop
1261  {
1262    i++;
1263    if (i >= strat->sl) break;
1264    if ((strat->syzComp==0) || (pGetComp(strat->S[i])<=strat->syzComp))
1265    {
1266      j=i;
1267      loop
1268      {
1269        j++;
1270        if (j > strat->sl) break;
1271        if (pDivisibleBy(strat->S[i],strat->S[j]))
1272        {
1273        //  Print("delete %d=",j);
1274        //  wrp(strat->S[j]);
1275        //  Print(" wegen %d=",i);
1276        //  wrp(strat->S[i]);
1277        //  Print("( fromQ=%d)\n", (strat->fromQ) ? strat->fromQ[j]:0);
1278          if ((strat->fromQ==NULL) || (strat->fromQ[j]==0))
1279          {
1280            deleteInS(j,strat);
1281            j--;
1282          }
1283        }
1284      }
1285    }
1286  }
1287}
1288
1289/*2
1290*reorders  s with respect to posInS,
1291*suc is the first changed index or zero
1292*/
1293void reorderS (int* suc,kStrategy strat)
1294{
1295  int i,j,at,ecart;
1296  int fq=0;
1297  poly  p;
1298
1299  *suc = -1;
1300  for (i=1; i<=strat->sl; i++)
1301  {
1302    at = posInS(strat->S,i-1,strat->S[i]);
1303    if (at != i)
1304    {
1305      if ((*suc > at) || (*suc == -1)) *suc = at;
1306      p = strat->S[i];
1307      ecart = strat->ecartS[i];
1308      if (strat->fromQ!=NULL) fq=strat->fromQ[i];
1309      for (j=i; j>=at+1; j--)
1310      {
1311        strat->S[j] = strat->S[j-1];
1312        strat->ecartS[j] = strat->ecartS[j-1];
1313      }
1314      strat->S[at] = p;
1315      strat->ecartS[at] = ecart;
1316      if (strat->fromQ!=NULL)
1317      {
1318        for (j=i; j>=at+1; j--)
1319        {
1320          strat->fromQ[j] = strat->fromQ[j-1];
1321        }
1322        strat->fromQ[at]=fq;
1323      }
1324    }
1325  }
1326}
1327
1328
1329/*2
1330*looks up the position of p in set
1331*set[0] is the smallest with respect to the ordering-procedure
1332*pComp
1333*/
1334int posInS (polyset set,int length,poly p)
1335{
1336  if(length==-1) return 0;
1337  int i;
1338  int an = 0;
1339  int en= length;
1340//  pTest(set[length]);
1341//  pTest(p);
1342  if (pMixedOrder)
1343  {
1344    int o=pWTotaldegree(p);
1345    int oo=pWTotaldegree(set[length]);
1346
1347    if ((oo<o)
1348    || ((o==oo) && (pComp0(set[length],p)!= pOrdSgn)))
1349    {
1350      return length+1;
1351    }
1352
1353    loop
1354    {
1355      if (an >= en-1)
1356      {
1357        if ((pWTotaldegree(set[an])>=o) && (pComp0(set[an],p) == pOrdSgn))
1358        {
1359          return an;
1360        }
1361        return en;
1362      }
1363      i=(an+en) / 2;
1364      if ((pWTotaldegree(set[an])>=o)
1365      && (pComp0(set[i],p) == pOrdSgn)) en=i;
1366      else                              an=i;
1367    }
1368  }
1369  else
1370  {
1371    if (pComp0(set[length],p)!= pOrdSgn) return length+1;
1372
1373    loop
1374    {
1375      if (an >= en-1)
1376      {
1377        if (pComp0(set[an],p) == pOrdSgn) return an;
1378        return en;
1379      }
1380      i=(an+en) / 2;
1381      if (pComp0(set[i],p) == pOrdSgn) en=i;
1382      else                             an=i;
1383    }
1384  }
1385}
1386
1387
1388/*2
1389* looks up the position of p in set
1390* the position is the last one
1391*/
1392int posInT0 (TSet set,int length,LObject p)
1393{
1394  return (length+1);
1395}
1396
1397
1398/*2
1399* looks up the position of p in T
1400* set[0] is the smallest with respect to the ordering-procedure
1401* pComp
1402*/
1403int posInT1 (TSet set,int length,LObject p)
1404{
1405  if (length==-1) return 0;
1406
1407  int i;
1408  int an = 0;
1409  int en= length;
1410
1411  if (pComp0(set[length].p,p.p)!= pOrdSgn) return length+1;
1412
1413  loop
1414  {
1415    if (an >= en-1)
1416    {
1417      if (pComp0(set[an].p,p.p) == pOrdSgn) return an;
1418      return en;
1419    }
1420    i=(an+en) / 2;
1421    if (pComp0(set[i].p,p.p) == pOrdSgn) en=i;
1422    else                                 an=i;
1423  }
1424}
1425
1426/*2
1427* looks up the position of p in T
1428* set[0] is the smallest with respect to the ordering-procedure
1429* length
1430*/
1431int posInT2 (TSet set,int length,LObject p)
1432{
1433  if (length==-1) return 0;
1434
1435  int i;
1436  int an = 0;
1437  int en= length;
1438
1439  if (set[length].length<p.length) return length+1;
1440
1441  loop
1442  {
1443    if (an >= en-1)
1444    {
1445      if (set[an].length>p.length) return an;
1446      return en;
1447    }
1448    i=(an+en) / 2;
1449    if (set[i].length>p.length) en=i;
1450    else                        an=i;
1451  }
1452}
1453
1454/*2
1455* looks up the position of p in T
1456* set[0] is the smallest with respect to the ordering-procedure
1457* totaldegree,pComp
1458*/
1459int posInT11 (TSet set,int length,LObject p)
1460/*{
1461 * int j=0;
1462 * int o;
1463 *
1464 * o = pFDeg(p.p);
1465 * loop
1466 * {
1467 *   if ((pFDeg(set[j].p) > o)
1468 *   || ((pFDeg(set[j].p) == o) && (pComp0(set[j].p,p.p) == pOrdSgn)))
1469 *   {
1470 *     return j;
1471 *   }
1472 *   j++;
1473 *   if (j > length) return j;
1474 * }
1475 *}
1476 */
1477{
1478  if (length==-1) return 0;
1479
1480  int i;
1481  int an = 0;
1482  int en= length;
1483  int o = pFDeg(p.p);
1484
1485  if ((pFDeg(set[length].p) < o)
1486  || ((pFDeg(set[length].p) == o) && (pComp0(set[length].p,p.p) != pOrdSgn)))
1487    return length+1;
1488
1489  loop
1490  {
1491    if (an >= en-1)
1492    {
1493      if ((pFDeg(set[an].p) > o)
1494      || ((pFDeg(set[an].p) == o) && (pComp0(set[an].p,p.p) == pOrdSgn)))
1495        return an;
1496      return en;
1497    }
1498    i=(an+en) / 2;
1499    if ((pFDeg(set[i].p) > o)
1500    || ((pFDeg(set[i].p) == o) && (pComp0(set[i].p,p.p) == pOrdSgn)))
1501      en=i;
1502    else
1503      an=i;
1504  }
1505}
1506
1507/*2
1508* looks up the position of p in T
1509* set[0] is the smallest with respect to the ordering-procedure
1510* totaldegree,pComp
1511*/
1512int posInT110 (TSet set,int length,LObject p)
1513{
1514  if (length==-1) return 0;
1515
1516  int i;
1517  int an = 0;
1518  int en= length;
1519  int o = pFDeg(p.p);
1520
1521  if ((pFDeg(set[length].p) < o)
1522  || ((pFDeg(set[length].p) == o) && (set[length].length<p.length))
1523  || ((pFDeg(set[length].p) == o) && (set[length].length == p.length)
1524     && (pComp0(set[length].p,p.p) != pOrdSgn)))
1525    return length+1;
1526
1527  loop
1528  {
1529    if (an >= en-1)
1530    {
1531      if ((pFDeg(set[an].p) > o)
1532      || ((pFDeg(set[an].p) == o) && (set[an].length > p.length))
1533      || ((pFDeg(set[an].p) == o) && (set[an].length == p.length)
1534         && (pComp0(set[an].p,p.p) == pOrdSgn)))
1535        return an;
1536      return en;
1537    }
1538    i=(an+en) / 2;
1539    if ((pFDeg(set[i].p) > o)
1540    || ((pFDeg(set[i].p) == o) && (set[i].length > p.length))
1541    || ((pFDeg(set[i].p) == o) && (set[i].length == p.length)
1542       && (pComp0(set[i].p,p.p) == pOrdSgn)))
1543      en=i;
1544    else
1545      an=i;
1546  }
1547}
1548
1549/*2
1550* looks up the position of p in set
1551* set[0] is the smallest with respect to the ordering-procedure
1552* pFDeg
1553*/
1554int posInT13 (TSet set,int length,LObject p)
1555{
1556  if (length==-1) return 0;
1557
1558  int i;
1559  int an = 0;
1560  int en= length;
1561  int o = pFDeg(p.p);
1562
1563  if (pFDeg(set[length].p) <= o)
1564    return length+1;
1565
1566  loop
1567  {
1568    if (an >= en-1)
1569    {
1570      if (pFDeg(set[an].p) > o)
1571        return an;
1572      return en;
1573    }
1574    i=(an+en) / 2;
1575    if (pFDeg(set[i].p) > o)
1576      en=i;
1577    else
1578      an=i;
1579  }
1580}
1581
1582/*2
1583* looks up the position of p in set
1584* set[0] is the smallest with respect to the ordering-procedure
1585* maximaldegree, pComp
1586*/
1587int posInT15 (TSet set,int length,LObject p)
1588/*{
1589 *int j=0;
1590 * int o;
1591 *
1592 * o = pFDeg(p.p)+p.ecart;
1593 * loop
1594 * {
1595 *   if ((pFDeg(set[j].p)+set[j].ecart > o)
1596 *   || ((pFDeg(set[j].p)+set[j].ecart == o)
1597 *     && (pComp0(set[j].p,p.p) == pOrdSgn)))
1598 *   {
1599 *     return j;
1600 *   }
1601 *   j++;
1602 *   if (j > length) return j;
1603 * }
1604 *}
1605 */
1606{
1607  if (length==-1) return 0;
1608
1609  int i;
1610  int an = 0;
1611  int en= length;
1612  int o = pFDeg(p.p) + p.ecart;
1613
1614  if ((pFDeg(set[length].p)+set[length].ecart < o)
1615  || ((pFDeg(set[length].p)+set[length].ecart == o)
1616     && (pComp0(set[length].p,p.p) != pOrdSgn)))
1617    return length+1;
1618
1619  loop
1620  {
1621    if (an >= en-1)
1622    {
1623      if ((pFDeg(set[an].p)+set[an].ecart > o)
1624      || ((pFDeg(set[an].p)+set[an].ecart  == o)
1625         && (pComp0(set[an].p,p.p) == pOrdSgn)))
1626        return an;
1627      return en;
1628    }
1629    i=(an+en) / 2;
1630    if ((pFDeg(set[i].p) +set[i].ecart > o)
1631    || ((pFDeg(set[i].p) +set[i].ecart == o)
1632       && (pComp0(set[i].p,p.p) == pOrdSgn)))
1633      en=i;
1634    else
1635      an=i;
1636  }
1637}
1638
1639/*2
1640* looks up the position of p in set
1641* set[0] is the smallest with respect to the ordering-procedure
1642* pFDeg+ecart, ecart, pComp
1643*/
1644int posInT17 (TSet set,int length,LObject p)
1645/*
1646*{
1647* int j=0;
1648* int  o;
1649*
1650*  o = pFDeg(p.p)+p.ecart;
1651*  loop
1652*  {
1653*    if ((pFDeg(set[j].p)+set[j].ecart > o)
1654*    || (((pFDeg(set[j].p)+set[j].ecart == o)
1655*      && (set[j].ecart < p.ecart)))
1656*    || ((pFDeg(set[j].p)+set[j].ecart == o)
1657*      && (set[j].ecart==p.ecart)
1658*      && (pComp0(set[j].p,p.p)==pOrdSgn)))
1659*      return j;
1660*    j++;
1661*    if (j > length) return j;
1662*  }
1663* }
1664*/
1665{
1666  if (length==-1) return 0;
1667
1668  int i;
1669  int an = 0;
1670  int en= length;
1671  int o = pFDeg(p.p) + p.ecart;
1672
1673  if ((pFDeg(set[length].p)+set[length].ecart < o)
1674  || ((pFDeg(set[length].p)+set[length].ecart == o)
1675     && (set[length].ecart > p.ecart))
1676  || ((pFDeg(set[length].p)+set[length].ecart == o)
1677     && (set[length].ecart==p.ecart)
1678     && (pComp0(set[length].p,p.p) != pOrdSgn)))
1679    return length+1;
1680
1681  loop
1682  {
1683    if (an >= en-1)
1684    {
1685      if ((pFDeg(set[an].p)+set[an].ecart > o)
1686      || ((pFDeg(set[an].p)+set[an].ecart == o) && (set[an].ecart < p.ecart))
1687      || ((pFDeg(set[an].p)+set[an].ecart  == o)
1688         && (set[an].ecart==p.ecart)
1689         && (pComp0(set[an].p,p.p) == pOrdSgn)))
1690        return an;
1691      return en;
1692    }
1693    i=(an+en) / 2;
1694    if ((pFDeg(set[i].p) +set[i].ecart > o)
1695    || ((pFDeg(set[i].p)+set[i].ecart == o)
1696       && (set[i].ecart < p.ecart))
1697    || ((pFDeg(set[i].p) +set[i].ecart == o)
1698       && (set[i].ecart == p.ecart)
1699       && (pComp0(set[i].p,p.p) == pOrdSgn)))
1700      en=i;
1701    else
1702      an=i;
1703  }
1704}
1705/*2
1706* looks up the position of p in set
1707* set[0] is the smallest with respect to the ordering-procedure
1708* pGetComp, pFDeg+ecart, ecart, pComp
1709*/
1710int posInT17_c (TSet set,int length,LObject p)
1711{
1712  if (length==-1) return 0;
1713
1714  int i;
1715  int an = 0;
1716  int en= length;
1717  int cc = (-1+2*currRing->order[0]==ringorder_c);
1718  /* cc==1 for (c,..), cc==-1 for (C,..) */
1719  int o = pFDeg(p.p) + p.ecart;
1720  int c = pGetComp(p.p)*cc;
1721
1722  if (pGetComp(set[length].p)*cc < c)
1723    return length+1;
1724  if (pGetComp(set[length].p)*cc == c)
1725  {
1726    if ((pFDeg(set[length].p)+set[length].ecart < o)
1727    || ((pFDeg(set[length].p)+set[length].ecart == o)
1728       && (set[length].ecart > p.ecart))
1729    || ((pFDeg(set[length].p)+set[length].ecart == o)
1730       && (set[length].ecart==p.ecart)
1731       && (pComp0(set[length].p,p.p) != pOrdSgn)))
1732      return length+1;
1733  }
1734
1735  loop
1736  {
1737    if (an >= en-1)
1738    {
1739      if (pGetComp(set[an].p)*cc < c)
1740        return en;
1741      if (pGetComp(set[an].p)*cc == c)
1742      {
1743        if ((pFDeg(set[an].p)+set[an].ecart > o)
1744        || ((pFDeg(set[an].p)+set[an].ecart == o) && (set[an].ecart < p.ecart))
1745        || ((pFDeg(set[an].p)+set[an].ecart  == o)
1746           && (set[an].ecart==p.ecart)
1747           && (pComp0(set[an].p,p.p) == pOrdSgn)))
1748          return an;
1749      }
1750      return en;
1751    }
1752    i=(an+en) / 2;
1753    if (pGetComp(set[i].p)*cc > c)
1754      en=i;
1755    else if (pGetComp(set[i].p)*cc == c)
1756    {
1757      if ((pFDeg(set[i].p) +set[i].ecart > o)
1758      || ((pFDeg(set[i].p)+set[i].ecart == o)
1759         && (set[i].ecart < p.ecart))
1760      || ((pFDeg(set[i].p) +set[i].ecart == o)
1761         && (set[i].ecart == p.ecart)
1762         && (pComp0(set[i].p,p.p) == pOrdSgn)))
1763        en=i;
1764      else
1765        an=i;
1766    }
1767    else
1768      an=i;
1769  }
1770}
1771
1772/*2
1773* looks up the position of p in set
1774* set[0] is the smallest with respect to
1775* ecart, pFDeg, length
1776*/
1777int posInT19 (TSet set,int length,LObject p)
1778{
1779  if (length==-1) return 0;
1780
1781  int i;
1782  int an = 0;
1783  int en= length;
1784  int o = p.ecart;
1785
1786  if ((set[length].ecart < o)
1787  || (((set[length].ecart == o)
1788     && (pFDeg(set[length].p) < pFDeg(p.p)))
1789  || ((set[length].ecart == o)
1790     && (pFDeg(set[length].p) == pFDeg(p.p))
1791     && (set[length].length < p.length))))
1792    return length+1;
1793
1794  loop
1795  {
1796    if (an >= en-1)
1797    {
1798      if ((set[an].ecart > o)
1799      || (((set[an].ecart == o)
1800         && (pFDeg(set[an].p) > pFDeg(p.p)))
1801      || ((set[an].ecart  == o)
1802         && (pFDeg(set[an].p) == pFDeg(p.p))
1803         && (set[an].length > p.length))))
1804        return an;
1805      return en;
1806    }
1807    i=(an+en) / 2;
1808    if ((set[i].ecart > o)
1809    || (((set[i].ecart == o)
1810       && (pFDeg(set[i].p) > pFDeg(p.p)))
1811    || ((set[i].ecart == o)
1812       && (pFDeg(set[i].p) == pFDeg(p.p))
1813       && (set[i].length > p.length))))
1814      en=i;
1815    else
1816      an=i;
1817  }
1818}
1819
1820/*2
1821*looks up the position of polynomial p in set
1822*set[length] is the smallest element in set with respect
1823*to the ordering-procedure pComp
1824*/
1825int posInLSpecial (LSet set, int length, LObject p,kStrategy strat)
1826{
1827  int i;
1828  int an = 0;
1829  int en= length;
1830  int d=pFDeg(p.p);
1831
1832  if (length<0) return 0;
1833  if ((pFDeg(set[length].p)>d)
1834     || ((pFDeg(set[length].p)==d) && (p.p1!=NULL)&&(set[length].p1==NULL))
1835     || (pComp0(set[length].p,p.p)== pOrdSgn))
1836     return length+1;
1837  loop
1838  {
1839    if (an >= en-1)
1840    {
1841      if ((pFDeg(set[an].p)>d)
1842         || ((pFDeg(set[an].p)==d) && (p.p1!=NULL)&&(set[an].p1==NULL))
1843         || (pComp0(set[an].p,p.p)== pOrdSgn))
1844         return en;
1845      return an;
1846    }
1847    i=(an+en) / 2;
1848    if ((pFDeg(set[i].p)>d)
1849       || ((pFDeg(set[i].p)==d) && (p.p1!=NULL)&&(set[i].p1==NULL))
1850       || (pComp0(set[i].p,p.p)== pOrdSgn))
1851       an=i;
1852    else
1853       en=i;
1854  }
1855}
1856
1857/*2
1858*looks up the position of polynomial p in set
1859*set[length] is the smallest element in set with respect
1860*to the ordering-procedure pComp
1861*/
1862int posInL0 (LSet set, int length, LObject p,kStrategy strat)
1863{
1864  int i;
1865  int an = 0;
1866  int en= length;
1867
1868  if (length<0) return 0;
1869  if (pComp0(set[length].p,p.p)== pOrdSgn) return length+1;
1870  loop
1871  {
1872    if (an >= en-1)
1873    {
1874      if (pComp0(set[an].p,p.p) == pOrdSgn) return en;
1875      return an;
1876    }
1877    i=(an+en) / 2;
1878    if (pComp0(set[i].p,p.p) == pOrdSgn) an=i;
1879    else                                 en=i;
1880    /*aend. fuer lazy == in !=- machen */
1881  }
1882}
1883
1884/*2
1885* looks up the position of polynomial p in set
1886* e is the ecart of p
1887* set[length] is the smallest element in set with respect
1888* to the ordering-procedure totaldegree,pComp
1889*/
1890int posInL11 (LSet set, int length, LObject p,kStrategy strat)
1891/*{
1892 * int j=0;
1893 * int o;
1894 *
1895 * o = pFDeg(p.p);
1896 * loop
1897 * {
1898 *   if (j > length)            return j;
1899 *   if ((pFDeg(set[j].p) < o)) return j;
1900 *   if ((pFDeg(set[j].p) == o) && (pComp0(set[j].p,p.p) == -pOrdSgn))
1901 *   {
1902 *     return j;
1903 *   }
1904 *   j++;
1905 * }
1906 *}
1907 */
1908{
1909  int i;
1910  int an = 0;
1911  int en= length;
1912  int o = pFDeg(p.p);
1913
1914  if (length<0) return 0;
1915  if ((pFDeg(set[length].p) > o)
1916  || ((pFDeg(set[length].p) == o) && (pComp0(set[length].p,p.p) != -pOrdSgn)))
1917    return length+1;
1918  loop
1919  {
1920    if (an >= en-1)
1921    {
1922      if ((pFDeg(set[an].p) > o)
1923      || ((pFDeg(set[an].p) == o) && (pComp0(set[an].p,p.p) != -pOrdSgn)))
1924        return en;
1925      return an;
1926    }
1927    i=(an+en) / 2;
1928    if ((pFDeg(set[i].p) > o)
1929    || ((pFDeg(set[i].p) == o) && (pComp0(set[i].p,p.p) != -pOrdSgn)))
1930      an=i;
1931    else
1932      en=i;
1933  }
1934}
1935
1936/*2
1937* looks up the position of polynomial p in set
1938* set[length] is the smallest element in set with respect
1939* to the ordering-procedure totaldegree,pLength0
1940*/
1941int posInL110 (LSet set, int length, LObject p,kStrategy strat)
1942{
1943  int i;
1944  int an = 0;
1945  int en= length;
1946  int o = pFDeg(p.p);
1947
1948  if (length<0) return 0;
1949  if ((pFDeg(set[length].p) > o)
1950  || ((pFDeg(set[length].p) == o) && (set[length].length >2*p.length))
1951  || ((pFDeg(set[length].p) == o) && (set[length].length <= 2*p.length)
1952     && (pComp0(set[length].p,p.p) != -pOrdSgn)))
1953    return length+1;
1954  loop
1955  {
1956    if (an >= en-1)
1957    {
1958      if ((pFDeg(set[an].p) > o)
1959      || ((pFDeg(set[an].p) == o) && (set[an].length >2*p.length))
1960      || ((pFDeg(set[an].p) == o) && (set[an].length <=2*p.length)
1961         && (pComp0(set[an].p,p.p) != -pOrdSgn)))
1962        return en;
1963      return an;
1964    }
1965    i=(an+en) / 2;
1966    if ((pFDeg(set[i].p) > o)
1967    || ((pFDeg(set[i].p) == o) && (set[i].length > 2*p.length))
1968    || ((pFDeg(set[i].p) == o) && (set[i].length <= 2*p.length)
1969       && (pComp0(set[i].p,p.p) != -pOrdSgn)))
1970      an=i;
1971    else
1972      en=i;
1973  }
1974}
1975/*2
1976* looks up the position of polynomial p in set
1977* e is the ecart of p
1978* set[length] is the smallest element in set with respect
1979* to the ordering-procedure totaldegree
1980*/
1981int posInL13 (LSet set, int length, LObject p,kStrategy strat)
1982{
1983  int i;
1984  int an = 0;
1985  int en= length;
1986  int o = pFDeg(p.p);
1987
1988  if (pFDeg(set[length].p) > o)
1989    return length+1;
1990
1991  loop
1992  {
1993    if (an >= en-1)
1994    {
1995      if (pFDeg(set[an].p) >= o)
1996        return en;
1997      return an;
1998    }
1999    i=(an+en) / 2;
2000    if (pFDeg(set[i].p) >= o)
2001      an=i;
2002    else
2003      en=i;
2004  }
2005}
2006
2007/*2
2008* looks up the position of polynomial p in set
2009* e is the ecart of p
2010* set[length] is the smallest element in set with respect
2011* to the ordering-procedure maximaldegree,pComp
2012*/
2013int posInL15 (LSet set, int length, LObject p,kStrategy strat)
2014/*{
2015 * int j=0;
2016 * int o;
2017 *
2018 * o = p.ecart+pFDeg(p.p);
2019 * loop
2020 * {
2021 *   if (j > length)                       return j;
2022 *   if (pFDeg(set[j].p)+set[j].ecart < o) return j;
2023 *   if ((pFDeg(set[j].p)+set[j].ecart == o)
2024 *   && (pComp0(set[j].p,p.p) == -pOrdSgn))
2025 *   {
2026 *     return j;
2027 *   }
2028 *   j++;
2029 * }
2030 *}
2031 */
2032{
2033  int i;
2034  int an = 0;
2035  int en= length;
2036  int o = pFDeg(p.p) + p.ecart;
2037
2038  if (length<0) return 0;
2039  if ((pFDeg(set[length].p) + set[length].ecart > o)
2040  || ((pFDeg(set[length].p) + set[length].ecart == o)
2041     && (pComp0(set[length].p,p.p) != -pOrdSgn)))
2042    return length+1;
2043  loop
2044  {
2045    if (an >= en-1)
2046    {
2047      if ((pFDeg(set[an].p) + set[an].ecart > o)
2048      || ((pFDeg(set[an].p) + set[an].ecart == o)
2049         && (pComp0(set[an].p,p.p) != -pOrdSgn)))
2050        return en;
2051      return an;
2052    }
2053    i=(an+en) / 2;
2054    if ((pFDeg(set[i].p) + set[i].ecart > o)
2055    || ((pFDeg(set[i].p) +set[i].ecart == o)
2056       && (pComp0(set[i].p,p.p) != -pOrdSgn)))
2057      an=i;
2058    else
2059      en=i;
2060  }
2061}
2062
2063/*2
2064* looks up the position of polynomial p in set
2065* e is the ecart of p
2066* set[length] is the smallest element in set with respect
2067* to the ordering-procedure totaldegree
2068*/
2069int posInL17 (LSet set, int length, LObject p,kStrategy strat)
2070/*
2071*{
2072*  int j=0;
2073* int o;
2074*
2075*  o = pFDeg(p.p)+p.ecart;
2076*  loop
2077*  {
2078*    if (j > length)
2079*     return j;
2080*    if (pFDeg(set[j].p)+set[j].ecart < o)
2081*      return j;
2082*    if ((pFDeg(set[j].p)+set[j].ecart == o)
2083*    && (set[j].ecart < p.ecart))
2084*      return j;
2085*    if ((pFDeg(set[j].p)+set[j].ecart == o)
2086*    && (set[j].ecart == p.ecart)
2087*    && (pComp0(set[j].p,p.p)==(-pOrdSgn)))
2088*      return j;
2089*    j++;
2090*  }
2091* }
2092*/
2093{
2094  int i;
2095  int an = 0;
2096  int en= length;
2097  int o = pFDeg(p.p) + p.ecart;
2098
2099  if (length<0) return 0;
2100  if ((pFDeg(set[length].p) + set[length].ecart > o)
2101  || ((pFDeg(set[length].p) + set[length].ecart == o)
2102     && (set[length].ecart > p.ecart))
2103  || ((pFDeg(set[length].p) + set[length].ecart == o)
2104     && (set[length].ecart == p.ecart)
2105     && (pComp0(set[length].p,p.p) != -pOrdSgn)))
2106    return length+1;
2107  loop
2108  {
2109    if (an >= en-1)
2110    {
2111      if ((pFDeg(set[an].p) + set[an].ecart > o)
2112      || ((pFDeg(set[an].p) + set[an].ecart == o)
2113         && (set[an].ecart > p.ecart))
2114      || ((pFDeg(set[an].p) + set[an].ecart == o)
2115         && (set[an].ecart == p.ecart)
2116         && (pComp0(set[an].p,p.p) != -pOrdSgn)))
2117        return en;
2118      return an;
2119    }
2120    i=(an+en) / 2;
2121    if ((pFDeg(set[i].p) + set[i].ecart > o)
2122    || ((pFDeg(set[i].p) + set[i].ecart == o)
2123       && (set[i].ecart > p.ecart))
2124    || ((pFDeg(set[i].p) +set[i].ecart == o)
2125       && (set[i].ecart == p.ecart)
2126       && (pComp0(set[i].p,p.p) != -pOrdSgn)))
2127      an=i;
2128    else
2129      en=i;
2130  }
2131}
2132/*2
2133* looks up the position of polynomial p in set
2134* e is the ecart of p
2135* set[length] is the smallest element in set with respect
2136* to the ordering-procedure pComp
2137*/
2138int posInL17_c (LSet set, int length, LObject p,kStrategy strat)
2139{
2140  int i;
2141  int an = 0;
2142  int en= length;
2143  int cc = (-1+2*currRing->order[0]==ringorder_c);
2144  /* cc==1 for (c,..), cc==-1 for (C,..) */
2145  int c = pGetComp(p.p)*cc;
2146  int o = pFDeg(p.p) + p.ecart;
2147
2148  if (length<0) return 0;
2149  if (pGetComp(set[length].p)*cc > c)
2150    return length+1;
2151  if (pGetComp(set[length].p)*cc == c)
2152  {
2153    if ((pFDeg(set[length].p) + set[length].ecart > o)
2154    || ((pFDeg(set[length].p) + set[length].ecart == o)
2155       && (set[length].ecart > p.ecart))
2156    || ((pFDeg(set[length].p) + set[length].ecart == o)
2157       && (set[length].ecart == p.ecart)
2158       && (pComp0(set[length].p,p.p) != -pOrdSgn)))
2159      return length+1;
2160  }
2161  loop
2162  {
2163    if (an >= en-1)
2164    {
2165      if (pGetComp(set[an].p)*cc > c)
2166        return en;
2167      if (pGetComp(set[an].p)*cc == c)
2168      {
2169        if ((pFDeg(set[an].p) + set[an].ecart > o)
2170        || ((pFDeg(set[an].p) + set[an].ecart == o)
2171           && (set[an].ecart > p.ecart))
2172        || ((pFDeg(set[an].p) + set[an].ecart == o)
2173           && (set[an].ecart == p.ecart)
2174           && (pComp0(set[an].p,p.p) != -pOrdSgn)))
2175          return en;
2176      }
2177      return an;
2178    }
2179    i=(an+en) / 2;
2180    if (pGetComp(set[i].p)*cc > c)
2181      an=i;
2182    else if (pGetComp(set[i].p)*cc == c)
2183    {
2184      if ((pFDeg(set[i].p) + set[i].ecart > o)
2185      || ((pFDeg(set[i].p) + set[i].ecart == o)
2186         && (set[i].ecart > p.ecart))
2187      || ((pFDeg(set[i].p) +set[i].ecart == o)
2188         && (set[i].ecart == p.ecart)
2189         && (pComp0(set[i].p,p.p) != -pOrdSgn)))
2190        an=i;
2191      else
2192        en=i;
2193    }
2194    else
2195      en=i;
2196  }
2197}
2198/*2
2199* reduces h using the set S
2200* procedure used in redtail
2201*/
2202/*
2203*void redtailS (poly* h,int maxIndex,kStrategy strat)
2204*{
2205*  int j = 0;
2206*
2207*  while (j <= maxIndex)
2208*  {
2209*    if (pDivisibleBy((strat->S)[j],*h))
2210*    {
2211*      *h = spSpolyRed(strat->S[j],*h,strat->kNoether);
2212*      if (*h == NULL) return;
2213*      j = 0;
2214*    }
2215*    else j++;
2216*  }
2217*}
2218*/
2219
2220/*2
2221*compute the normalform of the tail p->next of p
2222*with respect to S
2223*/
2224poly redtail (poly p, int pos, kStrategy strat)
2225{
2226  poly h, hn;
2227  int j, e, l;
2228
2229  if (strat->noTailReduction)
2230  {
2231    return p;
2232  }
2233  h = p;
2234  hn = pNext(h);
2235  while(hn != NULL)
2236  {
2237    e = pLDeg(hn,&l)-pFDeg(hn);
2238    j = 0;
2239    while (j <= pos)
2240    {
2241      if (pDivisibleBy(strat->S[j], hn)
2242      && ((e >= strat->ecartS[j])
2243        || strat->kHEdgeFound
2244        || ((Kstd1_deg>0)&&(pFDeg(hn)<=Kstd1_deg)))
2245      )
2246      {
2247        spSpolyTail(strat->S[j], p, h, strat->kNoether);
2248        hn = pNext(h);
2249        if (hn == NULL)
2250        {
2251          return p;
2252        }
2253        e = pLDeg(hn,&l)-pFDeg(hn);
2254        j = 0;
2255      }
2256      else
2257      {
2258        j++;
2259      }
2260    }
2261    h = hn;
2262    hn = pNext(h);
2263  }
2264  return p;
2265}
2266
2267/*2
2268*compute the normalform of the tail p->next of p
2269*with respect to S
2270*/
2271poly redtailBba (poly p, int pos, kStrategy strat)
2272{
2273  poly h, hn;
2274  int j;
2275
2276  if (strat->noTailReduction)
2277  {
2278    return p;
2279  }
2280  h = p;
2281  hn = pNext(h);
2282  while(hn != NULL)
2283  {
2284    j = 0;
2285    while (j <= pos)
2286    {
2287      if (pDivisibleBy(strat->S[j], hn))
2288      {
2289        spSpolyTail(strat->S[j], p, h, strat->kNoether);
2290        hn = pNext(h);
2291        if (hn == NULL)
2292        {
2293          return p;
2294        }
2295        j = 0;
2296      }
2297      else
2298      {
2299        j++;
2300      }
2301    }
2302    h = hn;
2303    hn = pNext(h);
2304  }
2305  return p;
2306}
2307
2308/*2
2309*compute the "normalform" of the tail p->next of p
2310*with respect to S for syzygies
2311*/
2312poly redtailSyz (poly p, int pos, kStrategy strat)
2313{
2314  poly h, hn;
2315  int j;
2316
2317  if (strat->noTailReduction)
2318  {
2319    return p;
2320  }
2321  h = p;
2322  hn = pNext(h);
2323  while(hn != NULL)
2324  {
2325    j = 0;
2326    while (j <= pos)
2327    {
2328      if (pDivisibleBy(strat->S[j], hn) && (!pEqual(strat->S[j],h)))
2329      {
2330        spSpolyTail(strat->S[j], p, h, strat->kNoether);
2331        hn = pNext(h);
2332        if (hn == NULL)
2333        {
2334          return p;
2335        }
2336        j = 0;
2337      }
2338      else
2339      {
2340        j++;
2341      }
2342    }
2343    h = hn;
2344    hn = pNext(h);
2345  }
2346  return p;
2347}
2348
2349/*2
2350*checks the change degree and write progress report
2351*/
2352void message (int i,int* reduc,int* olddeg,kStrategy strat)
2353{
2354  if (i != *olddeg)
2355  {
2356    Print("%d",i);
2357    *olddeg = i;
2358  }
2359  if (strat->Ll != *reduc)
2360  {
2361    if (strat->Ll != *reduc-1)
2362      Print("(%d)",strat->Ll+1);
2363    else
2364      PrintS("-");
2365    *reduc = strat->Ll;
2366  }
2367  else
2368    PrintS(".");
2369  mflush();
2370}
2371
2372/*2
2373*statistics
2374*/
2375void messageStat (int srmax,int lrmax,int hilbcount,kStrategy strat)
2376{
2377  //PrintS("\nUsage/Allocation of temporary storage:\n");
2378  //Print("%d/%d polynomials in standard base\n",srmax,IDELEMS(Shdl));
2379  //Print("%d/%d polynomials in set L (for lazy alg.)",lrmax+1,strat->Lmax);
2380  Print("\nproduct criterion:%d chain criterion:%d\n",strat->cp,strat->c3);
2381  if (hilbcount!=0) Print("hilbert series criterion:%d\n",hilbcount);
2382  /*mflush();*/
2383}
2384
2385/*2
2386*debugging output: all internal sets, if changed
2387*for testing purpuse only/has to be changed for later use
2388*/
2389void messageSets (kStrategy strat)
2390{
2391  int i;
2392  if (strat->news)
2393  {
2394    PrintS("set S");
2395    for (i=0; i<=strat->sl; i++)
2396    {
2397      Print("\n  %d:",i);
2398      wrp(strat->S[i]);
2399    }
2400    strat->news = FALSE;
2401  }
2402  if (strat->newt)
2403  {
2404    PrintS("\nset T");
2405    for (i=0; i<=strat->tl; i++)
2406    {
2407      Print("\n  %d:",i);
2408      wrp(strat->T[i].p);
2409      Print(" o:%d e:%d l:%d",
2410        /*pFDeg(strat->T[i].p)*/pGetOrder(strat->T[i].p),strat->T[i].ecart,strat->T[i].length);
2411    }
2412    strat->newt = FALSE;
2413  }
2414  PrintS("\nset L");
2415  for (i=strat->Ll; i>=0; i--)
2416  {
2417    Print("\n%d:",i);
2418    wrp(strat->L[i].p1);
2419    PrintS("  ");
2420    wrp(strat->L[i].p2);
2421    PrintS(" lcm: ");wrp(strat->L[i].lcm);
2422    PrintS("\n  p : ");
2423    wrp(strat->L[i].p);
2424    Print("  o:%d e:%d l:%d",
2425     pGetOrder(strat->L[i].p),strat->L[i].ecart,strat->L[i].length);
2426  }
2427  PrintLn();
2428}
2429
2430/*2
2431*construct the set s from F
2432*/
2433void initS (ideal F, ideal Q,kStrategy strat)
2434{
2435  LObject h;
2436  int   i,pos;
2437#ifdef SDRING
2438  polyset aug=(polyset)Alloc(setmax*sizeof(poly));
2439  int augmax=setmax, augl=-1;
2440#endif
2441
2442  h.ecart=0; h.length=0;
2443  if (Q!=NULL) i=IDELEMS(Q);
2444  else i=0;
2445  i=((i+IDELEMS(F)+15)/16)*16;
2446  strat->ecartS=initec(i);
2447  strat->fromQ=NULL;
2448  strat->Shdl=idInit(i,F->rank);
2449  strat->S=strat->Shdl->m;
2450  /*- put polys into S -*/
2451  if (Q!=NULL)
2452  {
2453    strat->fromQ=initec(i);
2454    memset(strat->fromQ,0,i*sizeof(int));
2455    for (i=0; i<IDELEMS(Q); i++)
2456    {
2457      if (Q->m[i]!=NULL)
2458      {
2459        h.p = pCopy(Q->m[i]);
2460        if (TEST_OPT_INTSTRATEGY)
2461        {
2462          //pContent(h.p);
2463          pCleardenom(h.p); // also does a pContent
2464        }
2465        else
2466        {
2467          pNorm(h.p);
2468        }
2469        strat->initEcart(&h);
2470        if (pOrdSgn==-1)
2471        {
2472          deleteHC(&h.p, &h.ecart, &h.length,strat);
2473        }
2474        if (h.p!=NULL)
2475        {
2476          if (strat->sl==-1)
2477            pos =0;
2478          else
2479          {
2480            pos = posInS(strat->S,strat->sl,h.p);
2481          }
2482          strat->enterS(h,pos,strat);
2483          strat->fromQ[pos]=1;
2484        }
2485      }
2486    }
2487  }
2488  for (i=0; i<IDELEMS(F); i++)
2489  {
2490    if (F->m[i]!=NULL)
2491    {
2492      h.p = pCopy(F->m[i]);
2493#ifdef SDRING
2494      aug[0]=h.p;
2495      augl=0;
2496#ifdef SRING
2497      if (pSRING)
2498        psAug(pCopy(h.p),pOne(),&aug,&augl,&augmax);
2499#endif
2500#ifdef DRING
2501      if (pDRING)
2502        pdAug(pCopy(h.p),&aug,&augl,&augmax);
2503#endif
2504      for (augl++;augl != 0;)
2505      {
2506        h.p=aug[--augl];
2507#ifdef KDEBUG
2508        pTest(h.p);
2509#endif
2510        if (TEST_OPT_DEBUG && pSDRING)
2511        {
2512          PrintS("new (aug) s:");
2513          wrp(h.p);
2514          PrintLn();
2515        }
2516#endif
2517        if (TEST_OPT_INTSTRATEGY)
2518        {
2519          //pContent(h.p);
2520          pCleardenom(h.p); // also does a pContent
2521        }
2522        else
2523        {
2524          pNorm(h.p);
2525        }
2526        strat->initEcart(&h);
2527        if (pOrdSgn==-1)
2528        {
2529          cancelunit(&h);  /*- tries to cancel a unit -*/
2530          deleteHC(&h.p, &h.ecart, &h.length,strat);
2531        }
2532        if (TEST_OPT_DEGBOUND
2533        && (((strat->honey) && (h.ecart+pFDeg(h.p)>Kstd1_deg))
2534          || ((!(strat->honey)) && (pFDeg(h.p)>Kstd1_deg))))
2535          pDelete(&h.p);
2536        else
2537        if (h.p!=NULL)
2538        {
2539          if (strat->sl==-1)
2540            pos =0;
2541          else
2542          {
2543            pos = posInS(strat->S,strat->sl,h.p);
2544          }
2545          strat->enterS(h,pos,strat);
2546        }
2547#ifdef SDRING
2548      }
2549#endif
2550    }
2551  }
2552  /*- test, if a unit is in F -*/
2553  if ((strat->sl>=0) && pIsConstant(strat->S[0]))
2554  {
2555    while (strat->sl>0) deleteInS(strat->sl,strat);
2556  }
2557#ifdef SDRING
2558  Free((ADDRESS)aug,augmax*sizeof(poly));
2559#endif
2560//  for(augl=0;augl<=strat->sl;augl++)
2561//    pTest(strat->S[augl]);
2562}
2563
2564void initSL (ideal F, ideal Q,kStrategy strat)
2565{
2566  LObject h;
2567  int   i,pos;
2568#ifdef SDRING
2569  polyset aug=(polyset)Alloc(setmax*sizeof(poly));
2570  int augmax=setmax, augl=-1;
2571#endif
2572
2573  h.ecart=0; h.length=0;
2574  if (Q!=NULL) i=IDELEMS(Q);
2575  else i=0;
2576  i=((i+16)/16)*16;
2577  strat->ecartS=initec(i);
2578  strat->fromQ=NULL;
2579  strat->Shdl=idInit(i,F->rank);
2580  strat->S=strat->Shdl->m;
2581  /*- put polys into S -*/
2582  if (Q!=NULL)
2583  {
2584    strat->fromQ=initec(i);
2585    memset(strat->fromQ,0,i*sizeof(int));
2586    for (i=0; i<IDELEMS(Q); i++)
2587    {
2588      if (Q->m[i]!=NULL)
2589      {
2590        h.p = pCopy(Q->m[i]);
2591        if (TEST_OPT_INTSTRATEGY)
2592        {
2593          //pContent(h.p);
2594          pCleardenom(h.p); // also does a pContent
2595        }
2596        else
2597        {
2598          pNorm(h.p);
2599        }
2600        strat->initEcart(&h);
2601        if (pOrdSgn==-1)
2602        {
2603          deleteHC(&h.p, &h.ecart, &h.length,strat);
2604        }
2605        if (h.p!=NULL)
2606        {
2607          if (strat->sl==-1)
2608            pos =0;
2609          else
2610          {
2611            pos = posInS(strat->S,strat->sl,h.p);
2612          }
2613          strat->enterS(h,pos,strat);
2614          strat->fromQ[pos]=1;
2615        }
2616      }
2617    }
2618  }
2619  for (i=0; i<IDELEMS(F); i++)
2620  {
2621    if (F->m[i]!=NULL)
2622    {
2623      h.p = pCopy(F->m[i]);
2624      h.p1=NULL;
2625      h.p2=NULL;
2626      h.lcm=NULL;
2627#ifdef SDRING
2628      aug[0]=h.p;
2629      augl=0;
2630#ifdef SRING
2631      if (pSRING)
2632        psAug(pCopy(h.p),pOne(),&aug,&augl,&augmax);
2633#endif
2634#ifdef DRING
2635      if (pDRING)
2636        pdAug(pCopy(h.p),&aug,&augl,&augmax);
2637#endif
2638      for (augl++;augl != 0;)
2639      {
2640        h.p=aug[--augl];
2641#ifdef KDEBUG
2642        pTest(h.p);
2643#endif
2644        if (TEST_OPT_DEBUG && pSDRING)
2645        {
2646          PrintS("new (aug) s:");
2647          wrp(h.p);
2648          PrintLn();
2649        }
2650#endif
2651        if (TEST_OPT_INTSTRATEGY)
2652        {
2653          //pContent(h.p);
2654          pCleardenom(h.p); // also does a pContent
2655        }
2656        else
2657        {
2658          pNorm(h.p);
2659        }
2660        strat->initEcart(&h);
2661        if (pOrdSgn==-1)
2662        {
2663          cancelunit(&h);  /*- tries to cancel a unit -*/
2664          deleteHC(&h.p, &h.ecart, &h.length,strat);
2665        }
2666        if (TEST_OPT_DEGBOUND
2667        && (((strat->honey) && (h.ecart+pFDeg(h.p)>Kstd1_deg))
2668          || ((!(strat->honey)) && (pFDeg(h.p)>Kstd1_deg))))
2669          pDelete(&h.p);
2670        else
2671        if (h.p!=NULL)
2672        {
2673          if (strat->Ll==-1)
2674            pos =0;
2675          else
2676          {
2677            pos = strat->posInL(strat->L,strat->Ll,h,strat);
2678          }
2679          enterL(&strat->L,&strat->Ll,&strat->Lmax,h,pos);
2680        }
2681#ifdef SDRING
2682      }
2683#endif
2684    }
2685  }
2686  /*- test, if a unit is in F -*/
2687  if ((strat->Ll>=0) && pIsConstant(strat->L[strat->Ll].p))
2688  {
2689    while (strat->Ll>0) deleteInL(strat->L,&strat->Ll,strat->Ll-1,strat);
2690  }
2691#ifdef SDRING
2692  Free((ADDRESS)aug,augmax*sizeof(poly));
2693#endif
2694//  for(augl=0;augl<=strat->sl;augl++)
2695//    pTest(strat->S[augl]);
2696}
2697
2698
2699/*2
2700*construct the set s from F u {P}
2701*/
2702void initSSpecial (ideal F, ideal Q, ideal P,kStrategy strat)
2703{
2704  LObject h;
2705  int   i,pos;
2706#ifdef SDRING
2707  polyset aug=(polyset)Alloc(setmax*sizeof(poly));
2708  int augmax=setmax, augl=-1;
2709#endif
2710
2711  h.ecart=0; h.length=0;
2712  if (Q!=NULL) i=IDELEMS(Q);
2713  else i=0;
2714  i=((i+IDELEMS(F)+15)/16)*16;
2715  strat->ecartS=initec(i);
2716  strat->fromQ=NULL;
2717  strat->Shdl=idInit(i,F->rank);
2718  strat->S=strat->Shdl->m;
2719
2720  /*- put polys into S -*/
2721  if (Q!=NULL)
2722  {
2723    strat->fromQ=initec(i);
2724    memset(strat->fromQ,0,i*sizeof(int));
2725    for (i=0; i<IDELEMS(Q); i++)
2726    {
2727      if (Q->m[i]!=NULL)
2728      {
2729        h.p = pCopy(Q->m[i]);
2730        //if (TEST_OPT_INTSTRATEGY)
2731        //{
2732        //  //pContent(h.p);
2733        //  pCleardenom(h.p); // also does a pContent
2734        //}
2735        //else
2736        //{
2737        //  pNorm(h.p);
2738        //}
2739        strat->initEcart(&h);
2740        if (pOrdSgn==-1)
2741        {
2742          deleteHC(&h.p, &h.ecart, &h.length,strat);
2743        }
2744        if (h.p!=NULL)
2745        {
2746          if (strat->sl==-1)
2747            pos =0;
2748          else
2749          {
2750            pos = posInS(strat->S,strat->sl,h.p);
2751          }
2752          strat->enterS(h,pos,strat);
2753          strat->fromQ[pos]=1;
2754        }
2755      }
2756    }
2757  }
2758  /*- put polys into S -*/
2759  for (i=0; i<IDELEMS(F); i++)
2760  {
2761    if (F->m[i]!=NULL)
2762    {
2763      h.p = pCopy(F->m[i]);
2764      if (pOrdSgn==1)
2765      {
2766        h.p=redtailBba(h.p,strat->sl,strat);
2767      }
2768      strat->initEcart(&h);
2769      if (pOrdSgn==-1)
2770      {
2771          deleteHC(&h.p, &h.ecart, &h.length,strat);
2772      }
2773      if (TEST_OPT_DEGBOUND
2774      && (((strat->honey) && (h.ecart+pFDeg(h.p)>Kstd1_deg))
2775        || ((!(strat->honey)) && (pFDeg(h.p)>Kstd1_deg))))
2776        pDelete(&h.p);
2777      else
2778      if (h.p!=NULL)
2779      {
2780        if (strat->sl==-1)
2781          pos =0;
2782        else
2783        {
2784          pos = posInS(strat->S,strat->sl,h.p);
2785        }
2786        strat->enterS(h,pos,strat);
2787        h.length = pLength(h.p);
2788        enterT(h,strat);
2789      }
2790    }
2791  }
2792  for (i=0; i<IDELEMS(P); i++)
2793  {
2794    if (P->m[i]!=NULL)
2795    {
2796      h.p=pCopy(P->m[i]);
2797      strat->initEcart(&h);
2798      h.length = pLength(h.p);
2799      if (TEST_OPT_INTSTRATEGY)
2800      {
2801        pCleardenom(h.p);
2802      }
2803      else
2804      {
2805        pNorm(h.p);
2806      }
2807      if(strat->sl>=0)
2808      {
2809        if (pOrdSgn==1)
2810        {
2811          h.p=redBba(h.p,strat->sl,strat);
2812          h.p=redtailBba(h.p,strat->sl,strat);
2813        }
2814        else
2815        {
2816          h.p=redMora(h.p,strat->sl,strat);
2817          strat->initEcart(&h);
2818        }
2819        if(h.p!=NULL)
2820        {
2821          if (TEST_OPT_INTSTRATEGY)
2822          {
2823            pCleardenom(h.p);
2824          }
2825          else
2826          {
2827            pNorm(h.p);
2828          }
2829          pos = posInS(strat->S,strat->sl,h.p);
2830          enterpairsSpecial(h.p,strat->sl,h.ecart,pos,strat);
2831          strat->enterS(h,pos,strat);
2832          enterT(h,strat);
2833        }
2834      }
2835      else
2836      {
2837        strat->enterS(h,0,strat);
2838        enterT(h,strat);
2839      }
2840    }
2841  }
2842#ifdef SDRING
2843  Free((ADDRESS)aug,augmax*sizeof(poly));
2844#endif
2845}
2846/*2
2847* reduces h using the set S
2848* procedure used in cancelunit1
2849*/
2850static poly redBba1 (poly h,int maxIndex,kStrategy strat)
2851{
2852  int j = 0;
2853
2854  while (j <= maxIndex)
2855  {
2856    if (pDivisibleBy(strat->S[j],h))
2857       return spSpolyRedNew(strat->S[j],h,strat->kNoether);
2858    else j++;
2859  }
2860  return h;
2861}
2862
2863/*2
2864*tests if p.p=monomial*unit and cancels the unit
2865*/
2866void cancelunit1 (LObject* p,int index,kStrategy strat )
2867{
2868  int k;
2869  poly r,h,h1,q;
2870
2871  if (!pIsVector((*p).p) && ((*p).ecart != 0))
2872  {
2873    k = 0;
2874    h1 = r = pCopy((*p).p);
2875    h =pNext(r);
2876    loop
2877    {
2878      if (h==NULL)
2879      {
2880        pDelete(&r);
2881        pDelete(&(pNext((*p).p)));
2882        (*p).ecart = 0;
2883        (*p).length = 1;
2884        return;
2885      }
2886      if (!pDivisibleBy(r,h))
2887      {
2888        q=redBba1(h,index ,strat);
2889        if (q != h)
2890        {
2891          k++;
2892          pDelete(&h);
2893          pNext(h1) = h = q;
2894        }
2895        else
2896        {
2897          pDelete(&r);
2898          return;
2899        }
2900      }
2901      else
2902      {
2903        h1 = h;
2904        pIter(h);
2905      }
2906      if (k > 10)
2907      {
2908        pDelete(&r);
2909        return;
2910      }
2911    }
2912  }
2913}
2914
2915/*2
2916* reduces h using the elements from Q in the set S
2917* procedure used in updateS
2918* must not be used for elements of Q or elements of an ideal !
2919*/
2920static poly redQ (poly h, int j, kStrategy strat)
2921{
2922  int start;
2923
2924  while ((j <= strat->sl) && (pGetComp(strat->S[j])!=0)) j++;
2925  start=j;
2926  while (j<=strat->sl)
2927  {
2928    if (pDivisibleBy(strat->S[j],h))
2929    {
2930      h = spSpolyRed(strat->S[j],h,strat->kNoether);
2931      if (h==NULL) return NULL;
2932      j = start;
2933    }
2934    else j++;
2935  }
2936  return h;
2937}
2938
2939/*2
2940* reduces h using the set S
2941* procedure used in updateS
2942*/
2943static poly redBba (poly h,int maxIndex,kStrategy strat)
2944{
2945  int j = 0;
2946
2947  while (j <= maxIndex)
2948  {
2949    if (pDivisibleBy(strat->S[j],h))
2950    {
2951      pTest(strat->S[j]);
2952      pTest(h);
2953      h = spSpolyRed(strat->S[j],h,strat->kNoether);
2954      if (h==NULL) return NULL;
2955      j = 0;
2956    }
2957    else j++;
2958  }
2959#ifdef KDEBUG
2960  pTest(h);
2961#endif
2962  return h;
2963}
2964
2965/*2
2966* reduces h using the set S
2967*e is the ecart of h
2968*procedure used in updateS
2969*/
2970static poly redMora (poly h,int maxIndex,kStrategy strat)
2971{
2972  int  j=0;
2973  int  e,l;
2974  poly h1;
2975
2976  if (maxIndex >= 0)
2977  {
2978    e = pLDeg(h,&l)-pFDeg(h);
2979    do
2980    {
2981      if (pDivisibleBy(strat->S[j],h)
2982      && ((e >= strat->ecartS[j]) || strat->kHEdgeFound))
2983      {
2984        h1 = spSpolyRedNew(strat->S[j],h,strat->kNoether);
2985        pDelete(&h);
2986        if (h1 == NULL) return NULL;
2987        h = h1;
2988        e = pLDeg(h,&l)-pFDeg(h);
2989        j = 0;
2990      }
2991      else j++;
2992    }
2993    while (j <= maxIndex);
2994  }
2995#ifdef KDEBUG
2996  pTest(h);
2997#endif
2998  return h;
2999}
3000
3001/*2
3002*updates S:
3003*the result is a set of polynomials which are in
3004*normalform with respect to S
3005*/
3006void updateS(BOOLEAN toT,kStrategy strat)
3007{
3008  LObject h;
3009  int i, suc=0;
3010  poly redSi=NULL;
3011#ifdef SDRING
3012  polyset aug=(polyset)Alloc(setmax*sizeof(poly));
3013  int augmax=setmax;
3014  int augl;
3015  int pos;
3016  BOOLEAN recursiv=FALSE;
3017#endif
3018
3019//Print("nach initS: updateS start mit sl=%d\n",(strat->sl));
3020//  for (i=0; i<=(strat->sl); i++)
3021//  {
3022//    Print("s%d:",i);
3023//    if (strat->fromQ!=NULL) Print("(Q:%d) ",strat->fromQ[i]);
3024//    pWrite(strat->S[i]);
3025//  }
3026  memset(&h,0,sizeof(h));
3027  if (pOrdSgn==1)
3028  {
3029    while (suc != -1)
3030    {
3031      i=suc+1;
3032      while (i<=strat->sl)
3033      {
3034        pTest(strat->S[i]);
3035        if (((strat->syzComp==0) || (pGetComp(strat->S[i])<=strat->syzComp))
3036        && ((strat->fromQ==NULL) || (strat->fromQ[i]==0)))
3037        {
3038          if (TEST_OPT_DEBUG)
3039          {
3040            PrintS("reduce:");
3041            wrp(strat->S[i]);
3042          }
3043          pDelete(&redSi);
3044          redSi = pHead(strat->S[i]);
3045          strat->S[i] = redBba(strat->S[i],i-1,strat);
3046          if ((strat->ak!=0)&&(strat->S[i]!=NULL)) strat->S[i]=redQ(strat->S[i],i+1,strat); /*reduce S[i] mod Q*/
3047          if (TEST_OPT_PROT && (pComp(redSi,strat->S[i])!=0))
3048          {
3049            if (strat->S[i]==NULL)
3050              PrintS("V");
3051            else
3052              PrintS("v");
3053            mflush();
3054          }
3055          if (strat->S[i]==NULL)
3056          {
3057            pDelete(&redSi);
3058            if (TEST_OPT_DEBUG) PrintS(" to 0");
3059            deleteInS(i,strat);
3060            i--;
3061          }
3062#ifdef SDRING
3063          else if ((pSDRING) && (pComp(redSi,strat->S[i])!=0))
3064          {
3065            pDelete(&redSi);
3066            redSi=strat->S[i];
3067            strat->S[i]=NULL;
3068            pTest(redSi);
3069            deleteInS(i,strat);
3070            suc=0;
3071            i=0;
3072            aug[0]=redSi;
3073            augl=0;
3074#ifdef SRING
3075            if (pSRING) psAug(pCopy(redSi),pOne(),&aug,&augl,&augmax);
3076#endif
3077#ifdef DRING
3078            if (pDRING) pdAug(pCopy(redSi),&aug,&augl,&augmax);
3079#endif
3080            redSi=NULL;
3081            if (augl>0) recursiv=TRUE;
3082            while (augl >= 0)
3083            {
3084              h.p=aug[augl];
3085              pTest(h.p);
3086              if (h.p!=NULL)
3087              {
3088                if (TEST_OPT_DEBUG)
3089                {
3090                  Print("new (aug %d) s:",augl);
3091                  wrp(h.p);
3092                  PrintLn();
3093                }
3094                if (TEST_OPT_INTSTRATEGY)
3095                {
3096                  //pContent(h.p);
3097                  pCleardenom(h.p);// also does a pContent
3098                }
3099                else
3100                {
3101                  pNorm(h.p);
3102                }
3103                strat->initEcart(&h);
3104                pos = posInS(strat->S,strat->sl,h.p);
3105                strat->enterS(h,pos,strat);
3106              }
3107              augl--;
3108            }
3109          }
3110#endif
3111          else
3112          {
3113            pDelete(&redSi);
3114            if (TEST_OPT_INTSTRATEGY)
3115            {
3116              //pContent(strat->S[i]);
3117              pCleardenom(strat->S[i]);// also does a pContent
3118            }
3119            else
3120            {
3121              pNorm(strat->S[i]);
3122            }
3123            if (TEST_OPT_DEBUG)
3124            {
3125              PrintS(" to ");
3126              wrp(strat->S[i]);
3127            }
3128          }
3129          if (TEST_OPT_DEBUG) PrintLn();
3130        }
3131        i++;
3132      }
3133      reorderS(&suc,strat);
3134    }
3135    if (toT)
3136    {
3137      for (i=0; i<=strat->sl; i++)
3138      {
3139        if (((strat->fromQ==NULL) || (strat->fromQ[i]==0))
3140#ifdef SDRING
3141        && (!pSDRING)
3142#endif
3143        )
3144          h.p = redtailBba(strat->S[i],i-1,strat);
3145        else
3146        {
3147          h.p = strat->S[i];
3148        }
3149        if (strat->honey)
3150        {
3151          strat->initEcart(&h);
3152          strat->ecartS[i] = h.ecart;
3153        }
3154        /*puts the elements of S also to T*/
3155        enterT(h,strat);
3156      }
3157    }
3158  }
3159  else
3160  {
3161    while (suc != -1)
3162    {
3163      i=suc+1;
3164      while (i<=strat->sl)
3165      {
3166        if (((strat->syzComp==0) || (pGetComp(strat->S[i])<=strat->syzComp))
3167        && ((strat->fromQ==NULL) || (strat->fromQ[i]==0)))
3168        {
3169          pDelete(&redSi);
3170          redSi=pHead((strat->S)[i]);
3171          (strat->S)[i] = redMora((strat->S)[i],i-1,strat);
3172          kTest(strat);
3173          if ((strat->S)[i]==NULL)
3174          {
3175            deleteInS(i,strat);
3176            i--;
3177          }
3178#ifdef SDRING
3179          else if ((pSDRING) && (pComp(redSi,(strat->S)[i])!=0))
3180          {
3181            pDelete(&redSi);
3182            redSi=(strat->S)[i];
3183            (strat->S)[i]=NULL;
3184            deleteInS(i,strat);
3185            i--;
3186            aug[0]=redSi;
3187            augl=0;
3188#ifdef SRING
3189            if (pSRING) psAug(pCopy(redSi),pOne(),&aug,&augl,&augmax);
3190#endif
3191#ifdef DRING
3192            if (pDRING) pdAug(pCopy(redSi),&aug,&augl,&augmax);
3193#endif
3194            redSi=NULL;
3195            if (augl>0) recursiv=TRUE;
3196            for (augl++;augl != 0;)
3197            {
3198              h.p=aug[--augl];
3199              if (TEST_OPT_DEBUG)
3200              {
3201                PrintS("new (aug) s:");
3202                wrp(h.p);
3203                PrintLn();
3204              }
3205              if (!TEST_OPT_INTSTRATEGY)
3206                pNorm(h.p);
3207              else
3208              {
3209                //pContent(h.p);
3210                pCleardenom(h.p);// also does a pContent
3211              }
3212              strat->initEcart(&h);
3213              pos = posInS(strat->S,strat->sl,h.p);
3214              strat->enterS(h,pos,strat);
3215            }
3216          }
3217#endif
3218          else if (TEST_OPT_INTSTRATEGY)
3219          {
3220            pDelete(&redSi);
3221            //pContent(strat->S[i]);
3222            pCleardenom(strat->S[i]);// also does a pContent
3223            h.p = strat->S[i];
3224            strat->initEcart(&h);
3225            strat->ecartS[i] = h.ecart;
3226          }
3227          else
3228          {
3229            pDelete(&redSi);
3230            pNorm(strat->S[i]);
3231            h.p = strat->S[i];
3232            strat->initEcart(&h);
3233            strat->ecartS[i] = h.ecart;
3234          }
3235        }
3236        i++;
3237      }
3238#ifdef KDEBUG
3239      kTest(strat);
3240#endif
3241      reorderS(&suc,strat);
3242      if (h.p!=NULL)
3243      {
3244        if (!strat->kHEdgeFound)
3245        {
3246          /*strat->kHEdgeFound =*/ HEckeTest(h.p,strat);
3247        }
3248        if (strat->kHEdgeFound)
3249          newHEdge(strat->S,strat->ak,strat);
3250      }
3251    }
3252    for (i=0; i<=strat->sl; i++)
3253    {
3254      if (((strat->fromQ==NULL) || (strat->fromQ[i]==0))
3255#ifdef SDRING
3256      && (!pSDRING)
3257#endif
3258      )
3259      {
3260        strat->S[i] = h.p = redtail(strat->S[i],strat->sl,strat);
3261        strat->initEcart(&h);
3262        strat->ecartS[i] = h.ecart;
3263      }
3264      else
3265      {
3266        h.p = strat->S[i];
3267        h.ecart=strat->ecartS[i];
3268      }
3269      if ((strat->fromQ==NULL) || (strat->fromQ[i]==0))
3270        cancelunit1(&h,strat->sl,strat);
3271      h.length = pLength(h.p);
3272      /*puts the elements of S also to T*/
3273      enterT(h,strat);
3274    }
3275  }
3276  if (redSi!=NULL) pDelete1(&redSi);
3277#ifdef SDRING
3278  Free((ADDRESS)aug,augmax*sizeof(poly));
3279  if (recursiv) updateS(FALSE,strat);
3280#endif
3281//  for(augl=0;augl<=strat->sl;augl++)
3282//    pTest(strat->S[augl]);
3283//Print("nach  updateS start mit sl=%d\n",strat->sl);
3284//  for (i=0; i<=strat->sl; i++)
3285//  {
3286//    Print("s%d:",i);
3287//    if (strat->fromQ) Print("(Q:%d) ",strat->fromQ[i]);
3288//    pWrite(strat->S[i]);
3289//  }
3290#ifdef KDEBUG
3291  kTest(strat);
3292#endif
3293}
3294
3295/*2
3296* -puts p to the standardbasis s at position at
3297* -saves the result in S
3298*/
3299void enterSBba (LObject p,int atS,kStrategy strat)
3300{
3301  int i;
3302
3303#ifdef SDRING
3304  if (pSDRING
3305  && (atS<=strat->sl)
3306  && pComparePolys(p.p,strat->S[atS]))
3307  {
3308    if (TEST_OPT_PROT)
3309      PrintS("m");
3310    p.p=NULL;
3311    return;
3312  }
3313  if (pSDRING
3314  && (atS<strat->sl)
3315  && pComparePolys(p.p,strat->S[atS+1]))
3316  {
3317    if (TEST_OPT_PROT)
3318      PrintS("m");
3319    p.p=NULL;
3320    return;
3321  }
3322  if (pSDRING
3323  && (atS>0)
3324  && pComparePolys(p.p,strat->S[atS-1]))
3325  {
3326    if (TEST_OPT_PROT)
3327      PrintS("m");
3328    p.p=NULL;
3329    return;
3330  }
3331#endif
3332  strat->news = TRUE;
3333  /*- puts p to the standardbasis s at position at -*/
3334  if (strat->sl == IDELEMS(strat->Shdl)-1)
3335  {
3336    strat->ecartS = (intset)ReAlloc(strat->ecartS,IDELEMS(strat->Shdl)*sizeof(int),
3337                                    (IDELEMS(strat->Shdl)+setmax)*sizeof(int));
3338    if (strat->fromQ!=NULL)
3339    {
3340      strat->fromQ = (intset)ReAlloc(strat->fromQ,IDELEMS(strat->Shdl)*sizeof(int),
3341                                    (IDELEMS(strat->Shdl)+setmax)*sizeof(int));
3342    }
3343    pEnlargeSet(&strat->S,IDELEMS(strat->Shdl),setmax);
3344    IDELEMS(strat->Shdl)+=setmax;
3345    strat->Shdl->m=strat->S;
3346  }
3347  for (i=strat->sl+1; i>=atS+1; i--)
3348  {
3349    strat->S[i] = strat->S[i-1];
3350    if (strat->honey) strat->ecartS[i] = strat->ecartS[i-1];
3351  }
3352  if (strat->fromQ!=NULL)
3353  {
3354    for (i=strat->sl+1; i>=atS+1; i--)
3355    {
3356      strat->fromQ[i] = strat->fromQ[i-1];
3357    }
3358    strat->fromQ[atS]=0;
3359  }
3360  /*- save result -*/
3361  strat->S[atS] = p.p;
3362  if (strat->honey) strat->ecartS[atS] = p.ecart;
3363  strat->sl++;
3364}
3365
3366/*2
3367* puts p to the set T at position atT
3368*/
3369void enterT (LObject p,kStrategy strat)
3370{
3371  int i,atT;
3372
3373#ifdef KDEBUG
3374  pTest(p.p);
3375  for (i=0; i<=strat->tl ;i++)
3376  {
3377    pTest(strat->T[i].p);
3378  }
3379#endif
3380  strat->newt = TRUE;
3381  if (strat->tl >= 0)
3382  {
3383    /*- puts p to the standardbasis s at position atT -*/
3384    atT = strat->posInT(strat->T,strat->tl,p);
3385//    if ((atT<=strat->tl)
3386//    && (p.p==strat->T[atT].p))
3387//    {
3388//      PrintS("?");
3389//      return;
3390//    }
3391//    if ((atT<strat->tl)
3392//    && (p.p==strat->T[atT+1].p))
3393//    {
3394//      PrintS("?");
3395//      return;
3396//    }
3397//    if ((atT>0)
3398//    && (p.p==strat->T[atT-1].p))
3399//    {
3400//      PrintS("?");
3401//      return;
3402//    }
3403    if (strat->tl == strat->tmax-1) enlargeT(&strat->T,&strat->tmax,setmax);
3404    for (i=strat->tl+1; i>=atT+1; i--)
3405      strat->T[i] = strat->T[i-1];
3406  }
3407  else atT = 0;
3408  strat->T[atT].p = p.p;
3409  strat->T[atT].ecart = p.ecart;
3410  strat->T[atT].length = p.length;
3411  strat->tl++;
3412}
3413
3414/*2
3415* puts p to the set T at position atT
3416*/
3417void enterTBba (LObject p, int atT,kStrategy strat)
3418{
3419  int i;
3420
3421  strat->newt = TRUE;
3422  if (strat->tl == strat->tmax-1) enlargeT(&strat->T,&strat->tmax,setmax);
3423  for (i=strat->tl+1; i>=atT+1; i--)
3424    strat->T[i] = strat->T[i-1];
3425  strat->T[atT].p = p.p;
3426  if (strat->honey)
3427    strat->T[atT].ecart = p.ecart;
3428  if (TEST_OPT_INTSTRATEGY)
3429    strat->T[atT].length = p.length;
3430  strat->tl++;
3431//    for (i=0; i<=strat->tl ;i++)
3432//    {
3433//      pTest(strat->T[i].p);
3434//    }
3435}
3436
3437void initHilbCrit(ideal F, ideal Q, intvec **hilb,kStrategy strat)
3438{
3439  if (strat->homog!=isHomog)
3440  {
3441    *hilb=NULL;
3442  }
3443}
3444
3445void initBuchMoraCrit(kStrategy strat)
3446{
3447  strat->sugarCrit =        TEST_OPT_SUGARCRIT;
3448  strat->Gebauer =          BTEST1(2) || strat->homog || strat->sugarCrit;
3449  strat->honey =            !strat->homog || strat->sugarCrit || TEST_OPT_WEIGHTM;
3450  if (TEST_OPT_NOT_SUGAR) strat->honey = FALSE;
3451  strat->pairtest = NULL;
3452  /* alway use tailreduction, except:
3453  * - in local rings, - in lex order case, -in ring over extensions */
3454  strat->noTailReduction = !TEST_OPT_REDTAIL;
3455  if (TEST_OPT_DEBUG)
3456  {
3457    if (strat->homog) PrintS("ideal/module is homogeneous\n");
3458    else              PrintS("ideal/module is not homogeneous\n");
3459  }
3460}
3461
3462void initBuchMoraPos (kStrategy strat)
3463{
3464  if (pOrdSgn==1)
3465  {
3466    if (strat->honey)
3467    {
3468      strat->posInL = posInL15;
3469      strat->posInT = posInT15;
3470    }
3471    else if (pLexOrder && !TEST_OPT_INTSTRATEGY)
3472    {
3473      strat->posInL = posInL11;
3474      strat->posInT = posInT11;
3475    }
3476    else if (TEST_OPT_INTSTRATEGY)
3477    {
3478      strat->posInL = posInL11;
3479      strat->posInT = posInT11;
3480    }
3481    else
3482    {
3483      strat->posInL = posInL0;
3484      strat->posInT = posInT0;
3485    }
3486    //if (strat->minim>0) strat->posInL =posInLSpecial;
3487  }
3488  else
3489  {
3490    if (strat->homog)
3491    {
3492      strat->posInL = posInL11;
3493      strat->posInT = posInT11;
3494    }
3495    else
3496    {
3497      if ((currRing->order[0]==ringorder_c)
3498      ||(currRing->order[0]==ringorder_C))
3499      {
3500        strat->posInL = posInL17_c;
3501        strat->posInT = posInT17_c;
3502      }
3503      else
3504      {
3505        strat->posInL = posInL17;
3506        strat->posInT = posInT17;
3507      }
3508    }
3509  }
3510  if (strat->minim>0) strat->posInL =posInLSpecial;
3511  // for further tests only
3512  if ((BTEST1(11)) || (BTEST1(12)))
3513    strat->posInL = posInL11;
3514  else if ((BTEST1(13)) || (BTEST1(14)))
3515    strat->posInL = posInL13;
3516  else if ((BTEST1(15)) || (BTEST1(16)))
3517    strat->posInL = posInL15;
3518  else if ((BTEST1(17)) || (BTEST1(18)))
3519    strat->posInL = posInL17;
3520  if (BTEST1(11))
3521    strat->posInT = posInT11;
3522  else if (BTEST1(13))
3523    strat->posInT = posInT13;
3524  else if (BTEST1(15))
3525    strat->posInT = posInT15;
3526  else if ((BTEST1(17)))
3527    strat->posInT = posInT17;
3528  else if ((BTEST1(19)))
3529    strat->posInT = posInT19;
3530  else if (BTEST1(12) || BTEST1(14) || BTEST1(16) || BTEST1(18))
3531    strat->posInT = posInT1;
3532}
3533
3534void initBuchMora (ideal F,ideal Q,kStrategy strat)
3535{
3536  strat->interpt = BTEST1(OPT_INTERRUPT);
3537  strat->ak = idRankFreeModule(F);
3538  strat->kHEdge=NULL;
3539  if (pOrdSgn==1) strat->kHEdgeFound=FALSE;
3540  /*- creating temp data structures------------------- -*/
3541  strat->cp = 0;
3542  strat->c3 = 0;
3543  strat->tail = pInit();
3544  /*- set s -*/
3545  strat->sl = -1;
3546  /*- set L -*/
3547  strat->Lmax = setmax;
3548  strat->Ll = -1;
3549  strat->L = initL();
3550  /*- set B -*/
3551  strat->Bmax = setmax;
3552  strat->Bl = -1;
3553  strat->B = initL();
3554  /*- set T -*/
3555  strat->tl = -1;
3556  strat->tmax = setmax;
3557  strat->T = initT();
3558  /*- init local data struct.---------------------------------------- -*/
3559  strat->P.ecart=0;
3560  strat->P.length=0;
3561  if (pOrdSgn==-1)
3562  {
3563    if (strat->kHEdge!=NULL) pGetComp(strat->kHEdge)=strat->ak;
3564    if (strat->kNoether!=NULL) pGetComp(strat->kNoether)=strat->ak;
3565  }
3566  if(TEST_OPT_SB_1)
3567  {
3568    int i;
3569    ideal P=idInit(IDELEMS(F)-strat->newIdeal,F->rank);
3570    for (i=strat->newIdeal;i<IDELEMS(F);i++)
3571    {
3572      P->m[i-strat->newIdeal] = F->m[i];
3573      F->m[i] = NULL;
3574    }
3575    initSSpecial(F,Q,P,strat);
3576    for (i=strat->newIdeal;i<IDELEMS(F);i++)
3577    {
3578      F->m[i] = P->m[i-strat->newIdeal];
3579      P->m[i-strat->newIdeal] = NULL;
3580    }
3581    idDelete(&P);
3582  }
3583  else
3584  {
3585    /*Shdl=*/initSL(F, Q,strat); /*sets also S, ecartS, fromQ */
3586    // /*Shdl=*/initS(F, Q,strat); /*sets also S, ecartS, fromQ */
3587  }
3588  strat->kIdeal = NULL;
3589  strat->fromT = FALSE;
3590  strat->noTailReduction = !TEST_OPT_REDTAIL;
3591  if(!TEST_OPT_SB_1)
3592  {
3593    updateS(TRUE,strat);
3594    pairs(strat);
3595  }
3596  if (strat->fromQ!=NULL) Free((ADDRESS)strat->fromQ,IDELEMS(strat->Shdl)*sizeof(int));
3597  strat->fromQ=NULL;
3598}
3599
3600void exitBuchMora (kStrategy strat)
3601{
3602  /*- release temp data -*/
3603  cleanT(strat);
3604  Free((ADDRESS)strat->T,(strat->tmax)*sizeof(TObject));
3605  Free((ADDRESS)strat->ecartS,IDELEMS(strat->Shdl)*sizeof(int));
3606  /*- set L: should be empty -*/
3607  Free((ADDRESS)strat->L,(strat->Lmax)*sizeof(LObject));
3608  /*- set B: should be empty -*/
3609  Free((ADDRESS)strat->B,(strat->Bmax)*sizeof(LObject));
3610  pDelete1(&strat->tail);
3611  strat->syzComp=0;
3612  if (strat->kIdeal!=NULL)
3613  {
3614    Free((ADDRESS)strat->kIdeal,sizeof(sleftv));
3615    strat->kIdeal=NULL;
3616  }
3617}
3618
3619/*2
3620* in the case of a standardbase of a module over a qring:
3621* replace polynomials in i by ak vectors,
3622* (the polynomial * unit vectors gen(1)..gen(ak)
3623* in every case (also for ideals:)
3624* deletes divisible vectors/polynomials
3625*/
3626void updateResult(ideal r,ideal Q,kStrategy strat)
3627{
3628  int l;
3629  if (strat->ak>0)
3630  {
3631    for (l=IDELEMS(r)-1;l>=0;l--)
3632    {
3633      if ((r->m[l]!=NULL) && (pGetComp(r->m[l])==0))
3634      {
3635        pDelete(&r->m[l]); // and set it to NULL
3636      }
3637    }
3638  }
3639  else
3640  {
3641    int q;
3642    poly p;
3643    for (l=IDELEMS(r)-1;l>=0;l--)
3644    {
3645      if (r->m[l]!=NULL)
3646      {
3647        for(q=IDELEMS(Q)-1; q>=0;q--)
3648        {
3649          if ((Q->m[q]!=NULL)
3650          &&(pEqual(r->m[l],Q->m[q])))
3651          {
3652            if (TEST_OPT_REDSB)
3653            {
3654              p=r->m[l];
3655              r->m[l]=kNF(Q,NULL,p);
3656              pDelete(&p);
3657            }
3658            else
3659            {
3660              pDelete(&r->m[l]); // and set it to NULL
3661            }
3662            break;
3663          }
3664        }
3665      }
3666    }
3667  }
3668  idSkipZeroes(r);
3669}
3670
3671void completeReduce (kStrategy strat)
3672{
3673  int i;
3674
3675  strat->noTailReduction = FALSE;
3676  if (TEST_OPT_PROT)
3677  {
3678    PrintLn();
3679    if (timerv) writeTime("standard base computed:");
3680  }
3681  if (TEST_OPT_PROT)
3682  {
3683    Print("(S:%d)",strat->sl);mflush();
3684  }
3685  if(pOrdSgn==1)
3686  {
3687    for (i=strat->sl; i>0; i--)
3688    {
3689      //if (strat->interpt) test_int_std(strat->kIdeal);
3690      strat->S[i] = redtailBba(strat->S[i],i-1,strat);
3691      if (TEST_OPT_INTSTRATEGY)
3692      {
3693        pCleardenom(strat->S[i]);
3694      }
3695      if (TEST_OPT_PROT)
3696      {
3697        PrintS("-");mflush();
3698      }
3699    }
3700  }
3701  else
3702  {
3703    for (i=strat->sl; i>=0; i--)
3704    {
3705      //if (strat->interpt) test_int_std(strat->kIdeal);
3706      strat->S[i] = redtail(strat->S[i],strat->sl,strat);
3707      if (TEST_OPT_INTSTRATEGY)
3708      {
3709        pCleardenom(strat->S[i]);
3710      }
3711      if (TEST_OPT_PROT)
3712      {
3713        PrintS("-");mflush();
3714      }
3715    }
3716  }
3717}
3718
3719/*2
3720* computes the new strat->kHEdge and the new pNoether,
3721* returns TRUE, if pNoether has changed
3722*/
3723BOOLEAN newHEdge(polyset S, int ak,kStrategy strat)
3724{
3725  int i,j;
3726  poly newNoether;
3727
3728  scComputeHC(strat->Shdl,ak,strat->kHEdge);
3729  /* compare old and new noether*/
3730  newNoether = pHead0(strat->kHEdge);
3731  j = pFDeg(newNoether);
3732  {
3733    short * e = (short *)Alloc((pVariables+1)*sizeof(short));
3734    pGetExpV(newNoether,e);
3735    for (i=1; i<=pVariables; i++)
3736    {
3737      if (e[i] > 0) (e[i])--;
3738    }
3739    pSetExpV(newNoether,e);
3740    Free((ADDRESS)e,(pVariables+1)*sizeof(short));
3741  }
3742  if (j < strat->HCord) /*- statistics -*/
3743  {
3744    if (TEST_OPT_PROT)
3745    {
3746      Print("H(%d)",j);
3747      mflush();
3748    }
3749    strat->HCord=j;
3750    if (TEST_OPT_DEBUG)
3751    {
3752      Print("H(%d):",pFDeg(strat->kHEdge));
3753      wrp(strat->kHEdge);
3754      PrintLn();
3755    }
3756  }
3757  if (pComp(strat->kNoether,newNoether)!=1)
3758  {
3759    pDelete(&strat->kNoether);
3760    strat->kNoether=newNoether;
3761    return TRUE;
3762  }
3763  pFree1(newNoether);
3764  return FALSE;
3765}
Note: See TracBrowser for help on using the repository browser.