source: git/kernel/tgb.cc @ 93ebe1

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