Tue, 31 Dec 2024 09:12:43 -0500
Try to have Field as member type in Mappings etc.
| 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> |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
13 | + for<'a> Apply<&'a X, Output=Self::Codomain> |
|
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
14 | + HasScalarField { |
| 0 | 15 | type 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`. | |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
22 | fn axpy(&mut self, α : F, x : X, β : F); |
| 0 | 23 | |
| 24 | /// Copies `x` to `self`. | |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
25 | fn copy_from(&mut self, x : X) { |
| 0 | 26 | self.axpy(1.0, x, 0.0) |
| 27 | } | |
| 28 | ||
| 5 | 29 | /// Computes `y = αx`, where `y` is `Self`. |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
30 | fn scale_from(&mut self, α : F, x : X) { |
| 0 | 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))] | |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
37 | pub trait GEMV<F : Num, X, Y = <Self as Apply<X>>::Output> : Apply<X> { |
| 5 | 38 | /// Computes `y = αAx + βy`, where `A` is `Self`. |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
39 | fn gemv(&self, y : &mut Y, α : F, x : X, β : F); |
| 0 | 40 | |
| 5 | 41 | /// Computes `y = Ax`, where `A` is `Self` |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
42 | fn apply_mut(&self, y : &mut Y, x : X){ |
| 0 | 43 | self.gemv(y, 1.0, x, 0.0) |
| 44 | } | |
| 45 | ||
| 5 | 46 | /// Computes `y += Ax`, where `A` is `Self` |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
47 | fn apply_add(&self, y : &mut Y, x : X){ |
| 0 | 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 | /// 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. | |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
58 | fn opnorm_bound(&self) -> Self::Field; |
| 0 | 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> { |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
73 | type AdjointCodomain : HasScalarField<Field=Self::Field>; |
| 0 | 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> { | |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
92 | type PreadjointCodomain : HasScalarField<Field=Self::Field>; |
| 0 | 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 | ||
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
103 | /// The identity operator on `X` with scalar field `F`. |
| 0 | 104 | #[derive(Clone,Copy,Debug,Serialize,Eq,PartialEq)] |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
105 | pub struct IdOp<X, F : Num> (PhantomData<(X, F)>); |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
106 | |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
107 | impl<X, F : Num> HasScalarField for IdOp<X, F> { |
|
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
108 | type Field = F; |
|
13
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 | |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
111 | impl<X, F : Num> IdOp<X, F> { |
|
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
112 | fn new() -> IdOp<X, F> { IdOp(PhantomData) } |
|
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
113 | } |
|
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
114 | |
|
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
115 | impl<X, F : Num, T : CloneIfNeeded<X>> Apply<T> for IdOp<X, F> { |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
116 | type Output = X; |
| 0 | 117 | |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
118 | fn apply(&self, x : T) -> X { |
|
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
119 | x.clone_if_needed() |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
120 | } |
|
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 | |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
123 | impl<X : Clone, F : Num> Linear<X> for IdOp<X, F> { |
| 0 | 124 | type Codomain = X; |
| 125 | } | |
| 126 | ||
| 127 | #[replace_float_literals(F::cast_from(literal))] | |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
128 | impl<F : Num, X : Clone, Y> GEMV<F, X, Y> for IdOp<X, F> where Y : AXPY<F, X> { |
| 0 | 129 | // Computes `y = αAx + βy`, where `A` is `Self`. |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
130 | fn gemv(&self, y : &mut Y, α : F, x : X, β : F) { |
| 0 | 131 | y.axpy(α, x, β) |
| 132 | } | |
| 133 | ||
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
134 | fn apply_mut(&self, y : &mut Y, x : X){ |
| 0 | 135 | y.copy_from(x); |
| 136 | } | |
| 137 | } | |
| 138 | ||
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
139 | impl<X, F : Num> BoundedLinear<X> for IdOp<X, F> where X : Clone { |
|
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
140 | fn opnorm_bound(&self) -> F { F::ONE } |
| 0 | 141 | } |
| 142 | ||
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
143 | impl<X, F : Num> Adjointable<X,X> for IdOp<X, F> where X : Clone + HasScalarField<Field=F> { |
| 0 | 144 | type AdjointCodomain=X; |
|
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
145 | type Adjoint<'a> = IdOp<X, F> where X : 'a; |
| 0 | 146 | fn adjoint(&self) -> Self::Adjoint<'_> { IdOp::new() } |
| 147 | } | |
| 148 |