1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: kspoly.cc,v 1.5 1999-11-05 19:11:07 obachman Exp $ */ |
---|
5 | /* |
---|
6 | * ABSTRACT - Routines for Spoly creation and reductions |
---|
7 | */ |
---|
8 | #include "mod2.h" |
---|
9 | #include "kutil.h" |
---|
10 | #include "polys.h" |
---|
11 | #include "pProcs.h" |
---|
12 | #include "numbers.h" |
---|
13 | |
---|
14 | // Define to enable tests in this file |
---|
15 | #define DEBUG_THIS |
---|
16 | |
---|
17 | #if ! (defined(DEBUG_THIS) || (defined(KDEBUG) && (KDEBUG > 1))) |
---|
18 | #undef assume |
---|
19 | #define assume(x) |
---|
20 | #endif |
---|
21 | |
---|
22 | |
---|
23 | /*************************************************************** |
---|
24 | * |
---|
25 | * Reduces PR with PW |
---|
26 | * Assumes PR != NULL, PW != NULL, Lm(PR) divides Lm(PW) |
---|
27 | * |
---|
28 | ***************************************************************/ |
---|
29 | void ksReducePoly(LObject* PR, |
---|
30 | TObject* PW, |
---|
31 | poly spNoether, |
---|
32 | number *coef) |
---|
33 | { |
---|
34 | assume(kTest_L(PR)); |
---|
35 | assume(kTest_T(PW)); |
---|
36 | |
---|
37 | poly p1 = PR->p; |
---|
38 | poly p2 = PW->p; |
---|
39 | |
---|
40 | assume(p2 != NULL && p1 != NULL && pDivisibleBy(p2, p1)); |
---|
41 | assume(pGetComp(p1) == pGetComp(p2) || |
---|
42 | (pMaxComp(p2) == 0)); |
---|
43 | |
---|
44 | poly a2 = pNext(p2), lm = p1; |
---|
45 | |
---|
46 | p1 = pNext(p1); |
---|
47 | |
---|
48 | if (a2==NULL) |
---|
49 | { |
---|
50 | pDelete1(&lm); |
---|
51 | PR->p = p1; |
---|
52 | if (coef != NULL) *coef = nInit(1); |
---|
53 | return; |
---|
54 | } |
---|
55 | |
---|
56 | if (! nIsOne(pGetCoeff(p2))) |
---|
57 | { |
---|
58 | number bn = pGetCoeff(lm); |
---|
59 | number an = pGetCoeff(p2); |
---|
60 | int ct = ksCheckCoeff(&an, &bn); |
---|
61 | pSetCoeff(lm, bn); |
---|
62 | if ((ct == 0) || (ct == 2)) |
---|
63 | p1 = p_Mult_n(p1, an); |
---|
64 | if (coef != NULL) *coef = an; |
---|
65 | else nDelete(&an); |
---|
66 | } |
---|
67 | else |
---|
68 | { |
---|
69 | if (coef != NULL) *coef = nInit(1); |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | pMonSubFrom(lm, p2); |
---|
74 | |
---|
75 | PR->p = p_Minus_m_Mult_q(p1, lm, a2, spNoether); |
---|
76 | |
---|
77 | pDelete1(&lm); |
---|
78 | } |
---|
79 | |
---|
80 | /*************************************************************** |
---|
81 | * |
---|
82 | * Creates S-Poly of p1 and p2 |
---|
83 | * |
---|
84 | * |
---|
85 | ***************************************************************/ |
---|
86 | void ksCreateSpoly(LObject* Pair, |
---|
87 | poly spNoether) |
---|
88 | { |
---|
89 | assume(kTest_L(Pair)); |
---|
90 | poly p1 = Pair->p1; |
---|
91 | poly p2 = Pair->p2; |
---|
92 | |
---|
93 | assume(p1 != NULL); |
---|
94 | assume(p2 != NULL); |
---|
95 | |
---|
96 | poly a1 = pNext(p1), a2 = pNext(p2); |
---|
97 | number lc1 = pGetCoeff(p1), lc2 = pGetCoeff(p2); |
---|
98 | poly m1, m2; |
---|
99 | int co=0, ct = ksCheckCoeff(&lc1, &lc2); |
---|
100 | int x, l1; |
---|
101 | |
---|
102 | if (pGetComp(p1)!=pGetComp(p2)) |
---|
103 | { |
---|
104 | if (pGetComp(p1)==0) |
---|
105 | { |
---|
106 | co=1; |
---|
107 | pSetCompP(p1,pGetComp(p2)); |
---|
108 | } |
---|
109 | else |
---|
110 | { |
---|
111 | co=2; |
---|
112 | pSetCompP(p2,pGetComp(p1)); |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | // get m1 = LCM(LM(p1), LM(p2))/LM(p1) |
---|
117 | // m2 = LCM(LM(p1), LM(p2))/LM(p2) |
---|
118 | m1 = pInit(); |
---|
119 | m2 = pInit(); |
---|
120 | for (int i = pVariables; i; i--) |
---|
121 | { |
---|
122 | x = pGetExpDiff(p1, p2, i); |
---|
123 | if (x > 0) |
---|
124 | { |
---|
125 | pSetExp(m2,i,x); |
---|
126 | pSetExp(m1,i,0); |
---|
127 | } |
---|
128 | else |
---|
129 | { |
---|
130 | pSetExp(m1,i,-x); |
---|
131 | pSetExp(m2,i,0); |
---|
132 | } |
---|
133 | } |
---|
134 | pSetm(m1); |
---|
135 | pSetm(m2); // now we have m1 * LM(p1) == m2 * LM(p2) |
---|
136 | pSetCoeff0(m1, lc2); |
---|
137 | pSetCoeff0(m2, lc1); // and now, m1 * LT(p1) == m2 * LT(p2) |
---|
138 | |
---|
139 | // get m2 * a2 |
---|
140 | a2 = p_Mult_m(a2, m2, spNoether); |
---|
141 | |
---|
142 | // and, finally, the spoly |
---|
143 | Pair->p = p_Minus_m_Mult_q(a2, m1, a1, spNoether); |
---|
144 | |
---|
145 | // Clean-up time |
---|
146 | pDelete1(&m1); |
---|
147 | pDelete1(&m2); |
---|
148 | |
---|
149 | if (co != 0) |
---|
150 | { |
---|
151 | if (co==1) |
---|
152 | { |
---|
153 | pSetCompP(p1,0); |
---|
154 | } |
---|
155 | else |
---|
156 | { |
---|
157 | pSetCompP(p2,0); |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | ////////////////////////////////////////////////////////////////////////// |
---|
163 | // Reduces PR at Current->next with PW |
---|
164 | // Assumes PR != NULL, Current contained in PR |
---|
165 | // Current->next != NULL, LM(PW) devides LM(Current->next) |
---|
166 | // Changes: PR |
---|
167 | // Const: PW |
---|
168 | void ksSpolyTail(LObject* PR, TObject* PW, poly Current, poly spNoether) |
---|
169 | { |
---|
170 | poly Lp = PR->p; |
---|
171 | number coef; |
---|
172 | poly Save = PW->p; |
---|
173 | |
---|
174 | if (Lp == Save) |
---|
175 | PW->p = pCopy(Save); |
---|
176 | |
---|
177 | assume(Lp != NULL && Current != NULL && pNext(Current) != NULL); |
---|
178 | assume(pIsMonomOf(Lp, Current)); |
---|
179 | |
---|
180 | PR->p = pNext(Current); |
---|
181 | ksReducePoly(PR, PW, spNoether, &coef); |
---|
182 | |
---|
183 | if (! nIsOne(coef)) |
---|
184 | { |
---|
185 | pNext(Current) = NULL; |
---|
186 | p_Mult_n(Lp, coef); |
---|
187 | } |
---|
188 | nDelete(&coef); |
---|
189 | pNext(Current) = PR->p; |
---|
190 | PR->p = Lp; |
---|
191 | if (PW->p != Save) |
---|
192 | { |
---|
193 | pDelete(&(PW->p)); |
---|
194 | PW->p = Save; // == Lp |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | /*************************************************************** |
---|
200 | * |
---|
201 | * Auxillary Routines |
---|
202 | * |
---|
203 | * |
---|
204 | ***************************************************************/ |
---|
205 | |
---|
206 | /* |
---|
207 | * input - output: a, b |
---|
208 | * returns: |
---|
209 | * a := a/gcd(a,b), b := b/gcd(a,b) |
---|
210 | * and return value |
---|
211 | * 0 -> a != 1, b != 1 |
---|
212 | * 1 -> a == 1, b != 1 |
---|
213 | * 2 -> a != 1, b == 1 |
---|
214 | * 3 -> a == 1, b == 1 |
---|
215 | * this value is used to control the spolys |
---|
216 | */ |
---|
217 | int ksCheckCoeff(number *a, number *b) |
---|
218 | { |
---|
219 | int c = 0; |
---|
220 | number an = *a, bn = *b; |
---|
221 | nTest(an); |
---|
222 | nTest(bn); |
---|
223 | number cn = nGcd(an, bn); |
---|
224 | |
---|
225 | if(nIsOne(cn)) |
---|
226 | { |
---|
227 | an = nCopy(an); |
---|
228 | bn = nCopy(bn); |
---|
229 | } |
---|
230 | else |
---|
231 | { |
---|
232 | an = nIntDiv(an, cn); |
---|
233 | bn = nIntDiv(bn, cn); |
---|
234 | } |
---|
235 | nDelete(&cn); |
---|
236 | if (nIsOne(an)) |
---|
237 | { |
---|
238 | c = 1; |
---|
239 | } |
---|
240 | if (nIsOne(bn)) |
---|
241 | { |
---|
242 | c += 2; |
---|
243 | } |
---|
244 | *a = an; |
---|
245 | *b = bn; |
---|
246 | return c; |
---|
247 | } |
---|
248 | |
---|
249 | /*2 |
---|
250 | * creates the leading term of the S-polynomial of p1 and p2 |
---|
251 | * do not destroy p1 and p2 |
---|
252 | * remarks: |
---|
253 | * 1. the coefficient is 0 (nNew) |
---|
254 | * 2. pNext is undefined |
---|
255 | */ |
---|
256 | //static void bbb() { int i=0; } |
---|
257 | poly ksCreateShortSpoly(poly p1, poly p2) |
---|
258 | { |
---|
259 | poly a1 = pNext(p1), a2 = pNext(p2); |
---|
260 | Exponent_t c1=pGetComp(p1),c2=pGetComp(p2); |
---|
261 | Exponent_t c; |
---|
262 | poly m1,m2; |
---|
263 | number t1,t2; |
---|
264 | int cm,i; |
---|
265 | BOOLEAN equal; |
---|
266 | |
---|
267 | if (a1==NULL) |
---|
268 | { |
---|
269 | if(a2!=NULL) |
---|
270 | { |
---|
271 | m2=pInit(); |
---|
272 | x2: |
---|
273 | for (i = pVariables; i; i--) |
---|
274 | { |
---|
275 | c = pGetExpDiff(p1, p2,i); |
---|
276 | if (c>0) |
---|
277 | { |
---|
278 | pSetExp(m2,i,(c+pGetExp(a2,i))); |
---|
279 | } |
---|
280 | else |
---|
281 | { |
---|
282 | pSetExp(m2,i,pGetExp(a2,i)); |
---|
283 | } |
---|
284 | } |
---|
285 | if ((c1==c2)||(c2!=0)) |
---|
286 | { |
---|
287 | pSetComp(m2,pGetComp(a2)); |
---|
288 | } |
---|
289 | else |
---|
290 | { |
---|
291 | pSetComp(m2,c1); |
---|
292 | } |
---|
293 | pSetm(m2); |
---|
294 | nNew(&(pGetCoeff(m2))); |
---|
295 | return m2; |
---|
296 | } |
---|
297 | else |
---|
298 | return NULL; |
---|
299 | } |
---|
300 | if (a2==NULL) |
---|
301 | { |
---|
302 | m1=pInit(); |
---|
303 | x1: |
---|
304 | for (i = pVariables; i; i--) |
---|
305 | { |
---|
306 | c = pGetExpDiff(p2, p1,i); |
---|
307 | if (c>0) |
---|
308 | { |
---|
309 | pSetExp(m1,i,(c+pGetExp(a1,i))); |
---|
310 | } |
---|
311 | else |
---|
312 | { |
---|
313 | pSetExp(m1,i,pGetExp(a1,i)); |
---|
314 | } |
---|
315 | } |
---|
316 | if ((c1==c2)||(c1!=0)) |
---|
317 | { |
---|
318 | pSetComp(m1,pGetComp(a1)); |
---|
319 | } |
---|
320 | else |
---|
321 | { |
---|
322 | pSetComp(m1,c2); |
---|
323 | } |
---|
324 | pSetm(m1); |
---|
325 | nNew(&(pGetCoeff(m1))); |
---|
326 | return m1; |
---|
327 | } |
---|
328 | m1 = pInit(); |
---|
329 | m2 = pInit(); |
---|
330 | for(;;) |
---|
331 | { |
---|
332 | for (i = pVariables; i; i--) |
---|
333 | { |
---|
334 | c = pGetExpDiff(p1, p2,i); |
---|
335 | if (c > 0) |
---|
336 | { |
---|
337 | pSetExp(m2,i,(c+pGetExp(a2,i))); |
---|
338 | pSetExp(m1,i,pGetExp(a1,i)); |
---|
339 | } |
---|
340 | else |
---|
341 | { |
---|
342 | pSetExp(m1,i,(pGetExp(a1,i)-c)); |
---|
343 | pSetExp(m2,i,pGetExp(a2,i)); |
---|
344 | } |
---|
345 | } |
---|
346 | if(c1==c2) |
---|
347 | { |
---|
348 | pSetComp(m1,pGetComp(a1)); |
---|
349 | pSetComp(m2,pGetComp(a2)); |
---|
350 | } |
---|
351 | else |
---|
352 | { |
---|
353 | if(c1!=0) |
---|
354 | { |
---|
355 | pSetComp(m1,pGetComp(a1)); |
---|
356 | pSetComp(m2,c1); |
---|
357 | } |
---|
358 | else |
---|
359 | { |
---|
360 | pSetComp(m2,pGetComp(a2)); |
---|
361 | pSetComp(m1,c2); |
---|
362 | } |
---|
363 | } |
---|
364 | pSetm(m1); |
---|
365 | pSetm(m2); |
---|
366 | cm = pComp0(m1, m2); |
---|
367 | if (cm!=0) |
---|
368 | { |
---|
369 | if(cm==1) |
---|
370 | { |
---|
371 | pFree1(m2); |
---|
372 | nNew(&(pGetCoeff(m1))); |
---|
373 | return m1; |
---|
374 | } |
---|
375 | else |
---|
376 | { |
---|
377 | pFree1(m1); |
---|
378 | nNew(&(pGetCoeff(m2))); |
---|
379 | return m2; |
---|
380 | } |
---|
381 | } |
---|
382 | t1 = nMult(pGetCoeff(a2),pGetCoeff(p1)); |
---|
383 | t2 = nMult(pGetCoeff(a1),pGetCoeff(p2)); |
---|
384 | equal = nEqual(t1,t2); |
---|
385 | nDelete(&t2); |
---|
386 | nDelete(&t1); |
---|
387 | if (!equal) |
---|
388 | { |
---|
389 | pFree1(m2); |
---|
390 | nNew(&(pGetCoeff(m1))); |
---|
391 | return m1; |
---|
392 | } |
---|
393 | pIter(a1); |
---|
394 | pIter(a2); |
---|
395 | if (a2==NULL) |
---|
396 | { |
---|
397 | pFree1(m2); |
---|
398 | if (a1==NULL) |
---|
399 | { |
---|
400 | pFree1(m1); |
---|
401 | return NULL; |
---|
402 | } |
---|
403 | goto x1; |
---|
404 | } |
---|
405 | if (a1==NULL) |
---|
406 | { |
---|
407 | pFree1(m1); |
---|
408 | goto x2; |
---|
409 | } |
---|
410 | } |
---|
411 | } |
---|
412 | |
---|
413 | |
---|
414 | /*************************************************************** |
---|
415 | * |
---|
416 | * Routines for backwards-Compatibility |
---|
417 | * |
---|
418 | * |
---|
419 | ***************************************************************/ |
---|
420 | poly ksOldSpolyRed(poly p1, poly p2, poly spNoether) |
---|
421 | { |
---|
422 | LObject L; |
---|
423 | TObject T; |
---|
424 | |
---|
425 | L.p = p2; |
---|
426 | T.p = p1; |
---|
427 | |
---|
428 | ksReducePoly(&L, &T, spNoether); |
---|
429 | |
---|
430 | return L.p; |
---|
431 | } |
---|
432 | |
---|
433 | poly ksOldSpolyRedNew(poly p1, poly p2, poly spNoether) |
---|
434 | { |
---|
435 | LObject L; |
---|
436 | TObject T; |
---|
437 | |
---|
438 | L.p = pCopy(p2); |
---|
439 | T.p = p1; |
---|
440 | |
---|
441 | ksReducePoly(&L, &T, spNoether); |
---|
442 | |
---|
443 | return L.p; |
---|
444 | } |
---|
445 | |
---|
446 | poly ksOldCreateSpoly(poly p1, poly p2, poly spNoether) |
---|
447 | { |
---|
448 | LObject L; |
---|
449 | L.p1 = p1; |
---|
450 | L.p2 = p2; |
---|
451 | |
---|
452 | ksCreateSpoly(&L, spNoether); |
---|
453 | return L.p; |
---|
454 | } |
---|
455 | |
---|
456 | void ksOldSpolyTail(poly p1, poly q, poly q2, poly spNoether) |
---|
457 | { |
---|
458 | LObject L; |
---|
459 | TObject T; |
---|
460 | |
---|
461 | L.p = q; |
---|
462 | T.p = p1; |
---|
463 | |
---|
464 | ksSpolyTail(&L, &T, q2, spNoether); |
---|
465 | return; |
---|
466 | } |
---|
467 | |
---|
468 | |
---|
469 | |
---|
470 | |
---|
471 | |
---|