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