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