source: git/Singular/kutil.cc @ c4bbf1f

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