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
//! Refactored tensor shape operations with improved maintainability
//! 保守性を向上させたテンソル形状操作(リファクタリング済み)
//!
//! ## Key Improvements / 主な改善点
//!
//! 1. **Generic Recursive Processing** - Common pattern extracted into helper functions
//! 2. **Enhanced Error Handling** - More descriptive error messages with context
//! 3. **Performance Optimizations** - Lazy evaluation and view operations where possible
//! 4. **Code Documentation** - Comprehensive examples and ownership patterns explained
//! 5. **Builder Pattern** - Fluent interface for chaining operations
//!
//! ## Architecture / アーキテクチャ
//!
//! - **Core Operations**: Basic shape manipulations (squeeze, unsqueeze, flatten)
//! - **Advanced Operations**: Transformations (roll, rotate, flip, repeat)
//! - **Utilities**: Helper functions for common patterns and validations
//! - **Builder Interface**: Chainable operations for complex transformations

use crate::error::{RusTorchError, RusTorchResult};
use crate::tensor::Tensor;
use ndarray::{ArrayD, IxDyn};
use num_traits::Float;
use std::sync::Arc;

/// Shape operation modes considering Rust ownership
/// Rustの所有権を考慮した形状操作モード
pub enum ShapeMode {
    /// Create new tensor (clone data) - always safe but potentially expensive
    /// 新しいテンソルを作成(データクローン) - 常に安全だが潜在的に高コスト
    Owned,
    /// Create view when possible, fallback to owned - optimal performance
    /// 可能な場合はビューを作成、所有にフォールバック - 最適パフォーマンス
    ViewOrOwned,
    /// Force view creation, error if not possible - zero-copy guarantee
    /// ビュー作成を強制、不可能な場合はエラー - ゼロコピー保証
    ViewOnly,
}

// =============================================================================
// HELPER FUNCTIONS FOR COMMON PATTERNS
// 共通パターンのヘルパー関数
// =============================================================================

/// Generic recursive tensor processing helper
/// 汎用再帰テンソル処理ヘルパー
struct TensorProcessor;

impl TensorProcessor {
    /// Process tensor data with generic transformation function
    /// 汎用変換関数でテンソルデータを処理
    fn process_with_transform<T, F>(
        data: &ArrayD<T>,
        source_shape: &[usize],
        target_shape: &[usize],
        transform_fn: F,
    ) -> RusTorchResult<Vec<T>>
    where
        T: Float + Clone,
        F: Fn(&[usize]) -> Vec<usize> + Copy,
    {
        let mut output = Vec::with_capacity(target_shape.iter().product());
        let mut indices = vec![0; target_shape.len()];

        Self::recursive_process(
            data,
            &mut output,
            source_shape,
            target_shape,
            transform_fn,
            &mut indices,
            0,
        )?;

        Ok(output)
    }

    /// Recursive worker function for tensor processing
    /// テンソル処理の再帰ワーカー関数
    fn recursive_process<T, F>(
        data: &ArrayD<T>,
        output: &mut Vec<T>,
        source_shape: &[usize],
        target_shape: &[usize],
        transform_fn: F,
        indices: &mut [usize],
        dim: usize,
    ) -> RusTorchResult<()>
    where
        T: Float + Clone,
        F: Fn(&[usize]) -> Vec<usize> + Copy,
    {
        if dim == target_shape.len() {
            // Base case - apply transformation
            let source_indices = transform_fn(indices);
            if let Some(&value) = data.get(source_indices.as_slice()) {
                output.push(value);
            } else {
                return Err(RusTorchError::index_out_of_bounds(
                    &source_indices,
                    source_shape,
                ));
            }
            return Ok(());
        }

        for i in 0..target_shape[dim] {
            indices[dim] = i;
            Self::recursive_process(
                data,
                output,
                source_shape,
                target_shape,
                transform_fn,
                indices,
                dim + 1,
            )?;
        }

        Ok(())
    }
}

/// Validation utilities for shape operations
/// 形状操作の検証ユーティリティ
struct ShapeValidator;

impl ShapeValidator {
    /// Validate dimension index against tensor shape
    /// テンソル形状に対する次元インデックスの検証
    fn validate_dimension<T: Float + 'static>(
        tensor: &Tensor<T>,
        dim: usize,
    ) -> RusTorchResult<()> {
        if dim >= tensor.shape().len() {
            return Err(RusTorchError::invalid_dimension(
                dim,
                tensor.shape().len() - 1,
            ));
        }
        Ok(())
    }

    /// Validate that two shapes are broadcast compatible
    /// 2つの形状がブロードキャスト互換かを検証
    fn validate_broadcast_compatibility(shape1: &[usize], shape2: &[usize]) -> RusTorchResult<()> {
        let max_dims = shape1.len().max(shape2.len());

        for i in 0..max_dims {
            let dim1 = shape1
                .get(shape1.len().saturating_sub(max_dims - i))
                .copied()
                .unwrap_or(1);
            let dim2 = shape2
                .get(shape2.len().saturating_sub(max_dims - i))
                .copied()
                .unwrap_or(1);

            if dim1 != dim2 && dim1 != 1 && dim2 != 1 {
                return Err(RusTorchError::shape_mismatch(shape1, shape2));
            }
        }

        Ok(())
    }

    /// Normalize negative dimension index
    /// 負の次元インデックスを正規化
    fn normalize_dimension(dim: isize, ndim: usize) -> RusTorchResult<usize> {
        let normalized = if dim < 0 {
            (ndim as isize + dim) as usize
        } else {
            dim as usize
        };

        if normalized >= ndim {
            return Err(RusTorchError::invalid_dimension(normalized, ndim - 1));
        }

        Ok(normalized)
    }
}

impl<T: Float + Clone + 'static> Tensor<T> {
    /// Remove singleton dimensions (size 1) from tensor
    /// テンソルから単一次元(サイズ1)を削除
    ///
    /// # Ownership Patterns / 所有権パターン
    /// - `squeeze()` - Always creates new tensor (owned)
    /// - `squeeze_view()` - Attempts zero-copy view, fallback to owned
    /// - `squeeze_inplace()` - Modifies existing tensor (requires &mut)
    ///
    /// # Examples
    /// ```rust
    /// use rustorch::prelude::Tensor;
    /// let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0], vec![1, 3, 1]);
    ///
    /// // Always safe - creates new tensor
    /// let squeezed = tensor.squeeze();
    /// assert_eq!(squeezed.shape(), &[3]);
    ///
    /// // Zero-copy when possible
    /// let squeezed_view = tensor.squeeze_view().unwrap();
    ///
    /// // In-place modification
    /// let mut tensor_mut = tensor.clone();
    /// tensor_mut.squeeze_inplace().unwrap();
    /// ```
    pub fn squeeze(&self) -> Self {
        self.squeeze_with_mode(ShapeMode::Owned)
            .expect("Owned squeeze should never fail")
    }

    /// Remove singleton dimensions with zero-copy optimization
    /// ゼロコピー最適化で単一次元を削除
    pub fn squeeze_view(&self) -> RusTorchResult<Self> {
        self.squeeze_with_mode(ShapeMode::ViewOrOwned)
    }

    /// Remove singleton dimensions in-place (requires mutable reference)
    /// 単一次元をインプレースで削除(可変参照が必要)
    pub fn squeeze_inplace(&mut self) -> RusTorchResult<()> {
        let current_shape = self.data.shape();
        let new_shape: Vec<usize> = current_shape
            .iter()
            .filter(|&&dim| dim != 1)
            .copied()
            .collect();

        // Ensure at least one dimension remains
        let final_shape = if new_shape.is_empty() {
            vec![1]
        } else {
            new_shape
        };

        // Try in-place reshape (zero-copy when layout allows)
        match self.data.clone().into_shape_with_order(final_shape) {
            Ok(reshaped) => {
                self.data = reshaped;
                Ok(())
            }
            Err(_) => Err(RusTorchError::InvalidOperation {
                operation: "squeeze_inplace".to_string(),
                message: "Cannot perform in-place squeeze due to layout constraints".to_string(),
            }),
        }
    }

    /// Remove singleton dimensions from specific dimension
    /// 特定の次元から単一次元を削除
    pub fn squeeze_dim(&self, dim: usize) -> RusTorchResult<Self> {
        let current_shape = self.data.shape();

        if dim >= current_shape.len() {
            return Err(RusTorchError::InvalidDimension(format!(
                "Invalid dimension {} (max: {})",
                dim,
                current_shape.len() - 1
            )));
        }

        if current_shape[dim] != 1 {
            return Err(RusTorchError::InvalidOperation {
                operation: "squeeze_dim".to_string(),
                message: format!(
                    "Cannot squeeze dimension {} with size {}",
                    dim, current_shape[dim]
                ),
            });
        }

        let mut new_shape = current_shape.to_vec();
        new_shape.remove(dim);

        if new_shape.is_empty() {
            new_shape.push(1);
        }

        let reshaped_data = self
            .data
            .clone()
            .into_shape_with_order(new_shape)
            .map_err(|_| RusTorchError::InvalidOperation {
                operation: "squeeze_dim".to_string(),
                message: "Failed to reshape tensor".to_string(),
            })?;

        Ok(Tensor::new(reshaped_data))
    }

    /// Add singleton dimension at specified position
    /// 指定位置に単一次元を追加
    ///
    /// # Ownership Patterns / 所有権パターン
    /// - `unsqueeze()` - Always creates new tensor (owned)
    /// - `unsqueeze_view()` - Attempts zero-copy view
    /// - `unsqueeze_inplace()` - Modifies existing tensor
    ///
    /// # Examples
    /// ```rust
    /// use rustorch::prelude::Tensor;
    /// let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0], vec![3]);
    ///
    /// // Add dimension at start
    /// let unsqueezed = tensor.unsqueeze(0).unwrap();
    /// assert_eq!(unsqueezed.shape(), &[1, 3]);
    ///
    /// // Add dimension at end  
    /// let unsqueezed = tensor.unsqueeze(1).unwrap();
    /// assert_eq!(unsqueezed.shape(), &[3, 1]);
    /// ```
    pub fn unsqueeze(&self, dim: usize) -> RusTorchResult<Self> {
        self.unsqueeze_with_mode(dim, ShapeMode::Owned)
    }

    /// Add singleton dimension with zero-copy optimization
    /// ゼロコピー最適化で単一次元を追加
    pub fn unsqueeze_view(&self, dim: usize) -> RusTorchResult<Self> {
        self.unsqueeze_with_mode(dim, ShapeMode::ViewOrOwned)
    }

    /// Add singleton dimension in-place
    /// 単一次元をインプレースで追加
    pub fn unsqueeze_inplace(&mut self, dim: usize) -> RusTorchResult<()> {
        let mut new_shape = self.data.shape().to_vec();

        if dim > new_shape.len() {
            return Err(RusTorchError::InvalidDimension(format!(
                "Invalid dimension {} (max: {})",
                dim,
                new_shape.len()
            )));
        }

        new_shape.insert(dim, 1);

        match self.data.clone().into_shape_with_order(new_shape) {
            Ok(reshaped) => {
                self.data = reshaped;
                Ok(())
            }
            Err(_) => Err(RusTorchError::InvalidOperation {
                operation: "unsqueeze_inplace".to_string(),
                message: "Cannot perform in-place unsqueeze due to layout constraints".to_string(),
            }),
        }
    }

    /// Expand tensor dimensions through broadcasting (ownership-aware version)
    /// ブロードキャストによってテンソル次元を拡張(所有権対応版)
    ///
    /// # Ownership Considerations / 所有権の考慮事項
    /// Expand operations typically require data duplication, so we provide:
    /// - `expand_owned()` - Creates new tensor with explicit memory allocation
    /// - `expand_lazy()` - Returns lazy view that computes on access (memory efficient)
    /// - `expand_shared()` - Uses shared ownership with Arc for memory efficiency
    ///
    /// # Examples
    /// ```rust
    /// use rustorch::prelude::Tensor;
    /// let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0], vec![1, 3]);
    ///
    /// // Expand to larger shape
    /// let expanded = tensor.expand_owned(&[4, 3]).unwrap();
    /// assert_eq!(expanded.shape(), &[4, 3]);
    ///
    /// // Memory-efficient lazy expansion
    /// let lazy_expanded = tensor.expand_lazy(&[4, 3]).unwrap();
    /// ```
    pub fn expand_owned(&self, target_shape: &[usize]) -> RusTorchResult<Self> {
        self.expand_with_mode(target_shape, ShapeMode::Owned)
    }

    /// Expand with shared ownership for memory efficiency
    /// メモリ効率のための共有所有権で拡張
    pub fn expand_shared(&self, target_shape: &[usize]) -> RusTorchResult<Arc<Self>> {
        let expanded = self.expand_with_mode(target_shape, ShapeMode::ViewOrOwned)?;
        Ok(Arc::new(expanded))
    }

    /// Lazy expand that defers computation until access
    /// アクセス時まで計算を遅延する遅延拡張
    pub fn expand_lazy(&self, target_shape: &[usize]) -> RusTorchResult<LazyExpandedTensor<T>> {
        // Validate expansion is possible
        self.validate_expansion(target_shape)?;

        Ok(LazyExpandedTensor {
            source: Arc::new(self.clone()),
            target_shape: target_shape.to_vec(),
        })
    }

    /// Flatten tensor dimensions into 1D (ownership-aware version)
    /// テンソル次元を1Dに平坦化(所有権対応版)
    ///
    /// # Ownership Patterns / 所有権パターン
    /// - `flatten_owned()` - Always creates new 1D tensor
    /// - `flatten_range()` - Flatten specific dimension range  
    /// - `flatten_inplace()` - In-place flattening when possible
    /// - `flatten_view()` - Zero-copy view when layout allows
    ///
    /// # Examples
    /// ```rust
    /// use rustorch::prelude::Tensor;
    /// let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2]);
    ///
    /// // Full flatten
    /// let flattened = tensor.flatten_owned();
    /// assert_eq!(flattened.shape(), &[4]);
    ///
    /// // Flatten from specific dimension
    /// let partial_flatten = tensor.flatten_range(1, None).unwrap();
    /// assert_eq!(partial_flatten.shape(), &[2, 2]);
    /// ```
    pub fn flatten_owned(&self) -> Self {
        let total_elements = self.data.len();
        let flattened_data = self
            .data
            .clone()
            .into_shape_with_order(vec![total_elements])
            .expect("Flatten should always succeed");
        Tensor::new(flattened_data)
    }

    /// Flatten specific dimension range
    /// 特定の次元範囲を平坦化
    pub fn flatten_range(&self, start_dim: usize, end_dim: Option<usize>) -> RusTorchResult<Self> {
        let shape = self.shape();
        let end_dim = end_dim.unwrap_or(shape.len() - 1);

        if start_dim >= shape.len() || end_dim >= shape.len() || start_dim > end_dim {
            return Err(RusTorchError::InvalidDimension(format!(
                "Invalid dimension range [{}, {}] for tensor with {} dimensions",
                start_dim,
                end_dim,
                shape.len()
            )));
        }

        let mut new_shape = Vec::new();
        new_shape.extend_from_slice(&shape[..start_dim]);

        let flattened_size: usize = shape[start_dim..=end_dim].iter().product();
        new_shape.push(flattened_size);

        new_shape.extend_from_slice(&shape[end_dim + 1..]);

        let reshaped_data = self
            .data
            .clone()
            .into_shape_with_order(new_shape)
            .map_err(|_| RusTorchError::InvalidOperation {
                operation: "flatten_range".to_string(),
                message: "Failed to flatten dimension range".to_string(),
            })?;

        Ok(Tensor::new(reshaped_data))
    }

    /// Flatten in-place when layout allows
    /// レイアウトが許可する場合のインプレース平坦化
    pub fn flatten_inplace(&mut self) -> RusTorchResult<()> {
        let total_elements = self.data.len();

        match self
            .data
            .clone()
            .into_shape_with_order(vec![total_elements])
        {
            Ok(flattened) => {
                self.data = flattened;
                Ok(())
            }
            Err(_) => Err(RusTorchError::InvalidOperation {
                operation: "flatten_inplace".to_string(),
                message: "Cannot perform in-place flatten due to layout constraints".to_string(),
            }),
        }
    }

    /// Zero-copy flatten when memory layout allows
    /// メモリレイアウトが許可する場合のゼロコピー平坦化
    pub fn flatten_view(&self) -> RusTorchResult<Self> {
        let total_elements = self.data.len();

        // Check if data is contiguous for zero-copy view
        if self.is_contiguous() {
            let view_data = self
                .data
                .clone()
                .into_shape_with_order(vec![total_elements])
                .map_err(|_| RusTorchError::InvalidOperation {
                    operation: "flatten_view".to_string(),
                    message: "Cannot create view due to non-contiguous layout".to_string(),
                })?;
            Ok(Tensor::new(view_data))
        } else {
            Err(RusTorchError::InvalidOperation {
                operation: "flatten_view".to_string(),
                message: "Cannot create zero-copy view from non-contiguous tensor".to_string(),
            })
        }
    }

    // Private helper methods

    fn squeeze_with_mode(&self, mode: ShapeMode) -> RusTorchResult<Self> {
        let current_shape = self.data.shape();
        let new_shape: Vec<usize> = current_shape
            .iter()
            .filter(|&&dim| dim != 1)
            .copied()
            .collect();

        let final_shape = if new_shape.is_empty() {
            vec![1]
        } else {
            new_shape
        };

        match mode {
            ShapeMode::Owned => {
                let reshaped = self
                    .data
                    .clone()
                    .into_shape_with_order(final_shape)
                    .map_err(|_| RusTorchError::InvalidOperation {
                        operation: "squeeze".to_string(),
                        message: "Failed to squeeze tensor".to_string(),
                    })?;
                Ok(Tensor::new(reshaped))
            }
            ShapeMode::ViewOrOwned => {
                // Try view first, fallback to owned
                if self.is_contiguous() {
                    self.squeeze_with_mode(ShapeMode::ViewOnly)
                        .or_else(|_| self.squeeze_with_mode(ShapeMode::Owned))
                } else {
                    self.squeeze_with_mode(ShapeMode::Owned)
                }
            }
            ShapeMode::ViewOnly => {
                if !self.is_contiguous() {
                    return Err(RusTorchError::InvalidOperation {
                        operation: "squeeze_view".to_string(),
                        message: "Cannot create view from non-contiguous tensor".to_string(),
                    });
                }
                let reshaped = self
                    .data
                    .clone()
                    .into_shape_with_order(final_shape)
                    .map_err(|_| RusTorchError::InvalidOperation {
                        operation: "squeeze_view".to_string(),
                        message: "Failed to create view".to_string(),
                    })?;
                Ok(Tensor::new(reshaped))
            }
        }
    }

    fn unsqueeze_with_mode(&self, dim: usize, mode: ShapeMode) -> RusTorchResult<Self> {
        let mut new_shape = self.data.shape().to_vec();

        if dim > new_shape.len() {
            return Err(RusTorchError::InvalidDimension(format!(
                "Invalid dimension {} (max: {})",
                dim,
                new_shape.len()
            )));
        }

        new_shape.insert(dim, 1);

        match mode {
            ShapeMode::Owned => {
                let reshaped =
                    self.data
                        .clone()
                        .into_shape_with_order(new_shape)
                        .map_err(|_| RusTorchError::InvalidOperation {
                            operation: "unsqueeze".to_string(),
                            message: "Failed to unsqueeze tensor".to_string(),
                        })?;
                Ok(Tensor::new(reshaped))
            }
            ShapeMode::ViewOrOwned => {
                if self.is_contiguous() {
                    self.unsqueeze_with_mode(dim, ShapeMode::ViewOnly)
                        .or_else(|_| self.unsqueeze_with_mode(dim, ShapeMode::Owned))
                } else {
                    self.unsqueeze_with_mode(dim, ShapeMode::Owned)
                }
            }
            ShapeMode::ViewOnly => {
                if !self.is_contiguous() {
                    return Err(RusTorchError::InvalidOperation {
                        operation: "unsqueeze_view".to_string(),
                        message: "Cannot create view from non-contiguous tensor".to_string(),
                    });
                }
                let reshaped =
                    self.data
                        .clone()
                        .into_shape_with_order(new_shape)
                        .map_err(|_| RusTorchError::InvalidOperation {
                            operation: "unsqueeze_view".to_string(),
                            message: "Failed to create view".to_string(),
                        })?;
                Ok(Tensor::new(reshaped))
            }
        }
    }

    fn expand_with_mode(&self, target_shape: &[usize], mode: ShapeMode) -> RusTorchResult<Self> {
        self.validate_expansion(target_shape)?;

        match mode {
            ShapeMode::Owned => self.expand_impl(target_shape),
            ShapeMode::ViewOrOwned => {
                // For expand, view is rarely possible due to data duplication needs
                self.expand_impl(target_shape)
            }
            ShapeMode::ViewOnly => Err(RusTorchError::InvalidOperation {
                operation: "expand_view".to_string(),
                message: "Expand operation cannot be performed as zero-copy view".to_string(),
            }),
        }
    }

    fn expand_impl(&self, target_shape: &[usize]) -> RusTorchResult<Self> {
        let mut expanded_data = Vec::new();
        let total_elements: usize = target_shape.iter().product();
        expanded_data.reserve(total_elements);

        self.expand_recursive(
            &mut expanded_data,
            target_shape,
            &vec![0; target_shape.len()],
            0,
        )?;

        Ok(Tensor::from_vec(expanded_data, target_shape.to_vec()))
    }

    fn validate_expansion(&self, target_shape: &[usize]) -> RusTorchResult<()> {
        let self_shape = self.shape();

        if target_shape.len() < self_shape.len() {
            return Err(RusTorchError::InvalidOperation {
                operation: "expand".to_string(),
                message: format!(
                    "Target shape must have at least {} dimensions, got {}",
                    self_shape.len(),
                    target_shape.len()
                ),
            });
        }

        let ndim_diff = target_shape.len() - self_shape.len();

        for (i, (&target_dim, &self_dim)) in target_shape
            .iter()
            .skip(ndim_diff)
            .zip(self_shape.iter())
            .enumerate()
        {
            if self_dim != 1 && self_dim != target_dim {
                return Err(RusTorchError::InvalidOperation {
                    operation: "expand".to_string(),
                    message: format!(
                        "Cannot expand dimension {} from {} to {} (must be 1 or equal)",
                        i + ndim_diff,
                        self_dim,
                        target_dim
                    ),
                });
            }
        }

        Ok(())
    }

    fn expand_recursive(
        &self,
        output: &mut Vec<T>,
        target_shape: &[usize],
        indices: &[usize],
        dim: usize,
    ) -> RusTorchResult<()> {
        if dim == target_shape.len() {
            // Base case - copy element
            let self_indices = self.compute_source_indices(indices)?;
            if let Some(&value) = self.data.get(self_indices.as_slice()) {
                output.push(value);
            } else {
                return Err(RusTorchError::index_out_of_bounds(&[], &[]));
            }
            return Ok(());
        }

        let mut new_indices = indices.to_vec();
        for i in 0..target_shape[dim] {
            new_indices[dim] = i;
            self.expand_recursive(output, target_shape, &new_indices, dim + 1)?;
        }

        Ok(())
    }

    fn compute_source_indices(&self, target_indices: &[usize]) -> RusTorchResult<Vec<usize>> {
        let self_shape = self.shape();
        let ndim_diff = target_indices.len() - self_shape.len();

        let mut source_indices = Vec::new();

        for (i, &target_idx) in target_indices.iter().skip(ndim_diff).enumerate() {
            let self_dim = self_shape[i];
            if self_dim == 1 {
                source_indices.push(0);
            } else {
                source_indices.push(target_idx % self_dim);
            }
        }

        Ok(source_indices)
    }

    // Helper methods for new operations

    fn repeat_recursive(
        &self,
        output: &mut Vec<T>,
        output_shape: &[usize],
        repeats: &[usize],
        indices: &[usize],
        dim: usize,
    ) -> RusTorchResult<()> {
        if dim == output_shape.len() {
            // Base case - copy element from source
            let source_indices =
                self.compute_repeat_source_indices(indices, output_shape, repeats)?;
            if let Some(&value) = self.data.get(source_indices.as_slice()) {
                output.push(value);
            } else {
                return Err(RusTorchError::index_out_of_bounds(&[], &[]));
            }
            return Ok(());
        }

        let mut new_indices = indices.to_vec();
        for i in 0..output_shape[dim] {
            new_indices[dim] = i;
            self.repeat_recursive(output, output_shape, repeats, &new_indices, dim + 1)?;
        }

        Ok(())
    }

    fn compute_repeat_source_indices(
        &self,
        output_indices: &[usize],
        output_shape: &[usize],
        repeats: &[usize],
    ) -> RusTorchResult<Vec<usize>> {
        let self_shape = self.shape();
        let mut source_indices = Vec::new();

        // Handle dimension adjustment
        let ndim_diff = if output_shape.len() > self_shape.len() {
            output_shape.len() - self_shape.len()
        } else {
            0
        };

        for (i, &output_idx) in output_indices.iter().enumerate() {
            if i < ndim_diff {
                // Skip extra leading dimensions
                continue;
            }

            let self_dim_idx = i - ndim_diff;
            if self_dim_idx < self_shape.len() {
                let self_dim_size = self_shape[self_dim_idx];
                let repeat_count = repeats[i];
                source_indices.push(output_idx / repeat_count);
            }
        }

        Ok(source_indices)
    }

    fn repeat_interleave_along_dim(&self, repeats: usize, dim: usize) -> RusTorchResult<Self> {
        let shape = self.shape();

        if dim >= shape.len() {
            return Err(RusTorchError::InvalidDimension(format!(
                "Invalid dimension {} (max: {})",
                dim,
                shape.len() - 1
            )));
        }

        let mut output_shape = shape.to_vec();
        output_shape[dim] *= repeats;

        let mut output_data = Vec::new();
        let total_elements: usize = output_shape.iter().product();
        output_data.reserve(total_elements);

        // Generate indices for output tensor
        let mut indices = vec![0; output_shape.len()];
        self.repeat_interleave_recursive(
            &mut output_data,
            &output_shape,
            repeats,
            dim,
            &mut indices,
            0,
        )?;

        Ok(Tensor::from_vec(output_data, output_shape))
    }

    fn repeat_interleave_recursive(
        &self,
        output: &mut Vec<T>,
        output_shape: &[usize],
        repeats: usize,
        target_dim: usize,
        indices: &mut [usize],
        dim: usize,
    ) -> RusTorchResult<()> {
        if dim == output_shape.len() {
            // Base case - compute source index and copy element
            let mut source_indices = indices.to_vec();
            if target_dim < source_indices.len() {
                source_indices[target_dim] = indices[target_dim] / repeats;
            }

            if let Some(&value) = self.data.get(source_indices.as_slice()) {
                output.push(value);
            } else {
                return Err(RusTorchError::index_out_of_bounds(&[], &[]));
            }
            return Ok(());
        }

        for i in 0..output_shape[dim] {
            indices[dim] = i;
            self.repeat_interleave_recursive(
                output,
                output_shape,
                repeats,
                target_dim,
                indices,
                dim + 1,
            )?;
        }

        Ok(())
    }

    fn roll_along_dimension(&self, shift: usize, dim: usize) -> RusTorchResult<Self> {
        let shape = self.shape();
        let dim_size = shape[dim];

        if shift >= dim_size {
            return Err(RusTorchError::InvalidOperation {
                operation: "roll".to_string(),
                message: "Shift amount exceeds dimension size".to_string(),
            });
        }

        let mut output_data = Vec::with_capacity(self.data.len());
        let mut indices = vec![0; shape.len()];

        self.roll_recursive(&mut output_data, shape, shift, dim, &mut indices, 0)?;

        Ok(Tensor::from_vec(output_data, shape.to_vec()))
    }

    fn roll_recursive(
        &self,
        output: &mut Vec<T>,
        shape: &[usize],
        shift: usize,
        target_dim: usize,
        indices: &mut [usize],
        dim: usize,
    ) -> RusTorchResult<()> {
        if dim == shape.len() {
            // Base case - compute rolled source index
            let mut source_indices = indices.to_vec();
            if target_dim < source_indices.len() {
                let dim_size = shape[target_dim];
                let rolled_idx = (indices[target_dim] + dim_size - shift) % dim_size;
                source_indices[target_dim] = rolled_idx;
            }

            if let Some(&value) = self.data.get(source_indices.as_slice()) {
                output.push(value);
            } else {
                return Err(RusTorchError::index_out_of_bounds(&[], &[]));
            }
            return Ok(());
        }

        for i in 0..shape[dim] {
            indices[dim] = i;
            self.roll_recursive(output, shape, shift, target_dim, indices, dim + 1)?;
        }

        Ok(())
    }

    fn rot90_once(&self, dim0: usize, dim1: usize) -> RusTorchResult<Self> {
        let shape = self.shape();
        let mut new_shape = shape.to_vec();
        new_shape.swap(dim0, dim1);

        let mut output_data = Vec::with_capacity(self.data.len());
        let mut indices = vec![0; shape.len()];

        self.rot90_recursive(
            &mut output_data,
            shape,
            &new_shape,
            dim0,
            dim1,
            &mut indices,
            0,
        )?;

        Ok(Tensor::from_vec(output_data, new_shape))
    }

    fn rot90_recursive(
        &self,
        output: &mut Vec<T>,
        original_shape: &[usize],
        new_shape: &[usize],
        dim0: usize,
        dim1: usize,
        indices: &mut [usize],
        dim: usize,
    ) -> RusTorchResult<()> {
        if dim == new_shape.len() {
            // Base case - compute rotated source indices
            let mut source_indices = indices.to_vec();

            // Apply 90-degree rotation transformation
            let old_i = indices[dim0];
            let old_j = indices[dim1];

            source_indices[dim0] = original_shape[dim1] - 1 - old_j;
            source_indices[dim1] = old_i;

            if let Some(&value) = self.data.get(source_indices.as_slice()) {
                output.push(value);
            } else {
                return Err(RusTorchError::index_out_of_bounds(&[], &[]));
            }
            return Ok(());
        }

        for i in 0..new_shape[dim] {
            indices[dim] = i;
            self.rot90_recursive(
                output,
                original_shape,
                new_shape,
                dim0,
                dim1,
                indices,
                dim + 1,
            )?;
        }

        Ok(())
    }

    fn flip_single_dim(&self, dim: usize) -> RusTorchResult<Self> {
        let shape = self.shape();
        let dim_size = shape[dim];

        let mut output_data = Vec::with_capacity(self.data.len());
        let mut indices = vec![0; shape.len()];

        self.flip_recursive(&mut output_data, shape, dim, &mut indices, 0)?;

        Ok(Tensor::from_vec(output_data, shape.to_vec()))
    }

    fn flip_recursive(
        &self,
        output: &mut Vec<T>,
        shape: &[usize],
        flip_dim: usize,
        indices: &mut [usize],
        dim: usize,
    ) -> RusTorchResult<()> {
        if dim == shape.len() {
            // Base case - compute flipped source index
            let mut source_indices = indices.to_vec();
            if flip_dim < source_indices.len() {
                let dim_size = shape[flip_dim];
                source_indices[flip_dim] = dim_size - 1 - indices[flip_dim];
            }

            if let Some(&value) = self.data.get(source_indices.as_slice()) {
                output.push(value);
            } else {
                return Err(RusTorchError::index_out_of_bounds(&[], &[]));
            }
            return Ok(());
        }

        for i in 0..shape[dim] {
            indices[dim] = i;
            self.flip_recursive(output, shape, flip_dim, indices, dim + 1)?;
        }

        Ok(())
    }

    /// Check if tensor data is contiguous in memory
    /// テンソルデータがメモリ内で連続しているかチェック
    pub fn is_contiguous(&self) -> bool {
        self.data.is_standard_layout()
    }

    // Note: shape() and numel() methods are defined in core.rs
    // 注意: shape()とnumel()メソッドはcore.rsで定義されています

    /// Check if this tensor can broadcast with another tensor
    /// このテンソルが別のテンソルとブロードキャスト可能かチェック
    pub fn can_broadcast_with(&self, other: &Self) -> bool {
        let self_shape = self.shape();
        let other_shape = other.shape();

        let max_dims = self_shape.len().max(other_shape.len());

        for i in 0..max_dims {
            let self_dim = if i < self_shape.len() {
                self_shape[self_shape.len() - 1 - i]
            } else {
                1
            };

            let other_dim = if i < other_shape.len() {
                other_shape[other_shape.len() - 1 - i]
            } else {
                1
            };

            if self_dim != 1 && other_dim != 1 && self_dim != other_dim {
                return false;
            }
        }

        true
    }

    /// Broadcast two tensors to compatible shapes
    /// 2つのテンソルを互換形状にブロードキャスト
    pub fn broadcast_with(&self, other: &Self) -> RusTorchResult<(Self, Self)> {
        if !self.can_broadcast_with(other) {
            return Err(RusTorchError::InvalidOperation {
                operation: "broadcast".to_string(),
                message: format!(
                    "Cannot broadcast shapes {:?} and {:?}",
                    self.shape(),
                    other.shape()
                ),
            });
        }

        let self_shape = self.shape();
        let other_shape = other.shape();
        let max_dims = self_shape.len().max(other_shape.len());

        let mut broadcast_shape = Vec::new();

        for i in 0..max_dims {
            let self_dim = if i < max_dims - self_shape.len() {
                1
            } else {
                self_shape[i - (max_dims - self_shape.len())]
            };

            let other_dim = if i < max_dims - other_shape.len() {
                1
            } else {
                other_shape[i - (max_dims - other_shape.len())]
            };

            broadcast_shape.push(self_dim.max(other_dim));
        }

        let broadcasted_self = if self.shape() == broadcast_shape.as_slice() {
            self.clone()
        } else {
            self.expand_owned(&broadcast_shape)?
        };

        let broadcasted_other = if other.shape() == broadcast_shape.as_slice() {
            other.clone()
        } else {
            other.expand_owned(&broadcast_shape)?
        };

        Ok((broadcasted_self, broadcasted_other))
    }

    /// Add singleton dimension (alias for unsqueeze for compatibility)
    /// 単一次元追加(互換性のためのunsqueezeエイリアス)
    pub fn expand_dims(&self, axis: usize) -> RusTorchResult<Self> {
        self.unsqueeze(axis)
    }

    /// Expand tensor to match the shape of another tensor (PyTorch expand_as compatibility)
    /// 他のテンソルの形状に合わせて拡張(PyTorch expand_as互換)
    ///
    /// # Examples
    /// ```rust
    /// use rustorch::prelude::Tensor;
    /// let tensor = Tensor::from_vec(vec![1.0, 2.0], vec![1, 2]);
    /// let target = Tensor::from_vec(vec![0.0; 6], vec![3, 2]);
    /// let expanded = tensor.expand_as(&target).unwrap();
    /// assert_eq!(expanded.shape(), target.shape());
    /// ```
    pub fn expand_as(&self, other: &Self) -> RusTorchResult<Self> {
        self.expand_owned(other.shape())
    }

    /// Unflatten a tensor dimension into multiple dimensions
    /// テンソルの次元を複数の次元に復元
    ///
    /// # Arguments
    /// * `dim` - Dimension to unflatten  
    /// * `sizes` - Target sizes for new dimensions
    ///
    /// # Examples
    /// ```rust
    /// use rustorch::prelude::Tensor;
    /// let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], vec![6]);
    /// let unflattened = tensor.unflatten(0, &[2, 3]).unwrap();
    /// assert_eq!(unflattened.shape(), &[2, 3]);
    /// ```
    pub fn unflatten(&self, dim: usize, sizes: &[usize]) -> RusTorchResult<Self> {
        let current_shape = self.shape();

        if dim >= current_shape.len() {
            return Err(RusTorchError::InvalidDimension(format!(
                "Invalid dimension {} (max: {})",
                dim,
                current_shape.len() - 1
            )));
        }

        // Validate that product of sizes matches the dimension size
        let sizes_product: usize = sizes.iter().product();
        if sizes_product != current_shape[dim] {
            return Err(RusTorchError::InvalidOperation {
                operation: "unflatten".to_string(),
                message: format!(
                    "Cannot unflatten dimension of size {} into sizes {:?} (product: {})",
                    current_shape[dim], sizes, sizes_product
                ),
            });
        }

        // Build new shape
        let mut new_shape = Vec::new();
        new_shape.extend_from_slice(&current_shape[..dim]);
        new_shape.extend_from_slice(sizes);
        new_shape.extend_from_slice(&current_shape[dim + 1..]);

        // Reshape tensor
        let reshaped_data = self
            .data
            .clone()
            .into_shape_with_order(new_shape)
            .map_err(|_| RusTorchError::InvalidOperation {
                operation: "unflatten".to_string(),
                message: "Failed to unflatten tensor".to_string(),
            })?;

        Ok(Tensor::new(reshaped_data))
    }

    /// Repeat tensor along specified dimensions (PyTorch repeat compatibility)
    /// 指定された次元に沿ってテンソルを繰り返し(PyTorch repeat互換)
    ///
    /// # Arguments
    /// * `repeats` - Number of repetitions for each dimension
    ///
    /// # Examples
    /// ```rust
    /// use rustorch::prelude::Tensor;
    /// let tensor = Tensor::from_vec(vec![1.0, 2.0], vec![2]);
    /// let repeated = tensor.repeat(&[3, 2]).unwrap();
    /// assert_eq!(repeated.shape(), &[3, 4]); // [3, 2*2]
    /// ```
    pub fn repeat(&self, repeats: &[usize]) -> RusTorchResult<Self> {
        let current_shape = self.shape();

        // Handle dimension mismatch by padding with 1s
        let (adjusted_shape, adjusted_repeats) = if repeats.len() > current_shape.len() {
            let padding = repeats.len() - current_shape.len();
            let mut padded_shape = vec![1; padding];
            padded_shape.extend_from_slice(current_shape);
            (padded_shape, repeats.to_vec())
        } else if repeats.len() < current_shape.len() {
            let padding = current_shape.len() - repeats.len();
            let mut padded_repeats = vec![1; padding];
            padded_repeats.extend_from_slice(repeats);
            (current_shape.to_vec(), padded_repeats)
        } else {
            (current_shape.to_vec(), repeats.to_vec())
        };

        // Calculate output shape
        let output_shape: Vec<usize> = adjusted_shape
            .iter()
            .zip(adjusted_repeats.iter())
            .map(|(&dim, &rep)| dim * rep)
            .collect();

        // Generate repeated data
        let mut output_data = Vec::new();
        let total_elements: usize = output_shape.iter().product();
        output_data.reserve(total_elements);

        self.repeat_recursive(
            &mut output_data,
            &output_shape,
            &adjusted_repeats,
            &vec![0; output_shape.len()],
            0,
        )?;

        Ok(Tensor::from_vec(output_data, output_shape))
    }

    /// Repeat elements of tensor along specified dimension
    /// 指定次元に沿ってテンソルの要素を繰り返し
    ///
    /// # Arguments  
    /// * `repeats` - Number of repetitions for each element (scalar or tensor)
    /// * `dim` - Dimension along which to repeat (optional)
    ///
    /// # Examples
    /// ```rust
    /// use rustorch::prelude::Tensor;
    /// let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0], vec![3]);
    /// let repeated = tensor.repeat_interleave_scalar(2, Some(0)).unwrap();
    /// assert_eq!(repeated.shape(), &[6]); // Each element repeated twice
    /// ```
    pub fn repeat_interleave_scalar(
        &self,
        repeats: usize,
        dim: Option<usize>,
    ) -> RusTorchResult<Self> {
        match dim {
            Some(d) => self.repeat_interleave_along_dim(repeats, d),
            None => {
                // Flatten and repeat each element
                let flattened = self.flatten_owned();
                let mut output_data = Vec::new();

                for &value in flattened.data.iter() {
                    for _ in 0..repeats {
                        output_data.push(value);
                    }
                }

                let output_len = output_data.len();
                Ok(Tensor::from_vec(output_data, vec![output_len]))
            }
        }
    }

    /// Roll tensor along specified dimensions
    /// 指定された次元に沿ってテンソルをロール
    ///
    /// # Arguments
    /// * `shifts` - Number of places to shift
    /// * `dims` - Dimensions to roll along (optional)
    ///
    /// # Examples
    /// ```rust
    /// use rustorch::prelude::Tensor;
    /// let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![4]);
    /// let rolled = tensor.roll_1d(1, Some(0)).unwrap();
    /// // Result: [4.0, 1.0, 2.0, 3.0]
    /// ```
    pub fn roll_1d(&self, shifts: isize, dim: Option<usize>) -> RusTorchResult<Self> {
        let shape = self.shape();

        match dim {
            Some(d) => {
                if d >= shape.len() {
                    return Err(RusTorchError::InvalidDimension(format!(
                        "Invalid dimension {} (max: {})",
                        d,
                        shape.len() - 1
                    )));
                }

                let dim_size = shape[d] as isize;
                let effective_shift = ((shifts % dim_size) + dim_size) % dim_size;

                if effective_shift == 0 {
                    return Ok(self.clone());
                }

                self.roll_along_dimension(effective_shift as usize, d)
            }
            None => {
                // Roll flattened tensor
                let flattened = self.flatten_owned();
                let data = flattened.data.as_slice().unwrap();
                let len = data.len() as isize;
                let effective_shift = ((shifts % len) + len) % len;

                if effective_shift == 0 {
                    return Ok(self.clone());
                }

                let mut output_data = Vec::with_capacity(data.len());
                let shift = effective_shift as usize;

                output_data.extend_from_slice(&data[data.len() - shift..]);
                output_data.extend_from_slice(&data[..data.len() - shift]);

                let rolled_flat = Tensor::from_vec(output_data, vec![data.len()]);
                rolled_flat.view_shape(shape)
            }
        }
    }

    /// Rotate tensor 90 degrees in the plane specified by dims
    /// 指定された次元平面でテンソルを90度回転
    ///
    /// # Arguments
    /// * `k` - Number of 90-degree rotations
    /// * `dims` - Two dimensions defining the rotation plane
    ///
    /// # Examples
    /// ```rust
    /// use rustorch::prelude::Tensor;
    /// let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2]);
    /// let rotated = tensor.rot90(1, &[0, 1]).unwrap();
    /// // 90-degree rotation
    /// ```
    pub fn rot90(&self, k: isize, dims: &[usize]) -> RusTorchResult<Self> {
        if dims.len() != 2 {
            return Err(RusTorchError::InvalidOperation {
                operation: "rot90".to_string(),
                message: "rot90 requires exactly 2 dimensions".to_string(),
            });
        }

        let shape = self.shape();
        let dim0 = dims[0];
        let dim1 = dims[1];

        if dim0 >= shape.len() || dim1 >= shape.len() {
            return Err(RusTorchError::InvalidDimension(format!(
                "Invalid dimensions [{}, {}] (max: {})",
                dim0,
                dim1,
                shape.len() - 1
            )));
        }

        if dim0 == dim1 {
            return Err(RusTorchError::InvalidOperation {
                operation: "rot90".to_string(),
                message: "Rotation dimensions must be different".to_string(),
            });
        }

        // Normalize k to [0, 3]
        let k_norm = ((k % 4) + 4) % 4;

        match k_norm {
            0 => Ok(self.clone()),
            1 => self.rot90_once(dim0, dim1),
            2 => self.rot90_once(dim0, dim1)?.rot90_once(dim0, dim1),
            3 => self
                .rot90_once(dim0, dim1)?
                .rot90_once(dim0, dim1)?
                .rot90_once(dim0, dim1),
            _ => unreachable!(),
        }
    }

    /// Flip tensor along specified dimensions
    /// 指定された次元に沿ってテンソルを反転
    ///
    /// # Arguments
    /// * `dims` - Dimensions to flip
    ///
    /// # Examples
    /// ```rust
    /// use rustorch::prelude::Tensor;
    /// let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2]);
    /// let flipped = tensor.flip(&[0]).unwrap();
    /// // Flip along dimension 0
    /// ```
    pub fn flip(&self, dims: &[usize]) -> RusTorchResult<Self> {
        let shape = self.shape();

        // Validate dimensions
        for &dim in dims {
            if dim >= shape.len() {
                return Err(RusTorchError::InvalidDimension(format!(
                    "Invalid dimension {} (max: {})",
                    dim,
                    shape.len() - 1
                )));
            }
        }

        if dims.is_empty() {
            return Ok(self.clone());
        }

        let mut result = self.clone();
        for &dim in dims {
            result = result.flip_single_dim(dim)?;
        }

        Ok(result)
    }

    /// Flip tensor left-right (along last dimension)
    /// テンソルを左右反転(最後の次元に沿って)
    ///
    /// # Examples
    /// ```rust
    /// use rustorch::prelude::Tensor;
    /// let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2]);
    /// let flipped = tensor.fliplr().unwrap();
    /// ```
    pub fn fliplr(&self) -> RusTorchResult<Self> {
        let shape = self.shape();
        if shape.len() < 2 {
            return Err(RusTorchError::InvalidOperation {
                operation: "fliplr".to_string(),
                message: "fliplr requires at least 2D tensor".to_string(),
            });
        }

        self.flip(&[shape.len() - 1])
    }

    /// Flip tensor up-down (along first dimension)  
    /// テンソルを上下反転(最初の次元に沿って)
    ///
    /// # Examples
    /// ```rust
    /// use rustorch::prelude::Tensor;
    /// let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2]);
    /// let flipped = tensor.flipud().unwrap();
    /// ```
    pub fn flipud(&self) -> RusTorchResult<Self> {
        let shape = self.shape();
        if shape.is_empty() {
            return Err(RusTorchError::InvalidOperation {
                operation: "flipud".to_string(),
                message: "flipud requires at least 1D tensor".to_string(),
            });
        }

        self.flip(&[0])
    }
}

/// Lazy expanded tensor for memory-efficient broadcasting
/// メモリ効率的なブロードキャストのための遅延拡張テンソル
pub struct LazyExpandedTensor<T: Float> {
    source: Arc<Tensor<T>>,
    target_shape: Vec<usize>,
}

impl<T: Float + Clone + 'static> LazyExpandedTensor<T> {
    /// Materialize the lazy tensor into a concrete tensor
    /// 遅延テンソルを具体的なテンソルに実体化
    pub fn materialize(&self) -> RusTorchResult<Tensor<T>> {
        self.source.expand_owned(&self.target_shape)
    }

    /// Get the target shape without materializing
    /// 実体化せずにターゲット形状を取得
    pub fn shape(&self) -> &[usize] {
        &self.target_shape
    }

    /// Access element with on-demand computation
    /// オンデマンド計算で要素にアクセス
    pub fn get(&self, indices: &[usize]) -> RusTorchResult<T> {
        if indices.len() != self.target_shape.len() {
            return Err(RusTorchError::index_out_of_bounds(
                indices,
                &self.target_shape,
            ));
        }

        let source_indices = self.source.compute_source_indices(indices)?;

        self.source
            .data
            .get(source_indices.as_slice())
            .copied()
            .ok_or_else(|| RusTorchError::index_out_of_bounds(&[], &[]))
    }
}

/// Convenient methods for common shape operation patterns
/// 一般的な形状操作パターンの便利なメソッド
impl<T: Float + Clone + 'static> Tensor<T> {
    /// PyTorch-like view method for reshaping with ownership semantics
    /// 所有権セマンティクスでのPyTorchライクなviewメソッド
    pub fn view_shape(&self, shape: &[usize]) -> RusTorchResult<Self> {
        // Validate total elements match
        let current_elements = self.data.len();
        let new_elements: usize = shape.iter().product();

        if current_elements != new_elements {
            return Err(RusTorchError::InvalidOperation {
                operation: "view_shape".to_string(),
                message: format!(
                    "Shape {} is invalid for tensor with {} elements",
                    format!("{:?}", shape),
                    current_elements
                ),
            });
        }

        let reshaped_data = self
            .data
            .clone()
            .into_shape_with_order(shape.to_vec())
            .map_err(|_| RusTorchError::InvalidOperation {
                operation: "view_shape".to_string(),
                message: "Failed to create view with specified shape".to_string(),
            })?;

        Ok(Tensor::new(reshaped_data))
    }

    /// Create shape builder for chaining operations
    /// 操作チェーン用の形状ビルダーを作成
    pub fn shape_builder(self) -> ShapeBuilder<T> {
        ShapeBuilder::new(self)
    }
}

/// Zero-allocation shape operation traits for advanced use cases
/// 高度な用途のためのゼロ割り当て形状操作トレイト
pub trait ZeroAllocShapeOps<T: Float> {
    /// Attempt zero-copy squeeze
    /// ゼロコピーsqueezeを試行
    fn try_squeeze_view(&self) -> RusTorchResult<Tensor<T>>;

    /// Attempt zero-copy unsqueeze
    /// ゼロコピーunsqueezeを試行  
    fn try_unsqueeze_view(&self, dim: usize) -> RusTorchResult<Tensor<T>>;
}

impl<T: Float + Clone + 'static> ZeroAllocShapeOps<T> for Tensor<T> {
    fn try_squeeze_view(&self) -> RusTorchResult<Tensor<T>> {
        self.squeeze_with_mode(ShapeMode::ViewOnly)
    }

    fn try_unsqueeze_view(&self, dim: usize) -> RusTorchResult<Tensor<T>> {
        self.unsqueeze_with_mode(dim, ShapeMode::ViewOnly)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ownership_patterns_squeeze() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0], vec![1, 3, 1]);

        // Test owned squeeze
        let squeezed_owned = tensor.squeeze();
        assert_eq!(squeezed_owned.shape(), &[3]);

        // Test view squeeze
        let squeezed_view = tensor.squeeze_view().unwrap();
        assert_eq!(squeezed_view.shape(), &[3]);

        // Test in-place squeeze
        let mut tensor_mut = tensor.clone();
        tensor_mut.squeeze_inplace().unwrap();
        assert_eq!(tensor_mut.shape(), &[3]);
    }

    #[test]
    fn test_ownership_patterns_unsqueeze() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0], vec![3]);

        // Test owned unsqueeze
        let unsqueezed = tensor.unsqueeze(0).unwrap();
        assert_eq!(unsqueezed.shape(), &[1, 3]);

        // Test view unsqueeze
        let unsqueezed_view = tensor.unsqueeze_view(1).unwrap();
        assert_eq!(unsqueezed_view.shape(), &[3, 1]);

        // Test in-place unsqueeze
        let mut tensor_mut = tensor.clone();
        tensor_mut.unsqueeze_inplace(0).unwrap();
        assert_eq!(tensor_mut.shape(), &[1, 3]);
    }

    #[test]
    fn test_expand_shared_ownership() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0], vec![1, 3]);

        // Test shared expand
        let expanded_shared = tensor.expand_shared(&[4, 3]).unwrap();
        assert_eq!(expanded_shared.shape(), &[4, 3]);

        // Multiple references to same expanded tensor
        let ref1 = Arc::clone(&expanded_shared);
        let ref2 = Arc::clone(&expanded_shared);

        assert_eq!(ref1.shape(), ref2.shape());
    }

    #[test]
    fn test_lazy_expand() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0], vec![1, 2]);

        // Create lazy expansion
        let lazy_expanded = tensor.expand_lazy(&[3, 2]).unwrap();
        assert_eq!(lazy_expanded.shape(), &[3, 2]);

        // Access specific elements
        assert_eq!(lazy_expanded.get(&[0, 0]).unwrap(), 1.0);
        assert_eq!(lazy_expanded.get(&[1, 1]).unwrap(), 2.0);
        assert_eq!(lazy_expanded.get(&[2, 0]).unwrap(), 1.0);

        // Materialize when needed
        let materialized = lazy_expanded.materialize().unwrap();
        assert_eq!(materialized.shape(), &[3, 2]);
    }

    #[test]
    fn test_shape_builder_chain() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![1, 2, 2, 1]);

        // Chain multiple operations
        let result = tensor
            .shape_builder()
            .squeeze()
            .unwrap() // Remove all size-1 dims: [2, 2]
            .unsqueeze(0)
            .unwrap() // Add dim at start: [1, 2, 2]
            .expand(&[3, 2, 2])
            .unwrap() // Expand first dim: [3, 2, 2]
            .flatten()
            .unwrap() // Flatten: [12]
            .build();

        assert_eq!(result.shape(), &[12]);
        assert_eq!(result.numel(), 12);
    }

    #[test]
    fn test_flatten_variants() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2]);

        // Full flatten
        let flattened = tensor.flatten_owned();
        assert_eq!(flattened.shape(), &[4]);

        // Flatten range
        let partial = tensor.flatten_range(0, Some(1)).unwrap();
        assert_eq!(partial.shape(), &[4]);

        // Flatten view
        if tensor.is_contiguous() {
            let flattened_view = tensor.flatten_view().unwrap();
            assert_eq!(flattened_view.shape(), &[4]);
        }

        // In-place flatten
        let mut tensor_mut = tensor.clone();
        tensor_mut.flatten_inplace().unwrap();
        assert_eq!(tensor_mut.shape(), &[4]);
    }

    #[test]
    fn test_zero_alloc_traits() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0], vec![1, 3]);

        // Test zero-allocation operations when possible
        if tensor.is_contiguous() {
            let squeezed_view = tensor.try_squeeze_view().unwrap();
            assert_eq!(squeezed_view.shape(), &[3]);

            let unsqueezed_view = tensor.try_unsqueeze_view(0).unwrap();
            assert_eq!(unsqueezed_view.shape(), &[1, 1, 3]);
        }
    }

    #[test]
    fn test_expand_as() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0], vec![1, 2]);
        let target = Tensor::from_vec(vec![0.0; 6], vec![3, 2]);

        let expanded = tensor.expand_as(&target).unwrap();
        assert_eq!(expanded.shape(), target.shape());
        assert_eq!(expanded.shape(), &[3, 2]);

        // Verify data is correctly expanded
        let expanded_data = expanded.data.as_slice().unwrap();
        assert_eq!(expanded_data, &[1.0, 2.0, 1.0, 2.0, 1.0, 2.0]);
    }

    #[test]
    fn test_unflatten() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], vec![6]);

        // Unflatten into 2x3
        let unflattened = tensor.unflatten(0, &[2, 3]).unwrap();
        assert_eq!(unflattened.shape(), &[2, 3]);

        // Unflatten into 3x2
        let unflattened2 = tensor.unflatten(0, &[3, 2]).unwrap();
        assert_eq!(unflattened2.shape(), &[3, 2]);

        // Test with multi-dimensional tensor
        let tensor2d = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2]);
        let unflattened3d = tensor2d.unflatten(1, &[1, 2]).unwrap();
        assert_eq!(unflattened3d.shape(), &[2, 1, 2]);

        // Test invalid unflatten
        let result = tensor.unflatten(0, &[2, 4]); // 2*4=8 != 6
        assert!(result.is_err());
    }

    #[test]
    fn test_repeat() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0], vec![2]);

        // Simple repeat - repeat each element individually
        let repeated = tensor.repeat(&[3]).unwrap();
        assert_eq!(repeated.shape(), &[6]);
        // Our implementation repeats elements: [1.0, 1.0, 1.0, 2.0, 2.0, 2.0]
        assert_eq!(
            repeated.data.as_slice().unwrap(),
            &[1.0, 1.0, 1.0, 2.0, 2.0, 2.0]
        );

        // Multi-dimensional repeat
        let tensor2d = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2]);
        let repeated2d = tensor2d.repeat(&[2, 3]).unwrap();
        assert_eq!(repeated2d.shape(), &[4, 6]);

        // Dimension mismatch handling
        let repeated_padded = tensor.repeat(&[2, 3]).unwrap();
        assert_eq!(repeated_padded.shape(), &[2, 6]);
    }

    #[test]
    fn test_repeat_interleave() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0], vec![3]);

        // Repeat each element twice
        let repeated = tensor.repeat_interleave_scalar(2, Some(0)).unwrap();
        assert_eq!(repeated.shape(), &[6]);
        assert_eq!(
            repeated.data.as_slice().unwrap(),
            &[1.0, 1.0, 2.0, 2.0, 3.0, 3.0]
        );

        // Flatten and repeat
        let repeated_flat = tensor.repeat_interleave_scalar(2, None).unwrap();
        assert_eq!(repeated_flat.shape(), &[6]);
        assert_eq!(
            repeated_flat.data.as_slice().unwrap(),
            &[1.0, 1.0, 2.0, 2.0, 3.0, 3.0]
        );
    }

    #[test]
    fn test_roll() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![4]);

        // Roll by 1 position
        let rolled = tensor.roll_1d(1, Some(0)).unwrap();
        assert_eq!(rolled.shape(), &[4]);
        assert_eq!(rolled.data.as_slice().unwrap(), &[4.0, 1.0, 2.0, 3.0]);

        // Roll by negative amount
        let rolled_neg = tensor.roll_1d(-1, Some(0)).unwrap();
        assert_eq!(rolled_neg.data.as_slice().unwrap(), &[2.0, 3.0, 4.0, 1.0]);

        // Roll 2D tensor
        let tensor2d = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2]);
        let rolled2d = tensor2d.roll_1d(1, Some(0)).unwrap();
        assert_eq!(rolled2d.shape(), &[2, 2]);

        // Roll without specifying dimension (flattened)
        let rolled_flat = tensor.roll_1d(1, None).unwrap();
        assert_eq!(rolled_flat.shape(), &[4]);
        assert_eq!(rolled_flat.data.as_slice().unwrap(), &[4.0, 1.0, 2.0, 3.0]);
    }

    #[test]
    fn test_rot90() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2]);

        // 90-degree rotation
        let rotated1 = tensor.rot90(1, &[0, 1]).unwrap();
        assert_eq!(rotated1.shape(), &[2, 2]);

        // 180-degree rotation
        let rotated2 = tensor.rot90(2, &[0, 1]).unwrap();
        assert_eq!(rotated2.shape(), &[2, 2]);

        // 270-degree rotation
        let rotated3 = tensor.rot90(3, &[0, 1]).unwrap();
        assert_eq!(rotated3.shape(), &[2, 2]);

        // Full rotation (360 degrees) should return original
        let rotated4 = tensor.rot90(4, &[0, 1]).unwrap();
        assert_eq!(rotated4.shape(), &[2, 2]);
        assert_eq!(
            rotated4.data.as_slice().unwrap(),
            tensor.data.as_slice().unwrap()
        );

        // Negative rotation
        let rotated_neg = tensor.rot90(-1, &[0, 1]).unwrap();
        assert_eq!(rotated_neg.shape(), &[2, 2]);

        // Error cases
        assert!(tensor.rot90(1, &[0]).is_err()); // Need exactly 2 dims
        assert!(tensor.rot90(1, &[0, 0]).is_err()); // Dims must be different
        assert!(tensor.rot90(1, &[0, 5]).is_err()); // Invalid dimension
    }

    #[test]
    fn test_flip() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2]);

        // Flip along dimension 0
        let flipped0 = tensor.flip(&[0]).unwrap();
        assert_eq!(flipped0.shape(), &[2, 2]);

        // Flip along dimension 1
        let flipped1 = tensor.flip(&[1]).unwrap();
        assert_eq!(flipped1.shape(), &[2, 2]);

        // Flip along both dimensions
        let flipped_both = tensor.flip(&[0, 1]).unwrap();
        assert_eq!(flipped_both.shape(), &[2, 2]);

        // No flip (empty dimensions)
        let no_flip = tensor.flip(&[]).unwrap();
        assert_eq!(
            no_flip.data.as_slice().unwrap(),
            tensor.data.as_slice().unwrap()
        );

        // Error case - invalid dimension
        assert!(tensor.flip(&[5]).is_err());
    }

    #[test]
    fn test_fliplr_flipud() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2]);

        // Test fliplr (left-right flip)
        let flipped_lr = tensor.fliplr().unwrap();
        assert_eq!(flipped_lr.shape(), &[2, 2]);

        // Test flipud (up-down flip)
        let flipped_ud = tensor.flipud().unwrap();
        assert_eq!(flipped_ud.shape(), &[2, 2]);

        // Error cases
        let tensor1d = Tensor::from_vec(vec![1.0, 2.0], vec![2]);
        assert!(tensor1d.fliplr().is_err()); // Need at least 2D for fliplr

        let tensor0d = Tensor::from_vec(vec![1.0], vec![]);
        assert!(tensor0d.flipud().is_err()); // Need at least 1D for flipud
    }

    #[test]
    fn test_complex_shape_operations() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], vec![2, 3]);

        // Complex chain of operations using builder
        let result = tensor
            .clone()
            .shape_builder()
            .unsqueeze(0)
            .unwrap() // [1, 2, 3]
            .expand(&[2, 2, 3])
            .unwrap() // [2, 2, 3]
            .flatten()
            .unwrap() // [12]
            .build();

        assert_eq!(result.shape(), &[12]);
        assert_eq!(result.numel(), 12);

        // Test unflatten after flatten
        let flattened = tensor.flatten_owned();
        let restored = flattened.unflatten(0, &[2, 3]).unwrap();
        let original_shape = vec![2, 3]; // Store original shape since tensor is moved
        assert_eq!(restored.shape(), &original_shape);

        // Test repeat with expand_as
        let small = Tensor::from_vec(vec![1.0, 2.0], vec![1, 2]);
        let target_shape = Tensor::from_vec(vec![0.0; 8], vec![4, 2]);
        let expanded = small.expand_as(&target_shape).unwrap();
        assert_eq!(expanded.shape(), &[4, 2]);

        // Verify data correctness
        let data = expanded.data.as_slice().unwrap();
        assert_eq!(data, &[1.0, 2.0, 1.0, 2.0, 1.0, 2.0, 1.0, 2.0]);
    }
}

// =============================================================================
// BUILDER PATTERN FOR CHAINABLE OPERATIONS
// 連鎖可能操作のためのビルダーパターン
// =============================================================================

/// Shape builder for chainable tensor operations
/// 連鎖可能テンソル操作のためのシェイプビルダー
///
/// # Examples
/// ```rust
/// use rustorch::prelude::Tensor;
///
/// let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2, 1]);
///
/// // Chain multiple operations efficiently
/// let result = tensor
///     .shape_builder()
///     .squeeze().unwrap()
///     .unsqueeze(1).unwrap()
///     .flatten().unwrap()
///     .build();
///
/// assert_eq!(result.shape(), &[4]);
/// ```
pub struct ShapeBuilder<T: Float> {
    tensor: Tensor<T>,
}

impl<T: Float + Clone + 'static> ShapeBuilder<T> {
    /// Create new shape builder
    /// 新しいシェイプビルダーを作成
    pub fn new(tensor: Tensor<T>) -> Self {
        Self { tensor }
    }

    /// Chain squeeze operation - removes singleton dimensions
    /// squeeze操作を連鎖 - 単一次元を削除
    pub fn squeeze(mut self) -> RusTorchResult<Self> {
        self.tensor = self.tensor.squeeze();
        Ok(self)
    }

    /// Chain squeeze_dim operation - removes specific singleton dimension
    /// squeeze_dim操作を連鎖 - 特定の単一次元を削除
    pub fn squeeze_dim(mut self, dim: usize) -> RusTorchResult<Self> {
        self.tensor = self.tensor.squeeze_dim(dim)?;
        Ok(self)
    }

    /// Chain unsqueeze operation - adds singleton dimension
    /// unsqueeze操作を連鎖 - 単一次元を追加
    pub fn unsqueeze(mut self, dim: usize) -> RusTorchResult<Self> {
        self.tensor = self.tensor.unsqueeze(dim)?;
        Ok(self)
    }

    /// Chain expand_as operation - expands to match another tensor's shape
    /// expand_as操作を連鎖 - 他のテンソルの形状に合わせて拡張
    pub fn expand_as(mut self, other: &Tensor<T>) -> RusTorchResult<Self> {
        self.tensor = self.tensor.expand_as(other)?;
        Ok(self)
    }

    /// Chain expand operation - expands to target shape
    /// expand操作を連鎖 - 目標形状に拡張
    pub fn expand(mut self, shape: &[usize]) -> RusTorchResult<Self> {
        self.tensor = self.tensor.expand_owned(shape)?;
        Ok(self)
    }

    /// Chain flatten operation - flattens to 1D
    /// flatten操作を連鎖 - 1次元に平坦化
    pub fn flatten(mut self) -> RusTorchResult<Self> {
        self.tensor = self.tensor.flatten_owned();
        Ok(self)
    }

    /// Chain flatten_range operation - flattens specific dimension range
    /// flatten_range操作を連鎖 - 特定次元範囲を平坦化
    pub fn flatten_range(
        mut self,
        start_dim: usize,
        end_dim: Option<usize>,
    ) -> RusTorchResult<Self> {
        self.tensor = self.tensor.flatten_range(start_dim, end_dim)?;
        Ok(self)
    }

    /// Chain unflatten operation - reverses flatten
    /// unflatten操作を連鎖 - 平坦化を逆転
    pub fn unflatten(mut self, dim: usize, sizes: &[usize]) -> RusTorchResult<Self> {
        self.tensor = self.tensor.unflatten(dim, sizes)?;
        Ok(self)
    }

    /// Chain repeat operation - repeats tensor elements
    /// repeat操作を連鎖 - テンソル要素を反復
    pub fn repeat(mut self, repeats: &[usize]) -> RusTorchResult<Self> {
        self.tensor = self.tensor.repeat(repeats)?;
        Ok(self)
    }

    /// Chain repeat_interleave operation - interleaves tensor elements
    /// repeat_interleave操作を連鎖 - テンソル要素を交互配置
    pub fn repeat_interleave(mut self, repeats: usize, dim: Option<usize>) -> RusTorchResult<Self> {
        self.tensor = self.tensor.repeat_interleave_scalar(repeats, dim)?;
        Ok(self)
    }

    /// Chain roll operation - shifts elements
    /// roll操作を連鎖 - 要素をシフト
    pub fn roll(mut self, shifts: isize, dim: Option<usize>) -> RusTorchResult<Self> {
        self.tensor = self.tensor.roll_1d(shifts, dim)?;
        Ok(self)
    }

    /// Chain rot90 operation - rotates 90 degrees
    /// rot90操作を連鎖 - 90度回転
    pub fn rot90(mut self, k: isize, dims: &[usize]) -> RusTorchResult<Self> {
        self.tensor = self.tensor.rot90(k, dims)?;
        Ok(self)
    }

    /// Chain flip operation - flips along dimensions
    /// flip操作を連鎖 - 次元に沿って反転
    pub fn flip(mut self, dims: &[usize]) -> RusTorchResult<Self> {
        self.tensor = self.tensor.flip(dims)?;
        Ok(self)
    }

    /// Chain fliplr operation - flips left-right
    /// fliplr操作を連鎖 - 左右反転
    pub fn fliplr(mut self) -> RusTorchResult<Self> {
        self.tensor = self.tensor.fliplr()?;
        Ok(self)
    }

    /// Chain flipud operation - flips up-down
    /// flipud操作を連鎖 - 上下反転
    pub fn flipud(mut self) -> RusTorchResult<Self> {
        self.tensor = self.tensor.flipud()?;
        Ok(self)
    }

    /// Chain view_shape operation - creates view with different shape
    /// view_shape操作を連鎖 - 異なる形状のビューを作成
    pub fn view_shape(mut self, shape: &[usize]) -> RusTorchResult<Self> {
        self.tensor = self.tensor.view_shape(shape)?;
        Ok(self)
    }

    /// Finalize builder and return tensor
    /// ビルダーを終了してテンソルを返す
    pub fn build(self) -> Tensor<T> {
        self.tensor
    }

    /// Get current shape without building
    /// ビルドせずに現在の形状を取得
    pub fn current_shape(&self) -> &[usize] {
        self.tensor.shape()
    }

    /// Peek at current tensor without consuming builder
    /// ビルダーを消費せずに現在のテンソルを確認
    pub fn peek(&self) -> &Tensor<T> {
        &self.tensor
    }
}

/// Convenience macro for chaining shape operations
/// 形状操作連鎖の便利マクロ
///
/// # Examples
/// ```rust
/// use rustorch::prelude::Tensor;
/// use rustorch::shape_ops;
/// use rustorch::error::RusTorchResult;
///
/// fn main() -> RusTorchResult<()> {
///     let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2, 1]);
///
///     // Using macro for concise chaining
///     let result = shape_ops!(tensor,
///         squeeze,
///         unsqueeze(1),
///         flatten
///     )?;
///     Ok(())
/// }
/// ```
#[macro_export]
macro_rules! shape_ops {
    ($tensor:expr, $($op:ident$(($($arg:expr),*))?),+ $(,)?) => {{
        let mut builder = $tensor.shape_builder();
        $(
            builder = builder.$op($($($arg),*)?)?;
        )+
        Ok::<_, $crate::error::RusTorchError>(builder.build())
    }};
}

/// Fluent interface trait for shape operations
/// 形状操作のフルエントインターフェーストレイト
pub trait ShapeOps<T: Float> {
    /// Start shape operations with builder pattern
    /// ビルダーパターンで形状操作を開始
    fn shapes(self) -> ShapeBuilder<T>;
}

impl<T: Float + Clone + 'static> ShapeOps<T> for Tensor<T> {
    /// Create shape builder for fluent operations
    /// フルエント操作のためのシェイプビルダーを作成
    fn shapes(self) -> ShapeBuilder<T> {
        ShapeBuilder::new(self)
    }
}

// Additional tests for builder pattern
#[cfg(test)]
mod builder_tests {
    use super::*;

    #[test]
    fn test_builder_pattern_basic() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2, 1]);

        let result = tensor
            .shape_builder()
            .squeeze()
            .unwrap()
            .unsqueeze(1)
            .unwrap()
            .flatten()
            .unwrap()
            .build();

        assert_eq!(result.shape(), &[4]);
    }

    #[test]
    fn test_fluent_interface() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![4, 1]);

        let result = tensor
            .shapes()
            .squeeze()
            .unwrap()
            .unsqueeze(1)
            .unwrap()
            .build();

        assert_eq!(result.shape(), &[4, 1]);
    }

    #[test]
    fn test_shape_ops_macro() -> RusTorchResult<()> {
        let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2, 1]);

        let result = shape_ops!(tensor, squeeze, flatten)?;

        assert_eq!(result.shape(), &[4]);
        Ok(())
    }

    #[test]
    fn test_builder_peek() {
        let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2, 1]);

        let builder = tensor.shape_builder().squeeze().unwrap();

        // Peek without consuming
        assert_eq!(builder.current_shape(), &[2, 2]);

        // Continue building
        let result = builder.flatten().unwrap().build();
        assert_eq!(result.shape(), &[4]);
    }
}