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

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