Tue, 02 Sep 2025 00:09:46 -0500
sketc
| 58 | 1 | /*! |
| 2 | Some convex analysis basics | |
| 3 | */ | |
| 4 | ||
| 112 | 5 | use crate::error::DynResult; |
| 104 | 6 | use crate::euclidean::Euclidean; |
| 150 | 7 | use crate::instance::{ClosedSpace, DecompositionMut, Instance}; |
|
129
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
8 | use crate::linops::{IdOp, Scaled, SimpleZeroOp, AXPY}; |
| 112 | 9 | use crate::mapping::{DifferentiableImpl, LipschitzDifferentiableImpl, Mapping, Space}; |
| 104 | 10 | use crate::norms::*; |
| 72 | 11 | use crate::operator_arithmetic::{Constant, Weighted}; |
| 104 | 12 | use crate::types::*; |
| 106 | 13 | use serde::{Deserialize, Serialize}; |
| 104 | 14 | use std::marker::PhantomData; |
| 58 | 15 | |
| 16 | /// Trait for convex mappings. Has no features, just serves as a constraint | |
| 17 | /// | |
| 18 | /// TODO: should constrain `Mapping::Codomain` to implement a partial order, | |
| 19 | /// but this makes everything complicated with little benefit. | |
| 109 | 20 | pub trait ConvexMapping<Domain: Normed<F>, F: Num = f64>: Mapping<Domain, Codomain = F> { |
| 21 | /// Returns (a lower estimate of) the factor of strong convexity in the norm of `Domain`. | |
| 108 | 22 | fn factor_of_strong_convexity(&self) -> F { |
| 23 | F::ZERO | |
| 24 | } | |
| 25 | } | |
| 58 | 26 | |
| 27 | /// Trait for mappings with a Fenchel conjugate | |
| 28 | /// | |
| 29 | /// The conjugate type has to implement [`ConvexMapping`], but a `Conjugable` mapping need | |
| 30 | /// not be convex. | |
|
107
441d30e66a4a
Missing restriction in Conjugable
Tuomo Valkonen <tuomov@iki.fi>
parents:
106
diff
changeset
|
31 | pub trait Conjugable<Domain: HasDual<F>, F: Num = f64>: Mapping<Domain, Codomain = F> { |
| 104 | 32 | type Conjugate<'a>: ConvexMapping<Domain::DualSpace, F> |
| 33 | where | |
| 34 | Self: 'a; | |
| 58 | 35 | |
| 36 | fn conjugate(&self) -> Self::Conjugate<'_>; | |
| 37 | } | |
| 38 | ||
| 39 | /// Trait for mappings with a Fenchel preconjugate | |
| 40 | /// | |
| 41 | /// In contrast to [`Conjugable`], the preconjugate need not implement [`ConvexMapping`], | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
42 | /// but a `Preconjugable` mapping has to be convex. |
| 104 | 43 | pub trait Preconjugable<Domain, Predual, F: Num = f64>: ConvexMapping<Domain, F> |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
44 | where |
| 109 | 45 | Domain: Normed<F>, |
| 104 | 46 | Predual: HasDual<F>, |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
47 | { |
|
107
441d30e66a4a
Missing restriction in Conjugable
Tuomo Valkonen <tuomov@iki.fi>
parents:
106
diff
changeset
|
48 | type Preconjugate<'a>: Mapping<Predual, Codomain = F> |
| 104 | 49 | where |
| 50 | Self: 'a; | |
| 58 | 51 | |
| 52 | fn preconjugate(&self) -> Self::Preconjugate<'_>; | |
| 53 | } | |
| 54 | ||
| 55 | /// Trait for mappings with a proximap map | |
| 56 | /// | |
| 57 | /// The conjugate type has to implement [`ConvexMapping`], but a `Conjugable` mapping need | |
| 58 | /// not be convex. | |
| 104 | 59 | pub trait Prox<Domain: Space>: Mapping<Domain> { |
| 150 | 60 | type Prox<'a>: Mapping<Domain, Codomain = Domain::OwnedSpace> |
| 104 | 61 | where |
| 62 | Self: 'a; | |
| 58 | 63 | |
| 64 | /// Returns a proximal mapping with weight τ | |
| 104 | 65 | fn prox_mapping(&self, τ: Self::Codomain) -> Self::Prox<'_>; |
| 58 | 66 | |
| 67 | /// Calculate the proximal mapping with weight τ | |
| 150 | 68 | fn prox<I: Instance<Domain>>(&self, τ: Self::Codomain, z: I) -> Domain::OwnedSpace { |
| 58 | 69 | self.prox_mapping(τ).apply(z) |
| 70 | } | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
71 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
72 | /// Calculate the proximal mapping with weight τ in-place |
| 150 | 73 | fn prox_mut<'b>(&self, τ: Self::Codomain, y: &'b mut Domain::OwnedSpace) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
74 | where |
| 104 | 75 | Domain::Decomp: DecompositionMut<Domain>, |
| 150 | 76 | for<'a> &'a Domain::OwnedSpace: Instance<Domain>, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
77 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
78 | *y = self.prox(τ, &*y); |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
79 | } |
| 58 | 80 | } |
| 81 | ||
| 88 | 82 | /// Constraint to the unit ball of the norm described by `E`. |
| 106 | 83 | #[derive(Copy, Clone, Debug, Serialize, Deserialize)] |
| 104 | 84 | pub struct NormConstraint<F: Float, E: NormExponent> { |
| 85 | radius: F, | |
| 86 | norm: NormMapping<F, E>, | |
| 72 | 87 | } |
| 88 | ||
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
89 | impl<Domain, E, F> ConvexMapping<Domain, F> for NormMapping<F, E> |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
90 | where |
| 109 | 91 | Domain: Normed<F>, |
| 104 | 92 | E: NormExponent, |
| 93 | F: Float, | |
| 94 | Self: Mapping<Domain, Codomain = F>, | |
| 95 | { | |
| 96 | } | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
97 | |
| 72 | 98 | impl<F, E, Domain> Mapping<Domain> for NormConstraint<F, E> |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
99 | where |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
100 | Domain: Space + Norm<E, F>, |
| 104 | 101 | F: Float, |
| 102 | E: NormExponent, | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
103 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
104 | type Codomain = F; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
105 | |
| 104 | 106 | fn apply<I: Instance<Domain>>(&self, d: I) -> F { |
| 72 | 107 | if d.eval(|x| x.norm(self.norm.exponent)) <= self.radius { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
108 | F::ZERO |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
109 | } else { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
110 | F::INFINITY |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
111 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
112 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
113 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
114 | |
| 72 | 115 | impl<Domain, E, F> ConvexMapping<Domain, F> for NormConstraint<F, E> |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
116 | where |
| 109 | 117 | Domain: Normed<F>, |
| 104 | 118 | E: NormExponent, |
| 119 | F: Float, | |
| 120 | Self: Mapping<Domain, Codomain = F>, | |
| 121 | { | |
| 122 | } | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
123 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
124 | impl<E, F, Domain> Conjugable<Domain, F> for NormMapping<F, E> |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
125 | where |
| 104 | 126 | E: HasDualExponent, |
| 127 | F: Float, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
128 | Domain: HasDual<F> + Norm<E, F> + Normed<F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
129 | <Domain as HasDual<F>>::DualSpace: Norm<E::DualExp, F>, |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
130 | { |
| 104 | 131 | type Conjugate<'a> |
| 132 | = NormConstraint<F, E::DualExp> | |
| 133 | where | |
| 134 | Self: 'a; | |
| 72 | 135 | |
| 136 | fn conjugate(&self) -> Self::Conjugate<'_> { | |
| 146 | 137 | NormConstraint { radius: F::ONE, norm: self.exponent.dual_exponent().as_mapping() } |
| 72 | 138 | } |
| 139 | } | |
| 140 | ||
| 141 | impl<C, E, F, Domain> Conjugable<Domain, F> for Weighted<NormMapping<F, E>, C> | |
| 142 | where | |
| 104 | 143 | C: Constant<Type = F>, |
| 144 | E: HasDualExponent, | |
| 145 | F: Float, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
146 | Domain: HasDual<F> + Norm<E, F> + Space, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
147 | <Domain as HasDual<F>>::DualSpace: Norm<E::DualExp, F>, |
| 72 | 148 | { |
| 104 | 149 | type Conjugate<'a> |
| 150 | = NormConstraint<F, E::DualExp> | |
| 151 | where | |
| 152 | Self: 'a; | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
153 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
154 | fn conjugate(&self) -> Self::Conjugate<'_> { |
| 72 | 155 | NormConstraint { |
| 104 | 156 | radius: self.weight.value(), |
| 157 | norm: self.base_fn.exponent.dual_exponent().as_mapping(), | |
| 72 | 158 | } |
| 159 | } | |
| 160 | } | |
| 161 | ||
| 162 | impl<Domain, E, F> Prox<Domain> for NormConstraint<F, E> | |
| 163 | where | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
164 | Domain: Space + Norm<E, F>, |
| 104 | 165 | E: NormExponent, |
| 166 | F: Float, | |
| 150 | 167 | NormProjection<F, E>: Mapping<Domain, Codomain = Domain::OwnedSpace>, |
| 72 | 168 | { |
| 104 | 169 | type Prox<'a> |
| 170 | = NormProjection<F, E> | |
| 171 | where | |
| 172 | Self: 'a; | |
| 72 | 173 | |
| 174 | #[inline] | |
| 104 | 175 | fn prox_mapping(&self, _τ: Self::Codomain) -> Self::Prox<'_> { |
| 72 | 176 | assert!(self.radius >= F::ZERO); |
| 146 | 177 | NormProjection { radius: self.radius, exponent: self.norm.exponent } |
| 72 | 178 | } |
| 179 | } | |
| 180 | ||
| 88 | 181 | /// Projection to the unit ball of the norm described by `E`. |
| 106 | 182 | #[derive(Copy, Clone, Debug, Serialize, Deserialize)] |
| 104 | 183 | pub struct NormProjection<F: Float, E: NormExponent> { |
| 184 | radius: F, | |
| 185 | exponent: E, | |
| 72 | 186 | } |
| 187 | ||
| 188 | /* | |
| 189 | impl<F, Domain> Mapping<Domain> for NormProjection<F, L2> | |
| 190 | where | |
| 191 | Domain : Space + Euclidean<F> + std::ops::MulAssign<F>, | |
| 192 | F : Float, | |
| 193 | { | |
| 194 | type Codomain = Domain; | |
| 195 | ||
| 196 | fn apply<I : Instance<Domain>>(&self, d : I) -> Domain { | |
| 197 | d.own().proj_ball2(self.radius) | |
| 198 | } | |
| 199 | } | |
| 200 | */ | |
| 201 | ||
| 202 | impl<F, E, Domain> Mapping<Domain> for NormProjection<F, E> | |
| 203 | where | |
| 109 | 204 | Domain: Normed<F> + Projection<F, E>, |
| 150 | 205 | Domain::OwnedSpace: ClosedSpace, |
| 104 | 206 | F: Float, |
| 207 | E: NormExponent, | |
| 72 | 208 | { |
| 150 | 209 | type Codomain = Domain::OwnedSpace; |
| 72 | 210 | |
| 150 | 211 | fn apply<I: Instance<Domain>>(&self, d: I) -> Self::Codomain { |
| 72 | 212 | d.own().proj_ball(self.radius, self.exponent) |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
213 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
214 | } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
215 | |
| 104 | 216 | /// The zero mapping |
| 106 | 217 | #[derive(Copy, Clone, Debug, Serialize, Deserialize)] |
| 104 | 218 | pub struct Zero<Domain: Space, F: Num>(PhantomData<(Domain, F)>); |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
219 | |
| 104 | 220 | impl<Domain: Space, F: Num> Zero<Domain, F> { |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
221 | pub fn new() -> Self { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
222 | Zero(PhantomData) |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
223 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
224 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
225 | |
| 104 | 226 | impl<Domain: Space, F: Num> Mapping<Domain> for Zero<Domain, F> { |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
227 | type Codomain = F; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
228 | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
229 | /// Compute the value of `self` at `x`. |
| 104 | 230 | fn apply<I: Instance<Domain>>(&self, _x: I) -> Self::Codomain { |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
231 | F::ZERO |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
232 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
233 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
234 | |
| 109 | 235 | impl<Domain: Normed<F>, F: Float> ConvexMapping<Domain, F> for Zero<Domain, F> {} |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
236 | |
| 104 | 237 | impl<Domain: HasDual<F>, F: Float> Conjugable<Domain, F> for Zero<Domain, F> { |
| 238 | type Conjugate<'a> | |
| 239 | = ZeroIndicator<Domain::DualSpace, F> | |
| 240 | where | |
| 241 | Self: 'a; | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
242 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
243 | #[inline] |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
244 | fn conjugate(&self) -> Self::Conjugate<'_> { |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
245 | ZeroIndicator::new() |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
246 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
247 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
248 | |
| 104 | 249 | impl<Domain, Predual, F: Float> Preconjugable<Domain, Predual, F> for Zero<Domain, F> |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
250 | where |
| 109 | 251 | Domain: Normed<F>, |
| 104 | 252 | Predual: HasDual<F>, |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
253 | { |
| 104 | 254 | type Preconjugate<'a> |
| 255 | = ZeroIndicator<Predual, F> | |
| 256 | where | |
| 257 | Self: 'a; | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
258 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
259 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
260 | fn preconjugate(&self) -> Self::Preconjugate<'_> { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
261 | ZeroIndicator::new() |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
262 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
263 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
264 | |
| 150 | 265 | impl<Domain: Space, F: Num> Prox<Domain> for Zero<Domain, F> { |
| 104 | 266 | type Prox<'a> |
| 267 | = IdOp<Domain> | |
| 268 | where | |
| 269 | Self: 'a; | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
270 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
271 | #[inline] |
| 104 | 272 | fn prox_mapping(&self, _τ: Self::Codomain) -> Self::Prox<'_> { |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
273 | IdOp::new() |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
274 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
275 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
276 | |
| 104 | 277 | /// The zero indicator |
| 106 | 278 | #[derive(Copy, Clone, Debug, Serialize, Deserialize)] |
| 104 | 279 | pub struct ZeroIndicator<Domain: Space, F: Num>(PhantomData<(Domain, F)>); |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
280 | |
| 104 | 281 | impl<Domain: Space, F: Num> ZeroIndicator<Domain, F> { |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
282 | pub fn new() -> Self { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
283 | ZeroIndicator(PhantomData) |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
284 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
285 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
286 | |
| 104 | 287 | impl<Domain: Normed<F>, F: Float> Mapping<Domain> for ZeroIndicator<Domain, F> { |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
288 | type Codomain = F; |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
289 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
290 | /// Compute the value of `self` at `x`. |
| 104 | 291 | fn apply<I: Instance<Domain>>(&self, x: I) -> Self::Codomain { |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
292 | x.eval(|x̃| if x̃.is_zero() { F::ZERO } else { F::INFINITY }) |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
293 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
294 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
295 | |
| 108 | 296 | impl<Domain: Normed<F>, F: Float> ConvexMapping<Domain, F> for ZeroIndicator<Domain, F> { |
| 297 | fn factor_of_strong_convexity(&self) -> F { | |
| 298 | F::INFINITY | |
| 299 | } | |
| 300 | } | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
301 | |
| 104 | 302 | impl<Domain: HasDual<F>, F: Float> Conjugable<Domain, F> for ZeroIndicator<Domain, F> { |
| 303 | type Conjugate<'a> | |
| 304 | = Zero<Domain::DualSpace, F> | |
| 305 | where | |
| 306 | Self: 'a; | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
307 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
308 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
309 | fn conjugate(&self) -> Self::Conjugate<'_> { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
310 | Zero::new() |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
311 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
312 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
313 | |
| 104 | 314 | impl<Domain, Predual, F: Float> Preconjugable<Domain, Predual, F> for ZeroIndicator<Domain, F> |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
315 | where |
| 104 | 316 | Domain: Normed<F>, |
| 317 | Predual: HasDual<F>, | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
318 | { |
| 104 | 319 | type Preconjugate<'a> |
| 320 | = Zero<Predual, F> | |
| 321 | where | |
| 322 | Self: 'a; | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
323 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
324 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
325 | fn preconjugate(&self) -> Self::Preconjugate<'_> { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
326 | Zero::new() |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
327 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
328 | } |
| 104 | 329 | |
|
129
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
330 | impl<Domain, F> Prox<Domain> for ZeroIndicator<Domain, F> |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
331 | where |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
332 | Domain: AXPY<Field = F, Owned = Domain> + Normed<F>, |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
333 | F: Float, |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
334 | { |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
335 | type Prox<'a> |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
336 | = SimpleZeroOp |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
337 | where |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
338 | Self: 'a; |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
339 | |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
340 | /// Returns a proximal mapping with weight τ |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
341 | fn prox_mapping(&self, _τ: F) -> Self::Prox<'_> { |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
342 | return SimpleZeroOp; |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
343 | } |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
344 | } |
|
d2994e34a5f5
Simplify ZeroOp to SimpleZeroOp, only from X to X. Add Prox for ZeroIndicator. Move F parameter to AXPY::Field.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
345 | |
| 104 | 346 | /// The squared Euclidean norm divided by two |
| 106 | 347 | #[derive(Copy, Clone, Serialize, Deserialize)] |
| 105 | 348 | pub struct Norm222<F: Float>(PhantomData<F>); |
| 104 | 349 | |
| 105 | 350 | impl</*Domain: Euclidean<F>,*/ F: Float> Norm222<F> { |
| 104 | 351 | pub fn new() -> Self { |
| 352 | Norm222(PhantomData) | |
| 353 | } | |
| 354 | } | |
| 355 | ||
| 112 | 356 | impl<X: Euclidean<F>, F: Float> Mapping<X> for Norm222<F> { |
| 104 | 357 | type Codomain = F; |
| 358 | ||
| 359 | /// Compute the value of `self` at `x`. | |
| 112 | 360 | fn apply<I: Instance<X>>(&self, x: I) -> Self::Codomain { |
| 104 | 361 | x.eval(|z| z.norm2_squared() / F::TWO) |
| 362 | } | |
| 363 | } | |
| 364 | ||
| 112 | 365 | impl<X: Euclidean<F>, F: Float> ConvexMapping<X, F> for Norm222<F> { |
| 108 | 366 | fn factor_of_strong_convexity(&self) -> F { |
| 367 | F::ONE | |
| 368 | } | |
| 369 | } | |
| 104 | 370 | |
| 112 | 371 | impl<X: Euclidean<F>, F: Float> Conjugable<X, F> for Norm222<F> { |
| 104 | 372 | type Conjugate<'a> |
| 373 | = Self | |
| 374 | where | |
| 375 | Self: 'a; | |
| 376 | ||
| 377 | #[inline] | |
| 378 | fn conjugate(&self) -> Self::Conjugate<'_> { | |
| 379 | Self::new() | |
| 380 | } | |
| 381 | } | |
| 382 | ||
| 112 | 383 | impl<X: Euclidean<F>, F: Float> Preconjugable<X, X, F> for Norm222<F> { |
| 104 | 384 | type Preconjugate<'a> |
| 385 | = Self | |
| 386 | where | |
| 387 | Self: 'a; | |
| 388 | ||
| 389 | #[inline] | |
| 390 | fn preconjugate(&self) -> Self::Preconjugate<'_> { | |
| 391 | Self::new() | |
| 392 | } | |
| 393 | } | |
| 394 | ||
| 112 | 395 | impl<X, F> Prox<X> for Norm222<F> |
| 104 | 396 | where |
| 397 | F: Float, | |
| 151 | 398 | X: Euclidean<F>, |
| 104 | 399 | { |
| 400 | type Prox<'a> | |
| 401 | = Scaled<F> | |
| 402 | where | |
| 403 | Self: 'a; | |
| 404 | ||
| 405 | fn prox_mapping(&self, τ: F) -> Self::Prox<'_> { | |
| 406 | Scaled(F::ONE / (F::ONE + τ)) | |
| 407 | } | |
| 408 | } | |
| 112 | 409 | |
| 410 | impl<X, F> DifferentiableImpl<X> for Norm222<F> | |
| 411 | where | |
| 412 | F: Float, | |
| 150 | 413 | X: Euclidean<F>, |
| 112 | 414 | { |
| 150 | 415 | type Derivative = X::Owned; |
| 112 | 416 | |
| 150 | 417 | fn differential_impl<I: Instance<X>>(&self, x: I) -> Self::Derivative { |
| 154 | 418 | x.into_owned() |
| 112 | 419 | } |
| 420 | } | |
| 421 | ||
| 422 | impl<X, F> LipschitzDifferentiableImpl<X, L2> for Norm222<F> | |
| 423 | where | |
| 424 | F: Float, | |
| 150 | 425 | X: Euclidean<F>, |
| 112 | 426 | { |
| 427 | type FloatType = F; | |
| 428 | ||
| 429 | fn diff_lipschitz_factor(&self, _: L2) -> DynResult<Self::FloatType> { | |
| 430 | Ok(F::ONE) | |
| 431 | } | |
| 432 | } |