manifolds-rs 0.3.3

Embedding methods implemented in Rust: (parametric) UMAP, tSNE, PHATE, Diffusion Map and PacMAP.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
//! Diffusion methods for PHATE and diffusion maps

use ann_search_rs::cpu::hnsw::{HnswIndex, HnswState};
use ann_search_rs::cpu::nndescent::{ApplySortedUpdates, NNDescent, NNDescentQuery};
use ann_search_rs::prelude::KMeansTrainingParams;
use ann_search_rs::utils::dist::{parse_ann_dist, Dist};
use ann_search_rs::utils::k_means_utils::{assign_all_parallel, train_centroids};
use faer::MatRef;
use faer_traits::ComplexField;
use num_traits::Float;
use rand::prelude::Distribution;
use rand::rngs::StdRng;
use rand::seq::index::sample;
use rand::seq::SliceRandom;
use rand::SeedableRng;
use rand_distr::weighted::WeightedIndex;
use rayon::prelude::*;
use rustc_hash::{FxBuildHasher, FxHashSet};
use std::ops::{AddAssign, Range};
use std::time::Instant;

use crate::data::graph::phate_alpha_decay_affinities;
use crate::data::structures::*;
use crate::prelude::*;
use crate::utils::math::*;
use crate::utils::sparse_ops::*;

/////////////
// Globals //
/////////////

/// Maximum iterations to use for powering the Markov transition matrix during
/// PHATE
pub const PHATE_MAX_T: usize = 100;

///////////
// PHATE //
///////////

////////////
// Params //
////////////

/// Parameters for the diffusion process
///
/// ### Fields
///
/// * `decay` - Decay exponent alpha (typical: 40). If None, returns binary
///   connectivity.
/// * `bandwidth_scale` - Multiplicative factor for bandwidth (default: 1.0)
/// * `thresh` - Threshold below which affinities are set to 0 (default: 1e-4,
///   for sparsity)
/// * `graph_symmetry` - symmetrisation method: "add" for (K+K^T)/2, "multiply"
///   for K*K^T, "none" for asymmetric.
/// * `n_landmarks` - Option to use landmarks. Set to something.
/// * `landmark_method` - String definining which landmark method to use.
/// * `n_svd` - Number of SVDs to use for the spectral clustering
#[derive(Debug, Clone)]
pub struct PhateDiffusionParams<T> {
    /// Decay exponent alpha (typical: 40). If None, returns binary
    /// connectivity.
    pub decay: Option<T>,
    /// Multiplicative factor for bandwidth (default: 1.0)
    pub bandwidth_scale: T,
    /// Threshold below which affinities are set to 0 (default: 1e-4, for
    /// sparsity)
    pub thresh: T,
    /// symmetrisation method: "add" for (K+K^T)/2, "multiply" for K*K^T,
    /// "none" for asymmetric.
    pub graph_symmetry: String,
    /// Option to use landmarks. Recommended to use for larger data sets.
    pub n_landmarks: Option<usize>,
    /// String definining which landmark method to use. Option of `"spectral"`,
    /// `"random"` or `"density"`.
    pub landmark_method: String,
    /// Number of SVDs to use for the spectral clustering
    pub n_svd: Option<usize>,
    /// Enum describing how to power the matrix. Auto or user-defined.
    pub t: PhateTime,
    /// Gamma parameter to control the informational distance between data
    /// points. Between `[-1.0, 1.0]`
    pub gamma: T,
}

impl<T> PhateDiffusionParams<T> {
    /// Generate new PhateDiffusionParams
    ///
    /// ### Params
    ///
    /// * `decay` - Decay exponent alpha (typical: 40). If None, returns binary
    ///   connectivity.
    /// * `bandwidth_scale` - Multiplicative factor for bandwidth (default: 1.0)
    /// * `thresh` - Threshold below which affinities are set to 0 (default: 1e-4,
    ///   for sparsity)
    /// * `graph_symmetry` - symmetrisation method: "add" for (K+K^T)/2,
    ///   "multiply"
    ///   for K*K^T, "none" for asymmetric.
    /// * `n_landmarks` - Option to use landmarks. Set to something.
    /// * `landmark_method` - String definining which landmark method to use.
    /// * `n_svd` - Number of SVDs to use for the spectral clustering.
    /// * `gamma` - To be written
    /// * `t_detection` -
    ///
    /// ### Returns
    ///
    /// Initialised self
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        decay: Option<T>,
        bandwidth_scale: T,
        thresh: T,
        graph_symmetry: String,
        n_landmarks: Option<usize>,
        landmark_method: String,
        n_svd: Option<usize>,
        t_max: Option<usize>,
        t_custom: Option<usize>,
        gamma: T,
    ) -> Self {
        let t_max = t_max.unwrap_or(PHATE_MAX_T);

        let t = parse_phate_time(t_custom, t_max);

        Self {
            decay,
            bandwidth_scale,
            thresh,
            graph_symmetry,
            n_landmarks,
            landmark_method,
            n_svd,
            t,
            gamma,
        }
    }
}

/// Build the row-stochastic Diffusion Operator P
///
/// Computes P = D^-1 * K, where D is the degree matrix (row sums).
/// This normalises the kernel so that each row sums to 1.0, representing
/// transition probabilities.
///
/// ### Params
///
/// * `matrix` - Input Kernel matrix in CSR format
///
/// ### Returns
///
/// Row-normalised CSR matrix
pub fn build_diffusion_operator<T>(matrix: &CompressedSparseData<T>) -> CompressedSparseData<T>
where
    T: ManifoldsFloat,
{
    if !matrix.cs_type.is_csr() {
        panic!("Diffusion operator requires CSR format input");
    }
    let (rows, _) = matrix.shape();
    // calculate degrees
    let degrees: Vec<T> = (0..rows)
        .into_par_iter()
        .map(|i| {
            let start = matrix.indptr[i];
            let end = matrix.indptr[i + 1];
            let mut sum = T::zero();
            for idx in start..end {
                sum += matrix.data[idx];
            }
            sum
        })
        .collect();

    // row-normalise the data
    let norm_data: Vec<T> = (0..rows)
        .into_par_iter()
        .flat_map(|i| {
            let start = matrix.indptr[i];
            let end = matrix.indptr[i + 1];
            let deg = degrees[i];

            let mut row_vals = Vec::with_capacity(end - start);

            if deg > T::zero() {
                for idx in start..end {
                    row_vals.push(matrix.data[idx] / deg);
                }
            } else {
                // Handle disconnected nodes / zero-degree rows
                // Usually keep as zero or set self-loop to 1?
                // PHATE generally assumes a connected component or handles zeros gracefully.
                for idx in start..end {
                    row_vals.push(matrix.data[idx]);
                }
            }
            row_vals
        })
        .collect();

    CompressedSparseData::new_csr(&norm_data, &matrix.indices, &matrix.indptr, matrix.shape())
}

////////////////////////
// Landmark diffusion //
////////////////////////

///////////
// Enums //
///////////

/// Enum representing different landmark diffusion methods.
pub enum PhateDiffusion<T>
where
    T: ComplexField + Float,
{
    /// Full diffusion using all nodes.
    Full {
        /// The full operator
        operator: CompressedSparseData<T>,
    },
    /// Landmark diffusion using a subset of nodes.
    Landmark {
        /// The landmark operator
        landmarks: PhateLandmarks<T>,
    },
}

/// Enum representing different time diffusion methods.
#[derive(Debug, Clone)]
pub enum PhateTime {
    /// Find optimal via VNE (default: 100)
    Auto {
        /// Maximum number of iterations to test
        t_max: usize,
    },
    /// Use specific t
    Fixed(usize),
}

/// Default implementation for PhateTime.
impl Default for PhateTime {
    fn default() -> Self {
        PhateTime::Auto { t_max: 100 }
    }
}

/// Parse the Phate t value based on the provided values
///
/// ### Params
///
/// * `t_custom` - If provided, it will use this value.
/// * `t_max` - Maximum number for auto-detection
///
/// ### Returns
///
/// PhateTime
pub fn parse_phate_time(t_custom: Option<usize>, t_max: usize) -> PhateTime {
    match t_custom {
        Some(t) => PhateTime::Fixed(t),
        None => PhateTime::Auto { t_max },
    }
}

/////////////
// Helpers //
/////////////

/// Enum representing different landmark diffusion methods.
#[derive(Debug, Clone)]
pub enum LandmarkMethod {
    /// Randomly select landmarks.
    Random {
        /// Seed for the randomised landmark selection
        seed: u64,
    },
    /// Use spectral clustering to select landmarks.
    Spectral {
        /// Number of PCs to use for spectral clustering
        n_svd: usize,
    },
    /// Density - leverage node degree
    Density {
        /// Seed for the randomised landmark selection
        seed: u64,
    },
}

impl Default for LandmarkMethod {
    /// Default to spectral with 100 PCs to calculate
    fn default() -> Self {
        LandmarkMethod::Spectral { n_svd: 100 }
    }
}

/// Type alias for a flattened matrix.
type FlattenData<T> = (Vec<T>, usize, usize);

/// Parse a string into a LandmarkMethod.
///
/// ### Params
///
/// * `s` - String to parse.
/// * `seed` - Optional seed for random landmark selection.
/// * `n_svd` - Optional number of singular values to use for spectral landmark
///   selection.
///
/// ### Returns
///
/// `Option<LandmarkMethod>` (which implements default if need be)
pub fn parse_landmark_method(
    s: &str,
    seed: Option<usize>,
    n_svd: Option<usize>,
) -> Option<LandmarkMethod> {
    let seed = seed.unwrap_or(42);
    let n_svd = n_svd.unwrap_or(10);

    match s.to_lowercase().as_str() {
        "random" => Some(LandmarkMethod::Random { seed: seed as u64 }),
        "spectral" => Some(LandmarkMethod::Spectral { n_svd }),
        "density" => Some(LandmarkMethod::Density { seed: seed as u64 }),
        _ => None,
    }
}

/// Flatten a matrix to a vector
///
/// ### Params
///
/// * `mat` - Matrix reference to flatten
///
/// ### Returns
///
/// The flatten vector
fn matrix_to_flat<T>(mat: MatRef<T>) -> FlattenData<T>
where
    T: Float,
{
    let n = mat.nrows();
    let dim = mat.ncols();

    let mut vectors_flat = Vec::with_capacity(n * dim);
    for i in 0..n {
        vectors_flat.extend(mat.row(i).iter().cloned());
    }

    (vectors_flat, n, dim)
}

/// Randomly sample indices from a range.
///
/// ### Params
///
/// * `range` - Range of indices to sample from.
/// * `n` - Number of indices to sample.
/// * `seed` - Seed for the random number generator.
///
/// ### Returns
///
/// A vector of sampled indices.
fn random_sample(range: Range<usize>, n: usize, seed: u64) -> Vec<usize> {
    let mut rng = StdRng::seed_from_u64(seed);
    sample(&mut rng, range.len(), n)
        .into_iter()
        .map(|i| range.start + i)
        .collect()
}

/// Assign a data point to the nearest landmark.
///
/// ### Params
///
/// * `data` - Data point to assign.
/// * `landmark_data` - Landmark data.
/// * `n_landmark` - Number of landmarks.
/// * `dim` - Dimensionality of the data.
///
/// ### Returns
///
/// Index of the nearest landmark.
fn assign_to_landmark<T>(
    data: &[T],
    norm_data: &T,
    landmark_data: &[T],
    norm_landmark: &[T],
    dist: &Dist,
    n_landmark: usize,
    dim: usize,
) -> usize
where
    T: ManifoldsFloat,
{
    let mut min_dist = T::infinity();
    let mut min_idx = 0;

    for idx in 0..n_landmark {
        let landmark = &landmark_data[idx * dim..(idx + 1) * dim];
        let dist = match dist {
            Dist::SquaredEuclidean => T::euclidean_simd(landmark, data),
            Dist::Cosine => {
                let dot = T::dot_simd(landmark, data);
                T::one() - (dot / (*norm_data * norm_landmark[idx]))
            }
            Dist::Manhattan => unreachable!(),
        };
        if dist < min_dist {
            min_dist = dist;
            min_idx = idx;
        }
    }

    min_idx
}

/// Build the row-stochastic Diffusion Operator P from the landmark data.
///
/// ### Params
///
/// * `diffusion_op`: The diffusion operator.
/// * `assignments`: The assignments of points to landmarks.
/// * `n_landmarks`: The number of landmarks.
/// * `n`: The number of points.
///
/// ### Returns
///
/// The row-stochastic Diffusion Operator P.
fn build_landmarks_to_data<T>(
    diffusion_op: &CompressedSparseData<T>,
    assignments: &[usize],
    n_landmarks: usize,
) -> CompressedSparseData<T>
where
    T: ManifoldsFloat,
{
    let n = assignments.len();
    let mut landmark_pts: Vec<Vec<usize>> = vec![Vec::new(); n_landmarks];
    for (point_idx, &landmark_idx) in assignments.iter().enumerate() {
        landmark_pts[landmark_idx].push(point_idx);
    }

    let rows: Vec<SparseRow<T>> = (0..n_landmarks)
        .into_par_iter()
        .map(|landmark_idx| sparse_row_sum(diffusion_op, &landmark_pts[landmark_idx]))
        .collect();

    sparse_row_to_csr(&rows, n)
}

/// Find the knee point in a curve using the method from PHATE
///
/// This identifies the "elbow" where the curve transitions from steep to flat.
///
/// ### Params
///
/// * `y` - The curve values (e.g., entropy at different t)
///
/// ### Returns
///
/// Index of the knee point
pub fn find_knee_point<T>(y: &[T]) -> usize
where
    T: Float,
{
    let n = y.len();

    if n < 3 {
        panic!("Cannot find knee point on vector of length < 3");
    }

    // use indices as x values
    let x: Vec<T> = (0..n).map(|i| T::from(i).unwrap()).collect();

    // compute cumulative sums for linear fits
    let mut sigma_x = Vec::with_capacity(n);
    let mut sigma_y = Vec::with_capacity(n);
    let mut sigma_xy = Vec::with_capacity(n);
    let mut sigma_xx = Vec::with_capacity(n);

    let mut sum_x = T::zero();
    let mut sum_y = T::zero();
    let mut sum_xy = T::zero();
    let mut sum_xx = T::zero();

    for i in 0..n {
        sum_x = sum_x + x[i];
        sum_y = sum_y + y[i];
        sum_xy = sum_xy + x[i] * y[i];
        sum_xx = sum_xx + x[i] * x[i];

        sigma_x.push(sum_x);
        sigma_y.push(sum_y);
        sigma_xy.push(sum_xy);
        sigma_xx.push(sum_xx);
    }

    // compute forward fits (left of knee)
    let mut mfwd = Vec::with_capacity(n - 1);
    let mut bfwd = Vec::with_capacity(n - 1);

    for i in 1..n {
        let n_points = T::from(i + 1).unwrap();
        let det = n_points * sigma_xx[i] - sigma_x[i] * sigma_x[i];

        if det.abs() > T::epsilon() {
            let m = (n_points * sigma_xy[i] - sigma_x[i] * sigma_y[i]) / det;
            let b = (sigma_xx[i] * sigma_y[i] - sigma_x[i] * sigma_xy[i]) / det;
            mfwd.push(m);
            bfwd.push(b);
        } else {
            mfwd.push(T::zero());
            bfwd.push(y[0]);
        }
    }

    // compute backward fits (right of knee) by reversing
    let x_rev: Vec<T> = x.iter().rev().copied().collect();
    let y_rev: Vec<T> = y.iter().rev().copied().collect();

    let mut sigma_x_rev = Vec::with_capacity(n);
    let mut sigma_y_rev = Vec::with_capacity(n);
    let mut sigma_xy_rev = Vec::with_capacity(n);
    let mut sigma_xx_rev = Vec::with_capacity(n);

    sum_x = T::zero();
    sum_y = T::zero();
    sum_xy = T::zero();
    sum_xx = T::zero();

    for i in 0..n {
        sum_x = sum_x + x_rev[i];
        sum_y = sum_y + y_rev[i];
        sum_xy = sum_xy + x_rev[i] * y_rev[i];
        sum_xx = sum_xx + x_rev[i] * x_rev[i];

        sigma_x_rev.push(sum_x);
        sigma_y_rev.push(sum_y);
        sigma_xy_rev.push(sum_xy);
        sigma_xx_rev.push(sum_xx);
    }

    let mut mbck = Vec::with_capacity(n - 1);
    let mut bbck = Vec::with_capacity(n - 1);

    for i in 1..n {
        let n_points = T::from(i + 1).unwrap();
        let det = n_points * sigma_xx_rev[i] - sigma_x_rev[i] * sigma_x_rev[i];

        if det.abs() > T::epsilon() {
            let m = (n_points * sigma_xy_rev[i] - sigma_x_rev[i] * sigma_y_rev[i]) / det;
            let b = (sigma_xx_rev[i] * sigma_y_rev[i] - sigma_x_rev[i] * sigma_xy_rev[i]) / det;
            mbck.push(m);
            bbck.push(b);
        } else {
            mbck.push(T::zero());
            bbck.push(y_rev[0]);
        }
    }

    mbck.reverse();
    bbck.reverse();

    // Compute error for each potential breakpoint
    let mut error_curve = vec![T::infinity(); n];

    for breakpt in 1..n - 1 {
        let mut error = T::zero();

        // Error from left fit
        for i in 0..=breakpt {
            let predicted = mfwd[breakpt - 1] * x[i] + bfwd[breakpt - 1];
            error = error + (predicted - y[i]).abs();
        }

        // error from right fit
        for i in breakpt..n {
            let predicted = mbck[breakpt - 1] * x[i] + bbck[breakpt - 1];
            error = error + (predicted - y[i]).abs();
        }

        error_curve[breakpt] = error;
    }

    // find minimum error
    let mut min_idx = 1;
    let mut min_error = error_curve[1];

    for (i, &err) in error_curve.iter().enumerate().skip(1).take(n - 2) {
        if err < min_error {
            min_error = err;
            min_idx = i;
        }
    }

    min_idx
}

////////////////////
// PhateLandmarks //
////////////////////

/// Struct representing PhateLandmarks.
#[allow(dead_code)]
pub struct PhateLandmarks<T>
where
    T: Float + ComplexField,
{
    /// Number of landmarks to select.
    n_landmarks: usize,
    /// Method to use for landmark selection.
    method: LandmarkMethod,
    /// Data point → landmark mapping.
    assignments: Vec<usize>,
    /// Small L matrix.
    landmark_op: CompressedSparseData<T>,
    /// P_nm for interpolation.
    transitions: CompressedSparseData<T>,
}

impl<T> PhateLandmarks<T>
where
    T: ManifoldsFloat,
{
    /// Build landmarks from an existing diffusion operator
    ///
    /// ### Params
    ///
    /// * `data` - Original data (N × features)
    /// * `affinity` - Original affinity matrix (N x N) - not yet normalised.
    /// * `diffusion_op` - The P matrix (N × N) already built and normalised.
    /// * `n_landmarks` - Number of landmarks to use
    /// * `method` - Random or Spectral
    /// * `distance` - Distance metric used
    /// * `seed` - Seed for random number generator
    /// * `n_svd` - Number of SVD components to use
    /// * `verbose` - If `0` -> silent or `1` for normal verbosity, `2` for
    ///   detailed verbosity.
    ///
    /// ### Returns
    ///
    /// PhateLandmarks structure ready for powering
    #[allow(clippy::too_many_arguments)]
    pub fn build(
        data: MatRef<T>,
        affinity: &CompressedSparseData<T>,
        diffusion_op: &CompressedSparseData<T>,
        n_landmarks: usize,
        method: &str,
        distance: &str,
        seed: usize,
        n_svd: Option<usize>,
        verbose: usize,
    ) -> Result<Self, ManifoldsError> {
        let verbosity = parse_verbosity_level(verbose);

        let (data, n, dim) = matrix_to_flat(data);
        let landmark_method = parse_landmark_method(method, Some(seed), n_svd).unwrap_or_default();
        let distance = parse_ann_dist(distance).unwrap_or_default();

        let assignments: Vec<usize> = match landmark_method {
            LandmarkMethod::Random { seed } => {
                if verbosity.normal_verbosity() {
                    println!(" Using random selection of landmarks.")
                }

                let landmark_indices = random_sample(0..n, n_landmarks, seed);

                let landmark_data: Vec<T> = landmark_indices
                    .iter()
                    .flat_map(|&i| data[i * dim..(i + 1) * dim].iter().copied())
                    .collect();

                // calculate norms if distance is cosine
                let norm_data = match distance {
                    Dist::Cosine => (0..n)
                        .into_par_iter()
                        .map(|i| T::calculate_l2_norm(&data[i * dim..(i + 1) * dim]))
                        .collect::<Vec<_>>(),
                    Dist::SquaredEuclidean => Vec::new(),
                    Dist::Manhattan => {
                        unreachable!("PHATE Landmark does not support Manhattan distance.")
                    }
                };
                let norm_landmark = match distance {
                    Dist::Cosine => (0..n_landmarks)
                        .into_par_iter()
                        .map(|i| T::calculate_l2_norm(&landmark_data[i * dim..(i + 1) * dim]))
                        .collect::<Vec<_>>(),
                    Dist::SquaredEuclidean => Vec::new(),
                    Dist::Manhattan => {
                        unreachable!("PHATE Landmark does not support Manhattan distance.")
                    }
                };

                (0..n)
                    .into_par_iter()
                    .map(|i| {
                        let norm = if matches!(distance, Dist::SquaredEuclidean) {
                            T::zero() // unused by assign_to_landmark for Euclidean
                        } else {
                            norm_data[i]
                        };
                        assign_to_landmark(
                            &data[i * dim..(i + 1) * dim],
                            &norm,
                            &landmark_data,
                            &norm_landmark,
                            &distance,
                            n_landmarks,
                            dim,
                        )
                    })
                    .collect()
            }
            #[allow(unused_variables)]
            LandmarkMethod::Spectral { n_svd } => {
                if verbosity.normal_verbosity() {
                    println!(" Using spectral detection of landmarks.")
                }

                let svd = sparse_randomised_svd(affinity, n_svd, seed as u64, None, None)?;

                if verbosity.detailed_verbosity() {
                    println!(" Finished calculation of randomised SVD on the affinity matrix.")
                }

                let v = &svd.v;
                let k = v.ncols();
                let mut embedding_flat: Vec<T> = vec![T::zero(); n * k];

                for i in 0..n {
                    for idx in diffusion_op.indptr[i]..diffusion_op.indptr[i + 1] {
                        let j = diffusion_op.indices[idx];
                        let a_val = diffusion_op.data[idx];
                        for col in 0..k {
                            embedding_flat[i * k + col] += a_val * v[(j, col)];
                        }
                    }
                }

                // reduce the number of iterations here...
                // doesn't have to be perfect
                let k_means_params = KMeansTrainingParams::new(30, None, None);

                // use ann-search-rs k-means-clustering
                let centroids = train_centroids(
                    &embedding_flat,
                    k,
                    n,
                    n_landmarks,
                    &Dist::SquaredEuclidean,
                    Some(k_means_params),
                    seed,
                    verbosity.detailed_verbosity(),
                )?;

                if verbosity.detailed_verbosity() {
                    println!(" Centroids identified.")
                }

                let centroid_norms = vec![T::one(); n_landmarks]; // Euclidean, unused
                let data_norms = vec![T::one(); n];

                let assignemnts = assign_all_parallel(
                    &embedding_flat,
                    &data_norms,
                    k,
                    n,
                    &centroids,
                    &centroid_norms,
                    n_landmarks,
                    &Dist::SquaredEuclidean,
                );

                if verbosity.normal_verbosity() {
                    println!(" Landmark assignments done.")
                }

                assignemnts
            }
            LandmarkMethod::Density { seed } => {
                if verbosity.normal_verbosity() {
                    println!(" Using degree-weighted (density) selection of landmarks.")
                }

                // calculate weights
                let weights: Vec<f64> = (0..n)
                    .into_par_iter()
                    .map(|i| {
                        let start = affinity.indptr[i];
                        let end = affinity.indptr[i + 1];
                        let mut sum = T::zero();
                        for idx in start..end {
                            sum += affinity.data[idx];
                        }

                        // damping prevents massive clusters from hogging all
                        // the landmarks, ensuring rare branches/trajectories
                        // still get sampled. neat trick over just degree-based
                        // sampling
                        sum.to_f64().unwrap_or(0.0).sqrt()
                    })
                    .collect();

                let mut rng = StdRng::seed_from_u64(seed);
                let dist = WeightedIndex::new(&weights).expect("Failed to create weighted index. Check affinity matrix for negative/NaN values.");

                // generate exactly n_landmarks
                let mut landmark_set =
                    FxHashSet::with_capacity_and_hasher(n_landmarks, FxBuildHasher);
                while landmark_set.len() < n_landmarks {
                    landmark_set.insert(dist.sample(&mut rng));
                }

                let landmark_indices: Vec<usize> = landmark_set.into_iter().collect();

                // extract the data
                let landmark_data: Vec<T> = landmark_indices
                    .iter()
                    .flat_map(|&i| data[i * dim..(i + 1) * dim].iter().copied())
                    .collect();

                // norms
                let norm_data = match distance {
                    Dist::Cosine => (0..n)
                        .into_par_iter()
                        .map(|i| T::calculate_l2_norm(&data[i * dim..(i + 1) * dim]))
                        .collect::<Vec<_>>(),
                    Dist::SquaredEuclidean => Vec::new(),
                    Dist::Manhattan => {
                        unreachable!("PHATE Landmark does not support Manhattan distance.")
                    }
                };
                let norm_landmark = match distance {
                    Dist::Cosine => (0..n_landmarks)
                        .into_par_iter()
                        .map(|i| T::calculate_l2_norm(&landmark_data[i * dim..(i + 1) * dim]))
                        .collect::<Vec<_>>(),
                    Dist::SquaredEuclidean => Vec::new(),
                    Dist::Manhattan => {
                        unreachable!("PHATE Landmark does not support Manhattan distance.")
                    }
                };

                // assign
                (0..n)
                    .into_par_iter()
                    .map(|i| {
                        let norm = if matches!(distance, Dist::SquaredEuclidean) {
                            T::zero() // unused by assign_to_landmark for Euclidean
                        } else {
                            norm_data[i]
                        };
                        assign_to_landmark(
                            &data[i * dim..(i + 1) * dim],
                            &norm,
                            &landmark_data,
                            &norm_landmark,
                            &distance,
                            n_landmarks,
                            dim,
                        )
                    })
                    .collect()
            }
        };

        let mut p_mn = build_landmarks_to_data(diffusion_op, &assignments, n_landmarks);
        let mut p_nm = p_mn.transpose();

        normalise_csr_rows_l1(&mut p_mn)?;
        normalise_csr_rows_l1(&mut p_nm)?;

        let landmark_op = csr_matmul_csr(&p_mn, &p_nm)?;

        Ok(Self {
            n_landmarks,
            method: landmark_method,
            assignments,
            landmark_op,
            transitions: p_nm,
        })
    }

    /// Power the landmark operator t times
    ///
    /// ### Params
    ///
    /// * `t` - Number of diffusion steps
    ///
    /// ### Returns
    ///
    /// L^t (n_landmarks × n_landmarks matrix)
    pub fn power(&self, t: usize) -> Result<CompressedSparseData<T>, ManifoldsError>
    where
        T: AddAssign,
    {
        if t == 0 {
            // Return identity matrix
            unimplemented!("Return identity for t = 0");
        } else if t == 1 {
            return Ok(self.landmark_op.clone());
        }

        matrix_power(&self.landmark_op, t)
    }

    /// Compute diffusion at optimal time
    ///
    /// ### Params
    ///
    /// * `t_max` - Maximum time to search for knee point
    ///
    /// ### Returns
    ///
    /// P^t at optimal t
    pub fn power_optimal(&self, t_max: usize) -> Result<CompressedSparseData<T>, ManifoldsError>
    where
        T: AddAssign,
    {
        let t_opt = self.find_optimal_t(t_max)?;
        let res = matrix_power(&self.landmark_op, t_opt)?;
        Ok(res)
    }

    /// Interpolate landmark diffusion back to full data space
    ///
    /// Computes P^t ≈ P_nm × L^t
    ///
    /// ### Params
    ///
    /// * `landmark_diffusion` - L^t (n_landmarks × n_landmarks)
    ///
    /// ### Returns
    ///
    /// Full diffusion operator P^t (N × n_landmarks)
    pub fn interpolate(
        &self,
        landmark_diffusion: &CompressedSparseData<T>,
    ) -> Result<CompressedSparseData<T>, ManifoldsError>
    where
        T: AddAssign,
    {
        // P^t ≈ P_nm × L^t
        // (N × n_landmarks) × (n_landmarks × n_landmarks) = (N × n_landmarks)
        csr_matmul_csr(&self.transitions, landmark_diffusion)
    }

    /// Determine optimal diffusion time using Von Neumann entropy
    ///
    /// ### Params
    ///
    /// * `t_max` - Maximum time to search
    ///
    /// ### Returns
    ///
    /// Optimal t value (knee point of entropy curve)
    pub fn find_optimal_t(&self, t_max: usize) -> Result<usize, ManifoldsError> {
        let entropy = landmark_von_neumann_entropy(&self.landmark_op, t_max)?;
        Ok(find_knee_point(&entropy))
    }

    /// Interpolate embedding from landmark embedding
    ///
    /// ### Params
    ///
    /// * `landmark_embedding` - The embedding calculated on the landmark
    ///   transition matrix
    ///
    /// ### Returns
    ///
    /// Interpolated embedding
    pub fn interpolate_embedding(&self, landmark_embedding: &[Vec<T>]) -> Vec<Vec<T>>
    where
        T: AddAssign,
    {
        let n = self.transitions.shape().0;
        let n_dim = landmark_embedding[0].len();
        let mut embedding = vec![vec![T::zero(); n_dim]; n];

        for i in 0..n {
            let start = self.transitions.indptr[i];
            let end = self.transitions.indptr[i + 1];
            for idx in start..end {
                let l = self.transitions.indices[idx];
                let w = self.transitions.data[idx];
                for d in 0..n_dim {
                    embedding[i][d] += w * landmark_embedding[l][d];
                }
            }
        }

        embedding
    }

    /// Get the numbers of landmarks
    pub fn get_n_landmarks(&self) -> usize {
        self.n_landmarks
    }
}

///////////////////
// Diffusion map //
///////////////////

/// Apply anisotropic (alpha) normalisation to a symmetric kernel:
///
/// `K'_ij = K_ij / (q_i^alpha * q_j^alpha),  q_i = sum_j K_ij.`
///
/// alpha = 0 leaves the kernel unchanged (normalised graph Laplacian).
/// alpha = 0.5 yields a Fokker-Planck-like operator.
/// alpha = 1 yields the Laplace-Beltrami operator, invariant to sampling
/// density -> the standard choice for diffusion maps.
///
/// ### Params
///
/// * `kernel` - The kernel matrix
/// * `alpha` - The alpha parameter
///
/// ### Returns
///
/// The anisotropic normalised kernel
pub fn apply_anisotropic_normalisation<T>(
    kernel: &CompressedSparseData<T>,
    alpha: T,
) -> Result<CompressedSparseData<T>, ManifoldsError>
where
    T: ManifoldsFloat,
{
    if !kernel.cs_type.is_csr() {
        return Err(ManifoldsError::SparseMatrixIsNotCsr);
    }

    if alpha.abs() < T::epsilon() {
        return Ok(kernel.clone());
    }

    let (nrows, _) = kernel.shape();

    let q: Vec<T> = (0..nrows)
        .into_par_iter()
        .map(|i| {
            let start = kernel.indptr[i];
            let end = kernel.indptr[i + 1];
            kernel.data[start..end].iter().copied().sum()
        })
        .collect();

    let q_alpha: Vec<T> = q.par_iter().map(|&qi| qi.powf(alpha)).collect();

    let data: Vec<T> = (0..nrows)
        .into_par_iter()
        .flat_map(|i| {
            let start = kernel.indptr[i];
            let end = kernel.indptr[i + 1];
            (start..end)
                .map(|idx| {
                    let j = kernel.indices[idx];
                    let denom = q_alpha[i] * q_alpha[j];
                    if denom > T::zero() {
                        kernel.data[idx] / denom
                    } else {
                        T::zero()
                    }
                })
                .collect::<Vec<T>>()
        })
        .collect();

    Ok(CompressedSparseData::new_csr(
        &data,
        &kernel.indices,
        &kernel.indptr,
        kernel.shape(),
    ))
}

/// Build the symmetric diffusion operator P_sym = D^{-1/2} K D^{-1/2}.
///
/// P_sym shares eigenvalues with the row-stochastic operator `P = D^{-1} K`;
/// right eigenvectors of P are recovered via `phi[i] = u[i] / sqrt(d_i)`,
/// where u are the symmetric eigenvectors.
///
/// ### Params
///
/// * `kernel` - The kernel matrix
///
/// ### Returns
///
/// `(P_sym, sqrt(degrees))`
pub fn build_symmetric_diffusion_operator<T>(
    kernel: &CompressedSparseData<T>,
) -> Result<(CompressedSparseData<T>, Vec<T>), ManifoldsError>
where
    T: ManifoldsFloat,
{
    if !kernel.cs_type.is_csr() {
        return Err(ManifoldsError::SparseMatrixIsNotCsr);
    }

    let (nrows, _) = kernel.shape();

    let degrees: Vec<T> = (0..nrows)
        .into_par_iter()
        .map(|i| {
            let start = kernel.indptr[i];
            let end = kernel.indptr[i + 1];
            kernel.data[start..end].iter().copied().sum()
        })
        .collect();

    let sqrt_d: Vec<T> = degrees.par_iter().map(|&d| d.sqrt()).collect();

    let data: Vec<T> = (0..nrows)
        .into_par_iter()
        .flat_map(|i| {
            let start = kernel.indptr[i];
            let end = kernel.indptr[i + 1];
            let sqrt_di = sqrt_d[i];
            (start..end)
                .map(|idx| {
                    let j = kernel.indices[idx];
                    let denom = sqrt_di * sqrt_d[j];
                    if denom > T::zero() {
                        kernel.data[idx] / denom
                    } else {
                        T::zero()
                    }
                })
                .collect::<Vec<T>>()
        })
        .collect();

    let p_sym =
        CompressedSparseData::new_csr(&data, &kernel.indices, &kernel.indptr, kernel.shape());

    Ok((p_sym, sqrt_d))
}

/// For each data point, compute Gaussian-weighted transitions to its k
/// nearest landmarks and row-normalise. Used as P_nl in Nystroem extension.
///
/// Bandwidth is set to the distance to the k-th nearest landmark, so sparse
/// rows are possible if few landmarks survive the threshold cut.
///
/// ### Params
///
/// * `data` - Flat N×dim data array
/// * `n` - Number of data points
/// * `landmark_data` - Flat n_landmarks×dim landmark coordinates
/// * `n_landmarks` - Number of landmarks
/// * `dim` - Feature dimensionality
/// * `k` - Number of nearest landmarks to connect each point to
/// * `bandwidth_scale` - Multiplicative factor applied to the bandwidth
/// * `thresh` - Minimum affinity weight; entries below this are dropped
/// * `distance` - Distance metric
///
/// ### Returns
///
/// Row-stochastic CSR matrix of shape (N, n_landmarks)
#[allow(clippy::too_many_arguments)]
fn build_data_to_landmark_transitions<T>(
    data: &[T],
    n: usize,
    landmark_data: &[T],
    n_landmarks: usize,
    dim: usize,
    k: usize,
    bandwidth_scale: T,
    thresh: T,
    distance: &Dist,
) -> CompressedSparseData<T>
where
    T: ManifoldsFloat,
{
    let k_used = k.min(n_landmarks).max(1);
    let machine_epsilon = T::epsilon();

    let data_norms: Vec<T> = match distance {
        Dist::Cosine => (0..n)
            .into_par_iter()
            .map(|i| T::calculate_l2_norm(&data[i * dim..(i + 1) * dim]))
            .collect(),
        Dist::SquaredEuclidean => Vec::new(),
        Dist::Manhattan => unreachable!(),
    };
    let landmark_norms: Vec<T> = match distance {
        Dist::Cosine => (0..n_landmarks)
            .into_par_iter()
            .map(|l| T::calculate_l2_norm(&landmark_data[l * dim..(l + 1) * dim]))
            .collect(),
        Dist::SquaredEuclidean => Vec::new(),
        Dist::Manhattan => unreachable!(),
    };

    let bw_factor = match distance {
        Dist::SquaredEuclidean => bandwidth_scale * bandwidth_scale,
        Dist::Cosine => bandwidth_scale,
        Dist::Manhattan => unreachable!(),
    };

    let rows: Vec<(Vec<usize>, Vec<T>)> = (0..n)
        .into_par_iter()
        .map(|i| {
            let x = &data[i * dim..(i + 1) * dim];

            let mut dists: Vec<(usize, T)> = (0..n_landmarks)
                .map(|l| {
                    let y = &landmark_data[l * dim..(l + 1) * dim];
                    let d = match distance {
                        Dist::SquaredEuclidean => T::euclidean_simd(x, y),
                        Dist::Cosine => {
                            let dot = T::dot_simd(x, y);
                            let denom = data_norms[i] * landmark_norms[l];
                            if denom > T::zero() {
                                T::one() - (dot / denom)
                            } else {
                                T::zero()
                            }
                        }
                        Dist::Manhattan => unreachable!(),
                    };
                    (l, d)
                })
                .collect();

            dists.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
            dists.truncate(k_used);

            let bandwidth_raw = dists.last().map(|(_, d)| *d).unwrap_or(T::zero());
            let bandwidth = (bandwidth_raw * bw_factor).max(machine_epsilon);

            let mut kept: Vec<(usize, T)> = dists
                .into_iter()
                .filter_map(|(l, d)| {
                    let w = match distance {
                        Dist::SquaredEuclidean => (-(d / bandwidth)).exp(),
                        Dist::Cosine => {
                            let scaled = d / bandwidth;
                            (-(scaled * scaled)).exp()
                        }
                        Dist::Manhattan => unreachable!(),
                    };
                    if w >= thresh {
                        Some((l, w))
                    } else {
                        None
                    }
                })
                .collect();

            let sum: T = kept.iter().map(|(_, w)| *w).sum();
            if sum > T::zero() {
                for (_, w) in &mut kept {
                    *w /= sum;
                }
            }

            kept.sort_by_key(|(l, _)| *l);
            let indices: Vec<usize> = kept.iter().map(|(l, _)| *l).collect();
            let values: Vec<T> = kept.iter().map(|(_, w)| *w).collect();
            (indices, values)
        })
        .collect();

    let total_nnz: usize = rows.iter().map(|(i, _)| i.len()).sum();
    let mut out_data = Vec::with_capacity(total_nnz);
    let mut out_indices = Vec::with_capacity(total_nnz);
    let mut out_indptr = Vec::with_capacity(n + 1);
    out_indptr.push(0);

    for (idx, vals) in rows {
        out_data.extend(vals);
        out_indices.extend(idx);
        out_indptr.push(out_data.len());
    }

    CompressedSparseData::new_csr(&out_data, &out_indices, &out_indptr, (n, n_landmarks))
}

/// Landmark diffusion maps operator.
///
/// Builds a symmetric Gaussian kernel on landmark coordinates, applies
/// anisotropic normalisation, constructs P_sym on landmarks, and stores a
/// row-stochastic data-to-landmark transition matrix for Nystroem extension.
///
/// Use `eigendecompose` → `compute_landmark_embedding` → `nystrom_extend`
/// in sequence to obtain a full-data diffusion embedding.
#[allow(dead_code)]
pub struct DiffusionMapsLandmarks<T>
where
    T: Float + ComplexField,
{
    /// Number of landmarks selected
    n_landmarks: usize,
    /// Landmark selection method used
    method: LandmarkMethod,
    /// Indices into the original N data points identifying each landmark
    landmark_indices: Vec<usize>,
    /// Symmetric L×L diffusion operator on landmarks: D^{-1/2} K D^{-1/2}
    p_sym_ll: CompressedSparseData<T>,
    /// Square-root of landmark node degrees; used to recover right eigenvectors
    /// of the row-stochastic operator from the symmetric eigenvectors
    sqrt_degrees_l: Vec<T>,
    /// Row-stochastic N×L transition matrix for Nystroem extension
    p_nl: CompressedSparseData<T>,
}

impl<T> DiffusionMapsLandmarks<T>
where
    T: ManifoldsFloat,
{
    /// Build the landmark diffusion maps operator.
    ///
    /// Selects landmarks, constructs a symmetric Gaussian kernel on them,
    /// applies anisotropic normalisation, and builds the Nystroem transition
    /// matrix. The `affinity` matrix is only used for `"spectral"` and
    /// `"density"` landmark selection; `"random"` ignores it.
    ///
    /// ### Params
    ///
    /// * `data` - Original N×features data matrix
    /// * `affinity` - Full N×N Gaussian kernel in CSR format
    /// * `n_landmarks` - Number of landmarks to select
    /// * `method` - Landmark selection method: `"random"`, `"spectral"`, or
    ///   `"density"`
    /// * `distance` - Distance metric: `"euclidean"` or `"cosine"`
    /// * `alpha_norm` - Anisotropic normalisation exponent (0 = none,
    ///   1 = Laplace-Beltrami)
    /// * `k` - Number of nearest neighbours for graph construction and Nystroem
    ///   transitions
    /// * `bandwidth_scale` - Multiplicative factor applied to the kernel
    ///   bandwidth
    /// * `thresh` - Minimum affinity; entries below this are dropped for
    ///   sparsity
    /// * `graph_symmetry` - Symmetrisation method: `"add"`, `"multiply"`, or
    ///   `"none"`
    /// * `seed` - RNG seed
    /// * `n_svd` - Number of SVD components for spectral landmark selection
    /// * `verbose` - If `0` -> silent or `1` for normal verbosity, `2` for
    ///   detailed verbosity.
    ///
    /// ### Returns
    ///
    /// Initialised `DiffusionMapsLandmarks`
    #[allow(clippy::too_many_arguments)]
    pub fn build(
        data: MatRef<T>,
        affinity: Option<&CompressedSparseData<T>>,
        n_landmarks: usize,
        method: &str,
        distance: &str,
        alpha_norm: T,
        k: usize,
        bandwidth_scale: T,
        thresh: T,
        graph_symmetry: &str,
        seed: usize,
        n_svd: Option<usize>,
        verbose: usize,
    ) -> Result<Self, ManifoldsError>
    where
        HnswIndex<T>: HnswState<T>,
        NNDescent<T>: ApplySortedUpdates<T> + NNDescentQuery<T>,
    {
        let verbosity = parse_verbosity_level(verbose);

        let start = Instant::now();

        let (data_flat, n, dim) = matrix_to_flat(data);
        let landmark_method = parse_landmark_method(method, Some(seed), n_svd).unwrap_or_default();
        let dist_enum = parse_ann_dist(distance).unwrap_or_default();

        // landmark selection
        let landmark_indices: Vec<usize> = match landmark_method {
            LandmarkMethod::Random { seed } => {
                if verbosity.normal_verbosity() {
                    println!(" Using random landmark selection.");
                }
                random_sample(0..n, n_landmarks, seed)
            }
            LandmarkMethod::Density { seed } => {
                if verbosity.normal_verbosity() {
                    println!(" Using density landmark selection.");
                }
                let affinity = affinity.expect("density landmarks require affinity matrix");
                let weights: Vec<f64> = (0..n)
                    .into_par_iter()
                    .map(|i| {
                        let start = affinity.indptr[i];
                        let end = affinity.indptr[i + 1];
                        let mut sum = T::zero();
                        for idx in start..end {
                            sum += affinity.data[idx];
                        }
                        sum.to_f64().unwrap_or(0.0).sqrt()
                    })
                    .collect();
                let mut rng = StdRng::seed_from_u64(seed);
                let weighted = WeightedIndex::new(&weights)
                    .expect("Failed to create weighted index for density landmarks");
                let mut set = FxHashSet::with_capacity_and_hasher(n_landmarks, FxBuildHasher);
                while set.len() < n_landmarks {
                    set.insert(weighted.sample(&mut rng));
                }
                set.into_iter().collect()
            }
            LandmarkMethod::Spectral { n_svd } => {
                if verbosity.normal_verbosity() {
                    println!(" Using spectral landmark selection.");
                }
                let affinity = affinity.expect("spectral landmarks require affinity matrix");
                let svd = sparse_randomised_svd(affinity, n_svd, seed as u64, None, None)?;
                let v = &svd.v;
                let k_proj = v.ncols();
                let mut embedding_flat: Vec<T> = vec![T::zero(); n * k_proj];

                for i in 0..n {
                    for idx in affinity.indptr[i]..affinity.indptr[i + 1] {
                        let j = affinity.indices[idx];
                        let a_val = affinity.data[idx];
                        for col in 0..k_proj {
                            embedding_flat[i * k_proj + col] += a_val * v[(j, col)];
                        }
                    }
                }

                let k_means_params = KMeansTrainingParams::new(30, None, None);

                let centroids = train_centroids(
                    &embedding_flat,
                    k_proj,
                    n,
                    n_landmarks,
                    &Dist::SquaredEuclidean,
                    Some(k_means_params),
                    seed,
                    verbosity.detailed_verbosity(),
                )?;
                let centroid_norms = vec![T::one(); n_landmarks];
                let data_norms = vec![T::one(); n];
                let assignments = assign_all_parallel(
                    &embedding_flat,
                    &data_norms,
                    k_proj,
                    n,
                    &centroids,
                    &centroid_norms,
                    n_landmarks,
                    &Dist::SquaredEuclidean,
                );

                // for each cluster, pick the point closest to its centroid
                let mut indices: Vec<usize> = Vec::with_capacity(n_landmarks);
                for cluster_idx in 0..n_landmarks {
                    let centroid = &centroids[cluster_idx * k_proj..(cluster_idx + 1) * k_proj];
                    let mut best: Option<(usize, T)> = None;
                    for (point_idx, &c) in assignments.iter().enumerate() {
                        if c == cluster_idx {
                            let point =
                                &embedding_flat[point_idx * k_proj..(point_idx + 1) * k_proj];
                            let d = T::euclidean_simd(centroid, point);
                            if best.is_none_or(|(_, bd)| d < bd) {
                                best = Some((point_idx, d));
                            }
                        }
                    }
                    if let Some((idx, _)) = best {
                        if !indices.contains(&idx) {
                            indices.push(idx);
                        }
                    }
                }

                // fill remainder (empty clusters or duplicate picks) with
                // random
                if indices.len() < n_landmarks {
                    let mut rng = StdRng::seed_from_u64(seed as u64 + 1);
                    let mut remaining: Vec<usize> =
                        (0..n).filter(|i| !indices.contains(i)).collect();
                    remaining.shuffle(&mut rng);
                    while indices.len() < n_landmarks {
                        if let Some(pick) = remaining.pop() {
                            indices.push(pick);
                        } else {
                            break;
                        }
                    }
                }
                indices
            }
        };

        // get coordinates
        let landmark_data: Vec<T> = landmark_indices
            .iter()
            .flat_map(|&i| data_flat[i * dim..(i + 1) * dim].iter().copied())
            .collect();

        // landmark-landmark gaussian kernel via self-kNN on landmarks
        let landmark_mat =
            faer::Mat::<T>::from_fn(n_landmarks, dim, |i, j| landmark_data[i * dim + j]);
        let k_ll = k.min(n_landmarks.saturating_sub(1)).max(1);
        let nn_params = NearestNeighbourParams {
            dist_metric: distance.to_string(),
            ..Default::default()
        };
        let (ll_indices, ll_dists) = run_ann_search(
            landmark_mat.as_ref(),
            k_ll,
            "kmknn".to_string(),
            &nn_params,
            seed,
            verbose,
        )?;
        // with this config it behaves like a Gaussian
        let ll_graph = phate_alpha_decay_affinities(
            &ll_indices,
            &ll_dists,
            k_ll,
            Some(T::from_f64(2.0).unwrap()),
            bandwidth_scale,
            thresh,
            graph_symmetry,
            distance == "euclidean",
        );
        let kernel_ll = coo_to_csr(&ll_graph);
        let kernel_ll_norm = apply_anisotropic_normalisation(&kernel_ll, alpha_norm)?;
        let (p_sym_ll, sqrt_degrees_l) = build_symmetric_diffusion_operator(&kernel_ll_norm)?;

        // data-to-landmark transitions (for Nystroem)
        let p_nl = build_data_to_landmark_transitions(
            &data_flat,
            n,
            &landmark_data,
            n_landmarks,
            dim,
            k,
            bandwidth_scale,
            thresh,
            &dist_enum,
        );

        if verbosity.normal_verbosity() {
            println!(" Finished landmark generation in {:.2?}", start.elapsed());
        }

        Ok(Self {
            n_landmarks,
            method: landmark_method,
            landmark_indices,
            p_sym_ll,
            sqrt_degrees_l,
            p_nl,
        })
    }

    /// Compute the top `n_dim + 1` eigenpairs of the symmetric landmark
    /// operator via Lanczos.
    ///
    /// Returns `n_dim + 1` pairs so the caller can discard the trivial first
    /// eigenvalue (λ₀ ≈ 1) and retain `n_dim` meaningful components.
    ///
    /// ### Params
    ///
    /// * `n_dim` - Number of diffusion components required (excluding the
    ///   trivial one)
    /// * `seed` - RNG seed for the Lanczos starting vector
    ///
    /// ### Returns
    ///
    /// `(eigenvalues, eigenvectors)` both in f32; eigenvectors indexed as
    /// `evecs[point][component]`
    pub fn eigendecompose(
        &self,
        n_dim: usize,
        seed: u64,
    ) -> Result<(Vec<f32>, Vec<Vec<f32>>), ManifoldsError> {
        compute_largest_eigenpairs_lanczos(&self.p_sym_ll, n_dim + 1, seed)
    }

    /// Select the optimal diffusion time via the Von Neumann entropy knee on
    /// the landmark operator.
    ///
    /// ### Params
    ///
    /// * `t_max` - Maximum number of diffusion steps to evaluate
    ///
    /// ### Returns
    ///
    /// Optimal `t` (knee point of the entropy curve)
    pub fn find_optimal_t(&self, t_max: usize) -> Result<usize, ManifoldsError> {
        let entropy = landmark_von_neumann_entropy(&self.p_sym_ll, t_max)?;
        Ok(find_knee_point(&entropy))
    }

    /// Compute diffusion coordinates on the landmarks: y_k(l) = λ_k^t · φ_k(l).
    ///
    /// Right eigenvectors of the row-stochastic operator are recovered as
    /// φ_k(l) = u_k(l) / sqrt(d_l), where u_k are eigenvectors of P_sym.
    /// The trivial first component is dropped; sign is fixed to the
    /// largest-magnitude element per component.
    ///
    /// ### Params
    ///
    /// * `evals` - Eigenvalues from `eigendecompose` (length n_dim + 1)
    /// * `evecs` - Eigenvectors from `eigendecompose`, indexed as
    ///   `evecs[point][component]`
    /// * `n_dim` - Number of diffusion dimensions to retain
    /// * `t` - Diffusion time (number of steps to raise eigenvalues to)
    ///
    /// ### Returns
    ///
    /// `(landmark_embedding, lambdas)` where `landmark_embedding` is L×n_dim
    /// and `lambdas` holds the n_dim eigenvalues used (needed for Nystroem
    /// extension)
    pub fn compute_landmark_embedding(
        &self,
        evals: &[f32],
        evecs: &[Vec<f32>],
        n_dim: usize,
        t: usize,
    ) -> (Vec<Vec<T>>, Vec<T>) {
        let mut embedding: Vec<Vec<T>> = vec![vec![T::zero(); n_dim]; self.n_landmarks];
        let mut lambdas: Vec<T> = Vec::with_capacity(n_dim);

        for comp_idx in 1..=n_dim {
            let lambda = T::from_f32(evals[comp_idx]).unwrap();
            let lambda_t = lambda.powi(t as i32);
            lambdas.push(lambda);

            let mut max_abs = 0.0f32;
            let mut sign = 1.0f32;
            for l in 0..self.n_landmarks {
                let v = evecs[l][comp_idx];
                if v.abs() > max_abs {
                    max_abs = v.abs();
                    sign = if v >= 0.0 { 1.0 } else { -1.0 };
                }
            }

            for l in 0..self.n_landmarks {
                let u = T::from_f32(evecs[l][comp_idx] * sign).unwrap();
                embedding[l][comp_idx - 1] = lambda_t * u / self.sqrt_degrees_l[l];
            }
        }

        (embedding, lambdas)
    }

    /// Extend the landmark embedding to all N points via Nystroem
    /// approximation.
    ///
    /// Computes y_k(x) = (1/λ_k) · Σ_l P_nl(x, l) · y_k(l). Components
    /// whose eigenvalue is near zero are left as zero rather than divided.
    ///
    /// ### Params
    ///
    /// * `landmark_embedding` - L×n_dim embedding from
    ///   `compute_landmark_embedding`
    /// * `lambdas` - Per-component eigenvalues from
    ///   `compute_landmark_embedding`
    ///
    /// ### Returns
    ///
    /// Full N×n_dim diffusion embedding
    pub fn nystrom_extend(&self, landmark_embedding: &[Vec<T>], lambdas: &[T]) -> Vec<Vec<T>> {
        let n = self.p_nl.shape().0;
        let n_dim = landmark_embedding[0].len();
        let mut out = vec![vec![T::zero(); n_dim]; n];

        for i in 0..n {
            let start = self.p_nl.indptr[i];
            let end = self.p_nl.indptr[i + 1];
            for idx in start..end {
                let l = self.p_nl.indices[idx];
                let w = self.p_nl.data[idx];
                for d in 0..n_dim {
                    out[i][d] += w * landmark_embedding[l][d];
                }
            }
        }

        for i in 0..n {
            for d in 0..n_dim {
                if lambdas[d].abs() > T::epsilon() {
                    out[i][d] /= lambdas[d];
                }
            }
        }

        out
    }

    /// Get the number of landmarks
    ///
    /// ### Returns
    ///
    /// Number of landmarks
    pub fn get_n_landmarks(&self) -> usize {
        self.n_landmarks
    }

    /// Get the landmark indices
    ///
    /// ### Return
    ///
    /// The indices of the landmarks
    pub fn get_landmark_indices(&self) -> &[usize] {
        &self.landmark_indices
    }
}

///////////
// Tests //
///////////

#[cfg(test)]
mod test_data_diffusion {
    use super::*;

    #[test]
    fn test_diffusion_operator_normalization() {
        // Create a CSR matrix manually (or via conversion)
        // Row 0: [10.0, 30.0] -> Sum = 40.0
        // Row 1: [5.0]        -> Sum = 5.0
        // Row 2: [0.0]        -> Sum = 0.0 (Empty)

        let data = vec![10.0, 30.0, 5.0];
        let indices = vec![1, 2, 0];
        let indptr = vec![0, 2, 3, 3]; // Row 0 (0..2), Row 1 (2..3), Row 2 (3..3)

        let kernel = CompressedSparseData::new_csr(&data, &indices, &indptr, (3, 3));

        let diffusion_op = build_diffusion_operator(&kernel);

        // Check Row 0: 10/40 = 0.25, 30/40 = 0.75
        assert_eq!(diffusion_op.data[0], 0.25);
        assert_eq!(diffusion_op.data[1], 0.75);

        // Check Row 1: 5/5 = 1.0
        assert_eq!(diffusion_op.data[2], 1.0);

        // Check Row Sums
        let row0_sum = diffusion_op.data[0] + diffusion_op.data[1];
        assert!((row0_sum - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_diffusion_operator_zero_degree_safety() {
        // Test ensuring that a row with all zeros (or empty) doesn't cause NaN
        // Row 0: Empty
        // Row 1: [2.0, 2.0]

        let data = vec![2.0, 2.0];
        let indices = vec![0, 2];
        let indptr = vec![0, 0, 2];

        let kernel = CompressedSparseData::new_csr(&data, &indices, &indptr, (2, 3));
        let diffusion_op = build_diffusion_operator(&kernel);

        // Row 0 is empty, indptr should remain [0, 0, ...]
        assert_eq!(diffusion_op.indptr[0], 0);
        assert_eq!(diffusion_op.indptr[1], 0);

        // Row 1 should be normalized
        assert_eq!(diffusion_op.data[0], 0.5);
        assert_eq!(diffusion_op.data[1], 0.5);
    }

    #[test]
    fn test_anisotropic_alpha_zero_is_identity() {
        let data = vec![0.5, 0.3, 0.2, 0.7];
        let indices = vec![0, 1, 0, 1];
        let indptr = vec![0, 2, 4];
        let kernel = CompressedSparseData::new_csr(&data, &indices, &indptr, (2, 2));

        let result = apply_anisotropic_normalisation(&kernel, 0.0).unwrap();
        assert_eq!(result.data, kernel.data);
        assert_eq!(result.indices, kernel.indices);
        assert_eq!(result.indptr, kernel.indptr);
    }

    #[test]
    fn test_anisotropic_alpha_one_values() {
        // K = [[1.0, 2.0], [2.0, 1.0]], symmetric
        // q = [3.0, 3.0]; with alpha = 1: K'_ij = K_ij / (3 * 3) = K_ij / 9
        let data = vec![1.0, 2.0, 2.0, 1.0];
        let indices = vec![0, 1, 0, 1];
        let indptr = vec![0, 2, 4];
        let kernel = CompressedSparseData::new_csr(&data, &indices, &indptr, (2, 2));

        let result = apply_anisotropic_normalisation(&kernel, 1.0).unwrap();
        use approx::assert_relative_eq;
        assert_relative_eq!(result.data[0], 1.0 / 9.0, epsilon = 1e-10);
        assert_relative_eq!(result.data[1], 2.0 / 9.0, epsilon = 1e-10);
        assert_relative_eq!(result.data[2], 2.0 / 9.0, epsilon = 1e-10);
        assert_relative_eq!(result.data[3], 1.0 / 9.0, epsilon = 1e-10);
    }

    #[test]
    fn test_anisotropic_preserves_symmetry() {
        let data = vec![1.0, 0.5, 0.5, 1.0, 0.3, 0.3, 1.0];
        let indices = vec![0, 1, 0, 1, 2, 1, 2];
        let indptr = vec![0, 2, 5, 7];
        let kernel = CompressedSparseData::new_csr(&data, &indices, &indptr, (3, 3));

        let result = apply_anisotropic_normalisation(&kernel, 0.5).unwrap();
        let dense = result.to_dense();
        use approx::assert_relative_eq;
        for i in 0..3 {
            for j in 0..3 {
                assert_relative_eq!(dense[(i, j)], dense[(j, i)], epsilon = 1e-12);
            }
        }
    }

    #[test]
    fn test_symmetric_operator_is_symmetric() {
        let data = vec![1.0, 0.5, 0.5, 1.0, 0.3, 0.3, 1.0];
        let indices = vec![0, 1, 0, 1, 2, 1, 2];
        let indptr = vec![0, 2, 5, 7];
        let kernel = CompressedSparseData::new_csr(&data, &indices, &indptr, (3, 3));

        let (p_sym, _) = build_symmetric_diffusion_operator(&kernel).unwrap();
        let dense = p_sym.to_dense();
        use approx::assert_relative_eq;
        for i in 0..3 {
            for j in 0..3 {
                assert_relative_eq!(dense[(i, j)], dense[(j, i)], epsilon = 1e-12);
            }
        }
    }

    #[test]
    fn test_symmetric_operator_sqrt_degrees() {
        let data = vec![1.0, 0.5, 0.5, 1.0, 0.3, 0.3, 1.0];
        let indices = vec![0, 1, 0, 1, 2, 1, 2];
        let indptr = vec![0, 2, 5, 7];
        let kernel = CompressedSparseData::new_csr(&data, &indices, &indptr, (3, 3));

        let (_, sqrt_d) = build_symmetric_diffusion_operator(&kernel).unwrap();
        use approx::assert_relative_eq;
        assert_relative_eq!(sqrt_d[0], 1.5_f64.sqrt(), epsilon = 1e-10);
        assert_relative_eq!(sqrt_d[1], 1.8_f64.sqrt(), epsilon = 1e-10);
        assert_relative_eq!(sqrt_d[2], 1.3_f64.sqrt(), epsilon = 1e-10);
    }

    #[test]
    fn test_symmetric_operator_largest_eigenvalue_is_one() {
        // For any row-stochastic P = D^{-1} K on a connected graph,
        // P_sym = D^{-1/2} K D^{-1/2} has largest eigenvalue = 1.
        use crate::utils::math::compute_largest_eigenpairs_lanczos;

        let data = vec![1.0, 0.5, 0.5, 1.0, 0.3, 0.3, 1.0];
        let indices = vec![0, 1, 0, 1, 2, 1, 2];
        let indptr = vec![0, 2, 5, 7];
        let kernel = CompressedSparseData::new_csr(&data, &indices, &indptr, (3, 3));

        let (p_sym, _) = build_symmetric_diffusion_operator(&kernel).unwrap();
        let (evals, _) = compute_largest_eigenpairs_lanczos(&p_sym, 1, 42).unwrap();
        use approx::assert_relative_eq;
        assert_relative_eq!(evals[0], 1.0, epsilon = 1e-4);
    }
}