Tue, 01 Aug 2023 10:25:09 +0300
Simplify and fix cut gaussian indicator convolution scaling
0 | 1 | //! Implementation of the gaussian kernel. |
2 | ||
3 | use float_extras::f64::erf; | |
4 | use numeric_literals::replace_float_literals; | |
5 | use serde::Serialize; | |
6 | use alg_tools::types::*; | |
7 | use alg_tools::euclidean::Euclidean; | |
8 | use alg_tools::norms::*; | |
9 | use alg_tools::loc::Loc; | |
10 | use alg_tools::sets::Cube; | |
11 | use alg_tools::bisection_tree::{ | |
12 | Support, | |
13 | Constant, | |
14 | Bounds, | |
15 | LocalAnalysis, | |
16 | GlobalAnalysis, | |
17 | Weighted, | |
18 | Bounded, | |
19 | }; | |
20 | use alg_tools::mapping::Apply; | |
21 | use alg_tools::maputil::array_init; | |
22 | ||
23 | use crate::fourier::Fourier; | |
24 | use super::base::*; | |
25 | use super::ball_indicator::CubeIndicator; | |
26 | ||
27 | /// Storage presentation of the the anisotropic gaussian kernel of `variance` $σ^2$. | |
28 | /// | |
29 | /// This is the function $f(x) = C e^{-\\|x\\|\_2^2/(2σ^2)}$ for $x ∈ ℝ^N$ | |
30 | /// with $C=1/(2πσ^2)^{N/2}$. | |
31 | #[derive(Copy,Clone,Debug,Serialize,Eq)] | |
32 | pub struct Gaussian<S : Constant, const N : usize> { | |
33 | /// The variance $σ^2$. | |
34 | pub variance : S, | |
35 | } | |
36 | ||
37 | impl<S1, S2, const N : usize> PartialEq<Gaussian<S2, N>> for Gaussian<S1, N> | |
38 | where S1 : Constant, | |
39 | S2 : Constant<Type=S1::Type> { | |
40 | fn eq(&self, other : &Gaussian<S2, N>) -> bool { | |
41 | self.variance.value() == other.variance.value() | |
42 | } | |
43 | } | |
44 | ||
45 | impl<S1, S2, const N : usize> PartialOrd<Gaussian<S2, N>> for Gaussian<S1, N> | |
46 | where S1 : Constant, | |
47 | S2 : Constant<Type=S1::Type> { | |
48 | ||
49 | fn partial_cmp(&self, other : &Gaussian<S2, N>) -> Option<std::cmp::Ordering> { | |
50 | // A gaussian is ≤ another gaussian if the Fourier transforms satisfy the | |
51 | // corresponding inequality. That in turns holds if and only if the variances | |
52 | // satisfy the opposite inequality. | |
53 | let σ1sq = self.variance.value(); | |
54 | let σ2sq = other.variance.value(); | |
55 | σ2sq.partial_cmp(&σ1sq) | |
56 | } | |
57 | } | |
58 | ||
59 | ||
60 | #[replace_float_literals(S::Type::cast_from(literal))] | |
61 | impl<'a, S, const N : usize> Apply<&'a Loc<S::Type, N>> for Gaussian<S, N> | |
62 | where S : Constant { | |
63 | type Output = S::Type; | |
64 | // This is not normalised to neither to have value 1 at zero or integral 1 | |
65 | // (unless the cut-off ε=0). | |
66 | #[inline] | |
67 | fn apply(&self, x : &'a Loc<S::Type, N>) -> Self::Output { | |
68 | let d_squared = x.norm2_squared(); | |
69 | let σ2 = self.variance.value(); | |
70 | let scale = self.scale(); | |
71 | (-d_squared / (2.0 * σ2)).exp() / scale | |
72 | } | |
73 | } | |
74 | ||
75 | impl<S, const N : usize> Apply<Loc<S::Type, N>> for Gaussian<S, N> | |
76 | where S : Constant { | |
77 | type Output = S::Type; | |
78 | // This is not normalised to neither to have value 1 at zero or integral 1 | |
79 | // (unless the cut-off ε=0). | |
80 | #[inline] | |
81 | fn apply(&self, x : Loc<S::Type, N>) -> Self::Output { | |
82 | self.apply(&x) | |
83 | } | |
84 | } | |
85 | ||
86 | ||
87 | #[replace_float_literals(S::Type::cast_from(literal))] | |
88 | impl<'a, S, const N : usize> Gaussian<S, N> | |
89 | where S : Constant { | |
90 | ||
91 | /// Returns the (reciprocal) scaling constant $1/C=(2πσ^2)^{N/2}$. | |
92 | #[inline] | |
93 | pub fn scale(&self) -> S::Type { | |
94 | let π = S::Type::PI; | |
95 | let σ2 = self.variance.value(); | |
96 | (2.0*π*σ2).powi(N as i32).sqrt() | |
97 | } | |
98 | } | |
99 | ||
100 | impl<'a, S, const N : usize> Support<S::Type, N> for Gaussian<S, N> | |
101 | where S : Constant { | |
102 | #[inline] | |
103 | fn support_hint(&self) -> Cube<S::Type,N> { | |
104 | array_init(|| [S::Type::NEG_INFINITY, S::Type::INFINITY]).into() | |
105 | } | |
106 | ||
107 | #[inline] | |
108 | fn in_support(&self, _x : &Loc<S::Type,N>) -> bool { | |
109 | true | |
110 | } | |
111 | } | |
112 | ||
113 | #[replace_float_literals(S::Type::cast_from(literal))] | |
114 | impl<S, const N : usize> GlobalAnalysis<S::Type, Bounds<S::Type>> for Gaussian<S, N> | |
115 | where S : Constant { | |
116 | #[inline] | |
117 | fn global_analysis(&self) -> Bounds<S::Type> { | |
118 | Bounds(0.0, 1.0/self.scale()) | |
119 | } | |
120 | } | |
121 | ||
122 | impl<S, const N : usize> LocalAnalysis<S::Type, Bounds<S::Type>, N> for Gaussian<S, N> | |
123 | where S : Constant { | |
124 | #[inline] | |
125 | fn local_analysis(&self, cube : &Cube<S::Type, N>) -> Bounds<S::Type> { | |
126 | // The function is maximised/minimised where the 2-norm is minimised/maximised. | |
127 | let lower = self.apply(cube.maxnorm_point()); | |
128 | let upper = self.apply(cube.minnorm_point()); | |
129 | Bounds(lower, upper) | |
130 | } | |
131 | } | |
132 | ||
133 | #[replace_float_literals(C::Type::cast_from(literal))] | |
134 | impl<'a, C : Constant, const N : usize> Norm<C::Type, L1> | |
135 | for Gaussian<C, N> { | |
136 | #[inline] | |
137 | fn norm(&self, _ : L1) -> C::Type { | |
138 | 1.0 | |
139 | } | |
140 | } | |
141 | ||
142 | #[replace_float_literals(C::Type::cast_from(literal))] | |
143 | impl<'a, C : Constant, const N : usize> Norm<C::Type, Linfinity> | |
144 | for Gaussian<C, N> { | |
145 | #[inline] | |
146 | fn norm(&self, _ : Linfinity) -> C::Type { | |
147 | self.bounds().upper() | |
148 | } | |
149 | } | |
150 | ||
151 | #[replace_float_literals(C::Type::cast_from(literal))] | |
152 | impl<'a, C : Constant, const N : usize> Fourier<C::Type> | |
153 | for Gaussian<C, N> { | |
154 | type Domain = Loc<C::Type, N>; | |
155 | type Transformed = Weighted<Gaussian<C::Type, N>, C::Type>; | |
156 | ||
157 | #[inline] | |
158 | fn fourier(&self) -> Self::Transformed { | |
159 | let π = C::Type::PI; | |
160 | let σ2 = self.variance.value(); | |
161 | let g = Gaussian { variance : 1.0 / (4.0*π*π*σ2) }; | |
162 | g.weigh(g.scale()) | |
163 | } | |
164 | } | |
165 | ||
166 | /// Representation of the “cut” gaussian $f χ\_{[-a, a]^n}$ | |
167 | /// where $a>0$ and $f$ is a gaussian kernel on $ℝ^n$. | |
168 | pub type BasicCutGaussian<C, S, const N : usize> = SupportProductFirst<CubeIndicator<C, N>, | |
169 | Gaussian<S, N>>; | |
170 | ||
171 | ||
172 | /// This implements $χ\_{[-b, b]^n} \* (f χ\_{[-a, a]^n})$ | |
173 | /// where $a,b>0$ and $f$ is a gaussian kernel on $ℝ^n$. | |
174 | #[replace_float_literals(F::cast_from(literal))] | |
175 | impl<'a, F : Float, R, C, S, const N : usize> Apply<&'a Loc<F, N>> | |
176 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
177 | where R : Constant<Type=F>, | |
178 | C : Constant<Type=F>, | |
179 | S : Constant<Type=F> { | |
180 | ||
181 | type Output = F; | |
182 | ||
183 | #[inline] | |
184 | fn apply(&self, y : &'a Loc<F, N>) -> F { | |
185 | let Convolution(ref ind, | |
186 | SupportProductFirst(ref cut, | |
187 | ref gaussian)) = self; | |
188 | let a = cut.r.value(); | |
189 | let b = ind.r.value(); | |
190 | let σ = gaussian.variance.value().sqrt(); | |
191 | let t = F::SQRT_2 * σ; | |
31
6105b5cd8d89
Simplify and fix cut gaussian indicator convolution scaling
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
192 | let c = 0.5; // 1/(σ√(2π) * σ√(π/2) = 1/2 |
0 | 193 | |
194 | // This is just a product of one-dimensional versions | |
31
6105b5cd8d89
Simplify and fix cut gaussian indicator convolution scaling
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
195 | y.product_map(|x| { |
0 | 196 | let c1 = -(a.min(b + x)); //(-a).max(-x-b); |
197 | let c2 = a.min(b - x); | |
198 | if c1 >= c2 { | |
199 | 0.0 | |
200 | } else { | |
201 | let e1 = F::cast_from(erf((c1 / t).as_())); | |
202 | let e2 = F::cast_from(erf((c2 / t).as_())); | |
203 | debug_assert!(e2 >= e1); | |
204 | c * (e2 - e1) | |
205 | } | |
31
6105b5cd8d89
Simplify and fix cut gaussian indicator convolution scaling
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
206 | }) |
0 | 207 | } |
208 | } | |
209 | ||
210 | impl<F : Float, R, C, S, const N : usize> Apply<Loc<F, N>> | |
211 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
212 | where R : Constant<Type=F>, | |
213 | C : Constant<Type=F>, | |
214 | S : Constant<Type=F> { | |
215 | ||
216 | type Output = F; | |
217 | ||
218 | #[inline] | |
219 | fn apply(&self, y : Loc<F, N>) -> F { | |
220 | self.apply(&y) | |
221 | } | |
222 | } | |
223 | ||
224 | impl<F : Float, R, C, S, const N : usize> | |
225 | Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
226 | where R : Constant<Type=F>, | |
227 | C : Constant<Type=F>, | |
228 | S : Constant<Type=F> { | |
229 | ||
230 | #[inline] | |
231 | fn get_r(&self) -> F { | |
232 | let Convolution(ref ind, | |
233 | SupportProductFirst(ref cut, ..)) = self; | |
234 | ind.r.value() + cut.r.value() | |
235 | } | |
236 | } | |
237 | ||
238 | impl<F : Float, R, C, S, const N : usize> Support<F, N> | |
239 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
240 | where R : Constant<Type=F>, | |
241 | C : Constant<Type=F>, | |
242 | S : Constant<Type=F> { | |
243 | #[inline] | |
244 | fn support_hint(&self) -> Cube<F, N> { | |
245 | let r = self.get_r(); | |
246 | array_init(|| [-r, r]).into() | |
247 | } | |
248 | ||
249 | #[inline] | |
250 | fn in_support(&self, y : &Loc<F, N>) -> bool { | |
251 | let r = self.get_r(); | |
252 | y.iter().all(|x| x.abs() <= r) | |
253 | } | |
254 | ||
255 | #[inline] | |
256 | fn bisection_hint(&self, cube : &Cube<F, N>) -> [Option<F>; N] { | |
257 | let r = self.get_r(); | |
258 | // From c1 = -a.min(b + x) and c2 = a.min(b - x) with c_1 < c_2, | |
259 | // solve bounds for x. that is 0 ≤ a.min(b + x) + a.min(b - x). | |
260 | // If b + x ≤ a and b - x ≤ a, the sum is 2b ≥ 0. | |
261 | // If b + x ≥ a and b - x ≥ a, the sum is 2a ≥ 0. | |
262 | // If b + x ≤ a and b - x ≥ a, the sum is b + x + a ⟹ need x ≥ -a - b = -r. | |
263 | // If b + x ≥ a and b - x ≤ a, the sum is a + b - x ⟹ need x ≤ a + b = r. | |
264 | cube.map(|c, d| symmetric_peak_hint(r, c, d)) | |
265 | } | |
266 | } | |
267 | ||
268 | impl<F : Float, R, C, S, const N : usize> GlobalAnalysis<F, Bounds<F>> | |
269 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
270 | where R : Constant<Type=F>, | |
271 | C : Constant<Type=F>, | |
272 | S : Constant<Type=F> { | |
273 | #[inline] | |
274 | fn global_analysis(&self) -> Bounds<F> { | |
275 | Bounds(F::ZERO, self.apply(Loc::ORIGIN)) | |
276 | } | |
277 | } | |
278 | ||
279 | impl<F : Float, R, C, S, const N : usize> LocalAnalysis<F, Bounds<F>, N> | |
280 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
281 | where R : Constant<Type=F>, | |
282 | C : Constant<Type=F>, | |
283 | S : Constant<Type=F> { | |
284 | #[inline] | |
285 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
286 | // The function is maximised/minimised where the absolute value is minimised/maximised. | |
287 | let lower = self.apply(cube.maxnorm_point()); | |
288 | let upper = self.apply(cube.minnorm_point()); | |
289 | Bounds(lower, upper) | |
290 | } | |
291 | } | |
292 |