source: git/Singular/kutil.cc @ 46feb1

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