src/bisection_tree/support.rs

Tue, 18 Jul 2023 15:44:10 +0300

author
Tuomo Valkonen <tuomov@iki.fi>
date
Tue, 18 Jul 2023 15:44:10 +0300
branch
dev
changeset 77
cf8ef9463664
parent 30
9f2214c961cb
permissions
-rw-r--r--

linearisation_error

0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
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
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
4 */
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 use serde::Serialize;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6 use std::ops::{MulAssign,DivAssign,Neg};
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
7 use crate::types::{Float, Num};
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
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
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
10 use crate::sets::Cube;
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
11 use crate::loc::Loc;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 use super::aggregator::Bounds;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 use crate::norms::{Norm, L1, L2, Linfinity};
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
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
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
17 /// The type of the value
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 type Type : Float;
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
19 /// Returns the value of the constant
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 fn value(&self) -> Self::Type;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 impl<F : Float> Constant for F {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 type Type = F;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 fn value(&self) -> F { *self }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
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
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
34 /// Return a cube containing the support of the function represented by `self`.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
35 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
36 /// The hint may be larger than the actual support, but must contain it.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 fn support_hint(&self) -> Cube<F,N>;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
39 /// Indicate whether `x` is in the support of the function represented by `self`.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 fn in_support(&self, x : &Loc<F,N>) -> bool;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
42 // Indicate whether `cube` is fully in the support of the function represented by `self`.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 //fn fully_in_support(&self, cube : &Cube<F,N>) -> bool;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
45 /// Return an optional hint for bisecting the support.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
46 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
47 /// The output along each axis a possible coordinate at which to bisect `cube`.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
48 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
49 /// This is useful for nonsmooth functions to make finite element models as used by
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
50 /// [`BTFN`][super::btfn::BTFN] minimisation/maximisation compatible with points of
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
51 /// non-differentiability.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
52 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
53 /// The default implementation returns `[None; N]`.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 #[inline]
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
55 #[allow(unused_variables)]
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
56 fn bisection_hint(&self, cube : &Cube<F, N>) -> [Option<F>; N] {
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
57 [None; N]
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
60 /// Translate `self` by `x`.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
61 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
62 fn shift(self, x : Loc<F, N>) -> Shift<Self, F, N> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
63 Shift { shift : x, base_fn : self }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
64 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
65
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
66 /// Multiply `self` by the scalar `a`.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
67 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
68 fn weigh<C : Constant<Type=F>>(self, a : C) -> Weighted<Self, C> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
69 Weighted { weight : a, base_fn : self }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
70 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
71 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
74 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
75 /// Typically `A` is an [`Aggregator`][super::aggregator::Aggregator] such as
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
76 /// [`Bounds`][super::aggregator::Bounds].
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
77 pub trait GlobalAnalysis<F : Num, A> {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
78 /// Perform global analysis of the property `A` of `Self`.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
79 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
80 /// As an example, in the case of `A` being [`Bounds`][super::aggregator::Bounds],
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
81 /// this function will return global upper and lower bounds for the mapping
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
82 /// represented by `self`.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
83 fn global_analysis(&self) -> A;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
84 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
85
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
86 // default impl<F, A, N, L> GlobalAnalysis<F, A, N> for L
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
87 // where L : LocalAnalysis<F, A, N> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
88 // #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
89 // fn global_analysis(&self) -> Bounds<F> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
90 // self.local_analysis(&self.support_hint())
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
91 // }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
92 // }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
95 /// within a [`Cube`].
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
96 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
97 /// Typically `A` is an [`Aggregator`][super::aggregator::Aggregator] such as
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
98 /// [`Bounds`][super::aggregator::Bounds].
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
99 pub trait LocalAnalysis<F : Num, A, const N : usize> : GlobalAnalysis<F, A> + Support<F, N> {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
100 /// Perform local analysis of the property `A` of `Self`.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
101 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
102 /// As an example, in the case of `A` being [`Bounds`][super::aggregator::Bounds],
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
103 /// this function will return upper and lower bounds within `cube` for the mapping
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
104 /// represented by `self`.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
105 fn local_analysis(&self, cube : &Cube<F, N>) -> A;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
106 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
109 ///
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
112 /// reference or non-reference arguments.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
113 pub trait Bounded<F : Float> : GlobalAnalysis<F, Bounds<F>> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
114 /// Return lower and upper bounds for the values of of `self`.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
115 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
116 fn bounds(&self) -> Bounds<F> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
117 self.global_analysis()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
118 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
119 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
120
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
121 impl<F : Float, T : GlobalAnalysis<F, Bounds<F>>> Bounded<F> for T { }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
124 #[derive(Copy,Clone,Debug,Serialize)] // Serialize! but not implemented by Loc.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
125 pub struct Shift<T, F, const N : usize> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
126 shift : Loc<F, N>,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
127 base_fn : T,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
128 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
136 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
137 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
145 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
146 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
147
77
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
148 impl<'a, T, V, W, F : Float, const N : usize> Differentiable<&'a Loc<F, N>>
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
149 for Shift<T,F,N>
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
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
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
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
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
156
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
157 #[inline]
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
158 fn linearisation_error(&self, x : &'a Loc<F, N>, y : &'a Loc<F, N>) -> W {
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
159 self.base_fn
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
160 .linearisation_error(x - &self.shift, y - &self.shift)
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
161 }
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
162
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
163 #[inline]
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
164 fn linearisation_error_gen(&self, x : &'a Loc<F, N>, y : &'a Loc<F, N>, z : &'a Loc<F, N>) -> W {
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
165 self.base_fn
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
166 .linearisation_error_gen(x - &self.shift, y - &self.shift, z - &self.shift)
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
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
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
170 impl<'a, T, V, W, F : Float, const N : usize> Differentiable<Loc<F, N>>
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
171 for Shift<T,F,N>
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
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
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
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
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
178
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
179 #[inline]
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
180 fn linearisation_error(&self, x : Loc<F, N>, y : Loc<F, N>) -> W {
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
181 self.base_fn
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
182 .linearisation_error(x - &self.shift, y - &self.shift)
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
183 }
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
184
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
185 #[inline]
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
186 fn linearisation_error_gen(&self, x : Loc<F, N>, y : Loc<F, N>, z : Loc<F, N>) -> W {
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
187 self.base_fn
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
188 .linearisation_error_gen(x - &self.shift, y - &self.shift, z - &self.shift)
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
192 impl<'a, T, F : Float, const N : usize> Support<F,N> for Shift<T,F,N>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
193 where T : Support<F, N> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
194 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
195 fn support_hint(&self) -> Cube<F,N> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
196 self.base_fn.support_hint().shift(&self.shift)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
197 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
198
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
199 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
200 fn in_support(&self, x : &Loc<F,N>) -> bool {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
201 self.base_fn.in_support(&(x - &self.shift))
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
202 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
203
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
204 // fn fully_in_support(&self, _cube : &Cube<F,N>) -> bool {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
205 // //self.base_fn.fully_in_support(cube.shift(&vectorneg(self.shift)))
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
206 // todo!("Not implemented, but not used at the moment")
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
207 // }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
208
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
209 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
210 fn bisection_hint(&self, cube : &Cube<F,N>) -> [Option<F>; N] {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
211 let base_hint = self.base_fn.bisection_hint(cube);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
212 map2(base_hint, &self.shift, |h, s| h.map(|z| z + *s))
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
213 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
214
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
215 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
216
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
217 impl<'a, T, F : Float, const N : usize> GlobalAnalysis<F, Bounds<F>> for Shift<T,F,N>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
218 where T : LocalAnalysis<F, Bounds<F>, N> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
219 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
220 fn global_analysis(&self) -> Bounds<F> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
221 self.base_fn.global_analysis()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
222 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
223 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
224
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
225 impl<'a, T, F : Float, const N : usize> LocalAnalysis<F, Bounds<F>, N> for Shift<T,F,N>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
226 where T : LocalAnalysis<F, Bounds<F>, N> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
227 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
228 fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
229 self.base_fn.local_analysis(&cube.shift(&(-self.shift)))
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
230 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
231 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
232
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
233 macro_rules! impl_shift_norm {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
234 ($($norm:ident)*) => { $(
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
235 impl<'a, T, F : Float, const N : usize> Norm<F, $norm> for Shift<T,F,N>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
236 where T : Norm<F, $norm> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
237 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
238 fn norm(&self, n : $norm) -> F {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
239 self.base_fn.norm(n)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
240 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
241 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
242 )* }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
243 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
244
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
245 impl_shift_norm!(L1 L2 Linfinity);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
248 /// output of [`Support::weigh`].
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
249 #[derive(Copy,Clone,Debug,Serialize)]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
250 pub struct Weighted<T, C : Constant> {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
251 /// The weight
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
254 pub base_fn : T,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
255 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
259 V : std::ops::Mul<F,Output=V>,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
265 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
266 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
270 V : std::ops::Mul<F,Output=V>,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
276 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
277 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
278
77
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
279 impl<'a, T, V, W, F : Float, C, const N : usize> Differentiable<&'a Loc<F, N>> for Weighted<T, C>
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
280 where T : for<'b> Differentiable<&'b Loc<F, N>, Output=V>
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
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
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
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
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
286
30
9f2214c961cb Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents: 15
diff changeset
287 #[inline]
77
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
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
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
291
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
292 #[inline]
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
293 fn linearisation_error(&self, x : &'a Loc<F, N>, y : &'a Loc<F, N>) -> W {
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
294 self.base_fn.linearisation_error(x, y) * self.weight.value()
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
295 }
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
296
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
297 #[inline]
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
298 fn linearisation_error_gen(&self, x : &'a Loc<F, N>, y : &'a Loc<F, N>, z : &'a Loc<F, N>) -> W {
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
299 self.base_fn.linearisation_error_gen(x, y, z) * self.weight.value()
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
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
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
303 impl<'a, T, V, W, F : Float, C, const N : usize> Differentiable<Loc<F, N>>
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
304 for Weighted<T, C>
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
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
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
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
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
310
30
9f2214c961cb Implement Differentiable for Weighted and Shift
Tuomo Valkonen <tuomov@iki.fi>
parents: 15
diff changeset
311 #[inline]
77
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
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
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
315
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
316 #[inline]
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
317 fn linearisation_error(&self, x : Loc<F, N>, y : Loc<F, N>) -> W {
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
318 self.base_fn.linearisation_error(x, y) * self.weight.value()
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
319 }
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
320
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
321 #[inline]
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
322 fn linearisation_error_gen(&self, x : Loc<F, N>, y : Loc<F, N>, z : Loc<F, N>) -> W {
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
323 self.base_fn.linearisation_error_gen(x, y, z) * self.weight.value()
cf8ef9463664 linearisation_error
Tuomo Valkonen <tuomov@iki.fi>
parents: 30
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
327 impl<'a, T, F : Float, C, const N : usize> Support<F,N> for Weighted<T, C>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
328 where T : Support<F, N>,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
329 C : Constant<Type=F> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
330
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
331 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
332 fn support_hint(&self) -> Cube<F,N> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
333 self.base_fn.support_hint()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
334 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
335
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
336 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
337 fn in_support(&self, x : &Loc<F,N>) -> bool {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
338 self.base_fn.in_support(x)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
339 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
340
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
341 // fn fully_in_support(&self, cube : &Cube<F,N>) -> bool {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
342 // self.base_fn.fully_in_support(cube)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
343 // }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
344
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
345 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
346 fn bisection_hint(&self, cube : &Cube<F,N>) -> [Option<F>; N] {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
347 self.base_fn.bisection_hint(cube)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
348 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
349 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
350
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
351 impl<'a, T, F : Float, C> GlobalAnalysis<F, Bounds<F>> for Weighted<T, C>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
352 where T : GlobalAnalysis<F, Bounds<F>>,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
353 C : Constant<Type=F> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
354 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
355 fn global_analysis(&self) -> Bounds<F> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
358 match self.weight.value() {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
359 w if w < F::ZERO => Bounds(w * upper, w * lower),
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
360 w => Bounds(w * lower, w * upper),
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
361 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
362 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
363 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
364
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
365 impl<'a, T, F : Float, C, const N : usize> LocalAnalysis<F, Bounds<F>, N> for Weighted<T, C>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
366 where T : LocalAnalysis<F, Bounds<F>, N>,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
367 C : Constant<Type=F> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
368 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
369 fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
372 match self.weight.value() {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
373 w if w < F::ZERO => Bounds(w * upper, w * lower),
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
374 w => Bounds(w * lower, w * upper),
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
375 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
376 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
377 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
378
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
379 macro_rules! make_weighted_scalarop_rhs {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
380 ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
381 impl<F : Float, T> std::ops::$trait_assign<F> for Weighted<T, F> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
382 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
383 fn $fn_assign(&mut self, t : F) {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
384 self.weight.$fn_assign(t);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
385 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
386 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
387
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
388 impl<'a, F : Float, T> std::ops::$trait<F> for Weighted<T, F> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
389 type Output = Self;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
390 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
391 fn $fn(mut self, t : F) -> Self {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
392 self.weight.$fn_assign(t);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
393 self
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
394 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
395 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
396
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
397 impl<'a, F : Float, T> std::ops::$trait<F> for &'a Weighted<T, F>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
398 where T : Clone {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
399 type Output = Weighted<T, F>;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
400 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
401 fn $fn(self, t : F) -> Self::Output {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
402 Weighted { weight : self.weight.$fn(t), base_fn : self.base_fn.clone() }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
403 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
404 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
405 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
406 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
407
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
408 make_weighted_scalarop_rhs!(Mul, mul, MulAssign, mul_assign);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
409 make_weighted_scalarop_rhs!(Div, div, DivAssign, div_assign);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
410
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
411 macro_rules! impl_weighted_norm {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
412 ($($norm:ident)*) => { $(
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
413 impl<'a, T, F : Float> Norm<F, $norm> for Weighted<T,F>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
414 where T : Norm<F, $norm> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
415 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
416 fn norm(&self, n : $norm) -> F {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
417 self.base_fn.norm(n) * self.weight.abs()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
418 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
419 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
420 )* }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
421 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
422
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
423 impl_weighted_norm!(L1 L2 Linfinity);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
424
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
427 ///
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
428 /// Currently only scalar-valued functions are supported.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
429 #[derive(Copy, Clone, Debug, Serialize, PartialEq)]
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
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
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
432 pub T
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
433 );
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
442 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
443 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
452 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
453 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
454
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
455 impl<'a, T, F : Float, const N : usize> Support<F,N> for Normalised<T>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
456 where T : Norm<F, L1> + Support<F, N> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
457
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
458 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
459 fn support_hint(&self) -> Cube<F,N> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
460 self.0.support_hint()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
461 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
462
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
463 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
464 fn in_support(&self, x : &Loc<F,N>) -> bool {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
465 self.0.in_support(x)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
466 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
467
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
468 // fn fully_in_support(&self, cube : &Cube<F,N>) -> bool {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
469 // self.0.fully_in_support(cube)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
470 // }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
471
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
472 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
473 fn bisection_hint(&self, cube : &Cube<F,N>) -> [Option<F>; N] {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
474 self.0.bisection_hint(cube)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
475 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
476 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
477
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
478 impl<'a, T, F : Float> GlobalAnalysis<F, Bounds<F>> for Normalised<T>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
479 where T : Norm<F, L1> + GlobalAnalysis<F, Bounds<F>> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
480 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
481 fn global_analysis(&self) -> Bounds<F> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
484 let w = self.0.norm(L1);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
485 debug_assert!(w >= F::ZERO);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
486 Bounds(w * lower, w * upper)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
487 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
488 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
489
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
490 impl<'a, T, F : Float, const N : usize> LocalAnalysis<F, Bounds<F>, N> for Normalised<T>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
491 where T : Norm<F, L1> + LocalAnalysis<F, Bounds<F>, N> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
492 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
493 fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
496 let w = self.0.norm(L1);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
497 debug_assert!(w >= F::ZERO);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
498 Bounds(w * lower, w * upper)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
499 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
500 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
501
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
502 impl<'a, T, F : Float> Norm<F, L1> for Normalised<T>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
503 where T : Norm<F, L1> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
504 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
505 fn norm(&self, _ : L1) -> F {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
506 let w = self.0.norm(L1);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
507 if w == F::ZERO { F::ZERO } else { F::ONE }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
508 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
509 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
510
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
511 macro_rules! impl_normalised_norm {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
512 ($($norm:ident)*) => { $(
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
513 impl<'a, T, F : Float> Norm<F, $norm> for Normalised<T>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
514 where T : Norm<F, $norm> + Norm<F, L1> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
515 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
516 fn norm(&self, n : $norm) -> F {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
517 let w = self.0.norm(L1);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
518 if w == F::ZERO { F::ZERO } else { self.0.norm(n) / w }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
519 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
520 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
521 )* }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
522 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
523
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
524 impl_normalised_norm!(L2 Linfinity);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
525
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
526 /*
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
527 impl<F : Num, S : Support<F, N>, const N : usize> LocalAnalysis<F, NullAggregator, N> for S {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
528 fn local_analysis(&self, _cube : &Cube<F, N>) -> NullAggregator { NullAggregator }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
529 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
530
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
531 impl<F : Float, S : Bounded<F>, const N : usize> LocalAnalysis<F, Bounds<F>, N> for S {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
532 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
533 fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
534 self.bounds(cube)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
535 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
536 }*/
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
537
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
538 /// Generator of [`Support`]-implementing component functions based on low storage requirement
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
539 /// [ids][`Self::Id`].
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
542 /// The identification type
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
545 type SupportType : 'static + Support<F, N>;
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
546 /// An iterator over all the [`Support`]s of the generator.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
547 type AllDataIter<'a> : Iterator<Item=(Self::Id, Self::SupportType)> where Self : 'a;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
548
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
549 /// Returns the component identified by `id`.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
550 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
551 /// Panics if `id` is an invalid identifier.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
552 fn support_for(&self, id : Self::Id) -> Self::SupportType;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
553
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
554 /// Returns the number of different components in this generator.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
555 fn support_count(&self) -> usize;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
556
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
557 /// Returns an iterator over all pairs of `(id, support)`.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
558 fn all_data(&self) -> Self::AllDataIter<'_>;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
559 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
560

mercurial