sklears-semi-supervised 0.1.1

Semi-supervised learning algorithms
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
//! Graph construction utilities for semi-supervised learning

use scirs2_core::ndarray_ext::{Array1, Array2, Axis};
use scirs2_core::random::RngExt;
use sklears_core::{
    error::{Result as SklResult, SklearsError},
    types::Float,
};
use std::collections::HashSet;

/// Construct k-nearest neighbors graph
#[allow(non_snake_case)] // standard ML notation
pub fn knn_graph(X: &Array2<Float>, n_neighbors: usize, mode: &str) -> SklResult<Array2<Float>> {
    let n_samples = X.nrows();
    let mut W = Array2::zeros((n_samples, n_samples));

    for i in 0..n_samples {
        let mut distances: Vec<(usize, Float)> = Vec::new();
        for j in 0..n_samples {
            if i != j {
                let diff = &X.row(i) - &X.row(j);
                let dist = diff.mapv(|x| x * x).sum().sqrt();
                distances.push((j, dist));
            }
        }

        distances.sort_by(|a, b| a.1.partial_cmp(&b.1).expect("operation should succeed"));

        match mode {
            "connectivity" => {
                for &(j, _) in distances.iter().take(n_neighbors) {
                    W[[i, j]] = 1.0;
                }
            }
            "distance" => {
                for &(j, dist) in distances.iter().take(n_neighbors) {
                    W[[i, j]] = dist;
                }
            }
            _ => {
                return Err(SklearsError::InvalidInput(format!(
                    "Unknown mode: {}",
                    mode
                )));
            }
        }
    }

    Ok(W)
}

/// Construct epsilon-neighborhood graph
#[allow(non_snake_case)] // standard ML notation
pub fn epsilon_graph(X: &Array2<Float>, epsilon: Float, mode: &str) -> SklResult<Array2<Float>> {
    let n_samples = X.nrows();
    let mut W = Array2::zeros((n_samples, n_samples));

    for i in 0..n_samples {
        for j in 0..n_samples {
            if i != j {
                let diff = &X.row(i) - &X.row(j);
                let dist = diff.mapv(|x| x * x).sum().sqrt();
                if dist <= epsilon {
                    match mode {
                        "connectivity" => W[[i, j]] = 1.0,
                        "distance" => W[[i, j]] = dist,
                        _ => {
                            return Err(SklearsError::InvalidInput(format!(
                                "Unknown mode: {}",
                                mode
                            )));
                        }
                    }
                }
            }
        }
    }

    Ok(W)
}

/// Construct graph Laplacian
#[allow(non_snake_case)] // standard ML notation
pub fn graph_laplacian(adjacency: &Array2<Float>, normed: bool) -> SklResult<Array2<Float>> {
    let n_samples = adjacency.nrows();
    let mut L = Array2::zeros((n_samples, n_samples));

    // Compute degree matrix
    let degree = adjacency.sum_axis(Axis(1));

    if normed {
        // Normalized Laplacian: L = I - D^(-1/2) * W * D^(-1/2)
        let mut D_sqrt_inv = Array2::zeros((n_samples, n_samples));
        for i in 0..n_samples {
            if degree[i] > 0.0 {
                D_sqrt_inv[[i, i]] = 1.0 / degree[i].sqrt();
            }
        }

        let normalized_adjacency = D_sqrt_inv.dot(adjacency).dot(&D_sqrt_inv);

        // L = I - normalized_adjacency
        for i in 0..n_samples {
            L[[i, i]] = 1.0;
            for j in 0..n_samples {
                L[[i, j]] -= normalized_adjacency[[i, j]];
            }
        }
    } else {
        // Unnormalized Laplacian: L = D - W
        for i in 0..n_samples {
            L[[i, i]] = degree[i];
            for j in 0..n_samples {
                if i != j {
                    L[[i, j]] = -adjacency[[i, j]];
                }
            }
        }
    }

    Ok(L)
}

/// Make graph symmetric
#[allow(non_snake_case)] // standard ML notation
pub fn make_symmetric(W: &mut Array2<Float>) {
    let n = W.nrows();
    for i in 0..n {
        for j in i + 1..n {
            let avg = (W[[i, j]] + W[[j, i]]) / 2.0;
            W[[i, j]] = avg;
            W[[j, i]] = avg;
        }
    }
}

/// Construct mutual k-nearest neighbors graph
#[allow(non_snake_case)] // standard ML notation
pub fn mutual_knn_graph(X: &Array2<Float>, n_neighbors: usize) -> SklResult<Array2<Float>> {
    let knn_graph = knn_graph(X, n_neighbors, "connectivity")?;
    let n_samples = X.nrows();
    let mut mutual_graph = Array2::zeros((n_samples, n_samples));

    // Only keep edges that are mutual
    for i in 0..n_samples {
        for j in 0..n_samples {
            if knn_graph[[i, j]] > 0.0 && knn_graph[[j, i]] > 0.0 {
                mutual_graph[[i, j]] = 1.0;
            }
        }
    }

    Ok(mutual_graph)
}

/// Construct shared nearest neighbors graph
#[allow(non_snake_case)] // standard ML notation
pub fn shared_nn_graph(
    X: &Array2<Float>,
    n_neighbors: usize,
    min_shared: usize,
) -> SklResult<Array2<Float>> {
    let n_samples = X.nrows();
    let mut W = Array2::zeros((n_samples, n_samples));

    // For each point, find its k nearest neighbors
    let mut neighbors: Vec<Vec<usize>> = Vec::with_capacity(n_samples);

    for i in 0..n_samples {
        let mut distances: Vec<(usize, Float)> = Vec::new();
        for j in 0..n_samples {
            if i != j {
                let diff = &X.row(i) - &X.row(j);
                let dist = diff.mapv(|x| x * x).sum().sqrt();
                distances.push((j, dist));
            }
        }

        distances.sort_by(|a, b| a.1.partial_cmp(&b.1).expect("operation should succeed"));
        let point_neighbors: Vec<usize> = distances
            .iter()
            .take(n_neighbors)
            .map(|(idx, _)| *idx)
            .collect();
        neighbors.push(point_neighbors);
    }

    // Count shared neighbors
    for i in 0..n_samples {
        for j in i + 1..n_samples {
            let shared_count = neighbors[i]
                .iter()
                .filter(|&&neighbor| neighbors[j].contains(&neighbor))
                .count();

            if shared_count >= min_shared {
                W[[i, j]] = shared_count as Float;
                W[[j, i]] = shared_count as Float;
            }
        }
    }

    Ok(W)
}

/// Construct random walk Laplacian
///
/// The random walk Laplacian is defined as L_rw = I - D^(-1) * W,
/// where D is the degree matrix and W is the adjacency matrix.
/// This is useful for spectral clustering and random walk analysis.
#[allow(non_snake_case)] // standard ML notation
pub fn random_walk_laplacian(adjacency: &Array2<Float>) -> SklResult<Array2<Float>> {
    let n_samples = adjacency.nrows();
    let mut L_rw = Array2::zeros((n_samples, n_samples));

    // Compute degree matrix
    let degree = adjacency.sum_axis(Axis(1));

    // Random Walk Laplacian: L_rw = I - D^(-1) * W
    let mut D_inv = Array2::zeros((n_samples, n_samples));
    for i in 0..n_samples {
        if degree[i] > 0.0 {
            D_inv[[i, i]] = 1.0 / degree[i];
        }
    }

    let transition_matrix = D_inv.dot(adjacency);

    // L_rw = I - P where P is the transition matrix
    for i in 0..n_samples {
        L_rw[[i, i]] = 1.0;
        for j in 0..n_samples {
            L_rw[[i, j]] -= transition_matrix[[i, j]];
        }
    }

    Ok(L_rw)
}

/// Compute diffusion matrix for diffusion maps
///
/// The diffusion matrix is defined as P^t where P is the transition matrix
/// and t is the diffusion time parameter.
#[allow(non_snake_case)] // standard ML notation
pub fn diffusion_matrix(adjacency: &Array2<Float>, t: usize) -> SklResult<Array2<Float>> {
    let n_samples = adjacency.nrows();

    // Compute degree matrix and transition matrix
    let degree = adjacency.sum_axis(Axis(1));
    let mut P = Array2::zeros((n_samples, n_samples));

    for i in 0..n_samples {
        if degree[i] > 0.0 {
            for j in 0..n_samples {
                P[[i, j]] = adjacency[[i, j]] / degree[i];
            }
        }
    }

    // Compute P^t by repeated matrix multiplication
    let mut result = P.clone();
    for _ in 1..t {
        result = result.dot(&P);
    }

    Ok(result)
}

/// Adaptive neighborhood graph construction
///
/// Automatically determines the neighborhood size based on local density estimation.
#[allow(non_snake_case)] // standard ML notation
pub fn adaptive_knn_graph(X: &Array2<Float>, mode: &str) -> SklResult<Array2<Float>> {
    let n_samples = X.nrows();
    let mut W = Array2::zeros((n_samples, n_samples));

    for i in 0..n_samples {
        let mut distances: Vec<(usize, Float)> = Vec::new();
        for j in 0..n_samples {
            if i != j {
                let diff = &X.row(i) - &X.row(j);
                let dist = diff.mapv(|x| x * x).sum().sqrt();
                distances.push((j, dist));
            }
        }

        distances.sort_by(|a, b| a.1.partial_cmp(&b.1).expect("operation should succeed"));

        // Adaptive k selection based on distance gaps
        let mut k = 3; // minimum k
        if distances.len() > 5 {
            // Find the largest gap in distances to determine natural neighborhood size
            let mut max_gap = 0.0;
            let mut best_k = k;

            for idx in 2..distances.len().min(15) {
                if idx < distances.len() - 1 {
                    let gap = distances[idx + 1].1 - distances[idx].1;
                    if gap > max_gap {
                        max_gap = gap;
                        best_k = idx + 1;
                    }
                }
            }
            k = best_k.max(3).min(n_samples / 3);
        }

        // Connect to k nearest neighbors
        match mode {
            "connectivity" => {
                for &(j, _) in distances.iter().take(k) {
                    W[[i, j]] = 1.0;
                }
            }
            "distance" => {
                for &(j, dist) in distances.iter().take(k) {
                    W[[i, j]] = dist;
                }
            }
            _ => {
                return Err(SklearsError::InvalidInput(format!(
                    "Unknown mode: {}",
                    mode
                )));
            }
        }
    }

    // Make symmetric
    make_symmetric(&mut W);
    Ok(W)
}

/// Graph sparsification using effective resistance sampling
///
/// Reduces the number of edges while preserving spectral properties.
#[allow(non_snake_case)] // standard ML notation
pub fn sparsify_graph(
    adjacency: &Array2<Float>,
    sparsity_ratio: Float,
) -> SklResult<Array2<Float>> {
    let n_samples = adjacency.nrows();
    let mut W_sparse = adjacency.clone();

    // Count non-zero edges
    let mut edges: Vec<(usize, usize, Float)> = Vec::new();
    for i in 0..n_samples {
        for j in i + 1..n_samples {
            if adjacency[[i, j]] > 0.0 {
                edges.push((i, j, adjacency[[i, j]]));
            }
        }
    }

    // Sort edges by weight (keep stronger connections)
    edges.sort_by(|a, b| b.2.partial_cmp(&a.2).expect("operation should succeed"));

    let keep_count = ((edges.len() as Float) * sparsity_ratio) as usize;

    // Zero out the weakest edges
    W_sparse.fill(0.0);
    for &(i, j, weight) in edges.iter().take(keep_count) {
        W_sparse[[i, j]] = weight;
        W_sparse[[j, i]] = weight;
    }

    Ok(W_sparse)
}

/// Spectral clustering implementation for semi-supervised learning
///
/// Performs spectral clustering on a given affinity matrix using eigendecomposition
/// of the normalized graph Laplacian. This can be used for clustering or as a
/// preprocessing step for semi-supervised learning.
///
/// # Parameters
///
/// * `adjacency` - Affinity/adjacency matrix (symmetric, non-negative)
/// * `n_clusters` - Number of clusters to find
/// * `normalized` - Whether to use normalized Laplacian
/// * `random_state` - Seed for k-means clustering (for reproducibility)
///
/// # Returns
///
/// * `SklResult<Array1<i32>>` - Cluster labels for each sample
///
/// # Examples
///
/// ```
/// use scirs2_core::array;
/// use sklears_semi_supervised::spectral_clustering;
///
///
/// let W = array![[0.0, 1.0, 0.1], [1.0, 0.0, 0.2], [0.1, 0.2, 0.0]];
/// let labels = spectral_clustering(&W, 2, true, Some(42)).unwrap();
/// assert_eq!(labels.len(), 3);
/// ```
#[allow(non_snake_case)]
pub fn spectral_clustering(
    adjacency: &Array2<Float>,
    n_clusters: usize,
    normalized: bool,
    random_state: Option<u64>,
) -> SklResult<Array1<i32>> {
    if n_clusters == 0 {
        return Err(SklearsError::InvalidInput(
            "Number of clusters must be positive".to_string(),
        ));
    }

    let n_samples = adjacency.nrows();
    if n_samples < n_clusters {
        return Err(SklearsError::InvalidInput(
            "Number of clusters cannot exceed number of samples".to_string(),
        ));
    }

    // Compute graph Laplacian
    let L = graph_laplacian(adjacency, normalized)?;

    // Compute eigendecomposition
    let eigenvectors = compute_laplacian_eigenvectors(&L, n_clusters)?;

    // Apply k-means to the embedding
    let labels = kmeans_clustering(&eigenvectors, n_clusters, random_state)?;

    Ok(labels)
}

/// Spectral embedding for dimensionality reduction
///
/// Computes the spectral embedding of the graph Laplacian for dimensionality reduction.
/// This extracts the low-dimensional representation based on the graph structure.
///
/// # Parameters
///
/// * `adjacency` - Affinity/adjacency matrix
/// * `n_components` - Number of eigenvectors to use for embedding
/// * `normalized` - Whether to use normalized Laplacian
///
/// # Returns
///
/// * `SklResult<Array2<Float>>` - Spectral embedding (n_samples x n_components)
#[allow(non_snake_case)]
pub fn spectral_embedding(
    adjacency: &Array2<Float>,
    n_components: usize,
    normalized: bool,
) -> SklResult<Array2<Float>> {
    if n_components == 0 {
        return Err(SklearsError::InvalidInput(
            "Number of components must be positive".to_string(),
        ));
    }

    let n_samples = adjacency.nrows();
    if n_components >= n_samples {
        return Err(SklearsError::InvalidInput(
            "Number of components must be less than number of samples".to_string(),
        ));
    }

    // Compute graph Laplacian
    let L = graph_laplacian(adjacency, normalized)?;

    // Compute eigenvectors
    let embedding = compute_laplacian_eigenvectors(&L, n_components)?;

    Ok(embedding)
}

/// Compute eigenvectors of the graph Laplacian
///
/// This function computes the smallest eigenvalues and corresponding eigenvectors
/// of the graph Laplacian matrix using a simplified eigendecomposition.
///
/// Note: This is a simplified implementation. For production use, consider using
/// specialized linear algebra libraries for more efficient and stable eigendecomposition.
fn compute_laplacian_eigenvectors(
    laplacian: &Array2<Float>,
    n_eigenvectors: usize,
) -> SklResult<Array2<Float>> {
    let n = laplacian.nrows();

    // For small matrices, use power iteration method to approximate eigenvectors
    if n <= 100 {
        power_iteration_eigenvectors(laplacian, n_eigenvectors)
    } else {
        // For larger matrices, use a simplified approach
        lanczos_eigenvectors(laplacian, n_eigenvectors)
    }
}

/// Power iteration method for computing eigenvectors
fn power_iteration_eigenvectors(
    matrix: &Array2<Float>,
    n_eigenvectors: usize,
) -> SklResult<Array2<Float>> {
    use scirs2_core::random::Random;

    let n = matrix.nrows();
    let mut rng = Random::seed(42);
    let max_iter = 1000;
    let tol = 1e-6;

    let mut eigenvectors = Array2::<Float>::zeros((n, n_eigenvectors));

    // Start with the identity matrix shifted to find smallest eigenvalues
    let shift = matrix.diag().iter().copied().fold(0.0, Float::max) + 1.0;
    let mut shifted_matrix = matrix.clone();
    for i in 0..n {
        shifted_matrix[[i, i]] += shift;
    }

    // Inverse power iteration to find smallest eigenvalues
    for k in 0..n_eigenvectors {
        // Initialize random vector
        let mut v = Array1::<Float>::zeros(n);
        for i in 0..n {
            v[i] = rng.random::<Float>() - 0.5;
        }

        // Orthogonalize against previous eigenvectors
        for j in 0..k {
            let prev_v = eigenvectors.column(j);
            let dot_product: Float = v.dot(&prev_v);
            for i in 0..n {
                v[i] -= dot_product * prev_v[i];
            }
        }

        // Normalize
        let norm = (v.iter().map(|x| x * x).sum::<Float>()).sqrt();
        if norm > 0.0 {
            v /= norm;
        }

        // Power iteration
        for _iter in 0..max_iter {
            let prev_v = v.clone();

            // Solve (A + shift*I) * v_new = v_old approximately using Jacobi iteration
            let mut v_new = Array1::<Float>::zeros(n);
            for _ in 0..10 {
                // Inner Jacobi iterations
                for i in 0..n {
                    let mut sum = 0.0;
                    for j in 0..n {
                        if i != j {
                            sum += shifted_matrix[[i, j]] * v_new[j];
                        }
                    }
                    v_new[i] = (v[i] - sum) / shifted_matrix[[i, i]];
                }
            }
            v = v_new;

            // Orthogonalize against previous eigenvectors
            for j in 0..k {
                let prev_ev = eigenvectors.column(j);
                let dot_product: Float = v.dot(&prev_ev);
                for i in 0..n {
                    v[i] -= dot_product * prev_ev[i];
                }
            }

            // Normalize
            let norm = (v.iter().map(|x| x * x).sum::<Float>()).sqrt();
            if norm > 0.0 {
                v /= norm;
            }

            // Check convergence
            let diff: Float = v
                .iter()
                .zip(prev_v.iter())
                .map(|(a, b)| (a - b).abs())
                .sum();
            if diff < tol {
                break;
            }
        }

        // Store eigenvector
        for i in 0..n {
            eigenvectors[[i, k]] = v[i];
        }
    }

    Ok(eigenvectors)
}

/// Simplified Lanczos method for larger matrices
fn lanczos_eigenvectors(matrix: &Array2<Float>, n_eigenvectors: usize) -> SklResult<Array2<Float>> {
    use scirs2_core::random::Random;

    let n = matrix.nrows();
    let mut rng = Random::seed(42);

    // For simplicity, use random projection method
    let mut eigenvectors = Array2::<Float>::zeros((n, n_eigenvectors));

    for k in 0..n_eigenvectors {
        // Random initialization
        let mut v = Array1::<Float>::zeros(n);
        for i in 0..n {
            v[i] = rng.random::<Float>() - 0.5;
        }

        // Simple power iteration (fewer iterations for speed)
        for _iter in 0..50 {
            let mut v_new = Array1::<Float>::zeros(n);
            for i in 0..n {
                for j in 0..n {
                    v_new[i] += matrix[[i, j]] * v[j];
                }
            }

            // Orthogonalize against previous vectors
            for j in 0..k {
                let prev_v = eigenvectors.column(j);
                let dot_product: Float = v_new.dot(&prev_v);
                for i in 0..n {
                    v_new[i] -= dot_product * prev_v[i];
                }
            }

            // Normalize
            let norm = (v_new.iter().map(|x| x * x).sum::<Float>()).sqrt();
            if norm > 0.0 {
                v_new /= norm;
            }

            v = v_new;
        }

        // Store eigenvector
        for i in 0..n {
            eigenvectors[[i, k]] = v[i];
        }
    }

    Ok(eigenvectors)
}

/// Simple k-means clustering for spectral clustering
#[allow(non_snake_case)] // standard ML notation
fn kmeans_clustering(
    X: &Array2<Float>,
    n_clusters: usize,
    _random_state: Option<u64>,
) -> SklResult<Array1<i32>> {
    use scirs2_core::random::Random;

    let (n_samples, n_features) = X.dim();
    let mut rng = Random::seed(42);

    if n_clusters >= n_samples {
        // Each sample is its own cluster
        return Ok(Array1::from_iter(0..n_samples as i32));
    }

    // Initialize centroids randomly
    let mut centroids = Array2::<Float>::zeros((n_clusters, n_features));
    let mut selected_indices = HashSet::new();

    for k in 0..n_clusters {
        let mut idx = rng.gen_range(0..n_samples);
        while selected_indices.contains(&idx) {
            idx = rng.gen_range(0..n_samples);
        }
        selected_indices.insert(idx);

        for j in 0..n_features {
            centroids[[k, j]] = X[[idx, j]];
        }
    }

    let mut labels = Array1::<i32>::zeros(n_samples);
    let max_iter = 300;

    for _iter in 0..max_iter {
        let prev_labels = labels.clone();

        // Assign points to closest centroid
        for i in 0..n_samples {
            let mut min_dist = Float::INFINITY;
            let mut best_cluster = 0;

            for k in 0..n_clusters {
                let mut dist = 0.0;
                for j in 0..n_features {
                    let diff = X[[i, j]] - centroids[[k, j]];
                    dist += diff * diff;
                }

                if dist < min_dist {
                    min_dist = dist;
                    best_cluster = k;
                }
            }

            labels[i] = best_cluster as i32;
        }

        // Update centroids
        let mut cluster_counts = vec![0; n_clusters];
        centroids.fill(0.0);

        for i in 0..n_samples {
            let cluster = labels[i] as usize;
            cluster_counts[cluster] += 1;
            for j in 0..n_features {
                centroids[[cluster, j]] += X[[i, j]];
            }
        }

        for k in 0..n_clusters {
            if cluster_counts[k] > 0 {
                let count = cluster_counts[k] as Float;
                for j in 0..n_features {
                    centroids[[k, j]] /= count;
                }
            }
        }

        // Check convergence
        if labels == prev_labels {
            break;
        }
    }

    Ok(labels)
}

/// Multi-scale graph construction
///
/// Constructs graphs at multiple scales and combines them to capture both
/// local and global structure. This is particularly useful for semi-supervised
/// learning where different scales may reveal different patterns in the data.
///
/// The method builds k-NN graphs at different neighborhood sizes and combines
/// them using weighted averaging, where weights can be learned or predefined.
///
/// # Parameters
///
/// * `X` - Input data matrix (n_samples x n_features)
/// * `scales` - Vector of k values for different scales (neighborhood sizes)
/// * `scale_weights` - Optional weights for combining scales (if None, uniform weights used)
/// * `combination_method` - Method for combining graphs ("weighted", "union", "intersection")
/// * `normalize_scales` - Whether to normalize each scale independently
///
/// # Returns
///
/// * `SklResult<Array2<Float>>` - Combined multi-scale adjacency matrix
///
/// # Examples
///
/// ```
/// use scirs2_core::array;
/// use sklears_semi_supervised::multi_scale_graph_construction;
///
///
/// let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
/// let scales = vec![2, 3];
/// let W = multi_scale_graph_construction(&X, &scales, None, "weighted", true).unwrap();
/// assert_eq!(W.dim(), (4, 4));
/// ```
#[allow(non_snake_case)] // standard ML notation
pub fn multi_scale_graph_construction(
    X: &Array2<Float>,
    scales: &[usize],
    scale_weights: Option<&[Float]>,
    combination_method: &str,
    normalize_scales: bool,
) -> SklResult<Array2<Float>> {
    if scales.is_empty() {
        return Err(SklearsError::InvalidInput(
            "At least one scale must be provided".to_string(),
        ));
    }

    let n_samples = X.nrows();

    // Validate scales
    for &scale in scales {
        if scale >= n_samples {
            return Err(SklearsError::InvalidInput(format!(
                "Scale {} must be less than number of samples {}",
                scale, n_samples
            )));
        }
    }

    // Set default weights if not provided
    let weights = if let Some(w) = scale_weights {
        if w.len() != scales.len() {
            return Err(SklearsError::InvalidInput(
                "Number of weights must match number of scales".to_string(),
            ));
        }
        w.to_vec()
    } else {
        vec![1.0 / scales.len() as Float; scales.len()]
    };

    // Validate weights sum to 1 for weighted combination
    if combination_method == "weighted" {
        let weight_sum: Float = weights.iter().sum();
        if (weight_sum - 1.0).abs() > 1e-10 {
            return Err(SklearsError::InvalidInput(
                "Weights must sum to 1 for weighted combination".to_string(),
            ));
        }
    }

    // Build graphs at each scale
    let mut scale_graphs = Vec::new();

    for &k in scales {
        let mut graph = knn_graph(X, k, "connectivity")?;

        // Make symmetric
        make_symmetric(&mut graph);

        // Normalize if requested
        if normalize_scales {
            let max_degree = graph
                .sum_axis(Axis(1))
                .iter()
                .fold(0.0 as Float, |acc, &x| acc.max(x));
            if max_degree > 0.0 {
                graph.mapv_inplace(|x| x / max_degree);
            }
        }

        scale_graphs.push(graph);
    }

    // Combine graphs according to specified method
    let mut combined_graph = Array2::zeros((n_samples, n_samples));

    match combination_method {
        "weighted" => {
            // Weighted combination of all scales
            for (graph, &weight) in scale_graphs.iter().zip(weights.iter()) {
                combined_graph = combined_graph + weight * graph;
            }
        }
        "union" => {
            // Union: edge exists if it exists in any scale
            for graph in scale_graphs.iter() {
                for i in 0..n_samples {
                    for j in 0..n_samples {
                        if graph[[i, j]] > 0.0 {
                            combined_graph[[i, j]] = 1.0;
                        }
                    }
                }
            }
        }
        "intersection" => {
            // Intersection: edge exists only if it exists in all scales
            // Initialize with all ones
            combined_graph.fill(1.0);

            for graph in scale_graphs.iter() {
                for i in 0..n_samples {
                    for j in 0..n_samples {
                        if graph[[i, j]] == 0.0 {
                            combined_graph[[i, j]] = 0.0;
                        }
                    }
                }
            }
        }
        "adaptive_weighted" => {
            // Adaptive weighting based on local density
            let mut adaptive_weights = Vec::new();

            for (scale_idx, graph) in scale_graphs.iter().enumerate() {
                let avg_degree = graph.sum_axis(Axis(1)).mean().unwrap_or(0.0);
                let scale_density = avg_degree / scales[scale_idx] as Float;
                adaptive_weights.push(scale_density);
            }

            // Normalize adaptive weights
            let total_weight: Float = adaptive_weights.iter().sum();
            if total_weight > 0.0 {
                for weight in adaptive_weights.iter_mut() {
                    *weight /= total_weight;
                }
            }

            // Apply adaptive weights
            for (graph, &weight) in scale_graphs.iter().zip(adaptive_weights.iter()) {
                combined_graph = combined_graph + weight * graph;
            }
        }
        "max_pooling" => {
            // Max pooling: take maximum edge weight across scales
            for graph in scale_graphs.iter() {
                for i in 0..n_samples {
                    for j in 0..n_samples {
                        combined_graph[[i, j]] = combined_graph[[i, j]].max(graph[[i, j]]);
                    }
                }
            }
        }
        "hierarchical" => {
            // Hierarchical combination: smaller scales have higher priority
            for (scale_idx, graph) in scale_graphs.iter().enumerate() {
                let hierarchy_weight = 1.0 / (scale_idx + 1) as Float;
                for i in 0..n_samples {
                    for j in 0..n_samples {
                        if graph[[i, j]] > 0.0 {
                            combined_graph[[i, j]] =
                                combined_graph[[i, j]].max(graph[[i, j]] * hierarchy_weight);
                        }
                    }
                }
            }
        }
        _ => {
            return Err(SklearsError::InvalidInput(format!(
                "Unknown combination method: {}. Supported methods: weighted, union, intersection, adaptive_weighted, max_pooling, hierarchical",
                combination_method
            )));
        }
    }

    // Final symmetry enforcement
    make_symmetric(&mut combined_graph);

    Ok(combined_graph)
}

/// Multi-scale spectral clustering
///
/// Performs spectral clustering using multi-scale graph construction to capture
/// structure at different resolutions. This can improve clustering performance
/// by leveraging both local and global connectivity patterns.
///
/// # Parameters
///
/// * `X` - Input data matrix
/// * `n_clusters` - Number of clusters
/// * `scales` - Vector of k values for different scales
/// * `scale_weights` - Optional weights for combining scales
/// * `combination_method` - Method for combining graphs
/// * `normalized` - Whether to use normalized Laplacian
/// * `random_state` - Random seed for reproducibility
///
/// # Returns
///
/// * `SklResult<Array1<i32>>` - Cluster labels
#[allow(non_snake_case)] // standard ML notation
pub fn multi_scale_spectral_clustering(
    X: &Array2<Float>,
    n_clusters: usize,
    scales: &[usize],
    scale_weights: Option<&[Float]>,
    combination_method: &str,
    normalized: bool,
    random_state: Option<u64>,
) -> SklResult<Array1<i32>> {
    // Build multi-scale graph
    let adjacency = multi_scale_graph_construction(
        X,
        scales,
        scale_weights,
        combination_method,
        true, // normalize scales
    )?;

    // Perform spectral clustering on the multi-scale graph
    spectral_clustering(&adjacency, n_clusters, normalized, random_state)
}

/// Adaptive multi-scale graph construction
///
/// Automatically determines appropriate scales based on data characteristics
/// and constructs a multi-scale graph. This version adaptively selects scales
/// based on local density and data distribution.
///
/// # Parameters
///
/// * `X` - Input data matrix
/// * `min_scale` - Minimum k value to consider
/// * `max_scale` - Maximum k value to consider
/// * `n_scales` - Number of scales to use
/// * `density_threshold` - Threshold for density-based scale selection
/// * `combination_method` - Method for combining graphs
///
/// # Returns
///
/// * `SklResult<(Array2<Float>, Vec<usize>)>` - Combined graph and selected scales
#[allow(non_snake_case)] // standard ML notation
pub fn adaptive_multi_scale_graph_construction(
    X: &Array2<Float>,
    min_scale: usize,
    max_scale: usize,
    n_scales: usize,
    density_threshold: Float,
    combination_method: &str,
) -> SklResult<(Array2<Float>, Vec<usize>)> {
    let n_samples = X.nrows();

    if max_scale >= n_samples {
        return Err(SklearsError::InvalidInput(
            "max_scale must be less than number of samples".to_string(),
        ));
    }

    if min_scale >= max_scale {
        return Err(SklearsError::InvalidInput(
            "min_scale must be less than max_scale".to_string(),
        ));
    }

    // Compute local densities to guide scale selection
    let mut densities = Vec::new();
    let candidate_scales: Vec<usize> = (min_scale..=max_scale)
        .step_by(((max_scale - min_scale) / n_scales.max(1)).max(1))
        .take(n_scales)
        .collect();

    for &k in &candidate_scales {
        let graph = knn_graph(X, k, "connectivity")?;
        let avg_degree = graph.sum_axis(Axis(1)).mean().unwrap_or(0.0);
        let density = avg_degree / k as Float;
        densities.push(density);
    }

    // Select scales based on density criteria
    let mut selected_scales = Vec::new();
    let mut selected_weights = Vec::new();

    for (i, &density) in densities.iter().enumerate() {
        if density >= density_threshold {
            selected_scales.push(candidate_scales[i]);
            selected_weights.push(density);
        }
    }

    // Ensure we have at least one scale
    if selected_scales.is_empty() {
        selected_scales.push(candidate_scales[densities.len() / 2]);
        selected_weights.push(1.0);
    }

    // Normalize weights
    let total_weight: Float = selected_weights.iter().sum();
    if total_weight > 0.0 {
        for weight in selected_weights.iter_mut() {
            *weight /= total_weight;
        }
    }

    // Build multi-scale graph with selected scales
    let combined_graph = multi_scale_graph_construction(
        X,
        &selected_scales,
        Some(&selected_weights),
        combination_method,
        true,
    )?;

    Ok((combined_graph, selected_scales))
}