source: git/Singular/tgb.cc @ 7256f9

spielwiese
Last change on this file since 7256f9 was 7256f9, checked in by Michael Brickenstein <bricken@…>, 21 years ago
+ bugfixing git-svn-id: file:///usr/local/Singular/svn/trunk@6391 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 50.9 KB
Line 
1
2// #define OM_CHECK 3
3// #define OM_TRACK 5
4// #define OM_KEEP  1
5
6#include "tgb.h"
7static int posInPairs (sorted_pair_node**  p, int pn, sorted_pair_node* qe,calc_dat* c,int an=0)
8{
9  if(pn==0) return 0;
10
11  int length=pn-1;
12  int i;
13  //int an = 0;
14  int en= length;
15
16  if (pair_better(qe,p[en],c))
17    return length+1;
18
19  while(1)
20    {
21      //if (an >= en-1)
22      if(en-1<=an)
23      {
24        if (pair_better(p[an],qe,c)) return an;
25        return en;
26      }
27      i=(an+en) / 2;
28        if (pair_better(p[i],qe,c))
29          en=i;
30      else an=i;
31    }
32}
33sorted_pair_node**  merge(sorted_pair_node** p, int pn,sorted_pair_node **q, int qn,calc_dat* c){
34  int i;
35  int* a= (int*) omalloc(qn*sizeof(int));
36 
37  int lastpos=0;
38  for(i=0;i<qn;i++){
39    lastpos=posInPairs(p,pn-1,q[i],c, max(lastpos-1,0));
40    //   cout<<lastpos<<"\n";
41    a[i]=lastpos;
42
43  }
44  p=(sorted_pair_node**) realloc(p,(pn+qn)*sizeof(sorted_pair_node*));
45  for(i=qn-1;i>=0;i--){
46    size_t size;
47    if(qn-1>i)
48      size=(a[i+1]-a[i])*sizeof(sorted_pair_node*);
49    else
50      size=(pn-a[i])*sizeof(sorted_pair_node*); //as indices begin with 0
51    memmove (p+a[i]+(1+i), p+a[i], size);
52    p[a[i]+i]=q[i];
53  }
54  omfree(a);
55  return p;
56}
57static BOOLEAN trivial_syzygie(int pos1,int pos2,poly bound,calc_dat* c){
58
59
60  poly f1=c->S->m[pos1];
61  poly f2=c->S->m[pos2];
62  ring r=c->r;
63 
64  int i=r->VarL_Size - 1;
65  unsigned long divmask = r->divmask;
66  unsigned long la, lb;
67
68  if (r->VarL_LowIndex >= 0)
69  {
70    i += r->VarL_LowIndex;
71    do
72    {
73      la = f1->exp[i]+f2->exp[i];
74      lb = bound->exp[i];
75      if ((la > lb) ||
76          (((la & divmask) ^ (lb & divmask)) != ((lb - la) & divmask)))
77      {
78//        pDivAssume(p_DebugLmDivisibleByNoComp(a, b, r) == FALSE);
79        return FALSE;
80      }
81      i--;
82    }
83    while (i>=r->VarL_LowIndex);
84  }
85  else
86  {
87    do
88    {
89      la = f1->exp[r->VarL_Offset[i]]+f2->exp[r->VarL_Offset[i]];
90      lb = bound->exp[r->VarL_Offset[i]];
91      if ((la > lb) ||
92          (((la & divmask) ^ (lb & divmask)) != ((lb - la) & divmask)))
93      {
94        //     pDivAssume(p_DebugLmDivisibleByNoComp(a, b, r) == FALSE);
95        return FALSE;
96      }
97      i--;
98    }
99    while (i>=0);
100  }
101//  pDivAssume(p_DebugLmDivisibleByNoComp(a, b, r) == TRUE);
102  PrintS("trivial");
103  return TRUE;
104
105}
106BOOLEAN good_has_t_rep(int i, int j,calc_dat* c){
107  assume(i>=0);
108    assume(j>=0);
109  if (has_t_rep(i,j,c)) return TRUE;
110  poly lm=pOne();
111
112  pLcm(c->S->m[i], c->S->m[j], lm);
113  pSetm(lm);
114  int deciding_deg= pFDeg(lm);
115  int* i_con =make_connections(i,j,lm,c);
116  p_Delete(&lm,c->r);
117
118
119  for (int n=0;((n<c->n) && (i_con[n]>=0));n++){
120    if (i_con[n]==j){
121      now_t_rep(i,j,c);
122      omfree(i_con);
123 
124      return TRUE;
125    }
126  }
127  omfree(i_con);
128 
129  return FALSE;
130}
131BOOLEAN lenS_correct(kStrategy strat){
132  int i;
133  for(i=0;i<=strat->sl;i++){
134    if (strat->lenS[i]!=pLength(strat->S[i]))
135      return FALSE;
136  }
137  return TRUE;
138   
139}
140
141static void notice_miss(int i, int j, calc_dat* c){
142  PrintS("-");
143  c->misses_counter++;
144  if(i>=0)
145    c->misses[i]++;
146  if (j>=0)
147    c->misses[j]++;
148  c->misses_series++;
149}
150static void soon_trep_them(redNF_inf* inf, calc_dat* c){
151  int_pair_node* hf=inf->soon_free;
152 
153  while(hf!=NULL)
154  {
155    int_pair_node* s=hf;
156    soon_t_rep(hf->a,hf->b,c);
157             
158    hf=hf->next;
159    omfree(s);
160  }
161  inf->soon_free=NULL;
162}
163static void trep_them(redNF_inf* inf, calc_dat* c){
164  int_pair_node* hf=inf->soon_free;
165 
166  while(hf!=NULL)
167  {
168    int_pair_node* s=hf;
169    now_t_rep(hf->a,hf->b,c);
170             
171    hf=hf->next;
172    omfree(s);
173  }
174  inf->soon_free=NULL;
175}
176static void cleanS(kStrategy strat){
177  int i=0;
178  LObject P;
179  while(i<=strat->sl){
180    P.p=strat->S[i];
181    P.sev=strat->sevS[i];
182    if(kFindDivisibleByInS(strat->S,strat->sevS,strat->sl,&P)!=i){
183      deleteInS(i,strat);
184
185      //remember destroying poly
186    }
187    else i++;
188  }
189}
190static int bucket_guess(kBucket* bucket){
191  int sum=0;
192  int i;
193  for (i=0;i<MAX_BUCKET+1;i++){
194    sum+=bucket->buckets_length[i];
195  }
196  return sum;
197}
198
199
200#ifdef RANDOM_WALK
201int my_rand(int n){
202  //RANDOM integer beetween 0,..,n-1
203  int erg=(double(rand())/RAND_MAX) *n;
204  return ((erg>=n)?erg-1:erg);
205
206}
207#endif
208
209static BOOLEAN is_empty(calc_dat* c){
210  int i;
211  for(i=0;i<PAR_N;i++){
212    if (!c->work_on[i].is_free) return FALSE;
213  }
214  return TRUE;
215}
216
217
218static void add_to_reductors(calc_dat* c, poly h, int len){
219  int i=simple_posInS(c->strat,h,len);
220
221  LObject P; memset(&P,0,sizeof(P));
222  P.tailRing=c->r;
223  P.p=h; /*p_Copy(h,c->r);*/
224  P.FDeg=pFDeg(P.p,c->r);
225  if (!rField_is_Zp(c->r)) pCleardenom(P.p);
226 
227  c->strat->enterS(P,i,c->strat);
228  c->strat->lenS[i]=len;
229  pNorm(c->strat->S[i]);
230
231}
232static void length_one_crit(calc_dat* c, int pos, int len)
233{
234  if (len==1)
235  {
236    int i;
237    for ( i=0;i<pos;i++)
238    {
239      if (c->lengths[i]==1)
240        c->states[pos][i]=HASTREP;
241    }
242    for ( i=pos+1;i<c->n;i++){
243      if (c->lengths[i]==1)
244        c->states[i][pos]=HASTREP;
245    }
246    shorten_tails(c,c->S->m[pos]);
247  }
248}
249static sorted_pair_node* find_next_pair2(calc_dat* c, BOOLEAN go_higher){
250  clean_top_of_pair_list(c);
251  sorted_pair_node* s=pop_pair(c);
252 
253  if (s){
254  c->found_i=s->i;
255  c->found_j=s->j;
256  } //this passage is deprecated
257  //free_sorted_pair_node(s, c->r);
258  return s;
259
260}
261static BOOLEAN find_next_pair(calc_dat* c, BOOLEAN go_higher)
262{
263  int start_i,start_j,i,j;
264  start_i=0;
265  start_j=-1;
266#ifndef HOMOGENEOUS_EXAMPLE
267  if (c->misses_series>80){
268    c->current_degree++;
269    c->misses_series=0;
270  }
271#endif
272  start_i=c->continue_i;
273  start_j=c->continue_j;
274
275  for (int i=start_i;i<c->n;i++){
276    if (c->T_deg[i]>c->current_degree)
277    {
278      c->skipped_pairs++;
279      continue;
280    }
281    for(int j=(i==start_i)?start_j+1:0;j<i;j++){
282      // printf("searching at %d,%d",i,j);
283      if (c->misses_counter>=2) {
284        c->skipped_pairs++;
285        break;
286      }
287      if (c->states[i][j]==UNCALCULATED){
288        if(c->deg[i][j]<=c->current_degree)
289        {
290          c->continue_i=c->found_i=i;
291          c->continue_j=c->found_j=j;
292          return TRUE;
293        }
294        else
295        {
296          ++(c->skipped_pairs);
297        }
298      }
299    }
300    c->misses_counter=0;
301  }
302
303
304  if (!((start_i==1) &&(start_j==0))){
305    c->continue_i=1;
306    c->continue_j=0;
307    return find_next_pair(c);
308  }
309
310
311  if ((c->skipped_pairs>0) && go_higher){
312    ++(c->current_degree);
313    c->skipped_pairs=0;
314    c->continue_i=0;
315    c->continue_j=0;
316    return find_next_pair(c);
317  }
318  return FALSE;
319}
320static void move_forward_in_S(int old_pos, int new_pos,kStrategy strat)
321{
322  assume(old_pos>=new_pos);
323  poly p=strat->S[old_pos];
324  int ecart=strat->ecartS[old_pos];
325  long sev=strat->sevS[old_pos];
326  int s_2_r=strat->S_2_R[old_pos];
327  int length=strat->lenS[old_pos];
328  int i;
329  for (i=old_pos; i>new_pos; i--)
330  {
331    strat->S[i] = strat->S[i-1];
332    strat->ecartS[i] = strat->ecartS[i-1];
333    strat->sevS[i] = strat->sevS[i-1];
334    strat->S_2_R[i] = strat->S_2_R[i-1];
335  }
336  if (strat->lenS!=NULL)
337    for (i=old_pos; i>new_pos; i--)
338      strat->lenS[i] = strat->lenS[i-1];
339
340  strat->S[new_pos]=p;
341  strat->ecartS[new_pos]=ecart;
342  strat->sevS[new_pos]=sev;
343  strat->S_2_R[new_pos]=s_2_r;
344  strat->lenS[new_pos]=length;
345  //assume(lenS_correct(strat));
346}
347static void replace_pair(int & i, int & j, calc_dat* c)
348{
349  c->soon_free=NULL;
350  int curr_deg;
351  poly lm=pOne();
352
353  pLcm(c->S->m[i], c->S->m[j], lm);
354  pSetm(lm);
355  int deciding_deg= pFDeg(lm);
356  int* i_con =make_connections(i,j,lm,c);
357  int z=0;
358
359  for (int n=0;((n<c->n) && (i_con[n]>=0));n++){
360    if (i_con[n]==j){
361      now_t_rep(i,j,c);
362      omfree(i_con);
363      p_Delete(&lm,c->r);
364      return;
365    }
366  }
367
368  int* j_con =make_connections(j,lm,c);
369  i= i_con[0];
370  j=j_con[0];
371  if(c->n>1){
372    if (i_con[1]>=0)
373      i=i_con[1];
374    else {
375      if (j_con[1]>=0)
376        j=j_con[1];
377    }
378  }
379  pLcm(c->S->m[i], c->S->m[j], lm);
380  pSetm(lm);
381  poly short_s;
382  curr_deg=pFDeg(lm);
383  int_pair_node* last=NULL;
384
385  for (int n=0;((n<c->n) && (j_con[n]>=0));n++){
386    for (int m=0;((m<c->n) && (i_con[m]>=0));m++){
387      pLcm(c->S->m[i_con[m]], c->S->m[j_con[n]], lm);
388      pSetm(lm);
389      if (pFDeg(lm)>=deciding_deg)
390      {
391        soon_t_rep(i_con[m],j_con[n],c);
392        int_pair_node* h= (int_pair_node*)omalloc(sizeof(int_pair_node));
393        if (last!=NULL)
394          last->next=h;
395        else
396          c->soon_free=h;
397        h->next=NULL;
398        h->a=i_con[m];
399        h->b=j_con[n];
400        last=h;
401      }
402      //      if ((comp_deg<curr_deg)
403      //  ||
404      //  ((comp_deg==curr_deg) &&
405      short_s=ksCreateShortSpoly(c->S->m[i_con[m]],c->S->m[j_con[n]],c->r);
406      if (short_s==NULL) {
407        i=i_con[m];
408        j=j_con[n];
409        now_t_rep(i_con[m],j_con[n],c);
410        p_Delete(&lm,c->r);
411        omfree(i_con);
412        omfree(j_con);
413
414        return;
415
416      }
417#ifdef QUICK_SPOLY_TEST
418
419      for (int dz=0;dz<=c->n;dz++){
420        if (dz==c->n) {
421          //have found not head reducing pair
422          i=i_con[m];
423          j=j_con[n];
424          p_Delete(&short_s,c->r);
425          p_Delete(&lm,c->r);
426          omfree(i_con);
427          omfree(j_con);
428
429          return;
430        }
431        if (p_LmDivisibleBy(c->S->m[dz],short_s,c->r)) break;
432      }
433#endif
434      int comp_deg(pFDeg(short_s));
435      p_Delete(&short_s,c->r);
436      if ((comp_deg<curr_deg)
437          ||
438          ((comp_deg==curr_deg) &&
439           (c->misses[i]+c->misses[j]
440            <=
441            c->misses[i_con[m]]+c->misses[j_con[n]])))
442
443      {
444        curr_deg=comp_deg;
445        i=i_con[m];
446        j=j_con[n];
447      }
448    }
449  }
450  p_Delete(&lm,c->r);
451  omfree(i_con);
452  omfree(j_con);
453  return;
454}
455
456static void replace_pair(redNF_inf* inf, calc_dat* c)
457{
458 
459//  inf->soon_free=NULL;
460 
461  int & i=inf->i;
462  int & j=inf->j;
463  assume(i>=0);
464  assume(j>=0);
465  int_pair_node* last=NULL;
466  if (last){
467    while(last->next){
468      last=last->next;
469    }
470  }
471  {
472    int_pair_node* h= (int_pair_node*)omalloc(sizeof(int_pair_node));
473    if (last!=NULL)
474      last->next=h;
475    else
476      inf->soon_free=h;
477    h->next=NULL;
478    h->a=i;
479    h->b=j;
480    last=h;
481  }
482  int curr_deg;
483  poly lm=pOne();
484
485  pLcm(c->S->m[i], c->S->m[j], lm);
486  pSetm(lm);
487  int deciding_deg= pFDeg(lm);
488  int* i_con =make_connections(i,j,lm,c);
489  int z=0;
490
491  for (int n=0;((n<c->n) && (i_con[n]>=0));n++){
492    if (i_con[n]==j){
493      //       curr_deg=pFDeg(lm);
494      //       for(int z1=0;((z1<c->n) && (i_con[z1]>=0));z1++)
495      //         for (int z2=z1+1;((z2<c->n)&&(i_con[z2]>=0));z2++)
496      //         {
497      //           pLcm(c->S->m[i_con[z1]], c->S->m[i_con[z2]], lm);
498      //           pSetm(lm);
499      //           if (pFDeg(lm)==curr_deg)
500      //             now_t_rep(i_con[z1],i_con[z2],c);
501      //         }
502      now_t_rep(i,j,c);
503//hack
504//      i=j;
505      omfree(i_con);
506      p_Delete(&lm,c->r);
507      return;
508    }
509  }
510
511  int* j_con =make_connections(j,lm,c);
512  i= i_con[0];
513  j=j_con[0];
514  if(c->n>1){
515    if (i_con[1]>=0)
516      i=i_con[1];
517    else {
518      if (j_con[1]>=0)
519        j=j_con[1];
520    }
521  }
522  pLcm(c->S->m[i], c->S->m[j], lm);
523  pSetm(lm);
524  poly short_s;
525  curr_deg=pFDeg(lm);
526
527
528  for (int n=0;((n<c->n) && (j_con[n]>=0));n++){
529    for (int m=0;((m<c->n) && (i_con[m]>=0));m++){
530      pLcm(c->S->m[i_con[m]], c->S->m[j_con[n]], lm);
531      pSetm(lm);
532      if (pFDeg(lm)>=deciding_deg)
533      {
534        //soon_t_rep(i_con[m],j_con[n],c);
535        int_pair_node* h= (int_pair_node*)omalloc(sizeof(int_pair_node));
536        if (last!=NULL)
537          last->next=h;
538        else
539          inf->soon_free=h;
540        h->next=NULL;
541        h->a=i_con[m];
542        h->b=j_con[n];
543        last=h;
544      }
545      //      if ((comp_deg<curr_deg)
546      //  ||
547      //  ((comp_deg==curr_deg) &&
548      short_s=ksCreateShortSpoly(c->S->m[i_con[m]],c->S->m[j_con[n]],c->r);
549      if (short_s==NULL) {
550        i=i_con[m];
551        j=j_con[n];
552        now_t_rep(i_con[m],j_con[n],c);
553        p_Delete(&lm,c->r);
554        omfree(i_con);
555        omfree(j_con);
556
557        return;
558
559      }
560#ifdef QUICK_SPOLY_TEST
561
562      for (int dz=0;dz<=c->n;dz++){
563        if (dz==c->n) {
564          //have found not head reducing pair
565          i=i_con[m];
566          j=j_con[n];
567          p_Delete(&short_s,c->r);
568          p_Delete(&lm,c->r);
569          omfree(i_con);
570          omfree(j_con);
571
572          return;
573        }
574        if (p_LmDivisibleBy(c->S->m[dz],short_s,c->r)) break;
575      }
576#endif
577      int comp_deg(pFDeg(short_s));
578      p_Delete(&short_s,c->r);
579      if ((comp_deg<curr_deg)
580          ||
581          ((comp_deg==curr_deg) &&
582           (c->misses[i]+c->misses[j]
583            <=
584            c->misses[i_con[m]]+c->misses[j_con[n]])))
585        //       if ((comp_deg<curr_deg)
586        //           ||
587        //           ((comp_deg==curr_deg) &&
588        //            (c->lengths[i]+c->lengths[j]
589        //             <=
590        //             c->lengths[i_con[m]]+c->lengths[j_con[n]])))
591
592      {
593        curr_deg=comp_deg;
594        i=i_con[m];
595        j=j_con[n];
596      }
597    }
598  }
599  p_Delete(&lm,c->r);
600  omfree(i_con);
601  omfree(j_con);
602  return;
603}
604static int* make_connections(int from, poly bound, calc_dat* c)
605{
606  ideal I=c->S;
607  int s=pFDeg(bound);
608  int* cans=(int*) omalloc(c->n*sizeof(int));
609  int* connected=(int*) omalloc(c->n*sizeof(int));
610  int cans_length=0;
611  connected[0]=from;
612  int connected_length=1;
613  long neg_bounds_short= ~p_GetShortExpVector(bound,c->r);
614  for (int i=0;i<c->n;i++){
615    if (c->T_deg[i]>s) continue;
616    if (i!=from){
617      if(p_LmShortDivisibleBy(I->m[i],c->short_Exps[i],bound,neg_bounds_short,c->r)){
618        cans[cans_length]=i;
619        cans_length++;
620      }
621    }
622  }
623  int not_yet_found=cans_length;
624  int con_checked=0;
625  int pos;
626  while((not_yet_found>0) && (con_checked<connected_length)){
627    pos=connected[con_checked];
628    for(int i=0;i<cans_length;i++){
629      if (cans[i]<0) continue;
630      if (has_t_rep(pos,cans[i],c)){
631
632        connected[connected_length]=cans[i];
633        connected_length++;
634        cans[i]=-1;
635        --not_yet_found;
636
637      }
638    }
639    con_checked++;
640  }
641  if (connected_length<c->n){
642    connected[connected_length]=-1;
643  }
644  omfree(cans);
645  return connected;
646}
647static int* make_connections(int from, int to, poly bound, calc_dat* c)
648{
649  ideal I=c->S;
650  int s=pFDeg(bound);
651  int* cans=(int*) omalloc(c->n*sizeof(int));
652  int* connected=(int*) omalloc(c->n*sizeof(int));
653  cans[0]=to;
654  int cans_length=1;
655  connected[0]=from;
656  int last_cans_pos=-1;
657  int connected_length=1;
658  long neg_bounds_short= ~p_GetShortExpVector(bound,c->r);
659
660  int not_yet_found=cans_length;
661  int con_checked=0;
662  int pos;
663  BOOLEAN can_find_more=TRUE;
664  while(((not_yet_found>0) && (con_checked<connected_length))||can_find_more){
665    if ((con_checked<connected_length)&& (not_yet_found>0)){
666      pos=connected[con_checked];
667      for(int i=0;i<cans_length;i++){
668        if (cans[i]<0) continue;
669        if (has_t_rep(pos,cans[i],c)||(trivial_syzygie(pos,cans[i],bound,c))
670){
671
672          connected[connected_length]=cans[i];
673          connected_length++;
674          cans[i]=-1;
675          --not_yet_found;
676
677          if (connected[connected_length-1]==to){
678            if (connected_length<c->n){
679              connected[connected_length]=-1;
680            }
681
682            omfree(cans);
683            return connected;
684          }
685        }
686      }
687      con_checked++;
688    } else {
689
690      for(last_cans_pos++;last_cans_pos<=c->n;last_cans_pos++){
691        if (last_cans_pos==c->n){
692          if (connected_length<c->n){
693            connected[connected_length]=-1;
694          }
695          omfree(cans);
696          return connected;
697        }
698        if ((last_cans_pos==from)||(last_cans_pos==to))
699          continue;
700        if(p_LmShortDivisibleBy(I->m[last_cans_pos],c->short_Exps[last_cans_pos],bound,neg_bounds_short,c->r)){
701          cans[cans_length]=last_cans_pos;
702          cans_length++;
703          break;
704        }
705      }
706      not_yet_found++;
707      for (int i=0;i<con_checked;i++){
708        if (has_t_rep(connected[i],last_cans_pos,c)){
709
710          connected[connected_length]=last_cans_pos;
711          connected_length++;
712          cans[cans_length-1]=-1;
713
714          --not_yet_found;
715          if (connected[connected_length-1]==to){
716            if (connected_length<c->n){
717              connected[connected_length]=-1;
718            }
719
720            omfree(cans);
721            return connected;
722          }
723          break;
724        }
725
726      }
727    }
728  }
729  if (connected_length<c->n){
730    connected[connected_length]=-1;
731  }
732
733  omfree(cans);
734  return connected;
735}
736#ifdef HEAD_BIN
737static inline poly p_MoveHead(poly p, omBin b)
738{
739  poly np;
740  omTypeAllocBin(poly, np, b);
741  memmove(np, p, b->sizeW*sizeof(long));
742  omFreeBinAddr(p);
743  return np;
744}
745#endif
746static int init_red_input_element(calc_dat* c, poly el, int pos){
747    c->work_on[pos].soon_free=NULL;
748  c->work_on[pos].is_free=FALSE;
749  c->work_on[pos].started=FALSE;
750  c->work_on[pos].i=-1 ;
751  c->work_on[pos].j=-1;
752  c->work_on[pos].h=NULL;
753  c->work_on[pos].need_std_rep=TRUE;
754 
755
756 
757 
758  c->work_on[pos].started=TRUE;
759 
760  if (!el){
761    c->work_on[pos].is_free=TRUE;
762    c->work_on[pos].h=NULL;
763    return pos;
764  }
765  if (c->work_on[pos].P!=NULL)
766    delete c->work_on[pos].P;
767
768
769 
770  int len=pLength(el);
771  c->work_on[pos].len_upper_bound=len;
772  c->work_on[pos].P=new LObject(el);
773  c->work_on[pos].P->SetShortExpVector();
774  c->work_on[pos].P->bucket = kBucketCreate(currRing);
775  assume(c->work_on[pos].P->p==el);
776  kBucketInit(c->work_on[pos].P->bucket,c->work_on[pos].P->p,len /*pLength(P.p)*/);
777 
778
779  return pos;
780 
781}
782static int init_red_phase1(calc_dat* c, int i, int j, int pos)
783{
784 
785  c->work_on[pos].soon_free=NULL;
786  c->work_on[pos].is_free=FALSE;
787  c->work_on[pos].started=FALSE;
788  c->work_on[pos].i=i;
789  c->work_on[pos].j=j;
790  c->work_on[pos].h=NULL;
791  c->work_on[pos].need_std_rep=FALSE;
792
793  replace_pair(&c->work_on[pos],c);
794//verhindern daß ein Paar mehrmals ausgerechnet wird
795  soon_trep_them(&c->work_on[pos], c);
796
797  return pos;
798}
799static void init_red_spoly_phase2(calc_dat* c,int pos){
800  replace_pair(&c->work_on[pos],c);
801  assume(c->work_on[pos].i>=0);
802    assume(c->work_on[pos].j>=0); 
803  if(c->work_on[pos].i==c->work_on[pos].j){
804    c->work_on[pos].is_free=TRUE;
805    c->work_on[pos].h=NULL;
806    return;
807  }
808  c->work_on[pos].started=TRUE;
809  poly h=ksOldCreateSpoly(c->S->m[c->work_on[pos].i], c->S->m[c->work_on[pos].j], NULL, c->r);
810  if (h==NULL){
811    c->work_on[pos].is_free=TRUE;
812    c->work_on[pos].h=NULL;
813    return;
814  }
815  if (c->work_on[pos].P!=NULL)
816    delete c->work_on[pos].P;
817
818
819 
820  int len=pLength(h);
821  c->work_on[pos].len_upper_bound=len;
822  c->work_on[pos].P=new LObject(h);
823  c->work_on[pos].P->SetShortExpVector();
824  c->work_on[pos].P->bucket = kBucketCreate(currRing);
825  kBucketInit(c->work_on[pos].P->bucket,c->work_on[pos].P->p,len /*pLength(P.p)*/);
826 
827};
828static void initial_data(calc_dat* c){
829  void* h;
830  int i,j;
831  c->last_index=-1;
832  c->work_on=(redNF_inf*) omalloc(PAR_N*sizeof(redNF_inf));
833  int counter;
834  for(counter=0;counter<PAR_N;counter++){
835    c->work_on[counter].is_free=TRUE;
836    c->work_on[counter].P=NULL;
837  }
838  c->misses_series=0;
839  c->soon_free=NULL;
840  c->misses_counter=0;
841  c->max_misses=0;
842  c->normal_forms=0;
843  c->current_degree=1;
844  c->skipped_pairs=0;
845  c->pairs=NULL;
846  int n=c->S->idelems();
847  c->n=n;
848  c->T_deg=(int*) omalloc(n*sizeof(int));
849  c->continue_i=0;
850  c->continue_j=0;
851  c->skipped_i=-1;
852#ifdef HEAD_BIN
853  c->HeadBin=omGetSpecBin(POLYSIZE + (currRing->ExpL_Size)*sizeof(long));
854#endif
855  /* omUnGetSpecBin(&(c->HeadBin)); */
856  h=omalloc(n*sizeof(char*));
857  if (h!=NULL)
858    c->states=(char**) h;
859  else
860    exit(1);
861  c->misses=(int*) omalloc(n*sizeof(int));
862  c->deg=(int **) omalloc(n*sizeof(int*));
863  h=omalloc(n*sizeof(int));
864  if (h!=NULL)
865    c->lengths=(int*) h;
866  else
867    exit(1);
868
869  h=omalloc(n*sizeof(int));
870  if (h!=NULL)
871    c->rep=(int*) h;
872  else
873    exit(1);
874  c->short_Exps=(long*) omalloc(n*sizeof(long));
875  for (i=0;i<n;i++)
876  {
877#ifdef HEAD_BIN
878    c->S->m[i]=p_MoveHead(c->S->m[i],c->HeadBin);
879#endif
880    c->T_deg[i]=pFDeg(c->S->m[i]);
881    c->lengths[i]=pLength(c->S->m[i]);
882    c->misses[i]=0;
883    c->states[i]=(char *)omAlloc((i+1)*sizeof(char));
884    c->deg[i]=(int*) omAlloc((i+1)*sizeof(int));
885    for (j=0;j<i;j++){
886      //check product criterion
887      if (pHasNotCF(c->S->m[i],c->S->m[j])){
888        c->states[i][j]=HASTREP;
889      } else {
890        c->states[i][j]=UNCALCULATED;
891      }
892      if ((c->lengths[i]==1) && (c->lengths[j]==1))
893        c->states[i][j]=HASTREP;
894      c->deg[i][j]=pLcmDeg(c->S->m[i],c->S->m[j]);
895      if (c->states[i][j]==UNCALCULATED){
896        sort_pair_in(i,j,c);
897      }
898    }
899     
900    c->rep[i]=i;
901    c->short_Exps[i]=p_GetShortExpVector(c->S->m[i],c->r);
902
903  }
904
905
906  c->strat=new skStrategy;
907  c->strat->syzComp = 0;
908  initBuchMoraCrit(c->strat);
909  initBuchMoraPos(c->strat);
910  c->strat->initEcart = initEcartBBA;
911  c->strat->enterS = enterSBba;
912  c->strat->sl = -1;
913  /* initS(c->S,NULL,c->strat); */
914/* intS start: */
915  i=((i+IDELEMS(c->S)+15)/16)*16;
916  c->strat->ecartS=(intset)omAlloc(i*sizeof(int)); /*initec(i);*/
917  c->strat->sevS=(unsigned long*)omAlloc0(i*sizeof(unsigned long));
918  /*initsevS(i);*/
919  c->strat->S_2_R=(int*)omAlloc0(i*sizeof(int));/*initS_2_R(i);*/
920  c->strat->fromQ=NULL;
921  c->strat->Shdl=idInit(i,1);
922  c->strat->S=c->strat->Shdl->m;
923  c->strat->lenS=(int*)omAlloc0(i*sizeof(int));
924  for (i=0; i<IDELEMS(c->S); i++)
925  {
926    int pos;
927    assume (c->S->m[i]!=NULL);
928    LObject h;
929    h.p = c->S->m[i];
930    h.pNorm();
931    c->strat->initEcart(&h);
932    assume(c->lengths[i]==pLength(h.p));
933    if (c->strat->sl==-1) pos=0;
934    else pos = simple_posInS(c->strat,h.p,c->lengths[i]);
935    h.sev = pGetShortExpVector(h.p);
936    c->strat->enterS(h,pos,c->strat);
937    c->strat->lenS[pos]=c->lengths[i];
938  }
939  //c->strat->lenS=(int*)omAlloc0(IDELEMS(c->strat->Shdl)*sizeof(int));
940  //for (i=c->strat->sl;i>=0;i--)
941  //{
942  //  pNorm(c->strat->S[i]);
943  //  c->strat->lenS[i]=pLength(c->strat->S[i]);
944  //}
945  /* initS end */
946}
947static void initial_data2(calc_dat* c, ideal I){
948  void* h;
949  poly hp;
950  int i,j;
951  c->reduction_steps=0;
952  c->last_index=-1;
953  c->work_on=(redNF_inf*) omalloc(PAR_N*sizeof(redNF_inf));
954  int counter;
955  for(counter=0;counter<PAR_N;counter++){
956    c->work_on[counter].is_free=TRUE;
957    c->work_on[counter].P=NULL;
958  }
959  c->Rcounter=0;
960  c->misses_series=0;
961  c->soon_free=NULL;
962  c->misses_counter=0;
963  c->max_misses=0;
964  c->normal_forms=0;
965  c->current_degree=1;
966  c->skipped_pairs=0;
967  c->pairs=NULL;
968  int n=I->idelems();
969  for (i=0;i<n;i++){
970    wrp(I->m[i]);
971    PrintS("\n");
972  }
973    i=0;
974
975  c->n=0;
976  c->T_deg=(int*) omalloc(n*sizeof(int));
977  c->continue_i=0;
978  c->continue_j=0;
979  c->skipped_i=-1;
980#ifdef HEAD_BIN
981  c->HeadBin=omGetSpecBin(POLYSIZE + (currRing->ExpL_Size)*sizeof(long));
982#endif
983  /* omUnGetSpecBin(&(c->HeadBin)); */
984  h=omalloc(n*sizeof(char*));
985 
986  c->states=(char**) h;
987 
988  c->misses=(int*) omalloc(n*sizeof(int));
989  c->deg=(int **) omalloc(n*sizeof(int*));
990  h=omalloc(n*sizeof(int));
991  c->lengths=(int*) h;
992  h=omalloc(n*sizeof(int));
993 
994  c->rep=(int*) h;
995   
996  c->short_Exps=(long*) omalloc(n*sizeof(long));
997  c->S=idInit(n,1);
998
999  c->strat=new skStrategy;
1000  c->strat->syzComp = 0;
1001  initBuchMoraCrit(c->strat);
1002  initBuchMoraPos(c->strat);
1003  c->strat->initEcart = initEcartBBA;
1004  c->strat->enterS = enterSBba;
1005  c->strat->sl = -1;
1006  i=n;
1007  /* initS(c->S,NULL,c->strat); */
1008/* intS start: */
1009  i=((i+IDELEMS(c->S)+15)/16)*16;
1010  c->strat->ecartS=(intset)omAlloc(i*sizeof(int)); /*initec(i);*/
1011  c->strat->sevS=(unsigned long*)omAlloc0(i*sizeof(unsigned long));
1012  /*initsevS(i);*/
1013  c->strat->S_2_R=(int*)omAlloc0(i*sizeof(int));/*initS_2_R(i);*/
1014  c->strat->fromQ=NULL;
1015  c->strat->Shdl=idInit(1,1);
1016  c->strat->S=c->strat->Shdl->m;
1017  c->strat->lenS=(int*)omAlloc0(i*sizeof(int));
1018  sorted_pair_node* si;
1019  assume(n>0);
1020  add_to_basis(I->m[0],-1,-1,c);
1021  sorted_pair_node** set_this= & (c->pairs);
1022  assume(c->strat->sl==c->strat->Shdl->idelems()-1);
1023 
1024  for (i=1;i<n;i++)//the 1 is wanted, because first element is added to basis
1025   {
1026//     add_to_basis(I->m[i],-1,-1,c);
1027     si=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1028      si->i=-1;
1029      si->j=-1;
1030      si->expected_length=pLength(I->m[i]);
1031      si->deg=pFDeg(I->m[i]);
1032      si->lcm_of_lm=I->m[i];
1033      si->next=NULL;
1034      PrintS("ho");
1035     (*set_this)=si;
1036      set_this=&(si->next);
1037     
1038
1039   }
1040 
1041}
1042static int simple_posInS (kStrategy strat, poly p,int len)
1043{
1044  if(strat->sl==-1) return 0;
1045  polyset set=strat->S;
1046  intset setL=strat->lenS;
1047  int length=strat->sl;
1048  int i;
1049  int an = 0;
1050  int en= length;
1051
1052  if ((len>setL[length])
1053      || ((len==setL[length]) && (pLmCmp(set[length],p)== -1)))
1054    return length+1;
1055
1056  loop
1057    {
1058      if (an >= en-1)
1059      {
1060        if ((len<setL[an])
1061            || ((len==setL[length]) && (pLmCmp(set[an],p) == 1))) return an;
1062        return en;
1063      }
1064      i=(an+en) / 2;
1065      if ((len<setL[i])
1066          || ((len==setL[i]) && (pLmCmp(set[i],p) == 1))) en=i;
1067      //else if ((len>setL[i])
1068      //|| ((len==setL[i]) && (pLmCmp(set[i],p) == -1))) an=i;
1069      else an=i;
1070    }
1071}
1072/*2
1073 *if the leading term of p
1074 *divides the leading term of some S[i] it will be canceled
1075 */
1076static inline void clearS (poly p, unsigned long p_sev,int l, int* at, int* k,
1077                           kStrategy strat)
1078{
1079  assume(p_sev == pGetShortExpVector(p));
1080  if (!pLmShortDivisibleBy(p,p_sev, strat->S[*at], ~ strat->sevS[*at])) return;
1081  if (l>=strat->lenS[*at]) return;
1082  PrintS("!");mflush();
1083  //pDelete(&strat->S[*at]);
1084  deleteInS((*at),strat);
1085  (*at)--;
1086  (*k)--;
1087//  assume(lenS_correct(strat));
1088}
1089static void add_to_basis(poly h, int i_pos, int j_pos,calc_dat* c)
1090{
1091//  BOOLEAN corr=lenS_correct(c->strat);
1092  BOOLEAN R_found=FALSE;
1093  void* hp;
1094  PrintS("s");
1095  ++(c->n);
1096  ++(c->S->ncols);
1097  int i,j;
1098  i=c->n-1;
1099  sorted_pair_node** nodes=(sorted_pair_node**) omalloc(sizeof(sorted_pair_node*)*i);
1100  int spc=0;
1101  c->T_deg=(int*) omrealloc(c->T_deg,c->n*sizeof(int));
1102  c->T_deg[i]=pFDeg(h);
1103  hp=omrealloc(c->rep, c->n *sizeof(int));
1104  if (hp!=NULL){
1105    c->rep=(int*) hp;
1106  } else {
1107    exit(1);
1108  }
1109  c->short_Exps=(long *) omrealloc(c->short_Exps ,c->n*sizeof(long));
1110
1111  hp=omrealloc(c->lengths, c->n *sizeof(int));
1112  if (hp!=NULL){
1113    c->lengths=(int*) hp;
1114  } else {
1115    exit(1);
1116  }
1117  c->misses=(int*) omrealloc(c->misses,c->n*sizeof(int));
1118  c->misses[i]=0;
1119  c->lengths[i]=pLength(h);
1120  hp=omrealloc(c->states, c->n * sizeof(char*));
1121  if (hp!=NULL){
1122    c->states=(char**) hp;
1123  } else {
1124    exit(1);
1125  }
1126  c->deg=(int**) omrealloc(c->deg, c->n * sizeof(int*));
1127  c->rep[i]=i;
1128  hp=omalloc(i*sizeof(char));
1129  if (hp!=NULL){
1130    c->states[i]=(char*) hp;
1131  } else {
1132    exit(1);
1133  }
1134  c->deg[i]=(int*) omalloc(i*sizeof(int));
1135  hp=omrealloc(c->S->m,c->n*sizeof(poly));
1136  if (hp!=NULL){
1137    c->S->m=(poly*) hp;
1138  } else {
1139    exit(1);
1140  }
1141  c->S->m[i]=h;
1142  c->short_Exps[i]=p_GetShortExpVector(h,c->r);
1143  for (j=0;j<i;j++){
1144    c->deg[i][j]=pLcmDeg(c->S->m[i],c->S->m[j]);
1145    if (c->rep[j]==j){
1146      //check product criterion
1147
1148      c->states[i][j]=UNCALCULATED;
1149
1150
1151      //lies I[i] under I[j] ?
1152      if(p_LmShortDivisibleBy(c->S->m[i],c->short_Exps[i],c->S->m[j],~(c->short_Exps[j]),c->r)){
1153        c->rep[j]=i;
1154        PrintS("R"); R_found=TRUE;
1155        c->Rcounter++;
1156        if((i_pos>=0) && (j_pos>=0)){
1157          c->misses[i_pos]--;
1158          c->misses[j_pos]--;
1159        }
1160        for(int z=0;z<j;z++){
1161          if(c->rep[z]!=z) continue;
1162          if (c->states[j][z]==UNCALCULATED){
1163            c->states[j][z]=UNIMPORTANT;
1164          }
1165        }
1166        for(int z=j+1;z<i;z++){
1167          if(c->rep[z]!=z) continue;
1168          if (c->states[z][j]==UNCALCULATED){
1169            c->states[z][j]=UNIMPORTANT;
1170          }
1171        }
1172      }
1173    }
1174    else {
1175      c->states[i][j]=UNIMPORTANT;
1176    }
1177    if ((c->lengths[i]==1) && (c->lengths[j]==1))
1178      c->states[i][j]=HASTREP;
1179    else if (pHasNotCF(c->S->m[i],c->S->m[j]))
1180      c->states[i][j]=HASTREP;
1181    if (c->states[i][j]==UNCALCULATED){
1182//      sort_pair_in(i,j,c);
1183      poly short_s=ksCreateShortSpoly(c->S->m[i],c->S->m[j],c->r);
1184      if (short_s)
1185      {
1186        sorted_pair_node* s=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1187        s->i=max(i,j);
1188        s->j=min(i,j);
1189        s->expected_length=c->lengths[i]+c->lengths[j]-2;
1190        s->deg=pFDeg(short_s);
1191        //poly lm=pOne();
1192     
1193        // pLcm(c->S->m[i], c->S->m[j], lm);
1194        //pSetm(lm);
1195        s->lcm_of_lm=short_s;
1196        //assume(lm!=NULL);
1197        nodes[spc]=s;
1198        spc++;
1199      }
1200      else 
1201      {
1202        c->states[i][j]=HASTREP;
1203      }
1204    }
1205  }
1206  if (c->skipped_i>0){
1207    c->continue_i=c->skipped_i;
1208    c->continue_j=0;
1209    c->skipped_i=-1;
1210  }
1211  add_to_reductors(c, h, c->lengths[c->n-1]);
1212  //i=posInS(c->strat,c->strat->sl,h,0 /*ecart*/);
1213 
1214  if (c->lengths[c->n-1]==1)
1215    shorten_tails(c,c->S->m[c->n-1]);
1216  // if (corr){
1217 
1218//     corr=lenS_correct(c->strat);
1219//     if(!corr){
1220//       PrintS("korupted in shorten tails");
1221//     }
1222//     }
1223  //you should really update c->lengths, c->strat->lenS, and the oder of polys in strat if you sort after lengths
1224
1225  //for(i=c->strat->sl; i>0;i--)
1226  //  if(c->strat->lenS[i]<c->strat->lenS[i-1]) printf("fehler bei %d\n",i);
1227  if (c->Rcounter>50) {
1228    c->Rcounter=0;
1229    cleanS(c->strat);
1230  }
1231  qsort(nodes,spc,sizeof(sorted_pair_node*),pair_better_gen);
1232  clean_top_of_pair_list(c);
1233  sorted_pair_node* last=c->pairs;
1234 
1235  if(spc>0){
1236    if (!c->pairs) 
1237    {
1238      --spc;
1239      c->pairs=nodes[spc];
1240      c->pairs->next=NULL;
1241      last=c->pairs;
1242    }
1243    while(spc>0){
1244      --spc;
1245      sorted_pair_node* h=last->next;
1246     
1247      while((h!=NULL)&&(pair_better(h,nodes[spc],c)))
1248      {
1249        if ((h->i>=0) && (!state_is(UNCALCULATED,h->j, h->i,c))){
1250          last->next=h->next;
1251         
1252          free_sorted_pair_node(h,c->r);
1253          h=last->next;
1254          if(!h) break;
1255        }
1256        last=h;
1257       
1258        h=h->next;
1259        assume((h==NULL)||(h->lcm_of_lm!=NULL));
1260      }
1261      last->next=nodes[spc];
1262      nodes[spc]->next=h;
1263      last=nodes[spc];
1264    }
1265  }
1266
1267  omfree(nodes);
1268
1269  }
1270#if 0
1271static poly redNF (poly h,kStrategy strat)
1272{
1273  int j = 0;
1274  int z = 3;
1275  unsigned long not_sev;
1276
1277  if (0 > strat->sl)
1278  {
1279    return h;
1280  }
1281  not_sev = ~ pGetShortExpVector(h);
1282  loop
1283    {
1284      if (pLmShortDivisibleBy(strat->S[j], strat->sevS[j], h, not_sev))
1285      {
1286        //if (strat->interpt) test_int_std(strat->kIdeal);
1287        /*- compute the s-polynomial -*/
1288#ifdef KDEBUG
1289        if (TEST_OPT_DEBUG)
1290        {
1291          PrintS("red:");
1292          wrp(h);
1293          PrintS(" with ");
1294          wrp(strat->S[j]);
1295        }
1296#endif
1297        h = ksOldSpolyRed(strat->S[j],h,strat->kNoether);
1298#ifdef KDEBUG
1299        if (TEST_OPT_DEBUG)
1300        {
1301          PrintS("\nto:");
1302          wrp(h);
1303          PrintLn();
1304        }
1305#endif
1306        if (h == NULL) return NULL;
1307        z++;
1308        if (z>=10)
1309        {
1310          z=0;
1311          pNormalize(h);
1312        }
1313        /*- try to reduce the s-polynomial -*/
1314        j = 0;
1315        not_sev = ~ pGetShortExpVector(h);
1316      }
1317      else
1318      {
1319        if (j >= strat->sl) return h;
1320        j++;
1321      }
1322    }
1323}
1324#else
1325
1326static poly redNF2 (poly h,calc_dat* c , int &len)
1327{
1328  len=0;
1329  if (h==NULL) return NULL;
1330  int j;
1331
1332  kStrategy strat=c->strat;
1333  len=pLength(h);
1334  int len_upper_bound=len;
1335  if (0 > strat->sl)
1336  {
1337    return h;
1338  }
1339  LObject P(h);
1340  P.SetShortExpVector();
1341  P.bucket = kBucketCreate(currRing);
1342  // BOOLEAN corr=lenS_correct(strat);
1343  kBucketInit(P.bucket,P.p,len /*pLength(P.p)*/);
1344  //int max_pos=simple_posInS(strat,P.p);
1345  loop
1346    {
1347//       if (corr){
1348       
1349//      corr=lenS_correct(strat);
1350//      if(!corr){
1351//        PrintS("korupt");
1352//      }
1353//       }
1354      int compare_bound;
1355      compare_bound=bucket_guess(P.bucket);
1356      len_upper_bound=min(compare_bound,len_upper_bound);
1357      j=kFindDivisibleByInS(strat->S,strat->sevS,strat->sl,&P);
1358      if (j>=0)
1359      {
1360        poly sec_copy=NULL;
1361        //pseudo code
1362        BOOLEAN must_expand=FALSE;
1363        BOOLEAN must_replace_in_basis=(len_upper_bound<strat->lenS[j]);//first test
1364        if (must_replace_in_basis)
1365        {
1366          //second test
1367          if (pLmEqual(P.p,strat->S[j]))
1368          {
1369            PrintS("b");
1370            sec_copy=kBucketClear(P.bucket);
1371            sec_copy=redTailShort(sec_copy, strat);
1372            kBucketInit(P.bucket,pCopy(sec_copy),pLength(sec_copy));
1373          }
1374          else
1375          {
1376            must_replace_in_basis=FALSE;
1377            if ((len_upper_bound==1)
1378                ||(len_upper_bound==2)
1379                ||(len_upper_bound<strat->lenS[j]/2))
1380            {
1381              PrintS("e");
1382              sec_copy=kBucketClear(P.bucket);
1383              kBucketInit(P.bucket,pCopy(sec_copy),pLength(sec_copy));
1384              must_expand=TRUE;
1385            }
1386          }
1387        }
1388//        must_expand=FALSE;
1389//        must_replace_in_basis=FALSE;
1390        nNormalize(pGetCoeff(P.p));
1391#ifdef KDEBUG
1392        if (TEST_OPT_DEBUG)
1393        {
1394          PrintS("red:");
1395          wrp(h);
1396          PrintS(" with ");
1397          wrp(strat->S[j]);
1398        }
1399#endif
1400        len_upper_bound=len_upper_bound+strat->lenS[j]-2;
1401        number coef=kBucketPolyRed(P.bucket,strat->S[j],
1402                                   strat->lenS[j]/*pLength(strat->S[j])*/,
1403                                   strat->kNoether);
1404        nDelete(&coef);
1405        h = kBucketGetLm(P.bucket);
1406       
1407        if (must_replace_in_basis){
1408          int pos_in_c=-1;
1409          poly p=strat->S[j];
1410          int z;
1411           
1412          int new_length=pLength(sec_copy);
1413          Print("%i",strat->lenS[j]-new_length);
1414          len_upper_bound=new_length +strat->lenS[j]-2;//old entries length
1415          int new_pos=simple_posInS(c->strat,strat->S[j],new_length);//hack
1416           
1417//          p=NULL;
1418          for (z=c->n;z;z--)
1419          {
1420            if(p==c->S->m[z-1])
1421            {
1422               
1423               
1424              pos_in_c=z-1;
1425
1426              break;
1427            }
1428          }
1429          if (z<=0){
1430            //not in c->S
1431            //LEAVE
1432            deleteInS(j,c->strat);
1433         
1434            add_to_reductors(c,sec_copy,pLength(sec_copy));
1435          } 
1436          else {
1437//shorten_tails may alter position (not the length, even not by recursion in GLOBAL case)
1438
1439            strat->S[j]=sec_copy;
1440            c->strat->lenS[j]=new_length;
1441            pDelete(&p);
1442             
1443            //        replace_quietly(c,j,sec_copy);
1444            // have to do many additional things for consistency
1445            {
1446               
1447               
1448               
1449
1450              int old_pos=j;
1451              new_pos=min(old_pos, new_pos);
1452              assume(new_pos<=old_pos);
1453               
1454               
1455              c->strat->lenS[old_pos]=new_length;
1456              int i=0;
1457              for(i=new_pos;i<old_pos;i++){
1458                if (strat->lenS[i]<=new_length)
1459                  new_pos++;
1460                else
1461                  break;
1462              }
1463              if (new_pos<old_pos)
1464                move_forward_in_S(old_pos,new_pos,c->strat);
1465               
1466              c->S->m[pos_in_c]=sec_copy;
1467                               
1468              c->lengths[pos_in_c]=new_length;
1469              if (new_length==1)
1470              {
1471                int i;
1472                for ( i=0;i<pos_in_c;i++)
1473                {
1474                  if (c->lengths[i]==1)
1475                    c->states[pos_in_c][i]=HASTREP;
1476                }
1477                for ( i=z;i<c->n;i++){
1478                  if (c->lengths[i]==1)
1479                    c->states[i][pos_in_c]=HASTREP;
1480                }
1481                shorten_tails(c,sec_copy);
1482              }
1483            }
1484          }
1485        }
1486        if(must_expand){
1487
1488          add_to_reductors(c,sec_copy,pLength(sec_copy));
1489        }
1490        if (h==NULL) return NULL;
1491        P.p=h;
1492        P.t_p=NULL;
1493        P.SetShortExpVector();
1494#ifdef KDEBUG
1495        if (TEST_OPT_DEBUG)
1496        {
1497          PrintS("\nto:");
1498          wrp(h);
1499          PrintLn();
1500        }
1501#endif
1502      }
1503      else
1504      {
1505        kBucketClear(P.bucket,&(P.p),&len);
1506        kBucketDestroy(&P.bucket);
1507        pNormalize(P.p);
1508        return P.p;
1509      }
1510    }
1511}
1512
1513
1514static poly redTailShort(poly h, kStrategy strat){
1515 
1516  int sl=strat->sl;
1517  int i;
1518  int len;
1519  for(i=0;i<=strat->sl;i++){
1520    if(strat->lenS[i]>2)
1521      break;
1522  }
1523  return(redNFTail(h,i-1,strat, len));
1524}
1525
1526
1527
1528static BOOLEAN redNF2_n_steps (redNF_inf* obj,calc_dat* c, int n)
1529{
1530 
1531  obj->P->SetShortExpVector();
1532  poly h;
1533  if (obj->is_free){
1534    return TRUE;}
1535  int len; 
1536  int j;
1537  kStrategy strat=c->strat;
1538  assume(strat->sl<50000);
1539  if (0 > strat->sl)
1540  {
1541   
1542    kBucketClear(obj->P->bucket,&(obj->P->p),&len);
1543    kBucketDestroy(&obj->P->bucket);
1544    pNormalize(obj->P->p);
1545    obj->h=obj->P->p;
1546    obj->is_free=TRUE;
1547    return TRUE;
1548
1549  }
1550  loop
1551    {
1552     
1553      if (n<=0){
1554       
1555        return FALSE;
1556      }
1557      kBucketCanonicalize(obj->P->bucket);
1558      int compare_bound;
1559      compare_bound=bucket_guess(obj->P->bucket);
1560      obj->len_upper_bound=min(compare_bound,obj->len_upper_bound);
1561      j=kFindDivisibleByInS(strat->S,strat->sevS,strat->sl,obj->P);
1562      if (j>=0)
1563      {
1564        poly sec_copy=NULL;
1565
1566        BOOLEAN must_expand=FALSE;
1567        BOOLEAN must_replace_in_basis=(obj->len_upper_bound<strat->lenS[j]);//first test
1568        if (must_replace_in_basis)
1569        {
1570          //second test
1571          if (pLmEqual(obj->P->p,strat->S[j]))
1572          {
1573            PrintS("b");
1574            sec_copy=kBucketClear(obj->P->bucket);
1575            kBucketInit(obj->P->bucket,pCopy(sec_copy),pLength(sec_copy));
1576          }
1577          else
1578          {
1579            must_replace_in_basis=FALSE;
1580            if ((obj->len_upper_bound==1)
1581                ||(obj->len_upper_bound==2)
1582                ||(obj->len_upper_bound<strat->lenS[j]/2))
1583            {
1584              PrintS("e");
1585              sec_copy=kBucketClear(obj->P->bucket);
1586              kBucketInit(obj->P->bucket,pCopy(sec_copy),pLength(sec_copy));
1587              must_expand=TRUE;
1588            }
1589          }
1590        }
1591
1592        nNormalize(pGetCoeff(obj->P->p));
1593#ifdef KDEBUG
1594        if (TEST_OPT_DEBUG)
1595        {
1596          PrintS("red:");
1597          wrp(h);
1598          PrintS(" with ");
1599          wrp(strat->S[j]);
1600        }
1601#endif
1602        obj->len_upper_bound=obj->len_upper_bound+strat->lenS[j]-2;
1603        number coef=kBucketPolyRed(obj->P->bucket,strat->S[j],
1604                                   strat->lenS[j]/*pLength(strat->S[j])*/,
1605                                   strat->kNoether);
1606        nDelete(&coef);
1607        h = kBucketGetLm(obj->P->bucket);
1608       
1609        if (must_replace_in_basis){
1610          obj->need_std_rep=TRUE;
1611          int pos_in_c=-1;
1612          poly p=strat->S[j];
1613          int z;
1614           
1615          int new_length=pLength(sec_copy);
1616          Print("%i",strat->lenS[j]-new_length);
1617          obj->len_upper_bound=new_length +strat->lenS[j]-2;//old entries length
1618          int new_pos=simple_posInS(c->strat,strat->S[j],new_length);//hack
1619           
1620//          p=NULL;
1621          for (z=c->n;z;z--)
1622          {
1623            if(p==c->S->m[z-1])
1624            {
1625              pos_in_c=z-1;
1626              break;
1627            }
1628          }
1629          if (z<=0){
1630            //not in c->S
1631            //LEAVE
1632            deleteInS(j,c->strat);
1633            add_to_reductors(c,sec_copy,pLength(sec_copy));
1634          } 
1635          else {
1636//shorten_tails may alter position (not the length, even not by recursion in GLOBAL case)
1637            strat->S[j]=sec_copy;
1638            c->strat->lenS[j]=new_length;
1639            pDelete(&p);
1640             
1641            //        replace_quietly(c,j,sec_copy);
1642            // have to do many additional things for consistency
1643            {
1644              int old_pos=j;
1645              new_pos=min(old_pos, new_pos);
1646              assume(new_pos<=old_pos);
1647              c->strat->lenS[old_pos]=new_length;
1648              int i=0;
1649              for(i=new_pos;i<old_pos;i++){
1650                if (strat->lenS[i]<=new_length)
1651                  new_pos++;
1652                else
1653                  break;
1654              }
1655              if (new_pos<old_pos)
1656                move_forward_in_S(old_pos,new_pos,c->strat);
1657               
1658              c->S->m[pos_in_c]=sec_copy;
1659                               
1660              c->lengths[pos_in_c]=new_length;
1661              length_one_crit(c,pos_in_c, new_length);
1662       
1663            }
1664          }
1665        }
1666        if(must_expand){
1667          obj->need_std_rep=TRUE;
1668          add_to_reductors(c,sec_copy,pLength(sec_copy));
1669        }
1670        if (h==NULL) {
1671          obj->h=NULL;
1672          obj->is_free=TRUE;
1673          return TRUE;
1674        }
1675        obj->P->p=h;
1676        obj->P->t_p=NULL;
1677        obj->P->SetShortExpVector();
1678
1679      }
1680      else
1681      {
1682        kBucketClear(obj->P->bucket,&(obj->P->p),&len);
1683        kBucketDestroy(&obj->P->bucket);
1684        pNormalize(obj->P->p);
1685        obj->h=obj->P->p;
1686        obj->is_free=TRUE;
1687        return TRUE;
1688      }
1689      n--;
1690      c->reduction_steps++;
1691    }
1692}
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704static poly redNF (poly h,kStrategy strat, int &len)
1705{
1706  len=0;
1707  if (h==NULL) return NULL;
1708  int j;
1709
1710  len=pLength(h);
1711  if (0 > strat->sl)
1712  {
1713    return h;
1714  }
1715  LObject P(h);
1716  P.SetShortExpVector();
1717  P.bucket = kBucketCreate(currRing);
1718  kBucketInit(P.bucket,P.p,len /*pLength(P.p)*/);
1719  //int max_pos=simple_posInS(strat,P.p);
1720  loop
1721    {
1722      j=kFindDivisibleByInS(strat->S,strat->sevS,strat->sl,&P);
1723      if (j>=0)
1724      {
1725        nNormalize(pGetCoeff(P.p));
1726#ifdef KDEBUG
1727        if (TEST_OPT_DEBUG)
1728        {
1729          PrintS("red:");
1730          wrp(h);
1731          PrintS(" with ");
1732          wrp(strat->S[j]);
1733        }
1734#endif
1735        number coef=kBucketPolyRed(P.bucket,strat->S[j],
1736                                   strat->lenS[j]/*pLength(strat->S[j])*/,
1737                                   strat->kNoether);
1738        nDelete(&coef);
1739        h = kBucketGetLm(P.bucket);
1740        if (h==NULL) return NULL;
1741        P.p=h;
1742        P.t_p=NULL;
1743        P.SetShortExpVector();
1744#ifdef KDEBUG
1745        if (TEST_OPT_DEBUG)
1746        {
1747          PrintS("\nto:");
1748          wrp(h);
1749          PrintLn();
1750        }
1751#endif
1752      }
1753      else
1754      {
1755        kBucketClear(P.bucket,&(P.p),&len);
1756        kBucketDestroy(&P.bucket);
1757        pNormalize(P.p);
1758        return P.p;
1759      }
1760    }
1761}
1762#endif
1763#ifdef REDTAIL_S
1764
1765static poly redNFTail (poly h,const int sl,kStrategy strat, int len)
1766{
1767  if (h==NULL) return NULL;
1768  if (pNext(h)==NULL) return h;
1769  pTest(h);
1770  if (0 > sl)
1771    return h;
1772
1773  int j;
1774  poly res=h;
1775  poly act=res;
1776  LObject P(pNext(h));
1777  pNext(res)=NULL;
1778  P.bucket = kBucketCreate(currRing);
1779  len--;
1780  h=P.p;
1781  if (len <=0) len=pLength(h);
1782  kBucketInit(P.bucket,h /*P.p*/,len /*pLength(P.p)*/);
1783  pTest(h);
1784  loop
1785    {
1786      P.p=h;
1787      P.t_p=NULL;
1788      P.SetShortExpVector();
1789      loop
1790        {
1791          j=kFindDivisibleByInS(strat->S,strat->sevS,sl,&P);
1792          if (j>=0)
1793          {
1794#ifdef REDTAIL_PROT
1795            PrintS("r");
1796#endif
1797            nNormalize(pGetCoeff(P.p));
1798#ifdef KDEBUG
1799            if (TEST_OPT_DEBUG)
1800            {
1801              PrintS("red tail:");
1802              wrp(h);
1803              PrintS(" with ");
1804              wrp(strat->S[j]);
1805            }
1806#endif
1807            number coef;
1808            pTest(strat->S[j]);
1809            coef=kBucketPolyRed(P.bucket,strat->S[j],
1810                                strat->lenS[j]/*pLength(strat->S[j])*/,strat->kNoether);
1811            pMult_nn(res,coef);
1812            nDelete(&coef);
1813            h = kBucketGetLm(P.bucket);
1814            pTest(h);
1815            if (h==NULL)
1816            {
1817#ifdef REDTAIL_PROT
1818              PrintS(" ");
1819#endif
1820              return res;
1821            }
1822            pTest(h);
1823            P.p=h;
1824            P.t_p=NULL;
1825            P.SetShortExpVector();
1826#ifdef KDEBUG
1827            if (TEST_OPT_DEBUG)
1828            {
1829              PrintS("\nto tail:");
1830              wrp(h);
1831              PrintLn();
1832            }
1833#endif
1834          }
1835          else
1836          {
1837#ifdef REDTAIL_PROT
1838            PrintS("n");
1839#endif
1840            break;
1841          }
1842        } /* end loop current mon */
1843      poly tmp=pHead(h /*kBucketGetLm(P.bucket)*/);
1844      act->next=tmp;pIter(act);
1845      poly tmp2=pHead(h);
1846      pNeg(tmp2);
1847      int ltmp2=1;
1848      pTest(tmp2);
1849      kBucket_Add_q(P.bucket, tmp2, &ltmp2);
1850
1851      h = kBucketGetLm(P.bucket);
1852      if (h==NULL)
1853      {
1854#ifdef REDTAIL_PROT
1855        PrintS(" ");
1856#endif
1857        return res;
1858      }
1859      pTest(h);
1860    }
1861}
1862#endif
1863
1864static void do_this_spoly_stuff(int i,int j,calc_dat* c){
1865  poly f=c->S->m[i];
1866  poly g=c->S->m[j];
1867  poly h=ksOldCreateSpoly(f, g, NULL, c->r);
1868  poly hr=NULL;
1869#ifdef FULLREDUCTIONS
1870  if (h!=NULL)
1871  {
1872    int len;
1873
1874    hr=redNF2(h,c,len);
1875//      hr=redNF(h,c->strat,len);
1876
1877    if (hr!=NULL)
1878#ifdef REDTAIL_S
1879      hr = redNFTail(hr,c->strat->sl,c->strat,len);
1880#else
1881    hr = redtailBba(hr,c->strat->sl,c->strat);
1882#endif
1883
1884  }
1885#else
1886  if (h!=NULL)
1887  {
1888    int len;
1889    hr=redNF2(h,c,len);
1890  }
1891#endif
1892  c->normal_forms++;
1893  if (hr==NULL)
1894  {
1895    notice_miss(i, j, c);
1896
1897  }
1898  else
1899  {
1900    c->misses_series=0;
1901#ifdef HEAD_BIN
1902    hr=p_MoveHead(hr,c->HeadBin);
1903#endif
1904    add_to_basis(hr, i,j,c);
1905  }
1906}
1907//try to fill, return FALSE iff queue is empty
1908static int find_next_empty(calc_dat* c){
1909  int n;
1910  for(n=0;n<PAR_N;n++)
1911    if(c->work_on[n].is_free)
1912      break;
1913  return n;
1914}
1915static BOOLEAN fillup(calc_dat* c){
1916  int n=find_next_empty(c);
1917  sorted_pair_node* p;
1918  while (n<PAR_N) {
1919    if 
1920      (p=find_next_pair2(c, TRUE)) //the = is wanted
1921    {
1922      if(p->i<0){
1923        init_red_input_element(c,p->lcm_of_lm,n);
1924        omfree(p);
1925      }
1926     
1927      else {
1928        init_red_phase1(c,p->i,p->j,n);
1929        free_sorted_pair_node(p,c->r);
1930      }
1931    } 
1932    else {
1933      if (n==0) return (!is_empty(c));
1934      return TRUE;
1935    }
1936   
1937    n=find_next_empty(c);
1938  }
1939  return TRUE;
1940}
1941
1942static BOOLEAN compute(calc_dat* c){
1943  int i=(c->last_index+ 1)%PAR_N;
1944  int len;
1945  BOOLEAN suc=FALSE;
1946  while(1){
1947    if(!c->work_on[i].is_free){
1948      if ((!c->work_on[i].need_std_rep) && (good_has_t_rep(c->work_on[i].i,c->work_on[i].j,c))){
1949        suc=TRUE;
1950        //clear ressources ;; needs to be done
1951        c->work_on[i].is_free=TRUE;
1952       
1953        trep_them(&c->work_on[i],c);
1954        c->work_on[i].soon_free=NULL;
1955           
1956        c->work_on[i].is_free=TRUE;
1957        c->last_index=i;
1958        return suc;
1959      }
1960      if (!c->work_on[i].started)
1961        init_red_spoly_phase2(c,i);
1962      if (!c->work_on[i].is_free){
1963//      Print("Computing i=%i,j=%i",c->work_on[i].i,c->work_on[i].j);
1964        if(redNF2_n_steps(&(c->work_on[i]),c,((c->reduction_steps+50)/(c->normal_forms+1))/2+5)){
1965         
1966
1967          trep_them(&c->work_on[i],c);
1968           
1969          c->work_on[i].soon_free=NULL;
1970           
1971          c->work_on[i].is_free=TRUE;
1972          poly hr=c->work_on[i].h;
1973          len=pLength(hr);
1974          suc=TRUE;
1975          int c2=0;
1976          for(c2=0;c2<=c->strat->sl;c2++){
1977            if(c->strat->lenS[c2]>2)
1978              break;
1979          }
1980          int sec_sl=c->strat->sl;
1981          c->strat->sl=c2-1;
1982
1983#ifdef FULLREDUCTIONS
1984          if (hr!=NULL)
1985#ifdef REDTAIL_S
1986            hr = redNFTail(hr,c->strat->sl,c->strat,len);
1987#else
1988          hr = redtailBba(hr,c->strat->sl,c->strat);
1989#endif
1990#endif
1991          c->strat->sl=sec_sl;
1992          c->normal_forms++;
1993         
1994          if (hr==NULL)
1995          {
1996            notice_miss(c->work_on[i].i, c->work_on[i].j, c);
1997
1998          }
1999          else
2000          {
2001            c->misses_series=0;
2002#ifdef HEAD_BIN
2003            hr=p_MoveHead(hr,c->HeadBin);
2004#endif
2005            add_to_basis(hr, c->work_on[i].i,c->work_on[i].j,c);
2006         
2007          }
2008        }
2009      }
2010      else {
2011        suc=TRUE;
2012        int_pair_node* hf=c->work_on[i].soon_free;
2013        //now_t_rep(c->work_on[i].i,c->work_on[i].j,c);
2014        while(hf!=NULL)
2015        {
2016          int_pair_node* s=hf;
2017          now_t_rep(hf->a,hf->b,c);
2018           
2019          hf=hf->next;
2020          omfree(s);
2021        }
2022        c->work_on[i].soon_free=NULL;
2023        PrintS("-");
2024        c->misses_counter++;
2025        if(c->work_on[i].i>=0){
2026        c->misses[c->work_on[i].i]++;
2027        c->misses[c->work_on[i].j]++;
2028        }
2029        c->misses_series++;
2030      }
2031      c->last_index=i;
2032      return suc;
2033    }
2034   
2035   
2036   
2037    i++;
2038    i=i%PAR_N;
2039  }
2040}
2041static int poly_crit(const void* ap1, const void* ap2){
2042  poly p1,p2;
2043  p1=*((poly*) ap1);
2044  p2=*((poly*)ap2);
2045 
2046  if (pLmCmp(p1,p2)!=0) return pLmCmp(p1,p2);
2047  if (pLength(p1)<pLength(p2)) return -1;
2048  if (pLength(p1)>pLength(p2)) return 1;
2049  return 0;
2050}
2051ideal t_rep_gb(ring r,ideal arg_I){
2052   Print("Idelems %i \n----------\n",IDELEMS(arg_I));
2053  ideal I_temp=idCopy(arg_I); //kInterRed(arg_I);
2054  ideal I=idCompactify(I_temp);
2055  qsort(I->m,IDELEMS(I),sizeof(poly),poly_crit);
2056  idDelete(&I_temp);
2057  Print("Idelems %i \n----------\n",IDELEMS(I));
2058  calc_dat* c=(calc_dat*) omalloc(sizeof(calc_dat));
2059  c->r=currRing;
2060 
2061  initial_data2(c,I);
2062#if 0
2063  while (find_next_pair(c)){
2064    int i,j;
2065    int z;
2066    i=c->found_i;
2067    j=c->found_j;
2068    replace_pair(i,j,c);
2069    if (!(c->states[max(i,j)][min(i,j)]==HASTREP)){
2070      do_this_spoly_stuff(i,j,c);
2071    }
2072    //else
2073    //  PrintS("f");
2074
2075    now_t_rep(i,j,c);
2076    now_t_rep(c->found_i,c->found_j,c);
2077#ifdef RANDOM_WALK
2078    c->continue_i=my_rand(c->n-1)+1;
2079    c->continue_j=my_rand(c->continue_i);
2080#endif
2081    int_pair_node* h=c->soon_free;
2082    while(h!=NULL)
2083    {
2084      int_pair_node* s=h;
2085      now_t_rep(h->a,h->b,c);
2086
2087      h=h->next;
2088      omfree(s);
2089    }
2090
2091
2092  }
2093#endif
2094  BOOLEAN SUC=TRUE;
2095  while((!SUC)||fillup(c)){
2096    if (is_empty(c)) break;
2097    SUC=compute(c);
2098  }
2099  omfree(c->rep);
2100  for(int z=0;z<c->n;z++){
2101    omfree(c->states[z]);
2102  }
2103  omfree(c->states);
2104  omfree(c->lengths);
2105  printf("calculated %d NFs\n",c->normal_forms);
2106  I=c->S;
2107  IDELEMS(I)=c->n;
2108  omfree(c);
2109 
2110  return(I);
2111}
2112static void now_t_rep(const int & arg_i, const int & arg_j, calc_dat* c){
2113  int i,j;
2114  if (arg_i==arg_j){
2115    return;
2116  }
2117  if (arg_i>arg_j){
2118    i=arg_j;
2119    j=arg_i;
2120  } else {
2121    i=arg_i;
2122    j=arg_j;
2123  }
2124  c->states[j][i]=HASTREP;
2125}
2126static void soon_t_rep(const int& arg_i, const int& arg_j, calc_dat* c)
2127{
2128  assume(0<=arg_i);
2129  assume(0<=arg_j);
2130  assume(arg_i<c->n);
2131  assume(arg_j<c->n);
2132  int i,j;
2133  if (arg_i==arg_j){
2134    return;
2135  }
2136  if (arg_i>arg_j){
2137    i=arg_j;
2138    j=arg_i;
2139  } else {
2140    i=arg_i;
2141    j=arg_j;
2142  }
2143  if (!
2144      (c->states[j][i]==HASTREP))
2145    c->states[j][i]=SOONTREP;
2146}
2147static BOOLEAN has_t_rep(const int & arg_i, const  int & arg_j, calc_dat* state){
2148  assume(0<=arg_i);
2149  assume(0<=arg_j);
2150  assume(arg_i<state->n);
2151  assume(arg_j<state->n);
2152  if (arg_i==arg_j)
2153  {
2154    return (TRUE);
2155  }
2156  if (arg_i>arg_j)
2157  {
2158    return (state->states[arg_i][arg_j]==HASTREP);
2159  } else
2160  {
2161    return (state->states[arg_j][arg_i]==HASTREP);
2162  }
2163}
2164static int pLcmDeg(poly a, poly b)
2165{
2166  int i;
2167  int n=0;
2168  for (i=pVariables; i; i--)
2169  {
2170    n+=max( pGetExp(a,i), pGetExp(b,i));
2171  }
2172  return n;
2173
2174}
2175static int pMinDeg3(poly f){
2176  if (f==NULL){
2177    return(-1);
2178
2179  }
2180
2181  poly h=f->next;
2182  int n=pFDeg(h);
2183  int i=0;
2184  while((i<2)){
2185    if (h==NULL)
2186      return(n);
2187    h=h->next;
2188    if (h!=NULL){
2189      n=min(n,pFDeg(h));
2190    }
2191    i++;
2192  }
2193
2194  return n;
2195}
2196
2197
2198static void shorten_tails(calc_dat* c, poly monom)
2199{
2200// BOOLEAN corr=lenS_correct(c->strat);
2201  for(int i=0;i<c->n;i++)
2202  {
2203    //enter tail
2204    if (c->rep[i]!=i) continue;
2205    if (c->S->m[i]==NULL) continue;
2206    poly tail=c->S->m[i]->next;
2207    poly prev=c->S->m[i];
2208    BOOLEAN did_something=FALSE;
2209    while((tail!=NULL)&& (pLmCmp(tail, monom)>=0))
2210    {
2211      if (p_LmDivisibleBy(monom,tail,c->r))
2212      {
2213        did_something=TRUE;
2214        prev->next=tail->next;
2215        tail->next=NULL;
2216        p_Delete(& tail,c->r);
2217        tail=prev;
2218        //PrintS("Shortened");
2219        c->lengths[i]--;
2220      }
2221      prev=tail;
2222      tail=tail->next;
2223    }
2224    if (did_something)
2225    {
2226      int new_pos=simple_posInS(c->strat,c->S->m[i],c->lengths[i]);
2227      int old_pos=-1;
2228      //assume new_pos<old_pos
2229      for (int z=0;z<=c->strat->sl;z++)
2230      {
2231        if (c->strat->S[z]==c->S->m[i])
2232        {
2233          old_pos=z;
2234          break;
2235        }
2236      }
2237      if (old_pos== -1)
2238        for (int z=new_pos-1;z>=0;z--)
2239        {
2240          if (c->strat->S[z]==c->S->m[i])
2241          {
2242            old_pos=z;
2243            break;
2244          }
2245        }
2246      assume(old_pos>=0);
2247      assume(pLength(c->strat->S[old_pos])==c->lengths[i]);
2248      c->strat->lenS[old_pos]=c->lengths[i];
2249      if (new_pos<old_pos)
2250        move_forward_in_S(old_pos,new_pos,c->strat);
2251         
2252      length_one_crit(c,i,c->lengths[i]);
2253    }
2254     
2255  }
2256
2257}
2258static sorted_pair_node* pop_pair(calc_dat* c){
2259  clean_top_of_pair_list(c);
2260  if(c->pairs==NULL) return NULL;
2261  sorted_pair_node* h=c->pairs;
2262  c->pairs=c->pairs->next;
2263  return h;
2264 
2265}
2266static BOOLEAN no_pairs(calc_dat* c){
2267  clean_top_of_pair_list(c);
2268  return (c==NULL);
2269}
2270
2271static void clean_top_of_pair_list(calc_dat* c){
2272  while((c->pairs) && (c->pairs->i>=0) && (!state_is(UNCALCULATED,c->pairs->j, c->pairs->i,c))){
2273    sorted_pair_node* s=c->pairs;
2274    c->pairs=c->pairs->next;
2275    free_sorted_pair_node(s,c->r);
2276  }
2277}
2278static BOOLEAN state_is(calc_state state, const int & arg_i, const  int & arg_j, calc_dat* c){
2279  assume(0<=arg_i);
2280  assume(0<=arg_j);
2281  assume(arg_i<c->n);
2282  assume(arg_j<c->n);
2283  if (arg_i==arg_j)
2284  {
2285    return (TRUE);
2286  }
2287  if (arg_i>arg_j)
2288  {
2289    return (c->states[arg_i][arg_j]==state);
2290  }
2291  else return(c->states[arg_j][arg_i]==state);
2292}
2293static void free_sorted_pair_node(sorted_pair_node* s, ring r){
2294  if (s->i>=0)
2295    p_Delete(&s->lcm_of_lm,r);
2296  omfree(s);
2297}
2298static BOOLEAN pair_better(sorted_pair_node* a,sorted_pair_node* b, calc_dat* c){
2299  if (a->deg<b->deg) return TRUE;
2300  if (a->deg>b->deg) return FALSE;
2301 
2302  if (a->expected_length<b->expected_length) return TRUE;
2303  if (a->expected_length>b->expected_length) return FALSE;
2304  int comp=pLmCmp(a->lcm_of_lm, b->lcm_of_lm);
2305  if (comp==1) return FALSE;
2306  if (-1==comp) return TRUE;
2307  if (a->i<b->i) return TRUE;
2308  if (a->j<b->j) return TRUE;
2309  return FALSE;
2310}
2311
2312static int pair_better_gen(const void* ap,const void* bp){
2313
2314  sorted_pair_node* a=*((sorted_pair_node**)ap);
2315  sorted_pair_node* b=*((sorted_pair_node**)bp);
2316  assume(a->i>a->j);
2317  assume(b->i>b->j);
2318  if (a->deg<b->deg) return TRUE;
2319  if (a->deg>b->deg) return FALSE;
2320
2321 
2322  if (a->expected_length<b->expected_length) return TRUE;
2323   if (a->expected_length>b->expected_length) return FALSE;
2324 int comp=pLmCmp(a->lcm_of_lm, b->lcm_of_lm);
2325  if ((comp==0)&& (((a->i==b->i) && (a->j==b->j))||((a->j==b->i)&&(a->i==b->j))))
2326    return 0;
2327  if (comp==1) return -1;
2328  if (-1==comp) return 1;
2329  if (a->i<b->i) return 1;
2330  if (a->j<b->j) return 1;
2331  return 1;
2332}
2333static void sort_pair_in(int i, int j,calc_dat* c){
2334  assume(0<=i);
2335  assume(0<=j);
2336  assume(i<c->n);
2337  assume(j<c->n);
2338  assume(i!=j);
2339  sorted_pair_node* s=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
2340  s->i=max(i,j);
2341  s->j=min(i,j);
2342  poly lm=pOne();
2343
2344  pLcm(c->S->m[i], c->S->m[j], lm);
2345  pSetm(lm);
2346  s->lcm_of_lm=lm;
2347 
2348  sorted_pair_node* last=c->pairs;
2349  if (!c->pairs) {c->pairs=s;s->next=NULL;}
2350  else
2351  {
2352    sorted_pair_node* h=c->pairs->next;
2353    while((h!=NULL)&&(pair_better(h,s,c))){
2354      last=h;
2355      h=h->next;
2356    }
2357    last->next=s;
2358    s->next=h;
2359  }
2360}
Note: See TracBrowser for help on using the repository browser.