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