source: git/kernel/tgb.cc @ 651f6f

spielwiese
Last change on this file since 651f6f was 651f6f, checked in by Hans Schönemann <hannes@…>, 17 years ago
*hannes: fixed naming git-svn-id: file:///usr/local/Singular/svn/trunk@9652 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 84.2 KB
Line 
1//! \file tgb.cc
2//       multiple rings
3//       shorten_tails und dessen Aufrufe pruefen wlength!!!
4/****************************************
5*  Computer Algebra System SINGULAR     *
6****************************************/
7/* $Id: tgb.cc,v 1.109 2007-01-09 11:21:15 Singular 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  // for SCA:
1529  // here write at the end of nodes_final[spc_final,...,spc_final+lmdeg-1]
1530  if(rIsSCA(c->r))
1531  {
1532    const poly pNext = pNext(h);
1533
1534    if(pNext != NULL)
1535    {
1536      // for additional polynomials
1537      const unsigned int m_iFirstAltVar = scaFirstAltVar(c->r);
1538      const unsigned int m_iLastAltVar  = scaLastAltVar(c->r);
1539
1540      int N = // c->r->N;
1541              m_iLastAltVar - m_iFirstAltVar + 1; // should be enough
1542      // TODO: but we may also use got = gcd({m}_{m\in f}))!
1543
1544       poly* array_arg=(poly*)omalloc(N*sizeof(poly)); // !
1545       int j = 0;
1546
1547
1548      for( unsigned short v = m_iFirstAltVar; v <= m_iLastAltVar; v++ )
1549      // for all x_v | Ann(lm(h))
1550      if( p_GetExp(h, v, c->r) ) // TODO: use 'got' here!
1551      {
1552        assume(p_GetExp(h, v, c->r)==1);
1553
1554        poly p = sca_pp_Mult_xi_pp(v, pNext, c->r); // x_v * h;
1555
1556        if(p != NULL) // if (x_v * h != 0)
1557          array_arg[j++] = p;
1558      } // for all x_v | Ann(lm(h))
1559
1560      c->introduceDelayedPairs(array_arg, j);
1561
1562      omfree(array_arg); // !!!
1563    }
1564//     Print("Saturation - done!!!\n");
1565  } // if SCAlgebra
1566
1567
1568  if(!ip){
1569    qsort(nodes_final,spc_final,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
1570
1571
1572    c->apairs=spn_merge(c->apairs,c->pair_top+1,nodes_final,spc_final,c);
1573    c->pair_top+=spc_final;
1574    clean_top_of_pair_list(c);
1575    omfree(nodes_final);
1576    return NULL;
1577  }
1578  {
1579    *ip=spc_final;
1580    return nodes_final;
1581  }
1582
1583
1584
1585}
1586
1587
1588static poly redNF2 (poly h,slimgb_alg* c , int &len, number&  m,int n)
1589{
1590  m=nInit(1);
1591  if (h==NULL) return NULL;
1592
1593  assume(len==pLength(h));
1594  kStrategy strat=c->strat;
1595  if (0 > strat->sl)
1596  {
1597    return h;
1598  }
1599  int j;
1600
1601  LObject P(h);
1602  P.SetShortExpVector();
1603  P.bucket = kBucketCreate(currRing);
1604  // BOOLEAN corr=lenS_correct(strat);
1605  kBucketInit(P.bucket,P.p,len /*pLength(P.p)*/);
1606  //wlen_set lenSw=(wlen_set) c->strat->lenS;
1607  //FIXME: plainly wrong
1608  //strat->lenS;
1609  //if (strat->lenSw!=NULL)
1610  //  lenSw=strat->lenSw;
1611  //int max_pos=simple_posInS(strat,P.p);
1612  loop
1613  {
1614      int dummy=strat->sl;
1615      j=kFindDivisibleByInS_easy(strat,P.p,P.sev);
1616      //j=kFindDivisibleByInS(strat,&dummy,&P);
1617      if ((j>=0) && ((!n)||
1618        ((strat->lenS[j]<=n) &&
1619         ((strat->lenSw==NULL)||
1620         (strat->lenSw[j]<=n)))))
1621      {
1622        nNormalize(pGetCoeff(P.p));
1623#ifdef KDEBUG
1624        if (TEST_OPT_DEBUG)
1625        {
1626          PrintS("red:");
1627          wrp(h);
1628          PrintS(" with ");
1629          wrp(strat->S[j]);
1630        }
1631#endif
1632
1633        number coef=kBucketPolyRed(P.bucket,strat->S[j],
1634                                   strat->lenS[j]/*pLength(strat->S[j])*/,
1635                                   strat->kNoether);
1636  number m2=nMult(m,coef);
1637  nDelete(&m);
1638  m=m2;
1639        nDelete(&coef);
1640        h = kBucketGetLm(P.bucket);
1641
1642  if (h==NULL) {
1643    len=0;
1644    kBucketDestroy(&P.bucket);
1645    return
1646    NULL;}
1647        P.p=h;
1648        P.t_p=NULL;
1649        P.SetShortExpVector();
1650#ifdef KDEBUG
1651        if (TEST_OPT_DEBUG)
1652        {
1653          PrintS("\nto:");
1654          wrp(h);
1655          PrintLn();
1656        }
1657#endif
1658      }
1659      else
1660      {
1661        kBucketClear(P.bucket,&(P.p),&len);
1662        kBucketDestroy(&P.bucket);
1663        pNormalize(P.p);
1664  assume(len==(pLength(P.p)));
1665        return P.p;
1666      }
1667    }
1668}
1669
1670
1671
1672static poly redTailShort(poly h, kStrategy strat){
1673  if (h==NULL) return NULL;//n_Init(1,currRing);
1674  if (TEST_V_MODPSOLVSB){
1675    bit_reduce(pNext(h), strat->tailRing);
1676  }
1677  int sl=strat->sl;
1678  int i;
1679  int len=pLength(h);
1680  for(i=0;i<=strat->sl;i++){
1681    if((strat->lenS[i]>2) || ((strat->lenSw!=NULL) && (strat->lenSw[i]>2)))
1682      break;
1683  }
1684  return(redNFTail(h,i-1,strat, len));
1685}
1686
1687static void line_of_extended_prod(int fixpos,slimgb_alg* c){
1688    if (c->gcd_of_terms[fixpos]==NULL)
1689  {
1690    c->gcd_of_terms[fixpos]=gcd_of_terms(c->S->m[fixpos],c->r);
1691    if (c->gcd_of_terms[fixpos])
1692    {
1693      int i;
1694      for(i=0;i<fixpos;i++)
1695        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)))
1696{
1697          c->states[fixpos][i]=HASTREP;
1698    c->extended_product_crit++;
1699}
1700      for(i=fixpos+1;i<c->n;i++)
1701        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)))
1702  {        c->states[i][fixpos]=HASTREP;
1703  c->extended_product_crit++;
1704  }
1705    }
1706  }
1707}
1708static void c_S_element_changed_hook(int pos, slimgb_alg* c){
1709  length_one_crit(c,pos, c->lengths[pos]);
1710  if (!c->nc)
1711    line_of_extended_prod(pos,c);
1712}
1713class poly_tree_node {
1714public:
1715  poly p;
1716  poly_tree_node* l;
1717  poly_tree_node* r;
1718  int n;
1719  poly_tree_node(int sn):l(NULL),r(NULL),n(sn){}
1720};
1721class exp_number_builder{
1722public:
1723  poly_tree_node* top_level;
1724  int n;
1725  int get_n(poly p);
1726  exp_number_builder():top_level(0),n(0){}
1727};
1728int exp_number_builder::get_n(poly p){
1729  poly_tree_node** node=&top_level;
1730  while(*node!=NULL){
1731    int c=pLmCmp(p,(*node)->p);
1732    if (c==0) return (*node)->n;
1733    if (c==-1) node=&((*node)->r);
1734    else
1735      node=&((*node)->l);
1736  }
1737  (*node)= new poly_tree_node(n);
1738  n++;
1739  (*node)->p=pLmInit(p);
1740  return (*node)->n;
1741}
1742
1743//mac_polys exp are smaller iff they are greater by monomial ordering
1744//corresponding to solving linear equations notation
1745
1746//! obsolete
1747struct int_poly_pair{
1748  poly p;
1749  int n;
1750};
1751
1752
1753//! obsolete
1754void t2ippa_rec(poly* ip,int* ia, poly_tree_node* k, int &offset){
1755    if(!k) return;
1756    t2ippa_rec(ip,ia,k->l,offset);
1757    ip[offset]=k->p;
1758    ia[k->n]=offset;
1759    ++offset;
1760
1761    t2ippa_rec(ip,ia,k->r,offset);
1762    delete k;
1763  }
1764
1765//! obsolete
1766void t2ippa(poly* ip,int* ia,exp_number_builder & e){
1767
1768  int o=0;
1769  t2ippa_rec(ip,ia,e.top_level,o);
1770}
1771int anti_poly_order(const void* a, const void* b){
1772  return -pLmCmp(((int_poly_pair*) a)->p,((int_poly_pair*) b)->p );
1773}
1774
1775BOOLEAN is_valid_ro(red_object & ro){
1776  red_object r2=ro;
1777  ro.validate();
1778  if ((r2.p!=ro.p)||(r2.sev!=ro.sev)) return FALSE;
1779  return TRUE;
1780}
1781
1782
1783
1784static void go_on (slimgb_alg* c){
1785  //set limit of 1000 for multireductions, at the moment for
1786  //programming reasons
1787  int i=0;
1788  c->average_length=0;
1789  for(i=0;i<c->n;i++){
1790    c->average_length+=c->lengths[i];
1791  }
1792  c->average_length=c->average_length/c->n;
1793  i=0;
1794  poly* p=(poly*) omalloc((PAR_N+1)*sizeof(poly));//nullterminated
1795
1796  int curr_deg=-1;
1797  while(i<PAR_N){
1798    sorted_pair_node* s=top_pair(c);//here is actually chain criterium done
1799
1800
1801    if (!s) break;
1802
1803    if(curr_deg>=0){
1804      if (s->deg >curr_deg) break;
1805    }
1806
1807    else curr_deg=s->deg;
1808    quick_pop_pair(c);
1809    if(s->i>=0){
1810      //be careful replace_pair use createShortSpoly which is not noncommutative
1811      now_t_rep(s->i,s->j,c);
1812    replace_pair(s->i,s->j,c);
1813
1814    if(s->i==s->j) {
1815      free_sorted_pair_node(s,c->r);
1816      continue;
1817    }
1818    now_t_rep(s->i,s->j,c);
1819    }
1820    poly h;
1821    if(s->i>=0){
1822      if (!c->nc)
1823  h=ksOldCreateSpoly(c->S->m[s->i], c->S->m[s->j], NULL, c->r);
1824      else
1825  h= nc_SPoly(c->S->m[s->i], c->S->m[s->j]/*, NULL*/, c->r);
1826    }
1827    else
1828      h=s->lcm_of_lm;
1829    // if(s->i>=0)
1830//       now_t_rep(s->j,s->i,c);
1831    number coef;
1832    int mlen=pLength(h);
1833    if (!c->nc){
1834      h=redNF2(h,c,mlen,coef,2);
1835      redTailShort(h,c->strat);
1836      nDelete(&coef);
1837    }
1838    free_sorted_pair_node(s,c->r);
1839    if(!h) continue;
1840    int len=pLength(h);
1841    p[i]=h;
1842
1843    i++;
1844  }
1845  p[i]=NULL;
1846//  pre_comp(p,i,c);
1847  if(i==0){
1848    omfree(p);
1849    return;
1850  }
1851  #ifdef TGB_RESORT_PAIRS
1852  c->replaced=new bool[c->n];
1853  c->used_b=FALSE;
1854  #endif
1855  red_object* buf=(red_object*) omalloc(i*sizeof(red_object));
1856  c->normal_forms+=i;
1857  int j;
1858  for(j=0;j<i;j++){
1859    buf[j].p=p[j];
1860    buf[j].sev=pGetShortExpVector(p[j]);
1861    buf[j].bucket = kBucketCreate(currRing);
1862    if (c->eliminationProblem){
1863        buf[j].sugar=pTotaldegree_full(p[j]);
1864    }
1865    int len=pLength(p[j]);
1866    kBucketInit(buf[j].bucket,buf[j].p,len);
1867    buf[j].initial_quality=buf[j].guess_quality(c);
1868    assume(buf[j].initial_quality>=0);
1869  }
1870  omfree(p);
1871  qsort(buf,i,sizeof(red_object),red_object_better_gen);
1872//    Print("\ncurr_deg:%i\n",curr_deg);
1873  if (TEST_OPT_PROT)
1874  {
1875    Print("%dM[%d,",curr_deg,i);
1876  }
1877#ifdef FIND_DETERMINISTIC
1878  c->modifiedS=(BOOLEAN*) omalloc((c->strat->sl+1)*sizeof(BOOLEAN));
1879  c->expandS=(poly*) omalloc((1)*sizeof(poly));
1880  c->expandS[0]=NULL;
1881  int z2;
1882  for(z2=0;z2<=c->strat->sl;z2++)
1883    c->modifiedS[z2]=FALSE;
1884#endif
1885  multi_reduction(buf, i, c);
1886  #ifdef TGB_RESORT_PAIRS
1887  if (c->used_b) {
1888    if (TEST_OPT_PROT)
1889        PrintS("B");
1890    int e;
1891    for(e=0;e<=c->pair_top;e++){
1892        if(c->apairs[e]->i<0) continue;
1893        assume(c->apairs[e]->j>=0);
1894        if ((c->replaced[c->apairs[e]->i])||(c->replaced[c->apairs[e]->j])) {
1895            sorted_pair_node* s=c->apairs[e];
1896            s->expected_length=pair_weighted_length(s->i,s->j,c);
1897        }
1898    }
1899    qsort(c->apairs,c->pair_top+1,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
1900  }
1901  #endif
1902#ifdef TGB_DEBUG
1903 {
1904   int k;
1905   for(k=0;k<i;k++)
1906   {
1907     assume(kFindDivisibleByInS_easy(c->strat,buf[k])<0);
1908     int k2;
1909     for(k2=0;k2<i;k2++)
1910     {
1911       if(k==k2) continue;
1912       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));
1913     }
1914   }
1915 }
1916#endif
1917  //resort S
1918#ifdef FIND_DETERMINISTIC
1919  for(z2=0;z2<=c->strat->sl;z2++)
1920  {
1921    if (c->modifiedS[z2])
1922    {
1923      wlen_type qual;
1924      int new_pos;
1925      if (c->strat->lenSw!=NULL)
1926          new_pos=simple_posInS(c->strat,c->strat->S[z2],strat->lenS[z2],strat->Sw[z2]);
1927      else
1928          new_pos=simple_posInS(c->strat,c->strat->S[z2],strat->lenS[z2],lenS[z2]);
1929
1930      if (new_pos<z2)
1931      {
1932         move_forward_in_S(z2,new_pos,c->strat);
1933      }
1934
1935      assume(new_pos<=z2);
1936    }
1937  }
1938  for(z2=0;c->expandS[z2]!=NULL;z2++)
1939  {
1940    add_to_reductors(c,c->expandS[z2],pLength(c->expandS[z2]));
1941    // PrintS("E");
1942  }
1943  omfree(c->modifiedS);
1944  c->modifiedS=NULL;
1945  omfree(c->expandS);
1946  c->expandS=NULL;
1947#endif
1948  if (TEST_OPT_PROT)
1949      Print("%i]",i);
1950
1951  int* ibuf=(int*) omalloc(i*sizeof(int));
1952  sorted_pair_node*** sbuf=(sorted_pair_node***) omalloc(i*sizeof(sorted_pair_node**));
1953  for(j=0;j<i;j++)
1954  {
1955    int len;
1956    poly p;
1957    buf[j].flatten();
1958    kBucketClear(buf[j].bucket,&p, &len);
1959    kBucketDestroy(&buf[j].bucket);
1960
1961    if (!c->nc) {
1962      if ((TEST_OPT_REDTAIL)&&(c->S->rank<=1)){
1963      p=redNFTail(p,c->strat->sl,c->strat, 0);
1964      } else {
1965      p=redTailShort(p, c->strat);
1966      }
1967      }
1968    sbuf[j]=add_to_basis_ideal_quotient(p,c,ibuf+j);
1969    //sbuf[j]=add_to_basis(p,-1,-1,c,ibuf+j);
1970  }
1971  int sum=0;
1972  for(j=0;j<i;j++){
1973    sum+=ibuf[j];
1974  }
1975  sorted_pair_node** big_sbuf=(sorted_pair_node**) omalloc(sum*sizeof(sorted_pair_node*));
1976  int partsum=0;
1977  for(j=0;j<i;j++)
1978  {
1979    memmove(big_sbuf+partsum, sbuf[j],ibuf[j]*sizeof(sorted_pair_node*));
1980    omfree(sbuf[j]);
1981    partsum+=ibuf[j];
1982  }
1983
1984  qsort(big_sbuf,sum,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
1985  c->apairs=spn_merge(c->apairs,c->pair_top+1,big_sbuf,sum,c);
1986  c->pair_top+=sum;
1987  clean_top_of_pair_list(c);
1988  omfree(big_sbuf);
1989  omfree(sbuf);
1990  omfree(ibuf);
1991  omfree(buf);
1992#ifdef TGB_DEBUG
1993  int z;
1994  for(z=1;z<=c->pair_top;z++)
1995  {
1996    assume(pair_better(c->apairs[z],c->apairs[z-1],c));
1997  }
1998#endif
1999  if (TEST_OPT_PROT)
2000      Print("(%d)",c->pair_top+1);
2001  while(!(idIs0(c->add_later))){
2002    ideal add=c->add_later;
2003    idSkipZeroes(add);
2004    for(j=0;j<add->idelems();j++){
2005        assume(pLength(add->m[j])==1);
2006        p_SetCoeff(add->m[j],n_Init(1,currRing),currRing);
2007
2008    }
2009    ideal add2=kInterRed(add,NULL);
2010    id_Delete(&add,currRing);
2011    idSkipZeroes(add2);
2012    c->add_later=idInit(ADD_LATER_SIZE,c->S->rank);
2013    memset(c->add_later->m,0,ADD_LATER_SIZE*sizeof(poly));
2014//     for(i=0;i<add2->idelems();i++){
2015//       if (add2->m[i]!=NULL)
2016//           add_to_basis_ideal_quotient(add2->m[i],-1,-1,c,NULL);
2017//       add2->m[i]=NULL;
2018//     }
2019    int i=add2->idelems();
2020    int* ibuf=(int*) omalloc(i*sizeof(int));
2021  sorted_pair_node*** sbuf=(sorted_pair_node***) omalloc(i*sizeof(sorted_pair_node**));
2022
2023  for(j=0;j<i;j++)
2024  {
2025    int len;
2026    poly p;
2027    //buf[j].flatten();
2028    //kBucketClear(buf[j].bucket,&p, &len);
2029    //kBucketDestroy(&buf[j].bucket);
2030    p=add2->m[j];
2031    add2->m[j]=NULL;
2032
2033    sbuf[j]=add_to_basis_ideal_quotient(p,c,ibuf+j);
2034    //sbuf[j]=add_to_basis(p,-1,-1,c,ibuf+j);
2035  }
2036  int sum=0;
2037  for(j=0;j<i;j++){
2038    sum+=ibuf[j];
2039  }
2040  sorted_pair_node** big_sbuf=(sorted_pair_node**) omalloc(sum*sizeof(sorted_pair_node*));
2041  int partsum=0;
2042  for(j=0;j<i;j++)
2043  {
2044    memmove(big_sbuf+partsum, sbuf[j],ibuf[j]*sizeof(sorted_pair_node*));
2045    omfree(sbuf[j]);
2046    partsum+=ibuf[j];
2047  }
2048
2049  qsort(big_sbuf,sum,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
2050  c->apairs=spn_merge(c->apairs,c->pair_top+1,big_sbuf,sum,c);
2051  c->pair_top+=sum;
2052  clean_top_of_pair_list(c);
2053  omfree(big_sbuf);
2054  omfree(sbuf);
2055  omfree(ibuf);
2056  //omfree(buf);
2057    id_Delete(&add2, c->r);
2058  }
2059  #ifdef TGB_RESORT_PAIRS
2060  delete c->replaced;
2061  c->replaced=NULL;
2062  c->used_b=FALSE;
2063  #endif
2064  return;
2065}
2066
2067
2068
2069#ifdef REDTAIL_S
2070
2071static poly redNFTail (poly h,const int sl,kStrategy strat, int len)
2072{
2073  if (h==NULL) return NULL;
2074  pTest(h);
2075  if (0 > sl)
2076    return h;
2077  if (pNext(h)==NULL) return h;
2078
2079  int j;
2080  poly res=h;
2081  poly act=res;
2082  LObject P(pNext(h));
2083  pNext(res)=NULL;
2084  P.bucket = kBucketCreate(currRing);
2085  len--;
2086  h=P.p;
2087  if (len <=0) len=pLength(h);
2088  kBucketInit(P.bucket,h /*P.p*/,len /*pLength(P.p)*/);
2089  pTest(h);
2090  loop
2091  {
2092      P.p=h;
2093      P.t_p=NULL;
2094      P.SetShortExpVector();
2095      loop
2096      {
2097          int dummy=strat->sl;
2098          j=kFindDivisibleByInS_easy(strat,P.p,P.sev);//kFindDivisibleByInS(strat,&dummy,&P);
2099          if (j>=0)
2100          {
2101#ifdef REDTAIL_PROT
2102            PrintS("r");
2103#endif
2104            nNormalize(pGetCoeff(P.p));
2105#ifdef KDEBUG
2106            if (TEST_OPT_DEBUG)
2107            {
2108              PrintS("red tail:");
2109              wrp(h);
2110              PrintS(" with ");
2111              wrp(strat->S[j]);
2112            }
2113#endif
2114            number coef;
2115            pTest(strat->S[j]);
2116            coef=kBucketPolyRed(P.bucket,strat->S[j],
2117                                strat->lenS[j]/*pLength(strat->S[j])*/,strat->kNoether);
2118            pMult_nn(res,coef);
2119            nDelete(&coef);
2120            h = kBucketGetLm(P.bucket);
2121            pTest(h);
2122            if (h==NULL)
2123            {
2124#ifdef REDTAIL_PROT
2125              PrintS(" ");
2126#endif
2127        kBucketDestroy(&P.bucket);
2128              return res;
2129            }
2130            pTest(h);
2131            P.p=h;
2132            P.t_p=NULL;
2133            P.SetShortExpVector();
2134#ifdef KDEBUG
2135            if (TEST_OPT_DEBUG)
2136            {
2137              PrintS("\nto tail:");
2138              wrp(h);
2139              PrintLn();
2140            }
2141#endif
2142          }
2143          else
2144          {
2145#ifdef REDTAIL_PROT
2146            PrintS("n");
2147#endif
2148            break;
2149          }
2150      } /* end loop current mon */
2151      //   poly tmp=pHead(h /*kBucketGetLm(P.bucket)*/);
2152      //act->next=tmp;pIter(act);
2153      act->next=kBucketExtractLm(P.bucket);pIter(act);
2154      h = kBucketGetLm(P.bucket);
2155      if (h==NULL)
2156      {
2157#ifdef REDTAIL_PROT
2158        PrintS(" ");
2159#endif
2160  kBucketDestroy(&P.bucket);
2161        return res;
2162      }
2163      pTest(h);
2164  }
2165}
2166#endif
2167
2168
2169//try to fill, return FALSE iff queue is empty
2170
2171//transfers ownership of m to mat
2172void init_with_mac_poly(tgb_sparse_matrix* mat, int row, mac_poly m){
2173  assume(mat->mp[row]==NULL);
2174  mat->mp[row]=m;
2175#ifdef TGB_DEBUG
2176  mac_poly r=m;
2177  while(r){
2178    assume(r->exp<mat->columns);
2179    r=r->next;
2180  }
2181#endif
2182}
2183poly free_row_to_poly(tgb_sparse_matrix* mat, int row, poly* monoms, int monom_index){
2184  poly p=NULL;
2185  poly* set_this=&p;
2186  mac_poly r=mat->mp[row];
2187  mat->mp[row]=NULL;
2188  while(r)
2189  {
2190    (*set_this)=pLmInit(monoms[monom_index-1-r->exp]);
2191    pSetCoeff((*set_this),r->coef);
2192    set_this=&((*set_this)->next);
2193    mac_poly old=r;
2194    r=r->next;
2195    delete old;
2196
2197  }
2198  return p;
2199
2200}
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212static int poly_crit(const void* ap1, const void* ap2){
2213  poly p1,p2;
2214  p1=*((poly*) ap1);
2215  p2=*((poly*)ap2);
2216
2217  int c=pLmCmp(p1,p2);
2218  if (c !=0) return c;
2219  int l1=pLength(p1);
2220  int l2=pLength(p2);
2221  if (l1<l2) return -1;
2222  if (l1>l2) return 1;
2223  return 0;
2224}
2225void slimgb_alg::introduceDelayedPairs(poly* pa,int s){
2226    if (s==0) return;
2227    sorted_pair_node** si_array=(sorted_pair_node**) omalloc(s* sizeof(sorted_pair_node*));
2228
2229    for( unsigned int i = 0; i < s; i++ )
2230    {
2231        sorted_pair_node* si=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
2232        si->i=-1;
2233        si->j=-2;
2234        poly p=pa[i];
2235        simplify_poly(p,r);
2236        si->expected_length=pQuality(p,this,pLength(p));
2237        si->deg=pTotaldegree_full(p);
2238        if (!rField_is_Zp(r)){
2239          pCleardenom(p);
2240        }
2241        si->lcm_of_lm=p;
2242
2243        //      c->apairs[n-1-i]=si;
2244        si_array[i]=si;
2245
2246  }
2247
2248  qsort(si_array,s,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
2249    apairs=spn_merge(apairs,pair_top+1,si_array,s,this);
2250  pair_top+=s;
2251}
2252slimgb_alg::slimgb_alg(ideal I, int syz_comp,BOOLEAN F4){
2253  completed=FALSE;
2254  this->syz_comp=syz_comp;
2255  r=currRing;
2256  nc=rIsPluralRing(r);
2257  this->lastDpBlockStart=get_last_dp_block_start(r);
2258  //Print("last dp Block start: %i\n", this->lastDpBlockStart);
2259  is_homog=TRUE;
2260  {
2261    int hz;
2262    for(hz=0;hz<IDELEMS(I);hz++){
2263      assume(I->m[hz]!=NULL);
2264      int d=pTotaldegree(I->m[hz]);
2265      poly t=I->m[hz]->next;
2266      while(t)
2267      {
2268        if (d!=pTotaldegree(t,r))
2269        {
2270          is_homog=FALSE;
2271          break;
2272        }
2273        t=t->next;
2274      }
2275      if(!(is_homog)) break;
2276    }
2277  }
2278  eliminationProblem=((!(is_homog))&&((pLexOrder)||(I->rank>0)));
2279
2280  //  Print("is homog:%d",c->is_homog);
2281  void* h;
2282  poly hp;
2283  int i,j;
2284  to_destroy=NULL;
2285  easy_product_crit=0;
2286  extended_product_crit=0;
2287  if (rField_is_Zp(r))
2288    isDifficultField=FALSE;
2289  else
2290    isDifficultField=TRUE;
2291  //not fully correct
2292  //(rChar()==0);
2293  F4_mode=F4;
2294
2295  reduction_steps=0;
2296  last_index=-1;
2297
2298  F=NULL;
2299  F_minus=NULL;
2300
2301  Rcounter=0;
2302
2303  soon_free=NULL;
2304
2305  tmp_lm=pOne();
2306
2307  normal_forms=0;
2308  current_degree=1;
2309
2310  max_pairs=5*I->idelems();
2311
2312  apairs=(sorted_pair_node**) omalloc(sizeof(sorted_pair_node*)*max_pairs);
2313  pair_top=-1;
2314
2315  int n=I->idelems();
2316  array_lengths=n;
2317
2318
2319  i=0;
2320  this->n=0;
2321  T_deg=(int*) omalloc(n*sizeof(int));
2322  if(eliminationProblem)
2323    T_deg_full=(int*) omalloc(n*sizeof(int));
2324  else
2325    T_deg_full=NULL;
2326  tmp_pair_lm=(poly*) omalloc(n*sizeof(poly));
2327  tmp_spn=(sorted_pair_node**) omalloc(n*sizeof(sorted_pair_node*));
2328  lm_bin=omGetSpecBin(POLYSIZE + (r->ExpL_Size)*sizeof(long));
2329#ifdef HEAD_BIN
2330  HeadBin=omGetSpecBin(POLYSIZE + (currRing->ExpL_Size)*sizeof(long));
2331#endif
2332  /* omUnGetSpecBin(&(c->HeadBin)); */
2333  #ifndef HAVE_BOOST
2334  #ifdef USE_STDVECBOOL
2335  #else
2336  h=omalloc(n*sizeof(char*));
2337
2338  states=(char**) h;
2339  #endif
2340  #endif
2341  h=omalloc(n*sizeof(int));
2342  lengths=(int*) h;
2343  weighted_lengths=(wlen_type*)omalloc(n*sizeof(wlen_type));
2344  gcd_of_terms=(poly*) omalloc(n*sizeof(poly));
2345
2346  short_Exps=(long*) omalloc(n*sizeof(long));
2347  if (F4_mode)
2348    S=idInit(n,I->rank);
2349  else
2350    S=idInit(1,I->rank);
2351  strat=new skStrategy;
2352  if (eliminationProblem)
2353    strat->honey=TRUE;
2354  strat->syzComp = 0;
2355  initBuchMoraCrit(strat);
2356  initBuchMoraPos(strat);
2357  strat->initEcart = initEcartBBA;
2358  strat->tailRing=r;
2359  strat->enterS = enterSBba;
2360  strat->sl = -1;
2361  i=n;
2362  i=1;//some strange bug else
2363  /* initS(c->S,NULL,c->strat); */
2364  /* intS start: */
2365  // i=((i+IDELEMS(c->S)+15)/16)*16;
2366  strat->ecartS=(intset)omAlloc(i*sizeof(int)); /*initec(i);*/
2367  strat->sevS=(unsigned long*)omAlloc0(i*sizeof(unsigned long));
2368  /*initsevS(i);*/
2369  strat->S_2_R=(int*)omAlloc0(i*sizeof(int));/*initS_2_R(i);*/
2370  strat->fromQ=NULL;
2371  strat->Shdl=idInit(1,1);
2372  strat->S=strat->Shdl->m;
2373  strat->lenS=(int*)omAlloc0(i*sizeof(int));
2374  if((isDifficultField)||(eliminationProblem))
2375    strat->lenSw=(wlen_type*)omAlloc0(i*sizeof(wlen_type));
2376  else
2377    strat->lenSw=NULL;
2378  sorted_pair_node* si;
2379  assume(n>0);
2380  add_to_basis_ideal_quotient(I->m[0],this,NULL);
2381
2382  assume(strat->sl==strat->Shdl->idelems()-1);
2383  if(!(F4_mode))
2384  {
2385        poly* array_arg=I->m;
2386        array_arg++;
2387        introduceDelayedPairs(array_arg,n-1);
2388        /*
2389    for (i=1;i<n;i++)//the 1 is wanted, because first element is added to basis
2390    {
2391      //     add_to_basis(I->m[i],-1,-1,c);
2392      si=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
2393      si->i=-1;
2394      si->j=-2;
2395      si->expected_length=pQuality(I->m[i],this,pLength(I->m[i]));
2396      si->deg=pTotaldegree(I->m[i]);
2397      if (!rField_is_Zp(r)){
2398        pCleardenom(I->m[i]);
2399      }
2400      si->lcm_of_lm=I->m[i];
2401
2402      //      c->apairs[n-1-i]=si;
2403      apairs[n-i-1]=si;
2404      ++(pair_top);
2405    }*/
2406  }
2407  else
2408  {
2409    for (i=1;i<n;i++)//the 1 is wanted, because first element is added to basis
2410      add_to_basis_ideal_quotient(I->m[i],this,NULL);
2411  }
2412  for(i=0;i<I->idelems();i++)
2413  {
2414    I->m[i]=NULL;
2415  }
2416  idDelete(&I);
2417  add_later=idInit(ADD_LATER_SIZE,S->rank);
2418  memset(add_later->m,0,ADD_LATER_SIZE*sizeof(poly));
2419}
2420slimgb_alg::~slimgb_alg(){
2421
2422  if (!(completed)){
2423      poly* add=(poly*) omalloc((pair_top+2)*sizeof(poly));
2424      int piter;
2425      int pos=0;
2426      for(piter=0;piter<=pair_top;piter++){
2427        sorted_pair_node* s=apairs[piter];
2428        if (s->i<0){
2429            //delayed element
2430            if (s->lcm_of_lm!=NULL){
2431                add[pos]=s->lcm_of_lm;
2432                pos++;
2433
2434            }
2435        }
2436
2437        free_sorted_pair_node(s,r);
2438        apairs[piter]=NULL;
2439      }
2440      pair_top=-1;
2441      add[pos]=NULL;
2442      pos=0;
2443      while(add[pos]!=NULL){
2444        add_to_basis_ideal_quotient(add[pos],this,NULL);
2445        pos++;
2446      }
2447      for(piter=0;piter<=pair_top;piter++){
2448        sorted_pair_node* s=apairs[piter];
2449        assume(s->i>=0);
2450        free_sorted_pair_node(s,r);
2451        apairs[piter]=NULL;
2452      }
2453      pair_top=-1;
2454  }
2455  id_Delete(&add_later,r);
2456  int i,j;
2457  slimgb_alg* c=this;
2458  while(c->to_destroy)
2459  {
2460    pDelete(&(c->to_destroy->p));
2461    poly_list_node* old=c->to_destroy;
2462    c->to_destroy=c->to_destroy->next;
2463    omfree(old);
2464  }
2465  while(c->F)
2466  {
2467    for(i=0;i<c->F->size;i++){
2468      pDelete(&(c->F->mp[i].m));
2469    }
2470    omfree(c->F->mp);
2471    c->F->mp=NULL;
2472    mp_array_list* old=c->F;
2473    c->F=c->F->next;
2474    omfree(old);
2475  }
2476  while(c->F_minus)
2477  {
2478    for(i=0;i<c->F_minus->size;i++){
2479      pDelete(&(c->F_minus->p[i]));
2480    }
2481    omfree(c->F_minus->p);
2482    c->F_minus->p=NULL;
2483    poly_array_list* old=c->F_minus;
2484    c->F_minus=c->F_minus->next;
2485    omfree(old);
2486  }
2487  #ifndef HAVE_BOOST
2488  #ifndef USE_STDVECBOOL
2489  for(int z=1 /* zero length at 0 */;z<c->n;z++){
2490    omfree(c->states[z]);
2491  }
2492  omfree(c->states);
2493  #endif
2494  #endif
2495
2496  omfree(c->lengths);
2497  omfree(c->weighted_lengths);
2498  for(int z=0;z<c->n;z++)
2499  {
2500    pDelete(&c->tmp_pair_lm[z]);
2501    omfree(c->tmp_spn[z]);
2502  }
2503  omfree(c->tmp_pair_lm);
2504  omfree(c->tmp_spn);
2505
2506  omfree(c->T_deg);
2507  if(c->T_deg_full)
2508    omfree(c->T_deg_full);
2509
2510  omFree(c->strat->ecartS);
2511  omFree(c->strat->sevS);
2512//   initsevS(i);
2513  omFree(c->strat->S_2_R);
2514
2515
2516  omFree(c->strat->lenS);
2517
2518  if(c->strat->lenSw)  omFree(c->strat->lenSw);
2519
2520
2521
2522
2523  for(i=0;i<c->n;i++){
2524    if(c->gcd_of_terms[i])
2525      pDelete(&(c->gcd_of_terms[i]));
2526  }
2527  omfree(c->gcd_of_terms);
2528
2529  omfree(c->apairs);
2530  if (TEST_OPT_PROT)
2531  {
2532    Print("calculated %d NFs\n",c->normal_forms);
2533    Print("applied %i product crit, %i extended_product crit \n", c->easy_product_crit, c->extended_product_crit);
2534  }
2535  int deleted_form_c_s=0;
2536
2537  for(i=0;i<=c->strat->sl;i++){
2538    if (!c->strat->S[i]) continue;
2539    BOOLEAN found=FALSE;
2540    for(j=0;j<c->n;j++){
2541      if (c->S->m[j]==c->strat->S[i]){
2542        found=TRUE;
2543        break;
2544      }
2545    }
2546    if(!found) pDelete(&c->strat->S[i]);
2547  }
2548//   for(i=0;i<c->n;i++){
2549//     if (c->rep[i]!=i){
2550// //       for(j=0;j<=c->strat->sl;j++){
2551// //   if(c->strat->S[j]==c->S->m[i]){
2552// //     c->strat->S[j]=NULL;
2553// //     break;
2554// //   }
2555// //       }
2556// //      PrintS("R_delete");
2557//       pDelete(&c->S->m[i]);
2558//     }
2559//   }
2560
2561  if (completed){
2562  for(i=0;i<c->n;i++)
2563  {
2564    assume(c->S->m[i]!=NULL);
2565    if (p_GetComp(c->S->m[i],currRing)>this->syz_comp) continue;
2566    for(j=0;j<c->n;j++)
2567    {
2568      if((c->S->m[j]==NULL)||(i==j))
2569        continue;
2570      assume(p_LmShortDivisibleBy(c->S->m[j],c->short_Exps[j],
2571             c->S->m[i],~c->short_Exps[i],
2572             c->r)==p_LmDivisibleBy(c->S->m[j],
2573             c->S->m[i],
2574             c->r));
2575      if (p_LmShortDivisibleBy(c->S->m[j],c->short_Exps[j],
2576          c->S->m[i],~c->short_Exps[i],
2577          c->r))
2578      {
2579        pDelete(&c->S->m[i]);
2580        break;
2581      }
2582    }
2583  }
2584  }
2585  omfree(c->short_Exps);
2586
2587
2588  ideal I=c->S;
2589
2590  IDELEMS(I)=c->n;
2591
2592  idSkipZeroes(I);
2593  for(i=0;i<=c->strat->sl;i++)
2594    c->strat->S[i]=NULL;
2595  id_Delete(&c->strat->Shdl,c->r);
2596  pDelete(&c->tmp_lm);
2597  omUnGetSpecBin(&lm_bin);
2598  delete c->strat;
2599}
2600ideal t_rep_gb(ring r,ideal arg_I, int syz_comp, BOOLEAN F4_mode){
2601
2602  //  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)));
2603
2604  if (TEST_OPT_PROT)
2605    if (F4_mode)
2606      PrintS("F4 Modus \n");
2607
2608  //debug_Ideal=arg_debug_Ideal;
2609  //if (debug_Ideal) PrintS("DebugIdeal received\n");
2610  // Print("Idelems %i \n----------\n",IDELEMS(arg_I));
2611  ideal I=idCompactify(arg_I);
2612   int i;
2613  if (idIs0(I)) return I;
2614  for(i=0;i<IDELEMS(I);i++)
2615  {
2616    assume(I->m[i]!=NULL);
2617    simplify_poly(I->m[i],currRing);
2618  }
2619
2620
2621  qsort(I->m,IDELEMS(I),sizeof(poly),poly_crit);
2622  //Print("Idelems %i \n----------\n",IDELEMS(I));
2623  //slimgb_alg* c=(slimgb_alg*) omalloc(sizeof(slimgb_alg));
2624  //int syz_comp=arg_I->rank;
2625  slimgb_alg* c=new slimgb_alg(I, syz_comp,F4_mode);
2626
2627
2628  while ((c->pair_top>=0) && ((!(TEST_OPT_DEGBOUND)) || (c->apairs[c->pair_top]->deg<=Kstd1_deg)))
2629  {
2630    if(F4_mode)
2631      go_on_F4(c);
2632    else
2633      go_on(c);
2634  }
2635  if (c->pair_top<0)
2636    c->completed=TRUE;
2637  I=c->S;
2638  delete c;
2639  if (TEST_OPT_REDSB){
2640    ideal erg=kInterRed(I,NULL);
2641    assume(I!=erg);
2642    id_Delete(&I, currRing);
2643    return erg;
2644  }
2645  //qsort(I->m, IDELEMS(I),sizeof(poly),pLmCmp_func);
2646  assume(I->rank>=idRankFreeModule(I));
2647  return(I);
2648}
2649void now_t_rep(const int & arg_i, const int & arg_j, slimgb_alg* c){
2650  int i,j;
2651  if (arg_i==arg_j){
2652    return;
2653  }
2654  if (arg_i>arg_j){
2655    i=arg_j;
2656    j=arg_i;
2657  } else {
2658    i=arg_i;
2659    j=arg_j;
2660  }
2661  c->states[j][i]=HASTREP;
2662}
2663
2664static BOOLEAN has_t_rep(const int & arg_i, const  int & arg_j, slimgb_alg* state){
2665  assume(0<=arg_i);
2666  assume(0<=arg_j);
2667  assume(arg_i<state->n);
2668  assume(arg_j<state->n);
2669  if (arg_i==arg_j)
2670  {
2671    return (TRUE);
2672  }
2673  if (arg_i>arg_j)
2674  {
2675    return (state->states[arg_i][arg_j]==HASTREP);
2676  } else
2677  {
2678    return (state->states[arg_j][arg_i]==HASTREP);
2679  }
2680}
2681static int pLcmDeg(poly a, poly b)
2682{
2683  int i;
2684  int n=0;
2685  for (i=pVariables; i; i--)
2686  {
2687    n+=si_max( pGetExp(a,i), pGetExp(b,i));
2688  }
2689  return n;
2690
2691}
2692
2693
2694
2695static void shorten_tails(slimgb_alg* c, poly monom)
2696{
2697  return;
2698// BOOLEAN corr=lenS_correct(c->strat);
2699  for(int i=0;i<c->n;i++)
2700  {
2701    //enter tail
2702
2703    if (c->S->m[i]==NULL) continue;
2704    poly tail=c->S->m[i]->next;
2705    poly prev=c->S->m[i];
2706    BOOLEAN did_something=FALSE;
2707    while((tail!=NULL)&& (pLmCmp(tail, monom)>=0))
2708    {
2709      if (p_LmDivisibleBy(monom,tail,c->r))
2710      {
2711        did_something=TRUE;
2712        prev->next=tail->next;
2713        tail->next=NULL;
2714        p_Delete(& tail,c->r);
2715        tail=prev;
2716        //PrintS("Shortened");
2717        c->lengths[i]--;
2718      }
2719      prev=tail;
2720      tail=tail->next;
2721    }
2722    if (did_something)
2723    {
2724      int new_pos;
2725      wlen_type q;
2726      q=pQuality(c->S->m[i],c,c->lengths[i]);
2727      new_pos=simple_posInS(c->strat,c->S->m[i],c->lengths[i],q);
2728
2729      int old_pos=-1;
2730      //assume new_pos<old_pos
2731      for (int z=0;z<=c->strat->sl;z++)
2732      {
2733        if (c->strat->S[z]==c->S->m[i])
2734        {
2735          old_pos=z;
2736          break;
2737        }
2738      }
2739      if (old_pos== -1)
2740        for (int z=new_pos-1;z>=0;z--)
2741        {
2742          if (c->strat->S[z]==c->S->m[i])
2743          {
2744            old_pos=z;
2745            break;
2746          }
2747        }
2748      assume(old_pos>=0);
2749      assume(new_pos<=old_pos);
2750      assume(pLength(c->strat->S[old_pos])==c->lengths[i]);
2751      c->strat->lenS[old_pos]=c->lengths[i];
2752      if (c->strat->lenSw)
2753        c->strat->lenSw[old_pos]=q;
2754
2755      if (new_pos<old_pos)
2756        move_forward_in_S(old_pos,new_pos,c->strat);
2757
2758      length_one_crit(c,i,c->lengths[i]);
2759    }
2760  }
2761}
2762static sorted_pair_node* pop_pair(slimgb_alg* c){
2763  clean_top_of_pair_list(c);
2764
2765  if(c->pair_top<0) return NULL;
2766  else return (c->apairs[c->pair_top--]);
2767}
2768sorted_pair_node* top_pair(slimgb_alg* c){
2769  super_clean_top_of_pair_list(c);//yeah, I know, it's odd that I use a different proc here
2770
2771  if(c->pair_top<0) return NULL;
2772  else return (c->apairs[c->pair_top]);
2773}
2774sorted_pair_node* quick_pop_pair(slimgb_alg* c){
2775  if(c->pair_top<0) return NULL;
2776  else return (c->apairs[c->pair_top--]);
2777}
2778
2779
2780
2781static void super_clean_top_of_pair_list(slimgb_alg* c){
2782  while((c->pair_top>=0)
2783  && (c->apairs[c->pair_top]->i>=0)
2784  && (good_has_t_rep(c->apairs[c->pair_top]->j, c->apairs[c->pair_top]->i,c)))
2785  {
2786
2787    free_sorted_pair_node(c->apairs[c->pair_top],c->r);
2788    c->pair_top--;
2789
2790  }
2791}
2792void clean_top_of_pair_list(slimgb_alg* c){
2793  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))){
2794
2795    free_sorted_pair_node(c->apairs[c->pair_top],c->r);
2796    c->pair_top--;
2797
2798  }
2799}
2800static BOOLEAN state_is(calc_state state, const int & arg_i, const  int & arg_j, slimgb_alg* c){
2801  assume(0<=arg_i);
2802  assume(0<=arg_j);
2803  assume(arg_i<c->n);
2804  assume(arg_j<c->n);
2805  if (arg_i==arg_j)
2806  {
2807    return (TRUE);
2808  }
2809  if (arg_i>arg_j)
2810  {
2811    return (c->states[arg_i][arg_j]==state);
2812  }
2813  else return(c->states[arg_j][arg_i]==state);
2814}
2815
2816
2817void free_sorted_pair_node(sorted_pair_node* s, ring r){
2818  if (s->i>=0)
2819    p_Delete(&s->lcm_of_lm,r);
2820  omfree(s);
2821}
2822static BOOLEAN pair_better(sorted_pair_node* a,sorted_pair_node* b, slimgb_alg* c){
2823  if (a->deg<b->deg) return TRUE;
2824  if (a->deg>b->deg) return FALSE;
2825
2826
2827  int comp=pLmCmp(a->lcm_of_lm, b->lcm_of_lm);
2828  if (comp==1) return FALSE;
2829  if (-1==comp) return TRUE;
2830  if (a->expected_length<b->expected_length) return TRUE;
2831  if (a->expected_length>b->expected_length) return FALSE;
2832  if (a->i+a->j<b->i+b->j) return TRUE;
2833   if (a->i+a->j>b->i+b->j) return FALSE;
2834  if (a->i<b->i) return TRUE;
2835  if (a->i>b->i) return FALSE;
2836  return TRUE;
2837}
2838
2839static int tgb_pair_better_gen(const void* ap,const void* bp){
2840
2841  sorted_pair_node* a=*((sorted_pair_node**)ap);
2842  sorted_pair_node* b=*((sorted_pair_node**)bp);
2843  assume((a->i>a->j) || (a->i < 0));
2844  assume((b->i>b->j) || (b->i < 0));
2845  if (a->deg<b->deg) return -1;
2846  if (a->deg>b->deg) return 1;
2847
2848
2849
2850 int comp=pLmCmp(a->lcm_of_lm, b->lcm_of_lm);
2851
2852  if (comp==1) return 1;
2853  if (-1==comp) return -1;
2854   if (a->expected_length<b->expected_length) return -1;
2855  if (a->expected_length>b->expected_length) return 1;
2856  if (a->i+a->j<b->i+b->j) return -1;
2857   if (a->i+a->j>b->i+b->j) return 1;
2858  if (a->i<b->i) return -1;
2859   if (a->i>b->i) return 1;
2860  return 0;
2861}
2862
2863
2864static poly gcd_of_terms(poly p, ring r){
2865  int max_g_0=0;
2866  assume(p!=NULL);
2867  int i;
2868  poly m=pOne();
2869  poly t;
2870  for (i=pVariables; i; i--)
2871  {
2872      pSetExp(m,i, pGetExp(p,i));
2873      if (max_g_0==0)
2874  if (pGetExp(m,i)>0)
2875    max_g_0=i;
2876  }
2877
2878  t=p->next;
2879  while (t!=NULL){
2880
2881    if (max_g_0==0) break;
2882    for (i=max_g_0; i; i--)
2883    {
2884      pSetExp(m,i, si_min(pGetExp(t,i),pGetExp(m,i)));
2885      if (max_g_0==i)
2886  if (pGetExp(m,i)==0)
2887    max_g_0=0;
2888      if ((max_g_0==0) && (pGetExp(m,i)>0)){
2889  max_g_0=i;
2890      }
2891    }
2892    t=t->next;
2893  }
2894
2895  if (max_g_0>0)
2896    return m;
2897  pDelete(&m);
2898  return NULL;
2899}
2900static inline BOOLEAN pHasNotCFExtended(poly p1, poly p2, poly m)
2901{
2902
2903  if (pGetComp(p1) > 0 || pGetComp(p2) > 0)
2904    return FALSE;
2905  int i = 1;
2906  loop
2907  {
2908    if ((pGetExp(p1, i)-pGetExp(m,i) >0) && (pGetExp(p2, i) -pGetExp(m,i)> 0))   return FALSE;
2909    if (i == pVariables)                                return TRUE;
2910    i++;
2911  }
2912}
2913
2914
2915//for impl reasons may return false if the the normal product criterion matches
2916static inline BOOLEAN extended_product_criterion(poly p1, poly gcd1, poly p2, poly gcd2, slimgb_alg* c){
2917  if (c->nc)
2918    return FALSE;
2919  if(gcd1==NULL) return FALSE;
2920        if(gcd2==NULL) return FALSE;
2921        gcd1->next=gcd2; //may ordered incorrect
2922        poly m=gcd_of_terms(gcd1,c->r);
2923        gcd1->next=NULL;
2924        if (m==NULL) return FALSE;
2925
2926        BOOLEAN erg=pHasNotCFExtended(p1,p2,m);
2927        pDelete(&m);
2928        return erg;
2929}
2930static poly kBucketGcd(kBucket* b, ring r)
2931{
2932  int s=0;
2933  int i;
2934  poly m, n;
2935  BOOLEAN initialized=FALSE;
2936  for (i=MAX_BUCKET-1;i>=0;i--)
2937  {
2938    if (b->buckets[i]!=NULL){
2939      if (!initialized){
2940  m=gcd_of_terms(b->buckets[i],r);
2941  initialized=TRUE;
2942  if (m==NULL) return NULL;
2943      }
2944      else
2945  {
2946    n=gcd_of_terms(b->buckets[i],r);
2947    if (n==NULL) {
2948      pDelete(&m);
2949      return NULL;
2950    }
2951    n->next=m;
2952    poly t=gcd_of_terms(n,r);
2953    n->next=NULL;
2954    pDelete(&m);
2955    pDelete(&n);
2956    m=t;
2957    if (m==NULL) return NULL;
2958
2959  }
2960    }
2961  }
2962  return m;
2963}
2964
2965
2966
2967
2968static inline wlen_type quality_of_pos_in_strat_S(int pos, slimgb_alg* c){
2969  if (c->strat->lenSw!=NULL) return c->strat->lenSw[pos];
2970  return c->strat->lenS[pos];
2971}
2972static inline wlen_type quality_of_pos_in_strat_S_mult_high(int pos, poly high, slimgb_alg* c)
2973  //meant only for nc
2974{
2975  poly m=pOne();
2976  pExpVectorDiff(m,high ,c->strat->S[pos]);
2977  poly product = mm_Mult_pp(m, c->strat->S[pos], c->r);
2978  wlen_type erg=pQuality(product,c);
2979  pDelete(&m);
2980  pDelete(&product);
2981  return erg;
2982}
2983
2984static void multi_reduction_lls_trick(red_object* los, int losl,slimgb_alg* c,find_erg & erg){
2985  erg.expand=NULL;
2986  BOOLEAN swap_roles; //from reduce_by, to_reduce_u if fromS
2987  if(erg.fromS){
2988    if(pLmEqual(c->strat->S[erg.reduce_by],los[erg.to_reduce_u].p))
2989    {
2990      int i;
2991      wlen_type quality_a=quality_of_pos_in_strat_S(erg.reduce_by,c);
2992      int best=erg.to_reduce_u+1;
2993/*
2994      for (i=erg.to_reduce_u;i>=erg.to_reduce_l;i--){
2995  int qc=los[i].guess_quality(c);
2996  if (qc<quality_a){
2997    best=i;
2998    quality_a=qc;
2999  }
3000      }
3001      if(best!=erg.to_reduce_u+1){*/
3002      wlen_type qc;
3003      best=find_best(los,erg.to_reduce_l,erg.to_reduce_u,qc,c);
3004      if(qc<quality_a){
3005  los[best].flatten();
3006  int b_pos=kBucketCanonicalize(los[best].bucket);
3007  los[best].p=los[best].bucket->buckets[b_pos];
3008  qc=pQuality(los[best].bucket->buckets[b_pos],c);
3009  if(qc<quality_a){
3010    red_object h=los[erg.to_reduce_u];
3011    los[erg.to_reduce_u]=los[best];
3012    los[best]=h;
3013    swap_roles=TRUE;
3014  }
3015  else
3016    swap_roles=FALSE;
3017      }
3018      else{
3019
3020  swap_roles=FALSE;
3021      }
3022
3023    }
3024      else
3025    {
3026      if (erg.to_reduce_u>erg.to_reduce_l){
3027
3028  int i;
3029  wlen_type quality_a=quality_of_pos_in_strat_S(erg.reduce_by,c);
3030  if (c->nc)
3031    quality_a=quality_of_pos_in_strat_S_mult_high(erg.reduce_by, los[erg.to_reduce_u].p, c);
3032  int best=erg.to_reduce_u+1;
3033  wlen_type qc;
3034  best=find_best(los,erg.to_reduce_l,erg.to_reduce_u,qc,c);
3035  assume(qc==los[best].guess_quality(c));
3036  if(qc<quality_a){
3037    los[best].flatten();
3038    int b_pos=kBucketCanonicalize(los[best].bucket);
3039    los[best].p=los[best].bucket->buckets[b_pos];
3040    qc==pQuality(los[best].bucket->buckets[b_pos],c);
3041    //(best!=erg.to_reduce_u+1)
3042    if(qc<quality_a){
3043    red_object h=los[erg.to_reduce_u];
3044    los[erg.to_reduce_u]=los[best];
3045    los[best]=h;
3046    erg.reduce_by=erg.to_reduce_u;
3047    erg.fromS=FALSE;
3048    erg.to_reduce_u--;
3049    }
3050  }
3051      }
3052      else
3053      {
3054  assume(erg.to_reduce_u==erg.to_reduce_l);
3055  wlen_type quality_a=
3056        quality_of_pos_in_strat_S(erg.reduce_by,c);
3057  wlen_type qc=los[erg.to_reduce_u].guess_quality(c);
3058  if (qc<0) PrintS("Wrong wlen_type");
3059  if(qc<quality_a){
3060    int best=erg.to_reduce_u;
3061    los[best].flatten();
3062    int b_pos=kBucketCanonicalize(los[best].bucket);
3063    los[best].p=los[best].bucket->buckets[b_pos];
3064    qc=pQuality(los[best].bucket->buckets[b_pos],c);
3065    assume(qc>=0);
3066    if(qc<quality_a){
3067      BOOLEAN exp=FALSE;
3068      if(qc<=2){
3069         //Print("\n qc is %lld \n",qc);
3070         exp=TRUE;
3071      }
3072
3073      else {
3074         if (qc<quality_a/2)
3075          exp=TRUE;
3076         else
3077       if(erg.reduce_by<c->n/4)
3078          exp=TRUE;
3079      }
3080      if (exp){
3081        poly clear_into;
3082        los[erg.to_reduce_u].flatten();
3083        kBucketClear(los[erg.to_reduce_u].bucket,&clear_into,&erg.expand_length);
3084        erg.expand=pCopy(clear_into);
3085        kBucketInit(los[erg.to_reduce_u].bucket,clear_into,erg.expand_length);
3086        if (TEST_OPT_PROT)
3087    PrintS("e");
3088
3089      }
3090    }
3091  }
3092
3093
3094      }
3095
3096      swap_roles=FALSE;
3097      return;
3098      }
3099
3100  }
3101  else{
3102    if(erg.reduce_by>erg.to_reduce_u){
3103      //then lm(rb)>= lm(tru) so =
3104      assume(erg.reduce_by==erg.to_reduce_u+1);
3105      int best=erg.reduce_by;
3106      wlen_type quality_a=los[erg.reduce_by].guess_quality(c);
3107      wlen_type qc;
3108      best=find_best(los,erg.to_reduce_l,erg.to_reduce_u,qc,c);
3109
3110      int i;
3111      if(qc<quality_a){
3112    red_object h=los[erg.reduce_by];
3113    los[erg.reduce_by]=los[best];
3114    los[best]=h;
3115  }
3116  swap_roles=FALSE;
3117  return;
3118
3119
3120    }
3121    else
3122    {
3123      assume(!pLmEqual(los[erg.reduce_by].p,los[erg.to_reduce_l].p));
3124      assume(erg.to_reduce_u==erg.to_reduce_l);
3125      //further assume, that reduce_by is the above all other polys
3126      //with same leading term
3127      int il=erg.reduce_by;
3128      wlen_type quality_a =los[erg.reduce_by].guess_quality(c);
3129      wlen_type qc;
3130      while((il>0) && pLmEqual(los[il-1].p,los[il].p)){
3131  il--;
3132  qc=los[il].guess_quality(c);
3133  if (qc<quality_a){
3134    quality_a=qc;
3135    erg.reduce_by=il;
3136  }
3137      }
3138      swap_roles=FALSE;
3139    }
3140
3141  }
3142  if(swap_roles)
3143  {
3144    if (TEST_OPT_PROT)
3145      PrintS("b");
3146    poly clear_into;
3147    int dummy_len;
3148    int new_length;
3149    int bp=erg.to_reduce_u;//bucket_positon
3150    //kBucketClear(los[bp].bucket,&clear_into,&new_length);
3151    new_length=los[bp].clear_to_poly();
3152    clear_into=los[bp].p;
3153    poly p=c->strat->S[erg.reduce_by];
3154    int j=erg.reduce_by;
3155    int old_length=c->strat->lenS[j];// in view of S
3156    los[bp].p=p;
3157    if (c->eliminationProblem){
3158        los[bp].sugar=pTotaldegree_full(p);
3159    }
3160    kBucketInit(los[bp].bucket,p,old_length);
3161    wlen_type qal=pQuality(clear_into,c,new_length);
3162    int pos_in_c=-1;
3163    int z;
3164    int new_pos;
3165    new_pos=simple_posInS(c->strat,clear_into,new_length, qal);
3166    assume(new_pos<=j);
3167    for (z=c->n;z;z--)
3168    {
3169      if(p==c->S->m[z-1])
3170      {
3171  pos_in_c=z-1;
3172  break;
3173      }
3174    }
3175
3176    int tdeg_full=-1;
3177    int tdeg=-1;
3178    if(pos_in_c>=0)
3179    {
3180      #ifdef TGB_RESORT_PAIRS
3181      c->used_b=TRUE;
3182      c->replaced[pos_in_c]=TRUE;
3183      #endif
3184      tdeg=c->T_deg[pos_in_c];
3185      c->S->m[pos_in_c]=clear_into;
3186      c->lengths[pos_in_c]=new_length;
3187      c->weighted_lengths[pos_in_c]=qal;
3188      if (c->gcd_of_terms[pos_in_c]==NULL)
3189        c->gcd_of_terms[pos_in_c]=gcd_of_terms(clear_into,c->r);
3190      if (c->T_deg_full)
3191        tdeg_full=c->T_deg_full[pos_in_c]=pTotaldegree_full(clear_into);
3192      else tdeg_full=tdeg;
3193      c_S_element_changed_hook(pos_in_c,c);
3194    } else {
3195      if (c->eliminationProblem){
3196        tdeg_full=pTotaldegree_full(clear_into);
3197        tdeg=pTotaldegree(clear_into);
3198      }
3199    }
3200    c->strat->S[j]=clear_into;
3201    c->strat->lenS[j]=new_length;
3202
3203    assume(pLength(clear_into)==new_length);
3204    if(c->strat->lenSw!=NULL)
3205      c->strat->lenSw[j]=qal;
3206    if (!rField_is_Zp(c->r))
3207    {
3208      pContent(clear_into);
3209      pCleardenom(clear_into);//should be unnecessary
3210    }
3211    else
3212      pNorm(clear_into);
3213#ifdef FIND_DETERMINISTIC
3214    erg.reduce_by=j;
3215    //resort later see diploma thesis, find_in_S must be deterministic
3216    //during multireduction if spolys are only in the span of the
3217    //input polys
3218#else
3219
3220    if (new_pos<j)
3221    {
3222      if (c->strat->honey) c->strat->ecartS[j]=tdeg_full-tdeg;
3223      move_forward_in_S(j,new_pos,c->strat);
3224      erg.reduce_by=new_pos;
3225    }
3226#endif
3227  }
3228}
3229static int fwbw(red_object* los, int i){
3230   int i2=i;
3231   int step=1;
3232
3233   BOOLEAN bw=FALSE;
3234   BOOLEAN incr=TRUE;
3235
3236   while(1)
3237   {
3238     if(!bw)
3239     {
3240       step=si_min(i2,step);
3241       if (step==0) break;
3242       i2-=step;
3243
3244       if(!pLmEqual(los[i].p,los[i2].p))
3245       {
3246   bw=TRUE;
3247   incr=FALSE;
3248       }
3249       else
3250       {
3251   if ((!incr) &&(step==1)) break;
3252       }
3253
3254
3255     }
3256     else
3257     {
3258
3259       step=si_min(i-i2,step);
3260       if (step==0) break;
3261       i2+=step;
3262       if(pLmEqual(los[i].p,los[i2].p)){
3263   if(step==1) break;
3264   else
3265   {
3266     bw=FALSE;
3267   }
3268       }
3269
3270     }
3271     if (incr)
3272       step*=2;
3273     else
3274     {
3275       if (step%2==1)
3276   step=(step+1)/2;
3277       else
3278   step/=2;
3279
3280     }
3281   }
3282   return i2;
3283}
3284static void canonicalize_region(red_object* los, int l, int u,slimgb_alg* c){
3285    assume(l<=u+1);
3286    int i;
3287    for(i=l;i<=u;i++){
3288        kBucketCanonicalize(los[i].bucket);
3289    }
3290
3291}
3292static void multi_reduction_find(red_object* los, int losl,slimgb_alg* c,int startf,find_erg & erg){
3293  kStrategy strat=c->strat;
3294
3295  assume(startf<=losl);
3296  assume((startf==losl-1)||(pLmCmp(los[startf].p,los[startf+1].p)==-1));
3297  int i=startf;
3298
3299  int j;
3300  while(i>=0){
3301    assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)<=0));
3302    assume(is_valid_ro(los[i]));
3303    assume((!(c->eliminationProblem))||(los[i].sugar>=pTotaldegree(los[i].p)));
3304    j=kFindDivisibleByInS_easy(strat,los[i]);
3305    if(j>=0){
3306
3307      erg.to_reduce_u=i;
3308      erg.reduce_by=j;
3309      erg.fromS=TRUE;
3310      int i2=fwbw(los,i);
3311      assume(pLmEqual(los[i].p,los[i2].p));
3312      assume((i2==0)||(!pLmEqual(los[i2].p,los[i2-1].p)));
3313      assume(i>=i2);
3314
3315
3316      erg.to_reduce_l=i2;
3317      assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)==-1));
3318      canonicalize_region(los,erg.to_reduce_u+1,startf,c);
3319      return;
3320    }
3321    if (j<0){
3322
3323      //not reduceable, try to use this for reducing higher terms
3324      int i2=fwbw(los,i);
3325      assume(pLmEqual(los[i].p,los[i2].p));
3326      assume((i2==0)||(!pLmEqual(los[i2].p,los[i2-1].p)));
3327      assume(i>=i2);
3328      if(i2!=i){
3329
3330
3331  erg.to_reduce_u=i-1;
3332  erg.to_reduce_l=i2;
3333  erg.reduce_by=i;
3334  erg.fromS=FALSE;
3335  assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)==-1));
3336  canonicalize_region(los,erg.to_reduce_u+1,startf,c);
3337  return;
3338      }
3339
3340      i--;
3341    }
3342  }
3343  erg.reduce_by=-1;//error code
3344  return;
3345}
3346
3347 //  nicht reduzierbare eintraege in ergebnisliste schreiben
3348//   nullen loeschen
3349//   while(finde_groessten leitterm reduzierbar(c,erg)){
3350
3351static int multi_reduction_clear_zeroes(red_object* los, int  losl, int l, int u)
3352{
3353
3354
3355  int deleted=0;
3356  int  i=l;
3357  int last=-1;
3358  while(i<=u)
3359  {
3360
3361    if(los[i].p==NULL){
3362      kBucketDestroy(&los[i].bucket);
3363//      delete los[i];//here we assume los are constructed with new
3364      //destroy resources, must be added here
3365     if (last>=0)
3366     {
3367       memmove(los+(int)(last+1-deleted),los+(last+1),sizeof(red_object)*(i-1-last));
3368     }
3369     last=i;
3370     deleted++;
3371    }
3372    i++;
3373  }
3374  if((last>=0)&&(last!=losl-1))
3375      memmove(los+(int)(last+1-deleted),los+last+1,sizeof(red_object)*(losl-1-last));
3376  return deleted;
3377
3378}
3379
3380static void sort_region_down(red_object* los, int l, int u, slimgb_alg* c)
3381{
3382  qsort(los+l,u-l+1,sizeof(red_object),red_object_better_gen);
3383  int i;
3384
3385  for(i=l;i<=u;i++)
3386  {
3387    BOOLEAN moved=FALSE;
3388    int j;
3389    for(j=i;j;j--)
3390    {
3391      if(pLmCmp(los[j].p,los[j-1].p)==-1){
3392  red_object h=los[j];
3393  los[j]=los[j-1];
3394  los[j-1]=h;
3395  moved=TRUE;
3396      }
3397      else break;
3398    }
3399    if(!moved) return;
3400  }
3401}
3402
3403//assume that los is ordered ascending by leading term, all non zero
3404static void multi_reduction(red_object* los, int & losl, slimgb_alg* c)
3405{
3406  poly* delay=(poly*) omalloc(losl*sizeof(poly));
3407  int delay_s=0;
3408  //initialize;
3409  assume(c->strat->sl>=0);
3410  assume(losl>0);
3411  int i;
3412  wlen_type max_initial_quality=0;
3413
3414  for(i=0;i<losl;i++){
3415    los[i].sev=pGetShortExpVector(los[i].p);
3416//SetShortExpVector();
3417    los[i].p=kBucketGetLm(los[i].bucket);
3418    if (los[i].initial_quality>max_initial_quality)
3419        max_initial_quality=los[i].initial_quality;
3420    // else
3421//         Print("init2_qal=%lld;", los[i].initial_quality);
3422//     Print("initial_quality=%lld;",max_initial_quality);
3423  }
3424
3425  kStrategy strat=c->strat;
3426  int curr_pos=losl-1;
3427
3428
3429//  nicht reduzierbare eintrï¿œe in ergebnisliste schreiben
3430  // nullen loeschen
3431  while(curr_pos>=0){
3432
3433    find_erg erg;
3434    multi_reduction_find(los, losl,c,curr_pos,erg);//last argument should be curr_pos
3435    if(erg.reduce_by<0) break;
3436
3437
3438
3439    erg.expand=NULL;
3440    int d=erg.to_reduce_u-erg.to_reduce_l+1;
3441
3442
3443    multi_reduction_lls_trick(los,losl,c,erg);
3444
3445
3446    int i;
3447    int len;
3448    //    wrp(los[erg.to_reduce_u].p);
3449    //Print("\n");
3450    multi_reduce_step(erg,los,c);
3451
3452
3453    if(!K_TEST_OPT_REDTHROUGH){
3454  for(i=erg.to_reduce_l;i<=erg.to_reduce_u;i++){
3455     if  (los[i].p!=NULL)  //the check (los[i].p!=NULL) might be invalid
3456     {
3457         //
3458         assume(los[i].initial_quality>0);
3459
3460               if(los[i].guess_quality(c)
3461                  >1.5*delay_factor*max_initial_quality){
3462                       if (TEST_OPT_PROT)
3463                           PrintS("v");
3464                       los[i].canonicalize();
3465                       if(los[i].guess_quality(c)
3466                           >delay_factor*max_initial_quality){
3467                               if (TEST_OPT_PROT)
3468                                   PrintS(".");
3469                               los[i].clear_to_poly();
3470                               //delay.push_back(los[i].p);
3471                               delay[delay_s]=los[i].p;
3472                               delay_s++;
3473
3474                               los[i].p=NULL;
3475
3476                      }
3477                  }
3478
3479            }
3480     }
3481  }
3482    int deleted=multi_reduction_clear_zeroes(los, losl, erg.to_reduce_l, erg.to_reduce_u);
3483    if(erg.fromS==FALSE)
3484      curr_pos=si_max(erg.to_reduce_u,erg.reduce_by);
3485    else
3486      curr_pos=erg.to_reduce_u;
3487    losl -= deleted;
3488    curr_pos -= deleted;
3489
3490    //Print("deleted %i \n",deleted);
3491    if ((TEST_V_UPTORADICAL) &&(!(erg.fromS)))
3492        sort_region_down(los,si_min(erg.to_reduce_l,erg.reduce_by),(si_max(erg.to_reduce_u,erg.reduce_by))-deleted,c);
3493    else
3494    sort_region_down(los, erg.to_reduce_l, erg.to_reduce_u-deleted, c);
3495
3496
3497    if(erg.expand)
3498    {
3499#ifdef FIND_DETERMINISTIC
3500      int i;
3501      for(i=0;c->expandS[i];i++);
3502      c->expandS=(poly*) omrealloc(c->expandS,(i+2)*sizeof(poly));
3503      c->expandS[i]=erg.expand;
3504      c->expandS[i+1]=NULL;
3505#else
3506      int ecart=0;
3507      if (c->eliminationProblem){
3508        ecart=pTotaldegree_full(erg.expand)-pTotaldegree(erg.expand);
3509      }
3510      add_to_reductors(c,erg.expand,erg.expand_length,ecart);
3511#endif
3512    }
3513
3514  }
3515
3516
3517  //sorted_pair_node** pairs=(sorted_pair_node**)
3518  //  omalloc(delay_s*sizeof(sorted_pair_node*));
3519  c->introduceDelayedPairs(delay,delay_s);
3520  /*
3521  for(i=0;i<delay_s;i++){
3522
3523      poly p=delay[i];
3524      //if (rPar(c->r)==0)
3525      simplify_poly(p,c->r);
3526      sorted_pair_node* si=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
3527      si->i=-1;
3528      si->j=-1;
3529       if (!rField_is_Zp(c->r)){
3530        if (!c->nc)
3531            p=redTailShort(p, c->strat);
3532        pCleardenom(p);
3533        pContent(p);
3534      }
3535      si->expected_length=pQuality(p,c,pLength(p));
3536      si->deg=pTotaldegree(p);
3537
3538      si->lcm_of_lm=p;
3539      pairs[i]=si;
3540  }
3541  qsort(pairs,delay_s,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
3542  c->apairs=spn_merge(c->apairs,c->pair_top+1,pairs,delay_s,c);
3543  c->pair_top+=delay_s;*/
3544  omfree(delay);
3545  //omfree(pairs);
3546  return;
3547}
3548void red_object::flatten(){
3549  assume(p==kBucketGetLm(bucket));
3550}
3551void red_object::validate(){
3552  p=kBucketGetLm(bucket);
3553  if(p)
3554    sev=pGetShortExpVector(p);
3555}
3556int red_object::clear_to_poly(){
3557  flatten();
3558  int l;
3559  kBucketClear(bucket,&p,&l);
3560  return l;
3561}
3562
3563
3564
3565
3566
3567void reduction_step::reduce(red_object* r, int l, int u){}
3568void simple_reducer::do_reduce(red_object & ro){
3569  number coef;
3570  if (!c->nc)
3571    coef=kBucketPolyRed(ro.bucket,p,
3572       p_len,
3573       c->strat->kNoether);
3574  else
3575    nc_BucketPolyRed_Z(ro.bucket, p, &coef);
3576  nDelete(&coef);
3577}
3578
3579
3580void simple_reducer::reduce(red_object* r, int l, int u){
3581  this->pre_reduce(r,l,u);
3582  int i;
3583//debug start
3584  int im;
3585
3586
3587  if(c->eliminationProblem){
3588    assume(p_LmEqual(r[l].p,r[u].p,c->r));
3589    /*int lm_deg=pTotaldegree(r[l].p);
3590    reducer_deg=lm_deg+pTotaldegree_full(p)-pTotaldegree(p);*/
3591  }
3592
3593  for(i=l;i<=u;i++){
3594
3595
3596
3597    this->do_reduce(r[i]);
3598    if (c->eliminationProblem){
3599        r[i].sugar=si_max(r[i].sugar,reducer_deg);
3600    }
3601  }
3602  for(i=l;i<=u;i++){
3603
3604    kBucketSimpleContent(r[i].bucket);
3605    r[i].validate();
3606    #ifdef TGB_DEBUG
3607    #endif
3608  }
3609}
3610reduction_step::~reduction_step(){}
3611simple_reducer::~simple_reducer(){
3612  if(fill_back!=NULL)
3613  {
3614    kBucketInit(fill_back,p,p_len);
3615  }
3616  fill_back=NULL;
3617
3618}
3619
3620void multi_reduce_step(find_erg & erg, red_object* r, slimgb_alg* c){
3621  static int id=0;
3622  id++;
3623  unsigned long sev;
3624    BOOLEAN lt_changed=FALSE;
3625  int rn=erg.reduce_by;
3626  poly red;
3627  int red_len;
3628  simple_reducer* pointer;
3629  BOOLEAN work_on_copy=FALSE;
3630  if(erg.fromS){
3631    red=c->strat->S[rn];
3632    red_len=c->strat->lenS[rn];
3633    assume(red_len==pLength(red));
3634  }
3635  else
3636  {
3637    r[rn].flatten();
3638    kBucketClear(r[rn].bucket,&red,&red_len);
3639
3640    if (!rField_is_Zp(c->r))
3641    {
3642      pContent(red);
3643      pCleardenom(red);//should be unnecessary
3644
3645    }
3646    pNormalize(red);
3647    if (c->eliminationProblem){
3648        r[rn].sugar=pTotaldegree_full(red);
3649    }
3650
3651    if ((!(erg.fromS))&&(TEST_V_UPTORADICAL)){
3652
3653         if (polynomial_root(red,c->r))
3654            lt_changed=TRUE;
3655            sev=p_GetShortExpVector(red,c->r);}
3656    red_len=pLength(red);
3657  }
3658  if (((TEST_V_MODPSOLVSB)&&(red_len>1))||((c->nc)||(erg.to_reduce_u-erg.to_reduce_l>5))){
3659    work_on_copy=TRUE;
3660    // poly m=pOne();
3661    poly m=c->tmp_lm;
3662    pSetCoeff(m,nInit(1));
3663    for(int i=1;i<=pVariables;i++)
3664      pSetExp(m,i,(pGetExp(r[erg.to_reduce_l].p, i)-pGetExp(red,i)));
3665    pSetm(m);
3666    poly red_cp;
3667    if (!c->nc)
3668      red_cp=ppMult_mm(red,m);
3669    else
3670      red_cp = mm_Mult_pp(m, red, c->r);
3671    if(!erg.fromS){
3672      kBucketInit(r[rn].bucket,red,red_len);
3673    }
3674    //now reduce the copy
3675    //static poly redNF2 (poly h,slimgb_alg* c , int &len, number&  m,int n)
3676
3677    if (!c->nc)
3678      redTailShort(red_cp,c->strat);
3679    //number mul;
3680    // red_len--;
3681//     red_cp->next=redNF2(red_cp->next,c,red_len,mul,c->average_length);
3682//     pSetCoeff(red_cp,nMult(red_cp->coef,mul));
3683//     nDelete(&mul);
3684//     red_len++;
3685    red=red_cp;
3686    red_len=pLength(red);
3687    // pDelete(&m);
3688
3689  }
3690  int i;
3691
3692
3693
3694  assume(red_len==pLength(red));
3695
3696  int reducer_deg=0;
3697  if (c->eliminationProblem){
3698     int lm_deg=pTotaldegree(r[erg.to_reduce_l].p);
3699     int ecart;
3700     if (erg.fromS){
3701       assume(pTotaldegree_full(red)<=c->T_deg_full[erg.reduce_by]);
3702       ecart=c->strat->ecartS[erg.reduce_by];
3703     } else {
3704       ecart=pTotaldegree_full(red)-lm_deg;
3705     }
3706     reducer_deg=lm_deg+ecart;
3707  }
3708  pointer=new simple_reducer(red,red_len,reducer_deg,c);
3709
3710  if ((!work_on_copy) && (!erg.fromS))
3711    pointer->fill_back=r[rn].bucket;
3712  else
3713    pointer->fill_back=NULL;
3714  pointer->reduction_id=id;
3715  pointer->c=c;
3716
3717  pointer->reduce(r,erg.to_reduce_l, erg.to_reduce_u);
3718  if(work_on_copy) pDelete(&pointer->p);
3719  delete pointer;
3720  if (lt_changed){
3721    assume(!erg.fromS);
3722    r[erg.reduce_by].sev=sev;
3723  }
3724
3725};
3726
3727
3728
3729
3730void simple_reducer:: pre_reduce(red_object* r, int l, int u){}
3731
Note: See TracBrowser for help on using the repository browser.