Wed, 30 Apr 2025 16:39:01 -0500
Use DynResult for Lipschitz factors and operator norm bounds
| 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 | ||
| 19 | /// Functions of the form $\frac{1}{2}\|Ax-b\|_2^2$ for an operator $A$ | |
| 20 | /// to a [`Euclidean`] space. | |
| 21 | pub struct DataTerm< | |
| 22 | F: Float, | |
| 23 | Domain: Space, | |
| 24 | A: Mapping<Domain>, | |
| 25 | G: Mapping<A::Codomain, Codomain = F>, | |
| 26 | > { | |
| 27 | opA: A, | |
| 28 | b: <A as Mapping<Domain>>::Codomain, | |
| 29 | g: G, | |
| 30 | } | |
| 31 | ||
| 32 | #[allow(non_snake_case)] | |
| 33 | impl<F: Float, Domain: Space, A: Mapping<Domain>, G: Mapping<A::Codomain, Codomain = F>> | |
| 34 | DataTerm<F, Domain, A, G> | |
| 35 | { | |
| 36 | pub fn new(opA: A, b: A::Codomain, g: G) -> Self { | |
| 37 | DataTerm { opA, b, g } | |
| 38 | } | |
| 39 | ||
| 40 | pub fn operator(&self) -> &'_ A { | |
| 41 | &self.opA | |
| 42 | } | |
| 43 | ||
| 44 | pub fn data(&self) -> &'_ <A as Mapping<Domain>>::Codomain { | |
| 45 | &self.b | |
| 46 | } | |
| 47 | ||
| 48 | pub fn fidelity(&self) -> &'_ G { | |
| 49 | &self.g | |
| 50 | } | |
| 51 | } | |
| 52 | ||
| 53 | //+ AdjointProductBoundedBy<RNDM<F, N>, P, FloatType = F>, | |
| 54 | ||
| 55 | impl<F, X, A, G> Mapping<X> for DataTerm<F, X, A, G> | |
| 56 | where | |
| 57 | F: Float, | |
| 58 | X: Space, | |
| 59 | A: Mapping<X>, | |
| 60 | G: Mapping<A::Codomain, Codomain = F>, | |
| 61 | A::Codomain: for<'a> Sub<&'a A::Codomain, Output = A::Codomain>, | |
| 62 | { | |
| 63 | type Codomain = F; | |
| 64 | ||
| 65 | fn apply<I: Instance<X>>(&self, x: I) -> F { | |
| 66 | // TODO: possibly (if at all more effcient) use GEMV once generalised | |
| 67 | // to not require preallocation. However, Rust should be pretty efficient | |
| 68 | // at not doing preallocations or anything here, as the result of self.opA.apply() | |
| 69 | // can be consumed, so maybe GEMV is no use. | |
| 70 | self.g.apply(self.opA.apply(x) - &self.b) | |
| 71 | } | |
| 72 | } | |
| 73 | ||
| 74 | impl<F, X, A, G> ConvexMapping<X, F> for DataTerm<F, X, A, G> | |
| 75 | where | |
| 76 | F: Float, | |
| 109 | 77 | X: Normed<F>, |
| 105 | 78 | A: Linear<X>, |
| 79 | G: ConvexMapping<A::Codomain, F>, | |
| 109 | 80 | A::Codomain: Normed<F> + for<'a> Sub<&'a A::Codomain, Output = A::Codomain>, |
| 105 | 81 | { |
| 82 | } | |
| 83 | ||
| 84 | impl<F, X, Y, A, G> DifferentiableImpl<X> for DataTerm<F, X, A, G> | |
| 85 | where | |
| 86 | F: Float, | |
| 87 | X: Space, | |
| 88 | Y: Space + for<'a> Sub<&'a Y, Output = Y>, | |
| 89 | //<A as Mapping<X>>::Codomain: Euclidean<F>, | |
| 90 | A: Linear<X, Codomain = Y> + Preadjointable<X, G::DerivativeDomain>, | |
| 91 | //<<A as Mapping<X>>::Codomain as Euclidean<F>>::Output: Instance<<A as Mapping<X>>::Codomain>, | |
| 92 | G: DifferentiableMapping<Y, Codomain = F>, | |
| 93 | { | |
| 94 | type Derivative = A::PreadjointCodomain; | |
| 95 | ||
| 96 | fn differential_impl<I: Instance<X>>(&self, x: I) -> Self::Derivative { | |
| 97 | // TODO: possibly (if at all more effcient) use GEMV once generalised | |
| 98 | // to not require preallocation. However, Rust should be pretty efficient | |
| 99 | // at not doing preallocations or anything here, as the result of self.opA.apply() | |
| 100 | // can be consumed, so maybe GEMV is no use. | |
| 101 | //self.opA.preadjoint().apply(self.opA.apply(x) - self.b) | |
| 102 | self.opA | |
| 103 | .preadjoint() | |
| 104 | .apply(self.g.diff_ref().apply(self.opA.apply(x) - &self.b)) | |
| 105 | } | |
| 106 | } | |
| 107 | ||
| 109 | 108 | impl<'a, F, X, Y, A, G> LipschitzDifferentiableImpl<X, X::NormExp> for DataTerm<F, X, A, G> |
| 105 | 109 | where |
| 110 | F: Float, | |
| 109 | 111 | X: Normed<F> + Clone, |
| 112 | Y: Normed<F>, | |
| 113 | A: Clone + BoundedLinear<X, X::NormExp, L2, F, Codomain = Y>, | |
| 114 | G: Mapping<Y, Codomain = F> + LipschitzDifferentiableImpl<Y, Y::NormExp>, | |
| 105 | 115 | Self: DifferentiableImpl<X>, |
| 116 | { | |
| 117 | type FloatType = F; | |
| 118 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
109
diff
changeset
|
119 | 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
|
120 | Ok(self.opA.opnorm_bound(seminorm, L2)?.powi(2)) |
| 105 | 121 | } |
| 122 | } |