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