1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721
|
/*
This file is part of PolyLib.
PolyLib is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PolyLib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PolyLib. If not, see <http://www.gnu.org/licenses/>.
*/
/* polyhedron.c
COPYRIGHT
Both this software and its documentation are
Copyright 1993 by IRISA /Universite de Rennes I - France,
Copyright 1995,1996 by BYU, Provo, Utah
all rights reserved.
Permission is granted to copy, use, and distribute
for any commercial or noncommercial purpose under the terms
of the GNU General Public license, version 2, June 1991
(see file : LICENSING).
*/
/*
1997/12/02 - Olivier Albiez
Ce fichier contient les fonctions de la polylib de l'IRISA,
passees en 64bits.
La structure de la polylib a donc ete modifie pour permettre
le passage aux Value. La fonction Chernikova a ete reecrite.
*/
/*
1998/26/02 - Vincent Loechner
Ajout de nombreuses fonctions, a la fin de ce fichier,
pour les polyedres parametres 64 bits.
1998/16/03
#define DEBUG printf
tests out of memory
compatibilite avec la version de doran
*/
#undef POLY_DEBUG /* debug printf: general functions */
#undef POLY_RR_DEBUG /* debug printf: Remove Redundants */
#undef POLY_CH_DEBUG /* debug printf: Chernikova */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <polylib/polylib.h>
#ifdef MAC_OS
#define abs __abs
#endif
/* WSIZE is the number of bits in a word or int type */
#define WSIZE (8*sizeof(int))
#define bexchange(a, b, l)\
{\
char *t = (char *)malloc(l*sizeof(char));\
memcpy((t), (char *)(a), (int)(l));\
memcpy((char *)(a), (char *)(b), (int)(l));\
memcpy((char *)(b), (t), (int)(l));\
free(t); \
}
#define exchange(a, b, t)\
{ (t)=(a); (a)=(b); (b)=(t); }
/* errormsg1 is an external function which is usually supplied by the
calling program (e.g. Domlib.c, ReadAlpha, etc...).
See errormsg.c for an example of such a function. */
void errormsg1(const char *f, const char *msgname, const char *msg);
int Pol_status; /* error status after operations */
/*
* The Saturation matrix is defined to be an integer (int type) matrix.
* It is a boolean matrix which has a row for every constraint and a column
* for every line or ray. The bits in the binary format of each integer in
* the stauration matrix stores the information whether the corresponding
* constraint is saturated by ray(line) or not.
*/
typedef struct {
unsigned int NbRows;
unsigned int NbColumns;
int **p;
int *p_init;
} SatMatrix;
/*
* Allocate memory space for a saturation matrix.
*/
static SatMatrix *SMAlloc(int rows,int cols) {
int **q, *p, i;
SatMatrix *result;
result = (SatMatrix *) malloc (sizeof(SatMatrix));
if(!result) {
errormsg1("SMAlloc", "outofmem", "out of memory space");
return 0;
}
result->NbRows = rows;
result->NbColumns = cols;
if(rows == 0 || cols == 0) {
result->p = NULL;
return result;
}
result->p = q = (int **)malloc(rows * sizeof(int *));
if(!result->p) {
errormsg1("SMAlloc", "outofmem", "out of memory space");
return 0;
}
result->p_init = p = (int *)malloc (rows * cols * sizeof (int));
if(!result->p_init) {
errormsg1("SMAlloc", "outofmem", "out of memory space");
return 0;
}
for (i=0; i<rows; i++) {
*q++ = p;
p += cols;
}
return result;
} /* SMAlloc */
/*
* Free the memory space occupied by saturation matrix.
*/
static void SMFree (SatMatrix **matrix) {
SatMatrix *SM = *matrix;
if (SM) {
if (SM->p) {
free ((char *) SM->p_init);
free ((char *) SM->p);
}
free ((char *) SM);
*matrix = NULL;
}
} /* SMFree */
/*
* Print the contents of a saturation matrix.
* This function is defined only for debugging purpose.
*/
static void SMPrint (SatMatrix *matrix) {
int *p;
int i, j;
unsigned NbRows, NbColumns;
fprintf(stderr,"%d %d\n",NbRows=matrix->NbRows, NbColumns=matrix->NbColumns);
for (i=0;i<NbRows;i++) {
p = *(matrix->p+i);
for (j=0;j<NbColumns;j++)
fprintf(stderr, " %10X ", *p++);
fprintf(stderr, "\n");
}
} /* SMPrint */
/*
* Compute the bitwise OR of two saturation matrices.
*/
static void SatVector_OR(int *p1,int *p2,int *p3,unsigned length) {
int *cp1, *cp2, *cp3;
int i;
cp1=p1;
cp2=p2;
cp3=p3;
for (i=0;i<length;i++) {
*cp3 = *cp1 | *cp2;
cp3++;
cp1++;
cp2++;
}
} /* SatVector_OR */
/*
* Copy a saturation matrix to another (macro definition).
*/
#define SMVector_Copy(p1, p2, length) \
memcpy((char *)(p2), (char *)(p1), (int)((length)*sizeof(int)))
/*
* Initialize a saturation matrix with zeros (macro definition)
*/
#define SMVector_Init(p1, length) \
memset((char *)(p1), 0, (int)((length)*sizeof(int)))
/*
* Defining operations on polyhedron --
*/
/*
* Vector p3 is a linear combination of two vectors (p1 and p2) such that
* p3[pos] is zero. First element of each vector (p1,p2,p3) is a status
* element and is not changed in p3. The value of 'pos' may be 0 however.
* The parameter 'length' does not include status element one.
*/
static void Combine(Value *p1, Value *p2, Value *p3, int pos, unsigned length) {
Value a1, a2, gcd;
Value abs_a1,abs_a2,neg_a1;
/* Initialize all the 'Value' variables */
value_init(a1); value_init(a2); value_init(gcd);
value_init(abs_a1); value_init(abs_a2); value_init(neg_a1);
/* a1 = p1[pos] */
value_assign(a1,p1[pos]);
/* a2 = p2[pos] */
value_assign(a2,p2[pos]);
/* a1_abs = |a1| */
value_absolute(abs_a1,a1);
/* a2_abs = |a2| */
value_absolute(abs_a2,a2);
/* gcd = Gcd(abs(a1), abs(a2)) */
value_gcd(gcd, abs_a1, abs_a2);
/* a1 = a1/gcd */
value_divexact(a1, a1, gcd);
/* a2 = a2/gcd */
value_divexact(a2, a2, gcd);
/* neg_a1 = -(a1) */
value_oppose(neg_a1,a1);
Vector_Combine(p1+1,p2+1,p3+1,a2,neg_a1,length);
Vector_Normalize(p3+1,length);
/* Clear all the 'Value' variables */
value_clear(a1); value_clear(a2); value_clear(gcd);
value_clear(abs_a1); value_clear(abs_a2); value_clear(neg_a1);
return;
} /* Combine */
/*
* Return the transpose of the saturation matrix 'Sat'. 'Mat' is a matrix
* of constraints and 'Ray' is a matrix of ray vectors and 'Sat' is the
* corresponding saturation matrix.
*/
static SatMatrix *TransformSat(Matrix *Mat, Matrix *Ray, SatMatrix *Sat) {
int i, j, sat_nbcolumns;
unsigned jx1, jx2, bx1, bx2;
SatMatrix *result;
if (Mat->NbRows != 0)
sat_nbcolumns = (Mat->NbRows-1) /(sizeof(int)*8) + 1;
else
sat_nbcolumns = 0;
result = SMAlloc(Ray->NbRows, sat_nbcolumns);
SMVector_Init(result->p_init, Ray->NbRows * sat_nbcolumns);
for(i=0,jx1=0,bx1=MSB; i<Ray->NbRows; i++) {
for(j=0,jx2=0,bx2=MSB; j<Mat->NbRows; j++) {
if (Sat->p[j][jx1] & bx1)
result->p[i][jx2] |= bx2;
NEXT(jx2,bx2);
}
NEXT(jx1, bx1);
}
return result;
} /* TransformSat */
/*
* Sort the rays (Ray, Sat) into three tiers as used in 'Chernikova' function:
* NbBid <= i < equal_bound : saturates the constraint
* equal_bound <= i < sup_bound : verifies the constraint
* sup_bound <= i < NbRay : does not verify
*
* 'Ray' is the matrix of rays and 'Sat' is the corresponding saturation
* matrix. (jx,bx) pair specify the constraint in the saturation matrix. The
* status element of the 'Ray' matrix holds the saturation value w.r.t the
* constraint specified by (jx,bx). Thus
* Ray->p[i][0] = 0 -> ray(i) saturates the constraint
* Ray->p[i][0] > 0 -> ray(i) verifies the constraint
* Ray->p[i][0] < 0 -> ray(i) doesn't verify the constraint
*/
static void RaySort(Matrix *Ray,SatMatrix *Sat,int NbBid,int NbRay,int *equal_bound,int *sup_bound,unsigned RowSize1, unsigned RowSize2, unsigned bx, unsigned jx) {
int inf_bound;
Value **uni_eq, **uni_sup, **uni_inf;
int **inc_eq, **inc_sup, **inc_inf;
/* 'uni_eq' points to the first ray in the ray matrix which verifies a
* constraint, 'inc_eq' is the corresponding pointer in saturation
* matrix. 'uni_inf' points to the first ray (from top) which doesn't
* verify a constraint, 'inc_inf' is the corresponding pointer in
* saturation matrix. 'uni_sup' scans the ray matrix and 'inc_sup' is
* the corresponding pointer in saturation matrix. 'inf_bound' holds the
* number of the first ray which does not verify the constraints.
*/
*sup_bound = *equal_bound = NbBid;
uni_sup = uni_eq = Ray->p+NbBid;
inc_sup = inc_eq = Sat->p+NbBid;
inf_bound = NbRay;
uni_inf = Ray->p+NbRay;
inc_inf = Sat->p+NbRay;
while (inf_bound>*sup_bound) {
if (value_zero_p(**uni_sup)) { /* status = satisfy */
if (inc_eq != inc_sup) {
Vector_Exchange(*uni_eq,*uni_sup,RowSize1);
bexchange(*inc_eq,*inc_sup,RowSize2);
}
(*equal_bound)++; uni_eq++; inc_eq++;
(*sup_bound)++; uni_sup++; inc_sup++;
}
else {
*((*inc_sup)+jx)|=bx;
/* if (**uni_sup<0) */
if (value_neg_p(**uni_sup)) { /* Status != verify */
inf_bound--; uni_inf--; inc_inf--;
if (inc_inf != inc_sup) {
Vector_Exchange(*uni_inf,*uni_sup,RowSize1);
bexchange(*inc_inf,*inc_sup,RowSize2);
}
}
else { /* status == verify */
(*sup_bound)++; uni_sup++; inc_sup++;
}
}
}
} /* RaySort */
static void SatMatrix_Extend(SatMatrix *Sat, Matrix* Mat, unsigned rows)
{
int i;
unsigned cols;
cols = (Mat->NbRows - 1)/(sizeof(int)*8) + 1;
Sat->p = (int **)realloc(Sat->p, rows * sizeof(int *));
if(!Sat->p) {
errormsg1("SatMatrix_Extend", "outofmem", "out of memory space");
return;
}
Sat->p_init = (int *)realloc(Sat->p_init, rows * cols * sizeof (int));
if(!Sat->p_init) {
errormsg1("SatMatrix_Extend", "outofmem", "out of memory space");
return;
}
for (i = 0; i < rows; ++i)
Sat->p[i] = Sat->p_init + (i * cols);
Sat->NbRows = rows;
}
/*
* Compute the dual of matrix 'Mat' and place it in matrix 'Ray'.'Mat'
* contains the constraints (equalities and inequalities) in rows and 'Ray'
* contains the ray space (lines and rays) in its rows. 'Sat' is a boolean
* saturation matrix defined as Sat(i,j)=0 if ray(i) saturates constraint(j),
* otherwise 1. The constraints in the 'Mat' matrix are processed starting at
* 'FirstConstraint', 'Ray' and 'Sat' matrices are changed accordingly.'NbBid'
* is the number of lines in the ray matrix and 'NbMaxRays' is the maximum
* number of rows (rays) permissible in the 'Ray' and 'Sat' matrix. Return 0
* if successful, otherwise return 1.
*/
static int Chernikova (Matrix *Mat,Matrix *Ray,SatMatrix *Sat, unsigned NbBid, unsigned NbMaxRays, unsigned FirstConstraint,unsigned dual) {
unsigned NbRay, Dimension, NbConstraints, RowSize1, RowSize2, sat_nbcolumns;
int sup_bound, equal_bound, index_non_zero, bound;
int i, j, k, l, redundant, rayonly, nbcommonconstraints;
int *Temp, aux;
int *ip1, *ip2;
unsigned bx, m, jx;
Value *p1, *p2, *p3;
#ifdef POLY_CH_DEBUG
fprintf(stderr, "[Chernikova: Input]\nRay = ");
Matrix_Print(stderr,0,Ray);
fprintf(stderr, "\nConstraints = ");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nSat = ");
SMPrint(Sat);
#endif
NbConstraints=Mat->NbRows;
NbRay = Ray->NbRows;
Dimension = Mat->NbColumns-1; /* Homogeneous Dimension */
sat_nbcolumns=Sat->NbColumns;
RowSize1=(Dimension+1);
RowSize2=sat_nbcolumns * sizeof(int);
Temp=(int *)malloc(RowSize2);
if(!Temp) {
errormsg1("Chernikova", "outofmem", "out of memory space");
return 0;
}
CATCH(any_exception_error) {
/*
* In case of overflow, free the allocated memory!
* Rethrow upwards the stack to forward the exception.
*/
free(Temp);
RETHROW();
}
TRY {
jx = FirstConstraint/WSIZE;
bx = MSB; bx >>= FirstConstraint%WSIZE;
for (k=FirstConstraint; k<NbConstraints; k++) {
/* Set the status word of each ray[i] to ray[i] dot constraint[k] */
/* This is equivalent to evaluating each ray by the constraint[k] */
/* 'index_non_zero' is assigned the smallest ray index which does */
/* not saturate the constraint. */
index_non_zero = NbRay;
for (i=0; i<NbRay; i++) {
p1 = Ray->p[i]+1;
p2 = Mat->p[k]+1;
p3 = Ray->p[i];
/* *p3 = *p1 * *p2 */
value_multiply(*p3,*p1,*p2);
p1++; p2++;
for (j=1; j<Dimension; j++) {
/* *p3 += *p1 * *p2 */
value_addmul(*p3, *p1, *p2);
p1++; p2++;
}
if (value_notzero_p(*p3) && (i<index_non_zero))
index_non_zero=i;
}
#ifdef POLY_CH_DEBUG
fprintf(stderr, "[Chernikova: A]\nRay = ");
Matrix_Print(stderr,0,Ray);
fprintf(stderr, "\nConstraints = ");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nSat = ");
SMPrint (Sat);
#endif
/* Find a bidirectional ray z such that cz <> 0 */
if (index_non_zero<NbBid) {
/* Discard index_non_zero bidirectional ray */
NbBid--;
if (NbBid!=index_non_zero)
Vector_Exchange(Ray->p[index_non_zero],Ray->p[NbBid],RowSize1);
#ifdef POLY_CH_DEBUG
fprintf(stderr,"************\n");
for(i=0;i<RowSize1;i++) {
value_print(stderr,P_VALUE_FMT,Ray->p[index_non_zero][i]);
}
fprintf(stderr,"\n******\n");
for(i=0;i<RowSize1;i++) {
value_print(stderr,P_VALUE_FMT,Ray->p[NbBid][i]);
}
fprintf(stderr,"\n*******\n");
#endif
/* Compute the new lineality space */
for (i=0; i<NbBid; i++)
if (value_notzero_p(Ray->p[i][0]))
Combine(Ray->p[i],Ray->p[NbBid],Ray->p[i],0,Dimension);
/* Add the positive part of index_non_zero bidirectional ray to */
/* the set of unidirectional rays */
if (value_neg_p(Ray->p[NbBid][0])) {
p1=Ray->p[NbBid];
for (j=0;j<Dimension+1; j++) {
/* *p1 = - *p1 */
value_oppose(*p1,*p1);
p1++;
}
}
#ifdef POLY_CH_DEBUG
fprintf(stderr, "[Chernikova: B]\nRay = ");
Ray->NbRows=NbRay;
Matrix_Print(stderr,0,Ray);
fprintf(stderr, "\nConstraints = ");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nSat = ");
SMPrint(Sat);
#endif
/* Compute the new pointed cone */
for (i=NbBid+1; i<NbRay; i++)
if (value_notzero_p(Ray->p[i][0]))
Combine(Ray->p[i],Ray->p[NbBid],Ray->p[i],0,Dimension);
/* Add the new ray */
if (value_notzero_p(Mat->p[k][0])) { /* Constraint is an inequality */
for (j=0;j<sat_nbcolumns;j++) {
Sat->p[NbBid][j] = 0; /* Saturation vec for new ray */
}
/* The new ray saturates everything except last inequality */
Sat->p[NbBid][jx] |= bx;
}
else { /* Constraint is an equality */
if (--NbRay != NbBid) {
Vector_Copy(Ray->p[NbRay],Ray->p[NbBid],Dimension+1);
SMVector_Copy(Sat->p[NbRay],Sat->p[NbBid],sat_nbcolumns);
}
}
#ifdef POLY_CH_DEBUG
fprintf(stderr, "[Chernikova: C]\nRay = ");
Ray->NbRows=NbRay;
Matrix_Print(stderr,0,Ray);
fprintf(stderr, "\nConstraints = ");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nSat = ");
SMPrint (Sat);
#endif
}
else { /* If the new constraint satisfies all the rays */
RaySort(Ray, Sat, NbBid, NbRay, &equal_bound, &sup_bound,
RowSize1, RowSize2,bx,jx);
/* Sort the unidirectional rays into R0, R+, R- */
/* Ray
NbRay-> bound-> ________
| R- | R- ==> ray.eq < 0 (outside domain)
sup-> |------|
| R+ | R+ ==> ray.eq > 0 (inside domain)
equal-> |------|
| R0 | R0 ==> ray.eq = 0 (on face of domain)
NbBid-> |______|
*/
#ifdef POLY_CH_DEBUG
fprintf(stderr, "[Chernikova: D]\nRay = ");
Ray->NbRows=NbRay;
Matrix_Print(stderr,0,Ray);
fprintf(stderr, "\nConstraints = ");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nSat = ");
SMPrint (Sat);
#endif
/* Compute only the new pointed cone */
bound=NbRay;
for (i=equal_bound; i<sup_bound; i++) /* for all pairs of R- and R+ */
for(j=sup_bound; j<bound; j++) {
/*--------------------------------------------------------------*/
/* Count the set of constraints saturated by R+ and R- */
/* Includes equalities, inequalities and the positivity constraint */
/*-----------------------------------------------------------------*/
nbcommonconstraints = 0;
for (l=0; l<jx; l++) {
aux = Temp[l] = Sat->p[i][l] | Sat->p[j][l];
for (m=MSB; m!=0; m>>=1)
if (!(aux&m))
nbcommonconstraints++;
}
aux = Temp[jx] = Sat->p[i][jx] | Sat->p[j][jx];
for (m=MSB; m!=bx; m>>=1)
if (!(aux&m))
nbcommonconstraints++;
rayonly = (value_zero_p(Ray->p[i][Dimension]) &&
value_zero_p(Ray->p[j][Dimension]) &&
(dual == 0));
if(rayonly)
nbcommonconstraints++; /* account for pos constr */
/*-----------------------------------------------------------------*/
/* Adjacency Test : is combination [R-,R+] a non redundant ray? */
/*-----------------------------------------------------------------*/
if (nbcommonconstraints+NbBid>=Dimension-2) { /* Dimensionality check*/
/* Check whether a ray m saturates the same set of constraints */
redundant=0;
for (m=NbBid; m<bound; m++)
if ((m!=i)&&(m!=j)) {
/* Two rays (r+ r-) are never made redundant by a vertex */
/* because the positivity constraint saturates both rays */
/* but not the vertex */
if (rayonly && value_notzero_p(Ray->p[m][Dimension]))
continue;
/* (r+ r-) is redundant if there doesn't exist an equation */
/* which saturates both r+ and r- but not rm. */
ip1 = Temp;
ip2 = Sat->p[m];
for (l=0; l<=jx; l++,ip2++,ip1++)
if (*ip2 & ~*ip1)
break;
if (l>jx) {
redundant=1;
break;
}
}
#ifdef POLY_CH_DEBUG
fprintf(stderr, "[Chernikova: E]\nRay = ");
Ray->NbRows=NbRay;
Matrix_Print(stderr,0,Ray);
fprintf(stderr, "\nConstraints = ");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nSat = ");
SMPrint (Sat);
#endif
/*------------------------------------------------------------*/
/* Add new ray generated by [R+,R-] */
/*------------------------------------------------------------*/
if (!redundant) {
if (NbRay==NbMaxRays) {
NbMaxRays *= 2;
Ray->NbRows = NbRay;
Matrix_Extend(Ray, NbMaxRays);
SatMatrix_Extend(Sat, Mat, NbMaxRays);
}
/* Compute the new ray */
Combine(Ray->p[j],Ray->p[i],Ray->p[NbRay],0,Dimension);
SatVector_OR(Sat->p[j],Sat->p[i],Sat->p[NbRay],sat_nbcolumns);
Sat->p[NbRay][jx] &= ~bx;
NbRay++;
}
}
}
#ifdef POLY_CH_DEBUG
fprintf(stderr,
"[Chernikova: F]\n"
"sup_bound=%d\n"
"equal_bound=%d\n"
"bound=%d\n"
"NbRay=%d\n"
"Dimension = %d\n"
"Ray = ",sup_bound,equal_bound,bound,NbRay,Dimension);
#endif
#ifdef POLY_CH_DEBUG
Ray->NbRows=NbRay;
fprintf(stderr, "[Chernikova: F]:\nRay = ");
Matrix_Print(stderr,0,Ray);
#endif
/* Eliminates all non extremal rays */
/* j = (Mat->p[k][0]) ? */
j = (value_notzero_p(Mat->p[k][0])) ?
sup_bound : equal_bound;
i = NbRay;
#ifdef POLY_CH_DEBUG
fprintf(stderr, "i = %d\nj = %d \n", i, j);
#endif
while ((j<bound)&&(i>bound)) {
i--;
Vector_Copy(Ray->p[i],Ray->p[j],Dimension+1);
SMVector_Copy(Sat->p[i],Sat->p[j],sat_nbcolumns);
j++;
}
#ifdef POLY_CH_DEBUG
fprintf(stderr, "i = %d\nj = %d \n", i, j);
fprintf(stderr,
"[Chernikova: F]\n"
"sup_bound=%d\n"
"equal_bound=%d\n"
"bound=%d\n"
"NbRay=%d\n"
"Dimension = %d\n"
"Ray = ",sup_bound,equal_bound,bound,NbRay, Dimension);
#endif
#ifdef POLY_CH_DEBUG
Ray->NbRows=NbRay;
fprintf(stderr, "[Chernikova: G]\nRay = ");
Matrix_Print(stderr,0,Ray);
#endif
if (j==bound)
NbRay=i;
else
NbRay=j;
}
NEXT(jx,bx);
}
Ray->NbRows=NbRay;
Sat->NbRows=NbRay;
} /* End of TRY */
UNCATCH(any_exception_error);
free(Temp);
#ifdef POLY_CH_DEBUG
fprintf(stderr, "[Chernikova: Output]\nRay = ");
Matrix_Print(stderr,0,Ray);
fprintf(stderr, "\nConstraints = ");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nSat = ");
SMPrint (Sat);
#endif
return 0;
} /* Chernikova */
static int Gauss4(Value **p, int NbEq, int NbRows, int Dimension)
{
int i, j, k, pivot, Rank;
int *column_index = NULL;
Value gcd;
value_init(gcd);
column_index=(int *)malloc(Dimension * sizeof(int));
if(!column_index) {
errormsg1("Gauss","outofmem","out of memory space");
value_clear(gcd);
return 0;
}
Rank=0;
CATCH(any_exception_error) {
if (column_index)
free(column_index);
value_clear(gcd);
RETHROW();
}
TRY {
for (j=1; j<=Dimension; j++) { /* for each column (except status) */
for (i=Rank; i<NbEq; i++) /* starting at diagonal, look down */
/* if (Mat->p[i][j] != 0) */
if (value_notzero_p(p[i][j]))
break; /* Find the first non zero element */
if (i!=NbEq) { /* If a non-zero element is found? */
if (i!=Rank) /* If it is found below the diagonal*/
Vector_Exchange(p[Rank]+1,p[i]+1,Dimension);
/* Normalize the pivot row by dividing it by the gcd */
/* gcd = Vector_Gcd(p[Rank]+1,Dimension) */
Vector_Gcd(p[Rank]+1,Dimension,&gcd);
if (value_cmp_si(gcd, 2) >= 0)
Vector_AntiScale(p[Rank]+1, p[Rank]+1, gcd, Dimension);
if (value_neg_p(p[Rank][j]))
Vector_Oppose(p[Rank]+1, p[Rank]+1, Dimension);
pivot=i;
for (i=pivot+1; i<NbEq; i++) { /* Zero out the rest of the column */
/* if (Mat->p[i][j] != 0) */
if (value_notzero_p(p[i][j]))
Combine(p[i],p[Rank],p[i],j,Dimension);
}
/* For each row with non-zero entry Mat->p[Rank], store the column */
/* number 'j' in 'column_index[Rank]'. This information will be */
/* useful in performing Gaussian elimination backward step. */
column_index[Rank]=j;
Rank++;
}
} /* end of Gaussian elimination forward step */
/* Back Substitution -- normalize the system of equations */
for (k=Rank-1; k>=0; k--) {
j = column_index[k];
/* Normalize the equations */
for (i=0; i<k; i++) {
/* if (Mat->p[i][j] != 0) */
if (value_notzero_p(p[i][j]))
Combine(p[i],p[k],p[i],j,Dimension);
}
/* Normalize the inequalities */
for (i=NbEq;i<NbRows;i++) {
/* if (Mat->p[i][j] != 0) */
if (value_notzero_p(p[i][j]))
Combine(p[i],p[k],p[i],j,Dimension);
}
}
} /* end of TRY */
UNCATCH(any_exception_error);
free(column_index), column_index = NULL;
value_clear(gcd);
return Rank;
} /* Gauss */
/*
* Compute a minimal system of equations using Gausian elimination method.
* 'Mat' is a matrix of constraints in which the first 'Nbeq' constraints
* are equations. The dimension of the homogenous system is 'Dimension'.
* The function returns the rank of the matrix 'Mat'.
*/
int Gauss(Matrix *Mat, int NbEq, int Dimension)
{
int Rank;
#ifdef POLY_DEBUG
fprintf(stderr, "[Gauss : Input]\nRay =");
Matrix_Print(stderr,0,Mat);
#endif
Rank = Gauss4(Mat->p, NbEq, Mat->NbRows, Dimension);
#ifdef POLY_DEBUG
fprintf(stderr, "[Gauss : Output]\nRay =");
Matrix_Print(stderr,0,Mat);
#endif
return Rank;
}
/*
* Given 'Mat' - a matrix of equations and inequalities, 'Ray' - a matrix of
* lines and rays, 'Sat' - the corresponding saturation matrix, and 'Filter'
* - an array to mark (with 1) the non-redundant equalities and inequalities,
* compute a polyhedron composed of 'Mat' as constraint matrix and 'Ray' as
* ray matrix after reductions. This function is usually called as a follow
* up to 'Chernikova' to remove redundant constraints or rays.
* Note: (1) 'Chernikova' ensures that there are no redundant lines and rays.
* (2) The same function can be used with constraint and ray matrix used
interchangbly.
*/
static Polyhedron *Remove_Redundants(Matrix *Mat,Matrix *Ray,SatMatrix *Sat,unsigned *Filter) {
int i, j, k;
unsigned Dimension, sat_nbcolumns, NbRay, NbConstraints, RowSize2,
*Trace = NULL, *bx = NULL, *jx = NULL, Dim_RaySpace, b;
unsigned NbBid, NbUni, NbEq, NbIneq;
unsigned NbBid2, NbUni2, NbEq2, NbIneq2;
int Redundant;
int aux, *temp2 = NULL;
Polyhedron *Pol = NULL;
Vector *temp1 = NULL;
unsigned Status;
Dimension = Mat->NbColumns-1; /* Homogeneous Dimension */
NbRay = Ray->NbRows;
sat_nbcolumns = Sat->NbColumns;
NbConstraints = Mat->NbRows;
RowSize2=sat_nbcolumns * sizeof(int);
temp1 = Vector_Alloc(Dimension+1);
if (!temp1) {
errormsg1("Remove_Redundants", "outofmem", "out of memory space");
return 0;
}
if (Filter) {
temp2 = (int *)calloc(sat_nbcolumns, sizeof(int));
if (!temp2)
goto oom;
}
/* Introduce indirections into saturation matrix 'Sat' to simplify */
/* processing with 'Sat' and allow easy exchanges of columns. */
bx = (unsigned *)malloc(NbConstraints * sizeof(unsigned));
if (!bx)
goto oom;
jx = (unsigned *)malloc(NbConstraints * sizeof(unsigned));
if (!jx)
goto oom;
CATCH(any_exception_error) {
Vector_Free(temp1);
if (temp2) free(temp2);
if (bx) free(bx);
if (jx) free(jx);
if (Trace) free(Trace);
if (Pol) Polyhedron_Free(Pol);
RETHROW();
}
TRY {
/* For each constraint 'j' following mapping is defined to facilitate */
/* data access from saturation matrix 'Sat' :- */
/* (1) jx[j] -> floor[j/(8*sizeof(int))] */
/* (2) bx[j] -> bin(00..10..0) where position of 1 = j%(8*sizeof(int)) */
i = 0;
b = MSB;
for (j=0; j<NbConstraints; j++) {
jx[j] = i;
bx[j] = b;
NEXT(i,b);
}
/*
* STEP(0): Count the number of vertices among the rays while initializing
* the ray status count to 0. If no vertices are found, quit the procedure
* and return an empty polyhedron as the result.
*/
/* Reset the status element of each ray to zero. Store the number of */
/* vertices in 'aux'. */
aux = 0;
for (i=0; i<NbRay; i++) {
/* Ray->p[i][0] = 0 */
value_set_si(Ray->p[i][0],0);
/* If ray(i) is a vertex of the Inhomogenous system */
if (value_notzero_p(Ray->p[i][Dimension]))
aux++;
}
/* If no vertices, return an empty polyhedron. */
if (!aux)
goto empty;
#ifdef POLY_RR_DEBUG
fprintf(stderr, "[Remove_redundants : Init]\nConstraints =");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nRays =");
Matrix_Print(stderr,0,Ray);
#endif
/*
* STEP(1): Compute status counts for both rays and inequalities. For each
* constraint, count the number of vertices/rays saturated by that
* constraint, and put the result in the status words. At the same time,
* for each vertex/ray, count the number of constraints saturated by it.
* Delete any positivity constraints, but give rays credit in their status
* counts for saturating the positivity constraint.
*/
NbEq=0;
#ifdef POLY_RR_DEBUG
fprintf (stderr, " j = ");
#endif
for (j=0; j<NbConstraints; j++) {
#ifdef POLY_RR_DEBUG
fprintf (stderr, " %i ", j);
fflush (stderr);
#endif
/* If constraint(j) is an equality, mark '1' in array 'temp2' */
if (Filter && value_zero_p(Mat->p[j][0]))
temp2[jx[j]] |= bx[j];
/* Reset the status element of each constraint to zero */
value_set_si(Mat->p[j][0],0);
/* Identify and remove the positivity constraint 1>=0 */
i = First_Non_Zero(Mat->p[j]+1, Dimension-1);
#ifdef POLY_RR_DEBUG
fprintf(stderr, "[Remove_redundants : IntoStep1]\nConstraints =");
Matrix_Print(stderr,0,Mat);
fprintf (stderr, " j = %i \n", j);
#endif
/* Check if constraint(j) is a positivity constraint, 1 >= 0, or if it */
/* is 1==0. If constraint(j) saturates all the rays of the matrix 'Ray'*/
/* then it is an equality. in this case, return an empty polyhedron. */
if (i == -1) {
for (i=0; i<NbRay; i++)
if (!(Sat->p[i][jx[j]]&bx[j])) {
/* Mat->p[j][0]++ */
value_increment(Mat->p[j][0],Mat->p[j][0]);
}
/* if ((Mat->p[j][0] == NbRay) && : it is an equality
(Mat->p[j][Dimension] != 0)) : and its not 0=0 */
if ((value_cmp_si(Mat->p[j][0], NbRay) == 0) &&
(value_notzero_p(Mat->p[j][Dimension])))
goto empty;
/* Delete the positivity constraint */
NbConstraints--;
if (j==NbConstraints) continue;
Vector_Exchange(Mat->p[j], Mat->p[NbConstraints], temp1->Size);
exchange(jx[j], jx[NbConstraints], aux);
exchange(bx[j], bx[NbConstraints], aux);
j--; continue;
}
/* Count the number of vertices/rays saturated by each constraint. At */
/* the same time, count the number of constraints saturated by each ray*/
for (i=0; i<NbRay; i++)
if (!(Sat->p[i][jx[j]]&bx[j])) {
/* Mat->p[j][0]++ */
value_increment(Mat->p[j][0],Mat->p[j][0]);
/* Ray->p[i][0]++ */
value_increment (Ray->p[i][0],Ray->p[i][0]);
}
/* if (Mat->p[j][0]==NbRay) then increment the number of eq. count */
if (value_cmp_si(Mat->p[j][0], NbRay) == 0)
NbEq++; /* all vertices/rays are saturated */
}
Mat->NbRows = NbConstraints;
NbBid=0;
for (i=0; i<NbRay; i++) {
/* Give rays credit for saturating the positivity constraint */
if (value_zero_p(Ray->p[i][Dimension]))
/* Ray->p[i][0]++ */
value_increment(Ray->p[i][0],Ray->p[i][0]);
/* If ray(i) saturates all the constraints including positivity */
/* constraint then it is a bi-directional ray or line. Increment */
/* 'NbBid' by one. */
/* if (Ray->p[i][0]==NbConstraints+1) */
if (value_cmp_si(Ray->p[i][0], NbConstraints+1) == 0)
NbBid++;
}
#ifdef POLY_RR_DEBUG
fprintf(stderr, "[Remove_redundants : Step1]\nConstraints =");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nRay =");
Matrix_Print(stderr,0,Ray);
#endif
/*
* STEP(2): Sort equalities to the top of constraint matrix 'Mat'. Detect
* implicit equations such as y>=3; y<=3. Keep Inequalities in same
* relative order. (Note: Equalities are constraints which saturate all of
* the rays)
*/
for (i=0; i<NbEq; i++) {
/* If constraint(i) doesn't saturate some ray, then it is an inequality*/
if (value_cmp_si(Mat->p[i][0], NbRay) != 0) {
/* Skip over inequalities and find an equality */
for (k=i+1; value_cmp_si(Mat->p[k][0], NbRay) != 0 && k < NbConstraints; k++)
;
if (k==NbConstraints) /* If none found then error */ break;
/* Slide inequalities down the array 'Mat' and move equality up to */
/* position 'i'. */
Vector_Copy(Mat->p[k], temp1->p, temp1->Size);
aux = jx[k];
j = bx[k];
for (;k>i;k--) {
Vector_Copy(Mat->p[k-1], Mat->p[k], temp1->Size);
jx[k] = jx[k-1];
bx[k] = bx[k-1];
}
Vector_Copy(temp1->p, Mat->p[i], temp1->Size);
jx[i] = aux;
bx[i] = j;
}
}
/* for SIMPLIFY */
if (Filter) {
Value mone;
value_init(mone);
value_set_si(mone, -1);
/* Normalize equalities to have lexpositive coefficients to
* be able to detect identical equalities.
*/
for (i = 0; i < NbEq; i++) {
int pos = First_Non_Zero(Mat->p[i]+1, Dimension);
if (pos == -1)
continue;
if (value_neg_p(Mat->p[i][1+pos]))
Vector_Scale(Mat->p[i]+1, Mat->p[i]+1, mone, Dimension);
}
value_clear(mone);
for (i=0; i<NbEq; i++) {
/* Detect implicit constraints such as y>=3 and y<=3 */
Redundant = 0;
for (j=i+1; j<NbEq; j++) {
/* Only check equalities, i.e., 'temp2' has entry 1 */
if (!(temp2[jx[j]] & bx[j]))
continue;
/* Redundant if both are same `and' constraint(j) was equality. */
if (Vector_Equal(Mat->p[i]+1, Mat->p[j]+1, Dimension)) {
Redundant=1;
break;
}
}
/* Set 'Filter' entry to 1 corresponding to the irredundant equality*/
if (!Redundant) Filter[jx[i]] |= bx[i]; /* set flag */
}
}
#ifdef POLY_RR_DEBUG
fprintf(stderr, "[Remove_redundants : Step2]\nConstraints =");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nRay =");
Matrix_Print(stderr,0,Ray);
#endif
/*
* STEP(3): Perform Gaussian elimiation on the list of equalities. Obtain
* a minimal basis by solving for as many variables as possible. Use this
* solution to reduce the inequalities by eliminating as many variables as
* possible. Set NbEq2 to the rank of the system of equalities.
*/
NbEq2 = Gauss(Mat,NbEq,Dimension);
/* If number of equalities is not less then the homogenous dimension, */
/* return an empty polyhedron. */
if (NbEq2 >= Dimension)
goto empty;
#ifdef POLY_RR_DEBUG
fprintf(stderr, "[Remove_redundants : Step3]\nConstraints =");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nRay =");
Matrix_Print(stderr,0,Ray);
#endif
/*
* STEP(4): Sort lines to the top of ray matrix 'Ray', leaving rays
* afterwards. Detect implicit lines such as ray(1,2) and ray(-1,-2).
* (Note: Lines are rays which saturate all of the constraints including
* the positivity constraint 1>=0.
*/
for (i=0, k=NbRay; i<NbBid && k>i; i++) {
/* If ray(i) doesn't saturate some constraint then it is not a line */
if (value_cmp_si(Ray->p[i][0], NbConstraints+1) != 0) {
/* Skip over rays and vertices and find a line (bi-directional rays) */
while (--k > i && value_cmp_si(Ray->p[k][0], NbConstraints+1) != 0)
;
/* Exchange positions of ray(i) and line(k), thus sorting lines to */
/* the top of matrix 'Ray'. */
Vector_Exchange(Ray->p[i], Ray->p[k], temp1->Size);
bexchange(Sat->p[i], Sat->p[k], RowSize2);
}
}
#ifdef POLY_RR_DEBUG
fprintf(stderr, "[Remove_redundants : Step4]\nConstraints =");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nRay =");
Matrix_Print(stderr,0,Ray);
#endif
/*
* STEP(5): Perform Gaussian elimination on the lineality space to obtain
* a minimal basis of lines. Use this basis to reduce the representation
* of the uniderectional rays. Set 'NbBid2' to the rank of the system of
* lines.
*/
NbBid2 = Gauss(Ray, NbBid, Dimension);
#ifdef POLY_RR_DEBUG
fprintf(stderr, "[Remove_redundants : After Gauss]\nRay =");
Matrix_Print(stderr,0,Ray);
#endif
/* If number of lines is not less then the homogenous dimension, return */
/* an empty polyhedron. */
if (NbBid2>=Dimension) {
errormsg1("RemoveRedundants", "rmrdt", "dimension error");
goto empty;
}
/* Compute dimension of non-homogenous ray space */
Dim_RaySpace = Dimension-1-NbEq2-NbBid2;
#ifdef POLY_RR_DEBUG
fprintf(stderr, "[Remove_redundants : Step5]\nConstraints =");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nRay =");
Matrix_Print(stderr,0,Ray);
#endif
/*
* STEP(6): Do a first pass filter of inequalities and equality identifi-
* cation. New positivity constraints may have been created by step(3).
* Check for and eliminate them. Count the irredundant inequalities and
* store count in 'NbIneq'.
*/
NbIneq=0;
for (j=0; j<NbConstraints; j++) {
/* Identify and remove the positivity constraint 1>=0 */
i = First_Non_Zero(Mat->p[j]+1, Dimension-1);
/* Check if constraint(j) is a positivity constraint, 1>= 0, or if it */
/* is 1==0. */
if (i == -1) {
/* if ((Mat->p[j][0]==NbRay) && : it is an equality
(Mat->p[j][Dimension]!=0)) : and its not 0=0 */
if ((value_cmp_si(Mat->p[j][0], NbRay) == 0) &&
(value_notzero_p(Mat->p[j][Dimension])))
goto empty;
/* Set the positivity constraint redundant by setting status element */
/* equal to 2. */
value_set_si(Mat->p[j][0],2);
continue;
}
Status = VALUE_TO_INT(Mat->p[j][0]);
if (Status == 0)
value_set_si(Mat->p[j][0], 2); /* constraint is redundant */
else if (Status < Dim_RaySpace)
value_set_si(Mat->p[j][0], 2); /* constraint is redundant */
else if (Status == NbRay)
value_set_si(Mat->p[j][0], 0); /* constraint is an equality */
else {
NbIneq++; /* constraint is an irredundant inequality */
value_set_si(Mat->p[j][0], 1); /* inequality */
}
}
#ifdef POLY_RR_DEBUG
fprintf(stderr, "[Remove_redundants : Step6]\nConstraints =");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nRay =");
Matrix_Print(stderr,0,Ray);
#endif
/*
* STEP(7): Do a first pass filter of rays and identification of lines.
* Count the irredundant Rays and store count in 'NbUni'.
*/
NbUni=0;
for (j=0; j<NbRay; j++) {
Status = VALUE_TO_INT(Ray->p[j][0]);
if (Status < Dim_RaySpace)
value_set_si(Ray->p[j][0], 2); /* ray is redundant */
else if (Status == NbConstraints+1)
value_set_si(Ray->p[j][0], 0); /* ray is a line */
else {
NbUni++; /* an irredundant unidirectional ray. */
value_set_si(Ray->p[j][0], 1); /* ray */
}
}
/*
* STEP(8): Create the polyhedron (using approximate sizes).
* Number of constraints = NbIneq + NbEq2 + 1
* Number of rays = NbUni + NbBid2
* Partially fill the polyhedron structure with the lines computed in step
* 3 and the equalities computed in step 5.
*/
Pol = Polyhedron_Alloc(Dimension-1, NbIneq+NbEq2+1, NbUni+NbBid2);
if (!Pol) {
UNCATCH(any_exception_error);
goto oom;
}
Pol->NbBid = NbBid2;
Pol->NbEq = NbEq2;
/* Partially fill the polyhedron structure */
if (NbBid2) Vector_Copy(Ray->p[0], Pol->Ray[0], (Dimension+1)*NbBid2);
if (NbEq2) Vector_Copy(Mat->p[0], Pol->Constraint[0], (Dimension+1)*NbEq2);
#ifdef POLY_RR_DEBUG
fprintf(stderr, "[Remove_redundants : Step7]\nConstraints =");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nRay =");
Matrix_Print(stderr,0,Ray);
#endif
/*
* STEP(9): Final Pass filter of inequalities and detection of redundant
* inequalities. Redundant inequalities include:
* (1) Inequalities which are always true, such as 1>=0,
* (2) Redundant inequalities such as y>=4 given y>=3, or x>=1 given x=2.
* (3) Redundant inequalities such as x+y>=5 given x>=3 and y>=2.
* Every 'good' inequality must saturate at least 'Dimension' rays and be
* unique.
*/
/* 'Trace' is a (1 X sat_nbcolumns) row matrix to hold the union of all */
/* rows (corresponding to irredundant rays) of saturation matrix 'Sat' */
/* which saturate some constraint 'j'. See figure below:- */
Trace=(unsigned *)malloc(sat_nbcolumns * sizeof(unsigned));
if(!Trace) {
UNCATCH(any_exception_error);
goto oom;
}
/* NbEq NbConstraints
|----------->
___________j____
| | |
| Mat | |
|___________|___|
|
NbRay ^ ________ ____________|____
| |-------|--------|-----------0---|t1
|i|-------|--------|-----------0---|t2
| | Ray | | Sat |
NbBid - |-------|--------|-----------0---|tk
|_______| |_______________|
|
|
-OR- (of rows t1,t2,...,tk)
________|___|____
|_____Trace_0___|
*/
NbIneq2 = 0;
for (j=NbEq; j<NbConstraints; j++) {
/* if (Mat->p[j][0]==1) : non-redundant inequality */
if (value_one_p (Mat->p[j][0])) {
for (k=0; k<sat_nbcolumns; k++) Trace[k]=0; /* init Trace */
/* Compute Trace: the union of all rows of Sat where constraint(j) */
/* is saturated. */
for (i=NbBid; i<NbRay; i++)
/* if (Ray->p[i][0]==1) */
if (value_one_p(Ray->p[i][0])) {
if (!(Sat->p[i][jx[j]]&bx[j]))
for (k=0; k<sat_nbcolumns; k++) Trace[k] |= Sat->p[i][k];
}
/* Only constraint(j) should saturate this set of vertices/rays. */
/* If another non-redundant constraint also saturates this set, */
/* then constraint(j) is redundant */
Redundant=0;
for (i=NbEq; i<NbConstraints; i++) {
/* if ((Mat->p[i][0] ==1) && (i!=j) && !(Trace[jx[i]] & bx[i]) ) */
if (value_one_p(Mat->p[i][0]) && (i!=j) && !(Trace[jx[i]] & bx[i])) {
Redundant=1;
break;
}
}
if (Redundant) {
value_set_si(Mat->p[j][0],2);
}
else {
Vector_Copy(Mat->p[j], Pol->Constraint[NbEq2+NbIneq2], Dimension+1);
if (Filter) Filter[jx[j]] |= bx[j]; /* for SIMPLIFY */
NbIneq2++;
}
}
}
free(Trace), Trace = NULL;
#ifdef POLY_RR_DEBUG
fprintf(stderr, "[Remove_redundants : Step8]\nConstraints =");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nRay =");
Matrix_Print(stderr,0,Ray);
#endif
/*
* Step(10): Final pass filter of rays and detection of redundant rays.
* The final list of rays is written to polyhedron.
*/
/* Trace is a (NbRay x 1) column matrix to hold the union of all columns */
/* (corresponding to irredundant inequalities) of saturation matrix 'Sat'*/
/* which saturate some ray 'i'. See figure below:- */
Trace=(unsigned *)malloc(NbRay * sizeof(unsigned));
if(!Trace) {
UNCATCH(any_exception_error);
goto oom;
}
/* NbEq NbConstraints
|---------->
___________j_____
| | | | |
| Mat | |
|______|_|___|__|
| | |
NbRay ^ _________ _______|_|___|__ ___
| | | | | | | | |T|
| | Ray | | Sat| | | | |r|
| | | | | | | | |a| Trace = Union[col(t1,t2,..,tk)]
|i|-------|------>i 0 0 0 | |c|
NbBid - | | | | | | | |e|
|_______| |______|_|___|__| |_|
t1 t2 tk
*/
NbUni2 = 0;
/* Let 'aux' be the number of rays not vertices */
aux = 0;
for (i=NbBid; i<NbRay; i++) {
/* if (Ray->p[i][0]==1) */
if (value_one_p (Ray->p[i][0])) {
/* if (Ray->p[i][Dimension]!=0) : vertex */
if (value_notzero_p (Ray->p[i][Dimension]))
for (k=NbBid; k<NbRay; k++) Trace[k]=0; /* init Trace */
else /* for ray */
/* Include the positivity constraint incidences for rays. The */
/* positivity constraint saturates all rays and no vertices */
for (k=NbBid; k<NbRay; k++)
/* Trace[k]=(Ray->p[k][Dimension]!=0); */
Trace[k] = (value_notzero_p (Ray->p[k][Dimension]));
/* Compute Trace: the union of all columns of Sat where ray(i) is */
/* saturated. */
for (j=NbEq; j<NbConstraints; j++)
/* if (Mat->p[j][0]==1) : inequality */
if (value_one_p (Mat->p[j][0])) {
if (!(Sat->p[i][jx[j]]&bx[j]))
for (k=NbBid; k<NbRay; k++) Trace[k] |= Sat->p[k][jx[j]]&bx[j];
}
/* If ray i does not saturate any inequalities (other than the */
/* the positivity constraint, then it is the case that there is */
/* only one inequality and that ray is its orthogonal */
/* only ray(i) should saturate this set of inequalities. If */
/* another non-redundant ray also saturates this set, then ray(i)*/
/* is redundant */
Redundant = 0;
for (j=NbBid; j<NbRay; j++) {
/* if ( (Ray->p[j][0]==1) && (i!=j) && !Trace[j] ) */
if (value_one_p (Ray->p[j][0]) && (i!=j) && !Trace[j]) {
Redundant=1;
break;
}
}
if (Redundant)
value_set_si(Ray->p[i][0],2);
else {
Vector_Copy(Ray->p[i], Pol->Ray[NbBid2+NbUni2], Dimension+1);
NbUni2++; /* Increment number of uni-directional rays */
/* if (Ray->p[i][Dimension]==0) */
if (value_zero_p (Ray->p[i][Dimension]))
aux++; /* Increment number of rays which are not vertices */
}
}
}
/* Include the positivity constraint */
if (aux>=Dim_RaySpace) {
Vector_Set(Pol->Constraint[NbEq2+NbIneq2],0,Dimension+1);
value_set_si(Pol->Constraint[NbEq2+NbIneq2][0],1);
value_set_si(Pol->Constraint[NbEq2+NbIneq2][Dimension],1);
NbIneq2++;
}
} /* end of TRY */
UNCATCH(any_exception_error);
#ifdef POLY_RR_DEBUG
fprintf(stderr, "[Remove_redundants : Step9]\nConstraints =");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nRay =");
Matrix_Print(stderr,0,Ray);
#endif
free(Trace);
free(bx);
free(jx);
if (temp2)
free(temp2);
Pol->NbConstraints = NbEq2 + NbIneq2;
Pol->NbRays = NbBid2 + NbUni2;
Vector_Free(temp1);
F_SET(Pol,
POL_VALID | POL_INEQUALITIES | POL_FACETS | POL_POINTS | POL_VERTICES);
return Pol;
oom:
errormsg1("Remove_Redundants", "outofmem", "out of memory space");
Vector_Free(temp1);
if (temp2)
free(temp2);
if (bx)
free(bx);
if (jx)
free(jx);
return NULL;
empty:
Vector_Free(temp1);
if (temp2)
free(temp2);
if (bx)
free(bx);
if (jx)
free(jx);
UNCATCH(any_exception_error);
return Empty_Polyhedron(Dimension-1);
} /* Remove_Redundants */
/*
* Allocate memory space for polyhedron.
*/
Polyhedron* Polyhedron_Alloc(unsigned Dimension,unsigned NbConstraints,unsigned NbRays) {
Polyhedron *Pol;
unsigned NbRows,NbColumns;
int i,j;
Value *p, **q;
Pol=(Polyhedron *)malloc(sizeof(Polyhedron));
if(!Pol) {
errormsg1("Polyhedron_Alloc", "outofmem", "out of memory space");
return 0;
}
Pol->next = (Polyhedron *)0;
Pol->Dimension = Dimension;
Pol->NbConstraints = NbConstraints;
Pol->NbRays = NbRays;
Pol->NbEq = 0;
Pol->NbBid = 0;
Pol->flags = 0;
NbRows = NbConstraints + NbRays;
NbColumns = Dimension + 2;
q = (Value **)malloc(NbRows * sizeof(Value *));
if(!q) {
errormsg1("Polyhedron_Alloc", "outofmem", "out of memory space");
return 0;
}
p = value_alloc(NbRows*NbColumns, &Pol->p_Init_size);
if(!p) {
free(q);
errormsg1("Polyhedron_Alloc", "outofmem", "out of memory space");
return 0;
}
Pol->Constraint = q;
Pol->Ray = q + NbConstraints;
Pol->p_Init = p;
for (i=0;i<NbRows;i++) {
*q++ = p;
p += NbColumns;
}
return Pol;
} /* Polyhedron_Alloc */
/*
* Free the memory space occupied by the single polyhedron.
*/
void Polyhedron_Free(Polyhedron *Pol)
{
if(!Pol)
return;
value_free(Pol->p_Init, Pol->p_Init_size);
free(Pol->Constraint);
free(Pol);
return;
} /* Polyhedron_Free */
/*
* Free the memory space occupied by the domain.
*/
void Domain_Free(Polyhedron *Pol)
{
Polyhedron *Next;
for (; Pol; Pol = Next) {
Next = Pol->next;
Polyhedron_Free(Pol);
}
return;
} /* Domain_Free */
/*
* Print the contents of a polyhedron.
*/
void Polyhedron_Print(FILE *Dst, const char *Format, const Polyhedron *Pol)
{
unsigned Dimension, NbConstraints, NbRays;
int i, j;
Value *p;
if (!Pol) {
fprintf(Dst, "<null polyhedron>\n");
return;
}
Dimension = Pol->Dimension + 2; /* Homogenous Dimension + status */
NbConstraints = Pol->NbConstraints;
NbRays = Pol->NbRays;
fprintf(Dst, "POLYHEDRON Dimension:%d\n", Pol->Dimension);
fprintf(Dst," Constraints:%d Equations:%d Rays:%d Lines:%d\n",
Pol->NbConstraints, Pol->NbEq, Pol->NbRays, Pol->NbBid);
fprintf(Dst,"Constraints %d %d\n", NbConstraints, Dimension);
for (i=0;i<NbConstraints;i++) {
p=Pol->Constraint[i];
/* if (*p) */
if (value_notzero_p (*p))
fprintf(Dst,"Inequality: [");
else
fprintf(Dst,"Equality: [");
p++;
for (j=1;j<Dimension;j++) {
value_print(Dst,Format,*p++);
}
(void)fprintf(Dst," ]\n");
}
(void)fprintf(Dst, "Rays %d %d\n", NbRays, Dimension);
for (i=0;i<NbRays;i++) {
p=Pol->Ray[i];
/* if (*p) */
if (value_notzero_p (*p)) {
p++;
/* if ( p[Dimension-2] ) */
if (value_notzero_p (p[Dimension-2]))
fprintf(Dst, "Vertex: [");
else
fprintf(Dst, "Ray: [");
}
else {
p++;
fprintf(Dst, "Line: [");
}
for (j=1; j < Dimension-1; j++) {
value_print(Dst,Format,*p++);
}
/* if (*p) */
if (value_notzero_p (*p)) {
fprintf( Dst, " ]/" );
value_print(Dst,VALUE_FMT,*p);
fprintf( Dst, "\n" );
}
else
fprintf(Dst, " ]\n");
}
if (Pol->next) {
fprintf(Dst, "UNION ");
Polyhedron_Print(Dst,Format,Pol->next);
}
} /* Polyhedron_Print */
/*
* Print the contents of a polyhedron 'Pol' (used for debugging purpose).
*/
void PolyPrint (Polyhedron *Pol) {
Polyhedron_Print(stderr,"%4d",Pol);
} /* PolyPrint */
/*
* Create and return an empty polyhedron of non-homogenous dimension
* 'Dimension'. An empty polyhedron is characterized by :-
* (a) The dimension of the ray-space is -1.
* (b) There is an over-constrained system of equations given by:
* x=0, y=0, ...... z=0, 1=0
*/
Polyhedron *Empty_Polyhedron(unsigned Dimension) {
Polyhedron *Pol;
int i;
Pol = Polyhedron_Alloc(Dimension, Dimension+1, 0);
if (!Pol) {
errormsg1("Empty_Polyhedron", "outofmem", "out of memory space");
return 0;
}
Vector_Set(Pol->Constraint[0],0,(Dimension+1)*(Dimension+2));
for (i=0; i<=Dimension; i++) {
/* Pol->Constraint[i][i+1]=1 */
value_set_si(Pol->Constraint[i][i+1],1);
}
Pol->NbEq = Dimension+1;
Pol->NbBid = 0;
F_SET(Pol,
POL_VALID | POL_INEQUALITIES | POL_FACETS | POL_POINTS | POL_VERTICES);
return Pol;
} /* Empty_Polyhedron */
/*
* Create and return a universe polyhedron of non-homogenous dimension
* 'Dimension'. A universe polyhedron is characterized by :-
* (a) The dimension of rayspace is zero.
* (b) The dimension of lineality space is the dimension of the polyhedron.
* (c) There is only one constraint (positivity constraint) in the constraint
* set given by : 1 >= 0.
* (d) The bi-directional ray set is the canonical set of vectors.
* (e) The only vertex is the origin (0,0,0,....0).
*/
Polyhedron *Universe_Polyhedron(unsigned Dimension) {
Polyhedron *Pol;
int i;
Pol = Polyhedron_Alloc(Dimension,1,Dimension+1);
if (!Pol) {
errormsg1("Universe_Polyhedron", "outofmem", "out of memory space");
return 0;
}
Vector_Set(Pol->Constraint[0],0,(Dimension+2));
/* Pol->Constraint[0][0] = 1 */
value_set_si(Pol->Constraint[0][0],1);
/* Pol->Constraint[0][Dimension+1] = 1 */
value_set_si(Pol->Constraint[0][Dimension+1],1);
Vector_Set(Pol->Ray[0],0,(Dimension+1)*(Dimension+2));
for (i=0;i<=Dimension;i++) {
/* Pol->Ray[i][i+1]=1 */
value_set_si(Pol->Ray[i][i+1],1);
}
/* Pol->Ray[Dimension][0] = 1 : vertex status */
value_set_si(Pol->Ray[Dimension][0],1);
Pol->NbEq = 0;
Pol->NbBid = Dimension;
F_SET(Pol,
POL_VALID | POL_INEQUALITIES | POL_FACETS | POL_POINTS | POL_VERTICES);
return Pol;
} /* Universe_Polyhedron */
/*
Sort constraints and remove trivially redundant constraints.
*/
static void SortConstraints(Matrix *Constraints, unsigned NbEq)
{
int i, j, k;
for (i = NbEq; i < Constraints->NbRows; ++i) {
int max = i;
for (k = i+1; k < Constraints->NbRows; ++k) {
for (j = 1; j < Constraints->NbColumns-1; ++j) {
if (value_eq(Constraints->p[k][j],
Constraints->p[max][j]))
continue;
if (value_abs_lt(Constraints->p[k][j],
Constraints->p[max][j]))
break;
if (value_abs_eq(Constraints->p[k][j],
Constraints->p[max][j]) &&
value_pos_p(Constraints->p[max][j]))
break;
max = k;
break;
}
/* equal, except for possibly the constant
* => remove constraint with biggest constant
*/
if (j == Constraints->NbColumns-1) {
if (value_lt(Constraints->p[k][j], Constraints->p[max][j]))
Vector_Exchange(Constraints->p[k],
Constraints->p[max],
Constraints->NbColumns);
Constraints->NbRows--;
if (k < Constraints->NbRows)
Vector_Exchange(Constraints->p[k],
Constraints->p[Constraints->NbRows],
Constraints->NbColumns);
k--;
}
}
if (max != i)
Vector_Exchange(Constraints->p[max], Constraints->p[i],
Constraints->NbColumns);
}
}
/*
Search for trivial implicit equalities,
assuming the constraints have been sorted.
*/
static int ImplicitEqualities(Matrix *Constraints, unsigned NbEq)
{
int row, nrow, k;
int found = 0;
Value tmp;
for (row = NbEq; row < Constraints->NbRows; ++row) {
int d = First_Non_Zero(Constraints->p[row]+1, Constraints->NbColumns-2);
if (d == -1) {
/* -n >= 0 */
if (value_neg_p(Constraints->p[row][Constraints->NbColumns-1])) {
value_set_si(Constraints->p[row][0], 0);
found = 1;
}
break;
}
if (value_neg_p(Constraints->p[row][1+d]))
continue;
for (nrow = row+1; nrow < Constraints->NbRows; ++nrow) {
if (value_zero_p(Constraints->p[nrow][1+d]))
break;
if (value_pos_p(Constraints->p[nrow][1+d]))
continue;
for (k = d; k < Constraints->NbColumns-1; ++k) {
if (value_abs_ne(Constraints->p[row][1+k],
Constraints->p[nrow][1+k]))
break;
if (value_zero_p(Constraints->p[row][1+k]))
continue;
if (value_eq(Constraints->p[row][1+k], Constraints->p[nrow][1+k]))
break;
}
if (k == Constraints->NbColumns-1) {
value_set_si(Constraints->p[row][0], 0);
value_set_si(Constraints->p[nrow][0], 0);
found = 1;
break;
}
if (k != Constraints->NbColumns-2)
continue;
/* if the constants are such that
* the sum c1+c2 is negative then the constraints conflict
*/
value_init(tmp);
value_addto(tmp, Constraints->p[row][1+k],
Constraints->p[nrow][1+k]);
if (value_sign(tmp) < 0) {
Vector_Set(Constraints->p[row], 0, Constraints->NbColumns-1);
Vector_Set(Constraints->p[nrow], 0, Constraints->NbColumns-1);
value_set_si(Constraints->p[row][1+k], 1);
value_set_si(Constraints->p[nrow][1+k], 1);
found = 1;
}
value_clear(tmp);
if (found)
break;
}
}
return found;
}
/**
Given a matrix of constraints ('Constraints'), construct and return a
polyhedron.
@param Constraints Constraints (may be modified!)
@param NbMaxRays Estimated number of rays in the ray matrix of the
polyhedron.
@return newly allocated Polyhedron
*/
Polyhedron *Constraints2Polyhedron(Matrix *Constraints,unsigned NbMaxRays) {
Polyhedron *Pol = NULL;
Matrix *Ray = NULL;
SatMatrix *Sat = NULL;
unsigned Dimension, nbcolumns;
int i;
Dimension = Constraints->NbColumns - 1; /* Homogeneous Dimension */
if (Dimension < 1) {
errormsg1("Constraints2Polyhedron","invalidpoly","invalid polyhedron dimension");
return 0;
}
/* If there is no constraint in the constraint matrix, return universe */
/* polyhderon. */
if (Constraints->NbRows==0) {
Pol = Universe_Polyhedron(Dimension-1);
return Pol;
}
if (POL_ISSET(NbMaxRays, POL_NO_DUAL)) {
unsigned NbEq;
unsigned Rank;
Value tmp;
if (POL_ISSET(NbMaxRays, POL_INTEGER))
value_init(tmp);
do {
NbEq = 0;
/* Move equalities up */
for (i = 0; i < Constraints->NbRows; ++i)
if (value_zero_p(Constraints->p[i][0])) {
if (POL_ISSET(NbMaxRays, POL_INTEGER) &&
ConstraintSimplify(Constraints->p[i],
Constraints->p[i], Dimension+1, &tmp)) {
value_clear(tmp);
return Empty_Polyhedron(Dimension-1);
}
/* detect 1 == 0, possibly created by ImplicitEqualities */
if (First_Non_Zero(Constraints->p[i]+1, Dimension-1) == -1 &&
value_notzero_p(Constraints->p[i][Dimension])) {
if (POL_ISSET(NbMaxRays, POL_INTEGER))
value_clear(tmp);
return Empty_Polyhedron(Dimension-1);
}
if (i != NbEq)
ExchangeRows(Constraints, i, NbEq);
++NbEq;
}
Rank = Gauss(Constraints, NbEq, Dimension);
if (POL_ISSET(NbMaxRays, POL_INTEGER))
for (i = NbEq; i < Constraints->NbRows; ++i)
ConstraintSimplify(Constraints->p[i],
Constraints->p[i], Dimension+1, &tmp);
SortConstraints(Constraints, NbEq);
} while (ImplicitEqualities(Constraints, NbEq));
if (POL_ISSET(NbMaxRays, POL_INTEGER))
value_clear(tmp);
Pol = Polyhedron_Alloc(Dimension-1, Constraints->NbRows - (NbEq-Rank), 0);
if (Rank > 0)
Vector_Copy(Constraints->p[0], Pol->Constraint[0],
Rank * Constraints->NbColumns);
if (Constraints->NbRows > NbEq)
Vector_Copy(Constraints->p[NbEq], Pol->Constraint[Rank],
(Constraints->NbRows - NbEq) * Constraints->NbColumns);
Pol->NbEq = Rank;
/* Make sure nobody accesses the rays by accident */
Pol->Ray = 0;
F_SET(Pol, POL_VALID | POL_INEQUALITIES);
return Pol;
}
if (Dimension > NbMaxRays)
NbMaxRays = Dimension;
/*
* Rather than adding a 'positivity constraint', it is better to
* initialize the lineality space with line in each of the index
* dimensions, but no line in the lambda dimension. Then initialize
* the ray space with an origin at 0. This is what you get anyway,
* after the positivity constraint has been processed by Chernikova
* function.
*/
/* Allocate and initialize the Ray Space */
Ray = Matrix_Alloc(NbMaxRays, Dimension+1);
if(!Ray) {
errormsg1("Constraints2Polyhedron","outofmem","out of memory space");
return 0;
}
Vector_Set(Ray->p_Init,0, NbMaxRays * (Dimension+1));
for (i=0; i<Dimension; i++) {
/* Ray->p[i][i+1] = 1 */
value_set_si(Ray->p[i][i+1],1);
}
/* Ray->p[Dimension-1][0] = 1 : mark for ray */
value_set_si(Ray->p[Dimension-1][0],1);
Ray->NbRows = Dimension;
/* Initialize the Sat Matrix */
nbcolumns = (Constraints->NbRows - 1)/(sizeof(int)*8) + 1;
Sat = SMAlloc(NbMaxRays, nbcolumns);
SMVector_Init(Sat->p_init,Dimension*nbcolumns);
Sat->NbRows = Dimension;
CATCH(any_exception_error) {
/* In case of overflow, free the allocated memory and forward. */
if (Sat) SMFree(&Sat);
if (Ray) Matrix_Free(Ray);
if (Pol) Polyhedron_Free(Pol);
RETHROW();
}
TRY {
/* Create ray matrix 'Ray' from constraint matrix 'Constraints' */
Chernikova(Constraints,Ray,Sat,Dimension-1,NbMaxRays,0,0);
#ifdef POLY_DEBUG
fprintf(stderr, "[constraints2polyhedron]\nConstraints = ");
Matrix_Print(stderr,0,Constraints);
fprintf(stderr, "\nRay = ");
Matrix_Print(stderr,0,Ray);
fprintf(stderr, "\nSat = ");
SMPrint(Sat);
#endif
/* Remove the redundant constraints and create the polyhedron */
Pol = Remove_Redundants(Constraints,Ray,Sat,0);
} /* end of TRY */
UNCATCH(any_exception_error);
#ifdef POLY_DEBUG
fprintf(stderr, "\nPol = ");
Polyhedron_Print(stderr,"%4d",Pol);
#endif
SMFree(&Sat), Sat = NULL;
Matrix_Free(Ray), Ray = NULL;
return Pol;
} /* Constraints2Polyhedron */
#undef POLY_DEBUG
/*
* Given a polyhedron 'Pol', return a matrix of constraints.
*/
Matrix *Polyhedron2Constraints(Polyhedron *Pol) {
Matrix *Mat;
unsigned NbConstraints,Dimension;
POL_ENSURE_INEQUALITIES(Pol);
NbConstraints = Pol->NbConstraints;
Dimension = Pol->Dimension+2;
Mat = Matrix_Alloc(NbConstraints,Dimension);
if(!Mat) {
errormsg1("Polyhedron2Constraints", "outofmem", "out of memory space");
return 0;
}
Vector_Copy(Pol->Constraint[0],Mat->p_Init,NbConstraints * Dimension);
return Mat;
} /* Polyhedron2Constraints */
/**
Given a matrix of rays 'Ray', create and return a polyhedron.
@param Ray Rays (may be modified!)
@param NbMaxConstrs Estimated number of constraints in the polyhedron.
@return newly allocated Polyhedron
*/
Polyhedron *Rays2Polyhedron(Matrix *Ray,unsigned NbMaxConstrs) {
Polyhedron *Pol = NULL;
Matrix *Mat = NULL;
SatMatrix *Sat = NULL, *SatTranspose = NULL;
unsigned Dimension, nbcolumns;
int i;
Dimension = Ray->NbColumns-1; /* Homogeneous Dimension */
Sat = NULL;
SatTranspose = NULL;
Mat = NULL;
if (Ray->NbRows==0) {
/* If there is no ray in the matrix 'Ray', return an empty polyhedron */
Pol = Empty_Polyhedron(Dimension-1);
return(Pol);
}
/* Ignore for now */
if (POL_ISSET(NbMaxConstrs, POL_NO_DUAL))
NbMaxConstrs = 0;
if (Dimension > NbMaxConstrs)
NbMaxConstrs = Dimension;
/* Allocate space for constraint matrix 'Mat' */
Mat = Matrix_Alloc(NbMaxConstrs,Dimension+1);
if(!Mat) {
errormsg1("Rays2Polyhedron","outofmem","out of memory space");
return 0;
}
/* Initialize the constraint matrix 'Mat' */
Vector_Set(Mat->p_Init,0,NbMaxConstrs * (Dimension+1));
for (i=0; i<Dimension; i++) {
/* Mat->p[i][i+1]=1 */
value_set_si(Mat->p[i][i+1],1);
}
/* Allocate and assign the saturation matrix. Remember we are using a */
/* transposed saturation matrix referenced by (constraint,ray) pair. */
Mat->NbRows = Dimension;
nbcolumns = (Ray->NbRows -1)/(sizeof(int)*8) + 1;
SatTranspose = SMAlloc(NbMaxConstrs,nbcolumns);
SMVector_Init(SatTranspose->p[0],Dimension * nbcolumns);
SatTranspose->NbRows = Dimension;
#ifdef POLY_DEBUG
fprintf(stderr, "[ray2polyhedron: Before]\nRay = ");
Matrix_Print(stderr,0,Ray);
fprintf(stderr, "\nConstraints = ");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nSatTranspose = ");
SMPrint (SatTranspose);
#endif
CATCH(any_exception_error) {
/* In case of overflow, free the allocated memory before forwarding
* the exception.
*/
if (SatTranspose) SMFree(&SatTranspose);
if (Sat) SMFree(&Sat);
if (Mat) Matrix_Free(Mat);
if (Pol) Polyhedron_Free(Pol);
RETHROW();
}
TRY {
/* Create constraint matrix 'Mat' from ray matrix 'Ray' */
Chernikova(Ray,Mat,SatTranspose,Dimension,NbMaxConstrs,0,1);
#ifdef POLY_DEBUG
fprintf(stderr, "[ray2polyhedron: After]\nRay = ");
Matrix_Print(stderr,0,Ray);
fprintf(stderr, "\nConstraints = ");
Matrix_Print(stderr,0,Mat);
fprintf(stderr, "\nSatTranspose = ");
SMPrint (SatTranspose);
#endif
/* Transform the saturation matrix 'SatTranspose' in the standard */
/* format, that is, ray X constraint format. */
Sat = TransformSat(Mat,Ray,SatTranspose);
#ifdef POLY_DEBUG
fprintf(stderr, "\nSat =");
SMPrint(Sat);
#endif
SMFree(&SatTranspose), SatTranspose = NULL;
/* Remove redundant rays from the ray matrix 'Ray' */
Pol = Remove_Redundants(Mat,Ray,Sat,0);
} /* of TRY */
UNCATCH(any_exception_error);
#ifdef POLY_DEBUG
fprintf(stderr, "\nPol = ");
Polyhedron_Print(stderr,"%4d",Pol);
#endif
SMFree(&Sat);
Matrix_Free(Mat);
return Pol;
} /* Rays2Polyhedron */
/*
* Compute the dual representation if P only contains one representation.
* Currently only handles the case where only the equalities are known.
*/
void Polyhedron_Compute_Dual(Polyhedron *P)
{
if (!F_ISSET(P, POL_VALID))
return;
if (F_ISSET(P, POL_FACETS | POL_VERTICES))
return;
if (F_ISSET(P, POL_INEQUALITIES)) {
Matrix M;
Polyhedron tmp, *Q;
/* Pretend P is a Matrix for a second */
M.NbRows = P->NbConstraints;
M.NbColumns = P->Dimension+2;
M.p_Init = P->p_Init;
M.p = P->Constraint;
Q = Constraints2Polyhedron(&M, 0);
/* Switch contents of P and Q ... */
tmp = *Q;
*Q = *P;
*P = tmp;
/* ... but keep the next pointer */
P->next = Q->next;
Polyhedron_Free(Q);
return;
}
/* other cases not handled yet */
assert(0);
}
/*
* Build a saturation matrix from constraint matrix 'Mat' and ray matrix
* 'Ray'. Only 'NbConstraints' constraint of matrix 'Mat' are considered
* in creating the saturation matrix. 'NbMaxRays' is the maximum number
* of rows (rays) allowed in the saturation matrix.
* Vin100's stuff, for the polyparam vertices to work.
*/
static SatMatrix *BuildSat(Matrix *Mat,Matrix *Ray,unsigned NbConstraints,unsigned NbMaxRays) {
SatMatrix *Sat = NULL;
int i, j, k, jx;
Value *p1, *p2, *p3;
unsigned Dimension, NbRay, bx, nbcolumns;
CATCH(any_exception_error) {
if (Sat)
SMFree(&Sat);
RETHROW();
}
TRY {
NbRay = Ray->NbRows;
Dimension = Mat->NbColumns-1; /* Homogeneous Dimension */
/* Build the Sat matrix */
nbcolumns = (Mat->NbRows - 1)/(sizeof(int)*8) + 1;
Sat = SMAlloc(NbMaxRays,nbcolumns);
Sat->NbRows = NbRay;
SMVector_Init(Sat->p_init, nbcolumns * NbRay);
jx=0; bx=MSB;
for (k=0; k<NbConstraints; k++) {
for (i=0; i<NbRay; i++) {
/* Compute the dot product of ray(i) and constraint(k) and */
/* store in the status element of ray(i). */
p1 = Ray->p[i]+1;
p2 = Mat->p[k]+1;
p3 = Ray->p[i];
value_set_si(*p3,0);
for (j=0; j<Dimension; j++) {
value_addmul(*p3, *p1, *p2);
p1++; p2++;
}
}
for (j=0; j<NbRay; j++) {
/* Set 1 in the saturation matrix if the ray doesn't saturate */
/* the constraint, otherwise the entry is 0. */
if (value_notzero_p(Ray->p[j][0]))
Sat->p[j][jx]|=bx;
}
NEXT(jx, bx);
}
} /* end of TRY */
UNCATCH(any_exception_error);
return Sat;
} /* BuildSat */
/*
* Add 'Nbconstraints' new constraints to polyhedron 'Pol'. Constraints are
* pointed by 'Con' and the maximum allowed rays in the new polyhedron is
* 'NbMaxRays'.
*/
Polyhedron *AddConstraints(Value *Con,unsigned NbConstraints,Polyhedron *Pol,unsigned NbMaxRays) {
Polyhedron *NewPol = NULL;
Matrix *Mat = NULL, *Ray = NULL;
SatMatrix *Sat = NULL;
unsigned NbRay, NbCon, Dimension;
if (NbConstraints == 0)
return Polyhedron_Copy(Pol);
POL_ENSURE_INEQUALITIES(Pol);
Dimension = Pol->Dimension + 2; /* Homogeneous Dimension + Status */
if (POL_ISSET(NbMaxRays, POL_NO_DUAL)) {
NbCon = Pol->NbConstraints + NbConstraints;
Mat = Matrix_Alloc(NbCon, Dimension);
if (!Mat) {
errormsg1("AddConstraints", "outofmem", "out of memory space");
return NULL;
}
Vector_Copy(Pol->Constraint[0], Mat->p[0], Pol->NbConstraints * Dimension);
Vector_Copy(Con, Mat->p[Pol->NbConstraints], NbConstraints * Dimension);
NewPol = Constraints2Polyhedron(Mat, NbMaxRays);
Matrix_Free(Mat);
return NewPol;
}
POL_ENSURE_VERTICES(Pol);
CATCH(any_exception_error) {
if (NewPol) Polyhedron_Free(NewPol);
if (Mat) Matrix_Free(Mat);
if (Ray) Matrix_Free(Ray);
if (Sat) SMFree(&Sat);
RETHROW();
}
TRY {
NbRay = Pol->NbRays;
NbCon = Pol->NbConstraints + NbConstraints;
if (NbRay > NbMaxRays)
NbMaxRays = NbRay;
Mat = Matrix_Alloc(NbCon, Dimension);
if(!Mat) {
errormsg1("AddConstraints", "outofmem", "out of memory space");
UNCATCH(any_exception_error);
return 0;
}
/* Copy constraints of polyhedron 'Pol' to matrix 'Mat' */
Vector_Copy(Pol->Constraint[0], Mat->p[0], Pol->NbConstraints * Dimension);
/* Add the new constraints pointed by 'Con' to matrix 'Mat' */
Vector_Copy(Con, Mat->p[Pol->NbConstraints], NbConstraints * Dimension);
/* Allocate space for ray matrix 'Ray' */
Ray = Matrix_Alloc(NbMaxRays, Dimension);
if(!Ray) {
errormsg1("AddConstraints", "outofmem", "out of memory space");
UNCATCH(any_exception_error);
return 0;
}
Ray->NbRows = NbRay;
/* Copy rays of polyhedron 'Pol' to matrix 'Ray' */
if (NbRay)
Vector_Copy(Pol->Ray[0], Ray->p[0], NbRay * Dimension);
/* Create the saturation matrix 'Sat' from constraint matrix 'Mat' and */
/* ray matrix 'Ray' . */
Sat = BuildSat(Mat, Ray, Pol->NbConstraints, NbMaxRays);
/* Create the ray matrix 'Ray' from the constraint matrix 'Mat' */
Pol_status = Chernikova(Mat, Ray, Sat, Pol->NbBid, NbMaxRays, Pol->NbConstraints,0);
/* Remove redundant constraints from matrix 'Mat' */
NewPol = Remove_Redundants(Mat, Ray, Sat, 0);
} /* end of TRY */
UNCATCH(any_exception_error);
SMFree(&Sat);
Matrix_Free(Ray);
Matrix_Free(Mat);
return NewPol;
} /* AddConstraints */
/*
* Return 1 if 'Pol1' includes (covers) 'Pol2', 0 otherwise.
* Polyhedron 'A' includes polyhedron 'B' if the rays of 'B' saturate
* the equalities and verify the inequalities of 'A'. Both 'Pol1' and
* 'Pol2' have same dimensions.
*/
int PolyhedronIncludes(Polyhedron *Pol1,Polyhedron *Pol2) {
int Dimension = Pol1->Dimension + 1; /* Homogenous Dimension */
int i, j, k;
Value *p1, *p2, p3;
POL_ENSURE_FACETS(Pol1);
POL_ENSURE_VERTICES(Pol1);
POL_ENSURE_FACETS(Pol2);
POL_ENSURE_VERTICES(Pol2);
value_init(p3);
for (k=0; k<Pol1->NbConstraints; k++) {
for (i=0;i<Pol2->NbRays;i++) {
/* Compute the dot product of ray(i) and constraint(k) and store in p3 */
p1 = Pol2->Ray[i]+1;
p2 = Pol1->Constraint[k]+1;
value_set_si(p3,0);
for(j=0;j<Dimension;j++) {
value_addmul(p3, *p1,*p2);
p1++; p2++;
}
/* If (p3 < 0) or (p3 > 0 and (constraint(k) is equality
or ray(i) is a line)), return 0 */
if(value_neg_p(p3) ||
(value_notzero_p(p3)
&& (value_zero_p(Pol1->Constraint[k][0]) || (value_zero_p(Pol2->Ray[i][0])) ) )) {
value_clear(p3);
return 0;
}
}
}
value_clear(p3);
return 1;
} /* PolyhedronIncludes */
/*
* Add Polyhedron 'Pol' to polhedral domain 'PolDomain'. If 'Pol' covers
* some polyhedron in the domain 'PolDomain', it is removed from the list.
* On the other hand if some polyhedron in the domain covers polyhedron
* 'Pol' then 'Pol' is not included in the domain.
*/
Polyhedron *AddPolyToDomain(Polyhedron *Pol,Polyhedron *PolDomain) {
Polyhedron *p, *pnext, *p_domain_end = (Polyhedron *) 0;
int Redundant;
if (!Pol)
return PolDomain;
if (!PolDomain)
return Pol;
POL_ENSURE_FACETS(Pol);
POL_ENSURE_VERTICES(Pol);
/* Check for emptiness of polyhedron 'Pol' */
if (emptyQ(Pol)) {
Polyhedron_Free(Pol);
return PolDomain;
}
POL_ENSURE_FACETS(PolDomain);
POL_ENSURE_VERTICES(PolDomain);
/* Check for emptiness of polyhedral domain 'PolDomain' */
if (emptyQ(PolDomain)) {
Polyhedron_Free(PolDomain);
return Pol;
}
/* Test 'Pol' against the domain 'PolDomain' */
Redundant = 0;
for (p=PolDomain,PolDomain=(Polyhedron *)0; p; p=pnext) {
/* If 'Pol' covers 'p' */
if (PolyhedronIncludes(Pol, p))
{
/* free p */
pnext = p->next;
Polyhedron_Free( p );
continue;
}
/* Add polyhedron p to the new domain list */
if (!PolDomain) PolDomain = p; else p_domain_end->next = p;
p_domain_end = p;
/* If p covers Pol */
if (PolyhedronIncludes(p,Pol)) {
Redundant = 1;
break;
}
pnext = p->next;
}
if (!Redundant) {
/* The whole list has been checked. Add new polyhedron 'Pol' to the */
/* new domain list. */
if (!PolDomain) PolDomain = Pol; else p_domain_end->next = Pol;
}
else {
/* The rest of the list is just inherited from p */
Polyhedron_Free(Pol);
}
return PolDomain;
} /* AddPolyToDomain */
/*
* Given a polyhedra 'Pol' and a single constraint 'Con' and an integer 'Pass'
* whose value ranges from 0 to 3, add the inverse of constraint 'Con' to the
* constraint set of 'Pol' and return the new polyhedron. 'NbMaxRays' is the
* maximum allowed rays in the new generated polyhedron.
* If Pass == 0, add ( -constraint -1) >= 0
* If Pass == 1, add ( +constraint -1) >= 0
* If Pass == 2, add ( -constraint ) >= 0
* If Pass == 3, add ( +constraint ) >= 0
*/
Polyhedron *SubConstraint(Value *Con,Polyhedron *Pol,unsigned NbMaxRays,int Pass) {
Polyhedron *NewPol = NULL;
Matrix *Mat = NULL, *Ray = NULL;
SatMatrix *Sat = NULL;
unsigned NbRay, NbCon, NbEle1, Dimension;
int i;
POL_ENSURE_FACETS(Pol);
POL_ENSURE_VERTICES(Pol);
CATCH(any_exception_error) {
if (NewPol) Polyhedron_Free(NewPol);
if (Mat) Matrix_Free(Mat);
if (Ray) Matrix_Free(Ray);
if (Sat) SMFree(&Sat);
RETHROW();
}
TRY {
/* If 'Con' is the positivity constraint, return Null */
Dimension = Pol->Dimension+1; /* Homogeneous Dimension */
for (i=1; i<Dimension; i++)
if (value_notzero_p(Con[i])) break;
if (i==Dimension) {
UNCATCH(any_exception_error);
return (Polyhedron *) 0;
}
NbRay = Pol->NbRays;
NbCon = Pol->NbConstraints;
Dimension = Pol->Dimension+2; /* Homogeneous Dimension + Status */
NbEle1 = NbCon * Dimension;
/* Ignore for now */
if (POL_ISSET(NbMaxRays, POL_NO_DUAL))
NbMaxRays = 0;
if (NbRay > NbMaxRays)
NbMaxRays = NbRay;
Mat = Matrix_Alloc(NbCon + 1, Dimension);
if(!Mat) {
errormsg1("SubConstraint", "outofmem", "out of memory space");
UNCATCH(any_exception_error);
return 0;
}
/* Set the constraints of Pol */
Vector_Copy(Pol->Constraint[0], Mat->p[0], NbEle1);
/* Add the new constraint */
value_set_si(Mat->p[NbCon][0],1);
if (!(Pass&1))
for(i=1; i<Dimension; i++)
value_oppose(Mat->p[NbCon][i],Con[i]);
else
for(i=1; i<Dimension; i++)
value_assign(Mat->p[NbCon][i],Con[i]);
if (!(Pass&2))
value_decrement(Mat->p[NbCon][Dimension-1],Mat->p[NbCon][Dimension-1]);
/* Allocate the ray matrix. */
Ray = Matrix_Alloc(NbMaxRays, Dimension);
if(!Ray) {
errormsg1("SubConstraint", "outofmem", "out of memory space");
UNCATCH(any_exception_error);
return 0;
}
/* Initialize the ray matrix with the rays of polyhedron 'Pol' */
Ray->NbRows = NbRay;
if (NbRay)
Vector_Copy(Pol->Ray[0], Ray->p[0], NbRay * Dimension);
/* Create the saturation matrix from the constraint matrix 'mat' and */
/* ray matrix 'Ray'. */
Sat = BuildSat(Mat, Ray, NbCon, NbMaxRays);
/* Create the ray matrix 'Ray' from consraint matrix 'Mat' */
Pol_status = Chernikova(Mat, Ray, Sat, Pol->NbBid, NbMaxRays, NbCon,0);
/* Remove redundant constraints from matrix 'Mat' */
NewPol = Remove_Redundants(Mat, Ray, Sat, 0);
} /* end of TRY */
UNCATCH(any_exception_error);
SMFree(&Sat);
Matrix_Free(Ray);
Matrix_Free(Mat);
return NewPol;
} /* SubConstraint */
/*
* Return the intersection of two polyhedral domains 'Pol1' and 'Pol2'.
* The maximum allowed rays in the new polyhedron generated is 'NbMaxRays'.
*/
Polyhedron *DomainIntersection(Polyhedron *Pol1,Polyhedron *Pol2,unsigned NbMaxRays) {
Polyhedron *p1, *p2, *p3, *d;
if (!Pol1 || !Pol2) return (Polyhedron*) 0;
if (Pol1->Dimension != Pol2->Dimension) {
errormsg1( "DomainIntersection", "diffdim",
"operation on different dimensions");
return (Polyhedron*) 0;
}
/* For every polyhedron pair (p1,p2) where p1 belongs to domain Pol1 and */
/* p2 belongs to domain Pol2, compute the intersection and add it to the */
/* new domain 'd'. */
d = (Polyhedron *)0;
for (p1=Pol1; p1; p1=p1->next) {
for (p2=Pol2; p2; p2=p2->next) {
p3 = AddConstraints(p2->Constraint[0],
p2->NbConstraints, p1, NbMaxRays);
d = AddPolyToDomain(p3,d);
}
}
if (!d)
return Empty_Polyhedron(Pol1->Dimension);
else
return d;
} /* DomainIntersection */
/*
* Given a polyhedron 'Pol', return a matrix of rays.
*/
Matrix *Polyhedron2Rays(Polyhedron *Pol) {
Matrix *Ray;
unsigned NbRays, Dimension;
POL_ENSURE_POINTS(Pol);
NbRays = Pol->NbRays;
Dimension = Pol->Dimension+2; /* Homogeneous Dimension + Status */
Ray = Matrix_Alloc(NbRays, Dimension);
if(!Ray) {
errormsg1("Polyhedron2Rays", "outofmem", "out of memory space");
return 0;
}
Vector_Copy(Pol->Ray[0], Ray->p_Init, NbRays*Dimension);
return Ray;
} /* Polyhedron2Rays */
/*
* Add 'NbAddedRays' rays to polyhedron 'Pol'. Rays are pointed by 'AddedRays'
* and the maximum allowed constraints in the new polyhedron is 'NbMaxConstrs'.
*/
Polyhedron *AddRays(Value *AddedRays,unsigned NbAddedRays,Polyhedron *Pol,unsigned NbMaxConstrs) {
Polyhedron *NewPol = NULL;
Matrix *Mat = NULL, *Ray = NULL;
SatMatrix *Sat = NULL, *SatTranspose = NULL;
unsigned NbCon, NbRay,NbEle1, Dimension;
POL_ENSURE_FACETS(Pol);
POL_ENSURE_VERTICES(Pol);
CATCH(any_exception_error) {
if (NewPol) Polyhedron_Free(NewPol);
if (Mat) Matrix_Free(Mat);
if (Ray) Matrix_Free(Ray);
if (Sat) SMFree(&Sat);
if (SatTranspose) SMFree(&SatTranspose);
RETHROW();
}
TRY {
NbCon = Pol->NbConstraints;
NbRay = Pol->NbRays;
Dimension = Pol->Dimension + 2; /* Homogeneous Dimension + Status */
NbEle1 = NbRay * Dimension;
Ray = Matrix_Alloc(NbAddedRays + NbRay, Dimension);
if(!Ray) {
errormsg1("AddRays", "outofmem", "out of memory space");
UNCATCH(any_exception_error);
return 0;
}
/* Copy rays of polyhedron 'Pol' to matrix 'Ray' */
if (NbRay)
Vector_Copy(Pol->Ray[0], Ray->p_Init, NbEle1);
/* Add the new rays pointed by 'AddedRays' to matrix 'Ray' */
Vector_Copy(AddedRays, Ray->p_Init+NbEle1, NbAddedRays * Dimension);
/* Ignore for now */
if (POL_ISSET(NbMaxConstrs, POL_NO_DUAL))
NbMaxConstrs = 0;
/* We need at least NbCon rows */
if (NbMaxConstrs < NbCon)
NbMaxConstrs = NbCon;
/* Allocate space for constraint matrix 'Mat' */
Mat = Matrix_Alloc(NbMaxConstrs, Dimension);
if(!Mat) {
errormsg1("AddRays", "outofmem", "out of memory space");
UNCATCH(any_exception_error);
return 0;
}
Mat->NbRows = NbCon;
/* Copy constraints of polyhedron 'Pol' to matrix 'Mat' */
Vector_Copy(Pol->Constraint[0], Mat->p_Init, NbCon*Dimension);
/* Create the saturation matrix 'SatTranspose' from constraint matrix */
/* 'Mat' and ray matrix 'Ray'. Remember the saturation matrix is */
/* referenced by (constraint,ray) pair */
SatTranspose = BuildSat(Ray, Mat, NbRay, NbMaxConstrs);
/* Create the constraint matrix 'Mat' from the ray matrix 'Ray' */
Pol_status = Chernikova(Ray, Mat, SatTranspose, Pol->NbEq, NbMaxConstrs, NbRay,1);
/* Transform the saturation matrix 'SatTranspose' in the standard format */
/* , that is, (ray X constraint) format. */
Sat = TransformSat(Mat, Ray, SatTranspose);
SMFree(&SatTranspose), SatTranspose = NULL;
/* Remove redundant rays from the ray matrix 'Ray' */
NewPol = Remove_Redundants(Mat, Ray, Sat, 0);
SMFree(&Sat), Sat = NULL;
Matrix_Free(Mat), Mat = NULL;
Matrix_Free(Ray), Ray = NULL;
} /* end of TRY */
UNCATCH(any_exception_error);
return NewPol;
} /* AddRays */
/*
* Add rays pointed by 'Ray' to each and every polyhedron in the polyhedral
* domain 'Pol'. 'NbMaxConstrs' is maximum allowed constraints in the
* constraint set of a polyhedron.
*/
Polyhedron *DomainAddRays(Polyhedron *Pol,Matrix *Ray,unsigned NbMaxConstrs) {
Polyhedron *PolA, *PolEndA, *p1, *p2, *p3;
int Redundant;
if (!Pol) return (Polyhedron*) 0;
if (!Ray || Ray->NbRows == 0)
return Domain_Copy(Pol);
if (Pol->Dimension != Ray->NbColumns-2) {
errormsg1(
"DomainAddRays", "diffdim", "operation on different dimensions");
return (Polyhedron*) 0;
}
/* Copy Pol to PolA */
PolA = PolEndA = (Polyhedron *)0;
for (p1=Pol; p1; p1=p1->next) {
p3 = AddRays(Ray->p[0], Ray->NbRows, p1, NbMaxConstrs);
/* Does any component of PolA cover 'p3' ? */
Redundant = 0;
for (p2=PolA; p2; p2=p2->next) {
if (PolyhedronIncludes(p2, p3)) { /* If p2 covers p3 */
Redundant = 1;
break;
}
}
/* If the new polyheron is not redundant, add it ('p3') to the list */
if (Redundant)
Polyhedron_Free(p3);
else {
if (!PolEndA)
PolEndA = PolA = p3;
else {
PolEndA->next = p3;
PolEndA = PolEndA->next;
}
}
}
return PolA;
} /* DomainAddRays */
/*
* Create a copy of the polyhedron 'Pol'
*/
Polyhedron *Polyhedron_Copy(Polyhedron *Pol) {
Polyhedron *Pol1;
if (!Pol) return (Polyhedron *)0;
/* Allocate space for the new polyhedron */
Pol1 = Polyhedron_Alloc(Pol->Dimension, Pol->NbConstraints, Pol->NbRays);
if (!Pol1) {
errormsg1("Polyhedron_Copy", "outofmem", "out of memory space");
return 0;
}
if( Pol->NbConstraints )
Vector_Copy(Pol->Constraint[0], Pol1->Constraint[0],
Pol->NbConstraints*(Pol->Dimension+2));
if( Pol->NbRays )
Vector_Copy(Pol->Ray[0], Pol1->Ray[0],
Pol->NbRays*(Pol->Dimension+2));
Pol1->NbBid = Pol->NbBid;
Pol1->NbEq = Pol->NbEq;
Pol1->flags = Pol->flags;
return Pol1;
} /* Polyhedron_Copy */
/*
* Create a copy of a polyhedral domain.
*/
Polyhedron *Domain_Copy(Polyhedron *Pol) {
Polyhedron *Pol1;
if (!Pol) return (Polyhedron *) 0;
Pol1 = Polyhedron_Copy(Pol);
if (Pol->next) Pol1->next = Domain_Copy(Pol->next);
return Pol1;
} /* Domain_Copy */
/*
* Given constraint number 'k' of a polyhedron, and an array 'Filter' to store
* the non-redundant constraints of the polyhedron in bit-wise notation, and
* a Matrix 'Sat', add the constraint 'k' in 'Filter' array. tmpR[i] stores the
* number of constraints, other than those in 'Filter', which ray(i) saturates
* or verifies. In case, ray(i) does not saturate or verify a constraint in
* array 'Filter', it is assigned to -1. Similarly, tmpC[j] stores the number
* of rays which constraint(j), if it doesn't belong to Filter, saturates or
* verifies. If constraint(j) belongs to 'Filter', then tmpC[j] is assigned to
* -1. 'NbConstraints' is the number of constraints in the constraint matrix of
* the polyhedron.
* NOTE: (1) 'Sat' is not the saturation matrix of the polyhedron. In fact,
* entry in 'Sat' is set to 1 if ray(i) of polyhedron1 verifies or
* saturates the constraint(j) of polyhedron2 and otherwise it is set
* to 0. So here the difference with saturation matrix is in terms
* definition and entries(1<->0) of the matrix 'Sat'.
*
* ALGORITHM:->
* (1) Include constraint(k) in array 'Filter'.
* (2) Set tmpC[k] to -1.
* (3) For all ray(i) {
* If ray(i) saturates or verifies constraint(k) then --(tmpR[i])
* Else {
* Discard ray(i) by assigning tmpR[i] = -1
* Decrement tmpC[j] for all constraint(j) not in array 'Filter'.
* }
* }
*/
static void addToFilter(int k, unsigned *Filter, SatMatrix *Sat,Value *tmpR,Value *tmpC,int NbRays,int NbConstraints) {
int kj, i,j, jx;
unsigned kb, bx;
/* Remove constraint k */
kj = k/WSIZE; kb = MSB; kb >>= k%WSIZE;
Filter[kj]|=kb;
value_set_si(tmpC[k],-1);
/* Remove rays excluded by constraint k */
for(i=0; i<NbRays; i++)
if (value_posz_p(tmpR[i])) {
if (Sat->p[i][kj]&kb)
value_decrement(tmpR[i],tmpR[i]); /* adjust included ray */
else {
/* Constraint k excludes ray i -- delete ray i */
value_set_si(tmpR[i],-1);
/* Adjust non-deleted constraints */
jx=0; bx=MSB;
for(j=0; j<NbConstraints; j++) {
if (value_posz_p(tmpC[j]) && (Sat->p[i][jx]&bx) )
value_decrement(tmpC[j],tmpC[j]);
NEXT(jx,bx);
}
}
}
} /* addToFilter */
/*
* Given polyhedra 'P1' and 'P2' such that their intersection is an empty
* polyhedron, find the minimal set of constraints of 'P1' which contradict
* all of the constraints of 'P2'. This is believed to be an NP-hard problem
* and so a heuristic is employed to solve it in worst case. The heuristic is
* to select in every turn that constraint of 'P1' which excludes most rays of
* 'P2'. A bit in the binary format of an element of array 'Filter' is set to
* 1 if the corresponding constraint is to be included in the minimal set of
* constraints otherwise it is set to 0.
*/
static void FindSimple(Polyhedron *P1,Polyhedron *P2,unsigned *Filter,unsigned NbMaxRays) {
Matrix *Mat = NULL;
SatMatrix *Sat = NULL;
int i, j, k, jx, found;
Value *p1, *p2, p3;
unsigned Dimension, NbRays, NbConstraints, bx, nc;
Value NbConstraintsLeft, tmp;
Value *tmpC = NULL, *tmpR = NULL;
Polyhedron *Pol = NULL, *Pol2 = NULL;
/* Initialize all the 'Value' variables */
value_init(p3); value_init(NbConstraintsLeft);
value_init(tmp);
CATCH(any_exception_error) {
if (tmpC) free(tmpC);
if (tmpR) free(tmpR);
if (Mat) Matrix_Free(Mat);
if (Sat) SMFree(&Sat);
if (Pol2 && Pol2!=P2) Polyhedron_Free(Pol2);
if (Pol && Pol!=Pol2 && Pol!=P2) Polyhedron_Free(Pol);
/* Clear all the 'Value' variables */
value_clear(p3); value_clear(NbConstraintsLeft);
value_clear(tmp);
RETHROW();
}
TRY {
Dimension = P1->Dimension+2; /* status + homogeneous Dimension */
Mat = Matrix_Alloc(P1->NbConstraints, Dimension);
if(!Mat) {
errormsg1("FindSimple", "outofmem", "out of memory space");
UNCATCH(any_exception_error);
/* Clear all the 'Value' variables */
value_clear(p3); value_clear(NbConstraintsLeft); value_clear(tmp);
return;
}
/* Post constraints in P1 already included by Filter */
jx = 0; bx = MSB; Mat->NbRows=0;
value_set_si(NbConstraintsLeft,0);
for (k=0; k<P1->NbConstraints; k++) {
if (Filter[jx]&bx) {
Vector_Copy(P1->Constraint[k], Mat->p[Mat->NbRows], Dimension);
Mat->NbRows++;
}
else
value_increment(NbConstraintsLeft,NbConstraintsLeft);
NEXT(jx,bx);
}
Pol2 = P2;
for (;;) {
if (Mat->NbRows==0)
Pol = Polyhedron_Copy(Pol2);
else {
Pol = AddConstraints(Mat->p_Init, Mat->NbRows, Pol2, NbMaxRays);
if (Pol2 != P2) Polyhedron_Free(Pol2), Pol2 = NULL;
}
if (emptyQ(Pol)) {
Matrix_Free(Mat), Mat = NULL;
Polyhedron_Free(Pol), Pol = NULL;
UNCATCH(any_exception_error);
/* Clear all the 'Value' variables */
value_clear(p3); value_clear(NbConstraintsLeft); value_clear(tmp);
return;
}
Mat->NbRows = 0; /* Reset Mat */
Pol2 = Pol;
/* Its not enough-- find some more constraints */
Dimension = Pol->Dimension+1; /* homogeneous Dimension */
NbRays = Pol->NbRays;
NbConstraints = P1->NbConstraints;
tmpR = (Value *)malloc(NbRays*sizeof(Value));
if(!tmpR) {
errormsg1("FindSimple", "outofmem", "out of memory space");
UNCATCH(any_exception_error);
/* Clear all the 'Value' variables */
value_clear(p3); value_clear(NbConstraintsLeft); value_clear(tmp);
return;
}
for(i=0;i<NbRays;i++)
value_init(tmpR[i]);
tmpC = (Value *)malloc(NbConstraints*sizeof(Value));
if(!tmpC) {
errormsg1("FindSimple", "outofmem", "out of memory space");
UNCATCH(any_exception_error);
/* Clear all the 'Value' variables */
value_clear(p3); value_clear(NbConstraintsLeft);
for(i=0;i<NbRays;i++)
value_clear(tmpR[i]);
free(tmpR);
return;
}
for(i=0;i<NbConstraints;i++)
value_init(tmpC[i]);
Vector_Set(tmpR,0,NbRays);
Vector_Set(tmpC,0,NbConstraints);
/* Build the Sat matrix */
nc = (NbConstraints - 1) / (sizeof(int)*8) + 1;
Sat = SMAlloc(NbRays, nc);
Sat->NbRows = NbRays;
SMVector_Init(Sat->p_init, nc*NbRays);
jx=0; bx=MSB;
for (k=0; k<NbConstraints; k++) {
if (Filter[jx]&bx)
value_set_si(tmpC[k],-1);
else
for (i=0; i<NbRays; i++) {
p1 = Pol->Ray[i]+1;
p2 = P1->Constraint[k]+1;
value_set_si(p3,0);
for (j=0; j<Dimension; j++) {
value_addmul(p3, *p1, *p2);
p1++; p2++;
}
if(value_zero_p(p3) ||
(value_pos_p(p3) && value_notzero_p(P1->Constraint[k][0]))) {
Sat->p[i][jx]|=bx; /* constraint includes ray, set flag */
value_increment(tmpR[i],tmpR[i]);
value_increment(tmpC[k],tmpC[k]);
}
}
NEXT(jx, bx);
}
do { /* find all of the essential constraints */
found = 0;
for(i=0; i<NbRays; i++)
if(value_posz_p(tmpR[i])) {
value_add_int(tmp,tmpR[i],1);
if(value_eq(tmp,NbConstraintsLeft)) {
/* Ray i is excluded by only one constraint... find it */
jx = 0; bx = MSB;
for(k=0; k<NbConstraints; k++) {
if(value_posz_p(tmpC[k]) && ((Sat->p[i][jx]&bx)==0)) {
addToFilter(k, Filter, Sat, tmpR, tmpC,
NbRays, NbConstraints);
Vector_Copy(P1->Constraint[k],
Mat->p[Mat->NbRows],Dimension+1);
Mat->NbRows++;
value_decrement(NbConstraintsLeft,NbConstraintsLeft);
found=1;
break;
}
NEXT(jx,bx);
}
break;
}
}
}
while (found);
if (!Mat->NbRows) { /* Well then, just use a stupid heuristic */
/* find the constraint which excludes the most */
Value cmax;
value_init(cmax);
#ifndef LINEAR_VALUE_IS_CHARS
value_set_si(cmax,(NbRays * NbConstraints+1));
#else
value_set_si(cmax,1);
#endif
j = -1;
for(k=0; k<NbConstraints; k++)
if (value_posz_p(tmpC[k])) {
if (value_gt(cmax,tmpC[k])) {
value_assign(cmax,tmpC[k]);
j = k;
}
}
value_clear(cmax);
if (j<0) {
errormsg1("DomSimplify","logerror","logic error");
}
else {
addToFilter(j, Filter, Sat, tmpR, tmpC, NbRays, NbConstraints);
Vector_Copy(P1->Constraint[j],Mat->p[Mat->NbRows],Dimension+1);
Mat->NbRows++;
value_decrement(NbConstraintsLeft,NbConstraintsLeft);
}
}
SMFree(&Sat), Sat = NULL;
free(tmpC), tmpC = NULL;
free(tmpR), tmpR = NULL;
}
} /* end of TRY */
/* Clear all the 'Value' variables */
value_clear(p3); value_clear(NbConstraintsLeft);
value_clear(tmp);
for(i=0;i<NbRays;i++)
value_clear(tmpR[i]);
for(i=0;i<NbRays;i++)
value_clear(tmpC[i]);
UNCATCH(any_exception_error);
} /* FindSimple */
/*
* Return 0 if the intersection of Pol1 and Pol2 is empty, otherwise return 1.
* If the intersection is non-empty, store the non-redundant constraints in
* 'Filter' array. If the intersection is empty then store the smallest set of
* constraints of 'Pol1' which on intersection with 'Pol2' gives empty set, in
* 'Filter' array. 'NbMaxRays' is the maximum allowed rays in the intersection
* of 'Pol1' and 'Pol2'.
*/
static int SimplifyConstraints(Polyhedron *Pol1,Polyhedron *Pol2,unsigned *Filter,unsigned NbMaxRays) {
Polyhedron *Pol = NULL;
Matrix *Mat = NULL, *Ray = NULL;
SatMatrix *Sat = NULL;
unsigned NbRay, NbCon, NbCon1, NbCon2, NbEle1, Dimension, notempty;
CATCH(any_exception_error) {
if (Pol) Polyhedron_Free(Pol);
if (Mat) Matrix_Free(Mat);
if (Ray) Matrix_Free(Ray);
if (Sat) SMFree(&Sat);
RETHROW();
}
TRY {
NbRay = Pol1->NbRays;
NbCon1 = Pol1->NbConstraints;
NbCon2 = Pol2->NbConstraints;
NbCon = NbCon1 + NbCon2;
Dimension = Pol1->Dimension+2; /* Homogeneous Dimension + Status */
NbEle1 = NbCon1*Dimension;
/* Ignore for now */
if (POL_ISSET(NbMaxRays, POL_NO_DUAL))
NbMaxRays = 0;
if (NbRay > NbMaxRays)
NbMaxRays = NbRay;
/* Allocate space for constraint matrix 'Mat' */
Mat = Matrix_Alloc(NbCon, Dimension);
if(!Mat) {
errormsg1("SimplifyConstraints", "outofmem", "out of memory space");
UNCATCH(any_exception_error);
return 0;
}
/* Copy constraints of 'Pol1' to matrix 'Mat' */
Vector_Copy(Pol1->Constraint[0], Mat->p_Init, NbEle1);
/* Add constraints of 'Pol2' to matrix 'Mat'*/
Vector_Copy(Pol2->Constraint[0], Mat->p_Init+NbEle1, NbCon2*Dimension);
/* Allocate space for ray matrix 'Ray' */
Ray = Matrix_Alloc(NbMaxRays, Dimension);
if(!Ray) {
errormsg1("SimplifyConstraints", "outofmem", "out of memory space");
UNCATCH(any_exception_error);
return 0;
}
Ray->NbRows = NbRay;
/* Copy rays of polyhedron 'Pol1' to matrix 'Ray' */
Vector_Copy(Pol1->Ray[0], Ray->p_Init, NbRay*Dimension);
/* Create saturation matrix from constraint matrix 'Mat' and ray matrix */
/* 'Ray'. */
Sat = BuildSat(Mat, Ray, NbCon1, NbMaxRays);
/* Create the ray matrix 'Ray' from the constraint matrix 'Mat' */
Pol_status = Chernikova(Mat, Ray, Sat, Pol1->NbBid, NbMaxRays, NbCon1,0);
/* Remove redundant constraints from the constraint matrix 'Mat' */
Pol = Remove_Redundants(Mat, Ray, Sat, Filter);
notempty = 1;
if (Filter && emptyQ(Pol)) {
notempty = 0;
FindSimple(Pol1, Pol2, Filter, NbMaxRays);
}
/* Polyhedron_Print(stderr,"%4d",Pol1); */
Polyhedron_Free(Pol), Pol = NULL;
SMFree(&Sat), Sat = NULL;
Matrix_Free(Ray), Ray = NULL;
Matrix_Free(Mat), Mat = NULL;
} /* end of TRY */
UNCATCH(any_exception_error);
return notempty;
} /* SimplifyConstraints */
/*
* Eliminate equations of Pol1 using equations of Pol2. Mark as needed,
* equations of Pol1 that are not eliminated. Or info into Filter vector.
*/
static void SimplifyEqualities(Polyhedron *Pol1, Polyhedron *Pol2, unsigned *Filter) {
int i,j;
unsigned ix, bx, NbEqn, NbEqn1, NbEqn2, NbEle2, Dimension;
Matrix *Mat;
NbEqn1 = Pol1->NbEq;
NbEqn2 = Pol2->NbEq;
NbEqn = NbEqn1 + NbEqn2;
Dimension = Pol1->Dimension+2; /* Homogeneous Dimension + Status */
NbEle2 = NbEqn2*Dimension;
Mat = Matrix_Alloc(NbEqn, Dimension);
if (!Mat) {
errormsg1("SimplifyEqualities", "outofmem", "out of memory space");
Pol_status = 1;
return;
}
/* Set the equalities of Pol2 */
Vector_Copy(Pol2->Constraint[0], Mat->p_Init, NbEle2);
/* Add the equalities of Pol1 */
Vector_Copy(Pol1->Constraint[0], Mat->p_Init+NbEle2, NbEqn1*Dimension);
Gauss(Mat, NbEqn2, Dimension-1);
ix = 0;
bx = MSB;
for (i=NbEqn2; i<NbEqn; i++) {
for (j=1; j<Dimension; j++) {
if (value_notzero_p(Mat->p[i][j])) {
/* If any coefficient of the equation is non-zero */
/* Set the filter bit for the equation */
Filter[ix] |= bx;
break;
}
}
NEXT(ix,bx);
}
Matrix_Free(Mat);
return;
} /* SimplifyEqualities */
/*
* Given two polyhedral domains 'Pol1' and 'Pol2', find the largest domain
* set (or the smallest list of non-redundant constraints), that when
* intersected with polyhedral domain 'Pol2' equals (Pol1)intersect(Pol2).
* The output is a polyhedral domain with the "redundant" constraints removed.
* 'NbMaxRays' is the maximium allowed rays in the new polyhedra.
*/
Polyhedron *DomainSimplify(Polyhedron *Pol1, Polyhedron *Pol2, unsigned NbMaxRays) {
Polyhedron *p1, *p2, *p3, *d;
unsigned k, jx, bx, nbentries, NbConstraints, Dimension, NbCon, empty;
unsigned *Filter;
Matrix *Constraints;
if (!Pol1 || !Pol2) return Pol1;
if (Pol1->Dimension != Pol2->Dimension) {
errormsg1("DomSimplify","diffdim","operation on different dimensions");
Pol_status = 1;
return 0;
}
POL_ENSURE_VERTICES(Pol1);
POL_ENSURE_VERTICES(Pol2);
if (emptyQ(Pol1)||emptyQ(Pol2))
return Empty_Polyhedron(Pol1->Dimension);
/* Find the maximum number of constraints over all polyhedron in the */
/* polyhedral domain 'Pol2' and store in 'NbCon'. */
NbCon = 0;
for (p2=Pol2; p2; p2=p2->next)
if (p2->NbConstraints > NbCon)
NbCon = p2->NbConstraints;
Dimension = Pol1->Dimension+2; /* Homogenous Dimension + Status */
d = (Polyhedron *)0;
for (p1=Pol1; p1; p1=p1->next) {
POL_ENSURE_VERTICES(p1);
/* Filter is an array of integers, each bit in an element of Filter */
/* array corresponds to a constraint. The bit is marked 1 if the */
/* corresponding constraint is non-redundant and is 0 if it is */
/* redundant. */
NbConstraints = p1->NbConstraints;
nbentries = (NbConstraints + NbCon - 1) / (sizeof(int)*8) + 1;
/* Allocate space for array 'Filter' */
Filter = (unsigned *)malloc(nbentries * sizeof(int));
if (!Filter) {
errormsg1("DomSimplify", "outofmem", "out of memory space\n");
Pol_status = 1;
return 0;
}
/* Initialize 'Filter' with zeros */
SMVector_Init(Filter, nbentries);
/* Filter the constraints of p1 in context of polyhedra p2(s) */
empty = 1;
for (p2=Pol2; p2; p2=p2->next) {
POL_ENSURE_VERTICES(p2);
/* Store the non-redundant constraints in array 'Filter'. With */
/* successive loops, the array 'Filter' holds the union of all */
/* non-redundant constraints. 'empty' is set to zero if the */
/* intersection of two polyhedra is non-empty and Filter is !Null */
SimplifyEqualities(p1, p2, Filter);
if (SimplifyConstraints(p1, p2, Filter, NbMaxRays))
empty=0;
/* takes the union of all non redundant constraints */
}
if (!empty) {
/* Copy all non-redundant constraints to matrix 'Constraints' */
Constraints = Matrix_Alloc(NbConstraints, Dimension);
if (!Constraints) {
errormsg1("DomSimplify", "outofmem", "out of memory space\n");
Pol_status = 1;
return 0;
}
Constraints->NbRows = 0;
for (k=0, jx=0, bx=MSB; k<NbConstraints; k++) {
/* If a bit entry in Filter[jx] is marked 1, copy the correspond- */
/* ing constraint in matrix 'Constraints'. */
if (Filter[jx]&bx) {
Vector_Copy(p1->Constraint[k],
Constraints->p[Constraints->NbRows],
Dimension);
Constraints->NbRows++;
}
NEXT(jx,bx);
}
/* Create the polyhedron 'p3' corresponding to the constraints in */
/* matrix 'Constraints'. */
p3 = Constraints2Polyhedron(Constraints,NbMaxRays);
Matrix_Free(Constraints);
/* Add polyhedron 'p3' in the domain 'd'. */
d = AddPolyToDomain (p3, d);
p3 = NULL;
}
free(Filter);
}
if (!d)
return Empty_Polyhedron(Pol1->Dimension);
else return d;
} /* DomainSimplify */
/*
* Domain Simplify as defined in Strasborg Polylib version.
*/
Polyhedron *Stras_DomainSimplify(Polyhedron *Pol1,Polyhedron *Pol2,unsigned NbMaxRays) {
Polyhedron *p1, *p2, *p3 = NULL, *d = NULL;
unsigned k, jx, bx, nbentries, NbConstraints, Dimension, NbCon, empty;
unsigned *Filter = NULL;
Matrix *Constraints = NULL;
CATCH(any_exception_error) {
if (Constraints) Matrix_Free(Constraints);
if (Filter) free(Filter);
if (d) Polyhedron_Free(d);
if (p2) Polyhedron_Free(p3);
RETHROW();
}
TRY {
if (!Pol1 || !Pol2) {
UNCATCH(any_exception_error);
return Pol1;
}
if (Pol1->Dimension != Pol2->Dimension) {
errormsg1("DomainSimplify","diffdim","operation on different dimensions");
UNCATCH(any_exception_error);
return 0;
}
POL_ENSURE_VERTICES(Pol1);
POL_ENSURE_VERTICES(Pol2);
if (emptyQ(Pol1)||emptyQ(Pol2)) {
UNCATCH(any_exception_error);
return Empty_Polyhedron(Pol1->Dimension);
}
/* Find the maximum number of constraints over all polyhedron in the */
/* polyhedral domain 'Pol2' and store in 'NbCon'. */
NbCon = 0;
for (p2=Pol2; p2; p2=p2->next)
if (p2->NbConstraints > NbCon)
NbCon = p2->NbConstraints;
Dimension = Pol1->Dimension+2; /* Homogenous Dimension + Status */
d = (Polyhedron *)0;
for (p1=Pol1; p1; p1=p1->next) {
/* Filter is an array of integers, each bit in an element of Filter */
/* array corresponds to a constraint. The bit is marked 1 if the */
/* corresponding constraint is non-redundant and is 0 if it is */
/* redundant. */
NbConstraints = p1->NbConstraints;
nbentries = (NbConstraints + NbCon - 1)/(sizeof(int)*8) + 1;
/* Allocate space for array 'Filter' */
Filter = (unsigned *)malloc(nbentries * sizeof(int));
if(!Filter) {
errormsg1("DomainSimplify", "outofmem", "out of memory space");
UNCATCH(any_exception_error);
return 0;
}
/* Initialize 'Filter' with zeros */
SMVector_Init(Filter, nbentries);
/* Filter the constraints of p1 in context to the polyhedra p2(s) */
empty = 1;
for (p2=Pol2; p2; p2=p2->next) {
/* Store the non-redundant constraints in array 'Filter'. With */
/* successive loops, the array 'Filter' holds the union of all */
/* non-redundant constraints. 'empty' is set to zero if the */
/* intersection of two polyhedra is non-empty and Filter is !Null */
if (SimplifyConstraints(p1, p2, Filter, NbMaxRays))
empty=0;
}
if (!empty) {
/* Copy all non-redundant constraints to matrix 'Constraints' */
Constraints = Matrix_Alloc(NbConstraints,Dimension);
if(!Constraints) {
errormsg1("DomainSimplify", "outofmem", "out of memory space");
UNCATCH(any_exception_error);
return 0;
}
Constraints->NbRows = 0;
for (k=0, jx=0, bx=MSB; k<NbConstraints; k++) {
/* If a bit entry in Filter[jx] is marked 1, copy the correspond- */
/* ing constraint in matrix 'Constraints'. */
if (Filter[jx]&bx) {
Vector_Copy(p1->Constraint[k],
Constraints->p[Constraints->NbRows],
Dimension);
Constraints->NbRows++;
}
NEXT(jx,bx);
}
/* Create the polyhedron 'p3' corresponding to the constraints in */
/* matrix 'Constraints'. */
p3 = Constraints2Polyhedron(Constraints,NbMaxRays);
Matrix_Free(Constraints), Constraints = NULL;
/* Add polyhedron 'p3' in the domain 'd'. */
d = AddPolyToDomain (p3, d);
p3 = NULL;
}
free(Filter), Filter = NULL;
}
} /* end of TRY */
UNCATCH(any_exception_error);
if (!d)
return Empty_Polyhedron(Pol1->Dimension);
else
return d;
} /* DomainSimplify */
/*
* Return the Union of two polyhedral domains 'Pol1' and Pol2'. The result is
* a new polyhedral domain.
*/
Polyhedron *DomainUnion(Polyhedron *Pol1,Polyhedron *Pol2,unsigned NbMaxRays) {
Polyhedron *PolA, *PolEndA, *PolB, *PolEndB, *p1, *p2;
int Redundant;
if (!Pol1 || !Pol2) return (Polyhedron *) 0;
if (Pol1->Dimension != Pol2->Dimension) {
errormsg1("DomainUnion","diffdim","operation on different dimensions");
return (Polyhedron*) 0;
}
/* Copy 'Pol1' to 'PolA' */
PolA = PolEndA = (Polyhedron *)0;
for (p1=Pol1; p1; p1=p1->next) {
/* Does any component of 'Pol2' cover 'p1' ? */
Redundant = 0;
for (p2=Pol2; p2; p2=p2->next) {
if (PolyhedronIncludes(p2, p1) ) { /* p2 covers p1 */
Redundant = 1;
break;
}
}
if (!Redundant) {
/* Add 'p1' to 'PolA' */
if (!PolEndA)
PolEndA = PolA = Polyhedron_Copy(p1);
else {
PolEndA->next = Polyhedron_Copy(p1);
PolEndA = PolEndA->next;
}
}
}
/* Copy 'Pol2' to PolB */
PolB = PolEndB = (Polyhedron *)0;
for (p2=Pol2; p2; p2=p2->next) {
/* Does any component of PolA cover 'p2' ? */
Redundant = 0;
for (p1=PolA; p1; p1=p1->next) {
if (PolyhedronIncludes(p1, p2)) { /* p1 covers p2 */
Redundant = 1;
break;
}
}
if (!Redundant) {
/* Add 'p2' to 'PolB' */
if (!PolEndB)
PolEndB = PolB = Polyhedron_Copy(p2);
else {
PolEndB->next = Polyhedron_Copy(p2);
PolEndB = PolEndB->next;
}
}
}
if (!PolA) return PolB;
PolEndA->next = PolB;
return PolA;
} /* DomainUnion */
/*
* Given a polyhedral domain 'Pol', concatenate the lists of rays and lines
* of the two (or more) polyhedra in the domain into one combined list, and
* find the set of constraints which tightly bound all of those objects.
* 'NbMaxConstrs' is the maximum allowed constraints in the new polyhedron.
*/
Polyhedron *DomainConvex(Polyhedron *Pol,unsigned NbMaxConstrs) {
Polyhedron *p, *q, *NewPol = NULL;
CATCH(any_exception_error) {
if (NewPol) Polyhedron_Free(NewPol);
RETHROW();
}
TRY {
if (!Pol) {
UNCATCH(any_exception_error);
return (Polyhedron*) 0;
}
POL_ENSURE_VERTICES(Pol);
NewPol = Polyhedron_Copy(Pol);
for (p=Pol->next; p; p=p->next) {
POL_ENSURE_VERTICES(p);
q = AddRays(p->Ray[0], p->NbRays, NewPol, NbMaxConstrs);
Polyhedron_Free(NewPol);
NewPol = q;
}
} /* end of TRY */
UNCATCH(any_exception_error);
return NewPol;
} /* DomainConvex */
/*
* Given polyhedral domains 'Pol1' and 'Pol2', create a new polyhedral
* domain which is mathematically the differnce of the two domains.
*/
Polyhedron *DomainDifference(Polyhedron *Pol1,Polyhedron *Pol2,unsigned NbMaxRays) {
Polyhedron *p1, *p2, *p3, *d;
int i;
if (!Pol1 || !Pol2) return (Polyhedron*) 0;
if (Pol1->Dimension != Pol2->Dimension) {
errormsg1("DomainDifference",
"diffdim", "operation on different dimensions");
return (Polyhedron*) 0;
}
POL_ENSURE_FACETS(Pol1);
POL_ENSURE_VERTICES(Pol1);
POL_ENSURE_FACETS(Pol2);
POL_ENSURE_VERTICES(Pol2);
if (emptyQ(Pol1) || emptyQ(Pol2))
return (Domain_Copy(Pol1));
d = (Polyhedron *)0;
for (p2=Pol2; p2; p2=p2->next) {
for (p1=Pol1; p1; p1=p1->next) {
for (i=0; i<p2->NbConstraints; i++) {
/* Add the constraint ( -p2->constraint[i] -1) >= 0 in 'p1' */
/* and create the new polyhedron 'p3'. */
p3 = SubConstraint(p2->Constraint[i], p1, NbMaxRays,0);
/* Add 'p3' in the new domain 'd' */
d = AddPolyToDomain (p3, d);
/* If the constraint p2->constraint[i][0] is an equality, then */
/* add the constraint ( +p2->constraint[i] -1) >= 0 in 'p1' and*/
/* create the new polyhedron 'p3'. */
if( value_notzero_p(p2->Constraint[i][0]) ) /* Inequality */
continue;
p3 = SubConstraint(p2->Constraint[i], p1, NbMaxRays,1);
/* Add 'p3' in the new domain 'd' */
d = AddPolyToDomain (p3, d);
}
}
if (p2 != Pol2)
Domain_Free(Pol1);
Pol1 = d;
d = (Polyhedron *)0;
}
if (!Pol1)
return Empty_Polyhedron(Pol2->Dimension);
else
return Pol1;
} /* DomainDifference */
/*
* Given a polyhedral domain 'Pol', convert it to a new polyhedral domain
* with dimension expanded to 'align_dimension'. 'NbMaxRays' is the maximum
* allowed rays in the new polyhedra.
*/
Polyhedron *align_context(Polyhedron *Pol,int align_dimension,int NbMaxRays) {
int i, j, k;
Polyhedron *p = NULL, **next, *result = NULL;
unsigned dim;
CATCH(any_exception_error) {
if (result) Polyhedron_Free(result);
RETHROW();
}
TRY {
if (!Pol) return Pol;
dim = Pol->Dimension;
if (align_dimension < Pol->Dimension) {
errormsg1("align_context", "diffdim", "context dimension exceeds data");
UNCATCH(any_exception_error);
return NULL;
}
if (align_dimension == Pol->Dimension) {
UNCATCH(any_exception_error);
return Domain_Copy(Pol);
}
/* 'k' is the dimension increment */
k = align_dimension - Pol->Dimension;
next = &result;
/* Expand the dimension of all polyhedra in the polyhedral domain 'Pol' */
for (; Pol; Pol=Pol->next) {
int have_cons = !F_ISSET(Pol, POL_VALID) || F_ISSET(Pol, POL_INEQUALITIES);
int have_rays = !F_ISSET(Pol, POL_VALID) || F_ISSET(Pol, POL_POINTS);
unsigned NbCons = have_cons ? Pol->NbConstraints : 0;
unsigned NbRays = have_rays ? Pol->NbRays + k : 0;
if (Pol->Dimension != dim) {
Domain_Free(result);
errormsg1("align_context", "diffdim", "context not of uniform dimension");
UNCATCH(any_exception_error);
return NULL;
}
p = Polyhedron_Alloc(align_dimension, NbCons, NbRays);
if (have_cons) {
for (i = 0; i < NbCons; ++i) {
value_assign(p->Constraint[i][0], Pol->Constraint[i][0]); /* Status bit */
Vector_Copy(Pol->Constraint[i]+1, p->Constraint[i]+k+1, Pol->Dimension+1);
}
p->NbEq = Pol->NbEq;
}
if (have_rays) {
for (i = 0; i < k; ++i)
value_set_si(p->Ray[i][1+i], 1); /* A line */
for (i = 0; i < Pol->NbRays; ++i) {
value_assign(p->Ray[k+i][0], Pol->Ray[i][0]); /* Status bit */
Vector_Copy(Pol->Ray[i]+1, p->Ray[i+k]+k+1, Pol->Dimension+1);
}
p->NbBid = Pol->NbBid + k;
}
p->flags = Pol->flags;
*next = p;
next = &p->next;
}
} /* end of TRY */
UNCATCH(any_exception_error);
return result;
} /* align_context */
/*----------------------------------------------------------------------*/
/* Polyhedron *Polyhedron_Scan(D, C, NbMaxRays) */
/* D : Domain to be scanned (single polyhedron only) */
/* C : Context domain */
/* NbMaxRays : Workspace size */
/* Returns a linked list of scan domains, outer loop first */
/*----------------------------------------------------------------------*/
Polyhedron *Polyhedron_Scan(Polyhedron *D, Polyhedron *C,unsigned NbMaxRays) {
int i, j, dim ;
Matrix *Mat;
Polyhedron *C1, *C2, *D1, *D2;
Polyhedron *res, *last, *tmp;
dim = D->Dimension - C->Dimension;
res = last = (Polyhedron *) 0;
if (dim==0) return (Polyhedron *)0;
assert(!D->next);
POL_ENSURE_FACETS(D);
POL_ENSURE_VERTICES(D);
POL_ENSURE_FACETS(C);
POL_ENSURE_VERTICES(C);
/* Allocate space for constraint matrix. */
Mat = Matrix_Alloc(D->Dimension, D->Dimension+2);
if(!Mat) {
errormsg1("Polyhedron_Scan", "outofmem", "out of memory space");
return 0;
}
C1 = align_context(C,D->Dimension,NbMaxRays);
if(!C1) {
return 0;
}
/* Vin100, aug 16, 2001: The context is intersected with D */
D2 = DomainIntersection( C1, D, NbMaxRays);
for (i=0; i<dim; i++)
{
Vector_Set(Mat->p_Init,0,D2->Dimension*(D2->Dimension + 2));
for (j=i+1; j<dim; j++) {
value_set_si(Mat->p[j-i-1][j+1],1);
}
Mat->NbRows = dim-i-1;
D1 = Mat->NbRows ? DomainAddRays(D2, Mat, NbMaxRays) : D2;
tmp = DomainSimplify(D1, C1, NbMaxRays);
if (!last) res = last = tmp;
else { last->next = tmp; last = tmp; }
C2 = DomainIntersection(C1, D1, NbMaxRays);
Domain_Free(C1);
C1 = C2;
if (Mat->NbRows) Domain_Free(D1);
}
Domain_Free(D2);
Domain_Free(C1);
Matrix_Free(Mat);
return res;
} /* Polyhedron_Scan */
/*---------------------------------------------------------------------*/
/* int lower_upper_bounds(pos,P,context,LBp,UBp) */
/* pos : index position of current loop index (1..hdim-1) */
/* P: loop domain */
/* context : context values for fixed indices */
/* notice that context[hdim] must be 1 */
/* LBp, UBp : pointers to resulting bounds */
/* returns the flag = (UB_INFINITY, LB_INFINITY) */
/*---------------------------------------------------------------------*/
int lower_upper_bounds(int pos,Polyhedron *P,Value *context,Value *LBp,Value *UBp) {
Value LB, UB;
int flag, i;
Value n, n1, d, tmp;
POL_ENSURE_FACETS(P);
POL_ENSURE_VERTICES(P);
/* Initialize all the 'Value' variables */
value_init(LB); value_init(UB); value_init(tmp);
value_init(n); value_init(n1); value_init(d);
value_set_si(LB,0);
value_set_si(UB,0);
/* Compute Upper Bound and Lower Bound for current loop */
flag = LB_INFINITY | UB_INFINITY;
for (i=0; i<P->NbConstraints; i++) {
value_assign(d,P->Constraint[i][pos]);
Inner_Product(&context[1],&(P->Constraint[i][1]),P->Dimension+1,&n);
if (value_zero_p(d)) {
/* If context doesn't satisfy constraints, return empty loop. */
if (value_neg_p(n) ||
(value_zero_p(P->Constraint[i][0]) && value_notzero_p(n)))
goto empty_loop;
continue;
}
value_oppose(n,n);
/*---------------------------------------------------*/
/* Compute n/d n/d<0 n/d>0 */
/*---------------------------------------------------*/
/* n%d == 0 floor = n/d floor = n/d */
/* ceiling = n/d ceiling = n/d */
/*---------------------------------------------------*/
/* n%d != 0 floor = n/d - 1 floor = n/d */
/* ceiling = n/d ceiling = n/d + 1 */
/*---------------------------------------------------*/
/* Check to see if constraint is inequality */
/* if constraint is equality, both upper and lower bounds are fixed */
if(value_zero_p(P->Constraint[i][0])) { /* Equality */
value_modulus(tmp,n,d);
/* if not integer, return 0; */
if (value_notzero_p(tmp))
goto empty_loop;
value_division(n1,n,d);
/* Upper and Lower bounds found */
if((flag&LB_INFINITY) || value_gt(n1,LB))
value_assign(LB,n1);
if((flag&UB_INFINITY) || value_lt(n1,UB))
value_assign(UB,n1);
flag = 0;
}
if (value_pos_p(d)) { /* Lower Bound */
value_modulus(tmp,n,d);
/* n1 = ceiling(n/d) */
if (value_pos_p(n) && value_notzero_p(tmp)) {
value_division(n1,n,d);
value_add_int(n1,n1,1);
}
else
value_division(n1,n,d);
if (flag&LB_INFINITY) {
value_assign(LB,n1);
flag^=LB_INFINITY;
}
else if(value_gt(n1,LB))
value_assign(LB,n1);
}
if (value_neg_p(d)) { /* Upper Bound */
value_modulus(tmp,n,d);
/* n1 = floor(n/d) */
if (value_pos_p(n) && value_notzero_p(tmp)) {
value_division(n1,n,d);
value_sub_int(n1,n1,1);
}
else
value_division(n1,n,d);
if (flag&UB_INFINITY) {
value_assign(UB,n1);
flag^=UB_INFINITY;
}
else if (value_lt(n1,UB))
value_assign(UB, n1);
}
}
if ((flag & LB_INFINITY)==0) value_assign(*LBp,LB);
if ((flag & UB_INFINITY)==0) value_assign(*UBp,UB);
if (0) {
empty_loop:
flag = 0;
value_set_si(*LBp, 1);
value_set_si(*UBp, 0); /* empty loop */
}
/* Clear all the 'Value' variables */
value_clear(LB); value_clear(UB); value_clear(tmp);
value_clear(n); value_clear(n1); value_clear(d);
return flag;
} /* lower_upper_bounds */
/*
* C = A x B
*/
static void Rays_Mult(Value **A, Matrix *B, Value **C, unsigned NbRays)
{
int i, j, k;
unsigned Dimension1, Dimension2;
Value Sum, tmp;
value_init(Sum); value_init(tmp);
CATCH(any_exception_error) {
value_clear(Sum); value_clear(tmp);
RETHROW();
}
TRY {
Dimension1 = B->NbRows;
Dimension2 = B->NbColumns;
for (i=0; i<NbRays; i++) {
value_assign(C[i][0],A[i][0]);
for (j=0; j<Dimension2; j++) {
value_set_si(Sum,0);
for (k=0; k<Dimension1; k++) {
/* Sum+=A[i][k+1] * B->p[k][j]; */
value_addmul(Sum, A[i][k+1], B->p[k][j]);
}
value_assign(C[i][j+1],Sum);
}
Vector_Gcd(C[i]+1, Dimension2, &tmp);
if (value_notone_p(tmp))
Vector_AntiScale(C[i]+1, C[i]+1, tmp, Dimension2);
}
}
UNCATCH(any_exception_error);
value_clear(Sum); value_clear(tmp);
}
/*
* C = A x B^T
*/
static void Rays_Mult_Transpose(Value **A, Matrix *B, Value **C,
unsigned NbRays)
{
int i, j, k;
unsigned Dimension1, Dimension2;
Value Sum, tmp;
value_init(Sum); value_init(tmp);
CATCH(any_exception_error) {
value_clear(Sum); value_clear(tmp);
RETHROW();
}
TRY {
Dimension1 = B->NbColumns;
Dimension2 = B->NbRows;
for (i=0; i<NbRays; i++) {
value_assign(C[i][0],A[i][0]);
for (j=0; j<Dimension2; j++) {
value_set_si(Sum,0);
for (k=0; k<Dimension1; k++) {
/* Sum+=A[i][k+1] * B->p[j][k]; */
value_addmul(Sum, A[i][k+1], B->p[j][k]);
}
value_assign(C[i][j+1],Sum);
}
Vector_Gcd(C[i]+1, Dimension2, &tmp);
if (value_notone_p(tmp))
Vector_AntiScale(C[i]+1, C[i]+1, tmp, Dimension2);
}
}
UNCATCH(any_exception_error);
value_clear(Sum); value_clear(tmp);
}
/*
* Given a polyhedron 'Pol' and a transformation matrix 'Func', return the
* polyhedron which when transformed by mapping function 'Func' gives 'Pol'.
* 'NbMaxRays' is the maximum number of rays that can be in the ray matrix
* of the resulting polyhedron.
*/
Polyhedron *Polyhedron_Preimage(Polyhedron *Pol,Matrix *Func,unsigned NbMaxRays) {
Matrix *Constraints = NULL;
Polyhedron *NewPol = NULL;
unsigned NbConstraints, Dimension1, Dimension2;
POL_ENSURE_INEQUALITIES(Pol);
CATCH(any_exception_error) {
if (Constraints) Matrix_Free(Constraints);
if (NewPol) Polyhedron_Free(NewPol);
RETHROW();
}
TRY {
NbConstraints = Pol->NbConstraints;
Dimension1 = Pol->Dimension+1; /* Homogeneous Dimension */
Dimension2 = Func->NbColumns; /* Homogeneous Dimension */
if (Dimension1!=(Func->NbRows)) {
errormsg1("Polyhedron_Preimage", "dimincomp", "incompatable dimensions");
UNCATCH(any_exception_error);
return Empty_Polyhedron(Dimension2-1);
}
/* Dim1 Dim2 Dim2
__k__ __j__ __j__
NbCon | | X Dim1| | = NbCon | |
i |___| k |___| i |___|
Pol->Constraints Function Constraints
*/
/* Allocate space for the resulting constraint matrix */
Constraints = Matrix_Alloc(NbConstraints, Dimension2+1);
if (!Constraints) {
errormsg1("Polyhedron_Preimage", "outofmem", "out of memory space\n");
Pol_status = 1;
UNCATCH(any_exception_error);
return 0;
}
/* The new constraint matrix is the product of constraint matrix of the */
/* polyhedron and the function matrix. */
Rays_Mult(Pol->Constraint, Func, Constraints->p, NbConstraints);
NewPol = Constraints2Polyhedron(Constraints, NbMaxRays);
Matrix_Free(Constraints), Constraints = NULL;
} /* end of TRY */
UNCATCH(any_exception_error);
return NewPol;
} /* Polyhedron_Preimage */
/*
* Given a polyhedral domain 'Pol' and a transformation matrix 'Func', return
* the polyhedral domain which when transformed by mapping function 'Func'
* gives 'Pol'. 'NbMaxRays' is the maximum number of rays that can be in the
* ray matrix of the resulting domain.
*/
Polyhedron *DomainPreimage(Polyhedron *Pol,Matrix *Func,unsigned NbMaxRays) {
Polyhedron *p, *q, *d = NULL;
CATCH(any_exception_error) {
if (d) Polyhedron_Free(d);
RETHROW();
}
TRY {
if (!Pol || !Func) {
UNCATCH(any_exception_error);
return (Polyhedron *) 0;
}
d = (Polyhedron *) 0;
for (p=Pol; p; p=p->next) {
q = Polyhedron_Preimage(p, Func, NbMaxRays);
d = AddPolyToDomain (q, d);
}
} /* end of TRY */
UNCATCH(any_exception_error);
return d;
} /* DomainPreimage */
/*
* Transform a polyhedron 'Pol' into another polyhedron according to a given
* affine mapping function 'Func'. 'NbMaxConstrs' is the maximum number of
* constraints that can be in the constraint matrix of the new polyhedron.
*/
Polyhedron *Polyhedron_Image(Polyhedron *Pol, Matrix *Func,unsigned NbMaxConstrs) {
Matrix *Rays = NULL;
Polyhedron *NewPol = NULL;
unsigned NbRays, Dimension1, Dimension2;
POL_ENSURE_FACETS(Pol);
POL_ENSURE_VERTICES(Pol);
CATCH(any_exception_error) {
if (Rays) Matrix_Free(Rays);
if (NewPol) Polyhedron_Free(NewPol);
RETHROW();
}
TRY {
NbRays = Pol->NbRays;
Dimension1 = Pol->Dimension+1; /* Homogeneous Dimension */
Dimension2 = Func->NbRows; /* Homogeneous Dimension */
if (Dimension1!=Func->NbColumns) {
errormsg1("Polyhedron_Image", "dimincomp", "incompatible dimensions");
UNCATCH(any_exception_error);
return Empty_Polyhedron(Dimension2-1);
}
/*
Dim1 / Dim1 \Transpose Dim2
__k__ | __k__ | __j__
NbRays| | X | Dim2 | | | = NbRays| |
i |___| | j |___| | i |___|
Pol->Rays \ Func / Rays
*/
if (Dimension1 == Dimension2) {
Matrix *M, *M2;
int ok;
M = Matrix_Copy(Func);
M2 = Matrix_Alloc(Dimension2, Dimension1);
if (!M2) {
errormsg1("Polyhedron_Image", "outofmem", "out of memory space\n");
UNCATCH(any_exception_error);
return 0;
}
ok = Matrix_Inverse(M, M2);
Matrix_Free(M);
if (ok) {
NewPol = Polyhedron_Alloc(Pol->Dimension, Pol->NbConstraints,
Pol->NbRays);
if (!NewPol) {
errormsg1("Polyhedron_Image", "outofmem",
"out of memory space\n");
UNCATCH(any_exception_error);
return 0;
}
Rays_Mult_Transpose(Pol->Ray, Func, NewPol->Ray, NbRays);
Rays_Mult(Pol->Constraint, M2, NewPol->Constraint,
Pol->NbConstraints);
NewPol->NbEq = Pol->NbEq;
NewPol->NbBid = Pol->NbBid;
if (NewPol->NbEq)
Gauss4(NewPol->Constraint, NewPol->NbEq, NewPol->NbConstraints,
NewPol->Dimension+1);
if (NewPol->NbBid)
Gauss4(NewPol->Ray, NewPol->NbBid, NewPol->NbRays,
NewPol->Dimension+1);
}
Matrix_Free(M2);
}
if (!NewPol) {
/* Allocate space for the resulting ray matrix */
Rays = Matrix_Alloc(NbRays, Dimension2+1);
if (!Rays) {
errormsg1("Polyhedron_Image", "outofmem", "out of memory space\n");
UNCATCH(any_exception_error);
return 0;
}
/* The new ray space is the product of ray matrix of the polyhedron and */
/* the transpose matrix of the mapping function. */
Rays_Mult_Transpose(Pol->Ray, Func, Rays->p, NbRays);
NewPol = Rays2Polyhedron(Rays, NbMaxConstrs);
Matrix_Free(Rays), Rays = NULL;
}
} /* end of TRY */
UNCATCH(any_exception_error);
return NewPol;
} /* Polyhedron_Image */
/*
*Transform a polyhedral domain 'Pol' into another domain according to a given
* affine mapping function 'Func'. 'NbMaxConstrs' is the maximum number of
* constraints that can be in the constraint matrix of the resulting domain.
*/
Polyhedron *DomainImage(Polyhedron *Pol,Matrix *Func,unsigned NbMaxConstrs) {
Polyhedron *p, *q, *d = NULL;
CATCH(any_exception_error) {
if (d) Polyhedron_Free(d);
RETHROW();
}
TRY {
if (!Pol || !Func) {
UNCATCH(any_exception_error);
return (Polyhedron *) 0;
}
d = (Polyhedron *) 0;
for (p=Pol; p; p=p->next) {
q = Polyhedron_Image(p, Func, NbMaxConstrs);
d = AddPolyToDomain (q, d);
}
} /* end of TRY */
UNCATCH(any_exception_error);
return d;
} /* DomainImage */
/*
* Given a polyhedron 'Pol' and an affine cost function 'Cost', compute the
* maximum and minimum value of the function over set of points representing
* polyhedron.
* Note: If Polyhedron 'Pol' is empty, then there is no feasible solution.
* Otherwise, if there is a bidirectional ray with Sum[cost(i)*ray(i)] != 0 or
* a unidirectional ray with Sum[cost(i)*ray(i)] >0, then the maximum is un-
* bounded else the finite optimal solution occurs at one of the vertices of
* the polyhderon.
*/
Interval *DomainCost(Polyhedron *Pol,Value *Cost) {
int i, j, NbRay, Dim;
Value *p1, *p2, p3, d, status;
Value tmp1, tmp2, tmp3;
Value **Ray;
Interval *I = NULL;
value_init(p3); value_init(d); value_init(status);
value_init(tmp1); value_init(tmp2); value_init(tmp3);
POL_ENSURE_FACETS(Pol);
POL_ENSURE_VERTICES(Pol);
CATCH(any_exception_error) {
if (I) free(I);
RETHROW();
value_clear(p3); value_clear(d); value_clear(status);
value_clear(tmp1); value_clear(tmp2); value_clear(tmp3);
}
TRY {
Ray = Pol->Ray;
NbRay = Pol->NbRays;
Dim = Pol->Dimension+1; /* Homogenous Dimension */
I = (Interval *) malloc (sizeof(Interval));
if (!I) {
errormsg1("DomainCost", "outofmem", "out of memory space\n");
UNCATCH(any_exception_error);
value_clear(p3); value_clear(d); value_clear(status);
value_clear(tmp1); value_clear(tmp2); value_clear(tmp3);
return 0;
}
/* The maximum and minimum values of the cost function over polyhedral */
/* domain is stored in 'I'. I->MaxN and I->MaxD store the numerator and */
/* denominator of the maximum value. Likewise,I->MinN and I->MinD store */
/* the numerator and denominator of the minimum value. I->MaxI and */
/* I->MinI store the ray indices corresponding to the max and min values*/
/* of the function. */
value_set_si(I->MaxN,-1);
value_set_si(I->MaxD,0); /* Actual cost is MaxN/MaxD */
I->MaxI = -1;
value_set_si(I->MinN,1);
value_set_si(I->MinD,0);
I->MinI = -1;
/* Compute the cost of each ray[i] */
for (i=0; i<NbRay; i++) {
p1 = Ray[i];
value_assign(status, *p1);
p1++;
p2 = Cost;
/* p3 = *p1++ * *p2++; */
value_multiply(p3,*p1,*p2);
p1++; p2++;
for (j=1; j<Dim; j++) {
value_multiply(tmp1,*p1,*p2);
/* p3 += *p1++ * *p2++; */
value_addto(p3,p3,tmp1);
p1++; p2++;
}
/* d = *--p1; */
p1--;
value_assign(d,*p1); /* d == 0 for lines and ray, non-zero for vertex */
value_multiply(tmp1,p3,I->MaxD);
value_multiply(tmp2,I->MaxN,d);
value_set_si(tmp3,1);
/* Compare p3/d with MaxN/MaxD to assign new maximum cost value */
if (I->MaxI==-1 ||
value_gt(tmp1,tmp2) ||
(value_eq(tmp1,tmp2) &&
value_eq(d,tmp3) && value_ne(I->MaxD,tmp3))) {
value_assign(I->MaxN,p3);
value_assign(I->MaxD,d);
I->MaxI = i;
}
value_multiply(tmp1,p3,I->MinD);
value_multiply(tmp2,I->MinN,d);
value_set_si(tmp3,1);
/* Compare p3/d with MinN/MinD to assign new minimum cost value */
if (I->MinI==-1 ||
value_lt(tmp1,tmp2) ||
(value_eq(tmp1,tmp2) &&
value_eq(d,tmp3) && value_ne(I->MinD,tmp3))) {
value_assign(I->MinN, p3);
value_assign(I->MinD, d);
I->MinI = i;
}
value_multiply(tmp1,p3,I->MaxD);
value_set_si(tmp2,0);
/* If there is a line, assign max to +infinity and min to -infinity */
if (value_zero_p(status)) { /* line , d is 0 */
if (value_lt(tmp1,tmp2)) {
value_oppose(I->MaxN,p3);
value_set_si(I->MaxD,0);
I->MaxI = i;
}
value_multiply(tmp1,p3,I->MinD);
value_set_si(tmp2,0);
if (value_gt(tmp1,tmp2)) {
value_oppose(I->MinN,p3);
value_set_si(I->MinD,0);
I->MinI = i;
}
}
}
} /* end of TRY */
UNCATCH(any_exception_error);
value_clear(p3); value_clear(d); value_clear(status);
value_clear(tmp1); value_clear(tmp2); value_clear(tmp3);
return I;
} /* DomainCost */
/*
* Add constraints pointed by 'Mat' to each and every polyhedron in the
* polyhedral domain 'Pol'. 'NbMaxRays' is maximum allowed rays in the ray
* matrix of a polyhedron.
*/
Polyhedron *DomainAddConstraints(Polyhedron *Pol,Matrix *Mat,unsigned NbMaxRays) {
Polyhedron *PolA, *PolEndA, *p1, *p2, *p3;
int Redundant;
if (!Pol) return (Polyhedron*) 0;
if (!Mat) return Pol;
if (Pol->Dimension != Mat->NbColumns-2) {
errormsg1("DomainAddConstraints",
"diffdim", "operation on different dimensions");
return (Polyhedron*) 0;
}
/* Copy 'Pol' to 'PolA' */
PolA = PolEndA = (Polyhedron *)0;
for (p1=Pol; p1; p1=p1->next) {
p3 = AddConstraints(Mat->p_Init, Mat->NbRows, p1, NbMaxRays);
/* Does any component of 'PolA' cover 'p3' */
Redundant = 0;
for (p2=PolA; p2; p2=p2->next) {
if (PolyhedronIncludes(p2, p3)) { /* 'p2' covers 'p3' */
Redundant = 1;
break;
}
}
/* If the new polyhedron 'p3' is not redundant, add it to the domain */
if (Redundant)
Polyhedron_Free(p3);
else {
if (!PolEndA)
PolEndA = PolA = p3;
else {
PolEndA->next = p3;
PolEndA = PolEndA->next;
}
}
}
return PolA;
} /* DomainAddConstraints */
/*
* Computes the disjoint union of a union of polyhedra.
* If flag = 0 the result is such that there are no intersections
* between the resulting polyhedra,
* if flag = 1 it computes a joint union, the resulting polyhedra are
* adjacent (they have their facets in common).
*
* WARNING: if all polyhedra are not of same geometrical dimension
* duplicates may appear.
*/
Polyhedron *Disjoint_Domain( Polyhedron *P, int flag, unsigned NbMaxRays )
{
Polyhedron *lP, *tmp, *Result, *lR, *prec, *reste;
Polyhedron *p1, *p2, *p3, *Pol1, *dx, *d1, *d2, *pi, *newpi;
int i;
if( flag!=0 && flag!=1 )
{
errormsg1("Disjoint_Domain",
"invalidarg", "flag should be equal to 0 or 1");
return (Polyhedron*) 0;
}
if(!P) return (Polyhedron*) 0;
if(!P->next) return Polyhedron_Copy(P);
Result = (Polyhedron *)0;
for(lP=P;lP;lP=lP->next)
{
reste = Polyhedron_Copy(lP);
prec = (Polyhedron *)0; /* preceeding lR */
/* Intersection with each polyhedron of the current Result */
lR=Result;
while( lR && reste )
{
/* dx = DomainIntersection(reste,lR->P,WS); */
dx = (Polyhedron *)0;
for( p1=reste; p1; p1=p1->next )
{
p3 = AddConstraints(lR->Constraint[0], lR->NbConstraints, p1,
NbMaxRays);
dx = AddPolyToDomain(p3,dx);
}
/* if empty intersection, continue */
if(!dx)
{ prec = lR;
lR=lR->next;
continue;
}
if (emptyQ(dx)) {
Domain_Free(dx);
prec = lR;
lR=lR->next;
continue;
}
/* intersection is not empty, we need to compute the differences */
/* between the intersection and the two polyhedra, such that the */
/* results are disjoint unions (according to flag) */
/* d1 = reste \ P = DomainDifference(reste,lR->P,WS); */
/* d2 = P \ reste = DomainDifference(lR->P,reste,WS); */
/* compute d1 */
d1 = (Polyhedron *)0;
for (p1=reste; p1; p1=p1->next)
{
pi = p1;
for (i=0; i<P->NbConstraints && pi ; i++)
{
/* Add the constraint ( -P->constraint[i] [-1 if flag=0]) >= 0 in 'p1' */
/* and create the new polyhedron 'p3'. */
p3 = SubConstraint(P->Constraint[i], pi, NbMaxRays,2*flag);
/* Add 'p3' in the new domain 'd1' */
d1 = AddPolyToDomain (p3, d1);
/* If the constraint P->constraint[i][0] is an equality, then add */
/* the constraint ( +P->constraint[i] [-1 if flag=0]) >= 0 in 'pi' */
/* and create the new polyhedron 'p3'. */
if( value_zero_p(P->Constraint[i][0]) ) /* Inequality */
{
p3 = SubConstraint(P->Constraint[i], pi, NbMaxRays,1+2*flag);
/* Add 'p3' in the new domain 'd1' */
d1 = AddPolyToDomain (p3, d1);
/* newpi : add constraint P->constraint[i]==0 to pi */
newpi = AddConstraints( P->Constraint[i], 1, pi, NbMaxRays);
}
else
{
/* newpi : add constraint +P->constraint[i] >= 0 in pi */
newpi = SubConstraint(P->Constraint[i], pi, NbMaxRays,3);
}
if( newpi && emptyQ( newpi ) )
{
Domain_Free( newpi );
newpi = (Polyhedron *)0;
}
if( pi != p1 )
Domain_Free( pi );
pi = newpi;
}
if( pi != p1 )
Domain_Free( pi );
}
/* and now d2 */
Pol1 = Polyhedron_Copy( lR );
for (p2=reste; p2; p2=p2->next)
{
d2 = (Polyhedron *)0;
for (p1=Pol1; p1; p1=p1->next)
{
pi = p1;
for (i=0; i<p2->NbConstraints && pi ; i++)
{
/* Add the constraint ( -p2->constraint[i] [-1 if flag=0]) >= 0 in 'pi' */
/* and create the new polyhedron 'p3'. */
p3 = SubConstraint(p2->Constraint[i], pi, NbMaxRays,2*flag);
/* Add 'p3' in the new domain 'd2' */
d2 = AddPolyToDomain (p3, d2);
/* If the constraint p2->constraint[i][0] is an equality, then add */
/* the constraint ( +p2->constraint[i] [-1 if flag=0]) >= 0 in 'pi' */
/* and create the new polyhedron 'p3'. */
if( value_zero_p(p2->Constraint[i][0]) ) /* Inequality */
{
p3 = SubConstraint(p2->Constraint[i], pi, NbMaxRays,1+2*flag);
/* Add 'p3' in the new domain 'd2' */
d2 = AddPolyToDomain (p3, d2);
/* newpi : add constraint p2->constraint[i]==0 to pi */
newpi = AddConstraints( p2->Constraint[i], 1, pi, NbMaxRays);
}
else
{
/* newpi : add constraint +p2->constraint[i] >= 0 in pi */
newpi = SubConstraint(p2->Constraint[i], pi, NbMaxRays,3);
}
if( newpi && emptyQ( newpi ) )
{
Domain_Free( newpi );
newpi = (Polyhedron *)0;
}
if( pi != p1 )
Domain_Free( pi );
pi = newpi;
}
if( pi && pi!=p1 )
Domain_Free( pi );
}
if( Pol1 )
Domain_Free( Pol1 );
Pol1 = d2;
}
/* ok, d1 and d2 are computed */
/* now, replace lR by d2+dx (at least dx is nonempty) and set reste to d1 */
if( d1 && emptyQ(d1) )
{
Domain_Free( d1 );
d1 = NULL;
}
if( d2 && emptyQ(d2) )
{
Domain_Free( d2 );
d2 = NULL;
}
/* set reste */
Domain_Free( reste );
reste = d1;
/* add d2 at beginning of Result */
if( d2 )
{
for( tmp=d2 ; tmp->next ; tmp=tmp->next )
;
tmp->next = Result;
Result = d2;
if( !prec )
prec = tmp;
}
/* add dx at beginning of Result */
for( tmp=dx ; tmp->next ; tmp=tmp->next )
;
tmp->next = Result;
Result = dx;
if( !prec )
prec = tmp;
/* suppress current lR */
if( !prec )
errormsg1( "Disjoint_Domain","internalerror","internal error");
prec->next = lR->next;
Polyhedron_Free( lR );
lR = prec->next;
} /* end for result */
/* if there is something left, add it to Result : */
if(reste)
{
if(emptyQ(reste))
{
Domain_Free( reste );
reste = NULL;
}
else
{
Polyhedron *tnext;
for( tmp=reste ; tmp ; tmp=tnext )
{
tnext = tmp->next;
tmp->next = NULL;
Result = AddPolyToDomain(tmp, Result);
}
}
}
}
return( Result );
}
/* Procedure to print constraint matrix of a polyhedron */
void Polyhedron_PrintConstraints(FILE *Dst, const char *Format, Polyhedron *Pol)
{
int i,j;
fprintf( Dst, "%d %d\n", Pol->NbConstraints, Pol->Dimension+2 );
for( i=0 ; i<Pol->NbConstraints ; i++ )
{
for( j=0 ; j<Pol->Dimension+2 ; j++ )
value_print( Dst, Format, Pol->Constraint[i][j] );
fprintf( Dst, "\n" );
}
}
/* Procedure to print constraint matrix of a domain */
void Domain_PrintConstraints(FILE *Dst, const char *Format, Polyhedron *Pol)
{
Polyhedron *Q;
for (Q = Pol; Q; Q = Q->next)
Polyhedron_PrintConstraints(Dst, Format, Q);
}
static Polyhedron *p_simplify_constraints(Polyhedron *P, Vector *row,
Value *g, unsigned MaxRays)
{
Polyhedron *T, *R = P;
int len = P->Dimension+2;
int r;
/* Also look at equalities.
* If an equality can be "simplified" then there
* are no integer solutions and we return an empty polyhedron
*/
for (r = 0; r < R->NbConstraints; ++r) {
if (ConstraintSimplify(R->Constraint[r], row->p, len, g)) {
T = R;
if (value_zero_p(R->Constraint[r][0])) {
R = Empty_Polyhedron(R->Dimension);
r = R->NbConstraints;
} else if (POL_ISSET(MaxRays, POL_NO_DUAL)) {
R = Polyhedron_Copy(R);
F_CLR(R, POL_FACETS | POL_VERTICES | POL_POINTS);
Vector_Copy(row->p+1, R->Constraint[r]+1, R->Dimension+1);
} else {
R = AddConstraints(row->p, 1, R, MaxRays);
r = -1;
}
if (T != P)
Polyhedron_Free(T);
}
}
if (R != P)
Polyhedron_Free(P);
return R;
}
/*
* Replaces constraint a x >= c by x >= ceil(c/a)
* where "a" is a common factor in the coefficients
* Destroys P and returns a newly allocated Polyhedron
* or just returns P in case no changes were made
*/
Polyhedron *DomainConstraintSimplify(Polyhedron *P, unsigned MaxRays)
{
Polyhedron **prev;
int len = P->Dimension+2;
Vector *row = Vector_Alloc(len);
Value g;
Polyhedron *R = P, *N;
value_set_si(row->p[0], 1);
value_init(g);
for (prev = &R; P; P = N) {
Polyhedron *T;
N = P->next;
T = p_simplify_constraints(P, row, &g, MaxRays);
if (emptyQ(T) && prev != &R) {
Polyhedron_Free(T);
*prev = NULL;
continue;
}
if (T != P)
T->next = N;
*prev = T;
prev = &T->next;
}
if (R->next && emptyQ(R)) {
N = R->next;
Polyhedron_Free(R);
R = N;
}
value_clear(g);
Vector_Free(row);
return R;
}
|