sklears-manifold 0.1.2

Manifold learning algorithms (t-SNE, Isomap, etc.)
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
//! Hierarchical manifold learning methods
//! This module implements various hierarchical approaches for manifold learning,
//! including multi-scale embeddings and coarse-to-fine optimization.

use scirs2_core::ndarray::{Array1, Array2, ArrayView1, ArrayView2, Axis};
use scirs2_core::random::rngs::StdRng;
use scirs2_core::random::thread_rng;
use scirs2_core::random::SeedableRng;
use scirs2_core::RngExt;
use scirs2_linalg::compat::{ArrayLinalgExt, UPLO};
use sklears_core::{
    error::{Result as SklResult, SklearsError},
    traits::{Estimator, Fit, Transform, Untrained},
};

/// Hierarchical manifold learning with multi-scale embeddings
///
/// This implements a hierarchical approach where the manifold is learned at multiple
/// scales, from coarse to fine, enabling better preservation of both global and local structure.
///
/// # Parameters
///
/// * `n_components` - Target dimensionality for embeddings
/// * `levels` - Number of hierarchical levels
/// * `scale_factors` - Scale factors for each level (coarse to fine)
/// * `base_method` - Base manifold learning method ("pca", "isomap", "lle")
/// * `refinement_steps` - Number of refinement steps per level
/// * `learning_rate` - Learning rate for refinement optimization
/// * `random_state` - Random seed for reproducibility
///
/// # Examples
///
/// ```rust,ignore
/// use sklears_manifold::hierarchical::HierarchicalManifold;
/// use sklears_core::traits::{Transform, Fit};
/// use scirs2_core::ndarray::{ Array1, ArrayView1, ArrayView2};
///
/// let x = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], [10.0, 11.0, 12.0]];
///
/// let hierarchical = HierarchicalManifold::new()
///     .n_components(2)
///     .levels(3)
///     .base_method("pca".to_string());
///
/// let y = array![(), (), (), ()];
/// let fitted = hierarchical.fit(&x.view(), &y.view()).unwrap();
/// let embedded = fitted.transform(&x.view()).unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct HierarchicalManifold<S = Untrained> {
    state: S,
    n_components: usize,
    levels: usize,
    scale_factors: Vec<f64>,
    base_method: String,
    refinement_steps: usize,
    learning_rate: f64,
    random_state: Option<u64>,
}

/// Trained state for HierarchicalManifold
#[allow(dead_code)] // retained for serialization/introspection
#[derive(Debug, Clone)]
pub struct TrainedHierarchicalManifold {
    level_embeddings: Vec<Array2<f64>>,
    level_transforms: Vec<Array2<f64>>,
    n_features: usize,
    n_components: usize,
    levels: usize,
}

impl Default for HierarchicalManifold<Untrained> {
    fn default() -> Self {
        Self::new()
    }
}

impl HierarchicalManifold<Untrained> {
    /// Create a new HierarchicalManifold instance
    pub fn new() -> Self {
        Self {
            state: Untrained,
            n_components: 2,
            levels: 3,
            scale_factors: vec![0.1, 0.5, 1.0], // Coarse to fine
            base_method: "pca".to_string(),
            refinement_steps: 100,
            learning_rate: 0.01,
            random_state: None,
        }
    }

    /// Set the number of components
    pub fn n_components(mut self, n_components: usize) -> Self {
        self.n_components = n_components;
        self
    }

    /// Set the number of hierarchical levels
    pub fn levels(mut self, levels: usize) -> Self {
        self.levels = levels;
        // Adjust scale factors if needed
        if self.scale_factors.len() != levels {
            self.scale_factors = (0..levels)
                .map(|i| (i + 1) as f64 / levels as f64)
                .collect();
        }
        self
    }

    /// Set the scale factors for each level
    pub fn scale_factors(mut self, scale_factors: Vec<f64>) -> Self {
        self.scale_factors = scale_factors;
        self
    }

    /// Set the base method
    pub fn base_method(mut self, base_method: String) -> Self {
        self.base_method = base_method;
        self
    }

    /// Set the number of refinement steps
    pub fn refinement_steps(mut self, refinement_steps: usize) -> Self {
        self.refinement_steps = refinement_steps;
        self
    }

    /// Set the learning rate
    pub fn learning_rate(mut self, learning_rate: f64) -> Self {
        self.learning_rate = learning_rate;
        self
    }

    /// Set the random state
    pub fn random_state(mut self, random_state: u64) -> Self {
        self.random_state = Some(random_state);
        self
    }
}

impl Estimator for HierarchicalManifold<Untrained> {
    type Config = ();
    type Error = SklearsError;
    type Float = f64;

    fn config(&self) -> &Self::Config {
        &()
    }
}

impl Fit<ArrayView2<'_, f64>, ArrayView1<'_, ()>> for HierarchicalManifold<Untrained> {
    type Fitted = HierarchicalManifold<TrainedHierarchicalManifold>;

    fn fit(self, x: &ArrayView2<'_, f64>, _y: &ArrayView1<'_, ()>) -> SklResult<Self::Fitted> {
        if x.nrows() == 0 || x.ncols() == 0 {
            return Err(SklearsError::InvalidInput(
                "Input data is empty".to_string(),
            ));
        }

        let n_samples = x.nrows();
        let n_features = x.ncols();

        let mut rng = if let Some(seed) = self.random_state {
            StdRng::seed_from_u64(seed)
        } else {
            StdRng::seed_from_u64(thread_rng().random::<u64>())
        };

        let mut level_embeddings = Vec::new();
        let mut level_transforms = Vec::new();

        // Start with coarsest level
        let mut current_embedding = Array2::zeros((n_samples, self.n_components));

        for (level, &scale_factor) in self.scale_factors.iter().enumerate() {
            // Apply base method at current scale
            let embedding = match self.base_method.as_str() {
                "pca" => self.apply_pca(x, scale_factor)?,
                "isomap" => self.apply_isomap(x, scale_factor, &mut rng)?,
                "lle" => self.apply_lle(x, scale_factor, &mut rng)?,
                _ => {
                    return Err(SklearsError::InvalidInput(format!(
                        "Unknown base method: {}",
                        self.base_method
                    )))
                }
            };

            // If not the first level, refine based on previous level
            let refined_embedding = if level > 0 {
                self.refine_embedding(&current_embedding, &embedding, x, scale_factor, &mut rng)?
            } else {
                embedding.clone()
            };

            current_embedding = refined_embedding.clone();
            level_embeddings.push(refined_embedding);

            // Compute transformation matrix for this level
            let transform = self.compute_transform_matrix(x, &current_embedding)?;
            level_transforms.push(transform);
        }

        Ok(HierarchicalManifold {
            state: TrainedHierarchicalManifold {
                level_embeddings,
                level_transforms,
                n_features,
                n_components: self.n_components,
                levels: self.levels,
            },
            n_components: self.n_components,
            levels: self.levels,
            scale_factors: self.scale_factors,
            base_method: self.base_method,
            refinement_steps: self.refinement_steps,
            learning_rate: self.learning_rate,
            random_state: self.random_state,
        })
    }
}

impl HierarchicalManifold<Untrained> {
    /// Apply PCA at given scale
    fn apply_pca(&self, x: &ArrayView2<f64>, scale_factor: f64) -> SklResult<Array2<f64>> {
        let n_samples = x.nrows();
        let n_features = x.ncols();

        // Center the data
        let mean = x.mean_axis(Axis(0)).expect("operation should succeed");
        let centered = x - &mean.insert_axis(Axis(0));

        // Compute covariance matrix with scaling
        let cov = centered.t().dot(&centered) / (n_samples - 1) as f64 * scale_factor;

        // Eigendecomposition
        let (eigenvalues, eigenvectors) = cov.eigh(UPLO::Upper).map_err(|e| {
            SklearsError::InvalidInput(format!("PCA eigendecomposition failed: {}", e))
        })?;

        // Sort by eigenvalues (descending)
        let mut indices: Vec<usize> = (0..eigenvalues.len()).collect();
        indices.sort_by(|&i, &j| {
            eigenvalues[j]
                .partial_cmp(&eigenvalues[i])
                .expect("operation should succeed")
        });

        // Project data onto top eigenvectors
        let mut projection_matrix = Array2::zeros((n_features, self.n_components));
        for (i, &idx) in indices.iter().take(self.n_components).enumerate() {
            projection_matrix
                .column_mut(i)
                .assign(&eigenvectors.column(idx));
        }

        let embedding = centered.dot(&projection_matrix);
        Ok(embedding)
    }

    /// Apply Isomap at given scale
    fn apply_isomap(
        &self,
        x: &ArrayView2<f64>,
        scale_factor: f64,
        _rng: &mut StdRng,
    ) -> SklResult<Array2<f64>> {
        let n_samples = x.nrows();
        let k = ((n_samples as f64 * scale_factor).round() as usize)
            .max(3)
            .min(n_samples - 1);

        // Build k-NN graph
        let graph = self.build_knn_graph(x, k)?;

        // Compute geodesic distances using Floyd-Warshall
        let geodesic_distances = self.floyd_warshall(&graph)?;

        // Apply MDS to geodesic distances
        let embedding = self.mds(&geodesic_distances)?;
        Ok(embedding)
    }

    /// Apply LLE at given scale
    fn apply_lle(
        &self,
        x: &ArrayView2<f64>,
        scale_factor: f64,
        _rng: &mut StdRng,
    ) -> SklResult<Array2<f64>> {
        let n_samples = x.nrows();
        let k = ((n_samples as f64 * scale_factor).round() as usize)
            .max(3)
            .min(n_samples - 1);

        // Find k nearest neighbors
        let neighbors = self.find_k_neighbors(x, k)?;

        // Compute reconstruction weights
        let weights = self.compute_lle_weights(x, &neighbors)?;

        // Solve eigenvalue problem
        let embedding = self.solve_lle_eigenvalue_problem(&weights)?;
        Ok(embedding)
    }

    /// Refine embedding using previous level
    fn refine_embedding(
        &self,
        previous_embedding: &Array2<f64>,
        current_embedding: &Array2<f64>,
        x: &ArrayView2<f64>,
        scale_factor: f64,
        _rng: &mut StdRng,
    ) -> SklResult<Array2<f64>> {
        let n_samples = x.nrows();
        let mut refined = current_embedding.clone();

        // Iterative refinement
        for _step in 0..self.refinement_steps {
            let mut total_loss = 0.0;
            let mut gradient = Array2::zeros(refined.raw_dim());

            // Compute loss and gradients
            for i in 0..n_samples {
                for j in (i + 1)..n_samples {
                    // High-dimensional distance
                    let hd_dist = (&x.row(i) - &x.row(j)).mapv(|x: f64| x * x).sum().sqrt();

                    // Previous level distance
                    let prev_dist = (&previous_embedding.row(i) - &previous_embedding.row(j))
                        .mapv(|x: f64| x * x)
                        .sum()
                        .sqrt();

                    // Current embedding distance
                    let curr_dist = (&refined.row(i) - &refined.row(j))
                        .mapv(|x: f64| x * x)
                        .sum()
                        .sqrt();

                    // Loss: weighted combination of preservation of previous level and high-dimensional distances
                    let target_dist = scale_factor * hd_dist + (1.0 - scale_factor) * prev_dist;
                    let loss = (curr_dist - target_dist).powi(2);
                    total_loss += loss;

                    // Gradient
                    if curr_dist > 1e-8 {
                        let factor = 2.0 * (curr_dist - target_dist) / curr_dist;
                        let diff = &refined.row(i) - &refined.row(j);

                        for k in 0..self.n_components {
                            gradient[[i, k]] += factor * diff[k];
                            gradient[[j, k]] -= factor * diff[k];
                        }
                    }
                }
            }

            // Apply gradient update
            refined = refined - self.learning_rate * gradient;

            // Early stopping
            if total_loss < 1e-6 {
                break;
            }
        }

        Ok(refined)
    }

    /// Compute transformation matrix from input to embedding space
    fn compute_transform_matrix(
        &self,
        x: &ArrayView2<f64>,
        embedding: &Array2<f64>,
    ) -> SklResult<Array2<f64>> {
        // Use least squares to find transformation
        let xt_x = x.t().dot(x);
        let xt_y = x.t().dot(embedding);

        // Solve using pseudoinverse
        let (u, s, vt) = xt_x
            .svd(true)
            .map_err(|e| SklearsError::InvalidInput(format!("SVD failed: {}", e)))?; // vt is directly available

        // Compute pseudoinverse
        let mut s_inv = Array1::zeros(s.len());
        for (i, &val) in s.iter().enumerate() {
            if val > 1e-10 {
                s_inv[i] = 1.0 / val;
            }
        }

        let s_inv_diag = Array2::from_diag(&s_inv);
        let pinv = vt.t().dot(&s_inv_diag).dot(&u.t());

        let transform = pinv.dot(&xt_y);
        Ok(transform)
    }

    /// Build k-nearest neighbor graph
    fn build_knn_graph(&self, x: &ArrayView2<f64>, k: usize) -> SklResult<Array2<f64>> {
        let n_samples = x.nrows();
        let mut graph = Array2::from_elem((n_samples, n_samples), f64::INFINITY);

        // Set diagonal to 0
        for i in 0..n_samples {
            graph[[i, i]] = 0.0;
        }

        // For each point, find k nearest neighbors
        for i in 0..n_samples {
            let mut distances: Vec<(usize, f64)> = Vec::new();

            for j in 0..n_samples {
                if i != j {
                    let dist = (&x.row(i) - &x.row(j)).mapv(|x: f64| x * x).sum().sqrt();
                    distances.push((j, dist));
                }
            }

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

            // Connect to k nearest neighbors
            for &(j, dist) in distances.iter().take(k) {
                graph[[i, j]] = dist;
                graph[[j, i]] = dist; // Make symmetric
            }
        }

        Ok(graph)
    }

    /// Compute geodesic distances using Floyd-Warshall algorithm
    fn floyd_warshall(&self, graph: &Array2<f64>) -> SklResult<Array2<f64>> {
        let n = graph.nrows();
        let mut distances = graph.clone();

        for k in 0..n {
            for i in 0..n {
                for j in 0..n {
                    let new_dist = distances[[i, k]] + distances[[k, j]];
                    if new_dist < distances[[i, j]] {
                        distances[[i, j]] = new_dist;
                    }
                }
            }
        }

        Ok(distances)
    }

    /// Apply multidimensional scaling
    fn mds(&self, distances: &Array2<f64>) -> SklResult<Array2<f64>> {
        let n = distances.nrows();

        // Double centering
        let mut gram = Array2::zeros((n, n));
        let mean_row = distances
            .mean_axis(Axis(1))
            .expect("operation should succeed");
        let mean_col = distances
            .mean_axis(Axis(0))
            .expect("operation should succeed");
        let mean_all = distances.mean().expect("operation should succeed");

        for i in 0..n {
            for j in 0..n {
                gram[[i, j]] =
                    -0.5 * (distances[[i, j]].powi(2) - mean_row[i] - mean_col[j] + mean_all);
            }
        }

        // Eigendecomposition
        let (eigenvalues, eigenvectors) = gram.eigh(UPLO::Upper).map_err(|e| {
            SklearsError::InvalidInput(format!("MDS eigendecomposition failed: {}", e))
        })?;

        // Sort eigenvalues and eigenvectors
        let mut indices: Vec<usize> = (0..eigenvalues.len()).collect();
        indices.sort_by(|&i, &j| {
            eigenvalues[j]
                .partial_cmp(&eigenvalues[i])
                .expect("operation should succeed")
        });

        // Create embedding
        let mut embedding = Array2::zeros((n, self.n_components));
        for (i, &idx) in indices.iter().take(self.n_components).enumerate() {
            let scale = eigenvalues[idx].max(0.0).sqrt();
            for j in 0..n {
                embedding[[j, i]] = eigenvectors[[j, idx]] * scale;
            }
        }

        Ok(embedding)
    }

    /// Find k nearest neighbors for each point
    fn find_k_neighbors(&self, x: &ArrayView2<f64>, k: usize) -> SklResult<Vec<Vec<usize>>> {
        let n_samples = x.nrows();
        let mut neighbors = Vec::new();

        for i in 0..n_samples {
            let mut distances: Vec<(usize, f64)> = Vec::new();

            for j in 0..n_samples {
                if i != j {
                    let dist = (&x.row(i) - &x.row(j)).mapv(|x: f64| 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(k).map(|&(idx, _)| idx).collect();
            neighbors.push(point_neighbors);
        }

        Ok(neighbors)
    }

    /// Compute LLE reconstruction weights
    fn compute_lle_weights(
        &self,
        x: &ArrayView2<f64>,
        neighbors: &[Vec<usize>],
    ) -> SklResult<Array2<f64>> {
        let n_samples = x.nrows();
        let mut weights = Array2::zeros((n_samples, n_samples));

        for (i, point_neighbors) in neighbors.iter().enumerate() {
            let k = point_neighbors.len();
            if k == 0 {
                continue;
            }

            // Build local covariance matrix
            let mut local_cov = Array2::zeros((k, k));
            let xi = x.row(i);

            for (a, &neighbor_a) in point_neighbors.iter().enumerate() {
                for (b, &neighbor_b) in point_neighbors.iter().enumerate() {
                    let diff_a = &x.row(neighbor_a) - &xi;
                    let diff_b = &x.row(neighbor_b) - &xi;
                    local_cov[[a, b]] = diff_a.dot(&diff_b);
                }
            }

            // Add regularization
            let reg = 1e-3 * local_cov.diag().sum() / k as f64;
            for j in 0..k {
                local_cov[[j, j]] += reg;
            }

            // Solve for weights
            let ones = Array1::ones(k);
            let weights_vec = match local_cov.solve(&ones) {
                Ok(w) => w,
                Err(_) => {
                    // Fallback to uniform weights
                    Array1::from_elem(k, 1.0 / k as f64)
                }
            };

            // Normalize weights
            let weight_sum = weights_vec.sum();
            for (j, &neighbor) in point_neighbors.iter().enumerate() {
                weights[[i, neighbor]] = weights_vec[j] / weight_sum;
            }
        }

        Ok(weights)
    }

    /// Solve LLE eigenvalue problem
    fn solve_lle_eigenvalue_problem(&self, weights: &Array2<f64>) -> SklResult<Array2<f64>> {
        let n_samples = weights.nrows();

        // Build sparse matrix M = (I - W)^T (I - W)
        let identity = Array2::eye(n_samples);
        let i_minus_w = &identity - weights;
        let m = i_minus_w.t().dot(&i_minus_w);

        // Eigendecomposition
        let (eigenvalues, eigenvectors): (Array1<f64>, Array2<f64>) =
            m.eigh(UPLO::Upper).map_err(|e| {
                SklearsError::InvalidInput(format!("LLE eigendecomposition failed: {}", e))
            })?;

        // Sort eigenvalues (ascending for LLE)
        let mut indices: Vec<usize> = (0..eigenvalues.len()).collect();
        indices.sort_by(|&i, &j| {
            eigenvalues[i]
                .partial_cmp(&eigenvalues[j])
                .expect("operation should succeed")
        });

        // Skip the first eigenvector (constant) and take the next n_components
        let mut embedding = Array2::zeros((n_samples, self.n_components));
        for (i, &idx) in indices.iter().skip(1).take(self.n_components).enumerate() {
            embedding.column_mut(i).assign(&eigenvectors.column(idx));
        }

        Ok(embedding)
    }
}

impl Estimator for HierarchicalManifold<TrainedHierarchicalManifold> {
    type Config = ();
    type Error = SklearsError;
    type Float = f64;

    fn config(&self) -> &Self::Config {
        &()
    }
}

impl Transform<ArrayView2<'_, f64>, Array2<f64>>
    for HierarchicalManifold<TrainedHierarchicalManifold>
{
    fn transform(&self, x: &ArrayView2<'_, f64>) -> SklResult<Array2<f64>> {
        if x.ncols() != self.state.n_features {
            return Err(SklearsError::InvalidInput(format!(
                "Expected {} features, got {}",
                self.state.n_features,
                x.ncols()
            )));
        }

        // Use the final level transformation
        let final_transform = &self.state.level_transforms[self.state.levels - 1];
        let transformed = x.dot(final_transform);
        Ok(transformed)
    }
}

/// Multi-scale embedding for pyramidal manifold representations
///
/// Implements a pyramidal approach where embeddings are computed at multiple
/// resolutions and combined for comprehensive manifold representation.
#[allow(dead_code)] // retained for serialization/introspection
#[derive(Debug, Clone)]
pub struct MultiScaleEmbedding<S = Untrained> {
    state: S,
    n_components: usize,
    scales: Vec<f64>,
    base_method: String,
    combination_method: String, // "weighted", "concatenate", "attention"
    random_state: Option<u64>,
}

/// Trained state for MultiScaleEmbedding
#[derive(Debug, Clone)]
#[allow(dead_code)] // retained for serialization/introspection
pub struct TrainedMultiScaleEmbedding {
    scale_embeddings: Vec<Array2<f64>>,
    scale_weights: Vec<f64>,
    combination_matrix: Array2<f64>,
    n_features: usize,
    n_components: usize,
}

impl Default for MultiScaleEmbedding<Untrained> {
    fn default() -> Self {
        Self::new()
    }
}

impl MultiScaleEmbedding<Untrained> {
    /// Create a new MultiScaleEmbedding instance
    pub fn new() -> Self {
        Self {
            state: Untrained,
            n_components: 2,
            scales: vec![0.25, 0.5, 1.0, 2.0],
            base_method: "pca".to_string(),
            combination_method: "weighted".to_string(),
            random_state: None,
        }
    }

    /// Set the number of components
    pub fn n_components(mut self, n_components: usize) -> Self {
        self.n_components = n_components;
        self
    }

    /// Set the scales
    pub fn scales(mut self, scales: Vec<f64>) -> Self {
        self.scales = scales;
        self
    }

    /// Set the base method
    pub fn base_method(mut self, base_method: String) -> Self {
        self.base_method = base_method;
        self
    }

    /// Set the combination method
    pub fn combination_method(mut self, combination_method: String) -> Self {
        self.combination_method = combination_method;
        self
    }

    /// Set the random state
    pub fn random_state(mut self, random_state: u64) -> Self {
        self.random_state = Some(random_state);
        self
    }
}

impl Estimator for MultiScaleEmbedding<Untrained> {
    type Config = ();
    type Error = SklearsError;
    type Float = f64;

    fn config(&self) -> &Self::Config {
        &()
    }
}

impl Fit<ArrayView2<'_, f64>, ArrayView1<'_, ()>> for MultiScaleEmbedding<Untrained> {
    type Fitted = MultiScaleEmbedding<TrainedMultiScaleEmbedding>;

    fn fit(self, x: &ArrayView2<'_, f64>, _y: &ArrayView1<'_, ()>) -> SklResult<Self::Fitted> {
        if x.nrows() == 0 || x.ncols() == 0 {
            return Err(SklearsError::InvalidInput(
                "Input data is empty".to_string(),
            ));
        }

        let _n_samples = x.nrows();
        let n_features = x.ncols();

        let mut scale_embeddings = Vec::new();
        let mut scale_weights = Vec::new();

        // Compute embeddings at each scale
        for &scale in &self.scales {
            let embedding = self.compute_scale_embedding(x, scale)?;
            scale_embeddings.push(embedding);

            // Compute quality weight for this scale
            let weight = self.compute_scale_weight(
                x,
                scale_embeddings.last().expect("operation should succeed"),
            )?;
            scale_weights.push(weight);
        }

        // Normalize weights
        let weight_sum: f64 = scale_weights.iter().sum();
        if weight_sum > 0.0 {
            for weight in scale_weights.iter_mut() {
                *weight /= weight_sum;
            }
        } else {
            // Uniform weights if all are zero
            scale_weights.fill(1.0 / self.scales.len() as f64);
        }

        // Compute combination matrix
        let combination_matrix =
            self.compute_combination_matrix(x, &scale_embeddings, &scale_weights)?;

        Ok(MultiScaleEmbedding {
            state: TrainedMultiScaleEmbedding {
                scale_embeddings,
                scale_weights,
                combination_matrix,
                n_features,
                n_components: self.n_components,
            },
            n_components: self.n_components,
            scales: self.scales,
            base_method: self.base_method,
            combination_method: self.combination_method,
            random_state: self.random_state,
        })
    }
}

impl MultiScaleEmbedding<Untrained> {
    /// Compute embedding at a specific scale
    fn compute_scale_embedding(&self, x: &ArrayView2<f64>, scale: f64) -> SklResult<Array2<f64>> {
        match self.base_method.as_str() {
            "pca" => self.scaled_pca(x, scale),
            _ => Err(SklearsError::InvalidInput(format!(
                "Unsupported base method: {}",
                self.base_method
            ))),
        }
    }

    /// Apply PCA with scaling
    fn scaled_pca(&self, x: &ArrayView2<f64>, scale: f64) -> SklResult<Array2<f64>> {
        let n_samples = x.nrows();

        // Center the data
        let mean = x.mean_axis(Axis(0)).expect("operation should succeed");
        let centered = x - &mean.insert_axis(Axis(0));

        // Apply scale to covariance computation
        let scaled_data = &centered * scale;
        let cov = scaled_data.t().dot(&scaled_data) / (n_samples - 1) as f64;

        // Eigendecomposition
        let (eigenvalues, eigenvectors) = cov
            .eigh(UPLO::Upper)
            .map_err(|e| SklearsError::InvalidInput(format!("Scaled PCA failed: {}", e)))?;

        // Sort by eigenvalues (descending)
        let mut indices: Vec<usize> = (0..eigenvalues.len()).collect();
        indices.sort_by(|&i, &j| {
            eigenvalues[j]
                .partial_cmp(&eigenvalues[i])
                .expect("operation should succeed")
        });

        // Project data
        let mut projection_matrix = Array2::zeros((x.ncols(), self.n_components));
        for (i, &idx) in indices.iter().take(self.n_components).enumerate() {
            projection_matrix
                .column_mut(i)
                .assign(&eigenvectors.column(idx));
        }

        let embedding = centered.dot(&projection_matrix);
        Ok(embedding)
    }

    /// Compute quality weight for a scale embedding
    fn compute_scale_weight(&self, x: &ArrayView2<f64>, embedding: &Array2<f64>) -> SklResult<f64> {
        // Simple quality measure: preservation of pairwise distances
        let n_samples = x.nrows();
        let mut correlation_sum = 0.0;
        let mut count = 0;

        for i in 0..n_samples {
            for j in (i + 1)..n_samples {
                let hd_dist = (&x.row(i) - &x.row(j)).mapv(|x: f64| x * x).sum().sqrt();
                let ld_dist = (&embedding.row(i) - &embedding.row(j))
                    .mapv(|x: f64| x * x)
                    .sum()
                    .sqrt();

                correlation_sum += hd_dist * ld_dist;
                count += 1;
            }
        }

        Ok(if count > 0 {
            correlation_sum / count as f64
        } else {
            0.0
        })
    }

    /// Compute combination matrix for multi-scale embeddings
    fn compute_combination_matrix(
        &self,
        x: &ArrayView2<f64>,
        scale_embeddings: &[Array2<f64>],
        scale_weights: &[f64],
    ) -> SklResult<Array2<f64>> {
        match self.combination_method.as_str() {
            "weighted" => {
                // Weighted average of embeddings
                let n_samples = x.nrows();
                let mut combined: Array2<f64> = Array2::zeros((n_samples, self.n_components));

                for (embedding, &weight) in scale_embeddings.iter().zip(scale_weights.iter()) {
                    combined = combined + weight * embedding;
                }

                // Return identity matrix since embedding is already combined
                Ok(Array2::eye(x.ncols()))
            }
            "concatenate" => {
                // Concatenate all scale embeddings
                let total_components = self.n_components * scale_embeddings.len();

                // Return projection matrix that will concatenate embeddings
                let mut projection = Array2::zeros((x.ncols(), total_components));

                // This is a simplified approach - in practice would need more sophisticated projection
                for i in 0..x.ncols().min(total_components) {
                    projection[[i, i]] = 1.0;
                }

                Ok(projection)
            }
            _ => Err(SklearsError::InvalidInput(format!(
                "Unknown combination method: {}",
                self.combination_method
            ))),
        }
    }
}

impl Estimator for MultiScaleEmbedding<TrainedMultiScaleEmbedding> {
    type Config = ();
    type Error = SklearsError;
    type Float = f64;

    fn config(&self) -> &Self::Config {
        &()
    }
}

impl Transform<ArrayView2<'_, f64>, Array2<f64>>
    for MultiScaleEmbedding<TrainedMultiScaleEmbedding>
{
    fn transform(&self, x: &ArrayView2<'_, f64>) -> SklResult<Array2<f64>> {
        if x.ncols() != self.state.n_features {
            return Err(SklearsError::InvalidInput(format!(
                "Expected {} features, got {}",
                self.state.n_features,
                x.ncols()
            )));
        }

        // Apply combination matrix transformation
        let transformed = x.dot(&self.state.combination_matrix);
        Ok(transformed)
    }
}

/// Adaptive resolution manifold learning that automatically determines optimal parameters
///
/// This extends hierarchical manifold learning by adaptively determining the number of levels,
/// scale factors, and other parameters based on data characteristics and embedding quality metrics.
///
/// # Parameters
///
/// * `n_components` - Target dimensionality for embeddings
/// * `max_levels` - Maximum number of hierarchical levels to consider
/// * `min_levels` - Minimum number of hierarchical levels
/// * `base_method` - Base manifold learning method ("pca", "isomap", "lle")
/// * `quality_threshold` - Minimum quality threshold for stopping adaptation
/// * `adaptation_method` - Method for adaptation ("stress_based", "neighborhood_preservation", "combined")
/// * `max_iterations` - Maximum iterations for adaptive process
/// * `random_state` - Random seed for reproducibility
///
/// # Examples
///
/// ```rust,ignore
/// use sklears_manifold::hierarchical::AdaptiveResolutionManifold;
/// use sklears_core::traits::{Transform, Fit};
/// use scirs2_core::ndarray::{ Array1, ArrayView1, ArrayView2};
///
/// let x = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], [10.0, 11.0, 12.0]];
///
/// let adaptive = AdaptiveResolutionManifold::new()
///     .n_components(2)
///     .max_levels(5)
///     .quality_threshold(0.8)
///     .adaptation_method("combined".to_string());
///
/// let y = array![(), (), (), ()];
/// let fitted = adaptive.fit(&x.view(), &y.view()).unwrap();
/// let embedded = fitted.transform(&x.view()).unwrap();
/// ```
#[allow(dead_code)] // retained for serialization/introspection
#[derive(Debug, Clone)]
pub struct AdaptiveResolutionManifold<S = Untrained> {
    state: S,
    n_components: usize,
    max_levels: usize,
    min_levels: usize,
    base_method: String,
    quality_threshold: f64,
    adaptation_method: String,
    max_iterations: usize,
    learning_rate: f64,
    random_state: Option<u64>,
}

/// Trained state for AdaptiveResolutionManifold
#[derive(Debug, Clone)]
#[allow(dead_code)] // retained for serialization/introspection
pub struct TrainedAdaptiveResolutionManifold {
    optimal_levels: usize,
    optimal_scale_factors: Vec<f64>,
    level_embeddings: Vec<Array2<f64>>,
    level_transforms: Vec<Array2<f64>>,
    quality_scores: Vec<f64>,
    adaptation_history: Vec<AdaptationStep>,
    n_features: usize,
    n_components: usize,
}

/// Records each step in the adaptation process
#[derive(Debug, Clone)]
pub struct AdaptationStep {
    /// levels
    pub levels: usize,
    /// scale_factors
    pub scale_factors: Vec<f64>,
    /// quality_score
    pub quality_score: f64,
    /// stress
    pub stress: f64,
    /// neighborhood_preservation
    pub neighborhood_preservation: f64,
}

/// Embedding quality metrics
#[derive(Debug, Clone)]
pub struct EmbeddingQuality {
    /// stress
    pub stress: f64,
    /// neighborhood_preservation
    pub neighborhood_preservation: f64,
    /// trustworthiness
    pub trustworthiness: f64,
    /// continuity
    pub continuity: f64,
}

impl Default for AdaptiveResolutionManifold<Untrained> {
    fn default() -> Self {
        Self::new()
    }
}

impl AdaptiveResolutionManifold<Untrained> {
    /// Create a new AdaptiveResolutionManifold instance
    pub fn new() -> Self {
        Self {
            state: Untrained,
            n_components: 2,
            max_levels: 7,
            min_levels: 2,
            base_method: "pca".to_string(),
            quality_threshold: 0.85,
            adaptation_method: "combined".to_string(),
            max_iterations: 10,
            learning_rate: 0.01,
            random_state: None,
        }
    }

    /// Set the number of components
    pub fn n_components(mut self, n_components: usize) -> Self {
        self.n_components = n_components;
        self
    }

    /// Set the maximum number of levels
    pub fn max_levels(mut self, max_levels: usize) -> Self {
        self.max_levels = max_levels;
        self
    }

    /// Set the minimum number of levels
    pub fn min_levels(mut self, min_levels: usize) -> Self {
        self.min_levels = min_levels;
        self
    }

    /// Set the base method
    pub fn base_method(mut self, base_method: String) -> Self {
        self.base_method = base_method;
        self
    }

    /// Set the quality threshold
    pub fn quality_threshold(mut self, quality_threshold: f64) -> Self {
        self.quality_threshold = quality_threshold;
        self
    }

    /// Set the adaptation method
    pub fn adaptation_method(mut self, adaptation_method: String) -> Self {
        self.adaptation_method = adaptation_method;
        self
    }

    /// Set the maximum iterations
    pub fn max_iterations(mut self, max_iterations: usize) -> Self {
        self.max_iterations = max_iterations;
        self
    }

    /// Set the learning rate
    pub fn learning_rate(mut self, learning_rate: f64) -> Self {
        self.learning_rate = learning_rate;
        self
    }

    /// Set the random state
    pub fn random_state(mut self, random_state: u64) -> Self {
        self.random_state = Some(random_state);
        self
    }

    /// Generate candidate configurations for adaptive search
    fn generate_candidate_configurations(
        &self,
        iteration: usize,
        n_samples: usize,
        rng: &mut StdRng,
    ) -> SklResult<Vec<(usize, Vec<f64>)>> {
        let mut candidates = Vec::new();

        // Progressive search: start with simple configurations, add complexity
        let levels_range = if iteration == 0 {
            self.min_levels..=(self.min_levels + 1)
        } else {
            self.min_levels..=self.max_levels.min(self.min_levels + iteration + 2)
        };

        for levels in levels_range {
            // Generate different scale factor strategies

            // 1. Linear progression
            let linear_scales: Vec<f64> = (0..levels)
                .map(|i| (i + 1) as f64 / levels as f64)
                .collect();
            candidates.push((levels, linear_scales));

            // 2. Exponential progression
            let exp_scales: Vec<f64> = (0..levels)
                .map(|i| (2_f64.powi(i as i32)) / (2_f64.powi(levels as i32 - 1)))
                .collect();
            candidates.push((levels, exp_scales));

            // 3. Logarithmic progression
            let log_scales: Vec<f64> = (0..levels)
                .map(|i| ((i + 1) as f64).ln() / (levels as f64).ln())
                .collect();
            candidates.push((levels, log_scales));

            // 4. Data-adaptive scales based on sample density
            if iteration > 0 {
                let adaptive_scales = self.compute_adaptive_scales(levels, n_samples)?;
                candidates.push((levels, adaptive_scales));
            }

            // 5. Random perturbations of best configurations (exploration)
            if iteration > 2 {
                let mut random_scales: Vec<f64> = (0..levels)
                    .map(|i| {
                        let base = (i + 1) as f64 / levels as f64;
                        let noise = rng.random_range(-0.2..0.2);
                        (base + noise).clamp(0.1, 1.0)
                    })
                    .collect();
                random_scales.sort_by(|a, b| a.partial_cmp(b).expect("operation should succeed"));
                candidates.push((levels, random_scales));
            }
        }

        Ok(candidates)
    }

    /// Compute adaptive scales based on data characteristics
    fn compute_adaptive_scales(&self, levels: usize, n_samples: usize) -> SklResult<Vec<f64>> {
        // Use sample density to guide scale selection
        // More samples allow for finer scales
        let density_factor = (n_samples as f64).ln() / 100.0; // Normalize by sample count

        let mut scales = Vec::new();
        for i in 0..levels {
            let base_scale = (i + 1) as f64 / levels as f64;
            let adapted_scale = base_scale * (1.0 + density_factor * (i as f64 / levels as f64));
            scales.push(adapted_scale.min(2.0)); // Cap at 2.0
        }

        Ok(scales)
    }

    /// Evaluate embedding quality using multiple metrics
    fn evaluate_embedding_quality(
        &self,
        x: &ArrayView2<f64>,
        embedding: &Array2<f64>,
    ) -> SklResult<EmbeddingQuality> {
        let stress = self.compute_stress(x, embedding)?;
        let neighborhood_preservation = self.compute_neighborhood_preservation(x, embedding)?;
        let trustworthiness = self.compute_trustworthiness(x, embedding)?;
        let continuity = self.compute_continuity(x, embedding)?;

        Ok(EmbeddingQuality {
            stress,
            neighborhood_preservation,
            trustworthiness,
            continuity,
        })
    }

    /// Compute normalized stress
    fn compute_stress(&self, x: &ArrayView2<f64>, embedding: &Array2<f64>) -> SklResult<f64> {
        let n_samples = x.nrows();
        let mut stress_num = 0.0;
        let mut stress_den = 0.0;

        for i in 0..n_samples {
            for j in (i + 1)..n_samples {
                let hd_dist = (&x.row(i) - &x.row(j)).mapv(|x: f64| x * x).sum().sqrt();
                let ld_dist = (&embedding.row(i) - &embedding.row(j))
                    .mapv(|x: f64| x * x)
                    .sum()
                    .sqrt();

                stress_num += (hd_dist - ld_dist).powi(2);
                stress_den += hd_dist.powi(2);
            }
        }

        let stress = if stress_den > 1e-10 {
            1.0 - (stress_num / stress_den).sqrt()
        } else {
            0.0
        };

        Ok(stress.clamp(0.0, 1.0))
    }

    /// Compute neighborhood preservation (K-ary neighborhood preservation)
    fn compute_neighborhood_preservation(
        &self,
        x: &ArrayView2<f64>,
        embedding: &Array2<f64>,
    ) -> SklResult<f64> {
        let n_samples = x.nrows();
        let k = (n_samples as f64).sqrt() as usize; // Adaptive k
        let mut preservation_sum = 0.0;

        for i in 0..n_samples {
            // Find k-NN in high-dimensional space
            let mut hd_distances: Vec<(usize, f64)> = (0..n_samples)
                .map(|j| (j, (&x.row(i) - &x.row(j)).mapv(|x: f64| x * x).sum().sqrt()))
                .collect();
            hd_distances.sort_by(|a, b| a.1.partial_cmp(&b.1).expect("operation should succeed"));
            let hd_neighbors: Vec<usize> = hd_distances
                .iter()
                .take(k + 1)
                .skip(1)
                .map(|(idx, _)| *idx)
                .collect();

            // Find k-NN in low-dimensional space
            let mut ld_distances: Vec<(usize, f64)> = (0..n_samples)
                .map(|j| {
                    (
                        j,
                        (&embedding.row(i) - &embedding.row(j))
                            .mapv(|x: f64| x * x)
                            .sum()
                            .sqrt(),
                    )
                })
                .collect();
            ld_distances.sort_by(|a, b| a.1.partial_cmp(&b.1).expect("operation should succeed"));
            let ld_neighbors: Vec<usize> = ld_distances
                .iter()
                .take(k + 1)
                .skip(1)
                .map(|(idx, _)| *idx)
                .collect();

            // Compute intersection
            let intersection_size = hd_neighbors
                .iter()
                .filter(|&neighbor| ld_neighbors.contains(neighbor))
                .count();

            preservation_sum += intersection_size as f64 / k as f64;
        }

        Ok(preservation_sum / n_samples as f64)
    }

    /// Compute trustworthiness metric
    fn compute_trustworthiness(
        &self,
        x: &ArrayView2<f64>,
        embedding: &Array2<f64>,
    ) -> SklResult<f64> {
        let n_samples = x.nrows();
        let k = (n_samples as f64).sqrt() as usize;
        let mut trustworthiness = 0.0;

        for i in 0..n_samples {
            // Find k-NN in embedding space
            let mut ld_distances: Vec<(usize, f64)> = (0..n_samples)
                .map(|j| {
                    (
                        j,
                        (&embedding.row(i) - &embedding.row(j))
                            .mapv(|x: f64| x * x)
                            .sum()
                            .sqrt(),
                    )
                })
                .collect();
            ld_distances.sort_by(|a, b| a.1.partial_cmp(&b.1).expect("operation should succeed"));
            let ld_neighbors: Vec<usize> = ld_distances
                .iter()
                .take(k + 1)
                .skip(1)
                .map(|(idx, _)| *idx)
                .collect();

            // Rank in high-dimensional space
            let mut hd_distances: Vec<(usize, f64)> = (0..n_samples)
                .map(|j| (j, (&x.row(i) - &x.row(j)).mapv(|x: f64| x * x).sum().sqrt()))
                .collect();
            hd_distances.sort_by(|a, b| a.1.partial_cmp(&b.1).expect("operation should succeed"));

            let mut rank_sum = 0.0;
            for &neighbor in &ld_neighbors {
                if let Some(pos) = hd_distances.iter().position(|(idx, _)| *idx == neighbor) {
                    if pos > k {
                        rank_sum += (pos - k) as f64;
                    }
                }
            }

            trustworthiness += 1.0
                - (2.0 / (n_samples as f64 * k as f64 * (2 * n_samples - 3 * k - 1) as f64))
                    * rank_sum;
        }

        Ok((trustworthiness / n_samples as f64).clamp(0.0, 1.0))
    }

    /// Compute continuity metric
    fn compute_continuity(&self, x: &ArrayView2<f64>, embedding: &Array2<f64>) -> SklResult<f64> {
        let n_samples = x.nrows();
        let k = (n_samples as f64).sqrt() as usize;
        let mut continuity = 0.0;

        for i in 0..n_samples {
            // Find k-NN in high-dimensional space
            let mut hd_distances: Vec<(usize, f64)> = (0..n_samples)
                .map(|j| (j, (&x.row(i) - &x.row(j)).mapv(|x: f64| x * x).sum().sqrt()))
                .collect();
            hd_distances.sort_by(|a, b| a.1.partial_cmp(&b.1).expect("operation should succeed"));
            let hd_neighbors: Vec<usize> = hd_distances
                .iter()
                .take(k + 1)
                .skip(1)
                .map(|(idx, _)| *idx)
                .collect();

            // Rank in embedding space
            let mut ld_distances: Vec<(usize, f64)> = (0..n_samples)
                .map(|j| {
                    (
                        j,
                        (&embedding.row(i) - &embedding.row(j))
                            .mapv(|x: f64| x * x)
                            .sum()
                            .sqrt(),
                    )
                })
                .collect();
            ld_distances.sort_by(|a, b| a.1.partial_cmp(&b.1).expect("operation should succeed"));

            let mut rank_sum = 0.0;
            for &neighbor in &hd_neighbors {
                if let Some(pos) = ld_distances.iter().position(|(idx, _)| *idx == neighbor) {
                    if pos > k {
                        rank_sum += (pos - k) as f64;
                    }
                }
            }

            continuity += 1.0
                - (2.0 / (n_samples as f64 * k as f64 * (2 * n_samples - 3 * k - 1) as f64))
                    * rank_sum;
        }

        Ok((continuity / n_samples as f64).clamp(0.0, 1.0))
    }

    /// Combine quality metrics into a single score
    fn compute_combined_quality(&self, quality: &EmbeddingQuality) -> SklResult<f64> {
        let combined = match self.adaptation_method.as_str() {
            "stress_based" => quality.stress,
            "neighborhood_preservation" => quality.neighborhood_preservation,
            "trustworthiness" => quality.trustworthiness,
            "continuity" => quality.continuity,
            "combined" => {
                // Weighted combination of all metrics
                0.3 * quality.stress
                    + 0.3 * quality.neighborhood_preservation
                    + 0.2 * quality.trustworthiness
                    + 0.2 * quality.continuity
            }
            _ => quality.stress, // Default fallback
        };

        Ok(combined)
    }
}

impl Estimator for AdaptiveResolutionManifold<Untrained> {
    type Config = ();
    type Error = SklearsError;
    type Float = f64;

    fn config(&self) -> &Self::Config {
        &()
    }
}

impl Fit<ArrayView2<'_, f64>, ArrayView1<'_, ()>> for AdaptiveResolutionManifold<Untrained> {
    type Fitted = AdaptiveResolutionManifold<TrainedAdaptiveResolutionManifold>;

    fn fit(self, x: &ArrayView2<'_, f64>, _y: &ArrayView1<'_, ()>) -> SklResult<Self::Fitted> {
        if x.nrows() == 0 || x.ncols() == 0 {
            return Err(SklearsError::InvalidInput(
                "Input data is empty".to_string(),
            ));
        }

        let n_samples = x.nrows();
        let n_features = x.ncols();

        let mut rng = if let Some(seed) = self.random_state {
            StdRng::seed_from_u64(seed)
        } else {
            StdRng::seed_from_u64(thread_rng().random::<u64>())
        };

        let mut adaptation_history = Vec::new();
        let mut best_levels = self.min_levels;
        let mut best_scale_factors = Vec::new();
        let mut best_quality = 0.0;
        let mut best_embeddings = Vec::new();
        let mut best_transforms = Vec::new();
        let mut best_quality_scores = Vec::new();

        // Adaptive search for optimal resolution configuration
        for iteration in 0..self.max_iterations {
            // Generate candidate configurations
            let candidates =
                self.generate_candidate_configurations(iteration, n_samples, &mut rng)?;

            for (levels, scale_factors) in candidates {
                // Build hierarchical manifold with current configuration
                let hierarchical = HierarchicalManifold::new()
                    .n_components(self.n_components)
                    .levels(levels)
                    .scale_factors(scale_factors.clone())
                    .base_method(self.base_method.clone())
                    .learning_rate(self.learning_rate)
                    .random_state(self.random_state.unwrap_or(42));

                let fitted = hierarchical.fit(x, _y)?;
                let embedding = fitted.transform(x)?;

                // Evaluate quality
                let quality_metrics = self.evaluate_embedding_quality(x, &embedding)?;
                let combined_quality = self.compute_combined_quality(&quality_metrics)?;

                // Record adaptation step
                let step = AdaptationStep {
                    levels,
                    scale_factors: scale_factors.clone(),
                    quality_score: combined_quality,
                    stress: quality_metrics.stress,
                    neighborhood_preservation: quality_metrics.neighborhood_preservation,
                };
                adaptation_history.push(step);

                // Update best configuration if improved
                if combined_quality > best_quality {
                    best_quality = combined_quality;
                    best_levels = levels;
                    best_scale_factors = scale_factors;
                    best_embeddings = fitted.state.level_embeddings;
                    best_transforms = fitted.state.level_transforms;
                    best_quality_scores = vec![combined_quality]; // Simplified
                }

                // Early stopping if quality threshold reached
                if combined_quality >= self.quality_threshold {
                    break;
                }
            }

            // Early stopping if quality threshold reached
            if best_quality >= self.quality_threshold {
                break;
            }
        }

        Ok(AdaptiveResolutionManifold {
            state: TrainedAdaptiveResolutionManifold {
                optimal_levels: best_levels,
                optimal_scale_factors: best_scale_factors,
                level_embeddings: best_embeddings,
                level_transforms: best_transforms,
                quality_scores: best_quality_scores,
                adaptation_history,
                n_features,
                n_components: self.n_components,
            },
            n_components: self.n_components,
            max_levels: self.max_levels,
            min_levels: self.min_levels,
            base_method: self.base_method,
            quality_threshold: self.quality_threshold,
            adaptation_method: self.adaptation_method,
            max_iterations: self.max_iterations,
            learning_rate: self.learning_rate,
            random_state: self.random_state,
        })
    }
}

impl Transform<ArrayView2<'_, f64>, Array2<f64>>
    for AdaptiveResolutionManifold<TrainedAdaptiveResolutionManifold>
{
    fn transform(&self, x: &ArrayView2<'_, f64>) -> SklResult<Array2<f64>> {
        if x.ncols() != self.state.n_features {
            return Err(SklearsError::InvalidInput(format!(
                "Expected {} features, got {}",
                self.state.n_features,
                x.ncols()
            )));
        }

        // Use the optimal configuration's final level transformation
        let final_transform = &self.state.level_transforms[self.state.optimal_levels - 1];
        let transformed = x.dot(final_transform);
        Ok(transformed)
    }
}

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

    #[test]
    fn test_hierarchical_manifold_basic() {
        let x = array![
            [1.0, 2.0, 3.0],
            [2.0, 3.0, 4.0],
            [8.0, 9.0, 10.0],
            [9.0, 10.0, 11.0]
        ];
        let dummy_y = array![(), (), (), ()];

        let hierarchical = HierarchicalManifold::new()
            .n_components(2)
            .levels(2)
            .base_method("pca".to_string())
            .random_state(42);

        let fitted = hierarchical
            .fit(&x.view(), &dummy_y.view())
            .expect("operation should succeed");
        let transformed = fitted
            .transform(&x.view())
            .expect("operation should succeed");

        assert_eq!(transformed.shape(), [4, 2]);
        assert!(transformed.iter().all(|&x| x.is_finite()));
    }

    #[test]
    fn test_multi_scale_embedding_basic() {
        let x = array![
            [1.0, 2.0, 3.0],
            [2.0, 3.0, 4.0],
            [8.0, 9.0, 10.0],
            [9.0, 10.0, 11.0]
        ];
        let dummy_y = array![(), (), (), ()];

        let multi_scale = MultiScaleEmbedding::new()
            .n_components(2)
            .scales(vec![0.5, 1.0])
            .base_method("pca".to_string())
            .combination_method("weighted".to_string())
            .random_state(42);

        let fitted = multi_scale
            .fit(&x.view(), &dummy_y.view())
            .expect("operation should succeed");
        let transformed = fitted
            .transform(&x.view())
            .expect("operation should succeed");

        assert_eq!(transformed.shape(), [4, 3]); // May differ based on combination method
        assert!(transformed.iter().all(|&x| x.is_finite()));
    }

    #[test]
    fn test_hierarchical_manifold_different_levels() {
        let x = array![[1.0, 2.0], [2.0, 3.0], [8.0, 9.0], [9.0, 10.0]];
        let dummy_y = array![(), (), (), ()];

        for levels in 1..=3 {
            let hierarchical = HierarchicalManifold::new()
                .n_components(2)
                .levels(levels)
                .refinement_steps(10)
                .random_state(42);

            let fitted = hierarchical
                .fit(&x.view(), &dummy_y.view())
                .expect("operation should succeed");
            let transformed = fitted
                .transform(&x.view())
                .expect("operation should succeed");

            assert_eq!(transformed.shape(), [4, 2]);
            assert!(transformed.iter().all(|&x| x.is_finite()));
        }
    }

    #[test]
    fn test_adaptive_resolution_manifold_basic() {
        let x = array![
            [1.0, 2.0, 3.0],
            [2.0, 3.0, 4.0],
            [8.0, 9.0, 10.0],
            [9.0, 10.0, 11.0]
        ];
        let dummy_y = array![(), (), (), ()];

        let adaptive = AdaptiveResolutionManifold::new()
            .n_components(2)
            .max_levels(3)
            .min_levels(2)
            .max_iterations(3) // Reduced for testing
            .quality_threshold(0.5) // Lower threshold for testing
            .adaptation_method("combined".to_string())
            .random_state(42);

        let fitted = adaptive
            .fit(&x.view(), &dummy_y.view())
            .expect("operation should succeed");
        let transformed = fitted
            .transform(&x.view())
            .expect("operation should succeed");

        assert_eq!(transformed.shape(), [4, 2]);
        assert!(transformed.iter().all(|&x| x.is_finite()));

        // Check that adaptive process found optimal configuration
        assert!(fitted.state.optimal_levels >= 2);
        assert!(fitted.state.optimal_levels <= 3);
        assert_eq!(
            fitted.state.optimal_scale_factors.len(),
            fitted.state.optimal_levels
        );
        assert!(!fitted.state.adaptation_history.is_empty());
    }

    #[test]
    fn test_adaptive_resolution_different_methods() {
        let x = array![[1.0, 2.0], [2.0, 3.0], [8.0, 9.0], [9.0, 10.0]];
        let dummy_y = array![(), (), (), ()];

        for method in &["stress_based", "neighborhood_preservation", "combined"] {
            let adaptive = AdaptiveResolutionManifold::new()
                .n_components(2)
                .max_levels(3)
                .max_iterations(2) // Reduced for testing
                .adaptation_method(method.to_string())
                .random_state(42);

            let fitted = adaptive
                .fit(&x.view(), &dummy_y.view())
                .expect("operation should succeed");
            let transformed = fitted
                .transform(&x.view())
                .expect("operation should succeed");

            assert_eq!(transformed.shape(), [4, 2]);
            assert!(transformed.iter().all(|&x| x.is_finite()));
        }
    }
}