Mon, 12 May 2025 22:48:16 -0500
instantiated-fubar
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
1 | /*! |
|
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
2 | Direct products of the form $A \times B$. |
|
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
3 | |
|
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
4 | TODO: This could be easily much more generic if `derive_more` could derive arithmetic |
|
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
5 | operations on references. |
|
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
6 | */ |
|
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
7 | |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
8 | use crate::euclidean::Euclidean; |
| 137 | 9 | use crate::instance::{ |
| 10 | Decomposition, DecompositionMut, Instance, InstanceMut, Instantiated, MyCow, | |
| 11 | }; | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
12 | use crate::linops::AXPY; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
13 | use crate::loc::Loc; |
| 94 | 14 | use crate::mapping::Space; |
| 15 | use crate::norms::{HasDual, Norm, NormExponent, Normed, PairNorm, L2}; | |
| 16 | use crate::types::{Float, Num}; | |
| 17 | use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; | |
| 18 | use serde::{Deserialize, Serialize}; | |
| 19 | use std::clone::Clone; | |
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
20 | |
| 94 | 21 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 22 | pub struct Pair<A, B>(pub A, pub B); | |
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
23 | |
| 94 | 24 | impl<A, B> Pair<A, B> { |
| 25 | pub fn new(a: A, b: B) -> Pair<A, B> { | |
| 26 | Pair(a, b) | |
| 27 | } | |
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
28 | } |
|
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
29 | |
| 94 | 30 | impl<A, B> From<(A, B)> for Pair<A, B> { |
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
31 | #[inline] |
| 94 | 32 | fn from((a, b): (A, B)) -> Pair<A, B> { |
| 33 | Pair(a, b) | |
| 34 | } | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
35 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
36 | |
| 94 | 37 | impl<A, B> From<Pair<A, B>> for (A, B) { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
38 | #[inline] |
| 94 | 39 | fn from(Pair(a, b): Pair<A, B>) -> (A, B) { |
| 40 | (a, b) | |
| 41 | } | |
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
42 | } |
|
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
43 | |
| 115 | 44 | macro_rules! impl_unary { |
| 45 | ($trait:ident, $fn:ident) => { | |
| 46 | impl<A, B> $trait for Pair<A, B> | |
| 47 | where | |
| 48 | A: $trait, | |
| 49 | B: $trait, | |
| 50 | { | |
| 51 | type Output = Pair<A::Output, B::Output>; | |
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
52 | fn $fn(self) -> Self::Output { |
| 115 | 53 | let Pair(a, b) = self; |
| 54 | Pair(a.$fn(), b.$fn()) | |
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
55 | } |
|
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
56 | } |
| 115 | 57 | |
| 121 | 58 | // Compiler overflow |
| 59 | // impl<'a, A, B> $trait for &'a Pair<A, B> | |
| 60 | // where | |
| 61 | // &'a A: $trait, | |
| 62 | // &'a B: $trait, | |
| 63 | // { | |
| 64 | // type Output = Pair<<&'a A as $trait>::Output, <&'a B as $trait>::Output>; | |
| 65 | // fn $fn(self) -> Self::Output { | |
| 66 | // let Pair(ref a, ref b) = self; | |
| 67 | // Pair(a.$fn(), b.$fn()) | |
| 68 | // } | |
| 69 | // } | |
| 115 | 70 | }; |
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
71 | } |
|
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
72 | |
| 115 | 73 | impl_unary!(Neg, neg); |
| 74 | ||
| 116 | 75 | macro_rules! impl_binary { |
| 76 | ($trait:ident, $fn:ident) => { | |
| 77 | impl<A, B, C, D> $trait<Pair<C, D>> for Pair<A, B> | |
| 78 | where | |
| 79 | A: $trait<C>, | |
| 80 | B: $trait<D>, | |
| 81 | { | |
| 82 | type Output = Pair<A::Output, B::Output>; | |
| 83 | fn $fn(self, Pair(c, d): Pair<C, D>) -> Self::Output { | |
| 84 | let Pair(a, b) = self; | |
| 85 | Pair(a.$fn(c), b.$fn(d)) | |
| 86 | } | |
| 87 | } | |
| 88 | ||
| 89 | impl<'a, A, B, C, D> $trait<Pair<C, D>> for &'a Pair<A, B> | |
| 90 | where | |
| 91 | &'a A: $trait<C>, | |
| 92 | &'a B: $trait<D>, | |
| 93 | { | |
| 94 | type Output = Pair<<&'a A as $trait<C>>::Output, <&'a B as $trait<D>>::Output>; | |
| 95 | fn $fn(self, Pair(c, d): Pair<C, D>) -> Self::Output { | |
| 96 | let Pair(ref a, ref b) = self; | |
| 97 | Pair(a.$fn(c), b.$fn(d)) | |
| 98 | } | |
| 99 | } | |
| 100 | ||
| 101 | impl<'a, 'b, A, B, C, D> $trait<&'b Pair<C, D>> for &'a Pair<A, B> | |
| 102 | where | |
| 103 | &'a A: $trait<&'b C>, | |
| 104 | &'a B: $trait<&'b D>, | |
| 105 | { | |
| 106 | type Output = Pair<<&'a A as $trait<&'b C>>::Output, <&'a B as $trait<&'b D>>::Output>; | |
| 107 | fn $fn(self, Pair(ref c, ref d): &'b Pair<C, D>) -> Self::Output { | |
| 108 | let Pair(ref a, ref b) = self; | |
| 109 | Pair(a.$fn(c), b.$fn(d)) | |
| 110 | } | |
| 111 | } | |
| 112 | ||
| 113 | impl<'b, A, B, C, D> $trait<&'b Pair<C, D>> for Pair<A, B> | |
| 114 | where | |
| 115 | A: $trait<&'b C>, | |
| 116 | B: $trait<&'b D>, | |
| 117 | { | |
| 118 | type Output = Pair<<A as $trait<&'b C>>::Output, <B as $trait<&'b D>>::Output>; | |
| 119 | fn $fn(self, Pair(ref c, ref d): &'b Pair<C, D>) -> Self::Output { | |
| 120 | let Pair(a, b) = self; | |
| 121 | Pair(a.$fn(c), b.$fn(d)) | |
| 122 | } | |
| 123 | } | |
| 124 | }; | |
| 125 | } | |
| 126 | ||
| 118 | 127 | impl_binary!(Add, add); |
| 128 | impl_binary!(Sub, sub); | |
| 129 | ||
| 119 | 130 | macro_rules! impl_scalar { |
| 120 | 131 | ($trait:ident, $fn:ident) => { |
| 132 | impl<A, B, F: Num> $trait<F> for Pair<A, B> | |
| 119 | 133 | where |
| 120 | 134 | A: $trait<F>, |
| 135 | B: $trait<F>, | |
| 119 | 136 | { |
| 137 | type Output = Pair<A::Output, B::Output>; | |
| 120 | 138 | fn $fn(self, t: F) -> Self::Output { |
| 119 | 139 | let Pair(a, b) = self; |
| 140 | Pair(a.$fn(t), b.$fn(t)) | |
| 141 | } | |
| 142 | } | |
| 143 | ||
| 120 | 144 | impl<'a, A, B, F: Num> $trait<F> for &'a Pair<A, B> |
| 119 | 145 | where |
| 120 | 146 | &'a A: $trait<F>, |
| 147 | &'a B: $trait<F>, | |
| 119 | 148 | { |
| 120 | 149 | type Output = Pair<<&'a A as $trait<F>>::Output, <&'a B as $trait<F>>::Output>; |
| 150 | fn $fn(self, t: F) -> Self::Output { | |
| 119 | 151 | let Pair(ref a, ref b) = self; |
| 152 | Pair(a.$fn(t), b.$fn(t)) | |
| 153 | } | |
| 154 | } | |
| 155 | ||
| 156 | // impl<'a, 'b, A, B> $trait<&'b $F> for &'a Pair<A, B> | |
| 157 | // where | |
| 158 | // &'a A: $trait<&'b $F>, | |
| 159 | // &'a B: $trait<&'b $F>, | |
| 160 | // { | |
| 161 | // type Output = | |
| 162 | // Pair<<&'a A as $trait<&'b $F>>::Output, <&'a B as $trait<&'b $F>>::Output>; | |
| 163 | // fn $fn(self, t: &'b $F) -> Self::Output { | |
| 164 | // let Pair(ref a, ref b) = self; | |
| 165 | // Pair(a.$fn(t), b.$fn(t)) | |
| 166 | // } | |
| 167 | // } | |
| 168 | ||
| 169 | // impl<'b, A, B> $trait<&'b $F> for Pair<A, B> | |
| 170 | // where | |
| 171 | // A: $trait<&'b $F>, | |
| 172 | // B: $trait<&'b $F>, | |
| 173 | // { | |
| 174 | // type Output = Pair<<A as $trait<&'b $F>>::Output, <B as $trait<&'b $F>>::Output>; | |
| 175 | // fn $fn(self, t: &'b $F) -> Self::Output { | |
| 176 | // let Pair(a, b) = self; | |
| 177 | // Pair(a.$fn(t), b.$fn(t)) | |
| 178 | // } | |
| 179 | // } | |
| 180 | }; | |
| 181 | } | |
| 182 | ||
| 120 | 183 | impl_scalar!(Mul, mul); |
| 130 | 184 | impl_scalar!(Div, div); |
| 119 | 185 | |
| 186 | macro_rules! impl_scalar_lhs { | |
| 187 | ($trait:ident, $fn:ident, $F:ty) => { | |
| 188 | impl<A, B> $trait<Pair<A, B>> for $F | |
| 189 | where | |
| 190 | $F: $trait<A> + $trait<B>, | |
| 191 | { | |
| 192 | type Output = Pair<<$F as $trait<A>>::Output, <$F as $trait<B>>::Output>; | |
| 193 | fn $fn(self, Pair(a, b): Pair<A, B>) -> Self::Output { | |
| 194 | Pair(self.$fn(a), self.$fn(b)) | |
| 195 | } | |
| 196 | } | |
| 197 | ||
| 198 | // Compiler overflow: | |
| 199 | // | |
| 200 | // impl<'a, A, B> $trait<&'a Pair<A, B>> for $F | |
| 201 | // where | |
| 202 | // $F: $trait<&'a A> + $trait<&'a B>, | |
| 203 | // { | |
| 204 | // type Output = Pair<<$F as $trait<&'a A>>::Output, <$F as $trait<&'a B>>::Output>; | |
| 205 | // fn $fn(self, Pair(a, b): &'a Pair<A, B>) -> Self::Output { | |
| 206 | // Pair(self.$fn(a), self.$fn(b)) | |
| 207 | // } | |
| 208 | // } | |
| 209 | }; | |
| 210 | } | |
| 211 | ||
| 212 | impl_scalar_lhs!(Mul, mul, f32); | |
| 213 | impl_scalar_lhs!(Mul, mul, f64); | |
| 130 | 214 | impl_scalar_lhs!(Div, div, f32); |
| 215 | impl_scalar_lhs!(Div, div, f64); | |
| 119 | 216 | |
| 117 | 217 | macro_rules! impl_binary_mut { |
| 218 | ($trait:ident, $fn:ident) => { | |
| 219 | impl<'a, A, B, C, D> $trait<Pair<C, D>> for Pair<A, B> | |
| 220 | where | |
| 221 | A: $trait<C>, | |
| 222 | B: $trait<D>, | |
| 223 | { | |
| 224 | fn $fn(&mut self, Pair(c, d): Pair<C, D>) { | |
| 225 | let Pair(ref mut a, ref mut b) = self; | |
| 226 | a.$fn(c); | |
| 227 | b.$fn(d); | |
| 228 | } | |
| 229 | } | |
| 230 | ||
| 231 | impl<'a, 'b, A, B, C, D> $trait<&'b Pair<C, D>> for Pair<A, B> | |
| 232 | where | |
| 233 | A: $trait<&'b C>, | |
| 234 | B: $trait<&'b D>, | |
| 235 | { | |
| 236 | fn $fn(&mut self, Pair(ref c, ref d): &'b Pair<C, D>) { | |
| 237 | let Pair(ref mut a, ref mut b) = self; | |
| 238 | a.$fn(c); | |
| 239 | b.$fn(d); | |
| 240 | } | |
| 241 | } | |
| 242 | }; | |
| 243 | } | |
| 244 | ||
| 245 | impl_binary_mut!(AddAssign, add_assign); | |
| 246 | impl_binary_mut!(SubAssign, sub_assign); | |
| 116 | 247 | |
| 118 | 248 | macro_rules! impl_scalar_mut { |
|
129
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
249 | ($trait:ident, $fn:ident) => { |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
250 | impl<'a, A, B, F: Num> $trait<F> for Pair<A, B> |
| 118 | 251 | where |
|
129
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
252 | A: $trait<F>, |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
253 | B: $trait<F>, |
| 118 | 254 | { |
|
129
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
255 | fn $fn(&mut self, t: F) { |
| 118 | 256 | let Pair(ref mut a, ref mut b) = self; |
| 257 | a.$fn(t); | |
| 258 | b.$fn(t); | |
| 259 | } | |
| 260 | } | |
| 261 | }; | |
| 262 | } | |
| 263 | ||
|
129
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
264 | impl_scalar_mut!(MulAssign, mul_assign); |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
265 | impl_scalar_mut!(DivAssign, div_assign); |
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
266 | |
| 119 | 267 | /// We only support 'closed' `Euclidean` `Pair`s, as more general ones cause |
| 268 | /// compiler overflows. | |
| 118 | 269 | impl<A, B, F: Float> Euclidean<F> for Pair<A, B> |
| 270 | where | |
| 271 | A: Euclidean<F>, | |
| 272 | B: Euclidean<F>, | |
| 273 | //Pair<A, B>: Euclidean<F>, | |
| 274 | Self: Sized | |
|
132
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
130
diff
changeset
|
275 | + Mul<F, Output = <Self as AXPY>::Owned> |
| 118 | 276 | + MulAssign<F> |
|
132
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
130
diff
changeset
|
277 | + Div<F, Output = <Self as AXPY>::Owned> |
| 118 | 278 | + DivAssign<F> |
|
132
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
130
diff
changeset
|
279 | + Add<Self, Output = <Self as AXPY>::Owned> |
|
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
130
diff
changeset
|
280 | + Sub<Self, Output = <Self as AXPY>::Owned> |
|
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
130
diff
changeset
|
281 | + for<'b> Add<&'b Self, Output = <Self as AXPY>::Owned> |
|
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
130
diff
changeset
|
282 | + for<'b> Sub<&'b Self, Output = <Self as AXPY>::Owned> |
| 118 | 283 | + AddAssign<Self> |
| 284 | + for<'b> AddAssign<&'b Self> | |
| 285 | + SubAssign<Self> | |
| 286 | + for<'b> SubAssign<&'b Self> | |
|
132
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
130
diff
changeset
|
287 | + Neg<Output = <Self as AXPY>::Owned>, |
| 118 | 288 | { |
| 289 | fn dot<I: Instance<Self>>(&self, other: I) -> F { | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
290 | other.eval_decompose(|Pair(u, v)| self.0.dot(u) + self.1.dot(v)) |
| 118 | 291 | } |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
292 | |
| 118 | 293 | fn norm2_squared(&self) -> F { |
| 294 | self.0.norm2_squared() + self.1.norm2_squared() | |
| 295 | } | |
| 296 | ||
| 297 | fn dist2_squared<I: Instance<Self>>(&self, other: I) -> F { | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
298 | other.eval_decompose(|Pair(u, v)| self.0.dist2_squared(u) + self.1.dist2_squared(v)) |
| 118 | 299 | } |
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
300 | } |
|
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
301 | |
|
129
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
302 | impl<F, A, B, U, V> AXPY<Pair<U, V>> for Pair<A, B> |
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
303 | where |
| 94 | 304 | U: Space, |
| 305 | V: Space, | |
|
129
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
306 | A: AXPY<U, Field = F>, |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
307 | B: AXPY<V, Field = F>, |
| 94 | 308 | F: Num, |
| 130 | 309 | Self: MulAssign<F> + DivAssign<F>, |
| 310 | Pair<A, B>: MulAssign<F> + DivAssign<F>, | |
|
129
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
311 | //A::Owned: MulAssign<F>, |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
312 | //B::Owned: MulAssign<F>, |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
313 | //Pair<A::Owned, B::Owned>: AXPY<Pair<U, V>, Field = F>, |
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
314 | { |
|
129
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
315 | type Field = F; |
|
62
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
316 | type Owned = Pair<A::Owned, B::Owned>; |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
317 | |
| 94 | 318 | fn axpy<I: Instance<Pair<U, V>>>(&mut self, α: F, x: I, β: F) { |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
319 | x.eval_decompose(|Pair(u, v)| { |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
320 | self.0.axpy(α, u, β); |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
321 | self.1.axpy(α, v, β); |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
322 | }) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
323 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
324 | |
| 94 | 325 | fn copy_from<I: Instance<Pair<U, V>>>(&mut self, x: I) { |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
326 | x.eval_decompose(|Pair(u, v)| { |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
327 | self.0.copy_from(u); |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
328 | self.1.copy_from(v); |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
329 | }) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
330 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
331 | |
| 94 | 332 | fn scale_from<I: Instance<Pair<U, V>>>(&mut self, α: F, x: I) { |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
333 | x.eval_decompose(|Pair(u, v)| { |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
334 | self.0.scale_from(α, u); |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
335 | self.1.scale_from(α, v); |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
336 | }) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
337 | } |
|
62
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
338 | |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
339 | /// Return a similar zero as `self`. |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
340 | fn similar_origin(&self) -> Self::Owned { |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
341 | Pair(self.0.similar_origin(), self.1.similar_origin()) |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
342 | } |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
343 | |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
344 | /// Set self to zero. |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
345 | fn set_zero(&mut self) { |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
346 | self.0.set_zero(); |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
347 | self.1.set_zero(); |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
348 | } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
349 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
350 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
351 | /// [`Decomposition`] for working with [`Pair`]s. |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
352 | #[derive(Copy, Clone, Debug)] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
353 | pub struct PairDecomposition<D, Q>(D, Q); |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
354 | |
| 94 | 355 | impl<A: Space, B: Space> Space for Pair<A, B> { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
356 | type Decomp = PairDecomposition<A::Decomp, B::Decomp>; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
357 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
358 | |
| 94 | 359 | impl<A, B, D, Q> Decomposition<Pair<A, B>> for PairDecomposition<D, Q> |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
360 | where |
| 94 | 361 | A: Space, |
| 362 | B: Space, | |
| 363 | D: Decomposition<A>, | |
| 364 | Q: Decomposition<B>, | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
365 | { |
| 94 | 366 | type Decomposition<'b> |
| 367 | = Pair<D::Decomposition<'b>, Q::Decomposition<'b>> | |
| 368 | where | |
| 369 | Pair<A, B>: 'b; | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
370 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
371 | |
| 137 | 372 | impl<A, B, U, V, D, Q> Instantiated<Pair<A, B>, PairDecomposition<D, Q>> for Pair<U, V> |
| 373 | where | |
| 374 | A: Space, | |
| 375 | B: Space, | |
| 376 | U: Instantiated<A, D>, | |
| 377 | V: Instantiated<B, Q>, | |
| 378 | D: Decomposition<A>, | |
| 379 | Q: Decomposition<B>, | |
| 380 | { | |
| 381 | type RefInstance<'b> | |
| 382 | = Pair<U::RefInstance<'b>, V::RefInstance<'b>> | |
| 383 | where | |
| 384 | Self: 'b; | |
| 385 | ||
| 386 | fn ref_inst(&self) -> Self::RefInstance<'_> { | |
| 387 | Pair(self.0.ref_inst(), self.1.ref_inst()) | |
| 388 | } | |
| 389 | } | |
| 390 | ||
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
391 | impl<A, B, U, V, D, Q> Instance<Pair<A, B>, PairDecomposition<D, Q>> for Pair<U, V> |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
392 | where |
| 94 | 393 | A: Space, |
| 394 | B: Space, | |
| 395 | D: Decomposition<A>, | |
| 396 | Q: Decomposition<B>, | |
| 397 | U: Instance<A, D>, | |
| 398 | V: Instance<B, Q>, | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
399 | { |
| 137 | 400 | type Instantiated = Pair<U::Instantiated, V::Instantiated>; |
| 401 | ||
| 402 | fn instantiate(self) -> Self::Instantiated { | |
| 403 | Pair(self.0.instantiate(), self.1.instantiate()) | |
| 404 | } | |
| 405 | ||
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
406 | fn eval_decompose<'b, R>( |
| 94 | 407 | self, |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
408 | f: impl FnOnce(Pair<D::Decomposition<'b>, Q::Decomposition<'b>>) -> R, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
409 | ) -> R |
| 94 | 410 | where |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
411 | Pair<A, B>: 'b, |
| 94 | 412 | Self: 'b, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
413 | { |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
414 | self.0 |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
415 | .eval_decompose(|a| self.1.eval_decompose(|b| f(Pair(a, b)))) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
416 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
417 | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
418 | fn eval_ref_decompose<'b, R>( |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
419 | &'b self, |
| 136 | 420 | f: impl FnOnce(Pair<D::Decomposition<'b>, Q::Decomposition<'b>>) -> R, |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
421 | ) -> R |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
422 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
423 | Pair<A, B>: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
424 | Self: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
425 | { |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
426 | self.0 |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
427 | .eval_ref_decompose(|a| self.1.eval_ref_decompose(|b| f(Pair(a, b)))) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
428 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
429 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
430 | #[inline] |
| 94 | 431 | fn cow<'b>(self) -> MyCow<'b, Pair<A, B>> |
| 432 | where | |
| 433 | Self: 'b, | |
| 434 | { | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
435 | MyCow::Owned(Pair(self.0.own(), self.1.own())) |
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
436 | } |
|
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
437 | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
438 | #[inline] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
439 | fn own(self) -> Pair<A, B> { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
440 | Pair(self.0.own(), self.1.own()) |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
441 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
442 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
443 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
444 | impl<'a, A, B, U, V, D, Q> Instance<Pair<A, B>, PairDecomposition<D, Q>> for &'a Pair<U, V> |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
445 | where |
| 94 | 446 | A: Space, |
| 447 | B: Space, | |
| 448 | D: Decomposition<A>, | |
| 449 | Q: Decomposition<B>, | |
| 450 | U: Instance<A, D>, | |
| 451 | V: Instance<B, Q>, | |
| 452 | &'a U: Instance<A, D>, | |
| 453 | &'a V: Instance<B, Q>, | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
454 | { |
| 137 | 455 | type Instantiated = |
| 456 | Pair<<&'a U as Instance<A, D>>::Instantiated, <&'a V as Instance<B, Q>>::Instantiated>; | |
| 457 | ||
| 458 | fn instantiate(self) -> Self::Instantiated { | |
| 459 | let &Pair(ref u, ref v) = self; | |
| 460 | Pair(u.instantiate(), v.instantiate()) | |
| 461 | } | |
| 462 | ||
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
463 | fn eval_decompose<'b, R>( |
| 94 | 464 | self, |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
465 | f: impl FnOnce(Pair<D::Decomposition<'b>, Q::Decomposition<'b>>) -> R, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
466 | ) -> R |
| 94 | 467 | where |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
468 | Pair<A, B>: 'b, |
| 94 | 469 | Self: 'b, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
470 | { |
| 136 | 471 | self.0 |
| 472 | .eval_ref_decompose(|a| self.1.eval_ref_decompose(|b| f(Pair(a, b)))) | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
473 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
474 | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
475 | fn eval_ref_decompose<'b, R>( |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
476 | &'b self, |
| 136 | 477 | f: impl FnOnce(Pair<D::Decomposition<'b>, Q::Decomposition<'b>>) -> R, |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
478 | ) -> R |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
479 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
480 | Pair<A, B>: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
481 | Self: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
482 | { |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
483 | self.0 |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
484 | .eval_ref_decompose(|a| self.1.eval_ref_decompose(|b| f(Pair(a, b)))) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
485 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
486 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
487 | #[inline] |
| 94 | 488 | fn cow<'b>(self) -> MyCow<'b, Pair<A, B>> |
| 489 | where | |
| 490 | Self: 'b, | |
| 491 | { | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
492 | MyCow::Owned(self.own()) |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
493 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
494 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
495 | #[inline] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
496 | fn own(self) -> Pair<A, B> { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
497 | let Pair(ref u, ref v) = self; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
498 | Pair(u.own(), v.own()) |
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
499 | } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
500 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
501 | |
| 94 | 502 | impl<A, B, D, Q> DecompositionMut<Pair<A, B>> for PairDecomposition<D, Q> |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
503 | where |
| 94 | 504 | A: Space, |
| 505 | B: Space, | |
| 506 | D: DecompositionMut<A>, | |
| 507 | Q: DecompositionMut<B>, | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
508 | { |
| 94 | 509 | type ReferenceMut<'b> |
| 510 | = Pair<D::ReferenceMut<'b>, Q::ReferenceMut<'b>> | |
| 511 | where | |
| 512 | Pair<A, B>: 'b; | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
513 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
514 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
515 | impl<A, B, U, V, D, Q> InstanceMut<Pair<A, B>, PairDecomposition<D, Q>> for Pair<U, V> |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
516 | where |
| 94 | 517 | A: Space, |
| 518 | B: Space, | |
| 519 | D: DecompositionMut<A>, | |
| 520 | Q: DecompositionMut<B>, | |
| 521 | U: InstanceMut<A, D>, | |
| 522 | V: InstanceMut<B, Q>, | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
523 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
524 | #[inline] |
| 94 | 525 | fn ref_instance_mut( |
| 526 | &mut self, | |
| 527 | ) -> <PairDecomposition<D, Q> as DecompositionMut<Pair<A, B>>>::ReferenceMut<'_> { | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
528 | Pair(self.0.ref_instance_mut(), self.1.ref_instance_mut()) |
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
529 | } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
530 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
531 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
532 | impl<'a, A, B, U, V, D, Q> InstanceMut<Pair<A, B>, PairDecomposition<D, Q>> for &'a mut Pair<U, V> |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
533 | where |
| 94 | 534 | A: Space, |
| 535 | B: Space, | |
| 536 | D: DecompositionMut<A>, | |
| 537 | Q: DecompositionMut<B>, | |
| 538 | U: InstanceMut<A, D>, | |
| 539 | V: InstanceMut<B, Q>, | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
540 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
541 | #[inline] |
| 94 | 542 | fn ref_instance_mut( |
| 543 | &mut self, | |
| 544 | ) -> <PairDecomposition<D, Q> as DecompositionMut<Pair<A, B>>>::ReferenceMut<'_> { | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
545 | Pair(self.0.ref_instance_mut(), self.1.ref_instance_mut()) |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
546 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
547 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
548 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
121
diff
changeset
|
549 | impl<F, A, B, ExpA, ExpB, ExpJ> Norm<PairNorm<ExpA, ExpB, ExpJ>, F> for Pair<A, B> |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
550 | where |
| 94 | 551 | F: Num, |
| 552 | ExpA: NormExponent, | |
| 553 | ExpB: NormExponent, | |
| 554 | ExpJ: NormExponent, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
121
diff
changeset
|
555 | A: Norm<ExpA, F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
121
diff
changeset
|
556 | B: Norm<ExpB, F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
121
diff
changeset
|
557 | Loc<2, F>: Norm<ExpJ, F>, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
558 | { |
| 94 | 559 | fn norm(&self, PairNorm(expa, expb, expj): PairNorm<ExpA, ExpB, ExpJ>) -> F { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
560 | Loc([self.0.norm(expa), self.1.norm(expb)]).norm(expj) |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
561 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
562 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
563 | |
| 94 | 564 | impl<F: Float, A, B> Normed<F> for Pair<A, B> |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
565 | where |
| 94 | 566 | A: Normed<F>, |
| 567 | B: Normed<F>, | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
568 | { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
569 | type NormExp = PairNorm<A::NormExp, B::NormExp, L2>; |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
570 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
571 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
572 | fn norm_exponent(&self) -> Self::NormExp { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
573 | PairNorm(self.0.norm_exponent(), self.1.norm_exponent(), L2) |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
574 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
575 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
576 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
577 | fn is_zero(&self) -> bool { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
578 | self.0.is_zero() && self.1.is_zero() |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
579 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
580 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
581 | |
| 94 | 582 | impl<F: Float, A, B> HasDual<F> for Pair<A, B> |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
583 | where |
| 94 | 584 | A: HasDual<F>, |
| 585 | B: HasDual<F>, | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
586 | { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
587 | type DualSpace = Pair<A::DualSpace, B::DualSpace>; |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
588 | } |