1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id$ */ |
---|
5 | |
---|
6 | /* |
---|
7 | * ABSTRACT - all basic methods to manipulate polynomials |
---|
8 | */ |
---|
9 | |
---|
10 | /* includes */ |
---|
11 | #include <stdio.h> |
---|
12 | #include <string.h> |
---|
13 | #include <ctype.h> |
---|
14 | #include <kernel/mod2.h> |
---|
15 | #include <kernel/options.h> |
---|
16 | #include <omalloc/omalloc.h> |
---|
17 | #include <kernel/febase.h> |
---|
18 | #include <kernel/numbers.h> |
---|
19 | #include <kernel/polys.h> |
---|
20 | #include <kernel/ring.h> |
---|
21 | #include <kernel/sbuckets.h> |
---|
22 | |
---|
23 | #ifdef HAVE_PLURAL |
---|
24 | #include <kernel/gring.h> |
---|
25 | #include <kernel/sca.h> |
---|
26 | #endif |
---|
27 | |
---|
28 | /* ----------- global variables, set by pSetGlobals --------------------- */ |
---|
29 | /* computes length and maximal degree of a POLYnomial */ |
---|
30 | pLDegProc pLDeg; |
---|
31 | /* computes the degree of the initial term, used for std */ |
---|
32 | pFDegProc pFDeg; |
---|
33 | /* the monomial ordering of the head monomials a and b */ |
---|
34 | /* returns -1 if a comes before b, 0 if a=b, 1 otherwise */ |
---|
35 | |
---|
36 | int pVariables; // number of variables |
---|
37 | |
---|
38 | /* 1 for polynomial ring, -1 otherwise */ |
---|
39 | int pOrdSgn; |
---|
40 | // it is of type int, not BOOLEAN because it is also in ip |
---|
41 | /* TRUE if the monomial ordering is not compatible with pFDeg */ |
---|
42 | BOOLEAN pLexOrder; |
---|
43 | |
---|
44 | /* ----------- global variables, set by procedures from hecke/kstd1 ----- */ |
---|
45 | /* the highest monomial below pHEdge */ |
---|
46 | poly ppNoether = NULL; |
---|
47 | |
---|
48 | /* -------------------------------------------------------- */ |
---|
49 | /*2 |
---|
50 | * change all global variables to fit the description of the new ring |
---|
51 | */ |
---|
52 | |
---|
53 | |
---|
54 | void pSetGlobals(const ring r, BOOLEAN complete) |
---|
55 | { |
---|
56 | int i; |
---|
57 | if (ppNoether!=NULL) pDelete(&ppNoether); |
---|
58 | pVariables = r->N; |
---|
59 | pOrdSgn = r->OrdSgn; |
---|
60 | pFDeg=r->pFDeg; |
---|
61 | pLDeg=r->pLDeg; |
---|
62 | pLexOrder=r->LexOrder; |
---|
63 | |
---|
64 | if (complete) |
---|
65 | { |
---|
66 | test &= ~ TEST_RINGDEP_OPTS; |
---|
67 | test |= r->options; |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | // resets the pFDeg and pLDeg: if pLDeg is not given, it is |
---|
72 | // set to currRing->pLDegOrig, i.e. to the respective LDegProc which |
---|
73 | // only uses pFDeg (and not pDeg, or pTotalDegree, etc) |
---|
74 | void pSetDegProcs(pFDegProc new_FDeg, pLDegProc new_lDeg) |
---|
75 | { |
---|
76 | assume(new_FDeg != NULL); |
---|
77 | pFDeg = new_FDeg; |
---|
78 | currRing->pFDeg = new_FDeg; |
---|
79 | |
---|
80 | if (new_lDeg == NULL) |
---|
81 | new_lDeg = currRing->pLDegOrig; |
---|
82 | |
---|
83 | pLDeg = new_lDeg; |
---|
84 | currRing->pLDeg = new_lDeg; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | // restores pFDeg and pLDeg: |
---|
89 | extern void pRestoreDegProcs(pFDegProc old_FDeg, pLDegProc old_lDeg) |
---|
90 | { |
---|
91 | assume(old_FDeg != NULL && old_lDeg != NULL); |
---|
92 | pFDeg = old_FDeg; |
---|
93 | currRing->pFDeg = old_FDeg; |
---|
94 | pLDeg = old_lDeg; |
---|
95 | currRing->pLDeg = old_lDeg; |
---|
96 | } |
---|
97 | |
---|
98 | /*2 |
---|
99 | * assumes that the head term of b is a multiple of the head term of a |
---|
100 | * and return the multiplicant *m |
---|
101 | * Frank's observation: If LM(b) = LM(a)*m, then we may actually set |
---|
102 | * negative(!) exponents in the below loop. I suspect that the correct |
---|
103 | * comment should be "assumes that LM(a) = LM(b)*m, for some monomial m..." |
---|
104 | */ |
---|
105 | poly pDivide(poly a, poly b) |
---|
106 | { |
---|
107 | int i; |
---|
108 | poly result = pInit(); |
---|
109 | |
---|
110 | for(i=(int)pVariables; i; i--) |
---|
111 | pSetExp(result,i, pGetExp(a,i)- pGetExp(b,i)); |
---|
112 | pSetComp(result, pGetComp(a) - pGetComp(b)); |
---|
113 | pSetm(result); |
---|
114 | return result; |
---|
115 | } |
---|
116 | |
---|
117 | #ifdef HAVE_RINGS //TODO Oliver |
---|
118 | #define pDiv_nn(p, n) p_Div_nn(p, n, currRing) |
---|
119 | |
---|
120 | poly p_Div_nn(poly p, const number n, const ring r) |
---|
121 | { |
---|
122 | pAssume(!n_IsZero(n,r)); |
---|
123 | p_Test(p, r); |
---|
124 | |
---|
125 | poly q = p; |
---|
126 | while (p != NULL) |
---|
127 | { |
---|
128 | number nc = pGetCoeff(p); |
---|
129 | pSetCoeff0(p, n_Div(nc, n, r)); |
---|
130 | n_Delete(&nc, r); |
---|
131 | pIter(p); |
---|
132 | } |
---|
133 | p_Test(q, r); |
---|
134 | return q; |
---|
135 | } |
---|
136 | #endif |
---|
137 | |
---|
138 | static void printNumber(const number z) |
---|
139 | { |
---|
140 | if (nIsZero(z)) printf("number = 0\n"); |
---|
141 | else |
---|
142 | { |
---|
143 | poly p = pOne(); |
---|
144 | pSetCoeff(p, nCopy(z)); |
---|
145 | pSetm(p); |
---|
146 | printf("number = %s\n", pString(p)); |
---|
147 | pDelete(&p); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | #ifdef HAVE_RINGS |
---|
152 | /* TRUE iff LT(f) | LT(g) */ |
---|
153 | BOOLEAN pDivisibleByRingCase(poly f, poly g) |
---|
154 | { |
---|
155 | int exponent; |
---|
156 | for(int i = (int)pVariables; i; i--) |
---|
157 | { |
---|
158 | exponent = pGetExp(g, i) - pGetExp(f, i); |
---|
159 | if (exponent < 0) return FALSE; |
---|
160 | } |
---|
161 | return nDivBy(pGetCoeff(g), pGetCoeff(f)); |
---|
162 | } |
---|
163 | #endif |
---|
164 | |
---|
165 | /*2 |
---|
166 | * divides a by the monomial b, ignores monomials which are not divisible |
---|
167 | * assumes that b is not NULL |
---|
168 | */ |
---|
169 | poly pDivideM(poly a, poly b) |
---|
170 | { |
---|
171 | if (a==NULL) return NULL; |
---|
172 | poly result=a; |
---|
173 | poly prev=NULL; |
---|
174 | int i; |
---|
175 | #ifdef HAVE_RINGS |
---|
176 | number inv=pGetCoeff(b); |
---|
177 | #else |
---|
178 | number inv=nInvers(pGetCoeff(b)); |
---|
179 | #endif |
---|
180 | |
---|
181 | while (a!=NULL) |
---|
182 | { |
---|
183 | if (pDivisibleBy(b,a)) |
---|
184 | { |
---|
185 | for(i=(int)pVariables; i; i--) |
---|
186 | pSubExp(a,i, pGetExp(b,i)); |
---|
187 | pSubComp(a, pGetComp(b)); |
---|
188 | pSetm(a); |
---|
189 | prev=a; |
---|
190 | pIter(a); |
---|
191 | } |
---|
192 | else |
---|
193 | { |
---|
194 | if (prev==NULL) |
---|
195 | { |
---|
196 | pLmDelete(&result); |
---|
197 | a=result; |
---|
198 | } |
---|
199 | else |
---|
200 | { |
---|
201 | pLmDelete(&pNext(prev)); |
---|
202 | a=pNext(prev); |
---|
203 | } |
---|
204 | } |
---|
205 | } |
---|
206 | #ifdef HAVE_RINGS |
---|
207 | if (nIsUnit(inv)) |
---|
208 | { |
---|
209 | inv = nInvers(inv); |
---|
210 | pMult_nn(result,inv); |
---|
211 | nDelete(&inv); |
---|
212 | } |
---|
213 | else |
---|
214 | { |
---|
215 | pDiv_nn(result,inv); |
---|
216 | } |
---|
217 | #else |
---|
218 | pMult_nn(result,inv); |
---|
219 | nDelete(&inv); |
---|
220 | #endif |
---|
221 | pDelete(&b); |
---|
222 | return result; |
---|
223 | } |
---|
224 | |
---|
225 | /*2 |
---|
226 | * returns the LCM of the head terms of a and b in *m |
---|
227 | */ |
---|
228 | void pLcm(poly a, poly b, poly m) |
---|
229 | { |
---|
230 | int i; |
---|
231 | for (i=pVariables; i; i--) |
---|
232 | { |
---|
233 | pSetExp(m,i, si_max( pGetExp(a,i), pGetExp(b,i))); |
---|
234 | } |
---|
235 | pSetComp(m, si_max(pGetComp(a), pGetComp(b))); |
---|
236 | /* Don't do a pSetm here, otherwise hres/lres chockes */ |
---|
237 | } |
---|
238 | |
---|
239 | /*2 |
---|
240 | * convert monomial given as string to poly, e.g. 1x3y5z |
---|
241 | */ |
---|
242 | const char * p_Read(const char *st, poly &rc, const ring r) |
---|
243 | { |
---|
244 | if (r==NULL) { rc=NULL;return st;} |
---|
245 | int i,j; |
---|
246 | rc = p_Init(r); |
---|
247 | const char *s = r->cf->nRead(st,&(rc->coef)); |
---|
248 | if (s==st) |
---|
249 | /* i.e. it does not start with a coeff: test if it is a ringvar*/ |
---|
250 | { |
---|
251 | j = r_IsRingVar(s,r); |
---|
252 | if (j >= 0) |
---|
253 | { |
---|
254 | p_IncrExp(rc,1+j,r); |
---|
255 | while (*s!='\0') s++; |
---|
256 | goto done; |
---|
257 | } |
---|
258 | } |
---|
259 | while (*s!='\0') |
---|
260 | { |
---|
261 | char ss[2]; |
---|
262 | ss[0] = *s++; |
---|
263 | ss[1] = '\0'; |
---|
264 | j = r_IsRingVar(ss,r); |
---|
265 | if (j >= 0) |
---|
266 | { |
---|
267 | const char *s_save=s; |
---|
268 | s = eati(s,&i); |
---|
269 | if (((unsigned long)i) > r->bitmask) |
---|
270 | { |
---|
271 | // exponent to large: it is not a monomial |
---|
272 | p_LmDelete(&rc,r); |
---|
273 | return s_save; |
---|
274 | } |
---|
275 | p_AddExp(rc,1+j, (long)i, r); |
---|
276 | } |
---|
277 | else |
---|
278 | { |
---|
279 | // 1st char of is not a varname |
---|
280 | p_LmDelete(&rc,r); |
---|
281 | s--; |
---|
282 | return s; |
---|
283 | } |
---|
284 | } |
---|
285 | done: |
---|
286 | if (r->cf->nIsZero(pGetCoeff(rc))) p_LmDelete(&rc,r); |
---|
287 | else |
---|
288 | { |
---|
289 | #ifdef HAVE_PLURAL |
---|
290 | // in super-commutative ring |
---|
291 | // squares of anti-commutative variables are zeroes! |
---|
292 | if(rIsSCA(r)) |
---|
293 | { |
---|
294 | const unsigned int iFirstAltVar = scaFirstAltVar(r); |
---|
295 | const unsigned int iLastAltVar = scaLastAltVar(r); |
---|
296 | |
---|
297 | assume(rc != NULL); |
---|
298 | |
---|
299 | for(unsigned int k = iFirstAltVar; k <= iLastAltVar; k++) |
---|
300 | if( p_GetExp(rc, k, r) > 1 ) |
---|
301 | { |
---|
302 | p_LmDelete(&rc, r); |
---|
303 | goto finish; |
---|
304 | } |
---|
305 | } |
---|
306 | #endif |
---|
307 | p_Setm(rc,r); |
---|
308 | } |
---|
309 | finish: |
---|
310 | return s; |
---|
311 | } |
---|
312 | |
---|
313 | BOOLEAN _p_Test(poly p, ring r, int level); |
---|
314 | poly pmInit(const char *st, BOOLEAN &ok) |
---|
315 | { |
---|
316 | poly p; |
---|
317 | const char *s=p_Read(st,p,currRing); |
---|
318 | if (*s!='\0') |
---|
319 | { |
---|
320 | if ((s!=st)&&isdigit(st[0])) |
---|
321 | { |
---|
322 | errorreported=TRUE; |
---|
323 | } |
---|
324 | ok=FALSE; |
---|
325 | pDelete(&p); |
---|
326 | return NULL; |
---|
327 | } |
---|
328 | #ifdef PDEBUG |
---|
329 | _p_Test(p,currRing,PDEBUG); |
---|
330 | #endif |
---|
331 | ok=!errorreported; |
---|
332 | return p; |
---|
333 | } |
---|
334 | |
---|
335 | /*2 |
---|
336 | *make p homogeneous by multiplying the monomials by powers of x_varnum |
---|
337 | *assume: deg(var(varnum))==1 |
---|
338 | */ |
---|
339 | poly pHomogen (poly p, int varnum) |
---|
340 | { |
---|
341 | pFDegProc deg; |
---|
342 | if (pLexOrder && (currRing->order[0]==ringorder_lp)) |
---|
343 | deg=p_Totaldegree; |
---|
344 | else |
---|
345 | deg=pFDeg; |
---|
346 | |
---|
347 | poly q=NULL, qn; |
---|
348 | int o,ii; |
---|
349 | sBucket_pt bp; |
---|
350 | |
---|
351 | if (p!=NULL) |
---|
352 | { |
---|
353 | if ((varnum < 1) || (varnum > pVariables)) |
---|
354 | { |
---|
355 | return NULL; |
---|
356 | } |
---|
357 | o=deg(p,currRing); |
---|
358 | q=pNext(p); |
---|
359 | while (q != NULL) |
---|
360 | { |
---|
361 | ii=deg(q,currRing); |
---|
362 | if (ii>o) o=ii; |
---|
363 | pIter(q); |
---|
364 | } |
---|
365 | q = pCopy(p); |
---|
366 | bp = sBucketCreate(currRing); |
---|
367 | while (q != NULL) |
---|
368 | { |
---|
369 | ii = o-deg(q,currRing); |
---|
370 | if (ii!=0) |
---|
371 | { |
---|
372 | pAddExp(q,varnum, (long)ii); |
---|
373 | pSetm(q); |
---|
374 | } |
---|
375 | qn = pNext(q); |
---|
376 | pNext(q) = NULL; |
---|
377 | sBucket_Add_p(bp, q, 1); |
---|
378 | q = qn; |
---|
379 | } |
---|
380 | sBucketDestroyAdd(bp, &q, &ii); |
---|
381 | } |
---|
382 | return q; |
---|
383 | } |
---|
384 | |
---|
385 | /*4 |
---|
386 | *Returns the exponent of the maximal power of the leading monomial of |
---|
387 | *p2 in that of p1 |
---|
388 | */ |
---|
389 | /*----------utilities for syzygies--------------*/ |
---|
390 | poly pTakeOutComp(poly * p, int k) |
---|
391 | { |
---|
392 | poly q = *p,qq=NULL,result = NULL; |
---|
393 | |
---|
394 | if (q==NULL) return NULL; |
---|
395 | BOOLEAN use_setmcomp=rOrd_SetCompRequiresSetm(currRing); |
---|
396 | if (pGetComp(q)==k) |
---|
397 | { |
---|
398 | result = q; |
---|
399 | do |
---|
400 | { |
---|
401 | pSetComp(q,0); |
---|
402 | if (use_setmcomp) pSetmComp(q); |
---|
403 | qq = q; |
---|
404 | pIter(q); |
---|
405 | } |
---|
406 | while ((q!=NULL) && (pGetComp(q)==k)); |
---|
407 | *p = q; |
---|
408 | pNext(qq) = NULL; |
---|
409 | } |
---|
410 | if (q==NULL) return result; |
---|
411 | if (pGetComp(q) > k) |
---|
412 | { |
---|
413 | pSubComp(q,1); |
---|
414 | if (use_setmcomp) pSetmComp(q); |
---|
415 | } |
---|
416 | poly pNext_q; |
---|
417 | while ((pNext_q=pNext(q))!=NULL) |
---|
418 | { |
---|
419 | if (pGetComp(pNext_q)==k) |
---|
420 | { |
---|
421 | if (result==NULL) |
---|
422 | { |
---|
423 | result = pNext_q; |
---|
424 | qq = result; |
---|
425 | } |
---|
426 | else |
---|
427 | { |
---|
428 | pNext(qq) = pNext_q; |
---|
429 | pIter(qq); |
---|
430 | } |
---|
431 | pNext(q) = pNext(pNext_q); |
---|
432 | pNext(qq) =NULL; |
---|
433 | pSetComp(qq,0); |
---|
434 | if (use_setmcomp) pSetmComp(qq); |
---|
435 | } |
---|
436 | else |
---|
437 | { |
---|
438 | /*pIter(q);*/ q=pNext_q; |
---|
439 | if (pGetComp(q) > k) |
---|
440 | { |
---|
441 | pSubComp(q,1); |
---|
442 | if (use_setmcomp) pSetmComp(q); |
---|
443 | } |
---|
444 | } |
---|
445 | } |
---|
446 | return result; |
---|
447 | } |
---|
448 | |
---|
449 | // Splits *p into two polys: *q which consists of all monoms with |
---|
450 | // component == comp and *p of all other monoms *lq == pLength(*q) |
---|
451 | void pTakeOutComp(poly *r_p, long comp, poly *r_q, int *lq) |
---|
452 | { |
---|
453 | spolyrec pp, qq; |
---|
454 | poly p, q, p_prev; |
---|
455 | int l = 0; |
---|
456 | |
---|
457 | #ifdef HAVE_ASSUME |
---|
458 | int lp = pLength(*r_p); |
---|
459 | #endif |
---|
460 | |
---|
461 | pNext(&pp) = *r_p; |
---|
462 | p = *r_p; |
---|
463 | p_prev = &pp; |
---|
464 | q = &qq; |
---|
465 | |
---|
466 | while(p != NULL) |
---|
467 | { |
---|
468 | while (pGetComp(p) == comp) |
---|
469 | { |
---|
470 | pNext(q) = p; |
---|
471 | pIter(q); |
---|
472 | pSetComp(p, 0); |
---|
473 | pSetmComp(p); |
---|
474 | pIter(p); |
---|
475 | l++; |
---|
476 | if (p == NULL) |
---|
477 | { |
---|
478 | pNext(p_prev) = NULL; |
---|
479 | goto Finish; |
---|
480 | } |
---|
481 | } |
---|
482 | pNext(p_prev) = p; |
---|
483 | p_prev = p; |
---|
484 | pIter(p); |
---|
485 | } |
---|
486 | |
---|
487 | Finish: |
---|
488 | pNext(q) = NULL; |
---|
489 | *r_p = pNext(&pp); |
---|
490 | *r_q = pNext(&qq); |
---|
491 | *lq = l; |
---|
492 | #ifdef HAVE_ASSUME |
---|
493 | assume(pLength(*r_p) + pLength(*r_q) == lp); |
---|
494 | #endif |
---|
495 | pTest(*r_p); |
---|
496 | pTest(*r_q); |
---|
497 | } |
---|
498 | |
---|
499 | #if 1 |
---|
500 | poly pTakeOutComp1(poly * p, int k) |
---|
501 | { |
---|
502 | poly q = *p; |
---|
503 | |
---|
504 | if (q==NULL) return NULL; |
---|
505 | |
---|
506 | poly qq=NULL,result = NULL; |
---|
507 | |
---|
508 | if (pGetComp(q)==k) |
---|
509 | { |
---|
510 | result = q; /* *p */ |
---|
511 | while ((q!=NULL) && (pGetComp(q)==k)) |
---|
512 | { |
---|
513 | pSetComp(q,0); |
---|
514 | pSetmComp(q); |
---|
515 | qq = q; |
---|
516 | pIter(q); |
---|
517 | } |
---|
518 | *p = q; |
---|
519 | pNext(qq) = NULL; |
---|
520 | } |
---|
521 | if (q==NULL) return result; |
---|
522 | // if (pGetComp(q) > k) pGetComp(q)--; |
---|
523 | while (pNext(q)!=NULL) |
---|
524 | { |
---|
525 | if (pGetComp(pNext(q))==k) |
---|
526 | { |
---|
527 | if (result==NULL) |
---|
528 | { |
---|
529 | result = pNext(q); |
---|
530 | qq = result; |
---|
531 | } |
---|
532 | else |
---|
533 | { |
---|
534 | pNext(qq) = pNext(q); |
---|
535 | pIter(qq); |
---|
536 | } |
---|
537 | pNext(q) = pNext(pNext(q)); |
---|
538 | pNext(qq) =NULL; |
---|
539 | pSetComp(qq,0); |
---|
540 | pSetmComp(qq); |
---|
541 | } |
---|
542 | else |
---|
543 | { |
---|
544 | pIter(q); |
---|
545 | // if (pGetComp(q) > k) pGetComp(q)--; |
---|
546 | } |
---|
547 | } |
---|
548 | return result; |
---|
549 | } |
---|
550 | #endif |
---|
551 | |
---|
552 | void pDeleteComp(poly * p,int k) |
---|
553 | { |
---|
554 | poly q; |
---|
555 | |
---|
556 | while ((*p!=NULL) && (pGetComp(*p)==k)) pLmDelete(p); |
---|
557 | if (*p==NULL) return; |
---|
558 | q = *p; |
---|
559 | if (pGetComp(q)>k) |
---|
560 | { |
---|
561 | pSubComp(q,1); |
---|
562 | pSetmComp(q); |
---|
563 | } |
---|
564 | while (pNext(q)!=NULL) |
---|
565 | { |
---|
566 | if (pGetComp(pNext(q))==k) |
---|
567 | pLmDelete(&(pNext(q))); |
---|
568 | else |
---|
569 | { |
---|
570 | pIter(q); |
---|
571 | if (pGetComp(q)>k) |
---|
572 | { |
---|
573 | pSubComp(q,1); |
---|
574 | pSetmComp(q); |
---|
575 | } |
---|
576 | } |
---|
577 | } |
---|
578 | } |
---|
579 | /*----------end of utilities for syzygies--------------*/ |
---|
580 | |
---|
581 | /*2 |
---|
582 | * pair has no common factor ? or is no polynomial |
---|
583 | */ |
---|
584 | BOOLEAN pHasNotCF(poly p1, poly p2) |
---|
585 | { |
---|
586 | |
---|
587 | if (pGetComp(p1) > 0 || pGetComp(p2) > 0) |
---|
588 | return FALSE; |
---|
589 | int i = pVariables; |
---|
590 | loop |
---|
591 | { |
---|
592 | if ((pGetExp(p1, i) > 0) && (pGetExp(p2, i) > 0)) return FALSE; |
---|
593 | i--; |
---|
594 | if (i == 0) return TRUE; |
---|
595 | } |
---|
596 | } |
---|
597 | |
---|
598 | /*2 |
---|
599 | * divides p1 by its leading coefficient if it is a unit |
---|
600 | * (this will always be true over fields; but not over coefficient rings) |
---|
601 | */ |
---|
602 | void pNorm(poly p1) |
---|
603 | { |
---|
604 | #ifdef HAVE_RINGS |
---|
605 | if (rField_is_Ring(currRing)) |
---|
606 | { |
---|
607 | if (!nIsUnit(pGetCoeff(p1))) return; |
---|
608 | } |
---|
609 | #endif |
---|
610 | if (p1!=NULL) |
---|
611 | { |
---|
612 | if (pNext(p1)==NULL) |
---|
613 | { |
---|
614 | pSetCoeff(p1,nInit(1)); |
---|
615 | return; |
---|
616 | } |
---|
617 | poly h; |
---|
618 | if (!nIsOne(pGetCoeff(p1))) |
---|
619 | { |
---|
620 | number k, c; |
---|
621 | nNormalize(pGetCoeff(p1)); |
---|
622 | k = pGetCoeff(p1); |
---|
623 | c = nInit(1); |
---|
624 | pSetCoeff0(p1,c); |
---|
625 | h = pNext(p1); |
---|
626 | while (h!=NULL) |
---|
627 | { |
---|
628 | c=nDiv(pGetCoeff(h),k); |
---|
629 | // no need to normalize: Z/p, R |
---|
630 | // normalize already in nDiv: Q_a, Z/p_a |
---|
631 | // remains: Q |
---|
632 | if (rField_is_Q() && (!nIsOne(c))) nNormalize(c); |
---|
633 | pSetCoeff(h,c); |
---|
634 | pIter(h); |
---|
635 | } |
---|
636 | nDelete(&k); |
---|
637 | } |
---|
638 | else |
---|
639 | { |
---|
640 | if (nNormalize != nDummy2) |
---|
641 | { |
---|
642 | h = pNext(p1); |
---|
643 | while (h!=NULL) |
---|
644 | { |
---|
645 | nNormalize(pGetCoeff(h)); |
---|
646 | pIter(h); |
---|
647 | } |
---|
648 | } |
---|
649 | } |
---|
650 | } |
---|
651 | } |
---|
652 | |
---|
653 | /*2 |
---|
654 | *normalize all coefficients |
---|
655 | */ |
---|
656 | void p_Normalize(poly p,const ring r) |
---|
657 | { |
---|
658 | if (rField_has_simple_inverse(r)) return; /* Z/p, GF(p,n), R, long R/C */ |
---|
659 | while (p!=NULL) |
---|
660 | { |
---|
661 | #ifdef LDEBUG |
---|
662 | if (currRing==r) {nTest(pGetCoeff(p));} |
---|
663 | #endif |
---|
664 | n_Normalize(pGetCoeff(p),r); |
---|
665 | pIter(p); |
---|
666 | } |
---|
667 | } |
---|
668 | |
---|
669 | // splits p into polys with Exp(n) == 0 and Exp(n) != 0 |
---|
670 | // Poly with Exp(n) != 0 is reversed |
---|
671 | static void pSplitAndReversePoly(poly p, int n, poly *non_zero, poly *zero) |
---|
672 | { |
---|
673 | if (p == NULL) |
---|
674 | { |
---|
675 | *non_zero = NULL; |
---|
676 | *zero = NULL; |
---|
677 | return; |
---|
678 | } |
---|
679 | spolyrec sz; |
---|
680 | poly z, n_z, next; |
---|
681 | z = &sz; |
---|
682 | n_z = NULL; |
---|
683 | |
---|
684 | while(p != NULL) |
---|
685 | { |
---|
686 | next = pNext(p); |
---|
687 | if (pGetExp(p, n) == 0) |
---|
688 | { |
---|
689 | pNext(z) = p; |
---|
690 | pIter(z); |
---|
691 | } |
---|
692 | else |
---|
693 | { |
---|
694 | pNext(p) = n_z; |
---|
695 | n_z = p; |
---|
696 | } |
---|
697 | p = next; |
---|
698 | } |
---|
699 | pNext(z) = NULL; |
---|
700 | *zero = pNext(&sz); |
---|
701 | *non_zero = n_z; |
---|
702 | return; |
---|
703 | } |
---|
704 | |
---|
705 | /*3 |
---|
706 | * substitute the n-th variable by 1 in p |
---|
707 | * destroy p |
---|
708 | */ |
---|
709 | static poly pSubst1 (poly p,int n) |
---|
710 | { |
---|
711 | poly qq=NULL, result = NULL; |
---|
712 | poly zero=NULL, non_zero=NULL; |
---|
713 | |
---|
714 | // reverse, so that add is likely to be linear |
---|
715 | pSplitAndReversePoly(p, n, &non_zero, &zero); |
---|
716 | |
---|
717 | while (non_zero != NULL) |
---|
718 | { |
---|
719 | assume(pGetExp(non_zero, n) != 0); |
---|
720 | qq = non_zero; |
---|
721 | pIter(non_zero); |
---|
722 | qq->next = NULL; |
---|
723 | pSetExp(qq,n,0); |
---|
724 | pSetm(qq); |
---|
725 | result = pAdd(result,qq); |
---|
726 | } |
---|
727 | p = pAdd(result, zero); |
---|
728 | pTest(p); |
---|
729 | return p; |
---|
730 | } |
---|
731 | |
---|
732 | /*3 |
---|
733 | * substitute the n-th variable by number e in p |
---|
734 | * destroy p |
---|
735 | */ |
---|
736 | static poly pSubst2 (poly p,int n, number e) |
---|
737 | { |
---|
738 | assume( ! nIsZero(e) ); |
---|
739 | poly qq,result = NULL; |
---|
740 | number nn, nm; |
---|
741 | poly zero, non_zero; |
---|
742 | |
---|
743 | // reverse, so that add is likely to be linear |
---|
744 | pSplitAndReversePoly(p, n, &non_zero, &zero); |
---|
745 | |
---|
746 | while (non_zero != NULL) |
---|
747 | { |
---|
748 | assume(pGetExp(non_zero, n) != 0); |
---|
749 | qq = non_zero; |
---|
750 | pIter(non_zero); |
---|
751 | qq->next = NULL; |
---|
752 | nPower(e, pGetExp(qq, n), &nn); |
---|
753 | nm = nMult(nn, pGetCoeff(qq)); |
---|
754 | #ifdef HAVE_RINGS |
---|
755 | if (nIsZero(nm)) |
---|
756 | { |
---|
757 | pLmFree(&qq); |
---|
758 | nDelete(&nm); |
---|
759 | } |
---|
760 | else |
---|
761 | #endif |
---|
762 | { |
---|
763 | pSetCoeff(qq, nm); |
---|
764 | pSetExp(qq, n, 0); |
---|
765 | pSetm(qq); |
---|
766 | result = pAdd(result,qq); |
---|
767 | } |
---|
768 | nDelete(&nn); |
---|
769 | } |
---|
770 | p = pAdd(result, zero); |
---|
771 | pTest(p); |
---|
772 | return p; |
---|
773 | } |
---|
774 | |
---|
775 | |
---|
776 | /* delete monoms whose n-th exponent is different from zero */ |
---|
777 | poly pSubst0(poly p, int n) |
---|
778 | { |
---|
779 | spolyrec res; |
---|
780 | poly h = &res; |
---|
781 | pNext(h) = p; |
---|
782 | |
---|
783 | while (pNext(h)!=NULL) |
---|
784 | { |
---|
785 | if (pGetExp(pNext(h),n)!=0) |
---|
786 | { |
---|
787 | pLmDelete(&pNext(h)); |
---|
788 | } |
---|
789 | else |
---|
790 | { |
---|
791 | pIter(h); |
---|
792 | } |
---|
793 | } |
---|
794 | pTest(pNext(&res)); |
---|
795 | return pNext(&res); |
---|
796 | } |
---|
797 | |
---|
798 | /*2 |
---|
799 | * substitute the n-th variable by e in p |
---|
800 | * destroy p |
---|
801 | */ |
---|
802 | poly pSubst(poly p, int n, poly e) |
---|
803 | { |
---|
804 | if (e == NULL) return pSubst0(p, n); |
---|
805 | |
---|
806 | if (pIsConstant(e)) |
---|
807 | { |
---|
808 | if (nIsOne(pGetCoeff(e))) return pSubst1(p,n); |
---|
809 | else return pSubst2(p, n, pGetCoeff(e)); |
---|
810 | } |
---|
811 | |
---|
812 | #ifdef HAVE_PLURAL |
---|
813 | if (rIsPluralRing(currRing)) |
---|
814 | { |
---|
815 | return nc_pSubst(p,n,e); |
---|
816 | } |
---|
817 | #endif |
---|
818 | |
---|
819 | int exponent,i; |
---|
820 | poly h, res, m; |
---|
821 | int *me,*ee; |
---|
822 | number nu,nu1; |
---|
823 | |
---|
824 | me=(int *)omAlloc((pVariables+1)*sizeof(int)); |
---|
825 | ee=(int *)omAlloc((pVariables+1)*sizeof(int)); |
---|
826 | if (e!=NULL) pGetExpV(e,ee); |
---|
827 | res=NULL; |
---|
828 | h=p; |
---|
829 | while (h!=NULL) |
---|
830 | { |
---|
831 | if ((e!=NULL) || (pGetExp(h,n)==0)) |
---|
832 | { |
---|
833 | m=pHead(h); |
---|
834 | pGetExpV(m,me); |
---|
835 | exponent=me[n]; |
---|
836 | me[n]=0; |
---|
837 | for(i=pVariables;i>0;i--) |
---|
838 | me[i]+=exponent*ee[i]; |
---|
839 | pSetExpV(m,me); |
---|
840 | if (e!=NULL) |
---|
841 | { |
---|
842 | nPower(pGetCoeff(e),exponent,&nu); |
---|
843 | nu1=nMult(pGetCoeff(m),nu); |
---|
844 | nDelete(&nu); |
---|
845 | pSetCoeff(m,nu1); |
---|
846 | } |
---|
847 | res=pAdd(res,m); |
---|
848 | } |
---|
849 | pLmDelete(&h); |
---|
850 | } |
---|
851 | omFreeSize((ADDRESS)me,(pVariables+1)*sizeof(int)); |
---|
852 | omFreeSize((ADDRESS)ee,(pVariables+1)*sizeof(int)); |
---|
853 | return res; |
---|
854 | } |
---|
855 | |
---|
856 | /* Returns TRUE if |
---|
857 | * LM(p) | LM(lcm) |
---|
858 | * LC(p) | LC(lcm) only if ring |
---|
859 | * Exists i, j: |
---|
860 | * LE(p, i) != LE(lcm, i) |
---|
861 | * LE(p1, i) != LE(lcm, i) ==> LCM(p1, p) != lcm |
---|
862 | * LE(p, j) != LE(lcm, j) |
---|
863 | * LE(p2, j) != LE(lcm, j) ==> LCM(p2, p) != lcm |
---|
864 | */ |
---|
865 | BOOLEAN pCompareChain (poly p,poly p1,poly p2,poly lcm) |
---|
866 | { |
---|
867 | int k, j; |
---|
868 | |
---|
869 | if (lcm==NULL) return FALSE; |
---|
870 | |
---|
871 | for (j=pVariables; j; j--) |
---|
872 | if ( pGetExp(p,j) > pGetExp(lcm,j)) return FALSE; |
---|
873 | if ( pGetComp(p) != pGetComp(lcm)) return FALSE; |
---|
874 | for (j=pVariables; j; j--) |
---|
875 | { |
---|
876 | if (pGetExp(p1,j)!=pGetExp(lcm,j)) |
---|
877 | { |
---|
878 | if (pGetExp(p,j)!=pGetExp(lcm,j)) |
---|
879 | { |
---|
880 | for (k=pVariables; k>j; k--) |
---|
881 | { |
---|
882 | if ((pGetExp(p,k)!=pGetExp(lcm,k)) |
---|
883 | && (pGetExp(p2,k)!=pGetExp(lcm,k))) |
---|
884 | return TRUE; |
---|
885 | } |
---|
886 | for (k=j-1; k; k--) |
---|
887 | { |
---|
888 | if ((pGetExp(p,k)!=pGetExp(lcm,k)) |
---|
889 | && (pGetExp(p2,k)!=pGetExp(lcm,k))) |
---|
890 | return TRUE; |
---|
891 | } |
---|
892 | return FALSE; |
---|
893 | } |
---|
894 | } |
---|
895 | else if (pGetExp(p2,j)!=pGetExp(lcm,j)) |
---|
896 | { |
---|
897 | if (pGetExp(p,j)!=pGetExp(lcm,j)) |
---|
898 | { |
---|
899 | for (k=pVariables; k>j; k--) |
---|
900 | { |
---|
901 | if ((pGetExp(p,k)!=pGetExp(lcm,k)) |
---|
902 | && (pGetExp(p1,k)!=pGetExp(lcm,k))) |
---|
903 | return TRUE; |
---|
904 | } |
---|
905 | for (k=j-1; k!=0 ; k--) |
---|
906 | { |
---|
907 | if ((pGetExp(p,k)!=pGetExp(lcm,k)) |
---|
908 | && (pGetExp(p1,k)!=pGetExp(lcm,k))) |
---|
909 | return TRUE; |
---|
910 | } |
---|
911 | return FALSE; |
---|
912 | } |
---|
913 | } |
---|
914 | } |
---|
915 | return FALSE; |
---|
916 | } |
---|
917 | #ifdef HAVE_RATGRING |
---|
918 | BOOLEAN pCompareChainPart (poly p,poly p1,poly p2,poly lcm) |
---|
919 | { |
---|
920 | int k, j; |
---|
921 | |
---|
922 | if (lcm==NULL) return FALSE; |
---|
923 | |
---|
924 | for (j=currRing->real_var_end; j>=currRing->real_var_start; j--) |
---|
925 | if ( pGetExp(p,j) > pGetExp(lcm,j)) return FALSE; |
---|
926 | if ( pGetComp(p) != pGetComp(lcm)) return FALSE; |
---|
927 | for (j=currRing->real_var_end; j>=currRing->real_var_start; j--) |
---|
928 | { |
---|
929 | if (pGetExp(p1,j)!=pGetExp(lcm,j)) |
---|
930 | { |
---|
931 | if (pGetExp(p,j)!=pGetExp(lcm,j)) |
---|
932 | { |
---|
933 | for (k=pVariables; k>j; k--) |
---|
934 | for (k=currRing->real_var_end; k>j; k--) |
---|
935 | { |
---|
936 | if ((pGetExp(p,k)!=pGetExp(lcm,k)) |
---|
937 | && (pGetExp(p2,k)!=pGetExp(lcm,k))) |
---|
938 | return TRUE; |
---|
939 | } |
---|
940 | for (k=j-1; k>=currRing->real_var_start; k--) |
---|
941 | { |
---|
942 | if ((pGetExp(p,k)!=pGetExp(lcm,k)) |
---|
943 | && (pGetExp(p2,k)!=pGetExp(lcm,k))) |
---|
944 | return TRUE; |
---|
945 | } |
---|
946 | return FALSE; |
---|
947 | } |
---|
948 | } |
---|
949 | else if (pGetExp(p2,j)!=pGetExp(lcm,j)) |
---|
950 | { |
---|
951 | if (pGetExp(p,j)!=pGetExp(lcm,j)) |
---|
952 | { |
---|
953 | for (k=currRing->real_var_end; k>j; k--) |
---|
954 | { |
---|
955 | if ((pGetExp(p,k)!=pGetExp(lcm,k)) |
---|
956 | && (pGetExp(p1,k)!=pGetExp(lcm,k))) |
---|
957 | return TRUE; |
---|
958 | } |
---|
959 | for (k=j-1; k>=currRing->real_var_start; k--) |
---|
960 | { |
---|
961 | if ((pGetExp(p,k)!=pGetExp(lcm,k)) |
---|
962 | && (pGetExp(p1,k)!=pGetExp(lcm,k))) |
---|
963 | return TRUE; |
---|
964 | } |
---|
965 | return FALSE; |
---|
966 | } |
---|
967 | } |
---|
968 | } |
---|
969 | return FALSE; |
---|
970 | } |
---|
971 | #endif |
---|
972 | |
---|
973 | int pSize(poly p) |
---|
974 | { |
---|
975 | int count = 0; |
---|
976 | while ( p != NULL ) |
---|
977 | { |
---|
978 | count+= nSize( pGetCoeff( p ) ); |
---|
979 | pIter( p ); |
---|
980 | } |
---|
981 | return count; |
---|
982 | } |
---|
983 | |
---|
984 | /*2 |
---|
985 | * returns the length of a (numbers of monomials) |
---|
986 | * respect syzComp |
---|
987 | */ |
---|
988 | poly pLast(poly a, int &l) |
---|
989 | { |
---|
990 | if (a == NULL) |
---|
991 | { |
---|
992 | l = 0; |
---|
993 | return NULL; |
---|
994 | } |
---|
995 | l = 1; |
---|
996 | if (! rIsSyzIndexRing(currRing)) |
---|
997 | { |
---|
998 | while (pNext(a)!=NULL) |
---|
999 | { |
---|
1000 | pIter(a); |
---|
1001 | l++; |
---|
1002 | } |
---|
1003 | } |
---|
1004 | else |
---|
1005 | { |
---|
1006 | int curr_limit = rGetCurrSyzLimit(currRing); |
---|
1007 | poly pp = a; |
---|
1008 | while ((a=pNext(a))!=NULL) |
---|
1009 | { |
---|
1010 | if (pGetComp(a)<=curr_limit/*syzComp*/) |
---|
1011 | l++; |
---|
1012 | else break; |
---|
1013 | pp = a; |
---|
1014 | } |
---|
1015 | a=pp; |
---|
1016 | } |
---|
1017 | return a; |
---|
1018 | } |
---|
1019 | |
---|