Thu, 01 May 2025 13:06:58 -0500
Transpose loc parameters to allow f64 defaults
| 5 | 1 | use std::iter::Chain; |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
2 | use std::sync::Arc; |
| 0 | 3 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
4 | use crate::iter::{MapF, MapZ, Mappable}; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
5 | use crate::loc::Loc; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
6 | use crate::mapping::{DifferentiableImpl, DifferentiableMapping, Instance, Mapping, Space}; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
7 | use crate::sets::Cube; |
| 0 | 8 | use crate::types::*; |
| 5 | 9 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
10 | use super::aggregator::*; |
| 0 | 11 | use super::support::*; |
| 12 | ||
| 5 | 13 | /// A structure for storing two [`SupportGenerator`]s summed/chain together. |
| 14 | /// | |
| 15 | /// This is needed to work with sums of different types of [`Support`]s. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
16 | #[derive(Debug, Clone)] |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
17 | pub struct BothGenerators<A, B>(pub(super) Arc<A>, pub(super) Arc<B>); |
| 0 | 18 | |
| 5 | 19 | /// A structure for a [`Support`] that can be either `A` or `B`. |
| 20 | /// | |
| 21 | /// This is needed to work with sums of different types of [`Support`]s. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
22 | #[derive(Debug, Clone)] |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
23 | pub enum EitherSupport<B, A> { |
| 0 | 24 | Left(A), |
| 25 | Right(B), | |
| 26 | } | |
| 27 | ||
| 28 | // We need type alias bounds to access associate types. | |
| 29 | #[allow(type_alias_bounds)] | |
| 30 | type BothAllDataIter< | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
31 | 'a, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
32 | F, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
33 | G1: SupportGenerator<N, F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
34 | G2: SupportGenerator<N, F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
35 | const N: usize, |
| 0 | 36 | > = Chain< |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
37 | MapF<G1::AllDataIter<'a>, (usize, EitherSupport<G2::SupportType, G1::SupportType>)>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
38 | MapZ<G2::AllDataIter<'a>, usize, (usize, EitherSupport<G2::SupportType, G1::SupportType>)>, |
| 0 | 39 | >; |
| 40 | ||
| 41 | impl<G1, G2> BothGenerators<G1, G2> { | |
| 5 | 42 | /// Helper for [`all_left_data`]. |
| 0 | 43 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
44 | fn map_left<F: Float, const N: usize>( |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
45 | (d, support): (G1::Id, G1::SupportType), |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
46 | ) -> (usize, EitherSupport<G2::SupportType, G1::SupportType>) |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
47 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
48 | G1: SupportGenerator<N, F, Id = usize>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
49 | G2: SupportGenerator<N, F, Id = usize>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
50 | { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
51 | let id: usize = d.into(); |
| 0 | 52 | (id.into(), EitherSupport::Left(support)) |
| 53 | } | |
| 54 | ||
| 5 | 55 | /// Helper for [`all_right_data`]. |
| 0 | 56 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
57 | fn map_right<F: Float, const N: usize>( |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
58 | n0: &usize, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
59 | (d, support): (G2::Id, G2::SupportType), |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
60 | ) -> (usize, EitherSupport<G2::SupportType, G1::SupportType>) |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
61 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
62 | G1: SupportGenerator<N, F, Id = usize>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
63 | G2: SupportGenerator<N, F, Id = usize>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
64 | { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
65 | let id: usize = d.into(); |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
66 | ((n0 + id).into(), EitherSupport::Right(support)) |
| 0 | 67 | } |
| 68 | ||
| 5 | 69 | /// Calls [`SupportGenerator::all_data`] on the “left” support generator. |
| 70 | /// | |
| 71 | /// Converts both the id and the [`Support`] into a form that corresponds to `BothGenerators`. | |
| 0 | 72 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
73 | pub(super) fn all_left_data<F: Float, const N: usize>( |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
74 | &self, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
75 | ) -> MapF<G1::AllDataIter<'_>, (usize, EitherSupport<G2::SupportType, G1::SupportType>)> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
76 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
77 | G1: SupportGenerator<N, F, Id = usize>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
78 | G2: SupportGenerator<N, F, Id = usize>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
79 | { |
| 0 | 80 | self.0.all_data().mapF(Self::map_left) |
| 81 | } | |
| 82 | ||
| 5 | 83 | /// Calls [`SupportGenerator::all_data`] on the “right” support generator. |
| 84 | /// | |
| 85 | /// Converts both the id and the [`Support`] into a form that corresponds to `BothGenerators`. | |
| 0 | 86 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
87 | pub(super) fn all_right_data<F: Float, const N: usize>( |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
88 | &self, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
89 | ) -> MapZ<G2::AllDataIter<'_>, usize, (usize, EitherSupport<G2::SupportType, G1::SupportType>)> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
90 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
91 | G1: SupportGenerator<N, F, Id = usize>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
92 | G2: SupportGenerator<N, F, Id = usize>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
93 | { |
| 0 | 94 | let n0 = self.0.support_count(); |
| 95 | self.1.all_data().mapZ(n0, Self::map_right) | |
| 96 | } | |
| 97 | } | |
| 98 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
99 | impl<F: Float, G1, G2, const N: usize> SupportGenerator<N, F> for BothGenerators<G1, G2> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
100 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
101 | G1: SupportGenerator<N, F, Id = usize>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
102 | G2: SupportGenerator<N, F, Id = usize>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
103 | { |
| 0 | 104 | type Id = usize; |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
105 | type SupportType = EitherSupport<G2::SupportType, G1::SupportType>; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
106 | type AllDataIter<'a> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
107 | = BothAllDataIter<'a, F, G1, G2, N> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
108 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
109 | G1: 'a, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
110 | G2: 'a; |
| 0 | 111 | |
| 112 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
113 | fn support_for(&self, id: Self::Id) -> Self::SupportType { |
| 0 | 114 | let n0 = self.0.support_count(); |
| 115 | if id < n0 { | |
| 116 | EitherSupport::Left(self.0.support_for(id.into())) | |
| 117 | } else { | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
118 | EitherSupport::Right(self.1.support_for((id - n0).into())) |
| 0 | 119 | } |
| 120 | } | |
| 121 | ||
| 122 | #[inline] | |
| 123 | fn support_count(&self) -> usize { | |
| 124 | self.0.support_count() + self.1.support_count() | |
| 125 | } | |
| 126 | ||
| 127 | #[inline] | |
| 128 | fn all_data(&self) -> Self::AllDataIter<'_> { | |
| 129 | self.all_left_data().chain(self.all_right_data()) | |
| 130 | } | |
| 131 | } | |
| 132 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
133 | impl<F: Float, S1, S2, const N: usize> Support<N, F> for EitherSupport<S2, S1> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
134 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
135 | S1: Support<N, F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
136 | S2: Support<N, F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
137 | { |
| 0 | 138 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
139 | fn support_hint(&self) -> Cube<N, F> { |
| 0 | 140 | match self { |
| 141 | EitherSupport::Left(ref a) => a.support_hint(), | |
| 142 | EitherSupport::Right(ref b) => b.support_hint(), | |
| 143 | } | |
| 144 | } | |
| 145 | ||
| 146 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
147 | fn in_support(&self, x: &Loc<N, F>) -> bool { |
| 0 | 148 | match self { |
| 149 | EitherSupport::Left(ref a) => a.in_support(x), | |
| 150 | EitherSupport::Right(ref b) => b.in_support(x), | |
| 151 | } | |
| 152 | } | |
| 153 | ||
| 154 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
155 | fn bisection_hint(&self, cube: &Cube<N, F>) -> [Option<F>; N] { |
| 0 | 156 | match self { |
| 157 | EitherSupport::Left(ref a) => a.bisection_hint(cube), | |
| 158 | EitherSupport::Right(ref b) => b.bisection_hint(cube), | |
| 159 | } | |
| 160 | } | |
| 161 | } | |
| 162 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
163 | impl<F: Float, A, S1, S2, const N: usize> LocalAnalysis<F, A, N> for EitherSupport<S2, S1> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
164 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
165 | A: Aggregator, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
166 | S1: LocalAnalysis<F, A, N>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
167 | S2: LocalAnalysis<F, A, N>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
168 | { |
| 0 | 169 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
170 | fn local_analysis(&self, cube: &Cube<N, F>) -> A { |
| 0 | 171 | match self { |
| 172 | EitherSupport::Left(ref a) => a.local_analysis(cube), | |
| 173 | EitherSupport::Right(ref b) => b.local_analysis(cube), | |
| 174 | } | |
| 175 | } | |
| 176 | } | |
| 177 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
178 | impl<F: Float, A, S1, S2> GlobalAnalysis<F, A> for EitherSupport<S2, S1> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
179 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
180 | A: Aggregator, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
181 | S1: GlobalAnalysis<F, A>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
182 | S2: GlobalAnalysis<F, A>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
183 | { |
| 0 | 184 | #[inline] |
| 185 | fn global_analysis(&self) -> A { | |
| 186 | match self { | |
| 187 | EitherSupport::Left(ref a) => a.global_analysis(), | |
| 188 | EitherSupport::Right(ref b) => b.global_analysis(), | |
| 189 | } | |
| 190 | } | |
| 191 | } | |
| 192 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
193 | impl<F, S1, S2, X> Mapping<X> for EitherSupport<S2, S1> |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
194 | where |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
195 | F: Space, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
196 | X: Space, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
197 | S1: Mapping<X, Codomain = F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
198 | S2: Mapping<X, Codomain = F>, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
199 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
200 | type Codomain = F; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
201 | |
| 0 | 202 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
203 | fn apply<I: Instance<X>>(&self, x: I) -> F { |
| 0 | 204 | match self { |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
205 | EitherSupport::Left(ref a) => a.apply(x), |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
206 | EitherSupport::Right(ref b) => b.apply(x), |
| 0 | 207 | } |
| 208 | } | |
| 209 | } | |
| 210 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
211 | impl<X, S1, S2, O> DifferentiableImpl<X> for EitherSupport<S2, S1> |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
212 | where |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
213 | O: Space, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
214 | X: Space, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
215 | S1: DifferentiableMapping<X, DerivativeDomain = O>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
216 | S2: DifferentiableMapping<X, DerivativeDomain = O>, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
217 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
218 | type Derivative = O; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
219 | |
|
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
220 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
221 | fn differential_impl<I: Instance<X>>(&self, x: I) -> O { |
|
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
222 | match self { |
|
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
223 | EitherSupport::Left(ref a) => a.differential(x), |
|
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
224 | EitherSupport::Right(ref b) => b.differential(x), |
|
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
225 | } |
|
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
226 | } |
|
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
227 | } |
|
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
228 | |
| 0 | 229 | macro_rules! make_either_scalarop_rhs { |
| 230 | ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => { | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
231 | impl<F: Float, G1, G2> std::ops::$trait_assign<F> for BothGenerators<G1, G2> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
232 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
233 | G1: std::ops::$trait_assign<F> + Clone, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
234 | G2: std::ops::$trait_assign<F> + Clone, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
235 | { |
| 0 | 236 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
237 | fn $fn_assign(&mut self, t: F) { |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
238 | Arc::make_mut(&mut self.0).$fn_assign(t); |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
239 | Arc::make_mut(&mut self.1).$fn_assign(t); |
| 0 | 240 | } |
| 241 | } | |
| 242 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
243 | impl<'a, F: Float, G1, G2> std::ops::$trait<F> for &'a BothGenerators<G1, G2> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
244 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
245 | &'a G1: std::ops::$trait<F, Output = G1>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
246 | &'a G2: std::ops::$trait<F, Output = G2>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
247 | { |
| 0 | 248 | type Output = BothGenerators<G1, G2>; |
| 249 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
250 | fn $fn(self, t: F) -> BothGenerators<G1, G2> { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
251 | BothGenerators(Arc::new(self.0.$fn(t)), Arc::new(self.1.$fn(t))) |
| 0 | 252 | } |
| 253 | } | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
254 | }; |
| 0 | 255 | } |
| 256 | ||
| 257 | make_either_scalarop_rhs!(Mul, mul, MulAssign, mul_assign); | |
| 258 | make_either_scalarop_rhs!(Div, div, DivAssign, div_assign); | |
| 259 | ||
| 260 | impl<G1, G2> std::ops::Neg for BothGenerators<G1, G2> | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
261 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
262 | G1: std::ops::Neg + Clone, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
263 | G2: std::ops::Neg + Clone, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
264 | { |
| 0 | 265 | type Output = BothGenerators<G1::Output, G2::Output>; |
| 266 | #[inline] | |
| 267 | fn neg(self) -> Self::Output { | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
268 | BothGenerators( |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
269 | Arc::new(Arc::unwrap_or_clone(self.0).neg()), |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
270 | Arc::new(Arc::unwrap_or_clone(self.1).neg()), |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
271 | ) |
| 0 | 272 | } |
| 273 | } | |
| 274 | /* | |
| 275 | impl<'a, G1, G2> std::ops::Neg for &'a BothGenerators<G1, G2> | |
| 276 | where &'a G1 : std::ops::Neg, &'a G2 : std::ops::Neg, { | |
| 277 | type Output = BothGenerators<<&'a G1 as std::ops::Neg>::Output, | |
| 278 | <&'a G2 as std::ops::Neg>::Output>; | |
| 279 | fn neg(self) -> Self::Output { | |
| 280 | BothGenerators(self.0.neg(), self.1.neg()) | |
| 281 | } | |
| 282 | } | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
283 | */ |