1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /*************************************************************** |
---|
5 | * File: p_polys.cc |
---|
6 | * Purpose: implementation of currRing independent poly procedures |
---|
7 | * Author: obachman (Olaf Bachmann) |
---|
8 | * Created: 8/00 |
---|
9 | * Version: $Id: p_polys.cc,v 1.7 2007-07-05 10:00:10 Singular Exp $ |
---|
10 | *******************************************************************/ |
---|
11 | |
---|
12 | #include "mod2.h" |
---|
13 | #include "structs.h" |
---|
14 | #include "structs.h" |
---|
15 | #include "p_polys.h" |
---|
16 | #include "ring.h" |
---|
17 | #include "int64vec.h" |
---|
18 | #ifndef NDEBUG |
---|
19 | #include "febase.h" |
---|
20 | #endif |
---|
21 | |
---|
22 | /*************************************************************** |
---|
23 | * |
---|
24 | * Completing what needs to be set for the monomial |
---|
25 | * |
---|
26 | ***************************************************************/ |
---|
27 | // this is special for the syz stuff |
---|
28 | static int* _Components = NULL; |
---|
29 | static long* _ShiftedComponents = NULL; |
---|
30 | static int _ExternalComponents = 0; |
---|
31 | |
---|
32 | BOOLEAN pSetm_error=0; |
---|
33 | |
---|
34 | void p_Setm_General(poly p, const ring r) |
---|
35 | { |
---|
36 | p_LmCheckPolyRing(p, r); |
---|
37 | int pos=0; |
---|
38 | if (r->typ!=NULL) |
---|
39 | { |
---|
40 | loop |
---|
41 | { |
---|
42 | long ord=0; |
---|
43 | sro_ord* o=&(r->typ[pos]); |
---|
44 | switch(o->ord_typ) |
---|
45 | { |
---|
46 | case ro_dp: |
---|
47 | { |
---|
48 | int a,e; |
---|
49 | a=o->data.dp.start; |
---|
50 | e=o->data.dp.end; |
---|
51 | for(int i=a;i<=e;i++) ord+=p_GetExp(p,i,r); |
---|
52 | p->exp[o->data.dp.place]=ord; |
---|
53 | break; |
---|
54 | } |
---|
55 | case ro_wp_neg: |
---|
56 | ord=POLY_NEGWEIGHT_OFFSET; |
---|
57 | // no break; |
---|
58 | case ro_wp: |
---|
59 | { |
---|
60 | int a,e; |
---|
61 | a=o->data.wp.start; |
---|
62 | e=o->data.wp.end; |
---|
63 | int *w=o->data.wp.weights; |
---|
64 | #if 1 |
---|
65 | for(int i=a;i<=e;i++) ord+=p_GetExp(p,i,r)*w[i-a]; |
---|
66 | #else |
---|
67 | long ai; |
---|
68 | int ei,wi; |
---|
69 | for(int i=a;i<=e;i++) |
---|
70 | { |
---|
71 | ei=p_GetExp(p,i,r); |
---|
72 | wi=w[i-a]; |
---|
73 | ai=ei*wi; |
---|
74 | if (ai/ei!=wi) pSetm_error=TRUE; |
---|
75 | ord+=ai; |
---|
76 | if (ord<ai) pSetm_error=TRUE; |
---|
77 | } |
---|
78 | #endif |
---|
79 | p->exp[o->data.wp.place]=ord; |
---|
80 | break; |
---|
81 | } |
---|
82 | case ro_wp64: |
---|
83 | { |
---|
84 | int64 ord=0; |
---|
85 | int a,e; |
---|
86 | a=o->data.wp64.start; |
---|
87 | e=o->data.wp64.end; |
---|
88 | int64 *w=o->data.wp64.weights64; |
---|
89 | int64 ei,wi,ai; |
---|
90 | for(int i=a;i<=e;i++) { |
---|
91 | //Print("exp %d w %d \n",p_GetExp(p,i,r),(int)w[i-a]); |
---|
92 | //ord+=((int64)p_GetExp(p,i,r))*w[i-a]; |
---|
93 | ei=(int64)p_GetExp(p,i,r); |
---|
94 | wi=w[i-a]; |
---|
95 | ai=ei*wi; |
---|
96 | if(ei!=0 && ai/ei!=wi){ |
---|
97 | pSetm_error=TRUE; |
---|
98 | Print("ai %lld, wi %lld\n",ai,wi); |
---|
99 | } |
---|
100 | ord+=ai; |
---|
101 | if (ord<ai){ |
---|
102 | pSetm_error=TRUE; |
---|
103 | Print("ai %lld, ord %lld\n",ai,ord); |
---|
104 | } |
---|
105 | } |
---|
106 | int64 mask=(int64)0x7fffffff; |
---|
107 | long a_0=(long)(ord&mask); //2^31 |
---|
108 | long a_1=(long)(ord >>31 ); /*(ord/(mask+1));*/ |
---|
109 | |
---|
110 | //Print("mask: %x, ord: %d, a_0: %d, a_1: %d\n" |
---|
111 | //,(int)mask,(int)ord,(int)a_0,(int)a_1); |
---|
112 | //Print("mask: %d",mask); |
---|
113 | |
---|
114 | p->exp[o->data.wp64.place]=a_1; |
---|
115 | p->exp[o->data.wp64.place+1]=a_0; |
---|
116 | // if(p_Setm_error) Print("***************************\n |
---|
117 | // ***************************\n |
---|
118 | // **WARNING: overflow error**\n |
---|
119 | // ***************************\n |
---|
120 | // ***************************\n"); |
---|
121 | break; |
---|
122 | } |
---|
123 | case ro_cp: |
---|
124 | { |
---|
125 | int a,e; |
---|
126 | a=o->data.cp.start; |
---|
127 | e=o->data.cp.end; |
---|
128 | int pl=o->data.cp.place; |
---|
129 | for(int i=a;i<=e;i++) { p->exp[pl]=p_GetExp(p,i,r); pl++; } |
---|
130 | break; |
---|
131 | } |
---|
132 | case ro_syzcomp: |
---|
133 | { |
---|
134 | int c=p_GetComp(p,r); |
---|
135 | long sc = c; |
---|
136 | int* Components = (_ExternalComponents ? _Components : |
---|
137 | o->data.syzcomp.Components); |
---|
138 | long* ShiftedComponents = (_ExternalComponents ? _ShiftedComponents: |
---|
139 | o->data.syzcomp.ShiftedComponents); |
---|
140 | if (ShiftedComponents != NULL) |
---|
141 | { |
---|
142 | assume(Components != NULL); |
---|
143 | assume(c == 0 || Components[c] != 0); |
---|
144 | sc = ShiftedComponents[Components[c]]; |
---|
145 | assume(c == 0 || sc != 0); |
---|
146 | } |
---|
147 | p->exp[o->data.syzcomp.place]=sc; |
---|
148 | break; |
---|
149 | } |
---|
150 | case ro_syz: |
---|
151 | { |
---|
152 | int c=p_GetComp(p, r); |
---|
153 | if (c > o->data.syz.limit) |
---|
154 | p->exp[o->data.syz.place] = o->data.syz.curr_index; |
---|
155 | else if (c > 0) |
---|
156 | p->exp[o->data.syz.place]= o->data.syz.syz_index[c]; |
---|
157 | else |
---|
158 | { |
---|
159 | assume(c == 0); |
---|
160 | p->exp[o->data.syz.place]= 0; |
---|
161 | } |
---|
162 | break; |
---|
163 | } |
---|
164 | default: |
---|
165 | dReportError("wrong ord in rSetm:%d\n",o->ord_typ); |
---|
166 | return; |
---|
167 | } |
---|
168 | pos++; |
---|
169 | if (pos == r->OrdSize) return; |
---|
170 | } |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | void p_Setm_Syz(poly p, ring r, int* Components, long* ShiftedComponents) |
---|
175 | { |
---|
176 | _Components = Components; |
---|
177 | _ShiftedComponents = ShiftedComponents; |
---|
178 | _ExternalComponents = 1; |
---|
179 | p_Setm_General(p, r); |
---|
180 | _ExternalComponents = 0; |
---|
181 | } |
---|
182 | |
---|
183 | // dummy for lp, ls, etc |
---|
184 | void p_Setm_Dummy(poly p, const ring r) |
---|
185 | { |
---|
186 | p_LmCheckPolyRing(p, r); |
---|
187 | } |
---|
188 | |
---|
189 | // for dp, Dp, ds, etc |
---|
190 | void p_Setm_TotalDegree(poly p, const ring r) |
---|
191 | { |
---|
192 | p_LmCheckPolyRing(p, r); |
---|
193 | p->exp[r->pOrdIndex] = p_ExpVectorQuerSum(p, r); |
---|
194 | } |
---|
195 | |
---|
196 | // for wp, Wp, ws, etc |
---|
197 | void p_Setm_WFirstTotalDegree(poly p, const ring r) |
---|
198 | { |
---|
199 | p_LmCheckPolyRing(p, r); |
---|
200 | p->exp[r->pOrdIndex] = pWFirstTotalDegree(p, r); |
---|
201 | } |
---|
202 | |
---|
203 | p_SetmProc p_GetSetmProc(ring r) |
---|
204 | { |
---|
205 | // covers lp, rp, ls, |
---|
206 | if (r->typ == NULL) return p_Setm_Dummy; |
---|
207 | |
---|
208 | if (r->OrdSize == 1) |
---|
209 | { |
---|
210 | if (r->typ[0].ord_typ == ro_dp && |
---|
211 | r->typ[0].data.dp.start == 1 && |
---|
212 | r->typ[0].data.dp.end == r->N && |
---|
213 | r->typ[0].data.dp.place == r->pOrdIndex) |
---|
214 | return p_Setm_TotalDegree; |
---|
215 | if (r->typ[0].ord_typ == ro_wp && |
---|
216 | r->typ[0].data.wp.start == 1 && |
---|
217 | r->typ[0].data.wp.end == r->N && |
---|
218 | r->typ[0].data.wp.place == r->pOrdIndex && |
---|
219 | r->typ[0].data.wp.weights == r->firstwv) |
---|
220 | return p_Setm_WFirstTotalDegree; |
---|
221 | } |
---|
222 | return p_Setm_General; |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | /* -------------------------------------------------------------------*/ |
---|
227 | /* several possibilities for pFDeg: the degree of the head term */ |
---|
228 | /*2 |
---|
229 | * compute the degree of the leading monomial of p |
---|
230 | * the ordering is compatible with degree, use a->order |
---|
231 | */ |
---|
232 | inline long _pDeg(poly a, ring r) |
---|
233 | { |
---|
234 | p_LmCheckPolyRing(a, r); |
---|
235 | assume(p_GetOrder(a, r) == pWTotaldegree(a, r)); |
---|
236 | return p_GetOrder(a, r); |
---|
237 | } |
---|
238 | |
---|
239 | long pDeg(poly a, ring r) |
---|
240 | { |
---|
241 | return _pDeg(a, r); |
---|
242 | } |
---|
243 | |
---|
244 | /*2 |
---|
245 | * compute the degree of the leading monomial of p |
---|
246 | * with respect to weigths 1 |
---|
247 | * (all are 1 so save multiplications or they are of different signs) |
---|
248 | * the ordering is not compatible with degree so do not use p->Order |
---|
249 | */ |
---|
250 | inline long _pTotaldegree(poly p, ring r) |
---|
251 | { |
---|
252 | p_LmCheckPolyRing(p, r); |
---|
253 | return (long) p_ExpVectorQuerSum(p, r); |
---|
254 | } |
---|
255 | long pTotaldegree(poly p, ring r) |
---|
256 | { |
---|
257 | return (long) _pTotaldegree(p, r); |
---|
258 | } |
---|
259 | |
---|
260 | // pWTotalDegree for weighted orderings |
---|
261 | // whose first block covers all variables |
---|
262 | inline long _pWFirstTotalDegree(poly p, ring r) |
---|
263 | { |
---|
264 | int i; |
---|
265 | long sum = 0; |
---|
266 | |
---|
267 | for (i=1; i<= r->firstBlockEnds; i++) |
---|
268 | { |
---|
269 | sum += p_GetExp(p, i, r)*r->firstwv[i-1]; |
---|
270 | } |
---|
271 | return sum; |
---|
272 | } |
---|
273 | |
---|
274 | long pWFirstTotalDegree(poly p, ring r) |
---|
275 | { |
---|
276 | return (long) _pWFirstTotalDegree(p, r); |
---|
277 | } |
---|
278 | |
---|
279 | /*2 |
---|
280 | * compute the degree of the leading monomial of p |
---|
281 | * with respect to weigths from the ordering |
---|
282 | * the ordering is not compatible with degree so do not use p->Order |
---|
283 | */ |
---|
284 | long pWTotaldegree(poly p, ring r) |
---|
285 | { |
---|
286 | p_LmCheckPolyRing(p, r); |
---|
287 | int i, k; |
---|
288 | long j =0; |
---|
289 | int factor; |
---|
290 | |
---|
291 | // iterate through each block: |
---|
292 | for (i=0;r->order[i]!=0;i++) |
---|
293 | { |
---|
294 | factor=1; |
---|
295 | switch(r->order[i]) |
---|
296 | { |
---|
297 | case ringorder_M: |
---|
298 | factor=r->OrdSgn; |
---|
299 | case ringorder_wp: |
---|
300 | case ringorder_ws: |
---|
301 | case ringorder_Wp: |
---|
302 | case ringorder_Ws: |
---|
303 | for (k=r->block0[i];k<=r->block1[i];k++) |
---|
304 | { // in jedem block: |
---|
305 | j+= p_GetExp(p,k,r)*r->wvhdl[i][k - r->block0[i]]*factor; |
---|
306 | } |
---|
307 | break; |
---|
308 | case ringorder_lp: |
---|
309 | case ringorder_ls: |
---|
310 | case ringorder_dp: |
---|
311 | case ringorder_ds: |
---|
312 | case ringorder_Dp: |
---|
313 | case ringorder_Ds: |
---|
314 | case ringorder_rp: |
---|
315 | for (k=r->block0[i];k<=r->block1[i];k++) |
---|
316 | { |
---|
317 | j+= p_GetExp(p,k,r); |
---|
318 | } |
---|
319 | break; |
---|
320 | case ringorder_a64: |
---|
321 | { |
---|
322 | int64* w=(int64*)r->wvhdl[i]; |
---|
323 | for (k=0;k<=(r->block1[i] - r->block0[i]);k++) |
---|
324 | { |
---|
325 | //there should be added a line which checks if w[k]>2^31 |
---|
326 | j+= p_GetExp(p,k+1, r)*(long)w[k]; |
---|
327 | } |
---|
328 | //break; |
---|
329 | return j; |
---|
330 | } |
---|
331 | case ringorder_c: |
---|
332 | case ringorder_C: |
---|
333 | case ringorder_S: |
---|
334 | case ringorder_s: |
---|
335 | case ringorder_aa: |
---|
336 | break; |
---|
337 | case ringorder_a: |
---|
338 | for (k=r->block0[i];k<=r->block1[i];k++) |
---|
339 | { // only one line |
---|
340 | j+= p_GetExp(p,k, r)*r->wvhdl[i][ k- r->block0[i]]; |
---|
341 | } |
---|
342 | //break; |
---|
343 | return j; |
---|
344 | |
---|
345 | #ifndef NDEBUG |
---|
346 | default: |
---|
347 | Print("missing order %d in pWTotaldegree\n",r->order[i]); |
---|
348 | break; |
---|
349 | #endif |
---|
350 | } |
---|
351 | } |
---|
352 | return j; |
---|
353 | } |
---|
354 | |
---|
355 | int pWeight(int i, const ring r) |
---|
356 | { |
---|
357 | if ((r->firstwv==NULL) || (i>r->firstBlockEnds)) |
---|
358 | { |
---|
359 | return 1; |
---|
360 | } |
---|
361 | return r->firstwv[i-1]; |
---|
362 | } |
---|
363 | |
---|
364 | long pWDegree(poly p, ring r) |
---|
365 | { |
---|
366 | if (r->firstwv==NULL) return pTotaldegree(p, r); |
---|
367 | p_LmCheckPolyRing(p, r); |
---|
368 | int i, k; |
---|
369 | long j =0; |
---|
370 | |
---|
371 | for(i=1;i<=r->firstBlockEnds;i++) |
---|
372 | j+=p_GetExp(p, i, r)*r->firstwv[i-1]; |
---|
373 | |
---|
374 | for (;i<=r->N;i++) |
---|
375 | j+=p_GetExp(p,i, r)*pWeight(i, r); |
---|
376 | |
---|
377 | return j; |
---|
378 | } |
---|
379 | |
---|
380 | |
---|
381 | /* ---------------------------------------------------------------------*/ |
---|
382 | /* several possibilities for pLDeg: the maximal degree of a monomial in p*/ |
---|
383 | /* compute in l also the pLength of p */ |
---|
384 | |
---|
385 | /*2 |
---|
386 | * compute the length of a polynomial (in l) |
---|
387 | * and the degree of the monomial with maximal degree: the last one |
---|
388 | */ |
---|
389 | long pLDeg0(poly p,int *l, ring r) |
---|
390 | { |
---|
391 | p_CheckPolyRing(p, r); |
---|
392 | Exponent_t k= p_GetComp(p, r); |
---|
393 | int ll=1; |
---|
394 | |
---|
395 | if (k > 0) |
---|
396 | { |
---|
397 | while ((pNext(p)!=NULL) && (p_GetComp(pNext(p), r)==k)) |
---|
398 | { |
---|
399 | pIter(p); |
---|
400 | ll++; |
---|
401 | } |
---|
402 | } |
---|
403 | else |
---|
404 | { |
---|
405 | while (pNext(p)!=NULL) |
---|
406 | { |
---|
407 | pIter(p); |
---|
408 | ll++; |
---|
409 | } |
---|
410 | } |
---|
411 | *l=ll; |
---|
412 | return r->pFDeg(p, r); |
---|
413 | } |
---|
414 | |
---|
415 | /*2 |
---|
416 | * compute the length of a polynomial (in l) |
---|
417 | * and the degree of the monomial with maximal degree: the last one |
---|
418 | * but search in all components before syzcomp |
---|
419 | */ |
---|
420 | long pLDeg0c(poly p,int *l, ring r) |
---|
421 | { |
---|
422 | assume(p!=NULL); |
---|
423 | #ifdef PDEBUG |
---|
424 | _p_Test(p,r,PDEBUG); |
---|
425 | #endif |
---|
426 | p_CheckPolyRing(p, r); |
---|
427 | long o; |
---|
428 | int ll=1; |
---|
429 | |
---|
430 | if (! rIsSyzIndexRing(r)) |
---|
431 | { |
---|
432 | while (pNext(p) != NULL) |
---|
433 | { |
---|
434 | pIter(p); |
---|
435 | ll++; |
---|
436 | } |
---|
437 | o = r->pFDeg(p, r); |
---|
438 | } |
---|
439 | else |
---|
440 | { |
---|
441 | int curr_limit = rGetCurrSyzLimit(r); |
---|
442 | poly pp = p; |
---|
443 | while ((p=pNext(p))!=NULL) |
---|
444 | { |
---|
445 | if (p_GetComp(p, r)<=curr_limit/*syzComp*/) |
---|
446 | ll++; |
---|
447 | else break; |
---|
448 | pp = p; |
---|
449 | } |
---|
450 | #ifdef PDEBUG |
---|
451 | _p_Test(pp,r,PDEBUG); |
---|
452 | #endif |
---|
453 | o = r->pFDeg(pp, r); |
---|
454 | } |
---|
455 | *l=ll; |
---|
456 | return o; |
---|
457 | } |
---|
458 | |
---|
459 | /*2 |
---|
460 | * compute the length of a polynomial (in l) |
---|
461 | * and the degree of the monomial with maximal degree: the first one |
---|
462 | * this works for the polynomial case with degree orderings |
---|
463 | * (both c,dp and dp,c) |
---|
464 | */ |
---|
465 | long pLDegb(poly p,int *l, ring r) |
---|
466 | { |
---|
467 | p_CheckPolyRing(p, r); |
---|
468 | Exponent_t k= p_GetComp(p, r); |
---|
469 | long o = r->pFDeg(p, r); |
---|
470 | int ll=1; |
---|
471 | |
---|
472 | if (k != 0) |
---|
473 | { |
---|
474 | while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k)) |
---|
475 | { |
---|
476 | ll++; |
---|
477 | } |
---|
478 | } |
---|
479 | else |
---|
480 | { |
---|
481 | while ((p=pNext(p)) !=NULL) |
---|
482 | { |
---|
483 | ll++; |
---|
484 | } |
---|
485 | } |
---|
486 | *l=ll; |
---|
487 | return o; |
---|
488 | } |
---|
489 | |
---|
490 | /*2 |
---|
491 | * compute the length of a polynomial (in l) |
---|
492 | * and the degree of the monomial with maximal degree: |
---|
493 | * this is NOT the last one, we have to look for it |
---|
494 | */ |
---|
495 | long pLDeg1(poly p,int *l, ring r) |
---|
496 | { |
---|
497 | p_CheckPolyRing(p, r); |
---|
498 | Exponent_t k= p_GetComp(p, r); |
---|
499 | int ll=1; |
---|
500 | long t,max; |
---|
501 | |
---|
502 | max=r->pFDeg(p, r); |
---|
503 | if (k > 0) |
---|
504 | { |
---|
505 | while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k)) |
---|
506 | { |
---|
507 | t=r->pFDeg(p, r); |
---|
508 | if (t>max) max=t; |
---|
509 | ll++; |
---|
510 | } |
---|
511 | } |
---|
512 | else |
---|
513 | { |
---|
514 | while ((p=pNext(p))!=NULL) |
---|
515 | { |
---|
516 | t=r->pFDeg(p, r); |
---|
517 | if (t>max) max=t; |
---|
518 | ll++; |
---|
519 | } |
---|
520 | } |
---|
521 | *l=ll; |
---|
522 | return max; |
---|
523 | } |
---|
524 | |
---|
525 | /*2 |
---|
526 | * compute the length of a polynomial (in l) |
---|
527 | * and the degree of the monomial with maximal degree: |
---|
528 | * this is NOT the last one, we have to look for it |
---|
529 | * in all components |
---|
530 | */ |
---|
531 | long pLDeg1c(poly p,int *l, ring r) |
---|
532 | { |
---|
533 | p_CheckPolyRing(p, r); |
---|
534 | int ll=1; |
---|
535 | long t,max; |
---|
536 | |
---|
537 | max=r->pFDeg(p, r); |
---|
538 | if (rIsSyzIndexRing(r)) |
---|
539 | { |
---|
540 | long limit = rGetCurrSyzLimit(r); |
---|
541 | while ((p=pNext(p))!=NULL) |
---|
542 | { |
---|
543 | if (p_GetComp(p, r)<=limit) |
---|
544 | { |
---|
545 | if ((t=r->pFDeg(p, r))>max) max=t; |
---|
546 | ll++; |
---|
547 | } |
---|
548 | else break; |
---|
549 | } |
---|
550 | } |
---|
551 | else |
---|
552 | { |
---|
553 | while ((p=pNext(p))!=NULL) |
---|
554 | { |
---|
555 | if ((t=r->pFDeg(p, r))>max) max=t; |
---|
556 | ll++; |
---|
557 | } |
---|
558 | } |
---|
559 | *l=ll; |
---|
560 | return max; |
---|
561 | } |
---|
562 | |
---|
563 | // like pLDeg1, only pFDeg == pDeg |
---|
564 | long pLDeg1_Deg(poly p,int *l, ring r) |
---|
565 | { |
---|
566 | assume(r->pFDeg == pDeg); |
---|
567 | p_CheckPolyRing(p, r); |
---|
568 | Exponent_t k= p_GetComp(p, r); |
---|
569 | int ll=1; |
---|
570 | long t,max; |
---|
571 | |
---|
572 | max=_pDeg(p, r); |
---|
573 | if (k > 0) |
---|
574 | { |
---|
575 | while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k)) |
---|
576 | { |
---|
577 | t=_pDeg(p, r); |
---|
578 | if (t>max) max=t; |
---|
579 | ll++; |
---|
580 | } |
---|
581 | } |
---|
582 | else |
---|
583 | { |
---|
584 | while ((p=pNext(p))!=NULL) |
---|
585 | { |
---|
586 | t=_pDeg(p, r); |
---|
587 | if (t>max) max=t; |
---|
588 | ll++; |
---|
589 | } |
---|
590 | } |
---|
591 | *l=ll; |
---|
592 | return max; |
---|
593 | } |
---|
594 | |
---|
595 | long pLDeg1c_Deg(poly p,int *l, ring r) |
---|
596 | { |
---|
597 | assume(r->pFDeg == pDeg); |
---|
598 | p_CheckPolyRing(p, r); |
---|
599 | int ll=1; |
---|
600 | long t,max; |
---|
601 | |
---|
602 | max=_pDeg(p, r); |
---|
603 | if (rIsSyzIndexRing(r)) |
---|
604 | { |
---|
605 | long limit = rGetCurrSyzLimit(r); |
---|
606 | while ((p=pNext(p))!=NULL) |
---|
607 | { |
---|
608 | if (p_GetComp(p, r)<=limit) |
---|
609 | { |
---|
610 | if ((t=_pDeg(p, r))>max) max=t; |
---|
611 | ll++; |
---|
612 | } |
---|
613 | else break; |
---|
614 | } |
---|
615 | } |
---|
616 | else |
---|
617 | { |
---|
618 | while ((p=pNext(p))!=NULL) |
---|
619 | { |
---|
620 | if ((t=_pDeg(p, r))>max) max=t; |
---|
621 | ll++; |
---|
622 | } |
---|
623 | } |
---|
624 | *l=ll; |
---|
625 | return max; |
---|
626 | } |
---|
627 | |
---|
628 | // like pLDeg1, only pFDeg == pTotoalDegree |
---|
629 | long pLDeg1_Totaldegree(poly p,int *l, ring r) |
---|
630 | { |
---|
631 | p_CheckPolyRing(p, r); |
---|
632 | Exponent_t k= p_GetComp(p, r); |
---|
633 | int ll=1; |
---|
634 | long t,max; |
---|
635 | |
---|
636 | max=_pTotaldegree(p, r); |
---|
637 | if (k > 0) |
---|
638 | { |
---|
639 | while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k)) |
---|
640 | { |
---|
641 | t=_pTotaldegree(p, r); |
---|
642 | if (t>max) max=t; |
---|
643 | ll++; |
---|
644 | } |
---|
645 | } |
---|
646 | else |
---|
647 | { |
---|
648 | while ((p=pNext(p))!=NULL) |
---|
649 | { |
---|
650 | t=_pTotaldegree(p, r); |
---|
651 | if (t>max) max=t; |
---|
652 | ll++; |
---|
653 | } |
---|
654 | } |
---|
655 | *l=ll; |
---|
656 | return max; |
---|
657 | } |
---|
658 | |
---|
659 | long pLDeg1c_Totaldegree(poly p,int *l, ring r) |
---|
660 | { |
---|
661 | p_CheckPolyRing(p, r); |
---|
662 | int ll=1; |
---|
663 | long t,max; |
---|
664 | |
---|
665 | max=_pTotaldegree(p, r); |
---|
666 | if (rIsSyzIndexRing(r)) |
---|
667 | { |
---|
668 | long limit = rGetCurrSyzLimit(r); |
---|
669 | while ((p=pNext(p))!=NULL) |
---|
670 | { |
---|
671 | if (p_GetComp(p, r)<=limit) |
---|
672 | { |
---|
673 | if ((t=_pTotaldegree(p, r))>max) max=t; |
---|
674 | ll++; |
---|
675 | } |
---|
676 | else break; |
---|
677 | } |
---|
678 | } |
---|
679 | else |
---|
680 | { |
---|
681 | while ((p=pNext(p))!=NULL) |
---|
682 | { |
---|
683 | if ((t=_pTotaldegree(p, r))>max) max=t; |
---|
684 | ll++; |
---|
685 | } |
---|
686 | } |
---|
687 | *l=ll; |
---|
688 | return max; |
---|
689 | } |
---|
690 | |
---|
691 | // like pLDeg1, only pFDeg == pWFirstTotalDegree |
---|
692 | long pLDeg1_WFirstTotalDegree(poly p,int *l, ring r) |
---|
693 | { |
---|
694 | p_CheckPolyRing(p, r); |
---|
695 | Exponent_t k= p_GetComp(p, r); |
---|
696 | int ll=1; |
---|
697 | long t,max; |
---|
698 | |
---|
699 | max=_pWFirstTotalDegree(p, r); |
---|
700 | if (k > 0) |
---|
701 | { |
---|
702 | while (((p=pNext(p))!=NULL) && (p_GetComp(p, r)==k)) |
---|
703 | { |
---|
704 | t=_pWFirstTotalDegree(p, r); |
---|
705 | if (t>max) max=t; |
---|
706 | ll++; |
---|
707 | } |
---|
708 | } |
---|
709 | else |
---|
710 | { |
---|
711 | while ((p=pNext(p))!=NULL) |
---|
712 | { |
---|
713 | t=_pWFirstTotalDegree(p, r); |
---|
714 | if (t>max) max=t; |
---|
715 | ll++; |
---|
716 | } |
---|
717 | } |
---|
718 | *l=ll; |
---|
719 | return max; |
---|
720 | } |
---|
721 | |
---|
722 | long pLDeg1c_WFirstTotalDegree(poly p,int *l, ring r) |
---|
723 | { |
---|
724 | p_CheckPolyRing(p, r); |
---|
725 | int ll=1; |
---|
726 | long t,max; |
---|
727 | |
---|
728 | max=_pWFirstTotalDegree(p, r); |
---|
729 | if (rIsSyzIndexRing(r)) |
---|
730 | { |
---|
731 | long limit = rGetCurrSyzLimit(r); |
---|
732 | while ((p=pNext(p))!=NULL) |
---|
733 | { |
---|
734 | if (p_GetComp(p, r)<=limit) |
---|
735 | { |
---|
736 | if ((t=_pTotaldegree(p, r))>max) max=t; |
---|
737 | ll++; |
---|
738 | } |
---|
739 | else break; |
---|
740 | } |
---|
741 | } |
---|
742 | else |
---|
743 | { |
---|
744 | while ((p=pNext(p))!=NULL) |
---|
745 | { |
---|
746 | if ((t=_pTotaldegree(p, r))>max) max=t; |
---|
747 | ll++; |
---|
748 | } |
---|
749 | } |
---|
750 | *l=ll; |
---|
751 | return max; |
---|
752 | } |
---|
753 | |
---|
754 | /*************************************************************** |
---|
755 | * |
---|
756 | * Maximal Exponent business |
---|
757 | * |
---|
758 | ***************************************************************/ |
---|
759 | |
---|
760 | static inline unsigned long |
---|
761 | p_GetMaxExpL2(unsigned long l1, unsigned long l2, ring r, |
---|
762 | unsigned long number_of_exp) |
---|
763 | { |
---|
764 | const unsigned long bitmask = r->bitmask; |
---|
765 | unsigned long ml1 = l1 & bitmask; |
---|
766 | unsigned long ml2 = l2 & bitmask; |
---|
767 | unsigned long max = (ml1 > ml2 ? ml1 : ml2); |
---|
768 | unsigned long j = number_of_exp - 1; |
---|
769 | |
---|
770 | if (j > 0) |
---|
771 | { |
---|
772 | unsigned long mask = bitmask << r->BitsPerExp; |
---|
773 | while (1) |
---|
774 | { |
---|
775 | ml1 = l1 & mask; |
---|
776 | ml2 = l2 & mask; |
---|
777 | max |= ((ml1 > ml2 ? ml1 : ml2) & mask); |
---|
778 | j--; |
---|
779 | if (j == 0) break; |
---|
780 | mask = mask << r->BitsPerExp; |
---|
781 | } |
---|
782 | } |
---|
783 | return max; |
---|
784 | } |
---|
785 | |
---|
786 | static inline unsigned long |
---|
787 | p_GetMaxExpL2(unsigned long l1, unsigned long l2, ring r) |
---|
788 | { |
---|
789 | return p_GetMaxExpL2(l1, l2, r, r->ExpPerLong); |
---|
790 | } |
---|
791 | |
---|
792 | poly p_GetMaxExpP(poly p, ring r) |
---|
793 | { |
---|
794 | p_CheckPolyRing(p, r); |
---|
795 | if (p == NULL) return p_Init(r); |
---|
796 | poly max = p_LmInit(p, r); |
---|
797 | pIter(p); |
---|
798 | if (p == NULL) return max; |
---|
799 | int i, offset; |
---|
800 | unsigned long l_p, l_max; |
---|
801 | unsigned long divmask = r->divmask; |
---|
802 | |
---|
803 | do |
---|
804 | { |
---|
805 | offset = r->VarL_Offset[0]; |
---|
806 | l_p = p->exp[offset]; |
---|
807 | l_max = max->exp[offset]; |
---|
808 | // do the divisibility trick to find out whether l has an exponent |
---|
809 | if (l_p > l_max || |
---|
810 | (((l_max & divmask) ^ (l_p & divmask)) != ((l_max-l_p) & divmask))) |
---|
811 | max->exp[offset] = p_GetMaxExpL2(l_max, l_p, r); |
---|
812 | |
---|
813 | for (i=1; i<r->VarL_Size; i++) |
---|
814 | { |
---|
815 | offset = r->VarL_Offset[i]; |
---|
816 | l_p = p->exp[offset]; |
---|
817 | l_max = max->exp[offset]; |
---|
818 | // do the divisibility trick to find out whether l has an exponent |
---|
819 | if (l_p > l_max || |
---|
820 | (((l_max & divmask) ^ (l_p & divmask)) != ((l_max-l_p) & divmask))) |
---|
821 | max->exp[offset] = p_GetMaxExpL2(l_max, l_p, r); |
---|
822 | } |
---|
823 | pIter(p); |
---|
824 | } |
---|
825 | while (p != NULL); |
---|
826 | return max; |
---|
827 | } |
---|
828 | |
---|
829 | unsigned long p_GetMaxExpL(poly p, ring r, unsigned long l_max) |
---|
830 | { |
---|
831 | unsigned long l_p, divmask = r->divmask; |
---|
832 | int i; |
---|
833 | |
---|
834 | while (p != NULL) |
---|
835 | { |
---|
836 | l_p = p->exp[r->VarL_Offset[0]]; |
---|
837 | if (l_p > l_max || |
---|
838 | (((l_max & divmask) ^ (l_p & divmask)) != ((l_max-l_p) & divmask))) |
---|
839 | l_max = p_GetMaxExpL2(l_max, l_p, r); |
---|
840 | for (i=1; i<r->VarL_Size; i++) |
---|
841 | { |
---|
842 | l_p = p->exp[r->VarL_Offset[i]]; |
---|
843 | // do the divisibility trick to find out whether l has an exponent |
---|
844 | if (l_p > l_max || |
---|
845 | (((l_max & divmask) ^ (l_p & divmask)) != ((l_max-l_p) & divmask))) |
---|
846 | l_max = p_GetMaxExpL2(l_max, l_p, r); |
---|
847 | } |
---|
848 | pIter(p); |
---|
849 | } |
---|
850 | return l_max; |
---|
851 | } |
---|
852 | |
---|
853 | |
---|
854 | |
---|
855 | |
---|
856 | /*************************************************************** |
---|
857 | * |
---|
858 | * Misc things |
---|
859 | * |
---|
860 | ***************************************************************/ |
---|
861 | // returns TRUE, if all monoms have the same component |
---|
862 | BOOLEAN p_OneComp(poly p, ring r) |
---|
863 | { |
---|
864 | if(p!=NULL) |
---|
865 | { |
---|
866 | long i = p_GetComp(p, r); |
---|
867 | while (pNext(p)!=NULL) |
---|
868 | { |
---|
869 | pIter(p); |
---|
870 | if(i != p_GetComp(p, r)) return FALSE; |
---|
871 | } |
---|
872 | } |
---|
873 | return TRUE; |
---|
874 | } |
---|
875 | |
---|
876 | /*2 |
---|
877 | *test if a monomial /head term is a pure power |
---|
878 | */ |
---|
879 | int p_IsPurePower(const poly p, const ring r) |
---|
880 | { |
---|
881 | int i,k=0; |
---|
882 | |
---|
883 | for (i=r->N;i;i--) |
---|
884 | { |
---|
885 | if (p_GetExp(p,i, r)!=0) |
---|
886 | { |
---|
887 | if(k!=0) return 0; |
---|
888 | k=i; |
---|
889 | } |
---|
890 | } |
---|
891 | return k; |
---|
892 | } |
---|
893 | |
---|
894 | /*2 |
---|
895 | * returns a polynomial representing the integer i |
---|
896 | */ |
---|
897 | poly p_ISet(int i, ring r) |
---|
898 | { |
---|
899 | poly rc = NULL; |
---|
900 | if (i!=0) |
---|
901 | { |
---|
902 | rc = p_Init(r); |
---|
903 | pSetCoeff0(rc,r->cf->nInit(i)); |
---|
904 | if (r->cf->nIsZero(p_GetCoeff(rc,r))) |
---|
905 | p_DeleteLm(&rc,r); |
---|
906 | } |
---|
907 | return rc; |
---|
908 | } |
---|
909 | |
---|
910 | /*2 |
---|
911 | * returns a polynomial representing the number n |
---|
912 | * destroys n |
---|
913 | */ |
---|
914 | poly p_NSet(number n, ring r) |
---|
915 | { |
---|
916 | if (r->cf->nIsZero(n)) |
---|
917 | { |
---|
918 | r->cf->cfDelete(&n, r); |
---|
919 | return NULL; |
---|
920 | } |
---|
921 | else |
---|
922 | { |
---|
923 | poly rc = p_Init(r); |
---|
924 | pSetCoeff0(rc,n); |
---|
925 | return rc; |
---|
926 | } |
---|
927 | } |
---|
928 | |
---|
929 | /*************************************************************** |
---|
930 | * |
---|
931 | * p_ShallowDelete |
---|
932 | * |
---|
933 | ***************************************************************/ |
---|
934 | #undef LINKAGE |
---|
935 | #define LINKAGE |
---|
936 | #undef p_Delete |
---|
937 | #define p_Delete p_ShallowDelete |
---|
938 | #undef n_Delete |
---|
939 | #define n_Delete(n, r) ((void)0) |
---|
940 | |
---|
941 | #include "p_Delete__T.cc" |
---|
942 | |
---|