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
|
/** @file ratio.c
*
* A variety of routines:
* The ratio command for partial fractioning
* (rather old. Schoonschip inheritance)
* The sum routines.
*/
/* #[ License : */
/*
* Copyright (C) 1984-2026 J.A.M. Vermaseren
* When using this file you are requested to refer to the publication
* J.A.M.Vermaseren "New features of FORM" math-ph/0010025
* This is considered a matter of courtesy as the development was paid
* for by FOM the Dutch physics granting agency and we would like to
* be able to track its scientific use to convince FOM of its value
* for the community.
*
* This file is part of FORM.
*
* FORM is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* FORM is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with FORM. If not, see <http://www.gnu.org/licenses/>.
*/
/* #] License : */
/*
#[ Includes : ratio.c
*/
#include "form3.h"
/*
#] Includes :
#[ Ratio :
These are the special operations regarding simple polynomials.
The first and most needed is the partial fractioning expansion.
Ratio,x1,x2,x3
The files belonging to the ratio command serve also as a good example
of how to implement a new operation.
#[ RatioFind :
The routine that should locate the need for a ratio command.
If located the corresponding symbols are removed and the
operational parameters are loaded. A subexpression pointer
is inserted and the code for success is returned.
params points at the compiler output block defined in RatioComp.
*/
int RatioFind(PHEAD WORD *term, WORD *params)
{
GETBIDENTITY
WORD *t, *m, *r;
WORD x1, x2, i;
WORD *y1, *y2, n1 = 0, n2 = 0;
x1 = params[3];
x2 = params[4];
m = t = term;
m += *m;
m -= ABS(m[-1]);
t++;
if ( t < m ) do {
if ( *t == SYMBOL ) {
y1 = 0;
y2 = 0;
r = t + t[1];
m = t + 2;
do {
if ( *m == x1 ) { y1 = m; n1 = m[1]; }
else if ( *m == x2 ) { y2 = m; n2 = m[1]; }
m += 2;
} while ( m < r );
if ( !y1 || !y2 || ( n1 > 0 && n2 > 0 ) ) return(0);
m -= 2;
if ( y1 > y2 ) { r = y1; y1 = y2; y2 = r; }
*y2 = *m; y2[1] = m[1];
m -= 2;
*y1 = *m; y1[1] = m[1];
i = WORDDIF(m,t);
#if SUBEXPSIZE > 6
We have to revise the code for the second case.
#endif
if ( i > 2 ) { /* Subexpression fits exactly */
t[1] = i;
y1 = term+*term;
y2 = y1+SUBEXPSIZE-4;
r = m+4;
while ( y1 > r ) *--y2 = *--y1;
*m++ = SUBEXPRESSION;
*m++ = SUBEXPSIZE;
*m++ = -1;
*m++ = 1;
*m++ = DUMMYBUFFER;
FILLSUB(m)
*term += SUBEXPSIZE-4;
}
else { /* All symbols are gone. Rest has to be moved */
m -= 2;
*m++ = SUBEXPRESSION;
*m++ = SUBEXPSIZE;
*m++ = -1;
*m++ = 1;
*m++ = DUMMYBUFFER;
FILLSUB(m)
t = term;
t += *t;
*term += SUBEXPSIZE-6;
r = m + 6-SUBEXPSIZE;
do { *m++ = *r++; } while ( r < t );
}
t = AT.TMout; /* Load up the TM out array for the generator */
*t++ = 7;
*t++ = RATIO;
*t++ = x1;
*t++ = x2;
*t++ = params[5];
*t++ = n1;
*t++ = n2;
return(1);
}
t += t[1];
} while ( t < m );
return(0);
}
/*
#] RatioFind :
#[ RatioGen :
The algorithm:
x1^-n1*x2^n2 ==> x2 --> x1 + x3
x1^n1*x2^-n2 ==> x1 --> x2 - x3
x1^-n1*x2^-n2 ==>
+sum(i=0,n1-1){(-1)^i*binom(n2-1+i,n2-1)
*x3^-(n2+i)*x1^-(n1-i)}
+sum(i=0,n2-1){(-1)^(n1)*binom(n1-1+i,n1-1)
*x3^-(n1+i)*x2^-(n2-i)}
Actually there is an amount of arbitrariness in the first two
formulae and the replacement x2 -> x1 + x3 could be made 'by hand'.
It is better to use the nontrivial 'minimal change' formula:
x1^-n1*x2^n2: if ( n1 >= n2 ) {
+sum(i=0,n2){x3^i*x1^-(n1-n2+i)*binom(n2,i)}
}
else {
sum(i=0,n2-n1){x2^(n2-n1-i)*x3^i*binom(n1-1+i,n1-1)}
+sum(i=0,n1-1){x3^(n2-i)*x1^-(n1-i)*binom(n2,i)}
}
x1^n1*x2^-n2: Same but x3 -> -x3.
The contents of the AT.TMout/params array are:
length,type,x1,x2,x3,n1,n2
*/
int RatioGen(PHEAD WORD *term, WORD *params, WORD num, WORD level)
{
GETBIDENTITY
WORD *t, *m;
WORD *tstops[3];
WORD n1, n2, i, j;
WORD x1,x2,x3;
UWORD *coef;
WORD ncoef, sign = 0;
coef = (UWORD *)AT.WorkPointer;
t = term;
tstops[2] = m = t + *t;
m -= ABS(m[-1]);
t++;
do {
if ( *t == SUBEXPRESSION && t[2] == num ) break;
t += t[1];
} while ( t < m );
tstops[0] = t;
tstops[1] = t + t[1];
/*
Copying to termout will be from term to tstop1, then the induced part
and finally from tstop2 to tstop3
Now separate the various cases:
*/
t = params + 2;
x1 = *t++;
x2 = *t++;
x3 = *t++;
n1 = *t++;
n2 = *t++;
if ( n1 > 0 ) { /* Flip the variables and indicate -x3 */
n2 = -n2;
sign = 1;
i = n1; n1 = n2; n2 = i;
i = x1; x1 = x2; x2 = i;
goto PosNeg;
}
else if ( n2 > 0 ) {
n1 = -n1;
PosNeg:
if ( n2 <= n1 ) { /* x1 -> x2 + x3 */
*coef = 1;
ncoef = 1;
AT.WorkPointer = (WORD *)(coef + 1);
j = n2;
for ( i = 0; i <= n2; i++ ) {
if ( BinomGen(BHEAD term,level,tstops,x1,x3,n2-n1-i,i,sign&i
,coef,ncoef) ) goto RatioCall;
if ( i < n2 ) {
if ( Product(coef,&ncoef,j) ) goto RatioCall;
if ( Quotient(coef,&ncoef,i+1) ) goto RatioCall;
j--;
AT.WorkPointer = (WORD *)(coef + ABS(ncoef));
}
}
AT.WorkPointer = (WORD *)(coef);
return(0);
}
else {
/*
sum(i=0,n2-n1){x2^(n2-n1-i)*x3^i*binom(n1-1+i,n1-1)}
+sum(i=0,n1-1){x3^(n2-i)*x1^-(n1-i)*binom(n2,i)}
*/
*coef = 1;
ncoef = 1;
AT.WorkPointer = (WORD *)(coef + 1);
j = n2 - n1;
for ( i = 0; i <= j; i++ ) {
if ( BinomGen(BHEAD term,level,tstops,x2,x3,n2-n1-i,i,sign&i
,coef,ncoef) ) goto RatioCall;
if ( i < j ) {
if ( Product(coef,&ncoef,n1+i) ) goto RatioCall;
if ( Quotient(coef,&ncoef,i+1) ) goto RatioCall;
AT.WorkPointer = (WORD *)(coef + ABS(ncoef));
}
}
*coef = 1;
ncoef = 1;
AT.WorkPointer = (WORD *)(coef + 1);
j = n1-1;
for ( i = 0; i <= j; i++ ) {
if ( BinomGen(BHEAD term,level,tstops,x1,x3,i-n1,n2-i,sign&(n2-i)
,coef,ncoef) ) goto RatioCall;
if ( i < j ) {
if ( Product(coef,&ncoef,n2-i) ) goto RatioCall;
if ( Quotient(coef,&ncoef,i+1) ) goto RatioCall;
AT.WorkPointer = (WORD *)(coef + ABS(ncoef));
}
}
AT.WorkPointer = (WORD *)(coef);
return(0);
}
}
else {
n2 = -n2;
n1 = -n1;
/*
+sum(i=0,n1-1){(-1)^i*binom(n2-1+i,n2-1)
*x3^-(n2+i)*x1^-(n1-i)}
+sum(i=0,n2-1){(-1)^(n1)*binom(n1-1+i,n1-1)
*x3^-(n1+i)*x2^-(n2-i)}
*/
*coef = 1;
ncoef = 1;
AT.WorkPointer = (WORD *)(coef + 1);
j = n1-1;
for ( i = 0; i <= j; i++ ) {
if ( BinomGen(BHEAD term,level,tstops,x1,x3,i-n1,-n2-i,i&1
,coef,ncoef) ) goto RatioCall;
if ( i < j ) {
if ( Product(coef,&ncoef,n2+i) ) goto RatioCall;
if ( Quotient(coef,&ncoef,i+1) ) goto RatioCall;
AT.WorkPointer = (WORD *)(coef + ABS(ncoef));
}
}
*coef = 1;
ncoef = 1;
AT.WorkPointer = (WORD *)(coef + 1);
j = n2-1;
for ( i = 0; i <= j; i++ ) {
if ( BinomGen(BHEAD term,level,tstops,x2,x3,i-n2,-n1-i,n1&1
,coef,ncoef) ) goto RatioCall;
if ( i < j ) {
if ( Product(coef,&ncoef,n1+i) ) goto RatioCall;
if ( Quotient(coef,&ncoef,i+1) ) goto RatioCall;
AT.WorkPointer = (WORD *)(coef + ABS(ncoef));
}
}
AT.WorkPointer = (WORD *)(coef);
return(0);
}
RatioCall:
MLOCK(ErrorMessageLock);
MesCall("RatioGen");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
/*
#] RatioGen :
#[ BinomGen :
Routine for the generation of terms in a binomialtype expansion.
*/
int BinomGen(PHEAD WORD *term, WORD level, WORD **tstops, WORD x1, WORD x2,
WORD pow1, WORD pow2, WORD sign, UWORD *coef, WORD ncoef)
{
GETBIDENTITY
WORD *t, *r;
WORD *termout;
WORD k;
termout = AT.WorkPointer;
t = termout;
r = term;
do { *t++ = *r++; } while ( r < tstops[0] );
*t++ = SYMBOL;
if ( pow2 == 0 ) {
if ( pow1 == 0 ) t--;
else { *t++ = 4; *t++ = x1; *t++ = pow1; }
}
else if ( pow1 == 0 ) {
*t++ = 4; *t++ = x2; *t++ = pow2;
}
else {
*t++ = 6; *t++ = x1; *t++ = pow1; *t++ = x2; *t++ = pow2;
}
*t++ = LNUMBER;
*t++ = ABS(ncoef) + 3;
*t = ncoef;
if ( sign ) *t = -*t;
t++;
ncoef = ABS(ncoef);
for ( k = 0; k < ncoef; k++ ) *t++ = coef[k];
r = tstops[1];
do { *t++ = *r++; } while ( r < tstops[2] );
*termout = WORDDIF(t,termout);
AT.WorkPointer = t;
if ( AT.WorkPointer > AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
*AN.RepPoint = 1;
AR.expchanged = 1;
if ( Generator(BHEAD termout,level) ) {
MLOCK(ErrorMessageLock);
MesCall("BinomGen");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
AT.WorkPointer = termout;
return(0);
}
/*
#] BinomGen :
#] Ratio :
#[ Sum :
#[ DoSumF1 :
Routine expands a sum_ function.
Its arguments are:
The term in which the function occurs.
The parameter list:
length of parameter field
function number (SUMNUM1)
number of the symbol that is loop parameter
min value
max value
increment
the number of the subexpression to be removed
the level in the generation tree.
Note that the insertion of the loop parameter in the argument
is done via the regular wildcard substitution mechanism.
*/
int DoSumF1(PHEAD WORD *term, WORD *params, WORD replac, WORD level)
{
GETBIDENTITY
WORD *termout, *t, extractbuff = AT.TMbuff;
WORD isum, ival, iinc;
LONG from;
CBUF *C;
ival = params[3];
iinc = params[5];
if ( ( iinc > 0 && params[4] >= ival )
|| ( iinc < 0 && params[4] <= ival ) ) {
isum = (params[4] - ival)/iinc + 1;
}
else return(0);
termout = AT.WorkPointer;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer);
if ( AT.WorkPointer > AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
t = term + 1;
while ( *t != SUBEXPRESSION || t[2] != replac || t[4] != extractbuff )
t += t[1];
C = cbuf+t[4];
t += SUBEXPSIZE;
if ( params[2] < 0 ) {
while ( *t != INDTOIND || t[2] != -params[2] ) t += t[1];
*t = INDTOIND;
}
else {
while ( *t > SYMTOSUB || t[2] != params[2] ) t += t[1];
*t = SYMTONUM;
}
do {
t[3] = ival;
from = C->rhs[replac] - C->Buffer;
while ( C->Buffer[from] ) {
if ( InsertTerm(BHEAD term,replac,extractbuff,C->Buffer+from,termout,0) < 0 ) goto SumF1Call;
AT.WorkPointer = termout + *termout;
if ( Generator(BHEAD termout,level) < 0 ) goto SumF1Call;
from += C->Buffer[from];
}
ival += iinc;
} while ( --isum > 0 );
AT.WorkPointer = termout;
return(0);
SumF1Call:
MLOCK(ErrorMessageLock);
MesCall("DoSumF1");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
/*
#] DoSumF1 :
#[ Glue :
Routine multiplies two terms. The second term is subject
to the wildcard substitutions in sub.
Output in the first term. This routine is a variation on
the routine InsertTerm.
*/
int Glue(PHEAD WORD *term1, WORD *term2, WORD *sub, WORD insert)
{
GETBIDENTITY
UWORD *coef;
WORD ncoef, *t, *t1, *t2, i, nc2, nc3, old, newer;
coef = (UWORD *)(TermMalloc("Glue"));
t = term1;
t += *t;
i = t[-1];
t -= ABS(i);
old = WORDDIF(t,term1);
ncoef = REDLENG(i);
if ( i < 0 ) i = -i;
i--;
t1 = t;
t2 = (WORD *)coef;
while ( --i >= 0 ) *t2++ = *t1++;
i = *--t;
nc2 = WildFill(BHEAD t,term2,sub);
*t = i;
t += nc2;
nc2 = t[-1];
t -= ABS(nc2);
newer = WORDDIF(t,term1);
if ( MulRat(BHEAD (UWORD *)t,REDLENG(nc2),coef,ncoef,(UWORD *)t,&nc3) ) {
MLOCK(ErrorMessageLock);
MesCall("Glue");
MUNLOCK(ErrorMessageLock);
TermFree(coef,"Glue");
SETERROR(-1)
}
i = (ABS(nc3))*2;
t += i++;
*t++ = (nc3 >= 0)?i:-i;
*term1 = WORDDIF(t,term1);
/*
Switch the new piece with the old tail, so that noncommuting
variables get into their proper spot.
*/
i = old - insert;
t1 = t;
t2 = term1+insert;
NCOPY(t1,t2,i);
i = newer - old;
t1 = term1+insert;
t2 = term1+old;
NCOPY(t1,t2,i);
t2 = t;
i = old - insert;
NCOPY(t1,t2,i);
TermFree(coef,"Glue");
return(0);
}
/*
#] Glue :
#[ DoSumF2 :
*/
int DoSumF2(PHEAD WORD *term, WORD *params, WORD replac, WORD level)
{
GETBIDENTITY
WORD *termout, *t, *from, *sub, *to, extractbuff = AT.TMbuff;
WORD isum, ival, iinc, insert, i;
CBUF *C;
ival = params[3];
iinc = params[5];
if ( ( iinc > 0 && params[4] >= ival )
|| ( iinc < 0 && params[4] <= ival ) ) {
isum = (params[4] - ival)/iinc + 1;
}
else return(0);
termout = AT.WorkPointer;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer);
if ( AT.WorkPointer > AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
t = term + 1;
while ( *t != SUBEXPRESSION || t[2] != replac || t[4] != extractbuff ) t += t[1];
insert = WORDDIF(t,term);
from = term;
to = termout;
while ( from < t ) *to++ = *from++;
from += t[1];
sub = term + *term;
while ( from < sub ) *to++ = *from++;
*termout -= t[1];
sub = t;
C = cbuf+t[4];
t += SUBEXPSIZE;
if ( params[2] < 0 ) {
while ( *t != INDTOIND || t[2] != -params[2] ) t += t[1];
*t = INDTOIND;
}
else {
while ( *t > SYMTOSUB || t[2] != params[2] ) t += t[1];
*t = SYMTONUM;
}
t[3] = ival;
for(;;) {
AT.WorkPointer = termout + *termout;
to = AT.WorkPointer;
if ( ( to + *termout ) > AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
from = termout;
i = *termout;
NCOPY(to,from,i);
from = AT.WorkPointer;
AT.WorkPointer = to;
if ( Generator(BHEAD from,level) < 0 ) goto SumF2Call;
if ( --isum <= 0 ) break;
ival += iinc;
t[3] = ival;
if ( Glue(BHEAD termout,C->rhs[replac],sub,insert) < 0 ) goto SumF2Call;
}
AT.WorkPointer = termout;
return(0);
SumF2Call:
MLOCK(ErrorMessageLock);
MesCall("DoSumF2");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
/*
#] DoSumF2 :
#] Sum :
#[ GCDfunction :
#[ GCDfunction :
*/
typedef struct {
WORD *buffer;
DOLLARS dollar;
LONG size;
int type;
int dummy;
} ARGBUFFER;
int GCDfunction(PHEAD WORD *term,WORD level)
{
GETBIDENTITY
WORD *t, *tstop, *tf, *termout, *tin, *tout, *m, *mnext, *mstop, *mm;
int todo, i, ii, j, istart, sign = 1, action = 0;
WORD firstshort = 0, firstvalue = 0, gcdisone = 0, mlength, tlength, newlength;
WORD totargs = 0, numargs, argsdone = 0, *mh, oldval1, *g, *gcdout = 0;
WORD *arg1, *arg2;
UWORD x1,x2,x3;
LONG args;
#if ( FUNHEAD > 4 )
WORD sh[FUNHEAD+5];
#else
WORD sh[9];
#endif
DOLLARS d;
ARGBUFFER *abuf = 0, ab;
/*
#[ Find Function. Count arguments :
First find the proper function
*/
t = term + *term; tlength = t[-1];
tstop = t - ABS(tlength);
t = term + 1;
while ( t < tstop ) {
if ( *t != GCDFUNCTION ) { t += t[1]; continue; }
todo = 1; totargs = 0;
tf = t + FUNHEAD;
while ( tf < t + t[1] ) {
totargs++;
if ( *tf > 0 && tf[1] != 0 ) todo = 0;
NEXTARG(tf);
}
if ( todo ) break;
t += t[1];
}
if ( t >= tstop ) {
MLOCK(ErrorMessageLock);
MesPrint("Internal error. Indicated gcd_ function not encountered.");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
WantAddPointers(totargs);
args = AT.pWorkPointer; AT.pWorkPointer += totargs;
/*
#] Find Function. Count arguments :
#[ Do short arguments :
The function we need, in agreement with TestSub, is now in t
Make first a compilation of the short arguments (except $-s and expressions)
to see whether we need to do much work.
This means that after this scan we can ignore all short arguments with
the exception of unevaluated $-s and expressions.
*/
numargs = 0;
firstshort = 0;
tf = t + FUNHEAD;
while ( tf < t + t[1] ) {
if ( *tf == -SNUMBER && tf[1] == 0 ) { NEXTARG(tf); continue; }
if ( *tf > 0 || *tf == -DOLLAREXPRESSION || *tf == -EXPRESSION ) {
AT.pWorkSpace[args+numargs++] = tf;
NEXTARG(tf); continue;
}
if ( firstshort == 0 ) {
firstshort = *tf;
if ( *tf <= -FUNCTION ) { firstvalue = -(*tf); }
else { firstvalue = tf[1]; }
NEXTARG(tf);
argsdone++;
continue;
}
else if ( *tf != firstshort ) {
if ( *tf != -INDEX && *tf != -VECTOR && *tf != -MINVECTOR ) {
argsdone++; gcdisone = 1; break;
}
if ( firstshort != -INDEX && firstshort != -VECTOR && firstshort != -MINVECTOR ) {
argsdone++; gcdisone = 1; break;
}
if ( tf[1] != firstvalue ) {
argsdone++; gcdisone = 1; break;
}
if ( *t == -MINVECTOR ) { firstshort = -VECTOR; }
if ( firstshort == -MINVECTOR ) { firstshort = -VECTOR; }
}
else if ( *tf > -FUNCTION && *tf != -SNUMBER && tf[1] != firstvalue ) {
argsdone++; gcdisone = 1; break;
}
if ( *tf == -SNUMBER && firstvalue != tf[1] ) {
/*
make a new firstvalue which is gcd_(firstvalue,tf[1])
*/
if ( firstvalue == 1 || tf[1] == 1 ) { gcdisone = 1; break; }
if ( firstvalue < 0 && tf[1] < 0 ) {
x1 = -firstvalue; x2 = -tf[1]; sign = -1;
}
else {
x1 = ABS(firstvalue); x2 = ABS(tf[1]); sign = 1;
}
while ( ( x3 = x1%x2 ) != 0 ) { x1 = x2; x2 = x3; }
firstvalue = ((WORD)x2)*sign;
argsdone++;
if ( firstvalue == 1 ) { gcdisone = 1; break; }
}
NEXTARG(tf);
}
termout = AT.WorkPointer;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer);
if ( AT.WorkPointer > AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
/*
#] Do short arguments :
#[ Do trivial GCD :
Copy head
*/
i = t - term; tin = term; tout = termout;
NCOPY(tout,tin,i);
if ( gcdisone || ( firstshort == -SNUMBER && firstvalue == 1 ) ) {
sign = 1;
gcdone:
tin += t[1]; tstop = term + *term;
while ( tin < tstop ) *tout++ = *tin++;
*termout = tout - termout;
if ( sign < 0 ) tout[-1] = -tout[-1];
AT.WorkPointer = tout;
if ( argsdone && Generator(BHEAD termout,level) < 0 ) goto CalledFrom;
AT.WorkPointer = termout;
AT.pWorkPointer = args;
return(0);
}
/*
#] Do trivial GCD :
#[ Do short argument GCD :
*/
if ( numargs == 0 ) { /* basically we are done */
doshort:
sign = 1;
if ( firstshort == 0 ) goto gcdone;
if ( firstshort == -SNUMBER ) {
*tout++ = SNUMBER; *tout++ = 4; *tout++ = firstvalue; *tout++ = 1;
goto gcdone;
}
else if ( firstshort == -SYMBOL ) {
*tout++ = SYMBOL; *tout++ = 4; *tout++ = firstvalue; *tout++ = 1;
goto gcdone;
}
else if ( firstshort == -VECTOR || firstshort == -INDEX ) {
*tout++ = INDEX; *tout++ = 3; *tout++ = firstvalue; goto gcdone;
}
else if ( firstshort == -MINVECTOR ) {
sign = -1;
*tout++ = INDEX; *tout++ = 3; *tout++ = firstvalue; goto gcdone;
}
else if ( firstshort <= -FUNCTION ) {
*tout++ = firstvalue; *tout++ = FUNHEAD; FILLFUN(tout);
goto gcdone;
}
else {
MLOCK(ErrorMessageLock);
MesPrint("Internal error. Illegal short argument in GCDfunction.");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
}
/*
#] Do short argument GCD :
#[ Convert short argument :
Now we allocate space for the arguments in general notation.
First the special one if there were short arguments
*/
if ( firstshort ) {
switch ( firstshort ) {
case -SNUMBER:
sh[0] = 4; sh[1] = ABS(firstvalue); sh[2] = 1;
if ( firstvalue < 0 ) sh[3] = -3;
else sh[3] = 3;
sh[4] = 0;
break;
case -MINVECTOR:
case -VECTOR:
case -INDEX:
sh[0] = 8; sh[1] = INDEX; sh[2] = 3; sh[3] = firstvalue;
sh[4] = 1; sh[5] = 1;
if ( firstshort == -MINVECTOR ) sh[6] = -3;
else sh[6] = 3;
sh[7] = 0;
break;
case -SYMBOL:
sh[0] = 8; sh[1] = SYMBOL; sh[2] = 4; sh[3] = firstvalue; sh[4] = 1;
sh[5] = 1; sh[6] = 1; sh[7] = 3; sh[8] = 0;
break;
default:
sh[0] = FUNHEAD+4; sh[1] = firstshort; sh[2] = FUNHEAD;
for ( i = 2; i < FUNHEAD; i++ ) sh[i+1] = 0;
sh[FUNHEAD+1] = 1; sh[FUNHEAD+2] = 1; sh[FUNHEAD+3] = 3; sh[FUNHEAD+4] = 0;
break;
}
}
/*
#] Convert short argument :
#[ Sort arguments :
Now we should sort the arguments in a way that the dollars and the
expressions come last. That way we may never need them.
*/
for ( i = 1; i < numargs; i++ ) {
for ( ii = i; ii > 0; ii-- ) {
arg1 = AT.pWorkSpace[args+ii];
arg2 = AT.pWorkSpace[args+ii-1];
if ( *arg1 < 0 ) {
if ( *arg2 < 0 ) {
if ( *arg1 == -EXPRESSION ) break;
if ( *arg2 == -DOLLAREXPRESSION ) break;
AT.pWorkSpace[args+ii] = arg2;
AT.pWorkSpace[args+ii-1] = arg1;
}
else break;
}
else if ( *arg2 < 0 ) {
AT.pWorkSpace[args+ii] = arg2;
AT.pWorkSpace[args+ii-1] = arg1;
}
else {
if ( *arg1 > *arg2 ) {
AT.pWorkSpace[args+ii] = arg2;
AT.pWorkSpace[args+ii-1] = arg1;
}
else break;
}
}
}
/*
#] Sort arguments :
#[ There is a single term argument :
*/
if ( firstshort ) {
mh = sh; istart = 0;
oneterm:;
for ( i = istart; i < numargs; i++ ) {
arg1 = AT.pWorkSpace[args+i];
if ( *arg1 > 0 ) {
oldval1 = arg1[*arg1]; arg1[*arg1] = 0;
m = arg1+ARGHEAD;
while ( *m ) {
GCDterms(BHEAD mh,m,mh); m += *m;
if ( mh[0] == 4 && mh[1] == 1 && mh[2] == 1 && mh[3] == 3 ) {
gcdisone = 1; sign = 1; arg1[*arg1] = oldval1; goto gcdone;
}
}
arg1[*arg1] = oldval1;
}
else if ( *arg1 == -DOLLAREXPRESSION ) {
if ( ( d = DolToTerms(BHEAD arg1[1]) ) != 0 ) {
m = d->where;
while ( *m ) {
GCDterms(BHEAD mh,m,mh); m += *m;
argsdone++;
if ( mh[0] == 4 && mh[1] == 1 && mh[2] == 1 && mh[3] == 3 ) {
gcdisone = 1; sign = 1;
if ( d->factors ) M_free(d->factors,"Dollar factors");
M_free(d,"Copy of dollar variable"); goto gcdone;
}
}
if ( d->factors ) M_free(d->factors,"Dollar factors");
M_free(d,"Copy of dollar variable");
}
}
else {
mm = CreateExpression(BHEAD arg1[1]);
m = mm;
while ( *m ) {
GCDterms(BHEAD mh,m,mh); m += *m;
argsdone++;
if ( mh[0] == 4 && mh[1] == 1 && mh[2] == 1 && mh[3] == 3 ) {
gcdisone = 1; sign = 1; M_free(mm,"CreateExpression"); goto gcdone;
}
}
M_free(mm,"CreateExpression");
}
}
if ( firstshort ) {
if ( mh[0] == 4 ) {
firstshort = -SNUMBER; firstvalue = mh[1] * (mh[3]/3);
}
else if ( mh[1] == SYMBOL ) {
firstshort = -SYMBOL; firstvalue = mh[3];
}
else if ( mh[1] == INDEX ) {
firstshort = -INDEX; firstvalue = mh[3];
if ( mh[6] == -3 ) firstshort = -MINVECTOR;
}
else if ( mh[1] >= FUNCTION ) {
firstshort = -mh[1]; firstvalue = mh[1];
}
goto doshort;
}
else {
/*
We have a GCD that is only a single term.
Paste it in and combine the coefficients.
*/
mh[mh[0]] = 0;
mm = mh;
ii = 0;
goto multiterms;
}
}
/*
Now we have only regular arguments.
But some have not yet been expanded.
Check whether there are proper long arguments and if so if there is
one with just a single term
*/
for ( i = 0; i < numargs; i++ ) {
arg1 = AT.pWorkSpace[args+i];
if ( *arg1 > 0 && arg1[ARGHEAD]+ARGHEAD == *arg1 ) {
/*
We have an argument with a single term
*/
if ( i != 0 ) {
arg2 = AT.pWorkSpace[args];
AT.pWorkSpace[args] = arg1;
AT.pWorkSpace[args+1] = arg2;
}
m = mh = AT.WorkPointer;
mm = arg1+ARGHEAD; i = *mm;
NCOPY(m,mm,i);
AT.WorkPointer = m;
istart = 1;
argsdone++;
goto oneterm;
}
}
/*
#] There is a single term argument :
#[ Expand $ and expr :
We have: 1: regular multiterm arguments
2: dollars
3: expressions.
The sum of them is numargs. Their addresses are in args. The problem is
that expansion will lead to allocations that we have to return and all
these allocations are different in nature.
*/
action = 1;
abuf = (ARGBUFFER *)Malloc1(numargs*sizeof(ARGBUFFER),"argbuffer");
for ( i = 0; i < numargs; i++ ) {
arg1 = AT.pWorkSpace[args+i];
if ( *arg1 > 0 ) {
m = (WORD *)Malloc1(*arg1*sizeof(WORD),"argbuffer type 0");
abuf[i].buffer = m;
abuf[i].type = 0;
mm = arg1+ARGHEAD;
j = *arg1-ARGHEAD;
abuf[i].size = j;
if ( j ) argsdone++;
NCOPY(m,mm,j);
*m = 0;
}
else if ( *arg1 == -DOLLAREXPRESSION ) {
d = DolToTerms(BHEAD arg1[1]);
abuf[i].buffer = d->where;
abuf[i].type = 1;
abuf[i].dollar = d;
m = abuf[i].buffer;
if ( *m ) argsdone++;
while ( *m ) m+= *m;
abuf[i].size = m-abuf[i].buffer;
}
else if ( *arg1 == -EXPRESSION ) {
abuf[i].buffer = CreateExpression(BHEAD arg1[1]);
abuf[i].type = 2;
m = abuf[i].buffer;
if ( *m ) argsdone++;
while ( *m ) m+= *m;
abuf[i].size = m-abuf[i].buffer;
}
else {
MLOCK(ErrorMessageLock);
MesPrint("What argument is this?");
MUNLOCK(ErrorMessageLock);
goto CalledFrom;
}
}
for ( i = 0; i < numargs; i++ ) {
arg1 = abuf[i].buffer;
if ( *arg1 == 0 ) {}
else if ( arg1[*arg1] == 0 ) {
/*
After expansion there is an argument with a single term
*/
ab = abuf[i]; abuf[i] = abuf[0]; abuf[0] = ab;
mh = abuf[0].buffer;
for ( j = 1; j < numargs; j++ ) {
m = abuf[j].buffer;
while ( *m ) {
GCDterms(BHEAD mh,m,mh); m += *m;
argsdone++;
if ( mh[0] == 4 && mh[1] == 1 && mh[2] == 1 && mh[3] == 3 ) {
gcdisone = 1; sign = 1; break;
}
}
if ( *m ) break;
}
mm = mh + *mh; if ( mm[-1] < 0 ) { sign = -1; mm[-1] = -mm[-1]; }
mstop = mm - mm[-1]; m = mh+1; mlength = mm[-1];
while ( tin < t ) *tout++ = *tin++;
while ( m < mstop ) *tout++ = *m++;
tin += tin[1];
while ( tin < tstop ) *tout++ = *tin++;
tlength = REDLENG(tlength);
mlength = REDLENG(mlength);
if ( MulRat(BHEAD (UWORD *)tstop,tlength,(UWORD *)mstop,mlength,
(UWORD *)tout,&newlength) < 0 ) goto CalledFrom;
mlength = INCLENG(newlength);
tout += ABS(mlength);
tout[-1] = mlength*sign;
*termout = tout - termout;
AT.WorkPointer = tout;
if ( argsdone && Generator(BHEAD termout,level) < 0 ) goto CalledFrom;
goto cleanup;
}
}
/*
There are only arguments with more than one term.
We order them by size to make the computations as easy as possible.
*/
for ( i = 1; i < numargs; i++ ) {
for ( ii = i; ii > 0; ii-- ) {
if ( abuf[ii-1].size <= abuf[ii].size ) break;
ab = abuf[ii-1]; abuf[ii-1] = abuf[ii]; abuf[ii] = ab;
}
}
/*
#] Expand $ and expr :
#[ Multiterm subexpressions :
*/
ii = 0;
gcdout = abuf[ii].buffer;
for ( i = 0; i < numargs; i++ ) {
if ( abuf[i].buffer[0] ) { gcdout = abuf[i].buffer; ii = i; i++; argsdone++; break; }
}
for ( ; i < numargs; i++ ) {
if ( abuf[i].buffer[0] ) {
g = GCDfunction3(BHEAD gcdout,abuf[i].buffer);
argsdone++;
if ( gcdout != abuf[ii].buffer ) M_free(gcdout,"gcdout");
gcdout = g;
if ( gcdout[*gcdout] == 0 && gcdout[0] == 4 && gcdout[1] == 1
&& gcdout[2] == 1 && gcdout[3] == 3 ) break;
}
}
mm = gcdout;
multiterms:;
tlength = REDLENG(tlength);
while ( *mm ) {
tin = term; tout = termout; while ( tin < t ) *tout++ = *tin++;
tin += t[1];
mnext = mm + *mm; mlength = mnext[-1]; mstop = mnext - ABS(mlength);
mm++;
while ( mm < mstop ) *tout++ = *mm++;
while ( tin < tstop ) *tout++ = *tin++;
mlength = REDLENG(mlength);
if ( MulRat(BHEAD (UWORD *)tstop,tlength,(UWORD *)mm,mlength,
(UWORD *)tout,&newlength) < 0 ) goto CalledFrom;
mlength = INCLENG(newlength);
tout += ABS(mlength);
tout[-1] = mlength;
*termout = tout - termout;
AT.WorkPointer = tout;
if ( argsdone && Generator(BHEAD termout,level) < 0 ) goto CalledFrom;
mm = mnext; /* next term */
}
if ( action && ( gcdout != abuf[ii].buffer ) ) M_free(gcdout,"gcdout");
/*
#] Multiterm subexpressions :
#[ Cleanup :
*/
cleanup:;
if ( action ) {
for ( i = 0; i < numargs; i++ ) {
if ( abuf[i].type == 0 ) { M_free(abuf[i].buffer,"argbuffer type 0"); }
else if ( abuf[i].type == 1 ) {
d = abuf[i].dollar;
if ( d->factors ) M_free(d->factors,"Dollar factors");
M_free(d,"Copy of dollar variable");
}
else if ( abuf[i].type == 2 ) { M_free(abuf[i].buffer,"CreateExpression"); }
}
M_free(abuf,"argbuffer");
}
/*
#] Cleanup :
*/
AT.pWorkPointer = args;
AT.WorkPointer = termout;
return(0);
CalledFrom:
MLOCK(ErrorMessageLock);
MesCall("GCDfunction");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
return(-1);
}
/*
#] GCDfunction :
#[ GCDfunction3 :
Finds the GCD of the two arguments which are buffers with terms.
In principle the first buffer can have only one term.
If both buffers have more than one term, we need to replace all
non-symbolic objects by generated symbols and substitute that back
afterwards. The rest we leave to the powerful routines.
Philosophical problem: What do we do with GCD_(x/z+y,x+y*z) ?
Method:
If we have only negative powers of z and no positive powers we let
the EXTRASYMBOLS do their job. When mixed, multiply the arguments with
the negative powers with enough powers of z to eliminate the negative powers.
The DENOMINATOR function is always eliminated with the mechanism as we
cannot tell whether there are positive powers of its contents.
*/
WORD *GCDfunction3(PHEAD WORD *in1, WORD *in2)
{
GETBIDENTITY
WORD oldsorttype = AR.SortType, *ow = AT.WorkPointer;;
WORD *t, *tt, *gcdout, *term1, *term2, *confree1, *confree2, *gcdout1, *proper1, *proper2;
int i, actionflag1, actionflag2;
WORD startebuf = cbuf[AT.ebufnum].numrhs;
WORD tryterm1, tryterm2;
if ( in2[*in2] == 0 ) { t = in1; in1 = in2; in2 = t; }
if ( in1[*in1] == 0 ) { /* First input with only one term */
gcdout = (WORD *)Malloc1((*in1+1)*sizeof(WORD),"gcdout");
i = *in1; t = gcdout; tt = in1; NCOPY(t,tt,i); *t = 0;
t = in2;
while ( *t ) {
GCDterms(BHEAD gcdout,t,gcdout);
if ( gcdout[0] == 4 && gcdout[1] == 1
&& gcdout[2] == 1 && gcdout[3] == 3 ) break;
t += *t;
}
AT.WorkPointer = ow;
return(gcdout);
}
/*
We need to take out the content from the two expressions
and determine their GCD. This plays with the negative powers!
*/
AR.SortType = SORTHIGHFIRST;
term1 = TermMalloc("GCDfunction3-a");
term2 = TermMalloc("GCDfunction3-b");
confree1 = TakeContent(BHEAD in1,term1);
tryterm1 = AN.tryterm; AN.tryterm = 0;
confree2 = TakeContent(BHEAD in2,term2);
tryterm2 = AN.tryterm; AN.tryterm = 0;
/*
confree1 = TakeSymbolContent(BHEAD in1,term1);
confree2 = TakeSymbolContent(BHEAD in2,term2);
*/
GCDterms(BHEAD term1,term2,term1);
TermFree(term2,"GCDfunction3-b");
/*
Now we have to replace all non-symbols and symbols to a negative power
by extra symbols.
*/
if ( ( proper1 = PutExtraSymbols(BHEAD confree1,startebuf,&actionflag1) ) == 0 ) goto CalledFrom;
if ( confree1 != in1 ) {
if ( tryterm1 ) { TermFree(confree1,"TakeContent"); }
else { M_free(confree1,"TakeContent"); }
}
/*
TermFree(confree1,"TakeSymbolContent");
*/
if ( ( proper2 = PutExtraSymbols(BHEAD confree2,startebuf,&actionflag2) ) == 0 ) goto CalledFrom;
if ( confree2 != in2 ) {
if ( tryterm2 ) { TermFree(confree2,"TakeContent"); }
else { M_free(confree2,"TakeContent"); }
}
/*
TermFree(confree2,"TakeSymbolContent");
*/
/*
And now the real work:
*/
gcdout1 = poly_gcd(BHEAD proper1,proper2,0);
M_free(proper1,"PutExtraSymbols");
M_free(proper2,"PutExtraSymbols");
AR.SortType = oldsorttype;
if ( actionflag1 || actionflag2 ) {
if ( ( gcdout = TakeExtraSymbols(BHEAD gcdout1,startebuf) ) == 0 ) goto CalledFrom;
M_free(gcdout1,"gcdout");
}
else {
gcdout = gcdout1;
}
cbuf[AT.ebufnum].numrhs = startebuf;
/*
Now multiply gcdout by term1
*/
if ( term1[0] != 4 || term1[3] != 3 || term1[1] != 1 || term1[2] != 1 ) {
AN.tryterm = -1;
if ( ( gcdout1 = MultiplyWithTerm(BHEAD gcdout,term1,2) ) == 0 ) goto CalledFrom;
AN.tryterm = 0;
M_free(gcdout,"gcdout");
gcdout = gcdout1;
}
TermFree(term1,"GCDfunction3-a");
AT.WorkPointer = ow;
return(gcdout);
CalledFrom:
AN.tryterm = 0;
MLOCK(ErrorMessageLock);
MesCall("GCDfunction3");
MUNLOCK(ErrorMessageLock);
return(0);
}
/*
#] GCDfunction3 :
#[ PutExtraSymbols :
*/
WORD *PutExtraSymbols(PHEAD WORD *in,WORD startebuf,int *actionflag)
{
WORD *termout = AT.WorkPointer;
int action;
*actionflag = 0;
NewSort(BHEAD0);
while ( *in ) {
if ( ( action = LocalConvertToPoly(BHEAD in,termout,startebuf,0) ) < 0 ) {
LowerSortLevel();
goto CalledFrom;
}
if ( action > 0 ) *actionflag = 1;
StoreTerm(BHEAD termout);
in += *in;
}
if ( EndSort(BHEAD (WORD *)((void *)(&termout)),2) < 0 ) goto CalledFrom;
return(termout);
CalledFrom:
MLOCK(ErrorMessageLock);
MesCall("PutExtraSymbols");
MUNLOCK(ErrorMessageLock);
return(0);
}
/*
#] PutExtraSymbols :
#[ TakeExtraSymbols :
*/
WORD *TakeExtraSymbols(PHEAD WORD *in,WORD startebuf)
{
CBUF *C = cbuf+AC.cbufnum;
CBUF *CC = cbuf+AT.ebufnum;
WORD *oldworkpointer = AT.WorkPointer, *termout;
termout = AT.WorkPointer;
NewSort(BHEAD0);
while ( *in ) {
if ( ConvertFromPoly(BHEAD in,termout,numxsymbol,CC->numrhs-startebuf+numxsymbol,startebuf-numxsymbol,1) <= 0 ) {
LowerSortLevel();
goto CalledFrom;
}
in += *in;
AT.WorkPointer = termout + *termout;
/*
ConvertFromPoly leaves terms with subexpressions. Hence:
*/
if ( Generator(BHEAD termout,C->numlhs) ) {
LowerSortLevel();
goto CalledFrom;
}
}
AT.WorkPointer = oldworkpointer;
if ( EndSort(BHEAD (WORD *)((void *)(&termout)),2) < 0 ) goto CalledFrom;
return(termout);
CalledFrom:
MLOCK(ErrorMessageLock);
MesCall("TakeExtraSymbols");
MUNLOCK(ErrorMessageLock);
return(0);
}
/*
#] TakeExtraSymbols :
#[ MultiplyWithTerm :
*/
WORD *MultiplyWithTerm(PHEAD WORD *in, WORD *term, WORD par)
{
WORD *termout, *t, *tt, *tstop, *ttstop;
WORD length, length1, length2;
WORD oldsorttype = AR.SortType;
COMPARE oldcompareroutine = (COMPARE)(AR.CompareRoutine);
AR.CompareRoutine = (COMPAREDUMMY)(&CompareSymbols);
if ( par == 0 || par == 2 ) AR.SortType = SORTHIGHFIRST;
else AR.SortType = SORTLOWFIRST;
termout = AT.WorkPointer;
NewSort(BHEAD0);
while ( *in ) {
tt = termout + 1;
tstop = in + *in; tstop -= ABS(tstop[-1]); t = in + 1;
while ( t < tstop ) *tt++ = *t++;
ttstop = term + *term; ttstop -= ABS(ttstop[-1]); t = term + 1;
while ( t < ttstop ) *tt++ = *t++;
length1 = REDLENG(in[*in-1]); length2 = REDLENG(term[*term-1]);
if ( MulRat(BHEAD (UWORD *)tstop,length1,
(UWORD *)ttstop,length2,(UWORD *)tt,&length) ) goto CalledFrom;
length = INCLENG(length);
tt += ABS(length); tt[-1] = length;
*termout = tt - termout;
SymbolNormalize(termout);
StoreTerm(BHEAD termout);
in += *in;
}
if ( par == 2 ) {
/* if ( AN.tryterm == 0 ) AN.tryterm = 1; */
AN.tryterm = 0; /* For now */
if ( EndSort(BHEAD (WORD *)((void *)(&termout)),2) < 0 ) goto CalledFrom;
}
else {
if ( EndSort(BHEAD termout,1) < 0 ) goto CalledFrom;
}
AR.CompareRoutine = (COMPAREDUMMY)oldcompareroutine;
AR.SortType = oldsorttype;
return(termout);
CalledFrom:
MLOCK(ErrorMessageLock);
MesCall("MultiplyWithTerm");
MUNLOCK(ErrorMessageLock);
return(0);
}
/*
#] MultiplyWithTerm :
#[ TakeContent :
*/
/**
* Implements part of the old ExecArg in which we take common factors
* from arguments with more than one term.
* Here the input is a sequence of terms in 'in' and the answer is a
* content-free sequence of terms. This sequence has been allocated by
* the Malloc1 routine in a call to EndSort, unless the expression was
* already content-free. In that case the input pointer is returned.
* The content is returned in term. This is supposed to be a separate
* allocation, made by TermMalloc in the calling routine.
*/
WORD *TakeContent(PHEAD WORD *in, WORD *term)
{
GETBIDENTITY
WORD *t, *tstop, *tcom, *tout, *tstore, *r, *rstop, *m, *mm, *w, *ww, *wterm;
WORD *tnext, *tt, *tterm, code[2];
WORD *inp, a, *den;
int i, j, k, action = 0, sign;
UWORD *GCDbuffer, *GCDbuffer2, *LCMbuffer, *LCMbuffer2, *ap;
WORD GCDlen, GCDlen2, LCMlen, LCMlen2, length, redlength, len1, len2;
tout = tstore = term+1;
/*
#[ INDEX :
*/
t = in;
tnext = t + *t;
tstop = tnext-ABS(tnext[-1]);
t++;
while ( t < tstop ) {
if ( *t == INDEX ) {
i = t[1]; NCOPY(tout,t,i); break;
}
else t += t[1];
}
if ( tout > tstore ) { /* There are indices in the first term */
t = tnext;
while ( *t ) {
tnext = t + *t;
rstop = tnext - ABS(tnext[-1]);
r = t+1;
if ( r == rstop ) goto noindices;
while ( r < rstop ) {
if ( *r != INDEX ) { r += r[1]; continue; }
m = tstore+2;
while ( m < tout ) {
for ( i = 2; i < r[1]; i++ ) {
if ( *m == r[i] ) break;
if ( *m > r[i] ) continue;
mm = m+1;
while ( mm < tout ) { mm[-1] = mm[0]; mm++; }
tout--; tstore[1]--; m--;
break;
}
m++;
}
}
if ( r >= rstop || tout <= tstore+2 ) {
tout = tstore; break;
}
}
if ( tout > tstore+2 ) { /* Now we have to take out what is in tstore */
t = in; w = in;
while ( *t ) {
wterm = w;
tnext = t + *t; t++; w++;
while ( *t != INDEX ) { i = t[1]; NCOPY(w,t,i); }
tt = t + t[1]; t += 2; r = tstore+2; ww = w; *w++ = INDEX; w++;
while ( r < tout && t < tt ) {
if ( *r > *t ) { *w++ = *t++; }
else if ( *r == *t ) { r++; t++; }
else goto CalledFrom;
}
if ( r < tout ) goto CalledFrom;
while ( t < tt ) *w++ = *t++;
ww[1] = w - ww;
if ( ww[1] == 2 ) w = ww;
while ( t < tnext ) *w++ = *t++;
*wterm = w - wterm;
}
*w = 0;
}
noindices:
tstore = tout;
}
/*
#] INDEX :
#[ VECTOR/DELTA :
*/
code[0] = VECTOR; code[1] = DELTA;
for ( k = 0; k < 2; k++ ) {
t = in;
tnext = t + *t;
tstop = tnext-ABS(tnext[-1]);
t++;
while ( t < tstop ) {
if ( *t == code[k] ) {
i = t[1]; NCOPY(tout,t,i); break;
}
else t += t[1];
}
if ( tout > tstore ) { /* There are vectors in the first term */
t = tnext;
while ( *t ) {
tnext = t + *t;
rstop = tnext - ABS(tnext[-1]);
r = t+1;
if ( r == rstop ) { tstore = tout; goto novectors; }
while ( r < rstop ) {
if ( *r != code[k] ) { r += r[1]; continue; }
m = tstore+2;
while ( m < tout ) {
for ( i = 2; i < r[1]; i += 2 ) {
if ( *m == r[i] && m[1] == r[i+1] ) break;
if ( *m > r[i] || ( *m == r[i] && m[1] > r[i+1] ) ) continue;
mm = m+2;
while ( mm < tout ) { mm[-2] = mm[0]; mm[-1] = mm[1]; mm += 2; }
tout -= 2; tstore[1] -= 2; m -= 2;
break;
}
m += 2;
}
}
if ( r >= rstop || tout <= tstore+2 ) {
tout = tstore; break;
}
}
if ( tout > tstore+2 ) { /* Now we have to take out what is in tstore */
t = in; w = in;
while ( *t ) {
wterm = w;
tnext = t + *t; t++; w++;
while ( *t != code[k] ) { i = t[1]; NCOPY(w,t,i); }
tt = t + t[1]; t += 2; r = tstore+2; ww = w; *w++ = code[k]; w++;
while ( r < tout && t < tt ) {
if ( ( *r > *t ) || ( *r == *t && r[1] > t[1] ) )
{ *w++ = *t++; *w++ = *t++; }
else if ( *r == *t && r[1] == t[1] ) { r += 2; t += 2; }
else goto CalledFrom;
}
if ( r < tout ) goto CalledFrom;
while ( t < tt ) *w++ = *t++;
ww[1] = w - ww;
if ( ww[1] == 2 ) w = ww;
while ( t < tnext ) *w++ = *t++;
*wterm = w - wterm;
}
*w = 0;
}
tstore = tout;
}
}
novectors:;
/*
#] VECTOR/DELTA :
#[ FUNCTIONS :
*/
t = in;
tnext = t + *t;
tstop = tnext-ABS(tnext[-1]);
t++;
tcom = 0;
while ( t < tstop ) {
if ( *t >= FUNCTION ) {
if ( functions[*t-FUNCTION].commute ) {
if ( tcom == 0 ) { tcom = tstore; }
else {
for ( i = 0; i < t[1]; i++ ) {
if ( t[i] != tcom[i] ) {
MLOCK(ErrorMessageLock);
MesPrint("GCD or factorization of more than one noncommuting object not allowed");
MUNLOCK(ErrorMessageLock);
goto CalledFrom;
}
}
}
}
i = t[1]; NCOPY(tstore,t,i);
}
else t += t[1];
}
if ( tout > tstore ) {
t = tnext;
while ( *t ) {
tnext = t + *t; tstop = tnext - ABS(tnext[-1]); t++;
if ( t == tstop ) goto nofunctions;
r = tstore;
while ( r < tout ) {
tt = t;
while ( tt < tstop ) {
for ( i = 0; i < r[1]; i++ ) {
if ( r[i] != tt[i] ) break;
}
if ( i == r[1] ) { r += r[1]; goto nextr1; }
}
/*
Not encountered in this term. Scratch from list
*/
m = r; mm = r + r[1];
while ( mm < tout ) *m++ = *mm++;
tout = m;
nextr1:;
}
if ( tout <= tstore ) break;
t += *t;
}
}
if ( tout > tstore ) {
/*
Now we have one or more functions left that are common in all terms.
Take them out. We do this one by one.
*/
r = tstore;
while ( r < tout ) {
t = in; ww = in; w = ww+1;
while ( *t ) {
tnext = t + *t;
t++;
for(;;) {
for ( i = 0; i < r[1]; i++ ) {
if ( t[i] != r[i] ) {
j = t[1]; NCOPY(w,t,j);
break;
}
}
if ( i == r[1] ) {
t += t[1];
while ( t < tnext ) *w++ = *t++;
*ww = w - ww;
break;
}
}
}
r += r[1];
*w = 0;
}
nofunctions:
tstore = tout;
}
/*
#] FUNCTIONS :
#[ SYMBOL :
We make a list of symbols and their minimal powers.
This includes negative powers. In the end we have to multiply by the
inverse of this list. That takes out all negative powers and leaves
things ready for further processing.
*/
tterm = AT.WorkPointer; tt = tterm+1;
tout[0] = SYMBOL; tout[1] = 2;
t = in;
tnext = t + *t; tstop = tnext - ABS(tnext[-1]); t++;
while ( t < tstop ) {
if ( *t == SYMBOL ) {
for ( i = 0; i < t[1]; i++ ) tout[i] = t[i];
break;
}
t += t[1];
}
t = tnext;
while ( *t ) {
tnext = t + *t; tstop = tnext - ABS(tnext[-1]); t++;
if ( t == tstop ) {
tout[1] = 2;
break;
}
else {
while ( t < tstop ) {
if ( *t == SYMBOL ) {
MergeSymbolLists(BHEAD tout,t,-1);
break;
}
t += t[1];
}
t = tnext;
}
}
if ( tout[1] > 2 ) {
t = tout;
tt[0] = t[0]; tt[1] = t[1];
for ( i = 2; i < t[1]; i += 2 ) {
tt[i] = t[i]; tt[i+1] = -t[i+1];
}
tt += tt[1];
tout += tout[1];
action++;
}
/*
#] SYMBOL :
#[ DOTPRODUCT :
We make a list of dotproducts and their minimal powers.
This includes negative powers. In the end we have to multiply by the
inverse of this list. That takes out all negative powers and leaves
things ready for further processing.
*/
tout[0] = DOTPRODUCT; tout[1] = 2;
t = in;
while ( *t ) {
tnext = t + *t; tstop = tnext - ABS(tnext[-1]); t++;
if ( t == tstop ) {
tout[1] = 2;
break;
}
while ( t < tstop ) {
if ( *t == DOTPRODUCT ) {
MergeDotproductLists(BHEAD tout,t,-1);
break;
}
t += t[1];
}
t = tnext;
}
if ( tout[1] > 2 ) {
t = tout;
tt[0] = t[0]; tt[1] = t[1];
for ( i = 2; i < t[1]; i += 3 ) {
tt[i] = t[i]; tt[i+1] = t[i+1]; tt[i+2] = -t[i+2];
}
tt += tt[1];
tout += tout[1];
action++;
}
/*
#] DOTPRODUCT :
#[ Coefficient :
Now we have to collect the GCD of the numerators
and the LCM of the denominators.
*/
AT.WorkPointer = tt;
if ( AN.cmod != 0 ) {
WORD x, ix, ip;
t = in; tnext = t + *t; tstop = tnext - ABS(tnext[-1]);
x = tstop[0];
if ( tnext[-1] < 0 ) x += AC.cmod[0];
if ( GetModInverses(x,(WORD)(AN.cmod[0]),&ix,&ip) ) goto CalledFrom;
*tout++ = x; *tout++ = 1; *tout++ = 3;
*tt++ = ix; *tt++ = 1; *tt++ = 3;
}
else {
GCDbuffer = NumberMalloc("MakeInteger");
GCDbuffer2 = NumberMalloc("MakeInteger");
LCMbuffer = NumberMalloc("MakeInteger");
LCMbuffer2 = NumberMalloc("MakeInteger");
t = in;
tnext = t + *t; length = tnext[-1];
if ( length < 0 ) { sign = -1; length = -length; }
else { sign = 1; }
tstop = tnext - length;
redlength = (length-1)/2;
for ( i = 0; i < redlength; i++ ) {
GCDbuffer[i] = (UWORD)(tstop[i]);
LCMbuffer[i] = (UWORD)(tstop[redlength+i]);
}
GCDlen = LCMlen = redlength;
while ( GCDbuffer[GCDlen-1] == 0 ) GCDlen--;
while ( LCMbuffer[LCMlen-1] == 0 ) LCMlen--;
t = tnext;
while ( *t ) {
tnext = t + *t; length = ABS(tnext[-1]);
tstop = tnext - length; redlength = (length-1)/2;
len1 = len2 = redlength;
den = tstop + redlength;
while ( tstop[len1-1] == 0 ) len1--;
while ( den[len2-1] == 0 ) len2--;
if ( GCDlen == 1 && GCDbuffer[0] == 1 ) {}
else {
GcdLong(BHEAD (UWORD *)tstop,len1,GCDbuffer,GCDlen,GCDbuffer2,&GCDlen2);
ap = GCDbuffer; GCDbuffer = GCDbuffer2; GCDbuffer2 = ap;
a = GCDlen; GCDlen = GCDlen2; GCDlen2 = a;
}
if ( len2 == 1 && den[0] == 1 ) {}
else {
GcdLong(BHEAD LCMbuffer,LCMlen,(UWORD *)den,len2,LCMbuffer2,&LCMlen2);
DivLong((UWORD *)den,len2,LCMbuffer2,LCMlen2,
GCDbuffer2,&GCDlen2,(UWORD *)AT.WorkPointer,&a);
MulLong(LCMbuffer,LCMlen,GCDbuffer2,GCDlen2,LCMbuffer2,&LCMlen2);
ap = LCMbuffer; LCMbuffer = LCMbuffer2; LCMbuffer2 = ap;
a = LCMlen; LCMlen = LCMlen2; LCMlen2 = a;
}
t = tnext;
}
if ( GCDlen != 1 || GCDbuffer[0] != 1 || LCMlen != 1 || LCMbuffer[0] != 1 ) {
redlength = GCDlen; if ( LCMlen > GCDlen ) redlength = LCMlen;
for ( i = 0; i < GCDlen; i++ ) *tout++ = (WORD)(GCDbuffer[i]);
for ( ; i < redlength; i++ ) *tout++ = 0;
for ( i = 0; i < LCMlen; i++ ) *tout++ = (WORD)(LCMbuffer[i]);
for ( ; i < redlength; i++ ) *tout++ = 0;
*tout++ = (2*redlength+1)*sign;
for ( i = 0; i < LCMlen; i++ ) *tt++ = (WORD)(LCMbuffer[i]);
for ( ; i < redlength; i++ ) *tt++ = 0;
for ( i = 0; i < GCDlen; i++ ) *tt++ = (WORD)(GCDbuffer[i]);
for ( ; i < redlength; i++ ) *tt++ = 0;
*tt++ = (2*redlength+1)*sign;
action++;
}
else {
*tout++ = 1; *tout++ = 1; *tout++ = 3*sign;
*tt++ = 1; *tt++ = 1; *tt++ = 3*sign;
if ( sign != 1 ) action++;
}
*tout = 0;
NumberFree(LCMbuffer2,"MakeInteger");
NumberFree(LCMbuffer ,"MakeInteger");
NumberFree(GCDbuffer2,"MakeInteger");
NumberFree(GCDbuffer ,"MakeInteger");
}
/*
#] Coefficient :
#[ Multiply by the inverse content :
*/
if ( action ) {
*tterm = tt - tterm;
AT.WorkPointer = tt;
inp = MultiplyWithTerm(BHEAD in,tterm,2);
AT.WorkPointer = tterm;
in = inp;
}
/*
#] Multiply by the inverse content :
*/
*term = tout - term;
AT.WorkPointer = tterm;
return(in);
CalledFrom:
MLOCK(ErrorMessageLock);
MesCall("TakeContent");
MUNLOCK(ErrorMessageLock);
return(0);
}
/*
#] TakeContent :
#[ MergeSymbolLists :
Merges the extra list into the old.
If par == -1 we take minimum powers
If par == 1 we take maximum powers
If par == 0 we take minimum of the absolute value of the powers
if one is positive and the other negative we get zero.
We assume that the symbols are in order in both lists
*/
int MergeSymbolLists(PHEAD WORD *old, WORD *extra, int par)
{
GETBIDENTITY
WORD *new = TermMalloc("MergeSymbolLists");
WORD *t1, *t2, *fill;
int i1,i2;
fill = new + 2; *new = SYMBOL;
i1 = old[1] - 2; i2 = extra[1] - 2;
t1 = old + 2; t2 = extra + 2;
switch ( par ) {
case -1:
while ( i1 > 0 && i2 > 0 ) {
if ( *t1 > *t2 ) {
if ( t2[1] < 0 ) { *fill++ = *t2++; *fill++ = *t2++; }
else t2 += 2;
i2 -= 2;
}
else if ( *t1 < *t2 ) {
if ( t1[1] < 0 ) { *fill++ = *t1++; *fill++ = *t1++; }
else t1 += 2;
i1 -= 2;
}
else if ( t1[1] < t2[1] ) {
*fill++ = *t1++; *fill++ = *t1++; t2 += 2;
i1 -= 2; i2 -=2;
}
else {
*fill++ = *t2++; *fill++ = *t2++; t1 += 2;
i1 -= 2; i2 -=2;
}
}
for ( ; i1 > 0; i1 -= 2 ) {
if ( t1[1] < 0 ) { *fill++ = *t1++; *fill++ = *t1++; }
else t1 += 2;
}
for ( ; i2 > 0; i2 -= 2 ) {
if ( t2[1] < 0 ) { *fill++ = *t2++; *fill++ = *t2++; }
else t2 += 2;
}
break;
case 1:
while ( i1 > 0 && i2 > 0 ) {
if ( *t1 > *t2 ) {
if ( t2[1] > 0 ) { *fill++ = *t2++; *fill++ = *t2++; }
else t2 += 2;
i2 -=2;
}
else if ( *t1 < *t2 ) {
if ( t1[1] > 0 ) { *fill++ = *t1++; *fill++ = *t1++; }
else t1 += 2;
i1 -= 2;
}
else if ( t1[1] > t2[1] ) {
*fill++ = *t1++; *fill++ = *t1++; t2 += 2;
i1 -= 2; i2 -=2;
}
else {
*fill++ = *t2++; *fill++ = *t2++; t1 += 2;
i1 -= 2; i2 -=2;
}
}
for ( ; i1 > 0; i1 -= 2 ) {
if ( t1[1] > 0 ) { *fill++ = *t1++; *fill++ = *t1++; }
else t1 += 2;
}
for ( ; i2 > 0; i2 -= 2 ) {
if ( t2[1] > 0 ) { *fill++ = *t2++; *fill++ = *t2++; }
else t2 += 2;
}
break;
case 0:
while ( i1 > 0 && i2 > 0 ) {
if ( *t1 > *t2 ) {
t2 += 2; i2 -= 2;
}
else if ( *t1 < *t2 ) {
t1 += 2; i1 -= 2;
}
else if ( ( t1[1] > 0 ) && ( t2[1] < 0 ) ) { t1 += 2; t2 += 2; i1 -= 2; i2 -= 2; }
else if ( ( t1[1] < 0 ) && ( t2[1] > 0 ) ) { t1 += 2; t2 += 2; i1 -= 2; i2 -= 2; }
else if ( t1[1] > 0 ) {
if ( t1[1] < t2[1] ) {
*fill++ = *t1++; *fill++ = *t1++; t2 += 2; i2 -= 2;
}
else {
*fill++ = *t2++; *fill++ = *t2++; t1 += 2; i1 -= 2;
}
}
else {
if ( t2[1] < t1[1] ) {
*fill++ = *t2++; *fill++ = *t2++; t1 += 2; i1 -= 2; i2 -= 2;
}
else {
*fill++ = *t1++; *fill++ = *t1++; t2 += 2; i1 -= 2; i2 -= 2;
}
}
}
for ( ; i1 > 0; i1-- ) *fill++ = *t1++;
for ( ; i2 > 0; i2-- ) *fill++ = *t2++;
break;
}
i1 = new[1] = fill - new;
t2 = new; t1 = old; NCOPY(t1,t2,i1);
TermFree(new,"MergeSymbolLists");
return(0);
}
/*
#] MergeSymbolLists :
#[ MergeDotproductLists :
Merges the extra list into the old.
If par == -1 we take minimum powers
If par == 1 we take maximum powers
If par == 0 we take minimum of the absolute value of the powers
if one is positive and the other negative we get zero.
We assume that the dotproducts are in order in both lists
*/
int MergeDotproductLists(PHEAD WORD *old, WORD *extra, int par)
{
GETBIDENTITY
WORD *new = TermMalloc("MergeDotproductLists");
WORD *t1, *t2, *fill;
int i1,i2;
fill = new + 2;
i1 = old[1] - 2; i2 = extra[1] - 2;
t1 = old + 2; t2 = extra + 2;
switch ( par ) {
case -1:
while ( i1 > 0 && i2 > 0 ) {
if ( ( *t1 > *t2 ) || ( *t1 == *t2 && t1[1] > t2[1] ) ) {
if ( t2[2] < 0 ) { *fill++ = *t2++; *fill++ = *t2++; *fill++ = *t2++; }
else t2 += 3;
}
else if ( ( *t1 < *t2 ) || ( *t1 == *t2 && t1[1] < t2[1] ) ) {
if ( t1[2] < 0 ) { *fill++ = *t1++; *fill++ = *t1++; *fill++ = *t1++; }
else t1 += 3;
}
else if ( t1[2] < t2[2] ) {
*fill++ = *t1++; *fill++ = *t1++; *fill++ = *t1++; t2 += 3;
}
else {
*fill++ = *t2++; *fill++ = *t2++; *fill++ = *t2++; t1 += 3;
}
}
break;
case 1:
while ( i1 > 0 && i2 > 0 ) {
if ( ( *t1 > *t2 ) || ( *t1 == *t2 && t1[1] > t2[1] ) ) {
if ( t2[2] > 0 ) { *fill++ = *t2++; *fill++ = *t2++; *fill++ = *t2++; }
else t2 += 3;
}
else if ( ( *t1 < *t2 ) || ( *t1 == *t2 && t1[1] < t2[1] ) ) {
if ( t1[2] > 0 ) { *fill++ = *t1++; *fill++ = *t1++; *fill++ = *t1++; }
else t1 += 3;
}
else if ( t1[2] > t2[2] ) {
*fill++ = *t1++; *fill++ = *t1++; *fill++ = *t1++; t2 += 3;
}
else {
*fill++ = *t2++; *fill++ = *t2++; *fill++ = *t2++; t1 += 3;
}
}
break;
case 0:
while ( i1 > 0 && i2 > 0 ) {
if ( ( *t1 > *t2 ) || ( *t1 == *t2 && t1[1] > t2[1] ) ) {
t2 += 3;
}
else if ( ( *t1 < *t2 ) || ( *t1 == *t2 && t1[1] < t2[1] ) ) {
t1 += 3;
}
else if ( ( t1[2] > 0 ) && ( t2[2] < 0 ) ) { t1 += 3; t2 += 3; }
else if ( ( t1[2] < 0 ) && ( t2[2] > 0 ) ) { t1 += 3; t2 += 3; }
else if ( t1[2] > 0 ) {
if ( t1[2] < t2[2] ) {
*fill++ = *t1++; *fill++ = *t1++; *fill++ = *t1++; t2 += 3;
}
else {
*fill++ = *t2++; *fill++ = *t2++; *fill++ = *t2++; t1 += 3;
}
}
else {
if ( t2[2] < t1[2] ) {
*fill++ = *t2++; *fill++ = *t2++; *fill++ = *t2++; t1 += 3;
}
else {
*fill++ = *t1++; *fill++ = *t1++; *fill++ = *t1++; t2 += 3;
}
}
}
break;
}
i1 = new[1] = fill - new;
t2 = new; t1 = old; NCOPY(t1,t2,i1);
TermFree(new,"MergeDotproductLists");
return(0);
}
/*
#] MergeDotproductLists :
#[ CreateExpression :
Looks for the expression in the argument, reads it and puts it
in a buffer. Returns the address of the buffer.
We send the expression through the Generator system, because there
may be unsubstituted (sub)expressions as in
Local F = (a+b);
Local G = gcd_(F,...);
*/
WORD *CreateExpression(PHEAD WORD nexp)
{
GETBIDENTITY
CBUF *C = cbuf+AC.cbufnum;
POSITION startposition, oldposition;
FILEHANDLE *fi;
WORD *term, *oldipointer = AR.CompressPointer;
;
switch ( Expressions[nexp].status ) {
case HIDDENLEXPRESSION:
case HIDDENGEXPRESSION:
case DROPHLEXPRESSION:
case DROPHGEXPRESSION:
case UNHIDELEXPRESSION:
case UNHIDEGEXPRESSION:
AR.GetOneFile = 2; fi = AR.hidefile;
break;
default:
AR.GetOneFile = 0; fi = AR.infile;
break;
}
SeekScratch(fi,&oldposition);
startposition = AS.OldOnFile[nexp];
term = AT.WorkPointer;
if ( GetOneTerm(BHEAD term,fi,&startposition,0) <= 0 ) goto CalledFrom;
NewSort(BHEAD0);
AR.CompressPointer = oldipointer;
while ( GetOneTerm(BHEAD term,fi,&startposition,0) > 0 ) {
AT.WorkPointer = term + *term;
if ( Generator(BHEAD term,C->numlhs) ) {
LowerSortLevel();
goto CalledFrom;
}
AR.CompressPointer = oldipointer;
}
AT.WorkPointer = term;
if ( EndSort(BHEAD (WORD *)((void *)(&term)),2) < 0 ) goto CalledFrom;
SetScratch(fi,&oldposition);
return(term);
CalledFrom:
MLOCK(ErrorMessageLock);
MesCall("CreateExpression");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
return(0);
}
/*
#] CreateExpression :
#[ GCDterms : GCD of two terms
Computes the GCD of two terms.
Output in termout.
termout may overlap with term1.
*/
int GCDterms(PHEAD WORD *term1, WORD *term2, WORD *termout)
{
GETBIDENTITY
WORD *t1, *t1stop, *t1next, *t2, *t2stop, *t2next, *tout, *tt1, *tt2;
int count1, count2, i, ii, x1, sign;
WORD length1, length2;
t1 = term1 + *term1; t1stop = t1 - ABS(t1[-1]); t1 = term1+1;
t2 = term2 + *term2; t2stop = t2 - ABS(t2[-1]); t2 = term2+1;
tout = termout+1;
while ( t1 < t1stop ) {
t1next = t1 + t1[1];
t2 = term2+1;
if ( *t1 == SYMBOL ) {
while ( t2 < t2stop && *t2 != SYMBOL ) t2 += t2[1];
if ( t2 < t2stop && *t2 == SYMBOL ) {
t2next = t2+t2[1];
tt1 = t1+2; tt2 = t2+2; count1 = 0;
while ( tt1 < t1next && tt2 < t2next ) {
if ( *tt1 < *tt2 ) tt1 += 2;
else if ( *tt1 > *tt2 ) tt2 += 2;
else if ( ( tt1[1] > 0 && tt2[1] < 0 ) ||
( tt2[1] > 0 && tt1[1] < 0 ) ) {
tt1 += 2; tt2 += 2;
}
else {
x1 = tt1[1];
if ( tt1[1] < 0 ) { if ( tt2[1] > x1 ) x1 = tt2[1]; }
else { if ( tt2[1] < x1 ) x1 = tt2[1]; }
tout[count1+2] = *tt1;
tout[count1+3] = x1;
tt1 += 2; tt2 += 2;
count1 += 2;
}
}
if ( count1 > 0 ) {
*tout = SYMBOL; tout[1] = count1+2; tout += tout[1];
}
}
}
else if ( *t1 == DOTPRODUCT ) {
while ( t2 < t2stop && *t2 != DOTPRODUCT ) t2 += t2[1];
if ( t2 < t2stop && *t2 == DOTPRODUCT ) {
t2next = t2+t2[1];
tt1 = t1+2; tt2 = t2+2; count1 = 0;
while ( tt1 < t1next && tt2 < t2next ) {
if ( *tt1 < *tt2 || ( *tt1 == *tt2 && tt1[1] < tt2[1] ) ) tt1 += 3;
else if ( *tt1 > *tt2 || ( *tt1 == *tt2 && tt1[1] > tt2[1] ) ) tt2 += 3;
else if ( ( tt1[2] > 0 && tt2[2] < 0 ) ||
( tt2[2] > 0 && tt1[2] < 0 ) ) {
tt1 += 3; tt2 += 3;
}
else {
x1 = tt1[2];
if ( tt1[2] < 0 ) { if ( tt2[2] > x1 ) x1 = tt2[2]; }
else { if ( tt2[2] < x1 ) x1 = tt2[2]; }
tout[count1+2] = *tt1;
tout[count1+3] = tt1[1];
tout[count1+4] = x1;
tt1 += 3; tt2 += 3;
count1 += 3;
}
}
if ( count1 > 0 ) {
*tout = DOTPRODUCT; tout[1] = count1+2; tout += tout[1];
}
}
}
else if ( *t1 == VECTOR ) {
while ( t2 < t2stop && *t2 != VECTOR ) t2 += t2[1];
if ( t2 < t2stop && *t2 == VECTOR ) {
t2next = t2+t2[1];
tt1 = t1+2; tt2 = t2+2; count1 = 0;
while ( tt1 < t1next && tt2 < t2next ) {
if ( *tt1 < *tt2 || ( *tt1 == *tt2 && tt1[1] < tt2[1] ) ) tt1 += 2;
else if ( *tt1 > *tt2 || ( *tt1 == *tt2 && tt1[1] > tt2[1] ) ) tt2 += 2;
else {
tout[count1+2] = *tt1;
tout[count1+3] = tt1[1];
tt1 += 2; tt2 += 2;
count1 += 2;
}
}
if ( count1 > 0 ) {
*tout = VECTOR; tout[1] = count1+2; tout += tout[1];
}
}
}
else if ( *t1 == INDEX ) {
while ( t2 < t2stop && *t2 != INDEX ) t2 += t2[1];
if ( t2 < t2stop && *t2 == INDEX ) {
t2next = t2+t2[1];
tt1 = t1+2; tt2 = t2+2; count1 = 0;
while ( tt1 < t1next && tt2 < t2next ) {
if ( *tt1 < *tt2 ) tt1 += 1;
else if ( *tt1 > *tt2 ) tt2 += 1;
else {
tout[count1+2] = *tt1;
tt1 += 1; tt2 += 1;
count1 += 1;
}
}
if ( count1 > 0 ) {
*tout = INDEX; tout[1] = count1+2; tout += tout[1];
}
}
}
else if ( *t1 == DELTA ) {
while ( t2 < t2stop && *t2 != DELTA ) t2 += t2[1];
if ( t2 < t2stop && *t2 == DELTA ) {
t2next = t2+t2[1];
tt1 = t1+2; tt2 = t2+2; count1 = 0;
while ( tt1 < t1next && tt2 < t2next ) {
if ( *tt1 < *tt2 || ( *tt1 == *tt2 && tt1[1] < tt2[1] ) ) tt1 += 2;
else if ( *tt1 > *tt2 || ( *tt1 == *tt2 && tt1[1] > tt2[1] ) ) tt2 += 2;
else {
tout[count1+2] = *tt1;
tout[count1+3] = tt1[1];
tt1 += 2; tt2 += 2;
count1 += 2;
}
}
if ( count1 > 0 ) {
*tout = DELTA; tout[1] = count1+2; tout += tout[1];
}
}
}
else if ( *t1 >= FUNCTION ) { /* noncommuting functions? Forbidden! */
/*
Count how many times this function occurs.
Then count how many times it is in term2.
*/
count1 = 1;
while ( t1next < t1stop && *t1 == *t1next && t1[1] == t1next[1] ) {
for ( i = 2; i < t1[1]; i++ ) {
if ( t1[i] != t1next[i] ) break;
}
if ( i < t1[1] ) break;
count1++;
t1next += t1next[1];
}
count2 = 0;
while ( t2 < t2stop ) {
if ( *t2 == *t1 && t2[1] == t1[1] ) {
for ( i = 2; i < t1[1]; i++ ) {
if ( t2[i] != t1[i] ) break;
}
if ( i >= t1[1] ) count2++;
}
t2 += t2[1];
}
if ( count1 < count2 ) count2 = count1; /* number of common occurrences */
if ( count2 > 0 ) {
if ( tout == t1 ) {
while ( count2 > 0 ) { tout += tout[1]; count2--; }
}
else {
i = t1[1]*count2;
NCOPY(tout,t1,i);
}
}
}
t1 = t1next;
}
/*
Now the coefficients. They are in t1stop and t2stop. Should go to tout.
*/
sign = 1;
length1 = term1[*term1-1]; ii = i = ABS(length1); t1 = t1stop;
if ( t1 != tout ) { NCOPY(tout,t1,i); tout -= ii; }
length2 = term2[*term2-1];
if ( length1 < 0 && length2 < 0 ) sign = -1;
if ( AccumGCD(BHEAD (UWORD *)tout,&length1,(UWORD *)t2stop,length2) ) {
MLOCK(ErrorMessageLock);
MesCall("GCDterms");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
if ( sign < 0 && length1 > 0 ) length1 = -length1;
tout += ABS(length1); tout[-1] = length1;
*termout = tout - termout; *tout = 0;
return(0);
}
/*
#] GCDterms :
#[ ReadPolyRatFun :
*/
int ReadPolyRatFun(PHEAD WORD *term)
{
WORD *oldworkpointer = AT.WorkPointer;
int flag, i;
WORD *t, *fun, *nextt, *num, *den, *t1, *t2, size, numsize, densize;
WORD *term1, *term2, *confree1, *confree2, *gcd, *num1, *den1, move, *newnum, *newden;
WORD *tstop, *m1, *m2;
WORD oldsorttype = AR.SortType;
COMPARE oldcompareroutine = (COMPARE)(AR.CompareRoutine);
AR.SortType = SORTHIGHFIRST;
AR.CompareRoutine = (COMPAREDUMMY)(&CompareSymbols);
tstop = term + *term; tstop -= ABS(tstop[-1]);
if ( term + *term == AT.WorkPointer ) flag = 1;
else flag = 0;
t = term+1;
while ( t < tstop ) {
if ( *t != AR.PolyFun ) { t += t[1]; continue; }
if ( ( t[2] & MUSTCLEANPRF ) == 0 ) { t += t[1]; continue; }
fun = t;
nextt = t + t[1];
if ( fun[1] > FUNHEAD && fun[FUNHEAD] == -SNUMBER && fun[FUNHEAD+1] == 0 )
{ *term = 0; break; }
if ( FromPolyRatFun(BHEAD fun, &num, &den) > 0 ) { t = nextt; continue; }
if ( *num == ARGHEAD ) { *term = 0; break; }
/*
Now we have num and den. Both are in general argument notation,
but can also be used as expressions as in num+ARGHEAD, den+ARGHEAD.
We need the gcd. For this we have to take out the contents
because PreGCD does not like contents.
*/
term1 = TermMalloc("ReadPolyRatFun");
term2 = TermMalloc("ReadPolyRatFun");
confree1 = TakeSymbolContent(BHEAD num+ARGHEAD,term1);
confree2 = TakeSymbolContent(BHEAD den+ARGHEAD,term2);
GCDclean(BHEAD term1,term2);
/* gcd = PreGCD(BHEAD confree1,confree2,1); */
gcd = poly_gcd(BHEAD confree1,confree2,1);
newnum = PolyDiv(BHEAD confree1,gcd,"ReadPolyRatFun");
newden = PolyDiv(BHEAD confree2,gcd,"ReadPolyRatFun");
TermFree(confree2,"ReadPolyRatFun");
TermFree(confree1,"ReadPolyRatFun");
num1 = MULfunc(BHEAD term1,newnum);
den1 = MULfunc(BHEAD term2,newden);
TermFree(newnum,"ReadPolyRatFun");
TermFree(newden,"ReadPolyRatFun");
/* M_free(gcd,"poly_gcd"); */
TermFree(gcd,"poly_gcd");
TermFree(term1,"ReadPolyRatFun");
TermFree(term2,"ReadPolyRatFun");
/*
Now we can put the function back together.
Notice that we cannot use ToFast, because there is no reservation
for the header of the argument. Fortunately there are only two
types of fast arguments.
*/
if ( num1[0] == 4 && num1[4] == 0 && num1[2] == 1 && num1[1] > 0 ) {
numsize = 2; num1[0] = -SNUMBER;
if ( num1[3] < 0 ) num1[1] = -num1[1];
}
else if ( num1[0] == 8 && num1[8] == 0 && num1[7] == 3 && num1[6] == 1
&& num1[5] == 1 && num1[1] == SYMBOL && num1[4] == 1 ) {
numsize = 2; num1[0] = -SYMBOL; num1[1] = num1[3];
}
else { m1 = num1; while ( *m1 ) m1 += *m1; numsize = (m1-num1)+ARGHEAD; }
if ( den1[0] == 4 && den1[4] == 0 && den1[2] == 1 && den1[1] > 0 ) {
densize = 2; den1[0] = -SNUMBER;
if ( den1[3] < 0 ) den1[1] = -den1[1];
}
else if ( den1[0] == 8 && den1[8] == 0 && den1[7] == 3 && den1[6] == 1
&& den1[5] == 1 && den1[1] == SYMBOL && den1[4] == 1 ) {
densize = 2; den1[0] = -SYMBOL; den1[1] = den1[3];
}
else { m2 = den1; while ( *m2 ) m2 += *m2; densize = (m2-den1)+ARGHEAD; }
size = FUNHEAD+numsize+densize;
if ( size > fun[1] ) {
move = size - fun[1];
t1 = term+*term; t2 = t1+move;
while ( t1 > nextt ) *--t2 = *--t1;
tstop += move; nextt += move;
*term += move;
}
else if ( size < fun[1] ) {
move = fun[1]-size;
t2 = fun+size; t1 = nextt;
tstop -= move; nextt -= move;
t = term+*term;
while ( t1 < t ) *t2++ = *t1++;
*term -= move;
}
else { /* no need to move anything */ }
fun[1] = size; fun[2] = 0;
t2 = fun+FUNHEAD; t1 = num1;
if ( *num1 < 0 ) { *t2++ = num1[0]; *t2++ = num1[1]; }
else { *t2++ = numsize; *t2++ = 0; FILLARG(t2);
i = numsize-ARGHEAD; NCOPY(t2,t1,i) }
t1 = den1;
if ( *den1 < 0 ) { *t2++ = den1[0]; *t2++ = den1[1]; }
else { *t2++ = densize; *t2++ = 0; FILLARG(t2);
i = densize-ARGHEAD; NCOPY(t2,t1,i) }
TermFree(num1,"MULfunc");
TermFree(den1,"MULfunc");
t = nextt;
}
if ( flag ) AT.WorkPointer = term +*term;
else AT.WorkPointer = oldworkpointer;
AR.CompareRoutine = (COMPAREDUMMY)oldcompareroutine;
AR.SortType = oldsorttype;
return(0);
}
/*
#] ReadPolyRatFun :
#[ FromPolyRatFun :
*/
int FromPolyRatFun(PHEAD WORD *fun, WORD **numout, WORD **denout)
{
WORD *nextfun, *tt, *num, *den;
int i;
nextfun = fun + fun[1];
fun += FUNHEAD;
num = AT.WorkPointer;
if ( *fun < 0 ) {
if ( *fun != -SNUMBER && *fun != -SYMBOL ) goto Improper;
ToGeneral(fun,num,0);
tt = num + *num; *tt++ = 0;
fun += 2;
}
else { i = *fun; tt = num; NCOPY(tt,fun,i); *tt++ = 0; }
den = tt;
if ( *fun < 0 ) {
if ( *fun != -SNUMBER && *fun != -SYMBOL ) goto Improper;
ToGeneral(fun,den,0);
tt = den + *den; *tt++ = 0;
fun += 2;
}
else { i = *fun; tt = den; NCOPY(tt,fun,i); *tt++ = 0; }
*numout = num; *denout = den;
if ( fun != nextfun ) { return(1); }
AT.WorkPointer = tt;
return(0);
Improper:
MLOCK(ErrorMessageLock);
MesPrint("Improper use of PolyRatFun");
MesCall("FromPolyRatFun");
MUNLOCK(ErrorMessageLock);
SETERROR(-1);
}
/*
#] FromPolyRatFun :
#[ TakeSymbolContent :
*/
/**
* Implements part of the old ExecArg in which we take common factors
* from arguments with more than one term.
* We allow only symbols as this code is used for the polyratfun only.
* We have a special routine, because the generic TakeContent does too
* much work and speed is at a premium here.
* Input: in is the input expression as a sequence of terms.
* Output: term: the content
* return value: the contentfree expression.
* it is in new allocation, made by TermMalloc.
* (should be in a TermMalloc space?)
*/
WORD *TakeSymbolContent(PHEAD WORD *in, WORD *term)
{
GETBIDENTITY
WORD *t, *tstop, *tout, *tstore;
WORD *tnext, *tt, *tterm;
WORD *inp, a, *den, *oldworkpointer = AT.WorkPointer;
int i, action = 0, sign, first;
UWORD *GCDbuffer, *GCDbuffer2, *LCMbuffer, *LCMbuffer2, *ap;
WORD GCDlen, GCDlen2, LCMlen, LCMlen2, length, redlength, len1, len2;
LONG j;
tout = tstore = term+1;
/*
#[ SYMBOL :
We make a list of symbols and their minimal powers.
This includes negative powers. In the end we have to multiply by the
inverse of this list. That takes out all negative powers and leaves
things ready for further processing.
*/
tterm = AT.WorkPointer; tt = tterm+1;
tout[0] = SYMBOL; tout[1] = 2;
t = in; first = 1;
while ( *t ) {
tnext = t + *t; tstop = tnext - ABS(tnext[-1]); t++;
while ( t < tstop ) {
if ( first ) {
if ( *t == SYMBOL ) {
for ( i = 0; i < t[1]; i++ ) tout[i] = t[i];
goto didwork;
}
else {
MLOCK(ErrorMessageLock);
MesPrint ((char*)"ERROR: polynomials and polyratfuns must contain symbols only");
MUNLOCK(ErrorMessageLock);
Terminate(1);
}
}
else if ( *t == SYMBOL ) {
MergeSymbolLists(BHEAD tout,t,-1);
goto didwork;
}
else {
t += t[1];
}
}
/*
Here we come when there were no symbols. Only keep the negative ones.
*/
if ( first == 0 ) {
int j = 2;
for ( i = 2; i < tout[1]; i += 2 ) {
if ( tout[i+1] < 0 ) {
if ( i == j ) { j += 2; }
else { tout[j] = tout[i]; tout[j+1] = tout[i+1]; j += 2; }
}
}
tout[1] = j;
}
didwork:;
first = 0;
t = tnext;
}
if ( tout[1] > 2 ) {
t = tout;
tt[0] = t[0]; tt[1] = t[1];
for ( i = 2; i < t[1]; i += 2 ) {
tt[i] = t[i]; tt[i+1] = -t[i+1];
}
tt += tt[1];
tout += tout[1];
action++;
}
/*
#] SYMBOL :
#[ Coefficient :
Now we have to collect the GCD of the numerators
and the LCM of the denominators.
*/
AT.WorkPointer = tt;
if ( AN.cmod != 0 ) {
WORD x, ix, ip;
t = in; tnext = t + *t; tstop = tnext - ABS(tnext[-1]);
x = tstop[0];
if ( tnext[-1] < 0 ) x += AC.cmod[0];
if ( GetModInverses(x,(WORD)(AN.cmod[0]),&ix,&ip) ) goto CalledFrom;
*tout++ = x; *tout++ = 1; *tout++ = 3;
*tt++ = ix; *tt++ = 1; *tt++ = 3;
}
else {
GCDbuffer = NumberMalloc("MakeInteger");
GCDbuffer2 = NumberMalloc("MakeInteger");
LCMbuffer = NumberMalloc("MakeInteger");
LCMbuffer2 = NumberMalloc("MakeInteger");
t = in;
tnext = t + *t; length = tnext[-1];
if ( length < 0 ) { sign = -1; length = -length; }
else { sign = 1; }
tstop = tnext - length;
redlength = (length-1)/2;
for ( i = 0; i < redlength; i++ ) {
GCDbuffer[i] = (UWORD)(tstop[i]);
LCMbuffer[i] = (UWORD)(tstop[redlength+i]);
}
GCDlen = LCMlen = redlength;
while ( GCDbuffer[GCDlen-1] == 0 ) GCDlen--;
while ( LCMbuffer[LCMlen-1] == 0 ) LCMlen--;
t = tnext;
while ( *t ) {
tnext = t + *t; length = ABS(tnext[-1]);
tstop = tnext - length; redlength = (length-1)/2;
len1 = len2 = redlength;
den = tstop + redlength;
while ( tstop[len1-1] == 0 ) len1--;
while ( den[len2-1] == 0 ) len2--;
if ( GCDlen == 1 && GCDbuffer[0] == 1 ) {}
else {
GcdLong(BHEAD (UWORD *)tstop,len1,GCDbuffer,GCDlen,GCDbuffer2,&GCDlen2);
ap = GCDbuffer; GCDbuffer = GCDbuffer2; GCDbuffer2 = ap;
a = GCDlen; GCDlen = GCDlen2; GCDlen2 = a;
}
if ( len2 == 1 && den[0] == 1 ) {}
else {
GcdLong(BHEAD LCMbuffer,LCMlen,(UWORD *)den,len2,LCMbuffer2,&LCMlen2);
DivLong((UWORD *)den,len2,LCMbuffer2,LCMlen2,
GCDbuffer2,&GCDlen2,(UWORD *)AT.WorkPointer,&a);
MulLong(LCMbuffer,LCMlen,GCDbuffer2,GCDlen2,LCMbuffer2,&LCMlen2);
ap = LCMbuffer; LCMbuffer = LCMbuffer2; LCMbuffer2 = ap;
a = LCMlen; LCMlen = LCMlen2; LCMlen2 = a;
}
t = tnext;
}
if ( GCDlen != 1 || GCDbuffer[0] != 1 || LCMlen != 1 || LCMbuffer[0] != 1 ) {
redlength = GCDlen; if ( LCMlen > GCDlen ) redlength = LCMlen;
for ( i = 0; i < GCDlen; i++ ) *tout++ = (WORD)(GCDbuffer[i]);
for ( ; i < redlength; i++ ) *tout++ = 0;
for ( i = 0; i < LCMlen; i++ ) *tout++ = (WORD)(LCMbuffer[i]);
for ( ; i < redlength; i++ ) *tout++ = 0;
*tout++ = (2*redlength+1)*sign;
for ( i = 0; i < LCMlen; i++ ) *tt++ = (WORD)(LCMbuffer[i]);
for ( ; i < redlength; i++ ) *tt++ = 0;
for ( i = 0; i < GCDlen; i++ ) *tt++ = (WORD)(GCDbuffer[i]);
for ( ; i < redlength; i++ ) *tt++ = 0;
*tt++ = (2*redlength+1)*sign;
action++;
}
else {
*tout++ = 1; *tout++ = 1; *tout++ = 3*sign;
*tt++ = 1; *tt++ = 1; *tt++ = 3*sign;
if ( sign != 1 ) action++;
}
NumberFree(LCMbuffer2,"MakeInteger");
NumberFree(LCMbuffer ,"MakeInteger");
NumberFree(GCDbuffer2,"MakeInteger");
NumberFree(GCDbuffer ,"MakeInteger");
}
/*
#] Coefficient :
#[ Multiply by the inverse content :
*/
if ( action ) {
*term = tout - term; *tout = 0;
*tterm = tt - tterm; *tt = 0;
AT.WorkPointer = tt;
inp = MultiplyWithTerm(BHEAD in,tterm,2);
AT.WorkPointer = tterm;
t = inp; while ( *t ) t += *t;
j = (t-inp); t = inp;
if ( j*sizeof(WORD) > (size_t)(AM.MaxTer) ) goto OverWork;
in = tout = TermMalloc("TakeSymbolContent");
NCOPY(tout,t,j); *tout = 0;
if ( AN.tryterm > 0 ) { TermFree(inp,"MultiplyWithTerm"); AN.tryterm = 0; }
else { M_free(inp,"MultiplyWithTerm"); }
}
else {
t = in; while ( *t ) t += *t;
j = (t-in); t = in;
if ( j*sizeof(WORD) > (size_t)(AM.MaxTer) ) goto OverWork;
in = tout = TermMalloc("TakeSymbolContent");
NCOPY(tout,t,j); *tout = 0;
term[0] = 4; term[1] = 1; term[2] = 1; term[3] = 3; term[4] = 0;
}
/*
#] Multiply by the inverse content :
AT.WorkPointer = tterm + *tterm;
*/
AT.WorkPointer = oldworkpointer;
return(in);
OverWork:
MLOCK(ErrorMessageLock);
MesPrint("Term too complex. Maybe increasing MaxTermSize can help");
MUNLOCK(ErrorMessageLock);
CalledFrom:
MLOCK(ErrorMessageLock);
MesCall("TakeSymbolContent");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
return(0);
}
/*
#] TakeSymbolContent :
#[ GCDclean :
Takes a numerator and a denominator that each consist of a
single term with only a coefficient and symbols and makes them
into a proper fraction. Output overwrites input.
*/
void GCDclean(PHEAD WORD *num, WORD *den)
{
WORD *out1 = TermMalloc("GCDclean");
WORD *out2 = TermMalloc("GCDclean");
WORD *t1, *t2, *r1, *r2, *t1stop, *t2stop, csize1, csize2, csize3, pow, sign;
int i;
t1stop = num+*num; sign = ( t1stop[-1] < 0 ) ? -1 : 1;
csize1 = ABS(t1stop[-1]); t1stop -= csize1;
t2stop = den+*den; if ( t2stop[-1] < 0 ) sign = -sign;
csize2 = ABS(t2stop[-1]); t2stop -= csize2;
t1 = num+1; t2 = den+1;
r1 = out1+3; r2 = out2+3;
if ( t1 == t1stop ) {
if ( t2 < t2stop ) {
for ( i = 2; i < t2[1]; i += 2 ) {
if ( t2[i+1] < 0 ) { *r1++ = t2[i]; *r1++ = -t2[i+1]; }
else { *r2++ = t2[i]; *r2++ = t2[i+1]; }
}
}
}
else if ( t2 == t2stop ) {
for ( i = 2; i < t1[1]; i += 2 ) {
if ( t1[i+1] < 0 ) { *r2++ = t1[i]; *r2++ = -t1[i+1]; }
else { *r1++ = t1[i]; *r1++ = t1[i+1]; }
}
}
else {
t1 += 2; t2 += 2;
while ( t1 < t1stop && t2 < t2stop ) {
if ( *t1 < *t2 ) {
if ( t1[1] > 0 ) { *r1++ = *t1; *r1++ = t1[1]; t1 += 2; }
else if ( t1[1] < 0 ) { *r2++ = *t1; *r2++ = -t1[1]; t1 += 2; }
}
else if ( *t1 > *t2 ) {
if ( t2[1] > 0 ) { *r2++ = *t2; *r2++ = t2[1]; t2 += 2; }
else if ( t2[1] < 0 ) { *r1++ = *t2; *r1++ = -t2[1]; t2 += 2; }
}
else {
pow = t1[1]-t2[1];
if ( pow > 0 ) { *r1++ = *t1; *r1++ = pow; }
else if ( pow < 0 ) { *r2++ = *t1; *r2++ = -pow; }
t1 += 2; t2 += 2;
}
}
while ( t1 < t1stop ) {
if ( t1[1] < 0 ) { *r2++ = *t1; *r2++ = -t1[1]; }
else { *r1++ = *t1; *r1++ = t1[1]; }
t1 += 2;
}
while ( t2 < t2stop ) {
if ( t2[1] < 0 ) { *r1++ = *t2; *r1++ = -t2[1]; }
else { *r2++ = *t2; *r2++ = t2[1]; }
t2 += 2;
}
}
if ( r1 > out1+3 ) { out1[1] = SYMBOL; out1[2] = r1 - out1 - 1; }
else r1 = out1+1;
if ( r2 > out2+3 ) { out2[1] = SYMBOL; out2[2] = r2 - out2 - 1; }
else r2 = out2+1;
/*
Now the coefficients.
*/
csize1 = REDLENG(csize1);
csize2 = REDLENG(csize2);
if ( DivRat(BHEAD (UWORD *)t1stop,csize1,(UWORD *)t2stop,csize2,(UWORD *)r1,&csize3) ) {
MLOCK(ErrorMessageLock);
MesCall("GCDclean");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
UnPack((UWORD *)r1,csize3,&csize2,&csize1);
t2 = r1+ABS(csize3);
for ( i = 0; i < csize2; i++ ) r2[i] = t2[i];
r2 += csize2; *r2++ = 1;
for ( i = 1; i < csize2; i++ ) *r2++ = 0;
csize2 = INCLENG(csize2); *r2++ = csize2; *out2 = r2-out2;
r1 += ABS(csize1); *r1++ = 1;
for ( i = 1; i < ABS(csize1); i++ ) *r1++ = 0;
csize1 = INCLENG(csize1); *r1++ = csize1; *out1 = r1-out1;
t1 = num; t2 = out1; i = *out1; NCOPY(t1,t2,i); *t1 = 0;
if ( sign < 0 ) t1[-1] = -t1[-1];
t1 = den; t2 = out2; i = *out2; NCOPY(t1,t2,i); *t1 = 0;
TermFree(out2,"GCDclean");
TermFree(out1,"GCDclean");
}
/*
#] GCDclean :
#[ PolyDiv :
Special stub function for polynomials that should fit inside a term.
We make sure that the space is allocated by TermMalloc.
This makes things much easier on the calling routines.
*/
WORD *PolyDiv(PHEAD WORD *a,WORD *b,char *text)
{
/*
Probably the following would work now
*/
DUMMYUSE(text);
return(poly_div(BHEAD a,b,1));
/*
WORD *quo, *qq;
WORD *x, *xx;
LONG i;
quo = poly_div(BHEAD a,b,1);
x = TermMalloc(text);
qq = quo; while ( *qq ) qq += *qq;
i = (qq-quo+1);
if ( i*sizeof(WORD) > (size_t)(AM.MaxTer) ) {
DUMMYUSE(text);
MLOCK(ErrorMessageLock);
MesPrint("PolyDiv: Term too complex. Maybe increasing MaxTermSize can help");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
xx = x; qq = quo;
NCOPY(xx,qq,i)
TermFree(quo,"poly_div");
return(x);
*/
}
/*
#] PolyDiv :
#] GCDfunction :
#[ DIVfunction :
Input: a div_ function that has two arguments inside a term.
Action: Calculates [arg1/arg2] using polynomial techniques if needed.
Output: The output result is combined with the remainder of the term
and sent to Generator for further processing.
Note that the output can be just a number or many terms.
In case par == 0 the output is [arg1/arg2]
In case par == 1 the output is [arg1%arg2]
In case par == 2 the output is [inverse of arg1 modulus arg2]
In case par == 3 the output is [arg1*arg2]
*/
WORD divrem[4] = { DIVFUNCTION, REMFUNCTION, INVERSEFUNCTION, MULFUNCTION };
int DIVfunction(PHEAD WORD *term,WORD level,int par)
{
GETBIDENTITY
WORD *t, *tt, *r, *arg1 = 0, *arg2 = 0, *arg3 = 0, *termout;
WORD *tstop, *tend, *r3, *rr, *rstop, tlength, rlength, newlength;
WORD *proper1, *proper2, *proper3 = 0, numdol = -1;
int numargs = 0, type1, type2, actionflag1, actionflag2;
WORD startebuf = cbuf[AT.ebufnum].numrhs;
int division = ( par <= 2 ); /* false for mul_ */
if ( par < 0 || par > 3 ) {
MLOCK(ErrorMessageLock);
MesPrint("Internal error. Illegal parameter %d in DIVfunction.",par);
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
/*
Find the function
*/
tend = term + *term; tstop = tend - ABS(tend[-1]);
t = term+1;
while ( t < tstop ) {
if ( *t != divrem[par] ) { t += t[1]; continue; }
r = t + FUNHEAD;
tt = t + t[1]; numargs = 0;
while ( r < tt ) {
if ( numargs == 0 ) { arg1 = r; }
if ( numargs == 1 ) { arg2 = r; }
if ( numargs == 2 && *r == -DOLLAREXPRESSION ) { numdol = r[1]; }
numargs++;
NEXTARG(r);
}
if ( numargs == 2 ) break;
if ( division && numargs == 3 ) break;
t = tt;
}
if ( t >= tstop ) {
MLOCK(ErrorMessageLock);
MesPrint("Internal error. Indicated div_ or rem_ function not encountered.");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
/*
We have two arguments in arg1 and arg2.
*/
if ( division && *arg1 == -SNUMBER && arg1[1] == 0 ) {
if ( *arg2 == -SNUMBER && arg2[1] == 0 ) {
zerozero:;
MLOCK(ErrorMessageLock);
MesPrint("0/0 in either div_ or rem_ function.");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
if ( numdol >= 0 ) PutTermInDollar(0,numdol);
return(0);
}
if ( division && *arg2 == -SNUMBER && arg2[1] == 0 ) {
divzero:;
MLOCK(ErrorMessageLock);
MesPrint("Division by zero in either div_ or rem_ function.");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
if ( !division ) {
if ( (*arg1 == -SNUMBER && arg1[1] == 0) ||
(*arg2 == -SNUMBER && arg2[1] == 0) ) {
return(0);
}
}
if ( ( arg1 = ConvertArgument(BHEAD arg1, &type1) ) == 0 ) goto CalledFrom;
if ( ( arg2 = ConvertArgument(BHEAD arg2, &type2) ) == 0 ) goto CalledFrom;
if ( division && *arg1 == 0 ) {
if ( *arg2 == 0 ) {
M_free(arg2,"DIVfunction");
M_free(arg1,"DIVfunction");
goto zerozero;
}
M_free(arg2,"DIVfunction");
M_free(arg1,"DIVfunction");
if ( numdol >= 0 ) PutTermInDollar(0,numdol);
return(0);
}
if ( division && *arg2 == 0 ) {
M_free(arg2,"DIVfunction");
M_free(arg1,"DIVfunction");
goto divzero;
}
if ( !division && (*arg1 == 0 || *arg2 == 0) ) {
M_free(arg2,"DIVfunction");
M_free(arg1,"DIVfunction");
return(0);
}
if ( ( proper1 = PutExtraSymbols(BHEAD arg1,startebuf,&actionflag1) ) == 0 ) goto CalledFrom;
if ( ( proper2 = PutExtraSymbols(BHEAD arg2,startebuf,&actionflag2) ) == 0 ) goto CalledFrom;
/*
if ( type2 == 0 ) M_free(arg2,"DIVfunction");
else {
DOLLARS d = ((DOLLARS)arg2)-1;
if ( d->factors ) M_free(d->factors,"Dollar factors");
M_free(d,"Copy of dollar variable");
}
*/
M_free(arg2,"DIVfunction");
/*
if ( type1 == 0 ) M_free(arg1,"DIVfunction");
else {
DOLLARS d = ((DOLLARS)arg1)-1;
if ( d->factors ) M_free(d->factors,"Dollar factors");
M_free(d,"Copy of dollar variable");
}
*/
M_free(arg1,"DIVfunction");
if ( par == 0 ) proper3 = poly_div(BHEAD proper1, proper2,0);
else if ( par == 1 ) proper3 = poly_rem(BHEAD proper1, proper2,0);
else if ( par == 2 ) proper3 = poly_inverse(BHEAD proper1, proper2);
else if ( par == 3 ) proper3 = poly_mul(BHEAD proper1, proper2);
if ( proper3 == 0 ) goto CalledFrom;
if ( actionflag1 || actionflag2 ) {
if ( ( arg3 = TakeExtraSymbols(BHEAD proper3,startebuf) ) == 0 ) goto CalledFrom;
M_free(proper3,"DIVfunction");
}
else {
arg3 = proper3;
}
M_free(proper2,"DIVfunction");
M_free(proper1,"DIVfunction");
cbuf[AT.ebufnum].numrhs = startebuf;
if ( *arg3 ) {
termout = AT.WorkPointer;
tlength = tend[-1];
tlength = REDLENG(tlength);
r3 = arg3;
while ( *r3 ) {
tt = term + 1; rr = termout + 1;
while ( tt < t ) *rr++ = *tt++;
r = r3 + 1;
r3 = r3 + *r3;
rstop = r3 - ABS(r3[-1]);
while ( r < rstop ) *rr++ = *r++;
tt += t[1];
while ( tt < tstop ) *rr++ = *tt++;
rlength = r3[-1];
rlength = REDLENG(rlength);
if ( MulRat(BHEAD (UWORD *)tstop,tlength,(UWORD *)rstop,rlength,
(UWORD *)rr,&newlength) < 0 ) goto CalledFrom;
rlength = INCLENG(newlength);
rr += ABS(rlength);
rr[-1] = rlength;
*termout = rr - termout;
AT.WorkPointer = rr;
if ( Generator(BHEAD termout,level) ) goto CalledFrom;
}
AT.WorkPointer = termout;
}
M_free(arg3,"DIVfunction");
return(0);
CalledFrom:
MLOCK(ErrorMessageLock);
MesCall("DIVfunction");
MUNLOCK(ErrorMessageLock);
SETERROR(-1)
}
/*
#] DIVfunction :
#[ MULfunc :
Multiplies two polynomials and puts the results in TermMalloc space.
*/
WORD *MULfunc(PHEAD WORD *p1, WORD *p2)
{
WORD *prod,size1,size2,size3,*t,*tfill,*ps1,*ps2,sign1,sign2, error, *p3;
UWORD *num1, *num2, *num3;
int i;
WORD oldsorttype = AR.SortType;
COMPARE oldcompareroutine = (COMPARE)(AR.CompareRoutine);
AR.SortType = SORTHIGHFIRST;
AR.CompareRoutine = (COMPAREDUMMY)(&CompareSymbols);
num3 = NumberMalloc("MULfunc");
prod = TermMalloc("MULfunc");
NewSort(BHEAD0);
while ( *p1 ) {
ps1 = p1+*p1; num1 = (UWORD *)(ps1 - ABS(ps1[-1])); size1 = ps1[-1];
if ( size1 < 0 ) { sign1 = -1; size1 = -size1; }
else sign1 = 1;
size1 = (size1-1)/2;
p3 = p2;
while ( *p3 ) {
ps2 = p3+*p3; num2 = (UWORD *)(ps2 - ABS(ps2[-1])); size2 = ps2[-1];
if ( size2 < 0 ) { sign2 = -1; size2 = -size2; }
else sign2 = 1;
size2 = (size2-1)/2;
if ( MulLong(num1,size1,num2,size2,num3,&size3) ) {
error = 1;
CalledFrom:
MLOCK(ErrorMessageLock);
MesPrint(" Error %d",error);
MesCall("MulFunc");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
tfill = prod+1;
t = p1+1; while ( t < (WORD *)num1 ) *tfill++ = *t++;
t = p3+1; while ( t < (WORD *)num2 ) *tfill++ = *t++;
t = (WORD *)num3;
for ( i = 0; i < size3; i++ ) *tfill++ = *t++;
*tfill++ = 1;
for ( i = 1; i < size3; i++ ) *tfill++ = 0;
*tfill++ = (2*size3+1)*sign1*sign2;
prod[0] = tfill - prod;
if ( SymbolNormalize(prod) ) { error = 2; goto CalledFrom; }
if ( StoreTerm(BHEAD prod) ) { error = 3; goto CalledFrom; }
p3 += *p3;
}
p1 += *p1;
}
NumberFree(num3,"MULfunc");
EndSort(BHEAD prod,1);
AR.CompareRoutine = (COMPAREDUMMY)oldcompareroutine;
AR.SortType = oldsorttype;
return(prod);
}
/*
#] MULfunc :
#[ ConvertArgument :
Converts an argument to a general notation in allocated space.
*/
WORD *ConvertArgument(PHEAD WORD *arg, int *type)
{
WORD *output, *t, *r;
int i, size;
if ( *arg > 0 ) {
output = (WORD *)Malloc1((*arg)*sizeof(WORD),"ConvertArgument");
i = *arg - ARGHEAD; t = arg + ARGHEAD; r = output;
NCOPY(r,t,i);
*r = 0; *type = 0;
return(output);
}
if ( *arg == -EXPRESSION ) {
*type = 0;
return(CreateExpression(BHEAD arg[1]));
}
if ( *arg == -DOLLAREXPRESSION ) {
DOLLARS d;
*type = 1;
d = DolToTerms(BHEAD arg[1]);
/*
The problem is that DolToTerms creates a copy of the dollar variable.
If we just return d->where we create a memory leak. Hence we have to
copy the contents of d->where to a new buffer
*/
output = (WORD *)Malloc1((d->size+1)*sizeof(WORD),"Copy of dollar content");
WCOPY(output,d->where,d->size+1);
if ( d->factors ) { M_free(d->factors,"Dollar factors"); d->factors = 0; }
M_free(d,"Copy of dollar variable");
return(output);
}
#if ( FUNHEAD > 4 )
size = FUNHEAD+5;
#else
size = 9;
#endif
output = (WORD *)Malloc1(size*sizeof(WORD),"ConvertArgument");
switch(*arg) {
case -SYMBOL:
output[0] = 8; output[1] = SYMBOL; output[2] = 4; output[3] = arg[1];
output[4] = 1; output[5] = 1; output[6] = 1; output[7] = 3;
output[8] = 0;
break;
case -INDEX:
case -VECTOR:
case -MINVECTOR:
output[0] = 7; output[1] = INDEX; output[2] = 3; output[3] = arg[1];
output[4] = 1; output[5] = 1;
if ( *arg == -MINVECTOR ) output[6] = -3;
else output[6] = 3;
output[7] = 0;
break;
case -SNUMBER:
output[0] = 4;
if ( arg[1] < 0 ) {
output[1] = -arg[1]; output[2] = 1; output[3] = -3;
}
else {
output[1] = arg[1]; output[2] = 1; output[3] = 3;
}
output[4] = 0;
break;
default:
output[0] = FUNHEAD+4;
output[1] = -*arg;
output[2] = FUNHEAD;
for ( i = 3; i <= FUNHEAD; i++ ) output[i] = 0;
output[FUNHEAD+1] = 1;
output[FUNHEAD+2] = 1;
output[FUNHEAD+3] = 3;
output[FUNHEAD+4] = 0;
break;
}
*type = 0;
return(output);
}
/*
#] ConvertArgument :
#[ ExpandRat :
Expands the denominator of a PolyRatFun in the variable PolyFunVar.
The output is a polyratfun with a single argument.
In the case that there is a polyratfun with more than one argument
or the dirtyflag is on, the argument(s) is/are normalized.
The output overwrites the input.
*/
char *TheErrorMessage[] = {
"PolyRatFun not of a type that FORM will expand: incorrect variable inside."
,"Division by zero in PolyRatFun encountered in ExpandRat."
,"Irregular code in PolyRatFun encountered in ExpandRat."
,"Called from ExpandRat."
,"WorkSpace overflow. Change parameter WorkSpace in setup file?"
};
int ExpandRat(PHEAD WORD *fun)
{
WORD *r, *rr, *rrr, *tt, *tnext, *arg1, *arg2, *rmin = 0, *rmininv;
WORD *rcoef, rsize, rcopy, *ow = AT.WorkPointer;
WORD *numerator, *denominator, *rnext;
WORD *thecopy, *rc, ncoef, newcoef, *m, *mm, nco, *outarg = 0;
UWORD co[2], co1[2], co2[2];
WORD OldPolyFunPow = AR.PolyFunPow;
int i, j, minpow = 0, eppow, first, error = 0, ipoly;
if ( fun[1] == FUNHEAD ) { return(0); }
tnext = fun + fun[1];
if ( fun[1] == fun[FUNHEAD]+FUNHEAD ) { /* Single argument */
if ( fun[2] == 0 ) { goto done; }
/*
We have to normalize the argument. This could make it shorter.
*/
NormArg:;
if ( outarg == 0 ) outarg = TermMalloc("ExpandRat")+ARGHEAD;
AT.TrimPower = 1;
NewSort(BHEAD0);
r = fun+FUNHEAD+ARGHEAD;
if ( AR.PolyFunExp == 2 ) { /* Find minimum power */
WORD minpow2 = MAXPOWER, *rrm;
rrm = r;
while ( rrm < tnext ) {
if ( *rrm == 4 ) {
if ( minpow2 > 0 ) minpow2 = 0;
}
else if ( ABS(rrm[*rrm-1]) == (*rrm-1) ) {
if ( minpow2 > 0 ) minpow2 = 0;
}
else {
if ( rrm[1] == SYMBOL && rrm[2] == 4 && rrm[3] == AR.PolyFunVar ) {
if ( rrm[4] < minpow2 ) minpow2 = rrm[4];
}
else {
MesPrint("Illegal term in expanded polyratfun.");
goto onerror;
}
}
rrm += *rrm;
}
AR.PolyFunPow += minpow2;
}
while ( r < tnext ) {
rr = r + *r;
i = *r; rrr = outarg; NCOPY(rrr,r,i);
Normalize(BHEAD outarg);
if ( *outarg > 0 ) StoreTerm(BHEAD outarg);
}
r = fun+FUNHEAD+ARGHEAD;
EndSort(BHEAD r,1);
AT.TrimPower = 0;
if ( *r == 0 ) {
fun[FUNHEAD] = -SNUMBER; fun[FUNHEAD+1] = 0;
fun[1] = FUNHEAD+2;
}
else {
rr = fun+FUNHEAD;
if ( ToFast(rr,rr) ) {
NEXTARG(rr); fun[1] = rr - fun;
}
else {
while ( *r ) r += *r;
*rr = r-rr; rr[1] = CLEANFLAG;
fun[1] = r - fun;
}
}
fun[2] = CLEANFLAG;
goto done;
}
/*
First test whether we have only AR.PolyFunVar in the denominator
*/
tt = fun + FUNHEAD;
arg1 = arg2 = 0;
if ( tt < tnext ) {
arg1 = tt; NEXTARG(tt);
if ( tt < tnext ) {
arg2 = tt; NEXTARG(tt);
if ( tt != tnext ) { arg1 = arg2 = 0; } /* more than two arguments */
}
}
if ( arg2 == 0 ) {
if ( *arg1 < 0 ) { fun[2] = CLEANFLAG; goto done; }
if ( fun[2] == CLEANFLAG ) goto done;
goto NormArg; /* Note: should not come here */
}
/*
Produce the output argument in outarg
*/
if ( outarg == 0 ) outarg = TermMalloc("ExpandRat")+ARGHEAD;
if ( *arg2 <= 0 ) {
/*
These cases are trivial.
We try as much as possible to write the output directly into the
function. We just have to be extremely careful not to overwrite
relevant information before we are finished with it.
*/
if ( *arg2 == -SYMBOL && arg2[1] == AR.PolyFunVar ) {
rr = r = fun+FUNHEAD+ARGHEAD;
if ( *arg1 < 0 ) {
if ( *arg1 == -SYMBOL ) {
if ( arg1[1] == AR.PolyFunVar ) {
*r++ = 4; *r++ = 1; *r++ = 1; *r++ = 3; *r = 0;
}
else {
*r++ = 10; *r++ = SYMBOL; *r++ = 6;
*r++ = arg1[1]; *r++ = 1;
*r++ = AR.PolyFunVar; *r++ = -1;
*r++ = 1; *r++ = 1; *r++ = 3; *r = 0;
Normalize(BHEAD rr);
}
}
else if ( *arg1 == -SNUMBER ) {
nco = arg1[1];
if ( nco == 0 ) { *r++ = 0; }
else {
*r++ = 8; *r++ = SYMBOL; *r++ = 4;
*r++ = AR.PolyFunVar; *r++ = -1;
*r++ = ABS(nco); *r++ = 1;
if ( nco < 0 ) *r++ = -3;
else *r++ = 3;
*r = 0;
}
}
else { error = 2; goto onerror; } /* should not happen! */
}
else { /* Multi-term numerator. */
m = arg1+ARGHEAD;
NewSort(BHEAD0); /* Technically maybe not needed */
if ( AR.PolyFunExp == 2 ) { /* Find minimum power */
WORD minpow2 = MAXPOWER, *rrm;
rrm = m;
while ( rrm < arg2 ) {
if ( *rrm == 4 ) {
if ( minpow2 > 0 ) minpow2 = 0;
}
else if ( ABS(rrm[*rrm-1]) == (*rrm-1) ) {
if ( minpow2 > 0 ) minpow2 = 0;
}
else {
if ( rrm[1] == SYMBOL && rrm[2] == 4 && rrm[3] == AR.PolyFunVar ) {
if ( rrm[4] < minpow2 ) minpow2 = rrm[4];
}
else {
MesPrint("Illegal term in expanded polyratfun.");
goto onerror;
}
}
rrm += *rrm;
}
AR.PolyFunPow += minpow2-1;
}
while ( m < arg2 ) {
r = outarg;
rrr = r++; mm = m + *m;
*r++ = SYMBOL; *r++ = 4; *r++ = AR.PolyFunVar; *r++ = -1;
m++; while ( m < mm ) *r++ = *m++;
*rrr = r-rrr;
Normalize(BHEAD rrr);
StoreTerm(BHEAD rrr);
}
EndSort(BHEAD rr,1);
r = rr; while ( *r ) r += *r;
}
if ( *rr == 0 ) {
fun[FUNHEAD] = -SNUMBER; fun[FUNHEAD+1] = CLEANFLAG;
fun[1] = FUNHEAD+2;
}
else {
rr = fun+FUNHEAD;
*rr = r-rr;
rr[1] = CLEANFLAG;
if ( ToFast(rr,rr) ) {
NEXTARG(rr);
fun[1] = rr - fun;
}
else { fun[1] = r - fun; }
}
fun[2] = CLEANFLAG;
goto done;
}
else if ( *arg2 == -SNUMBER ) {
rr = r = outarg;
if ( arg2[1] == 0 ) { error = 1; goto onerror; }
if ( *arg1 == -SNUMBER ) { /* Things may not be normalized */
if ( arg1[1] == 0 ) { *r++ = 0; }
else {
co1[0] = ABS(arg1[1]); co1[1] = 1;
co2[0] = 1; co2[1] = ABS(arg2[1]);
MulRat(BHEAD co1,1,co2,1,co,&nco);
*r++ = 4; *r++ = (WORD)(co[0]); *r++ = (WORD)(co[1]);
if ( ( arg1[1] < 0 && arg2[1] > 0 ) ||
( arg1[1] > 0 && arg2[1] < 0 ) ) *r++ = -3;
else *r++ = 3;
*r = 0;
}
}
else if ( *arg1 == -SYMBOL ) {
*r++ = 8; *r++ = SYMBOL; *r++ = 4;
*r++ = arg1[1]; *r++ = 1;
*r++ = 1; *r++ = ABS(arg2[1]);
if ( arg2[1] < 0 ) *r++ = -3;
else *r++ = 3;
*r = 0;
}
else if ( *arg1 < 0 ) { error = 2; goto onerror; }
else { /* Multi-term numerator. */
m = arg1+ARGHEAD;
NewSort(BHEAD0); /* Technically maybe not needed */
if ( AR.PolyFunExp == 2 ) { /* Find minimum power */
WORD minpow2 = MAXPOWER, *rrm;
rrm = m;
while ( rrm < arg2 ) {
if ( *rrm == 4 ) {
if ( minpow2 > 0 ) minpow2 = 0;
}
else if ( ABS(rrm[*rrm-1]) == (*rrm-1) ) {
if ( minpow2 > 0 ) minpow2 = 0;
}
else {
if ( rrm[1] == SYMBOL && rrm[2] == 4 && rrm[3] == AR.PolyFunVar ) {
if ( rrm[4] < minpow2 ) minpow2 = rrm[4];
}
else {
MesPrint("Illegal term in expanded polyratfun.");
goto onerror;
}
}
rrm += *rrm;
}
AR.PolyFunPow += minpow2;
}
while ( m < arg2 ) {
r = rr;
rrr = r++; mm = m + *m;
*r++ = DENOMINATOR; *r++ = FUNHEAD + 2; *r++ = DIRTYFLAG;
FILLFUN3(r);
*r++ = arg2[0]; *r++ = arg2[1];
m++; while ( m < mm ) *r++ = *m++;
*rrr = r-rrr;
if ( r < AT.WorkTop && r >= AT.WorkSpace )
AT.WorkPointer = r;
Normalize(BHEAD rrr);
if ( ABS(rrr[*rrr-1]) == *rrr-1 ) {
if ( AR.PolyFunPow >= 0 ) {
StoreTerm(BHEAD rrr);
}
}
else if ( rrr[1] == SYMBOL && rrr[2] == 4 &&
rrr[3] == AR.PolyFunVar && rrr[4] <= AR.PolyFunPow ) {
StoreTerm(BHEAD rrr);
}
}
EndSort(BHEAD rr,1);
}
r = rr; while ( *r ) r += *r;
i = r-rr;
r = fun + FUNHEAD + ARGHEAD;
NCOPY(r,rr,i);
rr = fun + FUNHEAD;
*rr = r - rr; rr[1] = CLEANFLAG;
if ( ToFast(rr,rr) ) {
NEXTARG(rr);
fun[1] = rr - fun;
}
else { fun[1] = r - fun; }
fun[2] = CLEANFLAG;
goto done;
}
else { error = 0; goto onerror; }
}
else {
r = arg2+ARGHEAD; /* The argument ends at tnext */
first = 1;
while ( r < tnext ) {
rr = r + *r; rr -= ABS(rr[-1]);
if ( r+1 == rr ) {
if ( first ) { minpow = 0; first = 0; rmin = r; }
else if ( minpow > 0 ) { minpow = 0; rmin = r; }
}
else if ( r[1] != SYMBOL || r[2] != 4 || r[3] != AR.PolyFunVar
|| r[4] > MAXPOWER ) { error = 0; goto onerror; }
else if ( first ) { minpow = r[4]; first = 0; rmin = r; }
else if ( r[4] < minpow ) { minpow = r[4]; rmin = r; }
r += *r;
}
/*
We have now:
1: a numerator in arg1 which can contain several variables.
2: a denominator in arg2 with at most only AR.PolyFunVar (ep).
3: the minimum power in the denominator is minpow and the
term with that minimum power is in rmin.
Divide numerator and denominator by this minimum power.
Determine the power range in the numerator.
Call InvPoly.
Multiply by the inverse in such a way that we never take more
powers of ep than necessary.
*/
/*
One: put 1/rmin in AT.WorkPointer -> rmininv
*/
AT.WorkPointer += AM.MaxTer/sizeof(WORD);
if ( AT.WorkPointer + (AM.MaxTer/sizeof(WORD)) >= AT.WorkTop ) {
error = 4; goto onerror;
}
rmininv = r = AT.WorkPointer;
rr = rmin; i = *rmin; NCOPY(r,rr,i)
if ( minpow != 0 ) { rmininv[4] = -rmininv[4]; }
rsize = ABS(r[-1]);
rcoef = r - rsize;
rsize = (rsize-1)/2; rr = rcoef + rsize;
for ( i = 0; i < rsize; i++ ) {
rcopy = rcoef[i]; rcoef[i] = rr[i]; rr[i] = rcopy;
}
AT.WorkPointer = r;
if ( *arg1 < 0 ) {
ToGeneral(arg1,r,0);
arg1 = r; r += *r; *r++ = 0; rcopy = 0;
AT.WorkPointer = r;
}
else {
r = arg1 + *arg1;
rcopy = *r; *r++ = 0;
}
/*
We can use MultiplyWithTerm.
*/
AT.LeaveNegative = 1;
numerator = MultiplyWithTerm(BHEAD arg1+ARGHEAD,rmininv,0);
AT.LeaveNegative = 0;
r[-1] = rcopy;
r = numerator; while ( *r ) r += *r;
AT.WorkPointer = r+1;
rcopy = arg2[*arg2]; arg2[*arg2] = 0;
denominator = MultiplyWithTerm(BHEAD arg2+ARGHEAD,rmininv,1);
arg2[*arg2] = rcopy;
r = denominator; while ( *r ) r += *r;
AT.WorkPointer = r+1;
/*
Now find the minimum power of ep in the numerator.
*/
r = numerator;
first = 1;
while ( *r ) {
rr = r + *r; rr -= ABS(rr[-1]);
if ( r+1 == rr ) {
if ( first ) { minpow = 0; first = 0; }
else if ( minpow > 0 ) { minpow = 0; }
}
else if ( r[1] != SYMBOL ) { error = 0; goto onerror; }
else {
for ( i = 3; i < r[2]; i += 2 ) {
if ( r[i] == AR.PolyFunVar ) {
if ( first ) { minpow = r[i+1]; first = 0; }
else if ( r[i+1] < minpow ) minpow = r[i+1];
break;
}
}
if ( i >= r[2] ) {
if ( first ) { minpow = 0; first = 0; }
else if ( minpow > 0 ) minpow = 0;
}
}
r += *r;
}
/*
We can invert the denominator.
Note that the return value is an offset in AT.pWorkSpace.
Hence there is no need to free memory afterwards.
*/
if ( AR.PolyFunExp == 3 ) {
ipoly = InvPoly(BHEAD denominator,AR.PolyFunPow-minpow,AR.PolyFunVar);
}
else {
ipoly = InvPoly(BHEAD denominator,AR.PolyFunPow,AR.PolyFunVar);
}
/*
Now we start the multiplying
*/
NewSort(BHEAD0);
r = numerator;
while ( *r ) {
/*
1: Find power of ep.
*/
rnext = r + *r;
rrr = rnext - ABS(rnext[-1]);
rr = r+1;
eppow = 0;
if ( rr < rrr ) {
j = rr[1] - 2; rr += 2;
while ( j > 0 ) {
if ( *rr == AR.PolyFunVar ) { eppow = rr[1]; break; }
j -= 2; rr += 2;
}
}
/*
2: Multiply by the proper terms in ipoly
*/
for ( i = 0; i <= AR.PolyFunPow-eppow+minpow; i++ ) {
if ( AT.pWorkSpace[ipoly+i] == 0 ) continue;
/*
Copy the term, add i to the power of ep and multiply coef.
*/
rc = r;
rr = thecopy = AT.WorkPointer;
while ( rc < rrr ) *rr++ = *rc++;
if ( i != 0 ) {
*rr++ = SYMBOL; *rr++ = 4; *rr++ = AR.PolyFunVar; *rr++ = i;
}
ncoef = REDLENG(rnext[-1]);
MulRat(BHEAD (UWORD *)rrr,ncoef,
(UWORD *)(AT.pWorkSpace[ipoly+i])+1,AT.pWorkSpace[ipoly+i][0]
,(UWORD *)rr,&newcoef);
ncoef = ABS(newcoef); rr += 2*ncoef;
newcoef = INCLENG(newcoef);
*rr++ = newcoef;
*thecopy = rr - thecopy;
AT.WorkPointer = rr;
Normalize(BHEAD thecopy);
if ( *thecopy > 0 ) StoreTerm(BHEAD thecopy);
AT.WorkPointer = thecopy;
}
r = rnext;
}
/*
Now we have all.
*/
rr = fun + FUNHEAD; r = rr + ARGHEAD;
EndSort(BHEAD r,1);
if ( *r == 0 ) {
fun[1] = FUNHEAD+2; fun[2] = CLEANFLAG;
fun[FUNHEAD] = -SNUMBER; fun[FUNHEAD+1] = 0;
}
else {
while ( *r ) r += *r;
rr[0] = r-rr; rr[1] = CLEANFLAG;
if ( ToFast(rr,rr) ) { NEXTARG(rr); fun[1] = rr-fun; }
else { fun[1] = r-fun; }
fun[2] = CLEANFLAG;
}
}
done:
if ( outarg ) TermFree(outarg-ARGHEAD,"ExpandRat");
AR.PolyFunPow = OldPolyFunPow;
AT.WorkPointer = ow;
AN.PolyNormFlag = 1;
return(0);
onerror:
if ( outarg ) TermFree(outarg-ARGHEAD,"ExpandRat");
AR.PolyFunPow = OldPolyFunPow;
AT.WorkPointer = ow;
MLOCK(ErrorMessageLock);
MesPrint(TheErrorMessage[error]);
MUNLOCK(ErrorMessageLock);
Terminate(-1);
return(-1);
}
/*
#] ExpandRat :
#[ InvPoly :
The input polynomial is represented as a sequence of terms in ascending
power. The first coefficient is 1. If we call this 1-a and
a = sum_(j,1,n,x^j*a(j)), and b = 1/(1-a) we can find the coefficients
of b with the recursion
b(0) = 1, b(n) = sum_(j,1,n,a(j)*b(n-j))
The variable is the symbol sym and we need maxpow powers in the answer.
The answer is an array of pointers to the coefficients of the various
powers as rational numbers in the notation signedsize,numerator,denominator
We put these powers in the workspace and the answer is in AT.pWorkSpace.
Hence the return value is an offset in the pWorkSpace.
A zero pointer indicates that this coefficient is zero.
*/
int InvPoly(PHEAD WORD *inpoly, WORD maxpow, WORD sym)
{
int needed, inpointers, outpointers, maxinput = 0, i, j;
WORD *t, *tt, *ttt, *w, *c, *cc, *ccc, lenc, lenc1, lenc2, rc, *c1, *c2;
/*
Step 0: allocate the space
*/
needed = (maxpow+1)*2;
WantAddPointers(needed);
inpointers = AT.pWorkPointer;
outpointers = AT.pWorkPointer+maxpow+1;
for ( i = 0; i < needed; i++ ) AT.pWorkSpace[inpointers+i] = 0;
/*
Step 1: determine the coefficients in inpoly
often there is a maximum power that is much smaller than maxpow.
keeping track of this can speed up things.
*/
t = inpoly;
w = AT.WorkPointer;
while ( *t ) {
if ( *t == 4 ) {
if ( t[1] != 1 || t[2] != 1 || t[3] != 3 ) goto onerror;
AT.pWorkSpace[inpointers] = 0;
}
else if ( t[1] != SYMBOL || t[2] != 4 || t[3] != sym || t[4] < 0 ) goto onerror;
else if ( t[4] > maxpow ) {} /* power outside useful range */
else {
if ( t[4] > maxinput ) maxinput = t[4];
AT.pWorkSpace[inpointers+t[4]] = w;
tt = t + *t; rc = -*--tt; /* we need - the coefficient! */
rc = REDLENG(rc); *w++ = rc;
ttt = t+5;
while ( ttt < tt ) *w++ = *ttt++;
}
t += *t;
}
/*
Step 2: compute the output. b(0) = 1.
then the recursion starts.
*/
AT.pWorkSpace[outpointers] = w;
*w++ = 1; *w++ = 1; *w++ = 1;
c = TermMalloc("InvPoly");
c1 = TermMalloc("InvPoly");
c2 = TermMalloc("InvPoly");
for ( j = 1; j <= maxpow; j++ ) {
/*
Start at c = a(j)*b(0) = a(j)
*/
if ( ( cc = AT.pWorkSpace[inpointers+j] ) != 0 ) {
lenc = *cc++; /* reduced length */
i = 2*ABS(lenc); ccc = c;
NCOPY(ccc,cc,i);
}
else { lenc = 0; }
for ( i = MiN(j-1,maxinput); i > 0; i-- ) {
/*
c -> c + a(i)*b(j-i)
*/
if ( AT.pWorkSpace[inpointers+i] == 0
|| AT.pWorkSpace[outpointers+j-i] == 0 ) {
}
else {
if ( MulRat(BHEAD (UWORD *)(AT.pWorkSpace[inpointers+i]+1),AT.pWorkSpace[inpointers+i][0],
(UWORD *)(AT.pWorkSpace[outpointers+j-i]+1),AT.pWorkSpace[outpointers+j-i][0],
(UWORD *)c1,&lenc1) ) goto calcerror;
if ( lenc == 0 ) {
cc = c; c = c1; c1 = cc;
lenc = lenc1;
}
else {
if ( AddRat(BHEAD (UWORD *)c,lenc,(UWORD *)c1,lenc1,(UWORD *)c2,&lenc2) )
goto calcerror;
cc = c; c = c2; c2 = cc;
lenc = lenc2;
}
}
}
/*
Copy c to the proper location
*/
if ( lenc == 0 ) AT.pWorkSpace[outpointers+j] = 0;
else {
AT.pWorkSpace[outpointers+j] = w;
*w++ = lenc;
i = 2*ABS(lenc); ccc = c;
NCOPY(w,ccc,i);
}
}
AT.WorkPointer = w;
TermFree(c2,"InvPoly");
TermFree(c1,"InvPoly");
TermFree(c ,"InvPoly");
return(outpointers);
onerror:
MLOCK(ErrorMessageLock);
MesPrint("Incorrect symbol field in InvPoly.");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
return(-1);
calcerror:
MLOCK(ErrorMessageLock);
MesPrint("Called from InvPoly.");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
return(-1);
}
/*
#] InvPoly :
*/
|