Mon, 13 Apr 2026 22:29:26 -0500
Automatic transport disabling after sufficient failures, for efficiency
| 0 | 1 | //! Things for constructing new kernels from component kernels and traits for analysing them |
| 2 | use numeric_literals::replace_float_literals; | |
|
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 serde::Serialize; |
| 0 | 4 | |
|
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
|
5 | use alg_tools::bisection_tree::Support; |
|
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::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
|
7 | use alg_tools::instance::{Instance, Space}; |
|
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 | 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
|
9 | 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
|
10 | DifferentiableImpl, DifferentiableMapping, 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
|
11 | }; |
|
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::maputil::{array_init, map1_indexed, map2}; |
| 0 | 13 | use alg_tools::norms::*; |
| 14 | use alg_tools::sets::Cube; | |
| 15 | use alg_tools::sets::SetOrd; | |
|
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
|
16 | use alg_tools::types::*; |
|
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
|
17 | use anyhow::anyhow; |
| 0 | 18 | |
| 19 | use crate::fourier::Fourier; | |
| 35 | 20 | use crate::types::*; |
| 0 | 21 | |
| 22 | /// Representation of the product of two kernels. | |
| 23 | /// | |
| 35 | 24 | /// The kernels typically implement [`Support`] and [`Mapping`]. |
| 0 | 25 | /// |
| 26 | /// The implementation [`Support`] only uses the [`Support::support_hint`] of the first parameter! | |
|
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, Serialize, Debug)] |
| 0 | 28 | pub struct SupportProductFirst<A, B>( |
| 29 | /// First kernel | |
| 30 | pub A, | |
| 31 | /// Second kernel | |
|
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
|
32 | pub B, |
| 0 | 33 | ); |
| 34 | ||
|
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
|
35 | impl<A, B, F: Float, const N: usize> Mapping<Loc<N, F>> for SupportProductFirst<A, B> |
| 35 | 36 | 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
|
37 | A: Mapping<Loc<N, F>, Codomain = 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
|
38 | B: Mapping<Loc<N, F>, Codomain = F>, |
| 35 | 39 | { |
| 40 | type Codomain = F; | |
| 41 | ||
| 0 | 42 | #[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
|
43 | fn apply<I: Instance<Loc<N, F>>>(&self, x: I) -> Self::Codomain { |
|
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 | x.eval_ref(|r| self.0.apply(r)) * self.1.apply(x) |
| 0 | 45 | } |
| 46 | } | |
| 47 | ||
|
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
|
48 | impl<A, B, F: Float, const N: usize> DifferentiableImpl<Loc<N, F>> for SupportProductFirst<A, B> |
| 35 | 49 | 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
|
50 | A: DifferentiableMapping<Loc<N, F>, DerivativeDomain = Loc<N, F>, Codomain = 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
|
51 | B: DifferentiableMapping<Loc<N, F>, DerivativeDomain = Loc<N, F>, Codomain = F>, |
| 35 | 52 | { |
|
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
|
53 | type Derivative = Loc<N, F>; |
| 35 | 54 | |
| 0 | 55 | #[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
|
56 | fn differential_impl<I: Instance<Loc<N, F>>>(&self, x: 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
|
57 | x.eval_ref(|xr| { |
|
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
|
58 | self.0.differential(xr) * self.1.apply(xr) + self.1.differential(xr) * self.0.apply(xr) |
|
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 | }) |
| 0 | 60 | } |
| 61 | } | |
| 62 | ||
|
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
|
63 | impl<A, B, M: Copy, F: Float> Lipschitz<M> for SupportProductFirst<A, B> |
|
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
|
64 | 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
|
65 | A: Lipschitz<M, FloatType = F> + Bounded<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
|
66 | B: Lipschitz<M, FloatType = F> + Bounded<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
|
67 | { |
| 35 | 68 | type FloatType = F; |
| 32 | 69 | #[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
|
70 | fn lipschitz_factor(&self, m: M) -> DynResult<F> { |
| 35 | 71 | // f(x)g(x) - f(y)g(y) = f(x)[g(x)-g(y)] - [f(y)-f(x)]g(y) |
| 72 | let &SupportProductFirst(ref f, ref g) = self; | |
|
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
|
73 | Ok(f.lipschitz_factor(m)? * g.bounds().uniform() |
|
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
|
74 | + g.lipschitz_factor(m)? * f.bounds().uniform()) |
| 32 | 75 | } |
| 76 | } | |
| 77 | ||
|
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
|
78 | impl<'a, A, B, M: Copy, Domain, F: Float> LipschitzDifferentiableImpl<Domain, M> |
|
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 | for SupportProductFirst<A, B> |
| 35 | 80 | 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
|
81 | Domain: Space, |
|
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
|
82 | Self: DifferentiableImpl<Domain>, |
|
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
|
83 | A: DifferentiableMapping<Domain> + Lipschitz<M, FloatType = F> + Bounded<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
|
84 | B: DifferentiableMapping<Domain> + Lipschitz<M, FloatType = F> + Bounded<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
|
85 | SupportProductFirst<A, B>: DifferentiableMapping<Domain>, |
|
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
|
86 | for<'b> A::Differential<'b>: Lipschitz<M, FloatType = F> + NormBounded<L2, FloatType = 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
|
87 | for<'b> B::Differential<'b>: Lipschitz<M, FloatType = F> + NormBounded<L2, FloatType = F>, |
| 35 | 88 | { |
| 89 | type FloatType = F; | |
| 32 | 90 | #[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
|
91 | fn diff_lipschitz_factor(&self, m: M) -> DynResult<F> { |
| 35 | 92 | // ∇[gf] = f∇g + g∇f |
| 93 | // ⟹ ∇[gf](x) - ∇[gf](y) = f(x)∇g(x) + g(x)∇f(x) - f(y)∇g(y) + g(y)∇f(y) | |
| 94 | // = f(x)[∇g(x)-∇g(y)] + g(x)∇f(x) - [f(y)-f(x)]∇g(y) + g(y)∇f(y) | |
| 95 | // = f(x)[∇g(x)-∇g(y)] + g(x)[∇f(x)-∇f(y)] | |
| 96 | // - [f(y)-f(x)]∇g(y) + [g(y)-g(x)]∇f(y) | |
|
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
|
97 | let &SupportProductFirst(ref f, ref g) = self; |
| 35 | 98 | let (df, dg) = (f.diff_ref(), g.diff_ref()); |
|
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
|
99 | Ok([ |
|
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
|
100 | df.lipschitz_factor(m)? * g.bounds().uniform(), |
|
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
|
101 | dg.lipschitz_factor(m)? * f.bounds().uniform(), |
|
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
|
102 | f.lipschitz_factor(m)? * dg.norm_bound(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
|
103 | g.lipschitz_factor(m)? * df.norm_bound(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
|
104 | ] |
|
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
|
105 | .into_iter() |
|
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
|
106 | .sum()) |
| 32 | 107 | } |
| 108 | } | |
| 109 | ||
|
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
|
110 | impl<'a, A, B, F: Float, const N: usize> Support<N, F> for SupportProductFirst<A, B> |
| 35 | 111 | 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
|
112 | A: 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
|
113 | B: Support<N, F>, |
| 35 | 114 | { |
| 0 | 115 | #[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
|
116 | fn support_hint(&self) -> Cube<N, F> { |
| 0 | 117 | self.0.support_hint() |
| 118 | } | |
| 119 | ||
| 120 | #[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
|
121 | fn in_support(&self, x: &Loc<N, F>) -> bool { |
| 0 | 122 | self.0.in_support(x) |
| 123 | } | |
| 124 | ||
| 125 | #[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
|
126 | fn bisection_hint(&self, cube: &Cube<N, F>) -> [Option<F>; N] { |
| 0 | 127 | self.0.bisection_hint(cube) |
| 128 | } | |
| 129 | } | |
| 130 | ||
|
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
|
131 | impl<'a, A, B, F: Float> GlobalAnalysis<F, Bounds<F>> for SupportProductFirst<A, B> |
|
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 | 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
|
133 | A: 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
|
134 | B: 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
|
135 | { |
| 0 | 136 | #[inline] |
| 137 | fn global_analysis(&self) -> Bounds<F> { | |
| 138 | self.0.global_analysis() * self.1.global_analysis() | |
| 139 | } | |
| 140 | } | |
| 141 | ||
|
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
|
142 | impl<'a, A, B, F: Float, 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
|
143 | for SupportProductFirst<A, B> |
|
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 | 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
|
145 | A: 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
|
146 | B: 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
|
147 | { |
| 0 | 148 | #[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
|
149 | fn local_analysis(&self, cube: &Cube<N, F>) -> Bounds<F> { |
| 0 | 150 | self.0.local_analysis(cube) * self.1.local_analysis(cube) |
| 151 | } | |
| 152 | } | |
| 153 | ||
| 154 | /// Representation of the sum of two kernels | |
| 155 | /// | |
| 35 | 156 | /// The kernels typically implement [`Support`] and [`Mapping`]. |
| 0 | 157 | /// |
| 158 | /// The implementation [`Support`] only uses the [`Support::support_hint`] of the first parameter! | |
|
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
|
159 | #[derive(Copy, Clone, Serialize, Debug)] |
| 0 | 160 | pub struct SupportSum<A, B>( |
| 161 | /// First kernel | |
| 162 | pub A, | |
| 163 | /// Second kernel | |
|
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
|
164 | pub B, |
| 0 | 165 | ); |
| 166 | ||
|
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
|
167 | impl<'a, A, B, F: Float, const N: usize> Mapping<Loc<N, F>> for SupportSum<A, B> |
| 35 | 168 | 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
|
169 | A: Mapping<Loc<N, F>, Codomain = 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
|
170 | B: Mapping<Loc<N, F>, Codomain = F>, |
| 35 | 171 | { |
| 172 | type Codomain = F; | |
| 173 | ||
| 0 | 174 | #[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
|
175 | fn apply<I: Instance<Loc<N, F>>>(&self, x: I) -> Self::Codomain { |
|
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
|
176 | x.eval_ref(|r| self.0.apply(r)) + self.1.apply(x) |
| 0 | 177 | } |
| 178 | } | |
| 179 | ||
|
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
|
180 | impl<'a, A, B, F: Float, const N: usize> DifferentiableImpl<Loc<N, F>> for SupportSum<A, B> |
| 35 | 181 | 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
|
182 | A: DifferentiableMapping<Loc<N, F>, DerivativeDomain = 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
|
183 | B: DifferentiableMapping<Loc<N, F>, DerivativeDomain = Loc<N, F>>, |
| 35 | 184 | { |
|
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
|
185 | type Derivative = Loc<N, F>; |
| 35 | 186 | |
| 32 | 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 differential_impl<I: Instance<Loc<N, F>>>(&self, x: 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
|
189 | x.eval_ref(|r| self.0.differential(r)) + self.1.differential(x) |
| 32 | 190 | } |
| 191 | } | |
| 192 | ||
|
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
|
193 | impl<'a, A, B, F: Float, const N: usize> Support<N, F> for SupportSum<A, B> |
|
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 | 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
|
195 | A: 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
|
196 | B: 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
|
197 | Cube<N, F>: SetOrd, |
|
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
|
198 | { |
| 0 | 199 | #[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
|
200 | fn support_hint(&self) -> Cube<N, F> { |
| 0 | 201 | self.0.support_hint().common(&self.1.support_hint()) |
| 202 | } | |
| 203 | ||
| 204 | #[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
|
205 | fn in_support(&self, x: &Loc<N, F>) -> bool { |
| 0 | 206 | self.0.in_support(x) || self.1.in_support(x) |
| 207 | } | |
| 208 | ||
| 209 | #[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
|
210 | fn bisection_hint(&self, cube: &Cube<N, F>) -> [Option<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
|
211 | map2( |
|
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
|
212 | self.0.bisection_hint(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
|
213 | self.1.bisection_hint(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
|
214 | |a, b| a.or(b), |
|
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
|
215 | ) |
| 0 | 216 | } |
| 217 | } | |
| 218 | ||
|
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
|
219 | impl<'a, A, B, F: Float> GlobalAnalysis<F, Bounds<F>> for SupportSum<A, B> |
|
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 | 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
|
221 | A: 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
|
222 | B: 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
|
223 | { |
| 0 | 224 | #[inline] |
| 225 | fn global_analysis(&self) -> Bounds<F> { | |
| 226 | self.0.global_analysis() + self.1.global_analysis() | |
| 227 | } | |
| 228 | } | |
| 229 | ||
|
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
|
230 | impl<'a, A, B, F: Float, const N: usize> LocalAnalysis<F, Bounds<F>, N> for SupportSum<A, B> |
|
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
|
231 | 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
|
232 | A: 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
|
233 | B: 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
|
234 | Cube<N, F>: SetOrd, |
|
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 | { |
| 0 | 236 | #[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
|
237 | fn local_analysis(&self, cube: &Cube<N, F>) -> Bounds<F> { |
| 0 | 238 | self.0.local_analysis(cube) + self.1.local_analysis(cube) |
| 239 | } | |
| 240 | } | |
| 241 | ||
|
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 | impl<F: Float, M: Copy, A, B> Lipschitz<M> for SupportSum<A, B> |
|
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
|
243 | 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
|
244 | A: Lipschitz<M, FloatType = 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
|
245 | B: Lipschitz<M, FloatType = 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
|
246 | { |
| 32 | 247 | type FloatType = F; |
| 248 | ||
|
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 | fn lipschitz_factor(&self, m: M) -> DynResult<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
|
250 | Ok(self.0.lipschitz_factor(m)? * self.1.lipschitz_factor(m)?) |
| 32 | 251 | } |
| 252 | } | |
| 253 | ||
|
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
|
254 | impl<'b, F: Float, M: Copy, A, B, Domain> LipschitzDifferentiableImpl<Domain, M> |
|
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 | for SupportSum<A, B> |
| 35 | 256 | 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
|
257 | Domain: Space, |
|
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 | Self: DifferentiableImpl<Domain>, |
|
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 | A: DifferentiableMapping<Domain, Codomain = 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 | B: DifferentiableMapping<Domain, Codomain = 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 | SupportSum<A, B>: DifferentiableMapping<Domain, Codomain = 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
|
262 | for<'a> A::Differential<'a>: Lipschitz<M, FloatType = 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
|
263 | for<'a> B::Differential<'a>: Lipschitz<M, FloatType = F>, |
| 35 | 264 | { |
| 265 | type FloatType = F; | |
| 266 | ||
|
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
|
267 | fn diff_lipschitz_factor(&self, m: M) -> DynResult<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
|
268 | Ok(self.0.diff_ref().lipschitz_factor(m)? + self.1.diff_ref().lipschitz_factor(m)?) |
| 35 | 269 | } |
| 270 | } | |
| 32 | 271 | |
| 0 | 272 | /// Representation of the convolution of two kernels. |
| 273 | /// | |
| 35 | 274 | /// The kernels typically implement [`Support`]s and [`Mapping`]. |
| 0 | 275 | // |
| 276 | /// Trait implementations have to be on a case-by-case basis. | |
|
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
|
277 | #[derive(Copy, Clone, Serialize, Debug, Eq, PartialEq)] |
| 0 | 278 | pub struct Convolution<A, B>( |
| 279 | /// First kernel | |
| 280 | pub A, | |
| 281 | /// Second kernel | |
|
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
|
282 | pub B, |
| 0 | 283 | ); |
| 284 | ||
|
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
|
285 | impl<F: Float, M, A, B> Lipschitz<M> for Convolution<A, B> |
|
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
|
286 | 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
|
287 | A: Norm<L1, 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
|
288 | B: Lipschitz<M, FloatType = 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
|
289 | { |
| 32 | 290 | type FloatType = F; |
| 291 | ||
|
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
|
292 | fn lipschitz_factor(&self, m: M) -> DynResult<F> { |
| 35 | 293 | // For [f * g](x) = ∫ f(x-y)g(y) dy we have |
| 294 | // [f * g](x) - [f * g](z) = ∫ [f(x-y)-f(z-y)]g(y) dy. | |
| 295 | // Hence |[f * g](x) - [f * g](z)| ≤ ∫ |f(x-y)-f(z-y)|g(y)| dy. | |
| 296 | // ≤ L|x-z| ∫ |g(y)| dy, | |
| 297 | // where L is the Lipschitz factor of 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
|
298 | Ok(self.1.lipschitz_factor(m)? * self.0.norm(L1)) |
| 35 | 299 | } |
| 300 | } | |
| 301 | ||
|
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
|
302 | impl<'b, F: Float, M, A, B, Domain> LipschitzDifferentiableImpl<Domain, M> for Convolution<A, B> |
| 35 | 303 | 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
|
304 | Domain: Space, |
|
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 | Self: DifferentiableImpl<Domain>, |
|
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 | A: Norm<L1, 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
|
307 | Convolution<A, B>: DifferentiableMapping<Domain, Codomain = 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
|
308 | B: DifferentiableMapping<Domain, Codomain = 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
|
309 | for<'a> B::Differential<'a>: Lipschitz<M, FloatType = F>, |
| 35 | 310 | { |
| 311 | type FloatType = F; | |
| 312 | ||
|
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 | fn diff_lipschitz_factor(&self, m: M) -> DynResult<F> { |
| 35 | 314 | // For [f * g](x) = ∫ f(x-y)g(y) dy we have |
| 315 | // ∇[f * g](x) - ∇[f * g](z) = ∫ [∇f(x-y)-∇f(z-y)]g(y) dy. | |
| 316 | // Hence |∇[f * g](x) - ∇[f * g](z)| ≤ ∫ |∇f(x-y)-∇f(z-y)|g(y)| dy. | |
| 317 | // ≤ L|x-z| ∫ |g(y)| dy, | |
| 318 | // where L is the Lipschitz factor of ∇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
|
319 | self.1 |
|
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
|
320 | .diff_ref() |
|
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
|
321 | .lipschitz_factor(m) |
|
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
|
322 | .map(|l| l * self.0.norm(L1)) |
| 32 | 323 | } |
| 324 | } | |
| 325 | ||
| 0 | 326 | /// Representation of the autoconvolution of a kernel. |
| 327 | /// | |
| 35 | 328 | /// The kernel typically implements [`Support`] and [`Mapping`]. |
| 0 | 329 | /// |
| 330 | /// Trait implementations have to be on a case-by-case basis. | |
|
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
|
331 | #[derive(Copy, Clone, Serialize, Debug, Eq, PartialEq)] |
| 0 | 332 | pub struct AutoConvolution<A>( |
| 333 | /// The kernel to be autoconvolved | |
|
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
|
334 | pub A, |
| 0 | 335 | ); |
| 336 | ||
|
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
|
337 | impl<F: Float, M, C> Lipschitz<M> for AutoConvolution<C> |
|
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
|
338 | 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
|
339 | C: Lipschitz<M, FloatType = F> + Norm<L1, 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
|
340 | { |
| 32 | 341 | type FloatType = F; |
| 342 | ||
|
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
|
343 | fn lipschitz_factor(&self, m: M) -> DynResult<F> { |
| 35 | 344 | self.0.lipschitz_factor(m).map(|l| l * self.0.norm(L1)) |
| 345 | } | |
| 346 | } | |
| 347 | ||
|
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
|
348 | impl<'b, F: Float, M, C, Domain> LipschitzDifferentiableImpl<Domain, M> for AutoConvolution<C> |
| 35 | 349 | 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
|
350 | Domain: Space, |
|
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
|
351 | Self: DifferentiableImpl<Domain>, |
|
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
|
352 | C: Norm<L1, F> + DifferentiableMapping<Domain, Codomain = 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
|
353 | AutoConvolution<C>: DifferentiableMapping<Domain, Codomain = 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
|
354 | for<'a> C::Differential<'a>: Lipschitz<M, FloatType = F>, |
| 35 | 355 | { |
| 356 | type FloatType = F; | |
| 357 | ||
|
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
|
358 | fn diff_lipschitz_factor(&self, m: M) -> DynResult<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 | self.0 |
|
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 | .diff_ref() |
|
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 | .lipschitz_factor(m) |
|
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
|
362 | .map(|l| l * self.0.norm(L1)) |
| 32 | 363 | } |
| 364 | } | |
| 365 | ||
| 0 | 366 | /// Representation a multi-dimensional product of a one-dimensional kernel. |
| 367 | /// | |
| 368 | /// For $G: ℝ → ℝ$, this is the function $F(x\_1, …, x\_n) := \prod_{i=1}^n G(x\_i)$. | |
| 35 | 369 | /// The kernel $G$ typically implements [`Support`] and [`Mapping`] |
|
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
|
370 | /// on [`Loc<1, F>`]. Then the product implements them on [`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
|
371 | #[derive(Copy, Clone, Serialize, Debug, Eq, PartialEq)] |
| 35 | 372 | #[allow(dead_code)] |
|
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
|
373 | struct UniformProduct<G, const N: usize>( |
| 0 | 374 | /// The one-dimensional kernel |
|
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
|
375 | G, |
| 0 | 376 | ); |
| 377 | ||
|
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
|
378 | impl<'a, G, F: Float, const N: usize> Mapping<Loc<N, F>> for UniformProduct<G, N> |
| 35 | 379 | 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
|
380 | G: Mapping<Loc<1, F>, Codomain = F>, |
| 35 | 381 | { |
| 382 | type Codomain = F; | |
| 383 | ||
| 0 | 384 | #[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
|
385 | fn apply<I: Instance<Loc<N, F>>>(&self, x: 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
|
386 | x.decompose() |
|
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
|
387 | .iter() |
|
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
|
388 | .map(|&y| self.0.apply(Loc([y]))) |
|
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
|
389 | .product() |
| 0 | 390 | } |
| 391 | } | |
| 392 | ||
|
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
|
393 | impl<'a, G, F: Float, const N: usize> DifferentiableImpl<Loc<N, F>> for UniformProduct<G, N> |
| 35 | 394 | 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
|
395 | G: DifferentiableMapping<Loc<1, F>, DerivativeDomain = F, Codomain = F>, |
| 35 | 396 | { |
|
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
|
397 | type Derivative = Loc<N, F>; |
| 35 | 398 | |
| 0 | 399 | #[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
|
400 | fn differential_impl<I: Instance<Loc<N, F>>>(&self, x0: I) -> Loc<N, F> { |
| 35 | 401 | x0.eval(|x| { |
| 402 | let vs = x.map(|y| self.0.apply(Loc([y]))); | |
| 403 | product_differential(x, &vs, |y| self.0.differential(Loc([y]))) | |
| 404 | }) | |
| 32 | 405 | } |
| 406 | } | |
| 407 | ||
| 408 | /// Helper function to calulate the differential of $f(x)=∏_{i=1}^N g(x_i)$. | |
| 409 | /// | |
| 410 | /// The vector `x` is the location, `vs` consists of the values `g(x_i)`, and | |
| 411 | /// `gd` calculates the derivative `g'`. | |
| 412 | #[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
|
413 | pub(crate) fn product_differential<F: Float, G: Fn(F) -> F, 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
|
414 | x: &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
|
415 | vs: &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
|
416 | gd: G, |
|
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
|
417 | ) -> Loc<N, F> { |
| 32 | 418 | map1_indexed(x, |i, &y| { |
|
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
|
419 | gd(y) |
|
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
|
420 | * vs.iter() |
|
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
|
421 | .zip(0..) |
|
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
|
422 | .filter_map(|(v, j)| (j != i).then_some(*v)) |
|
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
|
423 | .product() |
|
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 | }) |
|
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 | .into() |
| 32 | 426 | } |
| 427 | ||
| 35 | 428 | /// Helper function to calulate the Lipschitz factor of $∇f$ for $f(x)=∏_{i=1}^N g(x_i)$. |
| 429 | /// | |
| 430 | /// The parameter `bound` is a bound on $|g|_∞$, `lip` is a Lipschitz factor for $g$, | |
| 431 | /// `dbound` is a bound on $|∇g|_∞$, and `dlip` a Lipschitz factor for $∇g$. | |
| 432 | #[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
|
433 | pub(crate) fn product_differential_lipschitz_factor<F: Float, 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
|
434 | bound: 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
|
435 | lip: 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
|
436 | dbound: 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
|
437 | dlip: 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 | ) -> DynResult<F> { |
| 35 | 439 | // For arbitrary ψ(x) = ∏_{i=1}^n ψ_i(x_i), we have |
| 440 | // ψ(x) - ψ(y) = ∑_i [ψ_i(x_i)-ψ_i(y_i)] ∏_{j ≠ i} ψ_j(x_j) | |
| 441 | // by a simple recursive argument. In particular, if ψ_i=g for all i, j, we have | |
| 442 | // |ψ(x) - ψ(y)| ≤ ∑_i L_g M_g^{n-1}|x-y|, where L_g is the Lipschitz factor of g, and | |
| 443 | // M_g a bound on it. | |
| 444 | // | |
| 445 | // We also have in the general case ∇ψ(x) = ∑_i ∇ψ_i(x_i) ∏_{j ≠ i} ψ_j(x_j), whence | |
| 446 | // using the previous formula for each i with f_i=∇ψ_i and f_j=ψ_j for j ≠ i, we get | |
| 447 | // ∇ψ(x) - ∇ψ(y) = ∑_i[ ∇ψ_i(x_i)∏_{j ≠ i} ψ_j(x_j) - ∇ψ_i(y_i)∏_{j ≠ i} ψ_j(y_j)] | |
| 448 | // = ∑_i[ [∇ψ_i(x_i) - ∇ψ_j(x_j)] ∏_{j ≠ i}ψ_j(x_j) | |
| 449 | // + [∑_{k ≠ i} [ψ_k(x_k) - ∇ψ_k(x_k)] ∏_{j ≠ i, k}ψ_j(x_j)]∇ψ_i(x_i)]. | |
| 450 | // With $ψ_i=g for all i, j, it follows that | |
| 451 | // |∇ψ(x) - ∇ψ(y)| ≤ ∑_i L_{∇g} M_g^{n-1} + ∑_{k ≠ i} L_g M_g^{n-2} M_{∇g} | |
| 452 | // = n [L_{∇g} M_g^{n-1} + (n-1) L_g M_g^{n-2} M_{∇g}]. | |
| 453 | // = n M_g^{n-2}[L_{∇g} M_g + (n-1) L_g M_{∇g}]. | |
| 454 | if 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
|
455 | Ok(F::cast_from(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
|
456 | * bound.powi((N - 2) as i32) |
|
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 | * (dlip * bound + F::cast_from(N - 1) * lip * dbound)) |
|
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
|
458 | } else if N == 1 { |
|
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
|
459 | Ok(dlip) |
| 35 | 460 | } else { |
|
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
|
461 | Err(anyhow!("Invalid dimension")) |
| 32 | 462 | } |
| 463 | } | |
| 464 | ||
|
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
|
465 | impl<G, F: Float, const N: usize> Support<N, F> for UniformProduct<G, 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
|
466 | 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
|
467 | G: Support<1, 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
|
468 | { |
| 0 | 469 | #[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
|
470 | fn support_hint(&self) -> Cube<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
|
471 | let [a]: [[F; 2]; 1] = self.0.support_hint().into(); |
| 0 | 472 | array_init(|| a.clone()).into() |
| 473 | } | |
| 474 | ||
| 475 | #[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
|
476 | fn in_support(&self, x: &Loc<N, F>) -> bool { |
| 0 | 477 | x.iter().all(|&y| self.0.in_support(&Loc([y]))) |
| 478 | } | |
| 479 | ||
| 480 | #[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
|
481 | fn bisection_hint(&self, cube: &Cube<N, F>) -> [Option<F>; N] { |
| 0 | 482 | cube.map(|a, b| { |
| 483 | let [h] = self.0.bisection_hint(&[[a, b]].into()); | |
| 484 | h | |
| 485 | }) | |
| 486 | } | |
| 487 | } | |
| 488 | ||
|
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
|
489 | impl<G, F: Float, const N: usize> GlobalAnalysis<F, Bounds<F>> for UniformProduct<G, 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
|
490 | 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
|
491 | G: 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
|
492 | { |
| 0 | 493 | #[inline] |
| 494 | fn global_analysis(&self) -> Bounds<F> { | |
| 495 | let g = self.0.global_analysis(); | |
| 496 | (0..N).map(|_| g).product() | |
| 497 | } | |
| 498 | } | |
| 499 | ||
|
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
|
500 | impl<G, F: Float, const N: usize> LocalAnalysis<F, Bounds<F>, N> for UniformProduct<G, 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
|
501 | 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
|
502 | G: LocalAnalysis<F, Bounds<F>, 1>, |
|
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
|
503 | { |
| 0 | 504 | #[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
|
505 | fn local_analysis(&self, cube: &Cube<N, 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
|
506 | cube.iter_coords() |
|
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
|
507 | .map(|&[a, b]| self.0.local_analysis(&([[a, b]].into()))) |
|
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
|
508 | .product() |
| 0 | 509 | } |
| 510 | } | |
| 511 | ||
| 512 | macro_rules! product_lpnorm { | |
| 513 | ($lp:ident) => { | |
|
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
|
514 | impl<G, F: Float, const N: usize> Norm<$lp, F> for UniformProduct<G, 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
|
515 | 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
|
516 | G: Norm<$lp, 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
|
517 | { |
| 0 | 518 | #[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
|
519 | fn norm(&self, lp: $lp) -> F { |
| 0 | 520 | self.0.norm(lp).powi(N as i32) |
| 521 | } | |
| 522 | } | |
|
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
|
523 | }; |
| 0 | 524 | } |
| 525 | ||
| 526 | product_lpnorm!(L1); | |
| 527 | product_lpnorm!(L2); | |
| 528 | product_lpnorm!(Linfinity); | |
| 529 | ||
| 530 | /// Trait for bounding one kernel with respect to another. | |
| 531 | /// | |
| 532 | /// The type `F` is the scalar field, and `T` another kernel to which `Self` is compared. | |
|
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
|
533 | pub trait BoundedBy<F: Num, T> { |
| 0 | 534 | /// Calclate a bounding factor $c$ such that the Fourier transforms $ℱ\[v\] ≤ c ℱ\[u\]$ for |
| 535 | /// $v$ `self` and $u$ `other`. | |
| 536 | /// | |
| 537 | /// If no such factors exits, `None` is returned. | |
|
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
|
538 | fn bounding_factor(&self, other: &T) -> DynResult<F>; |
| 0 | 539 | } |
| 540 | ||
| 541 | /// This [`BoundedBy`] implementation bounds $(uv) * (uv)$ by $(ψ * ψ) u$. | |
| 542 | #[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
|
543 | impl<F, C, BaseP> BoundedBy<F, SupportProductFirst<AutoConvolution<C>, BaseP>> |
|
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
|
544 | for AutoConvolution<SupportProductFirst<C, BaseP>> |
|
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
|
545 | 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
|
546 | F: Float, |
|
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
|
547 | C: PartialEq, |
|
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
|
548 | BaseP: Fourier<F> + PartialOrd, // TODO: replace by BoundedBy, |
|
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
|
549 | <BaseP as Fourier<F>>::Transformed: Bounded<F> + Norm<L1, 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
|
550 | { |
|
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
|
551 | fn bounding_factor( |
|
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
|
552 | &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
|
553 | kernel: &SupportProductFirst<AutoConvolution<C>, BaseP>, |
|
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
|
554 | ) -> DynResult<F> { |
| 0 | 555 | let SupportProductFirst(AutoConvolution(ref cutoff2), base_spread2) = kernel; |
| 556 | let AutoConvolution(SupportProductFirst(ref cutoff, ref base_spread)) = self; | |
| 557 | let v̂ = base_spread.fourier(); | |
| 558 | ||
| 559 | // Verify that the cut-off and ideal physical model (base spread) are the same. | |
|
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
|
560 | if cutoff == cutoff2 && base_spread <= base_spread2 && v̂.bounds().lower() >= 0.0 { |
| 0 | 561 | // Calculate the factor between the convolution approximation |
| 562 | // `AutoConvolution<SupportProductFirst<C, BaseP>>` of $A_*A$ and the | |
| 563 | // kernel of the seminorm. This depends on the physical model P being | |
| 564 | // `SupportProductFirst<C, BaseP>` with the kernel `K` being | |
| 565 | // a `SupportSum` involving `SupportProductFirst<AutoConvolution<C>, BaseP>`. | |
|
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
|
566 | Ok(v̂.norm(L1)) |
| 0 | 567 | } else { |
| 568 | // We cannot compare | |
|
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
|
569 | Err(anyhow!("Incomprable kernels")) |
| 0 | 570 | } |
| 571 | } | |
| 572 | } | |
| 573 | ||
|
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
|
574 | impl<F: Float, A, B, C> BoundedBy<F, SupportSum<B, C>> for A |
|
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
|
575 | 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
|
576 | A: BoundedBy<F, B>, |
|
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
|
577 | C: Bounded<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
|
578 | { |
| 0 | 579 | #[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
|
580 | fn bounding_factor(&self, SupportSum(ref kernel1, kernel2): &SupportSum<B, C>) -> DynResult<F> { |
| 0 | 581 | if kernel2.bounds().lower() >= 0.0 { |
| 582 | self.bounding_factor(kernel1) | |
| 583 | } else { | |
|
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
|
584 | Err(anyhow!("Component kernel not lower-bounded by zero")) |
| 0 | 585 | } |
| 586 | } | |
| 587 | } | |
| 588 | ||
| 589 | /// Generates on $[a, b]$ [`Support::support_hint`] for a symmetric interval $[-r, r]$. | |
| 590 | /// | |
| 591 | /// It will attempt to place the subdivision point at $-r$ or $r$. | |
| 592 | /// If neither of these points lies within $[a, b]$, `None` is returned. | |
| 593 | #[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
|
594 | pub(super) fn symmetric_interval_hint<F: Float>(r: F, a: F, b: F) -> Option<F> { |
| 0 | 595 | if a < -r && -r < b { |
| 596 | Some(-r) | |
| 597 | } else if a < r && r < b { | |
| 598 | Some(r) | |
| 599 | } else { | |
| 600 | None | |
| 601 | } | |
| 602 | } | |
| 603 | ||
| 604 | /// Generates on $[a, b]$ [`Support::support_hint`] for a function with monotone derivative, | |
| 605 | /// support on $[-r, r]$ and peak at $0. | |
| 606 | /// | |
| 607 | /// It will attempt to place the subdivision point at $-r$, $r$, or $0$, depending on which | |
| 608 | /// gives the longer length for the shorter of the two subintervals. If none of these points | |
| 609 | /// lies within $[a, b]$, or the resulting interval would be shorter than $0.3r$, `None` is | |
| 610 | /// returned. | |
| 611 | #[replace_float_literals(F::cast_from(literal))] | |
| 612 | #[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
|
613 | pub(super) fn symmetric_peak_hint<F: Float>(r: F, a: F, b: F) -> Option<F> { |
| 0 | 614 | let stage1 = if a < -r { |
| 615 | if b <= -r { | |
| 616 | None | |
| 617 | } else if a + r < -b { | |
| 618 | Some(-r) | |
| 619 | } else { | |
| 620 | Some(0.0) | |
| 621 | } | |
| 622 | } else if a < 0.0 { | |
| 623 | if b <= 0.0 { | |
| 624 | None | |
| 625 | } else if a < r - b { | |
| 626 | Some(0.0) | |
| 627 | } else { | |
| 628 | Some(r) | |
| 629 | } | |
| 630 | } else if a < r && b > r { | |
| 631 | Some(r) | |
| 632 | } else { | |
| 633 | None | |
| 634 | }; | |
| 635 | ||
| 636 | // Ignore stage1 hint if either side of subdivision would be just a small fraction of the | |
| 637 | // interval | |
| 638 | match stage1 { | |
|
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
|
639 | Some(h) if (h - a).min(b - h) >= 0.3 * r => Some(h), |
|
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
|
640 | _ => None, |
| 0 | 641 | } |
| 642 | } |