source: git/Singular/kutil.cc @ 1caa72

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