forked from vermaseren/form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyfact.cc
1690 lines (1387 loc) · 43.1 KB
/
polyfact.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file polyfact.cc
*
* Contains the routines for factorizing multivariate polynomials
*/
/* #[ License : */
/*
* Copyright (C) 1984-2022 J.A.M. Vermaseren
* When using this file you are requested to refer to the publication
* J.A.M.Vermaseren "New features of FORM" math-ph/0010025
* This is considered a matter of courtesy as the development was paid
* for by FOM the Dutch physics granting agency and we would like to
* be able to track its scientific use to convince FOM of its value
* for the community.
*
* This file is part of FORM.
*
* FORM is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* FORM is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with FORM. If not, see <http://www.gnu.org/licenses/>.
*/
/* #] License : */
/*
#[ include :
*/
#include "poly.h"
#include "polygcd.h"
#include "polyfact.h"
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <climits>
//#define DEBUG
#ifdef DEBUG
#include "mytime.h"
#endif
using namespace std;
/*
#] include :
#[ tostring :
*/
// Turns a factorized_poly into a readable string
const string factorized_poly::tostring () const {
// empty
if (factor.size()==0)
return "no_factors";
string res;
// polynomial
for (int i=0; i<(int)factor.size(); i++) {
if (i>0) res += "*";
res += "(";
res += poly(factor[i],0,1).to_string();
res += ")";
if (power[i]>1) {
res += "^";
char tmp[100];
snprintf (tmp,100,"%i",power[i]);
res += tmp;
}
}
// modulo p^n
if (factor[0].modp>0) {
res += " (mod ";
char tmp[12];
snprintf (tmp,12,"%i",factor[0].modp);
res += tmp;
if (factor[0].modn > 1) {
snprintf (tmp,12,"%i",factor[0].modn);
res += "^";
res += tmp;
}
res += ")";
}
return res;
}
/*
#] tostring :
#[ ostream operator :
*/
// ostream operator for outputting a factorized_poly
ostream& operator<< (ostream &out, const factorized_poly &a) {
return out << a.tostring();
}
// ostream operator for outputting a vector<T>
template<class T> ostream& operator<< (ostream &out, const vector<T> &v) {
out<<"{";
for (int i=0; i<(int)v.size(); i++) {
if (i>0) out<<",";
out<<v[i];
}
out<<"}";
return out;
}
/*
#] ostream operator :
#[ add_factor :
*/
// adds a factor f^p to a factorization
void factorized_poly::add_factor(const poly &f, int p) {
factor.push_back(f);
power.push_back(p);
}
/*
#] add_factor :
#[ extended_gcd_Euclidean_lifted :
*/
/** Lifted Extended Euclidean Algorithm
*
* Description
* ===========
* Returns s(x) and t(x) such that
*
* s(x)*a(x) + t(x)*b(x) = 1 (mod p^n),
*
* with a(x) and b(x) univariate polynomials.
*
* Notes
* =====
* - The lifting part works only when a and b are relative prime;
* otherwise the method might crash/hang.
*
* [for details, see "Algorithms for Computer Algebra", pp. 32-37, 205-225]
*/
const vector<poly> polyfact::extended_gcd_Euclidean_lifted (const poly &a, const poly &b) {
#ifdef DEBUGALL
cout << "*** [" << thetime() << "] CALL: extended_Euclidean_lifted("<<a<<","<<b<<")"<<endl;
#endif
POLY_GETIDENTITY(a);
// Calculate s,t,gcd (mod p) with the Extended Euclidean Algorithm.
poly s(a,a.modp,1);
poly t(b,b.modp,1);
poly sa(BHEAD 1,a.modp,1);
poly sb(BHEAD 0,b.modp,1);
poly ta(BHEAD 0,a.modp,1);
poly tb(BHEAD 1,b.modp,1);
while (!t.is_zero()) {
poly x(s/t);
swap(s -=x*t , t);
swap(sa-=x*ta, ta);
swap(sb-=x*tb, tb);
}
// Normalize the result.
sa /= s.integer_lcoeff();
sb /= s.integer_lcoeff();
// Lift the result to modolu p^n with p-adic Newton's iteration.
poly samodp(sa);
poly sbmodp(sb);
poly term(BHEAD 1);
sa.setmod(0,1);
sb.setmod(0,1);
poly amodp(a,a.modp,1);
poly bmodp(b,a.modp,1);
poly error(poly(BHEAD 1) - sa*a - sb*b);
poly p(BHEAD a.modp);
for (int n=1; n<a.modn && !error.is_zero(); n++) {
error /= p;
term *= p;
poly errormodp(error,a.modp,1);
poly dsa((samodp * errormodp) % bmodp);
// equivalent, but faster than the symmetric
// poly dsb((sbmodp * errormodp) % amodp);
poly dsb((errormodp - dsa*amodp) / bmodp);
sa += term * dsa;
sb += term * dsb;
error -= a*dsa + b*dsb;
}
sa.setmod(a.modp,a.modn);
sb.setmod(a.modp,a.modn);
// Output the result
vector<poly> res;
res.push_back(sa);
res.push_back(sb);
#ifdef DEBUGALL
cout << "*** [" << thetime() << "] RES : extended_Euclidean_lifted("<<a<<","<<b<<") = "<<res<<endl;
#endif
return res;
}
/*
#] extended_gcd_Euclidean_lifted :
#[ solve_Diophantine_univariate :
*/
/** Univariate Diophantine equation solver modulo a prime power
*
* Description
* ===========
* Method for solving the Diophantine equation
*
* s1*A1 + ... + sk*Ak = b (mod p^n)
*
* The input a1,...,ak and b consists of univariate polynomials
* and Ai = product(aj|j!=i). The solution si consists therefore of
* univariate polynomials as well.
*
* When deg(c) < sum(deg(ai)), the result is the unique result with
* deg(si) < deg(ai) for all i. This is necessary for the Hensel
* construction.
*
* The equation is solved by iteratively solving the following
* two-term equations with the Extended Euclidean Algorithm:
*
* B0(x) = 1
* Bj(x) * aj(x) + sj(x) * product(ai(x) | i=j+1,...,r) = B{j-1}(x)
* sk(x) = B{k-1}(x)
*
* Substitution proves that this solution is indeed correct.
*
* Notes
* =====
* - The ai must be pairwise relatively prime modulo p.
*
* [for details, see "Algorithms for Computer Algebra", pp. 266-273]
*/
const vector<poly> polyfact::solve_Diophantine_univariate (const vector<poly> &a, const poly &b) {
#ifdef DEBUGALL
cout << "*** [" << thetime() << "] CALL: solve_Diophantine_univariate(" <<a<<","<<b<<")"<<endl;
#endif
POLY_GETIDENTITY(b);
vector<poly> s(1,b);
for (int i=0; i+1<(int)a.size(); i++) {
poly A(BHEAD 1,b.modp,b.modn);
for (int j=i+1; j<(int)a.size(); j++) A *= a[j];
vector<poly> t(extended_gcd_Euclidean_lifted(a[i],A));
poly prev(s.back());
s.back() = t[1] * prev % a[i];
s.push_back(t[0] * prev % A);
}
#ifdef DEBUGALL
cout << "*** [" << thetime() << "] RES : solve_Diophantine_univariate(" <<a<<","<<b<<") = "<<s<<endl;
#endif
return s;
}
/*
#] solve_Diophantine_univariate :
#[ solve_Diophantine_multivariate :
*/
/** Multivariate Diophantine equation solver modulo a prime power
*
* Description
* ===========
* Method for solving the Diophantine equation
*
* s1*A1 + ... + sk*Ak = b
*
* modulo <p^n,I^d>, with the ideal I=<x2-c1,...,xm-c{m-1}>. The
* input a1,...,ak and b consists of multivariate polynomials and
* Ai = product(aj|j!=i). The solution si consists therefore of
* multivariate polynomials as well.
*
* When deg(c,x1) < sum(deg(ai,x1)), the result is the unique result
* with deg(si,x1) < deg(ai,x1) for all i. This is necessary for the
* Hensel construction.
*
* The equation is solved in the following way:
* - reduce with the homomorphism <xm-c{m-1}>
* - solve the equation in one less variable
* - use ideal-adic Newton's iteration to add the xm-terms.
*
* Notes
* =====
* - The ai must be pairwise relatively prime modulo <I,p>.
* - The method returns an empty vector<poly>() iff the
* Diophantine equation has no solution (typically happens in
* gcd calculations with unlucky reductions).
*
* [for details, see "Algorithms for Computer Algebra", pp. 264-273]
*/
const vector<poly> polyfact::solve_Diophantine_multivariate (const vector<poly> &a, const poly &b, const vector<int> &x, const vector<int> &c, int d) {
#ifdef DEBUGALL
cout << "*** [" << thetime() << "] CALL: solve_Diophantine_multivariate(" <<a<<","<<b<<","<<x<<","<<c<<","<<d<<")"<<endl;
#endif
POLY_GETIDENTITY(b);
if (b.is_zero()) return vector<poly>(a.size(),poly(BHEAD 0));
if (x.size() == 1) return solve_Diophantine_univariate(a,b);
// Reduce the polynomial with the homomorphism <xm-c{m-1}>
poly simple(poly::simple_poly(BHEAD x.back(),c.back()));
vector<poly> ared (a);
for (int i=0; i<(int)ared.size(); i++)
ared[i] %= simple;
poly bred(b % simple);
vector<int> xred(x.begin(),x.end()-1);
vector<int> cred(c.begin(),c.end()-1);
// Solve the equation in one less variable
vector<poly> s(solve_Diophantine_multivariate(ared,bred,xred,cred,d));
if (s == vector<poly>()) return vector<poly>();
// Cache the Ai = product(aj | j!=i).
vector<poly> A(a.size(), poly(BHEAD 1,b.modp,b.modn));
for (int i=0; i<(int)a.size(); i++)
for (int j=0; j<(int)a.size(); j++)
if (i!=j) A[i] *= a[j];
// Add the powers (xm-c{m-1})^k with ideal-adic Newton iteration.
poly term(BHEAD 1,b.modp,b.modn);
poly error(b);
for (int i=0; i<(int)A.size(); i++)
error -= s[i] * A[i];
for (int deg=1; deg<=d; deg++) {
if (error.is_zero()) break;
error /= simple;
term *= simple;
vector<poly> ds(solve_Diophantine_multivariate(ared, error%simple, xred, cred, d));
if (ds == vector<poly>()) return vector<poly>();
for (int i=0; i<(int)s.size(); i++) {
s[i] += ds[i] * term;
error -= ds[i] * A[i];
}
}
if (!error.is_zero()) return vector<poly>();
#ifdef DEBUGALL
cout << "*** [" << thetime() << "] RES : solve_Diophantine_multivariate(" <<a<<","<<b<<","<<x<<","<<c<<","<<d<<") = "<<s<<endl;
#endif
return s;
}
/*
#] solve_Diophantine_multivariate :
#[ lift_coefficients :
*/
/** Univariate Hensel lifting of coefficients
*
* Description
* ===========
* Given a primitive univariate polynomial A and a list of
* univariate polynomials a1(x),...,ak(x), such that
*
* - N(A(x)) = N(a1(x))*...*N(ak(x)) mod p
* - gcd(ai,aj) = 1 (for i!=j),
*
* where N(A(x)) means make A monic modulo p, i.e., divide by its
* leading coefficient, the method returns a list of univariate
* polynomials A1(x),...,Ak(x), such that
*
* A(x) = A1(x)*...*Ak(x) mod p^n
*
* with
*
* N(Ai(x)) = N(ai(x)) mod p.
*
* If there exists a factorization of A over the integers, it is the
* one returned by the method if p^n is large enough.
*
* Notes
* =====
* - The polynomial A must be primitive.
* - If there exists no factorization over the integers, the
* coefficients of the factors modulo p^n typically become large,
* like 1/2 mod p^n.
*
* [for details, see "Algorithms for Computer Algebra", pp. 232-250]
*/
const vector<poly> polyfact::lift_coefficients (const poly &_A, const vector<poly> &_a) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL: lift_coefficients("<<_A<<","<<_a<<")"<<endl;
#endif
POLY_GETIDENTITY(_A);
poly A(_A);
vector<poly> a(_a);
poly term(BHEAD 1);
int x = A.first_variable();
// Replace the leading term of all factors with lterm(A) mod p
poly lead(A.integer_lcoeff());
for (int i=0; i<(int)a.size(); i++) {
a[i] *= lead / a[i].integer_lcoeff();
if (i>0) A*=lead;
}
// Solve Diophantine equation
vector<poly> s(solve_Diophantine_univariate(a,poly(BHEAD 1,A.modp,1)));
// Replace the leading term of all factors with lterm(A) mod p^n
for (int i=0; i<(int)a.size(); i++) {
a[i].setmod(A.modp,A.modn);
a[i] += (lead - a[i].integer_lcoeff()) * poly::simple_poly(BHEAD x,0,a[i].degree(x));
}
// Calculate the error, express it in terms of ai and add corrections.
for (int k=2; k<=A.modn; k++) {
term *= poly(BHEAD A.modp);
poly error(BHEAD -1);
for (int i=0; i<(int)a.size(); i++) error *= a[i];
error += A;
if (error.is_zero()) break;
error /= term;
error.setmod(A.modp,1);
for (int i=0; i<(int)a.size(); i++)
a[i] += term * (error * s[i] % a[i]);
}
// Fix leading coefficients by dividing out integer contents.
for (int i=0; i<(int)a.size(); i++)
a[i] /= polygcd::integer_content(poly(a[i],0,1));
#ifdef DEBUG
cout << "*** [" << thetime() << "] RES : lift_coefficients("<<_A<<","<<_a<<") = "<<a<<endl;
#endif
return a;
}
/*
#] lift_coefficients :
#[ predetermine :
*/
/** Predetermine coefficients
*
* Description
* ===========
* Helper function for multivariate Hensel lifting to predetermine
* coefficients. The function creates all products of terms of the
* polynomials a1,...,an and stores them according to degree in x1.
*
* All terms with equal power in x1 result in an equation that might
* be solved to predetermine a coefficient.
*
* For details, see Wang, "An Improved Polynomial Factoring
* Algorithm", Math. Comput. 32 (1978) pp. 1215-1231]
*/
void polyfact::predetermine (int dep, const vector<vector<int> > &state, vector<vector<vector<int> > > &terms, vector<int> &term, int sumdeg) {
// store the term
if (dep == (int)state.size()) {
terms[sumdeg].push_back(term);
return;
}
// recursively create new terms
term.push_back(0);
for (int deg=0; sumdeg+deg<(int)state[dep].size(); deg++)
if (state[dep][deg] > 0) {
term.back() = deg;
predetermine(dep+1, state, terms, term, sumdeg+deg);
}
term.pop_back();
}
/*
#] predetermine :
#[ lift_variables :
*/
/** Multivariate Hensel lifting of variables
*
* Description
* ===========
* Given a multivariate polynomial A modulo a prime power p^n and
* a list of univariate polynomials a1(x1),...,am(x1), such that
*
* - A(x1,...,xm) = a1(x1)*...*ak(x1) mod <p^n,I>,
* - gcd(ai,aj) = 1 (for i!=j),
*
* with the ideal I=<x2-c1,...,xm-c{m-1}>, the method returns a
* list of multivariate polynomials A1(x1,...xm),...,Ak(x1,...,xm),
* such that
*
* A(x1,...,xm) = A1(x1,...,xm)*...*Ak(x1,...,xm) mod p^n
*
* with
*
* Ai(x1,...,xm) = ai(x1) mod <p^n,I>.
*
* The correct multivariate leading coefficients should be given in
* the parameter lc.
*
* [for details, see "Algorithms for Computer Algebra", pp. 250-273]
*
* Before Hensel lifting, predetermination of coefficients is used
* for efficiency.
* [for details, see Wang, "An Improved Polynomial Factoring
* Algorithm", Math. Comput. 32 (1978) pp. 1215-1231]
*
* Notes
* =====
* - The polynomial A must be primitive.
* - Returns empty vector<poly>() if lifting is impossible.
*/
const vector<poly> polyfact::lift_variables (const poly &A, const vector<poly> &_a, const vector<int> &x, const vector<int> &c, const vector<poly> &lc) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL: lift_variables("<<A<<","<<_a<<","<<x<<","<<c<<","<<lc<<")\n";
#endif
// If univariate, don't lift
if (x.size()<=1) return _a;
POLY_GETIDENTITY(A);
vector<poly> a(_a);
// First method: predetermine coefficients
// check feasibility, otherwise it tries too many possibilities
int cnt = POLYFACT_MAX_PREDETERMINATION;
for (int i=0; i<(int)a.size(); i++) {
if (a[i].number_of_terms() == 0) return vector<poly>();
cnt /= a[i].number_of_terms();
}
if (cnt>0) {
// state[n][d]: coefficient of x^d in a[n] is
// 0: non-existent, 1: undetermined, 2: determined
int D = A.degree(x[0]);
vector<vector<int> > state(a.size(), vector<int>(D+1, 0));
for (int i=0; i<(int)a.size(); i++)
for (int j=1; j<a[i][0]; j+=a[i][j])
state[i][a[i][j+1+x[0]]] = j==1 ? 2 : 1;
// collect all products of terms
vector<vector<vector<int> > > terms(D+1);
vector<int> term;
predetermine(0,state,terms,term);
// count the number of undetermined coefficients
vector<int> num_undet(terms.size(),0);
for (int i=0; i<(int)terms.size(); i++)
for (int j=0; j<(int)terms[i].size(); j++)
for (int k=0; k<(int)terms[i][j].size(); k++)
if (state[k][terms[i][j][k]] == 1) num_undet[i]++;
// replace the current leading coefficients by the correct ones
for (int i=0; i<(int)a.size(); i++)
a[i] += (lc[i] - a[i].lcoeff_univar(x[0])) * poly::simple_poly(BHEAD x[0],0,a[i].degree(x[0]));
bool changed;
do {
changed = false;
for (int i=0; i<(int)terms.size(); i++) {
// is only one coefficient in a equation is undetermined, solve
// the equation to determine this coefficient
if (num_undet[i] == 1) {
// generate equation
poly lhs(BHEAD 0), rhs(A.coefficient(x[0],i), A.modp, A.modn);
int which_idx=-1, which_deg=-1;
for (int j=0; j<(int)terms[i].size(); j++) {
poly coeff(BHEAD 1, A.modp, A.modn);
bool undet=false;
for (int k=0; k<(int)terms[i][j].size(); k++) {
if (state[k][terms[i][j][k]] == 1) {
undet = true;
which_idx=k;
which_deg=terms[i][j][k];
}
else
coeff *= a[k].coefficient(x[0], terms[i][j][k]);
}
if (undet)
lhs = coeff;
else
rhs -= coeff;
}
// solve equation
if (A.modn > 1) rhs.setmod(0,1);
if (lhs.is_zero() || !(rhs%lhs).is_zero()) return vector<poly>();
a[which_idx] += (rhs / lhs - a[which_idx].coefficient(x[0],which_deg)) * poly::simple_poly(BHEAD x[0],0,which_deg);
state[which_idx][which_deg] = 2;
// update number of undetermined coefficients
for (int j=0; j<(int)terms.size(); j++)
for (int k=0; k<(int)terms[j].size(); k++)
if (terms[j][k][which_idx] == which_deg)
num_undet[j]--;
changed = true;
}
}
}
while (changed);
// if this is the complete result, skip lifting
poly check(BHEAD 1, A.modn>1?0:A.modp, 1);
for (int i=0; i<(int)a.size(); i++)
check *= a[i];
if (check == A) return a;
}
// Second method: Hensel lifting
// Calculate A and lc's modulo Ii = <xi-c{i-1],...,xm-c{m-1}> (for i=2,...,m)
vector<poly> simple(x.size(), poly(BHEAD 0));
for (int i=(int)x.size()-2; i>=0; i--)
simple[i] = poly::simple_poly(BHEAD x[i+1],c[i],1);
// Calculate the maximum degree of A in x2,...,xm
int maxdegA=0;
for (int i=1; i<(int)x.size(); i++)
maxdegA = MaX(maxdegA, A.degree(x[i]));
// Iteratively add the variables x2,...,xm
for (int xi=1; xi<(int)x.size(); xi++) {
// replace the current leading coefficients by the correct ones
for (int i=0; i<(int)a.size(); i++)
a[i] += (lc[i] - a[i].lcoeff_univar(x[0])) * poly::simple_poly(BHEAD x[0],0,a[i].degree(x[0]));
vector<poly> anew(a);
for (int i=0; i<(int)anew.size(); i++)
for (int j=xi-1; j<(int)c.size(); j++)
anew[i] %= simple[j];
vector<int> xnew(x.begin(), x.begin()+xi);
vector<int> cnew(c.begin(), c.begin()+xi-1);
poly term(BHEAD 1,A.modp,A.modn);
// Iteratively add the powers xi^k
for (int deg=1, maxdeg=A.degree(x[xi]); deg<=maxdeg; deg++) {
term *= simple[xi-1];
// Calculate the error, express it in terms of ai and add corrections.
poly error(BHEAD -1,A.modp,A.modn);
for (int i=0; i<(int)a.size(); i++) error *= a[i];
error += A;
for (int i=xi; i<(int)c.size(); i++) error %= simple[i];
if (error.is_zero()) break;
error /= term;
error %= simple[xi-1];
vector<poly> s(solve_Diophantine_multivariate(anew,error,xnew,cnew,maxdegA));
if (s == vector<poly>()) return vector<poly>();
for (int i=0; i<(int)a.size(); i++)
a[i] += s[i] * term;
}
// check whether PRODUCT(a[i]) = A mod <xi-c{i-1},...,xm-c{m-1]> over the integers or ZZ/p
poly check(BHEAD -1, A.modn>1?0:A.modp, 1);
for (int i=0; i<(int)a.size(); i++) check *= a[i];
check += A;
for (int i=xi; i<(int)c.size(); i++) check %= simple[i];
if (!check.is_zero()) return vector<poly>();
}
#ifdef DEBUG
cout << "*** [" << thetime() << "] RES : lift_variables("<<A<<","<<_a<<","<<x<<","<<c<<","<<lc<<") = " << a << endl;
#endif
return a;
}
/*
#] lift_variables :
#[ choose_prime :
*/
/** Choose a good prime number
*
* Description
* ===========
* Choose a prime number, such that lcoeff(a) != 0 (mod p) (which
* is equivalent to icont(lcoeff(a)) != 0 (mod p))
*
* Notes
* =====
* If a prime p is provided, it returns the next prime that is good.
*/
WORD polyfact::choose_prime (const poly &a, const vector<int> &x, WORD p) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL: choose_prime("<<a<<","<<x<<","<<p<<")"<<endl;
#endif
POLY_GETIDENTITY(a);
poly icont_lcoeff(polygcd::integer_content(a.lcoeff_univar(x[0])));
if (p==0) p = POLYFACT_FIRST_PRIME;
poly icont_lcoeff_modp(BHEAD 0);
do {
bool is_prime;
do {
p += 2;
is_prime = true;
for (int d=2; d*d<=p; d++)
if (p%d==0) { is_prime=false; break; }
}
while (!is_prime);
icont_lcoeff_modp = icont_lcoeff;
icont_lcoeff_modp.setmod(p,1);
}
while (icont_lcoeff_modp.is_zero());
#ifdef DEBUG
cout << "*** [" << thetime() << "] RES : choose_prime("<<a<<","<<x<<",?) = "<<p<<endl;
#endif
return p;
}
/*
#] choose_prime :
#[ choose_prime_power :
*/
/** Choose a good prime power
*
* Description
* ===========
* Choose a power n such that p^n is larger than the coefficients of
* any factorization of a. These coefficients are bounded by:
*
* goldenratio^degree * |f|
*
* with the norm |f| = (SUM coeff^2)^1/2
*
* [for details, see "Bounding the Coefficients of a Divisor of a
* Given Polynomial" by Andrew Granville]
*/
WORD polyfact::choose_prime_power (const poly &a, WORD p) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL: choose_prime_power("<<a<<","<<p<<")"<<endl;
#endif
POLY_GETIDENTITY(a);
// analyse the polynomial for calculating the bound
double maxdegree=0, maxlogcoeff=0, numterms=0;
for (int i=1; i<a[0]; i+=a[i]) {
for (int j=0; j<AN.poly_num_vars; j++)
maxdegree = MaX(maxdegree, a[i+1+j]);
maxlogcoeff = MaX(maxlogcoeff,
log(1.0+(UWORD)a[i+a[i]-2]) + // most significant digit + 1
BITSINWORD*log(2.0)*(ABS(a[i+a[i]-1])-1)); // number of digits
numterms++;
}
WORD res = (WORD)ceil((log((sqrt(5.0)+1)/2)*maxdegree + maxlogcoeff + 0.5*log(numterms)) / log((double)p));
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL: choose_prime_power("<<a<<","<<p<<") = "<<res<<endl;
#endif
return res;
}
/*
#] choose_prime_power :
#[ choose_ideal :
*/
/** Choose a good ideal
*
* Description
* ===========
* Choose an ideal I=<x2-c1,...,xm-c{m-1}) such that the following
* properties hold:
*
* - The leading coefficient of a, regarded as polynomial in x1,
* does not vanish mod I;
* - a mod I is squarefree;
* - Each factor of lcoeff(a) mod I (i.e., parameter lc) has a
* unique prime number factor that is not contained in one of the
* other factors. (This can be used to "identification" later.)
*
* This last condition is not fulfilled when calculating over ZZ/p.
*
* Notes
* =====
* - If a step fails, an empty vector<int> is returned. This is
* necessary, e.g., in the case of a non-squarefree input
* polynomial
*
* [for details, see:
* - "Algorithms for Computer Algebra", pp. 337-343,
* - Wang, "An Improved Polynomial Factoring Algorithm",
* Math. Comput. 32 (1978) pp. 1215-1231]
*/
const vector<int> polyfact::choose_ideal (const poly &a, int p, const factorized_poly &lc, const vector<int> &x) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL: polyfact::choose_ideal("
<<a<<","<<p<<","<<lc<<","<<x<<")"<<endl;
#endif
if (x.size()==1) return vector<int>();
POLY_GETIDENTITY(a);
vector<int> c(x.size()-1);
int dega = a.degree(x[0]);
poly amodI(a);
// choose random c
for (int i=0; i<(int)c.size(); i++) {
c[i] = 1 + wranf(BHEAD0) % ((p-1) / POLYFACT_IDEAL_FRACTION);
amodI %= poly::simple_poly(BHEAD x[i+1],c[i],1);
}
poly amodIp(amodI);
amodIp.setmod(p,1);
// check if leading coefficient is non-zero [equivalent to degree=old_degree]
if (amodIp.degree(x[0]) != dega)
return c = vector<int>();
// check if leading coefficient is squarefree [equivalent to gcd(a,a')==const]
if (!polygcd::gcd_Euclidean(amodIp, amodIp.derivative(x[0])).is_integer())
return c = vector<int>();
if (a.modp>0 && a.modn==1) return c;
// check for unique prime factors in each factor lc[i] of the leading coefficient
vector<poly> d(1, polygcd::integer_content(amodI));
for (int i=0; i<(int)lc.factor.size(); i++) {
// constant factor
if (i==0 && lc.factor[i].is_integer()) {
d[0] *= lc.factor[i];
continue;
}
// factor modulo I
poly q(lc.factor[i]);
for (int j=0; j<(int)c.size(); j++)
q %= poly::simple_poly(BHEAD x[j+1],c[j]);
if (q.sign() == -1) q *= poly(BHEAD -1);
// divide out common factors
for (int j=(int)d.size()-1; j>=0; j--) {
poly r(d[j]);
while (!r.is_one()) {
r = polygcd::integer_gcd(r,q);
q /= r;
}
}
// check whether there is some factor left
if (q.is_one()) return vector<int>();
d.push_back(q);
}
#ifdef DEBUG
cout << "*** [" << thetime() << "] RES : polyfact::choose_ideal("
<<a<<","<<p<<","<<lc<<","<<x<<") = "<<c<<endl;
#endif
return c;
}
/*
#] choose_ideal :
#[ squarefree_factors_Yun :
*/
/** Yun's squarefree factorization of a primitive polynomial
*
* Description
* ===========
* See description "squarefree_factors".
*/
const factorized_poly polyfact::squarefree_factors_Yun (const poly &_a) {
factorized_poly res;
poly a(_a);
int pow = 1;
int x = a.first_variable();
poly b(a.derivative(x));
poly c(polygcd::gcd(a,b));
while (true) {
a /= c;
b /= c;
b -= a.derivative(x);
if (b.is_zero()) break;
c = polygcd::gcd(a,b);
if (!c.is_one()) res.add_factor(c,pow);
pow++;
}
if (!a.is_one()) res.add_factor(a,pow);
return res;
}
/*
#] squarefree_factors_Yun :
#[ squarefree_factors_modp :
*/
/** Squarefree factorization of a primitive polynomial modulo a prime
*
* Description
* ===========
* See description "squarefree_factors".
*/
const factorized_poly polyfact::squarefree_factors_modp (const poly &_a) {
factorized_poly res;
poly a(_a);
int pow = 1;
int x = a.first_variable();
poly b(a.derivative(x));
// poly contains terms of the form c(x)^n (n!=c*p)
if (!b.is_zero()) {
poly c(polygcd::gcd(a,b));
a /= c;
while (!a.is_one()) {
b = polygcd::gcd(a,c);
a /= b;
if (!a.is_one()) res.add_factor(a,pow);
pow++;
a = b;
c /= a;