diffsl 0.9.4

A compiler for a domain-specific language for ordinary differential equations (ODE).
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
use anyhow::{anyhow, Result};
use ndarray::s;
use std::{
    cmp::min,
    convert::AsRef,
    fmt,
    hash::{Hash, Hasher},
    iter::zip,
    mem,
    ops::Deref,
    sync::Arc,
};

use crate::{ast::AstKind, discretise::Env};

use super::{broadcast_shapes, shape::Shape, Index, TensorBlock};

#[derive(Debug, Clone, PartialEq)]
pub enum LayoutKind {
    Dense,
    Diagonal,
    Sparse,
}

#[derive(Debug, Clone, Copy)]
pub enum TensorType {
    State,
    StateDot,
    Input,
    Other,
}

pub type NonZero = (Index, usize);

/// a sparsity pattern for a multidimensional tensor. A tensor can be sparse, diagonal or dense, as given by the kind field.
/// A tensor can also have n_dense_axes axes which are dense, these are the last n_dense_axes axes of the tensor. So for example,
/// you could have a 2D sparse tensor with 1 dense axis, combining to give a tensor of rank 3 (i.e. the shape is length 3).
/// indices are kept in row major order, so the last index is the fastest changing index.
///
/// For sparse layouts, the indices field contains the list of non-zero indices, excluding the dense axes.
/// For diagonal layouts, the indices field is empty, as the non-zero indices are implicit.
/// For dense layouts, the indices field is empty, as all indices are non-zero.
///
/// Each layout contains a vector of state and input dependencies, which are pairs of (index, state/input_j), indicating that the non-zero at index depends on state/input j.
#[derive(Debug, Clone)]
pub struct Layout {
    indices: Vec<Index>,
    state_deps: Vec<NonZero>,
    input_deps: Vec<NonZero>,
    shape: Shape,
    kind: LayoutKind,
    n_dense_axes: usize,
}

impl fmt::Display for Layout {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.is_scalar() {
            return Ok(());
        }
        write!(f, " (")?;
        for i in 0..self.rank() {
            let type_char = if self.is_diagonal() && i < self.rank() - self.n_dense_axes {
                Some('i')
            } else if self.is_sparse() && i < self.rank() - self.n_dense_axes {
                Some('s')
            } else {
                None
            };
            if let Some(type_char) = type_char {
                write!(f, "{}{}", self.shape()[i], type_char)?;
            } else {
                write!(f, "{}", self.shape()[i])?;
            }
            if i < self.rank() - 1 {
                write!(f, ", ")?;
            }
        }
        write!(f, ")")
    }
}

impl Layout {
    /// Merge two dependency lists, removing duplicates.
    fn merge_deps(mut a: Vec<(Index, usize)>, mut b: Vec<(Index, usize)>) -> Vec<(Index, usize)> {
        a.append(&mut b);
        a.sort_unstable_by(|x, y| Self::cmp_index(&x.0, &y.0).then(x.1.cmp(&y.1)));
        a.dedup();
        a
    }

    /// Check if two layouts are equal in terms of their non-zero patterns.
    pub fn eq_nonzeros(&self, other: &Layout) -> bool {
        if self.kind != other.kind {
            return false;
        }
        if self.n_dense_axes != other.n_dense_axes {
            return false;
        }
        if self.shape != other.shape {
            return false;
        }
        if self.indices != other.indices {
            return false;
        }
        true
    }

    /// Check if two layouts are equal in terms of their non-zero patterns and dependencies.
    pub fn eq_nonzeros_and_deps(&self, other: &Layout) -> bool {
        if !self.eq_nonzeros(other) {
            return false;
        }
        if self.state_deps != other.state_deps {
            return false;
        }
        if self.input_deps != other.input_deps {
            return false;
        }
        true
    }

    /// Filter dependencies to only include those with indices in the provided iterator. The iterator will always
    /// be in the same sorted order as deps.
    fn filter_deps(
        new_deps_rank: usize,
        deps: Vec<NonZero>,
        filtered_indices: impl Iterator<Item = Index>,
    ) -> Vec<NonZero> {
        let mut filtered_deps = Vec::new();
        let mut dep_iter = deps.iter();
        let mut current_dep = dep_iter.next();
        for index in filtered_indices {
            while let Some((ref dep_index, dep_j)) = current_dep {
                match Self::cmp_index(dep_index, &index) {
                    std::cmp::Ordering::Less => {
                        current_dep = dep_iter.next();
                    }
                    std::cmp::Ordering::Greater => {
                        break;
                    }
                    std::cmp::Ordering::Equal => {
                        filtered_deps.push((index.slice(s![..new_deps_rank]).to_owned(), *dep_j));
                        current_dep = dep_iter.next();
                    }
                }
            }
        }
        filtered_deps
    }

    /// Remap dependency pairs from a source layout into a destination layout using an index mapping function.
    /// `map_fn` may return multiple destination indices for a single source index (e.g. broadcasting).
    fn remap_dependencies<F>(
        state_deps: &[NonZero],
        input_deps: &[NonZero],
        map_fn: F,
    ) -> (Vec<NonZero>, Vec<NonZero>)
    where
        F: Fn(&Index) -> Vec<Index>,
    {
        let remap = |deps: &[(Index, usize)]| -> Vec<(Index, usize)> {
            let mut remapped = Vec::new();
            for (src_index, dep_idx) in deps.iter() {
                for mapped_index in map_fn(src_index).into_iter() {
                    remapped.push((mapped_index, *dep_idx));
                }
            }
            remapped.sort_unstable_by(|x, y| Self::cmp_index(&x.0, &y.0).then(x.1.cmp(&y.1)));
            remapped.dedup();
            remapped
        };

        (remap(state_deps), remap(input_deps))
    }

    /// Convenience wrapper to remap this layout's dependencies onto a destination layout.
    fn remap_self_dependencies<F>(&self, map_fn: F) -> (Vec<NonZero>, Vec<NonZero>)
    where
        F: Fn(&Index) -> Vec<Index>,
    {
        Self::remap_dependencies(&self.state_deps, &self.input_deps, map_fn)
    }

    /// Pad or truncate an index to the required length, filling missing entries with zeros.
    fn fit_index_len(index: &Index, len: usize) -> Index {
        if index.len() == len {
            return index.clone();
        }
        let mut new_index = Index::zeros(len);
        for i in 0..std::cmp::min(index.len(), len) {
            new_index[i] = index[i];
        }
        new_index
    }

    /// Add state or input dependencies to this layout based on the tensor type.
    pub fn add_tensor_dependencies(&mut self, tensor_type: TensorType, start: i64, env: &mut Env) {
        let indices = match tensor_type {
            TensorType::State | TensorType::StateDot | TensorType::Input => {
                let mut deps = Vec::new();
                let n_states = *self.shape().get(0).unwrap_or(&1) as i64;
                for i in 0_i64..n_states {
                    let index = Index::from(vec![i]);
                    deps.push((index, (i + start) as usize));
                }
                deps
            }
            TensorType::Other => Vec::new(),
        };
        match tensor_type {
            TensorType::State => {
                assert!(
                    self.state_deps.is_empty(),
                    "state tensor layout should not already have state dependencies",
                );
                self.state_deps = indices;
                // store the state0 input dependencies in the env since we don't want to propagate them further
                env.state0_input_deps = mem::take(&mut self.input_deps);
            }
            TensorType::StateDot => {
                assert!(
                    self.state_deps.is_empty(),
                    "state dot tensor layout should not already have state dependencies",
                );
                self.state_deps = indices;
                // store the dstate0 input dependencies in the env since we don't want to propagate them further
                env.dstate0_input_deps = mem::take(&mut self.input_deps);
            }
            TensorType::Input => {
                assert! {
                    self.input_deps.is_empty(),
                    "input tensor layout should not already have input dependencies",
                };
                self.input_deps = indices;
            }
            TensorType::Other => {}
        }
    }

    // row major order
    pub fn unravel_index(index: usize, shape: &Shape) -> Index {
        let mut idx = index;
        let mut res = Index::zeros(shape.len());
        for i in (0..shape.len()).rev() {
            res[i] = i64::try_from(idx % shape[i]).unwrap();
            idx /= shape[i];
        }
        res
    }

    // row major order
    pub fn ravel_index(index: &Index, shape: &Shape) -> usize {
        let mut res = 0;
        let mut stride = 1;
        for i in (0..shape.len()).rev() {
            res += usize::try_from(index[i]).unwrap() * stride;
            stride *= shape[i];
        }
        res
    }

    // row major order
    pub fn cmp_index(a: &Index, b: &Index) -> std::cmp::Ordering {
        for i in 0..a.len() {
            match a[i].cmp(&b[i]) {
                std::cmp::Ordering::Less => return std::cmp::Ordering::Less,
                std::cmp::Ordering::Greater => return std::cmp::Ordering::Greater,
                _ => {}
            }
        }
        std::cmp::Ordering::Equal
    }

    // contract_last_axis contracts the last axis of the layout, returning a new layout with the last axis contracted.
    pub fn contract_last_axis(&self) -> Result<Layout> {
        let rank = self.rank();
        if rank == 0 {
            return Err(anyhow!("cannot contract last axis of a scalar"));
        }
        let new_shape = self.shape.slice(s![0..rank - 1]).to_owned();
        let (new_indices, new_kind, new_n_dense_axes) = if self.is_dense() {
            (Vec::new(), LayoutKind::Dense, new_shape.len())
        } else if self.is_diagonal() {
            if self.n_dense_axes == 0 && rank > 2 {
                (Vec::new(), LayoutKind::Diagonal, self.n_dense_axes)
            } else if self.n_dense_axes == 0 && rank <= 2 {
                (Vec::new(), LayoutKind::Dense, new_shape.len())
            } else {
                (Vec::new(), LayoutKind::Diagonal, self.n_dense_axes - 1)
            }
        } else if self.n_dense_axes == 0 {
            let mut new_indices = self.indices.clone();
            (0..self.indices.len()).for_each(|i| {
                new_indices[i] = new_indices[i].slice(s![0..rank - 1]).to_owned();
            });

            // remove any duplicate indices
            new_indices.sort_by(Self::cmp_index);
            new_indices.dedup();

            // check if now dense
            let new_nnz = new_shape.iter().product();
            if new_indices.len() == new_nnz {
                (new_indices, LayoutKind::Dense, self.n_dense_axes)
            } else {
                (new_indices, LayoutKind::Sparse, self.n_dense_axes)
            }
        } else {
            // there are dense axes, so just remove it
            (Vec::new(), LayoutKind::Sparse, self.n_dense_axes - 1)
        };

        let mut new_layout = Layout {
            indices: new_indices,
            state_deps: Vec::new(),
            input_deps: Vec::new(),
            shape: new_shape,
            kind: new_kind,
            n_dense_axes: new_n_dense_axes,
        };

        let (state_deps, input_deps) = self.remap_self_dependencies(|idx| {
            // If the stored index has one more dimension than the new layout rank, drop the last axis.
            let mut truncated = if idx.len() > new_layout.rank() {
                idx.slice(s![0..idx.len() - 1]).to_owned()
            } else {
                idx.clone()
            };
            truncated = Self::fit_index_len(&truncated, new_layout.rank());
            vec![truncated]
        });
        new_layout.state_deps = state_deps;
        new_layout.input_deps = input_deps;

        Ok(new_layout)
    }

    // permute the axes of the layout and return a new layout
    pub fn permute(&self, permutation: &[usize]) -> Result<Layout> {
        // check that we have the right number of permutation indices
        if permutation.len() > self.rank() {
            return Err(anyhow!("too many permutation indices"));
        }
        // check that permutation is a valid permutation
        if permutation.len() < self.min_rank() {
            return Err(anyhow!("not enough permutation indices"));
        }

        // if its an empty permutation then return the same layout
        if permutation.is_empty() {
            return Ok(self.clone());
        }

        let new_rank = permutation.iter().max().unwrap() + 1;

        // for a sparse tensor, can only permute the sparse axes
        if self.is_sparse() {
            #[allow(clippy::needless_range_loop)]
            for i in self.rank() - self.n_dense_axes..self.rank() {
                if permutation[i] != i {
                    return Err(anyhow!("cannot permute dense axes of a sparse layout"));
                }
            }
        }

        // permute shape
        let mut new_shape = Shape::ones(new_rank);
        for (i, &p) in permutation.iter().enumerate() {
            new_shape[p] = self.shape[i];
        }

        // permute indices
        let new_indices = self
            .indices
            .iter()
            .map(|i| {
                let mut new_i = Index::zeros(new_rank);
                for (pi, &p) in permutation.iter().enumerate() {
                    new_i[p] = i[pi];
                }
                new_i
            })
            .collect::<Vec<_>>();

        // reduce the number of dense axes according to the permutation
        let n_dense_axes = if self.is_dense() {
            new_rank
        } else {
            self.n_dense_axes
        };

        let mut new_layout = Layout {
            indices: new_indices,
            state_deps: Vec::new(),
            input_deps: Vec::new(),
            shape: new_shape,
            kind: self.kind.clone(),
            n_dense_axes,
        };

        let (state_deps, input_deps) = self.remap_self_dependencies(|idx| {
            let mut new_idx = Index::zeros(new_layout.rank());
            for (pi, &p) in permutation.iter().enumerate() {
                if pi < idx.len() {
                    new_idx[p] = idx[pi];
                }
            }
            vec![new_idx]
        });
        new_layout.state_deps = state_deps;
        new_layout.input_deps = input_deps;

        Ok(new_layout)
    }

    // create a new layout by broadcasting a list of layouts
    pub fn broadcast(layouts: Vec<Layout>, op: Option<char>) -> Result<Layout> {
        // the shapes of the layouts must be broadcastable
        let shapes = layouts.iter().map(|x| &x.shape).collect::<Vec<_>>();
        let shape = match broadcast_shapes(&shapes[..]) {
            Some(x) => x,
            None => {
                let shapes_str = shapes
                    .iter()
                    .map(|x| format!("{x}"))
                    .collect::<Vec<_>>()
                    .join(", ");
                return Err(anyhow!("cannot broadcast shapes: {}", shapes_str));
            }
        };

        let is_call = op.is_none();
        let is_divide = if let Some(op) = op { op == '/' } else { false };
        let is_multiply = if let Some(op) = op { op == '*' } else { false };
        let is_multiply_or_divide = is_multiply || is_divide;

        let mut broadcasted_layouts: Vec<Layout> = layouts
            .iter()
            .map(|l| l.broadcast_to_shape(&shape))
            .collect();

        let mut ret = broadcasted_layouts.pop().unwrap();
        if is_call {
            ret.to_dense();
        }
        let mut first = true;
        for layout in broadcasted_layouts.drain(..).rev() {
            // if a / b, with b is sparse and a being a dense or different sparse layout, then we have a divide by zero issue
            if first
                && is_divide
                && ret.is_sparse()
                && (layout.is_dense() || !ret.eq_nonzeros(&layout))
            {
                return Err(anyhow!("divide-by-zero detected, cannot only divide by a sparse layout if the numerator has the same sparsity pattern"));
            }
            if is_multiply_or_divide {
                ret = Self::intersect(ret, layout);
            } else if !is_call {
                ret = Self::union(ret, layout);
            } else {
                ret = Self::union_dense(ret, layout);
            }
            first = false;
        }

        // if now dense then convert to dense layout
        if ret.is_sparse_yet_dense() {
            let new_layout = Layout {
                indices: Vec::new(),
                state_deps: ret.state_deps,
                input_deps: ret.input_deps,
                n_dense_axes: ret.shape.len(),
                shape,
                kind: LayoutKind::Dense,
            };
            return Ok(new_layout);
        }

        // if now diagonal then convert to diagonal layout
        if ret.is_sparse_yet_diagonal() {
            let new_layout = Layout {
                indices: Vec::new(),
                state_deps: ret.state_deps,
                input_deps: ret.input_deps,
                n_dense_axes: ret.n_dense_axes,
                shape,
                kind: LayoutKind::Diagonal,
            };
            return Ok(new_layout);
        }
        Ok(ret)
    }
    pub fn is_dense(&self) -> bool {
        self.kind == LayoutKind::Dense
    }
    pub fn is_sparse(&self) -> bool {
        self.kind == LayoutKind::Sparse
    }
    pub fn is_diagonal(&self) -> bool {
        self.kind == LayoutKind::Diagonal
    }
    pub fn is_sparse_yet_dense(&self) -> bool {
        self.is_sparse() && self.indices.len() == self.shape.product()
    }

    pub fn is_sparse_yet_diagonal(&self) -> bool {
        if !self.is_sparse() {
            return false;
        }
        let is_vector = self.rank() == 1;
        let all_dims_equal = self.shape.iter().all(|&x| x == self.shape[0]);
        let num_indices_equal_to_dim = self.indices.len() == self.shape[0];
        let indices_not_equal = self
            .indices
            .iter()
            .any(|index| index.iter().any(|&x| x != index[0]));
        !is_vector && !indices_not_equal && all_dims_equal && num_indices_equal_to_dim
    }
    pub fn is_scalar(&self) -> bool {
        self.rank() == 0
    }

    pub fn to_dense(&mut self) {
        if self.is_dense() {
            return;
        }
        self.indices = Vec::new();
        self.kind = LayoutKind::Dense;
        self.n_dense_axes = self.shape.len();
    }

    pub fn to_sparse(&mut self) {
        if self.is_sparse() {
            return;
        }
        let mut new_indices = Vec::new();
        match self.kind {
            LayoutKind::Dense => {
                for i in 0..self.shape.product() {
                    new_indices.push(Self::unravel_index(i, &self.shape));
                }
            }
            LayoutKind::Diagonal => {
                for i in 0..self.shape[0] {
                    let index = Index::zeros(self.rank()) + i64::try_from(i).unwrap();
                    new_indices.push(index);
                }
            }
            LayoutKind::Sparse => {
                unreachable!("already sparse");
            }
        }
        self.indices = new_indices;
        self.kind = LayoutKind::Sparse;
    }

    pub fn new_empty(rank: usize) -> Self {
        Layout {
            indices: vec![],
            state_deps: vec![],
            input_deps: vec![],
            shape: Shape::zeros(rank),
            kind: LayoutKind::Dense,
            n_dense_axes: rank,
        }
    }

    pub fn new_scalar() -> Self {
        Layout {
            indices: vec![],
            state_deps: vec![],
            input_deps: vec![],
            shape: Shape::zeros(0),
            kind: LayoutKind::Dense,
            n_dense_axes: 0,
        }
    }

    pub fn new_dense(shape: Shape) -> Self {
        let n_dense_axes = shape.len();
        Layout {
            indices: vec![],
            state_deps: vec![],
            input_deps: vec![],
            shape,
            kind: LayoutKind::Dense,
            n_dense_axes,
        }
    }

    pub fn new_diagonal(shape: Shape) -> Self {
        Layout {
            indices: vec![],
            state_deps: vec![],
            input_deps: vec![],
            shape,
            kind: LayoutKind::Diagonal,
            n_dense_axes: 0,
        }
    }

    pub fn new_diagonal_from(shape: Shape, layout: &Layout) -> Option<Self> {
        // should be scalar or vector layout, shape has only one non-one dimension
        if layout.is_scalar() {
            let mut state_deps = layout
                .state_deps
                .iter()
                .map(|(_idx, j)| (Index::zeros(shape.len()), *j))
                .collect::<Vec<_>>();
            let mut input_deps = layout
                .input_deps
                .iter()
                .map(|(_idx, j)| (Index::zeros(shape.len()), *j))
                .collect::<Vec<_>>();
            for i in 1..shape.len() {
                state_deps.extend(
                    layout
                        .state_deps
                        .iter()
                        .map(|(_idx, j)| (Index::zeros(shape.len()) + i as i64, *j)),
                );
                input_deps.extend(
                    layout
                        .input_deps
                        .iter()
                        .map(|(_idx, j)| (Index::zeros(shape.len()) + i as i64, *j)),
                );
            }
            return Some(Layout {
                indices: vec![],
                state_deps,
                input_deps,
                shape,
                kind: LayoutKind::Diagonal,
                n_dense_axes: 0,
            });
        }
        if layout.shape.iter().filter(|&&x| x > 1).count() > 1 {
            return None;
        }
        let axis = layout.shape.iter().position(|&x| x > 1).unwrap_or(0);

        let state_deps = layout
            .state_deps
            .iter()
            .map(|(idx, j)| (Index::zeros(shape.len()) + idx[axis], *j))
            .collect::<Vec<_>>();
        let input_deps = layout
            .input_deps
            .iter()
            .map(|(idx, j)| (Index::zeros(shape.len()) + idx[axis], *j))
            .collect::<Vec<_>>();
        Some(Layout {
            indices: vec![],
            state_deps,
            input_deps,
            shape,
            kind: LayoutKind::Diagonal,
            n_dense_axes: 0,
        })
    }

    /// filters the dependencies of layout, which is assumed to be a dense vector layout, to only include those in the range [start, end)
    /// subtracting start from the indices and assigning the filtered dependencies to self
    pub fn filter_deps_from(&mut self, mut layout: Layout, start: i64, end: i64) {
        assert!(layout.is_dense());
        assert!(layout.shape.iter().filter(|&&x| x > 1).count() <= 1);
        let axis = layout.shape.iter().position(|&x| x > 1).unwrap_or(0);
        // assert only one possible axis
        let layout_state_deps = mem::take(&mut layout.state_deps);
        let layout_input_deps = mem::take(&mut layout.input_deps);
        let indices = layout.indices().filter(|idx| {
            let i = idx[axis];
            i >= start && i < end
        });
        self.state_deps = Self::filter_deps(self.rank(), layout_state_deps, indices)
            .into_iter()
            .map(|(mut idx, j)| {
                if idx.len() > axis {
                    idx[axis] -= start;
                }
                (idx, j)
            })
            .collect();
        let indices = layout.indices().filter(|idx| {
            let i = idx[axis];
            i >= start && i < end
        });
        self.input_deps = Self::filter_deps(self.rank(), layout_input_deps, indices)
            .into_iter()
            .map(|(mut idx, j)| {
                if idx.len() > axis {
                    idx[axis] -= start;
                }
                (idx, j)
            })
            .collect();
    }

    // concatenate a list of layouts along the first axis
    pub fn concatenate(elmts: &[TensorBlock], rank: usize) -> Result<Self> {
        let layouts = elmts
            .iter()
            .map(|x| x.layout().as_ref())
            .collect::<Vec<_>>();
        let is_zero = elmts
            .iter()
            .map(|x| matches!(x.expr().kind, AstKind::Number(0.0)))
            .collect::<Vec<_>>();
        let starts = elmts.iter().map(|x| x.start()).collect::<Vec<_>>();

        // if there are no layouts then return an empty layout
        if layouts.is_empty() {
            return Ok(Layout::new_empty(0));
        }

        // get max rank of the elmts
        let max_rank = layouts.iter().map(|x| x.rank()).max().unwrap();
        if max_rank > rank {
            return Err(anyhow!(
                "cannot concatenate layouts with rank greater than the rank of the target tensor"
            ));
        }

        // get max shape of the elmts, each dim is at least 1
        let max_shape = layouts.iter().fold(Shape::ones(rank), |mut acc, x| {
            for i in 0..x.rank() {
                acc[i] = std::cmp::max(acc[i], x.shape()[i]);
            }
            acc
        });

        // check if the layouts are contiguous on the first axis
        // check if the layouts are contiguous on the diagonal
        let mut is_contiguous_on_first_axis = true;
        let mut is_contiguous_on_diagonal = true;
        if rank > 0 {
            let mut curr_index = 0;
            for (start, layout) in std::iter::zip(starts.iter(), layouts.iter()) {
                let mut expect_contiguous_on_first_axis = Index::zeros(rank);
                expect_contiguous_on_first_axis[0] = curr_index;
                is_contiguous_on_first_axis &= start == &expect_contiguous_on_first_axis;

                let expect_contiguous_on_diagonal = Index::zeros(rank) + curr_index;
                is_contiguous_on_diagonal &= start == &expect_contiguous_on_diagonal;

                curr_index += if layout.rank() == 0 {
                    1
                } else {
                    i64::try_from(layout.shape()[0]).unwrap()
                };
            }
        }

        let mut result_layout: Layout;

        // if all layouts are dense and contiguous then calculate the new shape
        if layouts.iter().all(|x| x.is_dense()) && is_contiguous_on_first_axis {
            // check that this shape can be broadcast into max_shape
            for layout in layouts.iter() {
                for i in 1..std::cmp::min(layout.rank(), rank) {
                    if layout.shape[i] != 1 && layout.shape[i] != max_shape[i] {
                        return Err(anyhow!(
                            "cannot concatenate layouts that cannot be broadcast into each other (got shapes {:?})",
                            layouts.iter().map(|x| x.shape()).collect::<Vec<_>>()
                        ));
                    }
                }
            }
            let mut new_shape = max_shape.clone();
            if rank > 0 {
                new_shape[0] = layouts
                    .iter()
                    .map(|x| if x.rank() > 0 { x.shape[0] } else { 1 })
                    .sum();
            }
            result_layout = Layout::dense(new_shape);
        } else {
            // must be at least one diagonal or sparse layout here, get its n_dense_axes
            let mut n_dense_axes = 0;
            for layout in layouts.iter() {
                if layout.is_diagonal() || layout.is_sparse() {
                    n_dense_axes = layout.n_dense_axes;
                    break;
                }
            }

            // check that the number of final dense axes is the same for all sparse or diagonal layouts
            if layouts
                .iter()
                .any(|x| !x.is_dense() && x.n_dense_axes != n_dense_axes)
            {
                return Err(anyhow!(
                    "cannot concatenate layouts with different numbers of final dense axes"
                ));
            }

            // check that the shapes of the final dense axes are the same for all layouts
            if layouts.iter().any(|x| {
                x.shape.slice(s![x.rank() - n_dense_axes..])
                    != layouts[0].shape.slice(s![x.rank() - n_dense_axes..])
            }) {
                return Err(anyhow!(
                    "cannot concatenate layouts with different shapes for the final dense axes"
                ));
            }

            // check that the rank is the same for all layouts
            if layouts.iter().any(|x| x.rank() != layouts[0].rank()) {
                return Err(anyhow!("cannot concatenate layouts with different ranks"));
            }

            // if all layouts are diagonal (or scalar), and on the diagonal then
            if layouts
                .iter()
                .all(|x| x.is_diagonal() || x.shape().iter().all(|x| *x == 1))
                && is_contiguous_on_diagonal
            {
                // add up the shapes for the non-final dense axes
                let mut new_shape = max_shape.clone();
                for i in 0..rank {
                    new_shape[i] = layouts.iter().map(|x| x.shape[i]).sum();
                }
                result_layout = Self {
                    shape: new_shape,
                    indices: Vec::new(),
                    state_deps: Vec::new(),
                    input_deps: Vec::new(),
                    kind: LayoutKind::Diagonal,
                    n_dense_axes,
                };
            } else {
                // find the maxiumum extent of the individual layouts
                let mut max_extent = Shape::zeros(rank);
                for (start, layout) in std::iter::zip(starts.iter(), layouts.iter()) {
                    for i in 0..layout.rank() {
                        max_extent[i] = std::cmp::max(
                            max_extent[i],
                            usize::try_from(start[i]).unwrap() + layout.shape[i],
                        );
                    }
                }

                // any other combination we convert all to sparse and concatenate
                let mut new_layout = Layout {
                    indices: Vec::new(),
                    state_deps: Vec::new(),
                    input_deps: Vec::new(),
                    shape: max_extent,
                    kind: LayoutKind::Sparse,
                    n_dense_axes,
                };
                for ((layout, start), is_zero) in
                    zip(zip(layouts.iter(), starts.iter()), is_zero.iter())
                {
                    if *is_zero {
                        continue;
                    }

                    // convert to sparse
                    new_layout.indices.extend(layout.indices().map(|mut x| {
                        for (i, xi) in x.iter_mut().enumerate() {
                            *xi += start[i];
                        }
                        x
                    }));
                }

                // sort the indices in row major order and remove duplicates
                new_layout.indices.sort_by(Self::cmp_index);
                new_layout.indices.dedup();

                // check if now dense
                if new_layout.indices.len() == new_layout.shape.product() {
                    new_layout.kind = LayoutKind::Dense;
                    new_layout.indices.clear();
                    new_layout.n_dense_axes = rank;
                }

                result_layout = new_layout;
            }
        }

        // collect dependencies from each source layout and offset indices by the block start
        let mut state_deps = Vec::new();
        let mut input_deps = Vec::new();
        for (layout, start) in std::iter::zip(layouts.iter(), starts.iter()) {
            let (s, i) = layout.remap_self_dependencies(|idx| {
                let mut new_idx = Layout::fit_index_len(idx, rank);
                for j in 0..std::cmp::min(new_idx.len(), start.len()) {
                    new_idx[j] += start[j];
                }
                vec![new_idx]
            });
            state_deps = Layout::merge_deps(state_deps, s);
            input_deps = Layout::merge_deps(input_deps, i);
        }
        result_layout.state_deps = state_deps;
        result_layout.input_deps = input_deps;
        Ok(result_layout)
    }

    pub fn rank(&self) -> usize {
        self.shape.len()
    }

    pub fn min_rank(&self) -> usize {
        for i in (0..self.rank()).rev() {
            if self.shape[i] != 1 {
                return i + 1;
            }
        }
        0
    }

    pub fn dense(shape: Shape) -> Self {
        let n_dense_axes = shape.len();
        Self {
            indices: Vec::new(),
            state_deps: Vec::new(),
            input_deps: Vec::new(),
            shape,
            kind: LayoutKind::Dense,
            n_dense_axes,
        }
    }
    pub fn diagonal(shape: Shape) -> Self {
        Self {
            indices: Vec::new(),
            state_deps: Vec::new(),
            input_deps: Vec::new(),
            shape,
            kind: LayoutKind::Diagonal,
            n_dense_axes: 0,
        }
    }
    pub fn sparse(indices: Vec<Index>, shape: Shape) -> Self {
        Self {
            indices,
            state_deps: Vec::new(),
            input_deps: Vec::new(),
            shape,
            kind: LayoutKind::Sparse,
            n_dense_axes: 0,
        }
    }

    /// returns the number of stored non-zero elements in the layout
    /// this does not include the implicit dense axes for sparse and diagonal layouts
    pub fn nnz(&self) -> usize {
        if self.is_dense() {
            self.shape.iter().product()
        } else if self.is_diagonal() {
            if self.shape.is_empty() {
                0
            } else {
                self.shape[0]
            }
        } else {
            self.indices.len()
        }
    }

    pub fn state_dependencies(&self) -> &Vec<(Index, usize)> {
        &self.state_deps
    }

    pub fn input_dependencies(&self) -> &Vec<(Index, usize)> {
        &self.input_deps
    }

    pub fn take_input_dependencies(&mut self) -> Vec<(Index, usize)> {
        std::mem::take(&mut self.input_deps)
    }

    /// return the non-zero indices of the layout as an iterator, corresponding to the order of the data entries
    pub fn indices(&self) -> impl Iterator<Item = Index> + '_ {
        match self.kind {
            LayoutKind::Dense => {
                let f = Box::new(move |i| Self::unravel_index(i, &self.shape));
                (0..self.shape.product()).map(f as Box<dyn Fn(usize) -> Index>)
            }
            LayoutKind::Diagonal => {
                let f = Box::new(move |i| Index::zeros(self.rank()) + i64::try_from(i).unwrap());
                (0..self.shape[0]).map(f as Box<dyn Fn(usize) -> Index>)
            }
            LayoutKind::Sparse => {
                let f = Box::new(move |i| {
                    let index: &Index = self.indices.get(i).unwrap();
                    index.clone()
                });
                (0..self.nnz()).map(f as Box<dyn Fn(usize) -> Index>)
            }
        }
    }

    /// return the explicit slice of indices for sparse layouts
    pub fn explicit_indices(&self) -> &[Index] {
        &self.indices
    }

    /// data entry for sparse block expression layouts
    pub fn to_data_layout(&self) -> Vec<i32> {
        let mut data_layout = vec![];
        if self.is_sparse() {
            for index in self.indices() {
                data_layout.extend(index.iter().map(|&x| x as i32));
            }
        }
        data_layout
    }

    // data entry when this layout is used within an expression with another layout
    // if the other layout is the same as self and permutation is [0,1,..n-1,n], then return an empty vec
    // if this layout is dense, then return an empty vec
    //
    // returns a vec with the same size as the number of nnz in other,
    // with each entry giving the index in self corresponding to that entry in other.
    // If an index in other does not exist in self, then a -1 is returned for that entry.
    // A permutation is also provided, giving the self index for each other index.
    pub fn to_binary_data_layout(&self, other: &Layout, permutation: &[usize]) -> Vec<i32> {
        if self.eq_nonzeros(other) && permutation.iter().enumerate().all(|(i, &p)| i == p) {
            return vec![];
        }
        if self.is_dense() {
            return vec![];
        }
        assert!(
            permutation.len() <= other.rank() + 1,
            "can have max one contracted axis"
        );
        let mut data_layout = vec![];
        let permute_index = |index: &Index| {
            let mut new_index = Index::zeros(self.rank());
            for (i, &p) in permutation.iter().enumerate() {
                if p < self.rank() {
                    new_index[p] = index[i];
                }
            }
            new_index
        };
        for index in other.indices() {
            let find_index = permute_index(&index);
            let nnz_index = self
                .find_nnz_index(&find_index)
                .map(|i| i as i32)
                .unwrap_or(-1);
            data_layout.push(nnz_index);
        }
        data_layout
    }

    /// broadcast this layout to the given rank, preserving the current shape
    /// if the rank is decreased, then we can only remove axes of size 1 from the end of the shape
    pub fn broadcast_to_rank(&self, rank: usize) -> Self {
        // new shape is the old shape with ones appended to the end
        let mut shape = Shape::ones(rank);
        for i in 0..min(self.rank(), rank) {
            shape[i] = self.shape[i];
        }
        // check the rest of the shape is ones
        for i in rank..self.rank() {
            assert_eq!(self.shape[i], 1);
        }
        self.broadcast_to_shape(&shape)
    }

    /// broadcast this layout to the given shape
    /// if we are increasing the rank, then we add dense axes
    /// if we are decreasing the rank, then we can only remove dense axes
    /// if any axis is being broadcasted, then it must be size 1 in the original layout
    pub fn broadcast_to_shape(&self, shape: &Shape) -> Self {
        for i in 0..min(self.rank(), shape.len()) {
            if self.shape[i] != shape[i] && self.shape[i] != 1 {
                panic!(
                    "cannot broadcast axis {} in layout shape {} to shape {}",
                    i, self.shape, shape
                );
            }
        }
        let mut indices = self.indices.clone();
        let broadcast_axes: Vec<usize> = (0..shape.len())
            .filter(|&i| i >= self.shape.len() || (self.shape[i] == 1 && self.shape[i] != shape[i]))
            .collect();
        let broadcast_sparse_axes = broadcast_axes
            .iter()
            .filter(|&&i| i < self.rank().saturating_sub(self.n_dense_axes))
            .cloned()
            .collect::<Vec<usize>>();

        // if sparse, we need to adjust the indices due to broadcasting
        if self.is_sparse() {
            for &axis in broadcast_sparse_axes.iter() {
                let mut new_broadcast_indices = Vec::new();
                for index in indices.iter() {
                    for j in 0..shape[axis] {
                        let mut new_bi = index.clone();
                        if axis < new_bi.len() {
                            new_bi[axis] = i64::try_from(j).unwrap();
                        }
                        new_broadcast_indices.push(new_bi);
                    }
                }
                indices = new_broadcast_indices;
            }
        }

        // sort the indices in standard ordering
        indices.sort_by(Self::cmp_index);

        // any layout needs to adjust its dependencies due to broadcasting
        let new_rank = shape.len();
        let (state_deps, input_deps) = self.remap_self_dependencies(|idx| {
            let mut base_idx = Index::zeros(new_rank);
            for (i, v) in idx.iter().enumerate() {
                if i < new_rank {
                    base_idx[i] = *v;
                }
            }
            let mut new_broadcast_indices = vec![base_idx.clone()];
            for &axis in broadcast_axes.iter() {
                for j in 0..shape[axis] {
                    let mut new_bi = base_idx.clone();
                    new_bi[axis] = i64::try_from(j).unwrap();
                    new_broadcast_indices.push(new_bi);
                }
            }
            new_broadcast_indices
        });

        if self.rank() == shape.len() {
            Self {
                indices,
                state_deps,
                input_deps,
                shape: shape.clone(),
                kind: self.kind.clone(),
                n_dense_axes: self.n_dense_axes,
            }
        } else if self.rank() < shape.len() {
            let new_ranks = shape.len() - self.rank();
            let n_dense_axes = self.n_dense_axes + new_ranks;
            Self {
                indices,
                state_deps,
                input_deps,
                shape: shape.clone(),
                kind: self.kind.clone(),
                n_dense_axes,
            }
        } else if (shape.len() < self.rank()) && (self.rank() - shape.len() <= self.n_dense_axes) {
            // must be reducing the rank by a number of dense axes
            let n_dense_axes = self.n_dense_axes - (self.rank() - shape.len());
            Self {
                indices,
                state_deps,
                input_deps,
                shape: shape.clone(),
                kind: self.kind.clone(),
                n_dense_axes,
            }
        } else {
            // invalid
            panic!(
                "cannot broadcast layout shape {} to shape {}. Requires removing more than {} dense axes.",
                self.shape,
                shape,
                self.n_dense_axes
            )
        }
    }

    // returns the index in the nnz array corresponding to the given index
    // this is broadcast aware, if the index is out of bounds for the layout shape and that axis is size 1, then the index is treated as 0
    // if the index has higher rank
    pub fn find_nnz_index(&self, index: &Index) -> Option<usize> {
        let bcast_index = {
            let mut new_index = index.clone();
            for i in 0..min(self.rank(), index.len()) {
                if self.shape[i] == 1 && index[i] > 0 {
                    new_index[i] = 0;
                } else if index[i] >= self.shape[i].try_into().unwrap() {
                    return None;
                }
            }
            new_index
        };
        // take off the dense axes for sparse and diagonal layouts
        let non_dense_index = bcast_index
            .slice(s![0..self.rank() - self.n_dense_axes])
            .to_owned();
        match self.kind {
            LayoutKind::Sparse => self
                .indices
                .binary_search_by(|x| Self::cmp_index(x, &non_dense_index))
                .ok(),
            LayoutKind::Dense => Some(Self::ravel_index(&bcast_index, self.shape())),
            LayoutKind::Diagonal => {
                if index.iter().all(|&x| x == non_dense_index[0])
                    && index[0] < self.shape[0].try_into().unwrap()
                {
                    Some(index[0].try_into().unwrap())
                } else {
                    None
                }
            }
        }
    }

    pub fn shape(&self) -> &Shape {
        &self.shape
    }

    pub fn kind(&self) -> &LayoutKind {
        &self.kind
    }

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

    /// only usable for sparse or dense layouts, adjusts n_dense_axes by expanding the indices
    /// can only reduce n_dense_axes
    fn remove_dense_axes(&mut self, new_n_dense_axes: usize) {
        assert!(
            self.is_sparse() || self.is_dense(),
            "can only remove dense axes from sparse or dense layouts"
        );
        assert!(new_n_dense_axes <= self.n_dense_axes);
        if self.n_dense_axes == new_n_dense_axes {
            return;
        }
        if self.is_dense() {
            self.n_dense_axes = new_n_dense_axes;
            return;
        }
        let rank = self.rank();
        let removed_axes = self.n_dense_axes - new_n_dense_axes;
        let dense_shape = self.shape.slice(s![rank - removed_axes..]).to_owned();
        let n_dense: usize = dense_shape.iter().product();
        let mut new_indices = Vec::new();
        for index in self.indices.iter() {
            for i in 0..n_dense {
                let mut new_index = Index::zeros(rank);
                for j in 0..std::cmp::min(index.len(), rank - removed_axes) {
                    new_index[j] = index[j];
                }
                let dense_index = Self::unravel_index(i, &dense_shape);
                for j in 0..removed_axes {
                    new_index[rank - removed_axes + j] = dense_index[j];
                }
                new_indices.push(new_index);
            }
        }

        let mut new_layout = Layout {
            indices: new_indices,
            state_deps: Vec::new(),
            input_deps: Vec::new(),
            shape: self.shape.clone(),
            kind: self.kind.clone(),
            n_dense_axes: new_n_dense_axes,
        };
        let (state_deps, input_deps) = self.remap_self_dependencies(|idx| {
            let mut mapped = Vec::new();
            for i in 0..n_dense {
                let dense_index = Layout::unravel_index(i, &dense_shape);
                let mut new_index = Index::zeros(rank);
                for j in 0..std::cmp::min(idx.len(), rank - removed_axes) {
                    new_index[j] = idx[j];
                }
                for j in 0..removed_axes {
                    new_index[rank - removed_axes + j] = dense_index[j];
                }
                mapped.push(new_index);
            }
            mapped
        });
        new_layout.state_deps = state_deps;
        new_layout.input_deps = input_deps;
        *self = new_layout;
    }

    /// the result is the dense union of the two layouts (i.e. from a call where f(0,0) != 0)
    /// result is always dense
    pub fn union_dense(self, other: Layout) -> Self {
        // union deps
        let state_deps = Layout::merge_deps(self.state_deps, other.state_deps);
        let input_deps = Layout::merge_deps(self.input_deps, other.input_deps);
        // result is always dense
        Layout {
            indices: Vec::new(),
            state_deps,
            input_deps,
            shape: self.shape.clone(),
            kind: LayoutKind::Dense,
            n_dense_axes: self.shape.len(),
        }
    }

    /// the result is the union of the two layouts (i.e. from addition/subtraction or any function where f(0,0) = 0
    ///
    /// 1. union deps
    /// 2. if either layout is dense, return dense layout
    /// 3. if both layouts are diagonal with same number of dense axes, return diagonal layout
    /// 4. no dense layouts, convert any diagonal layout to sparse
    /// 5. union indices, return the resulting sparse layout
    pub fn union(mut self, mut other: Layout) -> Self {
        assert!(
            self.shape == other.shape,
            "can only union layouts with the same shape"
        );

        // 1. union deps
        let state_deps = Layout::merge_deps(
            mem::take(&mut self.state_deps),
            mem::take(&mut other.state_deps),
        );
        let input_deps = Layout::merge_deps(
            mem::take(&mut self.input_deps),
            mem::take(&mut other.input_deps),
        );

        // if either layout is dense, return dense layout
        if self.is_dense() {
            self.state_deps = state_deps;
            self.input_deps = input_deps;
            return self;
        }

        if other.is_dense() {
            other.state_deps = state_deps;
            other.input_deps = input_deps;
            return other;
        }

        // if both layouts are diagonal with same number of dense axes, return diagonal layout
        if self.is_diagonal() && other.is_diagonal() && self.n_dense_axes == other.n_dense_axes {
            self.state_deps = state_deps;
            self.input_deps = input_deps;
            return self;
        }

        // convert any diagonal layout to sparse
        if self.is_diagonal() {
            self.to_sparse();
        }
        if other.is_diagonal() {
            other.to_sparse();
        }

        // union indices, return the resulting sparse layout
        if self.n_dense_axes > other.n_dense_axes {
            self.remove_dense_axes(other.n_dense_axes);
        } else if other.n_dense_axes > self.n_dense_axes {
            other.remove_dense_axes(self.n_dense_axes);
        }
        self.indices.extend(other.indices.iter().cloned());
        self.indices.sort_by(Self::cmp_index);
        self.indices.dedup();
        self.state_deps = state_deps;
        self.input_deps = input_deps;
        self
    }

    /// the result is the intersection of the two layouts (i.e. from multiplication)
    ///
    /// 1. start from sparsest layout (sparse, or self if both sparse)
    /// 2. intersect deps
    /// 3. if the remaining layout is dense, we're done
    /// 4. if both layouts are diagonal, we're done
    /// 5. if remaining layout is diagonal, convert it to sparse
    /// 6. must have two sparse layouts now, so intersect indices
    pub fn intersect(self, other: Layout) -> Self {
        assert!(
            self.shape == other.shape,
            "can only intersect layouts with the same shape"
        );

        // 1. start from sparsest layout (sparse, or self if both sparse)
        let mut sparse_layout: Layout;
        let mut other_layout: Layout;
        if self.is_sparse() {
            sparse_layout = self;
            other_layout = other;
        } else if other.is_sparse() {
            sparse_layout = other;
            other_layout = self;
        } else if self.is_diagonal() {
            sparse_layout = self;
            other_layout = other;
        } else if other.is_diagonal() {
            sparse_layout = other;
            other_layout = self;
        } else {
            sparse_layout = self;
            other_layout = other;
        }

        // 3. if the remaining layout is dense, we're done, return the sparse layout
        if other_layout.is_dense() {
            let state_deps = Layout::merge_deps(
                mem::take(&mut sparse_layout.state_deps),
                mem::take(&mut other_layout.state_deps),
            );
            let input_deps = Layout::merge_deps(
                mem::take(&mut sparse_layout.input_deps),
                mem::take(&mut other_layout.input_deps),
            );
            let state_deps =
                Layout::filter_deps(sparse_layout.rank(), state_deps, sparse_layout.indices());
            let input_deps =
                Layout::filter_deps(sparse_layout.rank(), input_deps, sparse_layout.indices());
            return Layout {
                indices: sparse_layout.indices.clone(),
                state_deps,
                input_deps,
                shape: sparse_layout.shape.clone(),
                kind: sparse_layout.kind.clone(),
                n_dense_axes: sparse_layout.n_dense_axes,
            };
        }

        // 4. if both layouts are diagonal with same number of dense axes, we're done
        if sparse_layout.is_diagonal()
            && other_layout.is_diagonal()
            && sparse_layout.n_dense_axes == other_layout.n_dense_axes
        {
            let state_deps = Layout::merge_deps(
                mem::take(&mut sparse_layout.state_deps),
                mem::take(&mut other_layout.state_deps),
            );
            let input_deps = Layout::merge_deps(
                mem::take(&mut sparse_layout.input_deps),
                mem::take(&mut other_layout.input_deps),
            );
            let n_dense_axes = sparse_layout.n_dense_axes;
            let ret = Layout {
                indices: Vec::new(),
                state_deps,
                input_deps,
                shape: sparse_layout.shape.clone(),
                kind: LayoutKind::Diagonal,
                n_dense_axes,
            };
            return ret;
        }

        // convert any diagonal layout to sparse
        if other_layout.is_diagonal() {
            other_layout.to_sparse();
        }
        if sparse_layout.is_diagonal() {
            sparse_layout.to_sparse();
        }

        // we must have two sparse layouts now, so intersect indices
        if sparse_layout.n_dense_axes > other_layout.n_dense_axes {
            sparse_layout.remove_dense_axes(other_layout.n_dense_axes);
        } else if other_layout.n_dense_axes > sparse_layout.n_dense_axes {
            other_layout.remove_dense_axes(sparse_layout.n_dense_axes);
        }
        let mut new_indices = Vec::new();
        let mut i = 0;
        let mut j = 0;
        while i < sparse_layout.indices.len() && j < other_layout.indices.len() {
            match Self::cmp_index(&sparse_layout.indices[i], &other_layout.indices[j]) {
                std::cmp::Ordering::Less => {
                    i += 1;
                }
                std::cmp::Ordering::Greater => {
                    j += 1;
                }
                std::cmp::Ordering::Equal => {
                    new_indices.push(sparse_layout.indices[i].clone());
                    i += 1;
                    j += 1;
                }
            }
        }

        let mut ret = Layout {
            indices: new_indices,
            state_deps: Vec::new(),
            input_deps: Vec::new(),
            shape: sparse_layout.shape.clone(),
            kind: LayoutKind::Sparse,
            n_dense_axes: sparse_layout.n_dense_axes,
        };

        let state_deps = Layout::merge_deps(
            mem::take(&mut sparse_layout.state_deps),
            mem::take(&mut other_layout.state_deps),
        );
        let input_deps = Layout::merge_deps(
            mem::take(&mut sparse_layout.input_deps),
            mem::take(&mut other_layout.input_deps),
        );
        ret.state_deps = Layout::filter_deps(sparse_layout.rank(), state_deps, ret.indices());
        ret.input_deps = Layout::filter_deps(sparse_layout.rank(), input_deps, ret.indices());

        if ret.indices.len() == ret.shape.product() {
            ret.kind = LayoutKind::Dense;
            ret.indices.clear();
            ret.n_dense_axes = ret.shape.len();
        }
        ret
    }
}

// ArcLayout is a wrapper for Arc<Layout> that implements Hash and PartialEq based on ptr equality
#[derive(Debug)]
pub struct ArcLayout(Arc<Layout>);
impl Hash for ArcLayout {
    fn hash<H: Hasher>(&self, state: &mut H) {
        Arc::as_ptr(&self.0).hash(state);
    }
}
impl PartialEq for ArcLayout {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}
impl Clone for ArcLayout {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}
impl Eq for ArcLayout {}
impl Deref for ArcLayout {
    type Target = Layout;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl AsRef<Layout> for ArcLayout {
    fn as_ref(&self) -> &Layout {
        &self.0
    }
}
impl ArcLayout {
    pub fn new(layout: Layout) -> Self {
        Self(Arc::new(layout))
    }
}

mod tests {
    #[allow(unused_imports)]
    use super::*;

    #[test]
    fn test_broadcast_layouts() -> Result<()> {
        let layout1 = Layout::dense(Shape::from(vec![2, 3, 4]));
        let layout2 = Layout::diagonal(Shape::from(vec![2, 3, 4]));
        let layout3 = Layout::sparse(vec![Index::from(vec![0, 0, 0])], Shape::from(vec![2, 3, 4]));
        let layout4 = Layout::sparse(
            vec![Index::from(vec![0, 0, 0]), Index::from(vec![1, 2, 3])],
            Shape::from(vec![2, 3, 4]),
        );
        let broadcasted1 = Layout::broadcast(vec![layout1.clone(), layout2.clone()], Some('*'))?;
        assert!(broadcasted1.is_diagonal());
        let broadcasted2 = Layout::broadcast(vec![layout1.clone(), layout2.clone()], Some('+'))?;
        assert!(broadcasted2.is_dense());
        let broadcasted3 = Layout::broadcast(vec![layout1.clone(), layout3.clone()], Some('*'))?;
        assert!(broadcasted3.is_sparse());
        let broadcasted4 = Layout::broadcast(vec![layout1.clone(), layout3.clone()], Some('+'))?;
        assert!(broadcasted4.is_dense());
        let broadcasted5 = Layout::broadcast(vec![layout3.clone(), layout4.clone()], Some('*'))?;
        assert!(broadcasted5.is_sparse());
        assert_eq!(broadcasted5.indices.len(), 1);
        assert_eq!(broadcasted5.indices[0], Index::from(vec![0, 0, 0]));
        let broadcasted6 = Layout::broadcast(vec![layout3.clone(), layout4.clone()], Some('+'))?;
        assert!(broadcasted6.is_sparse());
        assert_eq!(broadcasted6.indices.len(), 2);
        assert_eq!(broadcasted6.indices[0], Index::from(vec![0, 0, 0]));
        assert_eq!(broadcasted6.indices[1], Index::from(vec![1, 2, 3]));
        Ok(())
    }

    #[test]
    fn test_binary_data_layout() -> Result<()> {
        let layout1 = Layout::sparse(vec![Index::from(vec![0, 0, 0])], Shape::from(vec![2, 3, 4]));
        let layout2 = Layout::sparse(
            vec![Index::from(vec![0, 0, 0]), Index::from(vec![1, 2, 3])],
            Shape::from(vec![2, 3, 4]),
        );
        let layout1_plus_layout2 =
            Layout::broadcast(vec![layout1.clone(), layout2.clone()], Some('+'))?;
        assert!(layout1_plus_layout2.is_sparse());
        assert_eq!(layout1_plus_layout2.indices.len(), 2);
        assert_eq!(layout1_plus_layout2.indices[0], Index::from(vec![0, 0, 0]));
        assert_eq!(layout1_plus_layout2.indices[1], Index::from(vec![1, 2, 3]));
        let data_layout = layout1.to_binary_data_layout(&layout1_plus_layout2, &[0, 1, 2]);
        assert_eq!(data_layout, vec![0, -1]);
        let data_layout = layout2.to_binary_data_layout(&layout1_plus_layout2, &[0, 1, 2]);
        assert_eq!(data_layout, vec![]);
        Ok(())
    }

    #[test]
    fn test_union_sparse_layouts() {
        let layout1 = Layout::sparse(
            vec![Index::from(vec![0, 0]), Index::from(vec![1, 1])],
            Shape::from(vec![2, 2]),
        );
        let layout2 = Layout::sparse(vec![Index::from(vec![1, 0])], Shape::from(vec![2, 2]));
        let layout3 = Layout::union(layout1, layout2);
        assert_eq!(layout3.indices.len(), 3);

        let mut layout1 = Layout::sparse(vec![Index::from(vec![1])], Shape::from(vec![2, 2]));
        layout1.n_dense_axes = 1;
        let layout2 = Layout::sparse(vec![Index::from(vec![1, 0])], Shape::from(vec![2, 2]));
        let layout3 = Layout::union(layout1, layout2);
        assert_eq!(layout3.indices.len(), 2);
        assert_eq!(layout3.indices[0], Index::from(vec![1, 0]));
        assert_eq!(layout3.indices[1], Index::from(vec![1, 1]));
    }

    #[test]
    fn test_intersect_sparse_layouts() {
        let layout1 = Layout::sparse(
            vec![Index::from(vec![0, 0]), Index::from(vec![1, 1])],
            Shape::from(vec![2, 2]),
        );
        let layout2 = Layout::sparse(
            vec![Index::from(vec![1, 0]), Index::from(vec![1, 1])],
            Shape::from(vec![2, 2]),
        );
        let layout3 = Layout::intersect(layout1, layout2);
        assert_eq!(layout3.indices.len(), 1);
        assert_eq!(layout3.indices[0], Index::from(vec![1, 1]));

        let mut layout1 = Layout::sparse(vec![Index::from(vec![1])], Shape::from(vec![2, 2]));
        layout1.n_dense_axes = 1;
        let layout2 = Layout::sparse(vec![Index::from(vec![1, 0])], Shape::from(vec![2, 2]));
        let layout3 = Layout::intersect(layout1, layout2);
        assert_eq!(layout3.indices.len(), 1);
        assert_eq!(layout3.indices[0], Index::from(vec![1, 0]));
    }

    #[test]
    fn test_dependencies_roundtrip_dense_sparse() {
        let mut layout = Layout::sparse(
            vec![Index::from(vec![0]), Index::from(vec![1])],
            Shape::from(vec![2]),
        );
        layout.state_deps = vec![(Index::from(vec![1]), 7)];
        layout.input_deps = vec![(Index::from(vec![0]), 3)];

        layout.to_dense();
        assert!(layout.is_dense());
        assert_eq!(layout.state_deps, vec![(Index::from(vec![1]), 7)]);
        assert_eq!(layout.input_deps, vec![(Index::from(vec![0]), 3)]);

        layout.to_sparse();
        assert!(layout.is_sparse());
        assert_eq!(layout.state_deps, vec![(Index::from(vec![1]), 7)]);
        assert_eq!(layout.input_deps, vec![(Index::from(vec![0]), 3)]);
    }

    #[test]
    fn test_broadcast_to_shape_replicates_dependencies() {
        let mut layout = Layout::sparse(vec![Index::from(vec![0, 0])], Shape::from(vec![1, 2]));
        layout.state_deps = vec![(Index::from(vec![0, 0]), 42)];

        let broadcasted = layout.broadcast_to_shape(&Shape::from(vec![3, 2]));
        assert!(broadcasted.is_sparse());
        assert_eq!(broadcasted.shape, Shape::from(vec![3, 2]));
        assert_eq!(
            broadcasted.state_deps,
            vec![
                (Index::from(vec![0, 0]), 42),
                (Index::from(vec![1, 0]), 42),
                (Index::from(vec![2, 0]), 42)
            ]
        );
        assert!(broadcasted.input_deps.is_empty());
    }

    #[test]
    fn test_permute_remaps_dependencies() -> Result<()> {
        let mut layout = Layout::sparse(
            vec![Index::from(vec![0, 1]), Index::from(vec![1, 2])],
            Shape::from(vec![2, 3]),
        );
        layout.state_deps = vec![(Index::from(vec![1, 2]), 5)];
        layout.input_deps = vec![(Index::from(vec![0, 1]), 8)];

        let permuted = layout.permute(&[1, 0])?;
        assert_eq!(permuted.shape, Shape::from(vec![3, 2]));
        assert_eq!(
            permuted.indices,
            vec![Index::from(vec![1, 0]), Index::from(vec![2, 1])]
        );
        assert_eq!(permuted.state_deps, vec![(Index::from(vec![2, 1]), 5)]);
        assert_eq!(permuted.input_deps, vec![(Index::from(vec![1, 0]), 8)]);
        Ok(())
    }

    #[test]
    fn test_union_and_intersect_dependencies() {
        let mut layout1 = Layout::sparse(vec![Index::from(vec![0, 0])], Shape::from(vec![2, 2]));
        layout1.state_deps = vec![(Index::from(vec![0, 0]), 1)];

        let mut layout2 = Layout::sparse(vec![Index::from(vec![1, 1])], Shape::from(vec![2, 2]));
        layout2.input_deps = vec![(Index::from(vec![1, 1]), 2)];

        let union_layout = Layout::union(layout1, layout2);
        assert_eq!(
            union_layout.indices,
            vec![Index::from(vec![0, 0]), Index::from(vec![1, 1])]
        );
        assert_eq!(union_layout.state_deps, vec![(Index::from(vec![0, 0]), 1)]);
        assert_eq!(union_layout.input_deps, vec![(Index::from(vec![1, 1]), 2)]);

        let mut layout3 = Layout::sparse(
            vec![Index::from(vec![0, 0]), Index::from(vec![1, 1])],
            Shape::from(vec![2, 2]),
        );
        layout3.state_deps = vec![(Index::from(vec![1, 1]), 9)];
        layout3.input_deps = vec![(Index::from(vec![0, 0]), 8)];

        let mut layout4 = Layout::sparse(vec![Index::from(vec![1, 1])], Shape::from(vec![2, 2]));
        layout4.state_deps = vec![(Index::from(vec![1, 1]), 5)];

        let layout5 = Layout::intersect(layout3, layout4);
        assert_eq!(layout5.indices, vec![Index::from(vec![1, 1])]);
        assert_eq!(
            layout5.state_deps,
            vec![(Index::from(vec![1, 1]), 5), (Index::from(vec![1, 1]), 9)]
        );
        assert_eq!(layout5.input_deps, vec![]);
    }

    #[test]
    fn test_filter_deps_from_basic() {
        // Test basic filtering with a dense 1D layout
        let mut target_layout = Layout::dense(Shape::from(vec![5]));

        let mut source_layout = Layout::dense(Shape::from(vec![10]));
        source_layout.state_deps = vec![
            (Index::from(vec![0]), 0),
            (Index::from(vec![1]), 1),
            (Index::from(vec![2]), 2),
            (Index::from(vec![3]), 3),
            (Index::from(vec![4]), 4),
            (Index::from(vec![5]), 5),
            (Index::from(vec![6]), 6),
            (Index::from(vec![7]), 7),
        ];
        source_layout.input_deps = vec![
            (Index::from(vec![1]), 10),
            (Index::from(vec![3]), 11),
            (Index::from(vec![5]), 12),
            (Index::from(vec![7]), 13),
        ];

        // Filter to include indices 2..7
        target_layout.filter_deps_from(source_layout, 2, 7);

        // Should only include dependencies in the range [2, 7)
        assert_eq!(
            target_layout.state_deps,
            vec![
                (Index::from(vec![0]), 2),
                (Index::from(vec![1]), 3),
                (Index::from(vec![2]), 4),
                (Index::from(vec![3]), 5),
                (Index::from(vec![4]), 6),
            ]
        );
        assert_eq!(
            target_layout.input_deps,
            vec![(Index::from(vec![1]), 11), (Index::from(vec![3]), 12),]
        );
    }

    #[test]
    fn test_filter_deps_from_start_of_range() {
        // Test filtering from the start of the layout
        let mut target_layout = Layout::dense(Shape::from(vec![3]));

        let mut source_layout = Layout::dense(Shape::from(vec![8]));
        source_layout.state_deps = vec![
            (Index::from(vec![0]), 0),
            (Index::from(vec![1]), 1),
            (Index::from(vec![2]), 2),
            (Index::from(vec![5]), 5),
        ];
        source_layout.input_deps = vec![(Index::from(vec![0]), 10), (Index::from(vec![2]), 12)];

        // Filter to include indices 0..3
        target_layout.filter_deps_from(source_layout, 0, 3);

        assert_eq!(
            target_layout.state_deps,
            vec![
                (Index::from(vec![0]), 0),
                (Index::from(vec![1]), 1),
                (Index::from(vec![2]), 2),
            ]
        );
        assert_eq!(
            target_layout.input_deps,
            vec![(Index::from(vec![0]), 10), (Index::from(vec![2]), 12),]
        );
    }

    #[test]
    fn test_filter_deps_from_end_of_range() {
        // Test filtering from the end of the layout
        let mut target_layout = Layout::dense(Shape::from(vec![3]));

        let mut source_layout = Layout::dense(Shape::from(vec![8]));
        source_layout.state_deps = vec![
            (Index::from(vec![2]), 2),
            (Index::from(vec![5]), 5),
            (Index::from(vec![6]), 6),
            (Index::from(vec![7]), 7),
        ];
        source_layout.input_deps = vec![(Index::from(vec![5]), 15), (Index::from(vec![7]), 17)];

        // Filter to include indices 5..8
        target_layout.filter_deps_from(source_layout, 5, 8);

        assert_eq!(
            target_layout.state_deps,
            vec![
                (Index::from(vec![0]), 5),
                (Index::from(vec![1]), 6),
                (Index::from(vec![2]), 7),
            ]
        );
        assert_eq!(
            target_layout.input_deps,
            vec![(Index::from(vec![0]), 15), (Index::from(vec![2]), 17),]
        );
    }

    #[test]
    fn test_filter_deps_from_no_match_in_range() {
        // Test filtering when no dependencies fall in the range
        let mut target_layout = Layout::dense(Shape::from(vec![3]));

        let mut source_layout = Layout::dense(Shape::from(vec![10]));
        source_layout.state_deps = vec![
            (Index::from(vec![0]), 0),
            (Index::from(vec![1]), 1),
            (Index::from(vec![8]), 8),
            (Index::from(vec![9]), 9),
        ];
        source_layout.input_deps = vec![(Index::from(vec![0]), 10), (Index::from(vec![9]), 19)];

        // Filter to include indices 3..6 (no deps in this range)
        target_layout.filter_deps_from(source_layout, 3, 6);

        assert_eq!(target_layout.state_deps, vec![]);
        assert_eq!(target_layout.input_deps, vec![]);
    }

    #[test]
    fn test_filter_deps_from_single_index_range() {
        // Test filtering with a single index range
        let mut target_layout = Layout::dense(Shape::from(vec![1]));

        let mut source_layout = Layout::dense(Shape::from(vec![5]));
        source_layout.state_deps = vec![
            (Index::from(vec![0]), 0),
            (Index::from(vec![2]), 2),
            (Index::from(vec![3]), 3),
        ];
        source_layout.input_deps = vec![(Index::from(vec![2]), 12)];

        // Filter to include only index 2
        target_layout.filter_deps_from(source_layout, 2, 3);

        assert_eq!(target_layout.state_deps, vec![(Index::from(vec![0]), 2)]);
        assert_eq!(target_layout.input_deps, vec![(Index::from(vec![0]), 12)]);
    }

    #[test]
    fn test_filter_deps_from_empty_source() {
        // Test filtering when source has no dependencies
        let mut target_layout = Layout::dense(Shape::from(vec![5]));

        let source_layout = Layout::dense(Shape::from(vec![10]));
        // source_layout has no dependencies

        target_layout.filter_deps_from(source_layout, 2, 7);

        assert_eq!(target_layout.state_deps, vec![]);
        assert_eq!(target_layout.input_deps, vec![]);
    }

    #[test]
    fn test_filter_deps_from_full_range() {
        // Test filtering with the full range of the source layout
        let mut target_layout = Layout::dense(Shape::from(vec![5]));

        let mut source_layout = Layout::dense(Shape::from(vec![5]));
        source_layout.state_deps = vec![
            (Index::from(vec![0]), 0),
            (Index::from(vec![1]), 1),
            (Index::from(vec![2]), 2),
            (Index::from(vec![3]), 3),
            (Index::from(vec![4]), 4),
        ];
        source_layout.input_deps = vec![(Index::from(vec![1]), 11), (Index::from(vec![3]), 13)];

        // Filter to include all indices 0..5
        target_layout.filter_deps_from(source_layout, 0, 5);

        assert_eq!(
            target_layout.state_deps,
            vec![
                (Index::from(vec![0]), 0),
                (Index::from(vec![1]), 1),
                (Index::from(vec![2]), 2),
                (Index::from(vec![3]), 3),
                (Index::from(vec![4]), 4),
            ]
        );
        assert_eq!(
            target_layout.input_deps,
            vec![(Index::from(vec![1]), 11), (Index::from(vec![3]), 13),]
        );
    }

    #[test]
    fn test_broadcast_sparse_2d_with_dense_1d_state_deps() -> Result<()> {
        // Create a sparse 2D layout
        let sparse_2d = Layout::sparse(
            vec![
                Index::from(vec![0, 0]),
                Index::from(vec![0, 1]),
                Index::from(vec![1, 1]),
            ],
            Shape::from(vec![2, 2]),
        );

        // Create a dense 1D layout with state dependencies
        let mut dense_1d = Layout::dense(Shape::from(vec![2]));
        dense_1d.state_deps = vec![(Index::from(vec![0]), 0), (Index::from(vec![1]), 1)];

        // Broadcast with addition operation
        let result = Layout::broadcast(vec![sparse_2d.clone(), dense_1d.clone()], Some('+'))?;

        // Result should be dense when broadcasting with a dense layout
        assert!(result.is_dense());
        assert_eq!(result.shape(), &Shape::from(vec![2, 2]));

        // Check state dependencies are propagated correctly
        // The dense 1D state_deps are broadcast along the first axis only
        assert_eq!(
            result.state_deps,
            vec![
                (Index::from(vec![0, 0]), 0),
                (Index::from(vec![0, 1]), 0),
                (Index::from(vec![1, 0]), 1),
                (Index::from(vec![1, 1]), 1),
            ]
        );
        assert_eq!(result.input_deps, vec![]);

        Ok(())
    }

    #[test]
    fn test_broadcast_sparse_2d_with_dense_1d_multiply() -> Result<()> {
        // Create a sparse 2D layout
        let sparse_2d = Layout::sparse(
            vec![Index::from(vec![0, 0]), Index::from(vec![1, 1])],
            Shape::from(vec![2, 2]),
        );

        // Create a dense 1D layout with state dependencies
        let mut dense_1d = Layout::dense(Shape::from(vec![2]));
        dense_1d.state_deps = vec![(Index::from(vec![0]), 0), (Index::from(vec![1]), 1)];

        // Broadcast with multiplication operation
        let result = Layout::broadcast(vec![sparse_2d, dense_1d], Some('*'))?;

        // When multiplying diagonal sparse with dense, result is diagonal
        assert!(result.is_diagonal());
        assert_eq!(result.shape(), &Shape::from(vec![2, 2]));

        // State dependencies only at diagonal positions (off-diagonal zeros eliminate dependencies)
        assert_eq!(
            result.state_deps,
            vec![(Index::from(vec![0, 0]), 0), (Index::from(vec![1, 1]), 1),]
        );
        assert_eq!(result.input_deps, vec![]);

        Ok(())
    }

    #[test]
    fn test_broadcast_sparse_2d_with_dense_1d_mixed_deps() -> Result<()> {
        // Create a sparse 2D layout with its own dependencies
        let mut sparse_2d = Layout::sparse(
            vec![Index::from(vec![0, 1]), Index::from(vec![1, 0])],
            Shape::from(vec![2, 2]),
        );
        sparse_2d.input_deps = vec![(Index::from(vec![0, 1]), 5), (Index::from(vec![1, 0]), 6)];

        // Create a dense 1D layout with state dependencies
        let mut dense_1d = Layout::dense(Shape::from(vec![2]));
        dense_1d.state_deps = vec![(Index::from(vec![0]), 0), (Index::from(vec![1]), 1)];

        // Broadcast with addition
        let result = Layout::broadcast(vec![sparse_2d.clone(), dense_1d.clone()], Some('+'))?;

        // Result should be dense when one operand is dense
        assert!(result.is_dense());

        // State deps from dense_1d broadcast along first axis to all columns
        assert_eq!(
            result.state_deps,
            vec![
                (Index::from(vec![0, 0]), 0),
                (Index::from(vec![0, 1]), 0),
                (Index::from(vec![1, 0]), 1),
                (Index::from(vec![1, 1]), 1),
            ]
        );

        // Input deps from sparse_2d are broadcast to matching positions
        assert_eq!(
            result.input_deps,
            vec![(Index::from(vec![0, 1]), 5), (Index::from(vec![1, 0]), 6),]
        );

        Ok(())
    }

    #[test]
    fn test_broadcast_1d_to_2d_dense_deps() {
        // Test broadcasting a 1D dense layout with dependencies to 2D
        let mut layout_1d = Layout::dense(Shape::from(vec![2]));
        layout_1d.state_deps = vec![(Index::from(vec![0]), 0), (Index::from(vec![1]), 1)];

        let layout_2d = layout_1d.broadcast_to_shape(&Shape::from(vec![2, 2]));

        assert!(layout_2d.is_dense());
        assert_eq!(layout_2d.shape(), &Shape::from(vec![2, 2]));

        // Dependencies should be replicated across the new dimension
        assert_eq!(
            layout_2d.state_deps,
            vec![
                (Index::from(vec![0, 0]), 0),
                (Index::from(vec![0, 1]), 0),
                (Index::from(vec![1, 0]), 1),
                (Index::from(vec![1, 1]), 1),
            ]
        );
    }

    #[test]
    fn test_1d_no_broadcast_preserves_deps() {
        // Test that a 1D layout used without broadcasting keeps its dependencies
        let mut layout_1d = Layout::dense(Shape::from(vec![2]));
        layout_1d.state_deps = vec![(Index::from(vec![0]), 0), (Index::from(vec![1]), 1)];

        // Using the same shape (no broadcast) should not change dependencies
        let same_layout = layout_1d.broadcast_to_shape(&Shape::from(vec![2]));

        assert!(same_layout.is_dense());
        assert_eq!(same_layout.shape(), &Shape::from(vec![2]));

        // Dependencies should remain 1D
        assert_eq!(
            same_layout.state_deps,
            vec![(Index::from(vec![0]), 0), (Index::from(vec![1]), 1),]
        );
    }
}