source: git/libpolys/polys/monomials/p_polys.h @ c45b2dc

spielwiese
Last change on this file since c45b2dc was c45b2dc, checked in by Martin Lee <martinlee84@…>, 12 years ago
deleted forward declaration of static inline functions for I/O
  • Property mode set to 100644
File size: 47.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/***************************************************************
5 *  File:    p_polys.h
6 *  Purpose: declaration of poly stuf which are independent of
7 *           currRing
8 *  Author:  obachman (Olaf Bachmann)
9 *  Created: 9/00
10 *  Version: $Id$
11 *******************************************************************/
12#ifndef P_POLYS_H
13#define P_POLYS_H
14
15#include <polys/monomials/ring.h>
16#include <polys/monomials/monomials.h>
17#include <polys/monomials/polys-impl.h>
18#include <polys/templates/p_Procs.h>
19#include <polys/templates/p_Procs.h>
20#include <polys/sbuckets.h>
21
22/***************************************************************
23 *
24 * Primitives for accessing and setting fields of a poly
25 * poly must be != NULL
26 *
27 ***************************************************************/
28// next
29#define pNext(p)            ((p)->next)
30#define pIter(p)            ((p) = (p)->next)
31
32// coeff
33#define pGetCoeff(p)        ((p)->coef)
34// deletes old coeff before setting the new one
35#define pSetCoeff0(p,n)     (p)->coef=(n)
36#define p_GetCoeff(p,r)     pGetCoeff(p)
37#define p_SetCoeff0(p,n,r)  pSetCoeff0(p,n)
38
39/***************************************************************
40 *
41 * Comparisons: they are all done without regarding coeffs
42 *
43 ***************************************************************/
44#define p_LmCmpAction(p, q, r, actionE, actionG, actionS) \
45  _p_LmCmpAction(p, q, r, actionE, actionG, actionS)
46
47// returns 1 if ExpVector(p)==ExpVector(q): does not compare numbers !!
48#define p_LmEqual(p1, p2, r) p_ExpVectorEqual(p1, p2, r)
49
50/***************************************************************
51 *
52 * Divisiblity tests, args must be != NULL, except for
53 * pDivisbleBy
54 *
55 ***************************************************************/
56unsigned long p_GetShortExpVector(poly a, ring r);
57
58/***************************************************************
59 *
60 * Misc things on polys
61 *
62 ***************************************************************/
63// return the maximal exponent of p in form of the maximal long var
64unsigned long p_GetMaxExpL(poly p, const ring r, unsigned long l_max = 0);
65// return monomial r such that GetExp(r,i) is maximum of all
66// monomials in p; coeff == 0, next == NULL, ord is not set
67poly p_GetMaxExpP(poly p, ring r);
68
69int p_MinDeg(poly p,intvec *w, const ring R);
70
71long p_DegW(poly p, const short *w, const ring R);
72
73// return TRUE if all monoms have the same component
74BOOLEAN   p_OneComp(poly p, ring r);
75
76// return i, if head depends only on var(i)
77int       p_IsPurePower(const poly p, const ring r);
78
79// return i, if poly depends only on var(i)
80int       p_IsUnivariate(poly p, const ring r);
81
82// set entry e[i] to 1 if var(i) occurs in p, ignore var(j) if e[j]>0
83// return #(e[i]>0)
84int      p_GetVariables(poly p, int * e, const ring r);
85
86// returns the poly representing the integer i
87poly      p_ISet(int i, ring r);
88
89// returns the poly representing the number n, destroys n
90poly      p_NSet(number n, ring r);
91
92/***************************************************************
93 *
94 * Copying/Deletion of polys: args may be NULL
95 *
96 ***************************************************************/
97
98// simply deletes monomials, does not free coeffs
99void p_ShallowDelete(poly *p, const ring r);
100
101
102
103/***************************************************************
104 *
105 * Copying/Deleteion of polys: args may be NULL
106 *  - p/q as arg mean a poly
107 *  - m a monomial
108 *  - n a number
109 *  - pp (resp. qq, mm, nn) means arg is constant
110 *  - p (resp, q, m, n)     means arg is destroyed
111 *
112 ***************************************************************/
113
114poly      p_Sub(poly a, poly b, const ring r);
115
116poly      p_Power(poly p, int i, const ring r);
117/***************************************************************
118 *
119 * Misc stuff
120 *
121 ***************************************************************/
122/*2
123* returns the length of a (numbers of monomials)
124*/
125static inline int pLength(poly a)
126{
127  int l = 0;
128  while (a!=NULL)
129  {
130    pIter(a);
131    l++;
132  }
133  return l;
134}
135
136void      p_Norm(poly p1, const ring r);
137void      p_Normalize(poly p,const ring r);
138
139void      p_Content(poly p, const ring r);
140//void      p_SimpleContent(poly p, int s, const ring r);
141
142poly      p_Cleardenom(poly p, const ring r);
143void      p_Cleardenom_n(poly p, const ring r,number &c);
144number    p_GetAllDenom(poly ph, const ring r);
145
146int       pSize( poly p, const ring r );
147
148// homogenizes p by multiplying certain powers of the varnum-th variable
149poly      p_Homogen (poly p, int varnum, const ring r);
150
151BOOLEAN   p_IsHomogeneous (poly p, const ring r);
152
153static inline void p_Setm(poly p, const ring r);
154p_SetmProc p_GetSetmProc(ring r);
155
156poly      p_Subst(poly p, int n, poly e, const ring r);
157
158// TODO:
159#define p_SetmComp  p_Setm
160
161// component
162static inline  unsigned long p_SetComp(poly p, unsigned long c, ring r)
163{
164  p_LmCheckPolyRing2(p, r);
165  pAssume2(rRing_has_Comp(r));
166  __p_GetComp(p,r) = c;
167  return c;
168}
169// sets component of poly a to i, returns length of p
170static inline   void p_SetCompP(poly p, int i, ring r)
171{
172  if (p != NULL)
173  {
174#ifdef PDEBUG
175    poly q = p;
176    int l = 0;
177#endif
178
179    if (rOrd_SetCompRequiresSetm(r))
180    {
181      do
182      {
183        p_SetComp(p, i, r);
184        p_SetmComp(p, r);
185#ifdef PDEBUG
186        l++;
187#endif
188        pIter(p);
189      }
190      while (p != NULL);
191    }
192    else
193    {
194      do
195      {
196        p_SetComp(p, i, r);
197#ifdef PDEBUG
198        l++;
199#endif
200        pIter(p);
201      }
202      while(p != NULL);
203    }
204#ifdef PDEBUG
205    p_Test(q, r);
206    assume(l == pLength(q));
207#endif
208  }
209}
210
211static inline   void p_SetCompP(poly p, int i, ring lmRing, ring tailRing)
212{
213  if (p != NULL)
214  {
215    p_SetComp(p, i, lmRing);
216    p_SetmComp(p, lmRing);
217    p_SetCompP(pNext(p), i, tailRing);
218  }
219}
220
221// returns maximal column number in the modul element a (or 0)
222static inline long p_MaxComp(poly p, ring lmRing, ring tailRing)
223{
224  long result,i;
225
226  if(p==NULL) return 0;
227  result = p_GetComp(p, lmRing);
228  if (result != 0)
229  {
230    loop
231    {
232      pIter(p);
233      if(p==NULL) break;
234      i = p_GetComp(p, tailRing);
235      if (i>result) result = i;
236    }
237  }
238  return result;
239}
240
241static inline long p_MaxComp(poly p,ring lmRing) {return p_MaxComp(p,lmRing,lmRing);}
242
243static inline   long p_MinComp(poly p, ring lmRing, ring tailRing)
244{
245  long result,i;
246
247  if(p==NULL) return 0;
248  result = p_GetComp(p,lmRing);
249  if (result != 0)
250  {
251    loop
252    {
253      pIter(p);
254      if(p==NULL) break;
255      i = p_GetComp(p,tailRing);
256      if (i<result) result = i;
257    }
258  }
259  return result;
260}
261
262static inline long p_MinComp(poly p,ring lmRing) {return p_MinComp(p,lmRing,lmRing);}
263
264/***************************************************************
265 *
266 * poly things which are independent of ring
267 *
268 ***************************************************************/
269static inline poly      pLast(poly a, int &length);
270inline   poly      pLast(poly a) { int l; return pLast(a, l);}
271static inline poly pReverse(poly p)
272{
273  if (p == NULL || pNext(p) == NULL) return p;
274
275  poly q = pNext(p), // == pNext(p)
276    qn;
277  pNext(p) = NULL;
278  do
279  {
280    qn = pNext(q);
281    pNext(q) = p;
282    p = q;
283    q = qn;
284  }
285  while (qn != NULL);
286  return p;
287}
288void      pEnlargeSet(poly**p, int length, int increment);
289
290
291/***************************************************************
292 *
293 * I/O
294 *
295 ***************************************************************/
296char*     p_String(poly p, ring lmRing, ring tailRing);
297char*     p_String0(poly p, ring lmRing, ring tailRing);
298void      p_Write(poly p, ring lmRing, ring tailRing);
299void      p_Write0(poly p, ring lmRing, ring tailRing);
300void      p_wrp(poly p, ring lmRing, ring tailRing);
301
302/***************************************************************
303 *
304 * Degree stuff -- see p_polys.cc for explainations
305 *
306 ***************************************************************/
307extern pLDegProc pLDeg;
308extern pFDegProc pFDeg;
309long p_WFirstTotalDegree(poly p, ring r);
310long p_WTotaldegree(poly p, const ring r);
311long p_WDegree(poly p,const ring r);
312long pLDeg0(poly p,int *l, ring r);
313long pLDeg0c(poly p,int *l, ring r);
314long pLDegb(poly p,int *l, ring r);
315long pLDeg1(poly p,int *l, ring r);
316long pLDeg1c(poly p,int *l, ring r);
317long pLDeg1_Deg(poly p,int *l, ring r);
318long pLDeg1c_Deg(poly p,int *l, ring r);
319long pLDeg1_Totaldegree(poly p,int *l, ring r);
320long pLDeg1c_Totaldegree(poly p,int *l, ring r);
321long pLDeg1_WFirstTotalDegree(poly p,int *l, ring r);
322long pLDeg1c_WFirstTotalDegree(poly p,int *l, ring r);
323BOOLEAN p_EqualPolys(poly p1, poly p2, const ring r);
324
325long p_Deg(poly a, const ring r);
326/***************************************************************
327 *
328 * PDEBUG stuff
329 *
330 ***************************************************************/
331#ifdef PDEBUG
332// Returns TRUE if m is monom of p, FALSE otherwise
333BOOLEAN pIsMonomOf(poly p, poly m);
334// Returns TRUE if p and q have common monoms
335BOOLEAN pHaveCommonMonoms(poly p, poly q);
336
337// p_Check* routines return TRUE if everything is ok,
338// else, they report error message and return false
339
340// check if Lm(p) is from ring r
341BOOLEAN p_LmCheckIsFromRing(poly p, ring r);
342// check if Lm(p) != NULL, r != NULL and initialized && Lm(p) is from r
343BOOLEAN p_LmCheckPolyRing(poly p, ring r);
344// check if all monoms of p are from ring r
345BOOLEAN p_CheckIsFromRing(poly p, ring r);
346// check r != NULL and initialized && all monoms of p are from r
347BOOLEAN p_CheckPolyRing(poly p, ring r);
348// check if r != NULL and initialized
349BOOLEAN p_CheckRing(ring r);
350// only do check if cond
351
352
353#define pIfThen(cond, check) do {if (cond) {check;}} while (0)
354
355BOOLEAN _p_Test(poly p, ring r, int level);
356BOOLEAN _p_LmTest(poly p, ring r, int level);
357BOOLEAN _pp_Test(poly p, ring lmRing, ring tailRing, int level);
358
359#define p_Test(p,r)     _p_Test(p, r, PDEBUG)
360#define p_LmTest(p,r)   _p_LmTest(p, r, PDEBUG)
361#define pp_Test(p, lmRing, tailRing)    _pp_Test(p, lmRing, tailRing, PDEBUG)
362
363#else // ! PDEBUG
364
365#define pIsMonomOf(p, q)        (TRUE)
366#define pHaveCommonMonoms(p, q) (TRUE)
367#define p_LmCheckIsFromRing(p,r)  ((void)0)
368#define p_LmCheckPolyRing(p,r)    ((void)0)
369#define p_CheckIsFromRing(p,r)  ((void)0)
370#define p_CheckPolyRing(p,r)    ((void)0)
371#define p_CheckRing(r)          ((void)0)
372#define P_CheckIf(cond, check)  ((void)0)
373
374#define p_Test(p,r)     (1)
375#define p_LmTest(p,r)   (1)
376#define pp_Test(p, lmRing, tailRing) (1)
377
378#endif
379
380/***************************************************************
381 *
382 * Primitives for accessing and setting fields of a poly
383 *
384 ***************************************************************/
385
386static inline number p_SetCoeff(poly p, number n, ring r)
387{
388  p_LmCheckPolyRing2(p, r);
389  n_Delete(&(p->coef), r->cf);
390  (p)->coef=n;
391  return n;
392}
393
394// order
395static inline long p_GetOrder(poly p, ring r)
396{
397  p_LmCheckPolyRing2(p, r);
398  if (r->typ==NULL) return ((p)->exp[r->pOrdIndex]);
399  int i=0;
400  loop
401  {
402    switch(r->typ[i].ord_typ)
403    {
404      case ro_wp_neg:
405        return (((long)((p)->exp[r->pOrdIndex]))-POLY_NEGWEIGHT_OFFSET);
406      case ro_syzcomp:
407      case ro_syz:
408      case ro_cp:
409        i++;
410        break;
411      //case ro_dp:
412      //case ro_wp:
413      default:
414        return ((p)->exp[r->pOrdIndex]);
415    }
416  }
417}
418
419// Setm
420static inline void p_Setm(poly p, const ring r)
421{
422  p_CheckRing2(r);
423  r->p_Setm(p, r);
424}
425
426
427static inline unsigned long p_AddComp(poly p, unsigned long v, ring r)
428{
429  p_LmCheckPolyRing2(p, r);
430  pAssume2(rRing_has_Comp(r));
431  return __p_GetComp(p,r) += v;
432}
433static inline unsigned long p_SubComp(poly p, unsigned long v, ring r)
434{
435  p_LmCheckPolyRing2(p, r);
436  pAssume2(rRing_has_Comp(r));
437  _pPolyAssume2(__p_GetComp(p,r) >= v,p,r);
438  return __p_GetComp(p,r) -= v;
439}
440static inline int p_Comp_k_n(poly a, poly b, int k, ring r)
441{
442  if ((a==NULL) || (b==NULL) ) return FALSE;
443  p_LmCheckPolyRing2(a, r);
444  p_LmCheckPolyRing2(b, r);
445  pAssume2(k > 0 && k <= r->N);
446  int i=k;
447  for(;i<=r->N;i++)
448  {
449    if (p_GetExp(a,i,r) != p_GetExp(b,i,r)) return FALSE;
450    //    if (a->exp[(r->VarOffset[i] & 0xffffff)] != b->exp[(r->VarOffset[i] & 0xffffff)]) return FALSE;
451  }
452  return TRUE;
453}
454
455#ifndef HAVE_EXPSIZES
456
457/// get a single variable exponent
458/// @Note:
459/// the integer VarOffset encodes:
460/// 1. the position of a variable in the exponent vector p->exp (lower 24 bits)
461/// 2. number of bits to shift to the right in the upper 8 bits (which takes at most 6 bits for 64 bit)
462/// Thus VarOffset always has 2 zero higher bits!
463static inline long p_GetExp(const poly p, const unsigned long iBitmask, const int VarOffset)
464{
465  pAssume2((VarOffset >> (24 + 6)) == 0);
466#if 0
467  int pos=(VarOffset & 0xffffff);
468  int bitpos=(VarOffset >> 24);
469  unsigned long exp=(p->exp[pos] >> bitmask) & iBitmask;
470  return exp;
471#else
472  return (long)
473         ((p->exp[(VarOffset & 0xffffff)] >> (VarOffset >> 24))
474          & iBitmask);
475#endif
476}
477
478
479/// set a single variable exponent
480/// @Note:
481/// VarOffset encodes the position in p->exp @see p_GetExp
482static inline unsigned long p_SetExp(poly p, const unsigned long e, const unsigned long iBitmask, const int VarOffset)
483{
484  pAssume2(e>=0);
485  pAssume2(e<=iBitmask);
486  pAssume2((VarOffset >> (24 + 6)) == 0);
487
488  // shift e to the left:
489  register int shift = VarOffset >> 24;
490  unsigned long ee = e << shift /*(VarOffset >> 24)*/;
491  // find the bits in the exponent vector
492  register int offset = (VarOffset & 0xffffff);
493  // clear the bits in the exponent vector:
494  p->exp[offset]  &= ~( iBitmask << shift );
495  // insert e with |
496  p->exp[ offset ] |= ee;
497  return e;
498}
499
500
501#else // #ifdef HAVE_EXPSIZES // EXPERIMENTAL!!!
502
503static inline unsigned long BitMask(unsigned long bitmask, int twobits)
504{
505  // bitmask = 00000111111111111
506  // 0 must give bitmask!
507  // 1, 2, 3 - anything like 00011..11
508  pAssume2((twobits >> 2) == 0);
509  static const unsigned long _bitmasks[4] = {-1, 0x7fff, 0x7f, 0x3};
510  return bitmask & _bitmasks[twobits];
511}
512
513
514/// @Note: we may add some more info (6 ) into VarOffset and thus encode
515static inline long p_GetExp(const poly p, const unsigned long iBitmask, const int VarOffset)
516{
517  int pos  =(VarOffset & 0xffffff);
518  int hbyte= (VarOffset >> 24); // the highest byte
519  int bitpos = hbyte & 0x3f; // last 6 bits
520  long bitmask = BitMask(iBitmask, hbyte >> 6);
521
522  long exp=(p->exp[pos] >> bitpos) & bitmask;
523  return exp;
524
525}
526
527static inline long p_SetExp(poly p, const long e, const unsigned long iBitmask, const int VarOffset)
528{
529  pAssume2(e>=0);
530  pAssume2(e <= BitMask(iBitmask, VarOffset >> 30));
531
532  // shift e to the left:
533  register int hbyte = VarOffset >> 24;
534  int bitmask = BitMask(iBitmask, hbyte >> 6);
535  register int shift = hbyte & 0x3f;
536  long ee = e << shift;
537  // find the bits in the exponent vector
538  register int offset = (VarOffset & 0xffffff);
539  // clear the bits in the exponent vector:
540  p->exp[offset]  &= ~( bitmask << shift );
541  // insert e with |
542  p->exp[ offset ] |= ee;
543  return e;
544}
545
546#endif // #ifndef HAVE_EXPSIZES
547
548
549static inline long p_GetExp(const poly p, const ring r, const int VarOffset)
550{
551  p_LmCheckPolyRing2(p, r);
552  pAssume2(VarOffset != -1);
553  return p_GetExp(p, r->bitmask, VarOffset);
554}
555
556static inline long p_SetExp(poly p, const long e, const ring r, const int VarOffset)
557{
558  p_LmCheckPolyRing2(p, r);
559  pAssume2(VarOffset != -1);
560  return p_SetExp(p, e, r->bitmask, VarOffset);
561}
562
563
564
565/// get v^th exponent for a monomial
566static inline long p_GetExp(const poly p, const int v, const ring r)
567{
568  p_LmCheckPolyRing2(p, r);
569  pAssume2(v>0 && v <= r->N);
570  pAssume2(r->VarOffset[v] != -1);
571  return p_GetExp(p, r->bitmask, r->VarOffset[v]);
572}
573
574
575/// set v^th exponent for a monomial
576static inline long p_SetExp(poly p, const int v, const long e, const ring r)
577{
578  p_LmCheckPolyRing2(p, r);
579  pAssume2(v>0 && v <= r->N);
580  pAssume2(r->VarOffset[v] != -1);
581  return p_SetExp(p, e, r->bitmask, r->VarOffset[v]);
582}
583
584
585
586
587
588// the following should be implemented more efficiently
589static inline  long p_IncrExp(poly p, int v, ring r)
590{
591  p_LmCheckPolyRing2(p, r);
592  int e = p_GetExp(p,v,r);
593  e++;
594  return p_SetExp(p,v,e,r);
595}
596static inline  long p_DecrExp(poly p, int v, ring r)
597{
598  p_LmCheckPolyRing2(p, r);
599  int e = p_GetExp(p,v,r);
600  pAssume2(e > 0);
601  e--;
602  return p_SetExp(p,v,e,r);
603}
604static inline  long p_AddExp(poly p, int v, long ee, ring r)
605{
606  p_LmCheckPolyRing2(p, r);
607  int e = p_GetExp(p,v,r);
608  e += ee;
609  return p_SetExp(p,v,e,r);
610}
611static inline  long p_SubExp(poly p, int v, long ee, ring r)
612{
613  p_LmCheckPolyRing2(p, r);
614  long e = p_GetExp(p,v,r);
615  pAssume2(e >= ee);
616  e -= ee;
617  return p_SetExp(p,v,e,r);
618}
619static inline  long p_MultExp(poly p, int v, long ee, ring r)
620{
621  p_LmCheckPolyRing2(p, r);
622  long e = p_GetExp(p,v,r);
623  e *= ee;
624  return p_SetExp(p,v,e,r);
625}
626
627static inline long p_GetExpSum(poly p1, poly p2, int i, ring r)
628{
629  p_LmCheckPolyRing2(p1, r);
630  p_LmCheckPolyRing2(p2, r);
631  return p_GetExp(p1,i,r) + p_GetExp(p2,i,r);
632}
633static inline long p_GetExpDiff(poly p1, poly p2, int i, ring r)
634{
635  return p_GetExp(p1,i,r) - p_GetExp(p2,i,r);
636}
637
638
639/***************************************************************
640 *
641 * Allocation/Initalization/Deletion
642 *
643 ***************************************************************/
644static inline poly p_New(ring r, omBin bin)
645{
646  p_CheckRing2(r);
647  pAssume2(bin != NULL && r->PolyBin->sizeW == bin->sizeW);
648  poly p;
649  omTypeAllocBin(poly, p, bin);
650  p_SetRingOfLm(p, r);
651  return p;
652}
653
654static inline poly p_New(ring r)
655{
656  return p_New(r, r->PolyBin);
657}
658
659static inline void p_LmFree(poly p, ring r)
660{
661  p_LmCheckPolyRing2(p, r);
662  omFreeBinAddr(p);
663}
664static inline void p_LmFree(poly *p, ring r)
665{
666  p_LmCheckPolyRing2(*p, r);
667  poly h = *p;
668  *p = pNext(h);
669  omFreeBinAddr(h);
670}
671static inline poly p_LmFreeAndNext(poly p, ring r)
672{
673  p_LmCheckPolyRing2(p, r);
674  poly pnext = pNext(p);
675  omFreeBinAddr(p);
676  return pnext;
677}
678static inline void p_LmDelete(poly p, const ring r)
679{
680  p_LmCheckPolyRing2(p, r);
681  n_Delete(&pGetCoeff(p), r->cf);
682  omFreeBinAddr(p);
683}
684static inline void p_LmDelete(poly *p, const ring r)
685{
686  p_LmCheckPolyRing2(*p, r);
687  poly h = *p;
688  *p = pNext(h);
689  n_Delete(&pGetCoeff(h), r->cf);
690  omFreeBinAddr(h);
691}
692static inline poly p_LmDeleteAndNext(poly p, const ring r)
693{
694  p_LmCheckPolyRing2(p, r);
695  poly pnext = pNext(p);
696  n_Delete(&pGetCoeff(p), r->cf);
697  omFreeBinAddr(p);
698  return pnext;
699}
700
701/***************************************************************
702 *
703 * Misc routines
704 *
705 ***************************************************************/
706
707// pCmp: args may be NULL
708// returns: (p2==NULL ? 1 : (p1 == NULL ? -1 : p_LmCmp(p1, p2)))
709static inline int p_Cmp(poly p1, poly p2, ring r)
710{
711  if (p2==NULL)
712    return 1;
713  if (p1==NULL)
714    return -1;
715  return p_LmCmp(p1,p2,r);
716}
717
718static inline unsigned long p_GetMaxExp(const poly p, const ring r)
719{
720  return p_GetMaxExp(p_GetMaxExpL(p, r), r);
721}
722
723static inline unsigned long p_GetMaxExp(const unsigned long l, const ring r)
724{
725  unsigned long bitmask = r->bitmask;
726  unsigned long max = (l & bitmask);
727  unsigned long j = r->ExpPerLong - 1;
728
729  if (j > 0)
730  {
731    unsigned long i = r->BitsPerExp;
732    long e;
733    loop
734    {
735      e = ((l >> i) & bitmask);
736      if ((unsigned long) e > max)
737        max = e;
738      j--;
739      if (j==0) break;
740      i += r->BitsPerExp;
741    }
742  }
743  return max;
744}
745
746static inline unsigned long
747p_GetTotalDegree(const unsigned long l, const ring r, const int number_of_exps)
748{
749  const unsigned long bitmask = r->bitmask;
750  unsigned long sum = (l & bitmask);
751  unsigned long j = number_of_exps - 1;
752
753  if (j > 0)
754  {
755    unsigned long i = r->BitsPerExp;
756    loop
757    {
758      sum += ((l >> i) & bitmask);
759      j--;
760      if (j==0) break;
761      i += r->BitsPerExp;
762    }
763  }
764  return sum;
765}
766
767static inline unsigned long
768p_GetTotalDegree(const unsigned long l, const ring r)
769{
770  return p_GetTotalDegree(l, r, r->ExpPerLong);
771}
772
773/***************************************************************
774 *
775 * Dispatcher to r->p_Procs, they do the tests/checks
776 *
777 ***************************************************************/
778// returns a copy of p
779static inline poly p_Copy(poly p, const ring r)
780{
781#ifdef PDEBUG
782  poly pp= r->p_Procs->p_Copy(p, r);
783  p_Test(pp,r);
784  return pp;
785#else
786  return r->p_Procs->p_Copy(p, r);
787#endif
788}
789
790static inline poly p_Head(poly p, const ring r)
791{
792  if (p == NULL) return NULL;
793  p_LmCheckPolyRing1(p, r);
794  poly np;
795  omTypeAllocBin(poly, np, r->PolyBin);
796  p_SetRingOfLm(np, r);
797  p_MemCopy_LengthGeneral(np->exp, p->exp, r->ExpL_Size);
798  pNext(np) = NULL;
799  pSetCoeff0(np, n_Copy(pGetCoeff(p), r->cf));
800  return np;
801}
802
803// returns a copy of p with Lm(p) from lmRing and Tail(p) from tailRing
804static inline poly p_Copy(poly p, const ring lmRing, const ring tailRing)
805{
806#ifndef PDEBUG
807  if (tailRing == lmRing)
808    return tailRing->p_Procs->p_Copy(p, tailRing);
809#endif
810  if (p != NULL)
811  {
812    poly pres = p_Head(p, lmRing);
813    pNext(pres) = tailRing->p_Procs->p_Copy(pNext(p), tailRing);
814    return pres;
815  }
816  else
817    return NULL;
818}
819
820// deletes *p, and sets *p to NULL
821static inline void p_Delete(poly *p, const ring r)
822{
823  r->p_Procs->p_Delete(p, r);
824}
825
826static inline void p_Delete(poly *p,  const ring lmRing, const ring tailRing)
827{
828#ifndef PDEBUG
829  if (tailRing == lmRing)
830  {
831    tailRing->p_Procs->p_Delete(p, tailRing);
832    return;
833  }
834#endif
835  if (*p != NULL)
836  {
837    if (pNext(*p) != NULL)
838      tailRing->p_Procs->p_Delete(&pNext(*p), tailRing);
839    p_LmDelete(p, lmRing);
840  }
841}
842
843// copys monomials of p, allocates new monomials from bin,
844// deletes monomoals of p
845static inline poly p_ShallowCopyDelete(poly p, const ring r, omBin bin)
846{
847  p_LmCheckPolyRing2(p, r);
848  pAssume2(r->PolyBin->sizeW == bin->sizeW);
849  return r->p_Procs->p_ShallowCopyDelete(p, r, bin);
850}
851
852// returns p+q, destroys p and q
853static inline poly p_Add_q(poly p, poly q, const ring r)
854{
855  int shorter;
856  return r->p_Procs->p_Add_q(p, q, shorter, r);
857}
858
859/// like p_Add_q, except that if lp == pLength(lp) lq == pLength(lq) then lp == pLength(p+q)
860static inline poly p_Add_q(poly p, poly q, int &lp, int lq, const ring r)
861{
862  int shorter;
863  poly res = r->p_Procs->p_Add_q(p, q, shorter, r);
864  lp = (lp + lq) - shorter;
865  return res;
866}
867
868// returns p*n, destroys p
869static inline poly p_Mult_nn(poly p, number n, const ring r)
870{
871  if (n_IsOne(n, r->cf))
872    return p;
873  else
874    return r->p_Procs->p_Mult_nn(p, n, r);
875}
876
877static inline poly p_Mult_nn(poly p, number n, const ring lmRing,
878                        const ring tailRing)
879{
880#ifndef PDEBUG
881  if (lmRing == tailRing)
882  {
883    return p_Mult_nn(p, n, tailRing);
884  }
885#endif
886  poly pnext = pNext(p);
887  pNext(p) = NULL;
888  p = lmRing->p_Procs->p_Mult_nn(p, n, lmRing);
889  pNext(p) = tailRing->p_Procs->p_Mult_nn(pnext, n, tailRing);
890  return p;
891}
892
893// returns p*n, does not destroy p
894static inline poly pp_Mult_nn(poly p, number n, const ring r)
895{
896  if (n_IsOne(n, r->cf))
897    return p_Copy(p, r);
898  else
899    return r->p_Procs->pp_Mult_nn(p, n, r);
900}
901
902// test if the monomial is a constant as a vector component
903// i.e., test if all exponents are zero
904static inline BOOLEAN p_LmIsConstantComp(const poly p, const ring r)
905{
906  //p_LmCheckPolyRing(p, r);
907  int i = r->VarL_Size - 1;
908
909  do
910  {
911    if (p->exp[r->VarL_Offset[i]] != 0)
912      return FALSE;
913    i--;
914  }
915  while (i >= 0);
916  return TRUE;
917}
918
919// test if monomial is a constant, i.e. if all exponents and the component
920// is zero
921static inline BOOLEAN p_LmIsConstant(const poly p, const ring r)
922{
923  if (p_LmIsConstantComp(p, r))
924    return (p_GetComp(p, r) == 0);
925  return FALSE;
926}
927
928// returns Copy(p)*m, does neither destroy p nor m
929static inline poly pp_Mult_mm(poly p, poly m, const ring r)
930{
931  if (p_LmIsConstant(m, r))
932    return pp_Mult_nn(p, pGetCoeff(m), r);
933  else
934  {
935    poly last;
936    return r->p_Procs->pp_Mult_mm(p, m, r, last);
937  }
938}
939
940// returns p*m, destroys p, const: m
941static inline poly p_Mult_mm(poly p, poly m, const ring r)
942{
943  if (p_LmIsConstant(m, r))
944    return p_Mult_nn(p, pGetCoeff(m), r);
945  else
946    return r->p_Procs->p_Mult_mm(p, m, r);
947}
948
949// return p - m*Copy(q), destroys p; const: p,m
950static inline poly p_Minus_mm_Mult_qq(poly p, poly m, poly q, const ring r)
951{
952#ifdef HAVE_PLURAL
953  if (rIsPluralRing(r))
954  {
955    int lp, lq;
956    poly spNoether;
957    return nc_p_Minus_mm_Mult_qq(p, m, q, lp, lq, spNoether, r);
958  }
959#endif
960
961  int shorter;
962  poly last;
963
964  return r->p_Procs->p_Minus_mm_Mult_qq(p, m, q, shorter, NULL, r, last); // !!!
965}
966
967// like p_Minus_mm_Mult_qq, except that if lp == pLength(lp) lq == pLength(lq)
968// then lp == pLength(p -m*q)
969static inline poly p_Minus_mm_Mult_qq(poly p, poly m, poly q, int &lp, int lq,
970                                 poly spNoether, const ring r)
971{
972#ifdef HAVE_PLURAL
973  if (rIsPluralRing(r))
974     return nc_p_Minus_mm_Mult_qq(p, m, q, lp, lq, spNoether, r);
975#endif
976
977  int shorter;
978  poly last,res;
979  res = r->p_Procs->p_Minus_mm_Mult_qq(p, m, q, shorter, spNoether, r, last);
980  lp = (lp + lq) - shorter;
981  return res;
982}
983
984// returns p*Coeff(m) for such monomials pm of p, for which m is divisble by pm
985static inline poly pp_Mult_Coeff_mm_DivSelect(poly p, const poly m, const ring r)
986{
987  int shorter;
988  return r->p_Procs->pp_Mult_Coeff_mm_DivSelect(p, m, shorter, r);
989}
990
991// returns p*Coeff(m) for such monomials pm of p, for which m is divisble by pm
992// if lp is length of p on input then lp is length of returned poly on output
993static inline poly pp_Mult_Coeff_mm_DivSelect(poly p, int &lp, const poly m, const ring r)
994{
995  int shorter;
996  poly pp = r->p_Procs->pp_Mult_Coeff_mm_DivSelect(p, m, shorter, r);
997  lp -= shorter;
998  return pp;
999}
1000
1001// returns -p, destroys p
1002static inline poly p_Neg(poly p, const ring r)
1003{
1004  return r->p_Procs->p_Neg(p, r);
1005}
1006
1007extern poly  _p_Mult_q(poly p, poly q, const int copy, const ring r);
1008// returns p*q, destroys p and q
1009static inline poly p_Mult_q(poly p, poly q, const ring r)
1010{
1011  if (p == NULL)
1012  {
1013    r->p_Procs->p_Delete(&q, r);
1014    return NULL;
1015  }
1016  if (q == NULL)
1017  {
1018    r->p_Procs->p_Delete(&p, r);
1019    return NULL;
1020  }
1021
1022  if (pNext(p) == NULL)
1023  {
1024#ifdef HAVE_PLURAL
1025    if (rIsPluralRing(r))
1026      q = nc_mm_Mult_p(p, q, r);
1027    else
1028#endif /* HAVE_PLURAL */
1029      q = r->p_Procs->p_Mult_mm(q, p, r);
1030
1031    r->p_Procs->p_Delete(&p, r);
1032    return q;
1033  }
1034
1035  if (pNext(q) == NULL)
1036  {
1037  // NEEDED
1038#ifdef HAVE_PLURAL
1039/*    if (rIsPluralRing(r))
1040      p = gnc_p_Mult_mm(p, q, r); // ???
1041    else*/
1042#endif /* HAVE_PLURAL */
1043      p = r->p_Procs->p_Mult_mm(p, q, r);
1044
1045    r->p_Procs->p_Delete(&q, r);
1046    return p;
1047  }
1048#ifdef HAVE_PLURAL
1049  if (rIsPluralRing(r))
1050    return _nc_p_Mult_q(p, q, r);
1051  else
1052#endif
1053  return _p_Mult_q(p, q, 0, r);
1054}
1055
1056// returns p*q, does neither destroy p nor q
1057static inline poly pp_Mult_qq(poly p, poly q, const ring r)
1058{
1059  poly last;
1060  if (p == NULL || q == NULL) return NULL;
1061
1062  if (pNext(p) == NULL)
1063  {
1064#ifdef HAVE_PLURAL
1065    if (rIsPluralRing(r))
1066      return nc_mm_Mult_pp(p, q, r);
1067#endif
1068    return r->p_Procs->pp_Mult_mm(q, p, r, last);
1069  }
1070
1071  if (pNext(q) == NULL)
1072  {
1073    return r->p_Procs->pp_Mult_mm(p, q, r, last);
1074  }
1075
1076  poly qq = q;
1077  if (p == q)
1078    qq = p_Copy(q, r);
1079
1080  poly res;
1081#ifdef HAVE_PLURAL
1082  if (rIsPluralRing(r))
1083    res = _nc_pp_Mult_qq(p, qq, r);
1084  else
1085#endif
1086    res = _p_Mult_q(p, qq, 1, r);
1087
1088  if (qq != q)
1089    p_Delete(&qq, r);
1090  return res;
1091}
1092
1093// returns p + m*q destroys p, const: q, m
1094static inline poly p_Plus_mm_Mult_qq(poly p, poly m, poly q, int &lp, int lq,
1095                                const ring r)
1096{
1097#ifdef HAVE_PLURAL
1098  if (rIsPluralRing(r))
1099    return nc_p_Plus_mm_Mult_qq(p, m, q, lp, lq, r);
1100#endif
1101
1102// this should be implemented more efficiently
1103  poly res, last;
1104  int shorter;
1105  number n_old = pGetCoeff(m);
1106  number n_neg = n_Copy(n_old, r->cf);
1107  n_neg = n_Neg(n_neg, r->cf);
1108  pSetCoeff0(m, n_neg);
1109  res = r->p_Procs->p_Minus_mm_Mult_qq(p, m, q, shorter, NULL, r, last);
1110  lp = (lp + lq) - shorter;
1111  pSetCoeff0(m, n_old);
1112  n_Delete(&n_neg, r->cf);
1113  return res;
1114}
1115
1116static inline poly p_Plus_mm_Mult_qq(poly p, poly m, poly q, const ring r)
1117{
1118  int lp = 0, lq = 0;
1119  return p_Plus_mm_Mult_qq(p, m, q, lp, lq, r);
1120}
1121
1122// returns merged p and q, assumes p and q have no monomials which are equal
1123static inline poly p_Merge_q(poly p, poly q, const ring r)
1124{
1125  return r->p_Procs->p_Merge_q(p, q, r);
1126}
1127
1128// like p_SortMerge, except that p may have equal monimals
1129static inline poly p_SortAdd(poly p, const ring r, BOOLEAN revert= FALSE)
1130{
1131  if (revert) p = pReverse(p);
1132  return sBucketSortAdd(p, r);
1133}
1134
1135// sorts p using bucket sort: returns sorted poly
1136// assumes that monomials of p are all different
1137// reverses it first, if revert == TRUE, use this if input p is "almost" sorted
1138// correctly
1139static inline poly p_SortMerge(poly p, const ring r, BOOLEAN revert= FALSE)
1140{
1141  if (revert) p = pReverse(p);
1142  return sBucketSortMerge(p, r);
1143}
1144
1145/***************************************************************
1146 *
1147 * I/O
1148 *
1149 ***************************************************************/
1150static inline char*     p_String(poly p, ring p_ring)
1151{
1152  return p_String(p, p_ring, p_ring);
1153}
1154static inline char*     p_String0(poly p, ring p_ring)
1155{
1156  return p_String0(p, p_ring, p_ring);
1157}
1158static inline void      p_Write(poly p, ring p_ring)
1159{
1160  p_Write(p, p_ring, p_ring);
1161}
1162static inline void      p_Write0(poly p, ring p_ring)
1163{
1164  p_Write0(p, p_ring, p_ring);
1165}
1166static inline void      p_wrp(poly p, ring p_ring)
1167{
1168  p_wrp(p, p_ring, p_ring);
1169}
1170
1171/***************************************************************
1172 *  Purpose: implementation of poly procs which iter over ExpVector
1173 *  Author:  obachman (Olaf Bachmann)
1174 *  Created: 8/00
1175 *  Version: $Id$
1176 *******************************************************************/
1177#include <misc/mylimits.h>
1178#include <polys/templates/p_MemCmp.h>
1179// #include <polys/structs.h>
1180#include <polys/monomials/ring.h>
1181#include <coeffs/coeffs.h>
1182
1183#if PDEBUG > 0
1184
1185#define _p_LmCmpAction(p, q, r, actionE, actionG, actionS)  \
1186do                                                          \
1187{                                                           \
1188  int _cmp = p_LmCmp(p,q,r);                                \
1189  if (_cmp == 0) actionE;                                   \
1190  if (_cmp == 1) actionG;                                   \
1191  actionS;                                                  \
1192}                                                           \
1193while(0)
1194
1195#else
1196
1197#define _p_LmCmpAction(p, q, r, actionE, actionG, actionS)                      \
1198 p_MemCmp_LengthGeneral_OrdGeneral(p->exp, q->exp, r->CmpL_Size, r->ordsgn,    \
1199                                   actionE, actionG, actionS)
1200
1201#endif
1202
1203#define pDivAssume(x)   ((void)0)
1204
1205#include <omalloc/omalloc.h>
1206#include <coeffs/coeffs.h>
1207#include <polys/monomials/p_polys.h>
1208#include <polys/templates/p_MemAdd.h>
1209#include <polys/templates/p_MemCopy.h>
1210
1211/***************************************************************
1212 *
1213 * Allocation/Initalization/Deletion
1214 *
1215 ***************************************************************/
1216// adjustments for negative weights
1217static inline void p_MemAdd_NegWeightAdjust(poly p, const ring r)
1218{
1219  if (r->NegWeightL_Offset != NULL)
1220  {
1221    for (int i=r->NegWeightL_Size-1; i>=0; i--)
1222    {
1223      p->exp[r->NegWeightL_Offset[i]] -= POLY_NEGWEIGHT_OFFSET;
1224    }
1225  }
1226}
1227static inline void p_MemSub_NegWeightAdjust(poly p, const ring r)
1228{
1229  if (r->NegWeightL_Offset != NULL)
1230  {
1231    for (int i=r->NegWeightL_Size-1; i>=0; i--)
1232    {
1233      p->exp[r->NegWeightL_Offset[i]] += POLY_NEGWEIGHT_OFFSET;
1234    }
1235  }
1236}
1237// ExpVextor(d_p) = ExpVector(s_p)
1238static inline void p_ExpVectorCopy(poly d_p, poly s_p, const ring r)
1239{
1240  p_LmCheckPolyRing1(d_p, r);
1241  p_LmCheckPolyRing1(s_p, r);
1242  p_MemCopy_LengthGeneral(d_p->exp, s_p->exp, r->ExpL_Size);
1243}
1244
1245static inline poly p_Init(const ring r, omBin bin)
1246{
1247  p_CheckRing1(r);
1248  pAssume1(bin != NULL && r->PolyBin->sizeW == bin->sizeW);
1249  poly p;
1250  omTypeAlloc0Bin(poly, p, bin);
1251  p_MemAdd_NegWeightAdjust(p, r);
1252  p_SetRingOfLm(p, r);
1253  return p;
1254}
1255static inline poly p_Init(const ring r)
1256{
1257  return p_Init(r, r->PolyBin);
1258}
1259
1260static inline poly p_LmInit(poly p, const ring r)
1261{
1262  p_LmCheckPolyRing1(p, r);
1263  poly np;
1264  omTypeAllocBin(poly, np, r->PolyBin);
1265  p_SetRingOfLm(np, r);
1266  p_MemCopy_LengthGeneral(np->exp, p->exp, r->ExpL_Size);
1267  pNext(np) = NULL;
1268  pSetCoeff0(np, NULL);
1269  return np;
1270}
1271static inline poly p_LmInit(poly s_p, const ring s_r, const ring d_r, omBin d_bin)
1272{
1273  p_LmCheckPolyRing1(s_p, s_r);
1274  p_CheckRing(d_r);
1275  pAssume1(d_r->N <= s_r->N);
1276  poly d_p = p_Init(d_r, d_bin);
1277  for (int i=d_r->N; i>0; i--)
1278  {
1279    p_SetExp(d_p, i, p_GetExp(s_p, i,s_r), d_r);
1280  }
1281  if (rRing_has_Comp(d_r))
1282  {
1283    p_SetComp(d_p, p_GetComp(s_p,s_r), d_r);
1284  }
1285  p_Setm(d_p, d_r);
1286  return d_p;
1287}
1288static inline poly p_LmInit(poly s_p, const ring s_r, const ring d_r)
1289{
1290  pAssume1(d_r != NULL);
1291  return p_LmInit(s_p, s_r, d_r, d_r->PolyBin);
1292}
1293
1294// set all exponents l..k to 0, assume exp. k+1..n and 1..l-1 are in
1295// different blocks
1296// set coeff to 1
1297static inline poly p_GetExp_k_n(poly p, int l, int k, const ring r)
1298{
1299  if (p == NULL) return NULL;
1300  p_LmCheckPolyRing1(p, r);
1301  poly np;
1302  omTypeAllocBin(poly, np, r->PolyBin);
1303  p_SetRingOfLm(np, r);
1304  p_MemCopy_LengthGeneral(np->exp, p->exp, r->ExpL_Size);
1305  pNext(np) = NULL;
1306  pSetCoeff0(np, n_Init(1, r->cf));
1307  int i;
1308  for(i=l;i<=k;i++)
1309  {
1310    //np->exp[(r->VarOffset[i] & 0xffffff)] =0;
1311    p_SetExp(np,i,0,r);
1312  }
1313  p_Setm(np,r);
1314  return np;
1315}
1316
1317// simialar to p_ShallowCopyDelete but does it only for leading monomial
1318static inline poly p_LmShallowCopyDelete(poly p, const ring r, omBin bin)
1319{
1320  p_LmCheckPolyRing1(p, r);
1321  pAssume1(bin->sizeW == r->PolyBin->sizeW);
1322  poly new_p = p_New(r);
1323  p_MemCopy_LengthGeneral(new_p->exp, p->exp, r->ExpL_Size);
1324  pSetCoeff0(new_p, pGetCoeff(p));
1325  pNext(new_p) = pNext(p);
1326  omFreeBinAddr(p);
1327  return new_p;
1328}
1329
1330/***************************************************************
1331 *
1332 * Operation on ExpVectors
1333 *
1334 ***************************************************************/
1335// ExpVector(p1) += ExpVector(p2)
1336static inline void p_ExpVectorAdd(poly p1, poly p2, const ring r)
1337{
1338  p_LmCheckPolyRing1(p1, r);
1339  p_LmCheckPolyRing1(p2, r);
1340#if PDEBUG >= 1
1341  for (int i=1; i<=r->N; i++)
1342    pAssume1((unsigned long) (p_GetExp(p1, i, r) + p_GetExp(p2, i, r)) <= r->bitmask);
1343  pAssume1(p_GetComp(p1, r) == 0 || p_GetComp(p2, r) == 0);
1344#endif
1345
1346  p_MemAdd_LengthGeneral(p1->exp, p2->exp, r->ExpL_Size);
1347  p_MemAdd_NegWeightAdjust(p1, r);
1348}
1349// ExpVector(p1) -= ExpVector(p2)
1350static inline void p_ExpVectorSub(poly p1, poly p2, const ring r)
1351{
1352  p_LmCheckPolyRing1(p1, r);
1353  p_LmCheckPolyRing1(p2, r);
1354#if PDEBUG >= 1
1355  for (int i=1; i<=r->N; i++)
1356    pAssume1(p_GetExp(p1, i, r) >= p_GetExp(p2, i, r));
1357  pAssume1(p_GetComp(p1, r) == 0 || p_GetComp(p2, r) == 0 ||
1358          p_GetComp(p1, r) == p_GetComp(p2, r));
1359#endif
1360
1361  p_MemSub_LengthGeneral(p1->exp, p2->exp, r->ExpL_Size);
1362  p_MemSub_NegWeightAdjust(p1, r);
1363
1364}
1365// ExpVector(p1) += ExpVector(p2) - ExpVector(p3)
1366static inline void p_ExpVectorAddSub(poly p1, poly p2, poly p3, const ring r)
1367{
1368  p_LmCheckPolyRing1(p1, r);
1369  p_LmCheckPolyRing1(p2, r);
1370  p_LmCheckPolyRing1(p3, r);
1371#if PDEBUG >= 1
1372  for (int i=1; i<=r->N; i++)
1373    pAssume1(p_GetExp(p1, i, r) + p_GetExp(p2, i, r) >= p_GetExp(p3, i, r));
1374  pAssume1(p_GetComp(p1, r) == 0 ||
1375           (p_GetComp(p2, r) - p_GetComp(p3, r) == 0) ||
1376           (p_GetComp(p1, r) == p_GetComp(p2, r) - p_GetComp(p3, r)));
1377#endif
1378
1379  p_MemAddSub_LengthGeneral(p1->exp, p2->exp, p3->exp, r->ExpL_Size);
1380  // no need to adjust in case of NegWeights
1381}
1382
1383// ExpVector(pr) = ExpVector(p1) + ExpVector(p2)
1384static inline void p_ExpVectorSum(poly pr, poly p1, poly p2, const ring r)
1385{
1386  p_LmCheckPolyRing1(p1, r);
1387  p_LmCheckPolyRing1(p2, r);
1388  p_LmCheckPolyRing1(pr, r);
1389#if PDEBUG >= 1
1390  for (int i=1; i<=r->N; i++)
1391    pAssume1((unsigned long) (p_GetExp(p1, i, r) + p_GetExp(p2, i, r)) <= r->bitmask);
1392  pAssume1(p_GetComp(p1, r) == 0 || p_GetComp(p2, r) == 0);
1393#endif
1394
1395  p_MemSum_LengthGeneral(pr->exp, p1->exp, p2->exp, r->ExpL_Size);
1396  p_MemAdd_NegWeightAdjust(pr, r);
1397}
1398// ExpVector(pr) = ExpVector(p1) - ExpVector(p2)
1399static inline void p_ExpVectorDiff(poly pr, poly p1, poly p2, const ring r)
1400{
1401  p_LmCheckPolyRing1(p1, r);
1402  p_LmCheckPolyRing1(p2, r);
1403  p_LmCheckPolyRing1(pr, r);
1404#if PDEBUG >= 2
1405  for (int i=1; i<=r->N; i++)
1406    pAssume1(p_GetExp(p1, i, r) >= p_GetExp(p2, i, r));
1407  pAssume1(!rRing_has_Comp(r) || p_GetComp(p1, r) == p_GetComp(p2, r));
1408#endif
1409
1410  p_MemDiff_LengthGeneral(pr->exp, p1->exp, p2->exp, r->ExpL_Size);
1411  p_MemSub_NegWeightAdjust(pr, r);
1412}
1413
1414static inline BOOLEAN p_ExpVectorEqual(poly p1, poly p2, const ring r)
1415{
1416  p_LmCheckPolyRing1(p1, r);
1417  p_LmCheckPolyRing1(p2, r);
1418
1419  int i = r->ExpL_Size;
1420  unsigned long *ep = p1->exp;
1421  unsigned long *eq = p2->exp;
1422
1423  do
1424  {
1425    i--;
1426    if (ep[i] != eq[i]) return FALSE;
1427  }
1428  while (i);
1429  return TRUE;
1430}
1431
1432static inline long p_Totaldegree(poly p, const ring r)
1433{
1434  p_LmCheckPolyRing1(p, r);
1435  unsigned long s = p_GetTotalDegree(p->exp[r->VarL_Offset[0]],
1436                                     r,
1437                                     r->MinExpPerLong);
1438  for (int i=r->VarL_Size-1; i>0; i--)
1439  {
1440    s += p_GetTotalDegree(p->exp[r->VarL_Offset[i]], r);
1441  }
1442  return (long)s;
1443}
1444
1445static inline void p_GetExpV(poly p, int *ev, const ring r)
1446{
1447  p_LmCheckPolyRing1(p, r);
1448  for (int j = r->N; j; j--)
1449      ev[j] = p_GetExp(p, j, r);
1450
1451  ev[0] = p_GetComp(p, r);
1452}
1453static inline void p_SetExpV(poly p, int *ev, const ring r)
1454{
1455  p_LmCheckPolyRing1(p, r);
1456  for (int j = r->N; j; j--)
1457      p_SetExp(p, j, ev[j], r);
1458
1459  p_SetComp(p, ev[0],r);
1460  p_Setm(p, r);
1461}
1462
1463/***************************************************************
1464 *
1465 * Comparison w.r.t. monomial ordering
1466 *
1467 ***************************************************************/
1468static inline int p_LmCmp(poly p, poly q, const ring r)
1469{
1470  p_LmCheckPolyRing1(p, r);
1471  p_LmCheckPolyRing1(q, r);
1472
1473  p_MemCmp_LengthGeneral_OrdGeneral(p->exp, q->exp, r->CmpL_Size, r->ordsgn,
1474                                    return 0, return 1, return -1);
1475}
1476
1477
1478/***************************************************************
1479 *
1480 * divisibility
1481 *
1482 ***************************************************************/
1483// return: FALSE, if there exists i, such that a->exp[i] > b->exp[i]
1484//         TRUE, otherwise
1485// (1) Consider long vars, instead of single exponents
1486// (2) Clearly, if la > lb, then FALSE
1487// (3) Suppose la <= lb, and consider first bits of single exponents in l:
1488//     if TRUE, then value of these bits is la ^ lb
1489//     if FALSE, then la-lb causes an "overflow" into one of those bits, i.e.,
1490//               la ^ lb != la - lb
1491static inline BOOLEAN _p_LmDivisibleByNoComp(poly a, poly b, const ring r)
1492{
1493  int i=r->VarL_Size - 1;
1494  unsigned long divmask = r->divmask;
1495  unsigned long la, lb;
1496
1497  if (r->VarL_LowIndex >= 0)
1498  {
1499    i += r->VarL_LowIndex;
1500    do
1501    {
1502      la = a->exp[i];
1503      lb = b->exp[i];
1504      if ((la > lb) ||
1505          (((la & divmask) ^ (lb & divmask)) != ((lb - la) & divmask)))
1506      {
1507        pDivAssume(p_DebugLmDivisibleByNoComp(a, b, r) == FALSE);
1508        return FALSE;
1509      }
1510      i--;
1511    }
1512    while (i>=r->VarL_LowIndex);
1513  }
1514  else
1515  {
1516    do
1517    {
1518      la = a->exp[r->VarL_Offset[i]];
1519      lb = b->exp[r->VarL_Offset[i]];
1520      if ((la > lb) ||
1521          (((la & divmask) ^ (lb & divmask)) != ((lb - la) & divmask)))
1522      {
1523        pDivAssume(p_DebugLmDivisibleByNoComp(a, b, r) == FALSE);
1524        return FALSE;
1525      }
1526      i--;
1527    }
1528    while (i>=0);
1529  }
1530#ifdef HAVE_RINGS
1531  pDivAssume(p_DebugLmDivisibleByNoComp(a, b, r) == nDivBy(p_GetCoeff(b, r), p_GetCoeff(a, r)));
1532  return (!rField_is_Ring(r)) || n_DivBy(p_GetCoeff(b, r), p_GetCoeff(a, r), r->cf);
1533#else
1534  pDivAssume(p_DebugLmDivisibleByNoComp(a, b, r) == TRUE);
1535  return TRUE;
1536#endif
1537}
1538
1539static inline BOOLEAN _p_LmDivisibleByNoComp(poly a, const ring r_a, poly b, const ring r_b)
1540{
1541  int i=r_a->N;
1542  pAssume1(r_a->N == r_b->N);
1543
1544  do
1545  {
1546    if (p_GetExp(a,i,r_a) > p_GetExp(b,i,r_b))
1547      return FALSE;
1548    i--;
1549  }
1550  while (i);
1551#ifdef HAVE_RINGS
1552  return n_DivBy(p_GetCoeff(b, r_b), p_GetCoeff(a, r_a), r_a->cf);
1553#else
1554  return TRUE;
1555#endif
1556}
1557
1558#ifdef HAVE_RATGRING
1559static inline BOOLEAN _p_LmDivisibleByNoCompPart(poly a, const ring r_a, poly b, const ring r_b,const int start, const int end)
1560{
1561  int i=end;
1562  pAssume1(r_a->N == r_b->N);
1563
1564  do
1565  {
1566    if (p_GetExp(a,i,r_a) > p_GetExp(b,i,r_b))
1567      return FALSE;
1568    i--;
1569  }
1570  while (i>=start);
1571#ifdef HAVE_RINGS
1572  return nDivBy(p_GetCoeff(b, r), p_GetCoeff(a, r));
1573#else
1574  return TRUE;
1575#endif
1576}
1577static inline BOOLEAN _p_LmDivisibleByPart(poly a, const ring r_a, poly b, const ring r_b,const int start, const int end)
1578{
1579  if (p_GetComp(a, r_a) == 0 || p_GetComp(a,r_a) == p_GetComp(b,r_b))
1580    return _p_LmDivisibleByNoCompPart(a, r_a, b, r_b,start,end);
1581  return FALSE;
1582}
1583static inline BOOLEAN p_LmDivisibleByPart(poly a, poly b, const ring r,const int start, const int end)
1584{
1585  p_LmCheckPolyRing1(b, r);
1586  pIfThen1(a != NULL, p_LmCheckPolyRing1(b, r));
1587  if (p_GetComp(a, r) == 0 || p_GetComp(a,r) == p_GetComp(b,r))
1588    return _p_LmDivisibleByNoCompPart(a, r, b, r,start, end);
1589  return FALSE;
1590}
1591#endif
1592static inline BOOLEAN _p_LmDivisibleBy(poly a, poly b, const ring r)
1593{
1594  if (p_GetComp(a, r) == 0 || p_GetComp(a,r) == p_GetComp(b,r))
1595    return _p_LmDivisibleByNoComp(a, b, r);
1596  return FALSE;
1597}
1598static inline BOOLEAN _p_LmDivisibleBy(poly a, const ring r_a, poly b, const ring r_b)
1599{
1600  if (p_GetComp(a, r_a) == 0 || p_GetComp(a,r_a) == p_GetComp(b,r_b))
1601    return _p_LmDivisibleByNoComp(a, r_a, b, r_b);
1602  return FALSE;
1603}
1604static inline BOOLEAN p_LmDivisibleByNoComp(poly a, poly b, const ring r)
1605{
1606  p_LmCheckPolyRing1(a, r);
1607  p_LmCheckPolyRing1(b, r);
1608  return _p_LmDivisibleByNoComp(a, b, r);
1609}
1610static inline BOOLEAN p_LmDivisibleBy(poly a, poly b, const ring r)
1611{
1612  p_LmCheckPolyRing1(b, r);
1613  pIfThen1(a != NULL, p_LmCheckPolyRing1(b, r));
1614  if (p_GetComp(a, r) == 0 || p_GetComp(a,r) == p_GetComp(b,r))
1615    return _p_LmDivisibleByNoComp(a, b, r);
1616  return FALSE;
1617}
1618
1619static inline BOOLEAN p_DivisibleBy(poly a, poly b, const ring r)
1620{
1621  pIfThen1(b!=NULL, p_LmCheckPolyRing1(b, r));
1622  pIfThen1(a!=NULL, p_LmCheckPolyRing1(a, r));
1623
1624  if (a != NULL && (p_GetComp(a, r) == 0 || p_GetComp(a,r) == p_GetComp(b,r)))
1625      return _p_LmDivisibleByNoComp(a,b,r);
1626  return FALSE;
1627}
1628static inline BOOLEAN p_DivisibleBy(poly a, const ring r_a, poly b, const ring r_b)
1629{
1630  pIfThen1(b!=NULL, p_LmCheckPolyRing1(b, r_b));
1631  pIfThen1(a!=NULL, p_LmCheckPolyRing1(a, r_a));
1632  if (a != NULL) {
1633      return _p_LmDivisibleBy(a, r_a, b, r_b);
1634  }
1635  return FALSE;
1636}
1637static inline BOOLEAN p_LmDivisibleBy(poly a, const ring r_a, poly b, const ring r_b)
1638{
1639  p_LmCheckPolyRing(a, r_a);
1640  p_LmCheckPolyRing(b, r_b);
1641  return _p_LmDivisibleBy(a, r_a, b, r_b);
1642}
1643static inline BOOLEAN p_LmShortDivisibleBy(poly a, unsigned long sev_a,
1644                                    poly b, unsigned long not_sev_b, const ring r)
1645{
1646  p_LmCheckPolyRing1(a, r);
1647  p_LmCheckPolyRing1(b, r);
1648#ifndef PDIV_DEBUG
1649  _pPolyAssume2(p_GetShortExpVector(a, r) == sev_a, a, r);
1650  _pPolyAssume2(p_GetShortExpVector(b, r) == ~ not_sev_b, b, r);
1651
1652  if (sev_a & not_sev_b)
1653  {
1654    pAssume1(p_LmDivisibleByNoComp(a, b, r) == FALSE);
1655    return FALSE;
1656  }
1657  return p_LmDivisibleBy(a, b, r);
1658#else
1659  return pDebugLmShortDivisibleBy(a, sev_a, r, b, not_sev_b, r);
1660#endif
1661}
1662
1663static inline BOOLEAN p_LmShortDivisibleBy(poly a, unsigned long sev_a, const ring r_a,
1664                                      poly b, unsigned long not_sev_b, const ring r_b)
1665{
1666  p_LmCheckPolyRing1(a, r_a);
1667  p_LmCheckPolyRing1(b, r_b);
1668#ifndef PDIV_DEBUG
1669  _pPolyAssume2(p_GetShortExpVector(a, r_a) == sev_a, a, r_a);
1670  _pPolyAssume2(p_GetShortExpVector(b, r_b) == ~ not_sev_b, b, r_b);
1671
1672  if (sev_a & not_sev_b)
1673  {
1674    pAssume1(_p_LmDivisibleByNoComp(a, r_a, b, r_b) == FALSE);
1675    return FALSE;
1676  }
1677  return _p_LmDivisibleBy(a, r_a, b, r_b);
1678#else
1679  return pDebugLmShortDivisibleBy(a, sev_a, r_a, b, not_sev_b, r_b);
1680#endif
1681}
1682
1683/***************************************************************
1684 *
1685 * Misc things on Lm
1686 *
1687 ***************************************************************/
1688
1689
1690// like the respective p_LmIs* routines, except that p might be empty
1691static inline BOOLEAN p_IsConstantComp(const poly p, const ring r)
1692{
1693  if (p == NULL) return TRUE;
1694  return (pNext(p)==NULL) && p_LmIsConstantComp(p, r);
1695}
1696
1697static inline BOOLEAN p_IsConstant(const poly p, const ring r)
1698{
1699  if (p == NULL) return TRUE;
1700  return (pNext(p)==NULL) && p_LmIsConstant(p, r);
1701}
1702
1703static inline BOOLEAN p_IsConstantPoly(const poly p, const ring r)
1704{
1705  poly pp=p;
1706  while(pp!=NULL)
1707  {
1708    if (! p_LmIsConstantComp(pp, r))
1709      return FALSE;
1710    pIter(pp);
1711  }
1712  return TRUE;
1713}
1714
1715static inline BOOLEAN p_IsUnit(const poly p, const ring r)
1716{
1717  if (p == NULL) return FALSE;
1718#ifdef HAVE_RINGS
1719  if (rField_is_Ring(r))
1720    return (p_LmIsConstant(p, r) && nIsUnit(pGetCoeff(p),r->cf));
1721#endif
1722  return p_LmIsConstant(p, r);
1723}
1724
1725static inline BOOLEAN p_LmExpVectorAddIsOk(const poly p1, const poly p2,
1726                                      const ring r)
1727{
1728  p_LmCheckPolyRing(p1, r);
1729  p_LmCheckPolyRing(p2, r);
1730  unsigned long l1, l2, divmask = r->divmask;
1731  int i;
1732
1733  for (i=0; i<r->VarL_Size; i++)
1734  {
1735    l1 = p1->exp[r->VarL_Offset[i]];
1736    l2 = p2->exp[r->VarL_Offset[i]];
1737    // do the divisiblity trick
1738    if ( (l1 > ULONG_MAX - l2) ||
1739         (((l1 & divmask) ^ (l2 & divmask)) != ((l1 + l2) & divmask)))
1740      return FALSE;
1741  }
1742  return TRUE;
1743}
1744void      p_Split(poly p, poly * r);   /*p => IN(p), r => REST(p) */
1745BOOLEAN p_HasNotCF(poly p1, poly p2, const ring r);
1746poly      p_mInit(const char *s, BOOLEAN &ok, const ring r); /* monom s -> poly, interpreter */
1747const char *    p_Read(const char *s, poly &p,const ring r); /* monom -> poly */
1748poly      p_Divide(poly a, poly b, const ring r);
1749poly      p_DivideM(poly a, poly b, const ring r);
1750void      p_Lcm(poly a, poly b, poly m, const ring r);
1751poly      p_Diff(poly a, int k, const ring r);
1752poly      p_DiffOp(poly a, poly b,BOOLEAN multiply, const ring r);
1753int       p_Weight(int c, const ring r);
1754
1755/* syszygy stuff */
1756BOOLEAN   p_VectorHasUnitB(poly p, int * k, const ring r);
1757void      p_VectorHasUnit(poly p, int * k, int * len, const ring r);
1758poly      p_TakeOutComp1(poly * p, int k, const ring r);
1759// Splits *p into two polys: *q which consists of all monoms with
1760// component == comp and *p of all other monoms *lq == pLength(*q)
1761// On return all components pf *q == 0
1762void p_TakeOutComp(poly *p, long comp, poly *q, int *lq, const ring r);
1763
1764// This is something weird -- Don't use it, unless you know what you are doing
1765poly      p_TakeOutComp(poly * p, int k);
1766
1767void      p_DeleteComp(poly * p,int k, const ring r);
1768
1769/*-------------ring management:----------------------*/
1770void p_SetGlobals(const ring r, BOOLEAN complete = TRUE);
1771
1772// resets the pFDeg and pLDeg: if pLDeg is not given, it is
1773// set to currRing->pLDegOrig, i.e. to the respective LDegProc which
1774// only uses pFDeg (and not pDeg, or pTotalDegree, etc).
1775// If you use this, make sure your procs does not make any assumptions
1776// on ordering and/or OrdIndex -- otherwise they might return wrong results
1777// on strat->tailRing
1778void pSetDegProcs(ring r, pFDegProc new_FDeg, pLDegProc new_lDeg = NULL);
1779// restores pFDeg and pLDeg:
1780void pRestoreDegProcs(ring r, pFDegProc old_FDeg, pLDegProc old_lDeg);
1781
1782/*-------------pComp for syzygies:-------------------*/
1783void p_SetModDeg(intvec *w, ring r);
1784
1785/*------------ Jet ----------------------------------*/
1786poly pp_Jet(poly p, int m, const ring R);
1787poly p_Jet(poly p, int m,const ring R);
1788poly pp_JetW(poly p, int m, short *w, const ring R);
1789poly p_JetW(poly p, int m, short *w, const ring R);
1790
1791
1792poly      p_PermPoly (poly p, int * perm,const ring OldRing, const ring dst,
1793                     nMapFunc nMap, int *par_perm=NULL, int OldPar=0);
1794
1795/*----------------------------------------------------*/
1796poly p_Series(int n,poly p,poly u, intvec *w, const ring R);
1797poly p_Invers(int n,poly u,intvec *w, const ring R);
1798
1799
1800#endif // P_POLYS_H
1801
Note: See TracBrowser for help on using the repository browser.