Mon, 17 Feb 2025 14:10:52 -0500
Make some math in documentation render
0 | 1 | /*! This module implements the convolution operators $𝒟$. |
2 | ||
3 | The principal data type of the module is [`ConvolutionOp`] and the main abstraction | |
4 | the trait [`DiscreteMeasureOp`]. | |
5 | */ | |
6 | ||
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
7 | use crate::measures::{DeltaMeasure, DiscreteMeasure, Radon, SpikeIter, RNDM}; |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
8 | use alg_tools::bisection_tree::*; |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
9 | use alg_tools::instance::Instance; |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
10 | use alg_tools::iter::{FilterMapX, Mappable}; |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
11 | use alg_tools::linops::{BoundedLinear, Linear, Mapping}; |
0 | 12 | use alg_tools::loc::Loc; |
13 | use alg_tools::mapping::RealMapping; | |
14 | use alg_tools::nalgebra_support::ToNalgebraRealField; | |
35 | 15 | use alg_tools::norms::Linfinity; |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
16 | use alg_tools::sets::Cube; |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
17 | use alg_tools::types::*; |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
18 | use itertools::Itertools; |
0 | 19 | use nalgebra::DMatrix; |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
20 | use std::iter::Zip; |
0 | 21 | use std::marker::PhantomData; |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
22 | use std::ops::RangeFrom; |
0 | 23 | |
24 | /// Abstraction for operators $𝒟 ∈ 𝕃(𝒵(Ω); C_c(Ω))$. | |
25 | /// | |
26 | /// Here $𝒵(Ω) ⊂ ℳ(Ω)$ is the space of sums of delta measures, presented by [`DiscreteMeasure`]. | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
27 | pub trait DiscreteMeasureOp<Domain, F>: |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
28 | BoundedLinear<DiscreteMeasure<Domain, F>, Radon, Linfinity, F> |
35 | 29 | where |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
30 | F: Float + ToNalgebraRealField, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
31 | Domain: 'static + Clone + PartialEq, |
35 | 32 | { |
0 | 33 | /// The output type of [`Self::preapply`]. |
34 | type PreCodomain; | |
35 | ||
36 | /// Creates a finite-dimensional presentatin of the operator restricted to a fixed support. | |
37 | /// | |
38 | /// <p> | |
39 | /// This returns the matrix $C_*𝒟C$, where $C ∈ 𝕃(ℝ^n; 𝒵(Ω))$, $Ca = ∑_{i=1}^n α_i δ_{x_i}$ | |
40 | /// for a $x_1, …, x_n$ the coordinates given by the iterator `I`, and $a=(α_1,…,α_n)$. | |
41 | /// Here $C_* ∈ 𝕃(C_c(Ω); ℝ^n) $ stands for the preadjoint. | |
42 | /// </p> | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
43 | fn findim_matrix<'a, I>(&self, points: I) -> DMatrix<F::MixedType> |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
44 | where |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
45 | I: ExactSizeIterator<Item = &'a Domain> + Clone; |
0 | 46 | |
35 | 47 | /// [`Mapping`] that typically returns an uninitialised [`PreBTFN`] |
0 | 48 | /// instead of a full [`BTFN`]. |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
49 | fn preapply(&self, μ: DiscreteMeasure<Domain, F>) -> Self::PreCodomain; |
0 | 50 | } |
51 | ||
52 | // Blanket implementation of a measure as a linear functional over a predual | |
53 | // (that by assumption is a linear functional over a measure). | |
54 | /*impl<F, Domain, Predual> Linear<Predual> | |
55 | for DiscreteMeasure<Domain, F> | |
56 | where F : Float + ToNalgebraRealField, | |
57 | Predual : Linear<DiscreteMeasure<Domain, F>, Codomain=F> { | |
58 | type Codomain = F; | |
59 | ||
60 | #[inline] | |
61 | fn apply(&self, ω : &Predual) -> F { | |
62 | ω.apply(self) | |
63 | } | |
64 | }*/ | |
65 | ||
66 | // | |
67 | // Convolutions for discrete measures | |
68 | // | |
69 | ||
70 | /// A trait alias for simple convolution kernels. | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
71 | pub trait SimpleConvolutionKernel<F: Float, const N: usize>: |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
72 | RealMapping<F, N> + Support<F, N> + Bounded<F> + Clone + 'static |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
73 | { |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
74 | } |
0 | 75 | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
76 | impl<T, F: Float, const N: usize> SimpleConvolutionKernel<F, N> for T where |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
77 | T: RealMapping<F, N> + Support<F, N> + Bounded<F> + Clone + 'static |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
78 | { |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
79 | } |
0 | 80 | |
81 | /// [`SupportGenerator`] for [`ConvolutionOp`]. | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
82 | #[derive(Clone, Debug)] |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
83 | pub struct ConvolutionSupportGenerator<F: Float, K, const N: usize> |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
84 | where |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
85 | K: SimpleConvolutionKernel<F, N>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
86 | { |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
87 | kernel: K, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
88 | centres: RNDM<F, N>, |
0 | 89 | } |
90 | ||
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
91 | impl<F: Float, K, const N: usize> ConvolutionSupportGenerator<F, K, N> |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
92 | where |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
93 | K: SimpleConvolutionKernel<F, N>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
94 | { |
0 | 95 | /// Construct the convolution kernel corresponding to `δ`, i.e., one centered at `δ.x` and |
96 | /// weighted by `δ.α`. | |
97 | #[inline] | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
98 | fn construct_kernel<'a>( |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
99 | &'a self, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
100 | δ: &'a DeltaMeasure<Loc<F, N>, F>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
101 | ) -> Weighted<Shift<K, F, N>, F> { |
0 | 102 | self.kernel.clone().shift(δ.x).weigh(δ.α) |
103 | } | |
104 | ||
105 | /// This is a helper method for the implementation of [`ConvolutionSupportGenerator::all_data`]. | |
106 | /// It filters out `δ` with zero weight, and otherwise returns the corresponding convolution | |
107 | /// kernel. The `id` is passed through as-is. | |
108 | #[inline] | |
109 | fn construct_kernel_and_id_filtered<'a>( | |
110 | &'a self, | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
111 | (id, δ): (usize, &'a DeltaMeasure<Loc<F, N>, F>), |
0 | 112 | ) -> Option<(usize, Weighted<Shift<K, F, N>, F>)> { |
113 | (δ.α != F::ZERO).then(|| (id.into(), self.construct_kernel(δ))) | |
114 | } | |
115 | } | |
116 | ||
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
117 | impl<F: Float, K, const N: usize> SupportGenerator<F, N> for ConvolutionSupportGenerator<F, K, N> |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
118 | where |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
119 | K: SimpleConvolutionKernel<F, N>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
120 | { |
0 | 121 | type Id = usize; |
122 | type SupportType = Weighted<Shift<K, F, N>, F>; | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
123 | type AllDataIter<'a> = FilterMapX< |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
124 | 'a, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
125 | Zip<RangeFrom<usize>, SpikeIter<'a, Loc<F, N>, F>>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
126 | Self, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
127 | (Self::Id, Self::SupportType), |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
128 | >; |
0 | 129 | |
130 | #[inline] | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
131 | fn support_for(&self, d: Self::Id) -> Self::SupportType { |
0 | 132 | self.construct_kernel(&self.centres[d]) |
133 | } | |
134 | ||
135 | #[inline] | |
136 | fn support_count(&self) -> usize { | |
137 | self.centres.len() | |
138 | } | |
139 | ||
140 | #[inline] | |
141 | fn all_data(&self) -> Self::AllDataIter<'_> { | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
142 | (0..) |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
143 | .zip(self.centres.iter_spikes()) |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
144 | .filter_mapX(self, Self::construct_kernel_and_id_filtered) |
0 | 145 | } |
146 | } | |
147 | ||
148 | /// Representation of a convolution operator $𝒟$. | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
149 | #[derive(Clone, Debug)] |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
150 | pub struct ConvolutionOp<F, K, BT, const N: usize> |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
151 | where |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
152 | F: Float + ToNalgebraRealField, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
153 | BT: BTImpl<F, N, Data = usize>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
154 | K: SimpleConvolutionKernel<F, N>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
155 | { |
35 | 156 | /// Depth of the [`BT`] bisection tree for the outputs [`Mapping::apply`]. |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
157 | depth: BT::Depth, |
35 | 158 | /// Domain of the [`BT`] bisection tree for the outputs [`Mapping::apply`]. |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
159 | domain: Cube<F, N>, |
0 | 160 | /// The convolution kernel |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
161 | kernel: K, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
162 | _phantoms: PhantomData<(F, BT)>, |
0 | 163 | } |
164 | ||
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
165 | impl<F, K, BT, const N: usize> ConvolutionOp<F, K, BT, N> |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
166 | where |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
167 | F: Float + ToNalgebraRealField, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
168 | BT: BTImpl<F, N, Data = usize>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
169 | K: SimpleConvolutionKernel<F, N>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
170 | { |
0 | 171 | /// Creates a new convolution operator $𝒟$ with `kernel` on `domain`. |
172 | /// | |
35 | 173 | /// The output of [`Mapping::apply`] is a [`BT`] of given `depth`. |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
174 | pub fn new(depth: BT::Depth, domain: Cube<F, N>, kernel: K) -> Self { |
0 | 175 | ConvolutionOp { |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
176 | depth: depth, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
177 | domain: domain, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
178 | kernel: kernel, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
179 | _phantoms: PhantomData, |
0 | 180 | } |
181 | } | |
182 | ||
183 | /// Returns the support generator for this convolution operator. | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
184 | fn support_generator(&self, μ: RNDM<F, N>) -> ConvolutionSupportGenerator<F, K, N> { |
0 | 185 | // TODO: can we avoid cloning μ? |
186 | ConvolutionSupportGenerator { | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
187 | kernel: self.kernel.clone(), |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
188 | centres: μ, |
0 | 189 | } |
190 | } | |
191 | ||
192 | /// Returns a reference to the kernel of this convolution operator. | |
193 | pub fn kernel(&self) -> &K { | |
194 | &self.kernel | |
195 | } | |
196 | } | |
197 | ||
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
198 | impl<F, K, BT, const N: usize> Mapping<RNDM<F, N>> for ConvolutionOp<F, K, BT, N> |
35 | 199 | where |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
200 | F: Float + ToNalgebraRealField, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
201 | BT: BTImpl<F, N, Data = usize>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
202 | K: SimpleConvolutionKernel<F, N>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
203 | Weighted<Shift<K, F, N>, F>: LocalAnalysis<F, BT::Agg, N>, |
35 | 204 | { |
205 | type Codomain = BTFN<F, ConvolutionSupportGenerator<F, K, N>, BT, N>; | |
0 | 206 | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
207 | fn apply<I>(&self, μ: I) -> Self::Codomain |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
208 | where |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
209 | I: Instance<RNDM<F, N>>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
210 | { |
35 | 211 | let g = self.support_generator(μ.own()); |
0 | 212 | BTFN::construct(self.domain.clone(), self.depth, g) |
213 | } | |
214 | } | |
215 | ||
35 | 216 | /// [`ConvolutionOp`]s as linear operators over [`DiscreteMeasure`]s. |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
217 | impl<F, K, BT, const N: usize> Linear<RNDM<F, N>> for ConvolutionOp<F, K, BT, N> |
35 | 218 | where |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
219 | F: Float + ToNalgebraRealField, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
220 | BT: BTImpl<F, N, Data = usize>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
221 | K: SimpleConvolutionKernel<F, N>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
222 | Weighted<Shift<K, F, N>, F>: LocalAnalysis<F, BT::Agg, N>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
223 | { |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
224 | } |
35 | 225 | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
226 | impl<F, K, BT, const N: usize> BoundedLinear<RNDM<F, N>, Radon, Linfinity, F> |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
227 | for ConvolutionOp<F, K, BT, N> |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
228 | where |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
229 | F: Float + ToNalgebraRealField, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
230 | BT: BTImpl<F, N, Data = usize>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
231 | K: SimpleConvolutionKernel<F, N>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
232 | Weighted<Shift<K, F, N>, F>: LocalAnalysis<F, BT::Agg, N>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
233 | { |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
234 | fn opnorm_bound(&self, _: Radon, _: Linfinity) -> F { |
0 | 235 | // With μ = ∑_i α_i δ_{x_i}, we have |
236 | // |𝒟μ|_∞ | |
237 | // = sup_z |∑_i α_i φ(z - x_i)| | |
238 | // ≤ sup_z ∑_i |α_i| |φ(z - x_i)| | |
239 | // ≤ ∑_i |α_i| |φ|_∞ | |
240 | // = |μ|_ℳ |φ|_∞ | |
241 | self.kernel.bounds().uniform() | |
242 | } | |
243 | } | |
244 | ||
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
245 | impl<F, K, BT, const N: usize> DiscreteMeasureOp<Loc<F, N>, F> for ConvolutionOp<F, K, BT, N> |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
246 | where |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
247 | F: Float + ToNalgebraRealField, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
248 | BT: BTImpl<F, N, Data = usize>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
249 | K: SimpleConvolutionKernel<F, N>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
250 | Weighted<Shift<K, F, N>, F>: LocalAnalysis<F, BT::Agg, N>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
251 | { |
0 | 252 | type PreCodomain = PreBTFN<F, ConvolutionSupportGenerator<F, K, N>, N>; |
253 | ||
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
254 | fn findim_matrix<'a, I>(&self, points: I) -> DMatrix<F::MixedType> |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
255 | where |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
256 | I: ExactSizeIterator<Item = &'a Loc<F, N>> + Clone, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
257 | { |
0 | 258 | // TODO: Preliminary implementation. It be best to use sparse matrices or |
259 | // possibly explicit operators without matrices | |
260 | let n = points.len(); | |
261 | let points_clone = points.clone(); | |
262 | let pairs = points.cartesian_product(points_clone); | |
263 | let kernel = &self.kernel; | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
264 | let values = pairs.map(|(x, y)| kernel.apply(y - x).to_nalgebra_mixed()); |
0 | 265 | DMatrix::from_iterator(n, n, values) |
266 | } | |
267 | ||
35 | 268 | /// A version of [`Mapping::apply`] that does not instantiate the [`BTFN`] codomain with |
0 | 269 | /// a bisection tree, instead returning a [`PreBTFN`]. This can improve performance when |
270 | /// the output is to be added as the right-hand-side operand to a proper BTFN. | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
271 | fn preapply(&self, μ: RNDM<F, N>) -> Self::PreCodomain { |
0 | 272 | BTFN::new_pre(self.support_generator(μ)) |
273 | } | |
274 | } | |
275 | ||
276 | /// Generates an scalar operation (e.g. [`std::ops::Mul`], [`std::ops::Div`]) | |
277 | /// for [`ConvolutionSupportGenerator`]. | |
278 | macro_rules! make_convolutionsupportgenerator_scalarop_rhs { | |
279 | ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => { | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
280 | impl<F: Float, K: SimpleConvolutionKernel<F, N>, const N: usize> std::ops::$trait_assign<F> |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
281 | for ConvolutionSupportGenerator<F, K, N> |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
282 | { |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
283 | fn $fn_assign(&mut self, t: F) { |
0 | 284 | self.centres.$fn_assign(t); |
285 | } | |
286 | } | |
287 | ||
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
288 | impl<F: Float, K: SimpleConvolutionKernel<F, N>, const N: usize> std::ops::$trait<F> |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
289 | for ConvolutionSupportGenerator<F, K, N> |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
290 | { |
0 | 291 | type Output = ConvolutionSupportGenerator<F, K, N>; |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
292 | fn $fn(mut self, t: F) -> Self::Output { |
0 | 293 | std::ops::$trait_assign::$fn_assign(&mut self.centres, t); |
294 | self | |
295 | } | |
296 | } | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
297 | impl<'a, F: Float, K: SimpleConvolutionKernel<F, N>, const N: usize> std::ops::$trait<F> |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
298 | for &'a ConvolutionSupportGenerator<F, K, N> |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
299 | { |
0 | 300 | type Output = ConvolutionSupportGenerator<F, K, N>; |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
301 | fn $fn(self, t: F) -> Self::Output { |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
302 | ConvolutionSupportGenerator { |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
303 | kernel: self.kernel.clone(), |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
304 | centres: (&self.centres).$fn(t), |
0 | 305 | } |
306 | } | |
307 | } | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
308 | }; |
0 | 309 | } |
310 | ||
311 | make_convolutionsupportgenerator_scalarop_rhs!(Mul, mul, MulAssign, mul_assign); | |
312 | make_convolutionsupportgenerator_scalarop_rhs!(Div, div, DivAssign, div_assign); | |
313 | ||
314 | /// Generates an unary operation (e.g. [`std::ops::Neg`]) for [`ConvolutionSupportGenerator`]. | |
315 | macro_rules! make_convolutionsupportgenerator_unaryop { | |
316 | ($trait:ident, $fn:ident) => { | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
317 | impl<F: Float, K: SimpleConvolutionKernel<F, N>, const N: usize> std::ops::$trait |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
318 | for ConvolutionSupportGenerator<F, K, N> |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
319 | { |
0 | 320 | type Output = ConvolutionSupportGenerator<F, K, N>; |
321 | fn $fn(mut self) -> Self::Output { | |
322 | self.centres = self.centres.$fn(); | |
323 | self | |
324 | } | |
325 | } | |
326 | ||
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
327 | impl<'a, F: Float, K: SimpleConvolutionKernel<F, N>, const N: usize> std::ops::$trait |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
328 | for &'a ConvolutionSupportGenerator<F, K, N> |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
329 | { |
0 | 330 | type Output = ConvolutionSupportGenerator<F, K, N>; |
331 | fn $fn(self) -> Self::Output { | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
332 | ConvolutionSupportGenerator { |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
333 | kernel: self.kernel.clone(), |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
334 | centres: (&self.centres).$fn(), |
0 | 335 | } |
336 | } | |
337 | } | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
338 | }; |
0 | 339 | } |
340 | ||
341 | make_convolutionsupportgenerator_unaryop!(Neg, neg); |