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 | * @internal @version \$Id$ |
---|
11 | * |
---|
12 | **/ |
---|
13 | /*****************************************************************************/ |
---|
14 | |
---|
15 | #include <config.h> |
---|
16 | |
---|
17 | #include "cf_map.h" |
---|
18 | #include "cf_map_ext.h" |
---|
19 | #include "templates/ftmpl_functions.h" |
---|
20 | #include "ExtensionInfo.h" |
---|
21 | #include "cf_algorithm.h" |
---|
22 | #include "cf_factory.h" |
---|
23 | #include "cf_util.h" |
---|
24 | #include "imm.h" |
---|
25 | #include "cf_iter.h" |
---|
26 | #include "facFqBivarUtil.h" |
---|
27 | #include "cfNewtonPolygon.h" |
---|
28 | #include "facHensel.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 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
272 | i.getItem() /= Lc(i.getItem()); |
---|
273 | return; |
---|
274 | } |
---|
275 | |
---|
276 | void normalize (CFFList& factors) |
---|
277 | { |
---|
278 | for (CFFListIterator i= factors; i.hasItem(); i++) |
---|
279 | i.getItem()= CFFactor (i.getItem().factor()/Lc(i.getItem().factor()), |
---|
280 | i.getItem().exp()); |
---|
281 | return; |
---|
282 | } |
---|
283 | |
---|
284 | CFList subset (int index [], const int& s, const CFArray& elements, |
---|
285 | bool& noSubset) |
---|
286 | { |
---|
287 | int r= elements.size(); |
---|
288 | int i= 0; |
---|
289 | CFList result; |
---|
290 | noSubset= false; |
---|
291 | if (index[s - 1] == 0) |
---|
292 | { |
---|
293 | while (i < s) |
---|
294 | { |
---|
295 | index[i]= i + 1; |
---|
296 | result.append (elements[i]); |
---|
297 | i++; |
---|
298 | } |
---|
299 | return result; |
---|
300 | } |
---|
301 | int buf; |
---|
302 | int k; |
---|
303 | bool found= false; |
---|
304 | if (index[s - 1] == r) |
---|
305 | { |
---|
306 | if (index[0] == r - s + 1) |
---|
307 | { |
---|
308 | noSubset= true; |
---|
309 | return result; |
---|
310 | } |
---|
311 | else { |
---|
312 | while (found == false) |
---|
313 | { |
---|
314 | if (index[s - 2 - i] < r - i - 1) |
---|
315 | found= true; |
---|
316 | i++; |
---|
317 | } |
---|
318 | buf= index[s - i - 1]; |
---|
319 | k= 0; |
---|
320 | while (s - i - 1 + k < s) |
---|
321 | { |
---|
322 | index[s - i - 1 + k]= buf + k + 1; |
---|
323 | k++; |
---|
324 | } |
---|
325 | } |
---|
326 | for (int j= 0; j < s; j++) |
---|
327 | result.append (elements[index[j] - 1]); |
---|
328 | return result; |
---|
329 | } |
---|
330 | else |
---|
331 | { |
---|
332 | index[s - 1] += 1; |
---|
333 | for (int j= 0; j < s; j++) |
---|
334 | result.append (elements[index[j] - 1]); |
---|
335 | return result; |
---|
336 | } |
---|
337 | } |
---|
338 | |
---|
339 | CFArray copy (const CFList& list) |
---|
340 | { |
---|
341 | CFArray array= CFArray (list.length()); |
---|
342 | int j= 0; |
---|
343 | for (CFListIterator i= list; i.hasItem(); i++, j++) |
---|
344 | array[j]= i.getItem(); |
---|
345 | return array; |
---|
346 | } |
---|
347 | |
---|
348 | void indexUpdate (int index [], const int& subsetSize, const int& setSize, |
---|
349 | bool& noSubset) |
---|
350 | { |
---|
351 | noSubset= false; |
---|
352 | if (subsetSize > setSize) |
---|
353 | { |
---|
354 | noSubset= true; |
---|
355 | return; |
---|
356 | } |
---|
357 | int * v= new int [setSize]; |
---|
358 | for (int i= 0; i < setSize; i++) |
---|
359 | v[i]= index[i]; |
---|
360 | if (subsetSize == 1) |
---|
361 | { |
---|
362 | v[0]= v[0] - 1; |
---|
363 | if (v[0] >= setSize) |
---|
364 | { |
---|
365 | noSubset= true; |
---|
366 | delete [] v; |
---|
367 | return; |
---|
368 | } |
---|
369 | } |
---|
370 | else |
---|
371 | { |
---|
372 | if (v[subsetSize - 1] - v[0] + 1 == subsetSize && v[0] > 1) |
---|
373 | { |
---|
374 | if (v[0] + subsetSize - 1 > setSize) |
---|
375 | { |
---|
376 | noSubset= true; |
---|
377 | delete [] v; |
---|
378 | return; |
---|
379 | } |
---|
380 | v[0]= v[0] - 1; |
---|
381 | for (int i= 1; i < subsetSize - 1; i++) |
---|
382 | v[i]= v[i - 1] + 1; |
---|
383 | v[subsetSize - 1]= v[subsetSize - 2]; |
---|
384 | } |
---|
385 | else |
---|
386 | { |
---|
387 | if (v[0] + subsetSize - 1 > setSize) |
---|
388 | { |
---|
389 | noSubset= true; |
---|
390 | delete [] v; |
---|
391 | return; |
---|
392 | } |
---|
393 | for (int i= 1; i < subsetSize - 1; i++) |
---|
394 | v[i]= v[i - 1] + 1; |
---|
395 | v[subsetSize - 1]= v[subsetSize - 2]; |
---|
396 | } |
---|
397 | } |
---|
398 | for (int i= 0; i < setSize; i++) |
---|
399 | index[i]= v[i]; |
---|
400 | delete [] v; |
---|
401 | } |
---|
402 | |
---|
403 | int subsetDegree (const CFList& S) |
---|
404 | { |
---|
405 | int result= 0; |
---|
406 | for (CFListIterator i= S; i.hasItem(); i++) |
---|
407 | result += degree (i.getItem(), Variable (1)); |
---|
408 | return result; |
---|
409 | } |
---|
410 | |
---|
411 | CFFList multiplicity (CanonicalForm& F, const CFList& factors) |
---|
412 | { |
---|
413 | if (F.inCoeffDomain()) |
---|
414 | return CFFList (CFFactor (F, 1)); |
---|
415 | CFFList result; |
---|
416 | int multi= 0; |
---|
417 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
418 | { |
---|
419 | while (fdivides (i.getItem(), F)) |
---|
420 | { |
---|
421 | multi++; |
---|
422 | F /= i.getItem(); |
---|
423 | } |
---|
424 | if (multi > 0) |
---|
425 | result.append (CFFactor (i.getItem(), multi)); |
---|
426 | multi= 0; |
---|
427 | } |
---|
428 | return result; |
---|
429 | } |
---|
430 | |
---|
431 | CFArray |
---|
432 | logarithmicDerivative (const CanonicalForm& F, const CanonicalForm& G, int l, |
---|
433 | CanonicalForm& Q |
---|
434 | ) |
---|
435 | { |
---|
436 | Variable x= Variable (2); |
---|
437 | Variable y= Variable (1); |
---|
438 | CanonicalForm xToL= power (x, l); |
---|
439 | CanonicalForm q,r; |
---|
440 | CanonicalForm logDeriv; |
---|
441 | |
---|
442 | q= newtonDiv (F, G, xToL); |
---|
443 | |
---|
444 | logDeriv= mulMod2 (q, deriv (G, y), xToL); |
---|
445 | |
---|
446 | logDeriv= swapvar (logDeriv, x, y); |
---|
447 | int j= degree (logDeriv) + 1; |
---|
448 | CFArray result= CFArray (j); |
---|
449 | for (CFIterator i= logDeriv; i.hasTerms() && !logDeriv.isZero(); i++) |
---|
450 | { |
---|
451 | if (i.exp() == j - 1) |
---|
452 | { |
---|
453 | result [j - 1]= swapvar (i.coeff(), x, y); |
---|
454 | j--; |
---|
455 | } |
---|
456 | else |
---|
457 | { |
---|
458 | for (; i.exp() < j - 1; j--) |
---|
459 | result [j - 1]= 0; |
---|
460 | result [j - 1]= swapvar (i.coeff(), x, y); |
---|
461 | j--; |
---|
462 | } |
---|
463 | } |
---|
464 | for (; j > 0; j--) |
---|
465 | result [j - 1]= 0; |
---|
466 | Q= q; |
---|
467 | return result; |
---|
468 | } |
---|
469 | |
---|
470 | CFArray |
---|
471 | logarithmicDerivative (const CanonicalForm& F, const CanonicalForm& G, int l, |
---|
472 | int oldL, const CanonicalForm& oldQ, CanonicalForm& Q |
---|
473 | ) |
---|
474 | { |
---|
475 | Variable x= Variable (2); |
---|
476 | Variable y= Variable (1); |
---|
477 | CanonicalForm xToL= power (x, l); |
---|
478 | CanonicalForm xToOldL= power (x, oldL); |
---|
479 | CanonicalForm xToLOldL= power (x, l-oldL); |
---|
480 | CanonicalForm q,r; |
---|
481 | CanonicalForm logDeriv; |
---|
482 | |
---|
483 | CanonicalForm bufF= mod (F, xToL); |
---|
484 | CanonicalForm oldF= mulMod2 (G, oldQ, xToL); |
---|
485 | bufF -= oldF; |
---|
486 | bufF= div (bufF, xToOldL); |
---|
487 | |
---|
488 | q= newtonDiv (bufF, G, xToLOldL); |
---|
489 | q *= xToOldL; |
---|
490 | q += oldQ; |
---|
491 | |
---|
492 | logDeriv= mulMod2 (q, deriv (G, y), xToL); |
---|
493 | |
---|
494 | logDeriv= swapvar (logDeriv, x, y); |
---|
495 | int j= degree (logDeriv) + 1; |
---|
496 | CFArray result= CFArray (j); |
---|
497 | for (CFIterator i= logDeriv; i.hasTerms() && !logDeriv.isZero(); i++) |
---|
498 | { |
---|
499 | if (i.exp() == j - 1) |
---|
500 | { |
---|
501 | result [j - 1]= swapvar (i.coeff(), x, y); |
---|
502 | j--; |
---|
503 | } |
---|
504 | else |
---|
505 | { |
---|
506 | for (; i.exp() < j - 1; j--) |
---|
507 | result [j - 1]= 0; |
---|
508 | result [j - 1]= swapvar (i.coeff(), x, y); |
---|
509 | j--; |
---|
510 | } |
---|
511 | } |
---|
512 | for (; j > 0; j--) |
---|
513 | result [j - 1]= 0; |
---|
514 | Q= q; |
---|
515 | return result; |
---|
516 | } |
---|
517 | |
---|
518 | void |
---|
519 | writeInMatrix (CFMatrix& M, const CFArray& A, const int column, |
---|
520 | const int startIndex |
---|
521 | ) |
---|
522 | { |
---|
523 | ASSERT (A.size () - startIndex > 0, "wrong starting index"); |
---|
524 | ASSERT (A.size () - startIndex == M.rows(), "wrong starting index"); |
---|
525 | ASSERT (column > 0 && column <= M.columns(), "wrong column"); |
---|
526 | if (A.size() - startIndex <= 0) return; |
---|
527 | int j= 1; |
---|
528 | for (int i= startIndex; i < A.size(); i++, j++) |
---|
529 | M (j, column)= A [i]; |
---|
530 | } |
---|
531 | |
---|
532 | CFArray getCoeffs (const CanonicalForm& F, const int k) |
---|
533 | { |
---|
534 | ASSERT (F.isUnivariate(), "univariate input expected"); |
---|
535 | if (degree (F, 2) < k) |
---|
536 | return CFArray(); |
---|
537 | |
---|
538 | CFArray result= CFArray (degree (F) - k + 1); |
---|
539 | CFIterator j= F; |
---|
540 | for (int i= degree (F); i >= k; i--) |
---|
541 | { |
---|
542 | if (j.exp() == i) |
---|
543 | { |
---|
544 | result [i - k]= j.coeff(); |
---|
545 | j++; |
---|
546 | if (!j.hasTerms()) |
---|
547 | return result; |
---|
548 | } |
---|
549 | else |
---|
550 | result[i - k]= 0; |
---|
551 | } |
---|
552 | return result; |
---|
553 | } |
---|
554 | |
---|
555 | CFArray getCoeffs (const CanonicalForm& F, const int k, const Variable& alpha) |
---|
556 | { |
---|
557 | ASSERT (F.isUnivariate(), "univariate input expected"); |
---|
558 | if (degree (F, 2) < k) |
---|
559 | return CFArray (); |
---|
560 | |
---|
561 | int d= degree (getMipo (alpha)); |
---|
562 | CFArray result= CFArray ((degree (F) - k + 1)*d); |
---|
563 | CFIterator j= F; |
---|
564 | CanonicalForm buf; |
---|
565 | CFIterator iter; |
---|
566 | for (int i= degree (F); i >= k; i--) |
---|
567 | { |
---|
568 | if (j.exp() == i) |
---|
569 | { |
---|
570 | iter= j.coeff(); |
---|
571 | for (int l= degree (j.coeff(), alpha); l >= 0; l--) |
---|
572 | { |
---|
573 | if (iter.exp() == l) |
---|
574 | { |
---|
575 | result [(i - k)*d + l]= iter.coeff(); |
---|
576 | iter++; |
---|
577 | if (!iter.hasTerms()) |
---|
578 | break; |
---|
579 | } |
---|
580 | } |
---|
581 | j++; |
---|
582 | if (!j.hasTerms()) |
---|
583 | return result; |
---|
584 | } |
---|
585 | else |
---|
586 | { |
---|
587 | for (int l= 0; l < d; l++) |
---|
588 | result[(i - k)*d + l]= 0; |
---|
589 | } |
---|
590 | } |
---|
591 | return result; |
---|
592 | } |
---|
593 | |
---|
594 | #ifdef HAVE_NTL |
---|
595 | CFArray |
---|
596 | getCoeffs (const CanonicalForm& G, const int k, const int l, const int degMipo, |
---|
597 | const Variable& alpha, const CanonicalForm& evaluation, |
---|
598 | const mat_zz_p& M) |
---|
599 | { |
---|
600 | ASSERT (G.isUnivariate(), "univariate input expected"); |
---|
601 | CanonicalForm F= G (G.mvar() - evaluation, G.mvar()); |
---|
602 | if (F.isZero()) |
---|
603 | return CFArray (); |
---|
604 | |
---|
605 | Variable y= Variable (2); |
---|
606 | F= F (power (y, degMipo), y); |
---|
607 | F= F (y, alpha); |
---|
608 | zz_pX NTLF= convertFacCF2NTLzzpX (F); |
---|
609 | NTLF.rep.SetLength (l*degMipo); |
---|
610 | NTLF.rep= M*NTLF.rep; |
---|
611 | NTLF.normalize(); |
---|
612 | F= convertNTLzzpX2CF (NTLF, y); |
---|
613 | |
---|
614 | if (degree (F, 2) < k) |
---|
615 | return CFArray(); |
---|
616 | |
---|
617 | CFArray result= CFArray (degree (F) - k + 1); |
---|
618 | |
---|
619 | CFIterator j= F; |
---|
620 | for (int i= degree (F); i >= k; i--) |
---|
621 | { |
---|
622 | if (j.exp() == i) |
---|
623 | { |
---|
624 | result [i - k]= j.coeff(); |
---|
625 | j++; |
---|
626 | if (!j.hasTerms()) |
---|
627 | return result; |
---|
628 | } |
---|
629 | else |
---|
630 | result[i - k]= 0; |
---|
631 | } |
---|
632 | return result; |
---|
633 | } |
---|
634 | #endif |
---|
635 | |
---|
636 | int * computeBounds (const CanonicalForm& F, int& n) |
---|
637 | { |
---|
638 | n= degree (F, 1); |
---|
639 | int* result= new int [n]; |
---|
640 | int sizeOfNewtonPolygon; |
---|
641 | int** newtonPolyg= newtonPolygon (F, sizeOfNewtonPolygon); |
---|
642 | |
---|
643 | int minXIndex= 0, minYIndex= 0, maxXIndex= 0, maxYIndex= 0; |
---|
644 | int minX, minY, maxX, maxY; |
---|
645 | minX= newtonPolyg [0] [0]; |
---|
646 | minY= newtonPolyg [0] [1]; |
---|
647 | maxX= minX; |
---|
648 | maxY= minY; |
---|
649 | for (int i= 1; i < sizeOfNewtonPolygon; i++) |
---|
650 | { |
---|
651 | if (minX > newtonPolyg [i] [0]) |
---|
652 | { |
---|
653 | minX= newtonPolyg [i] [0]; |
---|
654 | minXIndex= i; |
---|
655 | } |
---|
656 | if (maxX < newtonPolyg [i] [0]) |
---|
657 | { |
---|
658 | maxX= newtonPolyg [i] [0]; |
---|
659 | maxXIndex= i; |
---|
660 | } |
---|
661 | if (minY > newtonPolyg [i] [1]) |
---|
662 | { |
---|
663 | minY= newtonPolyg [i] [1]; |
---|
664 | minYIndex= i; |
---|
665 | } |
---|
666 | if (maxY < newtonPolyg [i] [1]) |
---|
667 | { |
---|
668 | maxY= newtonPolyg [i] [1]; |
---|
669 | maxYIndex= i; |
---|
670 | } |
---|
671 | } |
---|
672 | |
---|
673 | int k= maxX; |
---|
674 | for (int i= 0; i < n; i++) |
---|
675 | { |
---|
676 | if (i + 1 > maxY || i + 1 < minY) |
---|
677 | { |
---|
678 | result [i]= 0; |
---|
679 | continue; |
---|
680 | } |
---|
681 | int* point= new int [2]; |
---|
682 | point [0]= k; |
---|
683 | point [1]= i + 1; |
---|
684 | while (!isInPolygon (newtonPolyg, sizeOfNewtonPolygon, point) && k > 0) |
---|
685 | { |
---|
686 | k--; |
---|
687 | point [0]= k; |
---|
688 | } |
---|
689 | result [i]= k; |
---|
690 | k= maxX; |
---|
691 | delete [] point; |
---|
692 | } |
---|
693 | |
---|
694 | return result; |
---|
695 | } |
---|
696 | |
---|
697 | int |
---|
698 | substituteCheck (const CanonicalForm& F, const Variable& x) |
---|
699 | { |
---|
700 | if (F.inCoeffDomain()) |
---|
701 | return 0; |
---|
702 | if (degree (F, x) < 0) |
---|
703 | return 0; |
---|
704 | CanonicalForm f= swapvar (F, F.mvar(), x); |
---|
705 | int sizef= 0; |
---|
706 | for (CFIterator i= f; i.hasTerms(); i++, sizef++) |
---|
707 | { |
---|
708 | if (i.exp() == 1) |
---|
709 | return 0; |
---|
710 | } |
---|
711 | int * expf= new int [sizef]; |
---|
712 | int j= 0; |
---|
713 | for (CFIterator i= f; i.hasTerms(); i++, j++) |
---|
714 | expf [j]= i.exp(); |
---|
715 | |
---|
716 | int indf= sizef - 1; |
---|
717 | if (expf[indf] == 0) |
---|
718 | indf--; |
---|
719 | |
---|
720 | int result= expf[indf]; |
---|
721 | for (int i= indf - 1; i >= 0; i--) |
---|
722 | { |
---|
723 | if (expf [i]%result != 0) |
---|
724 | { |
---|
725 | delete [] expf; |
---|
726 | return 0; |
---|
727 | } |
---|
728 | } |
---|
729 | |
---|
730 | delete [] expf; |
---|
731 | return result; |
---|
732 | } |
---|
733 | |
---|
734 | static int |
---|
735 | substituteCheck (const CanonicalForm& F, const CanonicalForm& G) |
---|
736 | { |
---|
737 | if (F.inCoeffDomain() || G.inCoeffDomain()) |
---|
738 | return 0; |
---|
739 | Variable x= Variable (1); |
---|
740 | if (degree (F, x) <= 1 || degree (G, x) <= 1) |
---|
741 | return 0; |
---|
742 | CanonicalForm f= swapvar (F, F.mvar(), x); |
---|
743 | CanonicalForm g= swapvar (G, G.mvar(), x); |
---|
744 | int sizef= 0; |
---|
745 | int sizeg= 0; |
---|
746 | for (CFIterator i= f; i.hasTerms(); i++, sizef++) |
---|
747 | { |
---|
748 | if (i.exp() == 1) |
---|
749 | return 0; |
---|
750 | } |
---|
751 | for (CFIterator i= g; i.hasTerms(); i++, sizeg++) |
---|
752 | { |
---|
753 | if (i.exp() == 1) |
---|
754 | return 0; |
---|
755 | } |
---|
756 | int * expf= new int [sizef]; |
---|
757 | int * expg= new int [sizeg]; |
---|
758 | int j= 0; |
---|
759 | for (CFIterator i= f; i.hasTerms(); i++, j++) |
---|
760 | { |
---|
761 | expf [j]= i.exp(); |
---|
762 | } |
---|
763 | j= 0; |
---|
764 | for (CFIterator i= g; i.hasTerms(); i++, j++) |
---|
765 | { |
---|
766 | expg [j]= i.exp(); |
---|
767 | } |
---|
768 | |
---|
769 | int indf= sizef - 1; |
---|
770 | int indg= sizeg - 1; |
---|
771 | if (expf[indf] == 0) |
---|
772 | indf--; |
---|
773 | if (expg[indg] == 0) |
---|
774 | indg--; |
---|
775 | |
---|
776 | if ((expg[indg]%expf [indf] != 0 && expf[indf]%expg[indg] != 0) || |
---|
777 | (expg[indg] == 1 && expf[indf] == 1)) |
---|
778 | { |
---|
779 | delete [] expg; |
---|
780 | delete [] expf; |
---|
781 | return 0; |
---|
782 | } |
---|
783 | |
---|
784 | int result; |
---|
785 | if (expg [indg]%expf [indf] == 0) |
---|
786 | result= expf[indf]; |
---|
787 | else |
---|
788 | result= expg[indg]; |
---|
789 | for (int i= indf - 1; i >= 0; i--) |
---|
790 | { |
---|
791 | if (expf [i]%result != 0) |
---|
792 | { |
---|
793 | delete [] expf; |
---|
794 | delete [] expg; |
---|
795 | return 0; |
---|
796 | } |
---|
797 | } |
---|
798 | |
---|
799 | for (int i= indg - 1; i >= 0; i--) |
---|
800 | { |
---|
801 | if (expg [i]%result != 0) |
---|
802 | { |
---|
803 | delete [] expf; |
---|
804 | delete [] expg; |
---|
805 | return 0; |
---|
806 | } |
---|
807 | } |
---|
808 | |
---|
809 | delete [] expg; |
---|
810 | delete [] expf; |
---|
811 | return result; |
---|
812 | } |
---|
813 | |
---|
814 | int recSubstituteCheck (const CanonicalForm& F, const int d) |
---|
815 | { |
---|
816 | if (F.inCoeffDomain()) |
---|
817 | return 0; |
---|
818 | Variable x= Variable (1); |
---|
819 | if (degree (F, x) <= 1) |
---|
820 | return 0; |
---|
821 | CanonicalForm f= swapvar (F, F.mvar(), x); |
---|
822 | int sizef= 0; |
---|
823 | for (CFIterator i= f; i.hasTerms(); i++, sizef++) |
---|
824 | { |
---|
825 | if (i.exp() == 1) |
---|
826 | return 0; |
---|
827 | } |
---|
828 | int * expf= new int [sizef]; |
---|
829 | int j= 0; |
---|
830 | for (CFIterator i= f; i.hasTerms(); i++, j++) |
---|
831 | { |
---|
832 | expf [j]= i.exp(); |
---|
833 | } |
---|
834 | |
---|
835 | int indf= sizef - 1; |
---|
836 | if (expf[indf] == 0) |
---|
837 | indf--; |
---|
838 | |
---|
839 | if ((d%expf [indf] != 0 && expf[indf]%d != 0) || (expf[indf] == 1)) |
---|
840 | { |
---|
841 | delete [] expf; |
---|
842 | return 0; |
---|
843 | } |
---|
844 | |
---|
845 | int result; |
---|
846 | if (d%expf [indf] == 0) |
---|
847 | result= expf[indf]; |
---|
848 | else |
---|
849 | result= d; |
---|
850 | for (int i= indf - 1; i >= 0; i--) |
---|
851 | { |
---|
852 | if (expf [i]%result != 0) |
---|
853 | { |
---|
854 | delete [] expf; |
---|
855 | return 0; |
---|
856 | } |
---|
857 | } |
---|
858 | |
---|
859 | delete [] expf; |
---|
860 | return result; |
---|
861 | } |
---|
862 | |
---|
863 | int substituteCheck (const CFList& L) |
---|
864 | { |
---|
865 | ASSERT (L.length() > 1, "expected a list of at least two elements"); |
---|
866 | if (L.length() < 2) |
---|
867 | return 0; |
---|
868 | CFListIterator i= L; |
---|
869 | i++; |
---|
870 | int result= substituteCheck (L.getFirst(), i.getItem()); |
---|
871 | if (result <= 1) |
---|
872 | return result; |
---|
873 | i++; |
---|
874 | for (;i.hasItem(); i++) |
---|
875 | { |
---|
876 | result= recSubstituteCheck (i.getItem(), result); |
---|
877 | if (result <= 1) |
---|
878 | return result; |
---|
879 | } |
---|
880 | return result; |
---|
881 | } |
---|
882 | |
---|
883 | void |
---|
884 | subst (const CanonicalForm& F, CanonicalForm& A, const int d, const Variable& x) |
---|
885 | { |
---|
886 | if (d <= 1) |
---|
887 | { |
---|
888 | A= F; |
---|
889 | return; |
---|
890 | } |
---|
891 | if (degree (F, x) <= 0) |
---|
892 | { |
---|
893 | A= F; |
---|
894 | return; |
---|
895 | } |
---|
896 | CanonicalForm C= 0; |
---|
897 | CanonicalForm f= swapvar (F, x, F.mvar()); |
---|
898 | for (CFIterator i= f; i.hasTerms(); i++) |
---|
899 | C += i.coeff()*power (f.mvar(), i.exp()/ d); |
---|
900 | A= swapvar (C, x, F.mvar()); |
---|
901 | } |
---|
902 | |
---|
903 | CanonicalForm |
---|
904 | reverseSubst (const CanonicalForm& F, const int d, const Variable& x) |
---|
905 | { |
---|
906 | if (d <= 1) |
---|
907 | return F; |
---|
908 | if (degree (F, x) <= 0) |
---|
909 | return F; |
---|
910 | CanonicalForm f= swapvar (F, x, F.mvar()); |
---|
911 | CanonicalForm result= 0; |
---|
912 | for (CFIterator i= f; i.hasTerms(); i++) |
---|
913 | result += i.coeff()*power (f.mvar(), d*i.exp()); |
---|
914 | return swapvar (result, x, F.mvar()); |
---|
915 | } |
---|
916 | |
---|
917 | void |
---|
918 | reverseSubst (CFList& L, const int d, const Variable& x) |
---|
919 | { |
---|
920 | for (CFListIterator i= L; i.hasItem(); i++) |
---|
921 | i.getItem()= reverseSubst (i.getItem(), d, x); |
---|
922 | } |
---|
923 | |
---|