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
|
@menu
* Introduction to Function Definition::
* Function::
* Macros::
* Functions and Variables for Function Definition::
@end menu
@c -----------------------------------------------------------------------------
@node Introduction to Function Definition, Function, Function Definition, Function Definition
@section Introduction to Function Definition
@c -----------------------------------------------------------------------------
@c -----------------------------------------------------------------------------
@node Function, Macros, Introduction to Function Definition, Function Definition
@c NEEDS WORK, THIS TOPIC IS IMPORTANT
@c MENTION DYNAMIC SCOPE (VS LEXICAL SCOPE)
@section Function
@c -----------------------------------------------------------------------------
@opencatbox{Categories:}
@category{Function definition}
@category{Programming}
@closecatbox
@c -----------------------------------------------------------------------------
@subsection Ordinary functions
@c -----------------------------------------------------------------------------
To define a function in Maxima you use the @code{:=} operator.
E.g.
@example
f(x) := sin(x)
@end example
@noindent
defines a function @code{f}.
Anonymous functions may also be created using @code{lambda}.
For example
@example
lambda ([i, j], ...)
@end example
@noindent
can be used instead of @code{f}
where
@example
f(i,j) := block ([], ...);
map (lambda ([i], i+1), l)
@end example
@noindent
would return a list with 1 added to each term.
You may also define a function with a variable number of arguments,
by having a final argument which is assigned to a list of the extra
arguments:
@c ===beg===
@c f ([u]) := u;
@c f (1, 2, 3, 4);
@c f (a, b, [u]) := [a, b, u];
@c f (1, 2, 3, 4, 5, 6);
@c ===end===
@example
(%i1) f ([u]) := u;
(%o1) f([u]) := u
(%i2) f (1, 2, 3, 4);
(%o2) [1, 2, 3, 4]
(%i3) f (a, b, [u]) := [a, b, u];
(%o3) f(a, b, [u]) := [a, b, u]
(%i4) f (1, 2, 3, 4, 5, 6);
(%o4) [1, 2, [3, 4, 5, 6]]
@end example
The right hand side of a function is an expression. Thus
if you want a sequence of expressions, you do
@example
f(x) := (expr1, expr2, ...., exprn);
@end example
and the value of @var{exprn} is what is returned by the function.
If you wish to make a @code{return} from some expression inside the
function then you must use @code{block} and @code{return}.
@example
block ([], expr1, ..., if (a > 10) then return(a), ..., exprn)
@end example
is itself an expression, and so could take the place of the
right hand side of a function definition. Here it may happen
that the return happens earlier than the last expression.
@c COPY THIS STUFF TO @defun block AS NEEDED
@c ESPECIALLY STUFF ABOUT LOCAL VARIABLES
The first @code{[]} in the block, may contain a list of variables and
variable assignments, such as @code{[a: 3, b, c: []]}, which would cause the
three variables @code{a},@code{b},and @code{c} to not refer to their
global values, but rather have these special values for as long as the
code executes inside the @code{block}, or inside functions called from
inside the @code{block}. This is called @i{dynamic} binding, since the
variables last from the start of the block to the time it exits. Once
you return from the @code{block}, or throw out of it, the old values (if
any) of the variables will be restored. It is certainly a good idea
to protect your variables in this way. Note that the assignments
in the block variables, are done in parallel. This means, that if
you had used @code{c: a} in the above, the value of @code{c} would
have been the value of @code{a} at the time you just entered the block,
but before @code{a} was bound. Thus doing something like
@example
block ([a: a], expr1, ... a: a+3, ..., exprn)
@end example
will protect the external value of @code{a} from being altered, but
would let you access what that value was. Thus the right hand
side of the assignments, is evaluated in the entering context, before
any binding occurs.
Using just @code{block ([x], ...)} would cause the @code{x} to have itself
as value, just as if it would have if you entered a fresh Maxima
session.
The actual arguments to a function are treated in exactly same way as
the variables in a block. Thus in
@example
f(x) := (expr1, ..., exprn);
@end example
and
@example
f(1);
@end example
we would have a similar context for evaluation of the expressions
as if we had done
@example
block ([x: 1], expr1, ..., exprn)
@end example
Inside functions, when the right hand side of a definition,
may be computed at runtime, it is useful to use @code{define} and
possibly @code{buildq}.
@anchor{memoizing function}
@anchor{memoizing functions}
@anchor{Memoizing function}
@anchor{Memoizing functions}
@c -----------------------------------------------------------------------------
@subsection Memoizing Functions
@c -----------------------------------------------------------------------------
A @i{memoizing function} caches the result the first time it is called with a
given argument, and returns the stored value, without recomputing it, when that
same argument is given. Memoizing functions are often called
@i{array function} and are in fact handled like arrays in many ways:
The names of memoizing functions are appended to the global list @code{arrays}
(not the global list @code{functions}). @code{arrayinfo} returns the list of
arguments for which there are stored values, and @code{listarray} returns the
stored values. @code{dispfun} and @code{fundef} return the array function
definition.
@code{arraymake} constructs an array function call,
analogous to @code{funmake} for ordinary functions.
@code{arrayapply} applies an array function to its arguments,
analogous to @code{apply} for ordinary functions.
There is nothing exactly analogous to @code{map} for array functions,
although @code{map(lambda([@var{x}], @var{a}[@var{x}]), @var{L})} or
@code{makelist(@var{a}[@var{x}], @var{x}, @var{L})}, where @var{L} is a list,
are not too far off the mark.
@code{remarray} removes an array function definition (including any stored
function values), analogous to @code{remfunction} for ordinary functions.
@code{kill(@var{a}[@var{x}])} removes the value of the array function @var{a}
stored for the argument @var{x};
the next time @var{a} is called with argument @var{x},
the function value is recomputed.
However, there is no way to remove all of the stored values at once,
except for @code{kill(@var{a})} or @code{remarray(@var{a})},
which also remove the function definition.
Examples
If evaluating the function needs much time and only a limited number of points
is ever evaluated (which means not much time is spent looking up results in a
long list of cached results) Memoizing functions can speed up calculations
considerably.
@c ===beg===
@c showtime:true$
@c a[x]:=float(sum(sin(x*t),t,1,10000));
@c a[1];
@c a[1];
@c ===end===
@example
@group
(%i1) showtime:true$
Evaluation took 0.0000 seconds (0.0000 elapsed) using 0 bytes.
@end group
@group
(%i2) a[x]:=float(sum(sin(x*t),t,1,10000));
Evaluation took 0.0000 seconds (0.0000 elapsed) using 0 bytes.
(%o2) a := float(sum(sin(x t), t, 1, 10000))
x
@end group
@group
(%i3) a[1];
Evaluation took 5.1250 seconds (5.1260 elapsed) using 775.250 MB.
(%o3) 1.633891021792447
@end group
@group
(%i4) a[1];
Evaluation took 0.0000 seconds (0.0000 elapsed) using 0 bytes.
(%o4) 1.633891021792447
@end group
@end example
As the memoizing function is only evaluated once for each input value
changes in variables the memoizing function uses are not considered
for values that are already cached:
@c ===beg===
@c a[x]:=b*x;
@c b:1;
@c a[2];
@c b:2;
@c a[1];
@c a[2];
@c ===end===
@example
@group
(%i1) a[x]:=b*x;
(%o1) a := b x
x
@end group
@group
(%i2) b:1;
(%o2) 1
@end group
@group
(%i3) a[2];
(%o3) 2
@end group
@group
(%i4) b:2;
(%o4) 2
@end group
@group
(%i5) a[1];
(%o5) 2
@end group
@group
(%i6) a[2];
(%o6) 2
@end group
@end example
@c -----------------------------------------------------------------------------
@node Macros, Functions and Variables for Function Definition, Function, Function Definition
@section Macros
@c -----------------------------------------------------------------------------
@c -----------------------------------------------------------------------------
@anchor{buildq}
@deffn {Function} buildq (@var{L}, @var{expr})
Substitutes variables named by the list @var{L} into the expression @var{expr},
in parallel, without evaluating @var{expr}. The resulting expression is
simplified, but not evaluated, after @code{buildq} carries out the substitution.
The elements of @var{L} are symbols or assignment expressions
@code{@var{symbol}: @var{value}}, evaluated in parallel. That is, the binding
of a variable on the right-hand side of an assignment is the binding of that
variable in the context from which @code{buildq} was called, not the binding of
that variable in the variable list @var{L}. If some variable in @var{L} is not
given an explicit assignment, its binding in @code{buildq} is the same as in
the context from which @code{buildq} was called.
Then the variables named by @var{L} are substituted into @var{expr} in parallel.
That is, the substitution for every variable is determined before any
substitution is made, so the substitution for one variable has no effect on any
other.
If any variable @var{x} appears as @code{splice (@var{x})} in @var{expr},
then @var{x} must be bound to a list,
and the list is spliced (interpolated) into @var{expr} instead of substituted.
Any variables in @var{expr} not appearing in @var{L} are carried into the result
verbatim, even if they have bindings in the context from which @code{buildq}
was called.
Examples
@code{a} is explicitly bound to @code{x}, while @code{b} has the same binding
(namely 29) as in the calling context, and @code{c} is carried through verbatim.
The resulting expression is not evaluated until the explicit evaluation
@code{''%}.
@c ===beg===
@c (a: 17, b: 29, c: 1729)$
@c buildq ([a: x, b], a + b + c);
@c ''%;
@c ===end===
@example
(%i1) (a: 17, b: 29, c: 1729)$
@group
(%i2) buildq ([a: x, b], a + b + c);
(%o2) x + c + 29
@end group
@group
(%i3) ''%;
(%o3) x + 1758
@end group
@end example
@code{e} is bound to a list, which appears as such in the arguments of
@code{foo}, and interpolated into the arguments of @code{bar}.
@c ===beg===
@c buildq ([e: [a, b, c]], foo (x, e, y));
@c buildq ([e: [a, b, c]], bar (x, splice (e), y));
@c ===end===
@example
@group
(%i1) buildq ([e: [a, b, c]], foo (x, e, y));
(%o1) foo(x, [a, b, c], y)
@end group
@group
(%i2) buildq ([e: [a, b, c]], bar (x, splice (e), y));
(%o2) bar(x, a, b, c, y)
@end group
@end example
The result is simplified after substitution. If simplification were applied
before substitution, these two results would be the same.
@c ===beg===
@c buildq ([e: [a, b, c]], splice (e) + splice (e));
@c buildq ([e: [a, b, c]], 2 * splice (e));
@c ===end===
@example
@group
(%i1) buildq ([e: [a, b, c]], splice (e) + splice (e));
(%o1) 2 c + 2 b + 2 a
@end group
@group
(%i2) buildq ([e: [a, b, c]], 2 * splice (e));
(%o2) 2 a b c
@end group
@end example
The variables in @var{L} are bound in parallel; if bound sequentially,
the first result would be @code{foo (b, b)}.
Substitutions are carried out in parallel;
compare the second result with the result of @code{subst},
which carries out substitutions sequentially.
@c ===beg===
@c buildq ([a: b, b: a], foo (a, b));
@c buildq ([u: v, v: w, w: x, x: y, y: z, z: u],
@c bar (u, v, w, x, y, z));
@c subst ([u=v, v=w, w=x, x=y, y=z, z=u],
@c bar (u, v, w, x, y, z));
@c ===end===
@example
@group
(%i1) buildq ([a: b, b: a], foo (a, b));
(%o1) foo(b, a)
@end group
@group
(%i2) buildq ([u: v, v: w, w: x, x: y, y: z, z: u],
bar (u, v, w, x, y, z));
(%o2) bar(v, w, x, y, z, u)
@end group
@group
(%i3) subst ([u=v, v=w, w=x, x=y, y=z, z=u],
bar (u, v, w, x, y, z));
(%o3) bar(u, u, u, u, u, u)
@end group
@end example
Construct a list of equations with some variables or expressions on the
left-hand side and their values on the right-hand side. @code{macroexpand}
shows the expression returned by @code{show_values}.
@c ===beg===
@c show_values ([L]) ::= buildq ([L], map ("=", 'L, L));
@c (a: 17, b: 29, c: 1729)$
@c show_values (a, b, c - a - b);
@c macroexpand (show_values (a, b, c - a - b));
@c ===end===
@example
@group
(%i1) show_values ([L]) ::= buildq ([L], map ("=", 'L, L));
(%o1) show_values([L]) ::= buildq([L], map("=", 'L, L))
@end group
(%i2) (a: 17, b: 29, c: 1729)$
@group
(%i3) show_values (a, b, c - a - b);
(%o3) [a = 17, b = 29, c - b - a = 1683]
@end group
@group
(%i4) macroexpand (show_values (a, b, c - a - b));
(%o4) map(=, '([a, b, c - b - a]), [a, b, c - b - a])
@end group
@end example
Given a function of several arguments,
create another function for which some of the arguments are fixed.
@c ===beg===
@c curry (f, [a]) :=
@c buildq ([f, a], lambda ([[x]], apply (f, append (a, x))))$
@c by3 : curry ("*", 3);
@c by3 (a + b);
@c ===end===
@example
@group
(%i1) curry (f, [a]) :=
buildq ([f, a], lambda ([[x]], apply (f, append (a, x))))$
@end group
@group
(%i2) by3 : curry ("*", 3);
(%o2) lambda([[x]], apply(*, append([3], x)))
@end group
@group
(%i3) by3 (a + b);
(%o3) 3 (b + a)
@end group
@end example
@opencatbox{Categories:}
@category{Function definition}
@closecatbox
@end deffn
@c -----------------------------------------------------------------------------
@anchor{macroexpand}
@deffn {Function} macroexpand (@var{expr})
Returns the macro expansion of @var{expr} without evaluating it,
when @code{expr} is a macro function call.
Otherwise, @code{macroexpand} returns @var{expr}.
If the expansion of @var{expr} yields another macro function call,
that macro function call is also expanded.
@code{macroexpand} quotes its argument.
However, if the expansion of a macro function call has side effects,
those side effects are executed.
See also @mrefcomma{::=} @mrefcomma{macros} and @mrefdot{macroexpand1}.
Examples
@c ===beg===
@c g (x) ::= x / 99;
@c h (x) ::= buildq ([x], g (x - a));
@c a: 1234;
@c macroexpand (h (y));
@c h (y);
@c ===end===
@example
@group
(%i1) g (x) ::= x / 99;
x
(%o1) g(x) ::= --
99
@end group
@group
(%i2) h (x) ::= buildq ([x], g (x - a));
(%o2) h(x) ::= buildq([x], g(x - a))
@end group
@group
(%i3) a: 1234;
(%o3) 1234
@end group
@group
(%i4) macroexpand (h (y));
y - a
(%o4) -----
99
@end group
@group
(%i5) h (y);
y - 1234
(%o5) --------
99
@end group
@end example
@opencatbox{Categories:}
@category{Function application}
@closecatbox
@end deffn
@c -----------------------------------------------------------------------------
@anchor{macroexpand1}
@deffn {Function} macroexpand1 (@var{expr})
Returns the macro expansion of @var{expr} without evaluating it,
when @code{expr} is a macro function call.
Otherwise, @code{macroexpand1} returns @var{expr}.
@code{macroexpand1} quotes its argument.
However, if the expansion of a macro function call has side effects,
those side effects are executed.
If the expansion of @var{expr} yields another macro function call,
that macro function call is not expanded.
See also @mrefcomma{::=} @mrefcomma{macros} and @mrefdot{macroexpand}
Examples
@c ===beg===
@c g (x) ::= x / 99;
@c h (x) ::= buildq ([x], g (x - a));
@c a: 1234;
@c macroexpand1 (h (y));
@c h (y);
@c ===end===
@example
@group
(%i1) g (x) ::= x / 99;
x
(%o1) g(x) ::= --
99
@end group
@group
(%i2) h (x) ::= buildq ([x], g (x - a));
(%o2) h(x) ::= buildq([x], g(x - a))
@end group
@group
(%i3) a: 1234;
(%o3) 1234
@end group
@group
(%i4) macroexpand1 (h (y));
(%o4) g(y - a)
@end group
@group
(%i5) h (y);
y - 1234
(%o5) --------
99
@end group
@end example
@opencatbox{Categories:}
@category{Function application}
@closecatbox
@end deffn
@c -----------------------------------------------------------------------------
@anchor{macros}
@defvr {Global variable} macros
Default value: @code{[]}
@code{macros} is the list of user-defined macro functions.
The macro function definition operator @code{::=} puts a new macro function
onto this list, and @code{kill}, @code{remove}, and @code{remfunction} remove
macro functions from the list.
See also @mrefdot{infolists}
@opencatbox{Categories:}
@category{Function definition}
@category{Global variables}
@closecatbox
@end defvr
@c -----------------------------------------------------------------------------
@anchor{splice}
@deffn {Function} splice (@var{a})
Splices (interpolates) the list named by the atom @var{a} into an expression,
but only if @code{splice} appears within @code{buildq};
otherwise, @code{splice} is treated as an undefined function.
If appearing within @code{buildq} as @var{a} alone (without @code{splice}),
@var{a} is substituted (not interpolated) as a list into the result.
The argument of @code{splice} can only be an atom;
it cannot be a literal list or an expression which yields a list.
Typically @code{splice} supplies the arguments for a function or operator.
For a function @code{f}, the expression @code{f (splice (@var{a}))} within
@code{buildq} expands to @code{f (@var{a}[1], @var{a}[2], @var{a}[3], ...)}.
For an operator @code{o}, the expression @code{"o" (splice (@var{a}))} within
@code{buildq} expands to @code{"o" (@var{a}[1], @var{a}[2], @var{a}[3], ...)},
where @code{o} may be any type of operator (typically one which takes multiple
arguments). Note that the operator must be enclosed in double quotes @code{"}.
Examples
@c ===beg===
@c buildq ([x: [1, %pi, z - y]], foo (splice (x)) / length (x));
@c buildq ([x: [1, %pi]], "/" (splice (x)));
@c matchfix ("<>", "<>");
@c buildq ([x: [1, %pi, z - y]], "<>" (splice (x)));
@c ===end===
@example
@group
(%i1) buildq ([x: [1, %pi, z - y]], foo (splice (x)) / length (x));
foo(1, %pi, z - y)
(%o1) -----------------------
length([1, %pi, z - y])
@end group
@group
(%i2) buildq ([x: [1, %pi]], "/" (splice (x)));
1
(%o2) ---
%pi
@end group
@group
(%i3) matchfix ("<>", "<>");
(%o3) <>
@end group
@group
(%i4) buildq ([x: [1, %pi, z - y]], "<>" (splice (x)));
(%o4) <>1, %pi, z - y<>
@end group
@end example
@opencatbox{Categories:}
@category{Function definition}
@closecatbox
@end deffn
@c end concepts Function Definition
@c -----------------------------------------------------------------------------
@node Functions and Variables for Function Definition, , Macros, Function Definition
@section Functions and Variables for Function Definition
@c -----------------------------------------------------------------------------
@c -----------------------------------------------------------------------------
@anchor{apply}
@deffn {Function} apply (@var{F}, [@var{x_1}, @dots{}, @var{x_n}])
Constructs and evaluates an expression @code{@var{F}(@var{arg_1}, ...,
@var{arg_n})}.
@code{apply} does not attempt to distinguish a @mref{memoizing function} from an ordinary
function; when @var{F} is the name of a memoizing function, @code{apply} evaluates
@code{@var{F}(...)} (that is, a function call with parentheses instead of square
brackets). @code{arrayapply} evaluates a function call with square brackets in
this case.
See also @mref{funmake} and @mrefdot{args}
Examples:
@code{apply} evaluates its arguments.
In this example, @code{min} is applied to the value of @code{L}.
@c ===beg===
@c L : [1, 5, -10.2, 4, 3];
@c apply (min, L);
@c ===end===
@example
@group
(%i1) L : [1, 5, -10.2, 4, 3];
(%o1) [1, 5, - 10.2, 4, 3]
@end group
@group
(%i2) apply (min, L);
(%o2) - 10.2
@end group
@end example
@code{apply} evaluates arguments, even if the function @var{F} quotes them.
@c ===beg===
@c F (x) := x / 1729;
@c fname : F;
@c dispfun (F);
@c dispfun (fname);
@c apply (dispfun, [fname]);
@c ===end===
@example
@group
(%i1) F (x) := x / 1729;
x
(%o1) F(x) := ----
1729
@end group
@group
(%i2) fname : F;
(%o2) F
@end group
@group
(%i3) dispfun (F);
x
(%t3) F(x) := ----
1729
(%o3) [%t3]
@end group
@group
(%i4) dispfun (fname);
fundef: no such function: fname
-- an error. To debug this try: debugmode(true);
@end group
@group
(%i5) apply (dispfun, [fname]);
x
(%t5) F(x) := ----
1729
(%o5) [%t5]
@end group
@end example
@code{apply} evaluates the function name @var{F}.
Single quote @code{'} defeats evaluation.
@code{demoivre} is the name of a global variable and also a function.
@c ===beg===
@c demoivre;
@c demoivre (exp (%i * x));
@c apply (demoivre, [exp (%i * x)]);
@c apply ('demoivre, [exp (%i * x)]);
@c ===end===
@example
@group
(%i1) demoivre;
(%o1) false
@end group
@group
(%i2) demoivre (exp (%i * x));
(%o2) %i sin(x) + cos(x)
@end group
@group
(%i3) apply (demoivre, [exp (%i * x)]);
apply: found false where a function was expected.
-- an error. To debug this try: debugmode(true);
@end group
@group
(%i4) apply ('demoivre, [exp (%i * x)]);
(%o4) %i sin(x) + cos(x)
@end group
@end example
How to convert a nested list into a matrix:
@c ===beg===
@c a:[[1,2],[3,4]];
@c apply(matrix,a);
@c ===end===
@example
@group
(%i1) a:[[1,2],[3,4]];
(%o1) [[1, 2], [3, 4]]
@end group
@group
(%i2) apply(matrix,a);
[ 1 2 ]
(%o2) [ ]
[ 3 4 ]
@end group
@end example
@opencatbox{Categories:}
@category{Function application}
@closecatbox
@end deffn
@c -----------------------------------------------------------------------------
@anchor{block}
@deffn {Function} block @
@fname{block} ([@var{v_1}, @dots{}, @var{v_m}], @var{expr_1}, @dots{}, @var{expr_n}) @
@fname{block} (@var{expr_1}, @dots{}, @var{expr_n})
The function @code{block} allows to make the variables @var{v_1}, @dots{},
@var{v_m} to be local for a sequence of commands. If these variables
are already bound @code{block} saves the current values of the
variables @var{v_1}, @dots{}, @var{v_m} (if any) upon entry to the
block, then unbinds the variables so that they evaluate to themselves;
The local variables may be bound to arbitrary values within the block
but when the block is exited the saved values are restored, and the
values assigned within the block are lost.
If there is no need to define local variables then the list at the
beginning of the @code{block} command may be omitted.
In this case if neither @mref{return} nor @mref{go} are used
@code{block} behaves similar to the following construct:
@example
( expr_1, expr_2,... , expr_n );
@end example
@var{expr_1}, @dots{}, @var{expr_n} will be evaluated in sequence and
the value of the last expression will be returned. The sequence can be
modified by the @code{go}, @code{throw}, and @code{return} functions. The last
expression is @var{expr_n} unless @code{return} or an expression containing
@code{throw} is evaluated.
The declaration @code{local(@var{v_1}, ..., @var{v_m})} within @code{block}
saves the properties associated with the symbols @var{v_1}, @dots{}, @var{v_m},
removes any properties before evaluating other expressions, and restores any
saved properties on exit from the block. Some declarations are implemented as
properties of a symbol, including @code{:=}, @code{array}, @code{dependencies},
@code{atvalue}, @code{matchdeclare}, @code{atomgrad}, @code{constant},
@code{nonscalar}, @code{assume}, and some others. The effect of @code{local}
is to make such declarations effective only within the block; otherwise
declarations within a block are actually global declarations.
@code{block} may appear within another @code{block}.
Local variables are established each time a new @code{block} is evaluated.
Local variables appear to be global to any enclosed blocks.
If a variable is non-local in a block,
its value is the value most recently assigned by an enclosing block, if any,
otherwise, it is the value of the variable in the global environment.
This policy may coincide with the usual understanding of "dynamic scope".
The value of the block is the value of the last statement or the
value of the argument to the function @code{return} which may be used to exit
explicitly from the block. The function @code{go} may be used to transfer
control to the statement of the block that is tagged with the argument
to @code{go}. To tag a statement, precede it by an atomic argument as
another statement in the block. For example:
@code{block ([x], x:1, loop, x: x+1, ..., go(loop), ...)}. The argument to
@code{go} must be the name of a tag appearing within the block. One cannot use
@code{go} to transfer to a tag in a block other than the one containing the
@code{go}.
Blocks typically appear on the right side of a function definition
but can be used in other places as well.
See also @mref{return} and @mrefdot{go}
@c Needs some examples.
@opencatbox{Categories:}
@category{Expressions}
@category{Programming}
@closecatbox
@end deffn
@c REPHRASE, NEEDS EXAMPLE
@c -----------------------------------------------------------------------------
@anchor{break}
@deffn {Function} break (@var{expr_1}, @dots{}, @var{expr_n})
Evaluates and prints @var{expr_1}, @dots{}, @var{expr_n} and then
causes a Maxima break at which point the user can examine and change
his environment. Upon typing @code{exit;} the computation resumes.
@opencatbox{Categories:}
@category{Debugging}
@closecatbox
@end deffn
@c FOR SOME REASON throw IS IN SOME OTHER FILE. MOVE throw INTO THIS FILE.
@c NEEDS CLARIFICATION
@c -----------------------------------------------------------------------------
@anchor{catch}
@deffn {Function} catch (@var{expr_1}, @dots{}, @var{expr_n})
Evaluates @var{expr_1}, @dots{}, @var{expr_n} one by one; if any
leads to the evaluation of an expression of the
form @code{throw (arg)}, then the value of the @code{catch} is the value of
@code{throw (arg)}, and no further expressions are evaluated.
This "non-local return" thus goes through any depth of
nesting to the nearest enclosing @code{catch}. If there is no @code{catch}
enclosing a @code{throw}, an error message is printed.
If the evaluation of the arguments does not lead to the evaluation of any
@code{throw} then the value of @code{catch} is the value of @var{expr_n}.
@c ===beg===
@c lambda ([x], if x < 0 then throw(x) else f(x))$
@c g(l) := catch (map (''%, l))$
@c g ([1, 2, 3, 7]);
@c g ([1, 2, -3, 7]);
@c ===end===
@example
(%i1) lambda ([x], if x < 0 then throw(x) else f(x))$
(%i2) g(l) := catch (map (''%, l))$
(%i3) g ([1, 2, 3, 7]);
(%o3) [f(1), f(2), f(3), f(7)]
(%i4) g ([1, 2, -3, 7]);
(%o4) - 3
@end example
@c REWORD THIS PART.
The function @code{g} returns a list of @code{f} of each element of @code{l} if
@code{l} consists only of non-negative numbers; otherwise, @code{g} "catches"
the first negative element of @code{l} and "throws" it up.
@opencatbox{Categories:}
@category{Programming}
@closecatbox
@end deffn
@c -----------------------------------------------------------------------------
@anchor{compfile}
@deffn {Function} compfile @
@fname{compfile} (@var{filename}, @var{f_1}, @dots{}, @var{f_n}) @
@fname{compfile} (@var{filename}, functions) @
@fname{compfile} (@var{filename}, all)
Translates Maxima functions into Lisp and writes the translated code into the
file @var{filename}.
@code{compfile(@var{filename}, @var{f_1}, ..., @var{f_n})} translates the
specified functions. @code{compfile (@var{filename}, functions)} and
@code{compfile (@var{filename}, all)} translate all user-defined functions.
The Lisp translations are not evaluated, nor is the output file processed by
the Lisp compiler.
@c SO LET'S CONSIDER GIVING THIS FUNCTION A MORE ACCURATE NAME.
@code{translate} creates and evaluates Lisp translations. @code{compile_file}
translates Maxima into Lisp, and then executes the Lisp compiler.
See also @mrefcomma{translate} @mrefcomma{translate_file} and @mrefdot{compile_file}
@opencatbox{Categories:}
@category{Translation and compilation}
@closecatbox
@end deffn
@c THIS VARIABLE IS OBSOLETE: ASSIGNING compgrind: true CAUSES compfile
@c TO EVENTUALLY CALL AN OBSOLETE FUNCTION SPRIN1.
@c RECOMMENDATION IS TO CUT THIS ITEM, AND CUT $compgrind FROM src/transs.lisp
@c @defvar compgrind
@c Default value: @code{false}
@c
@c When @code{compgrind} is @code{true}, function definitions printed by
@c @code{compfile} are pretty-printed.
@c
@c @end defvar
@c -----------------------------------------------------------------------------
@anchor{compile}
@deffn {Function} compile @
@fname{compile} (@var{f_1}, @dots{}, @var{f_n}) @
@fname{compile} (functions) @
@fname{compile} (all)
Translates Maxima functions @var{f_1}, @dots{}, @var{f_n} into Lisp, evaluates
the Lisp translations, and calls the Lisp function @code{COMPILE} on each
translated function. @code{compile} returns a list of the names of the
compiled functions.
@code{compile (all)} or @code{compile (functions)} compiles all user-defined
functions.
@code{compile} quotes its arguments;
the quote-quote operator @code{'@w{}'} defeats quotation.
Compiling a function to native code can mean a big increase in speed and might
cause the memory footprint to reduce drastically.
Code tends to be especially effective when the flexibility it needs to provide
is limited. If compilation doesn't provide the speed that is needed a few ways
to limit the code's functionality are the following:
@itemize @bullet
@item If the function accesses global variables the complexity of the function
can be drastically be reduced by limiting these variables to one data type,
for example using @mref{mode_declare} or a statement like the following one:
@code{put(x_1, bigfloat, numerical_type)}
@item The compiler might warn about undeclared variables if text could either be
a named option to a command or (if they are assigned a value to) the name
of a variable. Prepending the option with a single quote @code{'}
tells the compiler that the text is meant as an option.
@end itemize
@opencatbox{Categories:}
@category{Translation and compilation}
@closecatbox
@end deffn
@c -----------------------------------------------------------------------------
@anchor{define}
@deffn {Function} define @
@fname{define} (@var{f}(@var{x_1}, @dots{}, @var{x_n}), @var{expr}) @
@fname{define} (@var{f}[@var{x_1}, @dots{}, @var{x_n}], @var{expr}) @
@fname{define} (@var{f}[@var{x_1}, @dots{}, @var{x_n}](@var{y_1}, @dots{}, @var{y_m}), @var{expr}) @
@fname{define} (funmake (@var{f}, [@var{x_1}, @dots{}, @var{x_n}]), @var{expr}) @
@fname{define} (arraymake (@var{f}, [@var{x_1}, @dots{}, @var{x_n}]), @var{expr}) @
@fname{define} (ev (@var{expr_1}), @var{expr_2})
Defines a function named @var{f} with arguments @var{x_1}, @dots{}, @var{x_n}
and function body @var{expr}. @code{define} always evaluates its second
argument (unless explicitly quoted). The function so defined may be an ordinary
Maxima function (with arguments enclosed in parentheses) or a @mref{memoizing function}
(with arguments enclosed in square brackets).
When the last or only function argument @var{x_n} is a list of one element,
the function defined by @code{define} accepts a variable number of arguments.
Actual arguments are assigned one-to-one to formal arguments @var{x_1}, @dots{},
@var{x_(n - 1)}, and any further actual arguments, if present, are assigned to
@var{x_n} as a list.
When the first argument of @code{define} is an expression of the form
@code{@var{f}(@var{x_1}, ..., @var{x_n})} or @code{@var{f}[@var{x_1}, ...,
@var{x_n}]}, the function arguments are evaluated but @var{f} is not evaluated,
even if there is already a function or variable by that name.
When the first argument is an expression with operator @code{funmake},
@code{arraymake}, or @code{ev}, the first argument is evaluated;
this allows for the function name to be computed, as well as the body.
All function definitions appear in the same namespace; defining a function
@code{f} within another function @code{g} does not automatically limit the scope
of @code{f} to @code{g}. However, @code{local(f)} makes the definition of
function @code{f} effective only within the block or other compound expression
in which @code{local} appears.
If some formal argument @var{x_k} is a quoted symbol (after evaluation), the
function defined by @code{define} does not evaluate the corresponding actual
argument. Otherwise all actual arguments are evaluated.
See also @mref{:=} and @mrefdot{::=}
Examples:
@code{define} always evaluates its second argument (unless explicitly quoted).
@c ===beg===
@c expr : cos(y) - sin(x);
@c define (F1 (x, y), expr);
@c F1 (a, b);
@c F2 (x, y) := expr;
@c F2 (a, b);
@c ===end===
@example
@group
(%i1) expr : cos(y) - sin(x);
(%o1) cos(y) - sin(x)
@end group
@group
(%i2) define (F1 (x, y), expr);
(%o2) F1(x, y) := cos(y) - sin(x)
@end group
@group
(%i3) F1 (a, b);
(%o3) cos(b) - sin(a)
@end group
@group
(%i4) F2 (x, y) := expr;
(%o4) F2(x, y) := expr
@end group
@group
(%i5) F2 (a, b);
(%o5) cos(y) - sin(x)
@end group
@end example
The function defined by @code{define} may be an ordinary Maxima function or a
@mref{memoizing function}.
@c ===beg===
@c define (G1 (x, y), x.y - y.x);
@c define (G2 [x, y], x.y - y.x);
@c ===end===
@example
@group
(%i1) define (G1 (x, y), x.y - y.x);
(%o1) G1(x, y) := x . y - y . x
@end group
@group
(%i2) define (G2 [x, y], x.y - y.x);
(%o2) G2 := x . y - y . x
x, y
@end group
@end example
When the last or only function argument @var{x_n} is a list of one element,
the function defined by @code{define} accepts a variable number of arguments.
@c ===beg===
@c define (H ([L]), '(apply ("+", L)));
@c H (a, b, c);
@c ===end===
@example
@group
(%i1) define (H ([L]), '(apply ("+", L)));
(%o1) H([L]) := apply("+", L)
@end group
@group
(%i2) H (a, b, c);
(%o2) c + b + a
@end group
@end example
When the first argument is an expression with operator @code{funmake},
@code{arraymake}, or @code{ev}, the first argument is evaluated.
@c ===beg===
@c [F : I, u : x];
@c funmake (F, [u]);
@c define (funmake (F, [u]), cos(u) + 1);
@c define (arraymake (F, [u]), cos(u) + 1);
@c define (foo (x, y), bar (y, x));
@c define (ev (foo (x, y)), sin(x) - cos(y));
@c ===end===
@example
@group
(%i1) [F : I, u : x];
(%o1) [I, x]
@end group
@group
(%i2) funmake (F, [u]);
(%o2) I(x)
@end group
@group
(%i3) define (funmake (F, [u]), cos(u) + 1);
(%o3) I(x) := cos(x) + 1
@end group
@group
(%i4) define (arraymake (F, [u]), cos(u) + 1);
(%o4) I := cos(x) + 1
x
@end group
@group
(%i5) define (foo (x, y), bar (y, x));
(%o5) foo(x, y) := bar(y, x)
@end group
@group
(%i6) define (ev (foo (x, y)), sin(x) - cos(y));
(%o6) bar(y, x) := sin(x) - cos(y)
@end group
@end example
@opencatbox{Categories:}
@category{Function definition}
@closecatbox
@end deffn
@c SEE NOTE BELOW ABOUT THE DOCUMENTATION STRING
@c @deffn {Function} define_variable (@var{name}, @var{default_value}, @var{mode}, @var{documentation})
@c -----------------------------------------------------------------------------
@anchor{define_variable}
@deffn {Function} define_variable (@var{name}, @var{default_value}, @var{mode})
Introduces a global variable into the Maxima environment.
@code{define_variable} is useful in user-written packages, which are often
translated or compiled as it gives the compiler hints of the type (``mode'')
of a variable and therefore avoids requiring it to generate generic code that
can deal with every variable being an integer, float, maxima object, array etc.
@code{define_variable} carries out the following steps:
@enumerate
@item
@code{mode_declare (@var{name}, @var{mode})} declares the mode (``type'') of
@var{name} to the translator which can considerably speed up compiled code as
it allows having to create generic code. See @mref{mode_declare} for a list of
the possible modes.
@item
If the variable is unbound, @var{default_value} is assigned to @var{name}.
@item
Associates @var{name} with a test function
to ensure that @var{name} is only assigned values of the declared mode.
@end enumerate
@c FOLLOWING STATEMENT APPEARS TO BE OUT OF DATE.
@c EXAMINING DEFMSPEC $DEFINE_VARIABLE AND DEF%TR $DEFINE_VARIABLE IN src/trmode.lisp,
@c IT APPEARS THAT THE 4TH ARGUMENT IS NEVER REFERRED TO.
@c EXECUTING translate_file ON A MAXIMA BATCH FILE WHICH CONTAINS
@c define_variable (foo, 2222, integer, "THIS IS FOO");
@c DOES NOT PUT "THIS IS FOO" INTO THE LISP FILE NOR THE UNLISP FILE.
@c The optional 4th argument is a documentation string. When
@c @code{translate_file} is used on a package which includes documentation
@c strings, a second file is output in addition to the Lisp file which
@c will contain the documentation strings, formatted suitably for use in
@c manuals, usage files, or (for instance) @code{describe}.
The @code{value_check} property can be assigned to any variable which has been
defined via @code{define_variable} with a mode other than @code{any}.
The @code{value_check} property is a lambda expression or the name of a function
of one variable, which is called when an attempt is made to assign a value to
the variable. The argument of the @code{value_check} function is the would-be
assigned value.
@code{define_variable} evaluates @code{default_value}, and quotes @code{name}
and @code{mode}. @code{define_variable} returns the current value of
@code{name}, which is @code{default_value} if @code{name} was unbound before,
and otherwise it is the previous value of @code{name}.
Examples:
@code{foo} is a Boolean variable, with the initial value @code{true}.
@c ===beg===
@c define_variable (foo, true, boolean);
@c foo;
@c foo: false;
@c foo: %pi;
@c foo;
@c ===end===
@example
@group
(%i1) define_variable (foo, true, boolean);
(%o1) true
@end group
@group
(%i2) foo;
(%o2) true
@end group
@group
(%i3) foo: false;
(%o3) false
@end group
@group
(%i4) foo: %pi;
translator: foo was declared with mode boolean
, but it has value: %pi
-- an error. To debug this try: debugmode(true);
@end group
@group
(%i5) foo;
(%o5) false
@end group
@end example
@code{bar} is an integer variable, which must be prime.
@c ===beg===
@c define_variable (bar, 2, integer);
@c qput (bar, prime_test, value_check);
@c prime_test (y) := if not primep(y) then
@c error (y, "is not prime.");
@c bar: 1439;
@c bar: 1440;
@c bar;
@c ===end===
@example
@group
(%i1) define_variable (bar, 2, integer);
(%o1) 2
@end group
@group
(%i2) qput (bar, prime_test, value_check);
(%o2) prime_test
@end group
@group
(%i3) prime_test (y) := if not primep(y) then
error (y, "is not prime.");
(%o3) prime_test(y) := if not primep(y)
then error(y, "is not prime.")
@end group
@group
(%i4) bar: 1439;
(%o4) 1439
@end group
@group
(%i5) bar: 1440;
1440 is not prime.
#0: prime_test(y=1440)
-- an error. To debug this try: debugmode(true);
@end group
@group
(%i6) bar;
(%o6) 1439
@end group
@end example
@code{baz_quux} is a variable which cannot be assigned a value.
The mode @code{any_check} is like @code{any}, but @code{any_check} enables the
@code{value_check} mechanism, and @code{any} does not.
@c ===beg===
@c define_variable (baz_quux, 'baz_quux, any_check);
@c F: lambda ([y], if y # 'baz_quux then
@c error ("Cannot assign to `baz_quux'."));
@c qput (baz_quux, ''F, value_check);
@c baz_quux: 'baz_quux;
@c baz_quux: sqrt(2);
@c baz_quux;
@c ===end===
@example
@group
(%i1) define_variable (baz_quux, 'baz_quux, any_check);
(%o1) baz_quux
@end group
@group
(%i2) F: lambda ([y], if y # 'baz_quux then
error ("Cannot assign to `baz_quux'."));
(%o2) lambda([y], if y # 'baz_quux
then error(Cannot assign to `baz_quux'.))
@end group
@group
(%i3) qput (baz_quux, ''F, value_check);
(%o3) lambda([y], if y # 'baz_quux
then error(Cannot assign to `baz_quux'.))
@end group
@group
(%i4) baz_quux: 'baz_quux;
(%o4) baz_quux
@end group
@group
(%i5) baz_quux: sqrt(2);
Cannot assign to `baz_quux'.
#0: lambda([y],if y # 'baz_quux then
error("Cannot assign to `baz_quux'."))(y=sqrt(2))
-- an error. To debug this try: debugmode(true);
@end group
@group
(%i6) baz_quux;
(%o6) baz_quux
@end group
@end example
@opencatbox{Categories:}
@category{Translation and compilation}
@closecatbox
@end deffn
@c -----------------------------------------------------------------------------
@anchor{dispfun}
@deffn {Function} dispfun @
@fname{dispfun} (@var{f_1}, @dots{}, @var{f_n}) @
@fname{dispfun} (all)
Displays the definition of the user-defined functions @var{f_1}, @dots{},
@var{f_n}. Each argument may be the name of a macro (defined with @code{::=}),
an ordinary function (defined with @code{:=} or @code{define}), an array
function (defined with @code{:=} or @code{define}, but enclosing arguments in
square brackets @code{[ ]}), a subscripted function (defined with @code{:=} or
@code{define}, but enclosing some arguments in square brackets and others in
parentheses @code{( )}), one of a family of subscripted functions selected by a
particular subscript value, or a subscripted function defined with a constant
subscript.
@code{dispfun (all)} displays all user-defined functions as
given by the @code{functions}, @code{arrays}, and @code{macros} lists,
omitting subscripted functions defined with constant subscripts.
@code{dispfun} creates an intermediate expression label
(@code{%t1}, @code{%t2}, etc.)
for each displayed function, and assigns the function definition to the label.
In contrast, @code{fundef} returns the function definition.
@code{dispfun} quotes its arguments; the quote-quote operator @code{'@w{}'}
defeats quotation. @code{dispfun} returns the list of intermediate expression
labels corresponding to the displayed functions.
Examples:
@c ===beg===
@c m(x, y) ::= x^(-y);
@c f(x, y) := x^(-y);
@c g[x, y] := x^(-y);
@c h[x](y) := x^(-y);
@c i[8](y) := 8^(-y);
@c dispfun (m, f, g, h, h[5], h[10], i[8]);
@c ''%;
@c ===end===
@example
@group
(%i1) m(x, y) ::= x^(-y);
- y
(%o1) m(x, y) ::= x
@end group
@group
(%i2) f(x, y) := x^(-y);
- y
(%o2) f(x, y) := x
@end group
@group
(%i3) g[x, y] := x^(-y);
- y
(%o3) g := x
x, y
@end group
@group
(%i4) h[x](y) := x^(-y);
- y
(%o4) h (y) := x
x
@end group
@group
(%i5) i[8](y) := 8^(-y);
- y
(%o5) i (y) := 8
8
@end group
@group
(%i6) dispfun (m, f, g, h, h[5], h[10], i[8]);
- y
(%t6) m(x, y) ::= x
- y
(%t7) f(x, y) := x
- y
(%t8) g := x
x, y
- y
(%t9) h (y) := x
x
1
(%t10) h (y) := --
5 y
5
1
(%t11) h (y) := ---
10 y
10
- y
(%t12) i (y) := 8
8
(%o12) [%t6, %t7, %t8, %t9, %t10, %t11, %t12]
@end group
@group
(%i13) ''%;
- y - y - y
(%o13) [m(x, y) ::= x , f(x, y) := x , g := x ,
x, y
- y 1 1 - y
h (y) := x , h (y) := --, h (y) := ---, i (y) := 8 ]
x 5 y 10 y 8
5 10
@end group
@end example
@opencatbox{Categories:}
@category{Function definition}
@category{Display functions}
@closecatbox
@end deffn
@c -----------------------------------------------------------------------------
@anchor{fullmap}
@deffn {Function} fullmap (@var{f}, @var{expr_1}, @dots{})
Similar to @code{map}, but @code{fullmap} keeps mapping down all subexpressions
until the main operators are no longer the same.
@code{fullmap} is used by the Maxima simplifier for certain matrix
manipulations; thus, Maxima sometimes generates an error message concerning
@code{fullmap} even though @code{fullmap} was not explicitly called by the user.
Examples:
@c ===beg===
@c a + b * c;
@c fullmap (g, %);
@c map (g, %th(2));
@c ===end===
@example
@group
(%i1) a + b * c;
(%o1) b c + a
@end group
@group
(%i2) fullmap (g, %);
(%o2) g(b) g(c) + g(a)
@end group
@group
(%i3) map (g, %th(2));
(%o3) g(b c) + g(a)
@end group
@end example
@opencatbox{Categories:}
@category{Function application}
@category{Expressions}
@closecatbox
@end deffn
@c -----------------------------------------------------------------------------
@anchor{fullmapl}
@deffn {Function} fullmapl (@var{f}, @var{list_1}, @dots{})
Similar to @code{fullmap}, but @code{fullmapl} only maps onto lists and
matrices.
Example:
@c ===beg===
@c fullmapl ("+", [3, [4, 5]], [[a, 1], [0, -1.5]]);
@c ===end===
@example
@group
(%i1) fullmapl ("+", [3, [4, 5]], [[a, 1], [0, -1.5]]);
(%o1) [[a + 3, 4], [4, 3.5]]
@end group
@end example
@opencatbox{Categories:}
@category{Function application}
@category{Expressions}
@closecatbox
@end deffn
@c -----------------------------------------------------------------------------
@anchor{functions}
@defvr {System variable} functions
Default value: @code{[]}
@code{functions} is the list of ordinary Maxima functions
in the current session.
An ordinary function is a function constructed by
@code{define} or @code{:=} and called with parentheses @code{()}.
A function may be defined at the Maxima prompt
or in a Maxima file loaded by @code{load} or @code{batch}.
@mref{Memoizing functions} (called with square brackets, e.g., @code{F[x]}) and subscripted
functions (called with square brackets and parentheses, e.g., @code{F[x](y)})
are listed by the global variable @code{arrays}, and not by @code{functions}.
Lisp functions are not kept on any list.
Examples:
@c ===beg===
@c F_1 (x) := x - 100;
@c F_2 (x, y) := x / y;
@c define (F_3 (x), sqrt (x));
@c G_1 [x] := x - 100;
@c G_2 [x, y] := x / y;
@c define (G_3 [x], sqrt (x));
@c H_1 [x] (y) := x^y;
@c functions;
@c arrays;
@c ===end===
@example
@group
(%i1) F_1 (x) := x - 100;
(%o1) F_1(x) := x - 100
@end group
@group
(%i2) F_2 (x, y) := x / y;
x
(%o2) F_2(x, y) := -
y
@end group
@group
(%i3) define (F_3 (x), sqrt (x));
(%o3) F_3(x) := sqrt(x)
@end group
@group
(%i4) G_1 [x] := x - 100;
(%o4) G_1 := x - 100
x
@end group
@group
(%i5) G_2 [x, y] := x / y;
x
(%o5) G_2 := -
x, y y
@end group
@group
(%i6) define (G_3 [x], sqrt (x));
(%o6) G_3 := sqrt(x)
x
@end group
@group
(%i7) H_1 [x] (y) := x^y;
y
(%o7) H_1 (y) := x
x
@end group
@group
(%i8) functions;
(%o8) [F_1(x), F_2(x, y), F_3(x)]
@end group
@group
(%i9) arrays;
(%o9) [G_1, G_2, G_3, H_1]
@end group
@end example
@opencatbox{Categories:}
@category{Function definition}
@category{Global variables}
@closecatbox
@end defvr
@c -----------------------------------------------------------------------------
@anchor{fundef}
@deffn {Function} fundef (@var{f})
Returns the definition of the function @var{f}.
The argument may be
@itemize @bullet
@item the name of a macro (defined with @code{::=}),
@item an ordinary function (defined with @code{:=} or @code{define}),
@item a @mref{memoizing function} (defined with @code{:=} or @code{define}, but enclosing arguments in square brackets @code{[ ]}),
@item a subscripted function (defined with @code{:=} or @code{define},
but enclosing some arguments in square brackets and others in parentheses
@code{( )}),
@item one of a family of subscripted functions selected by a particular
subscript value,
@item or a subscripted function defined with a constant subscript.
@end itemize
@code{fundef} quotes its argument;
the quote-quote operator @code{'@w{}'} defeats quotation.
@code{fundef (@var{f})} returns the definition of @var{f}.
In contrast, @code{dispfun (@var{f})} creates an intermediate expression label
and assigns the definition to the label.
@c PROBABLY NEED SOME EXAMPLES HERE
@opencatbox{Categories:}
@category{Function definition}
@closecatbox
@end deffn
@c -----------------------------------------------------------------------------
@anchor{funmake}
@deffn {Function} funmake (@var{F}, [@var{arg_1}, @dots{}, @var{arg_n}])
Returns an expression @code{@var{F}(@var{arg_1}, ..., @var{arg_n})}.
The return value is simplified, but not evaluated,
so the function @var{F} is not called, even if it exists.
@code{funmake} does not attempt to distinguish @mref{memoizing functions} from ordinary
functions; when @var{F} is the name of a memoizing function,
@code{funmake} returns @code{@var{F}(...)}
(that is, a function call with parentheses instead of square brackets).
@code{arraymake} returns a function call with square brackets in this case.
@code{funmake} evaluates its arguments.
See also @mref{apply} and @mrefdot{args}
Examples:
@code{funmake} applied to an ordinary Maxima function.
@c ===beg===
@c F (x, y) := y^2 - x^2;
@c funmake (F, [a + 1, b + 1]);
@c ''%;
@c ===end===
@example
@group
(%i1) F (x, y) := y^2 - x^2;
2 2
(%o1) F(x, y) := y - x
@end group
@group
(%i2) funmake (F, [a + 1, b + 1]);
(%o2) F(a + 1, b + 1)
@end group
@group
(%i3) ''%;
2 2
(%o3) (b + 1) - (a + 1)
@end group
@end example
@code{funmake} applied to a macro.
@c ===beg===
@c G (x) ::= (x - 1)/2;
@c funmake (G, [u]);
@c ''%;
@c ===end===
@example
@group
(%i1) G (x) ::= (x - 1)/2;
x - 1
(%o1) G(x) ::= -----
2
@end group
@group
(%i2) funmake (G, [u]);
(%o2) G(u)
@end group
@group
(%i3) ''%;
u - 1
(%o3) -----
2
@end group
@end example
@code{funmake} applied to a subscripted function.
@c ===beg===
@c H [a] (x) := (x - 1)^a;
@c funmake (H [n], [%e]);
@c ''%;
@c funmake ('(H [n]), [%e]);
@c ''%;
@c ===end===
@example
@group
(%i1) H [a] (x) := (x - 1)^a;
a
(%o1) H (x) := (x - 1)
a
@end group
@group
(%i2) funmake (H [n], [%e]);
n
(%o2) lambda([x], (x - 1) )(%e)
@end group
@group
(%i3) ''%;
n
(%o3) (%e - 1)
@end group
@group
(%i4) funmake ('(H [n]), [%e]);
(%o4) H (%e)
n
@end group
@group
(%i5) ''%;
n
(%o5) (%e - 1)
@end group
@end example
@code{funmake} applied to a symbol which is not a defined function of any kind.
@c ===beg===
@c funmake (A, [u]);
@c ''%;
@c ===end===
@example
@group
(%i1) funmake (A, [u]);
(%o1) A(u)
@end group
@group
(%i2) ''%;
(%o2) A(u)
@end group
@end example
@code{funmake} evaluates its arguments, but not the return value.
@c ===beg===
@c det(a,b,c) := b^2 -4*a*c;
@c (x : 8, y : 10, z : 12);
@c f : det;
@c funmake (f, [x, y, z]);
@c ''%;
@c ===end===
@example
@group
(%i1) det(a,b,c) := b^2 -4*a*c;
2
(%o1) det(a, b, c) := b - 4 a c
@end group
@group
(%i2) (x : 8, y : 10, z : 12);
(%o2) 12
@end group
@group
(%i3) f : det;
(%o3) det
@end group
@group
(%i4) funmake (f, [x, y, z]);
(%o4) det(8, 10, 12)
@end group
@group
(%i5) ''%;
(%o5) - 284
@end group
@end example
Maxima simplifies @code{funmake}'s return value.
@c ===beg===
@c funmake (sin, [%pi / 2]);
@c ===end===
@example
@group
(%i1) funmake (sin, [%pi / 2]);
(%o1) 1
@end group
@end example
@opencatbox{Categories:}
@category{Function application}
@category{Expressions}
@closecatbox
@end deffn
@c -----------------------------------------------------------------------------
@anchor{lambda}
@deffn {Function} lambda @
@fname{lambda} ([@var{x_1}, @dots{}, @var{x_m}], @var{expr_1}, @dots{}, @var{expr_n}) @
@fname{lambda} ([[@var{L}]], @var{expr_1}, @dots{}, @var{expr_n}) @
@fname{lambda} ([@var{x_1}, @dots{}, @var{x_m}, [@var{L}]], @var{expr_1}, @dots{}, @var{expr_n})
Defines and returns a lambda expression (that is, an anonymous function).
The function may have required arguments @var{x_1}, @dots{}, @var{x_m} and/or
optional arguments @var{L}, which appear within the function body as a list.
The return value of the function is @var{expr_n}. A lambda expression can be
assigned to a variable and evaluated like an ordinary function. A lambda
expression may appear in some contexts in which a function name is expected.
When the function is evaluated, unbound local variables @var{x_1}, @dots{},
@var{x_m} are created. @code{lambda} may appear within @code{block} or another
@code{lambda}; local variables are established each time another @code{block} or
@code{lambda} is evaluated. Local variables appear to be global to any enclosed
@code{block} or @code{lambda}. If a variable is not local, its value is the
value most recently assigned in an enclosing @code{block} or @code{lambda}, if
any, otherwise, it is the value of the variable in the global environment.
This policy may coincide with the usual understanding of "dynamic scope".
After local variables are established, @var{expr_1} through @var{expr_n} are
evaluated in turn. The special variable @code{%%}, representing the value of
the preceding expression, is recognized. @code{throw} and @code{catch} may also
appear in the list of expressions.
@code{return} cannot appear in a lambda expression unless enclosed by
@code{block}, in which case @code{return} defines the return value of the block
and not of the lambda expression, unless the block happens to be @var{expr_n}.
Likewise, @code{go} cannot appear in a lambda expression unless enclosed by
@code{block}.
@code{lambda} quotes its arguments;
the quote-quote operator @code{'@w{}'} defeats quotation.
Examples:
@itemize @bullet
@item
A lambda expression can be assigned to a variable and evaluated like an ordinary
function.
@end itemize
@c ===beg===
@c f: lambda ([x], x^2);
@c f(a);
@c ===end===
@example
@group
(%i1) f: lambda ([x], x^2);
2
(%o1) lambda([x], x )
@end group
@group
(%i2) f(a);
2
(%o2) a
@end group
@end example
@itemize @bullet
@item
A lambda expression may appear in contexts in which a function evaluation is expected.
@end itemize
@c ===beg===
@c lambda ([x], x^2) (a);
@c apply (lambda ([x], x^2), [a]);
@c map (lambda ([x], x^2), [a, b, c, d, e]);
@c ===end===
@example
@group
(%i1) lambda ([x], x^2) (a);
2
(%o1) a
@end group
@group
(%i2) apply (lambda ([x], x^2), [a]);
2
(%o2) a
@end group
@group
(%i3) map (lambda ([x], x^2), [a, b, c, d, e]);
2 2 2 2 2
(%o3) [a , b , c , d , e ]
@end group
@end example
@itemize @bullet
@item
Argument variables are local variables.
Other variables appear to be global variables.
Global variables are evaluated at the time the lambda expression is evaluated,
unless some special evaluation is forced by some means, such as @code{'@w{}'}.
@end itemize
@c ===beg===
@c a: %pi$
@c b: %e$
@c g: lambda ([a], a*b);
@c b: %gamma$
@c g(1/2);
@c g2: lambda ([a], a*''b);
@c b: %e$
@c g2(1/2);
@c ===end===
@example
(%i1) a: %pi$
(%i2) b: %e$
@group
(%i3) g: lambda ([a], a*b);
(%o3) lambda([a], a b)
@end group
(%i4) b: %gamma$
@group
(%i5) g(1/2);
%gamma
(%o5) ------
2
@end group
@group
(%i6) g2: lambda ([a], a*''b);
(%o6) lambda([a], a %gamma)
@end group
(%i7) b: %e$
@group
(%i8) g2(1/2);
%gamma
(%o8) ------
2
@end group
@end example
@itemize @bullet
@item
Lambda expressions may be nested. Local variables within the outer lambda
expression appear to be global to the inner expression unless masked by local
variables of the same names.
@end itemize
@c ===beg===
@c h: lambda ([a, b], h2: lambda ([a], a*b), h2(1/2));
@c h(%pi, %gamma);
@c ===end===
@example
@group
(%i1) h: lambda ([a, b], h2: lambda ([a], a*b), h2(1/2));
1
(%o1) lambda([a, b], h2 : lambda([a], a b), h2(-))
2
@end group
@group
(%i2) h(%pi, %gamma);
%gamma
(%o2) ------
2
@end group
@end example
@itemize @bullet
@item
Since @code{lambda} quotes its arguments, lambda expression @code{i} below does
not define a "multiply by @code{a}" function. Such a function can be defined
via @code{buildq}, as in lambda expression @code{i2} below.
@end itemize
@c ===beg===
@c i: lambda ([a], lambda ([x], a*x));
@c i(1/2);
@c i2: lambda([a], buildq([a: a], lambda([x], a*x)));
@c i2(1/2);
@c i2(1/2)(%pi);
@c ===end===
@example
@group
(%i1) i: lambda ([a], lambda ([x], a*x));
(%o1) lambda([a], lambda([x], a x))
@end group
@group
(%i2) i(1/2);
(%o2) lambda([x], a x)
@end group
@group
(%i3) i2: lambda([a], buildq([a: a], lambda([x], a*x)));
(%o3) lambda([a], buildq([a : a], lambda([x], a x)))
@end group
@group
(%i4) i2(1/2);
1
(%o4) lambda([x], (-) x)
2
@end group
@group
(%i5) i2(1/2)(%pi);
%pi
(%o5) ---
2
@end group
@end example
@itemize @bullet
@item
A lambda expression may take a variable number of arguments,
which are indicated by @code{[@var{L}]} as the sole or final argument.
The arguments appear within the function body as a list.
@end itemize
@c ===beg===
@c f : lambda ([aa, bb, [cc]], aa * cc + bb);
@c f (foo, %i, 17, 29, 256);
@c g : lambda ([[aa]], apply ("+", aa));
@c g (17, 29, x, y, z, %e);
@c ===end===
@example
@group
(%i1) f : lambda ([aa, bb, [cc]], aa * cc + bb);
(%o1) lambda([aa, bb, [cc]], aa cc + bb)
@end group
@group
(%i2) f (foo, %i, 17, 29, 256);
(%o2) [17 foo + %i, 29 foo + %i, 256 foo + %i]
@end group
@group
(%i3) g : lambda ([[aa]], apply ("+", aa));
(%o3) lambda([[aa]], apply(+, aa))
@end group
@group
(%i4) g (17, 29, x, y, z, %e);
(%o4) z + y + x + %e + 46
@end group
@end example
@opencatbox{Categories:}
@category{Function definition}
@closecatbox
@end deffn
@c NEEDS CLARIFICATION AND EXAMPLES
@c -----------------------------------------------------------------------------
@anchor{local}
@deffn {Function} local (@var{v_1}, @dots{}, @var{v_n})
Saves the properties associated with the symbols @var{v_1}, @dots{}, @var{v_n},
removes any properties before evaluating other expressions,
and restores any saved properties on exit
from the block or other compound expression in which @code{local} appears.
Some declarations are implemented as properties of a symbol, including
@code{:=}, @code{array}, @code{dependencies}, @code{atvalue},
@code{matchdeclare}, @code{atomgrad}, @code{constant}, @code{nonscalar},
@code{assume}, and some others. The effect of @code{local} is to make such
declarations effective only within the block or other compound expression in
which @code{local} appears; otherwise such declarations are global declarations.
@code{local} can only appear in @code{block}
or in the body of a function definition or @code{lambda} expression,
and only one occurrence is permitted in each.
@code{local} quotes its arguments.
@code{local} returns @code{done}.
Example:
A local function definition.
@c ===beg===
@c foo (x) := 1 - x;
@c foo (100);
@c block (local (foo), foo (x) := 2 * x, foo (100));
@c foo (100);
@c ===end===
@example
@group
(%i1) foo (x) := 1 - x;
(%o1) foo(x) := 1 - x
@end group
@group
(%i2) foo (100);
(%o2) - 99
@end group
@group
(%i3) block (local (foo), foo (x) := 2 * x, foo (100));
(%o3) 200
@end group
@group
(%i4) foo (100);
(%o4) - 99
@end group
@end example
@opencatbox{Categories:}
@category{Function definition}
@category{Programming}
@closecatbox
@end deffn
@c -----------------------------------------------------------------------------
@anchor{macroexpansion}
@defvr {Option variable} macroexpansion
Default value: @code{false}
@code{macroexpansion} controls whether the expansion (that is, the return value)
of a macro function is substituted for the macro function call.
A substitution may speed up subsequent expression evaluations,
at the cost of storing the expansion.
@table @code
@item false
The expansion of a macro function is not substituted for the macro function call.
@item expand
The first time a macro function call is evaluated,
the expansion is stored.
The expansion is not recomputed on subsequent calls;
any side effects (such as @code{print} or assignment to global variables) happen
only when the macro function call is first evaluated.
Expansion in an expression does not affect other expressions
which have the same macro function call.
@item displace
The first time a macro function call is evaluated,
the expansion is substituted for the call,
thus modifying the expression from which the macro function was called.
The expansion is not recomputed on subsequent calls;
any side effects happen only when the macro function call is first evaluated.
Expansion in an expression does not affect other expressions
which have the same macro function call.
@end table
Examples
When @code{macroexpansion} is @code{false},
a macro function is called every time the calling expression is evaluated,
and the calling expression is not modified.
@c ===beg===
@c f (x) := h (x) / g (x);
@c g (x) ::= block (print ("x + 99 is equal to", x),
@c return (x + 99));
@c h (x) ::= block (print ("x - 99 is equal to", x),
@c return (x - 99));
@c macroexpansion: false;
@c f (a * b);
@c dispfun (f);
@c f (a * b);
@c ===end===
@example
@group
(%i1) f (x) := h (x) / g (x);
h(x)
(%o1) f(x) := ----
g(x)
@end group
@group
(%i2) g (x) ::= block (print ("x + 99 is equal to", x),
return (x + 99));
(%o2) g(x) ::= block(print("x + 99 is equal to", x),
return(x + 99))
@end group
@group
(%i3) h (x) ::= block (print ("x - 99 is equal to", x),
return (x - 99));
(%o3) h(x) ::= block(print("x - 99 is equal to", x),
return(x - 99))
@end group
@group
(%i4) macroexpansion: false;
(%o4) false
@end group
@group
(%i5) f (a * b);
x - 99 is equal to x
x + 99 is equal to x
a b - 99
(%o5) --------
a b + 99
@end group
@group
(%i6) dispfun (f);
h(x)
(%t6) f(x) := ----
g(x)
(%o6) [%t6]
@end group
@group
(%i7) f (a * b);
x - 99 is equal to x
x + 99 is equal to x
a b - 99
(%o7) --------
a b + 99
@end group
@end example
When @code{macroexpansion} is @code{expand},
a macro function is called once,
and the calling expression is not modified.
@c ===beg===
@c f (x) := h (x) / g (x);
@c g (x) ::= block (print ("x + 99 is equal to", x),
@c return (x + 99));
@c h (x) ::= block (print ("x - 99 is equal to", x),
@c return (x - 99));
@c macroexpansion: expand;
@c f (a * b);
@c dispfun (f);
@c f (a * b);
@c ===end===
@example
@group
(%i1) f (x) := h (x) / g (x);
h(x)
(%o1) f(x) := ----
g(x)
@end group
@group
(%i2) g (x) ::= block (print ("x + 99 is equal to", x),
return (x + 99));
(%o2) g(x) ::= block(print("x + 99 is equal to", x),
return(x + 99))
@end group
@group
(%i3) h (x) ::= block (print ("x - 99 is equal to", x),
return (x - 99));
(%o3) h(x) ::= block(print("x - 99 is equal to", x),
return(x - 99))
@end group
@group
(%i4) macroexpansion: expand;
(%o4) expand
@end group
@group
(%i5) f (a * b);
x - 99 is equal to x
x + 99 is equal to x
a b - 99
(%o5) --------
a b + 99
@end group
@group
(%i6) dispfun (f);
mmacroexpanded(x - 99, h(x))
(%t6) f(x) := ----------------------------
mmacroexpanded(x + 99, g(x))
(%o6) [%t6]
@end group
@group
(%i7) f (a * b);
a b - 99
(%o7) --------
a b + 99
@end group
@end example
When @code{macroexpansion} is @code{displace},
a macro function is called once,
and the calling expression is modified.
@c ===beg===
@c f (x) := h (x) / g (x);
@c g (x) ::= block (print ("x + 99 is equal to", x),
@c return (x + 99));
@c h (x) ::= block (print ("x - 99 is equal to", x),
@c return (x - 99));
@c macroexpansion: displace;
@c f (a * b);
@c dispfun (f);
@c f (a * b);
@c ===end===
@example
@group
(%i1) f (x) := h (x) / g (x);
h(x)
(%o1) f(x) := ----
g(x)
@end group
@group
(%i2) g (x) ::= block (print ("x + 99 is equal to", x),
return (x + 99));
(%o2) g(x) ::= block(print("x + 99 is equal to", x),
return(x + 99))
@end group
@group
(%i3) h (x) ::= block (print ("x - 99 is equal to", x),
return (x - 99));
(%o3) h(x) ::= block(print("x - 99 is equal to", x),
return(x - 99))
@end group
@group
(%i4) macroexpansion: displace;
(%o4) displace
@end group
@group
(%i5) f (a * b);
x - 99 is equal to x
x + 99 is equal to x
a b - 99
(%o5) --------
a b + 99
@end group
@group
(%i6) dispfun (f);
x - 99
(%t6) f(x) := ------
x + 99
(%o6) [%t6]
@end group
@group
(%i7) f (a * b);
a b - 99
(%o7) --------
a b + 99
@end group
@end example
@opencatbox{Categories:}
@category{Function application}
@category{Global flags}
@closecatbox
@end defvr
@c -----------------------------------------------------------------------------
@anchor{mode_declare}
@anchor{modedeclare}
@deffn {Function} mode_declare (@var{y_1}, @var{mode_1}, @dots{}, @var{y_n}, @var{mode_n})
@deffnx {Function} modedeclare (@var{y_1}, @var{mode_1}, @dots{}, @var{y_n}, @var{mode_n})
A @code{mode_declare} informs the compiler which type (lisp programmers name the type:
``mode'') a function parameter or its return value will be of. This can greatly
boost the efficiency of the code the compiler generates: Without knowing the type of
all variables and knowing the return value of all functions a function uses
in advance very generic (and thus potentially slow) code needs to be generated.
The arguments of @code{mode_declare} are pairs consisting of a variable (or a list
of variables all having the same mode) and a mode. Available modes (``types'') are:
@example
array an declared array (see the detailed description below)
boolean true or false
integer integers (including arbitrary-size integers)
fixnum integers (excluding arbitrary-size integers)
float machine-size floating-point numbers
real machine-size floating-point or integer
number Numbers
any any kind of object (useful for arrays of any)
@end example
A function parameter named @code{a} can be declared as an array filled with elements
of the type @code{t} the following way:
@example
mode_declare (a, array(t, dim1, dim2, ...))
@end example
If none of the elements of the array @code{a} needs to be checked if it still doesn't
contain a value additional code can be omitted by declaring this fact, too:
@example
mode_declare (a, array (t, complete, dim1, dim2, ...))
@end example
The @code{complete} has no effect if all array elements are of the type
@code{fixnum} or @code{float}: Machine-sized numbers inevitably contain a value
(and will automatically be initialized to 0 in most lisp implementations).
Another way to tell that all entries of the array @code{a} are of the type
(``mode'') @code{m} and have been assigned a value to would be:
@example
mode_declare (completearray (a), m))
@end example
Numeric code using arrays might run faster still if the size of the array is
known at compile time, as well, as in:
@example
mode_declare (completearray (a [10, 10]), float)
@end example
for a floating point number array named @code{a} which is 10 x 10.
@code{mode_declare} also can be used in order to declare the type of the result
of a function. In this case the function compilation needs to be preceded by
another @code{mode_declare} statement. For example the expression,
@example
mode_declare ([function (f_1, f_2, ...)], fixnum)
@end example
declares that the values returned by @code{f_1}, @code{f_2}, @dots{} are
single-word integers.
@code{modedeclare} is a synonym for @code{mode_declare}.
If the type of function parameters and results doesn't match the declaration by
@code{mode_declare} the function may misbehave or a warning or an error might
occur, see @mrefcomma{mode_checkp} @mref{mode_check_errorp} and
@mrefdot{mode_check_warnp}
See @mref{mode_identity} for declaring the type of lists and @mref{define_variable} for
declaring the type of all global variables compiled code uses, as well.
Example:
@c ===beg===
@c square_float(f):=(
@c mode_declare(f,float),
@c f*f
@c );
@c mode_declare([function(f)],float);
@c compile(square_float);
@c square_float(100.0);
@c ===end===
@example
@group
(%i1) square_float(f):=(
mode_declare(f,float),
f*f
);
(%o1) square_float(f) := (mode_declare(f, float), f f)
@end group
@group
(%i2) mode_declare([function(f)],float);
(%o2) [[function(f)]]
@end group
@group
(%i3) compile(square_float);
(%o3) [square_float]
@end group
@group
(%i4) square_float(100.0);
(%o4) 10000.0
@end group
@end example
@opencatbox{Categories:}
@category{Translation and compilation}
@closecatbox
@end deffn
@c NEEDS MORE EXAMPLES?
@c -----------------------------------------------------------------------------
@anchor{mode_checkp}
@defvr {Option variable} mode_checkp
Default value: @code{true}
When @code{mode_checkp} is @code{true}, @mref{mode_declare} does not only define
which type a variable will be of so the compiler can generate more efficient code,
but will also create a runtime warning if the variable isn't of the variable type
the code was compiled to deal with.
@c ===beg===
@c mode_checkp:true;
@c square(f):=(
@c mode_declare(f,float),
@c f^2);
@c compile(square);
@c square(2.3);
@c square(4);
@c ===end===
@example
@group
(%i1) mode_checkp:true;
(%o1) true
@end group
@group
(%i2) square(f):=(
mode_declare(f,float),
f^2);
2
(%o2) square(f) := (mode_declare(f, float), f )
@end group
@group
(%i3) compile(square);
(%o3) [square]
@end group
@group
(%i4) square(2.3);
(%o4) 5.289999999999999
@end group
@group
(%i5) square(4);
Maxima encountered a Lisp error:
The value
4
is not of type
DOUBLE-FLOAT
when binding $F
Automatically continuing.
To enable the Lisp debugger set *debugger-hook* to nil.
@end group
@end example
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c -----------------------------------------------------------------------------
@anchor{mode_check_errorp}
@defvr {Option variable} mode_check_errorp
Default value: @code{false}
When @code{mode_check_errorp} is @code{true}, @code{mode_declare} calls
error.
@c NEED SOME EXAMPLES HERE.
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c -----------------------------------------------------------------------------
@anchor{mode_check_warnp}
@defvr {Option variable} mode_check_warnp
Default value: @code{true}
@c WHAT DOES THIS MEAN ??
When @code{mode_check_warnp} is @code{true}, mode errors are
described.
@c NEED SOME EXAMPLES HERE.
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c NEEDS AN EXAMPLE FOR DECLARING THE RETURN TYPE
@c -----------------------------------------------------------------------------
@anchor{mode_identity}
@deffn {Function} mode_identity (@var{arg_1}, @var{arg_2})
@code{mode_identity} works similar to @mref{mode_declare}, but is used for
informing the compiler that a thing like a @code{macro} or a list operation
will only return a specific type of object. The purpose of doing so is that
maxima supports many objects: Machine integers, arbitrary length integers,
equations, machine floats, big floats, which means that for everything that
deals with return values of operations that can result in any object the
compiler needs to output generic (and therefore potentially slow) code.
The first argument to @code{mode_identity} is the type of return value
something will return (for possible types see @mref{mode_declare}).
(i.e., one of @code{float}, @code{fixnum}, @code{number},
The second argument is the expression that will return an object of this
type.
If the the return value of this expression is of a type the code was not
compiled for error or warning is signalled.
@c ARE THE MODE_DECLARE VARIABLES FOR TURNING OFF THIS ERROR OR WARNING
@c EFFECTIVE HERE, TOO?
If you knew that @code{first (l)} returned a number then you could write
@example
@code{mode_identity (number, first (l))}.
@end example
However, if you need this construct more often it would be more efficient
to define a function that returns a number fist:
@example
firstnumb (x) ::= buildq ([x], mode_identity (number, first(x)));
compile(firstnumb)
@end example
@code{firstnumb} now can be used every time you need the first element
of a list that is guaranteed to be filled with numbers.
@opencatbox{Categories:}
@category{Translation and compilation}
@closecatbox
@end deffn
@c -----------------------------------------------------------------------------
@anchor{remfunction}
@deffn {Function} remfunction @
@fname{remfunction} (@var{f_1}, @dots{}, @var{f_n}) @
@fname{remfunction} (all)
Unbinds the function definitions of the symbols @var{f_1}, @dots{}, @var{f_n}.
The arguments may be the names of ordinary functions (created by @mref{:=} or
@mref{define}) or macro functions (created by @mref{::=}).
@code{remfunction (all)} unbinds all function definitions.
@code{remfunction} quotes its arguments.
@code{remfunction} returns a list of the symbols for which the function
definition was unbound. @code{false} is returned in place of any symbol for
which there is no function definition.
@code{remfunction} does not apply to @mref{memoizing functions} or subscripted functions.
@mref{remarray} applies to those types of functions.
@opencatbox{Categories:}
@category{Function definition}
@closecatbox
@end deffn
@c NEEDS MORE WORK !!!
@c -----------------------------------------------------------------------------
@anchor{savedef}
@defvr {Option variable} savedef
Default value: @code{true}
When @code{savedef} is @code{true}, the Maxima version of a user function is
preserved when the function is translated. This permits the definition to be
displayed by @code{dispfun} and allows the function to be edited.
When @code{savedef} is @code{false}, the names of translated functions are
removed from the @code{functions} list.
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c -----------------------------------------------------------------------------
@anchor{translate}
@deffn {Function} translate @
@fname{translate} (@var{f_1}, @dots{}, @var{f_n}) @
@fname{translate} (functions) @
@fname{translate} (all)
Translates the user-defined functions @var{f_1}, @dots{}, @var{f_n} from the
Maxima language into Lisp and evaluates the Lisp translations.
Typically the translated functions run faster than the originals.
@code{translate (all)} or @code{translate (functions)} translates all
user-defined functions.
Functions to be translated should include a call to @code{mode_declare} at the
beginning when possible in order to produce more efficient code. For example:
@example
f (x_1, x_2, ...) := block ([v_1, v_2, ...],
mode_declare (v_1, mode_1, v_2, mode_2, ...), ...)
@end example
@noindent
where the @var{x_1}, @var{x_2}, @dots{} are the parameters to the function and
the @var{v_1}, @var{v_2}, @dots{} are the local variables.
The names of translated functions are removed from the @code{functions} list
if @code{savedef} is @code{false} (see below) and are added to the @code{props}
lists.
Functions should not be translated unless they are fully debugged.
Expressions are assumed simplified; if they are not, correct but non-optimal
code gets generated. Thus, the user should not set the @code{simp} switch to
@code{false} which inhibits simplification of the expressions to be translated.
The switch @code{translate}, if @code{true}, causes automatic
translation of a user's function to Lisp.
Note that translated
functions may not run identically to the way they did before
translation as certain incompatibilities may exist between the Lisp
and Maxima versions. Principally, the @code{rat} function with more than
one argument and the @code{ratvars} function should not be used if any
variables are @code{mode_declare}'d canonical rational expressions (CRE).
Also the @code{prederror: false} setting
will not translate.
@c WHAT ABOUT % AND %% ???
@code{savedef} - if @code{true} will cause the Maxima version of a user
function to remain when the function is @code{translate}'d. This permits the
definition to be displayed by @code{dispfun} and allows the function to be
edited.
@code{transrun} - if @code{false} will cause the interpreted version of all
functions to be run (provided they are still around) rather than the
translated version.
The result returned by @code{translate} is a list of the names of the
functions translated.
@opencatbox{Categories:}
@category{Translation and compilation}
@closecatbox
@end deffn
@c -----------------------------------------------------------------------------
@anchor{translate_file}
@deffn {Function} translate_file @
@fname{translate_file} (@var{maxima_filename}) @
@fname{translate_file} (@var{maxima_filename}, @var{lisp_filename})
Translates a file of Maxima code into a file of Lisp code.
@code{translate_file} returns a list of three filenames:
the name of the Maxima file, the name of the Lisp file, and the name of file
containing additional information about the translation.
@code{translate_file} evaluates its arguments.
@code{translate_file ("foo.mac"); load("foo.LISP")} is the same as the command
@code{batch ("foo.mac")} except for certain restrictions, the use of
@code{'@w{}'} and @code{%}, for example.
@c FIGURE OUT WHAT THE RESTRICTIONS ARE AND STATE THEM
@code{translate_file (@var{maxima_filename})} translates a Maxima file
@var{maxima_filename} into a similarly-named Lisp file.
For example, @code{foo.mac} is translated into @code{foo.LISP}.
The Maxima filename may include a directory name or names,
in which case the Lisp output file is written
to the same directory from which the Maxima input comes.
@code{translate_file (@var{maxima_filename}, @var{lisp_filename})} translates
a Maxima file @var{maxima_filename} into a Lisp file @var{lisp_filename}.
@code{translate_file} ignores the filename extension, if any, of
@code{lisp_filename}; the filename extension of the Lisp output file is always
@code{LISP}. The Lisp filename may include a directory name or names,
in which case the Lisp output file is written to the specified directory.
@code{translate_file} also writes a file of translator warning
messages of various degrees of severity.
The filename extension of this file is @code{UNLISP}.
This file may contain valuable information, though possibly obscure,
for tracking down bugs in translated code.
The @code{UNLISP} file is always written
to the same directory from which the Maxima input comes.
@code{translate_file} emits Lisp code which causes
some declarations and definitions to take effect as soon
as the Lisp code is compiled.
See @code{compile_file} for more on this topic.
@c CHECK ALL THESE AND SEE WHICH ONES ARE OBSOLETE
See also
@flushleft
@code{tr_array_as_ref}
@c tr_bind_mode_hook EXISTS BUT IT APPEARS TO BE A GROTESQUE UNDOCUMENTED HACK
@c WE DON'T WANT TO MENTION IT
@c @code{tr_bind_mode_hook},
@mrefcomma{tr_bound_function_applyp}
@c tr_exponent EXISTS AND WORKS AS ADVERTISED IN src/troper.lisp
@c NOT OTHERWISE DOCUMENTED; ITS EFFECT SEEMS TOO WEAK TO MENTION
@code{tr_exponent}
@mrefcomma{tr_file_tty_messagesp}
@mrefcomma{tr_float_can_branch_complex}
@mrefcomma{tr_function_call_default}
@mrefcomma{tr_numer}
@mrefcomma{tr_optimize_max_loop}
@mrefcomma{tr_state_vars}
@mrefcomma{tr_warnings_get}
@c Not documented
@code{tr_warn_bad_function_calls}
@mrefcomma{tr_warn_fexpr}
@mrefcomma{tr_warn_meval}
@mrefcomma{tr_warn_mode}
@mrefcomma{tr_warn_undeclared}
and @mrefdot{tr_warn_undefined_variable}
@end flushleft
@opencatbox{Categories:}
@category{Translation and compilation}
@closecatbox
@end deffn
@c -----------------------------------------------------------------------------
@anchor{transrun}
@defvr {Option variable} transrun
Default value: @code{true}
When @code{transrun} is @code{false} will cause the interpreted
version of all functions to be run (provided they are still around)
rather than the translated version.
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c IN WHAT CONTEXT IS tr_array_as_ref: false APPROPRIATE ??? NOT SEEING THE USEFULNESS HERE.
@c ALSO, I GUESS WE SHOULD HAVE AN ITEM FOR translate_fast_arrays, ANOTHER CONFUSING FLAG ...
@c -----------------------------------------------------------------------------
@anchor{tr_array_as_ref}
@defvr {Option variable} tr_array_as_ref
Default value: @code{true}
If @code{translate_fast_arrays} is @code{false}, array references in Lisp code
emitted by @code{translate_file} are affected by @code{tr_array_as_ref}.
When @code{tr_array_as_ref} is @code{true},
array names are evaluated,
otherwise array names appear as literal symbols in translated code.
@code{tr_array_as_ref} has no effect if @code{translate_fast_arrays} is
@code{true}.
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c WHY IS THIS FLAG NEEDED ??? UNDER WHAT CIRCUMSTANCES CAN TRANSLATION
@c OF A BOUND VARIABLE USED AS A FUNCTION GO WRONG ???
@c -----------------------------------------------------------------------------
@anchor{tr_bound_function_applyp}
@defvr {Option variable} tr_bound_function_applyp
Default value: @code{true}
When @code{tr_bound_function_applyp} is @code{true} and @code{tr_function_call_default}
is @code{general}, if a bound variable (such as a function argument) is found being
used as a function then Maxima will rewrite that function call using @code{apply} and
print a warning message.
For example, if @code{g} is defined by @code{g(f,x) := f(x+1)} then translating
@code{g} will cause Maxima to print a warning and rewrite @code{f(x+1)} as
@code{apply(f,[x+1])}.
@c ===beg===
@c f (x) := x^2$
@c g (f) := f (3)$
@c tr_bound_function_applyp : true$
@c translate (g)$
@c g (lambda ([x], x));
@c tr_bound_function_applyp : false$
@c translate (g)$
@c g (lambda ([x], x));
@c ===end===
@example
(%i1) f (x) := x^2$
(%i2) g (f) := f (3)$
(%i3) tr_bound_function_applyp : true$
@group
(%i4) translate (g)$
warning: f is a bound variable in f(3), but it is used as a function.
note: instead I'll translate it as: apply(f,[3])
@end group
@group
(%i5) g (lambda ([x], x));
(%o5) 3
@end group
(%i6) tr_bound_function_applyp : false$
(%i7) translate (g)$
@group
(%i8) g (lambda ([x], x));
(%o8) 9
@end group
@end example
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c -----------------------------------------------------------------------------
@anchor{tr_file_tty_messagesp}
@defvr {Option variable} tr_file_tty_messagesp
Default value: @code{false}
When @code{tr_file_tty_messagesp} is @code{true}, messages generated by
@code{translate_file} during translation of a file are displayed on the console
and inserted into the UNLISP file. When @code{false}, messages about
translation of the file are only inserted into the UNLISP file.
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c -----------------------------------------------------------------------------
@anchor{tr_float_can_branch_complex}
@defvr {Option variable} tr_float_can_branch_complex
Default value: @code{true}
Tells the Maxima-to-Lisp translator to assume that the functions
@code{acos}, @code{asin}, @code{asec}, @code{acsc}, @code{acosh},
@code{asech}, @code{atanh}, @code{acoth}, @code{log} and @code{sqrt}
can return complex results.
When it is @code{true} then @code{acos(x)} is of mode @code{any}
even if @code{x} is of mode @code{float} (as set by @code{mode_declare}).
When @code{false} then @code{acos(x)} is of mode
@code{float} if and only if @code{x} is of mode @code{float}.
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c -----------------------------------------------------------------------------
@anchor{tr_function_call_default}
@defvr {Option variable} tr_function_call_default
Default value: @code{general}
@code{false} means give up and call @code{meval}, @code{expr} means assume Lisp
fixed arg function. @code{general}, the default gives code good for
@code{mexprs} and @code{mlexprs} but not @code{macros}. @code{general} assures
variable bindings are correct in compiled code. In @code{general} mode, when
translating F(X), if F is a bound variable, then it assumes that
@code{apply (f, [x])} is meant, and translates a such, with appropriate warning.
There is no need to turn this off. With the default settings, no warning
messages implies full compatibility of translated and compiled code with the
Maxima interpreter.
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c -----------------------------------------------------------------------------
@anchor{tr_numer}
@defvr {Option variable} tr_numer
Default value: @code{false}
When @code{tr_numer} is @code{true}, @code{numer} properties are used for
atoms which have them, e.g. @code{%pi}.
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c -----------------------------------------------------------------------------
@anchor{tr_optimize_max_loop}
@defvr {Option variable} tr_optimize_max_loop
Default value: 100
@code{tr_optimize_max_loop} is the maximum number of times the
macro-expansion and optimization pass of the translator will loop in
considering a form. This is to catch macro expansion errors, and
non-terminating optimization properties.
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c ARE ANY OF THESE OBSOLETE ??
@c -----------------------------------------------------------------------------
@anchor{tr_state_vars}
@defvr {System variable} tr_state_vars
Default value:
@example
[translate_fast_arrays, tr_function_call_default, tr_bound_function_applyp,
tr_array_as_ref, tr_numer, tr_float_can_branch_complex, define_variable]
@end example
The list of the switches that affect the form of the
translated output.
@c DOES THE GENERAL USER REALLY CARE ABOUT DEBUGGING THE TRANSLATOR ???
@c I doubt it.
This information is useful to system people when
trying to debug the translator. By comparing the translated product
to what should have been produced for a given state, it is possible to
track down bugs.
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c tr_warnings_get EXISTS AND FUNCTIONS AS ADVERTISED (SORT OF) -- RETURNS *tr-runtime-warned*
@c WHICH HAS ONLY A FEW KINDS OF WARNINGS PUSHED ONTO IT; IT'S CERTAINLY NOT COMPREHENSIVE
@c DO WE REALLY NEED THIS SLIGHTLY WORKING FUNCTION ??
@c -----------------------------------------------------------------------------
@anchor{tr_warnings_get}
@deffn {Function} tr_warnings_get ()
Prints a list of warnings which have been given by
the translator during the current translation.
@opencatbox{Categories:}
@category{Translation and compilation}
@closecatbox
@end deffn
@c -----------------------------------------------------------------------------
@defvr {Option variable} tr_warn_bad_function_calls
Default value: @code{true}
- Gives a warning when
when function calls are being made which may not be correct due to
improper declarations that were made at translate time.
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c -----------------------------------------------------------------------------
@anchor{tr_warn_fexpr}
@defvr {Option variable} tr_warn_fexpr
Default value: @code{compfile}
- Gives a warning if any FEXPRs are
encountered. FEXPRs should not normally be output in translated code,
all legitimate special program forms are translated.
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c -----------------------------------------------------------------------------
@anchor{tr_warn_meval}
@defvr {Option variable} tr_warn_meval
Default value: @code{compfile}
- Gives a warning if the function @code{meval} gets called. If @code{meval} is
called that indicates problems in the translation.
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c -----------------------------------------------------------------------------
@anchor{tr_warn_mode}
@defvr {Option variable} tr_warn_mode
Default value: @code{all}
- Gives a warning when variables are
assigned values inappropriate for their mode.
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c -----------------------------------------------------------------------------
@anchor{tr_warn_undeclared}
@defvr {Option variable} tr_warn_undeclared
Default value: @code{compile}
- Determines when to send
warnings about undeclared variables to the TTY.
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c -----------------------------------------------------------------------------
@anchor{tr_warn_undefined_variable}
@defvr {Option variable} tr_warn_undefined_variable
Default value: @code{all}
- Gives a warning when
undefined global variables are seen.
@opencatbox{Categories:}
@category{Translation flags and variables}
@closecatbox
@end defvr
@c -----------------------------------------------------------------------------
@anchor{compile_file}
@deffn {Function} compile_file @
@fname{compile_file} (@var{filename}) @
@fname{compile_file} (@var{filename}, @var{compiled_filename}) @
@fname{compile_file} (@var{filename}, @var{compiled_filename}, @var{lisp_filename})
Translates the Maxima file @var{filename} into Lisp, and executes the Lisp compiler.
The compiled code is not loaded into Maxima.
@code{compile_file} returns a list of the names of four files: the original
Maxima file, the Lisp translation, notes on translation, and the compiled code.
If the compilation fails, the fourth item is @code{false}.
Some declarations and definitions take effect as soon
as the Lisp code is compiled (without loading the compiled code).
These include functions defined with the @code{:=} operator,
macros define with the @code{::=} operator,
@c HEDGE -- DON'T KNOW IF THERE IS ANOTHER WAY
@code{alias}, @code{declare},
@code{define_variable}, @code{mode_declare},
and
@code{infix}, @code{matchfix},
@code{nofix}, @code{postfix}, @code{prefix},
and @code{compfile}.
Assignments and function calls are not evaluated until the compiled code is
loaded. In particular, within the Maxima file, assignments to the translation
flags (@code{tr_numer}, etc.) have no effect on the translation.
@c @code{compile_file} may mistake warnings for errors and
@c return @code{false} as the name of the compiled code when, in fact,
@c the compilation succeeded. This is a bug.
@c REPORTED AS SOURCEFORGE BUG # 1103722.
@var{filename} may not contain @code{:lisp} statements.
@code{compile_file} evaluates its arguments.
@opencatbox{Categories:}
@category{Translation and compilation}
@closecatbox
@end deffn
@c NEEDS CLARIFICATION
@c -----------------------------------------------------------------------------
@anchor{declare_translated}
@deffn {Function} declare_translated (@var{f_1}, @var{f_2}, @dots{})
When translating a file of Maxima code
to Lisp, it is important for the translator to know which functions it
sees in the file are to be called as translated or compiled functions,
and which ones are just Maxima functions or undefined. Putting this
declaration at the top of the file, lets it know that although a symbol
does which does not yet have a Lisp function value, will have one at
call time. @code{(MFUNCTION-CALL fn arg1 arg2 ...)} is generated when
the translator does not know @code{fn} is going to be a Lisp function.
@opencatbox{Categories:}
@category{Translation and compilation}
@closecatbox
@end deffn
|