Skip to main content

gam_terms/
latent.rs

1//! `LatentCoord` — per-row latent coordinates as a first-class gamfit parameter.
2//!
3//! The Riemannian update path follows manifold GPLVM practice (mGPLVM;
4//! Jensen/Kao/Tran/Stevenson 2020 and related head-direction / population
5//! manifold work): angular, spherical, and product-topology latents are
6//! updated on their natural manifold instead of as Euclidean coordinates
7//! with basis-side periodic hacks. Retractions and Euclidean-to-Riemannian
8//! Hessian conversion follow Absil/Mahony/Sepulchre (2008) and the Manopt /
9//! Pymanopt implementation pattern. In the audit-revised gauge framing, the
10//! Riemannian update is itself a gauge restriction: Circle/Sphere/Torus
11//! structure identifies the latent up to the corresponding global isometry
12//! (for example one rotation per cycle), not up to the full diffeomorphism
13//! group of an unconstrained Euclidean latent chart.
14//!
15//! ## Summary
16//!
17//! `LatentCoordValues` is the structural sibling of `SpatialLogKappaCoords`
18//! (see [`crate::smooth`]). Both store a flat `Array1<f64>` that the
19//! REML/IFT outer loop treats as *design-moving, non-penalty-like*
20//! hyper-coordinates. `SpatialLogKappaCoords` holds one or more kernel-shape
21//! coordinates per spatial term. `LatentCoordValues`
22//! holds an `N × d` matrix of per-row latent coordinates `t_n ∈ ℝ^d`.
23//!
24//! For a Duchon (or any radial) basis:
25//!
26//! ```text
27//! Φ_{n,k} = φ(‖t_n − c_k‖),
28//! ∂Φ_{n,k}/∂t_n = φ'(r_{nk}) · (t_n − c_k) / r_{nk}.
29//! ```
30//!
31//! The radial-gradient `φ'(r)` is the same scalar the kernel-shape machinery already
32//! computes via `crate::basis::duchon_radial_jets`; the chain rule
33//! `(t_n − c_k)/r_{nk}` is what differs between "differentiate against the
34//! kernel scale" and "differentiate against the first kernel argument t".
35//! Everything downstream of `HyperDesignDerivative::from_implicit` (matrix-free
36//! Newton, IFT cache, persistent warm-start, REML/LAML evaluation) is reused
37//! verbatim.
38//!
39//! ## Gauge fixing
40//!
41//! The bare data-fit `½‖y − Φ(t)β‖²` is invariant under any diffeomorphism
42//! `t ↦ φ(t)` (absorb into a re-fit β), so the inner Hessian in the latent
43//! block is singular and IFT breaks. [`LatentIdMode`] enumerates the
44//! gauge-fix penalties exposed at the configuration layer:
45//!
46//! * [`LatentIdMode::AuxPrior`] — iVAE-style auxiliary-conditional prior
47//!   `R_id(t,u) = ½ μ ‖t − ĥ(u)‖²` where `ĥ` is a small ridge / linear map
48//!   fit internally against the auxiliary `u`. `μ` is REML-selectable like a
49//!   smoothing parameter only when the marginal likelihood includes the
50//!   log-`μ` normalizer, `ĥ` is at least C¹, and the conditional precision is
51//!   positive-definite on the anchored subspace. Under those regularity
52//!   conditions this is the principled identifiability fix (Khemakhem et al.
53//!   2020).
54//! * [`LatentIdMode::DimSelection`] — ARD on each latent axis. One ridge
55//!   penalty per axis; REML drives unused axes' precision to infinity only
56//!   after `AuxPrior` or a future isometry prior fixes the gauge.
57//! * [`LatentIdMode::None`] — no gauge fix. Useful only as an explicit
58//!   opt-out; the caller is responsible for separately providing a unique
59//!   inner minimum (e.g. via a custom penalty).
60//!
61//! [`LatentIdMode::IsometryToReference`] (proposal §4(b)) anchors the latent to
62//! a caller-supplied reference configuration via `½ μ ‖t − reference‖²` with a
63//! REML-selectable `μ`, fixing the gauge without an auxiliary signal `u`.
64
65use crate::basis::{BasisError, RadialScalarKind};
66use gam_problem::LatentRetractionRegistry;
67use ndarray::{Array1, Array2, Array3, ArrayView1, ArrayView2, ArrayView3};
68use std::sync::atomic::{AtomicU64, Ordering};
69const SPHERE_NORMAL_PIN: f64 = 1.0;
70static NEXT_LATENT_COORD_ID: AtomicU64 = AtomicU64::new(1);
71
72fn next_latent_coord_id() -> u64 {
73    NEXT_LATENT_COORD_ID.fetch_add(1, Ordering::Relaxed)
74}
75
76/// Choice of auxiliary-prior conditional mean estimator `ĥ(u)`.
77///
78/// `Ridge` is the cheap default that closes form (one `K_u × K_u` solve);
79/// `Linear` is equivalent to `Ridge` with zero ridge and is intended for
80/// auxiliaries `u` that are already low-dimensional and well-conditioned.
81#[derive(Debug, Clone, Copy)]
82pub enum AuxPriorFamily {
83    /// Ridge regression `t ≈ U · A` with a small diagonal regularizer.
84    /// The default ridge strength is `1e-6 · trace(UᵀU)/p`, which is
85    /// numerically benign and never under-constrains the fit when
86    /// `n_obs > p`.
87    Ridge,
88    /// Plain linear projection (no ridge). Errors out at construction if
89    /// `UᵀU` is singular.
90    Linear,
91}
92
93/// Strength of the auxiliary-prior identifiability penalty.
94///
95/// `Auto` defers the choice to REML — the strength is added to the outer
96/// vector as one extra `ρ`-axis (one log-precision per `LatentCoord`). When
97/// the caller supplies an explicit `Fixed(μ)` the strength is held constant
98/// throughout the fit; useful for warm-starts and reproducibility. The REML
99/// path is valid only with the prior normalizer included, a C¹ conditional
100/// mean map, and positive-definite precision on the anchored subspace.
101#[derive(Debug, Clone, Copy)]
102pub enum AuxPriorStrength {
103    Auto,
104    Fixed(f64),
105}
106
107/// Identifiability / gauge-fix mode for a [`LatentCoordValues`] block.
108///
109/// `AuxPrior` is currently the only standalone gauge-fixing mode; see the
110/// module docstring. `DimSelection` must be paired with `AuxPrior` (or a
111/// future isometry mode) by higher-level assembly before fitting.
112#[derive(Debug, Clone)]
113pub enum LatentIdMode {
114    /// Conditional Gaussian prior `p(t | u)` with mean `ĥ(u)` fit by
115    /// `family`. The penalty contribution is
116    /// `R_id = ½ μ · ‖t − ĥ(u)‖²`. `u` has shape `(n_obs, p)`. If
117    /// `strength == Auto`, REML selection of `μ` requires the log-`μ`
118    /// normalizer, C¹ regularity of `ĥ`, and positive-definiteness on the
119    /// subspace anchored by `u`.
120    AuxPrior {
121        u: Array2<f64>,
122        family: AuxPriorFamily,
123        strength: AuxPriorStrength,
124    },
125    /// Auxiliary prior plus ARD over latent axes. `AuxPrior` supplies the
126    /// identifiability anchor; `init_log_precision` seeds the per-axis ARD
127    /// coordinates.
128    AuxPriorDimSelection {
129        u: Array2<f64>,
130        family: AuxPriorFamily,
131        strength: AuxPriorStrength,
132        init_log_precision: Option<Array1<f64>>,
133    },
134    /// ARD over latent axes. One ridge penalty per latent axis; the per-axis
135    /// log-precision joins the outer ρ vector. `init_log_precision` seeds
136    /// the per-axis ρ — a vector of length `d`. `None` defaults to a flat
137    /// zero seed (precision = 1 on every axis).
138    DimSelection {
139        init_log_precision: Option<Array1<f64>>,
140    },
141    /// Behaviorally-anchored head (issue #912). The auxiliary signal is
142    /// promoted from a fixed-covariate *prior* to a modeled *outcome*: a GLM
143    /// behavioral head `g(E[y|t]) = a + t·w` whose design columns are the
144    /// latent codes contributes a *likelihood* term to the joint objective,
145    /// so REML balances reconstruction vs. behavioral fit with no trade-off
146    /// scalar (magic by default).
147    ///
148    /// The head's coefficients are direct hyperparameters appended to θ (one
149    /// `(1 + d)` block per η-channel), like the AuxPrior log-`μ`. Because a
150    /// single binary label pins ~1 gauge dimension, `AuxOutcome` *composes*
151    /// with `DimSelection` ARD (the `init_log_precision` seed) and the
152    /// isometry pin rather than replacing them; the validator requires that
153    /// composition and rejects a head with no labels.
154    AuxOutcome {
155        head: crate::decoders::behavioral_head::BehavioralHead,
156        /// ARD seed composed with the head, one log-precision per latent axis
157        /// (length `d`). `AuxOutcome` always carries the ARD axis-selection
158        /// alongside the behavioral anchor, since the label alone under-pins
159        /// the gauge. `None` defaults to a flat zero seed.
160        init_log_precision: Option<Array1<f64>>,
161    },
162    /// Anchor the latent configuration to a caller-supplied reference up to
163    /// the global isometry the chosen manifold already quotients out: penalty
164    /// `R_id = ½ μ · ‖t − reference‖²` with REML-selectable `μ` (the log-`μ`
165    /// normalizer enters the marginal likelihood exactly as in `AuxPrior`).
166    /// Unlike `AuxPrior`, the target is a fixed reference configuration (e.g. a
167    /// pilot embedding that fixes the isometry representative) rather than an
168    /// auxiliary-conditional mean `ĥ(u)`, so it pins the gauge with no
169    /// auxiliary signal `u`. `reference` has shape `(n_obs, d)`. As a standalone
170    /// anchor it is a valid gauge fix; it also composes with `DimSelection` ARD.
171    IsometryToReference {
172        reference: Array2<f64>,
173        strength: AuxPriorStrength,
174    },
175    /// No gauge fix. Inner Hessian is rank-deficient; results are not
176    /// uniquely defined. Intended only for the explicit "I supply my own
177    /// gauge constraint via the smoothing penalty" pathway.
178    None,
179}
180
181/// Natural manifold for per-row latent-coordinate updates.
182///
183/// `Euclidean` preserves the original additive update. `Circle` is a scalar
184/// angular coordinate wrapped modulo `2π`. `Sphere { dim }` is the embedded
185/// unit sphere in `R^dim`, with retraction `(t + ξ) / ||t + ξ||`. `Product`
186/// composes these blockwise; inside a product, `Euclidean` denotes one
187/// unconstrained scalar axis.
188#[derive(Debug, Clone, PartialEq, Default)]
189pub enum LatentManifold {
190    /// Unconstrained `R^d` — the current default.
191    #[default]
192    Euclidean,
193    /// Scalar periodic coordinate on `S^1` with caller-supplied period.
194    ///
195    /// Wraps modulo `period`; pass `period = 2π` for radian conventions and
196    /// `period = 1.0` for basis evaluators that interpret the latent as a
197    /// fraction of one period. The metric weight uses `1/period²` so the
198    /// trust-region radius respects the chosen unit.
199    Circle { period: f64 },
200    /// Embedded unit sphere `S^(dim-1)`.
201    Sphere { dim: usize },
202    /// Closed interval in `R`; the retraction clamps to the boundary.
203    Interval { lo: f64, hi: f64 },
204    /// Product manifold, split block-by-block in row-major ambient storage.
205    Product(Vec<LatentManifold>),
206    /// Product manifold with explicit per-axis trust-region metric weights.
207    ///
208    /// Without per-axis weighting, a Product of Circle + Interval treats
209    /// 1 radian as commensurate with the entire bounded range. With weights
210    /// = 1/scale², the trust-region radius respects each axis's natural unit.
211    ProductWithMetric {
212        manifolds: Vec<LatentManifold>,
213        weights: Vec<f64>,
214    },
215}
216
217impl LatentManifold {
218    pub fn is_euclidean(&self) -> bool {
219        matches!(self, Self::Euclidean)
220    }
221
222    /// Whether the Euclidean→Riemannian geometry transform applied by
223    /// `crate::solver::arrow_schur::ArrowSchurSystem::apply_riemannian_latent_geometry`
224    /// is the **identity** on the per-row gradient, `H_tt`, and `H_tβ` blocks
225    /// for *every* coordinate `t` on this chart.
226    ///
227    /// This is the exact condition under which a coupled Gauss-Newton block
228    /// `μ AᵀA = [[htt, cross],[crossᵀ, hbb]]` assembled from one residual
229    /// Jacobian survives the geometry pass with its PSD coherence intact: if
230    /// the transform leaves `htt` and the `htbeta` cross-block untouched, the
231    /// whole block is still `μ AᵀA` (PSD) and its Schur complement is PSD, so
232    /// the isometry cross-coupling can be kept (faster, exact Newton).
233    ///
234    /// A chart that rewrites `htt` with a curvature/connection term or
235    /// column-projects the cross-block (`Sphere`, an active `Interval`
236    /// boundary, any curved `Product` factor) breaks that pairing — the
237    /// cross-block is then no longer matched to diagonals from the same
238    /// Jacobian and the Schur complement can go indefinite (the #681
239    /// circle/sphere failure mode). Such charts must drop the cross-block.
240    ///
241    /// Flat charts (`Euclidean`, `Circle`, and `Product`s built only from
242    /// these) transform as the identity unconditionally — their tangent
243    /// projection is the identity, they carry no connection term, and they add
244    /// no normal pinning — so coherence is preserved and the cross-block is
245    /// kept. `Interval` is excluded: its tangent projection masks coordinates
246    /// at an active boundary (a `t`-dependent projection), which breaks the
247    /// pairing exactly like a curved chart.
248    pub fn preserves_isometry_cross_block_coherence(&self) -> bool {
249        match self {
250            Self::Euclidean | Self::Circle { .. } => true,
251            Self::Sphere { .. } | Self::Interval { .. } => false,
252            Self::Product(parts)
253            | Self::ProductWithMetric {
254                manifolds: parts, ..
255            } => parts
256                .iter()
257                .all(|part| part.preserves_isometry_cross_block_coherence()),
258        }
259    }
260
261    pub fn ambient_dim(&self, fallback_dim: usize) -> usize {
262        match self {
263            Self::Euclidean => fallback_dim,
264            Self::Circle { .. } | Self::Interval { .. } => 1,
265            Self::Sphere { dim } => *dim,
266            Self::Product(parts)
267            | Self::ProductWithMetric {
268                manifolds: parts, ..
269            } => parts.iter().map(|part| part.ambient_dim(1)).sum(),
270        }
271    }
272
273    /// Per-axis weights for the Riemannian trust-region metric.
274    ///
275    /// Defaults use `1/scale²`: Circle scale is `2π`, Sphere scale is `π`,
276    /// Interval scale is `hi - lo`, and Euclidean scale is `1`. Product
277    /// manifolds recurse and concatenate; [`Self::ProductWithMetric`] uses
278    /// the caller-supplied weights directly.
279    pub fn metric_weights(&self) -> Vec<f64> {
280        match self {
281            Self::Euclidean => vec![1.0],
282            Self::Circle { period } => {
283                assert!(
284                    period.is_finite() && *period > 0.0,
285                    "LatentManifold::Circle requires a finite positive period; got {period}"
286                );
287                vec![1.0 / (period * period)]
288            }
289            Self::Sphere { dim } => {
290                let w = 1.0 / (std::f64::consts::PI * std::f64::consts::PI);
291                vec![w; *dim]
292            }
293            Self::Interval { lo, hi } => {
294                let scale = hi - lo;
295                assert!(
296                    scale.is_finite() && scale > 0.0,
297                    "LatentManifold::Interval requires finite lo < hi; got lo={lo}, hi={hi}"
298                );
299                vec![1.0 / (scale * scale)]
300            }
301            Self::Product(parts) => {
302                let mut out = Vec::with_capacity(self.ambient_dim(1));
303                for part in parts {
304                    out.extend(part.metric_weights());
305                }
306                out
307            }
308            Self::ProductWithMetric { manifolds, weights } => {
309                let expected: usize = manifolds.iter().map(|part| part.ambient_dim(1)).sum();
310                assert_eq!(
311                    weights.len(),
312                    expected,
313                    "LatentManifold::ProductWithMetric weights length must match ambient dimension"
314                );
315                weights.clone()
316            }
317        }
318    }
319
320    /// Per-ambient-axis periodicity: `Some(period)` for an axis that wraps
321    /// modulo a finite period (a `Circle` factor, including the longitude of
322    /// the lat/lon sphere chart), `None` for a non-periodic axis (Euclidean,
323    /// Interval, or an embedded `Sphere` axis whose retraction is smooth and
324    /// has no cut).
325    ///
326    /// Used by the SAE-manifold ARD prior to switch from the cut-discontinuous
327    /// Euclidean `½α t²` to a smooth von-Mises energy on periodic axes. The
328    /// embedded `Sphere` is deliberately reported as non-periodic: its
329    /// retraction `(t+ξ)/‖t+ξ‖` is globally smooth, so the ambient `½α‖t‖²`
330    /// prior has no discontinuity there.
331    pub fn axis_periods(&self) -> Vec<Option<f64>> {
332        match self {
333            Self::Euclidean => vec![None],
334            Self::Circle { period } => {
335                assert!(
336                    period.is_finite() && *period > 0.0,
337                    "LatentManifold::Circle requires a finite positive period; got {period}"
338                );
339                vec![Some(*period)]
340            }
341            Self::Sphere { dim } => vec![None; *dim],
342            Self::Interval { .. } => vec![None],
343            Self::Product(parts) => {
344                let mut out = Vec::with_capacity(self.ambient_dim(1));
345                for part in parts {
346                    out.extend(part.axis_periods());
347                }
348                out
349            }
350            Self::ProductWithMetric { manifolds, .. } => {
351                let mut out = Vec::with_capacity(self.ambient_dim(1));
352                for part in manifolds {
353                    out.extend(part.axis_periods());
354                }
355                out
356            }
357        }
358    }
359
360    /// Project an arbitrary ambient point back to the manifold.
361    pub fn project_point(&self, t: ArrayView1<'_, f64>) -> Array1<f64> {
362        match self {
363            Self::Euclidean => t.to_owned(),
364            Self::Circle { period } => {
365                let mut out = Array1::<f64>::zeros(1);
366                out[0] = wrap_to_period(t[0], *period);
367                out
368            }
369            Self::Sphere { dim } => {
370                assert_eq!(t.len(), *dim);
371                normalize_or_axis(t, *dim)
372            }
373            Self::Interval { lo, hi } => {
374                // Order the bounds defensively: `f64::clamp` panics if min > max,
375                // so a reversed `Interval { lo, hi }` would otherwise crash deep
376                // in projection rather than clamp into the intended range.
377                let (lo, hi) = if lo <= hi { (*lo, *hi) } else { (*hi, *lo) };
378                let mut out = Array1::<f64>::zeros(1);
379                out[0] = t[0].clamp(lo, hi);
380                out
381            }
382            Self::Product(parts)
383            | Self::ProductWithMetric {
384                manifolds: parts, ..
385            } => {
386                let mut out = Array1::<f64>::zeros(t.len());
387                let mut offset = 0_usize;
388                for part in parts {
389                    let dim = part.ambient_dim(1);
390                    let projected = part.project_point(t.slice(ndarray::s![offset..offset + dim]));
391                    for a in 0..dim {
392                        out[offset + a] = projected[a];
393                    }
394                    offset += dim;
395                }
396                assert_eq!(offset, t.len());
397                out
398            }
399        }
400    }
401
402    /// Retraction `R_t(ξ)`, using closed-form analytic maps for every variant.
403    pub fn retract(&self, t: ArrayView1<'_, f64>, xi: ArrayView1<'_, f64>) -> Array1<f64> {
404        assert_eq!(t.len(), xi.len());
405        match self {
406            Self::Euclidean => {
407                let mut out = t.to_owned();
408                for a in 0..out.len() {
409                    out[a] += xi[a];
410                }
411                out
412            }
413            Self::Circle { period } => {
414                let mut out = Array1::<f64>::zeros(1);
415                out[0] = wrap_to_period(t[0] + xi[0], *period);
416                out
417            }
418            Self::Sphere { dim } => {
419                assert_eq!(t.len(), *dim);
420                let mut y = Array1::<f64>::zeros(*dim);
421                for a in 0..*dim {
422                    y[a] = t[a] + xi[a];
423                }
424                normalize_or_axis(y.view(), *dim)
425            }
426            Self::Interval { lo, hi } => {
427                // Order the bounds defensively: `f64::clamp` panics if min > max,
428                // so a reversed `Interval { lo, hi }` would otherwise crash the
429                // retraction instead of clamping into the intended range.
430                let (lo, hi) = if lo <= hi { (*lo, *hi) } else { (*hi, *lo) };
431                let mut out = Array1::<f64>::zeros(1);
432                out[0] = (t[0] + xi[0]).clamp(lo, hi);
433                out
434            }
435            Self::Product(parts)
436            | Self::ProductWithMetric {
437                manifolds: parts, ..
438            } => {
439                let mut out = Array1::<f64>::zeros(t.len());
440                let mut offset = 0_usize;
441                for part in parts {
442                    let dim = part.ambient_dim(1);
443                    let next = part.retract(
444                        t.slice(ndarray::s![offset..offset + dim]),
445                        xi.slice(ndarray::s![offset..offset + dim]),
446                    );
447                    for a in 0..dim {
448                        out[offset + a] = next[a];
449                    }
450                    offset += dim;
451                }
452                assert_eq!(offset, t.len());
453                out
454            }
455        }
456    }
457
458    /// Orthogonal projection of an ambient vector onto `T_t M`.
459    pub fn project_to_tangent(
460        &self,
461        t: ArrayView1<'_, f64>,
462        v: ArrayView1<'_, f64>,
463    ) -> Array1<f64> {
464        assert_eq!(t.len(), v.len());
465        match self {
466            Self::Euclidean | Self::Circle { .. } => v.to_owned(),
467            Self::Sphere { dim } => {
468                assert_eq!(t.len(), *dim);
469                let tv = t.dot(&v);
470                let mut out = v.to_owned();
471                for a in 0..*dim {
472                    out[a] -= tv * t[a];
473                }
474                out
475            }
476            Self::Interval { lo, hi } => {
477                let mut out = Array1::<f64>::zeros(1);
478                let at_lo = t[0] <= *lo && v[0] < 0.0;
479                let at_hi = t[0] >= *hi && v[0] > 0.0;
480                out[0] = if at_lo || at_hi { 0.0 } else { v[0] };
481                out
482            }
483            Self::Product(parts)
484            | Self::ProductWithMetric {
485                manifolds: parts, ..
486            } => {
487                let mut out = Array1::<f64>::zeros(v.len());
488                let mut offset = 0_usize;
489                for part in parts {
490                    let dim = part.ambient_dim(1);
491                    let projected = part.project_to_tangent(
492                        t.slice(ndarray::s![offset..offset + dim]),
493                        v.slice(ndarray::s![offset..offset + dim]),
494                    );
495                    for a in 0..dim {
496                        out[offset + a] = projected[a];
497                    }
498                    offset += dim;
499                }
500                assert_eq!(offset, v.len());
501                out
502            }
503        }
504    }
505
506    /// Project an objective gradient onto the linearized feasible update space.
507    ///
508    /// For smooth manifolds this is the usual tangent projection. For interval
509    /// endpoints the sign test is applied to the descent direction `-g`: at the
510    /// upper endpoint, a negative gradient would step outward, so the coordinate
511    /// is held fixed; at the lower endpoint, a positive gradient would step
512    /// outward. This is distinct from [`Self::project_to_tangent`], whose
513    /// interval branch projects update velocities.
514    pub fn project_gradient_to_tangent(
515        &self,
516        t: ArrayView1<'_, f64>,
517        g: ArrayView1<'_, f64>,
518    ) -> Array1<f64> {
519        assert_eq!(t.len(), g.len());
520        match self {
521            Self::Euclidean | Self::Circle { .. } | Self::Sphere { .. } => {
522                self.project_to_tangent(t, g)
523            }
524            Self::Interval { lo, hi } => {
525                let mut out = Array1::<f64>::zeros(1);
526                let descent_exits_lo = t[0] <= *lo && g[0] > 0.0;
527                let descent_exits_hi = t[0] >= *hi && g[0] < 0.0;
528                out[0] = if descent_exits_lo || descent_exits_hi {
529                    0.0
530                } else {
531                    g[0]
532                };
533                out
534            }
535            Self::Product(parts)
536            | Self::ProductWithMetric {
537                manifolds: parts, ..
538            } => {
539                let mut out = Array1::<f64>::zeros(g.len());
540                let mut offset = 0_usize;
541                for part in parts {
542                    let dim = part.ambient_dim(1);
543                    let projected = part.project_gradient_to_tangent(
544                        t.slice(ndarray::s![offset..offset + dim]),
545                        g.slice(ndarray::s![offset..offset + dim]),
546                    );
547                    for a in 0..dim {
548                        out[offset + a] = projected[a];
549                    }
550                    offset += dim;
551                }
552                // The per-part ambient widths (`part.ambient_dim(1)`) must tile
553                // `g` exactly. This holds because every `Product` is built in
554                // expanded scalar-factor form — a multi-dimensional Euclidean
555                // atom is stored as `d` single-axis `Euclidean` children, never
556                // one `d`-wide `Euclidean` (which `ambient_dim(1)` would
557                // under-count as one axis, mis-tiling a mixed-dimension composite
558                // and firing here — the #2295 zoo-fit panic). See
559                // `SaeManifoldTerm::append_coordinate_manifold_parts`.
560                assert_eq!(
561                    offset,
562                    g.len(),
563                    "Product factor ambient widths ({offset}) must tile the gradient ({}); a \
564                     Product must be in expanded scalar-factor form (see #2295)",
565                    g.len()
566                );
567                out
568            }
569        }
570    }
571
572    /// Project a coordinate-space Jacobian/cross-block column with the same
573    /// active interval coordinates selected by
574    /// [`Self::project_gradient_to_tangent`].
575    pub fn project_vector_to_gradient_tangent(
576        &self,
577        t: ArrayView1<'_, f64>,
578        g: ArrayView1<'_, f64>,
579        v: ArrayView1<'_, f64>,
580    ) -> Array1<f64> {
581        assert_eq!(t.len(), g.len());
582        assert_eq!(t.len(), v.len());
583        match self {
584            Self::Euclidean | Self::Circle { .. } | Self::Sphere { .. } => {
585                self.project_to_tangent(t, v)
586            }
587            Self::Interval { lo, hi } => {
588                let mut out = Array1::<f64>::zeros(1);
589                let descent_exits_lo = t[0] <= *lo && g[0] > 0.0;
590                let descent_exits_hi = t[0] >= *hi && g[0] < 0.0;
591                out[0] = if descent_exits_lo || descent_exits_hi {
592                    0.0
593                } else {
594                    v[0]
595                };
596                out
597            }
598            Self::Product(parts)
599            | Self::ProductWithMetric {
600                manifolds: parts, ..
601            } => {
602                let mut out = Array1::<f64>::zeros(v.len());
603                let mut offset = 0_usize;
604                for part in parts {
605                    let dim = part.ambient_dim(1);
606                    let projected = part.project_vector_to_gradient_tangent(
607                        t.slice(ndarray::s![offset..offset + dim]),
608                        g.slice(ndarray::s![offset..offset + dim]),
609                        v.slice(ndarray::s![offset..offset + dim]),
610                    );
611                    for a in 0..dim {
612                        out[offset + a] = projected[a];
613                    }
614                    offset += dim;
615                }
616                assert_eq!(offset, v.len());
617                out
618            }
619        }
620    }
621
622    /// Project every column of `matrix` with
623    /// [`Self::project_vector_to_gradient_tangent`].
624    pub fn project_matrix_columns_to_gradient_tangent(
625        &self,
626        t: ArrayView1<'_, f64>,
627        g: ArrayView1<'_, f64>,
628        matrix: ArrayView2<'_, f64>,
629    ) -> Array2<f64> {
630        let mut out = Array2::<f64>::zeros(matrix.dim());
631        assert_eq!(matrix.nrows(), t.len());
632        for col_idx in 0..matrix.ncols() {
633            let col = self.project_vector_to_gradient_tangent(t, g, matrix.column(col_idx));
634            for row_idx in 0..matrix.nrows() {
635                out[[row_idx, col_idx]] = col[row_idx];
636            }
637        }
638        out
639    }
640
641    /// Convert Euclidean Hessian action `eh · xi` to Riemannian Hessian action.
642    ///
643    /// For the sphere this is the Absil/Mahony/Sepulchre embedded-sphere
644    /// conversion: differentiate the projected gradient and project back to
645    /// the tangent space. The ambient derivative includes the normal
646    /// curvature term `-<grad_R, ξ> t`; the tangent action is equivalent to
647    /// `P_t(eh ξ) - <eg, t> ξ`.
648    pub fn euclidean_to_riemannian_hessian(
649        &self,
650        t: ArrayView1<'_, f64>,
651        eg: ArrayView1<'_, f64>,
652        eh: ArrayView2<'_, f64>,
653        xi: ArrayView1<'_, f64>,
654    ) -> Array1<f64> {
655        assert_eq!(t.len(), eg.len());
656        assert_eq!(t.len(), xi.len());
657        assert_eq!(eh.nrows(), t.len());
658        assert_eq!(eh.ncols(), t.len());
659        let eh_xi = eh.dot(&xi);
660        self.euclidean_hessian_action_to_riemannian(t, eg, xi, eh_xi.view())
661    }
662
663    fn euclidean_hessian_action_to_riemannian(
664        &self,
665        t: ArrayView1<'_, f64>,
666        eg: ArrayView1<'_, f64>,
667        xi: ArrayView1<'_, f64>,
668        eh_xi: ArrayView1<'_, f64>,
669    ) -> Array1<f64> {
670        assert_eq!(t.len(), eg.len());
671        assert_eq!(t.len(), xi.len());
672        assert_eq!(t.len(), eh_xi.len());
673        match self {
674            Self::Euclidean | Self::Circle { .. } => self.project_to_tangent(t, eh_xi),
675            Self::Interval { .. } => self.project_vector_to_gradient_tangent(t, eg, eh_xi),
676            Self::Sphere { dim } => {
677                assert_eq!(t.len(), *dim);
678                let grad_r = self.project_to_tangent(t, eg);
679                let mut ambient = self.project_to_tangent(t, eh_xi);
680                let eg_normal = eg.dot(&t);
681                let normal_curve = grad_r.dot(&xi);
682                for a in 0..*dim {
683                    ambient[a] -= eg_normal * xi[a];
684                    ambient[a] -= normal_curve * t[a];
685                }
686                self.project_to_tangent(t, ambient.view())
687            }
688            Self::Product(parts)
689            | Self::ProductWithMetric {
690                manifolds: parts, ..
691            } => {
692                let mut out = Array1::<f64>::zeros(t.len());
693                let mut offset = 0_usize;
694                for part in parts {
695                    let dim = part.ambient_dim(1);
696                    let converted = part.euclidean_hessian_action_to_riemannian(
697                        t.slice(ndarray::s![offset..offset + dim]),
698                        eg.slice(ndarray::s![offset..offset + dim]),
699                        xi.slice(ndarray::s![offset..offset + dim]),
700                        eh_xi.slice(ndarray::s![offset..offset + dim]),
701                    );
702                    for a in 0..dim {
703                        out[offset + a] = converted[a];
704                    }
705                    offset += dim;
706                }
707                assert_eq!(offset, t.len());
708                out
709            }
710        }
711    }
712
713    /// Dense ambient matrix representation of the tangent Hessian action.
714    ///
715    /// Normal directions are pinned with an identity block for embedded
716    /// constrained factors so existing BA Cholesky code can factor the ambient
717    /// matrix while RHS/cross blocks stay tangent-projected.
718    pub fn riemannian_hessian_matrix(
719        &self,
720        t: ArrayView1<'_, f64>,
721        eg: ArrayView1<'_, f64>,
722        eh: ArrayView2<'_, f64>,
723    ) -> Array2<f64> {
724        let d = t.len();
725        let mut out = Array2::<f64>::zeros((d, d));
726        let mut xi = Array1::<f64>::zeros(d);
727        for a in 0..d {
728            xi.fill(0.0);
729            xi[a] = 1.0;
730            let tangent_xi = self.project_vector_to_gradient_tangent(t, eg, xi.view());
731            let col = self.euclidean_to_riemannian_hessian(t, eg, eh, tangent_xi.view());
732            for b in 0..d {
733                out[[b, a]] = col[b];
734            }
735        }
736        self.add_normal_pinning(t, &mut out);
737        symmetrize(&mut out);
738        out
739    }
740
741    fn add_normal_pinning(&self, t: ArrayView1<'_, f64>, matrix: &mut Array2<f64>) {
742        match self {
743            Self::Sphere { dim } => {
744                assert_eq!(t.len(), *dim);
745                for a in 0..*dim {
746                    for b in 0..*dim {
747                        matrix[[a, b]] += SPHERE_NORMAL_PIN * t[a] * t[b];
748                    }
749                }
750            }
751            Self::Product(parts)
752            | Self::ProductWithMetric {
753                manifolds: parts, ..
754            } => {
755                let mut offset = 0_usize;
756                for part in parts {
757                    let dim = part.ambient_dim(1);
758                    let mut block =
759                        matrix.slice_mut(ndarray::s![offset..offset + dim, offset..offset + dim]);
760                    let mut owned = block.to_owned();
761                    part.add_normal_pinning(t.slice(ndarray::s![offset..offset + dim]), &mut owned);
762                    block.assign(&owned);
763                    offset += dim;
764                }
765            }
766            Self::Euclidean | Self::Circle { .. } | Self::Interval { .. } => {}
767        }
768    }
769}
770
771impl LatentIdMode {
772
773    /// Validate the mode's identifiability composition (issue #912 step 2).
774    ///
775    /// `AuxOutcome` must carry a non-vacuous head (at least one labeled row)
776    /// and composes with ARD — a bare label channel with no axis-selection
777    /// under-pins the gauge. Returns the offending reason on failure so the
778    /// builder can reject before fitting. (The former `reject_dim_selection_alone`
779    /// guard was unified here into the Result path for a panic-free gate.)
780    pub fn validate(&self) -> Result<(), String> {
781        if matches!(self, Self::DimSelection { .. }) {
782            // `DimSelection` alone is rotation-symmetric — not a valid
783            // gauge fix; callers must pair ARD with `AuxPrior`/`Isometry`.
784            // Beautiful unification: return a proper error instead of a
785            // panic guard (removes the tracked ban stub while keeping the
786            // gate).
787            return Err("LatentIdMode::DimSelection is not a standalone gauge fix; \
788                 pair ARD with AuxPrior or Isometry"
789                .to_string());
790        }
791        if let Self::AuxOutcome { head, .. } = self
792            && head.effective_labeled_count() <= 0.0
793        {
794            return Err(
795                "LatentIdMode::AuxOutcome: the behavioral head has no labeled rows \
796                 (Σ row-weights = 0); a label-free head pins no gauge dimension. \
797                 Provide labels or use AuxPrior/DimSelection composition."
798                    .to_string(),
799            );
800        }
801        Ok(())
802    }
803}
804
805/// Carrier for the `∂Φ/∂t` chain-rule input, dispatched on basis kind by
806/// `LatentCoordValues::design_gradient_wrt_t_dispatch`.
807///
808/// * [`InputLocationDerivative::Radial`] is the *radial-kernel* path: the
809///   caller supplies the radial kernel family together with the center
810///   coordinates, and the chain rule
811///   `∂Φ/∂t = q(r) · (t − c)` is applied internally. This covers every
812///   isotropic radial basis — Duchon (any nullspace order), Matérn (every
813///   supported half-integer ν), and anything else whose pointwise
814///   gradient is radial. Helpers:
815///   [`crate::basis::duchon_radial_first_derivative_nd`],
816///   [`crate::basis::matern_radial_first_derivative_nd`].
817/// * [`InputLocationDerivative::Jet`] is the *pre-computed jet* path: the
818///   caller has already assembled a closed-form `(N, K, d)` tensor for a
819///   basis whose chain rule is not a simple radial scalar times a unit
820///   vector. Sphere kernels carry the tangent-direction times `K'(cos γ)`;
821///   periodic-cyclic B-splines carry the closed-form cardinal derivative;
822///   tensor-product B-splines carry the product-rule mix. Helpers:
823///   [`crate::basis::sphere_first_derivative_nd`],
824///   [`crate::basis::periodic_bspline_first_derivative_nd`],
825///   [`crate::basis::bspline_tensor_first_derivative`].
826///
827/// The dispatch is an enum rather than a trait because each path's
828/// arguments differ structurally (radial bases reuse scalar radial kernels shared with
829/// the kernel-shape chain machinery; jet bases ship the full tensor). All chain rules
830/// are analytic and closed-form; no autodiff, no finite differences.
831pub enum InputLocationDerivative<'a> {
832    /// Radial-kernel chain rule. The chain rule `(t − c)/r` is reconstructed
833    /// internally from the finite `q = φ'(r)/r` scalar and the center coordinates.
834    Radial {
835        centers: ArrayView2<'a, f64>,
836        radial_kind: &'a RadialScalarKind,
837    },
838    /// Pre-computed analytic `(n_obs, n_centers, latent_dim)` jet.
839    Jet(ArrayView3<'a, f64>),
840}
841
842/// Per-row latent coordinates `t ∈ ℝ^{N × d}` stored as a flat
843/// row-major `Array1<f64>` of length `n_obs * latent_dim`.
844///
845/// The flat-`Array1` layout mirrors [`crate::smooth::SpatialLogKappaCoords`]
846/// so the same `HyperDesignDerivative::from_implicit` / `DirectionalHyperParam`
847/// outer plumbing can consume it without modification.
848#[derive(Debug, Clone)]
849pub struct LatentCoordValues {
850    /// Stable process-local identity for this latent-coordinate block.
851    id: u64,
852    /// Flattened (n_obs, latent_dim) latent matrix, row-major
853    /// (so `values[n * d + k] = t_n[k]`).
854    values: Array1<f64>,
855    /// Number of rows `N`.
856    n_obs: usize,
857    /// Number of latent dimensions `d`.
858    latent_dim: usize,
859    /// Identifiability / gauge-fix mode.
860    id_mode: LatentIdMode,
861    /// Manifold used for per-row Riemannian updates.
862    manifold: LatentManifold,
863    /// Explicit update-side retraction. The empty registry is Euclidean.
864    retraction_registry: LatentRetractionRegistry,
865}
866
867impl LatentCoordValues {
868    /// Construct from a dense `(n_obs, latent_dim)` matrix.
869    pub fn from_matrix(matrix: ArrayView2<'_, f64>, id_mode: LatentIdMode) -> Self {
870        Self::from_matrix_with_manifold(matrix, id_mode, LatentManifold::Euclidean)
871    }
872
873    /// Construct from a dense matrix and explicit latent manifold.
874    pub fn from_matrix_with_manifold(
875        matrix: ArrayView2<'_, f64>,
876        id_mode: LatentIdMode,
877        manifold: LatentManifold,
878    ) -> Self {
879        Self::from_matrix_with_manifold_and_retraction(
880            matrix,
881            id_mode,
882            manifold,
883            LatentRetractionRegistry::all_euclidean(),
884        )
885    }
886
887    pub fn from_matrix_with_manifold_and_retraction(
888        matrix: ArrayView2<'_, f64>,
889        id_mode: LatentIdMode,
890        manifold: LatentManifold,
891        retraction_registry: LatentRetractionRegistry,
892    ) -> Self {
893        id_mode
894            .validate()
895            .expect("invalid LatentIdMode for LatentCoordValues::from_matrix_with_manifold");
896        let n_obs = matrix.nrows();
897        let latent_dim = matrix.ncols();
898        retraction_registry
899            .validate_dim(latent_dim, "LatentCoordValues::from_matrix_with_manifold")
900            .expect("invalid latent retraction dimension");
901        let mut values = Array1::<f64>::zeros(n_obs * latent_dim);
902        for n in 0..n_obs {
903            for k in 0..latent_dim {
904                values[n * latent_dim + k] = matrix[[n, k]];
905            }
906        }
907        let mut out = Self {
908            id: next_latent_coord_id(),
909            values,
910            n_obs,
911            latent_dim,
912            id_mode,
913            manifold,
914            retraction_registry,
915        };
916        out.project_all_rows_to_manifold();
917        out
918    }
919
920    pub fn from_flat_with_manifold_and_retraction_and_id(
921        values: Array1<f64>,
922        n_obs: usize,
923        latent_dim: usize,
924        id_mode: LatentIdMode,
925        manifold: LatentManifold,
926        retraction_registry: LatentRetractionRegistry,
927        id: u64,
928    ) -> Self {
929        id_mode
930            .validate()
931            .expect("invalid LatentIdMode for LatentCoordValues::from_flat");
932        assert_eq!(
933            values.len(),
934            n_obs * latent_dim,
935            "LatentCoordValues::from_flat: length {} != n_obs * latent_dim = {}",
936            values.len(),
937            n_obs * latent_dim
938        );
939        retraction_registry
940            .validate_dim(latent_dim, "LatentCoordValues::from_flat_with_manifold")
941            .expect("invalid latent retraction dimension");
942        let mut out = Self {
943            id,
944            values,
945            n_obs,
946            latent_dim,
947            id_mode,
948            manifold,
949            retraction_registry,
950        };
951        out.project_all_rows_to_manifold();
952        out
953    }
954
955    pub fn latent_id(&self) -> u64 {
956        self.id
957    }
958
959    pub fn n_obs(&self) -> usize {
960        self.n_obs
961    }
962
963    pub fn latent_dim(&self) -> usize {
964        self.latent_dim
965    }
966
967    /// Total length of the flat value array (= `n_obs * latent_dim`).
968    pub fn len(&self) -> usize {
969        self.values.len()
970    }
971
972    pub fn is_empty(&self) -> bool {
973        self.values.is_empty()
974    }
975
976    pub fn id_mode(&self) -> &LatentIdMode {
977        &self.id_mode
978    }
979
980    pub fn manifold(&self) -> &LatentManifold {
981        &self.manifold
982    }
983
984    pub fn retraction_registry(&self) -> &LatentRetractionRegistry {
985        &self.retraction_registry
986    }
987
988    /// Effective "is all Euclidean" check used by the inner solver:
989    /// returns `true` only when *both* the declared `LatentManifold` and the
990    /// optional override retraction registry are Euclidean. The registry's
991    /// own `is_all_euclidean` answers a strictly narrower question (was an
992    /// explicit non-Euclidean override installed?) and would silently miss
993    /// non-Euclidean manifolds installed via `from_matrix_with_manifold` /
994    /// `with_manifold`, which left the registry at its `all_euclidean`
995    /// default. See `retract_flat_delta` for the matching update path.
996    pub fn effective_is_all_euclidean(&self) -> bool {
997        self.manifold.is_euclidean() && self.retraction_registry.is_all_euclidean()
998    }
999
1000    /// Effective per-axis trust-region metric weights. When the manifold is
1001    /// non-Euclidean it is the authoritative geometric description (it
1002    /// covers `Interval` and `ProductWithMetric`, which the registry's
1003    /// `RetractionKind` cannot express), so we read weights from it. When
1004    /// the manifold is Euclidean but an explicit override retraction was
1005    /// supplied (e.g. via the JSON `retraction:` key) the registry's
1006    /// weights win.
1007    pub fn effective_metric_weights(&self) -> Vec<f64> {
1008        if self.manifold.is_euclidean() {
1009            self.retraction_registry.metric_weights(self.latent_dim)
1010        } else {
1011            self.manifold.metric_weights()
1012        }
1013    }
1014
1015    /// Effective per-axis periodicity (`Some(period)` on wrapped axes). When
1016    /// the declared manifold is non-Euclidean it is authoritative; when it is
1017    /// Euclidean, an explicit override retraction (if any) decides. Returns a
1018    /// `Vec` of length `latent_dim`.
1019    pub fn effective_axis_periods(&self) -> Vec<Option<f64>> {
1020        let periods = if self.manifold.is_euclidean() {
1021            self.retraction_registry.axis_periods(self.latent_dim)
1022        } else {
1023            self.manifold.axis_periods()
1024        };
1025        assert_eq!(
1026            periods.len(),
1027            self.latent_dim,
1028            "effective_axis_periods length {} != latent_dim {}",
1029            periods.len(),
1030            self.latent_dim
1031        );
1032        periods
1033    }
1034
1035    pub fn with_manifold(&self, manifold: LatentManifold) -> Self {
1036        Self::from_flat_with_manifold_and_retraction_and_id(
1037            self.values.clone(),
1038            self.n_obs,
1039            self.latent_dim,
1040            self.id_mode.clone(),
1041            manifold,
1042            self.retraction_registry.clone(),
1043            self.id,
1044        )
1045    }
1046
1047    /// View the flat value array.
1048    pub fn as_flat(&self) -> &Array1<f64> {
1049        &self.values
1050    }
1051
1052    /// View row `n` as a length-`d` slice.
1053    pub fn row(&self, n: usize) -> &[f64] {
1054        let start = n * self.latent_dim;
1055        let end = start + self.latent_dim;
1056        &self.values.as_slice().expect("contiguous")[start..end]
1057    }
1058
1059    /// Materialize as a dense `(n_obs, latent_dim)` matrix view.
1060    /// Useful when handing `t` to a row-major basis evaluator
1061    /// (e.g. `build_duchon_basis`).
1062    pub fn as_matrix(&self) -> Array2<f64> {
1063        let mut out = Array2::<f64>::zeros((self.n_obs, self.latent_dim));
1064        for n in 0..self.n_obs {
1065            for k in 0..self.latent_dim {
1066                out[[n, k]] = self.values[n * self.latent_dim + k];
1067            }
1068        }
1069        out
1070    }
1071
1072    /// Mutable write back of the flat value array, e.g. after a Newton step.
1073    pub fn set_flat(&mut self, flat: ArrayView1<'_, f64>) {
1074        assert_eq!(flat.len(), self.values.len());
1075        self.values.assign(&flat);
1076        self.project_all_rows_to_manifold();
1077    }
1078
1079    /// Apply a flat tangent update row-by-row through the manifold retraction.
1080    pub fn retract_flat_delta(&mut self, delta: ArrayView1<'_, f64>) {
1081        assert_eq!(delta.len(), self.values.len());
1082        if self.retraction_registry.is_all_euclidean() {
1083            if self.manifold.is_euclidean() {
1084                for (t, dt) in self.values.iter_mut().zip(delta.iter()) {
1085                    *t += *dt;
1086                }
1087                return;
1088            }
1089            assert_eq!(
1090                self.manifold.ambient_dim(self.latent_dim),
1091                self.latent_dim,
1092                "LatentCoordValues::retract_flat_delta: manifold ambient dim does not match latent_dim",
1093            );
1094            for n in 0..self.n_obs {
1095                let start = n * self.latent_dim;
1096                let end = start + self.latent_dim;
1097                let next = self.manifold.retract(
1098                    self.values.slice(ndarray::s![start..end]),
1099                    delta.slice(ndarray::s![start..end]),
1100                );
1101                for a in 0..self.latent_dim {
1102                    self.values[start + a] = next[a];
1103                }
1104            }
1105            return;
1106        }
1107        for n in 0..self.n_obs {
1108            let start = n * self.latent_dim;
1109            let end = start + self.latent_dim;
1110            let mut current = self.values.slice_mut(ndarray::s![start..end]);
1111            let xi = delta.slice(ndarray::s![start..end]);
1112            self.retraction_registry.retract(&mut current, xi);
1113        }
1114    }
1115
1116    fn project_all_rows_to_manifold(&mut self) {
1117        if self.manifold.is_euclidean() {
1118            return;
1119        }
1120        // In-place row projection writes back into the same `latent_dim`-wide
1121        // slice it read, so the manifold's ambient dimension must equal the
1122        // latent dimension. A mismatch means the slice arithmetic below would
1123        // read or write past a row boundary; say which two numbers disagreed
1124        // rather than only that they did.
1125        assert_eq!(
1126            self.manifold.ambient_dim(self.latent_dim),
1127            self.latent_dim,
1128            "in-place manifold projection requires ambient_dim == latent_dim: manifold reports \
1129             ambient {} for latent_dim {}",
1130            self.manifold.ambient_dim(self.latent_dim),
1131            self.latent_dim,
1132        );
1133        for n in 0..self.n_obs {
1134            let start = n * self.latent_dim;
1135            let end = start + self.latent_dim;
1136            let projected = self
1137                .manifold
1138                .project_point(self.values.slice(ndarray::s![start..end]));
1139            for a in 0..self.latent_dim {
1140                self.values[start + a] = projected[a];
1141            }
1142        }
1143    }
1144
1145    /// Compute `∂Φ/∂t` for a radial-kernel design Φ — the original
1146    /// Duchon/Matérn path. See [`Self::design_gradient_wrt_t_dispatch`] for
1147    /// the basis-agnostic dispatch entry point.
1148    ///
1149    /// `centers` is `(n_centers, d)`.
1150    /// Returns a `(n_obs, n_centers, d)` jet whose `(n, k, a)` entry is
1151    /// `∂Φ_{n,k} / ∂t_{n,a} = q(r_{n,k}) · (t_{n,a} − c_{k,a})`.
1152    ///
1153    /// At `r = 0` the unit vector `(t − c)/r` is undefined; the radial scalar
1154    /// path therefore asks the kernel for the finite `q` limit and surfaces
1155    /// `BasisError::DegenerateAtCollision` when that limit does not exist.
1156    pub(crate) fn design_gradient_wrt_t(
1157        &self,
1158        centers: ArrayView2<'_, f64>,
1159        radial_kind: &RadialScalarKind,
1160    ) -> Result<Array3<f64>, BasisError> {
1161        let n_obs = self.n_obs;
1162        let d = self.latent_dim;
1163        let n_centers = centers.nrows();
1164        if centers.ncols() != d {
1165            crate::bail_dim_basis!(
1166                "LatentCoordValues::design_gradient_wrt_t center dimension mismatch: centers have {} cols but latent_dim is {}",
1167                centers.ncols(),
1168                d
1169            );
1170        }
1171        let mut jet = Array3::<f64>::zeros((n_obs, n_centers, d));
1172        for n in 0..n_obs {
1173            let t_n = self.row(n);
1174            for k in 0..n_centers {
1175                let mut r2 = 0.0_f64;
1176                for a in 0..d {
1177                    let delta = t_n[a] - centers[[k, a]];
1178                    r2 += delta * delta;
1179                }
1180                let r = r2.sqrt();
1181                let (_, q, _) = radial_kind.eval_design_triplet(r)?;
1182                if q == 0.0 {
1183                    continue;
1184                }
1185                for a in 0..d {
1186                    jet[[n, k, a]] = q * (t_n[a] - centers[[k, a]]);
1187                }
1188            }
1189        }
1190        Ok(jet)
1191    }
1192
1193    /// Compute `∂Φ/∂t` for an arbitrary supported basis kind, by dispatching
1194    /// to the right closed-form chain rule.
1195    ///
1196    /// All radial-kernel bases (Duchon, Matérn) reduce to the same
1197    /// `q(r) · (t − c)` chain that `design_gradient_wrt_t` already implements.
1198    /// Non-radial bases (sphere, periodic-cyclic B-spline, tensor
1199    /// B-spline) carry their own analytic `(N, K, d)` jet — the caller
1200    /// pre-builds that jet using the matching `*_first_derivative_nd` helper
1201    /// in [`crate::basis`] and passes it in via
1202    /// [`InputLocationDerivative::Jet`].
1203    ///
1204    /// This is the single entry point the outer optimizer should call; it
1205    /// stays in lock-step with the kernel-parameter chain rule that
1206    /// `SpatialLogKappaCoords` uses (re-pointed at the first kernel argument
1207    /// rather than at kernel anisotropy).
1208    pub(crate) fn design_gradient_wrt_t_dispatch(
1209        &self,
1210        input: InputLocationDerivative<'_>,
1211    ) -> Result<Array3<f64>, BasisError> {
1212        match input {
1213            InputLocationDerivative::Radial {
1214                centers,
1215                radial_kind,
1216            } => self.design_gradient_wrt_t(centers, radial_kind),
1217            InputLocationDerivative::Jet(jet) => {
1218                if jet.shape() != [self.n_obs, jet.shape()[1], self.latent_dim] {
1219                    crate::bail_dim_basis!(
1220                        "LatentCoordValues::design_gradient_wrt_t_dispatch jet shape {:?} does not match latent shape ({}, {}, {})",
1221                        jet.shape(),
1222                        self.n_obs,
1223                        jet.shape()[1],
1224                        self.latent_dim
1225                    );
1226                }
1227                // The non-radial helpers already produce a (N, K, d) tensor
1228                // in the layout downstream contraction consumes. Return a copy
1229                // so the caller owns the data and is decoupled from the source
1230                // array's lifetime.
1231                Ok(jet.to_owned())
1232            }
1233        }
1234    }
1235}
1236
1237fn wrap_to_period(x: f64, period: f64) -> f64 {
1238    assert!(
1239        period.is_finite() && period > 0.0,
1240        "wrap_to_period requires a finite positive period; got {period}"
1241    );
1242    let y = x.rem_euclid(period);
1243    if y == period { 0.0 } else { y }
1244}
1245
1246/// Normalize `v[0..dim]` to a unit vector (for `LatentManifold::Sphere`
1247/// projection and retraction).
1248///
1249/// "Or axis": if the input is zero or non-finite (degenerate or numerical
1250/// mishap in caller), gracefully fall back to the canonical first axis
1251/// unit vector `[1, 0, …, 0]`. This removes a hard panic while preserving
1252/// the sphere contract that every returned point has unit Euclidean norm.
1253/// Callers (project_point / retract on Sphere) already ensure dim matches
1254/// the view length for the manifold component.
1255fn normalize_or_axis(v: ArrayView1<'_, f64>, dim: usize) -> Array1<f64> {
1256    let mut norm_sq = 0.0_f64;
1257    for a in 0..dim {
1258        norm_sq += v[a] * v[a];
1259    }
1260    // Any positive finite `‖v‖²` normalizes without overflow: `1/√x` for the
1261    // smallest positive double is ~1e162, well inside range. Only an exactly
1262    // zero (or non-finite) norm has no direction and falls back to the axis.
1263    if norm_sq > 0.0 && norm_sq.is_finite() {
1264        let inv = 1.0 / norm_sq.sqrt();
1265        let mut out = Array1::<f64>::zeros(dim);
1266        for a in 0..dim {
1267            out[a] = v[a] * inv;
1268        }
1269        out
1270    } else {
1271        // "or axis" fallback — beautiful, non-panicking resolution for
1272        // degenerate ambient vector on the sphere.
1273        let mut out = Array1::<f64>::zeros(dim);
1274        if dim > 0 {
1275            out[0] = 1.0;
1276        }
1277        out
1278    }
1279}
1280
1281#[inline]
1282fn symmetrize(a: &mut Array2<f64>) {
1283    // Callers in this module always pass square (d, d) matrices; delegate to
1284    // the canonical helper in `linalg::utils`.
1285    gam_linalg::matrix::symmetrize_in_place(a)
1286}
1287
1288/// Auxiliary-prior penalty contribution: returns the per-row reference
1289/// coordinates `ĥ(u_n)` shape `(n_obs, d)` and the effective strength `μ`.
1290///
1291/// `t_target` is broadcast across the inner ridge of `½ μ · ‖t − t_target‖²`,
1292/// which the call site folds into the Y-stack via a virtual-row augmentation
1293/// (`y' = [y; √μ · t_target]`, `X' = [X; √μ · I_d ⊗ row-block]`). This
1294/// keeps the inner solver Gaussian-closed-form.
1295///
1296/// For `AuxPriorFamily::Ridge` the conditional mean is the closed-form ridge
1297/// regression `(UᵀU + ε I)⁻¹ UᵀT` evaluated at each row's `u_n`. For
1298/// `Linear` the ridge is zero (which raises if `UᵀU` is singular).
1299/// Closed-form auxiliary-prior REML statistics at a fixed outer coordinate `t`.
1300pub struct AuxPriorRemlStats {
1301    pub residual_sq: f64,
1302    pub log_mu: f64,
1303    pub mu: f64,
1304    pub auto: bool,
1305    pub score: f64,
1306}
1307
1308/// Auxiliary-prior REML statistics for a fixed outer coordinate `t`, given the
1309/// precomputed `targets` (see [`aux_prior_targets`]). Returns the residual sum of
1310/// squares, the precision `mu` (the supplied `aux_strength` when `Some`, else the
1311/// closed-form REML optimum `mu = K / Σr²`), whether it was auto-selected, and
1312/// the prior score `0.5·mu·Σr² − 0.5·K·ln(mu)`. The `log_mu` coordinate has this
1313/// closed-form optimum at fixed `t` because only the normalized auxiliary prior
1314/// depends on it.
1315///
1316/// `K = n_obs · latent_dim` is the number of scalar latent coordinates the single
1317/// shared precision `mu` governs. The normalizer term `−0.5·K·ln(mu)` is the prior
1318/// log-determinant `−0.5·log det₊(mu · I_K)`, so it counts every governed
1319/// coordinate. Counting only `n_obs` undercounts a `latent_dim`-dimensional latent
1320/// by exactly `latent_dim`, which biases the REML precision toward under-shrinkage
1321/// (the per-axis ARD path emits `−0.5·n_obs·ln(α)` for each of `latent_dim` axes;
1322/// a single shared `mu` must match that sum).
1323pub fn aux_prior_reml_stats(
1324    t_mat: ArrayView2<'_, f64>,
1325    targets: ArrayView2<'_, f64>,
1326    aux_strength: Option<f64>,
1327) -> Result<AuxPriorRemlStats, String> {
1328    let n_obs = t_mat.nrows();
1329    let latent_dim = t_mat.ncols();
1330    let mut residual_sq = 0.0_f64;
1331    for n in 0..n_obs {
1332        for a in 0..latent_dim {
1333            let diff = t_mat[[n, a]] - targets[[n, a]];
1334            residual_sq += diff * diff;
1335        }
1336    }
1337    if !residual_sq.is_finite() {
1338        return Err("auxiliary prior residual norm must be finite".to_string());
1339    }
1340    let (log_mu, mu, auto) = match aux_strength {
1341        Some(mu) => {
1342            if !(mu.is_finite() && mu > 0.0) {
1343                return Err(format!(
1344                    "aux_strength must be finite and positive; got {mu}"
1345                ));
1346            }
1347            (mu.ln(), mu, false)
1348        }
1349        None => {
1350            if residual_sq <= 0.0 {
1351                return Err(
1352                    "aux_strength='auto' has no finite REML optimum when the auxiliary residual is zero"
1353                        .to_string(),
1354                );
1355            }
1356            let mu = ((n_obs * latent_dim) as f64) / residual_sq;
1357            if !(mu.is_finite() && mu > 0.0) {
1358                return Err(format!(
1359                    "auto aux_strength selected a non-finite precision: {mu}"
1360                ));
1361            }
1362            (mu.ln(), mu, true)
1363        }
1364    };
1365    let score = 0.5 * mu * residual_sq - 0.5 * ((n_obs * latent_dim) as f64) * log_mu;
1366    Ok(AuxPriorRemlStats {
1367        residual_sq,
1368        log_mu,
1369        mu,
1370        auto,
1371        score,
1372    })
1373}
1374
1375pub fn aux_prior_targets(
1376    t: ArrayView2<'_, f64>,
1377    u: ArrayView2<'_, f64>,
1378    family: AuxPriorFamily,
1379) -> Result<Array2<f64>, String> {
1380    let n_obs = t.nrows();
1381    let d = t.ncols();
1382    if u.nrows() != n_obs {
1383        return Err(format!(
1384            "aux_prior_targets: u has {} rows but t has {}",
1385            u.nrows(),
1386            n_obs
1387        ));
1388    }
1389    let p = u.ncols();
1390    if p == 0 {
1391        return Err("aux_prior_targets: auxiliary u must have at least one column".into());
1392    }
1393    // gram = UᵀU  (p × p)
1394    let mut gram = Array2::<f64>::zeros((p, p));
1395    for n in 0..n_obs {
1396        for i in 0..p {
1397            for j in 0..p {
1398                gram[[i, j]] += u[[n, i]] * u[[n, j]];
1399            }
1400        }
1401    }
1402    let ridge_eps = match family {
1403        AuxPriorFamily::Ridge => {
1404            let trace: f64 = (0..p).map(|i| gram[[i, i]]).sum();
1405            (1e-6 * trace / p as f64).max(1e-12)
1406        }
1407        AuxPriorFamily::Linear => 0.0,
1408    };
1409    for i in 0..p {
1410        gram[[i, i]] += ridge_eps;
1411    }
1412    // rhs = UᵀT  (p × d)
1413    let mut rhs = Array2::<f64>::zeros((p, d));
1414    for n in 0..n_obs {
1415        for i in 0..p {
1416            for k in 0..d {
1417                rhs[[i, k]] += u[[n, i]] * t[[n, k]];
1418            }
1419        }
1420    }
1421    let coeffs = solve_spd(gram.view(), rhs.view())?;
1422    // targets = U · coeffs  (n_obs × d)
1423    let mut targets = Array2::<f64>::zeros((n_obs, d));
1424    for n in 0..n_obs {
1425        for k in 0..d {
1426            let mut acc = 0.0_f64;
1427            for i in 0..p {
1428                acc += u[[n, i]] * coeffs[[i, k]];
1429            }
1430            targets[[n, k]] = acc;
1431        }
1432    }
1433    Ok(targets)
1434}
1435
1436/// Lightweight Cholesky-based SPD solve. Keeps this module dependency-free
1437/// from the broader faer-wrapping surface; matrices here are tiny
1438/// (`p × p` with p = aux-feature count, typically O(10)).
1439fn solve_spd(a: ArrayView2<'_, f64>, b: ArrayView2<'_, f64>) -> Result<Array2<f64>, String> {
1440    let n = a.nrows();
1441    if a.ncols() != n {
1442        return Err("solve_spd: A must be square".into());
1443    }
1444    if b.nrows() != n {
1445        return Err("solve_spd: RHS row count mismatch".into());
1446    }
1447    // In-place Cholesky factorization. We pay the O(n³) copy + O(n³) factor
1448    // up front; n is tiny in the auxiliary-prior path.
1449    let mut l = Array2::<f64>::zeros((n, n));
1450    for i in 0..n {
1451        for j in 0..=i {
1452            let mut sum = a[[i, j]];
1453            for k in 0..j {
1454                sum -= l[[i, k]] * l[[j, k]];
1455            }
1456            if i == j {
1457                if sum <= 0.0 {
1458                    return Err(format!(
1459                        "solve_spd: non-positive pivot {sum} at index {i} \
1460                         (matrix is not positive definite)"
1461                    ));
1462                }
1463                l[[i, j]] = sum.sqrt();
1464            } else {
1465                l[[i, j]] = sum / l[[j, j]];
1466            }
1467        }
1468    }
1469    // Solve L y = b, then Lᵀ x = y, column by column.
1470    let d = b.ncols();
1471    let mut out = Array2::<f64>::zeros((n, d));
1472    for col in 0..d {
1473        let mut y = Array1::<f64>::zeros(n);
1474        for i in 0..n {
1475            let mut sum = b[[i, col]];
1476            for k in 0..i {
1477                sum -= l[[i, k]] * y[k];
1478            }
1479            y[i] = sum / l[[i, i]];
1480        }
1481        for i in (0..n).rev() {
1482            let mut sum = y[i];
1483            for k in (i + 1)..n {
1484                sum -= l[[k, i]] * out[[k, col]];
1485            }
1486            out[[i, col]] = sum / l[[i, i]];
1487        }
1488    }
1489    Ok(out)
1490}
1491
1492#[cfg(test)]
1493mod tests {
1494    use super::*;
1495    use ndarray::array;
1496
1497    #[test]
1498    fn from_matrix_roundtrip() {
1499        let m = array![[1.0_f64, 2.0], [3.0, 4.0], [5.0, 6.0]];
1500        let lc = LatentCoordValues::from_matrix(m.view(), LatentIdMode::None);
1501        assert_eq!(lc.n_obs(), 3);
1502        assert_eq!(lc.latent_dim(), 2);
1503        let back = lc.as_matrix();
1504        assert_eq!(back, m);
1505    }
1506
1507    #[test]
1508    fn row_access() {
1509        let m = array![[1.0_f64, 2.0], [3.0, 4.0]];
1510        let lc = LatentCoordValues::from_matrix(m.view(), LatentIdMode::None);
1511        assert_eq!(lc.row(0), &[1.0, 2.0]);
1512        assert_eq!(lc.row(1), &[3.0, 4.0]);
1513    }
1514
1515    /// `preserves_isometry_cross_block_coherence` must report exactly the
1516    /// charts whose Euclidean→Riemannian geometry transform is the identity on
1517    /// the per-row gradient / `H_tt` blocks. Keying the SAE isometry
1518    /// cross-block coupling decision on `is_euclidean()` instead of this
1519    /// predicate dropped the cross-block on the flat `Circle` chart, leaving a
1520    /// block-diagonal Hessian whose joint Newton step never reached KKT
1521    /// stationarity — the arrow-Schur proximal ridge then saturated at 1e15
1522    /// (issue #795, regression of #681). We pin the predicate AND its grounding
1523    /// invariant: on `Circle` the geometry transform really is the identity, so
1524    /// coherence is preserved; on `Sphere` / `Interval` it is not.
1525    #[test]
1526    fn isometry_cross_block_coherence_tracks_identity_geometry_transform() {
1527        assert!(LatentManifold::Euclidean.preserves_isometry_cross_block_coherence());
1528        assert!(
1529            LatentManifold::Circle {
1530                period: std::f64::consts::TAU
1531            }
1532            .preserves_isometry_cross_block_coherence()
1533        );
1534        assert!(!LatentManifold::Sphere { dim: 3 }.preserves_isometry_cross_block_coherence());
1535        assert!(
1536            !LatentManifold::Interval { lo: -1.0, hi: 1.0 }
1537                .preserves_isometry_cross_block_coherence()
1538        );
1539        // A Product is coherent iff every factor is.
1540        assert!(
1541            LatentManifold::Product(vec![
1542                LatentManifold::Euclidean,
1543                LatentManifold::Circle {
1544                    period: std::f64::consts::TAU
1545                },
1546            ])
1547            .preserves_isometry_cross_block_coherence()
1548        );
1549        assert!(
1550            !LatentManifold::Product(vec![
1551                LatentManifold::Circle {
1552                    period: std::f64::consts::TAU
1553                },
1554                LatentManifold::Sphere { dim: 3 },
1555            ])
1556            .preserves_isometry_cross_block_coherence()
1557        );
1558
1559        // Grounding invariant: on the Circle chart the geometry transform that
1560        // `apply_riemannian_latent_geometry` applies — gradient projection and
1561        // the Euclidean→Riemannian Hessian conversion — is the EXACT identity,
1562        // so the coupled `μ AᵀA` block survives intact and the cross-block must
1563        // be kept.
1564        let circle = LatentManifold::Circle {
1565            period: std::f64::consts::TAU,
1566        };
1567        let t = array![0.73_f64];
1568        let eg = array![2.4_f64];
1569        let eh = array![[1.7_f64]];
1570        let projected_g = circle.project_gradient_to_tangent(t.view(), eg.view());
1571        assert_eq!(
1572            projected_g, eg,
1573            "Circle gradient projection must be identity"
1574        );
1575        let rhess = circle.riemannian_hessian_matrix(t.view(), eg.view(), eh.view());
1576        assert_eq!(
1577            rhess, eh,
1578            "Circle Riemannian Hessian must equal the Euclidean Hessian"
1579        );
1580    }
1581
1582    /// Regression for #2295: a composite `Product` mixing a d=1 factor with a
1583    /// d=2 factor must split the flat gradient at the correct per-part offsets
1584    /// (each factor is projected at ITS OWN ambient width), round-trip with
1585    /// `offset == g.len()`, and reproduce every factor's standalone projection.
1586    /// The joint mixed-dimension superposition path (zoo dims=[1,1,2,2,2,2,2,1])
1587    /// drives exactly this split; a per-part width that ignored a factor's true
1588    /// dimension miscounted the offsets and tripped `assert_eq!(offset, g.len())`
1589    /// after the first sub-dimensional factor.
1590    #[test]
1591    fn product_gradient_projection_splits_mixed_dimensional_factors() {
1592        let circle = LatentManifold::Circle {
1593            period: std::f64::consts::TAU,
1594        };
1595        // `Sphere { dim: 2 }` is S¹ embedded in R², i.e. a genuinely 2-wide
1596        // ambient block whose tangent projection removes the radial component —
1597        // a non-trivial d=2 factor next to the flat d=1 circle.
1598        let sphere = LatentManifold::Sphere { dim: 2 };
1599        let product = LatentManifold::Product(vec![circle.clone(), sphere.clone()]);
1600
1601        // Ambient width = 1 (circle) + 2 (sphere) = 3, split at offsets 0 and 1.
1602        assert_eq!(product.ambient_dim(3), 3);
1603
1604        // Base point: the circle coordinate, then a unit 2-vector for the sphere.
1605        let t = array![0.5_f64, 0.6, 0.8];
1606        let g = array![1.3_f64, 2.0, -0.7];
1607
1608        let projected = product.project_gradient_to_tangent(t.view(), g.view());
1609        assert_eq!(
1610            projected.len(),
1611            3,
1612            "composite output tiles the full ambient"
1613        );
1614
1615        // Each factor projected standalone at its own offset/width must match the
1616        // composite's corresponding block.
1617        let circle_block = circle
1618            .project_gradient_to_tangent(t.slice(ndarray::s![0..1]), g.slice(ndarray::s![0..1]));
1619        let sphere_block = sphere
1620            .project_gradient_to_tangent(t.slice(ndarray::s![1..3]), g.slice(ndarray::s![1..3]));
1621        assert_eq!(projected[0], circle_block[0], "d=1 circle factor block");
1622        for a in 0..2 {
1623            assert_eq!(projected[1 + a], sphere_block[a], "d=2 sphere factor block");
1624        }
1625
1626        // Non-triviality guard: the sphere block genuinely removed the radial
1627        // component, so this is not a vacuous identity round-trip.
1628        let radial = g[1] * t[1] + g[2] * t[2];
1629        assert!(
1630            radial.abs() > 1e-6,
1631            "fixture must exercise a non-tangent gradient on the sphere factor"
1632        );
1633        assert!(
1634            (sphere_block[0] - (g[1] - radial * t[1])).abs() < 1e-12
1635                && (sphere_block[1] - (g[2] - radial * t[2])).abs() < 1e-12,
1636            "sphere tangent projection must remove the radial component"
1637        );
1638    }
1639
1640    /// Regression for issue #191 (and the K=2 periodic case of #174):
1641    /// `from_matrix_with_manifold(Circle)` must produce a value whose
1642    /// update path wraps into `[0, 2π)` even though the override
1643    /// `LatentRetractionRegistry` is left at its `all_euclidean` default.
1644    /// Before the fix, the retraction silently decayed to Euclidean and
1645    /// values drifted outside the circle on every Newton step.
1646    #[test]
1647    fn circle_manifold_update_wraps_into_canonical_interval() {
1648        let two_pi = std::f64::consts::TAU;
1649        let near_top = 6.2_f64;
1650        let m = array![[near_top]];
1651        let mut lc = LatentCoordValues::from_matrix_with_manifold(
1652            m.view(),
1653            LatentIdMode::None,
1654            LatentManifold::Circle { period: two_pi },
1655        );
1656        let delta = Array1::from(vec![1.5_f64]);
1657        lc.retract_flat_delta(delta.view());
1658        let updated = lc.row(0)[0];
1659        let expected = (near_top + 1.5).rem_euclid(two_pi);
1660        assert!(
1661            (0.0..two_pi).contains(&updated),
1662            "Circle retraction did not wrap into [0, 2π): got {updated}",
1663        );
1664        assert!(
1665            (updated - expected).abs() < 1e-12,
1666            "Circle retraction value mismatch: got {updated}, expected {expected}",
1667        );
1668
1669        let large_delta = Array1::from(vec![10.0 * two_pi + 0.25_f64]);
1670        lc.retract_flat_delta(large_delta.view());
1671        let after_big = lc.row(0)[0];
1672        assert!(
1673            (0.0..two_pi).contains(&after_big),
1674            "Circle retraction did not wrap a large delta: got {after_big}",
1675        );
1676    }
1677
1678    /// Mirror of the Circle regression for `LatentManifold::Sphere`: the
1679    /// per-row update must preserve unit norm. Before the fix the registry
1680    /// stayed Euclidean and the additive update broke the constraint.
1681    #[test]
1682    fn sphere_manifold_update_preserves_unit_norm() {
1683        let m = array![[1.0_f64, 0.0, 0.0]];
1684        let mut lc = LatentCoordValues::from_matrix_with_manifold(
1685            m.view(),
1686            LatentIdMode::None,
1687            LatentManifold::Sphere { dim: 3 },
1688        );
1689        let delta = Array1::from(vec![0.3_f64, 0.7, -0.2]);
1690        lc.retract_flat_delta(delta.view());
1691        let row = lc.row(0);
1692        let norm_sq: f64 = row.iter().map(|x| x * x).sum();
1693        assert!(
1694            (norm_sq.sqrt() - 1.0).abs() < 1e-12,
1695            "Sphere retraction did not preserve unit norm: ||t|| = {}",
1696            norm_sq.sqrt(),
1697        );
1698
1699        let big_delta = Array1::from(vec![50.0_f64, -25.0, 13.0]);
1700        lc.retract_flat_delta(big_delta.view());
1701        let row2 = lc.row(0);
1702        let norm_sq2: f64 = row2.iter().map(|x| x * x).sum();
1703        assert!(
1704            (norm_sq2.sqrt() - 1.0).abs() < 1e-12,
1705            "Sphere retraction failed to renormalize after large delta: ||t|| = {}",
1706            norm_sq2.sqrt(),
1707        );
1708    }
1709}