source: git/kernel/tgb.cc @ 612efe

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