nabla_ml/nab_array.rs
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
use rand::Rng;
use rand_distr::{StandardNormal, Uniform, Distribution};
use std::ops::{Add, Sub, Mul, Div};
#[derive(Debug, Clone)]
pub struct NDArray {
pub data: Vec<f64>,
pub shape: Vec<usize>,
}
impl NDArray {
pub fn new(data: Vec<f64>, shape: Vec<usize>) -> Self {
let total_size: usize = shape.iter().product();
assert_eq!(data.len(), total_size, "Data length must match shape dimensions");
NDArray { data, shape }
}
pub fn from_vec(data: Vec<f64>) -> Self {
let len = data.len();
Self::new(data, vec![len])
}
#[allow(dead_code)]
pub fn from_matrix(data: Vec<Vec<f64>>) -> Self {
let rows = data.len();
let cols = data.get(0).map_or(0, |row| row.len());
let flat_data: Vec<f64> = data.into_iter().flatten().collect();
Self::new(flat_data, vec![rows, cols])
}
pub fn shape(&self) -> &[usize] {
&self.shape
}
pub fn ndim(&self) -> usize {
self.shape.len()
}
/// Returns a reference to the data of the array
pub fn data(&self) -> &[f64] {
&self.data
}
/// Creates a 2D array (matrix) of random numbers between 0 and 1
///
/// # Arguments
///
/// * `rows` - The number of rows in the matrix.
/// * `cols` - The number of columns in the matrix.
///
/// # Returns
///
/// A 2D NDArray filled with random numbers.
#[allow(dead_code)]
pub fn rand_2d(rows: usize, cols: usize) -> Self {
let mut rng = rand::thread_rng();
let data: Vec<f64> = (0..rows * cols).map(|_| rng.gen()).collect();
Self::new(data, vec![rows, cols])
}
/// Creates a 1D array of random numbers following a normal distribution
///
/// # Arguments
///
/// * `size` - The number of elements in the array.
///
/// # Returns
///
/// A 1D NDArray filled with random numbers from a normal distribution.
#[allow(dead_code)]
pub fn randn(size: usize) -> Self {
let mut rng = rand::thread_rng();
let data: Vec<f64> = (0..size).map(|_| rng.sample(StandardNormal)).collect();
Self::from_vec(data)
}
/// Creates a 2D array (matrix) of random numbers following a normal distribution
///
/// # Arguments
///
/// * `rows` - The number of rows in the matrix.
/// * `cols` - The number of columns in the matrix.
///
/// # Returns
///
/// A 2D NDArray filled with random numbers from a normal distribution.
#[allow(dead_code)]
pub fn randn_2d(rows: usize, cols: usize) -> Self {
let mut rng = rand::thread_rng();
let data: Vec<f64> = (0..rows * cols).map(|_| rng.sample(StandardNormal)).collect();
Self::new(data, vec![rows, cols])
}
/// Creates a 1D array of random integers between `low` and `high`
///
/// # Arguments
///
/// * `low` - The lower bound (inclusive).
/// * `high` - The upper bound (exclusive).
/// * `size` - The number of elements in the array.
///
/// # Returns
///
/// A 1D NDArray filled with random integers.
#[allow(dead_code)]
pub fn randint(low: i32, high: i32, size: usize) -> Self {
let mut rng = rand::thread_rng();
let data: Vec<f64> = (0..size).map(|_| rng.gen_range(low..high) as f64).collect();
Self::from_vec(data)
}
/// Creates a 2D array (matrix) of random integers between `low` and `high`
///
/// # Arguments
///
/// * `low` - The lower bound (inclusive).
/// * `high` - The upper bound (exclusive).
/// * `rows` - The number of rows in the matrix.
/// * `cols` - The number of columns in the matrix.
///
/// # Returns
///
/// A 2D NDArray filled with random integers.
#[allow(dead_code)]
pub fn randint_2d(low: i32, high: i32, rows: usize, cols: usize) -> Self {
let mut rng = rand::thread_rng();
let data: Vec<f64> = (0..rows * cols).map(|_| rng.gen_range(low..high) as f64).collect();
Self::new(data, vec![rows, cols])
}
/// Reshapes the array to the specified shape, allowing one dimension to be inferred
///
/// # Arguments
///
/// * `new_shape` - A vector representing the new shape, with at most one dimension as `-1`.
///
/// # Returns
///
/// A new NDArray with the specified shape.
pub fn reshape(&self, new_shape: &[usize]) -> Result<Self, &'static str> {
let total_elements = self.data.len();
let new_total = new_shape.iter().product();
if total_elements != new_total {
return Err("New shape must have same total size as original");
}
Ok(NDArray {
data: self.data.clone(),
shape: new_shape.to_vec()
})
}
/// Returns the maximum value in the array
///
/// # Returns
///
/// The maximum value as an f64.
#[allow(dead_code)]
pub fn max(&self) -> f64 {
*self.data.iter().max_by(|a, b| a.partial_cmp(b).unwrap()).unwrap()
}
/// Returns the index of the maximum value in the array
///
/// # Returns
///
/// The index of the maximum value.
// #[allow(dead_code)]
// pub fn argmax(&self) -> usize {
// self.data.iter().enumerate().max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap()).map(|(i, _)| i).unwrap()
// }
/// Returns the indices of maximum values
/// For 1D arrays: returns a single index
/// For 2D arrays: returns indices along the specified axis
pub fn argmax(&self, axis: Option<usize>) -> Vec<usize> {
match axis {
None => {
// Global argmax
vec![self.data.iter()
.enumerate()
.max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap())
.map(|(i, _)| i)
.unwrap()]
},
Some(ax) => {
if ax >= self.shape.len() {
panic!("Axis {} out of bounds for shape {:?}", ax, self.shape);
}
// Axis-wise argmax
match ax {
0 => {
let cols = self.shape[1];
let mut indices = Vec::with_capacity(cols);
for j in 0..cols {
let mut max_idx = 0;
let mut max_val = self.data[j];
for i in 1..self.shape[0] {
let val = self.data[i * cols + j];
if val > max_val {
max_val = val;
max_idx = i;
}
}
indices.push(max_idx);
}
indices
},
1 => {
let cols = self.shape[1];
let mut indices = Vec::with_capacity(self.shape[0]);
for i in 0..self.shape[0] {
let row_start = i * cols;
let mut max_idx = 0;
let mut max_val = self.data[row_start];
for j in 1..cols {
let val = self.data[row_start + j];
if val > max_val {
max_val = val;
max_idx = j;
}
}
indices.push(max_idx);
}
indices
},
_ => panic!("Unsupported axis {}", ax)
}
}
}
}
/// Returns the minimum value in the array
///
/// # Returns
///
/// The minimum value as an f64.
#[allow(dead_code)]
pub fn min(&self) -> f64 {
*self.data.iter().min_by(|a, b| a.partial_cmp(b).unwrap()).unwrap()
}
/// Creates an NDArray from a flat vector and a specified shape
///
/// # Arguments
///
/// * `data` - A vector of f64 values representing the array's data.
/// * `shape` - A vector of usize values representing the dimensions of the array.
///
/// # Returns
///
/// A new NDArray instance.
#[allow(dead_code)]
pub fn from_vec_reshape(data: Vec<f64>, shape: Vec<usize>) -> Self {
let total_size: usize = shape.iter().product();
assert_eq!(data.len(), total_size, "Data length must match shape dimensions");
NDArray { data, shape }
}
/// Extracts a single sample from a batch of N-dimensional arrays
///
/// # Arguments
///
/// * `sample_index` - The index of the sample to extract
///
/// # Returns
///
/// A new NDArray containing just the specified sample with N-1 dimensions
#[allow(dead_code)]
pub fn extract_sample(&self, sample_index: usize) -> Self {
assert!(self.ndim() >= 2, "Array must have at least 2 dimensions");
assert!(sample_index < self.shape[0], "Sample index out of bounds");
let sample_size: usize = self.shape.iter().skip(1).product();
let start_index = sample_index * sample_size;
let end_index = start_index + sample_size;
// Create new shape without the first dimension
let new_shape: Vec<usize> = self.shape.iter().skip(1).cloned().collect();
NDArray::new(
self.data[start_index..end_index].to_vec(),
new_shape
)
}
/// Pretty prints an N-dimensional array
///
/// # Arguments
///
/// * `precision` - The number of decimal places to round each value to.
#[allow(dead_code)]
pub fn pretty_print(&self, precision: usize) {
let indent_str = " ".repeat(precision);
let format_value = |x: f64| -> String {
if x == 0.0 {
format!("{:.1}", x)
} else {
format!("{:.*}", precision, x)
}
};
match self.ndim() {
1 => println!("{}[{}]", indent_str, self.data.iter()
.map(|&x| format_value(x))
.collect::<Vec<_>>()
.join(" ")),
2 => {
println!("{}[", indent_str);
for i in 0..self.shape[0] {
print!("{} [", indent_str);
for j in 0..self.shape[1] {
print!("{}", format_value(self.get_2d(i, j)));
if j < self.shape[1] - 1 {
print!(" ");
}
}
println!("]");
}
println!("{}]", indent_str);
},
_ => {
println!("{}[", indent_str);
for i in 0..self.shape[0] {
let slice = self.extract_sample(i);
slice.pretty_print(precision + 2);
}
println!("{}]", indent_str);
}
}
}
/// Returns a specific element from the array
///
/// # Arguments
///
/// * `index` - The index of the element to retrieve.
///
/// # Returns
///
/// The element at the specified index.
#[allow(dead_code)]
pub fn get(&self, index: usize) -> f64 {
self.data[index]
}
/// Creates a 1D array with a range of numbers
///
/// # Arguments
///
/// * `start` - The starting value of the range (inclusive).
/// * `stop` - The stopping value of the range (exclusive).
/// * `step` - The step size between each value in the range.
///
/// # Returns
///
/// A 1D NDArray containing the range of numbers.
#[allow(dead_code)]
pub fn arange(start: f64, stop: f64, step: f64) -> Self {
let mut data = Vec::new();
let mut current = start;
while current < stop {
data.push(current);
current += step;
}
Self::from_vec(data)
}
/// Creates a 1D array filled with zeros
///
/// # Arguments
///
/// * `size` - The number of elements in the array.
///
/// # Returns
///
/// A 1D NDArray filled with zeros.
#[allow(dead_code)]
pub fn zeros(shape: Vec<usize>) -> Self {
let total_size: usize = shape.iter().product();
NDArray {
data: vec![0.0; total_size],
shape,
}
}
/// Creates a 2D array (matrix) filled with zeros
///
/// # Arguments
///
/// * `rows` - The number of rows in the matrix.
/// * `cols` - The number of columns in the matrix.
///
/// # Returns
///
/// A 2D NDArray filled with zeros.
#[allow(dead_code)]
pub fn zeros_2d(rows: usize, cols: usize) -> Self {
Self::new(vec![0.0; rows * cols], vec![rows, cols])
}
/// Creates a 1D array filled with ones
///
/// # Arguments
///
/// * `size` - The number of elements in the array.
///
/// # Returns
///
/// A 1D NDArray filled with ones.
#[allow(dead_code)]
pub fn ones(size: usize) -> Self {
Self::from_vec(vec![1.0; size])
}
/// Creates a 2D array (matrix) filled with ones
///
/// # Arguments
///
/// * `rows` - The number of rows in the matrix.
/// * `cols` - The number of columns in the matrix.
///
/// # Returns
///
/// A 2D NDArray filled with ones.
#[allow(dead_code)]
pub fn ones_2d(rows: usize, cols: usize) -> Self {
Self::new(vec![1.0; rows * cols], vec![rows, cols])
}
/// Creates a 1D array with evenly spaced numbers over a specified interval
///
/// # Arguments
///
/// * `start` - The starting value of the interval.
/// * `end` - The ending value of the interval.
/// * `num` - The number of evenly spaced samples to generate.
/// * `precision` - The number of decimal places to round each value to.
///
/// # Returns
///
/// A 1D NDArray containing the evenly spaced numbers.
#[allow(dead_code)]
pub fn linspace(start: f64, end: f64, num: usize, precision: usize) -> Self {
assert!(num > 1, "Number of samples must be greater than 1");
let step = (end - start) / (num - 1) as f64;
let mut data = Vec::with_capacity(num);
let factor = 10f64.powi(precision as i32);
for i in 0..num {
let value = start + step * i as f64;
let rounded_value = (value * factor).round() / factor;
data.push(rounded_value);
}
Self::from_vec(data)
}
/// Creates an identity matrix of size `n x n`
///
/// # Arguments
///
/// * `n` - The size of the identity matrix.
///
/// # Returns
///
/// An `n x n` identity matrix as an NDArray.
#[allow(dead_code)]
pub fn eye(n: usize) -> Self {
let mut data = vec![0.0; n * n];
for i in 0..n {
data[i * n + i] = 1.0;
}
Self::new(data, vec![n, n])
}
/// Creates a 1D array of random numbers between 0 and 1
///
/// # Arguments
///
/// * `size` - The number of elements in the array.
///
/// # Returns
///
/// A 1D NDArray filled with random numbers.
#[allow(dead_code)]
pub fn rand(size: usize) -> Self {
let mut rng = rand::thread_rng();
let data: Vec<f64> = (0..size).map(|_| rng.gen()).collect();
Self::from_vec(data)
}
/// Returns a sub-matrix from a 2D array
///
/// # Arguments
///
/// * `row_start` - The starting row index of the sub-matrix.
/// * `row_end` - The ending row index of the sub-matrix (exclusive).
/// * `col_start` - The starting column index of the sub-matrix.
/// * `col_end` - The ending column index of the sub-matrix (exclusive).
///
/// # Returns
///
/// A new NDArray representing the specified sub-matrix.
#[allow(dead_code)]
pub fn sub_matrix(&self, row_start: usize, row_end: usize, col_start: usize, col_end: usize) -> Self {
assert_eq!(self.ndim(), 2, "sub_matrix is only applicable to 2D arrays");
let cols = self.shape[1];
let mut data = Vec::new();
for row in row_start..row_end {
for col in col_start..col_end {
data.push(self.data[row * cols + col]);
}
}
Self::new(data, vec![row_end - row_start, col_end - col_start])
}
/// Sets a specific element in the array
///
/// # Arguments
///
/// * `index` - The index of the element to set.
/// * `value` - The value to set the element to.
#[allow(dead_code)]
pub fn set(&mut self, index: usize, value: f64) {
self.data[index] = value;
}
/// Sets a range of elements in the array to a specific value
///
/// # Arguments
///
/// * `start` - The starting index of the range.
/// * `end` - The ending index of the range (exclusive).
/// * `value` - The value to set the elements to.
#[allow(dead_code)]
pub fn set_range(&mut self, start: usize, end: usize, value: f64) {
for i in start..end {
self.data[i] = value;
}
}
/// Returns a copy of the array
///
/// # Returns
///
/// A new NDArray that is a copy of the original.
#[allow(dead_code)]
pub fn copy(&self) -> Self {
Self::new(self.data.clone(), self.shape.clone())
}
/// Returns a view (slice) of the array from start to end (exclusive)
///
/// # Arguments
///
/// * `start` - The starting index of the view.
/// * `end` - The ending index of the view (exclusive).
///
/// # Returns
///
/// A slice of f64 values representing the specified view.
#[allow(dead_code)]
pub fn view(&self, start: usize, end: usize) -> &[f64] {
&self.data[start..end]
}
/// Returns a mutable view (slice) of the array from start to end (exclusive)
///
/// # Arguments
///
/// * `start` - The starting index of the view.
/// * `end` - The ending index of the view (exclusive).
///
/// # Returns
///
/// A mutable slice of f64 values representing the specified view.
#[allow(dead_code)]
pub fn view_mut(&mut self, start: usize, end: usize) -> &mut [f64] {
&mut self.data[start..end]
}
/// Returns a specific element from a 2D array
///
/// # Arguments
///
/// * `row` - The row index of the element.
/// * `col` - The column index of the element.
///
/// # Returns
///
/// The element at the specified row and column.
#[allow(dead_code)]
pub fn get_2d(&self, row: usize, col: usize) -> f64 {
assert_eq!(self.ndim(), 2, "get_2d is only applicable to 2D arrays");
let cols = self.shape[1];
self.data[row * cols + col]
}
/// Sets a specific element in a 2D array
///
/// # Arguments
///
/// * `row` - The row index of the element.
/// * `col` - The column index of the element.
/// * `value` - The value to set the element to.
#[allow(dead_code)]
pub fn set_2d(&mut self, row: usize, col: usize, value: f64) {
assert_eq!(self.ndim(), 2, "set_2d is only applicable to 2D arrays");
let cols = self.shape[1];
self.data[row * cols + col] = value;
}
/// Adds a new axis to the array at the specified position
///
/// # Arguments
///
/// * `axis` - The position at which to add the new axis.
///
/// # Returns
///
/// A new NDArray with an additional axis.
#[allow(dead_code)]
pub fn new_axis(&self, axis: usize) -> Self {
let mut new_shape = self.shape.clone();
new_shape.insert(axis, 1);
Self::new(self.data.clone(), new_shape)
}
/// Expands the dimensions of the array by adding a new axis at the specified index
///
/// # Arguments
///
/// * `axis` - The index at which to add the new axis.
///
/// # Returns
///
/// A new NDArray with expanded dimensions.
#[allow(dead_code)]
pub fn expand_dims(&self, axis: usize) -> Self {
self.new_axis(axis)
}
/// Returns a boolean array indicating whether each element satisfies the condition
///
/// # Arguments
///
/// * `threshold` - The threshold value to compare each element against.
///
/// # Returns
///
/// A vector of boolean values indicating whether each element is greater than the threshold.
#[allow(dead_code)]
pub fn greater_than(&self, threshold: f64) -> Vec<bool> {
self.data.iter().map(|&x| x > threshold).collect()
}
/// Returns a new array containing only the elements that satisfy the condition
///
/// # Arguments
///
/// * `condition` - A closure that takes an f64 and returns a boolean.
///
/// # Returns
///
/// A new NDArray containing only the elements that satisfy the condition.
#[allow(dead_code)]
pub fn filter(&self, condition: impl Fn(&f64) -> bool) -> Self {
let data: Vec<f64> = self.data.iter().cloned().filter(condition).collect();
Self::from_vec(data)
}
/// Returns the data type of the elements in the array
///
/// # Returns
///
/// A string representing the data type of the elements.
#[allow(dead_code)]
pub fn dtype(&self) -> &'static str {
"f64" // Since we're using f64 for all elements
}
/// Returns the total number of elements in the array
///
/// # Returns
///
/// The total number of elements in the array.
#[allow(dead_code)]
pub fn size(&self) -> usize {
self.data.len()
}
/// Returns the index of the minimum value in the array
///
/// # Returns
///
/// The index of the minimum value.
#[allow(dead_code)]
pub fn argmin(&self) -> usize {
self.data.iter().enumerate().min_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap()).map(|(i, _)| i).unwrap()
}
/// Returns a slice of the array from start to end (exclusive)
///
/// # Arguments
///
/// * `start` - The starting index of the slice.
/// * `end` - The ending index of the slice (exclusive).
///
/// # Returns
///
/// A new NDArray containing the specified slice.
#[allow(dead_code)]
pub fn slice(&self, start: usize, end: usize) -> Self {
println!("Slicing array:");
println!(" Original shape: {:?}", self.shape);
println!(" Start: {}, End: {}", start, end);
let mut new_shape = self.shape.clone();
new_shape[0] = end - start;
if self.ndim() == 2 {
let cols = self.shape[1];
let start_idx = start * cols;
let end_idx = end * cols;
println!(" 2D array: keeping columns, new shape will be: {:?}", new_shape);
let sliced_data = self.data[start_idx..end_idx].to_vec();
println!(" Sliced data length: {}", sliced_data.len());
NDArray::new(sliced_data, new_shape)
} else {
println!(" 1D array: simple slice");
NDArray::new(self.data[start..end].to_vec(), new_shape)
}
}
/// Converts an NDArray of labels into a one-hot encoded NDArray
///
/// # Arguments
///
/// * `labels` - An NDArray containing numerical labels
///
/// # Returns
///
/// A new NDArray with one-hot encoded labels where each row corresponds to one label
///
/// # Panics
///
/// Panics if the input contains non-integer values
pub fn one_hot_encode(labels: &NDArray) -> Self {
// Verify that all values are integers
for &value in labels.data() {
// Check if the value is effectively an integer
if value.fract() != 0.0 {
panic!("All values must be integers for one-hot encoding");
}
}
// Convert values to integers and find unique classes
let labels_int: Vec<i32> = labels.data()
.iter()
.map(|&x| x as i32)
.collect();
// Find min and max to determine the range of classes
let min_label = labels_int.iter().min().unwrap();
let max_label = labels_int.iter().max().unwrap();
let num_classes = (max_label - min_label + 1) as usize;
let mut data = vec![0.0; labels_int.len() * num_classes];
// Shift indices by min_label to handle negative values
for (i, &label) in labels_int.iter().enumerate() {
let shifted_label = (label - min_label) as usize;
data[i * num_classes + shifted_label] = 1.0;
}
NDArray::new(data, vec![labels_int.len(), num_classes])
}
/// Transposes a 2D array (matrix)
///
/// # Returns
///
/// A new NDArray with transposed dimensions.
pub fn transpose(&self) -> Result<Self, &'static str> {
if self.shape.len() != 2 {
return Err("transpose currently only supports 2D arrays");
}
let (rows, cols) = (self.shape[0], self.shape[1]);
let mut new_data = vec![0.0; rows * cols];
for i in 0..rows {
for j in 0..cols {
new_data[j * rows + i] = self.data[i * cols + j];
}
}
Ok(NDArray {
data: new_data,
shape: vec![cols, rows]
})
}
/// Performs matrix multiplication (dot product) between two 2D arrays
///
/// # Arguments
///
/// * `other` - The other NDArray to multiply with.
///
/// # Returns
///
/// A new NDArray resulting from the matrix multiplication.
pub fn dot(&self, other: &NDArray) -> Self {
assert_eq!(self.ndim(), 2, "Dot product is only defined for 2D arrays");
assert_eq!(other.ndim(), 2, "Dot product is only defined for 2D arrays");
assert_eq!(self.shape[1], other.shape[0], "Inner dimensions must match for dot product");
let rows = self.shape[0];
let cols = other.shape[1];
let mut result_data = vec![0.0; rows * cols];
for i in 0..rows {
for j in 0..cols {
let mut sum = 0.0;
for k in 0..self.shape[1] {
sum += self.data[i * self.shape[1] + k] * other.data[k * other.shape[1] + j];
}
result_data[i * cols + j] = sum;
}
}
NDArray::new(result_data, vec![rows, cols])
}
/// Performs element-wise multiplication between two arrays
///
/// # Arguments
///
/// * `other` - The other NDArray to multiply with.
///
/// # Returns
///
/// A new NDArray resulting from the element-wise multiplication.
pub fn multiply(&self, other: &NDArray) -> Self {
assert_eq!(self.shape, other.shape, "Shapes must match for element-wise multiplication");
let data: Vec<f64> = self.data.iter().zip(other.data.iter()).map(|(a, b)| a * b).collect();
NDArray::new(data, self.shape.clone())
}
/// Subtracts a scalar from each element in the array
///
/// # Arguments
///
/// * `scalar` - The scalar value to subtract.
///
/// # Returns
///
/// A new NDArray with the scalar subtracted from each element.
pub fn scalar_sub(&self, scalar: f64) -> Self {
let data: Vec<f64> = self.data.iter().map(|&x| x - scalar).collect();
NDArray::new(data, self.shape.clone())
}
/// Multiplies each element in the array by a scalar
///
/// # Arguments
///
/// * `scalar` - The scalar value to multiply.
///
/// # Returns
///
/// A new NDArray with each element multiplied by the scalar.
pub fn multiply_scalar(&self, scalar: f64) -> Self {
let data: Vec<f64> = self.data.iter().map(|&x| x * scalar).collect();
NDArray::new(data, self.shape.clone())
}
/// Clips the values in the array to a specified range
///
/// # Arguments
///
/// * `min` - The minimum value to clip to.
/// * `max` - The maximum value to clip to.
///
/// # Returns
///
/// A new NDArray with values clipped to the specified range.
pub fn clip(&self, min: f64, max: f64) -> Self {
let data: Vec<f64> = self.data.iter().map(|&x| x.clamp(min, max)).collect();
NDArray::new(data, self.shape.clone())
}
/// Performs element-wise division between two arrays
///
/// # Arguments
///
/// * `other` - The other NDArray to divide by.
///
/// # Returns
///
/// A new NDArray resulting from the element-wise division.
pub fn divide(&self, other: &NDArray) -> Self {
assert_eq!(self.shape, other.shape, "Shapes must match for element-wise division");
let data: Vec<f64> = self.data.iter().zip(other.data.iter()).map(|(a, b)| a / b).collect();
NDArray::new(data, self.shape.clone())
}
/// Divides each element in the array by a scalar
///
/// # Arguments
///
/// * `scalar` - The scalar value to divide by.
///
/// # Returns
///
/// A new NDArray with each element divided by the scalar.
pub fn divide_scalar(&self, scalar: f64) -> Self {
let data: Vec<f64> = self.data.iter().map(|&x| x / scalar).collect();
NDArray::new(data, self.shape.clone())
}
/// Sums the elements of the array along a specified axis
///
/// # Arguments
///
/// * `axis` - The axis along which to sum the elements.
///
/// # Returns
///
/// A new NDArray with the summed elements along the specified axis.
pub fn sum_axis(&self, axis: usize) -> Self {
if axis >= self.shape.len() {
panic!("Axis {} out of bounds for shape {:?}", axis, self.shape);
}
match axis {
0 => {
let cols = self.shape[1];
let mut result = vec![0.0; cols];
for j in 0..cols {
for i in 0..self.shape[0] {
result[j] += self.data[i * cols + j];
}
}
NDArray::new(result, vec![1, cols])
},
1 => {
let cols = self.shape[1];
let mut result = vec![0.0; self.shape[0]];
for i in 0..self.shape[0] {
for j in 0..cols {
result[i] += self.data[i * cols + j];
}
}
NDArray::new(result, vec![self.shape[0], 1])
},
_ => panic!("Unsupported axis {}", axis)
}
}
/// Performs element-wise subtraction between two arrays
///
/// # Arguments
///
/// * `other` - The other NDArray to subtract.
///
/// # Returns
///
/// A new NDArray resulting from the element-wise subtraction.
pub fn subtract(&self, other: &NDArray) -> Self {
assert_eq!(self.shape, other.shape, "Shapes must match for element-wise subtraction");
let data: Vec<f64> = self.data.iter().zip(other.data.iter()).map(|(a, b)| a - b).collect();
NDArray::new(data, self.shape.clone())
}
/// Adds a scalar to each element in the array
///
/// # Arguments
///
/// * `scalar` - The scalar value to add.
///
/// # Returns
///
/// A new NDArray with the scalar added to each element.
pub fn add_scalar(&self, scalar: f64) -> Self {
let data: Vec<f64> = self.data.iter().map(|&x| x + scalar).collect();
NDArray::new(data, self.shape.clone())
}
/// Calculates the natural logarithm of each element in the array
///
/// # Returns
///
/// A new NDArray with the natural logarithm of each element.
pub fn log(&self) -> Self {
let data: Vec<f64> = self.data.iter().map(|&x| x.ln()).collect();
NDArray::new(data, self.shape.clone())
}
/// Sums all elements in the array
///
/// # Returns
///
/// The sum of all elements as an f64.
pub fn sum(&self) -> f64 {
self.data.iter().sum()
}
pub fn pad_to_size(&self, target_size: usize) -> Self {
if self.shape[0] >= target_size {
return self.clone();
}
let mut new_shape = self.shape.clone();
new_shape[0] = target_size;
let total_size: usize = new_shape.iter().product();
// Create new data vector with zeros
let mut new_data = vec![0.0; total_size];
// Copy existing data
let row_size = self.shape.iter().skip(1).product::<usize>();
let existing_data_size = self.shape[0] * row_size;
new_data[..existing_data_size].copy_from_slice(&self.data);
NDArray::new(new_data, new_shape)
}
/// Add layer normalization
pub fn layer_normalize(&self) -> Self {
let (rows, cols) = (self.shape[0], self.shape[1]);
let mut result = vec![0.0; self.data.len()];
for i in 0..rows {
let start = i * cols;
let end = start + cols;
let row = &self.data[start..end];
// Calculate mean and variance
let mean: f64 = row.iter().sum::<f64>() / cols as f64;
let var: f64 = row.iter()
.map(|&x| (x - mean).powi(2))
.sum::<f64>() / cols as f64;
let std = (var + 1e-5).sqrt();
// Normalize
for j in 0..cols {
result[start + j] = (row[j] - mean) / std;
}
}
NDArray::new(result, self.shape.clone())
}
/// Add batch normalization
pub fn batch_normalize(&self) -> Self {
let (batch_size, features) = (self.shape[0], self.shape[1]);
let mut result = vec![0.0; self.data.len()];
// For each feature
for j in 0..features {
// Calculate mean and variance across the batch
let mut mean = 0.0;
let mut var = 0.0;
// Calculate mean
for i in 0..batch_size {
mean += self.data[i * features + j];
}
mean /= batch_size as f64;
// Calculate variance
for i in 0..batch_size {
var += (self.data[i * features + j] - mean).powi(2);
}
var /= batch_size as f64;
// Normalize
let std = (var + 1e-5).sqrt();
for i in 0..batch_size {
result[i * features + j] = (self.data[i * features + j] - mean) / std;
}
}
NDArray::new(result, self.shape.clone())
}
pub fn add(self, other: &NDArray) -> Self {
println!("Adding arrays:");
println!(" Left shape: {:?}", self.shape);
println!(" Right shape: {:?}", other.shape);
// Handle broadcasting for shapes like [N, M] + [1, M]
if self.shape.len() == other.shape.len() &&
other.shape[0] == 1 &&
self.shape[1] == other.shape[1] {
println!(" Performing broadcasting addition");
let mut result_data = Vec::with_capacity(self.data.len());
let cols = other.shape[1];
// Add the broadcasted row to each row of self
for i in 0..self.shape[0] {
for j in 0..cols {
result_data.push(self.data[i * cols + j] + other.data[j]);
}
}
let result = NDArray::new(result_data, self.shape.clone());
println!(" Result shape: {:?}", result.shape);
return result;
}
// Regular element-wise addition for matching shapes
if self.shape != other.shape {
panic!("Shapes must match for element-wise addition\n left: {:?}\n right: {:?}",
self.shape, other.shape);
}
let data: Vec<f64> = self.data.iter()
.zip(other.data.iter())
.map(|(a, b)| a + b)
.collect();
NDArray::new(data, self.shape.clone())
}
/// Returns the mean of the array
pub fn mean(&self) -> f64 {
self.sum() / self.data.len() as f64
}
/// Returns the standard deviation of the array
pub fn std(&self) -> f64 {
let mean = self.mean();
let variance = self.data.iter()
.map(|&x| (x - mean).powi(2))
.sum::<f64>() / self.data.len() as f64;
variance.sqrt()
}
/// Returns the minimum value along the specified axis
pub fn min_axis(&self, axis: usize) -> Result<Self, &'static str> {
if axis >= self.shape.len() {
return Err("Axis out of bounds");
}
match axis {
0 => {
if self.shape.len() != 2 {
return Err("min_axis(0) requires 2D array");
}
let cols = self.shape[1];
let mut result = vec![f64::INFINITY; cols];
for j in 0..cols {
for i in 0..self.shape[0] {
result[j] = result[j].min(self.data[i * cols + j]);
}
}
Ok(NDArray::new(result, vec![1, cols]))
},
1 => {
if self.shape.len() != 2 {
return Err("min_axis(1) requires 2D array");
}
let cols = self.shape[1];
let mut result = vec![f64::INFINITY; self.shape[0]];
for i in 0..self.shape[0] {
for j in 0..cols {
result[i] = result[i].min(self.data[i * cols + j]);
}
}
Ok(NDArray::new(result, vec![self.shape[0], 1]))
},
_ => Err("Unsupported axis")
}
}
/// Concatenates two arrays along the specified axis
pub fn concatenate(&self, other: &Self, axis: usize) -> Result<Self, &'static str> {
if axis >= self.shape.len() {
return Err("Axis out of bounds");
}
if self.shape.len() != other.shape.len() {
return Err("Arrays must have same number of dimensions");
}
// Check that all dimensions except axis match
for (i, (&s1, &s2)) in self.shape.iter().zip(other.shape.iter()).enumerate() {
if i != axis && s1 != s2 {
return Err("All dimensions except concatenation axis must match");
}
}
let mut new_shape = self.shape.clone();
new_shape[axis] += other.shape[axis];
let mut new_data = Vec::with_capacity(self.data.len() + other.data.len());
match axis {
0 => {
new_data.extend_from_slice(&self.data);
new_data.extend_from_slice(&other.data);
},
1 => {
let rows = self.shape[0];
let cols1 = self.shape[1];
let cols2 = other.shape[1];
for i in 0..rows {
new_data.extend_from_slice(&self.data[i * cols1..(i + 1) * cols1]);
new_data.extend_from_slice(&other.data[i * cols2..(i + 1) * cols2]);
}
},
_ => return Err("Unsupported axis")
}
Ok(NDArray::new(new_data, new_shape))
}
pub fn map<F>(&self, f: F) -> Self
where F: Fn(f64) -> f64
{
let new_data: Vec<f64> = self.data.iter().map(|&x| f(x)).collect();
NDArray::new(new_data, self.shape.clone())
}
/// Returns the absolute values of array elements
///
/// # Returns
///
/// A new NDArray with absolute values
pub fn abs(&self) -> Self {
self.map(|x| x.abs())
}
/// Returns the exponential power of array elements
///
/// # Returns
///
/// A new NDArray with exponential values
pub fn power(&self, n: f64) -> Self {
self.map(|x| x.powf(n))
}
/// Returns the cumulative sum of array elements
///
/// # Returns
///
/// A new NDArray with cumulative sums
pub fn cumsum(&self) -> Self {
let mut result = Vec::with_capacity(self.data.len());
let mut sum = 0.0;
for &x in &self.data {
sum += x;
result.push(sum);
}
NDArray::new(result, self.shape.clone())
}
/// Returns array with elements rounded to specified decimals
///
/// # Arguments
///
/// * `decimals` - Number of decimal places to round to
///
/// # Returns
///
/// A new NDArray with rounded values
pub fn round(&self, decimals: i32) -> Self {
let factor = 10.0_f64.powi(decimals);
self.map(|x| (x * factor).round() / factor)
}
/// Returns indices that would sort the array
///
/// # Returns
///
/// A vector of indices that would sort the array
pub fn argsort(&self) -> Vec<usize> {
let mut indices: Vec<usize> = (0..self.data.len()).collect();
indices.sort_by(|&i, &j| self.data[i].partial_cmp(&self.data[j]).unwrap());
indices
}
/// Returns unique elements of the array
///
/// # Returns
///
/// A new NDArray containing unique elements in sorted order
pub fn unique(&self) -> Self {
let mut unique_vals = self.data.clone();
unique_vals.sort_by(|a, b| a.partial_cmp(b).unwrap());
unique_vals.dedup();
NDArray::new(unique_vals.to_vec(), vec![unique_vals.len()])
}
/// Applies a condition element-wise and returns a new array
///
/// # Arguments
///
/// * `condition` - Function that returns true/false for each element
/// * `x` - Value to use where condition is true
/// * `y` - Value to use where condition is false
///
/// # Returns
///
/// A new NDArray with values chosen based on condition
pub fn where_cond<F>(&self, condition: F, x: f64, y: f64) -> Self
where F: Fn(f64) -> bool
{
self.map(|val| if condition(val) { x } else { y })
}
/// Returns the median value of the array
///
/// # Returns
///
/// The median value as f64
pub fn median(&self) -> f64 {
let mut sorted = self.data.clone();
sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
let mid = sorted.len() / 2;
if sorted.len() % 2 == 0 {
(sorted[mid - 1] + sorted[mid]) / 2.0
} else {
sorted[mid]
}
}
/// Returns the maximum values along the specified axis
///
/// # Arguments
///
/// * `axis` - Axis along which to find maximum values
///
/// # Returns
///
/// NDArray containing maximum values along specified axis
pub fn max_axis(&self, axis: usize) -> Self {
if axis >= self.shape.len() {
panic!("Axis {} out of bounds for shape {:?}", axis, self.shape);
}
// Handle 1D array case
if self.shape.len() == 1 {
return NDArray::new(vec![self.data.iter().cloned().fold(f64::NEG_INFINITY, f64::max)], vec![1]);
}
// Handle 2D array case
match axis {
0 => {
let cols = self.shape[1];
let mut result = vec![f64::NEG_INFINITY; cols];
for j in 0..cols {
for i in 0..self.shape[0] {
result[j] = result[j].max(self.data[i * cols + j]);
}
}
NDArray::new(result, vec![1, cols])
},
1 => {
let cols = self.shape[1];
let mut result = vec![f64::NEG_INFINITY; self.shape[0]];
for i in 0..self.shape[0] {
for j in 0..cols {
result[i] = result[i].max(self.data[i * cols + j]);
}
}
NDArray::new(result, vec![self.shape[0], 1])
},
_ => panic!("Unsupported axis {}", axis)
}
}
/// Returns a string representation of the array
pub fn display(&self) -> String {
format!("NDArray(shape={:?}, data={:?})", self.shape, self.data)
}
/// Creates a new NDArray with random uniform values between 0 and 1
///
/// # Arguments
///
/// * `shape` - Shape of the array
///
/// # Example
/// ```
/// use nabla_ml::nab_array::NDArray;
///
/// let arr = NDArray::rand_uniform(&[2, 3]);
/// assert_eq!(arr.shape(), vec![2, 3]);
/// ```
pub fn rand_uniform(shape: &[usize]) -> Self {
let size: usize = shape.iter().product();
let uniform = Uniform::new(0.0, 1.0);
let mut rng = rand::thread_rng();
let data: Vec<f64> = (0..size)
.map(|_| uniform.sample(&mut rng))
.collect();
Self::new(data, shape.to_vec())
}
/// Calculates the mean along the specified axis
///
/// # Arguments
/// * `axis` - Axis along which to calculate mean (0 for columns, 1 for rows)
pub fn mean_axis(&self, axis: usize) -> Self {
let sum = self.sum_axis(axis);
let n = if axis == 0 { self.shape[0] } else { self.shape[1] } as f64;
sum.multiply_scalar(1.0 / n)
}
/// Calculates the variance along the specified axis
///
/// # Arguments
/// * `axis` - Axis along which to calculate variance (0 for columns, 1 for rows)
pub fn var_axis(&self, axis: usize) -> Self {
let mean = self.mean_axis(axis);
// For axis 0, we need to broadcast the mean to match original shape
let broadcasted_mean = if axis == 0 {
let mut result = Vec::with_capacity(self.data.len());
let cols = self.shape[1];
// Repeat mean values for each row
for _ in 0..self.shape[0] {
for j in 0..cols {
result.push(mean.data[j]);
}
}
NDArray::new(result, self.shape.clone())
} else {
mean
};
let centered = self.subtract(&broadcasted_mean);
let squared = centered.multiply(¢ered);
let n = if axis == 0 { self.shape[0] } else { self.shape[1] } as f64;
squared.sum_axis(axis).multiply_scalar(1.0 / n)
}
/// Converts a class vector (integers) to binary class matrix (one-hot encoding)
///
/// # Arguments
///
/// * `num_classes` - Optional number of classes. If None, it will be inferred from the data
///
/// # Returns
///
/// A 2D NDArray where each row is a one-hot encoded vector
///
/// # Example
///
/// ```
/// use nabla_ml::nab_array::NDArray;
///
/// let labels = NDArray::from_vec(vec![0.0, 1.0, 2.0]);
/// let categorical = labels.to_categorical(None);
/// assert_eq!(categorical.shape(), &[3, 3]);
/// assert_eq!(categorical.data(), &[1.0, 0.0, 0.0,
/// 0.0, 1.0, 0.0,
/// 0.0, 0.0, 1.0]);
/// ```
pub fn to_categorical(&self, num_classes: Option<usize>) -> Self {
// Ensure input is 1D
assert_eq!(self.ndim(), 1, "Input must be a 1D array");
// Find min and max labels to handle negative values
let min_label = self.data().iter()
.fold(f64::INFINITY, |a, &b| a.min(b)) as i32;
let max_label = self.data().iter()
.fold(f64::NEG_INFINITY, |a, &b| a.max(b)) as i32;
// Determine number of classes
let n_classes = num_classes.unwrap_or_else(||
(max_label - min_label + 1) as usize
);
let n_samples = self.shape()[0];
let mut categorical = vec![0.0; n_samples * n_classes];
// Fill the categorical array
for (sample_idx, &label) in self.data().iter().enumerate() {
// Shift label to be non-negative
let shifted_label = (label as i32 - min_label) as usize;
assert!(shifted_label < n_classes,
"Label {} is out of range for {} classes", label, n_classes);
let row_offset = sample_idx * n_classes;
categorical[row_offset + shifted_label] = 1.0;
}
NDArray::new(categorical, vec![n_samples, n_classes])
}
}
impl Add for NDArray {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
assert_eq!(self.shape, other.shape, "Shapes must match for element-wise addition");
let data = self.data.iter().zip(other.data.iter()).map(|(a, b)| a + b).collect();
NDArray::new(data, self.shape.clone())
}
}
impl Add<&NDArray> for NDArray {
type Output = Self;
fn add(self, other: &NDArray) -> Self::Output {
println!("Adding arrays:");
println!(" Left shape: {:?}", self.shape);
println!(" Right shape: {:?}", other.shape);
// Handle broadcasting for shapes like [N, M] + [1, M]
if self.shape.len() == other.shape.len() &&
other.shape[0] == 1 &&
self.shape[1] == other.shape[1] {
println!(" Performing broadcasting addition");
let mut result_data = Vec::with_capacity(self.data.len());
let cols = other.shape[1];
// Add the broadcasted row to each row of self
for i in 0..self.shape[0] {
for j in 0..cols {
result_data.push(self.data[i * cols + j] + other.data[j]);
}
}
let result = NDArray::new(result_data, self.shape.clone());
println!(" Result shape: {:?}", result.shape);
return result;
}
// Regular element-wise addition for matching shapes
if self.shape != other.shape {
panic!("Shapes must match for element-wise addition\n left: {:?}\n right: {:?}",
self.shape, other.shape);
}
let data: Vec<f64> = self.data.iter()
.zip(other.data.iter())
.map(|(a, b)| a + b)
.collect();
NDArray::new(data, self.shape.clone())
}
}
impl Sub for NDArray {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
assert_eq!(self.shape, other.shape, "Shapes must match for element-wise subtraction");
let data = self.data.iter().zip(other.data.iter()).map(|(a, b)| a - b).collect();
NDArray::new(data, self.shape.clone())
}
}
impl Mul<f64> for NDArray {
type Output = Self;
fn mul(self, scalar: f64) -> Self::Output {
let data = self.data.iter().map(|a| a * scalar).collect();
NDArray::new(data, self.shape.clone())
}
}
impl Add<f64> for NDArray {
type Output = Self;
fn add(self, scalar: f64) -> Self::Output {
self.add_scalar(scalar)
}
}
impl Mul<&NDArray> for f64 {
type Output = NDArray;
fn mul(self, rhs: &NDArray) -> NDArray {
rhs.multiply_scalar(self)
}
}
// Add std::fmt::Display implementation for convenient printing
impl std::fmt::Display for NDArray {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.display())
}
}
impl Sub<&NDArray> for NDArray {
type Output = Self;
fn sub(self, other: &NDArray) -> Self::Output {
if self.shape != other.shape {
panic!("Shapes must match for element-wise subtraction");
}
let data: Vec<f64> = self.data.iter()
.zip(other.data.iter())
.map(|(a, b)| a - b)
.collect();
NDArray::new(data, self.shape.clone())
}
}
/// Implements element-wise subtraction between two NDArray references
///
/// # Arguments
///
/// * `self` - The first NDArray reference
/// * `other` - The second NDArray reference to subtract from the first
///
/// # Returns
///
/// A new NDArray containing the element-wise difference
///
/// # Panics
///
/// Panics if the shapes of the two arrays don't match
impl<'a, 'b> Sub<&'b NDArray> for &'a NDArray {
type Output = NDArray;
fn sub(self, other: &'b NDArray) -> NDArray {
if self.shape != other.shape {
panic!("Shapes must match for element-wise subtraction");
}
let data: Vec<f64> = self.data.iter()
.zip(other.data.iter())
.map(|(a, b)| a - b)
.collect();
NDArray::new(data, self.shape.clone())
}
}
/// Implements element-wise addition between two NDArray references
///
/// # Arguments
///
/// * `self` - The first NDArray reference
/// * `other` - The second NDArray reference to add to the first
///
/// # Returns
///
/// A new NDArray containing the element-wise sum
///
/// # Panics
///
/// Panics if the shapes of the two arrays don't match
impl<'a, 'b> Add<&'b NDArray> for &'a NDArray {
type Output = NDArray;
fn add(self, other: &'b NDArray) -> NDArray {
if self.shape != other.shape {
panic!("Shapes must match for element-wise addition");
}
let data: Vec<f64> = self.data.iter()
.zip(other.data.iter())
.map(|(a, b)| a + b)
.collect();
NDArray::new(data, self.shape.clone())
}
}
/// Implements element-wise multiplication between two NDArray references
///
/// # Arguments
///
/// * `self` - The first NDArray reference
/// * `other` - The second NDArray reference to multiply with the first
///
/// # Returns
///
/// A new NDArray containing the element-wise product
///
/// # Panics
///
/// Panics if the shapes of the two arrays don't match
impl<'a, 'b> Mul<&'b NDArray> for &'a NDArray {
type Output = NDArray;
fn mul(self, other: &'b NDArray) -> NDArray {
if self.shape != other.shape {
panic!("Shapes must match for element-wise multiplication");
}
let data: Vec<f64> = self.data.iter()
.zip(other.data.iter())
.map(|(a, b)| a * b)
.collect();
NDArray::new(data, self.shape.clone())
}
}
/// Implements element-wise division between two NDArray references
///
/// # Arguments
///
/// * `self` - The first NDArray reference (numerator)
/// * `other` - The second NDArray reference (denominator)
///
/// # Returns
///
/// A new NDArray containing the element-wise quotient
///
/// # Panics
///
/// Panics if the shapes of the two arrays don't match
impl<'a, 'b> Div<&'b NDArray> for &'a NDArray {
type Output = NDArray;
fn div(self, other: &'b NDArray) -> NDArray {
if self.shape != other.shape {
panic!("Shapes must match for element-wise division");
}
let data: Vec<f64> = self.data.iter()
.zip(other.data.iter())
.map(|(a, b)| a / b)
.collect();
NDArray::new(data, self.shape.clone())
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Tests basic NDArray creation with explicit data and shape
#[test]
fn test_new_ndarray() {
let data = vec![1.0, 2.0, 3.0, 4.0];
let shape = vec![2, 2];
let array = NDArray::new(data.clone(), shape.clone());
assert_eq!(array.data(), &data);
assert_eq!(array.shape(), &shape);
}
/// Tests creation of 1D array from vector
#[test]
fn test_from_vec() {
let data = vec![1.0, 2.0, 3.0];
let array = NDArray::from_vec(data.clone());
assert_eq!(array.data(), &data);
assert_eq!(array.shape(), &[3]);
}
/// Tests array creation with evenly spaced values
#[test]
fn test_arange() {
let array = NDArray::arange(0.0, 5.0, 1.0);
assert_eq!(array.data(), &[0.0, 1.0, 2.0, 3.0, 4.0]);
}
/// Tests element-wise addition between two arrays
#[test]
fn test_element_wise_addition() {
let arr1 = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let arr2 = NDArray::from_vec(vec![4.0, 5.0, 6.0]);
let sum = arr1.clone() + arr2;
assert_eq!(sum.data(), &[5.0, 7.0, 9.0]);
}
/// Tests multiplication of array by scalar value
#[test]
fn test_scalar_multiplication() {
let arr = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let scaled = arr.clone() * 2.0;
assert_eq!(scaled.data(), &[2.0, 4.0, 6.0]);
}
/// Tests reshaping array to new dimensions while preserving data
#[test]
fn test_reshape() {
let arr = NDArray::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
let reshaped = arr.reshape(&[2, 3])
.expect("Failed to reshape array to valid dimensions");
assert_eq!(reshaped.data(), &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
}
/// Tests element-wise subtraction between arrays
#[test]
fn test_element_wise_subtraction() {
let arr1 = NDArray::from_vec(vec![5.0, 7.0, 9.0]);
let arr2 = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let diff = arr1 - arr2;
assert_eq!(diff.data(), &[4.0, 5.0, 6.0]);
}
/// Tests addition of scalar to array
#[test]
fn test_scalar_addition() {
let arr = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let result = arr + 1.0;
assert_eq!(result.data(), &[2.0, 3.0, 4.0]);
}
/// Tests combination of multiple operations in sequence
#[test]
#[allow(non_snake_case)]
fn test_combined_operations() {
let X = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let theta_1 = 2.0;
let theta_0 = 1.0;
let predictions = X.clone() * theta_1 + theta_0;
assert_eq!(predictions.data(), &[3.0, 5.0, 7.0]);
}
/// Tests one-hot encoding of label vectors
#[test]
fn test_one_hot_encode() {
let labels = NDArray::from_vec(vec![0.0, 1.0, 2.0, 1.0, 0.0]);
let one_hot = NDArray::one_hot_encode(&labels);
let expected = vec![
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
0.0, 1.0, 0.0,
1.0, 0.0, 0.0
];
assert_eq!(one_hot.shape(), &[5, 3]);
assert_eq!(one_hot.data(), &expected);
}
/// Tests one-hot encoding with negative label values
#[test]
fn test_one_hot_encode_negative() {
let labels = NDArray::from_vec(vec![-1.0, 0.0, 1.0, 0.0, -1.0]);
let one_hot = NDArray::one_hot_encode(&labels);
let expected = vec![
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
0.0, 1.0, 0.0,
1.0, 0.0, 0.0
];
assert_eq!(one_hot.shape(), &[5, 3]);
assert_eq!(one_hot.data(), &expected);
}
/// Tests that one-hot encoding fails with non-integer values
#[test]
#[should_panic(expected = "All values must be integers for one-hot encoding")]
fn test_one_hot_encode_non_integer() {
let labels = NDArray::from_vec(vec![0.0, 1.5, 2.0]);
NDArray::one_hot_encode(&labels);
}
/// Tests matrix transposition operation
#[test]
fn test_transpose() {
let arr = NDArray::from_matrix(vec![
vec![1.0, 2.0, 3.0],
vec![4.0, 5.0, 6.0]
]);
let transposed = arr.transpose()
.expect("Failed to transpose valid 2D array");
assert_eq!(transposed.shape(), &[3, 2]);
assert_eq!(transposed.data(), &[1.0, 4.0, 2.0, 5.0, 3.0, 6.0]);
}
/// Tests matrix multiplication (dot product)
#[test]
fn test_dot() {
let arr1 = NDArray::from_matrix(vec![
vec![1.0, 2.0, 3.0],
vec![4.0, 5.0, 6.0],
]);
let arr2 = NDArray::from_matrix(vec![
vec![7.0, 8.0],
vec![9.0, 10.0],
vec![11.0, 12.0],
]);
let dot = arr1.dot(&arr2);
assert_eq!(dot.data(), &[58.0, 64.0, 139.0, 154.0]); // Adjust expected values based on the dot product calculation
}
/// Tests element-wise multiplication between arrays
#[test]
fn test_multiply() {
let arr1 = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let arr2 = NDArray::from_vec(vec![4.0, 5.0, 6.0]);
let multiply = arr1.multiply(&arr2);
assert_eq!(multiply.data(), &[4.0, 10.0, 18.0]);
}
/// Tests subtraction of scalar from array
#[test]
fn test_scalar_sub() {
let arr = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let result = arr.scalar_sub(1.0);
assert_eq!(result.data(), &[0.0, 1.0, 2.0]);
}
/// Tests multiplication by scalar value
#[test]
fn test_multiply_scalar() {
let arr = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let result = arr.multiply_scalar(2.0);
assert_eq!(result.data(), &[2.0, 4.0, 6.0]);
}
/// Tests mapping function across array elements
#[test]
fn test_map() {
let arr = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let result = arr.map(|x| x * 2.0);
assert_eq!(result.data(), &[2.0, 4.0, 6.0]);
}
/// Tests clipping array values to specified range
#[test]
fn test_clip() {
let arr = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let result = arr.clip(1.0, 2.0);
assert_eq!(result.data(), &[1.0, 2.0, 2.0]);
}
/// Tests element-wise division between arrays
#[test]
fn test_divide() {
let arr1 = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let arr2 = NDArray::from_vec(vec![4.0, 5.0, 6.0]);
let divide = arr1.divide(&arr2);
assert_eq!(divide.data(), &[0.25, 0.4, 0.5]);
}
/// Tests division by scalar value
#[test]
fn test_divide_scalar() {
let arr = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let result = arr.divide_scalar(2.0);
assert_eq!(result.data(), &[0.5, 1.0, 1.5]);
}
/// Tests sum operation along specified axis
#[test]
fn test_sum_axis() {
let arr = NDArray::from_matrix(vec![
vec![1.0, 2.0, 3.0],
vec![4.0, 5.0, 6.0],
]);
let result = arr.sum_axis(0);
assert_eq!(result.data(), &[5.0, 7.0, 9.0]); // Sum along columns
assert_eq!(result.shape(), &[1, 3]); // Shape should be [1, 3]
let result = arr.sum_axis(1);
assert_eq!(result.data(), &[6.0, 15.0]); // Sum along rows
assert_eq!(result.shape(), &[2, 1]); // Shape should be [2, 1]
}
/// Tests element-wise subtraction between arrays
#[test]
fn test_subtract() {
let arr1 = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let arr2 = NDArray::from_vec(vec![4.0, 5.0, 6.0]);
let subtract = arr1.subtract(&arr2);
assert_eq!(subtract.data(), &[-3.0, -3.0, -3.0]);
}
/// Tests addition of scalar to array
#[test]
fn test_add_scalar() {
let arr = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let result = arr.add_scalar(1.0);
assert_eq!(result.data(), &[2.0, 3.0, 4.0]);
}
/// Tests creation of zero-filled array
#[test]
fn test_zeros() {
let shape = vec![2, 3];
let zeros = NDArray::zeros(shape);
assert_eq!(zeros.shape(), &[2, 3]);
assert_eq!(zeros.data(), &[0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
}
/// Tests natural logarithm of array elements
#[test]
fn test_log() {
let arr = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let result = arr.log();
assert_eq!(result.data(), &[0.0, 0.6931471805599453, 1.0986122886681098]);
}
/// Tests sum of all array elements
#[test]
fn test_sum() {
let arr = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let result = arr.sum();
assert_eq!(result, 6.0);
}
/// Tests calculation of array mean
#[test]
fn test_mean() {
let arr = NDArray::from_vec(vec![1.0, 2.0, 3.0, 4.0]);
assert_eq!(arr.mean(), 2.5);
}
/// Tests calculation of array standard deviation
#[test]
fn test_std() {
let arr = NDArray::from_vec(vec![1.0, 2.0, 3.0, 4.0]);
assert!((arr.std() - 1.118034).abs() < 1e-6);
}
/// Tests finding minimum values along specified axis
#[test]
fn test_min_axis() {
let arr = NDArray::from_matrix(vec![
vec![1.0, 2.0, 3.0],
vec![4.0, 0.5, 6.0],
]);
let min_axis_0 = arr.min_axis(0).unwrap();
assert_eq!(min_axis_0.data(), &[1.0, 0.5, 3.0]);
let min_axis_1 = arr.min_axis(1).unwrap();
assert_eq!(min_axis_1.data(), &[1.0, 0.5]);
}
/// Tests array concatenation along specified axis
#[test]
fn test_concatenate() {
let arr1 = NDArray::from_matrix(vec![
vec![1.0, 2.0],
vec![3.0, 4.0],
]);
let arr2 = NDArray::from_matrix(vec![
vec![5.0, 6.0],
vec![7.0, 8.0],
]);
let concat_0 = arr1.concatenate(&arr2, 0).unwrap();
assert_eq!(concat_0.shape(), &[4, 2]);
assert_eq!(concat_0.data(), &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]);
let concat_1 = arr1.concatenate(&arr2, 1).unwrap();
assert_eq!(concat_1.shape(), &[2, 4]);
assert_eq!(concat_1.data(), &[1.0, 2.0, 5.0, 6.0, 3.0, 4.0, 7.0, 8.0]);
}
/// Tests broadcasting addition between arrays of different shapes
#[test]
fn test_broadcast_addition() {
let arr1 = NDArray::from_matrix(vec![
vec![1.0, 2.0, 3.0],
vec![4.0, 5.0, 6.0]
]);
let arr2 = NDArray::from_matrix(vec![
vec![1.0, 2.0, 3.0]
]);
let result = arr1 + &arr2;
assert_eq!(result.shape(), &[2, 3]);
assert_eq!(result.data(), &[2.0, 4.0, 6.0, 5.0, 7.0, 9.0]);
}
/// Tests finding maximum value in array
#[test]
fn test_max() {
let arr = NDArray::from_vec(vec![1.0, 5.0, 3.0, 2.0]);
assert_eq!(arr.max(), 5.0);
}
/// Tests sum operation with broadcasting
#[test]
fn test_sum_with_broadcasting() {
let arr = NDArray::from_matrix(vec![
vec![1.0, 2.0, 3.0],
vec![4.0, 5.0, 6.0]
]);
let sum_cols = arr.sum_axis(0);
assert_eq!(sum_cols.shape(), &[1, 3]);
assert_eq!(sum_cols.data(), &[5.0, 7.0, 9.0]);
// Test broadcasting the sum back
let result = arr + &sum_cols;
assert_eq!(result.data(), &[6.0, 9.0, 12.0, 9.0, 12.0, 15.0]);
}
/// Tests various scalar operations (multiplication and addition)
#[test]
fn test_scalar_operations() {
let arr = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
// Test scalar multiplication from both sides
let result1 = arr.clone() * 2.0;
let result2 = 2.0 * &arr;
assert_eq!(result1.data(), result2.data());
// Test scalar addition
let result3 = arr + 1.0;
assert_eq!(result3.data(), &[2.0, 3.0, 4.0]);
}
/// Tests error handling for invalid array addition
#[test]
#[should_panic(expected = "Shapes must match for element-wise addition")]
fn test_invalid_addition() {
let arr1 = NDArray::from_matrix(vec![vec![1.0, 2.0]]);
let arr2 = NDArray::from_matrix(vec![vec![1.0, 2.0, 3.0]]);
let _result = arr1 + arr2;
}
/// Tests chaining multiple operations together
#[test]
fn test_chained_operations() {
let arr = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let result = (arr * 2.0 + 1.0).multiply_scalar(3.0);
assert_eq!(result.data(), &[9.0, 15.0, 21.0]);
}
/// Tests absolute value calculation
#[test]
fn test_abs() {
let arr = NDArray::from_vec(vec![-1.0, 2.0, -3.0]);
let result = arr.abs();
assert_eq!(result.data(), &[1.0, 2.0, 3.0]);
}
/// Tests exponential power calculation
#[test]
fn test_power() {
let arr = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let result = arr.power(2.0);
assert_eq!(result.data(), &[1.0, 4.0, 9.0]);
}
/// Tests cumulative sum calculation
#[test]
fn test_cumsum() {
let arr = NDArray::from_vec(vec![1.0, 2.0, 3.0, 4.0]);
let result = arr.cumsum();
assert_eq!(result.data(), &[1.0, 3.0, 6.0, 10.0]);
}
/// Tests rounding to specified decimals
#[test]
fn test_round() {
let arr = NDArray::from_vec(vec![1.234, 2.345, 3.456]);
let result = arr.round(2);
assert_eq!(result.data(), &[1.23, 2.35, 3.46]);
}
/// Tests getting indices that would sort the array
#[test]
fn test_argsort() {
let arr = NDArray::from_vec(vec![3.0, 1.0, 2.0]);
let indices = arr.argsort();
assert_eq!(indices, vec![1, 2, 0]);
}
/// Tests finding argmax along different axes
#[test]
fn test_argmax() {
let arr = NDArray::from_matrix(vec![
vec![1.0, 3.0, 2.0],
vec![4.0, 2.0, 6.0]
]);
// Test global argmax
assert_eq!(arr.argmax(None), vec![5]); // 6.0 is at index 5
// Test argmax along axis 0
assert_eq!(arr.argmax(Some(0)), vec![1, 0, 1]); // Max along columns
// Test argmax along axis 1
assert_eq!(arr.argmax(Some(1)), vec![1, 2]); // Max along rows
}
/// Tests finding unique values in array
#[test]
fn test_unique() {
let arr = NDArray::from_vec(vec![3.0, 1.0, 2.0, 1.0, 3.0]);
let unique = arr.unique();
assert_eq!(unique.data(), &[1.0, 2.0, 3.0]);
}
/// Tests conditional value selection
#[test]
fn test_where_cond() {
let arr = NDArray::from_vec(vec![-1.0, 2.0, -3.0, 4.0]);
let result = arr.where_cond(|x| x > 0.0, 1.0, -1.0);
assert_eq!(result.data(), &[-1.0, 1.0, -1.0, 1.0]);
}
/// Tests median calculation
#[test]
fn test_median() {
// Test odd number of elements
let arr1 = NDArray::from_vec(vec![1.0, 3.0, 2.0]);
assert_eq!(arr1.median(), 2.0);
// Test even number of elements
let arr2 = NDArray::from_vec(vec![1.0, 3.0, 2.0, 4.0]);
assert_eq!(arr2.median(), 2.5);
}
/// Tests finding maximum values along specified axis
#[test]
fn test_max_axis() {
let arr = NDArray::from_matrix(vec![
vec![1.0, 2.0, 3.0],
vec![4.0, 0.5, 6.0],
]);
let max_axis_0 = arr.max_axis(0);
assert_eq!(max_axis_0.shape(), &[1, 3]);
assert_eq!(max_axis_0.data(), &[4.0, 2.0, 6.0]); // Max along columns
let max_axis_1 = arr.max_axis(1);
assert_eq!(max_axis_1.shape(), &[2, 1]);
assert_eq!(max_axis_1.data(), &[3.0, 6.0]); // Max along rows
}
/// Tests element-wise subtraction between NDArray references
#[test]
fn test_element_wise_subtraction_ref() {
let arr1 = NDArray::from_vec(vec![5.0, 7.0, 9.0]);
let arr2 = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let diff = &arr1 - &arr2;
assert_eq!(diff.data(), &[4.0, 5.0, 6.0]);
}
/// Tests element-wise addition between NDArray references
#[test]
fn test_element_wise_addition_ref() {
let arr1 = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let arr2 = NDArray::from_vec(vec![4.0, 5.0, 6.0]);
let sum = &arr1 + &arr2;
assert_eq!(sum.data(), &[5.0, 7.0, 9.0]);
}
/// Tests element-wise multiplication between NDArray references
#[test]
fn test_element_wise_multiplication_ref() {
let arr1 = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let arr2 = NDArray::from_vec(vec![4.0, 5.0, 6.0]);
let product = &arr1 * &arr2;
assert_eq!(product.data(), &[4.0, 10.0, 18.0]);
}
/// Tests element-wise division between NDArray references
#[test]
fn test_element_wise_division_ref() {
let arr1 = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
let arr2 = NDArray::from_vec(vec![4.0, 5.0, 6.0]);
let quotient = &arr1 / &arr2;
assert_eq!(quotient.data(), &[0.25, 0.4, 0.5]);
}
/// Tests random uniform distribution generation
#[test]
fn test_rand_uniform() {
// Test shape
let shape = [2, 3];
let arr = NDArray::rand_uniform(&shape);
assert_eq!(arr.shape(), &[2, 3]);
// Test range (should be between 0 and 1)
for &val in arr.data() {
assert!(val >= 0.0 && val <= 1.0);
}
// Test randomness (generate multiple arrays and verify they're different)
let arr2 = NDArray::rand_uniform(&shape);
assert_ne!(arr.data(), arr2.data(), "Random arrays should be different");
// Test distribution (roughly uniform)
let large_arr = NDArray::rand_uniform(&[1000]);
let mean = large_arr.mean();
let std = large_arr.std();
// For uniform distribution between 0 and 1:
// Expected mean = 0.5
// Expected std = 1/sqrt(12) ≈ 0.289
assert!((mean - 0.5).abs() < 0.1, "Mean should be approximately 0.5");
assert!((std - 0.289).abs() < 0.1, "Std should be approximately 0.289");
}
#[test]
fn test_statistical_functions() {
let arr = NDArray::from_matrix(vec![
vec![1.0, 2.0, 3.0],
vec![4.0, 5.0, 6.0],
]);
// Test mean_axis
let mean_cols = arr.mean_axis(0);
assert_eq!(mean_cols.shape(), &[1, 3]);
assert_eq!(mean_cols.data(), &[2.5, 3.5, 4.5]);
// Test var_axis
let var_cols = arr.var_axis(0);
assert_eq!(var_cols.shape(), &[1, 3]);
assert_eq!(var_cols.data(), &[2.25, 2.25, 2.25]);
// Test sqrt (using NabMath trait)
let sqrt = arr.sqrt(); // This now uses the implementation from nab_math.rs
assert_eq!(sqrt.data(), &[1.0, 2.0_f64.sqrt(), 3.0_f64.sqrt(),
2.0, 5.0_f64.sqrt(), 6.0_f64.sqrt()]);
// Test add_scalar (using NabMath trait)
let added = arr.add_scalar(1.0); // This now uses the implementation from nab_math.rs
assert_eq!(added.data(), &[2.0, 3.0, 4.0, 5.0, 6.0, 7.0]);
}
#[test]
fn test_to_categorical() {
// Test basic functionality
let labels = NDArray::from_vec(vec![0.0, 1.0, 2.0]);
let categorical = labels.to_categorical(None);
assert_eq!(categorical.shape(), &[3, 3]);
assert_eq!(categorical.data(), &[
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0
]);
// Test with explicit num_classes
let labels = NDArray::from_vec(vec![0.0, 1.0]);
let categorical = labels.to_categorical(Some(3));
assert_eq!(categorical.shape(), &[2, 3]);
assert_eq!(categorical.data(), &[
1.0, 0.0, 0.0,
0.0, 1.0, 0.0
]);
// Test with negative labels
let labels = NDArray::from_vec(vec![-1.0, 0.0, 1.0]);
let categorical = labels.to_categorical(Some(3));
assert_eq!(categorical.shape(), &[3, 3]);
assert_eq!(categorical.data(), &[
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0
]);
}
}