source: git/Singular/tgb.cc @ a202ed

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