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