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