1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: spolys0.cc,v 1.10 1998-03-18 14:28:54 obachman Exp $ */ |
---|
5 | |
---|
6 | /* |
---|
7 | * ABSTRACT - s-polynomials and reduction in general |
---|
8 | */ |
---|
9 | |
---|
10 | #include <string.h> |
---|
11 | #include "mod2.h" |
---|
12 | #include "tok.h" |
---|
13 | #include "mmemory.h" |
---|
14 | #include "numbers.h" |
---|
15 | #include "polys.h" |
---|
16 | #include "spolys0.h" |
---|
17 | |
---|
18 | /*0 implementation*/ |
---|
19 | |
---|
20 | /* |
---|
21 | * input - output: a, b |
---|
22 | * returns: |
---|
23 | * a := a/gcd(a,b), b := b/gcd(a,b) |
---|
24 | * and return value |
---|
25 | * 0 -> a != 1, b != 1 |
---|
26 | * 1 -> a == 1, b != 1 |
---|
27 | * 2 -> a != 1, b == 1 |
---|
28 | * 3 -> a == 1, b == 1 |
---|
29 | * this value is used to control the spolys |
---|
30 | */ |
---|
31 | static int spCheckCoeff(number *a, number *b) |
---|
32 | { |
---|
33 | int c = 0; |
---|
34 | number an = *a, bn = *b; |
---|
35 | nTest(an); |
---|
36 | nTest(bn); |
---|
37 | number cn = nGcd(an, bn); |
---|
38 | |
---|
39 | if(nIsOne(cn)) |
---|
40 | { |
---|
41 | an = nCopy(an); |
---|
42 | bn = nCopy(bn); |
---|
43 | } |
---|
44 | else |
---|
45 | { |
---|
46 | an = nIntDiv(an, cn); |
---|
47 | bn = nIntDiv(bn, cn); |
---|
48 | } |
---|
49 | nDelete(&cn); |
---|
50 | if (nIsOne(an)) |
---|
51 | { |
---|
52 | c = 1; |
---|
53 | } |
---|
54 | if (nIsOne(bn)) |
---|
55 | { |
---|
56 | c += 2; |
---|
57 | } |
---|
58 | *a = an; |
---|
59 | *b = bn; |
---|
60 | return c; |
---|
61 | } |
---|
62 | |
---|
63 | /*2 |
---|
64 | * transforms a1 to a polynomial |
---|
65 | */ |
---|
66 | void spModuleToPoly(poly a1) |
---|
67 | { |
---|
68 | #ifdef PDEBUG |
---|
69 | poly t=a1; |
---|
70 | pTest(t); |
---|
71 | #endif |
---|
72 | do |
---|
73 | { |
---|
74 | pSetComp(a1,0); |
---|
75 | pIter(a1); |
---|
76 | } while (a1!=NULL); |
---|
77 | #ifdef PDEBUG |
---|
78 | pTest(t); |
---|
79 | #endif |
---|
80 | } |
---|
81 | |
---|
82 | /*2 |
---|
83 | * assume m = L(m) and Lc(m) = 1 |
---|
84 | * pNext(m) = result = p*m |
---|
85 | * do not destroy p |
---|
86 | */ |
---|
87 | static void spGMultCopy0(poly p, poly m, poly spNoether) |
---|
88 | { |
---|
89 | poly a, b; |
---|
90 | |
---|
91 | a = m; |
---|
92 | if (spNoether==NULL) |
---|
93 | { |
---|
94 | do |
---|
95 | { |
---|
96 | a = pNext(a) = pNew(); |
---|
97 | pCopyAddFast(a,p,m); |
---|
98 | pSetCoeff0(a,nCopy(pGetCoeff(a))); |
---|
99 | pIter(p); |
---|
100 | } |
---|
101 | while (p!=NULL); |
---|
102 | pNext(a) = NULL; |
---|
103 | return; |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | do |
---|
108 | { |
---|
109 | b = pNext(a) = pNew(); |
---|
110 | spMemcpy(b,p); |
---|
111 | spMonAdd(b,m); |
---|
112 | if (pComp0(b, spNoether) == -1) |
---|
113 | { |
---|
114 | pFree1(b); |
---|
115 | pNext(a) = NULL; |
---|
116 | return; |
---|
117 | } |
---|
118 | a = b; |
---|
119 | pSetCoeff0(a,nCopy(pGetCoeff(a))); |
---|
120 | pIter(p); |
---|
121 | } while (p!=NULL); |
---|
122 | pNext(a) = NULL; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | /*2 |
---|
127 | * assume m = L(m) and Lc(m) = -1 |
---|
128 | * pNext(n) = result = p*m |
---|
129 | * do not destroy p |
---|
130 | */ |
---|
131 | static void spGMultCopy1(poly p, poly m, poly n,poly spNoether) |
---|
132 | { |
---|
133 | poly a, b; |
---|
134 | |
---|
135 | a = n; |
---|
136 | if (spNoether==NULL) |
---|
137 | { |
---|
138 | do |
---|
139 | { |
---|
140 | number tmp; |
---|
141 | a = pNext(a) = pNew(); |
---|
142 | spMemcpy(a,p); |
---|
143 | spMonAdd(a,m); |
---|
144 | tmp=nCopy(pGetCoeff(a)); |
---|
145 | tmp=nNeg(tmp); |
---|
146 | pSetCoeff0(a,tmp); |
---|
147 | pIter(p); |
---|
148 | } |
---|
149 | while (p!=NULL); |
---|
150 | pNext(a) = NULL; |
---|
151 | return; |
---|
152 | } |
---|
153 | else |
---|
154 | { |
---|
155 | do |
---|
156 | { |
---|
157 | b = pNext(a) = pNew(); |
---|
158 | spMemcpy(b,p); |
---|
159 | spMonAdd(b,m); |
---|
160 | if (pComp(b, spNoether) == -1) |
---|
161 | { |
---|
162 | pFree1(b); |
---|
163 | pNext(a) = NULL; |
---|
164 | return; |
---|
165 | } |
---|
166 | a = b; |
---|
167 | number tmp; |
---|
168 | tmp=nCopy(pGetCoeff(a)); |
---|
169 | tmp=nNeg(tmp); |
---|
170 | pSetCoeff0(a,tmp); |
---|
171 | pIter(p); |
---|
172 | } |
---|
173 | while (p!=NULL); |
---|
174 | pNext(a) = NULL; |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | /*2 |
---|
179 | * assume m = L(m) and Lc(m) = 1 |
---|
180 | * pNext(m) = result = a2-a1*m |
---|
181 | * do not destroy a1, but a2 |
---|
182 | */ |
---|
183 | static void spGSpolyLoop1(poly a1, poly a2, poly m, poly spNoether) |
---|
184 | { |
---|
185 | poly a, b, s; |
---|
186 | number tb; |
---|
187 | int c; |
---|
188 | |
---|
189 | if (a2==NULL) |
---|
190 | { |
---|
191 | spGMultCopy1(a1, m, m,spNoether); |
---|
192 | return; |
---|
193 | } |
---|
194 | a = m; |
---|
195 | b = pNew(); |
---|
196 | spMemcpy(b,a1); |
---|
197 | spMonAdd(b,m); |
---|
198 | loop |
---|
199 | { |
---|
200 | c = pComp0(b, a2); |
---|
201 | if (c == 1) |
---|
202 | { |
---|
203 | number tmp=nCopy(pGetCoeff(b)); |
---|
204 | tmp=nNeg(tmp); |
---|
205 | pSetCoeff0(b,tmp); |
---|
206 | a = pNext(a) = b; |
---|
207 | pIter(a1); |
---|
208 | if (a1!=NULL) |
---|
209 | { |
---|
210 | b = pNew(); |
---|
211 | spMemcpy(b,a1); |
---|
212 | spMonAdd(b,m); |
---|
213 | } |
---|
214 | else |
---|
215 | { |
---|
216 | pNext(a) = a2; |
---|
217 | return; |
---|
218 | } |
---|
219 | } |
---|
220 | else if (c == -1) |
---|
221 | { |
---|
222 | a = pNext(a) = a2; |
---|
223 | pIter(a2); |
---|
224 | if (a2==NULL) |
---|
225 | { |
---|
226 | pFree1(b); |
---|
227 | spGMultCopy1(a1, m, a,spNoether); |
---|
228 | return; |
---|
229 | } |
---|
230 | } |
---|
231 | else |
---|
232 | { |
---|
233 | tb = pGetCoeff(a2); |
---|
234 | if (!nEqual(tb,pGetCoeff(a1))) |
---|
235 | { |
---|
236 | tb = nSub(tb,pGetCoeff(a1)); |
---|
237 | pSetCoeff(a2,tb); |
---|
238 | a = pNext(a) = a2; |
---|
239 | pIter(a2); |
---|
240 | } |
---|
241 | else |
---|
242 | { |
---|
243 | s = a2; |
---|
244 | pIter(a2); |
---|
245 | nDelete(&tb); |
---|
246 | pFree1(s); |
---|
247 | } |
---|
248 | pIter(a1); |
---|
249 | if (a1==NULL) |
---|
250 | { |
---|
251 | pFree1(b); |
---|
252 | pNext(a) = a2; |
---|
253 | return; |
---|
254 | } |
---|
255 | if (a2==NULL) |
---|
256 | { |
---|
257 | pFree1(b); |
---|
258 | spGMultCopy1(a1, m, a,spNoether); |
---|
259 | return; |
---|
260 | } |
---|
261 | spMemcpy(b,a1); |
---|
262 | spMonAdd(b,m); |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | /*2 |
---|
268 | * assume m = L(m) and Lc(m) = exp |
---|
269 | * pNext(n) = result = p*m |
---|
270 | * do not destroy p |
---|
271 | */ |
---|
272 | void spGMultCopyX(poly p, poly m, poly n, number exp, poly spNoether) |
---|
273 | { |
---|
274 | poly a, b; |
---|
275 | |
---|
276 | a = n; |
---|
277 | if (spNoether==NULL) |
---|
278 | { |
---|
279 | do |
---|
280 | { |
---|
281 | a = pNext(a) = pNew(); |
---|
282 | spMemcpy(a,p); |
---|
283 | spMonAdd(a,m); |
---|
284 | pSetCoeff0(a,nMult(pGetCoeff(p),exp)); |
---|
285 | pIter(p); |
---|
286 | } while (p!=NULL); |
---|
287 | pNext(a) = NULL; |
---|
288 | return; |
---|
289 | } |
---|
290 | else |
---|
291 | { |
---|
292 | do |
---|
293 | { |
---|
294 | b = pNext(a) = pNew(); |
---|
295 | spMemcpy(b,p); |
---|
296 | spMonAdd(b,m); |
---|
297 | if (pComp(b, spNoether) == -1) |
---|
298 | { |
---|
299 | pFree1(b); |
---|
300 | pNext(a) = NULL; |
---|
301 | return; |
---|
302 | } |
---|
303 | a = b; |
---|
304 | pSetCoeff0(a,nMult(pGetCoeff(p),exp)); |
---|
305 | pIter(p); |
---|
306 | } while (p!=NULL); |
---|
307 | pNext(a) = NULL; |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | /*2 |
---|
312 | * assume m = L(m) |
---|
313 | * pNext(m) = result = a2-a1*m |
---|
314 | * do not destroy a1, but a2 |
---|
315 | */ |
---|
316 | void spGSpolyLoop(poly a1, poly a2, poly m,poly spNoether) |
---|
317 | { |
---|
318 | poly a, b, s; |
---|
319 | number tm = pGetCoeff(m); |
---|
320 | number tneg,tb,t; |
---|
321 | int c; |
---|
322 | |
---|
323 | tneg = nCopy(tm); |
---|
324 | tneg = nNeg(tneg); |
---|
325 | if (a2==NULL) |
---|
326 | { |
---|
327 | spGMultCopyX(a1, m, m, tneg,spNoether); |
---|
328 | nDelete(&tneg); |
---|
329 | return; |
---|
330 | } |
---|
331 | a = m; |
---|
332 | b = pNew(); |
---|
333 | spMemcpy(b,a1); |
---|
334 | spMonAdd(b,m); |
---|
335 | loop |
---|
336 | { |
---|
337 | c = pComp0(b, a2); |
---|
338 | if (c == 1) |
---|
339 | { |
---|
340 | pSetCoeff0(b,nMult(pGetCoeff(a1),tneg)); |
---|
341 | a = pNext(a) = b; |
---|
342 | pIter(a1); |
---|
343 | if (a1!=NULL) |
---|
344 | { |
---|
345 | b = pNew(); |
---|
346 | spMemcpy(b,a1); |
---|
347 | spMonAdd(b,m); |
---|
348 | } |
---|
349 | else |
---|
350 | { |
---|
351 | pNext(a) = a2; |
---|
352 | nDelete(&tneg); |
---|
353 | return; |
---|
354 | } |
---|
355 | } |
---|
356 | else if (c == -1) |
---|
357 | { |
---|
358 | a = pNext(a) = a2; |
---|
359 | pIter(a2); |
---|
360 | if (a2==NULL) |
---|
361 | { |
---|
362 | pFree1(b); |
---|
363 | spGMultCopyX(a1, m, a, tneg,spNoether); |
---|
364 | nDelete(&tneg); |
---|
365 | return; |
---|
366 | } |
---|
367 | } |
---|
368 | else |
---|
369 | { |
---|
370 | t = pGetCoeff(a2); |
---|
371 | tb = nMult(pGetCoeff(a1),tm); |
---|
372 | if (!nEqual(t,tb)) |
---|
373 | { |
---|
374 | t = nSub(t,tb); |
---|
375 | pSetCoeff(a2,t); |
---|
376 | a = pNext(a) = a2; |
---|
377 | pIter(a2); |
---|
378 | } |
---|
379 | else |
---|
380 | { |
---|
381 | s = a2; |
---|
382 | pIter(a2); |
---|
383 | nDelete(&t); |
---|
384 | pFree1(s); |
---|
385 | } |
---|
386 | nDelete(&tb); |
---|
387 | pIter(a1); |
---|
388 | if (a1==NULL) |
---|
389 | { |
---|
390 | pFree1(b); |
---|
391 | pNext(a) = a2; |
---|
392 | nDelete(&tneg); |
---|
393 | return; |
---|
394 | } |
---|
395 | if (a2==NULL) |
---|
396 | { |
---|
397 | pFree1(b); |
---|
398 | spGMultCopyX(a1, m, a, tneg,spNoether); |
---|
399 | nDelete(&tneg); |
---|
400 | return; |
---|
401 | } |
---|
402 | spMemcpy(b,a1); |
---|
403 | spMonAdd(b,m); |
---|
404 | } |
---|
405 | } |
---|
406 | } |
---|
407 | |
---|
408 | /*2 |
---|
409 | * reduction of p2 with p1 |
---|
410 | * do not destroy p1, but p2 |
---|
411 | */ |
---|
412 | poly spGSpolyRed(poly p1, poly p2,poly spNoether, spSpolyLoopProc spSpolyLoop) |
---|
413 | { |
---|
414 | poly a1 = pNext(p1), a2 = pNext(p2); |
---|
415 | number an = pGetCoeff(p1), bn = pGetCoeff(p2); |
---|
416 | int ct = spCheckCoeff(&an, &bn); |
---|
417 | |
---|
418 | pSetCoeff(p2,bn); |
---|
419 | if(a1==NULL) |
---|
420 | { |
---|
421 | nDelete(&an); |
---|
422 | nDelete(&bn); |
---|
423 | pFree1(p2); |
---|
424 | return a2; |
---|
425 | } |
---|
426 | if ((ct == 0) || (ct == 2)) |
---|
427 | { |
---|
428 | pMultN(a2, an); |
---|
429 | } |
---|
430 | nDelete(&an); |
---|
431 | BOOLEAN reset_vec=FALSE; |
---|
432 | if (pGetComp(p1) != pGetComp(p2)) |
---|
433 | { |
---|
434 | pSetCompP(a1,pGetComp(p2)); |
---|
435 | reset_vec=TRUE; |
---|
436 | } |
---|
437 | spMonSub(p2,p1); |
---|
438 | if (ct < 2) |
---|
439 | { |
---|
440 | spGSpolyLoop(a1, a2, p2,spNoether); |
---|
441 | } |
---|
442 | else |
---|
443 | { |
---|
444 | spGSpolyLoop1(a1, a2, p2,spNoether); |
---|
445 | } |
---|
446 | a2 = pNext(p2); |
---|
447 | if (reset_vec) |
---|
448 | { |
---|
449 | spModuleToPoly(a1); |
---|
450 | } |
---|
451 | nDelete(&bn); |
---|
452 | pFree1(p2); |
---|
453 | return a2; |
---|
454 | } |
---|
455 | |
---|
456 | /*2 |
---|
457 | * reduction of tail(q) with p1 |
---|
458 | * lead(p1) divides lead(pNext(q2)) and pNext(q2) is reduced |
---|
459 | * do not destroy p1, but tail(q) |
---|
460 | */ |
---|
461 | void spGSpolyTail(poly p1, poly q, poly q2, poly spNoether, |
---|
462 | spSpolyLoopProc spSpolyLoop) |
---|
463 | { |
---|
464 | number t; |
---|
465 | poly m, h; |
---|
466 | poly a1 = pNext(p1), p2 = pNext(q2), a2 = pNext(p2); |
---|
467 | number an = pGetCoeff(p1), bn = pGetCoeff(p2); |
---|
468 | int ct = spCheckCoeff(&an, &bn); |
---|
469 | BOOLEAN reset_vec=FALSE; |
---|
470 | |
---|
471 | if(a1==NULL) |
---|
472 | { |
---|
473 | nDelete(&an); |
---|
474 | nDelete(&bn); |
---|
475 | nDelete(&pGetCoeff(p2)); |
---|
476 | pFree1(p2); |
---|
477 | pNext(q2) = a2; |
---|
478 | return; |
---|
479 | } |
---|
480 | if (p1 != q) |
---|
481 | { |
---|
482 | m = p2; |
---|
483 | pSetCoeff(m,bn); |
---|
484 | } |
---|
485 | else |
---|
486 | { |
---|
487 | m = pNew(); |
---|
488 | spMemcpy(m,p2); |
---|
489 | pSetCoeff0(m,bn); |
---|
490 | a2 = pCopy(a2); |
---|
491 | } |
---|
492 | if ((ct == 0) || (ct == 2)) |
---|
493 | { |
---|
494 | pMultN(a2, an); |
---|
495 | } |
---|
496 | if ((pGetComp(p1) != pGetComp(p2)) |
---|
497 | && (pGetComp(p1)==0)) |
---|
498 | { |
---|
499 | pSetCompP(a1,pGetComp(p2)); |
---|
500 | reset_vec=TRUE; |
---|
501 | } |
---|
502 | spMonSub(m,p1); |
---|
503 | if (ct < 2) |
---|
504 | { |
---|
505 | spGSpolyLoop(a1, a2, m,spNoether); |
---|
506 | } |
---|
507 | else |
---|
508 | { |
---|
509 | spGSpolyLoop1(a1, a2, m,spNoether); |
---|
510 | } |
---|
511 | if ((ct == 0) || (ct == 2)) |
---|
512 | { |
---|
513 | h = q; |
---|
514 | do |
---|
515 | { |
---|
516 | t = nMult(pGetCoeff(h),an); |
---|
517 | pSetCoeff(h,t); |
---|
518 | pIter(h); |
---|
519 | } |
---|
520 | while (h != p2); |
---|
521 | } |
---|
522 | h = pNext(m); |
---|
523 | nDelete(&an); |
---|
524 | nDelete(&bn); |
---|
525 | pFree1(m); |
---|
526 | pNext(q2) = h; |
---|
527 | if (reset_vec) |
---|
528 | spModuleToPoly(a1); |
---|
529 | if (p1 == q) |
---|
530 | { |
---|
531 | pDelete(&p2); |
---|
532 | } |
---|
533 | } |
---|
534 | |
---|
535 | /*2 |
---|
536 | * reduction of p2 with p1 |
---|
537 | * do not destroy p1 and p2 |
---|
538 | */ |
---|
539 | poly spGSpolyRedNew(poly p1, poly p2,poly spNoether, |
---|
540 | spSpolyLoopProc spSpolyLoop) |
---|
541 | { |
---|
542 | poly m; |
---|
543 | poly a1 = pNext(p1), a2 = pNext(p2); |
---|
544 | number an = pGetCoeff(p1), bn = pGetCoeff(p2); |
---|
545 | int ct = spCheckCoeff(&an, &bn); |
---|
546 | |
---|
547 | if(a1==NULL) |
---|
548 | { |
---|
549 | nDelete(&an); |
---|
550 | nDelete(&bn); |
---|
551 | return pCopy(a2); |
---|
552 | } |
---|
553 | if ((ct == 0) || (ct == 2)) |
---|
554 | { |
---|
555 | a2 = pMultCopyN(a2,an); |
---|
556 | } |
---|
557 | else |
---|
558 | { |
---|
559 | a2 = pCopy(a2); |
---|
560 | } |
---|
561 | pTest(a2); |
---|
562 | pTest(p2); |
---|
563 | nDelete(&an); |
---|
564 | BOOLEAN reset_vec=FALSE; |
---|
565 | if (pGetComp(p1) != pGetComp(p2)) |
---|
566 | { |
---|
567 | pSetCompP(a1,pGetComp(p2)); |
---|
568 | reset_vec=TRUE; |
---|
569 | } |
---|
570 | m = pNew(); |
---|
571 | spMemcpy(m,p2); |
---|
572 | spMonSub(m,p1); |
---|
573 | if (ct < 2) |
---|
574 | { |
---|
575 | pSetCoeff0(m,bn); |
---|
576 | spGSpolyLoop(a1, a2, m,spNoether); |
---|
577 | } |
---|
578 | else |
---|
579 | { |
---|
580 | spGSpolyLoop1(a1, a2, m,spNoether); |
---|
581 | } |
---|
582 | a2 = pNext(m); |
---|
583 | pTest(a2); |
---|
584 | pTest(p2); |
---|
585 | if (reset_vec) |
---|
586 | { |
---|
587 | spModuleToPoly(a1); |
---|
588 | } |
---|
589 | nDelete(&bn); |
---|
590 | pFree1(m); |
---|
591 | pTest(a2); |
---|
592 | pTest(p2); |
---|
593 | return a2; |
---|
594 | } |
---|
595 | |
---|
596 | /*2 |
---|
597 | * creates the S-polynomial of p1 and p2 |
---|
598 | * do not destroy p1 and p2 |
---|
599 | */ |
---|
600 | poly spGSpolyCreate(poly p1, poly p2,poly spNoether) |
---|
601 | { |
---|
602 | Exponent_t x; |
---|
603 | poly m, b; |
---|
604 | poly a1 = pNext(p1), a2 = pNext(p2); |
---|
605 | number an = pGetCoeff(p1), bn = pGetCoeff(p2); |
---|
606 | int co=0, ct = spCheckCoeff(&an, &bn); |
---|
607 | |
---|
608 | if (pGetComp(p1)!=pGetComp(p2)) |
---|
609 | { |
---|
610 | if (pGetComp(p1)==0) |
---|
611 | { |
---|
612 | co=1; |
---|
613 | pSetCompP(p1,pGetComp(p2)); |
---|
614 | } |
---|
615 | else |
---|
616 | { |
---|
617 | co=2; |
---|
618 | pSetCompP(p2,pGetComp(p1)); |
---|
619 | } |
---|
620 | } |
---|
621 | b = pInit(); |
---|
622 | m = pInit(); |
---|
623 | for (int i = pVariables; i; i--) |
---|
624 | { |
---|
625 | x = pGetExp(p1,i) - pGetExp(p2,i); |
---|
626 | if (x > 0) |
---|
627 | { |
---|
628 | pSetExp(b,i,x); |
---|
629 | pSetExp(m,i,0); |
---|
630 | } |
---|
631 | else |
---|
632 | { |
---|
633 | pSetExp(m,i,-x); |
---|
634 | pSetExp(b,i,0); |
---|
635 | } |
---|
636 | } |
---|
637 | pSetm(m); |
---|
638 | pSetm(b); |
---|
639 | if (a2!=NULL) |
---|
640 | { |
---|
641 | if ((ct == 0) || (ct == 2)) |
---|
642 | { |
---|
643 | spGMultCopyX(a2, b, b, an,spNoether); |
---|
644 | } |
---|
645 | else |
---|
646 | { |
---|
647 | spGMultCopy0(a2, b,spNoether); |
---|
648 | } |
---|
649 | a2 = pNext(b); |
---|
650 | } |
---|
651 | nDelete(&an); |
---|
652 | pFree1(b); |
---|
653 | if (a1!=NULL) |
---|
654 | { |
---|
655 | if (ct < 2) |
---|
656 | { |
---|
657 | pSetCoeff0(m,bn); |
---|
658 | spGSpolyLoop(a1, a2, m,spNoether); |
---|
659 | } |
---|
660 | else |
---|
661 | { |
---|
662 | spGSpolyLoop1(a1, a2, m,spNoether); |
---|
663 | } |
---|
664 | a2 = pNext(m); |
---|
665 | } |
---|
666 | nDelete(&bn); |
---|
667 | pFree1(m); |
---|
668 | if (co != 0) |
---|
669 | { |
---|
670 | if (co==1) |
---|
671 | { |
---|
672 | spModuleToPoly(p1); |
---|
673 | } |
---|
674 | else |
---|
675 | { |
---|
676 | spModuleToPoly(p2); |
---|
677 | } |
---|
678 | } |
---|
679 | return a2; |
---|
680 | } |
---|
681 | |
---|
682 | /*2 |
---|
683 | * creates the leading term of the S-polynomial of p1 and p2 |
---|
684 | * do not destroy p1 and p2 |
---|
685 | * remarks: |
---|
686 | * 1. the coefficient is 0 (nNew) |
---|
687 | * 2. pNext is undefined |
---|
688 | */ |
---|
689 | static void bbb() { int i=0; } |
---|
690 | poly spGSpolyShortBba(poly p1, poly p2) |
---|
691 | { |
---|
692 | poly a1 = pNext(p1), a2 = pNext(p2); |
---|
693 | Exponent_t c1=pGetComp(p1),c2=pGetComp(p2); |
---|
694 | Exponent_t c; |
---|
695 | poly m1,m2; |
---|
696 | number t1,t2; |
---|
697 | int cm,i; |
---|
698 | BOOLEAN equal; |
---|
699 | |
---|
700 | if (a1==NULL) |
---|
701 | { |
---|
702 | if(a2!=NULL) |
---|
703 | { |
---|
704 | m2=pInit(); |
---|
705 | x2: |
---|
706 | for (i = pVariables; i; i--) |
---|
707 | { |
---|
708 | c = pGetExpDiff(p1, p2,i); |
---|
709 | if (c>0) |
---|
710 | { |
---|
711 | pSetExp(m2,i,(c+pGetExp(a2,i))); |
---|
712 | } |
---|
713 | else |
---|
714 | { |
---|
715 | pSetExp(m2,i,pGetExp(a2,i)); |
---|
716 | } |
---|
717 | } |
---|
718 | if ((c1==c2)||(c2!=0)) |
---|
719 | { |
---|
720 | pSetComp(m2,pGetComp(a2)); |
---|
721 | } |
---|
722 | else |
---|
723 | { |
---|
724 | pSetComp(m2,c1); |
---|
725 | } |
---|
726 | pSetm(m2); |
---|
727 | nNew(&(pGetCoeff(m2))); |
---|
728 | return m2; |
---|
729 | } |
---|
730 | else |
---|
731 | return NULL; |
---|
732 | } |
---|
733 | if (a2==NULL) |
---|
734 | { |
---|
735 | m1=pInit(); |
---|
736 | x1: |
---|
737 | for (i = pVariables; i; i--) |
---|
738 | { |
---|
739 | c = pGetExpDiff(p2, p1,i); |
---|
740 | if (c>0) |
---|
741 | { |
---|
742 | pSetExp(m1,i,(c+pGetExp(a1,i))); |
---|
743 | } |
---|
744 | else |
---|
745 | { |
---|
746 | pSetExp(m1,i,pGetExp(a1,i)); |
---|
747 | } |
---|
748 | } |
---|
749 | if ((c1==c2)||(c1!=0)) |
---|
750 | { |
---|
751 | pSetComp(m1,pGetComp(a1)); |
---|
752 | } |
---|
753 | else |
---|
754 | { |
---|
755 | pSetComp(m1,c2); |
---|
756 | } |
---|
757 | pSetm(m1); |
---|
758 | nNew(&(pGetCoeff(m1))); |
---|
759 | return m1; |
---|
760 | } |
---|
761 | m1 = pInit(); |
---|
762 | m2 = pInit(); |
---|
763 | loop |
---|
764 | { |
---|
765 | for (i = pVariables; i; i--) |
---|
766 | { |
---|
767 | c = pGetExpDiff(p1, p2,i); |
---|
768 | if (c > 0) |
---|
769 | { |
---|
770 | pSetExp(m2,i,(c+pGetExp(a2,i))); |
---|
771 | pSetExp(m1,i,pGetExp(a1,i)); |
---|
772 | } |
---|
773 | else |
---|
774 | { |
---|
775 | pSetExp(m1,i,(pGetExp(a1,i)-c)); |
---|
776 | pSetExp(m2,i,pGetExp(a2,i)); |
---|
777 | } |
---|
778 | } |
---|
779 | if(c1==c2) |
---|
780 | { |
---|
781 | pSetComp(m1,pGetComp(a1)); |
---|
782 | pSetComp(m2,pGetComp(a2)); |
---|
783 | } |
---|
784 | else |
---|
785 | { |
---|
786 | if(c1!=0) |
---|
787 | { |
---|
788 | pSetComp(m1,pGetComp(a1)); |
---|
789 | pSetComp(m2,c1); |
---|
790 | } |
---|
791 | else |
---|
792 | { |
---|
793 | pSetComp(m2,pGetComp(a2)); |
---|
794 | pSetComp(m1,c2); |
---|
795 | } |
---|
796 | } |
---|
797 | pSetm(m1); |
---|
798 | pSetm(m2); |
---|
799 | cm = pComp0(m1, m2); |
---|
800 | if (cm!=0) |
---|
801 | { |
---|
802 | if(cm==1) |
---|
803 | { |
---|
804 | pFree1(m2); |
---|
805 | nNew(&(pGetCoeff(m1))); |
---|
806 | return m1; |
---|
807 | } |
---|
808 | else |
---|
809 | { |
---|
810 | pFree1(m1); |
---|
811 | nNew(&(pGetCoeff(m2))); |
---|
812 | return m2; |
---|
813 | } |
---|
814 | } |
---|
815 | t1 = nMult(pGetCoeff(a2),pGetCoeff(p1)); |
---|
816 | t2 = nMult(pGetCoeff(a1),pGetCoeff(p2)); |
---|
817 | equal = nEqual(t1,t2); |
---|
818 | nDelete(&t2); |
---|
819 | nDelete(&t1); |
---|
820 | if (!equal) |
---|
821 | { |
---|
822 | pFree1(m2); |
---|
823 | nNew(&(pGetCoeff(m1))); |
---|
824 | return m1; |
---|
825 | } |
---|
826 | pIter(a1); |
---|
827 | pIter(a2); |
---|
828 | if (a2==NULL) |
---|
829 | { |
---|
830 | pFree1(m2); |
---|
831 | if (a1==NULL) |
---|
832 | { |
---|
833 | pFree1(m1); |
---|
834 | return NULL; |
---|
835 | } |
---|
836 | goto x1; |
---|
837 | } |
---|
838 | if (a1==NULL) |
---|
839 | { |
---|
840 | pFree1(m1); |
---|
841 | goto x2; |
---|
842 | } |
---|
843 | } |
---|
844 | } |
---|
845 | |
---|
846 | /*2 |
---|
847 | * creates the last term of the S-polynomial of p1 and p2 |
---|
848 | * in order to compute the ecart |
---|
849 | * do not destroy p1 and p2 |
---|
850 | * remarks: |
---|
851 | * 1. the coefficient is undefined |
---|
852 | * 2. see above |
---|
853 | */ |
---|
854 | /* |
---|
855 | *static poly spGSpolyLast(poly p1, poly p2) |
---|
856 | *{ |
---|
857 | * poly a1 = pNext(p1), a2 = pNext(p2); |
---|
858 | * poly m, b, L1, L2; |
---|
859 | * number ta,tb,t; |
---|
860 | * int c; |
---|
861 | * |
---|
862 | * m = pNew(); |
---|
863 | * b = pNew(); |
---|
864 | * spMemcpy(m,p2); |
---|
865 | * spMonSub(m,p1); |
---|
866 | * pSetComp(b,pGetComp(p1)); |
---|
867 | * if (a2) |
---|
868 | * { |
---|
869 | * while (pNext(a2)) |
---|
870 | * pIter(a2); |
---|
871 | * } |
---|
872 | * if (a1) |
---|
873 | * { |
---|
874 | * while (pNext(a1)) |
---|
875 | * pIter(a1); |
---|
876 | * spMemcpy(b,a1); |
---|
877 | * spMonAdd(b,m); |
---|
878 | * } |
---|
879 | * else |
---|
880 | * { |
---|
881 | * spShort2(b,a2,m); |
---|
882 | * return b; |
---|
883 | * } |
---|
884 | * if(!a2) |
---|
885 | * { |
---|
886 | * spShort1(b,a1,m); |
---|
887 | * return b; |
---|
888 | * } |
---|
889 | * for (; ; ) |
---|
890 | * { |
---|
891 | * c = pComp0(b, a2); |
---|
892 | * if (c == -1) |
---|
893 | * { |
---|
894 | * spShort1(b,a1,m); |
---|
895 | * return b; |
---|
896 | * } |
---|
897 | * else if (c == 1) |
---|
898 | * { |
---|
899 | * spShort2(b,a2,m); |
---|
900 | * return b; |
---|
901 | * } |
---|
902 | * else |
---|
903 | * { |
---|
904 | * if (nIsOne(pGetCoeff(p1))) |
---|
905 | * { |
---|
906 | * if (nIsOne(pGetCoeff(p2))) |
---|
907 | * t = nSub(pGetCoeff(a2), pGetCoeff(a1)); |
---|
908 | * else |
---|
909 | * { |
---|
910 | * ta = nMult(pGetCoeff(a1), pGetCoeff(p2)); |
---|
911 | * t = nSub(ta, pGetCoeff(a2)); |
---|
912 | * nDelete(&ta); |
---|
913 | * } |
---|
914 | * } |
---|
915 | * else |
---|
916 | * { |
---|
917 | * if (nIsOne(pGetCoeff(p2))) |
---|
918 | * { |
---|
919 | * ta = nMult(pGetCoeff(a2), pGetCoeff(p1)); |
---|
920 | * t = nSub(ta, pGetCoeff(a1)); |
---|
921 | * } |
---|
922 | * else |
---|
923 | * { |
---|
924 | * ta = nMult(pGetCoeff(a1), pGetCoeff(p2)); |
---|
925 | * tb = nMult(pGetCoeff(a2), pGetCoeff(p1)); |
---|
926 | * t = nSub(ta, tb); |
---|
927 | * nDelete(&tb); |
---|
928 | * } |
---|
929 | * nDelete(&ta); |
---|
930 | * } |
---|
931 | * if (!nIsZero(t)) |
---|
932 | * { |
---|
933 | * nDelete(&t); |
---|
934 | * spShort1(b,a1,m); |
---|
935 | * return b; |
---|
936 | * } |
---|
937 | * nDelete(&t); |
---|
938 | * if (a1 != pNext(p1)) |
---|
939 | * { |
---|
940 | * L1 = a1; |
---|
941 | * a1 = pNext(p1); |
---|
942 | * while (pNext(a1) != L1) |
---|
943 | * pIter(a1); |
---|
944 | * spMemcpy(b,a1); |
---|
945 | * spMonAdd(b,m); |
---|
946 | * } |
---|
947 | * if (a2 != pNext(p2)) |
---|
948 | * { |
---|
949 | * L2 = a2; |
---|
950 | * a2 = pNext(p2); |
---|
951 | * while (pNext(a2) != L2) |
---|
952 | * pIter(a2); |
---|
953 | * } |
---|
954 | * } |
---|
955 | * } |
---|
956 | *} |
---|
957 | */ |
---|
958 | /*2 |
---|
959 | * creates the leading term of the S-polynomial of p1 and p2 |
---|
960 | * and computes the ecart under the assumtion that |
---|
961 | * the last term of the S-polynomial is the greatest |
---|
962 | * do not destroy p1 and p2 |
---|
963 | */ |
---|
964 | /* |
---|
965 | *poly spGSpolyShortMora(poly p1, poly p2, int *ecart) |
---|
966 | *{ |
---|
967 | * poly p, q; |
---|
968 | * |
---|
969 | * p = spGSpolyShortBba(p1, p2, ecart); |
---|
970 | * *ecart = 0; |
---|
971 | * if(p) |
---|
972 | * { |
---|
973 | * if(spNoether==NULL) |
---|
974 | * { |
---|
975 | * q = spGSpolyLast(p1, p2); |
---|
976 | * *ecart = pFDeg(q) - pFDeg(p); |
---|
977 | * pFree1(q); |
---|
978 | * return p; |
---|
979 | * } |
---|
980 | * if (pComp(p, spNoether) == -1) |
---|
981 | * { |
---|
982 | * pFree1(p); |
---|
983 | * return NULL; |
---|
984 | * } |
---|
985 | * q = spGSpolyLast(p1, p2); |
---|
986 | * if (pComp(q, spNoether) == -1) |
---|
987 | * *ecart = pFDeg(spNoether) - pFDeg(p); |
---|
988 | * else |
---|
989 | * *ecart = pFDeg(q) - pFDeg(p); |
---|
990 | * pFree1(q); |
---|
991 | * } |
---|
992 | * return p; |
---|
993 | *} |
---|
994 | */ |
---|