Skip to main content

gam_solve/reml/reml_outer_engine/
hyper_operator.rs

1use super::*;
2
3pub(crate) fn as_implicit(op: &dyn HyperOperator) -> Option<&ImplicitHyperOperator> {
4    op.as_any().downcast_ref::<ImplicitHyperOperator>()
5}
6
7pub(crate) fn as_composite(op: &dyn HyperOperator) -> Option<&CompositeHyperOperator> {
8    op.as_any().downcast_ref::<CompositeHyperOperator>()
9}
10
11pub(crate) fn as_weighted(op: &dyn HyperOperator) -> Option<&WeightedHyperOperator> {
12    op.as_any().downcast_ref::<WeightedHyperOperator>()
13}
14
15pub(crate) trait DriftDerivTraceExt {
16    fn trace_logdet(&self, hop: &dyn HessianFactorization) -> f64;
17
18    fn trace_logdet_hessian_cross(&self, rhs: &Self, hop: &dyn HessianFactorization) -> f64;
19}
20
21impl DriftDerivTraceExt for DriftDerivResult {
22    fn trace_logdet(&self, hop: &dyn HessianFactorization) -> f64 {
23        match self {
24            Self::Dense(matrix) => hop.trace_logdet_gradient(matrix),
25            Self::Operator(operator) => hop.trace_logdet_operator(operator.as_ref()),
26        }
27    }
28
29    fn trace_logdet_hessian_cross(&self, rhs: &Self, hop: &dyn HessianFactorization) -> f64 {
30        match (self, rhs) {
31            (Self::Dense(left), Self::Dense(right)) => hop.trace_logdet_hessian_cross(left, right),
32            (Self::Dense(left), Self::Operator(right)) => {
33                hop.trace_logdet_hessian_cross_matrix_operator(left, right.as_ref())
34            }
35            (Self::Operator(left), Self::Dense(right)) => {
36                hop.trace_logdet_hessian_cross_matrix_operator(right, left.as_ref())
37            }
38            (Self::Operator(left), Self::Operator(right)) => {
39                hop.trace_logdet_hessian_cross_operator(left.as_ref(), right.as_ref())
40            }
41        }
42    }
43}
44
45#[derive(Clone)]
46pub struct CompositeHyperOperator {
47    pub dense: Option<Array2<f64>>,
48    pub operators: Vec<Arc<dyn HyperOperator>>,
49    pub dim_hint: usize,
50}
51
52/// Group composite operators by shared `(implicit_deriv, x_design, w_diag)`
53/// so every Duchon ψ-axis built atop the same implicit derivative runs
54/// through a single row-kernel sweep via
55/// `trace_projected_factor_all_axes_with_xf`. Per-axis `s_psi` and
56/// `c_x_psi_beta` are threaded in individually so the batched path matches
57/// the per-axis path exactly. Non-implicit operators and singleton groups
58/// fall through to the original per-op trace path.
59pub(crate) fn composite_trace_implicit_batched(
60    operators: &[Arc<dyn HyperOperator>],
61    factor: &Array2<f64>,
62    cache: Option<&ProjectedFactorCache>,
63) -> f64 {
64    let mut trace = 0.0;
65    let mut group_starts: Vec<Vec<usize>> = Vec::new();
66    let mut handled = vec![false; operators.len()];
67
68    for (i, op) in operators.iter().enumerate() {
69        if handled[i] {
70            continue;
71        }
72        let Some(impl_i) = as_implicit(op.as_ref()) else {
73            continue;
74        };
75        let mut group = vec![i];
76        handled[i] = true;
77        for j in (i + 1)..operators.len() {
78            if handled[j] {
79                continue;
80            }
81            if let Some(impl_j) = as_implicit(operators[j].as_ref())
82                && Arc::ptr_eq(&impl_i.implicit_deriv, &impl_j.implicit_deriv)
83                && Arc::ptr_eq(&impl_i.x_design, &impl_j.x_design)
84                && Arc::ptr_eq(impl_i.w_diag.as_arc(), impl_j.w_diag.as_arc())
85                && impl_i.p == impl_j.p
86            {
87                group.push(j);
88                handled[j] = true;
89            }
90        }
91        group_starts.push(group);
92    }
93
94    for group in &group_starts {
95        if group.len() >= 2 {
96            let lead = as_implicit(operators[group[0]].as_ref()).unwrap();
97            let xf = match cache {
98                Some(c) => lead.cached_xf(factor, c),
99                None => Arc::new(lead.compute_xf(factor)),
100            };
101            let axes: Vec<(usize, &Array2<f64>, Option<&Array1<f64>>)> = group
102                .iter()
103                .map(|&k| {
104                    let op = as_implicit(operators[k].as_ref()).unwrap();
105                    (op.axis, &op.s_psi, op.c_x_psi_beta.as_deref())
106                })
107                .collect();
108            let values = lead.trace_projected_factor_all_axes_with_xf(factor, xf.view(), &axes);
109            trace += values.iter().sum::<f64>();
110        } else {
111            let op = &operators[group[0]];
112            trace += match cache {
113                Some(c) => op.trace_projected_factor_cached(factor, c),
114                None => op.trace_projected_factor(factor),
115            };
116        }
117    }
118
119    for (i, op) in operators.iter().enumerate() {
120        if handled[i] {
121            continue;
122        }
123        trace += match cache {
124            Some(c) => op.trace_projected_factor_cached(factor, c),
125            None => op.trace_projected_factor(factor),
126        };
127    }
128
129    trace
130}
131
132/// Vector form of the implicit-axis trace batching used by
133/// [`CompositeHyperOperator`].  It returns one exact `tr(Fᵀ B_i F)` value per
134/// input operator while sharing the expensive `X·F` projection and Duchon
135/// row-kernel sweeps across sibling implicit ψ/ρ axes.
136pub(crate) fn trace_projected_factors_batched(
137    operators: &[Arc<dyn HyperOperator>],
138    factor: &Array2<f64>,
139    cache: &ProjectedFactorCache,
140) -> Vec<f64> {
141    let mut out = vec![0.0; operators.len()];
142    let mut handled = vec![false; operators.len()];
143
144    for i in 0..operators.len() {
145        if handled[i] {
146            continue;
147        }
148        let Some(impl_i) = as_implicit(operators[i].as_ref()) else {
149            out[i] = operators[i].trace_projected_factor_cached(factor, cache);
150            handled[i] = true;
151            continue;
152        };
153
154        let mut group = vec![i];
155        handled[i] = true;
156        for j in (i + 1)..operators.len() {
157            if handled[j] {
158                continue;
159            }
160            if let Some(impl_j) = as_implicit(operators[j].as_ref())
161                && Arc::ptr_eq(&impl_i.implicit_deriv, &impl_j.implicit_deriv)
162                && Arc::ptr_eq(&impl_i.x_design, &impl_j.x_design)
163                && Arc::ptr_eq(impl_i.w_diag.as_arc(), impl_j.w_diag.as_arc())
164                && impl_i.p == impl_j.p
165            {
166                group.push(j);
167                handled[j] = true;
168            }
169        }
170
171        if group.len() >= 2 {
172            let xf = impl_i.cached_xf(factor, cache);
173            let axes: Vec<(usize, &Array2<f64>, Option<&Array1<f64>>)> = group
174                .iter()
175                .map(|&idx| {
176                    let op = as_implicit(operators[idx].as_ref()).unwrap();
177                    (op.axis, &op.s_psi, op.c_x_psi_beta.as_deref())
178                })
179                .collect();
180            let values = impl_i.trace_projected_factor_all_axes_with_xf(factor, xf.view(), &axes);
181            for (&idx, value) in group.iter().zip(values) {
182                out[idx] = value;
183            }
184        } else {
185            out[i] = operators[i].trace_projected_factor_cached(factor, cache);
186        }
187    }
188
189    out
190}
191
192pub(crate) fn collect_projected_trace_terms<'a>(
193    out_idx: usize,
194    weight: f64,
195    op: &'a dyn HyperOperator,
196    factor: &Array2<f64>,
197    dense_acc: &mut [f64],
198    terms: &mut Vec<(usize, f64, &'a dyn HyperOperator)>,
199) {
200    if weight == 0.0 {
201        return;
202    }
203    if let Some(composite) = as_composite(op) {
204        if let Some(dense) = composite.dense.as_ref() {
205            dense_acc[out_idx] += weight * dense_trace_projected_factor(dense, factor);
206        }
207        for inner in &composite.operators {
208            collect_projected_trace_terms(
209                out_idx,
210                weight,
211                inner.as_ref(),
212                factor,
213                dense_acc,
214                terms,
215            );
216        }
217    } else if let Some(weighted) = as_weighted(op) {
218        for (term_weight, inner) in &weighted.terms {
219            collect_projected_trace_terms(
220                out_idx,
221                weight * *term_weight,
222                inner.as_ref(),
223                factor,
224                dense_acc,
225                terms,
226            );
227        }
228    } else {
229        terms.push((out_idx, weight, op));
230    }
231}
232
233pub(crate) fn collect_projected_matrix_terms<'a>(
234    out_idx: usize,
235    weight: f64,
236    op: &'a dyn HyperOperator,
237    factor: &Array2<f64>,
238    dense_acc: &mut [Array2<f64>],
239    terms: &mut Vec<(usize, f64, &'a dyn HyperOperator)>,
240) {
241    if weight == 0.0 {
242        return;
243    }
244    if let Some(composite) = as_composite(op) {
245        if let Some(dense) = composite.dense.as_ref() {
246            dense_acc[out_idx].scaled_add(weight, &dense_projected_matrix(dense, factor));
247        }
248        for inner in &composite.operators {
249            collect_projected_matrix_terms(
250                out_idx,
251                weight,
252                inner.as_ref(),
253                factor,
254                dense_acc,
255                terms,
256            );
257        }
258    } else if let Some(weighted) = as_weighted(op) {
259        for (term_weight, inner) in &weighted.terms {
260            collect_projected_matrix_terms(
261                out_idx,
262                weight * *term_weight,
263                inner.as_ref(),
264                factor,
265                dense_acc,
266                terms,
267            );
268        }
269    } else {
270        terms.push((out_idx, weight, op));
271    }
272}
273
274pub(crate) fn trace_projected_operator_terms_batched(
275    n_out: usize,
276    terms: &[(usize, f64, &dyn HyperOperator)],
277    factor: &Array2<f64>,
278    cache: &ProjectedFactorCache,
279) -> Vec<f64> {
280    let mut out = vec![0.0_f64; n_out];
281    let mut handled = vec![false; terms.len()];
282
283    for i in 0..terms.len() {
284        if handled[i] {
285            continue;
286        }
287        let Some(impl_i) = as_implicit(terms[i].2) else {
288            continue;
289        };
290        let mut group = vec![i];
291        handled[i] = true;
292        for j in (i + 1)..terms.len() {
293            if handled[j] {
294                continue;
295            }
296            if let Some(impl_j) = as_implicit(terms[j].2)
297                && Arc::ptr_eq(&impl_i.implicit_deriv, &impl_j.implicit_deriv)
298                && Arc::ptr_eq(&impl_i.x_design, &impl_j.x_design)
299                && Arc::ptr_eq(impl_i.w_diag.as_arc(), impl_j.w_diag.as_arc())
300                && impl_i.p == impl_j.p
301            {
302                group.push(j);
303                handled[j] = true;
304            }
305        }
306
307        let lead = as_implicit(terms[group[0]].2).unwrap();
308        let xf = lead.cached_xf(factor, cache);
309        let axes: Vec<(usize, &Array2<f64>, Option<&Array1<f64>>)> = group
310            .iter()
311            .map(|&term_idx| {
312                let op = as_implicit(terms[term_idx].2).unwrap();
313                (op.axis, &op.s_psi, op.c_x_psi_beta.as_deref())
314            })
315            .collect();
316        let values = lead.trace_projected_factor_all_axes_with_xf(factor, xf.view(), &axes);
317        for (&term_idx, value) in group.iter().zip(values.iter()) {
318            let (out_idx, weight, _) = terms[term_idx];
319            out[out_idx] += weight * *value;
320        }
321    }
322
323    for (i, (out_idx, weight, op)) in terms.iter().enumerate() {
324        if handled[i] {
325            continue;
326        }
327        out[*out_idx] += *weight * op.trace_projected_factor_cached(factor, cache);
328    }
329
330    out
331}
332
333pub(crate) fn projected_operator_terms_batched(
334    n_out: usize,
335    terms: &[(usize, f64, &dyn HyperOperator)],
336    factor: &Array2<f64>,
337    cache: &ProjectedFactorCache,
338) -> Vec<Array2<f64>> {
339    let rank = factor.ncols();
340    let mut out: Vec<Array2<f64>> = (0..n_out)
341        .map(|_| Array2::<f64>::zeros((rank, rank)))
342        .collect();
343    for (out_idx, weight, op) in terms.iter() {
344        let projected = op.projected_matrix_cached(factor, cache);
345        out[*out_idx].scaled_add(*weight, &projected);
346    }
347    out
348}
349
350pub(crate) fn project_hyper_operators_batched(
351    n_out: usize,
352    terms: &[(usize, f64, &dyn HyperOperator)],
353    factor: &Array2<f64>,
354    cache: &ProjectedFactorCache,
355) -> Vec<Array2<f64>> {
356    projected_operator_terms_batched(n_out, terms, factor, cache)
357}
358
359pub(crate) fn trace_logdet_drifts_projected_factor_batched(
360    drifts: &[DriftDerivResult],
361    factor: &Array2<f64>,
362    cache: &ProjectedFactorCache,
363) -> Vec<f64> {
364    let mut out = vec![0.0_f64; drifts.len()];
365    let mut terms: Vec<(usize, f64, &dyn HyperOperator)> = Vec::new();
366    for (idx, drift) in drifts.iter().enumerate() {
367        match drift {
368            DriftDerivResult::Dense(matrix) => {
369                out[idx] += dense_trace_projected_factor(matrix, factor);
370            }
371            DriftDerivResult::Operator(op) => {
372                collect_projected_trace_terms(idx, 1.0, op.as_ref(), factor, &mut out, &mut terms);
373            }
374        }
375    }
376    let batched = trace_projected_operator_terms_batched(drifts.len(), &terms, factor, cache);
377    for (dst, value) in out.iter_mut().zip(batched) {
378        *dst += value;
379    }
380    out
381}
382
383pub(crate) fn dense_spectral_trace_logdet_drifts_batched(
384    ds: &DenseSpectralOperator,
385    drifts: &[DriftDerivResult],
386) -> Vec<f64> {
387    trace_logdet_drifts_projected_factor_batched(drifts, &ds.g_factor, &ds.projected_factor_cache)
388}
389
390pub(crate) fn penalty_subspace_trace_factor(kernel: &PenaltySubspaceTrace) -> Array2<f64> {
391    let (evals, evecs) = kernel
392        .h_proj_inverse
393        .eigh(faer::Side::Lower)
394        .expect("PenaltySubspaceTrace kernel factor eigendecomposition failed");
395    let r = evals.len();
396    // F must satisfy F·Fᵀ = K exactly: the batched `tr(FᵀAF)` is consumed as
397    // the gradient of the SAME pseudo-logdet criterion whose exact kernel the
398    // per-coordinate path contracts via `h_proj_inverse` directly. The kernel
399    // eigenvalues are `1/σ_a` over the kept Hessian spectrum, so their
400    // dynamic range is the Hessian condition number — clamp ONLY the
401    // roundoff-negative tail to zero (K is PSD by construction; a negative
402    // eigenvalue is O(ε)·‖K‖ eigensolver noise, and √(max(λ,0)) is the
403    // honest PSD square root). A relative floor here is NOT a stabilization:
404    // raising `1/σ_max` to `√ε·r·(1/σ_min)` rewrites the criterion's
405    // sensitivity along exactly the stiffest directions — where the ρ-drifts
406    // `λ_k·S_k` live — inflating the analytic trace by up to `√ε·r·κ(H_pen)`
407    // (O(1) once κ ≳ 1e7) while FD differentiates the true criterion. That
408    // desync red-lined every iso-κ Duchon probit/logit FD test and starved
409    // the spatial κ-optimizer of descent directions; Gaussian was immune
410    // because the intrinsic kernel is only installed for c-nontrivial
411    // families (#901).
412    let mut root = evecs.clone();
413    for col in 0..r {
414        let scale = evals[col].max(0.0).sqrt();
415        for row in 0..r {
416            root[[row, col]] *= scale;
417        }
418    }
419    gam_linalg::faer_ndarray::fast_ab(&kernel.u_s, &root)
420}
421
422pub(crate) fn penalty_subspace_trace_drifts_batched(
423    kernel: &PenaltySubspaceTrace,
424    drifts: &[DriftDerivResult],
425) -> Vec<f64> {
426    let factor = penalty_subspace_trace_factor(kernel);
427    let cache = ProjectedFactorCache::default();
428    trace_logdet_drifts_projected_factor_batched(drifts, &factor, &cache)
429}
430
431pub(crate) fn penalty_subspace_reduce_drifts_batched(
432    kernel: &PenaltySubspaceTrace,
433    drifts: &[DriftDerivResult],
434) -> Vec<Array2<f64>> {
435    drifts
436        .iter()
437        .map(|drift| match drift {
438            DriftDerivResult::Dense(matrix) => kernel.reduce(matrix),
439            // #901 layer-2 (outer-Hessian path): reduce the operator via
440            // `U_Sᵀ·A·U_S = U_Sᵀ·A.mul_mat(U_S)` — NOT `op.to_dense()` then
441            // reduce. For the GLM cubic correction `C[v] = Xᵀdiag(c⊙Xv)X` the
442            // dense materialization computes near-null quadratic forms by
443            // cancelling O(‖C‖) entries, and the spectral kernel's `1/σ_min`
444            // then amplifies the roundoff (the +39-vs-−0.30 / ~−7.7e5 blow-up).
445            // `reduce_operator` probes through the `X·U_S` matvecs instead, so
446            // tiny² stays tiny — the same stability cure as the first-order
447            // `trace_operator` path.
448            DriftDerivResult::Operator(op) => kernel.reduce_operator(op.as_ref()),
449        })
450        .collect()
451}
452
453pub(crate) fn dense_spectral_trace_logdet_operators_batched(
454    ds: &DenseSpectralOperator,
455    operators: &[Arc<dyn HyperOperator>],
456) -> Vec<f64> {
457    if operators.is_empty() {
458        return Vec::new();
459    }
460    if log::log_enabled!(log::Level::Info) {
461        let start = std::time::Instant::now();
462        let out =
463            trace_projected_factors_batched(operators, &ds.g_factor, &ds.projected_factor_cache);
464        let implicit_count = operators.iter().filter(|op| op.is_implicit()).count();
465        dense_spectral_stage_log(
466            &format!(
467                "DenseSpectralOperator::trace_logdet_operators_batched dim={} rank={} ops={} implicit_ops={}",
468                ds.n_dim,
469                ds.g_factor.ncols(),
470                operators.len(),
471                implicit_count,
472            ),
473            start.elapsed().as_secs_f64(),
474        );
475        out
476    } else {
477        trace_projected_factors_batched(operators, &ds.g_factor, &ds.projected_factor_cache)
478    }
479}
480
481impl HyperOperator for CompositeHyperOperator {
482    fn as_any(&self) -> &(dyn std::any::Any + 'static) {
483        self
484    }
485
486    fn dim(&self) -> usize {
487        self.dim_hint
488    }
489
490    fn mul_vec(&self, v: &Array1<f64>) -> Array1<f64> {
491        let mut out = Array1::<f64>::zeros(v.len());
492        self.mul_vec_into(v.view(), out.view_mut());
493        out
494    }
495
496    fn mul_vec_view(&self, v: ArrayView1<'_, f64>) -> Array1<f64> {
497        let mut out = Array1::<f64>::zeros(v.len());
498        self.mul_vec_into(v, out.view_mut());
499        out
500    }
501
502    fn mul_vec_into(&self, v: ArrayView1<'_, f64>, mut out: ArrayViewMut1<'_, f64>) {
503        if self.dense.is_none() && self.operators.len() == 1 {
504            self.operators[0].mul_vec_into(v, out);
505            return;
506        }
507
508        out.fill(0.0);
509        if let Some(dense) = self.dense.as_ref() {
510            dense::matvec_into(dense, v, out.view_mut());
511        }
512        for op in &self.operators {
513            op.scaled_add_mul_vec(v, 1.0, out.view_mut());
514        }
515    }
516
517    fn mul_basis_columns_into(&self, start: usize, mut out: ArrayViewMut2<'_, f64>) {
518        if self.dense.is_none() && self.operators.len() == 1 {
519            self.operators[0].mul_basis_columns_into(start, out);
520            return;
521        }
522
523        out.fill(0.0);
524        let cols = out.ncols();
525        let end = start + cols;
526        if let Some(dense) = self.dense.as_ref() {
527            out += &dense.slice(ndarray::s![.., start..end]);
528        }
529        let mut work = Array2::<f64>::zeros((out.nrows(), cols));
530        for op in &self.operators {
531            op.mul_basis_columns_into(start, work.view_mut());
532            out += &work;
533        }
534    }
535
536    fn scaled_add_mul_vec(
537        &self,
538        v: ArrayView1<'_, f64>,
539        scale: f64,
540        mut out: ArrayViewMut1<'_, f64>,
541    ) {
542        if scale == 0.0 {
543            return;
544        }
545        if self.dense.is_none() && self.operators.len() == 1 {
546            self.operators[0].scaled_add_mul_vec(v, scale, out);
547            return;
548        }
549
550        if let Some(dense) = self.dense.as_ref() {
551            dense::matvec_scaled_add_into(dense, v, scale, out.view_mut());
552        }
553        for op in &self.operators {
554            op.scaled_add_mul_vec(v, scale, out.view_mut());
555        }
556    }
557
558    /// Forward batched apply to inner operators so their `mul_mat` overrides
559    /// (matrix-free Khatri–Rao BLAS3 fuses) fire instead of the default
560    /// per-column parallel matvec — which would triple-nest rayon when an
561    /// inner op already parallelizes internally.
562    fn mul_mat(&self, factor: &Array2<f64>) -> Array2<f64> {
563        if self.dense.is_none() && self.operators.len() == 1 {
564            return self.operators[0].mul_mat(factor);
565        }
566        let p = factor.nrows();
567        let k = factor.ncols();
568        let mut out = Array2::<f64>::zeros((p, k));
569        if let Some(dense) = self.dense.as_ref() {
570            out += &dense.dot(factor);
571        }
572        for op in &self.operators {
573            out += &op.mul_mat(factor);
574        }
575        out
576    }
577
578    fn trace_projected_factor(&self, factor: &Array2<f64>) -> f64 {
579        if self.dense.is_none() && self.operators.len() == 1 {
580            return self.operators[0].trace_projected_factor(factor);
581        }
582
583        let mut trace = 0.0;
584        if let Some(dense) = self.dense.as_ref() {
585            let dense_factor = dense.dot(factor);
586            trace += factor
587                .iter()
588                .zip(dense_factor.iter())
589                .map(|(&f, &bf)| f * bf)
590                .sum::<f64>();
591        }
592        trace += composite_trace_implicit_batched(&self.operators, factor, None);
593        trace
594    }
595
596    fn trace_projected_factor_cached(
597        &self,
598        factor: &Array2<f64>,
599        cache: &ProjectedFactorCache,
600    ) -> f64 {
601        if self.dense.is_none() && self.operators.len() == 1 {
602            return self.operators[0].trace_projected_factor_cached(factor, cache);
603        }
604
605        let mut trace = 0.0;
606        if let Some(dense) = self.dense.as_ref() {
607            let dense_factor = dense.dot(factor);
608            trace += factor
609                .iter()
610                .zip(dense_factor.iter())
611                .map(|(&f, &bf)| f * bf)
612                .sum::<f64>();
613        }
614        trace += composite_trace_implicit_batched(&self.operators, factor, Some(cache));
615        trace
616    }
617
618    fn projected_matrix(&self, factor: &Array2<f64>) -> Array2<f64> {
619        if self.dense.is_none() && self.operators.len() == 1 {
620            return self.operators[0].projected_matrix(factor);
621        }
622
623        let rank = factor.ncols();
624        let mut projected = Array2::<f64>::zeros((rank, rank));
625        if let Some(dense) = self.dense.as_ref() {
626            let mf = gam_linalg::faer_ndarray::fast_ab(dense, factor);
627            projected += &gam_linalg::faer_ndarray::fast_atb(factor, &mf);
628        }
629        for op in &self.operators {
630            projected += &op.projected_matrix(factor);
631        }
632        projected
633    }
634
635    fn projected_matrix_cached(
636        &self,
637        factor: &Array2<f64>,
638        cache: &ProjectedFactorCache,
639    ) -> Array2<f64> {
640        if self.dense.is_none() && self.operators.len() == 1 {
641            return self.operators[0].projected_matrix_cached(factor, cache);
642        }
643
644        let rank = factor.ncols();
645        let mut projected = Array2::<f64>::zeros((rank, rank));
646        if let Some(dense) = self.dense.as_ref() {
647            let mf = gam_linalg::faer_ndarray::fast_ab(dense, factor);
648            projected += &gam_linalg::faer_ndarray::fast_atb(factor, &mf);
649        }
650        for op in &self.operators {
651            projected += &op.projected_matrix_cached(factor, cache);
652        }
653        projected
654    }
655
656    fn bilinear(&self, v: &Array1<f64>, u: &Array1<f64>) -> f64 {
657        let mut total = 0.0;
658        if let Some(dense) = self.dense.as_ref() {
659            total += dense::bilinear(dense, v.view(), u.view());
660        }
661        for op in &self.operators {
662            total += op.bilinear(v, u);
663        }
664        total
665    }
666
667    fn bilinear_view(&self, v: ArrayView1<'_, f64>, u: ArrayView1<'_, f64>) -> f64 {
668        let mut total = 0.0;
669        if let Some(dense) = self.dense.as_ref() {
670            total += dense::bilinear(dense, v, u);
671        }
672        for op in &self.operators {
673            total += op.bilinear_view(v, u);
674        }
675        total
676    }
677
678    fn to_dense(&self) -> Array2<f64> {
679        let mut out = self
680            .dense
681            .clone()
682            .unwrap_or_else(|| Array2::<f64>::zeros((self.dim_hint, self.dim_hint)));
683        for op in &self.operators {
684            out += &op.to_dense();
685        }
686        out
687    }
688
689    fn is_implicit(&self) -> bool {
690        self.operators.iter().any(|op| op.is_implicit())
691    }
692}
693
694/// Implicit Hessian-drift operator for a single anisotropic ψ_d coordinate.
695///
696/// Computes B_d · v on the fly:
697///   B_d · v = (∂X/∂ψ_d)^T (W · (X · v)) + X^T (W · ((∂X/∂ψ_d) · v)) + S_{ψ_d} · v
698///
699/// The first two terms use the implicit design-derivative operator (no dense
700/// (n × p) matrices), and S_{ψ_d} is a dense (p × p) penalty matrix (manageable).
701///
702/// Storage: the implicit operator holds O(n·k·D) radial jets, plus references
703/// to an active-basis X design operator and W (the working weights). The
704/// penalty matrix S_{ψ_d} is stored as a dense (p × p) matrix.
705/// Thread-local scratch buffers for `ImplicitHyperOperator::mul_vec_into`.
706/// Reused across PCG iterations and basis-column sweeps so each matvec
707/// avoids three fresh O(n)/O(p) allocations.
708mod implicit_matvec_scratch {
709    use std::cell::RefCell;
710
711    pub(super) struct Scratch {
712        pub x_v: Vec<f64>,
713        pub n_work: Vec<f64>,
714        pub p_work: Vec<f64>,
715    }
716
717    impl Scratch {
718        pub(crate) const fn new() -> Self {
719            Self {
720                x_v: Vec::new(),
721                n_work: Vec::new(),
722                p_work: Vec::new(),
723            }
724        }
725    }
726
727    thread_local! {
728        static SCRATCH: RefCell<Scratch> = const { RefCell::new(Scratch::new()) };
729    }
730
731    pub(super) fn with<R>(f: impl FnOnce(&mut Scratch) -> R) -> R {
732        SCRATCH.with(|cell| f(&mut cell.borrow_mut()))
733    }
734}
735
736pub struct ImplicitHyperOperator {
737    /// The implicit design-derivative operator (shared across all axes).
738    pub implicit_deriv: std::sync::Arc<gam_terms::basis::ImplicitDesignPsiDerivative>,
739    /// Which axis this operator is for.
740    pub axis: usize,
741    /// The active-basis design matrix X. This may be lazy / operator-backed.
742    pub(crate) x_design: std::sync::Arc<DesignMatrix>,
743    /// Working weights W (diagonal, length n) — observed-information curvature,
744    /// signed for non-canonical links. Carried as the owned [`gam_linalg::matrix::SignedWeightsArc`]
745    /// newtype so the sign character is construction-enforced at the operator
746    /// struct boundary; the function-boundary contract from `linalg/matrix.rs`
747    /// is no longer reconstructable accidentally inside `mul_vec`.
748    pub(crate) w_diag: gam_linalg::matrix::SignedWeightsArc,
749    /// Penalty derivative matrix S_{ψ_d} (p × p), dense.
750    pub s_psi: Array2<f64>,
751    /// Total basis dimension p.
752    pub(crate) p: usize,
753    /// Non-Gaussian fixed-β third-derivative correction: c ⊙ (X_{ψ_d} β̂),
754    /// length n. When present, the operator additionally applies
755    /// `Xᵀ diag(c_x_psi_beta) X v` so that the full B_d formula
756    /// `B_d v = (∂X/∂ψ_d)ᵀ W X v + Xᵀ W (∂X/∂ψ_d) v + Xᵀ diag(c ⊙ X_{ψ_d} β̂) X v + S_{ψ_d} v`
757    /// is matrix-free for non-Gaussian likelihoods. `None` for Gaussian
758    /// identity (c ≡ 0 there).
759    pub c_x_psi_beta: Option<std::sync::Arc<Array1<f64>>>,
760}
761
762impl HyperOperator for ImplicitHyperOperator {
763    fn dim(&self) -> usize {
764        self.p
765    }
766
767    fn mul_vec(&self, v: &Array1<f64>) -> Array1<f64> {
768        // Single canonical path: route every matvec through `mul_vec_into`,
769        // which routes through `matvec_with_shared_xz_into`. The four terms of
770        // B_d are assembled there, with the third-derivative correction added
771        // by `accumulate_c_correction_xt_into` so the four matvec entry points
772        // share one inner kernel.
773        let mut out = Array1::<f64>::zeros(self.p);
774        self.mul_vec_into(v.view(), out.view_mut());
775        out
776    }
777
778    fn mul_vec_view(&self, v: ArrayView1<'_, f64>) -> Array1<f64> {
779        let mut out = Array1::<f64>::zeros(self.p);
780        self.mul_vec_into(v, out.view_mut());
781        out
782    }
783
784    fn mul_vec_into(&self, v: ArrayView1<'_, f64>, out: ArrayViewMut1<'_, f64>) {
785        assert_eq!(v.len(), self.p);
786        let n_obs = self.w_diag.len();
787        // Reuse thread-local scratch across repeated matvec calls (e.g.
788        // PCG iterations, basis-column sweeps) instead of allocating
789        // (2 n_obs + p) f64s every time.
790        implicit_matvec_scratch::with(|s| {
791            s.x_v.clear();
792            s.x_v.resize(n_obs, 0.0);
793            s.n_work.clear();
794            s.n_work.resize(n_obs, 0.0);
795            s.p_work.clear();
796            s.p_work.resize(self.p, 0.0);
797            let mut x_v_view = ndarray::ArrayViewMut1::from(s.x_v.as_mut_slice());
798            let n_work_view = ndarray::ArrayViewMut1::from(s.n_work.as_mut_slice());
799            let p_work_view = ndarray::ArrayViewMut1::from(s.p_work.as_mut_slice());
800            self.x_design.apply_view_into(v, x_v_view.view_mut());
801            self.matvec_with_shared_xz_into(x_v_view.view(), v, out, n_work_view, p_work_view);
802        });
803    }
804
805    fn mul_basis_columns_into(&self, start: usize, mut out: ArrayViewMut2<'_, f64>) {
806        let cols = out.ncols();
807        assert!(start + cols <= self.p);
808
809        let n_obs = self.w_diag.len();
810        let mut basis = Array1::<f64>::zeros(self.p);
811        let mut x_col = Array1::<f64>::zeros(n_obs);
812        let mut dx_col = Array1::<f64>::zeros(n_obs);
813        let mut weighted = Array1::<f64>::zeros(n_obs);
814        let mut term = Array1::<f64>::zeros(self.p);
815
816        for local_col in 0..cols {
817            let global_col = start + local_col;
818            let mut out_col = out.column_mut(local_col);
819            out_col.assign(&self.s_psi.column(global_col));
820
821            self.x_design.column_into(global_col, x_col.view_mut());
822            Zip::from(weighted.view_mut())
823                .and(self.w_diag.view())
824                .and(x_col.view())
825                .par_for_each(|dst, &w, &x| *dst = w * x);
826            term.assign(
827                &self
828                    .implicit_deriv
829                    .transpose_mul(self.axis, &weighted.view())
830                    .expect("radial scalar evaluation failed during implicit hyper transpose_mul"),
831            );
832            out_col += &term;
833
834            basis[global_col] = 1.0;
835            dx_col.assign(
836                &self
837                    .implicit_deriv
838                    .forward_mul(self.axis, &basis.view())
839                    .expect("radial scalar evaluation failed during implicit hyper forward_mul"),
840            );
841            basis[global_col] = 0.0;
842
843            Zip::from(weighted.view_mut())
844                .and(self.w_diag.view())
845                .and(dx_col.view())
846                .par_for_each(|dst, &w, &dx| *dst = w * dx);
847            self.x_design
848                .transpose_apply_view_into(weighted.view(), term.view_mut());
849            out_col += &term;
850
851            // Non-Gaussian third-derivative correction column j: shared kernel.
852            self.accumulate_c_correction_xt_into(
853                x_col.view(),
854                weighted.view_mut(),
855                term.view_mut(),
856                out_col,
857            );
858        }
859    }
860
861    fn bilinear(&self, v: &Array1<f64>, u: &Array1<f64>) -> f64 {
862        self.bilinear_view(v.view(), u.view())
863    }
864
865    fn bilinear_view(&self, v: ArrayView1<'_, f64>, u: ArrayView1<'_, f64>) -> f64 {
866        assert_eq!(v.len(), self.p);
867        assert_eq!(u.len(), self.p);
868
869        let x_v = self.x_design.apply_view(v);
870        let x_u = self.x_design.apply_view(u);
871        let dx_v = self
872            .implicit_deriv
873            .forward_mul(self.axis, &v)
874            .expect("radial scalar evaluation failed during implicit hyper forward_mul");
875        let dx_u = self
876            .implicit_deriv
877            .forward_mul(self.axis, &u)
878            .expect("radial scalar evaluation failed during implicit hyper forward_mul");
879
880        let w = &*self.w_diag;
881        let mut design = 0.0;
882        for i in 0..w.len() {
883            design += dx_v[i] * w[i] * x_u[i];
884            design += dx_u[i] * w[i] * x_v[i];
885        }
886
887        design += self.c_correction_bilinear(&x_v, &x_u);
888
889        let penalty = dense::bilinear(&self.s_psi, v, u);
890
891        design + penalty
892    }
893
894    fn is_implicit(&self) -> bool {
895        true
896    }
897
898    fn as_any(&self) -> &(dyn std::any::Any + 'static) {
899        self
900    }
901
902    /// Compute `tr(F^T B F)` directly via fused chunked BLAS3 GEMMs on the
903    /// shared X and the shared raw kernel matrix, bypassing the rank-many
904    /// separate matvecs the default impl would run through the lazy /
905    /// operator-backed design.
906    ///
907    /// **Why this matters:** the default trait impl is
908    ///   `let bf = self.mul_mat(F); (F ⊙ bf).sum()`
909    /// which calls `mul_vec_into` per column of `F` (rank columns). On a
910    /// lazy Duchon / Matérn / CTN design each `mul_vec_into` triggers a
911    /// full `O(n · p · kernel_eval)` row-streamed matvec — and with rank ≈ p
912    /// at large-scale shape (16D-Duchon-aniso 32 ψ-axes, p ≈ 95, n = 320 K)
913    /// the per-axis trace landed at ~30 s. With 32 axes per outer Hessian
914    /// eval and ~5 outer iters that's the ~1 hr large-scale timeout.
915    ///
916    /// Algebra:
917    /// ```text
918    ///   B_d = D_d^T W X + X^T W D_d  + X^T diag(c) X  + S_psi
919    ///   D_d = (∂X/∂ψ_d) = K_d · Z_unproject       (raw kernel · unproject)
920    ///   tr(F^T B_d F) = 2 · ⟨W ⊙ DXF, XF⟩ + ⟨c ⊙ XF, XF⟩ + tr(F^T S_psi F)
921    /// ```
922    /// where `K_d` is the raw (n × n_knots) per-pair kernel scalar matrix
923    /// for axis `d` (`q · s_combo + c · coeff_sum · φ` per (i, j) pair) and
924    /// `Z_unproject` is the identifiability/padding back-projection.
925    ///
926    /// We compute `U_knot = unproject_matrix(F)` once at (n_knots × rank),
927    /// then for each row chunk do a fused pass:
928    ///   * `XF_chunk  = X_chunk · F`        (chunk × rank)  — shared-X GEMM
929    ///   * `Kd_chunk  = row_chunk_first_raw`(chunk × n_knots) — raw kernel
930    ///   * `DXF_chunk = Kd_chunk · U_knot`  (chunk × rank)  — single GEMM
931    /// and immediately accumulate `⟨W ⊙ DXF, XF⟩` and `⟨c ⊙ XF, XF⟩` over
932    /// the chunk, never materialising full XF or DXF.
933    ///
934    /// This replaces the previous `rank`-many `forward_mul` apply loop. On
935    /// the large-scale margslope-aniso-duchon16d shard each per-axis trace
936    /// drops from ~30 s to a single chunked-GEMM cost.
937    fn trace_projected_factor(&self, factor: &Array2<f64>) -> f64 {
938        assert_eq!(factor.nrows(), self.p);
939        let n_obs = self.w_diag.len();
940        let rank = factor.ncols();
941        if rank == 0 || n_obs == 0 {
942            return 0.0;
943        }
944        let xf = self.compute_xf(factor);
945        self.trace_projected_factor_with_xf(factor, xf.view())
946    }
947
948    /// Cached variant — *the* hot-path optimisation for large-scale outer
949    /// gradient/Hessian sweeps. Every ψ-axis built atop the same `x_design`
950    /// (e.g. all 32 ψ-axes of a marginal-slope model, or the same axis hit
951    /// from `g_factor` and `w_factor` traces) shares one chunked
952    /// `X · F` design GEMM per `(x_design, factor)` pair via
953    /// [`ProjectedFactorCache`]. With 32 axes per outer-gradient sweep and
954    /// O(rank) more cross-axis traces inside the outer-Hessian build, the
955    /// cache turns 32× redundant `O(n · p · rank)` GEMMs into a single one
956    /// per outer iter. At large-scale shape (`n = 320 K`, `p = rank = 95`) that
957    /// is the difference between minutes and seconds of design-GEMM work.
958    fn trace_projected_factor_cached(
959        &self,
960        factor: &Array2<f64>,
961        cache: &ProjectedFactorCache,
962    ) -> f64 {
963        assert_eq!(factor.nrows(), self.p);
964        let n_obs = self.w_diag.len();
965        let rank = factor.ncols();
966        if rank == 0 || n_obs == 0 {
967            return 0.0;
968        }
969        let xf = self.cached_xf(factor, cache);
970        self.trace_projected_factor_with_xf(factor, xf.view())
971    }
972}
973
974/// Row-block size that keeps each streamed `n × cols` chunk near an 8 MiB
975/// working set, with a 512-row floor so a wide design still makes useful BLAS-3
976/// progress per block, capped at the total row count. Shared by the implicit
977/// operator's row-streaming kernels so they cannot drift apart.
978pub(crate) fn byte_balanced_row_chunk(cols: usize, n_rows: usize) -> usize {
979    const TARGET_BYTES: usize = 8 * 1024 * 1024;
980    const MIN_CHUNK_ROWS: usize = 512;
981    let bytes_per_row = cols.max(1) * std::mem::size_of::<f64>();
982    (TARGET_BYTES / bytes_per_row)
983        .max(MIN_CHUNK_ROWS)
984        .min(n_rows)
985}
986
987impl ImplicitHyperOperator {
988    /// Chunked `X · F` via faer SIMD-parallel GEMM. The chunk-row sizing
989    /// targets ~8 MiB live blocks so the (chunk_n × p) row slice and
990    /// (chunk_n × rank) result both stay in L2/L3 across realistic large-scale
991    /// shapes; the kernel mirrors `xt_logdet_kernel_x_diagonal`'s sizing
992    /// rule. Caller wraps this in [`Self::cached_xf`] when invariance
993    /// across ψ-axes lets one matrix serve every axis at this `(x_design,
994    /// factor)` pair.
995    pub(crate) fn compute_xf(&self, factor: &Array2<f64>) -> Array2<f64> {
996        let n_obs = self.w_diag.len();
997        let rank = factor.ncols();
998        let mut xf = Array2::<f64>::zeros((n_obs, rank));
999        let chunk_rows = byte_balanced_row_chunk(self.p + rank, n_obs);
1000        let mut start = 0usize;
1001        while start < n_obs {
1002            let end = (start + chunk_rows).min(n_obs);
1003            let rows = self
1004                .x_design
1005                .try_row_chunk(start..end)
1006                // SAFETY: `try_row_chunk` only fails on operator
1007                // implementation bugs — `start..end` is built from
1008                // `0..n_obs = 0..x_design.nrows()` with
1009                // `end = (start+chunk_rows).min(n_obs)`, so the range is
1010                // always a valid sub-range of `x_design`. Failure means the
1011                // operator broke its row-chunk contract.
1012                .unwrap_or_else(|err| {
1013                    // SAFETY: row range is a valid sub-range of x_design; failure means operator broke contract.
1014                    reml_contract_panic(format!(
1015                        "ImplicitHyperOperator::compute_xf row chunk failed: {err}"
1016                    ))
1017                });
1018            let block = gam_linalg::faer_ndarray::fast_ab(&rows, factor);
1019            xf.slice_mut(ndarray::s![start..end, ..]).assign(&block);
1020            start = end;
1021        }
1022        xf
1023    }
1024
1025    /// Look up `X · F` from the [`ProjectedFactorCache`] (compute-on-miss).
1026    /// Cache key combines the shared `x_design` Arc pointer and the
1027    /// factor's value fingerprint, so two `ImplicitHyperOperator` instances
1028    /// built atop the same `x_design` (e.g. axis-0 and axis-1 of a 32-axis
1029    /// ψ-block) consult the same cache slot and hit after the first
1030    /// computes.
1031    pub(crate) fn cached_xf(
1032        &self,
1033        factor: &Array2<f64>,
1034        cache: &ProjectedFactorCache,
1035    ) -> Arc<Array2<f64>> {
1036        let design_id = Arc::as_ptr(&self.x_design) as usize;
1037        let key = ProjectedFactorKey::from_factor_view(design_id, factor.view());
1038        cache.get_or_insert_with(key, || self.compute_xf(factor))
1039    }
1040
1041    /// Evaluate `tr(Fᵀ B_d F)` given a precomputed `X · F`. Pulls every
1042    /// per-axis-redundant `X · F` out of the inner loop so the cache (or
1043    /// caller-supplied matrix) covers every ψ-axis at once. The remaining
1044    /// per-axis work is the row-kernel build (`row_chunk_first_raw`),
1045    /// the `K_d · U_knot` GEMM, the fused `⟨W ⊙ DXF, XF⟩` inner products,
1046    /// and the small dense penalty contraction.
1047    pub(crate) fn trace_projected_factor_with_xf(
1048        &self,
1049        factor: &Array2<f64>,
1050        xf: ArrayView2<'_, f64>,
1051    ) -> f64 {
1052        let rank = factor.ncols();
1053        let n_obs = self.w_diag.len();
1054        assert_eq!(xf.dim(), (n_obs, rank));
1055
1056        // Once: unproject F to raw knot space → (n_knots × rank).
1057        let u_knot = self.implicit_deriv.unproject_matrix(&factor.view());
1058
1059        // Match the chunk sizing `xt_logdet_kernel_x_diagonal` uses so the
1060        // live block stays in L2/L3 across realistic large-scale shapes.
1061        let chunk_rows = byte_balanced_row_chunk(self.p + rank, n_obs);
1062
1063        let w = self.w_diag.as_ref();
1064        let c_opt = self.c_x_psi_beta.as_ref().map(|arc| arc.as_ref());
1065        let mut design_total = 0.0_f64;
1066        let mut correction_total = 0.0_f64;
1067        let mut start = 0usize;
1068        while start < n_obs {
1069            let end = (start + chunk_rows).min(n_obs);
1070            let chunk_n = end - start;
1071
1072            // Cached-or-precomputed X·F slice for this chunk.
1073            let xf_chunk = xf.slice(ndarray::s![start..end, ..]);
1074
1075            // Raw kernel scalars for axis d on this chunk, then a single
1076            // (chunk × n_knots) · (n_knots × rank) GEMM gives DXF_chunk.
1077            let kd_chunk = self
1078                .implicit_deriv
1079                .row_chunk_first_raw(self.axis, start..end)
1080                .expect("radial scalar evaluation failed during implicit hyper forward_mul_matrix");
1081            let dxf_chunk = gam_linalg::faer_ndarray::fast_ab(&kd_chunk, &u_knot);
1082
1083            // Fused inner-product accumulation.
1084            for i_local in 0..chunk_n {
1085                let i = start + i_local;
1086                let w_i = w[i];
1087                let dxf_row = dxf_chunk.row(i_local);
1088                let xf_row = xf_chunk.row(i_local);
1089                for k in 0..rank {
1090                    design_total += dxf_row[k] * w_i * xf_row[k];
1091                }
1092                if let Some(c) = c_opt {
1093                    let c_i = c[i];
1094                    for k in 0..rank {
1095                        let v = xf_row[k];
1096                        correction_total += c_i * v * v;
1097                    }
1098                }
1099            }
1100            start = end;
1101        }
1102
1103        // Penalty trace: tr(F^T S_psi F) via dense BLAS3.
1104        let s_f = self.s_psi.dot(factor);
1105        let penalty: f64 = factor.iter().zip(s_f.iter()).map(|(&f, &s)| f * s).sum();
1106
1107        2.0 * design_total + correction_total + penalty
1108    }
1109
1110    /// Batched-axis sibling of [`Self::trace_projected_factor_with_xf`].
1111    /// Returns `tr(Fᵀ B_d F)` for every `(axis, s_psi, c_x_psi_beta)` triple
1112    /// in `axes`, sharing the unproject-and-row-sweep work across axes that
1113    /// only differ in their axis index / penalty matrix / correction vector.
1114    pub(crate) fn trace_projected_factor_all_axes_with_xf(
1115        &self,
1116        factor: &Array2<f64>,
1117        xf: ArrayView2<'_, f64>,
1118        axes: &[(usize, &Array2<f64>, Option<&Array1<f64>>)],
1119    ) -> Vec<f64> {
1120        let rank = factor.ncols();
1121        let n_obs = self.w_diag.len();
1122        assert_eq!(xf.dim(), (n_obs, rank));
1123
1124        let u_knot = self.implicit_deriv.unproject_matrix(&factor.view());
1125
1126        let chunk_rows = byte_balanced_row_chunk(self.p + rank, n_obs.max(1));
1127
1128        let w = self.w_diag.as_ref();
1129        let mut design_totals = vec![0.0_f64; axes.len()];
1130        let mut correction_totals = vec![0.0_f64; axes.len()];
1131
1132        let mut start = 0usize;
1133        while start < n_obs {
1134            let end = (start + chunk_rows).min(n_obs);
1135            let chunk_n = end - start;
1136            let xf_chunk = xf.slice(ndarray::s![start..end, ..]);
1137
1138            for (axis_idx, (axis, _s_psi, c_opt_axis)) in axes.iter().enumerate() {
1139                let kd_chunk = self
1140                    .implicit_deriv
1141                    .row_chunk_first_raw(*axis, start..end)
1142                    .expect(
1143                        "radial scalar evaluation failed during \
1144                         trace_projected_factor_all_axes_with_xf",
1145                    );
1146                let dxf_chunk = gam_linalg::faer_ndarray::fast_ab(&kd_chunk, &u_knot);
1147
1148                for i_local in 0..chunk_n {
1149                    let i = start + i_local;
1150                    let w_i = w[i];
1151                    let dxf_row = dxf_chunk.row(i_local);
1152                    let xf_row = xf_chunk.row(i_local);
1153                    for k in 0..rank {
1154                        design_totals[axis_idx] += dxf_row[k] * w_i * xf_row[k];
1155                    }
1156                    if let Some(c) = c_opt_axis {
1157                        let c_i = c[i];
1158                        for k in 0..rank {
1159                            let v = xf_row[k];
1160                            correction_totals[axis_idx] += c_i * v * v;
1161                        }
1162                    }
1163                }
1164            }
1165            start = end;
1166        }
1167
1168        axes.iter()
1169            .enumerate()
1170            .map(|(idx, (_axis, s_psi, _c_opt_axis))| {
1171                let s_f = s_psi.dot(factor);
1172                let penalty: f64 = factor.iter().zip(s_f.iter()).map(|(&f, &s)| f * s).sum();
1173                2.0 * design_totals[idx] + correction_totals[idx] + penalty
1174            })
1175            .collect()
1176    }
1177
1178    pub(crate) fn accumulate_c_correction_xt_into(
1179        &self,
1180        x_col: ArrayView1<'_, f64>,
1181        mut n_work: ArrayViewMut1<'_, f64>,
1182        mut p_work: ArrayViewMut1<'_, f64>,
1183        mut out_col: ArrayViewMut1<'_, f64>,
1184    ) {
1185        let Some(c_x_psi_beta) = self.c_x_psi_beta.as_ref() else {
1186            return;
1187        };
1188        let c = c_x_psi_beta.as_ref();
1189        assert_eq!(x_col.len(), c.len());
1190        assert_eq!(n_work.len(), c.len());
1191        assert_eq!(p_work.len(), self.p);
1192
1193        for i in 0..c.len() {
1194            n_work[i] = c[i] * x_col[i];
1195        }
1196        self.x_design
1197            .transpose_apply_view_into(n_work.view(), p_work.view_mut());
1198        out_col += &p_work;
1199    }
1200
1201    pub(crate) fn c_correction_bilinear(&self, x_v: &Array1<f64>, x_u: &Array1<f64>) -> f64 {
1202        let Some(c_x_psi_beta) = self.c_x_psi_beta.as_ref() else {
1203            return 0.0;
1204        };
1205        x_v.iter()
1206            .zip(x_u.iter())
1207            .zip(c_x_psi_beta.iter())
1208            .map(|((&xv, &xu), &c)| xv * c * xu)
1209            .sum()
1210    }
1211
1212    /// Compute the design-part bilinear form u^T (X^T C_d X) z using precomputed
1213    /// shared X-multiplies, avoiding the full B_d matvec.
1214    ///
1215    /// The design part of B_d is:
1216    ///   (∂X/∂ψ_d)^T W X + X^T W (∂X/∂ψ_d)
1217    ///
1218    /// For vectors z and u, the bilinear form u^T [design_part] z equals:
1219    ///   ((∂X/∂ψ_d) u)^T (W (Xz)) + (Xu)^T (W ((∂X/∂ψ_d) z))
1220    ///   = 2 * (w ⊙ y_vec)^T dx_z       [when u = u, z = z]
1221    ///
1222    /// where y_vec = X u, dx_z = (∂X/∂ψ_d) z.
1223    ///
1224    /// But the full bilinear form is NOT symmetric in its dependence on z vs u
1225    /// through the design derivative, so we compute both cross-terms:
1226    ///   dx_z^T (w ⊙ y_vec) + dx_u^T (w ⊙ x_vec)
1227    ///
1228    /// # Arguments
1229    /// - `x_vec`: X z (precomputed, shared across axes)
1230    /// - `y_vec`: X u (precomputed, shared across axes)
1231    /// - `z`: the probe vector (needed for forward_mul and penalty)
1232    /// - `u`: H⁻¹ z (needed for forward_mul and penalty)
1233    ///
1234    /// # Returns
1235    /// The full bilinear form u^T B_d z = design_part + penalty_part.
1236    pub fn bilinear_with_shared_x(
1237        &self,
1238        x_vec: &Array1<f64>,
1239        y_vec: &Array1<f64>,
1240        z: &Array1<f64>,
1241        u: &Array1<f64>,
1242    ) -> f64 {
1243        // Design part: dx_z^T (w ⊙ y_vec) + dx_u^T (w ⊙ x_vec)
1244        let dx_z = self
1245            .implicit_deriv
1246            .forward_mul(self.axis, &z.view())
1247            .expect("radial scalar evaluation failed during implicit hyper forward_mul");
1248        let dx_u = self
1249            .implicit_deriv
1250            .forward_mul(self.axis, &u.view())
1251            .expect("radial scalar evaluation failed during implicit hyper forward_mul");
1252
1253        let mut design = 0.0f64;
1254        let w = &*self.w_diag;
1255        for i in 0..x_vec.len() {
1256            let wi = w[i];
1257            design += dx_z[i] * wi * y_vec[i];
1258            design += dx_u[i] * wi * x_vec[i];
1259        }
1260
1261        // Non-Gaussian fixed-β third-derivative correction:
1262        //   uᵀ Xᵀ diag(c ⊙ X_{ψ_d} β̂) X z = Σ_i (X u)_i · c_x_psi_beta_i · (X z)_i
1263        //   = Σ_i y_vec[i] · c_x_psi_beta[i] · x_vec[i]
1264        if let Some(c_x_psi_beta) = self.c_x_psi_beta.as_ref() {
1265            let c = c_x_psi_beta.as_ref();
1266            for i in 0..x_vec.len() {
1267                design += y_vec[i] * c[i] * x_vec[i];
1268            }
1269        }
1270
1271        // Penalty part: u^T S_psi z
1272        let penalty = dense::bilinear(&self.s_psi, z.view(), u.view());
1273
1274        design + penalty
1275    }
1276
1277    /// Compute the design-part contribution to A_d z without the X^T step.
1278    ///
1279    /// Returns the n-vector C_d (X z) where C_d encodes the diagonal weighting.
1280    /// Specifically: (∂X/∂ψ_d)^T maps FROM n-space, but for stochastic trace
1281    /// estimation we need q_d = A_d z = X^T (C_d x_vec) + P_d z.
1282    ///
1283    /// This method computes q_d = A_d z using the shared x_vec = X z:
1284    ///   q_d = (∂X/∂ψ_d)^T (W (X z)) + X^T (W ((∂X/∂ψ_d) z)) + S_psi z
1285    /// which is the standard mul_vec but we can share x_vec across axes.
1286    pub fn matvec_with_shared_xz_into(
1287        &self,
1288        x_vec: ArrayView1<'_, f64>,
1289        z: ArrayView1<'_, f64>,
1290        mut out: ArrayViewMut1<'_, f64>,
1291        mut n_work: ArrayViewMut1<'_, f64>,
1292        mut p_work: ArrayViewMut1<'_, f64>,
1293    ) {
1294        assert_eq!(z.len(), self.p);
1295        assert_eq!(out.len(), self.p);
1296        assert_eq!(n_work.len(), self.w_diag.len());
1297        assert_eq!(p_work.len(), self.p);
1298
1299        let w = &*self.w_diag;
1300        for i in 0..w.len() {
1301            n_work[i] = w[i] * x_vec[i];
1302        }
1303        let term1 = self
1304            .implicit_deriv
1305            .transpose_mul(self.axis, &n_work.view())
1306            .expect("radial scalar evaluation failed during implicit hyper transpose_mul");
1307        out.assign(&term1);
1308
1309        let dx_z = self
1310            .implicit_deriv
1311            .forward_mul(self.axis, &z)
1312            .expect("radial scalar evaluation failed during implicit hyper forward_mul");
1313        for i in 0..w.len() {
1314            n_work[i] = w[i] * dx_z[i];
1315        }
1316        self.x_design
1317            .transpose_apply_view_into(n_work.view(), p_work.view_mut());
1318        out += &p_work;
1319
1320        dense::matvec_into(&self.s_psi, z, p_work.view_mut());
1321        out += &p_work;
1322
1323        // Non-Gaussian fixed-β third-derivative correction.
1324        if let Some(c_x_psi_beta) = self.c_x_psi_beta.as_ref() {
1325            let c = c_x_psi_beta.as_ref();
1326            for i in 0..w.len() {
1327                n_work[i] = c[i] * x_vec[i];
1328            }
1329            self.x_design
1330                .transpose_apply_view_into(n_work.view(), p_work.view_mut());
1331            out += &p_work;
1332        }
1333    }
1334}
1335
1336/// Operator-backed fixed-β Hessian drift for sparse-exact τ coordinates.
1337///
1338/// This stays in the original sparse/native coefficient basis and computes the
1339/// exact first-order τ Hessian drift
1340///   B_τ = X_τᵀ W X + Xᵀ W X_τ + Xᵀ diag(c ⊙ X_τ β̂) X + S_τ − (H_φ)_{τ}|_β
1341/// without materializing the full dense matrix up front.
1342pub struct SparseDirectionalHyperOperator {
1343    /// Original-basis design derivative X_τ.
1344    pub(crate) x_tau: super::super::HyperDesignDerivative,
1345    /// Design matrix X in the sparse-native basis.
1346    pub(crate) x_design: DesignMatrix,
1347    /// Working weights W (diagonal) — observed-information curvature, signed
1348    /// for non-canonical links.  Carried as the owned [`gam_linalg::matrix::SignedWeightsArc`]
1349    /// newtype so the sign character is construction-enforced at the operator
1350    /// struct boundary.
1351    pub(crate) w_diag: gam_linalg::matrix::SignedWeightsArc,
1352    /// Penalty derivative S_τ.
1353    pub(crate) s_tau: Array2<f64>,
1354    /// Fixed-β non-Gaussian curvature term c ⊙ (X_τ β̂), if applicable.
1355    pub(crate) c_x_tau_beta: Option<Array1<f64>>,
1356    /// Fixed-β Firth partial Hessian drift (H_φ)_{τ}|_β, if applicable.
1357    pub(crate) firth_hphi_tau_partial: Option<Array2<f64>>,
1358    /// Total coefficient dimension.
1359    pub(crate) p: usize,
1360}
1361
1362impl HyperOperator for SparseDirectionalHyperOperator {
1363    fn dim(&self) -> usize {
1364        self.p
1365    }
1366
1367    fn mul_vec(&self, v: &Array1<f64>) -> Array1<f64> {
1368        assert_eq!(v.len(), self.p);
1369
1370        // X v
1371        let x_v = self.x_design.matrixvectormultiply(v);
1372
1373        // X_tauᵀ (W (X v))
1374        let w_x_v = &*self.w_diag * &x_v;
1375        let term1 = self
1376            .x_tau
1377            .transpose_mul_original(&w_x_v)
1378            .expect("SparseDirectionalHyperOperator transpose product should be shape-consistent");
1379
1380        // Xᵀ (W (X_tau v))
1381        let x_tau_v = self
1382            .x_tau
1383            .forward_mul_original(v)
1384            .expect("SparseDirectionalHyperOperator forward product should be shape-consistent");
1385        let w_x_tau_v = &*self.w_diag * &x_tau_v;
1386        let term2 = self.x_design.transpose_vector_multiply(&w_x_tau_v);
1387
1388        // S_tau v
1389        let term3 = self.s_tau.dot(v);
1390
1391        let mut out = term1 + term2 + term3;
1392
1393        // Non-Gaussian fixed-beta curvature: Xᵀ diag(c ⊙ X_tau β̂) X v
1394        if let Some(c_x_tau_beta) = self.c_x_tau_beta.as_ref() {
1395            let weighted = c_x_tau_beta * &x_v;
1396            out += &self.x_design.transpose_vector_multiply(&weighted);
1397        }
1398
1399        // Firth fixed-beta partial: subtract (H_φ)_{τ}|_β v
1400        if let Some(hphi_tau_partial) = self.firth_hphi_tau_partial.as_ref() {
1401            out -= &hphi_tau_partial.dot(v);
1402        }
1403
1404        out
1405    }
1406
1407    fn is_implicit(&self) -> bool {
1408        false
1409    }
1410    fn as_any(&self) -> &(dyn std::any::Any + 'static) {
1411        self
1412    }
1413}
1414
1415/// Matrix-free GLM cubic-correction drift `C[v] = −Xᵀ diag(c ⊙ X v) X`
1416/// on the exact represented Hessian-curvature surface (sign folded into the
1417/// stored diagonal).
1418///
1419/// # Why this must stay an operator (#901 layer 2)
1420///
1421/// The spectral logdet kernel evaluates `tr(H⁺ · C)` as
1422/// `Σ_a (1/σ_a) · u_aᵀ C u_a` over the eigenpairs of `H_pen`. For a
1423/// near-null eigenvector (`σ_min ~ 1e−4` on the Duchon fixtures) the true
1424/// quadratic form is tiny — `‖X u_a‖² ≲ σ_a / w_min` — but a DENSE
1425/// materialization of `C` computes it as a cancellation across entries of
1426/// magnitude `‖C‖`, leaving roundoff `~ ε‖C‖p` that the kernel then
1427/// amplifies by `1/σ_min`. On the iso-κ Duchon binomial FD drivers this
1428/// turned a true cubic trace of `−0.30` into `+39.0`, and `~−7.7e5` on the
1429/// κ-scaled ψ arms where `‖C‖ ~ λ · ∂S/∂ψ` — the dominant #901 blow-up.
1430///
1431/// In operator form the kernel probes `C · u_a = −Xᵀ(d ⊙ (X u_a))`: the
1432/// cancellation happens inside the `X u_a` matvec (error `~ ε‖X‖‖u_a‖`),
1433/// and the quadratic form is the *square* of that already-small vector —
1434/// tiny² stays tiny, so the `1/σ_a` amplification acts on a relatively
1435/// accurate value. This is the same stability argument as evaluating
1436/// leverages via `(X u)ᵀ d (X u)` instead of `uᵀ (XᵀdX) u`.
1437pub struct GlmCurvatureCorrectionOperator {
1438    /// Design matrix X in the transformed basis (matrix-free capable).
1439    pub(crate) x_design: DesignMatrix,
1440    /// Pre-masked, sign-folded diagonal `−(c ⊙ X v)` over active rows.
1441    pub(crate) neg_c_xv: Array1<f64>,
1442    /// Total coefficient dimension.
1443    pub(crate) p: usize,
1444}
1445
1446impl HyperOperator for GlmCurvatureCorrectionOperator {
1447    fn dim(&self) -> usize {
1448        self.p
1449    }
1450
1451    fn mul_vec(&self, v: &Array1<f64>) -> Array1<f64> {
1452        assert_eq!(v.len(), self.p);
1453        let x_v = self.x_design.matrixvectormultiply(v);
1454        let weighted = &self.neg_c_xv * &x_v;
1455        self.x_design.transpose_vector_multiply(&weighted)
1456    }
1457
1458    fn as_any(&self) -> &(dyn std::any::Any + 'static) {
1459        self
1460    }
1461
1462    fn is_implicit(&self) -> bool {
1463        false
1464    }
1465}
1466
1467// ═══════════════════════════════════════════════════════════════════════════
1468//  Data structures
1469// ═══════════════════════════════════════════════════════════════════════════