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
|
/***************************************************************************
*
* Include
*
***************************************************************************/
#include <Python.h>
#include <string>
using std::string;
/*
#include <libnormaliz/cone.h>
#include <libnormaliz/map_operations.h>
#include <libnormaliz/vector_operations.h>
#include <libnormaliz/automorph.h>
*/
#include <libnormaliz/libnormaliz.h>
#ifdef ENFNORMALIZ
using eantic::renf_elem_class;
using eantic::renf_class;
#endif
using libnormaliz::Cone;
// using libnormaliz::ConeProperty;
using libnormaliz::ConeProperties;
using libnormaliz::Sublattice_Representation;
using libnormaliz::Type::InputType;
// using libnormaliz::BoolParam::Param;
using libnormaliz::AutomorphismGroup;
using libnormaliz::Matrix;
#ifdef LIBNORMALIZ_DYNAMIC_BITSET_H
using libnormaliz::dynamic_bitset;
#else
typedef boost::dynamic_bitset<> dynamic_bitset;
#endif
#include <vector>
using std::map;
using std::pair;
using std::vector;
#include <csignal>
typedef int py_size_t;
#include <fstream>
/***************************************************************************
*
* Macros for exception handling
*
***************************************************************************/
#define FUNC_BEGIN try {
#define FUNC_END \
} \
catch (libnormaliz::InterruptException & e) \
{ \
libnormaliz::nmz_interrupted = false; \
PyErr_SetString(PyExc_KeyboardInterrupt, \
"interrupted Normaliz Computation"); \
PyErr_SetInterrupt(); \
PyErr_CheckSignals(); \
return NULL; \
} \
catch (libnormaliz::NormalizException & e) \
{ \
PyErr_SetString(NormalizError, e.what()); \
return NULL; \
} \
catch (std::exception & e) \
{ \
PyErr_SetString(PyNormaliz_cppError, e.what()); \
return NULL; \
}
class PyNormalizInputException : public std::exception {
private:
std::string message_;
public:
explicit PyNormalizInputException(const std::string& message);
virtual const char* what() const throw()
{
return message_.c_str();
}
std::string what_message() const throw()
{
return message_;
}
};
PyNormalizInputException::PyNormalizInputException(const std::string& message)
: message_(message)
{
}
/***************************************************************************
*
* Signal handling
*
***************************************************************************/
static void signal_handler(int signal)
{
libnormaliz::nmz_interrupted = true;
}
// helper class implementing RAII pattern for our custom SIGINT handler;
// it helps ensure we *always* restore the signal handler inside a
// FUNC_BEGIN / FUNC_END block.
class TempSignalHandler {
PyOS_sighandler_t original_handler;
public:
TempSignalHandler() {
original_handler = PyOS_setsig(SIGINT, signal_handler);
}
~TempSignalHandler() {
PyOS_setsig(SIGINT, original_handler);
}
};
/***************************************************************************
*
* Static objects
*
***************************************************************************/
static PyObject* NormalizError;
static PyObject* PyNormaliz_cppError;
static const char* cone_name = "Cone";
static const char* cone_name_long = "Cone<long long>";
static const char* cone_name_renf = "Cone<renf_elem>";
static PyObject* RationalHandler = NULL;
static PyObject* FloatHandler = NULL;
#ifdef ENFNORMALIZ
static PyObject* NumberfieldElementHandler = NULL;
#endif
static PyObject* VectorHandler = NULL;
static PyObject* MatrixHandler = NULL;
/***************************************************************************
*
* Call func on one argument
*
***************************************************************************/
static PyObject* CallPythonFuncOnOneArg(PyObject* function, PyObject* single_arg)
{
PyObject* single_arg_tuple = PyTuple_Pack(1, single_arg);
PyObject* return_obj = PyObject_CallObject(function, single_arg_tuple);
Py_DecRef(single_arg);
Py_DecRef(single_arg_tuple);
return return_obj;
}
/***************************************************************************
*
* Compiler version control
*
***************************************************************************/
#if PY_MAJOR_VERSION >= 3
#define string_check PyUnicode_Check
#else
#define string_check PyString_Check
#endif
#ifndef NMZ_RELEASE
static_assert(
false,
"Your Normaliz version (unknown) is too old! Update to 3.11.0 or newer.");
#endif
#if NMZ_RELEASE < 31100
static_assert(false,
"Your Normaliz version is too old! Update to 3.11.0 or newer.");
#endif
/***************************************************************************
*
* Python-C data conversion functions
*
***************************************************************************/
static string PyUnicodeToString(PyObject* in)
{
if (!string_check(in)) {
throw PyNormalizInputException("input must be a string");
return NULL;
}
#if PY_MAJOR_VERSION >= 3
string out = "";
int length = PyUnicode_GET_LENGTH(in);
for (int i = 0; i < length; i++) {
out += PyUnicode_READ_CHAR(in, i);
}
return out;
#else
char* out = PyString_AsString(in);
return string(out);
#endif
}
static PyObject* StringToPyUnicode(const string &in)
{
#if PY_MAJOR_VERSION >= 3
return PyUnicode_FromString(in.c_str());
#else
return PyString_FromString(in.c_str());
#endif
}
// Boolean conversion
static inline PyObject* BoolToPyBool(bool in)
{
if (in)
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
// Converting MPZ's to PyLong and back via strings. Worst possible solution
// ever.
static bool PyNumberToNmz(PyObject*, mpz_class&);
static bool PyNumberToNmz(PyObject* in, mpq_class& out)
{
if (PyFloat_Check(in)) {
throw PyNormalizInputException("PyFloat not allowed in PyNormaliz input. Must be encoded as string.");
return true;
}
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(in)) {
out = PyInt_AsLong(in);
return true;
}
#endif
if (PyLong_Check(in)) {
mpz_class out_tmp;
bool check = PyNumberToNmz(in, out_tmp);
if (!check) {
return false;
}
out = mpq_class(out_tmp);
return true;
}
if (PyList_CheckExact(in) || PyTuple_CheckExact(in)) {
PyObject* py_num = PySequence_GetItem(in, 0);
PyObject* py_denom = PySequence_GetItem(in, 1);
mpz_class num;
if (!PyNumberToNmz(py_num, num)) {
return false;
}
mpz_class denom;
if (!PyNumberToNmz(py_denom, denom)) {
return false;
}
out = mpq_class(num, denom);
return true;
}
PyObject* in_as_string = PyObject_Str(in);
string s = PyUnicodeToString(in_as_string);
// int check = out.set_str(s.c_str(), 10);
libnormaliz::string2coeff(out,s);
return true;
}
static bool PyNumberToNmz(PyObject* in, mpz_class& out)
{
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(in)) {
out = PyInt_AsLong(in);
return true;
}
#endif
if (!PyLong_Check(in)) {
throw PyNormalizInputException(
"input coeff must be a PyInt or PyLong");
}
int overflow;
long input_long = PyLong_AsLongAndOverflow(in, &overflow);
if (overflow == 0) {
out = mpz_class(input_long);
return true;
}
PyObject* in_as_string = PyObject_Str(in);
string s = PyUnicodeToString(in_as_string);
out.set_str(s.c_str(), 10);
return true;
}
static PyObject* NmzToPyNumber(const mpz_class in)
{
if (in.fits_slong_p()) {
return PyLong_FromLong(in.get_si());
}
// in Python 2, the first argument to PyLong_FromString is not const, thus
// we need to perform a const cast here.
string mpz_as_string = in.get_str(16);
char* mpz_as_c_string = const_cast< char* >(mpz_as_string.c_str());
return PyLong_FromString(mpz_as_c_string, NULL, 16);
}
static PyObject* NmzToPyNumber(const mpq_class in)
{
PyObject* out_list = PyList_New(2);
PyList_SetItem(out_list, 0, NmzToPyNumber(in.get_num()));
PyList_SetItem(out_list, 1, NmzToPyNumber(in.get_den()));
if (RationalHandler != NULL)
out_list = CallPythonFuncOnOneArg(RationalHandler, out_list);
return out_list;
}
static bool PyNumberToNmz(PyObject* in, long long& out)
{
int overflow;
out = PyLong_AsLongLongAndOverflow(in, &overflow);
if (overflow == -1)
throw PyNormalizInputException(
"Cannot store input coefficient in long long");
return true;
}
static PyObject* NmzToPyNumber(unsigned int in)
{
return PyLong_FromUnsignedLong(in);
}
static PyObject* NmzToPyNumber(unsigned long in)
{
return PyLong_FromUnsignedLong(in);
}
static PyObject* NmzToPyNumber(int in)
{
return PyLong_FromLong(in);
}
static PyObject* NmzToPyNumber(long in)
{
return PyLong_FromLong(in);
}
static PyObject* NmzToPyNumber(long long in)
{
return PyLong_FromLongLong(in);
}
static PyObject* NmzToPyNumber(double in)
{
PyObject* x = PyFloat_FromDouble(in);
if(FloatHandler == NULL)
return x;
return CallPythonFuncOnOneArg(FloatHandler, x);
}
template < typename Integer >
static PyObject* NmzVectorToPyList(const vector< Integer >& in,
bool do_callback = true);
#ifdef ENFNORMALIZ
static PyObject* NmzToPyNumber(const renf_elem_class &in)
{
// std::cout << "IIIII " << in << std::endl;
vector< mpz_class > output_nums = in.get_num_vector();
mpz_class output_den = in.get_den();
vector< mpz_class > denoms(output_nums.size(), output_den);
for(size_t i=0; i< output_nums.size(); ++i){
mpq_class quot = output_nums[i];
quot /= output_den;
output_nums[i] = quot.get_num();
denoms[i] = quot.get_den();
}
/*std::cout << "NNN ";
for( size_t i = 0; i< output_nums.size(); ++i)
std::cout << output_nums[i] << " ";
std::cout << std::endl;
std::cout << "DDD ";
for( size_t i = 0; i< output_nums.size(); ++i)
std::cout << denoms[i] << " ";
std::cout << std::endl;*/
// PyObject* denom_py = NmzToPyNumber(output_den);
PyObject* out_list = PyList_New(output_nums.size());
for (size_t i = 0; i < output_nums.size(); i++) {
PyObject* current = PyList_New(2);
PyList_SetItem(current, 0, NmzToPyNumber(output_nums[i]));
// Py_IncRef(denom_py);
PyList_SetItem(current, 1, NmzToPyNumber(denoms[i]));
if (RationalHandler != NULL)
current = CallPythonFuncOnOneArg(RationalHandler, current);
PyList_SetItem(out_list, i, current);
}
// Py_DecRef(denom_py);
if (NumberfieldElementHandler != NULL)
out_list = CallPythonFuncOnOneArg(NumberfieldElementHandler, out_list);
return out_list;
}
#endif
PyObject* NmzToPyNumber(const dynamic_bitset& in)
{
size_t len = in.size();
PyObject* result = PyList_New(len);
for (size_t i = 0; i < len; i++) {
PyList_SetItem(result, i, NmzToPyNumber(in[i] ? 1 : 0));
}
return result;
}
template < typename Integer >
static bool PyListToNmz(vector< Integer >& out, PyObject* in)
{
if (!PySequence_Check(in))
throw PyNormalizInputException("Input list is not a sequence");
const int n = PySequence_Size(in);
out.resize(n);
for (int i = 0; i < n; ++i) {
PyObject* tmp = PySequence_GetItem(in, i);
if (!PyNumberToNmz(tmp, out[i]))
return false;
}
return true;
}
template < typename Integer >
static bool PyIntMatrixToNmz(vector< vector< Integer > >& out, PyObject* in)
{
if (!PySequence_Check(in))
throw PyNormalizInputException("Input matrix is not a sequence");
const int nr = PySequence_Size(in);
out.resize(nr);
for (int i = 0; i < nr; ++i) {
bool okay = PyListToNmz(out[i], PySequence_GetItem(in, i));
if (!okay)
return false;
}
return true;
}
#ifdef ENFNORMALIZ
template < typename NumberField, typename NumberFieldElem >
static bool prepare_nf_input(vector< vector< NumberFieldElem > >& out,
PyObject* in,
NumberField* nf)
{
if (!PySequence_Check(in))
throw PyNormalizInputException("Number field data is not a list");
const int nr = PySequence_Size(in);
out.resize(nr);
for (int i = 0; i < nr; ++i) {
PyObject* current_row = PySequence_GetItem(in, i);
int current_length = PySequence_Size(current_row);
out[i].resize(current_length);
for (int j = 0; j < current_length; j++) {
PyObject* current_element = PySequence_GetItem(current_row, j);
bool current_res;
NumberFieldElem current_elem;
if (PyList_CheckExact(current_element) || PyTuple_CheckExact(current_element)) {
vector< mpq_class > current_vector;
current_res = PyListToNmz(current_vector, current_element);
if (!current_res) {
return false;
}
current_elem = NumberFieldElem(*nf, current_vector);
}
if (string_check(current_element)) {
current_elem = NumberFieldElem(*nf,PyUnicodeToString(current_element));
// current_elem = PyUnicodeToString(current_element);
}
if (PyFloat_Check(current_element)){
throw PyNormalizInputException("Nonintegral numbers must be given as strings");
}
if (PyLong_Check(current_element)) {
mpq_class tmp;
current_res = PyNumberToNmz(current_element, tmp);
if (!current_res) {
return false;
}
current_elem = tmp;
}
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(current_element)) {
current_elem = PyInt_AsLong(current_element);
}
#endif
out[i][j] = current_elem;
}
}
return true;
}
#endif
template < typename Integer >
static bool PyInputToNmz(vector< vector< Integer > >& out, PyObject* in)
{
if (PyIntMatrixToNmz(out, in))
return true;
out.resize(1);
if (PyListToNmz(out[0], in)) {
return true;
}
throw PyNormalizInputException(
"Input could not be converted to vector or list");
}
template < typename Integer >
static PyObject* NmzVectorToPyList(const vector< Integer >& in, bool do_callback)
{
PyObject* vector;
const size_t n = in.size();
vector = PyList_New(n);
for (size_t i = 0; i < n; ++i) {
PyList_SetItem(vector, i, NmzToPyNumber(in[i]));
}
if (do_callback && VectorHandler != NULL)
vector = CallPythonFuncOnOneArg(VectorHandler, vector);
return vector;
}
template < typename Integer >
static PyObject* NmzMatrixToPyList(const vector< vector< Integer > >& in)
{
PyObject* matrix;
const size_t n = in.size();
matrix = PyList_New(n);
for (size_t i = 0; i < n; ++i) {
PyList_SetItem(matrix, i, NmzVectorToPyList(in[i]));
}
if (MatrixHandler != NULL)
matrix = CallPythonFuncOnOneArg(MatrixHandler, matrix);
return matrix;
}
static PyObject* NmzHilbertSeriesToPyList(const libnormaliz::HilbertSeries& HS,
bool is_HSOP)
{
PyObject* return_list = PyList_New(3);
if (is_HSOP) {
PyList_SetItem(return_list, 0, NmzVectorToPyList(HS.getHSOPNum()));
PyList_SetItem(
return_list, 1,
NmzVectorToPyList(libnormaliz::to_vector(HS.getHSOPDenom())));
PyList_SetItem(return_list, 2, NmzToPyNumber(HS.getShift()));
}
else {
PyList_SetItem(return_list, 0, NmzVectorToPyList(HS.getNum()));
PyList_SetItem(
return_list, 1,
NmzVectorToPyList(libnormaliz::to_vector(HS.getDenom())));
PyList_SetItem(return_list, 2, NmzToPyNumber(HS.getShift()));
}
return return_list;
}
template < typename Integer >
static PyObject* NmzWeightedEhrhartSeriesToPyList(
const std::pair< libnormaliz::HilbertSeries, Integer >& HS)
{
PyObject* return_list = PyList_New(4);
PyList_SetItem(return_list, 0, NmzVectorToPyList(HS.first.getNum()));
PyList_SetItem(
return_list, 1,
NmzVectorToPyList(libnormaliz::to_vector(HS.first.getDenom())));
PyList_SetItem(return_list, 2, NmzToPyNumber(HS.first.getShift()));
PyList_SetItem(return_list, 3, NmzToPyNumber(HS.second));
return return_list;
}
template < typename Integer >
static PyObject*
NmzHilbertQuasiPolynomialToPyList(const libnormaliz::HilbertSeries& HS)
{
vector< vector< Integer > > HQ = HS.getHilbertQuasiPolynomial();
const size_t n = HS.getPeriod();
PyObject* return_list = PyList_New(n + 1);
for (size_t i = 0; i < n; ++i) {
PyList_SetItem(return_list, i, NmzVectorToPyList(HQ[i]));
}
PyList_SetItem(return_list, n,
NmzToPyNumber(HS.getHilbertQuasiPolynomialDenom()));
return return_list;
}
template < typename Integer >
static PyObject* NmzWeightedEhrhartQuasiPolynomialToPyList(
const libnormaliz::IntegrationData& int_data)
{
vector< vector< Integer > > ehrhart_qp =
int_data.getWeightedEhrhartQuasiPolynomial();
const size_t n = ehrhart_qp.size();
PyObject* return_list = PyList_New(n + 1);
for (size_t i = 0; i < n; ++i) {
PyList_SetItem(return_list, i, NmzVectorToPyList(ehrhart_qp[i]));
}
PyList_SetItem(
return_list, n,
NmzToPyNumber(int_data.getWeightedEhrhartQuasiPolynomialDenom()));
return return_list;
}
template < typename Integer >
static PyObject* NmzTriangleListToPyList(
const pair<vector<libnormaliz::SHORTSIMPLEX<Integer> >, libnormaliz::Matrix<Integer> >& in)
{
const size_t n = in.first.size();
PyObject* M = PyList_New(n);
for (size_t i = 0; i < n; ++i) {
// convert the pair
PyObject* triple = PyList_New(3);
PyList_SetItem(triple, 0,
NmzVectorToPyList< libnormaliz::key_t >(in.first[i].key));
PyList_SetItem(triple, 1, NmzToPyNumber(in.first[i].vol));
PyList_SetItem(triple, 2, NmzToPyNumber(libnormaliz::bool_to_bitset(in.first[i].Excluded)));
PyList_SetItem(M, i, triple);
}
PyObject* Tr = PyList_New(2);
PyList_SetItem(Tr, 0,M);
PyList_SetItem(Tr, 1,NmzMatrixToPyList(in.second.get_elements()));
return Tr;
}
template < typename Integer >
static PyObject* NmzPairVectorToPyList(
const vector< pair< vector< libnormaliz::key_t >, Integer > >& in)
{
const size_t n = in.size();
PyObject* M = PyList_New(n);
for (size_t i = 0; i < n; ++i) {
// convert the pair
PyObject* pair = PyList_New(2);
PyList_SetItem(pair, 0,
NmzVectorToPyList< libnormaliz::key_t >(in[i].first));
PyList_SetItem(pair, 1, NmzToPyNumber(in[i].second));
PyList_SetItem(M, i, pair);
}
return M;
}
template < typename Integer >
static PyObject*
NmzStanleyDataToPyList(const libnormaliz::STANLEYDATA< Integer >& StanleyData)
{
PyObject* pair = PyList_New(2);
PyList_SetItem(pair, 0,
NmzVectorToPyList< libnormaliz::key_t >(StanleyData.key));
PyList_SetItem(pair, 1,
NmzMatrixToPyList(StanleyData.offsets.get_elements()));
return pair;
}
template < typename Integer >
static PyObject* NmzStanleyDecToPyList(
const std::pair<std::list<libnormaliz::STANLEYDATA<Integer> >, libnormaliz::Matrix<Integer> > & StanleyDec)
{
const size_t n = StanleyDec.first.size();
PyObject* M = PyList_New(n);
typename std::list< libnormaliz::STANLEYDATA< Integer > >::const_iterator S =
StanleyDec.first.begin();
for (size_t i = 0; i < n; ++i) {
PyList_SetItem(M, i, NmzStanleyDataToPyList(*S));
++S;
}
PyObject* St=PyList_New(2);
PyList_SetItem(St,0, M);
PyList_SetItem(St, 1, NmzMatrixToPyList(StanleyDec.second.get_elements()) );
return St;
}
template < typename Integer >
static PyObject* _NmzBasisChangeIntern(Cone< Integer >* C)
{
Sublattice_Representation< Integer > bc = C->getSublattice();
PyObject* res = PyList_New(3);
PyList_SetItem(res, 0, NmzMatrixToPyList(bc.getEmbedding()));
PyList_SetItem(res, 1, NmzMatrixToPyList(bc.getProjection()));
PyList_SetItem(res, 2, NmzToPyNumber(bc.getAnnihilator()));
// Dim, Rank, Equations and Congruences are already covered by special
// functions ditto ExternalIndex
return res;
}
static PyObject*
NmzFacelatticeToPython(const map<dynamic_bitset, int>& lattice)
{
ssize_t len = lattice.size();
PyObject* list = PyList_New(len);
ssize_t curr = 0;
for (auto it = lattice.begin(); it != lattice.end(); it++) {
PyObject* list_int = PyList_New(2);
PyList_SetItem(list_int, 0, NmzToPyNumber(it->first));
PyList_SetItem(list_int, 1, NmzToPyNumber(it->second));
PyList_SetItem(list, curr, list_int);
curr++;
}
return list;
}
static PyObject*
NmzModularGradingsToPython(const vector<vector<dynamic_bitset> >& gradings)
{
ssize_t nr_gradings = gradings.size();
PyObject* grad_list = PyList_New(nr_gradings);
if(nr_gradings == 0)
return grad_list;
size_t curr = 0;
for(auto& this_grad: gradings){
PyObject* list_part = PyList_New(this_grad.size());
size_t inner_curr = 0;
for(auto& part: this_grad){
PyList_SetItem(list_part, inner_curr, NmzToPyNumber(part));
inner_curr++;
}
PyList_SetItem(grad_list, curr, list_part);
curr++;
}
return grad_list;
}
template < typename Integer >
static PyObject*
NmzAutomorphismsToPython(const AutomorphismGroup< Integer >& grp)
{
int list_size = 6;
if(grp.IsInput() || grp.IsAmbient())
list_size =7;
PyObject* list = PyList_New(list_size);
PyList_SetItem(list, 0, NmzToPyNumber(grp.getOrder()));
PyList_SetItem(list, 1, BoolToPyBool(grp.IsIntegralityChecked()));
PyList_SetItem(list, 2, BoolToPyBool(grp.IsIntegral()));
if(grp.IsInput() || grp.IsAmbient()){
PyList_SetItem(list, 6, NmzMatrixToPyList(grp.getGens().get_elements()));
PyObject* current = PyList_New(2);
PyList_SetItem(current, 0, NmzMatrixToPyList(grp.getGensPerms()));
PyList_SetItem(current, 1, NmzMatrixToPyList(grp.getGensOrbits()));
PyList_SetItem(list, 3, current);
current = PyList_New(2);
vector<vector<long> > Empty;
PyList_SetItem(current, 0, NmzMatrixToPyList(Empty));
PyList_SetItem(current, 1, NmzMatrixToPyList(Empty));
PyList_SetItem(list, 4, current);
if(grp.IsAmbient()){
current = PyList_New(2);
PyList_SetItem(current, 0, NmzMatrixToPyList(grp.getLinFormsPerms()));
PyList_SetItem(current, 1, NmzMatrixToPyList(grp.getLinFormsOrbits()));
PyList_SetItem(list, 5, current);
}
else{
vector<vector<long> > Empty;
PyList_SetItem(current, 0, NmzMatrixToPyList(Empty));
PyList_SetItem(current, 1, NmzMatrixToPyList(Empty));
PyList_SetItem(list, 5, current);
}
}
else{
PyObject* current = PyList_New(2);
PyList_SetItem(current, 0, NmzMatrixToPyList(grp.getExtremeRaysPerms()));
PyList_SetItem(current, 1, NmzMatrixToPyList(grp.getExtremeRaysOrbits()));
PyList_SetItem(list, 3, current);
current = PyList_New(2);
PyList_SetItem(current, 0, NmzMatrixToPyList(grp.getVerticesPerms()));
PyList_SetItem(current, 1, NmzMatrixToPyList(grp.getVerticesOrbits()));
PyList_SetItem(list, 4, current);
current = PyList_New(2);
PyList_SetItem(current, 0,
NmzMatrixToPyList(grp.getSupportHyperplanesPerms()));
PyList_SetItem(current, 1,
NmzMatrixToPyList(grp.getSupportHyperplanesOrbits()));
PyList_SetItem(list, 5, current);
}
return list;
}
template < typename Integer >
static PyObject*
NmzFusionDataToPython(const vector<vector<Matrix<Integer> > >& FusData)
{
int outer_list_size = FusData.size();
PyObject* outer_list = PyList_New(outer_list_size);
for(int ring = 0; ring < outer_list_size; ring++){
int inner_list_size = FusData[ring].size();
PyObject* inner_list = PyList_New(inner_list_size);
for(int mat = 0; mat < inner_list_size; mat++){
PyList_SetItem(inner_list, mat, NmzMatrixToPyList(FusData[ring][mat].get_elements()));
}
PyList_SetItem(outer_list, ring, inner_list);
}
return outer_list;
}
/***************************************************************************
*
* PyCapsule handler functions
*
***************************************************************************/
#ifdef ENFNORMALIZ
struct NumberFieldCone {
const renf_class* nf;
Cone< renf_elem_class >* cone;
};
#endif
static void delete_cone_mpz(PyObject* cone)
{
Cone< mpz_class >* cone_ptr = reinterpret_cast< Cone< mpz_class >* >(
PyCapsule_GetPointer(cone, cone_name));
delete cone_ptr;
}
static void delete_cone_long(PyObject* cone)
{
Cone< long long >* cone_ptr = reinterpret_cast< Cone< long long >* >(
PyCapsule_GetPointer(cone, cone_name_long));
delete cone_ptr;
}
#ifdef ENFNORMALIZ
static void delete_cone_renf(PyObject* cone)
{
NumberFieldCone* cone_ptr = reinterpret_cast< NumberFieldCone* >(
PyCapsule_GetPointer(cone, cone_name_renf));
delete cone_ptr->cone;
// delete cone_ptr->nf;
}
#endif
static Cone< long long >* get_cone_long(PyObject* cone)
{
return reinterpret_cast< Cone< long long >* >(
PyCapsule_GetPointer(cone, cone_name_long));
}
static Cone< mpz_class >* get_cone_mpz(PyObject* cone)
{
return reinterpret_cast< Cone< mpz_class >* >(
PyCapsule_GetPointer(cone, cone_name));
}
#ifdef ENFNORMALIZ
static Cone< renf_elem_class >* get_cone_renf(PyObject* cone)
{
NumberFieldCone* cone_ptr = reinterpret_cast< NumberFieldCone* >(
PyCapsule_GetPointer(cone, cone_name_renf));
return cone_ptr->cone;
}
static const renf_class* get_cone_renf_renf(PyObject* cone)
{
NumberFieldCone* cone_ptr = reinterpret_cast< NumberFieldCone* >(
PyCapsule_GetPointer(cone, cone_name_renf));
return cone_ptr->nf;
}
#endif
static PyObject* pack_cone(Cone< mpz_class >* C, const void* dummy = nullptr)
{
return PyCapsule_New(reinterpret_cast< void* >(C), cone_name,
&delete_cone_mpz);
}
static PyObject* pack_cone(Cone< long long >* C, const void* dummy = nullptr)
{
return PyCapsule_New(reinterpret_cast< void* >(C), cone_name_long,
&delete_cone_long);
}
#ifdef ENFNORMALIZ
static PyObject* pack_cone(Cone< renf_elem_class >* C, const void* nf)
{
NumberFieldCone* cone_ptr = new NumberFieldCone();
cone_ptr->nf = reinterpret_cast< const renf_class* >(nf);
cone_ptr->cone = C;
return PyCapsule_New(reinterpret_cast< void* >(cone_ptr), cone_name_renf,
&delete_cone_renf);
}
#endif
static bool is_cone(PyObject* cone)
{
if (PyCapsule_CheckExact(cone)) {
const char *name = PyCapsule_GetName(cone);
return !strcmp(name, cone_name) || !strcmp(name, cone_name_long) ||
!strcmp(name, cone_name_renf);
}
return false;
}
static bool is_cone_mpz(PyObject* cone)
{
if (PyCapsule_CheckExact(cone)) {
const char *name = PyCapsule_GetName(cone);
return !strcmp(name, cone_name);
}
return false;
}
static bool is_cone_long(PyObject* cone)
{
if (PyCapsule_CheckExact(cone)) {
const char *name = PyCapsule_GetName(cone);
return !strcmp(name, cone_name_long);
}
return false;
}
#ifdef ENFNORMALIZ
static bool is_cone_renf(PyObject* cone)
{
if (PyCapsule_CheckExact(cone)) {
const char *name = PyCapsule_GetName(cone);
return !strcmp(name, cone_name_renf);
}
return false;
}
#endif
/***************************************************************************
*
* Cone property list
*
***************************************************************************/
/*
@Name NmzListConeProperties
@Arguments none
@Description
Returns two lists of strings.
The first list are all cone properties that define compute
goals in Normaliz (see Normaliz manual for details)
The second list are all cone properties that define internal
control flow control in Normaliz, and which should not be used
to get results of computations.
All entries of the first list can be passed to NmzResult
to get the result of a normaliz computation.
All entries of the second list can be passed to NmzCompute
to set different options for Normaliz computations.
*/
static PyObject* NmzListConeProperties(PyObject* args)
{
FUNC_BEGIN
PyObject* return_list = PyList_New(2);
ConeProperties goals = libnormaliz::all_goals();
ConeProperties options = libnormaliz::all_options();
int number_goals = goals.count();
int number_options = options.count();
PyObject* goal_list = PyList_New(number_goals);
PyObject* option_list = PyList_New(number_options);
PyList_SetItem(return_list, 0, goal_list);
PyList_SetItem(return_list, 1, option_list);
int list_position = 0;
for (int i = 0; i < libnormaliz::ConeProperty::EnumSize; i++) {
if (goals.test(static_cast< libnormaliz::ConeProperty::Enum >(i))) {
string name = libnormaliz::toString(
static_cast< libnormaliz::ConeProperty::Enum >(i));
PyList_SetItem(goal_list, list_position, StringToPyUnicode(name));
list_position++;
}
}
list_position = 0;
for (int i = 0; i < libnormaliz::ConeProperty::EnumSize; i++) {
if (options.test(static_cast< libnormaliz::ConeProperty::Enum >(i))) {
string name = libnormaliz::toString(
static_cast< libnormaliz::ConeProperty::Enum >(i));
PyList_SetItem(option_list, list_position,
StringToPyUnicode(name));
list_position++;
}
}
return return_list;
FUNC_END
}
/***************************************************************************
*
* NmzCone
*
***************************************************************************/
template < typename Integer >
static PyObject* _NmzConeIntern(PyObject* kwargs)
{
map< InputType, vector< vector< mpq_class > > > input;
bool grading_polynomial = false;
string polynomial;
if (kwargs != NULL) {
PyObject* keys = PyDict_Keys(kwargs);
PyObject* values = PyDict_Values(kwargs);
const int length = PySequence_Size(keys);
for (int i = 0; i < length; i++) {
string type_string =
PyUnicodeToString(PySequence_GetItem(keys, i));
if (type_string == "CreateAsLongLong") {
continue;
}
PyObject* current_value = PySequence_GetItem(values, i);
if (current_value == Py_None)
continue;
if (type_string.compare("polynomial") == 0) {
polynomial = PyUnicodeToString(current_value);
grading_polynomial = true;
continue;
}
vector< vector< mpq_class > > Mat;
try {
PyInputToNmz(Mat, current_value);
}
catch (PyNormalizInputException& e) {
PyErr_SetString(PyNormaliz_cppError,
(string("When parsing ") + type_string +
": " + e.what_message())
.c_str());
return NULL;
}
input[libnormaliz::to_type(type_string)] = Mat;
}
}
Cone< Integer >* C = new Cone< Integer >(input);
if (grading_polynomial) {
C->setPolynomial(polynomial);
}
PyObject* return_container = pack_cone(C);
return return_container;
}
#ifdef ENFNORMALIZ
static PyObject* _NmzConeIntern_renf(PyObject* kwargs)
{
FUNC_BEGIN
PyObject* number_field_data =
PyDict_GetItemString(kwargs, "number_field");
if (number_field_data == NULL) {
PyErr_SetString(PyNormaliz_cppError, "no number field data given");
return NULL;
}
if (!PySequence_Check(number_field_data)) {
PyErr_SetString(PyNormaliz_cppError,
"number field data must be a list");
return NULL;
}
if (PySequence_Size(number_field_data) != 3) {
PyErr_SetString(
PyNormaliz_cppError,
"number field data must be a list with three entries");
return NULL;
}
// number_field_data contains 3 entries: poly, var, emb
// All are strings
string poly = PyUnicodeToString(PySequence_GetItem(number_field_data, 0));
string var = PyUnicodeToString(PySequence_GetItem(number_field_data, 1));
string emb = PyUnicodeToString(PySequence_GetItem(number_field_data, 2));
// boost::intrusive_ptr<const renf_class>* renf = new boost::intrusive_ptr<const renf_class>;
boost::intrusive_ptr<const renf_class> renf = renf_class::make(poly, var, emb);
const renf_class* my_renf = renf.get();
map< InputType, vector< vector< renf_elem_class > > > input;
/* Do not delete entry of kwargs dict, as it might not
be owned by the cone constructor */
// PyDict_DelItemString(kwargs,"number_field");
if (kwargs != NULL) {
PyObject* keys = PyDict_Keys(kwargs);
PyObject* values = PyDict_Values(kwargs);
const int length = PySequence_Size(keys);
for (int i = 0; i < length; i++) {
string type_string =
PyUnicodeToString(PySequence_GetItem(keys, i));
if (type_string == "number_field")
continue;
PyObject* current_value = PySequence_GetItem(values, i);
if (current_value == Py_None)
continue;
vector< vector< renf_elem_class > > Mat;
try {
prepare_nf_input(Mat, current_value, my_renf);
}
catch (PyNormalizInputException& e) {
PyErr_SetString(PyNormaliz_cppError,
(string("When parsing ") + type_string +
": " + e.what_message())
.c_str());
return NULL;
}
input[libnormaliz::to_type(type_string)] = Mat;
}
}
Cone< renf_elem_class >* C = new Cone< renf_elem_class >(input);
C->setRenf(my_renf);
PyObject* return_container = pack_cone(C, my_renf);
return return_container;
FUNC_END
}
#endif
static PyObject* _NmzConeFromFile(PyObject* kwargs)
{
static const char* from_file = "file";
PyObject* create_from_file = StringToPyUnicode(from_file);
PyObject* FileName = PyDict_GetItem(kwargs, create_from_file);
string project(PyUnicodeToString(FileName));
std::string name_in = project + ".in";
const char* file_in = name_in.c_str();
#ifdef ENFNORMALIZ
std::ifstream in;
in.open(file_in, std::ifstream::in);
if (!in.is_open()) {
string message = "error: Failed to open file " + name_in;
throw libnormaliz::BadInputException(message);
}
bool number_field_in_input = false;
std::string poly, var, emb;
std::string test;
while(in.good()){
in >> test;
if(test == "number_field"){
number_field_in_input = true;
libnormaliz::read_number_field_strings(in, poly, var, emb);
break;
}
}
in.close();
if(number_field_in_input){
boost::intrusive_ptr<const renf_class> renf = renf_class::make(poly, var, emb);
const renf_class* my_renf = renf.get();
Cone< renf_elem_class >* C = new Cone< renf_elem_class >(project);
PyObject* return_container = pack_cone(C, my_renf);
return return_container;
}
#endif
static const char* string_for_long_long = "CreateAsLongLong";
PyObject* create_as_long_long = StringToPyUnicode(string_for_long_long);
if (PyDict_Contains(kwargs, create_as_long_long) == 1) {
Cone< long long >* C = new Cone< long long >(project);
PyObject* return_container = pack_cone(C);
return return_container;
}
else{
Cone< mpz_class >* C = new Cone< mpz_class >(project);
PyObject* return_container = pack_cone(C);
return return_container;
}
}
/*
@Name NmzCone
@Arguments <keywords>
@Description
Constructs a normaliz cone object. The keywords must be
Normaliz input types, and the values for the keys matrices
(consisting of either Longs, Floats, or strings for rationals),
lists for single vector input types, or bools for boolean input type.
Special cases are a string describing a polynomial for the polynomial
input type, and the CreateAsLongLong keyword to restrict normaliz computations
to machine integers instead of arbitrary precision numbers.
*/
static PyObject* _NmzCone(PyObject* self, PyObject* args, PyObject* kwargs)
{
FUNC_BEGIN
static const char* from_file = "file";
PyObject* create_from_file = StringToPyUnicode(from_file);
if (kwargs != NULL && PyDict_Contains(kwargs, create_from_file) == 1) {
return _NmzConeFromFile(kwargs);
}
static const char* string_for_long = "CreateAsLongLong";
PyObject* create_as_long_long = StringToPyUnicode(string_for_long);
#ifdef ENFNORMALIZ
static const char* string_for_renf = "number_field";
PyObject* create_as_renf = StringToPyUnicode(string_for_renf);
#endif
if (kwargs != NULL && PyDict_Contains(kwargs, create_as_long_long) == 1) {
create_as_long_long = PyDict_GetItem(kwargs, create_as_long_long);
if (create_as_long_long == Py_True) {
return _NmzConeIntern< long long >(kwargs);
}
}
#ifdef ENFNORMALIZ
else if (kwargs != NULL && PyDict_Contains(kwargs, create_as_renf) == 1) {
return _NmzConeIntern_renf(kwargs);
}
#endif
return _NmzConeIntern< mpz_class >(kwargs);
FUNC_END
}
/*
@Name NmzConeCopy
@Arguments Cone
@Description
Returns a copy of the cone.
*/
static PyObject* _NmzConeCopy(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
Cone< mpz_class >* new_cone = new Cone< mpz_class >(*cone_ptr);
return pack_cone(new_cone);
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
Cone< long long >* new_cone = new Cone< long long >(*cone_ptr);
return pack_cone(new_cone);
}
#ifdef ENFNORMALIZ
else if (is_cone_renf(cone)) {
Cone< renf_elem_class >* cone_ptr = get_cone_renf(cone);
Cone< renf_elem_class >* new_cone =
new Cone< renf_elem_class >(*cone_ptr);
return pack_cone(new_cone, get_cone_renf_renf(cone));
}
#endif
Py_RETURN_NONE;
FUNC_END
}
/***************************************************************************
*
* NmzHilbertSeries
*
***************************************************************************/
/* SUPERFLUOUS
template < typename Integer >
static PyObject* NmzHilbertSeries(Cone< Integer >* C, PyObject* args)
{
FUNC_BEGIN
const int arg_len = PyTuple_Size(args);
if (arg_len == 1) {
bool is_HSOP = C->isComputed(libnormaliz::ConeProperty::HSOP);
return NmzHilbertSeriesToPyList(C->getHilbertSeries(), is_HSOP);
}
PyObject* is_HSOP = PyTuple_GetItem(args, 1);
if (is_HSOP == Py_True) {
if (!C->isComputed(libnormaliz::ConeProperty::HSOP))
C->compute(libnormaliz::ConeProperty::HSOP);
return NmzHilbertSeriesToPyList(C->getHilbertSeries(), true);
}
else {
return NmzHilbertSeriesToPyList(C->getHilbertSeries(), false);
}
FUNC_END
}
static PyObject* NmzHilbertSeries_Outer(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
TempSignalHandler tmpHandler; // use custom signal handler
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
return NmzHilbertSeries(cone_ptr, args);
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
return NmzHilbertSeries(cone_ptr, args);
}
else {
PyErr_SetString(PyNormaliz_cppError,
"Hilbert series not available for renf cone");
return NULL;
}
FUNC_END
}
*/
/***************************************************************************
*
* NmzCompute
*
***************************************************************************/
template < typename Integer >
static PyObject* _NmzCompute(Cone< Integer >* C, PyObject* args)
{
FUNC_BEGIN
const int arg_len = PyTuple_Size(args);
PyObject* to_compute;
if (arg_len == 2) {
PyObject* first_arg = PyTuple_GetItem(args, 1);
if (PyList_Check(first_arg) || PyTuple_Check(first_arg)) {
to_compute = first_arg;
Py_IncRef(to_compute);
}
else {
to_compute = PyList_New(1);
int result = PyList_SetItem(to_compute, 0, first_arg);
if (result != 0) {
PyErr_SetString(PyNormaliz_cppError,
"List could not be created");
Py_DecRef(to_compute);
return NULL;
}
}
}
else {
to_compute = PyList_New(arg_len - 1);
for (int i = 1; i < arg_len; i++) {
PyList_SetItem(to_compute, i-1, PyTuple_GetItem(args, i));
}
}
ConeProperties propsToCompute;
const int n = PySequence_Size(to_compute);
for (int i = 0; i < n; ++i) {
PyObject* prop = PySequence_GetItem(to_compute, i);
if (!string_check(prop)) {
PyErr_SetString(PyNormaliz_cppError,
"All elements must be strings");
Py_DecRef(to_compute);
return NULL;
}
string prop_str(PyUnicodeToString(prop));
propsToCompute.set(libnormaliz::toConeProperty(prop_str));
}
ConeProperties notComputed = C->compute(propsToCompute);
// Cone.compute returns the not computed properties
// we return a bool, true when everything requested was computed
Py_DecRef(to_compute);
return BoolToPyBool(notComputed.goals().none());
FUNC_END
}
static PyObject* _NmzCompute_Outer(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
PyObject* result = NULL;
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
TempSignalHandler tmpHandler; // use custom signal handler
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
result = _NmzCompute(cone_ptr, args);
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
result = _NmzCompute(cone_ptr, args);
}
#ifdef ENFNORMALIZ
else if (is_cone_renf(cone)) {
Cone< renf_elem_class >* cone_ptr = get_cone_renf(cone);
result = _NmzCompute(cone_ptr, args);
}
#endif
return result;
FUNC_END
}
/***************************************************************************
*
* NmzModify
*
***************************************************************************/
template<typename Integer>
PyObject* _NmzModify(Cone<Integer>* cone, PyObject* args)
{
string property = PyUnicodeToString( PyTuple_GetItem(args, 1) );
PyObject* matrix_py = PyTuple_GetItem(args,2);
vector<vector<Integer>> mat;
PyInputToNmz( mat,matrix_py );
cone->modifyCone(libnormaliz::to_type(property),mat);
Py_RETURN_TRUE;
}
#ifdef ENFNORMALIZ
PyObject* _NmzModify_Renf(Cone<renf_elem_class>* cone, const renf_class* nf, PyObject* args)
{
string property = PyUnicodeToString( PyTuple_GetItem(args, 1) );
PyObject* matrix_py = PyTuple_GetItem(args,2);
vector<vector<renf_elem_class>> mat;
prepare_nf_input( mat,matrix_py,nf );
cone->modifyCone(libnormaliz::to_type(property),mat);
Py_RETURN_TRUE;
}
#endif
PyObject* _NmzModify_Outer(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
TempSignalHandler tmpHandler; // use custom signal handler
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
return _NmzModify(cone_ptr, args);
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
return _NmzModify(cone_ptr, args);
}
#ifdef ENFNORMALIZ
else if (is_cone_renf(cone)) {
Cone< renf_elem_class >* cone_ptr = get_cone_renf(cone);
const renf_class* nf = get_cone_renf_renf(cone);
return _NmzModify_Renf(cone_ptr, nf, args);
}
#endif
Py_RETURN_TRUE;
FUNC_END
}
/***************************************************************************
*
* NmzIsComputed
*
***************************************************************************/
/*
@Name NmzIsComputed
@Arguments <cone>, <property_string>
@Desctiption
Returns if the cone property <property_string> is computed in the cone <cone>.
*/
template < typename Integer >
static PyObject* NmzIsComputed(Cone< Integer >* C, PyObject* prop)
{
FUNC_BEGIN
libnormaliz::ConeProperty::Enum p =
libnormaliz::toConeProperty(PyUnicodeToString(prop));
return BoolToPyBool(C->isComputed(p));
FUNC_END
}
static PyObject* NmzIsComputed_Outer(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
PyObject* to_compute = PyTuple_GetItem(args, 1);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
return NmzIsComputed(cone_ptr, to_compute);
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
return NmzIsComputed(cone_ptr, to_compute);
}
#ifdef ENFNORMALIZ
else if (is_cone_renf(cone)) {
Cone< renf_elem_class >* cone_ptr = get_cone_renf(cone);
return NmzIsComputed(cone_ptr, to_compute);
}
#endif
Py_RETURN_FALSE;
FUNC_END
}
/***************************************************************************
*
* NmzSetGrading
*
***************************************************************************/
template < typename Integer >
static PyObject* NmzSetGrading_inner(Cone< Integer >* cone, PyObject* grad)
{
vector< Integer > grad_c;
bool result = PyListToNmz(grad_c, grad);
if (!result) {
PyErr_SetString(PyNormaliz_cppError,
"grading argument is not an integer list");
return NULL;
}
cone->resetGrading(grad_c);
Py_RETURN_NONE;
}
#ifdef ENFNORMALIZ
template <>
PyObject* NmzSetGrading_inner(Cone< renf_elem_class >* cone, PyObject* grad)
{
vector< renf_elem_class > grad_renf;
vector<vector< renf_elem_class> > grad_mat; // a cheap way to convert vectors
PyObject* PyHelpMat = PyList_New(1); // better: rebuild conversion to renf
PyList_SetItem(PyHelpMat, 0, grad);
prepare_nf_input(grad_mat, PyHelpMat,cone->getRenf());
grad_renf = grad_mat[0];
cone->resetGrading(grad_renf);
Py_RETURN_NONE;
}
#endif
static PyObject* NmzSetGrading(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
PyObject* grading_py = PyTuple_GetItem(args, 1);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
return NmzSetGrading_inner(cone_ptr, grading_py);
}
if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
return NmzSetGrading_inner(cone_ptr, grading_py);
}
#ifdef ENFNORMALIZ
if (is_cone_renf(cone)) {
Cone< renf_elem_class >* cone_ptr = get_cone_renf(cone);
return NmzSetGrading_inner(cone_ptr, grading_py);
}
#endif
FUNC_END
Py_RETURN_NONE;
}
/***************************************************************************
*
* Boolean parameters
*
***************************************************************************/
template < typename Integer >
static PyObject* NmzSetBoolParam_inner(Cone<Integer>* cone_ptr, const libnormaliz::BoolParam::Param bool_param, bool value){
if(bool_param == libnormaliz::BoolParam::verbose)
cone_ptr->setVerbose(value);
if(bool_param == libnormaliz::BoolParam::nonnegative)
cone_ptr->setNonnegative(value);
if(bool_param == libnormaliz::BoolParam::total_degree)
cone_ptr->setTotalDegree(value);
if(bool_param == libnormaliz::BoolParam::convert_equations)
cone_ptr->setConvertEquations(value);
if(bool_param == libnormaliz::BoolParam::no_coord_transf)
cone_ptr->setNoCoordTransf(value);
if(bool_param == libnormaliz::BoolParam::list_polynomials)
cone_ptr->setListPolynomials(value);
if(bool_param == libnormaliz::BoolParam::no_pos_orth_def)
cone_ptr->setNoPosOrthDef(value);
if(bool_param == libnormaliz::BoolParam::not_a_bool_param)
throw libnormaliz::BadInputException("Invalid boolean parameter");
Py_RETURN_NONE;
}
static PyObject* NmzSetBoolParam(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
PyObject* bool_param_py = PyTuple_GetItem(args, 1);
PyObject* bool_value_py = PyTuple_GetItem(args,2);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
string param_as_string = PyUnicodeToString(bool_param_py);
libnormaliz::BoolParam::Param bool_param = libnormaliz::to_boolpar(param_as_string);
if (bool_value_py != Py_True && bool_value_py != Py_False) {
PyErr_SetString(PyNormaliz_cppError,
"Argument must be True or False");
return NULL;
}
bool bool_value = (bool_value_py == Py_True);
if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
return NmzSetBoolParam_inner(cone_ptr, bool_param, bool_value);
}
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
return NmzSetBoolParam_inner(cone_ptr, bool_param, bool_value);
}
#ifdef ENFNORMALIZ
if (is_cone_renf(cone)) {
Cone< renf_elem_class >* cone_ptr = get_cone_renf(cone);
return NmzSetBoolParam_inner(cone_ptr, bool_param, bool_value);
}
#endif
FUNC_END
Py_RETURN_NONE;
}
/***************************************************************************
*
* NmzSetProjectionCoords
*
***************************************************************************/
template < typename Integer >
static PyObject* NmzSetProjectionCoords_inner(Cone< Integer >* cone, PyObject* coords)
{
vector< Integer > coords_c;
bool result = PyListToNmz(coords_c, coords);
if (!result) {
PyErr_SetString(PyNormaliz_cppError,
" is not an integer list");
return NULL;
}
for(size_t i=0; i< coords_c.size(); ++i){
if(coords_c[i]!=0 && coords_c[i]!=1)
PyErr_SetString(PyNormaliz_cppError, "Projection coordinates must be 0 or 1");
}
cone->resetProjectionCoords(coords_c);
Py_RETURN_NONE;
}
#ifdef ENFNORMALIZ
template <>
PyObject* NmzSetProjectionCoords_inner(Cone< renf_elem_class >* cone, PyObject* coords)
{
vector< renf_elem_class > coords_renf;
vector<vector< renf_elem_class> > coords_mat; // a cheap way to convert vectors
PyObject* PyHelpMat = PyList_New(1); // better: rebuild conversion to renf
PyList_SetItem(PyHelpMat, 0, coords);
prepare_nf_input(coords_mat, PyHelpMat,cone->getRenf());
coords_renf = coords_mat[0];
cone->resetProjectionCoords(coords_renf);
Py_RETURN_NONE;
}
#endif
static PyObject* NmzSetProjectionCoords(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
PyObject* coords_py = PyTuple_GetItem(args, 1);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
return NmzSetProjectionCoords_inner(cone_ptr, coords_py);
}
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
return NmzSetProjectionCoords_inner(cone_ptr, coords_py);
}
#ifdef ENFNORMALIZ
if (is_cone_renf(cone)) {
Cone< renf_elem_class >* cone_ptr = get_cone_renf(cone);
return NmzSetProjectionCoords_inner(cone_ptr, coords_py);
}
#endif
FUNC_END
Py_RETURN_NONE;
}
/***************************************************************************
*
* NmzResult
*
***************************************************************************/
/*
@Name NmzResult
@Arguments <cone>,<cone property string>,<keys>
@Description
Returns the cone property belonging to the string <cone property string> of
cone <cone>. Please see the Normaliz manual for details on which cone
properties are available. Here are some special outputs that might differ from
Normaliz:
* HilbertSeries and WeightedEhrhartSeries
The returned object is a list with three entries: The first one describes
the numerator of the Hilbert series, the second one the denominator, and the
last one is the shift. If you pass the HSOP option, output will be done in
HSOP format.
* Grading
Returns a list with two entries. First is the grading, second one is the
grading denominator.
* Sublattice
Returns a list with three entries. First is the embedding of the sublattice,
second is the projection third is the annihilator.
* IntegerHull and ProjectCone return new cones.
* StanleyDec
Returns a list containing the Stanley decomposition. All entries are
2-tuples. First entry in the tuple is the key, second the decomposition data.
*/
template < typename Integer >
static PyObject*
_NmzResultImpl(Cone< Integer >* C, PyObject* prop_obj, const void* nf = nullptr)
{
string prop = PyUnicodeToString(prop_obj);
libnormaliz::ConeProperty::Enum p = libnormaliz::toConeProperty(prop);
ConeProperties notComputed;
{
TempSignalHandler tmpHandler; // use custom signal handler
notComputed = C->compute(ConeProperties(p));
}
if (notComputed.goals().any()) {
Py_RETURN_NONE;
}
// Handle standard cases
libnormaliz::OutputType::Enum outputtype = libnormaliz::output_type(p);
switch (p) {
case libnormaliz::ConeProperty::Triangulation:{
return NmzTriangleListToPyList< Integer >(C->getTriangulation());
}
case libnormaliz::ConeProperty::ConeDecomposition:{
return NmzTriangleListToPyList< Integer >(C->getConeDecomposition());
}
case libnormaliz::ConeProperty::AllGeneratorsTriangulation:
return NmzTriangleListToPyList< Integer >(C->getTriangulation(
libnormaliz::ConeProperty::AllGeneratorsTriangulation));
case libnormaliz::ConeProperty::LatticePointTriangulation:
return NmzTriangleListToPyList< Integer >(C->getTriangulation(
libnormaliz::ConeProperty::LatticePointTriangulation));
case libnormaliz::ConeProperty::UnimodularTriangulation:
return NmzTriangleListToPyList< Integer >(C->getTriangulation(
libnormaliz::ConeProperty::UnimodularTriangulation));
case libnormaliz::ConeProperty::HilbertSeries: {
bool is_HSOP = C->isComputed(libnormaliz::ConeProperty::HSOP);
return NmzHilbertSeriesToPyList(C->getHilbertSeries(), is_HSOP);
}
case libnormaliz::ConeProperty::EhrhartSeries: {
bool is_HSOP = C->isComputed(libnormaliz::ConeProperty::HSOP);
return NmzHilbertSeriesToPyList(C->getEhrhartSeries(), is_HSOP);
}
case libnormaliz::ConeProperty::WeightedEhrhartSeries:
return NmzWeightedEhrhartSeriesToPyList( C->getWeightedEhrhartSeries());
// though Grading has the return type vector<Integer> we make it
// a complex structure within PyNormaliz since we want to combine it
// with the grading denominator
case libnormaliz::ConeProperty::Grading: {
vector< Integer > grad = C->getGrading();
Integer denom = C->getGradingDenom();
PyObject* return_list = PyList_New(2);
PyList_SetItem(return_list, 0, NmzVectorToPyList(grad));
PyList_SetItem(return_list, 1, NmzToPyNumber(denom));
return return_list;
}
case libnormaliz::ConeProperty::StanleyDec:
return NmzStanleyDecToPyList(C->getStanleyDec());
case libnormaliz::ConeProperty::InclusionExclusionData:
return NmzPairVectorToPyList< long >(
C->getInclusionExclusionData());
/* returned as a matrix, no need to make it a complex property
case libnormaliz::ConeProperty::Equations:
return NmzMatrixToPyList(C->getSublattice().getEquations());
case libnormaliz::ConeProperty::Congruences:
return NmzMatrixToPyList(C->getSublattice().getCongruences());
*/
case libnormaliz::ConeProperty::Sublattice:
return _NmzBasisChangeIntern(C);
case libnormaliz::ConeProperty::ExternalIndex:
return NmzToPyNumber(C->getSublattice().getExternalIndex());
case libnormaliz::ConeProperty::IntegerHull: {
Cone< Integer >* hull =
new Cone< Integer >(C->getIntegerHullCone());
return pack_cone(hull, nf);
}
case libnormaliz::ConeProperty::ProjectCone: {
Cone< Integer >* projection =
new Cone< Integer >(C->getProjectCone());
return pack_cone(projection, nf);
}
case libnormaliz::ConeProperty::HilbertQuasiPolynomial:
return NmzHilbertQuasiPolynomialToPyList< mpz_class >(
C->getHilbertSeries());
case libnormaliz::ConeProperty::EhrhartQuasiPolynomial:
return NmzHilbertQuasiPolynomialToPyList< mpz_class >(
C->getEhrhartSeries());
case libnormaliz::ConeProperty::WeightedEhrhartQuasiPolynomial:
return NmzWeightedEhrhartQuasiPolynomialToPyList< mpz_class >(
C->getIntData());
case libnormaliz::ConeProperty::ClassGroup:
return NmzVectorToPyList(C->getClassGroup());
case libnormaliz::ConeProperty::FVector:
return NmzVectorToPyList(C->getFVector());
case libnormaliz::ConeProperty::FVectorOrbits:
return NmzVectorToPyList(C->getFVectorOrbits());
case libnormaliz::ConeProperty::DualFVector:
return NmzVectorToPyList(C->getDualFVector());
case libnormaliz::ConeProperty::DualFVectorOrbits:
return NmzVectorToPyList(C->getDualFVectorOrbits());
case libnormaliz::ConeProperty::FaceLattice:
return NmzFacelatticeToPython(C->getFaceLattice());
case libnormaliz::ConeProperty::FaceLatticeOrbits:
return NmzFacelatticeToPython(C->getFaceLatticeOrbits());
case libnormaliz::ConeProperty::DualFaceLattice:
return NmzFacelatticeToPython(C->getDualFaceLattice());
case libnormaliz::ConeProperty::DualFaceLatticeOrbits:
return NmzFacelatticeToPython(C->getDualFaceLatticeOrbits());
case libnormaliz::ConeProperty::Automorphisms:
return NmzAutomorphismsToPython(C->getAutomorphismGroup(
libnormaliz::ConeProperty::Automorphisms));
case libnormaliz::ConeProperty::AmbientAutomorphisms:
return NmzAutomorphismsToPython(C->getAutomorphismGroup(
libnormaliz::ConeProperty::AmbientAutomorphisms));
case libnormaliz::ConeProperty::InputAutomorphisms:
return NmzAutomorphismsToPython(C->getAutomorphismGroup(
libnormaliz::ConeProperty::InputAutomorphisms));
case libnormaliz::ConeProperty::CombinatorialAutomorphisms:
return NmzAutomorphismsToPython(C->getAutomorphismGroup(
libnormaliz::ConeProperty::CombinatorialAutomorphisms));
case libnormaliz::ConeProperty::RationalAutomorphisms:
return NmzAutomorphismsToPython(C->getAutomorphismGroup(
libnormaliz::ConeProperty::RationalAutomorphisms));
case libnormaliz::ConeProperty::EuclideanAutomorphisms:
return NmzAutomorphismsToPython(C->getAutomorphismGroup(
libnormaliz::ConeProperty::EuclideanAutomorphisms));
case libnormaliz::ConeProperty::FusionData:
return NmzFusionDataToPython(C->getFusionDataMatrix());
case libnormaliz::ConeProperty::InductionMatrices:
return NmzFusionDataToPython(C->getInductionMatrices());
case libnormaliz::ConeProperty::Incidence:
return NmzVectorToPyList(C->getIncidence());
case libnormaliz::ConeProperty::ModularGradings:
return NmzModularGradingsToPython(C->getModularGradings());
default: {
switch (outputtype) {
case libnormaliz::OutputType::Matrix:
return NmzMatrixToPyList(C->getMatrixConeProperty(p));
case libnormaliz::OutputType::MatrixFloat:
return NmzMatrixToPyList(
C->getFloatMatrixConeProperty(p));
case libnormaliz::OutputType::Vector:
return NmzVectorToPyList(C->getVectorConeProperty(p));
case libnormaliz::OutputType::Integer:
return NmzToPyNumber(C->getIntegerConeProperty(p));
case libnormaliz::OutputType::GMPInteger:
return NmzToPyNumber(C->getGMPIntegerConeProperty(p));
case libnormaliz::OutputType::Rational:
return NmzToPyNumber(C->getRationalConeProperty(p));
case libnormaliz::OutputType::FieldElem:
return NmzToPyNumber(C->getFieldElemConeProperty(p));
case libnormaliz::OutputType::Float:
return NmzToPyNumber(C->getFloatConeProperty(p));
case libnormaliz::OutputType::MachineInteger:
return NmzToPyNumber(C->getMachineIntegerConeProperty(p));
case libnormaliz::OutputType::Bool:
return BoolToPyBool(C->getBooleanConeProperty(p));
case libnormaliz::OutputType::Void: {
PyErr_SetString(PyNormaliz_cppError,
"ConeProperty is input-only");
return NULL;
}
case libnormaliz::OutputType::Complex: {
PyErr_SetString(PyNormaliz_cppError,
"This should never happen");
return NULL;
}
}
}
}
Py_RETURN_NONE;
}
static PyObject* _NmzResult(PyObject* self, PyObject* args, PyObject* kwargs)
{
FUNC_BEGIN
RationalHandler = NULL;
FloatHandler = NULL;
#ifdef ENFNORMALIZ
NumberfieldElementHandler = NULL;
#endif
VectorHandler = NULL;
MatrixHandler = NULL;
if(PyTuple_Size(args)!=2){
PyErr_SetString(PyNormaliz_cppError, "Exactly one computation goal required for NmzResult");
return NULL;
}
PyObject* cone = PyTuple_GetItem(args, 0);
PyObject* prop = PyTuple_GetItem(args, 1);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
if (!string_check(prop)) {
PyErr_SetString(PyNormaliz_cppError,
"Second argument must be a unicode string");
return NULL;
}
if (kwargs) {
RationalHandler = PyDict_GetItemString(kwargs, "RationalHandler");
FloatHandler = PyDict_GetItemString(kwargs, "FloatHandler");
#ifdef ENFNORMALIZ
NumberfieldElementHandler =
PyDict_GetItemString(kwargs, "NumberfieldElementHandler");
#endif
VectorHandler = PyDict_GetItemString(kwargs, "VectorHandler");
MatrixHandler = PyDict_GetItemString(kwargs, "MatrixHandler");
}
PyObject* result = NULL;
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
result = _NmzResultImpl(cone_ptr, prop);
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
result = _NmzResultImpl(cone_ptr, prop);
}
#ifdef ENFNORMALIZ
else if (is_cone_renf(cone)) {
Cone< renf_elem_class >* cone_ptr = get_cone_renf(cone);
result = _NmzResultImpl(
cone_ptr, prop,
reinterpret_cast< const void* >(get_cone_renf_renf(cone)));
}
#endif
RationalHandler = NULL;
#ifdef ENFNORMALIZ
NumberfieldElementHandler = NULL;
#endif
VectorHandler = NULL;
MatrixHandler = NULL;
return result;
FUNC_END
}
/***************************************************************************
*
* Python verbosity
*
***************************************************************************/
static PyObject* NmzSetVerboseDefault(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* value = PyTuple_GetItem(args, 0);
if (value != Py_True && value != Py_False) {
PyErr_SetString(PyNormaliz_cppError,
"Argument must be True or False");
return NULL;
}
return BoolToPyBool(libnormaliz::setVerboseDefault(value == Py_True));
FUNC_END
}
template < typename Integer >
static PyObject* NmzSetVerbose(Cone< Integer >* C, PyObject* value)
{
FUNC_BEGIN
return BoolToPyBool(C->setVerbose(value == Py_True));
FUNC_END
}
static PyObject* NmzSetVerbose_Outer(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
PyObject* value = PyTuple_GetItem(args, 1);
if (value != Py_True && value != Py_False) {
PyErr_SetString(PyNormaliz_cppError,
"Second argument must be True or False");
return NULL;
}
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
return NmzSetVerbose(cone_ptr, value);
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
return NmzSetVerbose(cone_ptr, value);
}
#ifdef ENFNORMALIZ
else if (is_cone_renf(cone)) {
Cone< renf_elem_class >* cone_ptr = get_cone_renf(cone);
return NmzSetVerbose(cone_ptr, value);
}
#endif
Py_RETURN_NONE;
FUNC_END
}
/***************************************************************************
*
* Get Polynomial
*
***************************************************************************/
static PyObject* NmzGetPolynomial(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
TempSignalHandler tmpHandler; // use custom signal handler
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
return StringToPyUnicode((cone_ptr->getIntData()).getPolynomial());
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
return StringToPyUnicode((cone_ptr->getIntData()).getPolynomial());
}
else {
PyErr_SetString(PyNormaliz_cppError,
"Polynomial not available for renf cone");
return NULL;
}
FUNC_END
}
/***************************************************************************
*
* NrCoeffQuasiPol
*
***************************************************************************/
static PyObject* NmzSetNrCoeffQuasiPol(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
PyObject* bound_py = PyTuple_GetItem(args, 1);
TempSignalHandler tmpHandler; // use custom signal handler
int overflow;
long bound = PyLong_AsLongLongAndOverflow(bound_py, &overflow);
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
cone_ptr->setNrCoeffQuasiPol(bound);
Py_RETURN_TRUE;
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
cone_ptr->setNrCoeffQuasiPol(bound);
Py_RETURN_TRUE;
}
else {
PyErr_SetString(PyNormaliz_cppError,
"Cannot set quasi polynomial coeffs for renf cone");
return NULL;
}
FUNC_END
}
/***************************************************************************
*
* Polynomial
*
***************************************************************************/
static PyObject* NmzSetPolynomial(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
TempSignalHandler tmpHandler; // use custom signal handler
PyObject* poly_pi = PyTuple_GetItem(args, 1);
if(!string_check(poly_pi)){
PyErr_SetString(PyNormaliz_cppError, "Polynomial must be given as a string");
return NULL;
}
TempSignalHandler tmpHandler1; // use custom signal handler
string polynomial = PyUnicodeToString(poly_pi);
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
cone_ptr->setPolynomial(polynomial);
Py_RETURN_TRUE;
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
cone_ptr->setPolynomial(polynomial);
Py_RETURN_TRUE;
}
else {
PyErr_SetString(PyNormaliz_cppError,
"Polynomial cannot be set for renf cone");
return NULL;
}
FUNC_END
}
static PyObject* NmzSetPolynomialEquations(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
TempSignalHandler tmpHandler; // use custom signal handler
PyObject* polys_py = PyTuple_GetItem(args, 1);
if(!PyList_CheckExact(polys_py)) {
PyErr_SetString(PyNormaliz_cppError, "Second argument must be a list");
return NULL;
}
TempSignalHandler tmpHandler1; // use custom signal handler
size_t nr_polys = PySequence_Size(polys_py);
vector<string> PolyEquations;
for(size_t i = 0; i < nr_polys; ++i){
if(!string_check(PyList_GetItem(polys_py,i))) {
PyErr_SetString(PyNormaliz_cppError, "Polynomial must be given as a string");
return NULL;
}
string equ = PyUnicodeToString( PyList_GetItem(polys_py,i));
PolyEquations.push_back(equ);
}
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
cone_ptr->setPolynomialEquations(PolyEquations);
Py_RETURN_TRUE;
}
if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
cone_ptr->setPolynomialEquations(PolyEquations);
Py_RETURN_TRUE;
}
#ifdef ENFNORMALIZ
Cone<renf_elem_class>* cone_ptr = get_cone_renf(cone);
cone_ptr->setPolynomialEquations(PolyEquations);
Py_RETURN_TRUE;
#endif
FUNC_END
}
static PyObject* NmzSetPolynomialInequalities(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
TempSignalHandler tmpHandler; // use custom signal handler
PyObject* polys_py = PyTuple_GetItem(args, 1);
if(!PyList_CheckExact(polys_py)) {
PyErr_SetString(PyNormaliz_cppError, "Second argument must be a list");
return NULL;
}
TempSignalHandler tmpHandler1; // use custom signal handler
size_t nr_polys = PySequence_Size(polys_py);
vector<string> PolyInequalities;
for(size_t i = 0; i < nr_polys; ++i){
if(!string_check(PyList_GetItem(polys_py,i))) {
PyErr_SetString(PyNormaliz_cppError, "Polynomial must be given as a string");
return NULL;
}
string inequ = PyUnicodeToString( PyList_GetItem(polys_py,i));
PolyInequalities.push_back(inequ);
}
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
cone_ptr->setPolynomialInequalities(PolyInequalities);
Py_RETURN_TRUE;
}
if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
cone_ptr->setPolynomialInequalities(PolyInequalities);
Py_RETURN_TRUE;
}
#ifdef ENFNORMALIZ
Cone<renf_elem_class>* cone_ptr = get_cone_renf(cone);
cone_ptr->setPolynomialInequalities(PolyInequalities);
Py_RETURN_TRUE;
#endif
FUNC_END
}
/***************************************************************************
*
* FaceCodimBound and other numerical parameters
*
***************************************************************************/
static PyObject* NmzSetFaceCodimBound(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
PyObject* bound_py = PyTuple_GetItem(args, 1);
TempSignalHandler tmpHandler; // use custom signal handler
int overflow;
long bound = PyLong_AsLongLongAndOverflow(bound_py, &overflow);
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
cone_ptr->setFaceCodimBound(bound);
Py_RETURN_TRUE;
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
cone_ptr->setFaceCodimBound(bound);
Py_RETURN_TRUE;
}
#ifdef ENFNORMALIZ
else {
Cone<renf_elem_class>* cone_ptr = get_cone_renf(cone);
cone_ptr->setFaceCodimBound(bound);
Py_RETURN_TRUE;
}
#endif
FUNC_END
}
static PyObject* NmzSetModularGrading(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
PyObject* mod_gr_py = PyTuple_GetItem(args, 1);
TempSignalHandler tmpHandler; // use custom signal handler
int overflow;
long mod_gr = PyLong_AsLongLongAndOverflow(mod_gr_py, &overflow);
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
cone_ptr->setModularGraing(mod_gr);
Py_RETURN_TRUE;
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
cone_ptr->setModularGraing(mod_gr);
Py_RETURN_TRUE;
}
#ifdef ENFNORMALIZ
else {
Cone<renf_elem_class>* cone_ptr = get_cone_renf(cone);
cone_ptr->setModularGraing(mod_gr);
Py_RETURN_TRUE;
}
#endif
FUNC_END
}
static PyObject* NmzSetChosenFusionRing(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
PyObject* chosen_ring_py = PyTuple_GetItem(args, 1);
TempSignalHandler tmpHandler; // use custom signal handler
int overflow;
long chosen_ring = PyLong_AsLongLongAndOverflow(chosen_ring_py, &overflow);
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
cone_ptr->setChosenFusionRing(chosen_ring);
Py_RETURN_TRUE;
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
cone_ptr->setChosenFusionRing(chosen_ring);
Py_RETURN_TRUE;
}
#ifdef ENFNORMALIZ
else {
Cone<renf_elem_class>* cone_ptr = get_cone_renf(cone);
cone_ptr->setChosenFusionRing(chosen_ring);
Py_RETURN_TRUE;
}
#endif
FUNC_END
}
static PyObject* NmzSetGBDegreeBound(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
PyObject* bound_py = PyTuple_GetItem(args, 1);
TempSignalHandler tmpHandler; // use custom signal handler
int overflow;
long bound = PyLong_AsLongLongAndOverflow(bound_py, &overflow);
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
cone_ptr->setGBDegreeBound(bound);
Py_RETURN_TRUE;
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
cone_ptr->setGBDegreeBound(bound);
Py_RETURN_TRUE;
}
#ifdef ENFNORMALIZ
else {
PyErr_SetString(PyNormaliz_cppError, "GB degree bound not defined for algebraic polyhedra");
return NULL;
}
#endif
FUNC_END
}
static PyObject* NmzSetGBMinDegree(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
PyObject* bound_py = PyTuple_GetItem(args, 1);
TempSignalHandler tmpHandler; // use custom signal handler
int overflow;
long bound = PyLong_AsLongLongAndOverflow(bound_py, &overflow);
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
cone_ptr->setGBMinDegree(bound);
Py_RETURN_TRUE;
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
cone_ptr->setGBMinDegree(bound);
Py_RETURN_TRUE;
}
#ifdef ENFNORMALIZ
else {
PyErr_SetString(PyNormaliz_cppError, "GB min degree not defined for algebraic polyhedra");
return NULL;
}
#endif
FUNC_END
}
static PyObject* NmzSetDecimalDigits(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
PyObject* digits_py = PyTuple_GetItem(args, 1);
TempSignalHandler tmpHandler; // use custom signal handler
int overflow;
long digits = PyLong_AsLongLongAndOverflow(digits_py, &overflow);
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
cone_ptr->setDecimalDigits(digits);
Py_RETURN_TRUE;
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
cone_ptr->setDecimalDigits(digits);
Py_RETURN_TRUE;
}
#ifdef ENFNORMALIZ
else {
Cone<renf_elem_class>* cone_ptr = get_cone_renf(cone);
cone_ptr->setDecimalDigits(digits);
Py_RETURN_TRUE;
}
#endif
FUNC_END
}
/***************************************************************************
*
* Get Symmetrized cone
*
***************************************************************************/
static PyObject* NmzSymmetrizedCone(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
TempSignalHandler tmpHandler; // use custom signal handler
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
Cone< mpz_class >& symm_cone = cone_ptr->getSymmetrizedCone();
return pack_cone(new Cone< mpz_class >(symm_cone));
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
Cone< long long >& symm_cone = cone_ptr->getSymmetrizedCone();
return pack_cone(new Cone< long long >(symm_cone));
}
else {
PyErr_SetString(PyNormaliz_cppError,
"Symmetrized cone not available for renf cone");
return NULL;
}
FUNC_END
}
/***************************************************************************
*
* Get expanded Hilbert series
*
***************************************************************************/
static PyObject* NmzGetHilbertSeriesExpansion(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
PyObject* py_degree = PyTuple_GetItem(args, 1);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
if (!PyLong_Check(py_degree)) {
PyErr_SetString(PyNormaliz_cppError,
"Second argument must be a long");
return NULL;
}
long degree = PyLong_AsLong(py_degree);
libnormaliz::HilbertSeries HS;
TempSignalHandler tmpHandler; // use custom signal handler
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
HS = cone_ptr->getHilbertSeries();
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
HS = cone_ptr->getHilbertSeries();
}
else {
PyErr_SetString(
PyNormaliz_cppError,
"Hilbert series expansion not available for renf cone");
return NULL;
}
HS.set_expansion_degree(degree);
return NmzVectorToPyList(HS.getExpansion());
FUNC_END
}
static PyObject* NmzGetWeightedEhrhartSeriesExpansion(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
PyObject* py_degree = PyTuple_GetItem(args, 1);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
if (!PyLong_Check(py_degree)) {
PyErr_SetString(PyNormaliz_cppError,
"Second argument must be a long");
return NULL;
}
long degree = PyLong_AsLong(py_degree);
pair< libnormaliz::HilbertSeries, mpz_class > ES;
TempSignalHandler tmpHandler; // use custom signal handler
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
ES = cone_ptr->getWeightedEhrhartSeries();
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
ES = cone_ptr->getWeightedEhrhartSeries();
}
else {
PyErr_SetString(
PyNormaliz_cppError,
"Ehrhart series expansion not available for renf cone");
return NULL;
}
ES.first.set_expansion_degree(degree);
PyObject* return_list = PyList_New(2);
PyList_SetItem(return_list, 0, NmzVectorToPyList(ES.first.getExpansion()));
PyList_SetItem(return_list, 1, NmzToPyNumber(ES.second));
return return_list;
FUNC_END
}
static PyObject* NmzGetEhrhartSeriesExpansion(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* cone = PyTuple_GetItem(args, 0);
PyObject* py_degree = PyTuple_GetItem(args, 1);
if (!is_cone(cone)) {
PyErr_SetString(PyNormaliz_cppError, "First argument must be a cone");
return NULL;
}
if (!PyLong_Check(py_degree)) {
PyErr_SetString(PyNormaliz_cppError,
"Second argument must be a long");
return NULL;
}
long degree = PyLong_AsLong(py_degree);
libnormaliz::HilbertSeries ES;
TempSignalHandler tmpHandler; // use custom signal handler
if (is_cone_mpz(cone)) {
Cone< mpz_class >* cone_ptr = get_cone_mpz(cone);
ES = cone_ptr->getEhrhartSeries();
}
else if (is_cone_long(cone)) {
Cone< long long >* cone_ptr = get_cone_long(cone);
ES = cone_ptr->getEhrhartSeries();
}
else {
PyErr_SetString(
PyNormaliz_cppError,
"Ehrhart series expansion not available for renf cone");
return NULL;
}
ES.set_expansion_degree(degree);
return NmzVectorToPyList(ES.getExpansion());
FUNC_END
}
/***************************************************************************
*
* Set number of threads
*
***************************************************************************/
static PyObject* NmzSetNumberOfNormalizThreads(PyObject* self, PyObject* args)
{
FUNC_BEGIN
PyObject* num_threads = PyTuple_GetItem(args, 0);
long num_threads_long;
if (PyLong_Check(num_threads)) {
num_threads_long = PyLong_AsLong(num_threads);
}
#if PY_MAJOR_VERSION < 3
else if (PyInt_Check(num_threads)) {
num_threads_long = PyInt_AsLong(num_threads);
}
#endif
else {
throw PyNormalizInputException("argument must be an integer");
return NULL;
}
num_threads_long = libnormaliz::set_thread_limit(num_threads_long);
return PyLong_FromLong(num_threads_long);
FUNC_END
}
/***************************************************************************
*
* Check for various features
*
***************************************************************************/
static PyObject* NmzHasEAntic(PyObject* self)
{
#ifdef ENFNORMALIZ
Py_RETURN_TRUE;
#else
Py_RETURN_FALSE;
#endif
}
static PyObject* NmzHasNauty(PyObject* self)
{
#ifdef NMZ_NAUTY
Py_RETURN_TRUE;
#else
Py_RETURN_FALSE;
#endif
}
static PyObject* NmzHasFlint(PyObject* self)
{
#ifdef NMZ_FLINT
Py_RETURN_TRUE;
#else
Py_RETURN_FALSE;
#endif
}
static PyObject* NmzHasCocoa(PyObject* self)
{
#ifdef NMZ_COCOA
Py_RETURN_TRUE;
#else
Py_RETURN_FALSE;
#endif
}
/***************************************************************************
*
* Write output file
*
***************************************************************************/
static PyObject* NmzWriteOutputFile(PyObject* self, PyObject* args)
{
FUNC_BEGIN
if ((!PyTuple_Check(args)) || (PyTuple_Size(args) != 2)) {
throw PyNormalizInputException(
"The arguments must be a cone and a string");
return NULL;
}
PyObject* cone_py = PyTuple_GetItem(args, 0);
PyObject* filename_py = PyTuple_GetItem(args, 1);
string filename = PyUnicodeToString(filename_py);
if (is_cone_mpz(cone_py)) {
Cone< mpz_class >* cone = get_cone_mpz(cone_py);
cone->write_cone_output(filename);
Py_RETURN_TRUE;
}
if (is_cone_long(cone_py)) {
Cone< long long >* cone = get_cone_long(cone_py);
cone->write_cone_output(filename);
Py_RETURN_TRUE;
}
#ifdef ENFNORMALIZ
if (is_cone_renf(cone_py)) {
Cone< renf_elem_class >* cone = get_cone_renf(cone_py);
cone->write_cone_output(filename);
Py_RETURN_TRUE;
}
#endif
Py_RETURN_FALSE;
FUNC_END
}
/***************************************************************************
*
* Write file with precomputed data
*
***************************************************************************/
static PyObject* NmzWritePrecompData(PyObject* self, PyObject* args)
{
FUNC_BEGIN
if ((!PyTuple_Check(args)) || (PyTuple_Size(args) != 2)) {
throw PyNormalizInputException(
"The arguments must be a cone and a string");
return NULL;
}
PyObject* cone_py = PyTuple_GetItem(args, 0);
PyObject* filename_py = PyTuple_GetItem(args, 1);
string filename = PyUnicodeToString(filename_py);
if (is_cone_mpz(cone_py)) {
Cone< mpz_class >* cone = get_cone_mpz(cone_py);
cone->write_precomp_for_input(filename);
Py_RETURN_TRUE;
}
if (is_cone_long(cone_py)) {
Cone< long long >* cone = get_cone_long(cone_py);
cone->write_precomp_for_input(filename);
Py_RETURN_TRUE;
}
#ifdef ENFNORMALIZ
if (is_cone_renf(cone_py)) {
Cone< renf_elem_class >* cone = get_cone_renf(cone_py);
cone->write_precomp_for_input(filename);
Py_RETURN_TRUE;
}
#endif
Py_RETURN_FALSE;
FUNC_END
}
/***************************************************************************
*
* Get renf minpoly and precision
*
***************************************************************************/
static PyObject* NmzGetRenfInfo(PyObject* self, PyObject* args)
{
FUNC_BEGIN
#ifdef ENFNORMALIZ
if( (!PyTuple_Check(args)) || (PyTuple_Size(args) != 1) ){
throw PyNormalizInputException(
"Only one argument allowed"
);
return NULL;
}
PyObject* cone_py = PyTuple_GetItem(args, 0);
if(!is_cone_renf(cone_py)){
throw PyNormalizInputException(
"Only Renf cones allowed"
);
return NULL;
}
const renf_class* renf = get_cone_renf_renf(cone_py);
std::string minpoly_str;
minpoly_str = fmpq_poly_get_str_pretty(renf->get_renf()->nf->pol, renf->gen_name().c_str());
std::string res1 = arb_get_str(renf->get_renf()->emb, 64, 0);
// long prec = renf->get_renf()->prec;
return PyTuple_Pack(2, StringToPyUnicode(minpoly_str), StringToPyUnicode(res1));
#else
return NULL;
#endif
FUNC_END
}
/***************************************************************************
*
* Get name of field generator
*
***************************************************************************/
static PyObject* NmzFieldGenName(PyObject* self, PyObject* args)
{
FUNC_BEGIN
if( (!PyTuple_Check(args)) || (PyTuple_Size(args) != 1) ){
throw PyNormalizInputException(
"Only one argument allowed"
);
return NULL;
}
PyObject* cone_py = PyTuple_GetItem(args, 0);
std::string gen_name_string = "";
if (is_cone_mpz(cone_py)) {
return PyUnicode_FromString(gen_name_string.c_str());
}
if (is_cone_long(cone_py)) {
return PyUnicode_FromString(gen_name_string.c_str());
}
#ifdef ENFNORMALIZ
if (is_cone_renf(cone_py)) {
Cone< renf_elem_class >* cone_ptr = get_cone_renf(cone_py);
gen_name_string = cone_ptr->getRenfGenerator();
return PyUnicode_FromString(gen_name_string.c_str());
}
#endif
return NULL; // to make gcc happy
FUNC_END
}
/***************************************************************************
*
* Python init stuff
*
***************************************************************************/
struct module_state {
PyObject* error;
};
#if PY_MAJOR_VERSION >= 3
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
#else
#define GETSTATE(m) (&_state)
static struct module_state _state;
#endif
static PyObject* error_out(PyObject* m)
{
struct module_state* st = GETSTATE(m);
PyErr_SetString(st->error, "something bad happened");
return NULL;
}
static PyMethodDef PyNormaliz_cppMethods[] = {
{"error_out", (PyCFunction)error_out, METH_NOARGS, NULL},
{"NmzCone", (PyCFunction)_NmzCone, METH_VARARGS | METH_KEYWORDS,
"Create a cone"},
{"NmzConeCopy", (PyCFunction)_NmzConeCopy, METH_VARARGS,
"Copy an existing cone"},
{"NmzCompute", (PyCFunction)_NmzCompute_Outer, METH_VARARGS,
"Compute some stuff"},
{"NmzIsComputed", (PyCFunction)NmzIsComputed_Outer, METH_VARARGS,
"Check if property is computed "},
{"NmzSetGrading", (PyCFunction)NmzSetGrading, METH_VARARGS,
"Reset the grading of a cone"},
{"NmzSetBoolParam", (PyCFunction)NmzSetBoolParam, METH_VARARGS,
"Swts boolean parameters of a cone"},
{"NmzSetProjectionCoords", (PyCFunction)NmzSetProjectionCoords, METH_VARARGS,
"Reset the projection coordinates"},
{"NmzResult", (PyCFunction)_NmzResult, METH_VARARGS | METH_KEYWORDS,
"Return cone property"},
{"NmzSetVerboseDefault", (PyCFunction)NmzSetVerboseDefault, METH_VARARGS,
"Set verbosity"},
{"NmzSetVerbose", (PyCFunction)NmzSetVerbose_Outer, METH_VARARGS,
"Set verbosity of cone"},
{"NmzListConeProperties", (PyCFunction)NmzListConeProperties, METH_NOARGS,
"List all available properties"},
// {"NmzHilbertSeries", (PyCFunction)NmzHilbertSeries_Outer, METH_VARARGS,
// "Returns Hilbert series, either HSOP or not"},
{"NmzGetPolynomial", (PyCFunction)NmzGetPolynomial, METH_VARARGS,
"Returns grading polynomial"},
{"NmzSymmetrizedCone", (PyCFunction)NmzSymmetrizedCone, METH_VARARGS,
"Returns symmetrized cone"},
{"NmzSetNumberOfNormalizThreads",
(PyCFunction)NmzSetNumberOfNormalizThreads, METH_VARARGS,
"Sets the Normaliz thread limit"},
{"NmzSetNrCoeffQuasiPol", (PyCFunction)NmzSetNrCoeffQuasiPol,
METH_VARARGS, "Sets the number of computed coefficients for the quasi-polynomial"},
{"NmzSetDecimalDigits", (PyCFunction)NmzSetDecimalDigits,
METH_VARARGS, "Sets the number of decimal digits for fixed precision"},
{"NmzSetPolynomial", (PyCFunction)NmzSetPolynomial,
METH_VARARGS, "Sets the polynomial for integration and weighted series"},
{"NmzSetPolynomialEquations", (PyCFunction)NmzSetPolynomialEquations,
METH_VARARGS, "Sets the polynomial equations for lattice points"},
{"NmzSetPolynomialInequalities", (PyCFunction)NmzSetPolynomialInequalities,
METH_VARARGS, "Sets the polynomial inequalities for lattice points"},
{"NmzSetFaceCodimBound", (PyCFunction)NmzSetFaceCodimBound,
METH_VARARGS, "Sets the maximal codimension for the computed faces"},
{"NmzSetModularGrading", (PyCFunction)NmzSetModularGrading,
METH_VARARGS, "Sets the maximal codimension for the computed faces"},
{"NmzSetChosenFusionRing", (PyCFunction)NmzSetChosenFusionRing,
METH_VARARGS, "Picks a fusion ring (counting from 1)"},
{"NmzSetGBDegreeBound", (PyCFunction)NmzSetGBDegreeBound,
METH_VARARGS, "Sets the maximal degree for binomials in Gröbner and Markov bases"},
{"NmzSetGBMinDegree", (PyCFunction)NmzSetGBMinDegree,
METH_VARARGS, "Sets the minimal degree for binomials in Gröbner and Markov bases"},
{"NmzGetHilbertSeriesExpansion",
(PyCFunction)NmzGetHilbertSeriesExpansion, METH_VARARGS,
"Returns expansion of the hilbert series"},
{"NmzGetEhrhartSeriesExpansion",
(PyCFunction)NmzGetEhrhartSeriesExpansion, METH_VARARGS,
"Returns expansion of the Ehrhart series"},
{"NmzGetWeightedEhrhartSeriesExpansion",
(PyCFunction)NmzGetWeightedEhrhartSeriesExpansion, METH_VARARGS,
"Returns expansion of the weighted Ehrhart series"},
{"NmzHasEAntic", (PyCFunction)NmzHasEAntic, METH_NOARGS,
"Returns true if (Py)Normaliz was compiled with e-antic support"},
{"NmzHasNauty", (PyCFunction)NmzHasNauty, METH_NOARGS,
"Returns true if (Py)Normaliz was compiled with nauty support"},
{"NmzHasFlint", (PyCFunction)NmzHasFlint, METH_NOARGS,
"Returns true if (Py)Normaliz was compiled with Flint support"},
{"NmzHasCocoa", (PyCFunction)NmzHasCocoa, METH_NOARGS,
"Returns true if (Py)Normaliz was compiled with CoCoA support"},
{"NmzWriteOutputFile", (PyCFunction)NmzWriteOutputFile, METH_VARARGS,
"Prints the Normaliz cone output into a file"},
{"NmzWritePrecompData", (PyCFunction)NmzWritePrecompData, METH_VARARGS,
"Prints the Normaliz cone precomputed data for further input"},
{"NmzGetRenfInfo", (PyCFunction)NmzGetRenfInfo, METH_VARARGS,
"Outputs info of the number field associated to a renf cone"},
{"NmzFieldGenName", (PyCFunction)NmzFieldGenName, METH_VARARGS,
"Returns name of field generator"},
{"NmzModifyCone", (PyCFunction)_NmzModify_Outer, METH_VARARGS,
"Modifies a given input property of a cone using a new matrix"},
{
NULL,
} /* Sentinel */
};
#if PY_MAJOR_VERSION >= 3
static int PyNormaliz_cpp_traverse(PyObject* m, visitproc visit, void* arg)
{
Py_VISIT(GETSTATE(m)->error);
return 0;
}
static int PyNormaliz_cpp_clear(PyObject* m)
{
Py_CLEAR(GETSTATE(m)->error);
return 0;
}
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT, "PyNormaliz_cpp", NULL,
sizeof(struct module_state), PyNormaliz_cppMethods, NULL,
PyNormaliz_cpp_traverse, PyNormaliz_cpp_clear, NULL};
#define INITERROR return NULL
PyMODINIT_FUNC PyInit_PyNormaliz_cpp(void)
#else
#define INITERROR return
extern "C" void initPyNormaliz_cpp(void)
#endif
{
#if PY_MAJOR_VERSION >= 3
PyObject* module = PyModule_Create(&moduledef);
#else
PyObject* module = Py_InitModule("PyNormaliz_cpp", PyNormaliz_cppMethods);
#endif
if (module == NULL)
INITERROR;
struct module_state* st = GETSTATE(module);
st->error = PyErr_NewException(
const_cast< char* >("PyNormaliz_cpp.INITError"), NULL, NULL);
if (st->error == NULL) {
Py_DECREF(module);
INITERROR;
}
NormalizError = PyErr_NewException(
const_cast< char* >("PyNormaliz_cpp.NormalizError"), NULL, NULL);
Py_INCREF(NormalizError);
PyNormaliz_cppError = PyErr_NewException(
const_cast< char* >("PyNormaliz_cpp.NormalizInterfaceError"), NULL,
NULL);
Py_INCREF(PyNormaliz_cppError);
PyModule_AddObject(module, "normaliz_error", NormalizError);
PyModule_AddObject(module, "pynormaliz_error", PyNormaliz_cppError);
#if PY_MAJOR_VERSION >= 3
return module;
#endif
}
|