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
|
@menu
* Introduction to Function Definition::
* Function::
* Macros::
* Definitions for Function Definition::
@end menu
@node Introduction to Function Definition, Function, Function Definition, Function Definition
@section Introduction to Function Definition
@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
@subsection Ordinary functions
To define a function in Maxima you use the := operator.
E.g.
@example
f(x) := sin(x)
@end example
@noindent
defines a function @code{f}.
Anonmyous 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:
@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 @b{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}.
@subsection Array functions
An array function stores the function value the first time it is called with a given argument,
and returns the stored value, without recomputing it, when that same argument is given.
Such a function is often called a @i{memoizing function}.
Array function names 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.
@node Macros, Definitions for Function Definition, Function, Function Definition
@section Macros
@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)$
(%i2) buildq ([a: x, b], a + b + c);
(%o2) x + c + 29
(%i3) ''%;
(%o3) x + 1758
@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
(%i1) buildq ([e: [a, b, c]], foo (x, e, y));
(%o1) foo(x, [a, b, c], y)
(%i2) buildq ([e: [a, b, c]], bar (x, splice (e), y));
(%o2) bar(x, a, b, c, y)
@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
(%i1) buildq ([e: [a, b, c]], splice (e) + splice (e));
(%o1) 2 c + 2 b + 2 a
(%i2) buildq ([e: [a, b, c]], 2 * splice (e));
(%o2) 2 a b c
@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], bar (u, v, w, x, y, z));
@c subst ([u=v, v=w, w=x, x=y, y=z, z=u], bar (u, v, w, x, y, z));
@c ===end===
@example
(%i1) buildq ([a: b, b: a], foo (a, b));
(%o1) foo(b, a)
(%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)
(%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 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
(%i1) show_values ([L]) ::= buildq ([L], map ("=", 'L, L));
(%o1) show_values([L]) ::= buildq([L], map("=", 'L, L))
(%i2) (a: 17, b: 29, c: 1729)$
(%i3) show_values (a, b, c - a - b);
(%o3) [a = 17, b = 29, c - b - a = 1683]
(%i4) macroexpand (show_values (a, b, c - a - b));
(%o4) map(=, '([a, b, c - b - a]), [a, b, c - b - a])
@end example
@end deffn
@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 @code{::=}, @code{macros}, and @code{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
(%i1) g (x) ::= x / 99;
x
(%o1) g(x) ::= --
99
(%i2) h (x) ::= buildq ([x], g (x - a));
(%o2) h(x) ::= buildq([x], g(x - a))
(%i3) a: 1234;
(%o3) 1234
(%i4) macroexpand (h (y));
y - a
(%o4) -----
99
(%i5) h (y);
y - 1234
(%o5) --------
99
@end example
@end deffn
@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 @code{::=}, @code{macros}, and @code{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
(%i1) g (x) ::= x / 99;
x
(%o1) g(x) ::= --
99
(%i2) h (x) ::= buildq ([x], g (x - a));
(%o2) h(x) ::= buildq([x], g(x - a))
(%i3) a: 1234;
(%o3) 1234
(%i4) macroexpand1 (h (y));
(%o4) g(y - a)
(%i5) h (y);
y - 1234
(%o5) --------
99
@end example
@end deffn
@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 @code{infolists}.
@end defvr
@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
(%i1) buildq ([x: [1, %pi, z - y]], foo (splice (x)) / length (x));
foo(1, %pi, z - y)
(%o1) -----------------------
length([1, %pi, z - y])
(%i2) buildq ([x: [1, %pi]], "/" (splice (x)));
1
(%o2) ---
%pi
(%i3) matchfix ("<>", "<>");
(%o3) <>
(%i4) buildq ([x: [1, %pi, z - y]], "<>" (splice (x)));
(%o4) <>1, %pi, z - y<>
@end example
@end deffn
@c end concepts Function Definition
@node Definitions for Function Definition, , Macros, Function Definition
@section Definitions for Function Definition
@deffn {Function} apply (@var{F}, [@var{x_1}, ..., @var{x_n}])
Constructs and evaluates an expression @code{@var{F}(@var{arg_1}, ..., @var{arg_n})}.
@code{apply} does not attempt to distinguish array functions from ordinary functions;
when @var{F} is the name of an array 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.
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
(%i1) L : [1, 5, -10.2, 4, 3];
(%o1) [1, 5, - 10.2, 4, 3]
(%i2) apply (min, L);
(%o2) - 10.2
@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
(%i1) F (x) := x / 1729;
x
(%o1) F(x) := ----
1729
(%i2) fname : F;
(%o2) F
(%i3) dispfun (F);
x
(%t3) F(x) := ----
1729
(%o3) [%t3]
(%i4) dispfun (fname);
fname is not the name of a user function.
-- an error. Quitting. To debug this try debugmode(true);
(%i5) apply (dispfun, [fname]);
x
(%t5) F(x) := ----
1729
(%o5) [%t5]
@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
(%i1) demoivre;
(%o1) false
(%i2) demoivre (exp (%i * x));
(%o2) %i sin(x) + cos(x)
(%i3) apply (demoivre, [exp (%i * x)]);
demoivre evaluates to false
Improper name or value in functional position.
-- an error. Quitting. To debug this try debugmode(true);
(%i4) apply ('demoivre, [exp (%i * x)]);
(%o4) %i sin(x) + cos(x)
@end example
@end deffn
@deffn {Function} block ([@var{v_1}, ..., @var{v_m}], @var{expr_1}, ..., @var{expr_n})
@deffnx {Function} block (@var{expr_1}, ..., @var{expr_n})
@code{block} evaluates @var{expr_1}, ..., @var{expr_n} in sequence
and returns the value of the last expression evaluated.
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.
Some variables @var{v_1}, ..., @var{v_m} can be declared local to the block;
these are distinguished from global variables of the same names.
If no variables are declared local then the list may be omitted.
Within the block,
any variable other than @var{v_1}, ..., @var{v_m} is a global variable.
@code{block} saves the current values of the variables @var{v_1}, ..., @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.
@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".
If it is desired to save and restore other local properties
besides @code{value}, for example @code{array} (except for complete arrays),
@code{function}, @code{dependencies}, @code{atvalue}, @code{matchdeclare}, @code{atomgrad}, @code{constant}, and
@code{nonscalar} then the function @code{local} should be used inside of the block
with arguments being the names of the variables.
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.
@end deffn
@c REPHRASE, NEEDS EXAMPLE
@deffn {Function} break (@var{expr_1}, ..., @var{expr_n})
Evaluates and prints @var{expr_1}, ..., @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.
@end deffn
@c FOR SOME REASON throw IS IN SOME OTHER FILE. MOVE throw INTO THIS FILE.
@c NEEDS CLARIFICATION
@deffn {Function} catch (@var{expr_1}, ..., @var{expr_n})
Evaluates @var{expr_1}, ..., @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}.
@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.
@end deffn
@deffn {Function} compfile (@var{filename}, @var{f_1}, ..., @var{f_n})
Translates Maxima functions @var{f_1}, ..., @var{f_n} into Lisp
and writes the translated code into the file @var{filename}.
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 @code{translate}, @code{translate_file}, and @code{compile_file}.
@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
@deffn {Function} compile (@var{f_1}, ..., @var{f_n})
@deffnx {Function} compile (functions)
@deffnx {Function} compile (all)
Translates Maxima functions @var{f_1}, ..., @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.
@end deffn
@deffn {Function} define (@var{f}(@var{x_1}, ..., @var{x_n}), @var{expr})
Defines a function named @var{f} with arguments @var{x_1}, ..., @var{x_n} and function body @var{expr}.
@code{define} quotes its first argument in most cases,
and evaluates its second argument unless explicitly quoted.
However, if the first argument is an expression of the form
@code{ev (@var{expr})}, @code{funmake (@var{expr})}, or @code{arraymake (@var{expr})},
the first argument is evaluated;
this allows for the function name to be computed, as well as the body.
@code{define} is similar to the function definition operator @code{:=}, but when
@code{define} appears inside a function, the definition is created using the value
of @code{expr} at execution time rather than at the
time of definition of the function which contains it.
All function definitions appear in the same namespace;
defining a function @code{f} within another function @code{g}
does not limit the scope of @code{f} to @code{g}.
@c MAKE THIS SAME POINT IN :=
@code{define} defines array functions (called with arguments in square brackets @code{[ ]})
as well as ordinary functions.
Examples:
@example
(%i1) foo: 2^bar;
bar
(%o1) 2
(%i2) g(x) := (f_1 (y) := foo*x*y,
f_2 (y) := ''foo*x*y,
define (f_3 (y), foo*x*y),
define (f_4 (y), ''foo*x*y));
bar
(%o2) g(x) := (f_1(y) := foo x y, f_2(y) := 2 x y,
bar
define(f_3(y), foo x y), define(f_4(y), 2 x y))
(%i3) functions;
(%o3) [g(x)]
(%i4) g(a);
bar
(%o4) f_4(y) := a 2 y
(%i5) functions;
(%o5) [g(x), f_1(y), f_2(y), f_3(y), f_4(y)]
(%i6) dispfun (f_1, f_2, f_3, f_4);
(%t6) f_1(y) := foo x y
bar
(%t7) f_2(y) := 2 x y
bar
(%t8) f_3(y) := a 2 y
bar
(%t9) f_4(y) := a 2 y
(%o9) done
@end example
@end deffn
@c SEE NOTE BELOW ABOUT THE DOCUMENTATION STRING
@c @deffn {Function} define_variable (@var{name}, @var{default_value}, @var{mode}, @var{documentation})
@deffn {Function} define_variable (@var{name}, @var{default_value}, @var{mode})
Introduces a global variable into the Maxima environment.
@c IMPORT OF FOLLOWING STATEMENT UNCLEAR: IN WHAT WAY IS define_variable MORE USEFUL IN TRANSLATED CODE ??
@code{define_variable} is useful in user-written packages, which are often translated or compiled.
@code{define_variable} carries out the following steps:
@enumerate
@item
@code{mode_declare (@var{name}, @var{mode})} declares the mode of @var{name} to the translator.
See @code{mode_declare} for a list of the possible modes.
@item
If the variable is unbound, @var{default_value} is assigned to @var{name}.
@item
@code{declare (@var{name}, special)} declares it special.
@c CLARIFY THE MEANING OF SPECIAL FOR THE BENEFIT OF READERS OTHER THAN LISP PROGRAMMERS
@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 GENERATED FROM:
@c define_variable (foo, true, boolean);
@c foo;
@c foo: false;
@c foo: %pi;
@c foo;
@example
(%i1) define_variable (foo, true, boolean);
(%o1) true
(%i2) foo;
(%o2) true
(%i3) foo: false;
(%o3) false
(%i4) foo: %pi;
Error: foo was declared mode boolean, has value: %pi
-- an error. Quitting. To debug this try debugmode(true);
(%i5) foo;
(%o5) false
@end example
@code{bar} is an integer variable, which must be prime.
@c GENERATED FROM:
@c define_variable (bar, 2, integer);
@c qput (bar, prime_test, value_check);
@c prime_test (y) := if not primep(y) then error (y, "is not prime.");
@c bar: 1439;
@c bar: 1440;
@c bar;
@example
(%i1) define_variable (bar, 2, integer);
(%o1) 2
(%i2) qput (bar, prime_test, value_check);
(%o2) prime_test
(%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.")
(%i4) bar: 1439;
(%o4) 1439
(%i5) bar: 1440;
1440 is not prime.
#0: prime_test(y=1440)
-- an error. Quitting. To debug this try debugmode(true);
(%i6) bar;
(%o6) 1439
@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 GENERATED FROM:
@c define_variable (baz_quux, 'baz_quux, any_check);
@c F: lambda ([y], if y # 'baz_quux then 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;
@example
(%i1) define_variable (baz_quux, 'baz_quux, any_check);
(%o1) baz_quux
(%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'.))
(%i3) qput (baz_quux, ''F, value_check);
(%o3) lambda([y], if y # 'baz_quux
then error(Cannot assign to `baz_quux'.))
(%i4) baz_quux: 'baz_quux;
(%o4) baz_quux
(%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. Quitting. To debug this try debugmode(true);
(%i6) baz_quux;
(%o6) baz_quux
@end example
@end deffn
@deffn {Function} dispfun (@var{f_1}, ..., @var{f_n})
@deffnx {Function} dispfun (all)
Displays the definition of the user-defined functions @var{f_1}, ..., @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
(%i1) m(x, y) ::= x^(-y);
- y
(%o1) m(x, y) ::= x
(%i2) f(x, y) := x^(-y);
- y
(%o2) f(x, y) := x
(%i3) g[x, y] := x^(-y);
- y
(%o3) g := x
x, y
(%i4) h[x](y) := x^(-y);
- y
(%o4) h (y) := x
x
(%i5) i[8](y) := 8^(-y);
- y
(%o5) i (y) := 8
8
(%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]
(%i12) ''%;
- y - y - y
(%o12) [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 example
@end deffn
@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}.
Array 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
(%i1) F_1 (x) := x - 100;
(%o1) F_1(x) := x - 100
(%i2) F_2 (x, y) := x / y;
x
(%o2) F_2(x, y) := -
y
(%i3) define (F_3 (x), sqrt (x));
(%o3) F_3(x) := sqrt(x)
(%i4) G_1 [x] := x - 100;
(%o4) G_1 := x - 100
x
(%i5) G_2 [x, y] := x / y;
x
(%o5) G_2 := -
x, y y
(%i6) define (G_3 [x], sqrt (x));
(%o6) G_3 := sqrt(x)
x
(%i7) H_1 [x] (y) := x^y;
y
(%o7) H_1 (y) := x
x
(%i8) functions;
(%o8) [F_1(x), F_2(x, y), F_3(x)]
(%i9) arrays;
(%o9) [G_1, G_2, G_3, H_1]
@end example
@end defvr
@deffn {Function} fundef (@var{f})
Returns the definition of the function @var{f}.
@c PROBABLY THIS WOULD BE CLEARER AS A BULLET LIST
The 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{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
@end deffn
@deffn {Function} funmake (@var{F}, [@var{arg_1}, ..., @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 array functions from ordinary functions;
when @var{F} is the name of an array 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.
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
(%i1) F (x, y) := y^2 - x^2;
2 2
(%o1) F(x, y) := y - x
(%i2) funmake (F, [a + 1, b + 1]);
(%o2) F(a + 1, b + 1)
(%i3) ''%;
2 2
(%o3) (b + 1) - (a + 1)
@end example
@code{funmake} applied to a macro.
@c ===beg===
@c G (x) ::= (x - 1)/2;
@c funmake (G, [u]);
@c ''%;
@c ===end===
@example
(%i1) G (x) ::= (x - 1)/2;
x - 1
(%o1) G(x) ::= -----
2
(%i2) funmake (G, [u]);
(%o2) G(u)
(%i3) ''%;
u - 1
(%o3) -----
2
@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
(%i1) H [a] (x) := (x - 1)^a;
a
(%o1) H (x) := (x - 1)
a
(%i2) funmake (H [n], [%e]);
n
(%o2) lambda([x], (x - 1) )(%e)
(%i3) ''%;
n
(%o3) (%e - 1)
(%i4) funmake ('(H [n]), [%e]);
(%o4) H (%e)
n
(%i5) ''%;
n
(%o5) (%e - 1)
@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
(%i1) funmake (A, [u]);
(%o1) A(u)
(%i2) ''%;
(%o2) A(u)
@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
(%i1) det(a,b,c) := b^2 -4*a*c;
2
(%o1) det(a, b, c) := b - 4 a c
(%i2) (x : 8, y : 10, z : 12);
(%o2) 12
(%i3) f : det;
(%o3) det
(%i4) funmake (f, [x, y, z]);
(%o4) det(8, 10, 12)
(%i5) ''%;
(%o5) - 284
@end example
Maxima simplifies @code{funmake}'s return value.
@c ===beg===
@c funmake (sin, [%pi / 2]);
@c ===end===
@example
(%i1) funmake (sin, [%pi / 2]);
(%o1) 1
@end example
@end deffn
@deffn {Function} lambda ([@var{x_1}, ..., @var{x_m}], @var{expr_1}, ..., @var{expr_n})
@deffnx {Function} lambda ([[@var{L}]], @var{expr_1}, ..., @var{expr_n})
@deffnx {Function} lambda ([@var{x_1}, ..., @var{x_m}, [@var{L}]], @var{expr_1}, ..., @var{expr_n})
Defines and returns a lambda expression (that is, an anonymous function).
The function may have required arguments @var{x_1}, ..., @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}, ..., @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
(%i1) f: lambda ([x], x^2);
2
(%o1) lambda([x], x )
(%i2) f(a);
2
(%o2) a
@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
(%i3) lambda ([x], x^2) (a);
2
(%o3) a
(%i4) apply (lambda ([x], x^2), [a]);
2
(%o4) a
(%i5) map (lambda ([x], x^2), [a, b, c, d, e]);
2 2 2 2 2
(%o5) [a , b , c , d , e ]
@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
(%i6) a: %pi$
(%i7) b: %e$
(%i8) g: lambda ([a], a*b);
(%o8) lambda([a], a b)
(%i9) b: %gamma$
(%i10) g(1/2);
%gamma
(%o10) ------
2
(%i11) g2: lambda ([a], a*''b);
(%o11) lambda([a], a %gamma)
(%i12) b: %e$
(%i13) g2(1/2);
%gamma
(%o13) ------
2
@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
(%i14) h: lambda ([a, b], h2: lambda ([a], a*b), h2(1/2));
1
(%o14) lambda([a, b], h2 : lambda([a], a b), h2(-))
2
(%i15) h(%pi, %gamma);
%gamma
(%o15) ------
2
@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
(%i16) i: lambda ([a], lambda ([x], a*x));
(%o16) lambda([a], lambda([x], a x))
(%i17) i(1/2);
(%o17) lambda([x], a x)
(%i18) i2: lambda([a], buildq([a: a], lambda([x], a*x)));
(%o18) lambda([a], buildq([a : a], lambda([x], a x)))
(%i19) i2(1/2);
x
(%o19) lambda([x], -)
2
(%i20) i2(1/2)(%pi);
%pi
(%o20) ---
2
@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
(%i1) f : lambda ([aa, bb, [cc]], aa * cc + bb);
(%o1) lambda([aa, bb, [cc]], aa cc + bb)
(%i2) f (foo, %i, 17, 29, 256);
(%o2) [17 foo + %i, 29 foo + %i, 256 foo + %i]
(%i3) g : lambda ([[aa]], apply ("+", aa));
(%o3) lambda([[aa]], apply(+, aa))
(%i4) g (17, 29, x, y, z, %e);
(%o4) z + y + x + %e + 46
@end example
@end deffn
@c NEEDS CLARIFICATION AND EXAMPLES
@deffn {Function} local (@var{v_1}, ..., @var{v_n})
Declares the variables @var{v_1}, ..., @var{v_n} to be local with
respect to all the properties in the statement in which this function
is used.
@code{local} quotes its arguments.
@code{local} returns @code{done}.
@code{local} may only be used in @code{block}, in the body of function
definitions or @code{lambda} expressions, or in the @code{ev} function, and only one
occurrence is permitted in each.
@code{local} is independent of @code{context}.
@end deffn
@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), return (x + 99));
@c h (x) ::= block (print ("x - 99 is equal to", x), return (x - 99));
@c macroexpansion: false;
@c f (a * b);
@c dispfun (f);
@c f (a * b);
@c ===end===
@example
(%i1) f (x) := h (x) / g (x);
h(x)
(%o1) f(x) := ----
g(x)
(%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))
(%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))
(%i4) macroexpansion: false;
(%o4) false
(%i5) f (a * b);
x - 99 is equal to x
x + 99 is equal to x
a b - 99
(%o5) --------
a b + 99
(%i6) dispfun (f);
h(x)
(%t6) f(x) := ----
g(x)
(%o6) done
(%i7) f (a * b);
x - 99 is equal to x
x + 99 is equal to x
a b - 99
(%o7) --------
a b + 99
@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), return (x + 99));
@c h (x) ::= block (print ("x - 99 is equal to", x), return (x - 99));
@c macroexpansion: expand;
@c f (a * b);
@c dispfun (f);
@c f (a * b);
@c ===end===
@example
(%i1) f (x) := h (x) / g (x);
h(x)
(%o1) f(x) := ----
g(x)
(%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))
(%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))
(%i4) macroexpansion: expand;
(%o4) expand
(%i5) f (a * b);
x - 99 is equal to x
x + 99 is equal to x
a b - 99
(%o5) --------
a b + 99
(%i6) dispfun (f);
h(x)
(%t6) f(x) := ----
g(x)
(%o6) done
(%i7) f (a * b);
a b - 99
(%o7) --------
a b + 99
@end example
When @code{macroexpansion} is @code{expand},
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), return (x + 99));
@c h (x) ::= block (print ("x - 99 is equal to", x), return (x - 99));
@c macroexpansion: displace;
@c f (a * b);
@c dispfun (f);
@c f (a * b);
@c ===end===
@example
(%i1) f (x) := h (x) / g (x);
h(x)
(%o1) f(x) := ----
g(x)
(%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))
(%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))
(%i4) macroexpansion: displace;
(%o4) displace
(%i5) f (a * b);
x - 99 is equal to x
x + 99 is equal to x
a b - 99
(%o5) --------
a b + 99
(%i6) dispfun (f);
x - 99
(%t6) f(x) := ------
x + 99
(%o6) done
(%i7) f (a * b);
a b - 99
(%o7) --------
a b + 99
@end example
@end defvr
@defvr {Option variable} mode_checkp
Default value: @code{true}
@c WHAT DOES THIS MEAN ??
When @code{mode_checkp} is @code{true}, @code{mode_declare} checks the modes
of bound variables.
@c NEED SOME EXAMPLES HERE.
@end defvr
@defvr {Option variable} mode_check_errorp
Default value: @code{false}
@c WHAT DOES THIS MEAN ??
When @code{mode_check_errorp} is @code{true}, @code{mode_declare} calls
error.
@c NEED SOME EXAMPLES HERE.
@end defvr
@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.
@end defvr
@c NEEDS CLARIFICATION AND EXAMPLES
@deffn {Function} mode_declare (@var{y_1}, @var{mode_1}, ..., @var{y_n}, @var{mode_n})
@code{mode_declare} is used to declare the modes of variables and
functions for subsequent translation or compilation of functions.
@code{mode_declare} is typically placed at the beginning of a function
definition, at the beginning of a Maxima script, or executed at the interactive prompt.
The arguments of @code{mode_declare} are pairs consisting of a variable and a mode which is
one of @code{boolean}, @code{fixnum}, @code{number}, @code{rational}, or @code{float}.
Each variable may also
be a list of variables all of which are declared to have the same mode.
@c WHAT DOES THE FOLLOWING STATEMENT MEAN ???
If a variable is an array, and if every element of the array which is
referenced has a value then @code{array (yi, complete, dim1, dim2, ...)}
rather than
@example
array(yi, dim1, dim2, ...)
@end example
should be used when first
declaring the bounds of the array.
@c WHAT DOES THE FOLLOWING STATEMENT MEAN ???
If all the elements of the array
are of mode @code{fixnum} (@code{float}), use @code{fixnum} (@code{float}) instead of @code{complete}.
@c WHAT DOES THE FOLLOWING STATEMENT MEAN ???
Also if every element of the array is of the same mode, say @code{m}, then
@example
mode_declare (completearray (yi), m))
@end example
should be used for efficient
translation.
Numeric code using arrays might run faster
by declaring the expected size of the array, as in:
@example
mode_declare (completearray (a [10, 10]), float)
@end example
for a floating point number array which is 10 x 10.
One may declare the mode of the result of a function by
using @code{function (f_1, f_2, ...)} as an argument;
here @code{f_1}, @code{f_2}, ... are the names
of functions. 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}, ... are single-word integers.
@code{modedeclare} is a synonym for @code{mode_declare}.
@end deffn
@c WHAT IS THIS ABOUT ??
@c NEEDS CLARIFICATION AND EXAMPLES
@deffn {Function} mode_identity (@var{arg_1}, @var{arg_2})
A special form used with @code{mode_declare} and
@code{macros} to declare, e.g., a list of lists of flonums, or other compound
data object. The first argument to @code{mode_identity} is a primitive value
mode name as given to @code{mode_declare} (i.e., one of @code{float}, @code{fixnum}, @code{number},
@code{list}, or @code{any}), and the second argument is an expression which is
evaluated and returned as the value of @code{mode_identity}. However, if the
return value is not allowed by the mode declared in the first
argument, an error or warning is signalled. The important thing is
that the mode of the expression as determined by the Maxima to Lisp
translator, will be that given as the first argument, independent of
anything that goes on in the second argument.
E.g., @code{x: 3.3; mode_identity (fixnum, x);} yields an error. @code{mode_identity (flonum, x)}
returns 3.3 .
This has a number of uses, e.g., if you knew that @code{first (l)} returned a
number then you might write @code{mode_identity (number, first (l))}. However,
a more efficient way to do it would be to define a new primitive,
@example
firstnumb (x) ::= buildq ([x], mode_identity (number, x));
@end example
and use @code{firstnumb}
every time you take the first of a list of numbers.
@end deffn
@c IS THERE ANY REASON TO SET transcompile: false ??
@c MAYBE THIS VARIABLE COULD BE PERMANENTLY SET TO true AND STRUCK FROM THE DOCUMENTATION.
@defvr {Option variable} transcompile
Default value: @code{true}
When @code{transcompile} is @code{true}, @code{translate} and @code{translate_file} generate
declarations to make the translated code more suitable for compilation.
@c BUT THE DECLARATIONS DON'T SEEM TO BE NECESSARY, SO WHAT'S THE POINT AGAIN ??
@code{compfile} sets @code{transcompile: true} for the duration.
@end defvr
@deffn {Function} translate (@var{f_1}, ..., @var{f_n})
@deffnx {Function} translate (functions)
@deffnx {Function} translate (all)
Translates the user-defined functions
@var{f_1}, ..., @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}, ... are the parameters to the function and the
@var{v_1}, @var{v_2}, ... 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 incompatabilities 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.
@end deffn
@deffn {Function} translate_file (@var{maxima_filename})
@deffnx {Function} 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
@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 @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},
@code{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},
@code{tr_file_tty_messagesp},
@code{tr_float_can_branch_complex},
@code{tr_function_call_default},
@code{tr_numer},
@code{tr_optimize_max_loop},
@code{tr_semicompile},
@code{tr_state_vars},
@code{tr_warnings_get},
@code{tr_warn_bad_function_calls},
@code{tr_warn_fexpr},
@code{tr_warn_meval},
@code{tr_warn_mode},
@code{tr_warn_undeclared},
@code{tr_warn_undefined_variable},
and @code{tr_windy}.
@end deffn
@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.
@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 ...
@defvr {Option variable} tr_array_as_ref
Default value: @code{true}
If @code{translate_fast_arrays} is 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}.
@end defvr
@c WHY IS THIS FLAG NEEDED ??? UNDER WHAT CIRCUMSTANCES CAN TRANSLATION
@c OF A BOUND VARIABLE USED AS A FUNCTION GO WRONG ???
@defvr {Option variable} tr_bound_function_applyp
Default value: @code{true}
When @code{tr_bound_function_applyp} is @code{true}, Maxima gives a warning if a bound
variable (such as a function argument) is found being used as a function.
@code{tr_bound_function_applyp} does not affect the code generated in such cases.
For example, an expression such as @code{g (f, x) := f (x+1)} will trigger
the warning message.
@end defvr
@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.
@end defvr
@c THIS FLAG APPEARS TO HAVE NO EFFECT. SHOULD CUT OUT THIS ITEM AND RELATED CODE.
@c NOTE THAT THERE IS CODE IN src/transf.lisp WHICH USES THIS FLAG BUT THE MODE
@c FLAG IS LOST SOMEWHERE ALONG THE WAY TO THE LISP OUTPUT FILE.
@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}, and @code{acsc} can return complex results.
The ostensible effect of @code{tr_float_can_branch_complex} is the following.
However, it appears that this flag has no effect on the translator output.
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}.
@end defvr
@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
apropriate 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.
@end defvr
@defvr {Option variable} tr_numer
Default value: @code{false}
When @code{tr_numer} is @code{true} numer properties are used for
atoms which have them, e.g. @code{%pi}.
@end defvr
@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.
@end defvr
@defvr {Option variable} tr_semicompile
Default value: @code{false}
When @code{tr_semicompile} is @code{true}, @code{translate_file} and @code{compfile}
output forms which will be macroexpanded but not compiled into machine
code by the Lisp compiler.
@end defvr
@c ARE ANY OF THESE OBSOLETE ??
@defvr {System variable} tr_state_vars
Default value:
@example
[transcompile, tr_semicompile, tr_warn_undeclared, tr_warn_meval,
tr_warn_fexpr, tr_warn_mode, tr_warn_undefined_variable,
tr_function_call_default, tr_array_as_ref,tr_numer]
@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 ???
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.
@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 ??
@deffn {Function} tr_warnings_get ()
Prints a list of warnings which have been given by
the translator during the current translation.
@end deffn
@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.
@end defvr
@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.
@end defvr
@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.
@end defvr
@defvr {Option variable} tr_warn_mode
Default value: @code{all}
- Gives a warning when variables are
assigned values inappropriate for their mode.
@end defvr
@defvr {Option variable} tr_warn_undeclared
Default value: @code{compile}
- Determines when to send
warnings about undeclared variables to the TTY.
@end defvr
@defvr {Option variable} tr_warn_undefined_variable
Default value: @code{all}
- Gives a warning when
undefined global variables are seen.
@end defvr
@c $tr_windy IS USED IN EXACTLY ONE PLACE (def%tr $kill IN src/trans1.lisp)
@c WHERE IT CAUSES A WARNING ABOUT USING kill.
@c HOW ABOUT IF WE PERMANENTLY ENABLE THE WARNING MESSAGE
@c AND CUT OUT tr_windy FROM CODE AND DOCS.
@defvr {Option variable} tr_windy
Default value: @code{true}
- Generate helpful comments and
programming hints.
@end defvr
@deffn {Function} compile_file (@var{filename})
@deffnx {Function} compile_file (@var{filename}, @var{compiled_filename})
@deffnx {Function} compile_file (@var{filename}, @var{compiled_filename}, @var{lisp_filename})
Translates the Maxima file @var{filename} into Lisp,
executes the Lisp compiler,
and, if the translation and compilation succeed, loads the compiled code 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.
@end deffn
@c NEEDS CLARIFICATION
@deffn {Function} declare_translated (@var{f_1}, @var{f_2}, ...)
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 fn is going to be a Lisp function.
@end deffn
|