Sun, 18 May 2025 19:56:28 -0500
Added rustfmt.toml
| 58 | 1 | /*! |
| 2 | Some convex analysis basics | |
| 3 | */ | |
| 4 | ||
| 112 | 5 | use crate::error::DynResult; |
| 104 | 6 | use crate::euclidean::Euclidean; |
| 7 | use crate::instance::{DecompositionMut, Instance, InstanceMut}; | |
|
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> { |
| 60 | type Prox<'a>: Mapping<Domain, Codomain = Domain> | |
| 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 τ | |
| 104 | 68 | fn prox<I: Instance<Domain>>(&self, τ: Self::Codomain, z: I) -> Domain { |
| 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 |
| 104 | 73 | fn prox_mut<'b>(&self, τ: Self::Codomain, y: &'b mut Domain) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
74 | where |
| 104 | 75 | &'b mut Domain: InstanceMut<Domain>, |
| 76 | Domain::Decomp: DecompositionMut<Domain>, | |
| 77 | for<'a> &'a Domain: Instance<Domain>, | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
78 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
79 | *y = self.prox(τ, &*y); |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
80 | } |
| 58 | 81 | } |
| 82 | ||
| 88 | 83 | /// Constraint to the unit ball of the norm described by `E`. |
| 106 | 84 | #[derive(Copy, Clone, Debug, Serialize, Deserialize)] |
| 104 | 85 | pub struct NormConstraint<F: Float, E: NormExponent> { |
| 86 | radius: F, | |
| 87 | norm: NormMapping<F, E>, | |
| 72 | 88 | } |
| 89 | ||
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
90 | 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
|
91 | where |
| 109 | 92 | Domain: Normed<F>, |
| 104 | 93 | E: NormExponent, |
| 94 | F: Float, | |
| 95 | Self: Mapping<Domain, Codomain = F>, | |
| 96 | { | |
| 97 | } | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
98 | |
| 72 | 99 | 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
|
100 | where |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
101 | Domain: Space + Norm<E, F>, |
| 104 | 102 | F: Float, |
| 103 | E: NormExponent, | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
104 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
105 | type Codomain = F; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
106 | |
| 104 | 107 | fn apply<I: Instance<Domain>>(&self, d: I) -> F { |
| 72 | 108 | 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
|
109 | F::ZERO |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
110 | } else { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
111 | F::INFINITY |
|
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 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
115 | |
| 72 | 116 | 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
|
117 | where |
| 109 | 118 | Domain: Normed<F>, |
| 104 | 119 | E: NormExponent, |
| 120 | F: Float, | |
| 121 | Self: Mapping<Domain, Codomain = F>, | |
| 122 | { | |
| 123 | } | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
124 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
125 | 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
|
126 | where |
| 104 | 127 | E: HasDualExponent, |
| 128 | F: Float, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
129 | 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
|
130 | <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
|
131 | { |
| 104 | 132 | type Conjugate<'a> |
| 133 | = NormConstraint<F, E::DualExp> | |
| 134 | where | |
| 135 | Self: 'a; | |
| 72 | 136 | |
| 137 | fn conjugate(&self) -> Self::Conjugate<'_> { | |
| 138 | NormConstraint { | |
| 104 | 139 | radius: F::ONE, |
| 140 | norm: self.exponent.dual_exponent().as_mapping(), | |
| 72 | 141 | } |
| 142 | } | |
| 143 | } | |
| 144 | ||
| 145 | impl<C, E, F, Domain> Conjugable<Domain, F> for Weighted<NormMapping<F, E>, C> | |
| 146 | where | |
| 104 | 147 | C: Constant<Type = F>, |
| 148 | E: HasDualExponent, | |
| 149 | F: Float, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
150 | Domain: HasDual<F> + Norm<E, F> + Space, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
151 | <Domain as HasDual<F>>::DualSpace: Norm<E::DualExp, F>, |
| 72 | 152 | { |
| 104 | 153 | type Conjugate<'a> |
| 154 | = NormConstraint<F, E::DualExp> | |
| 155 | where | |
| 156 | Self: 'a; | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
157 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
158 | fn conjugate(&self) -> Self::Conjugate<'_> { |
| 72 | 159 | NormConstraint { |
| 104 | 160 | radius: self.weight.value(), |
| 161 | norm: self.base_fn.exponent.dual_exponent().as_mapping(), | |
| 72 | 162 | } |
| 163 | } | |
| 164 | } | |
| 165 | ||
| 166 | impl<Domain, E, F> Prox<Domain> for NormConstraint<F, E> | |
| 167 | where | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
112
diff
changeset
|
168 | Domain: Space + Norm<E, F>, |
| 104 | 169 | E: NormExponent, |
| 170 | F: Float, | |
| 171 | NormProjection<F, E>: Mapping<Domain, Codomain = Domain>, | |
| 72 | 172 | { |
| 104 | 173 | type Prox<'a> |
| 174 | = NormProjection<F, E> | |
| 175 | where | |
| 176 | Self: 'a; | |
| 72 | 177 | |
| 178 | #[inline] | |
| 104 | 179 | fn prox_mapping(&self, _τ: Self::Codomain) -> Self::Prox<'_> { |
| 72 | 180 | assert!(self.radius >= F::ZERO); |
| 104 | 181 | NormProjection { |
| 182 | radius: self.radius, | |
| 183 | exponent: self.norm.exponent, | |
| 184 | } | |
| 72 | 185 | } |
| 186 | } | |
| 187 | ||
| 88 | 188 | /// Projection to the unit ball of the norm described by `E`. |
| 106 | 189 | #[derive(Copy, Clone, Debug, Serialize, Deserialize)] |
| 104 | 190 | pub struct NormProjection<F: Float, E: NormExponent> { |
| 191 | radius: F, | |
| 192 | exponent: E, | |
| 72 | 193 | } |
| 194 | ||
| 195 | /* | |
| 196 | impl<F, Domain> Mapping<Domain> for NormProjection<F, L2> | |
| 197 | where | |
| 198 | Domain : Space + Euclidean<F> + std::ops::MulAssign<F>, | |
| 199 | F : Float, | |
| 200 | { | |
| 201 | type Codomain = Domain; | |
| 202 | ||
| 203 | fn apply<I : Instance<Domain>>(&self, d : I) -> Domain { | |
| 204 | d.own().proj_ball2(self.radius) | |
| 205 | } | |
| 206 | } | |
| 207 | */ | |
| 208 | ||
| 209 | impl<F, E, Domain> Mapping<Domain> for NormProjection<F, E> | |
| 210 | where | |
| 109 | 211 | Domain: Normed<F> + Projection<F, E>, |
| 104 | 212 | F: Float, |
| 213 | E: NormExponent, | |
| 72 | 214 | { |
| 215 | type Codomain = Domain; | |
| 216 | ||
| 104 | 217 | fn apply<I: Instance<Domain>>(&self, d: I) -> Domain { |
| 72 | 218 | d.own().proj_ball(self.radius, self.exponent) |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
219 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
220 | } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
221 | |
| 104 | 222 | /// The zero mapping |
| 106 | 223 | #[derive(Copy, Clone, Debug, Serialize, Deserialize)] |
| 104 | 224 | 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
|
225 | |
| 104 | 226 | impl<Domain: Space, F: Num> Zero<Domain, F> { |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
227 | pub fn new() -> Self { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
228 | Zero(PhantomData) |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
229 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
230 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
231 | |
| 104 | 232 | 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
|
233 | type Codomain = F; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
234 | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
235 | /// Compute the value of `self` at `x`. |
| 104 | 236 | 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
|
237 | F::ZERO |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
238 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
239 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
240 | |
| 109 | 241 | 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
|
242 | |
| 104 | 243 | impl<Domain: HasDual<F>, F: Float> Conjugable<Domain, F> for Zero<Domain, F> { |
| 244 | type Conjugate<'a> | |
| 245 | = ZeroIndicator<Domain::DualSpace, F> | |
| 246 | where | |
| 247 | Self: 'a; | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
248 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
249 | #[inline] |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
250 | fn conjugate(&self) -> Self::Conjugate<'_> { |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
251 | ZeroIndicator::new() |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
252 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
253 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
58
diff
changeset
|
254 | |
| 104 | 255 | 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
|
256 | where |
| 109 | 257 | Domain: Normed<F>, |
| 104 | 258 | Predual: HasDual<F>, |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
259 | { |
| 104 | 260 | type Preconjugate<'a> |
| 261 | = ZeroIndicator<Predual, F> | |
| 262 | where | |
| 263 | Self: 'a; | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
264 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
265 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
266 | fn preconjugate(&self) -> Self::Preconjugate<'_> { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
267 | ZeroIndicator::new() |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
268 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
269 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
270 | |
| 104 | 271 | impl<Domain: Space + Clone, F: Num> Prox<Domain> for Zero<Domain, F> { |
| 272 | type Prox<'a> | |
| 273 | = IdOp<Domain> | |
| 274 | where | |
| 275 | Self: 'a; | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
276 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
277 | #[inline] |
| 104 | 278 | fn prox_mapping(&self, _τ: Self::Codomain) -> Self::Prox<'_> { |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
279 | IdOp::new() |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
280 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
281 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
282 | |
| 104 | 283 | /// The zero indicator |
| 106 | 284 | #[derive(Copy, Clone, Debug, Serialize, Deserialize)] |
| 104 | 285 | 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
|
286 | |
| 104 | 287 | impl<Domain: Space, F: Num> ZeroIndicator<Domain, F> { |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
288 | pub fn new() -> Self { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
289 | ZeroIndicator(PhantomData) |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
290 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
291 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
292 | |
| 104 | 293 | 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
|
294 | type Codomain = F; |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
295 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
296 | /// Compute the value of `self` at `x`. |
| 104 | 297 | 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
|
298 | 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
|
299 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
300 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
301 | |
| 108 | 302 | impl<Domain: Normed<F>, F: Float> ConvexMapping<Domain, F> for ZeroIndicator<Domain, F> { |
| 303 | fn factor_of_strong_convexity(&self) -> F { | |
| 304 | F::INFINITY | |
| 305 | } | |
| 306 | } | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
307 | |
| 104 | 308 | impl<Domain: HasDual<F>, F: Float> Conjugable<Domain, F> for ZeroIndicator<Domain, F> { |
| 309 | type Conjugate<'a> | |
| 310 | = Zero<Domain::DualSpace, F> | |
| 311 | where | |
| 312 | Self: 'a; | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
313 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
314 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
315 | fn conjugate(&self) -> Self::Conjugate<'_> { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
316 | Zero::new() |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
317 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
318 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
319 | |
| 104 | 320 | 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
|
321 | where |
| 104 | 322 | Domain: Normed<F>, |
| 323 | Predual: HasDual<F>, | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
324 | { |
| 104 | 325 | type Preconjugate<'a> |
| 326 | = Zero<Predual, F> | |
| 327 | where | |
| 328 | Self: 'a; | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
329 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
330 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
331 | fn preconjugate(&self) -> Self::Preconjugate<'_> { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
332 | Zero::new() |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
333 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
334 | } |
| 104 | 335 | |
|
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
|
336 | 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
|
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 | 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
|
339 | 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
|
340 | { |
|
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 | 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
|
342 | = 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 | 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
|
344 | 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
|
345 | |
|
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
|
346 | /// 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
|
347 | 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
|
348 | 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
|
349 | } |
|
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
|
350 | } |
|
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
|
351 | |
| 104 | 352 | /// The squared Euclidean norm divided by two |
| 106 | 353 | #[derive(Copy, Clone, Serialize, Deserialize)] |
| 105 | 354 | pub struct Norm222<F: Float>(PhantomData<F>); |
| 104 | 355 | |
| 105 | 356 | impl</*Domain: Euclidean<F>,*/ F: Float> Norm222<F> { |
| 104 | 357 | pub fn new() -> Self { |
| 358 | Norm222(PhantomData) | |
| 359 | } | |
| 360 | } | |
| 361 | ||
| 112 | 362 | impl<X: Euclidean<F>, F: Float> Mapping<X> for Norm222<F> { |
| 104 | 363 | type Codomain = F; |
| 364 | ||
| 365 | /// Compute the value of `self` at `x`. | |
| 112 | 366 | fn apply<I: Instance<X>>(&self, x: I) -> Self::Codomain { |
| 104 | 367 | x.eval(|z| z.norm2_squared() / F::TWO) |
| 368 | } | |
| 369 | } | |
| 370 | ||
| 112 | 371 | impl<X: Euclidean<F>, F: Float> ConvexMapping<X, F> for Norm222<F> { |
| 108 | 372 | fn factor_of_strong_convexity(&self) -> F { |
| 373 | F::ONE | |
| 374 | } | |
| 375 | } | |
| 104 | 376 | |
| 112 | 377 | impl<X: Euclidean<F>, F: Float> Conjugable<X, F> for Norm222<F> { |
| 104 | 378 | type Conjugate<'a> |
| 379 | = Self | |
| 380 | where | |
| 381 | Self: 'a; | |
| 382 | ||
| 383 | #[inline] | |
| 384 | fn conjugate(&self) -> Self::Conjugate<'_> { | |
| 385 | Self::new() | |
| 386 | } | |
| 387 | } | |
| 388 | ||
| 112 | 389 | impl<X: Euclidean<F>, F: Float> Preconjugable<X, X, F> for Norm222<F> { |
| 104 | 390 | type Preconjugate<'a> |
| 391 | = Self | |
| 392 | where | |
| 393 | Self: 'a; | |
| 394 | ||
| 395 | #[inline] | |
| 396 | fn preconjugate(&self) -> Self::Preconjugate<'_> { | |
| 397 | Self::new() | |
| 398 | } | |
| 399 | } | |
| 400 | ||
| 112 | 401 | impl<X, F> Prox<X> for Norm222<F> |
| 104 | 402 | where |
| 403 | F: Float, | |
|
132
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
129
diff
changeset
|
404 | X: Euclidean<F, Owned = X>, |
| 104 | 405 | { |
| 406 | type Prox<'a> | |
| 407 | = Scaled<F> | |
| 408 | where | |
| 409 | Self: 'a; | |
| 410 | ||
| 411 | fn prox_mapping(&self, τ: F) -> Self::Prox<'_> { | |
| 412 | Scaled(F::ONE / (F::ONE + τ)) | |
| 413 | } | |
| 414 | } | |
| 112 | 415 | |
| 416 | impl<X, F> DifferentiableImpl<X> for Norm222<F> | |
| 417 | where | |
| 418 | F: Float, | |
|
132
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
129
diff
changeset
|
419 | X: Euclidean<F, Owned = X>, |
| 112 | 420 | { |
| 421 | type Derivative = X; | |
| 422 | ||
| 423 | fn differential_impl<I: Instance<X>>(&self, x: I) -> X { | |
| 424 | x.own() | |
| 425 | } | |
| 426 | } | |
| 427 | ||
| 428 | impl<X, F> LipschitzDifferentiableImpl<X, L2> for Norm222<F> | |
| 429 | where | |
| 430 | F: Float, | |
|
132
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
129
diff
changeset
|
431 | X: Euclidean<F, Owned = X>, |
| 112 | 432 | { |
| 433 | type FloatType = F; | |
| 434 | ||
| 435 | fn diff_lipschitz_factor(&self, _: L2) -> DynResult<Self::FloatType> { | |
| 436 | Ok(F::ONE) | |
| 437 | } | |
| 438 | } |