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 | int j= degree (logDeriv, y) + 1; |
---|
457 | CFArray result= CFArray (j); |
---|
458 | CFIterator ii; |
---|
459 | for (CFIterator i= logDeriv; i.hasTerms() && !logDeriv.isZero(); i++) |
---|
460 | { |
---|
461 | for (ii= i.coeff(); ii.hasTerms(); ii++) |
---|
462 | result[ii.exp()] += ii.coeff()*power (x,i.exp()); |
---|
463 | } |
---|
464 | Q= q; |
---|
465 | return result; |
---|
466 | } |
---|
467 | |
---|
468 | CFArray |
---|
469 | logarithmicDerivative (const CanonicalForm& F, const CanonicalForm& G, int l, |
---|
470 | int oldL, const CanonicalForm& oldQ, CanonicalForm& Q |
---|
471 | ) |
---|
472 | { |
---|
473 | Variable x= Variable (2); |
---|
474 | Variable y= Variable (1); |
---|
475 | CanonicalForm xToL= power (x, l); |
---|
476 | CanonicalForm xToOldL= power (x, oldL); |
---|
477 | CanonicalForm xToLOldL= power (x, l-oldL); |
---|
478 | CanonicalForm q,r; |
---|
479 | CanonicalForm logDeriv; |
---|
480 | |
---|
481 | CanonicalForm bufF; |
---|
482 | if ((oldL > 100 && l - oldL < 50) || (oldL < 100 && l - oldL < 30)) |
---|
483 | { |
---|
484 | bufF= F; |
---|
485 | CanonicalForm oldF= mulMod2 (G, oldQ, xToL); |
---|
486 | bufF -= oldF; |
---|
487 | bufF= div (bufF, xToOldL); |
---|
488 | } |
---|
489 | else |
---|
490 | { |
---|
491 | //middle product style computation of [G*oldQ]^{l}_{oldL} |
---|
492 | CanonicalForm G3= div (G, xToOldL); |
---|
493 | CanonicalForm Up= mulMod2 (G3, oldQ, xToLOldL); |
---|
494 | CanonicalForm xToOldL2= power (x, (oldL+1)/2); |
---|
495 | CanonicalForm G2= mod (G, xToOldL); |
---|
496 | CanonicalForm G1= div (G2, xToOldL2); |
---|
497 | CanonicalForm G0= mod (G2, xToOldL2); |
---|
498 | CanonicalForm oldQ1= div (oldQ, xToOldL2); |
---|
499 | CanonicalForm oldQ0= mod (oldQ, xToOldL2); |
---|
500 | CanonicalForm Mid; |
---|
501 | if (oldL % 2 == 1) |
---|
502 | Mid= mulMod2 (G1, oldQ1*x, xToLOldL); |
---|
503 | else |
---|
504 | Mid= mulMod2 (G1, oldQ1, xToLOldL); |
---|
505 | //computation of Low might be faster using a real middle product? |
---|
506 | CanonicalForm Low= mulMod2 (G0, oldQ1, xToOldL)+mulMod2 (G1, oldQ0, xToOldL); |
---|
507 | Low= div (Low, power (x, oldL/2)); |
---|
508 | Low= mod (Low, xToLOldL); |
---|
509 | Up += Mid + Low; |
---|
510 | bufF= div (F, xToOldL); |
---|
511 | bufF -= Up; |
---|
512 | } |
---|
513 | |
---|
514 | if (l-oldL > 0) |
---|
515 | q= newtonDiv (bufF, G, xToLOldL); |
---|
516 | else |
---|
517 | q= 0; |
---|
518 | q *= xToOldL; |
---|
519 | q += oldQ; |
---|
520 | |
---|
521 | logDeriv= mulMod2 (q, deriv (G, y), xToL); |
---|
522 | |
---|
523 | int j= degree (logDeriv,y) + 1; |
---|
524 | CFArray result= CFArray (j); |
---|
525 | CFIterator ii; |
---|
526 | for (CFIterator i= logDeriv; i.hasTerms() && !logDeriv.isZero(); i++) |
---|
527 | { |
---|
528 | for (ii= i.coeff(); ii.hasTerms(); ii++) |
---|
529 | result[ii.exp()] += ii.coeff()*power (x,i.exp()); |
---|
530 | } |
---|
531 | Q= q; |
---|
532 | return result; |
---|
533 | } |
---|
534 | #endif |
---|
535 | |
---|
536 | void |
---|
537 | writeInMatrix (CFMatrix& M, const CFArray& A, const int column, |
---|
538 | const int startIndex |
---|
539 | ) |
---|
540 | { |
---|
541 | ASSERT (A.size () - startIndex >= 0, "wrong starting index"); |
---|
542 | ASSERT (A.size () - startIndex <= M.rows(), "wrong starting index"); |
---|
543 | ASSERT (column > 0 && column <= M.columns(), "wrong column"); |
---|
544 | if (A.size() - startIndex <= 0) return; |
---|
545 | int j= 1; |
---|
546 | for (int i= startIndex; i < A.size(); i++, j++) |
---|
547 | M (j, column)= A [i]; |
---|
548 | } |
---|
549 | |
---|
550 | CFArray getCoeffs (const CanonicalForm& F, const int k) |
---|
551 | { |
---|
552 | ASSERT (F.isUnivariate() || F.inCoeffDomain(), "univariate input expected"); |
---|
553 | if (degree (F, 2) < k) |
---|
554 | return CFArray(); |
---|
555 | |
---|
556 | CFArray result= CFArray (degree (F) - k + 1); |
---|
557 | CFIterator j= F; |
---|
558 | for (int i= degree (F); i >= k; i--) |
---|
559 | { |
---|
560 | if (j.exp() == i) |
---|
561 | { |
---|
562 | result [i - k]= j.coeff(); |
---|
563 | j++; |
---|
564 | if (!j.hasTerms()) |
---|
565 | return result; |
---|
566 | } |
---|
567 | else |
---|
568 | result[i - k]= 0; |
---|
569 | } |
---|
570 | return result; |
---|
571 | } |
---|
572 | |
---|
573 | CFArray getCoeffs (const CanonicalForm& F, const int k, const Variable& alpha) |
---|
574 | { |
---|
575 | ASSERT (F.isUnivariate() || F.inCoeffDomain(), "univariate input expected"); |
---|
576 | if (degree (F, 2) < k) |
---|
577 | return CFArray (); |
---|
578 | |
---|
579 | int d= degree (getMipo (alpha)); |
---|
580 | CFArray result= CFArray ((degree (F) - k + 1)*d); |
---|
581 | CFIterator j= F; |
---|
582 | CanonicalForm buf; |
---|
583 | CFIterator iter; |
---|
584 | for (int i= degree (F); i >= k; i--) |
---|
585 | { |
---|
586 | if (j.exp() == i) |
---|
587 | { |
---|
588 | iter= j.coeff(); |
---|
589 | for (int l= degree (j.coeff(), alpha); l >= 0; l--) |
---|
590 | { |
---|
591 | if (iter.exp() == l) |
---|
592 | { |
---|
593 | result [(i - k)*d + l]= iter.coeff(); |
---|
594 | iter++; |
---|
595 | if (!iter.hasTerms()) |
---|
596 | break; |
---|
597 | } |
---|
598 | } |
---|
599 | j++; |
---|
600 | if (!j.hasTerms()) |
---|
601 | return result; |
---|
602 | } |
---|
603 | else |
---|
604 | { |
---|
605 | for (int l= 0; l < d; l++) |
---|
606 | result[(i - k)*d + l]= 0; |
---|
607 | } |
---|
608 | } |
---|
609 | return result; |
---|
610 | } |
---|
611 | |
---|
612 | #ifdef HAVE_NTL |
---|
613 | CFArray |
---|
614 | getCoeffs (const CanonicalForm& G, const int k, const int l, const int degMipo, |
---|
615 | const Variable& alpha, const CanonicalForm& evaluation, |
---|
616 | const mat_zz_p& M) |
---|
617 | { |
---|
618 | ASSERT (G.isUnivariate() || G.inCoeffDomain(), "univariate input expected"); |
---|
619 | CanonicalForm F= G (G.mvar() - evaluation, G.mvar()); |
---|
620 | if (F.isZero()) |
---|
621 | return CFArray (); |
---|
622 | |
---|
623 | Variable y= Variable (2); |
---|
624 | F= F (power (y, degMipo), y); |
---|
625 | F= F (y, alpha); |
---|
626 | zz_pX NTLF= convertFacCF2NTLzzpX (F); |
---|
627 | NTLF.rep.SetLength (l*degMipo); |
---|
628 | NTLF.rep= M*NTLF.rep; |
---|
629 | NTLF.normalize(); |
---|
630 | F= convertNTLzzpX2CF (NTLF, y); |
---|
631 | |
---|
632 | if (degree (F, 2) < k) |
---|
633 | return CFArray(); |
---|
634 | |
---|
635 | CFArray result= CFArray (degree (F) - k + 1); |
---|
636 | |
---|
637 | CFIterator j= F; |
---|
638 | for (int i= degree (F); i >= k; i--) |
---|
639 | { |
---|
640 | if (j.exp() == i) |
---|
641 | { |
---|
642 | result [i - k]= j.coeff(); |
---|
643 | j++; |
---|
644 | if (!j.hasTerms()) |
---|
645 | return result; |
---|
646 | } |
---|
647 | else |
---|
648 | result[i - k]= 0; |
---|
649 | } |
---|
650 | return result; |
---|
651 | } |
---|
652 | #endif |
---|
653 | |
---|
654 | int * computeBounds (const CanonicalForm& F, int& n, bool& isIrreducible) |
---|
655 | { |
---|
656 | n= degree (F, 1); |
---|
657 | int* result= new int [n]; |
---|
658 | int sizeOfNewtonPolygon; |
---|
659 | int** newtonPolyg= newtonPolygon (F, sizeOfNewtonPolygon); |
---|
660 | |
---|
661 | isIrreducible= false; |
---|
662 | if (sizeOfNewtonPolygon == 3) |
---|
663 | { |
---|
664 | bool check1= |
---|
665 | (newtonPolyg[0][0]==0 || newtonPolyg[1][0]==0 || newtonPolyg[2][0]==0); |
---|
666 | if (check1) |
---|
667 | { |
---|
668 | bool check2= |
---|
669 | (newtonPolyg[0][1]==0 || newtonPolyg[1][1]==0 || newtonPolyg[2][0]==0); |
---|
670 | if (check2) |
---|
671 | { |
---|
672 | int p=getCharacteristic(); |
---|
673 | int d=1; |
---|
674 | char bufGFName='Z'; |
---|
675 | bool GF= (CFFactory::gettype()==GaloisFieldDomain); |
---|
676 | if (GF) |
---|
677 | { |
---|
678 | d= getGFDegree(); |
---|
679 | bufGFName=gf_name; |
---|
680 | } |
---|
681 | setCharacteristic(0); |
---|
682 | CanonicalForm tmp= gcd (newtonPolyg[0][0],newtonPolyg[0][1]); |
---|
683 | tmp= gcd (tmp, newtonPolyg[1][0]); |
---|
684 | tmp= gcd (tmp, newtonPolyg[1][1]); |
---|
685 | tmp= gcd (tmp, newtonPolyg[2][0]); |
---|
686 | tmp= gcd (tmp, newtonPolyg[2][1]); |
---|
687 | isIrreducible= (tmp==1); |
---|
688 | if (GF) |
---|
689 | setCharacteristic (p, d, bufGFName); |
---|
690 | else |
---|
691 | setCharacteristic(p); |
---|
692 | } |
---|
693 | } |
---|
694 | } |
---|
695 | |
---|
696 | int minX, minY, maxX, maxY; |
---|
697 | minX= newtonPolyg [0] [0]; |
---|
698 | minY= newtonPolyg [0] [1]; |
---|
699 | maxX= minX; |
---|
700 | maxY= minY; |
---|
701 | for (int i= 1; i < sizeOfNewtonPolygon; i++) |
---|
702 | { |
---|
703 | if (minX > newtonPolyg [i] [0]) |
---|
704 | minX= newtonPolyg [i] [0]; |
---|
705 | if (maxX < newtonPolyg [i] [0]) |
---|
706 | maxX= newtonPolyg [i] [0]; |
---|
707 | if (minY > newtonPolyg [i] [1]) |
---|
708 | minY= newtonPolyg [i] [1]; |
---|
709 | if (maxY < newtonPolyg [i] [1]) |
---|
710 | maxY= newtonPolyg [i] [1]; |
---|
711 | } |
---|
712 | |
---|
713 | int k= maxX; |
---|
714 | for (int i= 0; i < n; i++) |
---|
715 | { |
---|
716 | if (i + 1 > maxY || i + 1 < minY) |
---|
717 | { |
---|
718 | result [i]= 0; |
---|
719 | continue; |
---|
720 | } |
---|
721 | int* point= new int [2]; |
---|
722 | point [0]= k; |
---|
723 | point [1]= i + 1; |
---|
724 | while (!isInPolygon (newtonPolyg, sizeOfNewtonPolygon, point) && k > 0) |
---|
725 | { |
---|
726 | k--; |
---|
727 | point [0]= k; |
---|
728 | } |
---|
729 | result [i]= k; |
---|
730 | k= maxX; |
---|
731 | delete [] point; |
---|
732 | } |
---|
733 | |
---|
734 | return result; |
---|
735 | } |
---|
736 | |
---|
737 | int * |
---|
738 | computeBoundsWrtDiffMainvar (const CanonicalForm& F, int& n, |
---|
739 | bool& isIrreducible) |
---|
740 | { |
---|
741 | n= degree (F, 2); |
---|
742 | int* result= new int [n]; |
---|
743 | int sizeOfNewtonPolygon; |
---|
744 | int** newtonPolyg= newtonPolygon (F, sizeOfNewtonPolygon); |
---|
745 | |
---|
746 | isIrreducible= false; |
---|
747 | if (sizeOfNewtonPolygon == 3) |
---|
748 | { |
---|
749 | bool check1= |
---|
750 | (newtonPolyg[0][0]==0 || newtonPolyg[1][0]==0 || newtonPolyg[2][0]==0); |
---|
751 | if (check1) |
---|
752 | { |
---|
753 | bool check2= |
---|
754 | (newtonPolyg[0][1]==0 || newtonPolyg[1][1]==0 || newtonPolyg[2][0]==0); |
---|
755 | if (check2) |
---|
756 | { |
---|
757 | int p=getCharacteristic(); |
---|
758 | int d=1; |
---|
759 | char bufGFName='Z'; |
---|
760 | bool GF= (CFFactory::gettype()==GaloisFieldDomain); |
---|
761 | if (GF) |
---|
762 | { |
---|
763 | d= getGFDegree(); |
---|
764 | bufGFName=gf_name; |
---|
765 | } |
---|
766 | setCharacteristic(0); |
---|
767 | CanonicalForm tmp= gcd (newtonPolyg[0][0],newtonPolyg[0][1]); |
---|
768 | tmp= gcd (tmp, newtonPolyg[1][0]); |
---|
769 | tmp= gcd (tmp, newtonPolyg[1][1]); |
---|
770 | tmp= gcd (tmp, newtonPolyg[2][0]); |
---|
771 | tmp= gcd (tmp, newtonPolyg[2][1]); |
---|
772 | isIrreducible= (tmp==1); |
---|
773 | if (GF) |
---|
774 | setCharacteristic (p, d, bufGFName); |
---|
775 | else |
---|
776 | setCharacteristic(p); |
---|
777 | } |
---|
778 | } |
---|
779 | } |
---|
780 | |
---|
781 | int minX, minY, maxX, maxY; |
---|
782 | minX= newtonPolyg [0] [0]; |
---|
783 | minY= newtonPolyg [0] [1]; |
---|
784 | maxX= minX; |
---|
785 | maxY= minY; |
---|
786 | for (int i= 1; i < sizeOfNewtonPolygon; i++) |
---|
787 | { |
---|
788 | if (minX > newtonPolyg [i] [0]) |
---|
789 | minX= newtonPolyg [i] [0]; |
---|
790 | if (maxX < newtonPolyg [i] [0]) |
---|
791 | maxX= newtonPolyg [i] [0]; |
---|
792 | if (minY > newtonPolyg [i] [1]) |
---|
793 | minY= newtonPolyg [i] [1]; |
---|
794 | if (maxY < newtonPolyg [i] [1]) |
---|
795 | maxY= newtonPolyg [i] [1]; |
---|
796 | } |
---|
797 | |
---|
798 | int k= maxY; |
---|
799 | for (int i= 0; i < n; i++) |
---|
800 | { |
---|
801 | if (i + 1 > maxX || i + 1 < minX) |
---|
802 | { |
---|
803 | result [i]= 0; |
---|
804 | continue; |
---|
805 | } |
---|
806 | int* point= new int [2]; |
---|
807 | point [0]= i + 1; |
---|
808 | point [1]= k; |
---|
809 | while (!isInPolygon (newtonPolyg, sizeOfNewtonPolygon, point) && k > 0) |
---|
810 | { |
---|
811 | k--; |
---|
812 | point [1]= k; |
---|
813 | } |
---|
814 | result [i]= k; |
---|
815 | k= maxY; |
---|
816 | delete [] point; |
---|
817 | } |
---|
818 | |
---|
819 | return result; |
---|
820 | } |
---|
821 | |
---|
822 | int |
---|
823 | substituteCheck (const CanonicalForm& F, const Variable& x) |
---|
824 | { |
---|
825 | if (F.inCoeffDomain()) |
---|
826 | return 0; |
---|
827 | if (degree (F, x) < 0) |
---|
828 | return 0; |
---|
829 | CanonicalForm f= swapvar (F, F.mvar(), x); |
---|
830 | int sizef= 0; |
---|
831 | for (CFIterator i= f; i.hasTerms(); i++, sizef++) |
---|
832 | { |
---|
833 | if (i.exp() == 1) |
---|
834 | return 0; |
---|
835 | } |
---|
836 | int * expf= new int [sizef]; |
---|
837 | int j= 0; |
---|
838 | for (CFIterator i= f; i.hasTerms(); i++, j++) |
---|
839 | expf [j]= i.exp(); |
---|
840 | |
---|
841 | int indf= sizef - 1; |
---|
842 | if (expf[indf] == 0) |
---|
843 | indf--; |
---|
844 | |
---|
845 | int result= expf[indf]; |
---|
846 | for (int i= indf - 1; i >= 0; i--) |
---|
847 | { |
---|
848 | if (expf [i]%result != 0) |
---|
849 | { |
---|
850 | delete [] expf; |
---|
851 | return 0; |
---|
852 | } |
---|
853 | } |
---|
854 | |
---|
855 | delete [] expf; |
---|
856 | return result; |
---|
857 | } |
---|
858 | |
---|
859 | static int |
---|
860 | substituteCheck (const CanonicalForm& F, const CanonicalForm& G) |
---|
861 | { |
---|
862 | if (F.inCoeffDomain() || G.inCoeffDomain()) |
---|
863 | return 0; |
---|
864 | Variable x= Variable (1); |
---|
865 | if (degree (F, x) <= 1 || degree (G, x) <= 1) |
---|
866 | return 0; |
---|
867 | CanonicalForm f= swapvar (F, F.mvar(), x); |
---|
868 | CanonicalForm g= swapvar (G, G.mvar(), x); |
---|
869 | int sizef= 0; |
---|
870 | int sizeg= 0; |
---|
871 | for (CFIterator i= f; i.hasTerms(); i++, sizef++) |
---|
872 | { |
---|
873 | if (i.exp() == 1) |
---|
874 | return 0; |
---|
875 | } |
---|
876 | for (CFIterator i= g; i.hasTerms(); i++, sizeg++) |
---|
877 | { |
---|
878 | if (i.exp() == 1) |
---|
879 | return 0; |
---|
880 | } |
---|
881 | int * expf= new int [sizef]; |
---|
882 | int * expg= new int [sizeg]; |
---|
883 | int j= 0; |
---|
884 | for (CFIterator i= f; i.hasTerms(); i++, j++) |
---|
885 | { |
---|
886 | expf [j]= i.exp(); |
---|
887 | } |
---|
888 | j= 0; |
---|
889 | for (CFIterator i= g; i.hasTerms(); i++, j++) |
---|
890 | { |
---|
891 | expg [j]= i.exp(); |
---|
892 | } |
---|
893 | |
---|
894 | int indf= sizef - 1; |
---|
895 | int indg= sizeg - 1; |
---|
896 | if (expf[indf] == 0) |
---|
897 | indf--; |
---|
898 | if (expg[indg] == 0) |
---|
899 | indg--; |
---|
900 | |
---|
901 | if ((expg[indg]%expf [indf] != 0 && expf[indf]%expg[indg] != 0) || |
---|
902 | (expg[indg] == 1 && expf[indf] == 1)) |
---|
903 | { |
---|
904 | delete [] expg; |
---|
905 | delete [] expf; |
---|
906 | return 0; |
---|
907 | } |
---|
908 | |
---|
909 | int result; |
---|
910 | if (expg [indg]%expf [indf] == 0) |
---|
911 | result= expf[indf]; |
---|
912 | else |
---|
913 | result= expg[indg]; |
---|
914 | for (int i= indf - 1; i >= 0; i--) |
---|
915 | { |
---|
916 | if (expf [i]%result != 0) |
---|
917 | { |
---|
918 | delete [] expf; |
---|
919 | delete [] expg; |
---|
920 | return 0; |
---|
921 | } |
---|
922 | } |
---|
923 | |
---|
924 | for (int i= indg - 1; i >= 0; i--) |
---|
925 | { |
---|
926 | if (expg [i]%result != 0) |
---|
927 | { |
---|
928 | delete [] expf; |
---|
929 | delete [] expg; |
---|
930 | return 0; |
---|
931 | } |
---|
932 | } |
---|
933 | |
---|
934 | delete [] expg; |
---|
935 | delete [] expf; |
---|
936 | return result; |
---|
937 | } |
---|
938 | |
---|
939 | int recSubstituteCheck (const CanonicalForm& F, const int d) |
---|
940 | { |
---|
941 | if (F.inCoeffDomain()) |
---|
942 | return 0; |
---|
943 | Variable x= Variable (1); |
---|
944 | if (degree (F, x) <= 1) |
---|
945 | return 0; |
---|
946 | CanonicalForm f= swapvar (F, F.mvar(), x); |
---|
947 | int sizef= 0; |
---|
948 | for (CFIterator i= f; i.hasTerms(); i++, sizef++) |
---|
949 | { |
---|
950 | if (i.exp() == 1) |
---|
951 | return 0; |
---|
952 | } |
---|
953 | int * expf= new int [sizef]; |
---|
954 | int j= 0; |
---|
955 | for (CFIterator i= f; i.hasTerms(); i++, j++) |
---|
956 | { |
---|
957 | expf [j]= i.exp(); |
---|
958 | } |
---|
959 | |
---|
960 | int indf= sizef - 1; |
---|
961 | if (expf[indf] == 0) |
---|
962 | indf--; |
---|
963 | |
---|
964 | if ((d%expf [indf] != 0 && expf[indf]%d != 0) || (expf[indf] == 1)) |
---|
965 | { |
---|
966 | delete [] expf; |
---|
967 | return 0; |
---|
968 | } |
---|
969 | |
---|
970 | int result; |
---|
971 | if (d%expf [indf] == 0) |
---|
972 | result= expf[indf]; |
---|
973 | else |
---|
974 | result= d; |
---|
975 | for (int i= indf - 1; i >= 0; i--) |
---|
976 | { |
---|
977 | if (expf [i]%result != 0) |
---|
978 | { |
---|
979 | delete [] expf; |
---|
980 | return 0; |
---|
981 | } |
---|
982 | } |
---|
983 | |
---|
984 | delete [] expf; |
---|
985 | return result; |
---|
986 | } |
---|
987 | |
---|
988 | int substituteCheck (const CFList& L) |
---|
989 | { |
---|
990 | ASSERT (L.length() > 1, "expected a list of at least two elements"); |
---|
991 | if (L.length() < 2) |
---|
992 | return 0; |
---|
993 | CFListIterator i= L; |
---|
994 | i++; |
---|
995 | int result= substituteCheck (L.getFirst(), i.getItem()); |
---|
996 | if (result <= 1) |
---|
997 | return result; |
---|
998 | i++; |
---|
999 | for (;i.hasItem(); i++) |
---|
1000 | { |
---|
1001 | result= recSubstituteCheck (i.getItem(), result); |
---|
1002 | if (result <= 1) |
---|
1003 | return result; |
---|
1004 | } |
---|
1005 | return result; |
---|
1006 | } |
---|
1007 | |
---|
1008 | void |
---|
1009 | subst (const CanonicalForm& F, CanonicalForm& A, const int d, const Variable& x) |
---|
1010 | { |
---|
1011 | if (d <= 1) |
---|
1012 | { |
---|
1013 | A= F; |
---|
1014 | return; |
---|
1015 | } |
---|
1016 | if (degree (F, x) <= 0) |
---|
1017 | { |
---|
1018 | A= F; |
---|
1019 | return; |
---|
1020 | } |
---|
1021 | CanonicalForm C= 0; |
---|
1022 | CanonicalForm f= swapvar (F, x, F.mvar()); |
---|
1023 | for (CFIterator i= f; i.hasTerms(); i++) |
---|
1024 | C += i.coeff()*power (f.mvar(), i.exp()/ d); |
---|
1025 | A= swapvar (C, x, F.mvar()); |
---|
1026 | } |
---|
1027 | |
---|
1028 | CanonicalForm |
---|
1029 | reverseSubst (const CanonicalForm& F, const int d, const Variable& x) |
---|
1030 | { |
---|
1031 | if (d <= 1) |
---|
1032 | return F; |
---|
1033 | if (degree (F, x) <= 0) |
---|
1034 | return F; |
---|
1035 | CanonicalForm f= swapvar (F, x, F.mvar()); |
---|
1036 | CanonicalForm result= 0; |
---|
1037 | for (CFIterator i= f; i.hasTerms(); i++) |
---|
1038 | result += i.coeff()*power (f.mvar(), d*i.exp()); |
---|
1039 | return swapvar (result, x, F.mvar()); |
---|
1040 | } |
---|
1041 | |
---|
1042 | void |
---|
1043 | reverseSubst (CFList& L, const int d, const Variable& x) |
---|
1044 | { |
---|
1045 | for (CFListIterator i= L; i.hasItem(); i++) |
---|
1046 | i.getItem()= reverseSubst (i.getItem(), d, x); |
---|
1047 | } |
---|
1048 | |
---|