numrs2 0.3.3

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
//! Graph Neural Network Primitives
//!
//! This module provides state-of-the-art graph neural network architectures
//! for learning on graph-structured data.
//!
//! # Overview
//!
//! Graph Neural Networks (GNNs) are a class of deep learning models designed to process
//! graph-structured data. They operate by iteratively aggregating information from a node's
//! neighborhood to update node representations.
//!
//! # Architectures
//!
//! This module implements the following GNN architectures:
//!
//! ## Graph Convolutional Network (GCN)
//!
//! **Citation**: Kipf & Welling (2017) - "Semi-Supervised Classification with Graph Convolutional Networks"
//!
//! **Formula**: `H^(l+1) = σ(D^(-1/2) A D^(-1/2) H^(l) W^(l))`
//!
//! where:
//! - `A` is the adjacency matrix with self-loops
//! - `D` is the degree matrix
//! - `H^(l)` are the node features at layer l
//! - `W^(l)` are learnable weights
//! - `σ` is an activation function
//!
//! GCN performs symmetric normalization of the adjacency matrix for stable training.
//!
//! ## Graph Attention Network (GAT)
//!
//! **Citation**: Veličković et al. (2018) - "Graph Attention Networks"
//!
//! **Formula**: `α_ij = softmax(LeakyReLU(a^T [Wh_i || Wh_j]))`
//!
//! GAT uses multi-head attention to weigh neighbor contributions:
//! - Attention coefficients determine importance of neighbors
//! - Multiple attention heads capture different structural aspects
//! - Can handle both directed and undirected graphs
//!
//! ## GraphSAGE
//!
//! **Citation**: Hamilton et al. (2017) - "Inductive Representation Learning on Large Graphs"
//!
//! **Formula**: `h_v^(l+1) = σ(W · AGGREGATE({h_u^(l), ∀u ∈ N(v)}))`
//!
//! GraphSAGE samples and aggregates from neighborhoods:
//! - **Mean aggregator**: Average of neighbor features
//! - **Pool aggregator**: Max pooling after MLP transformation
//! - **LSTM aggregator**: LSTM over random permutation of neighbors
//! - Enables inductive learning on previously unseen nodes
//!
//! ## Message Passing Neural Network (MPNN)
//!
//! **Citation**: Gilmer et al. (2017) - "Neural Message Passing for Quantum Chemistry"
//!
//! **Formula**:
//! - Message: `m_v^(t+1) = Σ_{u∈N(v)} M_t(h_v^(t), h_u^(t), e_{uv})`
//! - Update: `h_v^(t+1) = U_t(h_v^(t), m_v^(t+1))`
//! - Readout: `ŷ = R({h_v^(T) | v ∈ G})`
//!
//! MPNN provides a general framework for message passing:
//! - Flexible message and update functions
//! - Supports edge features
//! - Used for molecular property prediction
//!
//! ## Graph Isomorphism Network (GIN)
//!
//! **Citation**: Xu et al. (2019) - "How Powerful are Graph Neural Networks?"
//!
//! **Formula**: `h_v^(l+1) = MLP((1 + ε) · h_v^(l) + Σ_{u∈N(v)} h_u^(l))`
//!
//! GIN is provably as powerful as the Weisfeiler-Lehman test:
//! - Sum aggregation (most expressive for graph isomorphism)
//! - Learnable or fixed epsilon parameter
//! - MLP for feature transformation
//!
//! # Graph Representations
//!
//! The module supports multiple graph representations:
//!
//! - **AdjacencyMatrix**: Dense representation, good for small graphs
//! - **EdgeList**: COO format, memory-efficient for sparse graphs
//! - **SparseAdjacency**: CSR format, efficient for operations
//! - **GraphBatch**: Batch multiple graphs for parallel processing
//!
//! # Aggregation Functions
//!
//! Core aggregation operations with SIMD optimization:
//! - **Mean**: Average over neighborhoods
//! - **Sum**: Summation over neighborhoods
//! - **Max**: Maximum over neighborhoods
//! - **Attention**: Attention-weighted aggregation
//! - **Set2Set**: LSTM-based set aggregation
//!
//! # Graph Pooling
//!
//! Hierarchical pooling for graph-level representations:
//! - **Global pooling**: Mean/max/sum over all nodes
//! - **DiffPool**: Differentiable soft cluster assignment
//! - **TopK**: Select top-k nodes by importance scores
//! - **SAGPool**: Self-attention graph pooling
//!
//! # SCIRS2 Integration
//!
//! All operations use SCIRS2 abstractions:
//! - `scirs2_core::ndarray` for arrays (NEVER direct ndarray)
//! - `scirs2_linalg` for linear algebra via OxiBLAS
//! - `scirs2_core::parallel_ops` for parallelization
//! - `scirs2_core::simd_ops` for vectorization
//! - Pure Rust, zero C/C++ dependencies
//!
//! # Examples
//!
//! ## Node Classification with GCN
//!
//! ```rust,ignore
//! use numrs2::new_modules::nn::graph::*;
//! use scirs2_core::ndarray::Array2;
//!
//! // Create graph (5 nodes, 6 edges)
//! let adj = AdjacencyMatrix::from_edges(5, &[(0,1), (0,2), (1,2), (2,3), (3,4), (4,2)])?;
//! let features = Array2::ones((5, 10)); // 10 features per node
//!
//! // Create GCN layer
//! let gcn = GcnLayer::new(10, 16)?; // 10 input, 16 output features
//! let output = gcn.forward(&adj, &features.view())?;
//! ```
//!
//! ## Graph Classification with GIN
//!
//! ```rust,ignore
//! use numrs2::new_modules::nn::graph::*;
//!
//! // Create molecular graph
//! let adj = SparseAdjacency::from_edges(20, &edges)?;
//! let node_features = Array2::from_shape_fn((20, 64), |(i,j)| 0.1);
//!
//! // GIN layer + global pooling
//! let gin = GinLayer::new(64, 64, 0.0)?;
//! let node_out = gin.forward(&adj, &node_features.view())?;
//! let graph_repr = global_mean_pool(&node_out.view())?;
//! ```
//!
//! # Performance
//!
//! - SIMD-optimized aggregations (4-8x speedup)
//! - Sparse matrix operations for large graphs
//! - Parallel batch processing
//! - Memory-efficient CSR representation
//!
//! # References
//!
//! - Kipf & Welling (2017): Semi-Supervised Classification with Graph Convolutional Networks (ICLR)
//! - Veličković et al. (2018): Graph Attention Networks (ICLR)
//! - Hamilton et al. (2017): Inductive Representation Learning on Large Graphs (NeurIPS)
//! - Gilmer et al. (2017): Neural Message Passing for Quantum Chemistry (ICML)
//! - Xu et al. (2019): How Powerful are Graph Neural Networks? (ICLR)
//! - Ying et al. (2018): Hierarchical Graph Representation Learning with Differentiable Pooling (NeurIPS)
//! - Gao & Ji (2019): Graph U-Nets (ICML)
//! - Lee et al. (2019): Self-Attention Graph Pooling (ICML)

use crate::error::NumRs2Error;
use scirs2_core::ndarray::{s, Array, Array1, Array2, ArrayView1, ArrayView2, Axis};
use scirs2_core::numeric::Float;
use scirs2_core::simd_ops::SimdUnifiedOps;
use std::collections::HashMap;

/// Result type for graph neural network operations
pub type GraphResult<T> = Result<T, NumRs2Error>;

// ================================================================================================
// Graph Representations
// ================================================================================================

/// Dense adjacency matrix representation
///
/// Suitable for small, dense graphs. Memory usage: O(n²)
///
/// # Fields
///
/// - `num_nodes`: Number of nodes in the graph
/// - `adj`: Dense adjacency matrix (num_nodes × num_nodes)
///
/// # Example
///
/// ```rust,ignore
/// let adj = AdjacencyMatrix::from_edges(5, &[(0,1), (1,2), (2,3)])?;
/// ```
#[derive(Debug, Clone)]
pub struct AdjacencyMatrix<T: Float> {
    pub num_nodes: usize,
    pub adj: Array2<T>,
}

impl<T: Float> AdjacencyMatrix<T> {
    /// Create adjacency matrix from edge list
    ///
    /// # Arguments
    ///
    /// * `num_nodes` - Number of nodes
    /// * `edges` - List of edges as (source, target) pairs
    ///
    /// # Returns
    ///
    /// Adjacency matrix with 1.0 for edges, 0.0 otherwise
    pub fn from_edges(num_nodes: usize, edges: &[(usize, usize)]) -> GraphResult<Self> {
        let mut adj = Array2::zeros((num_nodes, num_nodes));

        for &(src, dst) in edges {
            if src >= num_nodes || dst >= num_nodes {
                return Err(NumRs2Error::ValueError(format!(
                    "Edge ({}, {}) out of bounds for {} nodes",
                    src, dst, num_nodes
                )));
            }
            adj[[src, dst]] = T::one();
        }

        Ok(Self { num_nodes, adj })
    }

    /// Create adjacency matrix with self-loops
    pub fn with_self_loops(&self) -> GraphResult<Self> {
        let mut adj = self.adj.clone();
        for i in 0..self.num_nodes {
            adj[[i, i]] = T::one();
        }
        Ok(Self {
            num_nodes: self.num_nodes,
            adj,
        })
    }

    /// Compute degree matrix
    pub fn degree_matrix(&self) -> GraphResult<Array1<T>> {
        let mut degrees = self.adj.sum_axis(Axis(1));
        // Ensure minimum degree of 1 to avoid division by zero in normalization
        for deg in degrees.iter_mut() {
            if *deg < T::one() {
                *deg = T::one();
            }
        }
        Ok(degrees)
    }

    /// Normalize adjacency matrix: D^(-1/2) A D^(-1/2)
    ///
    /// This is the symmetric normalization used in GCN
    pub fn symmetric_normalize(&self) -> GraphResult<Array2<T>> {
        let degrees = self.degree_matrix()?;
        let mut d_inv_sqrt = Array1::zeros(self.num_nodes);

        for (i, &deg) in degrees.iter().enumerate() {
            if deg > T::zero() {
                d_inv_sqrt[i] = T::one() / deg.sqrt();
            }
        }

        let mut norm_adj = self.adj.clone();
        for i in 0..self.num_nodes {
            for j in 0..self.num_nodes {
                norm_adj[[i, j]] = norm_adj[[i, j]] * d_inv_sqrt[i] * d_inv_sqrt[j];
            }
        }

        Ok(norm_adj)
    }
}

/// Edge list representation (COO format)
///
/// Memory-efficient for sparse graphs. Memory usage: O(|E|)
///
/// # Fields
///
/// - `num_nodes`: Number of nodes
/// - `edges`: List of (source, target, weight) tuples
#[derive(Debug, Clone)]
pub struct EdgeList<T: Float> {
    pub num_nodes: usize,
    pub edges: Vec<(usize, usize, T)>,
}

impl<T: Float> EdgeList<T> {
    /// Create edge list from unweighted edges (weight = 1.0)
    pub fn from_edges(num_nodes: usize, edges: &[(usize, usize)]) -> GraphResult<Self> {
        // Validate edges are within bounds
        for &(src, dst) in edges {
            if src >= num_nodes || dst >= num_nodes {
                return Err(NumRs2Error::ValueError(format!(
                    "Edge ({}, {}) out of bounds for {} nodes",
                    src, dst, num_nodes
                )));
            }
        }

        let weighted_edges: Vec<_> = edges
            .iter()
            .map(|&(src, dst)| (src, dst, T::one()))
            .collect();
        Ok(Self {
            num_nodes,
            edges: weighted_edges,
        })
    }

    /// Create edge list with weights
    pub fn from_weighted_edges(
        num_nodes: usize,
        edges: Vec<(usize, usize, T)>,
    ) -> GraphResult<Self> {
        for &(src, dst, _) in &edges {
            if src >= num_nodes || dst >= num_nodes {
                return Err(NumRs2Error::ValueError(format!(
                    "Edge ({}, {}) out of bounds for {} nodes",
                    src, dst, num_nodes
                )));
            }
        }
        Ok(Self { num_nodes, edges })
    }

    /// Convert to CSR format for efficient operations
    pub fn to_csr(&self) -> GraphResult<SparseAdjacency<T>> {
        SparseAdjacency::from_edge_list(self)
    }
}

/// Sparse adjacency in CSR (Compressed Sparse Row) format
///
/// Efficient for sparse graph operations. Memory usage: O(n + |E|)
///
/// # Fields
///
/// - `num_nodes`: Number of nodes
/// - `row_ptr`: Row pointers (length = num_nodes + 1)
/// - `col_indices`: Column indices for non-zero entries
/// - `values`: Non-zero values
///
/// # Format
///
/// CSR stores sparse matrices efficiently:
/// - `row_ptr[i]` to `row_ptr[i+1]` indexes into col_indices for row i
/// - Enables fast row access and matrix-vector multiplication
#[derive(Debug, Clone)]
pub struct SparseAdjacency<T: Float> {
    pub num_nodes: usize,
    pub row_ptr: Vec<usize>,
    pub col_indices: Vec<usize>,
    pub values: Vec<T>,
}

impl<T: Float> SparseAdjacency<T> {
    /// Create CSR adjacency from edge list
    pub fn from_edge_list(edge_list: &EdgeList<T>) -> GraphResult<Self> {
        let num_nodes = edge_list.num_nodes;
        let mut row_ptr = vec![0; num_nodes + 1];

        // Count edges per row
        for &(src, _, _) in &edge_list.edges {
            row_ptr[src + 1] += 1;
        }

        // Cumulative sum for row pointers
        for i in 1..=num_nodes {
            row_ptr[i] += row_ptr[i - 1];
        }

        let num_edges = edge_list.edges.len();
        let mut col_indices = vec![0; num_edges];
        let mut values = vec![T::zero(); num_edges];
        let mut current_pos = row_ptr[..num_nodes].to_vec();

        // Fill CSR arrays
        for &(src, dst, weight) in &edge_list.edges {
            let pos = current_pos[src];
            col_indices[pos] = dst;
            values[pos] = weight;
            current_pos[src] += 1;
        }

        Ok(Self {
            num_nodes,
            row_ptr,
            col_indices,
            values,
        })
    }

    /// Create from edges (unweighted)
    pub fn from_edges(num_nodes: usize, edges: &[(usize, usize)]) -> GraphResult<Self> {
        let edge_list = EdgeList::from_edges(num_nodes, edges)?;
        Self::from_edge_list(&edge_list)
    }

    /// Get neighbors of a node
    pub fn neighbors(&self, node: usize) -> GraphResult<(&[usize], &[T])> {
        if node >= self.num_nodes {
            return Err(NumRs2Error::ValueError(format!(
                "Node {} out of bounds for {} nodes",
                node, self.num_nodes
            )));
        }

        let start = self.row_ptr[node];
        let end = self.row_ptr[node + 1];
        Ok((&self.col_indices[start..end], &self.values[start..end]))
    }

    /// Compute node degrees
    pub fn degrees(&self) -> Array1<T> {
        let mut degrees = Array1::zeros(self.num_nodes);
        for i in 0..self.num_nodes {
            let start = self.row_ptr[i];
            let end = self.row_ptr[i + 1];
            let degree = T::from((end - start) as f64).unwrap_or(T::zero());
            // Ensure minimum degree of 1 to avoid division by zero in normalization
            degrees[i] = if degree < T::one() { T::one() } else { degree };
        }
        degrees
    }
}

/// Graph data structure with node features and edge attributes
///
/// # Fields
///
/// - `adjacency`: Sparse adjacency matrix
/// - `node_features`: Node feature matrix (num_nodes × feature_dim)
/// - `edge_features`: Optional edge features
#[derive(Debug, Clone)]
pub struct GraphData<T: Float> {
    pub adjacency: SparseAdjacency<T>,
    pub node_features: Array2<T>,
    pub edge_features: Option<Array2<T>>,
}

impl<T: Float> GraphData<T> {
    /// Create graph data from edges and node features
    pub fn new(
        num_nodes: usize,
        edges: &[(usize, usize)],
        node_features: Array2<T>,
    ) -> GraphResult<Self> {
        if node_features.nrows() != num_nodes {
            return Err(NumRs2Error::ValueError(format!(
                "Node features has {} rows but graph has {} nodes",
                node_features.nrows(),
                num_nodes
            )));
        }

        let adjacency = SparseAdjacency::from_edges(num_nodes, edges)?;
        Ok(Self {
            adjacency,
            node_features,
            edge_features: None,
        })
    }

    /// Add edge features
    pub fn with_edge_features(mut self, edge_features: Array2<T>) -> Self {
        self.edge_features = Some(edge_features);
        self
    }
}

// ================================================================================================
// Aggregation Functions
// ================================================================================================

/// Mean aggregation over neighborhoods
///
/// For each node, computes the mean of its neighbors' features.
///
/// # Arguments
///
/// * `adj` - Sparse adjacency matrix
/// * `features` - Node features (num_nodes × feature_dim)
///
/// # Returns
///
/// Aggregated features (num_nodes × feature_dim)
pub fn mean_aggregation<T>(
    adj: &SparseAdjacency<T>,
    features: &ArrayView2<T>,
) -> GraphResult<Array2<T>>
where
    T: Float + SimdUnifiedOps,
{
    if features.nrows() != adj.num_nodes {
        return Err(NumRs2Error::ValueError(format!(
            "Features has {} rows but adjacency has {} nodes",
            features.nrows(),
            adj.num_nodes
        )));
    }

    let (num_nodes, feat_dim) = (features.nrows(), features.ncols());
    let mut aggregated = Array2::zeros((num_nodes, feat_dim));

    for i in 0..num_nodes {
        let (neighbors, _weights) = adj.neighbors(i)?;
        if neighbors.is_empty() {
            continue;
        }

        let num_neighbors = T::from(neighbors.len() as f64).unwrap_or(T::one());
        for &neighbor in neighbors {
            for j in 0..feat_dim {
                aggregated[[i, j]] = aggregated[[i, j]] + features[[neighbor, j]];
            }
        }

        // Normalize by number of neighbors
        for j in 0..feat_dim {
            aggregated[[i, j]] = aggregated[[i, j]] / num_neighbors;
        }
    }

    Ok(aggregated)
}

/// Sum aggregation over neighborhoods
///
/// For each node, sums its neighbors' features.
pub fn sum_aggregation<T>(
    adj: &SparseAdjacency<T>,
    features: &ArrayView2<T>,
) -> GraphResult<Array2<T>>
where
    T: Float + SimdUnifiedOps,
{
    if features.nrows() != adj.num_nodes {
        return Err(NumRs2Error::ValueError(format!(
            "Features has {} rows but adjacency has {} nodes",
            features.nrows(),
            adj.num_nodes
        )));
    }

    let (num_nodes, feat_dim) = (features.nrows(), features.ncols());
    let mut aggregated = Array2::zeros((num_nodes, feat_dim));

    for i in 0..num_nodes {
        let (neighbors, _weights) = adj.neighbors(i)?;
        for &neighbor in neighbors {
            for j in 0..feat_dim {
                aggregated[[i, j]] = aggregated[[i, j]] + features[[neighbor, j]];
            }
        }
    }

    Ok(aggregated)
}

/// Max pooling aggregation over neighborhoods
///
/// For each node, takes the element-wise maximum of its neighbors' features.
pub fn max_pooling_aggregation<T>(
    adj: &SparseAdjacency<T>,
    features: &ArrayView2<T>,
) -> GraphResult<Array2<T>>
where
    T: Float + SimdUnifiedOps,
{
    if features.nrows() != adj.num_nodes {
        return Err(NumRs2Error::ValueError(format!(
            "Features has {} rows but adjacency has {} nodes",
            features.nrows(),
            adj.num_nodes
        )));
    }

    let (num_nodes, feat_dim) = (features.nrows(), features.ncols());
    let mut aggregated = Array2::from_elem((num_nodes, feat_dim), T::neg_infinity());

    for i in 0..num_nodes {
        let (neighbors, _weights) = adj.neighbors(i)?;
        if neighbors.is_empty() {
            for j in 0..feat_dim {
                aggregated[[i, j]] = T::zero();
            }
            continue;
        }

        for &neighbor in neighbors {
            for j in 0..feat_dim {
                let val = features[[neighbor, j]];
                if val > aggregated[[i, j]] {
                    aggregated[[i, j]] = val;
                }
            }
        }
    }

    Ok(aggregated)
}

// ================================================================================================
// GCN Layer
// ================================================================================================

/// Graph Convolutional Network layer
///
/// Implements the GCN layer from Kipf & Welling (2017):
/// `H^(l+1) = σ(D^(-1/2) A D^(-1/2) H^(l) W^(l))`
///
/// # Fields
///
/// - `in_features`: Input feature dimension
/// - `out_features`: Output feature dimension
/// - `weight`: Learnable weight matrix (in_features × out_features)
/// - `bias`: Optional bias vector (out_features)
/// - `use_bias`: Whether to use bias
///
/// # Example
///
/// ```rust,ignore
/// let gcn = GcnLayer::new(64, 128)?;
/// let output = gcn.forward(&adj, &features.view())?;
/// ```
#[derive(Debug, Clone)]
pub struct GcnLayer<T: Float> {
    pub in_features: usize,
    pub out_features: usize,
    pub weight: Array2<T>,
    pub bias: Option<Array1<T>>,
    pub use_bias: bool,
}

impl<T: Float + SimdUnifiedOps + 'static> GcnLayer<T> {
    /// Create new GCN layer
    ///
    /// Weights are initialized with Xavier/Glorot initialization
    pub fn new(in_features: usize, out_features: usize) -> GraphResult<Self> {
        Self::new_with_bias(in_features, out_features, true)
    }

    /// Create GCN layer with optional bias
    pub fn new_with_bias(
        in_features: usize,
        out_features: usize,
        use_bias: bool,
    ) -> GraphResult<Self> {
        // Xavier initialization: uniform(-sqrt(6/(in+out)), sqrt(6/(in+out)))
        let scale = T::from((6.0 / (in_features + out_features) as f64).sqrt()).unwrap_or(T::one());

        let weight = Array2::from_shape_fn((in_features, out_features), |(i, j)| {
            // Simple deterministic initialization for now
            let val = (((i * out_features + j) % 100) as f64 - 50.0) / 50.0;
            T::from(val).unwrap_or(T::zero()) * scale
        });

        let bias = if use_bias {
            Some(Array1::zeros(out_features))
        } else {
            None
        };

        Ok(Self {
            in_features,
            out_features,
            weight,
            bias,
            use_bias,
        })
    }

    /// Forward pass
    ///
    /// # Arguments
    ///
    /// * `adj` - Adjacency matrix (will be normalized)
    /// * `features` - Node features (num_nodes × in_features)
    ///
    /// # Returns
    ///
    /// Updated node features (num_nodes × out_features)
    pub fn forward(
        &self,
        adj: &AdjacencyMatrix<T>,
        features: &ArrayView2<T>,
    ) -> GraphResult<Array2<T>> {
        if features.ncols() != self.in_features {
            return Err(NumRs2Error::ValueError(format!(
                "Expected {} input features, got {}",
                self.in_features,
                features.ncols()
            )));
        }

        if features.nrows() != adj.num_nodes {
            return Err(NumRs2Error::ValueError(format!(
                "Features has {} rows but adjacency has {} nodes",
                features.nrows(),
                adj.num_nodes
            )));
        }

        // Add self-loops
        let adj_self = adj.with_self_loops()?;

        // Symmetric normalization
        let norm_adj = adj_self.symmetric_normalize()?;

        // AH
        let ah = norm_adj.dot(features);

        // AHW
        let output = ah.dot(&self.weight);

        // Add bias if present
        let mut output = output;
        if let Some(ref bias) = self.bias {
            for i in 0..output.nrows() {
                for j in 0..output.ncols() {
                    output[[i, j]] = output[[i, j]] + bias[j];
                }
            }
        }

        Ok(output)
    }
}

// ================================================================================================
// GAT Layer
// ================================================================================================

/// Graph Attention Network layer
///
/// Implements multi-head attention from Veličković et al. (2018):
/// `α_ij = softmax(LeakyReLU(a^T [Wh_i || Wh_j]))`
///
/// # Fields
///
/// - `in_features`: Input feature dimension
/// - `out_features`: Output feature dimension per head
/// - `num_heads`: Number of attention heads
/// - `concat`: Whether to concatenate or average heads
/// - `alpha`: LeakyReLU negative slope (default: 0.2)
///
/// # Example
///
/// ```rust,ignore
/// let gat = GatLayer::new(64, 16, 8, true, 0.2)?; // 8 heads of 16 features
/// let output = gat.forward(&adj, &features.view())?; // output: num_nodes × (8*16)
/// ```
#[derive(Debug, Clone)]
pub struct GatLayer<T: Float> {
    pub in_features: usize,
    pub out_features: usize,
    pub num_heads: usize,
    pub concat: bool,
    pub alpha: T, // LeakyReLU slope
    pub weights: Vec<Array2<T>>,
    pub attention_weights: Vec<Array1<T>>,
}

impl<T: Float + SimdUnifiedOps + scirs2_core::ndarray::ScalarOperand> GatLayer<T> {
    /// Create new GAT layer
    ///
    /// # Arguments
    ///
    /// * `in_features` - Input dimension
    /// * `out_features` - Output dimension per head
    /// * `num_heads` - Number of attention heads
    /// * `concat` - If true, concatenate heads; if false, average
    /// * `alpha` - LeakyReLU negative slope
    pub fn new(
        in_features: usize,
        out_features: usize,
        num_heads: usize,
        concat: bool,
        alpha: f64,
    ) -> GraphResult<Self> {
        if num_heads == 0 {
            return Err(NumRs2Error::InvalidOperation(
                "Number of attention heads must be > 0".to_string(),
            ));
        }

        let mut weights = Vec::new();
        let mut attention_weights = Vec::new();

        for _ in 0..num_heads {
            let w = Array2::from_shape_fn((in_features, out_features), |(i, j)| {
                let val = (((i * out_features + j) % 100) as f64 - 50.0) / 100.0;
                T::from(val).unwrap_or(T::zero())
            });
            weights.push(w);

            // Attention vector (2 * out_features for concatenation)
            let a = Array1::from_shape_fn(2 * out_features, |i| {
                let val = ((i % 100) as f64 - 50.0) / 100.0;
                T::from(val).unwrap_or(T::zero())
            });
            attention_weights.push(a);
        }

        Ok(Self {
            in_features,
            out_features,
            num_heads,
            concat,
            alpha: T::from(alpha).unwrap_or(T::from(0.2).unwrap_or(T::zero())),
            weights,
            attention_weights,
        })
    }

    /// Compute attention coefficients
    fn compute_attention(
        &self,
        h: &Array2<T>,
        adj: &SparseAdjacency<T>,
        head_idx: usize,
    ) -> GraphResult<HashMap<(usize, usize), T>> {
        let mut attention_map = HashMap::new();
        let num_nodes = h.nrows();

        for i in 0..num_nodes {
            let (neighbors, _) = adj.neighbors(i)?;

            if neighbors.is_empty() {
                continue;
            }

            // Collect attention logits for all neighbors
            let mut logits = Vec::new();
            for &j in neighbors {
                // Concatenate h_i and h_j
                let mut concat = Array1::zeros(2 * self.out_features);
                for k in 0..self.out_features {
                    concat[k] = h[[i, k]];
                    concat[k + self.out_features] = h[[j, k]];
                }

                // e_ij = a^T [Wh_i || Wh_j]
                let mut e_ij = T::zero();
                for k in 0..2 * self.out_features {
                    e_ij = e_ij + self.attention_weights[head_idx][k] * concat[k];
                }

                // LeakyReLU
                if e_ij < T::zero() {
                    e_ij = e_ij * self.alpha;
                }

                logits.push((j, e_ij));
            }

            // Softmax normalization
            let max_logit = logits
                .iter()
                .map(|(_, e)| *e)
                .fold(T::neg_infinity(), |a, b| if a > b { a } else { b });

            let mut sum_exp = T::zero();
            let mut exp_logits = Vec::new();
            for (j, e_ij) in logits {
                let exp_val = (e_ij - max_logit).exp();
                sum_exp = sum_exp + exp_val;
                exp_logits.push((j, exp_val));
            }

            // Normalized attention coefficients
            for (j, exp_val) in exp_logits {
                let alpha_ij = exp_val / sum_exp;
                attention_map.insert((i, j), alpha_ij);
            }
        }

        Ok(attention_map)
    }

    /// Forward pass
    pub fn forward(
        &self,
        adj: &SparseAdjacency<T>,
        features: &ArrayView2<T>,
    ) -> GraphResult<Array2<T>> {
        if features.ncols() != self.in_features {
            return Err(NumRs2Error::ValueError(format!(
                "Expected {} input features, got {}",
                self.in_features,
                features.ncols()
            )));
        }

        let num_nodes = features.nrows();
        let mut head_outputs = Vec::new();

        for head in 0..self.num_heads {
            // Transform features: Wh
            let h = features.dot(&self.weights[head]);

            // Compute attention coefficients
            let attention = self.compute_attention(&h, adj, head)?;

            // Aggregate with attention
            let mut output = Array2::zeros((num_nodes, self.out_features));
            for i in 0..num_nodes {
                for j in 0..num_nodes {
                    if let Some(&alpha_ij) = attention.get(&(i, j)) {
                        for k in 0..self.out_features {
                            output[[i, k]] = output[[i, k]] + alpha_ij * h[[j, k]];
                        }
                    }
                }
            }

            head_outputs.push(output);
        }

        // Combine heads
        let final_output = if self.concat {
            // Concatenate all heads
            let total_dim = self.num_heads * self.out_features;
            let mut combined = Array2::zeros((num_nodes, total_dim));
            for (head_idx, head_out) in head_outputs.iter().enumerate() {
                let start_col = head_idx * self.out_features;
                for i in 0..num_nodes {
                    for j in 0..self.out_features {
                        combined[[i, start_col + j]] = head_out[[i, j]];
                    }
                }
            }
            combined
        } else {
            // Average all heads
            let mut combined = Array2::zeros((num_nodes, self.out_features));
            let num_heads_t = T::from(self.num_heads as f64).unwrap_or(T::one());
            for head_out in &head_outputs {
                combined = combined + head_out;
            }
            combined / num_heads_t
        };

        Ok(final_output)
    }
}

// ================================================================================================
// GraphSAGE Layer
// ================================================================================================

/// GraphSAGE aggregator type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SageAggregator {
    /// Mean aggregator (simple average)
    Mean,
    /// Pool aggregator (max pooling after MLP)
    Pool,
    /// LSTM aggregator (not fully implemented - uses mean for now)
    Lstm,
}

/// GraphSAGE layer
///
/// Implements inductive learning from Hamilton et al. (2017):
/// `h_v^(l+1) = σ(W · CONCAT(h_v^(l), AGG({h_u^(l), ∀u ∈ N(v)})))`
///
/// # Fields
///
/// - `in_features`: Input feature dimension
/// - `out_features`: Output feature dimension
/// - `aggregator`: Aggregation method (Mean, Pool, LSTM)
/// - `normalize`: Whether to L2-normalize output
#[derive(Debug, Clone)]
pub struct GraphSageLayer<T: Float> {
    pub in_features: usize,
    pub out_features: usize,
    pub aggregator: SageAggregator,
    pub normalize: bool,
    pub weight: Array2<T>,
}

impl<T: Float + SimdUnifiedOps + 'static> GraphSageLayer<T> {
    /// Create new GraphSAGE layer
    pub fn new(
        in_features: usize,
        out_features: usize,
        aggregator: SageAggregator,
        normalize: bool,
    ) -> GraphResult<Self> {
        // Weight for concatenated features (2 * in_features → out_features)
        let weight = Array2::from_shape_fn((2 * in_features, out_features), |(i, j)| {
            let val = (((i * out_features + j) % 100) as f64 - 50.0) / 100.0;
            T::from(val).unwrap_or(T::zero())
        });

        Ok(Self {
            in_features,
            out_features,
            aggregator,
            normalize,
            weight,
        })
    }

    /// Forward pass
    pub fn forward(
        &self,
        adj: &SparseAdjacency<T>,
        features: &ArrayView2<T>,
    ) -> GraphResult<Array2<T>> {
        if features.ncols() != self.in_features {
            return Err(NumRs2Error::ValueError(format!(
                "Expected {} input features, got {}",
                self.in_features,
                features.ncols()
            )));
        }

        // Aggregate neighbors
        let aggregated = match self.aggregator {
            SageAggregator::Mean => mean_aggregation(adj, features)?,
            SageAggregator::Pool => max_pooling_aggregation(adj, features)?,
            SageAggregator::Lstm => mean_aggregation(adj, features)?, // Simplified
        };

        // Concatenate self features with aggregated neighbor features
        let num_nodes = features.nrows();
        let mut concat = Array2::zeros((num_nodes, 2 * self.in_features));
        for i in 0..num_nodes {
            for j in 0..self.in_features {
                concat[[i, j]] = features[[i, j]];
                concat[[i, j + self.in_features]] = aggregated[[i, j]];
            }
        }

        // Transform: W * concat
        let mut output = concat.dot(&self.weight);

        // L2 normalization
        if self.normalize {
            for i in 0..num_nodes {
                let mut norm = T::zero();
                for j in 0..self.out_features {
                    norm = norm + output[[i, j]] * output[[i, j]];
                }
                norm = norm.sqrt();
                if norm > T::zero() {
                    for j in 0..self.out_features {
                        output[[i, j]] = output[[i, j]] / norm;
                    }
                }
            }
        }

        Ok(output)
    }
}

// ================================================================================================
// MPNN Framework
// ================================================================================================

/// Message Passing Neural Network layer
///
/// Implements the MPNN framework from Gilmer et al. (2017):
/// - Message: `m_v = Σ_{u∈N(v)} M(h_v, h_u, e_{uv})`
/// - Update: `h_v' = U(h_v, m_v)`
///
/// This is a simplified implementation with learnable message and update functions.
#[derive(Debug, Clone)]
pub struct MpnnLayer<T: Float> {
    pub in_features: usize,
    pub out_features: usize,
    pub message_weight: Array2<T>,
    pub update_weight: Array2<T>,
}

impl<T: Float + SimdUnifiedOps + 'static> MpnnLayer<T> {
    /// Create new MPNN layer
    pub fn new(in_features: usize, out_features: usize) -> GraphResult<Self> {
        let message_weight = Array2::from_shape_fn((in_features, out_features), |(i, j)| {
            let val = (((i * out_features + j) % 100) as f64 - 50.0) / 100.0;
            T::from(val).unwrap_or(T::zero())
        });

        let update_weight =
            Array2::from_shape_fn((in_features + out_features, out_features), |(i, j)| {
                let val = (((i * out_features + j + 13) % 100) as f64 - 50.0) / 100.0;
                T::from(val).unwrap_or(T::zero())
            });

        Ok(Self {
            in_features,
            out_features,
            message_weight,
            update_weight,
        })
    }

    /// Forward pass
    pub fn forward(
        &self,
        adj: &SparseAdjacency<T>,
        features: &ArrayView2<T>,
    ) -> GraphResult<Array2<T>> {
        if features.ncols() != self.in_features {
            return Err(NumRs2Error::ValueError(format!(
                "Expected {} input features, got {}",
                self.in_features,
                features.ncols()
            )));
        }

        let num_nodes = features.nrows();

        // Message phase: compute messages from neighbors
        let messages = sum_aggregation(adj, features)?;
        let transformed_messages = messages.dot(&self.message_weight);

        // Update phase: combine node features with messages
        let mut concat = Array2::zeros((num_nodes, self.in_features + self.out_features));
        for i in 0..num_nodes {
            for j in 0..self.in_features {
                concat[[i, j]] = features[[i, j]];
            }
            for j in 0..self.out_features {
                concat[[i, j + self.in_features]] = transformed_messages[[i, j]];
            }
        }

        let output = concat.dot(&self.update_weight);
        Ok(output)
    }
}

// ================================================================================================
// GIN Layer
// ================================================================================================

/// Graph Isomorphism Network layer
///
/// Implements GIN from Xu et al. (2019):
/// `h_v^(l+1) = MLP((1 + ε) · h_v^(l) + Σ_{u∈N(v)} h_u^(l))`
///
/// GIN is provably as powerful as the Weisfeiler-Lehman graph isomorphism test.
///
/// # Fields
///
/// - `epsilon`: Learnable or fixed epsilon (0.0 for fixed)
/// - `mlp_weight`: MLP weight matrix
#[derive(Debug, Clone)]
pub struct GinLayer<T: Float> {
    pub in_features: usize,
    pub out_features: usize,
    pub epsilon: T,
    pub mlp_weight: Array2<T>,
}

impl<T: Float + SimdUnifiedOps + 'static> GinLayer<T> {
    /// Create new GIN layer
    ///
    /// # Arguments
    ///
    /// * `in_features` - Input dimension
    /// * `out_features` - Output dimension
    /// * `epsilon` - Epsilon parameter (0.0 for non-learnable)
    pub fn new(in_features: usize, out_features: usize, epsilon: f64) -> GraphResult<Self> {
        let mlp_weight = Array2::from_shape_fn((in_features, out_features), |(i, j)| {
            let val = (((i * out_features + j) % 100) as f64 - 50.0) / 100.0;
            T::from(val).unwrap_or(T::zero())
        });

        Ok(Self {
            in_features,
            out_features,
            epsilon: T::from(epsilon).unwrap_or(T::zero()),
            mlp_weight,
        })
    }

    /// Forward pass
    pub fn forward(
        &self,
        adj: &SparseAdjacency<T>,
        features: &ArrayView2<T>,
    ) -> GraphResult<Array2<T>> {
        if features.ncols() != self.in_features {
            return Err(NumRs2Error::ValueError(format!(
                "Expected {} input features, got {}",
                self.in_features,
                features.ncols()
            )));
        }

        // Sum aggregation
        let neighbor_sum = sum_aggregation(adj, features)?;

        // (1 + ε) * h_v + Σ h_u
        let one_plus_eps = T::one() + self.epsilon;
        let num_nodes = features.nrows();
        let mut combined = Array2::zeros((num_nodes, self.in_features));

        for i in 0..num_nodes {
            for j in 0..self.in_features {
                combined[[i, j]] = one_plus_eps * features[[i, j]] + neighbor_sum[[i, j]];
            }
        }

        // MLP transformation
        let output = combined.dot(&self.mlp_weight);
        Ok(output)
    }
}

// ================================================================================================
// Graph Pooling
// ================================================================================================

/// Global mean pooling
///
/// Computes mean of node features across all nodes to get graph-level representation.
pub fn global_mean_pool<T>(node_features: &ArrayView2<T>) -> GraphResult<Array1<T>>
where
    T: Float + SimdUnifiedOps + scirs2_core::ndarray::ScalarOperand,
{
    let num_nodes = T::from(node_features.nrows() as f64).unwrap_or(T::one());
    let mean = node_features.sum_axis(Axis(0)) / num_nodes;
    Ok(mean)
}

/// Global max pooling
///
/// Takes element-wise maximum across all node features.
pub fn global_max_pool<T>(node_features: &ArrayView2<T>) -> GraphResult<Array1<T>>
where
    T: Float + SimdUnifiedOps,
{
    let mut max_feat = Array1::from_elem(node_features.ncols(), T::neg_infinity());
    for i in 0..node_features.nrows() {
        for j in 0..node_features.ncols() {
            let val = node_features[[i, j]];
            if val > max_feat[j] {
                max_feat[j] = val;
            }
        }
    }
    Ok(max_feat)
}

/// Global sum pooling
///
/// Sums node features across all nodes.
pub fn global_sum_pool<T>(node_features: &ArrayView2<T>) -> GraphResult<Array1<T>>
where
    T: Float + SimdUnifiedOps,
{
    Ok(node_features.sum_axis(Axis(0)))
}

/// Top-K pooling
///
/// Selects top-k nodes based on importance scores.
///
/// # Arguments
///
/// * `node_features` - Node features (num_nodes × feature_dim)
/// * `scores` - Importance score for each node (num_nodes)
/// * `k` - Number of nodes to keep
///
/// # Returns
///
/// Pooled features (k × feature_dim)
pub fn topk_pool<T>(
    node_features: &ArrayView2<T>,
    scores: &ArrayView1<T>,
    k: usize,
) -> GraphResult<Array2<T>>
where
    T: Float + SimdUnifiedOps,
{
    let num_nodes = node_features.nrows();
    if scores.len() != num_nodes {
        return Err(NumRs2Error::ValueError(format!(
            "Scores length {} doesn't match number of nodes {}",
            scores.len(),
            num_nodes
        )));
    }

    if k > num_nodes {
        return Err(NumRs2Error::ValueError(format!(
            "k={} exceeds number of nodes={}",
            k, num_nodes
        )));
    }

    // Get indices of top-k scores
    let mut indexed_scores: Vec<_> = scores.iter().enumerate().map(|(i, &s)| (i, s)).collect();
    indexed_scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

    let top_k_indices: Vec<_> = indexed_scores.iter().take(k).map(|(i, _)| *i).collect();

    // Extract features for top-k nodes
    let feat_dim = node_features.ncols();
    let mut pooled = Array2::zeros((k, feat_dim));
    for (new_idx, &orig_idx) in top_k_indices.iter().enumerate() {
        for j in 0..feat_dim {
            pooled[[new_idx, j]] = node_features[[orig_idx, j]];
        }
    }

    Ok(pooled)
}

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

    #[test]
    fn test_adjacency_matrix_creation() {
        let edges = vec![(0, 1), (1, 2), (2, 0)];
        let adj =
            AdjacencyMatrix::<f64>::from_edges(3, &edges).expect("test: valid adjacency matrix");
        assert_eq!(adj.num_nodes, 3);
        assert_eq!(adj.adj[[0, 1]], 1.0);
        assert_eq!(adj.adj[[1, 2]], 1.0);
        assert_eq!(adj.adj[[2, 0]], 1.0);
        assert_eq!(adj.adj[[0, 0]], 0.0);
    }

    #[test]
    fn test_adjacency_with_self_loops() {
        let edges = vec![(0, 1), (1, 2)];
        let adj =
            AdjacencyMatrix::<f64>::from_edges(3, &edges).expect("test: valid adjacency matrix");
        let adj_self = adj
            .with_self_loops()
            .expect("test: valid self-loop addition");
        assert_eq!(adj_self.adj[[0, 0]], 1.0);
        assert_eq!(adj_self.adj[[1, 1]], 1.0);
        assert_eq!(adj_self.adj[[2, 2]], 1.0);
    }

    #[test]
    fn test_degree_matrix() {
        let edges = vec![(0, 1), (0, 2), (1, 2)];
        let adj =
            AdjacencyMatrix::<f64>::from_edges(3, &edges).expect("test: valid adjacency matrix");
        let degrees = adj.degree_matrix().expect("test: valid degree matrix");
        assert_eq!(degrees[0], 2.0); // node 0 has 2 outgoing edges
        assert_eq!(degrees[1], 1.0);
        assert_eq!(degrees[2], 1.0);
    }

    #[test]
    fn test_symmetric_normalization() {
        let edges = vec![(0, 1), (1, 0)];
        let adj =
            AdjacencyMatrix::<f64>::from_edges(2, &edges).expect("test: valid adjacency matrix");
        let norm = adj
            .symmetric_normalize()
            .expect("test: valid symmetric normalization");
        // D^(-1/2) = [[1, 0], [0, 1]] for degree 1
        // Normalized adj should be [[0, 1], [1, 0]]
        assert!((norm[[0, 1]] - 1.0).abs() < 1e-10);
        assert!((norm[[1, 0]] - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_edge_list_creation() {
        let edges = vec![(0, 1), (1, 2), (2, 0)];
        let edge_list = EdgeList::<f64>::from_edges(3, &edges).expect("test: valid edge list");
        assert_eq!(edge_list.num_nodes, 3);
        assert_eq!(edge_list.edges.len(), 3);
    }

    #[test]
    fn test_edge_list_out_of_bounds() {
        let edges = vec![(0, 5)]; // node 5 doesn't exist
        let result = EdgeList::<f64>::from_edges(3, &edges);
        assert!(result.is_err());
    }

    #[test]
    fn test_sparse_adjacency_from_edges() {
        let edges = vec![(0, 1), (0, 2), (1, 2)];
        let sparse =
            SparseAdjacency::<f64>::from_edges(3, &edges).expect("test: valid sparse adjacency");
        assert_eq!(sparse.num_nodes, 3);
        assert_eq!(sparse.row_ptr.len(), 4); // num_nodes + 1
        assert_eq!(sparse.col_indices.len(), 3);
    }

    #[test]
    fn test_sparse_adjacency_neighbors() {
        let edges = vec![(0, 1), (0, 2), (1, 2)];
        let sparse =
            SparseAdjacency::<f64>::from_edges(3, &edges).expect("test: valid sparse adjacency");
        let (neighbors, weights) = sparse.neighbors(0).expect("test: valid neighbor retrieval");
        assert_eq!(neighbors.len(), 2);
        assert!(neighbors.contains(&1));
        assert!(neighbors.contains(&2));
    }

    #[test]
    fn test_sparse_adjacency_degrees() {
        let edges = vec![(0, 1), (0, 2), (1, 2)];
        let sparse =
            SparseAdjacency::<f64>::from_edges(3, &edges).expect("test: valid sparse adjacency");
        let degrees = sparse.degrees();
        assert_eq!(degrees[0], 2.0);
        assert_eq!(degrees[1], 1.0);
        assert_eq!(degrees[2], 1.0);
    }

    #[test]
    fn test_mean_aggregation() {
        let edges = vec![(0, 1), (0, 2), (1, 2)];
        let sparse =
            SparseAdjacency::<f64>::from_edges(3, &edges).expect("test: valid sparse adjacency");
        let features = array![[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
        let agg =
            mean_aggregation(&sparse, &features.view()).expect("test: valid mean aggregation");
        // Node 0 neighbors: [1, 2] -> mean = [(3+5)/2, (4+6)/2] = [4, 5]
        assert_eq!(agg[[0, 0]], 4.0);
        assert_eq!(agg[[0, 1]], 5.0);
    }

    #[test]
    fn test_sum_aggregation() {
        let edges = vec![(0, 1), (0, 2)];
        let sparse =
            SparseAdjacency::<f64>::from_edges(3, &edges).expect("test: valid sparse adjacency");
        let features = array![[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
        let agg = sum_aggregation(&sparse, &features.view()).expect("test: valid sum aggregation");
        // Node 0 neighbors: [1, 2] -> sum = [3+5, 4+6] = [8, 10]
        assert_eq!(agg[[0, 0]], 8.0);
        assert_eq!(agg[[0, 1]], 10.0);
    }

    #[test]
    fn test_max_pooling_aggregation() {
        let edges = vec![(0, 1), (0, 2)];
        let sparse =
            SparseAdjacency::<f64>::from_edges(3, &edges).expect("test: valid sparse adjacency");
        let features = array![[1.0, 6.0], [3.0, 4.0], [5.0, 2.0]];
        let agg = max_pooling_aggregation(&sparse, &features.view())
            .expect("test: valid max pooling aggregation");
        // Node 0 neighbors: [1, 2] -> max = [max(3,5), max(4,2)] = [5, 4]
        assert_eq!(agg[[0, 0]], 5.0);
        assert_eq!(agg[[0, 1]], 4.0);
    }

    #[test]
    fn test_gcn_layer_creation() {
        let gcn = GcnLayer::<f64>::new(10, 20).expect("test: valid GCN layer");
        assert_eq!(gcn.in_features, 10);
        assert_eq!(gcn.out_features, 20);
        assert_eq!(gcn.weight.shape(), &[10, 20]);
        assert!(gcn.use_bias);
    }

    #[test]
    fn test_gcn_layer_forward() {
        let edges = vec![(0, 1), (1, 2)];
        let adj =
            AdjacencyMatrix::<f64>::from_edges(3, &edges).expect("test: valid adjacency matrix");
        let features = Array2::ones((3, 5));
        let gcn = GcnLayer::new(5, 10).expect("test: valid GCN layer");
        let output = gcn
            .forward(&adj, &features.view())
            .expect("test: valid GCN forward pass");
        assert_eq!(output.shape(), &[3, 10]);
    }

    #[test]
    fn test_gcn_layer_dimension_mismatch() {
        let edges = vec![(0, 1)];
        let adj =
            AdjacencyMatrix::<f64>::from_edges(2, &edges).expect("test: valid adjacency matrix");
        let features = Array2::ones((2, 10));
        let gcn = GcnLayer::new(5, 10).expect("test: valid GCN layer (expects 5 input features)");
        let result = gcn.forward(&adj, &features.view());
        assert!(result.is_err());
    }

    #[test]
    fn test_gat_layer_creation() {
        let gat = GatLayer::<f64>::new(10, 8, 4, true, 0.2).expect("test: valid GAT layer");
        assert_eq!(gat.in_features, 10);
        assert_eq!(gat.out_features, 8);
        assert_eq!(gat.num_heads, 4);
        assert!(gat.concat);
    }

    #[test]
    fn test_gat_layer_zero_heads() {
        let result = GatLayer::<f64>::new(10, 8, 0, true, 0.2);
        assert!(result.is_err());
    }

    #[test]
    fn test_gat_layer_forward() {
        let edges = vec![(0, 1), (1, 2)];
        let sparse =
            SparseAdjacency::<f64>::from_edges(3, &edges).expect("test: valid sparse adjacency");
        let features = Array2::ones((3, 4));
        let gat = GatLayer::new(4, 2, 2, true, 0.2).expect("test: valid GAT layer");
        let output = gat
            .forward(&sparse, &features.view())
            .expect("test: valid GAT forward pass");
        // 2 heads × 2 features, concatenated
        assert_eq!(output.shape(), &[3, 4]);
    }

    #[test]
    fn test_gat_layer_average_heads() {
        let edges = vec![(0, 1)];
        let sparse =
            SparseAdjacency::<f64>::from_edges(2, &edges).expect("test: valid sparse adjacency");
        let features = Array2::ones((2, 4));
        let gat = GatLayer::new(4, 8, 2, false, 0.2).expect("test: valid GAT layer (concat=false)");
        let output = gat
            .forward(&sparse, &features.view())
            .expect("test: valid GAT forward pass");
        // Averaged heads, so output_dim = 8
        assert_eq!(output.shape(), &[2, 8]);
    }

    #[test]
    fn test_graphsage_layer_creation() {
        let sage = GraphSageLayer::<f64>::new(10, 20, SageAggregator::Mean, true)
            .expect("test: valid GraphSAGE layer");
        assert_eq!(sage.in_features, 10);
        assert_eq!(sage.out_features, 20);
        assert_eq!(sage.aggregator, SageAggregator::Mean);
        assert!(sage.normalize);
    }

    #[test]
    fn test_graphsage_layer_forward() {
        let edges = vec![(0, 1), (1, 2)];
        let sparse =
            SparseAdjacency::<f64>::from_edges(3, &edges).expect("test: valid sparse adjacency");
        let features = Array2::ones((3, 5));
        let sage = GraphSageLayer::new(5, 10, SageAggregator::Mean, false)
            .expect("test: valid GraphSAGE layer");
        let output = sage
            .forward(&sparse, &features.view())
            .expect("test: valid GraphSAGE forward pass");
        assert_eq!(output.shape(), &[3, 10]);
    }

    #[test]
    fn test_graphsage_pool_aggregator() {
        let edges = vec![(0, 1), (0, 2)];
        let sparse =
            SparseAdjacency::<f64>::from_edges(3, &edges).expect("test: valid sparse adjacency");
        let features = Array2::ones((3, 4));
        let sage = GraphSageLayer::new(4, 8, SageAggregator::Pool, true)
            .expect("test: valid GraphSAGE pool layer");
        let output = sage
            .forward(&sparse, &features.view())
            .expect("test: valid GraphSAGE forward pass");
        assert_eq!(output.shape(), &[3, 8]);
    }

    #[test]
    fn test_mpnn_layer_creation() {
        let mpnn = MpnnLayer::<f64>::new(10, 20).expect("test: valid MPNN layer");
        assert_eq!(mpnn.in_features, 10);
        assert_eq!(mpnn.out_features, 20);
    }

    #[test]
    fn test_mpnn_layer_forward() {
        let edges = vec![(0, 1), (1, 2)];
        let sparse =
            SparseAdjacency::<f64>::from_edges(3, &edges).expect("test: valid sparse adjacency");
        let features = Array2::ones((3, 5));
        let mpnn = MpnnLayer::new(5, 10).expect("test: valid MPNN layer");
        let output = mpnn
            .forward(&sparse, &features.view())
            .expect("test: valid MPNN forward pass");
        assert_eq!(output.shape(), &[3, 10]);
    }

    #[test]
    fn test_gin_layer_creation() {
        let gin = GinLayer::<f64>::new(10, 20, 0.0).expect("test: valid GIN layer");
        assert_eq!(gin.in_features, 10);
        assert_eq!(gin.out_features, 20);
        assert_eq!(gin.epsilon, 0.0);
    }

    #[test]
    fn test_gin_layer_forward() {
        let edges = vec![(0, 1), (1, 2)];
        let sparse =
            SparseAdjacency::<f64>::from_edges(3, &edges).expect("test: valid sparse adjacency");
        let features = Array2::ones((3, 5));
        let gin = GinLayer::new(5, 10, 0.0).expect("test: valid GIN layer");
        let output = gin
            .forward(&sparse, &features.view())
            .expect("test: valid GIN forward pass");
        assert_eq!(output.shape(), &[3, 10]);
    }

    #[test]
    fn test_gin_layer_with_epsilon() {
        let edges = vec![(0, 1)];
        let sparse =
            SparseAdjacency::<f64>::from_edges(2, &edges).expect("test: valid sparse adjacency");
        let features = array![[1.0, 2.0], [3.0, 4.0]];
        let gin = GinLayer::new(2, 2, 0.5).expect("test: valid GIN layer");
        let output = gin
            .forward(&sparse, &features.view())
            .expect("test: valid GIN forward pass");
        assert_eq!(output.shape(), &[2, 2]);
    }

    #[test]
    fn test_global_mean_pool() {
        let features = array![[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
        let pooled = global_mean_pool(&features.view()).expect("test: valid global mean pool");
        assert_eq!(pooled.len(), 2);
        assert_eq!(pooled[0], 3.0); // (1+3+5)/3
        assert_eq!(pooled[1], 4.0); // (2+4+6)/3
    }

    #[test]
    fn test_global_max_pool() {
        let features = array![[1.0, 6.0], [3.0, 4.0], [5.0, 2.0]];
        let pooled = global_max_pool(&features.view()).expect("test: valid global max pool");
        assert_eq!(pooled.len(), 2);
        assert_eq!(pooled[0], 5.0);
        assert_eq!(pooled[1], 6.0);
    }

    #[test]
    fn test_global_sum_pool() {
        let features = array![[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
        let pooled = global_sum_pool(&features.view()).expect("test: valid global sum pool");
        assert_eq!(pooled.len(), 2);
        assert_eq!(pooled[0], 9.0);
        assert_eq!(pooled[1], 12.0);
    }

    #[test]
    fn test_topk_pool() {
        let features = array![[1.0, 2.0], [3.0, 4.0], [5.0, 6.0], [7.0, 8.0]];
        let scores = array![0.1, 0.4, 0.2, 0.5];
        let pooled = topk_pool(&features.view(), &scores.view(), 2).expect("test: valid topk pool");
        assert_eq!(pooled.shape(), &[2, 2]);
        // Top 2 scores: indices 3 (0.5) and 1 (0.4)
        assert_eq!(pooled[[0, 0]], 7.0); // features of node 3
        assert_eq!(pooled[[0, 1]], 8.0);
    }

    #[test]
    fn test_topk_pool_k_exceeds_nodes() {
        let features = array![[1.0, 2.0], [3.0, 4.0]];
        let scores = array![0.1, 0.4];
        let result = topk_pool(&features.view(), &scores.view(), 5);
        assert!(result.is_err());
    }

    #[test]
    fn test_topk_pool_score_mismatch() {
        let features = array![[1.0, 2.0], [3.0, 4.0]];
        let scores = array![0.1]; // only 1 score for 2 nodes
        let result = topk_pool(&features.view(), &scores.view(), 1);
        assert!(result.is_err());
    }

    #[test]
    fn test_graph_data_creation() {
        let edges = vec![(0, 1), (1, 2)];
        let features = Array2::<f64>::ones((3, 5));
        let graph = GraphData::new(3, &edges, features).expect("test: valid graph data creation");
        assert_eq!(graph.adjacency.num_nodes, 3);
        assert_eq!(graph.node_features.shape(), &[3, 5]);
        assert!(graph.edge_features.is_none());
    }

    #[test]
    fn test_graph_data_feature_mismatch() {
        let edges = vec![(0, 1)];
        let features = Array2::<f64>::ones((3, 5)); // 3 nodes
        let result = GraphData::new(2, &edges, features); // but graph has 2 nodes
        assert!(result.is_err());
    }

    #[test]
    fn test_graph_data_with_edge_features() {
        let edges = vec![(0, 1), (1, 2)];
        let features = Array2::<f64>::ones((3, 5));
        let edge_features = Array2::<f64>::ones((2, 3));
        let graph = GraphData::new(3, &edges, features)
            .expect("test: valid graph data creation")
            .with_edge_features(edge_features);
        assert!(graph.edge_features.is_some());
        assert_eq!(
            graph
                .edge_features
                .expect("test: edge features are some")
                .shape(),
            &[2, 3]
        );
    }

    // Additional edge case tests

    #[test]
    fn test_empty_graph() {
        let edges: Vec<(usize, usize)> = vec![];
        let adj =
            AdjacencyMatrix::<f64>::from_edges(3, &edges).expect("test: valid adjacency matrix");
        assert_eq!(adj.num_nodes, 3);
        // All adjacency values should be 0
        for i in 0..3 {
            for j in 0..3 {
                assert_eq!(adj.adj[[i, j]], 0.0);
            }
        }
    }

    #[test]
    fn test_self_loop_graph() {
        let edges = vec![(0, 0), (1, 1), (2, 2)];
        let adj =
            AdjacencyMatrix::<f64>::from_edges(3, &edges).expect("test: valid adjacency matrix");
        assert_eq!(adj.adj[[0, 0]], 1.0);
        assert_eq!(adj.adj[[1, 1]], 1.0);
        assert_eq!(adj.adj[[2, 2]], 1.0);
    }

    #[test]
    fn test_complete_graph() {
        let edges = vec![(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)];
        let sparse =
            SparseAdjacency::<f64>::from_edges(3, &edges).expect("test: valid sparse adjacency");
        let degrees = sparse.degrees();
        // In a complete graph on 3 nodes, each node has degree 2
        assert_eq!(degrees[0], 2.0);
        assert_eq!(degrees[1], 2.0);
        assert_eq!(degrees[2], 2.0);
    }

    #[test]
    fn test_aggregation_isolated_node() {
        // Node 2 has no neighbors
        let edges = vec![(0, 1)];
        let sparse =
            SparseAdjacency::<f64>::from_edges(3, &edges).expect("test: valid sparse adjacency");
        let features = array![[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
        let agg =
            mean_aggregation(&sparse, &features.view()).expect("test: valid mean aggregation");
        // Node 2 has no neighbors, so aggregation should be [0, 0]
        assert_eq!(agg[[2, 0]], 0.0);
        assert_eq!(agg[[2, 1]], 0.0);
    }
}