Tue, 06 Dec 2022 08:58:49 +0200
README improvements
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; | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
9 | pub use crate::mapping::Apply; |
0 | 10 | |
11 | /// Trait for linear operators on `X`. | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
12 | pub trait Linear<X> : Apply<X, Output=Self::Codomain> |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
13 | + for<'a> Apply<&'a X, Output=Self::Codomain> { |
0 | 14 | type Codomain; |
15 | } | |
16 | ||
17 | /// Efficient in-place summation. | |
18 | #[replace_float_literals(F::cast_from(literal))] | |
19 | pub trait AXPY<F : Num, X = Self> { | |
20 | /// Computes `y = βy + αx`, where `y` is `Self`. | |
21 | fn axpy(&mut self, α : F, x : &X, β : F); | |
22 | ||
23 | /// Copies `x` to `self`. | |
24 | fn copy_from(&mut self, x : &X) { | |
25 | self.axpy(1.0, x, 0.0) | |
26 | } | |
27 | ||
5 | 28 | /// Computes `y = αx`, where `y` is `Self`. |
0 | 29 | fn scale_from(&mut self, α : F, x : &X) { |
30 | self.axpy(α, x, 0.0) | |
31 | } | |
32 | } | |
33 | ||
34 | /// Efficient in-place application for [`Linear`] operators. | |
35 | #[replace_float_literals(F::cast_from(literal))] | |
36 | pub trait GEMV<F : Num, X, Y = <Self as Linear<X>>::Codomain> : Linear<X> { | |
5 | 37 | /// Computes `y = αAx + βy`, where `A` is `Self`. |
0 | 38 | fn gemv(&self, y : &mut Y, α : F, x : &X, β : F); |
39 | ||
5 | 40 | /// Computes `y = Ax`, where `A` is `Self` |
0 | 41 | fn apply_mut(&self, y : &mut Y, x : &X){ |
42 | self.gemv(y, 1.0, x, 0.0) | |
43 | } | |
44 | ||
5 | 45 | /// Computes `y += Ax`, where `A` is `Self` |
0 | 46 | fn apply_add(&self, y : &mut Y, x : &X){ |
47 | self.gemv(y, 1.0, x, 1.0) | |
48 | } | |
49 | } | |
50 | ||
51 | ||
52 | /// Bounded linear operators | |
53 | pub trait BoundedLinear<X> : Linear<X> { | |
54 | type FloatType : Float; | |
55 | /// A bound on the operator norm $\|A\|$ for the linear operator $A$=`self`. | |
56 | /// This is not expected to be the norm, just any bound on it that can be | |
57 | /// reasonably implemented. | |
58 | fn opnorm_bound(&self) -> Self::FloatType; | |
59 | } | |
60 | ||
5 | 61 | // Linear operator application into mutable target. The [`AsRef`] bound |
62 | // is used to guarantee compatibility with `Yʹ` and `Self::Codomain`; | |
63 | // the former is assumed to be e.g. a view into the latter. | |
0 | 64 | |
65 | /*impl<X,Y,T> Fn(&X) -> Y for T where T : Linear<X,Codomain=Y> { | |
66 | fn call(&self, x : &X) -> Y { | |
67 | self.apply(x) | |
68 | } | |
69 | }*/ | |
70 | ||
5 | 71 | /// Trait for forming the adjoint operator of `Self`. |
0 | 72 | pub trait Adjointable<X,Yʹ> : Linear<X> { |
73 | type AdjointCodomain; | |
74 | type Adjoint<'a> : Linear<Yʹ, Codomain=Self::AdjointCodomain> where Self : 'a; | |
75 | ||
76 | /// Form the adjoint operator of `self`. | |
77 | fn adjoint(&self) -> Self::Adjoint<'_>; | |
78 | ||
79 | /*fn adjoint_apply(&self, y : &Yʹ) -> Self::AdjointCodomain { | |
80 | self.adjoint().apply(y) | |
81 | }*/ | |
82 | } | |
83 | ||
5 | 84 | /// Trait for forming a preadjoint of an operator. |
85 | /// | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
86 | /// For an operator $A$ this is an operator $A\_\*$ |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
87 | /// such that its adjoint $(A\_\*)^\*=A$. The space `X` is the domain of the `Self` |
0 | 88 | /// operator. The space `Ypre` is the predual of its codomain, and should be the |
89 | /// domain of the adjointed operator. `Self::Preadjoint` should be | |
90 | /// [`Adjointable`]`<'a,Ypre,X>`. | |
91 | pub trait Preadjointable<X,Ypre> : Linear<X> { | |
92 | type PreadjointCodomain; | |
93 | type Preadjoint<'a> : Adjointable<Ypre, X, Codomain=Self::PreadjointCodomain> where Self : 'a; | |
94 | ||
95 | /// Form the preadjoint operator of `self`. | |
96 | fn preadjoint(&self) -> Self::Preadjoint<'_>; | |
97 | } | |
98 | ||
5 | 99 | /// Adjointable operators $A: X → Y$ on between reflexive spaces $X$ and $Y$. |
0 | 100 | pub trait SimplyAdjointable<X> : Adjointable<X,<Self as Linear<X>>::Codomain> {} |
101 | impl<'a,X,T> SimplyAdjointable<X> for T where T : Adjointable<X,<Self as Linear<X>>::Codomain> {} | |
102 | ||
103 | /// The identity operator | |
104 | #[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
|
105 | 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
|
106 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
107 | impl<X> IdOp<X> { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
108 | fn new() -> IdOp<X> { IdOp(PhantomData) } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
109 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
110 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
111 | impl<X> Apply<X> for IdOp<X> { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
112 | type Output = X; |
0 | 113 | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
114 | fn apply(&self, x : X) -> X { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
115 | x |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
116 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
117 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
118 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
119 | impl<'a, X> Apply<&'a X> for IdOp<X> where X : Clone { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
120 | type Output = X; |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
121 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
122 | fn apply(&self, x : &'a X) -> X { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
123 | x.clone() |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
124 | } |
0 | 125 | } |
126 | ||
127 | impl<X> Linear<X> for IdOp<X> where X : Clone { | |
128 | type Codomain = X; | |
129 | } | |
130 | ||
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
131 | |
0 | 132 | #[replace_float_literals(F::cast_from(literal))] |
133 | impl<F : Num, X, Y> GEMV<F, X, Y> for IdOp<X> where Y : AXPY<F, X>, X : Clone { | |
134 | // Computes `y = αAx + βy`, where `A` is `Self`. | |
135 | fn gemv(&self, y : &mut Y, α : F, x : &X, β : F) { | |
136 | y.axpy(α, x, β) | |
137 | } | |
138 | ||
139 | fn apply_mut(&self, y : &mut Y, x : &X){ | |
140 | y.copy_from(x); | |
141 | } | |
142 | } | |
143 | ||
144 | impl<X> BoundedLinear<X> for IdOp<X> where X : Clone { | |
145 | type FloatType = float; | |
146 | fn opnorm_bound(&self) -> float { 1.0 } | |
147 | } | |
148 | ||
149 | impl<X> Adjointable<X,X> for IdOp<X> where X : Clone { | |
150 | type AdjointCodomain=X; | |
151 | type Adjoint<'a> = IdOp<X> where X : 'a; | |
152 | fn adjoint(&self) -> Self::Adjoint<'_> { IdOp::new() } | |
153 | } | |
154 |