oxiphysics-core 0.1.0

Core types, traits, and abstractions for the OxiPhysics engine
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
#![allow(clippy::needless_range_loop)]
// Copyright 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Persistent homology and topological data analysis.
//!
//! Provides simplicial complexes, filtered complexes, persistence diagrams,
//! Vietoris-Rips filtration, persistence images, and barcode statistics.

#![allow(dead_code)]

use std::collections::HashMap;

// ─────────────────────────────────────────────────────────────────────────────
// Simplex
// ─────────────────────────────────────────────────────────────────────────────

/// A simplex defined by its vertices, dimension, and filtration value.
///
/// A `k`-simplex has `k+1` vertices (e.g., 0-simplex = vertex, 1-simplex = edge).
#[derive(Debug, Clone, PartialEq)]
pub struct Simplex {
    /// Sorted vertex indices forming this simplex.
    pub vertices: Vec<usize>,
    /// Homological dimension (0 = vertex, 1 = edge, 2 = triangle, …).
    pub dimension: usize,
    /// Filtration value at which this simplex enters the complex.
    pub filtration: f64,
}

impl Simplex {
    /// Create a new simplex from a list of vertices and filtration value.
    ///
    /// The dimension is inferred as `vertices.len() - 1`.
    /// Vertices are sorted in ascending order.
    pub fn new(mut vertices: Vec<usize>, filtration: f64) -> Self {
        vertices.sort_unstable();
        let dimension = vertices.len().saturating_sub(1);
        Self {
            vertices,
            dimension,
            filtration,
        }
    }

    /// Return the faces (boundary simplices) of this simplex.
    ///
    /// A `k`-simplex has `k+1` faces of dimension `k-1`, each formed by
    /// removing one vertex.
    pub fn faces(&self) -> Vec<Simplex> {
        if self.dimension == 0 {
            return vec![];
        }
        (0..self.vertices.len())
            .map(|i| {
                let verts: Vec<usize> = self
                    .vertices
                    .iter()
                    .enumerate()
                    .filter(|(j, _)| *j != i)
                    .map(|(_, &v)| v)
                    .collect();
                Simplex::new(verts, self.filtration)
            })
            .collect()
    }

    /// Return `true` if this simplex contains all vertices of `other`.
    pub fn contains(&self, other: &Simplex) -> bool {
        other.vertices.iter().all(|v| self.vertices.contains(v))
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// SimplicialComplex
// ─────────────────────────────────────────────────────────────────────────────

/// A finite simplicial complex with boundary operators and topological invariants.
#[derive(Debug, Clone)]
pub struct SimplicialComplex {
    /// All simplices grouped by dimension.
    pub simplices: Vec<Vec<Simplex>>,
    /// Maximum dimension present in the complex.
    pub max_dimension: usize,
}

impl SimplicialComplex {
    /// Create an empty simplicial complex.
    pub fn new() -> Self {
        Self {
            simplices: vec![],
            max_dimension: 0,
        }
    }

    /// Add a simplex (and all its faces recursively) to the complex.
    pub fn add_simplex(&mut self, s: Simplex) {
        // First ensure faces are present
        let faces = s.faces();
        for face in faces {
            self.add_simplex(face);
        }
        let dim = s.dimension;
        while self.simplices.len() <= dim {
            self.simplices.push(vec![]);
        }
        if dim > self.max_dimension {
            self.max_dimension = dim;
        }
        // Avoid duplicates
        if !self.simplices[dim].iter().any(|x| x.vertices == s.vertices) {
            self.simplices[dim].push(s);
        }
    }

    /// Return the number of simplices of dimension `k`.
    pub fn count(&self, k: usize) -> usize {
        self.simplices.get(k).map_or(0, |v| v.len())
    }

    /// Compute the Euler characteristic χ = Σ (-1)^k * f_k.
    ///
    /// Where `f_k` is the number of `k`-simplices.
    pub fn euler_characteristic(&self) -> i64 {
        self.simplices
            .iter()
            .enumerate()
            .map(|(k, s)| {
                if k % 2 == 0 {
                    s.len() as i64
                } else {
                    -(s.len() as i64)
                }
            })
            .sum()
    }

    /// Compute the boundary matrix for dimension `k`.
    ///
    /// Entry `[i][j]` is 1 if the `j`-th `(k-1)`-simplex is a face of the
    /// `i`-th `k`-simplex (with sign ignored for simplicity), else 0.
    pub fn boundary_matrix(&self, k: usize) -> Vec<Vec<i32>> {
        if k == 0 || k > self.max_dimension {
            return vec![];
        }
        let k_simplices = &self.simplices[k];
        let km1_simplices = &self.simplices[k - 1];
        let rows = k_simplices.len();
        let cols = km1_simplices.len();
        let mut mat = vec![vec![0i32; cols]; rows];
        for (i, sigma) in k_simplices.iter().enumerate() {
            for (j, tau) in km1_simplices.iter().enumerate() {
                if sigma.contains(tau) {
                    mat[i][j] = 1;
                }
            }
        }
        mat
    }
}

impl Default for SimplicialComplex {
    fn default() -> Self {
        Self::new()
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// FilteredComplex
// ─────────────────────────────────────────────────────────────────────────────

/// A simplicial complex with a filtration ordering.
///
/// Simplices are ordered by their filtration values, and then by dimension.
/// The boundary matrix algorithm operates on this ordering.
#[derive(Debug, Clone)]
pub struct FilteredComplex {
    /// All simplices in filtration order (sorted by `filtration` then `dimension`).
    pub ordered_simplices: Vec<Simplex>,
}

impl FilteredComplex {
    /// Create a filtered complex from a list of simplices.
    ///
    /// Simplices are sorted by filtration value then dimension.
    pub fn new(mut simplices: Vec<Simplex>) -> Self {
        simplices.sort_by(|a, b| {
            a.filtration
                .partial_cmp(&b.filtration)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then(a.dimension.cmp(&b.dimension))
        });
        Self {
            ordered_simplices: simplices,
        }
    }

    /// Return the number of simplices in this complex.
    pub fn len(&self) -> usize {
        self.ordered_simplices.len()
    }

    /// Return `true` if the complex is empty.
    pub fn is_empty(&self) -> bool {
        self.ordered_simplices.is_empty()
    }

    /// Compute the boundary matrix column for simplex at index `i`.
    ///
    /// Returns a sorted vector of indices `j < i` such that
    /// `ordered_simplices[j]` is a codimension-1 face of `ordered_simplices[i]`.
    pub fn boundary_column(&self, i: usize) -> Vec<usize> {
        let sigma = &self.ordered_simplices[i];
        if sigma.dimension == 0 {
            return vec![];
        }
        let faces = sigma.faces();
        let mut col = Vec::new();
        for face in &faces {
            if let Some(j) = self.ordered_simplices[..i]
                .iter()
                .rposition(|s| s.vertices == face.vertices)
            {
                col.push(j);
            }
        }
        col.sort_unstable();
        col
    }

    /// Run the standard persistence algorithm (column reduction).
    ///
    /// Returns the pivot pairs `(i, j)` where column `j` has pivot row `i`,
    /// meaning simplex `j` dies when simplex `i` is born.
    pub fn reduce(&self) -> Vec<(usize, usize)> {
        let n = self.ordered_simplices.len();
        // Reduced columns stored as sorted Vec<usize>
        let mut columns: Vec<Vec<usize>> = (0..n).map(|i| self.boundary_column(i)).collect();
        // low[j] = lowest set bit index in column j, or None
        let low = |col: &Vec<usize>| col.last().copied();
        let mut pivot_col: HashMap<usize, usize> = HashMap::new();
        let mut pairs = Vec::new();

        for j in 0..n {
            loop {
                if columns[j].is_empty() {
                    break;
                }
                let l = low(&columns[j]).expect("column j is non-empty");
                if let Some(&k) = pivot_col.get(&l) {
                    // Add column k to column j (XOR-style over Z/2Z)
                    let col_k = columns[k].clone();
                    let col_j = std::mem::take(&mut columns[j]);
                    columns[j] = xor_sorted_vecs(col_j, col_k);
                } else {
                    pivot_col.insert(l, j);
                    pairs.push((l, j));
                    break;
                }
            }
        }
        pairs
    }
}

/// Symmetric difference of two sorted vectors (XOR over Z/2Z).
fn xor_sorted_vecs(mut a: Vec<usize>, mut b: Vec<usize>) -> Vec<usize> {
    a.sort_unstable();
    b.sort_unstable();
    let mut result = Vec::new();
    let (mut i, mut j) = (0, 0);
    while i < a.len() && j < b.len() {
        match a[i].cmp(&b[j]) {
            std::cmp::Ordering::Less => {
                result.push(a[i]);
                i += 1;
            }
            std::cmp::Ordering::Greater => {
                result.push(b[j]);
                j += 1;
            }
            std::cmp::Ordering::Equal => {
                // Cancel (both drop out)
                i += 1;
                j += 1;
            }
        }
    }
    result.extend_from_slice(&a[i..]);
    result.extend_from_slice(&b[j..]);
    result
}

// ─────────────────────────────────────────────────────────────────────────────
// PersistenceDiagram
// ─────────────────────────────────────────────────────────────────────────────

/// A persistence diagram: collection of birth-death pairs per dimension.
#[derive(Debug, Clone)]
pub struct PersistenceDiagram {
    /// Birth-death pairs `(birth, death)` per homological dimension.
    pub pairs: Vec<(f64, f64, usize)>,
    /// Essential classes (never destroyed): `(birth, dimension)`.
    pub essential: Vec<(f64, usize)>,
}

impl PersistenceDiagram {
    /// Create a `PersistenceDiagram` from a `FilteredComplex` using standard reduction.
    pub fn from_filtered_complex(fc: &FilteredComplex) -> Self {
        let pairs_idx = fc.reduce();
        let n = fc.ordered_simplices.len();
        let mut paired = vec![false; n];
        let mut pairs = Vec::new();
        let mut essential = Vec::new();

        for (birth_idx, death_idx) in &pairs_idx {
            let b = fc.ordered_simplices[*birth_idx].filtration;
            let d = fc.ordered_simplices[*death_idx].filtration;
            let dim = fc.ordered_simplices[*birth_idx].dimension;
            if (d - b).abs() > 1e-15 {
                pairs.push((b, d, dim));
            }
            paired[*birth_idx] = true;
            paired[*death_idx] = true;
        }
        // Unpaired simplices → essential classes
        for (i, s) in fc.ordered_simplices.iter().enumerate() {
            if !paired[i] {
                essential.push((s.filtration, s.dimension));
            }
        }
        Self { pairs, essential }
    }

    /// Return Betti numbers up to dimension `max_dim` at filtration value `t`.
    ///
    /// β_k(t) = number of classes born at or before `t` that are still alive at `t`.
    pub fn betti_numbers(&self, t: f64, max_dim: usize) -> Vec<usize> {
        let mut betti = vec![0usize; max_dim + 1];
        for &(b, d, dim) in &self.pairs {
            if dim <= max_dim && b <= t && d > t {
                betti[dim] += 1;
            }
        }
        for &(b, dim) in &self.essential {
            if dim <= max_dim && b <= t {
                betti[dim] += 1;
            }
        }
        betti
    }

    /// Return total persistence (sum of lifetimes for all finite pairs).
    pub fn total_persistence(&self) -> f64 {
        self.pairs.iter().map(|(b, d, _)| d - b).sum()
    }

    /// Return all finite pairs for dimension `k`.
    pub fn pairs_for_dim(&self, k: usize) -> Vec<(f64, f64)> {
        self.pairs
            .iter()
            .filter(|&&(_, _, d)| d == k)
            .map(|&(b, d, _)| (b, d))
            .collect()
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// RipsFiltration
// ─────────────────────────────────────────────────────────────────────────────

/// Vietoris-Rips filtration built from a point cloud or distance matrix.
#[derive(Debug, Clone)]
pub struct RipsFiltration {
    /// Distance matrix (symmetric, zero diagonal).
    pub distances: Vec<Vec<f64>>,
    /// Number of points.
    pub n_points: usize,
    /// Maximum filtration radius.
    pub max_radius: f64,
    /// Maximum simplex dimension to build.
    pub max_dim: usize,
}

impl RipsFiltration {
    /// Build a Rips filtration from a 2D point cloud (Euclidean distances).
    ///
    /// `max_radius` controls how far to grow the filtration.
    /// `max_dim` controls the maximum simplex dimension built.
    pub fn from_point_cloud(points: &[[f64; 2]], max_radius: f64, max_dim: usize) -> Self {
        let n = points.len();
        let mut distances = vec![vec![0.0_f64; n]; n];
        for i in 0..n {
            for j in (i + 1)..n {
                let dx = points[i][0] - points[j][0];
                let dy = points[i][1] - points[j][1];
                let d = (dx * dx + dy * dy).sqrt();
                distances[i][j] = d;
                distances[j][i] = d;
            }
        }
        Self {
            distances,
            n_points: n,
            max_radius,
            max_dim,
        }
    }

    /// Build a Rips filtration from a precomputed distance matrix.
    pub fn from_distance_matrix(distances: Vec<Vec<f64>>, max_radius: f64, max_dim: usize) -> Self {
        let n = distances.len();
        Self {
            distances,
            n_points: n,
            max_radius,
            max_dim,
        }
    }

    /// Build a `FilteredComplex` from this filtration.
    pub fn build_complex(&self) -> FilteredComplex {
        let mut simplices = Vec::new();
        let n = self.n_points;

        // 0-simplices (vertices) — born at filtration 0
        for i in 0..n {
            simplices.push(Simplex::new(vec![i], 0.0));
        }

        // 1-simplices (edges)
        for i in 0..n {
            for j in (i + 1)..n {
                let d = self.distances[i][j];
                if d <= self.max_radius {
                    simplices.push(Simplex::new(vec![i, j], d));
                }
            }
        }

        // 2-simplices (triangles) if max_dim >= 2
        if self.max_dim >= 2 {
            for i in 0..n {
                for j in (i + 1)..n {
                    for k in (j + 1)..n {
                        let d = self.distances[i][j]
                            .max(self.distances[i][k])
                            .max(self.distances[j][k]);
                        if d <= self.max_radius {
                            simplices.push(Simplex::new(vec![i, j, k], d));
                        }
                    }
                }
            }
        }

        // 3-simplices (tetrahedra) if max_dim >= 3
        if self.max_dim >= 3 && n >= 4 {
            for i in 0..n {
                for j in (i + 1)..n {
                    for k in (j + 1)..n {
                        for l in (k + 1)..n {
                            let d = self.distances[i][j]
                                .max(self.distances[i][k])
                                .max(self.distances[i][l])
                                .max(self.distances[j][k])
                                .max(self.distances[j][l])
                                .max(self.distances[k][l]);
                            if d <= self.max_radius {
                                simplices.push(Simplex::new(vec![i, j, k, l], d));
                            }
                        }
                    }
                }
            }
        }

        FilteredComplex::new(simplices)
    }

    /// Compute persistence diagram for this Rips filtration.
    pub fn persistence_diagram(&self) -> PersistenceDiagram {
        let fc = self.build_complex();
        PersistenceDiagram::from_filtered_complex(&fc)
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// PersistenceImage
// ─────────────────────────────────────────────────────────────────────────────

/// Persistence image: a pixelated representation of a persistence diagram.
///
/// Points are represented in birth-persistence coordinates, then convolved
/// with a Gaussian kernel and discretized onto a pixel grid.
#[derive(Debug, Clone)]
pub struct PersistenceImage {
    /// Pixel grid (row-major, rows = persistence axis, cols = birth axis).
    pub pixels: Vec<Vec<f64>>,
    /// Number of grid rows (persistence resolution).
    pub n_rows: usize,
    /// Number of grid cols (birth resolution).
    pub n_cols: usize,
    /// Minimum birth value.
    pub birth_min: f64,
    /// Maximum birth value.
    pub birth_max: f64,
    /// Maximum persistence value.
    pub pers_max: f64,
    /// Gaussian bandwidth (sigma).
    pub sigma: f64,
}

impl PersistenceImage {
    /// Build a persistence image from a persistence diagram.
    ///
    /// `n_rows` and `n_cols` control resolution.
    /// `sigma` is the Gaussian kernel bandwidth.
    pub fn from_diagram(
        diagram: &PersistenceDiagram,
        n_rows: usize,
        n_cols: usize,
        sigma: f64,
    ) -> Self {
        // Collect finite pairs in birth-persistence coords
        let pts: Vec<(f64, f64)> = diagram
            .pairs
            .iter()
            .filter(|&&(b, d, _)| d.is_finite() && d > b)
            .map(|&(b, d, _)| (b, d - b))
            .collect();

        if pts.is_empty() {
            return Self {
                pixels: vec![vec![0.0; n_cols]; n_rows],
                n_rows,
                n_cols,
                birth_min: 0.0,
                birth_max: 1.0,
                pers_max: 1.0,
                sigma,
            };
        }

        let birth_min = pts.iter().map(|(b, _)| *b).fold(f64::INFINITY, f64::min);
        let birth_max = pts
            .iter()
            .map(|(b, _)| *b)
            .fold(f64::NEG_INFINITY, f64::max);
        let pers_max = pts
            .iter()
            .map(|(_, p)| *p)
            .fold(f64::NEG_INFINITY, f64::max);

        let birth_range = (birth_max - birth_min).max(1e-10);
        let pers_range = pers_max.max(1e-10);

        let mut pixels = vec![vec![0.0_f64; n_cols]; n_rows];
        let two_sigma_sq = 2.0 * sigma * sigma;

        for (bx, py) in &pts {
            // Ramp weight: persistence / max_persistence
            let weight = py / pers_range;
            for row in 0..n_rows {
                // row 0 = high persistence
                let p_grid =
                    pers_range * (n_rows - 1 - row) as f64 / (n_rows as f64 - 1.0).max(1.0);
                for col in 0..n_cols {
                    let b_grid =
                        birth_min + birth_range * col as f64 / (n_cols as f64 - 1.0).max(1.0);
                    let db = bx - b_grid;
                    let dp = py - p_grid;
                    let gauss = (-(db * db + dp * dp) / two_sigma_sq).exp();
                    pixels[row][col] += weight * gauss;
                }
            }
        }

        Self {
            pixels,
            n_rows,
            n_cols,
            birth_min,
            birth_max,
            pers_max,
            sigma,
        }
    }

    /// Flatten the pixel grid to a 1D feature vector (row-major).
    pub fn to_vector(&self) -> Vec<f64> {
        self.pixels
            .iter()
            .flat_map(|row| row.iter().copied())
            .collect()
    }

    /// Return the maximum pixel value.
    pub fn max_value(&self) -> f64 {
        self.pixels
            .iter()
            .flat_map(|row| row.iter())
            .cloned()
            .fold(f64::NEG_INFINITY, f64::max)
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// BarcodeStatistics
// ─────────────────────────────────────────────────────────────────────────────

/// Summary statistics computed from a persistence barcode.
#[derive(Debug, Clone)]
pub struct BarcodeStatistics {
    /// Sum of finite bar lengths.
    pub total_persistence: f64,
    /// Mean finite bar length.
    pub mean_persistence: f64,
    /// Median finite bar length.
    pub median_persistence: f64,
    /// Maximum finite bar length.
    pub max_persistence: f64,
    /// Number of finite bars.
    pub n_finite: usize,
    /// Number of essential (infinite) classes.
    pub n_essential: usize,
    /// Bottleneck distance approximation to the empty diagram.
    ///
    /// Equals the half-persistence of the most persistent finite bar.
    pub bottleneck_to_empty: f64,
}

impl BarcodeStatistics {
    /// Compute statistics from a `PersistenceDiagram`.
    pub fn from_diagram(diagram: &PersistenceDiagram) -> Self {
        let mut lifetimes: Vec<f64> = diagram
            .pairs
            .iter()
            .filter(|&&(b, d, _)| d.is_finite() && d > b)
            .map(|&(b, d, _)| d - b)
            .collect();
        lifetimes.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let n_finite = lifetimes.len();
        let total_persistence = lifetimes.iter().sum::<f64>();
        let mean_persistence = if n_finite > 0 {
            total_persistence / n_finite as f64
        } else {
            0.0
        };
        let median_persistence = if n_finite > 0 {
            if n_finite % 2 == 1 {
                lifetimes[n_finite / 2]
            } else {
                (lifetimes[n_finite / 2 - 1] + lifetimes[n_finite / 2]) / 2.0
            }
        } else {
            0.0
        };
        let max_persistence = lifetimes.last().copied().unwrap_or(0.0);
        let bottleneck_to_empty = max_persistence / 2.0;
        let n_essential = diagram.essential.len();

        Self {
            total_persistence,
            mean_persistence,
            median_persistence,
            max_persistence,
            n_finite,
            n_essential,
            bottleneck_to_empty,
        }
    }

    /// Approximate bottleneck distance between two diagrams.
    ///
    /// Uses greedy matching by L∞ distance in birth-persistence coordinates.
    pub fn bottleneck_distance(a: &PersistenceDiagram, b: &PersistenceDiagram) -> f64 {
        let pts_a: Vec<(f64, f64)> = a
            .pairs
            .iter()
            .filter(|&&(b_, d, _)| d.is_finite() && d > b_)
            .map(|&(b_, d, _)| (b_, d - b_))
            .collect();
        let pts_b: Vec<(f64, f64)> = b
            .pairs
            .iter()
            .filter(|&&(b_, d, _)| d.is_finite() && d > b_)
            .map(|&(b_, d, _)| (b_, d - b_))
            .collect();

        let linf = |pa: (f64, f64), pb: (f64, f64)| -> f64 {
            (pa.0 - pb.0).abs().max((pa.1 - pb.1).abs())
        };
        let diag_dist = |p: (f64, f64)| -> f64 { p.1 / 2.0 };

        let mut max_dist = 0.0_f64;
        let mut used = vec![false; pts_b.len()];

        for &pa in &pts_a {
            let best = pts_b
                .iter()
                .enumerate()
                .filter(|(idx, _)| !used[*idx])
                .map(|(idx, &pb)| (idx, linf(pa, pb)))
                .min_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or(std::cmp::Ordering::Equal));
            match best {
                Some((idx, d)) if d < diag_dist(pa) => {
                    used[idx] = true;
                    max_dist = max_dist.max(d);
                }
                _ => {
                    max_dist = max_dist.max(diag_dist(pa));
                }
            }
        }
        for (idx, &pb) in pts_b.iter().enumerate() {
            if !used[idx] {
                max_dist = max_dist.max(diag_dist(pb));
            }
        }
        max_dist
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

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

    // ── Simplex ───────────────────────────────────────────────────────────────

    #[test]
    fn test_simplex_vertex() {
        let s = Simplex::new(vec![3], 0.5);
        assert_eq!(s.dimension, 0);
        assert_eq!(s.vertices, vec![3]);
        assert!((s.filtration - 0.5_f64).abs() < 1e-12);
    }

    #[test]
    fn test_simplex_edge_dimension() {
        let s = Simplex::new(vec![0, 1], 1.0);
        assert_eq!(s.dimension, 1);
    }

    #[test]
    fn test_simplex_triangle_dimension() {
        let s = Simplex::new(vec![0, 1, 2], 2.0);
        assert_eq!(s.dimension, 2);
    }

    #[test]
    fn test_simplex_vertices_sorted() {
        let s = Simplex::new(vec![3, 1, 0, 2], 0.0);
        assert_eq!(s.vertices, vec![0, 1, 2, 3]);
    }

    #[test]
    fn test_simplex_vertex_has_no_faces() {
        let s = Simplex::new(vec![5], 0.0);
        assert!(s.faces().is_empty());
    }

    #[test]
    fn test_simplex_edge_has_two_faces() {
        let s = Simplex::new(vec![0, 1], 1.0);
        let faces = s.faces();
        assert_eq!(faces.len(), 2);
        assert!(faces.iter().all(|f| f.dimension == 0));
    }

    #[test]
    fn test_simplex_triangle_has_three_faces() {
        let s = Simplex::new(vec![0, 1, 2], 1.5);
        let faces = s.faces();
        assert_eq!(faces.len(), 3);
        assert!(faces.iter().all(|f| f.dimension == 1));
    }

    #[test]
    fn test_simplex_contains() {
        let edge = Simplex::new(vec![0, 1], 1.0);
        let v0 = Simplex::new(vec![0], 0.0);
        let v2 = Simplex::new(vec![2], 0.0);
        assert!(edge.contains(&v0));
        assert!(!edge.contains(&v2));
    }

    // ── SimplicialComplex ─────────────────────────────────────────────────────

    #[test]
    fn test_simplicial_complex_empty() {
        let sc = SimplicialComplex::new();
        assert_eq!(sc.count(0), 0);
        assert_eq!(sc.euler_characteristic(), 0);
    }

    #[test]
    fn test_simplicial_complex_add_triangle() {
        let mut sc = SimplicialComplex::new();
        sc.add_simplex(Simplex::new(vec![0, 1, 2], 1.0));
        assert_eq!(sc.count(0), 3); // 3 vertices
        assert_eq!(sc.count(1), 3); // 3 edges
        assert_eq!(sc.count(2), 1); // 1 triangle
    }

    #[test]
    fn test_simplicial_complex_euler_triangle() {
        let mut sc = SimplicialComplex::new();
        sc.add_simplex(Simplex::new(vec![0, 1, 2], 1.0));
        // χ = V - E + F = 3 - 3 + 1 = 1
        assert_eq!(sc.euler_characteristic(), 1);
    }

    #[test]
    fn test_simplicial_complex_no_duplicates() {
        let mut sc = SimplicialComplex::new();
        sc.add_simplex(Simplex::new(vec![0, 1], 1.0));
        sc.add_simplex(Simplex::new(vec![0, 1], 2.0)); // duplicate
        assert_eq!(sc.count(1), 1);
    }

    #[test]
    fn test_simplicial_complex_boundary_matrix_edge() {
        let mut sc = SimplicialComplex::new();
        sc.add_simplex(Simplex::new(vec![0, 1], 1.0));
        sc.add_simplex(Simplex::new(vec![1, 2], 1.0));
        let mat = sc.boundary_matrix(1);
        assert_eq!(mat.len(), 2); // 2 edges
    }

    #[test]
    fn test_simplicial_complex_default() {
        let sc = SimplicialComplex::default();
        assert_eq!(sc.count(0), 0);
    }

    // ── FilteredComplex ───────────────────────────────────────────────────────

    #[test]
    fn test_filtered_complex_ordering() {
        let simplices = vec![
            Simplex::new(vec![0, 1], 2.0),
            Simplex::new(vec![0], 0.0),
            Simplex::new(vec![1], 0.0),
        ];
        let fc = FilteredComplex::new(simplices);
        // Vertices first (dim 0, filt 0) then edge (dim 1, filt 2)
        assert_eq!(fc.ordered_simplices[0].dimension, 0);
        assert_eq!(fc.ordered_simplices[2].dimension, 1);
    }

    #[test]
    fn test_filtered_complex_len() {
        let simplices = vec![
            Simplex::new(vec![0], 0.0),
            Simplex::new(vec![1], 0.0),
            Simplex::new(vec![0, 1], 1.0),
        ];
        let fc = FilteredComplex::new(simplices);
        assert_eq!(fc.len(), 3);
        assert!(!fc.is_empty());
    }

    #[test]
    fn test_filtered_complex_empty() {
        let fc = FilteredComplex::new(vec![]);
        assert_eq!(fc.len(), 0);
        assert!(fc.is_empty());
    }

    #[test]
    fn test_filtered_complex_boundary_column_vertex() {
        let simplices = vec![Simplex::new(vec![0], 0.0)];
        let fc = FilteredComplex::new(simplices);
        assert!(fc.boundary_column(0).is_empty());
    }

    #[test]
    fn test_filtered_complex_reduce_two_points() {
        let simplices = vec![
            Simplex::new(vec![0], 0.0),
            Simplex::new(vec![1], 0.0),
            Simplex::new(vec![0, 1], 1.0),
        ];
        let fc = FilteredComplex::new(simplices);
        let pairs = fc.reduce();
        // The edge should pair with one of the vertices
        assert!(!pairs.is_empty());
    }

    // ── PersistenceDiagram ────────────────────────────────────────────────────

    #[test]
    fn test_persistence_diagram_two_points() {
        let simplices = vec![
            Simplex::new(vec![0], 0.0),
            Simplex::new(vec![1], 0.0),
            Simplex::new(vec![0, 1], 1.5),
        ];
        let fc = FilteredComplex::new(simplices);
        let diag = PersistenceDiagram::from_filtered_complex(&fc);
        // H0: one component dies when edge is added
        assert!(!diag.pairs.is_empty() || !diag.essential.is_empty());
    }

    #[test]
    fn test_persistence_diagram_betti_numbers() {
        let simplices = vec![
            Simplex::new(vec![0], 0.0),
            Simplex::new(vec![1], 0.0),
            Simplex::new(vec![0, 1], 1.0),
        ];
        let fc = FilteredComplex::new(simplices);
        let diag = PersistenceDiagram::from_filtered_complex(&fc);
        // At t=0.5: 2 components, at t=2.0: 1 component
        let b0 = diag.betti_numbers(2.0, 1);
        assert_eq!(b0[0], 1);
    }

    #[test]
    fn test_persistence_diagram_total_persistence() {
        let simplices = vec![
            Simplex::new(vec![0], 0.0),
            Simplex::new(vec![1], 0.0),
            Simplex::new(vec![0, 1], 1.0),
        ];
        let fc = FilteredComplex::new(simplices);
        let diag = PersistenceDiagram::from_filtered_complex(&fc);
        assert!(diag.total_persistence() >= 0.0);
    }

    // ── RipsFiltration ────────────────────────────────────────────────────────

    #[test]
    fn test_rips_from_point_cloud_distances() {
        let pts = [[0.0_f64, 0.0_f64], [1.0, 0.0], [0.0, 1.0]];
        let rips = RipsFiltration::from_point_cloud(&pts, 2.0, 2);
        assert_eq!(rips.n_points, 3);
        assert!((rips.distances[0][1] - 1.0_f64).abs() < 1e-12);
    }

    #[test]
    fn test_rips_build_complex_vertices() {
        let pts = [[0.0_f64, 0.0_f64], [1.0, 0.0], [2.0, 0.0]];
        let rips = RipsFiltration::from_point_cloud(&pts, 5.0, 1);
        let fc = rips.build_complex();
        let n_vertices = fc
            .ordered_simplices
            .iter()
            .filter(|s| s.dimension == 0)
            .count();
        assert_eq!(n_vertices, 3);
    }

    #[test]
    fn test_rips_build_complex_edges_within_radius() {
        let pts = [[0.0_f64, 0.0_f64], [1.0, 0.0], [10.0, 0.0]];
        let rips = RipsFiltration::from_point_cloud(&pts, 1.5, 1);
        let fc = rips.build_complex();
        let n_edges = fc
            .ordered_simplices
            .iter()
            .filter(|s| s.dimension == 1)
            .count();
        // Only edge [0,1] has dist 1.0 ≤ 1.5
        assert_eq!(n_edges, 1);
    }

    #[test]
    fn test_rips_from_distance_matrix() {
        let d = vec![
            vec![0.0_f64, 1.0, 2.0],
            vec![1.0, 0.0, 1.5],
            vec![2.0, 1.5, 0.0],
        ];
        let rips = RipsFiltration::from_distance_matrix(d, 2.0, 2);
        assert_eq!(rips.n_points, 3);
    }

    #[test]
    fn test_rips_persistence_diagram_single_point() {
        let pts = [[0.0_f64, 0.0_f64]];
        let rips = RipsFiltration::from_point_cloud(&pts, 1.0, 0);
        let diag = rips.persistence_diagram();
        // Single point: one essential H0 class
        assert!(!diag.essential.is_empty());
    }

    #[test]
    fn test_rips_triangle_has_triangles() {
        let pts = [[0.0_f64, 0.0_f64], [1.0, 0.0], [0.5, 1.0]];
        let rips = RipsFiltration::from_point_cloud(&pts, 3.0, 2);
        let fc = rips.build_complex();
        let n_tri = fc
            .ordered_simplices
            .iter()
            .filter(|s| s.dimension == 2)
            .count();
        assert_eq!(n_tri, 1);
    }

    // ── PersistenceImage ──────────────────────────────────────────────────────

    #[test]
    fn test_persistence_image_shape() {
        let diag = PersistenceDiagram {
            pairs: vec![(0.0, 1.0, 0), (0.5, 2.0, 0)],
            essential: vec![],
        };
        let img = PersistenceImage::from_diagram(&diag, 10, 10, 0.1);
        assert_eq!(img.pixels.len(), 10);
        assert_eq!(img.pixels[0].len(), 10);
    }

    #[test]
    fn test_persistence_image_non_negative() {
        let diag = PersistenceDiagram {
            pairs: vec![(0.0, 1.0, 0), (0.5, 2.0, 1)],
            essential: vec![],
        };
        let img = PersistenceImage::from_diagram(&diag, 8, 8, 0.2);
        for row in &img.pixels {
            for &v in row {
                assert!(v >= 0.0);
            }
        }
    }

    #[test]
    fn test_persistence_image_empty_diagram() {
        let diag = PersistenceDiagram {
            pairs: vec![],
            essential: vec![],
        };
        let img = PersistenceImage::from_diagram(&diag, 5, 5, 0.1);
        let total: f64 = img.to_vector().iter().sum();
        assert!(total.abs() < 1e-12);
    }

    #[test]
    fn test_persistence_image_to_vector_length() {
        let diag = PersistenceDiagram {
            pairs: vec![(0.0, 1.0, 0)],
            essential: vec![],
        };
        let img = PersistenceImage::from_diagram(&diag, 6, 7, 0.1);
        assert_eq!(img.to_vector().len(), 42);
    }

    #[test]
    fn test_persistence_image_max_value_positive() {
        let diag = PersistenceDiagram {
            pairs: vec![(0.0, 1.0, 0)],
            essential: vec![],
        };
        let img = PersistenceImage::from_diagram(&diag, 5, 5, 0.5);
        assert!(img.max_value() > 0.0);
    }

    // ── BarcodeStatistics ─────────────────────────────────────────────────────

    #[test]
    fn test_barcode_stats_empty() {
        let diag = PersistenceDiagram {
            pairs: vec![],
            essential: vec![],
        };
        let stats = BarcodeStatistics::from_diagram(&diag);
        assert_eq!(stats.n_finite, 0);
        assert!((stats.total_persistence).abs() < 1e-12);
    }

    #[test]
    fn test_barcode_stats_single_bar() {
        let diag = PersistenceDiagram {
            pairs: vec![(0.0, 2.0, 0)],
            essential: vec![],
        };
        let stats = BarcodeStatistics::from_diagram(&diag);
        assert_eq!(stats.n_finite, 1);
        assert!((stats.total_persistence - 2.0_f64).abs() < 1e-12);
        assert!((stats.mean_persistence - 2.0_f64).abs() < 1e-12);
        assert!((stats.max_persistence - 2.0_f64).abs() < 1e-12);
    }

    #[test]
    fn test_barcode_stats_median_odd() {
        let diag = PersistenceDiagram {
            pairs: vec![(0.0, 1.0, 0), (0.0, 2.0, 0), (0.0, 3.0, 0)],
            essential: vec![],
        };
        let stats = BarcodeStatistics::from_diagram(&diag);
        assert!((stats.median_persistence - 2.0_f64).abs() < 1e-12);
    }

    #[test]
    fn test_barcode_stats_median_even() {
        let diag = PersistenceDiagram {
            pairs: vec![(0.0, 1.0, 0), (0.0, 3.0, 0)],
            essential: vec![],
        };
        let stats = BarcodeStatistics::from_diagram(&diag);
        assert!((stats.median_persistence - 2.0_f64).abs() < 1e-12);
    }

    #[test]
    fn test_barcode_stats_essential_counted() {
        let diag = PersistenceDiagram {
            pairs: vec![],
            essential: vec![(0.0, 0), (0.0, 1)],
        };
        let stats = BarcodeStatistics::from_diagram(&diag);
        assert_eq!(stats.n_essential, 2);
    }

    #[test]
    fn test_barcode_bottleneck_identical() {
        let diag = PersistenceDiagram {
            pairs: vec![(0.0, 1.0, 0), (1.0, 3.0, 1)],
            essential: vec![],
        };
        let d = BarcodeStatistics::bottleneck_distance(&diag, &diag);
        assert!(d < 1e-12, "identical diagrams have bottleneck distance 0");
    }

    #[test]
    fn test_barcode_bottleneck_nonneg() {
        let a = PersistenceDiagram {
            pairs: vec![(0.0, 1.0, 0)],
            essential: vec![],
        };
        let b = PersistenceDiagram {
            pairs: vec![(0.1, 1.2, 0)],
            essential: vec![],
        };
        assert!(BarcodeStatistics::bottleneck_distance(&a, &b) >= 0.0);
    }

    #[test]
    fn test_barcode_bottleneck_empty_vs_nonempty() {
        let empty = PersistenceDiagram {
            pairs: vec![],
            essential: vec![],
        };
        let nonempty = PersistenceDiagram {
            pairs: vec![(0.0, 2.0, 0)],
            essential: vec![],
        };
        let d = BarcodeStatistics::bottleneck_distance(&empty, &nonempty);
        // Should equal diag_dist of (0, 2) = 1.0
        assert!((d - 1.0_f64).abs() < 1e-12);
    }

    #[test]
    fn test_xor_sorted_vecs_cancel() {
        let a = vec![0, 1, 2];
        let b = vec![1, 2, 3];
        let result = xor_sorted_vecs(a, b);
        assert_eq!(result, vec![0, 3]);
    }

    #[test]
    fn test_xor_sorted_vecs_empty() {
        let result = xor_sorted_vecs(vec![], vec![1, 2]);
        assert_eq!(result, vec![1, 2]);
    }
}