ferray-stats 0.3.0

Statistical functions, reductions, sorting, and histograms for ferray
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
// ferray-stats: Core reductions — sum, prod, min, max, argmin, argmax, mean, var, std (REQ-1, REQ-2)
//
// `cumulative` used to exist as an empty placeholder whose only content
// was a note that cumulative functions live in nan_aware / mod.rs; it's
// been deleted to stop pretending there's a dedicated module there (#162).

pub mod nan_aware;
pub mod quantile;

use std::any::TypeId;

use ferray_core::error::{FerrayError, FerrayResult};
use ferray_core::{Array, Dimension, Element, IxDyn};
use num_traits::Float;

use crate::parallel;

/// Try SIMD-accelerated fused sum of squared differences if T is f64.
/// Returns sum((x - mean)²) without allocating an intermediate Vec.
#[inline]
fn try_simd_sum_sq_diff<T: Element + Copy + 'static>(data: &[T], mean: T) -> Option<T> {
    if TypeId::of::<T>() == TypeId::of::<f64>() {
        // SAFETY: TypeId check guarantees T is f64. size_of::<T>() == size_of::<f64>().
        let f64_slice =
            unsafe { std::slice::from_raw_parts(data.as_ptr().cast::<f64>(), data.len()) };
        let mean_f64: f64 = unsafe { std::mem::transmute_copy(&mean) };
        let result = parallel::simd_sum_sq_diff_f64(f64_slice, mean_f64);
        Some(unsafe { std::mem::transmute_copy(&result) })
    } else {
        None
    }
}

/// Try SIMD-accelerated pairwise sum if T is f64.
/// Returns the sum transmuted back to T, or None if T is not f64.
#[inline]
fn try_simd_pairwise_sum<T: Element + Copy + 'static>(data: &[T]) -> Option<T> {
    if TypeId::of::<T>() == TypeId::of::<f64>() {
        // SAFETY: TypeId check guarantees T is f64. size_of::<T>() == size_of::<f64>().
        let f64_slice =
            unsafe { std::slice::from_raw_parts(data.as_ptr().cast::<f64>(), data.len()) };
        let result = parallel::pairwise_sum_f64(f64_slice);
        Some(unsafe { std::mem::transmute_copy(&result) })
    } else {
        None
    }
}

// ---------------------------------------------------------------------------
// Internal axis-reduction helper
// ---------------------------------------------------------------------------

/// Compute row-major strides for a given shape.
pub(crate) fn compute_strides(shape: &[usize]) -> Vec<usize> {
    let ndim = shape.len();
    let mut strides = vec![1usize; ndim];
    for i in (0..ndim.saturating_sub(1)).rev() {
        strides[i] = strides[i + 1] * shape[i + 1];
    }
    strides
}

/// Flat index from a multi-index given row-major strides.
pub(crate) fn flat_index(multi: &[usize], strides: &[usize]) -> usize {
    multi.iter().zip(strides.iter()).map(|(i, s)| i * s).sum()
}

/// Increment a multi-index in row-major order. Returns false if overflowed.
pub(crate) fn increment_multi_index(multi: &mut [usize], shape: &[usize]) -> bool {
    for d in (0..multi.len()).rev() {
        multi[d] += 1;
        if multi[d] < shape[d] {
            return true;
        }
        multi[d] = 0;
    }
    false
}

/// General axis reduction parameterized on the output element type.
///
/// Walks `data` (in row-major order with `shape`), gathers each lane
/// along `axis` into a temporary `Vec<T>`, and calls `f` to collapse it
/// to an output of type `U`. Returns the concatenated outputs in
/// row-major order for the shape with `axis` removed.
///
/// Used as the shared backbone for `reduce_axis_general` (T=T path) and
/// `reduce_axis_general_u64` (T=T, U=u64 path). The two used to have
/// copy-pasted bodies differing only in return type (#161).
pub(crate) fn reduce_axis_typed<T, U, F>(data: &[T], shape: &[usize], axis: usize, f: F) -> Vec<U>
where
    T: Copy,
    F: Fn(&[T]) -> U,
{
    let ndim = shape.len();
    let axis_len = shape[axis];
    let strides = compute_strides(shape);

    // Output shape: shape with axis removed
    let out_shape: Vec<usize> = shape
        .iter()
        .enumerate()
        .filter(|&(i, _)| i != axis)
        .map(|(_, &s)| s)
        .collect();
    let out_size: usize = if out_shape.is_empty() {
        1
    } else {
        out_shape.iter().product()
    };

    let mut result = Vec::with_capacity(out_size);
    let mut out_multi = vec![0usize; out_shape.len()];
    let mut in_multi = vec![0usize; ndim];
    let mut lane_vec: Vec<T> = Vec::with_capacity(axis_len);

    for _ in 0..out_size {
        // Build input multi-index by inserting axis position
        let mut out_dim = 0;
        for (d, idx) in in_multi.iter_mut().enumerate() {
            if d == axis {
                *idx = 0;
            } else {
                *idx = out_multi[out_dim];
                out_dim += 1;
            }
        }

        // Gather lane values
        lane_vec.clear();
        for k in 0..axis_len {
            in_multi[axis] = k;
            let idx = flat_index(&in_multi, &strides);
            lane_vec.push(data[idx]);
        }

        result.push(f(&lane_vec));

        // Increment output multi-index
        if !out_shape.is_empty() {
            increment_multi_index(&mut out_multi, &out_shape);
        }
    }

    result
}

/// Thin wrapper: `T -> T` reduction. Preserved for the many call sites
/// that pass `Fn(&[T]) -> T` kernels.
#[inline]
pub(crate) fn reduce_axis_general<T, F>(data: &[T], shape: &[usize], axis: usize, f: F) -> Vec<T>
where
    T: Copy,
    F: Fn(&[T]) -> T,
{
    reduce_axis_typed(data, shape, axis, f)
}

/// In-place variant of [`reduce_axis_typed`]: writes results directly into
/// `dst` without allocating a fresh `Vec<U>` to hold the output.
///
/// Used by the `*_into` reductions (#563) so callers that pre-allocate an
/// output buffer truly avoid every per-call allocation. The destination
/// slice must already have exactly `out_size` elements where `out_size` is
/// the product of `shape` with `axis` removed (or 1 if the result is
/// 0-D); callers should validate this via [`check_out_shape`] before
/// invoking the kernel.
pub(crate) fn reduce_axis_typed_into<T, U, F>(
    data: &[T],
    shape: &[usize],
    axis: usize,
    dst: &mut [U],
    f: F,
) where
    T: Copy,
    F: Fn(&[T]) -> U,
{
    let ndim = shape.len();
    let axis_len = shape[axis];
    let strides = compute_strides(shape);

    // Output multi-index walks the shape with `axis` removed.
    let out_shape: Vec<usize> = shape
        .iter()
        .enumerate()
        .filter(|&(i, _)| i != axis)
        .map(|(_, &s)| s)
        .collect();

    debug_assert_eq!(
        dst.len(),
        if out_shape.is_empty() {
            1
        } else {
            out_shape.iter().product::<usize>()
        },
        "reduce_axis_typed_into: dst length must match the reduction's output size"
    );

    let mut out_multi = vec![0usize; out_shape.len()];
    let mut in_multi = vec![0usize; ndim];
    // Reused per-lane buffer — one allocation total instead of one per
    // lane the way `reduce_axis_typed` does it via `Vec::push`.
    let mut lane_vec: Vec<T> = Vec::with_capacity(axis_len);

    for slot in dst.iter_mut() {
        // Build the input multi-index by inserting the axis position.
        let mut out_dim = 0;
        for (d, idx) in in_multi.iter_mut().enumerate() {
            if d == axis {
                *idx = 0;
            } else {
                *idx = out_multi[out_dim];
                out_dim += 1;
            }
        }

        // Gather lane values into the reusable buffer.
        lane_vec.clear();
        for k in 0..axis_len {
            in_multi[axis] = k;
            let idx = flat_index(&in_multi, &strides);
            lane_vec.push(data[idx]);
        }

        *slot = f(&lane_vec);

        if !out_shape.is_empty() {
            increment_multi_index(&mut out_multi, &out_shape);
        }
    }
}

/// In-place T-to-T reduction wrapper around [`reduce_axis_typed_into`].
#[inline]
pub(crate) fn reduce_axis_general_into<T, F>(
    data: &[T],
    shape: &[usize],
    axis: usize,
    dst: &mut [T],
    f: F,
) where
    T: Copy,
    F: Fn(&[T]) -> T,
{
    reduce_axis_typed_into(data, shape, axis, dst, f);
}

/// Validate axis parameter and return an error if out of bounds.
pub(crate) const fn validate_axis(axis: usize, ndim: usize) -> FerrayResult<()> {
    if axis >= ndim {
        Err(FerrayError::axis_out_of_bounds(axis, ndim))
    } else {
        Ok(())
    }
}

/// Collect array data into a contiguous Vec in logical (row-major) order.
pub(crate) fn collect_data<T: Element + Copy, D: Dimension>(a: &Array<T, D>) -> Vec<T> {
    a.iter().copied().collect()
}

/// Borrow contiguous data or copy if strided. Avoids allocation for contiguous arrays.
pub(crate) enum DataRef<'a, T> {
    Borrowed(&'a [T]),
    Owned(Vec<T>),
}

impl<T> std::ops::Deref for DataRef<'_, T> {
    type Target = [T];
    fn deref(&self) -> &[T] {
        match self {
            DataRef::Borrowed(s) => s,
            DataRef::Owned(v) => v,
        }
    }
}

/// Get a reference to contiguous data, or copy if strided.
pub(crate) fn borrow_data<T: Element + Copy, D: Dimension>(a: &Array<T, D>) -> DataRef<'_, T> {
    if let Some(slice) = a.as_slice() {
        DataRef::Borrowed(slice)
    } else {
        DataRef::Owned(a.iter().copied().collect())
    }
}

/// Build an `IxDyn` result array from output shape and data.
pub(crate) fn make_result<T: Element>(
    out_shape: &[usize],
    data: Vec<T>,
) -> FerrayResult<Array<T, IxDyn>> {
    Array::from_vec(IxDyn::new(out_shape), data)
}

/// Validate that `out` has the expected shape and is C-contiguous,
/// returning a mutable slice into its backing buffer.
///
/// Shared by every `*_into` reduction (#467, #563) so the validation
/// surface lives in exactly one place. Broadcasting is intentionally not
/// allowed because the destination shape is fixed by the input + axis
/// combination — accepting a mismatched destination would silently
/// re-route to a different reduction shape.
pub(crate) fn check_out_shape<'a, T: Element + Copy>(
    out: &'a mut Array<T, IxDyn>,
    expected_shape: &[usize],
    op_name: &str,
) -> FerrayResult<&'a mut [T]> {
    if out.shape() != expected_shape {
        return Err(FerrayError::shape_mismatch(format!(
            "{op_name}_into: out shape {:?} does not match expected reduction shape {:?}",
            out.shape(),
            expected_shape
        )));
    }
    out.as_slice_mut().ok_or_else(|| {
        FerrayError::invalid_value(format!("{op_name}_into: out must be C-contiguous"))
    })
}

/// Compute the output shape when reducing along an axis.
pub(crate) fn output_shape(shape: &[usize], axis: usize) -> Vec<usize> {
    shape
        .iter()
        .enumerate()
        .filter(|&(i, _)| i != axis)
        .map(|(_, &s)| s)
        .collect()
}

// ---------------------------------------------------------------------------
// Multi-axis reduction helpers (issues #457 + #458)
// ---------------------------------------------------------------------------

/// Normalize the `axes: Option<&[usize]>` argument of a multi-axis reduction:
///
/// - `None` or `Some(&[])` expands to all axes `[0..ndim]` (reduce everything)
/// - Duplicate axes are an error (matches `NumPy`'s `np.sum(a, axis=(0, 0))`)
/// - Any out-of-bounds axis is an error
///
/// Returns the sorted, unique axis list.
pub(crate) fn normalize_axes(axes: Option<&[usize]>, ndim: usize) -> FerrayResult<Vec<usize>> {
    let ax: Vec<usize> = match axes {
        None | Some([]) => (0..ndim).collect(),
        Some(s) => s.to_vec(),
    };
    for &a in &ax {
        if a >= ndim {
            return Err(FerrayError::axis_out_of_bounds(a, ndim));
        }
    }
    let mut sorted = ax;
    sorted.sort_unstable();
    for w in sorted.windows(2) {
        if w[0] == w[1] {
            return Err(FerrayError::invalid_value(format!(
                "duplicate axis {} in reduction axes",
                w[0]
            )));
        }
    }
    Ok(sorted)
}

/// Compute the output shape when reducing over multiple axes.
///
/// `axes` must be sorted and unique (produced by [`normalize_axes`]).
/// With `keepdims = true`, reduced axes are replaced by size 1; otherwise
/// they are removed.
pub(crate) fn output_shape_axes(shape: &[usize], axes: &[usize], keepdims: bool) -> Vec<usize> {
    if keepdims {
        shape
            .iter()
            .enumerate()
            .map(|(i, &s)| if axes.contains(&i) { 1 } else { s })
            .collect()
    } else {
        shape
            .iter()
            .enumerate()
            .filter(|(i, _)| !axes.contains(i))
            .map(|(_, &s)| s)
            .collect()
    }
}

/// Reduce over a set of axes, returning `(result_data, output_shape)`.
///
/// `axes` must be sorted and unique (typically produced by [`normalize_axes`]).
/// The caller supplies a reduction function `f` that collapses a lane of
/// reduced-axis values to a single scalar.
///
/// The output shape honors `keepdims`.
pub(crate) fn reduce_axes_general<T: Copy, F: Fn(&[T]) -> T>(
    data: &[T],
    shape: &[usize],
    axes: &[usize],
    keepdims: bool,
    f: F,
) -> (Vec<T>, Vec<usize>) {
    let ndim = shape.len();
    let strides = compute_strides(shape);

    // Partition axes into reduce / keep.
    let is_reduce: Vec<bool> = (0..ndim).map(|i| axes.contains(&i)).collect();
    let keep_axes: Vec<usize> = (0..ndim).filter(|i| !is_reduce[*i]).collect();
    let reduce_axes: Vec<usize> = (0..ndim).filter(|i| is_reduce[*i]).collect();
    let keep_shape: Vec<usize> = keep_axes.iter().map(|&i| shape[i]).collect();
    let reduce_shape: Vec<usize> = reduce_axes.iter().map(|&i| shape[i]).collect();

    let out_size: usize = if keep_shape.is_empty() {
        1
    } else {
        keep_shape.iter().product()
    };
    let lane_size: usize = if reduce_shape.is_empty() {
        1
    } else {
        reduce_shape.iter().product()
    };

    let out_shape = output_shape_axes(shape, axes, keepdims);

    let mut result = Vec::with_capacity(out_size);
    let mut lane: Vec<T> = Vec::with_capacity(lane_size);
    let mut keep_multi = vec![0usize; keep_shape.len()];
    let mut reduce_multi = vec![0usize; reduce_shape.len()];
    let mut full_multi = vec![0usize; ndim];

    for _ in 0..out_size {
        // Fill kept-axis positions from keep_multi.
        for (i, &ax) in keep_axes.iter().enumerate() {
            full_multi[ax] = keep_multi[i];
        }

        // Gather values along all reduced axes.
        lane.clear();
        reduce_multi.fill(0);
        for _ in 0..lane_size {
            for (i, &ax) in reduce_axes.iter().enumerate() {
                full_multi[ax] = reduce_multi[i];
            }
            lane.push(data[flat_index(&full_multi, &strides)]);
            if !reduce_shape.is_empty() {
                increment_multi_index(&mut reduce_multi, &reduce_shape);
            }
        }

        result.push(f(&lane));

        if !keep_shape.is_empty() {
            increment_multi_index(&mut keep_multi, &keep_shape);
        }
    }

    (result, out_shape)
}

// ---------------------------------------------------------------------------
// sum
// ---------------------------------------------------------------------------

/// Sum of array elements over a given axis, or over all elements if axis is None.
///
/// Equivalent to `numpy.sum`.
///
/// **Note:** Unlike `NumPy`, which auto-promotes `int32` sums to `int64`,
/// ferray returns the same type as the input. For large integer arrays
/// this may overflow. Use [`sum_as_f64`] for overflow-safe integer summation.
///
/// # Examples
/// ```ignore
/// let a = Array::<f64, Ix1>::from_vec(Ix1::new([4]), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
/// let s = sum(&a, None).unwrap();
/// assert_eq!(s.iter().next(), Some(&10.0));
/// ```
pub fn sum<T, D>(a: &Array<T, D>, axis: Option<usize>) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + std::ops::Add<Output = T> + Copy + Send + Sync,
    D: Dimension,
{
    let data = borrow_data(a);
    match axis {
        None => {
            let total = try_simd_pairwise_sum(&data)
                .unwrap_or_else(|| parallel::parallel_sum(&data, <T as Element>::zero()));
            make_result(&[], vec![total])
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape = a.shape();
            let out_s = output_shape(shape, ax);
            let result = reduce_axis_general(&data, shape, ax, |lane| {
                try_simd_pairwise_sum(lane)
                    .unwrap_or_else(|| parallel::pairwise_sum(lane, <T as Element>::zero()))
            });
            make_result(&out_s, result)
        }
    }
}

/// Sum of array elements, returning `f64` regardless of input type.
///
/// This works on integer arrays (i32, u64, etc.) without overflow risk.
/// The result is always `Array<f64, IxDyn>`, matching `NumPy`'s behavior
/// of promoting integer sums to a wider type.
pub fn sum_as_f64<T, D>(a: &Array<T, D>, axis: Option<usize>) -> FerrayResult<Array<f64, IxDyn>>
where
    T: Element + Copy + Send + Sync + num_traits::ToPrimitive,
    D: Dimension,
{
    match axis {
        None => {
            let total: f64 = a.iter().map(|x| x.to_f64().unwrap_or(0.0)).sum();
            make_result(&[], vec![total])
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape = a.shape();
            let out_s = output_shape(shape, ax);
            let f64_data: Vec<f64> = a.iter().map(|x| x.to_f64().unwrap_or(0.0)).collect();
            let result = reduce_axis_general(&f64_data, shape, ax, |lane| lane.iter().sum());
            make_result(&out_s, result)
        }
    }
}

// ---------------------------------------------------------------------------
// prod
// ---------------------------------------------------------------------------

/// Product of array elements over a given axis.
///
/// **Note:** Unlike `NumPy`, which auto-promotes integer products,
/// ferray returns the same type as the input. For large integer arrays
/// this may overflow.
/// Equivalent to `numpy.prod`.
pub fn prod<T, D>(a: &Array<T, D>, axis: Option<usize>) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + std::ops::Mul<Output = T> + Copy + Send + Sync,
    D: Dimension,
{
    let data = borrow_data(a);
    match axis {
        None => {
            let total = parallel::parallel_prod(&data, <T as Element>::one());
            make_result(&[], vec![total])
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape = a.shape();
            let out_s = output_shape(shape, ax);
            let result = reduce_axis_general(&data, shape, ax, |lane| {
                lane.iter()
                    .copied()
                    .fold(<T as Element>::one(), |acc, x| acc * x)
            });
            make_result(&out_s, result)
        }
    }
}

// ---------------------------------------------------------------------------
// min / max
// ---------------------------------------------------------------------------

/// Minimum value of array elements over a given axis.
///
/// Equivalent to `numpy.min` / `numpy.amin`.
pub fn min<T, D>(a: &Array<T, D>, axis: Option<usize>) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + PartialOrd + Copy,
    D: Dimension,
{
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute min of empty array",
        ));
    }
    // NaN-propagating min: if either operand is NaN (comparison returns false
    // for both orderings), propagate NaN to match NumPy behavior.
    let nan_min = |a: T, b: T| -> T {
        if a <= b {
            a
        } else if a > b {
            b
        } else {
            // One of them is NaN — return whichever is unordered
            // (if a is NaN, a <= b and a > b are both false; return a)
            a
        }
    };
    let data = borrow_data(a);
    match axis {
        None => {
            let m = data.iter().copied().reduce(nan_min).unwrap();
            make_result(&[], vec![m])
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape = a.shape();
            let out_s = output_shape(shape, ax);
            let result = reduce_axis_general(&data, shape, ax, |lane| {
                lane.iter().copied().reduce(nan_min).unwrap()
            });
            make_result(&out_s, result)
        }
    }
}

/// Maximum value of array elements over a given axis.
///
/// Equivalent to `numpy.max` / `numpy.amax`.
pub fn max<T, D>(a: &Array<T, D>, axis: Option<usize>) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + PartialOrd + Copy,
    D: Dimension,
{
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute max of empty array",
        ));
    }
    // NaN-propagating max: same logic as min but reversed ordering.
    let nan_max = |a: T, b: T| -> T {
        if a >= b {
            a
        } else if a < b {
            b
        } else {
            a
        }
    };
    let data = borrow_data(a);
    match axis {
        None => {
            let m = data.iter().copied().reduce(nan_max).unwrap();
            make_result(&[], vec![m])
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape = a.shape();
            let out_s = output_shape(shape, ax);
            let result = reduce_axis_general(&data, shape, ax, |lane| {
                lane.iter().copied().reduce(nan_max).unwrap()
            });
            make_result(&out_s, result)
        }
    }
}

// ---------------------------------------------------------------------------
// argmin / argmax
// ---------------------------------------------------------------------------

/// Index of the minimum value. For axis=None, returns the flat index.
/// For axis=Some(ax), returns indices along that axis.
///
/// Equivalent to `numpy.argmin`.
pub fn argmin<T, D>(a: &Array<T, D>, axis: Option<usize>) -> FerrayResult<Array<u64, IxDyn>>
where
    T: Element + PartialOrd + Copy,
    D: Dimension,
{
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute argmin of empty array",
        ));
    }
    let data = borrow_data(a);
    match axis {
        None => {
            let idx = data
                .iter()
                .enumerate()
                .reduce(|(ai, av), (bi, bv)| if av <= bv { (ai, av) } else { (bi, bv) })
                .unwrap()
                .0 as u64;
            make_result(&[], vec![idx])
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape = a.shape();
            let out_s = output_shape(shape, ax);
            let result = reduce_axis_general_u64(&data, shape, ax, |lane| {
                lane.iter()
                    .enumerate()
                    .reduce(|(ai, av), (bi, bv)| if av <= bv { (ai, av) } else { (bi, bv) })
                    .unwrap()
                    .0 as u64
            });
            make_result(&out_s, result)
        }
    }
}

/// Index of the maximum value.
///
/// Equivalent to `numpy.argmax`.
pub fn argmax<T, D>(a: &Array<T, D>, axis: Option<usize>) -> FerrayResult<Array<u64, IxDyn>>
where
    T: Element + PartialOrd + Copy,
    D: Dimension,
{
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute argmax of empty array",
        ));
    }
    let data = borrow_data(a);
    match axis {
        None => {
            let idx = data
                .iter()
                .enumerate()
                .reduce(|(ai, av), (bi, bv)| if av >= bv { (ai, av) } else { (bi, bv) })
                .unwrap()
                .0 as u64;
            make_result(&[], vec![idx])
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape = a.shape();
            let out_s = output_shape(shape, ax);
            let result = reduce_axis_general_u64(&data, shape, ax, |lane| {
                lane.iter()
                    .enumerate()
                    .reduce(|(ai, av), (bi, bv)| if av >= bv { (ai, av) } else { (bi, bv) })
                    .unwrap()
                    .0 as u64
            });
            make_result(&out_s, result)
        }
    }
}

/// Thin wrapper: `T -> u64` reduction (for `argmin`/`argmax` /
/// `bincount` paths). Shares its body with `reduce_axis_general`
/// via `reduce_axis_typed`.
#[inline]
pub(crate) fn reduce_axis_general_u64<T, F>(
    data: &[T],
    shape: &[usize],
    axis: usize,
    f: F,
) -> Vec<u64>
where
    T: Copy,
    F: Fn(&[T]) -> u64,
{
    reduce_axis_typed(data, shape, axis, f)
}

// ---------------------------------------------------------------------------
// mean
// ---------------------------------------------------------------------------

/// Range (peak-to-peak) of array elements over a given axis.
///
/// Returns `max - min`. Analogous to `numpy.ptp(a, axis=...)`.
///
/// # Errors
/// Returns `FerrayError::InvalidValue` if the array is empty.
pub fn ptp<T, D>(a: &Array<T, D>, axis: Option<usize>) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + PartialOrd + Copy + std::ops::Sub<Output = T>,
    D: Dimension,
{
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute ptp of empty array",
        ));
    }
    let lo = min(a, axis)?;
    let hi = max(a, axis)?;
    let lo_data: Vec<T> = lo.iter().copied().collect();
    let hi_data: Vec<T> = hi.iter().copied().collect();
    let result: Vec<T> = hi_data
        .into_iter()
        .zip(lo_data)
        .map(|(h, l)| h - l)
        .collect();
    make_result(lo.shape(), result)
}

/// Weighted average of array elements.
///
/// When `weights` is `None`, equivalent to [`mean`]. When `Some(w)`, computes
/// `sum(a * w) / sum(w)` along the given axis (or over all elements when
/// `axis=None`). The weights array must have the same shape as `a` (the
/// 1-D-along-axis broadcasting form supported by NumPy is intentionally
/// omitted here — call broadcast yourself first if you need it).
///
/// Analogous to `numpy.average`.
///
/// # Errors
/// - `FerrayError::InvalidValue` if the array is empty.
/// - `FerrayError::ShapeMismatch` if `weights.shape() != a.shape()`.
/// - `FerrayError::InvalidValue` if the weight sum along an axis is zero.
pub fn average<T, D>(
    a: &Array<T, D>,
    weights: Option<&Array<T, D>>,
    axis: Option<usize>,
) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + Float + Send + Sync,
    D: Dimension,
{
    let Some(w) = weights else {
        return mean(a, axis);
    };
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute average of empty array",
        ));
    }
    if a.shape() != w.shape() {
        return Err(FerrayError::shape_mismatch(format!(
            "average: weights shape {:?} differs from array shape {:?}",
            w.shape(),
            a.shape(),
        )));
    }
    let a_data = borrow_data(a);
    let w_data = borrow_data(w);
    match axis {
        None => {
            let mut wsum = <T as Element>::zero();
            let mut acc = <T as Element>::zero();
            for (&x, &wi) in a_data.iter().zip(w_data.iter()) {
                wsum = wsum + wi;
                acc = acc + x * wi;
            }
            if wsum == <T as Element>::zero() {
                return Err(FerrayError::invalid_value("average: weights sum to zero"));
            }
            make_result(&[], vec![acc / wsum])
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape = a.shape();
            let out_s = output_shape(shape, ax);
            // Walk lanes for both arrays; we can reuse reduce_axis_general
            // by zipping data + weights into a tagged buffer. Simpler path:
            // build a side buffer of (a_lane, w_lane) per lane.
            let outer: usize = out_s.iter().product::<usize>().max(1);
            let lane_len = shape[ax];
            // Use the same lane-walk as reduce_axis_general. We replicate
            // the inner loop here so we can read both data + weights.
            let mut result = Vec::with_capacity(outer);
            // Compute strides for picking out the lane.
            let mut strides = vec![1usize; shape.len()];
            for i in (0..shape.len() - 1).rev() {
                strides[i] = strides[i + 1] * shape[i + 1];
            }
            let mut idx = vec![0usize; shape.len()];
            for _ in 0..outer {
                let mut wsum = <T as Element>::zero();
                let mut acc = <T as Element>::zero();
                for j in 0..lane_len {
                    idx[ax] = j;
                    let mut flat = 0usize;
                    for (d, &s) in idx.iter().zip(strides.iter()) {
                        flat += d * s;
                    }
                    let x = a_data[flat];
                    let wi = w_data[flat];
                    wsum = wsum + wi;
                    acc = acc + x * wi;
                }
                if wsum == <T as Element>::zero() {
                    return Err(FerrayError::invalid_value(
                        "average: weights sum to zero along axis",
                    ));
                }
                result.push(acc / wsum);
                // Advance multi-index over output dims (every dim except ax).
                idx[ax] = 0;
                for d in (0..shape.len()).rev() {
                    if d == ax {
                        continue;
                    }
                    idx[d] += 1;
                    if idx[d] < shape[d] {
                        break;
                    }
                    idx[d] = 0;
                }
            }
            make_result(&out_s, result)
        }
    }
}

/// Mean of array elements over a given axis.
///
/// Equivalent to `numpy.mean`. The result is always floating-point.
pub fn mean<T, D>(a: &Array<T, D>, axis: Option<usize>) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + Float + Send + Sync,
    D: Dimension,
{
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute mean of empty array",
        ));
    }
    let data = borrow_data(a);
    match axis {
        None => {
            let n = T::from(data.len()).unwrap();
            let total = try_simd_pairwise_sum(&data)
                .unwrap_or_else(|| parallel::pairwise_sum(&data, <T as Element>::zero()));
            make_result(&[], vec![total / n])
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape = a.shape();
            let out_s = output_shape(shape, ax);
            let axis_len = shape[ax];
            let n = T::from(axis_len).unwrap();
            let result = reduce_axis_general(&data, shape, ax, |lane| {
                let total = try_simd_pairwise_sum(lane)
                    .unwrap_or_else(|| parallel::pairwise_sum(lane, <T as Element>::zero()));
                total / n
            });
            make_result(&out_s, result)
        }
    }
}

/// Mean of array elements, returning `f64` regardless of input type.
///
/// This works on integer arrays (i32, u64, etc.) where [`mean`] would
/// fail because integers don't implement `Float`. The result is always
/// `Array<f64, IxDyn>`, matching `NumPy`'s behavior of promoting integer
/// means to float64.
///
/// Equivalent to `numpy.mean` for integer inputs.
pub fn mean_as_f64<T, D>(a: &Array<T, D>, axis: Option<usize>) -> FerrayResult<Array<f64, IxDyn>>
where
    T: Element + Copy + Send + Sync + num_traits::ToPrimitive,
    D: Dimension,
{
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute mean of empty array",
        ));
    }
    match axis {
        None => {
            let n = a.size() as f64;
            let total: f64 = a.iter().map(|x| x.to_f64().unwrap_or(0.0)).sum();
            make_result(&[], vec![total / n])
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape = a.shape();
            let out_s = output_shape(shape, ax);
            let axis_len = shape[ax] as f64;
            let f64_data: Vec<f64> = a.iter().map(|x| x.to_f64().unwrap_or(0.0)).collect();
            let result = reduce_axis_general(&f64_data, shape, ax, |lane| {
                let total: f64 = lane.iter().sum();
                total / axis_len
            });
            make_result(&out_s, result)
        }
    }
}

// ---------------------------------------------------------------------------
// var
// ---------------------------------------------------------------------------

/// Variance of array elements over a given axis.
///
/// `ddof` is the delta degrees of freedom (0 for population variance, 1 for sample).
/// Equivalent to `numpy.var`.
pub fn var<T, D>(a: &Array<T, D>, axis: Option<usize>, ddof: usize) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + Float + Send + Sync,
    D: Dimension,
{
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute variance of empty array",
        ));
    }
    let data = borrow_data(a);
    match axis {
        None => {
            let n = data.len();
            if n <= ddof {
                return Err(FerrayError::invalid_value(
                    "ddof >= number of elements, variance undefined",
                ));
            }
            let nf = T::from(n).unwrap();
            let mean_val = try_simd_pairwise_sum(&data)
                .unwrap_or_else(|| parallel::pairwise_sum(&data, <T as Element>::zero()))
                / nf;
            let sum_sq = try_simd_sum_sq_diff(&data, mean_val).unwrap_or_else(|| {
                data.iter().copied().fold(<T as Element>::zero(), |acc, x| {
                    let d = x - mean_val;
                    acc + d * d
                })
            });
            let var_val = sum_sq / T::from(n - ddof).unwrap();
            make_result(&[], vec![var_val])
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape = a.shape();
            let out_s = output_shape(shape, ax);
            let axis_len = shape[ax];
            if axis_len <= ddof {
                return Err(FerrayError::invalid_value(
                    "ddof >= axis length, variance undefined",
                ));
            }
            let nf = T::from(axis_len).unwrap();
            let denom = T::from(axis_len - ddof).unwrap();
            let result = reduce_axis_general(&data, shape, ax, |lane| {
                let mean_val = try_simd_pairwise_sum(lane)
                    .unwrap_or_else(|| parallel::pairwise_sum(lane, <T as Element>::zero()))
                    / nf;
                let sum_sq = try_simd_sum_sq_diff(lane, mean_val).unwrap_or_else(|| {
                    lane.iter().copied().fold(<T as Element>::zero(), |acc, x| {
                        let d = x - mean_val;
                        acc + d * d
                    })
                });
                sum_sq / denom
            });
            make_result(&out_s, result)
        }
    }
}

// ---------------------------------------------------------------------------
// std_
// ---------------------------------------------------------------------------

/// Standard deviation of array elements over a given axis.
///
/// `ddof` is the delta degrees of freedom.
/// Equivalent to `numpy.std`.
pub fn std_<T, D>(
    a: &Array<T, D>,
    axis: Option<usize>,
    ddof: usize,
) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + Float + Send + Sync,
    D: Dimension,
{
    let v = var(a, axis, ddof)?;
    let data: Vec<T> = v.iter().map(|x| x.sqrt()).collect();
    make_result(v.shape(), data)
}

// ---------------------------------------------------------------------------
// Multi-axis + keepdims public API (issues #457 + #458)
//
// These `*_axes` variants complement the existing single-axis reductions
// with two additional features:
//   1. `axes: Option<&[usize]>` — reduce over multiple axes at once
//      (None or empty slice means "reduce all axes")
//   2. `keepdims: bool` — preserve reduced axes as size 1 so the result
//      can broadcast back against the original array
//
// The existing single-axis `sum/prod/etc.` functions remain unchanged.
// ---------------------------------------------------------------------------

/// Multi-axis sum with optional `keepdims`.
///
/// Equivalent to `numpy.sum(a, axis=axes, keepdims=keepdims)`. If `axes` is
/// `None` or an empty slice, reduces over all axes.
///
/// # Errors
/// Returns `FerrayError::AxisOutOfBounds` on any out-of-range axis, or
/// `FerrayError::InvalidValue` on duplicate axes.
pub fn sum_axes<T, D>(
    a: &Array<T, D>,
    axes: Option<&[usize]>,
    keepdims: bool,
) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + std::ops::Add<Output = T> + Copy + Send + Sync,
    D: Dimension,
{
    let ax = normalize_axes(axes, a.ndim())?;
    let data = borrow_data(a);
    let (result, out_shape) = reduce_axes_general(&data, a.shape(), &ax, keepdims, |lane| {
        try_simd_pairwise_sum(lane)
            .unwrap_or_else(|| parallel::pairwise_sum(lane, <T as Element>::zero()))
    });
    make_result(&out_shape, result)
}

/// Multi-axis product with optional `keepdims`.
pub fn prod_axes<T, D>(
    a: &Array<T, D>,
    axes: Option<&[usize]>,
    keepdims: bool,
) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + std::ops::Mul<Output = T> + Copy + Send + Sync,
    D: Dimension,
{
    let ax = normalize_axes(axes, a.ndim())?;
    let data = borrow_data(a);
    let (result, out_shape) = reduce_axes_general(&data, a.shape(), &ax, keepdims, |lane| {
        lane.iter()
            .copied()
            .fold(<T as Element>::one(), |acc, x| acc * x)
    });
    make_result(&out_shape, result)
}

/// Multi-axis minimum with optional `keepdims`. NaN-propagating.
pub fn min_axes<T, D>(
    a: &Array<T, D>,
    axes: Option<&[usize]>,
    keepdims: bool,
) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + PartialOrd + Copy,
    D: Dimension,
{
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute min of empty array",
        ));
    }
    let ax = normalize_axes(axes, a.ndim())?;
    let nan_min = |a: T, b: T| -> T {
        if a <= b {
            a
        } else if a > b {
            b
        } else {
            a // NaN propagates
        }
    };
    let data = borrow_data(a);
    let (result, out_shape) = reduce_axes_general(&data, a.shape(), &ax, keepdims, |lane| {
        lane.iter().copied().reduce(nan_min).unwrap()
    });
    make_result(&out_shape, result)
}

/// Multi-axis maximum with optional `keepdims`. NaN-propagating.
pub fn max_axes<T, D>(
    a: &Array<T, D>,
    axes: Option<&[usize]>,
    keepdims: bool,
) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + PartialOrd + Copy,
    D: Dimension,
{
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute max of empty array",
        ));
    }
    let ax = normalize_axes(axes, a.ndim())?;
    let nan_max = |a: T, b: T| -> T {
        if a >= b {
            a
        } else if a < b {
            b
        } else {
            a // NaN propagates
        }
    };
    let data = borrow_data(a);
    let (result, out_shape) = reduce_axes_general(&data, a.shape(), &ax, keepdims, |lane| {
        lane.iter().copied().reduce(nan_max).unwrap()
    });
    make_result(&out_shape, result)
}

/// Multi-axis mean with optional `keepdims`.
pub fn mean_axes<T, D>(
    a: &Array<T, D>,
    axes: Option<&[usize]>,
    keepdims: bool,
) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + Float + Send + Sync,
    D: Dimension,
{
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute mean of empty array",
        ));
    }
    let ax = normalize_axes(axes, a.ndim())?;
    // Compute n = product of reduced-axis lengths.
    let shape = a.shape();
    let lane_len: usize = ax.iter().map(|&i| shape[i]).product();
    let n = T::from(lane_len).unwrap();
    let data = borrow_data(a);
    let (result, out_shape) = reduce_axes_general(&data, shape, &ax, keepdims, |lane| {
        let total = try_simd_pairwise_sum(lane)
            .unwrap_or_else(|| parallel::pairwise_sum(lane, <T as Element>::zero()));
        total / n
    });
    make_result(&out_shape, result)
}

/// Multi-axis variance with optional `keepdims` and Bessel correction `ddof`.
pub fn var_axes<T, D>(
    a: &Array<T, D>,
    axes: Option<&[usize]>,
    ddof: usize,
    keepdims: bool,
) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + Float + Send + Sync,
    D: Dimension,
{
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute variance of empty array",
        ));
    }
    let ax = normalize_axes(axes, a.ndim())?;
    let shape = a.shape();
    let lane_len: usize = ax.iter().map(|&i| shape[i]).product();
    if lane_len <= ddof {
        return Err(FerrayError::invalid_value(
            "ddof >= reduced-axis length, variance undefined",
        ));
    }
    let nf = T::from(lane_len).unwrap();
    let denom = T::from(lane_len - ddof).unwrap();
    let data = borrow_data(a);
    let (result, out_shape) = reduce_axes_general(&data, shape, &ax, keepdims, |lane| {
        let mean_val = try_simd_pairwise_sum(lane)
            .unwrap_or_else(|| parallel::pairwise_sum(lane, <T as Element>::zero()))
            / nf;
        let sum_sq = try_simd_sum_sq_diff(lane, mean_val).unwrap_or_else(|| {
            lane.iter().copied().fold(<T as Element>::zero(), |acc, x| {
                let d = x - mean_val;
                acc + d * d
            })
        });
        sum_sq / denom
    });
    make_result(&out_shape, result)
}

/// Multi-axis standard deviation with optional `keepdims`.
pub fn std_axes<T, D>(
    a: &Array<T, D>,
    axes: Option<&[usize]>,
    ddof: usize,
    keepdims: bool,
) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + Float + Send + Sync,
    D: Dimension,
{
    let v = var_axes(a, axes, ddof, keepdims)?;
    let data: Vec<T> = v.iter().map(|x| x.sqrt()).collect();
    make_result(v.shape(), data)
}

// ---------------------------------------------------------------------------
// `*_into` reductions: write into a caller-provided destination
//
// NumPy reductions accept an `out=` parameter that lets callers reuse a
// pre-allocated output buffer across repeated reductions on same-shaped
// data — common in streaming statistics, online ML metrics, etc. The
// ferray-stats reduction functions all allocate a fresh `Array<T, IxDyn>`
// internally, so we add `*_into` companion functions that take a
// `&mut Array<T, IxDyn>` destination and skip the allocation entirely.
//
// History:
//   #467 introduced the `*_into` API surface but the first implementation
//   still went through the allocating kernel and copied into `out`,
//   leaving one Vec materialization per call. #563 plumbs the destination
//   slice through the kernel itself via `reduce_axis_typed_into` so the
//   path is truly zero-alloc — only the per-lane scratch buffer
//   (allocated once per call, reused for every lane) and the input
//   contig-borrow allocation (only when the input is already non-contig)
//   remain.
// ---------------------------------------------------------------------------

/// Sum reduction writing into a pre-allocated destination.
///
/// Equivalent to `np.sum(a, axis=axis, out=out)`. The destination must
/// be C-contiguous and have exactly the shape that `sum(a, axis)` would
/// produce; broadcasting is not supported.
///
/// # Errors
/// - `FerrayError::AxisOutOfBounds` if `axis` is out of range.
/// - `FerrayError::ShapeMismatch` if `out.shape()` does not match the
///   expected reduction shape.
/// - `FerrayError::InvalidValue` if `out` is not C-contiguous.
pub fn sum_into<T, D>(
    a: &Array<T, D>,
    axis: Option<usize>,
    out: &mut Array<T, IxDyn>,
) -> FerrayResult<()>
where
    T: Element + std::ops::Add<Output = T> + Copy + Send + Sync,
    D: Dimension,
{
    let data = borrow_data(a);
    match axis {
        None => {
            let dst = check_out_shape(out, &[], "sum")?;
            let total = try_simd_pairwise_sum(&data)
                .unwrap_or_else(|| parallel::parallel_sum(&data, <T as Element>::zero()));
            dst[0] = total;
            Ok(())
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape_vec = a.shape().to_vec();
            let out_s = output_shape(&shape_vec, ax);
            let dst = check_out_shape(out, &out_s, "sum")?;
            reduce_axis_general_into(&data, &shape_vec, ax, dst, |lane| {
                try_simd_pairwise_sum(lane)
                    .unwrap_or_else(|| parallel::pairwise_sum(lane, <T as Element>::zero()))
            });
            Ok(())
        }
    }
}

/// Product reduction writing into a pre-allocated destination.
///
/// See [`sum_into`] for the contract on `out`.
pub fn prod_into<T, D>(
    a: &Array<T, D>,
    axis: Option<usize>,
    out: &mut Array<T, IxDyn>,
) -> FerrayResult<()>
where
    T: Element + std::ops::Mul<Output = T> + Copy + Send + Sync,
    D: Dimension,
{
    let data = borrow_data(a);
    match axis {
        None => {
            let dst = check_out_shape(out, &[], "prod")?;
            dst[0] = parallel::parallel_prod(&data, <T as Element>::one());
            Ok(())
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape_vec = a.shape().to_vec();
            let out_s = output_shape(&shape_vec, ax);
            let dst = check_out_shape(out, &out_s, "prod")?;
            reduce_axis_general_into(&data, &shape_vec, ax, dst, |lane| {
                lane.iter()
                    .copied()
                    .fold(<T as Element>::one(), |acc, x| acc * x)
            });
            Ok(())
        }
    }
}

/// Min reduction writing into a pre-allocated destination.
///
/// See [`sum_into`] for the contract on `out`. Empty input arrays return
/// `FerrayError::InvalidValue` because `min` of an empty set is undefined.
pub fn min_into<T, D>(
    a: &Array<T, D>,
    axis: Option<usize>,
    out: &mut Array<T, IxDyn>,
) -> FerrayResult<()>
where
    T: Element + PartialOrd + Copy + Send + Sync,
    D: Dimension,
{
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute min of empty array",
        ));
    }
    // Same NaN-propagating reducer as `min` so the two paths agree.
    let nan_min = |a: T, b: T| -> T {
        if a <= b {
            a
        } else if a > b {
            b
        } else {
            a
        }
    };
    let data = borrow_data(a);
    match axis {
        None => {
            let dst = check_out_shape(out, &[], "min")?;
            dst[0] = data.iter().copied().reduce(nan_min).unwrap();
            Ok(())
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape_vec = a.shape().to_vec();
            let out_s = output_shape(&shape_vec, ax);
            let dst = check_out_shape(out, &out_s, "min")?;
            reduce_axis_general_into(&data, &shape_vec, ax, dst, |lane| {
                lane.iter().copied().reduce(nan_min).unwrap()
            });
            Ok(())
        }
    }
}

/// Max reduction writing into a pre-allocated destination.
///
/// See [`sum_into`] for the contract on `out`. Empty input arrays return
/// `FerrayError::InvalidValue`.
pub fn max_into<T, D>(
    a: &Array<T, D>,
    axis: Option<usize>,
    out: &mut Array<T, IxDyn>,
) -> FerrayResult<()>
where
    T: Element + PartialOrd + Copy + Send + Sync,
    D: Dimension,
{
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute max of empty array",
        ));
    }
    let nan_max = |a: T, b: T| -> T {
        if a >= b {
            a
        } else if a < b {
            b
        } else {
            a
        }
    };
    let data = borrow_data(a);
    match axis {
        None => {
            let dst = check_out_shape(out, &[], "max")?;
            dst[0] = data.iter().copied().reduce(nan_max).unwrap();
            Ok(())
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape_vec = a.shape().to_vec();
            let out_s = output_shape(&shape_vec, ax);
            let dst = check_out_shape(out, &out_s, "max")?;
            reduce_axis_general_into(&data, &shape_vec, ax, dst, |lane| {
                lane.iter().copied().reduce(nan_max).unwrap()
            });
            Ok(())
        }
    }
}

/// Mean reduction writing into a pre-allocated destination.
///
/// See [`sum_into`] for the contract on `out`. Empty inputs return
/// `FerrayError::InvalidValue`.
pub fn mean_into<T, D>(
    a: &Array<T, D>,
    axis: Option<usize>,
    out: &mut Array<T, IxDyn>,
) -> FerrayResult<()>
where
    T: Element + Float + Send + Sync,
    D: Dimension,
{
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute mean of empty array",
        ));
    }
    let data = borrow_data(a);
    match axis {
        None => {
            let dst = check_out_shape(out, &[], "mean")?;
            let n = T::from(data.len()).unwrap();
            let total = try_simd_pairwise_sum(&data)
                .unwrap_or_else(|| parallel::pairwise_sum(&data, <T as Element>::zero()));
            dst[0] = total / n;
            Ok(())
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape_vec = a.shape().to_vec();
            let out_s = output_shape(&shape_vec, ax);
            let axis_len = shape_vec[ax];
            let n = T::from(axis_len).unwrap();
            let dst = check_out_shape(out, &out_s, "mean")?;
            reduce_axis_general_into(&data, &shape_vec, ax, dst, |lane| {
                let total = try_simd_pairwise_sum(lane)
                    .unwrap_or_else(|| parallel::pairwise_sum(lane, <T as Element>::zero()));
                total / n
            });
            Ok(())
        }
    }
}

/// Variance reduction writing into a pre-allocated destination.
///
/// `ddof` is the delta degrees of freedom. See [`sum_into`] for the
/// contract on `out`. Returns `FerrayError::InvalidValue` for empty
/// inputs or when `ddof >= n`.
pub fn var_into<T, D>(
    a: &Array<T, D>,
    axis: Option<usize>,
    ddof: usize,
    out: &mut Array<T, IxDyn>,
) -> FerrayResult<()>
where
    T: Element + Float + Send + Sync,
    D: Dimension,
{
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute variance of empty array",
        ));
    }
    let data = borrow_data(a);
    match axis {
        None => {
            let n = data.len();
            if n <= ddof {
                return Err(FerrayError::invalid_value(
                    "ddof >= number of elements, variance undefined",
                ));
            }
            let dst = check_out_shape(out, &[], "var")?;
            let nf = T::from(n).unwrap();
            let mean_val = try_simd_pairwise_sum(&data)
                .unwrap_or_else(|| parallel::pairwise_sum(&data, <T as Element>::zero()))
                / nf;
            let sum_sq = try_simd_sum_sq_diff(&data, mean_val).unwrap_or_else(|| {
                data.iter().copied().fold(<T as Element>::zero(), |acc, x| {
                    let d = x - mean_val;
                    acc + d * d
                })
            });
            dst[0] = sum_sq / T::from(n - ddof).unwrap();
            Ok(())
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape_vec = a.shape().to_vec();
            let axis_len = shape_vec[ax];
            if axis_len <= ddof {
                return Err(FerrayError::invalid_value(
                    "ddof >= axis length, variance undefined",
                ));
            }
            let out_s = output_shape(&shape_vec, ax);
            let nf = T::from(axis_len).unwrap();
            let denom = T::from(axis_len - ddof).unwrap();
            let dst = check_out_shape(out, &out_s, "var")?;
            reduce_axis_general_into(&data, &shape_vec, ax, dst, |lane| {
                let mean_val = try_simd_pairwise_sum(lane)
                    .unwrap_or_else(|| parallel::pairwise_sum(lane, <T as Element>::zero()))
                    / nf;
                let sum_sq = try_simd_sum_sq_diff(lane, mean_val).unwrap_or_else(|| {
                    lane.iter().copied().fold(<T as Element>::zero(), |acc, x| {
                        let d = x - mean_val;
                        acc + d * d
                    })
                });
                sum_sq / denom
            });
            Ok(())
        }
    }
}

/// Standard deviation reduction writing into a pre-allocated destination.
///
/// Computes the variance directly into `out` and then takes the
/// element-wise square root in place — the std value never lives in any
/// intermediate buffer.
pub fn std_into<T, D>(
    a: &Array<T, D>,
    axis: Option<usize>,
    ddof: usize,
    out: &mut Array<T, IxDyn>,
) -> FerrayResult<()>
where
    T: Element + Float + Send + Sync,
    D: Dimension,
{
    var_into(a, axis, ddof, out)?;
    // Variance is now in `out`; sqrt each element in place.
    let dst = out
        .as_slice_mut()
        .ok_or_else(|| FerrayError::invalid_value("std_into: out must be C-contiguous"))?;
    for slot in dst.iter_mut() {
        *slot = slot.sqrt();
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// `*_with` reductions: NumPy `initial=` and `where=` parameters (#459)
//
// NumPy reductions accept two extra parameters that ferray-stats was
// missing entirely:
//
//   - `initial`: starting value for the accumulator. Lets `np.sum([])`
//     return a non-zero seed, lets `np.min(arr, initial=999)` provide
//     a fallback for empty inputs, and lets cumulative-style code
//     thread an initial state through a reduction.
//
//   - `where`: same-shape boolean mask. Only positions where the mask
//     is `true` contribute to the reduction. This is the masked-array
//     equivalent without materializing a MaskedArray — `np.sum(arr,
//     where=arr > 0)` is the canonical "sum the positives" idiom.
//
// The `*_with` family adds these as `Option<T>` / `Option<&Array<bool,
// D>>` parameters. Mask broadcasting is intentionally not supported in
// this first cut — the mask must have exactly the same shape as the
// input. NumPy allows broadcast-compatible masks, but the same-shape
// path covers every common use case (predicates produced by comparison
// ufuncs against the same input always have a matching shape).
// ---------------------------------------------------------------------------

/// Prepare a `where` mask for use by a `*_with` reduction kernel.
///
/// Accepts an `Option<&Array<bool, IxDyn>>` so the mask can have any
/// rank independently of the input — callers with typed masks should
/// call `.to_dyn()` before passing in.
///
/// Returns:
/// - `Ok(None)` if the caller passed no mask.
/// - `Ok(Some(vec))` where `vec` is a row-major materialization of the
///   mask aligned with `a`'s flat logical order. For same-shape masks
///   this is a direct copy; for broadcast-compatible masks the mask is
///   broadcast into `a.shape()` first via
///   [`ferray_core::dimension::broadcast::broadcast_to`] (#565) and
///   then materialized.
/// - `Err(FerrayError::ShapeMismatch)` if the mask is not
///   broadcast-compatible with `a.shape()`.
fn prepare_where_mask<T, D>(
    a: &Array<T, D>,
    mask: Option<&Array<bool, IxDyn>>,
    op_name: &str,
) -> FerrayResult<Option<Vec<bool>>>
where
    T: Element,
    D: Dimension,
{
    use ferray_core::dimension::broadcast::broadcast_to;

    let Some(m) = mask else {
        return Ok(None);
    };

    // Fast path: the mask is already the same shape as the input.
    if m.shape() == a.shape() {
        return Ok(Some(m.iter().copied().collect()));
    }

    // Broadcast path: let the core machinery check compatibility and
    // produce a stride-tricked view at the target shape. We then
    // materialize it into a flat Vec<bool> aligned with `a`'s logical
    // row-major order.
    let view = broadcast_to(m, a.shape()).map_err(|_| {
        FerrayError::shape_mismatch(format!(
            "{op_name}: where mask shape {:?} is not broadcast-compatible with array shape {:?}",
            m.shape(),
            a.shape()
        ))
    })?;
    Ok(Some(view.iter().copied().collect()))
}

/// Walk a single axis-Some output position, gathering masked-true lane
/// values into `lane_buf` (cleared first). Used by every `*_with` axis
/// path so the lane-gather logic exists in one place.
///
/// `out_multi` is the output multi-index excluding `axis`. The function
/// reconstructs the corresponding input flat positions and consults the
/// mask in lockstep.
fn gather_lane_with_mask<T: Copy>(
    data: &[T],
    mask: Option<&[bool]>,
    shape: &[usize],
    strides: &[usize],
    axis: usize,
    out_multi: &[usize],
    lane_buf: &mut Vec<T>,
) {
    let ndim = shape.len();
    let axis_len = shape[axis];
    let mut in_multi = vec![0usize; ndim];
    let mut out_dim = 0;
    for (d, idx) in in_multi.iter_mut().enumerate() {
        if d == axis {
            *idx = 0;
        } else {
            *idx = out_multi[out_dim];
            out_dim += 1;
        }
    }
    lane_buf.clear();
    for k in 0..axis_len {
        in_multi[axis] = k;
        let idx = flat_index(&in_multi, strides);
        match mask {
            Some(m) if !m[idx] => {}
            _ => lane_buf.push(data[idx]),
        }
    }
}

/// Sum reduction with `initial` and `where` parameters.
///
/// Equivalent to `np.sum(a, axis=axis, initial=initial, where=where_mask)`.
///
/// `initial` defaults to `T::zero()` when `None`. `where_mask`, when
/// provided, must be broadcast-compatible with `a.shape()` (#565).
/// Callers with typed masks (e.g. `Array<bool, Ix1>`) should call
/// `.to_dyn()` before passing. Positions where the broadcast mask is
/// `false` are skipped.
///
/// # Errors
/// - `FerrayError::AxisOutOfBounds` if `axis` is out of range.
/// - `FerrayError::ShapeMismatch` if `where_mask` is not
///   broadcast-compatible with `a.shape()`.
pub fn sum_with<T, D>(
    a: &Array<T, D>,
    axis: Option<usize>,
    initial: Option<T>,
    where_mask: Option<&Array<bool, IxDyn>>,
) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + std::ops::Add<Output = T> + Copy,
    D: Dimension,
{
    let init = initial.unwrap_or_else(<T as Element>::zero);
    let data = borrow_data(a);
    let mask_vec = prepare_where_mask(a, where_mask, "sum")?;
    let mask_slice: Option<&[bool]> = mask_vec.as_deref();

    match axis {
        None => {
            let total = match mask_slice {
                None => data.iter().copied().fold(init, |acc, x| acc + x),
                Some(mask) => data
                    .iter()
                    .zip(mask.iter())
                    .filter(|&(_, &m)| m)
                    .fold(init, |acc, (&x, _)| acc + x),
            };
            make_result(&[], vec![total])
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape_vec = a.shape().to_vec();
            let out_s = output_shape(&shape_vec, ax);
            let strides = compute_strides(&shape_vec);

            let out_size: usize = if out_s.is_empty() {
                1
            } else {
                out_s.iter().product()
            };
            let mut result: Vec<T> = Vec::with_capacity(out_size);
            let mut out_multi = vec![0usize; out_s.len()];
            let mut lane_buf: Vec<T> = Vec::with_capacity(shape_vec[ax]);

            for _ in 0..out_size {
                gather_lane_with_mask(
                    &data,
                    mask_slice,
                    &shape_vec,
                    &strides,
                    ax,
                    &out_multi,
                    &mut lane_buf,
                );
                let lane_sum = lane_buf.iter().copied().fold(init, |acc, x| acc + x);
                result.push(lane_sum);
                if !out_s.is_empty() {
                    increment_multi_index(&mut out_multi, &out_s);
                }
            }
            make_result(&out_s, result)
        }
    }
}

/// Product reduction with `initial` and `where` parameters.
///
/// Equivalent to `np.prod(a, axis=axis, initial=initial, where=where_mask)`.
/// `initial` defaults to `T::one()` when `None`. `where_mask` is
/// broadcast-compatible with `a.shape()` (#565).
pub fn prod_with<T, D>(
    a: &Array<T, D>,
    axis: Option<usize>,
    initial: Option<T>,
    where_mask: Option<&Array<bool, IxDyn>>,
) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + std::ops::Mul<Output = T> + Copy,
    D: Dimension,
{
    let init = initial.unwrap_or_else(<T as Element>::one);
    let data = borrow_data(a);
    let mask_vec = prepare_where_mask(a, where_mask, "prod")?;
    let mask_slice: Option<&[bool]> = mask_vec.as_deref();

    match axis {
        None => {
            let total = match mask_slice {
                None => data.iter().copied().fold(init, |acc, x| acc * x),
                Some(mask) => data
                    .iter()
                    .zip(mask.iter())
                    .filter(|&(_, &m)| m)
                    .fold(init, |acc, (&x, _)| acc * x),
            };
            make_result(&[], vec![total])
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape_vec = a.shape().to_vec();
            let out_s = output_shape(&shape_vec, ax);
            let strides = compute_strides(&shape_vec);

            let out_size: usize = if out_s.is_empty() {
                1
            } else {
                out_s.iter().product()
            };
            let mut result: Vec<T> = Vec::with_capacity(out_size);
            let mut out_multi = vec![0usize; out_s.len()];
            let mut lane_buf: Vec<T> = Vec::with_capacity(shape_vec[ax]);

            for _ in 0..out_size {
                gather_lane_with_mask(
                    &data,
                    mask_slice,
                    &shape_vec,
                    &strides,
                    ax,
                    &out_multi,
                    &mut lane_buf,
                );
                let lane_prod = lane_buf.iter().copied().fold(init, |acc, x| acc * x);
                result.push(lane_prod);
                if !out_s.is_empty() {
                    increment_multi_index(&mut out_multi, &out_s);
                }
            }
            make_result(&out_s, result)
        }
    }
}

/// Min reduction with `initial` and `where` parameters.
///
/// Equivalent to `np.min(a, axis=axis, initial=initial, where=where_mask)`.
/// Unlike plain `min`, an empty input (or a fully-masked-out lane) is
/// allowed when `initial` is supplied — the result is `initial`. Without
/// `initial`, an empty lane is an error. `where_mask` is
/// broadcast-compatible with `a.shape()` (#565).
pub fn min_with<T, D>(
    a: &Array<T, D>,
    axis: Option<usize>,
    initial: Option<T>,
    where_mask: Option<&Array<bool, IxDyn>>,
) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + PartialOrd + Copy,
    D: Dimension,
{
    let nan_min = |a: T, b: T| -> T {
        if a <= b {
            a
        } else if a > b {
            b
        } else {
            a
        }
    };
    let data = borrow_data(a);
    let mask_vec = prepare_where_mask(a, where_mask, "min")?;
    let mask_slice: Option<&[bool]> = mask_vec.as_deref();

    let lane_min = |lane: &[T], initial: Option<T>| -> FerrayResult<T> {
        let mut iter = lane.iter().copied();
        let seed = match initial {
            Some(v) => Some(v),
            None => iter.next(),
        };
        match seed {
            Some(s) => Ok(iter.fold(s, nan_min)),
            None => Err(FerrayError::invalid_value(
                "min: empty lane and no initial value",
            )),
        }
    };

    match axis {
        None => {
            let total = match mask_slice {
                None => lane_min(&data, initial)?,
                Some(mask) => {
                    let filtered: Vec<T> = data
                        .iter()
                        .copied()
                        .zip(mask.iter().copied())
                        .filter_map(|(x, m)| if m { Some(x) } else { None })
                        .collect();
                    lane_min(&filtered, initial)?
                }
            };
            make_result(&[], vec![total])
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape_vec = a.shape().to_vec();
            let out_s = output_shape(&shape_vec, ax);
            let strides = compute_strides(&shape_vec);

            let out_size: usize = if out_s.is_empty() {
                1
            } else {
                out_s.iter().product()
            };
            let mut result: Vec<T> = Vec::with_capacity(out_size);
            let mut out_multi = vec![0usize; out_s.len()];
            let mut lane_buf: Vec<T> = Vec::with_capacity(shape_vec[ax]);

            for _ in 0..out_size {
                gather_lane_with_mask(
                    &data,
                    mask_slice,
                    &shape_vec,
                    &strides,
                    ax,
                    &out_multi,
                    &mut lane_buf,
                );
                result.push(lane_min(&lane_buf, initial)?);
                if !out_s.is_empty() {
                    increment_multi_index(&mut out_multi, &out_s);
                }
            }
            make_result(&out_s, result)
        }
    }
}

/// Max reduction with `initial` and `where` parameters.
///
/// Symmetric to [`min_with`]. `where_mask` is broadcast-compatible with
/// `a.shape()` (#565).
pub fn max_with<T, D>(
    a: &Array<T, D>,
    axis: Option<usize>,
    initial: Option<T>,
    where_mask: Option<&Array<bool, IxDyn>>,
) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + PartialOrd + Copy,
    D: Dimension,
{
    let nan_max = |a: T, b: T| -> T {
        if a >= b {
            a
        } else if a < b {
            b
        } else {
            a
        }
    };
    let data = borrow_data(a);
    let mask_vec = prepare_where_mask(a, where_mask, "max")?;
    let mask_slice: Option<&[bool]> = mask_vec.as_deref();

    let lane_max = |lane: &[T], initial: Option<T>| -> FerrayResult<T> {
        let mut iter = lane.iter().copied();
        let seed = match initial {
            Some(v) => Some(v),
            None => iter.next(),
        };
        match seed {
            Some(s) => Ok(iter.fold(s, nan_max)),
            None => Err(FerrayError::invalid_value(
                "max: empty lane and no initial value",
            )),
        }
    };

    match axis {
        None => {
            let total = match mask_slice {
                None => lane_max(&data, initial)?,
                Some(mask) => {
                    let filtered: Vec<T> = data
                        .iter()
                        .copied()
                        .zip(mask.iter().copied())
                        .filter_map(|(x, m)| if m { Some(x) } else { None })
                        .collect();
                    lane_max(&filtered, initial)?
                }
            };
            make_result(&[], vec![total])
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape_vec = a.shape().to_vec();
            let out_s = output_shape(&shape_vec, ax);
            let strides = compute_strides(&shape_vec);

            let out_size: usize = if out_s.is_empty() {
                1
            } else {
                out_s.iter().product()
            };
            let mut result: Vec<T> = Vec::with_capacity(out_size);
            let mut out_multi = vec![0usize; out_s.len()];
            let mut lane_buf: Vec<T> = Vec::with_capacity(shape_vec[ax]);

            for _ in 0..out_size {
                gather_lane_with_mask(
                    &data,
                    mask_slice,
                    &shape_vec,
                    &strides,
                    ax,
                    &out_multi,
                    &mut lane_buf,
                );
                result.push(lane_max(&lane_buf, initial)?);
                if !out_s.is_empty() {
                    increment_multi_index(&mut out_multi, &out_s);
                }
            }
            make_result(&out_s, result)
        }
    }
}

/// Mean reduction with a `where` mask.
///
/// Equivalent to `np.mean(a, axis=axis, where=where_mask)`. The divisor
/// is the count of `true` positions in the (broadcast) mask, NOT the
/// lane length — fully-masked-out lanes return `T::nan()` (matching
/// `NumPy`'s "`RuntimeWarning`: Mean of empty slice" behavior, but without
/// the warning machinery). `initial` is intentionally not modeled
/// because the divisor for an "initial-bumped" mean is ambiguous in
/// `NumPy` too. `where_mask` is broadcast-compatible with `a.shape()`
/// (#565).
pub fn mean_where<T, D>(
    a: &Array<T, D>,
    axis: Option<usize>,
    where_mask: Option<&Array<bool, IxDyn>>,
) -> FerrayResult<Array<T, IxDyn>>
where
    T: Element + Float,
    D: Dimension,
{
    let data = borrow_data(a);
    let mask_vec = prepare_where_mask(a, where_mask, "mean")?;
    let mask_slice: Option<&[bool]> = mask_vec.as_deref();

    match axis {
        None => {
            let (sum, count) = match mask_slice {
                None => {
                    let total = data
                        .iter()
                        .copied()
                        .fold(<T as Element>::zero(), |acc, x| acc + x);
                    (total, data.len())
                }
                Some(mask) => {
                    let mut s = <T as Element>::zero();
                    let mut c = 0usize;
                    for (&x, &m) in data.iter().zip(mask.iter()) {
                        if m {
                            s = s + x;
                            c += 1;
                        }
                    }
                    (s, c)
                }
            };
            let result = if count == 0 {
                <T as Float>::nan()
            } else {
                sum / T::from(count).unwrap()
            };
            make_result(&[], vec![result])
        }
        Some(ax) => {
            validate_axis(ax, a.ndim())?;
            let shape_vec = a.shape().to_vec();
            let out_s = output_shape(&shape_vec, ax);
            let strides = compute_strides(&shape_vec);

            let out_size: usize = if out_s.is_empty() {
                1
            } else {
                out_s.iter().product()
            };
            let mut result: Vec<T> = Vec::with_capacity(out_size);
            let mut out_multi = vec![0usize; out_s.len()];
            let mut lane_buf: Vec<T> = Vec::with_capacity(shape_vec[ax]);

            for _ in 0..out_size {
                gather_lane_with_mask(
                    &data,
                    mask_slice,
                    &shape_vec,
                    &strides,
                    ax,
                    &out_multi,
                    &mut lane_buf,
                );
                let lane_mean = if lane_buf.is_empty() {
                    <T as Float>::nan()
                } else {
                    let s = lane_buf
                        .iter()
                        .copied()
                        .fold(<T as Element>::zero(), |acc, x| acc + x);
                    s / T::from(lane_buf.len()).unwrap()
                };
                result.push(lane_mean);
                if !out_s.is_empty() {
                    increment_multi_index(&mut out_multi, &out_s);
                }
            }
            make_result(&out_s, result)
        }
    }
}

/// Single-axis argmin with optional `keepdims`.
///
/// `NumPy`'s `argmin` only accepts a single axis (or `None`); this mirrors
/// that constraint. `keepdims` preserves the reduced axis as size 1.
pub fn argmin_keepdims<T, D>(
    a: &Array<T, D>,
    axis: Option<usize>,
    keepdims: bool,
) -> FerrayResult<Array<u64, IxDyn>>
where
    T: Element + PartialOrd + Copy,
    D: Dimension,
{
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute argmin of empty array",
        ));
    }
    let ndim = a.ndim();
    let ax_vec: Vec<usize> = match axis {
        None => (0..ndim).collect(),
        Some(ax) => {
            if ax >= ndim {
                return Err(FerrayError::axis_out_of_bounds(ax, ndim));
            }
            vec![ax]
        }
    };
    let shape = a.shape();
    let data = borrow_data(a);
    let (result_f64, out_shape) =
        reduce_axes_general_u64(&data, shape, &ax_vec, keepdims, |lane| {
            lane.iter()
                .enumerate()
                .reduce(|(ai, av), (bi, bv)| if av <= bv { (ai, av) } else { (bi, bv) })
                .unwrap()
                .0 as u64
        });
    make_result(&out_shape, result_f64)
}

/// Single-axis argmax with optional `keepdims`.
pub fn argmax_keepdims<T, D>(
    a: &Array<T, D>,
    axis: Option<usize>,
    keepdims: bool,
) -> FerrayResult<Array<u64, IxDyn>>
where
    T: Element + PartialOrd + Copy,
    D: Dimension,
{
    if a.is_empty() {
        return Err(FerrayError::invalid_value(
            "cannot compute argmax of empty array",
        ));
    }
    let ndim = a.ndim();
    let ax_vec: Vec<usize> = match axis {
        None => (0..ndim).collect(),
        Some(ax) => {
            if ax >= ndim {
                return Err(FerrayError::axis_out_of_bounds(ax, ndim));
            }
            vec![ax]
        }
    };
    let shape = a.shape();
    let data = borrow_data(a);
    let (result_u64, out_shape) =
        reduce_axes_general_u64(&data, shape, &ax_vec, keepdims, |lane| {
            lane.iter()
                .enumerate()
                .reduce(|(ai, av), (bi, bv)| if av >= bv { (ai, av) } else { (bi, bv) })
                .unwrap()
                .0 as u64
        });
    make_result(&out_shape, result_u64)
}

/// Multi-axis reduction that returns `u64` values (used by argmin/argmax variants).
pub(crate) fn reduce_axes_general_u64<T: Copy, F: Fn(&[T]) -> u64>(
    data: &[T],
    shape: &[usize],
    axes: &[usize],
    keepdims: bool,
    f: F,
) -> (Vec<u64>, Vec<usize>) {
    let ndim = shape.len();
    let strides = compute_strides(shape);

    let is_reduce: Vec<bool> = (0..ndim).map(|i| axes.contains(&i)).collect();
    let keep_axes: Vec<usize> = (0..ndim).filter(|i| !is_reduce[*i]).collect();
    let reduce_axes: Vec<usize> = (0..ndim).filter(|i| is_reduce[*i]).collect();
    let keep_shape: Vec<usize> = keep_axes.iter().map(|&i| shape[i]).collect();
    let reduce_shape: Vec<usize> = reduce_axes.iter().map(|&i| shape[i]).collect();

    let out_size: usize = if keep_shape.is_empty() {
        1
    } else {
        keep_shape.iter().product()
    };
    let lane_size: usize = if reduce_shape.is_empty() {
        1
    } else {
        reduce_shape.iter().product()
    };
    let out_shape = output_shape_axes(shape, axes, keepdims);

    let mut result = Vec::with_capacity(out_size);
    let mut lane: Vec<T> = Vec::with_capacity(lane_size);
    let mut keep_multi = vec![0usize; keep_shape.len()];
    let mut reduce_multi = vec![0usize; reduce_shape.len()];
    let mut full_multi = vec![0usize; ndim];

    for _ in 0..out_size {
        for (i, &ax) in keep_axes.iter().enumerate() {
            full_multi[ax] = keep_multi[i];
        }

        lane.clear();
        reduce_multi.fill(0);
        for _ in 0..lane_size {
            for (i, &ax) in reduce_axes.iter().enumerate() {
                full_multi[ax] = reduce_multi[i];
            }
            lane.push(data[flat_index(&full_multi, &strides)]);
            if !reduce_shape.is_empty() {
                increment_multi_index(&mut reduce_multi, &reduce_shape);
            }
        }

        result.push(f(&lane));

        if !keep_shape.is_empty() {
            increment_multi_index(&mut keep_multi, &keep_shape);
        }
    }

    (result, out_shape)
}

// ---------------------------------------------------------------------------
// Re-export cumulative operations from ferray-ufunc for discoverability
// ---------------------------------------------------------------------------

/// Cumulative sum along an axis (or flattened if axis is None).
///
/// Re-exported from `ferray_ufunc::cumsum` for convenience.
pub fn cumsum<T, D>(a: &Array<T, D>, axis: Option<usize>) -> FerrayResult<Array<T, D>>
where
    T: Element + std::ops::Add<Output = T> + Copy,
    D: Dimension,
{
    ferray_ufunc::cumsum(a, axis)
}

/// Cumulative product along an axis (or flattened if axis is None).
///
/// Re-exported from `ferray_ufunc::cumprod` for convenience.
pub fn cumprod<T, D>(a: &Array<T, D>, axis: Option<usize>) -> FerrayResult<Array<T, D>>
where
    T: Element + std::ops::Mul<Output = T> + Copy,
    D: Dimension,
{
    ferray_ufunc::cumprod(a, axis)
}

#[cfg(test)]
mod tests {
    use super::*;
    use ferray_core::{Ix1, Ix2};

    #[test]
    fn test_sum_1d_no_axis() {
        let a = Array::<f64, Ix1>::from_vec(Ix1::new([4]), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
        let s = sum(&a, None).unwrap();
        assert_eq!(s.shape(), &[]);
        assert_eq!(s.iter().next(), Some(&10.0));
    }

    #[test]
    fn test_sum_2d_axis0() {
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let s = sum(&a, Some(0)).unwrap();
        assert_eq!(s.shape(), &[3]);
        let data: Vec<f64> = s.iter().copied().collect();
        assert_eq!(data, vec![5.0, 7.0, 9.0]);
    }

    #[test]
    fn test_sum_2d_axis1() {
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let s = sum(&a, Some(1)).unwrap();
        assert_eq!(s.shape(), &[2]);
        let data: Vec<f64> = s.iter().copied().collect();
        assert_eq!(data, vec![6.0, 15.0]);
    }

    #[test]
    fn test_prod_1d() {
        let a = Array::<f64, Ix1>::from_vec(Ix1::new([4]), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
        let p = prod(&a, None).unwrap();
        assert_eq!(p.iter().next(), Some(&24.0));
    }

    #[test]
    fn test_min_max() {
        let a = Array::<f64, Ix1>::from_vec(Ix1::new([4]), vec![3.0, 1.0, 4.0, 2.0]).unwrap();
        let mn = min(&a, None).unwrap();
        let mx = max(&a, None).unwrap();
        assert_eq!(mn.iter().next(), Some(&1.0));
        assert_eq!(mx.iter().next(), Some(&4.0));
    }

    #[test]
    fn test_argmin_argmax() {
        let a = Array::<f64, Ix1>::from_vec(Ix1::new([4]), vec![3.0, 1.0, 4.0, 2.0]).unwrap();
        let ami = argmin(&a, None).unwrap();
        let amx = argmax(&a, None).unwrap();
        assert_eq!(ami.iter().next(), Some(&1u64));
        assert_eq!(amx.iter().next(), Some(&2u64));
    }

    #[test]
    fn test_mean() {
        let a = Array::<f64, Ix1>::from_vec(Ix1::new([4]), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
        let m = mean(&a, None).unwrap();
        assert!((m.iter().next().unwrap() - 2.5).abs() < 1e-12);
    }

    #[test]
    fn test_var_population() {
        let a = Array::<f64, Ix1>::from_vec(Ix1::new([4]), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
        let v = var(&a, None, 0).unwrap();
        // var = ((1-2.5)^2 + (2-2.5)^2 + (3-2.5)^2 + (4-2.5)^2) / 4 = 1.25
        assert!((v.iter().next().unwrap() - 1.25).abs() < 1e-12);
    }

    #[test]
    fn test_var_sample() {
        let a = Array::<f64, Ix1>::from_vec(Ix1::new([4]), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
        let v = var(&a, None, 1).unwrap();
        // var = 5.0 / 3.0 = 1.6666...
        assert!((v.iter().next().unwrap() - 5.0 / 3.0).abs() < 1e-12);
    }

    #[test]
    fn test_std() {
        let a = Array::<f64, Ix1>::from_vec(Ix1::new([4]), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
        let s = std_(&a, None, 1).unwrap();
        let expected = (5.0_f64 / 3.0).sqrt();
        assert!((s.iter().next().unwrap() - expected).abs() < 1e-12);
    }

    #[test]
    fn test_sum_axis_out_of_bounds() {
        let a = Array::<f64, Ix1>::from_vec(Ix1::new([3]), vec![1.0, 2.0, 3.0]).unwrap();
        assert!(sum(&a, Some(1)).is_err());
    }

    #[test]
    fn test_cumsum_reexport() {
        let a = Array::<i32, Ix1>::from_vec(Ix1::new([4]), vec![1, 2, 3, 4]).unwrap();
        let cs = cumsum(&a, None).unwrap();
        assert_eq!(cs.as_slice().unwrap(), &[1, 3, 6, 10]);
    }

    #[test]
    fn test_cumprod_reexport() {
        let a = Array::<i32, Ix1>::from_vec(Ix1::new([4]), vec![1, 2, 3, 4]).unwrap();
        let cp = cumprod(&a, None).unwrap();
        assert_eq!(cp.as_slice().unwrap(), &[1, 2, 6, 24]);
    }

    #[test]
    fn test_min_2d_axis0() {
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![3.0, 1.0, 4.0, 1.0, 5.0, 2.0])
            .unwrap();
        let m = min(&a, Some(0)).unwrap();
        let data: Vec<f64> = m.iter().copied().collect();
        assert_eq!(data, vec![1.0, 1.0, 2.0]);
    }

    #[test]
    fn test_argmin_2d_axis1() {
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![3.0, 1.0, 4.0, 1.0, 5.0, 2.0])
            .unwrap();
        let ami = argmin(&a, Some(1)).unwrap();
        let data: Vec<u64> = ami.iter().copied().collect();
        assert_eq!(data, vec![1, 0]);
    }

    #[test]
    fn test_mean_2d_axis0() {
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let m = mean(&a, Some(0)).unwrap();
        let data: Vec<f64> = m.iter().copied().collect();
        assert_eq!(data, vec![2.5, 3.5, 4.5]);
    }

    #[test]
    fn test_sum_integer() {
        let a = Array::<i32, Ix1>::from_vec(Ix1::new([5]), vec![1, 2, 3, 4, 5]).unwrap();
        let s = sum(&a, None).unwrap();
        assert_eq!(s.iter().next(), Some(&15));
    }

    #[test]
    fn test_mean_as_f64_integer() {
        let a = Array::<i32, Ix1>::from_vec(Ix1::new([4]), vec![1, 2, 3, 4]).unwrap();
        let m = mean_as_f64(&a, None).unwrap();
        assert!((m.iter().next().unwrap() - 2.5).abs() < 1e-12);
    }

    #[test]
    fn test_mean_as_f64_integer_axis() {
        let a = Array::<i32, Ix2>::from_vec(Ix2::new([2, 3]), vec![1, 2, 3, 4, 5, 6]).unwrap();
        let m = mean_as_f64(&a, Some(1)).unwrap();
        assert_eq!(m.shape(), &[2]);
        let data: Vec<f64> = m.iter().copied().collect();
        assert!((data[0] - 2.0).abs() < 1e-12);
        assert!((data[1] - 5.0).abs() < 1e-12);
    }

    #[test]
    fn test_mean_as_f64_u8() {
        let a = Array::<u8, Ix1>::from_vec(Ix1::new([4]), vec![10, 20, 30, 40]).unwrap();
        let m = mean_as_f64(&a, None).unwrap();
        assert!((m.iter().next().unwrap() - 25.0).abs() < 1e-12);
    }

    #[test]
    fn test_sum_as_f64_integer() {
        let a = Array::<i32, Ix1>::from_vec(Ix1::new([4]), vec![1, 2, 3, 4]).unwrap();
        let s = sum_as_f64(&a, None).unwrap();
        assert!((s.iter().next().unwrap() - 10.0).abs() < 1e-12);
    }

    #[test]
    fn test_sum_as_f64_large_values() {
        // Values that would overflow i32 if summed as i32
        let a =
            Array::<i32, Ix1>::from_vec(Ix1::new([3]), vec![i32::MAX, i32::MAX, i32::MAX]).unwrap();
        let s = sum_as_f64(&a, None).unwrap();
        let expected = 3.0 * f64::from(i32::MAX);
        assert!((s.iter().next().unwrap() - expected).abs() < 1.0);
    }

    // -----------------------------------------------------------------------
    // Multi-axis + keepdims tests (issues #457 + #458)
    // -----------------------------------------------------------------------

    use ferray_core::Ix3;

    fn arr1d(data: Vec<f64>) -> Array<f64, Ix1> {
        let n = data.len();
        Array::<f64, Ix1>::from_vec(Ix1::new([n]), data).unwrap()
    }

    fn arr2d(rows: usize, cols: usize, data: Vec<f64>) -> Array<f64, Ix2> {
        Array::<f64, Ix2>::from_vec(Ix2::new([rows, cols]), data).unwrap()
    }

    fn arr3d(s0: usize, s1: usize, s2: usize, data: Vec<f64>) -> Array<f64, Ix3> {
        Array::<f64, Ix3>::from_vec(Ix3::new([s0, s1, s2]), data).unwrap()
    }

    #[test]
    fn test_normalize_axes_none_reduces_all() {
        let ax = normalize_axes(None, 3).unwrap();
        assert_eq!(ax, vec![0, 1, 2]);
    }

    #[test]
    fn test_normalize_axes_empty_reduces_all() {
        let ax = normalize_axes(Some(&[]), 3).unwrap();
        assert_eq!(ax, vec![0, 1, 2]);
    }

    #[test]
    fn test_normalize_axes_sorts() {
        let ax = normalize_axes(Some(&[2, 0]), 3).unwrap();
        assert_eq!(ax, vec![0, 2]);
    }

    #[test]
    fn test_normalize_axes_rejects_duplicate() {
        assert!(normalize_axes(Some(&[0, 0]), 3).is_err());
    }

    #[test]
    fn test_normalize_axes_rejects_out_of_bounds() {
        assert!(normalize_axes(Some(&[3]), 3).is_err());
    }

    #[test]
    fn test_sum_axes_single_axis_matches_legacy() {
        let a = arr2d(2, 3, vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
        let legacy = sum(&a, Some(0)).unwrap();
        let new = sum_axes(&a, Some(&[0]), false).unwrap();
        assert_eq!(legacy.shape(), new.shape());
        let la: Vec<f64> = legacy.iter().copied().collect();
        let na: Vec<f64> = new.iter().copied().collect();
        assert_eq!(la, na);
    }

    #[test]
    fn test_sum_axes_keepdims_2d() {
        // sum((2, 3), axis=1, keepdims=True) -> shape (2, 1)
        let a = arr2d(2, 3, vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
        let s = sum_axes(&a, Some(&[1]), true).unwrap();
        assert_eq!(s.shape(), &[2, 1]);
        let data: Vec<f64> = s.iter().copied().collect();
        assert_eq!(data, vec![6.0, 15.0]);
    }

    #[test]
    fn test_sum_axes_keepdims_supports_broadcast_back() {
        // Canonical NumPy pattern: arr - arr.mean(axis=1, keepdims=True)
        // We do it with a 2x3 array.
        let a = arr2d(2, 3, vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
        let m = mean_axes(&a, Some(&[1]), true).unwrap();
        assert_eq!(m.shape(), &[2, 1]);
        // Row 0 mean = 2.0, row 1 mean = 5.0
        let md: Vec<f64> = m.iter().copied().collect();
        assert_eq!(md, vec![2.0, 5.0]);
    }

    #[test]
    fn test_sum_axes_multi_axis_3d() {
        // shape (2, 3, 4), sum over axes (0, 2) -> shape (3,)
        let data: Vec<f64> = (0..24).map(f64::from).collect();
        let a = arr3d(2, 3, 4, data);
        let s = sum_axes(&a, Some(&[0, 2]), false).unwrap();
        assert_eq!(s.shape(), &[3]);
        // For each j in [0,1,2], sum over i in [0,1] and k in [0,1,2,3]:
        //   a[i,j,k] = i*12 + j*4 + k
        //   sum = (0*12 + j*4) + ... + (0*12 + j*4+3) + (1*12 + j*4) + ... + (1*12 + j*4+3)
        //       = 2*(4*j*4 + 6) + 2*12*1
        // For j=0: (0+1+2+3) + (12+13+14+15) = 6 + 54 = 60
        // For j=1: (4+5+6+7) + (16+17+18+19) = 22 + 70 = 92
        // For j=2: (8+9+10+11) + (20+21+22+23) = 38 + 86 = 124
        let d: Vec<f64> = s.iter().copied().collect();
        assert_eq!(d, vec![60.0, 92.0, 124.0]);
    }

    #[test]
    fn test_sum_axes_multi_axis_keepdims_3d() {
        // Same as above but keepdims -> shape (1, 3, 1)
        let data: Vec<f64> = (0..24).map(f64::from).collect();
        let a = arr3d(2, 3, 4, data);
        let s = sum_axes(&a, Some(&[0, 2]), true).unwrap();
        assert_eq!(s.shape(), &[1, 3, 1]);
        let d: Vec<f64> = s.iter().copied().collect();
        assert_eq!(d, vec![60.0, 92.0, 124.0]);
    }

    #[test]
    fn test_sum_axes_none_reduces_all() {
        let a = arr2d(2, 3, vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
        let s = sum_axes(&a, None, false).unwrap();
        assert_eq!(s.shape(), &[]);
        assert_eq!(s.iter().next(), Some(&21.0));
    }

    #[test]
    fn test_sum_axes_none_keepdims() {
        let a = arr2d(2, 3, vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
        let s = sum_axes(&a, None, true).unwrap();
        assert_eq!(s.shape(), &[1, 1]);
        assert_eq!(s.iter().next(), Some(&21.0));
    }

    #[test]
    fn test_prod_axes_keepdims() {
        let a = arr2d(2, 3, vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
        let p = prod_axes(&a, Some(&[1]), true).unwrap();
        assert_eq!(p.shape(), &[2, 1]);
        let d: Vec<f64> = p.iter().copied().collect();
        assert_eq!(d, vec![6.0, 120.0]);
    }

    #[test]
    fn test_min_max_axes_multi_axis() {
        let data: Vec<f64> = (0..24).map(f64::from).collect();
        let a = arr3d(2, 3, 4, data);
        let mn = min_axes(&a, Some(&[0, 2]), false).unwrap();
        let mx = max_axes(&a, Some(&[0, 2]), false).unwrap();
        // min over i=0..2, k=0..4 for each j: min value on i=0 has smallest
        // indices. For j=0, values are 0..3 and 12..15 -> min=0, max=15
        assert_eq!(mn.iter().copied().collect::<Vec<_>>(), vec![0.0, 4.0, 8.0]);
        assert_eq!(
            mx.iter().copied().collect::<Vec<_>>(),
            vec![15.0, 19.0, 23.0]
        );
    }

    #[test]
    fn test_mean_axes_multi_axis() {
        let data: Vec<f64> = (0..24).map(f64::from).collect();
        let a = arr3d(2, 3, 4, data);
        let m = mean_axes(&a, Some(&[0, 2]), false).unwrap();
        assert_eq!(m.shape(), &[3]);
        // For j=0: 60 / 8 = 7.5
        // For j=1: 92 / 8 = 11.5
        // For j=2: 124 / 8 = 15.5
        let d: Vec<f64> = m.iter().copied().collect();
        assert_eq!(d, vec![7.5, 11.5, 15.5]);
    }

    #[test]
    fn test_var_axes_single_axis_keepdims() {
        let a = arr2d(2, 4, vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]);
        // Population variance along axis 1 (each row), keepdims = (2, 1)
        let v = var_axes(&a, Some(&[1]), 0, true).unwrap();
        assert_eq!(v.shape(), &[2, 1]);
        // Row 0: mean=2.5, var=((1-2.5)^2+(2-2.5)^2+(3-2.5)^2+(4-2.5)^2)/4 = 1.25
        // Row 1: same distribution = 1.25
        let d: Vec<f64> = v.iter().copied().collect();
        assert!((d[0] - 1.25).abs() < 1e-12);
        assert!((d[1] - 1.25).abs() < 1e-12);
    }

    #[test]
    fn test_std_axes_multi_axis() {
        let a = arr2d(2, 4, vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]);
        // Population std over all: mean=4.5, var=5.25, std≈2.2913
        let s = std_axes(&a, None, 0, false).unwrap();
        assert_eq!(s.shape(), &[]);
        let v = *s.iter().next().unwrap();
        assert!((v - 5.25_f64.sqrt()).abs() < 1e-12);
    }

    #[test]
    fn test_argmin_keepdims_single_axis() {
        let a = arr2d(2, 3, vec![3.0, 1.0, 4.0, 1.0, 5.0, 2.0]);
        let am = argmin_keepdims(&a, Some(1), true).unwrap();
        assert_eq!(am.shape(), &[2, 1]);
        let d: Vec<u64> = am.iter().copied().collect();
        assert_eq!(d, vec![1, 0]);
    }

    #[test]
    fn test_argmax_keepdims_single_axis() {
        let a = arr2d(2, 3, vec![3.0, 1.0, 4.0, 1.0, 5.0, 2.0]);
        let am = argmax_keepdims(&a, Some(1), false).unwrap();
        assert_eq!(am.shape(), &[2]);
        let d: Vec<u64> = am.iter().copied().collect();
        assert_eq!(d, vec![2, 1]);
    }

    #[test]
    fn test_axes_out_of_bounds_error() {
        let a = arr2d(2, 3, vec![0.0; 6]);
        assert!(sum_axes(&a, Some(&[5]), false).is_err());
    }

    #[test]
    fn test_axes_duplicate_error() {
        let a = arr3d(2, 3, 4, vec![0.0; 24]);
        assert!(sum_axes(&a, Some(&[0, 0]), false).is_err());
    }

    // ----- Infinity tests (#177) -----

    #[test]
    fn sum_with_infinity() {
        let a = arr1d(vec![1.0, f64::INFINITY, 3.0]);
        let s = sum(&a, None).unwrap();
        assert_eq!(s.iter().next().copied().unwrap(), f64::INFINITY);
    }

    #[test]
    fn sum_inf_minus_inf_is_nan() {
        let a = arr1d(vec![f64::INFINITY, f64::NEG_INFINITY]);
        let s = sum(&a, None).unwrap();
        assert!(s.iter().next().copied().unwrap().is_nan());
    }

    #[test]
    fn mean_with_infinity() {
        let a = arr1d(vec![1.0, f64::INFINITY, 3.0]);
        let m = mean(&a, None).unwrap();
        assert_eq!(m.iter().next().copied().unwrap(), f64::INFINITY);
    }

    #[test]
    fn min_with_neg_infinity() {
        let a = arr1d(vec![1.0, f64::NEG_INFINITY, 3.0]);
        let m = min(&a, None).unwrap();
        assert_eq!(m.iter().next().copied().unwrap(), f64::NEG_INFINITY);
    }

    #[test]
    fn max_with_infinity() {
        let a = arr1d(vec![1.0, f64::INFINITY, 3.0]);
        let m = max(&a, None).unwrap();
        assert_eq!(m.iter().next().copied().unwrap(), f64::INFINITY);
    }

    // ----- Single-element var/std with ddof (#178) -----

    #[test]
    fn var_single_element_ddof0() {
        let a = arr1d(vec![5.0]);
        let v = var(&a, None, 0).unwrap();
        assert_eq!(v.iter().next().copied().unwrap(), 0.0);
    }

    #[test]
    fn var_single_element_ddof1_errors() {
        // ddof=1 with N=1 → ddof >= N, ferray errors instead of
        // returning NaN (stricter than NumPy which returns NaN).
        let a = arr1d(vec![5.0]);
        assert!(var(&a, None, 1).is_err());
    }

    #[test]
    fn std_single_element_ddof0() {
        let a = arr1d(vec![5.0]);
        let s = std_(&a, None, 0).unwrap();
        assert_eq!(s.iter().next().copied().unwrap(), 0.0);
    }

    // ---- *_into reductions (#467) ----

    #[test]
    fn sum_into_axis_writes_into_destination() {
        // (2, 3) sum along axis=1 → shape (2,)
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let mut out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[2]), vec![0.0; 2]).unwrap();
        sum_into(&a, Some(1), &mut out).unwrap();
        assert_eq!(out.as_slice().unwrap(), &[6.0, 15.0]);
    }

    #[test]
    fn sum_into_no_axis_writes_scalar_destination() {
        let a = arr1d(vec![1.0, 2.0, 3.0, 4.0]);
        let mut out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[]), vec![0.0]).unwrap();
        sum_into(&a, None, &mut out).unwrap();
        assert_eq!(out.iter().next().copied().unwrap(), 10.0);
    }

    #[test]
    fn sum_into_rejects_wrong_shape() {
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        // axis=1 reduces to shape (2,), but out has shape (3,)
        let mut out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[3]), vec![0.0; 3]).unwrap();
        let err = sum_into(&a, Some(1), &mut out);
        assert!(err.is_err());
    }

    #[test]
    fn sum_into_rejects_axis_out_of_bounds() {
        let a = arr1d(vec![1.0, 2.0, 3.0]);
        let mut out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[1]), vec![0.0]).unwrap();
        assert!(sum_into(&a, Some(5), &mut out).is_err());
    }

    #[test]
    fn prod_into_basic() {
        let a = arr1d(vec![1.0, 2.0, 3.0, 4.0]);
        let mut out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[]), vec![0.0]).unwrap();
        prod_into(&a, None, &mut out).unwrap();
        assert_eq!(out.iter().next().copied().unwrap(), 24.0);
    }

    #[test]
    fn min_into_axis_basic() {
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 5.0, 2.0, 4.0, 3.0, 6.0])
            .unwrap();
        let mut out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[2]), vec![0.0; 2]).unwrap();
        min_into(&a, Some(1), &mut out).unwrap();
        assert_eq!(out.as_slice().unwrap(), &[1.0, 3.0]);
    }

    #[test]
    fn max_into_axis_basic() {
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 5.0, 2.0, 4.0, 3.0, 6.0])
            .unwrap();
        let mut out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[2]), vec![0.0; 2]).unwrap();
        max_into(&a, Some(1), &mut out).unwrap();
        assert_eq!(out.as_slice().unwrap(), &[5.0, 6.0]);
    }

    #[test]
    fn mean_into_axis_basic() {
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let mut out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[2]), vec![0.0; 2]).unwrap();
        mean_into(&a, Some(1), &mut out).unwrap();
        assert_eq!(out.as_slice().unwrap(), &[2.0, 5.0]);
    }

    #[test]
    fn var_into_basic() {
        // Variance with ddof=0 of [1,2,3,4] = 1.25
        let a = arr1d(vec![1.0, 2.0, 3.0, 4.0]);
        let mut out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[]), vec![0.0]).unwrap();
        var_into(&a, None, 0, &mut out).unwrap();
        assert!((out.iter().next().copied().unwrap() - 1.25).abs() < 1e-12);
    }

    #[test]
    fn std_into_basic() {
        // sqrt(var) for [1,2,3,4] with ddof=0 = sqrt(1.25)
        let a = arr1d(vec![1.0, 2.0, 3.0, 4.0]);
        let mut out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[]), vec![0.0]).unwrap();
        std_into(&a, None, 0, &mut out).unwrap();
        let expected = 1.25_f64.sqrt();
        assert!((out.iter().next().copied().unwrap() - expected).abs() < 1e-12);
    }

    #[test]
    fn into_reductions_can_reuse_destination_across_calls() {
        // The whole point of the out= API: a single allocation reused
        // across many reductions on same-shaped data.
        let mut out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[2]), vec![0.0; 2]).unwrap();
        for k in 0..3 {
            let base = f64::from(k);
            let a = Array::<f64, Ix2>::from_vec(
                Ix2::new([2, 3]),
                vec![
                    base + 1.0,
                    base + 2.0,
                    base + 3.0,
                    base + 4.0,
                    base + 5.0,
                    base + 6.0,
                ],
            )
            .unwrap();
            sum_into(&a, Some(1), &mut out).unwrap();
            assert_eq!(
                out.as_slice().unwrap(),
                &[3.0f64.mul_add(base, 6.0), 3.0f64.mul_add(base, 15.0)]
            );
        }
    }

    // ---- zero-alloc kernel regression tests (#563) ----

    #[test]
    fn sum_into_overwrites_existing_destination_garbage() {
        // The new kernel writes results directly into `out` rather than
        // routing through write_into's copy_from_slice; make sure existing
        // garbage is fully overwritten and never bleeds through.
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let mut out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[2]), vec![999.0, -999.0]).unwrap();
        sum_into(&a, Some(1), &mut out).unwrap();
        assert_eq!(out.as_slice().unwrap(), &[6.0, 15.0]);
    }

    #[test]
    fn reduce_axis_typed_into_matches_reduce_axis_typed() {
        // The in-place kernel must produce identical output to the
        // allocating kernel for any (shape, axis, fn).
        use super::{reduce_axis_typed, reduce_axis_typed_into};
        let data: Vec<f64> = (0..24).map(f64::from).collect();
        for shape in [vec![24usize], vec![4, 6], vec![2, 3, 4], vec![2, 2, 2, 3]] {
            for ax in 0..shape.len() {
                let allocated: Vec<f64> =
                    reduce_axis_typed(&data, &shape, ax, |lane| lane.iter().sum());
                let mut dst = vec![0.0; allocated.len()];
                reduce_axis_typed_into(&data, &shape, ax, &mut dst, |lane| lane.iter().sum());
                assert_eq!(dst, allocated, "shape {shape:?} axis {ax}");
            }
        }
    }

    #[test]
    fn sum_into_3d_axis_correct() {
        use ferray_core::Ix3;
        // (2, 3, 4) reducing axis 1 → shape (2, 4)
        let data: Vec<f64> = (0..24).map(f64::from).collect();
        let a = Array::<f64, Ix3>::from_vec(Ix3::new([2, 3, 4]), data).unwrap();
        let mut out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[2, 4]), vec![0.0; 8]).unwrap();
        sum_into(&a, Some(1), &mut out).unwrap();
        // Hand check: out[i, k] = sum_{j} a[i, j, k] = sum_{j} (i*12 + j*4 + k)
        let expected: Vec<f64> = (0..2)
            .flat_map(|i| {
                (0..4).map(move |k| (0..3).map(|j| f64::from(i * 12 + j * 4 + k)).sum::<f64>())
            })
            .collect();
        assert_eq!(out.as_slice().unwrap(), expected.as_slice());
    }

    #[test]
    fn min_into_rejects_empty_input() {
        let a = Array::<f64, Ix1>::from_vec(Ix1::new([0]), vec![]).unwrap();
        let mut out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[]), vec![0.0]).unwrap();
        assert!(min_into(&a, None, &mut out).is_err());
    }

    #[test]
    fn max_into_rejects_empty_input() {
        let a = Array::<f64, Ix1>::from_vec(Ix1::new([0]), vec![]).unwrap();
        let mut out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[]), vec![0.0]).unwrap();
        assert!(max_into(&a, None, &mut out).is_err());
    }

    #[test]
    fn var_into_rejects_ddof_too_large() {
        let a = arr1d(vec![1.0, 2.0, 3.0]);
        let mut out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[]), vec![0.0]).unwrap();
        // n=3, ddof=3 → ddof >= n, must error.
        assert!(var_into(&a, None, 3, &mut out).is_err());
    }

    #[test]
    fn std_into_does_not_leave_variance_in_destination_on_success() {
        // var of [1,2,3,4] with ddof=0 = 1.25; std = sqrt(1.25) ≈ 1.118.
        // The destination must hold the std value, not the variance, after
        // a successful call.
        let a = arr1d(vec![1.0, 2.0, 3.0, 4.0]);
        let mut out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[]), vec![0.0]).unwrap();
        std_into(&a, None, 0, &mut out).unwrap();
        let got = out.iter().next().copied().unwrap();
        let expected = 1.25_f64.sqrt();
        assert!((got - expected).abs() < 1e-12);
        // Sanity: result is not the variance.
        assert!((got - 1.25).abs() > 1e-3);
    }

    // ---- *_with reductions: initial= and where= (#459) ----

    #[test]
    fn sum_with_initial_only() {
        // Initial = 100; sum becomes 100 + 1 + 2 + 3 + 4 = 110
        let a = arr1d(vec![1.0, 2.0, 3.0, 4.0]);
        let r = sum_with(&a, None, Some(100.0), None).unwrap();
        assert_eq!(r.iter().next().copied().unwrap(), 110.0);
    }

    #[test]
    fn sum_with_where_mask_only() {
        // Sum positives only.
        let a = arr1d(vec![1.0, -2.0, 3.0, -4.0, 5.0]);
        let mask =
            Array::<bool, Ix1>::from_vec(Ix1::new([5]), vec![true, false, true, false, true])
                .unwrap();
        let r = sum_with(&a, None, None, Some(&mask.to_dyn())).unwrap();
        assert_eq!(r.iter().next().copied().unwrap(), 9.0);
    }

    #[test]
    fn sum_with_initial_and_mask_combined() {
        let a = arr1d(vec![1.0, 2.0, 3.0, 4.0]);
        let mask =
            Array::<bool, Ix1>::from_vec(Ix1::new([4]), vec![true, false, true, false]).unwrap();
        let r = sum_with(&a, None, Some(50.0), Some(&mask.to_dyn())).unwrap();
        // 50 + 1 + 3 = 54
        assert_eq!(r.iter().next().copied().unwrap(), 54.0);
    }

    #[test]
    fn sum_with_axis_and_mask() {
        // (2, 3); mask zeroes out the first column; row sums become
        // [2+3, 5+6] = [5, 11].
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let mask = Array::<bool, Ix2>::from_vec(
            Ix2::new([2, 3]),
            vec![false, true, true, false, true, true],
        )
        .unwrap();
        let r = sum_with(&a, Some(1), None, Some(&mask.to_dyn())).unwrap();
        assert_eq!(r.shape(), &[2]);
        assert_eq!(r.as_slice().unwrap(), &[5.0, 11.0]);
    }

    #[test]
    fn sum_with_axis_and_initial() {
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        // axis=1 with initial=10 → row sums become [10+6, 10+15] = [16, 25]
        let r = sum_with(&a, Some(1), Some(10.0), None).unwrap();
        assert_eq!(r.as_slice().unwrap(), &[16.0, 25.0]);
    }

    #[test]
    fn sum_with_no_initial_no_mask_matches_legacy_sum() {
        // Sanity: omitting both knobs should match the existing sum().
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let legacy = sum(&a, Some(1)).unwrap();
        let with_form = sum_with(&a, Some(1), None, None).unwrap();
        assert_eq!(legacy.as_slice().unwrap(), with_form.as_slice().unwrap());
    }

    #[test]
    fn sum_with_rejects_mismatched_mask_shape() {
        let a = arr1d(vec![1.0, 2.0, 3.0]);
        let mask = Array::<bool, Ix1>::from_vec(Ix1::new([4]), vec![true; 4]).unwrap();
        assert!(sum_with(&a, None, None, Some(&mask.to_dyn())).is_err());
    }

    #[test]
    fn prod_with_initial_only() {
        let a = arr1d(vec![2.0, 3.0, 4.0]);
        let r = prod_with(&a, None, Some(10.0), None).unwrap();
        // 10 * 2 * 3 * 4 = 240
        assert_eq!(r.iter().next().copied().unwrap(), 240.0);
    }

    #[test]
    fn prod_with_where_mask() {
        let a = arr1d(vec![2.0, 0.0, 3.0, 0.0, 4.0]);
        let mask =
            Array::<bool, Ix1>::from_vec(Ix1::new([5]), vec![true, false, true, false, true])
                .unwrap();
        let r = prod_with(&a, None, None, Some(&mask.to_dyn())).unwrap();
        // 2 * 3 * 4 = 24 (zero positions skipped)
        assert_eq!(r.iter().next().copied().unwrap(), 24.0);
    }

    #[test]
    fn min_with_initial_provides_fallback_for_empty_lane() {
        // Empty array + initial = the initial value.
        let a = Array::<f64, Ix1>::from_vec(Ix1::new([0]), vec![]).unwrap();
        let r = min_with(&a, None, Some(99.0), None).unwrap();
        assert_eq!(r.iter().next().copied().unwrap(), 99.0);
    }

    #[test]
    fn min_with_initial_caps_actual_min() {
        // Initial=0, data=[1,2,3] → min(0, 1, 2, 3) = 0.
        let a = arr1d(vec![1.0, 2.0, 3.0]);
        let r = min_with(&a, None, Some(0.0), None).unwrap();
        assert_eq!(r.iter().next().copied().unwrap(), 0.0);
    }

    #[test]
    fn min_with_where_mask_filters_then_reduces() {
        let a = arr1d(vec![5.0, 1.0, 4.0, 2.0, 3.0]);
        // Mask out positions 1 and 3 (values 1 and 2).
        let mask =
            Array::<bool, Ix1>::from_vec(Ix1::new([5]), vec![true, false, true, false, true])
                .unwrap();
        let r = min_with(&a, None, None, Some(&mask.to_dyn())).unwrap();
        // Filtered: [5, 4, 3] → min = 3
        assert_eq!(r.iter().next().copied().unwrap(), 3.0);
    }

    #[test]
    fn min_with_empty_lane_no_initial_errors() {
        let a = Array::<f64, Ix1>::from_vec(Ix1::new([0]), vec![]).unwrap();
        assert!(min_with(&a, None, None, None).is_err());
    }

    #[test]
    fn min_with_fully_masked_axis_lane_no_initial_errors() {
        // Each row fully masked out → error per lane.
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let mask = Array::<bool, Ix2>::from_vec(Ix2::new([2, 3]), vec![false; 6]).unwrap();
        assert!(min_with(&a, Some(1), None, Some(&mask.to_dyn())).is_err());
    }

    #[test]
    fn min_with_fully_masked_axis_lane_with_initial_uses_initial() {
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let mask = Array::<bool, Ix2>::from_vec(Ix2::new([2, 3]), vec![false; 6]).unwrap();
        let r = min_with(&a, Some(1), Some(99.0), Some(&mask.to_dyn())).unwrap();
        assert_eq!(r.as_slice().unwrap(), &[99.0, 99.0]);
    }

    #[test]
    fn max_with_initial_caps_actual_max() {
        let a = arr1d(vec![1.0, 2.0, 3.0]);
        let r = max_with(&a, None, Some(99.0), None).unwrap();
        assert_eq!(r.iter().next().copied().unwrap(), 99.0);
    }

    #[test]
    fn max_with_where_mask_filters_then_reduces() {
        let a = arr1d(vec![5.0, 10.0, 4.0, 20.0, 3.0]);
        // Mask out positions 1 and 3 (values 10 and 20).
        let mask =
            Array::<bool, Ix1>::from_vec(Ix1::new([5]), vec![true, false, true, false, true])
                .unwrap();
        let r = max_with(&a, None, None, Some(&mask.to_dyn())).unwrap();
        // Filtered: [5, 4, 3] → max = 5
        assert_eq!(r.iter().next().copied().unwrap(), 5.0);
    }

    #[test]
    fn mean_where_filters_and_divides_by_count() {
        // [1, 2, 3, 4, 5] with mask [T, F, T, F, T] → (1+3+5)/3 = 3.0
        let a = arr1d(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
        let mask =
            Array::<bool, Ix1>::from_vec(Ix1::new([5]), vec![true, false, true, false, true])
                .unwrap();
        let r = mean_where(&a, None, Some(&mask.to_dyn())).unwrap();
        assert!((r.iter().next().copied().unwrap() - 3.0).abs() < 1e-12);
    }

    #[test]
    fn mean_where_no_mask_matches_legacy_mean() {
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let legacy = mean(&a, Some(1)).unwrap();
        let where_form = mean_where(&a, Some(1), None).unwrap();
        assert_eq!(legacy.as_slice().unwrap(), where_form.as_slice().unwrap());
    }

    #[test]
    fn mean_where_fully_masked_lane_returns_nan() {
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let mask = Array::<bool, Ix2>::from_vec(
            Ix2::new([2, 3]),
            vec![true, true, true, false, false, false],
        )
        .unwrap();
        let r = mean_where(&a, Some(1), Some(&mask.to_dyn())).unwrap();
        let s = r.as_slice().unwrap();
        assert!((s[0] - 2.0).abs() < 1e-12);
        assert!(s[1].is_nan());
    }

    #[test]
    fn mean_where_axis_with_partial_mask() {
        // (2, 3); mask = [[T,T,F],[F,T,T]]
        // Row 0 mean: (1+2)/2 = 1.5
        // Row 1 mean: (5+6)/2 = 5.5
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let mask = Array::<bool, Ix2>::from_vec(
            Ix2::new([2, 3]),
            vec![true, true, false, false, true, true],
        )
        .unwrap();
        let r = mean_where(&a, Some(1), Some(&mask.to_dyn())).unwrap();
        assert_eq!(r.shape(), &[2]);
        let s = r.as_slice().unwrap();
        assert!((s[0] - 1.5).abs() < 1e-12);
        assert!((s[1] - 5.5).abs() < 1e-12);
    }

    // ---- broadcast-compatible where masks (#565) ----

    #[test]
    fn sum_with_mask_broadcasts_ix1_into_ix2() {
        // (2, 3) input; mask of shape (3,) — broadcasts to (2, 3) with
        // every row identical. sum ignores the first column, so both
        // row sums skip that column's contribution.
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let mask_1d = Array::<bool, Ix1>::from_vec(Ix1::new([3]), vec![false, true, true]).unwrap();
        let r = sum_with(&a, None, None, Some(&mask_1d.to_dyn())).unwrap();
        // Sum with first column masked out: 2 + 3 + 5 + 6 = 16.0
        assert!((r.iter().next().copied().unwrap() - 16.0).abs() < 1e-12);
    }

    #[test]
    fn sum_with_mask_broadcasts_column_vector_across_rows() {
        // (3, 4) input; mask of shape (3, 1) — broadcasts across the
        // four columns, so each row is either fully kept or fully
        // masked out.
        let a = Array::<f64, Ix2>::from_vec(
            Ix2::new([3, 4]),
            vec![
                1.0, 2.0, 3.0, 4.0, // row 0
                5.0, 6.0, 7.0, 8.0, // row 1
                9.0, 10.0, 11.0, 12.0, // row 2
            ],
        )
        .unwrap();
        let mask_col =
            Array::<bool, Ix2>::from_vec(Ix2::new([3, 1]), vec![true, false, true]).unwrap();
        let r = sum_with(&a, None, None, Some(&mask_col.to_dyn())).unwrap();
        // Rows 0 and 2 kept: 1+2+3+4 + 9+10+11+12 = 10 + 42 = 52
        assert!((r.iter().next().copied().unwrap() - 52.0).abs() < 1e-12);
    }

    #[test]
    fn sum_with_mask_broadcasts_row_vector_against_axis_reduction() {
        // (2, 3) reducing axis=1; mask shape (3,) broadcasts against
        // each row.
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let mask_1d = Array::<bool, Ix1>::from_vec(Ix1::new([3]), vec![true, false, true]).unwrap();
        let r = sum_with(&a, Some(1), None, Some(&mask_1d.to_dyn())).unwrap();
        assert_eq!(r.shape(), &[2]);
        // Row 0: 1 + 3 = 4; row 1: 4 + 6 = 10
        assert_eq!(r.as_slice().unwrap(), &[4.0, 10.0]);
    }

    #[test]
    fn prod_with_mask_broadcasts_ix1() {
        // (2, 3); mask shape (3,) keeps columns 0 and 2.
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
            .unwrap();
        let mask = Array::<bool, Ix1>::from_vec(Ix1::new([3]), vec![true, false, true]).unwrap();
        let r = prod_with(&a, None, None, Some(&mask.to_dyn())).unwrap();
        // Product of kept values: 2 * 4 * 5 * 7 = 280
        assert!((r.iter().next().copied().unwrap() - 280.0).abs() < 1e-12);
    }

    #[test]
    fn min_with_mask_broadcasts_ix1() {
        // Column mask; find min across the kept columns.
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![5.0, 1.0, 4.0, 2.0, 10.0, 3.0])
            .unwrap();
        let mask = Array::<bool, Ix1>::from_vec(Ix1::new([3]), vec![true, false, true]).unwrap();
        let r = min_with(&a, None, None, Some(&mask.to_dyn())).unwrap();
        // Kept values: 5, 4, 2, 3 → min = 2
        assert!((r.iter().next().copied().unwrap() - 2.0).abs() < 1e-12);
    }

    #[test]
    fn max_with_mask_broadcasts_ix1() {
        let a =
            Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![5.0, 100.0, 4.0, 2.0, 200.0, 3.0])
                .unwrap();
        let mask = Array::<bool, Ix1>::from_vec(Ix1::new([3]), vec![true, false, true]).unwrap();
        let r = max_with(&a, None, None, Some(&mask.to_dyn())).unwrap();
        // Kept values: 5, 4, 2, 3 → max = 5
        assert!((r.iter().next().copied().unwrap() - 5.0).abs() < 1e-12);
    }

    #[test]
    fn mean_where_mask_broadcasts_ix1() {
        // (2, 3); mask (3,) keeps columns 0 and 2. Mean of 4 kept
        // values: (1 + 3 + 4 + 6) / 4 = 3.5
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let mask = Array::<bool, Ix1>::from_vec(Ix1::new([3]), vec![true, false, true]).unwrap();
        let r = mean_where(&a, None, Some(&mask.to_dyn())).unwrap();
        assert!((r.iter().next().copied().unwrap() - 3.5).abs() < 1e-12);
    }

    #[test]
    fn with_mask_rejects_incompatible_broadcast_shape() {
        // Mask rank compatible but length wrong: shape (2,) against a
        // (2, 3) input cannot broadcast (the 2 aligns with the last
        // dim which is 3, not the first which is 2).
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let bad_mask = Array::<bool, Ix1>::from_vec(Ix1::new([2]), vec![true, false]).unwrap();
        assert!(sum_with(&a, None, None, Some(&bad_mask.to_dyn())).is_err());
    }

    #[test]
    fn sum_with_mask_broadcast_scalar_like_length_1() {
        // A shape-(1,) mask is the scalar case — broadcasts to every
        // position of the input, which for `true` is an identity and
        // for `false` yields 0 (everything masked out).
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let true_mask = Array::<bool, Ix1>::from_vec(Ix1::new([1]), vec![true]).unwrap();
        let r = sum_with(&a, None, None, Some(&true_mask.to_dyn())).unwrap();
        // All positions kept: 1+2+3+4+5+6 = 21
        assert!((r.iter().next().copied().unwrap() - 21.0).abs() < 1e-12);

        let false_mask = Array::<bool, Ix1>::from_vec(Ix1::new([1]), vec![false]).unwrap();
        let r2 = sum_with(&a, None, Some(100.0), Some(&false_mask.to_dyn())).unwrap();
        // Nothing kept, initial = 100 → result = 100
        assert!((r2.iter().next().copied().unwrap() - 100.0).abs() < 1e-12);
    }

    #[test]
    fn into_reductions_match_allocating_versions_3d_axis_2() {
        // Cross-check every *_into against its allocating sibling on a
        // 3-D input, axis = last dim — guards against an off-by-one in
        // the in-place index walker.
        use ferray_core::Ix3;
        let data: Vec<f64> = (0..24).map(|i| f64::from(i) + 0.5).collect();
        let a = Array::<f64, Ix3>::from_vec(Ix3::new([2, 3, 4]), data).unwrap();

        let s_alloc = sum(&a, Some(2)).unwrap();
        let mut s_out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[2, 3]), vec![0.0; 6]).unwrap();
        sum_into(&a, Some(2), &mut s_out).unwrap();
        assert_eq!(s_alloc.as_slice().unwrap(), s_out.as_slice().unwrap());

        let p_alloc = prod(&a, Some(2)).unwrap();
        let mut p_out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[2, 3]), vec![0.0; 6]).unwrap();
        prod_into(&a, Some(2), &mut p_out).unwrap();
        assert_eq!(p_alloc.as_slice().unwrap(), p_out.as_slice().unwrap());

        let mn_alloc = min(&a, Some(2)).unwrap();
        let mut mn_out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[2, 3]), vec![0.0; 6]).unwrap();
        min_into(&a, Some(2), &mut mn_out).unwrap();
        assert_eq!(mn_alloc.as_slice().unwrap(), mn_out.as_slice().unwrap());

        let mx_alloc = max(&a, Some(2)).unwrap();
        let mut mx_out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[2, 3]), vec![0.0; 6]).unwrap();
        max_into(&a, Some(2), &mut mx_out).unwrap();
        assert_eq!(mx_alloc.as_slice().unwrap(), mx_out.as_slice().unwrap());

        let me_alloc = mean(&a, Some(2)).unwrap();
        let mut me_out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[2, 3]), vec![0.0; 6]).unwrap();
        mean_into(&a, Some(2), &mut me_out).unwrap();
        assert_eq!(me_alloc.as_slice().unwrap(), me_out.as_slice().unwrap());

        let v_alloc = var(&a, Some(2), 0).unwrap();
        let mut v_out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[2, 3]), vec![0.0; 6]).unwrap();
        var_into(&a, Some(2), 0, &mut v_out).unwrap();
        // var output may have small numerical drift between the two-pass
        // and one-shot kernels — compare element-wise within tolerance.
        for (a, b) in v_alloc
            .as_slice()
            .unwrap()
            .iter()
            .zip(v_out.as_slice().unwrap())
        {
            assert!((a - b).abs() < 1e-10);
        }

        let sd_alloc = std_(&a, Some(2), 0).unwrap();
        let mut sd_out = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[2, 3]), vec![0.0; 6]).unwrap();
        std_into(&a, Some(2), 0, &mut sd_out).unwrap();
        for (a, b) in sd_alloc
            .as_slice()
            .unwrap()
            .iter()
            .zip(sd_out.as_slice().unwrap())
        {
            assert!((a - b).abs() < 1e-10);
        }
    }

    // -- ptp --

    #[test]
    fn test_ptp_1d() {
        let a = Array::<f64, Ix1>::from_vec(Ix1::new([5]), vec![1.0, 5.0, 3.0, 9.0, 2.0]).unwrap();
        let r = ptp(&a, None).unwrap();
        assert_eq!(r.iter().copied().next().unwrap(), 8.0);
    }

    #[test]
    fn test_ptp_2d_axis() {
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 5.0, 3.0, 7.0, 2.0, 9.0])
            .unwrap();
        let r = ptp(&a, Some(1)).unwrap();
        // row 0: max=5, min=1 → 4; row 1: max=9, min=2 → 7
        assert_eq!(r.iter().copied().collect::<Vec<_>>(), vec![4.0, 7.0]);
    }

    #[test]
    fn test_ptp_empty_errs() {
        let a: Array<f64, Ix1> = Array::from_vec(Ix1::new([0]), vec![]).unwrap();
        assert!(ptp(&a, None).is_err());
    }

    // -- average --

    #[test]
    fn test_average_unweighted_matches_mean() {
        let a = Array::<f64, Ix1>::from_vec(Ix1::new([4]), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
        let r = average(&a, None, None).unwrap();
        assert!((r.iter().copied().next().unwrap() - 2.5).abs() < 1e-12);
    }

    #[test]
    fn test_average_weighted() {
        let a = Array::<f64, Ix1>::from_vec(Ix1::new([4]), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
        let w = Array::<f64, Ix1>::from_vec(Ix1::new([4]), vec![1.0, 1.0, 1.0, 7.0]).unwrap();
        // (1+2+3+4*7) / (1+1+1+7) = 34/10 = 3.4
        let r = average(&a, Some(&w), None).unwrap();
        assert!((r.iter().copied().next().unwrap() - 3.4).abs() < 1e-12);
    }

    #[test]
    fn test_average_weighted_axis() {
        let a = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .unwrap();
        let w = Array::<f64, Ix2>::from_vec(Ix2::new([2, 3]), vec![1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
            .unwrap();
        // With uniform weights, axis-1 average = row mean = [2, 5]
        let r = average(&a, Some(&w), Some(1)).unwrap();
        let data: Vec<f64> = r.iter().copied().collect();
        assert!((data[0] - 2.0).abs() < 1e-12);
        assert!((data[1] - 5.0).abs() < 1e-12);
    }

    #[test]
    fn test_average_weights_zero_sum_errs() {
        let a = Array::<f64, Ix1>::from_vec(Ix1::new([3]), vec![1.0, 2.0, 3.0]).unwrap();
        let w = Array::<f64, Ix1>::from_vec(Ix1::new([3]), vec![0.0, 0.0, 0.0]).unwrap();
        assert!(average(&a, Some(&w), None).is_err());
    }

    #[test]
    fn test_average_shape_mismatch_errs() {
        let a = Array::<f64, Ix1>::from_vec(Ix1::new([3]), vec![1.0, 2.0, 3.0]).unwrap();
        let w = Array::<f64, Ix1>::from_vec(Ix1::new([4]), vec![1.0; 4]).unwrap();
        assert!(average(&a, Some(&w), None).is_err());
    }
}