source: git/libpolys/polys/monomials/p_polys.h @ 20d9284

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