Thu, 29 Aug 2024 00:00:00 -0500
Radon FB + sliding improvements
| 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 | }; | |
| 33 | 20 | use alg_tools::mapping::{Apply, Differentiable}; |
| 0 | 21 | use alg_tools::maputil::array_init; |
| 22 | ||
| 33 | 23 | use crate::types::Lipschitz; |
| 0 | 24 | use crate::fourier::Fourier; |
| 25 | use super::base::*; | |
| 26 | use super::ball_indicator::CubeIndicator; | |
| 27 | ||
| 28 | /// Storage presentation of the the anisotropic gaussian kernel of `variance` $σ^2$. | |
| 29 | /// | |
| 30 | /// This is the function $f(x) = C e^{-\\|x\\|\_2^2/(2σ^2)}$ for $x ∈ ℝ^N$ | |
| 31 | /// with $C=1/(2πσ^2)^{N/2}$. | |
| 32 | #[derive(Copy,Clone,Debug,Serialize,Eq)] | |
| 33 | pub struct Gaussian<S : Constant, const N : usize> { | |
| 34 | /// The variance $σ^2$. | |
| 35 | pub variance : S, | |
| 36 | } | |
| 37 | ||
| 38 | impl<S1, S2, const N : usize> PartialEq<Gaussian<S2, N>> for Gaussian<S1, N> | |
| 39 | where S1 : Constant, | |
| 40 | S2 : Constant<Type=S1::Type> { | |
| 41 | fn eq(&self, other : &Gaussian<S2, N>) -> bool { | |
| 42 | self.variance.value() == other.variance.value() | |
| 43 | } | |
| 44 | } | |
| 45 | ||
| 46 | impl<S1, S2, const N : usize> PartialOrd<Gaussian<S2, N>> for Gaussian<S1, N> | |
| 47 | where S1 : Constant, | |
| 48 | S2 : Constant<Type=S1::Type> { | |
| 49 | ||
| 50 | fn partial_cmp(&self, other : &Gaussian<S2, N>) -> Option<std::cmp::Ordering> { | |
| 51 | // A gaussian is ≤ another gaussian if the Fourier transforms satisfy the | |
| 52 | // corresponding inequality. That in turns holds if and only if the variances | |
| 53 | // satisfy the opposite inequality. | |
| 54 | let σ1sq = self.variance.value(); | |
| 55 | let σ2sq = other.variance.value(); | |
| 56 | σ2sq.partial_cmp(&σ1sq) | |
| 57 | } | |
| 58 | } | |
| 59 | ||
| 60 | ||
| 61 | #[replace_float_literals(S::Type::cast_from(literal))] | |
| 62 | impl<'a, S, const N : usize> Apply<&'a Loc<S::Type, N>> for Gaussian<S, N> | |
| 63 | where S : Constant { | |
| 64 | type Output = S::Type; | |
| 65 | // This is not normalised to neither to have value 1 at zero or integral 1 | |
| 66 | // (unless the cut-off ε=0). | |
| 67 | #[inline] | |
| 68 | fn apply(&self, x : &'a Loc<S::Type, N>) -> Self::Output { | |
| 69 | let d_squared = x.norm2_squared(); | |
| 70 | let σ2 = self.variance.value(); | |
| 71 | let scale = self.scale(); | |
| 72 | (-d_squared / (2.0 * σ2)).exp() / scale | |
| 73 | } | |
| 74 | } | |
| 75 | ||
| 76 | impl<S, const N : usize> Apply<Loc<S::Type, N>> for Gaussian<S, N> | |
| 77 | where S : Constant { | |
| 78 | type Output = S::Type; | |
| 79 | #[inline] | |
| 80 | fn apply(&self, x : Loc<S::Type, N>) -> Self::Output { | |
| 81 | self.apply(&x) | |
| 82 | } | |
| 83 | } | |
| 84 | ||
| 33 | 85 | #[replace_float_literals(S::Type::cast_from(literal))] |
| 86 | impl<'a, S, const N : usize> Differentiable<&'a Loc<S::Type, N>> for Gaussian<S, N> | |
| 87 | where S : Constant { | |
| 88 | type Output = Loc<S::Type, N>; | |
| 89 | #[inline] | |
| 90 | fn differential(&self, x : &'a Loc<S::Type, N>) -> Self::Output { | |
| 91 | x * (self.apply(x) / self.variance.value()) | |
| 92 | } | |
| 93 | } | |
| 94 | ||
| 95 | impl<S, const N : usize> Differentiable<Loc<S::Type, N>> for Gaussian<S, N> | |
| 96 | where S : Constant { | |
| 97 | type Output = Loc<S::Type, N>; | |
| 98 | // This is not normalised to neither to have value 1 at zero or integral 1 | |
| 99 | // (unless the cut-off ε=0). | |
| 100 | #[inline] | |
| 101 | fn differential(&self, x : Loc<S::Type, N>) -> Self::Output { | |
| 102 | x * (self.apply(&x) / self.variance.value()) | |
| 103 | } | |
| 104 | } | |
| 105 | ||
| 106 | #[replace_float_literals(S::Type::cast_from(literal))] | |
| 107 | impl<S, const N : usize> Lipschitz<L2> for Gaussian<S, N> | |
| 108 | where S : Constant { | |
| 109 | type FloatType = S::Type; | |
| 110 | fn lipschitz_factor(&self, L2 : L2) -> Option<Self::FloatType> { | |
| 111 | // f(x)=f_1(‖x‖_2/σ) * √(2π) / √(2πσ)^N, where f_1 is one-dimensional Gaussian with | |
| 112 | // variance 1. The Lipschitz factor of f_1 is e^{-1/2}/√(2π), see, e.g., | |
| 113 | // https://math.stackexchange.com/questions/3630967/is-the-gaussian-density-lipschitz-continuous | |
| 114 | // Thus the Lipschitz factor we want is e^{-1/2} / (√(2πσ)^N * σ). | |
| 115 | Some((-0.5).exp() / (self.scale() * self.variance.value().sqrt())) | |
| 116 | } | |
| 117 | } | |
| 0 | 118 | |
| 119 | #[replace_float_literals(S::Type::cast_from(literal))] | |
| 120 | impl<'a, S, const N : usize> Gaussian<S, N> | |
| 121 | where S : Constant { | |
| 122 | ||
| 123 | /// Returns the (reciprocal) scaling constant $1/C=(2πσ^2)^{N/2}$. | |
| 124 | #[inline] | |
| 125 | pub fn scale(&self) -> S::Type { | |
| 126 | let π = S::Type::PI; | |
| 127 | let σ2 = self.variance.value(); | |
| 128 | (2.0*π*σ2).powi(N as i32).sqrt() | |
| 129 | } | |
| 130 | } | |
| 131 | ||
| 132 | impl<'a, S, const N : usize> Support<S::Type, N> for Gaussian<S, N> | |
| 133 | where S : Constant { | |
| 134 | #[inline] | |
| 135 | fn support_hint(&self) -> Cube<S::Type,N> { | |
| 136 | array_init(|| [S::Type::NEG_INFINITY, S::Type::INFINITY]).into() | |
| 137 | } | |
| 138 | ||
| 139 | #[inline] | |
| 140 | fn in_support(&self, _x : &Loc<S::Type,N>) -> bool { | |
| 141 | true | |
| 142 | } | |
| 143 | } | |
| 144 | ||
| 145 | #[replace_float_literals(S::Type::cast_from(literal))] | |
| 146 | impl<S, const N : usize> GlobalAnalysis<S::Type, Bounds<S::Type>> for Gaussian<S, N> | |
| 147 | where S : Constant { | |
| 148 | #[inline] | |
| 149 | fn global_analysis(&self) -> Bounds<S::Type> { | |
| 150 | Bounds(0.0, 1.0/self.scale()) | |
| 151 | } | |
| 152 | } | |
| 153 | ||
| 154 | impl<S, const N : usize> LocalAnalysis<S::Type, Bounds<S::Type>, N> for Gaussian<S, N> | |
| 155 | where S : Constant { | |
| 156 | #[inline] | |
| 157 | fn local_analysis(&self, cube : &Cube<S::Type, N>) -> Bounds<S::Type> { | |
| 158 | // The function is maximised/minimised where the 2-norm is minimised/maximised. | |
| 159 | let lower = self.apply(cube.maxnorm_point()); | |
| 160 | let upper = self.apply(cube.minnorm_point()); | |
| 161 | Bounds(lower, upper) | |
| 162 | } | |
| 163 | } | |
| 164 | ||
| 165 | #[replace_float_literals(C::Type::cast_from(literal))] | |
| 166 | impl<'a, C : Constant, const N : usize> Norm<C::Type, L1> | |
| 167 | for Gaussian<C, N> { | |
| 168 | #[inline] | |
| 169 | fn norm(&self, _ : L1) -> C::Type { | |
| 170 | 1.0 | |
| 171 | } | |
| 172 | } | |
| 173 | ||
| 174 | #[replace_float_literals(C::Type::cast_from(literal))] | |
| 175 | impl<'a, C : Constant, const N : usize> Norm<C::Type, Linfinity> | |
| 176 | for Gaussian<C, N> { | |
| 177 | #[inline] | |
| 178 | fn norm(&self, _ : Linfinity) -> C::Type { | |
| 179 | self.bounds().upper() | |
| 180 | } | |
| 181 | } | |
| 182 | ||
| 183 | #[replace_float_literals(C::Type::cast_from(literal))] | |
| 184 | impl<'a, C : Constant, const N : usize> Fourier<C::Type> | |
| 185 | for Gaussian<C, N> { | |
| 186 | type Domain = Loc<C::Type, N>; | |
| 187 | type Transformed = Weighted<Gaussian<C::Type, N>, C::Type>; | |
| 188 | ||
| 189 | #[inline] | |
| 190 | fn fourier(&self) -> Self::Transformed { | |
| 191 | let π = C::Type::PI; | |
| 192 | let σ2 = self.variance.value(); | |
| 193 | let g = Gaussian { variance : 1.0 / (4.0*π*π*σ2) }; | |
| 194 | g.weigh(g.scale()) | |
| 195 | } | |
| 196 | } | |
| 197 | ||
| 198 | /// Representation of the “cut” gaussian $f χ\_{[-a, a]^n}$ | |
| 199 | /// where $a>0$ and $f$ is a gaussian kernel on $ℝ^n$. | |
| 200 | pub type BasicCutGaussian<C, S, const N : usize> = SupportProductFirst<CubeIndicator<C, N>, | |
| 201 | Gaussian<S, N>>; | |
| 202 | ||
| 203 | ||
| 33 | 204 | /// This implements $g := χ\_{[-b, b]^n} \* (f χ\_{[-a, a]^n})$ where $a,b>0$ and $f$ is |
| 205 | /// a gaussian kernel on $ℝ^n$. For an expression for $g$, see Lemma 3.9 in the manuscript. | |
| 0 | 206 | #[replace_float_literals(F::cast_from(literal))] |
| 207 | impl<'a, F : Float, R, C, S, const N : usize> Apply<&'a Loc<F, N>> | |
| 208 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
| 209 | where R : Constant<Type=F>, | |
| 210 | C : Constant<Type=F>, | |
| 211 | S : Constant<Type=F> { | |
| 212 | ||
| 213 | type Output = F; | |
| 214 | ||
| 215 | #[inline] | |
| 216 | fn apply(&self, y : &'a Loc<F, N>) -> F { | |
| 217 | let Convolution(ref ind, | |
| 218 | SupportProductFirst(ref cut, | |
| 219 | ref gaussian)) = self; | |
| 220 | let a = cut.r.value(); | |
| 221 | let b = ind.r.value(); | |
| 222 | let σ = gaussian.variance.value().sqrt(); | |
| 223 | let t = F::SQRT_2 * σ; | |
|
31
6105b5cd8d89
Simplify and fix cut gaussian indicator convolution scaling
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
224 | let c = 0.5; // 1/(σ√(2π) * σ√(π/2) = 1/2 |
| 0 | 225 | |
| 226 | // 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
|
227 | y.product_map(|x| { |
| 0 | 228 | let c1 = -(a.min(b + x)); //(-a).max(-x-b); |
| 229 | let c2 = a.min(b - x); | |
| 230 | if c1 >= c2 { | |
| 231 | 0.0 | |
| 232 | } else { | |
| 233 | let e1 = F::cast_from(erf((c1 / t).as_())); | |
| 234 | let e2 = F::cast_from(erf((c2 / t).as_())); | |
| 235 | debug_assert!(e2 >= e1); | |
| 236 | c * (e2 - e1) | |
| 237 | } | |
|
31
6105b5cd8d89
Simplify and fix cut gaussian indicator convolution scaling
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
238 | }) |
| 0 | 239 | } |
| 240 | } | |
| 241 | ||
| 242 | impl<F : Float, R, C, S, const N : usize> Apply<Loc<F, N>> | |
| 243 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
| 244 | where R : Constant<Type=F>, | |
| 245 | C : Constant<Type=F>, | |
| 246 | S : Constant<Type=F> { | |
| 247 | ||
| 248 | type Output = F; | |
| 249 | ||
| 250 | #[inline] | |
| 251 | fn apply(&self, y : Loc<F, N>) -> F { | |
| 252 | self.apply(&y) | |
| 253 | } | |
| 254 | } | |
| 255 | ||
| 33 | 256 | /// This implements the differential of $g := χ\_{[-b, b]^n} \* (f χ\_{[-a, a]^n})$ where $a,b>0$ |
| 257 | /// and $f$ is a gaussian kernel on $ℝ^n$. For an expression for the value of $g$, from which the | |
| 258 | /// derivative readily arises (at points of differentiability), see Lemma 3.9 in the manuscript. | |
| 259 | #[replace_float_literals(F::cast_from(literal))] | |
| 260 | impl<'a, F : Float, R, C, S, const N : usize> Differentiable<&'a Loc<F, N>> | |
| 261 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
| 262 | where R : Constant<Type=F>, | |
| 263 | C : Constant<Type=F>, | |
| 264 | S : Constant<Type=F> { | |
| 265 | ||
| 266 | type Output = Loc<F, N>; | |
| 267 | ||
| 268 | #[inline] | |
| 269 | fn differential(&self, y : &'a Loc<F, N>) -> Loc<F, N> { | |
| 270 | let Convolution(ref ind, | |
| 271 | SupportProductFirst(ref cut, | |
| 272 | ref gaussian)) = self; | |
| 273 | let a = cut.r.value(); | |
| 274 | let b = ind.r.value(); | |
| 275 | let σ = gaussian.variance.value().sqrt(); | |
| 276 | let t = F::SQRT_2 * σ; | |
| 277 | let c = 0.5; // 1/(σ√(2π) * σ√(π/2) = 1/2 | |
| 278 | let c_div_t = c / t; | |
| 279 | ||
| 280 | // Calculate the values for all component functions of the | |
| 281 | // product. This is just the loop from apply above. | |
| 282 | let unscaled_vs = y.map(|x| { | |
| 283 | let c1 = -(a.min(b + x)); //(-a).max(-x-b); | |
| 284 | let c2 = a.min(b - x); | |
| 285 | if c1 >= c2 { | |
| 286 | 0.0 | |
| 287 | } else { | |
| 288 | let e1 = F::cast_from(erf((c1 / t).as_())); | |
| 289 | let e2 = F::cast_from(erf((c2 / t).as_())); | |
| 290 | debug_assert!(e2 >= e1); | |
| 291 | c * (e2 - e1) | |
| 292 | } | |
| 293 | }); | |
| 294 | // This computes the gradient for each coordinate | |
| 295 | product_differential(y, &unscaled_vs, |x| { | |
| 296 | let c1 = -(a.min(b + x)); //(-a).max(-x-b); | |
| 297 | let c2 = a.min(b - x); | |
| 298 | if c1 >= c2 { | |
| 299 | 0.0 | |
| 300 | } else { | |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
301 | // erf'(z) = (2/√π)*exp(-z^2), and we get extra factor -1/(√2*σ) = -1/t |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
302 | // from the chain rule (the minus comes from inside c_1 or c_2). |
| 33 | 303 | let de1 = (-(c1/t).powi(2)).exp(); |
| 304 | let de2 = (-(c2/t).powi(2)).exp(); | |
| 305 | c_div_t * (de1 - de2) | |
| 306 | } | |
| 307 | }) | |
| 308 | } | |
| 309 | } | |
| 310 | ||
| 311 | impl<F : Float, R, C, S, const N : usize> Differentiable<Loc<F, N>> | |
| 312 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
| 313 | where R : Constant<Type=F>, | |
| 314 | C : Constant<Type=F>, | |
| 315 | S : Constant<Type=F> { | |
| 316 | ||
| 317 | type Output = Loc<F, N>; | |
| 318 | ||
| 319 | #[inline] | |
| 320 | fn differential(&self, y : Loc<F, N>) -> Loc<F, N> { | |
| 321 | self.differential(&y) | |
| 322 | } | |
| 323 | } | |
| 324 | ||
| 325 | #[replace_float_literals(F::cast_from(literal))] | |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
326 | impl<'a, F : Float, R, C, S, const N : usize> Lipschitz<L1> |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
327 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
328 | where R : Constant<Type=F>, |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
329 | C : Constant<Type=F>, |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
330 | S : Constant<Type=F> { |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
331 | type FloatType = F; |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
332 | |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
333 | fn lipschitz_factor(&self, L1 : L1) -> Option<F> { |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
334 | // To get the product Lipschitz factor, we note that for any ψ_i, we have |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
335 | // ∏_{i=1}^N φ_i(x_i) - ∏_{i=1}^N φ_i(y_i) |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
336 | // = [φ_1(x_1)-φ_1(y_1)] ∏_{i=2}^N φ_i(x_i) |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
337 | // + φ_1(y_1)[ ∏_{i=2}^N φ_i(x_i) - ∏_{i=2}^N φ_i(y_i)] |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
338 | // = ∑_{j=1}^N [φ_j(x_j)-φ_j(y_j)]∏_{i > j} φ_i(x_i) ∏_{i < j} φ_i(y_i) |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
339 | // Thus |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
340 | // |∏_{i=1}^N φ_i(x_i) - ∏_{i=1}^N φ_i(y_i)| |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
341 | // ≤ ∑_{j=1}^N |φ_j(x_j)-φ_j(y_j)| ∏_{j ≠ i} \max_i |φ_i| |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
342 | // |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
343 | // Thus we need 1D Lipschitz factors, and the maximum for φ = θ * ψ. |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
344 | // |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
345 | // We have |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
346 | // θ * ψ(x) = 0 if c_1(x) ≥ c_2(x) |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
347 | // = (1/2)[erf(c_2(x)/(√2σ)) - erf(c_1(x)/(√2σ))] if c_1(x) < c_2(x), |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
348 | // where c_1(x) = max{-x-b,-a} = -min{b+x,a} and c_2(x)=min{b-x,a}, C is the Gaussian |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
349 | // normalisation factor, and erf(s) = (2/√π) ∫_0^s e^{-t^2} dt. |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
350 | // Thus, if c_1(x) < c_2(x) and c_1(y) < c_2(y), we have |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
351 | // θ * ψ(x) - θ * ψ(y) = (1/√π)[∫_{c_1(x)/(√2σ)}^{c_1(y)/(√2σ) e^{-t^2} dt |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
352 | // - ∫_{c_2(x)/(√2σ)}^{c_2(y)/(√2σ)] e^{-t^2} dt] |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
353 | // Thus |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
354 | // |θ * ψ(x) - θ * ψ(y)| ≤ (1/√π)/(√2σ)(|c_1(x)-c_1(y)|+|c_2(x)-c_2(y)|) |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
355 | // ≤ 2(1/√π)/(√2σ)|x-y| |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
356 | // ≤ √2/(√πσ)|x-y|. |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
357 | // |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
358 | // For the product we also need the value θ * ψ(0), which is |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
359 | // (1/2)[erf(min{a,b}/(√2σ))-erf(max{-b,-a}/(√2σ)] |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
360 | // = (1/2)[erf(min{a,b}/(√2σ))-erf(-min{a,b}/(√2σ))] |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
361 | // = erf(min{a,b}/(√2σ)) |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
362 | // |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
363 | // If c_1(x) ≥ c_2(x), then x ∉ [-(a+b), a+b]. If also y is outside that range, |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
364 | // θ * ψ(x) = θ * ψ(y). If only y is in the range [-(a+b), a+b], we can replace |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
365 | // x by -(a+b) or (a+b), either of which is closer to y and still θ * ψ(x)=0. |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
366 | // Thus same calculations as above work for the Lipschitz factor. |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
367 | let Convolution(ref ind, |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
368 | SupportProductFirst(ref cut, |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
369 | ref gaussian)) = self; |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
370 | let a = cut.r.value(); |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
371 | let b = ind.r.value(); |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
372 | let σ = gaussian.variance.value().sqrt(); |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
373 | let π = F::PI; |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
374 | let t = F::SQRT_2 * σ; |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
375 | let l1d = F::SQRT_2 / (π.sqrt() * σ); |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
376 | let e0 = F::cast_from(erf((a.min(b) / t).as_())); |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
377 | Some(l1d * e0.powi(N as i32-1)) |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
378 | } |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
379 | } |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
380 | |
| 33 | 381 | impl<'a, F : Float, R, C, S, const N : usize> Lipschitz<L2> |
| 382 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
| 383 | where R : Constant<Type=F>, | |
| 384 | C : Constant<Type=F>, | |
| 385 | S : Constant<Type=F> { | |
| 386 | type FloatType = F; | |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
387 | #[inline] |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
388 | fn lipschitz_factor(&self, L2 : L2) -> Option<Self::FloatType> { |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
389 | self.lipschitz_factor(L1).map(|l1| l1 * <S::Type>::cast_from(N).sqrt()) |
| 33 | 390 | } |
| 391 | } | |
| 392 | ||
| 0 | 393 | impl<F : Float, R, C, S, const N : usize> |
| 394 | Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
| 395 | where R : Constant<Type=F>, | |
| 396 | C : Constant<Type=F>, | |
| 397 | S : Constant<Type=F> { | |
| 398 | ||
| 399 | #[inline] | |
| 400 | fn get_r(&self) -> F { | |
| 401 | let Convolution(ref ind, | |
| 402 | SupportProductFirst(ref cut, ..)) = self; | |
| 403 | ind.r.value() + cut.r.value() | |
| 404 | } | |
| 405 | } | |
| 406 | ||
| 407 | impl<F : Float, R, C, S, const N : usize> Support<F, N> | |
| 408 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
| 409 | where R : Constant<Type=F>, | |
| 410 | C : Constant<Type=F>, | |
| 411 | S : Constant<Type=F> { | |
| 412 | #[inline] | |
| 413 | fn support_hint(&self) -> Cube<F, N> { | |
| 414 | let r = self.get_r(); | |
| 415 | array_init(|| [-r, r]).into() | |
| 416 | } | |
| 417 | ||
| 418 | #[inline] | |
| 419 | fn in_support(&self, y : &Loc<F, N>) -> bool { | |
| 420 | let r = self.get_r(); | |
| 421 | y.iter().all(|x| x.abs() <= r) | |
| 422 | } | |
| 423 | ||
| 424 | #[inline] | |
| 425 | fn bisection_hint(&self, cube : &Cube<F, N>) -> [Option<F>; N] { | |
| 426 | let r = self.get_r(); | |
| 427 | // From c1 = -a.min(b + x) and c2 = a.min(b - x) with c_1 < c_2, | |
| 428 | // solve bounds for x. that is 0 ≤ a.min(b + x) + a.min(b - x). | |
| 429 | // If b + x ≤ a and b - x ≤ a, the sum is 2b ≥ 0. | |
| 430 | // If b + x ≥ a and b - x ≥ a, the sum is 2a ≥ 0. | |
| 431 | // If b + x ≤ a and b - x ≥ a, the sum is b + x + a ⟹ need x ≥ -a - b = -r. | |
| 432 | // If b + x ≥ a and b - x ≤ a, the sum is a + b - x ⟹ need x ≤ a + b = r. | |
| 433 | cube.map(|c, d| symmetric_peak_hint(r, c, d)) | |
| 434 | } | |
| 435 | } | |
| 436 | ||
| 437 | impl<F : Float, R, C, S, const N : usize> GlobalAnalysis<F, Bounds<F>> | |
| 438 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
| 439 | where R : Constant<Type=F>, | |
| 440 | C : Constant<Type=F>, | |
| 441 | S : Constant<Type=F> { | |
| 442 | #[inline] | |
| 443 | fn global_analysis(&self) -> Bounds<F> { | |
| 444 | Bounds(F::ZERO, self.apply(Loc::ORIGIN)) | |
| 445 | } | |
| 446 | } | |
| 447 | ||
| 448 | impl<F : Float, R, C, S, const N : usize> LocalAnalysis<F, Bounds<F>, N> | |
| 449 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
| 450 | where R : Constant<Type=F>, | |
| 451 | C : Constant<Type=F>, | |
| 452 | S : Constant<Type=F> { | |
| 453 | #[inline] | |
| 454 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
| 455 | // The function is maximised/minimised where the absolute value is minimised/maximised. | |
| 456 | let lower = self.apply(cube.maxnorm_point()); | |
| 457 | let upper = self.apply(cube.minnorm_point()); | |
| 458 | Bounds(lower, upper) | |
| 459 | } | |
| 460 | } | |
| 461 |