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