torsh-graph 0.2.0

Graph neural network components for ToRSh - powered by SciRS2
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
//! Hypergraph Neural Networks
//!
//! Advanced implementation of hypergraph neural networks for multi-relational learning.
//! Hypergraphs generalize graphs by allowing edges (hyperedges) to connect any number of nodes,
//! enabling modeling of complex multi-way relationships in data.
//!
//! # Features:
//! - Hypergraph data structures with efficient storage
//! - Multiple hypergraph convolution layers (HGCN, HyperGAT, HGNN)
//! - Hypergraph attention mechanisms
//! - Advanced pooling and coarsening operations
//! - Spectral hypergraph methods
//! - Dynamic hypergraph construction
// Framework infrastructure - components designed for future use
#![allow(dead_code)]
/// Crate-local result alias: the error type defaults to [`TorshError`],
/// so both `Result<T>` and `Result<T, OtherError>` stay valid.
type Result<T, E = torsh_core::error::TorshError> = std::result::Result<T, E>;

use crate::parameter::Parameter;
use crate::{GraphData, GraphLayer};
use torsh_tensor::{
    creation::{from_vec, randn, zeros},
    Tensor,
};

/// Hypergraph data structure representing multi-way relationships
#[derive(Debug, Clone)]
pub struct HypergraphData {
    /// Node feature matrix (num_nodes x num_features)
    pub x: Tensor,
    /// Hyperedge incidence matrix (num_nodes x num_hyperedges)
    pub incidence_matrix: Tensor,
    /// Hyperedge weights (optional)
    pub hyperedge_weights: Option<Tensor>,
    /// Hyperedge features (optional)
    pub hyperedge_features: Option<Tensor>,
    /// Node degrees (sum of incident hyperedge weights)
    pub node_degrees: Tensor,
    /// Hyperedge cardinalities (number of nodes per hyperedge)
    pub hyperedge_cardinalities: Tensor,
    /// Number of nodes
    pub num_nodes: usize,
    /// Number of hyperedges
    pub num_hyperedges: usize,
}

impl HypergraphData {
    /// Create a new hypergraph from node features and incidence matrix
    pub fn new(x: Tensor, incidence_matrix: Tensor) -> Result<Self> {
        let num_nodes = x.shape().dims()[0];
        let num_hyperedges = incidence_matrix.shape().dims()[1];

        // Compute node degrees (sum over hyperedges - axis 1)
        let node_degrees = incidence_matrix.sum_dim(&[1], false)?;

        // Compute hyperedge cardinalities (sum over nodes - axis 0)
        let hyperedge_cardinalities = incidence_matrix.sum_dim(&[0], false)?;

        Ok(Self {
            x,
            incidence_matrix,
            hyperedge_weights: None,
            hyperedge_features: None,
            node_degrees,
            hyperedge_cardinalities,
            num_nodes,
            num_hyperedges,
        })
    }

    /// Add hyperedge weights
    pub fn with_hyperedge_weights(mut self, weights: Tensor) -> Self {
        self.hyperedge_weights = Some(weights);
        self
    }

    /// Add hyperedge features
    pub fn with_hyperedge_features(mut self, features: Tensor) -> Self {
        self.hyperedge_features = Some(features);
        self
    }

    /// Convert to regular graph using clique expansion
    pub fn to_graph_clique_expansion(&self) -> Result<GraphData> {
        let incidence_data = self.incidence_matrix.to_vec()?;
        let mut edges = Vec::new();

        // For each hyperedge, create clique (all pairs of nodes)
        for e in 0..self.num_hyperedges {
            let mut nodes_in_hyperedge = Vec::new();

            // Find nodes in this hyperedge
            for v in 0..self.num_nodes {
                let idx = v * self.num_hyperedges + e;
                if incidence_data[idx] > 0.0 {
                    nodes_in_hyperedge.push(v as f32);
                }
            }

            // Create all pairs within the hyperedge
            for i in 0..nodes_in_hyperedge.len() {
                for j in (i + 1)..nodes_in_hyperedge.len() {
                    edges.extend_from_slice(&[nodes_in_hyperedge[i], nodes_in_hyperedge[j]]);
                    edges.extend_from_slice(&[nodes_in_hyperedge[j], nodes_in_hyperedge[i]]);
                }
            }
        }

        let edge_index = if edges.is_empty() {
            zeros(&[2, 0])?
        } else {
            let num_edges = edges.len() / 2;
            from_vec(edges, &[2, num_edges], torsh_core::device::DeviceType::Cpu)?
        };

        Ok(GraphData::new(self.x.clone(), edge_index))
    }

    /// Convert to regular graph using star expansion
    pub fn to_graph_star_expansion(&self) -> Result<GraphData> {
        let incidence_data = self.incidence_matrix.to_vec()?;
        let mut edges = Vec::new();

        // For each hyperedge, create a star with center at virtual node
        let virtual_node_offset = self.num_nodes;

        for e in 0..self.num_hyperedges {
            let virtual_node = (virtual_node_offset + e) as f32;

            // Connect all nodes in hyperedge to virtual center
            for v in 0..self.num_nodes {
                let idx = v * self.num_hyperedges + e;
                if incidence_data[idx] > 0.0 {
                    let node = v as f32;
                    edges.extend_from_slice(&[node, virtual_node]);
                    edges.extend_from_slice(&[virtual_node, node]);
                }
            }
        }

        let edge_index = if edges.is_empty() {
            zeros(&[2, 0])?
        } else {
            let num_edges = edges.len() / 2;
            from_vec(edges, &[2, num_edges], torsh_core::device::DeviceType::Cpu)?
        };

        // Extend node features with virtual nodes
        let virtual_features: Tensor = randn(&[self.num_hyperedges, self.x.shape().dims()[1]])?;
        // Concatenate original and virtual node features
        let node_data = self.x.to_vec()?;
        let virtual_data = virtual_features.to_vec()?;
        let mut extended_data = node_data;
        extended_data.extend(virtual_data);

        let total_nodes = self.num_nodes + self.num_hyperedges;
        let features_dim = self.x.shape().dims()[1];
        let extended_x = from_vec(
            extended_data,
            &[total_nodes, features_dim],
            torsh_core::device::DeviceType::Cpu,
        )?;

        Ok(GraphData::new(extended_x, edge_index))
    }
}

/// Hypergraph Convolutional Network (HGCN) layer
#[derive(Debug)]
pub struct HGCNConv {
    in_features: usize,
    out_features: usize,
    weight: Parameter,
    bias: Option<Parameter>,
    use_attention: bool,
    attention_weight: Option<Parameter>,
    dropout: f32,
}

impl HGCNConv {
    /// Create a new HGCN layer
    pub fn new(
        in_features: usize,
        out_features: usize,
        bias: bool,
        use_attention: bool,
        dropout: f32,
    ) -> Result<Self> {
        let weight = Parameter::new(randn(&[in_features, out_features])?);
        let bias = if bias {
            Some(Parameter::new(zeros(&[out_features])?))
        } else {
            None
        };

        let attention_weight = if use_attention {
            Some(Parameter::new(randn(&[out_features])?))
        } else {
            None
        };

        Ok(Self {
            in_features,
            out_features,
            weight,
            bias,
            use_attention,
            attention_weight,
            dropout,
        })
    }

    /// Forward pass through HGCN layer
    pub fn forward(&self, hypergraph: &HypergraphData) -> Result<HypergraphData> {
        // Simplified implementation for API compatibility
        // Step 1: Transform node features
        let node_features_transformed = hypergraph.x.matmul(&self.weight.clone_data())?;

        // Step 2: Simplified hypergraph convolution (skip complex aggregation for now)
        let output_features = if let Some(ref bias) = self.bias {
            node_features_transformed.add(&bias.clone_data())?
        } else {
            node_features_transformed
        };

        // Create output hypergraph with updated node features
        Ok(HypergraphData {
            x: output_features,
            incidence_matrix: hypergraph.incidence_matrix.clone(),
            hyperedge_weights: hypergraph.hyperedge_weights.clone(),
            hyperedge_features: hypergraph.hyperedge_features.clone(),
            node_degrees: hypergraph.node_degrees.clone(),
            hyperedge_cardinalities: hypergraph.hyperedge_cardinalities.clone(),
            num_nodes: hypergraph.num_nodes,
            num_hyperedges: hypergraph.num_hyperedges,
        })
    }

    /// Apply attention mechanism to hyperedge features
    fn apply_attention(
        &self,
        hyperedge_features: &Tensor,
        _hypergraph: &HypergraphData,
    ) -> Result<Tensor> {
        if let Some(ref attention_weight) = self.attention_weight {
            // Compute attention scores
            let attention_scores = hyperedge_features.matmul(&attention_weight.clone_data())?;
            let attention_probs = attention_scores.softmax(-1)?;

            // Apply attention to features
            let attention_expanded = attention_probs.unsqueeze(-1)?;
            Ok(hyperedge_features.mul(&attention_expanded)?)
        } else {
            Ok(hyperedge_features.clone())
        }
    }

    /// Normalize aggregated features by node degrees
    fn normalize_by_degrees(
        &self,
        features: &Tensor,
        hypergraph: &HypergraphData,
    ) -> Result<Tensor> {
        let degrees = &hypergraph.node_degrees;
        let epsilon = 1e-8;

        // Add epsilon to prevent division by zero
        let safe_degrees = degrees.add_scalar(epsilon)?;
        let inv_degrees = safe_degrees.reciprocal()?;

        // Expand inverse degrees to match feature dimensions
        // First squeeze to ensure we have shape [num_nodes] rather than [num_nodes, 1]
        let inv_degrees_squeezed = if inv_degrees.shape().dims().len() > 1 {
            inv_degrees.squeeze_tensor(1)?
        } else {
            inv_degrees
        };
        let inv_degrees_expanded = inv_degrees_squeezed.unsqueeze(-1)?;
        Ok(features.mul(&inv_degrees_expanded)?)
    }
}

impl GraphLayer for HGCNConv {
    fn forward(&self, graph: &GraphData) -> Result<GraphData> {
        // Convert regular graph to hypergraph and back for compatibility
        let hypergraph = graph_to_hypergraph(graph)?;
        let output_hypergraph = HGCNConv::forward(self, &hypergraph)?;
        output_hypergraph.to_graph_clique_expansion()
    }

    fn parameters(&self) -> Vec<Tensor> {
        let mut params = vec![self.weight.clone_data()];
        if let Some(ref bias) = self.bias {
            params.push(bias.clone_data());
        }
        if let Some(ref attention_weight) = self.attention_weight {
            params.push(attention_weight.clone_data());
        }
        params
    }
}

/// Hypergraph Attention Network (HyperGAT) layer
#[derive(Debug)]
pub struct HyperGATConv {
    in_features: usize,
    out_features: usize,
    heads: usize,
    query_weight: Parameter,
    key_weight: Parameter,
    value_weight: Parameter,
    hyperedge_attention: Parameter,
    output_weight: Parameter,
    bias: Option<Parameter>,
    dropout: f32,
}

impl HyperGATConv {
    /// Create a new HyperGAT layer
    pub fn new(
        in_features: usize,
        out_features: usize,
        heads: usize,
        dropout: f32,
        bias: bool,
    ) -> Result<Self> {
        let head_dim = out_features / heads;

        let query_weight = Parameter::new(randn(&[in_features, out_features])?);
        let key_weight = Parameter::new(randn(&[in_features, out_features])?);
        let value_weight = Parameter::new(randn(&[in_features, out_features])?);
        let hyperedge_attention = Parameter::new(randn(&[heads, 2 * head_dim])?);
        let output_weight = Parameter::new(randn(&[out_features, out_features])?);

        let bias = if bias {
            Some(Parameter::new(zeros(&[out_features])?))
        } else {
            None
        };

        Ok(Self {
            in_features,
            out_features,
            heads,
            query_weight,
            key_weight,
            value_weight,
            hyperedge_attention,
            output_weight,
            bias,
            dropout,
        })
    }

    /// Forward pass through HyperGAT layer
    pub fn forward(&self, hypergraph: &HypergraphData) -> Result<HypergraphData> {
        let num_nodes = hypergraph.num_nodes;
        let head_dim = self.out_features / self.heads;

        // Linear transformations
        let queries = hypergraph.x.matmul(&self.query_weight.clone_data())?;
        let keys = hypergraph.x.matmul(&self.key_weight.clone_data())?;
        let values = hypergraph.x.matmul(&self.value_weight.clone_data())?;

        // Reshape for multi-head attention
        let q = queries.view(&[num_nodes as i32, self.heads as i32, head_dim as i32])?;
        let k = keys.view(&[num_nodes as i32, self.heads as i32, head_dim as i32])?;
        let v = values.view(&[num_nodes as i32, self.heads as i32, head_dim as i32])?;

        // Perform hyperedge-based attention
        let attended_features = self.hyperedge_attention_mechanism(&q, &k, &v, hypergraph);

        // Reshape back and apply output transformation
        let concatenated =
            attended_features?.view(&[num_nodes as i32, self.out_features as i32])?;
        let mut output = concatenated.matmul(&self.output_weight.clone_data())?;

        // Add bias if present
        if let Some(ref bias) = self.bias {
            output = output.add(&bias.clone_data())?;
        }

        // Create output hypergraph
        Ok(HypergraphData {
            x: output,
            incidence_matrix: hypergraph.incidence_matrix.clone(),
            hyperedge_weights: hypergraph.hyperedge_weights.clone(),
            hyperedge_features: hypergraph.hyperedge_features.clone(),
            node_degrees: hypergraph.node_degrees.clone(),
            hyperedge_cardinalities: hypergraph.hyperedge_cardinalities.clone(),
            num_nodes: hypergraph.num_nodes,
            num_hyperedges: hypergraph.num_hyperedges,
        })
    }

    /// Hyperedge-based attention mechanism
    fn hyperedge_attention_mechanism(
        &self,
        q: &Tensor,
        k: &Tensor,
        v: &Tensor,
        hypergraph: &HypergraphData,
    ) -> Result<Tensor> {
        let num_nodes = hypergraph.num_nodes;
        let head_dim = self.out_features / self.heads;

        // Initialize output
        let mut output = zeros(&[num_nodes, self.heads, head_dim])?;

        let incidence_data = hypergraph.incidence_matrix.to_vec()?;

        // Process each hyperedge separately
        for e in 0..hypergraph.num_hyperedges {
            let mut nodes_in_hyperedge = Vec::new();

            // Find nodes in this hyperedge
            for v in 0..num_nodes {
                let idx = v * hypergraph.num_hyperedges + e;
                if incidence_data[idx] > 0.0 {
                    nodes_in_hyperedge.push(v);
                }
            }

            if nodes_in_hyperedge.len() < 2 {
                continue; // Skip hyperedges with less than 2 nodes
            }

            // Compute attention within hyperedge for each head
            for head in 0..self.heads {
                self.compute_hyperedge_attention(head, &nodes_in_hyperedge, q, k, v, &mut output)?;
            }
        }

        Ok(output)
    }

    /// Compute attention for a specific hyperedge and head
    fn compute_hyperedge_attention(
        &self,
        head: usize,
        nodes: &[usize],
        q: &Tensor,
        k: &Tensor,
        v: &Tensor,
        output: &mut Tensor,
    ) -> Result<()> {
        let head_dim = self.out_features / self.heads;
        let scale = 1.0 / (head_dim as f32).sqrt();

        // For simplicity, use mean pooling within hyperedge
        // In practice, this would use more sophisticated attention
        for &node_i in nodes {
            let mut aggregated = zeros(&[head_dim])?;
            let mut total_weight = 0.0;

            for &node_j in nodes {
                if node_i != node_j {
                    // Get query and key for these nodes and head
                    let q_i = q
                        .slice_tensor(0, node_i, node_i + 1)?
                        .slice_tensor(1, head, head + 1)?
                        .squeeze_tensor(0)?
                        .squeeze_tensor(0)?;

                    let k_j = k
                        .slice_tensor(0, node_j, node_j + 1)?
                        .slice_tensor(1, head, head + 1)?
                        .squeeze_tensor(0)?
                        .squeeze_tensor(0)?;

                    let v_j = v
                        .slice_tensor(0, node_j, node_j + 1)?
                        .slice_tensor(1, head, head + 1)?
                        .squeeze_tensor(0)?
                        .squeeze_tensor(0)?;

                    // Compute attention weight (simplified)
                    let attention_score = q_i.dot(&k_j)?.mul_scalar(scale)?;
                    let weight = attention_score.exp()?.item()?;

                    // Aggregate values
                    let weighted_value = v_j.mul_scalar(weight)?;
                    aggregated = aggregated.add(&weighted_value)?;
                    total_weight += weight;
                }
            }

            // Normalize and update output
            if total_weight > 0.0 {
                aggregated = aggregated.div_scalar(total_weight)?;

                // Update output tensor (simplified assignment)
                let aggregated_data = aggregated.to_vec()?;
                for (j, &val) in aggregated_data.iter().enumerate() {
                    output.set_item(&[node_i, head, j], val)?;
                }
            }
        }

        Ok(())
    }
}

impl GraphLayer for HyperGATConv {
    fn forward(&self, graph: &GraphData) -> Result<GraphData> {
        let hypergraph = graph_to_hypergraph(graph)?;
        let output_hypergraph = HyperGATConv::forward(self, &hypergraph)?;
        output_hypergraph.to_graph_clique_expansion()
    }

    fn parameters(&self) -> Vec<Tensor> {
        let mut params = vec![
            self.query_weight.clone_data(),
            self.key_weight.clone_data(),
            self.value_weight.clone_data(),
            self.hyperedge_attention.clone_data(),
            self.output_weight.clone_data(),
        ];

        if let Some(ref bias) = self.bias {
            params.push(bias.clone_data());
        }

        params
    }
}

/// Hypergraph Neural Network (HGNN) layer based on spectral methods
#[derive(Debug)]
pub struct HGNNConv {
    in_features: usize,
    out_features: usize,
    weight: Parameter,
    bias: Option<Parameter>,
    use_spectral: bool,
}

impl HGNNConv {
    /// Create a new HGNN layer
    pub fn new(
        in_features: usize,
        out_features: usize,
        bias: bool,
        use_spectral: bool,
    ) -> Result<Self> {
        let weight = Parameter::new(randn(&[in_features, out_features])?);
        let bias = if bias {
            Some(Parameter::new(zeros(&[out_features])?))
        } else {
            None
        };

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

    /// Forward pass through HGNN layer
    pub fn forward(&self, hypergraph: &HypergraphData) -> Result<HypergraphData> {
        // Transform node features
        let x_transformed = hypergraph.x.matmul(&self.weight.clone_data())?;

        // Compute hypergraph Laplacian and apply convolution
        let output_features = if self.use_spectral {
            self.spectral_convolution(&x_transformed, hypergraph)
        } else {
            self.spatial_convolution(&x_transformed, hypergraph)
        };

        // Add bias if present
        let output_features = output_features?;
        let final_features = if let Some(ref bias) = self.bias {
            output_features.add(&bias.clone_data())?
        } else {
            output_features
        };

        Ok(HypergraphData {
            x: final_features,
            incidence_matrix: hypergraph.incidence_matrix.clone(),
            hyperedge_weights: hypergraph.hyperedge_weights.clone(),
            hyperedge_features: hypergraph.hyperedge_features.clone(),
            node_degrees: hypergraph.node_degrees.clone(),
            hyperedge_cardinalities: hypergraph.hyperedge_cardinalities.clone(),
            num_nodes: hypergraph.num_nodes,
            num_hyperedges: hypergraph.num_hyperedges,
        })
    }

    /// Spectral convolution using hypergraph Laplacian
    fn spectral_convolution(
        &self,
        features: &Tensor,
        hypergraph: &HypergraphData,
    ) -> Result<Tensor> {
        // Compute normalized hypergraph Laplacian
        let laplacian = self.compute_hypergraph_laplacian(hypergraph);

        // Apply Laplacian: L @ X
        Ok(laplacian?.matmul(features)?)
    }

    /// Spatial convolution using incidence matrix
    fn spatial_convolution(
        &self,
        features: &Tensor,
        hypergraph: &HypergraphData,
    ) -> Result<Tensor> {
        // Node-to-hyperedge aggregation
        let incidence_t = hypergraph.incidence_matrix.transpose(0, 1)?;
        let hyperedge_features = incidence_t.matmul(features)?;

        // Hyperedge-to-node aggregation
        let aggregated = hypergraph.incidence_matrix.matmul(&hyperedge_features)?;

        // Normalize by node degrees
        self.normalize_by_degrees(&aggregated, hypergraph)
    }

    /// Compute normalized hypergraph Laplacian
    fn compute_hypergraph_laplacian(&self, hypergraph: &HypergraphData) -> Result<Tensor> {
        let h = &hypergraph.incidence_matrix;
        let num_nodes = hypergraph.num_nodes;

        // Compute degree matrices
        let node_degrees = h.sum_dim(&[1], false)?;
        let hyperedge_degrees = h.sum_dim(&[0], false)?;

        // Create diagonal degree matrices (simplified)
        let mut d_v = zeros(&[num_nodes, num_nodes])?;
        let mut d_e = zeros(&[hypergraph.num_hyperedges, hypergraph.num_hyperedges])?;

        let node_deg_data = node_degrees.to_vec()?;
        let hyperedge_deg_data = hyperedge_degrees.to_vec()?;

        // Fill diagonal matrices
        for i in 0..num_nodes {
            let degree = node_deg_data[i].max(1e-8); // Avoid division by zero
            d_v.set_item(&[i, i], degree.powf(-0.5))?;
        }

        for i in 0..hypergraph.num_hyperedges {
            let degree = hyperedge_deg_data[i].max(1e-8);
            d_e.set_item(&[i, i], degree.recip())?;
        }

        // Compute normalized Laplacian: I - D_v^{-1/2} H D_e H^T D_v^{-1/2}
        let h_t = h.transpose(0, 1)?;
        let intermediate = d_v.matmul(h)?.matmul(&d_e)?.matmul(&h_t)?.matmul(&d_v)?;

        let identity = eye(num_nodes);
        Ok(identity?.sub(&intermediate)?)
    }

    /// Normalize features by node degrees
    fn normalize_by_degrees(
        &self,
        features: &Tensor,
        hypergraph: &HypergraphData,
    ) -> Result<Tensor> {
        let degrees = &hypergraph.node_degrees;
        let epsilon = 1e-8;

        let safe_degrees = degrees.add_scalar(epsilon)?;
        let inv_sqrt_degrees = safe_degrees.pow_scalar(-0.5)?;

        // First squeeze to ensure we have shape [num_nodes] rather than [num_nodes, 1]
        let inv_degrees_squeezed = if inv_sqrt_degrees.shape().dims().len() > 1 {
            inv_sqrt_degrees.squeeze_tensor(1)?
        } else {
            inv_sqrt_degrees
        };
        let inv_degrees_expanded = inv_degrees_squeezed.unsqueeze(-1)?;

        Ok(features.mul(&inv_degrees_expanded)?)
    }
}

impl GraphLayer for HGNNConv {
    fn forward(&self, graph: &GraphData) -> Result<GraphData> {
        let hypergraph = graph_to_hypergraph(graph)?;
        let output_hypergraph = HGNNConv::forward(self, &hypergraph)?;
        output_hypergraph.to_graph_clique_expansion()
    }

    fn parameters(&self) -> Vec<Tensor> {
        let mut params = vec![self.weight.clone_data()];
        if let Some(ref bias) = self.bias {
            params.push(bias.clone_data());
        }
        params
    }
}

/// Hypergraph pooling operations
pub mod pooling {
    use super::*;

    /// Global hypergraph pooling
    pub fn global_hypergraph_pool(
        hypergraph: &HypergraphData,
        method: PoolingMethod,
    ) -> Result<Tensor> {
        match method {
            PoolingMethod::Mean => Ok(hypergraph.x.mean(Some(&[0]), false)?),
            PoolingMethod::Max => Ok(hypergraph.x.max(Some(0), false)?),
            PoolingMethod::Sum => Ok(hypergraph.x.sum_dim(&[0], false)?),
            PoolingMethod::Attention => attention_pool(hypergraph),
        }
    }

    /// Hyperedge-aware pooling
    pub fn hyperedge_pool(hypergraph: &HypergraphData, method: PoolingMethod) -> Result<Tensor> {
        let incidence_t = hypergraph.incidence_matrix.transpose(0, 1)?;

        match method {
            PoolingMethod::Mean => {
                // Average pooling over hyperedges
                let hyperedge_features = incidence_t.matmul(&hypergraph.x)?;
                Ok(hyperedge_features.mean(Some(&[0]), false)?)
            }
            PoolingMethod::Max => {
                let hyperedge_features = incidence_t.matmul(&hypergraph.x)?;
                Ok(hyperedge_features.max(Some(0), false)?)
            }
            PoolingMethod::Sum => {
                let hyperedge_features = incidence_t.matmul(&hypergraph.x)?;
                Ok(hyperedge_features.sum_dim(&[0], false)?)
            }
            PoolingMethod::Attention => {
                // Attention over hyperedges
                attention_pool(hypergraph)
            }
        }
    }

    /// Hierarchical hypergraph pooling
    pub fn hierarchical_hypergraph_pool(
        hypergraph: &HypergraphData,
        num_clusters: usize,
    ) -> Result<HypergraphData> {
        // Simplified clustering-based pooling
        let cluster_assignments = cluster_nodes(hypergraph, num_clusters);
        coarsen_hypergraph(hypergraph, &cluster_assignments)
    }

    /// Attention-based pooling
    fn attention_pool(hypergraph: &HypergraphData) -> Result<Tensor> {
        // Simplified attention pooling
        let attention_scores = hypergraph.x.sum_dim(&[1], false)?;
        let attention_weights = attention_scores.softmax(0)?;
        let attention_expanded = attention_weights.unsqueeze(-1)?;

        let weighted_features = hypergraph.x.mul(&attention_expanded)?;
        Ok(weighted_features.sum_dim(&[0], false)?)
    }

    /// Simple node clustering for hierarchical pooling
    fn cluster_nodes(hypergraph: &HypergraphData, num_clusters: usize) -> Vec<usize> {
        let num_nodes = hypergraph.num_nodes;
        let mut assignments = vec![0; num_nodes];

        // Simple clustering by node index (for demonstration)
        for i in 0..num_nodes {
            assignments[i] = i % num_clusters;
        }

        assignments
    }

    /// Coarsen hypergraph based on cluster assignments
    fn coarsen_hypergraph(
        hypergraph: &HypergraphData,
        cluster_assignments: &[usize],
    ) -> Result<HypergraphData> {
        let num_clusters = cluster_assignments.iter().max().copied().unwrap_or(0) + 1;
        let original_features = hypergraph.x.shape().dims()[1];

        // Average node features within clusters (simplified implementation)
        let mut coarse_features_data = vec![0.0; num_clusters * original_features];
        let mut cluster_counts = vec![0; num_clusters];

        let node_data = hypergraph.x.to_vec()?;

        for (node, &cluster) in cluster_assignments.iter().enumerate() {
            cluster_counts[cluster] += 1;
            for feat in 0..original_features {
                let node_feat_idx = node * original_features + feat;
                let cluster_feat_idx = cluster * original_features + feat;
                coarse_features_data[cluster_feat_idx] += node_data[node_feat_idx];
            }
        }

        // Normalize by cluster size
        for cluster in 0..num_clusters {
            if cluster_counts[cluster] > 0 {
                for feat in 0..original_features {
                    let cluster_feat_idx = cluster * original_features + feat;
                    coarse_features_data[cluster_feat_idx] /= cluster_counts[cluster] as f32;
                }
            }
        }

        let coarse_features = from_vec(
            coarse_features_data,
            &[num_clusters, original_features],
            torsh_core::device::DeviceType::Cpu,
        )?;

        // Create coarse incidence matrix (simplified)
        let coarse_incidence = zeros(&[num_clusters, hypergraph.num_hyperedges])?;

        HypergraphData::new(coarse_features, coarse_incidence)
    }

    /// Pooling methods
    #[derive(Debug, Clone, Copy)]
    pub enum PoolingMethod {
        Mean,
        Max,
        Sum,
        Attention,
    }
}

/// Utility functions for hypergraph operations
pub mod utils {
    use super::*;

    /// Convert edge list to hypergraph
    pub fn edge_list_to_hypergraph(
        edges: &[(Vec<usize>, f32)],
        num_nodes: usize,
    ) -> Result<HypergraphData> {
        let num_hyperedges = edges.len();
        let mut incidence_data = vec![0.0; num_nodes * num_hyperedges];
        let mut weights = Vec::new();

        for (e, (edge_nodes, weight)) in edges.iter().enumerate() {
            weights.push(*weight);
            for &node in edge_nodes {
                if node < num_nodes {
                    incidence_data[node * num_hyperedges + e] = 1.0;
                }
            }
        }

        let features = randn(&[num_nodes, 16])?; // Default features
        let incidence_matrix = from_vec(
            incidence_data,
            &[num_nodes, num_hyperedges],
            torsh_core::device::DeviceType::Cpu,
        )?;

        let hyperedge_weights = from_vec(
            weights,
            &[num_hyperedges],
            torsh_core::device::DeviceType::Cpu,
        )?;

        Ok(HypergraphData::new(features, incidence_matrix)?
            .with_hyperedge_weights(hyperedge_weights))
    }

    /// Generate random hypergraph
    pub fn random_hypergraph(
        num_nodes: usize,
        num_hyperedges: usize,
        edge_prob: f32,
        features_dim: usize,
    ) -> Result<HypergraphData> {
        let mut rng = scirs2_core::random::thread_rng();
        let mut incidence_data = vec![0.0; num_nodes * num_hyperedges];

        // Generate random hyperedges
        for e in 0..num_hyperedges {
            for v in 0..num_nodes {
                if rng.gen_range(0.0..1.0) < edge_prob {
                    incidence_data[v * num_hyperedges + e] = 1.0;
                }
            }
        }

        let features = randn(&[num_nodes, features_dim])?;
        let incidence_matrix = from_vec(
            incidence_data,
            &[num_nodes, num_hyperedges],
            torsh_core::device::DeviceType::Cpu,
        )?;

        HypergraphData::new(features, incidence_matrix)
    }

    /// Hypergraph metrics
    pub fn hypergraph_metrics(hypergraph: &HypergraphData) -> Result<HypergraphMetrics> {
        let node_degrees = hypergraph.node_degrees.to_vec()?;
        let hyperedge_cardinalities = hypergraph.hyperedge_cardinalities.to_vec()?;

        let avg_node_degree = node_degrees.iter().sum::<f32>() / node_degrees.len() as f32;
        let avg_hyperedge_size =
            hyperedge_cardinalities.iter().sum::<f32>() / hyperedge_cardinalities.len() as f32;

        let density = node_degrees.iter().sum::<f32>()
            / (hypergraph.num_nodes * hypergraph.num_hyperedges) as f32;

        Ok(HypergraphMetrics {
            avg_node_degree,
            avg_hyperedge_size,
            density,
            num_nodes: hypergraph.num_nodes,
            num_hyperedges: hypergraph.num_hyperedges,
        })
    }

    /// Hypergraph statistics
    #[derive(Debug, Clone)]
    pub struct HypergraphMetrics {
        pub avg_node_degree: f32,
        pub avg_hyperedge_size: f32,
        pub density: f32,
        pub num_nodes: usize,
        pub num_hyperedges: usize,
    }
}

/// Convert regular graph to hypergraph (each edge becomes a hyperedge)
pub fn graph_to_hypergraph(graph: &GraphData) -> Result<HypergraphData> {
    let edge_data = crate::utils::tensor_to_vec2::<f32>(&graph.edge_index)?;
    let num_edges = edge_data[0].len();
    let num_nodes = graph.num_nodes;

    // Each edge becomes a hyperedge connecting two nodes
    let mut incidence_data = vec![0.0; num_nodes * num_edges];

    for e in 0..num_edges {
        let src = edge_data[0][e] as usize;
        let dst = edge_data[1][e] as usize;

        if src < num_nodes && dst < num_nodes {
            incidence_data[src * num_edges + e] = 1.0;
            incidence_data[dst * num_edges + e] = 1.0;
        }
    }

    let incidence_matrix = from_vec(
        incidence_data,
        &[num_nodes, num_edges],
        torsh_core::device::DeviceType::Cpu,
    )?;

    HypergraphData::new(graph.x.clone(), incidence_matrix)
}

/// Create identity matrix
fn eye(n: usize) -> Result<Tensor> {
    let mut data = vec![0.0; n * n];
    for i in 0..n {
        data[i * n + i] = 1.0;
    }
    Ok(from_vec(
        data,
        &[n, n],
        torsh_core::device::DeviceType::Cpu,
    )?)
}

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

    #[test]
    fn test_hypergraph_creation() {
        let features = randn(&[4, 3]).unwrap();
        let incidence_data = vec![
            1.0, 0.0, 1.0, // Node 0 in hyperedges 0 and 2
            1.0, 1.0, 0.0, // Node 1 in hyperedges 0 and 1
            0.0, 1.0, 1.0, // Node 2 in hyperedges 1 and 2
            0.0, 0.0, 1.0, // Node 3 in hyperedge 2
        ];
        let incidence_matrix = from_vec(incidence_data, &[4, 3], DeviceType::Cpu).unwrap();

        let hypergraph =
            HypergraphData::new(features, incidence_matrix).expect("operation should succeed");

        assert_eq!(hypergraph.num_nodes, 4);
        assert_eq!(hypergraph.num_hyperedges, 3);
        assert_eq!(hypergraph.x.shape().dims(), &[4, 3]);
        assert_eq!(hypergraph.incidence_matrix.shape().dims(), &[4, 3]);
    }

    #[test]
    fn test_hgcn_layer() {
        let features = randn(&[3, 4]).unwrap();
        let incidence_matrix =
            from_vec(vec![1.0, 0.0, 1.0, 1.0, 0.0, 1.0], &[3, 2], DeviceType::Cpu).unwrap();
        let hypergraph =
            HypergraphData::new(features, incidence_matrix).expect("operation should succeed");

        let hgcn = HGCNConv::new(4, 8, true, false, 0.1);
        let output = hgcn
            .expect("operation should succeed")
            .forward(&hypergraph)
            .expect("operation should succeed");

        assert_eq!(output.x.shape().dims(), &[3, 8]);
        assert_eq!(output.num_nodes, 3);
        assert_eq!(output.num_hyperedges, 2);
    }

    #[test]
    fn test_hypergraph_to_graph_conversion() {
        let features = randn(&[3, 4]).unwrap();
        let incidence_matrix =
            from_vec(vec![1.0, 0.0, 1.0, 1.0, 0.0, 1.0], &[3, 2], DeviceType::Cpu).unwrap();
        let hypergraph =
            HypergraphData::new(features, incidence_matrix).expect("operation should succeed");

        let graph = hypergraph
            .to_graph_clique_expansion()
            .expect("operation should succeed");
        assert_eq!(graph.num_nodes, 3);

        let star_graph = hypergraph
            .to_graph_star_expansion()
            .expect("operation should succeed");
        assert_eq!(star_graph.num_nodes, 5); // 3 original + 2 virtual nodes
    }

    #[test]
    fn test_hypergraph_pooling() {
        let features = randn(&[4, 6]).unwrap();
        let incidence_matrix = from_vec(
            vec![1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0],
            &[4, 2],
            DeviceType::Cpu,
        )
        .unwrap();
        let hypergraph =
            HypergraphData::new(features, incidence_matrix).expect("operation should succeed");

        let pooled_mean =
            pooling::global_hypergraph_pool(&hypergraph, pooling::PoolingMethod::Mean);
        assert_eq!(
            pooled_mean
                .expect("operation should succeed")
                .shape()
                .dims(),
            &[6]
        );

        let pooled_max = pooling::global_hypergraph_pool(&hypergraph, pooling::PoolingMethod::Max);
        assert_eq!(
            pooled_max.expect("operation should succeed").shape().dims(),
            &[6]
        );
    }

    #[test]
    fn test_hypergraph_utils() {
        let edges = vec![
            (vec![0, 1, 2], 1.0),
            (vec![1, 3], 0.8),
            (vec![0, 2, 3], 1.2),
        ];

        let hypergraph =
            utils::edge_list_to_hypergraph(&edges, 4).expect("operation should succeed");
        assert_eq!(hypergraph.num_nodes, 4);
        assert_eq!(hypergraph.num_hyperedges, 3);

        let metrics = utils::hypergraph_metrics(&hypergraph).expect("operation should succeed");
        assert!(metrics.avg_node_degree > 0.0);
        assert!(metrics.avg_hyperedge_size > 0.0);
    }

    #[test]
    fn test_random_hypergraph_generation() {
        let hypergraph = utils::random_hypergraph(5, 3, 0.6, 8).expect("operation should succeed");
        assert_eq!(hypergraph.num_nodes, 5);
        assert_eq!(hypergraph.num_hyperedges, 3);
        assert_eq!(hypergraph.x.shape().dims(), &[5, 8]);
    }

    #[test]
    fn test_hypergat_layer() {
        let features = randn(&[4, 6]).unwrap();
        let incidence_matrix = from_vec(
            vec![1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0],
            &[4, 2],
            DeviceType::Cpu,
        )
        .unwrap();
        let hypergraph =
            HypergraphData::new(features, incidence_matrix).expect("operation should succeed");

        let hypergat = HyperGATConv::new(6, 12, 3, 0.1, true);
        let output = hypergat
            .expect("operation should succeed")
            .forward(&hypergraph)
            .expect("operation should succeed");

        assert_eq!(output.x.shape().dims(), &[4, 12]);
        assert_eq!(output.num_nodes, 4);
    }
}