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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
|
/** @file polyfact.cc
*
* Contains the routines for factorizing multivariate polynomials
*/
/* #[ License : */
/*
* Copyright (C) 1984-2026 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 modulo 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;
}
a = c;
}
// polynomial contains terms of the form c(x)^p
if (!a.is_one()) {
for (int i=1; i<a[1]; i+=a[i])
a[i+1+x] /= a.modp;
factorized_poly res2(squarefree_factors(a));
for (int i=0; i<(int)res2.factor.size(); i++) {
res.factor.push_back(res2.factor[i]);
res.power.push_back(a.modp*res2.power[i]);
}
}
return res;
}
/*
#] squarefree_factors_modp :
#[ squarefree_factors :
*/
/** Squarefree factorization of a primitive polynomial
*
* Description
* ===========
* Calculates a squarefree factorization of a multivariate
* polynomial, i.e., a factorization of the form
*
* a = PRODUCT(ai^i | i=1,...,k),
*
* with ai squarefree polynomials that are relatively prime. A
* polynomial is squarefree iff it is not divisable by b^2 for all b
* with degree greater than zero.
*
* Notes
* =====
* - For modp==0, Yun's efficient method is used
* - For modp!=0, a simple method is used
*
* [for details, see "Algorithms for Computer Algebra", pp. 337-343]
*/
const factorized_poly polyfact::squarefree_factors (const poly &a) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL: squarefree_factors("<<a<<")\n";
#endif
if (a.is_one()) return factorized_poly();
factorized_poly res;
if (a.modp==0)
res = squarefree_factors_Yun(a);
else
res = squarefree_factors_modp(a);
#ifdef DEBUG
cout << "*** [" << thetime() << "] RES : squarefree_factors("<<a<<") = "<<res<<"\n";
#endif
return res;
}
/*
#] squarefree_factors :
#[ Berlekamp_Qmatrix :
*/
/** Berlekamp Q-matrix
*
* Description
* ===========
* This method determines a basis for the eigenspace with eigenvalue
* 1 of the matrix Q required by Berlekamp's factorization
* algorithm. The matrix Q is defined by
*
* Qij = coefficient( x^j in x^(i*p) mod a(x) ),
*
* with i,j = 0,1,...,deg(a)-1. The eigenspace is determined with
* Gaussian elimination on Q-I.
*
* Notes
* =====
* - The polynomial a must be univariate.
* - The polynomial a must be squarefree.
* - The polynomial a must have coefficients in Z/p.
* - For efficiency, dense representations of polynomials are used.
*
* [for details, see "Algorithms for Computer Algebra", pp. 346-359]
*/
const vector<vector<WORD> > polyfact::Berlekamp_Qmatrix (const poly &_a) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL: Berlekamp_Qmatrix("<<_a<<")\n";
#endif
if (_a.all_variables() == vector<int>())
return vector<vector<WORD> >(0);
POLY_GETIDENTITY(_a);
poly a(_a);
int x = a.first_variable();
int n = a.degree(x);
int p = a.modp;
poly lc(a.integer_lcoeff());
a /= lc;
vector<vector<WORD> > Q(n, vector<WORD>(n));
// c is the vector of coefficients of the polynomial a
vector<WORD> c(n+1,0);
for (int j=1; j<a[0]; j+=a[j])
c[a[j+1+x]] = a[j+1+AN.poly_num_vars] * a[j+2+AN.poly_num_vars];
// d is the vector of coefficients of x^i mod a, starting with i=0
vector<WORD> d(n,0);
d[0]=1;
for (int i=0; i<=(n-1)*p; i++) {
// store the coefficients of x^(i*p) mod a
if (i%p==0) Q[i/p] = d;
// transform d=x^i mod a into d=x^(i+1) mod a
vector<WORD> e(n);
for (int j=0; j<n; j++) {
e[j] = (-(LONG)d[n-1]*c[j] + (j>0?d[j-1]:0)) % p;
if (e[j]<0) e[j]+=p;
}
d=e;
}
// Q = Q - I
for (int i=0; i<n; i++)
Q[i][i] = (Q[i][i] - 1 + p) % p;
// Gaussian elimination
for (int i=0; i<n; i++) {
// Find pivot
int ii=i; while (ii<n && Q[i][ii]==0) ii++;
if (ii==n) continue;
for (int k=0; k<n; k++)
swap(Q[k][ii],Q[k][i]);
// normalize row i, which becomes the pivot
WORD mul;
GetModInverses (Q[i][i], p, &mul, NULL);
vector<int> idx;
for (int k=0; k<n; k++) if (Q[k][i] != 0) {
// store indices of non-zero elements for sparse matrices
idx.push_back(k);
Q[k][i] = ((LONG)Q[k][i] * mul) % p;
}
// reduce
for (int j=0; j<n; j++)
if (j!=i && Q[i][j]!=0) {
LONG mul = Q[i][j];
for (int k=0; k<(int)idx.size(); k++) {
Q[idx[k]][j] = (Q[idx[k]][j] - mul*Q[idx[k]][i]) % p;
if (Q[idx[k]][j] < 0) Q[idx[k]][j]+=p;
}
}
}
for (int i=0; i<n; i++) {
// Q = Q - I
Q[i][i] = Q[i][i]-1;
// reduce all coefficients in the range 0,1,...,p-1
for (int j=0; j<n; j++) {
Q[i][j] = -Q[i][j]%p;
if (Q[i][j]<0) Q[i][j]+=p;
}
}
#ifdef DEBUG
cout << "*** [" << thetime() << "] RES : Berlekamp_Qmatrix("<<_a<<") = "<<Q<<"\n";
#endif
return Q;
}
/*
#] Berlekamp_Qmatrix :
#[ Berlekamp_find_factors :
*/
/** Berlekamp find factors
*
* Description
* ===========
* This method determines the factors of the polynomial, after a
* suitable prime number and ideal are selected, and the
* corresponding Q-matrix is calculated,
*
* This is done by trying each factor of the form
*
* gcd( a(x), q(x) + const ),
*
* with q(x) a polynomial, that is represented by a row of Q and
* const=0,1,...,p-1. These factors can be proven to form a exhaustive
* set of candidate factors.
*
* Notes
* =====
* a must be squarefree, and therefore a vector<poly> is suitable
* for returning the factors.
*
* [for details, see "Algorithms for Computer Algebra", pp. 346-359]
*/
const vector<poly> polyfact::Berlekamp_find_factors (const poly &_a, const vector<vector<WORD> > &_q) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL: Berlekamp_find_factors("<<_a<<","<<_q<<")\n";
#endif
if (_a.all_variables() == vector<int>()) return vector<poly>(1,_a);
POLY_GETIDENTITY(_a);
vector<vector<WORD> > q=_q;
int rank=0;
for (int i=0; i<(int)q.size(); i++)
if (q[i]!=vector<WORD>(q[i].size(),0)) rank++;
poly a(_a);
int x = a.first_variable();
int n = a.degree(x);
int p = a.modp;
a /= a.integer_lcoeff();
// Vector of factors, represented as dense polynomials mod p
vector<vector<WORD> > fac(1, vector<WORD>(n+1,0));
fac[0] = poly::to_coefficient_list(a);
bool finished=false;
// Loop over the columns of q + constant, i.e., an exhaustive list of possible factors
for (int i=1; i<n && !finished; i++) {
if (q[i] == vector<WORD>(n,0)) continue;
for (int s=0; s<p && !finished; s++) {
for (int j=0; j<(int)fac.size() && !finished; j++) {
vector<WORD> c = polygcd::coefficient_list_gcd(fac[j], q[i], p);
// If a non-trivial factor is found, add it to the list
if (c.size()!=1 && c.size()!=fac[j].size()) {
fac.push_back(c);
fac[j] = poly::coefficient_list_divmod(fac[j], c, p, 0);
if ((int)fac.size() == rank) finished=true;
}
}
// Increase the constant term by one
q[i][0] = (q[i][0]+1) % p;
}
}
// Convert the densely represented polynomials to sparse ones
vector<poly> res(fac.size(),poly(BHEAD 0, p));
for (int i=0; i<(int)fac.size(); i++)
res[i] = poly::from_coefficient_list(BHEAD fac[i],x,p);
#ifdef DEBUG
cout << "*** [" << thetime() << "] RES : Berlekamp_find_factors("<<_a<<","<<_q<<") = "<<res<<"\n";
#endif
return res;
}
/*
#] Berlekamp_find_factors :
#[ combine_factors :
*/
/** Combine incorrect factors
*
* Description
* ===========
* Occasionally, a polynomial is split into more factors (modulo
* <p,I>) than is possible over the integers. This might be caused
* by a unlucky choice of p or I, but might also happen for choices.
*
* When this happens, the coefficients of the factors are large
* after Hensel lifting, and therefore the factor does not divide
* the polynomial viewed as polynomial over the integers.
*
* This method combines those incorrect factors into correct ones.
*
* Notes
* =====
* Theoretically, this method takes exponential time (for ugly,
* constructed cases), but in practice it is fast. This can be fixed
* by implementing Van Hoeij's knapsack method (see: "Factoring
* polynomials and the knapsack problem" by M. van Hoeij). [TODO]
*/
const vector<poly> polyfact::combine_factors (const poly &a, const vector<poly> &f) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL: combine_factors("<<a<<","<<f<<")\n";
#endif
POLY_GETIDENTITY(a);
poly a0(a,0,1);
vector<poly> res;
int num_used = 0;
vector<bool> used(f.size(), false);
// Loop over all bitmasks with num=1,2,...,size(factors)/2 bits
// set, that contain only unused factors
for (int num=1; num<=(int)(f.size() - num_used)/2; num++) {
vector<int> next(f.size() - num_used, 0);
for (int i=0; i<num; i++) next[next.size()-1-i] = 1;
do {
poly fac(BHEAD 1,a.modp,a.modn);
for (int i=0, j=0; i<(int)f.size(); i++)
if (!used[i] && next[j++]) fac *= f[i];
fac /= fac.integer_lcoeff();
fac *= a.integer_lcoeff();
fac /= polygcd::integer_content(poly(fac,0,1));
if ((a0 % fac).is_zero()) {
res.push_back(fac);
for (int i=0, j=0; i<(int)f.size(); i++)
if (!used[i]) used[i] = next[j++];
num_used += num;
num--;
break;
}
}
while (next_permutation(next.begin(), next.end()));
}
// All unused factors together form one more factor
if (num_used != (int)f.size()) {
poly fac(BHEAD 1,a.modp,a.modn);
for (int i=0; i<(int)f.size(); i++)
if (!used[i]) fac *= f[i];
fac /= fac.integer_lcoeff();
fac *= a.integer_lcoeff();
fac /= polygcd::integer_content(poly(fac,0,1));
res.push_back(fac);
}
#ifdef DEBUG
cout << "*** [" << thetime() << "] RES : combine_factors("<<a<<","<<f<<") = "<<res<<"\n";
#endif
return res;
}
/*
#] combine_factors :
#[ factorize_squarefree :
*/
/** Factorization of a squarefree polynomial
*
* Description
* ===========
* This method find the factors of a primitive squarefree
* polynomial. It proceeds with the following steps:
*
* - Try a number a primes p and ideals I
* - Determine the rank (=number of factors) of the Q-matrix
* - Select the best (i.e., least number of factors) and find the
* actual factors mod <p,I>
* - Use Hensel lifting and factor combination to find the correct
* factors over the integers
*
* Notes
* =====
* The polynomial must be primitive and squarefree
*/
const vector<poly> polyfact::factorize_squarefree (const poly &a, const vector<int> &x) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL: factorize_squarefree("<<a<<")\n";
#endif
POLY_GETIDENTITY(a);
WORD p=a.modp, n=a.modn;
try_again:
int bestp=0;
int min_factors = INT_MAX;
poly amodI(BHEAD 0), bestamodI(BHEAD 0);
vector<int> c,d,bestc,bestd;
vector<vector<WORD> > q,bestq;
// Factorize leading coefficient
factorized_poly lc(factorize(a.lcoeff_univar(x[0])));
// Try a number of primes
int prime_tries = 0;
while (prime_tries<POLYFACT_NUM_CONFIRMATIONS && min_factors>1) {
if (a.modp == 0) {
p = choose_prime(a,x,p);
n = 0;
if (a.degree(x[0]) % p == 0) continue;
// Univariate case: check whether the polynomial mod p is squarefree
// Multivariate case: this check is done after choosing I (for efficiency)
if (x.size()==1) {
poly amodp(a,p,1);
if (polygcd::gcd_Euclidean(amodp, amodp.derivative(x[0])).degree(x[0]) != 0)
continue;
}
}
// Try a number of ideals
if (x.size()>1)
for (int ideal_tries=0; ideal_tries<POLYFACT_MAX_IDEAL_TRIES; ideal_tries++) {
c = choose_ideal(a,p,lc,x);
if (c.size()>0) break;
}
if (x.size()==1 || c.size()>0) {
amodI = a;
for (int i=0; i<(int)c.size(); i++)
amodI %= poly::simple_poly(BHEAD x[i+1],c[i]);
// Determine Q-matrix and its rank. Smaller rank is better.
q = Berlekamp_Qmatrix(poly(amodI,p,1));
int rank=0;
for (int i=0; i<(int)q.size(); i++)
if (q[i]!=vector<WORD>(q[i].size(),0)) rank++;
if (rank<min_factors) {
bestp=p;
bestc=c;
bestq=q;
bestamodI=amodI;
min_factors = rank;
prime_tries = 0;
}
if (rank==min_factors)
prime_tries++;
#ifdef DEBUG
cout << "*** [" << thetime() << "] (A) : factorize_squarefree("<<a<<
") try p=" << p << " #factors=" << rank << " (min="<<min_factors<<"x"<<prime_tries<<")" << endl;
#endif
}
}
p=bestp;
c=bestc;
q=bestq;
amodI=bestamodI;
// Determine to which power of p to lift
if (n==0) {
n = choose_prime_power(amodI,p);
n = MaX(n, choose_prime_power(a,p));
}
amodI.setmod(p,n);
#ifdef DEBUG
cout << "*** [" << thetime() << "] (B) : factorize_squarefree("<<a<<
") chosen c = " << c << ", p^n = "<<p<<"^"<<n<<endl;
cout << "*** [" << thetime() << "] (C) : factorize_squarefree("<<a<<
") #factors = " << min_factors << endl;
#endif
// Find factors
vector<poly> f(Berlekamp_find_factors(poly(amodI,p,1),q));
// Lift coefficients
if (f.size() > 1) {
f = lift_coefficients(amodI,f);
if (f==vector<poly>()) {
#ifdef DEBUG
cout << "factor_squarefree failed (lift_coeff step) : " << endl;
#endif
goto try_again;
}
// Combine factors
if (a.modp==0) f = combine_factors(amodI,f);
}
// Lift variables
if (f.size() == 1)
f = vector<poly>(1, a);
if (x.size() > 1 && f.size() > 1) {
// The correct leading coefficients of the factors can be
// reconstructed from prime number factors of the leading
// coefficients modulo I. This is possible since all factors of
// the leading coefficient have unique prime factors for the ideal
// I is chosen as such.
poly amodI(a);
for (int i=0; i<(int)c.size(); i++)
amodI %= poly::simple_poly(BHEAD x[i+1],c[i]);
poly delta(polygcd::integer_content(amodI));
vector<poly> lcmodI(lc.factor.size(), poly(BHEAD 0));
for (int i=0; i<(int)lc.factor.size(); i++) {
lcmodI[i] = lc.factor[i];
for (int j=0; j<(int)c.size(); j++)
lcmodI[i] %= poly::simple_poly(BHEAD x[j+1],c[j]);
}
vector<poly> correct_lc(f.size(), poly(BHEAD 1,p,n));
for (int j=0; j<(int)f.size(); j++) {
poly lc_f(f[j].integer_lcoeff() * delta);
WORD nlc_f = lc_f[lc_f[1]];
poly quo(BHEAD 0),rem(BHEAD 0);
WORD nquo,nrem;
for (int i=(int)lcmodI.size()-1; i>=0; i--) {
if (i==0 && lc.factor[i].is_integer()) continue;
do {
DivLong((UWORD *)&lc_f[2+AN.poly_num_vars], nlc_f,
(UWORD *)&lcmodI[i][2+AN.poly_num_vars], lcmodI[i][lcmodI[i][1]],
(UWORD *)&quo[0], &nquo,
(UWORD *)&rem[0], &nrem);
if (nrem == 0) {
correct_lc[j] *= lc.factor[i];
lc_f.termscopy(&quo[0], 2+AN.poly_num_vars, ABS(nquo));
nlc_f = nquo;
}
}
while (nrem == 0);
}
}
for (int i=0; i<(int)correct_lc.size(); i++) {
poly correct_modI(correct_lc[i]);
for (int j=0; j<(int)c.size(); j++)
correct_modI %= poly::simple_poly(BHEAD x[j+1],c[j]);
poly d(polygcd::integer_gcd(correct_modI, f[i].integer_lcoeff()));
correct_lc[i] *= f[i].integer_lcoeff() / d;
delta /= correct_modI / d;
f[i] *= correct_modI / d;
}
// increase n, because of multiplying with delta
if (!delta.is_one()) {
poly deltapow(BHEAD 1);
for (int i=1; i<(int)correct_lc.size(); i++)
deltapow *= delta;
while (!deltapow.is_zero()) {
deltapow /= poly(BHEAD p);
n++;
}
for (int i=0; i<(int)f.size(); i++) {
f[i].modn = n;
correct_lc[i].modn = n;
}
}
poly aa(a,p,n);
for (int i=0; i<(int)correct_lc.size(); i++) {
correct_lc[i] *= delta;
f[i] *= delta;
if (i>0) aa *= delta;
}
f = lift_variables(aa,f,x,c,correct_lc);
for (int i=0; i<(int)f.size(); i++)
if (a.modp == 0)
f[i] /= polygcd::integer_content(poly(f[i],0,1));
else
f[i] /= polygcd::content_univar(f[i], x[0]);
if (f==vector<poly>()) {
#ifdef DEBUG
cout << "factor_squarefree failed (lift_var step)" << endl;
#endif
goto try_again;
}
}
// set modulus of the factors correctly
if (a.modp==0)
for (int i=0; i<(int)f.size(); i++)
f[i].setmod(0,1);
// Final check (not sure if this is necessary, but it doesn't hurt)
poly check(BHEAD 1,a.modp,a.modn);
for (int i=0; i<(int)f.size(); i++)
check *= f[i];
if (check != a) {
#ifdef DEBUG
cout << "factor_squarefree failed (final check) : " << f << endl;
#endif
goto try_again;
}
#ifdef DEBUG
cout << "*** [" << thetime() << "] RES : factorize_squarefree("<<a<<","<<x<<","<<c<<") = "<<f<<"\n";
#endif
return f;
}
/*
#] factorize_squarefree :
#[ factorize :
*/
/** Factorization of polynomials
*
* Description
* ===========
* This method removes the content of a polynomial, splits it into
* squarefree factors and calls "factorize_squarefree" for each of
* these factors.
*/
const factorized_poly polyfact::factorize (const poly &a) {
#ifdef DEBUG
cout << "*** [" << thetime() << "] CALL: factorize("<<a<<")\n";
#endif
vector<int> x = a.all_variables();
// No variables, so just one factor
if (x.size() == 0) {
factorized_poly res;
if (a.is_one()) return res;
res.add_factor(a,1);
return res;
}
// Remove content
poly conta(polygcd::content_univar(a,x[0]));
factorized_poly faca(factorize(conta));
poly ppa(a / conta);
// Find a squarefree factorization
factorized_poly b(squarefree_factors(ppa));
#ifdef DEBUG
cout << "*** [" << thetime() << "] ... : factorize("<<a<<") : SFF = "<<b<<"\n";
#endif
// Factorize each squarefree factor and build the "factorized_poly"
for (int i=0; i<(int)b.factor.size(); i++) {
vector<poly> c(factorize_squarefree(b.factor[i], x));
for (int j=0; j<(int)c.size(); j++) {
faca.factor.push_back(c[j]);
faca.power.push_back(b.power[i]);
}
}
#ifdef DEBUG
cout << "*** [" << thetime() << "] RES : factorize("<<a<<") = "<<faca<<"\n";
#endif
return faca;
}
/*
#] factorize :
*/
|