source: git/kernel/tgb.cc @ 936551

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