Tue, 31 Dec 2024 09:34:24 -0500
Early transport sketches
0 | 1 | //! Implementation of the convolution of two hat functions, |
2 | //! and its convolution with a [`CubeIndicator`]. | |
3 | use numeric_literals::replace_float_literals; | |
4 | use serde::Serialize; | |
5 | use alg_tools::types::*; | |
6 | use alg_tools::norms::*; | |
7 | use alg_tools::loc::Loc; | |
8 | use alg_tools::sets::Cube; | |
9 | use alg_tools::bisection_tree::{ | |
10 | Support, | |
11 | Constant, | |
12 | Bounds, | |
13 | LocalAnalysis, | |
14 | GlobalAnalysis, | |
15 | Bounded, | |
16 | }; | |
32 | 17 | use alg_tools::mapping::{Apply, Differentiable}; |
0 | 18 | use alg_tools::maputil::array_init; |
19 | ||
32 | 20 | use crate::types::Lipschitz; |
0 | 21 | use super::base::*; |
22 | use super::ball_indicator::CubeIndicator; | |
23 | ||
24 | /// Hat convolution kernel. | |
25 | /// | |
26 | /// This struct represents the function | |
27 | /// $$ | |
28 | /// f(x\_1, …, x\_n) = \prod\_{i=1}^n \frac{4}{σ} (h\*h)(x\_i/σ) | |
29 | /// $$ | |
30 | /// where the “hat function” $h(y)= \max(0, 1 - |2y|)$. | |
31 | /// The factor $4/σ$ normalises $∫ f d x = 1$. | |
32 | /// We have | |
33 | /// $$ | |
34 | /// (h*h)(y) = | |
35 | /// \begin{cases} | |
36 | /// \frac{2}{3} (y+1)^3 & -1<y\leq -\frac{1}{2}, \\\\ | |
37 | /// -2 y^3-2 y^2+\frac{1}{3} & -\frac{1}{2}<y\leq 0, \\\\ | |
38 | /// 2 y^3-2 y^2+\frac{1}{3} & 0<y<\frac{1}{2}, \\\\ | |
39 | /// -\frac{2}{3} (y-1)^3 & \frac{1}{2}\leq y<1. \\\\ | |
40 | /// \end{cases} | |
41 | /// $$ | |
42 | #[derive(Copy,Clone,Debug,Serialize,Eq)] | |
43 | pub struct HatConv<S : Constant, const N : usize> { | |
44 | /// The parameter $σ$ of the kernel. | |
45 | pub radius : S, | |
46 | } | |
47 | ||
48 | impl<S1, S2, const N : usize> PartialEq<HatConv<S2, N>> for HatConv<S1, N> | |
49 | where S1 : Constant, | |
50 | S2 : Constant<Type=S1::Type> { | |
51 | fn eq(&self, other : &HatConv<S2, N>) -> bool { | |
52 | self.radius.value() == other.radius.value() | |
53 | } | |
54 | } | |
55 | ||
56 | impl<'a, S, const N : usize> HatConv<S, N> where S : Constant { | |
57 | /// Returns the $σ$ parameter of the kernel. | |
58 | #[inline] | |
59 | pub fn radius(&self) -> S::Type { | |
60 | self.radius.value() | |
61 | } | |
62 | } | |
63 | ||
64 | impl<'a, S, const N : usize> Apply<&'a Loc<S::Type, N>> for HatConv<S, N> | |
65 | where S : Constant { | |
66 | type Output = S::Type; | |
67 | #[inline] | |
68 | fn apply(&self, y : &'a Loc<S::Type, N>) -> Self::Output { | |
69 | let σ = self.radius(); | |
70 | y.product_map(|x| { | |
71 | self.value_1d_σ1(x / σ) / σ | |
72 | }) | |
73 | } | |
74 | } | |
75 | ||
76 | impl<'a, S, const N : usize> Apply<Loc<S::Type, N>> for HatConv<S, N> | |
77 | where S : Constant { | |
78 | type Output = S::Type; | |
79 | #[inline] | |
80 | fn apply(&self, y : Loc<S::Type, N>) -> Self::Output { | |
81 | self.apply(&y) | |
82 | } | |
83 | } | |
84 | ||
32 | 85 | #[replace_float_literals(S::Type::cast_from(literal))] |
86 | impl<S, const N : usize> Lipschitz<L1> for HatConv<S, N> | |
87 | where S : Constant { | |
88 | type FloatType = S::Type; | |
89 | #[inline] | |
90 | fn lipschitz_factor(&self, L1 : L1) -> Option<Self::FloatType> { | |
91 | // For any ψ_i, we have | |
92 | // ∏_{i=1}^N ψ_i(x_i) - ∏_{i=1}^N ψ_i(y_i) | |
93 | // = [ψ_1(x_1)-ψ_1(y_1)] ∏_{i=2}^N ψ_i(x_i) | |
94 | // + ψ_1(y_1)[ ∏_{i=2}^N ψ_i(x_i) - ∏_{i=2}^N ψ_i(y_i)] | |
95 | // = ∑_{j=1}^N [ψ_j(x_j)-ψ_j(y_j)]∏_{i > j} ψ_i(x_i) ∏_{i < j} ψ_i(y_i) | |
96 | // Thus | |
97 | // |∏_{i=1}^N ψ_i(x_i) - ∏_{i=1}^N ψ_i(y_i)| | |
98 | // ≤ ∑_{j=1}^N |ψ_j(x_j)-ψ_j(y_j)| ∏_{j ≠ i} \max_i |ψ_i| | |
99 | let σ = self.radius(); | |
100 | Some((self.lipschitz_1d_σ1() / (σ*σ)) * (self.value_1d_σ1(0.0) / σ)) | |
101 | } | |
102 | } | |
103 | ||
104 | impl<S, const N : usize> Lipschitz<L2> for HatConv<S, N> | |
105 | where S : Constant { | |
106 | type FloatType = S::Type; | |
107 | #[inline] | |
108 | fn lipschitz_factor(&self, L2 : L2) -> Option<Self::FloatType> { | |
109 | self.lipschitz_factor(L1).map(|l1| l1 * <S::Type>::cast_from(N).sqrt()) | |
110 | } | |
111 | } | |
112 | ||
113 | ||
114 | impl<'a, S, const N : usize> Differentiable<&'a Loc<S::Type, N>> for HatConv<S, N> | |
115 | where S : Constant { | |
116 | type Output = Loc<S::Type, N>; | |
117 | #[inline] | |
118 | fn differential(&self, y : &'a Loc<S::Type, N>) -> Self::Output { | |
119 | let σ = self.radius(); | |
120 | let σ2 = σ * σ; | |
121 | let vs = y.map(|x| { | |
122 | self.value_1d_σ1(x / σ) / σ | |
123 | }); | |
124 | product_differential(y, &vs, |x| { | |
125 | self.diff_1d_σ1(x / σ) / σ2 | |
126 | }) | |
127 | } | |
128 | } | |
129 | ||
130 | impl<'a, S, const N : usize> Differentiable<Loc<S::Type, N>> for HatConv<S, N> | |
131 | where S : Constant { | |
132 | type Output = Loc<S::Type, N>; | |
133 | #[inline] | |
134 | fn differential(&self, y : Loc<S::Type, N>) -> Self::Output { | |
135 | self.differential(&y) | |
136 | } | |
137 | } | |
0 | 138 | |
139 | #[replace_float_literals(S::Type::cast_from(literal))] | |
140 | impl<'a, F : Float, S, const N : usize> HatConv<S, N> | |
141 | where S : Constant<Type=F> { | |
142 | /// Computes the value of the kernel for $n=1$ with $σ=1$. | |
143 | #[inline] | |
144 | fn value_1d_σ1(&self, x : F) -> F { | |
145 | let y = x.abs(); | |
146 | if y >= 1.0 { | |
147 | 0.0 | |
148 | } else if y > 0.5 { | |
149 | - (8.0/3.0) * (y - 1.0).powi(3) | |
150 | } else /* 0 ≤ y ≤ 0.5 */ { | |
151 | (4.0/3.0) + 8.0 * y * y * (y - 1.0) | |
152 | } | |
153 | } | |
32 | 154 | |
155 | /// Computes the differential of the kernel for $n=1$ with $σ=1$. | |
156 | #[inline] | |
157 | fn diff_1d_σ1(&self, x : F) -> F { | |
158 | let y = x.abs(); | |
159 | if y >= 1.0 { | |
160 | 0.0 | |
161 | } else if y > 0.5 { | |
162 | - 8.0 * (y - 1.0).powi(2) | |
163 | } else /* 0 ≤ y ≤ 0.5 */ { | |
164 | (24.0 * y - 16.0) * y | |
165 | } | |
166 | } | |
167 | ||
168 | /// Computes the Lipschitz factor of the kernel for $n=1$ with $σ=1$. | |
169 | #[inline] | |
170 | fn lipschitz_1d_σ1(&self) -> F { | |
171 | // Maximal absolute differential achieved at ±0.5 by diff_1d_σ1 analysis | |
172 | 2.0 | |
173 | } | |
0 | 174 | } |
175 | ||
176 | impl<'a, S, const N : usize> Support<S::Type, N> for HatConv<S, N> | |
177 | where S : Constant { | |
178 | #[inline] | |
179 | fn support_hint(&self) -> Cube<S::Type,N> { | |
180 | let σ = self.radius(); | |
181 | array_init(|| [-σ, σ]).into() | |
182 | } | |
183 | ||
184 | #[inline] | |
185 | fn in_support(&self, y : &Loc<S::Type,N>) -> bool { | |
186 | let σ = self.radius(); | |
187 | y.iter().all(|x| x.abs() <= σ) | |
188 | } | |
189 | ||
190 | #[inline] | |
191 | fn bisection_hint(&self, cube : &Cube<S::Type, N>) -> [Option<S::Type>; N] { | |
192 | let σ = self.radius(); | |
193 | cube.map(|c, d| symmetric_peak_hint(σ, c, d)) | |
194 | } | |
195 | } | |
196 | ||
197 | #[replace_float_literals(S::Type::cast_from(literal))] | |
198 | impl<S, const N : usize> GlobalAnalysis<S::Type, Bounds<S::Type>> for HatConv<S, N> | |
199 | where S : Constant { | |
200 | #[inline] | |
201 | fn global_analysis(&self) -> Bounds<S::Type> { | |
202 | Bounds(0.0, self.apply(Loc::ORIGIN)) | |
203 | } | |
204 | } | |
205 | ||
206 | impl<S, const N : usize> LocalAnalysis<S::Type, Bounds<S::Type>, N> for HatConv<S, N> | |
207 | where S : Constant { | |
208 | #[inline] | |
209 | fn local_analysis(&self, cube : &Cube<S::Type, N>) -> Bounds<S::Type> { | |
210 | // The function is maximised/minimised where the 2-norm is minimised/maximised. | |
211 | let lower = self.apply(cube.maxnorm_point()); | |
212 | let upper = self.apply(cube.minnorm_point()); | |
213 | Bounds(lower, upper) | |
214 | } | |
215 | } | |
216 | ||
217 | #[replace_float_literals(C::Type::cast_from(literal))] | |
218 | impl<'a, C : Constant, const N : usize> Norm<C::Type, L1> | |
219 | for HatConv<C, N> { | |
220 | #[inline] | |
221 | fn norm(&self, _ : L1) -> C::Type { | |
222 | 1.0 | |
223 | } | |
224 | } | |
225 | ||
226 | #[replace_float_literals(C::Type::cast_from(literal))] | |
227 | impl<'a, C : Constant, const N : usize> Norm<C::Type, Linfinity> | |
228 | for HatConv<C, N> { | |
229 | #[inline] | |
230 | fn norm(&self, _ : Linfinity) -> C::Type { | |
231 | self.bounds().upper() | |
232 | } | |
233 | } | |
234 | ||
235 | #[replace_float_literals(F::cast_from(literal))] | |
236 | impl<'a, F : Float, R, C, const N : usize> Apply<&'a Loc<F, N>> | |
237 | for Convolution<CubeIndicator<R, N>, HatConv<C, N>> | |
238 | where R : Constant<Type=F>, | |
239 | C : Constant<Type=F> { | |
240 | ||
241 | type Output = F; | |
242 | ||
243 | #[inline] | |
244 | fn apply(&self, y : &'a Loc<F, N>) -> F { | |
245 | let Convolution(ref ind, ref hatconv) = self; | |
246 | let β = ind.r.value(); | |
247 | let σ = hatconv.radius(); | |
248 | ||
249 | // This is just a product of one-dimensional versions | |
250 | y.product_map(|x| { | |
251 | // With $u_σ(x) = u_1(x/σ)/σ$ the normalised hat convolution | |
252 | // we have | |
253 | // $$ | |
254 | // [χ_{-β,β} * u_σ](x) | |
255 | // = ∫_{x-β}^{x+β} u_σ(z) d z | |
256 | // = (1/σ)∫_{x-β}^{x+β} u_1(z/σ) d z | |
257 | // = ∫_{(x-β)/σ}^{(x+β)/σ} u_1(z) d z | |
258 | // = [χ_{-β/σ, β/σ} * u_1](x/σ) | |
259 | // $$ | |
260 | self.value_1d_σ1(x / σ, β / σ) | |
261 | }) | |
262 | } | |
263 | } | |
264 | ||
265 | impl<'a, F : Float, R, C, const N : usize> Apply<Loc<F, N>> | |
266 | for Convolution<CubeIndicator<R, N>, HatConv<C, N>> | |
267 | where R : Constant<Type=F>, | |
268 | C : Constant<Type=F> { | |
269 | ||
270 | type Output = F; | |
271 | ||
272 | #[inline] | |
273 | fn apply(&self, y : Loc<F, N>) -> F { | |
274 | self.apply(&y) | |
275 | } | |
276 | } | |
277 | ||
32 | 278 | #[replace_float_literals(F::cast_from(literal))] |
279 | impl<'a, F : Float, R, C, const N : usize> Differentiable<&'a Loc<F, N>> | |
280 | for Convolution<CubeIndicator<R, N>, HatConv<C, N>> | |
281 | where R : Constant<Type=F>, | |
282 | C : Constant<Type=F> { | |
283 | ||
284 | type Output = Loc<F, N>; | |
285 | ||
286 | #[inline] | |
287 | fn differential(&self, y : &'a Loc<F, N>) -> Loc<F, N> { | |
288 | let Convolution(ref ind, ref hatconv) = self; | |
289 | let β = ind.r.value(); | |
290 | let σ = hatconv.radius(); | |
291 | let σ2 = σ * σ; | |
292 | ||
293 | let vs = y.map(|x| { | |
294 | self.value_1d_σ1(x / σ, β / σ) | |
295 | }); | |
296 | product_differential(y, &vs, |x| { | |
297 | self.diff_1d_σ1(x / σ, β / σ) / σ2 | |
298 | }) | |
299 | } | |
300 | } | |
301 | ||
302 | impl<'a, F : Float, R, C, const N : usize> Differentiable<Loc<F, N>> | |
303 | for Convolution<CubeIndicator<R, N>, HatConv<C, N>> | |
304 | where R : Constant<Type=F>, | |
305 | C : Constant<Type=F> { | |
306 | ||
307 | type Output = Loc<F, N>; | |
308 | ||
309 | #[inline] | |
310 | fn differential(&self, y : Loc<F, N>) -> Loc<F, N> { | |
311 | self.differential(&y) | |
312 | } | |
313 | } | |
314 | ||
315 | /// Integrate $f$, whose support is $[c, d]$, on $[a, b]$. | |
316 | /// If $b > d$, add $g()$ to the result. | |
317 | #[inline] | |
318 | #[replace_float_literals(F::cast_from(literal))] | |
319 | fn i<F: Float>(a : F, b : F, c : F, d : F, f : impl Fn(F) -> F, | |
320 | g : impl Fn() -> F) -> F { | |
321 | if b < c { | |
322 | 0.0 | |
323 | } else if b <= d { | |
324 | if a <= c { | |
325 | f(b) - f(c) | |
326 | } else { | |
327 | f(b) - f(a) | |
328 | } | |
329 | } else /* b > d */ { | |
330 | g() + if a <= c { | |
331 | f(d) - f(c) | |
332 | } else if a < d { | |
333 | f(d) - f(a) | |
334 | } else { | |
335 | 0.0 | |
336 | } | |
337 | } | |
338 | } | |
0 | 339 | |
340 | #[replace_float_literals(F::cast_from(literal))] | |
341 | impl<F : Float, C, R, const N : usize> Convolution<CubeIndicator<R, N>, HatConv<C, N>> | |
342 | where R : Constant<Type=F>, | |
343 | C : Constant<Type=F> { | |
32 | 344 | |
345 | /// Calculates the value of the 1D hat convolution further convolved by a interval indicator. | |
346 | /// As both functions are piecewise polynomials, this is implemented by explicit integral over | |
347 | /// all subintervals of polynomiality of the cube indicator, using easily formed | |
348 | /// antiderivatives. | |
0 | 349 | #[inline] |
350 | pub fn value_1d_σ1(&self, x : F, β : F) -> F { | |
351 | // The integration interval | |
352 | let a = x - β; | |
353 | let b = x + β; | |
354 | ||
355 | #[inline] | |
356 | fn pow4<F : Float>(x : F) -> F { | |
357 | let y = x * x; | |
358 | y * y | |
359 | } | |
360 | ||
361 | // Observe the factor 1/6 at the front from the antiderivatives below. | |
362 | // The factor 4 is from normalisation of the original function. | |
363 | (4.0/6.0) * i(a, b, -1.0, -0.5, | |
32 | 364 | // (2/3) (y+1)^3 on -1 < y ≤ -1/2 |
0 | 365 | // The antiderivative is (2/12)(y+1)^4 = (1/6)(y+1)^4 |
366 | |y| pow4(y+1.0), | |
367 | || i(a, b, -0.5, 0.0, | |
368 | // -2 y^3 - 2 y^2 + 1/3 on -1/2 < y ≤ 0 | |
369 | // The antiderivative is -1/2 y^4 - 2/3 y^3 + 1/3 y | |
370 | |y| y*(-y*y*(y*3.0 + 4.0) + 2.0), | |
371 | || i(a, b, 0.0, 0.5, | |
372 | // 2 y^3 - 2 y^2 + 1/3 on 0 < y < 1/2 | |
373 | // The antiderivative is 1/2 y^4 - 2/3 y^3 + 1/3 y | |
374 | |y| y*(y*y*(y*3.0 - 4.0) + 2.0), | |
375 | || i(a, b, 0.5, 1.0, | |
376 | // -(2/3) (y-1)^3 on 1/2 < y ≤ 1 | |
377 | // The antiderivative is -(2/12)(y-1)^4 = -(1/6)(y-1)^4 | |
378 | |y| -pow4(y-1.0), | |
379 | || 0.0 | |
380 | ) | |
381 | ) | |
382 | ) | |
383 | ) | |
384 | } | |
32 | 385 | |
386 | /// Calculates the derivative of the 1D hat convolution further convolved by a interval | |
387 | /// indicator. The implementation is similar to [`Self::value_1d_σ1`], using the fact that | |
388 | /// $(θ * ψ)' = θ * ψ'$. | |
389 | #[inline] | |
390 | pub fn diff_1d_σ1(&self, x : F, β : F) -> F { | |
391 | // The integration interval | |
392 | let a = x - β; | |
393 | let b = x + β; | |
394 | ||
395 | // The factor 4 is from normalisation of the original function. | |
396 | 4.0 * i(a, b, -1.0, -0.5, | |
397 | // (2/3) (y+1)^3 on -1 < y ≤ -1/2 | |
398 | |y| (2.0/3.0) * (y + 1.0).powi(3), | |
399 | || i(a, b, -0.5, 0.0, | |
400 | // -2 y^3 - 2 y^2 + 1/3 on -1/2 < y ≤ 0 | |
401 | |y| -2.0*(y - 1.0) * y * y + (1.0/3.0), | |
402 | || i(a, b, 0.0, 0.5, | |
403 | // 2 y^3 - 2 y^2 + 1/3 on 0 < y < 1/2 | |
404 | |y| 2.0*(y - 1.0) * y * y + (1.0/3.0), | |
405 | || i(a, b, 0.5, 1.0, | |
406 | // -(2/3) (y-1)^3 on 1/2 < y ≤ 1 | |
407 | |y| -(2.0/3.0) * (y - 1.0).powi(3), | |
408 | || 0.0 | |
409 | ) | |
410 | ) | |
411 | ) | |
412 | ) | |
413 | } | |
0 | 414 | } |
415 | ||
416 | impl<F : Float, R, C, const N : usize> | |
417 | Convolution<CubeIndicator<R, N>, HatConv<C, N>> | |
418 | where R : Constant<Type=F>, | |
419 | C : Constant<Type=F> { | |
420 | ||
421 | #[inline] | |
422 | fn get_r(&self) -> F { | |
423 | let Convolution(ref ind, ref hatconv) = self; | |
424 | ind.r.value() + hatconv.radius() | |
425 | } | |
426 | } | |
427 | ||
428 | impl<F : Float, R, C, const N : usize> Support<F, N> | |
429 | for Convolution<CubeIndicator<R, N>, HatConv<C, N>> | |
430 | where R : Constant<Type=F>, | |
431 | C : Constant<Type=F> { | |
432 | ||
433 | #[inline] | |
434 | fn support_hint(&self) -> Cube<F, N> { | |
435 | let r = self.get_r(); | |
436 | array_init(|| [-r, r]).into() | |
437 | } | |
438 | ||
439 | #[inline] | |
440 | fn in_support(&self, y : &Loc<F, N>) -> bool { | |
441 | let r = self.get_r(); | |
442 | y.iter().all(|x| x.abs() <= r) | |
443 | } | |
444 | ||
445 | #[inline] | |
446 | fn bisection_hint(&self, cube : &Cube<F, N>) -> [Option<F>; N] { | |
447 | // It is not difficult to verify that [`HatConv`] is C^2. | |
448 | // Therefore, so is [`Convolution<CubeIndicator<R, N>, HatConv<C, N>>`] so that a finer | |
449 | // subdivision for the hint than this is not particularly useful. | |
450 | let r = self.get_r(); | |
451 | cube.map(|c, d| symmetric_peak_hint(r, c, d)) | |
452 | } | |
453 | } | |
454 | ||
455 | impl<F : Float, R, C, const N : usize> GlobalAnalysis<F, Bounds<F>> | |
456 | for Convolution<CubeIndicator<R, N>, HatConv<C, N>> | |
457 | where R : Constant<Type=F>, | |
458 | C : Constant<Type=F> { | |
459 | #[inline] | |
460 | fn global_analysis(&self) -> Bounds<F> { | |
461 | Bounds(F::ZERO, self.apply(Loc::ORIGIN)) | |
462 | } | |
463 | } | |
464 | ||
465 | impl<F : Float, R, C, const N : usize> LocalAnalysis<F, Bounds<F>, N> | |
466 | for Convolution<CubeIndicator<R, N>, HatConv<C, N>> | |
467 | where R : Constant<Type=F>, | |
468 | C : Constant<Type=F> { | |
469 | #[inline] | |
470 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
471 | // The function is maximised/minimised where the absolute value is minimised/maximised. | |
472 | let lower = self.apply(cube.maxnorm_point()); | |
473 | let upper = self.apply(cube.minnorm_point()); | |
474 | //assert!(upper >= lower); | |
475 | if upper < lower { | |
476 | let Convolution(ref ind, ref hatconv) = self; | |
477 | let β = ind.r.value(); | |
478 | let σ = hatconv.radius(); | |
479 | eprintln!("WARNING: Hat convolution {β} {σ} upper bound {upper} < lower bound {lower} on {cube:?} with min-norm point {:?} and max-norm point {:?}", cube.minnorm_point(), cube.maxnorm_point()); | |
480 | Bounds(upper, lower) | |
481 | } else { | |
482 | Bounds(lower, upper) | |
483 | } | |
484 | } | |
485 | } | |
486 | ||
487 | ||
488 | /// This [`BoundedBy`] implementation bounds $u * u$ by $(ψ * ψ) u$ for $u$ a hat convolution and | |
489 | /// $ψ = χ_{[-a,a]^N}$ for some $a>0$. | |
490 | /// | |
491 | /// This is based on the general formula for bounding $(uχ) * (uχ)$ by $(ψ * ψ) u$, | |
492 | /// where we take $ψ = χ_{[-a,a]^N}$ and $χ = χ_{[-σ,σ]^N}$ for $σ$ the width of the hat | |
493 | /// convolution. | |
494 | #[replace_float_literals(F::cast_from(literal))] | |
495 | impl<F, C, S, const N : usize> | |
496 | BoundedBy<F, SupportProductFirst<AutoConvolution<CubeIndicator<S, N>>, HatConv<C, N>>> | |
497 | for AutoConvolution<HatConv<C, N>> | |
498 | where F : Float, | |
499 | C : Constant<Type=F>, | |
500 | S : Constant<Type=F> { | |
501 | ||
502 | fn bounding_factor( | |
503 | &self, | |
504 | kernel : &SupportProductFirst<AutoConvolution<CubeIndicator<S, N>>, HatConv<C, N>> | |
505 | ) -> Option<F> { | |
506 | // We use the comparison $ℱ[𝒜(ψ v)] ≤ L_1 ℱ[𝒜(ψ)u] ⟺ I_{v̂} v̂ ≤ L_1 û$ with | |
507 | // $ψ = χ_{[-w, w]}$ satisfying $supp v ⊂ [-w, w]$, i.e. $w ≥ σ$. Here $v̂ = ℱ[v]$ and | |
508 | // $I_{v̂} = ∫ v̂ d ξ. For this relationship to be valid, we need $v̂ ≥ 0$, which is guaranteed | |
509 | // by $v̂ = u_σ$ being an autoconvolution. With $u = v$, therefore $L_1 = I_v̂ = ∫ u_σ(ξ) d ξ$. | |
510 | let SupportProductFirst(AutoConvolution(ref ind), hatconv2) = kernel; | |
511 | let σ = self.0.radius(); | |
512 | let a = ind.r.value(); | |
513 | let bounding_1d = 4.0 / (3.0 * σ); | |
514 | ||
515 | // Check that the cutting indicator of the comparison | |
516 | // `SupportProductFirst<AutoConvolution<CubeIndicator<S, N>>, HatConv<C, N>>` | |
517 | // is wide enough, and that the hat convolution has the same radius as ours. | |
518 | if σ <= a && hatconv2 == &self.0 { | |
519 | Some(bounding_1d.powi(N as i32)) | |
520 | } else { | |
521 | // We cannot compare | |
522 | None | |
523 | } | |
524 | } | |
525 | } | |
526 | ||
527 | /// This [`BoundedBy`] implementation bounds $u * u$ by $u$ for $u$ a hat convolution. | |
528 | /// | |
529 | /// This is based on Example 3.3 in the manuscript. | |
530 | #[replace_float_literals(F::cast_from(literal))] | |
531 | impl<F, C, const N : usize> | |
532 | BoundedBy<F, HatConv<C, N>> | |
533 | for AutoConvolution<HatConv<C, N>> | |
534 | where F : Float, | |
535 | C : Constant<Type=F> { | |
536 | ||
537 | /// Returns an estimate of the factor $L_1$. | |
538 | /// | |
539 | /// Returns `None` if `kernel` does not have the same width as hat convolution that `self` | |
540 | /// is based on. | |
541 | fn bounding_factor( | |
542 | &self, | |
543 | kernel : &HatConv<C, N> | |
544 | ) -> Option<F> { | |
545 | if kernel == &self.0 { | |
546 | Some(1.0) | |
547 | } else { | |
548 | // We cannot compare | |
549 | None | |
550 | } | |
551 | } | |
552 | } | |
553 | ||
554 | #[cfg(test)] | |
555 | mod tests { | |
556 | use alg_tools::lingrid::linspace; | |
557 | use alg_tools::mapping::Apply; | |
558 | use alg_tools::norms::Linfinity; | |
559 | use alg_tools::loc::Loc; | |
560 | use crate::kernels::{BallIndicator, CubeIndicator, Convolution}; | |
561 | use super::HatConv; | |
562 | ||
563 | /// Tests numerically that [`HatConv<f64, 1>`] is monotone. | |
564 | #[test] | |
565 | fn hatconv_monotonicity() { | |
566 | let grid = linspace(0.0, 1.0, 100000); | |
567 | let hatconv : HatConv<f64, 1> = HatConv{ radius : 1.0 }; | |
568 | let mut vals = grid.into_iter().map(|t| hatconv.apply(Loc::from(t))); | |
569 | let first = vals.next().unwrap(); | |
570 | let monotone = vals.fold((first, true), |(prev, ok), t| (prev, ok && prev >= t)).1; | |
571 | assert!(monotone); | |
572 | } | |
573 | ||
574 | /// Tests numerically that [`Convolution<CubeIndicator<f64, 1>, HatConv<f64, 1>>`] is monotone. | |
575 | #[test] | |
576 | fn convolution_cubeind_hatconv_monotonicity() { | |
577 | let grid = linspace(-2.0, 0.0, 100000); | |
578 | let hatconv : Convolution<CubeIndicator<f64, 1>, HatConv<f64, 1>> | |
579 | = Convolution(BallIndicator { r : 0.5, exponent : Linfinity }, | |
580 | HatConv{ radius : 1.0 } ); | |
581 | let mut vals = grid.into_iter().map(|t| hatconv.apply(Loc::from(t))); | |
582 | let first = vals.next().unwrap(); | |
583 | let monotone = vals.fold((first, true), |(prev, ok), t| (prev, ok && prev <= t)).1; | |
584 | assert!(monotone); | |
585 | ||
586 | let grid = linspace(0.0, 2.0, 100000); | |
587 | let hatconv : Convolution<CubeIndicator<f64, 1>, HatConv<f64, 1>> | |
588 | = Convolution(BallIndicator { r : 0.5, exponent : Linfinity }, | |
589 | HatConv{ radius : 1.0 } ); | |
590 | let mut vals = grid.into_iter().map(|t| hatconv.apply(Loc::from(t))); | |
591 | let first = vals.next().unwrap(); | |
592 | let monotone = vals.fold((first, true), |(prev, ok), t| (prev, ok && prev >= t)).1; | |
593 | assert!(monotone); | |
594 | } | |
595 | } |