1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: polys-impl.cc,v 1.22 1999-03-15 16:55:41 Singular 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 "ipid.h" |
---|
31 | |
---|
32 | /*************************************************************** |
---|
33 | * |
---|
34 | * Low - level routines which deal with var indicies |
---|
35 | * |
---|
36 | ***************************************************************/ |
---|
37 | // And here is how we determine the way exponents are stored: |
---|
38 | // There are the following four possibilities: |
---|
39 | // |
---|
40 | // |
---|
41 | // BIGENDIAN -- lex order |
---|
42 | // e_1, e_2, ... , e_n,..,comp: pVarOffset = -1, |
---|
43 | // pVarLowIndex = 0, |
---|
44 | // pVarHighIndex = pVariables-1 |
---|
45 | // pVarCompIndex = pVariables + #(..) |
---|
46 | // BIGENDIAN -- rev lex order |
---|
47 | // e_n, ... , e_2, e_1,..,comp: pVarOffset = pVariables, |
---|
48 | // pVarLowIndex = 0, |
---|
49 | // pVarHighIndex = pVariables-1 |
---|
50 | // pVarCompIndex = pVariables + #(..) |
---|
51 | // LITTLEENDIAN -- rev lex order |
---|
52 | // comp,.., e_1, e_2, ... , e_n : pVarOffset = #(..), |
---|
53 | // pVarLowIndex = 1 + #(..), |
---|
54 | // pVarHighIndex = #(..) + pVariables |
---|
55 | // pVarCompIndex = 0 |
---|
56 | // LITTLEENDIAN -- lex order |
---|
57 | // comp,..,e_n, .... , e_2, e_1 : pVarOffset = pVariables + 1 + #(..) |
---|
58 | // pVarLowIndex = 1 + #(..) |
---|
59 | // pVarHighIndex = #(..) + pVariables |
---|
60 | // pVarCompIndex = 0 |
---|
61 | // |
---|
62 | // Furthermore, the size of the exponent vector is always a multiple |
---|
63 | // of the word size -- "empty exponents" (exactly #(..) ones) are |
---|
64 | // filled in between comp and first/last exponent -- i.e. comp and |
---|
65 | // first/last exponent might not be next to each other |
---|
66 | |
---|
67 | void pGetVarIndicies_Lex(int nvars, int* VarOffset, int &VarCompIndex, |
---|
68 | int &VarLowIndex, int &VarHighIndex) |
---|
69 | { |
---|
70 | long temp = (nvars+1)*sizeof(Exponent_t); |
---|
71 | if ((temp % sizeof(long)) == 0) |
---|
72 | temp = temp / sizeof(long); |
---|
73 | else |
---|
74 | temp = (temp / sizeof(long)) + 1; // temp == pVariables1W |
---|
75 | #ifdef WORDS_BIGENDIAN |
---|
76 | VarCompIndex = temp * sizeof(long)/sizeof(Exponent_t) - 1; |
---|
77 | VarOffset[0] = VarCompIndex; |
---|
78 | for (temp=1; temp<=nvars; temp++) |
---|
79 | VarOffset[temp] = temp - 1; |
---|
80 | VarLowIndex = 0; |
---|
81 | VarHighIndex = nvars - 1; |
---|
82 | #else // ! WORDS_BIGENDIAN |
---|
83 | temp *= sizeof(long)/sizeof(Exponent_t); |
---|
84 | VarHighIndex = temp -1; |
---|
85 | VarLowIndex = temp - nvars; |
---|
86 | VarOffset[0] = 0; |
---|
87 | for (long i = 1; i<=nvars;i++) |
---|
88 | VarOffset[i] = temp - i; |
---|
89 | VarCompIndex = 0; |
---|
90 | #endif // WORDS_BIGENDIAN |
---|
91 | } |
---|
92 | |
---|
93 | void pGetVarIndicies_RevLex(int nvars,int *VarOffset,int &VarCompIndex, |
---|
94 | int &VarLowIndex, int &VarHighIndex) |
---|
95 | { |
---|
96 | long temp = (nvars+1)*sizeof(Exponent_t); |
---|
97 | if ((temp % sizeof(long)) == 0) |
---|
98 | temp = temp / sizeof(long); |
---|
99 | else |
---|
100 | temp = (temp / sizeof(long)) + 1; // temp == pVariables1W |
---|
101 | #ifdef WORDS_BIGENDIAN |
---|
102 | VarCompIndex = temp * sizeof(long)/sizeof(Exponent_t) - 1; |
---|
103 | VarOffset[0] = VarCompIndex; |
---|
104 | for (temp=1; temp <= nvars; temp++) |
---|
105 | VarOffset[temp] = nvars - temp; |
---|
106 | VarLowIndex = 0; |
---|
107 | VarHighIndex = nvars-1; |
---|
108 | #else // ! WORDS_BIGENDIAN |
---|
109 | temp *= sizeof(long)/sizeof(Exponent_t); |
---|
110 | VarHighIndex = temp -1; |
---|
111 | VarLowIndex = temp - nvars; |
---|
112 | VarOffset[0] = 0; |
---|
113 | for (int i = 1; i<=nvars; i++) |
---|
114 | VarOffset[i] = temp - nvars - 1 + i; |
---|
115 | VarCompIndex = 0; |
---|
116 | #endif // WORDS_BIGENDIAN |
---|
117 | } |
---|
118 | |
---|
119 | void pGetVarIndicies(int nvars, int *VarOffset, int &VarCompIndex, |
---|
120 | int &VarLowIndex, int &VarHighIndex) |
---|
121 | { |
---|
122 | pGetVarIndicies_Lex(nvars,VarOffset, VarCompIndex, VarLowIndex,VarHighIndex); |
---|
123 | } |
---|
124 | |
---|
125 | // gets var indicies w.r.t. the ring r |
---|
126 | void pGetVarIndicies(ring r, int *VarOffset, int &VarCompIndex, |
---|
127 | int &VarLowIndex, int &VarHighIndex) |
---|
128 | { |
---|
129 | // at the moment, non-default var indicies are only used for simple orderings |
---|
130 | if (rHasSimpleOrder(r)) |
---|
131 | { |
---|
132 | short s_order; |
---|
133 | if (r->order[0] == ringorder_c || r->order[0] == ringorder_C) |
---|
134 | s_order = r->order[1]; |
---|
135 | else |
---|
136 | s_order = r->order[0]; |
---|
137 | |
---|
138 | switch(s_order) |
---|
139 | { |
---|
140 | case ringorder_dp: |
---|
141 | case ringorder_wp: |
---|
142 | case ringorder_ds: |
---|
143 | case ringorder_ws: |
---|
144 | case ringorder_unspec: |
---|
145 | pGetVarIndicies_RevLex(r->N, VarOffset, VarCompIndex, |
---|
146 | VarLowIndex, VarHighIndex); |
---|
147 | break; |
---|
148 | |
---|
149 | #ifdef PDEBUG |
---|
150 | case ringorder_lp: |
---|
151 | case ringorder_Dp: |
---|
152 | case ringorder_Wp: |
---|
153 | case ringorder_Ds: |
---|
154 | case ringorder_Ws: |
---|
155 | case ringorder_ls: |
---|
156 | #else |
---|
157 | default: |
---|
158 | #endif |
---|
159 | pGetVarIndicies_Lex(r->N, VarOffset, VarCompIndex, |
---|
160 | VarLowIndex, VarHighIndex); |
---|
161 | #ifdef PDEBUG |
---|
162 | break; |
---|
163 | default: |
---|
164 | Werror("wrong internal ordering:%d at %s, l:%d\n", |
---|
165 | s_order,__FILE__,__LINE__); |
---|
166 | #endif |
---|
167 | } |
---|
168 | } |
---|
169 | else |
---|
170 | // default var indicies are used |
---|
171 | pGetVarIndicies(r->N, VarOffset, VarCompIndex, VarLowIndex, VarHighIndex); |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | inline void RingCopy2ExpV(poly dest, poly src, ring src_r) |
---|
176 | { |
---|
177 | for (int i=pVariables; i; i--) |
---|
178 | pSetExp(dest, i, pRingGetExp(src_r, src, i)); |
---|
179 | pSetComp(dest, pRingGetComp(src_r, src)); |
---|
180 | } |
---|
181 | |
---|
182 | // Returns a converted copy (in the sense that the returned poly is |
---|
183 | // poly of currRing) of poly p which is from ring r -- assumes that |
---|
184 | // currRing and r have the same number of variables, i.e. that polys |
---|
185 | // from r can be "fetched" into currRing |
---|
186 | #ifdef MDEBUG |
---|
187 | poly pDBFetchCopy(ring r, poly p,char *f,int l) |
---|
188 | #else |
---|
189 | poly _pFetchCopy(ring r, poly p) |
---|
190 | #endif |
---|
191 | { |
---|
192 | poly res; |
---|
193 | poly a; |
---|
194 | if (p==NULL) return NULL; |
---|
195 | if (r->VarOffset == pVarOffset) |
---|
196 | { |
---|
197 | #ifdef MDEBUG |
---|
198 | res = a = pDBNew(f,l); |
---|
199 | #else |
---|
200 | res = a = pNew(); |
---|
201 | #endif |
---|
202 | pCopy2(a,p); |
---|
203 | a->coef=nCopy(p->coef); |
---|
204 | pSetm(a); |
---|
205 | pIter(p); |
---|
206 | while (p!=NULL) |
---|
207 | { |
---|
208 | #ifdef MDEBUG |
---|
209 | a = pNext(a) = pDBNew(f,l); |
---|
210 | #else |
---|
211 | a = pNext(a) = pNew(); |
---|
212 | #endif |
---|
213 | pCopy2(a,p); |
---|
214 | a->coef=nCopy(p->coef); |
---|
215 | pSetm(a); |
---|
216 | pIter(p); |
---|
217 | } |
---|
218 | pNext(a) = NULL; |
---|
219 | } |
---|
220 | else |
---|
221 | { |
---|
222 | #ifdef MDEBUG |
---|
223 | a = res = pDBInit(f,l); |
---|
224 | #else |
---|
225 | a = res = pInit(); |
---|
226 | #endif |
---|
227 | res->coef = nCopy(p->coef); |
---|
228 | RingCopy2ExpV(res, p, r); |
---|
229 | pSetm(res); |
---|
230 | pIter(p); |
---|
231 | while (p != NULL) |
---|
232 | { |
---|
233 | // the VarOffset's are different: Hence we |
---|
234 | // convert betweeen a lex order and a revlex order -- to speed |
---|
235 | // up the sorting, we assemble new poly in inverse order |
---|
236 | #ifdef MDEBUG |
---|
237 | res = pDBInit(f,l); |
---|
238 | #else |
---|
239 | res = pInit(); |
---|
240 | #endif |
---|
241 | pNext(res) = a; |
---|
242 | a = res; |
---|
243 | res->coef = nCopy(p->coef); |
---|
244 | RingCopy2ExpV(res, p, r); |
---|
245 | pSetm(res); |
---|
246 | pIter(p); |
---|
247 | } |
---|
248 | } |
---|
249 | #ifdef PDEBUG |
---|
250 | res = pOrdPolyMerge(res); |
---|
251 | pTest(res); |
---|
252 | return res; |
---|
253 | #else |
---|
254 | return pOrdPolyMerge(res); |
---|
255 | #endif |
---|
256 | } |
---|
257 | |
---|
258 | |
---|
259 | /*************************************************************** |
---|
260 | * |
---|
261 | * Storage Managament Routines |
---|
262 | * |
---|
263 | ***************************************************************/ |
---|
264 | |
---|
265 | |
---|
266 | /*2 |
---|
267 | * create a new monomial and init |
---|
268 | */ |
---|
269 | #ifdef MDEBUG |
---|
270 | poly pDBInit(char * f, int l) |
---|
271 | { |
---|
272 | poly p=pDBNew(f,l); |
---|
273 | memset(p,0, pMonomSize); |
---|
274 | nNew(&(p->coef)); |
---|
275 | return p; |
---|
276 | } |
---|
277 | #endif |
---|
278 | |
---|
279 | /*2 |
---|
280 | * delete a poly, resets pointer |
---|
281 | * put the monomials in the freelist |
---|
282 | */ |
---|
283 | #ifdef MDEBUG |
---|
284 | void pDBDelete(poly * p, char * f, int l) |
---|
285 | { |
---|
286 | poly h = *p; |
---|
287 | |
---|
288 | while (h!=NULL) |
---|
289 | { |
---|
290 | #ifdef LDEBUG |
---|
291 | nDBDelete(&(h->coef),f,l); |
---|
292 | #else |
---|
293 | nDelete(&(h->coef)); |
---|
294 | #endif |
---|
295 | pIter(h); |
---|
296 | #ifdef MDEBUG |
---|
297 | pDBFree1((ADDRESS)*p,f,l); |
---|
298 | #else |
---|
299 | pFree1((ADDRESS)*p); |
---|
300 | #endif |
---|
301 | *p=h; |
---|
302 | if (l>0) l= -l; |
---|
303 | } |
---|
304 | *p = NULL; |
---|
305 | } |
---|
306 | #else |
---|
307 | void _pDelete(poly* p) |
---|
308 | { |
---|
309 | poly h = *p; |
---|
310 | poly pp; |
---|
311 | |
---|
312 | while (h!=NULL) |
---|
313 | { |
---|
314 | nDelete(&(h->coef)); |
---|
315 | pp=h; |
---|
316 | pIter(h); |
---|
317 | pFree1((ADDRESS)pp); |
---|
318 | } |
---|
319 | *p = NULL; |
---|
320 | } |
---|
321 | #endif |
---|
322 | |
---|
323 | /*2 |
---|
324 | * remove first monom |
---|
325 | */ |
---|
326 | #ifdef MDEBUG |
---|
327 | void pDBDelete1(poly * p, char * f, int l) |
---|
328 | { |
---|
329 | poly h = *p; |
---|
330 | |
---|
331 | if (h==NULL) return; |
---|
332 | nDelete(&(h->coef)); |
---|
333 | *p = pNext(h); |
---|
334 | #ifdef MDEBUG |
---|
335 | pDBFree1((ADDRESS)h,f,l); |
---|
336 | #else |
---|
337 | pFree1((ADDRESS)h); |
---|
338 | #endif |
---|
339 | } |
---|
340 | #else |
---|
341 | void _pDelete1(poly* p) |
---|
342 | { |
---|
343 | poly h = *p; |
---|
344 | |
---|
345 | if (h==NULL) return; |
---|
346 | nDelete(&(h->coef)); |
---|
347 | *p = pNext(h); |
---|
348 | pFree1((ADDRESS)h); |
---|
349 | } |
---|
350 | #endif |
---|
351 | |
---|
352 | void ppDelete(poly* p, ring rg) |
---|
353 | { |
---|
354 | ring origRing = currRing; |
---|
355 | rChangeCurrRing(rg, FALSE); |
---|
356 | pDelete(p); |
---|
357 | rChangeCurrRing(origRing, FALSE); |
---|
358 | } |
---|
359 | |
---|
360 | /*2 |
---|
361 | * creates a copy of p |
---|
362 | */ |
---|
363 | #ifdef MDEBUG |
---|
364 | poly pDBCopy(poly p,char *f,int l) |
---|
365 | #else |
---|
366 | poly _pCopy(poly p) |
---|
367 | #endif |
---|
368 | { |
---|
369 | poly w, a; |
---|
370 | |
---|
371 | if (p==NULL) return NULL; |
---|
372 | pDBTest(p,f,l); |
---|
373 | #ifdef MDEBUG |
---|
374 | w = a = pDBNew(f,l); |
---|
375 | #else |
---|
376 | w = a = pNew(); |
---|
377 | #endif |
---|
378 | pCopy2(w,p); |
---|
379 | w->coef=nCopy(p->coef); |
---|
380 | if (pNext(p)!=NULL) |
---|
381 | { |
---|
382 | pIter(p); |
---|
383 | do |
---|
384 | { |
---|
385 | #ifdef MDEBUG |
---|
386 | a = pNext(a) = pDBNew(f,l); |
---|
387 | #else |
---|
388 | a = pNext(a) = pNew(); |
---|
389 | #endif |
---|
390 | pCopy2(a,p); |
---|
391 | a->coef=nCopy(p->coef); |
---|
392 | pIter(p); |
---|
393 | } |
---|
394 | while (p!=NULL); |
---|
395 | } |
---|
396 | pNext(a) = NULL; |
---|
397 | return w; |
---|
398 | } |
---|
399 | |
---|
400 | |
---|
401 | /*2 |
---|
402 | * creates a copy of the initial monomial of p |
---|
403 | * sets the coeff of the copy to a defined value |
---|
404 | */ |
---|
405 | #ifdef MDEBUG |
---|
406 | poly pDBCopy1(poly p,char *f,int l) |
---|
407 | #else |
---|
408 | poly _pCopy1(poly p) |
---|
409 | #endif |
---|
410 | { |
---|
411 | poly w; |
---|
412 | #ifdef MDEBUG |
---|
413 | w = pDBNew(f,l); |
---|
414 | #else |
---|
415 | w = pNew(); |
---|
416 | #endif |
---|
417 | pCopy2(w,p); |
---|
418 | nNew(&(w->coef)); |
---|
419 | pNext(w) = NULL; |
---|
420 | return w; |
---|
421 | } |
---|
422 | |
---|
423 | /*2 |
---|
424 | * returns (a copy of) the head term of a |
---|
425 | */ |
---|
426 | #ifdef MDEBUG |
---|
427 | poly pDBHead(poly p,char *f, int l) |
---|
428 | #else |
---|
429 | poly _pHead(poly p) |
---|
430 | #endif |
---|
431 | { |
---|
432 | poly w=NULL; |
---|
433 | |
---|
434 | if (p!=NULL) |
---|
435 | { |
---|
436 | #ifdef MDEBUG |
---|
437 | w = pDBNew(f,l); |
---|
438 | #else |
---|
439 | w = pNew(); |
---|
440 | #endif |
---|
441 | pCopy2(w,p); |
---|
442 | pSetCoeff0(w,nCopy(pGetCoeff(p))); |
---|
443 | pNext(w) = NULL; |
---|
444 | } |
---|
445 | return w; |
---|
446 | } |
---|
447 | |
---|
448 | poly pHeadProc(poly p) |
---|
449 | { |
---|
450 | return pHead(p); |
---|
451 | } |
---|
452 | |
---|
453 | /*2 |
---|
454 | * returns (a copy of) the head term of a without the coef |
---|
455 | */ |
---|
456 | #ifdef MDEBUG |
---|
457 | poly pDBHead0(poly p,char *f, int l) |
---|
458 | #else |
---|
459 | poly _pHead0(poly p) |
---|
460 | #endif |
---|
461 | { |
---|
462 | poly w=NULL; |
---|
463 | |
---|
464 | if (p!=NULL) |
---|
465 | { |
---|
466 | #ifdef PDEBUG |
---|
467 | w = pDBNew(f,l); |
---|
468 | #else |
---|
469 | w = pNew(); |
---|
470 | #endif |
---|
471 | pCopy2(w,p); |
---|
472 | pSetCoeff0(w,NULL); |
---|
473 | pNext(w) = NULL; |
---|
474 | } |
---|
475 | return w; |
---|
476 | } |
---|
477 | |
---|
478 | |
---|
479 | /*************************************************************** |
---|
480 | * |
---|
481 | * Routines for turned on debugging |
---|
482 | * |
---|
483 | ***************************************************************/ |
---|
484 | |
---|
485 | #ifdef PDEBUG |
---|
486 | |
---|
487 | #if PDEBUG != 0 |
---|
488 | Exponent_t pPDSetExp(poly p, int v, Exponent_t e, char* f, int l) |
---|
489 | { |
---|
490 | if (v == 0) |
---|
491 | { |
---|
492 | Print("zero index to exponent in %s:%d\n", f, l); |
---|
493 | } |
---|
494 | if (v > pVariables) |
---|
495 | { |
---|
496 | Print("index %d to exponent too large in %s:%d\n", v, f, l); |
---|
497 | } |
---|
498 | return (p)->exp[_pExpIndex(v)]=(e); |
---|
499 | } |
---|
500 | |
---|
501 | Exponent_t pPDGetExp(poly p, int v, char* f, int l) |
---|
502 | { |
---|
503 | if (v == 0) |
---|
504 | { |
---|
505 | Print("zero index to exponent in %s:%d\n", f, l); |
---|
506 | } |
---|
507 | if (v > pVariables) |
---|
508 | { |
---|
509 | Print("index %d to exponent too large in %s:%d\n", v, f, l); |
---|
510 | } |
---|
511 | return (p)->exp[_pExpIndex(v)]; |
---|
512 | } |
---|
513 | |
---|
514 | Exponent_t pPDRingSetExp(ring r, poly p, int v, Exponent_t e, char* f, int l) |
---|
515 | { |
---|
516 | if (v == 0) |
---|
517 | { |
---|
518 | Print("zero index to exponent in %s:%d\n", f, l); |
---|
519 | } |
---|
520 | if (v > r->N) |
---|
521 | { |
---|
522 | Print("index %d to exponent too large in %s:%d\n", v, f, l); |
---|
523 | } |
---|
524 | return (p)->exp[_pRingExpIndex(r, v)]=(e); |
---|
525 | } |
---|
526 | |
---|
527 | Exponent_t pPDRingGetExp(ring r, poly p, int v, char* f, int l) |
---|
528 | { |
---|
529 | if (v == 0) |
---|
530 | { |
---|
531 | Print("zero index to exponent in %s:%d\n", f, l); |
---|
532 | } |
---|
533 | if (v > r->N) |
---|
534 | { |
---|
535 | Print("index %d to exponent too large in %s:%d\n", v, f, l); |
---|
536 | } |
---|
537 | return (p)->exp[_pRingExpIndex(r,v)]; |
---|
538 | } |
---|
539 | |
---|
540 | Exponent_t pPDIncrExp(poly p, int v, char* f, int l) |
---|
541 | { |
---|
542 | if (v == 0) |
---|
543 | { |
---|
544 | Print("zero index to exponent in %s:%d\n", f, l); |
---|
545 | } |
---|
546 | if (v > pVariables) |
---|
547 | { |
---|
548 | Print("index %d to exponent too large in %s:%d\n", v, f, l); |
---|
549 | } |
---|
550 | return ((p)->exp[_pExpIndex(v)])++; |
---|
551 | } |
---|
552 | |
---|
553 | Exponent_t pPDDecrExp(poly p, int v, char* f, int l) |
---|
554 | { |
---|
555 | if (v == 0) |
---|
556 | { |
---|
557 | Print("zero index to exponent in %s:%d\n", f, l); |
---|
558 | } |
---|
559 | if (v > pVariables) |
---|
560 | { |
---|
561 | Print("index %d to exponent too large in %s:%d\n", v, f, l); |
---|
562 | } |
---|
563 | return ((p)->exp[_pExpIndex(v)])--; |
---|
564 | } |
---|
565 | |
---|
566 | Exponent_t pPDAddExp(poly p, int v, Exponent_t e, char* f, int l) |
---|
567 | { |
---|
568 | if (v == 0) |
---|
569 | { |
---|
570 | Print("zero index to exponent in %s:%d\n", f, l); |
---|
571 | } |
---|
572 | if (v > pVariables) |
---|
573 | { |
---|
574 | Print("index %d to exponent too large in %s:%d\n", v, f, l); |
---|
575 | } |
---|
576 | return ((p)->exp[_pExpIndex(v)]) += (e); |
---|
577 | } |
---|
578 | |
---|
579 | Exponent_t pPDSubExp(poly p, int v, Exponent_t e, char* f, int l) |
---|
580 | { |
---|
581 | if (v == 0) |
---|
582 | { |
---|
583 | Print("zero index to exponent in %s:%d\n", f, l); |
---|
584 | } |
---|
585 | if (v > pVariables) |
---|
586 | { |
---|
587 | Print("index %d to exponent too large in %s:%d\n", v, f, l); |
---|
588 | } |
---|
589 | return ((p)->exp[_pExpIndex(v)]) -= (e); |
---|
590 | } |
---|
591 | |
---|
592 | Exponent_t pPDMultExp(poly p, int v, Exponent_t e, char* f, int l) |
---|
593 | { |
---|
594 | if (v == 0) |
---|
595 | { |
---|
596 | Print("zero index to exponent in %s:%d\n", f, l); |
---|
597 | } |
---|
598 | if (v > pVariables) |
---|
599 | { |
---|
600 | Print("index %d to exponent too large in %s:%d\n", v, f, l); |
---|
601 | } |
---|
602 | return ((p)->exp[_pExpIndex(v)]) *= (e); |
---|
603 | } |
---|
604 | |
---|
605 | // checks whether fast monom add did not overflow |
---|
606 | void pDBMonAddFast(poly p1, poly p2, char* f, int l) |
---|
607 | { |
---|
608 | poly ptemp = pNew(); |
---|
609 | pCopy2(ptemp, p1); |
---|
610 | |
---|
611 | __pMonAddFast(p1, p2); |
---|
612 | |
---|
613 | for (int i=1; i<=pVariables; i++) |
---|
614 | { |
---|
615 | pAddExp(ptemp, i, pGetExp(p2, i)); |
---|
616 | } |
---|
617 | pGetOrder(ptemp) += pGetOrder(p2); |
---|
618 | |
---|
619 | if (! pEqual(ptemp, p1)) |
---|
620 | { |
---|
621 | Print("Error in pMonAddFast in %s:%d\n", f, l); |
---|
622 | } |
---|
623 | |
---|
624 | pFree1(ptemp); |
---|
625 | } |
---|
626 | |
---|
627 | void pDBCopyAddFast(poly p1, poly p2, poly p3, char* f, int l) |
---|
628 | { |
---|
629 | if (p2 == p1 || p3 == p1) |
---|
630 | { |
---|
631 | Print("Error in pCopyAddFast: Destination equals source in %s:%d\n", f, l); |
---|
632 | } |
---|
633 | poly ptemp = pNew(); |
---|
634 | __pCopyAddFast(ptemp, p2, p3); |
---|
635 | |
---|
636 | pCopy2(p1, p2); |
---|
637 | pDBMonAddFast(p1, p3, f, l); |
---|
638 | |
---|
639 | if (! pEqual(ptemp, p1)) |
---|
640 | Print("Error in pCopyMonAddFast in %s:%d\n", f, l); |
---|
641 | pFree1(ptemp); |
---|
642 | } |
---|
643 | |
---|
644 | void pDBCopyAddFastHomog(poly p1, poly p2, poly p3, Order_t Order, |
---|
645 | char* f, int l) |
---|
646 | { |
---|
647 | pDBCopyAddFast(p1, p2, p3, f, l); |
---|
648 | if (p1->Order != Order) |
---|
649 | Print("Error in pCopyAddFastHomog: Order is different from sum\n"); |
---|
650 | } |
---|
651 | |
---|
652 | |
---|
653 | |
---|
654 | static BOOLEAN OldpDivisibleBy(poly a, poly b) |
---|
655 | { |
---|
656 | if ((a!=NULL)&&((pGetComp(a)==0) || (pGetComp(a) == pGetComp(b)))) |
---|
657 | { |
---|
658 | for (int i = 1; i<=pVariables; i++) |
---|
659 | if (pGetExp(a, i) > pGetExp(b,i)) return FALSE; |
---|
660 | return TRUE; |
---|
661 | } |
---|
662 | return FALSE; |
---|
663 | } |
---|
664 | |
---|
665 | |
---|
666 | BOOLEAN pDBDivisibleBy(poly a, poly b, char* f, int l) |
---|
667 | { |
---|
668 | BOOLEAN istrue = OldpDivisibleBy(a,b); |
---|
669 | BOOLEAN f_istrue = _pDivisibleBy_orig(a, b); |
---|
670 | |
---|
671 | if (istrue != f_istrue) |
---|
672 | { |
---|
673 | Print("Error in pDivisibleBy in %s:%d\n", f, l); |
---|
674 | _pDivisibleBy_orig(a, b); |
---|
675 | } |
---|
676 | return f_istrue; |
---|
677 | } |
---|
678 | |
---|
679 | BOOLEAN pDBDivisibleBy1(poly a, poly b, char* f, int l) |
---|
680 | { |
---|
681 | BOOLEAN istrue = OldpDivisibleBy(a,b); |
---|
682 | BOOLEAN f_istrue = _pDivisibleBy1_orig(a, b); |
---|
683 | |
---|
684 | if (istrue != f_istrue) |
---|
685 | { |
---|
686 | Print("Error in pDivisibleBy1 in %s:%d\n", f, l); |
---|
687 | _pDivisibleBy1_orig(a, b); |
---|
688 | } |
---|
689 | return f_istrue; |
---|
690 | } |
---|
691 | |
---|
692 | BOOLEAN pDBDivisibleBy2(poly a, poly b, char* f, int l) |
---|
693 | { |
---|
694 | BOOLEAN istrue = OldpDivisibleBy(a,b); |
---|
695 | BOOLEAN f_istrue = __pDivisibleBy(a, b); |
---|
696 | |
---|
697 | if (istrue != f_istrue) |
---|
698 | { |
---|
699 | Print("Error in pDivisibleBy2 in %s:%d\n", f, l); |
---|
700 | __pDivisibleBy(a, b); |
---|
701 | } |
---|
702 | return f_istrue; |
---|
703 | } |
---|
704 | |
---|
705 | #endif // PDEBUG != 0 |
---|
706 | |
---|
707 | BOOLEAN pDBTest(poly p, char *f, int l) |
---|
708 | { |
---|
709 | return pDBTest(p, mm_specHeap, f,l); |
---|
710 | } |
---|
711 | |
---|
712 | BOOLEAN pDBTest(poly p, memHeap tail_heap, memHeap lm_heap, char *f, int l) |
---|
713 | { |
---|
714 | if (tail_heap != lm_heap && lm_heap != NULL) |
---|
715 | { |
---|
716 | poly pn = pNext(p); |
---|
717 | BOOLEAN ret; |
---|
718 | pNext(p) = NULL; |
---|
719 | ret = pDBTest(pn, tail_heap, f, l) && pDBTest(p, lm_heap, f, l); |
---|
720 | pNext(p) = pn; |
---|
721 | return ret; |
---|
722 | } |
---|
723 | else |
---|
724 | { |
---|
725 | return pDBTest(p, tail_heap, f, l); |
---|
726 | } |
---|
727 | } |
---|
728 | |
---|
729 | BOOLEAN pDBTest(poly p, memHeap heap, char *f, int l) |
---|
730 | { |
---|
731 | poly old=NULL; |
---|
732 | BOOLEAN ismod=FALSE; |
---|
733 | while (p!=NULL) |
---|
734 | { |
---|
735 | #ifdef MDEBUG |
---|
736 | if (!mmDBTestHeapBlock(p, heap, f,l)) |
---|
737 | return FALSE; |
---|
738 | #elif defined(HEAP_DEBUG) |
---|
739 | if (! mmDebugCheckHeapAddr(p, heap, MM_HEAP_ADDR_USED_FLAG, f, l)) |
---|
740 | return FALSE; |
---|
741 | #endif |
---|
742 | #ifdef LDEBUG |
---|
743 | if (!nDBTest(p->coef,f,l)) |
---|
744 | return FALSE; |
---|
745 | #endif |
---|
746 | if ((p->coef==NULL)&&(nGetChar()<2)) |
---|
747 | { |
---|
748 | Print("NULL coef in poly in %s:%d\n",f,l); |
---|
749 | return FALSE; |
---|
750 | } |
---|
751 | if (nIsZero(p->coef)) |
---|
752 | { |
---|
753 | Print("zero coef in poly in %s:%d\n",f,l); |
---|
754 | return FALSE; |
---|
755 | } |
---|
756 | int i=pVariables; |
---|
757 | #ifndef DRING |
---|
758 | for(;i;i--) |
---|
759 | { |
---|
760 | if (pGetExp(p,i)<0) |
---|
761 | { |
---|
762 | Print("neg. Exponent in %s:%d\n",f,l); |
---|
763 | return FALSE; |
---|
764 | } |
---|
765 | } |
---|
766 | if (pGetComp(p)<0) |
---|
767 | { |
---|
768 | Print("neg Component in %s:%d\n",f,l); |
---|
769 | return FALSE; |
---|
770 | } |
---|
771 | #endif |
---|
772 | if (ismod==0) |
---|
773 | { |
---|
774 | if (pGetComp(p)>0) ismod=1; |
---|
775 | else ismod=2; |
---|
776 | } |
---|
777 | else if (ismod==1) |
---|
778 | { |
---|
779 | if (pGetComp(p)==0) |
---|
780 | { |
---|
781 | Print("mix vec./poly in %s:%d\n",f,l); |
---|
782 | return FALSE; |
---|
783 | } |
---|
784 | } |
---|
785 | else if (ismod==2) |
---|
786 | { |
---|
787 | if (pGetComp(p)!=0) |
---|
788 | { |
---|
789 | Print("mix poly/vec. in %s:%d\n",f,l); |
---|
790 | return FALSE; |
---|
791 | } |
---|
792 | } |
---|
793 | i=p->Order; |
---|
794 | pSetm(p); |
---|
795 | if(i!=p->Order) |
---|
796 | { |
---|
797 | Print("wrong ord-field in %s:%d\n",f,l); |
---|
798 | return FALSE; |
---|
799 | } |
---|
800 | old=p; |
---|
801 | pIter(p); |
---|
802 | if (pComp(old,p)!=1) |
---|
803 | { |
---|
804 | PrintS("wrong order ("); |
---|
805 | wrp(old); |
---|
806 | Print(") in %s:%d\n",f,l); |
---|
807 | return FALSE; |
---|
808 | } |
---|
809 | } |
---|
810 | return TRUE; |
---|
811 | } |
---|
812 | #endif // PDEBUG |
---|
813 | |
---|
814 | #define BIT_SIZEOF_LONG 8*SIZEOF_LONG |
---|
815 | unsigned long GetBitFields(Exponent_t e, |
---|
816 | unsigned int s, unsigned int n) |
---|
817 | { |
---|
818 | unsigned int i = 0, ev = 0; |
---|
819 | assume(n > 0 && s < BIT_SIZEOF_LONG); |
---|
820 | do |
---|
821 | { |
---|
822 | assume(s+i < BIT_SIZEOF_LONG); |
---|
823 | if (e > (Exponent_t) i) ev |= Sy_bit(s+i); |
---|
824 | else break; |
---|
825 | i++; |
---|
826 | } |
---|
827 | while (i < n); |
---|
828 | return ev; |
---|
829 | } |
---|
830 | |
---|
831 | unsigned long pGetShortExpVector(poly p) |
---|
832 | { |
---|
833 | unsigned long ev = 0; // short exponent vector |
---|
834 | unsigned int n = BIT_SIZEOF_LONG / pVariables; // number of bits per exp |
---|
835 | unsigned int m1; // highest bit which is filled with (n+1) |
---|
836 | unsigned int i = 0, j=0; |
---|
837 | |
---|
838 | if (n == 0) |
---|
839 | { |
---|
840 | n = 1; |
---|
841 | m1 = 0; |
---|
842 | } |
---|
843 | else |
---|
844 | { |
---|
845 | m1 = (n+1)*(BIT_SIZEOF_LONG - n*pVariables); |
---|
846 | } |
---|
847 | |
---|
848 | n++; |
---|
849 | while (i<m1) |
---|
850 | { |
---|
851 | ev |= GetBitFields(p->exp[pVarLowIndex + j], i, n); |
---|
852 | i += n; |
---|
853 | j++; |
---|
854 | } |
---|
855 | |
---|
856 | return ev; |
---|
857 | } |
---|
858 | |
---|
859 | #endif // POLYS_IMPL_CC |
---|