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 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753
|
/*
* Normaliz
* Copyright (C) 2007-2014 Winfried Bruns, Bogdan Ichim, Christof Soeger
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* As an exception, when this program is distributed through (i) the App Store
* by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or (iii) Google Play
* by Google Inc., then that store may impose any digital rights management,
* device limits and/or redistribution restrictions that are required by its
* terms of service.
*/
//---------------------------------------------------------------------------
#include <stdlib.h>
#include <set>
#include <map>
#include <iostream>
#include <string>
#include <algorithm>
#include <time.h>
#include <deque>
#include "libnormaliz/full_cone.h"
#include "libnormaliz/project_and_lift.h"
#include "libnormaliz/vector_operations.h"
#include "libnormaliz/list_operations.h"
#include "libnormaliz/map_operations.h"
#include "libnormaliz/my_omp.h"
#include "libnormaliz/integer.h"
#include "libnormaliz/sublattice_representation.h"
#include "libnormaliz/offload_handler.h"
//---------------------------------------------------------------------------
// const size_t RecBoundTriang=1000000; // if number(supphyps)*size(triang) > RecBoundTriang
// we pass to (non-recirsive) pyramids
// now in build_cone
const size_t EvalBoundTriang=2500000; // if more than EvalBoundTriang simplices have been stored
// evaluation is started (whenever possible)
const size_t EvalBoundPyr=200000; // the same for stored pyramids of level > 0
const size_t EvalBoundLevel0Pyr=200000; // 1000000; // the same for stored level 0 pyramids
// const size_t EvalBoundRecPyr=200000; // the same for stored RECURSIVE pyramids
// const size_t IntermedRedBoundHB=2000000; // bound for number of HB elements before
// intermediate reduction is called
const int largePyramidFactor=20; // pyramid is large if largePyramidFactor*Comparisons[Pyramid_key.size()-dim] > old_nr_supp_hyps
const int SuppHypRecursionFactor=200; // pyramids for supphyps formed if Pos*Neg > ...
const size_t RAM_Size=1000000000; // we assume that there is at least 1 GB of RAM
const long GMP_time_factor=10; // factor by which GMP arithmetic differs from long long
//---------------------------------------------------------------------------
namespace libnormaliz {
using namespace std;
//---------------------------------------------------------------------------
//private
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::check_simpliciality_hyperplane(const FACETDATA& hyp) const{
size_t nr_gen_in_hyp=0;
for(size_t i=0; i<nr_gen;++i)
if(in_triang[i]&& hyp.GenInHyp.test(i))
nr_gen_in_hyp++;
if((hyp.simplicial && nr_gen_in_hyp!=dim-2) || (!hyp.simplicial && nr_gen_in_hyp==dim-2)){
// NOTE: in_triang set at END of main loop in build_cone
errorOutput() << "Simplicial " << hyp.simplicial << " dim " << dim << " gen_in_hyp " << nr_gen_in_hyp << endl;
assert(false);
}
}
template<typename Integer>
void Full_Cone<Integer>::set_simplicial(FACETDATA& hyp){
size_t nr_gen_in_hyp=0;
for(size_t i=0; i<nr_gen;++i)
if(in_triang[i]&& hyp.GenInHyp.test(i))
nr_gen_in_hyp++;
hyp.simplicial=(nr_gen_in_hyp==dim-2);
}
template<typename Integer>
void Full_Cone<Integer>::number_hyperplane(FACETDATA& hyp, const size_t born_at, const size_t mother){
// add identifying number, the birth day and the number of mother
hyp.Mother=mother;
hyp.BornAt=born_at;
if(!multithreaded_pyramid){
hyp.Ident=HypCounter[0];
HypCounter[0]++;
return;
}
int tn;
if(omp_get_level()==omp_start_level)
tn=0;
else
tn = omp_get_ancestor_thread_num(omp_start_level+1);
hyp.Ident=HypCounter[tn];
HypCounter[tn]+=omp_get_max_threads();
}
//---------------------------------------------------------------------------
template<typename Integer>
bool Full_Cone<Integer>::is_hyperplane_included(FACETDATA& hyp) {
if (!is_pyramid) { // in the topcone we always have ov_sp > 0
return true;
}
//check if it would be an excluded hyperplane
Integer ov_sp = v_scalar_product(hyp.Hyp,Order_Vector);
if (ov_sp > 0) {
return true;
} else if (ov_sp == 0) {
for (size_t i=0; i<dim; i++) {
if (hyp.Hyp[i]>0) {
return true;
} else if (hyp.Hyp[i]<0) {
return false;
}
}
}
return false;
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::add_hyperplane(const size_t& new_generator, const FACETDATA & positive,const FACETDATA & negative,
list<FACETDATA>& NewHyps, bool known_to_be_simplicial){
// adds a new hyperplane found in find_new_facets to this cone (restricted to generators processed)
size_t k;
FACETDATA NewFacet; NewFacet.Hyp.resize(dim); NewFacet.GenInHyp.resize(nr_gen);
NewFacet.is_positive_on_all_original_gens=false;
NewFacet.is_negative_on_some_original_gen=false;
for (k = 0; k <dim; k++) {
NewFacet.Hyp[k]=positive.ValNewGen*negative.Hyp[k]-negative.ValNewGen*positive.Hyp[k];
if(!check_range(NewFacet.Hyp[k]))
break;
}
if(k==dim)
v_make_prime(NewFacet.Hyp);
else{
#pragma omp atomic
GMP_hyp++;
vector<mpz_class> mpz_neg(dim), mpz_pos(dim), mpz_sum(dim);
convert(mpz_neg, negative.Hyp);
convert(mpz_pos, positive.Hyp);
for (k = 0; k <dim; k++)
mpz_sum[k]=convertTo<mpz_class>(positive.ValNewGen)*mpz_neg[k]-convertTo<mpz_class>(negative.ValNewGen)*mpz_pos[k];
v_make_prime(mpz_sum);
convert(NewFacet.Hyp, mpz_sum);
}
NewFacet.ValNewGen=0;
NewFacet.GenInHyp=positive.GenInHyp & negative.GenInHyp; // new hyperplane contains old gen iff both pos and neg do
if(known_to_be_simplicial){
NewFacet.simplicial=true;
check_simpliciality_hyperplane(NewFacet);
}
else
set_simplicial(NewFacet);
NewFacet.GenInHyp.set(new_generator); // new hyperplane contains new generator
number_hyperplane(NewFacet,nrGensInCone,positive.Ident);
NewHyps.push_back(NewFacet);
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::find_new_facets(const size_t& new_generator){
// our Fourier-Motzkin implementation
// the special treatment of simplicial facets was inserted because of line shellings.
// At present these are not computed.
//to see if possible to replace the function .end with constant iterator since push-back is performed.
// for dimension 0 and 1 F-M is never necessary and can lead to problems
// when using dim-2
if (dim <= 1)
return;
// NEW: new_generator is the index of the generator being inserted
size_t i,k,nr_zero_i;
size_t subfacet_dim=dim-2; // NEW dimension of subfacet
size_t facet_dim=dim-1; // NEW dimension of facet
const bool tv_verbose = false; //verbose && !is_pyramid; // && Support_Hyperplanes.nr_of_rows()>10000; //verbose in this method call
// preparing the computations, the various types of facets are sorted into the deques
deque <FACETDATA*> Pos_Simp,Pos_Non_Simp;
deque <FACETDATA*> Neg_Simp,Neg_Non_Simp;
deque <FACETDATA*> Neutral_Simp, Neutral_Non_Simp;
boost::dynamic_bitset<> Zero_Positive(nr_gen),Zero_Negative(nr_gen); // here we collect the vertices that lie in a
// postive resp. negative hyperplane
bool simplex;
if (tv_verbose) verboseOutput()<<"transform_values:"<<flush;
typename list<FACETDATA>::iterator ii = Facets.begin();
for (; ii != Facets.end(); ++ii) {
// simplex=true;
// nr_zero_i=0;
simplex=ii->simplicial; // at present simplicial, will become nonsimplicial if neutral
/* for (size_t j=0; j<nr_gen; j++){
if (ii->GenInHyp.test(j)) {
if (++nr_zero_i > facet_dim) {
simplex=false;
break;
}
}
}*/
if (ii->ValNewGen==0) {
ii->GenInHyp.set(new_generator); // Must be set explicitly !!
ii->simplicial=false; // simpliciality definitly gone with the new generator
if (simplex) {
Neutral_Simp.push_back(&(*ii)); // simplicial without the new generator
} else {
Neutral_Non_Simp.push_back(&(*ii)); // nonsimplicial already without the new generator
}
}
else if (ii->ValNewGen>0) {
Zero_Positive |= ii->GenInHyp;
if (simplex) {
Pos_Simp.push_back(&(*ii));
} else {
Pos_Non_Simp.push_back(&(*ii));
}
}
else if (ii->ValNewGen<0) {
Zero_Negative |= ii->GenInHyp;
if (simplex) {
Neg_Simp.push_back(&(*ii));
} else {
Neg_Non_Simp.push_back(&(*ii));
}
}
}
// TO DO: Negativliste mit Zero_Positive verfeinern, also die aussondern, die nicht genug positive Erz enthalten
// Eventuell sogar Rang-Test einbauen.
// Letzteres könnte man auch bei den positiven machen, bevor sie verarbeitet werden
boost::dynamic_bitset<> Zero_PN(nr_gen);
Zero_PN = Zero_Positive & Zero_Negative;
size_t nr_PosSimp = Pos_Simp.size();
size_t nr_PosNonSimp = Pos_Non_Simp.size();
size_t nr_NegSimp = Neg_Simp.size();
size_t nr_NegNonSimp = Neg_Non_Simp.size();
size_t nr_NeuSimp = Neutral_Simp.size();
size_t nr_NeuNonSimp = Neutral_Non_Simp.size();
if (tv_verbose) verboseOutput()<<" PS "<<nr_PosSimp<<", P "<<nr_PosNonSimp<<", NS "<<nr_NegSimp<<", N "<<nr_NegNonSimp<<", ZS "<<nr_NeuSimp<<", Z "<<nr_NeuNonSimp<<endl;
if (tv_verbose) verboseOutput()<<"transform_values: subfacet of NS: "<<flush;
vector< list<pair < boost::dynamic_bitset<>, int> > > Neg_Subfacet_Multi(omp_get_max_threads()) ;
boost::dynamic_bitset<> zero_i, subfacet;
// This parallel region cannot throw a NormalizException
#pragma omp parallel for private(zero_i,subfacet,k,nr_zero_i)
for (i=0; i<nr_NegSimp;i++){
zero_i=Zero_PN & Neg_Simp[i]->GenInHyp;
nr_zero_i=0;
for(size_t j=0;j<nr_gen;j++){
if(zero_i.test(j))
nr_zero_i++;
if(nr_zero_i>subfacet_dim){
break;
}
}
if(nr_zero_i==subfacet_dim) // NEW This case treated separately
Neg_Subfacet_Multi[omp_get_thread_num()].push_back(pair <boost::dynamic_bitset<>, int> (zero_i,i));
if(nr_zero_i==facet_dim){
for (k =0; k<nr_gen; k++) {
if(zero_i.test(k)) {
subfacet=zero_i;
subfacet.reset(k); // remove k-th element from facet to obtain subfacet
Neg_Subfacet_Multi[omp_get_thread_num()].push_back(pair <boost::dynamic_bitset<>, int> (subfacet,i));
}
}
}
}
list < pair < boost::dynamic_bitset<>, int> > Neg_Subfacet_Multi_United;
for(int i=0;i<omp_get_max_threads();++i)
Neg_Subfacet_Multi_United.splice(Neg_Subfacet_Multi_United.begin(),Neg_Subfacet_Multi[i]);
Neg_Subfacet_Multi_United.sort();
if (tv_verbose) verboseOutput()<<Neg_Subfacet_Multi_United.size() << ", " << flush;
list< pair < boost::dynamic_bitset<>, int > >::iterator jj;
list< pair < boost::dynamic_bitset<>, int > >::iterator del;
jj =Neg_Subfacet_Multi_United.begin(); // remove negative subfacets shared
while (jj!= Neg_Subfacet_Multi_United.end()) { // by two neg simpl facets
del=jj++;
if (jj!=Neg_Subfacet_Multi_United.end() && (*jj).first==(*del).first) { //delete since is the intersection of two negative simplicies
Neg_Subfacet_Multi_United.erase(del);
del=jj++;
Neg_Subfacet_Multi_United.erase(del);
}
}
size_t nr_NegSubfMult = Neg_Subfacet_Multi_United.size();
if (tv_verbose) verboseOutput() << nr_NegSubfMult << ", " << flush;
vector<list<FACETDATA> > NewHypsSimp(nr_PosSimp);
vector<list<FACETDATA> > NewHypsNonSimp(nr_PosNonSimp);
map < boost::dynamic_bitset<>, int > Neg_Subfacet;
size_t nr_NegSubf=0;
// size_t NrMatches=0, NrCSF=0, NrRank=0, NrComp=0, NrNewF=0;
/* deque<bool> Indi(nr_NegNonSimp);
for(size_t j=0;j<nr_NegNonSimp;++j)
Indi[j]=false; */
if(multithreaded_pyramid){
#pragma omp atomic
nrTotalComparisons+=nr_NegNonSimp*nr_PosNonSimp;
}
else{
nrTotalComparisons+=nr_NegNonSimp*nr_PosNonSimp;
}
//=====================================================================
// parallel from here
bool skip_remaining = false;
#ifndef NCATCH
std::exception_ptr tmp_exception;
#endif
#pragma omp parallel private(jj)
{
size_t i,j,k,nr_zero_i;
boost::dynamic_bitset<> subfacet(dim-2);
jj = Neg_Subfacet_Multi_United.begin();
size_t jjpos=0;
int tn = omp_get_ancestor_thread_num(omp_start_level+1);
bool found;
// This for region cannot throw a NormalizException
#pragma omp for schedule(dynamic)
for (size_t j=0; j<nr_NegSubfMult; ++j) { // remove negative subfacets shared
for(;j > jjpos; ++jjpos, ++jj) ; // by non-simpl neg or neutral facets
for(;j < jjpos; --jjpos, --jj) ;
subfacet=(*jj).first;
found=false;
for (i = 0; i <nr_NeuSimp; i++) {
found=subfacet.is_subset_of(Neutral_Simp[i]->GenInHyp);
if(found)
break;
}
if (!found) {
for (i = 0; i <nr_NeuNonSimp; i++) {
found=subfacet.is_subset_of(Neutral_Non_Simp[i]->GenInHyp);
if(found)
break;
}
if(!found) {
for (i = 0; i <nr_NegNonSimp; i++) {
found=subfacet.is_subset_of(Neg_Non_Simp[i]->GenInHyp);
if(found)
break;
}
}
}
if (found) {
jj->second=-1;
}
}
#pragma omp single
{ //remove elements that where found in the previous loop
jj = Neg_Subfacet_Multi_United.begin();
map < boost::dynamic_bitset<>, int > ::iterator last_inserted=Neg_Subfacet.begin(); // used to speedup insertion into the new map
for (; jj!= Neg_Subfacet_Multi_United.end(); ++jj) {
if ((*jj).second != -1) {
last_inserted = Neg_Subfacet.insert(last_inserted,*jj);
}
}
nr_NegSubf=Neg_Subfacet.size();
}
#pragma omp single nowait
{Neg_Subfacet_Multi_United.clear();}
boost::dynamic_bitset<> zero_i(nr_gen);
map <boost::dynamic_bitset<>, int> ::iterator jj_map;
#pragma omp single nowait
if (tv_verbose) {
verboseOutput() << "PS vs NS and PS vs N , " << flush;
}
vector<key_t> key(nr_gen);
size_t nr_missing;
bool common_subfacet;
// we cannot use nowait here because of the way we handle exceptions in this loop
#pragma omp for schedule(dynamic) //nowait
for (size_t i =0; i<nr_PosSimp; i++){
if (skip_remaining) continue;
#ifndef NCATCH
try {
#endif
INTERRUPT_COMPUTATION_BY_EXCEPTION
zero_i=Zero_PN & Pos_Simp[i]->GenInHyp;
nr_zero_i=0;
for(j=0;j<nr_gen && nr_zero_i<=facet_dim;j++)
if(zero_i.test(j)){
key[nr_zero_i]=j;
nr_zero_i++;
}
if(nr_zero_i<subfacet_dim)
continue;
// first PS vs NS
if (nr_zero_i==subfacet_dim) { // NEW slight change in logic. Positive simpl facet shared at most
jj_map=Neg_Subfacet.find(zero_i); // one subfacet with negative simpl facet
if (jj_map!=Neg_Subfacet.end()) {
add_hyperplane(new_generator,*Pos_Simp[i],*Neg_Simp[(*jj_map).second],NewHypsSimp[i],true);
(*jj_map).second = -1; // block subfacet in further searches
}
}
if (nr_zero_i==facet_dim){ // now there could be more such subfacets. We make all and search them.
for (k =0; k<nr_gen; k++) { // BOOST ROUTINE
INTERRUPT_COMPUTATION_BY_EXCEPTION
if(zero_i.test(k)) {
subfacet=zero_i;
subfacet.reset(k); // remove k-th element from facet to obtain subfacet
jj_map=Neg_Subfacet.find(subfacet);
if (jj_map!=Neg_Subfacet.end()) {
add_hyperplane(new_generator,*Pos_Simp[i],*Neg_Simp[(*jj_map).second],NewHypsSimp[i],true);
(*jj_map).second = -1;
// Indi[j]=true;
}
}
}
}
// now PS vs N
for (j=0; j<nr_NegNonSimp; j++){ // search negative facet with common subfacet
INTERRUPT_COMPUTATION_BY_EXCEPTION
nr_missing=0;
common_subfacet=true;
for(k=0;k<nr_zero_i;k++) {
if(!Neg_Non_Simp[j]->GenInHyp.test(key[k])) {
nr_missing++;
if(nr_missing==2 || nr_zero_i==subfacet_dim) {
common_subfacet=false;
break;
}
}
}
if(common_subfacet){
add_hyperplane(new_generator,*Pos_Simp[i],*Neg_Non_Simp[j],NewHypsSimp[i],true);
if(nr_zero_i==subfacet_dim) // only one subfacet can lie in negative hyperplane
break;
}
}
#ifndef NCATCH
} catch(const std::exception& ) {
tmp_exception = std::current_exception();
skip_remaining = true;
#pragma omp flush(skip_remaining)
}
#endif
} // PS vs NS and PS vs N
if (!skip_remaining) {
#pragma omp single nowait
if (tv_verbose) {
verboseOutput() << "P vs NS and P vs N" << endl;
}
list<FACETDATA*> AllNonSimpHyp;
typename list<FACETDATA*>::iterator a;
for(i=0;i<nr_PosNonSimp;++i)
AllNonSimpHyp.push_back(&(*Pos_Non_Simp[i]));
for(i=0;i<nr_NegNonSimp;++i)
AllNonSimpHyp.push_back(&(*Neg_Non_Simp[i]));
for(i=0;i<nr_NeuNonSimp;++i)
AllNonSimpHyp.push_back(&(*Neutral_Non_Simp[i]));
size_t nr_NonSimp = nr_PosNonSimp+nr_NegNonSimp+nr_NeuNonSimp;
bool ranktest;
FACETDATA *hp_i, *hp_j, *hp_t; // pointers to current hyperplanes
size_t missing_bound, nr_common_zero;
boost::dynamic_bitset<> common_zero(nr_gen);
vector<key_t> common_key;
common_key.reserve(nr_gen);
vector<int> key_start(nrGensInCone);
#pragma omp for schedule(dynamic) // nowait
for (size_t i =0; i<nr_PosNonSimp; i++){ //Positive Non Simp vs.Negative Simp and Non Simp
if (skip_remaining) continue;
#ifndef NCATCH
try {
#endif
INTERRUPT_COMPUTATION_BY_EXCEPTION
jj_map = Neg_Subfacet.begin(); // First the Simp
for (j=0; j<nr_NegSubf; ++j,++jj_map) {
if ( (*jj_map).second != -1 ) { // skip used subfacets
if(jj_map->first.is_subset_of(Pos_Non_Simp[i]->GenInHyp)){
add_hyperplane(new_generator,*Pos_Non_Simp[i],*Neg_Simp[(*jj_map).second],NewHypsNonSimp[i],true);
(*jj_map).second = -1; // has now been used
}
}
}
// Now the NonSimp
hp_i=Pos_Non_Simp[i];
zero_i=Zero_PN & hp_i->GenInHyp; // these are the potential vertices in an intersection
nr_zero_i=0;
int last_existing=-1;
for(size_t jj=0;jj<nrGensInCone;jj++) // we make a "key" of the potential vertices in the intersection
{
j=GensInCone[jj];
if(zero_i.test(j)){
key[nr_zero_i]=j;
for(size_t kk= last_existing+1;kk<=jj;kk++) // used in the extension test
key_start[kk]=nr_zero_i; // to find out from which generator on both have existed
nr_zero_i++;
last_existing= jj;
}
}
if(last_existing< (int)nrGensInCone-1)
for(size_t kk=last_existing+1;kk<nrGensInCone;kk++)
key_start[kk]=nr_zero_i;
if (nr_zero_i<subfacet_dim)
continue;
// now nr_zero_i is the number of vertices in hp_i that have a chance to lie in a negative facet
// and key contains the indices
missing_bound=nr_zero_i-subfacet_dim; // at most this number of generators can be missing
// to have a chance for common subfacet
for (j=0; j<nr_NegNonSimp; j++){
hp_j=Neg_Non_Simp[j];
if(hp_i->Ident==hp_j->Mother || hp_j->Ident==hp_i->Mother){ // mother and daughter coming together
add_hyperplane(new_generator,*hp_i,*hp_j,NewHypsNonSimp[i],false); // their intersection is a subfacet
continue; // simplicial set in add_hyperplane
}
bool extension_test=hp_i->BornAt==hp_j->BornAt || (hp_i->BornAt<hp_j->BornAt && hp_j->Mother!=0)
|| (hp_j->BornAt<hp_i->BornAt && hp_i->Mother!=0);
// extension_test=false;
size_t both_existing_from=key_start[max(hp_i->BornAt,hp_j->BornAt)];
nr_missing=0;
nr_common_zero=0;
common_key.clear();
size_t second_loop_bound=nr_zero_i;
common_subfacet=true;
// We use the following criterion:
// if the two facets are not mother and daughter (taken care of already), then
// they cannot have intersected in a subfacet at the time when the second was born.
// In other words: they can only intersect in a subfacet now, if at least one common vertex
// has been added after the birth of the younger one.
// this is indicated by "extended".
if(extension_test){
bool extended=false;
second_loop_bound=both_existing_from; // fisrt we find the common vertices inserted from the step
// where both facets existed the first time
for(k=both_existing_from;k<nr_zero_i;k++){
if(!hp_j->GenInHyp.test(key[k])) {
nr_missing++;
if(nr_missing>missing_bound) {
common_subfacet=false;
break;
}
}
else {
extended=true; // in this case they have a common vertex added after their common existence
common_key.push_back(key[k]);
nr_common_zero++;
}
}
if(!extended || !common_subfacet) //
continue;
}
for(k=0;k<second_loop_bound;k++) { // now the remaining
if(!hp_j->GenInHyp.test(key[k])) {
nr_missing++;
if(nr_missing>missing_bound) {
common_subfacet=false;
break;
}
}
else {
common_key.push_back(key[k]);
nr_common_zero++;
}
}
if(!common_subfacet)
continue;
/* #pragma omp atomic
NrCSF++;*/
if(using_GMP<Integer>())
ranktest = (nr_NonSimp > GMP_time_factor*dim*dim*nr_common_zero/3); // in this case the rank computation takes longer
else
ranktest = (nr_NonSimp > dim*dim*nr_common_zero/3);
// ranktest=true;
if(ranktest) { // cout << "Rang" << endl;
/* #pragma omp atomic
NrRank++; */
Matrix<Integer>& Test = Top_Cone->RankTest[tn];
if (Test.rank_submatrix(Generators,common_key)<subfacet_dim) {
common_subfacet=false;
}
} // ranktest
else{ // now the comparison test
/* #pragma omp atomic
NrComp++; */
common_zero = zero_i & hp_j->GenInHyp;
for (a=AllNonSimpHyp.begin();a!=AllNonSimpHyp.end();++a){
hp_t=*a;
if ((hp_t!=hp_i) && (hp_t!=hp_j) && common_zero.is_subset_of(hp_t->GenInHyp)) {
common_subfacet=false;
AllNonSimpHyp.splice(AllNonSimpHyp.begin(),AllNonSimpHyp,a); // for the "darwinistic" mewthod
break;
}
}
} // else
if (common_subfacet) { //intersection of i and j is a subfacet
add_hyperplane(new_generator,*hp_i,*hp_j,NewHypsNonSimp[i],false); //simplicial set in add_hyperplane
/* #pragma omp atomic
NrNewF++; */
// Indi[j]=true;
}
}
#ifndef NCATCH
} catch(const std::exception& ) {
tmp_exception = std::current_exception();
skip_remaining = true;
#pragma omp flush(skip_remaining)
}
#endif
} // end for
} // end !skip_remaining
} //END parallel
#ifndef NCATCH
if (!(tmp_exception == 0)) std::rethrow_exception(tmp_exception);
#endif
//=====================================================================
// parallel until here
/* if(!is_pyramid)
cout << "Matches " << NrMatches << " pot. common subf " << NrCSF << " rank test " << NrRank << " comp test "
<< NrComp << " neww hyps " << NrNewF << endl; */
for(i=0;i<nr_PosSimp;i++)
Facets.splice(Facets.end(),NewHypsSimp[i]);
for(i=0;i<nr_PosNonSimp;i++)
Facets.splice(Facets.end(),NewHypsNonSimp[i]);
//removing the negative hyperplanes
// now done in build_cone
if (tv_verbose) verboseOutput()<<"transform_values: done"<<endl;
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::extend_triangulation(const size_t& new_generator){
// extends the triangulation of this cone by including new_generator
// simplicial facets save us from searching the "brother" in the existing triangulation
// to which the new simplex gets attached
size_t listsize =old_nr_supp_hyps; // Facets.size();
vector<typename list<FACETDATA>::iterator> visible;
visible.reserve(listsize);
typename list<FACETDATA>::iterator i = Facets.begin();
listsize=0;
for (; i!=Facets.end(); ++i)
if (i->ValNewGen < 0){ // visible facet
visible.push_back(i);
listsize++;
}
#ifndef NCATCH
std::exception_ptr tmp_exception;
#endif
typename list< SHORTSIMPLEX<Integer> >::iterator oldTriBack = --TriangulationBuffer.end();
#pragma omp parallel private(i)
{
size_t k,l;
bool one_not_in_i, not_in_facet;
size_t not_in_i=0;
// size_t facet_dim=dim-1;
// size_t nr_in_i=0;
list< SHORTSIMPLEX<Integer> > Triangulation_kk;
typename list< SHORTSIMPLEX<Integer> >::iterator j;
vector<key_t> key(dim);
// if we only want a partial triangulation but came here because of a deep level
// mark if this part of the triangulation has not to be evaluated
bool skip_eval = false;
#pragma omp for schedule(dynamic)
for (size_t kk=0; kk<listsize; ++kk) {
#ifndef NCATCH
try {
#endif
INTERRUPT_COMPUTATION_BY_EXCEPTION
i=visible[kk];
/* nr_in_i=0;
for(size_t m=0;m<nr_gen;m++){
if(i->GenInHyp.test(m))
nr_in_i++;
if(nr_in_i>facet_dim){
break;
}
}*/
skip_eval = Top_Cone->do_partial_triangulation && i->ValNewGen == -1
&& is_hyperplane_included(*i);
if (i->simplicial){ // simplicial
l=0;
for (k = 0; k <nr_gen; k++) {
if (i->GenInHyp[k]==1) {
key[l]=k;
l++;
}
}
key[dim-1]=new_generator;
if (skip_eval)
store_key(key,0,0,Triangulation_kk);
else
store_key(key,-i->ValNewGen,0,Triangulation_kk);
continue;
} // end simplicial
size_t irrelevant_vertices=0;
for(size_t vertex=0;vertex<nrGensInCone;++vertex){
if(i->GenInHyp[GensInCone[vertex]]==0) // lead vertex not in hyperplane
continue;
if(irrelevant_vertices<dim-2){
++irrelevant_vertices;
continue;
}
j=TriSectionFirst[vertex];
bool done=false;
for(;!done;j++)
{
done=(j==TriSectionLast[vertex]);
key=j->key;
one_not_in_i=false; // true indicates that one gen of simplex is not in hyperplane
not_in_facet=false; // true indicates that a second gen of simplex is not in hyperplane
for(k=0;k<dim;k++){
if ( !i->GenInHyp.test(key[k])) {
if(one_not_in_i){
not_in_facet=true;
break;
}
one_not_in_i=true;
not_in_i=k;
}
}
if(not_in_facet) // simplex does not share facet with hyperplane
continue;
key[not_in_i]=new_generator;
if (skip_eval)
store_key(key,0,j->vol,Triangulation_kk);
else
store_key(key,-i->ValNewGen,j->vol,Triangulation_kk);
} // j
} // for vertex
#ifndef NCATCH
} catch(const std::exception& ) {
tmp_exception = std::current_exception();
}
#endif
} // omp for kk
if (multithreaded_pyramid) {
#pragma omp critical(TRIANG)
TriangulationBuffer.splice(TriangulationBuffer.end(),Triangulation_kk);
} else
TriangulationBuffer.splice(TriangulationBuffer.end(),Triangulation_kk);
} // parallel
#ifndef NCATCH
if (!(tmp_exception == 0)) std::rethrow_exception(tmp_exception);
#endif
// GensInCone.push_back(new_generator); // now in extend_cone
TriSectionFirst.push_back(++oldTriBack);
TriSectionLast.push_back(--TriangulationBuffer.end());
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::store_key(const vector<key_t>& key, const Integer& height,
const Integer& mother_vol, list< SHORTSIMPLEX<Integer> >& Triangulation){
// stores a simplex given by key and height in Triangulation
// mother_vol is the volume of the simplex to which the new one is attached
SHORTSIMPLEX<Integer> newsimplex;
newsimplex.key=key;
newsimplex.height=height;
newsimplex.vol=0;
if(multithreaded_pyramid){
#pragma omp atomic
TriangulationBufferSize++;
}
else {
TriangulationBufferSize++;
}
int tn;
if(omp_get_level()==omp_start_level)
tn=0;
else
tn = omp_get_ancestor_thread_num(omp_start_level+1);
if (do_only_multiplicity) {
// directly compute the volume
if (mother_vol==1)
newsimplex.vol = height;
// the multiplicity is computed in SimplexEvaluator
for(size_t i=0; i<dim; i++) // and needs the key in TopCone numbers
newsimplex.key[i]=Top_Key[newsimplex.key[i]];
if (keep_triangulation)
sort(newsimplex.key.begin(),newsimplex.key.end());
Top_Cone->SimplexEval[tn].evaluate(newsimplex);
// restore the local generator numbering, needed in extend_triangulation
newsimplex.key=key;
}
if (height == 0) Top_Cone->triangulation_is_partial = true;
if (keep_triangulation){
Triangulation.push_back(newsimplex);
return;
}
bool Simpl_available=true;
typename list< SHORTSIMPLEX<Integer> >::iterator F;
if(Top_Cone->FS[tn].empty()){
if (Top_Cone->FreeSimpl.empty()) {
Simpl_available=false;
} else {
#pragma omp critical(FREESIMPL)
{
if (Top_Cone->FreeSimpl.empty()) {
Simpl_available=false;
} else {
// take 1000 simplices from FreeSimpl or what you can get
F = Top_Cone->FreeSimpl.begin();
size_t q;
for (q = 0; q < 1000; ++q, ++F) {
if (F == Top_Cone->FreeSimpl.end())
break;
}
if(q<1000)
Top_Cone->FS[tn].splice(Top_Cone->FS[tn].begin(),
Top_Cone->FreeSimpl);
else
Top_Cone->FS[tn].splice(Top_Cone->FS[tn].begin(),
Top_Cone->FreeSimpl,Top_Cone->FreeSimpl.begin(),F);
} // if empty global (critical)
} // critical
} // if empty global
} // if empty thread
if (Simpl_available) {
Triangulation.splice(Triangulation.end(),Top_Cone->FS[tn],
Top_Cone->FS[tn].begin());
Triangulation.back() = newsimplex;
} else {
Triangulation.push_back(newsimplex);
}
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::process_pyramids(const size_t new_generator,const bool recursive){
/*
We distinguish two types of pyramids:
(i) recursive pyramids that give their support hyperplanes back to the mother.
(ii) independent pyramids that are not linked to the mother.
The parameter "recursive" indicates whether the pyramids that will be created
in process_pyramid(s) are of type (i) or (ii).
Every pyramid can create subpyramids of both types (not the case in version 2.8 - 2.10).
Whether "this" is of type (i) or (ii) is indicated by do_all_hyperplanes.
The creation of (sub)pyramids of type (i) can be blocked by setting recursion_allowed=false.
(Not done in this version.)
is_pyramid==false for the top_cone and ==true else.
multithreaded_pyramid indicates whether parallelization takes place within the
computation of a pyramid or whether it is computed in a single thread (defined in build_cone).
Recursie pyramids are processed immediately after creation (as in 2.8). However, there
are two exceptions:
(a) In order to avoid very long waiting times for the computation of the "large" ones,
these are treated as follows: the support hyperplanes of "this" coming from their bases
(as negative hyperplanes of "this") are computed by matching them with the
positive hyperplanes of "this". This Fourier-Motzkin step is much more
efficient if a pyramid is large. For triangulation a large recursive
pyramid is then stored as a pyramid of type (ii).
(b) If "this" is processed in a parallelized loop calling process_pyramids, then
the loop in process_pyramids cannot be interrupted for the evaluation of simplices. As a
consequence an extremely long lst of simplices could arise if many small subpyramids of "this"
are created in process_pyramids. In order to prevent this dangeous effect, small recursive
subpyramids are stored for later triangulation if the simplex buffer has reached its
size bound.
Pyramids of type (ii) are stpred in Pyramids. The store_level of the created pyramids is 0
for all pyramids created (possibly recursively) from the top cone. Pyramids created
in evaluate_stored_pyramids get the store level for their subpyramids in that routine and
transfer it to their recursive daughters. (correction March 4, 2015).
Note: the top cone has pyr_level=-1. The pyr_level has no algorithmic relevance
at present, but it shows the depth of the pyramid recursion at which the pyramid has been
created.
*/
size_t start_level=omp_get_level(); // allows us to check that we are on level 0
// outside the loop and can therefore call evaluation
// in order to empty the buffers
vector<key_t> Pyramid_key;
Pyramid_key.reserve(nr_gen);
bool skip_triang; // make hyperplanes but skip triangulation (recursive pyramids only)
deque<bool> done(old_nr_supp_hyps,false);
bool skip_remaining;
#ifndef NCATCH
std::exception_ptr tmp_exception;
#endif
typename list< FACETDATA >::iterator hyp;
size_t nr_done=0;
do{ // repeats processing until all hyperplanes have been processed
hyp=Facets.begin();
size_t hyppos=0;
skip_remaining = false;
const long VERBOSE_STEPS = 50;
long step_x_size = old_nr_supp_hyps-VERBOSE_STEPS;
const size_t RepBound=10000;
#pragma omp parallel for private(skip_triang) firstprivate(hyppos,hyp,Pyramid_key) schedule(dynamic) reduction(+: nr_done)
for (size_t kk=0; kk<old_nr_supp_hyps; ++kk) {
if (skip_remaining) continue;
if(verbose && old_nr_supp_hyps>=RepBound){
#pragma omp critical(VERBOSE)
while ((long)(kk*VERBOSE_STEPS) >= step_x_size) {
step_x_size += old_nr_supp_hyps;
verboseOutput() << "." <<flush;
}
}
#ifndef NCATCH
try {
#endif
for(;kk > hyppos; hyppos++, hyp++) ;
for(;kk < hyppos; hyppos--, hyp--) ;
INTERRUPT_COMPUTATION_BY_EXCEPTION
if(done[hyppos])
continue;
done[hyppos]=true;
nr_done++;
if (hyp->ValNewGen == 0){ // MUST BE SET HERE
hyp->GenInHyp.set(new_generator);
if(recursive) hyp->simplicial=false; // in the recursive case
}
if (hyp->ValNewGen >= 0) // facet not visible
continue;
skip_triang = false;
if (Top_Cone->do_partial_triangulation && hyp->ValNewGen>=-1) { //ht1 criterion
skip_triang = is_hyperplane_included(*hyp);
if (skip_triang) {
Top_Cone->triangulation_is_partial = true;
if (!recursive) {
continue;
}
}
}
Pyramid_key.clear(); // make data of new pyramid
Pyramid_key.push_back(new_generator);
for(size_t i=0;i<nr_gen;i++){
if(in_triang[i] && hyp->GenInHyp.test(i)) {
Pyramid_key.push_back(i);
}
}
// now we can store the new pyramid at the right place (or finish the simplicial ones)
if (recursive && skip_triang) { // mark as "do not triangulate"
process_pyramid(Pyramid_key, new_generator,store_level,0, recursive,hyp,start_level);
} else { //default
process_pyramid(Pyramid_key, new_generator,store_level,-hyp->ValNewGen, recursive,hyp,start_level);
}
// interrupt parallel execution if it is really parallel
// to keep the triangulationand pyramid buffers under control
if (start_level==0) {
if (check_evaluation_buffer_size() || Top_Cone->check_pyr_buffer(store_level)) {
skip_remaining = true;
}
}
#ifndef NCATCH
} catch(const std::exception& ) {
tmp_exception = std::current_exception();
skip_remaining = true;
#pragma omp flush(skip_remaining)
}
#endif
} // end parallel loop over hyperplanes
#ifndef NCATCH
if (!(tmp_exception == 0)) std::rethrow_exception(tmp_exception);
#endif
if (!omp_in_parallel())
try_offload(0);
if (start_level==0 && check_evaluation_buffer_size()) {
Top_Cone->evaluate_triangulation();
}
if (start_level==0 && Top_Cone->check_pyr_buffer(store_level)) {
Top_Cone->evaluate_stored_pyramids(store_level);
}
if (verbose && old_nr_supp_hyps>=RepBound)
verboseOutput() << endl;
} while (nr_done < old_nr_supp_hyps);
evaluate_large_rec_pyramids(new_generator);
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::process_pyramid(const vector<key_t>& Pyramid_key,
const size_t new_generator,const size_t store_level, Integer height, const bool recursive,
typename list< FACETDATA >::iterator hyp, size_t start_level){
// processes simplicial pyramids directly, stores other pyramids into their depots
#pragma omp atomic
Top_Cone->totalNrPyr++;
if(Pyramid_key.size()==dim){ // simplicial pyramid completely done here
#pragma omp atomic // only for saving memory
Top_Cone->nrSimplicialPyr++;
if(recursive){ // the facets may be facets of the mother cone and if recursive==true must be given back
Matrix<Integer> H(dim,dim);
Integer dummy_vol;
Generators.simplex_data(Pyramid_key,H, dummy_vol,false);
list<FACETDATA> NewFacets;
FACETDATA NewFacet;
NewFacet.GenInHyp.resize(nr_gen);
for (size_t i=0; i<dim;i++) {
NewFacet.Hyp = H[i];
NewFacet.GenInHyp.set();
NewFacet.GenInHyp.reset(i);
NewFacet.simplicial=true;
NewFacet.is_positive_on_all_original_gens=false;
NewFacet.is_negative_on_some_original_gen=false;
NewFacets.push_back(NewFacet);
}
select_supphyps_from(NewFacets,new_generator,Pyramid_key); // takes itself care of multithreaded_pyramid
}
if (height != 0 && (do_triangulation || do_partial_triangulation)) {
if(multithreaded_pyramid) {
#ifndef NCATCH
std::exception_ptr tmp_exception;
#endif
#pragma omp critical(TRIANG)
{
#ifndef NCATCH
try{
#endif
store_key(Pyramid_key,height,0,TriangulationBuffer);
nrTotalComparisons+=dim*dim/2;
#ifndef NCATCH
} catch(const std::exception& ) {
tmp_exception = std::current_exception();
}
#endif
} // end critical
#ifndef NCATCH
if (!(tmp_exception == 0)) std::rethrow_exception(tmp_exception);
#endif
} else {
store_key(Pyramid_key,height,0,TriangulationBuffer);
nrTotalComparisons+=dim*dim/2;
}
}
}
else { // non-simplicial
bool large=(largePyramidFactor*Comparisons[Pyramid_key.size()-dim] > old_nr_supp_hyps); // Pyramid_key.size()>largePyramidFactor*dim;
if (!recursive || (large && (do_triangulation || do_partial_triangulation) && height!=0) ) { // must also store for triangulation if recursive and large
vector<key_t> key_wrt_top(Pyramid_key.size());
for(size_t i=0;i<Pyramid_key.size();i++)
key_wrt_top[i]=Top_Key[Pyramid_key[i]];
#pragma omp critical(STOREPYRAMIDS)
{
// cout << "store_level " << store_level << " large " << large << " pyr level " << pyr_level << endl;
Top_Cone->Pyramids[store_level].push_back(key_wrt_top);
Top_Cone->nrPyramids[store_level]++;
} // critical
if(!recursive) // in this case we need only store for future triangulation, and that has been done
return;
}
// now we are in the recursive case and must compute support hyperplanes of the subpyramid
if(large){ // large recursive pyramid
if(multithreaded_pyramid){
#pragma omp critical(LARGERECPYRS)
LargeRecPyrs.push_back(*hyp); // LargeRecPyrs are kept and evaluated locally
}
else
LargeRecPyrs.push_back(*hyp);
return; // done with the large recusive pyramids
}
// only recursive small ones left
Full_Cone<Integer> Pyramid(*this,Pyramid_key);
Pyramid.Mother = this;
Pyramid.Mother_Key = Pyramid_key; // need these data to give back supphyps
Pyramid.apex=new_generator;
if (height == 0) { //indicates "do not triangulate"
Pyramid.do_triangulation = false;
Pyramid.do_partial_triangulation = false;
Pyramid.do_Hilbert_basis = false;
Pyramid.do_deg1_elements=false;
}
bool store_for_triangulation=(store_level!=0) //loop in process_pyramids cannot be interrupted
&& (Pyramid.do_triangulation || Pyramid.do_partial_triangulation) // we must (partially) triangulate
&& (start_level!=0 && Top_Cone->TriangulationBufferSize > 2*EvalBoundTriang); // evaluation buffer already full // EvalBoundTriang
if (store_for_triangulation) {
vector<key_t> key_wrt_top(Pyramid_key.size());
for(size_t i=0;i<Pyramid_key.size();i++)
key_wrt_top[i]=Top_Key[Pyramid_key[i]];
#pragma omp critical(STOREPYRAMIDS)
{
Top_Cone->Pyramids[store_level].push_back(key_wrt_top);
Top_Cone->nrPyramids[store_level]++;
} // critical
// Now we must suppress immediate triangulation
Pyramid.do_triangulation = false;
Pyramid.do_partial_triangulation = false;
Pyramid.do_Hilbert_basis = false;
Pyramid.do_deg1_elements=false;
}
Pyramid.build_cone();
if(multithreaded_pyramid){
#pragma omp atomic
nrTotalComparisons+=Pyramid.nrTotalComparisons;
} else
nrTotalComparisons+=Pyramid.nrTotalComparisons;
} // else non-simplicial
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::find_and_evaluate_start_simplex(){
size_t i,j;
Integer factor;
/* Simplex<Integer> S = find_start_simplex();
vector<key_t> key=S.read_key(); // generators indexed from 0 */
vector<key_t> key=find_start_simplex();
assert(key.size()==dim); // safety heck
if(verbose){
verboseOutput() << "Start simplex ";
for(size_t i=0;i<key.size();++i)
verboseOutput() << key[i]+1 << " ";
verboseOutput() << endl;
}
Matrix<Integer> H(dim,dim);
Integer vol;
Generators.simplex_data(key,H,vol,do_partial_triangulation || do_triangulation);
// H.pretty_print(cout);
for (i = 0; i < dim; i++) {
in_triang[key[i]]=true;
GensInCone.push_back(key[i]);
if (deg1_triangulation && isComputed(ConeProperty::Grading))
deg1_triangulation = (gen_degrees[key[i]] == 1);
}
nrGensInCone=dim;
nrTotalComparisons=dim*dim/2;
if(using_GMP<Integer>())
nrTotalComparisons*=GMP_time_factor; // because of the linear algebra involved in this routine
Comparisons.push_back(nrTotalComparisons);
for (i = 0; i <dim; i++) {
FACETDATA NewFacet; NewFacet.GenInHyp.resize(nr_gen);
NewFacet.is_positive_on_all_original_gens=false;
NewFacet.is_negative_on_some_original_gen=false;
NewFacet.Hyp=H[i];
NewFacet.simplicial=true; // indeed, the start simplex is simplicial
for(j=0;j < dim;j++)
if(j!=i)
NewFacet.GenInHyp.set(key[j]);
NewFacet.ValNewGen=-1; // must be taken negative since opposite facet
number_hyperplane(NewFacet,0,0); // created with gen 0
Facets.push_back(NewFacet); // was visible before adding this vertex
}
if(!is_pyramid){
//define Order_Vector, decides which facets of the simplices are excluded
Order_Vector = vector<Integer>(dim,0);
// Matrix<Integer> G=S.read_generators();
for(i=0;i<dim;i++){
factor=(unsigned long) (1+i%10); // (2*(rand()%(2*dim))+3);
for(j=0;j<dim;j++)
Order_Vector[j]+=factor*Generators[key[i]][j];
}
}
//the volume is an upper bound for the height
if(do_triangulation || (do_partial_triangulation && vol>1))
{
store_key(key,vol,1,TriangulationBuffer);
if(do_only_multiplicity) {
#pragma omp atomic
TotDet++;
}
} else if (do_partial_triangulation) {
triangulation_is_partial = true;
}
if(do_triangulation){ // we must prepare the sections of the triangulation
for(i=0;i<dim;i++)
{
// GensInCone.push_back(key[i]); // now done in first loop since always needed
TriSectionFirst.push_back(TriangulationBuffer.begin());
TriSectionLast.push_back(TriangulationBuffer.begin());
}
}
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::select_supphyps_from(const list<FACETDATA>& NewFacets,
const size_t new_generator, const vector<key_t>& Pyramid_key){
// the mother cone (=this) selects supphyps from the list NewFacets supplied by the daughter
// the daughter provides the necessary information via the parameters
size_t i;
boost::dynamic_bitset<> in_Pyr(nr_gen);
for (i=0; i<Pyramid_key.size(); i++) {
in_Pyr.set(Pyramid_key[i]);
}
// the new generator is always the first in the pyramid
assert(Pyramid_key[0] == new_generator);
typename list<FACETDATA>::const_iterator pyr_hyp = NewFacets.begin();
bool new_global_hyp;
FACETDATA NewFacet;
NewFacet.is_positive_on_all_original_gens=false;
NewFacet.is_negative_on_some_original_gen=false;
NewFacet.GenInHyp.resize(nr_gen);
Integer test;
for (; pyr_hyp!=NewFacets.end(); ++pyr_hyp) {
if(!pyr_hyp->GenInHyp.test(0)) // new gen not in hyp
continue;
new_global_hyp=true;
for (i=0; i<nr_gen; ++i){
if(in_Pyr.test(i) || !in_triang[i])
continue;
test=v_scalar_product(Generators[i],pyr_hyp->Hyp);
if(test<=0){
new_global_hyp=false;
break;
}
}
if(new_global_hyp){
NewFacet.Hyp=pyr_hyp->Hyp;
NewFacet.GenInHyp.reset();
// size_t gens_in_facet=0;
for (i=0; i<Pyramid_key.size(); ++i) {
if (pyr_hyp->GenInHyp.test(i) && in_triang[Pyramid_key[i]]) {
NewFacet.GenInHyp.set(Pyramid_key[i]);
// gens_in_facet++;
}
}
/* for (i=0; i<nr_gen; ++i) {
if (NewFacet.GenInHyp.test(i) && in_triang[i]) {
gens_in_facet++;
}
}*/
// gens_in_facet++; // Note: new generator not yet in in_triang
NewFacet.GenInHyp.set(new_generator);
NewFacet.simplicial=pyr_hyp->simplicial; // (gens_in_facet==dim-1);
check_simpliciality_hyperplane(NewFacet);
number_hyperplane(NewFacet,nrGensInCone,0); //mother unknown
if(multithreaded_pyramid){
#pragma omp critical(GIVEBACKHYPS)
Facets.push_back(NewFacet);
} else {
Facets.push_back(NewFacet);
}
}
}
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::match_neg_hyp_with_pos_hyps(const FACETDATA& hyp, size_t new_generator,list<FACETDATA*>& PosHyps, boost::dynamic_bitset<>& Zero_P){
size_t missing_bound, nr_common_zero;
boost::dynamic_bitset<> common_zero(nr_gen);
vector<key_t> common_key;
common_key.reserve(nr_gen);
vector<key_t> key(nr_gen);
bool common_subfacet;
list<FACETDATA> NewHyp;
size_t subfacet_dim=dim-2;
size_t nr_missing;
typename list<FACETDATA*>::iterator a;
list<FACETDATA> NewHyps;
Matrix<Integer> Test(0,dim);
boost::dynamic_bitset<> zero_hyp=hyp.GenInHyp & Zero_P; // we intersect with the set of gens in positive hyps
vector<int> key_start(nrGensInCone);
size_t nr_zero_hyp=0;
size_t j;
int last_existing=-1;
for(size_t jj=0;jj<nrGensInCone;jj++)
{
j=GensInCone[jj];
if(zero_hyp.test(j)){
key[nr_zero_hyp]=j;
for(size_t kk= last_existing+1;kk<=jj;kk++)
key_start[kk]=nr_zero_hyp;
nr_zero_hyp++;
last_existing= jj;
}
}
if(last_existing< (int)nrGensInCone-1)
for(size_t kk=last_existing+1;kk<nrGensInCone;kk++)
key_start[kk]=nr_zero_hyp;
if (nr_zero_hyp<dim-2)
return;
int tn = omp_get_ancestor_thread_num(omp_start_level+1);
missing_bound=nr_zero_hyp-subfacet_dim; // at most this number of generators can be missing
// to have a chance for common subfacet
typename list< FACETDATA*>::iterator hp_j_iterator=PosHyps.begin();
FACETDATA* hp_j;
for (;hp_j_iterator!=PosHyps.end();++hp_j_iterator){ //match hyp with the given Pos
hp_j=*hp_j_iterator;
if(hyp.Ident==hp_j->Mother || hp_j->Ident==hyp.Mother){ // mother and daughter coming together
// their intersection is a subfacet
add_hyperplane(new_generator,*hp_j,hyp,NewHyps,false); // simplicial set in add_hyperplane
continue;
}
bool extension_test=hyp.BornAt==hp_j->BornAt || (hyp.BornAt<hp_j->BornAt && hp_j->Mother!=0)
|| (hp_j->BornAt<hyp.BornAt && hyp.Mother!=0);
size_t both_existing_from=key_start[max(hyp.BornAt,hp_j->BornAt)];
nr_missing=0;
nr_common_zero=0;
common_key.clear();
size_t second_loop_bound=nr_zero_hyp;
common_subfacet=true;
boost::dynamic_bitset<> common_zero(nr_gen);
if(extension_test){
bool extended=false;
second_loop_bound=both_existing_from;
for(size_t k=both_existing_from;k<nr_zero_hyp;k++){
if(!hp_j->GenInHyp.test(key[k])) {
nr_missing++;
if(nr_missing>missing_bound) {
common_subfacet=false;
break;
}
}
else {
extended=true;
common_key.push_back(key[k]);
common_zero.set(key[k]);
nr_common_zero++;
}
}
if(!extended || !common_subfacet) //
continue;
}
for(size_t k=0;k<second_loop_bound;k++) {
if(!hp_j->GenInHyp.test(key[k])) {
nr_missing++;
if(nr_missing>missing_bound) {
common_subfacet=false;
break;
}
}
else {
common_key.push_back(key[k]);
common_zero.set(key[k]);
nr_common_zero++;
}
}
if(!common_subfacet)
continue;
assert(nr_common_zero >=subfacet_dim);
if (!hp_j->simplicial){
bool ranktest;
/* if(using_GMP<Integer>())
ranktest = (old_nr_supp_hyps > 10*GMP_time_factor*dim*dim*nr_common_zero/3); // in this case the rank computation takes longer
else
ranktest = (old_nr_supp_hyps > 10*dim*dim*nr_common_zero/3); */
ranktest=true;
if(ranktest){
// cout << "Rank" << endl;
Matrix<Integer>& Test = Top_Cone->RankTest[tn];
if(Test.rank_submatrix(Generators,common_key)<subfacet_dim)
common_subfacet=false; // don't make a hyperplane
}
else{ // now the comparison test
// cout << "Compare" << endl;
auto hp_t=Facets.begin();
for (;hp_t!=Facets.end();++hp_t){
if(hp_t->simplicial)
continue;
if ((hp_t->Ident!=hyp.Ident) && (hp_t->Ident!=hp_j->Ident) && common_zero.is_subset_of(hp_t->GenInHyp)) {
common_subfacet=false;
break;
}
}
} // else
} // !simplicial
if(common_subfacet)
add_hyperplane(new_generator,*hp_j,hyp,NewHyps,false); // simplicial set in add_hyperplane
} // for
if(multithreaded_pyramid)
#pragma omp critical(GIVEBACKHYPS)
Facets.splice(Facets.end(),NewHyps);
else
Facets.splice(Facets.end(),NewHyps);
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::collect_pos_supphyps(list<FACETDATA*>& PosHyps, boost::dynamic_bitset<>& Zero_P, size_t& nr_pos){
// positive facets are collected in a list
typename list<FACETDATA>::iterator ii = Facets.begin();
nr_pos=0;
for (size_t ij=0; ij< old_nr_supp_hyps; ++ij, ++ii)
if (ii->ValNewGen>0) {
Zero_P |= ii->GenInHyp;
PosHyps.push_back(&(*ii));
nr_pos++;
}
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::evaluate_large_rec_pyramids(size_t new_generator){
size_t nrLargeRecPyrs=LargeRecPyrs.size();
if(nrLargeRecPyrs==0)
return;
if(verbose)
verboseOutput() << "large pyramids " << nrLargeRecPyrs << endl;
list<FACETDATA*> PosHyps;
boost::dynamic_bitset<> Zero_P(nr_gen);
size_t nr_pos;
collect_pos_supphyps(PosHyps,Zero_P,nr_pos);
nrTotalComparisons+=nr_pos*nrLargeRecPyrs;
#ifndef NCATCH
std::exception_ptr tmp_exception;
#endif
const long VERBOSE_STEPS = 50;
long step_x_size = nrLargeRecPyrs-VERBOSE_STEPS;
const size_t RepBound=100;
bool skip_remaining=false;
#pragma omp parallel
{
size_t ppos=0;
typename list<FACETDATA>::iterator p=LargeRecPyrs.begin();
#pragma omp for schedule(dynamic)
for(size_t i=0; i<nrLargeRecPyrs; i++){
if(skip_remaining)
continue;
for(; i > ppos; ++ppos, ++p) ;
for(; i < ppos; --ppos, --p) ;
if(verbose && nrLargeRecPyrs>=RepBound){
#pragma omp critical(VERBOSE)
while ((long)(i*VERBOSE_STEPS) >= step_x_size) {
step_x_size += nrLargeRecPyrs;
verboseOutput() << "." <<flush;
}
}
#ifndef NCATCH
try {
#endif
INTERRUPT_COMPUTATION_BY_EXCEPTION
match_neg_hyp_with_pos_hyps(*p,new_generator,PosHyps,Zero_P);
#ifndef NCATCH
} catch(const std::exception& ) {
tmp_exception = std::current_exception();
skip_remaining = true;
#pragma omp flush(skip_remaining)
}
#endif
}
} // parallel
#ifndef NCATCH
if (!(tmp_exception == 0)) std::rethrow_exception(tmp_exception);
#endif
if(verbose && nrLargeRecPyrs>=RepBound)
verboseOutput() << endl;
LargeRecPyrs.clear();
}
//---------------------------------------------------------------------------
template<typename Integer>
bool Full_Cone<Integer>::check_pyr_buffer(const size_t level){
if(level==0)
return(nrPyramids[0] > EvalBoundLevel0Pyr);
else
return(nrPyramids[level] > EvalBoundPyr);
}
//---------------------------------------------------------------------------
#ifdef NMZ_MIC_OFFLOAD
template<>
void Full_Cone<long long>::try_offload(size_t max_level) {
if (!is_pyramid && _Offload_get_device_number() < 0) // dynamic check for being on CPU (-1)
{
if (max_level >= nrPyramids.size()) max_level = nrPyramids.size()-1;
for (size_t level = 0; level <= max_level; ++level) {
if (nrPyramids[level] >= 100) {
// cout << "XXX: Try offload of level " << level << " pyramids ..." << endl;
mic_offloader.offload_pyramids(*this, level);
break;
}
}
}
}
template<typename Integer>
void Full_Cone<Integer>::try_offload(size_t max_level) {
}
//else it is implemented in the header
template<typename Integer>
void Full_Cone<Integer>::try_offload_loc(long place,size_t max_level){
verboseOutput() << "From place " << place << " " << "level " << max_level << endl;
try_offload(max_level);
}
#endif // NMZ_MIC_OFFLOAD
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::evaluate_stored_pyramids(const size_t level){
// evaluates the stored non-recursive pyramids
#ifdef NMZ_MIC_OFFLOAD
Pyramids_scrambled[level]=false;
if(level==0 && _Offload_get_device_number() >= 0){
verboseOutput() << "Start evaluation of " << nrPyramids[level] << " pyrs on level " << level << endl;
// verboseOutput() << "In parallel " << omp_in_parallel() << endl;
}
#endif // NMZ_MIC_OFFLOAD
if(Pyramids[level].empty())
return;
assert(omp_get_level()==omp_start_level); // assert(!omp_in_parallel());
assert(!is_pyramid);
if (Pyramids.size() < level+2) {
Pyramids.resize(level+2); // provide space for a new generation
nrPyramids.resize(level+2, 0);
Pyramids_scrambled.resize(level+2, false);
}
size_t eval_down_to = 0;
#ifdef NMZ_MIC_OFFLOAD
#ifndef __MIC__
// only on host and if offload is available
if (level == 0 && nrPyramids[0] > EvalBoundLevel0Pyr) {
eval_down_to = EvalBoundLevel0Pyr;
}
#endif
#endif
vector<char> Done(nrPyramids[level],0);
if (verbose) {
verboseOutput() << "**************************************************" << endl;
for (size_t l=0; l<=level; ++l) {
if (nrPyramids[l]>0) {
verboseOutput() << "level " << l << " pyramids remaining: "
<< nrPyramids[l] << endl;
}
}
verboseOutput() << "**************************************************" << endl;
}
typename list<vector<key_t> >::iterator p;
size_t ppos;
bool skip_remaining;
#ifndef NCATCH
std::exception_ptr tmp_exception;
#endif
while (nrPyramids[level] > eval_down_to) {
p = Pyramids[level].begin();
ppos=0;
skip_remaining = false;
#pragma omp parallel for firstprivate(p,ppos) schedule(dynamic)
for(size_t i=0; i<nrPyramids[level]; i++){
if (skip_remaining)
continue;
for(; i > ppos; ++ppos, ++p) ;
for(; i < ppos; --ppos, --p) ;
if(Done[i])
continue;
Done[i]=1;
#ifndef NCATCH
try {
#endif
INTERRUPT_COMPUTATION_BY_EXCEPTION
Full_Cone<Integer> Pyramid(*this,*p);
// Pyramid.recursion_allowed=false;
Pyramid.do_all_hyperplanes=false;
if (level>=2 && do_partial_triangulation){ // limits the descent of do_partial_triangulation
Pyramid.do_triangulation=true;
Pyramid.do_partial_triangulation=false;
}
Pyramid.store_level=level+1;
Pyramid.build_cone();
if (check_evaluation_buffer_size() || Top_Cone->check_pyr_buffer(level+1)) {
// interrupt parallel execution to keep the buffer under control
skip_remaining = true;
}
#ifndef NCATCH
} catch(const std::exception& ) {
tmp_exception = std::current_exception();
skip_remaining = true;
#pragma omp flush(skip_remaining)
}
#endif
} //end parallel for
#ifndef NCATCH
if (!(tmp_exception == 0)) std::rethrow_exception(tmp_exception);
#endif
// remove done pyramids
p = Pyramids[level].begin();
for(size_t i=0; p != Pyramids[level].end(); i++){
if (Done[i]) {
p=Pyramids[level].erase(p);
nrPyramids[level]--;
Done[i]=0;
} else {
++p;
}
}
try_offload(level+1);
if (check_evaluation_buffer_size()) {
if (verbose)
verboseOutput() << nrPyramids[level] <<
" pyramids remaining on level " << level << ", ";
Top_Cone->evaluate_triangulation();
try_offload(level+1);
}
if (Top_Cone->check_pyr_buffer(level+1)) {
evaluate_stored_pyramids(level+1);
}
} //end while (nrPyramids[level] > 0)
if (verbose) {
verboseOutput() << "**************************************************" << endl;
verboseOutput() << "all pyramids on level "<< level << " done!"<<endl;
if (nrPyramids[level+1] == 0) {
for (size_t l=0; l<=level; ++l) {
if (nrPyramids[l]>0) {
verboseOutput() << "level " << l << " pyramids remaining: "
<< nrPyramids[l] << endl;
}
}
verboseOutput() << "**************************************************" << endl;
}
}
if(check_evaluation_buffer())
{
Top_Cone->evaluate_triangulation();
}
evaluate_stored_pyramids(level+1);
}
//---------------------------------------------------------------------------
/* builds the cone successively by inserting generators */
template<typename Integer>
void Full_Cone<Integer>::build_cone() {
// if(dim>0){ //correction needed to include the 0 cone;
// cout << "Pyr " << pyr_level << endl;
long long RecBoundSuppHyp;
RecBoundSuppHyp = dim*dim*dim*SuppHypRecursionFactor;
if(using_GMP<Integer>())
RecBoundSuppHyp*=GMP_time_factor; // pyramid building is more difficult for complicated arithmetic
size_t RecBoundTriang=1000000; // if number(supphyps)*size(triang) > RecBoundTriang pass to pyramids
if(using_GMP<Integer>())
RecBoundTriang*=GMP_time_factor;
tri_recursion=false;
multithreaded_pyramid=(omp_get_level()==omp_start_level);
size_t nr_original_gen=0;
size_t steps_in_approximation = 0;
if (!is_pyramid && is_approximation)
{
nr_original_gen = OriginalGenerators.nr_of_rows();
vector<size_t> nr_approx_points; // how many points are in the approximation
for (size_t j=0;j<nr_original_gen;++j) {
nr_approx_points.push_back(approx_points_keys[j].size());
}
// for every vertex sort the approximation points via: number of positive halfspaces / index
vector<key_t> overall_perm;
// stores the perm of every list
vector<vector<key_t> > local_perms(nr_original_gen);
for (size_t current_gen = 0 ; current_gen<nr_original_gen;++current_gen){
vector<key_t> local_perm;
if (approx_points_keys[current_gen].size()>0){
auto jt=approx_points_keys[current_gen].begin();
list<pair<size_t,key_t> > max_halfspace_index_list;
size_t tmp_hyp=0;
// TODO: collect only those which belong to the current generator?
for (;jt!=approx_points_keys[current_gen].end();++jt){
// cout << dim << " " << Support_Hyperplanes.nr_of_columns()<< " " << Generators[*jt].size() << endl;
tmp_hyp = v_nr_negative(Support_Hyperplanes.MxV(Generators[*jt])); // nr of negative halfspaces
max_halfspace_index_list.insert(max_halfspace_index_list.end(),make_pair(tmp_hyp,*jt));
}
max_halfspace_index_list.sort([](const pair<size_t,key_t> &left, const pair<size_t,key_t> &right) {
return right.first < left.first;
});
auto list_it = max_halfspace_index_list.begin();
for(;list_it!=max_halfspace_index_list.end();++list_it){
local_perm.push_back(list_it->second);
}
}
local_perms[current_gen]=local_perm;
}
// concatenate the permutations
size_t local_perm_counter=0;
bool not_done = true;
while (not_done){
not_done=false;
for (size_t current_gen=0;current_gen<nr_original_gen;++current_gen){
if (local_perm_counter<nr_approx_points[current_gen]){
not_done=true;
overall_perm.push_back(local_perms[current_gen][local_perm_counter]);
}
}
++local_perm_counter;
}
assert(overall_perm.size()==nr_gen);
// sort the generators according to the permutations
Generators.order_rows_by_perm(overall_perm);
}
if(!use_existing_facets){
if(multithreaded_pyramid){
HypCounter.resize(omp_get_max_threads());
for(size_t i=0;i<HypCounter.size();++i)
HypCounter[i]=i+1;
} else{
HypCounter.resize(1);
HypCounter[0]=1;
}
find_and_evaluate_start_simplex();
}
size_t last_to_be_inserted; // good to know in case of do_all_hyperplanes==false
last_to_be_inserted=nr_gen-1; // because we don't need to compute support hyperplanes in this case
for(int j=nr_gen-1;j>=0;--j){
if(!in_triang[j]){
last_to_be_inserted=j;
break;
}
} // last_to_be_inserted now determined
bool is_new_generator;
typename list< FACETDATA >::iterator l;
bool check_original_gens=true;
for (size_t i=start_from;i<nr_gen;++i) {
INTERRUPT_COMPUTATION_BY_EXCEPTION
time_t start,end;
time (&start);
start_from=i;
if (in_triang[i])
continue;
if (!is_pyramid && is_approximation) steps_in_approximation++;
// we check whether all original generators are contained in the current cone
if (!is_pyramid && is_approximation && check_original_gens){
if (verbose)
verboseOutput() << "Check...";
size_t current_gen=0;
l=Facets.begin();
for (;l!=Facets.end();++l){
if (l->is_positive_on_all_original_gens) continue;
for (current_gen=0;current_gen<nr_original_gen;++current_gen){
if (!(v_scalar_product(l->Hyp,OriginalGenerators[current_gen])>=0)) {
l->is_negative_on_some_original_gen=true;
check_original_gens=false;
break;
}
}
if (current_gen==nr_original_gen){
l->is_positive_on_all_original_gens=true;
} else {
break;
}
}
if(verbose)
verboseOutput() << " done." << endl;
// now we need to stop
if (l==Facets.end()){
if(verbose)
verboseOutput() << "The original cone is now contained." << endl;
break;
}
}
if(do_triangulation && TriangulationBufferSize > 2*RecBoundTriang) // emermergency brake
tri_recursion=true; // to switch off production of simplices in favor
// of non-recursive pyramids
Integer scalar_product;
is_new_generator=false;
l=Facets.begin();
old_nr_supp_hyps=Facets.size(); // Facets will be xtended in the loop
long long nr_pos=0, nr_neg=0;
long long nr_neg_simp=0, nr_pos_simp=0;
vector<Integer> L;
#ifndef NCATCH
std::exception_ptr tmp_exception;
#endif
size_t lpos=0;
#pragma omp parallel for private(L,scalar_product) firstprivate(lpos,l) reduction(+: nr_pos, nr_neg)
for (size_t k=0; k<old_nr_supp_hyps; k++) {
#ifndef NCATCH
try {
#endif
for(;k > lpos; lpos++, l++) ;
for(;k < lpos; lpos--, l--) ;
L=Generators[i];
scalar_product=v_scalar_product(L,(*l).Hyp);
l->ValNewGen=scalar_product;
if (scalar_product<0) {
is_new_generator=true;
nr_neg++;
if(l->simplicial)
#pragma omp atomic
nr_neg_simp++;
}
if (scalar_product>0) {
nr_pos++;
if(l->simplicial)
#pragma omp atomic
nr_pos_simp++;
}
#ifndef NCATCH
} catch(const std::exception& ) {
tmp_exception = std::current_exception();
}
#endif
} //end parallel for
#ifndef NCATCH
if (!(tmp_exception == 0)) std::rethrow_exception(tmp_exception);
#endif
if(!is_new_generator)
continue;
// the i-th generator is used in the triangulation
// in_triang[i]=true; // now at end of loop
if (deg1_triangulation && isComputed(ConeProperty::Grading))
deg1_triangulation = (gen_degrees[i] == 1);
if (!omp_in_parallel())
try_offload(0);
/* if(!is_pyramid && verbose )
verboseOutput() << "Neg " << nr_neg << " Pos " << nr_pos << " NegSimp " <<nr_neg_simp << " PosSimp " <<nr_pos_simp << endl; */
// First we test whether to go to recursive pyramids because of too many supphyps
if (recursion_allowed && nr_neg*nr_pos-(nr_neg_simp*nr_pos_simp) > RecBoundSuppHyp) { // use pyramids because of supphyps
if(!is_pyramid && verbose )
verboseOutput() << "Building pyramids" << endl;
if (do_triangulation)
tri_recursion = true; // We can not go back to classical triangulation
if(check_evaluation_buffer()){
Top_Cone->evaluate_triangulation();
}
process_pyramids(i,true); //recursive
lastGen=i;
nextGen=i+1;
}
else{ // now we check whether to go to pyramids because of the size of triangulation
// once we have done so, we must stay with it
if( tri_recursion || (do_triangulation
&& (nr_neg*TriangulationBufferSize > RecBoundTriang
|| 3*omp_get_max_threads()*TriangulationBufferSize>EvalBoundTriang ))){ // go to pyramids because of triangulation
if(check_evaluation_buffer()){
Top_Cone->evaluate_triangulation();
}
tri_recursion=true;
process_pyramids(i,false); //non-recursive
}
else{ // no pyramids necesary
if(do_partial_triangulation)
process_pyramids(i,false); // non-recursive
if(do_triangulation)
extend_triangulation(i);
}
if(do_all_hyperplanes || i!=last_to_be_inserted)
find_new_facets(i);
}
size_t nr_new_facets = Facets.size() - old_nr_supp_hyps;
time (&end);
/* double dif = difftime (end,start);
if (verbose) {
verboseOutput() << "Generator took " << dif << " sec " <<endl;
}*/
// removing the negative hyperplanes if necessary
if(do_all_hyperplanes || i!=last_to_be_inserted){
l=Facets.begin();
for (size_t j=0; j<old_nr_supp_hyps;j++){
if (l->ValNewGen<0) {
if (is_approximation && l->is_negative_on_some_original_gen){
check_original_gens = true;
}
l=Facets.erase(l);
}
else
++l;
}
}
GensInCone.push_back(i);
nrGensInCone++;
Comparisons.push_back(nrTotalComparisons);
if(verbose) {
verboseOutput() << "gen="<< i+1 <<", ";
if (do_all_hyperplanes || i!=last_to_be_inserted) {
verboseOutput() << Facets.size()<<" hyp, " << nr_new_facets << " new";
} else {
verboseOutput() << Support_Hyperplanes.nr_of_rows()<<" hyp";
}
if(nrPyramids[0]>0)
verboseOutput() << ", " << nrPyramids[0] << " pyr";
if(do_triangulation||do_partial_triangulation)
verboseOutput() << ", " << TriangulationBufferSize << " simpl";
verboseOutput()<< endl;
}
in_triang[i]=true;
} // loop over i
start_from=nr_gen;
if (is_pyramid && do_all_hyperplanes) // must give supphyps back to mother
Mother->select_supphyps_from(Facets, apex, Mother_Key);
INTERRUPT_COMPUTATION_BY_EXCEPTION
// transfer Facets --> SupportHyperplanes
if (do_all_hyperplanes) {
nrSupport_Hyperplanes = Facets.size();
Support_Hyperplanes = Matrix<Integer>(nrSupport_Hyperplanes,0);
typename list<FACETDATA>::iterator IHV=Facets.begin();
for (size_t i=0; i<nrSupport_Hyperplanes; ++i, ++IHV) {
swap(Support_Hyperplanes[i],IHV->Hyp);
}
is_Computed.set(ConeProperty::SupportHyperplanes);
}
Support_Hyperplanes.set_nr_of_columns(dim);
if(do_extreme_rays && do_all_hyperplanes)
compute_extreme_rays(true);
INTERRUPT_COMPUTATION_BY_EXCEPTION
transfer_triangulation_to_top(); // transfer remaining simplices to top
if(check_evaluation_buffer()){
Top_Cone->evaluate_triangulation();
}
if (!is_pyramid && is_approximation && verbose){
verboseOutput() << "Performed " << steps_in_approximation << "/" << nr_gen << " steps." << endl;
}
// } // end if (dim>0)
Facets.clear();
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::find_bottom_facets() {
if(verbose)
verboseOutput() << "Computing bottom decomposition" << endl;
vector<key_t> start_simpl=Generators.max_rank_submatrix_lex();
Order_Vector = vector<Integer>(dim,0);
for(size_t i=0;i<dim;++i)
for(size_t j=0;j<dim;++j)
Order_Vector[j]+=((unsigned long) (1+i%10))*Generators[start_simpl[i]][j];
// First the generators for the recession cone = our cone
Matrix<Integer> BottomGen(0,dim+1);
vector<Integer> help(dim+1);
for(size_t i=0;i<nr_gen;++i){
for(size_t j=0;j<dim; ++j)
help[j]=Generators[i][j];
help[dim]=0;
BottomGen.append(help);
}
// then the same vectors as generators of the bottom polyhedron
for(size_t i=0;i<nr_gen;++i){
for(size_t j=0;j<dim; ++j)
help[j]=Generators[i][j];
help[dim]=1;
BottomGen.append(help);
}
Full_Cone BottomPolyhedron(BottomGen);
BottomPolyhedron.verbose=verbose;
BottomPolyhedron.do_extreme_rays=true;
BottomPolyhedron.keep_order = true;
try {
BottomPolyhedron.dualize_cone(); // includes finding extreme rays
} catch(const NonpointedException& ){};
// transfer pointedness
assert( BottomPolyhedron.isComputed(ConeProperty::IsPointed) );
pointed = BottomPolyhedron.pointed;
is_Computed.set(ConeProperty::IsPointed);
// BottomPolyhedron.Support_Hyperplanes.pretty_print(cout);
help.resize(dim);
// find extreme rays of Bottom among the generators
vector<key_t> BottomExtRays;
for(size_t i=0;i<nr_gen;++i)
if(BottomPolyhedron.Extreme_Rays_Ind[i+nr_gen])
BottomExtRays.push_back(i);
/* vector<key_t> BottomExtRays; // can be used if the bool vector should not exist anymore
size_t start_search=0;
for(size_t i=0;i<ExtStrahl.nr_of_rows();++i){
if(BottomPolyhedron.ExtStrahl[i][dim]==1){
BottomPolyhedron.ExtStrahl[i].resize(dim);
for(size_t j=0;j<nr_gen;++j){
size_t k=(j+start_search) % nr_gen;
if(BottomPolyhedron.ExtStrahl[i]==Generators[k]){
BottomExtRays.push_back(k);
start_search++;
}
}
}
}*/
if(verbose)
verboseOutput() << "Bottom has " << BottomExtRays.size() << " extreme rays" << endl;
INTERRUPT_COMPUTATION_BY_EXCEPTION
Matrix<Integer> BottomFacets(0,dim);
vector<Integer> BottomDegs(0,dim);
if (!isComputed(ConeProperty::SupportHyperplanes)) {
Support_Hyperplanes = Matrix<Integer>(0,dim);
nrSupport_Hyperplanes=0;
}
for(size_t i=0;i<BottomPolyhedron.nrSupport_Hyperplanes;++i){
Integer test=BottomPolyhedron.Support_Hyperplanes[i][dim];
for(size_t j=0;j<dim;++j)
help[j]=BottomPolyhedron.Support_Hyperplanes[i][j];
if(test==0 && !isComputed(ConeProperty::SupportHyperplanes)){
Support_Hyperplanes.append(help);
nrSupport_Hyperplanes++;
}
if (test < 0){
BottomFacets.append(help);
BottomDegs.push_back(-test);
}
}
is_Computed.set(ConeProperty::SupportHyperplanes);
if (!pointed)
throw NonpointedException();
INTERRUPT_COMPUTATION_BY_EXCEPTION
vector<key_t> facet;
for(size_t i=0;i<BottomFacets.nr_of_rows();++i){
facet.clear();
for(size_t k=0;k<BottomExtRays.size();++k)
if(v_scalar_product(Generators[BottomExtRays[k]],BottomFacets[i])==BottomDegs[i])
facet.push_back(BottomExtRays[k]);
Pyramids[0].push_back(facet);
nrPyramids[0]++;
}
if(verbose)
verboseOutput() << "Bottom decomposition computed, " << nrPyramids[0] << " subcones" << endl;
}
template<typename Integer>
void Full_Cone<Integer>::start_message() {
if (verbose) {
verboseOutput()<<"************************************************************"<<endl;
verboseOutput()<<"starting primal algorithm ";
if (do_partial_triangulation) verboseOutput()<<"with partial triangulation ";
if (do_triangulation) {
verboseOutput()<<"with full triangulation ";
}
if (!do_triangulation && !do_partial_triangulation) verboseOutput()<<"(only support hyperplanes) ";
verboseOutput()<<"..."<<endl;
}
}
template<typename Integer>
void Full_Cone<Integer>::end_message() {
if (verbose) {
verboseOutput() << "------------------------------------------------------------"<<endl;
}
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::build_top_cone() {
OldCandidates.verbose=verbose;
NewCandidates.verbose=verbose;
if(dim==0)
return;
if( ( !do_bottom_dec || deg1_generated || dim==1 || (!do_triangulation && !do_partial_triangulation))) {
build_cone();
}
else{
find_bottom_facets();
start_from=nr_gen;
deg1_triangulation=false;
vector<list<vector<key_t> >::iterator > level0_order;
level0_order.reserve(nrPyramids[0]);
auto p=Pyramids[0].begin();
for(size_t k=0;k<nrPyramids[0];++k,++p){
level0_order.push_back(p);
}
for(size_t k=0;k<5*nrPyramids[0];++k){
swap(level0_order[rand()%nrPyramids[0]],level0_order[rand()%nrPyramids[0]]);
}
list<vector<key_t> > new_order;
for(size_t k=0;k<nrPyramids[0];++k){
new_order.push_back(*level0_order[k]);
}
Pyramids[0].clear();
Pyramids[0].splice(Pyramids[0].begin(),new_order);
}
// try_offload(0); // superfluous since tried immediately in evaluate_stored_pyramids(0)
evaluate_stored_pyramids(0); // force evaluation of remaining pyramids
#ifdef NMZ_MIC_OFFLOAD
if (_Offload_get_device_number() < 0) // dynamic check for being on CPU (-1)
{
evaluate_stored_pyramids(0); // previous run could have left over pyramids
mic_offloader.evaluate_triangulation();
}
#endif // NMZ_MIC_OFFLOAD
}
//---------------------------------------------------------------------------
template<typename Integer>
bool Full_Cone<Integer>::check_evaluation_buffer(){
return(omp_get_level()==omp_start_level && check_evaluation_buffer_size());
}
//---------------------------------------------------------------------------
template<typename Integer>
bool Full_Cone<Integer>::check_evaluation_buffer_size(){
return(!Top_Cone->keep_triangulation &&
Top_Cone->TriangulationBufferSize > EvalBoundTriang);
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::transfer_triangulation_to_top(){
size_t i;
// cout << "Pyr level " << pyr_level << endl;
if(!is_pyramid) { // we are in top cone
if(check_evaluation_buffer()){
evaluate_triangulation();
}
return; // no transfer necessary
}
// now we are in a pyramid
// cout << "In pyramid " << endl;
int tn = 0;
if (omp_in_parallel())
tn = omp_get_ancestor_thread_num(omp_start_level+1);
auto pyr_simp=TriangulationBuffer.begin();
while (pyr_simp!=TriangulationBuffer.end()) {
if (pyr_simp->height == 0) { // it was marked to be skipped
Top_Cone->FS[tn].splice(Top_Cone->FS[tn].end(), TriangulationBuffer, pyr_simp++);
--TriangulationBufferSize;
} else {
for (i=0; i<dim; i++) // adjust key to topcone generators
pyr_simp->key[i]=Top_Key[pyr_simp->key[i]];
sort(pyr_simp->key.begin(),pyr_simp->key.end());
++pyr_simp;
}
}
// cout << "Keys transferred " << endl;
#pragma omp critical(TRIANG)
{
Top_Cone->TriangulationBuffer.splice(Top_Cone->TriangulationBuffer.end(),TriangulationBuffer);
Top_Cone->TriangulationBufferSize += TriangulationBufferSize;
}
TriangulationBufferSize = 0;
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::get_supphyps_from_copy(bool from_scratch){
if(isComputed(ConeProperty::SupportHyperplanes)) // we have them already
return;
Full_Cone copy((*this).Generators);
copy.verbose=verbose;
if(!from_scratch){
copy.start_from=start_from;
copy.use_existing_facets=true;
copy.keep_order=true;
copy.HypCounter=HypCounter;
copy.Extreme_Rays_Ind=Extreme_Rays_Ind;
copy.in_triang=in_triang;
copy.old_nr_supp_hyps=old_nr_supp_hyps;
if(isComputed(ConeProperty::ExtremeRays))
copy.is_Computed.set(ConeProperty::ExtremeRays);
copy.GensInCone=GensInCone;
copy.nrGensInCone=nrGensInCone;
copy.Comparisons=Comparisons;
if(!Comparisons.empty())
copy.nrTotalComparisons=Comparisons[Comparisons.size()-1];
typename list< FACETDATA >::const_iterator l=Facets.begin();
for(size_t i=0;i<old_nr_supp_hyps;++i){
copy.Facets.push_back(*l);
++l;
}
}
copy.dualize_cone();
std::swap(Support_Hyperplanes,copy.Support_Hyperplanes);
nrSupport_Hyperplanes = copy.nrSupport_Hyperplanes;
is_Computed.set(ConeProperty::SupportHyperplanes);
do_all_hyperplanes = false;
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::update_reducers(bool forced){
if((!do_Hilbert_basis || do_module_gens_intcl) && !forced)
return;
if(NewCandidates.Candidates.empty())
return;
INTERRUPT_COMPUTATION_BY_EXCEPTION
if(hilbert_basis_rec_cone_known){
NewCandidates.sort_by_deg();
NewCandidates.reduce_by(HBRC);
ModuleGensDepot.merge(NewCandidates);
return;
}
if(nr_gen==dim) // no global reduction in the simplicial case
NewCandidates.sort_by_deg();
if(nr_gen!=dim || forced){ // global reduction in the nonsimplicial case (or forced)
NewCandidates.auto_reduce();
if(verbose){
verboseOutput() << "reducing " << OldCandidates.Candidates.size() << " candidates by " << NewCandidates.Candidates.size() << " reducers" << endl;
}
OldCandidates.reduce_by(NewCandidates);
}
OldCandidates.merge(NewCandidates);
CandidatesSize=OldCandidates.Candidates.size();
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::prepare_old_candidates_and_support_hyperplanes(){
if(!isComputed(ConeProperty::SupportHyperplanes)){
if (verbose) {
verboseOutput() << "**** Computing support hyperplanes for reduction:" << endl;
}
get_supphyps_from_copy(false);
}
check_pointed();
if(!pointed){
throw NonpointedException();
}
int max_threads = omp_get_max_threads();
size_t Memory_per_gen=8*nrSupport_Hyperplanes;
size_t max_nr_gen=RAM_Size/(Memory_per_gen*max_threads);
// cout << "max_nr_gen " << max_nr_gen << endl;
AdjustedReductionBound=max_nr_gen;
if(AdjustedReductionBound < 2000)
AdjustedReductionBound=2000;
Sorting=compute_degree_function();
if (!is_approximation) {
bool save_do_module_gens_intcl=do_module_gens_intcl;
do_module_gens_intcl=false; // to avoid multiplying sort_deg by 2 for the original generators
// sort_deg of new candiadtes will be multiplied by 2
// so that all old candidates are tested for reducibility
for (size_t i = 0; i <nr_gen; i++) {
// cout << gen_levels[i] << " ** " << Generators[i];
if(!inhomogeneous || gen_levels[i]==0 || (!save_do_module_gens_intcl && gen_levels[i]<=1)) {
OldCandidates.Candidates.push_back(Candidate<Integer>(Generators[i],*this));
OldCandidates.Candidates.back().original_generator=true;
}
}
for(size_t i=0;i<HilbertBasisRecCone.nr_of_rows();++i){
HBRC.Candidates.push_back(Candidate<Integer>(HilbertBasisRecCone[i],*this));
}
do_module_gens_intcl=save_do_module_gens_intcl; // restore
if(HilbertBasisRecCone.nr_of_rows()>0){ // early enough to avoid multiplictaion of sort_deg by 2 for the elements
// in HilbertBasisRecCone
hilbert_basis_rec_cone_known=true;
HBRC.sort_by_deg();
}
if(!do_module_gens_intcl) // if do_module_gens_intcl we don't want to change the original monoid
OldCandidates.auto_reduce();
else
OldCandidates.sort_by_deg();
}
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::evaluate_triangulation(){
// prepare reduction
if (do_Hilbert_basis && OldCandidates.Candidates.empty()) {
prepare_old_candidates_and_support_hyperplanes();
}
if (TriangulationBufferSize == 0)
return;
assert(omp_get_level()==omp_start_level);
const long VERBOSE_STEPS = 50;
long step_x_size = TriangulationBufferSize-VERBOSE_STEPS;
if (verbose) {
verboseOutput() << "evaluating "<<TriangulationBufferSize<<" simplices" <<endl;
/* verboseOutput() << "---------+---------+---------+---------+---------+"
<< " (one | per 2%)" << endl;*/
}
totalNrSimplices += TriangulationBufferSize;
if(do_Stanley_dec || keep_triangulation){ // in these cases sorting is necessary
auto simp=TriangulationBuffer.begin();
for(;simp!=TriangulationBuffer.end();++simp)
sort(simp->key.begin(),simp->key.end());
}
if(do_evaluation && !do_only_multiplicity) {
deque<bool> done(TriangulationBufferSize,false);
bool skip_remaining;
#ifndef NCATCH
std::exception_ptr tmp_exception;
#endif
do{ // allows multiple run of loop below in case of interruption for the update of reducers
skip_remaining=false;
step_x_size = TriangulationBufferSize-VERBOSE_STEPS;
#pragma omp parallel
{
typename list< SHORTSIMPLEX<Integer> >::iterator s = TriangulationBuffer.begin();
size_t spos=0;
int tn = omp_get_thread_num();
#pragma omp for schedule(dynamic) nowait
for(size_t i=0; i<TriangulationBufferSize; i++){
#ifndef NCATCH
try {
#endif
if(skip_remaining)
continue;
for(; i > spos; ++spos, ++s) ;
for(; i < spos; --spos, --s) ;
INTERRUPT_COMPUTATION_BY_EXCEPTION
if(done[spos])
continue;
done[spos]=true;
/* if(keep_triangulation || do_Stanley_dec) -- now done above
sort(s->key.begin(),s->key.end()); */
if(!SimplexEval[tn].evaluate(*s)){
#pragma omp critical(LARGESIMPLEX)
LargeSimplices.push_back(SimplexEval[tn]);
}
if (verbose) {
#pragma omp critical(VERBOSE)
while ((long)(i*VERBOSE_STEPS) >= step_x_size) {
step_x_size += TriangulationBufferSize;
verboseOutput() << "|" <<flush;
}
}
if(do_Hilbert_basis && Results[tn].get_collected_elements_size() > AdjustedReductionBound)
skip_remaining=true;
#ifndef NCATCH
} catch(const std::exception& ) {
tmp_exception = std::current_exception();
skip_remaining = true;
#pragma omp flush(skip_remaining)
}
#endif
}
Results[tn].transfer_candidates();
} // end parallel
#ifndef NCATCH
if (!(tmp_exception == 0)) std::rethrow_exception(tmp_exception);
#endif
if (verbose)
verboseOutput() << endl;
update_reducers();
} while(skip_remaining);
} // do_evaluation
if (verbose)
{
verboseOutput() << totalNrSimplices << " simplices";
if(do_Hilbert_basis)
verboseOutput() << ", " << CandidatesSize << " HB candidates";
if(do_deg1_elements)
verboseOutput() << ", " << CandidatesSize << " deg1 vectors";
verboseOutput() << " accumulated." << endl;
}
if (keep_triangulation) {
Triangulation.splice(Triangulation.end(),TriangulationBuffer);
} else {
// #pragma omp critical(FREESIMPL)
FreeSimpl.splice(FreeSimpl.begin(),TriangulationBuffer);
}
TriangulationBufferSize=0;
if (verbose && use_bottom_points) {
size_t lss=LargeSimplices.size();
if(lss>0)
verboseOutput() << lss << " large simplices stored" << endl;
}
for(size_t i=0;i<Results.size();++i)
Results[i].transfer_candidates(); // any remaining ones
update_reducers();
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::evaluate_large_simplices(){
size_t lss = LargeSimplices.size();
if (lss == 0)
return;
assert(omp_get_level()==omp_start_level);
if (verbose) {
verboseOutput() << "Evaluating " << lss << " large simplices" << endl;
}
size_t j;
for (j = 0; j < lss; ++j) {
INTERRUPT_COMPUTATION_BY_EXCEPTION
evaluate_large_simplex(j, lss);
}
// decomposition might have created new simplices -- NO LONGER, now in Pyramids[0]
// evaluate_triangulation();
// also new large simplices are possible
/* if (!LargeSimplices.empty()) {
use_bottom_points = false;
lss += LargeSimplices.size();
if (verbose) {
verboseOutput() << "Continue evaluation of " << lss << " large simplices without new decompositions of simplicial cones." << endl;
}
for (; j < lss; ++j) {
INTERRUPT_COMPUTATION_BY_EXCEPTION
evaluate_large_simplex(j, lss);
}
}*/
assert(LargeSimplices.empty());
for(size_t i=0;i<Results.size();++i)
Results[i].transfer_candidates(); // any remaining ones
update_reducers();
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::evaluate_large_simplex(size_t j, size_t lss) {
if (verbose) {
verboseOutput() << "Large simplex " << j+1 << " / " << lss << endl;
}
if (do_deg1_elements && !do_h_vector && !do_Stanley_dec && !deg1_triangulation) {
compute_deg1_elements_via_projection_simplicial(LargeSimplices.front().get_key());
}
else {
LargeSimplices.front().Simplex_parallel_evaluation();
if (do_Hilbert_basis && Results[0].get_collected_elements_size() > AdjustedReductionBound) {
Results[0].transfer_candidates();
update_reducers();
}
}
LargeSimplices.pop_front();
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::compute_deg1_elements_via_projection_simplicial(const vector<key_t>& key){
Matrix<Integer> Gens=Generators.submatrix(key);
Sublattice_Representation<Integer> NewCoordinates=LLL_coordinates<Integer,Integer>(Gens);
Matrix<Integer> Gred=NewCoordinates.to_sublattice(Gens);
vector<Integer> GradT=NewCoordinates.to_sublattice_dual(Grading);
Matrix<Integer> GradMat(0,dim);
GradMat.append(GradT);
Cone<Integer> ProjCone(Type::cone,Gred,Type::grading, GradMat);
if(using_GMP<Integer>())
ProjCone.compute(ConeProperty::Projection,ConeProperty::NoLLL,ConeProperty::BigInt);
else
ProjCone.compute(ConeProperty::Projection,ConeProperty::NoLLL);
Matrix<Integer> Deg1=ProjCone.getDeg1ElementsMatrix();
Deg1=NewCoordinates.from_sublattice(Deg1);
Matrix<Integer> Supp=ProjCone.getSupportHyperplanesMatrix();
Supp=NewCoordinates.from_sublattice_dual(Supp);
/*for(size_t i=0;i<dim;++i)
for(size_t j=0;j<dim;++j)
assert(v_scalar_product(Supp[i],Gens[j])>=0); */
vector<bool> Excluded(dim,false); // we want to discard duplicates
for(size_t i=0;i<dim;++i){
Integer test=v_scalar_product(Supp[i],Order_Vector);
if(test>0)
continue;
if(test<0){
Excluded[i]=true;
continue;
}
size_t j;
for(j=0;j<dim;++j){
if(Supp[i][j]!=0)
break;
}
if(Supp[i][j]<0)
Excluded[i]=true;
}
typename vector<vector<Integer> >::const_iterator E;
for(E=Deg1.get_elements().begin();E!=Deg1.get_elements().end();++E){
size_t i;
for(i=0;i<dim;++i)
if(v_scalar_product(*E,Supp[i])==0 && Excluded[i])
break;
if(i<dim)
continue;
for(i=0;i<dim;++i) // exclude original generators
if(*E==Gens[i])
break;
if(i==dim){
Results[0].Deg1_Elements.push_back(*E); // Results[0].Deg1_Elements.push_back(*E);
Results[0].collected_elements_size++;
}
}
Results[0].transfer_candidates();
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::remove_duplicate_ori_gens_from_HB(){
return; //TODO reactivate!
set<vector<Integer> > OriGens;
typename list<Candidate<Integer> >:: iterator c=OldCandidates.Candidates.begin();
typename set<vector<Integer> >:: iterator found;
for(;c!=OldCandidates.Candidates.end();){
if(!c->original_generator){
++c;
continue;
}
found=OriGens.find(c->cand);
if(found!=OriGens.end()){
c=OldCandidates.Candidates.erase(c);
}
else{
if(c->original_generator)
OriGens.insert(c->cand);
++c;
}
}
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::primal_algorithm(){
primal_algorithm_initialize();
/***** Main Work is done in build_top_cone() *****/
build_top_cone(); // evaluates if keep_triangulation==false
/***** Main Work is done in build_top_cone() *****/
check_pointed();
if(!pointed){
throw NonpointedException();
}
primal_algorithm_finalize();
primal_algorithm_set_computed();
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::primal_algorithm_initialize() {
prepare_inclusion_exclusion();
SimplexEval = vector< SimplexEvaluator<Integer> >(omp_get_max_threads(),SimplexEvaluator<Integer>(*this));
for(size_t i=0;i<SimplexEval.size();++i)
SimplexEval[i].set_evaluator_tn(i);
Results = vector< Collector<Integer> >(omp_get_max_threads(),Collector<Integer>(*this));
Hilbert_Series.setVerbose(verbose);
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::primal_algorithm_finalize() {
if (isComputed(ConeProperty::Grading) && !deg1_generated) {
deg1_triangulation = false;
}
if (keep_triangulation) {
is_Computed.set(ConeProperty::Triangulation);
}
if (do_cone_dec) {
is_Computed.set(ConeProperty::ConeDecomposition);
}
evaluate_triangulation();
assert(nrPyramids[0]==0);
evaluate_large_simplices(); // can produce level 0 pyramids
use_bottom_points=false; // block new attempts for subdivision
evaluate_stored_pyramids(0); // in case subdivision took place
evaluate_triangulation();
FreeSimpl.clear();
compute_class_group();
// collect accumulated data from the SimplexEvaluators
for (int zi=0; zi<omp_get_max_threads(); zi++) {
detSum += Results[zi].getDetSum();
multiplicity += Results[zi].getMultiplicitySum();
if (do_h_vector) {
Hilbert_Series += Results[zi].getHilbertSeriesSum();
}
}
#ifdef NMZ_MIC_OFFLOAD
// collect accumulated data from mics
if (_Offload_get_device_number() < 0) // dynamic check for being on CPU (-1)
{
mic_offloader.finalize();
}
#endif // NMZ_MIC_OFFLOAD
if (do_h_vector) {
Hilbert_Series.collectData();
}
if(verbose) {
verboseOutput() << "Total number of pyramids = "<< totalNrPyr << ", among them simplicial " << nrSimplicialPyr << endl;
// cout << "Uni "<< Unimod << " Ht1NonUni " << Ht1NonUni << " NonDecided " << NonDecided << " TotNonDec " << NonDecidedHyp<< endl;
if(do_only_multiplicity)
verboseOutput() << "Determinants computed = " << TotDet << endl;
/* if(NrCompVect>0)
cout << "Vector comparisons " << NrCompVect << " Value comparisons " << NrCompVal
<< " Average " << NrCompVal/NrCompVect+1 << endl; */
}
if(verbose && GMP_hyp+GMP_scal_prod+GMP_mat>0)
verboseOutput() << "GMP transitions: matrices " << GMP_mat << " hyperplanes " << GMP_hyp << " vector operations " << GMP_scal_prod << endl;
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::make_module_gens(){
if(!inhomogeneous){
NewCandidates.extract(ModuleGeneratorsOverOriginalMonoid);
vector<Integer> Zero(dim,0);
ModuleGeneratorsOverOriginalMonoid.push_front(Zero);
// cout << "Mod " << endl;
// Matrix<Integer>(ModuleGeneratorsOverOriginalMonoid).pretty_print(cout);
// cout << "--------" << endl;
is_Computed.set(ConeProperty::ModuleGeneratorsOverOriginalMonoid,true);
return;
}
CandidateList<Integer> Level1OriGens;
for(size_t i=0;i<nr_gen;++i){
if(gen_levels[i]==1){
Level1OriGens.push_back(Candidate<Integer>(Generators[i],*this));
}
}
CandidateList<Integer> Level1Generators=Level1OriGens;
Candidate<Integer> new_cand(dim,Support_Hyperplanes.nr_of_rows());
typename list<Candidate<Integer> >::const_iterator lnew,l1;
for(lnew=NewCandidates.Candidates.begin();lnew!=NewCandidates.Candidates.end();++lnew){
INTERRUPT_COMPUTATION_BY_EXCEPTION
Integer level=v_scalar_product(lnew->cand,Truncation);
if(level==1){
new_cand=*lnew;
Level1Generators.reduce_by_and_insert(new_cand,OldCandidates);
}
else{
for(l1=Level1OriGens.Candidates.begin();l1!=Level1OriGens.Candidates.end();++l1){
new_cand=sum(*l1,*lnew);
Level1Generators.reduce_by_and_insert(new_cand,OldCandidates);
}
}
}
Level1Generators.extract(ModuleGeneratorsOverOriginalMonoid);
ModuleGeneratorsOverOriginalMonoid.sort();
ModuleGeneratorsOverOriginalMonoid.unique();
is_Computed.set(ConeProperty::ModuleGeneratorsOverOriginalMonoid,true);
for (size_t i = 0; i <nr_gen; i++) { // the level 1 input generators have not yet ben inserted into OldCandidates
if(gen_levels[i]==1) { // but they are needed for the truncated Hilbert basis computation
NewCandidates.Candidates.push_back(Candidate<Integer>(Generators[i],*this));
NewCandidates.Candidates.back().original_generator=true;
}
}
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::make_module_gens_and_extract_HB(){
make_module_gens();
NewCandidates.divide_sortdeg_by2(); // was previously multplied by 2
NewCandidates.sort_by_deg();
OldCandidates.merge(NewCandidates);
OldCandidates.auto_reduce();
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::primal_algorithm_set_computed() {
extreme_rays_and_deg1_check();
if(!pointed){
throw NonpointedException();
}
if (do_triangulation || do_partial_triangulation) {
is_Computed.set(ConeProperty::TriangulationSize,true);
if (do_evaluation) {
is_Computed.set(ConeProperty::TriangulationDetSum,true);
}
}
if (do_triangulation && do_evaluation && isComputed(ConeProperty::Grading))
is_Computed.set(ConeProperty::Multiplicity,true);
INTERRUPT_COMPUTATION_BY_EXCEPTION
if (do_Hilbert_basis) {
if(hilbert_basis_rec_cone_known){
// OldCandidates.Candidates.clear();
OldCandidates.merge(HBRC);
OldCandidates.merge(ModuleGensDepot);
}
if(do_module_gens_intcl){
make_module_gens_and_extract_HB();
}
OldCandidates.sort_by_val();
OldCandidates.extract(Hilbert_Basis);
OldCandidates.Candidates.clear();
Hilbert_Basis.unique();
is_Computed.set(ConeProperty::HilbertBasis,true);
if (isComputed(ConeProperty::Grading)) {
select_deg1_elements();
check_deg1_hilbert_basis();
}
}
INTERRUPT_COMPUTATION_BY_EXCEPTION
if (do_deg1_elements) {
for(size_t i=0;i<nr_gen;i++)
if(v_scalar_product(Grading,Generators[i])==1 && (!(is_approximation || is_global_approximation)
|| subcone_contains(Generators[i])))
Deg1_Elements.push_front(Generators[i]);
is_Computed.set(ConeProperty::Deg1Elements,true);
Deg1_Elements.sort();
Deg1_Elements.unique();
}
INTERRUPT_COMPUTATION_BY_EXCEPTION
if (do_h_vector) {
Hilbert_Series.setShift(convertTo<long>(shift));
Hilbert_Series.adjustShift();
// now the shift in the HilbertSeries may change and we would have to adjust
// the shift, the grading and more in the Full_Cone to continue to add data!
// COMPUTE HSOP here
if (do_hsop){
compute_hsop();
is_Computed.set(ConeProperty::HSOP);
}
Hilbert_Series.simplify();
is_Computed.set(ConeProperty::HilbertSeries);
}
if(do_Stanley_dec){
is_Computed.set(ConeProperty::StanleyDec);
}
// If the grading has gcd > 1 on the recession monoid,
// we must multiply the multiplicity by it.
// Without this correction, the multiplicity (relative to deg/g)
// is divided by g^r, but it must be g^{r-1}.
// We determine g and multiply by it.
//
// The reason behind this correction is that the determinants
// are computed with respect to a basis in which the
// basic simplex has volume 1/g instead of 1.
// The correction above takes care of this "mistake"
// that we are forced to make in order to keep data integral.
if(isComputed(ConeProperty::Multiplicity)){
Integer corr_factor;
if(!inhomogeneous)
corr_factor=v_gcd(Grading);
if(inhomogeneous && level0_dim==0)
corr_factor=1;
if(inhomogeneous && level0_dim>0){
Matrix<Integer> Level0Space=ProjToLevel0Quot.kernel();
corr_factor=0;
for(size_t i=0;i<Level0Space.nr_of_rows();++i)
corr_factor=libnormaliz::gcd(corr_factor,v_scalar_product(Grading,Level0Space[i]));
}
multiplicity*=convertTo<mpz_class>(corr_factor);
}
}
//---------------------------------------------------------------------------
// Normaliz modes (public)
//---------------------------------------------------------------------------
// check the do_* bools, they must be set in advance
// this method (de)activate them according to dependencies between them
template<typename Integer>
void Full_Cone<Integer>::do_vars_check(bool with_default) {
do_extreme_rays=true; // we always want to do this if compute() is called
/* if (do_default_mode && with_default) {
do_Hilbert_basis = true;
do_h_vector = true;
if(!inhomogeneous)
do_class_group=true;
}
*/
if (do_integrally_closed) {
if (do_Hilbert_basis) {
do_integrally_closed = false; // don't interrupt the computation
} else {
do_Hilbert_basis = true;
}
}
// activate implications
if (do_module_gens_intcl) do_Hilbert_basis= true;
if (do_module_gens_intcl) use_bottom_points= false;
//if (do_hsop) do_Hilbert_basis = true;
if (do_Stanley_dec) keep_triangulation = true;
if (do_cone_dec) keep_triangulation = true;
if (keep_triangulation) do_determinants = true;
if (do_multiplicity) do_determinants = true;
if ((do_multiplicity || do_h_vector) && inhomogeneous) do_module_rank = true;
if (do_determinants) do_triangulation = true;
if (do_h_vector && (with_default || explicit_h_vector)) do_triangulation = true;
if (do_deg1_elements) do_partial_triangulation = true;
if (do_Hilbert_basis) do_partial_triangulation = true;
// activate
do_only_multiplicity = do_determinants;
stop_after_cone_dec = true;
if(do_cone_dec) do_only_multiplicity=false;
if (do_Stanley_dec || do_h_vector || do_deg1_elements
|| do_Hilbert_basis) {
do_only_multiplicity = false;
stop_after_cone_dec = false;
do_evaluation = true;
}
if (do_determinants) do_evaluation = true;
// deactivate
if (do_triangulation) do_partial_triangulation = false;
if (do_Hilbert_basis) do_deg1_elements = false; // they will be extracted later
}
// general purpose compute method
// do_* bools must be set in advance, this method does sanity checks for it
// if no bool is set it does support hyperplanes and extreme rays
template<typename Integer>
void Full_Cone<Integer>::compute() {
omp_start_level=omp_get_level();
if(dim==0){
set_zero_cone();
return;
}
do_vars_check(false);
explicit_full_triang=do_triangulation; // to distinguish it from do_triangulation via default mode
if(do_default_mode)
do_vars_check(true);
if(inhomogeneous){
if(do_default_mode && !do_Hilbert_basis && !isComputed(ConeProperty::Grading) &&isComputed(ConeProperty::ExtremeRays))
return;
}
start_message();
if(Support_Hyperplanes.nr_of_rows()==0 && !do_Hilbert_basis && !do_h_vector && !do_multiplicity && !do_deg1_elements
&& !do_Stanley_dec && !do_triangulation && !do_determinants)
assert(Generators.max_rank_submatrix_lex().size() == dim);
minimize_support_hyperplanes(); // if they are given
if (inhomogeneous)
set_levels();
check_given_grading();
if ((!do_triangulation && !do_partial_triangulation)
|| (Grading.size()>0 && !isComputed(ConeProperty::Grading))){
// in the second case there are only two possibilities:
// either nonpointed or bad grading
do_triangulation=false;
do_partial_triangulation=false;
support_hyperplanes();
}
else{
// look for a grading if it is needed
find_grading();
if(isComputed(ConeProperty::IsPointed) && !pointed){
end_message();
return;
}
if (!isComputed(ConeProperty::Grading))
disable_grading_dep_comp();
bool polyhedron_is_polytope=inhomogeneous;
if(inhomogeneous){
find_level0_dim();
for(size_t i=0;i<nr_gen;++i)
if(gen_levels[i]==0){
polyhedron_is_polytope=false;
break;
}
}
set_degrees();
sort_gens_by_degree(true);
if(do_approximation && !deg1_generated){
if(!isComputed(ConeProperty::ExtremeRays) || !isComputed(ConeProperty::SupportHyperplanes)){
do_extreme_rays=true;
dualize_cone(false);// no start or end message
}
if(verbose)
verboseOutput() << "Approximating rational by lattice polytope" << endl;
if(do_deg1_elements){
compute_deg1_elements_via_approx_global();
is_Computed.set(ConeProperty::Deg1Elements,true);
if(do_triangulation){
do_deg1_elements=false;
do_partial_triangulation=false;
do_only_multiplicity = do_determinants;
primal_algorithm();
}
} else { // now we want subdividing elements for a simplicial cone
assert(do_Hilbert_basis);
compute_elements_via_approx(Hilbert_Basis);
}
}
else{
if(polyhedron_is_polytope && (do_Hilbert_basis || do_h_vector || do_multiplicity)){ // inthis situation we must find the
convert_polyhedron_to_polytope(); // lattice points in a polytope
}
else{
if(do_partial_triangulation || do_triangulation)
primal_algorithm();
else
return;
}
}
if(inhomogeneous){
find_module_rank();
// cout << "module rank " << module_rank << endl;
}
}
end_message();
}
// compute the degree vector of a hsop
template<typename Integer>
vector<Integer> degrees_hsop(const vector<Integer> gen_degrees,const vector<size_t> heights){
vector<Integer> hsop(heights.back());
hsop[0]=gen_degrees[0];
size_t k=1;
while (k<heights.size() && heights[k]>heights[k-1]){
hsop[k]=gen_degrees[k];
k++;
}
size_t j=k;
for (size_t i=k;i<heights.size();i++){
if (heights[i]>heights[i-1]){
hsop[j]=v_lcm_to(gen_degrees,k,i);
j++;
}
}
return hsop;
}
template<typename Integer>
void Full_Cone<Integer>::compute_hsop(){
vector<long> hsop_deg(dim,1);
// if all extreme rays are in degree one, there is nothing to compute
if (!isDeg1ExtremeRays()){
if(verbose){
verboseOutput() << "Computing heights ... " << flush;
}
vector<bool> choice = Extreme_Rays_Ind;
if (inhomogeneous){
for (size_t i=0; i<Generators.nr_of_rows(); i++) {
if (Extreme_Rays_Ind[i] && v_scalar_product(Generators[i],Truncation) != 0) {
choice[i]=false;
}
}
}
Matrix<Integer> ER = Generators.submatrix(choice);
Matrix<Integer> SH = getSupportHyperplanes();
if (inhomogeneous){
Sublattice_Representation<Integer> recession_lattice(ER,true);
Matrix<Integer> SH_raw = recession_lattice.to_sublattice_dual(SH);
Matrix<Integer> ER_embedded = recession_lattice.to_sublattice(ER);
Full_Cone<Integer> recession_cone(ER_embedded);
recession_cone.Support_Hyperplanes = SH_raw;
recession_cone.dualize_cone();
SH = recession_lattice.from_sublattice_dual(recession_cone.getSupportHyperplanes());
}
vector<size_t> ideal_heights(ER.nr_of_rows(),1);
// the heights vector is clear in the simplicial case
if (is_simplicial){
for (size_t j=0;j<ideal_heights.size();j++) ideal_heights[j]=j+1;
} else {
list<pair<boost::dynamic_bitset<> , size_t> > facet_list;
list<vector<key_t> > facet_keys;
vector<key_t> key;
size_t d = dim;
if (inhomogeneous) d = level0_dim;
for (size_t i=SH.nr_of_rows();i-->0;){
boost::dynamic_bitset<> new_facet(ER.nr_of_rows());
key.clear();
for (size_t j=0;j<ER.nr_of_rows();j++){
if (v_scalar_product(SH[i],ER[j])==0){
new_facet[new_facet.size()-1-j]=1;
} else {
key.push_back(j);
}
}
facet_list.push_back(make_pair(new_facet,d-1));
facet_keys.push_back(key);
}
facet_list.sort(); // should be sorted lex
//~ cout << "FACETS:" << endl;
//~ //cout << "size: " << facet_list.size() << " | " << facet_list << endl;
//~ for (auto jt=facet_list.begin();jt!=facet_list.end();++jt){
//~ cout << jt->first << " | " << jt->second << endl;
//~ }
//cout << "facet non_keys: " << facet_keys << endl;
heights(facet_keys,facet_list,ER.nr_of_rows()-1,ideal_heights,d-1);
}
if(verbose){
verboseOutput() << "done." << endl;
assert(ideal_heights[ER.nr_of_rows()-1]==dim);
verboseOutput() << "Heights vector: " << ideal_heights << endl;
}
vector<Integer> er_deg = ER.MxV(Grading);
hsop_deg = convertTo<vector<long> >(degrees_hsop(er_deg,ideal_heights));
}
if(verbose){
verboseOutput() << "Degrees of HSOP: " << hsop_deg << endl;
}
Hilbert_Series.setHSOPDenom(hsop_deg);
}
// recursive method to compute the heights
// TODO: at the moment: facets are a parameter. global would be better
template<typename Integer>
void Full_Cone<Integer>::heights(list<vector<key_t> >& facet_keys,list<pair<boost::dynamic_bitset<>,size_t> > faces, size_t index,vector<size_t>& ideal_heights,size_t max_dim){
// since we count the index backwards, this is the actual nr of the extreme ray
size_t ER_nr = ideal_heights.size()-index-1;
//~ cout << "starting calculation for extreme ray nr " << ER_nr << endl;
list<pair<boost::dynamic_bitset<>,size_t> > not_faces;
auto face_it=faces.begin();
for (;face_it!=faces.end();++face_it){
if (face_it->first.test(index)){ // check whether index is set
break;
}
// resize not_faces
face_it->first.resize(index);
}
not_faces.splice(not_faces.begin(),faces,faces.begin(),face_it);
//~ cout << "faces not containing it:" << endl;
//~ for (auto jt=not_faces.begin();jt!=not_faces.end();++jt){
//~ cout << jt->first << " | " << jt->second << endl;
//~ }
//~ cout << "faces containing it:" << endl;
//~ for (auto jt=faces.begin();jt!=faces.end();++jt){
//~ cout << jt->first << " | " << jt->second << endl;
//~ }
auto not_faces_it=not_faces.begin();
// update the heights
if (ER_nr>0){
if (!not_faces.empty()){
ideal_heights[ER_nr] = ideal_heights[ER_nr-1];
// compute the dimensions of not_faces
vector<bool> choice = Extreme_Rays_Ind;
if (inhomogeneous){
for (size_t i=0; i<Generators.nr_of_rows(); i++) {
if (Extreme_Rays_Ind[i] && v_scalar_product(Generators[i],Truncation) != 0) {
choice[i]=false;
}
}
}
Matrix<Integer> ER = Generators.submatrix(choice);
int tn;
if(omp_get_level()==omp_start_level)
tn=0;
else tn = omp_get_ancestor_thread_num(omp_start_level+1);
Matrix<Integer>& Test = Top_Cone->RankTest[tn];
vector<key_t> face_key;
for (;not_faces_it!=not_faces.end();++not_faces_it){
if (not_faces_it->second==0){ // dimension has not yet been computed
// generate the key vector
face_key.resize(0);
for (size_t i=0;i<not_faces_it->first.size();++i){
if (not_faces_it->first.test(i)){
face_key.push_back(ER.nr_of_rows()-1-i);
}
}
not_faces_it->second = Test.rank_submatrix(ER,face_key);
}
if (not_faces_it->second==max_dim) break;
}
if (not_faces_it==not_faces.end()) {
--max_dim;
ideal_heights[ER_nr] = ideal_heights[ER_nr-1]+1;
}
} else {
ideal_heights[ER_nr] = ideal_heights[ER_nr-1]+1;
--max_dim;
}
}
// we computed all the heights
if (index==0) return;
// if inner point, we can skip now
// take the union of all faces not containing the current extreme ray
boost::dynamic_bitset<> union_faces(index);
not_faces_it = not_faces.begin();
for (;not_faces_it!=not_faces.end();++not_faces_it){
union_faces |= not_faces_it->first; // take the union
}
//cout << "Their union: " << union_faces << endl;
// the not_faces now already have a size one smaller
union_faces.resize(index+1);
list<pair<boost::dynamic_bitset<>,size_t> > new_faces;
// delete all facets which only consist of the previous extreme rays
auto facet_it=facet_keys.begin();
size_t counter=0;
while(facet_it!=facet_keys.end()){
INTERRUPT_COMPUTATION_BY_EXCEPTION
counter=0;
for (size_t i=0;i<facet_it->size();i++){
if (facet_it->at(i)<=ER_nr) continue;
// now we only have new extreme rays
counter = i;
break;
}
size_t j=ER_nr+1;
for (;j<ideal_heights.size();j++){
if (facet_it->at(counter)!=j){ // facet contains the element j
break;
} else if (counter < facet_it->size()-1) counter++;
}
if (j==ideal_heights.size()){
facet_it = facet_keys.erase(facet_it);
} else ++facet_it;
}
facet_it=facet_keys.begin();
// main loop
for (;facet_it!=facet_keys.end();++facet_it){
INTERRUPT_COMPUTATION_BY_EXCEPTION
// check whether the facet is contained in the faces not containing the generator
// and the previous generators
// and check whether the generator is in the facet
// check whether intersection with facet contributes
bool not_containing_el =false;
// bool whether the facet contains an element which is NOT in the faces not containing the current extreme ray
bool containing_critical_el=false;
counter=0;
//cout << "check critical for facet " << *it << endl;
for (size_t i=0;i<facet_it->size();i++){
if (facet_it->at(i)==ER_nr){
not_containing_el = true;
}
if (facet_it->at(i)<=ER_nr && i<facet_it->size()-1) continue;
counter=i; // now we have elements which are bigger than the current extreme ray
if (not_containing_el){
for (size_t j=ER_nr+1;j<ideal_heights.size();j++){
if (facet_it->at(counter)!=j){ // i.e. j is in the facet
if (!union_faces.test(ideal_heights.size()-1-j)){
containing_critical_el = true;
break;
}
} else if (counter<facet_it->size()-1) counter++;
}
}
break;
}
if(not_containing_el && containing_critical_el){ //facet contributes
//cout << "Taking intersections with the facet " << *facet_it << endl;
face_it =faces.begin();
for (;face_it!=faces.end();++face_it){
boost::dynamic_bitset<> intersection(face_it->first);
for (size_t i=0;i<facet_it->size();i++){
if (facet_it->at(i)>ER_nr) intersection.set(ideal_heights.size()-1-facet_it->at(i),false);
}
intersection.resize(index);
if (intersection.any()){
// check whether the intersection lies in any of the not_faces
not_faces_it = not_faces.begin();
for (;not_faces_it!=not_faces.end();++not_faces_it){
if (intersection.is_subset_of(not_faces_it->first)) break;
}
if (not_faces_it== not_faces.end()) new_faces.push_back(make_pair(intersection,0));
}
}
}
}
// the new faces need to be sort in lex order anyway. this can be used to reduce operations
// for subset checking
new_faces.sort();
auto outer_it = new_faces.begin();
auto inner_it = new_faces.begin();
for (;outer_it!=new_faces.end();++outer_it){
INTERRUPT_COMPUTATION_BY_EXCEPTION
// work with a not-key vector
vector<key_t> face_not_key;
for (size_t i=0;i<outer_it->first.size();i++){
if (!outer_it->first.test(i)){
face_not_key.push_back(i);
}
}
inner_it=new_faces.begin();
size_t i=0;
while (inner_it!=outer_it){
i=0;
for (;i<face_not_key.size();++i){
if (inner_it->first.test(face_not_key[i])) break; //inner_it has an element which is not in outer_it
}
if (i==face_not_key.size()){
inner_it = new_faces.erase(inner_it); //inner_it is a subface of outer_it
} else ++inner_it;
}
}
new_faces.merge(not_faces);
/*cout << "The new faces: " << endl;
for (auto jt=new_faces.begin();jt!=new_faces.end();++jt){
cout << jt->first << " | " << jt->second << endl;
}*/
heights(facet_keys,new_faces,index-1,ideal_heights,max_dim);
}
template<typename Integer>
void Full_Cone<Integer>::convert_polyhedron_to_polytope() {
if(verbose){
verboseOutput() << "Converting polyhedron to polytope" << endl;
verboseOutput() << "Pointed since cone over polytope" << endl;
}
pointed=true;
is_Computed.set(ConeProperty::IsPointed);
Full_Cone Polytope(Generators);
Polytope.pointed=true;
Polytope.is_Computed.set(ConeProperty::IsPointed);
Polytope.keep_order=true;
Polytope.Grading=Truncation;
Polytope.is_Computed.set(ConeProperty::Grading);
if(isComputed(ConeProperty::SupportHyperplanes)){
Polytope.Support_Hyperplanes=Support_Hyperplanes;
Polytope.nrSupport_Hyperplanes=nrSupport_Hyperplanes;
Polytope.is_Computed.set(ConeProperty::SupportHyperplanes);
}
if(isComputed(ConeProperty::ExtremeRays)){
Polytope.Extreme_Rays_Ind=Extreme_Rays_Ind;
Polytope.is_Computed.set(ConeProperty::ExtremeRays);
}
Polytope.do_deg1_elements=true;
Polytope.verbose=verbose;
Polytope.compute();
if(Polytope.isComputed(ConeProperty::SupportHyperplanes) &&
!isComputed(ConeProperty::SupportHyperplanes)){
Support_Hyperplanes=Polytope.Support_Hyperplanes;
nrSupport_Hyperplanes=Polytope.nrSupport_Hyperplanes;
is_Computed.set(ConeProperty::SupportHyperplanes);
}
if(Polytope.isComputed(ConeProperty::ExtremeRays) &&
!isComputed(ConeProperty::ExtremeRays)){
Extreme_Rays_Ind=Polytope.Extreme_Rays_Ind;
is_Computed.set(ConeProperty::ExtremeRays);
}
if(Polytope.isComputed(ConeProperty::Deg1Elements)){
module_rank=Polytope.Deg1_Elements.size();
if(do_Hilbert_basis){
Hilbert_Basis=Polytope.Deg1_Elements;
is_Computed.set(ConeProperty::HilbertBasis);
}
is_Computed.set(ConeProperty::ModuleRank);
if(isComputed(ConeProperty::Grading)){
multiplicity=1; // of the recession cone;
is_Computed.set(ConeProperty::Multiplicity);
if(do_h_vector){
vector<num_t> hv(1);
typename list<vector<Integer> >::const_iterator hb=Polytope.Deg1_Elements.begin();
for(;hb!=Polytope.Deg1_Elements.end();++hb){
size_t deg = convertTo<long>(v_scalar_product(Grading,*hb));
if(deg+1>hv.size())
hv.resize(deg+1);
hv[deg]++;
}
Hilbert_Series.add(hv,vector<denom_t>());
Hilbert_Series.setShift(convertTo<long>(shift));
Hilbert_Series.adjustShift();
Hilbert_Series.simplify();
is_Computed.set(ConeProperty::HilbertSeries);
}
}
}
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::compute_deg1_elements_via_approx_global() {
compute_elements_via_approx(Deg1_Elements);
/* typename list<vector<Integer> >::iterator e; // now already done in simplex.cpp and directly for generators
for(e=Deg1_Elements.begin(); e!=Deg1_Elements.end();)
if(!contains(*e))
e=Deg1_Elements.erase(e);
else
++e; */
if(verbose)
verboseOutput() << Deg1_Elements.size() << " deg 1 elements found" << endl;
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::compute_elements_via_approx(list<vector<Integer> >& elements_from_approx) {
if (!isComputed(ConeProperty::Grading)){
support_hyperplanes(); // the only thing we can do now
return;
}
assert(elements_from_approx.empty());
vector<list<vector<Integer> > > approx_points = latt_approx();
vector<vector<key_t> > approx_points_indices;
key_t current_key =0;
//cout << "Approximation points: " << endl;
//for (size_t j=0;j<dim;++j){
////cout << "Original generator " << j << ": " << Generators[j] << endl;
////cout << approx_points[j] << endl;
//}
//cout << "Nr of ER: " << getExtremeRays().size() << endl;
//Matrix<Integer> all_approx_points(Generators);
Matrix<Integer> all_approx_points(0,dim);
for (size_t i=0;i<nr_gen;i++){
vector<key_t> indices(approx_points[i].size());
if (!approx_points[i].empty()){
all_approx_points.append(approx_points[i]);
for (size_t j=0;j<approx_points[i].size();++j){
indices[j]=current_key;
current_key++;
}
}
approx_points_indices.push_back(indices);
}
if(verbose){
verboseOutput() << "Nr original generators: " << nr_gen << endl;
verboseOutput() << "Nr approximation points: " << all_approx_points.nr_of_rows() << endl;
}
Full_Cone C_approx(all_approx_points);
C_approx.OriginalGenerators = Generators;
C_approx.approx_points_keys = approx_points_indices;
C_approx.verbose = verbose;
//C_temp.build_cone_approx(*this,approx_points_indices);
//Full_Cone C_approx(C_temp.getGenerators());
//Full_Cone C_approx(all_approx_points); // latt_approx computes a matrix of generators
C_approx.is_approximation=true;
// C_approx.Generators.pretty_print(cout);
C_approx.do_deg1_elements=do_deg1_elements; // for supercone C_approx that is generated in degree 1
C_approx.do_Hilbert_basis=do_Hilbert_basis;
C_approx.do_all_hyperplanes=false; // not needed
C_approx.Subcone_Support_Hyperplanes=Support_Hyperplanes; // *this is a subcone of C_approx, used to discard elements
C_approx.Support_Hyperplanes=Support_Hyperplanes; // UNFORTUNATELY NEEDED IN REDUCTION FOR SUBDIVIUSION BY APPROX
C_approx.is_Computed.set(ConeProperty::SupportHyperplanes);
C_approx.nrSupport_Hyperplanes = nrSupport_Hyperplanes;
C_approx.Grading = Grading;
C_approx.is_Computed.set(ConeProperty::Grading);
C_approx.Truncation=Truncation;
C_approx.TruncLevel=TruncLevel;
//if(verbose)
//verboseOutput() << "Computing elements in approximating cone with "
//<< C_approx.Generators.nr_of_rows() << " generators." << endl;
if(verbose){
verboseOutput() << "Computing elements in approximating cone." << endl;
}
bool verbose_tmp = verbose;
verbose =false;
C_approx.compute();
verbose = verbose_tmp;
//vector<key_t> used_gens;
//for (size_t j=0;j<nr_gen;++j){
//if (C_approx.in_triang[j]) used_gens.push_back(j);
//}
//C_approx.Generators = C_approx.Generators.submatrix(used_gens);
//if (verbose){
//verboseOutput() << "Used "<< C_approx.Generators.nr_of_rows() << " / " << C_approx.nr_gen << " generators." << endl;
//}
//C_approx.nr_gen=C_approx.Generators.nr_of_rows();
// TODO: with the current implementation, this is always the case!
if(!C_approx.contains(*this) || Grading!=C_approx.Grading){
throw FatalException("Wrong approximating cone.");
}
if(verbose)
verboseOutput() << "Sum of dets of simplicial cones evaluated in approximation = " << C_approx.detSum << endl;
if(verbose)
verboseOutput() << "Returning to original cone" << endl;
if(do_deg1_elements)
elements_from_approx.splice(elements_from_approx.begin(),C_approx.Deg1_Elements);
if(do_Hilbert_basis)
elements_from_approx.splice(elements_from_approx.begin(),C_approx.Hilbert_Basis);
}
// -s
template<typename Integer>
void Full_Cone<Integer>::support_hyperplanes() {
if(!isComputed(ConeProperty::SupportHyperplanes)){
sort_gens_by_degree(false); // we do not want to triangulate here
build_top_cone();
}
extreme_rays_and_deg1_check();
if(inhomogeneous){
find_level0_dim();
if(do_module_rank)
find_module_rank();
}
compute_class_group();
}
//---------------------------------------------------------------------------
// Checks and auxiliary algorithms
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::extreme_rays_and_deg1_check() {
check_pointed();
if(!pointed){
throw NonpointedException();
}
//cout << "Generators" << endl;
//Generators.pretty_print(cout);
//cout << "SupportHyperplanes" << endl;
//Support_Hyperplanes.pretty_print(cout);
compute_extreme_rays();
deg1_check();
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::check_given_grading(){
if(Grading.size()==0)
return;
bool positively_graded=true;
if(!isComputed(ConeProperty::Grading)){
size_t neg_index=0;
Integer neg_value;
bool nonnegative=true;
vector<Integer> degrees = Generators.MxV(Grading);
for (size_t i=0; i<degrees.size(); ++i) {
if (degrees[i]<=0 && (!inhomogeneous || gen_levels[i]==0)) {
// in the inhomogeneous case: test only generators of tail cone
positively_graded=false;;
if(degrees[i]<0){
nonnegative=false;
neg_index=i;
neg_value=degrees[i];
}
}
}
if(!nonnegative){
throw BadInputException("Grading gives negative value "
+ toString(neg_value) + " for generator "
+ toString(neg_index+1) + "!");
}
}
if(positively_graded){
is_Computed.set(ConeProperty::Grading);
if(inhomogeneous)
find_grading_inhom();
set_degrees();
}
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::find_grading(){
if(inhomogeneous) // in the inhomogeneous case we do not allow implicit grading
return;
deg1_check(); // trying to find grading under which all generators have the same degree
if (!isComputed(ConeProperty::Grading) && (do_multiplicity || do_deg1_elements || do_h_vector)) {
if (!isComputed(ConeProperty::ExtremeRays)) {
if (verbose) {
verboseOutput() << "Cannot find grading s.t. all generators have the degree 1! Computing Extreme rays first:" << endl;
}
get_supphyps_from_copy(true);
extreme_rays_and_deg1_check();
if(!pointed){
throw NonpointedException();
};
// We keep the SupportHyperplanes, so we do not need to recompute them
// for the last generator, and use them to make a global reduction earlier
}
}
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::find_level0_dim(){
if(isComputed(ConeProperty::RecessionRank))
return;
if(!isComputed(ConeProperty::Generators)){
throw FatalException("Missing Generators.");
}
Matrix<Integer> Help(nr_gen,dim);
for(size_t i=0; i<nr_gen;++i)
if(gen_levels[i]==0)
Help[i]=Generators[i];
ProjToLevel0Quot=Help.kernel(); // Necessary for the module rank
// For level0_dim the rank of Help would be enough
level0_dim=dim-ProjToLevel0Quot.nr_of_rows();
// level0_dim=Help.rank();
is_Computed.set(ConeProperty::RecessionRank);
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::find_level0_dim_from_HB(){
// we use the Hilbert basis if we don't have the extreme reys.
// This is possible if the HB was computed by the dual algorithm.
// Would be enough if we would take the extreme reys of the recession cone,
// but they have not been extracted from the HB
if(isComputed(ConeProperty::RecessionRank))
return;
assert(isComputed(ConeProperty::HilbertBasis));
Matrix<Integer> Help(0,dim);
for(auto H=Hilbert_Basis.begin(); H!= Hilbert_Basis.end();++H)
if(v_scalar_product(*H,Truncation)==0)
Help.append(*H);
ProjToLevel0Quot=Help.kernel(); // Necessary for the module rank
// For level0_dim the rank of Help would be enough
level0_dim=dim-ProjToLevel0Quot.nr_of_rows();
is_Computed.set(ConeProperty::RecessionRank);
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::find_module_rank(){
if(isComputed(ConeProperty::ModuleRank))
return;
if(level0_dim==dim){
module_rank=0;
is_Computed.set(ConeProperty::ModuleRank);
return;
}
if(isComputed(ConeProperty::HilbertBasis)){
find_module_rank_from_HB();
return;
}
// size_t HBrank = module_rank;
if(do_module_rank)
find_module_rank_from_proj();
/* if(isComputed(ConeProperty::HilbertBasis))
assert(HBrank==module_rank);
*/
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::find_module_rank_from_proj(){
if(verbose){
verboseOutput() << "Computing projection to quotient mod level 0" << endl;
}
Matrix<Integer> ProjGen(nr_gen,dim-level0_dim);
for(size_t i=0;i<nr_gen;++i){
ProjGen[i]=ProjToLevel0Quot.MxV(Generators[i]);
}
vector<Integer> GradingProj=ProjToLevel0Quot.transpose().solve_ZZ(Truncation);
Full_Cone<Integer> Cproj(ProjGen);
Cproj.verbose=false;
Cproj.Grading=GradingProj;
Cproj.is_Computed.set(ConeProperty::Grading);
Cproj.do_deg1_elements=true;
Cproj.compute();
module_rank=Cproj.Deg1_Elements.size();
is_Computed.set(ConeProperty::ModuleRank);
return;
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::find_module_rank_from_HB(){
if(level0_dim==0){
module_rank=Hilbert_Basis.size();
is_Computed.set(ConeProperty::ModuleRank);
return;
}
set<vector<Integer> > Quotient;
vector<Integer> v;
// cout << "=======================" << endl;
// ProjToLevel0Quot.print(cout);
// cout << "=======================" << endl;
typename list<vector<Integer> >::iterator h;
for(h=Hilbert_Basis.begin();h!=Hilbert_Basis.end();++h){
INTERRUPT_COMPUTATION_BY_EXCEPTION
v=ProjToLevel0Quot.MxV(*h);
bool zero=true;
for(size_t j=0;j<v.size();++j)
if(v[j]!=0){
zero=false;
break;
}
if(!zero)
Quotient.insert(v);
}
module_rank=Quotient.size();
is_Computed.set(ConeProperty::ModuleRank);
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::find_grading_inhom(){
if(Grading.size()==0 || Truncation.size()==0){
throw FatalException("Cannot find grading in the inhomogeneous case!");
}
if(shift!=0) // to avoid double computation
return;
bool first=true;
Integer level,degree,quot=0,min_quot=0;
for(size_t i=0;i<nr_gen;++i){
level=v_scalar_product(Truncation,Generators[i]);
if(level==0)
continue;
degree=v_scalar_product(Grading,Generators[i]);
quot=degree/level;
// cout << Generators[i];
// cout << "*** " << degree << " " << level << " " << quot << endl;
if(level*quot>=degree)
quot--;
if(first){
min_quot=quot;
first=false;
}
if(quot<min_quot)
min_quot=quot;
// cout << "+++ " << min_quot << endl;
}
shift = min_quot;
for(size_t i=0;i<dim;++i) // under this grading all generators have positive degree
Grading[i] = Grading[i] - shift * Truncation[i];
// shift--; // NO LONGER correction for the Hilbert series computation to have it start in degree 0
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::set_degrees() {
// Generators.pretty_print(cout);
// cout << "Grading " << Grading;
if (gen_degrees.size() != nr_gen && isComputed(ConeProperty::Grading)) // now we set the degrees
{
gen_degrees.resize(nr_gen);
if(do_h_vector || !using_GMP<Integer>())
gen_degrees_long.resize(nr_gen);
vector<Integer> gen_degrees_Integer=Generators.MxV(Grading);
for (size_t i=0; i<nr_gen; i++) {
if (gen_degrees_Integer[i] < 1) {
throw BadInputException("Grading gives non-positive value "
+ toString(gen_degrees_Integer[i])
+ " for generator " + toString(i+1) + ".");
}
convert(gen_degrees[i], gen_degrees_Integer[i]);
if(do_h_vector || !using_GMP<Integer>())
convert(gen_degrees_long[i], gen_degrees_Integer[i]);
}
}
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::set_levels() {
if(inhomogeneous && Truncation.size()!=dim){
throw FatalException("Truncation not defined in inhomogeneous case.");
}
// cout <<"trunc " << Truncation;
if(gen_levels.size()!=nr_gen) // now we compute the levels
{
gen_levels.resize(nr_gen);
vector<Integer> gen_levels_Integer=Generators.MxV(Truncation);
for (size_t i=0; i<nr_gen; i++) {
if (gen_levels_Integer[i] < 0) {
throw FatalException("Truncation gives non-positive value "
+ toString(gen_levels_Integer[i]) + " for generator "
+ toString(i+1) + ".");
}
convert(gen_levels[i], gen_levels_Integer[i]);
// cout << "Gen " << Generators[i];
// cout << "level " << gen_levels[i] << endl << "----------------------" << endl;
}
}
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::sort_gens_by_degree(bool triangulate) {
// if(deg1_extreme_rays) // gen_degrees.size()==0 ||
// return;
if(keep_order)
return;
Matrix<Integer> Weights(0,dim);
vector<bool> absolute;
if(triangulate){
if(isComputed(ConeProperty::Grading)){
Weights.append(Grading);
absolute.push_back(false);
}
else{
Weights.append(vector<Integer>(dim,1));
absolute.push_back(true);
}
}
vector<key_t> perm=Generators.perm_by_weights(Weights,absolute);
Generators.order_rows_by_perm(perm);
order_by_perm_bool(Extreme_Rays_Ind,perm);
if(isComputed(ConeProperty::Grading)){
order_by_perm(gen_degrees,perm);
if(do_h_vector || !using_GMP<Integer>())
order_by_perm(gen_degrees_long,perm);
}
if(inhomogeneous && gen_levels.size()==nr_gen)
order_by_perm(gen_levels,perm);
compose_perm_gens(perm);
if(triangulate){
Integer roughness;
if(isComputed(ConeProperty::Grading)){
roughness=gen_degrees[nr_gen-1]/gen_degrees[0];
}
else{
Integer max_norm=0, min_norm=0;
for(size_t i=0;i<dim;++i){
max_norm+=Iabs(Generators[nr_gen-1][i]);
min_norm+=Iabs(Generators[0][i]);
}
roughness=max_norm/min_norm;
}
if(verbose){
verboseOutput() << "Roughness " << roughness << endl;
}
if(roughness >= 10 && !suppress_bottom_dec){
do_bottom_dec=true;
if(verbose){
verboseOutput() << "Bottom decomposition activated" << endl;
}
}
}
if (verbose) {
if(triangulate){
if(isComputed(ConeProperty::Grading)){
verboseOutput() <<"Generators sorted by degree and lexicographically" << endl;
verboseOutput() << "Generators per degree:" << endl;
verboseOutput() << count_in_map<Integer,long>(gen_degrees);
}
else
verboseOutput() << "Generators sorted by 1-norm and lexicographically" << endl;
}
else{
verboseOutput() << "Generators sorted lexicographically" << endl;
}
}
keep_order=true;
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::compose_perm_gens(const vector<key_t>& perm) {
order_by_perm(PermGens,perm);
}
//---------------------------------------------------------------------------
// an alternative to compute() for the basic tasks that need no triangulation
template<typename Integer>
void Full_Cone<Integer>::dualize_cone(bool print_message){
omp_start_level=omp_get_level();
if(dim==0){
set_zero_cone();
return;
}
// DO NOT CALL do_vars_check!!
bool save_tri = do_triangulation;
bool save_part_tri = do_partial_triangulation;
do_triangulation = false;
do_partial_triangulation = false;
if(print_message) start_message();
sort_gens_by_degree(false);
if(!isComputed(ConeProperty::SupportHyperplanes))
build_top_cone();
if(do_pointed)
check_pointed();
if(do_extreme_rays) // in case we have known the support hyperplanes
compute_extreme_rays();
do_triangulation = save_tri;
do_partial_triangulation = save_part_tri;
if(print_message) end_message();
}
//---------------------------------------------------------------------------
template<typename Integer>
vector<key_t> Full_Cone<Integer>::find_start_simplex() const {
return Generators.max_rank_submatrix_lex();
}
//---------------------------------------------------------------------------
template<typename Integer>
Matrix<Integer> Full_Cone<Integer>::select_matrix_from_list(const list<vector<Integer> >& S,
vector<size_t>& selection){
sort(selection.begin(),selection.end());
assert(selection.back()<S.size());
size_t i=0,j=0;
size_t k=selection.size();
Matrix<Integer> M(selection.size(),S.front().size());
typename list<vector<Integer> >::const_iterator ll=S.begin();
for(;ll!=S.end()&&i<k;++ll){
if(j==selection[i]){
M[i]=*ll;
i++;
}
j++;
}
return M;
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::minimize_support_hyperplanes(){
if(Support_Hyperplanes.nr_of_rows() == 0)
return;
if(isComputed(ConeProperty::SupportHyperplanes)){
nrSupport_Hyperplanes=Support_Hyperplanes.nr_of_rows();
return;
}
if (verbose) {
verboseOutput() << "Minimize the given set of support hyperplanes by "
<< "computing the extreme rays of the dual cone" << endl;
}
Full_Cone<Integer> Dual(Support_Hyperplanes);
Dual.verbose=verbose;
Dual.Support_Hyperplanes = Generators;
Dual.is_Computed.set(ConeProperty::SupportHyperplanes);
Dual.compute_extreme_rays();
Support_Hyperplanes = Dual.Generators.submatrix(Dual.Extreme_Rays_Ind); //only essential hyperplanes
is_Computed.set(ConeProperty::SupportHyperplanes);
nrSupport_Hyperplanes=Support_Hyperplanes.nr_of_rows();
do_all_hyperplanes=false;
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::compute_extreme_rays(bool use_facets){
if (isComputed(ConeProperty::ExtremeRays))
return;
// when we do approximation, we do not have the correct hyperplanes
// and cannot compute the extreme rays
if (is_approximation)
return;
assert(isComputed(ConeProperty::SupportHyperplanes));
check_pointed();
if(!pointed){
throw NonpointedException();
}
if(dim*Support_Hyperplanes.nr_of_rows() < nr_gen) {
compute_extreme_rays_rank(use_facets);
} else {
compute_extreme_rays_compare(use_facets);
}
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::compute_extreme_rays_rank(bool use_facets){
if (verbose) verboseOutput() << "Select extreme rays via rank ... " << flush;
size_t i;
vector<key_t> gen_in_hyperplanes;
gen_in_hyperplanes.reserve(Support_Hyperplanes.nr_of_rows());
Matrix<Integer> M(Support_Hyperplanes.nr_of_rows(),dim);
deque<bool> Ext(nr_gen,false);
#pragma omp parallel for firstprivate(gen_in_hyperplanes,M)
for(i=0;i<nr_gen;++i){
// if (isComputed(ConeProperty::Triangulation) && !in_triang[i])
// continue;
INTERRUPT_COMPUTATION_BY_EXCEPTION
gen_in_hyperplanes.clear();
if(use_facets){
typename list<FACETDATA>::const_iterator IHV=Facets.begin();
for (size_t j=0; j<Support_Hyperplanes.nr_of_rows(); ++j, ++IHV){
if(IHV->GenInHyp.test(i))
gen_in_hyperplanes.push_back(j);
}
}
else{
for (size_t j=0; j<Support_Hyperplanes.nr_of_rows(); ++j){
if(v_scalar_product(Generators[i],Support_Hyperplanes[j])==0)
gen_in_hyperplanes.push_back(j);
}
}
if (gen_in_hyperplanes.size() < dim-1)
continue;
if (M.rank_submatrix(Support_Hyperplanes,gen_in_hyperplanes) >= dim-1)
Ext[i]=true;
}
for(i=0; i<nr_gen;++i)
Extreme_Rays_Ind[i]=Ext[i];
is_Computed.set(ConeProperty::ExtremeRays);
if (verbose) verboseOutput() << "done." << endl;
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::compute_extreme_rays_compare(bool use_facets){
if (verbose) verboseOutput() << "Select extreme rays via comparison ... " << flush;
size_t i,j,k;
// Matrix<Integer> SH=getSupportHyperplanes().transpose();
// Matrix<Integer> Val=Generators.multiplication(SH);
size_t nc=Support_Hyperplanes.nr_of_rows();
vector<vector<bool> > Val(nr_gen);
for (i=0;i<nr_gen;++i)
Val[i].resize(nc);
// In this routine Val[i][j]==1, i.e. true, indicates that
// the i-th generator is contained in the j-th support hyperplane
vector<key_t> Zero(nc);
vector<key_t> nr_ones(nr_gen);
for (i = 0; i <nr_gen; i++) {
INTERRUPT_COMPUTATION_BY_EXCEPTION
k=0;
Extreme_Rays_Ind[i]=true;
if(use_facets){
typename list<FACETDATA>::const_iterator IHV=Facets.begin();
for (j=0; j<Support_Hyperplanes.nr_of_rows(); ++j, ++IHV){
if(IHV->GenInHyp.test(i)){
k++;
Val[i][j]=true;
}
else
Val[i][j]=false;
}
}
else{
for (j = 0; j <nc; ++j) {
if (v_scalar_product(Generators[i],Support_Hyperplanes[j])==0) {
k++;
Val[i][j]=true;
}
else
Val[i][j]=false;
}
}
nr_ones[i]=k;
if (k<dim-1||k==nc) // not contained in enough facets or in all (0 as generator)
Extreme_Rays_Ind[i]=false;
}
maximal_subsets(Val,Extreme_Rays_Ind);
is_Computed.set(ConeProperty::ExtremeRays);
if (verbose) verboseOutput() << "done." << endl;
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::compute_class_group() { // from the support hyperplanes
if(!do_class_group || !isComputed(ConeProperty::SupportHyperplanes) || isComputed(ConeProperty::ClassGroup))
return;
Matrix<Integer> Trans=Support_Hyperplanes; // .transpose();
size_t rk;
Trans.SmithNormalForm(rk);
ClassGroup.push_back(Support_Hyperplanes.nr_of_rows()-rk);
for(size_t i=0;i<rk;++i)
if(Trans[i][i]!=1)
ClassGroup.push_back(Trans[i][i]);
is_Computed.set(ConeProperty::ClassGroup);
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::select_deg1_elements() { // from the Hilbert basis
if(inhomogeneous)
return;
typename list<vector<Integer> >::iterator h = Hilbert_Basis.begin();
for(;h!=Hilbert_Basis.end();h++){
if(v_scalar_product(Grading,*h)==1)
Deg1_Elements.push_back(*h);
}
is_Computed.set(ConeProperty::Deg1Elements,true);
}
//---------------------------------------------------------------------------
template<typename Integer>
bool Full_Cone<Integer>::subcone_contains(const vector<Integer>& v) {
for (size_t i=0; i<Subcone_Support_Hyperplanes.nr_of_rows(); ++i)
if (v_scalar_product(Subcone_Support_Hyperplanes[i],v) < 0)
return false;
for (size_t i=0; i<Subcone_Equations.nr_of_rows(); ++i)
if (v_scalar_product(Subcone_Equations[i],v) != 0)
return false;
if(is_global_approximation)
if(v_scalar_product(Subcone_Grading,v)!=1)
return false;
return true;
}
//---------------------------------------------------------------------------
template<typename Integer>
bool Full_Cone<Integer>::contains(const vector<Integer>& v) {
for (size_t i=0; i<Support_Hyperplanes.nr_of_rows(); ++i)
if (v_scalar_product(Support_Hyperplanes[i],v) < 0)
return false;
return true;
}
//---------------------------------------------------------------------------
template<typename Integer>
bool Full_Cone<Integer>::contains(const Full_Cone& C) {
for(size_t i=0;i<C.nr_gen;++i)
if(!contains(C.Generators[i])){
cerr << "Missing generator " << C.Generators[i] << endl;
return(false);
}
return(true);
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::select_deg1_elements(const Full_Cone& C) { // from vectors computed in
// the auxiliary cone C
assert(isComputed(ConeProperty::SupportHyperplanes));
assert(C.isComputed(ConeProperty::Deg1Elements));
typename list<vector<Integer> >::const_iterator h = C.Deg1_Elements.begin();
for(;h!=C.Deg1_Elements.end();++h){
if(contains(*h))
Deg1_Elements.push_back(*h);
}
is_Computed.set(ConeProperty::Deg1Elements,true);
}
//---------------------------------------------------------------------------
// so far only for experiments
/*
template<typename Integer>
void Full_Cone<Integer>::select_Hilbert_Basis(const Full_Cone& C) { // from vectors computed in
// the auxiliary cone C
assert(isComputed(ConeProperty::SupportHyperplanes));
assert(C.isComputed(ConeProperty::Deg1Elements));
typename list<vector<Integer> >::const_iterator h = C.Hilbert_Basis.begin();
for(;h!=C.Hilbert_Basis.end();++h){
if(contains(*h))
// Deg1_Elements.push_back(*h);
cout << *h;
}
exit(0);
is_Computed.set(ConeProperty::Deg1Elements,true);
}
*/
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::check_pointed() {
if (isComputed(ConeProperty::IsPointed))
return;
assert(isComputed(ConeProperty::SupportHyperplanes));
if (isComputed(ConeProperty::Grading)){
pointed=true;
if (verbose) verboseOutput() << "Pointed since graded" << endl;
is_Computed.set(ConeProperty::IsPointed);
return;
}
if (verbose) verboseOutput() << "Checking pointedness ... " << flush;
if(Support_Hyperplanes.nr_of_rows() <= dim*dim/2){
pointed = (Support_Hyperplanes.rank() == dim);
}
else
pointed = (Support_Hyperplanes.max_rank_submatrix_lex().size() == dim);
is_Computed.set(ConeProperty::IsPointed);
if(pointed && Grading.size()>0){
throw BadInputException("Grading not positive on pointed cone.");
}
if (verbose) verboseOutput() << "done." << endl;
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::disable_grading_dep_comp() {
if (do_multiplicity || do_deg1_elements || do_h_vector) {
if (do_default_mode) {
// if (verbose)
// verboseOutput() << "No grading specified and cannot find one. "
// << "Disabling some computations!" << endl;
do_deg1_elements = false;
do_h_vector = false;
if(!explicit_full_triang){
do_triangulation=false;
if(do_Hilbert_basis)
do_partial_triangulation=true;
}
} else {
throw NotComputableException("No grading specified and cannot find one. Cannot compute some requested properties!");
}
}
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::deg1_check() {
if(inhomogeneous) // deg 1 check disabled since it makes no sense in this case
return;
if (!isComputed(ConeProperty::Grading) && Grading.size()==0 // we still need it and
&& !isComputed(ConeProperty::IsDeg1ExtremeRays)) { // we have not tried it
if (isComputed(ConeProperty::ExtremeRays)) {
Matrix<Integer> Extreme=Generators.submatrix(Extreme_Rays_Ind);
if (has_generator_with_common_divisor)
Extreme.make_prime();
try{
Grading = Extreme.find_linear_form();
} catch(const ArithmeticException& e) { // if the exception has been thrown, the grading has
Grading.resize(0); // we consider the grafing as non existing -- though this may not be true
if(verbose)
verboseOutput() << "Giving up the check for a grading" << endl;
}
if (Grading.size() == dim && v_scalar_product(Grading,Extreme[0])==1) {
is_Computed.set(ConeProperty::Grading);
} else {
deg1_extreme_rays = false;
Grading.clear();
is_Computed.set(ConeProperty::IsDeg1ExtremeRays);
}
} else // extreme rays not known
if (!deg1_generated_computed) {
Matrix<Integer> GenCopy = Generators;
if (has_generator_with_common_divisor)
GenCopy.make_prime();
try{
Grading = GenCopy.find_linear_form();
} catch(const ArithmeticException& e) { // if the exception has been thrown,
Grading.resize(0); // we consider the grafing as non existing-- though this may not be true
if(verbose)
verboseOutput() << "Giving up the check for a grading" << endl;
}
if (Grading.size() == dim && v_scalar_product(Grading,GenCopy[0])==1) {
is_Computed.set(ConeProperty::Grading);
} else {
deg1_generated = false;
deg1_generated_computed = true;
Grading.clear();
}
}
}
//now we hopefully have a grading
if (!isComputed(ConeProperty::Grading)) {
if (isComputed(ConeProperty::ExtremeRays)) {
// there is no hope to find a grading later
deg1_generated = false;
deg1_generated_computed = true;
deg1_extreme_rays = false;
is_Computed.set(ConeProperty::IsDeg1ExtremeRays);
disable_grading_dep_comp();
}
return; // we are done
}
set_degrees();
vector<Integer> divided_gen_degrees = gen_degrees;
if (has_generator_with_common_divisor) {
Matrix<Integer> GenCopy = Generators;
GenCopy.make_prime();
convert(divided_gen_degrees, GenCopy.MxV(Grading));
}
if (!deg1_generated_computed) {
deg1_generated = true;
for (size_t i = 0; i < nr_gen; i++) {
if (divided_gen_degrees[i] != 1) {
deg1_generated = false;
break;
}
}
deg1_generated_computed = true;
if (deg1_generated) {
deg1_extreme_rays = true;
is_Computed.set(ConeProperty::IsDeg1ExtremeRays);
}
}
if (!isComputed(ConeProperty::IsDeg1ExtremeRays)
&& isComputed(ConeProperty::ExtremeRays)) {
deg1_extreme_rays = true;
for (size_t i = 0; i < nr_gen; i++) {
if (Extreme_Rays_Ind[i] && divided_gen_degrees[i] != 1) {
deg1_extreme_rays = false;
break;
}
}
is_Computed.set(ConeProperty::IsDeg1ExtremeRays);
}
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::check_deg1_hilbert_basis() {
if (isComputed(ConeProperty::IsDeg1HilbertBasis) || inhomogeneous)
return;
if ( !isComputed(ConeProperty::Grading) || !isComputed(ConeProperty::HilbertBasis)) {
if (verbose) {
errorOutput() << "WARNING: unsatisfied preconditions in check_deg1_hilbert_basis()!" <<endl;
}
return;
}
if (isComputed(ConeProperty::Deg1Elements)) {
deg1_hilbert_basis = (Deg1_Elements.size() == Hilbert_Basis.size());
} else {
deg1_hilbert_basis = true;
typename list< vector<Integer> >::iterator h;
for (h = Hilbert_Basis.begin(); h != Hilbert_Basis.end(); ++h) {
if (v_scalar_product((*h),Grading)!=1) {
deg1_hilbert_basis = false;
break;
}
}
}
is_Computed.set(ConeProperty::IsDeg1HilbertBasis);
}
//---------------------------------------------------------------------------
// Computes the generators of a supercone approximating "this" by a cone over a lattice polytope
// for every vertex of the simplex, we get a matrix with the integer points of the respective Weyl chamber
template<typename Integer>
vector<list<vector<Integer> > > Full_Cone<Integer>::latt_approx() {
assert(isComputed(ConeProperty::Grading));
assert(isComputed(ConeProperty::ExtremeRays));
Matrix<Integer> G(1,dim);
G[0]=Grading;
Matrix<Integer> G_copy=G;
// Lineare_Transformation<Integer> NewBasis(G); // gives a new basis in which the grading is a coordinate
size_t dummy;
Matrix<Integer> U=G_copy.SmithNormalForm(dummy); // the basis elements are the columns of U
Integer dummy_denom;
// vector<Integer> dummy_diag(dim);
Matrix<Integer> T=U.invert(dummy_denom); // T is the coordinate transformation
// to the new basis: v --> Tv (in this case)
// for which the grading is the FIRST coordinate
assert(dummy_denom==1); // for safety
// It can happen that -Grading has become the first row of T, but we want Grading. If necessary we replace the
// first row by its negative, and correspondingly the first column of U by its negative
if(T[0]!=Grading){
for(size_t i=0;i<dim;++i){
U[i][0]*=-1;
T[0][i]*=-1;
}
}
assert(T[0] == Grading);
Matrix<Integer> M;
vector<list<vector<Integer> > > approx_points;
size_t nr_approx=0;
for(size_t i=0;i<nr_gen;++i){
list<vector<Integer> > approx;
if(Extreme_Rays_Ind[i]){
//cout << "point before transformation: " << Generators[i];
approx_simplex(T.MxV(Generators[i]),approx,1);
// TODO: NECESSARY?
//approx.unique()
//cout << "Approximation points for generator " << i << ": " << Generators[i] << endl;
nr_approx = 0;
for(auto jt=approx.begin();jt!=approx.end();++jt){ // reverse transformation
*jt=U.MxV(*jt);
v_make_prime(*jt);
++nr_approx;
//cout << *jt << endl;
}
//~ M=Matrix<Integer>(approx);
//~
//~ // remove duplicates
//~ for (size_t j=0;j<approx_points.size();j++){
//~ M.remove_duplicate(approx_points[j]);
//~ }
// +++ TODO: REMOVE DUPLICATES MORE EFFICIENT +++
if (nr_approx>1){
for (size_t j=0;j<approx_points.size();j++){
for (auto jt=approx_points[j].begin();jt!=approx_points[j].end();++jt){
approx.remove(*jt);
}
}
}
}
approx_points.push_back(approx);
}
//cout << "-------" << endl;
//M.print(cout);
//cout << "-------" << endl;
return(approx_points);
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::prepare_inclusion_exclusion() {
if (ExcludedFaces.nr_of_rows() == 0)
return;
do_excluded_faces = do_h_vector || do_Stanley_dec;
if (verbose && !do_excluded_faces) {
errorOutput() << endl << "WARNING: exluded faces, but no h-vector computation or Stanley decomposition"
<< endl << "Therefore excluded faces will be ignored" << endl;
}
if (isComputed(ConeProperty::ExcludedFaces) &&
(isComputed(ConeProperty::InclusionExclusionData) || !do_excluded_faces) ) {
return;
}
// indicates which generators lie in the excluded faces
vector<boost::dynamic_bitset<> > GensInExcl(ExcludedFaces.nr_of_rows());
for(size_t j=0;j<ExcludedFaces.nr_of_rows();++j){ // now we produce these indicators
bool first_neq_0=true; // and check whether the linear forms in ExcludedFaces
bool non_zero=false; // have the cone on one side
GensInExcl[j].resize(nr_gen,false);
for(size_t i=0; i< nr_gen;++i){
Integer test=v_scalar_product(ExcludedFaces[j],Generators[i]);
if(test==0){
GensInExcl[j].set(i);
continue;
}
non_zero=true;
if(first_neq_0){
first_neq_0=false;
if(test<0){
for(size_t k=0;k<dim;++k) // replace linear form by its negative
ExcludedFaces[j][k]*=-1; // to get cone in positive halfspace
test*=-1; // (only for error check)
}
}
if(test<0){
throw FatalException("Excluded hyperplane does not define a face.");
}
}
if(!non_zero){ // not impossible if the hyperplane contains the vector space spanned by the cone
throw FatalException("Excluded face contains the full cone.");
}
}
vector<bool> essential(ExcludedFaces.nr_of_rows(),true);
bool remove_one=false;
for(size_t i=0;i<essential.size();++i)
for(size_t j=i+1;j<essential.size();++j){
if(GensInExcl[j].is_subset_of(GensInExcl[i])){
essential[j]=false;
remove_one=true;
continue;
}
if(GensInExcl[i].is_subset_of(GensInExcl[j])){
essential[i]=false;
remove_one=true;
}
}
if(remove_one){
Matrix<Integer> Help(0,dim);
vector<boost::dynamic_bitset<> > HelpGensInExcl;
for(size_t i=0;i<essential.size();++i)
if(essential[i]){
Help.append(ExcludedFaces[i]);
HelpGensInExcl.push_back(GensInExcl[i]);
}
ExcludedFaces=Help;
GensInExcl=HelpGensInExcl;
}
is_Computed.set(ConeProperty::ExcludedFaces);
if (isComputed(ConeProperty::InclusionExclusionData) || !do_excluded_faces) {
return;
}
vector< pair<boost::dynamic_bitset<> , long> > InExScheme; // now we produce the formal
boost::dynamic_bitset<> all_gens(nr_gen); // inclusion-exclusion scheme
all_gens.set(); // by forming all intersections of
// excluded faces
InExScheme.push_back(pair<boost::dynamic_bitset<> , long> (all_gens, 1));
size_t old_size=1;
for(size_t i=0;i<ExcludedFaces.nr_of_rows();++i){
for(size_t j=0;j<old_size;++j)
InExScheme.push_back(pair<boost::dynamic_bitset<> , long>
(InExScheme[j].first & GensInExcl[i], -InExScheme[j].second));
old_size*=2;
}
vector<pair<boost::dynamic_bitset<>, long> >::iterator G;
InExScheme.erase(InExScheme.begin()); // remove full cone
// map<boost::dynamic_bitset<>, long> InExCollect;
InExCollect.clear();
map<boost::dynamic_bitset<>, long>::iterator F;
for(size_t i=0;i<old_size-1;++i){ // we compactify the list of faces
F=InExCollect.find(InExScheme[i].first); // obtained as intersections
if(F!=InExCollect.end()) // by listing each face only once
F->second+=InExScheme[i].second; // but with the right multiplicity
else
InExCollect.insert(InExScheme[i]);
}
for(F=InExCollect.begin();F!=InExCollect.end();){ // faces with multiplicity 0
if(F->second==0) // can be erased
InExCollect.erase(F++);
else{
++F;
}
}
if(verbose){
verboseOutput() << endl;
verboseOutput() << "InEx complete, " << InExCollect.size() << " faces involved" << endl;
}
is_Computed.set(ConeProperty::InclusionExclusionData);
}
//---------------------------------------------------------------------------
/* computes a degree function, s.t. every generator has value >0 */
template<typename Integer>
vector<Integer> Full_Cone<Integer>::compute_degree_function() const {
size_t i;
vector<Integer> degree_function(dim,0);
if (isComputed(ConeProperty::Grading)) { //use the grading if we have one
for (i=0; i<dim; i++) {
degree_function[i] = Grading[i];
}
} else { // add hyperplanes to get a degree function
if(verbose) {
verboseOutput()<<"computing degree function... "<<flush;
}
size_t h;
for (h=0; h<Support_Hyperplanes.nr_of_rows(); ++h) {
for (i=0; i<dim; i++) {
degree_function[i] += Support_Hyperplanes.get_elem(h,i);
}
}
v_make_prime(degree_function);
if(verbose) {
verboseOutput()<<"done."<<endl;
}
}
return degree_function;
}
//---------------------------------------------------------------------------
/* adds generators, they have to lie inside the existing cone */
template<typename Integer>
void Full_Cone<Integer>::add_generators(const Matrix<Integer>& new_points) {
is_simplicial = false;
int nr_new_points = new_points.nr_of_rows();
int nr_old_gen = nr_gen;
Generators.append(new_points);
nr_gen += nr_new_points;
set_degrees();
Top_Key.resize(nr_gen);
Extreme_Rays_Ind.resize(nr_gen);
for (size_t i=nr_old_gen; i<nr_gen; ++i) {
Top_Key[i] = i;
Extreme_Rays_Ind[i] = false;
}
// inhom cones
if (inhomogeneous) {
set_levels();
}
// excluded faces have to be reinitialized
is_Computed.set(ConeProperty::ExcludedFaces, false);
is_Computed.set(ConeProperty::InclusionExclusionData, false);
prepare_inclusion_exclusion();
if (do_Hilbert_basis) {
// add new points to HilbertBasis
for (size_t i = nr_old_gen; i < nr_gen; ++i) {
if(!inhomogeneous || gen_levels[i]<=1) {
OldCandidates.Candidates.push_back(Candidate<Integer>(Generators[i],*this));
OldCandidates.Candidates.back().original_generator=true;
}
}
OldCandidates.auto_reduce();
}
}
//---------------------------------------------------------------------------
// Constructors
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::reset_tasks(){
do_triangulation = false;
do_partial_triangulation = false;
do_determinants = false;
do_multiplicity=false;
do_integrally_closed = false;
do_Hilbert_basis = false;
do_deg1_elements = false;
keep_triangulation = false;
do_Stanley_dec=false;
do_h_vector=false;
do_hsop = false;
do_excluded_faces=false;
do_approximation=false;
do_default_mode=false;
do_class_group = false;
do_module_gens_intcl = false;
do_module_rank = false;
do_cone_dec=false;
stop_after_cone_dec=false;
do_extreme_rays=false;
do_pointed=false;
do_evaluation = false;
do_only_multiplicity=false;
use_bottom_points = true;
nrSimplicialPyr=0;
totalNrPyr=0;
is_pyramid = false;
triangulation_is_nested = false;
triangulation_is_partial = false;
hilbert_basis_rec_cone_known=false;
}
//---------------------------------------------------------------------------
template<typename Integer>
Full_Cone<Integer>::Full_Cone(const Matrix<Integer>& M, bool do_make_prime){ // constructor of the top cone
omp_start_level=omp_get_level();
dim=M.nr_of_columns();
if(dim>0)
Generators=M;
/*
cout << "------------------" << endl;
cout << "dim " << dim << endl;
M.pretty_print(cout);
cout << "------------------" << endl;
M.transpose().pretty_print(cout);
cout << "==================" << endl;
*/
// assert(M.row_echelon()== dim); rank check now done later
/*index=1; // not used at present
for(size_t i=0;i<dim;++i)
index*=M[i][i];
index=Iabs(index); */
//make the generators coprime, remove 0 rows and duplicates
has_generator_with_common_divisor = false;
if (do_make_prime) {
Generators.make_prime();
} else {
nr_gen = Generators.nr_of_rows();
for (size_t i = 0; i < nr_gen; ++i) {
if (v_gcd(Generators[i]) != 1) {
has_generator_with_common_divisor = true;
break;
}
}
}
Generators.remove_duplicate_and_zero_rows();
nr_gen = Generators.nr_of_rows();
if (nr_gen != static_cast<size_t>(static_cast<key_t>(nr_gen))) {
throw FatalException("Too many generators to fit in range of key_t!");
}
multiplicity = 0;
is_Computed = bitset<ConeProperty::EnumSize>(); //initialized to false
is_Computed.set(ConeProperty::Generators);
pointed = false;
is_simplicial = nr_gen == dim;
deg1_extreme_rays = false;
deg1_generated = false;
deg1_generated_computed = false;
deg1_hilbert_basis = false;
reset_tasks();
Extreme_Rays_Ind = vector<bool>(nr_gen,false);
in_triang = vector<bool> (nr_gen,false);
deg1_triangulation = true;
if(dim==0){ //correction needed to include the 0 cone;
multiplicity = 1;
Hilbert_Series.add(vector<num_t>(1,1),vector<denom_t>());
is_Computed.set(ConeProperty::HilbertSeries);
is_Computed.set(ConeProperty::Triangulation);
}
pyr_level=-1;
Top_Cone=this;
Top_Key.resize(nr_gen);
for(size_t i=0;i<nr_gen;i++)
Top_Key[i]=i;
totalNrSimplices=0;
TriangulationBufferSize=0;
CandidatesSize=0;
detSum = 0;
shift = 0;
FS.resize(omp_get_max_threads());
Pyramids.resize(20); // prepare storage for pyramids
nrPyramids.resize(20,0);
Pyramids_scrambled.resize(20,false);
recursion_allowed=true;
do_all_hyperplanes=true;
// multithreaded_pyramid=true; now in build_cone where it is defined dynamically
nextGen=0;
store_level=0;
Comparisons.reserve(nr_gen);
nrTotalComparisons=0;
inhomogeneous=false;
level0_dim=dim; // must always be defined
use_existing_facets=false;
start_from=0;
old_nr_supp_hyps=0;
verbose=false;
OldCandidates.dual=false;
OldCandidates.verbose=verbose;
NewCandidates.dual=false;
NewCandidates.verbose=verbose;
RankTest = vector< Matrix<Integer> >(omp_get_max_threads(), Matrix<Integer>(0,dim));
do_bottom_dec=false;
suppress_bottom_dec=false;
keep_order=false;
is_approximation=false;
is_global_approximation=false;
PermGens.resize(nr_gen);
for(size_t i=0;i<nr_gen;++i)
PermGens[i]=i;
}
//---------------------------------------------------------------------------
template<typename Integer>
Full_Cone<Integer>::Full_Cone(Cone_Dual_Mode<Integer> &C) {
omp_start_level=omp_get_level();
is_Computed = bitset<ConeProperty::EnumSize>(); //initialized to false
dim = C.dim;
Generators.swap(C.Generators);
nr_gen = Generators.nr_of_rows();
if (Generators.nr_of_rows() > 0)
is_Computed.set(ConeProperty::Generators);
has_generator_with_common_divisor = false;
Extreme_Rays_Ind.swap(C.ExtremeRaysInd);
if (!Extreme_Rays_Ind.empty()) is_Computed.set(ConeProperty::ExtremeRays);
multiplicity = 0;
in_triang = vector<bool>(nr_gen,false);
Basis_Max_Subspace=C.BasisMaxSubspace;
is_Computed.set(ConeProperty::MaximalSubspace);
pointed = (Basis_Max_Subspace.nr_of_rows()==0);
is_Computed.set(ConeProperty::IsPointed);
is_simplicial = nr_gen == dim;
deg1_extreme_rays = false;
deg1_generated = false;
deg1_generated_computed = false;
deg1_triangulation = false;
deg1_hilbert_basis = false;
reset_tasks();
if (!Extreme_Rays_Ind.empty()) { // only then we can assume that all entries on C.Supp.. are relevant
Support_Hyperplanes.swap(C.SupportHyperplanes);
// there may be duplicates in the coordinates of the Full_Cone
Support_Hyperplanes.remove_duplicate_and_zero_rows();
is_Computed.set(ConeProperty::SupportHyperplanes);
}
if(!C.do_only_Deg1_Elements){
Hilbert_Basis.swap(C.Hilbert_Basis);
is_Computed.set(ConeProperty::HilbertBasis);
}
else {
Deg1_Elements.swap(C.Hilbert_Basis);
is_Computed.set(ConeProperty::Deg1Elements);
}
if(dim==0){ //correction needed to include the 0 cone;
multiplicity = 1;
Hilbert_Series.add(vector<num_t>(1,1),vector<denom_t>());
is_Computed.set(ConeProperty::HilbertSeries);
}
pyr_level=-1;
Top_Cone=this;
Top_Key.resize(nr_gen);
for(size_t i=0;i<nr_gen;i++)
Top_Key[i]=i;
totalNrSimplices=0;
TriangulationBufferSize=0;
CandidatesSize=0;
detSum = 0;
shift = 0;
do_all_hyperplanes=true;
tri_recursion=false;
nextGen=0;
inhomogeneous=C.inhomogeneous;
level0_dim=dim; // must always be defined
use_existing_facets=false;
start_from=0;
old_nr_supp_hyps=0;
verbose = C.verbose;
OldCandidates.dual=false;
OldCandidates.verbose=verbose;
NewCandidates.dual=false;
NewCandidates.verbose=verbose;
is_approximation=false;
verbose=C.verbose;
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::check_grading_after_dual_mode(){
if(dim>0 && Grading.size()>0 && !isComputed(ConeProperty::Grading)) {
if(isComputed(ConeProperty::Generators)){
vector<Integer> degrees=Generators.MxV(Grading);
vector<Integer> levels;
if(inhomogeneous)
levels=Generators.MxV(Truncation);
size_t i=0;
for(;i<degrees.size();++i){
if(degrees[i]<=0 &&(!inhomogeneous || levels[i]==0))
break;
}
if(i==degrees.size())
is_Computed.set(ConeProperty::Grading);
}
else if(isComputed(ConeProperty::HilbertBasis)){
auto hb=Hilbert_Basis.begin();
for(;hb!=Hilbert_Basis.end();++hb){
if(v_scalar_product(*hb,Grading)<=0 && (!inhomogeneous || v_scalar_product(*hb,Truncation)==0))
break;
}
if(hb==Hilbert_Basis.end())
is_Computed.set(ConeProperty::Grading);
}
}
if(isComputed(ConeProperty::Deg1Elements)){
auto hb=Deg1_Elements.begin();
for(;hb!=Deg1_Elements.end();++hb){
if(v_scalar_product(*hb,Grading)<=0)
break;
}
if(hb==Deg1_Elements.end())
is_Computed.set(ConeProperty::Grading);
}
if(Grading.size()>0 && !isComputed(ConeProperty::Grading)){
throw BadInputException("Grading not positive on pointed cone.");
}
}
template<typename Integer>
void Full_Cone<Integer>::dual_mode() {
omp_start_level=omp_get_level();
if(dim==0){
set_zero_cone();
return;
}
use_existing_facets=false; // completely irrelevant here
start_from=0;
old_nr_supp_hyps=0;
INTERRUPT_COMPUTATION_BY_EXCEPTION
compute_class_group();
check_grading_after_dual_mode();
if(dim>0 && !inhomogeneous){
deg1_check();
if (isComputed(ConeProperty::Grading) && !isComputed(ConeProperty::Deg1Elements)) {
if (verbose) {
verboseOutput() << "Find degree 1 elements" << endl;
}
select_deg1_elements();
}
}
if(dim==0){
deg1_extreme_rays = deg1_generated = true;
Grading=vector<Integer>(dim);
is_Computed.set(ConeProperty::IsDeg1ExtremeRays);
deg1_generated_computed = true;
is_Computed.set(ConeProperty::Grading);
}
if(!inhomogeneous && isComputed(ConeProperty::HilbertBasis)){
if (isComputed(ConeProperty::Grading))
check_deg1_hilbert_basis();
}
if (inhomogeneous && isComputed(ConeProperty::Generators)) {
set_levels();
find_level0_dim();
find_module_rank();
}
if (inhomogeneous && !isComputed(ConeProperty::Generators) && isComputed(ConeProperty::HilbertBasis)) {
find_level0_dim_from_HB();
find_module_rank();
}
use_existing_facets=false;
start_from=0;
}
//---------------------------------------------------------------------------
/* constructor for pyramids */
template<typename Integer>
Full_Cone<Integer>::Full_Cone(Full_Cone<Integer>& C, const vector<key_t>& Key) {
omp_start_level=C.omp_start_level;
Generators = C.Generators.submatrix(Key);
dim = Generators.nr_of_columns();
nr_gen = Generators.nr_of_rows();
has_generator_with_common_divisor = C.has_generator_with_common_divisor;
is_simplicial = nr_gen == dim;
Top_Cone=C.Top_Cone; // relate to top cone
Top_Key.resize(nr_gen);
for(size_t i=0;i<nr_gen;i++)
Top_Key[i]=C.Top_Key[Key[i]];
multiplicity = 0;
Extreme_Rays_Ind = vector<bool>(nr_gen,false);
is_Computed.set(ConeProperty::ExtremeRays, C.isComputed(ConeProperty::ExtremeRays));
if(isComputed(ConeProperty::ExtremeRays))
for(size_t i=0;i<nr_gen;i++)
Extreme_Rays_Ind[i]=C.Extreme_Rays_Ind[Key[i]];
in_triang = vector<bool> (nr_gen,false);
deg1_triangulation = true;
// not used in a pyramid, but set precaution
deg1_extreme_rays = false;
deg1_generated = false;
deg1_generated_computed = false;
deg1_hilbert_basis = false;
Grading=C.Grading;
is_Computed.set(ConeProperty::Grading, C.isComputed(ConeProperty::Grading));
Order_Vector=C.Order_Vector;
do_extreme_rays=false;
do_triangulation=C.do_triangulation;
do_partial_triangulation=C.do_partial_triangulation;
do_determinants=C.do_determinants;
do_multiplicity=C.do_multiplicity;
do_deg1_elements=C.do_deg1_elements;
do_h_vector=C.do_h_vector;
do_Hilbert_basis=C.do_Hilbert_basis;
keep_triangulation=C.keep_triangulation;
do_only_multiplicity=C.do_only_multiplicity;
do_evaluation=C.do_evaluation;
do_Stanley_dec=C.do_Stanley_dec;
inhomogeneous=C.inhomogeneous; // at present not used in proper pyramids
is_pyramid=true;
pyr_level=C.pyr_level+1;
totalNrSimplices=0;
detSum = 0;
shift = C.shift;
if(C.gen_degrees.size()>0){ // now we copy the degrees
gen_degrees.resize(nr_gen);
if(C.do_h_vector || !using_GMP<Integer>())
gen_degrees_long.resize(nr_gen);
for (size_t i=0; i<nr_gen; i++) {
gen_degrees[i] = C.gen_degrees[Key[i]];
if(C.do_h_vector || !using_GMP<Integer>())
gen_degrees_long[i] = C.gen_degrees_long[Key[i]];
}
}
if(C.gen_levels.size()>0){ // now we copy the levels
gen_levels.resize(nr_gen);
for (size_t i=0; i<nr_gen; i++) {
gen_levels[i] = C.gen_levels[Key[i]];
}
}
TriangulationBufferSize=0;
CandidatesSize=0;
recursion_allowed=C.recursion_allowed; // must be reset if necessary
do_all_hyperplanes=true; // must be reset for non-recursive pyramids
// multithreaded_pyramid=false; // SEE ABOVE
nextGen=0;
store_level = C.store_level;
Comparisons.reserve(nr_gen);
nrTotalComparisons=0;
level0_dim = C.level0_dim; // must always be defined
use_existing_facets=false;
start_from=0;
old_nr_supp_hyps=0;
verbose=false;
OldCandidates.dual=false;
OldCandidates.verbose=verbose;
NewCandidates.dual=false;
NewCandidates.verbose=verbose;
is_approximation = C.is_approximation;
is_global_approximation = C.is_global_approximation;
do_bottom_dec=false;
suppress_bottom_dec=false;
keep_order=true;
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::set_zero_cone() {
assert(dim==0);
if (verbose) {
verboseOutput() << "Zero cone detected!" << endl;
}
// The basis change already is transforming to zero.
is_Computed.set(ConeProperty::Sublattice);
is_Computed.set(ConeProperty::Generators);
is_Computed.set(ConeProperty::ExtremeRays);
Support_Hyperplanes=Matrix<Integer> (0);
is_Computed.set(ConeProperty::SupportHyperplanes);
totalNrSimplices = 0;
is_Computed.set(ConeProperty::TriangulationSize);
detSum = 0;
is_Computed.set(ConeProperty::TriangulationDetSum);
is_Computed.set(ConeProperty::Triangulation);
is_Computed.set(ConeProperty::StanleyDec);
multiplicity = 1;
is_Computed.set(ConeProperty::Multiplicity);
is_Computed.set(ConeProperty::HilbertBasis);
if(!inhomogeneous)
is_Computed.set(ConeProperty::Deg1Elements);
Hilbert_Series = HilbertSeries(vector<num_t>(1,1),vector<denom_t>()); // 1/1
is_Computed.set(ConeProperty::HilbertSeries);
if (!is_Computed.test(ConeProperty::Grading)) {
Grading = vector<Integer>(dim);
// GradingDenom = 1;
is_Computed.set(ConeProperty::Grading);
}
pointed = true;
is_Computed.set(ConeProperty::IsPointed);
deg1_extreme_rays = true;
is_Computed.set(ConeProperty::IsDeg1ExtremeRays);
deg1_hilbert_basis = true;
is_Computed.set(ConeProperty::IsDeg1HilbertBasis);
if (inhomogeneous) { // empty set of solutions
is_Computed.set(ConeProperty::VerticesOfPolyhedron);
module_rank = 0;
is_Computed.set(ConeProperty::ModuleRank);
is_Computed.set(ConeProperty::ModuleGenerators);
level0_dim=0;
is_Computed.set(ConeProperty::RecessionRank);
}
if (!inhomogeneous) {
ClassGroup.resize(1,0);
is_Computed.set(ConeProperty::ClassGroup);
}
if (inhomogeneous || ExcludedFaces.nr_of_rows() != 0) {
multiplicity = 0;
is_Computed.set(ConeProperty::Multiplicity);
Hilbert_Series.reset(); // 0/1
is_Computed.set(ConeProperty::HilbertSeries);
}
}
//---------------------------------------------------------------------------
template<typename Integer>
bool Full_Cone<Integer>::isComputed(ConeProperty::Enum prop) const{
return is_Computed.test(prop);
}
//---------------------------------------------------------------------------
// Data access
//---------------------------------------------------------------------------
template<typename Integer>
size_t Full_Cone<Integer>::getDimension()const{
return dim;
}
//---------------------------------------------------------------------------
template<typename Integer>
size_t Full_Cone<Integer>::getNrGenerators()const{
return nr_gen;
}
//---------------------------------------------------------------------------
template<typename Integer>
bool Full_Cone<Integer>::isPointed()const{
return pointed;
}
//---------------------------------------------------------------------------
template<typename Integer>
bool Full_Cone<Integer>::isDeg1ExtremeRays() const{
return deg1_extreme_rays;
}
template<typename Integer>
bool Full_Cone<Integer>::isDeg1HilbertBasis() const{
return deg1_hilbert_basis;
}
//---------------------------------------------------------------------------
template<typename Integer>
vector<Integer> Full_Cone<Integer>::getGrading() const{
return Grading;
}
//---------------------------------------------------------------------------
template<typename Integer>
mpq_class Full_Cone<Integer>::getMultiplicity()const{
return multiplicity;
}
//---------------------------------------------------------------------------
template<typename Integer>
Integer Full_Cone<Integer>::getShift()const{
return shift;
}
//---------------------------------------------------------------------------
template<typename Integer>
size_t Full_Cone<Integer>::getModuleRank()const{
return module_rank;
}
//---------------------------------------------------------------------------
template<typename Integer>
const Matrix<Integer>& Full_Cone<Integer>::getGenerators()const{
return Generators;
}
//---------------------------------------------------------------------------
template<typename Integer>
vector<bool> Full_Cone<Integer>::getExtremeRays()const{
return Extreme_Rays_Ind;
}
//---------------------------------------------------------------------------
template<typename Integer>
Matrix<Integer> Full_Cone<Integer>::getSupportHyperplanes()const{
return Support_Hyperplanes;
}
//---------------------------------------------------------------------------
template<typename Integer>
Matrix<Integer> Full_Cone<Integer>::getHilbertBasis()const{
if(Hilbert_Basis.empty())
return Matrix<Integer>(0,dim);
else
return Matrix<Integer>(Hilbert_Basis);
}
//---------------------------------------------------------------------------
template<typename Integer>
Matrix<Integer> Full_Cone<Integer>::getModuleGeneratorsOverOriginalMonoid()const{
if(ModuleGeneratorsOverOriginalMonoid.empty())
return Matrix<Integer>(0,dim);
else
return Matrix<Integer>(ModuleGeneratorsOverOriginalMonoid);
}
//---------------------------------------------------------------------------
template<typename Integer>
Matrix<Integer> Full_Cone<Integer>::getDeg1Elements()const{
if(Deg1_Elements.empty())
return Matrix<Integer>(0,dim);
else
return Matrix<Integer>(Deg1_Elements);
}
//---------------------------------------------------------------------------
template<typename Integer>
Matrix<Integer> Full_Cone<Integer>::getExcludedFaces()const{
return(ExcludedFaces);
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::error_msg(string s) const{
errorOutput() <<"\nFull Cone "<< s<<"\n";
}
//---------------------------------------------------------------------------
template<typename Integer>
void Full_Cone<Integer>::print()const{
verboseOutput()<<"\ndim="<<dim<<".\n";
verboseOutput()<<"\nnr_gen="<<nr_gen<<".\n";
// verboseOutput()<<"\nhyp_size="<<hyp_size<<".\n";
verboseOutput()<<"\nGrading is:\n";
verboseOutput()<< Grading;
verboseOutput()<<"\nMultiplicity is "<<multiplicity<<".\n";
verboseOutput()<<"\nGenerators are:\n";
Generators.pretty_print(verboseOutput());
verboseOutput()<<"\nExtreme_rays are:\n";
verboseOutput()<< Extreme_Rays_Ind;
verboseOutput()<<"\nSupport Hyperplanes are:\n";
Support_Hyperplanes.pretty_print(verboseOutput());
verboseOutput()<<"\nHilbert basis is:\n";
verboseOutput()<< Hilbert_Basis;
verboseOutput()<<"\nDeg1 elements are:\n";
verboseOutput()<< Deg1_Elements;
verboseOutput()<<"\nHilbert Series is:\n";
verboseOutput()<<Hilbert_Series;
}
#ifndef NMZ_MIC_OFFLOAD //offload with long is not supported
template class Full_Cone<long>;
#endif
template class Full_Cone<long long>;
template class Full_Cone<mpz_class>;
} //end namespace
|