Mon, 01 Sep 2025 13:51:03 -0500
fubar
| 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; |
| 150 | 11 | use crate::instance::{ClosedSpace, 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 { | |
| 150 | 44 | DataTerm { opA: self.opA.clone(), b: self.b.clone(), g: self.g.clone() } |
| 111 | 45 | } |
| 46 | } | |
| 47 | ||
| 105 | 48 | #[allow(non_snake_case)] |
| 49 | impl<F: Float, Domain: Space, A: Mapping<Domain>, G: Mapping<A::Codomain, Codomain = F>> | |
| 50 | DataTerm<F, Domain, A, G> | |
| 51 | { | |
| 52 | pub fn new(opA: A, b: A::Codomain, g: G) -> Self { | |
| 53 | DataTerm { opA, b, g } | |
| 54 | } | |
| 55 | ||
| 56 | pub fn operator(&self) -> &'_ A { | |
| 57 | &self.opA | |
| 58 | } | |
| 59 | ||
| 60 | pub fn data(&self) -> &'_ <A as Mapping<Domain>>::Codomain { | |
| 61 | &self.b | |
| 62 | } | |
| 63 | ||
| 64 | pub fn fidelity(&self) -> &'_ G { | |
| 65 | &self.g | |
| 66 | } | |
| 67 | } | |
| 68 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
111
diff
changeset
|
69 | //+ AdjointProductBoundedBy<RNDM<N, F>, P, FloatType = F>, |
| 105 | 70 | |
| 71 | impl<F, X, A, G> Mapping<X> for DataTerm<F, X, A, G> | |
| 72 | where | |
| 73 | F: Float, | |
| 74 | X: Space, | |
| 75 | A: Mapping<X>, | |
| 76 | G: Mapping<A::Codomain, Codomain = F>, | |
| 150 | 77 | A::Codomain: ClosedSpace + for<'a> Sub<&'a A::Codomain, Output = A::Codomain>, |
| 105 | 78 | { |
| 79 | type Codomain = F; | |
| 80 | ||
| 81 | fn apply<I: Instance<X>>(&self, x: I) -> F { | |
| 82 | // TODO: possibly (if at all more effcient) use GEMV once generalised | |
| 83 | // to not require preallocation. However, Rust should be pretty efficient | |
| 84 | // at not doing preallocations or anything here, as the result of self.opA.apply() | |
| 85 | // can be consumed, so maybe GEMV is no use. | |
| 86 | self.g.apply(self.opA.apply(x) - &self.b) | |
| 87 | } | |
| 88 | } | |
| 89 | ||
| 90 | impl<F, X, A, G> ConvexMapping<X, F> for DataTerm<F, X, A, G> | |
| 91 | where | |
| 92 | F: Float, | |
| 109 | 93 | X: Normed<F>, |
| 105 | 94 | A: Linear<X>, |
| 95 | G: ConvexMapping<A::Codomain, F>, | |
| 150 | 96 | A::Codomain: ClosedSpace + Normed<F> + for<'a> Sub<&'a A::Codomain, Output = A::Codomain>, |
| 105 | 97 | { |
| 98 | } | |
| 99 | ||
| 100 | impl<F, X, Y, A, G> DifferentiableImpl<X> for DataTerm<F, X, A, G> | |
| 101 | where | |
| 102 | F: Float, | |
| 103 | X: Space, | |
| 150 | 104 | Y: Space + Instance<Y> + for<'a> Sub<&'a Y, Output = Y>, |
| 105 | 105 | //<A as Mapping<X>>::Codomain: Euclidean<F>, |
| 106 | A: Linear<X, Codomain = Y> + Preadjointable<X, G::DerivativeDomain>, | |
| 150 | 107 | G::DerivativeDomain: Instance<G::DerivativeDomain>, |
| 108 | A::PreadjointCodomain: ClosedSpace, | |
| 105 | 109 | //<<A as Mapping<X>>::Codomain as Euclidean<F>>::Output: Instance<<A as Mapping<X>>::Codomain>, |
| 110 | G: DifferentiableMapping<Y, Codomain = F>, | |
| 111 | { | |
| 112 | type Derivative = A::PreadjointCodomain; | |
| 113 | ||
| 114 | fn differential_impl<I: Instance<X>>(&self, x: I) -> Self::Derivative { | |
| 115 | // TODO: possibly (if at all more effcient) use GEMV once generalised | |
| 116 | // to not require preallocation. However, Rust should be pretty efficient | |
| 117 | // at not doing preallocations or anything here, as the result of self.opA.apply() | |
| 118 | // can be consumed, so maybe GEMV is no use. | |
| 119 | //self.opA.preadjoint().apply(self.opA.apply(x) - self.b) | |
| 120 | self.opA | |
| 121 | .preadjoint() | |
| 122 | .apply(self.g.diff_ref().apply(self.opA.apply(x) - &self.b)) | |
| 123 | } | |
| 124 | } | |
| 125 | ||
| 109 | 126 | impl<'a, F, X, Y, A, G> LipschitzDifferentiableImpl<X, X::NormExp> for DataTerm<F, X, A, G> |
| 105 | 127 | where |
| 128 | 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
|
129 | X: Normed<F>, |
| 109 | 130 | 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
|
131 | A: BoundedLinear<X, X::NormExp, L2, F, Codomain = Y>, |
| 109 | 132 | G: Mapping<Y, Codomain = F> + LipschitzDifferentiableImpl<Y, Y::NormExp>, |
| 105 | 133 | Self: DifferentiableImpl<X>, |
| 134 | { | |
| 135 | type FloatType = F; | |
| 136 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
109
diff
changeset
|
137 | 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
|
138 | Ok(self.opA.opnorm_bound(seminorm, L2)?.powi(2)) |
| 105 | 139 | } |
| 140 | } |