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
//! Zero-cost abstractions for manifold learning
//!
//! This module provides zero-cost abstractions that compile down to efficient
//! machine code while providing high-level APIs for manifold learning operations.
//! Uses const generics, traits, and other Rust features to eliminate runtime overhead.

use scirs2_core::ndarray::{Array2, ArrayView1, ArrayView2};
/// Zero-cost distance metrics using const generics
use sklears_core::{
    error::{Result as SklResult, SklearsError},
    types::Float,
};
use std::marker::PhantomData;
pub trait DistanceMetric<const METRIC_ID: usize> {
    /// Compute distance between two points
    fn distance(a: ArrayView1<Float>, b: ArrayView1<Float>) -> Float;

    /// Get the metric name
    const NAME: &'static str;

    /// Whether this metric satisfies triangle inequality
    const IS_METRIC: bool;
}

/// Euclidean distance metric (L2 norm)
pub struct EuclideanDistance;

impl DistanceMetric<0> for EuclideanDistance {
    fn distance(a: ArrayView1<Float>, b: ArrayView1<Float>) -> Float {
        a.iter()
            .zip(b.iter())
            .map(|(x, y)| (x - y).powi(2))
            .sum::<Float>()
            .sqrt()
    }

    const NAME: &'static str = "euclidean";
    const IS_METRIC: bool = true;
}

/// Manhattan distance metric (L1 norm)
pub struct ManhattanDistance;

impl DistanceMetric<1> for ManhattanDistance {
    fn distance(a: ArrayView1<Float>, b: ArrayView1<Float>) -> Float {
        a.iter().zip(b.iter()).map(|(x, y)| (x - y).abs()).sum()
    }

    const NAME: &'static str = "manhattan";
    const IS_METRIC: bool = true;
}

/// Cosine distance (1 - cosine similarity)
pub struct CosineDistance;

impl DistanceMetric<2> for CosineDistance {
    fn distance(a: ArrayView1<Float>, b: ArrayView1<Float>) -> Float {
        let dot_product = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum::<Float>();
        let norm_a = a.iter().map(|x| x * x).sum::<Float>().sqrt();
        let norm_b = b.iter().map(|x| x * x).sum::<Float>().sqrt();

        if norm_a > 0.0 && norm_b > 0.0 {
            1.0 - dot_product / (norm_a * norm_b)
        } else {
            0.0
        }
    }

    const NAME: &'static str = "cosine";
    const IS_METRIC: bool = false; // Cosine distance doesn't satisfy triangle inequality
}

/// Chebyshev distance (L∞ norm)
pub struct ChebyshevDistance;

impl DistanceMetric<3> for ChebyshevDistance {
    fn distance(a: ArrayView1<Float>, b: ArrayView1<Float>) -> Float {
        a.iter()
            .zip(b.iter())
            .map(|(x, y)| (x - y).abs())
            .fold(0.0, |max_val, diff| max_val.max(diff))
    }

    const NAME: &'static str = "chebyshev";
    const IS_METRIC: bool = true;
}

/// Zero-cost manifold operations using const generics
pub trait ManifoldOperation<const OP_ID: usize> {
    type Input;
    type Output;

    /// Apply the operation
    fn apply(input: Self::Input) -> SklResult<Self::Output>;

    /// Get operation name
    const NAME: &'static str;

    /// Computational complexity class
    const COMPLEXITY: &'static str;
}

/// Principal Component Analysis operation
pub struct PCAOperation;

impl ManifoldOperation<0> for PCAOperation {
    type Input = ArrayView2<'static, Float>;
    type Output = (Array2<Float>, Array2<Float>); // (components, transformed_data)

    fn apply(input: Self::Input) -> SklResult<Self::Output> {
        // Simplified PCA implementation for demonstration
        let n_samples = input.nrows();
        let n_features = input.ncols();

        if n_samples == 0 || n_features == 0 {
            return Err(SklearsError::InvalidParameter {
                name: "input_shape".to_string(),
                reason: "Input must have positive dimensions".to_string(),
            });
        }

        // Center the data
        let mean = input
            .mean_axis(scirs2_core::ndarray::Axis(0))
            .expect("operation should succeed");
        let mut centered = input.to_owned();
        for mut row in centered.rows_mut() {
            row -= &mean;
        }

        // Compute covariance matrix
        let _cov = centered.t().dot(&centered) / (n_samples as Float - 1.0); // deferred: used in PCA whitening

        // For demonstration, return identity transformation
        let components = Array2::eye(n_features);
        let transformed = centered;

        Ok((components, transformed))
    }

    const NAME: &'static str = "PCA";
    const COMPLEXITY: &'static str = "O(n² * d)";
}

/// Independent Component Analysis operation
pub struct ICAOperation;

impl ManifoldOperation<1> for ICAOperation {
    type Input = ArrayView2<'static, Float>;
    type Output = (Array2<Float>, Array2<Float>); // (mixing_matrix, independent_components)

    fn apply(input: Self::Input) -> SklResult<Self::Output> {
        let n_samples = input.nrows();
        let n_features = input.ncols();

        if n_samples < n_features {
            return Err(SklearsError::InvalidParameter {
                name: "sample_size".to_string(),
                reason: "Number of samples must be >= number of features for ICA".to_string(),
            });
        }

        // Simplified ICA (placeholder implementation)
        let mixing_matrix = Array2::eye(n_features);
        let components = input.to_owned();

        Ok((mixing_matrix, components))
    }

    const NAME: &'static str = "ICA";
    const COMPLEXITY: &'static str = "O(n³)";
}

/// Zero-cost manifold learning algorithm abstraction
pub trait ZeroCostManifoldAlgorithm<
    const ALGO_ID: usize,
    M: DistanceMetric<METRIC_ID>,
    const METRIC_ID: usize,
>
{
    /// Algorithm configuration type
    type Config;

    /// Output embedding type
    type Embedding;

    /// Apply the manifold learning algorithm
    fn fit_transform(data: ArrayView2<Float>, config: Self::Config) -> SklResult<Self::Embedding>;

    /// Get algorithm name
    const NAME: &'static str;

    /// Whether the algorithm preserves distances
    const PRESERVES_DISTANCES: bool;

    /// Whether the algorithm is linear
    const IS_LINEAR: bool;
}

/// Configuration for MDS algorithm
#[derive(Debug, Clone)]
pub struct MDSConfig {
    /// n_components
    pub n_components: usize,
    /// max_iter
    pub max_iter: usize,
    /// eps
    pub eps: Float,
}

impl Default for MDSConfig {
    fn default() -> Self {
        Self {
            n_components: 2,
            max_iter: 300,
            eps: 1e-6,
        }
    }
}

/// Multidimensional Scaling algorithm
pub struct MDSAlgorithm<M, const METRIC_ID: usize>(PhantomData<M>);

impl<M: DistanceMetric<METRIC_ID>, const METRIC_ID: usize>
    ZeroCostManifoldAlgorithm<0, M, METRIC_ID> for MDSAlgorithm<M, METRIC_ID>
{
    type Config = MDSConfig;
    type Embedding = Array2<Float>;

    fn fit_transform(data: ArrayView2<Float>, config: Self::Config) -> SklResult<Self::Embedding> {
        let n_samples = data.nrows();

        if config.n_components > data.ncols() {
            return Err(SklearsError::InvalidParameter {
                name: "n_components".to_string(),
                reason: format!(
                    "n_components {} cannot exceed input dimensions {}",
                    config.n_components,
                    data.ncols()
                ),
            });
        }

        // Compute distance matrix using the metric
        let mut distances = Array2::zeros((n_samples, n_samples));
        for i in 0..n_samples {
            for j in i..n_samples {
                let dist = M::distance(data.row(i), data.row(j));
                distances[[i, j]] = dist;
                distances[[j, i]] = dist;
            }
        }

        // Classical MDS: double centering
        let mut gram = distances.mapv(|x| -0.5 * x * x);
        let row_means = gram
            .mean_axis(scirs2_core::ndarray::Axis(1))
            .expect("operation should succeed");
        let total_mean = row_means.mean().expect("operation should succeed");

        for i in 0..n_samples {
            for j in 0..n_samples {
                gram[[i, j]] = gram[[i, j]] - row_means[i] - row_means[j] + total_mean;
            }
        }

        // For demonstration, return first n_components columns
        let embedding = gram
            .slice(scirs2_core::ndarray::s![.., ..config.n_components])
            .to_owned();
        Ok(embedding)
    }

    const NAME: &'static str = "MDS";
    const PRESERVES_DISTANCES: bool = true;
    const IS_LINEAR: bool = false;
}

/// Configuration for Isomap algorithm
#[derive(Debug, Clone)]
pub struct IsomapConfig {
    /// n_components
    pub n_components: usize,
    /// n_neighbors
    pub n_neighbors: usize,
}

impl Default for IsomapConfig {
    fn default() -> Self {
        Self {
            n_components: 2,
            n_neighbors: 5,
        }
    }
}

/// Isomap algorithm
pub struct IsomapAlgorithm<M, const METRIC_ID: usize>(PhantomData<M>);

impl<M: DistanceMetric<METRIC_ID>, const METRIC_ID: usize>
    ZeroCostManifoldAlgorithm<1, M, METRIC_ID> for IsomapAlgorithm<M, METRIC_ID>
{
    type Config = IsomapConfig;
    type Embedding = Array2<Float>;

    fn fit_transform(data: ArrayView2<Float>, config: Self::Config) -> SklResult<Self::Embedding> {
        let n_samples = data.nrows();

        if config.n_neighbors >= n_samples {
            return Err(SklearsError::InvalidParameter {
                name: "n_neighbors".to_string(),
                reason: format!(
                    "n_neighbors {} must be less than n_samples {}",
                    config.n_neighbors, n_samples
                ),
            });
        }

        // Build k-NN graph
        let mut adjacency = Array2::from_elem((n_samples, n_samples), Float::INFINITY);

        for i in 0..n_samples {
            adjacency[[i, i]] = 0.0;

            // Find k nearest neighbors
            let mut distances: Vec<(Float, usize)> = Vec::new();
            for j in 0..n_samples {
                if i != j {
                    let dist = M::distance(data.row(i), data.row(j));
                    distances.push((dist, j));
                }
            }

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

            for (dist, neighbor) in distances.iter().take(config.n_neighbors) {
                adjacency[[i, *neighbor]] = *dist;
                adjacency[[*neighbor, i]] = *dist; // Make symmetric
            }
        }

        // Floyd-Warshall for geodesic distances
        let mut geodesic = adjacency.clone();
        for k in 0..n_samples {
            for i in 0..n_samples {
                for j in 0..n_samples {
                    let through_k = geodesic[[i, k]] + geodesic[[k, j]];
                    if through_k < geodesic[[i, j]] {
                        geodesic[[i, j]] = through_k;
                    }
                }
            }
        }

        // Apply MDS to geodesic distances
        let _mds_config = MDSConfig {
            n_components: config.n_components,
            ..Default::default()
        };

        // For demonstration, return projection of first n_components columns
        let embedding = data
            .slice(scirs2_core::ndarray::s![
                ..,
                ..config.n_components.min(data.ncols())
            ])
            .to_owned();
        Ok(embedding)
    }

    const NAME: &'static str = "Isomap";
    const PRESERVES_DISTANCES: bool = true;
    const IS_LINEAR: bool = false;
}

/// Zero-cost neighbor search using const generics
pub trait NeighborSearch<const SEARCH_ID: usize> {
    /// Find k nearest neighbors
    fn knn<M: DistanceMetric<METRIC_ID>, const METRIC_ID: usize>(
        data: ArrayView2<Float>,
        query: ArrayView1<Float>,
        k: usize,
    ) -> SklResult<(Vec<Float>, Vec<usize>)>;

    /// Algorithm name
    const NAME: &'static str;

    /// Computational complexity
    const COMPLEXITY: &'static str;
}

/// Brute force neighbor search
pub struct BruteForceSearch;

impl NeighborSearch<0> for BruteForceSearch {
    fn knn<M: DistanceMetric<METRIC_ID>, const METRIC_ID: usize>(
        data: ArrayView2<Float>,
        query: ArrayView1<Float>,
        k: usize,
    ) -> SklResult<(Vec<Float>, Vec<usize>)> {
        let n_samples = data.nrows();

        if k > n_samples {
            return Err(SklearsError::InvalidParameter {
                name: "k".to_string(),
                reason: format!("k={} cannot exceed n_samples={}", k, n_samples),
            });
        }

        let mut distances: Vec<(Float, usize)> = Vec::with_capacity(n_samples);

        for (i, row) in data.rows().into_iter().enumerate() {
            let dist = M::distance(row, query);
            distances.push((dist, i));
        }

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

        let (dists, indices): (Vec<Float>, Vec<usize>) = distances.into_iter().take(k).unzip();

        Ok((dists, indices))
    }

    const NAME: &'static str = "BruteForce";
    const COMPLEXITY: &'static str = "O(n * d)";
}

/// Compile-time manifold learning pipeline
pub struct ManifoldPipeline<A, M, const ALGO_ID: usize, const METRIC_ID: usize>
where
    A: ZeroCostManifoldAlgorithm<ALGO_ID, M, METRIC_ID>,
    M: DistanceMetric<METRIC_ID>,
{
    _algorithm: PhantomData<A>,
    _metric: PhantomData<M>,
}

impl<A, M, const ALGO_ID: usize, const METRIC_ID: usize> Default
    for ManifoldPipeline<A, M, ALGO_ID, METRIC_ID>
where
    A: ZeroCostManifoldAlgorithm<ALGO_ID, M, METRIC_ID>,
    M: DistanceMetric<METRIC_ID>,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<A, M, const ALGO_ID: usize, const METRIC_ID: usize> ManifoldPipeline<A, M, ALGO_ID, METRIC_ID>
where
    A: ZeroCostManifoldAlgorithm<ALGO_ID, M, METRIC_ID>,
    M: DistanceMetric<METRIC_ID>,
{
    /// Create a new pipeline
    pub const fn new() -> Self {
        Self {
            _algorithm: PhantomData,
            _metric: PhantomData,
        }
    }

    /// Run the pipeline
    pub fn run(data: ArrayView2<Float>, config: A::Config) -> SklResult<A::Embedding> {
        A::fit_transform(data, config)
    }

    /// Get pipeline information
    pub const fn info() -> (&'static str, &'static str, bool, bool) {
        (A::NAME, M::NAME, A::PRESERVES_DISTANCES, A::IS_LINEAR)
    }
}

/// Zero-cost kernel functions using const generics
pub trait KernelFunction<const KERNEL_ID: usize> {
    /// Compute kernel value between two points
    fn kernel(a: ArrayView1<Float>, b: ArrayView1<Float>, params: &Self::Params) -> Float;

    /// Kernel parameters type
    type Params: Default;

    /// Kernel name
    const NAME: &'static str;

    /// Whether kernel is positive definite
    const IS_PD: bool;
}

/// RBF (Gaussian) kernel parameters
#[derive(Debug, Clone)]
pub struct RBFParams {
    /// gamma
    pub gamma: Float,
}

impl Default for RBFParams {
    fn default() -> Self {
        Self { gamma: 1.0 }
    }
}

/// Radial Basis Function (RBF) kernel
pub struct RBFKernel;

impl KernelFunction<0> for RBFKernel {
    type Params = RBFParams;

    fn kernel(a: ArrayView1<Float>, b: ArrayView1<Float>, params: &Self::Params) -> Float {
        let squared_distance = a
            .iter()
            .zip(b.iter())
            .map(|(x, y)| (x - y).powi(2))
            .sum::<Float>();

        (-params.gamma * squared_distance).exp()
    }

    const NAME: &'static str = "RBF";
    const IS_PD: bool = true;
}

/// Polynomial kernel parameters
#[derive(Debug, Clone)]
pub struct PolynomialParams {
    /// degree
    pub degree: u32,
    /// coef0
    pub coef0: Float,
}

impl Default for PolynomialParams {
    fn default() -> Self {
        Self {
            degree: 3,
            coef0: 1.0,
        }
    }
}

/// Polynomial kernel
pub struct PolynomialKernel;

impl KernelFunction<1> for PolynomialKernel {
    type Params = PolynomialParams;

    fn kernel(a: ArrayView1<Float>, b: ArrayView1<Float>, params: &Self::Params) -> Float {
        let dot_product = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum::<Float>();
        (dot_product + params.coef0).powf(params.degree as Float)
    }

    const NAME: &'static str = "Polynomial";
    const IS_PD: bool = true;
}

/// Type aliases for common zero-cost configurations
pub type EuclideanMDS =
    ManifoldPipeline<MDSAlgorithm<EuclideanDistance, 0>, EuclideanDistance, 0, 0>;
pub type ManhattanMDS =
    ManifoldPipeline<MDSAlgorithm<ManhattanDistance, 1>, ManhattanDistance, 0, 1>;
pub type EuclideanIsomap =
    ManifoldPipeline<IsomapAlgorithm<EuclideanDistance, 0>, EuclideanDistance, 1, 0>;
pub type CosineIsomap = ManifoldPipeline<IsomapAlgorithm<CosineDistance, 2>, CosineDistance, 1, 2>;

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

    #[test]
    fn test_distance_metrics() {
        let a = array![1.0, 2.0, 3.0];
        let b = array![4.0, 5.0, 6.0];

        let euclidean_dist = EuclideanDistance::distance(a.view(), b.view());
        assert_abs_diff_eq!(euclidean_dist, 27.0_f64.sqrt(), epsilon = 1e-10);

        let manhattan_dist = ManhattanDistance::distance(a.view(), b.view());
        assert_abs_diff_eq!(manhattan_dist, 9.0, epsilon = 1e-10);

        assert_eq!(EuclideanDistance::NAME, "euclidean");
        // IS_METRIC is a compile-time constant; verify it as such
        const _: () = {
            assert!(EuclideanDistance::IS_METRIC);
        };
    }

    #[test]
    fn test_zero_cost_mds() {
        let data = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]];
        let config = MDSConfig::default();

        let embedding = EuclideanMDS::run(data.view(), config).expect("operation should succeed");
        assert_eq!(embedding.shape(), &[3, 2]);

        let (algo_name, metric_name, preserves_dist, is_linear) = EuclideanMDS::info();
        assert_eq!(algo_name, "MDS");
        assert_eq!(metric_name, "euclidean");
        assert!(preserves_dist);
        assert!(!is_linear);
    }

    #[test]
    fn test_neighbor_search() {
        let data = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]];
        let query = array![0.5, 0.5];

        let (distances, indices) =
            BruteForceSearch::knn::<EuclideanDistance, 0>(data.view(), query.view(), 2)
                .expect("operation should succeed");

        assert_eq!(distances.len(), 2);
        assert_eq!(indices.len(), 2);
        assert_eq!(BruteForceSearch::NAME, "BruteForce");
    }

    #[test]
    fn test_kernel_functions() {
        let a = array![1.0, 2.0];
        let b = array![3.0, 4.0];

        let rbf_params = RBFParams { gamma: 0.5 };
        let rbf_value = RBFKernel::kernel(a.view(), b.view(), &rbf_params);
        assert!(rbf_value > 0.0 && rbf_value <= 1.0);

        let poly_params = PolynomialParams::default();
        let poly_value = PolynomialKernel::kernel(a.view(), b.view(), &poly_params);
        assert!(poly_value > 0.0);

        assert_eq!(RBFKernel::NAME, "RBF");
        // IS_PD is a compile-time constant; verify it as such
        const _: () = {
            assert!(RBFKernel::IS_PD);
        };
    }

    #[test]
    fn test_compile_time_properties() {
        // These assertions are evaluated at compile time
        const _: () = {
            assert!(EuclideanDistance::IS_METRIC);
        };
        const _: () = {
            assert!(!CosineDistance::IS_METRIC);
        };
        const _: () = {
            assert!(RBFKernel::IS_PD);
        };

        // Test that we can access compile-time constants
        const EUCLIDEAN_NAME: &str = EuclideanDistance::NAME;
        assert_eq!(EUCLIDEAN_NAME, "euclidean");
    }
}