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
//! Closure-backed coupling implementations for [`NonSeparableMixedChernoff`].
//!
//! Provides [`ClosureScalarCoupling`] and [`ClosureBetaCoupling`] that store
//! `Arc<dyn Fn(F, F) -> F + Send + Sync>` coupling functions. These are used
//! by the Python bindings (v2.3 Phase 4) where sampled arrays must be captured
//! by a closure — bare function pointers are insufficient.
//!
//! Free-function constructors [`with_closure_c`] and [`with_closure_beta`] wrap
//! `NonSeparableMixedChernoff::with_coupling` (the `pub(crate)` escape hatch
//! added in v2.3, ADR-0058 §"v2.3 promotions").
extern crate alloc;
use alloc::{boxed::Box, sync::Arc};
use crate::{
chernoff::ChernoffFunction,
error::SemiflowError,
float::SemiflowFloat,
grid2d::Grid2D,
grid_fn::GridFn1D,
grid_fn2d::GridFn2D,
nonseparable_mixed::{cross_stencil_scalar, MixedDerivOp, NonSeparableMixedChernoff},
};
// ---------------------------------------------------------------------------
// ClosureScalarCoupling — Arc<dyn Fn> scalar variant
// ---------------------------------------------------------------------------
/// Arc-closure-backed scalar coupling `M = c(x,y) · ∂_x∂_y`.
///
/// Unlike `ScalarCoupling` (which stores a bare `fn` pointer), this variant
/// captures sampled array data via an `Arc`-wrapped heap closure.
#[doc(hidden)]
pub struct ClosureScalarCoupling<F: SemiflowFloat> {
/// Coupling function `c(x, y)`.
c: Arc<dyn Fn(F, F) -> F + Send + Sync + 'static>,
/// Sup-norm bound on `|c|`.
c_norm_bound: f64,
/// True when `c ≡ 0` (fast-path to `Strang2D`).
is_zero: bool,
}
impl<F: SemiflowFloat> MixedDerivOp<F> for ClosureScalarCoupling<F> {
fn norm_bound(&self) -> f64 {
self.c_norm_bound
}
fn is_zero(&self) -> bool {
self.is_zero
}
fn apply_mixed_into(&self, src: &GridFn2D<F>, dst: &mut GridFn2D<F>, grid: &Grid2D<F>) {
let c_ref = Arc::clone(&self.c);
cross_stencil_scalar(src, dst, grid, move |x, y| c_ref(x, y));
}
fn clone_box(&self) -> Box<dyn MixedDerivOp<F>> {
Box::new(ClosureScalarCoupling {
c: Arc::clone(&self.c),
c_norm_bound: self.c_norm_bound,
is_zero: self.is_zero,
})
}
fn debug_label(&self) -> &'static str {
"ClosureScalarCoupling"
}
}
// ---------------------------------------------------------------------------
// ClosureBetaCoupling — Arc<dyn Fn> position-dependent variant
// ---------------------------------------------------------------------------
/// Arc-closure-backed position-dependent coupling `M_β = β(x,y) · ∂_x∂_y`.
#[doc(hidden)]
pub struct ClosureBetaCoupling<F: SemiflowFloat> {
/// Anisotropy function `β(x, y)`.
beta: Arc<dyn Fn(F, F) -> F + Send + Sync + 'static>,
/// Sup-norm bound on `|β|`.
beta_norm_bound: f64,
/// True when `β ≡ 0` (fast-path to `Strang2D`).
is_zero: bool,
}
impl<F: SemiflowFloat> MixedDerivOp<F> for ClosureBetaCoupling<F> {
fn norm_bound(&self) -> f64 {
self.beta_norm_bound
}
fn is_zero(&self) -> bool {
self.is_zero
}
fn apply_mixed_into(&self, src: &GridFn2D<F>, dst: &mut GridFn2D<F>, grid: &Grid2D<F>) {
let beta_ref = Arc::clone(&self.beta);
cross_stencil_scalar(src, dst, grid, move |x, y| beta_ref(x, y));
}
fn clone_box(&self) -> Box<dyn MixedDerivOp<F>> {
Box::new(ClosureBetaCoupling {
beta: Arc::clone(&self.beta),
beta_norm_bound: self.beta_norm_bound,
is_zero: self.is_zero,
})
}
fn debug_label(&self) -> &'static str {
"ClosureBetaCoupling"
}
}
// ---------------------------------------------------------------------------
// pub(crate) constructors
// ---------------------------------------------------------------------------
/// Build a `NonSeparableMixedChernoff` with a scalar closure coupling `c(x,y)`.
///
/// The closure is stored via `Arc` for cheap `Clone` (used by `ChernoffSemigroup`).
///
/// # Errors
/// Returns [`SemiflowError::DomainViolation`] if `c_norm_bound` is not finite or
/// is negative.
pub fn with_closure_c<X, Y, F>(
x_inner: X,
y_inner: Y,
c: Arc<dyn Fn(F, F) -> F + Send + Sync + 'static>,
c_norm_bound: f64,
grid: Grid2D<F>,
) -> Result<NonSeparableMixedChernoff<X, Y, F>, SemiflowError>
where
X: ChernoffFunction<F, S = GridFn1D<F>> + Clone,
Y: ChernoffFunction<F, S = GridFn1D<F>> + Clone,
F: SemiflowFloat,
{
validate_norm_bound(c_norm_bound, "c_norm_bound")?;
let coupling: Box<dyn MixedDerivOp<F>> = Box::new(ClosureScalarCoupling {
c,
c_norm_bound,
is_zero: c_norm_bound == 0.0,
});
Ok(NonSeparableMixedChernoff::with_coupling(
x_inner, y_inner, grid, coupling,
))
}
/// Build a `NonSeparableMixedChernoff` with an anisotropy closure `β(x,y)`.
///
/// # Errors
/// Returns [`SemiflowError::DomainViolation`] if `beta_norm_bound` is not finite
/// or is negative.
pub fn with_closure_beta<X, Y, F>(
x_inner: X,
y_inner: Y,
beta: Arc<dyn Fn(F, F) -> F + Send + Sync + 'static>,
beta_norm_bound: f64,
grid: Grid2D<F>,
) -> Result<NonSeparableMixedChernoff<X, Y, F>, SemiflowError>
where
X: ChernoffFunction<F, S = GridFn1D<F>> + Clone,
Y: ChernoffFunction<F, S = GridFn1D<F>> + Clone,
F: SemiflowFloat,
{
validate_norm_bound(beta_norm_bound, "beta_norm_bound")?;
let coupling: Box<dyn MixedDerivOp<F>> = Box::new(ClosureBetaCoupling {
beta,
beta_norm_bound,
is_zero: beta_norm_bound == 0.0,
});
Ok(NonSeparableMixedChernoff::with_coupling(
x_inner, y_inner, grid, coupling,
))
}
// ---------------------------------------------------------------------------
// Helper
// ---------------------------------------------------------------------------
/// Validate that a norm bound is finite and non-negative.
fn validate_norm_bound(val: f64, name: &'static str) -> Result<(), SemiflowError> {
if !val.is_finite() || val < 0.0 {
return Err(SemiflowError::DomainViolation {
what: name,
value: val,
});
}
Ok(())
}
#[cfg(test)]
#[allow(unused_imports)]
mod tests {
use super::*;
include!("nonseparable_mixed_closure_tests_mod.rs");
}