source: git/Singular/kutil.cc @ fdc537

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