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