source: git/kernel/tgb.cc @ 1d138c

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