source: git/kernel/tgb.cc @ 3a67ea7

spielwiese
Last change on this file since 3a67ea7 was 01eca5, checked in by Michael Brickenstein <bricken@…>, 17 years ago
*bricken: some cleanup git-svn-id: file:///usr/local/Singular/svn/trunk@10000 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.152 2007-04-26 05:40:59 bricken Exp $ */
8/*
9* ABSTRACT: slimgb and F4 implementation
10*/
11//#include <vector>
12//using namespace std;
13
14
15///@TODO: delay nur auf Sugarvergrï¿œerung
16///@TODO: grade aus ecartS, setze dazu strat->honey; und nutze p.ecart
17//@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, char* prot, slimgb_alg* c){
1138    int i=0;
1139    //check, if it is already in the queue
1140
1141
1142    while(c->add_later->m[i]!=NULL){
1143        if (p_LmEqual(c->add_later->m[i],p,c->r))
1144            return;
1145        i++;
1146    }
1147    if (TEST_OPT_PROT)
1148        PrintS(prot);
1149    c->add_later->m[i]=p;
1150}
1151static int simple_posInS (kStrategy strat, poly p,int len, wlen_type wlen)
1152{
1153
1154
1155  if(strat->sl==-1) return 0;
1156  if (strat->lenSw) return pos_helper(strat,p,(wlen_type) wlen,(wlen_set) strat->lenSw,strat->S);
1157  return pos_helper(strat,p,len,strat->lenS,strat->S);
1158
1159}
1160
1161/*2
1162 *if the leading term of p
1163 *divides the leading term of some S[i] it will be canceled
1164 */
1165static inline void clearS (poly p, unsigned long p_sev,int l, int* at, int* k,
1166                           kStrategy strat)
1167{
1168  assume(p_sev == pGetShortExpVector(p));
1169  if (!pLmShortDivisibleBy(p,p_sev, strat->S[*at], ~ strat->sevS[*at])) return;
1170  if (l>=strat->lenS[*at]) return;
1171  if (TEST_OPT_PROT)
1172    PrintS("!");
1173  mflush();
1174  //pDelete(&strat->S[*at]);
1175  deleteInS((*at),strat);
1176  (*at)--;
1177  (*k)--;
1178//  assume(lenS_correct(strat));
1179}
1180
1181
1182
1183static int iq_crit(const void* ap,const void* bp){
1184
1185  sorted_pair_node* a=*((sorted_pair_node**)ap);
1186  sorted_pair_node* b=*((sorted_pair_node**)bp);
1187  assume(a->i>a->j);
1188  assume(b->i>b->j);
1189
1190
1191  if (a->deg<b->deg) return -1;
1192  if (a->deg>b->deg) return 1;
1193  int comp=pLmCmp(a->lcm_of_lm, b->lcm_of_lm);
1194  if(comp!=0)
1195    return comp;
1196  if (a->expected_length<b->expected_length) return -1;
1197  if (a->expected_length>b->expected_length) return 1;
1198  if (a->j>b->j) return 1;
1199  if (a->j<b->j) return -1;
1200  return 0;
1201}
1202static wlen_type coeff_mult_size_estimate(int s1, int s2, ring r){
1203    if (rField_is_Q(r)) return s1+s2;
1204    else return s1*s2;
1205}
1206static wlen_type pair_weighted_length(int i, int j, slimgb_alg* c){
1207    if ((c->isDifficultField) && (c->eliminationProblem))  {
1208        int c1=slim_nsize(p_GetCoeff(c->S->m[i],c->r),c->r);
1209        int c2=slim_nsize(p_GetCoeff(c->S->m[j],c->r),c->r);
1210        wlen_type el1=c->weighted_lengths[i]/c1;
1211        assume(el1!=0);
1212        assume(c->weighted_lengths[i] %c1==0);
1213        wlen_type el2=c->weighted_lengths[j]/c2;
1214        assume(el2!=0);
1215        assume(c->weighted_lengths[j] %c2==0);
1216        //should be * for function fields
1217        //return (c1+c2) * (el1+el2-2);
1218        wlen_type res=coeff_mult_size_estimate(c1,c2,c->r);
1219        res*=el1+el2-2;
1220        return res;
1221
1222    }
1223    if (c->isDifficultField) {
1224        //int cs=slim_nsize(p_GetCoeff(c->S->m[i],c->r),c->r)+
1225        //    slim_nsize(p_GetCoeff(c->S->m[j],c->r),c->r);
1226        if(!(TEST_V_COEFSTRAT)){
1227        wlen_type cs=
1228            coeff_mult_size_estimate(
1229                slim_nsize(p_GetCoeff(c->S->m[i],c->r),c->r),
1230                slim_nsize(p_GetCoeff(c->S->m[j],c->r),c->r),c->r);
1231        return (wlen_type)(c->lengths[i]+c->lengths[j]-2)*
1232            (wlen_type)cs;}
1233            else {
1234
1235            wlen_type cs=
1236            coeff_mult_size_estimate(
1237                slim_nsize(p_GetCoeff(c->S->m[i],c->r),c->r),
1238                slim_nsize(p_GetCoeff(c->S->m[j],c->r),c->r),c->r);
1239            cs*=cs;
1240        return (wlen_type)(c->lengths[i]+c->lengths[j]-2)*
1241            (wlen_type)cs;
1242            }
1243    }
1244    if (c->eliminationProblem) {
1245
1246        return (c->weighted_lengths[i]+c->weighted_lengths[j]-2);
1247    }
1248    return c->lengths[i]+c->lengths[j]-2;
1249
1250}
1251sorted_pair_node** add_to_basis_ideal_quotient(poly h, slimgb_alg* c, int* ip)
1252{
1253
1254  assume(h!=NULL);
1255  poly got=gcd_of_terms(h,c->r);
1256  if((got!=NULL) &&(TEST_V_UPTORADICAL)) {
1257    poly copy=p_Copy(got,c->r);
1258    //p_wrp(got,c->r);
1259    BOOLEAN changed=monomial_root(got,c->r);
1260    if (changed)
1261    {
1262         poly div_by=pDivide(copy, got);
1263         poly iter=h;
1264         while(iter){
1265            pExpVectorSub(iter,div_by);
1266            pIter(iter);
1267         }
1268         p_Delete(&div_by, c->r);
1269         PrintS("U");
1270    }
1271    p_Delete(&copy,c->r);
1272  }
1273
1274#define ENLARGE(pointer, type) pointer=(type*) omrealloc(pointer, c->array_lengths*sizeof(type))
1275//  BOOLEAN corr=lenS_correct(c->strat);
1276  BOOLEAN R_found=FALSE;
1277  void* hp;
1278  int sugar;
1279  int ecart=0;
1280  ++(c->n);
1281  ++(c->S->ncols);
1282  int i,j;
1283  i=c->n-1;
1284  sorted_pair_node** nodes=(sorted_pair_node**) omalloc(sizeof(sorted_pair_node*)*i);
1285  int spc=0;
1286  if(c->n>c->array_lengths){
1287    c->array_lengths=c->array_lengths*2;
1288    assume(c->array_lengths>=c->n);
1289    ENLARGE(c->T_deg, int);
1290    ENLARGE(c->tmp_pair_lm,poly);
1291    ENLARGE(c->tmp_spn,sorted_pair_node*);
1292
1293    ENLARGE(c->short_Exps,long);
1294    ENLARGE(c->lengths,int);
1295    #ifndef HAVE_BOOST
1296    #ifndef USE_STDVECBOOL
1297
1298    ENLARGE(c->states, char*);
1299    #endif
1300    #endif
1301    ENLARGE(c->gcd_of_terms,poly);
1302    //if (c->weighted_lengths!=NULL) {
1303    ENLARGE(c->weighted_lengths,wlen_type);
1304    //}
1305    //ENLARGE(c->S->m,poly);
1306
1307  }
1308  pEnlargeSet(&c->S->m,c->n-1,1);
1309  if (c->T_deg_full)
1310    ENLARGE(c->T_deg_full,int);
1311  sugar=c->T_deg[i]=pTotaldegree(h);
1312  if(c->T_deg_full){
1313    sugar=c->T_deg_full[i]=pTotaldegree_full(h);
1314    ecart=sugar-c->T_deg[i];
1315    assume(ecart>=0);
1316  }
1317
1318
1319  c->tmp_pair_lm[i]=pOne_Special(c->r);
1320
1321
1322  c->tmp_spn[i]=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1323
1324
1325  c->lengths[i]=pLength(h);
1326
1327  //necessary for correct weighted length
1328
1329   if (!rField_is_Zp(c->r)){
1330    pCleardenom(h);
1331    pContent(h); //is a duplicate call, but belongs here
1332
1333  }
1334  else
1335    pNorm(h);
1336  pNormalize(h);
1337
1338  c->weighted_lengths[i]=pQuality(h, c, c->lengths[i]);
1339  c->gcd_of_terms[i]=got;
1340  #ifdef HAVE_BOOST
1341    c->states.push_back(dynamic_bitset<>(i));
1342
1343  #else
1344  #ifdef USE_STDVECBOOL
1345
1346    c->states.push_back(vector<bool>(i));
1347
1348
1349  #else
1350  if (i>0)
1351    c->states[i]=(char*)  omalloc(i*sizeof(char));
1352  else
1353    c->states[i]=NULL;
1354  #endif
1355  #endif
1356
1357  c->S->m[i]=h;
1358  c->short_Exps[i]=p_GetShortExpVector(h,c->r);
1359
1360#undef ENLARGE
1361  if (p_GetComp(h,currRing)<=c->syz_comp){
1362  for (j=0;j<i;j++){
1363
1364
1365    #ifndef HAVE_BOOST
1366    c->states[i][j]=UNCALCULATED;
1367    #endif
1368    assume(p_LmDivisibleBy(c->S->m[i],c->S->m[j],c->r)==
1369     p_LmShortDivisibleBy(c->S->m[i],c->short_Exps[i],c->S->m[j],~(c->short_Exps[j]),c->r));
1370
1371
1372    if (_p_GetComp(c->S->m[i],c->r)!=_p_GetComp(c->S->m[j],c->r)){
1373      //c->states[i][j]=UNCALCULATED;
1374      //WARNUNG: be careful
1375      continue;
1376    } else
1377    if ((!c->nc) && (c->lengths[i]==1) && (c->lengths[j]==1)){
1378      c->states[i][j]=HASTREP;
1379
1380      }
1381    else if (( (!c->nc) || (c->is_homog && rIsSCA(c->r) ) ) &&  (pHasNotCF(c->S->m[i],c->S->m[j])))
1382//     else if ((!(c->nc)) &&  (pHasNotCF(c->S->m[i],c->S->m[j])))
1383    {
1384      c->easy_product_crit++;
1385      c->states[i][j]=HASTREP;
1386      continue;
1387    }
1388    else if(extended_product_criterion(c->S->m[i],c->gcd_of_terms[i],c->S->m[j],c->gcd_of_terms[j],c))
1389    {
1390      c->states[i][j]=HASTREP;
1391      c->extended_product_crit++;
1392
1393      //PrintS("E");
1394    }
1395      //  if (c->states[i][j]==UNCALCULATED){
1396
1397    if ((TEST_V_FINDMONOM) &&(!c->nc)) {
1398        //PrintS("COMMU");
1399       //  if (c->lengths[i]==c->lengths[j]){
1400//             poly short_s=ksCreateShortSpoly(c->S->m[i],c->S->m[j],c->r);
1401//             if (short_s==NULL){
1402//                 c->states[i][j]=HASTREP;
1403//             } else
1404//             {
1405//                 p_Delete(&short_s, currRing);
1406//             }
1407//         }
1408        if (c->lengths[i]+c->lengths[j]==3){
1409
1410
1411             poly short_s=ksCreateShortSpoly(c->S->m[i],c->S->m[j],c->r);
1412            if (short_s==NULL){
1413                c->states[i][j]=HASTREP;
1414            } else
1415            {
1416                assume(pLength(short_s)==1);
1417                if (TEST_V_UPTORADICAL)
1418                   monomial_root(short_s,c->r);
1419                int iS=
1420                   kFindDivisibleByInS_easy(c->strat,short_s, p_GetShortExpVector(short_s,c->r));
1421                if (iS<0){
1422                    //PrintS("N");
1423                    if (TRUE) {
1424                    c->states[i][j]=HASTREP;
1425                    add_later(short_s,"N",c);
1426                    } else p_Delete(&short_s,currRing);
1427                }
1428                else {
1429                    if (c->strat->lenS[iS]>1){
1430                        //PrintS("O");
1431                        if (TRUE) {
1432                        c->states[i][j]=HASTREP;
1433                        add_later(short_s,"O",c);
1434                        } else p_Delete(&short_s,currRing);
1435                    }
1436                    else
1437                     p_Delete(&short_s, currRing);
1438                     c->states[i][j]=HASTREP;
1439                }
1440
1441
1442            }
1443        }
1444    }
1445      //    if (short_s)
1446      //    {
1447    assume(spc<=j);
1448    sorted_pair_node* s=c->tmp_spn[spc];//(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1449    s->i=si_max(i,j);
1450    s->j=si_min(i,j);
1451    assume(s->j==j);
1452    s->expected_length=pair_weighted_length(i,j,c);//c->lengths[i]+c->lengths[j]-2;
1453
1454    poly lm=c->tmp_pair_lm[spc];//=pOne_Special();
1455
1456    pLcm(c->S->m[i], c->S->m[j], lm);
1457    pSetm(lm);
1458    s->deg=pTotaldegree(lm);
1459
1460    if(c->T_deg_full)//Sugar
1461    {
1462      int t_i=c->T_deg_full[s->i]-c->T_deg[s->i];
1463      int t_j=c->T_deg_full[s->j]-c->T_deg[s->j];
1464      s->deg+=si_max(t_i,t_j);
1465      //Print("\n max: %d\n",max(t_i,t_j));
1466    }
1467    s->lcm_of_lm=lm;
1468    //          pDelete(&short_s);
1469    //assume(lm!=NULL);
1470    nodes[spc]=s;
1471    spc++;
1472
1473  // }
1474  //else
1475  //{
1476        //c->states[i][j]=HASTREP;
1477  //}
1478  }
1479  }//if syz_comp end
1480
1481
1482
1483
1484  assume(spc<=i);
1485  //now ideal quotient crit
1486  qsort(nodes,spc,sizeof(sorted_pair_node*),iq_crit);
1487
1488    sorted_pair_node** nodes_final=(sorted_pair_node**) omalloc(sizeof(sorted_pair_node*)*i);
1489  int spc_final=0;
1490  j=0;
1491  while(j<spc)
1492  {
1493    int lower=j;
1494    int upper;
1495    BOOLEAN has=FALSE;
1496    for(upper=lower+1;upper<spc;upper++)
1497    {
1498
1499      if(!pLmEqual(nodes[lower]->lcm_of_lm,nodes[upper]->lcm_of_lm))
1500      {
1501  break;
1502      }
1503      if (has_t_rep(nodes[upper]->i,nodes[upper]->j,c))
1504  has=TRUE;
1505
1506    }
1507    upper=upper-1;
1508    int z;
1509    assume(spc_final<=j);
1510    for(z=0;z<spc_final;z++)
1511    {
1512      if(p_LmDivisibleBy(nodes_final[z]->lcm_of_lm,nodes[lower]->lcm_of_lm,c->r))
1513      {
1514  has=TRUE;
1515  break;
1516      }
1517    }
1518
1519    if(has)
1520    {
1521      for(;lower<=upper;lower++)
1522      {
1523  //free_sorted_pair_node(nodes[lower],c->r);
1524  //omfree(nodes[lower]);
1525  nodes[lower]=NULL;
1526      }
1527      j=upper+1;
1528      continue;
1529    }
1530    else
1531    {
1532      nodes[lower]->lcm_of_lm=pCopy(nodes[lower]->lcm_of_lm);
1533      assume(_p_GetComp(c->S->m[nodes[lower]->i],c->r)==_p_GetComp(c->S->m[nodes[lower]->j],c->r));
1534      nodes_final[spc_final]=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1535
1536      *(nodes_final[spc_final++])=*(nodes[lower]);
1537      //c->tmp_spn[nodes[lower]->j]=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1538      nodes[lower]=NULL;
1539      for(lower=lower+1;lower<=upper;lower++)
1540      {
1541  //  free_sorted_pair_node(nodes[lower],c->r);
1542  //omfree(nodes[lower]);
1543  nodes[lower]=NULL;
1544      }
1545      j=upper+1;
1546      continue;
1547    }
1548  }
1549
1550  //  Print("i:%d,spc_final:%d",i,spc_final);
1551
1552
1553
1554
1555  assume(spc_final<=spc);
1556  omfree(nodes);
1557  nodes=NULL;
1558
1559  add_to_reductors(c, h, c->lengths[c->n-1], ecart,TRUE);
1560  //i=posInS(c->strat,c->strat->sl,h,0 ecart);
1561  if (!(c->nc)){
1562    if (c->lengths[c->n-1]==1)
1563      shorten_tails(c,c->S->m[c->n-1]);
1564  }
1565  //you should really update c->lengths, c->strat->lenS, and the oder of polys in strat if you sort after lengths
1566
1567  //for(i=c->strat->sl; i>0;i--)
1568  //  if(c->strat->lenS[i]<c->strat->lenS[i-1]) printf("fehler bei %d\n",i);
1569  if (c->Rcounter>50) {
1570    c->Rcounter=0;
1571    cleanS(c->strat,c);
1572  }
1573
1574#ifdef HAVE_PLURAL
1575  // for SCA:
1576  // here write at the end of nodes_final[spc_final,...,spc_final+lmdeg-1]
1577  if(rIsSCA(c->r))
1578  {
1579    const poly pNext = pNext(h);
1580
1581    if(pNext != NULL)
1582    {
1583      // for additional polynomials
1584      const unsigned int m_iFirstAltVar = scaFirstAltVar(c->r);
1585      const unsigned int m_iLastAltVar  = scaLastAltVar(c->r);
1586
1587      int N = // c->r->N;
1588              m_iLastAltVar - m_iFirstAltVar + 1; // should be enough
1589      // TODO: but we may also use got = gcd({m}_{m\in f}))!
1590
1591       poly* array_arg=(poly*)omalloc(N*sizeof(poly)); // !
1592       int j = 0;
1593
1594
1595      for( unsigned short v = m_iFirstAltVar; v <= m_iLastAltVar; v++ )
1596      // for all x_v | Ann(lm(h))
1597      if( p_GetExp(h, v, c->r) ) // TODO: use 'got' here!
1598      {
1599        assume(p_GetExp(h, v, c->r)==1);
1600
1601        poly p = sca_pp_Mult_xi_pp(v, pNext, c->r); // x_v * h;
1602
1603        if(p != NULL) // if (x_v * h != 0)
1604          array_arg[j++] = p;
1605      } // for all x_v | Ann(lm(h))
1606
1607      c->introduceDelayedPairs(array_arg, j);
1608
1609      omfree(array_arg); // !!!
1610    }
1611//     Print("Saturation - done!!!\n");
1612  }
1613#endif // if SCAlgebra
1614
1615
1616  if(!ip){
1617    qsort(nodes_final,spc_final,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
1618
1619
1620    c->apairs=spn_merge(c->apairs,c->pair_top+1,nodes_final,spc_final,c);
1621    c->pair_top+=spc_final;
1622    clean_top_of_pair_list(c);
1623    omfree(nodes_final);
1624    return NULL;
1625  }
1626  {
1627    *ip=spc_final;
1628    return nodes_final;
1629  }
1630
1631
1632
1633}
1634
1635
1636static poly redNF2 (poly h,slimgb_alg* c , int &len, number&  m,int n)
1637{
1638  m=nInit(1);
1639  if (h==NULL) return NULL;
1640
1641  assume(len==pLength(h));
1642  kStrategy strat=c->strat;
1643  if (0 > strat->sl)
1644  {
1645    return h;
1646  }
1647  int j;
1648
1649  LObject P(h);
1650  P.SetShortExpVector();
1651  P.bucket = kBucketCreate(currRing);
1652  // BOOLEAN corr=lenS_correct(strat);
1653  kBucketInit(P.bucket,P.p,len /*pLength(P.p)*/);
1654  //wlen_set lenSw=(wlen_set) c->strat->lenS;
1655  //FIXME: plainly wrong
1656  //strat->lenS;
1657  //if (strat->lenSw!=NULL)
1658  //  lenSw=strat->lenSw;
1659  //int max_pos=simple_posInS(strat,P.p);
1660  loop
1661  {
1662      int dummy=strat->sl;
1663      j=kFindDivisibleByInS_easy(strat,P.p,P.sev);
1664      //j=kFindDivisibleByInS(strat,&dummy,&P);
1665      if ((j>=0) && ((!n)||
1666        ((strat->lenS[j]<=n) &&
1667         ((strat->lenSw==NULL)||
1668         (strat->lenSw[j]<=n)))))
1669      {
1670        nNormalize(pGetCoeff(P.p));
1671#ifdef KDEBUG
1672        if (TEST_OPT_DEBUG)
1673        {
1674          PrintS("red:");
1675          wrp(h);
1676          PrintS(" with ");
1677          wrp(strat->S[j]);
1678        }
1679#endif
1680
1681        number coef=kBucketPolyRed(P.bucket,strat->S[j],
1682                                   strat->lenS[j]/*pLength(strat->S[j])*/,
1683                                   strat->kNoether);
1684  number m2=nMult(m,coef);
1685  nDelete(&m);
1686  m=m2;
1687        nDelete(&coef);
1688        h = kBucketGetLm(P.bucket);
1689
1690  if (h==NULL) {
1691    len=0;
1692    kBucketDestroy(&P.bucket);
1693    return
1694    NULL;}
1695        P.p=h;
1696        P.t_p=NULL;
1697        P.SetShortExpVector();
1698#ifdef KDEBUG
1699        if (TEST_OPT_DEBUG)
1700        {
1701          PrintS("\nto:");
1702          wrp(h);
1703          PrintLn();
1704        }
1705#endif
1706      }
1707      else
1708      {
1709        kBucketClear(P.bucket,&(P.p),&len);
1710        kBucketDestroy(&P.bucket);
1711        pNormalize(P.p);
1712  assume(len==(pLength(P.p)));
1713        return P.p;
1714      }
1715    }
1716}
1717
1718
1719
1720static poly redTailShort(poly h, kStrategy strat){
1721  if (h==NULL) return NULL;//n_Init(1,currRing);
1722  if (TEST_V_MODPSOLVSB){
1723    bit_reduce(pNext(h), strat->tailRing);
1724  }
1725  int sl=strat->sl;
1726  int i;
1727  int len=pLength(h);
1728  for(i=0;i<=strat->sl;i++){
1729    if((strat->lenS[i]>2) || ((strat->lenSw!=NULL) && (strat->lenSw[i]>2)))
1730      break;
1731  }
1732  return(redNFTail(h,i-1,strat, len));
1733}
1734
1735static void line_of_extended_prod(int fixpos,slimgb_alg* c){
1736    if (c->gcd_of_terms[fixpos]==NULL)
1737  {
1738    c->gcd_of_terms[fixpos]=gcd_of_terms(c->S->m[fixpos],c->r);
1739    if (c->gcd_of_terms[fixpos])
1740    {
1741      int i;
1742      for(i=0;i<fixpos;i++)
1743        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)))
1744{
1745          c->states[fixpos][i]=HASTREP;
1746    c->extended_product_crit++;
1747}
1748      for(i=fixpos+1;i<c->n;i++)
1749        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)))
1750  {        c->states[i][fixpos]=HASTREP;
1751  c->extended_product_crit++;
1752  }
1753    }
1754  }
1755}
1756static void c_S_element_changed_hook(int pos, slimgb_alg* c){
1757  length_one_crit(c,pos, c->lengths[pos]);
1758  if (!c->nc)
1759    line_of_extended_prod(pos,c);
1760}
1761class poly_tree_node {
1762public:
1763  poly p;
1764  poly_tree_node* l;
1765  poly_tree_node* r;
1766  int n;
1767  poly_tree_node(int sn):l(NULL),r(NULL),n(sn){}
1768};
1769class exp_number_builder{
1770public:
1771  poly_tree_node* top_level;
1772  int n;
1773  int get_n(poly p);
1774  exp_number_builder():top_level(0),n(0){}
1775};
1776int exp_number_builder::get_n(poly p){
1777  poly_tree_node** node=&top_level;
1778  while(*node!=NULL){
1779    int c=pLmCmp(p,(*node)->p);
1780    if (c==0) return (*node)->n;
1781    if (c==-1) node=&((*node)->r);
1782    else
1783      node=&((*node)->l);
1784  }
1785  (*node)= new poly_tree_node(n);
1786  n++;
1787  (*node)->p=pLmInit(p);
1788  return (*node)->n;
1789}
1790
1791//mac_polys exp are smaller iff they are greater by monomial ordering
1792//corresponding to solving linear equations notation
1793
1794//! obsolete
1795struct int_poly_pair{
1796  poly p;
1797  int n;
1798};
1799
1800
1801//! obsolete
1802void t2ippa_rec(poly* ip,int* ia, poly_tree_node* k, int &offset){
1803    if(!k) return;
1804    t2ippa_rec(ip,ia,k->l,offset);
1805    ip[offset]=k->p;
1806    ia[k->n]=offset;
1807    ++offset;
1808
1809    t2ippa_rec(ip,ia,k->r,offset);
1810    delete k;
1811  }
1812
1813//! obsolete
1814void t2ippa(poly* ip,int* ia,exp_number_builder & e){
1815
1816  int o=0;
1817  t2ippa_rec(ip,ia,e.top_level,o);
1818}
1819int anti_poly_order(const void* a, const void* b){
1820  return -pLmCmp(((int_poly_pair*) a)->p,((int_poly_pair*) b)->p );
1821}
1822
1823BOOLEAN is_valid_ro(red_object & ro){
1824  red_object r2=ro;
1825  ro.validate();
1826  if ((r2.p!=ro.p)||(r2.sev!=ro.sev)) return FALSE;
1827  return TRUE;
1828}
1829int terms_sort_crit(const void* a, const void* b){
1830  return -pLmCmp(*((poly*) a),*((poly*) b));
1831}
1832static void unify_terms(poly* terms,int & sum){
1833  if (sum==0) return;
1834  int last=0;
1835  int curr=1;
1836  while(curr<sum){
1837    if (!(pLmEqual(terms[curr],terms[last]))){
1838      terms[++last]=terms[curr];
1839    }
1840    ++curr;
1841  }
1842  sum=last+1;
1843}
1844static void export_mat(number* number_array,int pn, int tn,const char* format_str, int mat_nr){
1845  char matname[20];
1846  sprintf(matname,format_str,mat_nr);
1847  FILE* out=fopen(matname,"w");
1848  int i,j;
1849  fprintf(out,"mat=[\n");
1850  for(i=0;i<pn;i++){
1851    fprintf(out,"[\n");
1852    for(j=0;j<tn;j++){
1853      if (j>0){
1854        fprintf(out,", ");
1855      }
1856      fprintf(out,"%i",npInt(number_array[i*tn+j]));
1857     
1858    }
1859    if (i<pn-1)
1860      fprintf(out,"],\n");
1861    else
1862      fprintf(out,"],\n");
1863  }
1864  fprintf(out,"]\n");
1865  fclose(out);
1866}
1867//typedef unsigned short number_type;
1868
1869
1870#ifdef USE_NORO
1871#ifndef NORO_CACHE
1872static void linalg_step_modp(poly *p, poly* p_out, int& pn, poly* terms,int tn, slimgb_alg* c){
1873  static int export_n=0;
1874  assume(terms[tn-1]!=NULL);
1875  assume(rField_is_Zp(c->r));
1876  //I don't do deletes, copies of number_types ...
1877  const number_type zero=0;//npInit(0);
1878  int array_size=pn*tn;
1879  number_type* number_array=(number_type*) omalloc(pn*tn*sizeof(number_type));
1880  int i;
1881  for(i=0;i<array_size;i++){
1882    number_array[i]=zero;
1883  }
1884  for(i=0;i<pn;i++){
1885    poly h=p[i];
1886    //int base=tn*i;
1887    write_poly_to_row(number_array+tn*i,h,terms,tn,c->r);
1888
1889  }
1890#if 0
1891  //export matrix
1892  export_mat(number_array,pn,tn,"mat%i.py",++export_n);
1893#endif
1894  int rank=pn;
1895  simplest_gauss_modp(number_array,rank,tn);
1896  int act_row=0;
1897  int p_pos=0;
1898  for(i=0;i<pn;i++){
1899    poly h=NULL;
1900    int j;
1901    int base=tn*i;
1902    number* row=number_array+base;
1903    h=row_to_poly(row,terms,tn,c->r);
1904   
1905   if (h!=NULL){
1906     p_out[p_pos++]=h;
1907   } 
1908  }
1909  pn=p_pos;
1910  //assert(p_pos==rank)
1911  while(p_pos<pn){
1912    p_out[p_pos++]=NULL;
1913  }
1914#if 0
1915  export_mat(number_array,pn,tn,"mat%i.py",++export_n);
1916#endif
1917}
1918#endif
1919#endif
1920static void mass_add(poly* p, int pn,slimgb_alg* c){
1921    int j;
1922    int* ibuf=(int*) omalloc(pn*sizeof(int));
1923    sorted_pair_node*** sbuf=(sorted_pair_node***) omalloc(pn*sizeof(sorted_pair_node**));
1924    for(j=0;j<pn;j++){
1925      sbuf[j]=add_to_basis_ideal_quotient(p[j],c,ibuf+j);
1926    }
1927    int sum=0;
1928    for(j=0;j<pn;j++){
1929      sum+=ibuf[j];
1930    }
1931    sorted_pair_node** big_sbuf=(sorted_pair_node**) omalloc(sum*sizeof(sorted_pair_node*));
1932    int partsum=0;
1933    for(j=0;j<pn;j++)
1934    {
1935      memmove(big_sbuf+partsum, sbuf[j],ibuf[j]*sizeof(sorted_pair_node*));
1936      omfree(sbuf[j]);
1937      partsum+=ibuf[j];
1938    }
1939
1940    qsort(big_sbuf,sum,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
1941    c->apairs=spn_merge(c->apairs,c->pair_top+1,big_sbuf,sum,c);
1942    c->pair_top+=sum;
1943    clean_top_of_pair_list(c);
1944    omfree(big_sbuf);
1945    omfree(sbuf);
1946    omfree(ibuf);
1947    //omfree(buf);
1948  #ifdef TGB_DEBUG
1949    int z;
1950    for(z=1;z<=c->pair_top;z++)
1951    {
1952      assume(pair_better(c->apairs[z],c->apairs[z-1],c));
1953    }
1954  #endif
1955   
1956}
1957
1958#ifdef NORO_CACHE
1959#ifndef NORO_NON_POLY
1960void NoroCache::evaluateRows(){
1961  //after that can evaluate placeholders
1962  int i;
1963  buffer=(number*) omalloc(nIrreducibleMonomials*sizeof(number));
1964  for(i=0;i<root.branches_len;i++){
1965    evaluateRows(1,root.branches[i]);
1966  }
1967  omfree(buffer);
1968  buffer=NULL;
1969}
1970void NoroCache::evaluateRows(int level, NoroCacheNode* node){
1971  assume(level>=0);
1972  if (node==NULL) return;
1973  if (level<pVariables){
1974    int i,sum;
1975    for(i=0;i<node->branches_len;i++){
1976      evaluateRows(level+1,node->branches[i]);
1977    }
1978  } else {
1979    DataNoroCacheNode* dn=(DataNoroCacheNode*) node;
1980    if (dn->value_len!=backLinkCode){
1981      poly p=dn->value_poly;
1982      #ifndef NORO_SPARSE_ROWS_PRE
1983      dn->row=new DenseRow();
1984      DenseRow* row=dn->row;
1985      memset(buffer,0,sizeof(number)*nIrreducibleMonomials);
1986     
1987      if (p==NULL) {row->array=NULL;row->begin=0;row->end=0; return;}
1988      int i=0;
1989      int idx;
1990      number* a=buffer;
1991      while(p){
1992        DataNoroCacheNode* ref=getCacheReference(p);
1993       
1994        idx=ref->term_index;
1995        assume(idx>=0);
1996        a[idx]=p_GetCoeff(p,currRing);
1997        if (i==0) row->begin=idx;
1998        i++;
1999        pIter(p);
2000      }
2001      row->end=idx+1;
2002      assume(row->end>row->begin);
2003      int len=row->end-row->begin;
2004      row->array=(number*) omalloc((len)*sizeof(number));
2005      memcpy(row->array,a+row->begin,len*sizeof(number));
2006      #else
2007      assume(dn->value_len==pLength(dn->value_poly));
2008      dn->row=new SparseRow(dn->value_len);
2009      SparseRow* row=dn->row;
2010      int i=0;
2011      while(p){
2012        DataNoroCacheNode* ref=getCacheReference(p);
2013       
2014        int idx=ref->term_index;
2015        assume(idx>=0);
2016        row->idx_array[i]=idx;
2017        row->coef_array[i]=p_GetCoeff(p,currRing);
2018       
2019        i++;
2020        pIter(p);
2021      }
2022      if (i!=dn->value_len){
2023        Print("F4 calc wrong, as poly len was wrong\n");
2024      }
2025      assume(i==dn->value_len);
2026      #endif
2027     
2028    } 
2029  }
2030}
2031
2032void NoroCache::evaluatePlaceHolder(number* row,std::vector<NoroPlaceHolder>& place_holders){
2033  int i;
2034  int s=place_holders.size();
2035  for(i=0;i<s;i++){
2036    DataNoroCacheNode* ref=place_holders[i].ref;
2037    number coef=place_holders[i].coef;
2038    if (ref->value_len==backLinkCode){
2039      row[ref->term_index]=npAddM(row[ref->term_index],coef);
2040    } else {
2041      #ifndef NORO_SPARSE_ROWS_PRE
2042      DenseRow* ref_row=ref->row;
2043      if (ref_row==NULL) continue;
2044      number* ref_begin=ref_row->array;
2045      number* ref_end=ref_row->array+(ref_row->end-ref_row->begin);
2046      number* my_pos=row+ref_row->begin;
2047      //TODO npisOne distinction
2048      if (!(npIsOne(coef))){
2049        while(ref_begin!=ref_end){
2050
2051          *my_pos=npAddM(*my_pos,npMult(coef,*ref_begin));
2052          ++ref_begin;
2053          ++my_pos;
2054        }
2055      }
2056      else{
2057        while(ref_begin!=ref_end){
2058
2059          *my_pos=npAddM(*my_pos,*ref_begin);
2060          ++ref_begin;
2061          ++my_pos;
2062        }
2063      }
2064
2065   
2066   
2067    #else
2068    SparseRow* ref_row=ref->row;
2069    if (ref_row==NULL) continue;
2070    int n=ref_row->len;
2071    int j;
2072    int* idx_array=ref_row->idx_array;
2073    number* coef_array=ref_row->coef_array;
2074    for(j=0;j<n;j++){
2075      int idx=idx_array[j];
2076      number ref_coef=coef_array[j];
2077      row[idx]=npAddM(row[idx],npMult(coef,ref_coef));
2078    }
2079    #endif
2080  }
2081  }
2082
2083}
2084#endif
2085
2086
2087
2088//poly noro_red_non_unique(poly p, int &len, NoroCache* cache,slimgb_alg* c);
2089
2090
2091#ifndef NORO_NON_POLY
2092MonRedRes noro_red_mon(poly t, BOOLEAN force_unique, NoroCache* cache,slimgb_alg* c){
2093  MonRedRes res_holder;
2094
2095  //wrp(t);
2096  res_holder.changed=TRUE;
2097  if (force_unique){
2098    DataNoroCacheNode* ref=cache->getCacheReference(t);
2099    if (ref!=NULL){
2100      res_holder.len=ref->value_len;
2101      if (res_holder.len==NoroCache::backLinkCode){
2102        res_holder.len=1;
2103
2104
2105      }
2106      res_holder.coef=p_GetCoeff(t,c->r);
2107      res_holder.p=ref->value_poly;
2108      res_holder.ref=ref;
2109      res_holder.onlyBorrowed=TRUE;
2110      res_holder.changed=TRUE;
2111      p_Delete(&t,c->r);
2112      return res_holder;
2113    }
2114  } else{
2115    BOOLEAN succ;
2116    poly cache_lookup=cache->lookup(t,succ, res_holder.len);//don't own this yet
2117    if (succ){
2118      if (cache_lookup==t){
2119      //know they are equal
2120      //res_holder.len=1;
2121
2122        res_holder.changed=FALSE;
2123        res_holder.p=t;
2124        res_holder.coef=npInit(1);
2125       
2126        res_holder.onlyBorrowed=FALSE;
2127        return res_holder;
2128      }
2129
2130      res_holder.coef=p_GetCoeff(t,c->r);
2131      p_Delete(&t,c->r);
2132
2133      res_holder.p=cache_lookup;
2134
2135      res_holder.onlyBorrowed=TRUE;
2136      return res_holder;
2137
2138    }
2139  }
2140
2141  unsigned long sev=p_GetShortExpVector(t,currRing);
2142  int i=kFindDivisibleByInS_easy(c->strat,t,sev);
2143  if (i>=0){
2144    number coef_bak=p_GetCoeff(t,c->r);
2145
2146    p_SetCoeff(t,npInit(1),c->r);
2147    assume(npIsOne(p_GetCoeff(c->strat->S[i],c->r)));
2148    number coefstrat=p_GetCoeff(c->strat->S[i],c->r);
2149
2150      //poly t_copy_mon=p_Copy(t,c->r);
2151    poly exp_diff=cache->temp_term;
2152    p_ExpVectorDiff(exp_diff,t,c->strat->S[i],c->r);
2153    p_SetCoeff(exp_diff,npNeg(npInvers(coefstrat)),c->r);
2154    p_Setm(exp_diff,c->r);
2155    assume(c->strat->S[i]!=NULL);
2156      //poly t_to_del=t;
2157    poly res;
2158    res=pp_Mult_mm(pNext(c->strat->S[i]),exp_diff,c->r);
2159
2160    res_holder.len=c->strat->lenS[i]-1;
2161    res=noro_red_non_unique(res,res_holder.len,cache,c);
2162   
2163    DataNoroCacheNode* ref=cache->insert(t,res,res_holder.len);
2164    p_Delete(&t,c->r);
2165      //p_Delete(&t_copy_mon,c->r);
2166      //res=pMult_nn(res,coef_bak);
2167
2168    res_holder.changed=TRUE;
2169    res_holder.p=res;
2170    res_holder.coef=coef_bak;
2171    res_holder.onlyBorrowed=TRUE;
2172    res_holder.ref=ref;
2173    return res_holder;
2174
2175  } else {
2176    number coef_bak=p_GetCoeff(t,c->r);
2177    number one=npInit(1);
2178    p_SetCoeff(t,one,c->r);
2179    res_holder.len=1;
2180    if (!(force_unique)){
2181      res_holder.ref=cache->insert(t,t,res_holder.len);
2182      p_SetCoeff(t,coef_bak,c->r);
2183      //return t;
2184
2185      //we need distinction
2186      res_holder.changed=FALSE;
2187      res_holder.p=t;
2188
2189      res_holder.coef=npInit(1);
2190      res_holder.onlyBorrowed=FALSE;
2191      return res_holder;
2192    } else {
2193      res_holder.ref=cache->insertAndTransferOwnerShip(t,c->r);
2194      res_holder.coef=coef_bak;
2195      res_holder.onlyBorrowed=TRUE;
2196      res_holder.changed=FALSE;
2197      res_holder.p=t;
2198      return res_holder;
2199    }
2200  }
2201
2202}
2203#endif
2204//SparseRow* noro_red_to_non_poly(poly p, int &len, NoroCache* cache,slimgb_alg* c);
2205#ifndef NORO_NON_POLY
2206//len input and out: Idea: reverse addition
2207poly noro_red_non_unique(poly p, int &len, NoroCache* cache,slimgb_alg* c){
2208  assume(len==pLength(p));
2209  poly orig_p=p;
2210  if (p==NULL) {
2211    len=0;
2212    return NULL;
2213  }
2214  kBucket_pt bucket=kBucketCreate(currRing);
2215  kBucketInit(bucket,NULL,0);
2216  poly unchanged_head=NULL;
2217  poly unchanged_tail=NULL;
2218  int unchanged_size=0;
2219
2220  while(p){
2221    poly t=p;
2222    pIter(p);
2223    pNext(t)=NULL;
2224#ifndef NDEBUG
2225    number coef_debug=p_GetCoeff(t,currRing);
2226#endif
2227    MonRedRes red=noro_red_mon(t,FALSE,cache,c);
2228    if ((!(red.changed))&&(!(red.onlyBorrowed))){
2229      unchanged_size++;
2230      assume(npIsOne(red.coef));
2231      assume(p_GetCoeff(red.p,currRing)==coef_debug);
2232      if (unchanged_head){
2233        pNext(unchanged_tail)=red.p;
2234        pIter(unchanged_tail);
2235      } else{
2236        unchanged_tail=red.p;
2237        unchanged_head=red.p;
2238      }
2239    } else{
2240      assume(red.len==pLength(red.p));
2241      if (red.onlyBorrowed){
2242        if (npIsOne(red.coef)){
2243          t=p_Copy(red.p,currRing);
2244        }else
2245        t=pp_Mult_nn(red.p,red.coef,currRing);
2246      } else {
2247        if (npIsOne(red.coef))
2248        t=red.p;
2249        else
2250         t=p_Mult_nn(red.p,red.coef,currRing);
2251      }
2252      kBucket_Add_q(bucket,t,&red.len);
2253    }
2254  }
2255  poly res=NULL;
2256  len=0;
2257  kBucket_Add_q(bucket,unchanged_head,&unchanged_size);
2258  kBucketClear(bucket,&res,&len);
2259  kBucketDestroy(&bucket);
2260
2261
2262
2263
2264  return res;
2265}
2266#endif
2267#ifdef NORO_SPARSE_ROWS_PRE
2268//len input and out: Idea: reverse addition
2269
2270/*template <class number_type> SparseRow<number_type>* noro_red_to_non_poly(poly p, int &len, NoroCache<number_type>* cache,slimgb_alg* c){
2271  if (npPrimeM<255){
2272    return noro_red_to_non_poly_t<tgb_uint8>(p,len,cache,c);
2273  } else {
2274    if (npPrimeM<65000){
2275      return noro_red_to_non_poly_t<tgb_uint16>(p,len,cache,c);
2276    } else{
2277      return noro_red_to_non_poly_t<tgb_uint32>(p,len,cache,c);
2278    }
2279  }
2280}*/
2281#endif
2282//len input and out: Idea: reverse addition
2283#ifndef NORO_NON_POLY
2284std::vector<NoroPlaceHolder> noro_red(poly p, int &len, NoroCache* cache,slimgb_alg* c){
2285  std::vector<NoroPlaceHolder> res;
2286   while(p){
2287      poly t=p;
2288      pIter(p);
2289      pNext(t)=NULL;
2290
2291      MonRedRes red=noro_red_mon(t,TRUE,cache,c);
2292      assume(red.onlyBorrowed);
2293      assume(red.coef);
2294      assume(red.ref);
2295      NoroPlaceHolder h;
2296      h.ref=red.ref;
2297      h.coef=red.coef;
2298      assume(!((h.ref->value_poly==NULL) &&(h.ref->value_len!=0)));
2299      if (h.ref->value_poly)
2300        res.push_back(h);
2301    }
2302    return res;
2303}
2304#endif
2305
2306
2307
2308
2309
2310#endif
2311#ifdef USE_NORO
2312#ifndef NORO_CACHE
2313void noro_step(poly*p,int &pn,slimgb_alg* c){
2314  poly* reduced=(poly*) omalloc(pn*sizeof(poly));
2315  int j;
2316  int* reduced_len=(int*) omalloc(pn*sizeof(int));
2317  int reduced_c=0;
2318  //if (TEST_OPT_PROT)
2319  //  PrintS("reduced system:\n");
2320#ifdef NORO_CACHE
2321  NoroCache cache;
2322#endif
2323  for(j=0;j<pn;j++){
2324   
2325    poly h=p[j];
2326    int h_len=pLength(h);
2327
2328    number coef;
2329#ifndef NORO_CACHE
2330    h=redNF2(p_Copy(h,c->r),c,h_len,coef,0);
2331#else
2332    h=noro_red(p_Copy(h,c->r),h_len,&cache,c);
2333    assume(pLength(h)==h_len);
2334#endif
2335    if (h!=NULL){
2336#ifndef NORO_CACHE
2337     
2338      h=redNFTail(h,c->strat->sl,c->strat,h_len);
2339      h_len=pLength(h);
2340#endif
2341     
2342
2343      reduced[reduced_c]=h;
2344      reduced_len[reduced_c]=h_len;
2345      reduced_c++;
2346      if (TEST_OPT_PROT)
2347        Print("%d ",h_len);
2348    }
2349  }
2350  int reduced_sum=0;
2351  for(j=0;j<reduced_c;j++){
2352    reduced_sum+=reduced_len[j];
2353  }
2354  poly* terms=(poly*) omalloc(reduced_sum*sizeof(poly));
2355  int tc=0;
2356  for(j=0;j<reduced_c;j++){
2357    poly h=reduced[j];
2358   
2359    while(h!=NULL){
2360      terms[tc++]=h;
2361      pIter(h);
2362      assume(tc<=reduced_sum);
2363    }
2364  }
2365  assume(tc==reduced_sum);
2366  qsort(terms,reduced_sum,sizeof(poly),terms_sort_crit);
2367  int nterms=reduced_sum;
2368  if (TEST_OPT_PROT)
2369    Print("orig estimation:%i\n",reduced_sum);
2370  unify_terms(terms,nterms);
2371  if (TEST_OPT_PROT)
2372    Print("actual number of columns:%i\n",nterms);
2373  int rank=reduced_c;
2374  linalg_step_modp(reduced, p,rank,terms,nterms,c);
2375  omfree(terms);
2376 
2377  pn=rank;
2378  omfree(reduced);
2379
2380  if (TEST_OPT_PROT)
2381    PrintS("\n");
2382}
2383#else
2384
2385
2386
2387#endif
2388#endif
2389static void go_on (slimgb_alg* c){
2390  //set limit of 1000 for multireductions, at the moment for
2391  //programming reasons
2392  #ifdef USE_NORO
2393  //Print("module rank%d\n",c->S->rank);
2394  const BOOLEAN use_noro=((!(c->nc))&&(c->S->rank<=1)&&(rField_is_Zp(c->r)) &&(!(c->eliminationProblem))&&(npPrimeM<=32003));
2395  #else
2396  const BOOLEAN use_noro=FALSE;
2397  #endif
2398  int i=0;
2399  c->average_length=0;
2400  for(i=0;i<c->n;i++){
2401    c->average_length+=c->lengths[i];
2402  }
2403  c->average_length=c->average_length/c->n;
2404  i=0;
2405  int max_pairs=bundle_size;
2406  if (use_noro)
2407    max_pairs=bundle_size_noro;
2408  poly* p=(poly*) omalloc((max_pairs+1)*sizeof(poly));//nullterminated
2409
2410  int curr_deg=-1;
2411  while(i<max_pairs){
2412    sorted_pair_node* s=top_pair(c);//here is actually chain criterium done
2413
2414
2415    if (!s) break;
2416
2417    if(curr_deg>=0){
2418      if (s->deg >curr_deg) break;
2419    }
2420
2421    else curr_deg=s->deg;
2422    quick_pop_pair(c);
2423    if(s->i>=0){
2424      //be careful replace_pair use createShortSpoly which is not noncommutative
2425      now_t_rep(s->i,s->j,c);
2426    replace_pair(s->i,s->j,c);
2427
2428    if(s->i==s->j) {
2429      free_sorted_pair_node(s,c->r);
2430      continue;
2431    }
2432    now_t_rep(s->i,s->j,c);
2433    }
2434    poly h;
2435    if(s->i>=0)
2436    {
2437#ifdef HAVE_PLURAL
2438      if (c->nc){
2439        h= nc_CreateSpoly(c->S->m[s->i], c->S->m[s->j]/*, NULL*/, c->r);
2440        if (h!=NULL)
2441          pCleardenom(h);
2442      }
2443      else
2444#endif
2445        h=ksOldCreateSpoly(c->S->m[s->i], c->S->m[s->j], NULL, c->r);
2446    }
2447    else
2448      h=s->lcm_of_lm;
2449    // if(s->i>=0)
2450//       now_t_rep(s->j,s->i,c);
2451    number coef;
2452    int mlen=pLength(h);
2453    if ((!c->nc)&(!(use_noro))){
2454      h=redNF2(h,c,mlen,coef,2);
2455      redTailShort(h,c->strat);
2456      nDelete(&coef);
2457    }
2458    free_sorted_pair_node(s,c->r);
2459    if(!h) continue;
2460    int len=pLength(h);
2461    p[i]=h;
2462
2463    i++;
2464  }
2465  p[i]=NULL;
2466//  pre_comp(p,i,c);
2467  if(i==0){
2468    omfree(p);
2469    return;
2470  }
2471  #ifdef TGB_RESORT_PAIRS
2472  c->replaced=new bool[c->n];
2473  c->used_b=FALSE;
2474  #endif
2475 
2476  c->normal_forms+=i;
2477  int j;
2478#ifdef USE_NORO
2479  //if ((!(c->nc))&&(rField_is_Zp(c->r))){
2480  if (use_noro){
2481    int pn=i;
2482    if (pn==0) {omfree(p);return;}
2483   
2484    {
2485     
2486      if (npPrimeM<255){
2487        noro_step<tgb_uint8>(p,pn,c);
2488      } else {
2489        if (npPrimeM<65000){
2490          noro_step<tgb_uint16>(p,pn,c);
2491        } else{
2492          noro_step<tgb_uint32>(p,pn,c);
2493        }
2494      }
2495     
2496     
2497     
2498    }
2499   
2500    if (TEST_OPT_PROT){
2501      Print("reported rank:%i\n",pn);
2502    }
2503    mass_add(p,pn,c);
2504    omfree(p);
2505    return;
2506    /*if (TEST_OPT_PROT)
2507      for(j=0;j<pn;j++){
2508        p_wrp(p[j],c->r);
2509      }*/
2510  }
2511#endif
2512  red_object* buf=(red_object*) omalloc(i*sizeof(red_object));
2513  for(j=0;j<i;j++){
2514    buf[j].p=p[j];
2515    buf[j].sev=pGetShortExpVector(p[j]);
2516    buf[j].bucket = kBucketCreate(currRing);
2517    if (c->eliminationProblem){
2518        buf[j].sugar=pTotaldegree_full(p[j]);
2519    }
2520    int len=pLength(p[j]);
2521    kBucketInit(buf[j].bucket,buf[j].p,len);
2522    buf[j].initial_quality=buf[j].guess_quality(c);
2523    assume(buf[j].initial_quality>=0);
2524  }
2525  omfree(p);
2526  qsort(buf,i,sizeof(red_object),red_object_better_gen);
2527//    Print("\ncurr_deg:%i\n",curr_deg);
2528  if (TEST_OPT_PROT)
2529  {
2530    Print("%dM[%d,",curr_deg,i);
2531  }
2532
2533  multi_reduction(buf, i, c);
2534  #ifdef TGB_RESORT_PAIRS
2535  if (c->used_b) {
2536    if (TEST_OPT_PROT)
2537        PrintS("B");
2538    int e;
2539    for(e=0;e<=c->pair_top;e++){
2540        if(c->apairs[e]->i<0) continue;
2541        assume(c->apairs[e]->j>=0);
2542        if ((c->replaced[c->apairs[e]->i])||(c->replaced[c->apairs[e]->j])) {
2543            sorted_pair_node* s=c->apairs[e];
2544            s->expected_length=pair_weighted_length(s->i,s->j,c);
2545        }
2546    }
2547    qsort(c->apairs,c->pair_top+1,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
2548  }
2549  #endif
2550#ifdef TGB_DEBUG
2551 {
2552   int k;
2553   for(k=0;k<i;k++)
2554   {
2555     assume(kFindDivisibleByInS_easy(c->strat,buf[k])<0);
2556     int k2;
2557     for(k2=0;k2<i;k2++)
2558     {
2559       if(k==k2) continue;
2560       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));
2561     }
2562   }
2563 }
2564#endif
2565  //resort S
2566
2567  if (TEST_OPT_PROT)
2568      Print("%i]",i);
2569
2570  poly* add_those=(poly*) omalloc(i*sizeof(poly));
2571  for(j=0;j<i;j++)
2572  {
2573    int len;
2574    poly p;
2575    buf[j].flatten();
2576    kBucketClear(buf[j].bucket,&p, &len);
2577    kBucketDestroy(&buf[j].bucket);
2578
2579    //if (!c->nc) {
2580      if (c->tailReductions){
2581      p=redNFTail(p,c->strat->sl,c->strat, 0);
2582      } else {
2583      p=redTailShort(p, c->strat);
2584      }
2585      //}
2586      add_those[j]=p;
2587
2588    //sbuf[j]=add_to_basis(p,-1,-1,c,ibuf+j);
2589  }
2590  mass_add(add_those,i,c);
2591  omfree(add_those);
2592  omfree(buf);
2593 
2594 
2595 
2596 
2597 
2598 
2599
2600  if (TEST_OPT_PROT)
2601      Print("(%d)",c->pair_top+1);
2602  //TODO: implement that while(!(idIs0(c->add_later)))
2603  #ifdef TGB_RESORT_PAIRS
2604  delete c->replaced;
2605  c->replaced=NULL;
2606  c->used_b=FALSE;
2607  #endif
2608  return;
2609}
2610
2611
2612
2613#ifdef REDTAIL_S
2614
2615static poly redNFTail (poly h,const int sl,kStrategy strat, int len)
2616{
2617  BOOLEAN nc=rIsPluralRing(currRing);
2618  if (h==NULL) return NULL;
2619  pTest(h);
2620  if (0 > sl)
2621    return h;
2622  if (pNext(h)==NULL) return h;
2623
2624  int j;
2625  poly res=h;
2626  poly act=res;
2627  LObject P(pNext(h));
2628  pNext(res)=NULL;
2629  P.bucket = kBucketCreate(currRing);
2630  len--;
2631  h=P.p;
2632  if (len <=0) len=pLength(h);
2633  kBucketInit(P.bucket,h /*P.p*/,len /*pLength(P.p)*/);
2634  pTest(h);
2635  loop
2636  {
2637      P.p=h;
2638      P.t_p=NULL;
2639      P.SetShortExpVector();
2640      loop
2641      {
2642          int dummy=strat->sl;
2643          j=kFindDivisibleByInS_easy(strat,P.p,P.sev);//kFindDivisibleByInS(strat,&dummy,&P);
2644          if (j>=0)
2645          {
2646#ifdef REDTAIL_PROT
2647            PrintS("r");
2648#endif
2649            nNormalize(pGetCoeff(P.p));
2650#ifdef KDEBUG
2651            if (TEST_OPT_DEBUG)
2652            {
2653              PrintS("red tail:");
2654              wrp(h);
2655              PrintS(" with ");
2656              wrp(strat->S[j]);
2657            }
2658#endif
2659            number coef;
2660            pTest(strat->S[j]);
2661#ifdef HAVE_PLURAL
2662            if (nc){
2663              nc_BucketPolyRed_Z(P.bucket, strat->S[j], &coef);
2664            } else
2665#endif
2666              coef=kBucketPolyRed(P.bucket,strat->S[j],
2667                                strat->lenS[j]/*pLength(strat->S[j])*/,strat->kNoether);
2668            pMult_nn(res,coef);
2669            nDelete(&coef);
2670            h = kBucketGetLm(P.bucket);
2671            pTest(h);
2672            if (h==NULL)
2673            {
2674#ifdef REDTAIL_PROT
2675              PrintS(" ");
2676#endif
2677        kBucketDestroy(&P.bucket);
2678              return res;
2679            }
2680            pTest(h);
2681            P.p=h;
2682            P.t_p=NULL;
2683            P.SetShortExpVector();
2684#ifdef KDEBUG
2685            if (TEST_OPT_DEBUG)
2686            {
2687              PrintS("\nto tail:");
2688              wrp(h);
2689              PrintLn();
2690            }
2691#endif
2692          }
2693          else
2694          {
2695#ifdef REDTAIL_PROT
2696            PrintS("n");
2697#endif
2698            break;
2699          }
2700      } /* end loop current mon */
2701      //   poly tmp=pHead(h /*kBucketGetLm(P.bucket)*/);
2702      //act->next=tmp;pIter(act);
2703      act->next=kBucketExtractLm(P.bucket);pIter(act);
2704      h = kBucketGetLm(P.bucket);
2705      if (h==NULL)
2706      {
2707#ifdef REDTAIL_PROT
2708        PrintS(" ");
2709#endif
2710  kBucketDestroy(&P.bucket);
2711        return res;
2712      }
2713      pTest(h);
2714  }
2715}
2716#endif
2717
2718
2719//try to fill, return FALSE iff queue is empty
2720
2721//transfers ownership of m to mat
2722void init_with_mac_poly(tgb_sparse_matrix* mat, int row, mac_poly m){
2723  assume(mat->mp[row]==NULL);
2724  mat->mp[row]=m;
2725#ifdef TGB_DEBUG
2726  mac_poly r=m;
2727  while(r){
2728    assume(r->exp<mat->columns);
2729    r=r->next;
2730  }
2731#endif
2732}
2733poly free_row_to_poly(tgb_sparse_matrix* mat, int row, poly* monoms, int monom_index){
2734  poly p=NULL;
2735  poly* set_this=&p;
2736  mac_poly r=mat->mp[row];
2737  mat->mp[row]=NULL;
2738  while(r)
2739  {
2740    (*set_this)=pLmInit(monoms[monom_index-1-r->exp]);
2741    pSetCoeff((*set_this),r->coef);
2742    set_this=&((*set_this)->next);
2743    mac_poly old=r;
2744    r=r->next;
2745    delete old;
2746
2747  }
2748  return p;
2749
2750}
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762static int poly_crit(const void* ap1, const void* ap2){
2763  poly p1,p2;
2764  p1=*((poly*) ap1);
2765  p2=*((poly*)ap2);
2766
2767  int c=pLmCmp(p1,p2);
2768  if (c !=0) return c;
2769  int l1=pLength(p1);
2770  int l2=pLength(p2);
2771  if (l1<l2) return -1;
2772  if (l1>l2) return 1;
2773  return 0;
2774}
2775void slimgb_alg::introduceDelayedPairs(poly* pa,int s){
2776    if (s==0) return;
2777    sorted_pair_node** si_array=(sorted_pair_node**) omalloc(s* sizeof(sorted_pair_node*));
2778
2779    for( unsigned int i = 0; i < s; i++ )
2780    {
2781        sorted_pair_node* si=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
2782        si->i=-1;
2783        si->j=-2;
2784        poly p=pa[i];
2785        simplify_poly(p,r);
2786        si->expected_length=pQuality(p,this,pLength(p));
2787        si->deg=pTotaldegree_full(p);
2788        /*if (!rField_is_Zp(r)){
2789          pContent(p);
2790          pCleardenom(p);
2791        }*/
2792        si->lcm_of_lm=p;
2793
2794        //      c->apairs[n-1-i]=si;
2795        si_array[i]=si;
2796
2797  }
2798
2799  qsort(si_array,s,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
2800    apairs=spn_merge(apairs,pair_top+1,si_array,s,this);
2801  pair_top+=s;
2802  omfree(si_array);
2803}
2804slimgb_alg::slimgb_alg(ideal I, int syz_comp,BOOLEAN F4){
2805
2806  lastCleanedDeg=-1;
2807  completed=FALSE;
2808  this->syz_comp=syz_comp;
2809  r=currRing;
2810  nc=rIsPluralRing(r);
2811  this->lastDpBlockStart=get_last_dp_block_start(r);
2812  //Print("last dp Block start: %i\n", this->lastDpBlockStart);
2813  is_homog=TRUE;
2814  {
2815    int hzz;
2816    for(hzz=0;hzz<IDELEMS(I);hzz++){
2817      assume(I->m[hzz]!=NULL);
2818      int d=pTotaldegree(I->m[hzz]);
2819      poly t=I->m[hzz]->next;
2820      while(t)
2821      {
2822        if (d!=pTotaldegree(t,r))
2823        {
2824          is_homog=FALSE;
2825          break;
2826        }
2827        t=t->next;
2828      }
2829      if(!(is_homog)) break;
2830    }
2831  }
2832  eliminationProblem=((!(is_homog))&&((pLexOrder)||(I->rank>1)));
2833  tailReductions=((is_homog)||((TEST_OPT_REDTAIL)&&(!(I->rank>1))));
2834  //  Print("is homog:%d",c->is_homog);
2835  void* h;
2836  poly hp;
2837  int i,j;
2838  to_destroy=NULL;
2839  easy_product_crit=0;
2840  extended_product_crit=0;
2841  if (rField_is_Zp(r))
2842    isDifficultField=FALSE;
2843  else
2844    isDifficultField=TRUE;
2845  //not fully correct
2846  //(rChar()==0);
2847  F4_mode=F4;
2848
2849  reduction_steps=0;
2850  last_index=-1;
2851
2852  F=NULL;
2853  F_minus=NULL;
2854
2855  Rcounter=0;
2856
2857  soon_free=NULL;
2858
2859  tmp_lm=pOne();
2860
2861  normal_forms=0;
2862  current_degree=1;
2863
2864  max_pairs=5*I->idelems();
2865
2866  apairs=(sorted_pair_node**) omalloc(sizeof(sorted_pair_node*)*max_pairs);
2867  pair_top=-1;
2868
2869  int n=I->idelems();
2870  array_lengths=n;
2871
2872
2873  i=0;
2874  this->n=0;
2875  T_deg=(int*) omalloc(n*sizeof(int));
2876  if(eliminationProblem)
2877    T_deg_full=(int*) omalloc(n*sizeof(int));
2878  else
2879    T_deg_full=NULL;
2880  tmp_pair_lm=(poly*) omalloc(n*sizeof(poly));
2881  tmp_spn=(sorted_pair_node**) omalloc(n*sizeof(sorted_pair_node*));
2882  lm_bin=omGetSpecBin(POLYSIZE + (r->ExpL_Size)*sizeof(long));
2883#ifdef HEAD_BIN
2884  HeadBin=omGetSpecBin(POLYSIZE + (currRing->ExpL_Size)*sizeof(long));
2885#endif
2886  /* omUnGetSpecBin(&(c->HeadBin)); */
2887  #ifndef HAVE_BOOST
2888  #ifdef USE_STDVECBOOL
2889  #else
2890  h=omalloc(n*sizeof(char*));
2891
2892  states=(char**) h;
2893  #endif
2894  #endif
2895  h=omalloc(n*sizeof(int));
2896  lengths=(int*) h;
2897  weighted_lengths=(wlen_type*)omalloc(n*sizeof(wlen_type));
2898  gcd_of_terms=(poly*) omalloc(n*sizeof(poly));
2899
2900  short_Exps=(long*) omalloc(n*sizeof(long));
2901  if (F4_mode)
2902    S=idInit(n,I->rank);
2903  else
2904    S=idInit(1,I->rank);
2905  strat=new skStrategy;
2906  if (eliminationProblem)
2907    strat->honey=TRUE;
2908  strat->syzComp = 0;
2909  initBuchMoraCrit(strat);
2910  initBuchMoraPos(strat);
2911  strat->initEcart = initEcartBBA;
2912  strat->tailRing=r;
2913  strat->enterS = enterSBba;
2914  strat->sl = -1;
2915  i=n;
2916  i=1;//some strange bug else
2917  /* initS(c->S,NULL,c->strat); */
2918  /* intS start: */
2919  // i=((i+IDELEMS(c->S)+15)/16)*16;
2920  strat->ecartS=(intset)omAlloc(i*sizeof(int)); /*initec(i);*/
2921  strat->sevS=(unsigned long*)omAlloc0(i*sizeof(unsigned long));
2922  /*initsevS(i);*/
2923  strat->S_2_R=(int*)omAlloc0(i*sizeof(int));/*initS_2_R(i);*/
2924  strat->fromQ=NULL;
2925  strat->Shdl=idInit(1,1);
2926  strat->S=strat->Shdl->m;
2927  strat->lenS=(int*)omAlloc0(i*sizeof(int));
2928  if((isDifficultField)||(eliminationProblem))
2929    strat->lenSw=(wlen_type*)omAlloc0(i*sizeof(wlen_type));
2930  else
2931    strat->lenSw=NULL;
2932  sorted_pair_node* si;
2933  assume(n>0);
2934  add_to_basis_ideal_quotient(I->m[0],this,NULL);
2935
2936  assume(strat->sl==strat->Shdl->idelems()-1);
2937  if(!(F4_mode))
2938  {
2939        poly* array_arg=I->m;
2940        array_arg++;
2941        introduceDelayedPairs(array_arg,n-1);
2942        /*
2943    for (i=1;i<n;i++)//the 1 is wanted, because first element is added to basis
2944    {
2945      //     add_to_basis(I->m[i],-1,-1,c);
2946      si=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
2947      si->i=-1;
2948      si->j=-2;
2949      si->expected_length=pQuality(I->m[i],this,pLength(I->m[i]));
2950      si->deg=pTotaldegree(I->m[i]);
2951      if (!rField_is_Zp(r)){
2952        pCleardenom(I->m[i]);
2953      }
2954      si->lcm_of_lm=I->m[i];
2955
2956      //      c->apairs[n-1-i]=si;
2957      apairs[n-i-1]=si;
2958      ++(pair_top);
2959    }*/
2960  }
2961  else
2962  {
2963    for (i=1;i<n;i++)//the 1 is wanted, because first element is added to basis
2964      add_to_basis_ideal_quotient(I->m[i],this,NULL);
2965  }
2966  for(i=0;i<I->idelems();i++)
2967  {
2968    I->m[i]=NULL;
2969  }
2970  idDelete(&I);
2971  add_later=idInit(ADD_LATER_SIZE,S->rank);
2972  memset(add_later->m,0,ADD_LATER_SIZE*sizeof(poly));
2973}
2974slimgb_alg::~slimgb_alg(){
2975
2976  if (!(completed)){
2977      poly* add=(poly*) omalloc((pair_top+2)*sizeof(poly));
2978      int piter;
2979      int pos=0;
2980      for(piter=0;piter<=pair_top;piter++){
2981        sorted_pair_node* s=apairs[piter];
2982        if (s->i<0){
2983            //delayed element
2984            if (s->lcm_of_lm!=NULL){
2985                add[pos]=s->lcm_of_lm;
2986                pos++;
2987
2988            }
2989        }
2990
2991        free_sorted_pair_node(s,r);
2992        apairs[piter]=NULL;
2993      }
2994      pair_top=-1;
2995      add[pos]=NULL;
2996      pos=0;
2997      while(add[pos]!=NULL){
2998        add_to_basis_ideal_quotient(add[pos],this,NULL);
2999        pos++;
3000      }
3001      for(piter=0;piter<=pair_top;piter++){
3002        sorted_pair_node* s=apairs[piter];
3003        assume(s->i>=0);
3004        free_sorted_pair_node(s,r);
3005        apairs[piter]=NULL;
3006      }
3007      pair_top=-1;
3008  }
3009  id_Delete(&add_later,r);
3010  int i,j;
3011  slimgb_alg* c=this;
3012  while(c->to_destroy)
3013  {
3014    pDelete(&(c->to_destroy->p));
3015    poly_list_node* old=c->to_destroy;
3016    c->to_destroy=c->to_destroy->next;
3017    omfree(old);
3018  }
3019  while(c->F)
3020  {
3021    for(i=0;i<c->F->size;i++){
3022      pDelete(&(c->F->mp[i].m));
3023    }
3024    omfree(c->F->mp);
3025    c->F->mp=NULL;
3026    mp_array_list* old=c->F;
3027    c->F=c->F->next;
3028    omfree(old);
3029  }
3030  while(c->F_minus)
3031  {
3032    for(i=0;i<c->F_minus->size;i++){
3033      pDelete(&(c->F_minus->p[i]));
3034    }
3035    omfree(c->F_minus->p);
3036    c->F_minus->p=NULL;
3037    poly_array_list* old=c->F_minus;
3038    c->F_minus=c->F_minus->next;
3039    omfree(old);
3040  }
3041  #ifndef HAVE_BOOST
3042  #ifndef USE_STDVECBOOL
3043  for(int z=1 /* zero length at 0 */;z<c->n;z++){
3044    omfree(c->states[z]);
3045  }
3046  omfree(c->states);
3047  #endif
3048  #endif
3049
3050  omfree(c->lengths);
3051  omfree(c->weighted_lengths);
3052  for(int z=0;z<c->n;z++)
3053  {
3054    pDelete(&c->tmp_pair_lm[z]);
3055    omfree(c->tmp_spn[z]);
3056  }
3057  omfree(c->tmp_pair_lm);
3058  omfree(c->tmp_spn);
3059
3060  omfree(c->T_deg);
3061  if(c->T_deg_full)
3062    omfree(c->T_deg_full);
3063
3064  omFree(c->strat->ecartS);
3065  omFree(c->strat->sevS);
3066//   initsevS(i);
3067  omFree(c->strat->S_2_R);
3068
3069
3070  omFree(c->strat->lenS);
3071
3072  if(c->strat->lenSw)  omFree(c->strat->lenSw);
3073
3074
3075
3076
3077  for(i=0;i<c->n;i++){
3078    if(c->gcd_of_terms[i])
3079      pDelete(&(c->gcd_of_terms[i]));
3080  }
3081  omfree(c->gcd_of_terms);
3082
3083  omfree(c->apairs);
3084  if (TEST_OPT_PROT)
3085  {
3086    //Print("calculated %d NFs\n",c->normal_forms);
3087      Print("\nNF:%i product criterion:%i, ext_product criterion:%i \n", c->normal_forms, c->easy_product_crit, c->extended_product_crit);
3088
3089
3090  }
3091  int deleted_form_c_s=0;
3092
3093  for(i=0;i<=c->strat->sl;i++){
3094    if (!c->strat->S[i]) continue;
3095    BOOLEAN found=FALSE;
3096    for(j=0;j<c->n;j++){
3097      if (c->S->m[j]==c->strat->S[i]){
3098        found=TRUE;
3099        break;
3100      }
3101    }
3102    if(!found) pDelete(&c->strat->S[i]);
3103  }
3104//   for(i=0;i<c->n;i++){
3105//     if (c->rep[i]!=i){
3106// //       for(j=0;j<=c->strat->sl;j++){
3107// //   if(c->strat->S[j]==c->S->m[i]){
3108// //     c->strat->S[j]=NULL;
3109// //     break;
3110// //   }
3111// //       }
3112// //      PrintS("R_delete");
3113//       pDelete(&c->S->m[i]);
3114//     }
3115//   }
3116
3117  if (completed){
3118  for(i=0;i<c->n;i++)
3119  {
3120    assume(c->S->m[i]!=NULL);
3121    if (p_GetComp(c->S->m[i],currRing)>this->syz_comp) continue;
3122    for(j=0;j<c->n;j++)
3123    {
3124      if((c->S->m[j]==NULL)||(i==j))
3125        continue;
3126      assume(p_LmShortDivisibleBy(c->S->m[j],c->short_Exps[j],
3127             c->S->m[i],~c->short_Exps[i],
3128             c->r)==p_LmDivisibleBy(c->S->m[j],
3129             c->S->m[i],
3130             c->r));
3131      if (p_LmShortDivisibleBy(c->S->m[j],c->short_Exps[j],
3132          c->S->m[i],~c->short_Exps[i],
3133          c->r))
3134      {
3135        pDelete(&c->S->m[i]);
3136        break;
3137      }
3138    }
3139  }
3140  }
3141  omfree(c->short_Exps);
3142
3143
3144  ideal I=c->S;
3145
3146  IDELEMS(I)=c->n;
3147
3148  idSkipZeroes(I);
3149  for(i=0;i<=c->strat->sl;i++)
3150    c->strat->S[i]=NULL;
3151  id_Delete(&c->strat->Shdl,c->r);
3152  pDelete(&c->tmp_lm);
3153  omUnGetSpecBin(&lm_bin);
3154  delete c->strat;
3155}
3156ideal t_rep_gb(ring r,ideal arg_I, int syz_comp, BOOLEAN F4_mode){
3157
3158  //  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)));
3159
3160  if (TEST_OPT_PROT)
3161    if (F4_mode)
3162      PrintS("F4 Modus \n");
3163
3164  //debug_Ideal=arg_debug_Ideal;
3165  //if (debug_Ideal) PrintS("DebugIdeal received\n");
3166  // Print("Idelems %i \n----------\n",IDELEMS(arg_I));
3167  ideal I=idCopy(arg_I);
3168  idCompactify(I);
3169  if (idIs0(I)) return I;
3170  int i;
3171  for(i=0;i<IDELEMS(I);i++)
3172  {
3173    assume(I->m[i]!=NULL);
3174    simplify_poly(I->m[i],currRing);
3175  }
3176
3177
3178  qsort(I->m,IDELEMS(I),sizeof(poly),poly_crit);
3179  //Print("Idelems %i \n----------\n",IDELEMS(I));
3180  //slimgb_alg* c=(slimgb_alg*) omalloc(sizeof(slimgb_alg));
3181  //int syz_comp=arg_I->rank;
3182  slimgb_alg* c=new slimgb_alg(I, syz_comp,F4_mode);
3183
3184
3185  while ((c->pair_top>=0) && ((!(TEST_OPT_DEGBOUND)) || (c->apairs[c->pair_top]->deg<=Kstd1_deg)))
3186  {
3187    #ifdef HAVE_F4
3188    if(F4_mode)
3189      go_on_F4(c);
3190
3191    else
3192    #endif
3193      go_on(c);
3194  }
3195  if (c->pair_top<0)
3196    c->completed=TRUE;
3197  I=c->S;
3198  delete c;
3199  if (TEST_OPT_REDSB){
3200    ideal erg=kInterRed(I,NULL);
3201    assume(I!=erg);
3202    id_Delete(&I, currRing);
3203    return erg;
3204  }
3205  //qsort(I->m, IDELEMS(I),sizeof(poly),pLmCmp_func);
3206  assume(I->rank>=idRankFreeModule(I));
3207  return(I);
3208}
3209void now_t_rep(const int & arg_i, const int & arg_j, slimgb_alg* c){
3210  int i,j;
3211  if (arg_i==arg_j){
3212    return;
3213  }
3214  if (arg_i>arg_j){
3215    i=arg_j;
3216    j=arg_i;
3217  } else {
3218    i=arg_i;
3219    j=arg_j;
3220  }
3221  c->states[j][i]=HASTREP;
3222}
3223
3224static BOOLEAN has_t_rep(const int & arg_i, const  int & arg_j, slimgb_alg* state){
3225  assume(0<=arg_i);
3226  assume(0<=arg_j);
3227  assume(arg_i<state->n);
3228  assume(arg_j<state->n);
3229  if (arg_i==arg_j)
3230  {
3231    return (TRUE);
3232  }
3233  if (arg_i>arg_j)
3234  {
3235    return (state->states[arg_i][arg_j]==HASTREP);
3236  } else
3237  {
3238    return (state->states[arg_j][arg_i]==HASTREP);
3239  }
3240}
3241static int pLcmDeg(poly a, poly b)
3242{
3243  int i;
3244  int n=0;
3245  for (i=pVariables; i; i--)
3246  {
3247    n+=si_max( pGetExp(a,i), pGetExp(b,i));
3248  }
3249  return n;
3250
3251}
3252
3253
3254
3255static void shorten_tails(slimgb_alg* c, poly monom)
3256{
3257  return;
3258// BOOLEAN corr=lenS_correct(c->strat);
3259  for(int i=0;i<c->n;i++)
3260  {
3261    //enter tail
3262
3263    if (c->S->m[i]==NULL) continue;
3264    poly tail=c->S->m[i]->next;
3265    poly prev=c->S->m[i];
3266    BOOLEAN did_something=FALSE;
3267    while((tail!=NULL)&& (pLmCmp(tail, monom)>=0))
3268    {
3269      if (p_LmDivisibleBy(monom,tail,c->r))
3270      {
3271        did_something=TRUE;
3272        prev->next=tail->next;
3273        tail->next=NULL;
3274        p_Delete(& tail,c->r);
3275        tail=prev;
3276        //PrintS("Shortened");
3277        c->lengths[i]--;
3278      }
3279      prev=tail;
3280      tail=tail->next;
3281    }
3282    if (did_something)
3283    {
3284      int new_pos;
3285      wlen_type q;
3286      q=pQuality(c->S->m[i],c,c->lengths[i]);
3287      new_pos=simple_posInS(c->strat,c->S->m[i],c->lengths[i],q);
3288
3289      int old_pos=-1;
3290      //assume new_pos<old_pos
3291      for (int z=0;z<=c->strat->sl;z++)
3292      {
3293        if (c->strat->S[z]==c->S->m[i])
3294        {
3295          old_pos=z;
3296          break;
3297        }
3298      }
3299      if (old_pos== -1)
3300        for (int z=new_pos-1;z>=0;z--)
3301        {
3302          if (c->strat->S[z]==c->S->m[i])
3303          {
3304            old_pos=z;
3305            break;
3306          }
3307        }
3308      assume(old_pos>=0);
3309      assume(new_pos<=old_pos);
3310      assume(pLength(c->strat->S[old_pos])==c->lengths[i]);
3311      c->strat->lenS[old_pos]=c->lengths[i];
3312      if (c->strat->lenSw)
3313        c->strat->lenSw[old_pos]=q;
3314
3315      if (new_pos<old_pos)
3316        move_forward_in_S(old_pos,new_pos,c->strat);
3317
3318      length_one_crit(c,i,c->lengths[i]);
3319    }
3320  }
3321}
3322static sorted_pair_node* pop_pair(slimgb_alg* c){
3323  clean_top_of_pair_list(c);
3324
3325  if(c->pair_top<0) return NULL;
3326  else return (c->apairs[c->pair_top--]);
3327}
3328void slimgb_alg::cleanDegs(int lower, int upper){
3329  assume(is_homog);
3330  int deg;
3331  if (TEST_OPT_PROT){
3332    PrintS("C");
3333  }
3334  for(deg=lower;deg<=upper;deg++){
3335    int i;
3336    for(i=0;i<n;i++){
3337      if (T_deg[i]==deg){
3338          poly h;
3339          h=S->m[i];
3340          h=redNFTail(h,strat->sl,strat,lengths[i]);
3341          if (!rField_is_Zp(r))
3342          {
3343            pCleardenom(h);
3344            pContent(h);
3345           
3346          } else pNorm(h);
3347          //TODO:GCD of TERMS
3348          poly got=::gcd_of_terms(h,r);
3349          p_Delete(&gcd_of_terms[i],r);
3350          gcd_of_terms[i]=got;
3351          int len=pLength(h);
3352          wlen_type wlen=pQuality(h,this,len);
3353          if (weighted_lengths)
3354            weighted_lengths[i]=wlen;
3355          lengths[i]=len;
3356          assume(h==S->m[i]);
3357          int j;
3358          for(j=0;j<=strat->sl;j++){
3359            if (h==strat->S[j]){
3360              int new_pos=simple_posInS(strat, h,len, wlen);
3361              if (strat->lenS){
3362                strat->lenS[j]=len;
3363              }
3364              if (strat->lenSw){
3365                strat->lenSw[j]=wlen;
3366              }
3367              if (new_pos<j){
3368                move_forward_in_S(j,new_pos,strat);
3369              }else{
3370                if (new_pos>j)
3371                 new_pos=new_pos-1;//is identical with one element
3372                if (new_pos>j)
3373                  move_backward_in_S(j,new_pos,strat);
3374              }
3375              break;
3376            }
3377          }
3378        }
3379       
3380    }
3381 
3382  }
3383  {
3384    int i,j;
3385    for(i=0;i<this->n;i++){
3386      for(j=0;j<i;j++){
3387        if (T_deg[i]+T_deg[j]<=upper){
3388          now_t_rep(i,j,this);
3389        }
3390      }
3391    }
3392  }
3393  //TODO resort and update strat->S,strat->lenSw
3394  //TODO mark pairs
3395}
3396sorted_pair_node* top_pair(slimgb_alg* c){
3397  while(c->pair_top>=0){
3398    super_clean_top_of_pair_list(c);//yeah, I know, it's odd that I use a different proc here
3399    if ((c->is_homog)&&(c->pair_top>=0)&&(c->apairs[c->pair_top]->deg>=c->lastCleanedDeg+2)){
3400      int upper=c->apairs[c->pair_top]->deg-1;
3401      c->cleanDegs(c->lastCleanedDeg+1,upper);
3402      c->lastCleanedDeg=upper;
3403    } else{
3404      break;
3405    }
3406   
3407  }
3408
3409
3410  if(c->pair_top<0) return NULL;
3411  else return (c->apairs[c->pair_top]);
3412}
3413sorted_pair_node* quick_pop_pair(slimgb_alg* c){
3414  if(c->pair_top<0) return NULL;
3415  else return (c->apairs[c->pair_top--]);
3416}
3417
3418
3419
3420static void super_clean_top_of_pair_list(slimgb_alg* c){
3421  while((c->pair_top>=0)
3422  && (c->apairs[c->pair_top]->i>=0)
3423  && (good_has_t_rep(c->apairs[c->pair_top]->j, c->apairs[c->pair_top]->i,c)))
3424  {
3425
3426    free_sorted_pair_node(c->apairs[c->pair_top],c->r);
3427    c->pair_top--;
3428
3429  }
3430}
3431void clean_top_of_pair_list(slimgb_alg* c){
3432  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))){
3433
3434    free_sorted_pair_node(c->apairs[c->pair_top],c->r);
3435    c->pair_top--;
3436
3437  }
3438}
3439static BOOLEAN state_is(calc_state state, const int & arg_i, const  int & arg_j, slimgb_alg* c){
3440  assume(0<=arg_i);
3441  assume(0<=arg_j);
3442  assume(arg_i<c->n);
3443  assume(arg_j<c->n);
3444  if (arg_i==arg_j)
3445  {
3446    return (TRUE);
3447  }
3448  if (arg_i>arg_j)
3449  {
3450    return (c->states[arg_i][arg_j]==state);
3451  }
3452  else return(c->states[arg_j][arg_i]==state);
3453}
3454
3455
3456void free_sorted_pair_node(sorted_pair_node* s, ring r){
3457  if (s->i>=0)
3458    p_Delete(&s->lcm_of_lm,r);
3459  omfree(s);
3460}
3461static BOOLEAN pair_better(sorted_pair_node* a,sorted_pair_node* b, slimgb_alg* c){
3462  if (a->deg<b->deg) return TRUE;
3463  if (a->deg>b->deg) return FALSE;
3464
3465
3466  int comp=pLmCmp(a->lcm_of_lm, b->lcm_of_lm);
3467  if (comp==1) return FALSE;
3468  if (-1==comp) return TRUE;
3469  if (a->expected_length<b->expected_length) return TRUE;
3470  if (a->expected_length>b->expected_length) return FALSE;
3471  if (a->i+a->j<b->i+b->j) return TRUE;
3472   if (a->i+a->j>b->i+b->j) return FALSE;
3473  if (a->i<b->i) return TRUE;
3474  if (a->i>b->i) return FALSE;
3475  return TRUE;
3476}
3477
3478static int tgb_pair_better_gen(const void* ap,const void* bp){
3479
3480  sorted_pair_node* a=*((sorted_pair_node**)ap);
3481  sorted_pair_node* b=*((sorted_pair_node**)bp);
3482  assume((a->i>a->j) || (a->i < 0));
3483  assume((b->i>b->j) || (b->i < 0));
3484  if (a->deg<b->deg) return -1;
3485  if (a->deg>b->deg) return 1;
3486
3487
3488
3489 int comp=pLmCmp(a->lcm_of_lm, b->lcm_of_lm);
3490
3491  if (comp==1) return 1;
3492  if (-1==comp) return -1;
3493   if (a->expected_length<b->expected_length) return -1;
3494  if (a->expected_length>b->expected_length) return 1;
3495  if (a->i+a->j<b->i+b->j) return -1;
3496   if (a->i+a->j>b->i+b->j) return 1;
3497  if (a->i<b->i) return -1;
3498   if (a->i>b->i) return 1;
3499  return 0;
3500}
3501
3502
3503static poly gcd_of_terms(poly p, ring r){
3504  int max_g_0=0;
3505  assume(p!=NULL);
3506  int i;
3507  poly m=pOne();
3508  poly t;
3509  for (i=pVariables; i; i--)
3510  {
3511      pSetExp(m,i, pGetExp(p,i));
3512      if (max_g_0==0)
3513  if (pGetExp(m,i)>0)
3514    max_g_0=i;
3515  }
3516
3517  t=p->next;
3518  while (t!=NULL){
3519
3520    if (max_g_0==0) break;
3521    for (i=max_g_0; i; i--)
3522    {
3523      pSetExp(m,i, si_min(pGetExp(t,i),pGetExp(m,i)));
3524      if (max_g_0==i)
3525  if (pGetExp(m,i)==0)
3526    max_g_0=0;
3527      if ((max_g_0==0) && (pGetExp(m,i)>0)){
3528  max_g_0=i;
3529      }
3530    }
3531    t=t->next;
3532  }
3533
3534  if (max_g_0>0)
3535    return m;
3536  pDelete(&m);
3537  return NULL;
3538}
3539static inline BOOLEAN pHasNotCFExtended(poly p1, poly p2, poly m)
3540{
3541
3542  if (pGetComp(p1) > 0 || pGetComp(p2) > 0)
3543    return FALSE;
3544  int i = 1;
3545  loop
3546  {
3547    if ((pGetExp(p1, i)-pGetExp(m,i) >0) && (pGetExp(p2, i) -pGetExp(m,i)> 0))   return FALSE;
3548    if (i == pVariables)                                return TRUE;
3549    i++;
3550  }
3551}
3552
3553
3554//for impl reasons may return false if the the normal product criterion matches
3555static inline BOOLEAN extended_product_criterion(poly p1, poly gcd1, poly p2, poly gcd2, slimgb_alg* c){
3556  if (c->nc)
3557    return FALSE;
3558  if(gcd1==NULL) return FALSE;
3559        if(gcd2==NULL) return FALSE;
3560        gcd1->next=gcd2; //may ordered incorrect
3561        poly m=gcd_of_terms(gcd1,c->r);
3562        gcd1->next=NULL;
3563        if (m==NULL) return FALSE;
3564
3565        BOOLEAN erg=pHasNotCFExtended(p1,p2,m);
3566        pDelete(&m);
3567        return erg;
3568}
3569static poly kBucketGcd(kBucket* b, ring r)
3570{
3571  int s=0;
3572  int i;
3573  poly m, n;
3574  BOOLEAN initialized=FALSE;
3575  for (i=MAX_BUCKET-1;i>=0;i--)
3576  {
3577    if (b->buckets[i]!=NULL){
3578      if (!initialized){
3579  m=gcd_of_terms(b->buckets[i],r);
3580  initialized=TRUE;
3581  if (m==NULL) return NULL;
3582      }
3583      else
3584  {
3585    n=gcd_of_terms(b->buckets[i],r);
3586    if (n==NULL) {
3587      pDelete(&m);
3588      return NULL;
3589    }
3590    n->next=m;
3591    poly t=gcd_of_terms(n,r);
3592    n->next=NULL;
3593    pDelete(&m);
3594    pDelete(&n);
3595    m=t;
3596    if (m==NULL) return NULL;
3597
3598  }
3599    }
3600  }
3601  return m;
3602}
3603
3604
3605
3606
3607static inline wlen_type quality_of_pos_in_strat_S(int pos, slimgb_alg* c){
3608  if (c->strat->lenSw!=NULL) return c->strat->lenSw[pos];
3609  return c->strat->lenS[pos];
3610}
3611#ifdef HAVE_PLURAL
3612static inline wlen_type quality_of_pos_in_strat_S_mult_high(int pos, poly high, slimgb_alg* c)
3613  //meant only for nc
3614{
3615  poly m=pOne();
3616  pExpVectorDiff(m,high ,c->strat->S[pos]);
3617  poly product = nc_mm_Mult_pp(m, c->strat->S[pos], c->r);
3618  wlen_type erg=pQuality(product,c);
3619  pDelete(&m);
3620  pDelete(&product);
3621  return erg;
3622}
3623#endif
3624
3625static void multi_reduction_lls_trick(red_object* los, int losl,slimgb_alg* c,find_erg & erg){
3626  erg.expand=NULL;
3627  BOOLEAN swap_roles; //from reduce_by, to_reduce_u if fromS
3628  if(erg.fromS){
3629    if(pLmEqual(c->strat->S[erg.reduce_by],los[erg.to_reduce_u].p))
3630    {
3631      int i;
3632      wlen_type quality_a=quality_of_pos_in_strat_S(erg.reduce_by,c);
3633      int best=erg.to_reduce_u+1;
3634/*
3635      for (i=erg.to_reduce_u;i>=erg.to_reduce_l;i--){
3636  int qc=los[i].guess_quality(c);
3637  if (qc<quality_a){
3638    best=i;
3639    quality_a=qc;
3640  }
3641      }
3642      if(best!=erg.to_reduce_u+1){*/
3643      wlen_type qc;
3644      best=find_best(los,erg.to_reduce_l,erg.to_reduce_u,qc,c);
3645      if(qc<quality_a){
3646  los[best].flatten();
3647  int b_pos=kBucketCanonicalize(los[best].bucket);
3648  los[best].p=los[best].bucket->buckets[b_pos];
3649  qc=pQuality(los[best].bucket->buckets[b_pos],c);
3650  if(qc<quality_a){
3651    red_object h=los[erg.to_reduce_u];
3652    los[erg.to_reduce_u]=los[best];
3653    los[best]=h;
3654    swap_roles=TRUE;
3655  }
3656  else
3657    swap_roles=FALSE;
3658      }
3659      else{
3660
3661  swap_roles=FALSE;
3662      }
3663
3664    }
3665      else
3666    {
3667      if (erg.to_reduce_u>erg.to_reduce_l){
3668
3669  int i;
3670  wlen_type quality_a=quality_of_pos_in_strat_S(erg.reduce_by,c);
3671  #ifdef HAVE_PLURAL
3672  if ((c->nc) && (!(rIsSCA(c->r))))
3673    quality_a=quality_of_pos_in_strat_S_mult_high(erg.reduce_by, los[erg.to_reduce_u].p, c);
3674  #endif
3675  int best=erg.to_reduce_u+1;
3676  wlen_type qc;
3677  best=find_best(los,erg.to_reduce_l,erg.to_reduce_u,qc,c);
3678  assume(qc==los[best].guess_quality(c));
3679  if(qc<quality_a){
3680    los[best].flatten();
3681    int b_pos=kBucketCanonicalize(los[best].bucket);
3682    los[best].p=los[best].bucket->buckets[b_pos];
3683    qc==pQuality(los[best].bucket->buckets[b_pos],c);
3684    //(best!=erg.to_reduce_u+1)
3685    if(qc<quality_a){
3686    red_object h=los[erg.to_reduce_u];
3687    los[erg.to_reduce_u]=los[best];
3688    los[best]=h;
3689    erg.reduce_by=erg.to_reduce_u;
3690    erg.fromS=FALSE;
3691    erg.to_reduce_u--;
3692    }
3693  }
3694      }
3695      else
3696      {
3697  assume(erg.to_reduce_u==erg.to_reduce_l);
3698  wlen_type quality_a=
3699        quality_of_pos_in_strat_S(erg.reduce_by,c);
3700  wlen_type qc=los[erg.to_reduce_u].guess_quality(c);
3701  if (qc<0) PrintS("Wrong wlen_type");
3702  if(qc<quality_a){
3703    int best=erg.to_reduce_u;
3704    los[best].flatten();
3705    int b_pos=kBucketCanonicalize(los[best].bucket);
3706    los[best].p=los[best].bucket->buckets[b_pos];
3707    qc=pQuality(los[best].bucket->buckets[b_pos],c);
3708    assume(qc>=0);
3709    if(qc<quality_a){
3710      BOOLEAN exp=FALSE;
3711      if(qc<=2){
3712         //Print("\n qc is %lld \n",qc);
3713         exp=TRUE;
3714      }
3715
3716      else {
3717         if (qc<quality_a/2)
3718          exp=TRUE;
3719         else
3720       if(erg.reduce_by<c->n/4)
3721          exp=TRUE;
3722      }
3723      if (exp){
3724        poly clear_into;
3725        los[erg.to_reduce_u].flatten();
3726        kBucketClear(los[erg.to_reduce_u].bucket,&clear_into,&erg.expand_length);
3727        erg.expand=pCopy(clear_into);
3728        kBucketInit(los[erg.to_reduce_u].bucket,clear_into,erg.expand_length);
3729        if (TEST_OPT_PROT)
3730    PrintS("e");
3731
3732      }
3733    }
3734  }
3735
3736
3737      }
3738
3739      swap_roles=FALSE;
3740      return;
3741      }
3742
3743  }
3744  else{
3745    if(erg.reduce_by>erg.to_reduce_u){
3746      //then lm(rb)>= lm(tru) so =
3747      assume(erg.reduce_by==erg.to_reduce_u+1);
3748      int best=erg.reduce_by;
3749      wlen_type quality_a=los[erg.reduce_by].guess_quality(c);
3750      wlen_type qc;
3751      best=find_best(los,erg.to_reduce_l,erg.to_reduce_u,qc,c);
3752
3753      int i;
3754      if(qc<quality_a){
3755    red_object h=los[erg.reduce_by];
3756    los[erg.reduce_by]=los[best];
3757    los[best]=h;
3758  }
3759  swap_roles=FALSE;
3760  return;
3761
3762
3763    }
3764    else
3765    {
3766      assume(!pLmEqual(los[erg.reduce_by].p,los[erg.to_reduce_l].p));
3767      assume(erg.to_reduce_u==erg.to_reduce_l);
3768      //further assume, that reduce_by is the above all other polys
3769      //with same leading term
3770      int il=erg.reduce_by;
3771      wlen_type quality_a =los[erg.reduce_by].guess_quality(c);
3772      wlen_type qc;
3773      while((il>0) && pLmEqual(los[il-1].p,los[il].p)){
3774  il--;
3775  qc=los[il].guess_quality(c);
3776  if (qc<quality_a){
3777    quality_a=qc;
3778    erg.reduce_by=il;
3779  }
3780      }
3781      swap_roles=FALSE;
3782    }
3783
3784  }
3785  if(swap_roles)
3786  {
3787    if (TEST_OPT_PROT)
3788      PrintS("b");
3789    poly clear_into;
3790    int dummy_len;
3791    int new_length;
3792    int bp=erg.to_reduce_u;//bucket_positon
3793    //kBucketClear(los[bp].bucket,&clear_into,&new_length);
3794    new_length=los[bp].clear_to_poly();
3795    clear_into=los[bp].p;
3796    poly p=c->strat->S[erg.reduce_by];
3797    int j=erg.reduce_by;
3798    int old_length=c->strat->lenS[j];// in view of S
3799    los[bp].p=p;
3800    if (c->eliminationProblem){
3801        los[bp].sugar=pTotaldegree_full(p);
3802    }
3803    kBucketInit(los[bp].bucket,p,old_length);
3804    wlen_type qal=pQuality(clear_into,c,new_length);
3805    int pos_in_c=-1;
3806    int z;
3807    int new_pos;
3808    new_pos=simple_posInS(c->strat,clear_into,new_length, qal);
3809    assume(new_pos<=j);
3810    for (z=c->n;z;z--)
3811    {
3812      if(p==c->S->m[z-1])
3813      {
3814  pos_in_c=z-1;
3815  break;
3816      }
3817    }
3818
3819    int tdeg_full=-1;
3820    int tdeg=-1;
3821    if(pos_in_c>=0)
3822    {
3823      #ifdef TGB_RESORT_PAIRS
3824      c->used_b=TRUE;
3825      c->replaced[pos_in_c]=TRUE;
3826      #endif
3827      tdeg=c->T_deg[pos_in_c];
3828      c->S->m[pos_in_c]=clear_into;
3829      c->lengths[pos_in_c]=new_length;
3830      c->weighted_lengths[pos_in_c]=qal;
3831      if (c->gcd_of_terms[pos_in_c]==NULL)
3832        c->gcd_of_terms[pos_in_c]=gcd_of_terms(clear_into,c->r);
3833      if (c->T_deg_full)
3834        tdeg_full=c->T_deg_full[pos_in_c]=pTotaldegree_full(clear_into);
3835      else tdeg_full=tdeg;
3836      c_S_element_changed_hook(pos_in_c,c);
3837    } else {
3838      if (c->eliminationProblem){
3839        tdeg_full=pTotaldegree_full(clear_into);
3840        tdeg=pTotaldegree(clear_into);
3841      }
3842    }
3843    c->strat->S[j]=clear_into;
3844    c->strat->lenS[j]=new_length;
3845
3846    assume(pLength(clear_into)==new_length);
3847    if(c->strat->lenSw!=NULL)
3848      c->strat->lenSw[j]=qal;
3849    if (!rField_is_Zp(c->r))
3850    {
3851      pCleardenom(clear_into);//should be unnecessary
3852      pContent(clear_into);
3853     
3854    }
3855    else
3856      pNorm(clear_into);
3857#ifdef FIND_DETERMINISTIC
3858    erg.reduce_by=j;
3859    //resort later see diploma thesis, find_in_S must be deterministic
3860    //during multireduction if spolys are only in the span of the
3861    //input polys
3862#else
3863
3864    if (new_pos<j)
3865    {
3866      if (c->strat->honey) c->strat->ecartS[j]=tdeg_full-tdeg;
3867      move_forward_in_S(j,new_pos,c->strat);
3868      erg.reduce_by=new_pos;
3869    }
3870#endif
3871  }
3872}
3873static int fwbw(red_object* los, int i){
3874   int i2=i;
3875   int step=1;
3876
3877   BOOLEAN bw=FALSE;
3878   BOOLEAN incr=TRUE;
3879
3880   while(1)
3881   {
3882     if(!bw)
3883     {
3884       step=si_min(i2,step);
3885       if (step==0) break;
3886       i2-=step;
3887
3888       if(!pLmEqual(los[i].p,los[i2].p))
3889       {
3890   bw=TRUE;
3891   incr=FALSE;
3892       }
3893       else
3894       {
3895   if ((!incr) &&(step==1)) break;
3896       }
3897
3898
3899     }
3900     else
3901     {
3902
3903       step=si_min(i-i2,step);
3904       if (step==0) break;
3905       i2+=step;
3906       if(pLmEqual(los[i].p,los[i2].p)){
3907   if(step==1) break;
3908   else
3909   {
3910     bw=FALSE;
3911   }
3912       }
3913
3914     }
3915     if (incr)
3916       step*=2;
3917     else
3918     {
3919       if (step%2==1)
3920   step=(step+1)/2;
3921       else
3922   step/=2;
3923
3924     }
3925   }
3926   return i2;
3927}
3928static void canonicalize_region(red_object* los, int l, int u,slimgb_alg* c){
3929    assume(l<=u+1);
3930    int i;
3931    for(i=l;i<=u;i++){
3932        kBucketCanonicalize(los[i].bucket);
3933    }
3934
3935}
3936static void multi_reduction_find(red_object* los, int losl,slimgb_alg* c,int startf,find_erg & erg){
3937  kStrategy strat=c->strat;
3938
3939  assume(startf<=losl);
3940  assume((startf==losl-1)||(pLmCmp(los[startf].p,los[startf+1].p)==-1));
3941  int i=startf;
3942
3943  int j;
3944  while(i>=0){
3945    assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)<=0));
3946    assume(is_valid_ro(los[i]));
3947    assume((!(c->eliminationProblem))||(los[i].sugar>=pTotaldegree(los[i].p)));
3948    j=kFindDivisibleByInS_easy(strat,los[i]);
3949    if(j>=0){
3950
3951      erg.to_reduce_u=i;
3952      erg.reduce_by=j;
3953      erg.fromS=TRUE;
3954      int i2=fwbw(los,i);
3955      assume(pLmEqual(los[i].p,los[i2].p));
3956      assume((i2==0)||(!pLmEqual(los[i2].p,los[i2-1].p)));
3957      assume(i>=i2);
3958
3959
3960      erg.to_reduce_l=i2;
3961      assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)==-1));
3962      canonicalize_region(los,erg.to_reduce_u+1,startf,c);
3963      return;
3964    }
3965    if (j<0){
3966
3967      //not reduceable, try to use this for reducing higher terms
3968      int i2=fwbw(los,i);
3969      assume(pLmEqual(los[i].p,los[i2].p));
3970      assume((i2==0)||(!pLmEqual(los[i2].p,los[i2-1].p)));
3971      assume(i>=i2);
3972      if(i2!=i){
3973
3974
3975  erg.to_reduce_u=i-1;
3976  erg.to_reduce_l=i2;
3977  erg.reduce_by=i;
3978  erg.fromS=FALSE;
3979  assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)==-1));
3980  canonicalize_region(los,erg.to_reduce_u+1,startf,c);
3981  return;
3982      }
3983
3984      i--;
3985    }
3986  }
3987  erg.reduce_by=-1;//error code
3988  return;
3989}
3990
3991 //  nicht reduzierbare eintraege in ergebnisliste schreiben
3992//   nullen loeschen
3993//   while(finde_groessten leitterm reduzierbar(c,erg)){
3994
3995static int multi_reduction_clear_zeroes(red_object* los, int  losl, int l, int u)
3996{
3997
3998
3999  int deleted=0;
4000  int  i=l;
4001  int last=-1;
4002  while(i<=u)
4003  {
4004
4005    if(los[i].p==NULL){
4006      kBucketDestroy(&los[i].bucket);
4007//      delete los[i];//here we assume los are constructed with new
4008      //destroy resources, must be added here
4009     if (last>=0)
4010     {
4011       memmove(los+(int)(last+1-deleted),los+(last+1),sizeof(red_object)*(i-1-last));
4012     }
4013     last=i;
4014     deleted++;
4015    }
4016    i++;
4017  }
4018  if((last>=0)&&(last!=losl-1))
4019      memmove(los+(int)(last+1-deleted),los+last+1,sizeof(red_object)*(losl-1-last));
4020  return deleted;
4021
4022}
4023int search_red_object_pos(red_object* a, int top, red_object* key ){
4024   
4025    int an = 0;
4026    int en= top;
4027    if (top==-1) return 0;
4028    if (pLmCmp(key->p,a[top].p)==1)
4029      return top+1;
4030    int i;
4031    loop
4032    {
4033      if (an >= en-1)
4034      {
4035        if (pLmCmp(key->p,a[an].p)==-1)
4036           return an;
4037        return en;
4038      }
4039      i=(an+en) / 2;
4040      if (pLmCmp(key->p,a[i].p)==-1)
4041        en=i;
4042      else
4043        an=i;
4044    }
4045
4046}
4047static void sort_region_down(red_object* los, int l, int u, slimgb_alg* c)
4048{
4049  int r_size=u-l+1;
4050  qsort(los+l,r_size,sizeof(red_object),red_object_better_gen);
4051  int i;
4052  int * new_indices=(int*) omalloc((r_size)*sizeof(int));
4053  int bound=0;
4054  BOOLEAN at_end=FALSE;
4055  for(i=l;i<=u;i++){
4056    if (!(at_end)){
4057      bound=new_indices[i-l]=bound+search_red_object_pos(los+bound,l-bound-1,los+i);
4058      if (bound==l) at_end=TRUE;
4059    }
4060    else{
4061      new_indices[i-l]=l;
4062    }
4063  }
4064  red_object* los_region=(red_object*) omalloc(sizeof(red_object)*(u-l+1));
4065  for (int i=0;i<r_size;i++){
4066    new_indices[i]+=i;
4067    los_region[i]=los[l+i];
4068    assume((i==0)||(new_indices[i]>new_indices[i-1]));
4069   
4070  }
4071
4072  i=r_size-1;
4073  int j=u;
4074  int j2=l-1;
4075  while(i>=0){
4076    if (new_indices[i]==j){
4077      los[j]=los_region[i];
4078      i--;
4079      j--;
4080    } else{
4081      assume(new_indices[i]<j);
4082      los[j]=los[j2];
4083      assume(j2>=0);
4084      j2--;
4085      j--;
4086    }
4087  }
4088  omfree(los_region);
4089 
4090  omfree(new_indices);
4091
4092}
4093
4094//assume that los is ordered ascending by leading term, all non zero
4095static void multi_reduction(red_object* los, int & losl, slimgb_alg* c)
4096{
4097  poly* delay=(poly*) omalloc(losl*sizeof(poly));
4098  int delay_s=0;
4099  //initialize;
4100  assume(c->strat->sl>=0);
4101  assume(losl>0);
4102  int i;
4103  wlen_type max_initial_quality=0;
4104
4105  for(i=0;i<losl;i++){
4106    los[i].sev=pGetShortExpVector(los[i].p);
4107//SetShortExpVector();
4108    los[i].p=kBucketGetLm(los[i].bucket);
4109    if (los[i].initial_quality>max_initial_quality)
4110        max_initial_quality=los[i].initial_quality;
4111    // else
4112//         Print("init2_qal=%lld;", los[i].initial_quality);
4113//     Print("initial_quality=%lld;",max_initial_quality);
4114  }
4115
4116  kStrategy strat=c->strat;
4117  int curr_pos=losl-1;
4118
4119
4120//  nicht reduzierbare eintrï¿œe in ergebnisliste schreiben
4121  // nullen loeschen
4122  while(curr_pos>=0){
4123
4124    find_erg erg;
4125    multi_reduction_find(los, losl,c,curr_pos,erg);//last argument should be curr_pos
4126    if(erg.reduce_by<0) break;
4127
4128
4129
4130    erg.expand=NULL;
4131    int d=erg.to_reduce_u-erg.to_reduce_l+1;
4132
4133
4134    multi_reduction_lls_trick(los,losl,c,erg);
4135
4136
4137    int i;
4138    int len;
4139    //    wrp(los[erg.to_reduce_u].p);
4140    //Print("\n");
4141    multi_reduce_step(erg,los,c);
4142
4143
4144    if(!K_TEST_OPT_REDTHROUGH){
4145  for(i=erg.to_reduce_l;i<=erg.to_reduce_u;i++){
4146     if  (los[i].p!=NULL)  //the check (los[i].p!=NULL) might be invalid
4147     {
4148         //
4149         assume(los[i].initial_quality>0);
4150
4151               if(los[i].guess_quality(c)
4152                  >1.5*delay_factor*max_initial_quality){
4153                       if (TEST_OPT_PROT)
4154                           PrintS("v");
4155                       los[i].canonicalize();
4156                       if(los[i].guess_quality(c)
4157                           >delay_factor*max_initial_quality){
4158                               if (TEST_OPT_PROT)
4159                                   PrintS(".");
4160                               los[i].clear_to_poly();
4161                               //delay.push_back(los[i].p);
4162                               delay[delay_s]=los[i].p;
4163                               delay_s++;
4164
4165                               los[i].p=NULL;
4166
4167                      }
4168                  }
4169
4170            }
4171     }
4172  }
4173    int deleted=multi_reduction_clear_zeroes(los, losl, erg.to_reduce_l, erg.to_reduce_u);
4174    if(erg.fromS==FALSE)
4175      curr_pos=si_max(erg.to_reduce_u,erg.reduce_by);
4176    else
4177      curr_pos=erg.to_reduce_u;
4178    losl -= deleted;
4179    curr_pos -= deleted;
4180
4181    //Print("deleted %i \n",deleted);
4182    if ((TEST_V_UPTORADICAL) &&(!(erg.fromS)))
4183        sort_region_down(los,si_min(erg.to_reduce_l,erg.reduce_by),(si_max(erg.to_reduce_u,erg.reduce_by))-deleted,c);
4184    else
4185    sort_region_down(los, erg.to_reduce_l, erg.to_reduce_u-deleted, c);
4186
4187
4188    if(erg.expand)
4189    {
4190#ifdef FIND_DETERMINISTIC
4191      int i;
4192      for(i=0;c->expandS[i];i++);
4193      c->expandS=(poly*) omrealloc(c->expandS,(i+2)*sizeof(poly));
4194      c->expandS[i]=erg.expand;
4195      c->expandS[i+1]=NULL;
4196#else
4197      int ecart=0;
4198      if (c->eliminationProblem){
4199        ecart=pTotaldegree_full(erg.expand)-pTotaldegree(erg.expand);
4200      }
4201      add_to_reductors(c,erg.expand,erg.expand_length,ecart);
4202#endif
4203    }
4204
4205  }
4206
4207
4208  //sorted_pair_node** pairs=(sorted_pair_node**)
4209  //  omalloc(delay_s*sizeof(sorted_pair_node*));
4210  c->introduceDelayedPairs(delay,delay_s);
4211  /*
4212  for(i=0;i<delay_s;i++){
4213
4214      poly p=delay[i];
4215      //if (rPar(c->r)==0)
4216      simplify_poly(p,c->r);
4217      sorted_pair_node* si=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
4218      si->i=-1;
4219      si->j=-1;
4220       if (!rField_is_Zp(c->r)){
4221        if (!c->nc)
4222            p=redTailShort(p, c->strat);
4223        pCleardenom(p);
4224        pContent(p);
4225      }
4226      si->expected_length=pQuality(p,c,pLength(p));
4227      si->deg=pTotaldegree(p);
4228
4229      si->lcm_of_lm=p;
4230      pairs[i]=si;
4231  }
4232  qsort(pairs,delay_s,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
4233  c->apairs=spn_merge(c->apairs,c->pair_top+1,pairs,delay_s,c);
4234  c->pair_top+=delay_s;*/
4235  omfree(delay);
4236  //omfree(pairs);
4237  return;
4238}
4239void red_object::flatten(){
4240  assume(p==kBucketGetLm(bucket));
4241}
4242void red_object::validate(){
4243  p=kBucketGetLm(bucket);
4244  if(p)
4245    sev=pGetShortExpVector(p);
4246}
4247int red_object::clear_to_poly(){
4248  flatten();
4249  int l;
4250  kBucketClear(bucket,&p,&l);
4251  return l;
4252}
4253
4254
4255
4256
4257
4258void reduction_step::reduce(red_object* r, int l, int u){}
4259void simple_reducer::do_reduce(red_object & ro)
4260{
4261  number coef;
4262#ifdef HAVE_PLURAL
4263  if (c->nc)
4264    nc_BucketPolyRed_Z(ro.bucket, p, &coef);
4265  else
4266#endif
4267    coef=kBucketPolyRed(ro.bucket,p,
4268       p_len,
4269       c->strat->kNoether);
4270  nDelete(&coef);
4271}
4272
4273
4274void simple_reducer::reduce(red_object* r, int l, int u){
4275  this->pre_reduce(r,l,u);
4276  int i;
4277//debug start
4278  int im;
4279
4280
4281  if(c->eliminationProblem){
4282    assume(p_LmEqual(r[l].p,r[u].p,c->r));
4283    /*int lm_deg=pTotaldegree(r[l].p);
4284    reducer_deg=lm_deg+pTotaldegree_full(p)-pTotaldegree(p);*/
4285  }
4286
4287  for(i=l;i<=u;i++){
4288
4289
4290
4291    this->do_reduce(r[i]);
4292    if (c->eliminationProblem){
4293        r[i].sugar=si_max(r[i].sugar,reducer_deg);
4294    }
4295  }
4296  for(i=l;i<=u;i++){
4297
4298    kBucketSimpleContent(r[i].bucket);
4299    r[i].validate();
4300    #ifdef TGB_DEBUG
4301    #endif
4302  }
4303}
4304reduction_step::~reduction_step(){}
4305simple_reducer::~simple_reducer(){
4306  if(fill_back!=NULL)
4307  {
4308    kBucketInit(fill_back,p,p_len);
4309  }
4310  fill_back=NULL;
4311
4312}
4313
4314void multi_reduce_step(find_erg & erg, red_object* r, slimgb_alg* c){
4315  static int id=0;
4316  id++;
4317  unsigned long sev;
4318    BOOLEAN lt_changed=FALSE;
4319  int rn=erg.reduce_by;
4320  poly red;
4321  int red_len;
4322  simple_reducer* pointer;
4323  BOOLEAN work_on_copy=FALSE;
4324  if(erg.fromS){
4325    red=c->strat->S[rn];
4326    red_len=c->strat->lenS[rn];
4327    assume(red_len==pLength(red));
4328  }
4329  else
4330  {
4331    r[rn].flatten();
4332    kBucketClear(r[rn].bucket,&red,&red_len);
4333
4334    if (!rField_is_Zp(c->r))
4335    {
4336      pCleardenom(red);//should be unnecessary
4337      pContent(red);
4338     
4339
4340    }
4341    pNormalize(red);
4342    if (c->eliminationProblem){
4343        r[rn].sugar=pTotaldegree_full(red);
4344    }
4345
4346    if ((!(erg.fromS))&&(TEST_V_UPTORADICAL)){
4347
4348         if (polynomial_root(red,c->r))
4349            lt_changed=TRUE;
4350            sev=p_GetShortExpVector(red,c->r);}
4351    red_len=pLength(red);
4352  }
4353  if (((TEST_V_MODPSOLVSB)&&(red_len>1))||((c->nc)||(erg.to_reduce_u-erg.to_reduce_l>5))){
4354    work_on_copy=TRUE;
4355    // poly m=pOne();
4356    poly m=c->tmp_lm;
4357    pSetCoeff(m,nInit(1));
4358    for(int i=1;i<=pVariables;i++)
4359      pSetExp(m,i,(pGetExp(r[erg.to_reduce_l].p, i)-pGetExp(red,i)));
4360    pSetm(m);
4361    poly red_cp;
4362    #ifdef HAVE_PLURAL
4363    if (c->nc)
4364      red_cp = nc_mm_Mult_pp(m, red, c->r);
4365    else
4366    #endif
4367      red_cp=ppMult_mm(red,m);
4368    if(!erg.fromS){
4369      kBucketInit(r[rn].bucket,red,red_len);
4370    }
4371    //now reduce the copy
4372    //static poly redNF2 (poly h,slimgb_alg* c , int &len, number&  m,int n)
4373
4374    if (!c->nc)
4375      redTailShort(red_cp,c->strat);
4376    //number mul;
4377    // red_len--;
4378//     red_cp->next=redNF2(red_cp->next,c,red_len,mul,c->average_length);
4379//     pSetCoeff(red_cp,nMult(red_cp->coef,mul));
4380//     nDelete(&mul);
4381//     red_len++;
4382    red=red_cp;
4383    red_len=pLength(red);
4384    // pDelete(&m);
4385
4386  }
4387  int i;
4388
4389
4390
4391  assume(red_len==pLength(red));
4392
4393  int reducer_deg=0;
4394  if (c->eliminationProblem){
4395     int lm_deg=pTotaldegree(r[erg.to_reduce_l].p);
4396     int ecart;
4397     if (erg.fromS){
4398       ecart=c->strat->ecartS[erg.reduce_by];
4399     } else {
4400       ecart=pTotaldegree_full(red)-lm_deg;
4401     }
4402     reducer_deg=lm_deg+ecart;
4403  }
4404  pointer=new simple_reducer(red,red_len,reducer_deg,c);
4405
4406  if ((!work_on_copy) && (!erg.fromS))
4407    pointer->fill_back=r[rn].bucket;
4408  else
4409    pointer->fill_back=NULL;
4410  pointer->reduction_id=id;
4411  pointer->c=c;
4412
4413  pointer->reduce(r,erg.to_reduce_l, erg.to_reduce_u);
4414  if(work_on_copy) pDelete(&pointer->p);
4415  delete pointer;
4416  if (lt_changed){
4417    assume(!erg.fromS);
4418    r[erg.reduce_by].sev=sev;
4419  }
4420
4421};
4422
4423
4424
4425
4426void simple_reducer:: pre_reduce(red_object* r, int l, int u){}
4427
Note: See TracBrowser for help on using the repository browser.