source: git/polys/monomials/p_polys.h @ 74021a

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