source: git/kernel/GBEngine/tgb.cc @ b3594cb

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