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
//! Riemannian manifold processing algorithms
//!
//! This module provides tools for working with Riemannian manifolds, including:
//! - **Geodesic computation**: Computing shortest paths on manifolds
//! - **Parallel transport**: Moving vectors along geodesics while preserving geometric properties
//! - **Curvature estimation**: Computing Gaussian and mean curvature
//! - **Riemannian optimization**: Optimization methods that respect manifold structure
//!
//! Riemannian manifolds are smooth manifolds equipped with a Riemannian metric,
//! which allows measurement of distances, angles, and curvature.

use scirs2_core::ndarray::{Array1, Array2, ArrayView1, ArrayView2, Axis};
use sklears_core::{
    error::{Result as SklResult, SklearsError},
    traits::{Estimator, Fit, Transform, Untrained},
    types::Float,
};

/// Riemannian manifold representation with geodesic computation capabilities
///
/// # Parameters
///
/// * `n_neighbors` - Number of neighbors for local metric estimation
/// * `metric_type` - Type of metric to use ("euclidean", "geodesic")
/// * `geodesic_method` - Method for geodesic computation ("dijkstra", "floyd_warshall")
/// * `parallel_transport_method` - Method for parallel transport ("schild_ladder", "pole_ladder")
///
/// # Examples
///
/// ```
/// use sklears_manifold::riemannian::RiemannianManifold;
/// use sklears_core::traits::{Transform, Fit};
/// use scirs2_core::ndarray::{array, Array1, ArrayView1, ArrayView2};
///
/// let data = array![[1.0, 2.0], [3.0, 4.0], [5.0, 6.0], [7.0, 8.0]];
/// let manifold = RiemannianManifold::builder()
///     .n_neighbors(3)
///     .metric_type("geodesic")
///     .build();
/// let fitted = manifold.fit(&data.view(), &()).unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct RiemannianManifold<S = Untrained> {
    n_neighbors: usize,
    metric_type: String,
    geodesic_method: String,
    parallel_transport_method: String,
    curvature_estimation_radius: Float,
    state: S,
}

/// Trained state for RiemannianManifold
#[derive(Debug, Clone)]
pub struct RiemannianManifoldTrained {
    /// metric_tensor
    pub metric_tensor: Array2<Float>,
    /// geodesic_distances
    pub geodesic_distances: Array2<Float>,
    /// christoffel_symbols
    pub christoffel_symbols: Array2<Float>,
    /// gaussian_curvature
    pub gaussian_curvature: Array1<Float>,
    /// mean_curvature
    pub mean_curvature: Array1<Float>,
    /// data_points
    pub data_points: Array2<Float>,
    /// n_neighbors
    pub n_neighbors: usize,
}

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

impl RiemannianManifold<Untrained> {
    /// Create a new RiemannianManifold builder
    pub fn builder() -> RiemannianManifoldBuilder {
        RiemannianManifoldBuilder::new()
    }

    /// Create a RiemannianManifold with default parameters
    pub fn new() -> Self {
        Self {
            n_neighbors: 5,
            metric_type: "euclidean".to_string(),
            geodesic_method: "dijkstra".to_string(),
            parallel_transport_method: "schild_ladder".to_string(),
            curvature_estimation_radius: 1.0,
            state: Untrained,
        }
    }
}

/// Builder for RiemannianManifold
#[derive(Debug)]
pub struct RiemannianManifoldBuilder {
    n_neighbors: usize,
    metric_type: String,
    geodesic_method: String,
    parallel_transport_method: String,
    curvature_estimation_radius: Float,
}

impl RiemannianManifoldBuilder {
    fn new() -> Self {
        Self {
            n_neighbors: 5,
            metric_type: "euclidean".to_string(),
            geodesic_method: "dijkstra".to_string(),
            parallel_transport_method: "schild_ladder".to_string(),
            curvature_estimation_radius: 1.0,
        }
    }

    /// Set number of neighbors for local metric estimation
    pub fn n_neighbors(mut self, n_neighbors: usize) -> Self {
        self.n_neighbors = n_neighbors;
        self
    }

    /// Set metric type ("euclidean", "geodesic")
    pub fn metric_type(mut self, metric_type: &str) -> Self {
        self.metric_type = metric_type.to_string();
        self
    }

    /// Set geodesic computation method ("dijkstra", "floyd_warshall")
    pub fn geodesic_method(mut self, method: &str) -> Self {
        self.geodesic_method = method.to_string();
        self
    }

    /// Set parallel transport method ("schild_ladder", "pole_ladder")
    pub fn parallel_transport_method(mut self, method: &str) -> Self {
        self.parallel_transport_method = method.to_string();
        self
    }

    /// Set radius for curvature estimation
    pub fn curvature_estimation_radius(mut self, radius: Float) -> Self {
        self.curvature_estimation_radius = radius;
        self
    }

    /// Build the RiemannianManifold
    pub fn build(self) -> RiemannianManifold<Untrained> {
        RiemannianManifold {
            n_neighbors: self.n_neighbors,
            metric_type: self.metric_type,
            geodesic_method: self.geodesic_method,
            parallel_transport_method: self.parallel_transport_method,
            curvature_estimation_radius: self.curvature_estimation_radius,
            state: Untrained,
        }
    }
}

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

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

impl Fit<ArrayView2<'_, Float>, ()> for RiemannianManifold<Untrained> {
    type Fitted = RiemannianManifold<RiemannianManifoldTrained>;

    fn fit(self, x: &ArrayView2<'_, Float>, _y: &()) -> SklResult<Self::Fitted> {
        let (n_samples, _n_features) = x.dim();

        if n_samples < self.n_neighbors {
            return Err(SklearsError::InvalidInput(format!(
                "Number of samples ({}) must be >= n_neighbors ({})",
                n_samples, self.n_neighbors
            )));
        }

        // Compute local metric tensor
        let metric_tensor = compute_metric_tensor(x, self.n_neighbors)?;

        // Compute geodesic distances
        let geodesic_distances = match self.geodesic_method.as_str() {
            "dijkstra" => compute_geodesic_distances_dijkstra(x, self.n_neighbors)?,
            "floyd_warshall" => compute_geodesic_distances_floyd_warshall(x, self.n_neighbors)?,
            _ => {
                return Err(SklearsError::InvalidInput(format!(
                    "Unknown geodesic method: {}",
                    self.geodesic_method
                )))
            }
        };

        // Compute Christoffel symbols for the connection
        let christoffel_symbols = compute_christoffel_symbols(&metric_tensor)?;

        // Estimate curvature
        let (gaussian_curvature, mean_curvature) =
            estimate_curvature(x, self.n_neighbors, self.curvature_estimation_radius)?;

        let trained_state = RiemannianManifoldTrained {
            metric_tensor,
            geodesic_distances,
            christoffel_symbols,
            gaussian_curvature,
            mean_curvature,
            data_points: x.to_owned(),
            n_neighbors: self.n_neighbors,
        };

        Ok(RiemannianManifold {
            n_neighbors: self.n_neighbors,
            metric_type: self.metric_type,
            geodesic_method: self.geodesic_method,
            parallel_transport_method: self.parallel_transport_method,
            curvature_estimation_radius: self.curvature_estimation_radius,
            state: trained_state,
        })
    }
}

impl Transform<ArrayView2<'_, Float>, Array2<Float>>
    for RiemannianManifold<RiemannianManifoldTrained>
{
    fn transform(&self, x: &ArrayView2<'_, Float>) -> SklResult<Array2<Float>> {
        // For new points, compute their geodesic distances to training points
        let n_new = x.nrows();
        let n_train = self.state.data_points.nrows();
        let mut result = Array2::zeros((n_new, n_train));

        for i in 0..n_new {
            for j in 0..n_train {
                let dist = compute_geodesic_distance_between_points(
                    &x.row(i),
                    &self.state.data_points.row(j),
                    &self.state.metric_tensor,
                )?;
                result[(i, j)] = dist;
            }
        }

        Ok(result)
    }
}

impl RiemannianManifold<RiemannianManifoldTrained> {
    /// Compute parallel transport of a vector along a geodesic
    pub fn parallel_transport(
        &self,
        vector: &ArrayView1<Float>,
        start_point: &ArrayView1<Float>,
        end_point: &ArrayView1<Float>,
    ) -> SklResult<Array1<Float>> {
        match self.parallel_transport_method.as_str() {
            "schild_ladder" => parallel_transport_schild_ladder(
                vector,
                start_point,
                end_point,
                &self.state.christoffel_symbols,
            ),
            "pole_ladder" => parallel_transport_pole_ladder(
                vector,
                start_point,
                end_point,
                &self.state.christoffel_symbols,
            ),
            _ => Err(SklearsError::InvalidInput(format!(
                "Unknown parallel transport method: {}",
                self.parallel_transport_method
            ))),
        }
    }

    /// Compute the exponential map at a point
    pub fn exponential_map(
        &self,
        base_point: &ArrayView1<Float>,
        tangent_vector: &ArrayView1<Float>,
    ) -> SklResult<Array1<Float>> {
        exponential_map(base_point, tangent_vector, &self.state.christoffel_symbols)
    }

    /// Compute the logarithmic map at a point  
    pub fn logarithmic_map(
        &self,
        base_point: &ArrayView1<Float>,
        target_point: &ArrayView1<Float>,
    ) -> SklResult<Array1<Float>> {
        logarithmic_map(base_point, target_point, &self.state.christoffel_symbols)
    }

    /// Get the Gaussian curvature at data points
    pub fn gaussian_curvature(&self) -> &Array1<Float> {
        &self.state.gaussian_curvature
    }

    /// Get the mean curvature at data points
    pub fn mean_curvature(&self) -> &Array1<Float> {
        &self.state.mean_curvature
    }

    /// Get the metric tensor
    pub fn metric_tensor(&self) -> &Array2<Float> {
        &self.state.metric_tensor
    }

    /// Get geodesic distances between training points
    pub fn geodesic_distances(&self) -> &Array2<Float> {
        &self.state.geodesic_distances
    }
}

// Helper functions for Riemannian computations

/// Compute local metric tensor using neighborhood information
fn compute_metric_tensor(
    x: &ArrayView2<'_, Float>,
    n_neighbors: usize,
) -> SklResult<Array2<Float>> {
    let (n_samples, n_features) = x.dim();
    let mut metric = Array2::zeros((n_features, n_features));

    // Use local PCA to estimate the metric tensor
    for i in 0..n_samples {
        let point = x.row(i);

        // Find k nearest neighbors
        let mut distances: Vec<(Float, usize)> = Vec::new();
        for j in 0..n_samples {
            if i != j {
                let dist = (&x.row(j) - &point).mapv(|x| x * x).sum().sqrt();
                distances.push((dist, j));
            }
        }
        distances.sort_by(|a, b| a.0.partial_cmp(&b.0).expect("operation should succeed"));

        let neighbors: Vec<usize> = distances
            .iter()
            .take(n_neighbors.min(n_samples - 1))
            .map(|(_, idx)| *idx)
            .collect();

        // Compute local covariance matrix
        let mut local_data = Array2::zeros((neighbors.len(), n_features));
        for (k, &neighbor_idx) in neighbors.iter().enumerate() {
            let diff = &x.row(neighbor_idx) - &point;
            local_data.row_mut(k).assign(&diff);
        }

        // Compute covariance matrix
        let mean = local_data
            .mean_axis(Axis(0))
            .expect("operation should succeed");
        let centered_data = &local_data - &mean;
        let cov = centered_data.t().dot(&centered_data) / (neighbors.len() as Float - 1.0);

        metric = metric + cov;
    }

    // Average over all points
    metric /= n_samples as Float;

    // Ensure positive definiteness by adding small regularization
    for i in 0..n_features {
        metric[(i, i)] += 1e-6;
    }

    Ok(metric)
}

/// Compute geodesic distances using Dijkstra's algorithm
fn compute_geodesic_distances_dijkstra(
    x: &ArrayView2<'_, Float>,
    n_neighbors: usize,
) -> SklResult<Array2<Float>> {
    let n_samples = x.nrows();
    let mut distances = Array2::from_elem((n_samples, n_samples), Float::INFINITY);

    // Set diagonal to zero
    for i in 0..n_samples {
        distances[(i, i)] = 0.0;
    }

    // Build neighborhood graph
    for i in 0..n_samples {
        let point = x.row(i);

        // Find k nearest neighbors
        let mut neighbor_distances: Vec<(Float, usize)> = Vec::new();
        for j in 0..n_samples {
            if i != j {
                let dist = (&x.row(j) - &point).mapv(|x| x * x).sum().sqrt();
                neighbor_distances.push((dist, j));
            }
        }
        neighbor_distances.sort_by(|a, b| a.0.partial_cmp(&b.0).expect("operation should succeed"));

        // Add edges to k nearest neighbors
        for &(dist, j) in neighbor_distances
            .iter()
            .take(n_neighbors.min(n_samples - 1))
        {
            distances[(i, j)] = dist;
            distances[(j, i)] = dist; // Make symmetric
        }
    }

    // Apply Dijkstra's algorithm from each point
    for start in 0..n_samples {
        let mut dist = vec![Float::INFINITY; n_samples];
        let mut visited = vec![false; n_samples];
        dist[start] = 0.0;

        for _ in 0..n_samples {
            // Find unvisited vertex with minimum distance
            let mut min_dist = Float::INFINITY;
            let mut min_vertex = 0;
            for v in 0..n_samples {
                if !visited[v] && dist[v] < min_dist {
                    min_dist = dist[v];
                    min_vertex = v;
                }
            }

            visited[min_vertex] = true;

            // Update distances to neighbors
            for neighbor in 0..n_samples {
                if !visited[neighbor] && distances[(min_vertex, neighbor)] != Float::INFINITY {
                    let new_dist = dist[min_vertex] + distances[(min_vertex, neighbor)];
                    if new_dist < dist[neighbor] {
                        dist[neighbor] = new_dist;
                    }
                }
            }
        }

        // Store computed distances
        for end in 0..n_samples {
            distances[(start, end)] = dist[end];
        }
    }

    Ok(distances)
}

/// Compute geodesic distances using Floyd-Warshall algorithm
fn compute_geodesic_distances_floyd_warshall(
    x: &ArrayView2<'_, Float>,
    n_neighbors: usize,
) -> SklResult<Array2<Float>> {
    let n_samples = x.nrows();
    let mut distances = Array2::from_elem((n_samples, n_samples), Float::INFINITY);

    // Set diagonal to zero
    for i in 0..n_samples {
        distances[(i, i)] = 0.0;
    }

    // Build neighborhood graph
    for i in 0..n_samples {
        let point = x.row(i);

        // Find k nearest neighbors
        let mut neighbor_distances: Vec<(Float, usize)> = Vec::new();
        for j in 0..n_samples {
            if i != j {
                let dist = (&x.row(j) - &point).mapv(|x| x * x).sum().sqrt();
                neighbor_distances.push((dist, j));
            }
        }
        neighbor_distances.sort_by(|a, b| a.0.partial_cmp(&b.0).expect("operation should succeed"));

        // Add edges to k nearest neighbors
        for &(dist, j) in neighbor_distances
            .iter()
            .take(n_neighbors.min(n_samples - 1))
        {
            distances[(i, j)] = dist;
            distances[(j, i)] = dist; // Make symmetric
        }
    }

    // Floyd-Warshall algorithm
    for k in 0..n_samples {
        for i in 0..n_samples {
            for j in 0..n_samples {
                if distances[(i, k)] != Float::INFINITY && distances[(k, j)] != Float::INFINITY {
                    let new_dist = distances[(i, k)] + distances[(k, j)];
                    if new_dist < distances[(i, j)] {
                        distances[(i, j)] = new_dist;
                    }
                }
            }
        }
    }

    Ok(distances)
}

/// Compute Christoffel symbols from metric tensor
fn compute_christoffel_symbols(metric: &Array2<Float>) -> SklResult<Array2<Float>> {
    let n = metric.nrows();
    let christoffel = Array2::zeros((n * n, n));

    // For a constant metric tensor, Christoffel symbols are zero
    // In practice, we would need metric derivatives for the full computation
    // This is a simplified version for demonstration

    // Γᵢⱼᵏ = ½ gᵏˡ (∂gᵢˡ/∂xʲ + ∂gⱼˡ/∂xᵢ - ∂gᵢⱼ/∂xˡ)
    // For constant metric, all derivatives are zero

    Ok(christoffel)
}

/// Estimate Gaussian and mean curvature at data points
fn estimate_curvature(
    x: &ArrayView2<'_, Float>,
    n_neighbors: usize,
    radius: Float,
) -> SklResult<(Array1<Float>, Array1<Float>)> {
    let n_samples = x.nrows();
    let mut gaussian_curvature = Array1::zeros(n_samples);
    let mut mean_curvature = Array1::zeros(n_samples);

    for i in 0..n_samples {
        let point = x.row(i);

        // Find neighbors within radius
        let mut neighbors = Vec::new();
        for j in 0..n_samples {
            if i != j {
                let dist = (&x.row(j) - &point).mapv(|x| x * x).sum().sqrt();
                if dist <= radius {
                    neighbors.push(j);
                }
            }
        }

        if neighbors.len() >= n_neighbors {
            // Estimate curvature using local surface fitting
            // This is a simplified estimation
            let local_variance = estimate_local_variance(x, i, &neighbors);
            gaussian_curvature[i] = 1.0 / (1.0 + local_variance);
            mean_curvature[i] = local_variance.sqrt();
        }
    }

    Ok((gaussian_curvature, mean_curvature))
}

/// Estimate local variance for curvature computation
fn estimate_local_variance(
    x: &ArrayView2<'_, Float>,
    center_idx: usize,
    neighbors: &[usize],
) -> Float {
    let center = x.row(center_idx);
    let mut variance = 0.0;

    for &neighbor_idx in neighbors {
        let diff = &x.row(neighbor_idx) - &center;
        variance += diff.mapv(|x| x * x).sum();
    }

    if !neighbors.is_empty() {
        variance / neighbors.len() as Float
    } else {
        0.0
    }
}

/// Compute geodesic distance between two specific points
fn compute_geodesic_distance_between_points(
    point1: &ArrayView1<Float>,
    point2: &ArrayView1<Float>,
    metric: &Array2<Float>,
) -> SklResult<Float> {
    let diff = point2 - point1;
    let dist_squared = diff.dot(&metric.dot(&diff));
    Ok(dist_squared.sqrt())
}

/// Parallel transport using Schild's ladder construction
fn parallel_transport_schild_ladder(
    vector: &ArrayView1<Float>,
    _start_point: &ArrayView1<Float>,
    _end_point: &ArrayView1<Float>,
    _christoffel: &Array2<Float>,
) -> SklResult<Array1<Float>> {
    // Simplified parallel transport - in practice this would use the geodesic equation
    // and Christoffel symbols for accurate parallel transport

    // For now, return the vector unchanged (valid for flat spaces)
    Ok(vector.to_owned())
}

/// Parallel transport using pole ladder construction  
fn parallel_transport_pole_ladder(
    vector: &ArrayView1<Float>,
    _start_point: &ArrayView1<Float>,
    _end_point: &ArrayView1<Float>,
    _christoffel: &Array2<Float>,
) -> SklResult<Array1<Float>> {
    // Simplified parallel transport - pole ladder is more accurate than Schild's ladder
    // For now, return the vector unchanged (valid for flat spaces)
    Ok(vector.to_owned())
}

/// Exponential map: map tangent vector to manifold point
fn exponential_map(
    base_point: &ArrayView1<Float>,
    tangent_vector: &ArrayView1<Float>,
    _christoffel: &Array2<Float>,
) -> SklResult<Array1<Float>> {
    // Simplified exponential map for flat space
    Ok(base_point + tangent_vector)
}

/// Logarithmic map: map manifold point to tangent vector
fn logarithmic_map(
    base_point: &ArrayView1<Float>,
    target_point: &ArrayView1<Float>,
    _christoffel: &Array2<Float>,
) -> SklResult<Array1<Float>> {
    // Simplified logarithmic map for flat space
    Ok(target_point - base_point)
}