geometric-traits 0.2.3

Trait-based algebraic and graph algorithms for Rust, supporting no_std environments
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
//! Classical Multidimensional Scaling (Torgerson MDS).
//!
//! Given a symmetric *n × n* dissimilarity (distance) matrix **D**, classical
//! MDS embeds the *n* objects into a *k*-dimensional Euclidean space so that
//! inter-point distances approximate the original dissimilarities.
//!
//! # Algorithm
//!
//! 1. Square element-wise: D²
//! 2. Double-center: B\[i,j\] = −0.5 × (D²\[i,j\] − row\_mean\[i\] −
//!    row\_mean\[j\] + grand\_mean)
//! 3. Eigendecompose B via the Jacobi solver → eigenvalues λ, eigenvectors V
//! 4. Coordinates: X\[i,d\] = √max(λ\[d\], 0) × V\[d\]\[i\] for top-*k*
//! 5. Kruskal stress-1: √(Σ(d\_ij − d̂\_ij)² / Σ d\_ij²)
//!
//! # Complexity
//!
//! O(n³) time (dominated by the Jacobi solver), O(n²) space.
//!
//! # Reference
//!
//! Torgerson, W. S. (1952). Multidimensional scaling: I. Theory and method.
//! *Psychometrika*, 17(4), 401–419.

use alloc::vec::Vec;

use num_traits::{AsPrimitive, Float, ToPrimitive};

use super::jacobi::{JacobiConfig, JacobiError, jacobi_decompose, sort_eigen, validate_config};
use crate::traits::{DenseValuedMatrix2D, Finite, Number};

// ============================================================================
// Configuration
// ============================================================================

/// Configuration for classical MDS.
#[derive(Debug, Clone, PartialEq)]
pub struct MdsConfig {
    /// Number of embedding dimensions (default: 2).
    pub dimensions: usize,
    /// Jacobi eigenvalue solver configuration.
    pub jacobi: JacobiConfig,
}

impl Default for MdsConfig {
    #[inline]
    fn default() -> Self {
        Self { dimensions: 2, jacobi: JacobiConfig::default() }
    }
}

// ============================================================================
// Result
// ============================================================================

/// Result of a classical MDS embedding.
///
/// Contains the embedded coordinates, the top-*k* eigenvalues used, and the
/// Kruskal stress-1 fit measure.
#[derive(Debug, Clone, PartialEq)]
pub struct MdsResult {
    /// Flat coordinate storage: point *i*, dimension *d* → `coords[i * dims +
    /// d]`.
    coordinates: Vec<f64>,
    /// Top-*k* eigenvalues used for the embedding.
    eigenvalues: Vec<f64>,
    /// Kruskal stress-1.
    stress: f64,
    /// Number of points.
    n: usize,
    /// Number of dimensions.
    dims: usize,
}

impl MdsResult {
    /// Returns the coordinates of point *i* (length =
    /// [`dimensions`](Self::dimensions)).
    ///
    /// # Panics
    ///
    /// Panics if `i >= self.num_points()`.
    #[must_use]
    #[inline]
    pub fn point(&self, i: usize) -> &[f64] {
        let start = i * self.dims;
        &self.coordinates[start..start + self.dims]
    }

    /// Returns the flat coordinate storage (length = `num_points() ×
    /// dimensions()`).
    #[must_use]
    #[inline]
    pub fn coordinates_flat(&self) -> &[f64] {
        &self.coordinates
    }

    /// Returns the top-*k* eigenvalues used for embedding.
    #[must_use]
    #[inline]
    pub fn eigenvalues(&self) -> &[f64] {
        &self.eigenvalues
    }

    /// Returns the Kruskal stress-1 goodness-of-fit measure.
    ///
    /// A stress of 0.0 indicates a perfect embedding.
    #[must_use]
    #[inline]
    pub fn stress(&self) -> f64 {
        self.stress
    }

    /// Returns the number of embedding dimensions.
    #[must_use]
    #[inline]
    pub fn dimensions(&self) -> usize {
        self.dims
    }

    /// Returns the number of embedded points.
    #[must_use]
    #[inline]
    pub fn num_points(&self) -> usize {
        self.n
    }
}

// ============================================================================
// Error
// ============================================================================

/// Errors that can occur during classical MDS.
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
pub enum MdsError {
    /// The distance matrix must be square.
    #[error("The distance matrix must be square, but has {rows} rows and {columns} columns.")]
    NonSquareMatrix {
        /// Number of rows.
        rows: usize,
        /// Number of columns.
        columns: usize,
    },
    /// The distance matrix is not symmetric.
    #[error(
        "The distance matrix is not symmetric: value at ({row}, {column}) differs from ({column}, {row})."
    )]
    NonSymmetricMatrix {
        /// Row index.
        row: usize,
        /// Column index.
        column: usize,
    },
    /// A matrix entry is not finite (NaN or ±∞).
    #[error("Found a non-finite value at ({row}, {column}).")]
    NonFiniteValue {
        /// Row index.
        row: usize,
        /// Column index.
        column: usize,
    },
    /// A distance is negative.
    #[error("Found a negative distance at ({row}, {column}).")]
    NegativeDistance {
        /// Row index.
        row: usize,
        /// Column index.
        column: usize,
    },
    /// The distance matrix is empty (0×0).
    #[error("The distance matrix is empty.")]
    EmptyMatrix,
    /// The number of embedding dimensions must be at least 1.
    #[error("The number of embedding dimensions must be at least 1, but got {0}.")]
    InvalidDimensions(usize),
    /// The requested number of dimensions exceeds the number of points.
    #[error(
        "Requested {dimensions} embedding dimensions, but the matrix has only {num_points} points."
    )]
    DimensionsExceedPoints {
        /// Requested dimensions.
        dimensions: usize,
        /// Number of points in the matrix.
        num_points: usize,
    },
    /// The maximum distance is too large; squaring it would overflow f64.
    #[error("The maximum distance {0} is too large; squaring it would overflow f64.")]
    DistanceTooLarge(f64),
    /// A diagonal entry is not zero.
    #[error("The diagonal entry at ({index}, {index}) must be zero, but is {value}.")]
    DiagonalNotZero {
        /// Diagonal index.
        index: usize,
        /// Observed value.
        value: f64,
    },
    /// Need at least 2 points for a meaningful embedding.
    #[error("Need at least 2 points for MDS, but got {0}.")]
    TooFewPoints(usize),
    /// The Jacobi eigenvalue solver failed.
    #[error(transparent)]
    JacobiError(#[from] JacobiError),
}

// ============================================================================
// Trait
// ============================================================================

/// Trait providing classical (Torgerson) Multidimensional Scaling.
///
/// Classical MDS embeds the rows of a symmetric distance matrix into
/// *k*-dimensional Euclidean space by eigendecomposing the double-centered
/// squared-distance matrix.
///
/// # Complexity
///
/// O(n³) time, O(n²) space.
///
/// # Examples
///
/// ```
/// use geometric_traits::{
///     impls::{PaddedMatrix2D, ValuedCSR2D},
///     prelude::*,
///     traits::{DenseValuedMatrix2D, EdgesBuilder},
/// };
///
/// // Build a 3×3 distance matrix for an equilateral triangle (all d=1).
/// let csr: ValuedCSR2D<usize, usize, usize, f64> =
///     GenericEdgesBuilder::<_, ValuedCSR2D<usize, usize, usize, f64>>::default()
///         .expected_number_of_edges(6)
///         .expected_shape((3, 3))
///         .edges(
///             vec![(0, 1, 1.0), (0, 2, 1.0), (1, 0, 1.0), (1, 2, 1.0), (2, 0, 1.0), (2, 1, 1.0)]
///                 .into_iter(),
///         )
///         .build()
///         .unwrap();
///
/// let padded = PaddedMatrix2D::new(
///     csr,
///     |coords: (usize, usize)| {
///         if coords.0 == coords.1 { 0.0 } else { 0.0 }
///     },
/// )
/// .unwrap();
///
/// let config = MdsConfig::default(); // 2D
/// let result = padded.classical_mds(&config).unwrap();
///
/// assert_eq!(result.num_points(), 3);
/// assert_eq!(result.dimensions(), 2);
/// assert!(result.stress() < 1e-6);
/// ```
pub trait ClassicalMds: DenseValuedMatrix2D + Sized
where
    Self::Value: Number + ToPrimitive + Finite,
    Self::RowIndex: AsPrimitive<usize>,
    Self::ColumnIndex: AsPrimitive<usize>,
{
    /// Embeds a distance matrix into Euclidean space via classical MDS.
    ///
    /// # Arguments
    ///
    /// * `config` – MDS configuration (dimensions and Jacobi sub-config).
    ///
    /// # Returns
    ///
    /// An [`MdsResult`] containing the embedding coordinates, eigenvalues,
    /// and Kruskal stress-1.
    ///
    /// # Errors
    ///
    /// Returns an [`MdsError`] if the input is invalid or the Jacobi solver
    /// fails.
    fn classical_mds(&self, config: &MdsConfig) -> Result<MdsResult, MdsError> {
        // ----- Validate config -----
        if config.dimensions == 0 {
            return Err(MdsError::InvalidDimensions(0));
        }
        validate_config(&config.jacobi)?;

        // ----- Read and validate distance matrix -----
        let num_rows: usize = self.number_of_rows().as_();
        let num_cols: usize = self.number_of_columns().as_();
        if num_rows != num_cols {
            return Err(MdsError::NonSquareMatrix { rows: num_rows, columns: num_cols });
        }
        let n = num_rows;
        if n == 0 {
            return Err(MdsError::EmptyMatrix);
        }
        if n == 1 {
            return Err(MdsError::TooFewPoints(1));
        }
        if config.dimensions > n {
            return Err(MdsError::DimensionsExceedPoints {
                dimensions: config.dimensions,
                num_points: n,
            });
        }

        let mut dist = Vec::with_capacity(n * n);
        for row_id in self.row_indices() {
            let row_idx: usize = row_id.as_();
            for (col_id, val) in self.column_indices().zip(self.row_values(row_id)) {
                let col_idx: usize = col_id.as_();
                if !val.is_finite() {
                    return Err(MdsError::NonFiniteValue { row: row_idx, column: col_idx });
                }
                let value = val
                    .to_f64()
                    .ok_or(MdsError::NonFiniteValue { row: row_idx, column: col_idx })?;
                if !value.is_finite() {
                    return Err(MdsError::NonFiniteValue { row: row_idx, column: col_idx });
                }
                if value < 0.0 {
                    return Err(MdsError::NegativeDistance { row: row_idx, column: col_idx });
                }
                dist.push(value);
            }
        }

        // Check diagonal is zero.
        for i in 0..n {
            let d = dist[i * n + i];
            if d != 0.0 {
                return Err(MdsError::DiagonalNotZero { index: i, value: d });
            }
        }

        // Check symmetry.
        for i in 0..n {
            for j in (i + 1)..n {
                let upper = dist[i * n + j];
                let lower = dist[j * n + i];
                let scale = upper.abs().max(lower.abs()).max(1.0);
                if (upper - lower).abs() > 16.0 * f64::EPSILON * scale {
                    return Err(MdsError::NonSymmetricMatrix { row: i, column: j });
                }
            }
        }

        // Force exact symmetry.
        for i in 0..n {
            for j in (i + 1)..n {
                let avg = dist[i * n + j].midpoint(dist[j * n + i]);
                dist[i * n + j] = avg;
                dist[j * n + i] = avg;
            }
        }

        // Guard against overflow: d² would exceed f64::MAX for d > ~1e154.
        let max_dist = dist.iter().copied().fold(0.0_f64, f64::max);
        if max_dist > 1e154 {
            return Err(MdsError::DistanceTooLarge(max_dist));
        }

        // Keep a copy for stress computation.
        let orig_dist = dist.clone();

        // Square, double-center, eigendecompose, embed, compute stress.
        let mut b = square_and_double_center(&dist, n);

        let mut evecs =
            jacobi_decompose(&mut b, n, config.jacobi.tolerance, config.jacobi.max_sweeps)?;
        let mut eigenvalues: Vec<f64> = (0..n).map(|i| b[i * n + i]).collect();
        sort_eigen(&mut eigenvalues, &mut evecs, n);

        let k = config.dimensions;
        let top_eigenvalues: Vec<f64> = eigenvalues[..k].to_vec();
        let mut coordinates = vec![0.0; n * k];
        for d in 0..k {
            let sqrt_lambda = Float::sqrt(eigenvalues[d].max(0.0));
            for i in 0..n {
                coordinates[i * k + d] = sqrt_lambda * evecs[d * n + i];
            }
        }

        let stress = compute_stress(&orig_dist, &coordinates, n, k);

        Ok(MdsResult { coordinates, eigenvalues: top_eigenvalues, stress, n, dims: k })
    }
}

impl<M: DenseValuedMatrix2D> ClassicalMds for M
where
    M::Value: Number + ToPrimitive + Finite,
    M::RowIndex: AsPrimitive<usize>,
    M::ColumnIndex: AsPrimitive<usize>,
{
}

/// Square element-wise and double-center the distance matrix to produce B.
#[allow(clippy::cast_precision_loss)]
fn square_and_double_center(dist: &[f64], n: usize) -> Vec<f64> {
    let mut d_sq = vec![0.0; n * n];
    for (sq, &d) in d_sq.iter_mut().zip(dist.iter()) {
        *sq = d * d;
    }

    let mut row_means = vec![0.0; n];
    for i in 0..n {
        let mut sum = 0.0;
        for j in 0..n {
            sum += d_sq[i * n + j];
        }
        row_means[i] = sum / n as f64;
    }
    let grand_mean: f64 = row_means.iter().sum::<f64>() / n as f64;

    let mut b = vec![0.0; n * n];
    for i in 0..n {
        for j in 0..n {
            b[i * n + j] = -0.5 * (d_sq[i * n + j] - row_means[i] - row_means[j] + grand_mean);
        }
    }

    // Force exact symmetry of B.
    for i in 0..n {
        for j in (i + 1)..n {
            let avg = b[i * n + j].midpoint(b[j * n + i]);
            b[i * n + j] = avg;
            b[j * n + i] = avg;
        }
    }

    b
}

/// Compute Kruskal stress-1: √(Σ(d_ij − d̂_ij)² / Σ d_ij²)
fn compute_stress(orig_dist: &[f64], coordinates: &[f64], n: usize, k: usize) -> f64 {
    let mut numerator = 0.0;
    let mut denominator = 0.0;

    for i in 0..n {
        for j in (i + 1)..n {
            let d_orig = orig_dist[i * n + j];
            // Euclidean distance in the embedding.
            let mut d_embed_sq = 0.0;
            for d in 0..k {
                let diff = coordinates[i * k + d] - coordinates[j * k + d];
                d_embed_sq += diff * diff;
            }
            let d_embed = Float::sqrt(d_embed_sq);
            let residual = d_orig - d_embed;
            numerator += residual * residual;
            denominator += d_orig * d_orig;
        }
    }

    if denominator == 0.0 { 0.0 } else { Float::sqrt(numerator / denominator) }
}