source: git/kernel/kutil.cc @ 05eb8c

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