1 | /*****************************************************************************\ |
---|
2 | * Computer Algebra System SINGULAR |
---|
3 | \*****************************************************************************/ |
---|
4 | /** @file facFqBivarUtil.cc |
---|
5 | * |
---|
6 | * This file provides utility functions for bivariate factorization |
---|
7 | * |
---|
8 | * @author Martin Lee |
---|
9 | * |
---|
10 | **/ |
---|
11 | /*****************************************************************************/ |
---|
12 | |
---|
13 | #include "config.h" |
---|
14 | |
---|
15 | #include "cf_map.h" |
---|
16 | #include "algext.h" |
---|
17 | #include "cf_map_ext.h" |
---|
18 | #include "templates/ftmpl_functions.h" |
---|
19 | #include "ExtensionInfo.h" |
---|
20 | #include "cf_algorithm.h" |
---|
21 | #include "cf_factory.h" |
---|
22 | #include "cf_util.h" |
---|
23 | #include "imm.h" |
---|
24 | #include "cf_iter.h" |
---|
25 | #include "facFqBivarUtil.h" |
---|
26 | #include "cfNewtonPolygon.h" |
---|
27 | #include "facHensel.h" |
---|
28 | #include "facMul.h" |
---|
29 | |
---|
30 | |
---|
31 | void append (CFList& factors1, const CFList& factors2) |
---|
32 | { |
---|
33 | for (CFListIterator i= factors2; i.hasItem(); i++) |
---|
34 | { |
---|
35 | if (!i.getItem().inCoeffDomain()) |
---|
36 | factors1.append (i.getItem()); |
---|
37 | } |
---|
38 | return; |
---|
39 | } |
---|
40 | |
---|
41 | void decompress (CFList& factors, const CFMap& N) |
---|
42 | { |
---|
43 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
44 | i.getItem()= N (i.getItem()); |
---|
45 | } |
---|
46 | |
---|
47 | void decompress (CFFList& factors, const CFMap& N) |
---|
48 | { |
---|
49 | for (CFFListIterator i= factors; i.hasItem(); i++) |
---|
50 | i.getItem()= CFFactor (N (i.getItem().factor()), i.getItem().exp()); |
---|
51 | } |
---|
52 | |
---|
53 | void appendSwapDecompress (CFList& factors1, const CFList& factors2, |
---|
54 | const CFList& factors3, const bool swap1, |
---|
55 | const bool swap2, const CFMap& N) |
---|
56 | { |
---|
57 | Variable x= Variable (1); |
---|
58 | Variable y= Variable (2); |
---|
59 | for (CFListIterator i= factors1; i.hasItem(); i++) |
---|
60 | { |
---|
61 | if (swap1) |
---|
62 | { |
---|
63 | if (!swap2) |
---|
64 | i.getItem()= swapvar (i.getItem(), x, y); |
---|
65 | } |
---|
66 | else |
---|
67 | { |
---|
68 | if (swap2) |
---|
69 | i.getItem()= swapvar (i.getItem(), y, x); |
---|
70 | } |
---|
71 | i.getItem()= N (i.getItem()); |
---|
72 | } |
---|
73 | for (CFListIterator i= factors2; i.hasItem(); i++) |
---|
74 | factors1.append (N (i.getItem())); |
---|
75 | for (CFListIterator i= factors3; i.hasItem(); i++) |
---|
76 | factors1.append (N (i.getItem())); |
---|
77 | return; |
---|
78 | } |
---|
79 | |
---|
80 | void swapDecompress (CFList& factors, const bool swap, const CFMap& N) |
---|
81 | { |
---|
82 | Variable x= Variable (1); |
---|
83 | Variable y= Variable (2); |
---|
84 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
85 | { |
---|
86 | if (swap) |
---|
87 | i.getItem()= swapvar (i.getItem(), x, y); |
---|
88 | i.getItem()= N (i.getItem()); |
---|
89 | } |
---|
90 | return; |
---|
91 | } |
---|
92 | |
---|
93 | static inline |
---|
94 | bool GFInExtensionHelper (const CanonicalForm& F, const int number) |
---|
95 | { |
---|
96 | if (F.isOne()) return false; |
---|
97 | InternalCF* buf; |
---|
98 | int exp; |
---|
99 | bool result= false; |
---|
100 | if (F.inBaseDomain()) |
---|
101 | { |
---|
102 | buf= F.getval(); |
---|
103 | exp= imm2int (buf); |
---|
104 | if (exp%number != 0) |
---|
105 | return true; |
---|
106 | else |
---|
107 | return result; |
---|
108 | } |
---|
109 | else |
---|
110 | { |
---|
111 | for (CFIterator i= F; i.hasTerms(); i++) |
---|
112 | { |
---|
113 | result= GFInExtensionHelper (i.coeff(), number); |
---|
114 | if (result == true) |
---|
115 | return result; |
---|
116 | } |
---|
117 | } |
---|
118 | return result; |
---|
119 | } |
---|
120 | |
---|
121 | static inline |
---|
122 | bool FqInExtensionHelper (const CanonicalForm& F, const CanonicalForm& gamma, |
---|
123 | const CanonicalForm& delta, CFList& source, |
---|
124 | CFList& dest) |
---|
125 | { |
---|
126 | bool result= false; |
---|
127 | if (F.inBaseDomain()) |
---|
128 | return result; |
---|
129 | else if (F.inCoeffDomain()) |
---|
130 | { |
---|
131 | if (!fdivides (gamma, F)) |
---|
132 | return true; |
---|
133 | else |
---|
134 | { |
---|
135 | int pos= findItem (source, F); |
---|
136 | if (pos > 0) |
---|
137 | return false; |
---|
138 | Variable a; |
---|
139 | hasFirstAlgVar (F, a); |
---|
140 | int bound= ipower (getCharacteristic(), degree (getMipo (a))); |
---|
141 | CanonicalForm buf= 1; |
---|
142 | for (int i= 1; i < bound; i++) |
---|
143 | { |
---|
144 | buf *= gamma; |
---|
145 | if (buf == F) |
---|
146 | { |
---|
147 | source.append (buf); |
---|
148 | dest.append (power (delta, i)); |
---|
149 | return false; |
---|
150 | } |
---|
151 | } |
---|
152 | return true; |
---|
153 | } |
---|
154 | } |
---|
155 | else |
---|
156 | { |
---|
157 | for (CFIterator i= F; i.hasTerms(); i++) |
---|
158 | { |
---|
159 | result= FqInExtensionHelper (i.coeff(), gamma, delta, source, dest); |
---|
160 | if (result == true) |
---|
161 | return result; |
---|
162 | } |
---|
163 | } |
---|
164 | return result; |
---|
165 | } |
---|
166 | |
---|
167 | bool isInExtension (const CanonicalForm& F, const CanonicalForm& gamma, |
---|
168 | const int k, const CanonicalForm& delta, |
---|
169 | CFList& source, CFList& dest) |
---|
170 | { |
---|
171 | bool result; |
---|
172 | if (CFFactory::gettype() == GaloisFieldDomain) |
---|
173 | { |
---|
174 | int p= getCharacteristic(); |
---|
175 | int orderFieldExtension= ipower (p, getGFDegree()) - 1; |
---|
176 | int order= ipower (p, k) - 1; |
---|
177 | int number= orderFieldExtension/order; |
---|
178 | result= GFInExtensionHelper (F, number); |
---|
179 | return result; |
---|
180 | } |
---|
181 | else |
---|
182 | { |
---|
183 | result= FqInExtensionHelper (F, gamma, delta, source, dest); |
---|
184 | return result; |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | CanonicalForm |
---|
189 | mapDown (const CanonicalForm& F, const ExtensionInfo& info, CFList& source, |
---|
190 | CFList& dest) |
---|
191 | { |
---|
192 | int k= info.getGFDegree(); |
---|
193 | Variable beta= info.getAlpha(); |
---|
194 | CanonicalForm primElem= info.getGamma(); |
---|
195 | CanonicalForm imPrimElem= info.getDelta(); |
---|
196 | if (k > 1) |
---|
197 | return GFMapDown (F, k); |
---|
198 | else if (k == 1) |
---|
199 | return F; |
---|
200 | if (/*k==0 &&*/ beta == Variable (1)) |
---|
201 | return F; |
---|
202 | else /*if (k==0 && beta != Variable (1))*/ |
---|
203 | return mapDown (F, imPrimElem, primElem, beta, source, dest); |
---|
204 | } |
---|
205 | |
---|
206 | void appendTestMapDown (CFList& factors, const CanonicalForm& f, |
---|
207 | const ExtensionInfo& info, CFList& source, CFList& dest) |
---|
208 | { |
---|
209 | int k= info.getGFDegree(); |
---|
210 | Variable beta= info.getBeta(); |
---|
211 | Variable alpha= info.getAlpha(); |
---|
212 | CanonicalForm delta= info.getDelta(); |
---|
213 | CanonicalForm gamma= info.getGamma(); |
---|
214 | CanonicalForm g= f; |
---|
215 | int degMipoBeta; |
---|
216 | if (!k && beta.level() == 1) |
---|
217 | degMipoBeta= 1; |
---|
218 | else if (!k && beta.level() != 1) |
---|
219 | degMipoBeta= degree (getMipo (beta)); |
---|
220 | if (k > 1) |
---|
221 | { |
---|
222 | if (!isInExtension (g, gamma, k, delta, source, dest)) |
---|
223 | { |
---|
224 | g= GFMapDown (g, k); |
---|
225 | factors.append (g); |
---|
226 | } |
---|
227 | } |
---|
228 | else if (k == 1) |
---|
229 | { |
---|
230 | if (!isInExtension (g, gamma, k, delta, source, dest)) |
---|
231 | factors.append (g); |
---|
232 | } |
---|
233 | else if (!k && beta == Variable (1)) |
---|
234 | { |
---|
235 | if (degree (g, alpha) < degMipoBeta) |
---|
236 | factors.append (g); |
---|
237 | } |
---|
238 | else if (!k && beta != Variable (1)) |
---|
239 | { |
---|
240 | if (!isInExtension (g, gamma, k, delta, source, dest)) |
---|
241 | { |
---|
242 | g= mapDown (g, delta, gamma, alpha, source, dest); |
---|
243 | factors.append (g); |
---|
244 | } |
---|
245 | } |
---|
246 | return; |
---|
247 | } |
---|
248 | |
---|
249 | void |
---|
250 | appendMapDown (CFList& factors, const CanonicalForm& g, |
---|
251 | const ExtensionInfo& info, CFList& source, CFList& dest) |
---|
252 | { |
---|
253 | int k= info.getGFDegree(); |
---|
254 | Variable beta= info.getBeta(); |
---|
255 | Variable alpha= info.getAlpha(); |
---|
256 | CanonicalForm gamma= info.getGamma(); |
---|
257 | CanonicalForm delta= info.getDelta(); |
---|
258 | if (k > 1) |
---|
259 | factors.append (GFMapDown (g, k)); |
---|
260 | else if (k == 1) |
---|
261 | factors.append (g); |
---|
262 | else if (!k && beta == Variable (1)) |
---|
263 | factors.append (g); |
---|
264 | else if (!k && beta != Variable (1)) |
---|
265 | factors.append (mapDown (g, delta, gamma, alpha, source, dest)); |
---|
266 | return; |
---|
267 | } |
---|
268 | |
---|
269 | void normalize (CFList& factors) |
---|
270 | { |
---|
271 | CanonicalForm lcinv; |
---|
272 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
273 | { |
---|
274 | lcinv= 1/Lc (i.getItem()); |
---|
275 | i.getItem() *= lcinv; |
---|
276 | } |
---|
277 | return; |
---|
278 | } |
---|
279 | |
---|
280 | void normalize (CFFList& factors) |
---|
281 | { |
---|
282 | CanonicalForm lcinv; |
---|
283 | for (CFFListIterator i= factors; i.hasItem(); i++) |
---|
284 | { |
---|
285 | lcinv= 1/ Lc (i.getItem().factor()); |
---|
286 | i.getItem()= CFFactor (i.getItem().factor()*lcinv, |
---|
287 | i.getItem().exp()); |
---|
288 | } |
---|
289 | return; |
---|
290 | } |
---|
291 | |
---|
292 | CFList subset (int index [], const int& s, const CFArray& elements, |
---|
293 | bool& noSubset) |
---|
294 | { |
---|
295 | int r= elements.size(); |
---|
296 | int i= 0; |
---|
297 | CFList result; |
---|
298 | noSubset= false; |
---|
299 | if (index[s - 1] == 0) |
---|
300 | { |
---|
301 | while (i < s) |
---|
302 | { |
---|
303 | index[i]= i + 1; |
---|
304 | result.append (elements[i]); |
---|
305 | i++; |
---|
306 | } |
---|
307 | return result; |
---|
308 | } |
---|
309 | int buf; |
---|
310 | int k; |
---|
311 | bool found= false; |
---|
312 | if (index[s - 1] == r) |
---|
313 | { |
---|
314 | if (index[0] == r - s + 1) |
---|
315 | { |
---|
316 | noSubset= true; |
---|
317 | return result; |
---|
318 | } |
---|
319 | else { |
---|
320 | while (found == false) |
---|
321 | { |
---|
322 | if (index[s - 2 - i] < r - i - 1) |
---|
323 | found= true; |
---|
324 | i++; |
---|
325 | } |
---|
326 | buf= index[s - i - 1]; |
---|
327 | k= 0; |
---|
328 | while (s - i - 1 + k < s) |
---|
329 | { |
---|
330 | index[s - i - 1 + k]= buf + k + 1; |
---|
331 | k++; |
---|
332 | } |
---|
333 | } |
---|
334 | for (int j= 0; j < s; j++) |
---|
335 | result.append (elements[index[j] - 1]); |
---|
336 | return result; |
---|
337 | } |
---|
338 | else |
---|
339 | { |
---|
340 | index[s - 1] += 1; |
---|
341 | for (int j= 0; j < s; j++) |
---|
342 | result.append (elements[index[j] - 1]); |
---|
343 | return result; |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | CFArray copy (const CFList& list) |
---|
348 | { |
---|
349 | CFArray array= CFArray (list.length()); |
---|
350 | int j= 0; |
---|
351 | for (CFListIterator i= list; i.hasItem(); i++, j++) |
---|
352 | array[j]= i.getItem(); |
---|
353 | return array; |
---|
354 | } |
---|
355 | |
---|
356 | void indexUpdate (int index [], const int& subsetSize, const int& setSize, |
---|
357 | bool& noSubset) |
---|
358 | { |
---|
359 | noSubset= false; |
---|
360 | if (subsetSize > setSize) |
---|
361 | { |
---|
362 | noSubset= true; |
---|
363 | return; |
---|
364 | } |
---|
365 | int * v= new int [setSize]; |
---|
366 | for (int i= 0; i < setSize; i++) |
---|
367 | v[i]= index[i]; |
---|
368 | if (subsetSize == 1) |
---|
369 | { |
---|
370 | v[0]= v[0] - 1; |
---|
371 | if (v[0] >= setSize) |
---|
372 | { |
---|
373 | noSubset= true; |
---|
374 | delete [] v; |
---|
375 | return; |
---|
376 | } |
---|
377 | } |
---|
378 | else |
---|
379 | { |
---|
380 | if (v[subsetSize - 1] - v[0] + 1 == subsetSize && v[0] > 1) |
---|
381 | { |
---|
382 | if (v[0] + subsetSize - 1 > setSize) |
---|
383 | { |
---|
384 | noSubset= true; |
---|
385 | delete [] v; |
---|
386 | return; |
---|
387 | } |
---|
388 | v[0]= v[0] - 1; |
---|
389 | for (int i= 1; i < subsetSize - 1; i++) |
---|
390 | v[i]= v[i - 1] + 1; |
---|
391 | v[subsetSize - 1]= v[subsetSize - 2]; |
---|
392 | } |
---|
393 | else |
---|
394 | { |
---|
395 | if (v[0] + subsetSize - 1 > setSize) |
---|
396 | { |
---|
397 | noSubset= true; |
---|
398 | delete [] v; |
---|
399 | return; |
---|
400 | } |
---|
401 | for (int i= 1; i < subsetSize - 1; i++) |
---|
402 | v[i]= v[i - 1] + 1; |
---|
403 | v[subsetSize - 1]= v[subsetSize - 2]; |
---|
404 | } |
---|
405 | } |
---|
406 | for (int i= 0; i < setSize; i++) |
---|
407 | index[i]= v[i]; |
---|
408 | delete [] v; |
---|
409 | } |
---|
410 | |
---|
411 | int subsetDegree (const CFList& S) |
---|
412 | { |
---|
413 | int result= 0; |
---|
414 | for (CFListIterator i= S; i.hasItem(); i++) |
---|
415 | result += degree (i.getItem(), Variable (1)); |
---|
416 | return result; |
---|
417 | } |
---|
418 | |
---|
419 | CFFList multiplicity (CanonicalForm& F, const CFList& factors) |
---|
420 | { |
---|
421 | if (F.inCoeffDomain()) |
---|
422 | return CFFList (CFFactor (F, 1)); |
---|
423 | CFFList result; |
---|
424 | int multi= 0; |
---|
425 | CanonicalForm quot; |
---|
426 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
427 | { |
---|
428 | while (fdivides (i.getItem(), F, quot)) |
---|
429 | { |
---|
430 | multi++; |
---|
431 | F= quot; |
---|
432 | } |
---|
433 | if (multi > 0) |
---|
434 | result.append (CFFactor (i.getItem(), multi)); |
---|
435 | multi= 0; |
---|
436 | } |
---|
437 | return result; |
---|
438 | } |
---|
439 | |
---|
440 | #ifdef HAVE_NTL |
---|
441 | CFArray |
---|
442 | logarithmicDerivative (const CanonicalForm& F, const CanonicalForm& G, int l, |
---|
443 | CanonicalForm& Q |
---|
444 | ) |
---|
445 | { |
---|
446 | Variable x= Variable (2); |
---|
447 | Variable y= Variable (1); |
---|
448 | CanonicalForm xToL= power (x, l); |
---|
449 | CanonicalForm q,r; |
---|
450 | CanonicalForm logDeriv; |
---|
451 | |
---|
452 | q= newtonDiv (F, G, xToL); |
---|
453 | |
---|
454 | logDeriv= mulMod2 (q, deriv (G, y), xToL); |
---|
455 | |
---|
456 | logDeriv= swapvar (logDeriv, x, y); |
---|
457 | int j= degree (logDeriv) + 1; |
---|
458 | CFArray result= CFArray (j); |
---|
459 | for (CFIterator i= logDeriv; i.hasTerms() && !logDeriv.isZero(); i++) |
---|
460 | { |
---|
461 | if (i.exp() == j - 1) |
---|
462 | { |
---|
463 | result [j - 1]= swapvar (i.coeff(), x, y); |
---|
464 | j--; |
---|
465 | } |
---|
466 | else |
---|
467 | { |
---|
468 | for (; i.exp() < j - 1; j--) |
---|
469 | result [j - 1]= 0; |
---|
470 | result [j - 1]= swapvar (i.coeff(), x, y); |
---|
471 | j--; |
---|
472 | } |
---|
473 | } |
---|
474 | for (; j > 0; j--) |
---|
475 | result [j - 1]= 0; |
---|
476 | Q= q; |
---|
477 | return result; |
---|
478 | } |
---|
479 | |
---|
480 | CFArray |
---|
481 | logarithmicDerivative (const CanonicalForm& F, const CanonicalForm& G, int l, |
---|
482 | int oldL, const CanonicalForm& oldQ, CanonicalForm& Q |
---|
483 | ) |
---|
484 | { |
---|
485 | Variable x= Variable (2); |
---|
486 | Variable y= Variable (1); |
---|
487 | CanonicalForm xToL= power (x, l); |
---|
488 | CanonicalForm xToOldL= power (x, oldL); |
---|
489 | CanonicalForm xToLOldL= power (x, l-oldL); |
---|
490 | CanonicalForm q,r; |
---|
491 | CanonicalForm logDeriv; |
---|
492 | |
---|
493 | CanonicalForm bufF; |
---|
494 | if ((oldL > 100 && l - oldL < 50) || (oldL < 100 && l - oldL < 30)) |
---|
495 | { |
---|
496 | bufF= F; |
---|
497 | CanonicalForm oldF= mulMod2 (G, oldQ, xToL); |
---|
498 | bufF -= oldF; |
---|
499 | bufF= div (bufF, xToOldL); |
---|
500 | } |
---|
501 | else |
---|
502 | { |
---|
503 | //middle product style computation of [G*oldQ]^{l}_{oldL} |
---|
504 | CanonicalForm G3= div (G, xToOldL); |
---|
505 | CanonicalForm Up= mulMod2 (G3, oldQ, xToLOldL); |
---|
506 | CanonicalForm xToOldL2= power (x, oldL/2); |
---|
507 | CanonicalForm G2= mod (G, xToOldL); |
---|
508 | CanonicalForm G1= div (G2, xToOldL2); |
---|
509 | CanonicalForm G0= mod (G2, xToOldL2); |
---|
510 | CanonicalForm oldQ1= div (oldQ, xToOldL2); |
---|
511 | CanonicalForm oldQ0= mod (oldQ, xToOldL2); |
---|
512 | CanonicalForm Mid= mulMod2 (G1, oldQ1, xToLOldL); |
---|
513 | //computation of Low might be faster using a real middle product? |
---|
514 | CanonicalForm Low= mulMod2 (G0, oldQ1, xToOldL)+mulMod2 (G1, oldQ0, xToOldL); |
---|
515 | Low= div (Low, xToOldL2); |
---|
516 | Up += Mid + Low; |
---|
517 | bufF= div (F, xToOldL); |
---|
518 | bufF -= Up; |
---|
519 | } |
---|
520 | |
---|
521 | q= newtonDiv (bufF, G, xToLOldL); |
---|
522 | q *= xToOldL; |
---|
523 | q += oldQ; |
---|
524 | |
---|
525 | logDeriv= mulMod2 (q, deriv (G, y), xToL); |
---|
526 | |
---|
527 | logDeriv= swapvar (logDeriv, x, y); |
---|
528 | int j= degree (logDeriv) + 1; |
---|
529 | CFArray result= CFArray (j); |
---|
530 | for (CFIterator i= logDeriv; i.hasTerms() && !logDeriv.isZero(); i++) |
---|
531 | { |
---|
532 | if (i.exp() == j - 1) |
---|
533 | { |
---|
534 | result [j - 1]= swapvar (i.coeff(), x, y); |
---|
535 | j--; |
---|
536 | } |
---|
537 | else |
---|
538 | { |
---|
539 | for (; i.exp() < j - 1; j--) |
---|
540 | result [j - 1]= 0; |
---|
541 | result [j - 1]= swapvar (i.coeff(), x, y); |
---|
542 | j--; |
---|
543 | } |
---|
544 | } |
---|
545 | for (; j > 0; j--) |
---|
546 | result [j - 1]= 0; |
---|
547 | Q= q; |
---|
548 | return result; |
---|
549 | } |
---|
550 | #endif |
---|
551 | |
---|
552 | void |
---|
553 | writeInMatrix (CFMatrix& M, const CFArray& A, const int column, |
---|
554 | const int startIndex |
---|
555 | ) |
---|
556 | { |
---|
557 | ASSERT (A.size () - startIndex >= 0, "wrong starting index"); |
---|
558 | ASSERT (A.size () - startIndex <= M.rows(), "wrong starting index"); |
---|
559 | ASSERT (column > 0 && column <= M.columns(), "wrong column"); |
---|
560 | if (A.size() - startIndex <= 0) return; |
---|
561 | int j= 1; |
---|
562 | for (int i= startIndex; i < A.size(); i++, j++) |
---|
563 | M (j, column)= A [i]; |
---|
564 | } |
---|
565 | |
---|
566 | CFArray getCoeffs (const CanonicalForm& F, const int k) |
---|
567 | { |
---|
568 | ASSERT (F.isUnivariate() || F.inCoeffDomain(), "univariate input expected"); |
---|
569 | if (degree (F, 2) < k) |
---|
570 | return CFArray(); |
---|
571 | |
---|
572 | CFArray result= CFArray (degree (F) - k + 1); |
---|
573 | CFIterator j= F; |
---|
574 | for (int i= degree (F); i >= k; i--) |
---|
575 | { |
---|
576 | if (j.exp() == i) |
---|
577 | { |
---|
578 | result [i - k]= j.coeff(); |
---|
579 | j++; |
---|
580 | if (!j.hasTerms()) |
---|
581 | return result; |
---|
582 | } |
---|
583 | else |
---|
584 | result[i - k]= 0; |
---|
585 | } |
---|
586 | return result; |
---|
587 | } |
---|
588 | |
---|
589 | CFArray getCoeffs (const CanonicalForm& F, const int k, const Variable& alpha) |
---|
590 | { |
---|
591 | ASSERT (F.isUnivariate() || F.inCoeffDomain(), "univariate input expected"); |
---|
592 | if (degree (F, 2) < k) |
---|
593 | return CFArray (); |
---|
594 | |
---|
595 | int d= degree (getMipo (alpha)); |
---|
596 | CFArray result= CFArray ((degree (F) - k + 1)*d); |
---|
597 | CFIterator j= F; |
---|
598 | CanonicalForm buf; |
---|
599 | CFIterator iter; |
---|
600 | for (int i= degree (F); i >= k; i--) |
---|
601 | { |
---|
602 | if (j.exp() == i) |
---|
603 | { |
---|
604 | iter= j.coeff(); |
---|
605 | for (int l= degree (j.coeff(), alpha); l >= 0; l--) |
---|
606 | { |
---|
607 | if (iter.exp() == l) |
---|
608 | { |
---|
609 | result [(i - k)*d + l]= iter.coeff(); |
---|
610 | iter++; |
---|
611 | if (!iter.hasTerms()) |
---|
612 | break; |
---|
613 | } |
---|
614 | } |
---|
615 | j++; |
---|
616 | if (!j.hasTerms()) |
---|
617 | return result; |
---|
618 | } |
---|
619 | else |
---|
620 | { |
---|
621 | for (int l= 0; l < d; l++) |
---|
622 | result[(i - k)*d + l]= 0; |
---|
623 | } |
---|
624 | } |
---|
625 | return result; |
---|
626 | } |
---|
627 | |
---|
628 | #ifdef HAVE_NTL |
---|
629 | CFArray |
---|
630 | getCoeffs (const CanonicalForm& G, const int k, const int l, const int degMipo, |
---|
631 | const Variable& alpha, const CanonicalForm& evaluation, |
---|
632 | const mat_zz_p& M) |
---|
633 | { |
---|
634 | ASSERT (G.isUnivariate() || G.inCoeffDomain(), "univariate input expected"); |
---|
635 | CanonicalForm F= G (G.mvar() - evaluation, G.mvar()); |
---|
636 | if (F.isZero()) |
---|
637 | return CFArray (); |
---|
638 | |
---|
639 | Variable y= Variable (2); |
---|
640 | F= F (power (y, degMipo), y); |
---|
641 | F= F (y, alpha); |
---|
642 | zz_pX NTLF= convertFacCF2NTLzzpX (F); |
---|
643 | NTLF.rep.SetLength (l*degMipo); |
---|
644 | NTLF.rep= M*NTLF.rep; |
---|
645 | NTLF.normalize(); |
---|
646 | F= convertNTLzzpX2CF (NTLF, y); |
---|
647 | |
---|
648 | if (degree (F, 2) < k) |
---|
649 | return CFArray(); |
---|
650 | |
---|
651 | CFArray result= CFArray (degree (F) - k + 1); |
---|
652 | |
---|
653 | CFIterator j= F; |
---|
654 | for (int i= degree (F); i >= k; i--) |
---|
655 | { |
---|
656 | if (j.exp() == i) |
---|
657 | { |
---|
658 | result [i - k]= j.coeff(); |
---|
659 | j++; |
---|
660 | if (!j.hasTerms()) |
---|
661 | return result; |
---|
662 | } |
---|
663 | else |
---|
664 | result[i - k]= 0; |
---|
665 | } |
---|
666 | return result; |
---|
667 | } |
---|
668 | #endif |
---|
669 | |
---|
670 | int * computeBounds (const CanonicalForm& F, int& n, bool& isIrreducible) |
---|
671 | { |
---|
672 | n= degree (F, 1); |
---|
673 | int* result= new int [n]; |
---|
674 | int sizeOfNewtonPolygon; |
---|
675 | int** newtonPolyg= newtonPolygon (F, sizeOfNewtonPolygon); |
---|
676 | |
---|
677 | isIrreducible= false; |
---|
678 | if (sizeOfNewtonPolygon == 3) |
---|
679 | { |
---|
680 | bool check1= |
---|
681 | (newtonPolyg[0][0]==0 || newtonPolyg[1][0]==0 || newtonPolyg[2][0]==0); |
---|
682 | if (check1) |
---|
683 | { |
---|
684 | bool check2= |
---|
685 | (newtonPolyg[0][1]==0 || newtonPolyg[1][1]==0 || newtonPolyg[2][0]==0); |
---|
686 | if (check2) |
---|
687 | { |
---|
688 | int p=getCharacteristic(); |
---|
689 | int d=1; |
---|
690 | char bufGFName='Z'; |
---|
691 | bool GF= (CFFactory::gettype()==GaloisFieldDomain); |
---|
692 | if (GF) |
---|
693 | { |
---|
694 | d= getGFDegree(); |
---|
695 | bufGFName=gf_name; |
---|
696 | } |
---|
697 | setCharacteristic(0); |
---|
698 | CanonicalForm tmp= gcd (newtonPolyg[0][0],newtonPolyg[0][1]); |
---|
699 | tmp= gcd (tmp, newtonPolyg[1][0]); |
---|
700 | tmp= gcd (tmp, newtonPolyg[1][1]); |
---|
701 | tmp= gcd (tmp, newtonPolyg[2][0]); |
---|
702 | tmp= gcd (tmp, newtonPolyg[2][1]); |
---|
703 | isIrreducible= (tmp==1); |
---|
704 | if (GF) |
---|
705 | setCharacteristic (p, d, bufGFName); |
---|
706 | else |
---|
707 | setCharacteristic(p); |
---|
708 | } |
---|
709 | } |
---|
710 | } |
---|
711 | |
---|
712 | int minX, minY, maxX, maxY; |
---|
713 | minX= newtonPolyg [0] [0]; |
---|
714 | minY= newtonPolyg [0] [1]; |
---|
715 | maxX= minX; |
---|
716 | maxY= minY; |
---|
717 | for (int i= 1; i < sizeOfNewtonPolygon; i++) |
---|
718 | { |
---|
719 | if (minX > newtonPolyg [i] [0]) |
---|
720 | minX= newtonPolyg [i] [0]; |
---|
721 | if (maxX < newtonPolyg [i] [0]) |
---|
722 | maxX= newtonPolyg [i] [0]; |
---|
723 | if (minY > newtonPolyg [i] [1]) |
---|
724 | minY= newtonPolyg [i] [1]; |
---|
725 | if (maxY < newtonPolyg [i] [1]) |
---|
726 | maxY= newtonPolyg [i] [1]; |
---|
727 | } |
---|
728 | |
---|
729 | int k= maxX; |
---|
730 | for (int i= 0; i < n; i++) |
---|
731 | { |
---|
732 | if (i + 1 > maxY || i + 1 < minY) |
---|
733 | { |
---|
734 | result [i]= 0; |
---|
735 | continue; |
---|
736 | } |
---|
737 | int* point= new int [2]; |
---|
738 | point [0]= k; |
---|
739 | point [1]= i + 1; |
---|
740 | while (!isInPolygon (newtonPolyg, sizeOfNewtonPolygon, point) && k > 0) |
---|
741 | { |
---|
742 | k--; |
---|
743 | point [0]= k; |
---|
744 | } |
---|
745 | result [i]= k; |
---|
746 | k= maxX; |
---|
747 | delete [] point; |
---|
748 | } |
---|
749 | |
---|
750 | return result; |
---|
751 | } |
---|
752 | |
---|
753 | int |
---|
754 | substituteCheck (const CanonicalForm& F, const Variable& x) |
---|
755 | { |
---|
756 | if (F.inCoeffDomain()) |
---|
757 | return 0; |
---|
758 | if (degree (F, x) < 0) |
---|
759 | return 0; |
---|
760 | CanonicalForm f= swapvar (F, F.mvar(), x); |
---|
761 | int sizef= 0; |
---|
762 | for (CFIterator i= f; i.hasTerms(); i++, sizef++) |
---|
763 | { |
---|
764 | if (i.exp() == 1) |
---|
765 | return 0; |
---|
766 | } |
---|
767 | int * expf= new int [sizef]; |
---|
768 | int j= 0; |
---|
769 | for (CFIterator i= f; i.hasTerms(); i++, j++) |
---|
770 | expf [j]= i.exp(); |
---|
771 | |
---|
772 | int indf= sizef - 1; |
---|
773 | if (expf[indf] == 0) |
---|
774 | indf--; |
---|
775 | |
---|
776 | int result= expf[indf]; |
---|
777 | for (int i= indf - 1; i >= 0; i--) |
---|
778 | { |
---|
779 | if (expf [i]%result != 0) |
---|
780 | { |
---|
781 | delete [] expf; |
---|
782 | return 0; |
---|
783 | } |
---|
784 | } |
---|
785 | |
---|
786 | delete [] expf; |
---|
787 | return result; |
---|
788 | } |
---|
789 | |
---|
790 | static int |
---|
791 | substituteCheck (const CanonicalForm& F, const CanonicalForm& G) |
---|
792 | { |
---|
793 | if (F.inCoeffDomain() || G.inCoeffDomain()) |
---|
794 | return 0; |
---|
795 | Variable x= Variable (1); |
---|
796 | if (degree (F, x) <= 1 || degree (G, x) <= 1) |
---|
797 | return 0; |
---|
798 | CanonicalForm f= swapvar (F, F.mvar(), x); |
---|
799 | CanonicalForm g= swapvar (G, G.mvar(), x); |
---|
800 | int sizef= 0; |
---|
801 | int sizeg= 0; |
---|
802 | for (CFIterator i= f; i.hasTerms(); i++, sizef++) |
---|
803 | { |
---|
804 | if (i.exp() == 1) |
---|
805 | return 0; |
---|
806 | } |
---|
807 | for (CFIterator i= g; i.hasTerms(); i++, sizeg++) |
---|
808 | { |
---|
809 | if (i.exp() == 1) |
---|
810 | return 0; |
---|
811 | } |
---|
812 | int * expf= new int [sizef]; |
---|
813 | int * expg= new int [sizeg]; |
---|
814 | int j= 0; |
---|
815 | for (CFIterator i= f; i.hasTerms(); i++, j++) |
---|
816 | { |
---|
817 | expf [j]= i.exp(); |
---|
818 | } |
---|
819 | j= 0; |
---|
820 | for (CFIterator i= g; i.hasTerms(); i++, j++) |
---|
821 | { |
---|
822 | expg [j]= i.exp(); |
---|
823 | } |
---|
824 | |
---|
825 | int indf= sizef - 1; |
---|
826 | int indg= sizeg - 1; |
---|
827 | if (expf[indf] == 0) |
---|
828 | indf--; |
---|
829 | if (expg[indg] == 0) |
---|
830 | indg--; |
---|
831 | |
---|
832 | if ((expg[indg]%expf [indf] != 0 && expf[indf]%expg[indg] != 0) || |
---|
833 | (expg[indg] == 1 && expf[indf] == 1)) |
---|
834 | { |
---|
835 | delete [] expg; |
---|
836 | delete [] expf; |
---|
837 | return 0; |
---|
838 | } |
---|
839 | |
---|
840 | int result; |
---|
841 | if (expg [indg]%expf [indf] == 0) |
---|
842 | result= expf[indf]; |
---|
843 | else |
---|
844 | result= expg[indg]; |
---|
845 | for (int i= indf - 1; i >= 0; i--) |
---|
846 | { |
---|
847 | if (expf [i]%result != 0) |
---|
848 | { |
---|
849 | delete [] expf; |
---|
850 | delete [] expg; |
---|
851 | return 0; |
---|
852 | } |
---|
853 | } |
---|
854 | |
---|
855 | for (int i= indg - 1; i >= 0; i--) |
---|
856 | { |
---|
857 | if (expg [i]%result != 0) |
---|
858 | { |
---|
859 | delete [] expf; |
---|
860 | delete [] expg; |
---|
861 | return 0; |
---|
862 | } |
---|
863 | } |
---|
864 | |
---|
865 | delete [] expg; |
---|
866 | delete [] expf; |
---|
867 | return result; |
---|
868 | } |
---|
869 | |
---|
870 | int recSubstituteCheck (const CanonicalForm& F, const int d) |
---|
871 | { |
---|
872 | if (F.inCoeffDomain()) |
---|
873 | return 0; |
---|
874 | Variable x= Variable (1); |
---|
875 | if (degree (F, x) <= 1) |
---|
876 | return 0; |
---|
877 | CanonicalForm f= swapvar (F, F.mvar(), x); |
---|
878 | int sizef= 0; |
---|
879 | for (CFIterator i= f; i.hasTerms(); i++, sizef++) |
---|
880 | { |
---|
881 | if (i.exp() == 1) |
---|
882 | return 0; |
---|
883 | } |
---|
884 | int * expf= new int [sizef]; |
---|
885 | int j= 0; |
---|
886 | for (CFIterator i= f; i.hasTerms(); i++, j++) |
---|
887 | { |
---|
888 | expf [j]= i.exp(); |
---|
889 | } |
---|
890 | |
---|
891 | int indf= sizef - 1; |
---|
892 | if (expf[indf] == 0) |
---|
893 | indf--; |
---|
894 | |
---|
895 | if ((d%expf [indf] != 0 && expf[indf]%d != 0) || (expf[indf] == 1)) |
---|
896 | { |
---|
897 | delete [] expf; |
---|
898 | return 0; |
---|
899 | } |
---|
900 | |
---|
901 | int result; |
---|
902 | if (d%expf [indf] == 0) |
---|
903 | result= expf[indf]; |
---|
904 | else |
---|
905 | result= d; |
---|
906 | for (int i= indf - 1; i >= 0; i--) |
---|
907 | { |
---|
908 | if (expf [i]%result != 0) |
---|
909 | { |
---|
910 | delete [] expf; |
---|
911 | return 0; |
---|
912 | } |
---|
913 | } |
---|
914 | |
---|
915 | delete [] expf; |
---|
916 | return result; |
---|
917 | } |
---|
918 | |
---|
919 | int substituteCheck (const CFList& L) |
---|
920 | { |
---|
921 | ASSERT (L.length() > 1, "expected a list of at least two elements"); |
---|
922 | if (L.length() < 2) |
---|
923 | return 0; |
---|
924 | CFListIterator i= L; |
---|
925 | i++; |
---|
926 | int result= substituteCheck (L.getFirst(), i.getItem()); |
---|
927 | if (result <= 1) |
---|
928 | return result; |
---|
929 | i++; |
---|
930 | for (;i.hasItem(); i++) |
---|
931 | { |
---|
932 | result= recSubstituteCheck (i.getItem(), result); |
---|
933 | if (result <= 1) |
---|
934 | return result; |
---|
935 | } |
---|
936 | return result; |
---|
937 | } |
---|
938 | |
---|
939 | void |
---|
940 | subst (const CanonicalForm& F, CanonicalForm& A, const int d, const Variable& x) |
---|
941 | { |
---|
942 | if (d <= 1) |
---|
943 | { |
---|
944 | A= F; |
---|
945 | return; |
---|
946 | } |
---|
947 | if (degree (F, x) <= 0) |
---|
948 | { |
---|
949 | A= F; |
---|
950 | return; |
---|
951 | } |
---|
952 | CanonicalForm C= 0; |
---|
953 | CanonicalForm f= swapvar (F, x, F.mvar()); |
---|
954 | for (CFIterator i= f; i.hasTerms(); i++) |
---|
955 | C += i.coeff()*power (f.mvar(), i.exp()/ d); |
---|
956 | A= swapvar (C, x, F.mvar()); |
---|
957 | } |
---|
958 | |
---|
959 | CanonicalForm |
---|
960 | reverseSubst (const CanonicalForm& F, const int d, const Variable& x) |
---|
961 | { |
---|
962 | if (d <= 1) |
---|
963 | return F; |
---|
964 | if (degree (F, x) <= 0) |
---|
965 | return F; |
---|
966 | CanonicalForm f= swapvar (F, x, F.mvar()); |
---|
967 | CanonicalForm result= 0; |
---|
968 | for (CFIterator i= f; i.hasTerms(); i++) |
---|
969 | result += i.coeff()*power (f.mvar(), d*i.exp()); |
---|
970 | return swapvar (result, x, F.mvar()); |
---|
971 | } |
---|
972 | |
---|
973 | void |
---|
974 | reverseSubst (CFList& L, const int d, const Variable& x) |
---|
975 | { |
---|
976 | for (CFListIterator i= L; i.hasItem(); i++) |
---|
977 | i.getItem()= reverseSubst (i.getItem(), d, x); |
---|
978 | } |
---|
979 | |
---|