1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: polys.cc,v 1.19 1998-03-23 22:51:04 obachman Exp $ */ |
---|
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 "mod2.h" |
---|
15 | #include "tok.h" |
---|
16 | #include "mmemory.h" |
---|
17 | #include "febase.h" |
---|
18 | #include "numbers.h" |
---|
19 | #include "polys.h" |
---|
20 | #include "ring.h" |
---|
21 | #include "binom.h" |
---|
22 | #include "ipid.h" |
---|
23 | #include "polys-comp.h" |
---|
24 | |
---|
25 | /* ----------- global variables, set by pChangeRing --------------------- */ |
---|
26 | /* initializes the internal data from the exp vector */ |
---|
27 | pSetmProc pSetm; |
---|
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 | pCompProc pComp0; |
---|
35 | |
---|
36 | int pVariables; // number of variables |
---|
37 | int pVariablesW; // number of words of pVariables exponents |
---|
38 | int pVariables1W; // number of words of (pVariables+1) exponents |
---|
39 | int pMonomSize; // size of monom (in bytes) |
---|
40 | int pMonomSizeW; // size of monom (in words) |
---|
41 | int pLexSgn; // 1, for lex monom comps; -1 otherwise (exception: ls) |
---|
42 | int pVarOffset; // controls the way exponents are stored in a vector |
---|
43 | int pVarLowIndex; // lowest exponent index |
---|
44 | int pVarHighIndex; // highest exponent index |
---|
45 | int pVarCompIndex; // Location of component in exponent vector |
---|
46 | |
---|
47 | /* 1 for polynomial ring, -1 otherwise */ |
---|
48 | int pOrdSgn; |
---|
49 | /* TRUE for momomial output as x2y, FALSE for x^2*y */ |
---|
50 | int pShortOut = (int)TRUE; |
---|
51 | // it is of type int, not BOOLEAN because it is also in ip |
---|
52 | /* TRUE if the monomial ordering is not compatible with pFDeg */ |
---|
53 | BOOLEAN pLexOrder; |
---|
54 | /* TRUE if the monomial ordering has polynomial and power series blocks */ |
---|
55 | BOOLEAN pMixedOrder; |
---|
56 | /* 1 for c ordering, -1 otherwise (i.e. for C ordering) */ |
---|
57 | int pComponentOrder; |
---|
58 | |
---|
59 | #ifdef DRING |
---|
60 | int p2; |
---|
61 | BOOLEAN pDRING=FALSE; |
---|
62 | #endif |
---|
63 | |
---|
64 | #ifdef SRING |
---|
65 | int pAltVars; |
---|
66 | BOOLEAN pSRING=FALSE; |
---|
67 | #endif |
---|
68 | |
---|
69 | #ifdef SDRING |
---|
70 | BOOLEAN pSDRING=FALSE; |
---|
71 | #include "polys.inc" |
---|
72 | #endif |
---|
73 | |
---|
74 | /* ----------- global variables, set by procedures from hecke/kstd1 ----- */ |
---|
75 | /* the highest monomial below pHEdge */ |
---|
76 | poly ppNoether = NULL; |
---|
77 | |
---|
78 | /* -------------- static variables --------------------------------------- */ |
---|
79 | /*is the basic comparing procedure during a computation of syzygies*/ |
---|
80 | static pCompProc pCompOld; |
---|
81 | /*for grouping module indicies during computations*/ |
---|
82 | int pMaxBound = 0; |
---|
83 | |
---|
84 | /*contains the headterms for the Schreyer orderings*/ |
---|
85 | static int* SchreyerOrd; |
---|
86 | static int maxSchreyer=0; |
---|
87 | static int indexShift=0; |
---|
88 | static pLDegProc pLDegOld; |
---|
89 | |
---|
90 | typedef int (*bcompProc)(poly p1, poly p2, int i1, int i2, short * w); |
---|
91 | static bcompProc bcomph[20]; |
---|
92 | static short** polys_wv; |
---|
93 | static short * firstwv; |
---|
94 | static int * block0; |
---|
95 | static int * block1; |
---|
96 | static int firstBlockEnds; |
---|
97 | static int * order; |
---|
98 | |
---|
99 | /*0 implementation*/ |
---|
100 | /*-------- the several possibilities for pSetm:-----------------------*/ |
---|
101 | |
---|
102 | /* Remark: These could be made more efficient by avoiding using pGetExp */ |
---|
103 | |
---|
104 | /*2 |
---|
105 | * define the order of p with respect to lex. ordering, N=1 |
---|
106 | */ |
---|
107 | static void setlex1(poly p) |
---|
108 | { |
---|
109 | p->Order = (Order_t)pGetExp(p,1); |
---|
110 | } |
---|
111 | |
---|
112 | /*2 |
---|
113 | * define the order of p with respect to lex. ordering, N>1 |
---|
114 | */ |
---|
115 | static void setlex2(poly p) |
---|
116 | { |
---|
117 | p->Order = (((Order_t)pGetExp(p,1))<<(sizeof(Exponent_t)*8)) |
---|
118 | + (Order_t)pGetExp(p,2); |
---|
119 | } |
---|
120 | |
---|
121 | /*2 |
---|
122 | * define the order of p with respect to a degree ordering |
---|
123 | */ |
---|
124 | static void setdeg1(poly p) |
---|
125 | { |
---|
126 | p->Order = pExpQuerSum1(p, firstBlockEnds); |
---|
127 | } |
---|
128 | |
---|
129 | /*2 |
---|
130 | * define the order of p with respect to a degree ordering |
---|
131 | * with weigthts |
---|
132 | */ |
---|
133 | static void setdeg1w(poly p) |
---|
134 | { |
---|
135 | Order_t i, j = 0; |
---|
136 | |
---|
137 | for (i = firstBlockEnds; i>0; i--) |
---|
138 | { |
---|
139 | j += ((Order_t) pGetExp(p,i))*firstwv[i-1]; |
---|
140 | } |
---|
141 | p->Order = j; |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | /*-------- IMPLEMENTATION OF MONOMIAL COMPARISONS ---------------------*/ |
---|
146 | |
---|
147 | |
---|
148 | #define NonZeroR(l, actionG, actionS) \ |
---|
149 | do \ |
---|
150 | { \ |
---|
151 | long _l = l; \ |
---|
152 | if (_l) \ |
---|
153 | { \ |
---|
154 | if (_l > 0) actionG; \ |
---|
155 | actionS; \ |
---|
156 | } \ |
---|
157 | } \ |
---|
158 | while(0) |
---|
159 | |
---|
160 | #define Mreturn(d, multiplier) \ |
---|
161 | { \ |
---|
162 | if (d > 0) return multiplier; \ |
---|
163 | return -multiplier; \ |
---|
164 | } |
---|
165 | |
---|
166 | static int pComp_otEXP_nwONE(poly p1, poly p2); |
---|
167 | static int pComp_otCOMPEXP_nwONE(poly p1, poly p2); |
---|
168 | static int pComp_otEXPCOMP_nwONE(poly p1, poly p2); |
---|
169 | static int pComp_otEXP_nwTWO(poly p1, poly p2); |
---|
170 | static int pComp_otCOMPEXP_nwTWO(poly p1, poly p2); |
---|
171 | static int pComp_otEXPCOMP_nwTWO(poly p1, poly p2); |
---|
172 | static int pComp_otEXP_nwEVEN(poly p1, poly p2); |
---|
173 | static int pComp_otCOMPEXP_nwEVEN(poly p1, poly p2); |
---|
174 | static int pComp_otEXPCOMP_nwEVEN(poly p1, poly p2); |
---|
175 | static int pComp_otEXP_nwODD(poly p1, poly p2); |
---|
176 | static int pComp_otCOMPEXP_nwODD(poly p1, poly p2); |
---|
177 | static int pComp_otEXPCOMP_nwODD(poly p1, poly p2); |
---|
178 | |
---|
179 | |
---|
180 | // comp_nwONE is used if pVariables1W == 1 and component is compatible |
---|
181 | // with ordering |
---|
182 | static int pComp_otEXP_nwONE(poly p1, poly p2) |
---|
183 | { |
---|
184 | register long d = pGetOrder(p1) - pGetOrder(p2); |
---|
185 | |
---|
186 | if (d) Mreturn(d, pOrdSgn); |
---|
187 | _pMonComp_otEXP_nwONE(p1, p2, d, goto NotEqual, return 0); |
---|
188 | |
---|
189 | NotEqual: |
---|
190 | Mreturn(d, pLexSgn); |
---|
191 | } |
---|
192 | |
---|
193 | // comp_otEXPCOMP_nwONE : pVariables1W == 1, priority is |
---|
194 | // given to exponents, component is incompatible with ordering |
---|
195 | static int pComp_otEXPCOMP_nwONE(poly p1, poly p2) |
---|
196 | { |
---|
197 | register long d = pGetOrder(p1) - pGetOrder(p2); |
---|
198 | |
---|
199 | if (d) Mreturn(d, pOrdSgn); |
---|
200 | _pMonComp_otEXPCOMP_nwONE(p1, p2, d, goto NotEqual , return 0); |
---|
201 | |
---|
202 | NotEqual: |
---|
203 | Mreturn(d, pLexSgn) |
---|
204 | } |
---|
205 | |
---|
206 | // comp_otEXPCOMP_nwONE : pVariables1W == 1, priority is given to component, |
---|
207 | // component is incompatible with ordering |
---|
208 | static int pComp_otCOMPEXP_nwONE(poly p1, poly p2) |
---|
209 | { |
---|
210 | register long d = pGetComp(p2) - pGetComp(p1); |
---|
211 | if (d) Mreturn(d, pComponentOrder); |
---|
212 | d = pGetOrder(p1) - pGetOrder(p2); |
---|
213 | if (d) Mreturn(d, pOrdSgn); |
---|
214 | _pMonComp_otEXP_nwONE(p1, p2, d, goto NotEqual , return 0); |
---|
215 | |
---|
216 | NotEqual: |
---|
217 | Mreturn(d, pLexSgn); |
---|
218 | } |
---|
219 | |
---|
220 | // comp_nwTWO : pVariables1W == 2 and component is compatible with ordering |
---|
221 | static int pComp_otEXP_nwTWO(poly p1, poly p2) |
---|
222 | { |
---|
223 | register long d = pGetOrder(p1) - pGetOrder(p2); |
---|
224 | |
---|
225 | if (d) Mreturn(d, pOrdSgn); |
---|
226 | _pMonComp_otEXP_nwTWO(p1, p2, d, goto NotEqual , return 0); |
---|
227 | |
---|
228 | NotEqual: |
---|
229 | Mreturn(d, pLexSgn); |
---|
230 | } |
---|
231 | |
---|
232 | // comp_otEXPCOMP_nwTWO : pVariables1W == 2, priority is given to exponents, |
---|
233 | // component is incompatible with ordering |
---|
234 | static int pComp_otEXPCOMP_nwTWO(poly p1, poly p2) |
---|
235 | { |
---|
236 | register long d = pGetOrder(p1) - pGetOrder(p2); |
---|
237 | |
---|
238 | if (d) Mreturn(d, pOrdSgn); |
---|
239 | // _pMonComp_otEXPCOMP_nwTWO(p1, p2, d, goto NotEqual, return 0); |
---|
240 | |
---|
241 | do |
---|
242 | { |
---|
243 | const long* s1 = (long*) &(p1->exp[0]); |
---|
244 | const long* s2 = (long*) &(p2->exp[0]); |
---|
245 | d = *s1 - *s2; |
---|
246 | if (d) goto NotEqual; |
---|
247 | d = *(s1 + 1) - *(s2 + 1); |
---|
248 | if (d) |
---|
249 | { |
---|
250 | if (((long) (pGetComp(p1) - pGetComp(p2))) == d) |
---|
251 | d = -d; |
---|
252 | goto NotEqual; |
---|
253 | } |
---|
254 | return 0; |
---|
255 | } |
---|
256 | while(0); |
---|
257 | |
---|
258 | NotEqual: |
---|
259 | Mreturn(d, pLexSgn); |
---|
260 | } |
---|
261 | |
---|
262 | // comp_otEXPCOMP_nwTWO : pVariables1W == 2, priority is given to component, |
---|
263 | // component is incompatible with ordering |
---|
264 | static int pComp_otCOMPEXP_nwTWO(poly p1, poly p2) |
---|
265 | { |
---|
266 | register long d = pGetComp(p2) - pGetComp(p1); |
---|
267 | if (d) Mreturn(d, pComponentOrder); |
---|
268 | d = pGetOrder(p1) - pGetOrder(p2); |
---|
269 | if (d) Mreturn(d, pOrdSgn); |
---|
270 | _pMonComp_otEXP_nwTWO(p1, p2, d, goto NotEqual , return 0); |
---|
271 | |
---|
272 | NotEqual: |
---|
273 | Mreturn(d, pLexSgn); |
---|
274 | } |
---|
275 | |
---|
276 | // comp_nwEVEN : pVariables1W == 2*i and component is compatible |
---|
277 | // with ordering |
---|
278 | static int pComp_otEXP_nwEVEN(poly p1, poly p2) |
---|
279 | { |
---|
280 | register long d = pGetOrder(p1) - pGetOrder(p2); |
---|
281 | |
---|
282 | if (d) Mreturn(d, pOrdSgn); |
---|
283 | _pMonComp_otEXP_nwEVEN(p1, p2, pVariables1W, d, goto NotEqual , return 0); |
---|
284 | |
---|
285 | NotEqual: |
---|
286 | Mreturn(d, pLexSgn); |
---|
287 | } |
---|
288 | |
---|
289 | // comp_otEXPCOMP_nwEVEN : pVariables1W == 2*i, priority is given to exponents, |
---|
290 | // component is incompatible with ordering |
---|
291 | static int pComp_otEXPCOMP_nwEVEN(poly p1, poly p2) |
---|
292 | { |
---|
293 | register long d = pGetOrder(p1) - pGetOrder(p2); |
---|
294 | |
---|
295 | if (d) Mreturn(d, pOrdSgn); |
---|
296 | _pMonComp_otEXPCOMP_nwEVEN(p1, p2, pVariables1W, d, goto NotEqual , return 0); |
---|
297 | |
---|
298 | NotEqual: |
---|
299 | Mreturn(d, pLexSgn); |
---|
300 | } |
---|
301 | |
---|
302 | // comp_otEXPCOMP_nwEVEN : pVariables1W == 2*i, priority is given to component, |
---|
303 | // component is incompatible with ordering |
---|
304 | static int pComp_otCOMPEXP_nwEVEN(poly p1, poly p2) |
---|
305 | { |
---|
306 | register long d = pGetComp(p2) - pGetComp(p1); |
---|
307 | if (d) Mreturn(d, pComponentOrder); |
---|
308 | d = pGetOrder(p1) - pGetOrder(p2); |
---|
309 | if (d) Mreturn(d, pOrdSgn); |
---|
310 | _pMonComp_otEXP_nwEVEN(p1, p2, pVariablesW, d, goto NotEqual, return 0); |
---|
311 | |
---|
312 | NotEqual: |
---|
313 | Mreturn(d, pLexSgn); |
---|
314 | } |
---|
315 | |
---|
316 | // comp_nwODD : pVariables1W == 2*i and component is compatible |
---|
317 | // with ordering |
---|
318 | static int pComp_otEXP_nwODD(poly p1, poly p2) |
---|
319 | { |
---|
320 | register long d = pGetOrder(p1) - pGetOrder(p2); |
---|
321 | |
---|
322 | if (d) Mreturn(d, pOrdSgn); |
---|
323 | _pMonComp_otEXP_nwODD(p1, p2, pVariables1W, d, goto NotEqual, return 0); |
---|
324 | |
---|
325 | NotEqual: |
---|
326 | Mreturn(d, pLexSgn); |
---|
327 | } |
---|
328 | |
---|
329 | // comp_otEXPCOMP_nwODD : pVariables1W == 2*i, priority is given to exponents, |
---|
330 | // component is incompatible with ordering |
---|
331 | static int pComp_otEXPCOMP_nwODD(poly p1, poly p2) |
---|
332 | { |
---|
333 | register long d = pGetOrder(p1) - pGetOrder(p2); |
---|
334 | if (d) |
---|
335 | { |
---|
336 | if (d > 0) return pOrdSgn; |
---|
337 | return -pOrdSgn; |
---|
338 | } |
---|
339 | |
---|
340 | _pMonComp_otEXPCOMP_nwODD(p1, p2, pVariables1W, d, goto NotEqual , return 0); |
---|
341 | |
---|
342 | NotEqual: |
---|
343 | Mreturn(d, pLexSgn); |
---|
344 | } |
---|
345 | |
---|
346 | // comp_otCOMPEXP_nwODD : pVariables1W == 2*i, priority is given to component, |
---|
347 | // component is incompatible with ordering |
---|
348 | static int pComp_otCOMPEXP_nwODD(poly p1, poly p2) |
---|
349 | { |
---|
350 | register long d = pGetComp(p2) - pGetComp(p1); |
---|
351 | if (d) Mreturn(d, pComponentOrder); |
---|
352 | d = pGetOrder(p1) - pGetOrder(p2); |
---|
353 | if (d) Mreturn(d, pOrdSgn); |
---|
354 | _pMonComp_otEXP_nwODD(p1, p2, pVariablesW, d, goto NotEqual, return 0); |
---|
355 | |
---|
356 | NotEqual: |
---|
357 | Mreturn(d, pLexSgn); |
---|
358 | } |
---|
359 | /*2 |
---|
360 | * compare the head monomial of p1 and p2 with weight vector |
---|
361 | */ |
---|
362 | static int comp1a ( poly p1, poly p2, int f, int l, short * w ) |
---|
363 | { |
---|
364 | int d= pGetOrder(p1) - pGetOrder(p2); |
---|
365 | if ( d > 0 /*p1->Order > p2->Order*/ ) |
---|
366 | return 1; |
---|
367 | else if ( d < 0 /*p1->Order < p2->Order*/ ) |
---|
368 | return -1; |
---|
369 | return 0; |
---|
370 | } |
---|
371 | |
---|
372 | |
---|
373 | /*---------------------------------------------------*/ |
---|
374 | |
---|
375 | /* These functions could be made faster if you use pointers to the |
---|
376 | * exponent vectors and pointer arithmetic instead of using the |
---|
377 | * macro pGetExp !!! |
---|
378 | */ |
---|
379 | |
---|
380 | /*2 |
---|
381 | * compare the head monomial of p1 and p2 with lexicographic ordering |
---|
382 | */ |
---|
383 | static int comp_lp ( poly p1, poly p2, int f, int l, short * w ) |
---|
384 | { |
---|
385 | int i = f; |
---|
386 | while ( ( i <= l ) && ( pGetExp(p1,i) == pGetExp(p2,i) ) ) |
---|
387 | i++; |
---|
388 | if ( i > l ) |
---|
389 | return 0; |
---|
390 | if ( pGetExp(p1,i) > pGetExp(p2,i) ) |
---|
391 | return 1; |
---|
392 | return -1; |
---|
393 | } |
---|
394 | |
---|
395 | /*2 |
---|
396 | * compare the head monomial of p1 and p2 with degree reverse lexicographic |
---|
397 | * ordering |
---|
398 | */ |
---|
399 | static int comp_dp ( poly p1, poly p2, int f, int l, short * w ) |
---|
400 | { |
---|
401 | int i, s1 = 0, s2 = 0; |
---|
402 | |
---|
403 | for ( i = f; i <= l; i++ ) |
---|
404 | { |
---|
405 | s1 += pGetExp(p1,i); |
---|
406 | s2 += pGetExp(p2,i); |
---|
407 | } |
---|
408 | if ( s1 == s2 ) |
---|
409 | { |
---|
410 | i = l; |
---|
411 | while ( (i >= f ) && ( pGetExp(p1,i) == pGetExp(p2,i) ) ) |
---|
412 | i--; |
---|
413 | if ( i < f ) |
---|
414 | return 0; |
---|
415 | if ( pGetExp(p1,i) > pGetExp(p2,i) ) |
---|
416 | return -1; |
---|
417 | return 1; |
---|
418 | } |
---|
419 | if ( s1 > s2 ) |
---|
420 | return 1; |
---|
421 | return -1; |
---|
422 | } |
---|
423 | |
---|
424 | /*2 |
---|
425 | * compare the head monomial of p1 and p2 with degree lexicographic ordering |
---|
426 | */ |
---|
427 | static int comp_Dp ( poly p1, poly p2, int f, int l, short * w ) |
---|
428 | { |
---|
429 | int i, s1 = 0, s2 = 0; |
---|
430 | |
---|
431 | for ( i = f; i <= l; i++ ) |
---|
432 | { |
---|
433 | s1 += pGetExp(p1,i); |
---|
434 | s2 += pGetExp(p2,i); |
---|
435 | } |
---|
436 | if ( s1 == s2 ) |
---|
437 | { |
---|
438 | i = f; |
---|
439 | while ( ( i <= l ) && ( pGetExp(p1,i) == pGetExp(p2,i) ) ) |
---|
440 | i++; |
---|
441 | if ( i > l ) |
---|
442 | return 0; |
---|
443 | if ( pGetExp(p1,i) > pGetExp(p2,i) ) |
---|
444 | return 1; |
---|
445 | return -1; |
---|
446 | } |
---|
447 | if ( s1 > s2 ) |
---|
448 | return 1; |
---|
449 | return -1; |
---|
450 | } |
---|
451 | |
---|
452 | /*2 |
---|
453 | * compare the head monomial of p1 and p2 with weighted degree reverse |
---|
454 | * lexicographic ordering |
---|
455 | */ |
---|
456 | static int comp_wp ( poly p1, poly p2, int f, int l, short * w ) |
---|
457 | { |
---|
458 | int i, s1 = 0, s2 = 0; |
---|
459 | |
---|
460 | for ( i = f; i <= l; i++, w++ ) |
---|
461 | { |
---|
462 | s1 += (int)pGetExp(p1,i)*(*w); |
---|
463 | s2 += (int)pGetExp(p2,i)*(*w); |
---|
464 | } |
---|
465 | if ( s1 == s2 ) |
---|
466 | { |
---|
467 | i = l; |
---|
468 | while ( ( i >= f ) && ( pGetExp(p1,i) == pGetExp(p2,i) ) ) |
---|
469 | i--; |
---|
470 | if ( i < f ) |
---|
471 | return 0; |
---|
472 | if ( pGetExp(p1,i) > pGetExp(p2,i) ) |
---|
473 | return -1; |
---|
474 | return 1; |
---|
475 | } |
---|
476 | if ( s1 > s2 ) |
---|
477 | return 1; |
---|
478 | return -1; |
---|
479 | } |
---|
480 | |
---|
481 | /*2 |
---|
482 | * compare the head monomial of p1 and p2 with weighted degree lexicographic |
---|
483 | * ordering |
---|
484 | */ |
---|
485 | static int comp_Wp ( poly p1, poly p2, int f, int l, short * w ) |
---|
486 | { |
---|
487 | int i, s1 = 0, s2 = 0; |
---|
488 | |
---|
489 | for ( i = f; i <= l; i++, w++ ) |
---|
490 | { |
---|
491 | s1 += (int)pGetExp(p1,i)*(*w); |
---|
492 | s2 += (int)pGetExp(p2,i)*(*w); |
---|
493 | } |
---|
494 | if ( s1 == s2 ) |
---|
495 | { |
---|
496 | i = f; |
---|
497 | while ( ( i <= l ) && ( pGetExp(p1,i) == pGetExp(p2,i) ) ) |
---|
498 | i++; |
---|
499 | if ( i > l ) |
---|
500 | return 0; |
---|
501 | if ( pGetExp(p1,i) > pGetExp(p2,i) ) |
---|
502 | return 1; |
---|
503 | return -1; |
---|
504 | } |
---|
505 | if ( s1 > s2 ) |
---|
506 | return 1; |
---|
507 | return -1; |
---|
508 | } |
---|
509 | |
---|
510 | /*2 |
---|
511 | * compare the head monomial of p1 and p2 with lexicographic ordering |
---|
512 | * (power series case) |
---|
513 | */ |
---|
514 | static int comp_ls ( poly p1, poly p2, int f, int l, short * w ) |
---|
515 | { |
---|
516 | int i; |
---|
517 | |
---|
518 | i = f; |
---|
519 | while ( ( i <= l ) && ( pGetExp(p1,i) == pGetExp(p2,i) ) ) |
---|
520 | i++; |
---|
521 | if ( i > l ) |
---|
522 | return 0; |
---|
523 | if ( pGetExp(p1,i) < pGetExp(p2,i) ) |
---|
524 | return 1; |
---|
525 | return -1; |
---|
526 | } |
---|
527 | |
---|
528 | /*2 |
---|
529 | * compare the head monomial of p1 and p2 with degree reverse lexicographic |
---|
530 | * ordering (power series case) |
---|
531 | */ |
---|
532 | static int comp_ds ( poly p1, poly p2, int f, int l, short * w ) |
---|
533 | { |
---|
534 | int i, s1 = 0, s2 = 0; |
---|
535 | |
---|
536 | for ( i = f; i <= l; i++ ) |
---|
537 | { |
---|
538 | s1 += pGetExp(p1,i); |
---|
539 | s2 += pGetExp(p2,i); |
---|
540 | } |
---|
541 | if ( s1 == s2 ) |
---|
542 | { |
---|
543 | i = l; |
---|
544 | while ( ( i >= f ) && ( pGetExp(p1,i) == pGetExp(p2,i) ) ) |
---|
545 | i--; |
---|
546 | if ( i < f ) |
---|
547 | return 0; |
---|
548 | if ( pGetExp(p1,i) < pGetExp(p2,i) ) |
---|
549 | return 1; |
---|
550 | return -1; |
---|
551 | } |
---|
552 | if ( s1 < s2 ) |
---|
553 | return 1; |
---|
554 | return -1; |
---|
555 | } |
---|
556 | |
---|
557 | /*2 |
---|
558 | * compare the head monomial of p1 and p2 with degree lexicographic ordering |
---|
559 | * (power series case) |
---|
560 | */ |
---|
561 | static int comp_Ds ( poly p1, poly p2, int f, int l, short * w ) |
---|
562 | { |
---|
563 | int i, s1 = 0, s2 = 0; |
---|
564 | |
---|
565 | for ( i = f; i <= l; i++ ) |
---|
566 | { |
---|
567 | s1 += pGetExp(p1,i); |
---|
568 | s2 += pGetExp(p2,i); |
---|
569 | } |
---|
570 | if ( s1 == s2 ) |
---|
571 | { |
---|
572 | i = f; |
---|
573 | while ( ( i <= l ) && ( pGetExp(p1,i) == pGetExp(p2,i) ) ) |
---|
574 | i++; |
---|
575 | if ( i > l ) |
---|
576 | return 0; |
---|
577 | if ( pGetExp(p1,i) < pGetExp(p2,i) ) |
---|
578 | return -1; |
---|
579 | return 1; |
---|
580 | } |
---|
581 | if ( s1 < s2 ) |
---|
582 | return 1; |
---|
583 | return -1; |
---|
584 | } |
---|
585 | |
---|
586 | /*2 |
---|
587 | * compare the head monomial of p1 and p2 with weighted degree reverse |
---|
588 | * lexicographic ordering (power series case) |
---|
589 | */ |
---|
590 | static int comp_ws ( poly p1, poly p2, int f, int l, short * w ) |
---|
591 | { |
---|
592 | int i, s1 = 0, s2 = 0; |
---|
593 | |
---|
594 | for ( i = f; i <= l; i++, w++ ) |
---|
595 | { |
---|
596 | s1 += (int)pGetExp(p1,i)*(*w); |
---|
597 | s2 += (int)pGetExp(p2,i)*(*w); |
---|
598 | } |
---|
599 | if ( s1 == s2 ) |
---|
600 | { |
---|
601 | i = l; |
---|
602 | while ( ( i >= f ) && ( pGetExp(p1,i) == pGetExp(p2,i) ) ) |
---|
603 | i--; |
---|
604 | if ( i < f ) |
---|
605 | return 0; |
---|
606 | if ( pGetExp(p1,i) < pGetExp(p2,i) ) |
---|
607 | return 1; |
---|
608 | return -1; |
---|
609 | } |
---|
610 | if ( s1 < s2 ) |
---|
611 | return 1; |
---|
612 | return -1; |
---|
613 | } |
---|
614 | |
---|
615 | /*2 |
---|
616 | * compare the head monomial of p1 and p2 with weighted degree lexicographic |
---|
617 | * ordering (power series case) |
---|
618 | */ |
---|
619 | static int comp_Ws ( poly p1, poly p2, int f, int l, short * w ) |
---|
620 | { |
---|
621 | int i, s1 = 0, s2 = 0; |
---|
622 | |
---|
623 | for ( i = f; i <= l; i++, w++ ) |
---|
624 | { |
---|
625 | s1 += (int)pGetExp(p1,i)*(*w); |
---|
626 | s2 += (int)pGetExp(p2,i)*(*w); |
---|
627 | } |
---|
628 | if ( s1 == s2 ) |
---|
629 | { |
---|
630 | i = f; |
---|
631 | while ( ( i <= l ) && ( pGetExp(p1,i) == pGetExp(p2,i) ) ) |
---|
632 | i++; |
---|
633 | if ( i > l ) |
---|
634 | return 0; |
---|
635 | if ( pGetExp(p1,i) < pGetExp(p2,i) ) |
---|
636 | return -1; |
---|
637 | return 1; |
---|
638 | } |
---|
639 | if ( s1 < s2 ) |
---|
640 | return 1; |
---|
641 | return -1; |
---|
642 | } |
---|
643 | |
---|
644 | /*2 |
---|
645 | * compare the head monomial of p1 and p2 with matrix order |
---|
646 | * w contains a series of l-f+1 lines |
---|
647 | */ |
---|
648 | static int comp_M ( poly p1, poly p2, int f, int l, short * w ) |
---|
649 | { |
---|
650 | int i, j, s1, s2; |
---|
651 | |
---|
652 | for ( i = f; i <= l; i++ ) |
---|
653 | { |
---|
654 | s1 = s2 = 0; |
---|
655 | for ( j = f; j <= l; j++, w++ ) |
---|
656 | { |
---|
657 | s1 += (int)pGetExp(p1,j)*(int)(*w); |
---|
658 | s2 += (int)pGetExp(p2,j)*(int)(*w); |
---|
659 | } |
---|
660 | if ( s1 < s2 ) |
---|
661 | return -1; |
---|
662 | if ( s1 > s2 ) |
---|
663 | return 1; |
---|
664 | /* now w points to the last element of the current row, the next w++ */ |
---|
665 | /* moves on to the first element of the next row ! */ |
---|
666 | } |
---|
667 | return 0; |
---|
668 | } |
---|
669 | |
---|
670 | /*2 |
---|
671 | * compare the head monomial of p1 and p2 with weight vector |
---|
672 | */ |
---|
673 | static int comp_a ( poly p1, poly p2, int f, int l, short * w ) |
---|
674 | { |
---|
675 | int i, s1 = 0, s2 = 0; |
---|
676 | |
---|
677 | for ( i = f; i <= l; i++, w++ ) |
---|
678 | { |
---|
679 | s1 += (int)pGetExp(p1,i)*(*w); |
---|
680 | s2 += (int)pGetExp(p2,i)*(*w); |
---|
681 | } |
---|
682 | if ( s1 > s2 ) |
---|
683 | return 1; |
---|
684 | if ( s1 < s2 ) |
---|
685 | return -1; |
---|
686 | return 0; |
---|
687 | } |
---|
688 | |
---|
689 | /*2 |
---|
690 | * compare the head monomial of p1 and p2 with module component |
---|
691 | */ |
---|
692 | static int comp_c ( poly p1, poly p2, int f, int l, short * w ) |
---|
693 | { |
---|
694 | if ( pGetComp(p1) > pGetComp(p2) ) |
---|
695 | return -pComponentOrder; |
---|
696 | if ( pGetComp(p1) < pGetComp(p2) ) |
---|
697 | return pComponentOrder; |
---|
698 | return 0; |
---|
699 | } |
---|
700 | |
---|
701 | /*---------------------------------------------------------------*/ |
---|
702 | |
---|
703 | /*2 |
---|
704 | * compare p1 and p2 by a block ordering |
---|
705 | * uses (*bcomph[])() to do the real work |
---|
706 | */ |
---|
707 | static int BlockComp(poly p1, poly p2) |
---|
708 | { |
---|
709 | int res, i, e, a; |
---|
710 | |
---|
711 | /*4 compare in all blocks,* |
---|
712 | * each block has var numbers a(=block0[i]) to e (=block1[i])* |
---|
713 | * the block number starts with 0*/ |
---|
714 | e = 0; |
---|
715 | i = 0; |
---|
716 | loop |
---|
717 | { |
---|
718 | a = block0[i]; |
---|
719 | e = block1[i]; |
---|
720 | res = (*bcomph[i])(p1, p2, a, e , polys_wv[i]); |
---|
721 | if (res) |
---|
722 | return res; |
---|
723 | i++; |
---|
724 | if (order[i]==0) |
---|
725 | break; |
---|
726 | } |
---|
727 | return 0; |
---|
728 | } |
---|
729 | |
---|
730 | int pComp(poly p1, poly p2) |
---|
731 | { |
---|
732 | if (p2==NULL) |
---|
733 | return 1; |
---|
734 | if (p1==NULL) |
---|
735 | return -1; |
---|
736 | return pComp0(p1,p2); |
---|
737 | } |
---|
738 | |
---|
739 | /*----------pComp handling for syzygies---------------------*/ |
---|
740 | static void newHeadsB(polyset actHeads,int length) |
---|
741 | { |
---|
742 | int i; |
---|
743 | int* newOrder=(int*)Alloc(length*sizeof(int)); |
---|
744 | |
---|
745 | for (i=0;i<length;i++) |
---|
746 | { |
---|
747 | if (actHeads[i]) |
---|
748 | { |
---|
749 | newOrder[i] = SchreyerOrd[pGetComp(actHeads[i])-1]; |
---|
750 | } |
---|
751 | else |
---|
752 | { |
---|
753 | newOrder[i]=0; |
---|
754 | } |
---|
755 | } |
---|
756 | Free((ADDRESS)SchreyerOrd,maxSchreyer*sizeof(int)); |
---|
757 | SchreyerOrd = newOrder; |
---|
758 | maxSchreyer = length; |
---|
759 | /* |
---|
760 | *for (i=0;i<maxSchreyer;i++); Print("%d ",SchreyerOrd[i]); |
---|
761 | *PrintLn(); |
---|
762 | */ |
---|
763 | } |
---|
764 | |
---|
765 | int mcompSchrB(poly p1,poly p2) |
---|
766 | { |
---|
767 | int CompP1=pGetComp(p1),CompP2=pGetComp(p2),result, |
---|
768 | cP1=SchreyerOrd[CompP1-1],cP2=SchreyerOrd[CompP2-1]; |
---|
769 | |
---|
770 | if (CompP1==CompP2) return pCompOld(p1,p2); |
---|
771 | pSetComp(p1,cP1); |
---|
772 | pSetComp(p2,cP2); |
---|
773 | result = pCompOld(p1,p2); |
---|
774 | pSetComp(p1,CompP1); |
---|
775 | pSetComp(p2,CompP2); |
---|
776 | if (!result) |
---|
777 | { |
---|
778 | if (CompP1>CompP2) |
---|
779 | return -1; |
---|
780 | else if (CompP1<CompP2) |
---|
781 | return 1; |
---|
782 | } |
---|
783 | return result; |
---|
784 | } |
---|
785 | |
---|
786 | |
---|
787 | static void newHeadsM(polyset actHeads,int length) |
---|
788 | { |
---|
789 | int i; |
---|
790 | int* newOrder= |
---|
791 | (int*)Alloc((length+maxSchreyer-indexShift)*sizeof(int)); |
---|
792 | |
---|
793 | for (i=0;i<length+maxSchreyer-indexShift;i++) |
---|
794 | newOrder[i]=0; |
---|
795 | for (i=indexShift;i<maxSchreyer;i++) |
---|
796 | { |
---|
797 | newOrder[i-indexShift] = SchreyerOrd[i]; |
---|
798 | SchreyerOrd[i] = 0; |
---|
799 | } |
---|
800 | for (i=maxSchreyer-indexShift;i<length+maxSchreyer-indexShift;i++) |
---|
801 | newOrder[i] = newOrder[pGetComp(actHeads[i-maxSchreyer+indexShift])-1]; |
---|
802 | Free((ADDRESS)SchreyerOrd,maxSchreyer*sizeof(int)); |
---|
803 | SchreyerOrd = newOrder; |
---|
804 | indexShift = maxSchreyer-indexShift; |
---|
805 | maxSchreyer = length+indexShift; |
---|
806 | } |
---|
807 | |
---|
808 | /*2 |
---|
809 | * compute the length of a polynomial (in l) |
---|
810 | * and the degree of the monomial with maximal degree: |
---|
811 | * this is NOT the last one and the module component |
---|
812 | * has to be <= indexShift |
---|
813 | */ |
---|
814 | static int ldegSchrM(poly p,int *l) |
---|
815 | { |
---|
816 | int t,max; |
---|
817 | |
---|
818 | (*l)=1; |
---|
819 | max=pFDeg(p); |
---|
820 | while ((pNext(p)!=NULL) && (pGetComp(pNext(p))<=indexShift)) |
---|
821 | { |
---|
822 | pIter(p); |
---|
823 | t=pFDeg(p); |
---|
824 | if (t>max) max=t; |
---|
825 | (*l)++; |
---|
826 | } |
---|
827 | return max; |
---|
828 | } |
---|
829 | |
---|
830 | int mcompSchrM(poly p1,poly p2) |
---|
831 | { |
---|
832 | if ( pGetComp(p1)<=indexShift) |
---|
833 | { |
---|
834 | if ( pGetComp(p2)>indexShift) return 1; |
---|
835 | } |
---|
836 | else if ( pGetComp(p2)<=indexShift) return -1; |
---|
837 | return mcompSchrB(p1,p2); |
---|
838 | } |
---|
839 | |
---|
840 | void pSetSchreyerOrdM(polyset nextOrder, int length,int comps) |
---|
841 | { |
---|
842 | int i; |
---|
843 | |
---|
844 | if (length!=0) |
---|
845 | { |
---|
846 | if (maxSchreyer!=0) |
---|
847 | newHeadsM(nextOrder, length); |
---|
848 | else |
---|
849 | { |
---|
850 | indexShift = comps; |
---|
851 | if (!indexShift) indexShift = 1; |
---|
852 | SchreyerOrd = (int*)Alloc((indexShift+length)*sizeof(int)); |
---|
853 | maxSchreyer = length+indexShift; |
---|
854 | for (i=0;i<indexShift;i++) |
---|
855 | SchreyerOrd[i] = i; |
---|
856 | for (i=indexShift;i<maxSchreyer;i++) |
---|
857 | SchreyerOrd[i] = pGetComp(nextOrder[i-indexShift]); |
---|
858 | pCompOld = pComp0; |
---|
859 | pComp0 = mcompSchrM; |
---|
860 | pLDegOld = pLDeg; |
---|
861 | pLDeg = ldegSchrM; |
---|
862 | } |
---|
863 | } |
---|
864 | else |
---|
865 | { |
---|
866 | if (maxSchreyer!=0) |
---|
867 | { |
---|
868 | Free((ADDRESS)SchreyerOrd,maxSchreyer*sizeof(int)); |
---|
869 | maxSchreyer = 0; |
---|
870 | indexShift = 0; |
---|
871 | pComp0 = pCompOld; |
---|
872 | pLDeg = pLDegOld; |
---|
873 | } |
---|
874 | } |
---|
875 | } |
---|
876 | |
---|
877 | /*2 |
---|
878 | *the pComp0 for normal syzygies |
---|
879 | *compares monomials in the usual ring order (pCompOld) |
---|
880 | *but groups module indecees according indexBounds befor |
---|
881 | */ |
---|
882 | static int mcompSyz(poly p1,poly p2) |
---|
883 | { |
---|
884 | if (pGetComp(p1)<=pMaxBound) |
---|
885 | { |
---|
886 | if (pGetComp(p2)>pMaxBound) return 1; |
---|
887 | } |
---|
888 | else if (pGetComp(p2)<=pMaxBound) |
---|
889 | { |
---|
890 | return -1; |
---|
891 | } |
---|
892 | return pCompOld(p1,p2); |
---|
893 | } |
---|
894 | |
---|
895 | void pSetSyzComp(int k) |
---|
896 | { |
---|
897 | if (k!=0) |
---|
898 | { |
---|
899 | if (pMaxBound==0) |
---|
900 | { |
---|
901 | pCompOld = pComp0; |
---|
902 | pComp0 = mcompSyz; |
---|
903 | } |
---|
904 | pMaxBound = k; |
---|
905 | } |
---|
906 | else |
---|
907 | { |
---|
908 | if (pMaxBound!=0) |
---|
909 | { |
---|
910 | pComp0 = pCompOld; |
---|
911 | pMaxBound = 0; |
---|
912 | } |
---|
913 | } |
---|
914 | } |
---|
915 | |
---|
916 | /*2 |
---|
917 | * the type of the module ordering: C: -1, c: 1 |
---|
918 | */ |
---|
919 | int pModuleOrder() |
---|
920 | { |
---|
921 | return pComponentOrder; |
---|
922 | } |
---|
923 | |
---|
924 | /* -------------------------------------------------------------------*/ |
---|
925 | /* several possibilities for pFDeg: the degree of the head term */ |
---|
926 | /*2 |
---|
927 | * compute the degree of the leading monomial of p |
---|
928 | * the ordering is compatible with degree, use a->order |
---|
929 | */ |
---|
930 | int pDeg(poly a) |
---|
931 | { |
---|
932 | return ((a!=NULL) ? (a->Order) : (-1)); |
---|
933 | } |
---|
934 | |
---|
935 | /*2 |
---|
936 | * compute the degree of the leading monomial of p |
---|
937 | * with respect to weigths 1 |
---|
938 | * (all are 1 so save multiplications or they are of different signs) |
---|
939 | * the ordering is not compatible with degree so do not use p->Order |
---|
940 | */ |
---|
941 | int pTotaldegree(poly p) |
---|
942 | { |
---|
943 | return pExpQuerSum(p); |
---|
944 | } |
---|
945 | |
---|
946 | /*2 |
---|
947 | * compute the degree of the leading monomial of p |
---|
948 | * with respect to weigths from the ordering |
---|
949 | * the ordering is not compatible with degree so do not use p->Order |
---|
950 | */ |
---|
951 | int pWTotaldegree(poly p) |
---|
952 | { |
---|
953 | int i, k; |
---|
954 | int j =0; |
---|
955 | |
---|
956 | // iterate through each block: |
---|
957 | for (i=0;order[i]!=0;i++) |
---|
958 | { |
---|
959 | switch(order[i]) |
---|
960 | { |
---|
961 | case ringorder_wp: |
---|
962 | case ringorder_ws: |
---|
963 | case ringorder_Wp: |
---|
964 | case ringorder_Ws: |
---|
965 | for (k=block0[i];k<=block1[i];k++) |
---|
966 | { // in jedem block: |
---|
967 | j+= pGetExp(p,k)*polys_wv[i][k-block0[i]]; |
---|
968 | } |
---|
969 | break; |
---|
970 | case ringorder_M: |
---|
971 | case ringorder_lp: |
---|
972 | case ringorder_dp: |
---|
973 | case ringorder_ds: |
---|
974 | case ringorder_Dp: |
---|
975 | case ringorder_Ds: |
---|
976 | for (k=block0[i];k<=block1[i];k++) |
---|
977 | { |
---|
978 | j+= pGetExp(p,k); |
---|
979 | } |
---|
980 | break; |
---|
981 | case ringorder_c: |
---|
982 | case ringorder_C: |
---|
983 | break; |
---|
984 | case ringorder_a: |
---|
985 | for (k=block0[i];k<=block1[i];k++) |
---|
986 | { // only one line |
---|
987 | j+= pGetExp(p,k)*polys_wv[i][k-block0[i]]; |
---|
988 | } |
---|
989 | return j; |
---|
990 | } |
---|
991 | } |
---|
992 | return j; |
---|
993 | } |
---|
994 | /* ---------------------------------------------------------------------*/ |
---|
995 | /* several possibilities for pLDeg: the maximal degree of a monomial in p*/ |
---|
996 | /* compute in l also the pLength of p */ |
---|
997 | |
---|
998 | /*2 |
---|
999 | * compute the length of a polynomial (in l) |
---|
1000 | * and the degree of the monomial with maximal degree: the last one |
---|
1001 | */ |
---|
1002 | static int ldeg0(poly p,int *l) |
---|
1003 | { |
---|
1004 | Exponent_t k= pGetComp(p); |
---|
1005 | int ll=1; |
---|
1006 | |
---|
1007 | while ((pNext(p)!=NULL) && (pGetComp(pNext(p))==k)) |
---|
1008 | { |
---|
1009 | pIter(p); |
---|
1010 | ll++; |
---|
1011 | } |
---|
1012 | *l=ll; |
---|
1013 | return (p->Order); |
---|
1014 | } |
---|
1015 | |
---|
1016 | /*2 |
---|
1017 | * compute the length of a polynomial (in l) |
---|
1018 | * and the degree of the monomial with maximal degree: the last one |
---|
1019 | * but search in all components before syzcomp |
---|
1020 | */ |
---|
1021 | static int ldeg0c(poly p,int *l) |
---|
1022 | { |
---|
1023 | int o=pFDeg(p); |
---|
1024 | int ll=1; |
---|
1025 | |
---|
1026 | if (pMaxBound/*syzComp*/==0) |
---|
1027 | { |
---|
1028 | while ((p=pNext(p))!=NULL) |
---|
1029 | { |
---|
1030 | o=pFDeg(p); |
---|
1031 | ll++; |
---|
1032 | } |
---|
1033 | } |
---|
1034 | else |
---|
1035 | { |
---|
1036 | while ((p=pNext(p))!=NULL) |
---|
1037 | { |
---|
1038 | if (pGetComp(p)<=pMaxBound/*syzComp*/) |
---|
1039 | { |
---|
1040 | o=pFDeg(p); |
---|
1041 | ll++; |
---|
1042 | } |
---|
1043 | else break; |
---|
1044 | } |
---|
1045 | } |
---|
1046 | *l=ll; |
---|
1047 | return o; |
---|
1048 | } |
---|
1049 | |
---|
1050 | /*2 |
---|
1051 | * compute the length of a polynomial (in l) |
---|
1052 | * and the degree of the monomial with maximal degree: the first one |
---|
1053 | * this works for the polynomial case with degree orderings |
---|
1054 | * (both c,dp and dp,c) |
---|
1055 | */ |
---|
1056 | static int ldegb(poly p,int *l) |
---|
1057 | { |
---|
1058 | Exponent_t k= pGetComp(p); |
---|
1059 | int o = p->Order; |
---|
1060 | int ll=1; |
---|
1061 | |
---|
1062 | while (((p=pNext(p))!=NULL) && (pGetComp(p)==k)) |
---|
1063 | { |
---|
1064 | ll++; |
---|
1065 | } |
---|
1066 | *l=ll; |
---|
1067 | return o; |
---|
1068 | } |
---|
1069 | |
---|
1070 | /*2 |
---|
1071 | * compute the length of a polynomial (in l) |
---|
1072 | * and the degree of the monomial with maximal degree: |
---|
1073 | * this is NOT the last one, we have to look for it |
---|
1074 | */ |
---|
1075 | static int ldeg1(poly p,int *l) |
---|
1076 | { |
---|
1077 | Exponent_t k= pGetComp(p); |
---|
1078 | int ll=1; |
---|
1079 | int t,max; |
---|
1080 | |
---|
1081 | max=pFDeg(p); |
---|
1082 | while (((p=pNext(p))!=NULL) && (pGetComp(p)==k)) |
---|
1083 | { |
---|
1084 | t=pFDeg(p); |
---|
1085 | if (t>max) max=t; |
---|
1086 | ll++; |
---|
1087 | } |
---|
1088 | *l=ll; |
---|
1089 | return max; |
---|
1090 | } |
---|
1091 | |
---|
1092 | /*2 |
---|
1093 | * compute the length of a polynomial (in l) |
---|
1094 | * and the degree of the monomial with maximal degree: |
---|
1095 | * this is NOT the last one, we have to look for it |
---|
1096 | * in all components |
---|
1097 | */ |
---|
1098 | static int ldeg1c(poly p,int *l) |
---|
1099 | { |
---|
1100 | int ll=1; |
---|
1101 | int t,max; |
---|
1102 | |
---|
1103 | max=pFDeg(p); |
---|
1104 | while ((p=pNext(p))!=NULL) |
---|
1105 | { |
---|
1106 | if ((pMaxBound/*syzComp*/==0) || (pGetComp(p)<=pMaxBound/*syzComp*/)) |
---|
1107 | { |
---|
1108 | if ((t=pFDeg(p))>max) max=t; |
---|
1109 | ll++; |
---|
1110 | } |
---|
1111 | else break; |
---|
1112 | } |
---|
1113 | *l=ll; |
---|
1114 | return max; |
---|
1115 | } |
---|
1116 | |
---|
1117 | /* -------------------------------------------------------- */ |
---|
1118 | /* set the variables for a choosen ordering */ |
---|
1119 | |
---|
1120 | |
---|
1121 | /* |
---|
1122 | * sets the comparision routine for monomials: for simple monomial orderings |
---|
1123 | * Priority is given to exponent vector |
---|
1124 | */ |
---|
1125 | static void SimpleChoose(int o_r, int comp_order, pCompProc *p) |
---|
1126 | { |
---|
1127 | switch(o_r) |
---|
1128 | { |
---|
1129 | case ringorder_dp: |
---|
1130 | case ringorder_wp: |
---|
1131 | case ringorder_ds: |
---|
1132 | case ringorder_ws: |
---|
1133 | case ringorder_ls: |
---|
1134 | case ringorder_unspec: |
---|
1135 | pSetVarIndicies_RevLex(pVariables); |
---|
1136 | pLexSgn = -1; |
---|
1137 | if (comp_order == ringorder_C || o_r == ringorder_unspec) |
---|
1138 | { |
---|
1139 | if (pVariables1W == 1) *p = pComp_otEXPCOMP_nwONE; |
---|
1140 | else if (pVariables1W == 2) *p = pComp_otEXPCOMP_nwTWO; |
---|
1141 | else if (pVariables1W & 1) *p = pComp_otEXPCOMP_nwODD; |
---|
1142 | else *p = pComp_otEXPCOMP_nwEVEN; |
---|
1143 | } |
---|
1144 | else |
---|
1145 | { |
---|
1146 | // component is compatible with exponent vector |
---|
1147 | if (pVariables1W == 1) *p = pComp_otEXP_nwONE; |
---|
1148 | else if (pVariables1W == 2) *p = pComp_otEXP_nwTWO; |
---|
1149 | else if (pVariables1W & 1) *p = pComp_otEXP_nwODD; |
---|
1150 | else *p = pComp_otEXP_nwEVEN; |
---|
1151 | } |
---|
1152 | break; |
---|
1153 | |
---|
1154 | #ifdef PDEBUG |
---|
1155 | case ringorder_lp: |
---|
1156 | case ringorder_Dp: |
---|
1157 | case ringorder_Wp: |
---|
1158 | case ringorder_Ds: |
---|
1159 | case ringorder_Ws: |
---|
1160 | #else |
---|
1161 | default: |
---|
1162 | #endif |
---|
1163 | pSetVarIndicies_Lex(pVariables); |
---|
1164 | pLexSgn = 1; |
---|
1165 | if (comp_order == ringorder_c) |
---|
1166 | { |
---|
1167 | if (pVariables1W == 1) *p = pComp_otEXPCOMP_nwONE; |
---|
1168 | else if (pVariables1W == 2) *p = pComp_otEXPCOMP_nwTWO; |
---|
1169 | else if (pVariables1W & 1) *p = pComp_otEXPCOMP_nwODD; |
---|
1170 | else *p = pComp_otEXPCOMP_nwEVEN; |
---|
1171 | } |
---|
1172 | else |
---|
1173 | { |
---|
1174 | // component is compatible with exponent vector |
---|
1175 | if (pVariables1W == 1) *p = pComp_otEXP_nwONE; |
---|
1176 | else if (pVariables1W == 2) *p = pComp_otEXP_nwTWO; |
---|
1177 | else if (pVariables1W & 1) *p = pComp_otEXP_nwODD; |
---|
1178 | else *p = pComp_otEXP_nwEVEN; |
---|
1179 | } |
---|
1180 | #ifdef PDEBUG |
---|
1181 | break; |
---|
1182 | default: |
---|
1183 | Werror("wrong internal ordering:%d at %s, l:%d\n",o_r,__FILE__,__LINE__); |
---|
1184 | #endif |
---|
1185 | } |
---|
1186 | |
---|
1187 | if (o_r == ringorder_lp || o_r == ringorder_ls) |
---|
1188 | { |
---|
1189 | pLexOrder=TRUE; |
---|
1190 | pFDeg = pTotaldegree; |
---|
1191 | pLDeg = ldeg1c; |
---|
1192 | if (o_r == ringorder_ls) |
---|
1193 | pSetVarIndicies_Lex(pVariables); |
---|
1194 | } |
---|
1195 | } |
---|
1196 | |
---|
1197 | /* |
---|
1198 | * sets the comparision routine for monomials: for simple monomial orderings |
---|
1199 | * Priority is given to component |
---|
1200 | */ |
---|
1201 | static void SimpleChooseC(int o_r, pCompProc *p) |
---|
1202 | { |
---|
1203 | switch(o_r) |
---|
1204 | { |
---|
1205 | case ringorder_dp: |
---|
1206 | case ringorder_wp: |
---|
1207 | case ringorder_ds: |
---|
1208 | case ringorder_ls: |
---|
1209 | case ringorder_ws: |
---|
1210 | pSetVarIndicies_RevLex(pVariables); |
---|
1211 | pLexSgn = -1; |
---|
1212 | if (pVariablesW == 1) |
---|
1213 | *p = pComp_otCOMPEXP_nwONE; |
---|
1214 | else if (pVariablesW == 2) |
---|
1215 | *p = pComp_otCOMPEXP_nwTWO; |
---|
1216 | else if (pVariablesW & 1) |
---|
1217 | *p = pComp_otCOMPEXP_nwODD; |
---|
1218 | else |
---|
1219 | *p = pComp_otCOMPEXP_nwEVEN; |
---|
1220 | break; |
---|
1221 | |
---|
1222 | #ifdef PDEBUG |
---|
1223 | case ringorder_lp: |
---|
1224 | case ringorder_Dp: |
---|
1225 | case ringorder_Wp: |
---|
1226 | case ringorder_Ds: |
---|
1227 | case ringorder_Ws: |
---|
1228 | #else |
---|
1229 | default: |
---|
1230 | #endif |
---|
1231 | pSetVarIndicies_Lex(pVariables); |
---|
1232 | pLexSgn = 1; |
---|
1233 | if (pVariablesW == 1) |
---|
1234 | *p = pComp_otCOMPEXP_nwONE; |
---|
1235 | else if (pVariablesW == 2) |
---|
1236 | *p = pComp_otCOMPEXP_nwTWO; |
---|
1237 | else if (pVariablesW & 1) |
---|
1238 | *p = pComp_otCOMPEXP_nwODD; |
---|
1239 | else |
---|
1240 | *p = pComp_otCOMPEXP_nwEVEN; |
---|
1241 | #ifdef PDEBUG |
---|
1242 | break; |
---|
1243 | default: |
---|
1244 | Werror("wrong internal ordering:%d at %s, l:%d\n",o_r,__FILE__,__LINE__); |
---|
1245 | #endif |
---|
1246 | } |
---|
1247 | if (o_r == ringorder_lp || o_r == ringorder_ls) |
---|
1248 | { |
---|
1249 | pLexOrder=TRUE; |
---|
1250 | pFDeg = pTotaldegree; |
---|
1251 | pLDeg = ldeg1c; |
---|
1252 | if (o_r == ringorder_ls) |
---|
1253 | pSetVarIndicies_Lex(pVariables); |
---|
1254 | } |
---|
1255 | } |
---|
1256 | |
---|
1257 | /*2 |
---|
1258 | * sets pSetm |
---|
1259 | * (according o_r = order of first block) |
---|
1260 | */ |
---|
1261 | static void SetpSetm(int o_r, int ip) |
---|
1262 | { |
---|
1263 | switch(o_r) |
---|
1264 | { |
---|
1265 | case ringorder_lp: |
---|
1266 | case ringorder_ls: |
---|
1267 | if (pVariables>1) |
---|
1268 | pSetm= setlex2; |
---|
1269 | else |
---|
1270 | pSetm= setlex1; |
---|
1271 | break; |
---|
1272 | case ringorder_dp: |
---|
1273 | case ringorder_Dp: |
---|
1274 | case ringorder_ds: |
---|
1275 | case ringorder_Ds: |
---|
1276 | case ringorder_unspec: |
---|
1277 | pSetm= setdeg1; |
---|
1278 | break; |
---|
1279 | case ringorder_a: |
---|
1280 | case ringorder_wp: |
---|
1281 | case ringorder_Wp: |
---|
1282 | case ringorder_ws: |
---|
1283 | case ringorder_Ws: |
---|
1284 | case ringorder_M: |
---|
1285 | pSetm= setdeg1w; |
---|
1286 | firstwv=polys_wv[ip]; |
---|
1287 | break; |
---|
1288 | case ringorder_c: |
---|
1289 | case ringorder_C: |
---|
1290 | return; |
---|
1291 | /*do not set firstBlockEnds for this orderings*/ |
---|
1292 | #ifdef TEST |
---|
1293 | default: |
---|
1294 | Werror("wrong internal ordering:%d at %s, l:%d\n",o_r,__FILE__,__LINE__); |
---|
1295 | #endif |
---|
1296 | } |
---|
1297 | firstBlockEnds=block1[ip]; |
---|
1298 | } |
---|
1299 | |
---|
1300 | |
---|
1301 | /*2 |
---|
1302 | * sets the comparision routine for monomials: for all but the first |
---|
1303 | * block of variables (ip is the block number, o_r the number of the ordering) |
---|
1304 | */ |
---|
1305 | static void HighSet(int ip, int o_r) |
---|
1306 | { |
---|
1307 | switch(o_r) |
---|
1308 | { |
---|
1309 | case ringorder_lp: |
---|
1310 | bcomph[ip]=comp_lp; |
---|
1311 | if (pOrdSgn==-1) pMixedOrder=TRUE; |
---|
1312 | break; |
---|
1313 | case ringorder_dp: |
---|
1314 | bcomph[ip]=comp_dp; |
---|
1315 | if (pOrdSgn==-1) pMixedOrder=TRUE; |
---|
1316 | break; |
---|
1317 | case ringorder_Dp: |
---|
1318 | bcomph[ip]=comp_Dp; |
---|
1319 | if (pOrdSgn==-1) pMixedOrder=TRUE; |
---|
1320 | break; |
---|
1321 | case ringorder_wp: |
---|
1322 | bcomph[ip]=comp_wp; |
---|
1323 | if (pOrdSgn==-1) pMixedOrder=TRUE; |
---|
1324 | break; |
---|
1325 | case ringorder_Wp: |
---|
1326 | bcomph[ip]=comp_Wp; |
---|
1327 | if (pOrdSgn==-1) pMixedOrder=TRUE; |
---|
1328 | break; |
---|
1329 | case ringorder_ls: |
---|
1330 | bcomph[ip]=comp_ls; |
---|
1331 | break; |
---|
1332 | case ringorder_ds: |
---|
1333 | bcomph[ip]=comp_ds; |
---|
1334 | break; |
---|
1335 | case ringorder_Ds: |
---|
1336 | bcomph[ip]=comp_Ds; |
---|
1337 | break; |
---|
1338 | case ringorder_ws: |
---|
1339 | bcomph[ip]=comp_ws; |
---|
1340 | break; |
---|
1341 | case ringorder_Ws: |
---|
1342 | bcomph[ip]=comp_Ws; |
---|
1343 | break; |
---|
1344 | case ringorder_c: |
---|
1345 | pComponentOrder=1; |
---|
1346 | bcomph[ip]=comp_c; |
---|
1347 | break; |
---|
1348 | case ringorder_C: |
---|
1349 | pComponentOrder=-1; |
---|
1350 | bcomph[ip]=comp_c; |
---|
1351 | break; |
---|
1352 | case ringorder_M: |
---|
1353 | bcomph[ip]=comp_M; |
---|
1354 | pMixedOrder=TRUE; |
---|
1355 | break; |
---|
1356 | case ringorder_a: |
---|
1357 | if (pOrdSgn==-1) pMixedOrder=TRUE; |
---|
1358 | if (ip==0) |
---|
1359 | bcomph[0]=comp1a; |
---|
1360 | else |
---|
1361 | bcomph[ip]=comp_a; |
---|
1362 | break; |
---|
1363 | #ifdef TEST |
---|
1364 | default: |
---|
1365 | Werror("wrong internal ordering:%d at %s, l:%d\n",o_r,__FILE__,__LINE__); |
---|
1366 | #endif |
---|
1367 | } |
---|
1368 | } |
---|
1369 | |
---|
1370 | /* -------------------------------------------------------- */ |
---|
1371 | /*2 |
---|
1372 | * change all variables to fit the description of the new ring |
---|
1373 | */ |
---|
1374 | |
---|
1375 | void pChangeRing(int n, int Sgn, int * orders, int * b0, int * b1, |
---|
1376 | short ** wv) |
---|
1377 | { |
---|
1378 | sip_sring tmpR; |
---|
1379 | memset(&tmpR, 0, sizeof(sip_sring)); |
---|
1380 | tmpR.N = n; |
---|
1381 | tmpR.OrdSgn = Sgn; |
---|
1382 | tmpR.order = orders; |
---|
1383 | tmpR.block0 = b0; |
---|
1384 | tmpR.block1 = b1; |
---|
1385 | tmpR.wvhdl = wv; |
---|
1386 | pSetGlobals(&tmpR); |
---|
1387 | } |
---|
1388 | |
---|
1389 | void pSetGlobals(ring r, BOOLEAN complete) |
---|
1390 | { |
---|
1391 | int i; |
---|
1392 | pComponentOrder=1; |
---|
1393 | if (ppNoether!=NULL) pDelete(&ppNoether); |
---|
1394 | #ifdef SRING |
---|
1395 | pSRING=FALSE; |
---|
1396 | pAltVars=r->N+1; |
---|
1397 | #endif |
---|
1398 | pVariables = r->N; |
---|
1399 | |
---|
1400 | // set the various size parameters and initialize memory |
---|
1401 | if ((((pVariables+1)*sizeof(Exponent_t)) % sizeof(void*)) == 0) |
---|
1402 | pVariables1W = (pVariables+1)*sizeof(Exponent_t) / sizeof(void*); |
---|
1403 | else |
---|
1404 | pVariables1W = ((pVariables+1)*sizeof(Exponent_t) / sizeof(void*)) + 1; |
---|
1405 | |
---|
1406 | if ((((pVariables)*sizeof(Exponent_t)) % sizeof(void*)) == 0) |
---|
1407 | pVariablesW = (pVariables)*sizeof(Exponent_t) / sizeof(void*); |
---|
1408 | else |
---|
1409 | pVariablesW = ((pVariables)*sizeof(Exponent_t) / sizeof(void*)) + 1; |
---|
1410 | |
---|
1411 | pMonomSize = POLYSIZE + (pVariables + 1) * sizeof(Exponent_t); |
---|
1412 | if ((pMonomSize % sizeof(void*)) == 0) |
---|
1413 | { |
---|
1414 | pMonomSizeW = pMonomSize/sizeof(void*); |
---|
1415 | } |
---|
1416 | else |
---|
1417 | { |
---|
1418 | pMonomSizeW = pMonomSize/sizeof(void*) + 1; |
---|
1419 | pMonomSize = pMonomSizeW*sizeof(void*); |
---|
1420 | } |
---|
1421 | |
---|
1422 | // Set default Var Indicies |
---|
1423 | pSetVarIndicies(pVariables); |
---|
1424 | |
---|
1425 | // Initialize memory management |
---|
1426 | mmSpecializeBlock(pMonomSize); |
---|
1427 | |
---|
1428 | pOrdSgn = r->OrdSgn; |
---|
1429 | pVectorOut=(r->order[0]==ringorder_c); |
---|
1430 | order=r->order; |
---|
1431 | block0=r->block0; |
---|
1432 | block1=r->block1; |
---|
1433 | firstwv=NULL; |
---|
1434 | polys_wv=r->wvhdl; |
---|
1435 | /*------- only one real block ----------------------*/ |
---|
1436 | pLexOrder=FALSE; |
---|
1437 | pMixedOrder=FALSE; |
---|
1438 | pFDeg=pDeg; |
---|
1439 | if (pOrdSgn == 1) pLDeg = ldegb; |
---|
1440 | else pLDeg = ldeg0; |
---|
1441 | /*======== ordering type is (_,c) =========================*/ |
---|
1442 | if ((order[0]==ringorder_unspec) |
---|
1443 | ||( |
---|
1444 | ((order[1]==ringorder_c)||(order[1]==ringorder_C)) |
---|
1445 | && (order[0]!=ringorder_M) |
---|
1446 | && (order[2]==0)) |
---|
1447 | ) |
---|
1448 | { |
---|
1449 | if ((order[0]!=ringorder_unspec) |
---|
1450 | && (order[1]==ringorder_C)) |
---|
1451 | pComponentOrder=-1; |
---|
1452 | if (pOrdSgn == -1) pLDeg = ldeg0c; |
---|
1453 | SimpleChoose(order[0],order[1], &pComp0); |
---|
1454 | SetpSetm(order[0],0); |
---|
1455 | } |
---|
1456 | /*======== ordering type is (c,_) =========================*/ |
---|
1457 | else if (((order[0]==ringorder_c)||(order[0]==ringorder_C)) |
---|
1458 | && (order[1]!=ringorder_M) |
---|
1459 | && (order[2]==0)) |
---|
1460 | { |
---|
1461 | /* pLDeg = ldeg0; is standard*/ |
---|
1462 | if (order[0]==ringorder_C) |
---|
1463 | pComponentOrder=-1; |
---|
1464 | SimpleChooseC(order[1], &pComp0); |
---|
1465 | SetpSetm(order[1],1); |
---|
1466 | } |
---|
1467 | /*------- more than one block ----------------------*/ |
---|
1468 | else |
---|
1469 | { |
---|
1470 | //pLexOrder=TRUE; |
---|
1471 | pVectorOut=order[0]==ringorder_c; |
---|
1472 | if ((pVectorOut)||(order[0]==ringorder_C)) |
---|
1473 | { |
---|
1474 | if(block1[1]!=pVariables) pLexOrder=TRUE; |
---|
1475 | } |
---|
1476 | else |
---|
1477 | { |
---|
1478 | if(block1[0]!=pVariables) pLexOrder=TRUE; |
---|
1479 | } |
---|
1480 | /*the number of orderings:*/ |
---|
1481 | i = 0; |
---|
1482 | while (order[++i] != 0); |
---|
1483 | do |
---|
1484 | { |
---|
1485 | i--; |
---|
1486 | HighSet(i, order[i]);/*sets also pMixedOrder to TRUE, if...*/ |
---|
1487 | SetpSetm(order[i],i); |
---|
1488 | } |
---|
1489 | while (i != 0); |
---|
1490 | |
---|
1491 | pComp0 = BlockComp; |
---|
1492 | if ((order[0]!=ringorder_c)&&(order[0]!=ringorder_C)) |
---|
1493 | { |
---|
1494 | pLDeg = ldeg1c; |
---|
1495 | } |
---|
1496 | else |
---|
1497 | { |
---|
1498 | pLDeg = ldeg1; |
---|
1499 | } |
---|
1500 | pFDeg = pWTotaldegree; // may be improved: pTotaldegree for lp/dp/ls/.. blocks |
---|
1501 | } |
---|
1502 | if ((pLexOrder) || (pOrdSgn==-1)) |
---|
1503 | { |
---|
1504 | test &= ~Sy_bit(OPT_REDTAIL); /* noredTail */ |
---|
1505 | } |
---|
1506 | } |
---|
1507 | |
---|
1508 | /* -------------------------------------------------------- */ |
---|
1509 | |
---|
1510 | static BOOLEAN pMultT_nok; |
---|
1511 | /*2 |
---|
1512 | * update the polynomial a by multipying it by |
---|
1513 | * the (number) coefficient |
---|
1514 | * and the exponent vector (of) exp (a well initialized polynomial) |
---|
1515 | */ |
---|
1516 | poly pMultT(poly a, poly exp ) |
---|
1517 | { |
---|
1518 | int i; |
---|
1519 | number t,x,y=pGetCoeff(exp); |
---|
1520 | poly aa=a; |
---|
1521 | poly prev=NULL; |
---|
1522 | #ifdef SDRING |
---|
1523 | poly pDRINGres=NULL; |
---|
1524 | #endif |
---|
1525 | |
---|
1526 | pMultT_nok = pGetComp(exp); |
---|
1527 | #ifdef PDEBUG |
---|
1528 | pTest(a); |
---|
1529 | pTest(exp); |
---|
1530 | #endif |
---|
1531 | while (a !=NULL) |
---|
1532 | { |
---|
1533 | x=pGetCoeff(a); |
---|
1534 | t=nMult(x/*pGetCoeff(a)*/,y/*pGetCoeff(exp)*/); |
---|
1535 | nDelete(&x/*pGetCoeff(a)*/); |
---|
1536 | pSetCoeff0(a,t); |
---|
1537 | if (nIsZero(t)) |
---|
1538 | { |
---|
1539 | if (prev==NULL) { pDelete1(&a); aa=a; } |
---|
1540 | else { pDelete1(&prev->next); a=prev->next;} |
---|
1541 | } |
---|
1542 | else |
---|
1543 | { |
---|
1544 | #ifdef DRING |
---|
1545 | if (pDRING) |
---|
1546 | { |
---|
1547 | if (pdDFlag(a)==1) |
---|
1548 | { |
---|
1549 | if (pdDFlag(exp)==1) |
---|
1550 | { |
---|
1551 | pDRINGres=pAdd(pDRINGres,pMultDD(a,exp)); |
---|
1552 | } |
---|
1553 | else |
---|
1554 | { |
---|
1555 | pDRINGres=pAdd(pDRINGres,pMultDT(a,exp)); |
---|
1556 | } |
---|
1557 | } |
---|
1558 | else |
---|
1559 | { |
---|
1560 | if (pdDFlag(exp)==1) |
---|
1561 | { |
---|
1562 | pDRINGres=pAdd(pDRINGres,pMultDD(a,exp)); |
---|
1563 | } |
---|
1564 | else |
---|
1565 | { |
---|
1566 | pDRINGres=pAdd(pDRINGres,pMultTT(a,exp)); |
---|
1567 | } |
---|
1568 | } |
---|
1569 | } |
---|
1570 | else |
---|
1571 | #endif |
---|
1572 | #ifdef SRING |
---|
1573 | if (pSRING) |
---|
1574 | { |
---|
1575 | pDRINGres=pAdd(pDRINGres,psMultM(a,exp)); |
---|
1576 | } |
---|
1577 | else |
---|
1578 | #endif |
---|
1579 | { |
---|
1580 | for (i=pVariables; i != 0; i--) |
---|
1581 | { |
---|
1582 | pAddExp(a,i, pGetExp(exp,i)); |
---|
1583 | } |
---|
1584 | #ifdef TEST_MAC_ORDER |
---|
1585 | if (bNoAdd) |
---|
1586 | pSetm(a); |
---|
1587 | else |
---|
1588 | #endif |
---|
1589 | a->Order += exp->Order; |
---|
1590 | if (pMultT_nok) |
---|
1591 | { |
---|
1592 | if (pGetComp(a) == 0) |
---|
1593 | { |
---|
1594 | pSetComp(a, pGetComp(exp)); |
---|
1595 | } |
---|
1596 | else |
---|
1597 | { |
---|
1598 | return NULL /*FALSE*/; |
---|
1599 | } |
---|
1600 | } |
---|
1601 | } |
---|
1602 | prev=a; |
---|
1603 | pIter(a); |
---|
1604 | } |
---|
1605 | } |
---|
1606 | pMultT_nok=0; |
---|
1607 | #ifdef DRING |
---|
1608 | if (pDRING) |
---|
1609 | { |
---|
1610 | pDelete(&aa); |
---|
1611 | return pDRINGres; |
---|
1612 | } |
---|
1613 | #endif |
---|
1614 | #ifdef SRING |
---|
1615 | if (pSRING) |
---|
1616 | { |
---|
1617 | pDelete(&aa); |
---|
1618 | return pDRINGres; |
---|
1619 | } |
---|
1620 | #endif |
---|
1621 | return aa; /*TRUE*/ |
---|
1622 | } |
---|
1623 | |
---|
1624 | /*2 |
---|
1625 | * multiply p1 with p2, p1 and p2 are destroyed |
---|
1626 | * do not put attention on speed: the procedure is only used in the interpreter |
---|
1627 | */ |
---|
1628 | poly pMult(poly p1, poly p2) |
---|
1629 | { |
---|
1630 | poly res, r, rn, a; |
---|
1631 | BOOLEAN cont; |
---|
1632 | |
---|
1633 | if ((p1!=NULL) && (p2!=NULL)) |
---|
1634 | { |
---|
1635 | #ifdef PDEBUG |
---|
1636 | pTest(p1); |
---|
1637 | pTest(p2); |
---|
1638 | #endif |
---|
1639 | cont = TRUE; |
---|
1640 | a = p1; |
---|
1641 | if (pNext(p2)!=NULL) |
---|
1642 | a = pCopy(a); |
---|
1643 | else |
---|
1644 | cont = FALSE; |
---|
1645 | res = pMultT(a, p2); |
---|
1646 | if (pMultT_nok) |
---|
1647 | { |
---|
1648 | if (cont) pDelete(&p1); |
---|
1649 | pDelete(&res); |
---|
1650 | pDelete(&p2); |
---|
1651 | return NULL; |
---|
1652 | } |
---|
1653 | pDelete1(&p2); |
---|
1654 | r = res; |
---|
1655 | if (r!=NULL) rn = pNext(r); |
---|
1656 | else rn=NULL; |
---|
1657 | while (cont) |
---|
1658 | { |
---|
1659 | if (pNext(p2)==NULL) |
---|
1660 | { |
---|
1661 | a = p1; |
---|
1662 | cont = FALSE; |
---|
1663 | } |
---|
1664 | else |
---|
1665 | { |
---|
1666 | a = pCopy(p1); |
---|
1667 | } |
---|
1668 | a=pMultT(a, p2); //sets pMultT_nok |
---|
1669 | if (pMultT_nok) |
---|
1670 | { |
---|
1671 | if (cont) pDelete(&p1); |
---|
1672 | pDelete(&a); |
---|
1673 | pDelete(&res); |
---|
1674 | pDelete(&p2); |
---|
1675 | return NULL; |
---|
1676 | } |
---|
1677 | while ((rn!=NULL) && (pComp0(rn,a)>0)) |
---|
1678 | { |
---|
1679 | r = rn; |
---|
1680 | pIter(rn); |
---|
1681 | } |
---|
1682 | if (r!=NULL) pNext(r) = rn = pAdd(a, rn); |
---|
1683 | else res=r=a; |
---|
1684 | pDelete1(&p2); |
---|
1685 | } |
---|
1686 | return res; |
---|
1687 | } |
---|
1688 | pDelete(&p1); |
---|
1689 | pDelete(&p2); |
---|
1690 | return NULL; |
---|
1691 | } |
---|
1692 | |
---|
1693 | /*2 |
---|
1694 | * update a by multiplying it with c (c will not be destroyed) |
---|
1695 | */ |
---|
1696 | void pMultN(poly a, number c) |
---|
1697 | { |
---|
1698 | number t; |
---|
1699 | |
---|
1700 | while (a!=NULL) |
---|
1701 | { |
---|
1702 | t=nMult(pGetCoeff(a), c); |
---|
1703 | //nNormalize(t); |
---|
1704 | pSetCoeff(a,t); |
---|
1705 | pIter(a); |
---|
1706 | } |
---|
1707 | } |
---|
1708 | |
---|
1709 | /*2 |
---|
1710 | * return a copy of the poly a times the number c (a,c will not be destroyed) |
---|
1711 | */ |
---|
1712 | poly pMultCopyN(poly a, number c) |
---|
1713 | { |
---|
1714 | poly result=NULL,hp; |
---|
1715 | |
---|
1716 | if (a != NULL) |
---|
1717 | { |
---|
1718 | result=pNew(); |
---|
1719 | pCopy2(result,a); |
---|
1720 | pNext(result)=NULL; |
---|
1721 | pGetCoeff(result)=nMult(pGetCoeff(a),c); |
---|
1722 | pIter(a); |
---|
1723 | hp=result; |
---|
1724 | while (a) |
---|
1725 | { |
---|
1726 | hp=pNext(hp)=pNew(); |
---|
1727 | pCopy2(hp,a); |
---|
1728 | pSetCoeff0(hp,nMult(pGetCoeff(a), c)); |
---|
1729 | pIter(a); |
---|
1730 | } |
---|
1731 | pNext(hp)=NULL; |
---|
1732 | } |
---|
1733 | return result; |
---|
1734 | } |
---|
1735 | |
---|
1736 | /*2 |
---|
1737 | * returns TRUE if the head term of b is a multiple of the head term of a |
---|
1738 | */ |
---|
1739 | #if defined(macintosh) |
---|
1740 | BOOLEAN pDivisibleBy(poly a, poly b) |
---|
1741 | { |
---|
1742 | if ((a!=NULL)&&(( pGetComp(a)==0) || ( pGetComp(a) == pGetComp(b)))) |
---|
1743 | { |
---|
1744 | int i=pVariables; |
---|
1745 | Exponent_t *e1=&( pGetExp(a,1)); |
---|
1746 | Exponent_t *e2=&( pGetExp(b,1)); |
---|
1747 | if ((*e1) > (*e2)) return FALSE; |
---|
1748 | do |
---|
1749 | { |
---|
1750 | i--; |
---|
1751 | if (i == 0) return TRUE; |
---|
1752 | e1++; |
---|
1753 | e2++; |
---|
1754 | } while ((*e1) <= (*e2)); |
---|
1755 | } |
---|
1756 | return FALSE; |
---|
1757 | } |
---|
1758 | #endif |
---|
1759 | |
---|
1760 | |
---|
1761 | /*2 |
---|
1762 | * assumes that the head term of b is a multiple of the head term of a |
---|
1763 | * and return the multiplicant *m |
---|
1764 | */ |
---|
1765 | poly pDivide(poly a, poly b) |
---|
1766 | { |
---|
1767 | int i; |
---|
1768 | poly result=pInit(); |
---|
1769 | |
---|
1770 | for(i=(int)pVariables; i; i--) |
---|
1771 | pSetExp(result,i, pGetExp(a,i)- pGetExp(b,i)); |
---|
1772 | pSetComp(result, pGetComp(a) - pGetComp(b)); |
---|
1773 | pSetm(result); |
---|
1774 | return result; |
---|
1775 | } |
---|
1776 | |
---|
1777 | /*2 |
---|
1778 | * divides a by the monomial b, ignores monomials wihich are not divisible |
---|
1779 | * assumes that b is not NULL |
---|
1780 | */ |
---|
1781 | poly pDivideM(poly a, poly b) |
---|
1782 | { |
---|
1783 | if (a==NULL) return NULL; |
---|
1784 | poly result=a; |
---|
1785 | poly prev=NULL; |
---|
1786 | int i; |
---|
1787 | number inv=nInvers(pGetCoeff(b)); |
---|
1788 | |
---|
1789 | while (a!=NULL) |
---|
1790 | { |
---|
1791 | if (pDivisibleBy(b,a)) |
---|
1792 | { |
---|
1793 | for(i=(int)pVariables; i; i--) |
---|
1794 | pSubExp(a,i, pGetExp(b,i)); |
---|
1795 | pSubComp(a, pGetComp(b)); |
---|
1796 | pSetm(a); |
---|
1797 | prev=a; |
---|
1798 | pIter(a); |
---|
1799 | } |
---|
1800 | else |
---|
1801 | { |
---|
1802 | if (prev==NULL) |
---|
1803 | { |
---|
1804 | pDelete1(&result); |
---|
1805 | a=result; |
---|
1806 | } |
---|
1807 | else |
---|
1808 | { |
---|
1809 | pDelete1(&pNext(prev)); |
---|
1810 | a=pNext(prev); |
---|
1811 | } |
---|
1812 | } |
---|
1813 | } |
---|
1814 | pMultN(result,inv); |
---|
1815 | nDelete(&inv); |
---|
1816 | pDelete(&b); |
---|
1817 | return result; |
---|
1818 | } |
---|
1819 | |
---|
1820 | /*2 |
---|
1821 | * returns the LCM of the head terms of a and b in *m |
---|
1822 | */ |
---|
1823 | void pLcm(poly a, poly b, poly m) |
---|
1824 | { |
---|
1825 | int i; |
---|
1826 | for (i=pVariables; i; i--) |
---|
1827 | { |
---|
1828 | pSetExp(m,i, max( pGetExp(a,i), pGetExp(b,i))); |
---|
1829 | } |
---|
1830 | pSetComp(m, max(pGetComp(a), pGetComp(b))); |
---|
1831 | } |
---|
1832 | |
---|
1833 | /*2 |
---|
1834 | * convert monomial given as string to poly, e.g. 1x3y5z |
---|
1835 | */ |
---|
1836 | poly pmInit(char *st, BOOLEAN &ok) |
---|
1837 | { |
---|
1838 | int i,j; |
---|
1839 | ok=FALSE; |
---|
1840 | BOOLEAN b=FALSE; |
---|
1841 | poly rc = pInit(); |
---|
1842 | char *s = nRead(st,&(rc->coef)); |
---|
1843 | if (s==st) |
---|
1844 | /* i.e. it does not start with a coeff: test if it is a ringvar*/ |
---|
1845 | { |
---|
1846 | j = rIsRingVar(s); |
---|
1847 | if (j >= 0) |
---|
1848 | { |
---|
1849 | pIncrExp(rc,1+j); |
---|
1850 | goto done; |
---|
1851 | } |
---|
1852 | } |
---|
1853 | else |
---|
1854 | b=TRUE; |
---|
1855 | while (*s!='\0') |
---|
1856 | { |
---|
1857 | char ss[2]; |
---|
1858 | ss[0] = *s++; |
---|
1859 | ss[1] = '\0'; |
---|
1860 | j = rIsRingVar(ss); |
---|
1861 | if (j >= 0) |
---|
1862 | { |
---|
1863 | s = eati(s,&i); |
---|
1864 | pAddExp(rc,1+j, (Exponent_t)i); |
---|
1865 | } |
---|
1866 | else |
---|
1867 | { |
---|
1868 | if ((s!=st)&&isdigit(st[0])) |
---|
1869 | { |
---|
1870 | errorreported=TRUE; |
---|
1871 | } |
---|
1872 | pDelete(&rc); |
---|
1873 | return NULL; |
---|
1874 | } |
---|
1875 | } |
---|
1876 | done: |
---|
1877 | ok=!errorreported; |
---|
1878 | if (nIsZero(pGetCoeff(rc))) pDelete1(&rc); |
---|
1879 | else |
---|
1880 | { |
---|
1881 | #ifdef DRING |
---|
1882 | if (pDRING) |
---|
1883 | { |
---|
1884 | for(i=1;i<=pdN;i++) |
---|
1885 | { |
---|
1886 | if(pGetExp(rc,pdDX(i))>0) |
---|
1887 | { |
---|
1888 | pdDFlag(rc)=1; |
---|
1889 | break; |
---|
1890 | } |
---|
1891 | } |
---|
1892 | } |
---|
1893 | #endif |
---|
1894 | pSetm(rc); |
---|
1895 | } |
---|
1896 | return rc; |
---|
1897 | } |
---|
1898 | |
---|
1899 | /*2 |
---|
1900 | *make p homgeneous by multiplying the monomials by powers of x_varnum |
---|
1901 | */ |
---|
1902 | poly pHomogen (poly p, int varnum) |
---|
1903 | { |
---|
1904 | poly q=NULL; |
---|
1905 | poly res; |
---|
1906 | int o,ii; |
---|
1907 | |
---|
1908 | if (p!=NULL) |
---|
1909 | { |
---|
1910 | if ((varnum < 1) || (varnum > pVariables)) |
---|
1911 | { |
---|
1912 | return NULL; |
---|
1913 | } |
---|
1914 | o=pWTotaldegree(p); |
---|
1915 | q=pNext(p); |
---|
1916 | while (q != NULL) |
---|
1917 | { |
---|
1918 | ii=pWTotaldegree(q); |
---|
1919 | if (ii>o) o=ii; |
---|
1920 | pIter(q); |
---|
1921 | } |
---|
1922 | q = pCopy(p); |
---|
1923 | res = q; |
---|
1924 | while (q != NULL) |
---|
1925 | { |
---|
1926 | ii = o-pWTotaldegree(q); |
---|
1927 | if (ii!=0) |
---|
1928 | { |
---|
1929 | pAddExp(q,varnum, (Exponent_t)ii); |
---|
1930 | pSetm(q); |
---|
1931 | } |
---|
1932 | pIter(q); |
---|
1933 | } |
---|
1934 | q = pOrdPolyInsertSetm(res); |
---|
1935 | } |
---|
1936 | return q; |
---|
1937 | } |
---|
1938 | |
---|
1939 | |
---|
1940 | /*2 |
---|
1941 | *replaces the maximal powers of the leading monomial of p2 in p1 by |
---|
1942 | *the same powers of n, utility for dehomogenization |
---|
1943 | */ |
---|
1944 | poly pDehomogen (poly p1,poly p2,number n) |
---|
1945 | { |
---|
1946 | polyset P; |
---|
1947 | int SizeOfSet=5; |
---|
1948 | int i; |
---|
1949 | poly p; |
---|
1950 | number nn; |
---|
1951 | |
---|
1952 | P = (polyset)Alloc(5*sizeof(poly)); |
---|
1953 | for (i=0; i<5; i++) |
---|
1954 | { |
---|
1955 | P[i] = NULL; |
---|
1956 | } |
---|
1957 | pCancelPolyByMonom(p1,p2,&P,&SizeOfSet); |
---|
1958 | p = P[0]; |
---|
1959 | //P[0] = NULL ;// for safety, may be remoeved later |
---|
1960 | for (i=1; i<SizeOfSet; i++) |
---|
1961 | { |
---|
1962 | if (P[i] != NULL) |
---|
1963 | { |
---|
1964 | nPower(n,i,&nn); |
---|
1965 | pMultN(P[i],nn); |
---|
1966 | p = pAdd(p,P[i]); |
---|
1967 | //P[i] =NULL; // for safety, may be removed later |
---|
1968 | nDelete(&nn); |
---|
1969 | } |
---|
1970 | } |
---|
1971 | Free((ADDRESS)P,SizeOfSet*sizeof(poly)); |
---|
1972 | return p; |
---|
1973 | } |
---|
1974 | |
---|
1975 | /*4 |
---|
1976 | *Returns the exponent of the maximal power of the leading monomial of |
---|
1977 | *p2 in that of p1 |
---|
1978 | */ |
---|
1979 | static int pGetMaxPower (poly p1,poly p2) |
---|
1980 | { |
---|
1981 | int i,k,res = 32000; /*a very large integer*/ |
---|
1982 | |
---|
1983 | if (p1 == NULL) return 0; |
---|
1984 | for (i=1; i<=pVariables; i++) |
---|
1985 | { |
---|
1986 | if ( pGetExp(p2,i) != 0) |
---|
1987 | { |
---|
1988 | k = pGetExp(p1,i) / pGetExp(p2,i); |
---|
1989 | if (k < res) res = k; |
---|
1990 | } |
---|
1991 | } |
---|
1992 | return res; |
---|
1993 | } |
---|
1994 | |
---|
1995 | /*2 |
---|
1996 | *Returns as i-th entry of P the coefficient of the (i-1) power of |
---|
1997 | *the leading monomial of p2 in p1 |
---|
1998 | */ |
---|
1999 | void pCancelPolyByMonom (poly p1,poly p2,polyset * P,int * SizeOfSet) |
---|
2000 | { |
---|
2001 | int maxPow; |
---|
2002 | poly p,qp,Coeff; |
---|
2003 | |
---|
2004 | if (*P == NULL) |
---|
2005 | { |
---|
2006 | *P = (polyset) Alloc(5*sizeof(poly)); |
---|
2007 | *SizeOfSet = 5; |
---|
2008 | } |
---|
2009 | p = pCopy(p1); |
---|
2010 | while (p != NULL) |
---|
2011 | { |
---|
2012 | qp = p->next; |
---|
2013 | p->next = NULL; |
---|
2014 | maxPow = pGetMaxPower(p,p2); |
---|
2015 | Coeff = pDivByMonom(p,p2); |
---|
2016 | if (maxPow > *SizeOfSet) |
---|
2017 | { |
---|
2018 | pEnlargeSet(P,*SizeOfSet,maxPow+1-*SizeOfSet); |
---|
2019 | *SizeOfSet = maxPow+1; |
---|
2020 | } |
---|
2021 | (*P)[maxPow] = pAdd((*P)[maxPow],Coeff); |
---|
2022 | pDelete(&p); |
---|
2023 | p = qp; |
---|
2024 | } |
---|
2025 | } |
---|
2026 | |
---|
2027 | /*2 |
---|
2028 | *returns the leading monomial of p1 divided by the maximal power of that |
---|
2029 | *of p2 |
---|
2030 | */ |
---|
2031 | poly pDivByMonom (poly p1,poly p2) |
---|
2032 | { |
---|
2033 | int k, i; |
---|
2034 | |
---|
2035 | if (p1 == NULL) return NULL; |
---|
2036 | k = pGetMaxPower(p1,p2); |
---|
2037 | if (k == 0) |
---|
2038 | return pHead(p1); |
---|
2039 | else |
---|
2040 | { |
---|
2041 | number n; |
---|
2042 | poly p = pInit(); |
---|
2043 | |
---|
2044 | p->next = NULL; |
---|
2045 | for (i=1; i<=pVariables; i++) |
---|
2046 | { |
---|
2047 | pSetExp(p,i, pGetExp(p1,i)-k* pGetExp(p2,i)); |
---|
2048 | } |
---|
2049 | nPower(p2->coef,k,&n); |
---|
2050 | pSetCoeff0(p,nDiv(p1->coef,n)); |
---|
2051 | nDelete(&n); |
---|
2052 | pSetm(p); |
---|
2053 | return p; |
---|
2054 | } |
---|
2055 | } |
---|
2056 | /*----------utilities for syzygies--------------*/ |
---|
2057 | poly pTakeOutComp(poly * p, int k) |
---|
2058 | { |
---|
2059 | poly q = *p,qq=NULL,result = NULL; |
---|
2060 | |
---|
2061 | if (q==NULL) return NULL; |
---|
2062 | if (pGetComp(q)==k) |
---|
2063 | { |
---|
2064 | result = q; |
---|
2065 | while ((q!=NULL) && (pGetComp(q)==k)) |
---|
2066 | { |
---|
2067 | pSetComp(q,0); |
---|
2068 | qq = q; |
---|
2069 | pIter(q); |
---|
2070 | } |
---|
2071 | *p = q; |
---|
2072 | pNext(qq) = NULL; |
---|
2073 | } |
---|
2074 | if (q==NULL) return result; |
---|
2075 | if (pGetComp(q) > k) pDecrComp(q); |
---|
2076 | poly pNext_q; |
---|
2077 | while ((pNext_q=pNext(q))!=NULL) |
---|
2078 | { |
---|
2079 | if (pGetComp(pNext_q)==k) |
---|
2080 | { |
---|
2081 | if (result==NULL) |
---|
2082 | { |
---|
2083 | result = pNext_q; |
---|
2084 | qq = result; |
---|
2085 | } |
---|
2086 | else |
---|
2087 | { |
---|
2088 | pNext(qq) = pNext_q; |
---|
2089 | pIter(qq); |
---|
2090 | } |
---|
2091 | pNext(q) = pNext(pNext_q); |
---|
2092 | pNext(qq) =NULL; |
---|
2093 | pSetComp(qq,0); |
---|
2094 | } |
---|
2095 | else |
---|
2096 | { |
---|
2097 | /*pIter(q);*/ q=pNext_q; |
---|
2098 | if (pGetComp(q) > k) pDecrComp(q); |
---|
2099 | } |
---|
2100 | } |
---|
2101 | return result; |
---|
2102 | } |
---|
2103 | |
---|
2104 | poly pTakeOutComp1(poly * p, int k) |
---|
2105 | { |
---|
2106 | poly q = *p; |
---|
2107 | |
---|
2108 | if (q==NULL) return NULL; |
---|
2109 | |
---|
2110 | poly qq=NULL,result = NULL; |
---|
2111 | |
---|
2112 | if (pGetComp(q)==k) |
---|
2113 | { |
---|
2114 | result = q; /* *p */ |
---|
2115 | while ((q!=NULL) && (pGetComp(q)==k)) |
---|
2116 | { |
---|
2117 | pSetComp(q,0); |
---|
2118 | qq = q; |
---|
2119 | pIter(q); |
---|
2120 | } |
---|
2121 | *p = q; |
---|
2122 | pNext(qq) = NULL; |
---|
2123 | } |
---|
2124 | if (q==NULL) return result; |
---|
2125 | // if (pGetComp(q) > k) pGetComp(q)--; |
---|
2126 | while (pNext(q)!=NULL) |
---|
2127 | { |
---|
2128 | if (pGetComp(pNext(q))==k) |
---|
2129 | { |
---|
2130 | if (result==NULL) |
---|
2131 | { |
---|
2132 | result = pNext(q); |
---|
2133 | qq = result; |
---|
2134 | } |
---|
2135 | else |
---|
2136 | { |
---|
2137 | pNext(qq) = pNext(q); |
---|
2138 | pIter(qq); |
---|
2139 | } |
---|
2140 | pNext(q) = pNext(pNext(q)); |
---|
2141 | pNext(qq) =NULL; |
---|
2142 | pSetComp(qq,0); |
---|
2143 | } |
---|
2144 | else |
---|
2145 | { |
---|
2146 | pIter(q); |
---|
2147 | // if (pGetComp(q) > k) pGetComp(q)--; |
---|
2148 | } |
---|
2149 | } |
---|
2150 | return result; |
---|
2151 | } |
---|
2152 | |
---|
2153 | void pDeleteComp(poly * p,int k) |
---|
2154 | { |
---|
2155 | poly q; |
---|
2156 | |
---|
2157 | while ((*p!=NULL) && (pGetComp(*p)==k)) pDelete1(p); |
---|
2158 | if (*p==NULL) return; |
---|
2159 | q = *p; |
---|
2160 | if (pGetComp(q)>k) pDecrComp(q); |
---|
2161 | while (pNext(q)!=NULL) |
---|
2162 | { |
---|
2163 | if (pGetComp(pNext(q))==k) |
---|
2164 | pDelete1(&(pNext(q))); |
---|
2165 | else |
---|
2166 | { |
---|
2167 | pIter(q); |
---|
2168 | if (pGetComp(q)>k) pDecrComp(q); |
---|
2169 | } |
---|
2170 | } |
---|
2171 | } |
---|
2172 | /*----------end of utilities for syzygies--------------*/ |
---|
2173 | |
---|
2174 | /*2 |
---|
2175 | * pair has no common factor ? or is no polynomial |
---|
2176 | */ |
---|
2177 | BOOLEAN pHasNotCF(poly p1, poly p2) |
---|
2178 | { |
---|
2179 | #ifdef SRING |
---|
2180 | if (pSRING) |
---|
2181 | return FALSE; |
---|
2182 | #endif |
---|
2183 | |
---|
2184 | if (pGetComp(p1) > 0 || pGetComp(p2) > 0) |
---|
2185 | return FALSE; |
---|
2186 | int i = 1; |
---|
2187 | loop |
---|
2188 | { |
---|
2189 | if ((pGetExp(p1, i) > 0) && (pGetExp(p2, i) > 0)) return FALSE; |
---|
2190 | if (i == pVariables) return TRUE; |
---|
2191 | i++; |
---|
2192 | } |
---|
2193 | } |
---|
2194 | |
---|
2195 | |
---|
2196 | /*2 |
---|
2197 | *should return 1 if p divides q and p<q, |
---|
2198 | * -1 if q divides p and q<p |
---|
2199 | * 0 otherwise |
---|
2200 | */ |
---|
2201 | int pDivComp(poly p, poly q) |
---|
2202 | { |
---|
2203 | if (pGetComp(p) == pGetComp(q)) |
---|
2204 | { |
---|
2205 | int i=pVariables; |
---|
2206 | long d; |
---|
2207 | BOOLEAN a=FALSE, b=FALSE; |
---|
2208 | for (; i>0; i--) |
---|
2209 | { |
---|
2210 | d = pGetExpDiff(p, q, i); |
---|
2211 | if (d) |
---|
2212 | { |
---|
2213 | if (d < 0) |
---|
2214 | { |
---|
2215 | if (b) return 0; |
---|
2216 | a =TRUE; |
---|
2217 | } |
---|
2218 | else |
---|
2219 | { |
---|
2220 | if (a) return 0; |
---|
2221 | b = TRUE; |
---|
2222 | } |
---|
2223 | } |
---|
2224 | } |
---|
2225 | if (a) return 1; |
---|
2226 | else if (b) return -1; |
---|
2227 | } |
---|
2228 | return 0; |
---|
2229 | } |
---|
2230 | /*2 |
---|
2231 | *divides p1 by its leading monomial |
---|
2232 | */ |
---|
2233 | void pNorm(poly p1) |
---|
2234 | { |
---|
2235 | poly h; |
---|
2236 | number k, c; |
---|
2237 | |
---|
2238 | if (p1!=NULL) |
---|
2239 | { |
---|
2240 | if (!nIsOne(pGetCoeff(p1))) |
---|
2241 | { |
---|
2242 | nNormalize(pGetCoeff(p1)); |
---|
2243 | k=pGetCoeff(p1); |
---|
2244 | c = nInit(1); |
---|
2245 | pSetCoeff0(p1,c); |
---|
2246 | h = pNext(p1); |
---|
2247 | while (h!=NULL) |
---|
2248 | { |
---|
2249 | c=nDiv(pGetCoeff(h),k); |
---|
2250 | if (!nIsOne(c)) nNormalize(c); |
---|
2251 | pSetCoeff(h,c); |
---|
2252 | pIter(h); |
---|
2253 | } |
---|
2254 | nDelete(&k); |
---|
2255 | } |
---|
2256 | else |
---|
2257 | { |
---|
2258 | h = pNext(p1); |
---|
2259 | while (h!=NULL) |
---|
2260 | { |
---|
2261 | nNormalize(pGetCoeff(h)); |
---|
2262 | pIter(h); |
---|
2263 | } |
---|
2264 | } |
---|
2265 | } |
---|
2266 | } |
---|
2267 | |
---|
2268 | /*2 |
---|
2269 | *normalize all coeffizients |
---|
2270 | */ |
---|
2271 | void pNormalize(poly p) |
---|
2272 | { |
---|
2273 | while (p!=NULL) |
---|
2274 | { |
---|
2275 | nTest(pGetCoeff(p)); |
---|
2276 | nNormalize(pGetCoeff(p)); |
---|
2277 | pIter(p); |
---|
2278 | } |
---|
2279 | } |
---|
2280 | |
---|
2281 | /*3 |
---|
2282 | * substitute the n-th variable by 1 in p |
---|
2283 | * destroy p |
---|
2284 | */ |
---|
2285 | static poly pSubst1 (poly p,int n) |
---|
2286 | { |
---|
2287 | poly qq,result = NULL; |
---|
2288 | |
---|
2289 | while (p != NULL) |
---|
2290 | { |
---|
2291 | qq = p; |
---|
2292 | pIter(p); |
---|
2293 | qq->next = NULL; |
---|
2294 | pSetExp(qq,n,0); |
---|
2295 | pSetm(qq); |
---|
2296 | result = pAdd(result,qq); |
---|
2297 | } |
---|
2298 | return result; |
---|
2299 | } |
---|
2300 | |
---|
2301 | /*2 |
---|
2302 | * substitute the n-th variable by e in p |
---|
2303 | * destroy p |
---|
2304 | */ |
---|
2305 | poly pSubst(poly p, int n, poly e) |
---|
2306 | { |
---|
2307 | if ((e!=NULL)&&(pIsConstant(e))&&(nIsOne(pGetCoeff(e)))) |
---|
2308 | return pSubst1(p,n); |
---|
2309 | |
---|
2310 | int exponent,i; |
---|
2311 | poly h, res, m; |
---|
2312 | Exponent_t *me,*ee; |
---|
2313 | number nu,nu1; |
---|
2314 | |
---|
2315 | me=(Exponent_t *)Alloc((pVariables+1)*sizeof(Exponent_t)); |
---|
2316 | ee=(Exponent_t *)Alloc((pVariables+1)*sizeof(Exponent_t)); |
---|
2317 | if (e!=NULL) pGetExpV(e,ee); |
---|
2318 | res=NULL; |
---|
2319 | h=p; |
---|
2320 | while (h!=NULL) |
---|
2321 | { |
---|
2322 | if ((e!=NULL) || (pGetExp(h,n)==0)) |
---|
2323 | { |
---|
2324 | m=pHead(h); |
---|
2325 | pGetExpV(m,me); |
---|
2326 | exponent=me[n]; |
---|
2327 | me[n]=0; |
---|
2328 | for(i=1;i<=pVariables;i++) |
---|
2329 | me[i]+=exponent*ee[i]; |
---|
2330 | pSetExpV(m,me); |
---|
2331 | if (e!=NULL) |
---|
2332 | { |
---|
2333 | nPower(pGetCoeff(e),exponent,&nu); |
---|
2334 | nu1=nMult(pGetCoeff(m),nu); |
---|
2335 | nDelete(&nu); |
---|
2336 | pSetCoeff(m,nu1); |
---|
2337 | } |
---|
2338 | res=pAdd(res,m); |
---|
2339 | } |
---|
2340 | pDelete1(&h); |
---|
2341 | } |
---|
2342 | Free((ADDRESS)me,(pVariables+1)*sizeof(Exponent_t)); |
---|
2343 | Free((ADDRESS)ee,(pVariables+1)*sizeof(Exponent_t)); |
---|
2344 | return res; |
---|
2345 | } |
---|
2346 | |
---|
2347 | BOOLEAN pCompareChain (poly p,poly p1,poly p2,poly lcm) |
---|
2348 | { |
---|
2349 | int k, j; |
---|
2350 | |
---|
2351 | if (lcm==NULL) return FALSE; |
---|
2352 | |
---|
2353 | for (j=pVariables; j; j--) |
---|
2354 | if ( pGetExp(p,j) > pGetExp(lcm,j)) return FALSE; |
---|
2355 | if ( pGetComp(p) != pGetComp(lcm)) return FALSE; |
---|
2356 | for (j=pVariables; j; j--) |
---|
2357 | { |
---|
2358 | if (pGetExp(p1,j)!=pGetExp(lcm,j)) |
---|
2359 | { |
---|
2360 | if (pGetExp(p,j)!=pGetExp(lcm,j)) |
---|
2361 | { |
---|
2362 | for (k=pVariables; k>j; k--) { |
---|
2363 | if ((pGetExp(p,k)!=pGetExp(lcm,k)) |
---|
2364 | && (pGetExp(p2,k)!=pGetExp(lcm,k))) |
---|
2365 | return TRUE; |
---|
2366 | } |
---|
2367 | for (k=j-1; k; k--) |
---|
2368 | { |
---|
2369 | if ((pGetExp(p,k)!=pGetExp(lcm,k)) |
---|
2370 | && (pGetExp(p2,k)!=pGetExp(lcm,k))) |
---|
2371 | return TRUE; |
---|
2372 | } |
---|
2373 | return FALSE; |
---|
2374 | } |
---|
2375 | } |
---|
2376 | else if (pGetExp(p2,j)!=pGetExp(lcm,j)) |
---|
2377 | { |
---|
2378 | if (pGetExp(p,j)!=pGetExp(lcm,j)) |
---|
2379 | { |
---|
2380 | for (k=pVariables; k>j; k--) |
---|
2381 | { |
---|
2382 | if ((pGetExp(p,k)!=pGetExp(lcm,k)) |
---|
2383 | && (pGetExp(p1,k)!=pGetExp(lcm,k))) |
---|
2384 | return TRUE; |
---|
2385 | } |
---|
2386 | for (k=j-1; k; k--) |
---|
2387 | { |
---|
2388 | if ((pGetExp(p,k)!=pGetExp(lcm,k)) |
---|
2389 | && (pGetExp(p1,k)!=pGetExp(lcm,k))) |
---|
2390 | return TRUE; |
---|
2391 | } |
---|
2392 | return FALSE; |
---|
2393 | } |
---|
2394 | } |
---|
2395 | } |
---|
2396 | return FALSE; |
---|
2397 | } |
---|
2398 | |
---|
2399 | int pWeight(int i) |
---|
2400 | { |
---|
2401 | if ((firstwv==NULL) || (i>firstBlockEnds)) |
---|
2402 | { |
---|
2403 | return 1; |
---|
2404 | } |
---|
2405 | return firstwv[i-1]; |
---|
2406 | } |
---|
2407 | |
---|
2408 | |
---|