source: git/kernel/tgb.cc @ d367e9

spielwiese
Last change on this file since d367e9 was d367e9, checked in by Michael Brickenstein <bricken@…>, 18 years ago
*bricken: + degBound git-svn-id: file:///usr/local/Singular/svn/trunk@8981 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 75.5 KB
Line 
1//! \file tgb.cc
2//       multiple rings
3//       shorten_tails und dessen Aufrufe pruefen wlength!!!
4/****************************************
5*  Computer Algebra System SINGULAR     *
6****************************************/
7/* $Id: tgb.cc,v 1.72 2006-02-24 10:12:33 bricken Exp $ */
8/*
9* ABSTRACT: slimgb and F4 implementation
10*/
11//#include <vector>
12//using namespace std;
13#include "mod2.h"
14#include "tgb.h"
15#include "tgb_internal.h"
16#include "tgbgauss.h"
17#include "F4.h"
18#include "digitech.h"
19#include "gring.h"
20
21#include "longrat.h"
22#define SR_HDL(A) ((long)(A))
23static const int bundle_size=100;
24static const int delay_factor=3;
25int QlogSize(number n);
26#if 1
27static omBin lm_bin=NULL;
28
29static void simplify_poly(poly p, ring r) {
30     assume(r==currRing);
31     if (!rField_is_Zp(r)){ 
32        pCleardenom(p);
33        pContent(p); //is a duplicate call, but belongs here
34       
35      }
36      else                     
37        pNorm(p);
38}
39//static const BOOLEAN up_to_radical=TRUE;
40
41static int slim_nsize(number n, ring r) {
42    if (rField_is_Zp(r)){
43        return 1;
44    }
45 if (rField_is_Q(r)){
46      return QlogSize(n);
47
48  }
49    else{
50      return n_Size(n,r);
51     
52    }
53}
54static BOOLEAN monomial_root(poly m, ring r){
55    BOOLEAN changed=FALSE;
56    int i;
57    for(i=1;i<=rVar(r);i++){
58        int e=p_GetExp(m,i,r);
59        if (e>1){
60            p_SetExp(m,i,1,r);
61            changed=TRUE;
62        }
63    }
64    if (changed) {
65        p_Setm(m,r);
66    }
67    return changed;
68}
69static BOOLEAN polynomial_root(poly h, ring r){
70  poly got=gcd_of_terms(h,r);
71  BOOLEAN changed=FALSE;
72  if((got!=NULL) &&(TEST_V_UPTORADICAL)) {
73    poly copy=p_Copy(got,r);
74    //p_wrp(got,c->r);
75    changed=monomial_root(got,r);
76    if (changed)
77    {
78         poly div_by=pDivide(copy, got);
79         poly iter=h;
80         while(iter){
81            pExpVectorSub(iter,div_by);
82            pIter(iter);
83         }
84         p_Delete(&div_by, r);
85         if (TEST_OPT_PROT)
86             PrintS("U");
87    }
88    p_Delete(&copy,r);
89  }
90  p_Delete(&got,r);
91  return changed;
92}
93static inline poly p_Init_Special(const ring r)
94{
95  return p_Init(r,lm_bin);
96}
97static inline poly pOne_Special(const ring r=currRing)
98{
99  poly rc = p_Init_Special(r);
100  pSetCoeff0(rc,r->cf->nInit(1));
101  return rc;
102}
103// zum Initialiseren: in t_rep_gb plazieren:
104
105#endif
106#define LEN_VAR3
107#define degbound(p) assume(pTotaldegree(p)<10)
108//#define inDebug(p) assume((debug_Ideal==NULL)||(kNF(debug_Ideal,NULL,p,0,0)==0))
109
110//die meisten Varianten stossen sich an coef_buckets
111
112
113
114#ifdef LEN_VAR1
115// erste Variante: Laenge: Anzahl der Monome
116inline int pSLength(poly p, int l) {
117  return l; }
118inline int kSBucketLength(kBucket* bucket, poly lm) {return bucket_guess(bucket);}
119#endif
120
121#ifdef LEN_VAR2
122// 2. Variante: Laenge: Platz fuer die Koeff.
123int pSLength(poly p,int l)
124{
125  int s=0;
126  while (p!=NULL) { s+=nSize(pGetCoeff(p));pIter(p); }
127  return s;
128}
129int kSBucketLength(kBucket* b, poly lm)
130{
131  int s=0;
132  int i;
133  for (i=MAX_BUCKET;i>=0;i--)
134  {
135    s+=pSLength(b->buckets[i],0);
136  }
137  return s;
138}
139#endif
140
141
142// 3.Variante: Laenge: Platz fuer Leitk * Monomanzahl
143
144
145 
146 
147int QlogSizeNew(number n){
148   
149    if (SR_HDL(n) & SR_INT){
150       long i=SR_TO_INT(n);
151       if (i==0) return 0;
152       
153       unsigned long v;
154       v=(i>=0)?i:-i;
155       int r = 0;
156
157       while (v >>= 1)
158       {
159        r++;
160       }
161       return r+1;
162    }
163    //assume denominator is 0
164    return mpz_sizeinbase(&n->z,2);
165}
166
167inline int QlogSize(number bigint){
168    return QlogSizeNew(bigint);
169  }
170#ifdef LEN_VAR3
171inline int pSLength(poly p,int l)
172{
173  int c;
174  number coef=pGetCoeff(p);
175  if (rField_is_Q(currRing)){
176    c=QlogSize(coef);
177  }
178  else
179    c=nSize(coef);
180 
181  return c*l /*pLength(p)*/;
182}
183//! TODO CoefBuckets berücksichtigen
184int kSBucketLength(kBucket* b, poly lm=NULL)
185{
186  int s=0;
187  int c;
188  number coef;
189  if(lm==NULL)
190    coef=pGetCoeff(kBucketGetLm(b));
191    //c=nSize(pGetCoeff(kBucketGetLm(b)));
192  else
193    coef=pGetCoeff(lm);
194    //c=nSize(pGetCoeff(lm));
195  if (rField_is_Q(currRing)){
196    c=QlogSize(coef);
197  }
198  else
199    c=nSize(coef);
200 
201  int i;
202  for (i=b->buckets_used;i>=0;i--)
203  {
204    assume((b->buckets_length[i]==0)||(b->buckets[i]!=NULL));
205    s+=b->buckets_length[i] /*pLength(b->buckets[i])*/;
206  }
207  #ifdef HAVE_COEF_BUCKETS
208  assume(b->buckets[0]==kBucketGetLm(b));
209  if (b->coef[0]!=NULL){
210   
211    if (rField_is_Q(currRing)){
212      int modifier=QlogSize(pGetCoeff(b->coef[0]));
213      c+=modifier;
214  }
215    else{
216      int modifier=nSize(pGetCoeff(b->coef[0]));
217      c*=modifier;}
218    }
219  #endif
220  return s*c;
221}
222#endif
223#ifdef LEN_VAR5
224inline wlen_type pSLength(poly p,int l)
225{
226  int c;
227  number coef=pGetCoeff(p);
228  if (rField_is_Q(currRing)){
229    c=QlogSize(coef);
230  }
231  else
232    c=nSize(coef);
233  wlen_type erg=l;
234  erg*=c;
235  erg*=c;
236  //PrintS("lenvar 5");
237  assume(erg>=0);
238  return erg; /*pLength(p)*/;
239}
240//! TODO CoefBuckets berücksichtigen
241wlen_type kSBucketLength(kBucket* b, poly lm=NULL)
242{
243  wlen_type s=0;
244  int c;
245  number coef;
246  if(lm==NULL)
247    coef=pGetCoeff(kBucketGetLm(b));
248    //c=nSize(pGetCoeff(kBucketGetLm(b)));
249  else
250    coef=pGetCoeff(lm);
251    //c=nSize(pGetCoeff(lm));
252  if (rField_is_Q(currRing)){
253    c=QlogSize(coef);
254  }
255  else
256    c=nSize(coef);
257 
258  int i;
259  for (i=b->buckets_used;i>=0;i--)
260  {
261    assume((b->buckets_length[i]==0)||(b->buckets[i]!=NULL));
262    s+=b->buckets_length[i] /*pLength(b->buckets[i])*/;
263  }
264  #ifdef HAVE_COEF_BUCKETS
265  assume(b->buckets[0]==kBucketGetLm(b));
266  if (b->coef[0]!=NULL){
267   
268    if (rField_is_Q(currRing)){
269      int modifier=QlogSize(pGetCoeff(b->coef[0]));
270      c+=modifier;
271  }
272    else{
273      int modifier=nSize(pGetCoeff(b->coef[0]));
274      c*=modifier;}
275    }
276  #endif
277  wlen_type erg=s;
278  erg*=c;
279  erg*=c;
280  return erg;
281}
282#endif
283
284#ifdef LEN_VAR4
285// 4.Variante: Laenge: Platz fuer Leitk * (1+Platz fuer andere Koeff.)
286int pSLength(poly p, int l)
287{
288  int s=1;
289  int c=nSize(pGetCoeff(p));
290  pIter(p);
291  while (p!=NULL) { s+=nSize(pGetCoeff(p));pIter(p); }
292  return s*c;
293}
294int kSBucketLength(kBucket* b)
295{
296  int s=1;
297  int c=nSize(pGetCoeff(kBucketGetLm(b)));
298  int i;
299  for (i=MAX_BUCKET;i>0;i--)
300  {
301    if(b->buckets[i]==NULL) continue;
302    s+=pSLength(b->buckets[i],0);
303  }
304  return s*c;
305}
306#endif
307static int do_pELength(poly p, slimgb_alg* c, int dlm=-1){
308  if(p==NULL) return 0;
309  int s=0;
310  poly pi=p;
311  if(dlm<0){
312    dlm=pTotaldegree(p,c->r);
313    s=1;
314    pi=p->next;
315  }
316 
317  while(pi){
318    int d=pTotaldegree(pi,c->r);
319    if(d>dlm)
320      s+=1+d-dlm;
321    else
322      ++s;
323    pi=pi->next;
324  }
325  return s;
326}
327int kEBucketLength(kBucket* b, poly lm,slimgb_alg* ca)
328{
329  int s=0;
330  if(lm==NULL){
331    lm=kBucketGetLm(b);
332  }
333  if(lm==NULL) return 0;
334  int d=pTotaldegree(lm,ca->r);
335  int i;
336  for (i=b->buckets_used;i>=0;i--)
337  {
338    if(b->buckets[i]==NULL) continue;
339    s+=do_pELength(b->buckets[i],ca,d);
340  }
341  return s;
342}
343
344static inline int pELength(poly p, slimgb_alg* c,int l){
345  if (p==NULL) return 0;
346  return do_pELength(p,c);
347}
348
349
350
351
352static inline wlen_type pQuality(poly p, slimgb_alg* c, int l=-1){
353 
354  if(l<0)
355    l=pLength(p);
356  if(c->is_char0) {
357    if((pLexOrder) &&(!c->is_homog)){
358      int cs;
359      number coef=pGetCoeff(p);
360      if (rField_is_Q(currRing)){
361         cs=QlogSize(coef);
362      }
363      else
364  cs=nSize(coef);
365     wlen_type erg=cs;
366     //erg*=cs;//for quadratic
367     erg*=pELength(p,c,l);
368    //FIXME: not quadratic coeff size
369      //return cs*pELength(p,c,l);
370      return erg;
371    }
372    //Print("I am here");
373    wlen_type r=pSLength(p,l);
374    assume(r>=0);
375    return r;
376  }
377  if((pLexOrder) &&(!c->is_homog)) return pELength(p,c,l);
378  return l;
379}
380
381static inline int pTotaldegree_full(poly p){
382  int r=0;
383  while(p){
384    int d=pTotaldegree(p);
385    r=si_max(r,d);
386    pIter(p);
387  }
388  return r;
389}
390
391wlen_type red_object::guess_quality(slimgb_alg* c){
392    //works at the moment only for lenvar 1, because in different
393    //case, you have to look on coefs
394    wlen_type s=0;
395    if (c->is_char0){
396      //s=kSBucketLength(bucket,this->p);
397      if((pLexOrder) &&(!c->is_homog)){
398    int cs;
399    number coef;
400   
401    coef=pGetCoeff(kBucketGetLm(bucket));
402    //c=nSize(pGetCoeff(kBucketGetLm(b)));
403   
404    //c=nSize(pGetCoeff(lm));
405    if (rField_is_Q(currRing)){
406      cs=QlogSize(coef);
407    }
408    else
409      cs=nSize(coef);
410    #ifdef HAVE_COEF_BUCKETS
411    if (bucket->coef[0]!=NULL){
412      if (rField_is_Q(currRing)){
413        int modifier=QlogSize(pGetCoeff(bucket->coef[0]));
414        cs+=modifier;
415      }
416      else{
417        int modifier=nSize(pGetCoeff(bucket->coef[0]));
418        cs*=modifier;}
419    }
420    #endif
421    //FIXME:not quadratic
422    wlen_type erg=kEBucketLength(this->bucket,this->p,c);
423    //erg*=cs;//for quadratic
424    erg*=cs;
425    //return cs*kEBucketLength(this->bucket,this->p,c);
426    return erg;
427      }
428      s=kSBucketLength(bucket,NULL);
429    }
430    else 
431    {
432      if((pLexOrder) &&(!c->is_homog))
433  //if (false)
434  s=kEBucketLength(this->bucket,this->p,c);
435      else s=bucket_guess(bucket);
436    }
437 
438    return s;
439}
440
441
442
443static void finalize_reduction_step(reduction_step* r){
444  delete r;
445}
446static int LObject_better_gen(const void* ap, const void* bp)
447{
448  LObject* a=*(LObject**)ap;
449  LObject* b=*(LObject**)bp;
450  return(pLmCmp(a->p,b->p));
451}
452static int red_object_better_gen(const void* ap, const void* bp)
453{
454
455
456  return(pLmCmp(((red_object*) ap)->p,((red_object*) bp)->p));
457}
458
459
460static int pLmCmp_func_inverted(const void* ap1, const void* ap2){
461    poly p1,p2;
462  p1=*((poly*) ap1);
463  p2=*((poly*)ap2);
464
465  return -pLmCmp(p1,p2);
466}
467
468int tgb_pair_better_gen2(const void* ap,const void* bp){
469  return(-tgb_pair_better_gen(ap,bp));
470}
471int kFindDivisibleByInS_easy(kStrategy strat,const red_object & obj){
472  int i;
473  long not_sev=~obj.sev;
474  poly p=obj.p;
475  for(i=0;i<=strat->sl;i++){
476    if (pLmShortDivisibleBy(strat->S[i],strat->sevS[i],p,not_sev))
477      return i;
478  }
479  return -1;
480}
481int kFindDivisibleByInS_easy(kStrategy strat,poly p, long sev){
482  int i;
483  long not_sev=~sev;
484  for(i=0;i<=strat->sl;i++){
485    if (pLmShortDivisibleBy(strat->S[i],strat->sevS[i],p,not_sev))
486      return i;
487  }
488  return -1;
489}
490static int posInPairs (sorted_pair_node**  p, int pn, sorted_pair_node* qe,slimgb_alg* c,int an=0)
491{
492  if(pn==0) return 0;
493
494  int length=pn-1;
495  int i;
496  //int an = 0;
497  int en= length;
498
499  if (pair_better(qe,p[en],c))
500    return length+1;
501
502  while(1)
503    {
504      //if (an >= en-1)
505      if(en-1<=an)
506      {
507        if (pair_better(p[an],qe,c)) return an;
508        return en;
509      }
510      i=(an+en) / 2;
511        if (pair_better(p[i],qe,c))
512          en=i;
513      else an=i;
514    }
515}
516
517static BOOLEAN  ascending(int* i,int top){
518  if(top<1) return TRUE;
519  if(i[top]<i[top-1]) return FALSE;
520  return ascending(i,top-1);
521}
522
523sorted_pair_node**  spn_merge(sorted_pair_node** p, int pn,sorted_pair_node **q, int qn,slimgb_alg* c){
524  int i;
525  int* a= (int*) omalloc(qn*sizeof(int));
526//   int mc;
527//   PrintS("Debug\n");
528//   for(mc=0;mc<qn;mc++)
529// {
530
531//     wrp(q[mc]->lcm_of_lm);
532//     PrintS("\n");
533// }
534//    PrintS("Debug they are in\n");
535//   for(mc=0;mc<pn;mc++)
536// {
537
538//     wrp(p[mc]->lcm_of_lm);
539//     PrintS("\n");
540// }
541  int lastpos=0;
542  for(i=0;i<qn;i++){
543    lastpos=posInPairs(p,pn,q[i],c, si_max(lastpos-1,0));
544    //   cout<<lastpos<<"\n";
545    a[i]=lastpos;
546
547  }
548  if((pn+qn)>c->max_pairs){
549    p=(sorted_pair_node**) omrealloc(p,2*(pn+qn)*sizeof(sorted_pair_node*));
550    c->max_pairs=2*(pn+qn);
551  }
552  for(i=qn-1;i>=0;i--){
553    size_t size;
554    if(qn-1>i)
555      size=(a[i+1]-a[i])*sizeof(sorted_pair_node*);
556    else
557      size=(pn-a[i])*sizeof(sorted_pair_node*); //as indices begin with 0
558    memmove (p+a[i]+(1+i), p+a[i], size);
559    p[a[i]+i]=q[i];
560  }
561  omfree(a);
562  return p;
563}
564
565
566static BOOLEAN trivial_syzygie(int pos1,int pos2,poly bound,slimgb_alg* c){
567
568
569  poly p1=c->S->m[pos1];
570  poly p2=c->S->m[pos2];
571   
572
573  if (pGetComp(p1) > 0 || pGetComp(p2) > 0)
574    return FALSE;
575  int i = 1;
576  poly m=NULL;
577  poly gcd1=c->gcd_of_terms[pos1];
578  poly gcd2=c->gcd_of_terms[pos2];
579 
580  if((gcd1!=NULL) && (gcd2!=NULL)) 
581    {
582      gcd1->next=gcd2; //may ordered incorrect
583      m=gcd_of_terms(gcd1,c->r);
584      gcd1->next=NULL;
585     
586    } 
587
588  if (m==NULL) 
589  {
590     loop
591      {
592  if (pGetExp(p1, i)+ pGetExp(p2, i) > pGetExp(bound,i))   return FALSE;
593  if (i == pVariables){
594    //PrintS("trivial");
595    return TRUE;
596  }
597  i++;
598      }
599  }
600  else 
601  {
602    loop
603      {
604  if (pGetExp(p1, i)-pGetExp(m,i) + pGetExp(p2, i) > pGetExp(bound,i))   return FALSE;
605  if (i == pVariables){
606    pDelete(&m);
607    //PrintS("trivial");
608    return TRUE;
609  }
610  i++;
611      }
612  }
613
614 
615
616 
617}
618
619//! returns position sets w as weight
620int find_best(red_object* r,int l, int u, int &w, slimgb_alg* c){
621  int best=l;
622  int i;
623  w=r[l].guess_quality(c);
624  for(i=l+1;i<=u;i++){
625    int w2=r[i].guess_quality(c);
626    if(w2<w){
627      w=w2;
628      best=i;
629    }
630   
631  }
632 return best;
633}
634
635
636void red_object::canonicalize(){
637  kBucketCanonicalize(bucket);
638 
639 
640}
641BOOLEAN good_has_t_rep(int i, int j,slimgb_alg* c){
642  assume(i>=0);
643    assume(j>=0);
644  if (has_t_rep(i,j,c)) return TRUE;
645  //poly lm=pOne();
646  assume (c->tmp_lm!=NULL);
647  poly lm=c->tmp_lm;
648
649  pLcm(c->S->m[i], c->S->m[j], lm);
650  pSetm(lm);
651  assume(lm!=NULL);
652  //int deciding_deg= pTotaldegree(lm);
653  int* i_con =make_connections(i,j,lm,c);
654  //p_Delete(&lm,c->r);
655
656
657  for (int n=0;((n<c->n) && (i_con[n]>=0));n++){
658    if (i_con[n]==j){
659      now_t_rep(i,j,c);
660      omfree(i_con);
661
662      return TRUE;
663    }
664  }
665  omfree(i_con);
666
667  return FALSE;
668}
669BOOLEAN lenS_correct(kStrategy strat){
670  int i;
671  for(i=0;i<=strat->sl;i++){
672    if (strat->lenS[i]!=pLength(strat->S[i]))
673      return FALSE;
674  }
675  return TRUE;
676}
677
678
679static void cleanS(kStrategy strat, slimgb_alg* c){
680  int i=0;
681  LObject P;
682  while(i<=strat->sl){
683    P.p=strat->S[i];
684    P.sev=strat->sevS[i];
685    if(kFindDivisibleByInS(strat->S,strat->sevS,strat->sl,&P)!=i)
686    {
687      deleteInS(i,strat);
688      //remember destroying poly
689      BOOLEAN found=FALSE;
690      int j;
691      for(j=0;j<c->n;j++)
692  if(c->S->m[j]==P.p)
693  {
694    found=TRUE;
695    break;
696  }
697      if (!found)
698  pDelete(&P.p);
699      //remember additional reductors
700    }
701    else i++;
702  }
703}
704static int bucket_guess(kBucket* bucket){
705  int sum=0;
706  int i;
707  for (i=bucket->buckets_used;i>=0;i--){
708    if(bucket->buckets[i])
709       sum+=bucket->buckets_length[i];
710  }
711  return sum;
712}
713
714
715
716
717
718
719static int add_to_reductors(slimgb_alg* c, poly h, int len, BOOLEAN simplified){
720  //inDebug(h);
721  assume(lenS_correct(c->strat));
722  assume(len==pLength(h));
723  int i;
724//   if (c->is_char0)
725//        i=simple_posInS(c->strat,h,pSLength(h,len),c->is_char0);
726//   else
727//     i=simple_posInS(c->strat,h,len,c->is_char0);
728
729  LObject P; memset(&P,0,sizeof(P));
730  P.tailRing=c->r;
731  P.p=h; /*p_Copy(h,c->r);*/
732  P.FDeg=pFDeg(P.p,c->r);
733  if (!(simplified)){
734      if (!rField_is_Zp(c->r)){ 
735        pCleardenom(P.p);
736        pContent(P.p); //is a duplicate call, but belongs here
737       
738      }
739      else                     
740        pNorm(P.p);
741    pNormalize(P.p);
742  }
743  wlen_type pq=pQuality(h,c,len);
744  i=simple_posInS(c->strat,h,len,pq);
745  c->strat->enterS(P,i,c->strat,-1);
746 
747 
748
749  c->strat->lenS[i]=len;
750  assume(pLength(c->strat->S[i])==c->strat->lenS[i]);
751  if(c->strat->lenSw!=NULL)
752    c->strat->lenSw[i]=pq;
753 
754  return i;
755 
756}
757static void length_one_crit(slimgb_alg* c, int pos, int len)
758{
759  if (c->nc)
760    return;
761  if (len==1)
762  {
763    int i;
764    for ( i=0;i<pos;i++)
765    {
766      if (c->lengths[i]==1)
767        c->states[pos][i]=HASTREP;
768    }
769    for ( i=pos+1;i<c->n;i++){
770      if (c->lengths[i]==1)
771        c->states[i][pos]=HASTREP;
772    }
773    if (!c->nc)
774      shorten_tails(c,c->S->m[pos]);
775  }
776}
777
778
779static void move_forward_in_S(int old_pos, int new_pos,kStrategy strat)
780{
781  assume(old_pos>=new_pos);
782  poly p=strat->S[old_pos];
783  int ecart=strat->ecartS[old_pos];
784  long sev=strat->sevS[old_pos];
785  int s_2_r=strat->S_2_R[old_pos];
786  int length=strat->lenS[old_pos];
787  assume(length==pLength(strat->S[old_pos]));
788  wlen_type length_w;
789  if(strat->lenSw!=NULL)
790    length_w=strat->lenSw[old_pos];
791  int i;
792  for (i=old_pos; i>new_pos; i--)
793  {
794    strat->S[i] = strat->S[i-1];
795    strat->ecartS[i] = strat->ecartS[i-1];
796    strat->sevS[i] = strat->sevS[i-1];
797    strat->S_2_R[i] = strat->S_2_R[i-1];
798  }
799  if (strat->lenS!=NULL)
800    for (i=old_pos; i>new_pos; i--)
801      strat->lenS[i] = strat->lenS[i-1];
802  if (strat->lenSw!=NULL)
803    for (i=old_pos; i>new_pos; i--)
804      strat->lenSw[i] = strat->lenSw[i-1];
805
806  strat->S[new_pos]=p;
807  strat->ecartS[new_pos]=ecart;
808  strat->sevS[new_pos]=sev;
809  strat->S_2_R[new_pos]=s_2_r;
810  strat->lenS[new_pos]=length;
811  if(strat->lenSw!=NULL)
812    strat->lenSw[new_pos]=length_w;
813  //assume(lenS_correct(strat));
814}
815
816static int* make_connections(int from, int to, poly bound, slimgb_alg* c)
817{
818  ideal I=c->S;
819  int* cans=(int*) omalloc(c->n*sizeof(int));
820  int* connected=(int*) omalloc(c->n*sizeof(int));
821  cans[0]=to;
822  int cans_length=1;
823  connected[0]=from;
824  int last_cans_pos=-1;
825  int connected_length=1;
826  long neg_bounds_short= ~p_GetShortExpVector(bound,c->r);
827
828  int not_yet_found=cans_length;
829  int con_checked=0;
830  int pos;
831 
832  while(TRUE){
833    if ((con_checked<connected_length)&& (not_yet_found>0)){
834      pos=connected[con_checked];
835      for(int i=0;i<cans_length;i++){
836        if (cans[i]<0) continue;
837        //FIXME: triv. syz. does not hold on noncommutative, check it for modules
838        if ((has_t_rep(pos,cans[i],c)) ||((!rIsPluralRing(c->r))&&(trivial_syzygie(pos,cans[i],bound,c))))
839{
840
841          connected[connected_length]=cans[i];
842          connected_length++;
843          cans[i]=-1;
844          --not_yet_found;
845
846          if (connected[connected_length-1]==to){
847            if (connected_length<c->n){
848              connected[connected_length]=-1;
849            }
850            omfree(cans);
851            return connected;
852          }
853        }
854      }
855      con_checked++;
856    }
857    else
858    {
859      for(last_cans_pos++;last_cans_pos<=c->n;last_cans_pos++){
860        if (last_cans_pos==c->n){
861          if (connected_length<c->n){
862            connected[connected_length]=-1;
863          }
864          omfree(cans);
865          return connected;
866        }
867        if ((last_cans_pos==from)||(last_cans_pos==to))
868          continue;
869        if(p_LmShortDivisibleBy(I->m[last_cans_pos],c->short_Exps[last_cans_pos],bound,neg_bounds_short,c->r)){
870          cans[cans_length]=last_cans_pos;
871          cans_length++;
872          break;
873        }
874      }
875      not_yet_found++;
876      for (int i=0;i<con_checked;i++){
877        if (has_t_rep(connected[i],last_cans_pos,c)){
878
879          connected[connected_length]=last_cans_pos;
880          connected_length++;
881          cans[cans_length-1]=-1;
882
883          --not_yet_found;
884          if (connected[connected_length-1]==to){
885            if (connected_length<c->n){
886              connected[connected_length]=-1;
887            }
888
889            omfree(cans);
890            return connected;
891          }
892          break;
893        }
894      }
895    }
896  }
897  if (connected_length<c->n){
898    connected[connected_length]=-1;
899  }
900
901  omfree(cans);
902  return connected;
903}
904#ifdef HEAD_BIN
905static inline poly p_MoveHead(poly p, omBin b)
906{
907  poly np;
908  omTypeAllocBin(poly, np, b);
909  memmove(np, p, b->sizeW*sizeof(long));
910  omFreeBinAddr(p);
911  return np;
912}
913#endif
914
915
916
917static void add_later(poly p, char* prot, slimgb_alg* c){
918    int i=0;
919    //check, if it is already in the queue
920   
921   
922    while(c->add_later->m[i]!=NULL){
923        if (p_LmEqual(c->add_later->m[i],p,c->r))
924            return;
925        i++;
926    }
927    if (TEST_OPT_PROT)
928        PrintS(prot);
929    c->add_later->m[i]=p;
930}
931static int simple_posInS (kStrategy strat, poly p,int len, wlen_type wlen)
932{
933
934
935  if(strat->sl==-1) return 0;
936  if (strat->lenSw) return pos_helper(strat,p,(wlen_type) wlen,(wlen_set) strat->lenSw,strat->S);
937  return pos_helper(strat,p,len,strat->lenS,strat->S);
938
939}
940
941/*2
942 *if the leading term of p
943 *divides the leading term of some S[i] it will be canceled
944 */
945static inline void clearS (poly p, unsigned long p_sev,int l, int* at, int* k,
946                           kStrategy strat)
947{
948  assume(p_sev == pGetShortExpVector(p));
949  if (!pLmShortDivisibleBy(p,p_sev, strat->S[*at], ~ strat->sevS[*at])) return;
950  if (l>=strat->lenS[*at]) return;
951  if (TEST_OPT_PROT)
952    PrintS("!");
953  mflush();
954  //pDelete(&strat->S[*at]);
955  deleteInS((*at),strat);
956  (*at)--;
957  (*k)--;
958//  assume(lenS_correct(strat));
959}
960
961
962
963static int iq_crit(const void* ap,const void* bp){
964
965  sorted_pair_node* a=*((sorted_pair_node**)ap);
966  sorted_pair_node* b=*((sorted_pair_node**)bp);
967  assume(a->i>a->j);
968  assume(b->i>b->j);
969 
970 
971  if (a->deg<b->deg) return -1;
972  if (a->deg>b->deg) return 1;
973  int comp=pLmCmp(a->lcm_of_lm, b->lcm_of_lm);
974  if(comp!=0)
975    return comp;
976  if (a->expected_length<b->expected_length) return -1;
977  if (a->expected_length>b->expected_length) return 1;
978  if (a->j>b->j) return 1;
979  if (a->j<b->j) return -1;
980  return 0;
981}
982static wlen_type coeff_mult_size_estimate(int s1, int s2, ring r){
983    if (rField_is_Q(r)) return s1+s2;
984    else return s1*s2;
985}
986static wlen_type pair_weighted_length(int i, int j, slimgb_alg* c){
987    if ((c->is_char0) && (pLexOrder) &&(!c->is_homog))  {
988        int c1=slim_nsize(p_GetCoeff(c->S->m[i],c->r),c->r);
989        int c2=slim_nsize(p_GetCoeff(c->S->m[j],c->r),c->r);
990        wlen_type el1=c->weighted_lengths[i]/c1;
991        assume(el1!=0);
992        assume(c->weighted_lengths[i] %c1==0);
993        wlen_type el2=c->weighted_lengths[j]/c2;
994        assume(el2!=0);
995        assume(c->weighted_lengths[j] %c2==0);
996        //should be * for function fields
997        //return (c1+c2) * (el1+el2-2);
998        wlen_type res=coeff_mult_size_estimate(c1,c2,c->r);
999        res*=el1+el2-2;
1000        return res;
1001       
1002    }
1003    if (c->is_char0) {
1004        //int cs=slim_nsize(p_GetCoeff(c->S->m[i],c->r),c->r)+
1005        //    slim_nsize(p_GetCoeff(c->S->m[j],c->r),c->r);
1006        wlen_type cs=
1007            coeff_mult_size_estimate(
1008                slim_nsize(p_GetCoeff(c->S->m[i],c->r),c->r),
1009                slim_nsize(p_GetCoeff(c->S->m[j],c->r),c->r),c->r);
1010        return (wlen_type)(c->lengths[i]+c->lengths[j]-2)*
1011            (wlen_type)cs;
1012    }
1013    if ((pLexOrder) &&(!c->is_homog)) {
1014
1015        return (c->weighted_lengths[i]+c->weighted_lengths[j]-2);
1016    }
1017    return c->lengths[i]+c->lengths[j]-2;
1018   
1019}
1020sorted_pair_node** add_to_basis_ideal_quotient(poly h, int i_pos, int j_pos,slimgb_alg* c, int* ip, BOOLEAN new_pairs)
1021{
1022
1023  assume(h!=NULL);
1024  poly got=gcd_of_terms(h,c->r);
1025  if((got!=NULL) &&(TEST_V_UPTORADICAL)) {
1026    poly copy=p_Copy(got,c->r);
1027    //p_wrp(got,c->r);
1028    BOOLEAN changed=monomial_root(got,c->r);
1029    if (changed)
1030    {
1031         poly div_by=pDivide(copy, got);
1032         poly iter=h;
1033         while(iter){
1034            pExpVectorSub(iter,div_by);
1035            pIter(iter);
1036         }
1037         p_Delete(&div_by, c->r);
1038         PrintS("U");
1039    }
1040    p_Delete(&copy,c->r);
1041  }
1042
1043#define ENLARGE(pointer, type) pointer=(type*) omrealloc(pointer, c->array_lengths*sizeof(type))
1044//  BOOLEAN corr=lenS_correct(c->strat);
1045  BOOLEAN R_found=FALSE;
1046  void* hp;
1047
1048  ++(c->n);
1049  ++(c->S->ncols);
1050  int i,j;
1051  i=c->n-1;
1052  sorted_pair_node** nodes=(sorted_pair_node**) omalloc(sizeof(sorted_pair_node*)*i);
1053  int spc=0;
1054  if(c->n>c->array_lengths){
1055    c->array_lengths=c->array_lengths*2;
1056    assume(c->array_lengths>=c->n);
1057    ENLARGE(c->T_deg, int);
1058    ENLARGE(c->tmp_pair_lm,poly);
1059    ENLARGE(c->tmp_spn,sorted_pair_node*);
1060
1061    ENLARGE(c->short_Exps,long);
1062    ENLARGE(c->lengths,int);
1063    #ifndef HAVE_BOOST
1064    ENLARGE(c->states, char*);
1065    #endif
1066    ENLARGE(c->gcd_of_terms,poly);
1067    //if (c->weighted_lengths!=NULL) {
1068    ENLARGE(c->weighted_lengths,wlen_type);
1069    //}
1070    //ENLARGE(c->S->m,poly);
1071   
1072  }
1073  pEnlargeSet(&c->S->m,c->n-1,1);
1074  if (c->T_deg_full)
1075    ENLARGE(c->T_deg_full,int);
1076  c->T_deg[i]=pTotaldegree(h);
1077  if(c->T_deg_full){
1078    c->T_deg_full[i]=pTotaldegree_full(h);
1079  }
1080 
1081
1082  c->tmp_pair_lm[i]=pOne_Special(c->r);
1083
1084
1085  c->tmp_spn[i]=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1086
1087
1088  c->lengths[i]=pLength(h);
1089 
1090  //necessary for correct weighted length
1091 
1092   if (!rField_is_Zp(c->r)){ 
1093    pCleardenom(h);
1094    pContent(h); //is a duplicate call, but belongs here
1095   
1096  }
1097  else                     
1098    pNorm(h);
1099  pNormalize(h);
1100 
1101  c->weighted_lengths[i]=pQuality(h, c, c->lengths[i]);
1102  c->gcd_of_terms[i]=got;
1103  #ifdef HAVE_BOOST
1104    c->states.push_back(dynamic_bitset<>(i));
1105 
1106  #else
1107  if (i>0)
1108    c->states[i]=(char*)  omalloc(i*sizeof(char));
1109  else
1110    c->states[i]=NULL;
1111  #endif
1112 
1113  c->S->m[i]=h;
1114  c->short_Exps[i]=p_GetShortExpVector(h,c->r);
1115 
1116#undef ENLARGE
1117 
1118  for (j=0;j<i;j++){
1119   
1120
1121    #ifndef HAVE_BOOST
1122    c->states[i][j]=UNCALCULATED;
1123    #endif
1124    if (!new_pairs) continue;
1125    assume(p_LmDivisibleBy(c->S->m[i],c->S->m[j],c->r)==
1126     p_LmShortDivisibleBy(c->S->m[i],c->short_Exps[i],c->S->m[j],~(c->short_Exps[j]),c->r));
1127
1128   
1129    if (_p_GetComp(c->S->m[i],c->r)!=_p_GetComp(c->S->m[j],c->r)){
1130      //c->states[i][j]=UNCALCULATED;
1131      //WARNUNG: be careful
1132      continue;
1133    } else
1134    if ((!c->nc) && (c->lengths[i]==1) && (c->lengths[j]==1)){
1135      c->states[i][j]=HASTREP;
1136     
1137      }
1138    else if ((!(c->nc)) &&  (pHasNotCF(c->S->m[i],c->S->m[j])))
1139    {
1140      c->easy_product_crit++;
1141      c->states[i][j]=HASTREP;
1142      //continue;
1143    }
1144    else if(extended_product_criterion(c->S->m[i],c->gcd_of_terms[i],c->S->m[j],c->gcd_of_terms[j],c))
1145    {
1146      c->states[i][j]=HASTREP;
1147      c->extended_product_crit++;
1148     
1149      //PrintS("E");
1150    }
1151      //  if (c->states[i][j]==UNCALCULATED){
1152   
1153    if ((TEST_V_FINDMONOM) &&(!c->nc)) {
1154        //PrintS("COMMU");
1155       //  if (c->lengths[i]==c->lengths[j]){
1156//             poly short_s=ksCreateShortSpoly(c->S->m[i],c->S->m[j],c->r);
1157//             if (short_s==NULL){
1158//                 c->states[i][j]=HASTREP;
1159//             } else
1160//             {
1161//                 p_Delete(&short_s, currRing);
1162//             }
1163//         }
1164        if ((c->lengths[i]+c->lengths[j]==3) &&( (pHasNotCF(c->S->m[i],c->S->m[j])))){
1165            poly                 short_s=ksCreateShortSpoly(c->S->m[i],c->S->m[j],c->r);
1166            if (short_s==NULL){
1167                c->states[i][j]=HASTREP;
1168            } else
1169            {
1170                assume(pLength(short_s)==1);
1171                if (TEST_V_UPTORADICAL)
1172                   monomial_root(short_s,c->r);
1173                int iS=
1174                   kFindDivisibleByInS_easy(c->strat,short_s, p_GetShortExpVector(short_s,c->r));
1175                if (iS<0){
1176                    //PrintS("N");
1177                    c->states[i][j]=HASTREP;
1178                    add_later(short_s,"N",c);
1179                }
1180                else {
1181                    if (c->strat->lenS[iS]>1){
1182                        //PrintS("O");
1183                        c->states[i][j]=HASTREP;
1184                        add_later(short_s,"O",c);
1185                    }
1186                    else
1187                     p_Delete(&short_s, currRing);
1188                }
1189               
1190               
1191            }
1192        }
1193    }
1194      //    if (short_s)
1195      //    {
1196    assume(spc<=j);
1197    sorted_pair_node* s=c->tmp_spn[spc];//(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1198    s->i=si_max(i,j);
1199    s->j=si_min(i,j);
1200    assume(s->j==j);
1201    s->expected_length=pair_weighted_length(i,j,c);//c->lengths[i]+c->lengths[j]-2;
1202     
1203    poly lm=c->tmp_pair_lm[spc];//=pOne_Special();
1204     
1205    pLcm(c->S->m[i], c->S->m[j], lm);
1206    pSetm(lm);
1207    s->deg=pTotaldegree(lm);
1208
1209    if(c->T_deg_full)//Sugar
1210    {
1211      int t_i=c->T_deg_full[s->i]-c->T_deg[s->i];
1212      int t_j=c->T_deg_full[s->j]-c->T_deg[s->j];
1213      s->deg+=si_max(t_i,t_j);
1214      //Print("\n max: %d\n",max(t_i,t_j));
1215    }
1216    s->lcm_of_lm=lm;
1217    //          pDelete(&short_s);
1218    //assume(lm!=NULL);
1219    nodes[spc]=s;
1220    spc++;
1221 
1222  // }
1223  //else
1224  //{
1225        //c->states[i][j]=HASTREP;
1226  //}
1227  }
1228 
1229  assume(spc<=i);
1230  //now ideal quotient crit
1231  qsort(nodes,spc,sizeof(sorted_pair_node*),iq_crit);
1232 
1233    sorted_pair_node** nodes_final=(sorted_pair_node**) omalloc(sizeof(sorted_pair_node*)*i);
1234  int spc_final=0;
1235  j=0;
1236  while(j<spc)
1237  {
1238    int lower=j;
1239    int upper;
1240    BOOLEAN has=FALSE;
1241    for(upper=lower+1;upper<spc;upper++)
1242    {
1243     
1244      if(!pLmEqual(nodes[lower]->lcm_of_lm,nodes[upper]->lcm_of_lm))
1245      {
1246  break;
1247      }
1248      if (has_t_rep(nodes[upper]->i,nodes[upper]->j,c))
1249  has=TRUE;
1250
1251    }
1252    upper=upper-1;
1253    int z;
1254    assume(spc_final<=j);
1255    for(z=0;z<spc_final;z++)
1256    {
1257      if(p_LmDivisibleBy(nodes_final[z]->lcm_of_lm,nodes[lower]->lcm_of_lm,c->r))
1258      {
1259  has=TRUE;
1260  break;
1261      }
1262    }
1263   
1264    if(has)
1265    {
1266      for(;lower<=upper;lower++)
1267      {
1268  //free_sorted_pair_node(nodes[lower],c->r);
1269  //omfree(nodes[lower]);
1270  nodes[lower]=NULL;
1271      }
1272      j=upper+1;
1273      continue;
1274    }
1275    else
1276    {
1277      nodes[lower]->lcm_of_lm=pCopy(nodes[lower]->lcm_of_lm);
1278      assume(_p_GetComp(c->S->m[nodes[lower]->i],c->r)==_p_GetComp(c->S->m[nodes[lower]->j],c->r));
1279      nodes_final[spc_final]=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1280     
1281      *(nodes_final[spc_final++])=*(nodes[lower]);
1282      //c->tmp_spn[nodes[lower]->j]=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1283      nodes[lower]=NULL;
1284      for(lower=lower+1;lower<=upper;lower++)
1285      {
1286  //  free_sorted_pair_node(nodes[lower],c->r);
1287  //omfree(nodes[lower]);
1288  nodes[lower]=NULL;
1289      }
1290      j=upper+1;
1291      continue;
1292    }
1293  }
1294
1295  //  Print("i:%d,spc_final:%d",i,spc_final);
1296
1297
1298
1299
1300  assume(spc_final<=spc);
1301  omfree(nodes);
1302  nodes=NULL;
1303
1304  add_to_reductors(c, h, c->lengths[c->n-1], TRUE);
1305  //i=posInS(c->strat,c->strat->sl,h,0 ecart);
1306  if (!(c->nc)){
1307    if (c->lengths[c->n-1]==1)
1308      shorten_tails(c,c->S->m[c->n-1]);
1309  }
1310  //you should really update c->lengths, c->strat->lenS, and the oder of polys in strat if you sort after lengths
1311
1312  //for(i=c->strat->sl; i>0;i--)
1313  //  if(c->strat->lenS[i]<c->strat->lenS[i-1]) printf("fehler bei %d\n",i);
1314  if (c->Rcounter>50) {
1315    c->Rcounter=0;
1316    cleanS(c->strat,c);
1317  }
1318  if(!ip){
1319    qsort(nodes_final,spc_final,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
1320 
1321   
1322    c->apairs=spn_merge(c->apairs,c->pair_top+1,nodes_final,spc_final,c);
1323    c->pair_top+=spc_final;
1324    clean_top_of_pair_list(c);
1325    omfree(nodes_final);
1326    return NULL;
1327  }
1328  {
1329    *ip=spc_final;
1330    return nodes_final;
1331  }
1332
1333 
1334
1335}
1336
1337
1338static poly redNF2 (poly h,slimgb_alg* c , int &len, number&  m,int n)
1339{
1340  m=nInit(1);
1341  if (h==NULL) return NULL;
1342
1343  assume(len==pLength(h));
1344  kStrategy strat=c->strat;
1345  if (0 > strat->sl)
1346  {
1347    return h;
1348  }
1349  int j;
1350 
1351  LObject P(h);
1352  P.SetShortExpVector();
1353  P.bucket = kBucketCreate(currRing);
1354  // BOOLEAN corr=lenS_correct(strat);
1355  kBucketInit(P.bucket,P.p,len /*pLength(P.p)*/);
1356  //wlen_set lenSw=(wlen_set) c->strat->lenS;
1357  //FIXME: plainly wrong
1358  //strat->lenS;
1359  //if (strat->lenSw!=NULL)
1360  //  lenSw=strat->lenSw;
1361  //int max_pos=simple_posInS(strat,P.p);
1362  loop
1363    {
1364
1365      j=kFindDivisibleByInS(strat->S,strat->sevS,strat->sl,&P);
1366      if ((j>=0) && ((!n)||
1367        ((strat->lenS[j]<=n) &&
1368         ((strat->lenSw==NULL)||
1369         (strat->lenSw[j]<=n)))))
1370      {
1371
1372 
1373        nNormalize(pGetCoeff(P.p));
1374#ifdef KDEBUG
1375        if (TEST_OPT_DEBUG)
1376        {
1377          PrintS("red:");
1378          wrp(h);
1379          PrintS(" with ");
1380          wrp(strat->S[j]);
1381        }
1382#endif
1383       
1384        number coef=kBucketPolyRed(P.bucket,strat->S[j],
1385                                   strat->lenS[j]/*pLength(strat->S[j])*/,
1386                                   strat->kNoether);
1387  number m2=nMult(m,coef);
1388  nDelete(&m);
1389  m=m2;
1390        nDelete(&coef);
1391        h = kBucketGetLm(P.bucket);
1392 
1393  if (h==NULL) {
1394    len=0;
1395    kBucketDestroy(&P.bucket);
1396    return 
1397    NULL;}
1398        P.p=h;
1399        P.t_p=NULL;
1400        P.SetShortExpVector();
1401#ifdef KDEBUG
1402        if (TEST_OPT_DEBUG)
1403        {
1404          PrintS("\nto:");
1405          wrp(h);
1406          PrintLn();
1407        }
1408#endif
1409      }
1410      else
1411      {
1412        kBucketClear(P.bucket,&(P.p),&len);
1413        kBucketDestroy(&P.bucket);
1414        pNormalize(P.p);
1415  assume(len==(pLength(P.p)));
1416        return P.p;
1417      }
1418    }
1419}
1420
1421
1422
1423static poly redTailShort(poly h, kStrategy strat){
1424  if (h==NULL) return NULL;//n_Init(1,currRing);
1425  if (TEST_V_MODPSOLVSB){
1426    bit_reduce(pNext(h), strat->tailRing);
1427  }
1428  int sl=strat->sl;
1429  int i;
1430  int len=pLength(h);
1431  for(i=0;i<=strat->sl;i++){
1432    if((strat->lenS[i]>2) || ((strat->lenSw!=NULL) && (strat->lenSw[i]>2)))
1433      break;
1434  }
1435  return(redNFTail(h,i-1,strat, len));
1436}
1437
1438static void line_of_extended_prod(int fixpos,slimgb_alg* c){
1439    if (c->gcd_of_terms[fixpos]==NULL)
1440  {
1441    c->gcd_of_terms[fixpos]=gcd_of_terms(c->S->m[fixpos],c->r);
1442    if (c->gcd_of_terms[fixpos])
1443    {
1444      int i;
1445      for(i=0;i<fixpos;i++)
1446        if((c->states[fixpos][i]!=HASTREP)&& (extended_product_criterion(c->S->m[fixpos],c->gcd_of_terms[fixpos], c->S->m[i],c->gcd_of_terms[i],c)))
1447{
1448          c->states[fixpos][i]=HASTREP;
1449    c->extended_product_crit++;
1450}     
1451      for(i=fixpos+1;i<c->n;i++)
1452        if((c->states[i][fixpos]!=HASTREP)&& (extended_product_criterion(c->S->m[fixpos],c->gcd_of_terms[fixpos], c->S->m[i],c->gcd_of_terms[i],c)))
1453  {        c->states[i][fixpos]=HASTREP;
1454  c->extended_product_crit++;
1455  }
1456    }
1457  }
1458}
1459static void c_S_element_changed_hook(int pos, slimgb_alg* c){
1460  length_one_crit(c,pos, c->lengths[pos]);
1461  if (!c->nc)
1462    line_of_extended_prod(pos,c);
1463}
1464class poly_tree_node {
1465public:
1466  poly p;
1467  poly_tree_node* l;
1468  poly_tree_node* r;
1469  int n;
1470  poly_tree_node(int sn):l(NULL),r(NULL),n(sn){}
1471};
1472class exp_number_builder{
1473public:
1474  poly_tree_node* top_level;
1475  int n;
1476  int get_n(poly p);
1477  exp_number_builder():top_level(0),n(0){}
1478};
1479int exp_number_builder::get_n(poly p){
1480  poly_tree_node** node=&top_level;
1481  while(*node!=NULL){
1482    int c=pLmCmp(p,(*node)->p);
1483    if (c==0) return (*node)->n;
1484    if (c==-1) node=&((*node)->r);
1485    else
1486      node=&((*node)->l);
1487  }
1488  (*node)= new poly_tree_node(n);
1489  n++;
1490  (*node)->p=pLmInit(p);
1491  return (*node)->n;
1492}
1493
1494//mac_polys exp are smaller iff they are greater by monomial ordering
1495//corresponding to solving linear equations notation
1496
1497//! obsolete
1498struct int_poly_pair{
1499  poly p;
1500  int n;
1501};
1502
1503
1504//! obsolete
1505void t2ippa_rec(poly* ip,int* ia, poly_tree_node* k, int &offset){
1506    if(!k) return;
1507    t2ippa_rec(ip,ia,k->l,offset);
1508    ip[offset]=k->p;
1509    ia[k->n]=offset;
1510    ++offset;
1511
1512    t2ippa_rec(ip,ia,k->r,offset);
1513    delete k;
1514  }
1515
1516//! obsolete
1517void t2ippa(poly* ip,int* ia,exp_number_builder & e){
1518
1519  int o=0;
1520  t2ippa_rec(ip,ia,e.top_level,o);
1521}
1522int anti_poly_order(const void* a, const void* b){
1523  return -pLmCmp(((int_poly_pair*) a)->p,((int_poly_pair*) b)->p );
1524}
1525
1526BOOLEAN is_valid_ro(red_object & ro){
1527  red_object r2=ro;
1528  ro.validate();
1529  if ((r2.p!=ro.p)||(r2.sev!=ro.sev)) return FALSE;
1530  return TRUE;
1531}
1532
1533
1534
1535static void go_on (slimgb_alg* c){
1536  //set limit of 1000 for multireductions, at the moment for
1537  //programming reasons
1538  int i=0;
1539  c->average_length=0;
1540  for(i=0;i<c->n;i++){
1541    c->average_length+=c->lengths[i];
1542  }
1543  c->average_length=c->average_length/c->n;
1544  i=0;
1545  poly* p=(poly*) omalloc((PAR_N+1)*sizeof(poly));//nullterminated
1546
1547  int curr_deg=-1;
1548  while(i<PAR_N){
1549    sorted_pair_node* s=top_pair(c);//here is actually chain criterium done
1550    if (!s) break;
1551    if(curr_deg>=0){
1552      if (s->deg >curr_deg) break;
1553     
1554    }
1555   
1556    else curr_deg=s->deg;
1557    if ((TEST_OPT_DEGBOUND) && (curr_deg>Kstd1_deg)){
1558        int j;
1559        for(j=c->pair_top;j>=0;j--){
1560            s=c->apairs[j];
1561            if (s->i<0){
1562               
1563                               add_to_basis_ideal_quotient(s->lcm_of_lm,-1,-1,c,NULL,FALSE);
1564                s->lcm_of_lm=NULL;
1565                free_sorted_pair_node(s,c->r);
1566            }
1567            else
1568                free_sorted_pair_node(s,c->r);
1569            c->pair_top--;
1570            assume(c->pair_top=j-1);
1571        }
1572        c->pair_top=-1;
1573        break;
1574    }
1575    quick_pop_pair(c);
1576    if(s->i>=0){
1577      //be careful replace_pair use createShortSpoly which is not noncommutative
1578      //replace_pair(s->i,s->j,c);
1579    if(s->i==s->j) {
1580      free_sorted_pair_node(s,c->r);
1581      continue;
1582  }
1583    }
1584    poly h;
1585    if(s->i>=0){
1586      if (!c->nc)
1587  h=ksOldCreateSpoly(c->S->m[s->i], c->S->m[s->j], NULL, c->r);
1588      else
1589  h= nc_CreateSpoly(c->S->m[s->i], c->S->m[s->j], NULL, c->r);
1590    } 
1591    else
1592      h=s->lcm_of_lm;
1593    if(s->i>=0)
1594      now_t_rep(s->j,s->i,c);
1595    number coef;
1596    int mlen=pLength(h);
1597    if (!c->nc){
1598      h=redNF2(h,c,mlen,coef,2);
1599      redTailShort(h,c->strat);
1600      nDelete(&coef);
1601    }
1602    free_sorted_pair_node(s,c->r);
1603    if(!h) continue;
1604    int len=pLength(h);
1605    p[i]=h;
1606   
1607    i++;
1608  }
1609  p[i]=NULL;
1610//  pre_comp(p,i,c);
1611  if(i==0){
1612    omfree(p);
1613    return;
1614  }
1615  #ifdef TGB_RESORT_PAIRS
1616  c->replaced=new bool[c->n];
1617  c->used_b=FALSE;
1618  #endif
1619  red_object* buf=(red_object*) omalloc(i*sizeof(red_object));
1620  c->normal_forms+=i;
1621  int j;
1622  for(j=0;j<i;j++){
1623    buf[j].p=p[j];
1624    buf[j].sev=pGetShortExpVector(p[j]);
1625    buf[j].bucket = kBucketCreate(currRing);
1626   
1627    int len=pLength(p[j]);
1628    kBucketInit(buf[j].bucket,buf[j].p,len);
1629    buf[j].initial_quality=buf[j].guess_quality(c);
1630    assume(buf[j].initial_quality>=0);
1631  }
1632  omfree(p);
1633  qsort(buf,i,sizeof(red_object),red_object_better_gen);
1634//    Print("\ncurr_deg:%i\n",curr_deg);
1635  if (TEST_OPT_PROT)
1636  {
1637    Print("%dM[%d,",curr_deg,i);
1638  }
1639#ifdef FIND_DETERMINISTIC
1640  c->modifiedS=(BOOLEAN*) omalloc((c->strat->sl+1)*sizeof(BOOLEAN));
1641  c->expandS=(poly*) omalloc((1)*sizeof(poly));
1642  c->expandS[0]=NULL;
1643  int z2;
1644  for(z2=0;z2<=c->strat->sl;z2++)
1645    c->modifiedS[z2]=FALSE;
1646#endif
1647  multi_reduction(buf, i, c);
1648  #ifdef TGB_RESORT_PAIRS
1649  if (c->used_b) {
1650    if (TEST_OPT_PROT)
1651        PrintS("B");
1652    int e;
1653    for(e=0;e<=c->pair_top;e++){
1654        if(c->apairs[e]->i<0) continue;
1655        assume(c->apairs[e]->j>=0);
1656        if ((c->replaced[c->apairs[e]->i])||(c->replaced[c->apairs[e]->j])) {
1657            sorted_pair_node* s=c->apairs[e];
1658            s->expected_length=pair_weighted_length(s->i,s->j,c);
1659        }
1660    }
1661    qsort(c->apairs,c->pair_top+1,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
1662  }
1663  #endif
1664#ifdef TGB_DEBUG
1665 {
1666   int k;
1667   for(k=0;k<i;k++)
1668   {
1669     assume(kFindDivisibleByInS_easy(c->strat,buf[k])<0);
1670     int k2;
1671     for(k2=0;k2<i;k2++)
1672     {
1673       if(k==k2) continue;
1674       assume((!(p_LmDivisibleBy(buf[k].p,buf[k2].p,c->r)))||(wrp(buf[k].p),Print(" k %d k2 %d ",k,k2),wrp(buf[k2].p),FALSE));
1675     }
1676   }
1677 }
1678#endif
1679  //resort S
1680#ifdef FIND_DETERMINISTIC
1681  for(z2=0;z2<=c->strat->sl;z2++)
1682  {
1683    if (c->modifiedS[z2])
1684    {
1685      wlen_type qual;
1686      int new_pos;
1687      if (c->strat->lenSw!=NULL)
1688          new_pos=simple_posInS(c->strat,c->strat->S[z2],strat->lenS[z2],strat->Sw[z2]);
1689      else
1690          new_pos=simple_posInS(c->strat,c->strat->S[z2],strat->lenS[z2],lenS[z2]);
1691     
1692      if (new_pos<z2)
1693      { 
1694         move_forward_in_S(z2,new_pos,c->strat);
1695      }
1696     
1697      assume(new_pos<=z2);
1698    }
1699  }
1700  for(z2=0;c->expandS[z2]!=NULL;z2++)
1701  {
1702    add_to_reductors(c,c->expandS[z2],pLength(c->expandS[z2]));
1703    // PrintS("E");
1704  }
1705  omfree(c->modifiedS);
1706  c->modifiedS=NULL;
1707  omfree(c->expandS);
1708  c->expandS=NULL;
1709#endif
1710  if (TEST_OPT_PROT)
1711      Print("%i]",i); 
1712
1713  int* ibuf=(int*) omalloc(i*sizeof(int));
1714  sorted_pair_node*** sbuf=(sorted_pair_node***) omalloc(i*sizeof(sorted_pair_node**));
1715  for(j=0;j<i;j++)
1716  {
1717    int len;
1718    poly p;
1719    buf[j].flatten();
1720    kBucketClear(buf[j].bucket,&p, &len);
1721    kBucketDestroy(&buf[j].bucket);
1722
1723    if (!c->nc)
1724      p=redTailShort(p, c->strat);
1725    sbuf[j]=add_to_basis_ideal_quotient(p,-1,-1,c,ibuf+j, TRUE);
1726    //sbuf[j]=add_to_basis(p,-1,-1,c,ibuf+j);
1727  }
1728  int sum=0;
1729  for(j=0;j<i;j++){
1730    sum+=ibuf[j];
1731  }
1732  sorted_pair_node** big_sbuf=(sorted_pair_node**) omalloc(sum*sizeof(sorted_pair_node*));
1733  int partsum=0;
1734  for(j=0;j<i;j++)
1735  {
1736    memmove(big_sbuf+partsum, sbuf[j],ibuf[j]*sizeof(sorted_pair_node*));
1737    omfree(sbuf[j]);
1738    partsum+=ibuf[j];
1739  }
1740
1741  qsort(big_sbuf,sum,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
1742  c->apairs=spn_merge(c->apairs,c->pair_top+1,big_sbuf,sum,c);
1743  c->pair_top+=sum;
1744  clean_top_of_pair_list(c);
1745  omfree(big_sbuf);
1746  omfree(sbuf);
1747  omfree(ibuf);
1748  omfree(buf);
1749#ifdef TGB_DEBUG
1750  int z;
1751  for(z=1;z<=c->pair_top;z++)
1752  {
1753    assume(pair_better(c->apairs[z],c->apairs[z-1],c));
1754  }
1755#endif
1756  if (TEST_OPT_PROT)
1757      Print("(%d)",c->pair_top+1); 
1758  while(!(idIs0(c->add_later))){
1759    ideal add=c->add_later;
1760   
1761    ideal add2=kInterRed(add,NULL);
1762    id_Delete(&add,currRing);
1763    idSkipZeroes(add2);
1764    c->add_later=idInit(5000,c->S->rank);
1765    memset(c->add_later->m,0,5000*sizeof(poly));
1766    for(i=0;i<add2->idelems();i++){
1767      if (add2->m[i]!=NULL)
1768          add_to_basis_ideal_quotient(add2->m[i],-1,-1,c,NULL, TRUE);
1769      add2->m[i]=NULL;
1770    }
1771    id_Delete(&add2, c->r);
1772  }
1773  #ifdef TGB_RESORT_PAIRS
1774  delete c->replaced;
1775  c->replaced=NULL;
1776  c->used_b=FALSE;
1777  #endif
1778  return;
1779}
1780
1781
1782
1783#ifdef REDTAIL_S
1784
1785static poly redNFTail (poly h,const int sl,kStrategy strat, int len)
1786{
1787  if (h==NULL) return NULL;
1788  pTest(h);
1789  if (0 > sl)
1790    return h;
1791  if (pNext(h)==NULL) return h;
1792
1793  int j;
1794  poly res=h;
1795  poly act=res;
1796  LObject P(pNext(h));
1797  pNext(res)=NULL;
1798  P.bucket = kBucketCreate(currRing);
1799  len--;
1800  h=P.p;
1801  if (len <=0) len=pLength(h);
1802  kBucketInit(P.bucket,h /*P.p*/,len /*pLength(P.p)*/);
1803  pTest(h);
1804  loop
1805  {
1806      P.p=h;
1807      P.t_p=NULL;
1808      P.SetShortExpVector();
1809      loop
1810      {
1811          j=kFindDivisibleByInS(strat->S,strat->sevS,sl,&P);
1812          if (j>=0)
1813          {
1814#ifdef REDTAIL_PROT
1815            PrintS("r");
1816#endif
1817            nNormalize(pGetCoeff(P.p));
1818#ifdef KDEBUG
1819            if (TEST_OPT_DEBUG)
1820            {
1821              PrintS("red tail:");
1822              wrp(h);
1823              PrintS(" with ");
1824              wrp(strat->S[j]);
1825            }
1826#endif
1827            number coef;
1828            pTest(strat->S[j]);
1829            coef=kBucketPolyRed(P.bucket,strat->S[j],
1830                                strat->lenS[j]/*pLength(strat->S[j])*/,strat->kNoether);
1831            pMult_nn(res,coef);
1832            nDelete(&coef);
1833            h = kBucketGetLm(P.bucket);
1834            pTest(h);
1835            if (h==NULL)
1836            {
1837#ifdef REDTAIL_PROT
1838              PrintS(" ");
1839#endif
1840        kBucketDestroy(&P.bucket);
1841              return res;
1842            }
1843            pTest(h);
1844            P.p=h;
1845            P.t_p=NULL;
1846            P.SetShortExpVector();
1847#ifdef KDEBUG
1848            if (TEST_OPT_DEBUG)
1849            {
1850              PrintS("\nto tail:");
1851              wrp(h);
1852              PrintLn();
1853            }
1854#endif
1855          }
1856          else
1857          {
1858#ifdef REDTAIL_PROT
1859            PrintS("n");
1860#endif
1861            break;
1862          }
1863      } /* end loop current mon */
1864      //   poly tmp=pHead(h /*kBucketGetLm(P.bucket)*/);
1865      //act->next=tmp;pIter(act);
1866      act->next=kBucketExtractLm(P.bucket);pIter(act);
1867      h = kBucketGetLm(P.bucket);
1868      if (h==NULL)
1869      {
1870#ifdef REDTAIL_PROT
1871        PrintS(" ");
1872#endif
1873  kBucketDestroy(&P.bucket);
1874        return res;
1875      }
1876      pTest(h);
1877  }
1878}
1879#endif
1880
1881
1882//try to fill, return FALSE iff queue is empty
1883
1884//transfers ownership of m to mat
1885void init_with_mac_poly(tgb_sparse_matrix* mat, int row, mac_poly m){
1886  assume(mat->mp[row]==NULL);
1887  mat->mp[row]=m;
1888#ifdef TGB_DEBUG
1889  mac_poly r=m;
1890  while(r){
1891    assume(r->exp<mat->columns);
1892    r=r->next;
1893  }
1894#endif
1895}
1896poly free_row_to_poly(tgb_sparse_matrix* mat, int row, poly* monoms, int monom_index){
1897  poly p=NULL;
1898  poly* set_this=&p;
1899  mac_poly r=mat->mp[row];
1900  mat->mp[row]=NULL;
1901  while(r)
1902  {
1903    (*set_this)=pLmInit(monoms[monom_index-1-r->exp]);
1904    pSetCoeff((*set_this),r->coef);
1905    set_this=&((*set_this)->next);
1906    mac_poly old=r;
1907    r=r->next;
1908    delete old;
1909   
1910  }
1911  return p;
1912
1913}
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925static int poly_crit(const void* ap1, const void* ap2){
1926  poly p1,p2;
1927  p1=*((poly*) ap1);
1928  p2=*((poly*)ap2);
1929
1930  int c=pLmCmp(p1,p2);
1931  if (c !=0) return c;
1932  int l1=pLength(p1);
1933  int l2=pLength(p2);
1934  if (l1<l2) return -1;
1935  if (l1>l2) return 1;
1936  return 0;
1937}
1938slimgb_alg::slimgb_alg(ideal I, BOOLEAN F4){
1939 
1940 
1941  r=currRing;
1942  nc=rIsPluralRing(r);
1943 
1944  is_homog=TRUE;
1945  {
1946    int hz;
1947    for(hz=0;hz<IDELEMS(I);hz++){
1948      assume(I->m[hz]!=NULL);
1949      int d=pTotaldegree(I->m[hz]);
1950      poly t=I->m[hz]->next;
1951      while(t)
1952      {
1953        if (d!=pTotaldegree(t,r))
1954        {
1955          is_homog=FALSE;
1956          break;
1957        }
1958        t=t->next;
1959      }
1960      if(!(is_homog)) break;
1961    }
1962  }
1963  //  Print("is homog:%d",c->is_homog);
1964  void* h;
1965  poly hp;
1966  int i,j;
1967  to_destroy=NULL;
1968  easy_product_crit=0;
1969  extended_product_crit=0;
1970  if (rField_is_Zp(r))
1971    is_char0=FALSE;
1972  else
1973    is_char0=TRUE;
1974  //not fully correct
1975  //(rChar()==0);
1976  F4_mode=F4;
1977 
1978  if ((!F4_mode)&&(!is_homog) &&(pLexOrder)){
1979    this->doubleSugar=TRUE;
1980  }
1981  else this->doubleSugar=FALSE;
1982  reduction_steps=0;
1983  last_index=-1;
1984
1985  F=NULL;
1986  F_minus=NULL;
1987
1988  Rcounter=0;
1989
1990  soon_free=NULL;
1991
1992  tmp_lm=pOne();
1993
1994  normal_forms=0;
1995  current_degree=1;
1996 
1997  max_pairs=5*I->idelems();
1998 
1999  apairs=(sorted_pair_node**) omalloc(sizeof(sorted_pair_node*)*max_pairs);
2000  pair_top=-1;
2001
2002  int n=I->idelems();
2003  array_lengths=n;
2004
2005 
2006  i=0;
2007  this->n=0;
2008  T_deg=(int*) omalloc(n*sizeof(int));
2009  if((!(is_homog)) &&(pLexOrder))
2010    T_deg_full=(int*) omalloc(n*sizeof(int));
2011  else
2012    T_deg_full=NULL;
2013  tmp_pair_lm=(poly*) omalloc(n*sizeof(poly));
2014  tmp_spn=(sorted_pair_node**) omalloc(n*sizeof(sorted_pair_node*));
2015  lm_bin=omGetSpecBin(POLYSIZE + (r->ExpL_Size)*sizeof(long));
2016#ifdef HEAD_BIN
2017  HeadBin=omGetSpecBin(POLYSIZE + (currRing->ExpL_Size)*sizeof(long));
2018#endif
2019  /* omUnGetSpecBin(&(c->HeadBin)); */
2020  #ifndef HAVE_BOOST
2021  h=omalloc(n*sizeof(char*));
2022 
2023  states=(char**) h;
2024  #endif
2025  h=omalloc(n*sizeof(int));
2026  lengths=(int*) h;
2027  weighted_lengths=(wlen_type*)omalloc(n*sizeof(wlen_type));
2028  gcd_of_terms=(poly*) omalloc(n*sizeof(poly));
2029 
2030  short_Exps=(long*) omalloc(n*sizeof(long));
2031  if (F4_mode)
2032    S=idInit(n,I->rank);
2033  else
2034    S=idInit(1,I->rank);
2035  strat=new skStrategy;
2036  strat->syzComp = 0;
2037  initBuchMoraCrit(strat);
2038  initBuchMoraPos(strat);
2039  strat->initEcart = initEcartBBA;
2040  strat->tailRing=r;
2041  strat->enterS = enterSBba;
2042  strat->sl = -1;
2043  i=n;
2044  i=1;//some strange bug else
2045  /* initS(c->S,NULL,c->strat); */
2046  /* intS start: */
2047  // i=((i+IDELEMS(c->S)+15)/16)*16;
2048  strat->ecartS=(intset)omAlloc(i*sizeof(int)); /*initec(i);*/
2049  strat->sevS=(unsigned long*)omAlloc0(i*sizeof(unsigned long));
2050  /*initsevS(i);*/
2051  strat->S_2_R=(int*)omAlloc0(i*sizeof(int));/*initS_2_R(i);*/
2052  strat->fromQ=NULL;
2053  strat->Shdl=idInit(1,1);
2054  strat->S=strat->Shdl->m;
2055  strat->lenS=(int*)omAlloc0(i*sizeof(int));
2056  if((is_char0)||((pLexOrder) &&(!is_homog)))
2057    strat->lenSw=(wlen_type*)omAlloc0(i*sizeof(wlen_type));
2058  else
2059    strat->lenSw=NULL;
2060  sorted_pair_node* si;
2061  assume(n>0);
2062  add_to_basis_ideal_quotient(I->m[0],-1,-1,this,NULL, TRUE);
2063
2064  assume(strat->sl==strat->Shdl->idelems()-1);
2065  if(!(F4_mode))
2066  {
2067    for (i=1;i<n;i++)//the 1 is wanted, because first element is added to basis
2068    {
2069      //     add_to_basis(I->m[i],-1,-1,c);
2070      si=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
2071      si->i=-1;
2072      si->j=-2;
2073      si->expected_length=pQuality(I->m[i],this,pLength(I->m[i]));
2074      si->deg=pTotaldegree(I->m[i]);
2075      if (!rField_is_Zp(r)){ 
2076        pCleardenom(I->m[i]);
2077      }
2078      si->lcm_of_lm=I->m[i];
2079     
2080      //      c->apairs[n-1-i]=si;
2081      apairs[n-i-1]=si;
2082      ++(pair_top);
2083    }
2084  }
2085  else
2086  {
2087    for (i=1;i<n;i++)//the 1 is wanted, because first element is added to basis
2088      add_to_basis_ideal_quotient(I->m[i],-1,-1,this,NULL, TRUE);
2089  }
2090  for(i=0;i<I->idelems();i++)
2091  {
2092    I->m[i]=NULL;
2093  }
2094  idDelete(&I);
2095  add_later=idInit(5000,S->rank);
2096  memset(add_later->m,0,5000*sizeof(poly));
2097}
2098slimgb_alg::~slimgb_alg(){
2099  id_Delete(&add_later,r);
2100  int i,j;
2101  slimgb_alg* c=this;
2102  while(c->to_destroy)
2103  {
2104    pDelete(&(c->to_destroy->p));
2105    poly_list_node* old=c->to_destroy;
2106    c->to_destroy=c->to_destroy->next;
2107    omfree(old);
2108  }
2109  while(c->F)
2110  {
2111    for(i=0;i<c->F->size;i++){
2112      pDelete(&(c->F->mp[i].m));
2113    }
2114    omfree(c->F->mp);
2115    c->F->mp=NULL;
2116    mp_array_list* old=c->F;
2117    c->F=c->F->next;
2118    omfree(old);
2119  }
2120  while(c->F_minus)
2121  {
2122    for(i=0;i<c->F_minus->size;i++){
2123      pDelete(&(c->F_minus->p[i]));
2124    }
2125    omfree(c->F_minus->p);
2126    c->F_minus->p=NULL;
2127    poly_array_list* old=c->F_minus;
2128    c->F_minus=c->F_minus->next;
2129    omfree(old);
2130  }
2131  #ifndef HAVE_BOOST
2132  for(int z=1 /* zero length at 0 */;z<c->n;z++){
2133    omfree(c->states[z]);
2134  }
2135  omfree(c->states);
2136  #endif
2137
2138  omfree(c->lengths);
2139  omfree(c->weighted_lengths);
2140  for(int z=0;z<c->n;z++)
2141  {
2142    pDelete(&c->tmp_pair_lm[z]);
2143    omfree(c->tmp_spn[z]);
2144  }
2145  omfree(c->tmp_pair_lm);
2146  omfree(c->tmp_spn);
2147 
2148  omfree(c->T_deg);
2149  if(c->T_deg_full)
2150    omfree(c->T_deg_full);
2151
2152  omFree(c->strat->ecartS);
2153  omFree(c->strat->sevS);
2154//   initsevS(i);
2155  omFree(c->strat->S_2_R);
2156   
2157
2158  omFree(c->strat->lenS);
2159
2160  if(c->strat->lenSw)  omFree(c->strat->lenSw);
2161
2162
2163
2164
2165  for(i=0;i<c->n;i++){
2166    if(c->gcd_of_terms[i])
2167      pDelete(&(c->gcd_of_terms[i]));
2168  }
2169  omfree(c->gcd_of_terms);
2170
2171  omfree(c->apairs);
2172  if (TEST_OPT_PROT)
2173  {
2174    Print("calculated %d NFs\n",c->normal_forms);
2175    Print("applied %i product crit, %i extended_product crit \n", c->easy_product_crit, c->extended_product_crit);
2176  }
2177  int deleted_form_c_s=0;
2178 
2179  for(i=0;i<=c->strat->sl;i++){
2180    if (!c->strat->S[i]) continue;
2181    BOOLEAN found=FALSE;
2182    for(j=0;j<c->n;j++){
2183      if (c->S->m[j]==c->strat->S[i]){
2184        found=TRUE;
2185        break;
2186      }
2187    }
2188    if(!found) pDelete(&c->strat->S[i]);
2189  }
2190//   for(i=0;i<c->n;i++){
2191//     if (c->rep[i]!=i){
2192// //       for(j=0;j<=c->strat->sl;j++){
2193// //   if(c->strat->S[j]==c->S->m[i]){
2194// //     c->strat->S[j]=NULL;
2195// //     break;
2196// //   }
2197// //       }
2198// //      PrintS("R_delete");
2199//       pDelete(&c->S->m[i]);
2200//     }
2201//   }
2202  if (!(TEST_OPT_DEGBOUND)){
2203
2204  //FIXME: not minimal when using find monomials
2205      for(i=0;i<c->n;i++)
2206      {
2207        assume(c->S->m[i]!=NULL);
2208        for(j=0;j<c->n;j++)
2209        {
2210          if((c->S->m[j]==NULL)||(i==j)) 
2211            continue;
2212          assume(p_LmShortDivisibleBy(c->S->m[j],c->short_Exps[j],
2213                 c->S->m[i],~c->short_Exps[i],
2214                 c->r)==p_LmDivisibleBy(c->S->m[j],
2215                 c->S->m[i],
2216                 c->r));
2217          if (p_LmShortDivisibleBy(c->S->m[j],c->short_Exps[j],
2218              c->S->m[i],~c->short_Exps[i],
2219              c->r))
2220          {
2221            pDelete(&c->S->m[i]);
2222            break;
2223          }
2224        }
2225      }
2226  }
2227  omfree(c->short_Exps);
2228 
2229
2230  ideal I=c->S;
2231 
2232  IDELEMS(I)=c->n;
2233
2234  idSkipZeroes(I);
2235  for(i=0;i<=c->strat->sl;i++)
2236    c->strat->S[i]=NULL;
2237  id_Delete(&c->strat->Shdl,c->r);
2238  pDelete(&c->tmp_lm);
2239  omUnGetSpecBin(&lm_bin);
2240  delete c->strat;
2241}
2242ideal t_rep_gb(ring r,ideal arg_I, BOOLEAN F4_mode){
2243  //Print("degb ound%d\n",Kstd1_deg);
2244  //  Print("QlogSize(0) %d, QlogSize(1) %d,QlogSize(-2) %d, QlogSize(5) %d\n", QlogSize(nlInit(0)),QlogSize(nlInit(1)),QlogSize(nlInit(-2)),QlogSize(nlInit(5)));
2245 
2246  if (TEST_OPT_PROT)
2247    if (F4_mode)
2248      PrintS("F4 Modus \n");
2249     
2250  //debug_Ideal=arg_debug_Ideal;
2251  //if (debug_Ideal) PrintS("DebugIdeal received\n");
2252  // Print("Idelems %i \n----------\n",IDELEMS(arg_I));
2253  ideal I=idCompactify(arg_I);
2254   int i;
2255  for(i=0;i<IDELEMS(I);i++){
2256    simplify_poly(I->m[i],currRing);
2257  }
2258  if (idIs0(I)) return I;
2259
2260  qsort(I->m,IDELEMS(I),sizeof(poly),poly_crit);
2261  //Print("Idelems %i \n----------\n",IDELEMS(I));
2262  //slimgb_alg* c=(slimgb_alg*) omalloc(sizeof(slimgb_alg));
2263  slimgb_alg* c=new slimgb_alg(I, F4_mode);
2264   
2265
2266  while(c->pair_top>=0)
2267  {
2268    if(F4_mode)
2269      go_on_F4(c);
2270    else
2271      go_on(c);
2272  }
2273  I=c->S;
2274  delete c;
2275  if (TEST_OPT_REDSB){
2276    ideal erg=kInterRed(I,NULL);
2277    assume(I!=erg);
2278    id_Delete(&I, currRing);
2279    return erg;
2280  }
2281  //qsort(I->m, IDELEMS(I),sizeof(poly),pLmCmp_func);
2282  assume(I->rank>=idRankFreeModule(I));
2283  return(I);
2284}
2285void now_t_rep(const int & arg_i, const int & arg_j, slimgb_alg* c){
2286  int i,j;
2287  if (arg_i==arg_j){
2288    return;
2289  }
2290  if (arg_i>arg_j){
2291    i=arg_j;
2292    j=arg_i;
2293  } else {
2294    i=arg_i;
2295    j=arg_j;
2296  }
2297  c->states[j][i]=HASTREP;
2298}
2299
2300static BOOLEAN has_t_rep(const int & arg_i, const  int & arg_j, slimgb_alg* state){
2301  assume(0<=arg_i);
2302  assume(0<=arg_j);
2303  assume(arg_i<state->n);
2304  assume(arg_j<state->n);
2305  if (arg_i==arg_j)
2306  {
2307    return (TRUE);
2308  }
2309  if (arg_i>arg_j)
2310  {
2311    return (state->states[arg_i][arg_j]==HASTREP);
2312  } else
2313  {
2314    return (state->states[arg_j][arg_i]==HASTREP);
2315  }
2316}
2317static int pLcmDeg(poly a, poly b)
2318{
2319  int i;
2320  int n=0;
2321  for (i=pVariables; i; i--)
2322  {
2323    n+=si_max( pGetExp(a,i), pGetExp(b,i));
2324  }
2325  return n;
2326
2327}
2328
2329
2330
2331static void shorten_tails(slimgb_alg* c, poly monom)
2332{
2333  return;
2334// BOOLEAN corr=lenS_correct(c->strat);
2335  for(int i=0;i<c->n;i++)
2336  {
2337    //enter tail
2338   
2339    if (c->S->m[i]==NULL) continue;
2340    poly tail=c->S->m[i]->next;
2341    poly prev=c->S->m[i];
2342    BOOLEAN did_something=FALSE;
2343    while((tail!=NULL)&& (pLmCmp(tail, monom)>=0))
2344    {
2345      if (p_LmDivisibleBy(monom,tail,c->r))
2346      {
2347        did_something=TRUE;
2348        prev->next=tail->next;
2349        tail->next=NULL;
2350        p_Delete(& tail,c->r);
2351        tail=prev;
2352        //PrintS("Shortened");
2353        c->lengths[i]--;
2354      }
2355      prev=tail;
2356      tail=tail->next;
2357    }
2358    if (did_something)
2359    {
2360      int new_pos;
2361      wlen_type q;
2362      q=pQuality(c->S->m[i],c,c->lengths[i]);
2363      new_pos=simple_posInS(c->strat,c->S->m[i],c->lengths[i],q);
2364
2365      int old_pos=-1;
2366      //assume new_pos<old_pos
2367      for (int z=0;z<=c->strat->sl;z++)
2368      {
2369        if (c->strat->S[z]==c->S->m[i])
2370        {
2371          old_pos=z;
2372          break;
2373        }
2374      }
2375      if (old_pos== -1)
2376        for (int z=new_pos-1;z>=0;z--)
2377        {
2378          if (c->strat->S[z]==c->S->m[i])
2379          {
2380            old_pos=z;
2381            break;
2382          }
2383        }
2384      assume(old_pos>=0);
2385      assume(new_pos<=old_pos);
2386      assume(pLength(c->strat->S[old_pos])==c->lengths[i]);
2387      c->strat->lenS[old_pos]=c->lengths[i];
2388      if (c->strat->lenSw)
2389        c->strat->lenSw[old_pos]=q;
2390
2391      if (new_pos<old_pos)
2392        move_forward_in_S(old_pos,new_pos,c->strat);
2393
2394      length_one_crit(c,i,c->lengths[i]);
2395    }
2396  }
2397}
2398static sorted_pair_node* pop_pair(slimgb_alg* c){
2399  clean_top_of_pair_list(c);
2400
2401  if(c->pair_top<0) return NULL;
2402  else return (c->apairs[c->pair_top--]);
2403}
2404sorted_pair_node* top_pair(slimgb_alg* c){
2405  super_clean_top_of_pair_list(c);//yeah, I know, it's odd that I use a different proc here
2406
2407  if(c->pair_top<0) return NULL;
2408  else return (c->apairs[c->pair_top]);
2409}
2410sorted_pair_node* quick_pop_pair(slimgb_alg* c){
2411  if(c->pair_top<0) return NULL;
2412  else return (c->apairs[c->pair_top--]);
2413}
2414
2415
2416
2417static void super_clean_top_of_pair_list(slimgb_alg* c){
2418  while((c->pair_top>=0)
2419  && (c->apairs[c->pair_top]->i>=0)
2420  && (good_has_t_rep(c->apairs[c->pair_top]->j, c->apairs[c->pair_top]->i,c)))
2421  {
2422
2423    free_sorted_pair_node(c->apairs[c->pair_top],c->r);
2424    c->pair_top--;
2425
2426  }
2427}
2428void clean_top_of_pair_list(slimgb_alg* c){
2429  while((c->pair_top>=0) && (c->apairs[c->pair_top]->i>=0) && (!state_is(UNCALCULATED,c->apairs[c->pair_top]->j, c->apairs[c->pair_top]->i,c))){
2430
2431    free_sorted_pair_node(c->apairs[c->pair_top],c->r);
2432    c->pair_top--;
2433
2434  }
2435}
2436static BOOLEAN state_is(calc_state state, const int & arg_i, const  int & arg_j, slimgb_alg* c){
2437  assume(0<=arg_i);
2438  assume(0<=arg_j);
2439  assume(arg_i<c->n);
2440  assume(arg_j<c->n);
2441  if (arg_i==arg_j)
2442  {
2443    return (TRUE);
2444  }
2445  if (arg_i>arg_j)
2446  {
2447    return (c->states[arg_i][arg_j]==state);
2448  }
2449  else return(c->states[arg_j][arg_i]==state);
2450}
2451
2452
2453void free_sorted_pair_node(sorted_pair_node* s, ring r){
2454  if (s->i>=0)
2455    p_Delete(&s->lcm_of_lm,r);
2456  omfree(s);
2457}
2458static BOOLEAN pair_better(sorted_pair_node* a,sorted_pair_node* b, slimgb_alg* c){
2459  if (a->deg<b->deg) return TRUE;
2460  if (a->deg>b->deg) return FALSE;
2461
2462//  if (a->expected_length<b->expected_length) return TRUE;
2463  //  if (a->expected_length>b->expected_length) return FALSE;
2464  int comp=pLmCmp(a->lcm_of_lm, b->lcm_of_lm);
2465  if (comp==1) return FALSE;
2466  if (-1==comp) return TRUE;
2467  if (a->i+a->j<b->i+b->j) return TRUE;
2468   if (a->i+a->j>b->i+b->j) return FALSE;
2469  if (a->i<b->i) return TRUE;
2470  if (a->i>b->i) return FALSE;
2471  return TRUE;
2472}
2473
2474static int tgb_pair_better_gen(const void* ap,const void* bp){
2475
2476  sorted_pair_node* a=*((sorted_pair_node**)ap);
2477  sorted_pair_node* b=*((sorted_pair_node**)bp);
2478  assume(a->i>a->j);
2479  assume(b->i>b->j);
2480  if (a->deg<b->deg) return -1;
2481  if (a->deg>b->deg) return 1;
2482
2483
2484//  if (a->expected_length<b->expected_length) return -1;
2485  // if (a->expected_length>b->expected_length) return 1;
2486 int comp=pLmCmp(a->lcm_of_lm, b->lcm_of_lm);
2487 
2488  if (comp==1) return 1;
2489  if (-1==comp) return -1;
2490  if (a->i+a->j<b->i+b->j) return -1;
2491   if (a->i+a->j>b->i+b->j) return 1;
2492  if (a->i<b->i) return -1;
2493   if (a->i>b->i) return 1;
2494  return 0;
2495}
2496
2497
2498static poly gcd_of_terms(poly p, ring r){
2499  int max_g_0=0;
2500  assume(p!=NULL);
2501  int i;
2502  poly m=pOne();
2503  poly t;
2504  for (i=pVariables; i; i--)
2505  {
2506      pSetExp(m,i, pGetExp(p,i));
2507      if (max_g_0==0)
2508  if (pGetExp(m,i)>0)
2509    max_g_0=i;
2510  }
2511 
2512  t=p->next;
2513  while (t!=NULL){
2514   
2515    if (max_g_0==0) break;
2516    for (i=max_g_0; i; i--)
2517    {
2518      pSetExp(m,i, si_min(pGetExp(t,i),pGetExp(m,i)));
2519      if (max_g_0==i)
2520  if (pGetExp(m,i)==0)
2521    max_g_0=0;
2522      if ((max_g_0==0) && (pGetExp(m,i)>0)){
2523  max_g_0=i;
2524      }
2525    }
2526    t=t->next;
2527  }
2528
2529  if (max_g_0>0)
2530    return m;
2531  pDelete(&m);
2532  return NULL;
2533}
2534static inline BOOLEAN pHasNotCFExtended(poly p1, poly p2, poly m)
2535{
2536
2537  if (pGetComp(p1) > 0 || pGetComp(p2) > 0)
2538    return FALSE;
2539  int i = 1;
2540  loop
2541  {
2542    if ((pGetExp(p1, i)-pGetExp(m,i) >0) && (pGetExp(p2, i) -pGetExp(m,i)> 0))   return FALSE;
2543    if (i == pVariables)                                return TRUE;
2544    i++;
2545  }
2546}
2547
2548
2549//for impl reasons may return false if the the normal product criterion matches
2550static inline BOOLEAN extended_product_criterion(poly p1, poly gcd1, poly p2, poly gcd2, slimgb_alg* c){
2551  if (c->nc)
2552    return FALSE;
2553  if(gcd1==NULL) return FALSE;
2554        if(gcd2==NULL) return FALSE;
2555        gcd1->next=gcd2; //may ordered incorrect
2556        poly m=gcd_of_terms(gcd1,c->r);
2557        gcd1->next=NULL;
2558        if (m==NULL) return FALSE;
2559
2560        BOOLEAN erg=pHasNotCFExtended(p1,p2,m);
2561        pDelete(&m);
2562        return erg;
2563}
2564static poly kBucketGcd(kBucket* b, ring r)
2565{
2566  int s=0;
2567  int i;
2568  poly m, n;
2569  BOOLEAN initialized=FALSE;
2570  for (i=MAX_BUCKET-1;i>=0;i--)
2571  { 
2572    if (b->buckets[i]!=NULL){
2573      if (!initialized){
2574  m=gcd_of_terms(b->buckets[i],r);
2575  initialized=TRUE;
2576  if (m==NULL) return NULL;
2577      }
2578      else
2579  {
2580    n=gcd_of_terms(b->buckets[i],r);
2581    if (n==NULL) {
2582      pDelete(&m);
2583      return NULL;   
2584    }
2585    n->next=m;
2586    poly t=gcd_of_terms(n,r);
2587    n->next=NULL;
2588    pDelete(&m);
2589    pDelete(&n);
2590    m=t;
2591    if (m==NULL) return NULL;
2592   
2593  }
2594    }
2595  }
2596  return m;
2597}
2598
2599
2600
2601
2602static inline int quality_of_pos_in_strat_S(int pos, slimgb_alg* c){
2603  if (c->strat->lenSw!=NULL) return c->strat->lenSw[pos];
2604  return c->strat->lenS[pos];
2605}
2606static inline int quality_of_pos_in_strat_S_mult_high(int pos, poly high, slimgb_alg* c)
2607  //meant only for nc
2608{
2609  poly m=pOne();
2610  pExpVectorDiff(m,high ,c->strat->S[pos]);
2611  poly product=nc_mm_Mult_p(m, pCopy(c->strat->S[pos]), c->r);
2612  int erg=pQuality(product,c);
2613  pDelete(&m);
2614  pDelete(&product);
2615  return erg;
2616}
2617
2618static void multi_reduction_lls_trick(red_object* los, int losl,slimgb_alg* c,find_erg & erg){
2619  erg.expand=NULL;
2620  BOOLEAN swap_roles; //from reduce_by, to_reduce_u if fromS
2621  if(erg.fromS){
2622    if(pLmEqual(c->strat->S[erg.reduce_by],los[erg.to_reduce_u].p))
2623    {
2624      int i;
2625      int quality_a=quality_of_pos_in_strat_S(erg.reduce_by,c);
2626      int best=erg.to_reduce_u+1;
2627/*
2628      for (i=erg.to_reduce_u;i>=erg.to_reduce_l;i--){
2629  int qc=los[i].guess_quality(c);
2630  if (qc<quality_a){
2631    best=i;
2632    quality_a=qc;
2633  }
2634      }
2635      if(best!=erg.to_reduce_u+1){*/
2636      int qc;
2637      best=find_best(los,erg.to_reduce_l,erg.to_reduce_u,qc,c);
2638      if(qc<quality_a){
2639  los[best].flatten();
2640  int b_pos=kBucketCanonicalize(los[best].bucket);
2641  los[best].p=los[best].bucket->buckets[b_pos];
2642  qc=pQuality(los[best].bucket->buckets[b_pos],c);
2643  if(qc<quality_a){
2644    red_object h=los[erg.to_reduce_u];
2645    los[erg.to_reduce_u]=los[best];
2646    los[best]=h;
2647    swap_roles=TRUE;
2648  }
2649  else
2650    swap_roles=FALSE;
2651      }
2652      else{
2653 
2654  swap_roles=FALSE;
2655      }
2656 
2657    }
2658      else
2659    {
2660      if (erg.to_reduce_u>erg.to_reduce_l){
2661
2662  int i;
2663  int quality_a=quality_of_pos_in_strat_S(erg.reduce_by,c);
2664  if (c->nc)
2665    quality_a=quality_of_pos_in_strat_S_mult_high(erg.reduce_by, los[erg.to_reduce_u].p, c);
2666  int best=erg.to_reduce_u+1;
2667  int qc;
2668  best=find_best(los,erg.to_reduce_l,erg.to_reduce_u,qc,c);
2669  assume(qc==los[best].guess_quality(c));
2670  if(qc<quality_a){
2671    los[best].flatten();
2672    int b_pos=kBucketCanonicalize(los[best].bucket);
2673    los[best].p=los[best].bucket->buckets[b_pos];
2674    qc==pQuality(los[best].bucket->buckets[b_pos],c);
2675    //(best!=erg.to_reduce_u+1)
2676    if(qc<quality_a){
2677    red_object h=los[erg.to_reduce_u];
2678    los[erg.to_reduce_u]=los[best];
2679    los[best]=h;
2680    erg.reduce_by=erg.to_reduce_u;
2681    erg.fromS=FALSE;
2682    erg.to_reduce_u--;
2683    }
2684  }
2685      }
2686      else 
2687      {
2688  assume(erg.to_reduce_u==erg.to_reduce_l);
2689  wlen_type quality_a=
2690        quality_of_pos_in_strat_S(erg.reduce_by,c);
2691  wlen_type qc=los[erg.to_reduce_u].guess_quality(c);
2692  if (qc<0) PrintS("Wrong wlen_type");
2693  if(qc<quality_a){
2694    int best=erg.to_reduce_u;
2695    los[best].flatten();
2696    int b_pos=kBucketCanonicalize(los[best].bucket);
2697    los[best].p=los[best].bucket->buckets[b_pos];
2698    qc=pQuality(los[best].bucket->buckets[b_pos],c);
2699    assume(qc>=0);
2700    if(qc<quality_a){
2701      BOOLEAN exp=FALSE;
2702      if(qc<=2){
2703         //Print("\n qc is %lld \n",qc);
2704         exp=TRUE;
2705      }
2706       
2707      else {
2708         if (qc<quality_a/2)
2709          exp=TRUE;
2710         else
2711       if(erg.reduce_by<c->n/4)
2712          exp=TRUE;
2713      }
2714      if (exp){
2715        poly clear_into;
2716        los[erg.to_reduce_u].flatten();
2717        kBucketClear(los[erg.to_reduce_u].bucket,&clear_into,&erg.expand_length);
2718        erg.expand=pCopy(clear_into);
2719        kBucketInit(los[erg.to_reduce_u].bucket,clear_into,erg.expand_length);
2720        if (TEST_OPT_PROT)
2721    PrintS("e");
2722       
2723      }
2724    }
2725  }
2726
2727 
2728      }
2729     
2730      swap_roles=FALSE;
2731      return;
2732      }
2733   
2734  }
2735  else{
2736    if(erg.reduce_by>erg.to_reduce_u){
2737      //then lm(rb)>= lm(tru) so =
2738      assume(erg.reduce_by==erg.to_reduce_u+1);
2739      int best=erg.reduce_by;
2740      wlen_type quality_a=los[erg.reduce_by].guess_quality(c);
2741      int qc;
2742      best=find_best(los,erg.to_reduce_l,erg.to_reduce_u,qc,c);
2743     
2744      int i;
2745      if(qc<quality_a){
2746    red_object h=los[erg.reduce_by];
2747    los[erg.reduce_by]=los[best];
2748    los[best]=h;
2749  }
2750  swap_roles=FALSE;
2751  return;
2752 
2753   
2754    }
2755    else
2756    {
2757      assume(!pLmEqual(los[erg.reduce_by].p,los[erg.to_reduce_l].p));
2758      assume(erg.to_reduce_u==erg.to_reduce_l);
2759      //further assume, that reduce_by is the above all other polys
2760      //with same leading term
2761      int il=erg.reduce_by;
2762      int quality_a =los[erg.reduce_by].guess_quality(c);
2763      int qc;
2764      while((il>0) && pLmEqual(los[il-1].p,los[il].p)){
2765  il--;
2766  qc=los[il].guess_quality(c);
2767  if (qc<quality_a){
2768    quality_a=qc;
2769    erg.reduce_by=il;
2770  }
2771      }
2772      swap_roles=FALSE;
2773    }
2774 
2775  }
2776  if(swap_roles)
2777  {
2778    if (TEST_OPT_PROT)
2779      PrintS("b");
2780    poly clear_into;
2781    int dummy_len;
2782    int new_length;
2783    int bp=erg.to_reduce_u;//bucket_positon
2784    //kBucketClear(los[bp].bucket,&clear_into,&new_length);
2785    new_length=los[bp].clear_to_poly();
2786    clear_into=los[bp].p;
2787    poly p=c->strat->S[erg.reduce_by];
2788    int j=erg.reduce_by;
2789    int old_length=c->strat->lenS[j];// in view of S
2790    los[bp].p=p;
2791    kBucketInit(los[bp].bucket,p,old_length);
2792    wlen_type qal=pQuality(clear_into,c,new_length);
2793    int pos_in_c=-1;   
2794    int z;
2795    int new_pos;
2796    new_pos=simple_posInS(c->strat,clear_into,new_length, qal);
2797    assume(new_pos<=j);
2798    for (z=c->n;z;z--)
2799    {
2800      if(p==c->S->m[z-1])
2801      {
2802  pos_in_c=z-1;
2803  break;
2804      }
2805    }
2806    if(pos_in_c>=0)
2807    {
2808      #ifdef TGB_RESORT_PAIRS
2809      c->used_b=TRUE;
2810      c->replaced[pos_in_c]=TRUE;
2811      #endif
2812      c->S->m[pos_in_c]=clear_into;
2813      c->lengths[pos_in_c]=new_length;
2814      c->weighted_lengths[pos_in_c]=qal;
2815      if (c->gcd_of_terms[pos_in_c]==NULL)
2816        c->gcd_of_terms[pos_in_c]=gcd_of_terms(clear_into,c->r);
2817      if (c->T_deg_full)
2818  c->T_deg_full[pos_in_c]=pTotaldegree_full(clear_into);
2819      c_S_element_changed_hook(pos_in_c,c);
2820    }
2821    c->strat->S[j]=clear_into;
2822    c->strat->lenS[j]=new_length;
2823   
2824    assume(pLength(clear_into)==new_length);
2825    if(c->strat->lenSw!=NULL)
2826      c->strat->lenSw[j]=qal;
2827    if (!rField_is_Zp(c->r))
2828    {
2829      pContent(clear_into);
2830      pCleardenom(clear_into);//should be unnecessary
2831    }
2832    else                     
2833      pNorm(clear_into);
2834#ifdef FIND_DETERMINISTIC
2835    erg.reduce_by=j;
2836    //resort later see diploma thesis, find_in_S must be deterministic
2837    //during multireduction if spolys are only in the span of the
2838    //input polys
2839#else
2840   
2841    if (new_pos<j)
2842    { 
2843      move_forward_in_S(j,new_pos,c->strat);
2844      erg.reduce_by=new_pos;
2845    }
2846#endif
2847  }
2848}
2849static int fwbw(red_object* los, int i){
2850   int i2=i;
2851   int step=1;
2852   
2853   BOOLEAN bw=FALSE;
2854   BOOLEAN incr=TRUE;
2855   
2856   while(1)
2857   {
2858     if(!bw)
2859     {
2860       step=si_min(i2,step);
2861       if (step==0) break;
2862       i2-=step;
2863   
2864       if(!pLmEqual(los[i].p,los[i2].p))
2865       {
2866   bw=TRUE;
2867   incr=FALSE;
2868       }
2869       else
2870       {
2871   if ((!incr) &&(step==1)) break;
2872       }
2873       
2874       
2875     }
2876     else
2877     {
2878       
2879       step=si_min(i-i2,step);
2880       if (step==0) break;
2881       i2+=step;
2882       if(pLmEqual(los[i].p,los[i2].p)){
2883   if(step==1) break;
2884   else
2885   {
2886     bw=FALSE;
2887   }
2888       }
2889       
2890     }
2891     if (incr)
2892       step*=2;
2893     else
2894     {
2895       if (step%2==1)
2896   step=(step+1)/2;
2897       else
2898   step/=2;
2899       
2900     }
2901   }
2902   return i2;
2903}
2904static void canonicalize_region(red_object* los, int l, int u,slimgb_alg* c){
2905    assume(l<=u+1);
2906    int i;
2907    for(i=l;i<=u;i++){
2908        kBucketCanonicalize(los[i].bucket);
2909    }
2910
2911}
2912static void multi_reduction_find(red_object* los, int losl,slimgb_alg* c,int startf,find_erg & erg){
2913  kStrategy strat=c->strat;
2914
2915  assume(startf<=losl);
2916  assume((startf==losl-1)||(pLmCmp(los[startf].p,los[startf+1].p)==-1));
2917  int i=startf;
2918 
2919  int j;
2920  while(i>=0){
2921    assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)<=0));
2922    assume(is_valid_ro(los[i]));
2923    j=kFindDivisibleByInS_easy(strat,los[i]);
2924    if(j>=0){
2925     
2926      erg.to_reduce_u=i;
2927      erg.reduce_by=j;
2928      erg.fromS=TRUE;
2929      int i2=fwbw(los,i);
2930      assume(pLmEqual(los[i].p,los[i2].p));
2931      assume((i2==0)||(!pLmEqual(los[i2].p,los[i2-1].p)));
2932      assume(i>=i2);
2933
2934
2935      erg.to_reduce_l=i2;
2936      assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)==-1));
2937      canonicalize_region(los,erg.to_reduce_u+1,startf,c);
2938      return;
2939    }
2940    if (j<0){
2941     
2942      //not reduceable, try to use this for reducing higher terms
2943      int i2=fwbw(los,i);
2944      assume(pLmEqual(los[i].p,los[i2].p));
2945      assume((i2==0)||(!pLmEqual(los[i2].p,los[i2-1].p)));
2946      assume(i>=i2);
2947      if(i2!=i){
2948 
2949 
2950  erg.to_reduce_u=i-1;
2951  erg.to_reduce_l=i2;
2952  erg.reduce_by=i;
2953  erg.fromS=FALSE;
2954  assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)==-1));
2955  canonicalize_region(los,erg.to_reduce_u+1,startf,c);
2956  return;
2957      }
2958      if((!(c->is_homog)) &&(!(c->doubleSugar)))
2959      {
2960
2961  for (i2=i+1;i2<losl;i2++){
2962    if (p_LmShortDivisibleBy(los[i].p,los[i].sev,los[i2].p,~los[i2].sev,
2963           c->r)){
2964      int i3=i2;
2965      while((i3+1<losl) && (pLmEqual(los[i2].p, los[i3+1].p)))
2966        i3++;
2967      erg.to_reduce_u=i3;
2968      erg.to_reduce_l=i2;
2969      erg.reduce_by=i;
2970      erg.fromS=FALSE;
2971      assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)==-1));
2972      canonicalize_region(los,erg.to_reduce_u+1,startf,c);
2973      return;
2974    }
2975    //  else {assume(!p_LmDivisibleBy(los[i].p, los[i2].p,c->r));}
2976  }
2977      }
2978      i--;
2979    }
2980  }
2981  erg.reduce_by=-1;//error code
2982  return;
2983}
2984
2985 //  nicht reduzierbare eintraege in ergebnisliste schreiben
2986//   nullen loeschen
2987//   while(finde_groessten leitterm reduzierbar(c,erg)){
2988 
2989static int multi_reduction_clear_zeroes(red_object* los, int  losl, int l, int u)
2990{
2991
2992
2993  int deleted=0;
2994  int  i=l;
2995  int last=-1;
2996  while(i<=u)
2997  {
2998   
2999    if(los[i].p==NULL){
3000      kBucketDestroy(&los[i].bucket);
3001//      delete los[i];//here we assume los are constructed with new
3002      //destroy resources, must be added here   
3003     if (last>=0)
3004     {
3005       memmove(los+(int)(last+1-deleted),los+(last+1),sizeof(red_object)*(i-1-last));
3006     }
3007     last=i;
3008     deleted++;
3009    }
3010    i++;
3011  }
3012  if((last>=0)&&(last!=losl-1))
3013      memmove(los+(int)(last+1-deleted),los+last+1,sizeof(red_object)*(losl-1-last));
3014  return deleted;
3015 
3016}
3017
3018static void sort_region_down(red_object* los, int l, int u, slimgb_alg* c)
3019{
3020  qsort(los+l,u-l+1,sizeof(red_object),red_object_better_gen);
3021  int i;
3022
3023  for(i=l;i<=u;i++)
3024  {
3025    BOOLEAN moved=FALSE;
3026    int j;
3027    for(j=i;j;j--)
3028    {
3029      if(pLmCmp(los[j].p,los[j-1].p)==-1){
3030  red_object h=los[j];
3031  los[j]=los[j-1];
3032  los[j-1]=h;
3033  moved=TRUE;
3034      }
3035      else break;
3036    }
3037    if(!moved) return;
3038  }
3039}
3040
3041//assume that los is ordered ascending by leading term, all non zero
3042static void multi_reduction(red_object* los, int & losl, slimgb_alg* c)
3043{
3044  poly* delay=(poly*) omalloc(losl*sizeof(poly));
3045  int delay_s=0;
3046  //initialize;
3047  assume(c->strat->sl>=0);
3048  assume(losl>0);
3049  int i;
3050  wlen_type max_initial_quality=0;
3051 
3052  for(i=0;i<losl;i++){
3053    los[i].sev=pGetShortExpVector(los[i].p);
3054//SetShortExpVector();
3055    los[i].p=kBucketGetLm(los[i].bucket);
3056    if (los[i].initial_quality>max_initial_quality)
3057        max_initial_quality=los[i].initial_quality;
3058    // else
3059//         Print("init2_qal=%lld;", los[i].initial_quality);
3060//     Print("initial_quality=%lld;",max_initial_quality);
3061  }
3062
3063  kStrategy strat=c->strat;
3064  int curr_pos=losl-1;
3065
3066
3067//  nicht reduzierbare einträge in ergebnisliste schreiben
3068  // nullen loeschen
3069  while(curr_pos>=0){
3070   
3071    find_erg erg;
3072    multi_reduction_find(los, losl,c,curr_pos,erg);//last argument should be curr_pos
3073    if(erg.reduce_by<0) break;
3074
3075
3076
3077    erg.expand=NULL;
3078    int d=erg.to_reduce_u-erg.to_reduce_l+1;
3079   
3080   
3081    multi_reduction_lls_trick(los,losl,c,erg);
3082   
3083   
3084    int i;
3085    int len;
3086    //    wrp(los[erg.to_reduce_u].p);
3087    //Print("\n");
3088    multi_reduce_step(erg,los,c);
3089   
3090
3091    if(!K_TEST_OPT_REDTHROUGH){
3092  for(i=erg.to_reduce_l;i<=erg.to_reduce_u;i++){
3093     if  (los[i].p!=NULL)  //the check (los[i].p!=NULL) might be invalid
3094     {
3095         //
3096         assume(los[i].initial_quality>0);
3097         
3098               if(los[i].guess_quality(c)
3099                  >1.5*delay_factor*max_initial_quality){
3100                       if (TEST_OPT_PROT)
3101                           PrintS("v");
3102                       los[i].canonicalize();
3103                       if(los[i].guess_quality(c)
3104                           >delay_factor*max_initial_quality){
3105                               if (TEST_OPT_PROT)
3106                                   PrintS(".");
3107                               los[i].clear_to_poly();
3108                               //delay.push_back(los[i].p);
3109                               delay[delay_s]=los[i].p;
3110                               delay_s++;
3111
3112                               los[i].p=NULL;
3113                     
3114                      }
3115                  }
3116           
3117            }
3118     }
3119  }
3120    int deleted=multi_reduction_clear_zeroes(los, losl, erg.to_reduce_l, erg.to_reduce_u);
3121    if(erg.fromS==FALSE)
3122      curr_pos=si_max(erg.to_reduce_u,erg.reduce_by);
3123    else
3124      curr_pos=erg.to_reduce_u;
3125    losl -= deleted;
3126    curr_pos -= deleted;
3127
3128    //Print("deleted %i \n",deleted);
3129    if ((TEST_V_UPTORADICAL) &&(!(erg.fromS)))
3130        sort_region_down(los,si_min(erg.to_reduce_l,erg.reduce_by),(si_max(erg.to_reduce_u,erg.reduce_by))-deleted,c);
3131    else   
3132    sort_region_down(los, erg.to_reduce_l, erg.to_reduce_u-deleted, c);
3133
3134
3135    if(erg.expand)
3136    {
3137#ifdef FIND_DETERMINISTIC
3138      int i;
3139      for(i=0;c->expandS[i];i++);
3140      c->expandS=(poly*) omrealloc(c->expandS,(i+2)*sizeof(poly));
3141      c->expandS[i]=erg.expand;
3142      c->expandS[i+1]=NULL;
3143#else
3144      add_to_reductors(c,erg.expand,erg.expand_length);
3145#endif
3146    }
3147     
3148  }
3149 
3150
3151  sorted_pair_node** pairs=(sorted_pair_node**)
3152    omalloc(delay_s*sizeof(sorted_pair_node*)); 
3153  for(i=0;i<delay_s;i++){
3154       
3155      poly p=delay[i];
3156      //if (rPar(c->r)==0)
3157      simplify_poly(p,c->r);
3158      sorted_pair_node* si=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
3159      si->i=-1;
3160      si->j=-1;
3161       if (!rField_is_Zp(c->r)){
3162        if (!c->nc)
3163            p=redTailShort(p, c->strat);
3164        pCleardenom(p);
3165        pContent(p);
3166      }
3167      si->expected_length=pQuality(p,c,pLength(p));
3168      si->deg=pTotaldegree(p);
3169     
3170      si->lcm_of_lm=p;
3171      pairs[i]=si;
3172  }
3173  qsort(pairs,delay_s,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
3174  c->apairs=spn_merge(c->apairs,c->pair_top+1,pairs,delay_s,c);
3175  c->pair_top+=delay_s;
3176  omfree(delay);
3177  return;
3178}
3179void red_object::flatten(){
3180  assume(p==kBucketGetLm(bucket));
3181}
3182void red_object::validate(){
3183  p=kBucketGetLm(bucket);
3184  if(p)
3185    sev=pGetShortExpVector(p);
3186}
3187int red_object::clear_to_poly(){
3188  flatten();
3189  int l;
3190  kBucketClear(bucket,&p,&l);
3191  return l;
3192}
3193
3194 
3195
3196
3197
3198void reduction_step::reduce(red_object* r, int l, int u){}
3199void simple_reducer::do_reduce(red_object & ro){
3200  number coef;
3201  if (!c->nc)
3202    coef=kBucketPolyRed(ro.bucket,p,
3203       p_len,
3204       c->strat->kNoether);
3205  else
3206    nc_kBucketPolyRed_Z(ro.bucket, p, &coef);
3207  nDelete(&coef);
3208}
3209
3210
3211void simple_reducer::reduce(red_object* r, int l, int u){
3212  this->pre_reduce(r,l,u);
3213  int i;
3214//debug start
3215  int im;
3216
3217
3218  for(i=l;i<=u;i++){
3219 
3220
3221
3222    this->do_reduce(r[i]);
3223 
3224  }
3225  for(i=l;i<=u;i++){
3226 
3227    kBucketSimpleContent(r[i].bucket);
3228    r[i].validate();
3229    #ifdef TGB_DEBUG
3230    #endif
3231  }
3232}
3233reduction_step::~reduction_step(){}
3234simple_reducer::~simple_reducer(){
3235  if(fill_back!=NULL)
3236  {
3237    kBucketInit(fill_back,p,p_len);
3238  }
3239  fill_back=NULL;
3240   
3241}
3242 
3243void multi_reduce_step(find_erg & erg, red_object* r, slimgb_alg* c){
3244  static int id=0;
3245  id++;
3246  unsigned long sev;
3247    BOOLEAN lt_changed=FALSE;
3248  int rn=erg.reduce_by;
3249  poly red;
3250  int red_len;
3251  simple_reducer* pointer;
3252  BOOLEAN work_on_copy=FALSE;
3253  if(erg.fromS){
3254    red=c->strat->S[rn];
3255    red_len=c->strat->lenS[rn];
3256    assume(red_len==pLength(red));
3257  }
3258  else
3259  {
3260    r[rn].flatten();
3261    kBucketClear(r[rn].bucket,&red,&red_len);
3262   
3263    if (!rField_is_Zp(c->r))
3264    {
3265      pContent(red);
3266      pCleardenom(red);//should be unnecessary
3267     
3268    }
3269    pNormalize(red);
3270   
3271
3272    if ((!(erg.fromS))&&(TEST_V_UPTORADICAL)){
3273         
3274         if (polynomial_root(red,c->r))
3275            lt_changed=TRUE;
3276            sev=p_GetShortExpVector(red,c->r);}
3277    red_len=pLength(red);
3278  }
3279  if (((TEST_V_MODPSOLVSB)&&(red_len>1))||((c->nc)||(erg.to_reduce_u-erg.to_reduce_l>5))){
3280    work_on_copy=TRUE;
3281    // poly m=pOne();
3282    poly m=c->tmp_lm;
3283    pSetCoeff(m,nInit(1));
3284    for(int i=1;i<=pVariables;i++)
3285      pSetExp(m,i,(pGetExp(r[erg.to_reduce_l].p, i)-pGetExp(red,i)));
3286    pSetm(m);
3287    poly red_cp;
3288    if (!c->nc)
3289      red_cp=ppMult_mm(red,m);
3290    else
3291      red_cp=nc_mm_Mult_p(m, pCopy(red), c->r);
3292    if(!erg.fromS){
3293      kBucketInit(r[rn].bucket,red,red_len);
3294    }
3295    //now reduce the copy
3296    //static poly redNF2 (poly h,slimgb_alg* c , int &len, number&  m,int n)
3297
3298    if (!c->nc)
3299      redTailShort(red_cp,c->strat);
3300    //number mul;
3301    // red_len--;
3302//     red_cp->next=redNF2(red_cp->next,c,red_len,mul,c->average_length);
3303//     pSetCoeff(red_cp,nMult(red_cp->coef,mul));
3304//     nDelete(&mul);
3305//     red_len++;
3306    red=red_cp;
3307    red_len=pLength(red);
3308    // pDelete(&m);
3309   
3310  }
3311  int i;
3312
3313
3314
3315  assume(red_len==pLength(red));
3316 
3317  pointer=new simple_reducer(red,red_len,c);
3318
3319  if ((!work_on_copy) && (!erg.fromS))
3320    pointer->fill_back=r[rn].bucket;
3321  else
3322    pointer->fill_back=NULL;
3323  pointer->reduction_id=id;
3324  pointer->c=c;
3325
3326  pointer->reduce(r,erg.to_reduce_l, erg.to_reduce_u);
3327  if(work_on_copy) pDelete(&pointer->p);
3328  delete pointer;
3329  if (lt_changed){
3330    assume(!erg.fromS);
3331    r[erg.reduce_by].sev=sev;
3332  }
3333 
3334};
3335
3336
3337
3338 
3339void simple_reducer:: pre_reduce(red_object* r, int l, int u){}
3340
Note: See TracBrowser for help on using the repository browser.