source: git/kernel/tgb.cc @ b17a5c

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