Tue, 06 Dec 2022 14:12:20 +0200
v1.0.0-pre-arxiv (missing arXiv links)
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 | }; | |
17 | use alg_tools::mapping::Apply; | |
18 | use alg_tools::maputil::array_init; | |
19 | ||
20 | use super::base::*; | |
21 | use super::ball_indicator::CubeIndicator; | |
22 | ||
23 | /// Hat convolution kernel. | |
24 | /// | |
25 | /// This struct represents the function | |
26 | /// $$ | |
27 | /// f(x\_1, …, x\_n) = \prod\_{i=1}^n \frac{4}{σ} (h\*h)(x\_i/σ) | |
28 | /// $$ | |
29 | /// where the “hat function” $h(y)= \max(0, 1 - |2y|)$. | |
30 | /// The factor $4/σ$ normalises $∫ f d x = 1$. | |
31 | /// We have | |
32 | /// $$ | |
33 | /// (h*h)(y) = | |
34 | /// \begin{cases} | |
35 | /// \frac{2}{3} (y+1)^3 & -1<y\leq -\frac{1}{2}, \\\\ | |
36 | /// -2 y^3-2 y^2+\frac{1}{3} & -\frac{1}{2}<y\leq 0, \\\\ | |
37 | /// 2 y^3-2 y^2+\frac{1}{3} & 0<y<\frac{1}{2}, \\\\ | |
38 | /// -\frac{2}{3} (y-1)^3 & \frac{1}{2}\leq y<1. \\\\ | |
39 | /// \end{cases} | |
40 | /// $$ | |
41 | #[derive(Copy,Clone,Debug,Serialize,Eq)] | |
42 | pub struct HatConv<S : Constant, const N : usize> { | |
43 | /// The parameter $σ$ of the kernel. | |
44 | pub radius : S, | |
45 | } | |
46 | ||
47 | impl<S1, S2, const N : usize> PartialEq<HatConv<S2, N>> for HatConv<S1, N> | |
48 | where S1 : Constant, | |
49 | S2 : Constant<Type=S1::Type> { | |
50 | fn eq(&self, other : &HatConv<S2, N>) -> bool { | |
51 | self.radius.value() == other.radius.value() | |
52 | } | |
53 | } | |
54 | ||
55 | impl<'a, S, const N : usize> HatConv<S, N> where S : Constant { | |
56 | /// Returns the $σ$ parameter of the kernel. | |
57 | #[inline] | |
58 | pub fn radius(&self) -> S::Type { | |
59 | self.radius.value() | |
60 | } | |
61 | } | |
62 | ||
63 | impl<'a, S, const N : usize> Apply<&'a Loc<S::Type, N>> for HatConv<S, N> | |
64 | where S : Constant { | |
65 | type Output = S::Type; | |
66 | #[inline] | |
67 | fn apply(&self, y : &'a Loc<S::Type, N>) -> Self::Output { | |
68 | let σ = self.radius(); | |
69 | y.product_map(|x| { | |
70 | self.value_1d_σ1(x / σ) / σ | |
71 | }) | |
72 | } | |
73 | } | |
74 | ||
75 | impl<'a, S, const N : usize> Apply<Loc<S::Type, N>> for HatConv<S, N> | |
76 | where S : Constant { | |
77 | type Output = S::Type; | |
78 | #[inline] | |
79 | fn apply(&self, y : Loc<S::Type, N>) -> Self::Output { | |
80 | self.apply(&y) | |
81 | } | |
82 | } | |
83 | ||
84 | ||
85 | #[replace_float_literals(S::Type::cast_from(literal))] | |
86 | impl<'a, F : Float, S, const N : usize> HatConv<S, N> | |
87 | where S : Constant<Type=F> { | |
88 | /// Computes the value of the kernel for $n=1$ with $σ=1$. | |
89 | #[inline] | |
90 | fn value_1d_σ1(&self, x : F) -> F { | |
91 | let y = x.abs(); | |
92 | if y >= 1.0 { | |
93 | 0.0 | |
94 | } else if y > 0.5 { | |
95 | - (8.0/3.0) * (y - 1.0).powi(3) | |
96 | } else /* 0 ≤ y ≤ 0.5 */ { | |
97 | (4.0/3.0) + 8.0 * y * y * (y - 1.0) | |
98 | } | |
99 | } | |
100 | } | |
101 | ||
102 | impl<'a, S, const N : usize> Support<S::Type, N> for HatConv<S, N> | |
103 | where S : Constant { | |
104 | #[inline] | |
105 | fn support_hint(&self) -> Cube<S::Type,N> { | |
106 | let σ = self.radius(); | |
107 | array_init(|| [-σ, σ]).into() | |
108 | } | |
109 | ||
110 | #[inline] | |
111 | fn in_support(&self, y : &Loc<S::Type,N>) -> bool { | |
112 | let σ = self.radius(); | |
113 | y.iter().all(|x| x.abs() <= σ) | |
114 | } | |
115 | ||
116 | #[inline] | |
117 | fn bisection_hint(&self, cube : &Cube<S::Type, N>) -> [Option<S::Type>; N] { | |
118 | let σ = self.radius(); | |
119 | cube.map(|c, d| symmetric_peak_hint(σ, c, d)) | |
120 | } | |
121 | } | |
122 | ||
123 | #[replace_float_literals(S::Type::cast_from(literal))] | |
124 | impl<S, const N : usize> GlobalAnalysis<S::Type, Bounds<S::Type>> for HatConv<S, N> | |
125 | where S : Constant { | |
126 | #[inline] | |
127 | fn global_analysis(&self) -> Bounds<S::Type> { | |
128 | Bounds(0.0, self.apply(Loc::ORIGIN)) | |
129 | } | |
130 | } | |
131 | ||
132 | impl<S, const N : usize> LocalAnalysis<S::Type, Bounds<S::Type>, N> for HatConv<S, N> | |
133 | where S : Constant { | |
134 | #[inline] | |
135 | fn local_analysis(&self, cube : &Cube<S::Type, N>) -> Bounds<S::Type> { | |
136 | // The function is maximised/minimised where the 2-norm is minimised/maximised. | |
137 | let lower = self.apply(cube.maxnorm_point()); | |
138 | let upper = self.apply(cube.minnorm_point()); | |
139 | Bounds(lower, upper) | |
140 | } | |
141 | } | |
142 | ||
143 | #[replace_float_literals(C::Type::cast_from(literal))] | |
144 | impl<'a, C : Constant, const N : usize> Norm<C::Type, L1> | |
145 | for HatConv<C, N> { | |
146 | #[inline] | |
147 | fn norm(&self, _ : L1) -> C::Type { | |
148 | 1.0 | |
149 | } | |
150 | } | |
151 | ||
152 | #[replace_float_literals(C::Type::cast_from(literal))] | |
153 | impl<'a, C : Constant, const N : usize> Norm<C::Type, Linfinity> | |
154 | for HatConv<C, N> { | |
155 | #[inline] | |
156 | fn norm(&self, _ : Linfinity) -> C::Type { | |
157 | self.bounds().upper() | |
158 | } | |
159 | } | |
160 | ||
161 | #[replace_float_literals(F::cast_from(literal))] | |
162 | impl<'a, F : Float, R, C, const N : usize> Apply<&'a Loc<F, N>> | |
163 | for Convolution<CubeIndicator<R, N>, HatConv<C, N>> | |
164 | where R : Constant<Type=F>, | |
165 | C : Constant<Type=F> { | |
166 | ||
167 | type Output = F; | |
168 | ||
169 | #[inline] | |
170 | fn apply(&self, y : &'a Loc<F, N>) -> F { | |
171 | let Convolution(ref ind, ref hatconv) = self; | |
172 | let β = ind.r.value(); | |
173 | let σ = hatconv.radius(); | |
174 | ||
175 | // This is just a product of one-dimensional versions | |
176 | y.product_map(|x| { | |
177 | // With $u_σ(x) = u_1(x/σ)/σ$ the normalised hat convolution | |
178 | // we have | |
179 | // $$ | |
180 | // [χ_{-β,β} * u_σ](x) | |
181 | // = ∫_{x-β}^{x+β} u_σ(z) d z | |
182 | // = (1/σ)∫_{x-β}^{x+β} u_1(z/σ) d z | |
183 | // = ∫_{(x-β)/σ}^{(x+β)/σ} u_1(z) d z | |
184 | // = [χ_{-β/σ, β/σ} * u_1](x/σ) | |
185 | // $$ | |
186 | self.value_1d_σ1(x / σ, β / σ) | |
187 | }) | |
188 | } | |
189 | } | |
190 | ||
191 | impl<'a, F : Float, R, C, const N : usize> Apply<Loc<F, N>> | |
192 | for Convolution<CubeIndicator<R, N>, HatConv<C, N>> | |
193 | where R : Constant<Type=F>, | |
194 | C : Constant<Type=F> { | |
195 | ||
196 | type Output = F; | |
197 | ||
198 | #[inline] | |
199 | fn apply(&self, y : Loc<F, N>) -> F { | |
200 | self.apply(&y) | |
201 | } | |
202 | } | |
203 | ||
204 | ||
205 | #[replace_float_literals(F::cast_from(literal))] | |
206 | impl<F : Float, C, R, const N : usize> Convolution<CubeIndicator<R, N>, HatConv<C, N>> | |
207 | where R : Constant<Type=F>, | |
208 | C : Constant<Type=F> { | |
209 | #[inline] | |
210 | pub fn value_1d_σ1(&self, x : F, β : F) -> F { | |
211 | // The integration interval | |
212 | let a = x - β; | |
213 | let b = x + β; | |
214 | ||
215 | #[inline] | |
216 | fn pow4<F : Float>(x : F) -> F { | |
217 | let y = x * x; | |
218 | y * y | |
219 | } | |
220 | ||
221 | /// Integrate $f$, whose support is $[c, d]$, on $[a, b]$. | |
222 | /// If $b > d$, add $g()$ to the result. | |
223 | #[inline] | |
224 | fn i<F: Float>(a : F, b : F, c : F, d : F, f : impl Fn(F) -> F, | |
225 | g : impl Fn() -> F) -> F { | |
226 | if b < c { | |
227 | 0.0 | |
228 | } else if b <= d { | |
229 | if a <= c { | |
230 | f(b) - f(c) | |
231 | } else { | |
232 | f(b) - f(a) | |
233 | } | |
234 | } else /* b > d */ { | |
235 | g() + if a <= c { | |
236 | f(d) - f(c) | |
237 | } else if a < d { | |
238 | f(d) - f(a) | |
239 | } else { | |
240 | 0.0 | |
241 | } | |
242 | } | |
243 | } | |
244 | ||
245 | // Observe the factor 1/6 at the front from the antiderivatives below. | |
246 | // The factor 4 is from normalisation of the original function. | |
247 | (4.0/6.0) * i(a, b, -1.0, -0.5, | |
248 | // (2/3) (y+1)^3 on -1 < y ≤ - 1/2 | |
249 | // The antiderivative is (2/12)(y+1)^4 = (1/6)(y+1)^4 | |
250 | |y| pow4(y+1.0), | |
251 | || i(a, b, -0.5, 0.0, | |
252 | // -2 y^3 - 2 y^2 + 1/3 on -1/2 < y ≤ 0 | |
253 | // The antiderivative is -1/2 y^4 - 2/3 y^3 + 1/3 y | |
254 | |y| y*(-y*y*(y*3.0 + 4.0) + 2.0), | |
255 | || i(a, b, 0.0, 0.5, | |
256 | // 2 y^3 - 2 y^2 + 1/3 on 0 < y < 1/2 | |
257 | // The antiderivative is 1/2 y^4 - 2/3 y^3 + 1/3 y | |
258 | |y| y*(y*y*(y*3.0 - 4.0) + 2.0), | |
259 | || i(a, b, 0.5, 1.0, | |
260 | // -(2/3) (y-1)^3 on 1/2 < y ≤ 1 | |
261 | // The antiderivative is -(2/12)(y-1)^4 = -(1/6)(y-1)^4 | |
262 | |y| -pow4(y-1.0), | |
263 | || 0.0 | |
264 | ) | |
265 | ) | |
266 | ) | |
267 | ) | |
268 | } | |
269 | } | |
270 | ||
271 | impl<F : Float, R, C, const N : usize> | |
272 | Convolution<CubeIndicator<R, N>, HatConv<C, N>> | |
273 | where R : Constant<Type=F>, | |
274 | C : Constant<Type=F> { | |
275 | ||
276 | #[inline] | |
277 | fn get_r(&self) -> F { | |
278 | let Convolution(ref ind, ref hatconv) = self; | |
279 | ind.r.value() + hatconv.radius() | |
280 | } | |
281 | } | |
282 | ||
283 | impl<F : Float, R, C, const N : usize> Support<F, N> | |
284 | for Convolution<CubeIndicator<R, N>, HatConv<C, N>> | |
285 | where R : Constant<Type=F>, | |
286 | C : Constant<Type=F> { | |
287 | ||
288 | #[inline] | |
289 | fn support_hint(&self) -> Cube<F, N> { | |
290 | let r = self.get_r(); | |
291 | array_init(|| [-r, r]).into() | |
292 | } | |
293 | ||
294 | #[inline] | |
295 | fn in_support(&self, y : &Loc<F, N>) -> bool { | |
296 | let r = self.get_r(); | |
297 | y.iter().all(|x| x.abs() <= r) | |
298 | } | |
299 | ||
300 | #[inline] | |
301 | fn bisection_hint(&self, cube : &Cube<F, N>) -> [Option<F>; N] { | |
302 | // It is not difficult to verify that [`HatConv`] is C^2. | |
303 | // Therefore, so is [`Convolution<CubeIndicator<R, N>, HatConv<C, N>>`] so that a finer | |
304 | // subdivision for the hint than this is not particularly useful. | |
305 | let r = self.get_r(); | |
306 | cube.map(|c, d| symmetric_peak_hint(r, c, d)) | |
307 | } | |
308 | } | |
309 | ||
310 | impl<F : Float, R, C, const N : usize> GlobalAnalysis<F, Bounds<F>> | |
311 | for Convolution<CubeIndicator<R, N>, HatConv<C, N>> | |
312 | where R : Constant<Type=F>, | |
313 | C : Constant<Type=F> { | |
314 | #[inline] | |
315 | fn global_analysis(&self) -> Bounds<F> { | |
316 | Bounds(F::ZERO, self.apply(Loc::ORIGIN)) | |
317 | } | |
318 | } | |
319 | ||
320 | impl<F : Float, R, C, const N : usize> LocalAnalysis<F, Bounds<F>, N> | |
321 | for Convolution<CubeIndicator<R, N>, HatConv<C, N>> | |
322 | where R : Constant<Type=F>, | |
323 | C : Constant<Type=F> { | |
324 | #[inline] | |
325 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
326 | // The function is maximised/minimised where the absolute value is minimised/maximised. | |
327 | let lower = self.apply(cube.maxnorm_point()); | |
328 | let upper = self.apply(cube.minnorm_point()); | |
329 | //assert!(upper >= lower); | |
330 | if upper < lower { | |
331 | let Convolution(ref ind, ref hatconv) = self; | |
332 | let β = ind.r.value(); | |
333 | let σ = hatconv.radius(); | |
334 | 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()); | |
335 | Bounds(upper, lower) | |
336 | } else { | |
337 | Bounds(lower, upper) | |
338 | } | |
339 | } | |
340 | } | |
341 | ||
342 | ||
343 | /// This [`BoundedBy`] implementation bounds $u * u$ by $(ψ * ψ) u$ for $u$ a hat convolution and | |
344 | /// $ψ = χ_{[-a,a]^N}$ for some $a>0$. | |
345 | /// | |
346 | /// This is based on the general formula for bounding $(uχ) * (uχ)$ by $(ψ * ψ) u$, | |
347 | /// where we take $ψ = χ_{[-a,a]^N}$ and $χ = χ_{[-σ,σ]^N}$ for $σ$ the width of the hat | |
348 | /// convolution. | |
349 | #[replace_float_literals(F::cast_from(literal))] | |
350 | impl<F, C, S, const N : usize> | |
351 | BoundedBy<F, SupportProductFirst<AutoConvolution<CubeIndicator<S, N>>, HatConv<C, N>>> | |
352 | for AutoConvolution<HatConv<C, N>> | |
353 | where F : Float, | |
354 | C : Constant<Type=F>, | |
355 | S : Constant<Type=F> { | |
356 | ||
357 | fn bounding_factor( | |
358 | &self, | |
359 | kernel : &SupportProductFirst<AutoConvolution<CubeIndicator<S, N>>, HatConv<C, N>> | |
360 | ) -> Option<F> { | |
361 | // We use the comparison $ℱ[𝒜(ψ v)] ≤ L_1 ℱ[𝒜(ψ)u] ⟺ I_{v̂} v̂ ≤ L_1 û$ with | |
362 | // $ψ = χ_{[-w, w]}$ satisfying $supp v ⊂ [-w, w]$, i.e. $w ≥ σ$. Here $v̂ = ℱ[v]$ and | |
363 | // $I_{v̂} = ∫ v̂ d ξ. For this relationship to be valid, we need $v̂ ≥ 0$, which is guaranteed | |
364 | // by $v̂ = u_σ$ being an autoconvolution. With $u = v$, therefore $L_1 = I_v̂ = ∫ u_σ(ξ) d ξ$. | |
365 | let SupportProductFirst(AutoConvolution(ref ind), hatconv2) = kernel; | |
366 | let σ = self.0.radius(); | |
367 | let a = ind.r.value(); | |
368 | let bounding_1d = 4.0 / (3.0 * σ); | |
369 | ||
370 | // Check that the cutting indicator of the comparison | |
371 | // `SupportProductFirst<AutoConvolution<CubeIndicator<S, N>>, HatConv<C, N>>` | |
372 | // is wide enough, and that the hat convolution has the same radius as ours. | |
373 | if σ <= a && hatconv2 == &self.0 { | |
374 | Some(bounding_1d.powi(N as i32)) | |
375 | } else { | |
376 | // We cannot compare | |
377 | None | |
378 | } | |
379 | } | |
380 | } | |
381 | ||
382 | /// This [`BoundedBy`] implementation bounds $u * u$ by $u$ for $u$ a hat convolution. | |
383 | /// | |
384 | /// This is based on Example 3.3 in the manuscript. | |
385 | #[replace_float_literals(F::cast_from(literal))] | |
386 | impl<F, C, const N : usize> | |
387 | BoundedBy<F, HatConv<C, N>> | |
388 | for AutoConvolution<HatConv<C, N>> | |
389 | where F : Float, | |
390 | C : Constant<Type=F> { | |
391 | ||
392 | /// Returns an estimate of the factor $L_1$. | |
393 | /// | |
394 | /// Returns `None` if `kernel` does not have the same width as hat convolution that `self` | |
395 | /// is based on. | |
396 | fn bounding_factor( | |
397 | &self, | |
398 | kernel : &HatConv<C, N> | |
399 | ) -> Option<F> { | |
400 | if kernel == &self.0 { | |
401 | Some(1.0) | |
402 | } else { | |
403 | // We cannot compare | |
404 | None | |
405 | } | |
406 | } | |
407 | } | |
408 | ||
409 | #[cfg(test)] | |
410 | mod tests { | |
411 | use alg_tools::lingrid::linspace; | |
412 | use alg_tools::mapping::Apply; | |
413 | use alg_tools::norms::Linfinity; | |
414 | use alg_tools::loc::Loc; | |
415 | use crate::kernels::{BallIndicator, CubeIndicator, Convolution}; | |
416 | use super::HatConv; | |
417 | ||
418 | /// Tests numerically that [`HatConv<f64, 1>`] is monotone. | |
419 | #[test] | |
420 | fn hatconv_monotonicity() { | |
421 | let grid = linspace(0.0, 1.0, 100000); | |
422 | let hatconv : HatConv<f64, 1> = HatConv{ radius : 1.0 }; | |
423 | let mut vals = grid.into_iter().map(|t| hatconv.apply(Loc::from(t))); | |
424 | let first = vals.next().unwrap(); | |
425 | let monotone = vals.fold((first, true), |(prev, ok), t| (prev, ok && prev >= t)).1; | |
426 | assert!(monotone); | |
427 | } | |
428 | ||
429 | /// Tests numerically that [`Convolution<CubeIndicator<f64, 1>, HatConv<f64, 1>>`] is monotone. | |
430 | #[test] | |
431 | fn convolution_cubeind_hatconv_monotonicity() { | |
432 | let grid = linspace(-2.0, 0.0, 100000); | |
433 | let hatconv : Convolution<CubeIndicator<f64, 1>, HatConv<f64, 1>> | |
434 | = Convolution(BallIndicator { r : 0.5, exponent : Linfinity }, | |
435 | HatConv{ radius : 1.0 } ); | |
436 | let mut vals = grid.into_iter().map(|t| hatconv.apply(Loc::from(t))); | |
437 | let first = vals.next().unwrap(); | |
438 | let monotone = vals.fold((first, true), |(prev, ok), t| (prev, ok && prev <= t)).1; | |
439 | assert!(monotone); | |
440 | ||
441 | let grid = linspace(0.0, 2.0, 100000); | |
442 | let hatconv : Convolution<CubeIndicator<f64, 1>, HatConv<f64, 1>> | |
443 | = Convolution(BallIndicator { r : 0.5, exponent : Linfinity }, | |
444 | HatConv{ radius : 1.0 } ); | |
445 | let mut vals = grid.into_iter().map(|t| hatconv.apply(Loc::from(t))); | |
446 | let first = vals.next().unwrap(); | |
447 | let monotone = vals.fold((first, true), |(prev, ok), t| (prev, ok && prev >= t)).1; | |
448 | assert!(monotone); | |
449 | } | |
450 | } |