[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
| 4 | /*************************************************************** |
---|
| 5 | * File: pInline2.h |
---|
| 6 | * Purpose: implementation of poly procs which are of constant time |
---|
| 7 | * Author: obachman (Olaf Bachmann) |
---|
| 8 | * Created: 8/00 |
---|
| 9 | *******************************************************************/ |
---|
| 10 | #ifndef PINLINE2_H |
---|
| 11 | #define PINLINE2_H |
---|
| 12 | |
---|
| 13 | /*************************************************************** |
---|
| 14 | * |
---|
| 15 | * Primitives for accessing and setting fields of a poly |
---|
| 16 | * |
---|
| 17 | ***************************************************************/ |
---|
| 18 | #if !defined(NO_PINLINE2) || defined(PINLINE2_CC) |
---|
| 19 | |
---|
[9734bac] | 20 | #include "structs.h" |
---|
| 21 | #include <omalloc.h> |
---|
| 22 | #include <coeffs.h> |
---|
| 23 | #include "p_Procs.h" |
---|
| 24 | #include "sbuckets.h" |
---|
[35aab3] | 25 | #ifdef HAVE_PLURAL |
---|
[9734bac] | 26 | #include <gring.h> |
---|
| 27 | #include <ring.h> |
---|
[35aab3] | 28 | #endif |
---|
| 29 | |
---|
| 30 | PINLINE2 number p_SetCoeff(poly p, number n, ring r) |
---|
| 31 | { |
---|
| 32 | p_LmCheckPolyRing2(p, r); |
---|
| 33 | n_Delete(&(p->coef), r); |
---|
| 34 | (p)->coef=n; |
---|
| 35 | return n; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | // order |
---|
[b07ba77] | 39 | PINLINE2 long p_GetOrder(poly p, ring r) |
---|
[35aab3] | 40 | { |
---|
| 41 | p_LmCheckPolyRing2(p, r); |
---|
| 42 | if (r->typ==NULL) return ((p)->exp[r->pOrdIndex]); |
---|
| 43 | int i=0; |
---|
| 44 | loop |
---|
| 45 | { |
---|
| 46 | switch(r->typ[i].ord_typ) |
---|
| 47 | { |
---|
| 48 | case ro_wp_neg: |
---|
[e784726] | 49 | return (((long)((p)->exp[r->pOrdIndex]))-POLY_NEGWEIGHT_OFFSET); |
---|
[35aab3] | 50 | case ro_syzcomp: |
---|
[273fed] | 51 | case ro_syz: |
---|
[35aab3] | 52 | case ro_cp: |
---|
| 53 | i++; |
---|
| 54 | break; |
---|
| 55 | //case ro_dp: |
---|
| 56 | //case ro_wp: |
---|
| 57 | default: |
---|
| 58 | return ((p)->exp[r->pOrdIndex]); |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | // Setm |
---|
[33c36d] | 64 | PINLINE2 void p_Setm(poly p, const ring r) |
---|
[35aab3] | 65 | { |
---|
| 66 | p_CheckRing2(r); |
---|
| 67 | r->p_Setm(p, r); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | // component |
---|
| 71 | PINLINE2 unsigned long p_SetComp(poly p, unsigned long c, ring r) |
---|
| 72 | { |
---|
| 73 | p_LmCheckPolyRing2(p, r); |
---|
| 74 | pAssume2(rRing_has_Comp(r)); |
---|
| 75 | __p_GetComp(p,r) = c; |
---|
| 76 | return c; |
---|
| 77 | } |
---|
| 78 | PINLINE2 unsigned long p_AddComp(poly p, unsigned long v, ring r) |
---|
| 79 | { |
---|
| 80 | p_LmCheckPolyRing2(p, r); |
---|
| 81 | pAssume2(rRing_has_Comp(r)); |
---|
| 82 | return __p_GetComp(p,r) += v; |
---|
| 83 | } |
---|
| 84 | PINLINE2 unsigned long p_SubComp(poly p, unsigned long v, ring r) |
---|
| 85 | { |
---|
| 86 | p_LmCheckPolyRing2(p, r); |
---|
| 87 | pAssume2(rRing_has_Comp(r)); |
---|
[367c32] | 88 | pPolyAssume2(__p_GetComp(p,r) >= v,p,r); |
---|
[35aab3] | 89 | return __p_GetComp(p,r) -= v; |
---|
| 90 | } |
---|
[52e2f6] | 91 | PINLINE2 int p_Comp_k_n(poly a, poly b, int k, ring r) |
---|
| 92 | { |
---|
| 93 | if ((a==NULL) || (b==NULL) ) return FALSE; |
---|
| 94 | p_LmCheckPolyRing2(a, r); |
---|
| 95 | p_LmCheckPolyRing2(b, r); |
---|
| 96 | pAssume2(k > 0 && k <= r->N); |
---|
| 97 | int i=k; |
---|
| 98 | for(;i<=r->N;i++) |
---|
| 99 | { |
---|
| 100 | if (p_GetExp(a,i,r) != p_GetExp(b,i,r)) return FALSE; |
---|
| 101 | // if (a->exp[(r->VarOffset[i] & 0xffffff)] != b->exp[(r->VarOffset[i] & 0xffffff)]) return FALSE; |
---|
| 102 | } |
---|
| 103 | return TRUE; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | #ifndef HAVE_EXPSIZES |
---|
[35aab3] | 107 | |
---|
[c92fb1] | 108 | /// get a single variable exponent |
---|
| 109 | /// @Note: |
---|
| 110 | /// the integer VarOffset encodes: |
---|
| 111 | /// 1. the position of a variable in the exponent vector p->exp (lower 24 bits) |
---|
| 112 | /// 2. number of bits to shift to the right in the upper 8 bits (which takes at most 6 bits for 64 bit) |
---|
| 113 | /// Thus VarOffset always has 2 zero higher bits! |
---|
[b1ff71] | 114 | PINLINE2 long p_GetExp(const poly p, const unsigned long iBitmask, const int VarOffset) |
---|
[35aab3] | 115 | { |
---|
[c92fb1] | 116 | pAssume2((VarOffset >> (24 + 6)) == 0); |
---|
[35aab3] | 117 | #if 0 |
---|
[c92fb1] | 118 | int pos=(VarOffset & 0xffffff); |
---|
| 119 | int bitpos=(VarOffset >> 24); |
---|
[b1ff71] | 120 | unsigned long exp=(p->exp[pos] >> bitmask) & iBitmask; |
---|
[35aab3] | 121 | return exp; |
---|
| 122 | #else |
---|
[b1ff71] | 123 | return (long) |
---|
[c92fb1] | 124 | ((p->exp[(VarOffset & 0xffffff)] >> (VarOffset >> 24)) |
---|
| 125 | & iBitmask); |
---|
[35aab3] | 126 | #endif |
---|
| 127 | } |
---|
[52e2f6] | 128 | |
---|
[c92fb1] | 129 | |
---|
| 130 | /// set a single variable exponent |
---|
| 131 | /// @Note: |
---|
| 132 | /// VarOffset encodes the position in p->exp @see p_GetExp |
---|
[b1ff71] | 133 | PINLINE2 unsigned long p_SetExp(poly p, const unsigned long e, const unsigned long iBitmask, const int VarOffset) |
---|
[35aab3] | 134 | { |
---|
| 135 | pAssume2(e>=0); |
---|
[b1ff71] | 136 | pAssume2(e<=iBitmask); |
---|
[c92fb1] | 137 | pAssume2((VarOffset >> (24 + 6)) == 0); |
---|
[35aab3] | 138 | |
---|
| 139 | // shift e to the left: |
---|
[c92fb1] | 140 | register int shift = VarOffset >> 24; |
---|
[b1ff71] | 141 | unsigned long ee = e << shift /*(VarOffset >> 24)*/; |
---|
[35aab3] | 142 | // find the bits in the exponent vector |
---|
[c92fb1] | 143 | register int offset = (VarOffset & 0xffffff); |
---|
[35aab3] | 144 | // clear the bits in the exponent vector: |
---|
[c92fb1] | 145 | p->exp[offset] &= ~( iBitmask << shift ); |
---|
[35aab3] | 146 | // insert e with | |
---|
| 147 | p->exp[ offset ] |= ee; |
---|
| 148 | return e; |
---|
| 149 | } |
---|
| 150 | |
---|
[52e2f6] | 151 | |
---|
[c92fb1] | 152 | #else // #ifdef HAVE_EXPSIZES // EXPERIMENTAL!!! |
---|
| 153 | |
---|
| 154 | PINLINE2 unsigned long BitMask(unsigned long bitmask, int twobits) |
---|
[52e2f6] | 155 | { |
---|
| 156 | // bitmask = 00000111111111111 |
---|
| 157 | // 0 must give bitmask! |
---|
| 158 | // 1, 2, 3 - anything like 00011..11 |
---|
| 159 | pAssume2((twobits >> 2) == 0); |
---|
[645a19] | 160 | static const unsigned long _bitmasks[4] = {-1, 0x7fff, 0x7f, 0x3}; |
---|
| 161 | return bitmask & _bitmasks[twobits]; |
---|
[52e2f6] | 162 | } |
---|
| 163 | |
---|
[c92fb1] | 164 | |
---|
[645a19] | 165 | /// @Note: we may add some more info (6 ) into VarOffset and thus encode |
---|
[b1ff71] | 166 | PINLINE2 long p_GetExp(const poly p, const unsigned long iBitmask, const int VarOffset) |
---|
[52e2f6] | 167 | { |
---|
[c92fb1] | 168 | int pos =(VarOffset & 0xffffff); |
---|
| 169 | int hbyte= (VarOffset >> 24); // the highest byte |
---|
[52e2f6] | 170 | int bitpos = hbyte & 0x3f; // last 6 bits |
---|
[b1ff71] | 171 | long bitmask = BitMask(iBitmask, hbyte >> 6); |
---|
[52e2f6] | 172 | |
---|
[b1ff71] | 173 | long exp=(p->exp[pos] >> bitpos) & bitmask; |
---|
[52e2f6] | 174 | return exp; |
---|
| 175 | |
---|
[c92fb1] | 176 | } |
---|
[52e2f6] | 177 | |
---|
[b1ff71] | 178 | PINLINE2 long p_SetExp(poly p, const long e, const unsigned long iBitmask, const int VarOffset) |
---|
[52e2f6] | 179 | { |
---|
| 180 | pAssume2(e>=0); |
---|
[b1ff71] | 181 | pAssume2(e <= BitMask(iBitmask, VarOffset >> 30)); |
---|
[52e2f6] | 182 | |
---|
| 183 | // shift e to the left: |
---|
[c92fb1] | 184 | register int hbyte = VarOffset >> 24; |
---|
| 185 | int bitmask = BitMask(iBitmask, hbyte >> 6); |
---|
[52e2f6] | 186 | register int shift = hbyte & 0x3f; |
---|
[b1ff71] | 187 | long ee = e << shift; |
---|
[52e2f6] | 188 | // find the bits in the exponent vector |
---|
[c92fb1] | 189 | register int offset = (VarOffset & 0xffffff); |
---|
[52e2f6] | 190 | // clear the bits in the exponent vector: |
---|
| 191 | p->exp[offset] &= ~( bitmask << shift ); |
---|
| 192 | // insert e with | |
---|
| 193 | p->exp[ offset ] |= ee; |
---|
| 194 | return e; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | #endif // #ifndef HAVE_EXPSIZES |
---|
| 198 | |
---|
[c92fb1] | 199 | |
---|
[b1ff71] | 200 | PINLINE2 long p_GetExp(const poly p, const ring r, const int VarOffset) |
---|
[c92fb1] | 201 | { |
---|
| 202 | p_LmCheckPolyRing2(p, r); |
---|
| 203 | pAssume2(VarOffset != -1); |
---|
| 204 | return p_GetExp(p, r->bitmask, VarOffset); |
---|
| 205 | } |
---|
| 206 | |
---|
[b1ff71] | 207 | PINLINE2 long p_SetExp(poly p, const long e, const ring r, const int VarOffset) |
---|
[c92fb1] | 208 | { |
---|
| 209 | p_LmCheckPolyRing2(p, r); |
---|
| 210 | pAssume2(VarOffset != -1); |
---|
| 211 | return p_SetExp(p, e, r->bitmask, VarOffset); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | /// get v^th exponent for a monomial |
---|
[b1ff71] | 217 | PINLINE2 long p_GetExp(const poly p, const int v, const ring r) |
---|
[c92fb1] | 218 | { |
---|
| 219 | p_LmCheckPolyRing2(p, r); |
---|
| 220 | pAssume2(v>0 && v <= r->N); |
---|
| 221 | pAssume2(r->VarOffset[v] != -1); |
---|
| 222 | return p_GetExp(p, r->bitmask, r->VarOffset[v]); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | |
---|
| 226 | /// set v^th exponent for a monomial |
---|
[b1ff71] | 227 | PINLINE2 long p_SetExp(poly p, const int v, const long e, const ring r) |
---|
[c92fb1] | 228 | { |
---|
| 229 | p_LmCheckPolyRing2(p, r); |
---|
| 230 | pAssume2(v>0 && v <= r->N); |
---|
| 231 | pAssume2(r->VarOffset[v] != -1); |
---|
| 232 | return p_SetExp(p, e, r->bitmask, r->VarOffset[v]); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | |
---|
| 236 | |
---|
| 237 | |
---|
| 238 | |
---|
[35aab3] | 239 | // the following should be implemented more efficiently |
---|
[b1ff71] | 240 | PINLINE2 long p_IncrExp(poly p, int v, ring r) |
---|
[35aab3] | 241 | { |
---|
| 242 | p_LmCheckPolyRing2(p, r); |
---|
| 243 | int e = p_GetExp(p,v,r); |
---|
| 244 | e++; |
---|
| 245 | return p_SetExp(p,v,e,r); |
---|
| 246 | } |
---|
[b1ff71] | 247 | PINLINE2 long p_DecrExp(poly p, int v, ring r) |
---|
[35aab3] | 248 | { |
---|
| 249 | p_LmCheckPolyRing2(p, r); |
---|
| 250 | int e = p_GetExp(p,v,r); |
---|
| 251 | pAssume2(e > 0); |
---|
| 252 | e--; |
---|
| 253 | return p_SetExp(p,v,e,r); |
---|
| 254 | } |
---|
[b1ff71] | 255 | PINLINE2 long p_AddExp(poly p, int v, long ee, ring r) |
---|
[35aab3] | 256 | { |
---|
| 257 | p_LmCheckPolyRing2(p, r); |
---|
| 258 | int e = p_GetExp(p,v,r); |
---|
| 259 | e += ee; |
---|
| 260 | return p_SetExp(p,v,e,r); |
---|
| 261 | } |
---|
[b1ff71] | 262 | PINLINE2 long p_SubExp(poly p, int v, long ee, ring r) |
---|
[35aab3] | 263 | { |
---|
| 264 | p_LmCheckPolyRing2(p, r); |
---|
[b1ff71] | 265 | long e = p_GetExp(p,v,r); |
---|
[35aab3] | 266 | pAssume2(e >= ee); |
---|
| 267 | e -= ee; |
---|
| 268 | return p_SetExp(p,v,e,r); |
---|
| 269 | } |
---|
[b1ff71] | 270 | PINLINE2 long p_MultExp(poly p, int v, long ee, ring r) |
---|
[35aab3] | 271 | { |
---|
| 272 | p_LmCheckPolyRing2(p, r); |
---|
[b1ff71] | 273 | long e = p_GetExp(p,v,r); |
---|
[35aab3] | 274 | e *= ee; |
---|
| 275 | return p_SetExp(p,v,e,r); |
---|
| 276 | } |
---|
| 277 | |
---|
[b1ff71] | 278 | PINLINE2 long p_GetExpSum(poly p1, poly p2, int i, ring r) |
---|
[35aab3] | 279 | { |
---|
| 280 | p_LmCheckPolyRing2(p1, r); |
---|
| 281 | p_LmCheckPolyRing2(p2, r); |
---|
| 282 | return p_GetExp(p1,i,r) + p_GetExp(p2,i,r); |
---|
| 283 | } |
---|
[b1ff71] | 284 | PINLINE2 long p_GetExpDiff(poly p1, poly p2, int i, ring r) |
---|
[35aab3] | 285 | { |
---|
| 286 | return p_GetExp(p1,i,r) - p_GetExp(p2,i,r); |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | |
---|
| 290 | /*************************************************************** |
---|
| 291 | * |
---|
| 292 | * Allocation/Initalization/Deletion |
---|
| 293 | * |
---|
| 294 | ***************************************************************/ |
---|
| 295 | PINLINE2 poly p_New(ring r, omBin bin) |
---|
| 296 | { |
---|
| 297 | p_CheckRing2(r); |
---|
[aa2b525] | 298 | pAssume2(bin != NULL && omSizeWOfBin(r->PolyBin) == omSizeWOfBin(bin)); |
---|
[35aab3] | 299 | poly p; |
---|
| 300 | omTypeAllocBin(poly, p, bin); |
---|
| 301 | p_SetRingOfLm(p, r); |
---|
| 302 | return p; |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | PINLINE2 poly p_New(ring r) |
---|
| 306 | { |
---|
| 307 | return p_New(r, r->PolyBin); |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | PINLINE2 void p_LmFree(poly p, ring r) |
---|
| 311 | { |
---|
| 312 | p_LmCheckPolyRing2(p, r); |
---|
| 313 | omFreeBinAddr(p); |
---|
| 314 | } |
---|
| 315 | PINLINE2 void p_LmFree(poly *p, ring r) |
---|
| 316 | { |
---|
| 317 | p_LmCheckPolyRing2(*p, r); |
---|
| 318 | poly h = *p; |
---|
| 319 | *p = pNext(h); |
---|
| 320 | omFreeBinAddr(h); |
---|
| 321 | } |
---|
| 322 | PINLINE2 poly p_LmFreeAndNext(poly p, ring r) |
---|
| 323 | { |
---|
| 324 | p_LmCheckPolyRing2(p, r); |
---|
| 325 | poly pnext = pNext(p); |
---|
| 326 | omFreeBinAddr(p); |
---|
| 327 | return pnext; |
---|
| 328 | } |
---|
| 329 | PINLINE2 void p_LmDelete(poly p, ring r) |
---|
| 330 | { |
---|
| 331 | p_LmCheckPolyRing2(p, r); |
---|
[367c32] | 332 | n_Delete(&pGetCoeff(p), r); |
---|
[35aab3] | 333 | omFreeBinAddr(p); |
---|
| 334 | } |
---|
| 335 | PINLINE2 void p_LmDelete(poly *p, ring r) |
---|
| 336 | { |
---|
| 337 | p_LmCheckPolyRing2(*p, r); |
---|
| 338 | poly h = *p; |
---|
| 339 | *p = pNext(h); |
---|
| 340 | n_Delete(&pGetCoeff(h), r); |
---|
| 341 | omFreeBinAddr(h); |
---|
| 342 | } |
---|
| 343 | PINLINE2 poly p_LmDeleteAndNext(poly p, ring r) |
---|
| 344 | { |
---|
| 345 | p_LmCheckPolyRing2(p, r); |
---|
[367c32] | 346 | poly pnext = pNext(p); |
---|
| 347 | n_Delete(&pGetCoeff(p), r); |
---|
[35aab3] | 348 | omFreeBinAddr(p); |
---|
| 349 | return pnext; |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | /*************************************************************** |
---|
| 353 | * |
---|
| 354 | * Misc routines |
---|
| 355 | * |
---|
| 356 | ***************************************************************/ |
---|
| 357 | PINLINE2 int p_Cmp(poly p1, poly p2, ring r) |
---|
| 358 | { |
---|
| 359 | if (p2==NULL) |
---|
| 360 | return 1; |
---|
| 361 | if (p1==NULL) |
---|
| 362 | return -1; |
---|
| 363 | return p_LmCmp(p1,p2,r); |
---|
| 364 | } |
---|
| 365 | |
---|
[a05493f] | 366 | PINLINE2 unsigned long p_GetMaxExp(const poly p, const ring r) |
---|
[35aab3] | 367 | { |
---|
| 368 | return p_GetMaxExp(p_GetMaxExpL(p, r), r); |
---|
| 369 | } |
---|
| 370 | |
---|
[ad23ba] | 371 | PINLINE2 unsigned long p_GetMaxExp(const unsigned long l, const ring r) |
---|
[35aab3] | 372 | { |
---|
| 373 | unsigned long bitmask = r->bitmask; |
---|
| 374 | unsigned long max = (l & bitmask); |
---|
[ad23ba] | 375 | unsigned long j = r->ExpPerLong - 1; |
---|
[35aab3] | 376 | |
---|
| 377 | if (j > 0) |
---|
| 378 | { |
---|
| 379 | unsigned long i = r->BitsPerExp; |
---|
| 380 | long e; |
---|
| 381 | loop |
---|
| 382 | { |
---|
| 383 | e = ((l >> i) & bitmask); |
---|
| 384 | if ((unsigned long) e > max) |
---|
| 385 | max = e; |
---|
| 386 | j--; |
---|
| 387 | if (j==0) break; |
---|
| 388 | i += r->BitsPerExp; |
---|
| 389 | } |
---|
| 390 | } |
---|
| 391 | return max; |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | PINLINE2 unsigned long |
---|
| 395 | p_GetTotalDegree(const unsigned long l, const ring r, const int number_of_exps) |
---|
| 396 | { |
---|
| 397 | const unsigned long bitmask = r->bitmask; |
---|
| 398 | unsigned long sum = (l & bitmask); |
---|
| 399 | unsigned long j = number_of_exps - 1; |
---|
| 400 | |
---|
| 401 | if (j > 0) |
---|
| 402 | { |
---|
| 403 | unsigned long i = r->BitsPerExp; |
---|
| 404 | loop |
---|
| 405 | { |
---|
| 406 | sum += ((l >> i) & bitmask); |
---|
| 407 | j--; |
---|
| 408 | if (j==0) break; |
---|
| 409 | i += r->BitsPerExp; |
---|
| 410 | } |
---|
| 411 | } |
---|
| 412 | return sum; |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | PINLINE2 unsigned long |
---|
| 416 | p_GetTotalDegree(const unsigned long l, const ring r) |
---|
| 417 | { |
---|
| 418 | return p_GetTotalDegree(l, r, r->ExpPerLong); |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | /*************************************************************** |
---|
| 422 | * |
---|
| 423 | * Dispatcher to r->p_Procs, they do the tests/checks |
---|
| 424 | * |
---|
| 425 | ***************************************************************/ |
---|
| 426 | // returns a copy of p |
---|
| 427 | PINLINE2 poly p_Copy(poly p, const ring r) |
---|
| 428 | { |
---|
[637e70d] | 429 | #ifdef PDEBUG |
---|
| 430 | poly pp= r->p_Procs->p_Copy(p, r); |
---|
| 431 | p_Test(pp,r); |
---|
| 432 | return pp; |
---|
| 433 | #else |
---|
[35aab3] | 434 | return r->p_Procs->p_Copy(p, r); |
---|
[637e70d] | 435 | #endif |
---|
[35aab3] | 436 | } |
---|
| 437 | |
---|
| 438 | PINLINE2 poly p_Copy(poly p, const ring lmRing, const ring tailRing) |
---|
| 439 | { |
---|
| 440 | #ifndef PDEBUG |
---|
| 441 | if (tailRing == lmRing) |
---|
| 442 | return tailRing->p_Procs->p_Copy(p, tailRing); |
---|
| 443 | #endif |
---|
| 444 | if (p != NULL) |
---|
| 445 | { |
---|
| 446 | poly pres = p_Head(p, lmRing); |
---|
| 447 | pNext(pres) = tailRing->p_Procs->p_Copy(pNext(p), tailRing); |
---|
| 448 | return pres; |
---|
| 449 | } |
---|
| 450 | else |
---|
| 451 | return NULL; |
---|
| 452 | } |
---|
| 453 | |
---|
| 454 | // deletes *p, and sets *p to NULL |
---|
| 455 | PINLINE2 void p_Delete(poly *p, const ring r) |
---|
| 456 | { |
---|
| 457 | r->p_Procs->p_Delete(p, r); |
---|
| 458 | } |
---|
| 459 | |
---|
| 460 | PINLINE2 void p_Delete(poly *p, const ring lmRing, const ring tailRing) |
---|
| 461 | { |
---|
| 462 | #ifndef PDEBUG |
---|
| 463 | if (tailRing == lmRing) |
---|
| 464 | { |
---|
| 465 | tailRing->p_Procs->p_Delete(p, tailRing); |
---|
| 466 | return; |
---|
| 467 | } |
---|
| 468 | #endif |
---|
| 469 | if (*p != NULL) |
---|
| 470 | { |
---|
| 471 | if (pNext(*p) != NULL) |
---|
| 472 | tailRing->p_Procs->p_Delete(&pNext(*p), tailRing); |
---|
| 473 | p_LmDelete(p, lmRing); |
---|
| 474 | } |
---|
| 475 | } |
---|
| 476 | |
---|
| 477 | PINLINE2 poly p_ShallowCopyDelete(poly p, const ring r, omBin bin) |
---|
| 478 | { |
---|
| 479 | p_LmCheckPolyRing2(p, r); |
---|
[aa2b525] | 480 | pAssume2(omSizeWOfBin(r->PolyBin) == omSizeWOfBin(bin)); |
---|
[35aab3] | 481 | return r->p_Procs->p_ShallowCopyDelete(p, r, bin); |
---|
| 482 | } |
---|
| 483 | |
---|
| 484 | // returns p+q, destroys p and q |
---|
| 485 | PINLINE2 poly p_Add_q(poly p, poly q, const ring r) |
---|
| 486 | { |
---|
| 487 | int shorter; |
---|
| 488 | return r->p_Procs->p_Add_q(p, q, shorter, r); |
---|
| 489 | } |
---|
| 490 | |
---|
| 491 | PINLINE2 poly p_Add_q(poly p, poly q, int &lp, int lq, const ring r) |
---|
| 492 | { |
---|
| 493 | int shorter; |
---|
| 494 | poly res = r->p_Procs->p_Add_q(p, q, shorter, r); |
---|
| 495 | lp = (lp + lq) - shorter; |
---|
| 496 | return res; |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | // returns p*n, destroys p |
---|
| 500 | PINLINE2 poly p_Mult_nn(poly p, number n, const ring r) |
---|
| 501 | { |
---|
| 502 | if (n_IsOne(n, r)) |
---|
| 503 | return p; |
---|
| 504 | else |
---|
| 505 | return r->p_Procs->p_Mult_nn(p, n, r); |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | PINLINE2 poly p_Mult_nn(poly p, number n, const ring lmRing, |
---|
| 509 | const ring tailRing) |
---|
| 510 | { |
---|
| 511 | #ifndef PDEBUG |
---|
| 512 | if (lmRing == tailRing) |
---|
| 513 | { |
---|
| 514 | return p_Mult_nn(p, n, tailRing); |
---|
| 515 | } |
---|
| 516 | #endif |
---|
| 517 | poly pnext = pNext(p); |
---|
| 518 | pNext(p) = NULL; |
---|
| 519 | p = lmRing->p_Procs->p_Mult_nn(p, n, lmRing); |
---|
| 520 | pNext(p) = tailRing->p_Procs->p_Mult_nn(pnext, n, tailRing); |
---|
| 521 | return p; |
---|
| 522 | } |
---|
| 523 | |
---|
| 524 | // returns p*n, does not destroy p |
---|
| 525 | PINLINE2 poly pp_Mult_nn(poly p, number n, const ring r) |
---|
| 526 | { |
---|
| 527 | if (n_IsOne(n, r)) |
---|
| 528 | return p_Copy(p, r); |
---|
| 529 | else |
---|
| 530 | return r->p_Procs->pp_Mult_nn(p, n, r); |
---|
| 531 | } |
---|
| 532 | |
---|
| 533 | // returns Copy(p)*m, does neither destroy p nor m |
---|
| 534 | PINLINE2 poly pp_Mult_mm(poly p, poly m, const ring r) |
---|
| 535 | { |
---|
| 536 | if (p_LmIsConstant(m, r)) |
---|
| 537 | return pp_Mult_nn(p, pGetCoeff(m), r); |
---|
| 538 | else |
---|
| 539 | { |
---|
| 540 | poly last; |
---|
| 541 | return r->p_Procs->pp_Mult_mm(p, m, r, last); |
---|
| 542 | } |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | // returns p*m, destroys p, const: m |
---|
| 546 | PINLINE2 poly p_Mult_mm(poly p, poly m, const ring r) |
---|
| 547 | { |
---|
| 548 | if (p_LmIsConstant(m, r)) |
---|
| 549 | return p_Mult_nn(p, pGetCoeff(m), r); |
---|
| 550 | else |
---|
| 551 | return r->p_Procs->p_Mult_mm(p, m, r); |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | // return p - m*Copy(q), destroys p; const: p,m |
---|
| 555 | PINLINE2 poly p_Minus_mm_Mult_qq(poly p, poly m, poly q, const ring r) |
---|
| 556 | { |
---|
[5a9e7b] | 557 | #ifdef HAVE_PLURAL |
---|
| 558 | if (rIsPluralRing(r)) |
---|
| 559 | { |
---|
| 560 | int lp, lq; |
---|
| 561 | poly spNoether; |
---|
| 562 | return nc_p_Minus_mm_Mult_qq(p, m, q, lp, lq, spNoether, r); |
---|
| 563 | } |
---|
| 564 | #endif |
---|
| 565 | |
---|
[35aab3] | 566 | int shorter; |
---|
| 567 | poly last; |
---|
[5a9e7b] | 568 | |
---|
| 569 | return r->p_Procs->p_Minus_mm_Mult_qq(p, m, q, shorter, NULL, r, last); // !!! |
---|
[35aab3] | 570 | } |
---|
[5a9e7b] | 571 | |
---|
[35aab3] | 572 | PINLINE2 poly p_Minus_mm_Mult_qq(poly p, poly m, poly q, int &lp, int lq, |
---|
| 573 | poly spNoether, const ring r) |
---|
| 574 | { |
---|
| 575 | #ifdef HAVE_PLURAL |
---|
| 576 | if (rIsPluralRing(r)) |
---|
[5a9e7b] | 577 | return nc_p_Minus_mm_Mult_qq(p, m, q, lp, lq, spNoether, r); |
---|
[35aab3] | 578 | #endif |
---|
[5a9e7b] | 579 | |
---|
| 580 | int shorter; |
---|
| 581 | poly last,res; |
---|
| 582 | res = r->p_Procs->p_Minus_mm_Mult_qq(p, m, q, shorter, spNoether, r, last); |
---|
| 583 | lp = (lp + lq) - shorter; |
---|
[35aab3] | 584 | return res; |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | PINLINE2 poly pp_Mult_Coeff_mm_DivSelect(poly p, const poly m, const ring r) |
---|
| 588 | { |
---|
| 589 | int shorter; |
---|
| 590 | return r->p_Procs->pp_Mult_Coeff_mm_DivSelect(p, m, shorter, r); |
---|
| 591 | } |
---|
| 592 | |
---|
| 593 | PINLINE2 poly pp_Mult_Coeff_mm_DivSelect(poly p, int &lp, const poly m, const ring r) |
---|
| 594 | { |
---|
| 595 | int shorter; |
---|
| 596 | poly pp = r->p_Procs->pp_Mult_Coeff_mm_DivSelect(p, m, shorter, r); |
---|
| 597 | lp -= shorter; |
---|
| 598 | return pp; |
---|
| 599 | } |
---|
| 600 | |
---|
| 601 | // returns -p, destroys p |
---|
| 602 | PINLINE2 poly p_Neg(poly p, const ring r) |
---|
| 603 | { |
---|
| 604 | return r->p_Procs->p_Neg(p, r); |
---|
| 605 | } |
---|
| 606 | |
---|
| 607 | extern poly _p_Mult_q(poly p, poly q, const int copy, const ring r); |
---|
| 608 | // returns p*q, destroys p and q |
---|
| 609 | PINLINE2 poly p_Mult_q(poly p, poly q, const ring r) |
---|
| 610 | { |
---|
| 611 | if (p == NULL) |
---|
| 612 | { |
---|
| 613 | r->p_Procs->p_Delete(&q, r); |
---|
| 614 | return NULL; |
---|
| 615 | } |
---|
| 616 | if (q == NULL) |
---|
| 617 | { |
---|
| 618 | r->p_Procs->p_Delete(&p, r); |
---|
| 619 | return NULL; |
---|
| 620 | } |
---|
| 621 | |
---|
| 622 | if (pNext(p) == NULL) |
---|
| 623 | { |
---|
| 624 | #ifdef HAVE_PLURAL |
---|
| 625 | if (rIsPluralRing(r)) |
---|
[86016d] | 626 | q = nc_mm_Mult_p(p, q, r); |
---|
[35aab3] | 627 | else |
---|
| 628 | #endif /* HAVE_PLURAL */ |
---|
| 629 | q = r->p_Procs->p_Mult_mm(q, p, r); |
---|
| 630 | |
---|
| 631 | r->p_Procs->p_Delete(&p, r); |
---|
| 632 | return q; |
---|
| 633 | } |
---|
| 634 | |
---|
| 635 | if (pNext(q) == NULL) |
---|
| 636 | { |
---|
[5be91d] | 637 | // NEEDED |
---|
| 638 | #ifdef HAVE_PLURAL |
---|
[5a9e7b] | 639 | /* if (rIsPluralRing(r)) |
---|
| 640 | p = gnc_p_Mult_mm(p, q, r); // ??? |
---|
| 641 | else*/ |
---|
[5be91d] | 642 | #endif /* HAVE_PLURAL */ |
---|
| 643 | p = r->p_Procs->p_Mult_mm(p, q, r); |
---|
[35aab3] | 644 | |
---|
| 645 | r->p_Procs->p_Delete(&q, r); |
---|
| 646 | return p; |
---|
| 647 | } |
---|
| 648 | #ifdef HAVE_PLURAL |
---|
| 649 | if (rIsPluralRing(r)) |
---|
[5a9e7b] | 650 | return _nc_p_Mult_q(p, q, r); |
---|
[35aab3] | 651 | else |
---|
| 652 | #endif |
---|
| 653 | return _p_Mult_q(p, q, 0, r); |
---|
| 654 | } |
---|
| 655 | |
---|
| 656 | // returns p*q, does neither destroy p nor q |
---|
| 657 | PINLINE2 poly pp_Mult_qq(poly p, poly q, const ring r) |
---|
| 658 | { |
---|
| 659 | poly last; |
---|
| 660 | if (p == NULL || q == NULL) return NULL; |
---|
| 661 | |
---|
| 662 | if (pNext(p) == NULL) |
---|
| 663 | { |
---|
| 664 | #ifdef HAVE_PLURAL |
---|
| 665 | if (rIsPluralRing(r)) |
---|
[d5f9aea] | 666 | return nc_mm_Mult_pp(p, q, r); |
---|
[35aab3] | 667 | #endif |
---|
| 668 | return r->p_Procs->pp_Mult_mm(q, p, r, last); |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | if (pNext(q) == NULL) |
---|
| 672 | { |
---|
| 673 | return r->p_Procs->pp_Mult_mm(p, q, r, last); |
---|
| 674 | } |
---|
| 675 | |
---|
| 676 | poly qq = q; |
---|
| 677 | if (p == q) |
---|
| 678 | qq = p_Copy(q, r); |
---|
| 679 | |
---|
| 680 | poly res; |
---|
| 681 | #ifdef HAVE_PLURAL |
---|
| 682 | if (rIsPluralRing(r)) |
---|
[5a9e7b] | 683 | res = _nc_pp_Mult_qq(p, qq, r); |
---|
[35aab3] | 684 | else |
---|
| 685 | #endif |
---|
| 686 | res = _p_Mult_q(p, qq, 1, r); |
---|
| 687 | |
---|
| 688 | if (qq != q) |
---|
| 689 | p_Delete(&qq, r); |
---|
| 690 | return res; |
---|
| 691 | } |
---|
| 692 | |
---|
| 693 | // returns p + m*q destroys p, const: q, m |
---|
| 694 | PINLINE2 poly p_Plus_mm_Mult_qq(poly p, poly m, poly q, int &lp, int lq, |
---|
| 695 | const ring r) |
---|
| 696 | { |
---|
[5a9e7b] | 697 | #ifdef HAVE_PLURAL |
---|
| 698 | if (rIsPluralRing(r)) |
---|
| 699 | return nc_p_Plus_mm_Mult_qq(p, m, q, lp, lq, r); |
---|
| 700 | #endif |
---|
| 701 | |
---|
[18057e] | 702 | // this should be implemented more efficiently |
---|
[35aab3] | 703 | poly res, last; |
---|
| 704 | int shorter; |
---|
| 705 | number n_old = pGetCoeff(m); |
---|
| 706 | number n_neg = n_Copy(n_old, r); |
---|
| 707 | n_neg = n_Neg(n_neg, r); |
---|
| 708 | pSetCoeff0(m, n_neg); |
---|
[5a9e7b] | 709 | res = r->p_Procs->p_Minus_mm_Mult_qq(p, m, q, shorter, NULL, r, last); |
---|
[35aab3] | 710 | lp = (lp + lq) - shorter; |
---|
| 711 | pSetCoeff0(m, n_old); |
---|
| 712 | n_Delete(&n_neg, r); |
---|
| 713 | return res; |
---|
| 714 | } |
---|
| 715 | |
---|
| 716 | PINLINE2 poly p_Plus_mm_Mult_qq(poly p, poly m, poly q, const ring r) |
---|
| 717 | { |
---|
| 718 | int lp = 0, lq = 0; |
---|
| 719 | return p_Plus_mm_Mult_qq(p, m, q, lp, lq, r); |
---|
| 720 | } |
---|
| 721 | |
---|
| 722 | PINLINE2 poly p_Merge_q(poly p, poly q, const ring r) |
---|
| 723 | { |
---|
| 724 | return r->p_Procs->p_Merge_q(p, q, r); |
---|
| 725 | } |
---|
| 726 | |
---|
| 727 | PINLINE2 poly p_SortAdd(poly p, const ring r, BOOLEAN revert) |
---|
| 728 | { |
---|
| 729 | if (revert) p = pReverse(p); |
---|
| 730 | return sBucketSortAdd(p, r); |
---|
| 731 | } |
---|
| 732 | |
---|
| 733 | PINLINE2 poly p_SortMerge(poly p, const ring r, BOOLEAN revert) |
---|
| 734 | { |
---|
| 735 | if (revert) p = pReverse(p); |
---|
| 736 | return sBucketSortMerge(p, r); |
---|
| 737 | } |
---|
| 738 | |
---|
| 739 | /*************************************************************** |
---|
| 740 | * |
---|
| 741 | * I/O |
---|
| 742 | * |
---|
| 743 | ***************************************************************/ |
---|
| 744 | PINLINE2 char* p_String(poly p, ring p_ring) |
---|
| 745 | { |
---|
| 746 | return p_String(p, p_ring, p_ring); |
---|
| 747 | } |
---|
| 748 | PINLINE2 char* p_String0(poly p, ring p_ring) |
---|
| 749 | { |
---|
| 750 | return p_String0(p, p_ring, p_ring); |
---|
| 751 | } |
---|
| 752 | PINLINE2 void p_Write(poly p, ring p_ring) |
---|
| 753 | { |
---|
| 754 | p_Write(p, p_ring, p_ring); |
---|
| 755 | } |
---|
| 756 | PINLINE2 void p_Write0(poly p, ring p_ring) |
---|
| 757 | { |
---|
| 758 | p_Write0(p, p_ring, p_ring); |
---|
| 759 | } |
---|
| 760 | PINLINE2 void p_wrp(poly p, ring p_ring) |
---|
| 761 | { |
---|
| 762 | p_wrp(p, p_ring, p_ring); |
---|
| 763 | } |
---|
| 764 | #endif // !defined(NO_PINLINE2) || defined(POLYS_IMPL_CC) |
---|
| 765 | #endif // PINLINE2_H |
---|