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