1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510
|
#!/usr/bin/python
r'''more-reasonable core functionality for numpy
* SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(6).reshape(2,3)
>>> b = a + 100
>>> row = a[0,:] + 1000
>>> a
array([[0, 1, 2],
[3, 4, 5]])
>>> b
array([[100, 101, 102],
[103, 104, 105]])
>>> row
array([1000, 1001, 1002])
>>> nps.glue(a,b, axis=-1)
array([[ 0, 1, 2, 100, 101, 102],
[ 3, 4, 5, 103, 104, 105]])
>>> nps.glue(a,b,row, axis=-2)
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 100, 101, 102],
[ 103, 104, 105],
[1000, 1001, 1002]])
>>> nps.cat(a,b)
array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[100, 101, 102],
[103, 104, 105]]])
>>> @nps.broadcast_define( (('n',), ('n',)) )
... def inner_product(a, b):
... return a.dot(b)
>>> inner_product(a,b)
array([ 305, 1250])
* DESCRIPTION
Numpy is a very widely used toolkit for numerical computation in Python. Despite
its popularity, some of its core functionality is mysterious and/or incomplete.
The numpysane library seeks to fill those gaps by providing its own replacement
routines. Many of the replacement functions are direct translations from PDL
(http://pdl.perl.org), a numerical computation library for perl. The functions
provided by this module fall into three broad categories:
- Broadcasting support
- Nicer array manipulation
- Basic linear algebra
** Broadcasting
Numpy has a limited support for broadcasting
(http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html), a generic way
to vectorize functions. A broadcasting-aware function knows the dimensionality
of its inputs, and any extra dimensions in the input are automatically used for
vectorization.
*** Broadcasting rules
A basic example is an inner product: a function that takes in two
identically-sized 1-dimensional arrays (input prototype (('n',), ('n',)) ) and
returns a scalar (output prototype () ). If one calls a broadcasting-aware inner
product with two arrays of shape (2,3,4) as input, it would compute 6 inner
products of length-4 each, and report the output in an array of shape (2,3).
In short:
- The most significant dimension in a numpy array is the LAST one, so the
prototype of an input argument must exactly match a given input's trailing
shape. So a prototype shape of (a,b,c) accepts an argument shape of (......,
a,b,c), with as many or as few leading dimensions as desired.
- The extra leading dimensions must be compatible across all the inputs. This
means that each leading dimension must either
- equal 1
- be missing (thus assumed to equal 1)
- equal to some positive integer >1, consistent across all arguments
- The output is collected into an array that's sized as a superset of the
above-prototype shape of each argument
More involved example: A function with input prototype ( (3,), ('n',3), ('n',),
('m',) ) given inputs of shape
(1,5, 3)
(2,1, 8,3)
( 8)
( 5, 9)
will return an output array of shape (2,5, ...), where ... is the shape of each
output slice. Note again that the prototype dictates the TRAILING shape of the
inputs.
*** What about the stock broadcasting support?
The numpy documentation dedicates a whole page explaining the broadcasting
rules, but only a small number of numpy functions provide any broadcasting
support. It's fairly inconsistent, and most functions have no broadcasting
support and no mention of it in the documentation. And as a result, this is not
a prominent part of the numpy ecosystem and there's little user awareness that
it exists.
*** What this module provides
This module contains functionality to make any arbitrary function broadcastable,
in either C or Python. In both cases, the input and output prototypes are
declared, and these are used for shape-checking and vectorization each time the
function is called.
The functions can have either
- A single output, returned as a numpy array. The output specification in the
prototype is a single shape tuple
- Multiple outputs, returned as a tuple of numpy arrays. The output
specification in the prototype is a tuple of shape tuples
*** Broadcasting in python
This is invoked as a decorator, applied to any function. An example:
>>> import numpysane as nps
>>> @nps.broadcast_define( (('n',), ('n',)) )
... def inner_product(a, b):
... return a.dot(b)
Here we have a simple inner product function to compute ONE inner product. The
'broadcast_define' decorator adds broadcasting-awareness: 'inner_product()'
expects two 1D vectors of length 'n' each (same 'n' for the two inputs),
vectorizing extra dimensions, as needed. The inputs are shape-checked, and
incompatible dimensions will trigger an exception. Example:
>>> import numpy as np
>>> a = np.arange(6).reshape(2,3)
>>> b = a + 100
>>> a
array([[0, 1, 2],
[3, 4, 5]])
>>> b
array([[100, 101, 102],
[103, 104, 105]])
>>> inner_product(a,b)
array([ 305, 1250])
Another related function in this module broadcast_generate(). It's similar to
broadcast_define(), but instead of adding broadcasting-awareness to an existing
function, it returns a generator that produces tuples from a set of arguments
according to a given prototype. Similarly, broadcast_extra_dims() is available
to report the outer shape of a potential broadcasting operation.
Stock numpy has some rudimentary support for all this with its vectorize()
function, but it assumes only scalar inputs and outputs, which severely limits
its usefulness. See the docstrings for 'broadcast_define' and
'broadcast_generate' in the INTERFACE section below for usage details.
*** Broadcasting in C
The python broadcasting is useful, but it is a python loop, so the loop itself
is computationally expensive if we have many iterations. If the function being
wrapped is available in C, we can apply broadcasting awareness in C, which makes
a much faster loop.
The "numpysane_pywrap" module generates code to wrap arbitrary C code in a
broadcasting-aware wrapper callable from python. This is an analogue of PDL::PP
(http://pdl.perl.org/PDLdocs/PP.html). This generated code is compiled and
linked into a python extension module, as usual. This functionality documented
separately: https://github.com/dkogan/numpysane/blob/master/README-pywrap.org
After I wrote this, I realized there is some support for this in stock numpy:
https://docs.scipy.org/doc/numpy-1.13.0/reference/c-api.ufunc.html
Note: I have not tried using these APIs.
** Nicer array manipulation
Numpy functions that move dimensions around and concatenate matrices are
unintuitive. For instance, a simple concatenation of a row-vector or a
column-vector to a matrix requires arcane knowledge to accomplish reliably. This
module provides new functions that can be used for these basic operations. These
new functions do have well-defined and sensible behavior, and they largely come
from the interfaces in PDL (http://pdl.perl.org). These all respect the core
rules of numpy broadcasting:
- LEADING length-1 dimensions don't affect the meaning of an array, so the
routines handle missing or extra length-1 dimensions at the front
- The inner-most dimensions of an array are the TRAILING ones, so whenever an
axis specification is used, it is strongly recommended (sometimes required) to
count the axes from the back by passing in axis<0
A high level description of the functionality is given here, and each function
is described in detail in the INTERFACE section below. In the following
examples, I use a function "arr" that returns a numpy array with given
dimensions:
>>> def arr(*shape):
... product = reduce( lambda x,y: x*y, shape)
... return numpy.arange(product).reshape(*shape)
>>> arr(1,2,3)
array([[[0, 1, 2],
[3, 4, 5]]])
>>> arr(1,2,3).shape
(1, 2, 3)
*** Concatenation
This module provides two functions to do this
**** glue
Concatenates some number of arrays along a given axis ('axis' must be given in a
kwarg). Implicit length-1 dimensions are added at the start as needed.
Dimensions other than the glueing axis must match exactly. Basic usage:
>>> row_vector = arr( 3,)
>>> col_vector = arr(5,1,)
>>> matrix = arr(5,3,)
>>> numpysane.glue(matrix, row_vector, axis = -2).shape
(6,3)
>>> numpysane.glue(matrix, col_vector, axis = -1).shape
(5,4)
**** cat
Concatenate some number of arrays along a new leading axis. Implicit length-1
dimensions are added, and the logical shapes of the inputs must match. This
function is a logical inverse of numpy array iteration: iteration splits an
array over its leading dimension, while cat joins a number of arrays via a new
leading dimension. Basic usage:
>>> numpysane.cat(arr(5,), arr(5,)).shape
(2,5)
>>> numpysane.cat(arr(5,), arr(1,1,5,)).shape
(2,1,1,5)
*** Manipulation of dimensions
Several functions are available, all being fairly direct ports of their PDL
(http://pdl.perl.org) equivalents
**** clump
Reshapes the array by grouping together 'n' dimensions, where 'n' is given in a
kwarg. If 'n' > 0, then n leading dimensions are clumped; if 'n' < 0, then -n
trailing dimensions are clumped. Basic usage:
>>> numpysane.clump( arr(2,3,4), n = -2).shape
(2, 12)
>>> numpysane.clump( arr(2,3,4), n = 2).shape
(6, 4)
**** atleast_dims
Adds length-1 dimensions at the front of an array so that all the given
dimensions are in-bounds. Any axis<0 may expand the shape. Adding new leading
dimensions (axis>=0) is never useful, since numpy broadcasts from the end, so
clump() treats axis>0 as a check only: the requested axis MUST already be
in-bounds, or an exception is thrown. This function always preserves the meaning
of all the axes in the array: axis=-1 is the same axis before and after the
call. Basic usage:
>>> numpysane.atleast_dims(arr(2,3), -1).shape
(2, 3)
>>> numpysane.atleast_dims(arr(2,3), -2).shape
(2, 3)
>>> numpysane.atleast_dims(arr(2,3), -3).shape
(1, 2, 3)
>>> numpysane.atleast_dims(arr(2,3), 0).shape
(2, 3)
>>> numpysane.atleast_dims(arr(2,3), 1).shape
(2, 3)
>>> numpysane.atleast_dims(arr(2,3), 2).shape
[exception]
**** mv
Moves a dimension from one position to another. Basic usage to move the last
dimension (-1) to the front (0)
>>> numpysane.mv(arr(2,3,4), -1, 0).shape
(4, 2, 3)
Or to move a dimension -5 (added implicitly) to the end
>>> numpysane.mv(arr(2,3,4), -5, -1).shape
(1, 2, 3, 4, 1)
**** xchg
Exchanges the positions of two dimensions. Basic usage to move the last
dimension (-1) to the front (0), and the front to the back.
>>> numpysane.xchg(arr(2,3,4), -1, 0).shape
(4, 3, 2)
Or to swap a dimension -5 (added implicitly) with dimension -2
>>> numpysane.xchg(arr(2,3,4), -5, -2).shape
(3, 1, 2, 1, 4)
**** transpose
Reverses the order of the two trailing dimensions in an array. The whole array
is seen as being an array of 2D matrices, each matrix living in the 2 most
significant dimensions, which implies this definition. Basic usage:
>>> numpysane.transpose( arr(2,3) ).shape
(3,2)
>>> numpysane.transpose( arr(5,2,3) ).shape
(5,3,2)
>>> numpysane.transpose( arr(3,) ).shape
(3,1)
Note that in the second example we had 5 matrices, and we transposed each one.
And in the last example we turned a row vector into a column vector by adding an
implicit leading length-1 dimension before transposing.
**** dummy
Adds a single length-1 dimension at the given position. Basic usage:
>>> numpysane.dummy(arr(2,3,4), -1).shape
(2, 3, 4, 1)
**** reorder
Reorders the dimensions in an array using the given order. Basic usage:
>>> numpysane.reorder( arr(2,3,4), -1, -2, -3 ).shape
(4, 3, 2)
>>> numpysane.reorder( arr(2,3,4), 0, -1, 1 ).shape
(2, 4, 3)
>>> numpysane.reorder( arr(2,3,4), -2 , -1, 0 ).shape
(3, 4, 2)
>>> numpysane.reorder( arr(2,3,4), -4 , -2, -5, -1, 0 ).shape
(1, 3, 1, 4, 2)
** Basic linear algebra
*** inner
Broadcast-aware inner product. Identical to numpysane.dot(). Basic usage to
compute 4 inner products of length 3 each:
>>> numpysane.inner(arr( 3,),
arr(4,3,)).shape
(4,)
>>> numpysane.inner(arr( 3,),
arr(4,3,))
array([5, 14, 23, 32])
*** dot
Broadcast-aware non-conjugating dot product. Identical to numpysane.inner().
*** vdot
Broadcast-aware conjugating dot product. Same as numpysane.dot(), except this
one conjugates complex input, which numpysane.dot() does not
*** outer
Broadcast-aware outer product. Basic usage to compute 4 outer products of length
3 each:
>>> numpysane.outer(arr( 3,),
arr(4,3,)).shape
array(4, 3, 3)
*** norm2
Broadcast-aware 2-norm. numpysane.norm2(x) is identical to numpysane.inner(x,x):
>>> numpysane.norm2(arr(4,3))
array([5, 50, 149, 302])
*** mag
Broadcast-aware vector magnitude. mag(x) is functionally identical to
sqrt(numpysane.norm2(x)) and sqrt(numpysane.inner(x,x))
>>> numpysane.mag(arr(4,3))
array([ 2.23606798, 7.07106781, 12.20655562, 17.3781472 ])
*** trace
Broadcast-aware matrix trace.
>>> numpysane.trace(arr(4,3,3))
array([12., 39., 66., 93.])
*** matmult
Broadcast-aware matrix multiplication. This accepts an arbitrary number of
inputs, and adds leading length-1 dimensions as needed. Multiplying a row-vector
by a matrix
>>> numpysane.matmult( arr(3,), arr(3,2) ).shape
(2,)
Multiplying a row-vector by 5 different matrices:
>>> numpysane.matmult( arr(3,), arr(5,3,2) ).shape
(5, 2)
Multiplying a matrix by a col-vector:
>>> numpysane.matmult( arr(3,2), arr(2,1) ).shape
(3, 1)
Multiplying a row-vector by a matrix by a col-vector:
>>> numpysane.matmult( arr(3,), arr(3,2), arr(2,1) ).shape
(1,)
** What's wrong with existing numpy functions?
Why did I go through all the trouble to reimplement all this? Doesn't numpy
already do all these things? Yes, it does. But in a very nonintuitive way.
*** Concatenation
**** hstack()
hstack() performs a "horizontal" concatenation. When numpy prints an array, this
is the last dimension (the most significant dimensions in numpy are at the end).
So one would expect that this function concatenates arrays along this last
dimension. In the special case of 1D and 2D arrays, one would be right:
>>> numpy.hstack( (arr(3), arr(3))).shape
(6,)
>>> numpy.hstack( (arr(2,3), arr(2,3))).shape
(2, 6)
but in any other case, one would be wrong:
>>> numpy.hstack( (arr(1,2,3), arr(1,2,3))).shape
(1, 4, 3) <------ I expect (1, 2, 6)
>>> numpy.hstack( (arr(1,2,3), arr(1,2,4))).shape
[exception] <------ I expect (1, 2, 7)
>>> numpy.hstack( (arr(3), arr(1,3))).shape
[exception] <------ I expect (1, 6)
>>> numpy.hstack( (arr(1,3), arr(3))).shape
[exception] <------ I expect (1, 6)
The above should all succeed, and should produce the shapes as indicated. Cases
such as "numpy.hstack( (arr(3), arr(1,3)))" are maybe up for debate, but
broadcasting rules allow adding as many extra length-1 dimensions as we want
without changing the meaning of the object, so I claim this should work. Either
way, if you print out the operands for any of the above, you too would expect a
"horizontal" stack() to work as stated above.
It turns out that normally hstack() concatenates along axis=1, unless the first
argument only has one dimension, in which case axis=0 is used. In a system where
the most significant dimension is the last one, this is only correct if everyone
has only 2D arrays. The correct way to do this is to concatenate along axis=-1.
It works for n-dimensionsal objects, and doesn't require the special case logic
for 1-dimensional objects.
**** vstack()
Similarly, vstack() performs a "vertical" concatenation. When numpy prints an
array, this is the second-to-last dimension (remember, the most significant
dimensions in numpy are at the end). So one would expect that this function
concatenates arrays along this second-to-last dimension. Again, in the special
case of 1D and 2D arrays, one would be right:
>>> numpy.vstack( (arr(2,3), arr(2,3))).shape
(4, 3)
>>> numpy.vstack( (arr(3), arr(3))).shape
(2, 3)
>>> numpy.vstack( (arr(1,3), arr(3))).shape
(2, 3)
>>> numpy.vstack( (arr(3), arr(1,3))).shape
(2, 3)
>>> numpy.vstack( (arr(2,3), arr(3))).shape
(3, 3)
Note that this function appears to tolerate some amount of shape mismatches. It
does it in a form one would expect, but given the state of the rest of this
system, I found it surprising. For instance "numpy.hstack( (arr(1,3), arr(3)))"
fails, so one would think that "numpy.vstack( (arr(1,3), arr(3)))" would fail
too.
And once again, adding more dimensions make it confused, for the same reason:
>>> numpy.vstack( (arr(1,2,3), arr(2,3))).shape
[exception] <------ I expect (1, 4, 3)
>>> numpy.vstack( (arr(1,2,3), arr(1,2,3))).shape
(2, 2, 3) <------ I expect (1, 4, 3)
Similarly to hstack(), vstack() concatenates along axis=0, which is "vertical"
only for 2D arrays, but not for any others. And similarly to hstack(), the 1D
case has special-cased logic to make it work properly.
The correct way to do this is to concatenate along axis=-2. It works for
n-dimensionsal objects, and doesn't require the special case for 1-dimensional
objects.
**** dstack()
I'll skip the detailed description, since this is similar to hstack() and
vstack(). The intent was to concatenate across axis=-3, but the implementation
takes axis=2 instead. This is wrong, as before. And I find it strange that these
3 functions even exist, since they are all special-cases: the concatenation axis
should be an argument, and at most, the edge special case (hstack()) should
exist. This brings us to the next function
**** concatenate()
This is a more general function, and unlike hstack(), vstack() and dstack(), it
takes as input a list of arrays AND the concatenation dimension. It accepts
negative concatenation dimensions to allow us to count from the end, so things
should work better. And in many cases that failed previously, they do:
>>> numpy.concatenate( (arr(1,2,3), arr(1,2,3)), axis=-1).shape
(1, 2, 6)
>>> numpy.concatenate( (arr(1,2,3), arr(1,2,4)), axis=-1).shape
(1, 2, 7)
>>> numpy.concatenate( (arr(1,2,3), arr(1,2,3)), axis=-2).shape
(1, 4, 3)
But many things still don't work as I would expect:
>>> numpy.concatenate( (arr(1,3), arr(3)), axis=-1).shape
[exception] <------ I expect (1, 6)
>>> numpy.concatenate( (arr(3), arr(1,3)), axis=-1).shape
[exception] <------ I expect (1, 6)
>>> numpy.concatenate( (arr(1,3), arr(3)), axis=-2).shape
[exception] <------ I expect (3, 3)
>>> numpy.concatenate( (arr(3), arr(1,3)), axis=-2).shape
[exception] <------ I expect (2, 3)
>>> numpy.concatenate( (arr(2,3), arr(2,3)), axis=-3).shape
[exception] <------ I expect (2, 2, 3)
This function works as expected only if
- All inputs have the same number of dimensions
- All inputs have a matching shape, except for the dimension along which we're
concatenating
- All inputs HAVE the dimension along which we're concatenating
A common use case that violates these conditions: I have an object that contains
N 3D vectors, and I want to add another 3D vector to it. This is essentially the
first failing example above.
**** stack()
The name makes it sound exactly like concatenate(), and it takes the same
arguments, but it is very different. stack() requires that all inputs have
EXACTLY the same shape. It then concatenates all the inputs along a new
dimension, and places that dimension in the location given by the 'axis' input.
If this is the exact type of concatenation you want, this function works fine.
But it's one of many things a user may want to do.
**** Thoughts on concatenation
This module introduces numpysane.glue() and numpysane.cat() to replace all the
above functions. These do not refer to anything being "horizontal" or
"vertical", nor do they talk about "rows" or "columns": these concepts simply
don't apply in a generic N-dimensional system. These functions are very explicit
about the dimensionality of the inputs/outputs, and fit well into a
broadcasting-aware system.
Since these functions assume that broadcasting is an important concept in the
system, the given axis indices should be counted from the most significant
dimension: the last dimension in numpy. This means that where an axis index is
specified, negative indices are encouraged. glue() forbids axis>=0 outright.
***** Example for further justification
An array containing N 3D vectors would have shape (N,3). Another array
containing a single 3D vector would have shape (3,). Counting the dimensions
from the end, each vector is indexed in dimension -1. However, counting from the
front, the vector is indexed in dimension 0 or 1, depending on which of the two
arrays we're looking at. If we want to add the single vector to the array
containing the N vectors, and we mistakenly try to concatenate along the first
dimension, it would fail if N != 3. But if we're unlucky, and N=3, then we'd get
a nonsensical output array of shape (3,4). Why would an array of N 3D vectors
have shape (N,3) and not (3,N)? Because if we apply python iteration to it, we'd
expect to get N iterates of arrays with shape (3,) each, and numpy iterates from
the first dimension:
>>> a = numpy.arange(2*3).reshape(2,3)
>>> a
array([[0, 1, 2],
[3, 4, 5]])
>>> [x for x in a]
[array([0, 1, 2]), array([3, 4, 5])]
*** Manipulation of dimensions
**** atleast_xd()
Numpy has 3 special-case functions atleast_1d(), atleast_2d() and atleast_3d().
For 4d and higher, you need to do something else. These do surprising things:
>>> numpy.atleast_3d(arr(3)).shape
(1, 3, 1)
**** transpose()
Given a matrix (a 2D array), numpy.transpose() swaps the two dimensions, as
expected. Given anything else, it does not do what is expected:
>>> numpy.transpose(arr(3, )).shape
(3,)
>>> numpy.transpose(arr(3,4, )).shape
(4, 3)
>>> numpy.transpose(arr(3,4,5,6,)).shape
(6, 5, 4, 3)
I.e. numpy.transpose() reverses the order of ALL dimensions in the array. So if
we have N 2D matrices in a single array, numpy.transpose() doesn't transpose
each matrix.
*** Basic linear algebra
**** inner() and dot()
numpy.inner() and numpy.dot() are strange. In a real-valued n-dimensional
Euclidean space, a "dot product" is just another name for an "inner product".
Numpy disagrees.
It looks like numpy.dot() is matrix multiplication, with some wonky behaviors
when given higher-dimension objects, and with some special-case behaviors for
1-dimensional and 0-dimensional objects:
>>> numpy.dot( arr(4,5,2,3), arr(3,5)).shape
(4, 5, 2, 5) <--- expected result for a broadcasted matrix multiplication
>>> numpy.dot( arr(3,5), arr(4,5,2,3)).shape
[exception] <--- numpy.dot() is not commutative.
Expected for matrix multiplication, but not for a dot
product
>>> numpy.dot( arr(4,5,2,3), arr(1,3,5)).shape
(4, 5, 2, 1, 5) <--- don't know where this came from at all
>>> numpy.dot( arr(4,5,2,3), arr(3)).shape
(4, 5, 2) <--- 1D special case. This is a dot product.
>>> numpy.dot( arr(4,5,2,3), 3).shape
(4, 5, 2, 3) <--- 0D special case. This is a scaling.
It looks like numpy.inner() is some sort of quasi-broadcastable inner product, also
with some funny dimensioning rules. In many cases it looks like numpy.dot(a,b) is
the same as numpy.inner(a, transpose(b)) where transpose() swaps the last two
dimensions:
>>> numpy.inner( arr(4,5,2,3), arr(5,3)).shape
(4, 5, 2, 5) <--- All the length-3 inner products collected into a shape
with not-quite-broadcasting rules
>>> numpy.inner( arr(5,3), arr(4,5,2,3)).shape
(5, 4, 5, 2) <--- numpy.inner() is not commutative. Unexpected
for an inner product
>>> numpy.inner( arr(4,5,2,3), arr(1,5,3)).shape
(4, 5, 2, 1, 5) <--- No idea
>>> numpy.inner( arr(4,5,2,3), arr(3)).shape
(4, 5, 2) <--- 1D special case. This is a dot product.
>>> numpy.inner( arr(4,5,2,3), 3).shape
(4, 5, 2, 3) <--- 0D special case. This is a scaling.
'''
import numpy as np
from functools import reduce
import itertools
import types
import inspect
from distutils.version import StrictVersion
# setup.py assumes the version is a simple string in '' quotes
__version__ = '0.42'
def _product(l):
r'''Returns product of all values in the given list'''
return reduce( lambda a,b: a*b, l )
def _clone_function(f, name):
r'''Returns a clone of a given function.
This is useful to copy a function, updating its metadata, such as the
documentation, name, etc. There are also differences here between python 2
and python 3 that this function handles.
'''
def get(f, what):
what2 = 'func_{}'.format(what)
what3 = '__{}__' .format(what)
try:
return getattr(f, what2)
except:
try:
return getattr(f, what3)
except:
pass
return None
return types.FunctionType(get(f, 'code'),
get(f, 'globals'),
name,
get(f, 'defaults'),
get(f, 'closure'))
class NumpysaneError(Exception):
def __init__(self, err): self.err = err
def __str__(self): return self.err
def _eval_broadcast_dims( args, prototype ):
r'''Helper function to evaluate a given list of arguments in respect to a given
broadcasting prototype. This function will flag any errors in the
dimensionality of the inputs. If no errors are detected, it returns
dims_extra,dims_named
where
dims_extra is the outer shape of the broadcast
This is a list: the union of all the leading shapes of all the
arguments, after the trailing shapes of the prototype have been stripped
dims_named is the sizes of the named dimensions
This is a dict mapping dimension names to their sizes
'''
# First I initialize dims_extra: the array containing the broadcasted
# slices. Each argument calls for some number of extra dimensions, and the
# overall array is as large as the biggest one of those
Ndims_extra = 0
for i_arg in range(len(args)):
Ndims_extra_here = len(args[i_arg].shape) - len(prototype[i_arg])
if Ndims_extra_here > Ndims_extra:
Ndims_extra = Ndims_extra_here
dims_extra = [1] * Ndims_extra
def parse_dim( name_arg,
shape_prototype, shape_arg, dims_named ):
def range_rev(n):
r'''Returns a range from -1 to -n.
Useful to index variable-sized lists while aligning their ends.'''
return range(-1, -n-1, -1)
# first, I make sure the input is at least as dimension-ful as the
# prototype. I do this by prepending dummy dimensions of length-1 as
# necessary
if len(shape_prototype) > len(shape_arg):
ndims_missing_here = len(shape_prototype) - len(shape_arg)
shape_arg = (1,) * ndims_missing_here + shape_arg
# MAKE SURE THE PROTOTYPE DIMENSIONS MATCH (the trailing dimensions)
#
# Loop through the dimensions. Set the dimensionality of any new named
# argument to whatever the current argument has. Any already-known
# argument must match
for i_dim in range_rev(len(shape_prototype)):
dim_prototype = shape_prototype[i_dim]
if not isinstance(dim_prototype, int):
# This is a named dimension. These can have any value, but ALL
# dimensions of the same name must thave the SAME value
# EVERYWHERE
if dim_prototype not in dims_named:
dims_named[dim_prototype] = shape_arg[i_dim]
dim_prototype = dims_named[dim_prototype]
# The prototype dimension (named or otherwise) now has a numeric
# value. Make sure it matches what I have
if dim_prototype != shape_arg[i_dim]:
raise NumpysaneError("Argument {} dimension '{}': expected {} but got {}".
format(name_arg,
shape_prototype[i_dim],
dim_prototype,
shape_arg[i_dim]))
# I now know that this argument matches the prototype. I look at the
# extra dimensions to broadcast, and make sure they match with the
# dimensions I saw previously
Ndims_extra_here = len(shape_arg) - len(shape_prototype)
# MAKE SURE THE BROADCASTED DIMENSIONS MATCH (the leading dimensions)
#
# This argument has Ndims_extra_here dimensions to broadcast. The
# current shape to broadcast must be at least as large, and must match
for i_dim in range_rev(Ndims_extra_here):
dim_arg = shape_arg[i_dim - len(shape_prototype)]
if dim_arg != 1:
if dims_extra[i_dim] == 1:
dims_extra[i_dim] = dim_arg
elif dims_extra[i_dim] != dim_arg:
raise NumpysaneError("Argument {} prototype {} extra broadcast dim {} mismatch: previous arg set this to {}, but this arg wants {}".
format(name_arg,
shape_prototype,
i_dim,
dims_extra[i_dim],
dim_arg))
dims_named = {} # parse_dim() adds to this
for i_arg in range(len(args)):
parse_dim( i_arg,
prototype[i_arg], args[i_arg].shape,
dims_named )
return dims_extra,dims_named
def _broadcast_iter_dim( args, prototype, dims_extra ):
r'''Generator to iterate through all the broadcasting slices.
'''
# pad the dimension of each arg with ones. This lets me use the full
# dims_extra index on each argument, without worrying about overflow
args = [ atleast_dims(args[i], -(len(prototype[i])+len(dims_extra)) ) for i in range(len(args)) ]
# per-arg dims_extra indexing varies: len-1 dimensions always index at 0. I
# make a mask that I apply each time
idx_slice_mask = np.ones( (len(args), len(dims_extra)), dtype=int)
for i in range(len(args)):
idx_slice_mask[i, np.array(args[i].shape,dtype=int)[:len(dims_extra)]==1] = 0
for idx_slice in itertools.product( *(range(x) for x in dims_extra) ):
# tuple(idx) because of wonky behavior differences:
# >>> a
# array([[0, 1, 2],
# [3, 4, 5]])
#
# >>> a[tuple((1,1))]
# 4
#
# >>> a[list((1,1))]
# array([[3, 4, 5],
# [3, 4, 5]])
yield tuple( args[i][tuple(idx_slice *
idx_slice_mask[i])] for i in range(len(args)) )
def broadcast_define(prototype, prototype_output=None, out_kwarg=None):
r'''Vectorizes an arbitrary function, expecting input as in the given prototype.
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> @nps.broadcast_define( (('n',), ('n',)) )
... def inner_product(a, b):
... return a.dot(b)
>>> a = np.arange(6).reshape(2,3)
>>> b = a + 100
>>> a
array([[0, 1, 2],
[3, 4, 5]])
>>> b
array([[100, 101, 102],
[103, 104, 105]])
>>> inner_product(a,b)
array([ 305, 1250])
The prototype defines the dimensionality of the inputs. In the inner product
example above, the input is two 1D n-dimensional vectors. In particular, the
'n' is the same for the two inputs. This function is intended to be used as
a decorator, applied to a function defining the operation to be vectorized.
Each element in the prototype list refers to each input, in order. In turn,
each such element is a list that describes the shape of that input. Each of
these shape descriptors can be any of
- a positive integer, indicating an input dimension of exactly that length
- a string, indicating an arbitrary, but internally consistent dimension
The normal numpy broadcasting rules (as described elsewhere) apply. In
summary:
- Dimensions are aligned at the end of the shape list, and must match the
prototype
- Extra dimensions left over at the front must be consistent for all the
input arguments, meaning:
- All dimensions of length != 1 must match
- Dimensions of length 1 match corresponding dimensions of any length in
other arrays
- Missing leading dimensions are implicitly set to length 1
- The output(s) have a shape where
- The trailing dimensions are whatever the function being broadcasted
returns
- The leading dimensions come from the extra dimensions in the inputs
Calling a function wrapped with broadcast_define() with extra arguments
(either positional or keyword), passes these verbatim to the inner function.
Only the arguments declared in the prototype are broadcast.
Scalars are represented as 0-dimensional numpy arrays: arrays with shape (),
and these broadcast as one would expect:
>>> @nps.broadcast_define( (('n',), ('n',), ()))
... def scaled_inner_product(a, b, scale):
... return a.dot(b)*scale
>>> a = np.arange(6).reshape(2,3)
>>> b = a + 100
>>> scale = np.array((10,100))
>>> a
array([[0, 1, 2],
[3, 4, 5]])
>>> b
array([[100, 101, 102],
[103, 104, 105]])
>>> scale
array([ 10, 100])
>>> scaled_inner_product(a,b,scale)
array([[ 3050],
[125000]])
Let's look at a more involved example. Let's say we have a function that
takes a set of points in R^2 and a single center point in R^2, and finds a
best-fit least-squares line that passes through the given center point. Let
it return a 3D vector containing the slope, y-intercept and the RMS residual
of the fit. This broadcasting-enabled function can be defined like this:
import numpy as np
import numpysane as nps
@nps.broadcast_define( (('n',2), (2,)) )
def fit(xy, c):
# line-through-origin-model: y = m*x
# E = sum( (m*x - y)**2 )
# dE/dm = 2*sum( (m*x-y)*x ) = 0
# ----> m = sum(x*y)/sum(x*x)
x,y = (xy - c).transpose()
m = np.sum(x*y) / np.sum(x*x)
err = m*x - y
err **= 2
rms = np.sqrt(err.mean())
# I return m,b because I need to translate the line back
b = c[1] - m*c[0]
return np.array((m,b,rms))
And I can use broadcasting to compute a number of these fits at once. Let's
say I want to compute 4 different fits of 5 points each. I can do this:
n = 5
m = 4
c = np.array((20,300))
xy = np.arange(m*n*2, dtype=np.float64).reshape(m,n,2) + c
xy += np.random.rand(*xy.shape)*5
res = fit( xy, c )
mb = res[..., 0:2]
rms = res[..., 2]
print "RMS residuals: {}".format(rms)
Here I had 4 different sets of points, but a single center point c. If I
wanted 4 different center points, I could pass c as an array of shape (4,2).
I can use broadcasting to plot all the results (the points and the fitted
lines):
import gnuplotlib as gp
gp.plot( *nps.mv(xy,-1,0), _with='linespoints',
equation=['{}*x + {}'.format(mb_single[0],
mb_single[1]) for mb_single in mb],
unset='grid', square=1)
The examples above all create a separate output array for each broadcasted
slice, and copy the contents from each such slice into the larger output
array that contains all the results. This is inefficient, and it is possible
to pre-allocate an array to forgo these extra allocation and copy
operations. There are several settings to control this. If the function
being broadcasted can write its output to a given array instead of creating
a new one, most of the inefficiency goes away. broadcast_define() supports
the case where this function takes this array in a kwarg: the name of this
kwarg can be given to broadcast_define() like so:
@nps.broadcast_define( ....., out_kwarg = "out" )
def func( ....., *, out):
.....
out[:] = result
When used this way, the return value of the broadcasted function is ignored.
In order for broadcast_define() to pass such an output array to the inner
function, this output array must be available, which means that it must be
given to us somehow, or we must create it.
The most efficient way to make a broadcasted call is to create the full
output array beforehand, and to pass that to the broadcasted function. In
this case, nothing extra will be allocated, and no unnecessary copies will
be made. This can be done like this:
@nps.broadcast_define( (('n',), ('n',)), ....., out_kwarg = "out" )
def inner_product(a, b, *, out):
.....
out.setfield(a.dot(b), out.dtype)
out = np.empty((2,4), np.float)
inner_product( np.arange(3), np.arange(2*4*3).reshape(2,4,3), out=out)
In this example, the caller knows that it's calling an inner_product
function, and that the shape of each output slice would be (). The caller
also knows the input dimensions and that we have an extra broadcasting
dimension (2,4), so the output array will have shape (2,4) + () = (2,4).
With this knowledge, the caller preallocates the array, and passes it to the
broadcasted function call. Furthermore, in this case the inner function will
be called with an output array EVERY time, and this is the only mode the
inner function needs to support.
If the caller doesn't know (or doesn't want to pre-compute) the shape of the
output, it can let the broadcasting machinery create this array for them. In
order for this to be possible, the shape of the output should be
pre-declared, and the dtype of the output should be known:
@nps.broadcast_define( (('n',), ('n',)),
(),
out_kwarg = "out" )
def inner_product(a, b, *, out, dtype):
.....
out.setfield(a.dot(b), out.dtype)
out = inner_product( np.arange(3), np.arange(2*4*3).reshape(2,4,3), dtype=int)
Note that the caller didn't need to specify the prototype of the output or
the extra broadcasting dimensions (output prototype is in the
broadcast_define() call, but not the inner_product() call). Specifying the
dtype here is optional: it defaults to float if omitted. If the dtype IS
given, the inner function must take a "dtype" argument; to use in cases
where out_kwarg isn't given, and the output array must be created by the
inner function.
If we want the
output array to be pre-allocated, the output prototype (it is () in this
example) is required: we must know the shape of the output array in order to
create it.
Without a declared output prototype, we can still make mostly- efficient
calls: the broadcasting mechanism can call the inner function for the first
slice as we showed earlier, by creating a new array for the slice. This new
array required an extra allocation and copy, but it contains the required
shape information. This infomation will be used to allocate the output, and
the subsequent calls to the inner function will be efficient:
@nps.broadcast_define( (('n',), ('n',)),
out_kwarg = "out" )
def inner_product(a, b, *, out=None):
.....
if out is None:
return a.dot(b)
out.setfield(a.dot(b), out.dtype)
return out
out = inner_product( np.arange(3), np.arange(2*4*3).reshape(2,4,3))
Here we were slighly inefficient, but the ONLY required extra specification
was out_kwarg: that's all you need. Also it is important to note that in
this case the inner function is called both with passing it an output array
to fill in, and with asking it to create a new one (by passing out=None to
the inner function). This inner function then must support both modes of
operation. If the inner function does not support filling in an output
array, none of these efficiency improvements are possible.
It is possible for a function to return more than one output, and this is
supported by broadcast_define(). This case works exactly like the one-output
case, except the output prototype is REQUIRED, and this output prototype
contains multiple tuples, one for each output. The inner function must
return the outputs in a tuple, and each individual output will be
broadcasted as expected.
broadcast_define() is analogous to thread_define() in PDL.
'''
def inner_decorator_for_some_reason(func):
# args broadcast, kwargs do not. All auxillary data should go into the
# kwargs
def broadcast_loop(*args, **kwargs):
if len(args) < len(prototype):
raise NumpysaneError("Mismatched number of input arguments. Wanted at least {} but got {}". \
format(len(prototype), len(args)))
args_passthru = args[ len(prototype):]
args = args[0:len(prototype) ]
# make sure all the arguments are numpy arrays
args = tuple(np.asarray(arg) for arg in args)
# dims_extra: extra dimensions to broadcast through
# dims_named: values of the named dimensions
dims_extra,dims_named = \
_eval_broadcast_dims( args, prototype)
# If None, the single output is either returned, or stored into
# out_kwarg. If an integer, then a tuple is returned (or stored into
# out_kwarg). If Noutputs==1 then we return a TUPLE of length 1
Noutputs = None
# substitute named variable values into the output prototype
prototype_output_expanded = None
if prototype_output is not None:
# If a single prototype_output is given, wrap it in a tuple to indicate
# that we only have one output
if all( type(o) is int or type(o) is str for o in prototype_output ):
prototype_output_expanded = \
[d if type(d) is int else dims_named[d] \
for d in prototype_output]
else:
Noutputs = len(prototype_output)
prototype_output_expanded = \
[ [d if type(d) is int else dims_named[d] \
for d in _prototype_output] for \
_prototype_output in prototype_output ]
# I checked all the dimensions and aligned everything. I have my
# to-broadcast dimension counts. Iterate through all the broadcasting
# output, and gather the results
output = None
i_slice = 0
if Noutputs is None:
# We expect a SINGLE output
# if the output was supposed to go to a particular place, set that
if out_kwarg is not None and out_kwarg in kwargs:
output = kwargs[out_kwarg]
if prototype_output_expanded is not None:
expected_shape = dims_extra + prototype_output_expanded
if output.shape != tuple(expected_shape):
raise NumpysaneError("Inconsistent output shape: expected {}, but got {}".format(expected_shape, output.shape))
# if we know enough to allocate the output, do that
elif prototype_output_expanded is not None:
kwargs_dtype = {}
if 'dtype' in kwargs:
kwargs_dtype['dtype'] = kwargs['dtype']
output = np.empty(dims_extra + prototype_output_expanded,
**kwargs_dtype)
# else:
# We don't have an output and we don't know its dimensions, so
# we can't allocate an array for it. Leave output as None. I
# will allocate it as soon I get the first slice; this will let
# me know how large the whole thing should be
# if no broadcasting involved, just call the function
if not dims_extra:
# if the function knows how to write directly to an array,
# request that
if output is not None and out_kwarg is not None:
kwargs[out_kwarg] = output
sliced_args = args + args_passthru
result = func( *sliced_args, **kwargs )
if out_kwarg is not None and \
kwargs.get(out_kwarg) is not None:
# We wrote the output in-place. Return the output array
return kwargs.get(out_kwarg)
# Using the returned output. Run some checks, and return the
# returned value
if isinstance(result, tuple):
raise NumpysaneError("Only a single output expected, but a tuple was returned!")
if prototype_output_expanded is not None and \
np.array(result).shape != tuple(prototype_output_expanded):
raise NumpysaneError("Inconsistent slice output shape: expected {}, but got {}".format(prototype_output_expanded,
np.array(result).shape))
return result
# reshaped output. I write to this array
if output is not None:
output_flattened = clump(output, n=len(dims_extra))
for x in _broadcast_iter_dim( args, prototype, dims_extra ):
# if the function knows how to write directly to an array,
# request that
if output is not None and out_kwarg is not None:
kwargs[out_kwarg] = output_flattened[i_slice, ...]
sliced_args = x + args_passthru
result = func( *sliced_args, **kwargs )
if output is None or out_kwarg is None:
# We weren't writing directly into the output, so check
# the output for validity
if isinstance(result, tuple):
raise NumpysaneError("Only a single output expected, but a tuple was returned!")
if not isinstance(result, np.ndarray):
result = np.array(result)
if prototype_output_expanded is None:
prototype_output_expanded = result.shape
else:
if result.shape != tuple(prototype_output_expanded):
raise NumpysaneError("Inconsistent slice output shape: expected {}, but got {}".format(prototype_output_expanded, result.shape))
if output is None:
# I didn't already have an output array because I didn't
# know how large it should be. But I now have the first
# slice, and I know how big the whole output should be.
# I create it
output = np.empty( dims_extra + list(result.shape),
dtype = result.dtype)
output_flattened = output.reshape( (_product(dims_extra),) + result.shape)
output_flattened[i_slice, ...] = result
elif out_kwarg is None:
output_flattened[i_slice, ...] = result
# else:
# I was writing directly to the output, so I don't need to
# manually populate the slice
i_slice = i_slice+1
else:
# We expect MULTIPLE outputs: a tuple of length Noutputs
# if the output was supposed to go to a particular place, set that
if out_kwarg is not None and out_kwarg in kwargs:
output = kwargs[out_kwarg]
if prototype_output_expanded is not None:
for i in range(Noutputs):
expected_shape = dims_extra + prototype_output_expanded[i]
if output[i].shape != tuple(expected_shape):
raise NumpysaneError("Inconsistent output shape for output {}: expected {}, but got {}". \
format(i, expected_shape, output[i].shape))
# if we know enough to allocate the output, do that
elif prototype_output_expanded is not None:
kwargs_dtype = {}
if 'dtype' in kwargs:
kwargs_dtype['dtype'] = kwargs['dtype']
output = [np.empty(dims_extra + prototype_output_expanded[i],
**kwargs_dtype) for i in range(Noutputs)]
# else:
# We don't have an output and we don't know its dimensions, so
# we can't allocate an array for it. Leave output as None. I
# will allocate it as soon I get the first slice; this will let
# me know how large the whole thing should be
# if no broadcasting involved, just call the function
if not dims_extra:
# if the function knows how to write directly to an array,
# request that
if output is not None and out_kwarg is not None:
kwargs[out_kwarg] = tuple(output)
sliced_args = args + args_passthru
result = func( *sliced_args, **kwargs )
if out_kwarg is not None and \
kwargs.get(out_kwarg) is not None:
# We wrote the output in-place. Return the output array
return kwargs.get(out_kwarg)
if not isinstance(result, tuple):
raise NumpysaneError("A tuple of {} outputs is expected, but an object of type {} was returned". \
format(Noutputs, type(result)))
if len(result) != Noutputs:
raise NumpysaneError("A tuple of {} outputs is expected, but a length-{} tuple was returned". \
format(Noutputs, len(result)))
if prototype_output_expanded is not None:
for i in range(Noutputs):
if np.array(result[i]).shape != tuple(prototype_output_expanded[i]):
raise NumpysaneError("Inconsistent output {} shape: expected {}, but got {}". \
format(i,
prototype_output_expanded[i],
np.array(result[i]).shape))
return result
# reshaped output. I write to this array
if output is not None:
output_flattened = [clump(output[i], n=len(dims_extra)) for i in range(Noutputs)]
for x in _broadcast_iter_dim( args, prototype, dims_extra ):
# if the function knows how to write directly to an array,
# request that
if output is not None and out_kwarg is not None:
kwargs[out_kwarg] = tuple(o[i_slice, ...] for o in output_flattened)
sliced_args = x + args_passthru
result = func( *sliced_args, **kwargs )
if output is None or out_kwarg is None:
# We weren't writing directly into the output, so check
# the output for validity
if not isinstance(result, tuple):
raise NumpysaneError("A tuple of {} outputs is expected, but an object of type {} was returned". \
format(Noutputs, type(result)))
if len(result) != Noutputs:
raise NumpysaneError("A tuple of {} outputs is expected, but a length-{} tuple was returned". \
format(Noutputs, len(result)))
result = [x if isinstance(x, np.ndarray) else np.array(x) for x in result]
if prototype_output_expanded is None:
prototype_output_expanded = [result[i].shape for i in range(Noutputs)]
else:
for i in range(Noutputs):
if result[i].shape != tuple(prototype_output_expanded[i]):
raise NumpysaneError("Inconsistent slice output {} shape: expected {}, but got {}". \
format(i, prototype_output_expanded[i], result[i].shape))
if output is None:
# I didn't already have an output array because I didn't
# know how large it should be. But I now have the first
# slice, and I know how big the whole output should be.
# I create it
output = [np.empty( dims_extra + list(result[i].shape),
dtype = result[i].dtype) for i in range(Noutputs)]
output_flattened = [output[i].reshape( (_product(dims_extra),) + result[i].shape) for i in range(Noutputs)]
for i in range(Noutputs):
output_flattened[i][i_slice, ...] = result[i]
elif out_kwarg is None:
for i in range(Noutputs):
output_flattened[i][i_slice, ...] = result[i]
# else:
# I was writing directly to the output, so I don't need to
# manually populate the slice
i_slice = i_slice+1
return output
if out_kwarg is not None and not isinstance(out_kwarg, str):
raise NumpysaneError("out_kwarg must be a string")
# Make sure all dimensions are >=0 and that named output dimensions are
# known from the input
known_named_dims = set()
if not isinstance(prototype, tuple):
raise NumpysaneError("Input prototype must be given as a tuple")
for dims_input in prototype:
if not isinstance(dims_input, tuple):
raise NumpysaneError("Input prototype dims must be given as a tuple")
for dim in dims_input:
if type(dim) is not int:
if type(dim) is not str:
raise NumpysaneError("Prototype dimensions must be integers > 0 or strings. Got '{}' of type '{}'". \
format(dim, type(dim)))
known_named_dims.add(dim)
else:
if dim < 0:
raise NumpysaneError("Prototype dimensions must be > 0. Got '{}'". \
format(dim))
if prototype_output is not None:
if not isinstance(prototype_output, tuple):
raise NumpysaneError("Output prototype dims must be given as a tuple")
# If a single prototype_output is given, wrap it in a tuple to indicate
# that we only have one output
if all( type(o) is int or type(o) is str for o in prototype_output ):
prototype_outputs = (prototype_output, )
else:
prototype_outputs = prototype_output
if not all( isinstance(p,tuple) for p in prototype_outputs ):
raise NumpysaneError("Output dimensions must be integers > 0 or strings. Each output must be a tuple. Some given output aren't tuples: {}". \
format(prototype_outputs))
for dims_output in prototype_outputs:
for dim in dims_output:
if type(dim) is not int:
if type(dim) is not str:
raise NumpysaneError("Output dimensions must be integers > 0 or strings. Got '{}' of type '{}'". \
format(dim, type(dim)))
if dim not in known_named_dims:
raise NumpysaneError("Output prototype has named dimension '{}' not seen in the input prototypes". \
format(dim))
else:
if dim < 0:
raise NumpysaneError("Prototype dimensions must be > 0. Got '{}'". \
format(dim))
func_out = _clone_function( broadcast_loop, func.__name__ )
func_out.__doc__ = inspect.getdoc(func)
if func_out.__doc__ is None:
func_out.__doc__ = ''
func_out.__doc__+= \
'''\n\nThis function is broadcast-aware through numpysane.broadcast_define().
The expected inputs have input prototype:
{prototype}
{output_prototype_text}
The first {nargs} positional arguments will broadcast. The trailing shape of
those arguments must match the input prototype; the leading shape must follow
the standard broadcasting rules. Positional arguments past the first {nargs} and
all the keyword arguments are passed through untouched.'''. \
format(prototype = prototype,
output_prototype_text = 'No output prototype is defined.' if prototype_output is None else
'and output prototype\n\n {}'.format(prototype_output),
nargs = len(prototype))
return func_out
return inner_decorator_for_some_reason
def broadcast_generate(prototype, args):
r'''A generator that produces broadcasted slices
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(6).reshape(2,3)
>>> b = a + 100
>>> a
array([[0, 1, 2],
[3, 4, 5]])
>>> b
array([[100, 101, 102],
[103, 104, 105]])
>>> for s in nps.broadcast_generate( (('n',), ('n',)), (a,b)):
... print "slice: {}".format(s)
slice: (array([0, 1, 2]), array([100, 101, 102]))
slice: (array([3, 4, 5]), array([103, 104, 105]))
The broadcasting operation of numpysane is described in detail in the
numpysane.broadcast_define() docstring and in the main README of numpysane.
This function can be used as a Python generator to produce each broadcasted
slice one by one
Since Python generators are inherently 1-dimensional, this function
effectively flattens the broadcasted results. If the correct output shape
needs to be reconstituted, the leading shape is available by calling
numpysane.broadcast_extra_dims() with the same arguments as this function.
'''
if len(args) != len(prototype):
raise NumpysaneError("Mismatched number of input arguments. Wanted {} but got {}". \
format(len(prototype), len(args)))
# make sure all the arguments are numpy arrays
args = tuple(np.asarray(arg) for arg in args)
# dims_extra: extra dimensions to broadcast through
# dims_named: values of the named dimensions
dims_extra,dims_named = \
_eval_broadcast_dims( args, prototype )
# I checked all the dimensions and aligned everything. I have my
# to-broadcast dimension counts. Iterate through all the broadcasting
# output, and gather the results
for x in _broadcast_iter_dim( args, prototype, dims_extra ):
yield x
def broadcast_extra_dims(prototype, args):
r'''Report the extra leading dimensions a broadcasted call would produce
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(6). reshape( 2,3)
>>> b = np.arange(15).reshape(5,1,3)
>>> print(nps.broadcast_extra_dims((('n',), ('n',)),
(a,b)))
[5,2]
The broadcasting operation of numpysane is described in detail in the
numpysane.broadcast_define() docstring and in the main README of numpysane.
This function applies the broadcasting rules to report the leading
dimensions of a broadcasted result if a broadcasted function was called with
the given arguments. This is most useful to reconstitute the desired shape
from flattened output produced by numpysane.broadcast_generate()
'''
if len(args) != len(prototype):
raise NumpysaneError("Mismatched number of input arguments. Wanted {} but got {}". \
format(len(prototype), len(args)))
# make sure all the arguments are numpy arrays
args = tuple(np.asarray(arg) for arg in args)
# dims_extra: extra dimensions to broadcast through
# dims_named: values of the named dimensions
dims_extra,dims_named = \
_eval_broadcast_dims( args, prototype )
return dims_extra
def glue(*args, **kwargs):
r'''Concatenates a given list of arrays along the given 'axis' keyword argument.
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(6).reshape(2,3)
>>> b = a + 100
>>> row = a[0,:] + 1000
>>> a
array([[0, 1, 2],
[3, 4, 5]])
>>> b
array([[100, 101, 102],
[103, 104, 105]])
>>> row
array([1000, 1001, 1002])
>>> nps.glue(a,b, axis=-1)
array([[ 0, 1, 2, 100, 101, 102],
[ 3, 4, 5, 103, 104, 105]])
# empty arrays ignored when glueing. Useful for initializing an accumulation
>>> nps.glue(a,b, np.array(()), axis=-1)
array([[ 0, 1, 2, 100, 101, 102],
[ 3, 4, 5, 103, 104, 105]])
>>> nps.glue(a,b,row, axis=-2)
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 100, 101, 102],
[ 103, 104, 105],
[1000, 1001, 1002]])
>>> nps.glue(a,b, axis=-3)
array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[100, 101, 102],
[103, 104, 105]]])
The 'axis' must be given in a keyword argument.
In order to count dimensions from the inner-most outwards, this function accepts
only negative axis arguments. This is because numpy broadcasts from the last
dimension, and the last dimension is the inner-most in the (usual) internal
storage scheme. Allowing glue() to look at dimensions at the start would allow
it to unalign the broadcasting dimensions, which is never what you want.
To glue along the last dimension, pass axis=-1; to glue along the second-to-last
dimension, pass axis=-2, and so on.
Unlike in PDL, this function refuses to create duplicated data to make the
shapes fit. In my experience, this isn't what you want, and can create bugs.
For instance, PDL does this:
pdl> p sequence(3,2)
[
[0 1 2]
[3 4 5]
]
pdl> p sequence(3)
[0 1 2]
pdl> p PDL::glue( 0, sequence(3,2), sequence(3) )
[
[0 1 2 0 1 2] <--- Note the duplicated "0,1,2"
[3 4 5 0 1 2]
]
while numpysane.glue() does this:
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(6).reshape(2,3)
>>> b = a[0:1,:]
>>> a
array([[0, 1, 2],
[3, 4, 5]])
>>> b
array([[0, 1, 2]])
>>> nps.glue(a,b,axis=-1)
[exception]
Finally, this function adds as many length-1 dimensions at the front as
required. Note that this does not create new data, just new degenerate
dimensions. Example:
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(6).reshape(2,3)
>>> b = a + 100
>>> a
array([[0, 1, 2],
[3, 4, 5]])
>>> b
array([[100, 101, 102],
[103, 104, 105]])
>>> res = nps.glue(a,b, axis=-5)
>>> res
array([[[[[ 0, 1, 2],
[ 3, 4, 5]]]],
[[[[100, 101, 102],
[103, 104, 105]]]]])
>>> res.shape
(2, 1, 1, 2, 3)
In numpysane older than 0.10 the semantics were slightly different: the axis
kwarg was optional, and glue(*args) would glue along a new leading
dimension, and thus would be equivalent to cat(*args). This resulted in very
confusing error messages if the user accidentally omitted the kwarg. To
request the legacy behavior, do
nps.glue.legacy_version = '0.9'
'''
legacy = \
hasattr(glue, 'legacy_version') and \
StrictVersion(glue.legacy_version) <= StrictVersion('0.9')
axis = kwargs.get('axis')
if legacy:
if axis is not None and axis >= 0:
raise NumpysaneError("axis >= 0 can make broadcasting dimensions inconsistent, and is thus not allowed")
else:
if axis is None:
raise NumpysaneError("glue() requires the axis to be given in the 'axis' kwarg")
if axis >= 0:
raise NumpysaneError("axis >= 0 can make broadcasting dimensions inconsistent, and is thus not allowed")
# deal with scalar (non-ndarray) args
args = [ np.asarray(x) for x in args ]
# Special case to support this common idiom:
#
# accum = np.array(())
# while ...:
# x = ...
# accum = nps.glue(accum, x, axis = -2)
#
# Without special logic, this would throw an error since accum.shape starts
# at (0,), which is almost certainly not compatible with x.shape. I support
# both glue(empty,x) and glue(x,empty)
if len(args) == 2:
if args[0].shape == (0,) and args[1].size != 0:
return atleast_dims(args[1], axis)
if args[1].shape == (0,) and args[0].size != 0:
return atleast_dims(args[0], axis)
# Legacy behavior: if no axis is given, add a new axis at the front, and
# glue along it
max_ndim = max( x.ndim for x in args )
if axis is None:
axis = -1 - max_ndim
# if we're glueing along a dimension beyond what we already have, expand the
# target dimension count
if max_ndim < -axis:
max_ndim = -axis
# Now I add dummy dimensions at the front of each array, to bring the source
# arrays to the same dimensionality. After this is done, ndims for all the
# matrices will be the same, and np.concatenate() should know what to do.
args = [ x[(np.newaxis,)*(max_ndim - x.ndim) + (Ellipsis,)] for x in args ]
return atleast_dims(np.concatenate( args, axis=axis ),
axis)
def cat(*args):
r'''Concatenates a given list of arrays along a new first (outer) dimension.
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(6).reshape(2,3)
>>> b = a + 100
>>> c = a - 100
>>> a
array([[0, 1, 2],
[3, 4, 5]])
>>> b
array([[100, 101, 102],
[103, 104, 105]])
>>> c
array([[-100, -99, -98],
[ -97, -96, -95]])
>>> res = nps.cat(a,b,c)
>>> res
array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 100, 101, 102],
[ 103, 104, 105]],
[[-100, -99, -98],
[ -97, -96, -95]]])
>>> res.shape
(3, 2, 3)
>>> [x for x in res]
[array([[0, 1, 2],
[3, 4, 5]]),
array([[100, 101, 102],
[103, 104, 105]]),
array([[-100, -99, -98],
[ -97, -96, -95]])]
### Note that this is the same as [a,b,c]: cat is the reverse of
### iterating on an array
This function concatenates the input arrays into an array shaped like the
highest-dimensioned input, but with a new outer (at the start) dimension.
The concatenation axis is this new dimension.
As usual, the dimensions are aligned along the last one, so broadcasting
will continue to work as expected. Note that this is the opposite operation
from iterating a numpy array: iteration splits an array over its leading
dimension, while cat joins a number of arrays via a new leading dimension.
'''
if len(args) == 0:
return np.array(())
max_ndim = max( x.ndim for x in args )
return glue(*args, axis = -1 - max_ndim)
def clump(x, **kwargs):
r'''Groups the given n dimensions together.
SYNOPSIS
>>> import numpysane as nps
>>> nps.clump( arr(2,3,4), n = -2).shape
(2, 12)
Reshapes the array by grouping together 'n' dimensions, where 'n' is given
in a kwarg. If 'n' > 0, then n leading dimensions are clumped; if 'n' < 0,
then -n trailing dimensions are clumped
So for instance, if x.shape is (2,3,4) then nps.clump(x, n = -2).shape is
(2,12) and nps.clump(x, n = 2).shape is (6, 4)
In numpysane older than 0.10 the semantics were different: n > 0 was
required, and we ALWAYS clumped the trailing dimensions. Thus the new
clump(-n) is equivalent to the old clump(n). To request the legacy behavior,
do
nps.clump.legacy_version = '0.9'
'''
legacy = \
hasattr(clump, 'legacy_version') and \
StrictVersion(clump.legacy_version) <= StrictVersion('0.9')
n = kwargs.get('n')
if n is None:
raise NumpysaneError("clump() requires a dimension count in the 'n' kwarg")
if legacy:
# old PDL-like clump(). Takes positive dimension counts, and acts from
# the most-significant dimension (from the back)
if n < 0:
raise NumpysaneError("clump() requires n > 0")
if n <= 1:
return x
if x.ndim < n:
n = x.ndim
s = list(x.shape[:-n]) + [ _product(x.shape[-n:]) ]
return x.reshape(s)
if -1 <= n and n <= 1:
return x
if x.ndim < n:
n = x.ndim
if -x.ndim > n:
n = -x.ndim
if n < 0:
s = list(x.shape[:n]) + [ _product(x.shape[n:]) ]
else:
s = [ _product(x.shape[:n]) ] + list(x.shape[n:])
return x.reshape(s)
def atleast_dims(x, *dims):
r'''Returns an array with extra length-1 dimensions to contain all given axes.
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(6).reshape(2,3)
>>> a
array([[0, 1, 2],
[3, 4, 5]])
>>> nps.atleast_dims(a, -1).shape
(2, 3)
>>> nps.atleast_dims(a, -2).shape
(2, 3)
>>> nps.atleast_dims(a, -3).shape
(1, 2, 3)
>>> nps.atleast_dims(a, 0).shape
(2, 3)
>>> nps.atleast_dims(a, 1).shape
(2, 3)
>>> nps.atleast_dims(a, 2).shape
[exception]
>>> l = [-3,-2,-1,0,1]
>>> nps.atleast_dims(a, l).shape
(1, 2, 3)
>>> l
[-3, -2, -1, 1, 2]
If the given axes already exist in the given array, the given array itself
is returned. Otherwise length-1 dimensions are added to the front until all
the requested dimensions exist. The given axis>=0 dimensions MUST all be
in-bounds from the start, otherwise the most-significant axis becomes
unaligned; an exception is thrown if this is violated. The given axis<0
dimensions that are out-of-bounds result in new dimensions added at the
front.
If new dimensions need to be added at the front, then any axis>=0 indices
become offset. For instance:
>>> x.shape
(2, 3, 4)
>>> [x.shape[i] for i in (0,-1)]
[2, 4]
>>> x = nps.atleast_dims(x, 0, -1, -5)
>>> x.shape
(1, 1, 2, 3, 4)
>>> [x.shape[i] for i in (0,-1)]
[1, 4]
Before the call, axis=0 refers to the length-2 dimension and axis=-1 refers
to the length=4 dimension. After the call, axis=-1 refers to the same
dimension as before, but axis=0 now refers to a new length=1 dimension. If
it is desired to compensate for this offset, then instead of passing the
axes as separate arguments, pass in a single list of the axes indices. This
list will be modified to offset the axis>=0 appropriately. Ideally, you only
pass in axes<0, and this does not apply. Doing this in the above example:
>>> l
[0, -1, -5]
>>> x.shape
(2, 3, 4)
>>> [x.shape[i] for i in (l[0],l[1])]
[2, 4]
>>> x=nps.atleast_dims(x, l)
>>> x.shape
(1, 1, 2, 3, 4)
>>> l
[2, -1, -5]
>>> [x.shape[i] for i in (l[0],l[1])]
[2, 4]
We passed the axis indices in a list, and this list was modified to reflect
the new indices: The original axis=0 becomes known as axis=2. Again, if you
pass in only axis<0, then you don't need to care about this.
'''
if any( not isinstance(d, int) for d in dims ):
if len(dims) == 1 and isinstance(dims[0], list):
dims = dims[0]
else:
raise NumpysaneError("atleast_dims() takes in axes as integers in separate arguments or\n"
"as a single modifiable list")
if max(dims) >= x.ndim:
raise NumpysaneError("Axis {} out of bounds because x.ndim={}.\n"
"To keep the last dimension anchored, "
"only <0 out-of-bounds axes are allowed".format(max(dims), x.ndim))
need_ndim = -min(d if d<0 else -1 for d in dims)
if x.ndim >= need_ndim:
return x
num_new_axes = need_ndim-x.ndim
# apply an offset to any axes that need it
if isinstance(dims, list):
dims[:] = [ d+num_new_axes if d >= 0 else d for d in dims ]
return x[ (np.newaxis,)*(num_new_axes) ]
def mv(x, axis_from, axis_to):
r'''Moves a given axis to a new position. Similar to numpy.moveaxis().
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(24).reshape(2,3,4)
>>> a.shape
(2, 3, 4)
>>> nps.mv( a, -1, 0).shape
(4, 2, 3)
>>> nps.mv( a, -1, -5).shape
(4, 1, 1, 2, 3)
>>> nps.mv( a, 0, -5).shape
(2, 1, 1, 3, 4)
New length-1 dimensions are added at the front, as required, and any axis>=0
that are passed in refer to the array BEFORE these new dimensions are added.
'''
axes = [axis_from, axis_to]
x = atleast_dims(x, axes)
# The below is equivalent to
# return np.moveaxis( x, *axes )
# but some older installs have numpy 1.8, where this isn't available
axis_from = axes[0] if axes[0] >= 0 else x.ndim + axes[0]
axis_to = axes[1] if axes[1] >= 0 else x.ndim + axes[1]
# python3 needs the list() cast
order = list(range(0, axis_from)) + list(range((axis_from+1), x.ndim))
order.insert(axis_to, axis_from)
return np.transpose(x, order)
def xchg(x, axis_a, axis_b):
r'''Exchanges the positions of the two given axes. Similar to numpy.swapaxes()
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(24).reshape(2,3,4)
>>> a.shape
(2, 3, 4)
>>> nps.xchg( a, -1, 0).shape
(4, 3, 2)
>>> nps.xchg( a, -1, -5).shape
(4, 1, 2, 3, 1)
>>> nps.xchg( a, 0, -5).shape
(2, 1, 1, 3, 4)
New length-1 dimensions are added at the front, as required, and any axis>=0
that are passed in refer to the array BEFORE these new dimensions are added.
'''
axes = [axis_a, axis_b]
x = atleast_dims(x, axes)
return np.swapaxes( x, *axes )
def transpose(x):
r'''Reverses the order of the last two dimensions.
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(24).reshape(2,3,4)
>>> a.shape
(2, 3, 4)
>>> nps.transpose(a).shape
(2, 4, 3)
>>> nps.transpose( np.arange(3) ).shape
(3, 1)
A "matrix" is generally seen as a 2D array that we can transpose by looking
at the 2 dimensions in the opposite order. Here we treat an n-dimensional
array as an n-2 dimensional object containing 2D matrices. As usual, the
last two dimensions contain the matrix.
New length-1 dimensions are added at the front, as required, meaning that 1D
input of shape (n,) is interpreted as a 2D input of shape (1,n), and the
transpose is 2 of shape (n,1).
'''
return xchg( atleast_dims(x, -2), -1, -2)
def dummy(x, axis, *axes_rest):
r'''Adds length-1 dimensions at the given positions.
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(24).reshape(2,3,4)
>>> a.shape
(2, 3, 4)
>>> nps.dummy(a, 0).shape
(1, 2, 3, 4)
>>> nps.dummy(a, 1).shape
(2, 1, 3, 4)
>>> nps.dummy(a, -1).shape
(2, 3, 4, 1)
>>> nps.dummy(a, -2).shape
(2, 3, 1, 4)
>>> nps.dummy(a, -2, -2).shape
(2, 3, 1, 1, 4)
>>> nps.dummy(a, -5).shape
(1, 1, 2, 3, 4)
This is similar to numpy.expand_dims(), but handles out-of-bounds dimensions
better. New length-1 dimensions are added at the front, as required, and any
axis>=0 that are passed in refer to the array BEFORE these new dimensions
are added.
'''
def dummy_inner(x, axis):
need_ndim = axis+1 if axis >= 0 else -axis
if x.ndim >= need_ndim:
# referring to an axis that already exists. expand_dims() thus works
return np.expand_dims(x, axis)
# referring to a non-existing axis. I simply add sufficient new axes, and
# I'm done
return atleast_dims(x, axis)
axes = (axis,) + axes_rest
for axis in axes: x = dummy_inner(x, axis)
return x
def reorder(x, *dims):
r'''Reorders the dimensions of an array.
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(24).reshape(2,3,4)
>>> a.shape
(2, 3, 4)
>>> nps.reorder( a, 0, -1, 1 ).shape
(2, 4, 3)
>>> nps.reorder( a, -2 , -1, 0 ).shape
(3, 4, 2)
>>> nps.reorder( a, -4 , -2, -5, -1, 0 ).shape
(1, 3, 1, 4, 2)
This is very similar to numpy.transpose(), but handles out-of-bounds
dimensions much better.
New length-1 dimensions are added at the front, as required, and any axis>=0
that are passed in refer to the array BEFORE these new dimensions are added.
'''
dims = list(dims)
x = atleast_dims(x, dims)
return np.transpose(x, dims)
# Note that this explicitly isn't done with @broadcast_define. Instead I
# implement the internals with core numpy routines. The advantage is that these
# are some of very few numpy functions that support broadcasting, and they do so
# in C, so their broadcasting loop is FAST. Much more so than my current
# @broadcast_define loop
def dot(a, b, out=None, dtype=None):
r'''Non-conjugating dot product of two 1-dimensional n-long vectors.
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(3)
>>> b = a+5
>>> a
array([0, 1, 2])
>>> b
array([5, 6, 7])
>>> nps.dot(a,b)
20
This is identical to numpysane.inner(). for a conjugating version of this
function, use nps.vdot(). Note that the stock numpy dot() has some special
handling when its dot() is given more than 1-dimensional input. THIS
function has no special handling: normal broadcasting rules are applied, as
expected.
In-place operation is available with the "out" kwarg. The output dtype can
be selected with the "dtype" kwarg. If omitted, the dtype of the input is
used
'''
if out is not None and dtype is not None and out.dtype != dtype:
raise NumpysaneError("'out' and 'dtype' given explicitly, but the dtypes are mismatched!")
if dtype is not None:
# Handle overflows. Cases that require this are checked in the tests
v = np.sum(a.astype(dtype)*b.astype(dtype), axis=-1, out=out, dtype=dtype )
else:
v = np.sum(a*b, axis=-1, out=out, dtype=dtype )
if out is None:
return v
return out
# nps.inner and nps.dot are equivalent. Set the functionality and update the
# docstring
inner = _clone_function( dot, "inner" )
doc = dot.__doc__
doc = doc.replace("vdot", "aaa")
doc = doc.replace("dot", "bbb")
doc = doc.replace("inner", "ccc")
doc = doc.replace("ccc", "dot")
doc = doc.replace("bbb", "inner")
doc = doc.replace("aaa", "vdot")
inner.__doc__ = doc
# Note that this explicitly isn't done with @broadcast_define. Instead I
# implement the internals with core numpy routines. The advantage is that these
# are some of very few numpy functions that support broadcasting, and they do so
# on the C level, so their broadcasting loop is FAST. Much more so than my
# current @broadcast_define loop
def vdot(a, b, out=None, dtype=None):
r'''Conjugating dot product of two 1-dimensional n-long vectors.
vdot(a,b) is equivalent to dot(np.conj(a), b)
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.array(( 1 + 2j, 3 + 4j, 5 + 6j))
>>> b = a+5
>>> a
array([ 1.+2.j, 3.+4.j, 5.+6.j])
>>> b
array([ 6.+2.j, 8.+4.j, 10.+6.j])
>>> nps.vdot(a,b)
array((136-60j))
>>> nps.dot(a,b)
array((24+148j))
For a non-conjugating version of this function, use nps.dot(). Note that the
numpy vdot() has some special handling when its vdot() is given more than
1-dimensional input. THIS function has no special handling: normal
broadcasting rules are applied.
'''
return dot(np.conj(a), b, out=out, dtype=dtype)
@broadcast_define( (('n',), ('m',)), prototype_output=('n','m'), out_kwarg='out' )
def outer(a, b, out=None):
r'''Outer product of two 1-dimensional n-long vectors.
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(3)
>>> b = a+5
>>> a
array([0, 1, 2])
>>> b
array([5, 6, 7])
>>> nps.outer(a,b)
array([[ 0, 0, 0],
[ 5, 6, 7],
[10, 12, 14]])
'''
if out is None:
return np.outer(a,b)
out.setfield(np.outer(a,b), out.dtype)
return out
# Note that this explicitly isn't done with @broadcast_define. Instead I
# implement the internals with core numpy routines. The advantage is that these
# are some of very few numpy functions that support broadcasting, and they do so
# in C, so their broadcasting loop is FAST. Much more so than my current
# @broadcast_define loop
def norm2(a, **kwargs):
r'''Broadcast-aware 2-norm. norm2(x) is identical to inner(x,x)
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(3)
>>> a
array([0, 1, 2])
>>> nps.norm2(a)
5
This is a convenience function to compute a 2-norm
'''
return inner(a,a, **kwargs)
def mag(a, out=None, dtype=None):
r'''Magnitude of a vector. mag(x) is functionally identical to sqrt(inner(x,x))
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(3)
>>> a
array([0, 1, 2])
>>> nps.mag(a)
2.23606797749979
This is a convenience function to compute a magnitude of a vector, with full
broadcasting support.
In-place operation is available with the "out" kwarg. The output dtype can
be selected with the "dtype" kwarg. If omitted, dtype=float is selected.
'''
if out is None:
if dtype is None:
dtype = float
out = inner(a,a, dtype=dtype)
if not isinstance(out, np.ndarray):
# given two vectors, and without and 'out' array, inner() produces a
# scalar, not an array. So I can't updated it inplace, and just
# return a copy
return np.sqrt(out)
else:
if dtype is None:
dtype = out.dtype
inner(a,a, out=out, dtype=dtype)
# in-place sqrt
np.sqrt.at(out,())
return out
@broadcast_define( (('n','n',),), prototype_output=() )
def trace(a):
r'''Broadcast-aware trace
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(3*4*4).reshape(3,4,4)
>>> a
array([[[ 0, 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]]])
>>> nps.trace(a)
array([ 30, 94, 158])
'''
return np.trace(a)
# Could be implemented with a simple loop around np.dot():
#
# @broadcast_define( (('n', 'm'), ('m', 'l')), prototype_output=('n','l'), out_kwarg='out' )
# def matmult2(a, b, out=None):
# return np.dot(a,b)
#
# but this would produce a python broadcasting loop, which is potentially slow.
# Instead I'm using the np.matmul() primitive to get C broadcasting loops. This
# function has stupid special-case rules for low-dimensional arrays, so I make
# sure to do the normal broadcasting thing in those cases
def matmult2(a, b, out=None):
r'''Multiplication of two matrices
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(6) .reshape(2,3)
>>> b = np.arange(12).reshape(3,4)
>>> a
array([[0, 1, 2],
[3, 4, 5]])
>>> b
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> nps.matmult2(a,b)
array([[20, 23, 26, 29],
[56, 68, 80, 92]])
This function is exposed publically mostly for legacy compatibility. Use
numpysane.matmult() instead
'''
if not isinstance(a, np.ndarray) and not isinstance(b, np.ndarray):
# two non-arrays (assuming two scalars)
if out is not None:
o = a*b
out.setfield(o, out.dtype)
out.resize([])
return out
return a*b
if not isinstance(a, np.ndarray) or len(a.shape) == 0:
# one non-array (assuming one scalar)
if out is not None:
out.setfield(a*b, out.dtype)
out.resize(b.shape)
return out
return a*b
if not isinstance(b, np.ndarray) or len(b.shape) == 0:
# one non-array (assuming one scalar)
if out is not None:
out.setfield(a*b, out.dtype)
out.resize(a.shape)
return out
return a*b
if len(b.shape) == 1:
b = b[np.newaxis, :]
o = np.matmul(a,b, out)
return o
def matmult( a, *rest, **kwargs ):
r'''Multiplication of N matrices
SYNOPSIS
>>> import numpy as np
>>> import numpysane as nps
>>> a = np.arange(6) .reshape(2,3)
>>> b = np.arange(12).reshape(3,4)
>>> c = np.arange(4) .reshape(4,1)
>>> a
array([[0, 1, 2],
[3, 4, 5]])
>>> b
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> c
array([[0],
[1],
[2],
[3]])
>>> nps.matmult(a,b,c)
array([[162],
[504]])
>>> abc = np.zeros((2,1), dtype=float)
>>> nps.matmult(a,b,c, out=abc)
>>> abc
array([[162],
[504]])
This multiplies N matrices together by repeatedly calling matmult2() for
each adjacent pair. In-place output is supported with the 'out' keyword
argument
This function supports broadcasting fully, in C internally
'''
if len(rest) == 0:
raise Exception("Need at least two terms to multiply")
out = None
if len(kwargs.keys()) > 1:
raise Exception("Only ONE kwarg is supported: 'out'")
if kwargs:
# have exactly one kwarg
if 'out' not in kwargs:
raise Exception("The only supported kwarg is 'out'")
out = kwargs['out']
return matmult2(a,reduce(matmult2, rest), out=out)
# I use np.matmul at one point. This was added in numpy 1.10.0, but
# apparently I want to support even older releases. I thus provide a
# compatibility function in that case. This is slower (python loop instead of C
# loop), but at least it works
if not hasattr(np, 'matmul'):
@broadcast_define( (('n','m'),('m','o')), ('n','o'))
def matmul(a,b, out=None):
return np.dot(a,b,out)
np.matmul = matmul
|