14namespace layer1_foundations {
19static void (*diophant_user_callback_solution_found)(
int *sol,
20 int len,
int nb_sol,
void *data) = NULL;
22static void diophant_callback_solution_found(
int *sol,
23 int len,
int nb_sol,
void *data);
116 int verbose_level = 0;
117 int f_v = (verbose_level >= 1);
121 cout <<
"diophant::freeself" << endl;
139 cout <<
"diophant::freeself before RHS" << endl;
145 cout <<
"diophant::freeself before RHS_low" << endl;
151 cout <<
"diophant::freeself before RHS1" << endl;
160 cout <<
"diophant::freeself before eqn_label" << endl;
163 for (i = 0; i <
m; i++) {
181 cout <<
"diophant::freeself done" << endl;
208 for (i = 0; i <
n; i++) {
214 for (i = 0; i <
m; i++) {
226 int f_v = (verbose_level >= 1);
229 cout <<
"diophant::init_var_labels" << endl;
235 cout <<
"diophant::init_var_labels done" << endl;
242 int f_v = (verbose_level >= 1);
243 int nb_rows, nb_cols;
248 cout <<
"diophant::join_problems" << endl;
250 if (D1->
n != D2->
n) {
251 cout <<
"D1->n != D2->n" << endl;
255 cout <<
"D1->sum != D2->sum" << endl;
259 cout <<
"!D1->f_has_sum" << endl;
263 cout <<
"!D2->f_has_sum" << endl;
267 if (D1->f_x_max != D2->f_x_max) {
268 cout <<
"D1->f_x_max != D2->f_x_max" << endl;
275 nb_rows = nb_r1 + nb_r2;
276 open(nb_rows, nb_cols);
282 for (i = 0; i < nb_cols; i++) {
284 cout <<
"D1->x_max[i] != D2->x_max[i]" << endl;
291 for (i = 0; i < nb_cols; i++) {
296 for (i = 0; i < nb_r1; i++) {
297 for (j = 0; j < nb_cols; j++) {
298 Aij(i, j) = D1->
Aij(i, j);
304 for (i = 0; i < nb_r2; i++) {
305 for (j = 0; j < nb_cols; j++) {
306 Aij(nb_r1 + i, j) = D2->
Aij(i, j);
313 cout <<
"diophant::join_problems done" << endl;
318 int *weights,
int nb_weights,
int target_value,
321 int f_v = (verbose_level >= 1);
325 cout <<
"diophant::init_partition_problem" << endl;
328 for (j = 0; j < nb_weights; j++) {
329 x_max[j] = target_value / weights[j];
335 for (j = 0; j < nb_weights; j++) {
336 Aij(0, j) = weights[j];
338 RHSi(0) = target_value;
341 cout <<
"diophant::init_partition_problem" << endl;
346 int *weights,
int *bounds,
int nb_weights,
int target_value,
349 int f_v = (verbose_level >= 1);
353 cout <<
"diophant::init_partition_problem_with_bounds" << endl;
356 for (j = 0; j < nb_weights; j++) {
357 x_max[j] = bounds[j];
363 for (j = 0; j < nb_weights; j++) {
364 Aij(0, j) = weights[j];
366 RHSi(0) = target_value;
369 cout <<
"diophant::init_partition_problem_with_bounds" << endl;
376 int nb_rows,
int nb_cols,
int *Inc,
int nb_to_select,
377 int *Rhs,
int verbose_level)
379 int f_v = (verbose_level >= 1);
383 cout <<
"diophant::init_problem_of_Steiner_type_with_RHS" << endl;
385 open(nb_rows, nb_cols);
386 for (j = 0; j < nb_cols; j++) {
393 for (i = 0; i < nb_rows; i++) {
394 for (j = 0; j < nb_cols; j++) {
395 Aij(i, j) = Inc[i * nb_cols + j];
401 cout <<
"diophant::init_problem_of_Steiner_type_with_RHS done" << endl;
406 int nb_rows,
int nb_cols,
int *Inc,
int nb_to_select,
409 int f_v = (verbose_level >= 1);
413 cout <<
"diophant::init_problem_of_Steiner_type" << endl;
415 open(nb_rows, nb_cols);
416 for (j = 0; j < nb_cols; j++) {
423 for (i = 0; i < nb_rows; i++) {
424 for (j = 0; j < nb_cols; j++) {
425 Aij(i, j) = Inc[i * nb_cols + j];
431 cout <<
"diophant::init_problem_of_Steiner_type done" << endl;
437 int f_v = (verbose_level >= 1);
441 cout <<
"diophant::init_RHS" << endl;
443 for (i = 0; i <
m; i++) {
447 cout <<
"diophant::init_RHS done" << endl;
452 int nb_to_select,
int verbose_level)
454 int f_v = (verbose_level >= 1);
456 int nb_zeros = 0, nb_ones = 0, total;
459 cout <<
"diophant::init_clique_finding_problem" << endl;
461 for (i = 0; i < nb_pts; i++) {
462 for (j = i + 1; j < nb_pts; j++) {
463 if (Adj[i * nb_pts + j]) {
471 total = nb_ones + nb_zeros;
473 cout <<
"nb_zeros=" << nb_zeros << endl;
474 cout <<
"nb_ones =" << nb_ones << endl;
475 total = nb_zeros + nb_ones;
476 cout <<
"edge density = " <<
477 (double)nb_ones / (
double)total << endl;
479 open(nb_zeros, nb_pts);
480 for (j = 0; j < nb_pts; j++) {
488 for (i1 = 0; i1 < nb_pts; i1++) {
489 for (i2 = i1 + 1; i2 < nb_pts; i2++) {
490 if (Adj[i1 * nb_pts + i2] == 0) {
501 cout <<
"diophant::init_clique_finding_problem done" << endl;
510 for (i = 0; i <
m *
n; i++) {
519 for (j = 0; j <
n; j++) {
528 for (j = 0; j <
n; j++) {
536 cout <<
"diophant::Aij i >= m" << endl;
537 cout <<
"i=" << i << endl;
538 cout <<
"m=" <<
m << endl;
542 cout <<
"diophant::Aij j >= n" << endl;
543 cout <<
"j=" << j << endl;
544 cout <<
"n=" <<
n << endl;
553 cout <<
"diophant::Gij i >= m" << endl;
554 cout <<
"i=" << i << endl;
555 cout <<
"m=" <<
m << endl;
559 cout <<
"diophant::Gij j >= n" << endl;
560 cout <<
"j=" << j << endl;
561 cout <<
"n=" <<
n << endl;
570 cout <<
"diophant::RHSi i >= m" << endl;
579 cout <<
"diophant::RHS_low_i i >= m" << endl;
590 cout <<
"diophant::init_eqn_label i >= m" << endl;
591 cout <<
"label: " <<
label << endl;
592 cout <<
"i=" << i << endl;
599 l = strlen(
label) + 1;
612 for (i = 0; i <
m; i++) {
614 for (j = 0; j <
n; j++) {
617 cout << setw(1) << c;
620 cout <<
" = " <<
RHS[i];
623 cout <<
" <= " <<
RHS[i];
626 cout <<
" in [" <<
RHS_low_i(i) <<
"," <<
RHS[i] <<
"]";
629 cout <<
" ZOR " <<
RHS[i];
631 cout <<
" (rowsum=" << s <<
")" << endl;
634 cout <<
"sum = " <<
sum << endl;
637 cout <<
"there is no restriction on the sum" << endl;
645 cout <<
"diophant with m=" <<
m <<
" n=" <<
n << endl;
646 for (i = 0; i <
m; i++) {
650 for (j = 0; j <
n; j++) {
651 cout <<
x_min[j] <<
" \\le x_{" << j <<
"} \\le " <<
x_max[j] <<
", ";
656 cout <<
"sum = " <<
sum << endl;
659 cout <<
"there is no condition on the sum of x_i" << endl;
668 cout <<
"diophant with m=" <<
m <<
" n=" <<
n << endl;
669 for (i = 0; i <
m; i++) {
673 for (j = 0; j <
n; j++) {
680 cout <<
"sum = " <<
sum << endl;
683 cout <<
"there is no condition on the sum of x_i" << endl;
692 cout <<
"diophant with m=" <<
m <<
" n=" <<
n << endl;
693 for (i = 0; i <
m; i++) {
697 for (j = 0; j <
n; j++) {
698 cout <<
x_min[j] <<
" \\le x_{" << j <<
"} \\le " <<
x_max[j] <<
", ";
703 cout <<
"sum = " <<
sum << endl;
706 cout <<
"there is no condition on the sum of x_i" << endl;
715 for (j = 0; j <
n; j++) {
716 cout << setw(3) <<
Aij(i, j) <<
" ";
718 cout <<
"|" << setw(3) <<
Gij(i, j) <<
" ";
722 cout <<
" = " << setw(3) <<
RHSi(i);
725 cout <<
" <= " << setw(3) <<
RHSi(i);
728 cout <<
" in [" <<
RHS_low_i(i) <<
", " << setw(3) <<
RHSi(i) <<
"]";
731 cout <<
" ZOR " << setw(3) <<
RHSi(i);
744 for (j = 0; j <
n; j++) {
745 if (
Aij(i, j) == 0) {
748 if (
Aij(i, j) == 1) {
749 cout <<
" + x_{" << j <<
"} ";
752 cout <<
" + " << setw(3)
753 <<
Aij(i, j) <<
" * x_{" << j <<
"} ";
768 cout << setw(3) <<
RHSi(i) <<
" ";
779 for (j = 0; j <
n; j++) {
783 cout <<
" = " << setw(3) <<
RHSi(i);
786 cout <<
" <= " << setw(3) <<
RHSi(i);
789 cout <<
" in [" <<
RHS_low_i(i) <<
"," << setw(3) <<
RHSi(i) <<
"]";
792 cout <<
" ZOR " << setw(3) <<
RHSi(i);
805 for (j = 0; j <
n; j++) {
806 cout <<
"x_{" << j <<
"} = " <<
x[j] << endl;
814 cout << setw(5) << header <<
" : ";
815 for (j = 0; j <
n; j++) {
816 cout << setw(3) <<
x[j] <<
" ";
825 for (k = 0; k <
m; k++) {
838 return solve_first_wassermann(verbose_level);
859int diophant::solve_first_wassermann(
int verbose_level)
861 solve_wassermann(verbose_level);
871 int maxresults = INT_MAX;
873 long int nb_backtrack_nodes;
878 cout <<
"diophant::solve_first_mckay "
879 "calling solve_mckay" << endl;
884 solve_mckay(
"", maxresults, nb_backtrack_nodes, nb_sol, verbose_level - 2);
886 cout <<
"diophant::solve_first_mckay found " <<
_resultanz
887 <<
" solutions, using " << nb_backtrack_nodes
888 <<
" backtrack nodes" << endl;
895 for (j = 0; j <
n; j++) {
901 cout <<
"diophant::solve_first_mckay done" << endl;
907void diophant::draw_solutions(std::string &fname_base,
int verbose_level)
909 int f_v = (verbose_level >= 1);
916 cout <<
"diophant::draw_solutions" << endl;
923 for (j = 0; j <
n; j++) {
925 solution[solution_sz++] = j;
929 std::string fname_base2;
931 fname_base2.assign(fname_base);
934 sprintf(str,
"_sol_%d", i);
935 fname_base2.append(str);
946 TRUE , solution, solution_sz,
953 cout <<
"diophant::draw_solutions done" << endl;
960 int f_v = (verbose_level >= 1);
966 cout <<
"diophant::write_solutions" << endl;
970 ofstream fp(fname.c_str());
976 for (j = 0; j <
n; j++) {
988 cout <<
"diophant::write_solutions h != sum" << endl;
990 cout <<
"sum = " <<
sum << endl;
991 cout <<
"h = " << h << endl;
992 cout <<
"res=" << endl;
993 for (j = 0; j <
n; j++) {
994 cout << j <<
" : " << res[j] << endl;
1005 cout <<
"diophant::write_solutions written file " << fname
1006 <<
" of size " << Fio.
file_size(fname) << endl;
1013 int f_v = (verbose_level >= 1);
1019 cout <<
"diophant::read_solutions_from_file" << endl;
1022 cout <<
"diophant::read_solutions_from_file reading file "
1023 << fname_sol <<
" of size " << Fio.
file_size(fname_sol) << endl;
1026 cout <<
"diophant::read_solutions_from_file file "
1027 << fname_sol <<
" does not exist" << endl;
1032 ifstream fp(fname_sol.c_str());
1038 cout <<
"diophant::read_solutions_from_file reading " << N
1039 <<
" solutions of length " <<
sum << endl;
1041 for (i = 0; i < N; i++) {
1043 for (j = 0; j <
n; j++) {
1046 for (h = 0; h <
n; h++) {
1055 cout <<
"diophant::read_solutions_from_file read " <<
_resultanz
1056 <<
" solutions from file " << fname_sol <<
" of size "
1064 int f_v = (verbose_level >= 1);
1069 cout <<
"diophant::get_solutions" << endl;
1071 cout <<
"sum = " <<
sum << endl;
1074 cout <<
"diophant::get_solutions !f_has_sum" << endl;
1082 for (j = 0; j <
n; j++) {
1085 Sol[i *
sum + h] = j;
1090 cout <<
"diophant::get_solutions h != sum" << endl;
1096 cout <<
"diophant::get_solutions done" << endl;
1101 int &nb_sol,
int verbose_level)
1103 int f_v = (verbose_level >= 1);
1108 cout <<
"diophant::get_solutions_full_length" << endl;
1110 cout <<
"sum = " <<
sum << endl;
1114 cout <<
"diophant::get_solutions_full_length !f_has_sum" << endl;
1122 for (j = 0; j <
n; j++) {
1123 Sol[i *
n + j] = res[j];
1128 cout <<
"diophant::get_solutions_full_length done" << endl;
1134 int f_v = (verbose_level >= 1);
1138 cout <<
"diophant::test_solution_full_length" << endl;
1141 for (j = 0; j <
n; j++) {
1144 cout <<
"diophant::test_solution_full_length s=" << s << endl;
1146 cout <<
"diophant::get_solutions_full_length !f_has_sum" << endl;
1150 cout <<
"diophant::test_solution_full_length s != sum" << endl;
1153 for (i = 0; i <
m; i++) {
1155 for (j = 0; j <
n; j++) {
1156 s +=
Aij(i, j) * sol[j];
1158 cout <<
"diophant::test_solution_full_length condition " << i
1159 <<
" / " <<
m <<
":" << endl;
1162 cout <<
"diophant::test_solution_full_length condition "
1163 << i <<
" / " <<
m <<
": s=" << s
1164 <<
" is not equal to " <<
RHSi(i) << endl;
1170 cout <<
"diophant::test_solution_full_length condition "
1171 << i <<
" / " <<
m <<
": s=" << s
1172 <<
" is larger than " <<
RHSi(i) << endl;
1178 cout <<
"diophant::test_solution_full_length condition "
1179 << i <<
" / " <<
m <<
": s=" << s
1180 <<
" is larger than " <<
RHSi(i) << endl;
1184 cout <<
"diophant::test_solution_full_length condition "
1185 << i <<
" / " <<
m <<
": s=" << s
1186 <<
" is less than " <<
RHS_low_i(i) << endl;
1191 if (s != 0 && s !=
RHSi(i)) {
1192 cout <<
"diophant::test_solution_full_length condition "
1193 << i <<
" / " <<
m <<
": s=" << s
1194 <<
" is not equal to " <<
RHSi(i)
1195 <<
" or zero" << endl;
1204 int f_v = (verbose_level >= 1);
1207 cout <<
"diophant::solve_all_DLX verbose_level="
1208 << verbose_level << endl;
1217 for (i = 0; i <
m; i++) {
1218 for (j = 0; j <
n; j++) {
1219 Inc[i *
n + j] =
Aij(i, j);
1240 cout <<
"diophant::solve_all_DLX before Solver.init" << endl;
1242 Solver.
init(&Descr, verbose_level);
1244 cout <<
"diophant::solve_all_DLX after Solver.init" << endl;
1250 cout <<
"diophant::solve_all_DLX before Solver.Solve" << endl;
1252 Solver.
Solve(verbose_level);
1254 cout <<
"diophant::solve_all_DLX after Solver.Solve" << endl;
1259 DlxTransposeAppendAndSolve(Inc,
m,
n, nb_sol, nb_backtrack,
1271 cout <<
"diophant::solve_all_DLX done found " <<
_resultanz
1273 <<
" backtrack steps" << endl;
1280 const char *fname_tree,
int verbose_level)
1282 cout <<
"diophant::solve_all_DLX_with_RHS disabled" << endl;
1284 int f_v = (verbose_level >= 1);
1285 int f_vv = (verbose_level >= 2);
1288 cout <<
"diophant::solve_all_DLX_with_RHS "
1289 "verbose_level=" << verbose_level << endl;
1292 cout <<
"diophant::solve_all_DLX_with_RHS "
1293 "m=" <<
m <<
" n=" <<
n << endl;
1295 install_callback_solution_found(
1296 diophant_callback_solution_found,
1304 int nb_sol, nb_backtrack;
1307 for (i = 0; i <
m; i++) {
1308 for (j = 0; j <
n; j++) {
1309 Inc[i *
n + j] =
Aij(i, j);
1313 cout <<
"diophant::solve_all_DLX_with_RHS "
1314 "the system:" << endl;
1320 for (i = 0; i <
m; i++) {
1322 my_type[i] =
type[i];
1325 cout <<
"diophant::solve_all_DLX_with_RHS RHS:" << endl;
1333 DlxTransposeAndSolveRHS(Inc,
m,
n,
1334 my_RHS, f_has_type, my_type,
1335 nb_sol, nb_backtrack,
1337 f_write_tree, fname_tree,
1345 cout <<
"diophant::solve_all_DLX_with_RHS done found "
1346 <<
_resultanz <<
" solutions with " << nb_backtrack
1347 <<
" backtrack steps" << endl;
1354 int f_write_tree,
const char *fname_tree,
1355 void (*user_callback_solution_found)(
int *sol,
1356 int len,
int nb_sol,
void *data),
1359 cout <<
"diophant::solve_all_DLX_with_RHS_and_callback disabled" << endl;
1362 int f_v = (verbose_level >= 1);
1363 int f_vv = (verbose_level >= 2);
1366 cout <<
"diophant::solve_all_DLX_with_RHS "
1367 "verbose_level=" << verbose_level << endl;
1370 cout <<
"diophant::solve_all_DLX_with_RHS "
1371 "m=" <<
m <<
" n=" <<
n << endl;
1373 diophant_user_callback_solution_found = user_callback_solution_found;
1375 install_callback_solution_found(
1376 diophant_callback_solution_found,
1384 int nb_sol, nb_backtrack;
1387 for (i = 0; i <
m; i++) {
1388 for (j = 0; j <
n; j++) {
1389 Inc[i *
n + j] =
Aij(i, j);
1393 cout <<
"diophant::solve_all_DLX_with_RHS "
1394 "the system:" << endl;
1401 for (i = 0; i <
m; i++) {
1403 my_type[i] =
type[i];
1407 cout <<
"diophant::solve_all_DLX_with_RHS RHS:" << endl;
1415 DlxTransposeAndSolveRHS(Inc,
m,
n,
1416 my_RHS, f_has_type, my_type,
1417 nb_sol, nb_backtrack,
1419 f_write_tree, fname_tree,
1427 cout <<
"diophant::solve_all_DLX_with_RHS done found "
1428 <<
_resultanz <<
" solutions with " << nb_backtrack
1429 <<
" backtrack steps" << endl;
1437 int f_v = (verbose_level >= 1);
1443 cout <<
"diophant::solve_all_mckay before solve_mckay, "
1444 "verbose_level=" << verbose_level << endl;
1447 nb_backtrack_nodes, nb_sol,
1450 cout <<
"diophant::solve_all_mckay found " <<
_resultanz
1451 <<
" solutions in " << nb_backtrack_nodes
1452 <<
" backtrack nodes" << endl;
1459 int f_v = (verbose_level >= 1);
1461 long int nb_backtrack_nodes;
1465 nb_backtrack_nodes, nb_sol, verbose_level - 2);
1467 cout <<
"diophant::solve_once_mckay found " <<
_resultanz
1468 <<
" solutions in " << nb_backtrack_nodes
1469 <<
" backtrack nodes" << endl;
1477 int f_v = (verbose_level >= 1);
1486 for (j = 0; j <
n; j++) {
1493 for (j = 0; j <
n; j++) {
1502 cout <<
"diophant::solve_all_betten found " <<
_resultanz
1509 int f_max_sol,
int max_sol,
1510 int f_max_time,
int max_time_in_seconds)
1512 int f_v = (verbose_level >= 1);
1529 cout <<
"solve_all_betten_with_conditions maxtime "
1536 for (j = 0; j <
n; j++) {
1546 for (j = 0; j <
n; j++) {
1561 cout <<
"diophant::solve_all_betten found " <<
_resultanz
1570 int f_v = (verbose_level >= 1);
1571 int f_vv = (verbose_level >= 2);
1577 cout <<
"diophant::solve_first_betten !f_has_sum" << endl;
1583 cout <<
"diophant::solve_first_betten(): m <= 0" << endl;
1589 for (i = 0; i <
m; i++) {
1593 cout <<
"diophant::solve_first_betten no solution "
1594 "in equation " << i <<
" because n=0 and "
1595 "RHS=" <<
RHS[i] <<
" and not an inequality"
1604 for (i = 0; i <
m; i++) {
1610 for (k = 0; k <
n; k++) {
1611 total_max +=
x_max[k];
1613 if (total_max <
sum) {
1615 cout <<
"diophant::solve_first_betten() total_max "
1616 << total_max <<
" < sum = " <<
sum
1617 <<
", no solution" << endl;
1626 for (i = 0; i <
m; i++) {
1633 for (; j >= 0; j--) {
1642 for (j = 0; j <
n; j++) {
1648 for (j = 0; j <
n; j++) {
1652 cout <<
"diophant::solve_first_betten: gcd computed:" << endl;
1661 cout <<
"diophant::solve_first_betten solution" << endl;
1668 cout <<
"diophant::solve_first_betten nb_steps_betten="
1674 cout <<
"time in seconds: " << t;
1690 f_v = (verbose_level >= 1);
1691 f_vv = (verbose_level >= 2);
1695 cout <<
"diophant::solve_first_betten j=" << j
1696 <<
" sum1=" <<
sum1 <<
" x:" << endl;
1700 if (!
j_fst(j, verbose_level - 2)) {
1708 cout <<
"diophant::solve_first_betten "
1709 "no solution" << endl;
1716 cout <<
"diophant::solve_first_betten nb_steps_betten="
1722 cout <<
"time in seconds: " << t;
1738 f_v = (verbose_level >= 1);
1739 f_vv = (verbose_level >= 2);
1743 cout <<
"diophant::solve_first_betten j=" << j
1744 <<
" sum1=" <<
sum1 <<
" x:" << endl;
1748 if (
j_nxt(j, verbose_level - 2)) {
1760 for (j = 0; j <
n; j++) {
1778 cout <<
"diophant::solve_next_betten !f_has_sum" << endl;
1792 cout <<
"diophant::solve_next_betten nb_steps_betten="
1798 cout <<
"time in seconds: " << t;
1811 if (
j_nxt(j, verbose_level)) {
1826 cout <<
"diophant::solve_next_betten nb_steps_betten="
1832 cout <<
"time in seconds: " << t;
1844 if (!
j_fst(j, verbose_level)) {
1860 int f_v = (verbose_level >= 1);
1861 int f_vv = (verbose_level >= 2);
1874 cout <<
"diophant::j_fst j=" << j <<
" trying x[j]=" <<
x[j] << endl;
1876 for (i = 0; i <
m; i++) {
1897 cout <<
"diophant::j_fst j=" << j <<
" reducing x[j] "
1898 "from " <<
x[j] <<
" to " << b
1899 <<
" because of equation " << i <<
" = "
1901 cout <<
"RHS1[i]=" <<
RHS1[i] << endl;
1902 cout <<
"a=" << a << endl;
1908 cout <<
"diophant::j_fst j=" << j <<
" trying "
1909 "x[j]=" <<
x[j] << endl;
1913 for (i = 0; i <
m; i++) {
1914 RHS1[i] -=
x[j] *
A[i *
n + j];
1916 for (i = 0; i <
m; i++) {
1918 cout <<
"diophant::j_fst(): RHS1[i] < 0" << endl;
1924 cout <<
"diophant::j_fst: x[" << j <<
"] = " <<
x[j] << endl;
1937 for (i = 0; i <
m; i++) {
1945 if (i < m || sum1 > 0) {
1954 for (ii = 0; ii <
m; ii++) {
1955 RHS1[ii] +=
x[j] *
A[ii *
n + j];
1960 cout <<
"diophant::j_fst no solution b/c RHS[i] "
1961 "nonzero || sum1 > 0" << endl;
1962 cout <<
"i=" << i << endl;
1963 cout <<
"j=" << j <<
" = n - 1" << endl;
1964 cout <<
"n=" <<
n << endl;
1965 cout <<
"RHS1[i]=" <<
RHS1[i] << endl;
1966 cout <<
"sum1=" <<
sum1 << endl;
1967 cout <<
"m=" <<
m << endl;
1977 for (i = 0; i <
m; i++) {
1983 if (g == 0 &&
RHS1[i] != 0) {
1991 label = (
char *)
"";
1993 cout <<
"diophant::j_fst g == 0 && RHS1[i] != 0 in "
1994 "eqn i=" << i <<
" = " <<
label << endl;
1995 cout <<
"g=" << g << endl;
1996 cout <<
"i=" << i << endl;
1997 cout <<
"j=" << j <<
" != n - 1" << endl;
1998 cout <<
"n=" <<
n << endl;
1999 cout <<
"RHS1[i]=" <<
RHS1[i] << endl;
2011 if ((
RHS1[i] % g) != 0) {
2019 label = (
char *)
"";
2021 cout <<
"diophant::j_fst (RHS1[i] % g) != 0 in "
2022 "equation i=" << i <<
" = " <<
label << endl;
2023 cout <<
"g=" << g << endl;
2024 cout <<
"i=" << i << endl;
2025 cout <<
"j=" << j <<
" != n - 1" << endl;
2026 cout <<
"n=" <<
n << endl;
2027 cout <<
"RHS1[i]=" <<
RHS1[i] << endl;
2038 cout <<
"gcd test failed !" << endl;
2049 label = (
char *)
"";
2051 cout <<
"diophant::j_fst no solution b/c gcd test "
2052 "failed in equation " << i <<
" = " <<
label << endl;
2053 cout <<
"j=" << j << endl;
2054 cout <<
"x[j]=" <<
x[j] << endl;
2055 cout <<
"RHS1[i]=" <<
RHS1[i] << endl;
2056 cout <<
"Gij(i,j)=" <<
Gij(i,j) << endl;
2063 for (ii = 0; ii <
m; ii++) {
2064 RHS1[ii] +=
A[ii *
n + j];
2067 cout <<
"diophant::j_fst() decrementing to: x[" << j
2068 <<
"] = " <<
x[j] << endl;
2076 int f_v = (verbose_level >= 1);
2077 int f_vv = (verbose_level >= 2);
2084 for (ii = 0; ii <
m; ii++) {
2085 RHS1[ii] +=
x[j] *
A[ii *
n + j];
2090 cout <<
"diophant::j_nxt no solution b/c j == n - 1" << endl;
2091 cout <<
"j=" << j << endl;
2092 cout <<
"n=" <<
n << endl;
2101 cout <<
"diophant::j_nxt() decrementing to: x[" << j
2102 <<
"] = " <<
x[j] << endl;
2105 for (ii = 0; ii <
m; ii++) {
2106 RHS1[ii] +=
A[ii *
n + j];
2110 for (i = 0; i <
m; i++) {
2116 if (g == 0 &&
RHS1[i] != 0) {
2126 if ((
RHS1[i] % g) != 0) {
2141 label = (
char *)
"";
2143 cout <<
"diophant::j_nxt() gcd restriction failed in "
2144 "eqn " << i <<
" = " <<
label << endl;
2148 cout <<
"diophant::j_nxt no solution b/c gcd test failed" << endl;
2149 cout <<
"j=" << j << endl;
2156 long int &nb_backtrack_nodes,
int &nb_sol,
int verbose_level)
2158 int f_v = (verbose_level >= 1);
2161 cout <<
"diophant::solve_mckay" << endl;
2164 maxresults, nb_backtrack_nodes, 0 , nb_sol,
2167 cout <<
"diophant::solve_mckay done" << endl;
2173 int maxresults,
long int &nb_backtrack_nodes,
2174 int minrhs,
int &nb_sol,
int verbose_level)
2176 int f_v = (verbose_level >= 1);
2178 vector<int> minres, maxres, fanz;
2180 vector<mckay::equation> eqn;
2181 map<int, int>::iterator it;
2182 vector<int> minvarvalue;
2183 vector<int> maxvarvalue;
2186 cout <<
"diophant::solve_mckay_override_minrhs_in_inequalities "
2187 <<
label <<
", a system "
2188 "of size " <<
m <<
" x " <<
n << endl;
2201 minres.resize(nb_eqns);
2202 maxres.resize(nb_eqns);
2206 for (i = 0; i <
m; i++) {
2209 minres[i] = (int)
RHS[i];
2210 maxres[i] = (int)
RHS[i];
2213 minres[i] = (int) minrhs;
2214 maxres[i] = (int)
RHS[i];
2218 maxres[i] = (int)
RHS[i];
2221 minres[i] = (int) 0;
2222 maxres[i] = (int)
RHS[i];
2225 cout <<
"diophant::solve_mckay_override_minrhs_in_inequalities "
2226 "we cannot do this type of "
2227 "condition, equation " << i << endl;
2233 for (j = 0; j <
n; j++) {
2243 for (j = 0; j <
n; j++) {
2246 eqn[i][nb].coeff = (int)
A[i *
n + j];
2257 for (j = 0; j <
n; j++) {
2259 eqn[i][j].coeff = 1;
2261 minres[i] = (int)
sum;
2262 maxres[i] = (int)
sum;
2266 minvarvalue.resize(
n);
2267 maxvarvalue.resize(
n);
2270 cout <<
"diophant::solve_mckay_override_minrhs_in_inequalities "
2271 "f_x_max=true" << endl;
2279 for (j = 0; j <
n; j++) {
2280 minvarvalue[j] = (int)
x_min[j];
2281 maxvarvalue[j] = (int)
x_max[j];
2287 cout <<
"diophant::solve_mckay_override_minrhs_in_inequalities "
2288 "f_x_max=false initializing with " << INT_MAX << endl;
2290 for (j = 0; j <
n; j++) {
2292 maxvarvalue[j] = (int) INT_MAX;
2299 for (i = 0; i <
m; i++) {
2301 cout <<
"trying to restrict using of equation " << i << endl;
2303 for (j = 0; j <
n; j++) {
2306 b =
RHS[i] /
A[i *
n + j];
2308 if (b < maxvarvalue[j]) {
2310 cout <<
"lowering maxvarvalue[" << j <<
"] from "
2311 << maxvarvalue[j] <<
" to " << b
2312 <<
" because of equation " << i << endl;
2320 cout <<
"after restrictions:" << endl;
2325 cout <<
"diophant::solve_mckay_override_minrhs_in_inequalities "
2326 "maxvarvalue=" << endl;
2327 for (j = 0; j <
n; j++) {
2328 cout << j <<
" : " << maxvarvalue[j] << endl;
2334 lgs.
possolve(minvarvalue, maxvarvalue,
2335 eqn, minres, maxres, fanz,
2341 cout <<
"diophant::solve_mckay_override_minrhs_in_inequalities "
2342 <<
label <<
" finished, "
2344 <<
" nb_backtrack_nodes=" << nb_backtrack_nodes << endl;
2357 ost <<
"\\begin{array}{|*{" <<
n <<
"}{r}|r|l|}" << endl;
2359 ost <<
"\\hline" << endl;
2361 for (j = 0; j <
n; j++) {
2362 ost << setw(2) << (int)(j / 10) <<
" & ";
2364 ost <<
" & & \\\\" << endl;
2366 for (j = 0; j <
n; j++) {
2367 ost << setw(2) << j % 10 <<
" & ";
2369 ost <<
" & & \\\\" << endl;
2372 for (j = 0; j <
n; j++) {
2373 ost << setw(2) << (int)(
x_max[j] / 10) <<
" & ";
2375 ost <<
" & & \\\\" << endl;
2377 for (j = 0; j <
n; j++) {
2378 ost << setw(2) <<
x_max[j] % 10 <<
" & ";
2380 ost <<
" & & \\\\" << endl;
2382 ost <<
"\\hline" << endl;
2384 ost <<
"\\hline" << endl;
2385 for (i = 0; i <
m; i++) {
2387 for (j = 0; j <
n; j++) {
2389 ost << setw(2) << a <<
" & ";
2392 ost <<
" = " << setw(2) <<
RHS[i] <<
" & ";
2395 ost <<
" \\le " << setw(2) <<
RHS[i] <<
" & ";
2398 ost <<
" \\in [" << setw(2) <<
RHS_low[i] <<
"," << setw(2) <<
RHS[i] <<
"] & ";
2401 ost <<
" ZOR " << setw(2) <<
RHS[i] <<
" & ";
2406 ost <<
"\\\\" << endl;
2408 ost <<
"\\hline" << endl;
2410 ost <<
"\\multicolumn{" <<
n + 2 <<
"}{|c|}{" << endl;
2411 ost <<
"\\mbox{subject to:}" << endl;
2412 ost <<
"}\\\\" << endl;
2413 ost <<
"\\hline" << endl;
2414 ost <<
"\\multicolumn{" <<
n + 2 <<
"}{|l|}{" << endl;
2415 for (j = 0; j <
n; j++) {
2416 ost <<
x_min[j] <<
" \\le x_{" << j + 1 <<
"} \\le " <<
x_max[j] <<
"\\," << endl;
2419 ost <<
"\\sum_{i=1}^{" <<
n <<
"} x_i=" <<
sum << endl;
2421 ost <<
"}\\\\" << endl;
2422 ost <<
"\\hline" << endl;
2424 ost <<
"\\end{array}" << endl;
2428 int &f_no_solution,
int verbose_level)
2430 int f_v = (verbose_level >= 1);
2435 cout <<
"diophant::trivial_row_reductions" << endl;
2438 f_no_solution =
FALSE;
2439 for (i =
m - 1; i >= 0; i--) {
2454 f_no_solution =
FALSE;
2462 cout <<
"diophant::trivial_row_reductions done, eliminated "
2463 << m1 -
m <<
" equations" << endl;
2469 int f_v = (verbose_level >= 1);
2470 int i, j, h, a, rhs;
2476 cout <<
"diophant::trivial_column_reductions" << endl;
2482 for (j = 0; j <
n; j++) {
2485 for (i = 0; i <
m; i++) {
2488 for (j = 0; j <
n; j++) {
2491 f_deleted[j] =
TRUE;
2496 for (j = 0; j <
n; j++) {
2497 cout << f_deleted[j];
2502 for (j = 0; j <
n; j++) {
2512 cout <<
"diophant::trivial_column_reductions nb_deleted = " << nb_deleted << endl;
2522 D2->
open(
m,
n - nb_deleted);
2528 for (i = 0; i <
m; i++) {
2531 for (j = 0; j <
n; j++) {
2536 D2->
Aij(i, h) =
Aij(i, j);
2540 for (j = 0; j <
n; j++) {
2562 for (j = 0; j <
n; j++) {
2572 int *&values,
int *&multiplicities,
int verbose_level)
2574 int f_v = (verbose_level >= 1);
2579 cout <<
"diophant::coefficient_values_in_row" << endl;
2584 for (j = 0; j <
n; j++) {
2588 for (k = nb_values; k > idx; k--) {
2589 values[k] = values[k - 1];
2590 multiplicities[k] = multiplicities[k - 1];
2593 multiplicities[idx] = 1;
2597 multiplicities[idx]++;
2606 int i, d_max = 0, d;
2608 for (i = 0; i <
m; i++) {
2616 int &nb_rows,
int &nb_cols,
int verbose_level)
2618 int f_v = (verbose_level >= 1);
2622 cout <<
"diophant::get_coefficient_matrix" << endl;
2627 for (i = 0; i <
m; i++) {
2628 for (j = 0; j <
n; j++) {
2629 M[i *
n + j] =
Aij(i, j);
2633 cout <<
"diophant::get_coefficient_matrix done" << endl;
2638void diophant::save_as_Levi_graph(std::string &fname,
int verbose_level)
2640 int f_v = (verbose_level >= 1);
2643 cout <<
"diophant::save_as_Levi_graph" << endl;
2649 int nb_rows, nb_cols;
2652 cout <<
"diophant::save_as_Levi_graph before create_Levi_graph_"
2653 "from_coefficient_matrix" << endl;
2660 CG->create_Levi_graph_from_incidence_matrix(
2661 M, nb_rows, nb_cols,
2666 cout <<
"diophant::save_as_Levi_graph after create_Levi_graph_"
2667 "from_coefficient_matrix" << endl;
2669 CG->save(fname, verbose_level);
2674 cout <<
"diophant::save_as_Levi_graph done" << endl;
2680void diophant::save_in_compact_format(
const char *fname,
int verbose_level)
2682 int f_v = (verbose_level >= 1);
2687 cout <<
"diophant::save_in_compact_format" << endl;
2689 for (i = 0; i <
m; i++) {
2690 for (j = 0; j <
n; j++) {
2693 cout <<
"diophant::save_in_compact_format coefficient "
2694 "matrix must be 0/1" << endl;
2702 fp <<
"% " << fname << endl;
2704 for (i = 0; i <
m; i++) {
2707 fp <<
"EQ" <<
" " <<
RHS[i];
2710 fp <<
"LE" <<
" " <<
RHS[i];
2713 fp <<
"INT" <<
" " <<
RHS_low[i] <<
" " <<
RHS[i];
2716 fp <<
"ZOR" <<
" " <<
RHS[i];
2722 for (j = 0; j <
n; j++) {
2730 fp <<
"END" << endl;
2733 cout <<
"diophant::save_in_compact_format done, " << endl;
2734 cout <<
"written file " << fname <<
" of size "
2735 << Fio.file_size(fname) << endl;
2739void diophant::read_compact_format(
const char *fname,
int verbose_level)
2741 int f_v = (verbose_level >= 1);
2743 int cnt, i, d, h, a;
2747 cout <<
"diophant::read_compact_format" << endl;
2755 ifstream myfile (fname);
2756 if (myfile.is_open()) {
2757 getline (myfile, line);
2758 getline (myfile, line);
2762 string str = line.substr(0, i);
2763 string remainder = line.substr(i + 1);
2766 m = atoi(str.c_str());
2768 i = remainder.find(
" ");
2769 str = remainder.substr(0, i);
2771 n = atoi(str.c_str());
2772 string remainder2 = remainder.substr(i + 1);
2774 i = remainder2.find(
" ");
2775 str = remainder2.substr(0, i);
2777 f_has_sum1 = atoi(str.c_str());
2779 str = remainder2.substr(i + 1);
2780 s = atoi(str.c_str());
2789 for (cnt = 0; cnt <
m; cnt++) {
2790 getline (myfile, line);
2792 remainder = line.substr(i + 1);
2796 str = line.substr(0, i);
2797 remainder = line.substr(i + 1);
2799 if (str.compare(
EQ) == 0) {
2803 else if (str.compare(
LE) == 0) {
2807 else if (str.compare(INT) == 0) {
2811 else if (str.compare(ZOR) == 0) {
2816 cout <<
"cannot find EQ or LE or ZOR" << endl;
2821 str = line.substr(0, i);
2822 remainder = line.substr(i + 1);
2824 RHSi(cnt) = atoi(str.c_str());
2831 str = line.substr(0, i);
2832 remainder = line.substr(i + 1);
2834 RHSi(cnt) = atoi(str.c_str());
2838 str = line.substr(0, i);
2839 d = atoi(str.c_str());
2840 remainder = line.substr(i + 1);
2844 for (h = 0; h < d; h++) {
2846 str = line.substr(0, i);
2847 a = atoi(str.c_str());
2848 remainder = line.substr(i + 1);
2858 while ( getline (myfile, line) ) {
2859 cout << line <<
'\n';
2866 cout <<
"Cannot open file " << fname << endl;
2871 cout <<
"diophant::read_compact_format read system with " <<
m
2872 <<
" rows and " <<
n <<
" columns and f_has_sum = " <<
f_has_sum
2873 <<
" and sum " <<
sum << endl;
2882 int f_v = (verbose_level >= 1);
2883 int i, j, a, d, h, val;
2888 cout <<
"diophant::save_in_general_format" << endl;
2892 for (i = 0; i <
m; i++) {
2893 for (j = 0; j <
n; j++) {
2896 cout <<
"diophant::save_in_general_format coefficient "
2897 "matrix must be 0/1" << endl;
2904 std::string fname_coeff;
2905 std::string fname_RHS;
2906 std::string fname_x_bounds;
2908 fname_coeff.assign(fname);
2909 fname_RHS.assign(fname);
2910 fname_x_bounds.assign(fname);
2921 cout <<
"diophant::save_in_general_format written file " << fname_coeff <<
" of size "
2927 for (i = 0; i <
m; i++) {
2928 RHS_coded[3 * i + 0] =
RHS_low[i];
2929 RHS_coded[3 * i + 1] =
RHS[i];
2931 RHS_coded[3 * i + 0] =
RHS[i];
2932 RHS_coded[3 * i + 2] = 1;
2935 RHS_coded[3 * i + 2] = 2;
2938 RHS_coded[3 * i + 2] = 3;
2941 RHS_coded[3 * i + 2] = 4;
2944 cout <<
"type[i] is not recognized" << endl;
2950 cout <<
"diophant::save_in_general_format written file " << fname_RHS <<
" of size "
2957 for (j = 0; j <
n; j++) {
2958 X_bounds[2 * j + 0] =
x_min[j];
2959 X_bounds[2 * j + 1] =
x_max[j];
2965 cout <<
"diophant::save_in_general_format written file " << fname_x_bounds <<
" of size "
2966 << Fio.
file_size(fname_x_bounds) << endl;
2972 fp <<
"% diophantine system in general format " << fname << endl;
2976 for (j = 0; j <
n; j++) {
2981 for (i = 0; i <
m; i++) {
2984 fp <<
"EQ" <<
" " <<
RHS[i];
2987 fp <<
"LE" <<
" " <<
RHS[i];
2990 fp <<
"INT" <<
" " <<
RHS_low[i] <<
" " <<
RHS[i];
2993 fp <<
"ZOR" <<
" " <<
RHS[i];
2998 int *values, *multiplicities;
3003 values, multiplicities, 0 );
3008 fp <<
" " << nb_values;
3009 for (h = 0; h < nb_values; h++) {
3011 d = multiplicities[h];
3015 for (j = 0; j <
n; j++) {
3023 cout <<
"d1 != d" << endl;
3024 cout <<
"i=" << i << endl;
3025 cout <<
"val=" << val << endl;
3026 cout <<
"d=" << d << endl;
3027 cout <<
"d1=" << d1 << endl;
3037 for (j = 0; j <
n; j++) {
3040 fp <<
"END" << endl;
3043 cout <<
"diophant::save_in_general_format done, " << endl;
3044 cout <<
"written file " << fname <<
" of size "
3051 int f_v = (verbose_level >= 1);
3054 int cnt, i, j, d, h, a, nb_types, t, val;
3056 int f_has_var_labels_save =
FALSE;
3059 cout <<
"diophant::read_general_format" << endl;
3067 ifstream myfile (fname);
3068 if (myfile.is_open()) {
3070 cout <<
"diophant::read_general_format" << endl;
3072 getline (myfile, line);
3076 getline (myfile, line);
3080 string str = line.substr(0, i);
3081 string remainder = line.substr(i + 1);
3084 m = atoi(str.c_str());
3086 i = remainder.find(
" ");
3087 str = remainder.substr(0, i);
3089 n = atoi(str.c_str());
3090 string remainder2 = remainder.substr(i + 1);
3092 i = remainder2.find(
" ");
3093 str = remainder2.substr(0, i);
3095 f_has_sum1 = atoi(str.c_str());
3097 string remainder3 = remainder2.substr(i + 1);
3098 i = remainder3.find(
" ");
3099 str = remainder3.substr(0, i);
3100 s = atoi(str.c_str());
3101 string remainder4 = remainder3.substr(i + 1);
3102 f_has_var_labels_save = atoi(remainder4.c_str());
3110 cout <<
"diophant::read_general_format "
3111 "m=" <<
m <<
" n=" <<
n <<
" sum=" << s
3112 <<
" f_has_var_labels=" << f_has_var_labels_save << endl;
3123 cout <<
"reading var labels" << endl;
3126 getline (myfile, line);
3127 for (j = 0; j <
n; j++) {
3131 str = line.substr(0, i);
3133 remainder = line.substr(i + 1);
3139 cout <<
"not reading var labels" << endl;
3143 for (cnt = 0; cnt <
m; cnt++) {
3145 cout <<
"reading equation " << cnt <<
" / " <<
m <<
":" << endl;
3147 getline (myfile, line);
3149 remainder = line.substr(i + 1);
3152 cout <<
"remainder = '" << remainder <<
"'" << endl;
3155 str = line.substr(0, i);
3156 remainder = line.substr(i + 1);
3158 if (str.compare(
EQ) == 0) {
3162 else if (str.compare(
LE) == 0) {
3166 else if (str.compare(INT) == 0) {
3170 else if (str.compare(ZOR) == 0) {
3175 cout <<
"cannot find EQ or LE or ZOR" << endl;
3183 str = line.substr(0, i);
3184 remainder = line.substr(i + 1);
3186 RHSi(cnt) = atoi(str.c_str());
3192 str = line.substr(0, i);
3193 remainder = line.substr(i + 1);
3195 RHSi(cnt) = atoi(str.c_str());
3203 str = line.substr(0, i);
3204 nb_types = atoi(str.c_str());
3205 remainder = line.substr(i + 1);
3208 for (t = 0; t < nb_types; t++) {
3212 str = line.substr(0, i);
3213 val = atoi(str.c_str());
3214 remainder = line.substr(i + 1);
3219 str = line.substr(0, i);
3220 d = atoi(str.c_str());
3221 remainder = line.substr(i + 1);
3227 for (h = 0; h < d; h++) {
3229 str = line.substr(0, i);
3230 a = atoi(str.c_str());
3231 remainder = line.substr(i + 1);
3243 while ( getline (myfile, line) ) {
3244 cout << line <<
'\n';
3250 for (j = 0; j <
n; j++) {
3251 getline (myfile, line);
3255 str = line.substr(0, i);
3256 x_min[j] = atoi(str.c_str());
3257 remainder = line.substr(i + 1);
3259 x_max[j] = atoi(remainder.c_str());
3265 cout <<
"Cannot open file " << fname << endl;
3270 cout <<
"diophant::read_general_format read system with " <<
m
3271 <<
" rows and " <<
n <<
" columns and f_has_sum=" <<
f_has_sum
3272 <<
" sum " <<
sum << endl;
3286 int f_v = (verbose_level >= 1);
3288 int f_delete =
FALSE;
3292 for (i = 0; i <
m; i++) {
3293 for (j = 0; j <
n; j++) {
3310 for (j = 0; j <
n; j++) {
3328 cout <<
"eliminate_zero_rows: eliminated " <<
m - mm
3329 <<
" zero rows" << endl;
3338 for (j = 0; j <
n; j++) {
3339 if (j >= first && j < first + len) {
3350 int *&eqn_number,
int &nb_eqns_replaced,
int *&eqns_replaced,
3356 nb_eqns_replaced = 0;
3358 for (i = 0; i <
m; i++) {
3361 eqns_replaced[nb_eqns_replaced++] = i;
3363 for (j = 0; j < len; j++) {
3364 D->
Aij(i, j) =
Aij(i, first + j);
3378 for (j = 0; j < len; j++) {
3389 int f_solve_case,
int solve_case_idx,
int verbose_level)
3391 int f_v = (verbose_level >= 1);
3394 cout <<
"diophant::split_by_equation" << endl;
3397 long int nb_backtrack_nodes;
3398 int max_results = INT_MAX;
3405 cout <<
"equation " << eqn_idx <<
" has " << a <<
" nonzero coefficients:" << endl;
3411 cout <<
"equation " << eqn_idx <<
" has " << N <<
" solutions." << endl;
3423 cout <<
"Solution " << solve_case_idx <<
" is ";
3424 for (j = 0; j <
n; j++) {
3425 if (Sol[solve_case_idx *
n + j] == 0) {
3432 cout <<
"Solution " << solve_case_idx <<
" is ";
3433 for (j = 0; j <
n; j++) {
3435 cout <<
"x_" << j <<
" = " << Sol[solve_case_idx *
n + j] <<
", ";
3440 for (j = 0; j <
n; j++) {
3442 x_min[j] =
x_max[j] = Sol[solve_case_idx *
n + j];
3445 cout <<
"solving case " << solve_case_idx << endl;
3447 cout <<
"solved case " << solve_case_idx <<
" with " <<
_resultanz <<
" solutions" << endl;
3457 int f_solve_case,
int solve_case_idx_r,
int solve_case_idx_m,
3460 int f_v = (verbose_level >= 1);
3463 cout <<
"diophant::split_by_two_equations" << endl;
3466 long int nb_backtrack_nodes;
3467 int max_results = INT_MAX;
3473 cout <<
"equations (" << eqn1_idx <<
", " << eqn2_idx <<
"):" << endl;
3479 cout <<
"equations (" << eqn1_idx <<
", " << eqn2_idx <<
") have " << N <<
" solutions." << endl;
3491 for (idx = 0; idx < nb_sol; idx++) {
3492 if ((idx % solve_case_idx_m) != solve_case_idx_r) {
3496 cout <<
"Solution " << idx <<
" is ";
3497 for (j = 0; j <
n; j++) {
3498 if (Sol[idx *
n + j] == 0) {
3505 cout <<
"Solution " << idx <<
" is ";
3506 for (j = 0; j <
n; j++) {
3508 cout <<
"x_" << j <<
" = " << Sol[idx *
n + j] <<
", ";
3513 for (j = 0; j <
n; j++) {
3518 cout <<
"solving case " << idx << endl;
3520 cout <<
"solved case " << idx <<
" with " <<
_resultanz <<
" solutions" << endl;
3530 int max_number_of_coefficients,
3533 int f_v = (verbose_level >= 1);
3536 cout <<
"diophant::project_to_single_equation_and_solve" << endl;
3537 cout <<
"max_number_of_coefficients = " << max_number_of_coefficients << endl;
3540 long int *nb_sol_in_eqn;
3541 long int nb_backtrack_nodes;
3542 int max_results = 10000;
3549 for (i = 0; i <
m; i++) {
3552 cout <<
"equation " << i <<
" has " << a <<
" nonzero coefficients" << endl;
3554 if (a < max_number_of_coefficients) {
3558 cout <<
"equation " << i <<
" has " << a <<
" nonzero coefficients:" << endl;
3569 cout <<
"ignoring equation " << i << endl;
3574 cout <<
"number of solutions of individual equations:" << endl;
3575 for (i = 0; i < h; i++) {
3576 cout << setw(3) << eqn_idx[i] <<
" : " << setw(4) << nb_sol_in_eqn[i] << endl;
3582 cout <<
"number of solutions of individual equations classified:" << endl;
3589 cout <<
"diophant::project_to_single_equation_and_solve done" << endl;
3596 int f_v = (verbose_level >= 1);
3600 cout <<
"diophant::project_to_single_equation" << endl;
3605 for (j = 0; j <
n; j++) {
3606 D->
Aij(0, j) =
Aij(eqn_idx, j);
3608 D->
RHS[0] =
RHS[eqn_idx];
3612 for (j = 0; j <
n; j++) {
3616 if (D->
Aij(0, j) == 0) {
3625 cout <<
"diophant::project_to_single_equation done" << endl;
3632 int f_v = (verbose_level >= 1);
3636 cout <<
"diophant::project_to_two_equations" << endl;
3641 for (j = 0; j <
n; j++) {
3642 D->
Aij(0, j) =
Aij(eqn1_idx, j);
3643 D->
Aij(1, j) =
Aij(eqn2_idx, j);
3645 D->
RHS[0] =
RHS[eqn1_idx];
3646 D->
RHS[1] =
RHS[eqn2_idx];
3652 for (j = 0; j <
n; j++) {
3656 if (D->
Aij(0, j) == 0 && D->
Aij(1, j) == 0) {
3665 cout <<
"diophant::project_to_two_equations done" << endl;
3673 for (i = 0; i <
m; i++) {
3675 for (j = 0; j <
n; j++) {
3676 a +=
Aij(i, j) *
x[j];
3687 ost <<
"<DIOPHANT label=\"" <<
label <<
"\" num_eqns=" <<
m
3688 <<
" num_vars=" <<
n <<
" f_has_sum=" <<
f_has_sum
3689 <<
" sum=" <<
sum <<
" >" << endl;
3690 for (i = 0; i <
m; i++) {
3691 for (j = 0;j <
n; j++) {
3692 ost << setw(4) <<
Aij(i, j) <<
" ";
3701 ost << setw(2) << 0 <<
" " << setw(4) <<
RHS[i];
3704 ost << setw(2) << 1 <<
" " << setw(4) <<
RHS[i];
3707 ost << setw(2) << 2 <<
" " << setw(4) <<
RHS_low_i(i) <<
" " << setw(4) <<
RHS[i];
3710 ost << setw(2) << 3 <<
" " << setw(4) <<
RHS[i];
3712 ost <<
" \"" << lbl <<
"\"" << endl;
3714 for (j = 0; j <
n; j++) {
3715 ost << setw(4) <<
x_min[j] <<
" " << setw(4) <<
x_max[j] << endl;
3717 ost <<
"</DIOPHANT>" << endl;
3725 int f_v = (verbose_level >= 1);
3726 string str, mapkey, mapval;
3728 int eqpos, l, M = 0, N = 0, F_has_sum = 0, Sum = 0, i, j, a;
3734 cout <<
"diophant::read_xml" << endl;
3737 f.ignore(INT_MAX,
'<');
3740 if (str !=
"DIOPHANT") {
3741 cout <<
"not a DIOPHANT object: str=" << str << endl;
3746 if (str.substr(str.size() - 1, 1) ==
">") {
3747 str = str.substr(0, str.size() - 1);
3750 eqpos = (int) str.find(
"=");
3752 mapkey = str.substr(0, eqpos);
3753 mapval = str.substr(eqpos + 1, str.size() - eqpos - 1);
3754 if (mapkey ==
"label") {
3755 l = (int) mapval.size();
3756 for (i = 1; i < l; i++) {
3757 label[i - 1] = mapval[i];
3761 else if (mapkey ==
"num_eqns") {
3764 cout <<
"diophant::read_xml num_eqns = " << M << endl;
3767 else if (mapkey ==
"num_vars") {
3770 cout <<
"diophant::read_xml num_vars = " << N << endl;
3773 else if (mapkey ==
"f_has_sum") {
3774 F_has_sum = ST.
str2int(mapval);
3776 cout <<
"diophant::read_xml F_has_sum = " << F_has_sum << endl;
3779 else if (mapkey ==
"sum") {
3782 cout <<
"diophant::read_xml Sum = " << Sum << endl;
3786 brk = brk || f.eof();
3789 cout <<
"diophant::read_xml M=" << M <<
" N=" << N << endl;
3794 for (i = 0; i <
m; i++) {
3795 for (j = 0; j <
n; j++) {
3837 for (j = 0; j < l; j++) {
3843 cout <<
"diophant::read_xml reading x_max[]" << endl;
3845 for (j = 0; j <
n; j++) {
3848 cout <<
"diophant::read_xml reading x_max[" << j <<
"]="
3849 <<
x_max[j] << endl;
3853 cout <<
"diophant::read_xml read the following file:" << endl;
3858 cout <<
"diophant::read_xml has a problem under windows"<< endl;
3865 int *AA, *R_low, *R, *R1, *Y1;
3879 for (i = 0; i <
m; i++) {
3881 for (j = 0; j <
n; j++) {
3882 AA[i *
n + j] =
Aij(i, j);
3926 for (i = I; i <
m - 1; i++) {
3932 for (j = 0; j <
n; j++) {
3933 Aij(i, j) =
Aij(i + 1, j);
3946 f <<
"Maximize" << endl;
3948 for (j = 0; j <
n; j++) {
3952 f <<
" Subject to" << endl;
3953 for (i = 0; i <
m; i++) {
3955 for (j = 0; j <
n; j++) {
3960 f <<
" + " << a <<
" x"<< j;
3962 f <<
" = " <<
RHSi(i) << endl;
3964 f <<
"Binary" << endl;
3965 for (i = 0; i <
n; i++) {
3966 f <<
"x" << i << endl;
3970 cout <<
"Written file " << fname <<
" of size "
3975 int f_box_width,
int box_width,
int bit_depth,
int verbose_level)
3977 int f_v = (verbose_level >= 1);
3980 cout <<
"diophant::draw_as_bitmap" << endl;
3989 for (i = 0; i < m1; i++) {
3990 for (j = 0; j < n1; j++) {
4015 Draw_bitmap_control.
M = M;
4016 Draw_bitmap_control.
m = m1;
4017 Draw_bitmap_control.
n = n1;
4019 Draw_bitmap_control.
box_width = box_width;
4020 Draw_bitmap_control.
bit_depth = bit_depth;
4024 GO.
draw_bitmap(&Draw_bitmap_control, verbose_level);
4027 draw_bitmap(fname, M, m1, n1,
4030 f_box_width, box_width,
4038 cout <<
"diophant::draw_as_bitmap done" << endl;
4044 std::string &fname_base,
4049 int f_partition =
FALSE;
4050 int f_bitmatrix =
FALSE;
4051 int f_row_grid =
FALSE;
4052 int f_col_grid =
FALSE;
4060 f_partition, 0, NULL, 0, NULL,
4061 f_row_grid, f_col_grid,
4062 f_bitmatrix, NULL,
A,
4069 std::string &fname_base,
4071 int f_solution,
int *solution,
int solution_sz,
4074 int f_v = (verbose_level >= 1);
4076 int f_bitmatrix =
FALSE;
4083 cout <<
"diophant::draw_partitioned" << endl;
4094 for (i = 0; i <
m; i++) {
4113 cout <<
"diophant::draw_partitioned we found " << C.
nb_types
4114 <<
" classes according to type[]" << endl;
4120 int size_complement;
4137 part_col[1] =
n - solution_sz;
4140 Int_vec_copy(solution, col_perm +
n - solution_sz, solution_sz);
4141 Combi.
set_complement(solution, solution_sz, col_perm, size_complement,
n);
4143 if (size_complement !=
n - solution_sz) {
4144 cout <<
"diophant::draw_partitioned size_complement "
4145 "!= n - solution_sz" << endl;
4154 for (j = 0; j <
n; j++) {
4162 cout <<
"row_perm:";
4167 cout <<
"col_perm:";
4168 int_vec_print(cout, col_perm,
n);
4174 for (i = 0; i <
m; i++) {
4177 for (j = 0; j <
n; j++) {
4184 cout <<
"diophant::draw_partitioned A2=" << endl;
4188 int f_row_grid =
FALSE;
4189 int f_col_grid =
FALSE;
4197 f_row_grid, f_col_grid,
4200 FALSE, NULL, verbose_level - 1);
4209 cout <<
"diophant::draw_partitioned done" << endl;
4215 int f_v = (verbose_level >= 1);
4216 int i, j, b, c, ret;
4219 cout <<
"diophant::test_solution" << endl;
4233 for (j = 0; j < len; j++) {
4236 for (i = 0; i <
m; i++) {
4238 for (j = 0; j <
n; j++) {
4239 c =
Aij(i, j) *
X[j];
4250 for (i = 0; i <
m; i++) {
4252 if (
Y[i] !=
RHS[i]) {
4253 cout <<
"diophant::test_solution t_EQ and Y[i] != RHS[i]" << endl;
4259 if (
Y[i] >
RHS[i]) {
4260 cout <<
"diophant::test_solution t_LE and Y[i] > RHS[i]" << endl;
4267 if (
Y[i] >
RHS[i]) {
4268 cout <<
"diophant::test_solution t_INT and Y[i] > RHS[i]" << endl;
4273 cout <<
"diophant::test_solution t_INT and Y[i] < RHS_low[i]" << endl;
4280 if (
Y[i] != 0 &&
Y[i] !=
RHS[i]) {
4286 cout <<
"diophant::test_solution unknown type" << endl;
4294 cout <<
"diophant::test_solution done" << endl;
4303 int f_v = (verbose_level >= 1);
4307 cout <<
"diophant::get_columns" << endl;
4312 for (h = 0; h < nb_col; h++) {
4315 for (i = 0; i <
m; i++) {
4323 for (i = 0; i <
m; i++) {
4335 int f_v = (verbose_level >= 1);
4336 int f_vv = (verbose_level >= 2);
4338 int nb_sol, sol_length;
4343 cout <<
"diophant::test_solution_file" << endl;
4347 for (i = 0; i < nb_sol; i++) {
4349 cout <<
"diophant::test_solution_file testing solution "
4350 << i <<
" / " << nb_sol <<
":" << endl;
4352 if (!
test_solution(Sol + i * sol_length, sol_length, verbose_level)) {
4353 cout <<
"solution " << i <<
" / " << nb_sol <<
" is bad" << endl;
4356 cout <<
"solution " << i <<
" / " << nb_sol <<
" is OK" << endl;
4365 cout <<
"classification: ";
4370 cout <<
"diophant::test_solution_file done" << endl;
4376 int f_v = (verbose_level >= 1);
4380 cout <<
"diophant::analyze" << endl;
4383 for (i = 0; i <
m; i++) {
4387 int *values, *multiplicities;
4391 values, multiplicities, 0 );
4394 cout <<
"row " << i <<
": ";
4395 for (h = 0; h < nb_values; h++) {
4397 d = multiplicities[h];
4398 cout << val <<
"^" << d;
4399 if (h < nb_values - 1) {
4411 cout <<
"diophant::analyze done" << endl;
4419 for (i = 0; i <
m; i++) {
4433 int f_v = (verbose_level >= 1);
4434 int j1, j2, L, k, i;
4438 cout <<
"diophant::make_clique_graph_adjacency_matrix" << endl;
4442 cout <<
"diophant::make_clique_graph_adjacency_matrix "
4443 "the system is not of Steiner type" << endl;
4447 L = (
n * (
n - 1)) >> 1;
4450 Adj = bitvector_allocate(L);
4451 for (i = 0; i < L; i++) {
4452 bitvector_m_ii(Adj, i, 1);
4457 for (i = 0; i <
m; i++) {
4458 for (j1 = 0; j1 <
n; j1++) {
4459 if (
Aij(i, j1) == 0) {
4462 for (j2 = j1 + 1; j2 <
n; j2++) {
4463 if (
Aij(i, j2) == 0) {
4467 k = Combi.
ij2k(j1, j2,
n);
4473 cout <<
"diophant::make_clique_graph_adjacency_matrix done" << endl;
4480 int f_v = (verbose_level >= 1);
4484 cout <<
"diophant::make_clique_graph" << endl;
4491 string label, label_tex;
4493 label.assign(
"clique_graph");
4494 label_tex.assign(
"clique\\_graph");
4500 cout <<
"diophant::make_clique_graph" << endl;
4505 std::string &clique_graph_fname,
int verbose_level)
4507 int f_v = (verbose_level >= 1);
4510 cout <<
"diophant::make_clique_graph_and_save" << endl;
4516 CG->
save(clique_graph_fname, verbose_level - 1);
4520 cout <<
"diophant::make_clique_graph_and_save done" << endl;
4533 for (i = 0; i < l - 1; i++) {
4535 for (j = 0; j <
n; j++) {
4536 if (cur[j] != last[j]) {
4541 cout <<
"The last solution " << l - 1
4542 <<
" is the same as solution " << i << endl;
4552 int f_once,
int verbose_level)
4556 int maxresults = 10000000;
4558 long int nb_backtrack_nodes;
4563 cout <<
"diophant::solve_first_mckay calling solve_mckay" << endl;
4570 maxresults, nb_backtrack_nodes, nb_sol,
4574 cout <<
"diophant::solve_first_mckay found " <<
_resultanz
4575 <<
" solutions, using " << nb_backtrack_nodes
4576 <<
" backtrack nodes" << endl;
4583 for (j = 0; j <
n; j++) {
4589 cout <<
"diophant::solve_first_mckay done" << endl;
4601static void diophant_callback_solution_found(
int *sol,
int len,
4602 int nb_sol,
void *data)
4609 if ((nb_sol % 1000) == 0) {
4613 cout <<
"diophant_callback_solution_found recording solution "
4614 << nb_sol <<
" len = " << len <<
" : ";
4617 cout <<
"D->_resultanz=" << D->
_resultanz << endl;
4619 for (i = 0; i < len; i++) {
4620 cout << 0 <<
"/" << sol[i];
4629 cout <<
"diophant_callback_solution_found the solution "
4630 "is not a solution" << endl;
4636 cout <<
"diophant_callback_solution_found D->test_solution "
4637 "returns TRUE" << endl;
4643 for (j = 0; j < D->
n; j++) {
4646 for (j = 0; j < len; j++) {
4652 if (diophant_user_callback_solution_found) {
4653 (*diophant_user_callback_solution_found)(sol, len, nb_sol, data);
a collection of combinatorial functions
void set_complement(int *subset, int subset_size, int *complement, int &size_complement, int universal_set_size)
int ij2k(int i, int j, int n)
compact storage of 0/1-data as bitvectors
void m_i(long int i, int a)
void allocate(long int length)
void matrix_print(int *p, int m, int n)
void init_simple(int underlying_set_size, int nb_sets, int verbose_level)
a collection of functions related to sorted vectors
int int_vec_search(int *v, int len, int a, int &idx)
a statistical analysis of data consisting of single integers
void init(int *data, int data_length, int f_second, int verbose_level)
void print(int f_backwards)
void init_lint(long int *data, int data_length, int f_second, int verbose_level)
void print_naked(int f_backwards)
a graph with a vertex coloring
void init_no_colors(int nb_points, data_structures::bitvector *Bitvec, int f_ownership_of_bitvec, std::string &label, std::string &label_tex, int verbose_level)
void save(std::string &fname, int verbose_level)
various functions related to graph theory
void draw_bitmatrix(std::string &fname_base, graphics::layered_graph_draw_options *Draw_options, int f_dots, int f_partition, int nb_row_parts, int *row_part_first, int nb_col_parts, int *col_part_first, int f_row_grid, int f_col_grid, int f_bitmatrix, data_structures::bitmatrix *Bitmatrix, int *M, int m, int n, int f_has_labels, int *labels, int verbose_level)
options for drawing bitmap files
a catch-all class for things related to 2D graphics
void draw_bitmap(draw_bitmap_control *C, int verbose_level)
options for drawing an object of type layered_graph
basic number theoretic functions
long int gcd_lint(long int m, long int n)
a collection of functions related to file io
void int_matrix_write_csv(std::string &fname, int *M, int m, int n)
long int file_size(std::string &fname)
void int_matrix_read_text(std::string &fname, int *&M, int &m, int &n)
data_structures::int_vec * Int_vec
interface to system functions
int os_ticks_per_second()
diophantine systems of equations (i.e., linear systems over the integers)
void make_clique_graph_adjacency_matrix(data_structures::bitvector *&Adj, int verbose_level)
void get_solutions(long int *&Sol, int &nb_sol, int verbose_level)
int solve_all_DLX(int verbose_level)
void trivial_row_reductions(int &f_no_solution, int verbose_level)
void make_clique_graph(graph_theory::colored_graph *&CG, int verbose_level)
int count_non_zero_coefficients_in_row(int i)
void print2(int f_with_gcd)
void init_problem_of_Steiner_type_with_RHS(int nb_rows, int nb_cols, int *Inc, int nb_to_select, int *Rhs, int verbose_level)
void init_eqn_label(int i, char *label)
int solve_all_DLX_with_RHS_and_callback(int f_write_tree, const char *fname_tree, void(*user_callback_solution_found)(int *sol, int len, int nb_sol, void *data), int verbose_level)
int maximum_number_of_non_zero_coefficients_in_row()
void read_xml(std::ifstream &f, char *label, int verbose_level)
int solve_next_mckay(int verbose_level)
void init_RHS(int RHS_value, int verbose_level)
void coefficient_values_in_row(int i, int &nb_values, int *&values, int *&multiplicities, int verbose_level)
void project_to_two_equations(diophant *D, int eqn1_idx, int eqn2_idx, int verbose_level)
int solve_first_mckay(int f_once, int verbose_level)
int solve_next_betten(int verbose_level)
int test_solution(int *sol, int len, int verbose_level)
int is_zero_outside(int first, int len, int i)
void fill_coefficient_matrix_with(int a)
int solve_once_mckay(int verbose_level)
void project_to_single_equation(diophant *D, int eqn_idx, int verbose_level)
void print_eqn(int i, int f_with_gcd)
int solve_all_betten_with_conditions(int verbose_level, int f_max_sol, int max_sol, int f_max_time, int max_time_in_seconds)
int f_broken_off_because_of_maxtime
void delete_equation(int I)
void set_x_max_constant(int a)
diophant_equation_type * type
int j_fst(int j, int verbose_level)
void multiply_A_x_to_RHS1()
void draw_partitioned(std::string &fname_base, graphics::layered_graph_draw_options *Draw_options, int f_solution, int *solution, int solution_sz, int verbose_level)
void read_solutions_from_file(std::string &fname_sol, int verbose_level)
int solve_all_mckay(long int &nb_backtrack_nodes, int maxresults, int verbose_level)
int solve_first_mckay_once_option(int f_once, int verbose_level)
void init_clique_finding_problem(int *Adj, int nb_pts, int nb_to_select, int verbose_level)
void solve_mckay(const char *label, int maxresults, long int &nb_backtrack_nodes, int &nb_sol, int verbose_level)
int solve_all_DLX_with_RHS(int f_write_tree, const char *fname_tree, int verbose_level)
void init_problem_of_Steiner_type(int nb_rows, int nb_cols, int *Inc, int nb_to_select, int verbose_level)
void write_solutions(std::string &fname, int verbose_level)
void project(diophant *D, int first, int len, int *&eqn_number, int &nb_eqns_replaced, int *&eqns_replaced, int verbose_level)
void get_solutions_full_length(int *&Sol, int &nb_sol, int verbose_level)
void make_clique_graph_and_save(std::string &clique_graph_fname, int verbose_level)
void draw_as_bitmap(std::string &fname, int f_box_width, int box_width, int bit_depth, int verbose_level)
void write_xml(std::ostream &ost, const char *label)
void init_partition_problem_with_bounds(int *weights, int *bounds, int nb_weights, int target_value, int verbose_level)
void split_by_two_equations(int eqn1_idx, int eqn2_idx, int f_solve_case, int solve_case_idx_r, int solve_case_idx_m, int verbose_level)
void project_to_single_equation_and_solve(int max_number_of_coefficients, int verbose_level)
int j_nxt(int j, int verbose_level)
void solve_mckay_override_minrhs_in_inequalities(const char *label, int maxresults, long int &nb_backtrack_nodes, int minrhs, int &nb_sol, int verbose_level)
void eliminate_zero_rows_quick(int verbose_level)
void test_if_the_last_solution_is_unique()
void analyze(int verbose_level)
int solve_all_betten(int verbose_level)
void get_coefficient_matrix(int *&M, int &nb_rows, int &nb_cols, int verbose_level)
void eliminate_zero_rows(int *&eqn_number, int verbose_level)
void print_eqn_compressed(int i)
void join_problems(diophant *D1, diophant *D2, int verbose_level)
void draw_it(std::string &fname_base, graphics::layered_graph_draw_options *Draw_options, int verbose_level)
int solve_first_betten(int verbose_level)
void test_solution_full_length(int *sol, int verbose_level)
std::deque< std::vector< int > > _results
void init_partition_problem(int *weights, int nb_weights, int target_value, int verbose_level)
void read_general_format(std::string &fname, int verbose_level)
void get_columns(int *col, int nb_col, data_structures::set_of_sets *&S, int verbose_level)
void write_gurobi_binary_variables(const char *fname)
int solve_first(int verbose_level)
void print_eqn_dense(int i)
void test_solution_file(std::string &solution_file, int verbose_level)
void set_x_min_constant(int a)
void split_by_equation(int eqn_idx, int f_solve_case, int solve_case_idx, int verbose_level)
diophant * trivial_column_reductions(int verbose_level)
void init_var_labels(long int *labels, int verbose_level)
void save_in_general_format(std::string &fname, int verbose_level)
description of a problem instance for dancing links solver
An implementation of Donald Knuth's dancing links algorithm to solve exact cover problems.
void init(dlx_problem_description *Descr, int verbose_level)
void install_callback_solution_found(void(*callback_solution_found)(int *solution, int len, int nb_sol, void *data), void *callback_solution_found_data)
void Solve(int verbose_level)
solves diophantine systems according to McKay
void possolve(std::vector< int > &lo, std::vector< int > &hi, std::vector< equation > &eqn, std::vector< int > &lorhs, std::vector< int > &hirhs, std::vector< int > &neqn, int numeqn, int numvar, int verbose_level)
void Init(diophant *lgs, const char *label, int aEqnAnz, int aVarAnz)
long int nb_calls_to_solve
#define Int_vec_zero(A, B)
#define Int_vec_print_fully(A, B, C)
#define Int_matrix_print(A, B, C)
#define NEW_OBJECTS(type, n)
#define Lint_vec_copy_to_int(A, B, C)
#define Int_vec_copy(A, B, C)
#define Int_vec_print(A, B, C)
orbiter_kernel_system::orbiter_session * Orbiter
global Orbiter session
the orbiter library for the classification of combinatorial objects