kbuckets.cc
Go to the documentation of this file.
1 /****************************************
2 * Computer Algebra System SINGULAR *
3 ****************************************/
4 
5 #include "misc/auxiliary.h"
6 #include "misc/options.h"
7 
9 #include "coeffs/coeffs.h"
10 #include "coeffs/numbers.h"
11 #include "polys/monomials/ring.h"
12 #include "polys/kbuckets.h"
13 
14 #ifdef HAVE_SHIFTBBA
15 #include "polys/shiftop.h"
16 #endif
17 
18 #ifdef HAVE_COEF_BUCKETS
19 #define USE_COEF_BUCKETS
20 #endif
21 
22 #ifdef USE_COEF_BUCKETS
23 #ifdef HAVE_RINGS_OLD
24 #define MULTIPLY_BUCKET(B,I) do \
25  { if (B->coef[I]!=NULL) \
26  { \
27  assume(p_IsConstant(B->Coef[i],B->bucket->ring)); \
28  B->buckets[I]=p_Mult_q(B->buckets[I],B->coef[I],B->bucket_ring); \
29  B->coef[I]=NULL; \
30  } \
31  } while(0) \
32  if (rField_is_Ring(B->bucket_ring)) B->buckets_length[i] = pLength(B->buckets[i]);
33 #else
34 #define MULTIPLY_BUCKET(B,I) do \
35  { if (B->coef[I]!=NULL) \
36  { \
37  B->buckets[I]=p_Mult_q(B->buckets[I],B->coef[I],B->bucket_ring); \
38  B->coef[I]=NULL; \
39  } \
40  } while(0)
41 #endif
42 #else
43 #define MULTIPLY_BUCKET(B,I)
44 #endif
46 #ifdef USE_COEF_BUCKETS
47 static int coef_start=1;
48 #endif
49 //////////////////////////////////////////////////////////////////////////
50 ///
51 /// Some internal stuff
52 ///
53 
54 // https://graphics.stanford.edu/~seander/bithacks.html#IntegerLog
55 #ifndef BUCKET_TWO_BASE
56 static inline int LOG4(int v)
57 {
58  const unsigned int b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
59  const unsigned int S[] = {1, 2, 4, 8, 16};
60 
61  unsigned int r = 0; // result of log4(v) will go here
62  if (v & b[4]) { v >>= S[4]; r |= S[3]; }
63  if (v & b[3]) { v >>= S[3]; r |= S[2]; }
64  if (v & b[2]) { v >>= S[2]; r |= S[1]; }
65  if (v & b[1]) { v >>= S[1]; r |= S[0]; }
66  return (int)r;
67 }
68 #endif
69 
70 // returns ceil(log_4(l))
71 static inline unsigned int pLogLength(unsigned int l)
72 {
73  unsigned int i = 0;
74 
75  if (l == 0) return 0;
76  l--;
77 #ifdef BUCKET_TWO_BASE
78  i=SI_LOG2(l);
79 #else
80  i=LOG4(l);
81 #endif
82  return i+1;
83 }
84 
85 // returns ceil(log_4(pLength(p)))
86 static inline unsigned int pLogLength(poly p)
87 {
88  return pLogLength((unsigned int) pLength(p));
89 }
90 
91 #ifdef KDEBUG
92 
93 #ifndef HAVE_PSEUDO_BUCKETS
94 BOOLEAN kbTest_i(kBucket_pt bucket, int i)
95 {//sBucketSortMerge
96  #ifdef USE_COEF_BUCKETS
97  assume(bucket->coef[0]==NULL);
98  if ((bucket->coef[i]!=NULL) && (bucket->buckets[i]==NULL))
99  {
100  dReportError("Bucket %d coef not NULL", i);
101  }
102  if (bucket->coef[i]!=NULL)
103  {
104  assume(bucket->buckets[i]!=NULL);
105  p_Test(bucket->coef[i],bucket->bucket_ring);
106  }
107  #endif
108  pFalseReturn(p_Test(bucket->buckets[i], bucket->bucket_ring));
109  if ((unsigned)bucket->buckets_length[i] != pLength(bucket->buckets[i]))
110  {
111  dReportError("Bucket %d lengths difference should:%d has:%d",
112  i, bucket->buckets_length[i], pLength(bucket->buckets[i]));
113  }
114  else if (i > 0 && (int) pLogLength(bucket->buckets_length[i]) > i)
115  {
116  dReportError("Bucket %d too long %d",
117  i, bucket->buckets_length[i]);
118  }
119  if (i==0 && bucket->buckets_length[0] > 1)
120  {
121  dReportError("Bucket 0 too long");
122  }
123  return TRUE;
124 }
125 
126 
127 BOOLEAN kbTest(kBucket_pt bucket)
128 {
129  #ifdef HAVE_COEF_BUCKETS
130  assume(bucket->coef[0]==NULL);
131  #endif
132  int i;
133  poly lm = bucket->buckets[0];
134 
135  omCheckAddrBin(bucket, kBucket_bin);
136  assume(bucket->buckets_used <= MAX_BUCKET);
137  if (! kbTest_i(bucket, 0)) return FALSE;
138  for (i=1; i<= (int) bucket->buckets_used; i++)
139  {
140  if (!kbTest_i(bucket, i)) return FALSE;
141  if (lm != NULL && bucket->buckets[i] != NULL
142  && p_LmCmp(lm, bucket->buckets[i], bucket->bucket_ring) != 1)
143  {
144  dReportError("Bucket %d larger or equal than lm", i);
145  if (p_LmCmp(lm, bucket->buckets[i], bucket->bucket_ring) ==0)
146  dReportError("Bucket %d equal to lm", i);
147  return FALSE;
148  }
149  if (!p_Test(bucket->buckets[i],bucket->bucket_ring))
150  {
151  dReportError("Bucket %d is not =0(4)", i);
152  return FALSE;
153  }
154  }
155 
156  for (; i<=MAX_BUCKET; i++)
157  {
158  if (bucket->buckets[i] != NULL || bucket->buckets_length[i] != 0)
159  {
160  dReportError("Bucket %d not zero", i);
161  return FALSE;
162  }
163  }
164  for(i=0;i<=MAX_BUCKET;i++)
165  {
166  if (bucket->buckets[i]!=NULL)
167  {
168  int j;
169  for(j=i+1;j<=MAX_BUCKET;j++)
170  {
171  if (bucket->buckets[j]==bucket->buckets[i])
172  {
173  dReportError("Bucket %d %d equal", i,j);
174  return FALSE;
175  }
176  }
177  }
178  #ifdef HAVE_COEF_BUCKETS
179  if (bucket->coef[i]!=NULL)
180  {
181  int j;
182  for(j=i+1;j<=MAX_BUCKET;j++)
183  {
184  if (bucket->coef[j]==bucket->coef[i])
185  {
186  dReportError("internal coef %d %d equal", i,j);
187  return FALSE;
188  }
189  }
190  }
191  #endif
192  }
193  return TRUE;
194 }
195 
196 #else // HAVE_PSEUDO_BUCKETS
198 {
199  return TRUE;
200 }
201 #endif // ! HAVE_PSEUDO_BUCKETS
202 #endif // KDEBUG
203 
204 //////////////////////////////////////////////////////////////////////////
205 ///
206 /// Creation/Destruction of buckets
207 ///
208 
209 kBucket_pt kBucketCreate(const ring bucket_ring)
210 {
211  assume(bucket_ring != NULL);
213  bucket->bucket_ring = bucket_ring;
214  return bucket;
215 }
216 void kBucketDestroy(kBucket_pt *bucket_pt)
217 {
218  omFreeBin(*bucket_pt, kBucket_bin);
219  *bucket_pt = NULL;
220 }
221 
222 
224 {
225  kBucket_pt bucket = *bucket_pt;
226  kbTest(bucket);
227  int i;
228  for (i=0; i<= bucket->buckets_used; i++)
229  {
230  p_Delete(&(bucket->buckets[i]), bucket->bucket_ring);
231 #ifdef USE_COEF_BUCKETS
232  p_Delete(&(bucket->coef[i]), bucket->bucket_ring);
233 #endif
234  }
235  omFreeBin(bucket, kBucket_bin);
236  *bucket_pt = NULL;
237 }
238 
239 /////////////////////////////////////////////////////////////////////////////
240 // Convertion from/to Bpolys
241 //
242 #ifndef HAVE_PSEUDO_BUCKETS
243 
244 inline void kBucketMergeLm(kBucket_pt bucket)
245 {
246  kbTest(bucket);
247  if (bucket->buckets[0] != NULL)
248  {
249  poly lm = bucket->buckets[0];
250  int i = 1;
251 #ifdef BUCKET_TWO_BASE
252  int l = 2;
253  while ( bucket->buckets_length[i] >= l)
254  {
255  i++;
256  l = l << 1;
257  }
258 #else
259  int l = 4;
260  while ( bucket->buckets_length[i] >= l)
261  {
262  i++;
263  l = l << 2;
264  }
265 #endif
266 #ifndef USE_COEF_BUCKETS
267  MULTIPLY_BUCKET(bucket,i);
268  pNext(lm) = bucket->buckets[i];
269  bucket->buckets[i] = lm;
270  bucket->buckets_length[i]++;
271  assume(i <= bucket->buckets_used+1);
272  if (i > bucket->buckets_used) bucket->buckets_used = i;
273  bucket->buckets[0] = NULL;
274  bucket->buckets_length[0] = 0;
275  kbTest(bucket);
276 #else
277  if (i > bucket->buckets_used) bucket->buckets_used = i;
278  assume(i!=0);
279  if (bucket->buckets[i]!=NULL)
280  {
281  MULTIPLY_BUCKET(bucket,i);
282  pNext(lm) = bucket->buckets[i];
283  bucket->buckets[i] = lm;
284  bucket->buckets_length[i]++;
285  assume(i <= bucket->buckets_used+1);
286  }
287  else
288  {
289  #if 1
290  assume(bucket->buckets[i]==NULL);
291  assume(bucket->coef[0]==NULL);
292  assume(pLength(lm)==1);
293  assume(pNext(lm)==NULL);
294  number coef=p_GetCoeff(lm,bucket->bucket_ring);
295  //WARNING: not thread_safe
296  p_SetCoeff0(lm, n_Init(1,bucket->bucket_ring), bucket->bucket_ring);
297  bucket->buckets[i]=lm;
298  bucket->buckets_length[i]=1;
299  bucket->coef[i]=p_NSet(n_Copy(coef,bucket->bucket_ring),bucket->bucket_ring);
300 
301  bucket->buckets[i]=lm;
302  bucket->buckets_length[i]=1;
303  #else
304  MULTIPLY_BUCKET(bucket,i);
305  pNext(lm) = bucket->buckets[i];
306  bucket->buckets[i] = lm;
307  bucket->buckets_length[i]++;
308  assume(i <= bucket->buckets_used+1);
309  #endif
310  }
311  bucket->buckets[0]=NULL;
312  bucket->buckets_length[0] = 0;
313  bucket->coef[0]=NULL;
314  kbTest(bucket);
315  #endif
316  }
317 
318 }
319 
321 {
322  int i;
323 
324  for (i = 0;i<=MAX_BUCKET;i++)
325  {
326  if (bucket->buckets[i] != NULL) return FALSE;
327  #ifdef HAVE_COEF_BUCKETS
328  if (bucket->coef[i] != NULL) return FALSE;
329  #endif
330  if (bucket->buckets_length[i] != 0) return FALSE;
331  }
332  return TRUE;
333 }
334 
335 void kBucketInit(kBucket_pt bucket, poly lm, int length)
336 {
337  //assume(false);
338  assume(bucket != NULL);
339  assume(length <= 0 || (unsigned)length == pLength(lm));
340  assume(kBucketIsCleared(bucket));
341 
342  if (lm == NULL) return;
343 
344  if (length <= 0)
345  length = pLength(lm);
346 
347  bucket->buckets[0] = lm;
348  #ifdef HAVE_COEF_BUCKETS
349  assume(bucket->coef[0]==NULL);
350  #endif
351  #ifdef USE_COEF_BUCKETS
352  bucket->coef[0]=NULL;
353  #endif
354  if (lm!=NULL)
355  bucket->buckets_length[0] = 1;
356  else
357  bucket->buckets_length[0]= 0;
358  if (length > 1)
359  {
360  unsigned int i = pLogLength(length-1);
361  bucket->buckets[i] = pNext(lm);
362  pNext(lm) = NULL;
363  bucket->buckets_length[i] = length-1;
364  bucket->buckets_used = i;
365  }
366  else
367  {
368  bucket->buckets_used = 0;
369  }
370 }
371 
373 {
374 #ifndef HAVE_PSEUDO_BUCKETS
375  assume(bucket->buckets_used<=MAX_BUCKET);
376  MULTIPLY_BUCKET(bucket,1);
377  kbTest(bucket);
378  poly p = bucket->buckets[1];
379  poly lm;
380  int pl = bucket->buckets_length[1];//, i;
381  int i;
382  bucket->buckets[1] = NULL;
383  bucket->buckets_length[1] = 0;
384  #ifdef USE_COEF_BUCKETS
385  assume(bucket->coef[1]==NULL);
386  #endif
387  ring r=bucket->bucket_ring;
388 
389 
390  for (i=1; i<=bucket->buckets_used; i++)
391  {
392  #ifdef USE_COEF_BUCKETS
393  if (bucket->coef[i]!=NULL)
394  {
395  assume(bucket->buckets[i]!=NULL);
396  p = p_Plus_mm_Mult_qq(p, bucket->coef[i], bucket->buckets[i],
397  pl, bucket->buckets_length[i], r);
398  p_Delete(&bucket->coef[i],r);
399  p_Delete(&bucket->buckets[i],r);
400  }
401  else
402  p = p_Add_q(p, bucket->buckets[i],
403  pl, bucket->buckets_length[i], r);
404  #else
405  p = p_Add_q(p, bucket->buckets[i],
406  pl, bucket->buckets_length[i], r);
407  #endif
408  if (i==1) continue;
409  bucket->buckets[i] = NULL;
410  bucket->buckets_length[i] = 0;
411  }
412  #ifdef HAVE_COEF_BUCKETS
413  assume(bucket->coef[0]==NULL);
414  #endif
415  lm = bucket->buckets[0];
416  if (lm != NULL)
417  {
418  pNext(lm) = p;
419  p = lm;
420  pl++;
421  bucket->buckets[0] = NULL;
422  bucket->buckets_length[0] = 0;
423  }
424  if (pl > 0)
425  {
426  i = pLogLength(pl);
427  bucket->buckets[i] = p;
428  bucket->buckets_length[i] = pl;
429  }
430  else
431  {
432  i = 0;
433  }
434  bucket->buckets_used = i;
435  assume(bucket->buckets_used <= MAX_BUCKET);
436  #ifdef USE_COEF_BUCKETS
437  assume(bucket->coef[0]==NULL);
438  assume(bucket->coef[i]==NULL);
439  #endif
440  assume(pLength(p) == (unsigned)pl);
441  //if (TEST_OPT_PROT) { Print("C(%d)",pl); }
442  kbTest(bucket);
443  return i;
444 #endif
445 }
446 
447 void kBucketNormalize(kBucket_pt bucket)
448 {
449 #ifdef HAVE_PSEUDO_BUCKETS
450  p_Normalize(bucket->p,bucket->bucket_ring);
451 #else
452  MULTIPLY_BUCKET(bucket,1);
453  for (int i=0; i<=bucket->buckets_used; i++)
454  {
455  p_Normalize(bucket->buckets[i],bucket->bucket_ring);
456  }
457 #endif
458 }
459 
460 void kBucketClear(kBucket_pt bucket, poly *p, int *length)
461 {
462  int i = kBucketCanonicalize(bucket);
463  if (i > 0)
464  {
465  #ifdef USE_COEF_BUCKETS
466  MULTIPLY_BUCKET(bucket,i);
467  //bucket->coef[i]=NULL;
468  #endif
469  *p = bucket->buckets[i];
470  *length = bucket->buckets_length[i];
471  bucket->buckets[i] = NULL;
472  bucket->buckets_length[i] = 0;
473  bucket->buckets_used = 0;
474 
475  }
476  else
477  {
478  *p = NULL;
479  *length = 0;
480  }
481 }
482 
483 void kBucketSetLm(kBucket_pt bucket, poly lm)
484 {
485  kBucketMergeLm(bucket);
486  pNext(lm) = NULL;
487  bucket->buckets[0] = lm;
488  bucket->buckets_length[0] = 1;
489 }
490 
491 #else // HAVE_PSEUDO_BUCKETS
492 
493 void kBucketInit(kBucket_pt bucket, poly lm, int length)
494 {
495  int i;
496 
497  assume(bucket != NULL);
498  assume(length <= 0 || length == pLength(lm));
499 
500  bucket->p = lm;
501  if (length <= 0) bucket->l = pLength(lm);
502  else bucket->l = length;
503 
504 }
505 
506 const poly kBucketGetLm(kBucket_pt bucket)
507 {
508  return bucket->p;
509 }
510 
512 {
513  poly lm = bucket->p;
514  assume(pLength(bucket->p) == bucket->l);
515  pIter(bucket->p);
516  (bucket->l)--;
517  pNext(lm) = NULL;
518  return lm;
519 }
520 
521 void kBucketClear(kBucket_pt bucket, poly *p, int *length)
522 {
523  assume(pLength(bucket->p) == bucket->l);
524  *p = bucket->p;
525  *length = bucket->l;
526  bucket->p = NULL;
527  bucket->l = 0;
528 }
529 
530 #endif // ! HAVE_PSEUDO_BUCKETS
531 //////////////////////////////////////////////////////////////////////////
532 ///
533 /// For changing the ring of the Bpoly to new_tailBin
534 ///
536  ring new_tailRing, omBin new_tailBin,
537  pShallowCopyDeleteProc p_shallow_copy_delete)
538 {
539 #ifndef HAVE_PSEUDO_BUCKETS
540  int i;
541 
542  kBucketCanonicalize(bucket);
543  for (i=0; i<= bucket->buckets_used; i++)
544  if (bucket->buckets[i] != NULL)
545  {
546  MULTIPLY_BUCKET(bucket,i);
547  bucket->buckets[i] = p_shallow_copy_delete(bucket->buckets[i],
548  bucket->bucket_ring,
549  new_tailRing,
550  new_tailBin);
551  }
552 #else
553  bucket->p = p_shallow_copy_delete(p,
554  bucket_ring,
555  new_tailRing,
556  new_tailBin);
557 #endif
558  bucket->bucket_ring = new_tailRing;
559 }
560 
561 //////////////////////////////////////////////////////////////////////////
562 ///
563 /// Bucket number i from bucket is out of length sync, resync
564 ///
565 void kBucketAdjust(kBucket_pt bucket, int i) {
566 
567  MULTIPLY_BUCKET(bucket,i);
568 
569  int l1 = bucket->buckets_length[i];
570  poly p1 = bucket->buckets[i];
571  bucket->buckets[i] = NULL;
572  bucket->buckets_length[i] = 0;
573  i = pLogLength(l1);
574 
575  while (bucket->buckets[i] != NULL)
576  {
577  //kbTest(bucket);
578  MULTIPLY_BUCKET(bucket,i);
579  p1 = p_Add_q(p1, bucket->buckets[i],
580  l1, bucket->buckets_length[i], bucket->bucket_ring);
581  bucket->buckets[i] = NULL;
582  bucket->buckets_length[i] = 0;
583  i = pLogLength(l1);
584  }
585 
586  bucket->buckets[i] = p1;
587  bucket->buckets_length[i]=l1;
588  if (i >= bucket->buckets_used)
589  bucket->buckets_used = i;
590  else
591  kBucketAdjustBucketsUsed(bucket);
592 }
593 
594 //////////////////////////////////////////////////////////////////////////
595 ///
596 /// Multiply Bucket by number ,i.e. Bpoly == n*Bpoly
597 ///
598 void kBucket_Mult_n(kBucket_pt bucket, number n)
599 {
600 #ifndef HAVE_PSEUDO_BUCKETS
601  kbTest(bucket);
602  ring r=bucket->bucket_ring;
603  int i;
604 
605  for (i=0; i<= bucket->buckets_used; i++)
606  {
607  if (bucket->buckets[i] != NULL)
608  {
609 #ifdef USE_COEF_BUCKETS
610  if (i<coef_start)
611  bucket->buckets[i] = __p_Mult_nn(bucket->buckets[i], n, r);
612  /* Frank Seelisch on March 11, 2010:
613  This looks a bit strange: The following "if" is indented
614  like the previous line of code. But coded as it is,
615  it should actually be two spaces less indented.
616  Question: Should the following "if" also only be
617  performed when "(i<coef_start)" is true?
618  For the time being, I leave it as it is. */
619  if (rField_is_Ring(r) && !(rField_is_Domain(r)))
620  {
621  bucket->buckets_length[i] = pLength(bucket->buckets[i]);
622  kBucketAdjust(bucket, i);
623  }
624  else
625  if (bucket->coef[i]!=NULL)
626  {
627  bucket->coef[i] = __p_Mult_nn(bucket->coef[i],n,r);
628  }
629  else
630  {
631  bucket->coef[i] = p_NSet(n_Copy(n,r),r);
632  }
633 #else
634  bucket->buckets[i] = __p_Mult_nn(bucket->buckets[i], n, r);
635  if (rField_is_Ring(r) && !(rField_is_Domain(r)))
636  {
637  bucket->buckets_length[i] = pLength(bucket->buckets[i]);
638  kBucketAdjust(bucket, i);
639  }
640 #endif
641  }
642  }
643  kbTest(bucket);
644 #else
645  bucket->p = __p_Mult_nn(bucket->p, n, bucket->bucket_ring);
646 #endif
647 }
648 
649 
650 //////////////////////////////////////////////////////////////////////////
651 ///
652 /// Add to Bucket a poly ,i.e. Bpoly == q+Bpoly
653 ///
654 void kBucket_Add_q(kBucket_pt bucket, poly q, int *l)
655 {
656  if (q == NULL) return;
657  assume(*l <= 0 || pLength(q) == *l);
658 
659  int i, l1;
660  ring r = bucket->bucket_ring;
661 
662  if (*l <= 0)
663  {
664  l1 = pLength(q);
665  *l = l1;
666  }
667  else
668  l1 = *l;
669 
670  kBucketMergeLm(bucket);
671  kbTest(bucket);
672  i = pLogLength(l1);
673 
674  while (bucket->buckets[i] != NULL)
675  {
676  //MULTIPLY_BUCKET(bucket,i);
677  #ifdef USE_COEF_BUCKETS
678  if (bucket->coef[i]!=NULL)
679  {
680  q = p_Plus_mm_Mult_qq(q, bucket->coef[i], bucket->buckets[i],
681  l1, bucket->buckets_length[i], r);
682  p_Delete(&bucket->coef[i],r);
683  p_Delete(&bucket->buckets[i],r);
684  }
685  else
686  q = p_Add_q(q, bucket->buckets[i],
687  l1, bucket->buckets_length[i], r);
688  #else
689  q = p_Add_q(q, bucket->buckets[i],
690  l1, bucket->buckets_length[i], r);
691  #endif
692  bucket->buckets[i] = NULL;
693  bucket->buckets_length[i] = 0;
694  i = pLogLength(l1);
695  assume(i<= MAX_BUCKET);
696  assume(bucket->buckets_used<= MAX_BUCKET);
697  }
698 
699  kbTest(bucket);
700  bucket->buckets[i] = q;
701  bucket->buckets_length[i]=l1;
702  if (i >= bucket->buckets_used)
703  bucket->buckets_used = i;
704  else
705  kBucketAdjustBucketsUsed(bucket);
706  kbTest(bucket);
707 }
708 
709 
710 
711 //////////////////////////////////////////////////////////////////////////
712 ///
713 /// Bpoly == Bpoly - m*p; where m is a monom
714 /// Does not destroy p and m
715 /// assume (*l <= 0 || pLength(p) == *l)
716 void kBucket_Minus_m_Mult_p(kBucket_pt bucket, poly m, poly p, int *l,
717  poly spNoether)
718 {
719  assume(*l <= 0 || pLength(p) == *l);
720  int i, l1;
721  poly p1 = p;
722  ring r = bucket->bucket_ring;
723 
724  if (*l <= 0)
725  {
726  l1 = pLength(p1);
727  *l = l1;
728  }
729  else
730  l1 = *l;
731 
732  if (m == NULL || p == NULL) return;
733 
734 #ifndef HAVE_PSEUDO_BUCKETS
735  kBucketMergeLm(bucket);
736  kbTest(bucket);
737  i = pLogLength(l1);
738 
739 #if defined(HAVE_PLURAL)
740  if ((rField_is_Ring(r) && !(rField_is_Domain(r)))
741  ||(rIsPluralRing(r)))
742  {
743  pSetCoeff0(m, n_InpNeg(pGetCoeff(m),r->cf));
744  p1=r->p_Procs->pp_mm_Mult(p,m,r);
745  pSetCoeff0(m, n_InpNeg(pGetCoeff(m),r->cf));
746  l1=pLength(p1);
747  i = pLogLength(l1);
748  }
749  else
750 #endif
751  {
752  if ((i <= bucket->buckets_used) && (bucket->buckets[i] != NULL))
753  {
754  assume(pLength(bucket->buckets[i])==(unsigned)bucket->buckets_length[i]);
755 //#ifdef USE_COEF_BUCKETS
756 // if(bucket->coef[i]!=NULL)
757 // {
758 // poly mult=p_Mult_mm(bucket->coef[i],m,r);
759 // bucket->coef[i]=NULL;
760 // p1 = p_Minus_mm_Mult_qq(bucket->buckets[i], mult, p1,
761 // bucket->buckets_length[i], l1,
762 // spNoether, r);
763 // }
764 // else
765 //#endif
766  MULTIPLY_BUCKET(bucket,i);
767  p1 = p_Minus_mm_Mult_qq(bucket->buckets[i], m, p1,
768  bucket->buckets_length[i], l1,
769  spNoether, r);
770  l1 = bucket->buckets_length[i];
771  bucket->buckets[i] = NULL;
772  bucket->buckets_length[i] = 0;
773  i = pLogLength(l1);
774  }
775  else
776  {
777  pSetCoeff0(m, n_InpNeg(pGetCoeff(m),r->cf));
778  if (spNoether != NULL)
779  {
780  l1 = -1;
781  p1 = r->p_Procs->pp_Mult_mm_Noether(p1, m, spNoether, l1, r);
782  i = pLogLength(l1);
783  }
784  else
785  {
786  p1 = r->p_Procs->pp_mm_Mult(p1, m, r);
787  }
788  pSetCoeff0(m, n_InpNeg(pGetCoeff(m),r->cf));
789  }
790  }
791 
792  while (bucket->buckets[i] != NULL)
793  {
794  //kbTest(bucket);
795  MULTIPLY_BUCKET(bucket,i);
796  p1 = p_Add_q(p1, bucket->buckets[i],
797  l1, bucket->buckets_length[i], r);
798  bucket->buckets[i] = NULL;
799  bucket->buckets_length[i] = 0;
800  i = pLogLength(l1);
801  }
802 
803  bucket->buckets[i] = p1;
804  bucket->buckets_length[i]=l1;
805  if (i >= bucket->buckets_used)
806  bucket->buckets_used = i;
807  else
808  kBucketAdjustBucketsUsed(bucket);
809 #else // HAVE_PSEUDO_BUCKETS
810  bucket->p = p_Minus_mm_Mult_qq(bucket->p, m, p,
811  bucket->l, l1,
812  spNoether, r);
813 #endif
814 }
815 
816 //////////////////////////////////////////////////////////////////////////
817 ///
818 /// Bpoly == Bpoly + m*p; where m is a monom
819 /// Does not destroy p and m
820 /// assume (l <= 0 || pLength(p) == l)
821 void kBucket_Plus_mm_Mult_pp(kBucket_pt bucket, poly m, poly p, int l)
822 {
823  assume((!rIsPluralRing(bucket->bucket_ring))||p_IsConstant(m, bucket->bucket_ring));
824  assume(l <= 0 || pLength(p) == (unsigned)l);
825  int i, l1;
826  poly p1 = p;
827  ring r = bucket->bucket_ring;
828 
829  if (m == NULL || p == NULL) return;
830 
831  if (l <= 0)
832  {
833  l1 = pLength(p1);
834  l = l1;
835  }
836  else
837  l1 = l;
838 
839  kBucketMergeLm(bucket);
840  kbTest(bucket);
841  i = pLogLength(l1);
842  #ifdef USE_COEF_BUCKETS
843  number n=n_Init(1,r->cf);
844  #endif
845  if (i <= bucket->buckets_used && bucket->buckets[i] != NULL)
846  {
847  //if (FALSE){
848  #ifdef USE_COEF_BUCKETS
849  if ((bucket->coef[i]!=NULL) &&(i>=coef_start))
850  {
851  number orig_coef=p_GetCoeff(bucket->coef[i],r);
852  //we take ownership:
853  p_SetCoeff0(bucket->coef[i],n_Init(0,r),r);
854  number add_coef=n_Copy(p_GetCoeff(m,r),r);
855  number gcd=n_Gcd(add_coef, orig_coef,r);
856 
857  if (!(n_IsOne(gcd,r)))
858  {
859  number orig_coef2=n_ExactDiv(orig_coef,gcd,r);
860  number add_coef2=n_ExactDiv(add_coef, gcd,r);
861  n_Delete(&orig_coef,r);
862  n_Delete(&add_coef,r);
863  orig_coef=orig_coef2;
864  add_coef=add_coef2;
865 
866  //p_Mult_nn(bucket->buckets[i], orig_coef,r);
867  n_Delete(&n,r);
868  n=gcd;
869  }
870 
871  //assume(n_IsOne(n,r));
872  number backup=p_GetCoeff(m,r);
873 
874  p_SetCoeff0(m,add_coef,r);
875  bucket->buckets[i]=__p_Mult_nn(bucket->buckets[i],orig_coef,r);
876 
877  n_Delete(&orig_coef,r);
878  p_Delete(&bucket->coef[i],r);
879 
880  p1 = p_Plus_mm_Mult_qq(bucket->buckets[i], m, p1,
881  bucket->buckets_length[i], l1, r);
882  l1=bucket->buckets_length[i];
883  bucket->buckets[i]=NULL;
884  bucket->buckets_length[i] = 0;
885  i = pLogLength(l1);
886  assume(l1==pLength(p1));
887 
888  p_SetCoeff(m,backup,r); //deletes add_coef
889  }
890  else
891  #endif
892  {
893  MULTIPLY_BUCKET(bucket,i);
894  p1 = p_Plus_mm_Mult_qq(bucket->buckets[i], m, p1,
895  bucket->buckets_length[i], l1, r);
896  l1 = bucket->buckets_length[i];
897  bucket->buckets[i] = NULL;
898  bucket->buckets_length[i] = 0;
899  i = pLogLength(l1);
900  }
901  }
902  else
903  {
904  #ifdef USE_COEF_BUCKETS
905  number swap_n=p_GetCoeff(m,r);
906 
907  assume(n_IsOne(n,r));
908  p_SetCoeff0(m,n,r);
909  n=swap_n;
910  //p_SetCoeff0(n, swap_n, r);
911  //p_GetCoeff0(n, swap_n,r);
912  #endif
913  p1 = r->p_Procs->pp_Mult_mm(p1, m, r);
914  #ifdef USE_COEF_BUCKETS
915  //m may not be changed
916  p_SetCoeff(m,n_Copy(n,r),r);
917  #endif
918  }
919 
920  while ((bucket->buckets[i] != NULL) && (p1!=NULL))
921  {
922  assume(i!=0);
923  #ifdef USE_COEF_BUCKETS
924  if ((bucket->coef[i]!=NULL) &&(i>=coef_start))
925  {
926  number orig_coef=p_GetCoeff(bucket->coef[i],r);
927  //we take ownership:
928  p_SetCoeff0(bucket->coef[i],n_Init(0,r),r);
929  number add_coef=n_Copy(n,r);
930  number gcd=n_Gcd(add_coef, orig_coef,r);
931 
932  if (!(n_IsOne(gcd,r)))
933  {
934  number orig_coef2=n_ExactDiv(orig_coef,gcd,r);
935  number add_coef2=n_ExactDiv(add_coef, gcd,r);
936  n_Delete(&orig_coef,r);
937  n_Delete(&n,r);
938  n_Delete(&add_coef,r);
939  orig_coef=orig_coef2;
940  add_coef=add_coef2;
941  //p_Mult_nn(bucket->buckets[i], orig_coef,r);
942  n=gcd;
943  }
944  //assume(n_IsOne(n,r));
945  bucket->buckets[i]=__p_Mult_nn(bucket->buckets[i],orig_coef,r);
946  p1=__p_Mult_nn(p1,add_coef,r);
947 
948  p1 = p_Add_q(p1, bucket->buckets[i],r);
949  l1=pLength(p1);
950 
951  bucket->buckets[i]=NULL;
952  n_Delete(&orig_coef,r);
953  p_Delete(&bucket->coef[i],r);
954  //l1=bucket->buckets_length[i];
955  assume(l1==pLength(p1));
956  }
957  else
958  #endif
959  {
960  //don't do that, pull out gcd
961  #ifdef USE_COEF_BUCKETS
962  if(!(n_IsOne(n,r)))
963  {
964  p1=__p_Mult_nn(p1, n, r);
965  n_Delete(&n,r);
966  n=n_Init(1,r);
967  }
968  #endif
969  MULTIPLY_BUCKET(bucket,i);
970  p1 = p_Add_q(p1, bucket->buckets[i],
971  l1, bucket->buckets_length[i], r);
972  bucket->buckets[i] = NULL;
973  bucket->buckets_length[i] = 0;
974  }
975  i = pLogLength(l1);
976  }
977 
978  bucket->buckets[i] = p1;
979 #ifdef USE_COEF_BUCKETS
980  assume(bucket->coef[i]==NULL);
981 
982  if (!(n_IsOne(n,r)))
983  {
984  bucket->coef[i]=p_NSet(n,r);
985  }
986  else
987  {
988  bucket->coef[i]=NULL;
989  n_Delete(&n,r);
990  }
991 
992  if (p1==NULL)
993  p_Delete(&bucket->coef[i],r);
994 #endif
995  bucket->buckets_length[i]=l1;
996  if (i > bucket->buckets_used)
997  bucket->buckets_used = i;
998  else
999  kBucketAdjustBucketsUsed(bucket);
1000 
1001  kbTest(bucket);
1002 }
1003 
1004 poly kBucket_ExtractLarger(kBucket_pt bucket, poly q, poly append)
1005 {
1006  if (q == NULL) return append;
1007  poly lm;
1008  loop
1009  {
1010  lm = kBucketGetLm(bucket);
1011  if (lm == NULL) return append;
1012  if (p_LmCmp(lm, q, bucket->bucket_ring) == 1)
1013  {
1014  lm = kBucketExtractLm(bucket);
1015  pNext(append) = lm;
1016  pIter(append);
1017  }
1018  else
1019  {
1020  return append;
1021  }
1022  }
1023 }
1024 
1025 /////////////////////////////////////////////////////////////////////////////
1026 //
1027 // Extract all monomials from bucket with component comp
1028 // Return as a polynomial *p with length *l
1029 // In other words, afterwards
1030 // Bpoly = Bpoly - (poly consisting of all monomials with component comp)
1031 // and components of monomials of *p are all 0
1032 //
1033 
1034 // Hmm... for now I'm too lazy to implement those independent of currRing
1035 // But better declare it extern than including polys.h
1036 extern void p_TakeOutComp(poly *p, long comp, poly *q, int *lq, const ring r);
1037 
1039  long comp,
1040  poly *r_p, int *l)
1041 {
1042  poly p = NULL, q;
1043  int i, lp = 0, lq;
1044 
1045 #ifndef HAVE_PSEUDO_BUCKETS
1046  kBucketMergeLm(bucket);
1047  for (i=1; i<=bucket->buckets_used; i++)
1048  {
1049  if (bucket->buckets[i] != NULL)
1050  {
1051  MULTIPLY_BUCKET(bucket,i);
1052  p_TakeOutComp(&(bucket->buckets[i]), comp, &q, &lq, bucket->bucket_ring);
1053  if (q != NULL)
1054  {
1055  assume(pLength(q) == (unsigned)lq);
1056  bucket->buckets_length[i] -= lq;
1057  assume(pLength(bucket->buckets[i]) == (unsigned)bucket->buckets_length[i]);
1058  p = p_Add_q(p, q, lp, lq, bucket->bucket_ring);
1059  }
1060  }
1061  }
1062  kBucketAdjustBucketsUsed(bucket);
1063 #else
1064  p_TakeOutComp(&(bucket->p), comp, &p, &lp,bucket->bucket_ring);
1065  (bucket->l) -= lp;
1066 #endif
1067  *r_p = p;
1068  *l = lp;
1069 
1070  kbTest(bucket);
1071 }
1072 
1073 /////////////////////////////////////////////////////////////////////////////
1074 // Reduction of Bpoly with a given poly
1075 //
1076 
1077 extern int ksCheckCoeff(number *a, number *b);
1078 
1080  poly p1, int l1,
1081  poly spNoether)
1082 {
1083  ring r=bucket->bucket_ring;
1084  assume((!rIsPluralRing(r))||p_LmEqual(p1,kBucketGetLm(bucket), r));
1085  assume(p1 != NULL &&
1086  p_DivisibleBy(p1, kBucketGetLm(bucket), r));
1087  assume(pLength(p1) == (unsigned) l1);
1088 
1089  poly a1 = pNext(p1), lm = kBucketExtractLm(bucket);
1090  BOOLEAN reset_vec=FALSE;
1091  number rn;
1092 
1093  /* we shall reduce bucket=bn*lm+... by p1=an*t+a1 where t=lm(p1)
1094  and an,bn shall be defined further down only if lc(p1)!=1
1095  we already know: an|bn and t|lm */
1096  if(a1==NULL)
1097  {
1098  p_LmDelete(&lm, r);
1099  return n_Init(1,r->cf);
1100  }
1101 
1102  if (! n_IsOne(pGetCoeff(p1),r->cf))
1103  {
1104  number an = pGetCoeff(p1), bn = pGetCoeff(lm);
1105 //StringSetS("##### an = "); nWrite(an); PrintS(StringEndS("\n")); // NOTE/TODO: use StringAppendS("\n"); omFree(s);
1106 //StringSetS("##### bn = "); nWrite(bn); PrintS(StringEndS("\n")); // NOTE/TODO: use StringAppendS("\n"); omFree(s);
1107  /* ksCheckCoeff: divide out gcd from an and bn: */
1108  int ct = ksCheckCoeff(&an, &bn,r->cf);
1109  /* the previous command returns ct=0 or ct=2 iff an!=1
1110  note: an is now 1 or -1 */
1111 
1112  /* setup factor for p1 which cancels leading terms */
1113  p_SetCoeff(lm, bn, r);
1114  if ((ct == 0) || (ct == 2))
1115  {
1116  /* next line used to be here before but is WRONG:
1117  kBucket_Mult_n(bucket, an);
1118  its use would result in a wrong sign for the tail of bucket
1119  in the reduction */
1120 
1121  /* correct factor for cancelation by changing sign if an=-1 */
1122  if (rField_is_Ring(r))
1123  lm = __p_Mult_nn(lm, an, r);
1124  else
1125  kBucket_Mult_n(bucket, an);
1126  }
1127  rn = an;
1128  }
1129  else
1130  {
1131  rn = n_Init(1,r->cf);
1132  }
1133 
1134  if (p_GetComp(p1, r) != p_GetComp(lm, r))
1135  {
1136  p_SetCompP(a1, p_GetComp(lm, r), r);
1137  reset_vec = TRUE;
1138  p_SetComp(lm, p_GetComp(p1, r), r);
1139  p_Setm(lm, r);
1140  }
1141 
1142  p_ExpVectorSub(lm, p1, r);
1143  l1--;
1144 
1145  assume((unsigned)l1==pLength(a1));
1146 
1147 #ifdef HAVE_SHIFTBBA
1148  poly lmRight;
1149  if (r->isLPring) {
1150  int firstBlock = p_mFirstVblock(p1, r);
1151  k_SplitFrame(lm, lmRight, firstBlock, r);
1152  }
1153 #endif
1154 #if 0
1155  BOOLEAN backuped=FALSE;
1156  number coef;
1157  //@Viktor, don't ignore coefficients on monomials
1158  if(l1==1) {
1159 
1160  //if (rField_is_Q(r)) {
1161  //avoid this for function fields, as gcds are expensive at the moment
1162 
1163 
1164  coef=p_GetCoeff(a1,r);
1165  lm=p_Mult_nn(lm, coef, r);
1166  p_SetCoeff0(a1, n_Init(1,r), r);
1167  backuped=TRUE;
1168  //WARNING: not thread_safe
1169  //deletes coef as side effect
1170  //}
1171  }
1172 #endif
1173 
1174 #ifdef HAVE_SHIFTBBA
1175  if (r->isLPring)
1176  {
1177  kBucket_Minus_m_Mult_p(bucket, lm, r->p_Procs->pp_Mult_mm(a1, lmRight, r), &l1, spNoether);
1178  }
1179  else
1180 #endif
1181  {
1182  kBucket_Minus_m_Mult_p(bucket, lm, a1, &l1, spNoether);
1183  }
1184 
1185 #if 0
1186  if (backuped)
1187  p_SetCoeff0(a1,coef,r);
1188 #endif
1189 
1190  p_LmDelete(&lm, r);
1191 #ifdef HAVE_SHIFTBBA
1192  if (r->isLPring)
1193  {
1194  p_LmDelete(&lmRight, r);
1195  }
1196 #endif
1197  if (reset_vec) p_SetCompP(a1, 0, r);
1198  kbTest(bucket);
1199  return rn;
1200 }
1201 
1202 #ifndef USE_COEF_BUCKETS
1204 {
1205  if (bucket->buckets[0]==NULL) return;
1206 
1207  ring r=bucket->bucket_ring;
1208  if (rField_is_Ring(r)) return;
1209 
1210  coeffs cf=r->cf;
1211  if (cf->cfSubringGcd==ndGcd) /* trivial gcd*/ return;
1212 
1213  number nn=pGetCoeff(bucket->buckets[0]);
1214  //if ((bucket->buckets_used==0)
1215  //&&(!n_IsOne(nn,cf)))
1216  //{
1217  // if (TEST_OPT_PROT) PrintS("@");
1218  // p_SetCoeff(bucket->buckets[0],n_Init(1,cf),r);
1219  // return;
1220  //}
1221 
1222  if (n_Size(nn,cf)<2) return;
1223 
1224  //kBucketAdjustBucketsUsed(bucket);
1225  number coef=n_Copy(nn,cf);
1226  // find an initial guess of a gcd
1227  for (int i=1; i<=bucket->buckets_used;i++)
1228  {
1229  if (bucket->buckets[i]!=NULL)
1230  {
1231  number t=p_InitContent(bucket->buckets[i],r);
1232  if (n_Size(t,cf)<2)
1233  {
1234  n_Delete(&t,cf);
1235  n_Delete(&coef,cf);
1236  return;
1237  }
1238  number t2=n_SubringGcd(coef,t,cf);
1239  n_Delete(&t,cf);
1240  n_Delete(&coef,cf);
1241  coef=t2;
1242  if (n_Size(coef,cf)<2) { n_Delete(&coef,cf);return;}
1243  }
1244  }
1245  // find the gcd
1246  for (int i=0; i<=bucket->buckets_used;i++)
1247  {
1248  if (bucket->buckets[i]!=NULL)
1249  {
1250  poly p=bucket->buckets[i];
1251  while(p!=NULL)
1252  {
1253  number t=n_SubringGcd(coef,pGetCoeff(p),cf);
1254  if (n_Size(t,cf)<2)
1255  {
1256  n_Delete(&t,cf);
1257  n_Delete(&coef,cf);
1258  return;
1259  }
1260  pIter(p);
1261  }
1262  }
1263  }
1264  // divided by the gcd
1265  if (TEST_OPT_PROT) PrintS("@");
1266  for (int i=bucket->buckets_used;i>=0;i--)
1267  {
1268  if (bucket->buckets[i]!=NULL)
1269  {
1270  poly p=bucket->buckets[i];
1271  while(p!=NULL)
1272  {
1273  number d = n_ExactDiv(pGetCoeff(p),coef,cf);
1274  p_SetCoeff(p,d,r);
1275  pIter(p);
1276  }
1277  }
1278  }
1279  n_Delete(&coef,cf);
1280 }
1281 #else
1282 static BOOLEAN nIsPseudoUnit(number n, ring r)
1283 {
1284  if (rField_is_Zp(r))
1285  return TRUE;
1286 
1287  if (rParameter(r)==NULL)
1288  {
1289  return (n_Size(n,r->cf)==1);
1290  }
1291  //if (r->parameter!=NULL)
1292  return (n_IsOne(n,r->cf) || n_IsMOne(n,r->cf));
1293 }
1294 
1295 void kBucketSimpleContent(kBucket_pt bucket)
1296 {
1297  ring r=bucket->bucket_ring;
1298  int i;
1299  //PrintS("HHHHHHHHHHHHH");
1300  for (i=0;i<=MAX_BUCKET;i++)
1301  {
1302  //if ((bucket->buckets[i]!=NULL) && (bucket->coef[i]!=NULL))
1303  // PrintS("H2H2H2");
1304  if (i==0)
1305  {
1306  assume(bucket->buckets[i]==NULL);
1307  }
1308  if ((bucket->buckets[i]!=NULL) && (bucket->coef[i]==NULL))
1309  return;
1310  }
1311  for (i=0;i<=MAX_BUCKET;i++)
1312  {
1313  //if ((bucket->buckets[i]!=NULL) && (bucket->coef[i]!=NULL))
1314  // PrintS("H2H2H2");
1315  if (i==0)
1316  {
1317  assume(bucket->buckets[i]==NULL);
1318  }
1319  if ((bucket->buckets[i]!=NULL)
1320  && (nIsPseudoUnit(p_GetCoeff(bucket->coef[i],r),r)))
1321  return;
1322  }
1323  //return;
1324 
1325  number coef=n_Init(0,r);
1326  //ATTENTION: will not work correct for GB over ring
1327  //if (TEST_OPT_PROT)
1328  // PrintS("CCCCCCCCCCCCC");
1329  for (i=MAX_BUCKET;i>=0;i--)
1330  {
1331  if (i==0)
1332  {
1333  assume(bucket->buckets[i]==NULL);
1334  }
1335  if (bucket->buckets[i]!=NULL)
1336  {
1337  assume(bucket->coef[i]!=NULL);
1338  assume(!(n_IsZero(pGetCoeff(bucket->coef[i]),r)));
1339 
1340  //in this way it should crash on programming errors, yeah
1341  number temp=n_Gcd(coef, pGetCoeff(bucket->coef[i]),r);
1342  n_Delete(&coef,r );
1343  coef=temp;
1344  if (nIsPseudoUnit(coef,r))
1345  {
1346  n_Delete(&coef,r);
1347  return;
1348  }
1349  assume(!(n_IsZero(coef,r)));
1350  }
1351  }
1352  if (n_IsZero(coef,r))
1353  {
1354  n_Delete(&coef,r);
1355  return;
1356  }
1357  if (TEST_OPT_PROT)
1358  PrintS("S");
1359  for(i=0;i<=MAX_BUCKET;i++)
1360  {
1361  if (bucket->buckets[i]!=NULL)
1362  {
1363  assume(!(n_IsZero(coef,r)));
1364  assume(bucket->coef[i]!=NULL);
1365  number lc=p_GetCoeff(bucket->coef[i],r);
1366  p_SetCoeff(bucket->coef[i], n_ExactDiv(lc,coef,r),r);
1367  assume(!(n_IsZero(p_GetCoeff(bucket->coef[i],r),r)));
1368  }
1369  }
1370  n_Delete(&coef,r);
1371 }
1372 #endif
1373 
1374 
1376 {
1377  assume(bucket->buckets[i]!=NULL);
1378 
1379  poly p=bucket->buckets[i];
1380  bucket->buckets_length[i]--;
1381 #ifdef USE_COEF_BUCKETS
1382  ring r=bucket->bucket_ring;
1383  if (bucket->coef[i]!=NULL)
1384  {
1385  poly next=pNext(p);
1386  if (next==NULL)
1387  {
1388  MULTIPLY_BUCKET(bucket,i);
1389  p=bucket->buckets[i];
1390  bucket->buckets[i]=NULL;
1391  return p;
1392  }
1393  else
1394  {
1395  bucket->buckets[i]=next;
1396  number c=p_GetCoeff(bucket->coef[i],r);
1397  pNext(p)=NULL;
1398  p=__p_Mult_nn(p,c,r);
1399  assume(p!=NULL);
1400  return p;
1401  }
1402  }
1403  else
1404 #endif
1405  {
1406  bucket->buckets[i]=pNext(bucket->buckets[i]);
1407  pNext(p)=NULL;
1408  assume(p!=NULL);
1409  return p;
1410  }
1411 }
1412 
1413 /*
1414 * input - output: a, b
1415 * returns:
1416 * a := a/gcd(a,b), b := b/gcd(a,b)
1417 * and return value
1418 * 0 -> a != 1, b != 1
1419 * 1 -> a == 1, b != 1
1420 * 2 -> a != 1, b == 1
1421 * 3 -> a == 1, b == 1
1422 * this value is used to control the spolys
1423 */
1424 int ksCheckCoeff(number *a, number *b, const coeffs r)
1425 {
1426  int c = 0;
1427  number an = *a, bn = *b;
1428  n_Test(an,r);
1429  n_Test(bn,r);
1430 
1431  number cn = n_SubringGcd(an, bn, r);
1432 
1433  if(n_IsOne(cn, r))
1434  {
1435  an = n_Copy(an, r);
1436  bn = n_Copy(bn, r);
1437  }
1438  else
1439  {
1440  an = n_ExactDiv(an, cn, r); n_Normalize(an,r);
1441  bn = n_ExactDiv(bn, cn, r); n_Normalize(bn,r);
1442  }
1443  n_Delete(&cn, r);
1444  if (n_IsOne(an, r))
1445  {
1446  c = 1;
1447  }
1448  if (n_IsOne(bn, r))
1449  {
1450  c += 2;
1451  }
1452  *a = an;
1453  *b = bn;
1454  return c;
1455 }
1456 
kBucket * kBucket_pt
Definition: ring.h:24
void kBucketClear(kBucket_pt bucket, poly *p, int *length)
Definition: kbuckets.cc:521
CFFList append(const CFFList &Inputlist, const CFFactor &TheFactor)
BOOLEAN kbTest(kBucket_pt bucket)
Tests.
Definition: kbuckets.cc:197
static FORCE_INLINE number n_Gcd(number a, number b, const coeffs r)
in Z: return the gcd of &#39;a&#39; and &#39;b&#39; in Z/nZ, Z/2^kZ: computed as in the case Z in Z/pZ...
Definition: coeffs.h:686
poly kBucketExtractLmOfBucket(kBucket_pt bucket, int i)
Definition: kbuckets.cc:1375
int j
Definition: facHensel.cc:105
#define MULTIPLY_BUCKET(B, I)
Definition: kbuckets.cc:43
void kBucketShallowCopyDelete(kBucket_pt bucket, ring new_tailRing, omBin new_tailBin, pShallowCopyDeleteProc p_shallow_copy_delete)
For changing the ring of the Bpoly to new_tailBin.
Definition: kbuckets.cc:535
void kBucketInit(kBucket_pt bucket, poly lm, int length)
Definition: kbuckets.cc:493
omBin_t * omBin
Definition: omStructs.h:12
number ndGcd(number, number, const coeffs r)
Definition: numbers.cc:161
static omBin kBucket_bin
Definition: kbuckets.cc:45
#define TEST_OPT_PROT
Definition: options.h:102
#define FALSE
Definition: auxiliary.h:94
void p_TakeOutComp(poly *p, long comp, poly *q, int *lq, const ring r)
Definition: p_polys.cc:3455
number kBucketPolyRed(kBucket_pt bucket, poly p1, int l1, poly spNoether)
Definition: kbuckets.cc:1079
static FORCE_INLINE BOOLEAN n_IsOne(number n, const coeffs r)
TRUE iff &#39;n&#39; represents the one element.
Definition: coeffs.h:468
poly p_NSet(number n, const ring r)
returns the poly representing the number n, destroys n
Definition: p_polys.cc:1455
static unsigned long p_SetComp(poly p, unsigned long c, ring r)
Definition: p_polys.h:246
#define p_GetComp(p, r)
Definition: monomials.h:64
static FORCE_INLINE number n_Init(long i, const coeffs r)
a number representing i in the given coeff field/ring r
Definition: coeffs.h:538
const poly kBucketGetLm(kBucket_pt bucket)
Definition: kbuckets.cc:506
int ksCheckCoeff(number *a, number *b)
int p_mFirstVblock(poly p, const ring ri)
Definition: shiftop.cc:469
#define MAX_BUCKET
Bucket definition (should be no one elses business, though)
Definition: kbuckets.h:175
#define TRUE
Definition: auxiliary.h:98
static BOOLEAN rField_is_Domain(const ring r)
Definition: ring.h:482
static int LOG4(int v)
Some internal stuff.
Definition: kbuckets.cc:56
static FORCE_INLINE void n_Normalize(number &n, const coeffs r)
inplace-normalization of n; produces some canonical representation of n;
Definition: coeffs.h:578
void kBucketNormalize(kBucket_pt bucket)
apply n_Normalize to all coefficients
static char const ** rParameter(const ring r)
(r->cf->parameter)
Definition: ring.h:619
#define loop
Definition: structs.h:80
static number & pGetCoeff(poly p)
return an alias to the leading coefficient of p assumes that p != NULL NOTE: not copy ...
Definition: monomials.h:44
void kBucket_Mult_n(kBucket_pt bucket, number n)
Multiply Bucket by number ,i.e. Bpoly == n*Bpoly.
Definition: kbuckets.cc:598
static number p_SetCoeff(poly p, number n, ring r)
Definition: p_polys.h:411
poly kBucketExtractLm(kBucket_pt bucket)
Definition: kbuckets.cc:511
CanonicalForm lc(const CanonicalForm &f)
int comp(const CanonicalForm &A, const CanonicalForm &B)
compare polynomials
#define pIter(p)
Definition: monomials.h:37
#define pFalseReturn(cond)
Definition: monomials.h:139
poly kBucket_ExtractLarger(kBucket_pt bucket, poly q, poly append)
Extract all monomials of bucket which are larger than q Append those to append, and return last monom...
Definition: kbuckets.cc:1004
CanonicalForm b
Definition: cfModGcd.cc:4044
Coefficient rings, fields and other domains suitable for Singular polynomials.
void kBucketDestroy(kBucket_pt *bucket_pt)
Definition: kbuckets.cc:216
static void p_SetCompP(poly p, int i, ring r)
Definition: p_polys.h:253
ring bucket_ring
Definition: kbuckets.h:192
#define assume(x)
Definition: mod2.h:390
static BOOLEAN rIsPluralRing(const ring r)
we must always have this test!
Definition: ring.h:397
static BOOLEAN p_IsConstant(const poly p, const ring r)
Definition: p_polys.h:1927
The main handler for Singular numbers which are suitable for Singular polynomials.
#define n_Test(a, r)
BOOLEAN n_Test(number a, const coeffs r)
Definition: coeffs.h:738
static BOOLEAN p_DivisibleBy(poly a, poly b, const ring r)
Definition: p_polys.h:1828
void kBucketSetLm(kBucket_pt bucket, poly lm)
All the auxiliary stuff.
static int p_LmCmp(poly p, poly q, const ring r)
Definition: p_polys.h:1496
int m
Definition: cfEzgcd.cc:121
static FORCE_INLINE number n_InpNeg(number n, const coeffs r)
in-place negation of n MUST BE USED: n = n_InpNeg(n) (no copy is returned)
Definition: coeffs.h:557
#define omCheckAddrBin(addr, bin)
Definition: omAllocDecl.h:325
int i
Definition: cfEzgcd.cc:125
void PrintS(const char *s)
Definition: reporter.cc:284
static void p_ExpVectorSub(poly p1, poly p2, const ring r)
Definition: p_polys.h:1375
static poly p_Mult_nn(poly p, number n, const ring r)
Definition: p_polys.h:913
static unsigned pLength(poly a)
Definition: p_polys.h:191
void kBucket_Minus_m_Mult_p(kBucket_pt bucket, poly m, poly p, int *l, poly spNoether)
Bpoly == Bpoly - m*p; where m is a monom Does not destroy p and m assume (*l <= 0 || pLength(p) == *l...
Definition: kbuckets.cc:716
static FORCE_INLINE BOOLEAN n_IsZero(number n, const coeffs r)
TRUE iff &#39;n&#39; represents the zero element.
Definition: coeffs.h:464
void kBucketAdjust(kBucket_pt bucket, int i)
Bucket number i from bucket is out of length sync, resync.
Definition: kbuckets.cc:565
number p_InitContent(poly ph, const ring r)
Definition: p_polys.cc:2581
#define p_Test(p, r)
Definition: p_polys.h:162
static BOOLEAN rField_is_Zp(const ring r)
Definition: ring.h:495
void p_Normalize(poly p, const ring r)
Definition: p_polys.cc:3732
static void p_Delete(poly *p, const ring r)
Definition: p_polys.h:856
#define omAlloc0Bin(bin)
Definition: omAllocDecl.h:206
#define omGetSpecBin(size)
Definition: omBin.h:11
#define p_LmEqual(p1, p2, r)
Definition: p_polys.h:1647
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:37
poly(* pShallowCopyDeleteProc)(poly s_p, ring source_r, ring dest_r, omBin dest_bin)
returns a poly from dest_r which is a ShallowCopy of s_p from source_r assumes that source_r->N == de...
Definition: ring.h:44
#define __p_Mult_nn(p, n, r)
Definition: p_polys.h:926
CanonicalForm cf
Definition: cfModGcd.cc:4024
static BOOLEAN rField_is_Ring(const ring r)
Definition: ring.h:479
#define NULL
Definition: omList.c:12
static FORCE_INLINE number n_Copy(number n, const coeffs r)
return a copy of &#39;n&#39;
Definition: coeffs.h:451
void kBucket_Plus_mm_Mult_pp(kBucket_pt bucket, poly m, poly p, int l)
Bpoly == Bpoly + m*p; where m is a monom Does not destroy p and m assume (l <= 0 || pLength(p) == l) ...
Definition: kbuckets.cc:821
int gcd(int a, int b)
Definition: walkSupport.cc:836
static BOOLEAN length(leftv result, leftv arg)
Definition: interval.cc:263
int l
Definition: kbuckets.h:183
#define pNext(p)
Definition: monomials.h:36
poly p
Definition: kbuckets.h:182
static FORCE_INLINE number n_ExactDiv(number a, number b, const coeffs r)
assume that there is a canonical subring in cf and we know that division is possible for these a and ...
Definition: coeffs.h:622
static void p_Setm(poly p, const ring r)
Definition: p_polys.h:232
#define pSetCoeff0(p, n)
Definition: monomials.h:59
#define p_GetCoeff(p, r)
Definition: monomials.h:50
static poly p_Minus_mm_Mult_qq(poly p, const poly m, const poly q, int &lp, int lq, const poly spNoether, const ring r)
Definition: p_polys.h:1005
static unsigned int pLogLength(unsigned int l)
Definition: kbuckets.cc:71
static FORCE_INLINE number n_SubringGcd(number a, number b, const coeffs r)
Definition: coeffs.h:688
Definition: lq.h:39
int dReportError(const char *fmt,...)
Definition: dError.cc:43
static void p_LmDelete(poly p, const ring r)
Definition: p_polys.h:710
#define p_SetCoeff0(p, n, r)
Definition: monomials.h:60
static int SI_LOG2(int v)
Definition: auxiliary.h:119
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete &#39;p&#39;
Definition: coeffs.h:455
static FORCE_INLINE BOOLEAN n_IsMOne(number n, const coeffs r)
TRUE iff &#39;n&#39; represents the additive inverse of the one element, i.e. -1.
Definition: coeffs.h:472
int p
Definition: cfModGcd.cc:4019
static poly p_Add_q(poly p, poly q, const ring r)
Definition: p_polys.h:891
BOOLEAN kBucketIsCleared(kBucket_pt bucket)
#define omFreeBin(addr, bin)
Definition: omAllocDecl.h:259
kBucket_pt kBucketCreate(const ring bucket_ring)
Creation/Destruction of buckets.
Definition: kbuckets.cc:209
int BOOLEAN
Definition: auxiliary.h:85
int kBucketCanonicalize(kBucket_pt bucket)
Canonicalizes Bpoly, i.e. converts polys of buckets into one poly in one bucket: Returns number of bu...
static FORCE_INLINE int n_Size(number n, const coeffs r)
return a non-negative measure for the complexity of n; return 0 only when n represents zero; (used fo...
Definition: coeffs.h:570
void kBucketDeleteAndDestroy(kBucket_pt *bucket_pt)
Definition: kbuckets.cc:223
void kBucketTakeOutComp(kBucket_pt bucket, long comp, poly *r_p, int *l)
Definition: kbuckets.cc:1038
void kBucketSimpleContent(kBucket_pt bucket)
Definition: kbuckets.cc:1203
int l
Definition: cfEzgcd.cc:93
static poly p_Plus_mm_Mult_qq(poly p, poly m, poly q, int &lp, int lq, const ring r)
Definition: p_polys.h:1118
void k_SplitFrame(poly &m1, poly &m2, int at, const ring r)
Definition: shiftop.cc:585
void kBucket_Add_q(kBucket_pt bucket, poly q, int *l)
Add to Bucket a poly ,i.e. Bpoly == q+Bpoly.
Definition: kbuckets.cc:654
ListNode * next
Definition: janet.h:31