Sun, 11 May 2025 02:03:45 -0500
Switch from Cow to MyCow for DifferentiableImpl to avoid Clone trait bound
| 105 | 1 | /*! |
| 2 | General deata terms of the form $g(Ax-b)$ for an operator $A$ | |
| 3 | to a [`Euclidean`] space, and a function g on that space. | |
| 4 | */ | |
| 5 | ||
| 6 | #![allow(non_snake_case)] | |
| 7 | ||
| 109 | 8 | use super::{DifferentiableImpl, DifferentiableMapping, LipschitzDifferentiableImpl, Mapping}; |
| 105 | 9 | use crate::convex::ConvexMapping; |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
109
diff
changeset
|
10 | use crate::error::DynResult; |
| 105 | 11 | use crate::instance::{Instance, Space}; |
| 109 | 12 | use crate::linops::{BoundedLinear, Linear, Preadjointable}; |
| 13 | use crate::norms::{Normed, L2}; | |
| 105 | 14 | use crate::types::Float; |
| 15 | use std::ops::Sub; | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
109
diff
changeset
|
16 | |
| 105 | 17 | //use serde::{Deserialize, Serialize}; |
| 18 | ||
| 111 | 19 | /// Functions of the form $g(Ax-b)$ for an operator $A$, data $b$, and fidelity $g$. |
| 105 | 20 | pub struct DataTerm< |
| 21 | F: Float, | |
| 22 | Domain: Space, | |
| 23 | A: Mapping<Domain>, | |
| 24 | G: Mapping<A::Codomain, Codomain = F>, | |
| 25 | > { | |
| 111 | 26 | // The operator A |
| 105 | 27 | opA: A, |
| 111 | 28 | // The data b |
| 105 | 29 | b: <A as Mapping<Domain>>::Codomain, |
| 111 | 30 | // The outer fidelity |
| 105 | 31 | g: G, |
| 32 | } | |
| 33 | ||
| 111 | 34 | // Derive has troubles with `b`. |
| 35 | impl<F, Domain, A, G> Clone for DataTerm<F, Domain, A, G> | |
| 36 | where | |
| 37 | F: Float, | |
| 38 | Domain: Space, | |
| 39 | A: Mapping<Domain> + Clone, | |
| 40 | <A as Mapping<Domain>>::Codomain: Clone, | |
| 41 | G: Mapping<A::Codomain, Codomain = F> + Clone, | |
| 42 | { | |
| 43 | fn clone(&self) -> Self { | |
| 44 | DataTerm { | |
| 45 | opA: self.opA.clone(), | |
| 46 | b: self.b.clone(), | |
| 47 | g: self.g.clone(), | |
| 48 | } | |
| 49 | } | |
| 50 | } | |
| 51 | ||
| 105 | 52 | #[allow(non_snake_case)] |
| 53 | impl<F: Float, Domain: Space, A: Mapping<Domain>, G: Mapping<A::Codomain, Codomain = F>> | |
| 54 | DataTerm<F, Domain, A, G> | |
| 55 | { | |
| 56 | pub fn new(opA: A, b: A::Codomain, g: G) -> Self { | |
| 57 | DataTerm { opA, b, g } | |
| 58 | } | |
| 59 | ||
| 60 | pub fn operator(&self) -> &'_ A { | |
| 61 | &self.opA | |
| 62 | } | |
| 63 | ||
| 64 | pub fn data(&self) -> &'_ <A as Mapping<Domain>>::Codomain { | |
| 65 | &self.b | |
| 66 | } | |
| 67 | ||
| 68 | pub fn fidelity(&self) -> &'_ G { | |
| 69 | &self.g | |
| 70 | } | |
| 71 | } | |
| 72 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
111
diff
changeset
|
73 | //+ AdjointProductBoundedBy<RNDM<N, F>, P, FloatType = F>, |
| 105 | 74 | |
| 75 | impl<F, X, A, G> Mapping<X> for DataTerm<F, X, A, G> | |
| 76 | where | |
| 77 | F: Float, | |
| 78 | X: Space, | |
| 79 | A: Mapping<X>, | |
| 80 | G: Mapping<A::Codomain, Codomain = F>, | |
| 81 | A::Codomain: for<'a> Sub<&'a A::Codomain, Output = A::Codomain>, | |
| 82 | { | |
| 83 | type Codomain = F; | |
| 84 | ||
| 85 | fn apply<I: Instance<X>>(&self, x: I) -> F { | |
| 86 | // TODO: possibly (if at all more effcient) use GEMV once generalised | |
| 87 | // to not require preallocation. However, Rust should be pretty efficient | |
| 88 | // at not doing preallocations or anything here, as the result of self.opA.apply() | |
| 89 | // can be consumed, so maybe GEMV is no use. | |
| 90 | self.g.apply(self.opA.apply(x) - &self.b) | |
| 91 | } | |
| 92 | } | |
| 93 | ||
| 94 | impl<F, X, A, G> ConvexMapping<X, F> for DataTerm<F, X, A, G> | |
| 95 | where | |
| 96 | F: Float, | |
| 109 | 97 | X: Normed<F>, |
| 105 | 98 | A: Linear<X>, |
| 99 | G: ConvexMapping<A::Codomain, F>, | |
| 109 | 100 | A::Codomain: Normed<F> + for<'a> Sub<&'a A::Codomain, Output = A::Codomain>, |
| 105 | 101 | { |
| 102 | } | |
| 103 | ||
| 104 | impl<F, X, Y, A, G> DifferentiableImpl<X> for DataTerm<F, X, A, G> | |
| 105 | where | |
| 106 | F: Float, | |
| 107 | X: Space, | |
| 108 | Y: Space + for<'a> Sub<&'a Y, Output = Y>, | |
| 109 | //<A as Mapping<X>>::Codomain: Euclidean<F>, | |
| 110 | A: Linear<X, Codomain = Y> + Preadjointable<X, G::DerivativeDomain>, | |
| 111 | //<<A as Mapping<X>>::Codomain as Euclidean<F>>::Output: Instance<<A as Mapping<X>>::Codomain>, | |
| 112 | G: DifferentiableMapping<Y, Codomain = F>, | |
| 113 | { | |
| 114 | type Derivative = A::PreadjointCodomain; | |
| 115 | ||
| 116 | fn differential_impl<I: Instance<X>>(&self, x: I) -> Self::Derivative { | |
| 117 | // TODO: possibly (if at all more effcient) use GEMV once generalised | |
| 118 | // to not require preallocation. However, Rust should be pretty efficient | |
| 119 | // at not doing preallocations or anything here, as the result of self.opA.apply() | |
| 120 | // can be consumed, so maybe GEMV is no use. | |
| 121 | //self.opA.preadjoint().apply(self.opA.apply(x) - self.b) | |
| 122 | self.opA | |
| 123 | .preadjoint() | |
| 124 | .apply(self.g.diff_ref().apply(self.opA.apply(x) - &self.b)) | |
| 125 | } | |
| 126 | } | |
| 127 | ||
| 109 | 128 | impl<'a, F, X, Y, A, G> LipschitzDifferentiableImpl<X, X::NormExp> for DataTerm<F, X, A, G> |
| 105 | 129 | where |
| 130 | F: Float, | |
|
128
f75bf34adda0
Switch from Cow to MyCow for DifferentiableImpl to avoid Clone trait bound
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
131 | X: Normed<F>, |
| 109 | 132 | Y: Normed<F>, |
|
128
f75bf34adda0
Switch from Cow to MyCow for DifferentiableImpl to avoid Clone trait bound
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
133 | A: BoundedLinear<X, X::NormExp, L2, F, Codomain = Y>, |
| 109 | 134 | G: Mapping<Y, Codomain = F> + LipschitzDifferentiableImpl<Y, Y::NormExp>, |
| 105 | 135 | Self: DifferentiableImpl<X>, |
| 136 | { | |
| 137 | type FloatType = F; | |
| 138 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
109
diff
changeset
|
139 | fn diff_lipschitz_factor(&self, seminorm: X::NormExp) -> DynResult<F> { |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
109
diff
changeset
|
140 | Ok(self.opA.opnorm_bound(seminorm, L2)?.powi(2)) |
| 105 | 141 | } |
| 142 | } |