-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomial.cpp
More file actions
2463 lines (2340 loc) · 90.2 KB
/
Polynomial.cpp
File metadata and controls
2463 lines (2340 loc) · 90.2 KB
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
#include "Polynomial.h"
#include "VeryLong.h"
#include "LongModular.h"
#include "VeryLongModular.h"
#include "Polynomial.inl"
#include "discriminant.h"
#include "Combinations.h"
#include "lll.h"
#include "timings.h"
#include <algorithm>
#include <deque>
#include <set>
#include <ctype.h>
#include "MPFloat.h"
//#define NEW_METHOD 1
//#define OLD_METHOD 1
#define THREADED_OLD_METHOD 1
#ifdef THREADED_OLD_METHOD
#include <thread>
#include <memory>
#include <chrono>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <sstream>
#include <sys/sysinfo.h>
#endif
#define DO_CHECKS 1
#if 1
template <> long int get_long(const VeryLong& i)
{
return i.get_long();
}
template <> long int get_long(const long int& i)
{
return i;
}
template <> long int get_long(const long long int& i)
{
return static_cast<long int>(i);
}
template <> long int get_integer(const LongModular& i)
{
return i.get_long();
}
template <> long long int get_integer(const LongModular& i)
{
return i.get_long();
}
template <> VeryLong get_integer(const LongModular& i)
{
return i.get_long();
}
template <> VeryLong get_integer(const VeryLongModular& i)
{
return i.get_very_long();
}
template <> VeryLong get_integer2(const VeryLong& i)
{
return i;
}
template <> long int get_integer2(const VeryLong& i)
{
return i.get_long();
}
template <> long long int get_integer2(const VeryLong& i)
{
return i.get_long();
}
#endif
Polynomial<VeryLong> read_skewed_polynomial(std::istream& is, VeryLong& m, long int& s)
{
// reads a skewed polynomial from the input stream
// assumes format
// 7111977108918472837611244937303928 - 2523764323493368834122550403406 X - 100831972740733548080518208
// X^2 + 7467483273731340429431 X^3 + 150036498813232980 X^4 + 2859632424000 X^5
// m = 20651964074656838659712611715
// s = 18428
std::vector<VeryLong> coeff;
coeff.resize(6);
int done = 0;
std::string poly_str;
std::string str;
while (!done)
{
int poly_done = 0;
while (!poly_done)
{
getline(is, str);
if (str.c_str()[0] == 'm') poly_done = 1;
else
{
poly_str = str;
}
}
m = str.substr(4);
getline(is, str);
//s = atol(&buf[4]);
s = std::atol(str.substr(4).c_str());
done = 1;
}
char tmp[1024];
const char* c = poly_str.c_str();
char* d = tmp;
int coeff_index = 0;
while (*c)
{
while (*c != ' ')
{
*d = *c;
c++;
d++;
}
*d = '\0';
coeff[coeff_index] = tmp;
coeff_index++;
c++; // skip space
if (*c == 'X')
{
while (*c && *c != ' ') c++; // skip to space before sign or to end of string
if (*c) c++;
}
if (*c)
{
d = tmp;
if (*c == '-')
{
*d = *c;
d++;
}
c++; // skip space
c++;
}
}
return Polynomial<VeryLong>(coeff);
}
template <> Polynomial<VeryLong> Polynomial<VeryLong>::read_polynomial(std::istream& is)
{
// reads a polynomial from the input stream
// assumes format
// 7111977108918472837611244937303928 - 2523764323493368834122550403406 X - 100831972740733548080518208
// X^2 + 7467483273731340429431 X^3 + 150036498813232980 X^4 + 2859632424000 X^5
std::string poly_str;
getline(is, poly_str);
if (poly_str.find("DEGREE = ") == 0)
{
std::string::size_type eqpos = poly_str.find('=');
if (std::string::npos != eqpos)
{
if (eqpos + 2 < poly_str.size())
{
std::string s = poly_str.substr(eqpos + 2);
int degree = std::atoi(s.c_str());
std::vector<VeryLong> coeff;
for (int i = 0; i <= degree; ++i)
{
getline(is, s);
VeryLong c(s.c_str());
coeff.push_back(c);
}
return Polynomial<VeryLong>(coeff);
}
}
}
return Polynomial<VeryLong>::read_polynomial(poly_str.c_str());
}
// Algorithm 3.3.1 (Sub-Resultant GCD)
Polynomial<VeryLong> sub_resultant_GCD(const Polynomial<VeryLong>& AA, const Polynomial<VeryLong>& BB)
{
bool debug = false;
if (std::getenv("FACTOR_VERBOSE_OUTPUT") && (::atoi(std::getenv("FACTOR_VERBOSE_OUTPUT")) & 128)) debug = true;
if (debug) std::cout << "+++++ sub_resultant_GCD:" << std::endl;
if (debug) std::cout << "+++++ AA = " << AA << ", BB = " << BB << std::endl;
Polynomial<VeryLong> A = AA;
Polynomial<VeryLong> B = BB;
// Step 1. [Initializations and reductions]
if (B.deg() > A.deg())
{
A = BB;
B = AA;
}
const VeryLong zero(0L);
if (B == Polynomial<VeryLong>(zero)) return A;
VeryLong a = A.content();
VeryLong b = B.content();
VeryLong d = gcd<VeryLong>(a, b);
A = A / a;
B = B / b;
VeryLong g(1L);
VeryLong h(1L);
while (1)
{
// Step 2. [Pseudo division]
if (debug) std::cout << "+++++ Step 2. " << std::endl;
int delta = A.deg() - B.deg();
Polynomial<VeryLong> Q;
Polynomial<VeryLong> R;
pseudo_divide(A, B, Q, R);
if (debug) std::cout << "+++++ A = " << A << ", B = " << B << ", Q = " << Q << ", R = " << R << std::endl;
#ifdef DO_CHECKS
// Check
// delta+1
// We should have l(B) A = BQ + R
//
auto check1 = pow<VeryLong, int>(B.coefficient(B.deg()), delta + 1) * A;
auto check2 = B * Q + R;
if (check1 != check2)
{
std::cout << "Problem! : check1 = " << check1 << ", check2 = " << check2 << std::endl;
}
if (debug) std::cout << "+++++ check1 - check2 = " << check1 - check2 << std::endl;
#endif
if (R == Polynomial<VeryLong>(zero))
{
// Step 4. [Terminate]
if (debug) std::cout << "+++++ Step 4. R == 0 " << std::endl;
Polynomial<VeryLong> res = B / B.content();
res = d * res;
return res;
}
if (R.deg() == 0)
{
// Step 4. [Terminate]
if (debug) std::cout << "+++++ Step 4. deg(R) == 0 " << std::endl;
Polynomial<VeryLong> res(d);
return res;
}
// Step 3. [Reduce remainder]
if (debug) std::cout << "+++++ Step 3. " << std::endl;
A = B;
VeryLong e = g * pow<VeryLong, int>(h, delta);
B = R / e;
g = A.coefficient(A.deg());
if (delta < 1L)
{
h = pow<VeryLong, int>(g, delta) * pow<VeryLong, int>(h, 1 - delta);
}
else
{
h = pow<VeryLong, int>(g, delta) / pow<VeryLong, int>(h, delta - 1);
}
if (debug) std::cout << "+++++ A = " << A << ", B = " << B << ", g = " << g << ", h = " << h << ", delta = " << delta << std::endl;
}
}
//Algorithm 3.5.5 (Hensel Lift)
void hensel_lift(const VeryLong& p,
const VeryLong& q,
const Polynomial<VeryLong>& A,
const Polynomial<VeryLong>& B,
const Polynomial<VeryLong>& C,
const Polynomial<VeryLong>& U,
const Polynomial<VeryLong>& V,
Polynomial<VeryLong>& A1,
Polynomial<VeryLong>& B1)
{
bool debug = false;
if (std::getenv("FACTOR_VERBOSE_OUTPUT") && (::atoi(std::getenv("FACTOR_VERBOSE_OUTPUT")) & 4)) debug = true;
// AB = C mod q
// AU + BV = 1 mod q
if (debug) std::cout << "***** hensel_lift:" << std::endl;
if (debug) std::cout << "***** p = " << p << ", q = " << q << std::endl;
if (debug) std::cout << "***** A = " << A << ", B = " << B << ", C = " << C << std::endl;
if (debug) std::cout << "***** U = " << U << ", V = " << V << std::endl;
#ifdef DO_CHECKS
Polynomial<VeryLong> check3 = C - A * B;
Polynomial<VeryLongModular> check4 = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(check3, q);
if (check4 != Polynomial<VeryLongModular>(VeryLongModular(0L)))
{
std::cout << "Problem: C - A * B != 0 mod " << q << std::endl;
std::cout << "check4 = " << check4 << std::endl;
}
#endif
if (debug) std::cout << "***** C - AB = " << C - A * B << std::endl;
VeryLong r = gcd<VeryLong>(p, q);
// Step 1. [Euclidean division]
Polynomial<VeryLong> f1 = (C - A * B) / q;
if (debug) std::cout << "***** f1 = " << f1 << std::endl;
VeryLongModular::set_default_modulus(r);
Polynomial<VeryLongModular> f = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(f1, r);
Polynomial<VeryLongModular> V_ = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(V, r);
Polynomial<VeryLongModular> A_ = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(A, r);
if (debug) std::cout << "***** f = " << f << ", V_ = " << V_ << ", A_ = " << A_ << std::endl;
Polynomial<VeryLongModular> t;
Polynomial<VeryLongModular> R;
euclidean_division<VeryLongModular>(V_ * f, A_, t, R);
if (debug) std::cout << "***** t = " << t << std::endl;
if (debug) std::cout << "***** R = " << R << std::endl;
// check
if (R.deg() >= A.deg())
{
std::cout << "Problem: euclidean_division of " << V_ * f << " and " << A_ << std::endl;
std::cout << "gave t = " << t << ", R = " << R << std::endl;
}
#ifdef DO_CHECKS
Polynomial<VeryLongModular> check = V_ * f - A_ * t;
if (check != R)
{
std::cout << "Problem: check != R, check = " << check << ", R = " << R << std::endl;
}
#endif
// Step 2. [Terminate]
Polynomial<VeryLong> A0 = lift<VeryLong, VeryLongModular>(R);
Polynomial<VeryLongModular> U_ = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(U, r);
Polynomial<VeryLongModular> B_ = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(B, r);
Polynomial<VeryLong> B0 = lift<VeryLong, VeryLongModular>(U_ * f + B_ * t);
if (debug) std::cout << "***** A0 = " << A0 << std::endl;
if (debug) std::cout << "***** B0 = " << B0 << std::endl;
A1 = A + q * A0;
B1 = B + q * B0;
if (debug) std::cout << "***** A1 = " << A1 << std::endl;
if (debug) std::cout << "***** B1 = " << B1 << std::endl;
// should have C = A1 * B1 mod qr
#ifdef DO_CHECKS
Polynomial<VeryLong> check1 = A1 * B1;
VeryLongModular::set_default_modulus(q * r);
Polynomial<VeryLongModular> C2 = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(C, q * r);
Polynomial<VeryLongModular> check2 = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(check1, q * r);
if (C2 != check2)
{
std::cout << "Problem in result of hensel lift : " << std::endl;
std::cout << "qr = " << q * r << std::endl;
std::cout << "C mod qr = " << C2 << std::endl;
std::cout << "A1 * B1 mod qr = " << check2 << std::endl;
}
#endif
}
//Algorithm 3.5.6 (Quadratic Hensel Lift)
void quadratic_hensel_lift(const VeryLong& p,
const Polynomial<VeryLong>& A1,
const Polynomial<VeryLong>& B1,
const Polynomial<VeryLong>& U,
const Polynomial<VeryLong>& V,
Polynomial<VeryLong>& U1,
Polynomial<VeryLong>& V1)
{
bool debug = false;
if (std::getenv("FACTOR_VERBOSE_OUTPUT") && (::atoi(std::getenv("FACTOR_VERBOSE_OUTPUT")) & 4)) debug = true;
// Step 1. [Euclidean division]
VeryLong r(p);
Polynomial<VeryLong> polyone(VeryLong(1L));
Polynomial<VeryLong> gg = (polyone - U * A1 - V * B1) / p;
VeryLongModular::set_default_modulus(r);
Polynomial<VeryLongModular> g = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(gg, r);
if (debug) std::cout << "***** quadratic_hensel_lift" << std::endl;
if (debug) std::cout << "***** g = " << g << std::endl;
Polynomial<VeryLongModular> V_ = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(V, r);
Polynomial<VeryLongModular> A1_ = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(A1, r);
Polynomial<VeryLongModular> t;
Polynomial<VeryLongModular> R;
euclidean_division<VeryLongModular>(V_ * g, A1_, t, R);
if (debug) std::cout << "***** t = " << t << std::endl;
if (debug) std::cout << "***** R = " << R << std::endl;
// check
if (R.deg() >= A1.deg())
{
std::cout << "Problem: euclidean_division of " << V_ * g << " and " << A1_ << std::endl;
std::cout << "gave t = " << t << ", R = " << R << std::endl;
}
#ifdef DO_CHECKS
Polynomial<VeryLongModular> check = V_ * g - A1_ * t;
if (check != R)
{
std::cout << "Problem: check != R, check = " << check << ", R = " << R << std::endl;
}
#endif
// Step 2. [Terminate]
// lift(U * g + B1 * t)
Polynomial<VeryLongModular> U_ = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(U, r);
Polynomial<VeryLongModular> B1_ = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(B1, r);
Polynomial<VeryLong> U0 = lift<VeryLong, VeryLongModular>(U_ * g + B1_ * t);
Polynomial<VeryLong> V0 = lift<VeryLong, VeryLongModular>(V_ * g - A1_ * t);
U1 = U + p * U0;
V1 = V + p * V0;
}
/*
* Given polynomials f, A and B in Z[X] with
*
* f = A * B mod p
*
* return polynomials A1 and B1 in Z[X] with
*
*
* f = A * B mod p
*
* using a combination of hensel_lift and quadratic_hensel_lift
*
*/
void multiple_hensel_lift(const VeryLong& p,
long int e,
const Polynomial<VeryLong>& f,
const Polynomial<VeryLong>& AA,
const Polynomial<VeryLong>& BB,
Polynomial<VeryLong>& A1,
Polynomial<VeryLong>& B1)
{
bool debug = false;
if (std::getenv("FACTOR_VERBOSE_OUTPUT") && (::atoi(std::getenv("FACTOR_VERBOSE_OUTPUT")) & 4)) debug = true;
Polynomial<VeryLong> A(AA);
Polynomial<VeryLong> B(BB);
VeryLongModular::set_default_modulus(p);
Polynomial<VeryLongModular> Ap_ = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(A, p);
Polynomial<VeryLongModular> Bp_ = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(B, p);
Polynomial<VeryLongModular> U_;
Polynomial<VeryLongModular> V_;
if (debug) std::cout << "%%%%% Ap_ = " << Ap_ << ", Bp_ = " << Bp_ << ", gcd(Ap_,Bp_) = " << gcd(Ap_, Bp_) << std::endl;
Polynomial<VeryLongModular> G = extended_gcd<Polynomial<VeryLongModular> >(Ap_, Bp_, U_, V_);
if (debug) std::cout << "%%%%% G = " << G << ", U_ = " << U_ << ", V_ = " << V_ << std::endl;
VeryLongModular g = G.coefficient(0);
U_ /= g;
V_ /= g;
if (debug) std::cout << "%%%%% U_ = " << U_ << ", V_ = " << V_ << std::endl;
#ifdef DO_CHECKS
Polynomial<VeryLongModular> check = U_ * Ap_ + V_ * Bp_;
const VeryLongModular one(1L);
if (check != Polynomial<VeryLongModular>(one))
{
std::cout << "Problem U_ Ap_ + V_ Bp_ != 1, check = " << check << std::endl;
std::cout << "Ap_ = " << Ap_ << ", Bp_ = " << Bp_ << ", gcd(Ap_,Bp_) = " << gcd(Ap_, Bp_) << std::endl;
std::cout << "U_ = " << U_ << ", V_ = " << V_ << std::endl;
}
#endif
Polynomial<VeryLong> U = lift<VeryLong, VeryLongModular>(U_);
Polynomial<VeryLong> V = lift<VeryLong, VeryLongModular>(V_);
VeryLong p_power(p);
long int u(1L);
long int v(0L);
while (u <= e / 2L)
{
hensel_lift(p_power, p_power, A, B, f, U, V, A1, B1);
Polynomial<VeryLong> U1;
Polynomial<VeryLong> V1;
quadratic_hensel_lift(p_power, A1, B1, U, V, U1, V1);
U = U1;
V = V1;
A = A1;
B = B1;
u *= 2L;
v++;
p_power *= p_power;
}
/*
* Here A1 * B1 = f mod p_power where
*
* u
* p_power = p
*
* v
* u = 2
*
* and u <= e
*
* e
* Now need to hensel_lift to p_power = p
*
*/
VeryLong p_power_deficit = pow<VeryLong, int>(p, e) / p_power;
if (debug) std::cout << "%%%%% p_power = " << p_power << std::endl;
if (debug) std::cout << "%%%%% p_power_deficit = " << p_power_deficit << std::endl;
if (p_power_deficit> VeryLong(1L))
{
if (debug) std::cout << "%%%%% calling final hensel_lift" << std::endl;
hensel_lift(p_power_deficit, p_power, A, B, f, U, V, A1, B1);
}
}
void hensel_lift(const VeryLong& p,
const VeryLong& q,
const Polynomial<VeryLong>& C,
std::deque<Polynomial<VeryLong> >& VV,
std::deque<Polynomial<VeryLong> >& W)
{
bool debug = false;
if (std::getenv("FACTOR_VERBOSE_OUTPUT") && (::atoi(std::getenv("FACTOR_VERBOSE_OUTPUT")) & 4)) debug = true;
if (debug) std::cout << "%%%%% generalised hensel lift : " << std::endl;
Polynomial<VeryLong> A = VV[0];
Polynomial<VeryLong> B = VV[1];
for (size_t i = 2; i < VV.size(); i++) B *= VV[i];
VeryLongModular::set_default_modulus(p);
Polynomial<VeryLongModular> Ap_ = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(A, p);
Polynomial<VeryLongModular> Bp_ = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(B, p);
Polynomial<VeryLongModular> U_;
Polynomial<VeryLongModular> V_;
if (debug) std::cout << "%%%%% Ap_ = " << Ap_ << ", Bp_ = " << Bp_ << ", gcd(Ap_,Bp_) = " << gcd(Ap_, Bp_) << std::endl;
Polynomial<VeryLongModular> G = extended_gcd<Polynomial<VeryLongModular> >(Ap_, Bp_, U_, V_);
if (debug) std::cout << "%%%%% G = " << G << ", U_ = " << U_ << ", V_ = " << V_ << std::endl;
VeryLongModular g = G.coefficient(0);
U_ /= g;
V_ /= g;
if (debug) std::cout << "%%%%% U_ = " << U_ << ", V_ = " << V_ << std::endl;
#ifdef DO_CHECKS
Polynomial<VeryLongModular> check = U_ * Ap_ + V_ * Bp_;
const VeryLongModular one(1L);
if (check != Polynomial<VeryLongModular>(one))
{
std::cout << "Problem U_ Ap_ + V_ Bp_ != 1, check = " << check << std::endl;
std::cout << "Ap_ = " << Ap_ << ", Bp_ = " << Bp_ << ", gcd(Ap_,Bp_) = " << gcd(Ap_, Bp_) << std::endl;
std::cout << "U_ = " << U_ << ", V_ = " << V_ << std::endl;
}
#endif
Polynomial<VeryLong> U = lift<VeryLong, VeryLongModular>(U_);
Polynomial<VeryLong> V = lift<VeryLong, VeryLongModular>(V_);
Polynomial<VeryLong> A1;
Polynomial<VeryLong> B1;
if (debug) std::cout << "%%%%% Calling hensel_lift(), p = " << p << ", q = " << q << std::endl;
hensel_lift(p, q, A, B, C, U, V, A1, B1);
//Polynomial<VeryLong> U1;
//Polynomial<VeryLong> V1;
//quadratic_hensel_lift(p, q, A1, B1, U, V, U1, V1)
W.push_back(A1);
VV.pop_front();
if (VV.size() > 1)
{
if (debug) std::cout << "%%%%% Calling hensel_lift(), p = " << p << ", q = " << q << ", B1 = " << B1 << std::endl;
hensel_lift(p, q, B1, VV, W);
}
else
{
W.push_back(B1);
}
}
static long int combinations(int n, int j)
{
// number of ways of choosing j from n
if (j > n) return 0;
// n! / (n-j)!j! = n(n-1)...(n-j+1) / j!
// we only need to consider n up to 5 for the moment, so we can use
// a naive calculation
int res = 1;
for (int i = 0; i < j; i++)
{
res *= n - i;
}
for (int i = 0; i < j; i++)
{
res /= i + 1;
}
return (long int)res;
}
static VeryLong size(const Polynomial<VeryLong>& A)
{
int n = A.deg();
VeryLong s(0L);
for (int i = 0; i <= n; i++)
{
s += A.coefficient(i) * A.coefficient(i);
}
VeryLong t = s.nth_root(2);
if (t*t < s) t += 1L;
return t;
}
static VeryLong bound(const Polynomial<VeryLong>& A, int n)
{
const VeryLong zero(0L);
/* find bound on coefficients of any polynomial B that is a factor of A
// with A having degree m, and B having degree n, n | m
// From Theorem 3.3.1 the bound is the maximum of the bounds given by
// / n - 1 \ / n - 1 \
// | b_j | <= | | | A | + | | | a_m |
// \ j / \ j - 1 /
*/
VeryLong sizeA = size(A);
VeryLong B(zero);
if (n >= A.deg()) return B;
VeryLong a_m = A.coefficient(A.deg());
if (a_m < zero) a_m = -a_m;
for (int j = 0; j <= n; j++)
{
VeryLong B_j = sizeA * VeryLong(combinations(n - 1, j)) + a_m * VeryLong(combinations(n - 1, j - 1));
if (B_j > B) B = B_j;
}
return B;
}
#ifdef DO_CHECKS
void check(const Polynomial<VeryLong>& U,
const std::deque<Polynomial<VeryLong> >& Ufactors,
const VeryLong p_power)
{
// check that U = prod Ufactors[i] mod p_power
Polynomial<VeryLong> check(VeryLong(1L));
for (size_t i = 0; i < Ufactors.size(); i++)
{
check *= Ufactors[i];
}
Polynomial<VeryLongModular> check1 = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(check, p_power);
Polynomial<VeryLongModular> check2 = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(U, p_power);
if (check1 != check2)
{
std::cout << "Problem: check1 - check2 = " << check1 - check2 << std::endl;
std::cout << "check1 = " << check1 << ", check2 = " << check2 << std::endl;
}
}
#endif
void display(const std::vector<std::vector<int> >& v)
{
std::cout << "Total combinations = " << static_cast<long int>(v.size()) << std::endl;
for (size_t i = 0; i < v.size(); i++)
{
std::cout << "(";
for (size_t j = 0; j < v[i].size(); j++)
{
std::cout << v[i][j];
if (j < v[i].size() - 1) std::cout << " ";
}
std::cout << ")" << std::endl;
}
}
std::vector<std::vector<int> > generate_combinations(int r, int d)
{
std::vector<std::vector<int> > res;
for (KnuthCombinations c(d, r); !c.done(); c.next())
//for (CoolexCombinations c(d, r); !c.done(); c.next())
{
std::vector<int> combination;
for (size_t i = 0; i < c.size(); ++i)
{
combination.push_back(c(i) + 1);
}
res.push_back(combination);
}
return res;
}
std::vector<std::vector<int> > generate_combinations_(int r, int d)
{
static thread_local int recurse = 0;
recurse++;
// cout << "generate_combinations_(" << r << ", " << d << ")" << endl;
std::vector<std::vector<int> > res;
if (d == 1)
{
for (int i = 0; i < r; i++)
{
std::vector<int> v;
v.push_back(i + 1);
res.push_back(v);
}
recurse--;
return res;
}
if (d == r)
{
std::vector<int> v;
for (int i = 0; i < r; i++)
{
v.push_back(i + 1);
}
res.push_back(v);
recurse--;
return res;
}
if (recurse == 1 && 2 * d == r)
{
std::vector<std::vector<int> > res1 = generate_combinations_(r - 1, d - 1);
for (size_t k = 0; k < res1.size(); k++)
{
res1[k].push_back(r);
res.push_back(res1[k]);
}
recurse--;
return res;
}
for (int j = r; j >= d; --j)
{
std::vector<std::vector<int> > res1 = generate_combinations_(j - 1, d - 1);
for (size_t k = 0; k < res1.size(); k++)
{
res1[k].push_back(j);
res.push_back(res1[k]);
}
}
recurse--;
return res;
}
#ifdef THREADED_OLD_METHOD
bool process_combination(const Polynomial<VeryLong>& U, const VeryLong l_U, const VeryLong p_power, const std::vector<Polynomial<VeryLongModular> > Ufactors_, std::vector<int>& combination, int d, int r, Polynomial<VeryLong>& V, bool debug, std::string& debug_output)
{
const VeryLong two(2L);
const Polynomial<VeryLong> unit_poly(VeryLong(1L));
VeryLongModular::set_default_modulus(p_power);
Polynomial<VeryLongModular> V_(VeryLongModular(1L));
std::ostringstream oss;
if (debug) oss << ">>>> V_ = ";
for (int j = 0; j < d; j++)
{
int index = combination[j] - 1;
#if 0
if (j == d - 1 && 2 * d == r)
{
index = 0;
combination[j] = 1;
}
#endif
if (debug) oss << "(U(" << index + 1 << ") = " << Ufactors_[index] << ")";
V_ *= Ufactors_[index];
}
if (debug) oss << std::endl;
if (debug) oss << ">>>> V_ = " << V_ << std::endl;
Polynomial<VeryLongModular> V__(V_);
if (2 * V_.deg() <= U.deg())
{
V__ *= VeryLongModular(l_U);
}
else
{
V__ = convert_to_F_p<VeryLong, VeryLong, VeryLongModular>(U, p_power) / V_;
}
if (debug) oss << ">>>> " << "V__ = " << V__ << std::endl;
V = lift<VeryLong, VeryLongModular>(V__);
if (debug) oss << ">>>> " << "V = " << V << std::endl;
if (debug) oss << ">>>> " << "p_power = " << p_power << std::endl;
for (int j = 0; j <= V.deg(); j++)
{
if (two * V.coefficient(j) < -p_power)
{
VeryLong m = (two * V.coefficient(j) + p_power) / (two * p_power);
V.set_coefficient(j, V.coefficient(j) - m * p_power);
}
if (two * V.coefficient(j) >= p_power)
{
VeryLong m = (two * V.coefficient(j) - p_power) / (two * p_power) + VeryLong(1L);
V.set_coefficient(j, V.coefficient(j) - m * p_power);
}
while (two * V.coefficient(j) < -p_power)
{
V.set_coefficient(j, V.coefficient(j) + p_power);
}
while (two * V.coefficient(j) >= p_power)
{
V.set_coefficient(j, V.coefficient(j) - p_power);
}
}
if (debug) oss << ">>>> " << "V = " << V << std::endl;
if (V.deg() <= 0 || V == unit_poly)
{
return false;
}
Polynomial<VeryLong> Q = (l_U * U) / V;
Polynomial<VeryLong> UUU = Q;
UUU *= V;
if (UUU == l_U * U)
{
if (debug) oss << ">>>> " << "V divides l(U)U!" << std::endl;
if (debug) oss << ">>>> " << "l(U)U = " << l_U * U << ", V = " << V << std::endl;
if (debug) oss << ">>>> " << "l(U)U / V = " << (l_U * U) / V << std::endl;
if (debug)
{
oss << ">>>> " << "V is" << std::endl;
for (int j = 0; j < d; j++)
{
int index = combination[j];
oss << ">>>> U(" << index << ") = " << Ufactors_[index - 1] << std::endl;
}
}
debug_output = oss.str();
return true;
}
debug_output = oss.str();
return false;
}
template <typename T>
class WorkerThread
{
public:
WorkerThread(bool debug = false) : debug_(debug), stopped_(false)
{
}
void set_debug(bool debug)
{
debug_ = debug;
}
void submit(const T& work)
{
work_.push_back(work);
}
void run()
{
if (work_.empty())
{
return;
}
worker_thread_ = std::unique_ptr<std::thread> (new std::thread([this]()
{
std::this_thread::sleep_for(std::chrono::milliseconds(50));
size_t i(0);
if (debug_)
{
std::cerr << "Thread " << std::this_thread::get_id() << " starting : work_.size() = " << work_.size() << std::endl;
}
for (auto& w : work_)
{
w();
++i;
if (debug_ && i % 100 == 0)
{
std::cerr << "Thread " << std::this_thread::get_id() << " processed : " << i << std::endl;
}
if (stopped_) break;
}
if (debug_)
{
std::cerr << "Thread " << std::this_thread::get_id() << " done" << std::endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}));
}
void join()
{
if (worker_thread_ && worker_thread_->joinable())
{
if (debug_) std::cerr << "Waiting for thread " << worker_thread_->get_id() << " to finish" << std::endl;
worker_thread_->join();
}
}
void stop()
{
if (debug_)
{
if (worker_thread_)
{
std::cerr << "Thread " << worker_thread_->get_id() << " stopping" << std::endl;
}
}
stopped_ = true;
}
~WorkerThread()
{
if (debug_)
{
std::cerr << "In ~WorkerThread()" << std::endl;
}
join();
}
WorkerThread(const WorkerThread& t) = delete;
WorkerThread& operator=(const WorkerThread& t) = delete;
private:
std::vector<T> work_;
std::unique_ptr<std::thread> worker_thread_;
bool debug_;
bool stopped_;
};
template <typename T>
class WorkerThreadManager
{
public:
WorkerThreadManager(size_t thread_count = 4, bool debug = false)
: thread_count_(thread_count), worker_threads_(thread_count), debug_(debug), next_(0)
{
for (auto& worker_thread: worker_threads_)
{
worker_thread.set_debug(debug);
}
}
~WorkerThreadManager() {}
WorkerThreadManager(const WorkerThreadManager& t) = delete;
WorkerThreadManager& operator=(const WorkerThreadManager& t) = delete;
void submit(const T& work)
{
worker_threads_[next_ % thread_count_].submit(work);
next_++;
}
void run()
{
for (auto& worker_thread: worker_threads_)
{
worker_thread.run();
}
}
void wait()
{
for (auto& worker_thread: worker_threads_)
{
worker_thread.join();
}
}
void stop()
{
for (auto& worker_thread: worker_threads_)
{
worker_thread.stop();
}
}
private:
size_t thread_count_;
std::vector<WorkerThread<T> > worker_threads_;
bool debug_;
size_t next_;
};
struct process_combinations_results
{
std::vector<int> factor_combination;
Polynomial<VeryLong> factor;
int operator<(const process_combinations_results& pcr)
{
return (factor < pcr.factor);
}
};
int get_cores_to_use(size_t work_queue_size)
{
// Get the number of cores (i.e. the number of threads)
// which we will spread work_queue_size jobs over.
// Never use more than half the cores.
// Always use at least one core.
// work_queue size for each thread = work_queue_size / cores
// <=>
// cores = work_queue_size / work_queue size for each thread
// Set a minimum size for the work queue size for each thread
const int min_thread_work_queue_size(100);
int cores_to_use = std::min(static_cast<int>(get_nprocs() / 2), std::max(1, static_cast<int>(work_queue_size / min_thread_work_queue_size)));
//std::cout << "get_cores_to_use: " << cores_to_use << std::endl;
return cores_to_use;
}
bool process_combinations(const Polynomial<VeryLong>& U, const VeryLong l_U, const std::vector<Polynomial<VeryLongModular> >& Ufactors_, const VeryLong p_power, int d, int r, std::vector<process_combinations_results>& results, bool debug)
{
std::vector<std::vector<int> > combination_set = generate_combinations(r, d);
bool factorFound = false;
typedef std::function<void ()> worker_function;
WorkerThreadManager<worker_function> manager(get_cores_to_use(combination_set.size()), debug);
std::mutex result_mutex;
std::vector<process_combinations_results> res;
auto debug_output_callback = [&result_mutex, debug](const std::string& s)
{
std::lock_guard<std::mutex> lock(result_mutex);
if (debug) std::cerr << s << std::endl;
};
auto factor_found_callback = [&result_mutex, &factorFound, &res, &manager, debug](const std::vector<int>& combination, const Polynomial<VeryLong>& V)
{
process_combinations_results pcr;
pcr.factor_combination = combination;
pcr.factor = V;
std::lock_guard<std::mutex> lock(result_mutex);
res.push_back(pcr);
factorFound = true;
};
#define LIMIT_SUBMISSIONS 1
#ifdef LIMIT_SUBMISSIONS
const size_t MAX_SUBMITTED = 100L;
size_t comb = 0;
while (comb < combination_set.size())
{
size_t submitted = 0;
while (submitted < MAX_SUBMITTED && comb < combination_set.size())
{
const auto& f = [U, l_U, p_power, Ufactors_, &combination = combination_set[comb], d, r, &factor_found_callback, &debug_output_callback, debug]()
{
Polynomial<VeryLong> V_;
std::string debug_output;