Tue, 02 Sep 2025 00:09:46 -0500
sketc
|
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 | ||
| 278 | /// Returns an owned instance of a reference. | |
| 279 | fn clone_owned(&self) -> Self::OwnedVariant { | |
| 280 | Pair(self.0.clone_owned(), self.1.clone_owned()) | |
| 281 | } | |
| 282 | } | |
| 283 | ||
| 119 | 284 | /// We only support 'closed' `Euclidean` `Pair`s, as more general ones cause |
| 285 | /// compiler overflows. | |
| 118 | 286 | impl<A, B, F: Float> Euclidean<F> for Pair<A, B> |
| 287 | where | |
| 288 | A: Euclidean<F>, | |
| 289 | B: Euclidean<F>, | |
| 151 | 290 | // //Pair<A, B>: Euclidean<F>, |
| 291 | // Self: Sized | |
| 292 | // + Mul<F, Output = Self::OwnedEuclidean> | |
| 293 | // + MulAssign<F> | |
| 294 | // + Div<F, Output = Self::OwnedEuclidean> | |
| 295 | // + DivAssign<F> | |
| 296 | // + Add<Self, Output = Self::OwnedEuclidean> | |
| 297 | // + Sub<Self, Output = Self::OwnedEuclidean> | |
| 298 | // + for<'b> Add<&'b Self, Output = Self::OwnedEuclidean> | |
| 299 | // + for<'b> Sub<&'b Self, Output = Self::OwnedEuclidean> | |
| 300 | // + AddAssign<Self> | |
| 301 | // + for<'b> AddAssign<&'b Self> | |
| 302 | // + SubAssign<Self> | |
| 303 | // + for<'b> SubAssign<&'b Self> | |
| 304 | // + Neg<Output = Self::OwnedEuclidean>, | |
| 118 | 305 | { |
| 151 | 306 | type OwnedEuclidean = Pair<A::OwnedEuclidean, B::OwnedEuclidean>; |
| 307 | ||
| 118 | 308 | 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
|
309 | other.eval_decompose(|Pair(u, v)| self.0.dot(u) + self.1.dot(v)) |
| 118 | 310 | } |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
311 | |
| 118 | 312 | fn norm2_squared(&self) -> F { |
| 313 | self.0.norm2_squared() + self.1.norm2_squared() | |
| 314 | } | |
| 315 | ||
| 316 | 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
|
317 | other.eval_decompose(|Pair(u, v)| self.0.dist2_squared(u) + self.1.dist2_squared(v)) |
| 118 | 318 | } |
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
319 | } |
|
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
320 | |
| 151 | 321 | impl<F, A, B> VectorSpace for Pair<A, B> |
| 150 | 322 | where |
| 151 | 323 | A: VectorSpace<Field = F>, |
| 324 | B: VectorSpace<Field = F>, | |
| 150 | 325 | F: Num, |
| 326 | { | |
| 327 | type Field = F; | |
| 151 | 328 | type Owned = Pair<A::Owned, B::Owned>; |
| 150 | 329 | |
| 330 | /// Return a similar zero as `self`. | |
| 331 | fn similar_origin(&self) -> Self::Owned { | |
| 332 | Pair(self.0.similar_origin(), self.1.similar_origin()) | |
| 333 | } | |
| 334 | ||
| 335 | // #[inline] | |
| 336 | // fn into_owned(self) -> Self::Owned { | |
| 337 | // Pair(self.0.into_owned(), self.1.into_owned()) | |
| 338 | // } | |
| 339 | } | |
| 340 | ||
|
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
|
341 | 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
|
342 | where |
| 94 | 343 | U: Space, |
| 344 | 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
|
345 | 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
|
346 | B: AXPY<V, Field = F>, |
| 94 | 347 | F: Num, |
| 151 | 348 | // Self: MulAssign<F> + DivAssign<F>, |
| 349 | // Pair<A, B>: MulAssign<F> + DivAssign<F>, | |
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
350 | { |
| 94 | 351 | 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
|
352 | 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
|
353 | self.0.axpy(α, u, β); |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
354 | self.1.axpy(α, v, β); |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
355 | }) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
356 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
357 | |
| 94 | 358 | 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
|
359 | 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
|
360 | 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
|
361 | 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
|
362 | }) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
363 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
364 | |
| 94 | 365 | 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
|
366 | 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
|
367 | 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
|
368 | 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
|
369 | }) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
370 | } |
|
62
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
371 | |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
372 | /// Set self to zero. |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
373 | fn set_zero(&mut self) { |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
374 | self.0.set_zero(); |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
375 | self.1.set_zero(); |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
376 | } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
377 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
378 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
379 | /// [`Decomposition`] for working with [`Pair`]s. |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
380 | #[derive(Copy, Clone, Debug)] |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
381 | pub struct PairDecomposition<D, Q>(D, Q); |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
382 | |
| 94 | 383 | impl<A: Space, B: Space> Space for Pair<A, B> { |
| 151 | 384 | type OwnedSpace = Pair<A::OwnedSpace, B::OwnedSpace>; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
385 | type Decomp = PairDecomposition<A::Decomp, B::Decomp>; |
|
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 | |
| 94 | 388 | 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
|
389 | where |
| 94 | 390 | A: Space, |
| 391 | B: Space, | |
| 392 | D: Decomposition<A>, | |
| 393 | Q: Decomposition<B>, | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
394 | { |
| 155 | 395 | type OwnedInstance = Pair<D::OwnedInstance, Q::OwnedInstance>; |
| 396 | ||
| 94 | 397 | type Decomposition<'b> |
| 398 | = Pair<D::Decomposition<'b>, Q::Decomposition<'b>> | |
| 399 | where | |
| 400 | Pair<A, B>: 'b; | |
| 401 | type Reference<'b> | |
| 402 | = Pair<D::Reference<'b>, Q::Reference<'b>> | |
| 403 | where | |
| 404 | Pair<A, B>: 'b; | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
405 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
406 | #[inline] |
| 94 | 407 | 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
|
408 | Pair(D::lift(u), Q::lift(v)) |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
409 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
410 | } |
|
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 | 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
|
413 | where |
| 94 | 414 | A: Space, |
| 415 | B: Space, | |
| 416 | D: Decomposition<A>, | |
| 417 | Q: Decomposition<B>, | |
| 418 | U: Instance<A, D>, | |
| 419 | V: Instance<B, Q>, | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
420 | { |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
421 | fn eval_decompose<'b, R>( |
| 94 | 422 | self, |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
423 | 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
|
424 | ) -> R |
| 94 | 425 | where |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
426 | Pair<A, B>: 'b, |
| 94 | 427 | Self: 'b, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
428 | { |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
429 | self.0 |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
430 | .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
|
431 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
432 | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
433 | 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
|
434 | &'b self, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
435 | 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
|
436 | ) -> R |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
437 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
438 | Pair<A, B>: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
439 | Self: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
440 | { |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
441 | self.0 |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
442 | .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
|
443 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
444 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
445 | #[inline] |
| 155 | 446 | fn cow<'b>(self) -> MyCow<'b, Pair<D::OwnedInstance, Q::OwnedInstance>> |
| 94 | 447 | where |
| 448 | Self: 'b, | |
| 449 | { | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
450 | 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
|
451 | } |
|
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
452 | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
453 | #[inline] |
| 155 | 454 | fn own(self) -> Pair<D::OwnedInstance, Q::OwnedInstance> { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
455 | Pair(self.0.own(), self.1.own()) |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
456 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
457 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
458 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
459 | 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
|
460 | where |
| 94 | 461 | A: Space, |
| 462 | B: Space, | |
| 463 | D: Decomposition<A>, | |
| 464 | Q: Decomposition<B>, | |
| 465 | U: Instance<A, D>, | |
| 466 | V: Instance<B, Q>, | |
| 467 | &'a U: Instance<A, D>, | |
| 468 | &'a V: Instance<B, Q>, | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
469 | { |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
470 | fn eval_decompose<'b, R>( |
| 94 | 471 | self, |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
472 | 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
|
473 | ) -> R |
| 94 | 474 | where |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
475 | Pair<A, B>: 'b, |
| 94 | 476 | Self: 'b, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
477 | { |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
478 | 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
|
479 | self.1 |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
480 | .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
|
481 | }) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
482 | } |
|
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 | 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
|
485 | &'b self, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
486 | 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
|
487 | ) -> R |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
488 | where |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
489 | Pair<A, B>: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
490 | Self: 'b, |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
491 | { |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
492 | self.0 |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
493 | .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
|
494 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
495 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
496 | #[inline] |
| 155 | 497 | fn cow<'b>(self) -> MyCow<'b, Pair<D::OwnedInstance, Q::OwnedInstance>> |
| 94 | 498 | where |
| 499 | Self: 'b, | |
| 500 | { | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
501 | MyCow::Owned(self.own()) |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
502 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
503 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
504 | #[inline] |
| 155 | 505 | fn own(self) -> Pair<D::OwnedInstance, Q::OwnedInstance> { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
506 | let Pair(ref u, ref v) = self; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
507 | Pair(u.own(), v.own()) |
|
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
508 | } |
|
59
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 | |
| 94 | 511 | 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
|
512 | where |
| 94 | 513 | A: Space, |
| 514 | B: Space, | |
| 515 | D: DecompositionMut<A>, | |
| 516 | Q: DecompositionMut<B>, | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
517 | { |
| 94 | 518 | type ReferenceMut<'b> |
| 519 | = Pair<D::ReferenceMut<'b>, Q::ReferenceMut<'b>> | |
| 520 | where | |
| 521 | Pair<A, B>: 'b; | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
522 | } |
|
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 | 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
|
525 | where |
| 94 | 526 | A: Space, |
| 527 | B: Space, | |
| 528 | D: DecompositionMut<A>, | |
| 529 | Q: DecompositionMut<B>, | |
| 530 | U: InstanceMut<A, D>, | |
| 531 | V: InstanceMut<B, Q>, | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
532 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
533 | #[inline] |
| 94 | 534 | fn ref_instance_mut( |
| 535 | &mut self, | |
| 536 | ) -> <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
|
537 | 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
|
538 | } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
539 | } |
|
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 | 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
|
542 | where |
| 94 | 543 | A: Space, |
| 544 | B: Space, | |
| 545 | D: DecompositionMut<A>, | |
| 546 | Q: DecompositionMut<B>, | |
| 547 | U: InstanceMut<A, D>, | |
| 548 | V: InstanceMut<B, Q>, | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
549 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
550 | #[inline] |
| 94 | 551 | fn ref_instance_mut( |
| 552 | &mut self, | |
| 553 | ) -> <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
|
554 | 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
|
555 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
556 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
557 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
121
diff
changeset
|
558 | 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
|
559 | where |
| 94 | 560 | F: Num, |
| 561 | ExpA: NormExponent, | |
| 562 | ExpB: NormExponent, | |
| 563 | ExpJ: NormExponent, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
121
diff
changeset
|
564 | A: Norm<ExpA, F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
121
diff
changeset
|
565 | B: Norm<ExpB, F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
121
diff
changeset
|
566 | Loc<2, F>: Norm<ExpJ, F>, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
567 | { |
| 94 | 568 | 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
|
569 | 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
|
570 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
571 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
572 | |
| 94 | 573 | 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
|
574 | where |
| 94 | 575 | A: Normed<F>, |
| 576 | B: Normed<F>, | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
577 | { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
578 | type NormExp = PairNorm<A::NormExp, B::NormExp, L2>; |
|
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 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
581 | fn norm_exponent(&self) -> Self::NormExp { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
582 | 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
|
583 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
584 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
585 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
586 | fn is_zero(&self) -> bool { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
587 | self.0.is_zero() && self.1.is_zero() |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
588 | } |
|
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 | |
| 94 | 591 | 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
|
592 | where |
| 94 | 593 | A: HasDual<F>, |
| 594 | B: HasDual<F>, | |
|
60
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 | type DualSpace = Pair<A::DualSpace, B::DualSpace>; |
| 138 | 597 | |
| 150 | 598 | fn dual_origin(&self) -> <Self::DualSpace as VectorSpace>::Owned { |
| 138 | 599 | Pair(self.0.dual_origin(), self.1.dual_origin()) |
| 600 | } | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
601 | } |
| 144 | 602 | |
| 603 | #[cfg(feature = "pyo3")] | |
| 604 | mod python { | |
| 605 | use super::Pair; | |
| 606 | use pyo3::conversion::FromPyObject; | |
| 607 | use pyo3::types::{PyAny, PyTuple}; | |
| 608 | use pyo3::{Bound, IntoPyObject, PyErr, PyResult, Python}; | |
| 609 | ||
| 610 | impl<'py, A, B> IntoPyObject<'py> for Pair<A, B> | |
| 611 | where | |
| 612 | A: IntoPyObject<'py>, | |
| 613 | B: IntoPyObject<'py>, | |
| 614 | { | |
| 615 | type Target = PyTuple; | |
| 616 | type Error = PyErr; | |
| 617 | type Output = Bound<'py, Self::Target>; | |
| 618 | ||
| 619 | fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> { | |
| 620 | (self.0, self.1).into_pyobject(py) | |
| 621 | } | |
| 622 | } | |
| 623 | ||
| 624 | impl<'a, 'py, A, B> IntoPyObject<'py> for &'a mut Pair<A, B> | |
| 625 | where | |
| 626 | &'a mut A: IntoPyObject<'py>, | |
| 627 | &'a mut B: IntoPyObject<'py>, | |
| 628 | { | |
| 629 | type Target = PyTuple; | |
| 630 | type Error = PyErr; | |
| 631 | type Output = Bound<'py, Self::Target>; | |
| 632 | ||
| 633 | fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> { | |
| 634 | (&mut self.0, &mut self.1).into_pyobject(py) | |
| 635 | } | |
| 636 | } | |
| 637 | ||
| 638 | impl<'a, 'py, A, B> IntoPyObject<'py> for &'a Pair<A, B> | |
| 639 | where | |
| 640 | &'a A: IntoPyObject<'py>, | |
| 641 | &'a B: IntoPyObject<'py>, | |
| 642 | { | |
| 643 | type Target = PyTuple; | |
| 644 | type Error = PyErr; | |
| 645 | type Output = Bound<'py, Self::Target>; | |
| 646 | ||
| 647 | fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> { | |
| 648 | (&self.0, &self.1).into_pyobject(py) | |
| 649 | } | |
| 650 | } | |
| 651 | ||
| 652 | impl<'py, A, B> FromPyObject<'py> for Pair<A, B> | |
| 653 | where | |
| 654 | A: Clone + FromPyObject<'py>, | |
| 655 | B: Clone + FromPyObject<'py>, | |
| 656 | { | |
| 657 | fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> { | |
| 658 | FromPyObject::extract_bound(ob).map(|(a, b)| Pair(a, b)) | |
| 659 | } | |
| 660 | } | |
| 661 | } |