1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//! Sixth-order Magnus expansion for time-dependent graph heat:
//! `∂_t u = −L_G(t) u` on a fixed-topology weighted graph.
//!
//! Three-point Gauss-Legendre quadrature (GL₆) + **2-commutator BCOR form**
//! (Blanes-Casas-Oteo-Ros 2009, canonical order-6 Magnus integrator):
//!
//! ```text
//! B₁ = τ · A₂
//! B₂ = τ · (√15/3) · (A₃ − A₁)
//! B₃ = τ · (10/3) · (A₃ − 2·A₂ + A₁)
//!
//! C₁ = [B₁, B₂]
//! C₂ = −(1/60) · [B₁, 2·B₃ + C₁]
//!
//! Ω₆(τ) = B₁ + (1/12)·B₃ + (1/240)·[−20·B₁ − B₃ + C₁ , B₂ + C₂]
//! ```
//!
//! Local truncation error O(τ⁷); global order 6 on non-commuting A(t).
//! Only 2 commutator evaluations; cheaper than the old 4-commutator form.
//!
//! `A_i = −L_G(t₀ + c_i·τ)` at GL₆ abscissae:
//! `c₁ = (5−√15)/10, c₂ = 1/2, c₃ = (5+√15)/10`.
//!
//! **f64 ONLY** — `impl ChernoffFunction<f32>` is intentionally absent.
//! See ADR-0056 §"f32 instability rationale".
//!
//! # References
//!
//! - S. Blanes, F. Casas, J. A. Oteo, J. Ros, *Phys. Rep.* **470** (2009)
//! §4 and Table 5 — canonical 2-commutator order-6 Magnus method.
//! - A. Iserles, H. Z. Munthe-Kaas, S. P. Nørsett, A. Zanna, *Acta Numerica*
//! **9** (2000) §6, Table III — GL₆ quadrature abscissae and weights.
//! - M. Hochbruck, A. Ostermann, *Acta Numerica* **19** (2010) §3.
//!
//! See `contracts/semiflow-core.math.md` §16 (NORMATIVE) and ADR-0056, ADR-0114.
//!
//! # Zero-alloc steady state
//!
//! `apply_into` acquires **22 scratch buffers** via `ScratchPool::take_vec`
//! and returns them all before returning (R4 zero-alloc invariant, ADR-0114).
use alloc::sync::Arc;
use crate::{
chernoff::{ChernoffFunction, Growth},
error::SemiflowError,
float::{from_f64, SemiflowFloat},
graph::{Graph, Laplacian},
graph_signal::GraphSignal,
magnus_graph::LaplacianAtTime,
scratch::ScratchPool,
state::State,
};
#[path = "magnus6_graph_helpers.rs"]
mod helpers;
pub(crate) use helpers::apply_exp_omega6_kernel;
// ---------------------------------------------------------------------------
// GL₆ constants (NORMATIVE — DO NOT CHANGE; see contracts §16.1)
// ---------------------------------------------------------------------------
/// GL₆ first abscissa: `c₁ = (5 − √15) / 10 ≈ 0.11270166537925831`.
///
/// Source: Iserles+ 2000 *Acta Numerica* Table III; Blanes+ 2009 Table 6.
pub const GL6_C1: f64 = 0.112_701_665_379_258_31;
/// GL₆ second abscissa: `c₂ = 1/2`.
pub const GL6_C2: f64 = 0.5;
/// GL₆ third abscissa: `c₃ = (5 + √15) / 10 ≈ 0.88729833462074169`.
#[allow(clippy::excessive_precision)]
pub const GL6_C3: f64 = 0.887_298_334_620_741_7;
/// GL₆ first weight: `b₁ = 5/18`.
pub const GL6_B1: f64 = 5.0 / 18.0;
/// GL₆ second weight: `b₂ = 8/18`.
pub const GL6_B2: f64 = 8.0 / 18.0;
/// GL₆ third weight: `b₃ = 5/18`.
pub const GL6_B3: f64 = 5.0 / 18.0;
// ---------------------------------------------------------------------------
// BCOR-6 operator constants (NORMATIVE — ADR-0114 / math.md §16.2)
// ---------------------------------------------------------------------------
/// `√15 / 3` — scale for B₂ = τ·(√15/3)·(A₃ − A₁).
#[allow(clippy::excessive_precision)]
pub(crate) const SQRT15_OVER_3: f64 = 1.290_994_448_735_805_6;
/// `10/3` — scale for B₃ = τ·(10/3)·(A₃ − 2·A₂ + A₁).
pub(crate) const TEN_OVER_3: f64 = 10.0 / 3.0;
/// `1/12` — coefficient of B₃ in Ω₆.
pub(crate) const ONE_OVER_12: f64 = 1.0 / 12.0;
/// `1/60` — coefficient in C₂ = −(1/60)·[B₁, 2·B₃ + C₁].
pub(crate) const ONE_OVER_60: f64 = 1.0 / 60.0;
/// `1/240` — outer bracket coefficient in Ω₆.
pub(crate) const ONE_OVER_240: f64 = 1.0 / 240.0;
// ---------------------------------------------------------------------------
// MagnusGraphHeat6thChernoff<F>
// ---------------------------------------------------------------------------
/// Order-6 Magnus Chernoff for `∂_t u = −L_G(t) u` on a fixed-topology
/// weighted graph with time-varying edge weights.
///
/// Uses the canonical Blanes-Casas-Oteo-Ros 2-commutator form (BCOR-6,
/// Phys. Rep. 470 2009 §4) with GL₆ quadrature. Local truncation error
/// O(τ⁷); global order 6 on genuinely non-commuting `L_G(t)`.
///
/// **f64 ONLY** — `impl ChernoffFunction<f32>` intentionally absent (ADR-0056).
pub struct MagnusGraphHeat6thChernoff<F: SemiflowFloat = f64> {
/// Fixed-topology graph.
graph: Arc<Graph<F>>,
/// `t ↦ Arc<Laplacian<F>>` — caller-supplied sampler.
lap_at_t: LaplacianAtTime<F>,
/// Gershgorin radius bound `ρ̄_max`.
pub(crate) rho_bar_max: F,
/// If `true`, each `apply_into` checks `ρ̄_max · τ < π/2`.
pub(crate) convergence_radius_check: bool,
}
impl<F: SemiflowFloat> MagnusGraphHeat6thChernoff<F> {
/// Construct from topology + time-to-Laplacian closure.
///
/// # Errors
///
/// [`SemiflowError::DomainViolation`] if `rho_bar_max` is not finite and
/// strictly positive, or if `graph.n_nodes() == 0`.
pub fn new(
graph: Arc<Graph<F>>,
lap_at_t: LaplacianAtTime<F>,
rho_bar_max: F,
convergence_radius_check: bool,
) -> Result<Self, SemiflowError> {
validate_rho(rho_bar_max)?;
if graph.n_nodes() == 0 {
return Err(SemiflowError::DomainViolation {
what: "MagnusGraphHeat6thChernoff: graph must have at least one node",
value: 0.0,
});
}
Ok(Self {
graph,
lap_at_t,
rho_bar_max,
convergence_radius_check,
})
}
/// Borrow the topology graph.
#[must_use]
pub fn graph(&self) -> &Graph<F> {
&self.graph
}
/// Return the Laplacian sampled at time `t` (clones the `Arc`).
#[must_use]
pub fn laplacian_at(&self, t: F) -> Arc<Laplacian<F>> {
(self.lap_at_t)(t)
}
/// Apply one Magnus K=6 step starting at absolute time `t_start`.
///
/// GL₆ nodes are `t_start + c_i · τ` — correct for time-varying generators.
///
/// # Errors
///
/// Same as [`ChernoffFunction::apply_into`].
pub fn apply_into_at(
&self,
t_start: F,
tau: F,
src: &GraphSignal<F>,
dst: &mut GraphSignal<F>,
scratch: &mut ScratchPool<F>,
) -> Result<(), SemiflowError>
where
GraphSignal<F>: Clone,
{
apply_magnus_k6_into_at(self, t_start, tau, src, dst, scratch)
}
}
// ---------------------------------------------------------------------------
// ChernoffFunction<f64> impl — f64 ONLY (no f32 impl; see ADR-0056)
// ---------------------------------------------------------------------------
/// **f64 ONLY** — building `MagnusGraphHeat6thChernoff::<f32>` as a
/// `ChernoffFunction<f32>` is intentionally not supported (ADR-0056).
impl ChernoffFunction<f64> for MagnusGraphHeat6thChernoff<f64> {
type S = GraphSignal<f64>;
/// Apply one Magnus K=6 step from `t_start = 0`.
///
/// **WARNING**: hardcodes `t_start = 0` — for time-varying `L(t)` call
/// `apply_into_at` instead (see ADR-0114).
fn apply_into(
&self,
tau: f64,
src: &GraphSignal<f64>,
dst: &mut GraphSignal<f64>,
scratch: &mut ScratchPool<f64>,
) -> Result<(), SemiflowError> {
apply_magnus_k6_into_at(self, 0.0_f64, tau, src, dst, scratch)
}
fn order(&self) -> u32 {
6
}
fn growth(&self) -> Growth<f64> {
Growth {
multiplier: 1.0_f64,
omega: self.rho_bar_max,
}
}
}
// ---------------------------------------------------------------------------
// Validation helpers
// ---------------------------------------------------------------------------
fn validate_rho<F: SemiflowFloat>(rho: F) -> Result<(), SemiflowError> {
let v = rho.to_f64().unwrap_or(f64::NAN);
if !v.is_finite() || v <= 0.0 {
return Err(SemiflowError::DomainViolation {
what: "MagnusGraphHeat6thChernoff: rho_bar_max must be finite and > 0",
value: v,
});
}
Ok(())
}
fn validate_tau<F: SemiflowFloat>(tau: F) -> Result<(), SemiflowError> {
let v = tau.to_f64().unwrap_or(f64::NAN);
if !v.is_finite() || v < 0.0 {
return Err(SemiflowError::DomainViolation {
what: "MagnusGraphHeat6thChernoff: tau must be finite and >= 0",
value: v,
});
}
Ok(())
}
fn validate_magnus_radius<F: SemiflowFloat>(rho_bar_max: F, tau: F) -> Result<(), SemiflowError> {
let radius = rho_bar_max * tau;
let half_pi = from_f64::<F>(core::f64::consts::FRAC_PI_2);
if radius >= half_pi {
return Err(SemiflowError::OutOfMagnusRadius {
tau: tau.to_f64().unwrap_or(f64::NAN),
rho_estimate: rho_bar_max.to_f64().unwrap_or(f64::NAN),
});
}
Ok(())
}
// ---------------------------------------------------------------------------
// Core kernel
// ---------------------------------------------------------------------------
/// Apply `exp(Ω₆(t_start, τ)) · src` into `dst`.
fn apply_magnus_k6_into_at<F: SemiflowFloat>(
mc: &MagnusGraphHeat6thChernoff<F>,
t_start: F,
tau: F,
src: &GraphSignal<F>,
dst: &mut GraphSignal<F>,
scratch: &mut ScratchPool<F>,
) -> Result<(), SemiflowError> {
validate_tau(tau)?;
if mc.convergence_radius_check {
validate_magnus_radius(mc.rho_bar_max, tau)?;
}
let n = src.len();
debug_assert_eq!(dst.len(), n, "apply_magnus_k6_into_at: dst len mismatch");
let c1 = from_f64::<F>(GL6_C1);
let c2 = from_f64::<F>(GL6_C2);
let c3 = from_f64::<F>(GL6_C3);
let lap1 = (mc.lap_at_t)(t_start + c1 * tau);
let lap2 = (mc.lap_at_t)(t_start + c2 * tau);
let lap3 = (mc.lap_at_t)(t_start + c3 * tau);
#[cfg(debug_assertions)]
{
debug_assert_eq!(
lap1.n_nodes(),
mc.graph.n_nodes(),
"MagnusGraphHeat6thChernoff: topology drift at c1"
);
debug_assert_eq!(
lap2.n_nodes(),
mc.graph.n_nodes(),
"MagnusGraphHeat6thChernoff: topology drift at c2"
);
debug_assert_eq!(
lap3.n_nodes(),
mc.graph.n_nodes(),
"MagnusGraphHeat6thChernoff: topology drift at c3"
);
}
helpers::apply_exp_omega6_kernel(&lap1, &lap2, &lap3, tau, src, dst, scratch);
Ok(())
}
// ---------------------------------------------------------------------------
// Unit smoke tests
// ---------------------------------------------------------------------------
#[cfg(test)]
#[path = "magnus6_graph_tests.rs"]
mod tests;