gam-sae 0.3.155

Sparse-autoencoder latent-manifold terms for the gam penalized-likelihood engine
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
//! First-class multi-chart manifold atoms (#1890).
//!
//! The numerical SAE representation stores one decoder block and one routing
//! column per chart.  A registered [`ManifoldChartAtlas`] quotients that storage
//! into one *semantic* atom without changing the represented function:
//!
//! ```text
//! sum_{c in A} a_c gamma_c
//!   = (sum_c a_c) sum_c [a_c / (sum_j a_j)] gamma_c.
//! ```
//!
//! The bracketed weights are a partition of unity on the atlas support and the
//! prefactor is the atlas activation.  Thus registration is image-exact, keeps
//! the independently valid local coordinates at a pole or half-twist, and does
//! not duplicate reconstruction logic.  Ordinary orientation-preserving
//! over-tiles can still be physically fused; seams that must survive are stored
//! here with their exact unit-speed affine transition.

use std::collections::{BTreeMap, BTreeSet, VecDeque};

use super::SaeManifoldTerm;

/// Geometric role of an atlas overlap.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AtlasSeamKind {
    /// An ordinary overlap between two regular local charts.
    Regular,
    /// A pole overlap: both charts are required because one coordinate frame is
    /// singular at the other's pole.  The transition remains an isometry on the
    /// regular overlap; the tag prevents a consumer from flattening the cover.
    Pole,
}

/// Exact affine isometry between two unit-speed one-dimensional charts.
///
/// The convention is `t_to = sign * t_from + offset (mod period)`.  Unit speed
/// makes the only possible linear part `sign in {+1,-1}`; no fitted slope or
/// numerical tolerance is stored.  `-1` records an orientation-reversing
/// overlap.  Non-orientability is a property of the transition *cocycle*, not a
/// single negative edge; see [`ManifoldChartAtlas::orientability`].
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct UnitSpeedChartTransition {
    pub from_chart: usize,
    pub to_chart: usize,
    pub sign: i8,
    pub offset: f64,
    pub period: f64,
    pub seam_kind: AtlasSeamKind,
}

impl UnitSpeedChartTransition {
    #[must_use = "transition validation errors must be handled"]
    pub fn new(
        from_chart: usize,
        to_chart: usize,
        sign: i8,
        offset: f64,
        period: f64,
        seam_kind: AtlasSeamKind,
    ) -> Result<Self, String> {
        if from_chart == to_chart {
            return Err("unit-speed chart transition cannot be a self-edge".to_string());
        }
        if !matches!(sign, -1 | 1) {
            return Err(format!(
                "unit-speed chart transition sign must be +1 or -1, got {sign}"
            ));
        }
        if !(period.is_finite() && period > 0.0) {
            return Err(format!(
                "unit-speed chart transition period must be finite and positive, got {period}"
            ));
        }
        if !offset.is_finite() {
            return Err(format!(
                "unit-speed chart transition offset must be finite, got {offset}"
            ));
        }
        Ok(Self {
            from_chart,
            to_chart,
            sign,
            offset: offset.rem_euclid(period),
            period,
            seam_kind,
        })
    }

    /// Apply the exact unit-speed transition.
    #[must_use]
    pub fn apply(&self, coordinate: f64) -> f64 {
        (self.sign as f64 * coordinate + self.offset).rem_euclid(self.period)
    }

    /// Exact inverse isometry, with the chart endpoints swapped.
    #[must_use]
    pub fn inverse(&self) -> Self {
        let sign = self.sign;
        let offset = (-(sign as f64) * self.offset).rem_euclid(self.period);
        Self {
            from_chart: self.to_chart,
            to_chart: self.from_chart,
            sign,
            offset,
            period: self.period,
            seam_kind: self.seam_kind,
        }
    }

    /// Compose `self: A -> B` followed by `next: B -> C`.
    #[must_use = "transition composition errors must be handled"]
    pub fn compose(&self, next: &Self) -> Result<Self, String> {
        if self.to_chart != next.from_chart {
            return Err(format!(
                "cannot compose chart transitions {}->{} and {}->{}",
                self.from_chart, self.to_chart, next.from_chart, next.to_chart
            ));
        }
        if self.period.to_bits() != next.period.to_bits() {
            return Err(format!(
                "cannot compose chart transitions with periods {} and {}",
                self.period, next.period
            ));
        }
        let sign = self.sign * next.sign;
        let offset = (next.sign as f64 * self.offset + next.offset).rem_euclid(self.period);
        // Composition is a closed algebraic operation on isometries: composing a
        // transition with its inverse yields the IDENTITY self-map (`from == to`),
        // which `new` rejects as a registration self-edge.  Build the result
        // directly so `compose` stays total on validated inputs.
        Ok(Self {
            from_chart: self.from_chart,
            to_chart: next.to_chart,
            sign,
            offset,
            period: self.period,
            seam_kind: if matches!(self.seam_kind, AtlasSeamKind::Pole)
                || matches!(next.seam_kind, AtlasSeamKind::Pole)
            {
                AtlasSeamKind::Pole
            } else {
                AtlasSeamKind::Regular
            },
        })
    }

    pub(crate) fn remap(&mut self, old_to_new: &[Option<usize>]) -> Result<(), String> {
        self.from_chart = old_to_new
            .get(self.from_chart)
            .and_then(|x| *x)
            .ok_or_else(|| {
                "cannot remove an atlas chart while its seam is registered".to_string()
            })?;
        self.to_chart = old_to_new
            .get(self.to_chart)
            .and_then(|x| *x)
            .ok_or_else(|| {
                "cannot remove an atlas chart while its seam is registered".to_string()
            })?;
        Ok(())
    }
}

/// Provenance of an orthogonal sphere-chart transition.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SphereTransitionProvenance {
    /// The map is derived analytically from the chart definitions and may
    /// contribute an exact orientation cocycle sign.
    Analytic,
    /// The map is a polar factor fitted from decoder frames. Orthogonality of
    /// the stored matrix does not turn its estimated sign into an exact fact.
    Fitted,
}

/// Ambient isometry between two `latent_dim = 2` sphere charts (#1890 pole
/// seams), with analytic-versus-fitted provenance retained in the type.
///
/// Two lat/lon charts covering ONE ambient sphere with their poles in each
/// other's interior are related by an ambient rotation `R ∈ O(3)` acting on the
/// intrinsic unit vector `u = [x, y, z]`: `u_to = R · u_from`.  A sphere pole
/// seam is *intrinsically* two-dimensional — the transition is a full `3×3`
/// orthogonal matrix, not the `±1` sign a one-dimensional
/// [`UnitSpeedChartTransition`] carries — so a pole seam CANNOT be described by
/// a one-dimensional affine map, and is stored as this distinct kind rather than
/// a `Pole`-tagged 1-D transition (which would assert a map the overlap does not
/// have). For an analytically derived seam, orientability is read from `det R`:
/// `+1` preserves orientation and `-1` reverses it, exactly the role `sign`
/// plays for the 1-D transition. A fitted polar factor retains its determinant
/// only as geometry and never enters the exact sign cocycle.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SphereChartTransition {
    from_chart: usize,
    to_chart: usize,
    /// Ambient rotation `R ∈ O(3)`, row-major, mapping `from_chart`'s intrinsic
    /// unit vector to `to_chart`'s: `u_to = R · u_from`.
    rotation: [[f64; 3]; 3],
    seam_kind: AtlasSeamKind,
    provenance: SphereTransitionProvenance,
}

impl SphereChartTransition {
    #[must_use]
    pub fn from_chart(&self) -> usize {
        self.from_chart
    }

    #[must_use]
    pub fn to_chart(&self) -> usize {
        self.to_chart
    }

    #[must_use]
    pub fn rotation(&self) -> &[[f64; 3]; 3] {
        &self.rotation
    }

    #[must_use]
    pub fn seam_kind(&self) -> AtlasSeamKind {
        self.seam_kind
    }

    #[must_use]
    pub fn provenance(&self) -> SphereTransitionProvenance {
        self.provenance
    }

    #[must_use = "fitted transition validation errors must be handled"]
    pub fn new_fitted(
        from_chart: usize,
        to_chart: usize,
        rotation: [[f64; 3]; 3],
        seam_kind: AtlasSeamKind,
    ) -> Result<Self, String> {
        Self::validate(
            from_chart,
            to_chart,
            rotation,
            seam_kind,
            SphereTransitionProvenance::Fitted,
        )
    }

    fn validate(
        from_chart: usize,
        to_chart: usize,
        rotation: [[f64; 3]; 3],
        seam_kind: AtlasSeamKind,
        provenance: SphereTransitionProvenance,
    ) -> Result<Self, String> {
        if from_chart == to_chart {
            return Err("sphere chart transition cannot be a self-edge".to_string());
        }
        if rotation.iter().flatten().any(|x| !x.is_finite()) {
            return Err("sphere chart transition rotation must be finite".to_string());
        }
        // `RᵀR = I`: accept precisely the backward error of a three-term dot
        // product at this matrix's scale. There is no statistical or fitted
        // tolerance here: a non-orthogonal fitted map belongs in the noisy
        // holonomy certificate, not this exact-transition type.
        let frame_scale: f64 = rotation.iter().flatten().map(|value| value * value).sum();
        let backward_error = f64::EPSILON * 3.0 * frame_scale.max(1.0);
        for i in 0..3 {
            for j in 0..3 {
                let dot: f64 = (0..3).map(|k| rotation[k][i] * rotation[k][j]).sum();
                let target = if i == j { 1.0 } else { 0.0 };
                if (dot - target).abs() > backward_error {
                    return Err(format!(
                        "sphere chart transition rotation is not orthonormal: (RᵀR)[{i},{j}] = {dot}, machine backward-error bound={backward_error}"
                    ));
                }
            }
        }
        // A square orthogonal matrix is nonsingular and has determinant ±1;
        // re-testing that theorem with an independent numeric band would only
        // reintroduce a second, potentially contradictory threshold.
        Ok(Self {
            from_chart,
            to_chart,
            rotation,
            seam_kind,
            provenance,
        })
    }

    fn determinant_of(r: &[[f64; 3]; 3]) -> f64 {
        r[0][0] * (r[1][1] * r[2][2] - r[1][2] * r[2][1])
            - r[0][1] * (r[1][0] * r[2][2] - r[1][2] * r[2][0])
            + r[0][2] * (r[1][0] * r[2][1] - r[1][1] * r[2][0])
    }

    /// Signed determinant of the ambient rotation.
    #[must_use]
    pub fn determinant(&self) -> f64 {
        Self::determinant_of(&self.rotation)
    }

    /// Exact orientation contribution when and only when the seam is analytic.
    /// A fitted polar factor deliberately returns `None` even though its
    /// numerical determinant is ±1.
    #[must_use]
    pub fn analytic_sign(&self) -> Option<i8> {
        matches!(self.provenance, SphereTransitionProvenance::Analytic)
            .then(|| if self.determinant() >= 0.0 { 1 } else { -1 })
    }

    /// Apply the validated orthogonal ambient map to a unit vector.
    #[must_use]
    pub fn apply(&self, u: [f64; 3]) -> [f64; 3] {
        let mut out = [0.0; 3];
        for (i, row) in self.rotation.iter().enumerate() {
            out[i] = row[0] * u[0] + row[1] * u[1] + row[2] * u[2];
        }
        out
    }

    /// Inverse isometry (`R⁻¹ = Rᵀ` for an orthogonal `R`), endpoints
    /// swapped.
    #[must_use]
    pub fn inverse(&self) -> Self {
        let mut transpose = [[0.0; 3]; 3];
        for i in 0..3 {
            for j in 0..3 {
                transpose[i][j] = self.rotation[j][i];
            }
        }
        Self {
            from_chart: self.to_chart,
            to_chart: self.from_chart,
            rotation: transpose,
            seam_kind: self.seam_kind,
            provenance: self.provenance,
        }
    }

    /// Compose `self: A -> B` with `next: B -> C` (matrix product `R_next · R_self`).
    #[must_use = "transition composition errors must be handled"]
    pub fn compose(&self, next: &Self) -> Result<Self, String> {
        if self.to_chart != next.from_chart {
            return Err(format!(
                "cannot compose sphere chart transitions {}->{} and {}->{}",
                self.from_chart, self.to_chart, next.from_chart, next.to_chart
            ));
        }
        let mut product = [[0.0; 3]; 3];
        for i in 0..3 {
            for j in 0..3 {
                product[i][j] = (0..3)
                    .map(|k| next.rotation[i][k] * self.rotation[k][j])
                    .sum();
            }
        }
        // As with the 1-D transition, composing with the inverse yields the
        // identity self-map; build directly so `compose` stays total (the
        // self-edge check only guards registration).
        Ok(Self {
            from_chart: self.from_chart,
            to_chart: next.to_chart,
            rotation: product,
            seam_kind: if matches!(self.seam_kind, AtlasSeamKind::Pole)
                || matches!(next.seam_kind, AtlasSeamKind::Pole)
            {
                AtlasSeamKind::Pole
            } else {
                AtlasSeamKind::Regular
            },
            provenance: if matches!(self.provenance, SphereTransitionProvenance::Analytic)
                && matches!(next.provenance, SphereTransitionProvenance::Analytic)
            {
                SphereTransitionProvenance::Analytic
            } else {
                SphereTransitionProvenance::Fitted
            },
        })
    }

    pub(crate) fn remap(&mut self, old_to_new: &[Option<usize>]) -> Result<(), String> {
        self.from_chart = old_to_new
            .get(self.from_chart)
            .and_then(|x| *x)
            .ok_or_else(|| {
                "cannot remove an atlas chart while its sphere seam is registered".to_string()
            })?;
        self.to_chart = old_to_new
            .get(self.to_chart)
            .and_then(|x| *x)
            .ok_or_else(|| {
                "cannot remove an atlas chart while its sphere seam is registered".to_string()
            })?;
        Ok(())
    }
}

/// Orientability of the signed transition cocycle.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AtlasOrientability {
    /// All transition signs admit a consistent choice of local chart
    /// orientations.  Negative tree edges are allowed: only cycle holonomy is
    /// invariant under changing a chart's local orientation.
    Orientable,
    /// Some cycle has negative sign holonomy (the Möbius obstruction).
    NonOrientable,
}

/// Several local charts representing one manifold atom.
///
/// Overlaps come in two geometric kinds. Analytic instances share one exact
/// sign cocycle; fitted sphere polar maps remain unsigned until a statistical
/// certificate resolves them. The one-dimensional unit-speed affine
/// [`UnitSpeedChartTransition`] (circles, Möbius half-twists) and the
/// two-dimensional ambient-rotation
/// [`SphereChartTransition`] (sphere pole seams).  Both are stored so a single
/// atlas can mix them; connectivity and orientability read the union of their
/// signed edges.
#[derive(Clone, Debug, PartialEq)]
pub struct ManifoldChartAtlas {
    charts: Vec<usize>,
    transitions: Vec<UnitSpeedChartTransition>,
    sphere_transitions: Vec<SphereChartTransition>,
}

impl ManifoldChartAtlas {
    #[must_use = "atlas validation errors must be handled"]
    pub fn from_transition(transition: UnitSpeedChartTransition) -> Result<Self, String> {
        let charts = vec![
            transition.from_chart.min(transition.to_chart),
            transition.from_chart.max(transition.to_chart),
        ];
        let atlas = Self {
            charts,
            transitions: vec![transition],
            sphere_transitions: Vec::new(),
        };
        atlas.validate()?;
        Ok(atlas)
    }

    #[must_use = "atlas validation errors must be handled"]
    pub fn from_sphere_transition(transition: SphereChartTransition) -> Result<Self, String> {
        let charts = vec![
            transition.from_chart.min(transition.to_chart),
            transition.from_chart.max(transition.to_chart),
        ];
        let atlas = Self {
            charts,
            transitions: Vec::new(),
            sphere_transitions: vec![transition],
        };
        atlas.validate()?;
        Ok(atlas)
    }

    #[must_use]
    pub fn charts(&self) -> &[usize] {
        &self.charts
    }

    #[must_use]
    pub fn transitions(&self) -> &[UnitSpeedChartTransition] {
        &self.transitions
    }

    /// The registered two-dimensional sphere pole seams, in canonical order.
    #[must_use]
    pub fn sphere_transitions(&self) -> &[SphereChartTransition] {
        &self.sphere_transitions
    }

    /// Every transition with an analytic sign as `(from, to, sign)`. Fitted
    /// sphere edges are intentionally absent from this exact cocycle.
    fn signed_edges(&self) -> impl Iterator<Item = (usize, usize, i8)> + '_ {
        self.transitions
            .iter()
            .map(|t| (t.from_chart, t.to_chart, t.sign))
            .chain(self.sphere_transitions.iter().filter_map(|t| {
                t.analytic_sign()
                    .map(|sign| (t.from_chart, t.to_chart, sign))
            }))
    }

    fn transition_edges(&self) -> impl Iterator<Item = (usize, usize)> + '_ {
        self.transitions
            .iter()
            .map(|transition| (transition.from_chart, transition.to_chart))
            .chain(
                self.sphere_transitions
                    .iter()
                    .map(|transition| (transition.from_chart, transition.to_chart)),
            )
    }

    fn transition_count(&self) -> usize {
        self.transitions.len() + self.sphere_transitions.len()
    }

    #[must_use]
    pub fn contains_chart(&self, chart: usize) -> bool {
        self.charts.binary_search(&chart).is_ok()
    }

    pub(crate) fn add_transition(
        &mut self,
        transition: UnitSpeedChartTransition,
    ) -> Result<(), String> {
        self.charts.push(transition.from_chart);
        self.charts.push(transition.to_chart);
        self.charts.sort_unstable();
        self.charts.dedup();
        self.transitions.push(transition);
        self.canonicalize_transitions();
        self.validate()
    }

    pub(crate) fn add_sphere_transition(
        &mut self,
        transition: SphereChartTransition,
    ) -> Result<(), String> {
        self.charts.push(transition.from_chart);
        self.charts.push(transition.to_chart);
        self.charts.sort_unstable();
        self.charts.dedup();
        self.sphere_transitions.push(transition);
        self.canonicalize_transitions();
        self.validate()
    }

    pub(crate) fn replace_directed_transition(
        &mut self,
        transition: UnitSpeedChartTransition,
    ) -> Result<bool, String> {
        let Some(existing) = self.transitions.iter_mut().find(|existing| {
            existing.from_chart == transition.from_chart
                && existing.to_chart == transition.to_chart
                && existing.seam_kind == transition.seam_kind
        }) else {
            return Ok(false);
        };
        *existing = transition;
        self.canonicalize_transitions();
        self.validate()?;
        Ok(true)
    }

    pub(crate) fn replace_directed_sphere_transition(
        &mut self,
        transition: SphereChartTransition,
    ) -> Result<bool, String> {
        let Some(existing) = self.sphere_transitions.iter_mut().find(|existing| {
            existing.from_chart == transition.from_chart
                && existing.to_chart == transition.to_chart
                && existing.seam_kind == transition.seam_kind
        }) else {
            return Ok(false);
        };
        *existing = transition;
        self.canonicalize_transitions();
        self.validate()?;
        Ok(true)
    }

    pub(crate) fn merge_with_transition(
        &mut self,
        mut other: Self,
        transition: UnitSpeedChartTransition,
    ) -> Result<(), String> {
        self.charts.append(&mut other.charts);
        self.charts.sort_unstable();
        self.charts.dedup();
        self.transitions.append(&mut other.transitions);
        self.sphere_transitions
            .append(&mut other.sphere_transitions);
        // The new edge is what connects the formerly separate atlas
        // components. Validate only after it is present; validating the plain
        // union first would (correctly but prematurely) reject it as
        // disconnected and make atlas growth impossible.
        self.transitions.push(transition);
        self.canonicalize_transitions();
        self.validate()
    }

    pub(crate) fn merge_with_sphere_transition(
        &mut self,
        mut other: Self,
        transition: SphereChartTransition,
    ) -> Result<(), String> {
        self.charts.append(&mut other.charts);
        self.charts.sort_unstable();
        self.charts.dedup();
        self.transitions.append(&mut other.transitions);
        self.sphere_transitions
            .append(&mut other.sphere_transitions);
        self.sphere_transitions.push(transition);
        self.canonicalize_transitions();
        self.validate()
    }

    fn canonicalize_transitions(&mut self) {
        self.transitions.sort_by(|a, b| {
            a.from_chart
                .cmp(&b.from_chart)
                .then(a.to_chart.cmp(&b.to_chart))
                .then(a.sign.cmp(&b.sign))
                .then(a.offset.total_cmp(&b.offset))
                .then((a.seam_kind as u8).cmp(&(b.seam_kind as u8)))
        });
        self.sphere_transitions.sort_by(|a, b| {
            a.from_chart
                .cmp(&b.from_chart)
                .then(a.to_chart.cmp(&b.to_chart))
                .then((a.provenance as u8).cmp(&(b.provenance as u8)))
                .then(a.determinant().total_cmp(&b.determinant()))
                .then((a.seam_kind as u8).cmp(&(b.seam_kind as u8)))
                .then_with(|| {
                    for (x, y) in a.rotation.iter().flatten().zip(b.rotation.iter().flatten()) {
                        let ord = x.total_cmp(y);
                        if ord != std::cmp::Ordering::Equal {
                            return ord;
                        }
                    }
                    std::cmp::Ordering::Equal
                })
        });
    }

    fn validate(&self) -> Result<(), String> {
        if self.charts.len() < 2 {
            return Err("a chart atlas requires at least two charts".to_string());
        }
        if self.charts.windows(2).any(|w| w[0] >= w[1]) {
            return Err("atlas chart indices must be strictly increasing".to_string());
        }
        if self.transition_count() == 0 {
            return Err("a chart atlas requires at least one transition".to_string());
        }
        for (from, to) in self.transition_edges() {
            if !self.contains_chart(from) || !self.contains_chart(to) {
                return Err("atlas transition endpoint is not an atlas chart".to_string());
            }
        }
        // A semantic atom must be one connected cover, not a bag of unrelated
        // charts.  Connectivity is purely combinatorial and deterministic.
        let mut reached = BTreeSet::from([self.charts[0]]);
        let mut queue = VecDeque::from([self.charts[0]]);
        while let Some(chart) = queue.pop_front() {
            for (from, to) in self.transition_edges() {
                let next = if from == chart {
                    Some(to)
                } else if to == chart {
                    Some(from)
                } else {
                    None
                };
                if let Some(next) = next {
                    if reached.insert(next) {
                        queue.push_back(next);
                    }
                }
            }
        }
        if reached.len() != self.charts.len() {
            return Err("atlas transition graph is disconnected".to_string());
        }
        Ok(())
    }

    /// Read orientability from the sign cocycle.  Local orientations are
    /// propagated across the transition graph; a contradictory revisit is
    /// exactly a negative-holonomy cycle and therefore the Möbius obstruction.
    /// Fitted sphere signs remain unknown: after contracting every consistent
    /// analytic signed component, an unknown-edge forest is harmless (all its
    /// signs can be gauged away), while an unknown cycle is unresolved.
    #[must_use]
    pub fn orientability(&self) -> Option<AtlasOrientability> {
        let mut orientation = BTreeMap::new();
        let mut analytic_component = BTreeMap::new();
        let mut component_count = 0usize;
        for &root in &self.charts {
            if orientation.contains_key(&root) {
                continue;
            }
            let component = component_count;
            component_count += 1;
            orientation.insert(root, 1_i8);
            analytic_component.insert(root, component);
            let mut queue = VecDeque::from([root]);
            while let Some(chart) = queue.pop_front() {
                let here = orientation[&chart];
                for (from, to, edge_sign) in self.signed_edges() {
                    let (next, sign) = if from == chart {
                        (to, edge_sign)
                    } else if to == chart {
                        (from, edge_sign)
                    } else {
                        continue;
                    };
                    let required = here * sign;
                    match orientation.get(&next) {
                        Some(&existing) if existing != required => {
                            return Some(AtlasOrientability::NonOrientable);
                        }
                        Some(_) => {}
                        None => {
                            orientation.insert(next, required);
                            analytic_component.insert(next, component);
                            queue.push_back(next);
                        }
                    }
                }
            }
        }

        let mut parents: Vec<_> = (0..component_count).collect();
        for transition in self
            .sphere_transitions
            .iter()
            .filter(|transition| transition.analytic_sign().is_none())
        {
            let left = disjoint_set_root(&mut parents, analytic_component[&transition.from_chart]);
            let right = disjoint_set_root(&mut parents, analytic_component[&transition.to_chart]);
            if left == right {
                return None;
            }
            parents[right] = left;
        }
        Some(AtlasOrientability::Orientable)
    }

    pub(crate) fn remap(&mut self, old_to_new: &[Option<usize>]) -> Result<(), String> {
        let mut charts = Vec::with_capacity(self.charts.len());
        for &chart in &self.charts {
            charts.push(
                old_to_new
                    .get(chart)
                    .and_then(|x| *x)
                    .ok_or_else(|| format!("cannot remove registered atlas chart {chart}"))?,
            );
        }
        charts.sort_unstable();
        charts.dedup();
        self.charts = charts;
        for transition in &mut self.transitions {
            transition.remap(old_to_new)?;
        }
        for transition in &mut self.sphere_transitions {
            transition.remap(old_to_new)?;
        }
        self.canonicalize_transitions();
        self.validate()
    }

    pub(crate) fn shift_indices(&mut self, offset: usize) {
        for chart in &mut self.charts {
            *chart += offset;
        }
        for transition in &mut self.transitions {
            transition.from_chart += offset;
            transition.to_chart += offset;
        }
        for transition in &mut self.sphere_transitions {
            transition.from_chart += offset;
            transition.to_chart += offset;
        }
    }
}

fn disjoint_set_root(parents: &mut [usize], node: usize) -> usize {
    let mut root = node;
    while parents[root] != root {
        root = parents[root];
    }
    let mut cursor = node;
    while parents[cursor] != cursor {
        let next = parents[cursor];
        parents[cursor] = root;
        cursor = next;
    }
    root
}

impl SaeManifoldTerm {
    /// Registered multi-chart semantic atoms in canonical chart-index order.
    #[must_use]
    pub fn chart_atlases(&self) -> &[ManifoldChartAtlas] {
        &self.chart_atlases
    }

    /// Whether two numerical chart blocks have already been quotiented into the
    /// same semantic atlas atom.
    #[must_use]
    pub fn charts_share_atlas(&self, a: usize, b: usize) -> bool {
        self.chart_atlases
            .iter()
            .any(|atlas| atlas.contains_chart(a) && atlas.contains_chart(b))
    }

    /// Register an exact transition, creating or joining atlas components as
    /// needed.  Atlas membership is disjoint by construction: merging two
    /// existing components consumes both and emits one connected component.
    #[must_use = "atlas registration errors must be handled"]
    pub fn register_chart_transition(
        &mut self,
        transition: UnitSpeedChartTransition,
    ) -> Result<(), String> {
        let k = self.k_atoms();
        if transition.from_chart >= k || transition.to_chart >= k {
            return Err(format!(
                "chart transition {}->{} is outside dictionary width K={k}",
                transition.from_chart, transition.to_chart
            ));
        }
        let left = self
            .chart_atlases
            .iter()
            .position(|atlas| atlas.contains_chart(transition.from_chart));
        let right = self
            .chart_atlases
            .iter()
            .position(|atlas| atlas.contains_chart(transition.to_chart));
        match (left, right) {
            (None, None) => self
                .chart_atlases
                .push(ManifoldChartAtlas::from_transition(transition)?),
            (Some(index), None) | (None, Some(index)) => {
                self.chart_atlases[index].add_transition(transition)?;
            }
            (Some(left), Some(right)) if left == right => {
                self.chart_atlases[left].add_transition(transition)?;
            }
            (Some(left), Some(right)) => {
                let (keep, take) = if left < right {
                    (left, right)
                } else {
                    (right, left)
                };
                let other = self.chart_atlases.remove(take);
                self.chart_atlases[keep].merge_with_transition(other, transition)?;
            }
        }
        self.chart_atlases
            .sort_by_key(|atlas| atlas.charts().first().copied().unwrap_or(usize::MAX));
        Ok(())
    }

    /// Register an exact sphere pole seam (an ambient rotation between two
    /// `latent_dim = 2` charts), creating or joining atlas components exactly as
    /// [`Self::register_chart_transition`] does for the one-dimensional kind.
    #[must_use = "atlas registration errors must be handled"]
    pub fn register_sphere_chart_transition(
        &mut self,
        transition: SphereChartTransition,
    ) -> Result<(), String> {
        let k = self.k_atoms();
        if transition.from_chart >= k || transition.to_chart >= k {
            return Err(format!(
                "sphere chart transition {}->{} is outside dictionary width K={k}",
                transition.from_chart, transition.to_chart
            ));
        }
        let left = self
            .chart_atlases
            .iter()
            .position(|atlas| atlas.contains_chart(transition.from_chart));
        let right = self
            .chart_atlases
            .iter()
            .position(|atlas| atlas.contains_chart(transition.to_chart));
        match (left, right) {
            (None, None) => self
                .chart_atlases
                .push(ManifoldChartAtlas::from_sphere_transition(transition)?),
            (Some(index), None) | (None, Some(index)) => {
                self.chart_atlases[index].add_sphere_transition(transition)?;
            }
            (Some(left), Some(right)) if left == right => {
                self.chart_atlases[left].add_sphere_transition(transition)?;
            }
            (Some(left), Some(right)) => {
                let (keep, take) = if left < right {
                    (left, right)
                } else {
                    (right, left)
                };
                let other = self.chart_atlases.remove(take);
                self.chart_atlases[keep].merge_with_sphere_transition(other, transition)?;
            }
        }
        self.chart_atlases
            .sort_by_key(|atlas| atlas.charts().first().copied().unwrap_or(usize::MAX));
        Ok(())
    }

    /// Replace the fitted map on an already-registered directed seam.  Used
    /// after a polish refit so the persisted transition describes the terminal
    /// charts rather than their pre-refit warm start.
    pub(crate) fn refresh_chart_transition(
        &mut self,
        transition: UnitSpeedChartTransition,
    ) -> Result<(), String> {
        for atlas in &mut self.chart_atlases {
            if atlas.replace_directed_transition(transition)? {
                return Ok(());
            }
        }
        Err(format!(
            "cannot refresh unregistered chart transition {}->{}",
            transition.from_chart, transition.to_chart
        ))
    }

    /// Replace the fitted rotation on an already-registered sphere seam.
    pub(crate) fn refresh_sphere_chart_transition(
        &mut self,
        transition: SphereChartTransition,
    ) -> Result<(), String> {
        for atlas in &mut self.chart_atlases {
            if atlas.replace_directed_sphere_transition(transition)? {
                return Ok(());
            }
        }
        Err(format!(
            "cannot refresh unregistered sphere chart transition {}->{}",
            transition.from_chart, transition.to_chart
        ))
    }

    pub(crate) fn remap_chart_atlases(
        &mut self,
        old_to_new: &[Option<usize>],
    ) -> Result<(), String> {
        for atlas in &mut self.chart_atlases {
            atlas.remap(old_to_new)?;
        }
        self.chart_atlases
            .sort_by_key(|atlas| atlas.charts().first().copied().unwrap_or(usize::MAX));
        Ok(())
    }
}

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

    fn tr(from: usize, to: usize, sign: i8, offset: f64) -> UnitSpeedChartTransition {
        UnitSpeedChartTransition::new(from, to, sign, offset, 1.0, AtlasSeamKind::Regular).unwrap()
    }

    #[test]
    fn exact_transition_inverse_and_composition_are_unit_speed() {
        let ab = tr(0, 1, -1, 0.25);
        let ba = ab.inverse();
        let identity = ab.compose(&ba).unwrap();
        assert_eq!(identity.sign, 1);
        assert_eq!(identity.offset.to_bits(), 0.0_f64.to_bits());
        for coordinate in [0.0, 0.125, 0.75, 1.25] {
            assert!(
                (ba.apply(ab.apply(coordinate)) - coordinate.rem_euclid(1.0)).abs()
                    < 8.0 * f64::EPSILON
            );
        }
    }

    #[test]
    fn sign_holonomy_distinguishes_coordinate_reversal_from_mobius() {
        // One negative edge is removable by flipping one chart orientation.
        let mut interval_cover = ManifoldChartAtlas::from_transition(tr(0, 1, -1, 0.0)).unwrap();
        assert_eq!(
            interval_cover.orientability(),
            Some(AtlasOrientability::Orientable)
        );

        interval_cover.add_transition(tr(1, 2, 1, 0.0)).unwrap();
        interval_cover.add_transition(tr(2, 0, 1, 0.0)).unwrap();
        assert_eq!(
            interval_cover.orientability(),
            Some(AtlasOrientability::NonOrientable),
            "the cycle sign product is -1: the atlas has Möbius holonomy"
        );
    }

    #[test]
    fn bridge_transition_joins_two_connected_atlas_components() {
        let mut left = ManifoldChartAtlas::from_transition(tr(0, 1, 1, 0.1)).unwrap();
        let right = ManifoldChartAtlas::from_transition(tr(2, 3, -1, 0.2)).unwrap();
        left.merge_with_transition(right, tr(1, 2, 1, 0.3)).unwrap();
        assert_eq!(left.charts(), &[0, 1, 2, 3]);
        assert_eq!(left.transitions().len(), 3);
        assert_eq!(left.orientability(), Some(AtlasOrientability::Orientable));
    }

    #[test]
    fn fitted_sphere_cycle_is_unresolved_but_cannot_erase_a_known_negative_cycle() {
        let identity = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
        let fitted = |from, to| {
            SphereChartTransition::new_fitted(from, to, identity, AtlasSeamKind::Pole).unwrap()
        };
        let mut unknown_cycle = ManifoldChartAtlas::from_sphere_transition(fitted(0, 1)).unwrap();
        unknown_cycle.add_sphere_transition(fitted(1, 2)).unwrap();
        unknown_cycle.add_sphere_transition(fitted(2, 0)).unwrap();
        assert_eq!(unknown_cycle.orientability(), None);

        let mut known_negative = ManifoldChartAtlas::from_transition(tr(0, 1, -1, 0.0)).unwrap();
        known_negative.add_transition(tr(1, 2, 1, 0.0)).unwrap();
        known_negative.add_transition(tr(2, 0, 1, 0.0)).unwrap();
        known_negative.add_sphere_transition(fitted(2, 3)).unwrap();
        assert_eq!(
            known_negative.orientability(),
            Some(AtlasOrientability::NonOrientable)
        );
    }
}