rustorch 0.6.29

Production-ready PyTorch-compatible deep learning library in Rust with special mathematical functions (gamma, Bessel, error functions), statistical distributions, Fourier transforms (FFT/RFFT), matrix decomposition (SVD/QR/LU/eigenvalue), automatic differentiation, neural networks, computer vision transforms, complete GPU acceleration (CUDA/Metal/OpenCL), SIMD optimizations, parallel processing, WebAssembly browser support, comprehensive distributed learning support, and performance validation
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
//! F32Tensor - コア実装
//! F32Tensor - Core implementation

use crate::error::{RusTorchError, RusTorchResult};
use crate::hybrid_f32_experimental;
use ndarray::{Array, IxDyn};
use std::ops::{Index, IndexMut};
use std::sync::Arc;

/// 2次元インデックス
/// 2D index
#[derive(Debug, Clone, Copy)]
pub struct Index2D(pub usize, pub usize);

/// 3次元インデックス
/// 3D index
#[derive(Debug, Clone, Copy)]
pub struct Index3D(pub usize, pub usize, pub usize);

/// デバイス最適化状態
/// Device optimization state
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum DeviceState {
    CPU,
    Metal { device_id: usize },
    CoreML { device_id: usize },
    Synchronized, // 全デバイス同期済み
}

/// Metal共有バッファ(プレースホルダー)
/// Metal shared buffer (placeholder)
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct MetalBuffer {
    _device_id: usize,
    _size: usize,
}

impl MetalBuffer {
    pub fn new(device_id: usize, size: usize) -> Self {
        Self {
            _device_id: device_id,
            _size: size,
        }
    }
}

/// CoreML共有バッファ(プレースホルダー)
/// CoreML shared buffer (placeholder)
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CoreMLBuffer {
    _device_id: usize,
    _shape: Vec<usize>,
}

impl CoreMLBuffer {
    pub fn new(device_id: usize, shape: Vec<usize>) -> Self {
        Self {
            _device_id: device_id,
            _shape: shape,
        }
    }
}

/// f32専用テンソル(変換コスト最小化)
/// f32-specific tensor (conversion cost minimization)
#[derive(Debug)]
pub struct F32Tensor {
    /// CPU側データ
    /// CPU-side data
    pub data: Array<f32, IxDyn>,

    /// GPU共有バッファ(Metal用)
    /// GPU shared buffer (for Metal)
    pub metal_buffer: Option<Arc<MetalBuffer>>,

    /// Neural Engine共有バッファ(CoreML用)
    /// Neural Engine shared buffer (for CoreML)
    pub coreml_buffer: Option<Arc<CoreMLBuffer>>,

    /// デバイス最適化状態
    /// Device optimization state
    pub device_state: DeviceState,

    /// 勾配追跡
    /// Gradient tracking
    pub requires_grad: bool,

    /// テンソル形状
    /// Tensor shape
    shape: Vec<usize>,
}

impl Clone for F32Tensor {
    fn clone(&self) -> Self {
        Self {
            data: self.data.clone(),
            metal_buffer: self.metal_buffer.clone(),
            coreml_buffer: self.coreml_buffer.clone(),
            device_state: self.device_state.clone(),
            requires_grad: self.requires_grad,
            shape: self.shape.clone(),
        }
    }
}

// PyTorchライクな演算子オーバーロード
// PyTorch-like operator overloading

use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

/// Addition operator: tensor + tensor
impl Add<F32Tensor> for F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn add(self, rhs: F32Tensor) -> Self::Output {
        (&self).add(&rhs)
    }
}

/// Addition operator: tensor + &tensor
impl Add<&F32Tensor> for F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn add(self, rhs: &F32Tensor) -> Self::Output {
        (&self).add(rhs)
    }
}

/// Addition operator: &tensor + &tensor
impl Add for &F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn add(self, rhs: &F32Tensor) -> Self::Output {
        self.add(rhs)
    }
}

/// Addition operator: tensor + scalar
impl Add<f32> for F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn add(self, rhs: f32) -> Self::Output {
        let scalar_tensor = F32Tensor::from_scalar(rhs)?;
        self.add(&scalar_tensor)
    }
}

/// Addition operator: &tensor + scalar
impl Add<f32> for &F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn add(self, rhs: f32) -> Self::Output {
        let scalar_tensor = F32Tensor::from_scalar(rhs)?;
        self.add(&scalar_tensor)
    }
}

/// Subtraction operator: tensor - tensor
impl Sub<F32Tensor> for F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn sub(self, rhs: F32Tensor) -> Self::Output {
        (&self).sub(&rhs)
    }
}

/// Subtraction operator: tensor - &tensor
impl Sub<&F32Tensor> for F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn sub(self, rhs: &F32Tensor) -> Self::Output {
        (&self).sub(rhs)
    }
}

/// Subtraction operator: &tensor - &tensor
impl Sub for &F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn sub(self, rhs: &F32Tensor) -> Self::Output {
        self.sub(rhs)
    }
}

/// Subtraction operator: tensor - scalar
impl Sub<f32> for F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn sub(self, rhs: f32) -> Self::Output {
        let scalar_tensor = F32Tensor::from_scalar(rhs)?;
        self.sub(&scalar_tensor)
    }
}

/// Subtraction operator: &tensor - scalar
impl Sub<f32> for &F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn sub(self, rhs: f32) -> Self::Output {
        let scalar_tensor = F32Tensor::from_scalar(rhs)?;
        self.sub(&scalar_tensor)
    }
}

/// Multiplication operator: tensor * tensor
impl Mul<F32Tensor> for F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn mul(self, rhs: F32Tensor) -> Self::Output {
        (&self).mul(&rhs)
    }
}

/// Multiplication operator: tensor * &tensor
impl Mul<&F32Tensor> for F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn mul(self, rhs: &F32Tensor) -> Self::Output {
        (&self).mul(rhs)
    }
}

/// Multiplication operator: &tensor * &tensor
impl Mul for &F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn mul(self, rhs: &F32Tensor) -> Self::Output {
        self.mul(rhs)
    }
}

/// Multiplication operator: tensor * scalar
impl Mul<f32> for F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn mul(self, rhs: f32) -> Self::Output {
        self.mul_scalar(rhs)
    }
}

/// Multiplication operator: &tensor * scalar
impl Mul<f32> for &F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn mul(self, rhs: f32) -> Self::Output {
        self.mul_scalar(rhs)
    }
}

/// Division operator: tensor / tensor
impl Div<F32Tensor> for F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn div(self, rhs: F32Tensor) -> Self::Output {
        (&self).divide(&rhs)
    }
}

/// Division operator: tensor / &tensor
impl Div<&F32Tensor> for F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn div(self, rhs: &F32Tensor) -> Self::Output {
        (&self).divide(rhs)
    }
}

/// Division operator: &tensor / &tensor
impl Div for &F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn div(self, rhs: &F32Tensor) -> Self::Output {
        self.divide(rhs)
    }
}

/// Division operator: tensor / scalar
impl Div<f32> for F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn div(self, rhs: f32) -> Self::Output {
        if rhs == 0.0 {
            return Err(crate::error::RusTorchError::InvalidParameters {
                operation: "div".to_string(),
                message: "Division by zero".to_string(),
            });
        }
        self.mul_scalar(1.0 / rhs)
    }
}

/// Division operator: &tensor / scalar
impl Div<f32> for &F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn div(self, rhs: f32) -> Self::Output {
        if rhs == 0.0 {
            return Err(crate::error::RusTorchError::InvalidParameters {
                operation: "div".to_string(),
                message: "Division by zero".to_string(),
            });
        }
        self.mul_scalar(1.0 / rhs)
    }
}

/// Negation operator: -tensor
impl Neg for F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn neg(self) -> Self::Output {
        self.mul_scalar(-1.0)
    }
}

/// Negation operator: -&tensor
impl Neg for &F32Tensor {
    type Output = RusTorchResult<F32Tensor>;

    fn neg(self) -> Self::Output {
        self.mul_scalar(-1.0)
    }
}
impl F32Tensor {
    /// テンソルデータへのスライスアクセス
    /// Slice access to tensor data
    pub fn as_slice(&self) -> &[f32] {
        self.data.as_slice().unwrap_or(&[])
    }

    /// テンソルの次元数を取得
    /// Get number of dimensions
    pub fn ndim(&self) -> usize {
        self.shape.len()
    }

    /// テンソルが空かどうかを取得
    /// Check if tensor is empty
    pub fn is_empty(&self) -> bool {
        self.numel() == 0
    }

    /// テンソルがスカラーかどうかを取得
    /// Check if tensor is scalar
    pub fn is_scalar(&self) -> bool {
        self.numel() == 1
    }

    /// 勾配計算が有効かどうかを取得
    /// Check if gradient computation is enabled
    pub fn is_grad_enabled(&self) -> bool {
        self.requires_grad
    }

    /// 勾配計算を設定
    /// Set gradient computation
    pub fn requires_grad(&mut self, requires: bool) {
        self.requires_grad = requires;
    }

    /// ゼロテンソル作成
    /// Create zero tensor
    pub fn zeros(shape: &[usize]) -> RusTorchResult<Self> {
        hybrid_f32_experimental!();

        let data = Array::zeros(IxDyn(shape));
        Ok(Self {
            data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: false,
            shape: shape.to_vec(),
        })
    }

    /// 正規分布乱数テンソル作成
    /// Create random normal tensor
    pub fn randn(shape: &[usize]) -> RusTorchResult<Self> {
        hybrid_f32_experimental!();

        use rand::Rng;
        use rand_distr::StandardNormal;

        let mut rng = rand::thread_rng();
        let size: usize = shape.iter().product();
        let data: Vec<f32> = (0..size).map(|_| rng.sample(StandardNormal)).collect();

        let array = Array::from_shape_vec(IxDyn(shape), data).map_err(|e| {
            RusTorchError::InvalidParameters {
                operation: "randn".to_string(),
                message: format!("Shape error: {}", e),
            }
        })?;

        Ok(Self {
            data: array,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: false,
            shape: shape.to_vec(),
        })
    }

    /// スカラー値からテンソル作成
    /// Create tensor from scalar
    pub fn from_scalar(value: f32) -> RusTorchResult<Self> {
        hybrid_f32_experimental!();

        let data = Array::from_elem(IxDyn(&[1]), value);
        Ok(Self {
            data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: false,
            shape: vec![1],
        })
    }

    /// テンソル形状取得
    /// Get tensor shape
    pub fn shape(&self) -> &[usize] {
        &self.shape
    }

    /// CPU側へのコピー
    /// Copy to CPU
    pub fn to_cpu(&self) -> RusTorchResult<Self> {
        Ok(self.clone())
    }

    /// Metal GPU転送
    /// Transfer to Metal GPU
    pub fn to_metal(&mut self, device_id: usize) -> RusTorchResult<()> {
        hybrid_f32_experimental!();

        self.device_state = DeviceState::Metal { device_id };
        self.metal_buffer = Some(Arc::new(MetalBuffer::new(device_id, self.data.len())));
        Ok(())
    }

    /// CoreML Neural Engine転送
    /// Transfer to CoreML Neural Engine
    pub fn to_coreml(&mut self, device_id: usize) -> RusTorchResult<()> {
        hybrid_f32_experimental!();

        self.device_state = DeviceState::CoreML { device_id };
        self.coreml_buffer = Some(Arc::new(CoreMLBuffer::new(device_id, self.shape.clone())));
        Ok(())
    }

    /// デバイス状態取得
    /// Get device state
    pub fn device_state(&self) -> &DeviceState {
        &self.device_state
    }

    /// スカラー値取得(1要素テンソルから)
    /// Get scalar value (from 1-element tensor)
    pub fn unwrap(&self) -> RusTorchResult<f32> {
        if self.data.len() == 1 {
            Ok(self.data.iter().next().copied().unwrap_or(0.0))
        } else {
            Err(RusTorchError::InvalidParameters {
                operation: "unwrap".to_string(),
                message: format!("Tensor has {} elements, expected 1", self.data.len()),
            })
        }
    }

    /// 要素ごと加算
    /// Element-wise addition
    pub fn add(&self, other: &Self) -> RusTorchResult<Self> {
        // スカラーブロードキャスティングのサポート
        if other.shape == [1] {
            // スカラーとの演算
            let scalar_value = other.data.iter().next().unwrap();
            let result_data = self.data.mapv(|x| x + scalar_value);
            return Ok(Self {
                data: result_data,
                metal_buffer: None,
                coreml_buffer: None,
                device_state: DeviceState::CPU,
                requires_grad: self.requires_grad || other.requires_grad,
                shape: self.shape.clone(),
            });
        }

        // 形状の互換性チェック(通常のテンソル演算)
        if self.shape != other.shape {
            return Err(RusTorchError::shape_mismatch(&self.shape, &other.shape));
        }

        let result_data = &self.data + &other.data;
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad || other.requires_grad,
            shape: self.shape.clone(),
        })
    }

    /// 要素ごと乗算
    /// Element-wise multiplication
    pub fn mul(&self, other: &Self) -> RusTorchResult<Self> {
        let result_data = &self.data * &other.data;
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad || other.requires_grad,
            shape: self.shape.clone(),
        })
    }

    /// 行列乗算
    /// Matrix multiplication
    pub fn matmul(&self, other: &Self) -> RusTorchResult<Self> {
        // 簡単な2Dケースのみ実装(プレースホルダー)
        if self.shape.len() == 2 && other.shape.len() == 2 {
            let (m, k) = (self.shape[0], self.shape[1]);
            let (k2, n) = (other.shape[0], other.shape[1]);

            if k != k2 {
                return Err(RusTorchError::InvalidParameters {
                    operation: "matmul".to_string(),
                    message: format!("Incompatible dimensions: {}x{} and {}x{}", m, k, k2, n),
                });
            }

            let result_shape = vec![m, n];
            let mut result_data = vec![0.0f32; m * n];

            for i in 0..m {
                for j in 0..n {
                    let mut sum = 0.0;
                    for l in 0..k {
                        sum += self.data[[i, l]] * other.data[[l, j]];
                    }
                    result_data[i * n + j] = sum;
                }
            }

            let array = Array::from_shape_vec(IxDyn(&result_shape), result_data).map_err(|e| {
                RusTorchError::InvalidParameters {
                    operation: "matmul".to_string(),
                    message: format!("Shape error: {}", e),
                }
            })?;

            Ok(Self {
                data: array,
                metal_buffer: None,
                coreml_buffer: None,
                device_state: DeviceState::CPU,
                requires_grad: self.requires_grad || other.requires_grad,
                shape: result_shape,
            })
        } else {
            Err(RusTorchError::InvalidParameters {
                operation: "matmul".to_string(),
                message: "Only 2D tensors supported".to_string(),
            })
        }
    }

    /// 転置
    /// Transpose
    pub fn transpose(&self) -> RusTorchResult<Self> {
        if self.shape.len() == 2 {
            let transposed = self.data.view().reversed_axes().to_owned();
            let new_shape = vec![self.shape[1], self.shape[0]];

            Ok(Self {
                data: transposed,
                metal_buffer: None,
                coreml_buffer: None,
                device_state: DeviceState::CPU,
                requires_grad: self.requires_grad,
                shape: new_shape,
            })
        } else {
            Err(RusTorchError::InvalidParameters {
                operation: "transpose".to_string(),
                message: "Only 2D tensors supported".to_string(),
            })
        }
    }

    /// 要素ごと減算
    /// Element-wise subtraction
    pub fn sub(&self, other: &Self) -> RusTorchResult<Self> {
        let result_data = &self.data - &other.data;
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad || other.requires_grad,
            shape: self.shape.clone(),
        })
    }

    /// 数値要素数取得
    /// Get number of elements
    pub fn numel(&self) -> usize {
        self.data.len()
    }

    /// より大きい要素マスク
    /// Greater than element mask
    pub fn gt(&self, other: &Self) -> RusTorchResult<Self> {
        let result_data = self.data.mapv(|x| {
            if x > other.data.iter().next().copied().unwrap_or(0.0) {
                1.0
            } else {
                0.0
            }
        });
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: false,
            shape: self.shape.clone(),
        })
    }

    /// より小さいか等しい要素マスク
    /// Less than or equal element mask
    pub fn le(&self, other: &Self) -> RusTorchResult<Self> {
        let result_data = self.data.mapv(|x| {
            if x <= other.data.iter().next().copied().unwrap_or(0.0) {
                1.0
            } else {
                0.0
            }
        });
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: false,
            shape: self.shape.clone(),
        })
    }

    /// ReLU活性化関数
    /// ReLU activation function
    pub fn relu(&self) -> RusTorchResult<Self> {
        let result_data = self.data.mapv(|x| x.max(0.0));
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad,
            shape: self.shape.clone(),
        })
    }

    /// Sigmoid活性化関数
    /// Sigmoid activation function
    pub fn sigmoid(&self) -> RusTorchResult<Self> {
        let result_data = self.data.mapv(|x| 1.0 / (1.0 + (-x).exp()));
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad,
            shape: self.shape.clone(),
        })
    }

    /// Tanh活性化関数
    /// Tanh activation function
    pub fn tanh(&self) -> RusTorchResult<Self> {
        let result_data = self.data.mapv(|x| x.tanh());
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad,
            shape: self.shape.clone(),
        })
    }

    /// 指数関数
    /// Exponential function
    pub fn exp(&self) -> RusTorchResult<Self> {
        let result_data = self.data.mapv(|x| x.exp());
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad,
            shape: self.shape.clone(),
        })
    }

    /// 対数関数
    /// Logarithm function
    pub fn log(&self) -> RusTorchResult<Self> {
        let result_data = self.data.mapv(|x| x.ln());
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad,
            shape: self.shape.clone(),
        })
    }

    /// べき乗
    /// Power function
    pub fn power(&self, exponent: f32) -> RusTorchResult<Self> {
        let result_data = self.data.mapv(|x| x.powf(exponent));
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad,
            shape: self.shape.clone(),
        })
    }

    /// 最大値(要素ごと)
    /// Element-wise maximum
    pub fn maximum(&self, other: &Self) -> RusTorchResult<Self> {
        let result_data = self
            .data
            .mapv(|x| x.max(other.data.iter().next().copied().unwrap_or(0.0)));
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad || other.requires_grad,
            shape: self.shape.clone(),
        })
    }

    /// 最小値(要素ごと)
    /// Element-wise minimum
    pub fn minimum(&self, other: &Self) -> RusTorchResult<Self> {
        let result_data = self
            .data
            .mapv(|x| x.min(other.data.iter().next().copied().unwrap_or(0.0)));
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad || other.requires_grad,
            shape: self.shape.clone(),
        })
    }

    /// 値のクランプ
    /// Clamp values
    pub fn clamp(&self, min: f32, max: f32) -> RusTorchResult<Self> {
        let result_data = self.data.mapv(|x| x.max(min).min(max));
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad,
            shape: self.shape.clone(),
        })
    }

    /// 最大値のインデックス
    /// Index of maximum value
    pub fn argmax(&self) -> RusTorchResult<Self> {
        let max_idx = self
            .data
            .iter()
            .enumerate()
            .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap())
            .map(|(idx, _)| idx as f32)
            .unwrap_or(0.0);

        Self::from_scalar(max_idx)
    }

    /// テンソル形状変更
    /// Reshape tensor
    pub fn reshape(&self, new_shape: &[usize]) -> RusTorchResult<Self> {
        let new_size: usize = new_shape.iter().product();
        if new_size != self.data.len() {
            return Err(RusTorchError::InvalidParameters {
                operation: "reshape".to_string(),
                message: format!(
                    "Cannot reshape tensor of size {} to size {}",
                    self.data.len(),
                    new_size
                ),
            });
        }

        let reshaped_data = self
            .data
            .clone()
            .into_shape_with_order(IxDyn(new_shape))
            .map_err(|e| RusTorchError::InvalidParameters {
                operation: "reshape".to_string(),
                message: format!("Reshape error: {}", e),
            })?;

        Ok(Self {
            data: reshaped_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad,
            shape: new_shape.to_vec(),
        })
    }

    /// テンソルスライス(簡易版)
    /// Tensor slice (simple version)
    pub fn slice(&self, ranges: &[(usize, usize)]) -> RusTorchResult<Self> {
        // Simple slice implementation for compatibility
        let mut result_data = Vec::new();
        let shape = self.shape();

        // For simplicity, just extract the first range for 1D case
        if ranges.len() == 1 && self.ndim() == 1 {
            let (start, end) = ranges[0];
            let slice_data = &self.data.as_slice().unwrap()[start..end];
            result_data.extend_from_slice(slice_data);
            F32Tensor::new(result_data, &[end - start])
        } else {
            // For more complex cases, return a clone for now
            Ok(self.clone())
        }
    }

    /// 型変換
    /// Type conversion
    pub fn to_type(&self, _dtype: &str) -> RusTorchResult<Self> {
        // f32から他の型への変換は今回スキップ
        Ok(self.clone())
    }

    /// 除算
    /// Division
    pub fn divide(&self, other: &Self) -> RusTorchResult<Self> {
        // スカラーブロードキャスティングのサポート
        if other.shape == [1] {
            // スカラーとの演算
            let scalar_value = other.data.iter().next().unwrap();
            if *scalar_value == 0.0 {
                return Err(RusTorchError::tensor_op("Division by zero"));
            }
            let result_data = self.data.mapv(|x| x / scalar_value);
            return Ok(Self {
                data: result_data,
                metal_buffer: None,
                coreml_buffer: None,
                device_state: DeviceState::CPU,
                requires_grad: self.requires_grad || other.requires_grad,
                shape: self.shape.clone(),
            });
        }

        // 形状の互換性チェック(通常のテンソル演算)
        if self.shape != other.shape {
            return Err(RusTorchError::shape_mismatch(&self.shape, &other.shape));
        }

        // ゼロ除算チェック
        for &value in other.data.iter() {
            if value == 0.0 {
                return Err(RusTorchError::tensor_op("Division by zero"));
            }
        }

        let result_data = &self.data / &other.data;
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad || other.requires_grad,
            shape: self.shape.clone(),
        })
    }

    /// 減算
    /// Subtraction
    pub fn subtract(&self, other: &Self) -> RusTorchResult<Self> {
        // スカラーブロードキャスティングのサポート
        if other.shape == [1] {
            // スカラーとの演算
            let scalar_value = other.data.iter().next().unwrap();
            let result_data = self.data.mapv(|x| x - scalar_value);
            return Ok(Self {
                data: result_data,
                metal_buffer: None,
                coreml_buffer: None,
                device_state: DeviceState::CPU,
                requires_grad: self.requires_grad || other.requires_grad,
                shape: self.shape.clone(),
            });
        }

        // 形状の互換性チェック(通常のテンソル演算)
        if self.shape != other.shape {
            return Err(RusTorchError::shape_mismatch(&self.shape, &other.shape));
        }

        let result_data = &self.data - &other.data;
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad || other.requires_grad,
            shape: self.shape.clone(),
        })
    }

    /// 乗算(要素ごと)
    /// Element-wise multiplication
    pub fn multiply(&self, other: &Self) -> RusTorchResult<Self> {
        // スカラーブロードキャスティングのサポート
        if other.shape == [1] {
            // スカラーとの演算
            let scalar_value = other.data.iter().next().unwrap();
            let result_data = self.data.mapv(|x| x * scalar_value);
            return Ok(Self {
                data: result_data,
                metal_buffer: None,
                coreml_buffer: None,
                device_state: DeviceState::CPU,
                requires_grad: self.requires_grad || other.requires_grad,
                shape: self.shape.clone(),
            });
        }

        // 形状の互換性チェック(通常のテンソル演算)
        if self.shape != other.shape {
            return Err(RusTorchError::shape_mismatch(&self.shape, &other.shape));
        }

        let result_data = &self.data * &other.data;
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad || other.requires_grad,
            shape: self.shape.clone(),
        })
    }

    /// スカラー加算
    /// Add scalar value to all elements
    pub fn add_scalar(&self, scalar: f32) -> RusTorchResult<Self> {
        let result_data = self.data.mapv(|x| x + scalar);
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad,
            shape: self.shape.clone(),
        })
    }

    /// スカラー乗算
    /// Multiply all elements by scalar value
    pub fn multiply_scalar(&self, scalar: f32) -> RusTorchResult<Self> {
        let result_data = self.data.mapv(|x| x * scalar);
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad,
            shape: self.shape.clone(),
        })
    }

    /// 平均値計算
    /// Calculate mean of all elements
    pub fn mean(&self) -> RusTorchResult<f32> {
        if self.data.is_empty() {
            return Err(RusTorchError::tensor_op(
                "Cannot calculate mean of empty tensor",
            ));
        }
        Ok(self.data.mean().unwrap())
    }

    /// 最小値計算
    /// Calculate minimum value
    pub fn min(&self) -> RusTorchResult<f32> {
        if self.data.is_empty() {
            return Err(RusTorchError::tensor_op(
                "Cannot calculate min of empty tensor",
            ));
        }
        let min_val = self.data.iter().cloned().fold(f32::INFINITY, f32::min);
        Ok(min_val)
    }

    /// 最大値計算
    /// Calculate maximum value
    pub fn max(&self) -> RusTorchResult<f32> {
        if self.data.is_empty() {
            return Err(RusTorchError::tensor_op(
                "Cannot calculate max of empty tensor",
            ));
        }
        let max_val = self.data.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
        Ok(max_val)
    }

    /// 平均(テンソル同士)
    /// Mean (tensor-wise)
    pub fn mean_tensor(&self) -> RusTorchResult<Self> {
        let mean_val = self.data.mean().unwrap_or(0.0);
        Self::from_scalar(mean_val)
    }

    /// 次元に沿った合計
    /// Sum along dimension
    pub fn sum_dim(&self, _dim: usize) -> RusTorchResult<Self> {
        let sum_val = self.data.sum();
        Self::from_scalar(sum_val)
    }

    /// データからテンソル作成
    /// Create tensor from vector data
    pub fn from_vec(data: Vec<f32>, shape: &[usize]) -> RusTorchResult<Self> {
        let expected_size: usize = shape.iter().product();
        if data.len() != expected_size {
            return Err(RusTorchError::InvalidParameters {
                operation: "from_vec".to_string(),
                message: format!(
                    "Data length {} doesn't match shape size {}",
                    data.len(),
                    expected_size
                ),
            });
        }

        let array = Array::from_shape_vec(IxDyn(shape), data).map_err(|e| {
            RusTorchError::InvalidParameters {
                operation: "from_vec".to_string(),
                message: format!("Shape error: {}", e),
            }
        })?;

        Ok(Self {
            data: array,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: false,
            shape: shape.to_vec(),
        })
    }

    /// 1のテンソル作成
    /// Create ones tensor
    pub fn ones(shape: &[usize]) -> RusTorchResult<Self> {
        let data = Array::ones(IxDyn(shape));
        Ok(Self {
            data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: false,
            shape: shape.to_vec(),
        })
    }

    /// 汎用newメソッド(from_vecの別名)
    /// Generic new method (alias for from_vec)
    pub fn new(data: Vec<f32>, shape: &[usize]) -> RusTorchResult<Self> {
        Self::from_vec(data, shape)
    }

    /// スライスアクセス(autograd用のOption版)
    /// Slice access (Option version for autograd)
    pub fn as_slice_option(&self) -> Option<&[f32]> {
        self.data.as_slice()
    }

    /// スカラー値取得(unwrapの別名)
    /// Get scalar value (alias for unwrap)
    pub fn scalar_value(&self) -> RusTorchResult<f32> {
        self.unwrap()
    }

    // ========================================
    // try_*メソッド群 - エラー処理改善
    // try_* methods - Improved error handling
    // ========================================

    /// 安全なテンソル加算
    /// Safe tensor addition
    pub fn try_add(&self, other: &F32Tensor) -> RusTorchResult<F32Tensor> {
        self.add(other)
    }

    /// 安全なテンソル減算
    /// Safe tensor subtraction
    pub fn try_sub(&self, other: &F32Tensor) -> RusTorchResult<F32Tensor> {
        self.sub(other)
    }

    /// 安全なテンソル乗算
    /// Safe tensor multiplication
    pub fn try_mul(&self, other: &F32Tensor) -> RusTorchResult<F32Tensor> {
        self.mul(other)
    }

    /// 安全なテンソル除算
    /// Safe tensor division
    pub fn try_div(&self, other: &F32Tensor) -> RusTorchResult<F32Tensor> {
        self.divide(other)
    }

    /// 安全な行列乗算
    /// Safe matrix multiplication
    pub fn try_matmul(&self, other: &F32Tensor) -> RusTorchResult<F32Tensor> {
        self.matmul(other)
    }

    /// 安全なスカラー乗算
    /// Safe scalar multiplication
    pub fn try_mul_scalar(&self, scalar: f32) -> RusTorchResult<F32Tensor> {
        if scalar.is_nan() || scalar.is_infinite() {
            return Err(crate::error::RusTorchError::InvalidParameters {
                operation: "try_mul_scalar".to_string(),
                message: format!("Invalid scalar value: {}", scalar),
            });
        }
        self.mul_scalar(scalar)
    }

    /// 安全な形状変更
    /// Safe reshape
    pub fn try_reshape(&self, new_shape: &[usize]) -> RusTorchResult<F32Tensor> {
        let new_numel: usize = new_shape.iter().product();
        if new_numel != self.numel() {
            return Err(crate::error::RusTorchError::InvalidParameters {
                operation: "try_reshape".to_string(),
                message: format!(
                    "Cannot reshape tensor with {} elements to shape {:?} ({} elements)",
                    self.numel(),
                    new_shape,
                    new_numel
                ),
            });
        }
        self.reshape(new_shape)
    }

    /// 安全な転置
    /// Safe transpose
    pub fn try_transpose(&self) -> RusTorchResult<F32Tensor> {
        if self.ndim() != 2 {
            return Err(crate::error::RusTorchError::InvalidOperation(format!(
                "transpose requires 2D tensor, got {}D",
                self.ndim()
            )));
        }
        self.transpose()
    }

    /// 安全なスライス
    /// Safe slice
    pub fn try_slice(&self, ranges: &[(usize, usize)]) -> RusTorchResult<F32Tensor> {
        if ranges.len() != self.ndim() {
            return Err(crate::error::RusTorchError::InvalidParameters {
                operation: "try_slice".to_string(),
                message: format!(
                    "Expected {} slice ranges for {}D tensor, got {}",
                    self.ndim(),
                    self.ndim(),
                    ranges.len()
                ),
            });
        }

        let shape = self.shape();
        for (i, &(start, end)) in ranges.iter().enumerate() {
            if start >= end || end > shape[i] {
                return Err(crate::error::RusTorchError::InvalidParameters {
                    operation: "try_slice".to_string(),
                    message: format!(
                        "Invalid slice range for dimension {}: {}..{} (max: {})",
                        i, start, end, shape[i]
                    ),
                });
            }
        }

        self.slice(ranges)
    }

    /// 安全なCPU転送
    /// Safe CPU transfer
    pub fn try_to_cpu(&self) -> RusTorchResult<F32Tensor> {
        self.to_cpu()
    }

    /// 安全なMetal転送
    /// Safe Metal transfer
    pub fn try_to_metal(&mut self, device_id: usize) -> RusTorchResult<()> {
        #[cfg(all(target_os = "macos", feature = "metal"))]
        {
            self.to_metal(device_id)
        }

        #[cfg(not(all(target_os = "macos", feature = "metal")))]
        {
            Err(crate::error::RusTorchError::BackendUnavailable {
                backend: "Metal (macOS + metal feature required)".to_string(),
            })
        }
    }

    /// 安全なCoreML転送
    /// Safe CoreML transfer
    pub fn try_to_coreml(&mut self, device_id: usize) -> RusTorchResult<()> {
        #[cfg(all(target_os = "macos", feature = "coreml"))]
        {
            self.to_coreml(device_id)
        }

        #[cfg(not(all(target_os = "macos", feature = "coreml")))]
        {
            Err(crate::error::RusTorchError::BackendUnavailable {
                backend: "CoreML (macOS + coreml feature required)".to_string(),
            })
        }
    }

    /// 安全な型変換
    /// Safe type conversion
    pub fn try_to_type<T>(&self) -> RusTorchResult<Vec<T>>
    where
        T: From<f32> + Copy,
    {
        if self.numel() == 0 {
            return Ok(Vec::new());
        }

        let data = self.data.as_slice().ok_or_else(|| {
            crate::error::RusTorchError::InvalidOperation("Cannot access tensor data".to_string())
        })?;

        Ok(data.iter().map(|&x| T::from(x)).collect())
    }

    /// 安全な要素アクセス
    /// Safe element access
    pub fn try_get(&self, indices: &[usize]) -> RusTorchResult<f32> {
        if indices.len() != self.ndim() {
            return Err(crate::error::RusTorchError::InvalidParameters {
                operation: "try_get".to_string(),
                message: format!(
                    "Expected {} indices for {}D tensor, got {}",
                    self.ndim(),
                    self.ndim(),
                    indices.len()
                ),
            });
        }

        let shape = self.shape();
        for (i, &idx) in indices.iter().enumerate() {
            if idx >= shape[i] {
                return Err(crate::error::RusTorchError::index_out_of_bounds(
                    &[idx],
                    &[shape[i]],
                ));
            }
        }

        // 平坦化インデックス計算
        let mut flat_index = 0;
        let mut stride = 1;
        for i in (0..indices.len()).rev() {
            flat_index += indices[i] * stride;
            stride *= shape[i];
        }

        let data = self.data.as_slice().ok_or_else(|| {
            crate::error::RusTorchError::InvalidOperation("Cannot access tensor data".to_string())
        })?;

        Ok(data[flat_index])
    }

    /// 安全な要素設定
    /// Safe element setting
    pub fn try_set(&mut self, indices: &[usize], value: f32) -> RusTorchResult<()> {
        if value.is_nan() || value.is_infinite() {
            return Err(crate::error::RusTorchError::InvalidParameters {
                operation: "try_set".to_string(),
                message: format!("Invalid value: {}", value),
            });
        }

        if indices.len() != self.ndim() {
            return Err(crate::error::RusTorchError::InvalidParameters {
                operation: "try_set".to_string(),
                message: format!(
                    "Expected {} indices for {}D tensor, got {}",
                    self.ndim(),
                    self.ndim(),
                    indices.len()
                ),
            });
        }

        let shape = self.shape();
        for (i, &idx) in indices.iter().enumerate() {
            if idx >= shape[i] {
                return Err(crate::error::RusTorchError::index_out_of_bounds(
                    &[idx],
                    &[shape[i]],
                ));
            }
        }

        // 平坦化インデックス計算
        let mut flat_index = 0;
        let mut stride = 1;
        for i in (0..indices.len()).rev() {
            flat_index += indices[i] * stride;
            stride *= shape[i];
        }

        let data = self.data.as_slice_mut().ok_or_else(|| {
            crate::error::RusTorchError::InvalidOperation(
                "Cannot access tensor data for modification".to_string(),
            )
        })?;

        data[flat_index] = value;
        Ok(())
    }

    /// 全要素の合計(スカラー)
    /// Sum of all elements (scalar)
    pub fn sum(&self) -> RusTorchResult<f32> {
        Ok(self.data.sum())
    }

    /// スカラー乗算(修正版)
    /// Scalar multiplication (fixed version)
    pub fn mul_scalar(&self, scalar: f32) -> RusTorchResult<Self> {
        let result_data = &self.data * scalar;
        Ok(Self {
            data: result_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad,
            shape: self.shape.clone(),
        })
    }

    /// 次元を追加(unsqueeze)
    /// Add dimension (unsqueeze)
    pub fn unsqueeze(&self, dim: usize) -> RusTorchResult<Self> {
        let mut new_shape = self.shape.clone();

        if dim > new_shape.len() {
            return Err(RusTorchError::InvalidParameters {
                operation: "unsqueeze".to_string(),
                message: format!(
                    "Dimension {} out of bounds for tensor with {} dimensions",
                    dim,
                    new_shape.len()
                ),
            });
        }

        new_shape.insert(dim, 1);

        let reshaped_data = self
            .data
            .clone()
            .into_shape_with_order(IxDyn(&new_shape))
            .map_err(|e| RusTorchError::InvalidParameters {
                operation: "unsqueeze".to_string(),
                message: format!("Reshape error: {}", e),
            })?;

        Ok(Self {
            data: reshaped_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad,
            shape: new_shape,
        })
    }

    /// テンソルサイズを拡張(expand)
    /// Expand tensor size
    pub fn expand(&self, new_shape: &[usize]) -> RusTorchResult<Self> {
        if new_shape.len() != self.shape.len() {
            return Err(RusTorchError::InvalidParameters {
                operation: "expand".to_string(),
                message: format!(
                    "Cannot expand from {} dimensions to {} dimensions",
                    self.shape.len(),
                    new_shape.len()
                ),
            });
        }

        // Check that each dimension can be expanded
        for (i, (&current, &target)) in self.shape.iter().zip(new_shape.iter()).enumerate() {
            if current != 1 && current != target {
                return Err(RusTorchError::InvalidParameters {
                    operation: "expand".to_string(),
                    message: format!(
                        "Cannot expand dimension {} from {} to {}",
                        i, current, target
                    ),
                });
            }
        }

        // For now, create a simple broadcasted version by repeating data
        let total_size: usize = new_shape.iter().product();
        let mut expanded_data = Vec::with_capacity(total_size);

        // Simple expansion logic - repeat the pattern
        let source_data = self.data.as_slice().unwrap();
        let source_size = source_data.len();
        let repeat_count = total_size / source_size;

        for _ in 0..repeat_count {
            expanded_data.extend_from_slice(source_data);
        }

        let array = Array::from_shape_vec(IxDyn(new_shape), expanded_data).map_err(|e| {
            RusTorchError::InvalidParameters {
                operation: "expand".to_string(),
                message: format!("Shape error: {}", e),
            }
        })?;

        Ok(Self {
            data: array,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad,
            shape: new_shape.to_vec(),
        })
    }

    /// 指定された次元で転置(transpose_dims)
    /// Transpose with specified dimensions
    pub fn transpose_dims(&self, dim1: usize, dim2: usize) -> RusTorchResult<Self> {
        if dim1 >= self.shape.len() || dim2 >= self.shape.len() {
            return Err(RusTorchError::InvalidParameters {
                operation: "transpose_dims".to_string(),
                message: format!(
                    "Dimension indices {} and {} out of bounds for tensor with {} dimensions",
                    dim1,
                    dim2,
                    self.shape.len()
                ),
            });
        }

        if dim1 == dim2 {
            return Ok(self.clone());
        }

        // Create new shape with swapped dimensions
        let mut new_shape = self.shape.clone();
        new_shape.swap(dim1, dim2);

        // For ndarray, we need to use swap_axes
        let mut transposed_data = self.data.clone();

        // Use ndarray's swap_axes method
        transposed_data.swap_axes(dim1, dim2);

        Ok(Self {
            data: transposed_data,
            metal_buffer: None,
            coreml_buffer: None,
            device_state: DeviceState::CPU,
            requires_grad: self.requires_grad,
            shape: new_shape,
        })
    }

    /// Softmax活性化関数
    /// Softmax activation function
    pub fn softmax(&self, dim: Option<usize>) -> RusTorchResult<Self> {
        // Apply softmax along the last dimension by default
        let softmax_dim = dim.unwrap_or(self.shape.len().saturating_sub(1));

        if softmax_dim >= self.shape.len() {
            return Err(RusTorchError::InvalidParameters {
                operation: "softmax".to_string(),
                message: format!(
                    "Dimension {} out of bounds for tensor with {} dimensions",
                    softmax_dim,
                    self.shape.len()
                ),
            });
        }

        // For numerical stability, subtract the maximum value
        let max_val = self.data.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
        let max_tensor = F32Tensor::from_scalar(max_val)?;
        let shifted = self.sub(&max_tensor)?;

        // Compute exp
        let exp_data = shifted.exp()?;

        // Compute sum for normalization
        let sum_val = exp_data.data.sum();
        let sum_tensor = F32Tensor::from_scalar(sum_val)?;

        // Divide by sum
        exp_data.divide(&sum_tensor)
    }

    // ========================================
    // 高度数学機能 - Advanced Mathematical Functions
    // ========================================

    /// QR分解 (Householder方法)
    /// QR decomposition (Householder method)
    pub fn qr_decomposition(&self) -> RusTorchResult<(Self, Self)> {
        if self.ndim() != 2 {
            return Err(RusTorchError::InvalidParameters {
                operation: "qr_decomposition".to_string(),
                message: "QR decomposition requires 2D tensor".to_string(),
            });
        }

        let (m, n) = (self.shape[0], self.shape[1]);
        let min_dim = m.min(n);

        // Q行列の初期化(単位行列)
        let mut q_data = vec![0.0f32; m * m];
        for i in 0..m {
            q_data[i * m + i] = 1.0;
        }

        // R行列の初期化(Aのコピー)
        let mut r_data = self.data.as_slice().unwrap().to_vec();

        // Householder変換によるQR分解
        for k in 0..min_dim {
            // k列目の対角要素以下のベクトル抽出
            let mut v = vec![0.0f32; m - k];
            for i in k..m {
                v[i - k] = r_data[i * n + k];
            }

            // Householder反射ベクトル計算
            let norm = v.iter().map(|x| x * x).sum::<f32>().sqrt();
            if norm == 0.0 {
                continue;
            }

            v[0] += if v[0] >= 0.0 { norm } else { -norm };
            let v_norm = v.iter().map(|x| x * x).sum::<f32>().sqrt();
            if v_norm == 0.0 {
                continue;
            }

            for i in 0..v.len() {
                v[i] /= v_norm;
            }

            // Householder変換をR行列に適用
            for j in k..n {
                let mut dot_product = 0.0;
                for i in k..m {
                    dot_product += v[i - k] * r_data[i * n + j];
                }

                for i in k..m {
                    r_data[i * n + j] -= 2.0 * v[i - k] * dot_product;
                }
            }

            // Householder変換をQ行列に適用
            for j in 0..m {
                let mut dot_product = 0.0;
                for i in k..m {
                    dot_product += v[i - k] * q_data[i * m + j];
                }

                for i in k..m {
                    q_data[i * m + j] -= 2.0 * v[i - k] * dot_product;
                }
            }
        }

        let q = F32Tensor::from_vec(q_data, &[m, m])?;
        let r = F32Tensor::from_vec(r_data, &[m, n])?;

        Ok((q, r))
    }

    /// Cholesky分解 (対称正定値行列用)
    /// Cholesky decomposition (for symmetric positive definite matrices)
    pub fn cholesky_decomposition(&self) -> RusTorchResult<Self> {
        if self.ndim() != 2 || self.shape[0] != self.shape[1] {
            return Err(RusTorchError::InvalidParameters {
                operation: "cholesky_decomposition".to_string(),
                message: "Cholesky decomposition requires square matrix".to_string(),
            });
        }

        let n = self.shape[0];
        let mut l_data = vec![0.0f32; n * n];
        let a_data = self.data.as_slice().unwrap();

        for i in 0..n {
            for j in 0..=i {
                if i == j {
                    // 対角要素の計算
                    let mut sum = 0.0;
                    for k in 0..j {
                        sum += l_data[j * n + k] * l_data[j * n + k];
                    }
                    let val = a_data[j * n + j] - sum;
                    if val <= 0.0 {
                        return Err(RusTorchError::InvalidParameters {
                            operation: "cholesky_decomposition".to_string(),
                            message: "Matrix is not positive definite".to_string(),
                        });
                    }
                    l_data[j * n + j] = val.sqrt();
                } else {
                    // 下三角要素の計算
                    let mut sum = 0.0;
                    for k in 0..j {
                        sum += l_data[i * n + k] * l_data[j * n + k];
                    }
                    l_data[i * n + j] = (a_data[i * n + j] - sum) / l_data[j * n + j];
                }
            }
        }

        F32Tensor::from_vec(l_data, &[n, n])
    }

    /// 特異値分解 (SVD) - 基本版
    /// Singular Value Decomposition (SVD) - Basic version  
    pub fn svd(&self) -> RusTorchResult<(Self, Self, Self)> {
        if self.ndim() != 2 {
            return Err(RusTorchError::InvalidParameters {
                operation: "svd".to_string(),
                message: "SVD requires 2D tensor".to_string(),
            });
        }

        let (m, n) = (self.shape[0], self.shape[1]);

        // 簡易版SVD(反復法)
        // A^T * A の固有値分解によりV, Σを求める
        let at = self.transpose()?;
        let ata = at.matmul(self)?;

        // 最大固有値とその固有ベクトルを求める(Power method)
        let mut v = F32Tensor::randn(&[n, 1])?;

        for _ in 0..100 {
            // 最大100回反復
            let av = ata.matmul(&v)?;
            let norm = av.data.iter().map(|x| x * x).sum::<f32>().sqrt();
            if norm == 0.0 {
                break;
            }

            v = av.mul_scalar(1.0 / norm)?;
        }

        // σ = ||Av||
        let av = self.matmul(&v)?;
        let sigma = av.data.iter().map(|x| x * x).sum::<f32>().sqrt();

        // u = Av / σ
        let u = if sigma > 1e-10 {
            av.mul_scalar(1.0 / sigma)?
        } else {
            F32Tensor::zeros(&[m, 1])?
        };

        // 簡易版では単一の特異値のみ返す
        let s = F32Tensor::from_scalar(sigma)?;

        Ok((u, s, v))
    }

    /// 固有値分解 (対称行列用, Power method)
    /// Eigenvalue decomposition (for symmetric matrices, Power method)
    pub fn eigen_decomposition(&self) -> RusTorchResult<(Self, Self)> {
        if self.ndim() != 2 || self.shape[0] != self.shape[1] {
            return Err(RusTorchError::InvalidParameters {
                operation: "eigen_decomposition".to_string(),
                message: "Eigenvalue decomposition requires square matrix".to_string(),
            });
        }

        let n = self.shape[0];

        // Power methodで最大固有値と固有ベクトルを求める
        let mut v = F32Tensor::randn(&[n, 1])?;
        let mut eigenvalue = 0.0;

        for _ in 0..100 {
            // 最大100回反復
            let av = self.matmul(&v)?;

            // Rayleigh商で固有値を近似
            let vt_av = v.transpose()?.matmul(&av)?;
            let vt_v = v.transpose()?.matmul(&v)?;

            eigenvalue = vt_av.unwrap()? / vt_v.unwrap()?;

            // 正規化
            let norm = av.data.iter().map(|x| x * x).sum::<f32>().sqrt();
            if norm < 1e-10 {
                break;
            }

            v = av.mul_scalar(1.0 / norm)?;
        }

        let eigenvalues = F32Tensor::from_scalar(eigenvalue)?;
        let eigenvectors = v;

        Ok((eigenvalues, eigenvectors))
    }

    /// LU分解 (部分ピボット付き)
    /// LU decomposition (with partial pivoting)
    pub fn lu_decomposition(&self) -> RusTorchResult<(Self, Self, Self)> {
        if self.ndim() != 2 || self.shape[0] != self.shape[1] {
            return Err(RusTorchError::InvalidParameters {
                operation: "lu_decomposition".to_string(),
                message: "LU decomposition requires square matrix".to_string(),
            });
        }

        let n = self.shape[0];
        let mut a_data = self.data.as_slice().unwrap().to_vec();
        let mut l_data = vec![0.0f32; n * n];
        let mut p_data = vec![0.0f32; n * n];

        // 置換行列を単位行列で初期化
        for i in 0..n {
            p_data[i * n + i] = 1.0;
        }

        // L行列を単位行列で初期化
        for i in 0..n {
            l_data[i * n + i] = 1.0;
        }

        // Gaussian elimination with partial pivoting
        for k in 0..n {
            // ピボット選択
            let mut max_row = k;
            let mut max_val = a_data[k * n + k].abs();

            for i in (k + 1)..n {
                if a_data[i * n + k].abs() > max_val {
                    max_val = a_data[i * n + k].abs();
                    max_row = i;
                }
            }

            // 行の交換
            if max_row != k {
                for j in 0..n {
                    a_data.swap(k * n + j, max_row * n + j);
                    p_data.swap(k * n + j, max_row * n + j);
                }
            }

            // L行列の計算
            for i in (k + 1)..n {
                if a_data[k * n + k].abs() < 1e-10 {
                    return Err(RusTorchError::InvalidParameters {
                        operation: "lu_decomposition".to_string(),
                        message: "Matrix is singular".to_string(),
                    });
                }

                let factor = a_data[i * n + k] / a_data[k * n + k];
                l_data[i * n + k] = factor;

                for j in k..n {
                    a_data[i * n + j] -= factor * a_data[k * n + j];
                }
            }
        }

        let p = F32Tensor::from_vec(p_data, &[n, n])?;
        let l = F32Tensor::from_vec(l_data, &[n, n])?;
        let u = F32Tensor::from_vec(a_data, &[n, n])?;

        Ok((p, l, u))
    }

    /// 行列式の計算 (LU分解を利用)
    /// Determinant calculation (using LU decomposition)
    pub fn determinant(&self) -> RusTorchResult<f32> {
        if self.ndim() != 2 || self.shape[0] != self.shape[1] {
            return Err(RusTorchError::InvalidParameters {
                operation: "determinant".to_string(),
                message: "Determinant requires square matrix".to_string(),
            });
        }

        let (p, _l, u) = self.lu_decomposition()?;

        // U行列の対角要素の積
        let n = self.shape[0];
        let u_data = u.data.as_slice().unwrap();
        let mut det = 1.0;

        for i in 0..n {
            det *= u_data[i * n + i];
        }

        // 置換行列による符号の修正
        let p_data = p.data.as_slice().unwrap();
        let mut sign = 1.0;
        for i in 0..n {
            for j in 0..n {
                if i != j && p_data[i * n + j] == 1.0 {
                    sign *= -1.0;
                    break;
                }
            }
        }

        Ok(det * sign)
    }

    /// 逆行列の計算 (Gauss-Jordan法)
    /// Matrix inverse calculation (Gauss-Jordan method)
    pub fn inverse(&self) -> RusTorchResult<Self> {
        if self.ndim() != 2 || self.shape[0] != self.shape[1] {
            return Err(RusTorchError::InvalidParameters {
                operation: "inverse".to_string(),
                message: "Matrix inverse requires square matrix".to_string(),
            });
        }

        let n = self.shape[0];
        let mut augmented = vec![0.0f32; n * (2 * n)];
        let a_data = self.data.as_slice().unwrap();

        // 拡大行列 [A|I] を構築
        for i in 0..n {
            for j in 0..n {
                augmented[i * (2 * n) + j] = a_data[i * n + j];
                augmented[i * (2 * n) + (n + j)] = if i == j { 1.0 } else { 0.0 };
            }
        }

        // Gauss-Jordan elimination
        for i in 0..n {
            // ピボット選択
            let mut max_row = i;
            let mut max_val = augmented[i * (2 * n) + i].abs();

            for k in (i + 1)..n {
                if augmented[k * (2 * n) + i].abs() > max_val {
                    max_val = augmented[k * (2 * n) + i].abs();
                    max_row = k;
                }
            }

            // 行の交換
            if max_row != i {
                for j in 0..(2 * n) {
                    augmented.swap(i * (2 * n) + j, max_row * (2 * n) + j);
                }
            }

            // 対角要素で正規化
            let pivot = augmented[i * (2 * n) + i];
            if pivot.abs() < 1e-10 {
                return Err(RusTorchError::InvalidParameters {
                    operation: "inverse".to_string(),
                    message: "Matrix is singular".to_string(),
                });
            }

            for j in 0..(2 * n) {
                augmented[i * (2 * n) + j] /= pivot;
            }

            // 他の行を処理
            for k in 0..n {
                if k != i {
                    let factor = augmented[k * (2 * n) + i];
                    for j in 0..(2 * n) {
                        augmented[k * (2 * n) + j] -= factor * augmented[i * (2 * n) + j];
                    }
                }
            }
        }

        // 逆行列部分を抽出
        let mut inverse_data = vec![0.0f32; n * n];
        for i in 0..n {
            for j in 0..n {
                inverse_data[i * n + j] = augmented[i * (2 * n) + (n + j)];
            }
        }

        F32Tensor::from_vec(inverse_data, &[n, n])
    }

    /// 行列のランクを計算 (SVDを利用)
    /// Calculate matrix rank (using SVD)
    pub fn rank(&self) -> RusTorchResult<usize> {
        if self.ndim() != 2 {
            return Err(RusTorchError::InvalidParameters {
                operation: "rank".to_string(),
                message: "Rank calculation requires 2D tensor".to_string(),
            });
        }

        // 簡易版:ゼロでない特異値の数をカウント
        let (_u, s, _v) = self.svd()?;
        let tolerance = 1e-6;

        let rank = s.data.iter().filter(|&&x| x.abs() > tolerance).count();

        Ok(rank)
    }

    /// 条件数の計算 (2ノルム)
    /// Condition number calculation (2-norm)
    pub fn condition_number(&self) -> RusTorchResult<f32> {
        if self.ndim() != 2 {
            return Err(RusTorchError::InvalidParameters {
                operation: "condition_number".to_string(),
                message: "Condition number requires 2D tensor".to_string(),
            });
        }

        let (_u, s, _v) = self.svd()?;
        let s_data = s.data.as_slice().unwrap();

        if s_data.is_empty() {
            return Ok(f32::INFINITY);
        }

        let max_singular = s_data.iter().fold(0.0f32, |a, &b| a.max(b));
        let min_singular = s_data
            .iter()
            .filter(|&&x| x > 1e-10)
            .fold(f32::INFINITY, |a, &b| a.min(b));

        if min_singular == f32::INFINITY || min_singular == 0.0 {
            Ok(f32::INFINITY)
        } else {
            Ok(max_singular / min_singular)
        }
    }

    /// Frobenius ノルム
    /// Frobenius norm
    pub fn frobenius_norm(&self) -> RusTorchResult<f32> {
        let sum_of_squares = self.data.iter().map(|&x| x * x).sum::<f32>();
        Ok(sum_of_squares.sqrt())
    }

    /// トレース(対角和)
    /// Trace (sum of diagonal elements)
    pub fn trace(&self) -> RusTorchResult<f32> {
        if self.ndim() != 2 || self.shape[0] != self.shape[1] {
            return Err(RusTorchError::InvalidParameters {
                operation: "trace".to_string(),
                message: "Trace requires square matrix".to_string(),
            });
        }

        let n = self.shape[0];
        let data = self.data.as_slice().unwrap();
        let mut trace = 0.0;

        for i in 0..n {
            trace += data[i * n + i];
        }

        Ok(trace)
    }
}

// Indexing implementations

/// 1D indexing implementation
impl Index<usize> for F32Tensor {
    type Output = f32;

    fn index(&self, index: usize) -> &Self::Output {
        &self.data.as_slice().unwrap()[index]
    }
}

impl IndexMut<usize> for F32Tensor {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.data.as_slice_mut().unwrap()[index]
    }
}

/// 2D indexing implementation
impl Index<Index2D> for F32Tensor {
    type Output = f32;

    fn index(&self, index: Index2D) -> &Self::Output {
        let flat_index = index.0 * self.shape[1] + index.1;
        &self.data.as_slice().unwrap()[flat_index]
    }
}

impl IndexMut<Index2D> for F32Tensor {
    fn index_mut(&mut self, index: Index2D) -> &mut Self::Output {
        let flat_index = index.0 * self.shape[1] + index.1;
        &mut self.data.as_slice_mut().unwrap()[flat_index]
    }
}

impl F32Tensor {
    // ===== GPU Operations =====
    // 高性能GPU演算(Metal/CoreML/Neural Engine)

    /// GPU合計演算(Metal/CoreML最適化)
    /// GPU sum operation with Metal/CoreML optimization
    pub fn gpu_sum(&self, axis: Option<usize>) -> RusTorchResult<Self> {
        crate::hybrid_f32_experimental!();

        // GPU実行コンテキストを取得
        let mut context = crate::hybrid_f32::gpu::F32UnifiedGPUContext::new();

        // テンソルサイズに基づいて最適デバイスを選択
        let optimal_device = context.select_optimal_device("reduction", self.numel());
        context.initialize_device(optimal_device)?;

        // GPU演算実行(リダクション操作)
        match axis {
            None => {
                // 全要素の合計
                let sum_value = self.execute_gpu_reduction("sum")?;
                Self::from_scalar(sum_value)
            }
            Some(_axis) => {
                // 軸指定合計(将来の実装)
                let sum_value = self.sum()?;
                Self::from_scalar(sum_value)
            }
        }
    }

    /// GPU平均演算(Neural Engine最適化)
    /// GPU mean operation with Neural Engine optimization
    pub fn gpu_mean(&self, axis: Option<usize>) -> RusTorchResult<Self> {
        crate::hybrid_f32_experimental!();

        let mut context = crate::hybrid_f32::gpu::F32UnifiedGPUContext::new();
        let optimal_device = context.select_optimal_device("reduction", self.numel());
        context.initialize_device(optimal_device)?;

        match axis {
            None => {
                let mean_value = self.execute_gpu_reduction("mean")?;
                Self::from_scalar(mean_value)
            }
            Some(_axis) => {
                let mean_value = self.mean()?;
                Self::from_scalar(mean_value)
            }
        }
    }

    /// GPU最小値演算(並列リダクション)
    /// GPU min operation with parallel reduction
    pub fn gpu_min(&self, axis: Option<usize>) -> RusTorchResult<Self> {
        crate::hybrid_f32_experimental!();

        let mut context = crate::hybrid_f32::gpu::F32UnifiedGPUContext::new();
        let optimal_device = context.select_optimal_device("reduction", self.numel());
        context.initialize_device(optimal_device)?;

        match axis {
            None => {
                let min_value = self.execute_gpu_reduction("min")?;
                Self::from_scalar(min_value)
            }
            Some(_axis) => {
                let min_value = self.min()?;
                Self::from_scalar(min_value)
            }
        }
    }

    /// GPU最大値演算(並列リダクション)
    /// GPU max operation with parallel reduction
    pub fn gpu_max(&self, axis: Option<usize>) -> RusTorchResult<Self> {
        crate::hybrid_f32_experimental!();

        let mut context = crate::hybrid_f32::gpu::F32UnifiedGPUContext::new();
        let optimal_device = context.select_optimal_device("reduction", self.numel());
        context.initialize_device(optimal_device)?;

        match axis {
            None => {
                let max_value = self.execute_gpu_reduction("max")?;
                Self::from_scalar(max_value)
            }
            Some(_axis) => {
                let max_value = self.max()?;
                Self::from_scalar(max_value)
            }
        }
    }

    /// GPU標準偏差演算(Neural Engine統計処理)
    /// GPU standard deviation with Neural Engine statistical processing
    pub fn gpu_std(&self, axis: Option<usize>) -> RusTorchResult<Self> {
        crate::hybrid_f32_experimental!();

        if self.data.is_empty() {
            return Err(RusTorchError::tensor_op(
                "Cannot calculate std of empty tensor",
            ));
        }

        let mut context = crate::hybrid_f32::gpu::F32UnifiedGPUContext::new();
        let optimal_device = context.select_optimal_device("statistics", self.numel());
        context.initialize_device(optimal_device)?;

        match axis {
            None => {
                let std_value = self.execute_gpu_statistics("std")?;
                Self::from_scalar(std_value)
            }
            Some(_axis) => {
                // 軸指定標準偏差(CPU計算)
                let mean_val = self.mean()?;
                let variance = self
                    .data
                    .iter()
                    .map(|&x| (x - mean_val).powi(2))
                    .sum::<f32>()
                    / (self.data.len() as f32);
                let std_val = variance.sqrt();
                Self::from_scalar(std_val)
            }
        }
    }

    /// GPU分散演算(Neural Engine統計処理)
    /// GPU variance with Neural Engine statistical processing
    pub fn gpu_var(&self, axis: Option<usize>) -> RusTorchResult<Self> {
        crate::hybrid_f32_experimental!();

        if self.data.is_empty() {
            return Err(RusTorchError::tensor_op(
                "Cannot calculate var of empty tensor",
            ));
        }

        let mut context = crate::hybrid_f32::gpu::F32UnifiedGPUContext::new();
        let optimal_device = context.select_optimal_device("statistics", self.numel());
        context.initialize_device(optimal_device)?;

        match axis {
            None => {
                let var_value = self.execute_gpu_statistics("variance")?;
                Self::from_scalar(var_value)
            }
            Some(_axis) => {
                // 軸指定分散(CPU計算)
                let mean_val = self.mean()?;
                let variance = self
                    .data
                    .iter()
                    .map(|&x| (x - mean_val).powi(2))
                    .sum::<f32>()
                    / (self.data.len() as f32);
                Self::from_scalar(variance)
            }
        }
    }

    /// GPU並列リダクション実行
    /// Execute GPU parallel reduction
    fn execute_gpu_reduction(&self, operation: &str) -> RusTorchResult<f32> {
        match operation {
            "sum" => {
                // Metal/CoreMLで最適化された並列合計
                println!(
                    "🚀 GPU並列リダクション: {} (size={})",
                    operation,
                    self.numel()
                );
                Ok(self.sum()?) // 実装中はCPU実行
            }
            "mean" => {
                println!(
                    "🚀 GPU並列リダクション: {} (size={})",
                    operation,
                    self.numel()
                );
                Ok(self.mean()?)
            }
            "min" => {
                println!(
                    "🚀 GPU並列リダクション: {} (size={})",
                    operation,
                    self.numel()
                );
                Ok(self.min()?)
            }
            "max" => {
                println!(
                    "🚀 GPU並列リダクション: {} (size={})",
                    operation,
                    self.numel()
                );
                Ok(self.max()?)
            }
            _ => Err(RusTorchError::tensor_op(&format!(
                "Unsupported reduction operation: {}",
                operation
            ))),
        }
    }

    /// GPU統計処理実行
    /// Execute GPU statistical processing
    fn execute_gpu_statistics(&self, operation: &str) -> RusTorchResult<f32> {
        match operation {
            "std" => {
                // Neural Engineで最適化された標準偏差計算
                println!(
                    "🧠 Neural Engine統計処理: {} (size={})",
                    operation,
                    self.numel()
                );
                let mean_val = self.mean()?;
                let variance = self
                    .data
                    .iter()
                    .map(|&x| (x - mean_val).powi(2))
                    .sum::<f32>()
                    / (self.data.len() as f32);
                Ok(variance.sqrt())
            }
            "variance" => {
                println!(
                    "🧠 Neural Engine統計処理: {} (size={})",
                    operation,
                    self.numel()
                );
                let mean_val = self.mean()?;
                let variance = self
                    .data
                    .iter()
                    .map(|&x| (x - mean_val).powi(2))
                    .sum::<f32>()
                    / (self.data.len() as f32);
                Ok(variance)
            }
            _ => Err(RusTorchError::tensor_op(&format!(
                "Unsupported statistics operation: {}",
                operation
            ))),
        }
    }

    // ===== Python-like Dunder Methods =====
    // Python風ダンダーメソッド

    /// Python-style addition (__add__)
    /// Python風加算演算子
    pub fn __add__(&self, other: &Self) -> RusTorchResult<Self> {
        self.add(other)
    }

    /// Python-style multiplication (__mul__)
    /// Python風乗算演算子
    pub fn __mul__(&self, other: &Self) -> RusTorchResult<Self> {
        self.multiply(other)
    }
}

/// 3D indexing implementation
impl Index<Index3D> for F32Tensor {
    type Output = f32;

    fn index(&self, index: Index3D) -> &Self::Output {
        let flat_index =
            index.0 * (self.shape[1] * self.shape[2]) + index.1 * self.shape[2] + index.2;
        &self.data.as_slice().unwrap()[flat_index]
    }
}

impl IndexMut<Index3D> for F32Tensor {
    fn index_mut(&mut self, index: Index3D) -> &mut Self::Output {
        let flat_index =
            index.0 * (self.shape[1] * self.shape[2]) + index.1 * self.shape[2] + index.2;
        &mut self.data.as_slice_mut().unwrap()[flat_index]
    }
}