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