Tue, 31 Dec 2024 23:49:09 -0500
simplify
0 | 1 | /*! |
2 | Abstract linear operators. | |
3 | */ | |
4 | ||
5 | use numeric_literals::replace_float_literals; | |
6 | use std::marker::PhantomData; | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
7 | use serde::Serialize; |
0 | 8 | use crate::types::*; |
61 | 9 | pub use crate::mapping::{Mapping, Space, Composition}; |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
10 | use crate::direct_product::Pair; |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
11 | use crate::instance::Instance; |
61 | 12 | use crate::norms::{NormExponent, PairNorm, L1, L2, Linfinity, Norm}; |
0 | 13 | |
14 | /// Trait for linear operators on `X`. | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
15 | pub trait Linear<X : Space> : Mapping<X> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
16 | { } |
0 | 17 | |
18 | /// Efficient in-place summation. | |
19 | #[replace_float_literals(F::cast_from(literal))] | |
62
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
20 | pub trait AXPY<F, X = Self> : Space + std::ops::MulAssign<F> |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
21 | where |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
22 | F : Num, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
23 | X : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
24 | { |
82 | 25 | /// Computes `βy + αx`, where `y` is `Self`. |
85 | 26 | fn add_mul<I : Instance<X>>(self, α : F, x : I, β : F) -> X; |
82 | 27 | |
0 | 28 | /// Computes `y = βy + αx`, where `y` is `Self`. |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
29 | fn axpy<I : Instance<X>>(&mut self, α : F, x : I, β : F); |
0 | 30 | |
31 | /// Copies `x` to `self`. | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
32 | fn copy_from<I : Instance<X>>(&mut self, x : I) { |
0 | 33 | self.axpy(1.0, x, 0.0) |
34 | } | |
35 | ||
5 | 36 | /// Computes `y = αx`, where `y` is `Self`. |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
37 | fn scale_from<I : Instance<X>>(&mut self, α : F, x : I) { |
0 | 38 | self.axpy(α, x, 0.0) |
39 | } | |
62
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
40 | |
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
41 | /// Return a similar zero as `self`. |
85 | 42 | fn similar_origin(&self) -> X; |
62
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
43 | |
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
44 | /// Set self to zero. |
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
45 | fn set_zero(&mut self); |
0 | 46 | } |
47 | ||
48 | /// Efficient in-place application for [`Linear`] operators. | |
49 | #[replace_float_literals(F::cast_from(literal))] | |
85 | 50 | pub trait GEMV<F : Num, X : Space, Y : AXPY<F> = <Self as Mapping<X>>::Codomain> |
51 | : Linear<X, Codomain=Y> | |
52 | { | |
82 | 53 | |
83
9b0a7475a786
Incomplete sketch of GEMV apply_add_mul
Tuomo Valkonen <tuomov@iki.fi>
parents:
82
diff
changeset
|
54 | /// Computes `αAx + βy`, where `A` is `Self`. |
85 | 55 | fn apply_add_mul<I : Instance<X>>(&self, y : Y, α : F, x : I, β : F) -> Y { |
83
9b0a7475a786
Incomplete sketch of GEMV apply_add_mul
Tuomo Valkonen <tuomov@iki.fi>
parents:
82
diff
changeset
|
56 | y.add_mul(α, self.apply(x), β) |
9b0a7475a786
Incomplete sketch of GEMV apply_add_mul
Tuomo Valkonen <tuomov@iki.fi>
parents:
82
diff
changeset
|
57 | } |
9b0a7475a786
Incomplete sketch of GEMV apply_add_mul
Tuomo Valkonen <tuomov@iki.fi>
parents:
82
diff
changeset
|
58 | |
5 | 59 | /// Computes `y = αAx + βy`, where `A` is `Self`. |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
60 | fn gemv<I : Instance<X>>(&self, y : &mut Y, α : F, x : I, β : F); |
0 | 61 | |
67 | 62 | #[inline] |
5 | 63 | /// Computes `y = Ax`, where `A` is `Self` |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
64 | fn apply_mut<I : Instance<X>>(&self, y : &mut Y, x : I){ |
0 | 65 | self.gemv(y, 1.0, x, 0.0) |
66 | } | |
67 | ||
67 | 68 | #[inline] |
5 | 69 | /// Computes `y += Ax`, where `A` is `Self` |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
70 | fn apply_add<I : Instance<X>>(&self, y : &mut Y, x : I){ |
0 | 71 | self.gemv(y, 1.0, x, 1.0) |
72 | } | |
73 | } | |
74 | ||
75 | ||
76 | /// Bounded linear operators | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
77 | pub trait BoundedLinear<X, XExp, CodExp, F = f64> : Linear<X> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
78 | where |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
79 | F : Num, |
61 | 80 | X : Space + Norm<F, XExp>, |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
81 | XExp : NormExponent, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
82 | CodExp : NormExponent |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
83 | { |
0 | 84 | /// A bound on the operator norm $\|A\|$ for the linear operator $A$=`self`. |
85 | /// This is not expected to be the norm, just any bound on it that can be | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
86 | /// reasonably implemented. The [`NormExponent`] `xexp` indicates the norm |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
87 | /// in `X`, and `codexp` in the codomain. |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
88 | fn opnorm_bound(&self, xexp : XExp, codexp : CodExp) -> F; |
0 | 89 | } |
90 | ||
5 | 91 | // Linear operator application into mutable target. The [`AsRef`] bound |
92 | // is used to guarantee compatibility with `Yʹ` and `Self::Codomain`; | |
93 | // the former is assumed to be e.g. a view into the latter. | |
0 | 94 | |
95 | /*impl<X,Y,T> Fn(&X) -> Y for T where T : Linear<X,Codomain=Y> { | |
96 | fn call(&self, x : &X) -> Y { | |
97 | self.apply(x) | |
98 | } | |
99 | }*/ | |
100 | ||
5 | 101 | /// Trait for forming the adjoint operator of `Self`. |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
102 | pub trait Adjointable<X, Yʹ> : Linear<X> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
103 | where |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
104 | X : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
105 | Yʹ : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
106 | { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
107 | type AdjointCodomain : Space; |
0 | 108 | type Adjoint<'a> : Linear<Yʹ, Codomain=Self::AdjointCodomain> where Self : 'a; |
109 | ||
110 | /// Form the adjoint operator of `self`. | |
111 | fn adjoint(&self) -> Self::Adjoint<'_>; | |
112 | } | |
113 | ||
5 | 114 | /// Trait for forming a preadjoint of an operator. |
115 | /// | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
116 | /// For an operator $A$ this is an operator $A\_\*$ |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
117 | /// such that its adjoint $(A\_\*)^\*=A$. The space `X` is the domain of the `Self` |
0 | 118 | /// operator. The space `Ypre` is the predual of its codomain, and should be the |
119 | /// domain of the adjointed operator. `Self::Preadjoint` should be | |
120 | /// [`Adjointable`]`<'a,Ypre,X>`. | |
65
9327d544ca0b
Reduce preadjointing constraints
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
121 | /// We do not make additional restrictions on `Self::Preadjoint` (in particular, it |
9327d544ca0b
Reduce preadjointing constraints
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
122 | /// does not have to be adjointable) to allow `X` to be a subspace yet the preadjoint |
9327d544ca0b
Reduce preadjointing constraints
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
123 | /// have the full space as the codomain, etc. |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
124 | pub trait Preadjointable<X : Space, Ypre : Space> : Linear<X> { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
125 | type PreadjointCodomain : Space; |
65
9327d544ca0b
Reduce preadjointing constraints
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
126 | type Preadjoint<'a> : Linear< |
9327d544ca0b
Reduce preadjointing constraints
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
127 | Ypre, Codomain=Self::PreadjointCodomain |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
128 | > where Self : 'a; |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
129 | |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
130 | /// Form the adjoint operator of `self`. |
0 | 131 | fn preadjoint(&self) -> Self::Preadjoint<'_>; |
132 | } | |
133 | ||
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
134 | /// Adjointable operators $A: X → Y$ between reflexive spaces $X$ and $Y$. |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
135 | pub trait SimplyAdjointable<X : Space> : Adjointable<X,<Self as Mapping<X>>::Codomain> {} |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
136 | impl<'a,X : Space, T> SimplyAdjointable<X> for T |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
137 | where T : Adjointable<X,<Self as Mapping<X>>::Codomain> {} |
0 | 138 | |
139 | /// The identity operator | |
140 | #[derive(Clone,Copy,Debug,Serialize,Eq,PartialEq)] | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
141 | pub struct IdOp<X> (PhantomData<X>); |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
142 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
143 | impl<X> IdOp<X> { |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
144 | pub fn new() -> IdOp<X> { IdOp(PhantomData) } |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
145 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
146 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
147 | impl<X : Clone + Space> Mapping<X> for IdOp<X> { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
148 | type Codomain = X; |
0 | 149 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
150 | fn apply<I : Instance<X>>(&self, x : I) -> X { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
151 | x.own() |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
152 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
153 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
154 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
155 | impl<X : Clone + Space> Linear<X> for IdOp<X> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
156 | { } |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
157 | |
0 | 158 | #[replace_float_literals(F::cast_from(literal))] |
84 | 159 | impl<F : Num, X> GEMV<F, X, X> for IdOp<X> |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
160 | where |
84 | 161 | X : AXPY<F> + Clone + Space |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
162 | { |
0 | 163 | // Computes `y = αAx + βy`, where `A` is `Self`. |
84 | 164 | fn gemv<I : Instance<X>>(&self, y : &mut X, α : F, x : I, β : F) { |
0 | 165 | y.axpy(α, x, β) |
166 | } | |
167 | ||
84 | 168 | fn apply_mut<I : Instance<X>>(&self, y : &mut X, x : I){ |
0 | 169 | y.copy_from(x); |
170 | } | |
171 | } | |
172 | ||
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
173 | impl<F, X, E> BoundedLinear<X, E, E, F> for IdOp<X> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
174 | where |
61 | 175 | X : Space + Clone + Norm<F, E>, |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
176 | F : Num, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
177 | E : NormExponent |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
178 | { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
179 | fn opnorm_bound(&self, _xexp : E, _codexp : E) -> F { F::ONE } |
0 | 180 | } |
181 | ||
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
182 | impl<X : Clone + Space> Adjointable<X,X> for IdOp<X> { |
0 | 183 | type AdjointCodomain=X; |
184 | type Adjoint<'a> = IdOp<X> where X : 'a; | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
185 | |
0 | 186 | fn adjoint(&self) -> Self::Adjoint<'_> { IdOp::new() } |
187 | } | |
188 | ||
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
189 | impl<X : Clone + Space> Preadjointable<X,X> for IdOp<X> { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
190 | type PreadjointCodomain=X; |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
191 | type Preadjoint<'a> = IdOp<X> where X : 'a; |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
192 | |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
193 | fn preadjoint(&self) -> Self::Preadjoint<'_> { IdOp::new() } |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
194 | } |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
195 | |
61 | 196 | |
66 | 197 | /// The zero operator |
198 | #[derive(Clone,Copy,Debug,Serialize,Eq,PartialEq)] | |
199 | pub struct ZeroOp<'a, X, XD, Y, F> { | |
200 | zero : &'a Y, // TODO: don't pass this in `new`; maybe not even store. | |
201 | dual_or_predual_zero : XD, | |
202 | _phantoms : PhantomData<(X, Y, F)>, | |
203 | } | |
204 | ||
205 | // TODO: Need to make Zero in Instance. | |
206 | ||
207 | impl<'a, F : Num, X : Space, XD, Y : Space + Clone> ZeroOp<'a, X, XD, Y, F> { | |
208 | pub fn new(zero : &'a Y, dual_or_predual_zero : XD) -> Self { | |
209 | ZeroOp{ zero, dual_or_predual_zero, _phantoms : PhantomData } | |
210 | } | |
211 | } | |
212 | ||
213 | impl<'a, F : Num, X : Space, XD, Y : AXPY<F> + Clone> Mapping<X> for ZeroOp<'a, X, XD, Y, F> { | |
214 | type Codomain = Y; | |
215 | ||
216 | fn apply<I : Instance<X>>(&self, _x : I) -> Y { | |
217 | self.zero.clone() | |
218 | } | |
219 | } | |
220 | ||
221 | impl<'a, F : Num, X : Space, XD, Y : AXPY<F> + Clone> Linear<X> for ZeroOp<'a, X, XD, Y, F> | |
222 | { } | |
223 | ||
224 | #[replace_float_literals(F::cast_from(literal))] | |
225 | impl<'a, F, X, XD, Y> GEMV<F, X, Y> for ZeroOp<'a, X, XD, Y, F> | |
226 | where | |
227 | F : Num, | |
228 | Y : AXPY<F, Y> + Clone, | |
229 | X : Space | |
230 | { | |
231 | // Computes `y = αAx + βy`, where `A` is `Self`. | |
232 | fn gemv<I : Instance<X>>(&self, y : &mut Y, _α : F, _x : I, β : F) { | |
233 | *y *= β; | |
234 | } | |
235 | ||
236 | fn apply_mut<I : Instance<X>>(&self, y : &mut Y, _x : I){ | |
237 | y.set_zero(); | |
238 | } | |
239 | } | |
240 | ||
241 | impl<'a, F, X, XD, Y, E1, E2> BoundedLinear<X, E1, E2, F> for ZeroOp<'a, X, XD, Y, F> | |
242 | where | |
243 | X : Space + Norm<F, E1>, | |
244 | Y : AXPY<F> + Clone + Norm<F, E2>, | |
245 | F : Num, | |
246 | E1 : NormExponent, | |
247 | E2 : NormExponent, | |
248 | { | |
249 | fn opnorm_bound(&self, _xexp : E1, _codexp : E2) -> F { F::ZERO } | |
250 | } | |
251 | ||
252 | impl<'a, F : Num, X, XD, Y, Yprime : Space> Adjointable<X, Yprime> for ZeroOp<'a, X, XD, Y, F> | |
253 | where | |
254 | X : Space, | |
255 | Y : AXPY<F> + Clone + 'static, | |
256 | XD : AXPY<F> + Clone + 'static, | |
257 | { | |
258 | type AdjointCodomain = XD; | |
259 | type Adjoint<'b> = ZeroOp<'b, Yprime, (), XD, F> where Self : 'b; | |
260 | // () means not (pre)adjointable. | |
261 | ||
262 | fn adjoint(&self) -> Self::Adjoint<'_> { | |
263 | ZeroOp::new(&self.dual_or_predual_zero, ()) | |
264 | } | |
265 | } | |
266 | ||
267 | impl<'a, F, X, XD, Y, Ypre> Preadjointable<X, Ypre> for ZeroOp<'a, X, XD, Y, F> | |
268 | where | |
269 | F : Num, | |
270 | X : Space, | |
271 | Y : AXPY<F> + Clone, | |
272 | Ypre : Space, | |
273 | XD : AXPY<F> + Clone + 'static, | |
274 | { | |
275 | type PreadjointCodomain = XD; | |
276 | type Preadjoint<'b> = ZeroOp<'b, Ypre, (), XD, F> where Self : 'b; | |
277 | // () means not (pre)adjointable. | |
278 | ||
279 | fn preadjoint(&self) -> Self::Preadjoint<'_> { | |
280 | ZeroOp::new(&self.dual_or_predual_zero, ()) | |
281 | } | |
282 | } | |
283 | ||
61 | 284 | impl<S, T, E, X> Linear<X> for Composition<S, T, E> |
285 | where | |
286 | X : Space, | |
287 | T : Linear<X>, | |
288 | S : Linear<T::Codomain> | |
289 | { } | |
290 | ||
291 | impl<F, S, T, E, X, Y> GEMV<F, X, Y> for Composition<S, T, E> | |
292 | where | |
293 | F : Num, | |
83
9b0a7475a786
Incomplete sketch of GEMV apply_add_mul
Tuomo Valkonen <tuomov@iki.fi>
parents:
82
diff
changeset
|
294 | Y : AXPY<F>, |
61 | 295 | X : Space, |
296 | T : Linear<X>, | |
297 | S : GEMV<F, T::Codomain, Y>, | |
298 | { | |
299 | fn gemv<I : Instance<X>>(&self, y : &mut Y, α : F, x : I, β : F) { | |
300 | self.outer.gemv(y, α, self.inner.apply(x), β) | |
301 | } | |
302 | ||
303 | /// Computes `y = Ax`, where `A` is `Self` | |
304 | fn apply_mut<I : Instance<X>>(&self, y : &mut Y, x : I){ | |
305 | self.outer.apply_mut(y, self.inner.apply(x)) | |
306 | } | |
307 | ||
308 | /// Computes `y += Ax`, where `A` is `Self` | |
309 | fn apply_add<I : Instance<X>>(&self, y : &mut Y, x : I){ | |
310 | self.outer.apply_add(y, self.inner.apply(x)) | |
311 | } | |
312 | } | |
313 | ||
314 | impl<F, S, T, X, Z, Xexp, Yexp, Zexp> BoundedLinear<X, Xexp, Yexp, F> for Composition<S, T, Zexp> | |
315 | where | |
316 | F : Num, | |
317 | X : Space + Norm<F, Xexp>, | |
318 | Z : Space + Norm<F, Zexp>, | |
319 | Xexp : NormExponent, | |
320 | Yexp : NormExponent, | |
321 | Zexp : NormExponent, | |
322 | T : BoundedLinear<X, Xexp, Zexp, F, Codomain=Z>, | |
323 | S : BoundedLinear<Z, Zexp, Yexp, F>, | |
324 | { | |
325 | fn opnorm_bound(&self, xexp : Xexp, yexp : Yexp) -> F { | |
326 | let zexp = self.intermediate_norm_exponent; | |
327 | self.outer.opnorm_bound(zexp, yexp) * self.inner.opnorm_bound(xexp, zexp) | |
328 | } | |
329 | } | |
330 | ||
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
331 | /// “Row operator” $(S, T)$; $(S, T)(x, y)=Sx + Ty$. |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
332 | pub struct RowOp<S, T>(pub S, pub T); |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
333 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
334 | use std::ops::Add; |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
335 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
336 | impl<A, B, S, T> Mapping<Pair<A, B>> for RowOp<S, T> |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
337 | where |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
338 | A : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
339 | B : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
340 | S : Mapping<A>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
341 | T : Mapping<B>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
342 | S::Codomain : Add<T::Codomain>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
343 | <S::Codomain as Add<T::Codomain>>::Output : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
344 | |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
345 | { |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
346 | type Codomain = <S::Codomain as Add<T::Codomain>>::Output; |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
347 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
348 | fn apply<I : Instance<Pair<A, B>>>(&self, x : I) -> Self::Codomain { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
349 | let Pair(a, b) = x.decompose(); |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
350 | self.0.apply(a) + self.1.apply(b) |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
351 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
352 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
353 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
354 | impl<A, B, S, T> Linear<Pair<A, B>> for RowOp<S, T> |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
355 | where |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
356 | A : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
357 | B : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
358 | S : Linear<A>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
359 | T : Linear<B>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
360 | S::Codomain : Add<T::Codomain>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
361 | <S::Codomain as Add<T::Codomain>>::Output : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
362 | { } |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
363 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
364 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
365 | impl<'b, F, S, T, Y, U, V> GEMV<F, Pair<U, V>, Y> for RowOp<S, T> |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
366 | where |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
367 | U : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
368 | V : Space, |
83
9b0a7475a786
Incomplete sketch of GEMV apply_add_mul
Tuomo Valkonen <tuomov@iki.fi>
parents:
82
diff
changeset
|
369 | Y : AXPY<F>, |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
370 | S : GEMV<F, U, Y>, |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
371 | T : GEMV<F, V, Y>, |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
372 | F : Num, |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
373 | Self : Linear<Pair<U, V>, Codomain=Y> |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
374 | { |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
375 | fn gemv<I : Instance<Pair<U, V>>>(&self, y : &mut Y, α : F, x : I, β : F) { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
376 | let Pair(u, v) = x.decompose(); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
377 | self.0.gemv(y, α, u, β); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
378 | self.1.gemv(y, α, v, F::ONE); |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
379 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
380 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
381 | fn apply_mut<I : Instance<Pair<U, V>>>(&self, y : &mut Y, x : I) { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
382 | let Pair(u, v) = x.decompose(); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
383 | self.0.apply_mut(y, u); |
74 | 384 | self.1.apply_add(y, v); |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
385 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
386 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
387 | /// Computes `y += Ax`, where `A` is `Self` |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
388 | fn apply_add<I : Instance<Pair<U, V>>>(&self, y : &mut Y, x : I) { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
389 | let Pair(u, v) = x.decompose(); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
390 | self.0.apply_add(y, u); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
391 | self.1.apply_add(y, v); |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
392 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
393 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
394 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
395 | /// “Column operator” $(S; T)$; $(S; T)x=(Sx, Tx)$. |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
396 | pub struct ColOp<S, T>(pub S, pub T); |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
397 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
398 | impl<A, S, T> Mapping<A> for ColOp<S, T> |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
399 | where |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
400 | A : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
401 | S : Mapping<A>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
402 | T : Mapping<A>, |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
403 | { |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
404 | type Codomain = Pair<S::Codomain, T::Codomain>; |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
405 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
406 | fn apply<I : Instance<A>>(&self, a : I) -> Self::Codomain { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
407 | Pair(self.0.apply(a.ref_instance()), self.1.apply(a)) |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
408 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
409 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
410 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
411 | impl<A, S, T> Linear<A> for ColOp<S, T> |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
412 | where |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
413 | A : Space, |
83
9b0a7475a786
Incomplete sketch of GEMV apply_add_mul
Tuomo Valkonen <tuomov@iki.fi>
parents:
82
diff
changeset
|
414 | S : Linear<A>, |
9b0a7475a786
Incomplete sketch of GEMV apply_add_mul
Tuomo Valkonen <tuomov@iki.fi>
parents:
82
diff
changeset
|
415 | T : Linear<A>, |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
416 | { } |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
417 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
418 | impl<F, S, T, A, B, X> GEMV<F, X, Pair<A, B>> for ColOp<S, T> |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
419 | where |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
420 | X : Space, |
83
9b0a7475a786
Incomplete sketch of GEMV apply_add_mul
Tuomo Valkonen <tuomov@iki.fi>
parents:
82
diff
changeset
|
421 | A : AXPY<F>, |
9b0a7475a786
Incomplete sketch of GEMV apply_add_mul
Tuomo Valkonen <tuomov@iki.fi>
parents:
82
diff
changeset
|
422 | B : AXPY<F>, |
9b0a7475a786
Incomplete sketch of GEMV apply_add_mul
Tuomo Valkonen <tuomov@iki.fi>
parents:
82
diff
changeset
|
423 | Pair<A, B> : AXPY<F>, |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
424 | S : GEMV<F, X, A>, |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
425 | T : GEMV<F, X, B>, |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
426 | F : Num, |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
427 | Self : Linear<X, Codomain=Pair<A, B>> |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
428 | { |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
429 | fn gemv<I : Instance<X>>(&self, y : &mut Pair<A, B>, α : F, x : I, β : F) { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
430 | self.0.gemv(&mut y.0, α, x.ref_instance(), β); |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
431 | self.1.gemv(&mut y.1, α, x, β); |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
432 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
433 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
434 | fn apply_mut<I : Instance<X>>(&self, y : &mut Pair<A, B>, x : I){ |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
435 | self.0.apply_mut(&mut y.0, x.ref_instance()); |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
436 | self.1.apply_mut(&mut y.1, x); |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
437 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
438 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
439 | /// Computes `y += Ax`, where `A` is `Self` |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
440 | fn apply_add<I : Instance<X>>(&self, y : &mut Pair<A, B>, x : I){ |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
441 | self.0.apply_add(&mut y.0, x.ref_instance()); |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
442 | self.1.apply_add(&mut y.1, x); |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
443 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
444 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
445 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
446 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
447 | impl<A, B, Yʹ, S, T> Adjointable<Pair<A,B>, Yʹ> for RowOp<S, T> |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
448 | where |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
449 | A : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
450 | B : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
451 | Yʹ : Space, |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
452 | S : Adjointable<A, Yʹ>, |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
453 | T : Adjointable<B, Yʹ>, |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
454 | Self : Linear<Pair<A, B>>, |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
455 | // for<'a> ColOp<S::Adjoint<'a>, T::Adjoint<'a>> : Linear< |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
456 | // Yʹ, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
457 | // Codomain=Pair<S::AdjointCodomain, T::AdjointCodomain> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
458 | // >, |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
459 | { |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
460 | type AdjointCodomain = Pair<S::AdjointCodomain, T::AdjointCodomain>; |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
461 | type Adjoint<'a> = ColOp<S::Adjoint<'a>, T::Adjoint<'a>> where Self : 'a; |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
462 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
463 | fn adjoint(&self) -> Self::Adjoint<'_> { |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
464 | ColOp(self.0.adjoint(), self.1.adjoint()) |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
465 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
466 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
467 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
468 | impl<A, B, Yʹ, S, T> Preadjointable<Pair<A,B>, Yʹ> for RowOp<S, T> |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
469 | where |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
470 | A : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
471 | B : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
472 | Yʹ : Space, |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
473 | S : Preadjointable<A, Yʹ>, |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
474 | T : Preadjointable<B, Yʹ>, |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
475 | Self : Linear<Pair<A, B>>, |
65
9327d544ca0b
Reduce preadjointing constraints
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
476 | for<'a> ColOp<S::Preadjoint<'a>, T::Preadjoint<'a>> : Linear< |
9327d544ca0b
Reduce preadjointing constraints
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
477 | Yʹ, Codomain=Pair<S::PreadjointCodomain, T::PreadjointCodomain>, |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
478 | >, |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
479 | { |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
480 | type PreadjointCodomain = Pair<S::PreadjointCodomain, T::PreadjointCodomain>; |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
481 | type Preadjoint<'a> = ColOp<S::Preadjoint<'a>, T::Preadjoint<'a>> where Self : 'a; |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
482 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
483 | fn preadjoint(&self) -> Self::Preadjoint<'_> { |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
484 | ColOp(self.0.preadjoint(), self.1.preadjoint()) |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
485 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
486 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
487 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
488 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
489 | impl<A, Xʹ, Yʹ, R, S, T> Adjointable<A,Pair<Xʹ,Yʹ>> for ColOp<S, T> |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
490 | where |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
491 | A : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
492 | Xʹ : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
493 | Yʹ : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
494 | R : Space + ClosedAdd, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
495 | S : Adjointable<A, Xʹ, AdjointCodomain = R>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
496 | T : Adjointable<A, Yʹ, AdjointCodomain = R>, |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
497 | Self : Linear<A>, |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
498 | // for<'a> RowOp<S::Adjoint<'a>, T::Adjoint<'a>> : Linear< |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
499 | // Pair<Xʹ,Yʹ>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
500 | // Codomain=R, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
501 | // >, |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
502 | { |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
503 | type AdjointCodomain = R; |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
504 | type Adjoint<'a> = RowOp<S::Adjoint<'a>, T::Adjoint<'a>> where Self : 'a; |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
505 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
506 | fn adjoint(&self) -> Self::Adjoint<'_> { |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
507 | RowOp(self.0.adjoint(), self.1.adjoint()) |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
508 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
509 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
510 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
511 | impl<A, Xʹ, Yʹ, R, S, T> Preadjointable<A,Pair<Xʹ,Yʹ>> for ColOp<S, T> |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
512 | where |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
513 | A : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
514 | Xʹ : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
515 | Yʹ : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
516 | R : Space + ClosedAdd, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
517 | S : Preadjointable<A, Xʹ, PreadjointCodomain = R>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
518 | T : Preadjointable<A, Yʹ, PreadjointCodomain = R>, |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
519 | Self : Linear<A>, |
65
9327d544ca0b
Reduce preadjointing constraints
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
520 | for<'a> RowOp<S::Preadjoint<'a>, T::Preadjoint<'a>> : Linear< |
9327d544ca0b
Reduce preadjointing constraints
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
521 | Pair<Xʹ,Yʹ>, Codomain = R, |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
522 | >, |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
523 | { |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
524 | type PreadjointCodomain = R; |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
525 | type Preadjoint<'a> = RowOp<S::Preadjoint<'a>, T::Preadjoint<'a>> where Self : 'a; |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
526 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
527 | fn preadjoint(&self) -> Self::Preadjoint<'_> { |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
528 | RowOp(self.0.preadjoint(), self.1.preadjoint()) |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
529 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
530 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
531 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
532 | /// Diagonal operator |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
533 | pub struct DiagOp<S, T>(pub S, pub T); |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
534 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
535 | impl<A, B, S, T> Mapping<Pair<A, B>> for DiagOp<S, T> |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
536 | where |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
537 | A : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
538 | B : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
539 | S : Mapping<A>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
540 | T : Mapping<B>, |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
541 | { |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
542 | type Codomain = Pair<S::Codomain, T::Codomain>; |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
543 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
544 | fn apply<I : Instance<Pair<A, B>>>(&self, x : I) -> Self::Codomain { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
545 | let Pair(a, b) = x.decompose(); |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
546 | Pair(self.0.apply(a), self.1.apply(b)) |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
547 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
548 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
549 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
550 | impl<A, B, S, T> Linear<Pair<A, B>> for DiagOp<S, T> |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
551 | where |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
552 | A : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
553 | B : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
554 | S : Linear<A>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
555 | T : Linear<B>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
556 | { } |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
557 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
558 | impl<F, S, T, A, B, U, V> GEMV<F, Pair<U, V>, Pair<A, B>> for DiagOp<S, T> |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
559 | where |
83
9b0a7475a786
Incomplete sketch of GEMV apply_add_mul
Tuomo Valkonen <tuomov@iki.fi>
parents:
82
diff
changeset
|
560 | A : AXPY<F>, |
9b0a7475a786
Incomplete sketch of GEMV apply_add_mul
Tuomo Valkonen <tuomov@iki.fi>
parents:
82
diff
changeset
|
561 | B : AXPY<F>, |
9b0a7475a786
Incomplete sketch of GEMV apply_add_mul
Tuomo Valkonen <tuomov@iki.fi>
parents:
82
diff
changeset
|
562 | Pair<A, B> : AXPY<F>, |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
563 | U : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
564 | V : Space, |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
565 | S : GEMV<F, U, A>, |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
566 | T : GEMV<F, V, B>, |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
567 | F : Num, |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
568 | Self : Linear<Pair<U, V>, Codomain=Pair<A, B>>, |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
569 | { |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
570 | fn gemv<I : Instance<Pair<U, V>>>(&self, y : &mut Pair<A, B>, α : F, x : I, β : F) { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
571 | let Pair(u, v) = x.decompose(); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
572 | self.0.gemv(&mut y.0, α, u, β); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
573 | self.1.gemv(&mut y.1, α, v, β); |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
574 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
575 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
576 | fn apply_mut<I : Instance<Pair<U, V>>>(&self, y : &mut Pair<A, B>, x : I){ |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
577 | let Pair(u, v) = x.decompose(); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
578 | self.0.apply_mut(&mut y.0, u); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
579 | self.1.apply_mut(&mut y.1, v); |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
580 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
581 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
582 | /// Computes `y += Ax`, where `A` is `Self` |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
583 | fn apply_add<I : Instance<Pair<U, V>>>(&self, y : &mut Pair<A, B>, x : I){ |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
584 | let Pair(u, v) = x.decompose(); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
585 | self.0.apply_add(&mut y.0, u); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
586 | self.1.apply_add(&mut y.1, v); |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
587 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
588 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
589 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
590 | impl<A, B, Xʹ, Yʹ, R, S, T> Adjointable<Pair<A,B>, Pair<Xʹ,Yʹ>> for DiagOp<S, T> |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
591 | where |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
592 | A : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
593 | B : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
594 | Xʹ: Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
595 | Yʹ : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
596 | R : Space, |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
597 | S : Adjointable<A, Xʹ>, |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
598 | T : Adjointable<B, Yʹ>, |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
599 | Self : Linear<Pair<A, B>>, |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
600 | for<'a> DiagOp<S::Adjoint<'a>, T::Adjoint<'a>> : Linear< |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
601 | Pair<Xʹ,Yʹ>, Codomain=R, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
602 | >, |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
603 | { |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
604 | type AdjointCodomain = R; |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
605 | type Adjoint<'a> = DiagOp<S::Adjoint<'a>, T::Adjoint<'a>> where Self : 'a; |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
606 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
607 | fn adjoint(&self) -> Self::Adjoint<'_> { |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
608 | DiagOp(self.0.adjoint(), self.1.adjoint()) |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
609 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
610 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
611 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
612 | impl<A, B, Xʹ, Yʹ, R, S, T> Preadjointable<Pair<A,B>, Pair<Xʹ,Yʹ>> for DiagOp<S, T> |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
613 | where |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
614 | A : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
615 | B : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
616 | Xʹ: Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
617 | Yʹ : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
618 | R : Space, |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
619 | S : Preadjointable<A, Xʹ>, |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
620 | T : Preadjointable<B, Yʹ>, |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
621 | Self : Linear<Pair<A, B>>, |
65
9327d544ca0b
Reduce preadjointing constraints
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
622 | for<'a> DiagOp<S::Preadjoint<'a>, T::Preadjoint<'a>> : Linear< |
9327d544ca0b
Reduce preadjointing constraints
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
623 | Pair<Xʹ,Yʹ>, Codomain=R, |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
624 | >, |
57
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
625 | { |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
626 | type PreadjointCodomain = R; |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
627 | type Preadjoint<'a> = DiagOp<S::Preadjoint<'a>, T::Preadjoint<'a>> where Self : 'a; |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
628 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
629 | fn preadjoint(&self) -> Self::Preadjoint<'_> { |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
630 | DiagOp(self.0.preadjoint(), self.1.preadjoint()) |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
631 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
632 | } |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
633 | |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
634 | /// Block operator |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
635 | pub type BlockOp<S11, S12, S21, S22> = ColOp<RowOp<S11, S12>, RowOp<S21, S22>>; |
1b3b1687b9ed
Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
636 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
637 | |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
638 | macro_rules! pairnorm { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
639 | ($expj:ty) => { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
640 | impl<F, A, B, S, T, ExpA, ExpB, ExpR> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
641 | BoundedLinear<Pair<A, B>, PairNorm<ExpA, ExpB, $expj>, ExpR, F> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
642 | for RowOp<S, T> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
643 | where |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
644 | F : Float, |
61 | 645 | A : Space + Norm<F, ExpA>, |
646 | B : Space + Norm<F, ExpB>, | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
647 | S : BoundedLinear<A, ExpA, ExpR, F>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
648 | T : BoundedLinear<B, ExpB, ExpR, F>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
649 | S::Codomain : Add<T::Codomain>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
650 | <S::Codomain as Add<T::Codomain>>::Output : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
651 | ExpA : NormExponent, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
652 | ExpB : NormExponent, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
653 | ExpR : NormExponent, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
654 | { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
655 | fn opnorm_bound( |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
656 | &self, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
657 | PairNorm(expa, expb, _) : PairNorm<ExpA, ExpB, $expj>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
658 | expr : ExpR |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
659 | ) -> F { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
660 | // An application of the triangle inequality bounds the norm by the maximum |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
661 | // of the individual norms. A simple observation shows this to be exact. |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
662 | let na = self.0.opnorm_bound(expa, expr); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
663 | let nb = self.1.opnorm_bound(expb, expr); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
664 | na.max(nb) |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
665 | } |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
666 | } |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
667 | |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
668 | impl<F, A, S, T, ExpA, ExpS, ExpT> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
669 | BoundedLinear<A, ExpA, PairNorm<ExpS, ExpT, $expj>, F> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
670 | for ColOp<S, T> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
671 | where |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
672 | F : Float, |
61 | 673 | A : Space + Norm<F, ExpA>, |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
674 | S : BoundedLinear<A, ExpA, ExpS, F>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
675 | T : BoundedLinear<A, ExpA, ExpT, F>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
676 | ExpA : NormExponent, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
677 | ExpS : NormExponent, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
678 | ExpT : NormExponent, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
679 | { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
680 | fn opnorm_bound( |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
681 | &self, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
682 | expa : ExpA, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
683 | PairNorm(exps, expt, _) : PairNorm<ExpS, ExpT, $expj> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
684 | ) -> F { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
685 | // This is based on the rule for RowOp and ‖A^*‖ = ‖A‖, hence, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
686 | // for A=[S; T], ‖A‖=‖[S^*, T^*]‖ ≤ max{‖S^*‖, ‖T^*‖} = max{‖S‖, ‖T‖} |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
687 | let ns = self.0.opnorm_bound(expa, exps); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
688 | let nt = self.1.opnorm_bound(expa, expt); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
689 | ns.max(nt) |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
690 | } |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
691 | } |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
692 | } |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
693 | } |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
694 | |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
695 | pairnorm!(L1); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
696 | pairnorm!(L2); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
697 | pairnorm!(Linfinity); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
57
diff
changeset
|
698 |