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