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