Tue, 18 Jul 2023 15:44:10 +0300
linearisation_error
| 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; | |
|
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
9 | use crate::mapping::{Apply, Differentiable}; |
| 5 | 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 |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
16 | pub trait Constant : Copy + Sync + Send + '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 | ||
|
15
e03ce15643da
Fix broken links in doc comments after Mapping -> Apply change.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
30 | /// A trait for working with the supports of [`Apply`]s. |
| 5 | 31 | /// |
|
15
e03ce15643da
Fix broken links in doc comments after Mapping -> Apply change.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
32 | /// 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
|
33 | pub trait Support<F : Num, const N : usize> : Sized + Sync + Send + 'static { |
| 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 | ||
|
15
e03ce15643da
Fix broken links in doc comments after Mapping -> Apply change.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
73 | /// Trait for globally analysing a property `A` of a [`Apply`]. |
| 5 | 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 | ||
|
15
e03ce15643da
Fix broken links in doc comments after Mapping -> Apply change.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
94 | /// Trait for locally analysing a property `A` of a [`Apply`] (implementing [`Support`]) |
| 5 | 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 | ||
|
15
e03ce15643da
Fix broken links in doc comments after Mapping -> Apply change.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
108 | /// Trait for determining the upper and lower bounds of an float-valued [`Apply`]. |
| 5 | 109 | /// |
| 0 | 110 | /// 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
|
111 | /// [`Apply`] is not a supertrait to allow flexibility in the implementation of either |
| 0 | 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 | ||
|
15
e03ce15643da
Fix broken links in doc comments after Mapping -> Apply change.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
123 | /// Shift of [`Support`] and [`Apply`]; 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 | ||
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
130 | impl<'a, T, V, F : Float, const N : usize> Apply<&'a Loc<F, N>> for Shift<T,F,N> |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
131 | where T : Apply<Loc<F, N>, Output=V> { |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
132 | type Output = V; |
| 0 | 133 | #[inline] |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
134 | fn apply(&self, x : &'a Loc<F, N>) -> Self::Output { |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
135 | self.base_fn.apply(x - &self.shift) |
| 0 | 136 | } |
| 137 | } | |
| 138 | ||
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
139 | impl<'a, T, V, F : Float, const N : usize> Apply<Loc<F, N>> for Shift<T,F,N> |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
140 | where T : Apply<Loc<F, N>, Output=V> { |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
141 | type Output = V; |
| 0 | 142 | #[inline] |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
143 | fn apply(&self, x : Loc<F, N>) -> Self::Output { |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
144 | self.base_fn.apply(x - &self.shift) |
| 0 | 145 | } |
| 146 | } | |
| 147 | ||
| 77 | 148 | impl<'a, T, V, W, F : Float, const N : usize> Differentiable<&'a Loc<F, N>> |
| 149 | for Shift<T,F,N> | |
| 150 | where T : Differentiable<Loc<F, N>, Output=V> + Apply<Loc<F, N>, Output=W> { | |
|
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
151 | type Output = V; |
|
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
152 | #[inline] |
| 77 | 153 | fn differential(&self, x : &'a Loc<F, N>) -> V { |
|
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
154 | self.base_fn.differential(x - &self.shift) |
|
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
155 | } |
| 77 | 156 | |
| 157 | #[inline] | |
| 158 | fn linearisation_error(&self, x : &'a Loc<F, N>, y : &'a Loc<F, N>) -> W { | |
| 159 | self.base_fn | |
| 160 | .linearisation_error(x - &self.shift, y - &self.shift) | |
| 161 | } | |
| 162 | ||
| 163 | #[inline] | |
| 164 | fn linearisation_error_gen(&self, x : &'a Loc<F, N>, y : &'a Loc<F, N>, z : &'a Loc<F, N>) -> W { | |
| 165 | self.base_fn | |
| 166 | .linearisation_error_gen(x - &self.shift, y - &self.shift, z - &self.shift) | |
| 167 | } | |
|
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
168 | } |
|
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
169 | |
| 77 | 170 | impl<'a, T, V, W, F : Float, const N : usize> Differentiable<Loc<F, N>> |
| 171 | for Shift<T,F,N> | |
| 172 | where T : Differentiable<Loc<F, N>, Output=V> + Apply<Loc<F, N>, Output=W> { | |
|
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
173 | type Output = V; |
|
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
174 | #[inline] |
| 77 | 175 | fn differential(&self, x : Loc<F, N>) -> V { |
|
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
176 | self.base_fn.differential(x - &self.shift) |
|
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
177 | } |
| 77 | 178 | |
| 179 | #[inline] | |
| 180 | fn linearisation_error(&self, x : Loc<F, N>, y : Loc<F, N>) -> W { | |
| 181 | self.base_fn | |
| 182 | .linearisation_error(x - &self.shift, y - &self.shift) | |
| 183 | } | |
| 184 | ||
| 185 | #[inline] | |
| 186 | fn linearisation_error_gen(&self, x : Loc<F, N>, y : Loc<F, N>, z : Loc<F, N>) -> W { | |
| 187 | self.base_fn | |
| 188 | .linearisation_error_gen(x - &self.shift, y - &self.shift, z - &self.shift) | |
| 189 | } | |
|
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
190 | } |
|
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
191 | |
| 0 | 192 | impl<'a, T, F : Float, const N : usize> Support<F,N> for Shift<T,F,N> |
| 193 | where T : Support<F, N> { | |
| 194 | #[inline] | |
| 195 | fn support_hint(&self) -> Cube<F,N> { | |
| 196 | self.base_fn.support_hint().shift(&self.shift) | |
| 197 | } | |
| 198 | ||
| 199 | #[inline] | |
| 200 | fn in_support(&self, x : &Loc<F,N>) -> bool { | |
| 201 | self.base_fn.in_support(&(x - &self.shift)) | |
| 202 | } | |
| 203 | ||
| 204 | // fn fully_in_support(&self, _cube : &Cube<F,N>) -> bool { | |
| 205 | // //self.base_fn.fully_in_support(cube.shift(&vectorneg(self.shift))) | |
| 206 | // todo!("Not implemented, but not used at the moment") | |
| 207 | // } | |
| 208 | ||
| 209 | #[inline] | |
| 210 | fn bisection_hint(&self, cube : &Cube<F,N>) -> [Option<F>; N] { | |
| 211 | let base_hint = self.base_fn.bisection_hint(cube); | |
| 212 | map2(base_hint, &self.shift, |h, s| h.map(|z| z + *s)) | |
| 213 | } | |
| 214 | ||
| 215 | } | |
| 216 | ||
| 217 | impl<'a, T, F : Float, const N : usize> GlobalAnalysis<F, Bounds<F>> for Shift<T,F,N> | |
| 218 | where T : LocalAnalysis<F, Bounds<F>, N> { | |
| 219 | #[inline] | |
| 220 | fn global_analysis(&self) -> Bounds<F> { | |
| 221 | self.base_fn.global_analysis() | |
| 222 | } | |
| 223 | } | |
| 224 | ||
| 225 | impl<'a, T, F : Float, const N : usize> LocalAnalysis<F, Bounds<F>, N> for Shift<T,F,N> | |
| 226 | where T : LocalAnalysis<F, Bounds<F>, N> { | |
| 227 | #[inline] | |
| 228 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
| 229 | self.base_fn.local_analysis(&cube.shift(&(-self.shift))) | |
| 230 | } | |
| 231 | } | |
| 232 | ||
| 233 | macro_rules! impl_shift_norm { | |
| 234 | ($($norm:ident)*) => { $( | |
| 235 | impl<'a, T, F : Float, const N : usize> Norm<F, $norm> for Shift<T,F,N> | |
| 236 | where T : Norm<F, $norm> { | |
| 237 | #[inline] | |
| 238 | fn norm(&self, n : $norm) -> F { | |
| 239 | self.base_fn.norm(n) | |
| 240 | } | |
| 241 | } | |
| 242 | )* } | |
| 243 | } | |
| 244 | ||
| 245 | impl_shift_norm!(L1 L2 Linfinity); | |
| 246 | ||
|
15
e03ce15643da
Fix broken links in doc comments after Mapping -> Apply change.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
247 | /// Weighting of a [`Support`] and [`Apply`] by scalar multiplication; |
| 5 | 248 | /// output of [`Support::weigh`]. |
| 0 | 249 | #[derive(Copy,Clone,Debug,Serialize)] |
| 250 | pub struct Weighted<T, C : Constant> { | |
| 5 | 251 | /// The weight |
| 0 | 252 | pub weight : C, |
|
15
e03ce15643da
Fix broken links in doc comments after Mapping -> Apply change.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
253 | /// The base [`Support`] or [`Apply`] being weighted. |
| 0 | 254 | pub base_fn : T, |
| 255 | } | |
| 256 | ||
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
257 | impl<'a, T, V, F : Float, C, const N : usize> Apply<&'a Loc<F, N>> for Weighted<T, C> |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
258 | where T : for<'b> Apply<&'b Loc<F, N>, Output=V>, |
| 0 | 259 | V : std::ops::Mul<F,Output=V>, |
| 260 | C : Constant<Type=F> { | |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
261 | type Output = V; |
| 0 | 262 | #[inline] |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
263 | fn apply(&self, x : &'a Loc<F, N>) -> Self::Output { |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
264 | self.base_fn.apply(x) * self.weight.value() |
| 0 | 265 | } |
| 266 | } | |
| 267 | ||
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
268 | impl<'a, T, V, F : Float, C, const N : usize> Apply<Loc<F, N>> for Weighted<T, C> |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
269 | where T : Apply<Loc<F, N>, Output=V>, |
| 0 | 270 | V : std::ops::Mul<F,Output=V>, |
| 271 | C : Constant<Type=F> { | |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
272 | type Output = V; |
| 0 | 273 | #[inline] |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
274 | fn apply(&self, x : Loc<F, N>) -> Self::Output { |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
275 | self.base_fn.apply(x) * self.weight.value() |
| 0 | 276 | } |
| 277 | } | |
| 278 | ||
| 77 | 279 | impl<'a, T, V, W, F : Float, C, const N : usize> Differentiable<&'a Loc<F, N>> for Weighted<T, C> |
| 280 | where T : for<'b> Differentiable<&'b Loc<F, N>, Output=V> | |
| 281 | + for<'b> Apply<&'b Loc<F, N>, Output=W>, | |
|
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
282 | V : std::ops::Mul<F, Output=V>, |
| 77 | 283 | W : std::ops::Mul<F, Output=W>, |
|
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
284 | C : Constant<Type=F> { |
|
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
285 | type Output = V; |
| 77 | 286 | |
|
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
287 | #[inline] |
| 77 | 288 | fn differential(&self, x : &'a Loc<F, N>) -> V { |
|
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
289 | self.base_fn.differential(x) * self.weight.value() |
|
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
290 | } |
| 77 | 291 | |
| 292 | #[inline] | |
| 293 | fn linearisation_error(&self, x : &'a Loc<F, N>, y : &'a Loc<F, N>) -> W { | |
| 294 | self.base_fn.linearisation_error(x, y) * self.weight.value() | |
| 295 | } | |
| 296 | ||
| 297 | #[inline] | |
| 298 | fn linearisation_error_gen(&self, x : &'a Loc<F, N>, y : &'a Loc<F, N>, z : &'a Loc<F, N>) -> W { | |
| 299 | self.base_fn.linearisation_error_gen(x, y, z) * self.weight.value() | |
| 300 | } | |
|
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
301 | } |
|
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
302 | |
| 77 | 303 | impl<'a, T, V, W, F : Float, C, const N : usize> Differentiable<Loc<F, N>> |
| 304 | for Weighted<T, C> | |
| 305 | where T : Differentiable<Loc<F, N>, Output=V> + Apply<Loc<F, N>, Output=W>, | |
|
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
306 | V : std::ops::Mul<F, Output=V>, |
| 77 | 307 | W : std::ops::Mul<F, Output=W>, |
|
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
308 | C : Constant<Type=F> { |
|
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
309 | type Output = V; |
| 77 | 310 | |
|
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
311 | #[inline] |
| 77 | 312 | fn differential(&self, x : Loc<F, N>) -> V { |
|
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
313 | self.base_fn.differential(x) * self.weight.value() |
|
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
314 | } |
| 77 | 315 | |
| 316 | #[inline] | |
| 317 | fn linearisation_error(&self, x : Loc<F, N>, y : Loc<F, N>) -> W { | |
| 318 | self.base_fn.linearisation_error(x, y) * self.weight.value() | |
| 319 | } | |
| 320 | ||
| 321 | #[inline] | |
| 322 | fn linearisation_error_gen(&self, x : Loc<F, N>, y : Loc<F, N>, z : Loc<F, N>) -> W { | |
| 323 | self.base_fn.linearisation_error_gen(x, y, z) * self.weight.value() | |
| 324 | } | |
|
30
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
325 | } |
|
9f2214c961cb
Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
326 | |
| 0 | 327 | impl<'a, T, F : Float, C, const N : usize> Support<F,N> for Weighted<T, C> |
| 328 | where T : Support<F, N>, | |
| 329 | C : Constant<Type=F> { | |
| 330 | ||
| 331 | #[inline] | |
| 332 | fn support_hint(&self) -> Cube<F,N> { | |
| 333 | self.base_fn.support_hint() | |
| 334 | } | |
| 335 | ||
| 336 | #[inline] | |
| 337 | fn in_support(&self, x : &Loc<F,N>) -> bool { | |
| 338 | self.base_fn.in_support(x) | |
| 339 | } | |
| 340 | ||
| 341 | // fn fully_in_support(&self, cube : &Cube<F,N>) -> bool { | |
| 342 | // self.base_fn.fully_in_support(cube) | |
| 343 | // } | |
| 344 | ||
| 345 | #[inline] | |
| 346 | fn bisection_hint(&self, cube : &Cube<F,N>) -> [Option<F>; N] { | |
| 347 | self.base_fn.bisection_hint(cube) | |
| 348 | } | |
| 349 | } | |
| 350 | ||
| 351 | impl<'a, T, F : Float, C> GlobalAnalysis<F, Bounds<F>> for Weighted<T, C> | |
| 352 | where T : GlobalAnalysis<F, Bounds<F>>, | |
| 353 | C : Constant<Type=F> { | |
| 354 | #[inline] | |
| 355 | fn global_analysis(&self) -> Bounds<F> { | |
| 356 | let Bounds(lower, upper) = self.base_fn.global_analysis(); | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
357 | debug_assert!(lower <= upper); |
| 0 | 358 | match self.weight.value() { |
| 359 | w if w < F::ZERO => Bounds(w * upper, w * lower), | |
| 360 | w => Bounds(w * lower, w * upper), | |
| 361 | } | |
| 362 | } | |
| 363 | } | |
| 364 | ||
| 365 | impl<'a, T, F : Float, C, const N : usize> LocalAnalysis<F, Bounds<F>, N> for Weighted<T, C> | |
| 366 | where T : LocalAnalysis<F, Bounds<F>, N>, | |
| 367 | C : Constant<Type=F> { | |
| 368 | #[inline] | |
| 369 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
| 370 | 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
|
371 | debug_assert!(lower <= upper); |
| 0 | 372 | match self.weight.value() { |
| 373 | w if w < F::ZERO => Bounds(w * upper, w * lower), | |
| 374 | w => Bounds(w * lower, w * upper), | |
| 375 | } | |
| 376 | } | |
| 377 | } | |
| 378 | ||
| 379 | macro_rules! make_weighted_scalarop_rhs { | |
| 380 | ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => { | |
| 381 | impl<F : Float, T> std::ops::$trait_assign<F> for Weighted<T, F> { | |
| 382 | #[inline] | |
| 383 | fn $fn_assign(&mut self, t : F) { | |
| 384 | self.weight.$fn_assign(t); | |
| 385 | } | |
| 386 | } | |
| 387 | ||
| 388 | impl<'a, F : Float, T> std::ops::$trait<F> for Weighted<T, F> { | |
| 389 | type Output = Self; | |
| 390 | #[inline] | |
| 391 | fn $fn(mut self, t : F) -> Self { | |
| 392 | self.weight.$fn_assign(t); | |
| 393 | self | |
| 394 | } | |
| 395 | } | |
| 396 | ||
| 397 | impl<'a, F : Float, T> std::ops::$trait<F> for &'a Weighted<T, F> | |
| 398 | where T : Clone { | |
| 399 | type Output = Weighted<T, F>; | |
| 400 | #[inline] | |
| 401 | fn $fn(self, t : F) -> Self::Output { | |
| 402 | Weighted { weight : self.weight.$fn(t), base_fn : self.base_fn.clone() } | |
| 403 | } | |
| 404 | } | |
| 405 | } | |
| 406 | } | |
| 407 | ||
| 408 | make_weighted_scalarop_rhs!(Mul, mul, MulAssign, mul_assign); | |
| 409 | make_weighted_scalarop_rhs!(Div, div, DivAssign, div_assign); | |
| 410 | ||
| 411 | macro_rules! impl_weighted_norm { | |
| 412 | ($($norm:ident)*) => { $( | |
| 413 | impl<'a, T, F : Float> Norm<F, $norm> for Weighted<T,F> | |
| 414 | where T : Norm<F, $norm> { | |
| 415 | #[inline] | |
| 416 | fn norm(&self, n : $norm) -> F { | |
| 417 | self.base_fn.norm(n) * self.weight.abs() | |
| 418 | } | |
| 419 | } | |
| 420 | )* } | |
| 421 | } | |
| 422 | ||
| 423 | impl_weighted_norm!(L1 L2 Linfinity); | |
| 424 | ||
| 425 | ||
|
15
e03ce15643da
Fix broken links in doc comments after Mapping -> Apply change.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
426 | /// Normalisation of [`Support`] and [`Apply`] to L¹ norm 1. |
| 5 | 427 | /// |
| 0 | 428 | /// Currently only scalar-valued functions are supported. |
| 429 | #[derive(Copy, Clone, Debug, Serialize, PartialEq)] | |
| 5 | 430 | 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
|
431 | /// The base [`Support`] or [`Apply`]. |
| 5 | 432 | pub T |
| 433 | ); | |
| 0 | 434 | |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
435 | impl<'a, T, F : Float, const N : usize> Apply<&'a Loc<F, N>> for Normalised<T> |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
436 | where T : Norm<F, L1> + for<'b> Apply<&'b Loc<F, N>, Output=F> { |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
437 | type Output = F; |
| 0 | 438 | #[inline] |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
439 | fn apply(&self, x : &'a Loc<F, N>) -> Self::Output { |
| 0 | 440 | 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
|
441 | if w == F::ZERO { F::ZERO } else { self.0.apply(x) / w } |
| 0 | 442 | } |
| 443 | } | |
| 444 | ||
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
445 | impl<'a, T, F : Float, const N : usize> Apply<Loc<F, N>> for Normalised<T> |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
446 | where T : Norm<F, L1> + Apply<Loc<F,N>, Output=F> { |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
447 | type Output = F; |
| 0 | 448 | #[inline] |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
449 | fn apply(&self, x : Loc<F, N>) -> Self::Output { |
| 0 | 450 | 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
|
451 | if w == F::ZERO { F::ZERO } else { self.0.apply(x) / w } |
| 0 | 452 | } |
| 453 | } | |
| 454 | ||
| 455 | impl<'a, T, F : Float, const N : usize> Support<F,N> for Normalised<T> | |
| 456 | where T : Norm<F, L1> + Support<F, N> { | |
| 457 | ||
| 458 | #[inline] | |
| 459 | fn support_hint(&self) -> Cube<F,N> { | |
| 460 | self.0.support_hint() | |
| 461 | } | |
| 462 | ||
| 463 | #[inline] | |
| 464 | fn in_support(&self, x : &Loc<F,N>) -> bool { | |
| 465 | self.0.in_support(x) | |
| 466 | } | |
| 467 | ||
| 468 | // fn fully_in_support(&self, cube : &Cube<F,N>) -> bool { | |
| 469 | // self.0.fully_in_support(cube) | |
| 470 | // } | |
| 471 | ||
| 472 | #[inline] | |
| 473 | fn bisection_hint(&self, cube : &Cube<F,N>) -> [Option<F>; N] { | |
| 474 | self.0.bisection_hint(cube) | |
| 475 | } | |
| 476 | } | |
| 477 | ||
| 478 | impl<'a, T, F : Float> GlobalAnalysis<F, Bounds<F>> for Normalised<T> | |
| 479 | where T : Norm<F, L1> + GlobalAnalysis<F, Bounds<F>> { | |
| 480 | #[inline] | |
| 481 | fn global_analysis(&self) -> Bounds<F> { | |
| 482 | let Bounds(lower, upper) = self.0.global_analysis(); | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
483 | debug_assert!(lower <= upper); |
| 0 | 484 | let w = self.0.norm(L1); |
| 485 | debug_assert!(w >= F::ZERO); | |
| 486 | Bounds(w * lower, w * upper) | |
| 487 | } | |
| 488 | } | |
| 489 | ||
| 490 | impl<'a, T, F : Float, const N : usize> LocalAnalysis<F, Bounds<F>, N> for Normalised<T> | |
| 491 | where T : Norm<F, L1> + LocalAnalysis<F, Bounds<F>, N> { | |
| 492 | #[inline] | |
| 493 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
| 494 | let Bounds(lower, upper) = self.0.local_analysis(cube); | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
495 | debug_assert!(lower <= upper); |
| 0 | 496 | let w = self.0.norm(L1); |
| 497 | debug_assert!(w >= F::ZERO); | |
| 498 | Bounds(w * lower, w * upper) | |
| 499 | } | |
| 500 | } | |
| 501 | ||
| 502 | impl<'a, T, F : Float> Norm<F, L1> for Normalised<T> | |
| 503 | where T : Norm<F, L1> { | |
| 504 | #[inline] | |
| 505 | fn norm(&self, _ : L1) -> F { | |
| 506 | let w = self.0.norm(L1); | |
| 507 | if w == F::ZERO { F::ZERO } else { F::ONE } | |
| 508 | } | |
| 509 | } | |
| 510 | ||
| 511 | macro_rules! impl_normalised_norm { | |
| 512 | ($($norm:ident)*) => { $( | |
| 513 | impl<'a, T, F : Float> Norm<F, $norm> for Normalised<T> | |
| 514 | where T : Norm<F, $norm> + Norm<F, L1> { | |
| 515 | #[inline] | |
| 516 | fn norm(&self, n : $norm) -> F { | |
| 517 | let w = self.0.norm(L1); | |
| 518 | if w == F::ZERO { F::ZERO } else { self.0.norm(n) / w } | |
| 519 | } | |
| 520 | } | |
| 521 | )* } | |
| 522 | } | |
| 523 | ||
| 524 | impl_normalised_norm!(L2 Linfinity); | |
| 525 | ||
| 526 | /* | |
| 527 | impl<F : Num, S : Support<F, N>, const N : usize> LocalAnalysis<F, NullAggregator, N> for S { | |
| 528 | fn local_analysis(&self, _cube : &Cube<F, N>) -> NullAggregator { NullAggregator } | |
| 529 | } | |
| 530 | ||
| 531 | impl<F : Float, S : Bounded<F>, const N : usize> LocalAnalysis<F, Bounds<F>, N> for S { | |
| 532 | #[inline] | |
| 533 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
| 534 | self.bounds(cube) | |
| 535 | } | |
| 536 | }*/ | |
| 537 | ||
| 5 | 538 | /// Generator of [`Support`]-implementing component functions based on low storage requirement |
| 539 | /// [ids][`Self::Id`]. | |
| 0 | 540 | pub trait SupportGenerator<F : Float, const N : usize> |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
541 | : MulAssign<F> + DivAssign<F> + Neg<Output=Self> + Clone + Sync + Send + 'static { |
| 5 | 542 | /// The identification type |
| 0 | 543 | 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
|
544 | /// The type of the [`Support`] (often also a [`Apply`]). |
| 0 | 545 | type SupportType : 'static + Support<F, N>; |
| 5 | 546 | /// An iterator over all the [`Support`]s of the generator. |
| 0 | 547 | type AllDataIter<'a> : Iterator<Item=(Self::Id, Self::SupportType)> where Self : 'a; |
| 548 | ||
| 5 | 549 | /// Returns the component identified by `id`. |
| 550 | /// | |
| 551 | /// Panics if `id` is an invalid identifier. | |
| 552 | fn support_for(&self, id : Self::Id) -> Self::SupportType; | |
| 0 | 553 | |
| 5 | 554 | /// Returns the number of different components in this generator. |
| 0 | 555 | fn support_count(&self) -> usize; |
| 556 | ||
| 5 | 557 | /// Returns an iterator over all pairs of `(id, support)`. |
| 0 | 558 | fn all_data(&self) -> Self::AllDataIter<'_>; |
| 559 | } | |
| 560 |