1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id$ */ |
---|
5 | |
---|
6 | /* |
---|
7 | * ABSTRACT - all basic methods to manipulate polynomials: |
---|
8 | * independent of representation |
---|
9 | */ |
---|
10 | |
---|
11 | /* includes */ |
---|
12 | #include <string.h> |
---|
13 | #include <kernel/mod2.h> |
---|
14 | #include <kernel/options.h> |
---|
15 | #include <kernel/numbers.h> |
---|
16 | #include <kernel/ffields.h> |
---|
17 | #include <omalloc/omalloc.h> |
---|
18 | #include <kernel/febase.h> |
---|
19 | #include <kernel/weight.h> |
---|
20 | #include <kernel/intvec.h> |
---|
21 | #include <kernel/longalg.h> |
---|
22 | #include <kernel/longtrans.h> |
---|
23 | #include <kernel/ring.h> |
---|
24 | #include <kernel/ideals.h> |
---|
25 | #include <kernel/polys.h> |
---|
26 | //#include "ipid.h" |
---|
27 | #ifdef HAVE_FACTORY |
---|
28 | #include <kernel/clapsing.h> |
---|
29 | #endif |
---|
30 | |
---|
31 | #ifdef HAVE_RATGRING |
---|
32 | #include <kernel/ratgring.h> |
---|
33 | #endif |
---|
34 | |
---|
35 | |
---|
36 | /*3 |
---|
37 | * create binomial coef. |
---|
38 | */ |
---|
39 | static number* pnBin(int exp) |
---|
40 | { |
---|
41 | int e, i, h; |
---|
42 | number x, y, *bin=NULL; |
---|
43 | |
---|
44 | x = nInit(exp); |
---|
45 | if (nIsZero(x)) |
---|
46 | { |
---|
47 | nDelete(&x); |
---|
48 | return bin; |
---|
49 | } |
---|
50 | h = (exp >> 1) + 1; |
---|
51 | bin = (number *)omAlloc0(h*sizeof(number)); |
---|
52 | bin[1] = x; |
---|
53 | if (exp < 4) |
---|
54 | return bin; |
---|
55 | i = exp - 1; |
---|
56 | for (e=2; e<h; e++) |
---|
57 | { |
---|
58 | x = nInit(i); |
---|
59 | i--; |
---|
60 | y = nMult(x,bin[e-1]); |
---|
61 | nDelete(&x); |
---|
62 | x = nInit(e); |
---|
63 | bin[e] = nIntDiv(y,x); |
---|
64 | nDelete(&x); |
---|
65 | nDelete(&y); |
---|
66 | } |
---|
67 | return bin; |
---|
68 | } |
---|
69 | |
---|
70 | static void pnFreeBin(number *bin, int exp) |
---|
71 | { |
---|
72 | int e, h = (exp >> 1) + 1; |
---|
73 | |
---|
74 | if (bin[1] != NULL) |
---|
75 | { |
---|
76 | for (e=1; e<h; e++) |
---|
77 | nDelete(&(bin[e])); |
---|
78 | } |
---|
79 | omFreeSize((ADDRESS)bin, h*sizeof(number)); |
---|
80 | } |
---|
81 | |
---|
82 | /*2 |
---|
83 | * handle memory request for sets of polynomials (ideals) |
---|
84 | * l is the length of *p, increment is the difference (may be negative) |
---|
85 | */ |
---|
86 | void pEnlargeSet(polyset *p, int l, int increment) |
---|
87 | { |
---|
88 | polyset h; |
---|
89 | |
---|
90 | h=(polyset)omReallocSize((poly*)*p,l*sizeof(poly),(l+increment)*sizeof(poly)); |
---|
91 | if (increment>0) |
---|
92 | { |
---|
93 | //for (i=l; i<l+increment; i++) |
---|
94 | // h[i]=NULL; |
---|
95 | memset(&(h[l]),0,increment*sizeof(poly)); |
---|
96 | } |
---|
97 | *p=h; |
---|
98 | } |
---|
99 | |
---|
100 | number pInitContent(poly ph); |
---|
101 | number pInitContent_a(poly ph); |
---|
102 | |
---|
103 | void p_Content(poly ph, const ring r) |
---|
104 | { |
---|
105 | #ifdef HAVE_RINGS |
---|
106 | if (rField_is_Ring(r)) |
---|
107 | { |
---|
108 | if (ph!=NULL) |
---|
109 | { |
---|
110 | number k = nGetUnit(pGetCoeff(ph)); |
---|
111 | if (!nGreaterZero(pGetCoeff(ph))) k = nNeg(k); // in-place negation |
---|
112 | if (!nIsOne(k)) |
---|
113 | { |
---|
114 | number tmpNumber = k; |
---|
115 | k = nInvers(k); |
---|
116 | nDelete(&tmpNumber); |
---|
117 | poly h = pNext(ph); |
---|
118 | p_Mult_nn(ph,k,currRing); |
---|
119 | pNormalize(ph); |
---|
120 | } |
---|
121 | nDelete(&k); |
---|
122 | } |
---|
123 | return; |
---|
124 | } |
---|
125 | #endif |
---|
126 | number h,d; |
---|
127 | poly p; |
---|
128 | |
---|
129 | // if(TEST_OPT_CONTENTSB) return; |
---|
130 | if(pNext(ph)==NULL) |
---|
131 | { |
---|
132 | pSetCoeff(ph,nInit(1)); |
---|
133 | } |
---|
134 | else |
---|
135 | { |
---|
136 | nNormalize(pGetCoeff(ph)); |
---|
137 | if(!nGreaterZero(pGetCoeff(ph))) ph = pNeg(ph); |
---|
138 | if (rField_is_Q()) |
---|
139 | { |
---|
140 | h=pInitContent(ph); |
---|
141 | p=ph; |
---|
142 | } |
---|
143 | else if ((rField_is_Extension(r)) |
---|
144 | && ((rPar(r)>1)||(r->minpoly==NULL))) |
---|
145 | { |
---|
146 | h=pInitContent_a(ph); |
---|
147 | p=ph; |
---|
148 | } |
---|
149 | else |
---|
150 | { |
---|
151 | h=nCopy(pGetCoeff(ph)); |
---|
152 | p = pNext(ph); |
---|
153 | } |
---|
154 | while (p!=NULL) |
---|
155 | { |
---|
156 | nNormalize(pGetCoeff(p)); |
---|
157 | d=nGcd(h,pGetCoeff(p),r); |
---|
158 | nDelete(&h); |
---|
159 | h = d; |
---|
160 | if(nIsOne(h)) |
---|
161 | { |
---|
162 | break; |
---|
163 | } |
---|
164 | pIter(p); |
---|
165 | } |
---|
166 | p = ph; |
---|
167 | //number tmp; |
---|
168 | if(!nIsOne(h)) |
---|
169 | { |
---|
170 | while (p!=NULL) |
---|
171 | { |
---|
172 | //d = nDiv(pGetCoeff(p),h); |
---|
173 | //tmp = nIntDiv(pGetCoeff(p),h); |
---|
174 | //if (!nEqual(d,tmp)) |
---|
175 | //{ |
---|
176 | // StringSetS("** div0:");nWrite(pGetCoeff(p));StringAppendS("/"); |
---|
177 | // nWrite(h);StringAppendS("=");nWrite(d);StringAppendS(" int:"); |
---|
178 | // nWrite(tmp);Print(StringAppendS("\n")); |
---|
179 | //} |
---|
180 | //nDelete(&tmp); |
---|
181 | if (rField_is_Zp_a(currRing) || rField_is_Q_a(currRing)) |
---|
182 | d = nDiv(pGetCoeff(p),h); |
---|
183 | else |
---|
184 | d = nIntDiv(pGetCoeff(p),h); |
---|
185 | pSetCoeff(p,d); |
---|
186 | pIter(p); |
---|
187 | } |
---|
188 | } |
---|
189 | nDelete(&h); |
---|
190 | #ifdef HAVE_FACTORY |
---|
191 | if ( (nGetChar() == 1) || (nGetChar() < 0) ) /* Q[a],Q(a),Zp[a],Z/p(a) */ |
---|
192 | { |
---|
193 | singclap_divide_content(ph); |
---|
194 | if(!nGreaterZero(pGetCoeff(ph))) ph = pNeg(ph); |
---|
195 | } |
---|
196 | #endif |
---|
197 | if (rField_is_Q_a(r)) |
---|
198 | { |
---|
199 | number hzz = nlInit(1, r); |
---|
200 | h = nlInit(1, r); |
---|
201 | p=ph; |
---|
202 | while (p!=NULL) |
---|
203 | { // each monom: coeff in Q_a |
---|
204 | lnumber c_n_n=(lnumber)pGetCoeff(p); |
---|
205 | napoly c_n=c_n_n->z; |
---|
206 | while (c_n!=NULL) |
---|
207 | { // each monom: coeff in Q |
---|
208 | d=nlLcm(hzz,pGetCoeff(c_n),r->algring); |
---|
209 | n_Delete(&hzz,r->algring); |
---|
210 | hzz=d; |
---|
211 | pIter(c_n); |
---|
212 | } |
---|
213 | c_n=c_n_n->n; |
---|
214 | while (c_n!=NULL) |
---|
215 | { // each monom: coeff in Q |
---|
216 | d=nlLcm(h,pGetCoeff(c_n),r->algring); |
---|
217 | n_Delete(&h,r->algring); |
---|
218 | h=d; |
---|
219 | pIter(c_n); |
---|
220 | } |
---|
221 | pIter(p); |
---|
222 | } |
---|
223 | /* hzz contains the 1/lcm of all denominators in c_n_n->z*/ |
---|
224 | /* h contains the 1/lcm of all denominators in c_n_n->n*/ |
---|
225 | number htmp=nlInvers(h); |
---|
226 | number hzztmp=nlInvers(hzz); |
---|
227 | number hh=nlMult(hzz,h); |
---|
228 | nlDelete(&hzz,r->algring); |
---|
229 | nlDelete(&h,r->algring); |
---|
230 | number hg=nlGcd(hzztmp,htmp,r->algring); |
---|
231 | nlDelete(&hzztmp,r->algring); |
---|
232 | nlDelete(&htmp,r->algring); |
---|
233 | h=nlMult(hh,hg); |
---|
234 | nlDelete(&hg,r->algring); |
---|
235 | nlDelete(&hh,r->algring); |
---|
236 | nlNormalize(h); |
---|
237 | if(!nlIsOne(h)) |
---|
238 | { |
---|
239 | p=ph; |
---|
240 | while (p!=NULL) |
---|
241 | { // each monom: coeff in Q_a |
---|
242 | lnumber c_n_n=(lnumber)pGetCoeff(p); |
---|
243 | napoly c_n=c_n_n->z; |
---|
244 | while (c_n!=NULL) |
---|
245 | { // each monom: coeff in Q |
---|
246 | d=nlMult(h,pGetCoeff(c_n)); |
---|
247 | nlNormalize(d); |
---|
248 | nlDelete(&pGetCoeff(c_n),r->algring); |
---|
249 | pGetCoeff(c_n)=d; |
---|
250 | pIter(c_n); |
---|
251 | } |
---|
252 | c_n=c_n_n->n; |
---|
253 | while (c_n!=NULL) |
---|
254 | { // each monom: coeff in Q |
---|
255 | d=nlMult(h,pGetCoeff(c_n)); |
---|
256 | nlNormalize(d); |
---|
257 | nlDelete(&pGetCoeff(c_n),r->algring); |
---|
258 | pGetCoeff(c_n)=d; |
---|
259 | pIter(c_n); |
---|
260 | } |
---|
261 | pIter(p); |
---|
262 | } |
---|
263 | } |
---|
264 | nlDelete(&h,r->algring); |
---|
265 | } |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | void pSimpleContent(poly ph,int smax) |
---|
270 | { |
---|
271 | //if(TEST_OPT_CONTENTSB) return; |
---|
272 | if (ph==NULL) return; |
---|
273 | if (pNext(ph)==NULL) |
---|
274 | { |
---|
275 | pSetCoeff(ph,nInit(1)); |
---|
276 | return; |
---|
277 | } |
---|
278 | if ((pNext(pNext(ph))==NULL)||(!rField_is_Q())) |
---|
279 | { |
---|
280 | return; |
---|
281 | } |
---|
282 | number d=pInitContent(ph); |
---|
283 | if (nlSize(d)<=smax) |
---|
284 | { |
---|
285 | //if (TEST_OPT_PROT) PrintS("G"); |
---|
286 | return; |
---|
287 | } |
---|
288 | poly p=ph; |
---|
289 | number h=d; |
---|
290 | if (smax==1) smax=2; |
---|
291 | while (p!=NULL) |
---|
292 | { |
---|
293 | #if 0 |
---|
294 | d=nlGcd(h,pGetCoeff(p),currRing); |
---|
295 | nlDelete(&h,currRing); |
---|
296 | h = d; |
---|
297 | #else |
---|
298 | nlInpGcd(h,pGetCoeff(p),currRing); |
---|
299 | #endif |
---|
300 | if(nlSize(h)<smax) |
---|
301 | { |
---|
302 | //if (TEST_OPT_PROT) PrintS("g"); |
---|
303 | return; |
---|
304 | } |
---|
305 | pIter(p); |
---|
306 | } |
---|
307 | p = ph; |
---|
308 | if (!nlGreaterZero(pGetCoeff(p))) h=nlNeg(h); |
---|
309 | if(nlIsOne(h)) return; |
---|
310 | //if (TEST_OPT_PROT) PrintS("c"); |
---|
311 | // |
---|
312 | number inv=nlInvers(h); |
---|
313 | p_Mult_nn(p,inv,currRing); |
---|
314 | pNormalize(p); |
---|
315 | //while (p!=NULL) |
---|
316 | //{ |
---|
317 | #if 1 |
---|
318 | // d = nlIntDiv(pGetCoeff(p),h); |
---|
319 | // pSetCoeff(p,d); |
---|
320 | #else |
---|
321 | // nlInpIntDiv(pGetCoeff(p),h,currRing); |
---|
322 | #endif |
---|
323 | // pIter(p); |
---|
324 | //} |
---|
325 | nlDelete(&inv,currRing); |
---|
326 | nlDelete(&h,currRing); |
---|
327 | } |
---|
328 | |
---|
329 | number pInitContent(poly ph) |
---|
330 | // only for coefficients in Q |
---|
331 | #if 0 |
---|
332 | { |
---|
333 | //assume(!TEST_OPT_CONTENTSB); |
---|
334 | assume(ph!=NULL); |
---|
335 | assume(pNext(ph)!=NULL); |
---|
336 | assume(rField_is_Q()); |
---|
337 | if (pNext(pNext(ph))==NULL) |
---|
338 | { |
---|
339 | return nlGetNom(pGetCoeff(pNext(ph)),currRing); |
---|
340 | } |
---|
341 | poly p=ph; |
---|
342 | number n1=nlGetNom(pGetCoeff(p),currRing); |
---|
343 | pIter(p); |
---|
344 | number n2=nlGetNom(pGetCoeff(p),currRing); |
---|
345 | pIter(p); |
---|
346 | number d; |
---|
347 | number t; |
---|
348 | loop |
---|
349 | { |
---|
350 | nlNormalize(pGetCoeff(p)); |
---|
351 | t=nlGetNom(pGetCoeff(p),currRing); |
---|
352 | if (nlGreaterZero(t)) |
---|
353 | d=nlAdd(n1,t); |
---|
354 | else |
---|
355 | d=nlSub(n1,t); |
---|
356 | nlDelete(&t,currRing); |
---|
357 | nlDelete(&n1,currRing); |
---|
358 | n1=d; |
---|
359 | pIter(p); |
---|
360 | if (p==NULL) break; |
---|
361 | nlNormalize(pGetCoeff(p)); |
---|
362 | t=nlGetNom(pGetCoeff(p),currRing); |
---|
363 | if (nlGreaterZero(t)) |
---|
364 | d=nlAdd(n2,t); |
---|
365 | else |
---|
366 | d=nlSub(n2,t); |
---|
367 | nlDelete(&t,currRing); |
---|
368 | nlDelete(&n2,currRing); |
---|
369 | n2=d; |
---|
370 | pIter(p); |
---|
371 | if (p==NULL) break; |
---|
372 | } |
---|
373 | d=nlGcd(n1,n2,currRing); |
---|
374 | nlDelete(&n1,currRing); |
---|
375 | nlDelete(&n2,currRing); |
---|
376 | return d; |
---|
377 | } |
---|
378 | #else |
---|
379 | { |
---|
380 | number d=pGetCoeff(ph); |
---|
381 | if(SR_HDL(d)&SR_INT) return d; |
---|
382 | int s=mpz_size1(d->z); |
---|
383 | int s2=-1; |
---|
384 | number d2; |
---|
385 | loop |
---|
386 | { |
---|
387 | pIter(ph); |
---|
388 | if(ph==NULL) |
---|
389 | { |
---|
390 | if (s2==-1) return nlCopy(d); |
---|
391 | break; |
---|
392 | } |
---|
393 | if (SR_HDL(pGetCoeff(ph))&SR_INT) |
---|
394 | { |
---|
395 | s2=s; |
---|
396 | d2=d; |
---|
397 | s=0; |
---|
398 | d=pGetCoeff(ph); |
---|
399 | if (s2==0) break; |
---|
400 | } |
---|
401 | else |
---|
402 | if (mpz_size1((pGetCoeff(ph)->z))<=s) |
---|
403 | { |
---|
404 | s2=s; |
---|
405 | d2=d; |
---|
406 | d=pGetCoeff(ph); |
---|
407 | s=mpz_size1(d->z); |
---|
408 | } |
---|
409 | } |
---|
410 | return nlGcd(d,d2,currRing); |
---|
411 | } |
---|
412 | #endif |
---|
413 | |
---|
414 | number pInitContent_a(poly ph) |
---|
415 | // only for coefficients in K(a) anf K(a,...) |
---|
416 | { |
---|
417 | number d=pGetCoeff(ph); |
---|
418 | int s=naParDeg(d); |
---|
419 | if (s /* naParDeg(d)*/ <=1) return naCopy(d); |
---|
420 | int s2=-1; |
---|
421 | number d2; |
---|
422 | int ss; |
---|
423 | loop |
---|
424 | { |
---|
425 | pIter(ph); |
---|
426 | if(ph==NULL) |
---|
427 | { |
---|
428 | if (s2==-1) return naCopy(d); |
---|
429 | break; |
---|
430 | } |
---|
431 | if ((ss=naParDeg(pGetCoeff(ph)))<s) |
---|
432 | { |
---|
433 | s2=s; |
---|
434 | d2=d; |
---|
435 | s=ss; |
---|
436 | d=pGetCoeff(ph); |
---|
437 | if (s2<=1) break; |
---|
438 | } |
---|
439 | } |
---|
440 | return naGcd(d,d2,currRing); |
---|
441 | } |
---|
442 | |
---|
443 | |
---|
444 | //void pContent(poly ph) |
---|
445 | //{ |
---|
446 | // number h,d; |
---|
447 | // poly p; |
---|
448 | // |
---|
449 | // p = ph; |
---|
450 | // if(pNext(p)==NULL) |
---|
451 | // { |
---|
452 | // pSetCoeff(p,nInit(1)); |
---|
453 | // } |
---|
454 | // else |
---|
455 | // { |
---|
456 | //#ifdef PDEBUG |
---|
457 | // if (!pTest(p)) return; |
---|
458 | //#endif |
---|
459 | // nNormalize(pGetCoeff(p)); |
---|
460 | // if(!nGreaterZero(pGetCoeff(ph))) |
---|
461 | // { |
---|
462 | // ph = pNeg(ph); |
---|
463 | // nNormalize(pGetCoeff(p)); |
---|
464 | // } |
---|
465 | // h=pGetCoeff(p); |
---|
466 | // pIter(p); |
---|
467 | // while (p!=NULL) |
---|
468 | // { |
---|
469 | // nNormalize(pGetCoeff(p)); |
---|
470 | // if (nGreater(h,pGetCoeff(p))) h=pGetCoeff(p); |
---|
471 | // pIter(p); |
---|
472 | // } |
---|
473 | // h=nCopy(h); |
---|
474 | // p=ph; |
---|
475 | // while (p!=NULL) |
---|
476 | // { |
---|
477 | // d=nGcd(h,pGetCoeff(p)); |
---|
478 | // nDelete(&h); |
---|
479 | // h = d; |
---|
480 | // if(nIsOne(h)) |
---|
481 | // { |
---|
482 | // break; |
---|
483 | // } |
---|
484 | // pIter(p); |
---|
485 | // } |
---|
486 | // p = ph; |
---|
487 | // //number tmp; |
---|
488 | // if(!nIsOne(h)) |
---|
489 | // { |
---|
490 | // while (p!=NULL) |
---|
491 | // { |
---|
492 | // d = nIntDiv(pGetCoeff(p),h); |
---|
493 | // pSetCoeff(p,d); |
---|
494 | // pIter(p); |
---|
495 | // } |
---|
496 | // } |
---|
497 | // nDelete(&h); |
---|
498 | //#ifdef HAVE_FACTORY |
---|
499 | // if ( (nGetChar() == 1) || (nGetChar() < 0) ) /* Q[a],Q(a),Zp[a],Z/p(a) */ |
---|
500 | // { |
---|
501 | // pTest(ph); |
---|
502 | // singclap_divide_content(ph); |
---|
503 | // pTest(ph); |
---|
504 | // } |
---|
505 | //#endif |
---|
506 | // } |
---|
507 | //} |
---|
508 | #if 0 |
---|
509 | void p_Content(poly ph, ring r) |
---|
510 | { |
---|
511 | number h,d; |
---|
512 | poly p; |
---|
513 | |
---|
514 | if(pNext(ph)==NULL) |
---|
515 | { |
---|
516 | pSetCoeff(ph,n_Init(1,r)); |
---|
517 | } |
---|
518 | else |
---|
519 | { |
---|
520 | n_Normalize(pGetCoeff(ph),r); |
---|
521 | if(!n_GreaterZero(pGetCoeff(ph),r)) ph = p_Neg(ph,r); |
---|
522 | h=n_Copy(pGetCoeff(ph),r); |
---|
523 | p = pNext(ph); |
---|
524 | while (p!=NULL) |
---|
525 | { |
---|
526 | n_Normalize(pGetCoeff(p),r); |
---|
527 | d=n_Gcd(h,pGetCoeff(p),r); |
---|
528 | n_Delete(&h,r); |
---|
529 | h = d; |
---|
530 | if(n_IsOne(h,r)) |
---|
531 | { |
---|
532 | break; |
---|
533 | } |
---|
534 | pIter(p); |
---|
535 | } |
---|
536 | p = ph; |
---|
537 | //number tmp; |
---|
538 | if(!n_IsOne(h,r)) |
---|
539 | { |
---|
540 | while (p!=NULL) |
---|
541 | { |
---|
542 | //d = nDiv(pGetCoeff(p),h); |
---|
543 | //tmp = nIntDiv(pGetCoeff(p),h); |
---|
544 | //if (!nEqual(d,tmp)) |
---|
545 | //{ |
---|
546 | // StringSetS("** div0:");nWrite(pGetCoeff(p));StringAppendS("/"); |
---|
547 | // nWrite(h);StringAppendS("=");nWrite(d);StringAppendS(" int:"); |
---|
548 | // nWrite(tmp);Print(StringAppendS("\n")); |
---|
549 | //} |
---|
550 | //nDelete(&tmp); |
---|
551 | d = n_IntDiv(pGetCoeff(p),h,r); |
---|
552 | p_SetCoeff(p,d,r); |
---|
553 | pIter(p); |
---|
554 | } |
---|
555 | } |
---|
556 | n_Delete(&h,r); |
---|
557 | #ifdef HAVE_FACTORY |
---|
558 | //if ( (n_GetChar(r) == 1) || (n_GetChar(r) < 0) ) /* Q[a],Q(a),Zp[a],Z/p(a) */ |
---|
559 | //{ |
---|
560 | // singclap_divide_content(ph); |
---|
561 | // if(!n_GreaterZero(pGetCoeff(ph),r)) ph = p_Neg(ph,r); |
---|
562 | //} |
---|
563 | #endif |
---|
564 | } |
---|
565 | } |
---|
566 | #endif |
---|
567 | |
---|
568 | poly p_Cleardenom(poly ph, const ring r) |
---|
569 | { |
---|
570 | poly start=ph; |
---|
571 | number d, h; |
---|
572 | poly p; |
---|
573 | |
---|
574 | #ifdef HAVE_RINGS |
---|
575 | if (rField_is_Ring(r)) |
---|
576 | { |
---|
577 | p_Content(ph,r); |
---|
578 | return start; |
---|
579 | } |
---|
580 | #endif |
---|
581 | if (rField_is_Zp(r) && TEST_OPT_INTSTRATEGY) return start; |
---|
582 | p = ph; |
---|
583 | if(pNext(p)==NULL) |
---|
584 | { |
---|
585 | /* |
---|
586 | if (TEST_OPT_CONTENTSB) |
---|
587 | { |
---|
588 | number n=nGetDenom(pGetCoeff(p)); |
---|
589 | if (!nIsOne(n)) |
---|
590 | { |
---|
591 | number nn=nMult(pGetCoeff(p),n); |
---|
592 | nNormalize(nn); |
---|
593 | pSetCoeff(p,nn); |
---|
594 | } |
---|
595 | nDelete(&n); |
---|
596 | } |
---|
597 | else |
---|
598 | */ |
---|
599 | pSetCoeff(p,nInit(1)); |
---|
600 | } |
---|
601 | else |
---|
602 | { |
---|
603 | h = nInit(1); |
---|
604 | while (p!=NULL) |
---|
605 | { |
---|
606 | nNormalize(pGetCoeff(p)); |
---|
607 | d=nLcm(h,pGetCoeff(p),currRing); |
---|
608 | nDelete(&h); |
---|
609 | h=d; |
---|
610 | pIter(p); |
---|
611 | } |
---|
612 | /* contains the 1/lcm of all denominators */ |
---|
613 | if(!nIsOne(h)) |
---|
614 | { |
---|
615 | p = ph; |
---|
616 | while (p!=NULL) |
---|
617 | { |
---|
618 | /* should be: |
---|
619 | * number hh; |
---|
620 | * nGetDenom(p->coef,&hh); |
---|
621 | * nMult(&h,&hh,&d); |
---|
622 | * nNormalize(d); |
---|
623 | * nDelete(&hh); |
---|
624 | * nMult(d,p->coef,&hh); |
---|
625 | * nDelete(&d); |
---|
626 | * nDelete(&(p->coef)); |
---|
627 | * p->coef =hh; |
---|
628 | */ |
---|
629 | d=nMult(h,pGetCoeff(p)); |
---|
630 | nNormalize(d); |
---|
631 | pSetCoeff(p,d); |
---|
632 | pIter(p); |
---|
633 | } |
---|
634 | nDelete(&h); |
---|
635 | if (nGetChar()==1) |
---|
636 | { |
---|
637 | loop |
---|
638 | { |
---|
639 | h = nInit(1); |
---|
640 | p=ph; |
---|
641 | while (p!=NULL) |
---|
642 | { |
---|
643 | d=nLcm(h,pGetCoeff(p),currRing); |
---|
644 | nDelete(&h); |
---|
645 | h=d; |
---|
646 | pIter(p); |
---|
647 | } |
---|
648 | /* contains the 1/lcm of all denominators */ |
---|
649 | if(!nIsOne(h)) |
---|
650 | { |
---|
651 | p = ph; |
---|
652 | while (p!=NULL) |
---|
653 | { |
---|
654 | /* should be: |
---|
655 | * number hh; |
---|
656 | * nGetDenom(p->coef,&hh); |
---|
657 | * nMult(&h,&hh,&d); |
---|
658 | * nNormalize(d); |
---|
659 | * nDelete(&hh); |
---|
660 | * nMult(d,p->coef,&hh); |
---|
661 | * nDelete(&d); |
---|
662 | * nDelete(&(p->coef)); |
---|
663 | * p->coef =hh; |
---|
664 | */ |
---|
665 | d=nMult(h,pGetCoeff(p)); |
---|
666 | nNormalize(d); |
---|
667 | pSetCoeff(p,d); |
---|
668 | pIter(p); |
---|
669 | } |
---|
670 | nDelete(&h); |
---|
671 | } |
---|
672 | else |
---|
673 | { |
---|
674 | nDelete(&h); |
---|
675 | break; |
---|
676 | } |
---|
677 | } |
---|
678 | } |
---|
679 | } |
---|
680 | if (h!=NULL) nDelete(&h); |
---|
681 | |
---|
682 | p_Content(ph,r); |
---|
683 | #ifdef HAVE_RATGRING |
---|
684 | if (rIsRatGRing(r)) |
---|
685 | { |
---|
686 | /* quick unit detection in the rational case is done in gr_nc_bba */ |
---|
687 | pContentRat(ph); |
---|
688 | start=ph; |
---|
689 | } |
---|
690 | #endif |
---|
691 | } |
---|
692 | return start; |
---|
693 | } |
---|
694 | |
---|
695 | void p_Cleardenom_n(poly ph,const ring r,number &c) |
---|
696 | { |
---|
697 | number d, h; |
---|
698 | poly p; |
---|
699 | |
---|
700 | p = ph; |
---|
701 | if(pNext(p)==NULL) |
---|
702 | { |
---|
703 | c=nInvers(pGetCoeff(p)); |
---|
704 | pSetCoeff(p,nInit(1)); |
---|
705 | } |
---|
706 | else |
---|
707 | { |
---|
708 | h = nInit(1); |
---|
709 | while (p!=NULL) |
---|
710 | { |
---|
711 | nNormalize(pGetCoeff(p)); |
---|
712 | d=nLcm(h,pGetCoeff(p),r); |
---|
713 | nDelete(&h); |
---|
714 | h=d; |
---|
715 | pIter(p); |
---|
716 | } |
---|
717 | c=h; |
---|
718 | /* contains the 1/lcm of all denominators */ |
---|
719 | if(!nIsOne(h)) |
---|
720 | { |
---|
721 | p = ph; |
---|
722 | while (p!=NULL) |
---|
723 | { |
---|
724 | /* should be: |
---|
725 | * number hh; |
---|
726 | * nGetDenom(p->coef,&hh); |
---|
727 | * nMult(&h,&hh,&d); |
---|
728 | * nNormalize(d); |
---|
729 | * nDelete(&hh); |
---|
730 | * nMult(d,p->coef,&hh); |
---|
731 | * nDelete(&d); |
---|
732 | * nDelete(&(p->coef)); |
---|
733 | * p->coef =hh; |
---|
734 | */ |
---|
735 | d=nMult(h,pGetCoeff(p)); |
---|
736 | nNormalize(d); |
---|
737 | pSetCoeff(p,d); |
---|
738 | pIter(p); |
---|
739 | } |
---|
740 | if (nGetChar()==1) |
---|
741 | { |
---|
742 | loop |
---|
743 | { |
---|
744 | h = nInit(1); |
---|
745 | p=ph; |
---|
746 | while (p!=NULL) |
---|
747 | { |
---|
748 | d=nLcm(h,pGetCoeff(p),r); |
---|
749 | nDelete(&h); |
---|
750 | h=d; |
---|
751 | pIter(p); |
---|
752 | } |
---|
753 | /* contains the 1/lcm of all denominators */ |
---|
754 | if(!nIsOne(h)) |
---|
755 | { |
---|
756 | p = ph; |
---|
757 | while (p!=NULL) |
---|
758 | { |
---|
759 | /* should be: |
---|
760 | * number hh; |
---|
761 | * nGetDenom(p->coef,&hh); |
---|
762 | * nMult(&h,&hh,&d); |
---|
763 | * nNormalize(d); |
---|
764 | * nDelete(&hh); |
---|
765 | * nMult(d,p->coef,&hh); |
---|
766 | * nDelete(&d); |
---|
767 | * nDelete(&(p->coef)); |
---|
768 | * p->coef =hh; |
---|
769 | */ |
---|
770 | d=nMult(h,pGetCoeff(p)); |
---|
771 | nNormalize(d); |
---|
772 | pSetCoeff(p,d); |
---|
773 | pIter(p); |
---|
774 | } |
---|
775 | number t=nMult(c,h); |
---|
776 | nDelete(&c); |
---|
777 | c=t; |
---|
778 | } |
---|
779 | else |
---|
780 | { |
---|
781 | break; |
---|
782 | } |
---|
783 | nDelete(&h); |
---|
784 | } |
---|
785 | } |
---|
786 | } |
---|
787 | } |
---|
788 | } |
---|
789 | |
---|
790 | number p_GetAllDenom(poly ph, const ring r) |
---|
791 | { |
---|
792 | number d=n_Init(1,r); |
---|
793 | poly p = ph; |
---|
794 | |
---|
795 | while (p!=NULL) |
---|
796 | { |
---|
797 | number h=n_GetDenom(pGetCoeff(p),r); |
---|
798 | if (!n_IsOne(h,r)) |
---|
799 | { |
---|
800 | number dd=n_Mult(d,h,r); |
---|
801 | n_Delete(&d,r); |
---|
802 | d=dd; |
---|
803 | } |
---|
804 | n_Delete(&h,r); |
---|
805 | pIter(p); |
---|
806 | } |
---|
807 | return d; |
---|
808 | } |
---|
809 | |
---|
810 | /*2 |
---|
811 | *tests if p is homogeneous with respect to the actual weigths |
---|
812 | */ |
---|
813 | BOOLEAN pIsHomogeneous (poly p) |
---|
814 | { |
---|
815 | poly qp=p; |
---|
816 | int o; |
---|
817 | |
---|
818 | if ((p == NULL) || (pNext(p) == NULL)) return TRUE; |
---|
819 | pFDegProc d; |
---|
820 | if (pLexOrder && (currRing->order[0]==ringorder_lp)) |
---|
821 | d=p_Totaldegree; |
---|
822 | else |
---|
823 | d=pFDeg; |
---|
824 | o = d(p,currRing); |
---|
825 | do |
---|
826 | { |
---|
827 | if (d(qp,currRing) != o) return FALSE; |
---|
828 | pIter(qp); |
---|
829 | } |
---|
830 | while (qp != NULL); |
---|
831 | return TRUE; |
---|
832 | } |
---|
833 | |
---|
834 | /*2 |
---|
835 | *returns a re-ordered copy of a polynomial, with permutation of the variables |
---|
836 | */ |
---|
837 | poly pPermPoly (poly p, int * perm, const ring oldRing, nMapFunc nMap, |
---|
838 | int *par_perm, int OldPar) |
---|
839 | { |
---|
840 | int OldpVariables = oldRing->N; |
---|
841 | poly result = NULL; |
---|
842 | poly result_last = NULL; |
---|
843 | poly aq=NULL; /* the map coefficient */ |
---|
844 | poly qq; /* the mapped monomial */ |
---|
845 | |
---|
846 | while (p != NULL) |
---|
847 | { |
---|
848 | if ((OldPar==0)||(rField_is_GF(oldRing))) |
---|
849 | { |
---|
850 | qq = pInit(); |
---|
851 | number n=nMap(pGetCoeff(p)); |
---|
852 | if ((currRing->minpoly!=NULL) |
---|
853 | && ((rField_is_Zp_a()) || (rField_is_Q_a()))) |
---|
854 | { |
---|
855 | nNormalize(n); |
---|
856 | } |
---|
857 | pGetCoeff(qq)=n; |
---|
858 | // coef may be zero: pTest(qq); |
---|
859 | } |
---|
860 | else |
---|
861 | { |
---|
862 | qq=pOne(); |
---|
863 | aq=napPermNumber(pGetCoeff(p),par_perm,OldPar, oldRing); |
---|
864 | if ((aq!=NULL) && (currRing->minpoly!=NULL) |
---|
865 | && ((rField_is_Zp_a()) || (rField_is_Q_a()))) |
---|
866 | { |
---|
867 | pNormalize(aq); |
---|
868 | } |
---|
869 | pTest(aq); |
---|
870 | if (aq==NULL) |
---|
871 | pSetCoeff(qq,nInit(0)); |
---|
872 | } |
---|
873 | if (rRing_has_Comp(currRing)) pSetComp(qq, p_GetComp(p,oldRing)); |
---|
874 | if (nIsZero(pGetCoeff(qq))) |
---|
875 | { |
---|
876 | pLmDelete(&qq); |
---|
877 | } |
---|
878 | else |
---|
879 | { |
---|
880 | int i; |
---|
881 | int mapped_to_par=0; |
---|
882 | for(i=1; i<=OldpVariables; i++) |
---|
883 | { |
---|
884 | int e=p_GetExp(p,i,oldRing); |
---|
885 | if (e!=0) |
---|
886 | { |
---|
887 | if (perm==NULL) |
---|
888 | { |
---|
889 | pSetExp(qq,i, e); |
---|
890 | } |
---|
891 | else if (perm[i]>0) |
---|
892 | pAddExp(qq,perm[i], e/*p_GetExp( p,i,oldRing)*/); |
---|
893 | else if (perm[i]<0) |
---|
894 | { |
---|
895 | if (rField_is_GF()) |
---|
896 | { |
---|
897 | number c=pGetCoeff(qq); |
---|
898 | number ee=nfPar(1); |
---|
899 | number eee;nfPower(ee,e,&eee); //nfDelete(ee,currRing); |
---|
900 | ee=nfMult(c,eee); |
---|
901 | //nfDelete(c,currRing);nfDelete(eee,currRing); |
---|
902 | pSetCoeff0(qq,ee); |
---|
903 | } |
---|
904 | else |
---|
905 | { |
---|
906 | lnumber c=(lnumber)pGetCoeff(qq); |
---|
907 | if (c->z->next==NULL) |
---|
908 | napAddExp(c->z,-perm[i],e/*p_GetExp( p,i,oldRing)*/); |
---|
909 | else /* more difficult: we have really to multiply: */ |
---|
910 | { |
---|
911 | lnumber mmc=(lnumber)naInit(1,currRing); |
---|
912 | napSetExp(mmc->z,-perm[i],e/*p_GetExp( p,i,oldRing)*/); |
---|
913 | napSetm(mmc->z); |
---|
914 | pGetCoeff(qq)=naMult((number)c,(number)mmc); |
---|
915 | nDelete((number *)&c); |
---|
916 | nDelete((number *)&mmc); |
---|
917 | } |
---|
918 | mapped_to_par=1; |
---|
919 | } |
---|
920 | } |
---|
921 | else |
---|
922 | { |
---|
923 | /* this variable maps to 0 !*/ |
---|
924 | pLmDelete(&qq); |
---|
925 | break; |
---|
926 | } |
---|
927 | } |
---|
928 | } |
---|
929 | if (mapped_to_par |
---|
930 | && (currRing->minpoly!=NULL)) |
---|
931 | { |
---|
932 | number n=pGetCoeff(qq); |
---|
933 | nNormalize(n); |
---|
934 | pGetCoeff(qq)=n; |
---|
935 | } |
---|
936 | } |
---|
937 | pIter(p); |
---|
938 | #if 1 |
---|
939 | if (qq!=NULL) |
---|
940 | { |
---|
941 | pSetm(qq); |
---|
942 | pTest(aq); |
---|
943 | pTest(qq); |
---|
944 | if (aq!=NULL) qq=pMult(aq,qq); |
---|
945 | aq = qq; |
---|
946 | while (pNext(aq) != NULL) pIter(aq); |
---|
947 | if (result_last==NULL) |
---|
948 | { |
---|
949 | result=qq; |
---|
950 | } |
---|
951 | else |
---|
952 | { |
---|
953 | pNext(result_last)=qq; |
---|
954 | } |
---|
955 | result_last=aq; |
---|
956 | aq = NULL; |
---|
957 | } |
---|
958 | else if (aq!=NULL) |
---|
959 | { |
---|
960 | pDelete(&aq); |
---|
961 | } |
---|
962 | } |
---|
963 | result=pSortAdd(result); |
---|
964 | #else |
---|
965 | // if (qq!=NULL) |
---|
966 | // { |
---|
967 | // pSetm(qq); |
---|
968 | // pTest(qq); |
---|
969 | // pTest(aq); |
---|
970 | // if (aq!=NULL) qq=pMult(aq,qq); |
---|
971 | // aq = qq; |
---|
972 | // while (pNext(aq) != NULL) pIter(aq); |
---|
973 | // pNext(aq) = result; |
---|
974 | // aq = NULL; |
---|
975 | // result = qq; |
---|
976 | // } |
---|
977 | // else if (aq!=NULL) |
---|
978 | // { |
---|
979 | // pDelete(&aq); |
---|
980 | // } |
---|
981 | //} |
---|
982 | //p = result; |
---|
983 | //result = NULL; |
---|
984 | //while (p != NULL) |
---|
985 | //{ |
---|
986 | // qq = p; |
---|
987 | // pIter(p); |
---|
988 | // qq->next = NULL; |
---|
989 | // result = pAdd(result, qq); |
---|
990 | //} |
---|
991 | #endif |
---|
992 | pTest(result); |
---|
993 | return result; |
---|
994 | } |
---|
995 | |
---|
996 | poly ppJet(poly p, int m) |
---|
997 | { |
---|
998 | poly r=NULL; |
---|
999 | poly t=NULL; |
---|
1000 | |
---|
1001 | while (p!=NULL) |
---|
1002 | { |
---|
1003 | if (p_Totaldegree(p,currRing)<=m) |
---|
1004 | { |
---|
1005 | if (r==NULL) |
---|
1006 | r=pHead(p); |
---|
1007 | else |
---|
1008 | if (t==NULL) |
---|
1009 | { |
---|
1010 | pNext(r)=pHead(p); |
---|
1011 | t=pNext(r); |
---|
1012 | } |
---|
1013 | else |
---|
1014 | { |
---|
1015 | pNext(t)=pHead(p); |
---|
1016 | pIter(t); |
---|
1017 | } |
---|
1018 | } |
---|
1019 | pIter(p); |
---|
1020 | } |
---|
1021 | return r; |
---|
1022 | } |
---|
1023 | |
---|
1024 | poly pJet(poly p, int m) |
---|
1025 | { |
---|
1026 | poly t=NULL; |
---|
1027 | |
---|
1028 | while((p!=NULL) && (p_Totaldegree(p,currRing)>m)) pLmDelete(&p); |
---|
1029 | if (p==NULL) return NULL; |
---|
1030 | poly r=p; |
---|
1031 | while (pNext(p)!=NULL) |
---|
1032 | { |
---|
1033 | if (p_Totaldegree(pNext(p),currRing)>m) |
---|
1034 | { |
---|
1035 | pLmDelete(&pNext(p)); |
---|
1036 | } |
---|
1037 | else |
---|
1038 | pIter(p); |
---|
1039 | } |
---|
1040 | return r; |
---|
1041 | } |
---|
1042 | |
---|
1043 | poly ppJetW(poly p, int m, short *w) |
---|
1044 | { |
---|
1045 | poly r=NULL; |
---|
1046 | poly t=NULL; |
---|
1047 | while (p!=NULL) |
---|
1048 | { |
---|
1049 | if (totaldegreeWecart_IV(p,currRing,w)<=m) |
---|
1050 | { |
---|
1051 | if (r==NULL) |
---|
1052 | r=pHead(p); |
---|
1053 | else |
---|
1054 | if (t==NULL) |
---|
1055 | { |
---|
1056 | pNext(r)=pHead(p); |
---|
1057 | t=pNext(r); |
---|
1058 | } |
---|
1059 | else |
---|
1060 | { |
---|
1061 | pNext(t)=pHead(p); |
---|
1062 | pIter(t); |
---|
1063 | } |
---|
1064 | } |
---|
1065 | pIter(p); |
---|
1066 | } |
---|
1067 | return r; |
---|
1068 | } |
---|
1069 | |
---|
1070 | poly pJetW(poly p, int m, short *w) |
---|
1071 | { |
---|
1072 | while((p!=NULL) && (totaldegreeWecart_IV(p,currRing,w)>m)) pLmDelete(&p); |
---|
1073 | if (p==NULL) return NULL; |
---|
1074 | poly r=p; |
---|
1075 | while (pNext(p)!=NULL) |
---|
1076 | { |
---|
1077 | if (totaldegreeWecart_IV(pNext(p),currRing,w)>m) |
---|
1078 | { |
---|
1079 | pLmDelete(&pNext(p)); |
---|
1080 | } |
---|
1081 | else |
---|
1082 | pIter(p); |
---|
1083 | } |
---|
1084 | return r; |
---|
1085 | } |
---|
1086 | |
---|
1087 | int pMinDeg(poly p,intvec *w) |
---|
1088 | { |
---|
1089 | if(p==NULL) |
---|
1090 | return -1; |
---|
1091 | int d=-1; |
---|
1092 | while(p!=NULL) |
---|
1093 | { |
---|
1094 | int d0=0; |
---|
1095 | for(int j=0;j<pVariables;j++) |
---|
1096 | if(w==NULL||j>=w->length()) |
---|
1097 | d0+=pGetExp(p,j+1); |
---|
1098 | else |
---|
1099 | d0+=(*w)[j]*pGetExp(p,j+1); |
---|
1100 | if(d0<d||d==-1) |
---|
1101 | d=d0; |
---|
1102 | pIter(p); |
---|
1103 | } |
---|
1104 | return d; |
---|
1105 | } |
---|
1106 | |
---|
1107 | poly pSeries(int n,poly p,poly u, intvec *w) |
---|
1108 | { |
---|
1109 | short *ww=iv2array(w); |
---|
1110 | if(p!=NULL) |
---|
1111 | { |
---|
1112 | if(u==NULL) |
---|
1113 | p=pJetW(p,n,ww); |
---|
1114 | else |
---|
1115 | p=pJetW(pMult(p,pInvers(n-pMinDeg(p,w),u,w)),n,ww); |
---|
1116 | } |
---|
1117 | omFreeSize((ADDRESS)ww,(pVariables+1)*sizeof(short)); |
---|
1118 | return p; |
---|
1119 | } |
---|
1120 | |
---|
1121 | poly pInvers(int n,poly u,intvec *w) |
---|
1122 | { |
---|
1123 | short *ww=iv2array(w); |
---|
1124 | if(n<0) |
---|
1125 | return NULL; |
---|
1126 | number u0=nInvers(pGetCoeff(u)); |
---|
1127 | poly v=pNSet(u0); |
---|
1128 | if(n==0) |
---|
1129 | return v; |
---|
1130 | poly u1=pJetW(pSub(pOne(),pMult_nn(u,u0)),n,ww); |
---|
1131 | if(u1==NULL) |
---|
1132 | return v; |
---|
1133 | poly v1=pMult_nn(pCopy(u1),u0); |
---|
1134 | v=pAdd(v,pCopy(v1)); |
---|
1135 | for(int i=n/pMinDeg(u1,w);i>1;i--) |
---|
1136 | { |
---|
1137 | v1=pJetW(pMult(v1,pCopy(u1)),n,ww); |
---|
1138 | v=pAdd(v,pCopy(v1)); |
---|
1139 | } |
---|
1140 | pDelete(&u1); |
---|
1141 | pDelete(&v1); |
---|
1142 | omFreeSize((ADDRESS)ww,(pVariables+1)*sizeof(short)); |
---|
1143 | return v; |
---|
1144 | } |
---|
1145 | |
---|
1146 | long pDegW(poly p, const short *w) |
---|
1147 | { |
---|
1148 | long r=-LONG_MAX; |
---|
1149 | |
---|
1150 | while (p!=NULL) |
---|
1151 | { |
---|
1152 | long t=totaldegreeWecart_IV(p,currRing,w); |
---|
1153 | if (t>r) r=t; |
---|
1154 | pIter(p); |
---|
1155 | } |
---|
1156 | return r; |
---|
1157 | } |
---|
1158 | |
---|
1159 | /*-----------type conversions ----------------------------*/ |
---|
1160 | #if 0 |
---|
1161 | /*2 |
---|
1162 | * input: a set of polys (len elements: p[0]..p[len-1]) |
---|
1163 | * output: a vector |
---|
1164 | * p will not be changed |
---|
1165 | */ |
---|
1166 | poly pPolys2Vec(polyset p, int len) |
---|
1167 | { |
---|
1168 | poly v=NULL; |
---|
1169 | poly h; |
---|
1170 | int i; |
---|
1171 | |
---|
1172 | for (i=len-1; i>=0; i--) |
---|
1173 | { |
---|
1174 | if (p[i]) |
---|
1175 | { |
---|
1176 | h=pCopy(p[i]); |
---|
1177 | pSetCompP(h,i+1); |
---|
1178 | v=pAdd(v,h); |
---|
1179 | } |
---|
1180 | } |
---|
1181 | return v; |
---|
1182 | } |
---|
1183 | #endif |
---|
1184 | |
---|
1185 | /*2 |
---|
1186 | * convert a vector to a set of polys, |
---|
1187 | * allocates the polyset, (entries 0..(*len)-1) |
---|
1188 | * the vector will not be changed |
---|
1189 | */ |
---|
1190 | void pVec2Polys(poly v, polyset *p, int *len) |
---|
1191 | { |
---|
1192 | poly h; |
---|
1193 | int k; |
---|
1194 | |
---|
1195 | *len=pMaxComp(v); |
---|
1196 | if (*len==0) *len=1; |
---|
1197 | *p=(polyset)omAlloc0((*len)*sizeof(poly)); |
---|
1198 | while (v!=NULL) |
---|
1199 | { |
---|
1200 | h=pHead(v); |
---|
1201 | k=pGetComp(h); |
---|
1202 | pSetComp(h,0); |
---|
1203 | (*p)[k-1]=pAdd((*p)[k-1],h); |
---|
1204 | pIter(v); |
---|
1205 | } |
---|
1206 | } |
---|
1207 | |
---|
1208 | int p_Var(poly m,const ring r) |
---|
1209 | { |
---|
1210 | if (m==NULL) return 0; |
---|
1211 | if (pNext(m)!=NULL) return 0; |
---|
1212 | int i,e=0; |
---|
1213 | for (i=r->N; i>0; i--) |
---|
1214 | { |
---|
1215 | int exp=p_GetExp(m,i,r); |
---|
1216 | if (exp==1) |
---|
1217 | { |
---|
1218 | if (e==0) e=i; |
---|
1219 | else return 0; |
---|
1220 | } |
---|
1221 | else if (exp!=0) |
---|
1222 | { |
---|
1223 | return 0; |
---|
1224 | } |
---|
1225 | } |
---|
1226 | return e; |
---|
1227 | } |
---|
1228 | |
---|
1229 | /*2 |
---|
1230 | * returns TRUE if p1 = p2 |
---|
1231 | */ |
---|
1232 | BOOLEAN p_EqualPolys(poly p1,poly p2, const ring r) |
---|
1233 | { |
---|
1234 | while ((p1 != NULL) && (p2 != NULL)) |
---|
1235 | { |
---|
1236 | if (! p_LmEqual(p1, p2,r)) |
---|
1237 | return FALSE; |
---|
1238 | if (! n_Equal(p_GetCoeff(p1,r), p_GetCoeff(p2,r),r )) |
---|
1239 | return FALSE; |
---|
1240 | pIter(p1); |
---|
1241 | pIter(p2); |
---|
1242 | } |
---|
1243 | return (p1==p2); |
---|
1244 | } |
---|
1245 | |
---|
1246 | /*2 |
---|
1247 | *returns TRUE if p1 is a skalar multiple of p2 |
---|
1248 | *assume p1 != NULL and p2 != NULL |
---|
1249 | */ |
---|
1250 | BOOLEAN pComparePolys(poly p1,poly p2) |
---|
1251 | { |
---|
1252 | number n,nn; |
---|
1253 | pAssume(p1 != NULL && p2 != NULL); |
---|
1254 | |
---|
1255 | if (!pLmEqual(p1,p2)) //compare leading mons |
---|
1256 | return FALSE; |
---|
1257 | if ((pNext(p1)==NULL) && (pNext(p2)!=NULL)) |
---|
1258 | return FALSE; |
---|
1259 | if ((pNext(p2)==NULL) && (pNext(p1)!=NULL)) |
---|
1260 | return FALSE; |
---|
1261 | if (pLength(p1) != pLength(p2)) |
---|
1262 | return FALSE; |
---|
1263 | #ifdef HAVE_RINGS |
---|
1264 | if (rField_is_Ring(currRing)) |
---|
1265 | { |
---|
1266 | if (!nDivBy(pGetCoeff(p1), pGetCoeff(p2))) return FALSE; |
---|
1267 | } |
---|
1268 | #endif |
---|
1269 | n=nDiv(pGetCoeff(p1),pGetCoeff(p2)); |
---|
1270 | while ((p1 != NULL) /*&& (p2 != NULL)*/) |
---|
1271 | { |
---|
1272 | if ( ! pLmEqual(p1, p2)) |
---|
1273 | { |
---|
1274 | nDelete(&n); |
---|
1275 | return FALSE; |
---|
1276 | } |
---|
1277 | if (!nEqual(pGetCoeff(p1),nn=nMult(pGetCoeff(p2),n))) |
---|
1278 | { |
---|
1279 | nDelete(&n); |
---|
1280 | nDelete(&nn); |
---|
1281 | return FALSE; |
---|
1282 | } |
---|
1283 | nDelete(&nn); |
---|
1284 | pIter(p1); |
---|
1285 | pIter(p2); |
---|
1286 | } |
---|
1287 | nDelete(&n); |
---|
1288 | return TRUE; |
---|
1289 | } |
---|