1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: polys-impl.cc,v 1.34 1999-11-24 14:07:23 obachman Exp $ */ |
---|
5 | |
---|
6 | /*************************************************************** |
---|
7 | * |
---|
8 | * File: polys-impl.cc |
---|
9 | * Purpose: low-level procuders for polys. |
---|
10 | * |
---|
11 | * If you touch anything here, you better know what you are doing. |
---|
12 | * What is here should not be used directly from other routines -- the |
---|
13 | * encapsulations in polys.[h,cc] should be used, instead. |
---|
14 | * |
---|
15 | ***************************************************************/ |
---|
16 | #ifndef POLYS_IMPL_CC |
---|
17 | #define POLYS_IMPL_CC |
---|
18 | |
---|
19 | #include <stdio.h> |
---|
20 | #include <string.h> |
---|
21 | #include "mod2.h" |
---|
22 | #include "tok.h" |
---|
23 | #include "structs.h" |
---|
24 | #include "mmprivate.h" |
---|
25 | #include "mmemory.h" |
---|
26 | #include "febase.h" |
---|
27 | #include "numbers.h" |
---|
28 | #include "polys.h" |
---|
29 | #include "ring.h" |
---|
30 | #include "polys-impl.h" |
---|
31 | |
---|
32 | /*************************************************************** |
---|
33 | * |
---|
34 | * Storage Managament Routines |
---|
35 | * |
---|
36 | ***************************************************************/ |
---|
37 | |
---|
38 | |
---|
39 | /*2 |
---|
40 | * delete a poly, resets pointer |
---|
41 | * put the monomials in the freelist |
---|
42 | */ |
---|
43 | #ifdef MDEBUG |
---|
44 | void pDBDelete(poly * p, memHeap heap, char * f, int l) |
---|
45 | { |
---|
46 | poly h = *p; |
---|
47 | |
---|
48 | while (h!=NULL) |
---|
49 | { |
---|
50 | #ifdef LDEBUG |
---|
51 | nDBDelete(&(h->coef),f,l); |
---|
52 | #else |
---|
53 | nDelete(&(h->coef)); |
---|
54 | #endif |
---|
55 | pIter(h); |
---|
56 | pDBFree1((ADDRESS)*p, heap, f,l); |
---|
57 | *p=h; |
---|
58 | if (l>0) l= -l; |
---|
59 | } |
---|
60 | *p = NULL; |
---|
61 | } |
---|
62 | #else |
---|
63 | void _pDelete(poly* p, memHeap heap) |
---|
64 | { |
---|
65 | poly h = *p; |
---|
66 | poly pp; |
---|
67 | |
---|
68 | while (h!=NULL) |
---|
69 | { |
---|
70 | nDelete(&(h->coef)); |
---|
71 | pp=h; |
---|
72 | pIter(h); |
---|
73 | _pFree1((ADDRESS)pp, heap); |
---|
74 | } |
---|
75 | *p = NULL; |
---|
76 | } |
---|
77 | #endif |
---|
78 | |
---|
79 | /*2 |
---|
80 | * remove first monom |
---|
81 | */ |
---|
82 | #ifdef MDEBUG |
---|
83 | void pDBDelete1(poly * p, memHeap heap, char * f, int l) |
---|
84 | { |
---|
85 | poly h = *p; |
---|
86 | |
---|
87 | if (h==NULL) return; |
---|
88 | nDelete(&(h->coef)); |
---|
89 | *p = pNext(h); |
---|
90 | pDBFree1((ADDRESS)h, heap, f,l); |
---|
91 | } |
---|
92 | #else |
---|
93 | void _pDelete1(poly* p, memHeap heap) |
---|
94 | { |
---|
95 | poly h = *p; |
---|
96 | |
---|
97 | if (h==NULL) return; |
---|
98 | nDelete(&(h->coef)); |
---|
99 | *p = pNext(h); |
---|
100 | _pFree1((ADDRESS)h, heap); |
---|
101 | } |
---|
102 | #endif |
---|
103 | |
---|
104 | |
---|
105 | void ppDelete(poly* p, ring rg) |
---|
106 | { |
---|
107 | ring origRing = currRing; |
---|
108 | rChangeCurrRing(rg, FALSE); |
---|
109 | pDelete(p); |
---|
110 | rChangeCurrRing(origRing, FALSE); |
---|
111 | } |
---|
112 | |
---|
113 | /*2 |
---|
114 | * creates a copy of p |
---|
115 | */ |
---|
116 | #ifdef MDEBUG |
---|
117 | poly pDBCopy(memHeap d_h, poly s_p, char *f,int l) |
---|
118 | #else |
---|
119 | poly _pCopy(memHeap d_h, poly s_p) |
---|
120 | #endif |
---|
121 | { |
---|
122 | spolyrec dp; |
---|
123 | poly d_p = &dp; |
---|
124 | |
---|
125 | assume(d_h != NULL && (d_h == mm_specHeap) || |
---|
126 | d_h->size == mm_specHeap->size); |
---|
127 | pTest(s_p); |
---|
128 | while (s_p != NULL) |
---|
129 | { |
---|
130 | #ifdef MDEBUG |
---|
131 | d_p->next = (poly) mmDBAllocHeap(d_h, f, l); |
---|
132 | #else |
---|
133 | mmAllocHeap(d_p->next, d_h); |
---|
134 | #endif |
---|
135 | d_p = d_p->next; |
---|
136 | pSetCoeff0(d_p, nCopy(pGetCoeff(s_p))); |
---|
137 | memcpyW(&(d_p->exp.l[0]), &(s_p->exp.l[0]), currRing->ExpLSize); |
---|
138 | pIter(s_p); |
---|
139 | } |
---|
140 | pNext(d_p) = NULL; |
---|
141 | #if defined(MDEBUG) && defined(PDEBUG) |
---|
142 | pDBTest(dp.next, d_h, f,l); |
---|
143 | #else |
---|
144 | pHeapTest(dp.next, d_h); |
---|
145 | #endif |
---|
146 | return dp.next; |
---|
147 | } |
---|
148 | |
---|
149 | #ifdef MDEBUG |
---|
150 | poly pDBCopy(poly s_p, char *f,int l) |
---|
151 | { |
---|
152 | return pDBCopy(mm_specHeap, s_p, f, l); |
---|
153 | } |
---|
154 | #else |
---|
155 | poly _pCopy(poly s_p) |
---|
156 | { |
---|
157 | return _pCopy(mm_specHeap, s_p); |
---|
158 | } |
---|
159 | #endif |
---|
160 | |
---|
161 | |
---|
162 | #ifdef MDEBUG |
---|
163 | poly pDBShallowCopyDelete(memHeap d_h,poly *p,memHeap s_h, char *f,int l) |
---|
164 | #else |
---|
165 | poly _pShallowCopyDelete(memHeap d_h, poly *p, memHeap s_h) |
---|
166 | #endif |
---|
167 | { |
---|
168 | spolyrec dp; |
---|
169 | poly d_p = &dp, tmp; |
---|
170 | poly s_p = *p; |
---|
171 | |
---|
172 | assume(d_h != NULL && s_h != NULL && |
---|
173 | d_h->size == s_h->size); |
---|
174 | |
---|
175 | if (currRing->ExpLSize <= 2) |
---|
176 | { |
---|
177 | if (currRing->ExpLSize == 1) |
---|
178 | { |
---|
179 | while (s_p != NULL) |
---|
180 | { |
---|
181 | #ifdef MDEBUG |
---|
182 | d_p->next = (poly) mmDBAllocHeap(d_h, f, l); |
---|
183 | #else |
---|
184 | mmAllocHeap(d_p->next, d_h); |
---|
185 | #endif |
---|
186 | d_p = d_p->next; |
---|
187 | |
---|
188 | d_p->coef = s_p->coef; |
---|
189 | d_p->exp.l[0] = s_p->exp.l[0]; |
---|
190 | |
---|
191 | tmp = pNext(s_p); |
---|
192 | #ifdef MDEBUG |
---|
193 | mmDBFreeHeap(s_p, s_h, f, l); |
---|
194 | #else |
---|
195 | mmFreeHeap(s_p, s_h); |
---|
196 | #endif |
---|
197 | s_p = tmp; |
---|
198 | } |
---|
199 | } |
---|
200 | else |
---|
201 | { |
---|
202 | while (s_p != NULL) |
---|
203 | { |
---|
204 | #ifdef MDEBUG |
---|
205 | d_p->next = (poly) mmDBAllocHeap(d_h, f, l); |
---|
206 | #else |
---|
207 | mmAllocHeap(d_p->next, d_h); |
---|
208 | #endif |
---|
209 | d_p = d_p->next; |
---|
210 | |
---|
211 | d_p->coef = s_p->coef; |
---|
212 | d_p->exp.l[0] = s_p->exp.l[0]; |
---|
213 | d_p->exp.l[1] = s_p->exp.l[1]; |
---|
214 | |
---|
215 | tmp = pNext(s_p); |
---|
216 | #ifdef MDEBUG |
---|
217 | mmDBFreeHeap(s_p, s_h, f, l); |
---|
218 | #else |
---|
219 | mmFreeHeap(s_p, s_h); |
---|
220 | #endif |
---|
221 | s_p = tmp; |
---|
222 | } |
---|
223 | } |
---|
224 | } |
---|
225 | else |
---|
226 | { |
---|
227 | if (currRing->ExpLSize & 1) |
---|
228 | { |
---|
229 | while (s_p != NULL) |
---|
230 | { |
---|
231 | |
---|
232 | #ifdef MDEBUG |
---|
233 | d_p->next = (poly) mmDBAllocHeap(d_h, f, l); |
---|
234 | #else |
---|
235 | mmAllocHeap(d_p->next, d_h); |
---|
236 | #endif |
---|
237 | d_p = d_p->next; |
---|
238 | |
---|
239 | d_p->coef = s_p->coef; |
---|
240 | memcpy_nwODD(&(d_p->exp.l[0]), &(s_p->exp.l[1]), currRing->ExpLSize); |
---|
241 | |
---|
242 | tmp = pNext(s_p); |
---|
243 | #ifdef MDEBUG |
---|
244 | mmDBFreeHeap(s_p, s_h, f, l); |
---|
245 | #else |
---|
246 | mmFreeHeap(s_p, s_h); |
---|
247 | #endif |
---|
248 | s_p = tmp; |
---|
249 | } |
---|
250 | } |
---|
251 | else |
---|
252 | { |
---|
253 | while (s_p != NULL) |
---|
254 | { |
---|
255 | |
---|
256 | #ifdef MDEBUG |
---|
257 | d_p->next = (poly) mmDBAllocHeap(d_h, f, l); |
---|
258 | #else |
---|
259 | mmAllocHeap(d_p->next, d_h); |
---|
260 | #endif |
---|
261 | d_p = d_p->next; |
---|
262 | |
---|
263 | d_p->coef = s_p->coef; |
---|
264 | memcpy_nwEVEN(&(d_p->exp.l[0]), &(s_p->exp.l[1]), currRing->ExpLSize); |
---|
265 | |
---|
266 | tmp = pNext(s_p); |
---|
267 | #ifdef MDEBUG |
---|
268 | mmDBFreeHeap(s_p, s_h, f, l); |
---|
269 | #else |
---|
270 | mmFreeHeap(s_p, s_h); |
---|
271 | #endif |
---|
272 | s_p = tmp; |
---|
273 | } |
---|
274 | } |
---|
275 | } |
---|
276 | pNext(d_p) = NULL; |
---|
277 | pHeapTest(dp.next, d_h); |
---|
278 | *p = NULL; |
---|
279 | return pNext(dp.next); |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | /*2 |
---|
284 | * creates a copy of the initial monomial of p |
---|
285 | * sets the coeff of the copy to a defined value |
---|
286 | */ |
---|
287 | #ifdef MDEBUG |
---|
288 | poly pDBCopy1(poly p,char *f,int l) |
---|
289 | #else |
---|
290 | poly _pCopy1(poly p) |
---|
291 | #endif |
---|
292 | { |
---|
293 | poly w; |
---|
294 | #ifdef MDEBUG |
---|
295 | w = pDBNew(mm_specHeap, f,l); |
---|
296 | #else |
---|
297 | w = pNew(); |
---|
298 | #endif |
---|
299 | pCopy2(w,p); |
---|
300 | nNew(&(w->coef)); |
---|
301 | pNext(w) = NULL; |
---|
302 | return w; |
---|
303 | } |
---|
304 | |
---|
305 | /*2 |
---|
306 | * returns (a copy of) the head term of a |
---|
307 | */ |
---|
308 | #ifdef MDEBUG |
---|
309 | poly pDBHead(memHeap heap, poly p,char *f, int l) |
---|
310 | #else |
---|
311 | poly _pHead(memHeap heap, poly p) |
---|
312 | #endif |
---|
313 | { |
---|
314 | poly w=NULL; |
---|
315 | |
---|
316 | if (p!=NULL) |
---|
317 | { |
---|
318 | assume(heap != NULL && (heap == mm_specHeap) || |
---|
319 | heap->size == mm_specHeap->size); |
---|
320 | |
---|
321 | #ifdef MDEBUG |
---|
322 | w = (poly) mmDBAllocHeap(heap, f, l); |
---|
323 | #else |
---|
324 | mmAllocHeap(w, heap); |
---|
325 | #endif |
---|
326 | memcpyW(&(w->exp.l[0]), &(p->exp.l[0]), currRing->ExpLSize); |
---|
327 | pSetCoeff0(w,nCopy(pGetCoeff(p))); |
---|
328 | pNext(w) = NULL; |
---|
329 | } |
---|
330 | return w; |
---|
331 | } |
---|
332 | |
---|
333 | #ifdef MDEBUG |
---|
334 | poly pDBShallowCopyDeleteHead(memHeap d_h,poly *s_p,memHeap s_h, char *f,int l) |
---|
335 | #else |
---|
336 | poly _pShallowCopyDeleteHead(memHeap d_h, poly *s_p, memHeap s_h) |
---|
337 | #endif |
---|
338 | { |
---|
339 | poly w = NULL; |
---|
340 | poly p = *s_p; |
---|
341 | |
---|
342 | if (p!=NULL) |
---|
343 | { |
---|
344 | assume(d_h != NULL && s_h != NULL && |
---|
345 | d_h->size == s_h->size); |
---|
346 | |
---|
347 | #ifdef MDEBUG |
---|
348 | w = (poly) mmDBAllocHeap(d_h, f, l); |
---|
349 | #else |
---|
350 | mmAllocHeap(w, d_h); |
---|
351 | #endif |
---|
352 | memcpyW(&(w->exp.l[0]), &(p->exp.l[0]), currRing->ExpLSize); |
---|
353 | pSetCoeff0(w,pGetCoeff(p)); |
---|
354 | pNext(w) = NULL; |
---|
355 | |
---|
356 | *s_p = pNext(p); |
---|
357 | #ifdef MDEBUG |
---|
358 | mmDBFreeHeap(p, s_h, f, l); |
---|
359 | #else |
---|
360 | mmFreeHeap(p, s_h); |
---|
361 | #endif |
---|
362 | } |
---|
363 | return w; |
---|
364 | } |
---|
365 | |
---|
366 | |
---|
367 | |
---|
368 | poly pHeadProc(poly p) |
---|
369 | { |
---|
370 | return pHead(p); |
---|
371 | } |
---|
372 | |
---|
373 | /*2 |
---|
374 | * returns (a copy of) the head term of a without the coef |
---|
375 | */ |
---|
376 | #ifdef MDEBUG |
---|
377 | poly pDBHead0(poly p,char *f, int l) |
---|
378 | #else |
---|
379 | poly _pHead0(poly p) |
---|
380 | #endif |
---|
381 | { |
---|
382 | poly w=NULL; |
---|
383 | |
---|
384 | if (p!=NULL) |
---|
385 | { |
---|
386 | #if defined(PDEBUG) && defined(MDEBUG) |
---|
387 | w = pDBNew(mm_specHeap, f,l); |
---|
388 | #else |
---|
389 | w = pNew(); |
---|
390 | #endif |
---|
391 | pCopy2(w,p); |
---|
392 | pSetCoeff0(w,NULL); |
---|
393 | pNext(w) = NULL; |
---|
394 | } |
---|
395 | return w; |
---|
396 | } |
---|
397 | |
---|
398 | |
---|
399 | /*************************************************************** |
---|
400 | * |
---|
401 | * Routines for turned on debugging |
---|
402 | * |
---|
403 | ***************************************************************/ |
---|
404 | |
---|
405 | #if defined(PDEBUG) && PDEBUG > 1 |
---|
406 | Exponent_t pPDSetExp(poly p, int v, Exponent_t e, char* f, int l) |
---|
407 | { |
---|
408 | if (v == 0) |
---|
409 | { |
---|
410 | Warn("zero index to exponent in %s:%d\n", f, l); |
---|
411 | } |
---|
412 | if (v > pVariables) |
---|
413 | { |
---|
414 | Warn("index %d to exponent too large in %s:%d\n", v, f, l); |
---|
415 | } |
---|
416 | return (p)->exp.e[_pExpIndex(v)]=(e); |
---|
417 | } |
---|
418 | |
---|
419 | Exponent_t pPDGetExp(poly p, int v, char* f, int l) |
---|
420 | { |
---|
421 | if (v == 0) |
---|
422 | { |
---|
423 | Warn("zero index to exponent in %s:%d\n", f, l); |
---|
424 | } |
---|
425 | if (v > pVariables) |
---|
426 | { |
---|
427 | Warn("index %d to exponent too large in %s:%d\n", v, f, l); |
---|
428 | } |
---|
429 | return (p)->exp.e[_pExpIndex(v)]; |
---|
430 | } |
---|
431 | |
---|
432 | Exponent_t pPDRingSetExp(ring r, poly p, int v, Exponent_t e, char* f, int l) |
---|
433 | { |
---|
434 | if (v == 0) |
---|
435 | { |
---|
436 | Warn("zero index to exponent in %s:%d\n", f, l); |
---|
437 | } |
---|
438 | if (v > r->N) |
---|
439 | { |
---|
440 | Warn("index %d to exponent too large in %s:%d\n", v, f, l); |
---|
441 | } |
---|
442 | return (p)->exp.e[_pRingExpIndex(r, v)]=(e); |
---|
443 | } |
---|
444 | |
---|
445 | Exponent_t pPDRingGetExp(ring r, poly p, int v, char* f, int l) |
---|
446 | { |
---|
447 | if (v == 0) |
---|
448 | { |
---|
449 | Warn("zero index to exponent in %s:%d\n", f, l); |
---|
450 | } |
---|
451 | if (v > r->N) |
---|
452 | { |
---|
453 | Warn("index %d to exponent too large in %s:%d\n", v, f, l); |
---|
454 | } |
---|
455 | return (p)->exp.e[_pRingExpIndex(r,v)]; |
---|
456 | } |
---|
457 | |
---|
458 | Exponent_t pPDIncrExp(poly p, int v, char* f, int l) |
---|
459 | { |
---|
460 | if (v == 0) |
---|
461 | { |
---|
462 | Warn("zero index to exponent in %s:%d\n", f, l); |
---|
463 | } |
---|
464 | if (v > pVariables) |
---|
465 | { |
---|
466 | Warn("index %d to exponent too large in %s:%d\n", v, f, l); |
---|
467 | } |
---|
468 | return ((p)->exp.e[_pExpIndex(v)])++; |
---|
469 | } |
---|
470 | |
---|
471 | Exponent_t pPDDecrExp(poly p, int v, char* f, int l) |
---|
472 | { |
---|
473 | if (v == 0) |
---|
474 | { |
---|
475 | Warn("zero index to exponent in %s:%d\n", f, l); |
---|
476 | } |
---|
477 | if (v > pVariables) |
---|
478 | { |
---|
479 | Warn("index %d to exponent too large in %s:%d\n", v, f, l); |
---|
480 | } |
---|
481 | return ((p)->exp.e[_pExpIndex(v)])--; |
---|
482 | } |
---|
483 | |
---|
484 | Exponent_t pPDAddExp(poly p, int v, Exponent_t e, char* f, int l) |
---|
485 | { |
---|
486 | if (v == 0) |
---|
487 | { |
---|
488 | Warn("zero index to exponent in %s:%d\n", f, l); |
---|
489 | } |
---|
490 | if (v > pVariables) |
---|
491 | { |
---|
492 | Warn("index %d to exponent too large in %s:%d\n", v, f, l); |
---|
493 | } |
---|
494 | return ((p)->exp.e[_pExpIndex(v)]) += (e); |
---|
495 | } |
---|
496 | |
---|
497 | Exponent_t pPDSubExp(poly p, int v, Exponent_t e, char* f, int l) |
---|
498 | { |
---|
499 | if (v == 0) |
---|
500 | { |
---|
501 | Warn("zero index to exponent in %s:%d\n", f, l); |
---|
502 | } |
---|
503 | if (v > pVariables) |
---|
504 | { |
---|
505 | Warn("index %d to exponent too large in %s:%d\n", v, f, l); |
---|
506 | } |
---|
507 | return ((p)->exp.e[_pExpIndex(v)]) -= (e); |
---|
508 | } |
---|
509 | |
---|
510 | Exponent_t pPDMultExp(poly p, int v, Exponent_t e, char* f, int l) |
---|
511 | { |
---|
512 | if (v == 0) |
---|
513 | { |
---|
514 | Warn("zero index to exponent in %s:%d\n", f, l); |
---|
515 | } |
---|
516 | if (v > pVariables) |
---|
517 | { |
---|
518 | Warn("index %d to exponent too large in %s:%d\n", v, f, l); |
---|
519 | } |
---|
520 | return ((p)->exp.e[_pExpIndex(v)]) *= (e); |
---|
521 | } |
---|
522 | |
---|
523 | // routines on components |
---|
524 | Exponent_t pDBSetComp(poly p, Exponent_t k, int le, char* f, int l) |
---|
525 | { |
---|
526 | if (k < 0) |
---|
527 | { |
---|
528 | Warn("set negative component %d in %s:%d", k, f, l); |
---|
529 | } |
---|
530 | if (currRing->order[1] == ringorder_S) |
---|
531 | { |
---|
532 | if (le <= 0) le = currRing->typ[1].data.syzcomp.length; |
---|
533 | if (k > l) |
---|
534 | { |
---|
535 | Warn("component %d larger then max %d in %s:%d", |
---|
536 | k, le, f, l); |
---|
537 | } |
---|
538 | } |
---|
539 | return _pGetComp(p) = (k); |
---|
540 | } |
---|
541 | |
---|
542 | Exponent_t pDBDecrComp(poly p, char* f, int l) |
---|
543 | { |
---|
544 | if (_pGetComp(p) < 1) |
---|
545 | { |
---|
546 | Warn("decrement to negative component %d in %s:%d\n", _pGetComp(p), f, l); |
---|
547 | } |
---|
548 | return _pGetComp(p)--; |
---|
549 | } |
---|
550 | |
---|
551 | Exponent_t pDBAddComp(poly p, Exponent_t k, int le, char* f, int l) |
---|
552 | { |
---|
553 | if (_pGetComp(p) + k < 0) |
---|
554 | { |
---|
555 | Warn("add to negative component %d + %d = %d in %s:%d\n", _pGetComp(p), |
---|
556 | k, _pGetComp(p) + k, f, l); |
---|
557 | } |
---|
558 | _pGetComp(p) += (k); |
---|
559 | |
---|
560 | if (currRing->order[1] == ringorder_S) |
---|
561 | { |
---|
562 | if (le <= 0) le = currRing->typ[1].data.syzcomp.length; |
---|
563 | if (_pGetComp(p) > le) |
---|
564 | { |
---|
565 | Warn("sum of components %d larger then max %d in %s:%d\n", |
---|
566 | _pGetComp(p), le, f, l); |
---|
567 | assume(0); |
---|
568 | } |
---|
569 | } |
---|
570 | return _pGetComp(p); |
---|
571 | } |
---|
572 | |
---|
573 | Exponent_t pDBSubComp(poly p, Exponent_t k, char* f, int l) |
---|
574 | { |
---|
575 | if (_pGetComp(p) - k < 0) |
---|
576 | { |
---|
577 | Warn("sub to negative component %d - %d = %d in %s:%d\n", _pGetComp(p), |
---|
578 | k, _pGetComp(p) - k, f, l); |
---|
579 | } |
---|
580 | return _pGetComp(p) -= (k); |
---|
581 | } |
---|
582 | |
---|
583 | Exponent_t pDBRingSetComp(ring r, poly p, Exponent_t k, char* f, int l) |
---|
584 | { |
---|
585 | if (k < 0) |
---|
586 | { |
---|
587 | Warn("set negative component %d in %s:%d\n", k, f, l); |
---|
588 | } |
---|
589 | return _pRingGetComp(r, p) = (k); |
---|
590 | } |
---|
591 | |
---|
592 | // checks whether fast monom add did not overflow |
---|
593 | void pDBMonAddOn(poly p1, poly p2, char* f, int l) |
---|
594 | { |
---|
595 | poly ptemp = pHead0(p1); |
---|
596 | |
---|
597 | if (pGetComp(p1) != 0 && pGetComp(p2) != 0) |
---|
598 | { |
---|
599 | Warn("Error in pMonAddOn: both components %d:%d !=0 in %s:%d", |
---|
600 | pGetComp(p1), pGetComp(p2), f, l); |
---|
601 | } |
---|
602 | |
---|
603 | __pMonAddOn(p1, p2); |
---|
604 | |
---|
605 | for (int i=1; i<=pVariables; i++) |
---|
606 | { |
---|
607 | pAddExp(ptemp, i, pGetExp(p2, i)); |
---|
608 | } |
---|
609 | pAddComp(ptemp, pGetComp(p2)); |
---|
610 | pSetm(ptemp); |
---|
611 | |
---|
612 | if (! pEqual(ptemp, p1)) |
---|
613 | { |
---|
614 | Warn("Error in pMonAddOn in %s:%d\n", f, l); |
---|
615 | } |
---|
616 | |
---|
617 | pFree1(ptemp); |
---|
618 | } |
---|
619 | |
---|
620 | void pDBMonSubFrom(poly p1, poly p2, char* f, int l) |
---|
621 | { |
---|
622 | poly ptemp = pNew(); |
---|
623 | pCopy2(ptemp, p1); |
---|
624 | |
---|
625 | if ((pGetComp(p1) != pGetComp(p2)) && (pGetComp(p2)!=0)) |
---|
626 | { |
---|
627 | Warn("Error in pMonSubFrom: components are different %d:%d in %s:%d", |
---|
628 | pGetComp(p1), pGetComp(p2), f, l); |
---|
629 | } |
---|
630 | |
---|
631 | __pMonSubFrom(p1, p2); |
---|
632 | |
---|
633 | for (int i=1; i<=pVariables; i++) |
---|
634 | { |
---|
635 | if (pGetExp(ptemp, i) < pGetExp(p2, i)) |
---|
636 | { |
---|
637 | Warn("Error in pMonSubFrom: %dth exponent %d of p1 smaller than %d of p2", i, pGetExp(ptemp, i), pGetExp(p2, i)); |
---|
638 | } |
---|
639 | pSubExp(ptemp, i, pGetExp(p2, i)); |
---|
640 | } |
---|
641 | pSetComp(ptemp, pGetComp(ptemp)-pGetComp(p2)); |
---|
642 | pSetm(ptemp); |
---|
643 | |
---|
644 | if (! pEqual(ptemp, p1)) |
---|
645 | { |
---|
646 | Warn("Error in pMonSubFrom in %s:%d", f, l); |
---|
647 | } |
---|
648 | |
---|
649 | pFree1(ptemp); |
---|
650 | } |
---|
651 | |
---|
652 | void pDBMonAdd(poly p1, poly p2, poly p3, ring r, char* f, int l) |
---|
653 | { |
---|
654 | if (r == currRing) |
---|
655 | { |
---|
656 | if (pGetComp(p3) != 0 && pGetComp(p2) != 0) |
---|
657 | { |
---|
658 | Warn("Error in pMonAdd: both components %d:%d !=0 in %s:%d", |
---|
659 | pGetComp(p3), pGetComp(p2), f, l); |
---|
660 | } |
---|
661 | if (p2 == p1 || p3 == p1) |
---|
662 | { |
---|
663 | Warn("Error in pMonAdd: Destination equals source in %s:%d", f, l); |
---|
664 | } |
---|
665 | } |
---|
666 | __pMonAdd(p1, p2, p3, r); |
---|
667 | |
---|
668 | if (r == currRing) |
---|
669 | { |
---|
670 | poly ptemp = pInit(); |
---|
671 | for (int i=1; i<=pVariables; i++) |
---|
672 | { |
---|
673 | pSetExp(ptemp, i, pGetExp(p2, i) + pGetExp(p3, i)); |
---|
674 | if (pGetExp(ptemp, i) != pGetExp(p1, i)) |
---|
675 | { |
---|
676 | Warn("Error in pMonAdd: %th exponent: %d != (%d == %d + %d)", |
---|
677 | i, pGetExp(p1, i), pGetExp(ptemp, i), pGetExp(p2, i), |
---|
678 | pGetExp(p3, i)); |
---|
679 | } |
---|
680 | } |
---|
681 | pSetComp(ptemp, pGetComp(p2) + pGetComp(p3)); |
---|
682 | pSetm(ptemp); |
---|
683 | |
---|
684 | if (! pEqual(ptemp, p1)) |
---|
685 | Warn("Error in pMonAdd in %s:%d", f, l); |
---|
686 | pFree1(ptemp); |
---|
687 | } |
---|
688 | } |
---|
689 | |
---|
690 | static BOOLEAN OldpDivisibleBy(poly a, poly b) |
---|
691 | { |
---|
692 | if ((a!=NULL)&&((pGetComp(a)==0) || (pGetComp(a) == pGetComp(b)))) |
---|
693 | { |
---|
694 | for (int i = 1; i<=pVariables; i++) |
---|
695 | if (pGetExp(a, i) > pGetExp(b,i)) return FALSE; |
---|
696 | return TRUE; |
---|
697 | } |
---|
698 | return FALSE; |
---|
699 | } |
---|
700 | |
---|
701 | |
---|
702 | BOOLEAN pDBDivisibleBy(poly a, poly b, char* f, int l) |
---|
703 | { |
---|
704 | BOOLEAN istrue = OldpDivisibleBy(a,b); |
---|
705 | BOOLEAN f_istrue = _pDivisibleBy_orig(a, b); |
---|
706 | |
---|
707 | if (istrue != f_istrue) |
---|
708 | { |
---|
709 | Warn("Error in pDivisibleBy in %s:%d\n", f, l); |
---|
710 | _pDivisibleBy_orig(a, b); |
---|
711 | } |
---|
712 | return istrue; |
---|
713 | } |
---|
714 | |
---|
715 | BOOLEAN pDBDivisibleBy1(poly a, poly b, char* f, int l) |
---|
716 | { |
---|
717 | BOOLEAN istrue = OldpDivisibleBy(a,b); |
---|
718 | BOOLEAN f_istrue = _pDivisibleBy1_orig(a, b); |
---|
719 | |
---|
720 | if (istrue != f_istrue) |
---|
721 | { |
---|
722 | Warn("Error in pDivisibleBy1 in %s:%d\n", f, l); |
---|
723 | _pDivisibleBy1_orig(a, b); |
---|
724 | } |
---|
725 | return istrue; |
---|
726 | } |
---|
727 | |
---|
728 | BOOLEAN pDBDivisibleBy2(poly a, poly b, char* f, int l) |
---|
729 | { |
---|
730 | BOOLEAN istrue = OldpDivisibleBy(a,b); |
---|
731 | BOOLEAN f_istrue = __pDivisibleBy(a, b); |
---|
732 | |
---|
733 | if (istrue != f_istrue) |
---|
734 | { |
---|
735 | Warn("Error in pDivisibleBy2 in %s:%d\n", f, l); |
---|
736 | __pDivisibleBy(a, b); |
---|
737 | } |
---|
738 | return f_istrue; |
---|
739 | } |
---|
740 | |
---|
741 | #endif // defined(PDEBUG) && PDEBUG > 1 |
---|
742 | |
---|
743 | #ifdef PDEBUG |
---|
744 | BOOLEAN mmDBEqual(poly p, poly q, char *f, int l) |
---|
745 | { |
---|
746 | int i; |
---|
747 | |
---|
748 | for (i = 1; i<=pVariables; i++) |
---|
749 | { |
---|
750 | if (pGetExp(p,i) != pGetExp(q, i)) return FALSE; |
---|
751 | } |
---|
752 | if (pGetComp(p) != pGetComp(q)) return FALSE; |
---|
753 | if (__pEqual(p, q) != TRUE) |
---|
754 | { |
---|
755 | Warn("Error in pEqual: exp/comp same, bug monoms different in %s:%d", |
---|
756 | f, l); |
---|
757 | } |
---|
758 | return TRUE; |
---|
759 | } |
---|
760 | |
---|
761 | BOOLEAN prDBTest(poly p, ring r, char* f, int l) |
---|
762 | { |
---|
763 | ring old_ring = NULL; |
---|
764 | BOOLEAN res; |
---|
765 | |
---|
766 | if (r != currRing) |
---|
767 | { |
---|
768 | old_ring = currRing; |
---|
769 | rChangeCurrRing(r); |
---|
770 | } |
---|
771 | res = pDBTest(p, currRing->mm_specHeap, f, l); |
---|
772 | if (old_ring != NULL) |
---|
773 | { |
---|
774 | rChangeCurrRing(old_ring); |
---|
775 | } |
---|
776 | return res; |
---|
777 | } |
---|
778 | |
---|
779 | |
---|
780 | BOOLEAN pDBTest(poly p, char *f, int l) |
---|
781 | { |
---|
782 | return pDBTest(p, mm_specHeap, f,l); |
---|
783 | } |
---|
784 | |
---|
785 | BOOLEAN pDBTest(poly p, memHeap heap, char *f, int l) |
---|
786 | { |
---|
787 | poly old=NULL; |
---|
788 | BOOLEAN ismod=FALSE; |
---|
789 | if (heap == NULL) heap = mm_specHeap; |
---|
790 | |
---|
791 | while (p!=NULL) |
---|
792 | { |
---|
793 | if (heap != MM_UNKNOWN_HEAP) |
---|
794 | { |
---|
795 | #ifdef MDEBUG |
---|
796 | if (!mmDBTestHeapBlock(p, heap, f,l)) |
---|
797 | return FALSE; |
---|
798 | #elif defined(HEAP_DEBUG) |
---|
799 | if (! mmDebugCheckHeapAddr(p, heap, MM_HEAP_ADDR_USED_FLAG, f, l)) |
---|
800 | return FALSE; |
---|
801 | #endif |
---|
802 | } |
---|
803 | |
---|
804 | #ifdef LDEBUG |
---|
805 | if (!nDBTest(p->coef,f,l)) |
---|
806 | return FALSE; |
---|
807 | #endif |
---|
808 | if ((p->coef==NULL)&&(nGetChar()<2)) |
---|
809 | { |
---|
810 | Warn("NULL coef in poly in %s:%d\n",f,l); |
---|
811 | return FALSE; |
---|
812 | } |
---|
813 | if (nIsZero(p->coef)) |
---|
814 | { |
---|
815 | Warn("zero coef in poly in %s:%d\n",f,l); |
---|
816 | return FALSE; |
---|
817 | } |
---|
818 | int i=pVariables; |
---|
819 | for(;i;i--) |
---|
820 | { |
---|
821 | if (pGetExp(p,i)<0) |
---|
822 | { |
---|
823 | Warn("neg. Exponent %d of x(%d) in %s:%d\n",pGetExp(p,i),i,f,l); |
---|
824 | return FALSE; |
---|
825 | } |
---|
826 | } |
---|
827 | if (pGetComp(p)<0) |
---|
828 | { |
---|
829 | Warn("neg Component in %s:%d\n",f,l); |
---|
830 | return FALSE; |
---|
831 | } |
---|
832 | if (ismod==0) |
---|
833 | { |
---|
834 | if (pGetComp(p)>0) ismod=1; |
---|
835 | else ismod=2; |
---|
836 | } |
---|
837 | else if (ismod==1) |
---|
838 | { |
---|
839 | if (pGetComp(p)==0) |
---|
840 | { |
---|
841 | Warn("mix vec./poly in %s:%d\n",f,l); |
---|
842 | return FALSE; |
---|
843 | } |
---|
844 | } |
---|
845 | else if (ismod==2) |
---|
846 | { |
---|
847 | if (pGetComp(p)!=0) |
---|
848 | { |
---|
849 | Warn("mix poly/vec. in %s:%d\n",f,l); |
---|
850 | return FALSE; |
---|
851 | } |
---|
852 | } |
---|
853 | if (currRing->order[1] == ringorder_S) |
---|
854 | { |
---|
855 | long c1, cc1, ccc1, ec1; |
---|
856 | sro_ord* o = &(currRing->typ[1]); |
---|
857 | |
---|
858 | c1 = pGetComp(p); |
---|
859 | cc1 = o->data.syzcomp.Components[c1]; |
---|
860 | ccc1 = o->data.syzcomp.ShiftedComponents[cc1]; |
---|
861 | if (! (c1 == 0 || cc1 != 0)) |
---|
862 | { |
---|
863 | Warn("Component <-> TrueComponent zero mismatch\n", f, l); |
---|
864 | return FALSE; |
---|
865 | } |
---|
866 | if (! (c1 == 0 || ccc1 != 0)) |
---|
867 | { |
---|
868 | Warn("Component <-> ShiftedComponent zero mismatch\n", f, l); |
---|
869 | return FALSE; |
---|
870 | } |
---|
871 | ec1 = p->exp.l[currRing->typ[1].data.syzcomp.place]; |
---|
872 | if (ec1 != ccc1) |
---|
873 | { |
---|
874 | Warn("Shifted comp out of sync. should %d, is %d", ccc1, ec1); |
---|
875 | return FALSE; |
---|
876 | } |
---|
877 | } |
---|
878 | if (currRing->order[0] == ringorder_s) |
---|
879 | { |
---|
880 | unsigned long syzindex = p->exp.l[currRing->typ[0].data.syz.place]; |
---|
881 | pSetm(p); |
---|
882 | if (p->exp.l[currRing->typ[0].data.syz.place] != syzindex) |
---|
883 | { |
---|
884 | Warn("Syzindex wrong: Was %dl but should be %d in %s:%d\n", |
---|
885 | syzindex, p->exp.l[currRing->typ[0].data.syz.place], f, l); |
---|
886 | } |
---|
887 | } |
---|
888 | old=p; |
---|
889 | pIter(p); |
---|
890 | if (pComp(old,p)!=1) |
---|
891 | { |
---|
892 | Warn("wrong order ("); |
---|
893 | wrp(old); |
---|
894 | Print(") in %s:%d (pComp=%d)\n",f,l,pComp(old,p)); |
---|
895 | return FALSE; |
---|
896 | } |
---|
897 | if (p != NULL) |
---|
898 | { |
---|
899 | if (pGetComp(old) == pGetComp(p)) |
---|
900 | { |
---|
901 | i = pVariables; |
---|
902 | for (i=pVariables;i; i--) |
---|
903 | { |
---|
904 | if (pGetExp(old, i) != pGetExp(p, i)) break; |
---|
905 | } |
---|
906 | if (i == 0) |
---|
907 | { |
---|
908 | Warn("different Compare, but same exponent vector for"); |
---|
909 | wrp(old); |
---|
910 | Print(" in %s%d\n", f, l); |
---|
911 | return FALSE; |
---|
912 | } |
---|
913 | } |
---|
914 | } |
---|
915 | } |
---|
916 | return TRUE; |
---|
917 | } |
---|
918 | #endif // PDEBUG |
---|
919 | |
---|
920 | static unsigned long GetBitFields(Exponent_t e, |
---|
921 | unsigned int s, unsigned int n) |
---|
922 | { |
---|
923 | unsigned int i = 0, ev = 0; |
---|
924 | assume(n > 0 && s < BIT_SIZEOF_LONG); |
---|
925 | do |
---|
926 | { |
---|
927 | assume(s+i < BIT_SIZEOF_LONG); |
---|
928 | if (e > (Exponent_t) i) ev |= Sy_bit(s+i); |
---|
929 | else break; |
---|
930 | i++; |
---|
931 | } |
---|
932 | while (i < n); |
---|
933 | return ev; |
---|
934 | } |
---|
935 | |
---|
936 | // Short Exponent Vectors are used for fast divisibility tests |
---|
937 | // ShortExpVectors "squeeze" an exponent vector into one word as follows: |
---|
938 | // Let n = BIT_SIZEOF_LONG / pVariables. |
---|
939 | // If n == 0 (i.e. pVariables > BIT_SIZE_OF_LONG), let m == the number |
---|
940 | // of non-zero exponents. If (m>BIT_SIZEOF_LONG), then sev = ~0, else |
---|
941 | // first m bits of sev are set to 1. |
---|
942 | // Otherwise (i.e. pVariables <= BIT_SIZE_OF_LONG) |
---|
943 | // represented by a bit-field of length n (resp. n+1 for some |
---|
944 | // exponents). If the value of an exponent is greater or equal to n, then |
---|
945 | // all of its respective n bits are set to 1. If the value of an exponent |
---|
946 | // is smaller than n, say m, then only the first m bits of the respective |
---|
947 | // n bits are set to 1, the others are set to 0. |
---|
948 | // This way, we have: |
---|
949 | // exp1 / exp2 ==> (ev1 & ~ev2) == 0, i.e., |
---|
950 | // if (ev1 & ~ev2) then exp1 does not divide exp2 |
---|
951 | unsigned long pGetShortExpVector(poly p) |
---|
952 | { |
---|
953 | assume(p != NULL); |
---|
954 | if (p == NULL) return 0; |
---|
955 | unsigned long ev = 0; // short exponent vector |
---|
956 | unsigned int n = BIT_SIZEOF_LONG / pVariables; // number of bits per exp |
---|
957 | unsigned int m1; // highest bit which is filled with (n+1) |
---|
958 | unsigned int i = 0, j=1; |
---|
959 | |
---|
960 | if (n == 0) |
---|
961 | { |
---|
962 | for (; j<=(unsigned long) pVariables; j++) |
---|
963 | { |
---|
964 | if (pGetExp(p,j) > 0) i++; |
---|
965 | if (i == BIT_SIZEOF_LONG) break; |
---|
966 | } |
---|
967 | ev = (unsigned long) ~0 >> ((unsigned long) (BIT_SIZEOF_LONG - i)); |
---|
968 | return ev; |
---|
969 | } |
---|
970 | else |
---|
971 | { |
---|
972 | m1 = (n+1)*(BIT_SIZEOF_LONG - n*pVariables); |
---|
973 | } |
---|
974 | |
---|
975 | n++; |
---|
976 | while (i<m1) |
---|
977 | { |
---|
978 | ev |= GetBitFields(pGetExp(p, j), i, n); |
---|
979 | i += n; |
---|
980 | j++; |
---|
981 | } |
---|
982 | |
---|
983 | n--; |
---|
984 | while (i<BIT_SIZEOF_LONG) |
---|
985 | { |
---|
986 | ev |= GetBitFields(pGetExp(p, j), i, n); |
---|
987 | i += n; |
---|
988 | j++; |
---|
989 | } |
---|
990 | return ev; |
---|
991 | } |
---|
992 | |
---|
993 | #ifdef PDIV_DEBUG |
---|
994 | static int pDivisibleBy_number = 1; |
---|
995 | static int pDivisibleBy_FALSE = 1; |
---|
996 | static int pDivisibleBy_ShortFalse = 1; |
---|
997 | static int pDivisibleBy_Null = 1; |
---|
998 | BOOLEAN pDBShortDivisibleBy(poly p1, unsigned long sev_1, |
---|
999 | poly p2, unsigned long not_sev_2, |
---|
1000 | char* f, int l) |
---|
1001 | { |
---|
1002 | if (sev_1 != 0 && pGetShortExpVector(p1) != sev_1) |
---|
1003 | { |
---|
1004 | Warn("sev1 is %o but should be %o in %s:%d\n", sev_1, |
---|
1005 | pGetShortExpVector(p1), f, l); |
---|
1006 | assume(0); |
---|
1007 | } |
---|
1008 | if (~ pGetShortExpVector(p2) != not_sev_2) |
---|
1009 | { |
---|
1010 | Warn("not_sev2 is %o but should be %o in %s:%d\n", not_sev_2, |
---|
1011 | ~ pGetShortExpVector(p2), f, l); |
---|
1012 | assume(0); |
---|
1013 | } |
---|
1014 | if (sev_1 == 0) pDivisibleBy_Null++; |
---|
1015 | pDivisibleBy_number++; |
---|
1016 | BOOLEAN ret = pDivisibleBy(p1, p2); |
---|
1017 | if (! ret) pDivisibleBy_FALSE++; |
---|
1018 | if (sev_1 & not_sev_2) |
---|
1019 | { |
---|
1020 | pDivisibleBy_ShortFalse++; |
---|
1021 | if (ret) |
---|
1022 | { |
---|
1023 | Warn("p1 divides p2, but sev's are wrong in %s:%d\n", f, l); |
---|
1024 | assume(0); |
---|
1025 | } |
---|
1026 | } |
---|
1027 | return ret; |
---|
1028 | } |
---|
1029 | |
---|
1030 | void pPrintDivisbleByStat() |
---|
1031 | { |
---|
1032 | Print("#Tests: %d; #FALSE %d(%d); #SHORT %d(%d) #NULL:%d(%d)\n", |
---|
1033 | pDivisibleBy_number, |
---|
1034 | pDivisibleBy_FALSE, pDivisibleBy_FALSE*100/pDivisibleBy_number, |
---|
1035 | pDivisibleBy_ShortFalse, pDivisibleBy_ShortFalse*100/pDivisibleBy_FALSE, |
---|
1036 | pDivisibleBy_Null, pDivisibleBy_Null*100/pDivisibleBy_number); |
---|
1037 | |
---|
1038 | } |
---|
1039 | #endif |
---|
1040 | |
---|
1041 | #endif // POLYS_IMPL_CC |
---|