source: git/kernel/kutil.cc @ 40cb86

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