Sat, 01 Feb 2025 16:47:11 -0500
Parameter adjustments
| 35 | 1 | /*! |
| 2 | Sensor grid forward model | |
| 3 | */ | |
| 4 | ||
| 5 | use numeric_literals::replace_float_literals; | |
| 6 | use nalgebra::base::{ | |
| 7 | DMatrix, | |
| 8 | DVector | |
| 9 | }; | |
| 10 | use std::iter::Zip; | |
| 11 | use std::ops::RangeFrom; | |
| 12 | ||
| 13 | pub use alg_tools::linops::*; | |
| 14 | use alg_tools::norms::{ | |
| 15 | L1, Linfinity, L2, Norm | |
| 16 | }; | |
| 17 | use alg_tools::bisection_tree::*; | |
| 18 | use alg_tools::mapping::{ | |
| 19 | RealMapping, | |
| 20 | DifferentiableMapping | |
| 21 | }; | |
| 22 | use alg_tools::lingrid::*; | |
| 23 | use alg_tools::iter::{MapX, Mappable}; | |
| 24 | use alg_tools::nalgebra_support::ToNalgebraRealField; | |
| 25 | use alg_tools::tabledump::write_csv; | |
| 26 | use alg_tools::error::DynError; | |
| 27 | use alg_tools::maputil::map2; | |
| 28 | use alg_tools::instance::Instance; | |
| 29 | ||
| 30 | use crate::types::*; | |
| 31 | use crate::measures::{DiscreteMeasure, Radon}; | |
| 32 | use crate::seminorms::{ | |
| 33 | ConvolutionOp, | |
| 34 | SimpleConvolutionKernel, | |
| 35 | }; | |
| 36 | use crate::kernels::{ | |
| 37 | Convolution, | |
| 38 | AutoConvolution, | |
| 39 | BoundedBy, | |
| 40 | }; | |
| 41 | use crate::preadjoint_helper::PreadjointHelper; | |
| 42 | use super::{ | |
| 43 | ForwardModel, | |
| 44 | 44 | BoundedCurvature, |
| 35 | 45 | AdjointProductBoundedBy |
| 46 | }; | |
| 47 | use crate::frank_wolfe::FindimQuadraticModel; | |
| 48 | ||
| 49 | type RNDM<F, const N : usize> = DiscreteMeasure<Loc<F,N>, F>; | |
| 50 | ||
| 51 | pub type ShiftedSensor<F, S, P, const N : usize> = Shift<Convolution<S, P>, F, N>; | |
| 52 | ||
| 53 | /// Trait for physical convolution models. Has blanket implementation for all cases. | |
| 54 | pub trait Spread<F : Float, const N : usize> | |
| 55 | : 'static + Clone + Support<F, N> + RealMapping<F, N> + Bounded<F> {} | |
| 56 | ||
| 57 | impl<F, T, const N : usize> Spread<F, N> for T | |
| 58 | where F : Float, | |
| 59 | T : 'static + Clone + Support<F, N> + Bounded<F> + RealMapping<F, N> {} | |
| 60 | ||
| 61 | /// Trait for compactly supported sensors. Has blanket implementation for all cases. | |
| 62 | pub trait Sensor<F : Float, const N : usize> : Spread<F, N> + Norm<F, L1> + Norm<F, Linfinity> {} | |
| 63 | ||
| 64 | impl<F, T, const N : usize> Sensor<F, N> for T | |
| 65 | where F : Float, | |
| 66 | T : Spread<F, N> + Norm<F, L1> + Norm<F, Linfinity> {} | |
| 67 | ||
| 68 | ||
| 69 | pub trait SensorGridBT<F, S, P, const N : usize> : | |
| 70 | Clone + BTImpl<F, N, Data=usize, Agg=Bounds<F>> | |
| 71 | where F : Float, | |
| 72 | S : Sensor<F, N>, | |
| 73 | P : Spread<F, N> {} | |
| 74 | ||
| 75 | impl<F, S, P, T, const N : usize> | |
| 76 | SensorGridBT<F, S, P, N> | |
| 77 | for T | |
| 78 | where T : Clone + BTImpl<F, N, Data=usize, Agg=Bounds<F>>, | |
| 79 | F : Float, | |
| 80 | S : Sensor<F, N>, | |
| 81 | P : Spread<F, N> {} | |
| 82 | ||
| 83 | // We need type alias bounds to access associated types | |
| 84 | #[allow(type_alias_bounds)] | |
| 85 | pub type SensorGridBTFN<F, S, P, BT : SensorGridBT<F, S, P, N>, const N : usize> | |
| 86 | = BTFN<F, SensorGridSupportGenerator<F, S, P, N>, BT, N>; | |
| 87 | ||
| 88 | /// Sensor grid forward model | |
| 89 | #[derive(Clone)] | |
| 90 | pub struct SensorGrid<F, S, P, BT, const N : usize> | |
| 91 | where F : Float, | |
| 92 | S : Sensor<F, N>, | |
| 93 | P : Spread<F, N>, | |
| 94 | Convolution<S, P> : Spread<F, N>, | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
95 | BT : SensorGridBT<F, S, P, N>, |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
96 | { |
| 35 | 97 | domain : Cube<F, N>, |
| 98 | sensor_count : [usize; N], | |
| 99 | sensor : S, | |
| 100 | spread : P, | |
| 101 | base_sensor : Convolution<S, P>, | |
| 102 | bt : BT, | |
| 103 | } | |
| 104 | ||
| 105 | impl<F, S, P, BT, const N : usize> SensorGrid<F, S, P, BT, N> | |
| 106 | where F : Float, | |
| 107 | BT : SensorGridBT<F, S, P, N>, | |
| 108 | S : Sensor<F, N>, | |
| 109 | P : Spread<F, N>, | |
| 110 | Convolution<S, P> : Spread<F, N> + LocalAnalysis<F, BT::Agg, N>, | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
111 | { |
| 35 | 112 | |
| 113 | /// Create a new sensor grid. | |
| 114 | /// | |
| 115 | /// The parameter `depth` indicates the search depth of the created [`BT`]s | |
| 116 | /// for the adjoint values. | |
| 117 | pub fn new( | |
| 118 | domain : Cube<F, N>, | |
| 119 | sensor_count : [usize; N], | |
| 120 | sensor : S, | |
| 121 | spread : P, | |
| 122 | depth : BT::Depth | |
| 123 | ) -> Self { | |
| 124 | let base_sensor = Convolution(sensor.clone(), spread.clone()); | |
| 125 | let bt = BT::new(domain, depth); | |
| 126 | let mut sensorgrid = SensorGrid { | |
| 127 | domain, | |
| 128 | sensor_count, | |
| 129 | sensor, | |
| 130 | spread, | |
| 131 | base_sensor, | |
| 132 | bt, | |
| 133 | }; | |
| 134 | ||
| 135 | for (x, id) in sensorgrid.grid().into_iter().zip(0usize..) { | |
| 136 | let s = sensorgrid.shifted_sensor(x); | |
| 137 | sensorgrid.bt.insert(id, &s); | |
| 138 | } | |
| 139 | ||
| 140 | sensorgrid | |
| 141 | } | |
| 142 | } | |
| 143 | ||
| 144 | ||
| 145 | impl<F, S, P, BT, const N : usize> SensorGrid<F, S, P, BT, N> | |
| 146 | where F : Float, | |
| 147 | BT : SensorGridBT<F, S, P, N>, | |
| 148 | S : Sensor<F, N>, | |
| 149 | P : Spread<F, N>, | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
150 | Convolution<S, P> : Spread<F, N> |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
151 | { |
| 35 | 152 | |
| 153 | /// Return the grid of sensor locations. | |
| 154 | pub fn grid(&self) -> LinGrid<F, N> { | |
| 155 | lingrid_centered(&self.domain, &self.sensor_count) | |
| 156 | } | |
| 157 | ||
| 158 | /// Returns the number of sensors (number of grid points) | |
| 159 | pub fn n_sensors(&self) -> usize { | |
| 160 | self.sensor_count.iter().product() | |
| 161 | } | |
| 162 | ||
| 163 | /// Constructs a sensor shifted by `x`. | |
| 164 | #[inline] | |
| 165 | fn shifted_sensor(&self, x : Loc<F, N>) -> ShiftedSensor<F, S, P, N> { | |
| 166 | self.base_sensor.clone().shift(x) | |
| 167 | } | |
| 168 | ||
| 169 | #[inline] | |
| 170 | fn _zero_observable(&self) -> DVector<F> { | |
| 171 | DVector::zeros(self.n_sensors()) | |
| 172 | } | |
| 173 | ||
| 174 | /// Returns the maximum number of overlapping sensors $N_\psi$. | |
| 175 | pub fn max_overlapping(&self) -> F { | |
| 176 | let w = self.base_sensor.support_hint().width(); | |
| 177 | let d = map2(self.domain.width(), &self.sensor_count, |wi, &i| wi/F::cast_from(i)); | |
| 178 | w.iter() | |
| 179 | .zip(d.iter()) | |
| 180 | .map(|(&wi, &di)| (wi/di).ceil()) | |
| 181 | .reduce(F::mul) | |
| 182 | .unwrap() | |
| 183 | } | |
| 184 | } | |
| 185 | ||
| 186 | impl<F, S, P, BT, const N : usize> Mapping<RNDM<F, N>> for SensorGrid<F, S, P, BT, N> | |
| 187 | where | |
| 188 | F : Float, | |
| 189 | BT : SensorGridBT<F, S, P, N>, | |
| 190 | S : Sensor<F, N>, | |
| 191 | P : Spread<F, N>, | |
| 192 | Convolution<S, P> : Spread<F, N>, | |
| 193 | { | |
| 194 | ||
| 195 | type Codomain = DVector<F>; | |
| 196 | ||
| 197 | #[inline] | |
| 198 | fn apply<I : Instance<RNDM<F, N>>>(&self, μ : I) -> DVector<F> { | |
| 199 | let mut y = self._zero_observable(); | |
| 200 | self.apply_add(&mut y, μ); | |
| 201 | y | |
| 202 | } | |
| 203 | } | |
| 204 | ||
| 205 | ||
| 206 | impl<F, S, P, BT, const N : usize> Linear<RNDM<F, N>> for SensorGrid<F, S, P, BT, N> | |
| 207 | where | |
| 208 | F : Float, | |
| 209 | BT : SensorGridBT<F, S, P, N>, | |
| 210 | S : Sensor<F, N>, | |
| 211 | P : Spread<F, N>, | |
| 212 | Convolution<S, P> : Spread<F, N>, | |
| 213 | { } | |
| 214 | ||
| 215 | ||
| 216 | #[replace_float_literals(F::cast_from(literal))] | |
| 217 | impl<F, S, P, BT, const N : usize> GEMV<F, RNDM<F, N>, DVector<F>> for SensorGrid<F, S, P, BT, N> | |
| 218 | where F : Float, | |
| 219 | BT : SensorGridBT<F, S, P, N>, | |
| 220 | S : Sensor<F, N>, | |
| 221 | P : Spread<F, N>, | |
| 222 | Convolution<S, P> : Spread<F, N>, | |
| 223 | { | |
| 224 | ||
| 225 | fn gemv<I : Instance<RNDM<F, N>>>( | |
| 226 | &self, y : &mut DVector<F>, α : F, μ : I, β : F | |
| 227 | ) { | |
| 228 | let grid = self.grid(); | |
| 229 | if β == 0.0 { | |
| 230 | y.fill(0.0) | |
| 231 | } else if β != 1.0 { | |
| 232 | *y *= β; // Need to multiply first, as we have to be able to add to y. | |
| 233 | } | |
| 234 | if α == 1.0 { | |
| 235 | self.apply_add(y, μ) | |
| 236 | } else { | |
| 237 | for δ in μ.ref_instance() { | |
| 238 | for &d in self.bt.iter_at(&δ.x) { | |
| 239 | let sensor = self.shifted_sensor(grid.entry_linear_unchecked(d)); | |
| 240 | y[d] += sensor.apply(&δ.x) * (α * δ.α); | |
| 241 | } | |
| 242 | } | |
| 243 | } | |
| 244 | } | |
| 245 | ||
| 246 | fn apply_add<I : Instance<RNDM<F, N>>>( | |
| 247 | &self, y : &mut DVector<F>, μ : I | |
| 248 | ) { | |
| 249 | let grid = self.grid(); | |
| 250 | for δ in μ.ref_instance() { | |
| 251 | for &d in self.bt.iter_at(&δ.x) { | |
| 252 | let sensor = self.shifted_sensor(grid.entry_linear_unchecked(d)); | |
| 253 | y[d] += sensor.apply(&δ.x) * δ.α; | |
| 254 | } | |
| 255 | } | |
| 256 | } | |
| 257 | ||
| 258 | } | |
| 259 | ||
| 260 | ||
| 261 | impl<F, S, P, BT, const N : usize> | |
| 262 | BoundedLinear<RNDM<F, N>, Radon, L2, F> | |
| 263 | for SensorGrid<F, S, P, BT, N> | |
| 264 | where F : Float, | |
| 265 | BT : SensorGridBT<F, S, P, N, Agg=Bounds<F>>, | |
| 266 | S : Sensor<F, N>, | |
| 267 | P : Spread<F, N>, | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
268 | Convolution<S, P> : Spread<F, N> + LocalAnalysis<F, BT::Agg, N> |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
269 | { |
| 35 | 270 | |
| 271 | /// An estimate on the operator norm in $𝕃(ℳ(Ω); ℝ^n)$ with $ℳ(Ω)$ equipped | |
| 272 | /// with the Radon norm, and $ℝ^n$ with the Euclidean norm. | |
| 273 | fn opnorm_bound(&self, _ : Radon, _ : L2) -> F { | |
| 274 | // With {x_i}_{i=1}^n the grid centres and φ the kernel, we have | |
| 275 | // |Aμ|_2 = sup_{|z|_2 ≤ 1} ⟨z,Αμ⟩ = sup_{|z|_2 ≤ 1} ⟨A^*z|μ⟩ | |
| 276 | // ≤ sup_{|z|_2 ≤ 1} |A^*z|_∞ |μ|_ℳ | |
| 277 | // = sup_{|z|_2 ≤ 1} |∑ φ(· - x_i)z_i|_∞ |μ|_ℳ | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
278 | // ≤ sup_{|z|_2 ≤ 1} |φ(y)| ∑_{i:th sensor active at y}|z_i| |μ|_ℳ |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
279 | // where the supremum of |∑ φ(· - x_i)z_i|_∞ is reached at y |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
280 | // ≤ sup_{|z|_2 ≤ 1} |φ|_∞ √N_ψ |z|_2 |μ|_ℳ |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
281 | // where N_ψ is the maximum number of sensors that overlap, and |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
282 | // |z|_2 is restricted to the active sensors. |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
283 | // = |φ|_∞ √N_ψ |μ|_ℳ. |
| 35 | 284 | // Hence |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
285 | let n = self.max_overlapping(); |
| 35 | 286 | self.base_sensor.bounds().uniform() * n.sqrt() |
| 287 | } | |
| 288 | } | |
| 289 | ||
| 290 | type SensorGridPreadjoint<'a, A, F, const N : usize> = PreadjointHelper<'a, A, RNDM<F,N>>; | |
| 291 | ||
| 292 | ||
| 293 | impl<F, S, P, BT, const N : usize> | |
| 294 | Preadjointable<RNDM<F, N>, DVector<F>> | |
| 295 | for SensorGrid<F, S, P, BT, N> | |
| 296 | where F : Float, | |
| 297 | BT : SensorGridBT<F, S, P, N>, | |
| 298 | S : Sensor<F, N>, | |
| 299 | P : Spread<F, N>, | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
300 | Convolution<S, P> : Spread<F, N> + LocalAnalysis<F, BT::Agg, N> |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
301 | { |
| 35 | 302 | type PreadjointCodomain = BTFN<F, SensorGridSupportGenerator<F, S, P, N>, BT, N>; |
| 303 | type Preadjoint<'a> = SensorGridPreadjoint<'a, Self, F, N> where Self : 'a; | |
| 304 | ||
| 305 | fn preadjoint(&self) -> Self::Preadjoint<'_> { | |
| 306 | PreadjointHelper::new(self) | |
| 307 | } | |
| 308 | } | |
| 309 | ||
| 44 | 310 | /* |
| 35 | 311 | #[replace_float_literals(F::cast_from(literal))] |
| 312 | impl<'a, F, S, P, BT, const N : usize> LipschitzValues | |
| 313 | for SensorGridPreadjoint<'a, SensorGrid<F, S, P, BT, N>, F, N> | |
| 314 | where F : Float, | |
| 315 | BT : SensorGridBT<F, S, P, N>, | |
| 316 | S : Sensor<F, N>, | |
| 317 | P : Spread<F, N>, | |
| 318 | Convolution<S, P> : Spread<F, N> + Lipschitz<L2, FloatType=F> + DifferentiableMapping<Loc<F,N>> + LocalAnalysis<F, BT::Agg, N>, | |
| 319 | for<'b> <Convolution<S, P> as DifferentiableMapping<Loc<F,N>>>::Differential<'b> : Lipschitz<L2, FloatType=F>, | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
320 | { |
| 35 | 321 | |
| 322 | type FloatType = F; | |
| 323 | ||
| 324 | fn value_unit_lipschitz_factor(&self) -> Option<F> { | |
| 325 | // The Lipschitz factor of the sensors has to be scaled by the square root of twice | |
| 326 | // the number of overlapping sensors at a single ponit, as Lipschitz estimates involve | |
| 327 | // two points. | |
| 328 | let fw = self.forward_op; | |
| 329 | let n = fw.max_overlapping(); | |
| 330 | fw.base_sensor.lipschitz_factor(L2).map(|l| (2.0 * n).sqrt() * l) | |
| 331 | } | |
| 332 | ||
| 333 | fn value_diff_unit_lipschitz_factor(&self) -> Option<F> { | |
| 334 | // The Lipschitz factor of the sensors has to be scaled by the square root of twice | |
| 335 | // the number of overlapping sensors at a single ponit, as Lipschitz estimates involve | |
| 336 | // two points. | |
| 337 | let fw = self.forward_op; | |
| 338 | let n = fw.max_overlapping(); | |
| 339 | fw.base_sensor.diff_ref().lipschitz_factor(L2).map(|l| (2.0 * n).sqrt() * l) | |
| 340 | } | |
| 341 | } | |
| 44 | 342 | */ |
| 343 | ||
| 344 | #[replace_float_literals(F::cast_from(literal))] | |
| 345 | impl<'a, F, S, P, BT, const N : usize> BoundedCurvature | |
| 346 | for SensorGrid<F, S, P, BT, N> | |
| 347 | where F : Float, | |
| 348 | BT : SensorGridBT<F, S, P, N>, | |
| 349 | S : Sensor<F, N>, | |
| 350 | P : Spread<F, N>, | |
| 351 | Convolution<S, P> : Spread<F, N> + Lipschitz<L2, FloatType=F> + DifferentiableMapping<Loc<F,N>> + LocalAnalysis<F, BT::Agg, N>, | |
| 352 | for<'b> <Convolution<S, P> as DifferentiableMapping<Loc<F,N>>>::Differential<'b> : Lipschitz<L2, FloatType=F>, | |
| 353 | { | |
| 354 | ||
| 355 | type FloatType = F; | |
| 356 | ||
| 357 | /// Returns a bound $ℓ_F$ on the curvature | |
| 358 | /// $$ | |
| 359 | /// 𝒦_F(μ, γ) = ∫ B_{F'(μ)} dγ + B_F(μ, μ+Δ). | |
| 360 | /// $$ | |
| 361 | /// such that $𝒦_F(μ, γ) ≤ ℓ_F ∫ c_2 d|γ|$. | |
| 362 | /// | |
| 363 | /// For $F(μ)=(1/2)‖Aμ-b‖^2$, we have $B_F(μ, μ+Δ)=(1/2)‖AΔ‖^2$, where $Δ = (π_♯^1-π_♯^0)γ$. | |
| 364 | /// So we use Lemma 3.8 for that, bounding | |
| 365 | /// $(1/2)‖AΔ‖^2 ≤ Θ ∫ c_2 dγ$ for $Θ=2N_ψML_ψ^2$, where | |
| 366 | /// * $L_ψ$ is the 2-norm Lipschitz factor of $ψ$ (sensor * base_spread), and | |
| 367 | /// * $N_ψ$ the maximum overlap, | |
| 368 | /// * M is a bound on $|γ|(Ω^2)$. | |
| 369 | /// | |
| 370 | /// We also have $B_{F'(μ)}(x, y) = v(y) - v(x) ⟨∇v(x), x-y⟩$ for $v(x)=A^*(Aμ-b)$. | |
| 371 | /// This we want the Lipschitz factor of $∇v$. | |
| 372 | /// By Example 4.15, it makes sense to estimate this by $√(2N_ψ)L_{∇ψ}‖b‖$, where | |
| 373 | /// $L_{∇ψ}$ is the Lipshitz factor of $∇ψ$. | |
| 374 | fn curvature_bound_components(&self) -> (Option<Self::FloatType>, Option<Self::FloatType>) { | |
| 375 | let n_ψ = self.max_overlapping(); | |
| 376 | let ψ_diff_lip = self.base_sensor.diff_ref().lipschitz_factor(L2); | |
| 377 | let ψ_lip = self.base_sensor.lipschitz_factor(L2); | |
| 378 | let a = ψ_diff_lip.map(|l| (2.0 * n_ψ).sqrt() * l); | |
| 379 | let b = ψ_lip.map(|l| 2.0 * n_ψ * l.powi(2)); | |
| 380 | ||
| 381 | (a, b) | |
| 382 | } | |
| 383 | } | |
| 384 | ||
| 385 | ||
| 35 | 386 | |
| 387 | #[derive(Clone,Debug)] | |
| 388 | pub struct SensorGridSupportGenerator<F, S, P, const N : usize> | |
| 389 | where F : Float, | |
| 390 | S : Sensor<F, N>, | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
391 | P : Spread<F, N> |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
392 | { |
| 35 | 393 | base_sensor : Convolution<S, P>, |
| 394 | grid : LinGrid<F, N>, | |
| 395 | weights : DVector<F> | |
| 396 | } | |
| 397 | ||
| 398 | impl<F, S, P, const N : usize> SensorGridSupportGenerator<F, S, P, N> | |
| 399 | where F : Float, | |
| 400 | S : Sensor<F, N>, | |
| 401 | P : Spread<F, N>, | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
402 | Convolution<S, P> : Spread<F, N> |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
403 | { |
| 35 | 404 | |
| 405 | #[inline] | |
| 406 | fn construct_sensor(&self, id : usize, w : F) -> Weighted<ShiftedSensor<F, S, P, N>, F> { | |
| 407 | let x = self.grid.entry_linear_unchecked(id); | |
| 408 | self.base_sensor.clone().shift(x).weigh(w) | |
| 409 | } | |
| 410 | ||
| 411 | #[inline] | |
| 412 | fn construct_sensor_and_id<'a>(&'a self, (id, w) : (usize, &'a F)) | |
| 413 | -> (usize, Weighted<ShiftedSensor<F, S, P, N>, F>) { | |
| 414 | (id.into(), self.construct_sensor(id, *w)) | |
| 415 | } | |
| 416 | } | |
| 417 | ||
| 418 | impl<F, S, P, const N : usize> SupportGenerator<F, N> | |
| 419 | for SensorGridSupportGenerator<F, S, P, N> | |
| 420 | where F : Float, | |
| 421 | S : Sensor<F, N>, | |
| 422 | P : Spread<F, N>, | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
423 | Convolution<S, P> : Spread<F, N> |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
424 | { |
| 35 | 425 | type Id = usize; |
| 426 | type SupportType = Weighted<ShiftedSensor<F, S, P, N>, F>; | |
| 427 | type AllDataIter<'a> = MapX<'a, Zip<RangeFrom<usize>, | |
| 428 | std::slice::Iter<'a, F>>, | |
| 429 | Self, | |
| 430 | (Self::Id, Self::SupportType)> | |
| 431 | where Self : 'a; | |
| 432 | ||
| 433 | #[inline] | |
| 434 | fn support_for(&self, d : Self::Id) -> Self::SupportType { | |
| 435 | self.construct_sensor(d, self.weights[d]) | |
| 436 | } | |
| 437 | ||
| 438 | #[inline] | |
| 439 | fn support_count(&self) -> usize { | |
| 440 | self.weights.len() | |
| 441 | } | |
| 442 | ||
| 443 | #[inline] | |
| 444 | fn all_data(&self) -> Self::AllDataIter<'_> { | |
| 445 | (0..).zip(self.weights.as_slice().iter()).mapX(self, Self::construct_sensor_and_id) | |
| 446 | } | |
| 447 | } | |
| 448 | ||
| 449 | impl<F, S, P, BT, const N : usize> ForwardModel<DiscreteMeasure<Loc<F, N>, F>, F> | |
| 450 | for SensorGrid<F, S, P, BT, N> | |
| 451 | where F : Float + ToNalgebraRealField<MixedType=F> + nalgebra::RealField, | |
| 452 | BT : SensorGridBT<F, S, P, N>, | |
| 453 | S : Sensor<F, N>, | |
| 454 | P : Spread<F, N>, | |
| 455 | Convolution<S, P> : Spread<F, N> + LocalAnalysis<F, BT::Agg, N>, | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
456 | { |
| 35 | 457 | type Observable = DVector<F>; |
| 458 | ||
| 459 | fn write_observable(&self, b : &Self::Observable, prefix : String) -> DynError { | |
| 460 | let it = self.grid().into_iter().zip(b.iter()).map(|(x, &v)| (x, v)); | |
| 461 | write_csv(it, prefix + ".txt") | |
| 462 | } | |
| 463 | ||
| 464 | #[inline] | |
| 465 | fn zero_observable(&self) -> Self::Observable { | |
| 466 | self._zero_observable() | |
| 467 | } | |
| 468 | } | |
| 469 | ||
| 470 | impl<F, S, P, BT, const N : usize> FindimQuadraticModel<Loc<F, N>, F> | |
| 471 | for SensorGrid<F, S, P, BT, N> | |
| 472 | where F : Float + ToNalgebraRealField<MixedType=F> + nalgebra::RealField, | |
| 473 | BT : SensorGridBT<F, S, P, N>, | |
| 474 | S : Sensor<F, N>, | |
| 475 | P : Spread<F, N>, | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
476 | Convolution<S, P> : Spread<F, N> + LocalAnalysis<F, BT::Agg, N> |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
477 | { |
| 35 | 478 | |
| 479 | fn findim_quadratic_model( | |
| 480 | &self, | |
| 481 | μ : &DiscreteMeasure<Loc<F, N>, F>, | |
| 482 | b : &Self::Observable | |
| 483 | ) -> (DMatrix<F::MixedType>, DVector<F::MixedType>) { | |
| 484 | assert_eq!(b.len(), self.n_sensors()); | |
| 485 | let mut mA = DMatrix::zeros(self.n_sensors(), μ.len()); | |
| 486 | let grid = self.grid(); | |
| 487 | for (mut mAcol, δ) in mA.column_iter_mut().zip(μ.iter_spikes()) { | |
| 488 | for &d in self.bt.iter_at(&δ.x) { | |
| 489 | let sensor = self.shifted_sensor(grid.entry_linear_unchecked(d)); | |
| 490 | mAcol[d] += sensor.apply(&δ.x); | |
| 491 | } | |
| 492 | } | |
| 493 | let mAt = mA.transpose(); | |
| 494 | (&mAt * mA, &mAt * b) | |
| 495 | } | |
| 496 | } | |
| 497 | ||
| 498 | /// Implements the calculation a factor $L$ such that $A_*A ≤ L 𝒟$ for $A$ the forward model | |
| 499 | /// and $𝒟$ a seminorm of suitable form. | |
| 500 | /// | |
| 501 | /// **This assumes (but does not check) that the sensors are not overlapping.** | |
| 502 | #[replace_float_literals(F::cast_from(literal))] | |
| 503 | impl<F, BT, S, P, K, const N : usize> | |
| 504 | AdjointProductBoundedBy<RNDM<F, N>, ConvolutionOp<F, K, BT, N>> | |
| 505 | for SensorGrid<F, S, P, BT, N> | |
| 506 | where F : Float + nalgebra::RealField + ToNalgebraRealField, | |
| 507 | BT : SensorGridBT<F, S, P, N>, | |
| 508 | S : Sensor<F, N>, | |
| 509 | P : Spread<F, N>, | |
| 510 | Convolution<S, P> : Spread<F, N>, | |
| 511 | K : SimpleConvolutionKernel<F, N>, | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
512 | AutoConvolution<P> : BoundedBy<F, K> |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
513 | { |
| 35 | 514 | |
| 515 | type FloatType = F; | |
| 516 | ||
| 517 | fn adjoint_product_bound(&self, seminorm : &ConvolutionOp<F, K, BT, N>) -> Option<F> { | |
| 518 | // Sensors should not take on negative values to allow | |
| 519 | // A_*A to be upper bounded by a simple convolution of `spread`. | |
| 520 | if self.sensor.bounds().lower() < 0.0 { | |
| 521 | return None | |
| 522 | } | |
| 523 | ||
| 524 | // Calculate the factor $L_1$ for betwee $ℱ[ψ * ψ] ≤ L_1 ℱ[ρ]$ for $ψ$ the base spread | |
| 525 | // and $ρ$ the kernel of the seminorm. | |
| 526 | let l1 = AutoConvolution(self.spread.clone()).bounding_factor(seminorm.kernel())?; | |
| 527 | ||
| 528 | // Calculate the factor for transitioning from $A_*A$ to `AutoConvolution<P>`, where A | |
| 529 | // consists of several `Convolution<S, P>` for the physical model `P` and the sensor `S`. | |
| 530 | let l0 = self.sensor.norm(Linfinity) * self.sensor.norm(L1); | |
| 531 | ||
| 532 | // The final transition factor is: | |
| 533 | Some(l0 * l1) | |
| 534 | } | |
| 535 | } | |
| 536 | ||
| 537 | macro_rules! make_sensorgridsupportgenerator_scalarop_rhs { | |
| 538 | ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => { | |
| 539 | impl<F, S, P, const N : usize> | |
| 540 | std::ops::$trait_assign<F> | |
| 541 | for SensorGridSupportGenerator<F, S, P, N> | |
| 542 | where F : Float, | |
| 543 | S : Sensor<F, N>, | |
| 544 | P : Spread<F, N>, | |
| 545 | Convolution<S, P> : Spread<F, N> { | |
| 546 | fn $fn_assign(&mut self, t : F) { | |
| 547 | self.weights.$fn_assign(t); | |
| 548 | } | |
| 549 | } | |
| 550 | ||
| 551 | impl<F, S, P, const N : usize> | |
| 552 | std::ops::$trait<F> | |
| 553 | for SensorGridSupportGenerator<F, S, P, N> | |
| 554 | where F : Float, | |
| 555 | S : Sensor<F, N>, | |
| 556 | P : Spread<F, N>, | |
| 557 | Convolution<S, P> : Spread<F, N> { | |
| 558 | type Output = SensorGridSupportGenerator<F, S, P, N>; | |
| 559 | fn $fn(mut self, t : F) -> Self::Output { | |
| 560 | std::ops::$trait_assign::$fn_assign(&mut self.weights, t); | |
| 561 | self | |
| 562 | } | |
| 563 | } | |
| 564 | ||
| 565 | impl<'a, F, S, P, const N : usize> | |
| 566 | std::ops::$trait<F> | |
| 567 | for &'a SensorGridSupportGenerator<F, S, P, N> | |
| 568 | where F : Float, | |
| 569 | S : Sensor<F, N>, | |
| 570 | P : Spread<F, N>, | |
| 571 | Convolution<S, P> : Spread<F, N> { | |
| 572 | type Output = SensorGridSupportGenerator<F, S, P, N>; | |
| 573 | fn $fn(self, t : F) -> Self::Output { | |
| 574 | SensorGridSupportGenerator{ | |
| 575 | base_sensor : self.base_sensor.clone(), | |
| 576 | grid : self.grid, | |
| 577 | weights : (&self.weights).$fn(t) | |
| 578 | } | |
| 579 | } | |
| 580 | } | |
| 581 | } | |
| 582 | } | |
| 583 | ||
| 584 | make_sensorgridsupportgenerator_scalarop_rhs!(Mul, mul, MulAssign, mul_assign); | |
| 585 | make_sensorgridsupportgenerator_scalarop_rhs!(Div, div, DivAssign, div_assign); | |
| 586 | ||
| 587 | macro_rules! make_sensorgridsupportgenerator_unaryop { | |
| 588 | ($trait:ident, $fn:ident) => { | |
| 589 | impl<F, S, P, const N : usize> | |
| 590 | std::ops::$trait | |
| 591 | for SensorGridSupportGenerator<F, S, P, N> | |
| 592 | where F : Float, | |
| 593 | S : Sensor<F, N>, | |
| 594 | P : Spread<F, N>, | |
| 595 | Convolution<S, P> : Spread<F, N> { | |
| 596 | type Output = SensorGridSupportGenerator<F, S, P, N>; | |
| 597 | fn $fn(mut self) -> Self::Output { | |
| 598 | self.weights = self.weights.$fn(); | |
| 599 | self | |
| 600 | } | |
| 601 | } | |
| 602 | ||
| 603 | impl<'a, F, S, P, const N : usize> | |
| 604 | std::ops::$trait | |
| 605 | for &'a SensorGridSupportGenerator<F, S, P, N> | |
| 606 | where F : Float, | |
| 607 | S : Sensor<F, N>, | |
| 608 | P : Spread<F, N>, | |
| 609 | Convolution<S, P> : Spread<F, N> { | |
| 610 | type Output = SensorGridSupportGenerator<F, S, P, N>; | |
| 611 | fn $fn(self) -> Self::Output { | |
| 612 | SensorGridSupportGenerator{ | |
| 613 | base_sensor : self.base_sensor.clone(), | |
| 614 | grid : self.grid, | |
| 615 | weights : (&self.weights).$fn() | |
| 616 | } | |
| 617 | } | |
| 618 | } | |
| 619 | } | |
| 620 | } | |
| 621 | ||
| 622 | make_sensorgridsupportgenerator_unaryop!(Neg, neg); | |
| 623 | ||
| 624 | impl<'a, F, S, P, BT, const N : usize> Mapping<DVector<F>> | |
| 625 | for PreadjointHelper<'a, SensorGrid<F, S, P, BT, N>, RNDM<F,N>> | |
| 626 | where F : Float, | |
| 627 | BT : SensorGridBT<F, S, P, N>, | |
| 628 | S : Sensor<F, N>, | |
| 629 | P : Spread<F, N>, | |
| 630 | Convolution<S, P> : Spread<F, N> + LocalAnalysis<F, Bounds<F>, N>, | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
631 | { |
| 35 | 632 | |
| 633 | type Codomain = SensorGridBTFN<F, S, P, BT, N>; | |
| 634 | ||
| 635 | fn apply<I : Instance<DVector<F>>>(&self, x : I) -> Self::Codomain { | |
| 636 | let fwd = &self.forward_op; | |
| 637 | let generator = SensorGridSupportGenerator{ | |
| 638 | base_sensor : fwd.base_sensor.clone(), | |
| 639 | grid : fwd.grid(), | |
| 640 | weights : x.own() | |
| 641 | }; | |
| 642 | BTFN::new_refresh(&fwd.bt, generator) | |
| 643 | } | |
| 644 | } | |
| 645 | ||
| 646 | impl<'a, F, S, P, BT, const N : usize> Linear<DVector<F>> | |
| 647 | for PreadjointHelper<'a, SensorGrid<F, S, P, BT, N>, RNDM<F,N>> | |
| 648 | where F : Float, | |
| 649 | BT : SensorGridBT<F, S, P, N>, | |
| 650 | S : Sensor<F, N>, | |
| 651 | P : Spread<F, N>, | |
| 652 | Convolution<S, P> : Spread<F, N> + LocalAnalysis<F, Bounds<F>, N>, | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
653 | { } |
| 44 | 654 |