1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! [`Diffusion4thZeta4Chernoff`] — order-4-temporal ζ⁴ kernel (ADR-0086 Path β resolution).
//!
//! ## Mathematical foundation (ADR-0086, math.md §27 AMENDMENT)
//!
//! Path β: single-step Richardson extrapolation of [`Diffusion4thChernoff`] (K5),
//! achieving order-4 temporal convergence via cancellation of the leading O(τ³) error term:
//!
//! $$
//! F_\beta(\tau) f = \frac{4}{3} K_5(\tau/2)^2 f - \frac{1}{3} K_5(\tau) f
//! $$
//!
//! Since K5 is a symmetric (time-reversible) semigroup approximation, its global
//! error has only odd powers of τ:
//! `K5(τ)^n f = exp(tA)f + c₂τ²Ε + c₄τ⁴E' + O(τ⁶)` (n=t/τ).
//! Richardson cancels the leading `c₂τ²` term and jumps to `O(τ⁴)` global accuracy.
//!
//! This is unconditionally stable (each K5 step is contractive) and works at any N.
//! The 4-term Taylor expansion of A⊕ was explored but fails at N=512 because the
//! divergence-form stencil spectral radius (~3916) makes τ·ρ ≈ 122 at n=16, causing
//! catastrophic overflow. Richardson avoids this entirely.
//!
//! ## Algorithm (Normative)
//!
//! For each outer step of size τ, three inner K5 calls are made:
//!
//! ```text
//! coarse = K5(τ) · f // one coarse step
//! half = K5(τ/2) · f // first half-step
//! fine = K5(τ/2) · half // second half-step
//! F_β(τ) f = (4·fine − coarse) / 3 // Richardson combination
//! ```
//!
//! Work per outer step: 3× inner K5 applications (vs 1× for plain K5).
//! Temporal order: 4 (via Richardson; vs 2 for plain K5).
//!
//! ## Acceptance gates
//!
//! - **G_zeta4_const_a_richardson_ratio** (RELEASE_BLOCKING per ADR-0086 AMENDMENT 1):
//! log₂(err₄/err₈) ≥ 3.5 in constant-a regime (Richardson order-gain detector).
//! (`tests/zeta4_correction_slope.rs` `g_zeta4_const_a_richardson_ratio`, feature `slow-tests`)
//! Gate params: N_SPATIAL=512, n-pair {4,8}, T=0.5, analytic oracle. Measured ≈ 3.585.
//! *Note*: a prior −3.9 OLS gate was superseded by ADR-0086 AMENDMENT 1; no test
//! enforces −3.9. The var-a OLS gate ≤ −2.5 is ADVISORY (not BLOCKING).
//! - **G_zeta4_const_a_richardson_cheb** (RELEASE_BLOCKING per ADR-0109 + AMENDMENT 1):
//! Richardson ratio log₂(err_4/err_8) ≥ 3.1 (v5.0.0 baseline PRESERVED; SepticHermite-floor
//! invariant per math.md §40.5.bis — gate measures pre-asymp K5+Richardson temporal
//! transition regime, INDEPENDENT of the spatial floor).
//! (`tests/zeta4_correction_slope_cheb.rs`, feature `slow-tests`)
//! Note: ≈ 1.49e-12 SepticHermite-bound (ADR-0109; const-a gate measures pre-asymp-temporal
//! regime per §40.5.bis). K=4 order proven by G_zeta4_TRUTHFUL_ORDER (ADR-0110).
//! - **G_zeta4_TRUTHFUL_ORDER** (RELEASE_BLOCKING per ADR-0110 AMENDMENT 1):
//! OLS slope ≤ −3.5 in pre-asymptotic regime. T=2.0, N_STEPS={2,4,8,16},
//! Chebyshev M=64. (`tests/zeta4_truthful_order.rs`, feature `slow-tests`)
//! Demonstrates honest GLOBAL order-4 via middle-pair (4→8) clean signal.
//! AMENDMENT 1 revises original -3.95 (per-step formula model) to -3.5 (GLOBAL
//! model + OLS tolerance for boundary anomalies); ζ⁶/ζ⁸ TRUTHFUL_ORDER siblings
//! DEFERRED to v7.0+ OCTONIC per AMENDMENT 1 §"Path D".
//! - **T23N** (NORMATIVE): 4 sub-checks — Taylor coefficient, Hermite tangency,
//! rate constant, τ² coefficient verification.
//! (`scripts/verify_zeta4_correction.py`)
//!
//! ## Caller invariants
//!
//! 1. `f ∈ D(A^4)`: pre-check `kernel.in_subspace::<4>(&f)` before iterating.
//! 2. `a ∈ C^4_b`: assert via `a_kth_bound: Some(c)` at construction.
//! 3. `a(x) > 0` everywhere (strict ellipticity, inherited from inner).
//!
//! ## References
//!
//! - Galkin, Remizov (2025) *Israel J. Math.* — Theorem 3.1 (m=4 Taylor tangency).
//! - Remizov (2025) *Vladikavkaz Math. J.* 27:4 — Theorem 6 (foundational).
//! - ADR-0086 — G_zeta4 resolution via Path β (supersedes ADR-0085 deferral).
//! - math.md §27 AMENDMENT — Path β normative algorithm spec.
// Mathematical LaTeX symbols (A^k, C^4_b, etc.) are intentional; not code identifiers.
use crate::;
// ---------------------------------------------------------------------------
// Struct
// ---------------------------------------------------------------------------
/// Order-4-temporal Chernoff kernel for `∂_t u = ∂_x(a(x) ∂_x u)` (ADR-0086 Path β).
///
/// Wraps [`Diffusion4thChernoff`] (order-2 temporal, order-4 spatial) with Richardson
/// extrapolation that achieves genuine order-4 temporal convergence per
/// Galkin-Remizov 2025 *Israel J. Math.* Theorem 3.1 (m=4 specialisation).
/// Each step makes 3 inner K5 calls: `(4·K5(τ/2)²·f − K5(τ)·f)/3`.
///
/// # Constructor
///
/// ```rust
/// use semiflow::{ChernoffFunction, Diffusion4thChernoff, Diffusion4thZeta4Chernoff, Grid1D};
/// let grid = Grid1D::new(-10.0, 10.0, 512).unwrap();
/// let inner = Diffusion4thChernoff::new(
/// |x: f64| 1.0 + 0.5 * x.tanh().powi(2),
/// |x: f64| x.tanh() * (1.0 - x.tanh().powi(2)),
/// |x: f64| (1.0 - x.tanh().powi(2)).powi(2) - 2.0 * x.tanh().powi(2) * (1.0 - x.tanh().powi(2)),
/// 2.5,
/// grid,
/// );
/// let kernel = Diffusion4thZeta4Chernoff::new(inner, Some(2.5_f64)).unwrap();
/// assert_eq!(kernel.order(), 4); // v4.1: Path β achieves what v3.0 promised
/// ```
///
/// # Caller invariants
///
/// 1. `f ∈ D(A^4)`: pre-check `kernel.in_subspace::<4>(&f)` once.
/// 2. `a ∈ C^4_b`: assert via `a_kth_bound: Some(c)`.
/// 3. `a(x) > 0` everywhere (strict ellipticity).
///
/// Failure to meet invariant 3 causes a `DomainViolation` error from the inner kernel.
/// Failure to meet invariants 1 or 2 does NOT cause a panic but may degrade convergence
/// below order 4. See math.md §27 AMENDMENT.
// ---------------------------------------------------------------------------
// Constructor
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Private helper: apply the divergence-form A = ∂_x(a(x) ∂_x) stencil
// ---------------------------------------------------------------------------
/// Apply 3-point divergence-form `A = ∂_x(a(x)·∂_x)` with Neumann BCs.
///
/// `(Af)_i = [a(x_{i+½})(f_{i+1}-f_i) - a(x_{i-½})(f_i-f_{i-1})] / dx²`
///
/// Reused from `diffusion4_zeta4_data.rs` (now deleted per ADR-0086 AC4).
/// This is a `pub(crate)` helper so `ApproximationSubspace` jet impls can call it.
// n ≤ grid size; well within f64 mantissa
pub
/// Compute K-jet `[f, Af, ..., A^K f]` via K iterations of `apply_div_form`.
///
/// `out` must have length K+1. `out[0] = f` (identity).
pub
/// Validate tau: finite, non-negative (f64).
// ---------------------------------------------------------------------------
// ChernoffFunction impl
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ApproximationSubspace impl (K=4 only; K=6 retired per ADR-0086 AC9)
// ---------------------------------------------------------------------------
/// K=4 approximation subspace witness (ADR-0073, ADR-0086 §AC9).
///
/// Path β requires `D(A^4)` — the K=4 witness is necessary and sufficient.
/// The K=6 witness (`ApproximationSubspace<6, F>`) is REMOVED per ADR-0086 AC9:
/// Path β does not require the strict `D(A^6)` core. Callers who relied on the
/// K=6 witness must downgrade to K=4 (strictly more permissive — any K=4-OK datum
/// passes K=4 trivially). See `docs/migration/v3-to-v4.md` §"v4.1 G_zeta4 resolution".
///
/// `in_subspace`: true when:
/// - grid has ≥ 17 points (4-iteration 9-point stencil minimum),
/// - all values are finite,
/// - `a_kth_bound` is `Some(_)` (caller-asserted `a ∈ C^4_b`).
// ---------------------------------------------------------------------------
// Unit tests (fast, no feature gate) — extracted to sibling file per ≤500-line cap.
// ---------------------------------------------------------------------------