1 | number pInitContent(poly ph); |
---|
2 | number pInitContent_a(poly ph); |
---|
3 | |
---|
4 | void p_Content(poly ph, const ring r) |
---|
5 | { |
---|
6 | #ifdef HAVE_RINGS |
---|
7 | if (rField_is_Ring(r)) |
---|
8 | { |
---|
9 | if (ph!=NULL) |
---|
10 | { |
---|
11 | number k = nGetUnit(pGetCoeff(ph)); |
---|
12 | if (!nGreaterZero(pGetCoeff(ph))) k = nNeg(k); // in-place negation |
---|
13 | if (!nIsOne(k)) |
---|
14 | { |
---|
15 | number tmpNumber = k; |
---|
16 | k = nInvers(k); |
---|
17 | nDelete(&tmpNumber); |
---|
18 | poly h = pNext(ph); |
---|
19 | p_Mult_nn(ph,k,currRing); |
---|
20 | pNormalize(ph); |
---|
21 | } |
---|
22 | nDelete(&k); |
---|
23 | } |
---|
24 | return; |
---|
25 | } |
---|
26 | #endif |
---|
27 | number h,d; |
---|
28 | poly p; |
---|
29 | |
---|
30 | // if(TEST_OPT_CONTENTSB) return; |
---|
31 | if(pNext(ph)==NULL) |
---|
32 | { |
---|
33 | pSetCoeff(ph,nInit(1)); |
---|
34 | } |
---|
35 | else |
---|
36 | { |
---|
37 | nNormalize(pGetCoeff(ph)); |
---|
38 | if(!nGreaterZero(pGetCoeff(ph))) ph = pNeg(ph); |
---|
39 | if (rField_is_Q()) |
---|
40 | { |
---|
41 | h=pInitContent(ph); |
---|
42 | p=ph; |
---|
43 | } |
---|
44 | else if ((rField_is_Extension(r)) |
---|
45 | && ((rPar(r)>1)||(r->cf->minpoly==NULL))) |
---|
46 | { |
---|
47 | h=pInitContent_a(ph); |
---|
48 | p=ph; |
---|
49 | } |
---|
50 | else |
---|
51 | { |
---|
52 | h=nCopy(pGetCoeff(ph)); |
---|
53 | p = pNext(ph); |
---|
54 | } |
---|
55 | while (p!=NULL) |
---|
56 | { |
---|
57 | nNormalize(pGetCoeff(p)); |
---|
58 | d=n_Gcd(h,pGetCoeff(p),r); |
---|
59 | nDelete(&h); |
---|
60 | h = d; |
---|
61 | if(nIsOne(h)) |
---|
62 | { |
---|
63 | break; |
---|
64 | } |
---|
65 | pIter(p); |
---|
66 | } |
---|
67 | p = ph; |
---|
68 | //number tmp; |
---|
69 | if(!nIsOne(h)) |
---|
70 | { |
---|
71 | while (p!=NULL) |
---|
72 | { |
---|
73 | //d = nDiv(pGetCoeff(p),h); |
---|
74 | //tmp = nIntDiv(pGetCoeff(p),h); |
---|
75 | //if (!nEqual(d,tmp)) |
---|
76 | //{ |
---|
77 | // StringSetS("** div0:");nWrite(pGetCoeff(p));StringAppendS("/"); |
---|
78 | // nWrite(h);StringAppendS("=");nWrite(d);StringAppendS(" int:"); |
---|
79 | // nWrite(tmp);Print(StringAppendS("\n")); |
---|
80 | //} |
---|
81 | //nDelete(&tmp); |
---|
82 | if (rField_is_Zp_a(currRing) || rField_is_Q_a(currRing)) |
---|
83 | d = nDiv(pGetCoeff(p),h); |
---|
84 | else |
---|
85 | d = nIntDiv(pGetCoeff(p),h); |
---|
86 | pSetCoeff(p,d); |
---|
87 | pIter(p); |
---|
88 | } |
---|
89 | } |
---|
90 | nDelete(&h); |
---|
91 | #ifdef HAVE_FACTORY |
---|
92 | if ( (n_GetChar() == 1) || (n_GetChar() < 0) ) /* Q[a],Q(a),Zp[a],Z/p(a) */ |
---|
93 | { |
---|
94 | singclap_divide_content(ph); |
---|
95 | if(!nGreaterZero(pGetCoeff(ph))) ph = pNeg(ph); |
---|
96 | } |
---|
97 | #endif |
---|
98 | if (rField_is_Q_a(r)) |
---|
99 | { |
---|
100 | number hzz = nlInit(1, r); |
---|
101 | h = nlInit(1, r); |
---|
102 | p=ph; |
---|
103 | while (p!=NULL) |
---|
104 | { // each monom: coeff in Q_a |
---|
105 | lnumber c_n_n=(lnumber)pGetCoeff(p); |
---|
106 | napoly c_n=c_n_n->z; |
---|
107 | while (c_n!=NULL) |
---|
108 | { // each monom: coeff in Q |
---|
109 | d=nlLcm(hzz,pGetCoeff(c_n),r->extRing); |
---|
110 | n_Delete(&hzz,r->extRing); |
---|
111 | hzz=d; |
---|
112 | pIter(c_n); |
---|
113 | } |
---|
114 | c_n=c_n_n->n; |
---|
115 | while (c_n!=NULL) |
---|
116 | { // each monom: coeff in Q |
---|
117 | d=nlLcm(h,pGetCoeff(c_n),r->extRing); |
---|
118 | n_Delete(&h,r->extRing); |
---|
119 | h=d; |
---|
120 | pIter(c_n); |
---|
121 | } |
---|
122 | pIter(p); |
---|
123 | } |
---|
124 | /* hzz contains the 1/lcm of all denominators in c_n_n->z*/ |
---|
125 | /* h contains the 1/lcm of all denominators in c_n_n->n*/ |
---|
126 | number htmp=nlInvers(h); |
---|
127 | number hzztmp=nlInvers(hzz); |
---|
128 | number hh=nlMult(hzz,h); |
---|
129 | nlDelete(&hzz,r->extRing); |
---|
130 | nlDelete(&h,r->extRing); |
---|
131 | number hg=nlGcd(hzztmp,htmp,r->extRing); |
---|
132 | nlDelete(&hzztmp,r->extRing); |
---|
133 | nlDelete(&htmp,r->extRing); |
---|
134 | h=nlMult(hh,hg); |
---|
135 | nlDelete(&hg,r->extRing); |
---|
136 | nlDelete(&hh,r->extRing); |
---|
137 | nlNormalize(h); |
---|
138 | if(!nlIsOne(h)) |
---|
139 | { |
---|
140 | p=ph; |
---|
141 | while (p!=NULL) |
---|
142 | { // each monom: coeff in Q_a |
---|
143 | lnumber c_n_n=(lnumber)pGetCoeff(p); |
---|
144 | napoly c_n=c_n_n->z; |
---|
145 | while (c_n!=NULL) |
---|
146 | { // each monom: coeff in Q |
---|
147 | d=nlMult(h,pGetCoeff(c_n)); |
---|
148 | nlNormalize(d); |
---|
149 | nlDelete(&pGetCoeff(c_n),r->extRing); |
---|
150 | pGetCoeff(c_n)=d; |
---|
151 | pIter(c_n); |
---|
152 | } |
---|
153 | c_n=c_n_n->n; |
---|
154 | while (c_n!=NULL) |
---|
155 | { // each monom: coeff in Q |
---|
156 | d=nlMult(h,pGetCoeff(c_n)); |
---|
157 | nlNormalize(d); |
---|
158 | nlDelete(&pGetCoeff(c_n),r->extRing); |
---|
159 | pGetCoeff(c_n)=d; |
---|
160 | pIter(c_n); |
---|
161 | } |
---|
162 | pIter(p); |
---|
163 | } |
---|
164 | } |
---|
165 | nlDelete(&h,r->extRing); |
---|
166 | } |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | void pSimpleContent(poly ph,int smax) // in currRing! |
---|
171 | { |
---|
172 | //if(TEST_OPT_CONTENTSB) return; |
---|
173 | if (ph==NULL) return; |
---|
174 | if (pNext(ph)==NULL) |
---|
175 | { |
---|
176 | pSetCoeff(ph,nInit(1)); |
---|
177 | return; |
---|
178 | } |
---|
179 | if ((pNext(pNext(ph))==NULL)||(!rField_is_Q())) |
---|
180 | { |
---|
181 | return; |
---|
182 | } |
---|
183 | number d=pInitContent(ph); |
---|
184 | if (nlSize(d)<=smax) |
---|
185 | { |
---|
186 | //if (TEST_OPT_PROT) PrintS("G"); |
---|
187 | return; |
---|
188 | } |
---|
189 | poly p=ph; |
---|
190 | number h=d; |
---|
191 | if (smax==1) smax=2; |
---|
192 | while (p!=NULL) |
---|
193 | { |
---|
194 | #if 0 |
---|
195 | d=nlGcd(h,pGetCoeff(p),currRing); |
---|
196 | nlDelete(&h,currRing); |
---|
197 | h = d; |
---|
198 | #else |
---|
199 | nlInpGcd(h,pGetCoeff(p),currRing); |
---|
200 | #endif |
---|
201 | if(nlSize(h)<smax) |
---|
202 | { |
---|
203 | //if (TEST_OPT_PROT) PrintS("g"); |
---|
204 | return; |
---|
205 | } |
---|
206 | pIter(p); |
---|
207 | } |
---|
208 | p = ph; |
---|
209 | if (!nlGreaterZero(pGetCoeff(p))) h=nlNeg(h); |
---|
210 | if(nlIsOne(h)) return; |
---|
211 | //if (TEST_OPT_PROT) PrintS("c"); |
---|
212 | // |
---|
213 | number inv=nlInvers(h); |
---|
214 | p_Mult_nn(p,inv,currRing); |
---|
215 | pNormalize(p); |
---|
216 | //while (p!=NULL) |
---|
217 | //{ |
---|
218 | #if 1 |
---|
219 | // d = nlIntDiv(pGetCoeff(p),h); |
---|
220 | // pSetCoeff(p,d); |
---|
221 | #else |
---|
222 | // nlInpIntDiv(pGetCoeff(p),h,currRing); |
---|
223 | #endif |
---|
224 | // pIter(p); |
---|
225 | //} |
---|
226 | nlDelete(&inv,currRing); |
---|
227 | nlDelete(&h,currRing); |
---|
228 | } |
---|
229 | |
---|
230 | number pInitContent(poly ph) // in currRing! |
---|
231 | // only for coefficients in Q |
---|
232 | #if 0 |
---|
233 | { |
---|
234 | //assume(!TEST_OPT_CONTENTSB); |
---|
235 | assume(ph!=NULL); |
---|
236 | assume(pNext(ph)!=NULL); |
---|
237 | assume(rField_is_Q()); |
---|
238 | if (pNext(pNext(ph))==NULL) |
---|
239 | { |
---|
240 | return nlGetNom(pGetCoeff(pNext(ph)),currRing); |
---|
241 | } |
---|
242 | poly p=ph; |
---|
243 | number n1=nlGetNom(pGetCoeff(p),currRing); |
---|
244 | pIter(p); |
---|
245 | number n2=nlGetNom(pGetCoeff(p),currRing); |
---|
246 | pIter(p); |
---|
247 | number d; |
---|
248 | number t; |
---|
249 | loop |
---|
250 | { |
---|
251 | nlNormalize(pGetCoeff(p)); |
---|
252 | t=nlGetNom(pGetCoeff(p),currRing); |
---|
253 | if (nlGreaterZero(t)) |
---|
254 | d=nlAdd(n1,t); |
---|
255 | else |
---|
256 | d=nlSub(n1,t); |
---|
257 | nlDelete(&t,currRing); |
---|
258 | nlDelete(&n1,currRing); |
---|
259 | n1=d; |
---|
260 | pIter(p); |
---|
261 | if (p==NULL) break; |
---|
262 | nlNormalize(pGetCoeff(p)); |
---|
263 | t=nlGetNom(pGetCoeff(p),currRing); |
---|
264 | if (nlGreaterZero(t)) |
---|
265 | d=nlAdd(n2,t); |
---|
266 | else |
---|
267 | d=nlSub(n2,t); |
---|
268 | nlDelete(&t,currRing); |
---|
269 | nlDelete(&n2,currRing); |
---|
270 | n2=d; |
---|
271 | pIter(p); |
---|
272 | if (p==NULL) break; |
---|
273 | } |
---|
274 | d=nlGcd(n1,n2,currRing); |
---|
275 | nlDelete(&n1,currRing); |
---|
276 | nlDelete(&n2,currRing); |
---|
277 | return d; |
---|
278 | } |
---|
279 | #else |
---|
280 | { |
---|
281 | number d=pGetCoeff(ph); |
---|
282 | if(SR_HDL(d)&SR_INT) return d; |
---|
283 | int s=mpz_size1(d->z); |
---|
284 | int s2=-1; |
---|
285 | number d2; |
---|
286 | loop |
---|
287 | { |
---|
288 | pIter(ph); |
---|
289 | if(ph==NULL) |
---|
290 | { |
---|
291 | if (s2==-1) return nlCopy(d); |
---|
292 | break; |
---|
293 | } |
---|
294 | if (SR_HDL(pGetCoeff(ph))&SR_INT) |
---|
295 | { |
---|
296 | s2=s; |
---|
297 | d2=d; |
---|
298 | s=0; |
---|
299 | d=pGetCoeff(ph); |
---|
300 | if (s2==0) break; |
---|
301 | } |
---|
302 | else |
---|
303 | if (mpz_size1((pGetCoeff(ph)->z))<=s) |
---|
304 | { |
---|
305 | s2=s; |
---|
306 | d2=d; |
---|
307 | d=pGetCoeff(ph); |
---|
308 | s=mpz_size1(d->z); |
---|
309 | } |
---|
310 | } |
---|
311 | return nlGcd(d,d2,currRing); |
---|
312 | } |
---|
313 | #endif |
---|
314 | |
---|
315 | number pInitContent_a(poly ph) // in currRing! |
---|
316 | // only for coefficients in K(a) anf K(a,...) |
---|
317 | { |
---|
318 | number d=pGetCoeff(ph); |
---|
319 | int s=n_ParDeg(d, currRing); |
---|
320 | if (s /* n_ParDeg(d, currRing)*/ <=1) return naCopy(d); |
---|
321 | int s2=-1; |
---|
322 | number d2; |
---|
323 | int ss; |
---|
324 | loop |
---|
325 | { |
---|
326 | pIter(ph); |
---|
327 | if(ph==NULL) |
---|
328 | { |
---|
329 | if (s2==-1) return naCopy(d); |
---|
330 | break; |
---|
331 | } |
---|
332 | if ((ss=n_ParDeg(pGetCoeff(ph), currRing))<s) |
---|
333 | { |
---|
334 | s2=s; |
---|
335 | d2=d; |
---|
336 | s=ss; |
---|
337 | d=pGetCoeff(ph); |
---|
338 | if (s2<=1) break; |
---|
339 | } |
---|
340 | } |
---|
341 | return naGcd(d,d2,currRing); |
---|
342 | } |
---|
343 | |
---|
344 | |
---|
345 | //void pContent(poly ph) |
---|
346 | //{ |
---|
347 | // number h,d; |
---|
348 | // poly p; |
---|
349 | // |
---|
350 | // p = ph; |
---|
351 | // if(pNext(p)==NULL) |
---|
352 | // { |
---|
353 | // pSetCoeff(p,nInit(1)); |
---|
354 | // } |
---|
355 | // else |
---|
356 | // { |
---|
357 | //#ifdef PDEBUG |
---|
358 | // if (!pTest(p)) return; |
---|
359 | //#endif |
---|
360 | // nNormalize(pGetCoeff(p)); |
---|
361 | // if(!nGreaterZero(pGetCoeff(ph))) |
---|
362 | // { |
---|
363 | // ph = pNeg(ph); |
---|
364 | // nNormalize(pGetCoeff(p)); |
---|
365 | // } |
---|
366 | // h=pGetCoeff(p); |
---|
367 | // pIter(p); |
---|
368 | // while (p!=NULL) |
---|
369 | // { |
---|
370 | // nNormalize(pGetCoeff(p)); |
---|
371 | // if (nGreater(h,pGetCoeff(p))) h=pGetCoeff(p); |
---|
372 | // pIter(p); |
---|
373 | // } |
---|
374 | // h=nCopy(h); |
---|
375 | // p=ph; |
---|
376 | // while (p!=NULL) |
---|
377 | // { |
---|
378 | // d=n_Gcd(h,pGetCoeff(p)); |
---|
379 | // nDelete(&h); |
---|
380 | // h = d; |
---|
381 | // if(nIsOne(h)) |
---|
382 | // { |
---|
383 | // break; |
---|
384 | // } |
---|
385 | // pIter(p); |
---|
386 | // } |
---|
387 | // p = ph; |
---|
388 | // //number tmp; |
---|
389 | // if(!nIsOne(h)) |
---|
390 | // { |
---|
391 | // while (p!=NULL) |
---|
392 | // { |
---|
393 | // d = nIntDiv(pGetCoeff(p),h); |
---|
394 | // pSetCoeff(p,d); |
---|
395 | // pIter(p); |
---|
396 | // } |
---|
397 | // } |
---|
398 | // nDelete(&h); |
---|
399 | //#ifdef HAVE_FACTORY |
---|
400 | // if ( (n_GetChar() == 1) || (n_GetChar() < 0) ) /* Q[a],Q(a),Zp[a],Z/p(a) */ |
---|
401 | // { |
---|
402 | // pTest(ph); |
---|
403 | // singclap_divide_content(ph); |
---|
404 | // pTest(ph); |
---|
405 | // } |
---|
406 | //#endif |
---|
407 | // } |
---|
408 | //} |
---|
409 | #if 0 |
---|
410 | void p_Content(poly ph, ring r) |
---|
411 | { |
---|
412 | number h,d; |
---|
413 | poly p; |
---|
414 | |
---|
415 | if(pNext(ph)==NULL) |
---|
416 | { |
---|
417 | pSetCoeff(ph,n_Init(1,r)); |
---|
418 | } |
---|
419 | else |
---|
420 | { |
---|
421 | n_Normalize(pGetCoeff(ph),r); |
---|
422 | if(!n_GreaterZero(pGetCoeff(ph),r)) ph = p_Neg(ph,r); |
---|
423 | h=n_Copy(pGetCoeff(ph),r); |
---|
424 | p = pNext(ph); |
---|
425 | while (p!=NULL) |
---|
426 | { |
---|
427 | n_Normalize(pGetCoeff(p),r); |
---|
428 | d=n_Gcd(h,pGetCoeff(p),r); |
---|
429 | n_Delete(&h,r); |
---|
430 | h = d; |
---|
431 | if(n_IsOne(h,r)) |
---|
432 | { |
---|
433 | break; |
---|
434 | } |
---|
435 | pIter(p); |
---|
436 | } |
---|
437 | p = ph; |
---|
438 | //number tmp; |
---|
439 | if(!n_IsOne(h,r)) |
---|
440 | { |
---|
441 | while (p!=NULL) |
---|
442 | { |
---|
443 | //d = nDiv(pGetCoeff(p),h); |
---|
444 | //tmp = nIntDiv(pGetCoeff(p),h); |
---|
445 | //if (!nEqual(d,tmp)) |
---|
446 | //{ |
---|
447 | // StringSetS("** div0:");nWrite(pGetCoeff(p));StringAppendS("/"); |
---|
448 | // nWrite(h);StringAppendS("=");nWrite(d);StringAppendS(" int:"); |
---|
449 | // nWrite(tmp);Print(StringAppendS("\n")); |
---|
450 | //} |
---|
451 | //nDelete(&tmp); |
---|
452 | d = n_IntDiv(pGetCoeff(p),h,r); |
---|
453 | p_SetCoeff(p,d,r); |
---|
454 | pIter(p); |
---|
455 | } |
---|
456 | } |
---|
457 | n_Delete(&h,r); |
---|
458 | #ifdef HAVE_FACTORY |
---|
459 | //if ( (n_GetChar(r) == 1) || (n_GetChar(r) < 0) ) /* Q[a],Q(a),Zp[a],Z/p(a) */ |
---|
460 | //{ |
---|
461 | // singclap_divide_content(ph); |
---|
462 | // if(!n_GreaterZero(pGetCoeff(ph),r)) ph = p_Neg(ph,r); |
---|
463 | //} |
---|
464 | #endif |
---|
465 | } |
---|
466 | } |
---|
467 | #endif |
---|
468 | |
---|
469 | poly p_Cleardenom(poly ph, const ring r) |
---|
470 | { |
---|
471 | poly start=ph; |
---|
472 | number d, h; |
---|
473 | poly p; |
---|
474 | |
---|
475 | #ifdef HAVE_RINGS |
---|
476 | if (rField_is_Ring(r)) |
---|
477 | { |
---|
478 | p_Content(ph,r); |
---|
479 | return start; |
---|
480 | } |
---|
481 | #endif |
---|
482 | if (rField_is_Zp(r) && TEST_OPT_INTSTRATEGY) return start; |
---|
483 | p = ph; |
---|
484 | if(pNext(p)==NULL) |
---|
485 | { |
---|
486 | /* |
---|
487 | if (TEST_OPT_CONTENTSB) |
---|
488 | { |
---|
489 | number n=nGetDenom(pGetCoeff(p)); |
---|
490 | if (!nIsOne(n)) |
---|
491 | { |
---|
492 | number nn=nMult(pGetCoeff(p),n); |
---|
493 | nNormalize(nn); |
---|
494 | pSetCoeff(p,nn); |
---|
495 | } |
---|
496 | nDelete(&n); |
---|
497 | } |
---|
498 | else |
---|
499 | */ |
---|
500 | pSetCoeff(p,nInit(1)); |
---|
501 | } |
---|
502 | else |
---|
503 | { |
---|
504 | h = nInit(1); |
---|
505 | while (p!=NULL) |
---|
506 | { |
---|
507 | nNormalize(pGetCoeff(p)); |
---|
508 | d=nLcm(h,pGetCoeff(p),currRing); |
---|
509 | nDelete(&h); |
---|
510 | h=d; |
---|
511 | pIter(p); |
---|
512 | } |
---|
513 | /* contains the 1/lcm of all denominators */ |
---|
514 | if(!nIsOne(h)) |
---|
515 | { |
---|
516 | p = ph; |
---|
517 | while (p!=NULL) |
---|
518 | { |
---|
519 | /* should be: |
---|
520 | * number hh; |
---|
521 | * nGetDenom(p->coef,&hh); |
---|
522 | * nMult(&h,&hh,&d); |
---|
523 | * nNormalize(d); |
---|
524 | * nDelete(&hh); |
---|
525 | * nMult(d,p->coef,&hh); |
---|
526 | * nDelete(&d); |
---|
527 | * nDelete(&(p->coef)); |
---|
528 | * p->coef =hh; |
---|
529 | */ |
---|
530 | d=nMult(h,pGetCoeff(p)); |
---|
531 | nNormalize(d); |
---|
532 | pSetCoeff(p,d); |
---|
533 | pIter(p); |
---|
534 | } |
---|
535 | nDelete(&h); |
---|
536 | if (n_GetChar()==1) |
---|
537 | { |
---|
538 | loop |
---|
539 | { |
---|
540 | h = nInit(1); |
---|
541 | p=ph; |
---|
542 | while (p!=NULL) |
---|
543 | { |
---|
544 | d=nLcm(h,pGetCoeff(p),currRing); |
---|
545 | nDelete(&h); |
---|
546 | h=d; |
---|
547 | pIter(p); |
---|
548 | } |
---|
549 | /* contains the 1/lcm of all denominators */ |
---|
550 | if(!nIsOne(h)) |
---|
551 | { |
---|
552 | p = ph; |
---|
553 | while (p!=NULL) |
---|
554 | { |
---|
555 | /* should be: |
---|
556 | * number hh; |
---|
557 | * nGetDenom(p->coef,&hh); |
---|
558 | * nMult(&h,&hh,&d); |
---|
559 | * nNormalize(d); |
---|
560 | * nDelete(&hh); |
---|
561 | * nMult(d,p->coef,&hh); |
---|
562 | * nDelete(&d); |
---|
563 | * nDelete(&(p->coef)); |
---|
564 | * p->coef =hh; |
---|
565 | */ |
---|
566 | d=nMult(h,pGetCoeff(p)); |
---|
567 | nNormalize(d); |
---|
568 | pSetCoeff(p,d); |
---|
569 | pIter(p); |
---|
570 | } |
---|
571 | nDelete(&h); |
---|
572 | } |
---|
573 | else |
---|
574 | { |
---|
575 | nDelete(&h); |
---|
576 | break; |
---|
577 | } |
---|
578 | } |
---|
579 | } |
---|
580 | } |
---|
581 | if (h!=NULL) nDelete(&h); |
---|
582 | |
---|
583 | p_Content(ph,r); |
---|
584 | #ifdef HAVE_RATGRING |
---|
585 | if (rIsRatGRing(r)) |
---|
586 | { |
---|
587 | /* quick unit detection in the rational case is done in gr_nc_bba */ |
---|
588 | pContentRat(ph); |
---|
589 | start=ph; |
---|
590 | } |
---|
591 | #endif |
---|
592 | } |
---|
593 | return start; |
---|
594 | } |
---|
595 | |
---|
596 | void p_Cleardenom_n(poly ph,const ring r,number &c) |
---|
597 | { |
---|
598 | number d, h; |
---|
599 | poly p; |
---|
600 | |
---|
601 | p = ph; |
---|
602 | if(pNext(p)==NULL) |
---|
603 | { |
---|
604 | c=nInvers(pGetCoeff(p)); |
---|
605 | pSetCoeff(p,nInit(1)); |
---|
606 | } |
---|
607 | else |
---|
608 | { |
---|
609 | h = nInit(1); |
---|
610 | while (p!=NULL) |
---|
611 | { |
---|
612 | nNormalize(pGetCoeff(p)); |
---|
613 | d=nLcm(h,pGetCoeff(p),r); |
---|
614 | nDelete(&h); |
---|
615 | h=d; |
---|
616 | pIter(p); |
---|
617 | } |
---|
618 | c=h; |
---|
619 | /* contains the 1/lcm of all denominators */ |
---|
620 | if(!nIsOne(h)) |
---|
621 | { |
---|
622 | p = ph; |
---|
623 | while (p!=NULL) |
---|
624 | { |
---|
625 | /* should be: |
---|
626 | * number hh; |
---|
627 | * nGetDenom(p->coef,&hh); |
---|
628 | * nMult(&h,&hh,&d); |
---|
629 | * nNormalize(d); |
---|
630 | * nDelete(&hh); |
---|
631 | * nMult(d,p->coef,&hh); |
---|
632 | * nDelete(&d); |
---|
633 | * nDelete(&(p->coef)); |
---|
634 | * p->coef =hh; |
---|
635 | */ |
---|
636 | d=nMult(h,pGetCoeff(p)); |
---|
637 | nNormalize(d); |
---|
638 | pSetCoeff(p,d); |
---|
639 | pIter(p); |
---|
640 | } |
---|
641 | if (n_GetChar()==1) |
---|
642 | { |
---|
643 | loop |
---|
644 | { |
---|
645 | h = nInit(1); |
---|
646 | p=ph; |
---|
647 | while (p!=NULL) |
---|
648 | { |
---|
649 | d=nLcm(h,pGetCoeff(p),r); |
---|
650 | nDelete(&h); |
---|
651 | h=d; |
---|
652 | pIter(p); |
---|
653 | } |
---|
654 | /* contains the 1/lcm of all denominators */ |
---|
655 | if(!nIsOne(h)) |
---|
656 | { |
---|
657 | p = ph; |
---|
658 | while (p!=NULL) |
---|
659 | { |
---|
660 | /* should be: |
---|
661 | * number hh; |
---|
662 | * nGetDenom(p->coef,&hh); |
---|
663 | * nMult(&h,&hh,&d); |
---|
664 | * nNormalize(d); |
---|
665 | * nDelete(&hh); |
---|
666 | * nMult(d,p->coef,&hh); |
---|
667 | * nDelete(&d); |
---|
668 | * nDelete(&(p->coef)); |
---|
669 | * p->coef =hh; |
---|
670 | */ |
---|
671 | d=nMult(h,pGetCoeff(p)); |
---|
672 | nNormalize(d); |
---|
673 | pSetCoeff(p,d); |
---|
674 | pIter(p); |
---|
675 | } |
---|
676 | number t=nMult(c,h); |
---|
677 | nDelete(&c); |
---|
678 | c=t; |
---|
679 | } |
---|
680 | else |
---|
681 | { |
---|
682 | break; |
---|
683 | } |
---|
684 | nDelete(&h); |
---|
685 | } |
---|
686 | } |
---|
687 | } |
---|
688 | } |
---|
689 | } |
---|
690 | |
---|
691 | number p_GetAllDenom(poly ph, const ring r) |
---|
692 | { |
---|
693 | number d=n_Init(1,r); |
---|
694 | poly p = ph; |
---|
695 | |
---|
696 | while (p!=NULL) |
---|
697 | { |
---|
698 | number h=n_GetDenom(pGetCoeff(p),r); |
---|
699 | if (!n_IsOne(h,r)) |
---|
700 | { |
---|
701 | number dd=n_Mult(d,h,r); |
---|
702 | n_Delete(&d,r); |
---|
703 | d=dd; |
---|
704 | } |
---|
705 | n_Delete(&h,r); |
---|
706 | pIter(p); |
---|
707 | } |
---|
708 | return d; |
---|
709 | } |
---|
710 | |
---|
711 | /*2 |
---|
712 | *tests if p is homogeneous with respect to the actual weigths |
---|
713 | */ |
---|
714 | BOOLEAN pIsHomogeneous (poly p) |
---|
715 | { |
---|
716 | poly qp=p; |
---|
717 | int o; |
---|
718 | |
---|
719 | if ((p == NULL) || (pNext(p) == NULL)) return TRUE; |
---|
720 | pFDegProc d; |
---|
721 | if (pLexOrder && (currRing->order[0]==ringorder_lp)) |
---|
722 | d=p_Totaldegree; |
---|
723 | else |
---|
724 | d=pFDeg; |
---|
725 | o = d(p,currRing); |
---|
726 | do |
---|
727 | { |
---|
728 | if (d(qp,currRing) != o) return FALSE; |
---|
729 | pIter(qp); |
---|
730 | } |
---|
731 | while (qp != NULL); |
---|
732 | return TRUE; |
---|
733 | } |
---|
734 | |
---|
735 | /*2 |
---|
736 | *returns a re-ordered copy of a polynomial, with permutation of the variables |
---|
737 | */ |
---|
738 | poly pPermPoly (poly p, int * perm, const ring oldRing, nMapFunc nMap, |
---|
739 | int *par_perm, int OldPar) |
---|
740 | { |
---|
741 | int OldpVariables = oldRing->N; |
---|
742 | poly result = NULL; |
---|
743 | poly result_last = NULL; |
---|
744 | poly aq=NULL; /* the map coefficient */ |
---|
745 | poly qq; /* the mapped monomial */ |
---|
746 | |
---|
747 | while (p != NULL) |
---|
748 | { |
---|
749 | if ((OldPar==0)||(rField_is_GF(oldRing))) |
---|
750 | { |
---|
751 | qq = pInit(); |
---|
752 | number n=nMap(pGetCoeff(p)); |
---|
753 | if ((currRing->cf->minpoly!=NULL) |
---|
754 | && ((rField_is_Zp_a()) || (rField_is_Q_a()))) |
---|
755 | { |
---|
756 | nNormalize(n); |
---|
757 | } |
---|
758 | pGetCoeff(qq)=n; |
---|
759 | // coef may be zero: pTest(qq); |
---|
760 | } |
---|
761 | else |
---|
762 | { |
---|
763 | qq=pOne(); |
---|
764 | aq=napPermNumber(pGetCoeff(p),par_perm,OldPar, oldRing); |
---|
765 | if ((aq!=NULL) && (currRing->cf->minpoly!=NULL) |
---|
766 | && ((rField_is_Zp_a()) || (rField_is_Q_a()))) |
---|
767 | { |
---|
768 | pNormalize(aq); |
---|
769 | } |
---|
770 | pTest(aq); |
---|
771 | if (aq==NULL) |
---|
772 | pSetCoeff(qq,nInit(0)); |
---|
773 | } |
---|
774 | if (rRing_has_Comp(currRing)) pSetComp(qq, p_GetComp(p,oldRing)); |
---|
775 | if (nIsZero(pGetCoeff(qq))) |
---|
776 | { |
---|
777 | pLmDelete(&qq); |
---|
778 | } |
---|
779 | else |
---|
780 | { |
---|
781 | int i; |
---|
782 | int mapped_to_par=0; |
---|
783 | for(i=1; i<=OldpVariables; i++) |
---|
784 | { |
---|
785 | int e=p_GetExp(p,i,oldRing); |
---|
786 | if (e!=0) |
---|
787 | { |
---|
788 | if (perm==NULL) |
---|
789 | { |
---|
790 | pSetExp(qq,i, e); |
---|
791 | } |
---|
792 | else if (perm[i]>0) |
---|
793 | pAddExp(qq,perm[i], e/*p_GetExp( p,i,oldRing)*/); |
---|
794 | else if (perm[i]<0) |
---|
795 | { |
---|
796 | if (rField_is_GF()) |
---|
797 | { |
---|
798 | number c=pGetCoeff(qq); |
---|
799 | number ee=n_Parameter(1, currRing->cf); // ? |
---|
800 | number eee;nfPower(ee,e,&eee); //nfDelete(ee,currRing); |
---|
801 | ee=nfMult(c,eee); |
---|
802 | //nfDelete(c,currRing);nfDelete(eee,currRing); |
---|
803 | pSetCoeff0(qq,ee); |
---|
804 | } |
---|
805 | else |
---|
806 | { |
---|
807 | lnumber c=(lnumber)pGetCoeff(qq); |
---|
808 | if (c->z->next==NULL) |
---|
809 | napAddExp(c->z,-perm[i],e/*p_GetExp( p,i,oldRing)*/); |
---|
810 | else /* more difficult: we have really to multiply: */ |
---|
811 | { |
---|
812 | lnumber mmc=(lnumber)naInit(1,currRing); |
---|
813 | napSetExp(mmc->z,-perm[i],e/*p_GetExp( p,i,oldRing)*/); |
---|
814 | napSetm(mmc->z); |
---|
815 | pGetCoeff(qq)=naMult((number)c,(number)mmc); |
---|
816 | nDelete((number *)&c); |
---|
817 | nDelete((number *)&mmc); |
---|
818 | } |
---|
819 | mapped_to_par=1; |
---|
820 | } |
---|
821 | } |
---|
822 | else |
---|
823 | { |
---|
824 | /* this variable maps to 0 !*/ |
---|
825 | pLmDelete(&qq); |
---|
826 | break; |
---|
827 | } |
---|
828 | } |
---|
829 | } |
---|
830 | if (mapped_to_par |
---|
831 | && (currRing->cf->minpoly!=NULL)) |
---|
832 | { |
---|
833 | number n=pGetCoeff(qq); |
---|
834 | nNormalize(n); |
---|
835 | pGetCoeff(qq)=n; |
---|
836 | } |
---|
837 | } |
---|
838 | pIter(p); |
---|
839 | #if 1 |
---|
840 | if (qq!=NULL) |
---|
841 | { |
---|
842 | pSetm(qq); |
---|
843 | pTest(aq); |
---|
844 | pTest(qq); |
---|
845 | if (aq!=NULL) qq=pMult(aq,qq); |
---|
846 | aq = qq; |
---|
847 | while (pNext(aq) != NULL) pIter(aq); |
---|
848 | if (result_last==NULL) |
---|
849 | { |
---|
850 | result=qq; |
---|
851 | } |
---|
852 | else |
---|
853 | { |
---|
854 | pNext(result_last)=qq; |
---|
855 | } |
---|
856 | result_last=aq; |
---|
857 | aq = NULL; |
---|
858 | } |
---|
859 | else if (aq!=NULL) |
---|
860 | { |
---|
861 | pDelete(&aq); |
---|
862 | } |
---|
863 | } |
---|
864 | result=pSortAdd(result); |
---|
865 | #else |
---|
866 | // if (qq!=NULL) |
---|
867 | // { |
---|
868 | // pSetm(qq); |
---|
869 | // pTest(qq); |
---|
870 | // pTest(aq); |
---|
871 | // if (aq!=NULL) qq=pMult(aq,qq); |
---|
872 | // aq = qq; |
---|
873 | // while (pNext(aq) != NULL) pIter(aq); |
---|
874 | // pNext(aq) = result; |
---|
875 | // aq = NULL; |
---|
876 | // result = qq; |
---|
877 | // } |
---|
878 | // else if (aq!=NULL) |
---|
879 | // { |
---|
880 | // pDelete(&aq); |
---|
881 | // } |
---|
882 | //} |
---|
883 | //p = result; |
---|
884 | //result = NULL; |
---|
885 | //while (p != NULL) |
---|
886 | //{ |
---|
887 | // qq = p; |
---|
888 | // pIter(p); |
---|
889 | // qq->next = NULL; |
---|
890 | // result = pAdd(result, qq); |
---|
891 | //} |
---|
892 | #endif |
---|
893 | pTest(result); |
---|
894 | return result; |
---|
895 | } |
---|
896 | |
---|