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