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