Sat, 01 Feb 2025 16:47:11 -0500
Parameter adjustments
| 0 | 1 | |
| 2 | //! Things for constructing new kernels from component kernels and traits for analysing them | |
| 3 | use serde::Serialize; | |
| 4 | use numeric_literals::replace_float_literals; | |
| 5 | ||
| 6 | use alg_tools::types::*; | |
| 7 | use alg_tools::norms::*; | |
| 8 | use alg_tools::loc::Loc; | |
| 9 | use alg_tools::sets::Cube; | |
| 10 | use alg_tools::bisection_tree::{ | |
| 11 | Support, | |
| 12 | Bounds, | |
| 13 | LocalAnalysis, | |
| 14 | GlobalAnalysis, | |
| 15 | Bounded, | |
| 16 | }; | |
| 35 | 17 | use alg_tools::mapping::{ |
| 18 | Mapping, | |
| 19 | DifferentiableImpl, | |
| 20 | DifferentiableMapping, | |
| 21 | Differential, | |
| 22 | }; | |
| 23 | use alg_tools::instance::{Instance, Space}; | |
| 32 | 24 | use alg_tools::maputil::{array_init, map2, map1_indexed}; |
| 0 | 25 | use alg_tools::sets::SetOrd; |
| 26 | ||
| 27 | use crate::fourier::Fourier; | |
| 35 | 28 | use crate::types::*; |
| 0 | 29 | |
| 30 | /// Representation of the product of two kernels. | |
| 31 | /// | |
| 35 | 32 | /// The kernels typically implement [`Support`] and [`Mapping`]. |
| 0 | 33 | /// |
| 34 | /// The implementation [`Support`] only uses the [`Support::support_hint`] of the first parameter! | |
| 35 | #[derive(Copy,Clone,Serialize,Debug)] | |
| 36 | pub struct SupportProductFirst<A, B>( | |
| 37 | /// First kernel | |
| 38 | pub A, | |
| 39 | /// Second kernel | |
| 40 | pub B | |
| 41 | ); | |
| 42 | ||
| 35 | 43 | impl<A, B, F : Float, const N : usize> Mapping<Loc<F, N>> |
| 0 | 44 | for SupportProductFirst<A, B> |
| 35 | 45 | where |
| 46 | A : Mapping<Loc<F, N>, Codomain = F>, | |
| 47 | B : Mapping<Loc<F, N>, Codomain = F>, | |
| 48 | { | |
| 49 | type Codomain = F; | |
| 50 | ||
| 0 | 51 | #[inline] |
| 35 | 52 | fn apply<I : Instance<Loc<F, N>>>(&self, x : I) -> Self::Codomain { |
| 53 | self.0.apply(x.ref_instance()) * self.1.apply(x) | |
| 0 | 54 | } |
| 55 | } | |
| 56 | ||
| 35 | 57 | impl<A, B, F : Float, const N : usize> DifferentiableImpl<Loc<F, N>> |
| 0 | 58 | for SupportProductFirst<A, B> |
| 35 | 59 | where |
| 60 | A : DifferentiableMapping< | |
| 61 | Loc<F, N>, | |
| 62 | DerivativeDomain=Loc<F, N>, | |
| 63 | Codomain = F | |
| 64 | >, | |
| 65 | B : DifferentiableMapping< | |
| 66 | Loc<F, N>, | |
| 67 | DerivativeDomain=Loc<F, N>, | |
| 68 | Codomain = F, | |
| 69 | > | |
| 70 | { | |
| 71 | type Derivative = Loc<F, N>; | |
| 72 | ||
| 0 | 73 | #[inline] |
| 35 | 74 | fn differential_impl<I : Instance<Loc<F, N>>>(&self, x : I) -> Self::Derivative { |
| 75 | let xr = x.ref_instance(); | |
| 76 | self.0.differential(xr) * self.1.apply(xr) + self.1.differential(xr) * self.0.apply(x) | |
| 0 | 77 | } |
| 78 | } | |
| 79 | ||
| 35 | 80 | impl<A, B, M : Copy, F : Float> Lipschitz<M> |
| 32 | 81 | for SupportProductFirst<A, B> |
| 35 | 82 | where A : Lipschitz<M, FloatType = F> + Bounded<F>, |
| 83 | B : Lipschitz<M, FloatType = F> + Bounded<F> { | |
| 84 | type FloatType = F; | |
| 32 | 85 | #[inline] |
| 35 | 86 | fn lipschitz_factor(&self, m : M) -> Option<F> { |
| 87 | // f(x)g(x) - f(y)g(y) = f(x)[g(x)-g(y)] - [f(y)-f(x)]g(y) | |
| 88 | let &SupportProductFirst(ref f, ref g) = self; | |
| 89 | f.lipschitz_factor(m).map(|l| l * g.bounds().uniform()) | |
| 90 | .zip(g.lipschitz_factor(m).map(|l| l * f.bounds().uniform())) | |
| 91 | .map(|(a, b)| a + b) | |
| 32 | 92 | } |
| 93 | } | |
| 94 | ||
| 35 | 95 | impl<'a, A, B, M : Copy, Domain, F : Float> Lipschitz<M> |
| 96 | for Differential<'a, Domain, SupportProductFirst<A, B>> | |
| 97 | where | |
| 98 | Domain : Space, | |
| 99 | A : Clone + DifferentiableMapping<Domain> + Lipschitz<M, FloatType = F> + Bounded<F>, | |
| 100 | B : Clone + DifferentiableMapping<Domain> + Lipschitz<M, FloatType = F> + Bounded<F>, | |
| 101 | SupportProductFirst<A, B> : DifferentiableMapping<Domain>, | |
| 102 | for<'b> A::Differential<'b> : Lipschitz<M, FloatType = F> + NormBounded<L2, FloatType=F>, | |
| 103 | for<'b> B::Differential<'b> : Lipschitz<M, FloatType = F> + NormBounded<L2, FloatType=F> | |
| 104 | { | |
| 105 | type FloatType = F; | |
| 32 | 106 | #[inline] |
| 35 | 107 | fn lipschitz_factor(&self, m : M) -> Option<F> { |
| 108 | // ∇[gf] = f∇g + g∇f | |
| 109 | // ⟹ ∇[gf](x) - ∇[gf](y) = f(x)∇g(x) + g(x)∇f(x) - f(y)∇g(y) + g(y)∇f(y) | |
| 110 | // = f(x)[∇g(x)-∇g(y)] + g(x)∇f(x) - [f(y)-f(x)]∇g(y) + g(y)∇f(y) | |
| 111 | // = f(x)[∇g(x)-∇g(y)] + g(x)[∇f(x)-∇f(y)] | |
| 112 | // - [f(y)-f(x)]∇g(y) + [g(y)-g(x)]∇f(y) | |
| 113 | let &SupportProductFirst(ref f, ref g) = self.base_fn(); | |
| 114 | let (df, dg) = (f.diff_ref(), g.diff_ref()); | |
| 115 | [ | |
| 116 | df.lipschitz_factor(m).map(|l| l * g.bounds().uniform()), | |
| 117 | dg.lipschitz_factor(m).map(|l| l * f.bounds().uniform()), | |
| 118 | f.lipschitz_factor(m).map(|l| l * dg.norm_bound(L2)), | |
| 119 | g.lipschitz_factor(m).map(|l| l * df.norm_bound(L2)) | |
| 120 | ].into_iter().sum() | |
| 32 | 121 | } |
| 122 | } | |
| 123 | ||
| 124 | ||
| 0 | 125 | impl<'a, A, B, F : Float, const N : usize> Support<F, N> |
| 126 | for SupportProductFirst<A, B> | |
| 35 | 127 | where |
| 128 | A : Support<F, N>, | |
| 129 | B : Support<F, N> | |
| 130 | { | |
| 0 | 131 | #[inline] |
| 132 | fn support_hint(&self) -> Cube<F, N> { | |
| 133 | self.0.support_hint() | |
| 134 | } | |
| 135 | ||
| 136 | #[inline] | |
| 137 | fn in_support(&self, x : &Loc<F, N>) -> bool { | |
| 138 | self.0.in_support(x) | |
| 139 | } | |
| 140 | ||
| 141 | #[inline] | |
| 142 | fn bisection_hint(&self, cube : &Cube<F, N>) -> [Option<F>; N] { | |
| 143 | self.0.bisection_hint(cube) | |
| 144 | } | |
| 145 | } | |
| 146 | ||
| 147 | impl<'a, A, B, F : Float> GlobalAnalysis<F, Bounds<F>> | |
| 148 | for SupportProductFirst<A, B> | |
| 149 | where A : GlobalAnalysis<F, Bounds<F>>, | |
| 150 | B : GlobalAnalysis<F, Bounds<F>> { | |
| 151 | #[inline] | |
| 152 | fn global_analysis(&self) -> Bounds<F> { | |
| 153 | self.0.global_analysis() * self.1.global_analysis() | |
| 154 | } | |
| 155 | } | |
| 156 | ||
| 157 | impl<'a, A, B, F : Float, const N : usize> LocalAnalysis<F, Bounds<F>, N> | |
| 158 | for SupportProductFirst<A, B> | |
| 159 | where A : LocalAnalysis<F, Bounds<F>, N>, | |
| 160 | B : LocalAnalysis<F, Bounds<F>, N> { | |
| 161 | #[inline] | |
| 162 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
| 163 | self.0.local_analysis(cube) * self.1.local_analysis(cube) | |
| 164 | } | |
| 165 | } | |
| 166 | ||
| 167 | /// Representation of the sum of two kernels | |
| 168 | /// | |
| 35 | 169 | /// The kernels typically implement [`Support`] and [`Mapping`]. |
| 0 | 170 | /// |
| 171 | /// The implementation [`Support`] only uses the [`Support::support_hint`] of the first parameter! | |
| 172 | #[derive(Copy,Clone,Serialize,Debug)] | |
| 173 | pub struct SupportSum<A, B>( | |
| 174 | /// First kernel | |
| 175 | pub A, | |
| 176 | /// Second kernel | |
| 177 | pub B | |
| 178 | ); | |
| 179 | ||
| 35 | 180 | impl<'a, A, B, F : Float, const N : usize> Mapping<Loc<F, N>> |
| 0 | 181 | for SupportSum<A, B> |
| 35 | 182 | where |
| 183 | A : Mapping<Loc<F, N>, Codomain = F>, | |
| 184 | B : Mapping<Loc<F, N>, Codomain = F>, | |
| 185 | { | |
| 186 | type Codomain = F; | |
| 187 | ||
| 0 | 188 | #[inline] |
| 35 | 189 | fn apply<I : Instance<Loc<F, N>>>(&self, x : I) -> Self::Codomain { |
| 190 | self.0.apply(x.ref_instance()) + self.1.apply(x) | |
| 0 | 191 | } |
| 192 | } | |
| 193 | ||
| 35 | 194 | impl<'a, A, B, F : Float, const N : usize> DifferentiableImpl<Loc<F, N>> |
| 32 | 195 | for SupportSum<A, B> |
| 35 | 196 | where |
| 197 | A : DifferentiableMapping< | |
| 198 | Loc<F, N>, | |
| 199 | DerivativeDomain = Loc<F, N> | |
| 200 | >, | |
| 201 | B : DifferentiableMapping< | |
| 202 | Loc<F, N>, | |
| 203 | DerivativeDomain = Loc<F, N>, | |
| 204 | > | |
| 205 | { | |
| 206 | ||
| 207 | type Derivative = Loc<F, N>; | |
| 208 | ||
| 32 | 209 | #[inline] |
| 35 | 210 | fn differential_impl<I : Instance<Loc<F, N>>>(&self, x : I) -> Self::Derivative { |
| 211 | self.0.differential(x.ref_instance()) + self.1.differential(x) | |
| 32 | 212 | } |
| 213 | } | |
| 214 | ||
| 215 | ||
| 0 | 216 | impl<'a, A, B, F : Float, const N : usize> Support<F, N> |
| 217 | for SupportSum<A, B> | |
| 218 | where A : Support<F, N>, | |
| 219 | B : Support<F, N>, | |
| 220 | Cube<F, N> : SetOrd { | |
| 35 | 221 | |
| 0 | 222 | #[inline] |
| 223 | fn support_hint(&self) -> Cube<F, N> { | |
| 224 | self.0.support_hint().common(&self.1.support_hint()) | |
| 225 | } | |
| 226 | ||
| 227 | #[inline] | |
| 228 | fn in_support(&self, x : &Loc<F, N>) -> bool { | |
| 229 | self.0.in_support(x) || self.1.in_support(x) | |
| 230 | } | |
| 231 | ||
| 232 | #[inline] | |
| 233 | fn bisection_hint(&self, cube : &Cube<F, N>) -> [Option<F>; N] { | |
| 234 | map2(self.0.bisection_hint(cube), | |
| 235 | self.1.bisection_hint(cube), | |
| 236 | |a, b| a.or(b)) | |
| 237 | } | |
| 238 | } | |
| 239 | ||
| 240 | impl<'a, A, B, F : Float> GlobalAnalysis<F, Bounds<F>> | |
| 241 | for SupportSum<A, B> | |
| 242 | where A : GlobalAnalysis<F, Bounds<F>>, | |
| 243 | B : GlobalAnalysis<F, Bounds<F>> { | |
| 244 | #[inline] | |
| 245 | fn global_analysis(&self) -> Bounds<F> { | |
| 246 | self.0.global_analysis() + self.1.global_analysis() | |
| 247 | } | |
| 248 | } | |
| 249 | ||
| 250 | impl<'a, A, B, F : Float, const N : usize> LocalAnalysis<F, Bounds<F>, N> | |
| 251 | for SupportSum<A, B> | |
| 252 | where A : LocalAnalysis<F, Bounds<F>, N>, | |
| 253 | B : LocalAnalysis<F, Bounds<F>, N>, | |
| 254 | Cube<F, N> : SetOrd { | |
| 255 | #[inline] | |
| 256 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
| 257 | self.0.local_analysis(cube) + self.1.local_analysis(cube) | |
| 258 | } | |
| 259 | } | |
| 260 | ||
| 32 | 261 | impl<F : Float, M : Copy, A, B> Lipschitz<M> for SupportSum<A, B> |
| 262 | where A : Lipschitz<M, FloatType = F>, | |
| 263 | B : Lipschitz<M, FloatType = F> { | |
| 264 | type FloatType = F; | |
| 265 | ||
| 266 | fn lipschitz_factor(&self, m : M) -> Option<F> { | |
| 267 | match (self.0.lipschitz_factor(m), self.1.lipschitz_factor(m)) { | |
| 268 | (Some(l0), Some(l1)) => Some(l0 + l1), | |
| 269 | _ => None | |
| 270 | } | |
| 271 | } | |
| 272 | } | |
| 273 | ||
| 35 | 274 | impl<'b, F : Float, M : Copy, A, B, Domain> Lipschitz<M> |
| 275 | for Differential<'b, Domain, SupportSum<A, B>> | |
| 276 | where | |
| 277 | Domain : Space, | |
| 278 | A : Clone + DifferentiableMapping<Domain, Codomain=F>, | |
| 279 | B : Clone + DifferentiableMapping<Domain, Codomain=F>, | |
| 280 | SupportSum<A, B> : DifferentiableMapping<Domain, Codomain=F>, | |
| 281 | for<'a> A :: Differential<'a> : Lipschitz<M, FloatType = F>, | |
| 282 | for<'a> B :: Differential<'a> : Lipschitz<M, FloatType = F> | |
| 283 | { | |
| 284 | type FloatType = F; | |
| 285 | ||
| 286 | fn lipschitz_factor(&self, m : M) -> Option<F> { | |
| 287 | let base = self.base_fn(); | |
| 288 | base.0.diff_ref().lipschitz_factor(m) | |
| 289 | .zip(base.1.diff_ref().lipschitz_factor(m)) | |
| 290 | .map(|(a, b)| a + b) | |
| 291 | } | |
| 292 | } | |
| 32 | 293 | |
| 0 | 294 | /// Representation of the convolution of two kernels. |
| 295 | /// | |
| 35 | 296 | /// The kernels typically implement [`Support`]s and [`Mapping`]. |
| 0 | 297 | // |
| 298 | /// Trait implementations have to be on a case-by-case basis. | |
| 299 | #[derive(Copy,Clone,Serialize,Debug,Eq,PartialEq)] | |
| 300 | pub struct Convolution<A, B>( | |
| 301 | /// First kernel | |
| 302 | pub A, | |
| 303 | /// Second kernel | |
| 304 | pub B | |
| 305 | ); | |
| 306 | ||
| 32 | 307 | impl<F : Float, M, A, B> Lipschitz<M> for Convolution<A, B> |
| 35 | 308 | where A : Norm<F, L1> , |
| 32 | 309 | B : Lipschitz<M, FloatType = F> { |
| 310 | type FloatType = F; | |
| 311 | ||
| 312 | fn lipschitz_factor(&self, m : M) -> Option<F> { | |
| 35 | 313 | // For [f * g](x) = ∫ f(x-y)g(y) dy we have |
| 314 | // [f * g](x) - [f * g](z) = ∫ [f(x-y)-f(z-y)]g(y) dy. | |
| 315 | // Hence |[f * g](x) - [f * g](z)| ≤ ∫ |f(x-y)-f(z-y)|g(y)| dy. | |
| 316 | // ≤ L|x-z| ∫ |g(y)| dy, | |
| 317 | // where L is the Lipschitz factor of f. | |
| 318 | self.1.lipschitz_factor(m).map(|l| l * self.0.norm(L1)) | |
| 319 | } | |
| 320 | } | |
| 321 | ||
| 322 | impl<'b, F : Float, M, A, B, Domain> Lipschitz<M> | |
| 323 | for Differential<'b, Domain, Convolution<A, B>> | |
| 324 | where | |
| 325 | Domain : Space, | |
| 326 | A : Clone + Norm<F, L1> , | |
| 327 | Convolution<A, B> : DifferentiableMapping<Domain, Codomain=F>, | |
| 328 | B : Clone + DifferentiableMapping<Domain, Codomain=F>, | |
| 329 | for<'a> B :: Differential<'a> : Lipschitz<M, FloatType = F> | |
| 330 | { | |
| 331 | type FloatType = F; | |
| 332 | ||
| 333 | fn lipschitz_factor(&self, m : M) -> Option<F> { | |
| 334 | // For [f * g](x) = ∫ f(x-y)g(y) dy we have | |
| 335 | // ∇[f * g](x) - ∇[f * g](z) = ∫ [∇f(x-y)-∇f(z-y)]g(y) dy. | |
| 336 | // Hence |∇[f * g](x) - ∇[f * g](z)| ≤ ∫ |∇f(x-y)-∇f(z-y)|g(y)| dy. | |
| 337 | // ≤ L|x-z| ∫ |g(y)| dy, | |
| 338 | // where L is the Lipschitz factor of ∇f. | |
| 339 | let base = self.base_fn(); | |
| 340 | base.1.diff_ref().lipschitz_factor(m).map(|l| l * base.0.norm(L1)) | |
| 32 | 341 | } |
| 342 | } | |
| 343 | ||
| 0 | 344 | /// Representation of the autoconvolution of a kernel. |
| 345 | /// | |
| 35 | 346 | /// The kernel typically implements [`Support`] and [`Mapping`]. |
| 0 | 347 | /// |
| 348 | /// Trait implementations have to be on a case-by-case basis. | |
| 349 | #[derive(Copy,Clone,Serialize,Debug,Eq,PartialEq)] | |
| 350 | pub struct AutoConvolution<A>( | |
| 351 | /// The kernel to be autoconvolved | |
| 352 | pub A | |
| 353 | ); | |
| 354 | ||
| 32 | 355 | impl<F : Float, M, C> Lipschitz<M> for AutoConvolution<C> |
| 35 | 356 | where C : Lipschitz<M, FloatType = F> + Norm<F, L1> { |
| 32 | 357 | type FloatType = F; |
| 358 | ||
| 359 | fn lipschitz_factor(&self, m : M) -> Option<F> { | |
| 35 | 360 | self.0.lipschitz_factor(m).map(|l| l * self.0.norm(L1)) |
| 361 | } | |
| 362 | } | |
| 363 | ||
| 364 | impl<'b, F : Float, M, C, Domain> Lipschitz<M> | |
| 365 | for Differential<'b, Domain, AutoConvolution<C>> | |
| 366 | where | |
| 367 | Domain : Space, | |
| 368 | C : Clone + Norm<F, L1> + DifferentiableMapping<Domain, Codomain=F>, | |
| 369 | AutoConvolution<C> : DifferentiableMapping<Domain, Codomain=F>, | |
| 370 | for<'a> C :: Differential<'a> : Lipschitz<M, FloatType = F> | |
| 371 | { | |
| 372 | type FloatType = F; | |
| 373 | ||
| 374 | fn lipschitz_factor(&self, m : M) -> Option<F> { | |
| 375 | let base = self.base_fn(); | |
| 376 | base.0.diff_ref().lipschitz_factor(m).map(|l| l * base.0.norm(L1)) | |
| 32 | 377 | } |
| 378 | } | |
| 379 | ||
| 380 | ||
| 0 | 381 | /// Representation a multi-dimensional product of a one-dimensional kernel. |
| 382 | /// | |
| 383 | /// For $G: ℝ → ℝ$, this is the function $F(x\_1, …, x\_n) := \prod_{i=1}^n G(x\_i)$. | |
| 35 | 384 | /// The kernel $G$ typically implements [`Support`] and [`Mapping`] |
| 0 | 385 | /// on [`Loc<F, 1>`]. Then the product implements them on [`Loc<F, N>`]. |
| 386 | #[derive(Copy,Clone,Serialize,Debug,Eq,PartialEq)] | |
| 35 | 387 | #[allow(dead_code)] |
| 0 | 388 | struct UniformProduct<G, const N : usize>( |
| 389 | /// The one-dimensional kernel | |
| 390 | G | |
| 391 | ); | |
| 392 | ||
| 35 | 393 | impl<'a, G, F : Float, const N : usize> Mapping<Loc<F, N>> |
| 0 | 394 | for UniformProduct<G, N> |
| 35 | 395 | where |
| 396 | G : Mapping<Loc<F, 1>, Codomain = F> | |
| 397 | { | |
| 398 | type Codomain = F; | |
| 399 | ||
| 0 | 400 | #[inline] |
| 35 | 401 | fn apply<I : Instance<Loc<F, N>>>(&self, x : I) -> F { |
| 402 | x.cow().iter().map(|&y| self.0.apply(Loc([y]))).product() | |
| 0 | 403 | } |
| 404 | } | |
| 405 | ||
| 35 | 406 | |
| 407 | ||
| 408 | impl<'a, G, F : Float, const N : usize> DifferentiableImpl<Loc<F, N>> | |
| 0 | 409 | for UniformProduct<G, N> |
| 35 | 410 | where |
| 411 | G : DifferentiableMapping< | |
| 412 | Loc<F, 1>, | |
| 413 | DerivativeDomain = F, | |
| 414 | Codomain = F, | |
| 415 | > | |
| 416 | { | |
| 417 | type Derivative = Loc<F, N>; | |
| 418 | ||
| 0 | 419 | #[inline] |
| 35 | 420 | fn differential_impl<I : Instance<Loc<F, N>>>(&self, x0 : I) -> Loc<F, N> { |
| 421 | x0.eval(|x| { | |
| 422 | let vs = x.map(|y| self.0.apply(Loc([y]))); | |
| 423 | product_differential(x, &vs, |y| self.0.differential(Loc([y]))) | |
| 424 | }) | |
| 32 | 425 | } |
| 426 | } | |
| 427 | ||
| 428 | /// Helper function to calulate the differential of $f(x)=∏_{i=1}^N g(x_i)$. | |
| 429 | /// | |
| 430 | /// The vector `x` is the location, `vs` consists of the values `g(x_i)`, and | |
| 431 | /// `gd` calculates the derivative `g'`. | |
| 432 | #[inline] | |
| 433 | pub(crate) fn product_differential<F : Float, G : Fn(F) -> F, const N : usize>( | |
| 434 | x : &Loc<F, N>, | |
| 435 | vs : &Loc<F, N>, | |
| 436 | gd : G | |
| 437 | ) -> Loc<F, N> { | |
| 438 | map1_indexed(x, |i, &y| { | |
| 439 | gd(y) * vs.iter() | |
| 440 | .zip(0..) | |
| 441 | .filter_map(|(v, j)| (j != i).then_some(*v)) | |
| 442 | .product() | |
| 443 | }).into() | |
| 444 | } | |
| 445 | ||
| 35 | 446 | /// Helper function to calulate the Lipschitz factor of $∇f$ for $f(x)=∏_{i=1}^N g(x_i)$. |
| 447 | /// | |
| 448 | /// The parameter `bound` is a bound on $|g|_∞$, `lip` is a Lipschitz factor for $g$, | |
| 449 | /// `dbound` is a bound on $|∇g|_∞$, and `dlip` a Lipschitz factor for $∇g$. | |
| 450 | #[inline] | |
| 451 | pub(crate) fn product_differential_lipschitz_factor<F : Float, const N : usize>( | |
| 452 | bound : F, | |
| 453 | lip : F, | |
| 454 | dbound : F, | |
| 455 | dlip : F | |
| 456 | ) -> F { | |
| 457 | // For arbitrary ψ(x) = ∏_{i=1}^n ψ_i(x_i), we have | |
| 458 | // ψ(x) - ψ(y) = ∑_i [ψ_i(x_i)-ψ_i(y_i)] ∏_{j ≠ i} ψ_j(x_j) | |
| 459 | // by a simple recursive argument. In particular, if ψ_i=g for all i, j, we have | |
| 460 | // |ψ(x) - ψ(y)| ≤ ∑_i L_g M_g^{n-1}|x-y|, where L_g is the Lipschitz factor of g, and | |
| 461 | // M_g a bound on it. | |
| 462 | // | |
| 463 | // We also have in the general case ∇ψ(x) = ∑_i ∇ψ_i(x_i) ∏_{j ≠ i} ψ_j(x_j), whence | |
| 464 | // using the previous formula for each i with f_i=∇ψ_i and f_j=ψ_j for j ≠ i, we get | |
| 465 | // ∇ψ(x) - ∇ψ(y) = ∑_i[ ∇ψ_i(x_i)∏_{j ≠ i} ψ_j(x_j) - ∇ψ_i(y_i)∏_{j ≠ i} ψ_j(y_j)] | |
| 466 | // = ∑_i[ [∇ψ_i(x_i) - ∇ψ_j(x_j)] ∏_{j ≠ i}ψ_j(x_j) | |
| 467 | // + [∑_{k ≠ i} [ψ_k(x_k) - ∇ψ_k(x_k)] ∏_{j ≠ i, k}ψ_j(x_j)]∇ψ_i(x_i)]. | |
| 468 | // With $ψ_i=g for all i, j, it follows that | |
| 469 | // |∇ψ(x) - ∇ψ(y)| ≤ ∑_i L_{∇g} M_g^{n-1} + ∑_{k ≠ i} L_g M_g^{n-2} M_{∇g} | |
| 470 | // = n [L_{∇g} M_g^{n-1} + (n-1) L_g M_g^{n-2} M_{∇g}]. | |
| 471 | // = n M_g^{n-2}[L_{∇g} M_g + (n-1) L_g M_{∇g}]. | |
| 472 | if N >= 2 { | |
| 473 | F::cast_from(N) * bound.powi((N-2) as i32) | |
| 474 | * (dlip * bound + F::cast_from(N-1) * lip * dbound) | |
| 475 | } else if N==1 { | |
| 476 | dlip | |
| 477 | } else { | |
| 478 | panic!("Invalid dimension") | |
| 32 | 479 | } |
| 480 | } | |
| 481 | ||
| 0 | 482 | impl<G, F : Float, const N : usize> Support<F, N> |
| 483 | for UniformProduct<G, N> | |
| 484 | where G : Support<F, 1> { | |
| 485 | #[inline] | |
| 486 | fn support_hint(&self) -> Cube<F, N> { | |
| 487 | let [a] : [[F; 2]; 1] = self.0.support_hint().into(); | |
| 488 | array_init(|| a.clone()).into() | |
| 489 | } | |
| 490 | ||
| 491 | #[inline] | |
| 492 | fn in_support(&self, x : &Loc<F, N>) -> bool { | |
| 493 | x.iter().all(|&y| self.0.in_support(&Loc([y]))) | |
| 494 | } | |
| 495 | ||
| 496 | #[inline] | |
| 497 | fn bisection_hint(&self, cube : &Cube<F, N>) -> [Option<F>; N] { | |
| 498 | cube.map(|a, b| { | |
| 499 | let [h] = self.0.bisection_hint(&[[a, b]].into()); | |
| 500 | h | |
| 501 | }) | |
| 502 | } | |
| 503 | } | |
| 504 | ||
| 505 | impl<G, F : Float, const N : usize> GlobalAnalysis<F, Bounds<F>> | |
| 506 | for UniformProduct<G, N> | |
| 507 | where G : GlobalAnalysis<F, Bounds<F>> { | |
| 508 | #[inline] | |
| 509 | fn global_analysis(&self) -> Bounds<F> { | |
| 510 | let g = self.0.global_analysis(); | |
| 511 | (0..N).map(|_| g).product() | |
| 512 | } | |
| 513 | } | |
| 514 | ||
| 515 | impl<G, F : Float, const N : usize> LocalAnalysis<F, Bounds<F>, N> | |
| 516 | for UniformProduct<G, N> | |
| 517 | where G : LocalAnalysis<F, Bounds<F>, 1> { | |
| 518 | #[inline] | |
| 519 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
| 520 | cube.iter_coords().map( | |
| 521 | |&[a, b]| self.0.local_analysis(&([[a, b]].into())) | |
| 522 | ).product() | |
| 523 | } | |
| 524 | } | |
| 525 | ||
| 526 | macro_rules! product_lpnorm { | |
| 527 | ($lp:ident) => { | |
| 528 | impl<G, F : Float, const N : usize> Norm<F, $lp> | |
| 529 | for UniformProduct<G, N> | |
| 530 | where G : Norm<F, $lp> { | |
| 531 | #[inline] | |
| 532 | fn norm(&self, lp : $lp) -> F { | |
| 533 | self.0.norm(lp).powi(N as i32) | |
| 534 | } | |
| 535 | } | |
| 536 | } | |
| 537 | } | |
| 538 | ||
| 539 | product_lpnorm!(L1); | |
| 540 | product_lpnorm!(L2); | |
| 541 | product_lpnorm!(Linfinity); | |
| 542 | ||
| 543 | ||
| 544 | /// Trait for bounding one kernel with respect to another. | |
| 545 | /// | |
| 546 | /// The type `F` is the scalar field, and `T` another kernel to which `Self` is compared. | |
| 547 | pub trait BoundedBy<F : Num, T> { | |
| 548 | /// Calclate a bounding factor $c$ such that the Fourier transforms $ℱ\[v\] ≤ c ℱ\[u\]$ for | |
| 549 | /// $v$ `self` and $u$ `other`. | |
| 550 | /// | |
| 551 | /// If no such factors exits, `None` is returned. | |
| 552 | fn bounding_factor(&self, other : &T) -> Option<F>; | |
| 553 | } | |
| 554 | ||
| 555 | /// This [`BoundedBy`] implementation bounds $(uv) * (uv)$ by $(ψ * ψ) u$. | |
| 556 | #[replace_float_literals(F::cast_from(literal))] | |
| 557 | impl<F, C, BaseP> | |
| 558 | BoundedBy<F, SupportProductFirst<AutoConvolution<C>, BaseP>> | |
| 559 | for AutoConvolution<SupportProductFirst<C, BaseP>> | |
| 560 | where F : Float, | |
| 561 | C : Clone + PartialEq, | |
| 562 | BaseP : Fourier<F> + PartialOrd, // TODO: replace by BoundedBy, | |
| 563 | <BaseP as Fourier<F>>::Transformed : Bounded<F> + Norm<F, L1> { | |
| 564 | ||
| 565 | fn bounding_factor(&self, kernel : &SupportProductFirst<AutoConvolution<C>, BaseP>) -> Option<F> { | |
| 566 | let SupportProductFirst(AutoConvolution(ref cutoff2), base_spread2) = kernel; | |
| 567 | let AutoConvolution(SupportProductFirst(ref cutoff, ref base_spread)) = self; | |
| 568 | let v̂ = base_spread.fourier(); | |
| 569 | ||
| 570 | // Verify that the cut-off and ideal physical model (base spread) are the same. | |
| 571 | if cutoff == cutoff2 | |
| 572 | && base_spread <= base_spread2 | |
| 573 | && v̂.bounds().lower() >= 0.0 { | |
| 574 | // Calculate the factor between the convolution approximation | |
| 575 | // `AutoConvolution<SupportProductFirst<C, BaseP>>` of $A_*A$ and the | |
| 576 | // kernel of the seminorm. This depends on the physical model P being | |
| 577 | // `SupportProductFirst<C, BaseP>` with the kernel `K` being | |
| 578 | // a `SupportSum` involving `SupportProductFirst<AutoConvolution<C>, BaseP>`. | |
| 579 | Some(v̂.norm(L1)) | |
| 580 | } else { | |
| 581 | // We cannot compare | |
| 582 | None | |
| 583 | } | |
| 584 | } | |
| 585 | } | |
| 586 | ||
| 587 | impl<F : Float, A, B, C> BoundedBy<F, SupportSum<B, C>> for A | |
| 588 | where A : BoundedBy<F, B>, | |
| 589 | C : Bounded<F> { | |
| 590 | ||
| 591 | #[replace_float_literals(F::cast_from(literal))] | |
| 592 | fn bounding_factor(&self, SupportSum(ref kernel1, kernel2) : &SupportSum<B, C>) -> Option<F> { | |
| 593 | if kernel2.bounds().lower() >= 0.0 { | |
| 594 | self.bounding_factor(kernel1) | |
| 595 | } else { | |
| 596 | None | |
| 597 | } | |
| 598 | } | |
| 599 | } | |
| 600 | ||
| 601 | /// Generates on $[a, b]$ [`Support::support_hint`] for a symmetric interval $[-r, r]$. | |
| 602 | /// | |
| 603 | /// It will attempt to place the subdivision point at $-r$ or $r$. | |
| 604 | /// If neither of these points lies within $[a, b]$, `None` is returned. | |
| 605 | #[inline] | |
| 606 | pub(super) fn symmetric_interval_hint<F : Float>(r : F, a : F, b : F) -> Option<F> { | |
| 607 | if a < -r && -r < b { | |
| 608 | Some(-r) | |
| 609 | } else if a < r && r < b { | |
| 610 | Some(r) | |
| 611 | } else { | |
| 612 | None | |
| 613 | } | |
| 614 | } | |
| 615 | ||
| 616 | /// Generates on $[a, b]$ [`Support::support_hint`] for a function with monotone derivative, | |
| 617 | /// support on $[-r, r]$ and peak at $0. | |
| 618 | /// | |
| 619 | /// It will attempt to place the subdivision point at $-r$, $r$, or $0$, depending on which | |
| 620 | /// gives the longer length for the shorter of the two subintervals. If none of these points | |
| 621 | /// lies within $[a, b]$, or the resulting interval would be shorter than $0.3r$, `None` is | |
| 622 | /// returned. | |
| 623 | #[replace_float_literals(F::cast_from(literal))] | |
| 624 | #[inline] | |
| 625 | pub(super) fn symmetric_peak_hint<F : Float>(r : F, a : F, b : F) -> Option<F> { | |
| 626 | let stage1 = if a < -r { | |
| 627 | if b <= -r { | |
| 628 | None | |
| 629 | } else if a + r < -b { | |
| 630 | Some(-r) | |
| 631 | } else { | |
| 632 | Some(0.0) | |
| 633 | } | |
| 634 | } else if a < 0.0 { | |
| 635 | if b <= 0.0 { | |
| 636 | None | |
| 637 | } else if a < r - b { | |
| 638 | Some(0.0) | |
| 639 | } else { | |
| 640 | Some(r) | |
| 641 | } | |
| 642 | } else if a < r && b > r { | |
| 643 | Some(r) | |
| 644 | } else { | |
| 645 | None | |
| 646 | }; | |
| 647 | ||
| 648 | // Ignore stage1 hint if either side of subdivision would be just a small fraction of the | |
| 649 | // interval | |
| 650 | match stage1 { | |
| 651 | Some(h) if (h - a).min(b-h) >= 0.3 * r => Some(h), | |
| 652 | _ => None | |
| 653 | } | |
| 654 | } |