source: git/kernel/tgb.cc @ 070cdff

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