source: git/kernel/kutil.cc @ 182e83

spielwiese
Last change on this file since 182e83 was 182e83, checked in by Hans Schönemann <hannes@…>, 15 years ago
*hannes: syntax fix git-svn-id: file:///usr/local/Singular/svn/trunk@11447 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 184.5 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: kutil.cc,v 1.126 2009-02-23 11:33:41 Singular Exp $ */
5/*
6* ABSTRACT: kernel: utils for kStd
7*/
8
9// #define PDEBUG 2
10// #define PDIV_DEBUG
11#define KUTIL_CC
12#include <stdlib.h>
13#include <string.h>
14#include "mod2.h"
15#include <mylimits.h>
16#include "structs.h"
17#include "gring.h"
18#include "sca.h"
19#ifdef KDEBUG
20#undef KDEBUG
21#define KDEBUG 2
22#endif
23
24#ifdef HAVE_RING2TOM
25#include "ideals.h"
26#endif
27
28// define if enterL, enterT should use memmove instead of doing it manually
29// on topgun, this is slightly faster (see monodromy_l.tst, homog_gonnet.sing)
30#ifndef SunOS_4
31#define ENTER_USE_MEMMOVE
32#endif
33
34// define, if the my_memmove inlines should be used instead of
35// system memmove -- it does not seem to pay off, though
36// #define ENTER_USE_MYMEMMOVE
37
38#include "kutil.h"
39#include "kbuckets.h"
40#include "febase.h"
41#include "omalloc.h"
42#include "numbers.h"
43#include "polys.h"
44#include "ring.h"
45#include "ideals.h"
46#include "timer.h"
47//#include "cntrlc.h"
48#include "stairc.h"
49#include "kstd1.h"
50#include "pShallowCopyDelete.h"
51
52/* shiftgb stuff */
53#include "shiftgb.h"
54#include "prCopy.h"
55
56#ifdef KDEBUG
57#undef KDEBUG
58#define KDEBUG 2
59#endif
60
61
62#ifdef ENTER_USE_MYMEMMOVE
63inline void _my_memmove_d_gt_s(unsigned long* d, unsigned long* s, long l)
64{
65  register unsigned long* _dl = (unsigned long*) d;
66  register unsigned long* _sl = (unsigned long*) s;
67  register long _i = l - 1;
68
69  do
70  {
71    _dl[_i] = _sl[_i];
72    _i--;
73  }
74  while (_i >= 0);
75}
76
77inline void _my_memmove_d_lt_s(unsigned long* d, unsigned long* s, long l)
78{
79  register long _ll = l;
80  register unsigned long* _dl = (unsigned long*) d;
81  register unsigned long* _sl = (unsigned long*) s;
82  register long _i = 0;
83
84  do
85  {
86    _dl[_i] = _sl[_i];
87    _i++;
88  }
89  while (_i < _ll);
90}
91
92inline void _my_memmove(void* d, void* s, long l)
93{
94  unsigned long _d = (unsigned long) d;
95  unsigned long _s = (unsigned long) s;
96  unsigned long _l = ((l) + SIZEOF_LONG - 1) >> LOG_SIZEOF_LONG;
97
98  if (_d > _s) _my_memmove_d_gt_s(_d, _s, _l);
99  else _my_memmove_d_lt_s(_d, _s, _l);
100}
101
102#undef memmove
103#define memmove(d,s,l) _my_memmove(d, s, l)
104#endif
105
106static poly redMora (poly h,int maxIndex,kStrategy strat);
107static poly redBba (poly h,int maxIndex,kStrategy strat);
108
109#ifdef HAVE_RINGS
110#define pDivComp_EQUAL 2
111#define pDivComp_LESS 1
112#define pDivComp_GREATER -1
113#define pDivComp_INCOMP 0
114/* Checks the relation of LM(p) and LM(q)
115     LM(p) = LM(q) => return pDivComp_EQUAL
116     LM(p) | LM(q) => return pDivComp_LESS
117     LM(q) | LM(p) => return pDivComp_GREATER
118     else return pDivComp_INCOMP */
119static inline int pDivCompRing(poly p, poly q)
120{
121  if (pGetComp(p) == pGetComp(q))
122  {
123    BOOLEAN a=FALSE, b=FALSE;
124    int i;
125    unsigned long la, lb;
126    unsigned long divmask = currRing->divmask;
127    for (i=0; i<currRing->VarL_Size; i++)
128    {
129      la = p->exp[currRing->VarL_Offset[i]];
130      lb = q->exp[currRing->VarL_Offset[i]];
131      if (la != lb)
132      {
133        if (la < lb)
134        {
135          if (b) return pDivComp_INCOMP;
136          if (((la & divmask) ^ (lb & divmask)) != ((lb - la) & divmask))
137            return pDivComp_INCOMP;
138          a = TRUE;
139        }
140        else
141        {
142          if (a) return pDivComp_INCOMP;
143          if (((la & divmask) ^ (lb & divmask)) != ((la - lb) & divmask))
144            return pDivComp_INCOMP;
145          b = TRUE;
146        }
147      }
148    }
149    if (a) return pDivComp_LESS;
150    if (b) return pDivComp_GREATER;
151    if (!a & !b) return pDivComp_EQUAL;
152  }
153  return 0;
154}
155#endif
156
157static inline int pDivComp(poly p, poly q)
158{
159  if (pGetComp(p) == pGetComp(q))
160  {
161#ifdef HAVE_PLURAL
162    if (rIsRatGRing(currRing))
163    {
164      if (_p_LmDivisibleByPart(p,currRing,
165                           q,currRing,
166                           currRing->real_var_start, currRing->real_var_end))
167        return 0; 
168      return pLmCmp(q,p); // ONLY FOR GLOBAL ORDER!
169    }
170#endif
171    BOOLEAN a=FALSE, b=FALSE;
172    int i;
173    unsigned long la, lb;
174    unsigned long divmask = currRing->divmask;
175    for (i=0; i<currRing->VarL_Size; i++)
176    {
177      la = p->exp[currRing->VarL_Offset[i]];
178      lb = q->exp[currRing->VarL_Offset[i]];
179      if (la != lb)
180      {
181        if (la < lb)
182        {
183          if (b) return 0;
184          if (((la & divmask) ^ (lb & divmask)) != ((lb - la) & divmask))
185            return 0;
186          a = TRUE;
187        }
188        else
189        {
190          if (a) return 0;
191          if (((la & divmask) ^ (lb & divmask)) != ((la - lb) & divmask))
192            return 0;
193          b = TRUE;
194        }
195      }
196    }
197    if (a) { /*assume(pLmCmp(q,p)==1);*/ return 1; }
198    if (b) { /*assume(pLmCmp(q,p)==-1);*/return -1; }
199    /*assume(pLmCmp(q,p)==0);*/
200  }
201  return 0;
202}
203
204
205BITSET  test=(BITSET)0;
206int     HCord;
207int     Kstd1_deg;
208int     mu=32000;
209
210/*2
211*deletes higher monomial of p, re-compute ecart and length
212*works only for orderings with ecart =pFDeg(end)-pFDeg(start)
213*/
214void deleteHC(LObject *L, kStrategy strat, BOOLEAN fromNext)
215{
216  if (strat->kHEdgeFound)
217  {
218    kTest_L(L);
219    poly p1;
220    poly p = L->GetLmTailRing();
221    int l = 1;
222    kBucket_pt bucket = NULL;
223    if (L->bucket != NULL)
224    {
225      kBucketClear(L->bucket, &pNext(p), &L->pLength);
226      L->pLength++;
227      bucket = L->bucket;
228      L->bucket = NULL;
229      L->last = NULL;
230    }
231
232    if (!fromNext && p_Cmp(p,strat->kNoetherTail(), L->tailRing) == -1)
233    {
234      L->Delete();
235      L->Clear();
236      L->ecart = -1;
237      if (bucket != NULL) kBucketDestroy(&bucket);
238      return;
239    }
240    p1 = p;
241    while (pNext(p1)!=NULL)
242    {
243      if (p_LmCmp(pNext(p1), strat->kNoetherTail(), L->tailRing) == -1)
244      {
245        L->last = p1;
246        p_Delete(&pNext(p1), L->tailRing);
247        if (p1 == p)
248        {
249          if (L->t_p != NULL)
250          {
251            assume(L->p != NULL && p == L->t_p);
252            pNext(L->p) = NULL;
253          }
254          L->max  = NULL;
255        }
256        else if (fromNext)
257          L->max  = p_GetMaxExpP(pNext(L->p), L->tailRing ); // p1;
258        //if (L->pLength != 0)
259        L->pLength = l;
260        // Hmmm when called from updateT, then only
261        // reset ecart when cut
262        if (fromNext)
263          L->ecart = L->pLDeg() - L->GetpFDeg();
264        break;
265      }
266      l++;
267      pIter(p1);
268    }
269    if (! fromNext)
270    {
271      L->SetpFDeg();
272      L->ecart = L->pLDeg(strat->LDegLast) - L->GetpFDeg();
273    }
274    if (bucket != NULL)
275    {
276      if (L->pLength > 1)
277      {
278        kBucketInit(bucket, pNext(p), L->pLength - 1);
279        pNext(p) = NULL;
280        if (L->t_p != NULL) pNext(L->t_p) = NULL;
281        L->pLength = 0;
282        L->bucket = bucket;
283        L->last = NULL;
284      }
285      else
286        kBucketDestroy(&bucket);
287    }
288    kTest_L(L);
289  }
290}
291
292void deleteHC(poly* p, int* e, int* l,kStrategy strat)
293{
294  LObject L(*p, currRing, strat->tailRing);
295
296  deleteHC(&L, strat);
297  *p = L.p;
298  *e = L.ecart;
299  *l = L.length;
300  if (L.t_p != NULL) p_LmFree(L.t_p, strat->tailRing);
301}
302
303/*2
304*tests if p.p=monomial*unit and cancels the unit
305*/
306void cancelunit (LObject* L,BOOLEAN inNF)
307{
308  int  i;
309  poly h;
310
311  if(rHasGlobalOrdering_currRing()) return;
312  if(TEST_OPT_CANCELUNIT) return;
313
314  ring r = L->tailRing;
315  poly p = L->GetLmTailRing();
316
317#ifdef HAVE_RINGS_LOC
318  // Leading coef have to be a unit
319  if ( !(nIsUnit(p_GetCoeff(p, r))) ) return;
320#endif
321
322  if(p_GetComp(p, r) != 0 && !p_OneComp(p, r)) return;
323
324  if (L->ecart != 0)
325  {
326//    for(i=r->N;i>0;i--)
327//    {
328//      if ((p_GetExp(p,i,r)>0) && (rIsPolyVar(i, r)==TRUE)) return;
329//    }
330    h = pNext(p);
331    loop
332    {
333      if (h==NULL)
334      {
335        p_Delete(&pNext(p), r);
336        if (!inNF)
337        {
338          number eins=nInit(1);
339          if (L->p != NULL)  pSetCoeff(L->p,eins);
340          else if (L->t_p != NULL) nDelete(&pGetCoeff(L->t_p));
341          if (L->t_p != NULL) pSetCoeff0(L->t_p,eins);
342        }
343        L->ecart = 0;
344        L->length = 1;
345        //if (L->pLength > 0)
346        L->pLength = 1;
347        if (L->last != NULL) L->last = p;
348
349        if (L->t_p != NULL && pNext(L->t_p) != NULL)
350          pNext(L->t_p) = NULL;
351        if (L->p != NULL && pNext(L->p) != NULL)
352          pNext(L->p) = NULL;
353        return;
354      }
355      i = 0;
356      loop
357      {
358        i++;
359        if (p_GetExp(p,i,r) > p_GetExp(h,i,r)) return ; // does not divide
360        if (i == r->N) break; // does divide, try next monom
361      }
362      pIter(h);
363    }
364  }
365}
366
367/*2
368*pp is the new element in s
369*returns TRUE (in strat->kHEdgeFound) if
370*-HEcke is allowed
371*-we are in the last componente of the vector
372*-on all axis are monomials (all elements in NotUsedAxis are FALSE)
373*returns FALSE for pLexOrderings,
374*assumes in module case an ordering of type c* !!
375* HEckeTest is only called with strat->kHEdgeFound==FALSE !
376*/
377void HEckeTest (poly pp,kStrategy strat)
378{
379  int   j,k,p;
380
381  strat->kHEdgeFound=FALSE;
382  if (pLexOrder || currRing->MixedOrder)
383  {
384    return;
385  }
386  if (strat->ak > 1)           /*we are in the module case*/
387  {
388    return; // until ....
389    //if (!pVectorOut)     /*pVectorOut <=> order = c,* */
390    //  return FALSE;
391    //if (pGetComp(pp) < strat->ak) /* ak is the number of the last component */
392    //  return FALSE;
393  }
394  k = 0;
395  p=pIsPurePower(pp);
396  if (p!=0) strat->NotUsedAxis[p] = FALSE;
397  /*- the leading term of pp is a power of the p-th variable -*/
398  for (j=pVariables;j>0; j--)
399  {
400    if (strat->NotUsedAxis[j])
401    {
402      return;
403    }
404  }
405  strat->kHEdgeFound=TRUE;
406}
407
408/*2
409*utilities for TSet, LSet
410*/
411inline static intset initec (const int maxnr)
412{
413  return (intset)omAlloc(maxnr*sizeof(int));
414}
415
416inline static unsigned long* initsevS (const int maxnr)
417{
418  return (unsigned long*)omAlloc0(maxnr*sizeof(unsigned long));
419}
420inline static int* initS_2_R (const int maxnr)
421{
422  return (int*)omAlloc0(maxnr*sizeof(int));
423}
424
425static inline void enlargeT (TSet &T, TObject** &R, unsigned long* &sevT,
426                             int &length, const int incr)
427{
428  assume(T!=NULL);
429  assume(sevT!=NULL);
430  assume(R!=NULL);
431  assume((length+incr) > 0);
432
433  int i;
434  T = (TSet)omRealloc0Size(T, length*sizeof(TObject),
435                           (length+incr)*sizeof(TObject));
436
437  sevT = (unsigned long*) omReallocSize(sevT, length*sizeof(long*),
438                           (length+incr)*sizeof(long*));
439
440  R = (TObject**)omRealloc0Size(R,length*sizeof(TObject*),
441                                (length+incr)*sizeof(TObject*));
442  for (i=length-1;i>=0;i--) R[T[i].i_r] = &(T[i]);
443  length += incr;
444}
445
446void cleanT (kStrategy strat)
447{
448  int i,j;
449  poly  p;
450  assume(currRing == strat->tailRing || strat->tailRing != NULL);
451
452  pShallowCopyDeleteProc p_shallow_copy_delete =
453    (strat->tailRing != currRing ?
454     pGetShallowCopyDeleteProc(strat->tailRing, currRing) :
455     NULL);
456
457  for (j=0; j<=strat->tl; j++)
458  {
459    p = strat->T[j].p;
460    strat->T[j].p=NULL;
461    if (strat->T[j].max != NULL)
462    {
463      p_LmFree(strat->T[j].max, strat->tailRing);
464    }
465    i = -1;
466    loop
467    {
468      i++;
469      if (i>strat->sl)
470      {
471        if (strat->T[j].t_p != NULL)
472        {
473          p_Delete(&(strat->T[j].t_p), strat->tailRing);
474          p_LmFree(p, currRing);
475        }
476        else
477          pDelete(&p);
478        break;
479      }
480      if (p == strat->S[i])
481      {
482        if (strat->T[j].t_p != NULL)
483        {
484          assume(p_shallow_copy_delete != NULL);
485          pNext(p) = p_shallow_copy_delete(pNext(p),strat->tailRing,currRing,
486                                           currRing->PolyBin);
487          p_LmFree(strat->T[j].t_p, strat->tailRing);
488        }
489        break;
490      }
491    }
492  }
493  strat->tl=-1;
494}
495
496//LSet initL ()
497//{
498//  int i;
499//  LSet l = (LSet)omAlloc(setmaxL*sizeof(LObject));
500//  return l;
501//}
502
503static inline void enlargeL (LSet* L,int* length,const int incr)
504{
505  assume((*L)!=NULL);
506  assume((length+incr)>0);
507
508  *L = (LSet)omReallocSize((*L),(*length)*sizeof(LObject),
509                                   ((*length)+incr)*sizeof(LObject));
510  (*length) += incr;
511}
512
513void initPairtest(kStrategy strat)
514{
515  strat->pairtest = (BOOLEAN *)omAlloc0((strat->sl+2)*sizeof(BOOLEAN));
516}
517
518/*2
519*test whether (p1,p2) or (p2,p1) is in L up position length
520*it returns TRUE if yes and the position k
521*/
522BOOLEAN isInPairsetL(int length,poly p1,poly p2,int*  k,kStrategy strat)
523{
524  LObject *p=&(strat->L[length]);
525
526  *k = length;
527  loop
528  {
529    if ((*k) < 0) return FALSE;
530    if (((p1 == (*p).p1) && (p2 == (*p).p2))
531    ||  ((p1 == (*p).p2) && (p2 == (*p).p1)))
532      return TRUE;
533    (*k)--;
534    p--;
535  }
536}
537
538/*2
539*in B all pairs have the same element p on the right
540*it tests whether (q,p) is in B and returns TRUE if yes
541*and the position k
542*/
543BOOLEAN isInPairsetB(poly q,int*  k,kStrategy strat)
544{
545  LObject *p=&(strat->B[strat->Bl]);
546
547  *k = strat->Bl;
548  loop
549  {
550    if ((*k) < 0) return FALSE;
551    if (q == (*p).p1)
552      return TRUE;
553    (*k)--;
554    p--;
555  }
556}
557
558int kFindInT(poly p, TSet T, int tlength)
559{
560  int i;
561
562  for (i=0; i<=tlength; i++)
563  {
564    if (T[i].p == p) return i;
565  }
566  return -1;
567}
568
569int kFindInT(poly p, kStrategy strat)
570{
571  int i;
572  do
573  {
574    i = kFindInT(p, strat->T, strat->tl);
575    if (i >= 0) return i;
576    strat = strat->next;
577  }
578  while (strat != NULL);
579  return -1;
580}
581
582#ifdef KDEBUG
583
584void sTObject::wrp()
585{
586  if (t_p != NULL) p_wrp(t_p, tailRing);
587  else if (p != NULL) p_wrp(p, currRing, tailRing);
588  else ::wrp(NULL);
589}
590
591#define kFalseReturn(x) do { if (!x) return FALSE;} while (0)
592
593// check that Lm's of a poly from T are "equal"
594static const char* kTest_LmEqual(poly p, poly t_p, ring tailRing)
595{
596  int i;
597  for (i=1; i<=tailRing->N; i++)
598  {
599    if (p_GetExp(p, i, currRing) != p_GetExp(t_p, i, tailRing))
600      return "Lm[i] different";
601  }
602  if (p_GetComp(p, currRing) != p_GetComp(t_p, tailRing))
603    return "Lm[0] different";
604  if (pNext(p) != pNext(t_p))
605    return "Lm.next different";
606  if (pGetCoeff(p) != pGetCoeff(t_p))
607    return "Lm.coeff different";
608  return NULL;
609}
610
611static BOOLEAN sloppy_max = FALSE;
612BOOLEAN kTest_T(TObject * T, ring strat_tailRing, int i, char TN)
613{
614  ring tailRing = T->tailRing;
615  if (strat_tailRing == NULL) strat_tailRing = tailRing;
616  r_assume(strat_tailRing == tailRing);
617
618  poly p = T->p;
619  ring r = currRing;
620
621  if (T->p == NULL && T->t_p == NULL && i >= 0)
622    return dReportError("%c[%d].poly is NULL", TN, i);
623
624  if (T->tailRing != currRing)
625  {
626    if (T->t_p == NULL && i > 0)
627      return dReportError("%c[%d].t_p is NULL", TN, i);
628    pFalseReturn(p_Test(T->t_p, T->tailRing));
629    if (T->p != NULL) pFalseReturn(p_LmTest(T->p, currRing));
630    if (T->p != NULL && T->t_p != NULL)
631    {
632      const char* msg = kTest_LmEqual(T->p, T->t_p, T->tailRing);
633      if (msg != NULL)
634        return dReportError("%c[%d] %s", TN, i, msg);
635      r = T->tailRing;
636      p = T->t_p;
637    }
638    if (T->p == NULL)
639    {
640      p = T->t_p;
641      r = T->tailRing;
642    }
643    if (T->t_p != NULL && i >= 0 && TN == 'T')
644    {
645      if (pNext(T->t_p) == NULL)
646      {
647        if (T->max != NULL)
648          return dReportError("%c[%d].max is not NULL as it should be", TN, i);
649      }
650      else
651      {
652        if (T->max == NULL)
653          return dReportError("%c[%d].max is NULL", TN, i);
654        if (pNext(T->max) != NULL)
655          return dReportError("pNext(%c[%d].max) != NULL", TN, i);
656
657        pFalseReturn(p_CheckPolyRing(T->max, tailRing));
658        omCheckBinAddrSize(T->max, (tailRing->PolyBin->sizeW)*SIZEOF_LONG);
659#if KDEBUG > 0
660        if (! sloppy_max)
661        {
662          poly test_max = p_GetMaxExpP(pNext(T->t_p), tailRing);
663          p_Setm(T->max, tailRing);
664          p_Setm(test_max, tailRing);
665          BOOLEAN equal = p_ExpVectorEqual(T->max, test_max, tailRing);
666          if (! equal)
667            return dReportError("%c[%d].max out of sync", TN, i);
668          p_LmFree(test_max, tailRing);
669        }
670#endif
671      }
672    }
673  }
674  else
675  {
676    if (T->max != NULL)
677      return dReportError("%c[%d].max != NULL but tailRing == currRing",TN,i);
678    if (T->t_p != NULL)
679      return dReportError("%c[%d].t_p != NULL but tailRing == currRing",TN,i);
680    if (T->p == NULL && i > 0)
681      return dReportError("%c[%d].p is NULL", TN, i);
682    pFalseReturn(p_Test(T->p, currRing));
683  }
684
685  if (i >= 0 && T->pLength != 0 && T->pLength != pLength(p))
686  {
687    int l=T->pLength;
688    T->pLength=pLength(p);
689    return dReportError("%c[%d] pLength error: has %d, specified to have %d",
690                        TN, i , pLength(p), l);
691  }
692
693  // check FDeg,  for elements in L and T
694  if (i >= 0 && (TN == 'T' || TN == 'L'))
695  {
696    // FDeg has ir element from T of L set
697    if (T->FDeg  != T->pFDeg())
698    {
699      int d=T->FDeg;
700      T->FDeg=T->pFDeg();
701      return dReportError("%c[%d] FDeg error: has %d, specified to have %d",
702                          TN, i , T->pFDeg(), d);
703    }
704  }
705
706  // check is_normalized for elements in T
707  if (i >= 0 && TN == 'T')
708  {
709    if (T->is_normalized && ! nIsOne(pGetCoeff(p)))
710      return dReportError("T[%d] is_normalized error", i);
711
712  }
713  return TRUE;
714}
715
716BOOLEAN kTest_L(LObject *L, ring strat_tailRing,
717                BOOLEAN testp, int lpos, TSet T, int tlength)
718{
719  if (testp)
720  {
721    poly pn = NULL;
722    if (L->bucket != NULL)
723    {
724      kFalseReturn(kbTest(L->bucket));
725      r_assume(L->bucket->bucket_ring == L->tailRing);
726      if (L->p != NULL && pNext(L->p) != NULL)
727      {
728        pn = pNext(L->p);
729        pNext(L->p) = NULL;
730      }
731    }
732    kFalseReturn(kTest_T(L, strat_tailRing, lpos, 'L'));
733    if (pn != NULL)
734      pNext(L->p) = pn;
735
736    ring r;
737    poly p;
738    L->GetLm(p, r);
739    if (L->sev != 0 && p_GetShortExpVector(p, r) != L->sev)
740    {
741      return dReportError("L[%d] wrong sev: has %o, specified to have %o",
742                          lpos, p_GetShortExpVector(p, r), L->sev);
743    }
744    if (lpos > 0 && L->last != NULL && pLast(p) != L->last)
745    {
746      return dReportError("L[%d] last wrong: has %p specified to have %p",
747                          lpos, pLast(p), L->last);
748    }
749  }
750  if (L->p1 == NULL)
751  {
752    // L->p2 either NULL or "normal" poly
753    pFalseReturn(pp_Test(L->p2, currRing, L->tailRing));
754  }
755  else if (tlength > 0 && T != NULL && (lpos >=0))
756  {
757    // now p1 and p2 must be != NULL and must be contained in T
758    int i;
759    i = kFindInT(L->p1, T, tlength);
760    if (i < 0)
761      return dReportError("L[%d].p1 not in T",lpos);
762    i = kFindInT(L->p2, T, tlength);
763    if (i < 0)
764      return dReportError("L[%d].p2 not in T",lpos);
765  }
766  return TRUE;
767}
768
769BOOLEAN kTest (kStrategy strat)
770{
771  int i;
772
773  // test P
774  kFalseReturn(kTest_L(&(strat->P), strat->tailRing,
775                       (strat->P.p != NULL && pNext(strat->P.p)!=strat->tail),
776                       -1, strat->T, strat->tl));
777
778  // test T
779  if (strat->T != NULL)
780  {
781    for (i=0; i<=strat->tl; i++)
782    {
783      kFalseReturn(kTest_T(&(strat->T[i]), strat->tailRing, i, 'T'));
784      if (strat->sevT[i] != pGetShortExpVector(strat->T[i].p))
785        return dReportError("strat->sevT[%d] out of sync", i);
786    }
787  }
788
789  // test L
790  if (strat->L != NULL)
791  {
792    for (i=0; i<=strat->Ll; i++)
793    {
794      kFalseReturn(kTest_L(&(strat->L[i]), strat->tailRing,
795                           strat->L[i].Next() != strat->tail, i,
796                           strat->T, strat->tl));
797      if (strat->use_buckets && strat->L[i].Next() != strat->tail &&
798          strat->L[i].Next() != NULL && strat->L[i].p1 != NULL)
799      {
800        assume(strat->L[i].bucket != NULL);
801      }
802    }
803  }
804
805  // test S
806  if (strat->S != NULL)
807    kFalseReturn(kTest_S(strat));
808
809  return TRUE;
810}
811
812BOOLEAN kTest_S(kStrategy strat)
813{
814  int i;
815  BOOLEAN ret = TRUE;
816  for (i=0; i<=strat->sl; i++)
817  {
818    if (strat->S[i] != NULL &&
819        strat->sevS[i] != pGetShortExpVector(strat->S[i]))
820    {
821      return dReportError("S[%d] wrong sev: has %o, specified to have %o",
822                          i , pGetShortExpVector(strat->S[i]), strat->sevS[i]);
823    }
824  }
825  return ret;
826}
827
828
829
830BOOLEAN kTest_TS(kStrategy strat)
831{
832  int i, j;
833  BOOLEAN ret = TRUE;
834  kFalseReturn(kTest(strat));
835
836  // test strat->R, strat->T[i].i_r
837  for (i=0; i<=strat->tl; i++)
838  {
839    if (strat->T[i].i_r < 0 || strat->T[i].i_r > strat->tl)
840      return dReportError("strat->T[%d].i_r == %d out of bounds", i,
841                          strat->T[i].i_r);
842    if (strat->R[strat->T[i].i_r] != &(strat->T[i]))
843      return dReportError("T[%d].i_r with R out of sync", i);
844  }
845  // test containment of S inT
846  if (strat->S != NULL)
847  {
848    for (i=0; i<=strat->sl; i++)
849    {
850      j = kFindInT(strat->S[i], strat->T, strat->tl);
851      if (j < 0)
852        return dReportError("S[%d] not in T", i);
853      if (strat->S_2_R[i] != strat->T[j].i_r)
854        return dReportError("S_2_R[%d]=%d != T[%d].i_r=%d\n",
855                            i, strat->S_2_R[i], j, strat->T[j].i_r);
856    }
857  }
858  // test strat->L[i].i_r1
859  for (i=0; i<=strat->Ll; i++)
860  {
861    if (strat->L[i].p1 != NULL && strat->L[i].p2)
862    {
863      if (strat->L[i].i_r1 < 0 ||
864          strat->L[i].i_r1 > strat->tl ||
865          strat->L[i].T_1(strat)->p != strat->L[i].p1)
866        return dReportError("L[%d].i_r1 out of sync", i);
867      if (strat->L[i].i_r2 < 0 ||
868          strat->L[i].i_r2 > strat->tl ||
869          strat->L[i].T_2(strat)->p != strat->L[i].p2);
870    }
871    else
872    {
873      if (strat->L[i].i_r1 != -1)
874        return dReportError("L[%d].i_r1 out of sync", i);
875      if (strat->L[i].i_r2 != -1)
876        return dReportError("L[%d].i_r2 out of sync", i);
877    }
878    if (strat->L[i].i_r != -1)
879      return dReportError("L[%d].i_r out of sync", i);
880  }
881  return TRUE;
882}
883
884#endif // KDEBUG
885
886/*2
887*cancels the i-th polynomial in the standardbase s
888*/
889void deleteInS (int i,kStrategy strat)
890{
891#ifdef ENTER_USE_MEMMOVE
892  memmove(&(strat->S[i]), &(strat->S[i+1]), (strat->sl - i)*sizeof(poly));
893  memmove(&(strat->ecartS[i]),&(strat->ecartS[i+1]),(strat->sl - i)*sizeof(int));
894  memmove(&(strat->sevS[i]),&(strat->sevS[i+1]),(strat->sl - i)*sizeof(long));
895  memmove(&(strat->S_2_R[i]),&(strat->S_2_R[i+1]),(strat->sl - i)*sizeof(int));
896#else
897  int j;
898  for (j=i; j<strat->sl; j++)
899  {
900    strat->S[j] = strat->S[j+1];
901    strat->ecartS[j] = strat->ecartS[j+1];
902    strat->sevS[j] = strat->sevS[j+1];
903    strat->S_2_R[j] = strat->S_2_R[j+1];
904  }
905#endif
906  if (strat->lenS!=NULL)
907  {
908#ifdef ENTER_USE_MEMMOVE
909    memmove(&(strat->lenS[i]),&(strat->lenS[i+1]),(strat->sl - i)*sizeof(int));
910#else
911    for (j=i; j<strat->sl; j++) strat->lenS[j] = strat->lenS[j+1];
912#endif
913  }
914  if (strat->lenSw!=NULL)
915  {
916#ifdef ENTER_USE_MEMMOVE
917    memmove(&(strat->lenSw[i]),&(strat->lenSw[i+1]),(strat->sl - i)*sizeof(wlen_type));
918#else
919    for (j=i; j<strat->sl; j++) strat->lenSw[j] = strat->lenSw[j+1];
920#endif
921  }
922  if (strat->fromQ!=NULL)
923  {
924#ifdef ENTER_USE_MEMMOVE
925    memmove(&(strat->fromQ[i]),&(strat->fromQ[i+1]),(strat->sl - i)*sizeof(int));
926#else
927    for (j=i; j<strat->sl; j++)
928    {
929      strat->fromQ[j] = strat->fromQ[j+1];
930    }
931#endif
932  }
933  strat->S[strat->sl] = NULL;
934  strat->sl--;
935}
936
937/*2
938*cancels the j-th polynomial in the set
939*/
940void deleteInL (LSet set, int *length, int j,kStrategy strat)
941{
942  if (set[j].lcm!=NULL)
943  {
944#ifdef HAVE_RINGS
945    if (pGetCoeff(set[j].lcm) != NULL)
946      pLmDelete(set[j].lcm);
947    else
948#endif
949      pLmFree(set[j].lcm);
950  }
951  if (set[j].p!=NULL)
952  {
953    if (pNext(set[j].p) == strat->tail)
954    {
955#ifdef HAVE_RINGS
956      if (pGetCoeff(set[j].p) != NULL)
957        pLmDelete(set[j].p);
958      else
959#endif
960        pLmFree(set[j].p);
961      /*- tail belongs to several int spolys -*/
962    }
963    else
964    {
965      // search p in T, if it is there, do not delete it
966      if (pOrdSgn != -1 || kFindInT(set[j].p, strat) < 0)
967      {
968        // assure that for global orderings kFindInT fails
969        assume(pOrdSgn == -1 || kFindInT(set[j].p, strat) < 0);
970        set[j].Delete();
971      }
972    }
973  }
974  if (*length > 0 && j < *length)
975  {
976#ifdef ENTER_USE_MEMMOVE
977    memmove(&(set[j]), &(set[j+1]), (*length - j)*sizeof(LObject));
978#else
979    int i;
980    for (i=j; i < (*length); i++)
981      set[i] = set[i+1];
982#endif
983  }
984#ifdef KDEBUG
985  memset(&(set[*length]),0,sizeof(LObject));
986#endif
987  (*length)--;
988}
989
990/*2
991*enters p at position at in L
992*/
993void enterL (LSet *set,int *length, int *LSetmax, LObject p,int at)
994{
995#ifdef PDEBUG
996  /*  zaehler++; */
997#endif /*PDEBUG*/
998  int i;
999  // this should be corrected
1000  assume(p.FDeg == p.pFDeg());
1001
1002  if ((*length)>=0)
1003  {
1004    if ((*length) == (*LSetmax)-1) enlargeL(set,LSetmax,setmaxLinc);
1005    if (at <= (*length))
1006#ifdef ENTER_USE_MEMMOVE
1007      memmove(&((*set)[at+1]), &((*set)[at]), ((*length)-at+1)*sizeof(LObject));
1008#else
1009    for (i=(*length)+1; i>=at+1; i--) (*set)[i] = (*set)[i-1];
1010#endif
1011  }
1012  else at = 0;
1013  (*set)[at] = p;
1014  (*length)++;
1015}
1016
1017/*2
1018* computes the normal ecart;
1019* used in mora case and if pLexOrder & sugar in bba case
1020*/
1021void initEcartNormal (LObject* h)
1022{
1023  h->FDeg = h->pFDeg();
1024  h->ecart = h->pLDeg() - h->FDeg;
1025  // h->length is set by h->pLDeg
1026  h->length=h->pLength=pLength(h->p);
1027}
1028
1029void initEcartBBA (LObject* h)
1030{
1031  h->FDeg = h->pFDeg();
1032  (*h).ecart = 0;
1033  h->length=h->pLength=pLength(h->p);
1034}
1035
1036void initEcartPairBba (LObject* Lp,poly f,poly g,int ecartF,int ecartG)
1037{
1038  Lp->FDeg = Lp->pFDeg();
1039  (*Lp).ecart = 0;
1040  (*Lp).length = 0;
1041}
1042
1043void initEcartPairMora (LObject* Lp,poly f,poly g,int ecartF,int ecartG)
1044{
1045  Lp->FDeg = Lp->pFDeg();
1046  (*Lp).ecart = si_max(ecartF,ecartG);
1047  (*Lp).ecart = (*Lp).ecart- (Lp->FDeg -pFDeg((*Lp).lcm,currRing));
1048  (*Lp).length = 0;
1049}
1050
1051/*2
1052*if ecart1<=ecart2 it returns TRUE
1053*/
1054static inline BOOLEAN sugarDivisibleBy(int ecart1, int ecart2)
1055{
1056  return (ecart1 <= ecart2);
1057}
1058
1059#ifdef HAVE_RINGS
1060/*2
1061* put the pair (s[i],p)  into the set B, ecart=ecart(p) (ring case)
1062*/
1063void enterOnePairRing (int i,poly p,int ecart, int isFromQ,kStrategy strat, int atR = -1)
1064{
1065  assume(i<=strat->sl);
1066  int      l,j,compare,compareCoeff;
1067  LObject  Lp;
1068
1069  if (strat->interred_flag) return;
1070#ifdef KDEBUG
1071  Lp.ecart=0; Lp.length=0;
1072#endif
1073  /*- computes the lcm(s[i],p) -*/
1074  Lp.lcm = pInit();
1075  pSetCoeff0(Lp.lcm, nLcm(pGetCoeff(p), pGetCoeff(strat->S[i]), currRing));
1076  // Lp.lcm == 0
1077  if (nIsZero(pGetCoeff(Lp.lcm)))
1078  {
1079#ifdef KDEBUG
1080      if (TEST_OPT_DEBUG)
1081      {
1082        PrintS("--- Lp.lcm == 0\n");
1083        PrintS("p:");
1084        wrp(p);
1085        Print("  strat->S[%d]:", i);
1086        wrp(strat->S[i]);
1087        PrintLn();
1088      }
1089#endif
1090      strat->cp++;
1091      pLmDelete(Lp.lcm);
1092      return;
1093  }
1094  // basic product criterion
1095  pLcm(p,strat->S[i],Lp.lcm);
1096  pSetm(Lp.lcm);
1097  assume(!strat->sugarCrit);
1098  if (pHasNotCF(p,strat->S[i]) && nIsUnit(pGetCoeff(p)) && nIsUnit(pGetCoeff(strat->S[i])))
1099  {
1100#ifdef KDEBUG
1101      if (TEST_OPT_DEBUG)
1102      {
1103        PrintS("--- product criterion func enterOnePairRing type 1\n");
1104        PrintS("p:");
1105        wrp(p);
1106        Print("  strat->S[%d]:", i);
1107        wrp(strat->S[i]);
1108        PrintLn();
1109      }
1110#endif
1111      strat->cp++;
1112      pLmDelete(Lp.lcm);
1113      return;
1114  }
1115  assume(!strat->fromT);
1116  /*
1117  *the set B collects the pairs of type (S[j],p)
1118  *suppose (r,p) is in B and (s,p) is the new pair and lcm(s,p) != lcm(r,p)
1119  *if the leading term of s devides lcm(r,p) then (r,p) will be canceled
1120  *if the leading term of r devides lcm(s,p) then (s,p) will not enter B
1121  */
1122  for(j = strat->Bl;j>=0;j--)
1123  {
1124    compare=pDivCompRing(strat->B[j].lcm,Lp.lcm);
1125    compareCoeff = nDivComp(pGetCoeff(strat->B[j].lcm), pGetCoeff(Lp.lcm));
1126    if (compareCoeff == 0 || compare == compareCoeff)
1127    {
1128      if (compare == 1)
1129      {
1130        strat->c3++;
1131#ifdef KDEBUG
1132        if (TEST_OPT_DEBUG)
1133        {
1134          PrintS("--- chain criterion type 1\n");
1135          PrintS("strat->B[j]:");
1136          wrp(strat->B[j].lcm);
1137          PrintS("  Lp.lcm:");
1138          wrp(Lp.lcm);
1139          PrintLn();
1140        }
1141#endif
1142        if ((strat->fromQ==NULL) || (isFromQ==0) || (strat->fromQ[i]==0))
1143        {
1144          pLmDelete(Lp.lcm);
1145          return;
1146        }
1147        break;
1148      }
1149      else
1150      if (compare == -1)
1151      {
1152#ifdef KDEBUG
1153        if (TEST_OPT_DEBUG)
1154        {
1155          PrintS("--- chain criterion type 2\n");
1156          Print("strat->B[%d].lcm:",j);
1157          wrp(strat->B[j].lcm);
1158          PrintS("  Lp.lcm:");
1159          wrp(Lp.lcm);
1160          PrintLn();
1161        }
1162#endif
1163        deleteInL(strat->B,&strat->Bl,j,strat);
1164        strat->c3++;
1165      }
1166    }
1167    if ((compare == pDivComp_EQUAL) && (compareCoeff != 2))
1168    {
1169      if (compareCoeff == pDivComp_LESS)
1170      {
1171#ifdef KDEBUG
1172        if (TEST_OPT_DEBUG)
1173        {
1174          PrintS("--- chain criterion type 3\n");
1175          Print("strat->B[%d].lcm:", j);
1176          wrp(strat->B[j].lcm);
1177          PrintS("  Lp.lcm:");
1178          wrp(Lp.lcm);
1179          PrintLn();
1180        }
1181#endif
1182        strat->c3++;
1183        if ((strat->fromQ==NULL) || (isFromQ==0) || (strat->fromQ[i]==0))
1184        {
1185          pLmDelete(Lp.lcm);
1186          return;
1187        }
1188        break;
1189      }
1190      else
1191      // Add hint for same LM and LC (later) (TODO Oliver)
1192      // if (compareCoeff == pDivComp_GREATER)
1193      {
1194#ifdef KDEBUG
1195        if (TEST_OPT_DEBUG)
1196        {
1197          PrintS("--- chain criterion type 4\n");
1198          Print("strat->B[%d].lcm:", j);
1199          wrp(strat->B[j].lcm);
1200          PrintS("  Lp.lcm:");
1201          wrp(Lp.lcm);
1202          PrintLn();
1203        }
1204#endif
1205        deleteInL(strat->B,&strat->Bl,j,strat);
1206        strat->c3++;
1207      }
1208    }
1209  }
1210  /*
1211  *the pair (S[i],p) enters B if the spoly != 0
1212  */
1213  /*-  compute the short s-polynomial -*/
1214  if ((strat->S[i]==NULL) || (p==NULL)) {
1215#ifdef KDEBUG
1216    if (TEST_OPT_DEBUG)
1217    {
1218      PrintS("--- spoly = NULL\n");
1219    }
1220#endif
1221    pLmDelete(Lp.lcm);
1222    return;
1223  }
1224  if ((strat->fromQ!=NULL) && (isFromQ!=0) && (strat->fromQ[i]!=0))
1225  {
1226    // Is from a previous computed GB, therefore we know that spoly will
1227    // reduce to zero. Oliver.
1228    WarnS("Could we come here? 8738947389");
1229    Lp.p=NULL;
1230  }
1231  else
1232  {
1233    Lp.p = ksCreateShortSpoly(strat->S[i], p, strat->tailRing);
1234  }
1235  if (Lp.p == NULL)
1236  {
1237#ifdef KDEBUG
1238    if (TEST_OPT_DEBUG)
1239    {
1240      PrintS("--- spoly = NULL\n");
1241    }
1242#endif
1243    /*- the case that the s-poly is 0 -*/
1244    if (strat->pairtest==NULL) initPairtest(strat);
1245    strat->pairtest[i] = TRUE;/*- hint for spoly(S^[i],p)=0 -*/
1246    strat->pairtest[strat->sl+1] = TRUE;
1247    /*hint for spoly(S[i],p) == 0 for some i,0 <= i <= sl*/
1248    /*
1249    *suppose we have (s,r),(r,p),(s,p) and spoly(s,p) == 0 and (r,p) is
1250    *still in B (i.e. lcm(r,p) == lcm(s,p) or the leading term of s does not
1251    *devide lcm(r,p)). In the last case (s,r) can be canceled if the leading
1252    *term of p devides the lcm(s,r)
1253    *(this canceling should be done here because
1254    *the case lcm(s,p) == lcm(s,r) is not covered in chainCrit)
1255    *the first case is handeled in chainCrit
1256    */
1257    pLmDelete(Lp.lcm);
1258  }
1259  else
1260  {
1261    /*- the pair (S[i],p) enters B -*/
1262    Lp.p1 = strat->S[i];
1263    Lp.p2 = p;
1264
1265    pNext(Lp.p) = strat->tail;
1266
1267    if (atR >= 0)
1268    {
1269      Lp.i_r2 = atR;
1270      Lp.i_r1 = strat->S_2_R[i];
1271    }
1272    strat->initEcartPair(&Lp,strat->S[i],p,strat->ecartS[i],ecart);
1273    l = strat->posInL(strat->B,strat->Bl,&Lp,strat);
1274    enterL(&strat->B,&strat->Bl,&strat->Bmax,Lp,l);
1275  }
1276}
1277
1278
1279/*2
1280* put the  lcm(s[i],p)  into the set B
1281*/
1282
1283BOOLEAN enterOneStrongPoly (int i,poly p,int ecart, int isFromQ,kStrategy strat, int atR = -1)
1284{
1285  number d, s, t;
1286  assume(i<=strat->sl);
1287  assume(atR >= 0);
1288  poly m1, m2, gcd;
1289
1290  d = nExtGcd(pGetCoeff(p), pGetCoeff(strat->S[i]), &s, &t);
1291
1292  if (nIsZero(s) || nIsZero(t))  // evtl. durch divBy tests ersetzen
1293  {
1294    nDelete(&d);
1295    nDelete(&s);
1296    nDelete(&t);
1297    return FALSE;
1298  }
1299
1300  k_GetStrongLeadTerms(p, strat->S[i], currRing, m1, m2, gcd, strat->tailRing);
1301  while (! kCheckStrongCreation(atR, m1, i, m2, strat) )
1302  {
1303    memset(&(strat->P), 0, sizeof(strat->P));
1304    kStratChangeTailRing(strat);
1305    strat->P = *(strat->R[atR]);
1306    p_LmFree(m1, strat->tailRing);
1307    p_LmFree(m2, strat->tailRing);
1308    p_LmFree(gcd, currRing);
1309    k_GetStrongLeadTerms(p, strat->S[i], currRing, m1, m2, gcd, strat->tailRing);
1310  }
1311  pSetCoeff0(m1, s);
1312  pSetCoeff0(m2, t);
1313  pSetCoeff0(gcd, d);
1314
1315#ifdef KDEBUG
1316  if (TEST_OPT_DEBUG)
1317  {
1318    // Print("t = %d; s = %d; d = %d\n", nInt(t), nInt(s), nInt(d));
1319    PrintS("m1 = ");
1320    p_wrp(m1, strat->tailRing);
1321    PrintS(" ; m2 = ");
1322    p_wrp(m2, strat->tailRing);
1323    PrintS(" ; gcd = ");
1324    wrp(gcd);
1325    PrintS("\n--- create strong gcd poly: ");
1326    Print("\n p: ", i);
1327    wrp(p);
1328    Print("\n strat->S[%d]: ", i);
1329    wrp(strat->S[i]);
1330    PrintS(" ---> ");
1331  }
1332#endif
1333
1334  pNext(gcd) = p_Add_q(pp_Mult_mm(pNext(p), m1, strat->tailRing), pp_Mult_mm(pNext(strat->S[i]), m2, strat->tailRing), strat->tailRing);
1335  p_LmDelete(m1, strat->tailRing);
1336  p_LmDelete(m2, strat->tailRing);
1337
1338#ifdef KDEBUG
1339  if (TEST_OPT_DEBUG)
1340  {
1341    wrp(gcd);
1342    PrintLn();
1343  }
1344#endif
1345
1346  LObject h;
1347  h.p = gcd;
1348  h.tailRing = strat->tailRing;
1349  int posx;
1350  h.pCleardenom();
1351  strat->initEcart(&h);
1352  if (strat->Ll==-1)
1353    posx =0;
1354  else
1355    posx = strat->posInL(strat->L,strat->Ll,&h,strat);
1356  h.sev = pGetShortExpVector(h.p);
1357  if (currRing!=strat->tailRing)
1358    h.t_p = k_LmInit_currRing_2_tailRing(h.p, strat->tailRing);
1359  enterL(&strat->L,&strat->Ll,&strat->Lmax,h,posx);
1360  return TRUE;
1361}
1362#endif
1363
1364/*2
1365* put the pair (s[i],p)  into the set B, ecart=ecart(p)
1366*/
1367
1368
1369void enterOnePairNormal (int i,poly p,int ecart, int isFromQ,kStrategy strat, int atR = -1)
1370{
1371  assume(i<=strat->sl);
1372  if (strat->interred_flag) return;
1373
1374  int      l,j,compare;
1375  LObject  Lp;
1376  Lp.i_r = -1;
1377
1378#ifdef KDEBUG
1379  Lp.ecart=0; Lp.length=0;
1380#endif
1381  /*- computes the lcm(s[i],p) -*/
1382  Lp.lcm = pInit();
1383
1384  pLcm(p,strat->S[i],Lp.lcm);
1385  pSetm(Lp.lcm);
1386
1387#define MYTEST 0
1388
1389  if (strat->sugarCrit && ALLOW_PROD_CRIT(strat))
1390  {
1391    if((!((strat->ecartS[i]>0)&&(ecart>0)))
1392    && pHasNotCF(p,strat->S[i]))
1393    {
1394    /*
1395    *the product criterion has applied for (s,p),
1396    *i.e. lcm(s,p)=product of the leading terms of s and p.
1397    *Suppose (s,r) is in L and the leading term
1398    *of p divides lcm(s,r)
1399    *(==> the leading term of p divides the leading term of r)
1400    *but the leading term of s does not divide the leading term of r
1401    *(notice that tis condition is automatically satisfied if r is still
1402    *in S), then (s,r) can be cancelled.
1403    *This should be done here because the
1404    *case lcm(s,r)=lcm(s,p) is not covered by chainCrit.
1405    *
1406    *Moreover, skipping (s,r) holds also for the noncommutative case.
1407    */
1408      strat->cp++;
1409      pLmFree(Lp.lcm);
1410      Lp.lcm=NULL;
1411      return;
1412    }
1413    else
1414      Lp.ecart = si_max(ecart,strat->ecartS[i]);
1415    if (strat->fromT && (strat->ecartS[i]>ecart))
1416    {
1417      pLmFree(Lp.lcm);
1418      Lp.lcm=NULL;
1419      return;
1420      /*the pair is (s[i],t[.]), discard it if the ecart is too big*/
1421    }
1422    /*
1423    *the set B collects the pairs of type (S[j],p)
1424    *suppose (r,p) is in B and (s,p) is the new pair and lcm(s,p)#lcm(r,p)
1425    *if the leading term of s devides lcm(r,p) then (r,p) will be canceled
1426    *if the leading term of r devides lcm(s,p) then (s,p) will not enter B
1427    */
1428    {
1429      j = strat->Bl;
1430      loop
1431      {
1432        if (j < 0)  break;
1433        compare=pDivComp(strat->B[j].lcm,Lp.lcm);
1434        if ((compare==1)
1435        &&(sugarDivisibleBy(strat->B[j].ecart,Lp.ecart)))
1436        {
1437          strat->c3++;
1438          if ((strat->fromQ==NULL) || (isFromQ==0) || (strat->fromQ[i]==0))
1439          {
1440            pLmFree(Lp.lcm);
1441            return;
1442          }
1443          break;
1444        }
1445        else
1446        if ((compare ==-1)
1447        && sugarDivisibleBy(Lp.ecart,strat->B[j].ecart))
1448        {
1449          deleteInL(strat->B,&strat->Bl,j,strat);
1450          strat->c3++;
1451        }
1452        j--;
1453      }
1454    }
1455  }
1456  else /*sugarcrit*/
1457  {
1458    if (ALLOW_PROD_CRIT(strat))
1459    {
1460      // if currRing->nc_type!=quasi (or skew)
1461      // TODO: enable productCrit for super commutative algebras...
1462      if(/*(strat->ak==0) && productCrit(p,strat->S[i])*/
1463      pHasNotCF(p,strat->S[i]))
1464      {
1465      /*
1466      *the product criterion has applied for (s,p),
1467      *i.e. lcm(s,p)=product of the leading terms of s and p.
1468      *Suppose (s,r) is in L and the leading term
1469      *of p devides lcm(s,r)
1470      *(==> the leading term of p devides the leading term of r)
1471      *but the leading term of s does not devide the leading term of r
1472      *(notice that tis condition is automatically satisfied if r is still
1473      *in S), then (s,r) can be canceled.
1474      *This should be done here because the
1475      *case lcm(s,r)=lcm(s,p) is not covered by chainCrit.
1476      */
1477          strat->cp++;
1478          pLmFree(Lp.lcm);
1479          Lp.lcm=NULL;
1480          return;
1481      }
1482      if (strat->fromT && (strat->ecartS[i]>ecart))
1483      {
1484        pLmFree(Lp.lcm);
1485        Lp.lcm=NULL;
1486        return;
1487        /*the pair is (s[i],t[.]), discard it if the ecart is too big*/
1488      }
1489      /*
1490      *the set B collects the pairs of type (S[j],p)
1491      *suppose (r,p) is in B and (s,p) is the new pair and lcm(s,p)#lcm(r,p)
1492      *if the leading term of s devides lcm(r,p) then (r,p) will be canceled
1493      *if the leading term of r devides lcm(s,p) then (s,p) will not enter B
1494      */
1495      for(j = strat->Bl;j>=0;j--)
1496      {
1497        compare=pDivComp(strat->B[j].lcm,Lp.lcm);
1498        if (compare==1)
1499        {
1500          strat->c3++;
1501          if ((strat->fromQ==NULL) || (isFromQ==0) || (strat->fromQ[i]==0))
1502          {
1503            pLmFree(Lp.lcm);
1504            return;
1505          }
1506          break;
1507        }
1508        else
1509        if (compare ==-1)
1510        {
1511          deleteInL(strat->B,&strat->Bl,j,strat);
1512          strat->c3++;
1513        }
1514      }
1515    }
1516  }
1517  /*
1518  *the pair (S[i],p) enters B if the spoly != 0
1519  */
1520  /*-  compute the short s-polynomial -*/
1521  if (strat->fromT && !TEST_OPT_INTSTRATEGY)
1522    pNorm(p);
1523
1524  if ((strat->S[i]==NULL) || (p==NULL))
1525    return;
1526
1527  if ((strat->fromQ!=NULL) && (isFromQ!=0) && (strat->fromQ[i]!=0))
1528    Lp.p=NULL;
1529  else
1530  {
1531    #ifdef HAVE_PLURAL
1532    if ( rIsPluralRing(currRing) )
1533    {
1534      if(pHasNotCF(p, strat->S[i]))
1535      {
1536//         if(ncRingType(currRing) == nc_lie)
1537//         {
1538//             // generalized prod-crit for lie-type
1539//             strat->cp++;
1540//             Lp.p = nc_p_Bracket_qq(pCopy(p),strat->S[i]);
1541//         }
1542//         else
1543        if( ALLOW_PROD_CRIT(strat) )
1544        {
1545            // product criterion for homogeneous case in SCA
1546            strat->cp++;
1547            Lp.p = NULL;
1548        }
1549        else
1550          Lp.p = // nc_CreateSpoly(strat->S[i],p,currRing);
1551                 nc_CreateShortSpoly(strat->S[i], p, currRing); 
1552      }
1553      else
1554        Lp.p = // nc_CreateSpoly(strat->S[i],p,currRing);
1555                nc_CreateShortSpoly(strat->S[i], p, currRing); 
1556
1557     
1558#if MYTEST
1559      if (TEST_OPT_DEBUG)
1560      {
1561        PrintS("strat->S[i]: "); pWrite(strat->S[i]);
1562        PrintS("p: "); pWrite(p);
1563        PrintS("SPoly: "); pWrite(Lp.p);
1564      }
1565#endif     
1566     
1567    }
1568    else
1569    #endif
1570    {
1571      assume(!rIsPluralRing(currRing));
1572      Lp.p = ksCreateShortSpoly(strat->S[i], p, strat->tailRing);
1573#if MYTEST
1574      if (TEST_OPT_DEBUG)
1575      {
1576        PrintS("strat->S[i]: "); pWrite(strat->S[i]);
1577        PrintS("p: "); pWrite(p);
1578        PrintS("commutative SPoly: "); pWrite(Lp.p);
1579      }
1580#endif     
1581
1582      }
1583  }
1584  if (Lp.p == NULL)
1585  {
1586    /*- the case that the s-poly is 0 -*/
1587    if (strat->pairtest==NULL) initPairtest(strat);
1588    strat->pairtest[i] = TRUE;/*- hint for spoly(S^[i],p)=0 -*/
1589    strat->pairtest[strat->sl+1] = TRUE;
1590    /*hint for spoly(S[i],p) == 0 for some i,0 <= i <= sl*/
1591    /*
1592    *suppose we have (s,r),(r,p),(s,p) and spoly(s,p) == 0 and (r,p) is
1593    *still in B (i.e. lcm(r,p) == lcm(s,p) or the leading term of s does not
1594    *devide lcm(r,p)). In the last case (s,r) can be canceled if the leading
1595    *term of p devides the lcm(s,r)
1596    *(this canceling should be done here because
1597    *the case lcm(s,p) == lcm(s,r) is not covered in chainCrit)
1598    *the first case is handeled in chainCrit
1599    */
1600    if (Lp.lcm!=NULL) pLmFree(Lp.lcm);
1601  }
1602  else
1603  {
1604    /*- the pair (S[i],p) enters B -*/
1605    Lp.p1 = strat->S[i];
1606    Lp.p2 = p;
1607
1608//    if ( !rIsPluralRing(currRing) ) // !!!!
1609    assume(pNext(Lp.p)==NULL);
1610    pNext(Lp.p) = strat->tail; // !!!
1611
1612    if (atR >= 0)
1613    {
1614      Lp.i_r1 = strat->S_2_R[i];
1615      Lp.i_r2 = atR;
1616    }
1617    else
1618    {
1619      Lp.i_r1 = -1;
1620      Lp.i_r2 = -1;
1621    }
1622    strat->initEcartPair(&Lp,strat->S[i],p,strat->ecartS[i],ecart);
1623
1624    if (TEST_OPT_INTSTRATEGY)
1625    {
1626      if (!rIsPluralRing(currRing))
1627        nDelete(&(Lp.p->coef));
1628    }
1629
1630    l = strat->posInL(strat->B,strat->Bl,&Lp,strat);
1631    enterL(&strat->B,&strat->Bl,&strat->Bmax,Lp,l);
1632  }
1633}
1634
1635/*2
1636* put the pair (s[i],p) into the set L, ecart=ecart(p)
1637* in the case that s forms a SB of (s)
1638*/
1639void enterOnePairSpecial (int i,poly p,int ecart,kStrategy strat, int atR = -1)
1640{
1641  //PrintS("try ");wrp(strat->S[i]);PrintS(" and ");wrp(p);PrintLn();
1642  if(pHasNotCF(p,strat->S[i]))
1643  {
1644    //PrintS("prod-crit\n");
1645    if(ALLOW_PROD_CRIT(strat))
1646    {
1647      //PrintS("prod-crit\n");
1648      strat->cp++;
1649      return;
1650    }
1651  }
1652
1653  int      l,j,compare;
1654  LObject  Lp;
1655  Lp.i_r = -1;
1656
1657  Lp.lcm = pInit();
1658  pLcm(p,strat->S[i],Lp.lcm);
1659  pSetm(Lp.lcm);
1660  for(j = strat->Ll;j>=0;j--)
1661  {
1662    compare=pDivComp(strat->L[j].lcm,Lp.lcm);
1663    if ((compare==1) || (pLmEqual(strat->L[j].lcm,Lp.lcm)))
1664    {
1665      //PrintS("c3-crit\n");
1666      strat->c3++;
1667      pLmFree(Lp.lcm);
1668      return;
1669    }
1670    else if (compare ==-1)
1671    {
1672      //Print("c3-crit with L[%d]\n",j);
1673      deleteInL(strat->L,&strat->Ll,j,strat);
1674      strat->c3++;
1675    }
1676  }
1677  /*-  compute the short s-polynomial -*/
1678
1679  #ifdef HAVE_PLURAL
1680  if (rIsPluralRing(currRing))
1681  {
1682    Lp.p = nc_CreateShortSpoly(strat->S[i],p); // ??? strat->tailRing?
1683  }
1684  else
1685  #endif
1686    Lp.p = ksCreateShortSpoly(strat->S[i],p,strat->tailRing);
1687
1688  if (Lp.p == NULL)
1689  {
1690     //PrintS("short spoly==NULL\n");
1691     pLmFree(Lp.lcm);
1692  }
1693  else
1694  {
1695    /*- the pair (S[i],p) enters L -*/
1696    Lp.p1 = strat->S[i];
1697    Lp.p2 = p;
1698    if (atR >= 0)
1699    {
1700      Lp.i_r1 = strat->S_2_R[i];
1701      Lp.i_r2 = atR;
1702    }
1703    else
1704    {
1705      Lp.i_r1 = -1;
1706      Lp.i_r2 = -1;
1707    }
1708    assume(pNext(Lp.p) == NULL);
1709    pNext(Lp.p) = strat->tail;
1710    strat->initEcartPair(&Lp,strat->S[i],p,strat->ecartS[i],ecart);
1711    if (TEST_OPT_INTSTRATEGY)
1712    {
1713      nDelete(&(Lp.p->coef));
1714    }
1715    l = strat->posInL(strat->L,strat->Ll,&Lp,strat);
1716    //Print("-> L[%d]\n",l);
1717    enterL(&strat->L,&strat->Ll,&strat->Lmax,Lp,l);
1718  }
1719}
1720
1721/*2
1722* merge set B into L
1723*/
1724void kMergeBintoL(kStrategy strat)
1725{
1726  int j=strat->Ll+strat->Bl+1;
1727  if (j>strat->Lmax)
1728  {
1729    j=((j+setmaxLinc-1)/setmaxLinc)*setmaxLinc;
1730    strat->L = (LSet)omReallocSize(strat->L,strat->Lmax*sizeof(LObject),
1731                                 j*sizeof(LObject));
1732    strat->Lmax=j;
1733  }
1734  j = strat->Ll;
1735  int i;
1736  for (i=strat->Bl; i>=0; i--)
1737  {
1738    j = strat->posInL(strat->L,j,&(strat->B[i]),strat);
1739    enterL(&strat->L,&strat->Ll,&strat->Lmax,strat->B[i],j);
1740  }
1741  strat->Bl = -1;
1742}
1743/*2
1744*the pairset B of pairs of type (s[i],p) is complete now. It will be updated
1745*using the chain-criterion in B and L and enters B to L
1746*/
1747void chainCritNormal (poly p,int ecart,kStrategy strat)
1748{
1749  int i,j,l;
1750
1751  /*
1752  *pairtest[i] is TRUE if spoly(S[i],p) == 0.
1753  *In this case all elements in B such
1754  *that their lcm is divisible by the leading term of S[i] can be canceled
1755  */
1756  if (strat->pairtest!=NULL)
1757  {
1758    {
1759      /*- i.e. there is an i with pairtest[i]==TRUE -*/
1760      for (j=0; j<=strat->sl; j++)
1761      {
1762        if (strat->pairtest[j])
1763        {
1764          for (i=strat->Bl; i>=0; i--)
1765          {
1766            if (pDivisibleBy(strat->S[j],strat->B[i].lcm))
1767            {
1768              deleteInL(strat->B,&strat->Bl,i,strat);
1769              strat->c3++;
1770            }
1771          }
1772        }
1773      }
1774    }
1775    omFreeSize(strat->pairtest,(strat->sl+2)*sizeof(BOOLEAN));
1776    strat->pairtest=NULL;
1777  }
1778  if (strat->Gebauer || strat->fromT)
1779  {
1780    if (strat->sugarCrit)
1781    {
1782    /*
1783    *suppose L[j] == (s,r) and p/lcm(s,r)
1784    *and lcm(s,r)#lcm(s,p) and lcm(s,r)#lcm(r,p)
1785    *and in case the sugar is o.k. then L[j] can be canceled
1786    */
1787      for (j=strat->Ll; j>=0; j--)
1788      {
1789        if (sugarDivisibleBy(ecart,strat->L[j].ecart)
1790        && ((pNext(strat->L[j].p) == strat->tail) || (pOrdSgn==1))
1791        && pCompareChain(p,strat->L[j].p1,strat->L[j].p2,strat->L[j].lcm))
1792        {
1793          if (strat->L[j].p == strat->tail)
1794          {
1795              deleteInL(strat->L,&strat->Ll,j,strat);
1796              strat->c3++;
1797          }
1798        }
1799      }
1800      /*
1801      *this is GEBAUER-MOELLER:
1802      *in B all elements with the same lcm except the "best"
1803      *(i.e. the last one in B with this property) will be canceled
1804      */
1805      j = strat->Bl;
1806      loop /*cannot be changed into a for !!! */
1807      {
1808        if (j <= 0) break;
1809        i = j-1;
1810        loop
1811        {
1812          if (i <  0) break;
1813          if (pLmEqual(strat->B[j].lcm,strat->B[i].lcm))
1814          {
1815            strat->c3++;
1816            if (sugarDivisibleBy(strat->B[j].ecart,strat->B[i].ecart))
1817            {
1818              deleteInL(strat->B,&strat->Bl,i,strat);
1819              j--;
1820            }
1821            else
1822            {
1823              deleteInL(strat->B,&strat->Bl,j,strat);
1824              break;
1825            }
1826          }
1827          i--;
1828        }
1829        j--;
1830      }
1831    }
1832    else /*sugarCrit*/
1833    {
1834      /*
1835      *suppose L[j] == (s,r) and p/lcm(s,r)
1836      *and lcm(s,r)#lcm(s,p) and lcm(s,r)#lcm(r,p)
1837      *and in case the sugar is o.k. then L[j] can be canceled
1838      */
1839      for (j=strat->Ll; j>=0; j--)
1840      {
1841        if (pCompareChain(p,strat->L[j].p1,strat->L[j].p2,strat->L[j].lcm))
1842        {
1843          if ((pNext(strat->L[j].p) == strat->tail)||(pOrdSgn==1))
1844          {
1845            deleteInL(strat->L,&strat->Ll,j,strat);
1846            strat->c3++;
1847          }
1848        }
1849      }
1850      /*
1851      *this is GEBAUER-MOELLER:
1852      *in B all elements with the same lcm except the "best"
1853      *(i.e. the last one in B with this property) will be canceled
1854      */
1855      j = strat->Bl;
1856      loop   /*cannot be changed into a for !!! */
1857      {
1858        if (j <= 0) break;
1859        for(i=j-1; i>=0; i--)
1860        {
1861          if (pLmEqual(strat->B[j].lcm,strat->B[i].lcm))
1862          {
1863            strat->c3++;
1864            deleteInL(strat->B,&strat->Bl,i,strat);
1865            j--;
1866          }
1867        }
1868        j--;
1869      }
1870    }
1871    /*
1872    *the elements of B enter L
1873    */
1874    kMergeBintoL(strat);
1875  }
1876  else
1877  {
1878    for (j=strat->Ll; j>=0; j--)
1879    {
1880      if (pCompareChain(p,strat->L[j].p1,strat->L[j].p2,strat->L[j].lcm))
1881      {
1882        if ((pNext(strat->L[j].p) == strat->tail)||(pOrdSgn==1))
1883        {
1884          deleteInL(strat->L,&strat->Ll,j,strat);
1885          strat->c3++;
1886        }
1887      }
1888    }
1889    /*
1890    *this is our MODIFICATION of GEBAUER-MOELLER:
1891    *First the elements of B enter L,
1892    *then we fix a lcm and the "best" element in L
1893    *(i.e the last in L with this lcm and of type (s,p))
1894    *and cancel all the other elements of type (r,p) with this lcm
1895    *except the case the element (s,r) has also the same lcm
1896    *and is on the worst position with respect to (s,p) and (r,p)
1897    */
1898    /*
1899    *B enters to L/their order with respect to B is permutated for elements
1900    *B[i].p with the same leading term
1901    */
1902    kMergeBintoL(strat);
1903    j = strat->Ll;
1904    loop  /*cannot be changed into a for !!! */
1905    {
1906      if (j <= 0)
1907      {
1908        /*now L[0] cannot be canceled any more and the tail can be removed*/
1909        if (strat->L[0].p2 == strat->tail) strat->L[0].p2 = p;
1910        break;
1911      }
1912      if (strat->L[j].p2 == p)
1913      {
1914        i = j-1;
1915        loop
1916        {
1917          if (i < 0)  break;
1918          if ((strat->L[i].p2 == p) && pLmEqual(strat->L[j].lcm,strat->L[i].lcm))
1919          {
1920            /*L[i] could be canceled but we search for a better one to cancel*/
1921            strat->c3++;
1922            if (isInPairsetL(i-1,strat->L[j].p1,strat->L[i].p1,&l,strat)
1923            && (pNext(strat->L[l].p) == strat->tail)
1924            && (!pLmEqual(strat->L[i].p,strat->L[l].p))
1925            && pDivisibleBy(p,strat->L[l].lcm))
1926            {
1927              /*
1928              *"NOT equal(...)" because in case of "equal" the element L[l]
1929              *is "older" and has to be from theoretical point of view behind
1930              *L[i], but we do not want to reorder L
1931              */
1932              strat->L[i].p2 = strat->tail;
1933              /*
1934              *L[l] will be canceled, we cannot cancel L[i] later on,
1935              *so we mark it with "tail"
1936              */
1937              deleteInL(strat->L,&strat->Ll,l,strat);
1938              i--;
1939            }
1940            else
1941            {
1942              deleteInL(strat->L,&strat->Ll,i,strat);
1943            }
1944            j--;
1945          }
1946          i--;
1947        }
1948      }
1949      else if (strat->L[j].p2 == strat->tail)
1950      {
1951        /*now L[j] cannot be canceled any more and the tail can be removed*/
1952        strat->L[j].p2 = p;
1953      }
1954      j--;
1955    }
1956  }
1957}
1958#ifdef HAVE_RATGRING
1959void chainCritPart (poly p,int ecart,kStrategy strat)
1960{
1961  int i,j,l;
1962
1963  /*
1964  *pairtest[i] is TRUE if spoly(S[i],p) == 0.
1965  *In this case all elements in B such
1966  *that their lcm is divisible by the leading term of S[i] can be canceled
1967  */
1968  if (strat->pairtest!=NULL)
1969  {
1970    {
1971      /*- i.e. there is an i with pairtest[i]==TRUE -*/
1972      for (j=0; j<=strat->sl; j++)
1973      {
1974        if (strat->pairtest[j])
1975        {
1976          for (i=strat->Bl; i>=0; i--)
1977          {
1978            if (_p_LmDivisibleByPart(strat->S[j],currRing,
1979               strat->B[i].lcm,currRing,
1980               currRing->real_var_start,currRing->real_var_end))
1981            {
1982              if(TEST_OPT_DEBUG)
1983              {
1984                 Print("chain-crit-part: S[%d]=",j); 
1985                 p_wrp(strat->S[j],currRing);
1986                 Print(" divide B[%d].lcm=",i);
1987                 p_wrp(strat->B[i].lcm,currRing);
1988                 PrintLn();
1989              }
1990              deleteInL(strat->B,&strat->Bl,i,strat);
1991              strat->c3++;
1992            }
1993          }
1994        }
1995      }
1996    }
1997    omFreeSize(strat->pairtest,(strat->sl+2)*sizeof(BOOLEAN));
1998    strat->pairtest=NULL;
1999  }
2000  if (strat->Gebauer || strat->fromT)
2001  {
2002    if (strat->sugarCrit)
2003    {
2004    /*
2005    *suppose L[j] == (s,r) and p/lcm(s,r)
2006    *and lcm(s,r)#lcm(s,p) and lcm(s,r)#lcm(r,p)
2007    *and in case the sugar is o.k. then L[j] can be canceled
2008    */
2009      for (j=strat->Ll; j>=0; j--)
2010      {
2011        if (sugarDivisibleBy(ecart,strat->L[j].ecart)
2012        && ((pNext(strat->L[j].p) == strat->tail) || (pOrdSgn==1))
2013        && pCompareChainPart(p,strat->L[j].p1,strat->L[j].p2,strat->L[j].lcm))
2014        {
2015          if (strat->L[j].p == strat->tail)
2016          {
2017              if(TEST_OPT_DEBUG)
2018              {
2019                 PrintS("chain-crit-part: pCompareChainPart p="); 
2020                 p_wrp(p,currRing);
2021                 Print(" delete L[%d]",j);
2022                 p_wrp(strat->L[j].lcm,currRing);
2023                 PrintLn();
2024              }
2025              deleteInL(strat->L,&strat->Ll,j,strat);
2026              strat->c3++;
2027          }
2028        }
2029      }
2030      /*
2031      *this is GEBAUER-MOELLER:
2032      *in B all elements with the same lcm except the "best"
2033      *(i.e. the last one in B with this property) will be canceled
2034      */
2035      j = strat->Bl;
2036      loop /*cannot be changed into a for !!! */
2037      {
2038        if (j <= 0) break;
2039        i = j-1;
2040        loop
2041        {
2042          if (i <  0) break;
2043          if (pLmEqual(strat->B[j].lcm,strat->B[i].lcm))
2044          {
2045            strat->c3++;
2046            if (sugarDivisibleBy(strat->B[j].ecart,strat->B[i].ecart))
2047            {
2048              if(TEST_OPT_DEBUG)
2049              {
2050                 Print("chain-crit-part: sugar B[%d].lcm=",j); 
2051                 p_wrp(strat->B[j].lcm,currRing);
2052                 Print(" delete B[%d]",i);
2053                 p_wrp(strat->B[i].lcm,currRing);
2054                 PrintLn();
2055              }
2056              deleteInL(strat->B,&strat->Bl,i,strat);
2057              j--;
2058            }
2059            else
2060            {
2061              if(TEST_OPT_DEBUG)
2062              {
2063                 Print("chain-crit-part: sugar B[%d].lcm=",i); 
2064                 p_wrp(strat->B[i].lcm,currRing);
2065                 Print(" delete B[%d]",j);
2066                 p_wrp(strat->B[j].lcm,currRing);
2067                 PrintLn();
2068              }
2069              deleteInL(strat->B,&strat->Bl,j,strat);
2070              break;
2071            }
2072          }
2073          i--;
2074        }
2075        j--;
2076      }
2077    }
2078    else /*sugarCrit*/
2079    {
2080      /*
2081      *suppose L[j] == (s,r) and p/lcm(s,r)
2082      *and lcm(s,r)#lcm(s,p) and lcm(s,r)#lcm(r,p)
2083      *and in case the sugar is o.k. then L[j] can be canceled
2084      */
2085      for (j=strat->Ll; j>=0; j--)
2086      {
2087        if (pCompareChainPart(p,strat->L[j].p1,strat->L[j].p2,strat->L[j].lcm))
2088        {
2089          if ((pNext(strat->L[j].p) == strat->tail)||(pOrdSgn==1))
2090          {
2091              if(TEST_OPT_DEBUG)
2092              {
2093                 PrintS("chain-crit-part: sugar:pCompareChainPart p="); 
2094                 p_wrp(p,currRing);
2095                 Print(" delete L[%d]",j);
2096                 p_wrp(strat->L[j].lcm,currRing);
2097                 PrintLn();
2098              }
2099            deleteInL(strat->L,&strat->Ll,j,strat);
2100            strat->c3++;
2101          }
2102        }
2103      }
2104      /*
2105      *this is GEBAUER-MOELLER:
2106      *in B all elements with the same lcm except the "best"
2107      *(i.e. the last one in B with this property) will be canceled
2108      */
2109      j = strat->Bl;
2110      loop   /*cannot be changed into a for !!! */
2111      {
2112        if (j <= 0) break;
2113        for(i=j-1; i>=0; i--)
2114        {
2115          if (pLmEqual(strat->B[j].lcm,strat->B[i].lcm))
2116          {
2117              if(TEST_OPT_DEBUG)
2118              {
2119                 Print("chain-crit-part: equal lcm B[%d].lcm=",j); 
2120                 p_wrp(strat->B[j].lcm,currRing);
2121                 Print(" delete B[%d]\n",i);
2122              }
2123            strat->c3++;
2124            deleteInL(strat->B,&strat->Bl,i,strat);
2125            j--;
2126          }
2127        }
2128        j--;
2129      }
2130    }
2131    /*
2132    *the elements of B enter L
2133    */
2134    kMergeBintoL(strat);
2135  }
2136  else
2137  {
2138    for (j=strat->Ll; j>=0; j--)
2139    {
2140      if (pCompareChainPart(p,strat->L[j].p1,strat->L[j].p2,strat->L[j].lcm))
2141      {
2142        if ((pNext(strat->L[j].p) == strat->tail)||(pOrdSgn==1))
2143        {
2144              if(TEST_OPT_DEBUG)
2145              {
2146                 PrintS("chain-crit-part: pCompareChainPart p="); 
2147                 p_wrp(p,currRing);
2148                 Print(" delete L[%d]",j);
2149                 p_wrp(strat->L[j].lcm,currRing);
2150                 PrintLn();
2151              }
2152          deleteInL(strat->L,&strat->Ll,j,strat);
2153          strat->c3++;
2154        }
2155      }
2156    }
2157    /*
2158    *this is our MODIFICATION of GEBAUER-MOELLER:
2159    *First the elements of B enter L,
2160    *then we fix a lcm and the "best" element in L
2161    *(i.e the last in L with this lcm and of type (s,p))
2162    *and cancel all the other elements of type (r,p) with this lcm
2163    *except the case the element (s,r) has also the same lcm
2164    *and is on the worst position with respect to (s,p) and (r,p)
2165    */
2166    /*
2167    *B enters to L/their order with respect to B is permutated for elements
2168    *B[i].p with the same leading term
2169    */
2170    kMergeBintoL(strat);
2171    j = strat->Ll;
2172    loop  /*cannot be changed into a for !!! */
2173    {
2174      if (j <= 0)
2175      {
2176        /*now L[0] cannot be canceled any more and the tail can be removed*/
2177        if (strat->L[0].p2 == strat->tail) strat->L[0].p2 = p;
2178        break;
2179      }
2180      if (strat->L[j].p2 == p)
2181      {
2182        i = j-1;
2183        loop
2184        {
2185          if (i < 0)  break;
2186          if ((strat->L[i].p2 == p) && pLmEqual(strat->L[j].lcm,strat->L[i].lcm))
2187          {
2188            /*L[i] could be canceled but we search for a better one to cancel*/
2189            strat->c3++;
2190            if (isInPairsetL(i-1,strat->L[j].p1,strat->L[i].p1,&l,strat)
2191            && (pNext(strat->L[l].p) == strat->tail)
2192            && (!pLmEqual(strat->L[i].p,strat->L[l].p))
2193            && _p_LmDivisibleByPart(p,currRing,
2194                           strat->L[l].lcm,currRing,
2195                           currRing->real_var_start, currRing->real_var_end))
2196
2197            {
2198              /*
2199              *"NOT equal(...)" because in case of "equal" the element L[l]
2200              *is "older" and has to be from theoretical point of view behind
2201              *L[i], but we do not want to reorder L
2202              */
2203              strat->L[i].p2 = strat->tail;
2204              /*
2205              *L[l] will be canceled, we cannot cancel L[i] later on,
2206              *so we mark it with "tail"
2207              */
2208              if(TEST_OPT_DEBUG)
2209              {
2210                 PrintS("chain-crit-part: divisible_by p="); 
2211                 p_wrp(p,currRing);
2212                 Print(" delete L[%d]",l);
2213                 p_wrp(strat->L[l].lcm,currRing);
2214                 PrintLn();
2215              }
2216              deleteInL(strat->L,&strat->Ll,l,strat);
2217              i--;
2218            }
2219            else
2220            {
2221              if(TEST_OPT_DEBUG)
2222              {
2223                 PrintS("chain-crit-part: divisible_by(2) p="); 
2224                 p_wrp(p,currRing);
2225                 Print(" delete L[%d]",i);
2226                 p_wrp(strat->L[i].lcm,currRing);
2227                 PrintLn();
2228              }
2229              deleteInL(strat->L,&strat->Ll,i,strat);
2230            }
2231            j--;
2232          }
2233          i--;
2234        }
2235      }
2236      else if (strat->L[j].p2 == strat->tail)
2237      {
2238        /*now L[j] cannot be canceled any more and the tail can be removed*/
2239        strat->L[j].p2 = p;
2240      }
2241      j--;
2242    }
2243  }
2244}
2245#endif
2246
2247/*2
2248*(s[0],h),...,(s[k],h) will be put to the pairset L
2249*/
2250void initenterpairs (poly h,int k,int ecart,int isFromQ,kStrategy strat, int atR = -1)
2251{
2252
2253  if ((strat->syzComp==0)
2254  || (pGetComp(h)<=strat->syzComp))
2255  {
2256    int j;
2257    BOOLEAN new_pair=FALSE;
2258
2259    if (pGetComp(h)==0)
2260    {
2261      /* for Q!=NULL: build pairs (f,q),(f1,f2), but not (q1,q2)*/
2262      if ((isFromQ)&&(strat->fromQ!=NULL))
2263      {
2264        for (j=0; j<=k; j++)
2265        {
2266          if (!strat->fromQ[j])
2267          {
2268            new_pair=TRUE;
2269            strat->enterOnePair(j,h,ecart,isFromQ,strat, atR);
2270          //Print("j:%d, Ll:%d\n",j,strat->Ll);
2271          }
2272        }
2273      }
2274      else
2275      {
2276        new_pair=TRUE;
2277        for (j=0; j<=k; j++)
2278        {
2279          strat->enterOnePair(j,h,ecart,isFromQ,strat, atR);
2280          //Print("j:%d, Ll:%d\n",j,strat->Ll);
2281        }
2282      }
2283    }
2284    else
2285    {
2286      for (j=0; j<=k; j++)
2287      {
2288        if ((pGetComp(h)==pGetComp(strat->S[j]))
2289        || (pGetComp(strat->S[j])==0))
2290        {
2291          new_pair=TRUE;
2292          strat->enterOnePair(j,h,ecart,isFromQ,strat, atR);
2293        //Print("j:%d, Ll:%d\n",j,strat->Ll);
2294        }
2295      }
2296    }
2297
2298    if (new_pair) 
2299    {
2300      strat->chainCrit(h,ecart,strat);
2301    }
2302  }
2303}
2304
2305#ifdef HAVE_RINGS
2306/*2
2307*the pairset B of pairs of type (s[i],p) is complete now. It will be updated
2308*using the chain-criterion in B and L and enters B to L
2309*/
2310void chainCritRing (poly p,int ecart,kStrategy strat)
2311{
2312  int i,j,l;
2313  /*
2314  *pairtest[i] is TRUE if spoly(S[i],p) == 0.
2315  *In this case all elements in B such
2316  *that their lcm is divisible by the leading term of S[i] can be canceled
2317  */
2318  if (strat->pairtest!=NULL)
2319  {
2320    {
2321      /*- i.e. there is an i with pairtest[i]==TRUE -*/
2322      for (j=0; j<=strat->sl; j++)
2323      {
2324        if (strat->pairtest[j])
2325        {
2326          for (i=strat->Bl; i>=0; i--)
2327          {
2328            if (pDivisibleBy(strat->S[j],strat->B[i].lcm))
2329            {
2330#ifdef KDEBUG
2331              if (TEST_OPT_DEBUG)
2332              {
2333                PrintS("--- chain criterion func chainCritRing type 1\n");
2334                PrintS("strat->S[j]:");
2335                wrp(strat->S[j]);
2336                PrintS("  strat->B[i].lcm:");
2337                wrp(strat->B[i].lcm);
2338                PrintLn();
2339              }
2340#endif
2341              deleteInL(strat->B,&strat->Bl,i,strat);
2342              strat->c3++;
2343            }
2344          }
2345        }
2346      }
2347    }
2348    omFreeSize(strat->pairtest,(strat->sl+2)*sizeof(BOOLEAN));
2349    strat->pairtest=NULL;
2350  }
2351  assume(!(strat->Gebauer || strat->fromT));
2352  for (j=strat->Ll; j>=0; j--)
2353  {
2354    if (strat->L[j].lcm != NULL && nDivBy(pGetCoeff(strat->L[j].lcm), pGetCoeff(p)))
2355    {
2356      if (pCompareChain(p,strat->L[j].p1,strat->L[j].p2,strat->L[j].lcm))
2357      {
2358        if ((pNext(strat->L[j].p) == strat->tail) || (pOrdSgn==1))
2359        {
2360          deleteInL(strat->L,&strat->Ll,j,strat);
2361          strat->c3++;
2362#ifdef KDEBUG
2363              if (TEST_OPT_DEBUG)
2364              {
2365                PrintS("--- chain criterion func chainCritRing type 2\n");
2366                PrintS("strat->L[j].p:");
2367                wrp(strat->L[j].p);
2368                PrintS("  p:");
2369                wrp(p);
2370                PrintLn();
2371              }
2372#endif
2373        }
2374      }
2375    }
2376  }
2377  /*
2378  *this is our MODIFICATION of GEBAUER-MOELLER:
2379  *First the elements of B enter L,
2380  *then we fix a lcm and the "best" element in L
2381  *(i.e the last in L with this lcm and of type (s,p))
2382  *and cancel all the other elements of type (r,p) with this lcm
2383  *except the case the element (s,r) has also the same lcm
2384  *and is on the worst position with respect to (s,p) and (r,p)
2385  */
2386  /*
2387  *B enters to L/their order with respect to B is permutated for elements
2388  *B[i].p with the same leading term
2389  */
2390  kMergeBintoL(strat);
2391  j = strat->Ll;
2392  loop  /*cannot be changed into a for !!! */
2393  {
2394    if (j <= 0)
2395    {
2396      /*now L[0] cannot be canceled any more and the tail can be removed*/
2397      if (strat->L[0].p2 == strat->tail) strat->L[0].p2 = p;
2398      break;
2399    }
2400    if (strat->L[j].p2 == p) // Was the element added from B?
2401    {
2402      i = j-1;
2403      loop
2404      {
2405        if (i < 0)  break;
2406        // Element is from B and has the same lcm as L[j]
2407        if ((strat->L[i].p2 == p) && nDivBy(pGetCoeff(strat->L[j].lcm), pGetCoeff(strat->L[i].lcm))
2408             && pLmEqual(strat->L[j].lcm,strat->L[i].lcm))
2409        {
2410          /*L[i] could be canceled but we search for a better one to cancel*/
2411          strat->c3++;
2412#ifdef KDEBUG
2413          if (TEST_OPT_DEBUG)
2414          {
2415            PrintS("--- chain criterion func chainCritRing type 3\n");
2416            PrintS("strat->L[j].lcm:");
2417            wrp(strat->L[j].lcm);
2418            PrintS("  strat->L[i].lcm:");
2419            wrp(strat->L[i].lcm);
2420            PrintLn();
2421          }
2422#endif
2423          if (isInPairsetL(i-1,strat->L[j].p1,strat->L[i].p1,&l,strat)
2424          && (pNext(strat->L[l].p) == strat->tail)
2425          && (!pLmEqual(strat->L[i].p,strat->L[l].p))
2426          && pDivisibleBy(p,strat->L[l].lcm))
2427          {
2428            /*
2429            *"NOT equal(...)" because in case of "equal" the element L[l]
2430            *is "older" and has to be from theoretical point of view behind
2431            *L[i], but we do not want to reorder L
2432            */
2433            strat->L[i].p2 = strat->tail;
2434            /*
2435            *L[l] will be canceled, we cannot cancel L[i] later on,
2436            *so we mark it with "tail"
2437            */
2438            deleteInL(strat->L,&strat->Ll,l,strat);
2439            i--;
2440          }
2441          else
2442          {
2443            deleteInL(strat->L,&strat->Ll,i,strat);
2444          }
2445          j--;
2446        }
2447        i--;
2448      }
2449    }
2450    else if (strat->L[j].p2 == strat->tail)
2451    {
2452      /*now L[j] cannot be canceled any more and the tail can be removed*/
2453      strat->L[j].p2 = p;
2454    }
2455    j--;
2456  }
2457}
2458#endif
2459
2460#ifdef HAVE_RING2TOM
2461long ind2(long arg)
2462{
2463  long ind = 0;
2464  if (arg <= 0) return 0;
2465  while (arg%2 == 0)
2466  {
2467    arg = arg / 2;
2468    ind++;
2469  }
2470  return ind;
2471}
2472
2473long ind_fact_2(long arg)
2474{
2475  long ind = 0;
2476  if (arg <= 0) return 0;
2477  if (arg%2 == 1) { arg--; }
2478  while (arg > 0)
2479  {
2480    ind += ind2(arg);
2481    arg = arg - 2;
2482  }
2483  return ind;
2484}
2485#endif
2486
2487#ifdef HAVE_VANIDEAL
2488long twoPow(long arg)
2489{
2490  return 1L << arg;
2491}
2492
2493/*2
2494* put the pair (p, f) in B and f in T
2495*/
2496void enterOneZeroPairRing (poly f, poly t_p, poly p, int ecart, kStrategy strat, int atR = -1)
2497{
2498  int      l,j,compare,compareCoeff;
2499  LObject  Lp;
2500
2501  if (strat->interred_flag) return;
2502#ifdef KDEBUG
2503  Lp.ecart=0; Lp.length=0;
2504#endif
2505  /*- computes the lcm(s[i],p) -*/
2506  Lp.lcm = pInit();
2507
2508  pLcm(p,f,Lp.lcm);
2509  pSetm(Lp.lcm);
2510  pSetCoeff(Lp.lcm, nLcm(pGetCoeff(p), pGetCoeff(f), currRing));
2511  assume(!strat->sugarCrit);
2512  assume(!strat->fromT);
2513  /*
2514  *the set B collects the pairs of type (S[j],p)
2515  *suppose (r,p) is in B and (s,p) is the new pair and lcm(s,p) != lcm(r,p)
2516  *if the leading term of s devides lcm(r,p) then (r,p) will be canceled
2517  *if the leading term of r devides lcm(s,p) then (s,p) will not enter B
2518  */
2519  for(j = strat->Bl;j>=0;j--)
2520  {
2521    compare=pDivCompRing(strat->B[j].lcm,Lp.lcm);
2522    compareCoeff = nDivComp(pGetCoeff(strat->B[j].lcm), pGetCoeff(Lp.lcm));
2523    if (compareCoeff == 0 || compare == compareCoeff)
2524    {
2525      if (compare == 1)
2526      {
2527        strat->c3++;
2528        pLmDelete(Lp.lcm);
2529        return;
2530      }
2531      else
2532      if (compare == -1)
2533      {
2534        deleteInL(strat->B,&strat->Bl,j,strat);
2535        strat->c3++;
2536      }
2537    }
2538    if (compare == pDivComp_EQUAL)
2539    {
2540      // Add hint for same LM and direction of LC (later) (TODO Oliver)
2541      if (compareCoeff == 1)
2542      {
2543        strat->c3++;
2544        pLmDelete(Lp.lcm);
2545        return;
2546      }
2547      else
2548      if (compareCoeff == -1)
2549      {
2550        deleteInL(strat->B,&strat->Bl,j,strat);
2551        strat->c3++;
2552      }
2553    }
2554  }
2555  /*
2556  *the pair (S[i],p) enters B if the spoly != 0
2557  */
2558  /*-  compute the short s-polynomial -*/
2559  if ((f==NULL) || (p==NULL)) return;
2560  pNorm(p);
2561  {
2562    Lp.p = ksCreateShortSpoly(f, p, strat->tailRing);
2563  }
2564  if (Lp.p == NULL) //deactivated, as we are adding pairs with zeropoly and not from S
2565  {
2566    /*- the case that the s-poly is 0 -*/
2567//    if (strat->pairtest==NULL) initPairtest(strat);
2568//    strat->pairtest[i] = TRUE;/*- hint for spoly(S^[i],p)=0 -*/
2569//    strat->pairtest[strat->sl+1] = TRUE;
2570    /*hint for spoly(S[i],p) == 0 for some i,0 <= i <= sl*/
2571    /*
2572    *suppose we have (s,r),(r,p),(s,p) and spoly(s,p) == 0 and (r,p) is
2573    *still in B (i.e. lcm(r,p) == lcm(s,p) or the leading term of s does not
2574    *devide lcm(r,p)). In the last case (s,r) can be canceled if the leading
2575    *term of p devides the lcm(s,r)
2576    *(this canceling should be done here because
2577    *the case lcm(s,p) == lcm(s,r) is not covered in chainCrit)
2578    *the first case is handeled in chainCrit
2579    */
2580    if (Lp.lcm!=NULL) pLmDelete(Lp.lcm);
2581  }
2582  else
2583  {
2584    /*- the pair (S[i],p) enters B -*/
2585    Lp.p1 = f;
2586    Lp.p2 = p;
2587
2588    pNext(Lp.p) = strat->tail;
2589
2590    LObject tmp_h(f, currRing, strat->tailRing);
2591    tmp_h.SetShortExpVector();
2592    strat->initEcart(&tmp_h);
2593    tmp_h.sev = pGetShortExpVector(tmp_h.p);
2594    tmp_h.t_p = t_p;
2595
2596    enterT(tmp_h, strat, strat->tl + 1);
2597
2598    if (atR >= 0)
2599    {
2600      Lp.i_r2 = atR;
2601      Lp.i_r1 = strat->tl;
2602    }
2603
2604    strat->initEcartPair(&Lp,f,p,0/*strat->ecartS[i]*/,ecart);     // Attention: TODO: break ecart
2605    l = strat->posInL(strat->B,strat->Bl,&Lp,strat);
2606    enterL(&strat->B, &strat->Bl, &strat->Bmax, Lp, l);
2607  }
2608}
2609
2610/* Helper for kCreateZeroPoly
2611 * enumerating the exponents
2612ring r = 2^2, (a, b, c), lp; ideal G0 = system("createG0"); ideal G = interred(G0); ncols(G0); ncols(G);
2613 */
2614
2615int nextZeroSimplexExponent (long exp[], long ind[], long cexp[], long cind[], long* cabsind, long step[], long bound, long N)
2616/* gives the next exponent from the set H_1 */
2617{
2618  long add = ind2(cexp[1] + 2);
2619  if ((*cabsind < bound) && (*cabsind - step[1] + add < bound))
2620  {
2621    cexp[1] += 2;
2622    cind[1] += add;
2623    *cabsind += add;
2624  }
2625  else
2626  {
2627    // cabsind >= habsind
2628    if (N == 1) return 0;
2629    int i = 1;
2630    while (exp[i] == cexp[i] && i <= N) i++;
2631    cexp[i] = exp[i];
2632    *cabsind -= cind[i];
2633    cind[i] = ind[i];
2634    step[i] = 500000;
2635    *cabsind += cind[i];
2636    // Print("in: %d\n", *cabsind);
2637    i += 1;
2638    if (i > N) return 0;
2639    do
2640    {
2641      step[1] = 500000;
2642      for (int j = i + 1; j <= N; j++)
2643      {
2644        if (step[1] > step[j]) step[1] = step[j];
2645      }
2646      add = ind2(cexp[i] + 2);
2647      if (*cabsind - step[1] + add >= bound)
2648      {
2649        cexp[i] = exp[i];
2650        *cabsind -= cind[i];
2651        cind[i] = ind[i];
2652        *cabsind += cind[i];
2653        step[i] = 500000;
2654        i += 1;
2655        if (i > N) return 0;
2656      }
2657      else step[1] = -1;
2658    } while (step[1] != -1);
2659    step[1] = 500000;
2660    cexp[i] += 2;
2661    cind[i] += add;
2662    *cabsind += add;
2663    if (add < step[i]) step[i] = add;
2664    for (i = 2; i <= N; i++)
2665    {
2666      if (step[1] > step[i]) step[1] = step[i];
2667    }
2668  }
2669  return 1;
2670}
2671
2672/*
2673 * Creates the zero Polynomial on position exp
2674 * long exp[] : exponent of leading term
2675 * cabsind    : total 2-ind of exp (if -1 will be computed)
2676 * poly* t_p  : will hold the LT in tailRing
2677 * leadRing   : ring for the LT
2678 * tailRing   : ring for the tail
2679 */
2680
2681poly kCreateZeroPoly(long exp[], long cabsind, poly* t_p, ring leadRing, ring tailRing)
2682{
2683
2684  poly zeroPoly = NULL;
2685
2686  number tmp1;
2687  poly tmp2, tmp3;
2688
2689  if (cabsind == -1)
2690  {
2691    cabsind = 0;
2692    for (int i = 1; i <= leadRing->N; i++)
2693    {
2694      cabsind += ind_fact_2(exp[i]);
2695    }
2696//    Print("cabsind: %d\n", cabsind);
2697  }
2698  if (cabsind < leadRing->ch)
2699  {
2700    zeroPoly = p_ISet(twoPow(leadRing->ch - cabsind), tailRing);
2701  }
2702  else
2703  {
2704    zeroPoly = p_ISet(1, tailRing);
2705  }
2706  for (int i = 1; i <= leadRing->N; i++)
2707  {
2708    for (long j = 1; j <= exp[i]; j++)
2709    {
2710      tmp1 = nInit(j);
2711      tmp2 = p_ISet(1, tailRing);
2712      p_SetExp(tmp2, i, 1, tailRing);
2713      p_Setm(tmp2, tailRing);
2714      if (nIsZero(tmp1))
2715      { // should nowbe obsolet, test ! TODO OLIVER
2716        zeroPoly = p_Mult_q(zeroPoly, tmp2, tailRing);
2717      }
2718      else
2719      {
2720        tmp3 = p_NSet(nCopy(tmp1), tailRing);
2721        zeroPoly = p_Mult_q(zeroPoly, p_Add_q(tmp3, tmp2, tailRing), tailRing);
2722      }
2723    }
2724  }
2725  tmp2 = p_NSet(nCopy(pGetCoeff(zeroPoly)), leadRing);
2726  for (int i = 1; i <= leadRing->N; i++)
2727  {
2728    pSetExp(tmp2, i, p_GetExp(zeroPoly, i, tailRing));
2729  }
2730  p_Setm(tmp2, leadRing);
2731  *t_p = zeroPoly;
2732  zeroPoly = pNext(zeroPoly);
2733  pNext(*t_p) = NULL;
2734  pNext(tmp2) = zeroPoly;
2735  return tmp2;
2736}
2737
2738// #define OLI_DEBUG
2739
2740/*
2741 * Generate the s-polynomial for the virtual set of zero-polynomials
2742 */
2743
2744void initenterzeropairsRing (poly p, int ecart, kStrategy strat, int atR)
2745{
2746  // Initialize
2747  long exp[50];            // The exponent of \hat{X} (basepoint)
2748  long cexp[50];           // The current exponent for iterating over all
2749  long ind[50];            // The power of 2 in the i-th component of exp
2750  long cind[50];           // analog for cexp
2751  long mult[50];           // How to multiply the elements of G
2752  long cabsind = 0;        // The abs. index of cexp, i.e. the sum of cind
2753  long habsind = 0;        // The abs. index of the coefficient of h
2754  long step[50];           // The last increases
2755  for (int i = 1; i <= currRing->N; i++)
2756  {
2757    exp[i] = p_GetExp(p, i, currRing);
2758    if (exp[i] & 1 != 0)
2759    {
2760      exp[i] = exp[i] - 1;
2761      mult[i] = 1;
2762    }
2763    cexp[i] = exp[i];
2764    ind[i] = ind_fact_2(exp[i]);
2765    cabsind += ind[i];
2766    cind[i] = ind[i];
2767    step[i] = 500000;
2768  }
2769  step[1] = 500000;
2770  habsind = ind2((long) p_GetCoeff(p, currRing));
2771  long bound = currRing->ch - habsind;
2772#ifdef OLI_DEBUG
2773  PrintS("-------------\npoly  :");
2774  wrp(p);
2775  Print("\nexp   : (%d, %d)\n", exp[1] + mult[1], exp[2] + mult[1]);
2776  Print("cexp  : (%d, %d)\n", cexp[1], cexp[2]);
2777  Print("cind  : (%d, %d)\n", cind[1], cind[2]);
2778  Print("bound : %d\n", bound);
2779  Print("cind  : %d\n", cabsind);
2780#endif
2781  if (cabsind == 0)
2782  {
2783    if (!(nextZeroSimplexExponent(exp, ind, cexp, cind, &cabsind, step, bound, currRing->N)))
2784    {
2785      return;
2786    }
2787  }
2788  // Now the whole simplex
2789  do
2790  {
2791    // Build s-polynomial
2792    // 2**ind-def * mult * g - exp-def * h
2793    poly t_p;
2794    poly zeroPoly = kCreateZeroPoly(cexp, cabsind, &t_p, currRing, strat->tailRing);
2795#ifdef OLI_DEBUG
2796    Print("%d, (%d, %d), ind = (%d, %d)\n", cabsind, cexp[1], cexp[2], cind[1], cind[2]);
2797    Print("zPoly : ");
2798    wrp(zeroPoly);
2799    Print("\n");
2800#endif
2801    enterOneZeroPairRing(zeroPoly, t_p, p, ecart, strat, atR);
2802  }
2803  while ( nextZeroSimplexExponent(exp, ind, cexp, cind, &cabsind, step, bound, currRing->N) );
2804}
2805
2806/*
2807 * Create the Groebner basis of the vanishing polynomials.
2808 */
2809
2810ideal createG0()
2811{
2812  // Initialize
2813  long exp[50];            // The exponent of \hat{X} (basepoint)
2814  long cexp[50];           // The current exponent for iterating over all
2815  long ind[50];            // The power of 2 in the i-th component of exp
2816  long cind[50];           // analog for cexp
2817  long mult[50];           // How to multiply the elements of G
2818  long cabsind = 0;        // The abs. index of cexp, i.e. the sum of cind
2819  long habsind = 0;        // The abs. index of the coefficient of h
2820  long step[50];           // The last increases
2821  for (int i = 1; i <= currRing->N; i++)
2822  {
2823    exp[i] = 0;
2824    cexp[i] = exp[i];
2825    ind[i] = 0;
2826    step[i] = 500000;
2827    cind[i] = ind[i];
2828  }
2829  long bound = currRing->ch;
2830  step[1] = 500000;
2831#ifdef OLI_DEBUG
2832  PrintS("-------------\npoly  :");
2833//  wrp(p);
2834  Print("\nexp   : (%d, %d)\n", exp[1] + mult[1], exp[2] + mult[1]);
2835  Print("cexp  : (%d, %d)\n", cexp[1], cexp[2]);
2836  Print("cind  : (%d, %d)\n", cind[1], cind[2]);
2837  Print("bound : %d\n", bound);
2838  Print("cind  : %d\n", cabsind);
2839#endif
2840  if (cabsind == 0)
2841  {
2842    if (!(nextZeroSimplexExponent(exp, ind, cexp, cind, &cabsind, step, bound, currRing->N)))
2843    {
2844      return idInit(1, 1);
2845    }
2846  }
2847  ideal G0 = idInit(1, 1);
2848  // Now the whole simplex
2849  do
2850  {
2851    // Build s-polynomial
2852    // 2**ind-def * mult * g - exp-def * h
2853    poly t_p;
2854    poly zeroPoly = kCreateZeroPoly(cexp, cabsind, &t_p, currRing, currRing);
2855#ifdef OLI_DEBUG
2856    Print("%d, (%d, %d), ind = (%d, %d)\n", cabsind, cexp[1], cexp[2], cind[1], cind[2]);
2857    Print("zPoly : ");
2858    wrp(zeroPoly);
2859    Print("\n");
2860#endif
2861    // Add to ideal
2862    pEnlargeSet(&(G0->m), IDELEMS(G0), 1);
2863    IDELEMS(G0) += 1;
2864    G0->m[IDELEMS(G0) - 1] = zeroPoly;
2865  }
2866  while ( nextZeroSimplexExponent(exp, ind, cexp, cind, &cabsind, step, bound, currRing->N) );
2867  idSkipZeroes(G0);
2868  return G0;
2869}
2870#endif
2871
2872#ifdef HAVE_RINGS
2873/*2
2874*(s[0],h),...,(s[k],h) will be put to the pairset L
2875*/
2876void initenterstrongPairs (poly h,int k,int ecart,int isFromQ,kStrategy strat, int atR = -1)
2877{
2878
2879  if (!nIsOne(pGetCoeff(h)))
2880  {
2881    int j;
2882    BOOLEAN new_pair=FALSE;
2883
2884    for (j=0; j<=k; j++)
2885    {
2886      // Print("j:%d, Ll:%d\n",j,strat->Ll);
2887//      if (((unsigned long) pGetCoeff(h) % (unsigned long) pGetCoeff(strat->S[j]) != 0) &&
2888//         ((unsigned long) pGetCoeff(strat->S[j]) % (unsigned long) pGetCoeff(h) != 0))
2889      {
2890        if (enterOneStrongPoly(j,h,ecart,isFromQ,strat, atR))
2891          new_pair=TRUE;
2892      }
2893    }
2894  }
2895/*
2896ring r=256,(x,y,z),dp;
2897ideal I=12xz-133y, 2xy-z;
2898*/
2899
2900}
2901
2902/*2
2903* Generates spoly(0, h) if applicable. Assumes ring in Z/2^n.
2904*/
2905void enterExtendedSpoly(poly h,kStrategy strat)
2906{
2907  if (nIsOne(pGetCoeff(h))) return;
2908  number gcd;
2909  bool go = false;
2910  if (nDivBy((number) 0, pGetCoeff(h)))
2911  {
2912    gcd = nIntDiv((number) 0, pGetCoeff(h));
2913    go = true;
2914  }
2915  else
2916    gcd = nGcd((number) 0, pGetCoeff(h), strat->tailRing);
2917  if (go || !nIsOne(gcd))
2918  {
2919    poly p = h->next;
2920    if (!go)
2921    {
2922      number tmp = gcd;
2923      gcd = nIntDiv(0, gcd);
2924      nDelete(&tmp);
2925    }
2926    p = pp_Mult_nn(p, gcd, strat->tailRing);
2927    nDelete(&gcd);
2928
2929    if (p != NULL)
2930    {
2931      if (TEST_OPT_PROT)
2932      {
2933        PrintS("Z");
2934      }
2935#ifdef KDEBUG
2936      if (TEST_OPT_DEBUG)
2937      {
2938        PrintS("--- create zero spoly: ");
2939        wrp(h);
2940        PrintS(" ---> ");
2941      }
2942#endif
2943      poly tmp = pInit();
2944      pSetCoeff0(tmp, pGetCoeff(p));
2945      for (int i = 1; i <= currRing->N; i++)
2946      {
2947        pSetExp(tmp, i, p_GetExp(p, i, strat->tailRing));
2948      }
2949      if (rRing_has_Comp(currRing))
2950        p_SetComp(tmp, p_GetComp(p, strat->tailRing), currRing);
2951      p_Setm(tmp, currRing);
2952      p = p_LmFreeAndNext(p, strat->tailRing);
2953      pNext(tmp) = p;
2954      LObject h;
2955      h.p = tmp;
2956      h.tailRing = strat->tailRing;
2957      int posx;
2958      if (h.p!=NULL)
2959      {
2960        if (TEST_OPT_INTSTRATEGY)
2961        {
2962          //pContent(h.p);
2963          h.pCleardenom(); // also does a pContent
2964        }
2965        else
2966        {
2967          h.pNorm();
2968        }
2969        strat->initEcart(&h);
2970        if (strat->Ll==-1)
2971          posx =0;
2972        else
2973          posx = strat->posInL(strat->L,strat->Ll,&h,strat);
2974        h.sev = pGetShortExpVector(h.p);
2975        if (strat->tailRing != currRing)
2976          h.t_p = k_LmInit_currRing_2_tailRing(h.p, strat->tailRing);
2977        if (pNext(p) != NULL)
2978        {
2979          // What does this? (Oliver)
2980          // pShallowCopyDeleteProc p_shallow_copy_delete
2981          //      = pGetShallowCopyDeleteProc(strat->tailRing, new_tailRing);
2982          // pNext(p) = p_shallow_copy_delete(pNext(p),
2983          //              currRing, strat->tailRing, strat->tailRing->PolyBin);
2984        }
2985#ifdef KDEBUG
2986        if (TEST_OPT_DEBUG)
2987        {
2988          wrp(tmp);
2989          PrintLn();
2990        }
2991#endif
2992        enterL(&strat->L,&strat->Ll,&strat->Lmax,h,posx);
2993      }
2994    }
2995  }
2996  nDelete(&gcd);
2997}
2998
2999void clearSbatch (poly h,int k,int pos,kStrategy strat)
3000{
3001  int j = pos;
3002  if ( (!strat->fromT)
3003  && (1//(strat->syzComp==0)
3004    //||(pGetComp(h)<=strat->syzComp)))
3005  ))
3006  {
3007    // Print("start clearS k=%d, pos=%d, sl=%d\n",k,pos,strat->sl);
3008    unsigned long h_sev = pGetShortExpVector(h);
3009    loop
3010    {
3011      if (j > k) break;
3012      clearS(h,h_sev, &j,&k,strat);
3013      j++;
3014    }
3015    // Print("end clearS sl=%d\n",strat->sl);
3016  }
3017}
3018
3019/*2
3020* Generates a sufficient set of spolys (maybe just a finite generating
3021* set of the syzygys)
3022*/
3023void superenterpairs (poly h,int k,int ecart,int pos,kStrategy strat, int atR)
3024{
3025    assume (rField_is_Ring(currRing));
3026    // enter also zero divisor * poly, if this is non zero and of smaller degree
3027    if (!(rField_is_Domain(currRing))) enterExtendedSpoly(h, strat);
3028    initenterpairs(h, k, ecart, 0, strat, atR);
3029    initenterstrongPairs(h, k, ecart, 0, strat, atR);
3030    clearSbatch(h, k, pos, strat);
3031}
3032#endif
3033
3034/*2
3035*(s[0],h),...,(s[k],h) will be put to the pairset L(via initenterpairs)
3036*superfluous elements in S will be deleted
3037*/
3038void enterpairs (poly h,int k,int ecart,int pos,kStrategy strat, int atR)
3039{
3040  int j=pos;
3041
3042#ifdef HAVE_RINGS
3043  assume (!rField_is_Ring(currRing));
3044#endif
3045
3046  initenterpairs(h,k,ecart,0,strat, atR);
3047  if ( (!strat->fromT)
3048  && ((strat->syzComp==0)
3049    ||(pGetComp(h)<=strat->syzComp)))
3050  {
3051    //Print("start clearS k=%d, pos=%d, sl=%d\n",k,pos,strat->sl);
3052    unsigned long h_sev = pGetShortExpVector(h);
3053    loop
3054    {
3055      if (j > k) break;
3056      clearS(h,h_sev, &j,&k,strat);
3057      j++;
3058    }
3059    //Print("end clearS sl=%d\n",strat->sl);
3060  }
3061 // PrintS("end enterpairs\n");
3062}
3063
3064/*2
3065*(s[0],h),...,(s[k],h) will be put to the pairset L(via initenterpairs)
3066*superfluous elements in S will be deleted
3067*/
3068void enterpairsSpecial (poly h,int k,int ecart,int pos,kStrategy strat, int atR = -1)
3069{
3070  int j;
3071
3072  for (j=0; j<=k; j++)
3073  {
3074    if ((pGetComp(h)==pGetComp(strat->S[j]))
3075    || (0==pGetComp(strat->S[j])))
3076    {
3077      enterOnePairSpecial(j,h,ecart,strat, atR);
3078    }
3079  }
3080//   #ifdef HAVE_PLURAL
3081  if (!rIsPluralRing(currRing))
3082//   #endif
3083  {
3084    j=pos;
3085    loop
3086    {
3087      unsigned long h_sev = pGetShortExpVector(h);
3088      if (j > k) break;
3089      clearS(h,h_sev,&j,&k,strat);
3090      j++;
3091    }
3092  }
3093}
3094
3095/*2
3096*reorders  s with respect to posInS,
3097*suc is the first changed index or zero
3098*/
3099
3100void reorderS (int* suc,kStrategy strat)
3101{
3102  int i,j,at,ecart, s2r;
3103  int fq=0;
3104  unsigned long sev;
3105  poly  p;
3106  int new_suc=strat->sl+1;
3107  i= *suc;
3108  if (i<0) i=0;
3109
3110  for (; i<=strat->sl; i++)
3111  {
3112    at = posInS(strat,i-1,strat->S[i],strat->ecartS[i]);
3113    if (at != i)
3114    {
3115      if (new_suc > at) new_suc = at;
3116      p = strat->S[i];
3117      ecart = strat->ecartS[i];
3118      sev = strat->sevS[i];
3119      s2r = strat->S_2_R[i];
3120      if (strat->fromQ!=NULL) fq=strat->fromQ[i];
3121      for (j=i; j>=at+1; j--)
3122      {
3123        strat->S[j] = strat->S[j-1];
3124        strat->ecartS[j] = strat->ecartS[j-1];
3125        strat->sevS[j] = strat->sevS[j-1];
3126        strat->S_2_R[j] = strat->S_2_R[j-1];
3127      }
3128      strat->S[at] = p;
3129      strat->ecartS[at] = ecart;
3130      strat->sevS[at] = sev;
3131      strat->S_2_R[at] = s2r;
3132      if (strat->fromQ!=NULL)
3133      {
3134        for (j=i; j>=at+1; j--)
3135        {
3136          strat->fromQ[j] = strat->fromQ[j-1];
3137        }
3138        strat->fromQ[at]=fq;
3139      }
3140    }
3141  }
3142  if (new_suc <= strat->sl) *suc=new_suc;
3143  else                      *suc=-1;
3144}
3145
3146
3147/*2
3148*looks up the position of p in set
3149*set[0] is the smallest with respect to the ordering-procedure deg/pComp
3150* Assumption: posInS only depends on the leading term
3151*             otherwise, bba has to be changed
3152*/
3153int posInS (const kStrategy strat, const int length,const poly p,
3154            const int ecart_p)
3155{
3156  if(length==-1) return 0;
3157  polyset set=strat->S;
3158  int i;
3159  int an = 0;
3160  int en = length;
3161  int cmp_int = pOrdSgn;
3162  int pc=pGetComp(p);
3163  if ((currRing->MixedOrder)
3164  && (currRing->real_var_start==0)
3165#if 0
3166  || ((strat->ak>0) && ((currRing->order[0]==ringorder_c)||((currRing->order[0]==ringorder_C))))
3167#endif
3168  )
3169  {
3170    int o=pWTotaldegree(p);
3171    int oo=pWTotaldegree(set[length]);
3172
3173    if ((oo<o)
3174    || ((o==oo) && (pLmCmp(set[length],p)!= cmp_int)))
3175      return length+1;
3176
3177    loop
3178    {
3179      if (an >= en-1)
3180      {
3181        if ((pWTotaldegree(set[an])>=o) && (pLmCmp(set[an],p) == cmp_int))
3182        {
3183          return an;
3184        }
3185        return en;
3186      }
3187      i=(an+en) / 2;
3188      if ((pWTotaldegree(set[i])>=o) && (pLmCmp(set[i],p) == cmp_int)) en=i;
3189      else                              an=i;
3190    }
3191  }
3192  else
3193  {
3194#ifdef HAVE_RINGS
3195    if (rField_is_Ring(currRing))
3196    {
3197      if (pLmCmp(set[length],p)== -cmp_int)
3198        return length+1;
3199      int cmp;
3200      loop
3201      {
3202        if (an >= en-1)
3203        {
3204          cmp = pLmCmp(set[an],p);
3205          if (cmp == cmp_int)  return an;
3206          if (cmp == -cmp_int) return en;
3207          if (nDivBy(pGetCoeff(p), pGetCoeff(set[an]))) return en;
3208          return an;
3209        }
3210        i = (an+en) / 2;
3211        cmp = pLmCmp(set[i],p);
3212        if (cmp == cmp_int)         en = i;
3213        else if (cmp == -cmp_int)   an = i;
3214        else
3215        {
3216          if (nDivBy(pGetCoeff(p), pGetCoeff(set[i]))) an = i;
3217          else en = i;
3218        }
3219      }
3220    }
3221    else
3222#endif
3223    if (pLmCmp(set[length],p)== -cmp_int)
3224      return length+1;
3225
3226    loop
3227    {
3228      if (an >= en-1)
3229      {
3230        if (pLmCmp(set[an],p) == cmp_int) return an;
3231        if (pLmCmp(set[an],p) == -cmp_int) return en;
3232        if ((cmp_int!=1)
3233        && ((strat->ecartS[an])>ecart_p))
3234          return an;
3235        return en;
3236      }
3237      i=(an+en) / 2;
3238      if (pLmCmp(set[i],p) == cmp_int) en=i;
3239      else if (pLmCmp(set[i],p) == -cmp_int) an=i;
3240      else
3241      {
3242        if ((cmp_int!=1)
3243        &&((strat->ecartS[i])<ecart_p))
3244          en=i;
3245        else
3246          an=i;
3247      }
3248    }
3249  }
3250}
3251
3252
3253/*2
3254* looks up the position of p in set
3255* the position is the last one
3256*/
3257int posInT0 (const TSet set,const int length,LObject &p)
3258{
3259  return (length+1);
3260}
3261
3262
3263/*2
3264* looks up the position of p in T
3265* set[0] is the smallest with respect to the ordering-procedure
3266* pComp
3267*/
3268int posInT1 (const TSet set,const int length,LObject &p)
3269{
3270  if (length==-1) return 0;
3271
3272  if (pLmCmp(set[length].p,p.p)!= pOrdSgn) return length+1;
3273
3274  int i;
3275  int an = 0;
3276  int en= length;
3277
3278  loop
3279  {
3280    if (an >= en-1)
3281    {
3282      if (pLmCmp(set[an].p,p.p) == pOrdSgn) return an;
3283      return en;
3284    }
3285    i=(an+en) / 2;
3286    if (pLmCmp(set[i].p,p.p) == pOrdSgn) en=i;
3287    else                                 an=i;
3288  }
3289}
3290
3291/*2
3292* looks up the position of p in T
3293* set[0] is the smallest with respect to the ordering-procedure
3294* length
3295*/
3296int posInT2 (const TSet set,const int length,LObject &p)
3297{
3298  if (length==-1)
3299    return 0;
3300  if (set[length].length<p.length)
3301    return length+1;
3302
3303  int i;
3304  int an = 0;
3305  int en= length;
3306
3307  loop
3308  {
3309    if (an >= en-1)
3310    {
3311      if (set[an].length>p.length) return an;
3312      return en;
3313    }
3314    i=(an+en) / 2;
3315    if (set[i].length>p.length) en=i;
3316    else                        an=i;
3317  }
3318}
3319
3320/*2
3321* looks up the position of p in T
3322* set[0] is the smallest with respect to the ordering-procedure
3323* totaldegree,pComp
3324*/
3325int posInT11 (const TSet set,const int length,LObject &p)
3326/*{
3327 * int j=0;
3328 * int o;
3329 *
3330 * o = p.GetpFDeg();
3331 * loop
3332 * {
3333 *   if ((pFDeg(set[j].p) > o)
3334 *   || ((pFDeg(set[j].p) == o) && (pLmCmp(set[j].p,p.p) == pOrdSgn)))
3335 *   {
3336 *     return j;
3337 *   }
3338 *   j++;
3339 *   if (j > length) return j;
3340 * }
3341 *}
3342 */
3343{
3344  if (length==-1) return 0;
3345
3346  int o = p.GetpFDeg();
3347  int op = set[length].GetpFDeg();
3348
3349  if ((op < o)
3350  || ((op == o) && (pLmCmp(set[length].p,p.p) != pOrdSgn)))
3351    return length+1;
3352
3353  int i;
3354  int an = 0;
3355  int en= length;
3356
3357  loop
3358  {
3359    if (an >= en-1)
3360    {
3361      op= set[an].GetpFDeg();
3362      if ((op > o)
3363      || (( op == o) && (pLmCmp(set[an].p,p.p) == pOrdSgn)))
3364        return an;
3365      return en;
3366    }
3367    i=(an+en) / 2;
3368    op = set[i].GetpFDeg();
3369    if (( op > o)
3370    || (( op == o) && (pLmCmp(set[i].p,p.p) == pOrdSgn)))
3371      en=i;
3372    else
3373      an=i;
3374  }
3375}
3376
3377/*2 Pos for rings T: Here I am
3378* looks up the position of p in T
3379* set[0] is the smallest with respect to the ordering-procedure
3380* totaldegree,pComp
3381*/
3382int posInTrg0 (const TSet set,const int length,LObject &p)
3383{
3384  if (length==-1) return 0;
3385  int o = p.GetpFDeg();
3386  int op = set[length].GetpFDeg();
3387  int i;
3388  int an = 0;
3389  int en = length;
3390  int cmp_int = pOrdSgn;
3391  if ((op < o) || (pLmCmp(set[length].p,p.p)== -cmp_int))
3392    return length+1;
3393  int cmp;
3394  loop
3395  {
3396    if (an >= en-1)
3397    {
3398      op = set[an].GetpFDeg();
3399      if (op > o) return an;
3400      if (op < 0) return en;
3401      cmp = pLmCmp(set[an].p,p.p);
3402      if (cmp == cmp_int)  return an;
3403      if (cmp == -cmp_int) return en;
3404      if (nGreater(pGetCoeff(p.p), pGetCoeff(set[an].p))) return en;
3405      return an;
3406    }
3407    i = (an + en) / 2;
3408    op = set[i].GetpFDeg();
3409    if (op > o)       en = i;
3410    else if (op < o)  an = i;
3411    else
3412    {
3413      cmp = pLmCmp(set[i].p,p.p);
3414      if (cmp == cmp_int)                                     en = i;
3415      else if (cmp == -cmp_int)                               an = i;
3416      else if (nGreater(pGetCoeff(p.p), pGetCoeff(set[i].p))) an = i;
3417      else                                                    en = i;
3418    }
3419  }
3420}
3421/*
3422  int o = p.GetpFDeg();
3423  int op = set[length].GetpFDeg();
3424
3425  if ((op < o)
3426  || ((op == o) && (pLmCmp(set[length].p,p.p) != pOrdSgn)))
3427    return length+1;
3428
3429  int i;
3430  int an = 0;
3431  int en= length;
3432
3433  loop
3434  {
3435    if (an >= en-1)
3436    {
3437      op= set[an].GetpFDeg();
3438      if ((op > o)
3439      || (( op == o) && (pLmCmp(set[an].p,p.p) == pOrdSgn)))
3440        return an;
3441      return en;
3442    }
3443    i=(an+en) / 2;
3444    op = set[i].GetpFDeg();
3445    if (( op > o)
3446    || (( op == o) && (pLmCmp(set[i].p,p.p) == pOrdSgn)))
3447      en=i;
3448    else
3449      an=i;
3450  }
3451}
3452  */
3453/*2
3454* looks up the position of p in T
3455* set[0] is the smallest with respect to the ordering-procedure
3456* totaldegree,pComp
3457*/
3458int posInT110 (const TSet set,const int length,LObject &p)
3459{
3460  if (length==-1) return 0;
3461
3462  int o = p.GetpFDeg();
3463  int op = set[length].GetpFDeg();
3464
3465  if (( op < o)
3466  || (( op == o) && (set[length].length<p.length))
3467  || (( op == o) && (set[length].length == p.length)
3468     && (pLmCmp(set[length].p,p.p) != pOrdSgn)))
3469    return length+1;
3470
3471  int i;
3472  int an = 0;
3473  int en= length;
3474  loop
3475  {
3476    if (an >= en-1)
3477    {
3478      op = set[an].GetpFDeg();
3479      if (( op > o)
3480      || (( op == o) && (set[an].length > p.length))
3481      || (( op == o) && (set[an].length == p.length)
3482         && (pLmCmp(set[an].p,p.p) == pOrdSgn)))
3483        return an;
3484      return en;
3485    }
3486    i=(an+en) / 2;
3487    op = set[i].GetpFDeg();
3488    if (( op > o)
3489    || (( op == o) && (set[i].length > p.length))
3490    || (( op == o) && (set[i].length == p.length)
3491       && (pLmCmp(set[i].p,p.p) == pOrdSgn)))
3492      en=i;
3493    else
3494      an=i;
3495  }
3496}
3497
3498/*2
3499* looks up the position of p in set
3500* set[0] is the smallest with respect to the ordering-procedure
3501* pFDeg
3502*/
3503int posInT13 (const TSet set,const int length,LObject &p)
3504{
3505  if (length==-1) return 0;
3506
3507  int o = p.GetpFDeg();
3508
3509  if (set[length].GetpFDeg() <= o)
3510    return length+1;
3511
3512  int i;
3513  int an = 0;
3514  int en= length;
3515  loop
3516  {
3517    if (an >= en-1)
3518    {
3519      if (set[an].GetpFDeg() > o)
3520        return an;
3521      return en;
3522    }
3523    i=(an+en) / 2;
3524    if (set[i].GetpFDeg() > o)
3525      en=i;
3526    else
3527      an=i;
3528  }
3529}
3530
3531// determines the position based on: 1.) Ecart 2.) pLength
3532int posInT_EcartpLength(const TSet set,const int length,LObject &p)
3533{
3534  if (length==-1) return 0;
3535
3536  int op=p.ecart;
3537  int ol = p.GetpLength();
3538
3539  int oo=set[length].ecart;
3540  if ((oo < op) || ((oo==op) && (set[length].length < ol)))
3541    return length+1;
3542
3543  int i;
3544  int an = 0;
3545  int en= length;
3546  loop
3547    {
3548      if (an >= en-1)
3549      {
3550        int oo=set[an].ecart;
3551        if((oo > op)
3552           || ((oo==op) && (set[an].pLength > ol)))
3553          return an;
3554        return en;
3555      }
3556      i=(an+en) / 2;
3557      int oo=set[i].ecart;
3558      if ((oo > op)
3559          || ((oo == op) && (set[i].pLength > ol)))
3560        en=i;
3561      else
3562        an=i;
3563    }
3564}
3565
3566/*2
3567* looks up the position of p in set
3568* set[0] is the smallest with respect to the ordering-procedure
3569* maximaldegree, pComp
3570*/
3571int posInT15 (const TSet set,const int length,LObject &p)
3572/*{
3573 *int j=0;
3574 * int o;
3575 *
3576 * o = p.GetpFDeg()+p.ecart;
3577 * loop
3578 * {
3579 *   if ((set[j].GetpFDeg()+set[j].ecart > o)
3580 *   || ((set[j].GetpFDeg()+set[j].ecart == o)
3581 *     && (pLmCmp(set[j].p,p.p) == pOrdSgn)))
3582 *   {
3583 *     return j;
3584 *   }
3585 *   j++;
3586 *   if (j > length) return j;
3587 * }
3588 *}
3589 */
3590{
3591  if (length==-1) return 0;
3592
3593  int o = p.GetpFDeg() + p.ecart;
3594  int op = set[length].GetpFDeg()+set[length].ecart;
3595
3596  if ((op < o)
3597  || ((op == o)
3598     && (pLmCmp(set[length].p,p.p) != pOrdSgn)))
3599    return length+1;
3600
3601  int i;
3602  int an = 0;
3603  int en= length;
3604  loop
3605  {
3606    if (an >= en-1)
3607    {
3608      op = set[an].GetpFDeg()+set[an].ecart;
3609      if (( op > o)
3610      || (( op  == o) && (pLmCmp(set[an].p,p.p) == pOrdSgn)))
3611        return an;
3612      return en;
3613    }
3614    i=(an+en) / 2;
3615    op = set[i].GetpFDeg()+set[i].ecart;
3616    if (( op > o)
3617    || (( op == o) && (pLmCmp(set[i].p,p.p) == pOrdSgn)))
3618      en=i;
3619    else
3620      an=i;
3621  }
3622}
3623
3624/*2
3625* looks up the position of p in set
3626* set[0] is the smallest with respect to the ordering-procedure
3627* pFDeg+ecart, ecart, pComp
3628*/
3629int posInT17 (const TSet set,const int length,LObject &p)
3630/*
3631*{
3632* int j=0;
3633* int  o;
3634*
3635*  o = p.GetpFDeg()+p.ecart;
3636*  loop
3637*  {
3638*    if ((pFDeg(set[j].p)+set[j].ecart > o)
3639*    || (((pFDeg(set[j].p)+set[j].ecart == o)
3640*      && (set[j].ecart < p.ecart)))
3641*    || ((pFDeg(set[j].p)+set[j].ecart == o)
3642*      && (set[j].ecart==p.ecart)
3643*      && (pLmCmp(set[j].p,p.p)==pOrdSgn)))
3644*      return j;
3645*    j++;
3646*    if (j > length) return j;
3647*  }
3648* }
3649*/
3650{
3651  if (length==-1) return 0;
3652
3653  int o = p.GetpFDeg() + p.ecart;
3654  int op = set[length].GetpFDeg()+set[length].ecart;
3655
3656  if ((op < o)
3657  || (( op == o) && (set[length].ecart > p.ecart))
3658  || (( op == o) && (set[length].ecart==p.ecart)
3659     && (pLmCmp(set[length].p,p.p) != pOrdSgn)))
3660    return length+1;
3661
3662  int i;
3663  int an = 0;
3664  int en= length;
3665  loop
3666  {
3667    if (an >= en-1)
3668    {
3669      op = set[an].GetpFDeg()+set[an].ecart;
3670      if (( op > o)
3671      || (( op == o) && (set[an].ecart < p.ecart))
3672      || (( op  == o) && (set[an].ecart==p.ecart)
3673         && (pLmCmp(set[an].p,p.p) == pOrdSgn)))
3674        return an;
3675      return en;
3676    }
3677    i=(an+en) / 2;
3678    op = set[i].GetpFDeg()+set[i].ecart;
3679    if ((op > o)
3680    || (( op == o) && (set[i].ecart < p.ecart))
3681    || (( op == o) && (set[i].ecart == p.ecart)
3682       && (pLmCmp(set[i].p,p.p) == pOrdSgn)))
3683      en=i;
3684    else
3685      an=i;
3686  }
3687}
3688/*2
3689* looks up the position of p in set
3690* set[0] is the smallest with respect to the ordering-procedure
3691* pGetComp, pFDeg+ecart, ecart, pComp
3692*/
3693int posInT17_c (const TSet set,const int length,LObject &p)
3694{
3695  if (length==-1) return 0;
3696
3697  int cc = (-1+2*currRing->order[0]==ringorder_c);
3698  /* cc==1 for (c,..), cc==-1 for (C,..) */
3699  int o = p.GetpFDeg() + p.ecart;
3700  int c = pGetComp(p.p)*cc;
3701
3702  if (pGetComp(set[length].p)*cc < c)
3703    return length+1;
3704  if (pGetComp(set[length].p)*cc == c)
3705  {
3706    int op = set[length].GetpFDeg()+set[length].ecart;
3707    if ((op < o)
3708    || ((op == o) && (set[length].ecart > p.ecart))
3709    || ((op == o) && (set[length].ecart==p.ecart)
3710       && (pLmCmp(set[length].p,p.p) != pOrdSgn)))
3711      return length+1;
3712  }
3713
3714  int i;
3715  int an = 0;
3716  int en= length;
3717  loop
3718  {
3719    if (an >= en-1)
3720    {
3721      if (pGetComp(set[an].p)*cc < c)
3722        return en;
3723      if (pGetComp(set[an].p)*cc == c)
3724      {
3725        int op = set[an].GetpFDeg()+set[an].ecart;
3726        if ((op > o)
3727        || ((op == o) && (set[an].ecart < p.ecart))
3728        || ((op == o) && (set[an].ecart==p.ecart)
3729           && (pLmCmp(set[an].p,p.p) == pOrdSgn)))
3730          return an;
3731      }
3732      return en;
3733    }
3734    i=(an+en) / 2;
3735    if (pGetComp(set[i].p)*cc > c)
3736      en=i;
3737    else if (pGetComp(set[i].p)*cc == c)
3738    {
3739      int op = set[i].GetpFDeg()+set[i].ecart;
3740      if ((op > o)
3741      || ((op == o) && (set[i].ecart < p.ecart))
3742      || ((op == o) && (set[i].ecart == p.ecart)
3743         && (pLmCmp(set[i].p,p.p) == pOrdSgn)))
3744        en=i;
3745      else
3746        an=i;
3747    }
3748    else
3749      an=i;
3750  }
3751}
3752
3753/*2
3754* looks up the position of p in set
3755* set[0] is the smallest with respect to
3756* ecart, pFDeg, length
3757*/
3758int posInT19 (const TSet set,const int length,LObject &p)
3759{
3760  if (length==-1) return 0;
3761
3762  int o = p.ecart;
3763  int op=p.GetpFDeg();
3764
3765  if (set[length].ecart < o)
3766    return length+1;
3767  if (set[length].ecart == o)
3768  {
3769     int oo=set[length].GetpFDeg();
3770     if ((oo < op) || ((oo==op) && (set[length].length < p.length)))
3771       return length+1;
3772  }
3773
3774  int i;
3775  int an = 0;
3776  int en= length;
3777  loop
3778  {
3779    if (an >= en-1)
3780    {
3781      if (set[an].ecart > o)
3782        return an;
3783      if (set[an].ecart == o)
3784      {
3785         int oo=set[an].GetpFDeg();
3786         if((oo > op)
3787         || ((oo==op) && (set[an].length > p.length)))
3788           return an;
3789      }
3790      return en;
3791    }
3792    i=(an+en) / 2;
3793    if (set[i].ecart > o)
3794      en=i;
3795    else if (set[i].ecart == o)
3796    {
3797       int oo=set[i].GetpFDeg();
3798       if ((oo > op)
3799       || ((oo == op) && (set[i].length > p.length)))
3800         en=i;
3801       else
3802        an=i;
3803    }
3804    else
3805      an=i;
3806  }
3807}
3808
3809/*2
3810*looks up the position of polynomial p in set
3811*set[length] is the smallest element in set with respect
3812*to the ordering-procedure pComp
3813*/
3814int posInLSpecial (const LSet set, const int length,
3815                   LObject *p,const kStrategy strat)
3816{
3817  if (length<0) return 0;
3818
3819  int d=p->GetpFDeg();
3820  int op=set[length].GetpFDeg();
3821
3822  if ((op > d)
3823  || ((op == d) && (p->p1!=NULL)&&(set[length].p1==NULL))
3824  || (pLmCmp(set[length].p,p->p)== pOrdSgn))
3825     return length+1;
3826
3827  int i;
3828  int an = 0;
3829  int en= length;
3830  loop
3831  {
3832    if (an >= en-1)
3833    {
3834      op=set[an].GetpFDeg();
3835      if ((op > d)
3836      || ((op == d) && (p->p1!=NULL) && (set[an].p1==NULL))
3837      || (pLmCmp(set[an].p,p->p)== pOrdSgn))
3838         return en;
3839      return an;
3840    }
3841    i=(an+en) / 2;
3842    op=set[i].GetpFDeg();
3843    if ((op>d)
3844    || ((op==d) && (p->p1!=NULL) && (set[i].p1==NULL))
3845    || (pLmCmp(set[i].p,p->p) == pOrdSgn))
3846      an=i;
3847    else
3848      en=i;
3849  }
3850}
3851
3852/*2
3853*looks up the position of polynomial p in set
3854*set[length] is the smallest element in set with respect
3855*to the ordering-procedure pComp
3856*/
3857int posInL0 (const LSet set, const int length,
3858             LObject* p,const kStrategy strat)
3859{
3860  if (length<0) return 0;
3861
3862  if (pLmCmp(set[length].p,p->p)== pOrdSgn)
3863    return length+1;
3864
3865  int i;
3866  int an = 0;
3867  int en= length;
3868  loop
3869  {
3870    if (an >= en-1)
3871    {
3872      if (pLmCmp(set[an].p,p->p) == pOrdSgn) return en;
3873      return an;
3874    }
3875    i=(an+en) / 2;
3876    if (pLmCmp(set[i].p,p->p) == pOrdSgn) an=i;
3877    else                                 en=i;
3878    /*aend. fuer lazy == in !=- machen */
3879  }
3880}
3881
3882/*2
3883* looks up the position of polynomial p in set
3884* e is the ecart of p
3885* set[length] is the smallest element in set with respect
3886* to the ordering-procedure totaldegree,pComp
3887*/
3888int posInL11 (const LSet set, const int length,
3889              LObject* p,const kStrategy strat)
3890/*{
3891 * int j=0;
3892 * int o;
3893 *
3894 * o = p->GetpFDeg();
3895 * loop
3896 * {
3897 *   if (j > length)            return j;
3898 *   if ((set[j].GetpFDeg() < o)) return j;
3899 *   if ((set[j].GetpFDeg() == o) && (pLmCmp(set[j].p,p->p) == -pOrdSgn))
3900 *   {
3901 *     return j;
3902 *   }
3903 *   j++;
3904 * }
3905 *}
3906 */
3907{
3908  if (length<0) return 0;
3909
3910  int o = p->GetpFDeg();
3911  int op = set[length].GetpFDeg();
3912
3913  if ((op > o)
3914  || ((op == o) && (pLmCmp(set[length].p,p->p) != -pOrdSgn)))
3915    return length+1;
3916  int i;
3917  int an = 0;
3918  int en= length;
3919  loop
3920  {
3921    if (an >= en-1)
3922    {
3923      op = set[an].GetpFDeg();
3924      if ((op > o)
3925      || ((op == o) && (pLmCmp(set[an].p,p->p) != -pOrdSgn)))
3926        return en;
3927      return an;
3928    }
3929    i=(an+en) / 2;
3930    op = set[i].GetpFDeg();
3931    if ((op > o)
3932    || ((op == o) && (pLmCmp(set[i].p,p->p) != -pOrdSgn)))
3933      an=i;
3934    else
3935      en=i;
3936  }
3937}
3938
3939/*2 Position for rings L: Here I am
3940* looks up the position of polynomial p in set
3941* e is the ecart of p
3942* set[length] is the smallest element in set with respect
3943* to the ordering-procedure totaldegree,pComp
3944*/
3945inline int getIndexRng(long coeff)
3946{
3947  if (coeff == 0) return -1;
3948  long tmp = coeff;
3949  int ind = 0;
3950  while (tmp % 2 == 0)
3951  {
3952    tmp = tmp / 2;
3953    ind++;
3954  }
3955  return ind;
3956}
3957
3958int posInLrg0 (const LSet set, const int length,
3959              LObject* p,const kStrategy strat)
3960/*          if (nGreater(pGetCoeff(p), pGetCoeff(set[an]))) return en;
3961        if (pLmCmp(set[i],p) == cmp_int)         en = i;
3962        else if (pLmCmp(set[i],p) == -cmp_int)   an = i;
3963        else
3964        {
3965          if (nGreater(pGetCoeff(p), pGetCoeff(set[i]))) an = i;
3966          else en = i;
3967        }*/
3968{
3969  if (length < 0) return 0;
3970
3971  int o = p->GetpFDeg();
3972  int op = set[length].GetpFDeg();
3973
3974  if ((op > o) || ((op == o) && (pLmCmp(set[length].p,p->p) != -pOrdSgn)))
3975    return length + 1;
3976  int i;
3977  int an = 0;
3978  int en = length;
3979  loop
3980  {
3981    if (an >= en - 1)
3982    {
3983      op = set[an].GetpFDeg();
3984      if ((op > o) || ((op == o) && (pLmCmp(set[an].p,p->p) != -pOrdSgn)))
3985        return en;
3986      return an;
3987    }
3988    i = (an+en) / 2;
3989    op = set[i].GetpFDeg();
3990    if ((op > o) || ((op == o) && (pLmCmp(set[i].p,p->p) != -pOrdSgn)))
3991      an = i;
3992    else
3993      en = i;
3994  }
3995}
3996
3997/*{
3998  if (length < 0) return 0;
3999
4000  int o = p->GetpFDeg();
4001  int op = set[length].GetpFDeg();
4002
4003  int inde = getIndexRng((unsigned long) pGetCoeff(set[length].p));
4004  int indp = getIndexRng((unsigned long) pGetCoeff(p->p));
4005  int inda;
4006  int indi;
4007
4008  if ((inda > indp) || ((inda == inde) && ((op > o) || ((op == o) && (pLmCmp(set[length].p,p->p) != -pOrdSgn)))))
4009    return length + 1;
4010  int i;
4011  int an = 0;
4012  inda = getIndexRng((unsigned long) pGetCoeff(set[an].p));
4013  int en = length;
4014  loop
4015  {
4016    if (an >= en-1)
4017    {
4018      op = set[an].GetpFDeg();
4019      if ((indp > inda) || ((indp == inda) && ((op > o) || ((op == o) && (pLmCmp(set[an].p,p->p) != -pOrdSgn)))))
4020        return en;
4021      return an;
4022    }
4023    i = (an + en) / 2;
4024    indi = getIndexRng((unsigned long) pGetCoeff(set[i].p));
4025    op = set[i].GetpFDeg();
4026    if ((indi > indp) || ((indi == indp) && ((op > o) || ((op == o) && (pLmCmp(set[i].p,p->p) != -pOrdSgn)))))
4027    // if ((op > o) || ((op == o) && (pLmCmp(set[i].p,p->p) != -pOrdSgn)))
4028    {
4029      an = i;
4030      inda = getIndexRng((unsigned long) pGetCoeff(set[an].p));
4031    }
4032    else
4033      en = i;
4034  }
4035} */
4036
4037/*2
4038* looks up the position of polynomial p in set
4039* set[length] is the smallest element in set with respect
4040* to the ordering-procedure totaldegree,pLength0
4041*/
4042int posInL110 (const LSet set, const int length,
4043               LObject* p,const kStrategy strat)
4044{
4045  if (length<0) return 0;
4046
4047  int o = p->GetpFDeg();
4048  int op = set[length].GetpFDeg();
4049
4050  if ((op > o)
4051  || ((op == o) && (set[length].length >p->length))
4052  || ((op == o) && (set[length].length <= p->length)
4053     && (pLmCmp(set[length].p,p->p) != -pOrdSgn)))
4054    return length+1;
4055  int i;
4056  int an = 0;
4057  int en= length;
4058  loop
4059  {
4060    if (an >= en-1)
4061    {
4062      op = set[an].GetpFDeg();
4063      if ((op > o)
4064      || ((op == o) && (set[an].length >p->length))
4065      || ((op == o) && (set[an].length <=p->length)
4066         && (pLmCmp(set[an].p,p->p) != -pOrdSgn)))
4067        return en;
4068      return an;
4069    }
4070    i=(an+en) / 2;
4071    op = set[i].GetpFDeg();
4072    if ((op > o)
4073    || ((op == o) && (set[i].length > p->length))
4074    || ((op == o) && (set[i].length <= p->length)
4075       && (pLmCmp(set[i].p,p->p) != -pOrdSgn)))
4076      an=i;
4077    else
4078      en=i;
4079  }
4080}
4081
4082/*2
4083* looks up the position of polynomial p in set
4084* e is the ecart of p
4085* set[length] is the smallest element in set with respect
4086* to the ordering-procedure totaldegree
4087*/
4088int posInL13 (const LSet set, const int length,
4089              LObject* p,const kStrategy strat)
4090{
4091  if (length<0) return 0;
4092
4093  int o = p->GetpFDeg();
4094
4095  if (set[length].GetpFDeg() > o)
4096    return length+1;
4097
4098  int i;
4099  int an = 0;
4100  int en= length;
4101  loop
4102  {
4103    if (an >= en-1)
4104    {
4105      if (set[an].GetpFDeg() >= o)
4106        return en;
4107      return an;
4108    }
4109    i=(an+en) / 2;
4110    if (set[i].GetpFDeg() >= o)
4111      an=i;
4112    else
4113      en=i;
4114  }
4115}
4116
4117/*2
4118* looks up the position of polynomial p in set
4119* e is the ecart of p
4120* set[length] is the smallest element in set with respect
4121* to the ordering-procedure maximaldegree,pComp
4122*/
4123int posInL15 (const LSet set, const int length,
4124              LObject* p,const kStrategy strat)
4125/*{
4126 * int j=0;
4127 * int o;
4128 *
4129 * o = p->ecart+p->GetpFDeg();
4130 * loop
4131 * {
4132 *   if (j > length)                       return j;
4133 *   if (set[j].GetpFDeg()+set[j].ecart < o) return j;
4134 *   if ((set[j].GetpFDeg()+set[j].ecart == o)
4135 *   && (pLmCmp(set[j].p,p->p) == -pOrdSgn))
4136 *   {
4137 *     return j;
4138 *   }
4139 *   j++;
4140 * }
4141 *}
4142 */
4143{
4144  if (length<0) return 0;
4145
4146  int o = p->GetpFDeg() + p->ecart;
4147  int op = set[length].GetpFDeg() + set[length].ecart;
4148
4149  if ((op > o)
4150  || ((op == o) && (pLmCmp(set[length].p,p->p) != -pOrdSgn)))
4151    return length+1;
4152  int i;
4153  int an = 0;
4154  int en= length;
4155  loop
4156  {
4157    if (an >= en-1)
4158    {
4159      op = set[an].GetpFDeg() + set[an].ecart;
4160      if ((op > o)
4161      || ((op == o) && (pLmCmp(set[an].p,p->p) != -pOrdSgn)))
4162        return en;
4163      return an;
4164    }
4165    i=(an+en) / 2;
4166    op = set[i].GetpFDeg() + set[i].ecart;
4167    if ((op > o)
4168    || ((op == o) && (pLmCmp(set[i].p,p->p) != -pOrdSgn)))
4169      an=i;
4170    else
4171      en=i;
4172  }
4173}
4174
4175/*2
4176* looks up the position of polynomial p in set
4177* e is the ecart of p
4178* set[length] is the smallest element in set with respect
4179* to the ordering-procedure totaldegree
4180*/
4181int posInL17 (const LSet set, const int length,
4182              LObject* p,const kStrategy strat)
4183{
4184  if (length<0) return 0;
4185
4186  int o = p->GetpFDeg() + p->ecart;
4187
4188  if ((set[length].GetpFDeg() + set[length].ecart > o)
4189  || ((set[length].GetpFDeg() + set[length].ecart == o)
4190     && (set[length].ecart > p->ecart))
4191  || ((set[length].GetpFDeg() + set[length].ecart == o)
4192     && (set[length].ecart == p->ecart)
4193     && (pLmCmp(set[length].p,p->p) != -pOrdSgn)))
4194    return length+1;
4195  int i;
4196  int an = 0;
4197  int en= length;
4198  loop
4199  {
4200    if (an >= en-1)
4201    {
4202      if ((set[an].GetpFDeg() + set[an].ecart > o)
4203      || ((set[an].GetpFDeg() + set[an].ecart == o)
4204         && (set[an].ecart > p->ecart))
4205      || ((set[an].GetpFDeg() + set[an].ecart == o)
4206         && (set[an].ecart == p->ecart)
4207         && (pLmCmp(set[an].p,p->p) != -pOrdSgn)))
4208        return en;
4209      return an;
4210    }
4211    i=(an+en) / 2;
4212    if ((set[i].GetpFDeg() + set[i].ecart > o)
4213    || ((set[i].GetpFDeg() + set[i].ecart == o)
4214       && (set[i].ecart > p->ecart))
4215    || ((set[i].GetpFDeg() +set[i].ecart == o)
4216       && (set[i].ecart == p->ecart)
4217       && (pLmCmp(set[i].p,p->p) != -pOrdSgn)))
4218      an=i;
4219    else
4220      en=i;
4221  }
4222}
4223/*2
4224* looks up the position of polynomial p in set
4225* e is the ecart of p
4226* set[length] is the smallest element in set with respect
4227* to the ordering-procedure pComp
4228*/
4229int posInL17_c (const LSet set, const int length,
4230                LObject* p,const kStrategy strat)
4231{
4232  if (length<0) return 0;
4233
4234  int cc = (-1+2*currRing->order[0]==ringorder_c);
4235  /* cc==1 for (c,..), cc==-1 for (C,..) */
4236  int c = pGetComp(p->p)*cc;
4237  int o = p->GetpFDeg() + p->ecart;
4238
4239  if (pGetComp(set[length].p)*cc > c)
4240    return length+1;
4241  if (pGetComp(set[length].p)*cc == c)
4242  {
4243    if ((set[length].GetpFDeg() + set[length].ecart > o)
4244    || ((set[length].GetpFDeg() + set[length].ecart == o)
4245       && (set[length].ecart > p->ecart))
4246    || ((set[length].GetpFDeg() + set[length].ecart == o)
4247       && (set[length].ecart == p->ecart)
4248       && (pLmCmp(set[length].p,p->p) != -pOrdSgn)))
4249      return length+1;
4250  }
4251  int i;
4252  int an = 0;
4253  int en= length;
4254  loop
4255  {
4256    if (an >= en-1)
4257    {
4258      if (pGetComp(set[an].p)*cc > c)
4259        return en;
4260      if (pGetComp(set[an].p)*cc == c)
4261      {
4262        if ((set[an].GetpFDeg() + set[an].ecart > o)
4263        || ((set[an].GetpFDeg() + set[an].ecart == o)
4264           && (set[an].ecart > p->ecart))
4265        || ((set[an].GetpFDeg() + set[an].ecart == o)
4266           && (set[an].ecart == p->ecart)
4267           && (pLmCmp(set[an].p,p->p) != -pOrdSgn)))
4268          return en;
4269      }
4270      return an;
4271    }
4272    i=(an+en) / 2;
4273    if (pGetComp(set[i].p)*cc > c)
4274      an=i;
4275    else if (pGetComp(set[i].p)*cc == c)
4276    {
4277      if ((set[i].GetpFDeg() + set[i].ecart > o)
4278      || ((set[i].GetpFDeg() + set[i].ecart == o)
4279         && (set[i].ecart > p->ecart))
4280      || ((set[i].GetpFDeg() +set[i].ecart == o)
4281         && (set[i].ecart == p->ecart)
4282         && (pLmCmp(set[i].p,p->p) != -pOrdSgn)))
4283        an=i;
4284      else
4285        en=i;
4286    }
4287    else
4288      en=i;
4289  }
4290}
4291
4292/***************************************************************
4293 *
4294 * Tail reductions
4295 *
4296 ***************************************************************/
4297TObject*
4298kFindDivisibleByInS(kStrategy strat, int pos, LObject* L, TObject *T,
4299                    long ecart)
4300{
4301  int j = 0;
4302  const unsigned long not_sev = ~L->sev;
4303  const unsigned long* sev = strat->sevS;
4304  poly p;
4305  ring r;
4306  L->GetLm(p, r);
4307
4308  assume(~not_sev == p_GetShortExpVector(p, r));
4309
4310  if (r == currRing)
4311  {
4312    loop
4313    {
4314      if (j > pos) return NULL;
4315#if defined(PDEBUG) || defined(PDIV_DEBUG)
4316      if (p_LmShortDivisibleBy(strat->S[j], sev[j], p, not_sev, r) &&
4317          (ecart== LONG_MAX || ecart>= strat->ecartS[j]))
4318        break;
4319#else
4320      if (!(sev[j] & not_sev) &&
4321          (ecart== LONG_MAX || ecart>= strat->ecartS[j]) &&
4322          p_LmDivisibleBy(strat->S[j], p, r))
4323        break;
4324
4325#endif
4326      j++;
4327    }
4328    // if called from NF, T objects do not exist:
4329    if (strat->tl < 0 || strat->S_2_R[j] == -1)
4330    {
4331      T->Set(strat->S[j], r, strat->tailRing);
4332      return T;
4333    }
4334    else
4335    {
4336/////      assume (j >= 0 && j <= strat->tl && strat->S_2_T(j) != NULL
4337/////      && strat->S_2_T(j)->p == strat->S[j]); // wrong?
4338//      assume (j >= 0 && j <= strat->sl && strat->S_2_T(j) != NULL && strat->S_2_T(j)->p == strat->S[j]);
4339      return strat->S_2_T(j);
4340    }
4341  }
4342  else
4343  {
4344    TObject* t;
4345    loop
4346    {
4347      if (j > pos) return NULL;
4348      assume(strat->S_2_R[j] != -1);
4349#if defined(PDEBUG) || defined(PDIV_DEBUG)
4350      t = strat->S_2_T(j);
4351      assume(t != NULL && t->t_p != NULL && t->tailRing == r);
4352      if (p_LmShortDivisibleBy(t->t_p, sev[j], p, not_sev, r) &&
4353          (ecart== LONG_MAX || ecart>= strat->ecartS[j]))
4354        return t;
4355#else
4356      if (! (sev[j] & not_sev) && (ecart== LONG_MAX || ecart>= strat->ecartS[j]))
4357      {
4358        t = strat->S_2_T(j);
4359        assume(t != NULL && t->t_p != NULL && t->tailRing == r && t->p == strat->S[j]);
4360        if (p_LmDivisibleBy(t->t_p, p, r)) return t;
4361      }
4362#endif
4363      j++;
4364    }
4365  }
4366}
4367
4368poly redtail (LObject* L, int pos, kStrategy strat)
4369{
4370  poly h, hn;
4371  int j;
4372  unsigned long not_sev;
4373  strat->redTailChange=FALSE;
4374
4375  poly p = L->p;
4376  if (strat->noTailReduction || pNext(p) == NULL)
4377    return p;
4378
4379  LObject Ln(strat->tailRing);
4380  TObject* With;
4381  // placeholder in case strat->tl < 0
4382  TObject  With_s(strat->tailRing);
4383  h = p;
4384  hn = pNext(h);
4385  long op = strat->tailRing->pFDeg(hn, strat->tailRing);
4386  long e;
4387  int l;
4388  BOOLEAN save_HE=strat->kHEdgeFound;
4389  strat->kHEdgeFound |=
4390    ((Kstd1_deg>0) && (op<=Kstd1_deg)) || TEST_OPT_INFREDTAIL;
4391
4392  while(hn != NULL)
4393  {
4394    op = strat->tailRing->pFDeg(hn, strat->tailRing);
4395    if ((Kstd1_deg>0)&&(op>Kstd1_deg)) goto all_done;
4396    e = strat->tailRing->pLDeg(hn, &l, strat->tailRing) - op;
4397    loop
4398    {
4399      Ln.Set(hn, strat->tailRing);
4400      Ln.sev = p_GetShortExpVector(hn, strat->tailRing);
4401      if (strat->kHEdgeFound)
4402        With = kFindDivisibleByInS(strat, pos, &Ln, &With_s);
4403      else
4404        With = kFindDivisibleByInS(strat, pos, &Ln, &With_s, e);
4405      if (With == NULL) break;
4406      With->length=0;
4407      With->pLength=0;
4408      strat->redTailChange=TRUE;
4409      if (ksReducePolyTail(L, With, h, strat->kNoetherTail()))
4410      {
4411        // reducing the tail would violate the exp bound
4412        if (kStratChangeTailRing(strat, L))
4413        {
4414          strat->kHEdgeFound = save_HE;
4415          return redtail(L, pos, strat);
4416        }
4417        else
4418          return NULL;
4419      }
4420      hn = pNext(h);
4421      if (hn == NULL) goto all_done;
4422      op = strat->tailRing->pFDeg(hn, strat->tailRing);
4423      if ((Kstd1_deg>0)&&(op>Kstd1_deg)) goto all_done;
4424      e = strat->tailRing->pLDeg(hn, &l, strat->tailRing) - op;
4425    }
4426    h = hn;
4427    hn = pNext(h);
4428  }
4429
4430  all_done:
4431  if (strat->redTailChange)
4432  {
4433    L->last = 0;
4434    L->pLength = 0;
4435  }
4436  strat->kHEdgeFound = save_HE;
4437  return p;
4438}
4439
4440poly redtail (poly p, int pos, kStrategy strat)
4441{
4442  LObject L(p, currRing);
4443  return redtail(&L, pos, strat);
4444}
4445
4446poly redtailBba (LObject* L, int pos, kStrategy strat, BOOLEAN withT, BOOLEAN normalize)
4447{
4448#define REDTAIL_CANONICALIZE 100
4449  strat->redTailChange=FALSE;
4450  if (strat->noTailReduction) return L->GetLmCurrRing();
4451  poly h, p;
4452  p = h = L->GetLmTailRing();
4453  if ((h==NULL) || (pNext(h)==NULL))
4454    return L->GetLmCurrRing();
4455
4456  TObject* With;
4457  // placeholder in case strat->tl < 0
4458  TObject  With_s(strat->tailRing);
4459
4460  LObject Ln(pNext(h), strat->tailRing);
4461  Ln.pLength = L->GetpLength() - 1;
4462
4463  pNext(h) = NULL;
4464  if (L->p != NULL) pNext(L->p) = NULL;
4465  L->pLength = 1;
4466
4467  Ln.PrepareRed(strat->use_buckets);
4468
4469  int cnt=REDTAIL_CANONICALIZE;
4470  while(!Ln.IsNull())
4471  {
4472    loop
4473    {
4474      Ln.SetShortExpVector();
4475      if (withT)
4476      {
4477        int j;
4478        j = kFindDivisibleByInT(strat->T, strat->sevT, strat->tl, &Ln);
4479        if (j < 0) break;
4480        With = &(strat->T[j]);
4481      }
4482      else
4483      {
4484        With = kFindDivisibleByInS(strat, pos, &Ln, &With_s);
4485        if (With == NULL) break;
4486      }
4487      cnt--;
4488      if (cnt==0)
4489      {
4490        cnt=REDTAIL_CANONICALIZE; 
4491        poly tmp=Ln.CanonicalizeP(); 
4492        if (normalize) 
4493        {
4494          Ln.Normalize();
4495          //pNormalize(tmp);
4496          //if (TEST_OPT_PROT) { PrintS("n"); mflush(); }
4497        }
4498      }
4499      if (normalize && (!TEST_OPT_INTSTRATEGY) && (!nIsOne(pGetCoeff(With->p))))
4500      {
4501        With->pNorm();
4502      }
4503      strat->redTailChange=TRUE;
4504      if (ksReducePolyTail(L, With, &Ln))
4505      {
4506        // reducing the tail would violate the exp bound
4507        //  set a flag and hope for a retry (in bba)
4508        strat->completeReduce_retry=TRUE;
4509        do
4510        {
4511          pNext(h) = Ln.LmExtractAndIter();
4512          pIter(h);
4513          L->pLength++;
4514        } while (!Ln.IsNull());
4515        goto all_done;
4516      }
4517      if (Ln.IsNull()) goto all_done;
4518      if (! withT) With_s.Init(currRing);
4519    }
4520    pNext(h) = Ln.LmExtractAndIter();
4521    pIter(h);
4522    pNormalize(h);
4523    L->pLength++;
4524  }
4525
4526  all_done:
4527  Ln.Delete();
4528  if (L->p != NULL) pNext(L->p) = pNext(p);
4529
4530  if (strat->redTailChange)
4531  {
4532    L->last = NULL;
4533    L->length = 0;
4534  }
4535
4536  //if (TEST_OPT_PROT) { PrintS("N"); mflush(); }
4537  //L->Normalize(); // HANNES: should have a test
4538  kTest_L(L);
4539  return L->GetLmCurrRing();
4540}
4541
4542/*2
4543*checks the change degree and write progress report
4544*/
4545void message (int i,int* reduc,int* olddeg,kStrategy strat, int red_result)
4546{
4547  if (i != *olddeg)
4548  {
4549    Print("%d",i);
4550    *olddeg = i;
4551  }
4552  if (K_TEST_OPT_OLDSTD)
4553  {
4554    if (strat->Ll != *reduc)
4555    {
4556      if (strat->Ll != *reduc-1)
4557        Print("(%d)",strat->Ll+1);
4558      else
4559        PrintS("-");
4560      *reduc = strat->Ll;
4561    }
4562    else
4563      PrintS(".");
4564    mflush();
4565  }
4566  else
4567  {
4568    if (red_result == 0)
4569      PrintS("-");
4570    else if (red_result < 0)
4571      PrintS(".");
4572    if ((red_result > 0) || ((strat->Ll % 100)==99))
4573    {
4574      if (strat->Ll != *reduc && strat->Ll > 0)
4575      {
4576        Print("(%d)",strat->Ll+1);
4577        *reduc = strat->Ll;
4578      }
4579    }
4580  }
4581}
4582
4583/*2
4584*statistics
4585*/
4586void messageStat (int srmax,int lrmax,int hilbcount,kStrategy strat)
4587{
4588  //PrintS("\nUsage/Allocation of temporary storage:\n");
4589  //Print("%d/%d polynomials in standard base\n",srmax,IDELEMS(Shdl));
4590  //Print("%d/%d polynomials in set L (for lazy alg.)",lrmax+1,strat->Lmax);
4591  Print("product criterion:%d chain criterion:%d\n",strat->cp,strat->c3);
4592  if (hilbcount!=0) Print("hilbert series criterion:%d\n",hilbcount);
4593  /* in usual case strat->cv is 0, it gets changed only in shift routines */
4594  if (strat->cv!=0) Print("shift V criterion:%d\n",strat->cv);
4595  /*mflush();*/
4596}
4597
4598#ifdef KDEBUG
4599/*2
4600*debugging output: all internal sets, if changed
4601*for testing purpuse only/has to be changed for later use
4602*/
4603void messageSets (kStrategy strat)
4604{
4605  int i;
4606  if (strat->news)
4607  {
4608    PrintS("set S");
4609    for (i=0; i<=strat->sl; i++)
4610    {
4611      Print("\n  %d:",i);
4612      p_wrp(strat->S[i], currRing, strat->tailRing);
4613    }
4614    strat->news = FALSE;
4615  }
4616  if (strat->newt)
4617  {
4618    PrintS("\nset T");
4619    for (i=0; i<=strat->tl; i++)
4620    {
4621      Print("\n  %d:",i);
4622      strat->T[i].wrp();
4623      Print(" o:%d e:%d l:%d",
4624        strat->T[i].pFDeg(),strat->T[i].ecart,strat->T[i].length);
4625    }
4626    strat->newt = FALSE;
4627  }
4628  PrintS("\nset L");
4629  for (i=strat->Ll; i>=0; i--)
4630  {
4631    Print("\n%d:",i);
4632    p_wrp(strat->L[i].p1, currRing, strat->tailRing);
4633    PrintS("  ");
4634    p_wrp(strat->L[i].p2, currRing, strat->tailRing);
4635    PrintS(" lcm: ");p_wrp(strat->L[i].lcm, currRing);
4636    PrintS("\n  p : ");
4637    strat->L[i].wrp();
4638    Print("  o:%d e:%d l:%d",
4639          strat->L[i].pFDeg(),strat->L[i].ecart,strat->L[i].length);
4640  }
4641  PrintLn();
4642}
4643
4644#endif
4645
4646
4647/*2
4648*construct the set s from F
4649*/
4650void initS (ideal F, ideal Q, kStrategy strat)
4651{
4652  int   i,pos;
4653
4654  if (Q!=NULL) i=((IDELEMS(F)+IDELEMS(Q)+(setmaxTinc-1))/setmaxTinc)*setmaxTinc;
4655  else         i=((IDELEMS(F)+(setmaxTinc-1))/setmaxTinc)*setmaxTinc;
4656  strat->ecartS=initec(i);
4657  strat->sevS=initsevS(i);
4658  strat->S_2_R=initS_2_R(i);
4659  strat->fromQ=NULL;
4660  strat->Shdl=idInit(i,F->rank);
4661  strat->S=strat->Shdl->m;
4662  /*- put polys into S -*/
4663  if (Q!=NULL)
4664  {
4665    strat->fromQ=initec(i);
4666    memset(strat->fromQ,0,i*sizeof(int));
4667    for (i=0; i<IDELEMS(Q); i++)
4668    {
4669      if (Q->m[i]!=NULL)
4670      {
4671        LObject h;
4672        h.p = pCopy(Q->m[i]);
4673        if (TEST_OPT_INTSTRATEGY)
4674        {
4675          //pContent(h.p);
4676          h.pCleardenom(); // also does a pContent
4677        }
4678        else
4679        {
4680          h.pNorm();
4681        }
4682        if (pOrdSgn==-1)
4683        {
4684          deleteHC(&h, strat);
4685        }
4686        if (h.p!=NULL)
4687        {
4688          strat->initEcart(&h);
4689          if (strat->sl==-1)
4690            pos =0;
4691          else
4692          {
4693            pos = posInS(strat,strat->sl,h.p,h.ecart);
4694          }
4695          h.sev = pGetShortExpVector(h.p);
4696          strat->enterS(h,pos,strat,-1);
4697          strat->fromQ[pos]=1;
4698        }
4699      }
4700    }
4701  }
4702  for (i=0; i<IDELEMS(F); i++)
4703  {
4704    if (F->m[i]!=NULL)
4705    {
4706      LObject h;
4707      h.p = pCopy(F->m[i]);
4708      if (pOrdSgn==-1)
4709      {
4710        cancelunit(&h);  /*- tries to cancel a unit -*/
4711        deleteHC(&h, strat);
4712      }
4713      if (TEST_OPT_INTSTRATEGY)
4714      {
4715        //pContent(h.p);
4716        h.pCleardenom(); // also does a pContent
4717      }
4718      else
4719      {
4720        h.pNorm();
4721      }
4722      if (h.p!=NULL)
4723      {
4724        strat->initEcart(&h);
4725        if (strat->sl==-1)
4726          pos =0;
4727        else
4728          pos = posInS(strat,strat->sl,h.p,h.ecart);
4729        h.sev = pGetShortExpVector(h.p);
4730        strat->enterS(h,pos,strat,-1);
4731      }
4732    }
4733  }
4734  /*- test, if a unit is in F -*/
4735  if ((strat->sl>=0)
4736#ifdef HAVE_RINGS
4737       && nIsUnit(pGetCoeff(strat->S[0]))
4738#endif
4739       && pIsConstant(strat->S[0]))
4740  {
4741    while (strat->sl>0) deleteInS(strat->sl,strat);
4742  }
4743}
4744
4745void initSL (ideal F, ideal Q,kStrategy strat)
4746{
4747  int   i,pos;
4748
4749  if (Q!=NULL) i=((IDELEMS(Q)+(setmaxTinc-1))/setmaxTinc)*setmaxTinc;
4750  else i=setmaxT;
4751  strat->ecartS=initec(i);
4752  strat->sevS=initsevS(i);
4753  strat->S_2_R=initS_2_R(i);
4754  strat->fromQ=NULL;
4755  strat->Shdl=idInit(i,F->rank);
4756  strat->S=strat->Shdl->m;
4757  /*- put polys into S -*/
4758  if (Q!=NULL)
4759  {
4760    strat->fromQ=initec(i);
4761    memset(strat->fromQ,0,i*sizeof(int));
4762    for (i=0; i<IDELEMS(Q); i++)
4763    {
4764      if (Q->m[i]!=NULL)
4765      {
4766        LObject h;
4767        h.p = pCopy(Q->m[i]);
4768        if (pOrdSgn==-1)
4769        {
4770          deleteHC(&h,strat);
4771        }
4772        if (TEST_OPT_INTSTRATEGY)
4773        {
4774          //pContent(h.p);
4775          h.pCleardenom(); // also does a pContent
4776        }
4777        else
4778        {
4779          h.pNorm();
4780        }
4781        if (h.p!=NULL)
4782        {
4783          strat->initEcart(&h);
4784          if (strat->sl==-1)
4785            pos =0;
4786          else
4787          {
4788            pos = posInS(strat,strat->sl,h.p,h.ecart);
4789          }
4790          h.sev = pGetShortExpVector(h.p);
4791          strat->enterS(h,pos,strat,-1);
4792          strat->fromQ[pos]=1;
4793        }
4794      }
4795    }
4796  }
4797  for (i=0; i<IDELEMS(F); i++)
4798  {
4799    if (F->m[i]!=NULL)
4800    {
4801      LObject h;
4802      h.p = pCopy(F->m[i]);
4803      if (h.p!=NULL)
4804      {
4805        if (pOrdSgn==-1)
4806        {
4807          cancelunit(&h);  /*- tries to cancel a unit -*/
4808          deleteHC(&h, strat);
4809        }
4810        if (h.p!=NULL)
4811        {
4812          if (TEST_OPT_INTSTRATEGY)
4813          {
4814            //pContent(h.p);
4815            h.pCleardenom(); // also does a pContent
4816          }
4817          else
4818          {
4819            h.pNorm();
4820          }
4821          strat->initEcart(&h);
4822          if (strat->Ll==-1)
4823            pos =0;
4824          else
4825            pos = strat->posInL(strat->L,strat->Ll,&h,strat);
4826          h.sev = pGetShortExpVector(h.p);
4827          enterL(&strat->L,&strat->Ll,&strat->Lmax,h,pos);
4828        }
4829      }
4830    }
4831  }
4832  /*- test, if a unit is in F -*/
4833
4834  if ((strat->Ll>=0) 
4835#ifdef HAVE_RINGS
4836       && nIsUnit(pGetCoeff(strat->L[strat->Ll].p))
4837#endif
4838       && pIsConstant(strat->L[strat->Ll].p))
4839  {
4840    while (strat->Ll>0) deleteInL(strat->L,&strat->Ll,strat->Ll-1,strat);
4841  }
4842}
4843
4844
4845/*2
4846*construct the set s from F and {P}
4847*/
4848void initSSpecial (ideal F, ideal Q, ideal P,kStrategy strat)
4849{
4850  int   i,pos;
4851
4852  if (Q!=NULL) i=((IDELEMS(Q)+(setmaxTinc-1))/setmaxTinc)*setmaxTinc;
4853  else i=setmaxT;
4854  i=((i+IDELEMS(F)+IDELEMS(P)+15)/16)*16;
4855  strat->ecartS=initec(i);
4856  strat->sevS=initsevS(i);
4857  strat->S_2_R=initS_2_R(i);
4858  strat->fromQ=NULL;
4859  strat->Shdl=idInit(i,F->rank);
4860  strat->S=strat->Shdl->m;
4861
4862  /*- put polys into S -*/
4863  if (Q!=NULL)
4864  {
4865    strat->fromQ=initec(i);
4866    memset(strat->fromQ,0,i*sizeof(int));
4867    for (i=0; i<IDELEMS(Q); i++)
4868    {
4869      if (Q->m[i]!=NULL)
4870      {
4871        LObject h;
4872        h.p = pCopy(Q->m[i]);
4873        //if (TEST_OPT_INTSTRATEGY)
4874        //{
4875        //  //pContent(h.p);
4876        //  h.pCleardenom(); // also does a pContent
4877        //}
4878        //else
4879        //{
4880        //  h.pNorm();
4881        //}
4882        if (pOrdSgn==-1)
4883        {
4884          deleteHC(&h,strat);
4885        }
4886        if (h.p!=NULL)
4887        {
4888          strat->initEcart(&h);
4889          if (strat->sl==-1)
4890            pos =0;
4891          else
4892          {
4893            pos = posInS(strat,strat->sl,h.p,h.ecart);
4894          }
4895          h.sev = pGetShortExpVector(h.p);
4896          strat->enterS(h,pos,strat, strat->tl+1);
4897          enterT(h, strat);
4898          strat->fromQ[pos]=1;
4899        }
4900      }
4901    }
4902  }
4903  /*- put polys into S -*/
4904  for (i=0; i<IDELEMS(F); i++)
4905  {
4906    if (F->m[i]!=NULL)
4907    {
4908      LObject h;
4909      h.p = pCopy(F->m[i]);
4910      if (pOrdSgn==-1)
4911      {
4912        deleteHC(&h,strat);
4913      }
4914      else
4915      {
4916        h.p=redtailBba(h.p,strat->sl,strat);
4917      }
4918      if (h.p!=NULL)
4919      {
4920        strat->initEcart(&h);
4921        if (strat->sl==-1)
4922          pos =0;
4923        else
4924          pos = posInS(strat,strat->sl,h.p,h.ecart);
4925        h.sev = pGetShortExpVector(h.p);
4926        strat->enterS(h,pos,strat, strat->tl+1);
4927        enterT(h,strat);
4928      }
4929    }
4930  }
4931  for (i=0; i<IDELEMS(P); i++)
4932  {
4933    if (P->m[i]!=NULL)
4934    {
4935      LObject h;
4936      h.p=pCopy(P->m[i]);
4937      if (TEST_OPT_INTSTRATEGY)
4938      {
4939        h.pCleardenom();
4940      }
4941      else
4942      {
4943        h.pNorm();
4944      }
4945      if(strat->sl>=0)
4946      {
4947        if (pOrdSgn==1)
4948        {
4949          h.p=redBba(h.p,strat->sl,strat);
4950          if (h.p!=NULL)
4951          {
4952            h.p=redtailBba(h.p,strat->sl,strat);
4953          }
4954        }
4955        else
4956        {
4957          h.p=redMora(h.p,strat->sl,strat);
4958        }
4959        if(h.p!=NULL)
4960        {
4961          strat->initEcart(&h);
4962          if (TEST_OPT_INTSTRATEGY)
4963          {
4964            h.pCleardenom();
4965          }
4966          else
4967          {
4968            h.is_normalized = 0;
4969            h.pNorm();
4970          }
4971          h.sev = pGetShortExpVector(h.p);
4972          h.SetpFDeg();
4973          pos = posInS(strat,strat->sl,h.p,h.ecart);
4974          enterpairsSpecial(h.p,strat->sl,h.ecart,pos,strat,strat->tl+1);
4975          strat->enterS(h,pos,strat, strat->tl+1);
4976          enterT(h,strat);
4977        }
4978      }
4979      else
4980      {
4981        h.sev = pGetShortExpVector(h.p);
4982        strat->initEcart(&h);
4983        strat->enterS(h,0,strat, strat->tl+1);
4984        enterT(h,strat);
4985      }
4986    }
4987  }
4988}
4989/*2
4990* reduces h using the set S
4991* procedure used in cancelunit1
4992*/
4993static poly redBba1 (poly h,int maxIndex,kStrategy strat)
4994{
4995  int j = 0;
4996  unsigned long not_sev = ~ pGetShortExpVector(h);
4997
4998  while (j <= maxIndex)
4999  {
5000    if (pLmShortDivisibleBy(strat->S[j],strat->sevS[j],h, not_sev))
5001       return ksOldSpolyRedNew(strat->S[j],h,strat->kNoetherTail());
5002    else j++;
5003  }
5004  return h;
5005}
5006
5007/*2
5008*tests if p.p=monomial*unit and cancels the unit
5009*/
5010void cancelunit1 (LObject* p,int *suc, int index,kStrategy strat )
5011{
5012  int k;
5013  poly r,h,h1,q;
5014
5015  if (!pIsVector((*p).p) && ((*p).ecart != 0))
5016  {
5017#ifdef HAVE_RINGS_LOC
5018    // Leading coef have to be a unit
5019    if ( !(nIsUnit(p_GetCoeff((*p).p, r))) ) return;
5020#endif
5021    k = 0;
5022    h1 = r = pCopy((*p).p);
5023    h =pNext(r);
5024    loop
5025    {
5026      if (h==NULL)
5027      {
5028        pDelete(&r);
5029        pDelete(&(pNext((*p).p)));
5030        (*p).ecart = 0;
5031        (*p).length = 1;
5032#ifdef HAVE_RINGS_LOC
5033        (*p).pLength = 1;  // Why wasn't this set already?
5034#endif
5035        (*suc)=0;
5036        return;
5037      }
5038      if (!pDivisibleBy(r,h))
5039      {
5040        q=redBba1(h,index ,strat);
5041        if (q != h)
5042        {
5043          k++;
5044          pDelete(&h);
5045          pNext(h1) = h = q;
5046        }
5047        else
5048        {
5049          pDelete(&r);
5050          return;
5051        }
5052      }
5053      else
5054      {
5055        h1 = h;
5056        pIter(h);
5057      }
5058      if (k > 10)
5059      {
5060        pDelete(&r);
5061        return;
5062      }
5063    }
5064  }
5065}
5066
5067#if 0
5068/*2
5069* reduces h using the elements from Q in the set S
5070* procedure used in updateS
5071* must not be used for elements of Q or elements of an ideal !
5072*/
5073static poly redQ (poly h, int j, kStrategy strat)
5074{
5075  int start;
5076  unsigned long not_sev = ~ pGetShortExpVector(h);
5077  while ((j <= strat->sl) && (pGetComp(strat->S[j])!=0)) j++;
5078  start=j;
5079  while (j<=strat->sl)
5080  {
5081    if (pLmShortDivisibleBy(strat->S[j],strat->sevS[j], h, not_sev))
5082    {
5083      h = ksOldSpolyRed(strat->S[j],h,strat->kNoetherTail());
5084      if (h==NULL) return NULL;
5085      j = start;
5086      not_sev = ~ pGetShortExpVector(h);
5087    }
5088    else j++;
5089  }
5090  return h;
5091}
5092#endif
5093
5094/*2
5095* reduces h using the set S
5096* procedure used in updateS
5097*/
5098static poly redBba (poly h,int maxIndex,kStrategy strat)
5099{
5100  int j = 0;
5101  unsigned long not_sev = ~ pGetShortExpVector(h);
5102
5103  while (j <= maxIndex)
5104  {
5105    if (pLmShortDivisibleBy(strat->S[j],strat->sevS[j], h, not_sev))
5106    {
5107      h = ksOldSpolyRed(strat->S[j],h,strat->kNoetherTail());
5108      if (h==NULL) return NULL;
5109      j = 0;
5110      not_sev = ~ pGetShortExpVector(h);    }
5111    else j++;
5112  }
5113  return h;
5114}
5115
5116/*2
5117* reduces h using the set S
5118*e is the ecart of h
5119*procedure used in updateS
5120*/
5121static poly redMora (poly h,int maxIndex,kStrategy strat)
5122{
5123  int  j=0;
5124  int  e,l;
5125  unsigned long not_sev = ~ pGetShortExpVector(h);
5126
5127  if (maxIndex >= 0)
5128  {
5129    e = pLDeg(h,&l,currRing)-pFDeg(h,currRing);
5130    do
5131    {
5132      if (pLmShortDivisibleBy(strat->S[j],strat->sevS[j], h, not_sev)
5133      && ((e >= strat->ecartS[j]) || strat->kHEdgeFound))
5134      {
5135#ifdef KDEBUG
5136        if (TEST_OPT_DEBUG)
5137          {PrintS("reduce ");wrp(h);Print(" with S[%d] (",j);wrp(strat->S[j]);}
5138#endif
5139        h = ksOldSpolyRed(strat->S[j],h,strat->kNoetherTail());
5140#ifdef KDEBUG
5141        if(TEST_OPT_DEBUG)
5142          {PrintS(")\nto "); wrp(h); PrintLn();}
5143#endif
5144        // pDelete(&h);
5145        if (h == NULL) return NULL;
5146        e = pLDeg(h,&l,currRing)-pFDeg(h,currRing);
5147        j = 0;
5148        not_sev = ~ pGetShortExpVector(h);
5149      }
5150      else j++;
5151    }
5152    while (j <= maxIndex);
5153  }
5154  return h;
5155}
5156
5157/*2
5158*updates S:
5159*the result is a set of polynomials which are in
5160*normalform with respect to S
5161*/
5162void updateS(BOOLEAN toT,kStrategy strat)
5163{
5164  LObject h;
5165  int i, suc=0;
5166  poly redSi=NULL;
5167  BOOLEAN change,any_change;
5168//  Print("nach initS: updateS start mit sl=%d\n",(strat->sl));
5169//  for (i=0; i<=(strat->sl); i++)
5170//  {
5171//    Print("s%d:",i);
5172//    if (strat->fromQ!=NULL) Print("(Q:%d) ",strat->fromQ[i]);
5173//    pWrite(strat->S[i]);
5174//  }
5175//  Print("pOrdSgn=%d\n", pOrdSgn);
5176  any_change=FALSE;
5177  if (pOrdSgn==1)
5178  {
5179    while (suc != -1)
5180    {
5181      i=suc+1;
5182      while (i<=strat->sl)
5183      {
5184        change=FALSE;
5185        if (((strat->fromQ==NULL) || (strat->fromQ[i]==0)) && (i>0))
5186        {
5187          redSi = pHead(strat->S[i]);
5188          strat->S[i] = redBba(strat->S[i],i-1,strat);
5189          //if ((strat->ak!=0)&&(strat->S[i]!=NULL))
5190          //  strat->S[i]=redQ(strat->S[i],i+1,strat); /*reduce S[i] mod Q*/
5191          if (pCmp(redSi,strat->S[i])!=0)
5192          {
5193            change=TRUE;
5194            any_change=TRUE;
5195            #ifdef KDEBUG
5196            if (TEST_OPT_DEBUG)
5197            {
5198              PrintS("reduce:");
5199              wrp(redSi);PrintS(" to ");p_wrp(strat->S[i], currRing, strat->tailRing);PrintLn();
5200            }
5201            #endif
5202            if (TEST_OPT_PROT)
5203            {
5204              if (strat->S[i]==NULL)
5205                PrintS("V");
5206              else
5207                PrintS("v");
5208              mflush();
5209            }
5210          }
5211          pDeleteLm(&redSi);
5212          if (strat->S[i]==NULL)
5213          {
5214            deleteInS(i,strat);
5215            i--;
5216          }
5217          else if (change)
5218          {
5219            if (TEST_OPT_INTSTRATEGY)
5220            {
5221              //pContent(strat->S[i]);
5222              pCleardenom(strat->S[i]);// also does a pContent
5223            }
5224            else
5225            {
5226              pNorm(strat->S[i]);
5227            }
5228            strat->sevS[i] = pGetShortExpVector(strat->S[i]);
5229          }
5230        }
5231        i++;
5232      }
5233      if (any_change) reorderS(&suc,strat);
5234      else break;
5235    }
5236    if (toT)
5237    {
5238      for (i=0; i<=strat->sl; i++)
5239      {
5240        if ((strat->fromQ==NULL) || (strat->fromQ[i]==0))
5241        {
5242          h.p = redtailBba(strat->S[i],i-1,strat);
5243          if (TEST_OPT_INTSTRATEGY)
5244          {
5245            pCleardenom(h.p);// also does a pContent
5246          }
5247        }
5248        else
5249        {
5250          h.p = strat->S[i];
5251        }
5252        strat->initEcart(&h);
5253        if (strat->honey)
5254        {
5255          strat->ecartS[i] = h.ecart;
5256        }
5257        if (strat->sevS[i] == 0) {strat->sevS[i] = pGetShortExpVector(h.p);}
5258        else assume(strat->sevS[i] == pGetShortExpVector(h.p));
5259        h.sev = strat->sevS[i];
5260        /*puts the elements of S also to T*/
5261        enterT(h,strat);
5262        strat->S_2_R[i] = strat->tl;
5263      }
5264    }
5265  }
5266  else
5267  {
5268    while (suc != -1)
5269    {
5270      i=suc;
5271      while (i<=strat->sl)
5272      {
5273        change=FALSE;
5274        if (((strat->fromQ==NULL) || (strat->fromQ[i]==0)) && (i>0))
5275        {
5276          redSi=pHead((strat->S)[i]);
5277          (strat->S)[i] = redMora((strat->S)[i],i-1,strat);
5278          if ((strat->S)[i]==NULL)
5279          {
5280            deleteInS(i,strat);
5281            i--;
5282          }
5283          else if (pCmp((strat->S)[i],redSi)!=0)
5284          {
5285            any_change=TRUE;
5286            h.p = strat->S[i];
5287            strat->initEcart(&h);
5288            strat->ecartS[i] = h.ecart;
5289            if (TEST_OPT_INTSTRATEGY)
5290            {
5291              pCleardenom(strat->S[i]);// also does a pContent
5292            }
5293            else
5294            {
5295              pNorm(strat->S[i]); // == h.p
5296            }
5297            h.sev =  pGetShortExpVector(h.p);
5298            strat->sevS[i] = h.sev;
5299          }
5300          pDeleteLm(&redSi);
5301          kTest(strat);
5302        }
5303        i++;
5304      }
5305#ifdef KDEBUG
5306      kTest(strat);
5307#endif
5308      if (any_change) reorderS(&suc,strat);
5309      else { suc=-1; break; }
5310      if (h.p!=NULL)
5311      {
5312        if (!strat->kHEdgeFound)
5313        {
5314          /*strat->kHEdgeFound =*/ HEckeTest(h.p,strat);
5315        }
5316        if (strat->kHEdgeFound)
5317          newHEdge(strat->S,strat);
5318      }
5319    }
5320    for (i=0; i<=strat->sl; i++)
5321    {
5322      if ((strat->fromQ==NULL) || (strat->fromQ[i]==0))
5323      {
5324        strat->S[i] = h.p = redtail(strat->S[i],strat->sl,strat);
5325        strat->initEcart(&h);
5326        strat->ecartS[i] = h.ecart;
5327        h.sev = pGetShortExpVector(h.p);
5328        strat->sevS[i] = h.sev;
5329      }
5330      else
5331      {
5332        h.p = strat->S[i];
5333        h.ecart=strat->ecartS[i];
5334        h.sev = strat->sevS[i];
5335        h.length = h.pLength = pLength(h.p);
5336      }
5337      if ((strat->fromQ==NULL) || (strat->fromQ[i]==0))
5338        cancelunit1(&h,&suc,strat->sl,strat);
5339      h.SetpFDeg();
5340      /*puts the elements of S also to T*/
5341      enterT(h,strat);
5342      strat->S_2_R[i] = strat->tl;
5343    }
5344    if (suc!= -1) updateS(toT,strat);
5345  }
5346#ifdef KDEBUG
5347  kTest(strat);
5348#endif
5349}
5350
5351
5352/*2
5353* -puts p to the standardbasis s at position at
5354* -saves the result in S
5355*/
5356void enterSBba (LObject p,int atS,kStrategy strat, int atR)
5357{
5358  int i;
5359  strat->news = TRUE;
5360  /*- puts p to the standardbasis s at position at -*/
5361  if (strat->sl == IDELEMS(strat->Shdl)-1)
5362  {
5363    strat->sevS = (unsigned long*) omRealloc0Size(strat->sevS,
5364                                    IDELEMS(strat->Shdl)*sizeof(unsigned long),
5365                                    (IDELEMS(strat->Shdl)+setmaxTinc)
5366                                                  *sizeof(unsigned long));
5367    strat->ecartS = (intset)omReallocSize(strat->ecartS,
5368                                          IDELEMS(strat->Shdl)*sizeof(int),
5369                                          (IDELEMS(strat->Shdl)+setmaxTinc)
5370                                                  *sizeof(int));
5371    strat->S_2_R = (int*) omRealloc0Size(strat->S_2_R,
5372                                         IDELEMS(strat->Shdl)*sizeof(int),
5373                                         (IDELEMS(strat->Shdl)+setmaxTinc)
5374                                                  *sizeof(int));
5375    if (strat->lenS!=NULL)
5376      strat->lenS=(int*)omRealloc0Size(strat->lenS,
5377                                       IDELEMS(strat->Shdl)*sizeof(int),
5378                                       (IDELEMS(strat->Shdl)+setmaxTinc)
5379                                                 *sizeof(int));
5380    if (strat->lenSw!=NULL)
5381      strat->lenSw=(wlen_type*)omRealloc0Size(strat->lenSw,
5382                                       IDELEMS(strat->Shdl)*sizeof(wlen_type),
5383                                       (IDELEMS(strat->Shdl)+setmaxTinc)
5384                                                 *sizeof(wlen_type));
5385    if (strat->fromQ!=NULL)
5386    {
5387      strat->fromQ = (intset)omReallocSize(strat->fromQ,
5388                                    IDELEMS(strat->Shdl)*sizeof(int),
5389                                    (IDELEMS(strat->Shdl)+setmaxTinc)*sizeof(int));
5390    }
5391    pEnlargeSet(&strat->S,IDELEMS(strat->Shdl),setmaxTinc);
5392    IDELEMS(strat->Shdl)+=setmaxTinc;
5393    strat->Shdl->m=strat->S;
5394  }
5395  if (atS <= strat->sl)
5396  {
5397#ifdef ENTER_USE_MEMMOVE
5398// #if 0
5399    memmove(&(strat->S[atS+1]), &(strat->S[atS]),
5400            (strat->sl - atS + 1)*sizeof(poly));
5401    memmove(&(strat->ecartS[atS+1]), &(strat->ecartS[atS]),
5402            (strat->sl - atS + 1)*sizeof(int));
5403    memmove(&(strat->sevS[atS+1]), &(strat->sevS[atS]),
5404            (strat->sl - atS + 1)*sizeof(unsigned long));
5405    memmove(&(strat->S_2_R[atS+1]), &(strat->S_2_R[atS]),
5406            (strat->sl - atS + 1)*sizeof(int));
5407    if (strat->lenS!=NULL)
5408    memmove(&(strat->lenS[atS+1]), &(strat->lenS[atS]),
5409            (strat->sl - atS + 1)*sizeof(int));
5410    if (strat->lenSw!=NULL)
5411    memmove(&(strat->lenSw[atS+1]), &(strat->lenSw[atS]),
5412            (strat->sl - atS + 1)*sizeof(wlen_type));
5413#else
5414    for (i=strat->sl+1; i>=atS+1; i--)
5415    {
5416      strat->S[i] = strat->S[i-1];
5417      strat->ecartS[i] = strat->ecartS[i-1];
5418      strat->sevS[i] = strat->sevS[i-1];
5419      strat->S_2_R[i] = strat->S_2_R[i-1];
5420    }
5421    if (strat->lenS!=NULL)
5422    for (i=strat->sl+1; i>=atS+1; i--)
5423      strat->lenS[i] = strat->lenS[i-1];
5424    if (strat->lenSw!=NULL)
5425    for (i=strat->sl+1; i>=atS+1; i--)
5426      strat->lenSw[i] = strat->lenSw[i-1];
5427#endif
5428  }
5429  if (strat->fromQ!=NULL)
5430  {
5431#ifdef ENTER_USE_MEMMOVE
5432    memmove(&(strat->fromQ[atS+1]), &(strat->fromQ[atS]),
5433                  (strat->sl - atS + 1)*sizeof(int));
5434#else
5435    for (i=strat->sl+1; i>=atS+1; i--)
5436    {
5437      strat->fromQ[i] = strat->fromQ[i-1];
5438    }
5439#endif
5440    strat->fromQ[atS]=0;
5441  }
5442
5443  /*- save result -*/
5444  strat->S[atS] = p.p;
5445  if (strat->honey) strat->ecartS[atS] = p.ecart;
5446  if (p.sev == 0)
5447    p.sev = pGetShortExpVector(p.p);
5448  else
5449    assume(p.sev == pGetShortExpVector(p.p));
5450  strat->sevS[atS] = p.sev;
5451  strat->ecartS[atS] = p.ecart;
5452  strat->S_2_R[atS] = atR;
5453  strat->sl++;
5454}
5455
5456/*2
5457* puts p to the set T at position atT
5458*/
5459void enterT(LObject p, kStrategy strat, int atT)
5460{
5461  int i;
5462
5463  pp_Test(p.p, currRing, p.tailRing);
5464  assume(strat->tailRing == p.tailRing);
5465  // redMoraNF complains about this -- but, we don't really
5466  // neeed this so far
5467  assume(p.pLength == 0 || pLength(p.p) == p.pLength);
5468  assume(p.FDeg == p.pFDeg());
5469  assume(!p.is_normalized || nIsOne(pGetCoeff(p.p)));
5470
5471#ifdef KDEBUG 
5472  // do not put an LObject twice into T:
5473  for(i=strat->tl;i>=0;i--)
5474  {
5475    if (p.p==strat->T[i].p) 
5476    {
5477      printf("already in T at pos %d of %d, atT=%d\n",i,strat->tl,atT);
5478      return;
5479    }
5480  }
5481#endif 
5482  strat->newt = TRUE;
5483  if (atT < 0)
5484    atT = strat->posInT(strat->T, strat->tl, p);
5485  if (strat->tl == strat->tmax-1)
5486    enlargeT(strat->T,strat->R,strat->sevT,strat->tmax,setmaxTinc);
5487  if (atT <= strat->tl)
5488  {
5489#ifdef ENTER_USE_MEMMOVE
5490    memmove(&(strat->T[atT+1]), &(strat->T[atT]),
5491            (strat->tl-atT+1)*sizeof(TObject));
5492    memmove(&(strat->sevT[atT+1]), &(strat->sevT[atT]),
5493            (strat->tl-atT+1)*sizeof(unsigned long));
5494#endif
5495    for (i=strat->tl+1; i>=atT+1; i--)
5496    {
5497#ifndef ENTER_USE_MEMMOVE
5498      strat->T[i] = strat->T[i-1];
5499      strat->sevT[i] = strat->sevT[i-1];
5500#endif
5501      strat->R[strat->T[i].i_r] = &(strat->T[i]);
5502    }
5503  }
5504
5505  if (strat->tailBin != NULL && (pNext(p.p) != NULL))
5506  {
5507    pNext(p.p)=p_ShallowCopyDelete(pNext(p.p),
5508                                   (strat->tailRing != NULL ?
5509                                    strat->tailRing : currRing),
5510                                   strat->tailBin);
5511    if (p.t_p != NULL) pNext(p.t_p) = pNext(p.p);
5512  }
5513  strat->T[atT] = (TObject) p;
5514
5515  if (strat->tailRing != currRing && pNext(p.p) != NULL)
5516    strat->T[atT].max = p_GetMaxExpP(pNext(p.p), strat->tailRing);
5517  else
5518    strat->T[atT].max = NULL;
5519
5520  strat->tl++;
5521  strat->R[strat->tl] = &(strat->T[atT]);
5522  strat->T[atT].i_r = strat->tl;
5523  assume(p.sev == 0 || pGetShortExpVector(p.p) == p.sev);
5524  strat->sevT[atT] = (p.sev == 0 ? pGetShortExpVector(p.p) : p.sev);
5525  kTest_T(&(strat->T[atT]));
5526}
5527
5528void initHilbCrit(ideal F, ideal Q, intvec **hilb,kStrategy strat)
5529{
5530  if (strat->homog!=isHomog)
5531  {
5532    *hilb=NULL;
5533  }
5534}
5535
5536void initBuchMoraCrit(kStrategy strat)
5537{
5538  strat->enterOnePair=enterOnePairNormal;
5539  strat->chainCrit=chainCritNormal;
5540#ifdef HAVE_RINGS
5541  if (rField_is_Ring(currRing))
5542  {
5543    strat->enterOnePair=enterOnePairRing;
5544    strat->chainCrit=chainCritRing;
5545  }
5546#endif
5547#ifdef HAVE_RATGRING
5548  if (rIsRatGRing(currRing))
5549  {
5550     strat->chainCrit=chainCritPart;
5551  }
5552#endif
5553
5554  strat->sugarCrit =        TEST_OPT_SUGARCRIT;
5555  // obachman: Hmm.. I need BTEST1(2) for notBuckets ..
5556  //  strat->Gebauer =          BTEST1(2) || strat->homog || strat->sugarCrit;
5557  strat->Gebauer =          strat->homog || strat->sugarCrit;
5558  strat->honey =            !strat->homog || strat->sugarCrit || TEST_OPT_WEIGHTM;
5559  if (TEST_OPT_NOT_SUGAR) strat->honey = FALSE;
5560  strat->pairtest = NULL;
5561  /* alway use tailreduction, except:
5562  * - in local rings, - in lex order case, -in ring over extensions */
5563  strat->noTailReduction = !TEST_OPT_REDTAIL;
5564
5565#ifdef HAVE_PLURAL
5566  // and r is plural_ring
5567  if( rIsPluralRing(currRing) || (rIsSCA(currRing) && !strat->z2homog) )
5568  {    //or it has non-quasi-comm type... later
5569    strat->sugarCrit = FALSE;
5570    strat->Gebauer = FALSE;
5571    strat->honey = FALSE;
5572  }
5573#endif
5574
5575#ifdef HAVE_RINGS
5576  // Coefficient ring?
5577  if (rField_is_Ring(currRing))
5578  {
5579    strat->sugarCrit = FALSE;
5580    strat->Gebauer = FALSE ;
5581    strat->honey = FALSE;
5582  }
5583#endif
5584  #ifdef KDEBUG
5585  if (TEST_OPT_DEBUG)
5586  {
5587    if (strat->homog) PrintS("ideal/module is homogeneous\n");
5588    else              PrintS("ideal/module is not homogeneous\n");
5589  }
5590  #endif
5591}
5592
5593BOOLEAN kPosInLDependsOnLength(int (*pos_in_l)
5594                               (const LSet set, const int length,
5595                                LObject* L,const kStrategy strat))
5596{
5597  if (pos_in_l == posInL110 ||
5598      pos_in_l == posInL10)
5599    return TRUE;
5600
5601  return FALSE;
5602}
5603
5604void initBuchMoraPos (kStrategy strat)
5605{
5606  if (pOrdSgn==1)
5607  {
5608    if (strat->honey)
5609    {
5610      strat->posInL = posInL15;
5611      // ok -- here is the deal: from my experiments for Singular-2-0
5612      // I conclude that that posInT_EcartpLength is the best of
5613      // posInT15, posInT_EcartFDegpLength, posInT_FDegLength, posInT_pLength
5614      // see the table at the end of this file
5615      if (K_TEST_OPT_OLDSTD)
5616        strat->posInT = posInT15;
5617      else
5618        strat->posInT = posInT_EcartpLength;
5619    }
5620    else if (pLexOrder && !TEST_OPT_INTSTRATEGY)
5621    {
5622      strat->posInL = posInL11;
5623      strat->posInT = posInT11;
5624    }
5625    else if (TEST_OPT_INTSTRATEGY)
5626    {
5627      strat->posInL = posInL11;
5628      strat->posInT = posInT11;
5629    }
5630    else
5631    {
5632      strat->posInL = posInL0;
5633      strat->posInT = posInT0;
5634    }
5635    //if (strat->minim>0) strat->posInL =posInLSpecial;
5636    if (strat->homog)
5637    {
5638       strat->posInL = posInL110;
5639       strat->posInT = posInT110;
5640    }
5641  }
5642  else
5643  {
5644    if (strat->homog)
5645    {
5646      strat->posInL = posInL11;
5647      strat->posInT = posInT11;
5648    }
5649    else
5650    {
5651      if ((currRing->order[0]==ringorder_c)
5652      ||(currRing->order[0]==ringorder_C))
5653      {
5654        strat->posInL = posInL17_c;
5655        strat->posInT = posInT17_c;
5656      }
5657      else
5658      {
5659        strat->posInL = posInL17;
5660        strat->posInT = posInT17;
5661      }
5662    }
5663  }
5664  if (strat->minim>0) strat->posInL =posInLSpecial;
5665  // for further tests only
5666  if ((BTEST1(11)) || (BTEST1(12)))
5667    strat->posInL = posInL11;
5668  else if ((BTEST1(13)) || (BTEST1(14)))
5669    strat->posInL = posInL13;
5670  else if ((BTEST1(15)) || (BTEST1(16)))
5671    strat->posInL = posInL15;
5672  else if ((BTEST1(17)) || (BTEST1(18)))
5673    strat->posInL = posInL17;
5674  if (BTEST1(11))
5675    strat->posInT = posInT11;
5676  else if (BTEST1(13))
5677    strat->posInT = posInT13;
5678  else if (BTEST1(15))
5679    strat->posInT = posInT15;
5680  else if ((BTEST1(17)))
5681    strat->posInT = posInT17;
5682  else if ((BTEST1(19)))
5683    strat->posInT = posInT19;
5684  else if (BTEST1(12) || BTEST1(14) || BTEST1(16) || BTEST1(18))
5685    strat->posInT = posInT1;
5686#ifdef HAVE_RINGS
5687  if (rField_is_Ring(currRing))
5688  {
5689    strat->posInL = posInL11;
5690    strat->posInT = posInT11;
5691  }
5692#endif
5693  strat->posInLDependsOnLength = kPosInLDependsOnLength(strat->posInL);
5694}
5695
5696void initBuchMora (ideal F,ideal Q,kStrategy strat)
5697{
5698  strat->interpt = BTEST1(OPT_INTERRUPT);
5699  strat->kHEdge=NULL;
5700  if (pOrdSgn==1) strat->kHEdgeFound=FALSE;
5701  /*- creating temp data structures------------------- -*/
5702  strat->cp = 0;
5703  strat->c3 = 0;
5704  strat->tail = pInit();
5705  /*- set s -*/
5706  strat->sl = -1;
5707  /*- set L -*/
5708  strat->Lmax = ((IDELEMS(F)+setmaxLinc-1)/setmaxLinc)*setmaxLinc;
5709  strat->Ll = -1;
5710  strat->L = initL(((IDELEMS(F)+setmaxLinc-1)/setmaxLinc)*setmaxLinc);
5711  /*- set B -*/
5712  strat->Bmax = setmaxL;
5713  strat->Bl = -1;
5714  strat->B = initL();
5715  /*- set T -*/
5716  strat->tl = -1;
5717  strat->tmax = setmaxT;
5718  strat->T = initT();
5719  strat->R = initR();
5720  strat->sevT = initsevT();
5721  /*- init local data struct.---------------------------------------- -*/
5722  strat->P.ecart=0;
5723  strat->P.length=0;
5724  if (pOrdSgn==-1)
5725  {
5726    if (strat->kHEdge!=NULL) pSetComp(strat->kHEdge, strat->ak);
5727    if (strat->kNoether!=NULL) pSetComp(strat->kNoetherTail(), strat->ak);
5728  }
5729  if(TEST_OPT_SB_1)
5730  {
5731    int i;
5732    ideal P=idInit(IDELEMS(F)-strat->newIdeal,F->rank);
5733    for (i=strat->newIdeal;i<IDELEMS(F);i++)
5734    {
5735      P->m[i-strat->newIdeal] = F->m[i];
5736      F->m[i] = NULL;
5737    }
5738    initSSpecial(F,Q,P,strat);
5739    for (i=strat->newIdeal;i<IDELEMS(F);i++)
5740    {
5741      F->m[i] = P->m[i-strat->newIdeal];
5742      P->m[i-strat->newIdeal] = NULL;
5743    }
5744    idDelete(&P);
5745  }
5746  else
5747  {
5748    /*Shdl=*/initSL(F, Q,strat); /*sets also S, ecartS, fromQ */
5749    // /*Shdl=*/initS(F, Q,strat); /*sets also S, ecartS, fromQ */
5750  }
5751  strat->kIdeal = NULL;
5752  strat->fromT = FALSE;
5753  strat->noTailReduction = !TEST_OPT_REDTAIL;
5754  if (!TEST_OPT_SB_1)
5755  {
5756    updateS(TRUE,strat);
5757  }
5758  if (strat->fromQ!=NULL) omFreeSize(strat->fromQ,IDELEMS(strat->Shdl)*sizeof(int));
5759  strat->fromQ=NULL;
5760}
5761
5762void exitBuchMora (kStrategy strat)
5763{
5764  /*- release temp data -*/
5765  cleanT(strat);
5766  omFreeSize(strat->T,(strat->tmax)*sizeof(TObject));
5767  omFreeSize(strat->R,(strat->tmax)*sizeof(TObject*));
5768  omFreeSize(strat->sevT, (strat->tmax)*sizeof(unsigned long));
5769  omFreeSize(strat->ecartS,IDELEMS(strat->Shdl)*sizeof(int));
5770  omFreeSize(strat->sevS,IDELEMS(strat->Shdl)*sizeof(int));
5771  omFreeSize(strat->S_2_R,IDELEMS(strat->Shdl)*sizeof(int));
5772  /*- set L: should be empty -*/
5773  omFreeSize(strat->L,(strat->Lmax)*sizeof(LObject));
5774  /*- set B: should be empty -*/
5775  omFreeSize(strat->B,(strat->Bmax)*sizeof(LObject));
5776  pDeleteLm(&strat->tail);
5777  strat->syzComp=0;
5778  if (strat->kIdeal!=NULL)
5779  {
5780    omFreeBin(strat->kIdeal, sleftv_bin);
5781    strat->kIdeal=NULL;
5782  }
5783}
5784
5785/*2
5786* in the case of a standardbase of a module over a qring:
5787* replace polynomials in i by ak vectors,
5788* (the polynomial * unit vectors gen(1)..gen(ak)
5789* in every case (also for ideals:)
5790* deletes divisible vectors/polynomials
5791*/
5792void updateResult(ideal r,ideal Q, kStrategy strat)
5793{
5794  int l;
5795  if (strat->ak>0)
5796  {
5797    for (l=IDELEMS(r)-1;l>=0;l--)
5798    {
5799      if ((r->m[l]!=NULL) && (pGetComp(r->m[l])==0))
5800      {
5801        pDelete(&r->m[l]); // and set it to NULL
5802      }
5803    }
5804    int q;
5805    poly p;
5806    for (l=IDELEMS(r)-1;l>=0;l--)
5807    {
5808      if ((r->m[l]!=NULL)
5809      && (strat->syzComp>0)
5810      && (pGetComp(r->m[l])<=strat->syzComp))
5811      {
5812        for(q=IDELEMS(Q)-1; q>=0;q--)
5813        {
5814          if ((Q->m[q]!=NULL)
5815          &&(pLmDivisibleBy(Q->m[q],r->m[l])))
5816          {
5817            if (TEST_OPT_REDSB)
5818            {
5819              p=r->m[l];
5820              r->m[l]=kNF(Q,NULL,p);
5821              pDelete(&p);
5822            }
5823            else
5824            {
5825              pDelete(&r->m[l]); // and set it to NULL
5826            }
5827            break;
5828          }
5829        }
5830      }
5831    }
5832  }
5833  else
5834  {
5835    int q;
5836    poly p;
5837    for (l=IDELEMS(r)-1;l>=0;l--)
5838    {
5839      if (r->m[l]!=NULL)
5840      {
5841        for(q=IDELEMS(Q)-1; q>=0;q--)
5842        {
5843          if ((Q->m[q]!=NULL)
5844          &&(pLmEqual(r->m[l],Q->m[q])))
5845          {
5846            if (TEST_OPT_REDSB)
5847            {
5848              p=r->m[l];
5849              r->m[l]=kNF(Q,NULL,p);
5850              pDelete(&p);
5851            }
5852            else
5853            {
5854              pDelete(&r->m[l]); // and set it to NULL
5855            }
5856            break;
5857          }
5858        }
5859      }
5860    }
5861  }
5862  idSkipZeroes(r);
5863}
5864
5865void completeReduce (kStrategy strat, BOOLEAN withT)
5866{
5867  int i;
5868  int low = (pOrdSgn == 1 ? 1 : 0);
5869  LObject L;
5870
5871#ifdef KDEBUG
5872  // need to set this: during tailreductions of T[i], T[i].max is out of
5873  // sync
5874  sloppy_max = TRUE;
5875#endif
5876
5877  strat->noTailReduction = FALSE;
5878  if (TEST_OPT_PROT)
5879  {
5880    PrintLn();
5881    if (timerv) writeTime("standard base computed:");
5882  }
5883  if (TEST_OPT_PROT)
5884  {
5885    Print("(S:%d)",strat->sl);mflush();
5886  }
5887  for (i=strat->sl; i>=low; i--)
5888  {
5889    TObject* T_j = strat->s_2_t(i);
5890    if (T_j != NULL)
5891    {
5892      L = *T_j;
5893      poly p;
5894      if (pOrdSgn == 1)
5895        strat->S[i] = redtailBba(&L, i-1, strat, withT);
5896      else
5897        strat->S[i] = redtail(&L, strat->sl, strat);
5898
5899      if (strat->redTailChange && strat->tailRing != currRing)
5900      {
5901        if (T_j->max != NULL) p_LmFree(T_j->max, strat->tailRing);
5902        if (pNext(T_j->p) != NULL)
5903          T_j->max = p_GetMaxExpP(pNext(T_j->p), strat->tailRing);
5904        else
5905          T_j->max = NULL;
5906      }
5907      if (TEST_OPT_INTSTRATEGY)
5908        T_j->pCleardenom();
5909    }
5910    else
5911    {
5912      assume(currRing == strat->tailRing);
5913      if (pOrdSgn == 1)
5914        strat->S[i] = redtailBba(strat->S[i], i-1, strat, withT);
5915      else
5916        strat->S[i] = redtail(strat->S[i], strat->sl, strat);
5917      if (TEST_OPT_INTSTRATEGY)
5918        pCleardenom(strat->S[i]);
5919    }
5920    if (TEST_OPT_PROT)
5921      PrintS("-");
5922  }
5923  if (TEST_OPT_PROT) PrintLn();
5924#ifdef KDEBUG
5925  sloppy_max = FALSE;
5926#endif
5927}
5928
5929
5930/*2
5931* computes the new strat->kHEdge and the new pNoether,
5932* returns TRUE, if pNoether has changed
5933*/
5934BOOLEAN newHEdge(polyset S, kStrategy strat)
5935{
5936  int i,j;
5937  poly newNoether;
5938
5939#if 0
5940  if (currRing->weight_all_1)
5941    scComputeHC(strat->Shdl,NULL,strat->ak,strat->kHEdge, strat->tailRing);
5942  else
5943    scComputeHCw(strat->Shdl,NULL,strat->ak,strat->kHEdge, strat->tailRing);
5944#else   
5945  scComputeHC(strat->Shdl,NULL,strat->ak,strat->kHEdge, strat->tailRing);
5946#endif 
5947  if (strat->t_kHEdge != NULL) p_LmFree(strat->t_kHEdge, strat->tailRing);
5948  if (strat->tailRing != currRing)
5949    strat->t_kHEdge = k_LmInit_currRing_2_tailRing(strat->kHEdge, strat->tailRing);
5950  /* compare old and new noether*/
5951  newNoether = pLmInit(strat->kHEdge);
5952  j = pFDeg(newNoether,currRing);
5953  for (i=1; i<=pVariables; i++)
5954  {
5955    if (pGetExp(newNoether, i) > 0) pDecrExp(newNoether,i);
5956  }
5957  pSetm(newNoether);
5958  if (j < strat->HCord) /*- statistics -*/
5959  {
5960    if (TEST_OPT_PROT)
5961    {
5962      Print("H(%d)",j);
5963      mflush();
5964    }
5965    strat->HCord=j;
5966    #ifdef KDEBUG
5967    if (TEST_OPT_DEBUG)
5968    {
5969      Print("H(%d):",j);
5970      wrp(strat->kHEdge);
5971      PrintLn();
5972    }
5973    #endif
5974  }
5975  if (pCmp(strat->kNoether,newNoether)!=1)
5976  {
5977    pDelete(&strat->kNoether);
5978    strat->kNoether=newNoether;
5979    if (strat->t_kNoether != NULL) p_LmFree(strat->t_kNoether, strat->tailRing);
5980    if (strat->tailRing != currRing)
5981      strat->t_kNoether = k_LmInit_currRing_2_tailRing(strat->kNoether, strat->tailRing);
5982
5983    return TRUE;
5984  }
5985  pLmFree(newNoether);
5986  return FALSE;
5987}
5988
5989/***************************************************************
5990 *
5991 * Routines related for ring changes during std computations
5992 *
5993 ***************************************************************/
5994BOOLEAN kCheckSpolyCreation(LObject *L, kStrategy strat, poly &m1, poly &m2)
5995{
5996  assume(L->p1 != NULL && L->p2 != NULL);
5997  // shift changes: from 0 to -1
5998  assume(L->i_r1 >= -1 && L->i_r1 <= strat->tl);
5999  assume(L->i_r2 >= -1 && L->i_r2 <= strat->tl);
6000  assume(strat->tailRing != currRing);
6001
6002  if (! k_GetLeadTerms(L->p1, L->p2, currRing, m1, m2, strat->tailRing))
6003    return FALSE;
6004  // shift changes: extra case inserted
6005  if ((L->i_r1 == -1) || (L->i_r2 == -1) )
6006  {
6007    return TRUE;
6008  }
6009  poly p1_max = (strat->R[L->i_r1])->max;
6010  poly p2_max = (strat->R[L->i_r2])->max;
6011
6012  if ((p1_max != NULL && !p_LmExpVectorAddIsOk(m1, p1_max, strat->tailRing)) ||
6013      (p2_max != NULL && !p_LmExpVectorAddIsOk(m2, p2_max, strat->tailRing)))
6014  {
6015    p_LmFree(m1, strat->tailRing);
6016    p_LmFree(m2, strat->tailRing);
6017    m1 = NULL;
6018    m2 = NULL;
6019    return FALSE;
6020  }
6021  return TRUE;
6022}
6023
6024#ifdef HAVE_RINGS
6025/***************************************************************
6026 *
6027 * Checks, if we can compute the gcd poly / strong pair
6028 * gcd-poly = m1 * R[atR] + m2 * S[atS]
6029 *
6030 ***************************************************************/
6031BOOLEAN kCheckStrongCreation(int atR, poly m1, int atS, poly m2, kStrategy strat)
6032{
6033  assume(strat->S_2_R[atS] >= -1 && strat->S_2_R[atS] <= strat->tl);
6034  //assume(strat->tailRing != currRing);
6035
6036  poly p1_max = (strat->R[atR])->max;
6037  poly p2_max = (strat->R[strat->S_2_R[atS]])->max;
6038
6039  if ((p1_max != NULL && !p_LmExpVectorAddIsOk(m1, p1_max, strat->tailRing)) ||
6040      (p2_max != NULL && !p_LmExpVectorAddIsOk(m2, p2_max, strat->tailRing)))
6041  {
6042    return FALSE