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
|
(* Orpie -- a stack-based RPN calculator for the console
* Copyright (C) 2003-2004 Paul Pelzl
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Please send bug reports, patches, etc. to Paul Pelzl at
* <pelzlpj@eecs.umich.edu>.
*)
open Rpc_stack;;
open Gsl_assist;;
open Big_int;;
type interruptable_args_t =
| Gcd_args of big_int * big_int * orpie_data_t * orpie_data_t
| Lcm_args of big_int * big_int * big_int * orpie_data_t * orpie_data_t
| Fact_args of big_int * big_int * orpie_data_t
| Binom_args of big_int * big_int * big_int *
big_int * orpie_data_t * orpie_data_t
| Perm_args of big_int * big_int * big_int *
orpie_data_t * orpie_data_t
| NoArgs;;
let pi = 3.14159265358979323846;;
class rpc_calc =
object(self)
val mutable stack = new rpc_stack
val mutable backup_stack = new rpc_stack
val mutable modes = {angle = Rad; base = Dec; complex = Rect}
val mutable variables = Hashtbl.create 10
val mutable interr_args = NoArgs
method backup () =
backup_stack <- stack#backup ()
method undo () =
stack <- backup_stack
method mode_rad () =
modes <- {angle = Rad; base = modes.base; complex = modes.complex}
method mode_deg () =
modes <- {angle = Deg; base = modes.base; complex = modes.complex}
method mode_rect () =
modes <- {angle = modes.angle; base = modes.base; complex = Rect}
method mode_polar () =
modes <- {angle = modes.angle; base = modes.base; complex = Polar}
method mode_bin () =
modes <- {angle = modes.angle; base = Bin; complex = modes.complex}
method mode_oct () =
modes <- {angle = modes.angle; base = Oct; complex = modes.complex}
method mode_dec () =
modes <- {angle = modes.angle; base = Dec; complex = modes.complex}
method mode_hex () =
modes <- {angle = modes.angle; base = Hex; complex = modes.complex}
method get_variables () =
variables
method toggle_angle_mode () =
match modes.angle with
|Rad -> self#mode_deg ()
|Deg -> self#mode_rad ()
method toggle_complex_mode () =
match modes.complex with
|Rect -> self#mode_polar ()
|Polar -> self#mode_rect ()
method cycle_base () =
match modes.base with
|Bin -> self#mode_oct ()
|Oct -> self#mode_dec ()
|Dec -> self#mode_hex ()
|Hex -> self#mode_bin ()
method save_state () =
stack#save_state modes variables
method load_state () =
let m, v = stack#load_state () in
modes <- m;
variables <- v;
self#backup ()
method abort_computation () =
match interr_args with
|Gcd_args (a, b, el1, el2) ->
stack#push el1;
stack#push el2;
interr_args <- NoArgs
|Lcm_args (coeff, a, b, el1, el2) ->
stack#push el1;
stack#push el2;
interr_args <- NoArgs
|Fact_args (num, acc, el) ->
stack#push el;
interr_args <- NoArgs
|Binom_args (n, k, num, denom, el1, el2) ->
stack#push el1;
stack#push el2;
interr_args <- NoArgs
|Perm_args (n, term, partial, el1, el2) ->
stack#push el1;
stack#push el2;
interr_args <- NoArgs
|NoArgs ->
()
(* all calc functions will need to have arguments checked
* and a backup performed, so we handle it with a little wrapper function *)
method private check_args (num_args : int) (fn_str : string) (fn : unit -> unit) =
if stack#length >= num_args then begin
self#backup ();
fn ()
end else if stack#length = 0 then
raise (Invalid_argument "empty stack")
else
raise (Invalid_argument ("insufficient arguments for " ^ fn_str))
method add () = self#check_args 2 "addition" self#internal_add
method private internal_add () =
Add.add stack self#evaln
method sub () = self#check_args 2 "subtraction" self#internal_sub
method private internal_sub () =
Sub.sub stack self#evaln
method mult () = self#check_args 2 "multiplication" self#internal_mult
method private internal_mult () =
Mult.mult stack self#evaln
method div () = self#check_args 2 "division" self#internal_div
method private internal_div () =
Div.div stack self#evaln
method inv () = self#check_args 1 "inv" self#internal_inv
method private internal_inv () =
Inv.inv stack self#evaln
method pow () = self#check_args 2 "pow" self#internal_pow
method private internal_pow () =
Pow.pow stack self#evaln
method get_modes () =
modes
method get_stack_size () =
stack#length
method dup () = self#check_args 1 "dup" self#internal_dup
(* Warning: dup() creates multiple references to the same object.
* Therefore all operations need to leave the original stack elements
* unaltered. *)
method private internal_dup () = stack#dup ()
method neg () = self#check_args 1 "neg" self#internal_neg
method private internal_neg () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcInt (minus_big_int el))
|RpcFloatUnit el ->
let new_el = {
Units.coeff = Complex.neg el.Units.coeff;
Units.factors = el.Units.factors
} in
stack#push (RpcFloatUnit new_el)
|RpcComplexUnit el ->
let new_unit = {
Units.coeff = Gsl_complex.negative el.Units.coeff;
Units.factors = el.Units.factors
} in
stack#push (RpcComplexUnit new_unit)
|RpcFloatMatrixUnit (el, uu) ->
let copy = Gsl_matrix.copy el in
(Gsl_matrix.scale copy (-1.0);
stack#push (RpcFloatMatrixUnit (copy, uu)))
|RpcComplexMatrixUnit (el, uu) ->
let copy = Gsl_matrix_complex.copy el in
(Gsl_matrix_complex.scale copy {Complex.re=(-1.0); Complex.im=0.0};
stack#push (RpcComplexMatrixUnit (copy, uu)))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
method sq () = self#check_args 1 "sq" self#internal_sq
method private internal_sq () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcInt (mult_big_int el el))
|RpcFloatUnit el ->
let new_el = Units.mult el el in
stack#push (RpcFloatUnit new_el)
|RpcComplexUnit el ->
stack#push (RpcComplexUnit (Units.mult el el))
|RpcFloatMatrixUnit (el, uu) ->
let n, m = (Gsl_matrix.dims el) in
if n = m then
let result = Gsl_matrix.create n m in
(Gsl_blas.gemm Gsl_blas.NoTrans Gsl_blas.NoTrans 1.0 el el 0.0 result;
stack#push (RpcFloatMatrixUnit (result, Units.mult uu uu)))
else
(stack#push gen_el;
raise (Invalid_argument "matrix is non-square"))
|RpcComplexMatrixUnit (el, uu) ->
let n, m = (Gsl_matrix_complex.dims el) in
if m = n then
let result = Gsl_matrix_complex.create n m in
Gsl_blas.Complex.gemm Gsl_blas.NoTrans Gsl_blas.NoTrans
Complex.one el el Complex.zero result;
stack#push (RpcComplexMatrixUnit (result, Units.mult uu uu))
else
(stack#push gen_el;
raise (Invalid_argument "matrix is non-square"))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
method sqrt () = self#check_args 1 "sqrt" self#internal_sqrt
method private internal_sqrt () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcFloatUnit el ->
stack#push (RpcFloatUnit (Units.pow el 0.5))
|RpcComplexUnit el ->
stack#push (RpcComplexUnit (Units.pow el 0.5))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method abs () = self#check_args 1 "abs" self#internal_abs
method private internal_abs () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcInt (abs_big_int el))
|RpcFloatUnit el ->
let new_el = {
Units.coeff = {
Complex.re = abs_float (el.Units.coeff.Complex.re);
Complex.im = 0.0
};
Units.factors = el.Units.factors
} in
stack#push (RpcFloatUnit new_el)
|RpcComplexUnit el ->
let new_el = {
Units.coeff = {
Complex.re = Gsl_complex.abs el.Units.coeff;
Complex.im = 0.0
};
Units.factors = el.Units.factors
} in
stack#push (RpcFloatUnit new_el)
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method arg () = self#check_args 1 "arg" self#internal_arg
method private internal_arg () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcComplexUnit el ->
let c_el = el.Units.coeff in
begin match modes.angle with
|Rad ->
stack#push (RpcFloatUnit (funit_of_float (Gsl_complex.arg c_el)))
|Deg ->
stack#push (RpcFloatUnit (funit_of_float
(180.0 /. pi *. (Gsl_complex.arg c_el))))
end
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method exp () = self#check_args 1 "exp" self#internal_exp
method private internal_exp () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcFloatUnit (funit_of_float (exp (float_of_big_int el))))
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot exponentiate dimensioned value"
end else
stack#push (RpcFloatUnit (funit_of_float
(exp el.Units.coeff.Complex.re)))
|RpcComplexUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot exponentiate dimensioned value"
end else
stack#push (RpcComplexUnit (cunit_of_cpx
(Gsl_complex.exp el.Units.coeff)))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method ln () = self#check_args 1 "ln" self#internal_ln
method private internal_ln () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcFloatUnit (funit_of_float
(log (float_of_big_int el))))
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute logarithm of dimensioned value"
end else
stack#push (RpcFloatUnit (funit_of_float
(log el.Units.coeff.Complex.re)))
|RpcComplexUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute logarithm of dimensioned value"
end else
stack#push (RpcComplexUnit (cunit_of_cpx
(Gsl_complex.log el.Units.coeff)))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method ten_pow_x () = self#check_args 1 "10_x" self#internal_ten_pow_x
method private internal_ten_pow_x () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcFloatUnit (funit_of_float
(10.0 ** (float_of_big_int el))))
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot exponentiate dimensioned value"
end else
stack#push (RpcFloatUnit (funit_of_float
(10.0 ** el.Units.coeff.Complex.re)))
|RpcComplexUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot exponentiate dimensioned value"
end else
stack#push (RpcComplexUnit (cunit_of_cpx
(Complex.pow (cmpx_of_float 10.0) el.Units.coeff)))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method log10 () = self#check_args 1 "log10" self#internal_log10
method private internal_log10 () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcFloatUnit (funit_of_float (log10
(float_of_big_int el))))
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute logarithm of dimensioned value"
end else
stack#push (RpcFloatUnit (funit_of_float
(log10 el.Units.coeff.Complex.re)))
|RpcComplexUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute logarithm of dimensioned value"
end else
stack#push (RpcComplexUnit (cunit_of_cpx
(Gsl_complex.log10 el.Units.coeff)))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method conj () = self#check_args 1 "conj" self#internal_conj
method private internal_conj () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcInt el)
|RpcFloatUnit el ->
stack#push (RpcFloatUnit el)
|RpcComplexUnit el ->
let new_el = {
Units.coeff = Gsl_complex.conjugate el.Units.coeff;
Units.factors = el.Units.factors
} in
stack#push (RpcComplexUnit new_el)
|RpcFloatMatrixUnit (el, uu) ->
stack#push (RpcFloatMatrixUnit (el, uu))
|RpcComplexMatrixUnit (el, uu) ->
(* element-by-element conjugation *)
let rows, cols = Gsl_matrix_complex.dims el and
arr = Gsl_matrix_complex.to_array el in
let conj_arr = Array.map Gsl_complex.conjugate arr in
let conj_mat = Gsl_matrix_complex.of_array conj_arr rows cols in
stack#push (RpcComplexMatrixUnit (conj_mat, uu))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
method sin () = self#check_args 1 "sin" self#internal_sin
method private internal_sin () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcFloatUnit (funit_of_float
begin
match modes.angle with
|Rad ->
sin (float_of_big_int el)
|Deg ->
sin (pi /. 180.0 *. (float_of_big_int el))
end))
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute sine of dimensioned value"
end else
let f_el = el.Units.coeff.Complex.re in
stack#push (RpcFloatUnit (funit_of_float
begin
match modes.angle with
|Rad ->
sin f_el
|Deg ->
sin (pi /. 180.0 *. f_el)
end))
|RpcComplexUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute sine of dimensioned value"
end else
stack#push (RpcComplexUnit (cunit_of_cpx
(Gsl_complex.sin el.Units.coeff)))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method cos () = self#check_args 1 "cos" self#internal_cos
method private internal_cos () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcFloatUnit (funit_of_float
begin
match modes.angle with
|Rad ->
cos (float_of_big_int el)
|Deg ->
cos (pi /. 180.0 *. (float_of_big_int el))
end))
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute cosine of dimensioned value"
end else
let f_el = el.Units.coeff.Complex.re in
stack#push (RpcFloatUnit (funit_of_float
begin
match modes.angle with
|Rad ->
cos f_el
|Deg ->
cos (pi /. 180.0 *. f_el)
end))
|RpcComplexUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute cosine of dimensioned value"
end else
stack#push (RpcComplexUnit (cunit_of_cpx
(Gsl_complex.cos el.Units.coeff)))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method tan () = self#check_args 1 "tan" self#internal_tan
method private internal_tan () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcFloatUnit (funit_of_float
begin
match modes.angle with
|Rad ->
tan (float_of_big_int el)
|Deg ->
tan (pi /. 180.0 *. (float_of_big_int el))
end))
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute tangent of dimensioned value"
end else
let f_el = el.Units.coeff.Complex.re in
stack#push (RpcFloatUnit (funit_of_float
begin
match modes.angle with
|Rad ->
tan f_el
|Deg ->
tan (pi /. 180.0 *. f_el)
end))
|RpcComplexUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute tangent of dimensioned value"
end else
stack#push (RpcComplexUnit (cunit_of_cpx
(Gsl_complex.tan el.Units.coeff)))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method asin () = self#check_args 1 "asin" self#internal_asin
method private internal_asin () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcFloatUnit (funit_of_float
begin
match modes.angle with
|Rad ->
asin (float_of_big_int el)
|Deg ->
180.0 /. pi *. asin (float_of_big_int el)
end))
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute arcsine of dimensioned value"
end else
let f_el = el.Units.coeff.Complex.re in
stack#push (RpcFloatUnit (funit_of_float
begin
match modes.angle with
|Rad ->
asin f_el
|Deg ->
180.0 /. pi *. asin f_el
end))
|RpcComplexUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute arcsine of dimensioned value"
end else
stack#push (RpcComplexUnit (cunit_of_cpx
(Gsl_complex.arcsin el.Units.coeff)))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method acos () = self#check_args 1 "acos" self#internal_acos
method private internal_acos () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcFloatUnit (funit_of_float
begin
match modes.angle with
|Rad ->
acos (float_of_big_int el)
|Deg ->
180.0 /. pi *. acos (float_of_big_int el)
end))
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute arccos of dimensioned value"
end else
let f_el = el.Units.coeff.Complex.re in
stack#push (RpcFloatUnit (funit_of_float
begin
match modes.angle with
|Rad ->
acos f_el
|Deg ->
180.0 /. pi *. acos f_el
end))
|RpcComplexUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute arccos of dimensioned value"
end else
stack#push (RpcComplexUnit (cunit_of_cpx
(Gsl_complex.arccos el.Units.coeff)))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method atan () = self#check_args 1 "atan" self#internal_atan
method private internal_atan () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcFloatUnit (funit_of_float
begin
match modes.angle with
|Rad ->
atan (float_of_big_int el)
|Deg ->
180.0 /. pi *. atan (float_of_big_int el)
end))
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute arctan of dimensioned value"
end else
let f_el = el.Units.coeff.Complex.re in
stack#push (RpcFloatUnit (funit_of_float
begin
match modes.angle with
|Rad ->
atan f_el
|Deg ->
180.0 /. pi *. atan f_el
end))
|RpcComplexUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute arctan of dimensioned value"
end else
stack#push (RpcComplexUnit (cunit_of_cpx
(Gsl_complex.arctan el.Units.coeff)))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method sinh () = self#check_args 1 "sinh" self#internal_sinh
method private internal_sinh () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcFloatUnit (funit_of_float
(sinh (float_of_big_int el))))
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute sinh of dimensioned value"
end else
let f_el = el.Units.coeff.Complex.re in
stack#push (RpcFloatUnit (funit_of_float (sinh f_el)))
|RpcComplexUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute sinh of dimensioned value"
end else
stack#push (RpcComplexUnit (cunit_of_cpx
(Gsl_complex.sinh el.Units.coeff)))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method cosh () = self#check_args 1 "cosh" self#internal_cosh
method private internal_cosh () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcFloatUnit (funit_of_float
(cosh (float_of_big_int el))))
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute cosh of dimensioned value"
end else
let f_el = el.Units.coeff.Complex.re in
stack#push (RpcFloatUnit (funit_of_float (cosh f_el)))
|RpcComplexUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute cosh of dimensioned value"
end else
stack#push (RpcComplexUnit (cunit_of_cpx
(Gsl_complex.cosh el.Units.coeff)))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method tanh () = self#check_args 1 "tanh" self#internal_tanh
method private internal_tanh () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcFloatUnit (funit_of_float
(tanh (float_of_big_int el))))
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute tanh of dimensioned value"
end else
let f_el = el.Units.coeff.Complex.re in
stack#push (RpcFloatUnit (funit_of_float (tanh f_el)))
|RpcComplexUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute tanh of dimensioned value"
end else
stack#push (RpcComplexUnit (cunit_of_cpx
(Gsl_complex.tanh el.Units.coeff)))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method asinh () = self#check_args 1 "asinh" self#internal_asinh
method private internal_asinh () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcFloatUnit (funit_of_float
(Gsl_math.asinh (float_of_big_int el))))
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute asinh of dimensioned value"
end else
let f_el = el.Units.coeff.Complex.re in
stack#push (RpcFloatUnit (funit_of_float (Gsl_math.asinh f_el)))
|RpcComplexUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute asinh of dimensioned value"
end else
stack#push (RpcComplexUnit (cunit_of_cpx
(Gsl_complex.arcsinh el.Units.coeff)))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method acosh () = self#check_args 1 "acosh" self#internal_acosh
method private internal_acosh () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcFloatUnit (funit_of_float
(Gsl_math.acosh (float_of_big_int el))))
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute acosh of dimensioned value"
end else
let f_el = el.Units.coeff.Complex.re in
stack#push (RpcFloatUnit (funit_of_float (Gsl_math.acosh f_el)))
|RpcComplexUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute acosh of dimensioned value"
end else
stack#push (RpcComplexUnit (cunit_of_cpx
(Gsl_complex.arccosh el.Units.coeff)))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method atanh () = self#check_args 1 "atanh" self#internal_atanh
method private internal_atanh () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcFloatUnit (funit_of_float
(Gsl_math.atanh (float_of_big_int el))))
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute atanh of dimensioned value"
end else
let f_el = el.Units.coeff.Complex.re in
stack#push (RpcFloatUnit (funit_of_float (Gsl_math.atanh f_el)))
|RpcComplexUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute atanh of dimensioned value"
end else
stack#push (RpcComplexUnit (cunit_of_cpx
(Gsl_complex.arctanh el.Units.coeff)))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method re () = self#check_args 1 "re" self#internal_re
(* real part of complex (or complex matrix) *)
method private internal_re () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push gen_el
|RpcFloatUnit el ->
stack#push gen_el
|RpcComplexUnit el ->
let new_el = {
Units.coeff = {
Complex.re = el.Units.coeff.Complex.re;
Complex.im = 0.0
};
Units.factors = el.Units.factors
} in
stack#push (RpcFloatUnit new_el)
|RpcFloatMatrixUnit (el, uu) ->
stack#push gen_el
|RpcComplexMatrixUnit (el, uu) ->
let n, m = Gsl_matrix_complex.dims el
and carr = Gsl_matrix_complex.to_array el in
let farr = Array.make (n * m) 0.0 in
for i = 0 to pred (n * m) do
farr.(i) <- carr.(i).Complex.re
done;
stack#push (RpcFloatMatrixUnit (Gsl_matrix.of_array farr n m, uu))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
method im () = self#check_args 1 "im" self#internal_im
(* imaginary part of complex (or complex matrix) *)
method private internal_im () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcInt zero_big_int)
|RpcFloatUnit el ->
stack#push (RpcFloatUnit (funit_of_float 0.0))
|RpcComplexUnit el ->
let new_el = {
Units.coeff = {
Complex.re = el.Units.coeff.Complex.im;
Complex.im = 0.0
};
Units.factors = el.Units.factors
} in
stack#push (RpcFloatUnit new_el)
|RpcFloatMatrixUnit (el, uu) ->
let n, m = Gsl_matrix.dims el in
let farr = Array.make (n * m) 0.0 in
stack#push (RpcFloatMatrixUnit (Gsl_matrix.of_array farr n m, uu))
|RpcComplexMatrixUnit (el, uu) ->
let n, m = Gsl_matrix_complex.dims el
and carr = Gsl_matrix_complex.to_array el in
let farr = Array.make (n * m) 0.0 in
for i = 0 to pred (n * m) do
farr.(i) <- carr.(i).Complex.im
done;
stack#push (RpcFloatMatrixUnit (Gsl_matrix.of_array farr n m, uu))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
method gamma () = self#check_args 1 "gamma" self#internal_gamma
(* Euler gamma function *)
method private internal_gamma () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
begin try
stack#push (RpcFloatUnit (funit_of_float
(Gsl_sf.gamma (float_of_big_int el))))
with
Gsl_error.Gsl_exn (err, errstr) ->
(stack#push gen_el;
raise (Invalid_argument errstr))
end
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute gamma of dimensioned value"
end else
begin try
let f_el = el.Units.coeff.Complex.re in
stack#push (RpcFloatUnit (funit_of_float (Gsl_sf.gamma f_el)))
with
Gsl_error.Gsl_exn (err, errstr) ->
(stack#push gen_el;
raise (Invalid_argument errstr))
end
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method lngamma () = self#check_args 1 "lngamma" self#internal_lngamma
(* log_e of Euler gamma function *)
method private internal_lngamma () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
begin try
stack#push (RpcFloatUnit (funit_of_float
(Gsl_sf.lngamma (float_of_big_int el))))
with
Gsl_error.Gsl_exn (err, errstr) ->
(stack#push gen_el;
raise (Invalid_argument errstr))
end
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute lngamma of dimensioned value"
end else
begin try
let f_el = el.Units.coeff.Complex.re in
stack#push (RpcFloatUnit (funit_of_float (Gsl_sf.lngamma f_el)))
with
Gsl_error.Gsl_exn (err, errstr) ->
(stack#push gen_el;
raise (Invalid_argument errstr))
end
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method erf () = self#check_args 1 "erf" self#internal_erf
(* error function *)
method private internal_erf () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
begin try
stack#push (RpcFloatUnit (funit_of_float
(Gsl_sf.erf (float_of_big_int el))))
with
Gsl_error.Gsl_exn (err, errstr) ->
(stack#push gen_el;
raise (Invalid_argument errstr))
end
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute error function of dimensioned value"
end else
begin try
let f_el = el.Units.coeff.Complex.re in
stack#push (RpcFloatUnit (funit_of_float (Gsl_sf.erf f_el)))
with
Gsl_error.Gsl_exn (err, errstr) ->
(stack#push gen_el;
raise (Invalid_argument errstr))
end
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
method erfc () = self#check_args 1 "erfc" self#internal_erfc
(* complementary error function *)
method private internal_erfc () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
begin try
stack#push (RpcFloatUnit (funit_of_float
(Gsl_sf.erfc (float_of_big_int el))))
with
Gsl_error.Gsl_exn (err, errstr) ->
(stack#push gen_el;
raise (Invalid_argument errstr))
end
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute erfc of dimensioned value"
end else
begin try
let f_el = el.Units.coeff.Complex.re in
stack#push (RpcFloatUnit (funit_of_float (Gsl_sf.erfc f_el)))
with
Gsl_error.Gsl_exn (err, errstr) ->
(stack#push gen_el;
raise (Invalid_argument errstr))
end
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
(* factorial
* calls gamma function for float arguments, and jumps
* to an interruptible exact implementation for integer
* arguments.
* This function is designed to be called multiple times
* until it returns true. If computation is aborted, the interface
* should call abort_computation() to clean up. *)
method fact () =
match interr_args with
|Fact_args (num, acc, el) ->
if eq_big_int num zero_big_int then begin
stack#push (RpcInt acc);
interr_args <- NoArgs;
true
end else begin
let next_num = pred_big_int num
and next_acc = mult_big_int acc num in
interr_args <- Fact_args (next_num, next_acc, el);
false
end
|NoArgs ->
if stack#length > 0 then begin
self#backup ();
self#evaln 1;
let gen_el = stack#pop () in
begin match gen_el with
|RpcInt el ->
if sign_big_int el >= 0 then begin
interr_args <- Fact_args (el, unit_big_int, gen_el);
false
end else begin
stack#push gen_el;
raise (Invalid_argument "integer factorial requires non-negative argument")
end
|RpcFloatUnit el ->
if has_units el then begin
stack#push gen_el;
raise_invalid "cannot compute factorial of dimensioned value"
end else
begin try
let f_el = el.Units.coeff.Complex.re in
stack#push (RpcFloatUnit (funit_of_float
(Gsl_sf.gamma (f_el +. 1.0))));
true
with
Gsl_error.Gsl_exn (err, errstr) ->
(stack#push gen_el;
raise (Invalid_argument errstr))
end
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "invalid argument"))
end
end else
raise (Invalid_argument "empty stack")
|_ ->
(* shouldn't hit this point if interface is well-behaved *)
self#abort_computation ();
false
method transpose () = self#check_args 1 "transpose" self#internal_transpose
(* matrix transpose *)
method private internal_transpose () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcFloatMatrixUnit (el, uu) ->
let n, m = (Gsl_matrix.dims el) in
let trans_mat = Gsl_matrix.create m n in
Gsl_matrix.transpose trans_mat el;
stack#push (RpcFloatMatrixUnit (trans_mat, uu))
|RpcComplexMatrixUnit (el, uu) ->
let n, m = (Gsl_matrix_complex.dims el) in
let trans_mat = Gsl_matrix_complex.create m n in
Gsl_matrix_complex.transpose trans_mat el;
stack#push (RpcComplexMatrixUnit (trans_mat, uu))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "transpose requires a matrix argument"))
method mod_int () = self#check_args 2 "mod" self#internal_mod_int
(* mod (remainder) *)
method private internal_mod_int () =
self#evaln 2;
let gen_el2 = stack#pop () in
let gen_el1 = stack#pop () in
match gen_el1 with
|RpcInt el1 ->
begin match gen_el2 with
|RpcInt el2 ->
stack#push (RpcInt (mod_big_int el1 el2))
|_ ->
(stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument "mod can only be applied to arguments of type integer"))
end
|RpcVariable s ->
stack#push gen_el1;
stack#push gen_el2;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument "mod can only be applied to arguments of type integer"))
method floor () = self#check_args 1 "floor" self#internal_floor
(* floor function *)
method private internal_floor () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcFloatUnit el ->
let new_el = {
Units.coeff = {
Complex.re = floor el.Units.coeff.Complex.re;
Complex.im = 0.0
};
Units.factors = el.Units.factors
} in
stack#push (RpcFloatUnit new_el)
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "floor can only be applied to real data"))
method ceiling () = self#check_args 1 "ceiling" self#internal_ceiling
(* ceiling function *)
method private internal_ceiling () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcFloatUnit el ->
let new_el = {
Units.coeff = {
Complex.re = ceil el.Units.coeff.Complex.re;
Complex.im = 0.0
};
Units.factors = el.Units.factors
} in
stack#push (RpcFloatUnit new_el)
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "ceiling can only be applied to real data"))
method to_int () = self#check_args 1 "toint" self#internal_to_int
(* coerce to an integer type *)
method private internal_to_int () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push gen_el
|RpcFloatUnit el ->
let ff = el.Units.coeff.Complex.re in
if (abs_float ff) < 1e9 then
stack#push (RpcInt (big_int_of_int (int_of_float ff)))
else
(stack#push gen_el;
raise (Invalid_argument "value is too large to convert to integer"))
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "to_int can only be applied to real data"))
method to_float () = self#check_args 1 "toreal" self#internal_to_float
(* coerce to a floating-point type *)
method private internal_to_float () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcInt el ->
stack#push (RpcFloatUnit (funit_of_float (float_of_big_int el)))
|RpcFloatMatrixUnit (el, uu) ->
let n, m = Gsl_matrix.dims el in
if n = 1 && m = 1 then
stack#push (RpcFloatUnit {
Units.coeff = {
Complex.re = el.{0, 0};
Complex.im = 0.0;
};
Units.factors = uu.Units.factors
})
else begin
stack#push gen_el;
raise_invalid "matrix argument of to_float must be 1x1"
end
|RpcVariable s ->
stack#push gen_el;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el;
raise (Invalid_argument "to_float can only be applied to integer data"))
method solve_linear () = self#check_args 2 "solvelin"
self#internal_solve_linear
(* solve a linear system Ax = b, with input nxn matrix A and output nx1
* matrix b *)
method private internal_solve_linear () =
Solvelin.solve_linear stack self#evaln
method enter_pi () =
self#backup ();
stack#push (RpcFloatUnit (funit_of_float pi))
method get_display_line line_num =
stack#get_display_string line_num modes
method get_fullscreen_display line_num =
stack#get_fullscreen_display_string line_num modes
(* fill in the display string lookup table *)
method launch_fill_in_thread () =
stack#launch_fill_in_thread ()
method drop () = self#check_args 1 "drop" self#internal_drop
method private internal_drop () =
let dummy = stack#pop () in
()
method swap () = self#check_args 2 "swap" self#internal_swap
method private internal_swap () = stack#swap ()
method clear () =
self#backup ();
for i = 1 to stack#length do
let dummy = stack#pop () in ()
done
method push (v : orpie_data_t) =
self#backup ();
stack#push v
method echo el_num =
if el_num <= stack#length then
stack#echo el_num
else
raise (Invalid_argument "cannot echo nonexistant element")
method rolldown i =
stack#rolldown i
method rollup i =
stack#rollup i
method delete i =
stack#delete i
method deleteN i =
stack#deleteN i
method keep i =
stack#keep i
method keepN i =
stack#keepN i
method enter_int i =
stack#push (RpcInt i)
method enter_float f =
stack#push (RpcFloatUnit (funit_of_float f))
method enter_cmpx f =
stack#push (RpcComplexUnit f)
method enter_fmat fm uu =
stack#push (RpcFloatMatrixUnit (fm, uu))
method enter_cmat cm uu =
stack#push (RpcComplexMatrixUnit (cm, uu))
(* evaluate last n variables of the stack (internal use only) *)
method private evaln (num : int) =
(* grab the last n stack elements into a list *)
let rec grab_elements el_lst n =
if n > 0 then
let next_el = stack#pop () in
grab_elements (next_el :: el_lst) (pred n)
else
el_lst
in
(* eval the list elements one-by-one; if there
* is a lookup failure, push everything back on the stack. *)
let rec eval_elements el_lst =
match el_lst with
|[] ->
()
|head :: tail ->
begin match head with
|RpcVariable s ->
begin try
let data = Hashtbl.find variables s in
stack#push data;
eval_elements tail
with
|Not_found ->
let err_msg = Printf.sprintf "variable \"%s\" is not bound" s in
List.iter stack#push el_lst;
raise (Invalid_argument err_msg)
end
|_ ->
stack#push head;
eval_elements tail
end
in
let raw_elements = grab_elements [] num in
eval_elements raw_elements
method eval () =
if stack#length > 0 then begin
(* the extra push and pop is necessary to be able to back up the
* stack *only* when the eval() changes it *)
let gen_el = stack#pop () in
match gen_el with
|RpcVariable s ->
stack#push gen_el;
self#backup ();
self#evaln 1
|_ ->
stack#push gen_el
end else
raise (Invalid_argument "empty stack")
method store () = self#check_args 2 "store" self#internal_store
(* store in a variable *)
method private internal_store () =
let gen_el2 = stack#pop () in
let gen_el1 = stack#pop () in
match gen_el2 with
|RpcVariable s ->
begin match gen_el1 with
|RpcVariable dummy ->
stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument "cannot store variables inside variables")
|_ ->
Hashtbl.remove variables s;
Hashtbl.add variables s gen_el1
end
|_ ->
stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument "cannot store inside non-variable")
method purge () = self#check_args 1 "purge" self#internal_purge
(* clear a variable *)
method private internal_purge () =
let gen_el = stack#pop () in
match gen_el with
|RpcVariable s ->
Hashtbl.remove variables s
|_ ->
stack#push gen_el;
raise (Invalid_argument "only variables can be purged")
(* greatest common divisor
* This is an interruptible computation, and should be
* called multiple times until it returns true.
* If computation is aborted, the interface should call
* abort_computation() to clean up. *)
method gcd () =
match interr_args with
|Gcd_args (a, b, el1, el2) ->
if eq_big_int b zero_big_int then begin
stack#push (RpcInt a);
interr_args <- NoArgs;
true
end else begin
let a_mod_b = mod_big_int a b in
interr_args <- Gcd_args (b, a_mod_b, el1, el2);
false
end
|NoArgs ->
if stack#length > 1 then begin
self#backup ();
self#evaln 2;
let gen_el2 = stack#pop () in
let gen_el1 = stack#pop () in
begin match gen_el1 with
|RpcInt a ->
begin match gen_el2 with
|RpcInt b ->
let abs_a = abs_big_int a
and abs_b = abs_big_int b in
interr_args <- Gcd_args (abs_a, abs_b, gen_el1, gen_el2);
false
|_ ->
stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument "gcd requires integer arguments")
end
|_ ->
stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument "gcd requires integer arguments")
end
end else
raise (Invalid_argument "insufficient arguments for gcd")
|_ ->
(* shouldn't hit this point if interface is well-behaved *)
self#abort_computation ();
false
(* least common multiple
* This is an interruptible computation, and should be
* called multiple times until it returns true.
* If computation is aborted, the interface should call
* abort_computation() to clean up. *)
method lcm () =
match interr_args with
|Lcm_args (coeff, a, b, el1, el2) ->
if eq_big_int b zero_big_int then begin
let result = div_big_int coeff a in
stack#push (RpcInt result);
interr_args <- NoArgs;
true
end else begin
let a_mod_b = mod_big_int a b in
interr_args <- Lcm_args (coeff, b, a_mod_b, el1, el2);
false
end
|NoArgs ->
if stack#length > 1 then begin
self#backup ();
self#evaln 2;
let gen_el2 = stack#pop () in
let gen_el1 = stack#pop () in
begin match gen_el1 with
|RpcInt a ->
begin match gen_el2 with
|RpcInt b ->
let coeff = mult_big_int a b
and abs_a = abs_big_int a
and abs_b = abs_big_int b in
interr_args <- Lcm_args (coeff, abs_a, abs_b, gen_el1, gen_el2);
false
|_ ->
stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument "lcm requires integer arguments")
end
|_ ->
stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument "lcm requires integer arguments")
end
end else
raise (Invalid_argument "insufficient arguments for lcm")
|_ ->
(* shouldn't hit this point if interface is well-behaved *)
self#abort_computation ();
false
(* binomial coefficient
* For a float argument, this is computed using lngamma in order to avoid
* overflow. For an integer argument, jump to an interruptible
* exact arithmetic value. *)
method binom () =
match interr_args with
|Binom_args (n, k, num, denom, el1, el2) ->
if eq_big_int k zero_big_int then begin
let result = div_big_int num denom in
stack#push (RpcInt result);
interr_args <- NoArgs;
true
end else begin
let nmk = sub_big_int n k in
let new_num = mult_big_int num (succ_big_int nmk) in
let new_denom = mult_big_int denom k in
interr_args <- Binom_args (n, (pred_big_int k), new_num,
new_denom, el1, el2);
false
end
|NoArgs ->
if stack#length > 1 then begin
self#backup ();
self#evaln 2;
let gen_el2 = stack#pop () in
let gen_el1 = stack#pop () in
begin match gen_el1 with
|RpcInt el1 ->
begin match gen_el2 with
|RpcInt el2 ->
if sign_big_int el1 >= 0 && sign_big_int el2 >= 0 then
if ge_big_int el1 el2 then
(* save a little computation via a binomial identity *)
let nmk = sub_big_int el1 el2 in
if lt_big_int nmk el2 then begin
interr_args <- Binom_args (el1, nmk, unit_big_int,
unit_big_int, gen_el1, gen_el2);
false
end else begin
interr_args <- Binom_args (el1, el2, unit_big_int,
unit_big_int, gen_el1, gen_el2);
false
end
else
(stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument "first argument to binom must be >= second argument"))
else
(stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument "integer binom requires nonnegative arguments"))
|_ ->
(stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument "binom requires either two integer or two real arguments"))
end
|RpcFloatUnit el1 ->
begin match gen_el2 with
|RpcFloatUnit el2 ->
if has_units el1 || has_units el2 then begin
stack#push gen_el1;
stack#push gen_el2;
raise_invalid "cannot compute binom of dimensioned values"
end else
begin try
let f_el1 = el1.Units.coeff.Complex.re
and f_el2 = el2.Units.coeff.Complex.re in
let log_coeff = (Gsl_sf.lngamma (f_el1 +. 1.0)) -.
(Gsl_sf.lngamma (f_el2 +. 1.0)) -.
(Gsl_sf.lngamma (f_el1 -. f_el2 +. 1.0)) in
stack#push (RpcFloatUnit (funit_of_float (exp log_coeff)));
true
with
Gsl_error.Gsl_exn (err, errstr) ->
(stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument errstr))
end
|_ ->
(stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument "binom requires either two integer or two real arguments"))
end
|RpcVariable s ->
stack#push gen_el1;
stack#push gen_el2;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument "binom can only be applied to real or integer arguments"))
end
end else
raise (Invalid_argument "insufficient arguments for binom")
|_ ->
(* shouldn't hit this point if interface is well-behaved *)
self#abort_computation ();
false
(* # of permutations of subsets of a population
* For a float argument, this is computed using lngamma in order to avoid
* overflow. For an integer argument, jump to an interruptible
* exact arithmetic value. *)
method permutations () =
match interr_args with
|Perm_args (n, term, partial, el1, el2) ->
if eq_big_int n term then begin
stack#push (RpcInt partial);
interr_args <- NoArgs;
true
end else begin
let new_partial = mult_big_int n partial in
interr_args <- Perm_args ((pred_big_int n), term, new_partial,
el1, el2);
false
end
|NoArgs ->
if stack#length > 1 then begin
self#backup ();
self#evaln 2;
let gen_el2 = stack#pop () in
let gen_el1 = stack#pop () in
begin match gen_el1 with
|RpcInt el1 ->
begin match gen_el2 with
|RpcInt el2 ->
if sign_big_int el1 >= 0 && sign_big_int el2 >= 0 then
if ge_big_int el1 el2 then
let nmk = sub_big_int el1 el2 in
interr_args <- Perm_args (el1, nmk, unit_big_int,
gen_el1, gen_el2);
false
else
(stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument "first argument to perm must be >= second argument"))
else
(stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument "integer perm requires nonnegative arguments"))
|_ ->
(stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument "perm requires either two integer or two real arguments"))
end
|RpcFloatUnit el1 ->
begin match gen_el2 with
|RpcFloatUnit el2 ->
if has_units el1 || has_units el2 then begin
stack#push gen_el1;
stack#push gen_el2;
raise_invalid "cannot compute permutations of dimensioned values"
end else
begin try
let f_el1 = el1.Units.coeff.Complex.re
and f_el2 = el2.Units.coeff.Complex.re in
let log_perm = (Gsl_sf.lngamma (f_el1 +. 1.0)) -.
(Gsl_sf.lngamma (f_el1 -. f_el2 +. 1.0)) in
stack#push (RpcFloatUnit (funit_of_float (exp log_perm)));
true
with
Gsl_error.Gsl_exn (err, errstr) ->
(stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument errstr))
end
|_ ->
(stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument "perm requires either two integer or two real arguments"))
end
|RpcVariable s ->
stack#push gen_el1;
stack#push gen_el2;
let err_msg =
Printf.sprintf "variable \"%s\" has not been evaluated" s
in
raise (Invalid_argument err_msg)
|_ ->
(stack#push gen_el1;
stack#push gen_el2;
raise (Invalid_argument "perm can only be applied to real or integer arguments"))
end
end else
raise (Invalid_argument "insufficient arguments for perm")
|_ ->
(* shouldn't hit this point if interface is well-behaved *)
self#abort_computation ();
false
method total () = self#check_args 1 "total" self#internal_total
(* single-variable statistics: total *)
method private internal_total () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcFloatMatrixUnit (mat, uu) ->
(* multiply on the left by a row of ones *)
let n, m = Gsl_matrix.dims mat in
let ones_arr = Array.make n 1.0 in
let ones = Gsl_matrix.of_array ones_arr 1 n in
let result = Gsl_matrix.create 1 m in
Gsl_blas.gemm Gsl_blas.NoTrans Gsl_blas.NoTrans 1.0 ones mat
0.0 result;
stack#push (RpcFloatMatrixUnit (result, uu))
|_ ->
stack#push gen_el;
raise (Invalid_argument "total can only be applied to real matrices")
method mean () = self#check_args 1 "mean" self#internal_mean
(* single-variable statistics: sample mean *)
method private internal_mean () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcFloatMatrixUnit (mat, uu) ->
(* multiply on the left by a row of ones, divided by n *)
let n, m = Gsl_matrix.dims mat in
let ones_arr = Array.make n (1.0 /. (float_of_int n)) in
let ones = Gsl_matrix.of_array ones_arr 1 n in
let result = Gsl_matrix.create 1 m in
Gsl_blas.gemm Gsl_blas.NoTrans Gsl_blas.NoTrans 1.0 ones mat
0.0 result;
stack#push (RpcFloatMatrixUnit (result, uu))
|_ ->
stack#push gen_el;
raise (Invalid_argument "total can only be applied to real matrices")
method sum_squares () = self#check_args 1 "sumsq"
self#internal_sum_squares
(* single-variable statistics: sum of squares *)
method private internal_sum_squares () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcFloatMatrixUnit (mat, uu) ->
let n, m = Gsl_matrix.dims mat in
let result = Gsl_matrix.create 1 m in
for col = 0 to pred m do
result.{0, col} <- 0.0;
for row = 0 to pred n do
let squared_el = mat.{row, col} *. mat.{row, col} in
result.{0, col} <- result.{0, col} +. squared_el
done
done;
stack#push (RpcFloatMatrixUnit (result, Units.mult uu uu))
|_ ->
stack#push gen_el;
raise (Invalid_argument "sumsq can only be applied to real matrices")
method variance_unbiased () = self#check_args 1 "var"
self#internal_variance_unbiased
(* single-variable statistics: bias-corrected sample variance *)
method private internal_variance_unbiased () =
self#evaln 1;
let gen_el = stack#peek 1 in
match gen_el with
|RpcFloatMatrixUnit (mat, uu) ->
let n, m = Gsl_matrix.dims mat in
if n >= 2 then begin
self#internal_variance_biased ();
let n_over_nm1 = (float_of_int n) /. (float_of_int (pred n)) in
stack#push (RpcFloatUnit (funit_of_float n_over_nm1));
self#internal_mult ()
end else
raise (Invalid_argument "insufficient matrix rows for unbiased sample variance")
|_ ->
raise (Invalid_argument "varbias can only be applied to real matrices")
method variance_biased () = self#check_args 1 "varbias"
self#internal_variance_biased
(* single-variable statistics: sample variance (biased) *)
method private internal_variance_biased () =
self#evaln 1;
let gen_el = stack#peek 1 in
match gen_el with
|RpcFloatMatrixUnit (mat, uu) ->
let n, m = Gsl_matrix.dims mat in
let float_n = float_of_int n in
(* computes variance as E[X^2] - E[X]^2 *)
self#internal_dup ();
self#internal_sum_squares ();
stack#push (RpcFloatUnit (funit_of_float float_n));
self#internal_div ();
self#internal_swap ();
self#internal_mean ();
self#internal_sum_squares ();
self#internal_sub ()
|_ ->
raise (Invalid_argument "var can only be applied to real matrices")
method standard_deviation_unbiased () = self#check_args 1 "stdev"
self#internal_standard_deviation_unbiased
(* single-variable statistics: unbiased sample standard deviation *)
method private internal_standard_deviation_unbiased () =
self#internal_variance_unbiased ();
let gen_el = stack#pop () in
match gen_el with
|RpcFloatMatrixUnit (mat, uu) ->
let n, m = Gsl_matrix.dims mat in
let result = Gsl_matrix.create 1 m in
for col = 0 to pred m do
result.{0, col} <- sqrt mat.{0, col}
done;
stack#push (RpcFloatMatrixUnit (result, Units.pow uu 0.5))
|_ -> ()
method standard_deviation_biased () = self#check_args 1 "stdevbias"
self#internal_standard_deviation_biased
(* single-variable statistics: unbiased sample standard deviation *)
method private internal_standard_deviation_biased () =
self#internal_variance_biased ();
let gen_el = stack#pop () in
match gen_el with
|RpcFloatMatrixUnit (mat, uu) ->
let n, m = Gsl_matrix.dims mat in
let result = Gsl_matrix.create 1 m in
for col = 0 to pred m do
result.{0, col} <- sqrt mat.{0, col}
done;
stack#push (RpcFloatMatrixUnit (result, Units.pow uu 0.5))
|_ -> ()
method minimum () = self#check_args 1 "min" self#internal_minimum
(* single-variable statistics: minimum of set *)
method private internal_minimum () = self#min_or_max true ()
method maximum () = self#check_args 1 "max" self#internal_maximum
(* single-variable statistics: maximum of set *)
method private internal_maximum () = self#min_or_max false ()
method private min_or_max operation_is_min () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcFloatMatrixUnit (mat, uu) ->
let n, m = Gsl_matrix.dims mat in
let result = Gsl_matrix.create 1 m in
for col = 0 to pred m do
result.{0, col} <- mat.{0, col};
for row = 1 to pred n do
if operation_is_min then
if mat.{row, col} < result.{0, col} then
result.{0, col} <- mat.{row, col}
else
()
else
if mat.{row, col} > result.{0, col} then
result.{0, col} <- mat.{row, col}
else
()
done
done;
stack#push (RpcFloatMatrixUnit (result, uu))
|_ ->
stack#push gen_el;
raise (Invalid_argument "min can only be applied to real matrices")
method upper_tail_prob_normal () = self#check_args 3 "utpn"
self#internal_upper_tail_prob_normal
method private internal_upper_tail_prob_normal () =
self#evaln 3;
let gen_el3 = stack#pop () in
let gen_el2 = stack#pop () in
let gen_el1 = stack#pop () in
let get_float_args gen_el =
match gen_el with
|RpcInt i_el ->
funit_of_float (float_of_big_int i_el)
|RpcFloatUnit el ->
el
|_ ->
stack#push gen_el1;
stack#push gen_el2;
stack#push gen_el3;
raise (Invalid_argument "utpn requires real scalar arguments")
in
let mean_units = get_float_args gen_el1
and var_units = get_float_args gen_el2
and cutoff_units = get_float_args gen_el3 in
try
(* check that units are consistent *)
let cutoff = cutoff_units.Units.coeff.Complex.re in
let mean_cpx = Units.conversion_factor_unitary mean_units cutoff_units in
let mean = mean_cpx.Complex.re in
let var_cpx = Units.conversion_factor_unitary var_units
(Units.mult cutoff_units cutoff_units) in
let var = var_cpx.Complex.re in
if var <= 0.0 then begin
stack#push gen_el1;
stack#push gen_el2;
stack#push gen_el3;
raise (Invalid_argument "variance argument to utpn must be positive")
end else begin
let arg = (cutoff -. mean) /. (sqrt (2.0 *. var)) in
stack#push (RpcFloatUnit (funit_of_float arg));
self#internal_erfc ();
stack#push (RpcFloatUnit {
Units.coeff = {
Complex.re = 0.5;
Complex.im = 0.0
};
Units.factors = cutoff_units.Units.factors
});
self#internal_mult ()
end
with Units.Units_error s ->
stack#push gen_el1;
stack#push gen_el2;
stack#push gen_el3;
raise_invalid s
(* random float between 0 and 1 *)
method rand () =
self#backup ();
stack#push (RpcFloatUnit (funit_of_float (Random.float 1.0)))
(* standardize units *)
method standardize_units () = self#check_args 1 "ustand"
self#internal_standardize_units
method private internal_standardize_units () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcFloatUnit el ->
stack#push (RpcFloatUnit (Units.standardize_units el))
|RpcComplexUnit el ->
stack#push (RpcComplexUnit (Units.standardize_units el))
|RpcFloatMatrixUnit (el, uu) ->
stack#push (RpcFloatMatrixUnit (el, Units.standardize_units uu))
|RpcComplexMatrixUnit (el, uu) ->
stack#push (RpcComplexMatrixUnit (el, Units.standardize_units uu))
|_ ->
stack#push gen_el
(* obtain the magnitude of a dimensioned value *)
method unit_value () = self#check_args 1 "uvalue"
self#internal_unit_value
method private internal_unit_value () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcFloatUnit el ->
stack#push (RpcFloatUnit (funit_of_float
el.Units.coeff.Complex.re))
|RpcComplexUnit el ->
stack#push (RpcComplexUnit (cunit_of_cpx el.Units.coeff))
|RpcFloatMatrixUnit (el, uu) ->
stack#push (RpcFloatMatrixUnit (el, funit_of_float 1.0))
|RpcComplexMatrixUnit (el, uu) ->
stack#push (RpcComplexMatrixUnit (el, cunit_of_cpx Complex.one))
|_ ->
stack#push gen_el
(* obtain the magnitude of a dimensioned value *)
method convert_units () = self#check_args 2 "uconvert"
self#internal_convert_units
method private internal_convert_units () =
self#evaln 1;
let gen_el2 = stack#pop () in
let gen_el1 = stack#pop () in
match gen_el2 with
|RpcFloatUnit el2 ->
begin match gen_el1 with
|RpcFloatUnit el1 | RpcComplexUnit el1 ->
begin try
let conv = Units.conversion_factor_unitary el1 el2 in
let new_unit = {
Units.coeff = conv;
Units.factors = el2.Units.factors
} in
begin match gen_el1 with
|RpcFloatUnit el1 ->
stack#push (RpcFloatUnit new_unit)
|RpcComplexUnit el1 ->
stack#push (RpcComplexUnit new_unit)
|_ -> ()
end
with Units.Units_error s ->
stack#push gen_el1;
stack#push gen_el2;
raise_invalid s
end
|RpcFloatMatrixUnit (el1, uu) ->
begin try
let conv = Units.conversion_factor_unitary uu el2 in
let result = Gsl_matrix.copy el1 in
Gsl_matrix.scale result conv.Complex.re;
stack#push (RpcFloatMatrixUnit (result, unorm el2))
with Units.Units_error s ->
stack#push gen_el1;
stack#push gen_el2;
raise_invalid s
end
|RpcComplexMatrixUnit (el1, uu) ->
begin try
let conv = Units.conversion_factor_unitary uu el2 in
let result = Gsl_matrix_complex.copy el1 in
Gsl_matrix_complex.scale result conv;
stack#push (RpcComplexMatrixUnit (result, unorm el2))
with Units.Units_error s ->
stack#push gen_el1;
stack#push gen_el2;
raise_invalid s
end
|_ ->
stack#push gen_el1;
stack#push gen_el2;
raise_invalid "cannot convert units for this data type"
end
|_ ->
stack#push gen_el1;
stack#push gen_el2;
raise_invalid "unit conversion target must be real-valued"
(* trace of a matrix *)
method trace () = self#check_args 1 "trace"
self#internal_trace
method private internal_trace () =
self#evaln 1;
let gen_el = stack#pop () in
match gen_el with
|RpcFloatMatrixUnit (el, uu) ->
let n, m = Gsl_matrix.dims el in
if n = m then begin
let result = ref 0.0 in
for i = 0 to pred n do
result := !result +. el.{i, i}
done;
let new_el = {
Units.coeff = {
Complex.re = !result;
Complex.im = 0.0
};
Units.factors = uu.Units.factors
} in
stack#push (RpcFloatUnit new_el)
end else begin
stack#push gen_el;
raise_invalid "argument of trace must be a square matrix"
end
|RpcComplexMatrixUnit (el, uu) ->
let n, m = Gsl_matrix_complex.dims el in
if n = m then begin
let result = ref Complex.zero in
for i = 0 to pred n do
result := Complex.add !result el.{i, i}
done;
let new_el = {
Units.coeff = !result;
Units.factors = uu.Units.factors
} in
stack#push (RpcComplexUnit new_el)
end else begin
stack#push gen_el;
raise_invalid "argument of trace must be a square matrix"
end
|_ ->
stack#push gen_el;
raise_invalid "argument of trace must be a square matrix"
(* method print_stack () =
let print_el line_num el = Printf.printf "%2d: %s\n" line_num el in
for i = stack#length downto 1 do
print_el i (stack#get_display_line i modes)
done
*)
end;;
(* arch-tag: DO_NOT_CHANGE_548916d4-da42-49b4-8941-c0d42306f1b7 *)
|