Mon, 24 Oct 2022 10:52:19 +0300
Added type for numerical errors
0 | 1 | /*! |
2 | Abstract linear operators. | |
3 | */ | |
4 | ||
5 | use numeric_literals::replace_float_literals; | |
6 | use std::marker::PhantomData; | |
7 | use crate::types::*; | |
8 | use serde::Serialize; | |
9 | ||
10 | /// Trait for linear operators on `X`. | |
11 | pub trait Linear<X> { | |
12 | /// The range space of the operator. | |
13 | type Codomain; | |
14 | /// Apply the linear operator to `x`. | |
15 | fn apply(&self, x : &X) -> Self::Codomain; | |
16 | } | |
17 | ||
18 | /// Efficient in-place summation. | |
19 | #[replace_float_literals(F::cast_from(literal))] | |
20 | pub trait AXPY<F : Num, X = Self> { | |
21 | /// Computes `y = βy + αx`, where `y` is `Self`. | |
22 | fn axpy(&mut self, α : F, x : &X, β : F); | |
23 | ||
24 | /// Copies `x` to `self`. | |
25 | fn copy_from(&mut self, x : &X) { | |
26 | self.axpy(1.0, x, 0.0) | |
27 | } | |
28 | ||
29 | /// Computes `y = αx`, where `y` is `Self`. | |
30 | fn scale_from(&mut self, α : F, x : &X) { | |
31 | self.axpy(α, x, 0.0) | |
32 | } | |
33 | } | |
34 | ||
35 | /// Efficient in-place application for [`Linear`] operators. | |
36 | #[replace_float_literals(F::cast_from(literal))] | |
37 | pub trait GEMV<F : Num, X, Y = <Self as Linear<X>>::Codomain> : Linear<X> { | |
38 | // Computes `y = αAx + βy`, where `A` is `Self`. | |
39 | fn gemv(&self, y : &mut Y, α : F, x : &X, β : F); | |
40 | ||
41 | // Computes `y = Ax`, where `A` is `Self` | |
42 | fn apply_mut(&self, y : &mut Y, x : &X){ | |
43 | self.gemv(y, 1.0, x, 0.0) | |
44 | } | |
45 | ||
46 | // Computes `y += Ax`, where `A` is `Self` | |
47 | fn apply_add(&self, y : &mut Y, x : &X){ | |
48 | self.gemv(y, 1.0, x, 1.0) | |
49 | } | |
50 | } | |
51 | ||
52 | ||
53 | /// Bounded linear operators | |
54 | pub trait BoundedLinear<X> : Linear<X> { | |
55 | type FloatType : Float; | |
56 | /// A bound on the operator norm $\|A\|$ for the linear operator $A$=`self`. | |
57 | /// This is not expected to be the norm, just any bound on it that can be | |
58 | /// reasonably implemented. | |
59 | fn opnorm_bound(&self) -> Self::FloatType; | |
60 | } | |
61 | ||
62 | /// Linear operator application into mutable target. The [`AsRef`] bound | |
63 | /// is used to guarantee compatibility with `Yʹ` and `Self::Codomain`; | |
64 | /// the former is assumed to be e.g. a view into the latter. | |
65 | ||
66 | /*impl<X,Y,T> Fn(&X) -> Y for T where T : Linear<X,Codomain=Y> { | |
67 | fn call(&self, x : &X) -> Y { | |
68 | self.apply(x) | |
69 | } | |
70 | }*/ | |
71 | ||
72 | /// Trait for forming the adjoint operator of an operator $A$=`Self`. | |
73 | pub trait Adjointable<X,Yʹ> : Linear<X> { | |
74 | type AdjointCodomain; | |
75 | type Adjoint<'a> : Linear<Yʹ, Codomain=Self::AdjointCodomain> where Self : 'a; | |
76 | ||
77 | /// Form the adjoint operator of `self`. | |
78 | fn adjoint(&self) -> Self::Adjoint<'_>; | |
79 | ||
80 | /*fn adjoint_apply(&self, y : &Yʹ) -> Self::AdjointCodomain { | |
81 | self.adjoint().apply(y) | |
82 | }*/ | |
83 | } | |
84 | ||
85 | /// Trait for forming a preadjoint of an operator $A$, i.e., an operator $A_*$ | |
86 | /// such that its adjoint $(A_*)^*=A$. The space `X` is the domain of the `Self` | |
87 | /// operator. The space `Ypre` is the predual of its codomain, and should be the | |
88 | /// domain of the adjointed operator. `Self::Preadjoint` should be | |
89 | /// [`Adjointable`]`<'a,Ypre,X>`. | |
90 | pub trait Preadjointable<X,Ypre> : Linear<X> { | |
91 | type PreadjointCodomain; | |
92 | type Preadjoint<'a> : Adjointable<Ypre, X, Codomain=Self::PreadjointCodomain> where Self : 'a; | |
93 | ||
94 | /// Form the preadjoint operator of `self`. | |
95 | fn preadjoint(&self) -> Self::Preadjoint<'_>; | |
96 | } | |
97 | ||
98 | /// Adjointable operators $A: X → Y$ on between reflexibe spaces $X$ and $Y$. | |
99 | pub trait SimplyAdjointable<X> : Adjointable<X,<Self as Linear<X>>::Codomain> {} | |
100 | impl<'a,X,T> SimplyAdjointable<X> for T where T : Adjointable<X,<Self as Linear<X>>::Codomain> {} | |
101 | ||
102 | /// The identity operator | |
103 | #[derive(Clone,Copy,Debug,Serialize,Eq,PartialEq)] | |
104 | pub struct IdOp<X : Clone> (PhantomData<X>); | |
105 | ||
106 | impl<X> IdOp<X> where X : Clone { | |
107 | fn new() -> IdOp<X> { IdOp(PhantomData) } | |
108 | } | |
109 | ||
110 | impl<X> Linear<X> for IdOp<X> where X : Clone { | |
111 | type Codomain = X; | |
112 | fn apply(&self, x : &X) -> X { | |
113 | x.clone() | |
114 | } | |
115 | } | |
116 | ||
117 | #[replace_float_literals(F::cast_from(literal))] | |
118 | impl<F : Num, X, Y> GEMV<F, X, Y> for IdOp<X> where Y : AXPY<F, X>, X : Clone { | |
119 | // Computes `y = αAx + βy`, where `A` is `Self`. | |
120 | fn gemv(&self, y : &mut Y, α : F, x : &X, β : F) { | |
121 | y.axpy(α, x, β) | |
122 | } | |
123 | ||
124 | fn apply_mut(&self, y : &mut Y, x : &X){ | |
125 | y.copy_from(x); | |
126 | } | |
127 | } | |
128 | ||
129 | impl<X> BoundedLinear<X> for IdOp<X> where X : Clone { | |
130 | type FloatType = float; | |
131 | fn opnorm_bound(&self) -> float { 1.0 } | |
132 | } | |
133 | ||
134 | impl<X> Adjointable<X,X> for IdOp<X> where X : Clone { | |
135 | type AdjointCodomain=X; | |
136 | type Adjoint<'a> = IdOp<X> where X : 'a; | |
137 | fn adjoint(&self) -> Self::Adjoint<'_> { IdOp::new() } | |
138 | } | |
139 |