source: git/kernel/tgb.cc @ 10ea45f

spielwiese
Last change on this file since 10ea45f was 10ea45f, checked in by Hans Schönemann <hannes@…>, 17 years ago
*hannes: idCompatify changed git-svn-id: file:///usr/local/Singular/svn/trunk@9679 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 84.3 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.111 2007-01-10 17:17:27 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("\nNF:%i product criterion:%i, ext_product criterion:%i \n", c->normal_forms, c->easy_product_crit, c->extended_product_crit);
2534     
2535   
2536  }
2537  int deleted_form_c_s=0;
2538
2539  for(i=0;i<=c->strat->sl;i++){
2540    if (!c->strat->S[i]) continue;
2541    BOOLEAN found=FALSE;
2542    for(j=0;j<c->n;j++){
2543      if (c->S->m[j]==c->strat->S[i]){
2544        found=TRUE;
2545        break;
2546      }
2547    }
2548    if(!found) pDelete(&c->strat->S[i]);
2549  }
2550//   for(i=0;i<c->n;i++){
2551//     if (c->rep[i]!=i){
2552// //       for(j=0;j<=c->strat->sl;j++){
2553// //   if(c->strat->S[j]==c->S->m[i]){
2554// //     c->strat->S[j]=NULL;
2555// //     break;
2556// //   }
2557// //       }
2558// //      PrintS("R_delete");
2559//       pDelete(&c->S->m[i]);
2560//     }
2561//   }
2562
2563  if (completed){
2564  for(i=0;i<c->n;i++)
2565  {
2566    assume(c->S->m[i]!=NULL);
2567    if (p_GetComp(c->S->m[i],currRing)>this->syz_comp) continue;
2568    for(j=0;j<c->n;j++)
2569    {
2570      if((c->S->m[j]==NULL)||(i==j))
2571        continue;
2572      assume(p_LmShortDivisibleBy(c->S->m[j],c->short_Exps[j],
2573             c->S->m[i],~c->short_Exps[i],
2574             c->r)==p_LmDivisibleBy(c->S->m[j],
2575             c->S->m[i],
2576             c->r));
2577      if (p_LmShortDivisibleBy(c->S->m[j],c->short_Exps[j],
2578          c->S->m[i],~c->short_Exps[i],
2579          c->r))
2580      {
2581        pDelete(&c->S->m[i]);
2582        break;
2583      }
2584    }
2585  }
2586  }
2587  omfree(c->short_Exps);
2588
2589
2590  ideal I=c->S;
2591
2592  IDELEMS(I)=c->n;
2593
2594  idSkipZeroes(I);
2595  for(i=0;i<=c->strat->sl;i++)
2596    c->strat->S[i]=NULL;
2597  id_Delete(&c->strat->Shdl,c->r);
2598  pDelete(&c->tmp_lm);
2599  omUnGetSpecBin(&lm_bin);
2600  delete c->strat;
2601}
2602ideal t_rep_gb(ring r,ideal arg_I, int syz_comp, BOOLEAN F4_mode){
2603
2604  //  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)));
2605
2606  if (TEST_OPT_PROT)
2607    if (F4_mode)
2608      PrintS("F4 Modus \n");
2609
2610  //debug_Ideal=arg_debug_Ideal;
2611  //if (debug_Ideal) PrintS("DebugIdeal received\n");
2612  // Print("Idelems %i \n----------\n",IDELEMS(arg_I));
2613  ideal I=idCopy(arg_I);
2614  idCompactify(I);
2615  if (idIs0(I)) return I;
2616  int i;
2617  for(i=0;i<IDELEMS(I);i++)
2618  {
2619    assume(I->m[i]!=NULL);
2620    simplify_poly(I->m[i],currRing);
2621  }
2622
2623
2624  qsort(I->m,IDELEMS(I),sizeof(poly),poly_crit);
2625  //Print("Idelems %i \n----------\n",IDELEMS(I));
2626  //slimgb_alg* c=(slimgb_alg*) omalloc(sizeof(slimgb_alg));
2627  //int syz_comp=arg_I->rank;
2628  slimgb_alg* c=new slimgb_alg(I, syz_comp,F4_mode);
2629
2630
2631  while ((c->pair_top>=0) && ((!(TEST_OPT_DEGBOUND)) || (c->apairs[c->pair_top]->deg<=Kstd1_deg)))
2632  {
2633    if(F4_mode)
2634      go_on_F4(c);
2635    else
2636      go_on(c);
2637  }
2638  if (c->pair_top<0)
2639    c->completed=TRUE;
2640  I=c->S;
2641  delete c;
2642  if (TEST_OPT_REDSB){
2643    ideal erg=kInterRed(I,NULL);
2644    assume(I!=erg);
2645    id_Delete(&I, currRing);
2646    return erg;
2647  }
2648  //qsort(I->m, IDELEMS(I),sizeof(poly),pLmCmp_func);
2649  assume(I->rank>=idRankFreeModule(I));
2650  return(I);
2651}
2652void now_t_rep(const int & arg_i, const int & arg_j, slimgb_alg* c){
2653  int i,j;
2654  if (arg_i==arg_j){
2655    return;
2656  }
2657  if (arg_i>arg_j){
2658    i=arg_j;
2659    j=arg_i;
2660  } else {
2661    i=arg_i;
2662    j=arg_j;
2663  }
2664  c->states[j][i]=HASTREP;
2665}
2666
2667static BOOLEAN has_t_rep(const int & arg_i, const  int & arg_j, slimgb_alg* state){
2668  assume(0<=arg_i);
2669  assume(0<=arg_j);
2670  assume(arg_i<state->n);
2671  assume(arg_j<state->n);
2672  if (arg_i==arg_j)
2673  {
2674    return (TRUE);
2675  }
2676  if (arg_i>arg_j)
2677  {
2678    return (state->states[arg_i][arg_j]==HASTREP);
2679  } else
2680  {
2681    return (state->states[arg_j][arg_i]==HASTREP);
2682  }
2683}
2684static int pLcmDeg(poly a, poly b)
2685{
2686  int i;
2687  int n=0;
2688  for (i=pVariables; i; i--)
2689  {
2690    n+=si_max( pGetExp(a,i), pGetExp(b,i));
2691  }
2692  return n;
2693
2694}
2695
2696
2697
2698static void shorten_tails(slimgb_alg* c, poly monom)
2699{
2700  return;
2701// BOOLEAN corr=lenS_correct(c->strat);
2702  for(int i=0;i<c->n;i++)
2703  {
2704    //enter tail
2705
2706    if (c->S->m[i]==NULL) continue;
2707    poly tail=c->S->m[i]->next;
2708    poly prev=c->S->m[i];
2709    BOOLEAN did_something=FALSE;
2710    while((tail!=NULL)&& (pLmCmp(tail, monom)>=0))
2711    {
2712      if (p_LmDivisibleBy(monom,tail,c->r))
2713      {
2714        did_something=TRUE;
2715        prev->next=tail->next;
2716        tail->next=NULL;
2717        p_Delete(& tail,c->r);
2718        tail=prev;
2719        //PrintS("Shortened");
2720        c->lengths[i]--;
2721      }
2722      prev=tail;
2723      tail=tail->next;
2724    }
2725    if (did_something)
2726    {
2727      int new_pos;
2728      wlen_type q;
2729      q=pQuality(c->S->m[i],c,c->lengths[i]);
2730      new_pos=simple_posInS(c->strat,c->S->m[i],c->lengths[i],q);
2731
2732      int old_pos=-1;
2733      //assume new_pos<old_pos
2734      for (int z=0;z<=c->strat->sl;z++)
2735      {
2736        if (c->strat->S[z]==c->S->m[i])
2737        {
2738          old_pos=z;
2739          break;
2740        }
2741      }
2742      if (old_pos== -1)
2743        for (int z=new_pos-1;z>=0;z--)
2744        {
2745          if (c->strat->S[z]==c->S->m[i])
2746          {
2747            old_pos=z;
2748            break;
2749          }
2750        }
2751      assume(old_pos>=0);
2752      assume(new_pos<=old_pos);
2753      assume(pLength(c->strat->S[old_pos])==c->lengths[i]);
2754      c->strat->lenS[old_pos]=c->lengths[i];
2755      if (c->strat->lenSw)
2756        c->strat->lenSw[old_pos]=q;
2757
2758      if (new_pos<old_pos)
2759        move_forward_in_S(old_pos,new_pos,c->strat);
2760
2761      length_one_crit(c,i,c->lengths[i]);
2762    }
2763  }
2764}
2765static sorted_pair_node* pop_pair(slimgb_alg* c){
2766  clean_top_of_pair_list(c);
2767
2768  if(c->pair_top<0) return NULL;
2769  else return (c->apairs[c->pair_top--]);
2770}
2771sorted_pair_node* top_pair(slimgb_alg* c){
2772  super_clean_top_of_pair_list(c);//yeah, I know, it's odd that I use a different proc here
2773
2774  if(c->pair_top<0) return NULL;
2775  else return (c->apairs[c->pair_top]);
2776}
2777sorted_pair_node* quick_pop_pair(slimgb_alg* c){
2778  if(c->pair_top<0) return NULL;
2779  else return (c->apairs[c->pair_top--]);
2780}
2781
2782
2783
2784static void super_clean_top_of_pair_list(slimgb_alg* c){
2785  while((c->pair_top>=0)
2786  && (c->apairs[c->pair_top]->i>=0)
2787  && (good_has_t_rep(c->apairs[c->pair_top]->j, c->apairs[c->pair_top]->i,c)))
2788  {
2789
2790    free_sorted_pair_node(c->apairs[c->pair_top],c->r);
2791    c->pair_top--;
2792
2793  }
2794}
2795void clean_top_of_pair_list(slimgb_alg* c){
2796  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))){
2797
2798    free_sorted_pair_node(c->apairs[c->pair_top],c->r);
2799    c->pair_top--;
2800
2801  }
2802}
2803static BOOLEAN state_is(calc_state state, const int & arg_i, const  int & arg_j, slimgb_alg* c){
2804  assume(0<=arg_i);
2805  assume(0<=arg_j);
2806  assume(arg_i<c->n);
2807  assume(arg_j<c->n);
2808  if (arg_i==arg_j)
2809  {
2810    return (TRUE);
2811  }
2812  if (arg_i>arg_j)
2813  {
2814    return (c->states[arg_i][arg_j]==state);
2815  }
2816  else return(c->states[arg_j][arg_i]==state);
2817}
2818
2819
2820void free_sorted_pair_node(sorted_pair_node* s, ring r){
2821  if (s->i>=0)
2822    p_Delete(&s->lcm_of_lm,r);
2823  omfree(s);
2824}
2825static BOOLEAN pair_better(sorted_pair_node* a,sorted_pair_node* b, slimgb_alg* c){
2826  if (a->deg<b->deg) return TRUE;
2827  if (a->deg>b->deg) return FALSE;
2828
2829
2830  int comp=pLmCmp(a->lcm_of_lm, b->lcm_of_lm);
2831  if (comp==1) return FALSE;
2832  if (-1==comp) return TRUE;
2833  if (a->expected_length<b->expected_length) return TRUE;
2834  if (a->expected_length>b->expected_length) return FALSE;
2835  if (a->i+a->j<b->i+b->j) return TRUE;
2836   if (a->i+a->j>b->i+b->j) return FALSE;
2837  if (a->i<b->i) return TRUE;
2838  if (a->i>b->i) return FALSE;
2839  return TRUE;
2840}
2841
2842static int tgb_pair_better_gen(const void* ap,const void* bp){
2843
2844  sorted_pair_node* a=*((sorted_pair_node**)ap);
2845  sorted_pair_node* b=*((sorted_pair_node**)bp);
2846  assume((a->i>a->j) || (a->i < 0));
2847  assume((b->i>b->j) || (b->i < 0));
2848  if (a->deg<b->deg) return -1;
2849  if (a->deg>b->deg) return 1;
2850
2851
2852
2853 int comp=pLmCmp(a->lcm_of_lm, b->lcm_of_lm);
2854
2855  if (comp==1) return 1;
2856  if (-1==comp) return -1;
2857   if (a->expected_length<b->expected_length) return -1;
2858  if (a->expected_length>b->expected_length) return 1;
2859  if (a->i+a->j<b->i+b->j) return -1;
2860   if (a->i+a->j>b->i+b->j) return 1;
2861  if (a->i<b->i) return -1;
2862   if (a->i>b->i) return 1;
2863  return 0;
2864}
2865
2866
2867static poly gcd_of_terms(poly p, ring r){
2868  int max_g_0=0;
2869  assume(p!=NULL);
2870  int i;
2871  poly m=pOne();
2872  poly t;
2873  for (i=pVariables; i; i--)
2874  {
2875      pSetExp(m,i, pGetExp(p,i));
2876      if (max_g_0==0)
2877  if (pGetExp(m,i)>0)
2878    max_g_0=i;
2879  }
2880
2881  t=p->next;
2882  while (t!=NULL){
2883
2884    if (max_g_0==0) break;
2885    for (i=max_g_0; i; i--)
2886    {
2887      pSetExp(m,i, si_min(pGetExp(t,i),pGetExp(m,i)));
2888      if (max_g_0==i)
2889  if (pGetExp(m,i)==0)
2890    max_g_0=0;
2891      if ((max_g_0==0) && (pGetExp(m,i)>0)){
2892  max_g_0=i;
2893      }
2894    }
2895    t=t->next;
2896  }
2897
2898  if (max_g_0>0)
2899    return m;
2900  pDelete(&m);
2901  return NULL;
2902}
2903static inline BOOLEAN pHasNotCFExtended(poly p1, poly p2, poly m)
2904{
2905
2906  if (pGetComp(p1) > 0 || pGetComp(p2) > 0)
2907    return FALSE;
2908  int i = 1;
2909  loop
2910  {
2911    if ((pGetExp(p1, i)-pGetExp(m,i) >0) && (pGetExp(p2, i) -pGetExp(m,i)> 0))   return FALSE;
2912    if (i == pVariables)                                return TRUE;
2913    i++;
2914  }
2915}
2916
2917
2918//for impl reasons may return false if the the normal product criterion matches
2919static inline BOOLEAN extended_product_criterion(poly p1, poly gcd1, poly p2, poly gcd2, slimgb_alg* c){
2920  if (c->nc)
2921    return FALSE;
2922  if(gcd1==NULL) return FALSE;
2923        if(gcd2==NULL) return FALSE;
2924        gcd1->next=gcd2; //may ordered incorrect
2925        poly m=gcd_of_terms(gcd1,c->r);
2926        gcd1->next=NULL;
2927        if (m==NULL) return FALSE;
2928
2929        BOOLEAN erg=pHasNotCFExtended(p1,p2,m);
2930        pDelete(&m);
2931        return erg;
2932}
2933static poly kBucketGcd(kBucket* b, ring r)
2934{
2935  int s=0;
2936  int i;
2937  poly m, n;
2938  BOOLEAN initialized=FALSE;
2939  for (i=MAX_BUCKET-1;i>=0;i--)
2940  {
2941    if (b->buckets[i]!=NULL){
2942      if (!initialized){
2943  m=gcd_of_terms(b->buckets[i],r);
2944  initialized=TRUE;
2945  if (m==NULL) return NULL;
2946      }
2947      else
2948  {
2949    n=gcd_of_terms(b->buckets[i],r);
2950    if (n==NULL) {
2951      pDelete(&m);
2952      return NULL;
2953    }
2954    n->next=m;
2955    poly t=gcd_of_terms(n,r);
2956    n->next=NULL;
2957    pDelete(&m);
2958    pDelete(&n);
2959    m=t;
2960    if (m==NULL) return NULL;
2961
2962  }
2963    }
2964  }
2965  return m;
2966}
2967
2968
2969
2970
2971static inline wlen_type quality_of_pos_in_strat_S(int pos, slimgb_alg* c){
2972  if (c->strat->lenSw!=NULL) return c->strat->lenSw[pos];
2973  return c->strat->lenS[pos];
2974}
2975static inline wlen_type quality_of_pos_in_strat_S_mult_high(int pos, poly high, slimgb_alg* c)
2976  //meant only for nc
2977{
2978  poly m=pOne();
2979  pExpVectorDiff(m,high ,c->strat->S[pos]);
2980  poly product = mm_Mult_pp(m, c->strat->S[pos], c->r);
2981  wlen_type erg=pQuality(product,c);
2982  pDelete(&m);
2983  pDelete(&product);
2984  return erg;
2985}
2986
2987static void multi_reduction_lls_trick(red_object* los, int losl,slimgb_alg* c,find_erg & erg){
2988  erg.expand=NULL;
2989  BOOLEAN swap_roles; //from reduce_by, to_reduce_u if fromS
2990  if(erg.fromS){
2991    if(pLmEqual(c->strat->S[erg.reduce_by],los[erg.to_reduce_u].p))
2992    {
2993      int i;
2994      wlen_type quality_a=quality_of_pos_in_strat_S(erg.reduce_by,c);
2995      int best=erg.to_reduce_u+1;
2996/*
2997      for (i=erg.to_reduce_u;i>=erg.to_reduce_l;i--){
2998  int qc=los[i].guess_quality(c);
2999  if (qc<quality_a){
3000    best=i;
3001    quality_a=qc;
3002  }
3003      }
3004      if(best!=erg.to_reduce_u+1){*/
3005      wlen_type qc;
3006      best=find_best(los,erg.to_reduce_l,erg.to_reduce_u,qc,c);
3007      if(qc<quality_a){
3008  los[best].flatten();
3009  int b_pos=kBucketCanonicalize(los[best].bucket);
3010  los[best].p=los[best].bucket->buckets[b_pos];
3011  qc=pQuality(los[best].bucket->buckets[b_pos],c);
3012  if(qc<quality_a){
3013    red_object h=los[erg.to_reduce_u];
3014    los[erg.to_reduce_u]=los[best];
3015    los[best]=h;
3016    swap_roles=TRUE;
3017  }
3018  else
3019    swap_roles=FALSE;
3020      }
3021      else{
3022
3023  swap_roles=FALSE;
3024      }
3025
3026    }
3027      else
3028    {
3029      if (erg.to_reduce_u>erg.to_reduce_l){
3030
3031  int i;
3032  wlen_type quality_a=quality_of_pos_in_strat_S(erg.reduce_by,c);
3033  if (c->nc)
3034    quality_a=quality_of_pos_in_strat_S_mult_high(erg.reduce_by, los[erg.to_reduce_u].p, c);
3035  int best=erg.to_reduce_u+1;
3036  wlen_type qc;
3037  best=find_best(los,erg.to_reduce_l,erg.to_reduce_u,qc,c);
3038  assume(qc==los[best].guess_quality(c));
3039  if(qc<quality_a){
3040    los[best].flatten();
3041    int b_pos=kBucketCanonicalize(los[best].bucket);
3042    los[best].p=los[best].bucket->buckets[b_pos];
3043    qc==pQuality(los[best].bucket->buckets[b_pos],c);
3044    //(best!=erg.to_reduce_u+1)
3045    if(qc<quality_a){
3046    red_object h=los[erg.to_reduce_u];
3047    los[erg.to_reduce_u]=los[best];
3048    los[best]=h;
3049    erg.reduce_by=erg.to_reduce_u;
3050    erg.fromS=FALSE;
3051    erg.to_reduce_u--;
3052    }
3053  }
3054      }
3055      else
3056      {
3057  assume(erg.to_reduce_u==erg.to_reduce_l);
3058  wlen_type quality_a=
3059        quality_of_pos_in_strat_S(erg.reduce_by,c);
3060  wlen_type qc=los[erg.to_reduce_u].guess_quality(c);
3061  if (qc<0) PrintS("Wrong wlen_type");
3062  if(qc<quality_a){
3063    int best=erg.to_reduce_u;
3064    los[best].flatten();
3065    int b_pos=kBucketCanonicalize(los[best].bucket);
3066    los[best].p=los[best].bucket->buckets[b_pos];
3067    qc=pQuality(los[best].bucket->buckets[b_pos],c);
3068    assume(qc>=0);
3069    if(qc<quality_a){
3070      BOOLEAN exp=FALSE;
3071      if(qc<=2){
3072         //Print("\n qc is %lld \n",qc);
3073         exp=TRUE;
3074      }
3075
3076      else {
3077         if (qc<quality_a/2)
3078          exp=TRUE;
3079         else
3080       if(erg.reduce_by<c->n/4)
3081          exp=TRUE;
3082      }
3083      if (exp){
3084        poly clear_into;
3085        los[erg.to_reduce_u].flatten();
3086        kBucketClear(los[erg.to_reduce_u].bucket,&clear_into,&erg.expand_length);
3087        erg.expand=pCopy(clear_into);
3088        kBucketInit(los[erg.to_reduce_u].bucket,clear_into,erg.expand_length);
3089        if (TEST_OPT_PROT)
3090    PrintS("e");
3091
3092      }
3093    }
3094  }
3095
3096
3097      }
3098
3099      swap_roles=FALSE;
3100      return;
3101      }
3102
3103  }
3104  else{
3105    if(erg.reduce_by>erg.to_reduce_u){
3106      //then lm(rb)>= lm(tru) so =
3107      assume(erg.reduce_by==erg.to_reduce_u+1);
3108      int best=erg.reduce_by;
3109      wlen_type quality_a=los[erg.reduce_by].guess_quality(c);
3110      wlen_type qc;
3111      best=find_best(los,erg.to_reduce_l,erg.to_reduce_u,qc,c);
3112
3113      int i;
3114      if(qc<quality_a){
3115    red_object h=los[erg.reduce_by];
3116    los[erg.reduce_by]=los[best];
3117    los[best]=h;
3118  }
3119  swap_roles=FALSE;
3120  return;
3121
3122
3123    }
3124    else
3125    {
3126      assume(!pLmEqual(los[erg.reduce_by].p,los[erg.to_reduce_l].p));
3127      assume(erg.to_reduce_u==erg.to_reduce_l);
3128      //further assume, that reduce_by is the above all other polys
3129      //with same leading term
3130      int il=erg.reduce_by;
3131      wlen_type quality_a =los[erg.reduce_by].guess_quality(c);
3132      wlen_type qc;
3133      while((il>0) && pLmEqual(los[il-1].p,los[il].p)){
3134  il--;
3135  qc=los[il].guess_quality(c);
3136  if (qc<quality_a){
3137    quality_a=qc;
3138    erg.reduce_by=il;
3139  }
3140      }
3141      swap_roles=FALSE;
3142    }
3143
3144  }
3145  if(swap_roles)
3146  {
3147    if (TEST_OPT_PROT)
3148      PrintS("b");
3149    poly clear_into;
3150    int dummy_len;
3151    int new_length;
3152    int bp=erg.to_reduce_u;//bucket_positon
3153    //kBucketClear(los[bp].bucket,&clear_into,&new_length);
3154    new_length=los[bp].clear_to_poly();
3155    clear_into=los[bp].p;
3156    poly p=c->strat->S[erg.reduce_by];
3157    int j=erg.reduce_by;
3158    int old_length=c->strat->lenS[j];// in view of S
3159    los[bp].p=p;
3160    if (c->eliminationProblem){
3161        los[bp].sugar=pTotaldegree_full(p);
3162    }
3163    kBucketInit(los[bp].bucket,p,old_length);
3164    wlen_type qal=pQuality(clear_into,c,new_length);
3165    int pos_in_c=-1;
3166    int z;
3167    int new_pos;
3168    new_pos=simple_posInS(c->strat,clear_into,new_length, qal);
3169    assume(new_pos<=j);
3170    for (z=c->n;z;z--)
3171    {
3172      if(p==c->S->m[z-1])
3173      {
3174  pos_in_c=z-1;
3175  break;
3176      }
3177    }
3178
3179    int tdeg_full=-1;
3180    int tdeg=-1;
3181    if(pos_in_c>=0)
3182    {
3183      #ifdef TGB_RESORT_PAIRS
3184      c->used_b=TRUE;
3185      c->replaced[pos_in_c]=TRUE;
3186      #endif
3187      tdeg=c->T_deg[pos_in_c];
3188      c->S->m[pos_in_c]=clear_into;
3189      c->lengths[pos_in_c]=new_length;
3190      c->weighted_lengths[pos_in_c]=qal;
3191      if (c->gcd_of_terms[pos_in_c]==NULL)
3192        c->gcd_of_terms[pos_in_c]=gcd_of_terms(clear_into,c->r);
3193      if (c->T_deg_full)
3194        tdeg_full=c->T_deg_full[pos_in_c]=pTotaldegree_full(clear_into);
3195      else tdeg_full=tdeg;
3196      c_S_element_changed_hook(pos_in_c,c);
3197    } else {
3198      if (c->eliminationProblem){
3199        tdeg_full=pTotaldegree_full(clear_into);
3200        tdeg=pTotaldegree(clear_into);
3201      }
3202    }
3203    c->strat->S[j]=clear_into;
3204    c->strat->lenS[j]=new_length;
3205
3206    assume(pLength(clear_into)==new_length);
3207    if(c->strat->lenSw!=NULL)
3208      c->strat->lenSw[j]=qal;
3209    if (!rField_is_Zp(c->r))
3210    {
3211      pContent(clear_into);
3212      pCleardenom(clear_into);//should be unnecessary
3213    }
3214    else
3215      pNorm(clear_into);
3216#ifdef FIND_DETERMINISTIC
3217    erg.reduce_by=j;
3218    //resort later see diploma thesis, find_in_S must be deterministic
3219    //during multireduction if spolys are only in the span of the
3220    //input polys
3221#else
3222
3223    if (new_pos<j)
3224    {
3225      if (c->strat->honey) c->strat->ecartS[j]=tdeg_full-tdeg;
3226      move_forward_in_S(j,new_pos,c->strat);
3227      erg.reduce_by=new_pos;
3228    }
3229#endif
3230  }
3231}
3232static int fwbw(red_object* los, int i){
3233   int i2=i;
3234   int step=1;
3235
3236   BOOLEAN bw=FALSE;
3237   BOOLEAN incr=TRUE;
3238
3239   while(1)
3240   {
3241     if(!bw)
3242     {
3243       step=si_min(i2,step);
3244       if (step==0) break;
3245       i2-=step;
3246
3247       if(!pLmEqual(los[i].p,los[i2].p))
3248       {
3249   bw=TRUE;
3250   incr=FALSE;
3251       }
3252       else
3253       {
3254   if ((!incr) &&(step==1)) break;
3255       }
3256
3257
3258     }
3259     else
3260     {
3261
3262       step=si_min(i-i2,step);
3263       if (step==0) break;
3264       i2+=step;
3265       if(pLmEqual(los[i].p,los[i2].p)){
3266   if(step==1) break;
3267   else
3268   {
3269     bw=FALSE;
3270   }
3271       }
3272
3273     }
3274     if (incr)
3275       step*=2;
3276     else
3277     {
3278       if (step%2==1)
3279   step=(step+1)/2;
3280       else
3281   step/=2;
3282
3283     }
3284   }
3285   return i2;
3286}
3287static void canonicalize_region(red_object* los, int l, int u,slimgb_alg* c){
3288    assume(l<=u+1);
3289    int i;
3290    for(i=l;i<=u;i++){
3291        kBucketCanonicalize(los[i].bucket);
3292    }
3293
3294}
3295static void multi_reduction_find(red_object* los, int losl,slimgb_alg* c,int startf,find_erg & erg){
3296  kStrategy strat=c->strat;
3297
3298  assume(startf<=losl);
3299  assume((startf==losl-1)||(pLmCmp(los[startf].p,los[startf+1].p)==-1));
3300  int i=startf;
3301
3302  int j;
3303  while(i>=0){
3304    assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)<=0));
3305    assume(is_valid_ro(los[i]));
3306    assume((!(c->eliminationProblem))||(los[i].sugar>=pTotaldegree(los[i].p)));
3307    j=kFindDivisibleByInS_easy(strat,los[i]);
3308    if(j>=0){
3309
3310      erg.to_reduce_u=i;
3311      erg.reduce_by=j;
3312      erg.fromS=TRUE;
3313      int i2=fwbw(los,i);
3314      assume(pLmEqual(los[i].p,los[i2].p));
3315      assume((i2==0)||(!pLmEqual(los[i2].p,los[i2-1].p)));
3316      assume(i>=i2);
3317
3318
3319      erg.to_reduce_l=i2;
3320      assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)==-1));
3321      canonicalize_region(los,erg.to_reduce_u+1,startf,c);
3322      return;
3323    }
3324    if (j<0){
3325
3326      //not reduceable, try to use this for reducing higher terms
3327      int i2=fwbw(los,i);
3328      assume(pLmEqual(los[i].p,los[i2].p));
3329      assume((i2==0)||(!pLmEqual(los[i2].p,los[i2-1].p)));
3330      assume(i>=i2);
3331      if(i2!=i){
3332
3333
3334  erg.to_reduce_u=i-1;
3335  erg.to_reduce_l=i2;
3336  erg.reduce_by=i;
3337  erg.fromS=FALSE;
3338  assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)==-1));
3339  canonicalize_region(los,erg.to_reduce_u+1,startf,c);
3340  return;
3341      }
3342
3343      i--;
3344    }
3345  }
3346  erg.reduce_by=-1;//error code
3347  return;
3348}
3349
3350 //  nicht reduzierbare eintraege in ergebnisliste schreiben
3351//   nullen loeschen
3352//   while(finde_groessten leitterm reduzierbar(c,erg)){
3353
3354static int multi_reduction_clear_zeroes(red_object* los, int  losl, int l, int u)
3355{
3356
3357
3358  int deleted=0;
3359  int  i=l;
3360  int last=-1;
3361  while(i<=u)
3362  {
3363
3364    if(los[i].p==NULL){
3365      kBucketDestroy(&los[i].bucket);
3366//      delete los[i];//here we assume los are constructed with new
3367      //destroy resources, must be added here
3368     if (last>=0)
3369     {
3370       memmove(los+(int)(last+1-deleted),los+(last+1),sizeof(red_object)*(i-1-last));
3371     }
3372     last=i;
3373     deleted++;
3374    }
3375    i++;
3376  }
3377  if((last>=0)&&(last!=losl-1))
3378      memmove(los+(int)(last+1-deleted),los+last+1,sizeof(red_object)*(losl-1-last));
3379  return deleted;
3380
3381}
3382
3383static void sort_region_down(red_object* los, int l, int u, slimgb_alg* c)
3384{
3385  qsort(los+l,u-l+1,sizeof(red_object),red_object_better_gen);
3386  int i;
3387
3388  for(i=l;i<=u;i++)
3389  {
3390    BOOLEAN moved=FALSE;
3391    int j;
3392    for(j=i;j;j--)
3393    {
3394      if(pLmCmp(los[j].p,los[j-1].p)==-1){
3395  red_object h=los[j];
3396  los[j]=los[j-1];
3397  los[j-1]=h;
3398  moved=TRUE;
3399      }
3400      else break;
3401    }
3402    if(!moved) return;
3403  }
3404}
3405
3406//assume that los is ordered ascending by leading term, all non zero
3407static void multi_reduction(red_object* los, int & losl, slimgb_alg* c)
3408{
3409  poly* delay=(poly*) omalloc(losl*sizeof(poly));
3410  int delay_s=0;
3411  //initialize;
3412  assume(c->strat->sl>=0);
3413  assume(losl>0);
3414  int i;
3415  wlen_type max_initial_quality=0;
3416
3417  for(i=0;i<losl;i++){
3418    los[i].sev=pGetShortExpVector(los[i].p);
3419//SetShortExpVector();
3420    los[i].p=kBucketGetLm(los[i].bucket);
3421    if (los[i].initial_quality>max_initial_quality)
3422        max_initial_quality=los[i].initial_quality;
3423    // else
3424//         Print("init2_qal=%lld;", los[i].initial_quality);
3425//     Print("initial_quality=%lld;",max_initial_quality);
3426  }
3427
3428  kStrategy strat=c->strat;
3429  int curr_pos=losl-1;
3430
3431
3432//  nicht reduzierbare eintrï¿œe in ergebnisliste schreiben
3433  // nullen loeschen
3434  while(curr_pos>=0){
3435
3436    find_erg erg;
3437    multi_reduction_find(los, losl,c,curr_pos,erg);//last argument should be curr_pos
3438    if(erg.reduce_by<0) break;
3439
3440
3441
3442    erg.expand=NULL;
3443    int d=erg.to_reduce_u-erg.to_reduce_l+1;
3444
3445
3446    multi_reduction_lls_trick(los,losl,c,erg);
3447
3448
3449    int i;
3450    int len;
3451    //    wrp(los[erg.to_reduce_u].p);
3452    //Print("\n");
3453    multi_reduce_step(erg,los,c);
3454
3455
3456    if(!K_TEST_OPT_REDTHROUGH){
3457  for(i=erg.to_reduce_l;i<=erg.to_reduce_u;i++){
3458     if  (los[i].p!=NULL)  //the check (los[i].p!=NULL) might be invalid
3459     {
3460         //
3461         assume(los[i].initial_quality>0);
3462
3463               if(los[i].guess_quality(c)
3464                  >1.5*delay_factor*max_initial_quality){
3465                       if (TEST_OPT_PROT)
3466                           PrintS("v");
3467                       los[i].canonicalize();
3468                       if(los[i].guess_quality(c)
3469                           >delay_factor*max_initial_quality){
3470                               if (TEST_OPT_PROT)
3471                                   PrintS(".");
3472                               los[i].clear_to_poly();
3473                               //delay.push_back(los[i].p);
3474                               delay[delay_s]=los[i].p;
3475                               delay_s++;
3476
3477                               los[i].p=NULL;
3478
3479                      }
3480                  }
3481
3482            }
3483     }
3484  }
3485    int deleted=multi_reduction_clear_zeroes(los, losl, erg.to_reduce_l, erg.to_reduce_u);
3486    if(erg.fromS==FALSE)
3487      curr_pos=si_max(erg.to_reduce_u,erg.reduce_by);
3488    else
3489      curr_pos=erg.to_reduce_u;
3490    losl -= deleted;
3491    curr_pos -= deleted;
3492
3493    //Print("deleted %i \n",deleted);
3494    if ((TEST_V_UPTORADICAL) &&(!(erg.fromS)))
3495        sort_region_down(los,si_min(erg.to_reduce_l,erg.reduce_by),(si_max(erg.to_reduce_u,erg.reduce_by))-deleted,c);
3496    else
3497    sort_region_down(los, erg.to_reduce_l, erg.to_reduce_u-deleted, c);
3498
3499
3500    if(erg.expand)
3501    {
3502#ifdef FIND_DETERMINISTIC
3503      int i;
3504      for(i=0;c->expandS[i];i++);
3505      c->expandS=(poly*) omrealloc(c->expandS,(i+2)*sizeof(poly));
3506      c->expandS[i]=erg.expand;
3507      c->expandS[i+1]=NULL;
3508#else
3509      int ecart=0;
3510      if (c->eliminationProblem){
3511        ecart=pTotaldegree_full(erg.expand)-pTotaldegree(erg.expand);
3512      }
3513      add_to_reductors(c,erg.expand,erg.expand_length,ecart);
3514#endif
3515    }
3516
3517  }
3518
3519
3520  //sorted_pair_node** pairs=(sorted_pair_node**)
3521  //  omalloc(delay_s*sizeof(sorted_pair_node*));
3522  c->introduceDelayedPairs(delay,delay_s);
3523  /*
3524  for(i=0;i<delay_s;i++){
3525
3526      poly p=delay[i];
3527      //if (rPar(c->r)==0)
3528      simplify_poly(p,c->r);
3529      sorted_pair_node* si=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
3530      si->i=-1;
3531      si->j=-1;
3532       if (!rField_is_Zp(c->r)){
3533        if (!c->nc)
3534            p=redTailShort(p, c->strat);
3535        pCleardenom(p);
3536        pContent(p);
3537      }
3538      si->expected_length=pQuality(p,c,pLength(p));
3539      si->deg=pTotaldegree(p);
3540
3541      si->lcm_of_lm=p;
3542      pairs[i]=si;
3543  }
3544  qsort(pairs,delay_s,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
3545  c->apairs=spn_merge(c->apairs,c->pair_top+1,pairs,delay_s,c);
3546  c->pair_top+=delay_s;*/
3547  omfree(delay);
3548  //omfree(pairs);
3549  return;
3550}
3551void red_object::flatten(){
3552  assume(p==kBucketGetLm(bucket));
3553}
3554void red_object::validate(){
3555  p=kBucketGetLm(bucket);
3556  if(p)
3557    sev=pGetShortExpVector(p);
3558}
3559int red_object::clear_to_poly(){
3560  flatten();
3561  int l;
3562  kBucketClear(bucket,&p,&l);
3563  return l;
3564}
3565
3566
3567
3568
3569
3570void reduction_step::reduce(red_object* r, int l, int u){}
3571void simple_reducer::do_reduce(red_object & ro){
3572  number coef;
3573  if (!c->nc)
3574    coef=kBucketPolyRed(ro.bucket,p,
3575       p_len,
3576       c->strat->kNoether);
3577  else
3578    nc_BucketPolyRed_Z(ro.bucket, p, &coef);
3579  nDelete(&coef);
3580}
3581
3582
3583void simple_reducer::reduce(red_object* r, int l, int u){
3584  this->pre_reduce(r,l,u);
3585  int i;
3586//debug start
3587  int im;
3588
3589
3590  if(c->eliminationProblem){
3591    assume(p_LmEqual(r[l].p,r[u].p,c->r));
3592    /*int lm_deg=pTotaldegree(r[l].p);
3593    reducer_deg=lm_deg+pTotaldegree_full(p)-pTotaldegree(p);*/
3594  }
3595
3596  for(i=l;i<=u;i++){
3597
3598
3599
3600    this->do_reduce(r[i]);
3601    if (c->eliminationProblem){
3602        r[i].sugar=si_max(r[i].sugar,reducer_deg);
3603    }
3604  }
3605  for(i=l;i<=u;i++){
3606
3607    kBucketSimpleContent(r[i].bucket);
3608    r[i].validate();
3609    #ifdef TGB_DEBUG
3610    #endif
3611  }
3612}
3613reduction_step::~reduction_step(){}
3614simple_reducer::~simple_reducer(){
3615  if(fill_back!=NULL)
3616  {
3617    kBucketInit(fill_back,p,p_len);
3618  }
3619  fill_back=NULL;
3620
3621}
3622
3623void multi_reduce_step(find_erg & erg, red_object* r, slimgb_alg* c){
3624  static int id=0;
3625  id++;
3626  unsigned long sev;
3627    BOOLEAN lt_changed=FALSE;
3628  int rn=erg.reduce_by;
3629  poly red;
3630  int red_len;
3631  simple_reducer* pointer;
3632  BOOLEAN work_on_copy=FALSE;
3633  if(erg.fromS){
3634    red=c->strat->S[rn];
3635    red_len=c->strat->lenS[rn];
3636    assume(red_len==pLength(red));
3637  }
3638  else
3639  {
3640    r[rn].flatten();
3641    kBucketClear(r[rn].bucket,&red,&red_len);
3642
3643    if (!rField_is_Zp(c->r))
3644    {
3645      pContent(red);
3646      pCleardenom(red);//should be unnecessary
3647
3648    }
3649    pNormalize(red);
3650    if (c->eliminationProblem){
3651        r[rn].sugar=pTotaldegree_full(red);
3652    }
3653
3654    if ((!(erg.fromS))&&(TEST_V_UPTORADICAL)){
3655
3656         if (polynomial_root(red,c->r))
3657            lt_changed=TRUE;
3658            sev=p_GetShortExpVector(red,c->r);}
3659    red_len=pLength(red);
3660  }
3661  if (((TEST_V_MODPSOLVSB)&&(red_len>1))||((c->nc)||(erg.to_reduce_u-erg.to_reduce_l>5))){
3662    work_on_copy=TRUE;
3663    // poly m=pOne();
3664    poly m=c->tmp_lm;
3665    pSetCoeff(m,nInit(1));
3666    for(int i=1;i<=pVariables;i++)
3667      pSetExp(m,i,(pGetExp(r[erg.to_reduce_l].p, i)-pGetExp(red,i)));
3668    pSetm(m);
3669    poly red_cp;
3670    if (!c->nc)
3671      red_cp=ppMult_mm(red,m);
3672    else
3673      red_cp = mm_Mult_pp(m, red, c->r);
3674    if(!erg.fromS){
3675      kBucketInit(r[rn].bucket,red,red_len);
3676    }
3677    //now reduce the copy
3678    //static poly redNF2 (poly h,slimgb_alg* c , int &len, number&  m,int n)
3679
3680    if (!c->nc)
3681      redTailShort(red_cp,c->strat);
3682    //number mul;
3683    // red_len--;
3684//     red_cp->next=redNF2(red_cp->next,c,red_len,mul,c->average_length);
3685//     pSetCoeff(red_cp,nMult(red_cp->coef,mul));
3686//     nDelete(&mul);
3687//     red_len++;
3688    red=red_cp;
3689    red_len=pLength(red);
3690    // pDelete(&m);
3691
3692  }
3693  int i;
3694
3695
3696
3697  assume(red_len==pLength(red));
3698
3699  int reducer_deg=0;
3700  if (c->eliminationProblem){
3701     int lm_deg=pTotaldegree(r[erg.to_reduce_l].p);
3702     int ecart;
3703     if (erg.fromS){
3704       assume(pTotaldegree_full(red)<=c->T_deg_full[erg.reduce_by]);
3705       ecart=c->strat->ecartS[erg.reduce_by];
3706     } else {
3707       ecart=pTotaldegree_full(red)-lm_deg;
3708     }
3709     reducer_deg=lm_deg+ecart;
3710  }
3711  pointer=new simple_reducer(red,red_len,reducer_deg,c);
3712
3713  if ((!work_on_copy) && (!erg.fromS))
3714    pointer->fill_back=r[rn].bucket;
3715  else
3716    pointer->fill_back=NULL;
3717  pointer->reduction_id=id;
3718  pointer->c=c;
3719
3720  pointer->reduce(r,erg.to_reduce_l, erg.to_reduce_u);
3721  if(work_on_copy) pDelete(&pointer->p);
3722  delete pointer;
3723  if (lt_changed){
3724    assume(!erg.fromS);
3725    r[erg.reduce_by].sev=sev;
3726  }
3727
3728};
3729
3730
3731
3732
3733void simple_reducer:: pre_reduce(red_object* r, int l, int u){}
3734
Note: See TracBrowser for help on using the repository browser.