rustframe 0.0.1-a.20250805

A simple dataframe and math toolkit
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
//! A simple column-major Matrix implementation with element-wise operations.

use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Index, IndexMut, Mul, Not, Sub};

/// A column‑major 2D matrix of `T`. Index as `Array(row, column)`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Matrix<T> {
    rows: usize,
    cols: usize,
    data: Vec<T>,
}

impl<T: Clone> Matrix<T> {
    /// Build from columns (each inner Vec is one column)
    pub fn from_cols(cols_data: Vec<Vec<T>>) -> Self {
        let cols = cols_data.len();
        assert!(cols > 0, "need at least one column");
        // Handle empty cols_data
        let rows = cols_data.get(0).map_or(0, |c| c.len());
        // Allow 0-row matrices if columns are empty, but not 0-col matrices if rows > 0
        assert!(
            rows > 0 || cols == 0,
            "need at least one row if columns exist"
        );

        for (i, col) in cols_data.iter().enumerate() {
            assert!(
                col.len() == rows,
                "col {} has len {}, expected {}",
                i,
                col.len(),
                rows
            );
        }
        // Flatten column data directly
        let data = cols_data.into_iter().flatten().collect();
        Matrix { rows, cols, data }
    }

    /// Build from a flat Vec, assuming column-major order.
    pub fn from_vec(data: Vec<T>, rows: usize, cols: usize) -> Self {
        assert!(
            rows > 0 || cols == 0,
            "need at least one row if columns exist"
        );
        assert!(
            cols > 0 || rows == 0,
            "need at least one column if rows exist"
        );
        if rows * cols != 0 {
            // Only assert length if matrix is non-empty
            assert_eq!(
                data.len(),
                rows * cols,
                "data length mismatch: expected {}, got {}",
                rows * cols,
                data.len()
            );
        } else {
            assert!(data.is_empty(), "data must be empty for 0-sized matrix");
        }

        Matrix { rows, cols, data }
    }

    /// Build from a flat Vec, assuming row-major order.
    pub fn from_rows_vec(data: Vec<T>, rows: usize, cols: usize) -> Self {
        let mut new_vec = Vec::with_capacity(rows * cols);

        for c in 0..cols {
            for r in 0..rows {
                new_vec.push(data[r * cols + c].clone());
            }
        }

        Matrix::from_vec(new_vec, rows, cols)
    }

    pub fn data(&self) -> &[T] {
        &self.data
    }

    pub fn data_mut(&mut self) -> &mut [T] {
        &mut self.data
    }

    /// Consumes the Matrix and returns its underlying data Vec.
    pub fn into_vec(self) -> Vec<T> {
        self.data
    }

    /// Creates a new `Vec<T>` containing the matrix data (cloned).
    pub fn to_vec(&self) -> Vec<T> {
        self.data.clone()
    }

    pub fn rows(&self) -> usize {
        self.rows
    }

    pub fn cols(&self) -> usize {
        self.cols
    }

    pub fn shape(&self) -> (usize, usize) {
        (self.rows, self.cols)
    }

    /// Get element reference (immutable). Panics on out-of-bounds.
    pub fn get(&self, r: usize, c: usize) -> &T {
        &self[(r, c)]
    }

    /// Get element reference (mutable). Panics on out-of-bounds.
    pub fn get_mut(&mut self, r: usize, c: usize) -> &mut T {
        &mut self[(r, c)]
    }

    #[inline]
    pub fn column(&self, c: usize) -> &[T] {
        assert!(
            c < self.cols,
            "column index {} out of bounds for {} columns",
            c,
            self.cols
        );
        let start = c * self.rows;
        &self.data[start..start + self.rows]
    }

    #[inline]
    pub fn column_mut(&mut self, c: usize) -> &mut [T] {
        assert!(
            c < self.cols,
            "column index {} out of bounds for {} columns",
            c,
            self.cols
        );
        let start = c * self.rows;
        &mut self.data[start..start + self.rows]
    }

    pub fn iter_columns(&self) -> impl Iterator<Item = &[T]> {
        (0..self.cols).map(move |c| self.column(c))
    }

    pub fn iter_rows(&self) -> impl Iterator<Item = MatrixRow<'_, T>> {
        (0..self.rows).map(move |r| MatrixRow {
            matrix: self,
            row: r,
        })
    }

    /// Swaps two columns in the matrix. Panics on out-of-bounds.
    pub fn swap_columns(&mut self, c1: usize, c2: usize) {
        assert!(
            c1 < self.cols,
            "column index c1={} out of bounds for {} columns",
            c1,
            self.cols
        );
        assert!(
            c2 < self.cols,
            "column index c2={} out of bounds for {} columns",
            c2,
            self.cols
        );
        if c1 == c2 || self.rows == 0 || self.cols == 0 {
            return;
        }

        let (start1, end1) = (c1 * self.rows, (c1 + 1) * self.rows);
        let (start2, end2) = (c2 * self.rows, (c2 + 1) * self.rows);

        if (start1 < start2 && end1 > start2) || (start2 < start1 && end2 > start1) {
            panic!("Cannot swap overlapping columns");
        }

        // element-wise swap
        for r in 0..self.rows {
            self.data.swap(start1 + r, start2 + r);
        }
    }

    /// Deletes a column from the matrix. Panics on out-of-bounds.
    /// This is O(N) where N is the number of elements.
    pub fn delete_column(&mut self, col: usize) {
        assert!(
            col < self.cols,
            "column index {} out of bounds for {} columns",
            col,
            self.cols
        );
        let start = col * self.rows;
        self.data.drain(start..start + self.rows); // Efficient removal
        self.cols -= 1;
    }

    #[inline]
    pub fn row(&self, r: usize) -> Vec<T> {
        assert!(
            r < self.rows,
            "row index {} out of bounds for {} rows",
            r,
            self.rows
        );
        let mut row_data = Vec::with_capacity(self.cols);
        for c in 0..self.cols {
            row_data.push(self[(r, c)].clone()); // Clone each element
        }
        row_data
    }
    pub fn row_copy_from_slice(&mut self, r: usize, values: &[T]) {
        assert!(
            r < self.rows,
            "row index {} out of bounds for {} rows",
            r,
            self.rows
        );
        assert!(
            values.len() == self.cols,
            "input slice length {} does not match number of columns {}",
            values.len(),
            self.cols
        );

        for (c, value) in values.iter().enumerate() {
            let idx = r + c * self.rows; // column-major index
            self.data[idx] = value.clone();
        }
    }

    /// Deletes a row from the matrix. Panics on out-of-bounds.
    /// This is O(N) where N is the number of elements, as it rebuilds the data vec.
    pub fn delete_row(&mut self, row: usize) {
        assert!(
            row < self.rows,
            "row index {} out of bounds for {} rows",
            row,
            self.rows
        );
        if self.rows == 0 {
            return;
        } // Nothing to delete

        let old_rows = self.rows;
        let new_rows = self.rows - 1;
        let mut new_data = Vec::with_capacity(new_rows * self.cols);

        for c in 0..self.cols {
            let col_start_old = c * old_rows;
            for r in 0..old_rows {
                if r != row {
                    // Must clone as we are reading from the old data while building the new one
                    new_data.push(self.data[col_start_old + r].clone());
                }
            }
        }
        self.data = new_data;
        self.rows = new_rows;
    }

    pub fn transpose(&self) -> Matrix<T> {
        let (m, n) = (self.rows, self.cols);
        let mut transposed_data = Vec::with_capacity(m * n);

        // In the transposed matrix the old rows become the new columns.
        for j in 0..m {
            // new column index = old row index
            for i in 0..n {
                // new row index = old col index
                transposed_data.push(self[(j, i)].clone()); // A(T)[i,j] = A[j,i]
            }
        }

        Matrix::from_vec(transposed_data, n, m) // size is n × m
    }
}

impl<T: Clone> Matrix<T> {
    /// Adds a column to the matrix at the specified index. Panics if index > cols or length mismatch.
    /// This is O(N) where N is the number of elements.
    pub fn add_column(&mut self, index: usize, column: Vec<T>) {
        assert!(
            index <= self.cols,
            "add_column index {} out of bounds for {} columns",
            index,
            self.cols
        );
        assert_eq!(
            column.len(),
            self.rows,
            "column length mismatch: expected {}, got {}",
            self.rows,
            column.len()
        );
        if self.rows == 0 && self.cols == 0 {
            // Special case: adding first column to empty matrix
            assert!(index == 0, "index must be 0 for adding first column");
            self.data = column;
            self.cols = 1;
            // self.rows should be correctly set by column.len() assertion
        } else {
            let insert_pos = index * self.rows;
            self.data.splice(insert_pos..insert_pos, column); // Efficient insertion
            self.cols += 1;
        }
    }

    /// Adds a row to the matrix at the specified index. Panics if index > rows or length mismatch.
    /// This is O(N) where N is the number of elements, as it rebuilds the data vec.
    pub fn add_row(&mut self, index: usize, row: Vec<T>) {
        assert!(
            index <= self.rows,
            "add_row index {} out of bounds for {} rows",
            index,
            self.rows
        );
        assert_eq!(
            row.len(),
            self.cols,
            "row length mismatch: expected {} (cols), got {}",
            self.cols,
            row.len()
        );

        if self.cols == 0 && self.rows == 0 {
            // Special case: adding first row to empty matrix
            assert!(index == 0, "index must be 0 for adding first row");
            // Cannot add a row if there are no columns yet. Maybe panic or change API?
            assert!(
                self.cols > 0 || row.is_empty(),
                "cannot add non-empty row to matrix with 0 columns"
            );
            if row.is_empty() {
                return;
            } // Adding empty row to empty matrix is no-op
        }

        let old_rows = self.rows;
        let new_rows = self.rows + 1;
        let mut new_data = Vec::with_capacity(new_rows * self.cols);
        let mut row_iter = row.into_iter(); // Consume the input row vec

        for c in 0..self.cols {
            let old_col_start = c * old_rows;
            for r in 0..new_rows {
                if r == index {
                    // Take the next element from the provided row vector
                    new_data.push(row_iter.next().expect("Row iterator exhausted prematurely - should have been caught by length assert"));
                } else {
                    // Calculate the corresponding old row index
                    let old_r = if r < index { r } else { r - 1 };
                    // Must clone as we are reading from the old data while building the new one
                    new_data.push(self.data[old_col_start + old_r].clone());
                }
            }
        }
        self.data = new_data;
        self.rows = new_rows;
    }

    /// Return a new matrix where row 0 of `self` is repeated `n` times.
    pub fn repeat_rows(&self, n: usize) -> Matrix<T>
    where
        T: Clone,
    {
        let mut data = Vec::with_capacity(n * self.cols());
        let zeroth_row = self.row(0);
        for value in &zeroth_row {
            for _ in 0..n {
                data.push(value.clone()); // Clone each element
            }
        }
        Matrix::from_vec(data, n, self.cols)
    }

    /// Creates a new matrix filled with a specific value of the specified size.
    pub fn filled(rows: usize, cols: usize, value: T) -> Self {
        Matrix {
            rows,
            cols,
            data: vec![value; rows * cols], // Fill with the specified value
        }
    }

    /// Creates a new matrix by broadcasting a 1-row matrix to a target shape.
    /// Panics if `self` is not a 1-row matrix or if `self.cols()` does not match `target_cols`.
    pub fn broadcast_row_to_target_shape(
        &self,
        target_rows: usize,
        target_cols: usize,
    ) -> Matrix<T> {
        assert_eq!(
            self.rows(),
            1,
            "broadcast_row_to_target_shape can only be called on a 1-row matrix."
        );
        assert_eq!(
            self.cols(),
            target_cols,
            "Column count mismatch for broadcasting: source has {} columns, target has {} columns.",
            self.cols(),
            target_cols
        );

        let mut data = Vec::with_capacity(target_rows * target_cols);
        let original_row_data = self.row(0); // Get the single row data

        for _ in 0..target_rows {
            // Repeat 'target_rows' times
            for value in &original_row_data {
                // Iterate over elements of the row
                data.push(value.clone());
            }
        }
        // The data is now in row-major order for the new matrix.
        // We need to convert it to column-major for Matrix::from_vec.
        Matrix::from_rows_vec(data, target_rows, target_cols)
    }
}

impl Matrix<f64> {
    /// Creates a new matrix filled with zeros of the specified size.
    pub fn zeros(rows: usize, cols: usize) -> Self {
        Matrix::filled(rows, cols, 0.0)
    }

    /// Creates a new matrix filled with ones of the specified size.
    pub fn ones(rows: usize, cols: usize) -> Self {
        Matrix::filled(rows, cols, 1.0)
    }

    /// Creates a new matrix filled with NaN values of the specified size.
    pub fn nan(rows: usize, cols: usize) -> Matrix<f64> {
        Matrix::filled(rows, cols, f64::NAN)
    }
}

impl<T> Index<(usize, usize)> for Matrix<T> {
    type Output = T;

    #[inline]
    fn index(&self, (r, c): (usize, usize)) -> &T {
        // Validate that the requested indices are within bounds
        assert!(
            r < self.rows && c < self.cols,
            "index out of bounds: ({}, {}) vs {}x{}",
            r,
            c,
            self.rows,
            self.cols
        );
        // Compute column-major offset and return reference
        &self.data[c * self.rows + r]
    }
}

impl<T> IndexMut<(usize, usize)> for Matrix<T> {
    #[inline]
    fn index_mut(&mut self, (r, c): (usize, usize)) -> &mut T {
        // Validate that the requested indices are within bounds
        assert!(
            r < self.rows && c < self.cols,
            "index out of bounds: ({}, {}) vs {}x{}",
            r,
            c,
            self.rows,
            self.cols
        );
        // Compute column-major offset and return mutable reference
        &mut self.data[c * self.rows + r]
    }
}

// --- Row Iterator Helper ---

/// Represents an immutable view of a single row in the matrix.
pub struct MatrixRow<'a, T> {
    matrix: &'a Matrix<T>,
    row: usize,
}

impl<'a, T> MatrixRow<'a, T> {
    /// Returns a reference to the element at the given column in this row.
    pub fn get(&self, c: usize) -> &T {
        &self.matrix[(self.row, c)]
    }

    /// Returns an iterator over all elements in this row.
    pub fn iter(&self) -> impl Iterator<Item = &T> {
        (0..self.matrix.cols).map(move |c| self.get(c))
    }
}

// --- Reduction Axis Enum ---

/// Specifies the axis along which to perform a reduction operation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Axis {
    /// Apply reduction along columns (vertical axis). Result has 1 row.
    Col,
    /// Apply reduction along rows (horizontal axis). Result has 1 column.
    Row,
}

// --- Broadcasting ---

/// A trait to turn either a `Matrix<T>` or a scalar T into a `Vec<T>` of
/// length `rows*cols` (broadcasting the scalar). Used for comparisons.
pub trait Broadcastable<T> {
    fn to_vec(&self, rows: usize, cols: usize) -> Vec<T>;
}

impl<T: Clone> Broadcastable<T> for T {
    fn to_vec(&self, rows: usize, cols: usize) -> Vec<T> {
        vec![self.clone(); rows * cols]
    }
}

impl<T: Clone> Broadcastable<T> for Matrix<T> {
    fn to_vec(&self, rows: usize, cols: usize) -> Vec<T> {
        assert_eq!(self.rows, rows, "row count mismatch in broadcast");
        assert_eq!(self.cols, cols, "col count mismatch in broadcast");
        self.data.clone() // Clone the data for the broadcasted vec
    }
}

// --- Element-wise Comparisons ---

/// Generates element-wise eq, lt, le, gt and ge methods
/// where the rhs can be a `Matrix<T>` or a scalar T.
macro_rules! impl_elementwise_cmp {
    (
        $( $method:ident => $op:tt ),* $(,)?
    ) => {
        impl<T: PartialOrd + Clone> Matrix<T> {
            $(
            #[doc = concat!("Element-wise comparison `self ", stringify!($op), " rhs`,\n\
                             where `rhs` may be a `Matrix<T>` or a scalar T.\n\
                             Returns a `BoolMatrix`.")]
            pub fn $method<Rhs>(&self, rhs: Rhs) -> BoolMatrix
            where
                Rhs: Broadcastable<T>,
            {
                // Prepare broadcasted rhs-data using the trait
                let rhs_data = rhs.to_vec(self.rows, self.cols);

                // Pairwise compare using iterators
                let data = self
                    .data
                    .iter() // Borrow self's data
                    .zip(rhs_data.iter()) // Borrow rhs's broadcasted data
                    .map(|(a, b)| a $op b) // Perform comparison op
                    .collect();

                // Create BoolMatrix from result
                Matrix::<bool>::from_vec(data, self.rows, self.cols)
            }
            )*
        }
    };
}

// Instantiate element-wise comparison implementations for matrices.
impl_elementwise_cmp! {
    eq_elem => ==,
    ne_elem => !=,
    lt_elem => <,
    le_elem => <=,
    gt_elem => >,
    ge_elem => >=,
}

// --- Element-wise Arithmetic Operations (Macros generating all ownership variants) ---

fn check_matrix_dims_for_ops<T>(lhs: &Matrix<T>, rhs: &Matrix<T>) {
    assert!(
        lhs.rows == rhs.rows,
        "Row count mismatch: left has {} rows, right has {} rows",
        lhs.rows,
        rhs.rows
    );
    assert!(
        lhs.cols == rhs.cols,
        "Column count mismatch: left has {} columns, right has {} columns",
        lhs.cols,
        rhs.cols
    );
}

/// Generates element-wise arithmetic implementations for Matrix + Matrix
macro_rules! impl_elementwise_op_matrix_all {
    ($OpTrait:ident, $method:ident, $op:tt) => {
        // &Matrix + &Matrix
        impl<'a, 'b, T> $OpTrait<&'b Matrix<T>> for &'a Matrix<T>
        where T: Clone + $OpTrait<Output = T> {
            type Output = Matrix<T>;
            fn $method(self, rhs: &'b Matrix<T>) -> Matrix<T> {
                check_matrix_dims_for_ops(self, rhs);
                let data = self.data.iter().cloned().zip(rhs.data.iter().cloned()).map(|(a, b)| a $op b).collect();
                Matrix { rows: self.rows, cols: self.cols, data }
            }
        }
        // Matrix + &Matrix (Consumes self)
        impl<'b, T> $OpTrait<&'b Matrix<T>> for Matrix<T>
        where T: Clone + $OpTrait<Output = T> {
            type Output = Matrix<T>;
            fn $method(mut self, rhs: &'b Matrix<T>) -> Matrix<T> { // Make self mutable for potential in-place modification
                check_matrix_dims_for_ops(&self, rhs);
                // Modify data in place
                for (a, b) in self.data.iter_mut().zip(rhs.data.iter().cloned()) {
                    *a = a.clone() $op b; // Requires T: Clone for the *a = part
                }
                // Return modified self (its data vec was consumed conceptually)
                self
                // Alternative: Collect into new Vec if T is not Clone or in-place is complex
                // let data = self.data.into_iter().zip(rhs.data.iter().cloned()).map(|(a, b)| a $op b).collect();
                // Matrix { rows: self.rows, cols: self.cols, data }
            }
        }
         // &Matrix + Matrix (Consumes rhs)
        impl<'a, T> $OpTrait<Matrix<T>> for &'a Matrix<T>
        where T: Clone + $OpTrait<Output = T> {
            type Output = Matrix<T>;
            fn $method(self, mut rhs: Matrix<T>) -> Matrix<T> { // Make rhs mutable
                check_matrix_dims_for_ops(self, &rhs);
                 // Modify rhs data in place
                 for (a, b) in self.data.iter().cloned().zip(rhs.data.iter_mut()) {
                    *b = a $op b.clone(); // Requires T: Clone for the *b = part
                }
                // Return modified rhs
                rhs
                // Alternative: Collect into new Vec
                // let data = self.data.iter().cloned().zip(rhs.data.into_iter()).map(|(a, b)| a $op b).collect();
                // Matrix { rows: self.rows, cols: self.cols, data }
            }
        }
        // Matrix + Matrix (Consumes both)
        impl<T> $OpTrait<Matrix<T>> for Matrix<T>
        where T: Clone + $OpTrait<Output = T> {
            type Output = Matrix<T>;
            fn $method(mut self, rhs: Matrix<T>) -> Matrix<T> { // Make self mutable
                check_matrix_dims_for_ops(&self, &rhs);
                // Modify self data in place
                 for (a, b) in self.data.iter_mut().zip(rhs.data.into_iter()) {
                    *a = a.clone() $op b; // Requires T: Clone
                }
                // Return modified self
                self
                // Alternative: Collect into new Vec
                // let data = self.data.into_iter().zip(rhs.data.into_iter()).map(|(a, b)| a $op b).collect();
                // Matrix { rows: self.rows, cols: self.cols, data }
            }
        }
    };
}

/// Generates element-wise arithmetic implementations for Matrix + Scalar
macro_rules! impl_elementwise_op_scalar_all {
    ($OpTrait:ident, $method:ident, $op:tt) => {
        // &Matrix + Scalar
        impl<'a, T> $OpTrait<T> for &'a Matrix<T>
        where T: Clone + $OpTrait<Output = T> {
            type Output = Matrix<T>;
            fn $method(self, rhs: T) -> Matrix<T> {
                let data = self.data.iter().cloned().map(|a| a $op rhs.clone()).collect();
                Matrix { rows: self.rows, cols: self.cols, data }
            }
        }
        // Matrix + Scalar (Consumes self)
        impl<T> $OpTrait<T> for Matrix<T>
        where T: Clone + $OpTrait<Output = T> {
            type Output = Matrix<T>;
            fn $method(mut self, rhs: T) -> Matrix<T> { // Make self mutable
                 // Modify self data in place
                for a in self.data.iter_mut() {
                     *a = a.clone() $op rhs.clone(); // Requires T: Clone
                }
                // Return modified self
                self
                // Alternative: Collect into new Vec
                // let data = self.data.into_iter().map(|a| a $op rhs.clone()).collect();
                // Matrix { rows: self.rows, cols: self.cols, data }
            }
        }
        // NOTE: Scalar + Matrix (e.g., 1.0 + matrix) is NOT implemented here.
        // It would require `impl Add<Matrix<T>> for T`, which is discouraged
        // for primitive types unless inside the crate defining T.
    };
}

// Instantiate ALL combinations for arithmetic ops using the new macros
impl_elementwise_op_matrix_all!(Add, add, +);
impl_elementwise_op_matrix_all!(Sub, sub, -);
impl_elementwise_op_matrix_all!(Mul, mul, *); // Element-wise multiplication
impl_elementwise_op_matrix_all!(Div, div, /); // Element-wise division

impl_elementwise_op_scalar_all!(Add, add, +);
impl_elementwise_op_scalar_all!(Sub, sub, -);
impl_elementwise_op_scalar_all!(Mul, mul, *);
impl_elementwise_op_scalar_all!(Div, div, /);

// --- Element-wise Bitwise Operations (BoolMatrix) ---

macro_rules! impl_bitwise_op_all {
    ($OpTrait:ident, $method:ident, $op:tt) => {
        // &Matrix<bool> OP &Matrix<bool>
        impl<'a, 'b> $OpTrait<&'b Matrix<bool>> for &'a Matrix<bool> {
            type Output = Matrix<bool>;
            fn $method(self, rhs: &'b Matrix<bool>) -> Matrix<bool> {
                check_matrix_dims_for_ops(self, rhs);
                let data = self.data.iter().cloned().zip(rhs.data.iter().cloned()).map(|(a, b)| a $op b).collect();
                Matrix { rows: self.rows, cols: self.cols, data }
            }
        }
        // Matrix<bool> OP &Matrix<bool>
        impl<'b> $OpTrait<&'b Matrix<bool>> for Matrix<bool> {
            type Output = Matrix<bool>;
            fn $method(mut self, rhs: &'b Matrix<bool>) -> Matrix<bool> {
                check_matrix_dims_for_ops(&self, rhs);
                for (a, b) in self.data.iter_mut().zip(rhs.data.iter()) { *a = *a $op *b; } // bool is Copy
                self
            }
        }
        // &Matrix<bool> OP Matrix<bool>
        impl<'a> $OpTrait<Matrix<bool>> for &'a Matrix<bool> {
            type Output = Matrix<bool>;
            fn $method(self, mut rhs: Matrix<bool>) -> Matrix<bool> {
                check_matrix_dims_for_ops(self, &rhs);
                for (a, b) in self.data.iter().zip(rhs.data.iter_mut()) { *b = *a $op *b; } // bool is Copy
                rhs
            }
        }
        // Matrix<bool> OP Matrix<bool>
        impl $OpTrait<Matrix<bool>> for Matrix<bool> {
            type Output = Matrix<bool>;
            fn $method(mut self, rhs: Matrix<bool>) -> Matrix<bool> {
                check_matrix_dims_for_ops(&self, &rhs);
                for (a, b) in self.data.iter_mut().zip(rhs.data.iter()) { *a = *a $op *b; } // bool is Copy
                self
            }
        }
    };
}

// Instantiate ALL combinations for bitwise ops
impl_bitwise_op_all!(BitAnd, bitand, &);
impl_bitwise_op_all!(BitOr, bitor, |);
impl_bitwise_op_all!(BitXor, bitxor, ^);

// --- Logical Not ---

// `!Matrix<bool>` (consumes matrix)
impl Not for Matrix<bool> {
    type Output = Matrix<bool>;
    fn not(mut self) -> Matrix<bool> {
        // Take by value, make mutable
        for val in self.data.iter_mut() {
            *val = !*val; // Invert in place
        }
        self // Return the modified matrix
    }
}

// `!&Matrix<bool>` (borrows matrix, returns new matrix)
impl Not for &Matrix<bool> {
    type Output = Matrix<bool>;
    fn not(self) -> Matrix<bool> {
        // Take by reference
        let data = self.data.iter().map(|&v| !v).collect(); // Create new data vec
        Matrix {
            rows: self.rows,
            cols: self.cols,
            data,
        }
    }
}

// --- Type Aliases ---
pub type FloatMatrix = Matrix<f64>;
pub type BoolMatrix = Matrix<bool>;
pub type IntMatrix = Matrix<i32>;
pub type StringMatrix = Matrix<String>;

// --- Unit Tests ---

#[cfg(test)]
mod tests {
    use crate::matrix::BoolOps;

    use super::*; // Import items from outer scope

    // Helper to create a 2x2 f64 matrix easily
    fn make_f64_matrix(a: f64, b: f64, c: f64, d: f64) -> FloatMatrix {
        Matrix::from_cols(vec![vec![a, c], vec![b, d]])
    }

    // Helper to create a 2x2 bool matrix easily
    fn make_bool_matrix(a: bool, b: bool, c: bool, d: bool) -> BoolMatrix {
        Matrix::from_cols(vec![vec![a, c], vec![b, d]])
    }

    // --- Arithmetic Tests ---

    #[test]
    fn test_add_f64() {
        let m1 = make_f64_matrix(1.0, 2.0, 3.0, 4.0);
        let m2 = make_f64_matrix(5.0, 6.0, 7.0, 8.0);
        let expected = make_f64_matrix(6.0, 8.0, 10.0, 12.0);

        assert_eq!(m1.clone() + m2.clone(), expected, "M + M");
        assert_eq!(m1.clone() + &m2, expected, "M + &M");
        assert_eq!(&m1 + m2.clone(), expected, "&M + M");
        assert_eq!(&m1 + &m2, expected, "&M + &M");
    }

    #[test]
    fn test_add_scalar_f64() {
        let m1 = make_f64_matrix(1.0, 2.0, 3.0, 4.0);
        let scalar = 10.0;
        let expected = make_f64_matrix(11.0, 12.0, 13.0, 14.0);

        assert_eq!(m1.clone() + scalar, expected, "M + S");
        assert_eq!(&m1 + scalar, expected, "&M + S");
    }

    #[test]
    fn test_sub_f64() {
        let m1 = make_f64_matrix(10.0, 20.0, 30.0, 40.0);
        let m2 = make_f64_matrix(1.0, 2.0, 3.0, 4.0);
        let expected = make_f64_matrix(9.0, 18.0, 27.0, 36.0);

        assert_eq!(m1.clone() - m2.clone(), expected, "M - M");
        assert_eq!(m1.clone() - &m2, expected, "M - &M");
        assert_eq!(&m1 - m2.clone(), expected, "&M - M");
        assert_eq!(&m1 - &m2, expected, "&M - &M");
    }

    #[test]
    fn test_sub_scalar_f64() {
        let m1 = make_f64_matrix(11.0, 12.0, 13.0, 14.0);
        let scalar = 10.0;
        let expected = make_f64_matrix(1.0, 2.0, 3.0, 4.0);

        assert_eq!(m1.clone() - scalar, expected, "M - S");
        assert_eq!(&m1 - scalar, expected, "&M - S");
    }

    #[test]
    fn test_mul_f64() {
        // Element-wise
        let m1 = make_f64_matrix(1.0, 2.0, 3.0, 4.0);
        let m2 = make_f64_matrix(5.0, 6.0, 7.0, 8.0);
        let expected = make_f64_matrix(5.0, 12.0, 21.0, 32.0);

        assert_eq!(m1.clone() * m2.clone(), expected, "M * M");
        assert_eq!(m1.clone() * &m2, expected, "M * &M");
        assert_eq!(&m1 * m2.clone(), expected, "&M * M");
        assert_eq!(&m1 * &m2, expected, "&M * &M");
    }

    #[test]
    fn test_mul_scalar_f64() {
        let m1 = make_f64_matrix(1.0, 2.0, 3.0, 4.0);
        let scalar = 3.0;
        let expected = make_f64_matrix(3.0, 6.0, 9.0, 12.0);

        assert_eq!(m1.clone() * scalar, expected, "M * S");
        assert_eq!(&m1 * scalar, expected, "&M * S");
    }

    #[test]
    fn test_div_f64() {
        // Element-wise
        let m1 = make_f64_matrix(10.0, 20.0, 30.0, 40.0);
        let m2 = make_f64_matrix(2.0, 5.0, 6.0, 8.0);
        let expected = make_f64_matrix(5.0, 4.0, 5.0, 5.0);

        assert_eq!(m1.clone() / m2.clone(), expected, "M / M");
        assert_eq!(m1.clone() / &m2, expected, "M / &M");
        assert_eq!(&m1 / m2.clone(), expected, "&M / M");
        assert_eq!(&m1 / &m2, expected, "&M / &M");
    }

    #[test]
    fn test_div_scalar_f64() {
        let m1 = make_f64_matrix(10.0, 20.0, 30.0, 40.0);
        let scalar = 10.0;
        let expected = make_f64_matrix(1.0, 2.0, 3.0, 4.0);

        assert_eq!(m1.clone() / scalar, expected, "M / S");
        assert_eq!(&m1 / scalar, expected, "&M / S");
    }

    #[test]
    fn test_chained_ops_f64() {
        let m = make_f64_matrix(1.0, 2.0, 3.0, 4.0);
        let result = (((m.clone() + 1.0) * 2.0) - 4.0) / 2.0;
        // Expected:
        // m+1 = [2, 3], [4, 5]
        // *2  = [4, 6], [8, 10]
        // -4  = [0, 2], [4, 6]
        // /2  = [0, 1], [2, 3]
        let expected = make_f64_matrix(0.0, 1.0, 2.0, 3.0);
        assert_eq!(result, expected);
    }

    // --- Boolean Logic Tests ---

    #[test]
    fn test_bitand_bool() {
        let m1 = make_bool_matrix(true, false, true, false);
        let m2 = make_bool_matrix(true, true, false, false);
        let expected = make_bool_matrix(true, false, false, false);

        assert_eq!(m1.clone() & m2.clone(), expected, "M & M");
        assert_eq!(m1.clone() & &m2, expected, "M & &M");
        assert_eq!(&m1 & m2.clone(), expected, "&M & M");
        assert_eq!(&m1 & &m2, expected, "&M & &M");
    }

    #[test]
    fn test_bitor_bool() {
        let m1 = make_bool_matrix(true, false, true, false);
        let m2 = make_bool_matrix(true, true, false, false);
        let expected = make_bool_matrix(true, true, true, false);

        assert_eq!(m1.clone() | m2.clone(), expected, "M | M");
        assert_eq!(m1.clone() | &m2, expected, "M | &M");
        assert_eq!(&m1 | m2.clone(), expected, "&M | M");
        assert_eq!(&m1 | &m2, expected, "&M | &M");
    }

    #[test]
    fn test_bitxor_bool() {
        let m1 = make_bool_matrix(true, false, true, false);
        let m2 = make_bool_matrix(true, true, false, false);
        let expected = make_bool_matrix(false, true, true, false);

        assert_eq!(m1.clone() ^ m2.clone(), expected, "M ^ M");
        assert_eq!(m1.clone() ^ &m2, expected, "M ^ &M");
        assert_eq!(&m1 ^ m2.clone(), expected, "&M ^ M");
        assert_eq!(&m1 ^ &m2, expected, "&M ^ &M");
    }

    #[test]
    fn test_not_bool() {
        let m = make_bool_matrix(true, false, true, false);
        let expected = make_bool_matrix(false, true, false, true);

        assert_eq!(!m.clone(), expected, "!M (consuming)");
        assert_eq!(!&m, expected, "!&M (borrowing)");

        // Check original is unchanged when using !&M
        let original = make_bool_matrix(true, false, true, false);
        let _negated_ref = !&original;
        assert_eq!(original, make_bool_matrix(true, false, true, false));
    }

    // --- Comparison Tests ---
    #[test]
    fn test_comparison_eq_elem() {
        let m1 = make_f64_matrix(1.0, 2.0, 3.0, 4.0);
        let m2 = make_f64_matrix(1.0, 0.0, 3.0, 5.0);
        let s = 3.0;
        let expected_m = make_bool_matrix(true, false, true, false);
        let expected_s = make_bool_matrix(false, false, true, false);

        assert_eq!(m1.eq_elem(m2), expected_m, "eq_elem matrix");
        assert_eq!(m1.eq_elem(s), expected_s, "eq_elem scalar");
    }

    #[test]
    fn test_comparison_gt_elem() {
        let m1 = make_f64_matrix(1.0, 2.0, 3.0, 4.0);
        let m2 = make_f64_matrix(0.0, 3.0, 3.0, 5.0);
        let s = 2.5;
        let expected_m = make_bool_matrix(true, false, false, false);
        let expected_s = make_bool_matrix(false, false, true, true);

        assert_eq!(m1.gt_elem(m2), expected_m, "gt_elem matrix");
        assert_eq!(m1.gt_elem(s), expected_s, "gt_elem scalar");
    }

    // Add more comparison tests (lt, le, ge, ne) if desired...

    // --- Basic Method Tests ---
    #[test]
    fn test_indexing() {
        let m = make_f64_matrix(1.0, 2.0, 3.0, 4.0);
        assert_eq!(m[(0, 0)], 1.0);
        assert_eq!(m[(0, 1)], 2.0);
        assert_eq!(m[(1, 0)], 3.0);
        assert_eq!(m[(1, 1)], 4.0);
        assert_eq!(*m.get(1, 0), 3.0); // Test get() too
    }

    #[test]
    #[should_panic]
    fn test_index_out_of_bounds_row() {
        let m = make_f64_matrix(1.0, 2.0, 3.0, 4.0);
        let _ = m[(2, 0)]; // Row 2 is out of bounds
    }

    #[test]
    #[should_panic]
    fn test_index_out_of_bounds_col() {
        let m = make_f64_matrix(1.0, 2.0, 3.0, 4.0);
        let _ = m[(0, 2)]; // Col 2 is out of bounds
    }

    #[test]
    fn test_dimensions() {
        let m = make_f64_matrix(1.0, 2.0, 3.0, 4.0);
        assert_eq!(m.rows(), 2);
        assert_eq!(m.cols(), 2);
    }

    #[test]
    fn test_from_vec() {
        let data = vec![1.0, 3.0, 2.0, 4.0]; // Column major: [col0_row0, col0_row1, col1_row0, col1_row1]
        let m = Matrix::from_vec(data, 2, 2);
        let expected = make_f64_matrix(1.0, 2.0, 3.0, 4.0);
        assert_eq!(m, expected);
        assert_eq!(m.to_vec(), vec![1.0, 3.0, 2.0, 4.0]);
    }

    #[test]
    fn test_from_rows_vec() {
        // Matrix with rows [1, 2, 3] and [4, 5, 6]
        let rows_data = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
        let matrix = Matrix::from_rows_vec(rows_data, 2, 3);

        let data = vec![1.0, 4.0, 2.0, 5.0, 3.0, 6.0]; // Column-major
        let expected = Matrix::from_vec(data, 2, 3);

        assert_eq!(matrix, expected);
    }

    // Helper function to create a basic Matrix for testing
    fn static_test_matrix() -> Matrix<i32> {
        // Column-major data representing a 3x3 matrix of sequential integers
        let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
        Matrix::from_vec(data, 3, 3)
    }

    // Another helper for a different size
    fn static_test_matrix_2x4() -> Matrix<i32> {
        // Column-major data representing a 2x4 matrix of sequential integers
        let data = vec![1, 2, 3, 4, 5, 6, 7, 8];
        Matrix::from_vec(data, 2, 4)
    }

    #[test]
    fn test_from_vec_basic() {
        let data = vec![1, 2, 3, 4, 5, 6]; // 2 rows, 3 cols (column-major)
        let matrix = Matrix::from_vec(data, 2, 3);

        assert_eq!(matrix.rows(), 2);
        assert_eq!(matrix.cols(), 3);
        assert_eq!(matrix.data(), &[1, 2, 3, 4, 5, 6]);

        // Check some elements
        assert_eq!(matrix[(0, 0)], 1); // First row, first col
        assert_eq!(matrix[(1, 0)], 2); // Second row, first col
        assert_eq!(matrix[(0, 1)], 3); // First row, second col
        assert_eq!(matrix[(1, 2)], 6); // Second row, third col
    }

    #[test]
    fn test_transpose() {
        let matrix = static_test_matrix();
        let transposed = matrix.transpose();
        let round_triped = transposed.transpose();
        assert_eq!(
            round_triped, matrix,
            "Transposing twice should return original matrix"
        );
        for r in 0..matrix.rows() {
            for c in 0..matrix.cols() {
                assert_eq!(matrix[(r, c)], transposed[(c, r)]);
            }
        }
    }

    #[test]
    fn test_transpose_big() {
        let data: Vec<i32> = (1..=20000).collect(); //
        let matrix = Matrix::from_vec(data, 100, 200);
        let transposed = matrix.transpose();
        assert_eq!(transposed.rows(), 200);
        assert_eq!(transposed.cols(), 100);
        assert_eq!(transposed.data().len(), 20000);
        assert_eq!(transposed[(0, 0)], 1);

        let round_trip = transposed.transpose();

        assert_eq!(
            round_trip, matrix,
            "Transposing back should return original matrix"
        );
    }

    #[test]
    #[should_panic(expected = "data length mismatch")]
    fn test_from_vec_wrong_length() {
        let data = vec![1, 2, 3, 4, 5]; // Should be 6 for 2x3
        Matrix::from_vec(data, 2, 3);
    }

    #[test]
    #[should_panic(expected = "need at least one row")]
    fn test_from_vec_zero_rows() {
        let data = vec![1, 2, 3];
        Matrix::from_vec(data, 0, 3);
    }

    #[test]
    #[should_panic(expected = "need at least one column")]
    fn test_from_vec_zero_cols() {
        let data = vec![1, 2, 3];
        Matrix::from_vec(data, 3, 0);
    }

    #[test]
    fn test_from_cols_basic() {
        // Matrix with columns forming a 3x3 sequence
        let cols_data = vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]];
        let matrix = Matrix::from_cols(cols_data);

        assert_eq!(matrix.rows(), 3);
        assert_eq!(matrix.cols(), 3);
        // Internal data should be column-major
        assert_eq!(matrix.data(), &[1, 2, 3, 4, 5, 6, 7, 8, 9]);

        // Check some elements
        assert_eq!(matrix[(0, 0)], 1);
        assert_eq!(matrix[(2, 0)], 3);
        assert_eq!(matrix[(1, 1)], 5);
        assert_eq!(matrix[(0, 2)], 7);
    }

    #[test]
    fn test_from_cols_1x1() {
        let cols_data = vec![vec![42]];
        let matrix = Matrix::from_cols(cols_data);
        assert_eq!(matrix.rows(), 1);
        assert_eq!(matrix.cols(), 1);
        assert_eq!(matrix.data(), &[42]);
        assert_eq!(matrix[(0, 0)], 42);
    }

    #[test]
    #[should_panic(expected = "need at least one column")]
    fn test_from_cols_empty_cols() {
        let empty_cols: Vec<Vec<i32>> = vec![];
        Matrix::from_cols(empty_cols);
    }

    #[test]
    #[should_panic(expected = "need at least one row")]
    fn test_from_cols_empty_rows() {
        let empty_row: Vec<Vec<String>> = vec![vec![], vec![]];
        Matrix::from_cols(empty_row);
    }

    #[test]
    #[should_panic(expected = "col 1 has len 2, expected 3")]
    fn test_from_cols_mismatched_lengths() {
        let cols_data = vec![vec![1, 2, 3], vec![4, 5], vec![6, 7, 8]];
        Matrix::from_cols(cols_data);
    }

    #[test]
    fn test_getters() {
        let matrix = static_test_matrix();
        assert_eq!(matrix.rows(), 3);
        assert_eq!(matrix.cols(), 3);
        assert_eq!(matrix.data(), &[1, 2, 3, 4, 5, 6, 7, 8, 9]);
    }

    #[test]
    fn test_index_and_get() {
        let matrix = static_test_matrix();
        assert_eq!(matrix[(0, 0)], 1);
        assert_eq!(matrix[(1, 1)], 5);
        assert_eq!(matrix[(2, 2)], 9);

        assert_eq!(*matrix.get(0, 0), 1);
        assert_eq!(*matrix.get(1, 1), 5);
        assert_eq!(*matrix.get(2, 2), 9);
    }

    #[test]
    #[should_panic(expected = "index out of bounds")]
    fn test_index_out_of_bounds_row_alt() {
        let matrix = static_test_matrix();
        let _ = matrix[(3, 0)];
    }

    #[test]
    #[should_panic(expected = "index out of bounds")]
    fn test_index_out_of_bounds_col_alt() {
        let matrix = static_test_matrix();
        let _ = matrix[(0, 3)];
    }

    #[test]
    fn test_index_mut_and_get_mut() {
        let mut matrix = static_test_matrix();

        matrix[(0, 0)] = 10;
        matrix[(1, 1)] = 20;
        matrix[(2, 2)] = 30;

        assert_eq!(matrix[(0, 0)], 10);
        assert_eq!(matrix[(1, 1)], 20);
        assert_eq!(matrix[(2, 2)], 30);

        *matrix.get_mut(0, 1) = 15;
        *matrix.get_mut(2, 1) = 25;

        assert_eq!(matrix[(0, 1)], 15);
        assert_eq!(matrix[(2, 1)], 25);

        assert_eq!(matrix.data(), &[10, 2, 3, 15, 20, 25, 7, 8, 30]);
    }

    #[test]
    #[should_panic(expected = "index out of bounds")]
    fn test_index_mut_out_of_bounds_row() {
        let mut matrix = static_test_matrix();
        matrix[(3, 0)] = 99;
    }

    #[test]
    #[should_panic(expected = "index out of bounds")]
    fn test_index_mut_out_of_bounds_col() {
        let mut matrix = static_test_matrix();
        matrix[(0, 3)] = 99;
    }

    #[test]
    fn test_row() {
        let ma = static_test_matrix();
        assert_eq!(ma.row(0), &[1, 4, 7]);
        assert_eq!(ma.row(1), &[2, 5, 8]);
        assert_eq!(ma.row(2), &[3, 6, 9]);
    }

    #[test]
    fn test_row_copy_from_slice() {
        let mut ma = static_test_matrix();
        let new_row = vec![10, 20, 30];
        ma.row_copy_from_slice(1, &new_row);
        assert_eq!(ma.row(1), &[10, 20, 30]);
    }
    #[test]
    #[should_panic(expected = "row index 4 out of bounds for 3 rows")]
    fn test_row_copy_from_slice_out_of_bounds() {
        let mut ma = static_test_matrix();
        let new_row = vec![10, 20, 30];
        ma.row_copy_from_slice(4, &new_row);
    }

    #[test]
    #[should_panic(expected = "row index 3 out of bounds for 3 rows")]
    fn test_row_out_of_bounds_index() {
        let ma = static_test_matrix();
        ma.row(3);
    }

    #[test]
    #[should_panic(expected = "input slice length 2 does not match number of columns 3")]
    fn test_row_copy_from_slice_wrong_length() {
        let mut ma = static_test_matrix();
        let new_row = vec![10, 20]; // Only 2 elements, but row length is 3
        ma.row_copy_from_slice(1, &new_row);
    }

    #[test]
    fn test_shape() {
        let ma = static_test_matrix_2x4();
        assert_eq!(ma.shape(), (2, 4));
        assert_eq!(ma.rows(), 2);
        assert_eq!(ma.cols(), 4);
    }

    #[test]
    fn test_repeat_rows() {
        let ma = static_test_matrix();
        // Returns a new matrix where row 0 of `self` is repeated `n` times.
        let repeated = ma.repeat_rows(3);
        // assert all rows are equal to the first row
        for r in 0..repeated.rows() {
            assert_eq!(repeated.row(r), ma.row(0));
        }
    }

    #[test]
    #[should_panic(expected = "row index 3 out of bounds for 3 rows")]
    fn test_row_out_of_bounds() {
        let ma = static_test_matrix();
        ma.row(3);
    }

    #[test]
    fn test_column() {
        let matrix = static_test_matrix_2x4();

        assert_eq!(matrix.column(0), &[1, 2]);
        assert_eq!(matrix.column(1), &[3, 4]);
        assert_eq!(matrix.column(2), &[5, 6]);
        assert_eq!(matrix.column(3), &[7, 8]);
    }

    #[test]
    #[should_panic(expected = "column index 4 out of bounds for 4 columns")]
    fn test_column_out_of_bounds() {
        let matrix = static_test_matrix_2x4();
        matrix.column(4);
    }

    #[test]
    fn test_column_mut() {
        let mut matrix = static_test_matrix_2x4();

        let col1_mut = matrix.column_mut(1);
        col1_mut[0] = 30;
        col1_mut[1] = 40;

        let col3_mut = matrix.column_mut(3);
        col3_mut[0] = 70;

        // Check changes via indexing
        assert_eq!(matrix[(0, 1)], 30);
        assert_eq!(matrix[(1, 1)], 40);
        assert_eq!(matrix[(0, 3)], 70);
        assert_eq!(matrix[(1, 3)], 8); // Unchanged

        assert_eq!(matrix.data(), &[1, 2, 30, 40, 5, 6, 70, 8]);
    }

    #[test]
    #[should_panic(expected = "column index 4 out of bounds for 4 columns")]
    fn test_column_mut_out_of_bounds() {
        let mut matrix = static_test_matrix_2x4();
        matrix.column_mut(4);
    }

    #[test]
    fn test_iter_columns() {
        let matrix = static_test_matrix_2x4();

        let cols: Vec<&[i32]> = matrix.iter_columns().collect();
        assert_eq!(cols.len(), 4);
        assert_eq!(cols[0], &[1, 2]);
        assert_eq!(cols[1], &[3, 4]);
        assert_eq!(cols[2], &[5, 6]);
        assert_eq!(cols[3], &[7, 8]);
    }

    #[test]
    fn test_iter_rows() {
        let matrix = static_test_matrix_2x4();

        let rows: Vec<Vec<i32>> = matrix
            .iter_rows()
            .map(|row| row.iter().cloned().collect())
            .collect();
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0], vec![1, 3, 5, 7]);
        assert_eq!(rows[1], vec![2, 4, 6, 8]);
    }

    // test data_mut
    #[test]
    fn test_data_mut() {
        let mut matrix = static_test_matrix();

        let data_mut = matrix.data_mut();
        data_mut[0] = 10;
        data_mut[1] = 20;

        assert_eq!(matrix[(0, 0)], 10);
        assert_eq!(matrix[(1, 0)], 20);
    }

    #[test]
    fn test_matrix_row_get_and_iter() {
        let matrix = static_test_matrix_2x4();
        let row0 = matrix.iter_rows().next().unwrap();
        assert_eq!(*row0.get(0), 1);
        assert_eq!(*row0.get(1), 3);
        assert_eq!(*row0.get(3), 7);
        let row0_vec: Vec<i32> = row0.iter().cloned().collect();
        assert_eq!(row0_vec, vec![1, 3, 5, 7]);

        let row1 = matrix.iter_rows().nth(1).unwrap();
        assert_eq!(*row1.get(0), 2);
        assert_eq!(*row1.get(2), 6);
        let row1_vec: Vec<i32> = row1.iter().cloned().collect();
        assert_eq!(row1_vec, vec![2, 4, 6, 8]);
    }

    #[test]
    fn test_swap_columns() {
        let mut matrix = static_test_matrix();

        matrix.swap_columns(0, 2); // swap first and last

        assert_eq!(matrix.rows(), 3);
        assert_eq!(matrix.cols(), 3);
        assert_eq!(matrix[(0, 0)], 7);
        assert_eq!(matrix[(1, 0)], 8);
        assert_eq!(matrix[(2, 0)], 9);
        assert_eq!(matrix[(0, 1)], 4); // Middle col unchanged
        assert_eq!(matrix[(1, 1)], 5);
        assert_eq!(matrix[(2, 1)], 6);
        assert_eq!(matrix[(0, 2)], 1);
        assert_eq!(matrix[(1, 2)], 2);
        assert_eq!(matrix[(2, 2)], 3);

        // swap the same column (should do nothing)
        let original_data = matrix.data().to_vec();
        matrix.swap_columns(1, 1);
        assert_eq!(matrix.data(), &original_data); // Data should be identical

        // Check underlying data (column-major) after swap(0, 2)
        assert_eq!(matrix.data(), &[7, 8, 9, 4, 5, 6, 1, 2, 3]);
    }

    #[test]
    #[should_panic(expected = "column index c2=3 out of bounds for 3 columns")]
    fn test_swap_columns_out_of_bounds() {
        let mut matrix = static_test_matrix();
        matrix.swap_columns(0, 3);
    }

    #[test]
    fn test_delete_column() {
        let mut matrix = static_test_matrix_2x4();
        matrix.delete_column(1); // Delete the second column (index 1)

        assert_eq!(matrix.rows(), 2);
        assert_eq!(matrix.cols(), 3);
        assert_eq!(matrix[(0, 0)], 1);
        assert_eq!(matrix[(1, 0)], 2);
        assert_eq!(matrix[(0, 1)], 5);
        assert_eq!(matrix[(1, 1)], 6);
        assert_eq!(matrix[(0, 2)], 7);
        assert_eq!(matrix[(1, 2)], 8);

        // check underlying data
        assert_eq!(matrix.data(), &[1, 2, 5, 6, 7, 8]);

        // Delete the first column
        matrix.delete_column(0);
        assert_eq!(matrix.rows(), 2);
        assert_eq!(matrix.cols(), 2);
        assert_eq!(matrix.data(), &[5, 6, 7, 8]);

        // Delete the last column
        matrix.delete_column(1);
        assert_eq!(matrix.rows(), 2);
        assert_eq!(matrix.cols(), 1);
        assert_eq!(matrix.data(), &[5, 6]);

        // Delete the only column
        matrix.delete_column(0);
        // Should be empty
        assert_eq!(matrix.rows(), 2); // Rows stay the same
        assert_eq!(matrix.cols(), 0); // Cols becomes 0
        assert_eq!(matrix.data(), &[]);
    }

    #[test]
    #[should_panic(expected = "column index 4 out of bounds for 4 columns")]
    fn test_delete_column_out_of_bounds() {
        let mut matrix = static_test_matrix_2x4();
        matrix.delete_column(4);
    }

    #[test]
    fn test_delete_row() {
        let mut matrix = static_test_matrix();

        matrix.delete_row(1); // Delete the second row

        assert_eq!(matrix.rows(), 2);
        assert_eq!(matrix.cols(), 3);
        assert_eq!(matrix[(0, 0)], 1);
        assert_eq!(matrix[(1, 0)], 3);
        assert_eq!(matrix[(0, 1)], 4);
        assert_eq!(matrix[(1, 1)], 6);
        assert_eq!(matrix[(0, 2)], 7);
        assert_eq!(matrix[(1, 2)], 9);

        // check underlying data (column-major)
        assert_eq!(matrix.data(), &[1, 3, 4, 6, 7, 9]);

        // Delete the first row
        matrix.delete_row(0);
        // Resulting data should be [3, 6, 9]
        assert_eq!(matrix.rows(), 1);
        assert_eq!(matrix.cols(), 3);
        assert_eq!(matrix.data(), &[3, 6, 9]);

        // Delete the last (and only) row
        matrix.delete_row(0);
        // Should be empty
        assert_eq!(matrix.rows(), 0); // Rows becomes 0
        assert_eq!(matrix.cols(), 3); // Cols stay the same
        assert_eq!(matrix.data(), &[]);
    }

    #[test]
    #[should_panic(expected = "row index 3 out of bounds for 3 rows")]
    fn test_delete_row_out_of_bounds() {
        let mut matrix = static_test_matrix();
        matrix.delete_row(3);
    }

    #[test]
    fn test_add_column() {
        let mut matrix = static_test_matrix_2x4();
        let new_col = vec![9, 10];
        matrix.add_column(2, new_col);

        assert_eq!(matrix.rows(), 2);
        assert_eq!(matrix.cols(), 5);
        assert_eq!(matrix[(0, 0)], 1);
        assert_eq!(matrix[(1, 0)], 2);
        assert_eq!(matrix[(0, 1)], 3);
        assert_eq!(matrix[(1, 1)], 4);
        assert_eq!(matrix[(0, 2)], 9);
        assert_eq!(matrix[(1, 2)], 10);
        assert_eq!(matrix[(0, 3)], 5); // Shifted
        assert_eq!(matrix[(1, 3)], 6);
        assert_eq!(matrix[(0, 4)], 7); // Shifted
        assert_eq!(matrix[(1, 4)], 8);

        // Check underlying data
        assert_eq!(matrix.data(), &[1, 2, 3, 4, 9, 10, 5, 6, 7, 8]);

        // Add a column at the beginning
        let new_col_start = vec![11, 12];
        matrix.add_column(0, new_col_start);
        assert_eq!(matrix.rows(), 2);
        assert_eq!(matrix.cols(), 6);
        assert_eq!(matrix[(0, 0)], 11);
        assert_eq!(matrix[(1, 0)], 12);
        assert_eq!(matrix.data(), &[11, 12, 1, 2, 3, 4, 9, 10, 5, 6, 7, 8]);

        // Add a column at the end
        let new_col_end = vec![13, 14];
        matrix.add_column(6, new_col_end);
        assert_eq!(matrix.rows(), 2);
        assert_eq!(matrix.cols(), 7);
        assert_eq!(matrix[(0, 6)], 13);
        assert_eq!(matrix[(1, 6)], 14);
        assert_eq!(
            matrix.data(),
            &[11, 12, 1, 2, 3, 4, 9, 10, 5, 6, 7, 8, 13, 14]
        );
    }

    #[test]
    #[should_panic(expected = "add_column index 5 out of bounds for 4 columns")]
    fn test_add_column_out_of_bounds() {
        let mut matrix = static_test_matrix_2x4();
        let new_col = vec![9, 10];
        matrix.add_column(5, new_col); // Index 5 is out of bounds for 4 columns
    }

    #[test]
    #[should_panic(expected = "column length mismatch")]
    fn test_add_column_length_mismatch() {
        let mut matrix = static_test_matrix_2x4();
        let new_col = vec![9, 10, 11]; // Wrong length
        matrix.add_column(0, new_col);
    }

    #[test]
    fn test_add_row() {
        let mut matrix = static_test_matrix_2x4();
        let new_row = vec![9, 10, 11, 12];
        matrix.add_row(1, new_row);

        assert_eq!(matrix.rows(), 3);
        assert_eq!(matrix.cols(), 4);

        assert_eq!(matrix[(0, 0)], 1);
        assert_eq!(matrix[(0, 1)], 3);
        assert_eq!(matrix[(0, 2)], 5);
        assert_eq!(matrix[(0, 3)], 7);
        assert_eq!(matrix[(1, 0)], 9);
        assert_eq!(matrix[(1, 1)], 10);
        assert_eq!(matrix[(1, 2)], 11);
        assert_eq!(matrix[(1, 3)], 12);
        assert_eq!(matrix[(2, 0)], 2);
        assert_eq!(matrix[(2, 1)], 4);
        assert_eq!(matrix[(2, 2)], 6);
        assert_eq!(matrix[(2, 3)], 8);

        // Check underlying data (column-major)
        assert_eq!(matrix.data(), &[1, 9, 2, 3, 10, 4, 5, 11, 6, 7, 12, 8]);

        // Add a row at the beginning
        let new_row_start = vec![13, 14, 15, 16];
        matrix.add_row(0, new_row_start);
        assert_eq!(matrix.rows(), 4);
        assert_eq!(matrix.cols(), 4);
        assert_eq!(matrix[(0, 0)], 13);
        assert_eq!(matrix[(0, 1)], 14);
        assert_eq!(matrix[(0, 2)], 15);
        assert_eq!(matrix[(0, 3)], 16);
        assert_eq!(matrix[(1, 0)], 1);
        assert_eq!(matrix[(2, 1)], 10);
        assert_eq!(matrix[(3, 3)], 8);

        // Add a row at the end
        let new_row_end = vec![17, 18, 19, 20];
        matrix.add_row(4, new_row_end);
        assert_eq!(matrix.rows(), 5);
        assert_eq!(matrix.cols(), 4);
        assert_eq!(matrix[(4, 0)], 17);
        assert_eq!(matrix[(4, 3)], 20);
    }

    #[test]
    #[should_panic(expected = "add_row index 3 out of bounds for 2 rows")]
    fn test_add_row_out_of_bounds() {
        let mut matrix = static_test_matrix_2x4();
        let new_row = vec![9, 10, 11, 12];
        matrix.add_row(3, new_row); // Index 3 is out of bounds for 2 rows
    }

    #[test]
    #[should_panic(expected = "row length mismatch")]
    fn test_add_row_length_mismatch() {
        let mut matrix = static_test_matrix_2x4();
        let new_row = vec![9, 10, 11]; // Wrong length
        matrix.add_row(0, new_row);
    }

    #[test]
    fn test_elementwise_add() {
        let matrix1 = static_test_matrix();
        let matrix2 = Matrix::from_vec(vec![9, 8, 7, 6, 5, 4, 3, 2, 1], 3, 3);

        let result = &matrix1 + &matrix2;

        assert_eq!(result.rows(), 3);
        assert_eq!(result.cols(), 3);

        // Expect all 10s
        assert_eq!(result.data(), &[10, 10, 10, 10, 10, 10, 10, 10, 10]);
        assert_eq!(result[(0, 0)], 10);
        assert_eq!(result[(1, 1)], 10);
        assert_eq!(result[(2, 2)], 10);
    }

    #[test]
    fn test_elementwise_sub() {
        let matrix1 = static_test_matrix();
        let matrix2 = Matrix::from_vec(vec![1, 1, 1, 2, 2, 2, 3, 3, 3], 3, 3);

        let result = &matrix1 - &matrix2;

        assert_eq!(result.rows(), 3);
        assert_eq!(result.cols(), 3);

        assert_eq!(result.data(), &[0, 1, 2, 2, 3, 4, 4, 5, 6]);
        assert_eq!(result[(0, 0)], 0);
        assert_eq!(result[(1, 1)], 3);
        assert_eq!(result[(2, 2)], 6);
    }

    #[test]
    fn test_elementwise_mul() {
        let matrix1 = static_test_matrix();
        let matrix2 = Matrix::from_vec(vec![1, 2, 3, 1, 2, 3, 1, 2, 3], 3, 3);

        let result = &matrix1 * &matrix2;

        assert_eq!(result.rows(), 3);
        assert_eq!(result.cols(), 3);

        // Expected
        assert_eq!(result.data(), &[1, 4, 9, 4, 10, 18, 7, 16, 27]);
        assert_eq!(result[(0, 0)], 1);
        assert_eq!(result[(1, 1)], 10);
        assert_eq!(result[(2, 2)], 27);
    }

    #[test]
    fn test_elementwise_div() {
        let matrix1 = static_test_matrix();
        let matrix2 = Matrix::from_vec(vec![1, 1, 1, 2, 2, 2, 7, 8, 9], 3, 3);

        let result = &matrix1 / &matrix2; // Integer division

        assert_eq!(result.rows(), 3);
        assert_eq!(result.cols(), 3);

        assert_eq!(result.data(), &[1, 2, 3, 2, 2, 3, 1, 1, 1]);
        assert_eq!(result[(0, 0)], 1);
        assert_eq!(result[(1, 1)], 2);
        assert_eq!(result[(2, 2)], 1);
    }

    #[test]
    #[should_panic(expected = "Row count mismatch: left has 3 rows, right has 2 rows")]
    fn test_elementwise_op_row_mismatch() {
        let matrix1 = static_test_matrix();
        let matrix2 = static_test_matrix_2x4();
        let _ = &matrix1 + &matrix2; // Should panic
    }

    #[test]
    #[should_panic(expected = "Row count mismatch: left has 3 rows, right has 2 ro")]
    fn test_elementwise_op_col_mismatch() {
        let matrix1 = static_test_matrix();
        let matrix2 = static_test_matrix_2x4();
        let _ = &matrix1 * &matrix2; // Should panic
    }

    #[test]
    fn test_bitwise_and() {
        let data1 = vec![true, false, true, false, true, false];
        let data2 = vec![true, true, false, false, true, true];
        let matrix1 = BoolMatrix::from_vec(data1, 2, 3);
        let matrix2 = BoolMatrix::from_vec(data2, 2, 3);

        let expected_data = vec![true, false, false, false, true, false];
        let expected_matrix = BoolMatrix::from_vec(expected_data, 2, 3);

        let result = &matrix1 & &matrix2;
        assert_eq!(result, expected_matrix);
    }

    #[test]
    fn test_bitwise_or() {
        let data1 = vec![true, false, true, false, true, false];
        let data2 = vec![true, true, false, false, true, true];
        let matrix1 = BoolMatrix::from_vec(data1, 2, 3);
        let matrix2 = BoolMatrix::from_vec(data2, 2, 3);

        let expected_data = vec![true, true, true, false, true, true];
        let expected_matrix = BoolMatrix::from_vec(expected_data, 2, 3);

        let result = &matrix1 | &matrix2;
        assert_eq!(result, expected_matrix);
    }

    #[test]
    fn test_bitwise_xor() {
        let data1 = vec![true, false, true, false, true, false];
        let data2 = vec![true, true, false, false, true, true];
        let matrix1 = BoolMatrix::from_vec(data1, 2, 3);
        let matrix2 = BoolMatrix::from_vec(data2, 2, 3);

        let expected_data = vec![false, true, true, false, false, true];
        let expected_matrix = BoolMatrix::from_vec(expected_data, 2, 3);

        let result = &matrix1 ^ &matrix2;
        assert_eq!(result, expected_matrix);
    }

    #[test]
    fn test_bitwise_not() {
        let data = vec![true, false, true, false, true, false];
        let matrix = BoolMatrix::from_vec(data, 2, 3);

        let expected_data = vec![false, true, false, true, false, true];
        let expected_matrix = BoolMatrix::from_vec(expected_data, 2, 3);

        let result = !matrix; // Not consumes the matrix
        assert_eq!(result, expected_matrix);
    }

    #[test]
    #[should_panic(expected = "Column count mismatch: left has 2 columns, right has 3 columns")]
    fn test_bitwise_op_row_mismatch() {
        let data1 = vec![true, false, true, false];
        let data2 = vec![true, true, false, false, true, true];
        let matrix1 = BoolMatrix::from_vec(data1, 2, 2);
        let matrix2 = BoolMatrix::from_vec(data2, 2, 3);
        let _ = &matrix1 & &matrix2; // Should panic
    }

    #[test]
    #[should_panic(expected = "Column count mismatch: left has 2 columns, right has 3 columns")]
    fn test_bitwise_op_col_mismatch() {
        let data1 = vec![true, false, true, false];
        let data2 = vec![true, true, false, false, true, true];
        let matrix1 = BoolMatrix::from_vec(data1, 2, 2);
        let matrix2 = BoolMatrix::from_vec(data2, 2, 3);
        let _ = &matrix1 | &matrix2; // Should panic
    }

    // Test with String type (requires Clone, PartialEq)
    #[test]
    fn test_string_matrix() {
        let data = vec![
            "a".to_string(),
            "b".to_string(),
            "c".to_string(),
            "d".to_string(),
        ];
        let matrix = StringMatrix::from_vec(data.clone(), 2, 2);

        assert_eq!(matrix[(0, 0)], "a".to_string());
        assert_eq!(matrix[(1, 0)], "b".to_string());
        assert_eq!(matrix[(0, 1)], "c".to_string());
        assert_eq!(matrix[(1, 1)], "d".to_string());

        // Test modification
        let mut matrix = matrix;
        matrix[(0, 0)] = "hello".to_string();
        assert_eq!(matrix[(0, 0)], "hello".to_string());

        // Test add_column
        let new_col = vec!["e".to_string(), "f".to_string()];
        matrix.add_column(1, new_col);

        assert_eq!(matrix.rows(), 2);
        assert_eq!(matrix.cols(), 3);
        assert_eq!(matrix[(0, 0)], "hello".to_string());
        assert_eq!(matrix[(1, 0)], "b".to_string());
        assert_eq!(matrix[(0, 1)], "e".to_string()); // New col
        assert_eq!(matrix[(1, 1)], "f".to_string()); // New col
        assert_eq!(matrix[(0, 2)], "c".to_string()); // Shifted
        assert_eq!(matrix[(1, 2)], "d".to_string()); // Shifted

        // Test add_row (requires Clone)
        let new_row = vec!["g".to_string(), "h".to_string(), "i".to_string()];
        matrix.add_row(0, new_row);

        assert_eq!(matrix.rows(), 3);
        assert_eq!(matrix.cols(), 3);
        assert_eq!(matrix[(0, 0)], "g".to_string());
        assert_eq!(matrix[(0, 1)], "h".to_string());
        assert_eq!(matrix[(0, 2)], "i".to_string());
        assert_eq!(matrix[(1, 0)], "hello".to_string()); // Shifted
        assert_eq!(matrix[(2, 2)], "d".to_string()); // Shifted
    }

    #[test]
    fn test_float_matrix_ops() {
        let data1 = vec![1.0, 2.0, 3.0, 4.0];
        let data2 = vec![0.5, 1.5, 2.5, 3.5];
        let matrix1 = FloatMatrix::from_vec(data1, 2, 2);
        let matrix2 = FloatMatrix::from_vec(data2, 2, 2);

        let sum = &matrix1 + &matrix2;
        let diff = &matrix1 - &matrix2;
        let prod = &matrix1 * &matrix2;
        let div = &matrix1 / &matrix2;

        assert_eq!(sum.data(), &[1.5, 3.5, 5.5, 7.5]);

        assert_eq!(diff.data(), &[0.5, 0.5, 0.5, 0.5]);

        assert_eq!(prod.data(), &[0.5, 3.0, 7.5, 14.0]);

        // Check divs (col-major): [2.0, 1.333..., 1.2, 1.14...]
        assert_eq!(div.rows(), 2);
        assert_eq!(div.cols(), 2);
        assert!((div[(0, 0)] - 1.0 / 0.5).abs() < 1e-9); // 2.0
        assert!((div[(1, 0)] - 2.0 / 1.5).abs() < 1e-9); // 1.333...
        assert!((div[(0, 1)] - 3.0 / 2.5).abs() < 1e-9); // 1.2
        assert!((div[(1, 1)] - 4.0 / 3.5).abs() < 1e-9); // 1.14...
    }

    fn create_test_matrix_i32() -> Matrix<i32> {
        Matrix::from_cols(vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]])
    }

    #[test]
    fn test_matrix_swap_columns_directly() {
        let mut matrix = create_test_matrix_i32();

        // Store the initial state of the columns we intend to swap AND one that shouldn't change
        let initial_col0_data = matrix.column(0).to_vec(); // Should be [1, 2, 3]
        let initial_col1_data = matrix.column(1).to_vec(); // Should be [4, 5, 6]
        let initial_col2_data = matrix.column(2).to_vec(); // Should be [7, 8, 9]

        // Perform the swap directly on the matrix
        matrix.swap_columns(0, 2); // Swap column 0 and column 2

        // Verify the dimensions are unchanged
        assert_eq!(matrix.rows(), 3, "Matrix rows should remain unchanged");
        assert_eq!(matrix.cols(), 3, "Matrix cols should remain unchanged");

        // Verify the column that was NOT swapped is unchanged
        assert_eq!(
            matrix.column(1),
            initial_col1_data.as_slice(), // Comparing slice to slice
            "Column 1 data should be unchanged"
        );

        // Verify the data swap occurred correctly using the COLUMN ACCESSOR
        // The data originally at index 0 should now be at index 2
        assert_eq!(
            matrix.column(2),
            initial_col0_data.as_slice(),
            "Column 2 should now contain the original data from column 0"
        );
        // The data originally at index 2 should now be at index 0
        assert_eq!(
            matrix.column(0),
            initial_col2_data.as_slice(),
            "Column 0 should now contain the original data from column 2"
        );

        // Verify the underlying raw data vector
        assert_eq!(
            matrix.data(),
            &[7, 8, 9, 4, 5, 6, 1, 2, 3],
            "Underlying data vector is incorrect after swap"
        );

        // Test swapping with self (should be a no-op)
        let state_before_self_swap = matrix.clone();
        matrix.swap_columns(1, 1);
        assert_eq!(
            matrix, state_before_self_swap,
            "Swapping a column with itself should not change the matrix"
        );

        // Test swapping adjacent columns
        let mut matrix2 = create_test_matrix_i32();
        let initial_col0_data_m2 = matrix2.column(0).to_vec();
        let initial_col1_data_m2 = matrix2.column(1).to_vec();
        matrix2.swap_columns(0, 1);
        assert_eq!(matrix2.column(0), initial_col1_data_m2.as_slice());
        assert_eq!(matrix2.column(1), initial_col0_data_m2.as_slice());
        assert_eq!(matrix2.data(), &[4, 5, 6, 1, 2, 3, 7, 8, 9]);
    }

    // Test broadcastable operations
    #[test]
    fn test_comparision_broadcast() {
        let matrix = static_test_matrix();
        // test all > 0
        let result = matrix.gt_elem(0).into_vec();
        let expected = vec![true; result.len()];
        assert_eq!(result, expected);

        let ma = static_test_matrix();
        let mb = static_test_matrix();

        let result = ma.eq_elem(mb);
        assert!(result.all());

        let result = matrix.lt_elem(1e10 as i32).all();
        assert!(result);

        for i in 0..matrix.rows() {
            for j in 0..matrix.cols() {
                let vx = matrix[(i, j)];
                let c = &(matrix.le_elem(vx)) & &(matrix.ge_elem(vx));
                assert_eq!(c.count(), 1);
            }
        }
    }

    #[test]
    fn test_arithmetic_broadcast() {
        let matrix = static_test_matrix();
        let result = &matrix + 1;
        for i in 0..matrix.rows() {
            for j in 0..matrix.cols() {
                assert_eq!(result[(i, j)], matrix[(i, j)] + 1);
            }
        }

        // test mul and div
        let result = &matrix * 2;
        let result2 = &matrix / 2;
        for i in 0..matrix.rows() {
            for j in 0..matrix.cols() {
                assert_eq!(result[(i, j)], matrix[(i, j)] * 2);
                assert_eq!(result2[(i, j)], matrix[(i, j)] / 2);
            }
        }
    }

    #[test]
    fn test_matrix_zeros_ones_filled() {
        // Test zeros
        let m = Matrix::<f64>::zeros(2, 3);
        assert_eq!(m.rows(), 2);
        assert_eq!(m.cols(), 3);
        assert_eq!(m.data(), &[0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);

        // Test ones
        let m = Matrix::<f64>::ones(3, 2);
        assert_eq!(m.rows(), 3);
        assert_eq!(m.cols(), 2);
        assert_eq!(m.data(), &[1.0, 1.0, 1.0, 1.0, 1.0, 1.0]);

        // Test filled
        let m = Matrix::<f64>::filled(2, 2, 42.5);
        assert_eq!(m.rows(), 2);
        assert_eq!(m.cols(), 2);
        assert_eq!(m.data(), &[42.5, 42.5, 42.5, 42.5]);

        // test with an integer matrix
        let m = Matrix::<i32>::filled(2, 3, 7);
        assert_eq!(m.rows(), 2);
        assert_eq!(m.cols(), 3);
        assert_eq!(m.data(), &[7, 7, 7, 7, 7, 7]);

        // test with nans
        let m = Matrix::nan(3, 3);
        assert_eq!(m.rows(), 3);
        assert_eq!(m.cols(), 3);
        for &value in m.data() {
            assert!(value.is_nan(), "Expected NaN, got {}", value);
        }
    }

    #[test]
    fn test_broadcast_row_to_target_shape_basic() {
        let single_row_matrix = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0], 1, 3);
        let target_rows = 5;
        let target_cols = 3;

        let broadcasted = single_row_matrix.broadcast_row_to_target_shape(target_rows, target_cols);

        assert_eq!(broadcasted.rows(), target_rows);
        assert_eq!(broadcasted.cols(), target_cols);

        for r in 0..target_rows {
            assert_eq!(broadcasted.row(r), vec![1.0, 2.0, 3.0]);
        }
    }

    #[test]
    fn test_broadcast_row_to_target_shape_single_row() {
        let single_row_matrix = Matrix::from_rows_vec(vec![10.0, 20.0], 1, 2);
        let target_rows = 1;
        let target_cols = 2;

        let broadcasted = single_row_matrix.broadcast_row_to_target_shape(target_rows, target_cols);

        assert_eq!(broadcasted.rows(), target_rows);
        assert_eq!(broadcasted.cols(), target_cols);
        assert_eq!(broadcasted.row(0), vec![10.0, 20.0]);
    }

    #[test]
    #[should_panic(
        expected = "broadcast_row_to_target_shape can only be called on a 1-row matrix."
    )]
    fn test_broadcast_row_to_target_shape_panic_not_1_row() {
        let multi_row_matrix = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
        multi_row_matrix.broadcast_row_to_target_shape(3, 2);
    }

    #[test]
    #[should_panic(
        expected = "Column count mismatch for broadcasting: source has 3 columns, target has 4 columns."
    )]
    fn test_broadcast_row_to_target_shape_panic_col_mismatch() {
        let single_row_matrix = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0], 1, 3);
        single_row_matrix.broadcast_row_to_target_shape(5, 4);
    }
}