src/euclidean/wrap.rs

branch
dev
changeset 183
d077dff509f1
parent 179
724413fc8d17
equal deleted inserted replaced
182:71fa6d3df947 183:d077dff509f1
4 4
5 use crate::euclidean::Euclidean; 5 use crate::euclidean::Euclidean;
6 use crate::instance::Space; 6 use crate::instance::Space;
7 use crate::types::Float; 7 use crate::types::Float;
8 8
9 pub trait WrapGuard<'a, F: Float> {
10 type View<'b>: Euclidean<F>
11 where
12 Self: 'b;
13 fn get_view(&self) -> Self::View<'_>;
14 }
15
16 pub trait WrapGuardMut<'a, F: Float> {
17 type ViewMut<'b>: Euclidean<F>
18 where
19 Self: 'b;
20 fn get_view_mut(&mut self) -> Self::ViewMut<'_>;
21 }
22
9 pub trait Wrapped: Space { 23 pub trait Wrapped: Space {
10 type WrappedField: Float; 24 type WrappedField: Float;
11 type Unwrapped: Euclidean<Self::WrappedField>; 25 type Guard<'a>: WrapGuard<'a, Self::WrappedField>
12 type UnwrappedMut: Euclidean<Self::WrappedField>; 26 where
13 type UnwrappedOutput: Euclidean<Self::WrappedField>; 27 Self: 'a;
28 type GuardMut<'a>: WrapGuardMut<'a, Self::WrappedField>
29 where
30 Self: 'a;
31 type UnwrappedOutput;
14 type WrappedOutput; 32 type WrappedOutput;
15 fn get_view(&self) -> Self::Unwrapped; 33 fn get_guard(&self) -> Self::Guard<'_>;
16 fn get_view_mut(&mut self) -> Self::UnwrappedMut; 34 fn get_guard_mut(&mut self) -> Self::GuardMut<'_>;
17 fn wrap(output: Self::UnwrappedOutput) -> Self::WrappedOutput; 35 fn wrap(output: Self::UnwrappedOutput) -> Self::WrappedOutput;
18 } 36 }
19 37
20 #[macro_export] 38 #[macro_export]
21 macro_rules! wrap { 39 macro_rules! wrap {
23 // manually code paths through $($trait:ident)::+. 41 // manually code paths through $($trait:ident)::+.
24 (impl_unary $type:ty, $($trait:ident)::+, $fn:ident where $($qual:tt)*) => { 42 (impl_unary $type:ty, $($trait:ident)::+, $fn:ident where $($qual:tt)*) => {
25 impl<$($qual)*> $($trait)::+ for $type { 43 impl<$($qual)*> $($trait)::+ for $type {
26 type Output = <Self as $crate::euclidean::wrap::Wrapped>::WrappedOutput; 44 type Output = <Self as $crate::euclidean::wrap::Wrapped>::WrappedOutput;
27 fn $fn(self) -> Self::Output { 45 fn $fn(self) -> Self::Output {
28 Self::wrap(self.get_view().$fn()) 46 let a = self.get_guard();
47 Self::wrap(a.get_view().$fn())
29 } 48 }
30 } 49 }
31 }; 50 };
32 (impl_binary $type:ty, $($trait:ident)::+, $fn:ident where $($qual:tt)*) => { 51 (impl_binary $type:ty, $($trait:ident)::+, $fn:ident where $($qual:tt)*) => {
33 impl<$($qual)*> $($trait)::+<$type> for $type { 52 impl<$($qual)*> $($trait)::+<$type> for $type {
34 type Output = <Self as $crate::euclidean::wrap::Wrapped>::WrappedOutput; 53 type Output = <Self as $crate::euclidean::wrap::Wrapped>::WrappedOutput;
35 fn $fn(self, other: $type) -> Self::Output { 54 fn $fn(self, other: $type) -> Self::Output {
36 Self::wrap(self.get_view().$fn(other.get_view())) 55 let a = self.get_guard();
56 let b = other.get_guard();
57 Self::wrap(a.get_view().$fn(b.get_view()))
37 } 58 }
38 } 59 }
39 60
40 impl<'a, $($qual)*> $($trait)::+<$type> for &'a $type { 61 impl<'a, $($qual)*> $($trait)::+<$type> for &'a $type {
41 type Output = <$type as $crate::euclidean::wrap::Wrapped>::WrappedOutput; 62 type Output = <$type as $crate::euclidean::wrap::Wrapped>::WrappedOutput;
42 fn $fn(self, other: $type) -> Self::Output { 63 fn $fn(self, other: $type) -> Self::Output {
43 <$type>::wrap(self.get_view().$fn(other.get_view())) 64 let a = self.get_guard();
65 let b = other.get_guard();
66 <$type>::wrap(a.get_view().$fn(b.get_view()))
44 } 67 }
45 } 68 }
46 69
47 impl<'a, 'b, $($qual)*> $($trait)::+<&'b $type> for &'a $type { 70 impl<'a, 'b, $($qual)*> $($trait)::+<&'b $type> for &'a $type {
48 type Output = <$type as $crate::euclidean::wrap::Wrapped>::WrappedOutput; 71 type Output = <$type as $crate::euclidean::wrap::Wrapped>::WrappedOutput;
49 fn $fn(self, other: &'b $type) -> Self::Output { 72 fn $fn(self, other: &'b $type) -> Self::Output {
50 <$type>::wrap(self.get_view().$fn(other.get_view())) 73 let a = self.get_guard();
74 let b = other.get_guard();
75 <$type>::wrap(a.get_view().$fn(b.get_view()))
51 } 76 }
52 } 77 }
53 78
54 impl<'b, $($qual)*> $($trait)::+<&'b $type> for $type { 79 impl<'b, $($qual)*> $($trait)::+<&'b $type> for $type {
55 type Output = <Self as $crate::euclidean::wrap::Wrapped>::WrappedOutput; 80 type Output = <Self as $crate::euclidean::wrap::Wrapped>::WrappedOutput;
56 fn $fn(self, other: &'b $type) -> Self::Output { 81 fn $fn(self, other: &'b $type) -> Self::Output {
57 Self::wrap(self.get_view().$fn(other.get_view())) 82 let a = self.get_guard();
83 let b = other.get_guard();
84 Self::wrap(a.get_view().$fn(b.get_view()))
58 } 85 }
59 } 86 }
60 }; 87 };
61 (impl_scalar $F:ty, $type:ty, $($trait:ident)::+, $fn:ident where $($qual:tt)*) => { 88 (impl_scalar $F:ty, $type:ty, $($trait:ident)::+, $fn:ident where $($qual:tt)*) => {
62 impl<$($qual)*> $($trait)::+<$F> for $type 89 impl<$($qual)*> $($trait)::+<$F> for $type
64 // $type: $crate::euclidean::wrap::Wrapped<WrappedField = F>, 91 // $type: $crate::euclidean::wrap::Wrapped<WrappedField = F>,
65 // //$type::Unwrapped: $($trait)::+<F>, 92 // //$type::Unwrapped: $($trait)::+<F>,
66 { 93 {
67 type Output = <Self as $crate::euclidean::wrap::Wrapped>::WrappedOutput; 94 type Output = <Self as $crate::euclidean::wrap::Wrapped>::WrappedOutput;
68 fn $fn(self, t: $F) -> Self::Output { 95 fn $fn(self, t: $F) -> Self::Output {
69 Self::wrap(self.get_view().$fn(t)) 96 let a = self.get_guard();
97 Self::wrap(a.get_view().$fn(t))
70 } 98 }
71 } 99 }
72 100
73 impl<'a, $($qual)*> $($trait)::+<$F> for &'a $type 101 impl<'a, $($qual)*> $($trait)::+<$F> for &'a $type
74 // where 102 // where
75 // $type: $crate::euclidean::wrap::Wrapped<WrappedField = F>, 103 // $type: $crate::euclidean::wrap::Wrapped<WrappedField = F>,
76 // //$type::Unwrapped: $($trait)::+<F>, 104 // //$type::Unwrapped: $($trait)::+<F>,
77 { 105 {
78 type Output = <$type as $crate::euclidean::wrap::Wrapped>::WrappedOutput; 106 type Output = <$type as $crate::euclidean::wrap::Wrapped>::WrappedOutput;
79 fn $fn(self, t: $F) -> Self::Output { 107 fn $fn(self, t: $F) -> Self::Output {
80 <$type>::wrap(self.get_view().$fn(t)) 108 let a = self.get_guard();
109 <$type>::wrap(a.get_view().$fn(t))
81 } 110 }
82 } 111 }
83 112
84 }; 113 };
85 (impl_scalar_lhs $F:ty, $type:ty, $($trait:ident)::+, $fn:ident where $($qual:tt)*) => { 114 (impl_scalar_lhs $F:ty, $type:ty, $($trait:ident)::+, $fn:ident where $($qual:tt)*) => {
89 // // where 118 // // where
90 // // $F: $($trait)::+<$type::Unwrapped>, 119 // // $F: $($trait)::+<$type::Unwrapped>,
91 { 120 {
92 type Output = <$type as $crate::euclidean::wrap::Wrapped>::WrappedOutput; 121 type Output = <$type as $crate::euclidean::wrap::Wrapped>::WrappedOutput;
93 fn $fn(self, rhs: $type) -> Self::Output { 122 fn $fn(self, rhs: $type) -> Self::Output {
94 <$type>::wrap(self.$fn(rhs.get_view())) 123 let b = rhs.get_guard();
124 <$type>::wrap(self.$fn(b.get_view()))
95 } 125 }
96 } 126 }
97 }; 127 };
98 (impl_binary_mut $type:ty, $($trait:ident)::+, $fn:ident where $($qual:tt)*) => { 128 (impl_binary_mut $type:ty, $($trait:ident)::+, $fn:ident where $($qual:tt)*) => {
99 impl<$($qual)*> $($trait)::+<$type> for $type { 129 impl<$($qual)*> $($trait)::+<$type> for $type {
100 fn $fn(&mut self, rhs: $type) { 130 fn $fn(&mut self, rhs: $type) {
101 self.get_view_mut().$fn(rhs.get_view()) 131 let mut a = self.get_guard_mut();
132 let b = rhs.get_guard();
133 a.get_view_mut().$fn(b.get_view())
102 } 134 }
103 } 135 }
104 136
105 impl<'b, $($qual)*> $($trait)::+<&'b $type> for $type { 137 impl<'b, $($qual)*> $($trait)::+<&'b $type> for $type {
106 fn $fn(&mut self, rhs: &'b $type) { 138 fn $fn(&mut self, rhs: &'b $type) {
107 self.get_view_mut().$fn(rhs.get_view()) 139 let mut a = self.get_guard_mut();
140 let b = rhs.get_guard();
141 a.get_view_mut().$fn(b.get_view())
108 } 142 }
109 } 143 }
110 }; 144 };
111 (impl_scalar_mut $F:ty, $type:ty, $($trait:ident)::+, $fn:ident where $($qual:tt)*) => { 145 (impl_scalar_mut $F:ty, $type:ty, $($trait:ident)::+, $fn:ident where $($qual:tt)*) => {
112 impl<$($qual)*> $($trait)::+<$F> for $type 146 impl<$($qual)*> $($trait)::+<$F> for $type
114 // $type: $crate::euclidean::wrap::Wrapped<WrappedField = F>, 148 // $type: $crate::euclidean::wrap::Wrapped<WrappedField = F>,
115 // // where 149 // // where
116 // // $type::UnwrappedMut: $($trait)::+<$($trait)::+<F>>, 150 // // $type::UnwrappedMut: $($trait)::+<$($trait)::+<F>>,
117 { 151 {
118 fn $fn(&mut self, t: $F) { 152 fn $fn(&mut self, t: $F) {
119 self.get_view_mut().$fn(t) 153 let mut a = self.get_guard_mut();
154 a.get_view_mut().$fn(t)
120 } 155 }
121 } 156 }
122 }; 157 };
123 // ($type:ty) => { 158 // ($type:ty) => {
124 // $crate::wrap!(imp<> do $type); 159 // $crate::wrap!(imp<> do $type);
139 $crate::self_ownable!($type where $($qual)*); 174 $crate::self_ownable!($type where $($qual)*);
140 175
141 impl<$($qual)*> $crate::norms::Norm<$crate::norms::L2, $F> for $type 176 impl<$($qual)*> $crate::norms::Norm<$crate::norms::L2, $F> for $type
142 { 177 {
143 fn norm(&self, p : $crate::norms::L2) -> $F { 178 fn norm(&self, p : $crate::norms::L2) -> $F {
144 $crate::norms::Norm::norm(&self.get_view(), p) 179 let a = self.get_guard();
180 $crate::norms::Norm::norm(&a.get_view(), p)
145 } 181 }
146 } 182 }
147 183
148 impl<$($qual)*> $crate::norms::Dist<$crate::norms::L2, $F> for $type 184 impl<$($qual)*> $crate::norms::Dist<$crate::norms::L2, $F> for $type
149 { 185 {
150 fn dist<I: $crate::instance::Instance<Self>>(&self, other : I, p : $crate::norms::L2) -> $F { 186 fn dist<I: $crate::instance::Instance<Self>>(&self, other : I, p : $crate::norms::L2) -> $F {
151 other.eval_ref(|x| self.get_view().dist(x.get_view(), p)) 187 other.eval_ref(|other| {
188 let a = self.get_guard();
189 let b = other.get_guard();
190 a.get_view().dist(b.get_view(), p)
191 })
152 } 192 }
153 } 193 }
154 194
155 impl<$($qual)*> $crate::norms::Normed<$F> for $type { 195 impl<$($qual)*> $crate::norms::Normed<$F> for $type {
156 type NormExp = $crate::norms::L2; 196 type NormExp = $crate::norms::L2;
187 // + std::ops::Neg<Output = <Self as $crate::linops::AXPY>::Owned>, 227 // + std::ops::Neg<Output = <Self as $crate::linops::AXPY>::Owned>,
188 { 228 {
189 type PrincipalE = Self; 229 type PrincipalE = Self;
190 230
191 fn dot<I: $crate::instance::Instance<Self>>(&self, other: I) -> $F { 231 fn dot<I: $crate::instance::Instance<Self>>(&self, other: I) -> $F {
192 other.eval_decompose(|x| self.get_view().dot(&x.get_view())) 232 other.eval_decompose(|other| {
233 let a = self.get_guard();
234 let b = other.get_guard();
235 a.get_view().dot(&b.get_view())
236 })
193 } 237 }
194 238
195 fn norm2_squared(&self) -> $F { 239 fn norm2_squared(&self) -> $F {
196 self.get_view().norm2_squared() 240 let a = self.get_guard();
241 a.get_view().norm2_squared()
197 } 242 }
198 243
199 fn dist2_squared<I: $crate::instance::Instance<Self>>(&self, other: I) -> $F { 244 fn dist2_squared<I: $crate::instance::Instance<Self>>(&self, other: I) -> $F {
200 other.eval_decompose(|x| self.get_view().dist2_squared(x.get_view())) 245 other.eval_decompose(|other| {
246 let a = self.get_guard();
247 let b = other.get_guard();
248 a.get_view().dist2_squared(b.get_view())
249 })
201 } 250 }
202 } 251 }
203 252
204 impl<$($qual)*> $crate::linops::VectorSpace for $type 253 impl<$($qual)*> $crate::linops::VectorSpace for $type
205 // where 254 // where
211 type Field = $F; 260 type Field = $F;
212 type PrincipalV = Self; 261 type PrincipalV = Self;
213 262
214 /// Return a similar zero as `self`. 263 /// Return a similar zero as `self`.
215 fn similar_origin(&self) -> Self::PrincipalV { 264 fn similar_origin(&self) -> Self::PrincipalV {
216 Self::wrap(self.get_view().similar_origin()) 265 let a = self.get_guard();
266 Self::wrap(a.get_view().similar_origin())
217 } 267 }
218 } 268 }
219 269
220 impl<$($qual)*> $crate::linops::AXPY for $type 270 impl<$($qual)*> $crate::linops::AXPY for $type
221 // where 271 // where
223 // Self::Unwrapped : $crate::linops::AXPY<Field = F>, 273 // Self::Unwrapped : $crate::linops::AXPY<Field = F>,
224 // Self: std::ops::MulAssign<F> + std::ops::DivAssign<F>, 274 // Self: std::ops::MulAssign<F> + std::ops::DivAssign<F>,
225 // Self::Unwrapped: std::ops::MulAssign<F> + std::ops::DivAssign<F>, 275 // Self::Unwrapped: std::ops::MulAssign<F> + std::ops::DivAssign<F>,
226 { 276 {
227 fn axpy<I: $crate::instance::Instance<Self>>(&mut self, α: $F, x: I, β: $F) { 277 fn axpy<I: $crate::instance::Instance<Self>>(&mut self, α: $F, x: I, β: $F) {
228 x.eval_decompose(|v| { 278 x.eval_decompose(|other| {
229 $crate::linops::AXPY::axpy(&mut self.get_view_mut(), α, v.get_view(), β) 279 let mut a = self.get_guard_mut();
280 let b = other.get_guard();
281 $crate::linops::AXPY::axpy(&mut a.get_view_mut(), α, b.get_view(), β)
230 }) 282 })
231 } 283 }
232 284
233 fn copy_from<I: $crate::instance::Instance<Self>>(&mut self, x: I) { 285 fn copy_from<I: $crate::instance::Instance<Self>>(&mut self, x: I) {
234 x.eval_decompose(|v| { 286 x.eval_decompose(|other| {
235 $crate::linops::AXPY::copy_from(&mut self.get_view_mut(), v.get_view()) 287 let mut a = self.get_guard_mut();
288 let b = other.get_guard();
289 $crate::linops::AXPY::copy_from(&mut a.get_view_mut(), b.get_view())
236 }) 290 })
237 } 291 }
238 292
239 fn scale_from<I: $crate::instance::Instance<Self>>(&mut self, α: $F, x: I) { 293 fn scale_from<I: $crate::instance::Instance<Self>>(&mut self, α: $F, x: I) {
240 x.eval_decompose(|v| { 294 x.eval_decompose(|other| {
241 $crate::linops::AXPY::scale_from(&mut self.get_view_mut(), α, v.get_view()) 295 let mut a = self.get_guard_mut();
296 let b = other.get_guard();
297 $crate::linops::AXPY::scale_from(&mut a.get_view_mut(), α, b.get_view())
242 }) 298 })
243 } 299 }
244 300
245 /// Set self to zero. 301 /// Set self to zero.
246 fn set_zero(&mut self) { 302 fn set_zero(&mut self) {
247 self.get_view_mut().set_zero() 303 let mut a = self.get_guard_mut();
304 a.get_view_mut().set_zero()
248 } 305 }
249 } 306 }
250 307
251 impl<$($qual)*> $crate::instance::Space for $type { 308 impl<$($qual)*> $crate::instance::Space for $type {
252 type Decomp = $crate::instance::BasicDecomposition; 309 type Decomp = $crate::instance::BasicDecomposition;

mercurial