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

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