source: git/kernel/tgb.cc @ cae954

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