Mon, 06 Jan 2025 20:29:25 -0500
More Serialize / Deserialize / Debug derives
0 | 1 | |
5 | 2 | /*! |
75
e9f4550cfa18
Fix out-of-date references in doc comments
Tuomo Valkonen <tuomov@iki.fi>
parents:
68
diff
changeset
|
3 | Traits for representing the support of a [`Mapping`], and analysing the mapping on a [`Cube`]. |
5 | 4 | */ |
86
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
5 | use serde::{Serialize, Deserialize}; |
0 | 6 | use std::ops::{MulAssign,DivAssign,Neg}; |
5 | 7 | use crate::types::{Float, Num}; |
8 | use crate::maputil::map2; | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
9 | use crate::mapping::{ |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
10 | Instance, Mapping, DifferentiableImpl, DifferentiableMapping, Space |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
11 | }; |
5 | 12 | use crate::sets::Cube; |
13 | use crate::loc::Loc; | |
0 | 14 | use super::aggregator::Bounds; |
15 | use crate::norms::{Norm, L1, L2, Linfinity}; | |
68
c5f70e767511
Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
16 | pub use crate::operator_arithmetic::{Weighted, Constant}; |
0 | 17 | |
75
e9f4550cfa18
Fix out-of-date references in doc comments
Tuomo Valkonen <tuomov@iki.fi>
parents:
68
diff
changeset
|
18 | /// A trait for working with the supports of [`Mapping`]s. |
5 | 19 | /// |
75
e9f4550cfa18
Fix out-of-date references in doc comments
Tuomo Valkonen <tuomov@iki.fi>
parents:
68
diff
changeset
|
20 | /// `Mapping` is not a super-trait to allow more general use. |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
21 | pub trait Support<F : Num, const N : usize> : Sized + Sync + Send + 'static { |
5 | 22 | /// Return a cube containing the support of the function represented by `self`. |
23 | /// | |
24 | /// The hint may be larger than the actual support, but must contain it. | |
0 | 25 | fn support_hint(&self) -> Cube<F,N>; |
26 | ||
5 | 27 | /// Indicate whether `x` is in the support of the function represented by `self`. |
0 | 28 | fn in_support(&self, x : &Loc<F,N>) -> bool; |
29 | ||
5 | 30 | // Indicate whether `cube` is fully in the support of the function represented by `self`. |
0 | 31 | //fn fully_in_support(&self, cube : &Cube<F,N>) -> bool; |
32 | ||
5 | 33 | /// Return an optional hint for bisecting the support. |
34 | /// | |
35 | /// The output along each axis a possible coordinate at which to bisect `cube`. | |
36 | /// | |
37 | /// This is useful for nonsmooth functions to make finite element models as used by | |
38 | /// [`BTFN`][super::btfn::BTFN] minimisation/maximisation compatible with points of | |
39 | /// non-differentiability. | |
40 | /// | |
41 | /// The default implementation returns `[None; N]`. | |
0 | 42 | #[inline] |
5 | 43 | #[allow(unused_variables)] |
44 | fn bisection_hint(&self, cube : &Cube<F, N>) -> [Option<F>; N] { | |
45 | [None; N] | |
0 | 46 | } |
47 | ||
5 | 48 | /// Translate `self` by `x`. |
0 | 49 | #[inline] |
50 | fn shift(self, x : Loc<F, N>) -> Shift<Self, F, N> { | |
51 | Shift { shift : x, base_fn : self } | |
52 | } | |
53 | } | |
54 | ||
75
e9f4550cfa18
Fix out-of-date references in doc comments
Tuomo Valkonen <tuomov@iki.fi>
parents:
68
diff
changeset
|
55 | /// Trait for globally analysing a property `A` of a [`Mapping`]. |
5 | 56 | /// |
57 | /// Typically `A` is an [`Aggregator`][super::aggregator::Aggregator] such as | |
58 | /// [`Bounds`][super::aggregator::Bounds]. | |
0 | 59 | pub trait GlobalAnalysis<F : Num, A> { |
5 | 60 | /// Perform global analysis of the property `A` of `Self`. |
61 | /// | |
62 | /// As an example, in the case of `A` being [`Bounds`][super::aggregator::Bounds], | |
63 | /// this function will return global upper and lower bounds for the mapping | |
64 | /// represented by `self`. | |
0 | 65 | fn global_analysis(&self) -> A; |
66 | } | |
67 | ||
68 | // default impl<F, A, N, L> GlobalAnalysis<F, A, N> for L | |
69 | // where L : LocalAnalysis<F, A, N> { | |
70 | // #[inline] | |
71 | // fn global_analysis(&self) -> Bounds<F> { | |
72 | // self.local_analysis(&self.support_hint()) | |
73 | // } | |
74 | // } | |
75 | ||
75
e9f4550cfa18
Fix out-of-date references in doc comments
Tuomo Valkonen <tuomov@iki.fi>
parents:
68
diff
changeset
|
76 | /// Trait for locally analysing a property `A` of a [`Mapping`] (implementing [`Support`]) |
5 | 77 | /// within a [`Cube`]. |
78 | /// | |
79 | /// Typically `A` is an [`Aggregator`][super::aggregator::Aggregator] such as | |
80 | /// [`Bounds`][super::aggregator::Bounds]. | |
0 | 81 | pub trait LocalAnalysis<F : Num, A, const N : usize> : GlobalAnalysis<F, A> + Support<F, N> { |
5 | 82 | /// Perform local analysis of the property `A` of `Self`. |
83 | /// | |
84 | /// As an example, in the case of `A` being [`Bounds`][super::aggregator::Bounds], | |
85 | /// this function will return upper and lower bounds within `cube` for the mapping | |
86 | /// represented by `self`. | |
0 | 87 | fn local_analysis(&self, cube : &Cube<F, N>) -> A; |
88 | } | |
89 | ||
75
e9f4550cfa18
Fix out-of-date references in doc comments
Tuomo Valkonen <tuomov@iki.fi>
parents:
68
diff
changeset
|
90 | /// Trait for determining the upper and lower bounds of an float-valued [`Mapping`]. |
5 | 91 | /// |
0 | 92 | /// This is a blanket-implemented alias for [`GlobalAnalysis`]`<F, Bounds<F>>` |
75
e9f4550cfa18
Fix out-of-date references in doc comments
Tuomo Valkonen <tuomov@iki.fi>
parents:
68
diff
changeset
|
93 | /// [`Mapping`] is not a supertrait to allow flexibility in the implementation of either |
0 | 94 | /// reference or non-reference arguments. |
95 | pub trait Bounded<F : Float> : GlobalAnalysis<F, Bounds<F>> { | |
96 | /// Return lower and upper bounds for the values of of `self`. | |
97 | #[inline] | |
98 | fn bounds(&self) -> Bounds<F> { | |
99 | self.global_analysis() | |
100 | } | |
101 | } | |
102 | ||
103 | impl<F : Float, T : GlobalAnalysis<F, Bounds<F>>> Bounded<F> for T { } | |
104 | ||
75
e9f4550cfa18
Fix out-of-date references in doc comments
Tuomo Valkonen <tuomov@iki.fi>
parents:
68
diff
changeset
|
105 | /// Shift of [`Support`] and [`Mapping`]; output of [`Support::shift`]. |
86
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
106 | #[derive(Copy,Clone,Debug,Serialize,Deserialize)] |
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
107 | #[serde(bound( |
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
108 | serialize = "Loc<F, N> : Serialize, |
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
109 | T : Serialize,", |
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
110 | deserialize = "Loc<F, N> : for<'a> Deserialize<'a>, |
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
111 | T : for<'a> Deserialize<'a>", |
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
112 | ))] |
0 | 113 | pub struct Shift<T, F, const N : usize> { |
114 | shift : Loc<F, N>, | |
115 | base_fn : T, | |
116 | } | |
117 | ||
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
118 | impl<'a, T, V : Space, F : Float, const N : usize> Mapping<Loc<F, N>> for Shift<T,F,N> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
119 | where T : Mapping<Loc<F, N>, Codomain=V> { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
120 | type Codomain = V; |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
121 | |
0 | 122 | #[inline] |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
123 | fn apply<I : Instance<Loc<F, N>>>(&self, x : I) -> Self::Codomain { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
124 | self.base_fn.apply(x.own() - &self.shift) |
0 | 125 | } |
126 | } | |
127 | ||
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
128 | impl<'a, T, V : Space, F : Float, const N : usize> DifferentiableImpl<Loc<F, N>> for Shift<T,F,N> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
129 | where T : DifferentiableMapping<Loc<F, N>, DerivativeDomain=V> { |
47
a0db98c16ab5
Some Differentiable simplifications and clarifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
130 | type Derivative = V; |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
131 | |
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
132 | #[inline] |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
133 | fn differential_impl<I : Instance<Loc<F, N>>>(&self, x : I) -> Self::Derivative { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
134 | self.base_fn.differential(x.own() - &self.shift) |
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
135 | } |
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
136 | } |
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
137 | |
0 | 138 | impl<'a, T, F : Float, const N : usize> Support<F,N> for Shift<T,F,N> |
139 | where T : Support<F, N> { | |
140 | #[inline] | |
141 | fn support_hint(&self) -> Cube<F,N> { | |
142 | self.base_fn.support_hint().shift(&self.shift) | |
143 | } | |
144 | ||
145 | #[inline] | |
146 | fn in_support(&self, x : &Loc<F,N>) -> bool { | |
147 | self.base_fn.in_support(&(x - &self.shift)) | |
148 | } | |
149 | ||
150 | // fn fully_in_support(&self, _cube : &Cube<F,N>) -> bool { | |
151 | // //self.base_fn.fully_in_support(cube.shift(&vectorneg(self.shift))) | |
152 | // todo!("Not implemented, but not used at the moment") | |
153 | // } | |
154 | ||
155 | #[inline] | |
156 | fn bisection_hint(&self, cube : &Cube<F,N>) -> [Option<F>; N] { | |
157 | let base_hint = self.base_fn.bisection_hint(cube); | |
158 | map2(base_hint, &self.shift, |h, s| h.map(|z| z + *s)) | |
159 | } | |
160 | ||
161 | } | |
162 | ||
163 | impl<'a, T, F : Float, const N : usize> GlobalAnalysis<F, Bounds<F>> for Shift<T,F,N> | |
164 | where T : LocalAnalysis<F, Bounds<F>, N> { | |
165 | #[inline] | |
166 | fn global_analysis(&self) -> Bounds<F> { | |
167 | self.base_fn.global_analysis() | |
168 | } | |
169 | } | |
170 | ||
171 | impl<'a, T, F : Float, const N : usize> LocalAnalysis<F, Bounds<F>, N> for Shift<T,F,N> | |
172 | where T : LocalAnalysis<F, Bounds<F>, N> { | |
173 | #[inline] | |
174 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
175 | self.base_fn.local_analysis(&cube.shift(&(-self.shift))) | |
176 | } | |
177 | } | |
178 | ||
179 | macro_rules! impl_shift_norm { | |
180 | ($($norm:ident)*) => { $( | |
181 | impl<'a, T, F : Float, const N : usize> Norm<F, $norm> for Shift<T,F,N> | |
182 | where T : Norm<F, $norm> { | |
183 | #[inline] | |
184 | fn norm(&self, n : $norm) -> F { | |
185 | self.base_fn.norm(n) | |
186 | } | |
187 | } | |
188 | )* } | |
189 | } | |
190 | ||
191 | impl_shift_norm!(L1 L2 Linfinity); | |
192 | ||
193 | impl<'a, T, F : Float, C, const N : usize> Support<F,N> for Weighted<T, C> | |
194 | where T : Support<F, N>, | |
195 | C : Constant<Type=F> { | |
196 | ||
197 | #[inline] | |
198 | fn support_hint(&self) -> Cube<F,N> { | |
199 | self.base_fn.support_hint() | |
200 | } | |
201 | ||
202 | #[inline] | |
203 | fn in_support(&self, x : &Loc<F,N>) -> bool { | |
204 | self.base_fn.in_support(x) | |
205 | } | |
206 | ||
207 | // fn fully_in_support(&self, cube : &Cube<F,N>) -> bool { | |
208 | // self.base_fn.fully_in_support(cube) | |
209 | // } | |
210 | ||
211 | #[inline] | |
212 | fn bisection_hint(&self, cube : &Cube<F,N>) -> [Option<F>; N] { | |
213 | self.base_fn.bisection_hint(cube) | |
214 | } | |
215 | } | |
216 | ||
217 | impl<'a, T, F : Float, C> GlobalAnalysis<F, Bounds<F>> for Weighted<T, C> | |
218 | where T : GlobalAnalysis<F, Bounds<F>>, | |
219 | C : Constant<Type=F> { | |
220 | #[inline] | |
221 | fn global_analysis(&self) -> Bounds<F> { | |
222 | let Bounds(lower, upper) = self.base_fn.global_analysis(); | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
223 | debug_assert!(lower <= upper); |
0 | 224 | match self.weight.value() { |
225 | w if w < F::ZERO => Bounds(w * upper, w * lower), | |
226 | w => Bounds(w * lower, w * upper), | |
227 | } | |
228 | } | |
229 | } | |
230 | ||
231 | impl<'a, T, F : Float, C, const N : usize> LocalAnalysis<F, Bounds<F>, N> for Weighted<T, C> | |
232 | where T : LocalAnalysis<F, Bounds<F>, N>, | |
233 | C : Constant<Type=F> { | |
234 | #[inline] | |
235 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
236 | let Bounds(lower, upper) = self.base_fn.local_analysis(cube); | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
237 | debug_assert!(lower <= upper); |
0 | 238 | match self.weight.value() { |
239 | w if w < F::ZERO => Bounds(w * upper, w * lower), | |
240 | w => Bounds(w * lower, w * upper), | |
241 | } | |
242 | } | |
243 | } | |
244 | ||
245 | macro_rules! make_weighted_scalarop_rhs { | |
246 | ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => { | |
247 | impl<F : Float, T> std::ops::$trait_assign<F> for Weighted<T, F> { | |
248 | #[inline] | |
249 | fn $fn_assign(&mut self, t : F) { | |
250 | self.weight.$fn_assign(t); | |
251 | } | |
252 | } | |
253 | ||
254 | impl<'a, F : Float, T> std::ops::$trait<F> for Weighted<T, F> { | |
255 | type Output = Self; | |
256 | #[inline] | |
257 | fn $fn(mut self, t : F) -> Self { | |
258 | self.weight.$fn_assign(t); | |
259 | self | |
260 | } | |
261 | } | |
262 | ||
263 | impl<'a, F : Float, T> std::ops::$trait<F> for &'a Weighted<T, F> | |
264 | where T : Clone { | |
265 | type Output = Weighted<T, F>; | |
266 | #[inline] | |
267 | fn $fn(self, t : F) -> Self::Output { | |
268 | Weighted { weight : self.weight.$fn(t), base_fn : self.base_fn.clone() } | |
269 | } | |
270 | } | |
271 | } | |
272 | } | |
273 | ||
274 | make_weighted_scalarop_rhs!(Mul, mul, MulAssign, mul_assign); | |
275 | make_weighted_scalarop_rhs!(Div, div, DivAssign, div_assign); | |
276 | ||
277 | macro_rules! impl_weighted_norm { | |
278 | ($($norm:ident)*) => { $( | |
279 | impl<'a, T, F : Float> Norm<F, $norm> for Weighted<T,F> | |
280 | where T : Norm<F, $norm> { | |
281 | #[inline] | |
282 | fn norm(&self, n : $norm) -> F { | |
283 | self.base_fn.norm(n) * self.weight.abs() | |
284 | } | |
285 | } | |
286 | )* } | |
287 | } | |
288 | ||
289 | impl_weighted_norm!(L1 L2 Linfinity); | |
290 | ||
291 | ||
75
e9f4550cfa18
Fix out-of-date references in doc comments
Tuomo Valkonen <tuomov@iki.fi>
parents:
68
diff
changeset
|
292 | /// Normalisation of [`Support`] and [`Mapping`] to L¹ norm 1. |
5 | 293 | /// |
0 | 294 | /// Currently only scalar-valued functions are supported. |
295 | #[derive(Copy, Clone, Debug, Serialize, PartialEq)] | |
5 | 296 | pub struct Normalised<T>( |
75
e9f4550cfa18
Fix out-of-date references in doc comments
Tuomo Valkonen <tuomov@iki.fi>
parents:
68
diff
changeset
|
297 | /// The base [`Support`] or [`Mapping`]. |
5 | 298 | pub T |
299 | ); | |
0 | 300 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
301 | impl<'a, T, F : Float, const N : usize> Mapping<Loc<F, N>> for Normalised<T> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
302 | where T : Norm<F, L1> + Mapping<Loc<F,N>, Codomain=F> { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
303 | type Codomain = F; |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
304 | |
0 | 305 | #[inline] |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
306 | fn apply<I : Instance<Loc<F, N>>>(&self, x : I) -> Self::Codomain { |
0 | 307 | let w = self.0.norm(L1); |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
308 | if w == F::ZERO { F::ZERO } else { self.0.apply(x) / w } |
0 | 309 | } |
310 | } | |
311 | ||
312 | impl<'a, T, F : Float, const N : usize> Support<F,N> for Normalised<T> | |
313 | where T : Norm<F, L1> + Support<F, N> { | |
314 | ||
315 | #[inline] | |
316 | fn support_hint(&self) -> Cube<F,N> { | |
317 | self.0.support_hint() | |
318 | } | |
319 | ||
320 | #[inline] | |
321 | fn in_support(&self, x : &Loc<F,N>) -> bool { | |
322 | self.0.in_support(x) | |
323 | } | |
324 | ||
325 | // fn fully_in_support(&self, cube : &Cube<F,N>) -> bool { | |
326 | // self.0.fully_in_support(cube) | |
327 | // } | |
328 | ||
329 | #[inline] | |
330 | fn bisection_hint(&self, cube : &Cube<F,N>) -> [Option<F>; N] { | |
331 | self.0.bisection_hint(cube) | |
332 | } | |
333 | } | |
334 | ||
335 | impl<'a, T, F : Float> GlobalAnalysis<F, Bounds<F>> for Normalised<T> | |
336 | where T : Norm<F, L1> + GlobalAnalysis<F, Bounds<F>> { | |
337 | #[inline] | |
338 | fn global_analysis(&self) -> Bounds<F> { | |
339 | let Bounds(lower, upper) = self.0.global_analysis(); | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
340 | debug_assert!(lower <= upper); |
0 | 341 | let w = self.0.norm(L1); |
342 | debug_assert!(w >= F::ZERO); | |
343 | Bounds(w * lower, w * upper) | |
344 | } | |
345 | } | |
346 | ||
347 | impl<'a, T, F : Float, const N : usize> LocalAnalysis<F, Bounds<F>, N> for Normalised<T> | |
348 | where T : Norm<F, L1> + LocalAnalysis<F, Bounds<F>, N> { | |
349 | #[inline] | |
350 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
351 | let Bounds(lower, upper) = self.0.local_analysis(cube); | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
352 | debug_assert!(lower <= upper); |
0 | 353 | let w = self.0.norm(L1); |
354 | debug_assert!(w >= F::ZERO); | |
355 | Bounds(w * lower, w * upper) | |
356 | } | |
357 | } | |
358 | ||
359 | impl<'a, T, F : Float> Norm<F, L1> for Normalised<T> | |
360 | where T : Norm<F, L1> { | |
361 | #[inline] | |
362 | fn norm(&self, _ : L1) -> F { | |
363 | let w = self.0.norm(L1); | |
364 | if w == F::ZERO { F::ZERO } else { F::ONE } | |
365 | } | |
366 | } | |
367 | ||
368 | macro_rules! impl_normalised_norm { | |
369 | ($($norm:ident)*) => { $( | |
370 | impl<'a, T, F : Float> Norm<F, $norm> for Normalised<T> | |
371 | where T : Norm<F, $norm> + Norm<F, L1> { | |
372 | #[inline] | |
373 | fn norm(&self, n : $norm) -> F { | |
374 | let w = self.0.norm(L1); | |
375 | if w == F::ZERO { F::ZERO } else { self.0.norm(n) / w } | |
376 | } | |
377 | } | |
378 | )* } | |
379 | } | |
380 | ||
381 | impl_normalised_norm!(L2 Linfinity); | |
382 | ||
383 | /* | |
384 | impl<F : Num, S : Support<F, N>, const N : usize> LocalAnalysis<F, NullAggregator, N> for S { | |
385 | fn local_analysis(&self, _cube : &Cube<F, N>) -> NullAggregator { NullAggregator } | |
386 | } | |
387 | ||
388 | impl<F : Float, S : Bounded<F>, const N : usize> LocalAnalysis<F, Bounds<F>, N> for S { | |
389 | #[inline] | |
390 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
391 | self.bounds(cube) | |
392 | } | |
393 | }*/ | |
394 | ||
5 | 395 | /// Generator of [`Support`]-implementing component functions based on low storage requirement |
396 | /// [ids][`Self::Id`]. | |
0 | 397 | pub trait SupportGenerator<F : Float, const N : usize> |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
398 | : MulAssign<F> + DivAssign<F> + Neg<Output=Self> + Clone + Sync + Send + 'static { |
5 | 399 | /// The identification type |
0 | 400 | type Id : 'static + Copy; |
75
e9f4550cfa18
Fix out-of-date references in doc comments
Tuomo Valkonen <tuomov@iki.fi>
parents:
68
diff
changeset
|
401 | /// The type of the [`Support`] (often also a [`Mapping`]). |
0 | 402 | type SupportType : 'static + Support<F, N>; |
5 | 403 | /// An iterator over all the [`Support`]s of the generator. |
0 | 404 | type AllDataIter<'a> : Iterator<Item=(Self::Id, Self::SupportType)> where Self : 'a; |
405 | ||
5 | 406 | /// Returns the component identified by `id`. |
407 | /// | |
408 | /// Panics if `id` is an invalid identifier. | |
409 | fn support_for(&self, id : Self::Id) -> Self::SupportType; | |
0 | 410 | |
5 | 411 | /// Returns the number of different components in this generator. |
0 | 412 | fn support_count(&self) -> usize; |
413 | ||
5 | 414 | /// Returns an iterator over all pairs of `(id, support)`. |
0 | 415 | fn all_data(&self) -> Self::AllDataIter<'_>; |
416 | } | |
417 |