Tue, 25 Oct 2022 23:05:40 +0300
Added NormExponent trait for exponents of norms
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 | ||
5 | 29 | /// Computes `y = αx`, where `y` is `Self`. |
0 | 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> { | |
5 | 38 | /// Computes `y = αAx + βy`, where `A` is `Self`. |
0 | 39 | fn gemv(&self, y : &mut Y, α : F, x : &X, β : F); |
40 | ||
5 | 41 | /// Computes `y = Ax`, where `A` is `Self` |
0 | 42 | fn apply_mut(&self, y : &mut Y, x : &X){ |
43 | self.gemv(y, 1.0, x, 0.0) | |
44 | } | |
45 | ||
5 | 46 | /// Computes `y += Ax`, where `A` is `Self` |
0 | 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 | ||
5 | 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. | |
0 | 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 | ||
5 | 72 | /// Trait for forming the adjoint operator of `Self`. |
0 | 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 | ||
5 | 85 | /// Trait for forming a preadjoint of an operator. |
86 | /// | |
87 | /// For an operator $A$ this is an operator $A_*$ | |
0 | 88 | /// such that its adjoint $(A_*)^*=A$. The space `X` is the domain of the `Self` |
89 | /// operator. The space `Ypre` is the predual of its codomain, and should be the | |
90 | /// domain of the adjointed operator. `Self::Preadjoint` should be | |
91 | /// [`Adjointable`]`<'a,Ypre,X>`. | |
92 | pub trait Preadjointable<X,Ypre> : Linear<X> { | |
93 | type PreadjointCodomain; | |
94 | type Preadjoint<'a> : Adjointable<Ypre, X, Codomain=Self::PreadjointCodomain> where Self : 'a; | |
95 | ||
96 | /// Form the preadjoint operator of `self`. | |
97 | fn preadjoint(&self) -> Self::Preadjoint<'_>; | |
98 | } | |
99 | ||
5 | 100 | /// Adjointable operators $A: X → Y$ on between reflexive spaces $X$ and $Y$. |
0 | 101 | pub trait SimplyAdjointable<X> : Adjointable<X,<Self as Linear<X>>::Codomain> {} |
102 | impl<'a,X,T> SimplyAdjointable<X> for T where T : Adjointable<X,<Self as Linear<X>>::Codomain> {} | |
103 | ||
104 | /// The identity operator | |
105 | #[derive(Clone,Copy,Debug,Serialize,Eq,PartialEq)] | |
106 | pub struct IdOp<X : Clone> (PhantomData<X>); | |
107 | ||
108 | impl<X> IdOp<X> where X : Clone { | |
109 | fn new() -> IdOp<X> { IdOp(PhantomData) } | |
110 | } | |
111 | ||
112 | impl<X> Linear<X> for IdOp<X> where X : Clone { | |
113 | type Codomain = X; | |
114 | fn apply(&self, x : &X) -> X { | |
115 | x.clone() | |
116 | } | |
117 | } | |
118 | ||
119 | #[replace_float_literals(F::cast_from(literal))] | |
120 | impl<F : Num, X, Y> GEMV<F, X, Y> for IdOp<X> where Y : AXPY<F, X>, X : Clone { | |
121 | // Computes `y = αAx + βy`, where `A` is `Self`. | |
122 | fn gemv(&self, y : &mut Y, α : F, x : &X, β : F) { | |
123 | y.axpy(α, x, β) | |
124 | } | |
125 | ||
126 | fn apply_mut(&self, y : &mut Y, x : &X){ | |
127 | y.copy_from(x); | |
128 | } | |
129 | } | |
130 | ||
131 | impl<X> BoundedLinear<X> for IdOp<X> where X : Clone { | |
132 | type FloatType = float; | |
133 | fn opnorm_bound(&self) -> float { 1.0 } | |
134 | } | |
135 | ||
136 | impl<X> Adjointable<X,X> for IdOp<X> where X : Clone { | |
137 | type AdjointCodomain=X; | |
138 | type Adjoint<'a> = IdOp<X> where X : 'a; | |
139 | fn adjoint(&self) -> Self::Adjoint<'_> { IdOp::new() } | |
140 | } | |
141 |