source: git/Singular/tgb.cc @ 666c90

spielwiese
Last change on this file since 666c90 was 08db0a, checked in by Hans Schönemann <hannes@…>, 19 years ago
*hannes: gcc-3.4 git-svn-id: file:///usr/local/Singular/svn/trunk@7734 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 115.8 KB
Line 
1
2// #define OM_CHECK 3
3// #define OM_TRACK 5
4// #define OM_KEEP  1
5// TODO:
6//       deg -> poly_crit
7//       multiple rings
8//       shorten_tails und dessen Aufrufe pruefen wlength!!!
9//       calculating with formal sums
10//       try to create spolys as formal sums
11
12#include "tgb.h"
13static const int bundle_size=100;
14#if 1
15static omBin lm_bin=NULL;
16static inline poly p_Init_Special(const ring r)
17{
18  return p_Init(r,lm_bin);
19}
20static inline poly pOne_Special(const ring r=currRing)
21{
22  poly rc = p_Init_Special(r);
23  pSetCoeff0(rc,r->cf->nInit(1));
24  return rc;
25}
26// zum Initialiseren: in t_rep_gb plazieren:
27
28#endif
29#define LEN_VAR3
30#define degbound(p) assume(pTotaldegree(p)<10)
31//#define inDebug(p) assume((debug_Ideal==NULL)||(kNF(debug_Ideal,NULL,p,0,0)==0))
32
33//die meisten Varianten stossen sich an coef_buckets
34#ifdef LEN_VAR1
35// erste Variante: Laenge: Anzahl der Monome
36inline int pSLength(poly p, int l) {
37  return l; }
38inline int kSBucketLength(kBucket* bucket, poly lm) {return bucket_guess(bucket);}
39#endif
40
41#ifdef LEN_VAR2
42// 2. Variante: Laenge: Platz fuer die Koeff.
43int pSLength(poly p,int l)
44{
45  int s=0;
46  while (p!=NULL) { s+=nSize(pGetCoeff(p));pIter(p); }
47  return s;
48}
49int kSBucketLength(kBucket* b, poly lm)
50{
51  int s=0;
52  int i;
53  for (i=MAX_BUCKET;i>=0;i--)
54  {
55    s+=pSLength(b->buckets[i],0);
56  }
57  return s;
58}
59#endif
60
61#ifdef LEN_VAR3
62// 3.Variante: Laenge: Platz fuer Leitk * Monomanzahl
63inline int pSLength(poly p,int l)
64{
65  int c=nSize(pGetCoeff(p));
66  return c*l /*pLength(p)*/;
67}
68int kSBucketLength(kBucket* b, poly lm=NULL)
69{
70  int s=0;
71  int c;
72  if(lm==NULL)
73    c=nSize(pGetCoeff(kBucketGetLm(b)));
74  else
75    c=nSize(pGetCoeff(lm));
76  int i;
77  for (i=b->buckets_used;i>=0;i--)
78  {
79    assume((b->buckets_length[i]==0)||(b->buckets[i]!=NULL));
80    s+=b->buckets_length[i] /*pLength(b->buckets[i])*/;
81  }
82  return s*c;
83}
84#endif
85
86#ifdef LEN_VAR4
87// 4.Variante: Laenge: Platz fuer Leitk * (1+Platz fuer andere Koeff.)
88int pSLength(poly p, int l)
89{
90  int s=1;
91  int c=nSize(pGetCoeff(p));
92  pIter(p);
93  while (p!=NULL) { s+=nSize(pGetCoeff(p));pIter(p); }
94  return s*c;
95}
96int kSBucketLength(kBucket* b)
97{
98  int s=1;
99  int c=nSize(pGetCoeff(kBucketGetLm(b)));
100  int i;
101  for (i=MAX_BUCKET;i>0;i--)
102  {
103    if(b->buckets[i]==NULL) continue;
104    s+=pSLength(b->buckets[i],0);
105  }
106  return s*c;
107}
108#endif
109static int do_pELength(poly p, calc_dat* c, int dlm=-1){
110  if(p==NULL) return 0;
111  int s=0;
112  poly pi=p;
113  if(dlm<0){
114    dlm=pTotaldegree(p,c->r);
115    s=1;
116    pi=p->next;
117  }
118 
119  while(pi){
120    int d=pTotaldegree(pi,c->r);
121    if(d>dlm)
122      s+=1+d-dlm;
123    else
124      ++s;
125    pi=pi->next;
126  }
127  return s;
128}
129int kEBucketLength(kBucket* b, poly lm,calc_dat* ca)
130{
131  int s=0;
132  if(lm==NULL){
133    lm=kBucketGetLm(b);
134  }
135  if(lm==NULL) return 0;
136  int d=pTotaldegree(lm,ca->r);
137  int i;
138  for (i=b->buckets_used;i>=0;i--)
139  {
140    if(b->buckets[i]==NULL) continue;
141    s+=do_pELength(b->buckets[i],ca,d);
142  }
143  return s;
144}
145
146static inline int pELength(poly p, calc_dat* c,int l){
147  if (p==NULL) return 0;
148  return do_pELength(p,c);
149}
150// static int quality(poly p, int len, calc_dat* c){
151//   if (c->is_char0) return pSLength(p,len);
152//   return pLength(p);
153// }
154static inline int pQuality(poly p, calc_dat* c, int l=-1){
155 
156  if(l<0)
157    l=pLength(p);
158  if(c->is_char0) return pSLength(p,l);
159  if((pLexOrder) &&(!c->is_homog)) return pELength(p,c,l);
160  return l;
161}
162static inline int pTotaldegree_full(poly p){
163  int r=0;
164  while(p){
165    int d=pTotaldegree(p);
166    r=max(r,d);
167    pIter(p);
168  }
169  return r;
170}
171
172int red_object::guess_quality(calc_dat* c){
173    //works at the moment only for lenvar 1, because in different
174    //case, you have to look on coefs
175    int s=0;
176    if (c->is_char0)
177      s=kSBucketLength(bucket,this->p);
178    else 
179    {
180      if((pLexOrder) &&(!c->is_homog)) 
181        s=kEBucketLength(this->bucket,this->p,c);
182      else s=bucket_guess(bucket);
183    }
184    if (sum!=NULL){
185      if (c->is_char0)
186        s+=kSBucketLength(sum->ac->bucket);
187      else
188      {
189        if((pLexOrder) &&(!c->is_homog)) 
190          s=kEBucketLength(sum->ac->bucket,this->p,c);
191        else
192          s+=bucket_guess(sum->ac->bucket);
193      }
194    }
195    return s;
196}
197// static int guess_quality(const red_object & p, calc_dat* c){
198//   //looks only on bucket
199//   if (c->is_char0) return kSBucketLength(p.bucket,p.p);
200//   if((pLexOrder) &&(!c->is_homog)) return kEBucketLength(p.bucket,p.p,c);
201//   return (bucket_guess(p.bucket));
202// }
203
204static void finalize_reduction_step(reduction_step* r){
205  delete r;
206}
207static int LObject_better_gen(const void* ap, const void* bp)
208{
209  LObject* a=*(LObject**)ap;
210  LObject* b=*(LObject**)bp;
211  return(pLmCmp(a->p,b->p));
212}
213static int red_object_better_gen(const void* ap, const void* bp)
214{
215
216
217  return(pLmCmp(((red_object*) ap)->p,((red_object*) bp)->p));
218}
219static int monom_poly_crit(const void* ap1, const void* ap2){
220  monom_poly* p1;
221  monom_poly* p2;
222  p1=((monom_poly*) ap1);
223  p2=((monom_poly*)ap2);
224  if(((unsigned long) p1->f)>((unsigned long) p2->f)) return 1;
225  if(((unsigned long) p1->f)<((unsigned long)p2->f)) return -1; 
226
227  return pLmCmp(p1->m,p2->m);
228 
229}
230static int pLmCmp_func(const void* ap1, const void* ap2){
231    poly p1,p2;
232  p1=*((poly*) ap1);
233  p2=*((poly*)ap2);
234
235  return pLmCmp(p1,p2);
236}
237static int pLmCmp_func_inverted(const void* ap1, const void* ap2){
238    poly p1,p2;
239  p1=*((poly*) ap1);
240  p2=*((poly*)ap2);
241
242  return -pLmCmp(p1,p2);
243}
244
245static int pair_better_gen2(const void* ap,const void* bp){
246  return(-pair_better_gen(ap,bp));
247}
248static int kFindDivisibleByInS_easy(kStrategy strat,const red_object & obj){
249  int i;
250  long not_sev=~obj.sev;
251  poly p=obj.p;
252  for(i=0;i<=strat->sl;i++){
253    if (pLmShortDivisibleBy(strat->S[i],strat->sevS[i],p,not_sev))
254      return i;
255  }
256  return -1;
257}
258static int kFindDivisibleByInS_easy(kStrategy strat,poly p, long sev){
259  int i;
260  long not_sev=~sev;
261  for(i=0;i<=strat->sl;i++){
262    if (pLmShortDivisibleBy(strat->S[i],strat->sevS[i],p,not_sev))
263      return i;
264  }
265  return -1;
266}
267static int posInPairs (sorted_pair_node**  p, int pn, sorted_pair_node* qe,calc_dat* c,int an=0)
268{
269  if(pn==0) return 0;
270
271  int length=pn-1;
272  int i;
273  //int an = 0;
274  int en= length;
275
276  if (pair_better(qe,p[en],c))
277    return length+1;
278
279  while(1)
280    {
281      //if (an >= en-1)
282      if(en-1<=an)
283      {
284        if (pair_better(p[an],qe,c)) return an;
285        return en;
286      }
287      i=(an+en) / 2;
288        if (pair_better(p[i],qe,c))
289          en=i;
290      else an=i;
291    }
292}
293static int posInPolys (poly*  p, int pn, poly qe,calc_dat* c)
294{
295  if(pn==0) return 0;
296
297  int length=pn-1;
298  int i;
299  int an = 0;
300  int en= length;
301
302  if (pLmCmp(qe,p[en])==1)
303    return length+1;
304
305  while(1)
306    {
307      //if (an >= en-1)
308      if(en-1<=an)
309      {
310        if (pLmCmp(p[an],qe)==1) return an;
311        return en;
312      }
313      i=(an+en) / 2;
314        if (pLmCmp(p[i],qe)==1)
315          en=i;
316      else an=i;
317    }
318}
319static int posInMonomPolys (monom_poly*  p, int pn, monom_poly & qe,calc_dat* c)
320{
321  if(pn==0) return 0;
322
323  int length=pn-1;
324  int i;
325  int an = 0;
326  int en= length;
327
328  if (monom_poly_crit(&qe,&p[en])==1)
329    return length+1;
330
331  while(1)
332    {
333      //if (an >= en-1)
334      if(en-1<=an)
335      {
336        if (monom_poly_crit(&p[an],&qe)==1) return an;
337        return en;
338      }
339      i=(an+en) / 2;
340        if (monom_poly_crit(&p[i],&qe)==1)
341          en=i;
342      else an=i;
343    }
344}
345static BOOLEAN  ascending(int* i,int top){
346  if(top<1) return TRUE;
347  if(i[top]<i[top-1]) return FALSE;
348  return ascending(i,top-1);
349}
350
351sorted_pair_node**  merge(sorted_pair_node** p, int pn,sorted_pair_node **q, int qn,calc_dat* c){
352  int i;
353  int* a= (int*) omalloc(qn*sizeof(int));
354//   int mc;
355//   PrintS("Debug\n");
356//   for(mc=0;mc<qn;mc++)
357// {
358
359//     wrp(q[mc]->lcm_of_lm);
360//     PrintS("\n");
361// }
362//    PrintS("Debug they are in\n");
363//   for(mc=0;mc<pn;mc++)
364// {
365
366//     wrp(p[mc]->lcm_of_lm);
367//     PrintS("\n");
368// }
369  int lastpos=0;
370  for(i=0;i<qn;i++){
371    lastpos=posInPairs(p,pn,q[i],c, max(lastpos-1,0));
372    //   cout<<lastpos<<"\n";
373    a[i]=lastpos;
374
375  }
376  if((pn+qn)>c->max_pairs){
377    p=(sorted_pair_node**) omrealloc(p,2*(pn+qn)*sizeof(sorted_pair_node*));
378    c->max_pairs=2*(pn+qn);
379  }
380  for(i=qn-1;i>=0;i--){
381    size_t size;
382    if(qn-1>i)
383      size=(a[i+1]-a[i])*sizeof(sorted_pair_node*);
384    else
385      size=(pn-a[i])*sizeof(sorted_pair_node*); //as indices begin with 0
386    memmove (p+a[i]+(1+i), p+a[i], size);
387    p[a[i]+i]=q[i];
388  }
389  omfree(a);
390  return p;
391}
392
393
394static BOOLEAN trivial_syzygie(int pos1,int pos2,poly bound,calc_dat* c){
395
396
397  poly p1=c->S->m[pos1];
398  poly p2=c->S->m[pos2];
399  ring r=c->r;
400 
401
402  if (pGetComp(p1) > 0 || pGetComp(p2) > 0)
403    return FALSE;
404  int i = 1;
405  poly m=NULL;
406  poly gcd1=c->gcd_of_terms[pos1];
407  poly gcd2=c->gcd_of_terms[pos2];
408 
409  if((gcd1!=NULL) && (gcd2!=NULL)) 
410    {
411      gcd1->next=gcd2; //may ordered incorrect
412      poly m=gcd_of_terms(gcd1,c->r);
413      gcd1->next=NULL;
414     
415    } 
416
417  if (m==NULL) 
418  {
419     loop
420      {
421        if (pGetExp(p1, i)+ pGetExp(p2, i) > pGetExp(bound,i))   return FALSE;
422        if (i == pVariables){
423          //PrintS("trivial");
424          return TRUE;
425        }
426        i++;
427      }
428  }
429  else 
430  {
431    loop
432      {
433        if (pGetExp(p1, i)-pGetExp(m,i) + pGetExp(p2, i) > pGetExp(bound,i))   return FALSE;
434        if (i == pVariables){
435          pDelete(&m);
436          //PrintS("trivial");
437          return TRUE;
438        }
439        i++;
440      }
441  }
442
443 
444
445 
446}
447
448///returns position sets w as weight
449int find_best(red_object* r,int l, int u, int &w, calc_dat* c){
450  int best=l;
451  int i;
452  w=r[l].guess_quality(c);
453  for(i=l+1;i<=u;i++){
454    int w2=r[i].guess_quality(c);
455    if(w2<w){
456      w=w2;
457      best=i;
458    }
459   
460  }
461 return best;
462}
463
464#if 0 
465int find_best(red_object* r,int l, int u, int &w, calc_dat* c){
466
467  int sz=u-l+1;
468  int n=sz/10+1;
469  int filled=0;
470  int* indizes=(int*) omalloc(n*sizeof(int));
471  int* weight=(int*) omalloc(n*sizeof(int));
472  int worst=-1;
473  int i; 
474  for(i=l;i<=u;i++){
475    int q=r[i].guess_quality(c);
476    if ((filled<n)||(q<worst)){
477      if(filled<n){
478        worst=max(q,worst);
479        indizes[filled]=i;
480        weight[filled]=q;
481        filled++;
482      }
483    }
484    else{
485      int j;
486      for(j=0;j<filled;j++){
487        if (worst==weight[j]){
488          weight[j]=q;
489          indizes[j]=i;
490        }
491      }
492      worst=-1;
493      for(j=0;j<filled;j++){
494        if (worst<weight[j]){
495          worst=weight[j];
496        }
497      }
498    }
499  }
500  assume(filled==n);
501  int pos=0;
502
503  for(i=0;i<filled;i++){ 
504    r[indizes[i]].canonicalize();
505    weight[i]=r[indizes[i]].guess_quality(c);
506    if(weight[i]<weight[pos]) pos=i;
507  }
508  w=weight[pos];
509  pos=indizes[pos];
510
511  omfree(indizes);
512  omfree(weight);
513
514  assume(w==r[pos].guess_quality(c));
515  assume(l<=pos);
516  assume(u>=pos);
517  return pos;
518 
519}
520
521#endif
522void red_object::canonicalize(){
523  kBucketCanonicalize(bucket);
524  if(sum)
525    kBucketCanonicalize(sum->ac->bucket);
526 
527}
528BOOLEAN good_has_t_rep(int i, int j,calc_dat* c){
529  assume(i>=0);
530    assume(j>=0);
531  if (has_t_rep(i,j,c)) return TRUE;
532  //poly lm=pOne();
533  assume (c->tmp_lm!=NULL);
534  poly lm=c->tmp_lm;
535
536  pLcm(c->S->m[i], c->S->m[j], lm);
537  pSetm(lm);
538  assume(lm!=NULL);
539  //int deciding_deg= pTotaldegree(lm);
540  int* i_con =make_connections(i,j,lm,c);
541  //p_Delete(&lm,c->r);
542
543
544  for (int n=0;((n<c->n) && (i_con[n]>=0));n++){
545    if (i_con[n]==j){
546      now_t_rep(i,j,c);
547      omfree(i_con);
548
549      return TRUE;
550    }
551  }
552  omfree(i_con);
553
554  return FALSE;
555}
556BOOLEAN lenS_correct(kStrategy strat){
557  int i;
558  for(i=0;i<=strat->sl;i++){
559    if (strat->lenS[i]!=pLength(strat->S[i]))
560      return FALSE;
561  }
562  return TRUE;
563}
564
565static void notice_miss(int i, int j, calc_dat* c){
566  if (TEST_OPT_PROT)
567    PrintS("-");
568 
569}
570
571static void cleanS(kStrategy strat, calc_dat* c){
572  int i=0;
573  LObject P;
574  while(i<=strat->sl){
575    P.p=strat->S[i];
576    P.sev=strat->sevS[i];
577    if(kFindDivisibleByInS(strat->S,strat->sevS,strat->sl,&P)!=i)
578    {
579      deleteInS(i,strat);
580      //remember destroying poly
581      BOOLEAN found=FALSE;
582      int j;
583      for(j=0;j<c->n;j++)
584        if(c->S->m[j]==P.p)
585        {
586          found=TRUE;
587          break;
588        }
589      if (!found)
590        pDelete(&P.p);
591      //remember additional reductors
592    }
593    else i++;
594  }
595}
596static int bucket_guess(kBucket* bucket){
597  int sum=0;
598  int i;
599  for (i=bucket->buckets_used;i>=0;i--){
600    if(bucket->buckets[i])
601       sum+=bucket->buckets_length[i];
602  }
603  return sum;
604}
605
606
607
608
609
610
611static int add_to_reductors(calc_dat* c, poly h, int len){
612  //inDebug(h);
613  assume(lenS_correct(c->strat));
614  assume(len==pLength(h));
615  int i;
616//   if (c->is_char0)
617//        i=simple_posInS(c->strat,h,pSLength(h,len),c->is_char0);
618//   else
619//     i=simple_posInS(c->strat,h,len,c->is_char0);
620
621  LObject P; memset(&P,0,sizeof(P));
622  P.tailRing=c->r;
623  P.p=h; /*p_Copy(h,c->r);*/
624  P.FDeg=pFDeg(P.p,c->r);
625 
626  if (!rField_is_Zp(c->r)){ 
627    pCleardenom(P.p);
628    pContent(P.p); //is a duplicate call, but belongs here
629   
630  }
631  else                     
632    pNorm(P.p);
633  pNormalize(P.p);
634  int pq=pQuality(h,c,len);
635  i=simple_posInS(c->strat,h,pq);
636  c->strat->enterS(P,i,c->strat,-1);
637 
638 
639
640  c->strat->lenS[i]=len;
641  assume(pLength(c->strat->S[i])==c->strat->lenS[i]);
642  if(c->strat->lenSw)
643    c->strat->lenSw[i]=pq;
644 
645  return i;
646 
647}
648static void length_one_crit(calc_dat* c, int pos, int len)
649{
650  if (len==1)
651  {
652    int i;
653    for ( i=0;i<pos;i++)
654    {
655      if (c->lengths[i]==1)
656        c->states[pos][i]=HASTREP;
657    }
658    for ( i=pos+1;i<c->n;i++){
659      if (c->lengths[i]==1)
660        c->states[i][pos]=HASTREP;
661    }
662    shorten_tails(c,c->S->m[pos]);
663  }
664}
665static sorted_pair_node* find_next_pair2(calc_dat* c, BOOLEAN go_higher){
666  clean_top_of_pair_list(c);
667  sorted_pair_node* s=pop_pair(c);
668
669
670  return s;
671}
672
673static void move_forward_in_S(int old_pos, int new_pos,kStrategy strat)
674{
675  assume(old_pos>=new_pos);
676  poly p=strat->S[old_pos];
677  int ecart=strat->ecartS[old_pos];
678  long sev=strat->sevS[old_pos];
679  int s_2_r=strat->S_2_R[old_pos];
680  int length=strat->lenS[old_pos];
681  assume(length==pLength(strat->S[old_pos]));
682  int length_w;
683  if(strat->lenSw)
684    length_w=strat->lenSw[old_pos];
685  int i;
686  for (i=old_pos; i>new_pos; i--)
687  {
688    strat->S[i] = strat->S[i-1];
689    strat->ecartS[i] = strat->ecartS[i-1];
690    strat->sevS[i] = strat->sevS[i-1];
691    strat->S_2_R[i] = strat->S_2_R[i-1];
692  }
693  if (strat->lenS!=NULL)
694    for (i=old_pos; i>new_pos; i--)
695      strat->lenS[i] = strat->lenS[i-1];
696  if (strat->lenSw!=NULL)
697    for (i=old_pos; i>new_pos; i--)
698      strat->lenSw[i] = strat->lenSw[i-1];
699
700  strat->S[new_pos]=p;
701  strat->ecartS[new_pos]=ecart;
702  strat->sevS[new_pos]=sev;
703  strat->S_2_R[new_pos]=s_2_r;
704  strat->lenS[new_pos]=length;
705  if(strat->lenSw)
706    strat->lenSw[new_pos]=length_w;
707  //assume(lenS_correct(strat));
708}
709static void replace_pair(int & i, int & j, calc_dat* c)
710{
711  c->soon_free=NULL;
712  int curr_deg;
713  poly lm=pOne();
714
715  pLcm(c->S->m[i], c->S->m[j], lm);
716  pSetm(lm);
717  int deciding_deg= pTotaldegree(lm);
718  int* i_con =make_connections(i,j,lm,c);
719  int z=0;
720
721  for (int n=0;((n<c->n) && (i_con[n]>=0));n++){
722    if (i_con[n]==j){
723      now_t_rep(i,j,c);
724      omfree(i_con);
725      p_Delete(&lm,c->r);
726      return;
727    }
728  }
729
730  int* j_con =make_connections(j,lm,c);
731  i= i_con[0];
732  j=j_con[0];
733  if(c->n>1){
734    if (i_con[1]>=0)
735      i=i_con[1];
736    else {
737      if (j_con[1]>=0)
738        j=j_con[1];
739    }
740  }
741  pLcm(c->S->m[i], c->S->m[j], lm);
742  pSetm(lm);
743  poly short_s;
744  curr_deg=pTotaldegree(lm);
745  int_pair_node* last=NULL;
746
747  for (int n=0;((n<c->n) && (j_con[n]>=0));n++){
748    for (int m=0;((m<c->n) && (i_con[m]>=0));m++){
749      pLcm(c->S->m[i_con[m]], c->S->m[j_con[n]], lm);
750      pSetm(lm);
751      if (pTotaldegree(lm)>=deciding_deg)
752      {
753        soon_t_rep(i_con[m],j_con[n],c);
754        int_pair_node* h= (int_pair_node*)omalloc(sizeof(int_pair_node));
755        if (last!=NULL)
756          last->next=h;
757        else
758          c->soon_free=h;
759        h->next=NULL;
760        h->a=i_con[m];
761        h->b=j_con[n];
762        last=h;
763      }
764      //      if ((comp_deg<curr_deg)
765      //  ||
766      //  ((comp_deg==curr_deg) &&
767      short_s=ksCreateShortSpoly(c->S->m[i_con[m]],c->S->m[j_con[n]],c->r);
768      if (short_s==NULL) {
769        i=i_con[m];
770        j=j_con[n];
771        now_t_rep(i_con[m],j_con[n],c);
772        p_Delete(&lm,c->r);
773        omfree(i_con);
774        omfree(j_con);
775
776        return;
777      }
778#ifdef QUICK_SPOLY_TEST
779      for (int dz=0;dz<=c->n;dz++){
780        if (dz==c->n) {
781          //have found not head reducing pair
782          i=i_con[m];
783          j=j_con[n];
784          p_Delete(&short_s,c->r);
785          p_Delete(&lm,c->r);
786          omfree(i_con);
787          omfree(j_con);
788
789          return;
790        }
791        if (p_LmDivisibleBy(c->S->m[dz],short_s,c->r)) break;
792      }
793#endif
794      int comp_deg(pTotaldegree(short_s));
795      p_Delete(&short_s,c->r);
796      if ((comp_deg<curr_deg))
797         
798      {
799        curr_deg=comp_deg;
800        i=i_con[m];
801        j=j_con[n];
802      }
803    }
804  }
805  p_Delete(&lm,c->r);
806  omfree(i_con);
807  omfree(j_con);
808  return;
809}
810
811
812static int* make_connections(int from, poly bound, calc_dat* c)
813{
814  ideal I=c->S;
815  int s=pTotaldegree(bound);
816  int* cans=(int*) omalloc(c->n*sizeof(int));
817  int* connected=(int*) omalloc(c->n*sizeof(int));
818  int cans_length=0;
819  connected[0]=from;
820  int connected_length=1;
821  long neg_bounds_short= ~p_GetShortExpVector(bound,c->r);
822  for (int i=0;i<c->n;i++){
823    if (c->T_deg[i]>s) continue;
824    if (i!=from){
825      if(p_LmShortDivisibleBy(I->m[i],c->short_Exps[i],bound,neg_bounds_short,c->r)){
826        cans[cans_length]=i;
827        cans_length++;
828      }
829    }
830  }
831  int not_yet_found=cans_length;
832  int con_checked=0;
833  int pos;
834  while((not_yet_found>0) && (con_checked<connected_length)){
835    pos=connected[con_checked];
836    for(int i=0;i<cans_length;i++){
837      if (cans[i]<0) continue;
838      if (has_t_rep(pos,cans[i],c))
839      {
840        connected[connected_length]=cans[i];
841        connected_length++;
842        cans[i]=-1;
843        --not_yet_found;
844      }
845    }
846    con_checked++;
847  }
848  if (connected_length<c->n){
849    connected[connected_length]=-1;
850  }
851  omfree(cans);
852  return connected;
853}
854static int* make_connections(int from, int to, poly bound, calc_dat* c)
855{
856  ideal I=c->S;
857  int s=pTotaldegree(bound);
858  int* cans=(int*) omalloc(c->n*sizeof(int));
859  int* connected=(int*) omalloc(c->n*sizeof(int));
860  cans[0]=to;
861  int cans_length=1;
862  connected[0]=from;
863  int last_cans_pos=-1;
864  int connected_length=1;
865  long neg_bounds_short= ~p_GetShortExpVector(bound,c->r);
866
867  int not_yet_found=cans_length;
868  int con_checked=0;
869  int pos;
870  BOOLEAN can_find_more=TRUE;
871  while(TRUE){
872    if ((con_checked<connected_length)&& (not_yet_found>0)){
873      pos=connected[con_checked];
874      for(int i=0;i<cans_length;i++){
875        if (cans[i]<0) continue;
876        if (has_t_rep(pos,cans[i],c))//||(trivial_syzygie(pos,cans[i],bound,c))
877{
878
879          connected[connected_length]=cans[i];
880          connected_length++;
881          cans[i]=-1;
882          --not_yet_found;
883
884          if (connected[connected_length-1]==to){
885            if (connected_length<c->n){
886              connected[connected_length]=-1;
887            }
888            omfree(cans);
889            return connected;
890          }
891        }
892      }
893      con_checked++;
894    }
895    else
896    {
897      for(last_cans_pos++;last_cans_pos<=c->n;last_cans_pos++){
898        if (last_cans_pos==c->n){
899          if (connected_length<c->n){
900            connected[connected_length]=-1;
901          }
902          omfree(cans);
903          return connected;
904        }
905        if ((last_cans_pos==from)||(last_cans_pos==to))
906          continue;
907        if(p_LmShortDivisibleBy(I->m[last_cans_pos],c->short_Exps[last_cans_pos],bound,neg_bounds_short,c->r)){
908          cans[cans_length]=last_cans_pos;
909          cans_length++;
910          break;
911        }
912      }
913      not_yet_found++;
914      for (int i=0;i<con_checked;i++){
915        if (has_t_rep(connected[i],last_cans_pos,c)){
916
917          connected[connected_length]=last_cans_pos;
918          connected_length++;
919          cans[cans_length-1]=-1;
920
921          --not_yet_found;
922          if (connected[connected_length-1]==to){
923            if (connected_length<c->n){
924              connected[connected_length]=-1;
925            }
926
927            omfree(cans);
928            return connected;
929          }
930          break;
931        }
932      }
933    }
934  }
935  if (connected_length<c->n){
936    connected[connected_length]=-1;
937  }
938
939  omfree(cans);
940  return connected;
941}
942#ifdef HEAD_BIN
943static inline poly p_MoveHead(poly p, omBin b)
944{
945  poly np;
946  omTypeAllocBin(poly, np, b);
947  memmove(np, p, b->sizeW*sizeof(long));
948  omFreeBinAddr(p);
949  return np;
950}
951#endif
952
953
954
955//len should be weighted length in char 0
956static int simple_posInS (kStrategy strat, poly p,int len)
957{
958
959
960  if(strat->sl==-1) return 0;
961  polyset set=strat->S;
962  intset setL=strat->lenS;
963  if (strat->lenSw) setL=strat->lenSw;
964  int length=strat->sl;
965  int i;
966  int an = 0;
967  int en= length;
968
969  if ((len>setL[length])
970      || ((len==setL[length]) && (pLmCmp(set[length],p)== -1)))
971    return length+1;
972
973  loop
974  {
975    if (an >= en-1)
976    {
977      if ((len<setL[an])
978          || ((len==setL[an]) && (pLmCmp(set[an],p) == 1))) return an;
979      return en;
980    }
981    i=(an+en) / 2;
982    if ((len<setL[i])
983        || ((len==setL[i]) && (pLmCmp(set[i],p) == 1))) en=i;
984    //else if ((len>setL[i])
985    //|| ((len==setL[i]) && (pLmCmp(set[i],p) == -1))) an=i;
986    else an=i;
987  }
988}
989/*2
990 *if the leading term of p
991 *divides the leading term of some S[i] it will be canceled
992 */
993static inline void clearS (poly p, unsigned long p_sev,int l, int* at, int* k,
994                           kStrategy strat)
995{
996  assume(p_sev == pGetShortExpVector(p));
997  if (!pLmShortDivisibleBy(p,p_sev, strat->S[*at], ~ strat->sevS[*at])) return;
998  if (l>=strat->lenS[*at]) return;
999  if (TEST_OPT_PROT)
1000    PrintS("!");
1001  mflush();
1002  //pDelete(&strat->S[*at]);
1003  deleteInS((*at),strat);
1004  (*at)--;
1005  (*k)--;
1006//  assume(lenS_correct(strat));
1007}
1008static sorted_pair_node** add_to_basis(poly h, int i_pos, int j_pos,calc_dat* c, int* ip)
1009{
1010
1011  assume(h!=NULL);
1012//  BOOLEAN corr=lenS_correct(c->strat);
1013  BOOLEAN R_found=FALSE;
1014  void* hp;
1015
1016  ++(c->n);
1017  ++(c->S->ncols);
1018  int i,j;
1019  i=c->n-1;
1020  sorted_pair_node** nodes=(sorted_pair_node**) omalloc(sizeof(sorted_pair_node*)*i);
1021  int spc=0;
1022  c->T_deg=(int*) omrealloc(c->T_deg,c->n*sizeof(int));
1023  c->T_deg[i]=pTotaldegree(h);
1024  if(c->T_deg_full){
1025    c->T_deg_full=(int*) omrealloc(c->T_deg_full,c->n*sizeof(int));
1026    c->T_deg_full[i]=pTotaldegree_full(h);
1027  }
1028 
1029  c->tmp_pair_lm=(poly*) omrealloc(c->tmp_pair_lm,c->n*sizeof(poly));
1030  c->tmp_pair_lm[i]=pOne_Special(c->r);
1031  c->tmp_spn=(sorted_pair_node**) omrealloc(c->tmp_spn,c->n*sizeof(sorted_pair_node*));
1032  c->tmp_spn[i]=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1033  hp=omrealloc(c->rep, c->n *sizeof(int));
1034  if (hp!=NULL){
1035    c->rep=(int*) hp;
1036  } else {
1037    exit(1);
1038  }
1039  c->short_Exps=(long *) omrealloc(c->short_Exps ,c->n*sizeof(long));
1040
1041  hp=omrealloc(c->lengths, c->n *sizeof(int));
1042  if (hp!=NULL){
1043    c->lengths=(int*) hp;
1044  } else {
1045    exit(1);
1046  }
1047  c->lengths[i]=pLength(h);
1048  hp=omrealloc(c->states, c->n * sizeof(char*));
1049 
1050    c->states=(char**) hp;
1051  c->gcd_of_terms=(poly*) omrealloc(c->gcd_of_terms, c->n *sizeof(poly));
1052  c->gcd_of_terms[i]=gcd_of_terms(h,c->r);
1053  c->rep[i]=i;
1054  hp=omalloc(i*sizeof(char));
1055  if (hp!=NULL){
1056    c->states[i]=(char*) hp;
1057  } else {
1058    exit(1);
1059  }
1060  hp=omrealloc(c->S->m,c->n*sizeof(poly));
1061  if (hp!=NULL){
1062    c->S->m=(poly*) hp;
1063  } else {
1064    exit(1);
1065  }
1066  c->S->m[i]=h;
1067  c->short_Exps[i]=p_GetShortExpVector(h,c->r);
1068  for (j=0;j<i;j++){
1069    if (c->rep[j]==j){
1070      //check product criterion
1071
1072      c->states[i][j]=UNCALCULATED;
1073      assume(p_LmDivisibleBy(c->S->m[i],c->S->m[j],c->r)==
1074             p_LmShortDivisibleBy(c->S->m[i],c->short_Exps[i],c->S->m[j],~(c->short_Exps[j]),c->r));
1075      if(!c->F4_mode)
1076      {
1077        assume(!(p_LmDivisibleBy(c->S->m[j],c->S->m[i],c->r)));
1078      }
1079      //lies I[i] under I[j] ?
1080      if(p_LmShortDivisibleBy(c->S->m[i],c->short_Exps[i],c->S->m[j],~(c->short_Exps[j]),c->r)){
1081        c->rep[j]=i;
1082        if (TEST_OPT_PROT)
1083          PrintS("R"); 
1084        R_found=TRUE;
1085
1086        c->Rcounter++;
1087        if((i_pos>=0) && (j_pos>=0)){
1088       
1089        }
1090        for(int z=0;z<j;z++){
1091          if(c->rep[z]!=z) continue;
1092          if (c->states[j][z]==UNCALCULATED){
1093            c->states[j][z]=UNIMPORTANT;
1094          }
1095        }
1096        for(int z=j+1;z<i;z++){
1097          if(c->rep[z]!=z) continue;
1098          if (c->states[z][j]==UNCALCULATED){
1099            c->states[z][j]=UNIMPORTANT;
1100          }
1101        }
1102      }
1103    }
1104    else {
1105      c->states[i][j]=UNIMPORTANT;
1106    }
1107    if ((c->lengths[i]==1) && (c->lengths[j]==1))
1108      c->states[i][j]=HASTREP;
1109    else if (pHasNotCF(c->S->m[i],c->S->m[j])){
1110      c->easy_product_crit++;
1111      c->states[i][j]=HASTREP;
1112    }
1113                        else if(extended_product_criterion(c->S->m[i],c->gcd_of_terms[i],c->S->m[j],c->gcd_of_terms[j],c)){
1114                                        c->states[i][j]=HASTREP;
1115                                        c->extended_product_crit++;
1116                                        //PrintS("E");
1117                        }
1118    if (c->states[i][j]==UNCALCULATED){
1119
1120     
1121//      poly short_s=ksCreateShortSpoly(c->S->m[i],c->S->m[j],c->r);
1122      //    if (short_s)
1123      //    {
1124        sorted_pair_node* s=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1125        s->i=max(i,j);
1126        s->j=min(i,j);
1127        s->expected_length=c->lengths[i]+c->lengths[j]-2;
1128     
1129        poly lm=pOne();
1130
1131        pLcm(c->S->m[i], c->S->m[j], lm);
1132        pSetm(lm);
1133        s->deg=pTotaldegree(lm);
1134        if(c->T_deg_full)
1135        {
1136          int t_i=c->T_deg_full[s->i]-c->T_deg[s->i];
1137          int t_j=c->T_deg_full[s->j]-c->T_deg[s->j];
1138          s->deg+=max(t_i,t_j);
1139        }
1140        s->lcm_of_lm=lm;
1141//          pDelete(&short_s);
1142        //assume(lm!=NULL);
1143        nodes[spc]=s;
1144        spc++;
1145        // }
1146        //else
1147        //{
1148        //c->states[i][j]=HASTREP;
1149        //}
1150    }
1151  }
1152
1153  add_to_reductors(c, h, c->lengths[c->n-1]);
1154  //i=posInS(c->strat,c->strat->sl,h,0 ecart);
1155
1156  if (c->lengths[c->n-1]==1)
1157    shorten_tails(c,c->S->m[c->n-1]);
1158  //you should really update c->lengths, c->strat->lenS, and the oder of polys in strat if you sort after lengths
1159
1160  //for(i=c->strat->sl; i>0;i--)
1161  //  if(c->strat->lenS[i]<c->strat->lenS[i-1]) printf("fehler bei %d\n",i);
1162  if (c->Rcounter>50) {
1163    c->Rcounter=0;
1164    cleanS(c->strat,c);
1165  }
1166  if(!ip){
1167    qsort(nodes,spc,sizeof(sorted_pair_node*),pair_better_gen2);
1168 
1169   
1170    c->apairs=merge(c->apairs,c->pair_top+1,nodes,spc,c);
1171    c->pair_top+=spc;
1172    clean_top_of_pair_list(c);
1173    omfree(nodes);
1174    return NULL;
1175  }
1176  {
1177    *ip=spc;
1178    return nodes;
1179  }
1180
1181 
1182
1183}
1184
1185
1186static int iq_crit(const void* ap,const void* bp){
1187
1188  sorted_pair_node* a=*((sorted_pair_node**)ap);
1189  sorted_pair_node* b=*((sorted_pair_node**)bp);
1190  assume(a->i>a->j);
1191  assume(b->i>b->j);
1192  int comp=pLmCmp(a->lcm_of_lm, b->lcm_of_lm);
1193  if(comp!=0)
1194    return comp;
1195  if (a->deg<b->deg) return -1;
1196  if (a->deg>b->deg) return 1;
1197
1198
1199  if (a->expected_length<b->expected_length) return -1;
1200  if (a->expected_length>b->expected_length) return 1;
1201  if (a->j>b->j) return 1;
1202  if (a->j<b->j) return -1;
1203  return 0;
1204}
1205
1206static sorted_pair_node** add_to_basis_ideal_quotient(poly h, int i_pos, int j_pos,calc_dat* c, int* ip)
1207{
1208
1209  assume(h!=NULL);
1210//  BOOLEAN corr=lenS_correct(c->strat);
1211  BOOLEAN R_found=FALSE;
1212  void* hp;
1213
1214  ++(c->n);
1215  ++(c->S->ncols);
1216  int i,j;
1217  i=c->n-1;
1218  sorted_pair_node** nodes=(sorted_pair_node**) omalloc(sizeof(sorted_pair_node*)*i);
1219  int spc=0;
1220  c->T_deg=(int*) omrealloc(c->T_deg,c->n*sizeof(int));
1221  c->T_deg[i]=pTotaldegree(h);
1222  if(c->T_deg_full){
1223    c->T_deg_full=(int*) omrealloc(c->T_deg_full,c->n*sizeof(int));
1224    c->T_deg_full[i]=pTotaldegree_full(h);
1225  }
1226  c->tmp_pair_lm=(poly*) omrealloc(c->tmp_pair_lm,c->n*sizeof(poly));
1227  c->tmp_pair_lm[i]=pOne_Special(c->r);
1228  c->tmp_spn=(sorted_pair_node**) omrealloc(c->tmp_spn,c->n*sizeof(sorted_pair_node*));
1229  c->tmp_spn[i]=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1230
1231  hp=omrealloc(c->rep, c->n *sizeof(int));
1232  if (hp!=NULL){
1233    c->rep=(int*) hp;
1234  } else {
1235    exit(1);
1236  }
1237  c->short_Exps=(long *) omrealloc(c->short_Exps ,c->n*sizeof(long));
1238
1239  hp=omrealloc(c->lengths, c->n *sizeof(int));
1240  if (hp!=NULL){
1241    c->lengths=(int*) hp;
1242  } else {
1243    exit(1);
1244  }
1245  c->lengths[i]=pLength(h);
1246  hp=omrealloc(c->states, c->n * sizeof(char*));
1247 
1248    c->states=(char**) hp;
1249  c->gcd_of_terms=(poly*) omrealloc(c->gcd_of_terms, c->n *sizeof(poly));
1250  c->gcd_of_terms[i]=gcd_of_terms(h,c->r);
1251  c->rep[i]=i;
1252  hp=omalloc(i*sizeof(char));
1253  if (hp!=NULL){
1254    c->states[i]=(char*) hp;
1255  } else {
1256    exit(1);
1257  }
1258  hp=omrealloc(c->S->m,c->n*sizeof(poly));
1259  if (hp!=NULL){
1260    c->S->m=(poly*) hp;
1261  } else {
1262    exit(1);
1263  }
1264  c->S->m[i]=h;
1265  c->short_Exps[i]=p_GetShortExpVector(h,c->r);
1266  for (j=0;j<i;j++){
1267   
1268    //check product criterion
1269   
1270    c->states[i][j]=UNCALCULATED;
1271    assume(p_LmDivisibleBy(c->S->m[i],c->S->m[j],c->r)==
1272           p_LmShortDivisibleBy(c->S->m[i],c->short_Exps[i],c->S->m[j],~(c->short_Exps[j]),c->r));
1273    if(!c->F4_mode)
1274    {
1275      assume(!(p_LmDivisibleBy(c->S->m[j],c->S->m[i],c->r)));
1276    }
1277    //lies I[i] under I[j] ?
1278   
1279    if ((c->lengths[i]==1) && (c->lengths[j]==1))
1280      c->states[i][j]=HASTREP;
1281    else if (pHasNotCF(c->S->m[i],c->S->m[j]))
1282    {
1283      c->easy_product_crit++;
1284      c->states[i][j]=HASTREP;
1285    }
1286    else if(extended_product_criterion(c->S->m[i],c->gcd_of_terms[i],c->S->m[j],c->gcd_of_terms[j],c))
1287    {
1288      c->states[i][j]=HASTREP;
1289      c->extended_product_crit++;
1290      //PrintS("E");
1291    }
1292      //  if (c->states[i][j]==UNCALCULATED){
1293
1294     
1295//      poly short_s=ksCreateShortSpoly(c->S->m[i],c->S->m[j],c->r);
1296      //    if (short_s)
1297      //    {
1298    sorted_pair_node* s=c->tmp_spn[j];//(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1299    s->i=max(i,j);
1300    s->j=min(i,j);
1301    assume(s->j==j);
1302    s->expected_length=c->lengths[i]+c->lengths[j]-2;
1303     
1304    poly lm=c->tmp_pair_lm[j];//=pOne_Special();
1305     
1306    pLcm(c->S->m[i], c->S->m[j], lm);
1307    pSetm(lm);
1308    s->deg=pTotaldegree(lm);
1309    if(c->T_deg_full)
1310    {
1311      int t_i=c->T_deg_full[s->i]-c->T_deg[s->i];
1312      int t_j=c->T_deg_full[s->j]-c->T_deg[s->j];
1313      s->deg+=max(t_i,t_j);
1314      //Print("\n max: %d\n",max(t_i,t_j));
1315    }
1316    s->lcm_of_lm=lm;
1317    //          pDelete(&short_s);
1318    //assume(lm!=NULL);
1319    nodes[spc]=s;
1320    spc++;
1321        // }
1322        //else
1323        //{
1324        //c->states[i][j]=HASTREP;
1325        //}
1326  }
1327 
1328  assume(spc==i);
1329  //now ideal quotient crit
1330  qsort(nodes,spc,sizeof(sorted_pair_node*),iq_crit);
1331 
1332    sorted_pair_node** nodes_final=(sorted_pair_node**) omalloc(sizeof(sorted_pair_node*)*i);
1333  int spc_final=0;
1334  j=0;
1335  while(j<spc)
1336  {
1337    int lower=j;
1338    int upper;
1339    BOOLEAN has=FALSE;
1340    for(upper=lower+1;upper<spc;upper++)
1341    {
1342     
1343      if(!pLmEqual(nodes[lower]->lcm_of_lm,nodes[upper]->lcm_of_lm))
1344      {
1345        break;
1346      }
1347      if (has_t_rep(nodes[upper]->i,nodes[upper]->j,c))
1348        has=TRUE;
1349
1350    }
1351    upper=upper-1;
1352    int z;
1353    assume(spc_final<=j);
1354    for(z=0;z<spc_final;z++)
1355    {
1356      if(p_LmDivisibleBy(nodes_final[z]->lcm_of_lm,nodes[lower]->lcm_of_lm,c->r))
1357      {
1358        has=TRUE;
1359        break;
1360      }
1361    }
1362   
1363    if(has)
1364    {
1365      for(;lower<=upper;lower++)
1366      {
1367        //free_sorted_pair_node(nodes[lower],c->r);
1368        //omfree(nodes[lower]);
1369        nodes[lower]=NULL;
1370      }
1371      j=upper+1;
1372      continue;
1373    }
1374    else
1375    {
1376      nodes[lower]->lcm_of_lm=pCopy(nodes[lower]->lcm_of_lm);
1377      nodes_final[spc_final++]=nodes[lower];
1378      c->tmp_spn[nodes[lower]->j]=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1379      nodes[lower]=NULL;
1380      for(lower=lower+1;lower<=upper;lower++)
1381      {
1382        //      free_sorted_pair_node(nodes[lower],c->r);
1383        //omfree(nodes[lower]);
1384        nodes[lower]=NULL;
1385      }
1386      j=upper+1;
1387      continue;
1388    }
1389  }
1390
1391  //  Print("i:%d,spc_final:%d",i,spc_final);
1392
1393
1394
1395
1396  assume(spc_final<=spc);
1397  omfree(nodes);
1398  nodes=NULL;
1399
1400  add_to_reductors(c, h, c->lengths[c->n-1]);
1401  //i=posInS(c->strat,c->strat->sl,h,0 ecart);
1402
1403  if (c->lengths[c->n-1]==1)
1404    shorten_tails(c,c->S->m[c->n-1]);
1405  //you should really update c->lengths, c->strat->lenS, and the oder of polys in strat if you sort after lengths
1406
1407  //for(i=c->strat->sl; i>0;i--)
1408  //  if(c->strat->lenS[i]<c->strat->lenS[i-1]) printf("fehler bei %d\n",i);
1409  if (c->Rcounter>50) {
1410    c->Rcounter=0;
1411    cleanS(c->strat,c);
1412  }
1413  if(!ip){
1414    qsort(nodes_final,spc_final,sizeof(sorted_pair_node*),pair_better_gen2);
1415 
1416   
1417    c->apairs=merge(c->apairs,c->pair_top+1,nodes_final,spc_final,c);
1418    c->pair_top+=spc_final;
1419    clean_top_of_pair_list(c);
1420    omfree(nodes_final);
1421    return NULL;
1422  }
1423  {
1424    *ip=spc_final;
1425    return nodes_final;
1426  }
1427
1428 
1429
1430}
1431
1432
1433
1434
1435
1436
1437#if 0
1438static poly redNF (poly h,kStrategy strat)
1439{
1440  int j = 0;
1441  int z = 3;
1442  unsigned long not_sev;
1443
1444  if (0 > strat->sl)
1445  {
1446    return h;
1447  }
1448  not_sev = ~ pGetShortExpVector(h);
1449  loop
1450    {
1451      if (pLmShortDivisibleBy(strat->S[j], strat->sevS[j], h, not_sev))
1452      {
1453        //if (strat->interpt) test_int_std(strat->kIdeal);
1454        /*- compute the s-polynomial -*/
1455#ifdef KDEBUG
1456        if (TEST_OPT_DEBUG)
1457        {
1458         
1459          PrintS("red:");
1460          wrp(h);
1461          PrintS(" with ");
1462          wrp(strat->S[j]);
1463        }
1464#endif
1465        h = ksOldSpolyRed(strat->S[j],h,strat->kNoether);
1466#ifdef KDEBUG
1467        if (TEST_OPT_DEBUG)
1468        {
1469          PrintS("\nto:");
1470          wrp(h);
1471          PrintLn();
1472        }
1473#endif
1474        if (h == NULL) return NULL;
1475        z++;
1476        if (z>=10)
1477        {
1478          z=0;
1479          pNormalize(h);
1480        }
1481        /*- try to reduce the s-polynomial -*/
1482        j = 0;
1483        not_sev = ~ pGetShortExpVector(h);
1484      }
1485      else
1486      {
1487        if (j >= strat->sl) return h;
1488        j++;
1489      }
1490    }
1491}
1492#else
1493
1494static poly redNF2 (poly h,calc_dat* c , int &len, number&  m,int n)
1495{
1496  m=nInit(1);
1497  if (h==NULL) return NULL;
1498
1499  assume(len==pLength(h));
1500  kStrategy strat=c->strat;
1501  if (0 > strat->sl)
1502  {
1503    return h;
1504  }
1505  int j;
1506 
1507  LObject P(h);
1508  P.SetShortExpVector();
1509  P.bucket = kBucketCreate(currRing);
1510  // BOOLEAN corr=lenS_correct(strat);
1511  kBucketInit(P.bucket,P.p,len /*pLength(P.p)*/);
1512  //int max_pos=simple_posInS(strat,P.p);
1513  loop
1514    {
1515
1516      j=kFindDivisibleByInS(strat->S,strat->sevS,strat->sl,&P);
1517      if ((j>=0) && ((!n)||(strat->lenS[j]<=n)))
1518      {
1519
1520       
1521        nNormalize(pGetCoeff(P.p));
1522#ifdef KDEBUG
1523        if (TEST_OPT_DEBUG)
1524        {
1525          PrintS("red:");
1526          wrp(h);
1527          PrintS(" with ");
1528          wrp(strat->S[j]);
1529        }
1530#endif
1531       
1532        number coef=kBucketPolyRed(P.bucket,strat->S[j],
1533                                   strat->lenS[j]/*pLength(strat->S[j])*/,
1534                                   strat->kNoether);
1535        number m2=nMult(m,coef);
1536        nDelete(&m);
1537        m=m2;
1538        nDelete(&coef);
1539        h = kBucketGetLm(P.bucket);
1540       
1541        if (h==NULL) {
1542          len=0;
1543          kBucketDestroy(&P.bucket);
1544          return 
1545          NULL;}
1546        P.p=h;
1547        P.t_p=NULL;
1548        P.SetShortExpVector();
1549#ifdef KDEBUG
1550        if (TEST_OPT_DEBUG)
1551        {
1552          PrintS("\nto:");
1553          wrp(h);
1554          PrintLn();
1555        }
1556#endif
1557      }
1558      else
1559      {
1560        kBucketClear(P.bucket,&(P.p),&len);
1561        kBucketDestroy(&P.bucket);
1562        pNormalize(P.p);
1563        assume(len==(pLength(P.p)));
1564        return P.p;
1565      }
1566    }
1567}
1568
1569
1570static poly redTailShort(poly h, kStrategy strat){
1571
1572  int sl=strat->sl;
1573  int i;
1574  int len=pLength(h);
1575  for(i=0;i<=strat->sl;i++){
1576    if(strat->lenS[i]>2)
1577      break;
1578  }
1579  return(redNFTail(h,i-1,strat, len));
1580}
1581
1582static void line_of_extended_prod(int fixpos,calc_dat* c){
1583    if (c->gcd_of_terms[fixpos]==NULL)
1584  {
1585    c->gcd_of_terms[fixpos]=gcd_of_terms(c->S->m[fixpos],c->r);
1586    if (c->gcd_of_terms[fixpos])
1587    {
1588      int i;
1589      for(i=0;i<fixpos;i++)
1590        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)))
1591{
1592          c->states[fixpos][i]=HASTREP;
1593          c->extended_product_crit++;
1594}     
1595      for(i=fixpos+1;i<c->n;i++)
1596        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)))
1597        {        c->states[i][fixpos]=HASTREP;
1598        c->extended_product_crit++;
1599        }
1600    }
1601  }
1602}
1603static void c_S_element_changed_hook(int pos, calc_dat* c){
1604  length_one_crit(c,pos, c->lengths[pos]);
1605  line_of_extended_prod(pos,c);
1606}
1607class poly_tree_node {
1608public:
1609  poly p;
1610  poly_tree_node* l;
1611  poly_tree_node* r;
1612  int n;
1613  poly_tree_node(int sn):l(NULL),r(NULL),n(sn){}
1614};
1615class exp_number_builder{
1616public:
1617  poly_tree_node* top_level;
1618  int n;
1619  int get_n(poly p);
1620  exp_number_builder():top_level(0),n(0){}
1621};
1622int exp_number_builder::get_n(poly p){
1623  poly_tree_node** node=&top_level;
1624  while(*node!=NULL){
1625    int c=pLmCmp(p,(*node)->p);
1626    if (c==0) return (*node)->n;
1627    if (c==-1) node=&((*node)->r);
1628    else
1629      node=&((*node)->l);
1630  }
1631  (*node)= new poly_tree_node(n);
1632  n++;
1633  (*node)->p=pLmInit(p);
1634  return (*node)->n;
1635}
1636
1637//mac_polys exp are smaller iff they are greater by monomial ordering
1638//corresponding to solving linear equations notation
1639struct int_poly_pair{
1640  poly p;
1641  int n;
1642};
1643
1644void t2ippa_rec(poly* ip,int* ia, poly_tree_node* k, int &offset){
1645    if(!k) return;
1646    t2ippa_rec(ip,ia,k->l,offset);
1647    ip[offset]=k->p;
1648    ia[k->n]=offset;
1649    ++offset;
1650
1651    t2ippa_rec(ip,ia,k->r,offset);
1652    delete k;
1653  }
1654void t2ippa(poly* ip,int* ia,exp_number_builder & e){
1655
1656  int o=0;
1657  t2ippa_rec(ip,ia,e.top_level,o);
1658}
1659int anti_poly_order(const void* a, const void* b){
1660  return -pLmCmp(((int_poly_pair*) a)->p,((int_poly_pair*) b)->p );
1661}
1662mac_poly mac_p_add_ff_qq(mac_poly a, number f,mac_poly b){
1663  mac_poly erg;
1664  mac_poly* set_this;
1665  set_this=&erg;
1666  while((a!=NULL) &&(b!=NULL)){
1667    if (a->exp<b->exp){
1668      (*set_this)=a;
1669      a=a->next;
1670      set_this= &((*set_this)->next);
1671    } 
1672    else{
1673      if (a->exp>b->exp){
1674        mac_poly in =new mac_poly_r();
1675        in->exp=b->exp;
1676        in->coef=nMult(b->coef,f);
1677        (*set_this)=in;
1678        b=b->next;
1679        set_this= &((*set_this)->next);
1680      }
1681      else {
1682        //a->exp==b->ecp
1683        number n=nMult(b->coef,f);
1684        number n2=nAdd(a->coef,n);
1685        nDelete(&n);
1686        nDelete(&(a->coef));
1687        if (nIsZero(n2)){
1688          nDelete(&n2);
1689          mac_poly ao=a;
1690          a=a->next;
1691          delete ao;
1692          b=b->next;
1693         
1694        } else {
1695          a->coef=n2;
1696          b=b->next;
1697          (*set_this)=a;
1698          a=a->next;
1699          set_this= &((*set_this)->next);
1700        }
1701 
1702      }
1703   
1704    }
1705  }
1706  if((a==NULL)&&(b==NULL)){
1707    (*set_this)=NULL;
1708    return erg;
1709  }
1710  if (b==NULL) {
1711    (*set_this=a);
1712    return erg;
1713  }
1714 
1715  //a==NULL
1716  while(b!=NULL){
1717    mac_poly mp= new mac_poly_r();
1718    mp->exp=b->exp;
1719    mp->coef=nMult(f,b->coef);
1720    (*set_this)=mp;
1721    set_this=&(mp->next);
1722    b=b->next;
1723  }
1724  (*set_this)=NULL;
1725  return erg;
1726 
1727}
1728void mac_mult_cons(mac_poly p,number c){
1729  while(p){
1730    number m=nMult(p->coef,c);
1731    nDelete(&(p->coef));
1732    p->coef=m;
1733    p=p->next;
1734  }
1735 
1736}
1737int mac_length(mac_poly p){
1738  int l=0;
1739  while(p){
1740    l++;
1741    p=p->next;
1742  }
1743  return l;
1744}
1745//contrary to delete on the mac_poly_r, the coefficients are also destroyed here
1746void mac_destroy(mac_poly p){
1747  mac_poly iter=p;
1748  while(iter)
1749  {
1750    mac_poly next=iter->next;
1751    nDelete(&iter->coef);
1752    delete iter;
1753    iter=next;
1754  }
1755}
1756BOOLEAN is_valid_ro(red_object & ro){
1757  red_object r2=ro;
1758  ro.validate();
1759  if ((r2.p!=ro.p)||(r2.sev!=ro.sev)||(r2.sum!=ro.sum)) return FALSE;
1760  return TRUE;
1761}
1762void pre_comp(poly* p,int & pn,calc_dat* c){
1763  if(!(pn))
1764    return;
1765  mac_poly* q=(mac_poly*) omalloc(pn*sizeof(mac_poly)); 
1766  int i;
1767  exp_number_builder e;
1768  for(i=0;i<pn;i++){
1769    assume(p[i]!=NULL);
1770    q[i]=new mac_poly_r();
1771    q[i]->exp=e.get_n(p[i]);
1772    q[i]->coef=nCopy(p[i]->coef);
1773   
1774    mac_poly qa=q[i];
1775    poly pa=p[i];
1776    while(pa->next!=NULL){
1777      qa->next = new mac_poly_r();
1778      qa=qa->next;
1779      pa=pa->next,
1780      qa->exp=e.get_n(pa);
1781      qa->coef=nCopy(pa->coef);
1782     
1783     
1784    }
1785    qa->next=NULL;
1786    pDelete(&p[i]);
1787  }
1788  poly* ip= (poly*)omalloc (e.n*sizeof(poly));
1789  int* ia=(int*) omalloc (e.n*sizeof(int));
1790  t2ippa(ip,ia,e);
1791  for(i=0;i<pn;i++){
1792    mac_poly mp=q[i];
1793    while(mp!=NULL){
1794      mp->exp=ia[mp->exp];
1795      mp=mp->next;
1796    }
1797   
1798  }
1799//gaus reduction start
1800  int col, row;
1801  col=0;
1802  row=0;
1803  assume(pn>0);
1804  while(row<pn-1){
1805    //row is the row where pivot should be
1806    // row== pn-1 means we have only to act on one row so no red nec.
1807    //we assume further all rows till the pn-1 row are non-zero
1808   
1809    //select column
1810    int i;
1811    col=q[row]->exp;
1812    int found_in_row=row;
1813    for(i=row;i<pn;i++){
1814      if(q[i]->exp<col){
1815        col=q[i]->exp;
1816        found_in_row=i;
1817      }
1818     
1819    }
1820    //select pivot
1821    int act_l=mac_length(q[found_in_row]);
1822    for(i=row+1;i<pn;i++){
1823      if((q[i]->exp==col)&&(mac_length(q[i])<act_l)){
1824        found_in_row=i;
1825        act_l=mac_length(q[i]);//should be optimized here
1826      }
1827    }
1828    mac_poly h=q[row];
1829    q[row]=q[found_in_row];
1830    q[found_in_row]=h;
1831
1832    //reduction
1833    for(i=row+1;i<pn;i++){
1834      if(q[i]->exp==q[row]->exp){
1835       
1836        number c1=nNeg(nCopy(q[i]->coef));
1837        number c2=q[row]->coef;
1838        //use checkcoeff later
1839        mac_mult_cons(q[i],c2);
1840        q[i]=mac_p_add_ff_qq(q[i],c1,q[row]);
1841      }
1842         
1843       
1844       
1845    }
1846    for(i=row+1;i<pn;i++){
1847      if(q[i]==NULL){
1848        q[i]=q[pn-1];
1849        pn--;
1850        if(i!=pn){i--;}
1851      }
1852    }
1853 
1854    row++;
1855  }
1856
1857
1858//gaus reduction end 
1859
1860  for(i=0;i<pn;i++){
1861    poly pa;
1862    mac_poly qa;
1863    p[i]=pLmInit(ip[q[i]->exp]);
1864    pSetCoeff(p[i],q[i]->coef);
1865    pa=p[i];
1866    qa=q[i];
1867    while(qa->next!=NULL){
1868      qa=qa->next;
1869      pa->next=pLmInit(ip[qa->exp]);
1870      pa=pa->next;
1871      pa->coef=qa->coef;
1872    }
1873    pa->next=NULL;
1874  }
1875  for(i=0;i<e.n;i++){
1876    pDelete(&ip[i]); 
1877  }
1878  omfree(ip);
1879  omfree(ia);
1880}
1881
1882static void go_on (calc_dat* c){
1883  //set limit of 1000 for multireductions, at the moment for
1884  //programming reasons
1885  int i=0;
1886  c->average_length=0;
1887  for(i=0;i<c->n;i++){
1888    c->average_length+=c->lengths[i];
1889  }
1890  c->average_length=c->average_length/c->n;
1891  i=0;
1892  poly* p=(poly*) omalloc((PAR_N+1)*sizeof(poly));//nullterminated
1893
1894  int curr_deg=-1;
1895  while(i<PAR_N){
1896    sorted_pair_node* s=top_pair(c);//here is actually chain criterium done
1897    if (!s) break;
1898    if(curr_deg>=0){
1899      if (s->deg >curr_deg) break;
1900    }
1901
1902    else curr_deg=s->deg;
1903    quick_pop_pair(c);
1904    if(s->i>=0){
1905      //replace_pair(s->i,s->j,c);
1906    if(s->i==s->j) {
1907      free_sorted_pair_node(s,c->r);
1908      continue;
1909        }
1910    }
1911    poly h;
1912    if(s->i>=0)
1913      h=ksOldCreateSpoly(c->S->m[s->i], c->S->m[s->j], NULL, c->r);
1914    else
1915      h=s->lcm_of_lm;
1916    if(s->i>=0)
1917      now_t_rep(s->j,s->i,c);
1918    number coef;
1919    int mlen=pLength(h);
1920    h=redNF2(h,c,mlen,coef,2);
1921    redTailShort(h,c->strat);
1922    nDelete(&coef);
1923
1924    free_sorted_pair_node(s,c->r);
1925    if(!h) continue;
1926    int len=pLength(h);
1927    p[i]=h;
1928   
1929    i++;
1930  }
1931  p[i]=NULL;
1932//  pre_comp(p,i,c);
1933  if(i==0){
1934    omfree(p);
1935    return;
1936  }
1937  red_object* buf=(red_object*) omalloc(i*sizeof(red_object));
1938  c->normal_forms+=i;
1939  int j;
1940  for(j=0;j<i;j++){
1941    buf[j].p=p[j];
1942    buf[j].sev=pGetShortExpVector(p[j]);
1943    buf[j].sum=NULL;
1944    buf[j].bucket = kBucketCreate(currRing);
1945    int len=pLength(p[j]);
1946    kBucketInit(buf[j].bucket,buf[j].p,len);
1947  }
1948  omfree(p);
1949  qsort(buf,i,sizeof(red_object),red_object_better_gen);
1950//    Print("\ncurr_deg:%i\n",curr_deg);
1951  if (TEST_OPT_PROT)
1952    Print("M[%i, ",i);
1953#ifdef FIND_DETERMINISTIC
1954  c->modifiedS=(BOOLEAN*) omalloc((c->strat->sl+1)*sizeof(BOOLEAN));
1955  c->expandS=(poly*) omalloc((1)*sizeof(poly));
1956  c->expandS[0]=NULL;
1957  int z2;
1958  for(z2=0;z2<=c->strat->sl;z2++)
1959    c->modifiedS[z2]=FALSE;
1960#endif
1961  multi_reduction(buf, i, c);
1962#ifdef TGB_DEBUG
1963 {
1964   int k;
1965   for(k=0;k<i;k++)
1966   {
1967     assume(kFindDivisibleByInS_easy(c->strat,buf[k])<0);
1968     int k2;
1969     for(k2=0;k2<i;k2++)
1970     {
1971       if(k==k2) continue;
1972       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));
1973     }
1974   }
1975 }
1976#endif
1977  //resort S
1978#ifdef FIND_DETERMINISTIC
1979  for(z2=0;z2<=c->strat->sl;z2++)
1980  {
1981    if (c->modifiedS[z2])
1982    {
1983      int qal;
1984      if (c->strat->lenSw)
1985        qal=c->strat->lenSw[z2];
1986      else
1987        qal=c->strat->lenS[z2];
1988      int new_pos=simple_posInS(c->strat,c->strat->S[z2],qal);
1989      if (new_pos<z2)
1990      { 
1991        move_forward_in_S(z2,new_pos,c->strat);
1992      }
1993    }
1994  }
1995  for(z2=0;c->expandS[z2]!=NULL;z2++)
1996  {
1997    add_to_reductors(c,c->expandS[z2],pLength(c->expandS[z2]));
1998    // PrintS("E");
1999  }
2000  omfree(c->modifiedS);
2001  c->modifiedS=NULL;
2002  omfree(c->expandS);
2003  c->expandS=NULL;
2004#endif
2005  if (TEST_OPT_PROT)
2006      Print("%i]",i); 
2007 //  for(j=0;j<i;j++){
2008//     if(buf[j].p==NULL) PrintS("\n ZERO ALERT \n");
2009//     int z;
2010//      for(z=0;z<j;z++){
2011//       if (pLmEqual(buf[z].p, buf[j].p))
2012//      PrintS("\n Critical Warning!!!! \n");
2013     
2014//     }
2015//   }
2016  int* ibuf=(int*) omalloc(i*sizeof(int));
2017  sorted_pair_node*** sbuf=(sorted_pair_node***) omalloc(i*sizeof(sorted_pair_node**));
2018  for(j=0;j<i;j++)
2019  {
2020    int len;
2021    poly p;
2022    buf[j].flatten();
2023    kBucketClear(buf[j].bucket,&p, &len);
2024    kBucketDestroy(&buf[j].bucket);
2025    // delete buf[j];
2026    //remember to free res here
2027    p=redTailShort(p, c->strat);
2028    sbuf[j]=add_to_basis_ideal_quotient(p,-1,-1,c,ibuf+j);
2029    //sbuf[j]=add_to_basis(p,-1,-1,c,ibuf+j);
2030  }
2031  int sum=0;
2032  for(j=0;j<i;j++){
2033    sum+=ibuf[j];
2034  }
2035  sorted_pair_node** big_sbuf=(sorted_pair_node**) omalloc(sum*sizeof(sorted_pair_node*));
2036  int partsum=0;
2037  for(j=0;j<i;j++)
2038  {
2039    memmove(big_sbuf+partsum, sbuf[j],ibuf[j]*sizeof(sorted_pair_node*));
2040    omfree(sbuf[j]);
2041    partsum+=ibuf[j];
2042  }
2043
2044  qsort(big_sbuf,sum,sizeof(sorted_pair_node*),pair_better_gen2);
2045  c->apairs=merge(c->apairs,c->pair_top+1,big_sbuf,sum,c);
2046  c->pair_top+=sum;
2047  clean_top_of_pair_list(c);
2048  omfree(big_sbuf);
2049  omfree(sbuf);
2050  omfree(ibuf);
2051  omfree(buf);
2052#ifdef TGB_DEBUG
2053  int z;
2054  for(z=1;z<=c->pair_top;z++)
2055  {
2056    assume(pair_better(c->apairs[z],c->apairs[z-1],c));
2057  }
2058#endif
2059  return;
2060}
2061
2062static poly redNF (poly h,kStrategy strat, int &len)
2063{
2064  len=0;
2065  if (h==NULL) return NULL;
2066  int j;
2067
2068  len=pLength(h);
2069  if (0 > strat->sl)
2070  {
2071    return h;
2072  }
2073  LObject P(h);
2074  P.SetShortExpVector();
2075  P.bucket = kBucketCreate(currRing);
2076  kBucketInit(P.bucket,P.p,len /*pLength(P.p)*/);
2077  //int max_pos=simple_posInS(strat,P.p);
2078  loop
2079    {
2080      j=kFindDivisibleByInS(strat->S,strat->sevS,strat->sl,&P);
2081      if (j>=0)
2082      {
2083        nNormalize(pGetCoeff(P.p));
2084#ifdef KDEBUG
2085        if (TEST_OPT_DEBUG)
2086        {
2087          PrintS("red:");
2088          wrp(h);
2089          PrintS(" with ");
2090          wrp(strat->S[j]);
2091        }
2092#endif
2093        number coef=kBucketPolyRed(P.bucket,strat->S[j],
2094                                   strat->lenS[j]/*pLength(strat->S[j])*/,
2095                                   strat->kNoether);
2096        nDelete(&coef);
2097        h = kBucketGetLm(P.bucket);
2098        if (h==NULL) return NULL;
2099        P.p=h;
2100        P.t_p=NULL;
2101        P.SetShortExpVector();
2102#ifdef KDEBUG
2103        if (TEST_OPT_DEBUG)
2104        {
2105          PrintS("\nto:");
2106          wrp(h);
2107          PrintLn();
2108        }
2109#endif
2110      }
2111      else
2112      {
2113        kBucketClear(P.bucket,&(P.p),&len);
2114        kBucketDestroy(&P.bucket);
2115        pNormalize(P.p);
2116        return P.p;
2117      }
2118    }
2119}
2120#endif
2121#ifdef REDTAIL_S
2122
2123static poly redNFTail (poly h,const int sl,kStrategy strat, int len)
2124{
2125  if (h==NULL) return NULL;
2126  pTest(h);
2127  if (0 > sl)
2128    return h;
2129  if (pNext(h)==NULL) return h;
2130
2131  int j;
2132  poly res=h;
2133  poly act=res;
2134  LObject P(pNext(h));
2135  pNext(res)=NULL;
2136  P.bucket = kBucketCreate(currRing);
2137  len--;
2138  h=P.p;
2139  if (len <=0) len=pLength(h);
2140  kBucketInit(P.bucket,h /*P.p*/,len /*pLength(P.p)*/);
2141  pTest(h);
2142  loop
2143  {
2144      P.p=h;
2145      P.t_p=NULL;
2146      P.SetShortExpVector();
2147      loop
2148      {
2149          j=kFindDivisibleByInS(strat->S,strat->sevS,sl,&P);
2150          if (j>=0)
2151          {
2152#ifdef REDTAIL_PROT
2153            PrintS("r");
2154#endif
2155            nNormalize(pGetCoeff(P.p));
2156#ifdef KDEBUG
2157            if (TEST_OPT_DEBUG)
2158            {
2159              PrintS("red tail:");
2160              wrp(h);
2161              PrintS(" with ");
2162              wrp(strat->S[j]);
2163            }
2164#endif
2165            number coef;
2166            pTest(strat->S[j]);
2167            coef=kBucketPolyRed(P.bucket,strat->S[j],
2168                                strat->lenS[j]/*pLength(strat->S[j])*/,strat->kNoether);
2169            pMult_nn(res,coef);
2170            nDelete(&coef);
2171            h = kBucketGetLm(P.bucket);
2172            pTest(h);
2173            if (h==NULL)
2174            {
2175#ifdef REDTAIL_PROT
2176              PrintS(" ");
2177#endif
2178              kBucketDestroy(&P.bucket);
2179              return res;
2180            }
2181            pTest(h);
2182            P.p=h;
2183            P.t_p=NULL;
2184            P.SetShortExpVector();
2185#ifdef KDEBUG
2186            if (TEST_OPT_DEBUG)
2187            {
2188              PrintS("\nto tail:");
2189              wrp(h);
2190              PrintLn();
2191            }
2192#endif
2193          }
2194          else
2195          {
2196#ifdef REDTAIL_PROT
2197            PrintS("n");
2198#endif
2199            break;
2200          }
2201      } /* end loop current mon */
2202      //   poly tmp=pHead(h /*kBucketGetLm(P.bucket)*/);
2203      //act->next=tmp;pIter(act);
2204      act->next=kBucketExtractLm(P.bucket);pIter(act);
2205      h = kBucketGetLm(P.bucket);
2206      if (h==NULL)
2207      {
2208#ifdef REDTAIL_PROT
2209        PrintS(" ");
2210#endif
2211        kBucketDestroy(&P.bucket);
2212        return res;
2213      }
2214      pTest(h);
2215  }
2216}
2217#endif
2218
2219static void do_this_spoly_stuff(int i,int j,calc_dat* c){
2220  poly f=c->S->m[i];
2221  poly g=c->S->m[j];
2222  poly h=ksOldCreateSpoly(f, g, NULL, c->r);
2223  poly hr=NULL;
2224#ifdef FULLREDUCTIONS
2225  if (h!=NULL)
2226  {
2227    int len;
2228
2229//    hr=redNF2(h,c,len);
2230//      hr=redNF(h,c->strat,len);
2231
2232    if (hr!=NULL)
2233#ifdef REDTAIL_S
2234      hr = redNFTail(hr,c->strat->sl,c->strat,len);
2235#else
2236    hr = redtailBba(hr,c->strat->sl,c->strat);
2237#endif
2238
2239  }
2240#else
2241  if (h!=NULL)
2242  {
2243    int len;
2244//    hr=redNF2(h,c,len);
2245  }
2246#endif
2247  c->normal_forms++;
2248  if (hr==NULL)
2249  {
2250    notice_miss(i, j, c);
2251
2252  }
2253  else
2254  {
2255
2256#ifdef HEAD_BIN
2257    hr=p_MoveHead(hr,c->HeadBin);
2258#endif
2259    add_to_basis(hr, i,j,c);
2260  }
2261}
2262//try to fill, return FALSE iff queue is empty
2263static void simplify(monom_poly& h, calc_dat* c){
2264  mp_array_list* F=c->F;
2265  poly_array_list* F_minus=c->F_minus;
2266  while(F)
2267  {
2268    assume(F_minus!=NULL);
2269    int i;
2270    int posm=posInMonomPolys (F->mp, F->size, h,c);
2271#ifdef TGB_DEBUG
2272#endif
2273     //for(i=0;i<F->size;i++)
2274    for(i=min(posm,F->size-1);i>=0;i--)
2275    {
2276      //      if((i>=posm)&&(F->mp[i].f!=h.f)) break;
2277      if((i<posm)&&(F->mp[i].f!=h.f)) break;
2278      if ((h.f==F->mp[i].f) &&(p_LmDivisibleBy(F->mp[i].m,h.m,c->r)))
2279      {
2280       
2281        //      Print("found");
2282       
2283          //according to the algorithm you should test (!(pIsConstant(F[i].m)))
2284          //but I think this is only because of bad formulation
2285        int j;
2286       
2287        poly lm=pLmInit(h.f);
2288        pSetCoeff(lm,nInit(1));
2289
2290        lm=pMult_mm(lm,F->mp[i].m);
2291
2292        int pos;
2293        j=-1;
2294        pos=posInPolys (F_minus->p, F_minus->size, lm,c);
2295      if((F_minus->size>pos)&&(pLmEqual(lm,F_minus->p[pos])))
2296        j=pos;
2297      if((pos>0) &&(pLmEqual(lm,F_minus->p[pos-1])))
2298        j=pos-1;
2299      assume(j!=-1);
2300      //        if(j==-1) Print("\n jAltert \n");
2301//      for(j=0;j<F_minus->size;j++)
2302//      {
2303//        if (pLmEqual(F_minus->p[j],lm))
2304//          break;
2305//      }
2306//      assume(j<F_minus->size);
2307        pDelete(&lm);
2308        if(j>=0)
2309        {
2310          pExpVectorSub(h.m,F->mp[i].m);
2311          h.f=F_minus->p[j];
2312        }
2313        break;
2314      }
2315    }
2316    F=F->next;
2317    F_minus=F_minus->next;
2318  }
2319  assume(F_minus==NULL);
2320}
2321tgb_matrix::tgb_matrix(int i, int j){
2322  n=(number**) omalloc(i*sizeof (number*));;
2323  int z;
2324  int z2;
2325  for(z=0;z<i;z++)
2326  {
2327    n[z]=(number*)omalloc(j*sizeof(number));
2328    for(z2=0;z2<j;z2++)
2329    {
2330      n[z][z2]=nInit(0);
2331    }
2332  }
2333  this->columns=j;
2334  this->rows=i;
2335  free_numbers=FALSE;
2336}
2337tgb_matrix::~tgb_matrix(){
2338  int z;
2339  for(z=0;z<rows;z++)
2340  {
2341    if(n[z])
2342    {
2343      if(free_numbers)
2344      {
2345        int z2;
2346        for(z2=0;z2<columns;z2++)
2347        {
2348          nDelete(&(n[z][z2]));
2349        }
2350      }
2351      omfree(n[z]);
2352    }
2353  }
2354  omfree(n);
2355}
2356void tgb_matrix::print(){
2357  int i;
2358  int j;
2359  Print("\n");
2360  for(i=0;i<rows;i++)
2361  {
2362    Print("(");
2363    for(j=0;j<columns;j++)
2364    {
2365      StringSetS("");
2366      n_Write(n[i][j],currRing);
2367      Print(StringAppendS(""));
2368      Print("\t");
2369    }
2370    Print(")\n");
2371  }
2372}
2373//transfers ownership of n to the matrix
2374void tgb_matrix::set(int i, int j, number n){
2375  assume(i<rows);
2376  assume(j<columns);
2377  this->n[i][j]=n;
2378}
2379int tgb_matrix::get_rows(){
2380  return rows;
2381}
2382int tgb_matrix::get_columns(){
2383  return columns;
2384}
2385number tgb_matrix::get(int i, int j){
2386  assume(i<rows);
2387  assume(j<columns);
2388  return n[i][j];
2389}
2390BOOLEAN tgb_matrix::is_zero_entry(int i, int j){
2391  return (nIsZero(n[i][j]));
2392}
2393void tgb_matrix::perm_rows(int i, int j){
2394  number* h;
2395  h=n[i];
2396  n[i]=n[j];
2397  n[j]=h;
2398}
2399int tgb_matrix::min_col_not_zero_in_row(int row){
2400  int i;
2401  for(i=0;i<columns;i++)
2402  {
2403    if(!(nIsZero(n[row][i])))
2404      return i;
2405  }
2406  return columns;//error code
2407}
2408int tgb_matrix::next_col_not_zero(int row,int pre){
2409  int i;
2410  for(i=pre+1;i<columns;i++)
2411  {
2412    if(!(nIsZero(n[row][i])))
2413      return i;
2414  }
2415  return columns;//error code
2416}
2417BOOLEAN tgb_matrix::zero_row(int row){
2418  int i;
2419  for(i=0;i<columns;i++)
2420  {
2421    if(!(nIsZero(n[row][i])))
2422      return FALSE;
2423  }
2424  return TRUE;
2425}
2426int tgb_matrix::non_zero_entries(int row){
2427  int i;
2428  int z=0;
2429  for(i=0;i<columns;i++)
2430  {
2431    if(!(nIsZero(n[row][i])))
2432      z++;
2433  }
2434  return z;
2435}
2436//row add_to=row add_to +row summand*factor
2437void tgb_matrix::add_lambda_times_row(int add_to,int summand,number factor){
2438  int i;
2439  for(i=0;i<columns;i++){
2440    if(!(nIsZero(n[summand][i])))
2441    {
2442      number n1=n[add_to][i];
2443      number n2=nMult(factor,n[summand][i]);
2444      n[add_to][i]=nAdd(n1,n2);
2445      nDelete(&n1);
2446      nDelete(&n2);
2447    }
2448  }
2449}
2450void tgb_matrix::mult_row(int row,number factor){
2451  if (nIsOne(factor))
2452    return;
2453  int i;
2454  for(i=0;i<columns;i++){
2455    if(!(nIsZero(n[row][i])))
2456    {
2457      number n1=n[row][i];
2458      n[row][i]=nMult(n1,factor);
2459      nDelete(&n1);
2460    }
2461  }
2462}
2463void tgb_matrix::free_row(int row, BOOLEAN free_non_zeros){
2464  int i;
2465  for(i=0;i<columns;i++)
2466    if((free_non_zeros)||(!(nIsZero(n[row][i]))))
2467      nDelete(&(n[row][i]));
2468  omfree(n[row]);
2469  n[row]=NULL;
2470}
2471
2472
2473tgb_sparse_matrix::tgb_sparse_matrix(int i, int j, ring rarg){
2474  mp=(mac_poly*) omalloc(i*sizeof (mac_poly));;
2475  int z;
2476  int z2;
2477  for(z=0;z<i;z++)
2478  {
2479    mp[z]=NULL;
2480  }
2481  this->columns=j;
2482  this->rows=i;
2483  free_numbers=FALSE;
2484  r=rarg;
2485}
2486tgb_sparse_matrix::~tgb_sparse_matrix(){
2487  int z;
2488  for(z=0;z<rows;z++)
2489  {
2490    if(mp[z])
2491    {
2492      if(free_numbers)
2493      {
2494        mac_destroy(mp[z]);
2495      }
2496      else {
2497        while(mp[z])
2498        {
2499         
2500          mac_poly next=mp[z]->next;
2501          delete mp[z];
2502          mp[z]=next;
2503        }
2504      }
2505    }
2506  }
2507  omfree(mp);
2508}
2509int row_cmp_gen(const void* a, const void* b){
2510  const mac_poly ap= *((mac_poly*) a);
2511  const mac_poly bp=*((mac_poly*) b);
2512  if (ap==NULL) return 1;
2513  if (bp==NULL) return -1;
2514  if (ap->exp<bp->exp) return -1;
2515  return 1;
2516}
2517void tgb_sparse_matrix::sort_rows(){
2518  qsort(mp,rows,sizeof(mac_poly),row_cmp_gen);
2519}
2520void tgb_sparse_matrix::print(){
2521  int i;
2522  int j;
2523  Print("\n");
2524  for(i=0;i<rows;i++)
2525  {
2526    Print("(");
2527    for(j=0;j<columns;j++)
2528    {
2529      StringSetS("");
2530      number n=get(i,j);
2531      n_Write(n,currRing);
2532      Print(StringAppendS(""));
2533      Print("\t");
2534    }
2535    Print(")\n");
2536  }
2537}
2538//transfers ownership of n to the matrix
2539void tgb_sparse_matrix::set(int i, int j, number n){
2540  assume(i<rows);
2541  assume(j<columns);
2542  mac_poly* set_this=&mp[i];
2543  //  while(((*set_this)!=NULL)&&((*set_this)­>exp<j))
2544  while(((*set_this)!=NULL) && ((*set_this)->exp<j))
2545    set_this=&((*set_this)->next);
2546
2547  if (((*set_this)==NULL)||((*set_this)->exp>j))
2548  {
2549    if (nIsZero(n)) return;
2550    mac_poly old=(*set_this);
2551    (*set_this)=new mac_poly_r();
2552    (*set_this)->exp=j;
2553    (*set_this)->coef=n;
2554    (*set_this)->next=old;
2555    return;
2556  }
2557  assume((*set_this)->exp==j);
2558  if(!nIsZero(n))
2559  {
2560    nDelete(&(*set_this)->coef);
2561    (*set_this)->coef=n;
2562  }
2563  else
2564  {
2565    nDelete(&(*set_this)->coef);
2566    mac_poly dt=(*set_this);
2567    (*set_this)=dt->next;
2568    delete dt;
2569   
2570   
2571  }
2572  return;
2573}
2574
2575
2576
2577int tgb_sparse_matrix::get_rows(){
2578  return rows;
2579}
2580int tgb_sparse_matrix::get_columns(){
2581  return columns;
2582}
2583number tgb_sparse_matrix::get(int i, int j){
2584  assume(i<rows);
2585  assume(j<columns);
2586  mac_poly r=mp[i];
2587  while((r!=NULL)&&(r->exp<j))
2588    r=r->next;
2589  if ((r==NULL)||(r->exp>j))
2590  {
2591    number n=nInit(0);
2592    return n;
2593  }
2594  assume(r->exp==j);
2595  return r->coef;
2596}
2597BOOLEAN tgb_sparse_matrix::is_zero_entry(int i, int j){
2598  assume(i<rows);
2599  assume(j<columns);
2600  mac_poly r=mp[i];
2601  while((r!=NULL)&&(r->exp<j))
2602    r=r->next;
2603  if ((r==NULL)||(r->exp>j))
2604  {
2605    return TRUE;
2606  }
2607  assume(!nIsZero(r->coef));
2608  assume(r->exp==j);
2609  return FALSE;
2610 
2611}
2612
2613int tgb_sparse_matrix::min_col_not_zero_in_row(int row){
2614  if(mp[row]!=NULL)
2615  {
2616    assume(!nIsZero(mp[row]->coef));
2617    return mp[row]->exp;
2618  }
2619  else
2620
2621 
2622  return columns;//error code
2623}
2624int tgb_sparse_matrix::next_col_not_zero(int row,int pre){ 
2625  mac_poly r=mp[row];
2626  while((r!=NULL)&&(r->exp<=pre))
2627    r=r->next;
2628  if(r!=NULL)
2629  {
2630    assume(!nIsZero(r->coef));
2631    return r->exp;
2632  }
2633  return columns;//error code
2634}
2635BOOLEAN tgb_sparse_matrix::zero_row(int row){
2636  assume((mp[row]==NULL)||(!nIsZero(mp[row]->coef)));
2637  if (mp[row]==NULL)
2638    return TRUE;
2639  else
2640    return FALSE;
2641}
2642void tgb_sparse_matrix::row_normalize(int row){
2643  if (!rField_has_simple_inverse(r))  /* Z/p, GF(p,n), R, long R/C */
2644  {
2645    mac_poly m=mp[row];
2646        while (m!=NULL)
2647        {
2648          if (currRing==r) {nTest(m->coef);}
2649          n_Normalize(m->coef,r);
2650          m=m->next;
2651        }
2652  }
2653}
2654void tgb_sparse_matrix::row_content(int row){
2655 
2656  mac_poly ph=mp[row];
2657  number h,d;
2658  mac_poly p;
2659
2660  if(TEST_OPT_CONTENTSB) return;
2661  if(ph->next==NULL)
2662  {
2663    nDelete(&ph->coef);
2664    ph->coef=nInit(1);
2665  }
2666  else
2667  {
2668    nNormalize(ph->coef);
2669    if(!nGreaterZero(ph->coef)) {
2670      //ph = pNeg(ph);
2671      p=ph;
2672      while(p)
2673      {
2674        p->coef=nNeg(p->coef);
2675        p=p->next;
2676      }
2677    }
2678   
2679    h=nCopy(ph->coef);
2680    p = ph->next;
2681   
2682    while (p!=NULL)
2683    {
2684      nNormalize(p->coef);
2685      d=nGcd(h,p->coef,currRing);
2686      nDelete(&h);
2687      h = d;
2688      if(nIsOne(h))
2689      {
2690        break;
2691      }
2692      p=p->next;
2693    }
2694    p = ph;
2695    //number tmp;
2696    if(!nIsOne(h))
2697    {
2698      while (p!=NULL)
2699      {
2700   
2701        d = nIntDiv(pGetCoeff(p),h);
2702        nDelete(&p->coef);
2703        p->coef=d;
2704        p=p->next;
2705      }
2706    }
2707    nDelete(&h);
2708
2709  }
2710}
2711int tgb_sparse_matrix::non_zero_entries(int row){
2712
2713  return mac_length(mp[row]);
2714}
2715//row add_to=row add_to +row summand*factor
2716void tgb_sparse_matrix::add_lambda_times_row(int add_to,int summand,number factor){
2717  mp[add_to]= mac_p_add_ff_qq(mp[add_to], factor,mp[summand]);
2718
2719}
2720void tgb_sparse_matrix::mult_row(int row,number factor){
2721  if (nIsZero(factor))
2722  {
2723    mac_destroy(mp[row]);
2724    mp[row]=NULL;
2725   
2726    return;
2727  }
2728  if(nIsOne(factor))
2729    return;
2730  mac_mult_cons(mp[row],factor);
2731}
2732void tgb_sparse_matrix::free_row(int row, BOOLEAN free_non_zeros){
2733  if(free_non_zeros)
2734    mac_destroy(mp[row]);
2735  else
2736  {
2737    while(mp[row])
2738    {
2739     
2740      mac_poly next=mp[row]->next;
2741      delete mp[row];
2742      mp[row]=next;
2743    }
2744  }
2745  mp[row]=NULL;
2746}
2747//transfers ownership of m to mat
2748void init_with_mac_poly(tgb_sparse_matrix* mat, int row, mac_poly m){
2749  assume(mat->mp[row]==NULL);
2750  mat->mp[row]=m;
2751#ifdef TGB_DEBUG
2752  mac_poly r=m;
2753  while(r){
2754    assume(r->exp<mat->columns);
2755    r=r->next;
2756  }
2757#endif
2758}
2759poly free_row_to_poly(tgb_sparse_matrix* mat, int row, poly* monoms, int monom_index){
2760  poly p=NULL;
2761  poly* set_this=&p;
2762  mac_poly r=mat->mp[row];
2763  mat->mp[row]=NULL;
2764  while(r)
2765  {
2766    (*set_this)=pLmInit(monoms[monom_index-1-r->exp]);
2767    pSetCoeff((*set_this),r->coef);
2768    set_this=&((*set_this)->next);
2769    mac_poly old=r;
2770    r=r->next;
2771    delete old;
2772   
2773  }
2774  return p;
2775
2776}
2777
2778void simple_gauss(tgb_sparse_matrix* mat, calc_dat* c){
2779  int col, row;
2780  int* row_cache=(int*) omalloc(mat->get_rows()*sizeof(int));
2781  col=0;
2782  row=0;
2783  int i;
2784  int pn=mat->get_rows();
2785  int matcol=mat->get_columns();
2786  int* area=(int*) omalloc(sizeof(int)*((matcol-1)/bundle_size+1));
2787  const int max_area_index=(matcol-1)/bundle_size;
2788    //rows are divided in areas
2789  //if row begins with columns col, it is located in [area[col/bundle_size],area[col/bundle_size+1]-1]
2790  assume(pn>0);
2791  //first clear zeroes
2792  for(i=0;i<pn;i++)
2793  {
2794    if(mat->zero_row(i))
2795    {
2796      mat->perm_rows(i,pn-1);
2797      pn--;
2798      if(i!=pn){i--;}
2799    }
2800 
2801  }
2802  mat->sort_rows();
2803  for(i=0;i<pn;i++)
2804  {
2805      row_cache[i]=mat->min_col_not_zero_in_row(i);
2806      // Print("row_cache:%d\n",row_cache[i]);
2807  }
2808  int last_area=-1;
2809  for(i=0;i<pn;i++)
2810  {
2811    int this_area=row_cache[i]/bundle_size;
2812    assume(this_area>=last_area);
2813    if(this_area>last_area)
2814    {
2815      int j;
2816      for(j=last_area+1;j<=this_area;j++)
2817        area[j]=i;
2818      last_area=this_area;
2819    }
2820  }
2821  for(i=last_area+1;i<=max_area_index;i++)
2822  {
2823    area[i]=pn;
2824  }
2825  while(row<pn-1){
2826    //row is the row where pivot should be
2827    // row== pn-1 means we have only to act on one row so no red nec.
2828    //we assume further all rows till the pn-1 row are non-zero
2829   
2830    //select column
2831   
2832    //col=mat->min_col_not_zero_in_row(row);
2833    int max_in_area;
2834    {
2835      int tai=row_cache[row]/bundle_size;
2836      assume(tai<=max_area_index);
2837      if(tai==max_area_index)
2838        max_in_area=pn-1;
2839      else
2840        max_in_area=area[tai+1]-1;
2841    }
2842    assume(row_cache[row]==mat->min_col_not_zero_in_row(row));
2843    col=row_cache[row];
2844
2845   
2846    assume(col!=matcol);
2847    int found_in_row;
2848   
2849    found_in_row=row;
2850    BOOLEAN must_reduce=FALSE;
2851    assume(pn<=mat->get_rows());
2852    for(i=row+1;i<=max_in_area;i++){
2853      int first;//=mat->min_col_not_zero_in_row(i);
2854      assume(row_cache[i]==mat->min_col_not_zero_in_row(i));
2855      first=row_cache[i];
2856      assume(first!=matcol);
2857      if(first<col)
2858      {
2859        col=first;
2860        found_in_row=i;
2861        must_reduce=FALSE;
2862      }
2863      else {
2864        if(first==col)
2865          must_reduce=TRUE;
2866      }
2867    }
2868    //select pivot
2869    int act_l=nSize(mat->get(found_in_row,col))*mat->non_zero_entries(found_in_row);
2870    if(must_reduce)
2871    {
2872      for(i=found_in_row+1;i<=max_in_area;i++){
2873        assume(mat->min_col_not_zero_in_row(i)>=col);
2874        int first;
2875        assume(row_cache[i]==mat->min_col_not_zero_in_row(i));
2876        first=row_cache[i];
2877        assume(first!=matcol);
2878        //      if((!(mat->is_zero_entry(i,col)))&&(mat->non_zero_entries(i)<act_l))
2879        int nz;
2880        if((row_cache[i]==col)&&((nz=nSize(mat->get(i,col))*mat->non_zero_entries(i))<act_l))
2881        {
2882          found_in_row=i;
2883        act_l=nz;
2884        }
2885       
2886      }
2887    }
2888    mat->perm_rows(row,found_in_row);
2889    int h=row_cache[row];
2890    row_cache[row]=row_cache[found_in_row];
2891    row_cache[found_in_row]=h;
2892
2893
2894
2895    if(!must_reduce){
2896      row++;
2897      continue;
2898    }
2899    //reduction
2900    //must extract content and normalize here
2901    mat->row_content(row);
2902    mat->row_normalize(row);
2903
2904    //for(i=row+1;i<pn;i++){
2905    for(i=max_in_area;i>row;i--)
2906    {
2907      int col_area_index=col/bundle_size;
2908      assume(col_area_index<=max_area_index);
2909      assume(mat->min_col_not_zero_in_row(i)>=col);
2910      int first;
2911      assume(row_cache[i]==mat->min_col_not_zero_in_row(i));
2912      first=row_cache[i];
2913      assume(first!=matcol);
2914      if(row_cache[i]==col)
2915      {
2916       
2917        number c1=mat->get(i,col);
2918        number c2=mat->get(row,col);
2919        number n1=c1;
2920        number n2=c2;
2921
2922        ksCheckCoeff(&n1,&n2);
2923        //nDelete(&c1);
2924        n1=nNeg(n1);
2925        mat->mult_row(i,n2);
2926        mat->add_lambda_times_row(i,row,n1);
2927        nDelete(&n1);
2928        nDelete(&n2);
2929        assume(mat->is_zero_entry(i,col));
2930        row_cache[i]=mat->min_col_not_zero_in_row(i);
2931        assume(mat->min_col_not_zero_in_row(i)>col);
2932        if(row_cache[i]==matcol)
2933        {
2934          int index;
2935          index=i;
2936          int last_in_area;
2937          int this_cai=col_area_index;
2938          while(this_cai<max_area_index)
2939          {
2940            last_in_area=area[this_cai+1]-1;
2941            int h_c=row_cache[last_in_area];
2942            row_cache[last_in_area]=row_cache[index];
2943            row_cache[index]=h_c;
2944            mat->perm_rows(index,last_in_area);
2945            index=last_in_area;
2946            this_cai++;
2947            area[this_cai]--;
2948          }
2949          mat->perm_rows(index,pn-1);
2950          row_cache[index]=row_cache[pn-1];
2951          row_cache[pn-1]=matcol;
2952          pn--;
2953        }
2954        else 
2955        {
2956          int index;
2957          index=i;
2958          int last_in_area;
2959          int this_cai=col_area_index;
2960          int final_cai=row_cache[index]/bundle_size;
2961          assume(final_cai<=max_area_index);
2962          while(this_cai<final_cai)
2963          {
2964            last_in_area=area[this_cai+1]-1;
2965            int h_c=row_cache[last_in_area];
2966            row_cache[last_in_area]=row_cache[index];
2967            row_cache[index]=h_c;
2968            mat->perm_rows(index,last_in_area);
2969            index=last_in_area;
2970            this_cai++;
2971            area[this_cai]--;
2972          }
2973        }
2974      }
2975      else
2976        assume(mat->min_col_not_zero_in_row(i)>col);
2977    }
2978
2979//     for(i=row+1;i<pn;i++)
2980//     {
2981//       assume(mat->min_col_not_zero_in_row(i)==row_cache[i]);
2982//       // if(mat->zero_row(i))
2983//       assume(matcol==mat->get_columns());
2984//       if(row_cache[i]==matcol)
2985//       {
2986//      assume(mat->zero_row(i));
2987//      mat->perm_rows(i,pn-1);
2988//      row_cache[i]=row_cache[pn-1];
2989//      row_cache[pn-1]=matcol;
2990//      pn--;
2991//      if(i!=pn){i--;}
2992//       }
2993//     }
2994#ifdef TGB_DEBUG
2995  {
2996  int last=-1;
2997  for(i=0;i<pn;i++)
2998  {
2999    int act=mat->min_col_not_zero_in_row(i);
3000    assume(act>last);
3001   
3002  }
3003  for(i=pn;i<mat->get_rows();i++)
3004  {
3005    assume(mat->zero_row(i));
3006   
3007  }
3008 
3009
3010  }
3011#endif
3012    row++;
3013  }
3014  omfree(area);
3015  omfree(row_cache);
3016}
3017void simple_gauss2(tgb_matrix* mat){
3018  int col, row;
3019  col=0;
3020  row=0;
3021  int i;
3022  int pn=mat->get_rows();
3023  assume(pn>0);
3024  //first clear zeroes
3025//   for(i=0;i<pn;i++)
3026//   {
3027//     if(mat->zero_row(i))
3028//     {
3029//       mat->perm_rows(i,pn-1);
3030//       pn--;
3031//       if(i!=pn){i--;}
3032//     }
3033//   }
3034  while((row<pn-1)&&(col<mat->get_columns())){
3035    //row is the row where pivot should be
3036    // row== pn-1 means we have only to act on one row so no red nec.
3037    //we assume further all rows till the pn-1 row are non-zero
3038   
3039    //select column
3040   
3041    //    col=mat->min_col_not_zero_in_row(row);
3042    assume(col!=mat->get_columns());
3043    int found_in_row=-1;
3044   
3045    //    found_in_row=row;
3046    assume(pn<=mat->get_rows());
3047    for(i=row;i<pn;i++)
3048    {
3049      //    int first=mat->min_col_not_zero_in_row(i);
3050      //  if(first<col)
3051      if(!(mat->is_zero_entry(i,col))){
3052       
3053        found_in_row=i;
3054        break;
3055      }
3056    }
3057    if(found_in_row!=-1)
3058    {
3059    //select pivot
3060      int act_l=mat->non_zero_entries(found_in_row);
3061      for(i=i+1;i<pn;i++)
3062      {
3063        int vgl;
3064        assume(mat->min_col_not_zero_in_row(i)>=col);
3065        if((!(mat->is_zero_entry(i,col)))&&((vgl=mat->non_zero_entries(i))<act_l))
3066        {
3067          found_in_row=i;
3068          act_l=vgl;
3069        }
3070       
3071      }
3072      mat->perm_rows(row,found_in_row);
3073     
3074     
3075      //reduction
3076      for(i=row+1;i<pn;i++){
3077        assume(mat->min_col_not_zero_in_row(i)>=col);
3078        if(!(mat->is_zero_entry(i,col)))
3079        {
3080         
3081          number c1=nNeg(nCopy(mat->get(i,col)));
3082          number c2=mat->get(row,col);
3083          number n1=c1;
3084          number n2=c2;
3085         
3086          ksCheckCoeff(&n1,&n2);
3087          nDelete(&c1);
3088          mat->mult_row(i,n2);
3089          mat->add_lambda_times_row(i,row,n1);
3090          assume(mat->is_zero_entry(i,col));
3091         
3092        } 
3093        assume(mat->min_col_not_zero_in_row(i)>col);
3094      }
3095      row++;
3096    }
3097    col++;
3098    // for(i=row+1;i<pn;i++)
3099//     {
3100//       if(mat->zero_row(i))
3101//       {
3102//      mat->perm_rows(i,pn-1);
3103//      pn--;
3104//      if(i!=pn){i--;}
3105//       }
3106//     }
3107
3108  }
3109}
3110
3111static tgb_matrix* build_matrix(poly* p,int p_index,poly* done, int done_index, calc_dat* c){
3112  tgb_matrix* t=new tgb_matrix(p_index,done_index);
3113  int i, pos;
3114  //  Print("\n 0:%s\n",pString(done[done_index-1]));
3115  //Print("\n 1:%s\n",pString(done[done_index-2]));
3116  assume((!(pLmEqual(done[done_index-1],done[done_index-2]))));
3117#ifdef TGB_DEGUG
3118  for(i=0;i<done_index;i++)
3119  {
3120    int j;
3121    for(j=0;j<i;j++)
3122    {
3123      assume((!(pLmEqual(done[i],done[j]))));
3124    }
3125  }
3126#endif
3127  for(i=0;i<p_index;i++)
3128  { 
3129    // Print("%i ter Eintrag:%s\n",i,pString(p[i]));
3130    poly p_i=p[i];
3131    while(p_i)
3132    {
3133
3134      int v=-1;
3135      pos=posInPolys (done, done_index, p_i,c);
3136      if((done_index>pos)&&(pLmEqual(p_i,done[pos])))
3137        v=pos;
3138      if((pos>0) &&(pLmEqual(p_i,done[pos-1])))
3139        v=pos-1;
3140      assume(v!=-1);
3141      //v is ascending ordered, we need descending order
3142      v=done_index-1-v;
3143      number nt=t->get(i,v);
3144      nDelete(&nt);
3145      t->set(i,v,nCopy(p_i->coef));
3146      p_i=p_i->next;
3147    }
3148  }
3149  return t;
3150}
3151static tgb_sparse_matrix* build_sparse_matrix(poly* p,int p_index,poly* done, int done_index, calc_dat* c){
3152  tgb_sparse_matrix* t=new tgb_sparse_matrix(p_index,done_index,c->r);
3153  int i, pos;
3154  //  Print("\n 0:%s\n",pString(done[done_index-1]));
3155  //Print("\n 1:%s\n",pString(done[done_index-2]));
3156  //  assume((!(pLmEqual(done[done_index-1],done[done_index-2]))));
3157#ifdef TGB_DEGUG
3158  for(i=0;i<done_index;i++)
3159  {
3160    int j;
3161    for(j=0;j<i;j++)
3162    {
3163      assume((!(pLmEqual(done[i],done[j]))));
3164    }
3165  }
3166#endif
3167  for(i=0;i<p_index;i++)
3168  { 
3169    // Print("%i ter Eintrag:%s\n",i,pString(p[i]));
3170    mac_poly m=NULL;
3171    mac_poly* set_this=&m;
3172    poly p_i=p[i];
3173    while(p_i)
3174    {
3175
3176      int v=-1;
3177      pos=posInPolys (done, done_index, p_i,c);
3178      if((done_index>pos)&&(pLmEqual(p_i,done[pos])))
3179        v=pos;
3180      if((pos>0) &&(pLmEqual(p_i,done[pos-1])))
3181        v=pos-1;
3182      assume(v!=-1);
3183      //v is ascending ordered, we need descending order
3184      v=done_index-1-v;
3185      (*set_this)=new mac_poly_r();
3186      (*set_this)->exp=v;
3187       
3188      (*set_this)->coef=nCopy(p_i->coef);
3189       set_this=&(*set_this)->next;
3190       p_i=p_i->next;
3191
3192    }
3193    init_with_mac_poly(t,i,m);
3194  }
3195  return t;
3196}
3197
3198
3199static int retranslate(poly* m,tgb_sparse_matrix* mat,poly* done, calc_dat* c){
3200  int i;
3201  int m_index=0;
3202  for(i=0;i<mat->get_rows();i++)
3203  {
3204    if(mat->zero_row(i))
3205    {
3206      mat->free_row(i);
3207      continue;
3208    }
3209    m[m_index++]= free_row_to_poly(mat, i, done, mat->get_columns());
3210
3211  }
3212
3213  delete mat;
3214  return m_index;
3215
3216}
3217
3218//returns m_index and destroys mat
3219static int retranslate(poly* m,tgb_matrix* mat,poly* done, calc_dat* c){
3220  int i;
3221  int m_index=0;
3222  for(i=0;i<mat->get_rows();i++)
3223  {
3224    if(mat->zero_row(i))
3225    {
3226      mat->free_row(i);
3227      continue;
3228    }
3229   
3230    m[m_index]=pInit();
3231    int v=mat->min_col_not_zero_in_row(i);
3232    //v=done_index-1-pos; => pos=done_index-1-v=mat->get_columns()-1-v
3233    int pos=mat->get_columns()-1-v;
3234    int* ev=(int*) omalloc((c->r->N+1)*sizeof(int));
3235    pGetExpV(done[pos],ev);
3236    pSetExpV(m[m_index],ev);
3237    omfree(ev);
3238   
3239    poly p=m[m_index];
3240    pSetCoeff(p,mat->get(i,v));
3241    while((v=mat->next_col_not_zero(i,v))!=mat->get_columns())
3242    {
3243      poly pn=pInit();
3244     
3245      //v=done_index-1-pos; => pos=done_index-1-v=mat->get_columns()-1-v
3246      pos=mat->get_columns()-1-v;
3247      ev=(int*) omalloc((c->r->N+1)*sizeof(int));
3248      pGetExpV(done[pos],ev);
3249      pSetExpV(pn,ev);
3250      omfree(ev);
3251      pSetCoeff(pn,mat->get(i,v));
3252      p->next=pn;
3253      p=pn;
3254    }
3255    p->next=NULL;
3256    mat->free_row(i, FALSE);
3257    m_index++;
3258  }
3259
3260  delete mat;
3261  return m_index;
3262
3263}
3264static void go_on_F4 (calc_dat* c){
3265  //set limit of 1000 for multireductions, at the moment for
3266  //programming reasons
3267  int max_par;
3268  if (c->is_homog)
3269    max_par=PAR_N_F4;
3270  else
3271    max_par=200;
3272  int done_size=max_par*2;
3273  poly* done=(poly*) omalloc(done_size*sizeof(poly));
3274  int done_index=0; //done_index must always be smaller than done_size
3275  int chosen_size=max_par*2;
3276  monom_poly* chosen=(monom_poly*) omalloc(chosen_size*sizeof(monom_poly));
3277  int chosen_index=0;
3278  //  monom_poly* vgl=(monom_poly*) omalloc(chosen_size*sizeof(monom_poly));
3279  int i=0;
3280  c->average_length=0;
3281  for(i=0;i<c->n;i++){
3282    c->average_length+=c->lengths[i];
3283  }
3284  c->average_length=c->average_length/c->n;
3285  i=0;
3286  poly* p;
3287  int nfs=0;
3288  int curr_deg=-1;
3289
3290  //choose pairs and preprocess symbolically
3291  while(chosen_index<max_par)
3292  {
3293   
3294    //    sorted_pair_node* s=c->apairs[c->pair_top];
3295    sorted_pair_node* s;
3296    if(c->pair_top>=0)
3297      s=top_pair(c);//here is actually chain criterium done
3298    else
3299      s=NULL;
3300    if (!s) break;
3301    nfs++;
3302    if(curr_deg>=0)
3303    {
3304      if (s->deg >curr_deg) break;
3305    }
3306
3307    else curr_deg=s->deg;
3308    quick_pop_pair(c);
3309    if(s->i>=0)
3310    {
3311      //replace_pair(s->i,s->j,c);
3312      if(s->i==s->j) 
3313      {
3314        free_sorted_pair_node(s,c->r);
3315        continue;
3316      }
3317    }
3318    assume(s->i>=0);
3319    monom_poly h;
3320    BOOLEAN only_free=FALSE;
3321    if(s->i>=0)
3322    {
3323     
3324      poly lcm=pOne();
3325     
3326      pLcm(c->S->m[s->i], c->S->m[s->j], lcm);
3327      pSetm(lcm);
3328      assume(lcm!=NULL);
3329      poly factor1=pCopy(lcm);
3330      pExpVectorSub(factor1,c->S->m[s->i]);
3331      poly factor2=pCopy(lcm);
3332      pExpVectorSub(factor2,c->S->m[s->j]);
3333
3334      if(done_index>=done_size)
3335      {
3336        done_size+=max_par;
3337        done=(poly*) omrealloc(done,done_size*sizeof(poly));
3338      }
3339      done[done_index++]=lcm;
3340      if(chosen_index+1>=chosen_size)
3341      {
3342        //max_par must be greater equal 2
3343        chosen_size+=max(max_par,2);
3344        chosen=(monom_poly*) omrealloc(chosen,chosen_size*sizeof(monom_poly));
3345      }
3346      h.f=c->S->m[s->i];
3347      h.m=factor1;
3348      chosen[chosen_index++]=h;
3349      h.f=c->S->m[s->j];
3350      h.m=factor2;
3351      chosen[chosen_index++]=h;
3352    }
3353    else
3354    {
3355      if(chosen_index>=chosen_size)
3356      {
3357        chosen_size+=max_par;
3358        chosen=(monom_poly*) omrealloc(chosen,chosen_size*sizeof(monom_poly));
3359      }
3360      h.f=s->lcm_of_lm;
3361      h.m=pOne();
3362      //pSetm(h.m); done by pOne
3363      chosen[chosen_index++]=h;
3364      //must carefull remember to destroy such a h;
3365      poly_list_node* next=c->to_destroy;
3366     
3367      c->to_destroy=(poly_list_node*) omalloc(sizeof(poly_list_node));
3368      c->to_destroy->p=h.f;
3369      c->to_destroy->next=next;
3370      only_free=TRUE;
3371    }
3372
3373    if(s->i>=0)
3374      now_t_rep(s->j,s->i,c);
3375
3376    if(!(only_free))
3377      free_sorted_pair_node(s,c->r);
3378    else
3379      omfree(s);
3380   
3381 
3382
3383  }
3384  c->normal_forms+=nfs;
3385  if (TEST_OPT_PROT)
3386      Print("M[%i, ",nfs);
3387  //next Step, simplify all pairs
3388  for(i=0;i<chosen_index;i++)
3389  {
3390    //    PrintS("simplifying ");
3391    //Print("from %s",pString(chosen[i].m));
3392    //Print(", %s", pString(chosen[i].f));
3393     simplify(chosen[i],c);
3394    //Print(" to %s",pString(chosen[i].m));
3395    //Print(", %s \n", pString(chosen[i].f));
3396  }
3397 
3398  //next Step remove duplicate entries
3399  qsort(chosen,chosen_index,sizeof(monom_poly),monom_poly_crit);
3400  int pos=0;
3401  for(i=1;i<chosen_index;i++)
3402  {
3403    if(((chosen[i].f!=chosen[pos].f))||(!(pLmEqual(chosen[i].m,chosen[pos].m))))
3404      chosen[++pos]=chosen[i];
3405    else pDelete(&(chosen[i].m));
3406  }
3407  if(chosen_index>0)
3408    chosen_index=pos+1;
3409  //next step process polys
3410  int p_size=2*chosen_index;
3411  int p_index=0;
3412  p=(poly*) omalloc(p_size*sizeof(poly));
3413  for(p_index=0;p_index<chosen_index;p_index++)
3414  {
3415    p[p_index]=ppMult_mm(chosen[p_index].f,chosen[p_index].m);
3416  }
3417
3418  qsort(done, done_index,sizeof(poly),pLmCmp_func);
3419  pos=0;
3420  //Print("Done_index:%i",done_index);
3421  if(done_index>0)
3422  {
3423    pTest(done[0]);
3424  }
3425  for(i=1;i<done_index;i++)
3426  {
3427    pTest(done[i]);
3428    if((!(pLmEqual(done[i],done[pos]))))
3429      done[++pos]=done[i];
3430    else pDelete(&(done[i]));
3431  }
3432  if(done_index>0)
3433    done_index=pos+1;
3434#ifdef TGB_DEBUG
3435  for(i=0;i<done_index;i++)
3436  {
3437    int j;
3438    for(j=0;j<i;j++)
3439    {
3440      assume((!pLmEqual(done[j],done[i])));
3441    }
3442  }
3443#endif
3444#ifdef TGB_DEBUG
3445  for(i=0;i<done_index;i++)
3446  {
3447    pTest(done[i]);
3448  }
3449#endif
3450  poly* m;
3451  int m_index=0;
3452  int m_size=0;
3453  for(i=0;i<p_index;i++)
3454  {
3455    m_size+=pLength(p[i]);
3456  }
3457  m=(poly*) omalloc(m_size*sizeof(poly));
3458  //q=(poly*) omalloc(m_size*sizeof(poly));
3459
3460 
3461
3462  for(i=0;i<p_index;i++)
3463  {
3464
3465    poly p_i=p[i];
3466
3467    pTest(p[i]);
3468
3469    while(p_i)
3470    {
3471
3472      m[m_index]=pLmInit(p_i);
3473
3474      pSetCoeff(m[m_index],nInit(1));
3475
3476       p_i=p_i->next;
3477
3478      m_index++;
3479
3480    }
3481  }
3482
3483  int q_size=m_index;
3484  poly* q=(poly*) omalloc(q_size*sizeof(poly));
3485  int q_index=0;
3486  //next Step additional reductors
3487
3488  while(m_index>0)
3489  {
3490#ifdef TGB_DEBUG
3491     
3492      for(i=0;i<done_index;i++)
3493      {
3494        pTest(done[i]);
3495      }
3496#endif
3497
3498    qsort(m, m_index,sizeof(poly),pLmCmp_func);
3499
3500   
3501    pos=0;
3502    #ifdef TGB_DEBUG
3503     
3504      for(i=0;i<done_index;i++)
3505      {
3506       
3507        pTest(done[i]);
3508      }
3509#endif
3510    for(i=1;i<m_index;i++)
3511    {
3512      pTest(m[i]);
3513      pTest(m[pos]);
3514      if((!(pLmEqual(m[i],m[pos]))))
3515        m[++pos]=m[i];
3516      else pDelete(&(m[i]));
3517    }
3518    if(m_index>1)
3519      m_index=pos+1;
3520    if(done_size<done_index+m_index)
3521    {
3522      done_size=done_index+2*m_index;
3523      done=(poly*) omrealloc(done,done_size*sizeof(poly));
3524    }
3525    if(chosen_size<chosen_index+m_index)
3526    {
3527      chosen_size=chosen_index+2*m_index;
3528      chosen=(monom_poly*) omrealloc(chosen,chosen_size*sizeof(monom_poly));
3529    }
3530    q_index=0;
3531    if(q_size<m_index)
3532    {
3533      q_size=2*m_index;
3534      omfree(q);
3535      q=(poly*) omalloc(q_size*sizeof(poly));
3536    }
3537
3538    for(i=0;i<m_index;i++)
3539    {
3540
3541#ifdef TGB_DEBUG
3542      {
3543        int my_i;
3544        for(my_i=0;my_i<done_index;my_i++)
3545        {
3546          int my_j;
3547          for(my_j=0;my_j<my_i;my_j++)
3548          {
3549            assume((!pLmEqual(done[my_j],done[my_i])));
3550          }
3551        }
3552      }
3553#endif
3554      BOOLEAN in_done=FALSE;
3555      pTest(m[i]);
3556
3557      pos=posInPolys (done, done_index, m[i],c);
3558#ifdef TGB_DEBUG
3559      {
3560        int my_i;
3561        for (my_i=0;my_i<done_index;my_i++)
3562          pTest(done[my_i]);
3563      }
3564#endif     
3565      if(((done_index>pos)&&(pLmEqual(m[i],done[pos]))) ||(pos>0) &&(pLmEqual(m[i],done[pos-1])))
3566        in_done=TRUE;
3567      if (!(in_done))
3568      {
3569       
3570        int S_pos=kFindDivisibleByInS_easy(c->strat,m[i], pGetShortExpVector(m[i]));
3571        if(S_pos>=0)
3572        {
3573          monom_poly h;
3574          h.f=c->strat->S[S_pos];
3575          h.m=pOne();
3576          int* ev=(int*) omalloc((c->r->N+1)*sizeof(int));
3577          pGetExpV(m[i],ev);
3578          pSetExpV(h.m,ev);
3579          omfree(ev);
3580          pExpVectorSub(h.m,c->strat->S[S_pos]);
3581          simplify(h,c);
3582          q[q_index]=ppMult_mm(h.f,h.m);
3583          chosen[chosen_index++]=h;
3584          q_index++;
3585        }
3586        pTest(m[i]);
3587#ifdef TGB_DEBUG
3588        {
3589          int my_i;
3590          for (my_i=0;my_i<done_index;my_i++)
3591            pTest(done[my_i]);
3592        }
3593#endif     
3594        memmove(&(done[pos+1]),&(done[pos]), (done_index-pos)*sizeof(poly));
3595        done[pos]=m[i];
3596        done_index++;
3597#ifdef TGB_DEBUG
3598        {
3599          int my_i;
3600          for (my_i=0;my_i<done_index;my_i++)
3601          {
3602            //      Print("Position %i pos %i size %i\n",my_i,pos,done_index);
3603            pTest(done[my_i]);
3604          }
3605        }
3606#endif     
3607      }
3608      else
3609        pDelete(&m[i]);
3610#ifdef TGB_DEBUG
3611      {
3612        int my_i;
3613        for(my_i=0;my_i<done_index;my_i++)
3614        {
3615          pTest(done[my_i]);
3616          int my_j;
3617          for(my_j=0;my_j<my_i;my_j++)
3618          {
3619            assume((!pLmEqual(done[my_j],done[my_i])));
3620          }
3621        }
3622      }
3623#endif
3624    }
3625    if(p_size<p_index+q_index)
3626    {
3627      p_size=p_index+2*q_index;
3628      p=(poly*) omrealloc(p,p_size*sizeof(poly));
3629    }
3630    for (i=0;i<q_index;i++)
3631      p[p_index++]=q[i];
3632    m_index=0;
3633    int sum=0;
3634    for(i=0;i<p_index;i++)
3635    {
3636      sum+=pLength(p[i])-1;
3637    }
3638    if (m_size<sum)
3639    {
3640      omfree(m);
3641      m=(poly*) omalloc(sum*sizeof(poly));
3642    }
3643    m_size=sum;
3644    for(i=0;i<q_index;i++)
3645    {
3646      poly p_i=q[i]->next;
3647      while(p_i)
3648      {
3649        m[m_index]=pLmInit(p_i);
3650        pSetCoeff(m[m_index],nInit(1));
3651        p_i=p_i->next;
3652        m_index++;
3653      }
3654    }
3655    //qsort(done, done_index,sizeof(poly),pLmCmp_func);
3656#ifdef TGB_DEBUG
3657    for(i=0;i<done_index;i++)
3658    {
3659      pTest(done[i]);
3660    }
3661#endif
3662  }
3663#ifdef TGB_DEBUG
3664  for(i=0;i<done_index;i++)
3665  {
3666    int j;
3667    for(j=0;j<i;j++)
3668    {
3669      assume((!pLmEqual(done[j],done[i])));
3670    }
3671  }
3672#endif
3673  omfree(m);
3674  omfree(q);
3675  if (TEST_OPT_PROT)
3676    Print("Mat[%i x %i], ",chosen_index, done_index);
3677  //next Step build matrix
3678  #ifdef TGB_DEBUG
3679  for(i=0;i<done_index;i++)
3680  {
3681    int j;
3682    for(j=0;j<i;j++)
3683    {
3684      assume((!pLmEqual(done[j],done[i])));
3685    }
3686  }
3687#endif
3688  assume(p_index==chosen_index);
3689 
3690 //  tgb_matrix* mat=build_matrix(p,p_index,done, done_index,c);
3691 
3692//   simple_gauss2(mat);
3693  tgb_sparse_matrix* mat=build_sparse_matrix(p,p_index,done, done_index,c);
3694  simple_gauss(mat,c);
3695  m_size=mat->get_rows();
3696  m=(poly*) omalloc(m_size*sizeof(poly));
3697  m_index=retranslate(m,mat,done,c);
3698 
3699  mat=NULL;
3700  for(i=0;i<done_index;i++)
3701    pDelete(&done[i]);
3702  omfree(done);
3703  done=NULL;
3704  //next Step addElements to basis
3705  int F_plus_size=m_index;
3706  poly* F_plus=(poly*)omalloc(F_plus_size*sizeof(poly));
3707  int F_plus_index=0;
3708  int F_minus_size=m_index;
3709  poly* F_minus=(poly*) omalloc(F_minus_size*sizeof(poly));
3710  int F_minus_index=0;
3711
3712  //better algorithm replace p by its monoms, qsort,delete duplicates and binary search for testing if monom is contained in array
3713  qsort(p, p_index,sizeof(poly),pLmCmp_func);
3714  for(i=0;i<p_index;i++)
3715    pDelete(&p[i]->next);
3716  for(i=m_index-1;i>=0;--i)
3717  {
3718    int j;
3719    int pos=posInPolys (p,p_index, m[i],c);
3720    BOOLEAN minus=FALSE;
3721    if(((p_index>pos)&&(pLmEqual(m[i],p[pos]))) ||(pos>0) &&(pLmEqual(m[i],p[pos-1])))
3722      minus=TRUE;
3723   
3724    if(minus)
3725    {
3726      F_minus[F_minus_index++]=m[i];
3727      m[i]=NULL;
3728    }
3729    else
3730    {
3731      F_plus[F_plus_index++]=m[i];
3732      m[i]=NULL;
3733    }
3734  }
3735
3736//   for(i=m_index-1;i>=0;--i)
3737//   {
3738//     int j;
3739//     BOOLEAN minus=FALSE;
3740//     for(j=0;j<p_index;j++)
3741//       if (pLmEqual(p[j],m[i]))
3742//       {
3743//      minus=TRUE;
3744//      break;
3745//       }
3746//     if(minus)
3747//     {
3748//       F_minus[F_minus_index++]=m[i];
3749//       m[i]=NULL;
3750//     }
3751//     else
3752//     {
3753//       F_plus[F_plus_index++]=m[i];
3754//       m[i]=NULL;
3755//     }
3756//   }
3757  //in this order F_minus will be automatically ascending sorted
3758  //to make this sure for foreign gauss
3759  //uncomment the following line
3760  //  qsort(F_minus, F_minus_index,sizeof(poly),pLmCmp_func);
3761  assume((F_plus_index+F_minus_index)==m_index);
3762  if (TEST_OPT_PROT)
3763    Print("%i]", F_plus_index);
3764  for(i=0;i<p_index;i++) 
3765    pDelete(&p[i]);
3766  omfree(p);
3767  p=NULL;
3768  omfree(m);
3769  m=NULL;
3770
3771  //the F_minus list must be cleared separately at the end
3772  mp_array_list** F_i;
3773  poly_array_list** F_m_i;
3774  F_i=&(c->F);
3775  F_m_i=&(c->F_minus);
3776
3777  while((*F_i)!=NULL)
3778  {
3779    assume((*F_m_i)!=NULL);
3780    F_i=(&((*F_i)->next));
3781    F_m_i=(&((*F_m_i)->next));
3782  }
3783  assume((*F_m_i)==NULL);
3784  //should resize the array to save memory
3785  //F and F_minus
3786  qsort(chosen,chosen_index,sizeof(monom_poly),monom_poly_crit);//important for simplify
3787  (*F_m_i)=(poly_array_list*) omalloc(sizeof(poly_array_list));
3788  (*F_m_i)->size=F_minus_index;
3789  (*F_m_i)->p=F_minus;
3790  (*F_m_i)->next=NULL;
3791  (*F_i)=(mp_array_list*) omalloc(sizeof(poly_array_list));
3792  (*F_i)->size=chosen_index;
3793  (*F_i)->mp=chosen;
3794  (*F_i)->next=NULL;
3795 
3796  if(F_plus_index>0)
3797  {
3798    int j;
3799    int* ibuf=(int*) omalloc(F_plus_index*sizeof(int));
3800    sorted_pair_node*** sbuf=(sorted_pair_node***) omalloc(F_plus_index*sizeof(sorted_pair_node**));
3801 
3802    for(j=0;j<F_plus_index;j++)
3803    {
3804      int len;
3805      poly p=F_plus[j];
3806   
3807    // delete buf[j];
3808    //remember to free res here
3809    //    p=redTailShort(p, c->strat);
3810      sbuf[j]=add_to_basis_ideal_quotient(p,-1,-1,c,ibuf+j);
3811   
3812    }
3813    int sum=0;
3814    for(j=0;j<F_plus_index;j++)
3815    {
3816      sum+=ibuf[j];
3817    }
3818    sorted_pair_node** big_sbuf=(sorted_pair_node**) omalloc(sum*sizeof(sorted_pair_node*));
3819    int partsum=0;
3820    for(j=0;j<F_plus_index;j++)
3821    {
3822      memmove(big_sbuf+partsum, sbuf[j],ibuf[j]*sizeof(sorted_pair_node*));
3823      omfree(sbuf[j]);
3824      partsum+=ibuf[j];
3825    }
3826
3827    qsort(big_sbuf,sum,sizeof(sorted_pair_node*),pair_better_gen2);
3828    c->apairs=merge(c->apairs,c->pair_top+1,big_sbuf,sum,c);
3829    c->pair_top+=sum;
3830    clean_top_of_pair_list(c);
3831    omfree(big_sbuf);
3832    omfree(sbuf);
3833    omfree(ibuf);
3834  }
3835  omfree(F_plus);
3836#ifdef TGB_DEBUG
3837  int z;
3838  for(z=1;z<=c->pair_top;z++)
3839  {
3840    assume(pair_better(c->apairs[z],c->apairs[z-1],c));
3841  }
3842#endif
3843
3844  return;
3845}
3846
3847
3848static int poly_crit(const void* ap1, const void* ap2){
3849  poly p1,p2;
3850  p1=*((poly*) ap1);
3851  p2=*((poly*)ap2);
3852
3853  int c=pLmCmp(p1,p2);
3854  if (c !=0) return c;
3855  int l1=pLength(p1);
3856  int l2=pLength(p2);
3857  if (l1<l2) return -1;
3858  if (l1>l2) return 1;
3859  return 0;
3860}
3861
3862ideal t_rep_gb(ring r,ideal arg_I, BOOLEAN F4_mode){
3863  if (TEST_OPT_PROT)
3864    if (F4_mode)
3865      PrintS("F4 Modus \n");
3866   
3867     
3868  //debug_Ideal=arg_debug_Ideal;
3869  //if (debug_Ideal) PrintS("DebugIdeal received\n");
3870  // Print("Idelems %i \n----------\n",IDELEMS(arg_I));
3871  ideal I=idCompactify(arg_I);
3872  if (IDELEMS(I)==0) return I;
3873  qsort(I->m,IDELEMS(I),sizeof(poly),poly_crit);
3874  //Print("Idelems %i \n----------\n",IDELEMS(I));
3875  calc_dat* c=(calc_dat*) omalloc(sizeof(calc_dat));
3876 
3877  c->r=currRing;
3878  c->is_homog=TRUE;
3879  {
3880    int hz;
3881    for(hz=0;hz<IDELEMS(I);hz++){
3882      assume(I->m[hz]!=NULL);
3883      int d=pTotaldegree(I->m[hz]);
3884      poly t=I->m[hz]->next;
3885      while(t)
3886      {
3887        if (d!=pTotaldegree(t,c->r))
3888        {
3889          c->is_homog=FALSE;
3890          break;
3891        }
3892        t=t->next;
3893      }
3894      if(!(c->is_homog)) break;
3895    }
3896  }
3897  //  Print("is homog:%d",c->is_homog);
3898  void* h;
3899  poly hp;
3900  int i,j;
3901  c->to_destroy=NULL;
3902  c->easy_product_crit=0;
3903  c->extended_product_crit=0;
3904  if (rField_is_Zp(c->r))
3905    c->is_char0=FALSE;
3906  else
3907    c->is_char0=TRUE;
3908  //not fully correct
3909
3910//(rChar()==0);
3911  c->F4_mode=F4_mode;
3912  c->reduction_steps=0;
3913  c->last_index=-1;
3914
3915  c->F=NULL;
3916  c->F_minus=NULL;
3917
3918  c->Rcounter=0;
3919
3920  c->soon_free=NULL;
3921
3922  c->tmp_lm=pOne();
3923
3924  c->normal_forms=0;
3925  c->current_degree=1;
3926 
3927  c->max_pairs=5*I->idelems();
3928 
3929  c->apairs=(sorted_pair_node**) omalloc(sizeof(sorted_pair_node*)*c->max_pairs);
3930  c->pair_top=-1;
3931
3932  int n=I->idelems();
3933  if (TEST_OPT_PROT)
3934    for (i=0;i<n;i++){
3935      wrp(I->m[i]);
3936      PrintS("\n");
3937    }
3938  i=0;
3939  c->n=0;
3940  c->T_deg=(int*) omalloc(n*sizeof(int));
3941  if((!(c->is_homog)) &&(pLexOrder))
3942    c->T_deg_full=(int*) omalloc(n*sizeof(int));
3943  else
3944    c->T_deg_full=NULL;
3945  c->tmp_pair_lm=(poly*) omalloc(n*sizeof(poly));
3946  c->tmp_spn=(sorted_pair_node**) omalloc(n*sizeof(sorted_pair_node*));
3947  lm_bin=omGetSpecBin(POLYSIZE + (r->ExpL_Size)*sizeof(long));
3948#ifdef HEAD_BIN
3949  c->HeadBin=omGetSpecBin(POLYSIZE + (currRing->ExpL_Size)*sizeof(long));
3950#endif
3951  /* omUnGetSpecBin(&(c->HeadBin)); */
3952  h=omalloc(n*sizeof(char*));
3953  c->states=(char**) h;
3954  h=omalloc(n*sizeof(int));
3955  c->lengths=(int*) h;
3956  h=omalloc(n*sizeof(int));
3957        c->gcd_of_terms=(poly*) omalloc(n*sizeof(poly));
3958  c->rep=(int*) h;
3959  c->short_Exps=(long*) omalloc(n*sizeof(long));
3960  c->S=idInit(n,1);
3961  c->strat=new skStrategy;
3962  c->strat->syzComp = 0;
3963  initBuchMoraCrit(c->strat);
3964  initBuchMoraPos(c->strat);
3965  c->strat->initEcart = initEcartBBA;
3966  c->strat->enterS = enterSBba;
3967  c->strat->sl = -1;
3968  i=n;
3969  i=1;
3970  /* initS(c->S,NULL,c->strat); */
3971/* intS start: */
3972  // i=((i+IDELEMS(c->S)+15)/16)*16;
3973  c->strat->ecartS=(intset)omAlloc(i*sizeof(int)); /*initec(i);*/
3974  c->strat->sevS=(unsigned long*)omAlloc0(i*sizeof(unsigned long));
3975  /*initsevS(i);*/
3976  c->strat->S_2_R=(int*)omAlloc0(i*sizeof(int));/*initS_2_R(i);*/
3977  c->strat->fromQ=NULL;
3978  c->strat->Shdl=idInit(1,1);
3979  c->strat->S=c->strat->Shdl->m;
3980  c->strat->lenS=(int*)omAlloc0(i*sizeof(int));
3981  if((c->is_char0)||((pLexOrder) &&(!c->is_homog)))
3982    c->strat->lenSw=(int*)omAlloc0(i*sizeof(int));
3983  else
3984    c->strat->lenSw=NULL;
3985  sorted_pair_node* si;
3986  assume(n>0);
3987  add_to_basis(I->m[0],-1,-1,c);
3988
3989  assume(c->strat->sl==c->strat->Shdl->idelems()-1);
3990  if(!(F4_mode))
3991  {
3992    for (i=1;i<n;i++)//the 1 is wanted, because first element is added to basis
3993    {
3994      //     add_to_basis(I->m[i],-1,-1,c);
3995      si=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
3996      si->i=-1;
3997      si->j=-1;
3998      si->expected_length=pLength(I->m[i]);
3999      si->deg=pTotaldegree(I->m[i]);
4000      if (!rField_is_Zp(c->r)){ 
4001        pCleardenom(I->m[i]);
4002      }
4003      si->lcm_of_lm=I->m[i];
4004     
4005      //      c->apairs[n-1-i]=si;
4006      c->apairs[n-i-1]=si;
4007      ++(c->pair_top);
4008    }
4009  }
4010  else
4011  {
4012     for (i=1;i<n;i++)//the 1 is wanted, because first element is added to basis
4013       add_to_basis(I->m[i],-1,-1,c);
4014  }
4015   
4016
4017  while(c->pair_top>=0)
4018  {
4019    if(F4_mode)
4020      go_on_F4(c);
4021    else
4022      go_on(c);
4023  }
4024  while(c->to_destroy)
4025  {
4026    pDelete(&(c->to_destroy->p));
4027    poly_list_node* old=c->to_destroy;
4028    c->to_destroy=c->to_destroy->next;
4029    omfree(old);
4030  }
4031  while(c->F)
4032  {
4033    for(i=0;i<c->F->size;i++){
4034      pDelete(&(c->F->mp[i].m));
4035    }
4036    omfree(c->F->mp);
4037    c->F->mp=NULL;
4038    mp_array_list* old=c->F;
4039    c->F=c->F->next;
4040    omfree(old);
4041  }
4042  while(c->F_minus)
4043  {
4044    for(i=0;i<c->F_minus->size;i++){
4045      pDelete(&(c->F_minus->p[i]));
4046    }
4047    omfree(c->F_minus->p);
4048    c->F_minus->p=NULL;
4049    poly_array_list* old=c->F_minus;
4050    c->F_minus=c->F_minus->next;
4051    omfree(old);
4052  }
4053  for(int z=0;z<c->n;z++){
4054    omfree(c->states[z]);
4055  }
4056  omfree(c->states);
4057  omfree(c->lengths);
4058  for(int z=0;z<c->n;z++)
4059  {
4060    pDelete(&c->tmp_pair_lm[z]);
4061    omfree(c->tmp_spn[z]);
4062  }
4063  omfree(c->tmp_pair_lm);
4064  omfree(c->tmp_spn);
4065 
4066  omfree(c->T_deg);
4067  if(c->T_deg_full)
4068    omfree(c->T_deg_full);
4069
4070  omFree(c->strat->ecartS);
4071  omFree(c->strat->sevS);
4072//   initsevS(i);
4073  omFree(c->strat->S_2_R);
4074   
4075
4076  omFree(c->strat->lenS);
4077
4078  if(c->strat->lenSw)  omFree(c->strat->lenSw);
4079
4080
4081
4082
4083  for(i=0;i<c->n;i++){
4084    if(c->gcd_of_terms[i])
4085      pDelete(&(c->gcd_of_terms[i]));
4086  }
4087  omfree(c->gcd_of_terms);
4088
4089  omfree(c->apairs);
4090  if (TEST_OPT_PROT)
4091  {
4092    Print("calculated %d NFs\n",c->normal_forms);
4093    Print("applied %i product crit, %i extended_product crit \n", c->easy_product_crit, c->extended_product_crit);
4094  }
4095  int deleted_form_c_s=0;
4096 
4097  for(i=0;i<=c->strat->sl;i++){
4098    if (!c->strat->S[i]) continue;
4099    BOOLEAN found=FALSE;
4100    for(j=0;j<c->n;j++){
4101      if (c->S->m[j]==c->strat->S[i]){
4102        found=TRUE;
4103        break;
4104      }
4105    }
4106    if(!found) pDelete(&c->strat->S[i]);
4107  }
4108//   for(i=0;i<c->n;i++){
4109//     if (c->rep[i]!=i){
4110// //       for(j=0;j<=c->strat->sl;j++){
4111// //   if(c->strat->S[j]==c->S->m[i]){
4112// //     c->strat->S[j]=NULL;
4113// //     break;
4114// //   }
4115// //       }
4116// //      PrintS("R_delete");
4117//       pDelete(&c->S->m[i]);
4118//     }
4119//   }
4120
4121//  qsort(c->S->m, c->n,sizeof(poly),pLmCmp_func);
4122  for(i=0;i<c->n;i++)
4123  {
4124    assume(c->S->m[i]!=NULL);
4125    for(j=0;j<c->n;j++)
4126    {
4127      if((c->S->m[j]==NULL)||(i==j)) 
4128        continue;
4129      assume(p_LmShortDivisibleBy(c->S->m[j],c->short_Exps[j],
4130                               c->S->m[i],~c->short_Exps[i],
4131                                   c->r)==p_LmDivisibleBy(c->S->m[j],
4132                                                        c->S->m[i],
4133                                                          c->r));
4134      if (p_LmShortDivisibleBy(c->S->m[j],c->short_Exps[j],
4135                               c->S->m[i],~c->short_Exps[i],
4136                                   c->r))
4137      {
4138        pDelete(&c->S->m[i]);
4139        break;
4140      }
4141    }
4142  }
4143  omfree(c->short_Exps);
4144  omfree(c->rep);
4145  for(i=0;i<I->idelems();i++)
4146  {
4147    I->m[i]=NULL;
4148  }
4149  idDelete(&I);
4150  I=c->S;
4151 
4152  IDELEMS(I)=c->n;
4153
4154  idSkipZeroes(I);
4155  for(i=0;i<=c->strat->sl;i++)
4156    c->strat->S[i]=NULL;
4157  id_Delete(&c->strat->Shdl,c->r);
4158  pDelete(&c->tmp_lm);
4159  omUnGetSpecBin(&lm_bin);
4160  delete c->strat;
4161  omfree(c);
4162
4163  return(I);
4164}
4165static void now_t_rep(const int & arg_i, const int & arg_j, calc_dat* c){
4166  int i,j;
4167  if (arg_i==arg_j){
4168    return;
4169  }
4170  if (arg_i>arg_j){
4171    i=arg_j;
4172    j=arg_i;
4173  } else {
4174    i=arg_i;
4175    j=arg_j;
4176  }
4177  c->states[j][i]=HASTREP;
4178}
4179static void soon_t_rep(const int& arg_i, const int& arg_j, calc_dat* c)
4180{
4181  assume(0<=arg_i);
4182  assume(0<=arg_j);
4183  assume(arg_i<c->n);
4184  assume(arg_j<c->n);
4185  int i,j;
4186  if (arg_i==arg_j){
4187    return;
4188  }
4189  if (arg_i>arg_j){
4190    i=arg_j;
4191    j=arg_i;
4192  } else {
4193    i=arg_i;
4194    j=arg_j;
4195  }
4196  if (!
4197      (c->states[j][i]==HASTREP))
4198    c->states[j][i]=SOONTREP;
4199}
4200static BOOLEAN has_t_rep(const int & arg_i, const  int & arg_j, calc_dat* state){
4201  assume(0<=arg_i);
4202  assume(0<=arg_j);
4203  assume(arg_i<state->n);
4204  assume(arg_j<state->n);
4205  if (arg_i==arg_j)
4206  {
4207    return (TRUE);
4208  }
4209  if (arg_i>arg_j)
4210  {
4211    return (state->states[arg_i][arg_j]==HASTREP);
4212  } else
4213  {
4214    return (state->states[arg_j][arg_i]==HASTREP);
4215  }
4216}
4217static int pLcmDeg(poly a, poly b)
4218{
4219  int i;
4220  int n=0;
4221  for (i=pVariables; i; i--)
4222  {
4223    n+=max( pGetExp(a,i), pGetExp(b,i));
4224  }
4225  return n;
4226
4227}
4228
4229
4230
4231static void shorten_tails(calc_dat* c, poly monom)
4232{
4233  return;
4234// BOOLEAN corr=lenS_correct(c->strat);
4235  for(int i=0;i<c->n;i++)
4236  {
4237    //enter tail
4238    if (c->rep[i]!=i) continue;
4239    if (c->S->m[i]==NULL) continue;
4240    poly tail=c->S->m[i]->next;
4241    poly prev=c->S->m[i];
4242    BOOLEAN did_something=FALSE;
4243    while((tail!=NULL)&& (pLmCmp(tail, monom)>=0))
4244    {
4245      if (p_LmDivisibleBy(monom,tail,c->r))
4246      {
4247        did_something=TRUE;
4248        prev->next=tail->next;
4249        tail->next=NULL;
4250        p_Delete(& tail,c->r);
4251        tail=prev;
4252        //PrintS("Shortened");
4253        c->lengths[i]--;
4254      }
4255      prev=tail;
4256      tail=tail->next;
4257    }
4258    if (did_something)
4259    {
4260      int new_pos;
4261      int q;
4262      q=pQuality(c->S->m[i],c,c->lengths[i]);
4263      new_pos=simple_posInS(c->strat,c->S->m[i],q);
4264
4265      int old_pos=-1;
4266      //assume new_pos<old_pos
4267      for (int z=0;z<=c->strat->sl;z++)
4268      {
4269        if (c->strat->S[z]==c->S->m[i])
4270        {
4271          old_pos=z;
4272          break;
4273        }
4274      }
4275      if (old_pos== -1)
4276        for (int z=new_pos-1;z>=0;z--)
4277        {
4278          if (c->strat->S[z]==c->S->m[i])
4279          {
4280            old_pos=z;
4281            break;
4282          }
4283        }
4284      assume(old_pos>=0);
4285      assume(new_pos<=old_pos);
4286      assume(pLength(c->strat->S[old_pos])==c->lengths[i]);
4287      c->strat->lenS[old_pos]=c->lengths[i];
4288      if (c->strat->lenSw)
4289        c->strat->lenSw[old_pos]=q;
4290
4291      if (new_pos<old_pos)
4292        move_forward_in_S(old_pos,new_pos,c->strat);
4293
4294      length_one_crit(c,i,c->lengths[i]);
4295    }
4296  }
4297}
4298static sorted_pair_node* pop_pair(calc_dat* c){
4299  clean_top_of_pair_list(c);
4300
4301  if(c->pair_top<0) return NULL;
4302  else return (c->apairs[c->pair_top--]);
4303}
4304static sorted_pair_node* top_pair(calc_dat* c){
4305  super_clean_top_of_pair_list(c);//yeah, I know, it's odd that I use a different proc here
4306
4307  if(c->pair_top<0) return NULL;
4308  else return (c->apairs[c->pair_top]);
4309}
4310static sorted_pair_node* quick_pop_pair(calc_dat* c){
4311  if(c->pair_top<0) return NULL;
4312  else return (c->apairs[c->pair_top--]);
4313}
4314static BOOLEAN no_pairs(calc_dat* c){
4315  clean_top_of_pair_list(c);
4316  return (c->pair_top==-1);
4317}
4318
4319
4320static void super_clean_top_of_pair_list(calc_dat* c){
4321  while((c->pair_top>=0)
4322  && (c->apairs[c->pair_top]->i>=0)
4323  && (good_has_t_rep(c->apairs[c->pair_top]->j, c->apairs[c->pair_top]->i,c)))
4324  {
4325
4326    free_sorted_pair_node(c->apairs[c->pair_top],c->r);
4327    c->pair_top--;
4328
4329  }
4330}
4331static void clean_top_of_pair_list(calc_dat* c){
4332  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))){
4333
4334    free_sorted_pair_node(c->apairs[c->pair_top],c->r);
4335    c->pair_top--;
4336
4337  }
4338}
4339static BOOLEAN state_is(calc_state state, const int & arg_i, const  int & arg_j, calc_dat* c){
4340  assume(0<=arg_i);
4341  assume(0<=arg_j);
4342  assume(arg_i<c->n);
4343  assume(arg_j<c->n);
4344  if (arg_i==arg_j)
4345  {
4346    return (TRUE);
4347  }
4348  if (arg_i>arg_j)
4349  {
4350    return (c->states[arg_i][arg_j]==state);
4351  }
4352  else return(c->states[arg_j][arg_i]==state);
4353}
4354static void free_sorted_pair_node(sorted_pair_node* s, ring r){
4355  if (s->i>=0)
4356    p_Delete(&s->lcm_of_lm,r);
4357  omfree(s);
4358}
4359static BOOLEAN pair_better(sorted_pair_node* a,sorted_pair_node* b, calc_dat* c){
4360  if (a->deg<b->deg) return TRUE;
4361  if (a->deg>b->deg) return FALSE;
4362
4363//  if (a->expected_length<b->expected_length) return TRUE;
4364  //  if (a->expected_length>b->expected_length) return FALSE;
4365  int comp=pLmCmp(a->lcm_of_lm, b->lcm_of_lm);
4366  if (comp==1) return FALSE;
4367  if (-1==comp) return TRUE;
4368  if (a->i+a->j<b->i+b->j) return TRUE;
4369   if (a->i+a->j>b->i+b->j) return FALSE;
4370  if (a->i<b->i) return TRUE;
4371  if (a->i>b->i) return FALSE;
4372  return TRUE;
4373}
4374
4375static int pair_better_gen(const void* ap,const void* bp){
4376
4377  sorted_pair_node* a=*((sorted_pair_node**)ap);
4378  sorted_pair_node* b=*((sorted_pair_node**)bp);
4379  assume(a->i>a->j);
4380  assume(b->i>b->j);
4381  if (a->deg<b->deg) return -1;
4382  if (a->deg>b->deg) return 1;
4383
4384
4385//  if (a->expected_length<b->expected_length) return -1;
4386  // if (a->expected_length>b->expected_length) return 1;
4387 int comp=pLmCmp(a->lcm_of_lm, b->lcm_of_lm);
4388 
4389  if (comp==1) return 1;
4390  if (-1==comp) return -1;
4391  if (a->i+a->j<b->i+b->j) return -1;
4392   if (a->i+a->j>b->i+b->j) return 1;
4393  if (a->i<b->i) return -1;
4394   if (a->i>b->i) return 1;
4395  return 0;
4396}
4397
4398
4399static poly gcd_of_terms(poly p, ring r){
4400  int max_g_0=0;
4401  assume(p!=NULL);
4402  int i;
4403  poly m=pOne();
4404  poly t;
4405  for (i=pVariables; i; i--)
4406  {
4407      pSetExp(m,i, pGetExp(p,i));
4408      if (max_g_0==0)
4409        if (pGetExp(m,i)>0)
4410          max_g_0=i;
4411  }
4412 
4413  t=p->next;
4414  while (t!=NULL){
4415   
4416    if (max_g_0==0) break;
4417    for (i=max_g_0; i; i--)
4418    {
4419      pSetExp(m,i, min(pGetExp(t,i),pGetExp(m,i)));
4420      if (max_g_0==i)
4421        if (pGetExp(m,i)==0)
4422          max_g_0=0;
4423      if ((max_g_0==0) && (pGetExp(m,i)>0)){
4424        max_g_0=i;
4425      }
4426    }
4427    t=t->next;
4428  }
4429  // for (i=pVariables;i;i--)
4430//   {
4431//     if(pGetExp(m,i)>0)
4432//       return m;
4433//   }
4434  if (max_g_0>0)
4435    return m;
4436  pDelete(&m);
4437  return NULL;
4438}
4439static inline BOOLEAN pHasNotCFExtended(poly p1, poly p2, poly m)
4440{
4441
4442  if (pGetComp(p1) > 0 || pGetComp(p2) > 0)
4443    return FALSE;
4444  int i = 1;
4445  loop
4446  {
4447    if ((pGetExp(p1, i)-pGetExp(m,i) >0) && (pGetExp(p2, i) -pGetExp(m,i)> 0))   return FALSE;
4448    if (i == pVariables)                                return TRUE;
4449    i++;
4450  }
4451}
4452
4453
4454//for impl reasons may return false if the the normal product criterion matches
4455static inline BOOLEAN extended_product_criterion(poly p1, poly gcd1, poly p2, poly gcd2, calc_dat* c){
4456  if(gcd1==NULL) return FALSE;
4457        if(gcd2==NULL) return FALSE;
4458        gcd1->next=gcd2; //may ordered incorrect
4459        poly m=gcd_of_terms(gcd1,c->r);
4460        gcd1->next=NULL;
4461        if (m==NULL) return FALSE;
4462
4463        BOOLEAN erg=pHasNotCFExtended(p1,p2,m);
4464        pDelete(&m);
4465        return erg;
4466}
4467static poly kBucketGcd(kBucket* b, ring r)
4468{
4469  int s=0;
4470  int i;
4471  poly m, n;
4472  BOOLEAN initialized=FALSE;
4473  for (i=MAX_BUCKET-1;i>=0;i--)
4474  { 
4475    if (b->buckets[i]!=NULL){
4476      if (!initialized){
4477        m=gcd_of_terms(b->buckets[i],r);
4478        initialized=TRUE;
4479        if (m==NULL) return NULL;
4480      }
4481      else
4482        {
4483          n=gcd_of_terms(b->buckets[i],r);
4484          if (n==NULL) {
4485            pDelete(&m);
4486            return NULL;   
4487          }
4488          n->next=m;
4489          poly t=gcd_of_terms(n,r);
4490          n->next=NULL;
4491          pDelete(&m);
4492          pDelete(&n);
4493          m=t;
4494          if (m==NULL) return NULL;
4495         
4496        }
4497    }
4498  }
4499  return m;
4500}
4501
4502
4503
4504
4505static inline int quality_of_pos_in_strat_S(int pos, calc_dat* c){
4506  if (c->strat->lenSw!=NULL) return c->strat->lenSw[pos];
4507  return c->strat->lenS[pos];
4508}
4509
4510static void multi_reduction_lls_trick(red_object* los, int losl,calc_dat* c,find_erg & erg){
4511  erg.expand=NULL;
4512  BOOLEAN swap_roles; //from reduce_by, to_reduce_u if fromS
4513  if(erg.fromS){
4514    if(pLmEqual(c->strat->S[erg.reduce_by],los[erg.to_reduce_u].p))
4515    {
4516      int i;
4517      int quality_a=quality_of_pos_in_strat_S(erg.reduce_by,c);
4518      int best=erg.to_reduce_u+1;
4519/*
4520      for (i=erg.to_reduce_u;i>=erg.to_reduce_l;i--){
4521        int qc=los[i].guess_quality(c);
4522        if (qc<quality_a){
4523          best=i;
4524          quality_a=qc;
4525        }
4526      }
4527      if(best!=erg.to_reduce_u+1){*/
4528      int qc;
4529      best=find_best(los,erg.to_reduce_l,erg.to_reduce_u,qc,c);
4530      if(qc<quality_a){
4531        los[best].flatten();
4532        int b_pos=kBucketCanonicalize(los[best].bucket);
4533        los[best].p=los[best].bucket->buckets[b_pos];
4534        qc==pQuality(los[best].bucket->buckets[b_pos],c);
4535        if(qc<quality_a){
4536          red_object h=los[erg.to_reduce_u];
4537          los[erg.to_reduce_u]=los[best];
4538          los[best]=h;
4539          swap_roles=TRUE;
4540        }
4541        else
4542          swap_roles=FALSE;
4543      }
4544      else{
4545       
4546        swap_roles=FALSE;
4547      }
4548 
4549    }
4550      else
4551    {
4552      if (erg.to_reduce_u>erg.to_reduce_l){
4553
4554        int i;
4555        int quality_a=quality_of_pos_in_strat_S(erg.reduce_by,c);
4556        int best=erg.to_reduce_u+1;
4557        int qc;
4558        best=find_best(los,erg.to_reduce_l,erg.to_reduce_u,qc,c);
4559        assume(qc==los[best].guess_quality(c));
4560        if(qc<quality_a){
4561          los[best].flatten();
4562          int b_pos=kBucketCanonicalize(los[best].bucket);
4563          los[best].p=los[best].bucket->buckets[b_pos];
4564          qc==pQuality(los[best].bucket->buckets[b_pos],c);
4565          //(best!=erg.to_reduce_u+1)
4566          if(qc<quality_a){
4567          red_object h=los[erg.to_reduce_l];
4568          los[erg.to_reduce_l]=los[best];
4569          los[best]=h;
4570          erg.reduce_by=erg.to_reduce_l;
4571          erg.fromS=FALSE;
4572          erg.to_reduce_l++;
4573          }
4574        }
4575      }
4576      else 
4577      {
4578        assume(erg.to_reduce_u==erg.to_reduce_l);
4579        int quality_a=quality_of_pos_in_strat_S(erg.reduce_by,c);
4580        int qc=los[erg.to_reduce_u].guess_quality(c);
4581        if(qc<quality_a){
4582          int best=erg.to_reduce_u;
4583          los[best].flatten();
4584          int b_pos=kBucketCanonicalize(los[best].bucket);
4585          los[best].p=los[best].bucket->buckets[b_pos];
4586          qc==pQuality(los[best].bucket->buckets[b_pos],c);
4587          if(qc<quality_a){
4588            BOOLEAN exp=FALSE;
4589            if(qc<=2)
4590              exp=TRUE;
4591            else {
4592              if (qc<quality_a/2)
4593                exp=TRUE;
4594              else
4595                if(erg.reduce_by<c->n/4)
4596                  exp=TRUE;
4597            }
4598            if (exp){
4599              poly clear_into;
4600              los[erg.to_reduce_u].flatten();
4601              kBucketClear(los[erg.to_reduce_u].bucket,&clear_into,&erg.expand_length);
4602              erg.expand=pCopy(clear_into);
4603              kBucketInit(los[erg.to_reduce_u].bucket,clear_into,erg.expand_length);
4604              if (TEST_OPT_PROT)
4605                PrintS("e");
4606             
4607            }
4608          }
4609        }
4610
4611       
4612      }
4613     
4614      swap_roles=FALSE;
4615      return;
4616      }
4617   
4618  }
4619  else{
4620    if(erg.reduce_by>erg.to_reduce_u){
4621      //then lm(rb)>= lm(tru) so =
4622      assume(erg.reduce_by==erg.to_reduce_u+1);
4623      int best=erg.reduce_by;
4624      int quality_a=los[erg.reduce_by].guess_quality(c);
4625      int qc;
4626      best=find_best(los,erg.to_reduce_l,erg.to_reduce_u,qc,c);
4627     
4628      int i;
4629      if(qc<quality_a){
4630          red_object h=los[erg.reduce_by];
4631          los[erg.reduce_by]=los[best];
4632          los[best]=h;
4633        }
4634        swap_roles=FALSE;
4635        return;
4636       
4637         
4638    }
4639    else
4640    {
4641      assume(!pLmEqual(los[erg.reduce_by].p,los[erg.to_reduce_l].p));
4642      assume(erg.to_reduce_u==erg.to_reduce_l);
4643      //further assume, that reduce_by is the above all other polys
4644      //with same leading term
4645      int il=erg.reduce_by;
4646      int quality_a =los[erg.reduce_by].guess_quality(c);
4647      int qc;
4648      while((il>0) && pLmEqual(los[il-1].p,los[il].p)){
4649        il--;
4650        qc=los[il].guess_quality(c);
4651        if (qc<quality_a){
4652          quality_a=qc;
4653          erg.reduce_by=il;
4654        }
4655      }
4656      swap_roles=FALSE;
4657    }
4658 
4659  }
4660  if(swap_roles)
4661  {
4662    if (TEST_OPT_PROT)
4663      PrintS("b");
4664    poly clear_into;
4665    int dummy_len;
4666    int new_length;
4667    int bp=erg.to_reduce_u;//bucket_positon
4668    //kBucketClear(los[bp].bucket,&clear_into,&new_length);
4669    new_length=los[bp].clear_to_poly();
4670    clear_into=los[bp].p;
4671    poly p=c->strat->S[erg.reduce_by];
4672    int j=erg.reduce_by;
4673    int old_length=c->strat->lenS[j];// in view of S
4674    los[bp].p=p;
4675    kBucketInit(los[bp].bucket,p,old_length);
4676    int qal=pQuality(clear_into,c,new_length);
4677    int pos_in_c=-1;   
4678    int z;
4679    int new_pos;
4680    new_pos=simple_posInS(c->strat,clear_into,qal);
4681    assume(new_pos<=j);
4682    for (z=c->n;z;z--)
4683    {
4684      if(p==c->S->m[z-1])
4685      {
4686        pos_in_c=z-1;
4687        break;
4688      }
4689    }
4690    if(pos_in_c>=0)
4691    {
4692      c->S->m[pos_in_c]=clear_into;
4693      c->lengths[pos_in_c]=new_length;
4694      if (c->T_deg_full)
4695        c->T_deg_full[pos_in_c]=pTotaldegree_full(clear_into);
4696      c_S_element_changed_hook(pos_in_c,c);
4697    }
4698    c->strat->S[j]=clear_into;
4699    c->strat->lenS[j]=new_length;
4700    assume(pLength(clear_into)==new_length);
4701    if(c->strat->lenSw)
4702      c->strat->lenSw[j]=qal;
4703    if (!rField_is_Zp(c->r))
4704    {
4705      pContent(clear_into);
4706      pCleardenom(clear_into);//should be unnecessary
4707    }
4708    else                     
4709      pNorm(clear_into);
4710#ifdef FIND_DETERMINISTIC
4711    erg.reduce_by=j;
4712    //resort later see diploma thesis, find_in_S must be deterministic
4713    //during multireduction if spolys are only in the span of the
4714    //input polys
4715#else
4716   
4717    if (new_pos<j)
4718    { 
4719      move_forward_in_S(j,new_pos,c->strat);
4720      erg.reduce_by=new_pos;
4721    }
4722#endif
4723  }
4724}
4725static int fwbw(red_object* los, int i){
4726   int i2=i;
4727   int step=1;
4728   
4729   BOOLEAN bw=FALSE;
4730   BOOLEAN incr=TRUE;
4731   
4732   while(1)
4733   {
4734     if(!bw)
4735     {
4736       step=min(i2,step);
4737       if (step==0) break;
4738       i2-=step;
4739         
4740       if(!pLmEqual(los[i].p,los[i2].p))
4741       {
4742         bw=TRUE;
4743         incr=FALSE;
4744       }
4745       else
4746       {
4747         if ((!incr) &&(step==1)) break;
4748       }
4749       
4750       
4751     }
4752     else
4753     {
4754       
4755       step=min(i-i2,step);
4756       if (step==0) break;
4757       i2+=step;
4758       if(pLmEqual(los[i].p,los[i2].p)){
4759         if(step==1) break;
4760         else
4761         {
4762           bw=FALSE;
4763         }
4764       }
4765       
4766     }
4767     if (incr)
4768       step*=2;
4769     else
4770     {
4771       if (step%2==1)
4772         step=(step+1)/2;
4773       else
4774         step/=2;
4775       
4776     }
4777   }
4778   return i2;
4779}
4780static void multi_reduction_find(red_object* los, int losl,calc_dat* c,int startf,find_erg & erg){
4781  kStrategy strat=c->strat;
4782
4783  assume(startf<=losl);
4784  assume((startf==losl-1)||(pLmCmp(los[startf].p,los[startf+1].p)==-1));
4785  int i=startf;
4786 
4787  int j;
4788  while(i>=0){
4789    assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)<=0));
4790    assume(is_valid_ro(los[i]));
4791    j=kFindDivisibleByInS_easy(strat,los[i]);
4792    if(j>=0){
4793     
4794      erg.to_reduce_u=i;
4795      erg.reduce_by=j;
4796      erg.fromS=TRUE;
4797      int i2=fwbw(los,i);
4798      assume(pLmEqual(los[i].p,los[i2].p));
4799      assume((i2==0)||(!pLmEqual(los[i2].p,los[i2-1].p)));
4800      assume(i>=i2);
4801
4802//       int i2;
4803//       for(i2=i-1;i2>=0;i2--){
4804//      if(!pLmEqual(los[i].p,los[i2].p))
4805//        break;
4806//      }
4807     
4808//      erg.to_reduce_l=i2+1;
4809      erg.to_reduce_l=i2;
4810      assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)==-1));
4811      return;
4812    }
4813    if (j<0){
4814     
4815      //not reduceable, try to use this for reducing higher terms
4816      int i2=fwbw(los,i);
4817      assume(pLmEqual(los[i].p,los[i2].p));
4818      assume((i2==0)||(!pLmEqual(los[i2].p,los[i2-1].p)));
4819      assume(i>=i2);
4820      if(i2!=i){
4821       
4822        erg.to_reduce_u=i-1;
4823        erg.to_reduce_l=i2;
4824        erg.reduce_by=i;
4825        erg.fromS=FALSE;
4826        assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)==-1));
4827        return;
4828      }
4829      if(!(c->is_homog))
4830      {
4831
4832        for (i2=i+1;i2<losl;i2++){
4833          if (p_LmShortDivisibleBy(los[i].p,los[i].sev,los[i2].p,~los[i2].sev,
4834                                   c->r)){
4835            int i3=i2;
4836            while((i3+1<losl) && (pLmEqual(los[i2].p, los[i3+1].p)))
4837              i3++;
4838            erg.to_reduce_u=i3;
4839            erg.to_reduce_l=i2;
4840            erg.reduce_by=i;
4841            erg.fromS=FALSE;
4842            assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)==-1));
4843            return;
4844          }
4845          //    else {assume(!p_LmDivisibleBy(los[i].p, los[i2].p,c->r));}
4846        }
4847      }
4848      i--;
4849    }
4850  }
4851  erg.reduce_by=-1;//error code
4852  return;
4853}
4854
4855 //  nicht reduzierbare eintraege in ergebnisliste schreiben
4856//   nullen loeschen
4857//   while(finde_groessten leitterm reduzierbar(c,erg)){
4858 
4859static int multi_reduction_clear_zeroes(red_object* los, int  losl, int l, int u)
4860{
4861
4862
4863  int deleted=0;
4864  int  i=l;
4865  int last=-1;
4866  while(i<=u)
4867  {
4868   
4869    if(los[i].p==NULL){
4870      kBucketDestroy(&los[i].bucket);
4871//      delete los[i];//here we assume los are constructed with new
4872      //destroy resources, must be added here   
4873     if (last>=0)
4874     {
4875       memmove(los+(int)(last+1-deleted),los+(last+1),sizeof(red_object)*(i-1-last));
4876     }
4877     last=i;
4878     deleted++;
4879    }
4880    i++;
4881  }
4882  if((last>=0)&&(last!=losl-1))
4883      memmove(los+(int)(last+1-deleted),los+last+1,sizeof(red_object)*(losl-1-last));
4884  return deleted;
4885 
4886}
4887
4888static void sort_region_down(red_object* los, int l, int u, calc_dat* c)
4889{
4890  qsort(los+l,u-l+1,sizeof(red_object),red_object_better_gen);
4891  int i;
4892
4893  for(i=l;i<=u;i++)
4894  {
4895    BOOLEAN moved=FALSE;
4896    int j;
4897    for(j=i;j;j--)
4898    {
4899      if(pLmCmp(los[j].p,los[j-1].p)==-1){
4900        red_object h=los[j];
4901        los[j]=los[j-1];
4902        los[j-1]=h;
4903        moved=TRUE;
4904      }
4905      else break;
4906    }
4907    if(!moved) return;
4908  }
4909}
4910
4911//assume that los is ordered ascending by leading term, all non zero
4912static void multi_reduction(red_object* los, int & losl, calc_dat* c)
4913{
4914 
4915  //initialize;
4916  assume(c->strat->sl>=0);
4917  assume(losl>0);
4918  int i;
4919  for(i=0;i<losl;i++){
4920    los[i].sev=pGetShortExpVector(los[i].p);
4921//SetShortExpVector();
4922    los[i].p=kBucketGetLm(los[i].bucket);
4923  }
4924
4925  kStrategy strat=c->strat;
4926  int curr_pos=losl-1;
4927
4928
4929//  nicht reduzierbare einträge in ergebnisliste schreiben
4930  // nullen loeschen
4931  while(curr_pos>=0){
4932   
4933    find_erg erg;
4934    multi_reduction_find(los, losl,c,curr_pos,erg);//last argument should be curr_pos
4935   //  PrintS("\n erg:\n");
4936//     Print("upper:%i\n",erg.to_reduce_u);
4937//     Print("lower:%i\n",erg.to_reduce_l);
4938//     Print("reduce_by:%i\n",erg.reduce_by);
4939//     Print("fromS:%i\n",erg.fromS);
4940    if(erg.reduce_by<0) break;
4941
4942
4943
4944    erg.expand=NULL;
4945    int d=erg.to_reduce_u-erg.to_reduce_l+1;
4946    //if ((!erg.fromS)&&(d>100)){
4947    if (0){
4948      PrintS("L");
4949      if(!erg.fromS){
4950        erg.to_reduce_u=max(erg.to_reduce_u,erg.reduce_by);
4951        if (pLmEqual(los[erg.reduce_by].p,los[erg.to_reduce_l].p))
4952          erg.to_reduce_l=min(erg.to_reduce_l,erg.reduce_by);
4953      }
4954      int pn=erg.to_reduce_u+1-erg.to_reduce_l;
4955      poly* p=(poly*) omalloc((pn)*sizeof(poly));
4956      int i;
4957      for(i=0;i<pn;i++){
4958        int len;
4959        los[erg.to_reduce_l+i].flatten();
4960        kBucketClear(los[erg.to_reduce_l+i].bucket,&p[i],&len);
4961       
4962        redTailShort(p[i],c->strat);
4963      }
4964      pre_comp(p,pn,c);
4965      int j;
4966      for(j=0;j<pn;j++){
4967        los[erg.to_reduce_l+j].p=p[j];
4968        los[erg.to_reduce_l+j].sev=pGetShortExpVector(p[j]);
4969        los[erg.to_reduce_l+j].sum=NULL;
4970        int len=pLength(p[j]);
4971        kBucketInit(los[erg.to_reduce_l+j].bucket,los[erg.to_reduce_l+j].p,len);
4972      }
4973      for(j=erg.to_reduce_l+pn;j<=erg.to_reduce_u;j++){
4974        los[j].p=NULL;
4975       
4976      }
4977
4978      omfree(p);
4979    }
4980    else {
4981    multi_reduction_lls_trick(los,losl,c,erg);
4982    int sum=0;
4983
4984   
4985    int i;
4986    int len;
4987    //    wrp(los[erg.to_reduce_u].p);
4988    //Print("\n");
4989    multi_reduce_step(erg,los,c);
4990    }
4991//     reduction_step *rs=create_reduction_step(erg, los, c);
4992//     rs->reduce(los,erg.to_reduce_l,erg.to_reduce_u);
4993//     finalize_reduction_step(rs);
4994                 
4995    int deleted=multi_reduction_clear_zeroes(los, losl, erg.to_reduce_l, erg.to_reduce_u);
4996    if(erg.fromS==FALSE)
4997      curr_pos=max(erg.to_reduce_u,erg.reduce_by);
4998    else
4999      curr_pos=erg.to_reduce_u;
5000    losl -= deleted;
5001    curr_pos -= deleted;
5002
5003    //Print("deleted %i \n",deleted);
5004    sort_region_down(los, erg.to_reduce_l, erg.to_reduce_u-deleted, c);
5005//   sort_region_down(los, 0, losl-1, c);
5006    //  qsort(los,losl,sizeof(red_object),red_object_better_gen);
5007    if(erg.expand)
5008    {
5009#ifdef FIND_DETERMINISTIC
5010      int i;
5011      for(i=0;c->expandS[i];i++);
5012      c->expandS=(poly*) omrealloc(c->expandS,(i+2)*sizeof(poly));
5013      c->expandS[i]=erg.expand;
5014      c->expandS[i+1]=NULL;
5015#else
5016      add_to_reductors(c,erg.expand,erg.expand_length);
5017#endif
5018    }
5019     
5020  }
5021  return;
5022}
5023void red_object::flatten(){
5024  if (sum!=NULL)
5025  {
5026
5027 
5028    if(kBucketGetLm(sum->ac->bucket)!=NULL){
5029      number mult_my=n_Mult(sum->c_my,sum->ac->multiplied,currRing);
5030      number mult_my_h=mult_my;
5031      number ac_h=sum->c_ac;
5032      ksCheckCoeff(&mult_my_h, &ac_h);
5033      nDelete(&sum->c_ac);
5034      nDelete(&mult_my);
5035      mult_my=mult_my_h;
5036      sum->c_ac=ac_h;
5037      poly add_this;
5038      if(!nIsOne(mult_my))
5039        kBucket_Mult_n(bucket,mult_my);
5040      int len;
5041      poly clear_into;
5042      kBucketClear(sum->ac->bucket,&clear_into,&len);
5043      if(sum->ac->counter>1){
5044        add_this=pCopy(clear_into);
5045        kBucketInit(sum->ac->bucket,clear_into,len);
5046      }
5047      else
5048        add_this=clear_into;
5049      if(!nIsOne(sum->c_ac))
5050        pMult_nn(add_this, sum->c_ac);
5051     
5052      kBucket_Add_q(bucket,add_this, &len);
5053       nDelete(&mult_my);
5054     
5055    }
5056    nDelete(&sum->c_ac);
5057    nDelete(&sum->c_my);
5058   
5059    sum->ac->decrease_counter();
5060    delete sum;
5061    p=kBucketGetLm(bucket);
5062    sum=NULL;
5063  }
5064  assume(p==kBucketGetLm(bucket));
5065  assume(sum==NULL);
5066}
5067void red_object::validate(){
5068  BOOLEAN flattened=FALSE;
5069  if(sum!=NULL)
5070  {
5071    poly lm=kBucketGetLm(bucket);
5072    poly lm_ac=kBucketGetLm(sum->ac->bucket);
5073    if ((lm_ac==NULL)||((lm!=NULL) && (pLmCmp(lm,lm_ac)!=-1))){
5074      flatten();
5075      assume(sum==NULL);
5076      flattened=TRUE;
5077      p=kBucketGetLm(bucket);
5078      if (p!=NULL)
5079        sev=pGetShortExpVector(p);
5080    } 
5081    else
5082    {
5083 
5084      p=lm_ac;
5085      assume(sum->ac->sev==pGetShortExpVector(p));
5086      sev=sum->ac->sev;
5087    }
5088   
5089  }
5090  else{
5091    p=kBucketGetLm(bucket);
5092    if(p)
5093    sev=pGetShortExpVector(p);
5094  }
5095  assume((sum==NULL)||(kBucketGetLm(sum->ac->bucket)!=NULL));
5096}
5097int red_object::clear_to_poly(){
5098  flatten();
5099  int l;
5100  kBucketClear(bucket,&p,&l);
5101  return l;
5102}
5103
5104 
5105
5106void red_object::adjust_coefs(number c_r, number c_ac_r){
5107  assume(this->sum!=NULL);
5108  number n1=nMult(sum->c_my, c_ac_r);
5109  number n2=nMult(sum->c_ac,c_r);
5110  nDelete(&sum->c_my);
5111  nDelete(&sum->c_ac);
5112 
5113  int ct = ksCheckCoeff(&n1, &n2);
5114  sum->c_my=n1;
5115  sum->c_ac=nNeg(n2);
5116  //  nDelete(&n2);
5117 
5118
5119}
5120
5121void reduction_step::reduce(red_object* r, int l, int u){}
5122void simple_reducer::target_is_no_sum_reduce(red_object & ro){
5123  kBucketPolyRed(ro.bucket,p,
5124                 p_len,
5125                 c->strat->kNoether);
5126}
5127
5128void simple_reducer::target_is_a_sum_reduce(red_object & ro){
5129  pTest(p);
5130  kbTest(ro.bucket);
5131  kbTest(ro.sum->ac->bucket);
5132  assume(ro.sum!=NULL);
5133  assume(ro.sum->ac!=NULL);
5134  if(ro.sum->ac->last_reduction_id!=reduction_id){
5135    number n1=kBucketPolyRed(ro.sum->ac->bucket,p, p_len, c->strat->kNoether);
5136    number n2=nMult(n1,ro.sum->ac->multiplied);
5137    nDelete(&ro.sum->ac->multiplied);
5138    nDelete(&n1);
5139    ro.sum->ac->multiplied=n2;
5140    poly lm=kBucketGetLm(ro.sum->ac->bucket);
5141    if (lm)
5142      ro.sum->ac->sev=pGetShortExpVector(lm);
5143    ro.sum->ac->last_reduction_id=reduction_id;
5144  }
5145  ro.sev=ro.sum->ac->sev;
5146  ro.p=kBucketGetLm(ro.sum->ac->bucket);
5147}
5148void simple_reducer::reduce(red_object* r, int l, int u){
5149  this->pre_reduce(r,l,u);
5150  int i;
5151//debug start
5152  int im;
5153//  for(im=l;im<=u;im++)
5154  //  assume(is_valid_ro(r[im]));
5155 
5156
5157//debug end
5158
5159  for(i=l;i<=u;i++){
5160 
5161
5162    if(r[i].sum==NULL)
5163      this->target_is_no_sum_reduce(r[i]);
5164
5165    else 
5166    {
5167      this->target_is_a_sum_reduce(r[i]);
5168      //reduce and adjust multiplied
5169      r[i].sum->ac->last_reduction_id=reduction_id;
5170     
5171    }
5172    //most elegant would be multimethods at this point and subclassing
5173    //red_object for sum
5174 
5175  }
5176  for(i=l;i<=u;i++){
5177    r[i].validate();
5178    #ifdef TGB_DEBUG
5179    if (r[i].sum) r[i].sev=r[i].sum->ac->sev;
5180
5181    #endif
5182      }
5183}
5184reduction_step::~reduction_step(){}
5185simple_reducer::~simple_reducer(){
5186  if(fill_back!=NULL)
5187  {
5188    kBucketInit(fill_back,p,p_len);
5189  }
5190  fill_back=NULL;
5191   
5192}
5193 join_simple_reducer::~join_simple_reducer(){
5194   if(fill_back!=NULL)
5195   {
5196     kBucketInit(fill_back,p,p_len);
5197   }
5198   fill_back=NULL;
5199   
5200}
5201void multi_reduce_step(find_erg & erg, red_object* r, calc_dat* c){
5202  static int id=0;
5203  id++;
5204
5205  int rn=erg.reduce_by;
5206  poly red;
5207  int red_len;
5208  simple_reducer* pointer;
5209  BOOLEAN woc=FALSE;
5210  if(erg.fromS){
5211    red=c->strat->S[rn];
5212    red_len=c->strat->lenS[rn];
5213    assume(red_len==pLength(red));
5214  }
5215  else
5216  {
5217    r[rn].flatten();
5218    kBucketClear(r[rn].bucket,&red,&red_len);
5219    if (!rField_is_Zp(c->r))
5220    {
5221      pContent(red);
5222      pCleardenom(red);//should be unnecessary
5223     
5224    }
5225    pNormalize(red);
5226    red_len=pLength(red);
5227  }
5228  if (erg.to_reduce_u-erg.to_reduce_l>5){
5229    woc=TRUE;
5230    // poly m=pOne();
5231    poly m=c->tmp_lm;
5232    pSetCoeff(m,nInit(1));
5233    for(int i=1;i<=pVariables;i++)
5234      pSetExp(m,i,(pGetExp(r[erg.to_reduce_l].p, i)-pGetExp(red,i)));
5235    pSetm(m);
5236    poly red_cp=ppMult_mm(red,m);
5237   
5238    if(!erg.fromS){
5239      kBucketInit(r[rn].bucket,red,red_len);
5240    }
5241    //now reduce the copy
5242    //static poly redNF2 (poly h,calc_dat* c , int &len, number&  m,int n)
5243    redTailShort(red_cp,c->strat);
5244    //number mul;
5245    // red_len--;
5246//     red_cp->next=redNF2(red_cp->next,c,red_len,mul,c->average_length);
5247//     pSetCoeff(red_cp,nMult(red_cp->coef,mul));
5248//     nDelete(&mul);
5249//     red_len++;
5250    red=red_cp;
5251    red_len=pLength(red);
5252    // pDelete(&m);
5253   
5254  }
5255  int i;
5256
5257
5258  int red_c=0;
5259  if(red_len>2*c->average_length){
5260    for(i=erg.to_reduce_l;i<=erg.to_reduce_u;i++){
5261      if((r[i].sum==NULL) ||(r[i].sum->ac->counter<=AC_FLATTEN)) red_c++;
5262    }
5263  }
5264  assume(red_len==pLength(red));
5265  if (red_c>=AC_NEW_MIN)
5266    pointer=new join_simple_reducer(red,red_len,r[erg.to_reduce_l].p);
5267  else
5268    pointer=new simple_reducer(red,red_len,c);
5269
5270  if ((!woc) && (!erg.fromS))
5271    pointer->fill_back=r[rn].bucket;
5272  else
5273    pointer->fill_back=NULL;
5274  pointer->reduction_id=id;
5275  pointer->c=c;
5276
5277  pointer->reduce(r,erg.to_reduce_l, erg.to_reduce_u);
5278  if(woc) pDelete(&pointer->p);
5279  delete pointer;
5280 
5281};
5282
5283void join_simple_reducer::target_is_no_sum_reduce(red_object & ro){
5284  kbTest(ro.bucket);
5285  ro.sum=new formal_sum_descriptor();
5286  ro.sum->ac=ac;
5287  ac->counter++;
5288  kBucket_pt bucket=ro.bucket;
5289  poly a1 = pNext(p), lm = kBucketExtractLm(bucket);
5290  BOOLEAN reset_vec=FALSE;
5291  number rn;
5292  assume(a1!=NULL);
5293  number an = pGetCoeff(p), bn = pGetCoeff(lm);
5294  lm->next=NULL;
5295  int ct = ksCheckCoeff(&an, &bn);
5296  ro.sum->c_ac=nNeg(bn);
5297  ro.sum->c_my=an;
5298  assume(nIsZero(nAdd(nMult(ro.sum->c_my,lm->coef),nMult(p->coef,ro.sum->c_ac) )));
5299  if (p_GetComp(p, bucket->bucket_ring) != p_GetComp(lm, bucket->bucket_ring))
5300  {
5301    p_SetCompP(a1, p_GetComp(lm, bucket->bucket_ring), bucket->bucket_ring);
5302    reset_vec = TRUE;
5303    p_SetComp(lm, p_GetComp(p, bucket->bucket_ring), bucket->bucket_ring);
5304    p_Setm(lm, bucket->bucket_ring);
5305  }
5306
5307
5308 
5309
5310  p_DeleteLm(&lm, bucket->bucket_ring);
5311  if (reset_vec) p_SetCompP(a1, 0, bucket->bucket_ring);
5312  kbTest(bucket);
5313
5314}
5315
5316  reduction_accumulator::reduction_accumulator(poly p, int p_len, poly high_to){
5317    //sev needs to be removed from interfaces,makes no sense
5318   
5319
5320    poly my=pOne();
5321    counter=0;
5322   
5323    for(int i=1;i<=pVariables;i++)
5324      pSetExp(my,i,(pGetExp(high_to, i)-pGetExp(p,i)));
5325    pSetm(my);
5326
5327    last_reduction_id=-1;
5328    multiplied=nInit(1);
5329    bucket=kBucketCreate(currRing);
5330    poly a=pMult_mm(pCopy(p->next),my);
5331
5332    this->sev=pGetShortExpVector(a);
5333    kBucketInit(bucket, a,p_len-1);
5334    pDelete(&my);
5335  }
5336void simple_reducer:: pre_reduce(red_object* r, int l, int u){}
5337void join_simple_reducer:: pre_reduce(red_object* r, int l, int u){
5338  for(int i=l;i<=u;i++)
5339    {
5340      if (r[i].sum){
5341        if(r[i].sum->ac->counter<=AC_FLATTEN) r[i].flatten();
5342       
5343      }
5344    }
5345}
Note: See TracBrowser for help on using the repository browser.