# HG changeset patch # User Tuomo Valkonen # Date 1735653775 18000 # Node ID 848ecc05becf7975deec090896b0ccd5b900dc81 # Parent 9226980e45a704d75ae1b3c8c808e318647792af More convexity, normed spaces, etc. diff -r 9226980e45a7 -r 848ecc05becf src/convex.rs --- a/src/convex.rs Tue Dec 31 08:30:02 2024 -0500 +++ b/src/convex.rs Tue Dec 31 09:02:55 2024 -0500 @@ -2,8 +2,10 @@ Some convex analysis basics */ +use std::marker::PhantomData; use crate::types::*; use crate::mapping::{Mapping, Space}; +use crate::linops::IdOp; use crate::instance::{Instance, InstanceMut, DecompositionMut}; use crate::norms::*; @@ -11,16 +13,15 @@ /// /// TODO: should constrain `Mapping::Codomain` to implement a partial order, /// but this makes everything complicated with little benefit. -pub trait ConvexMapping : Mapping +pub trait ConvexMapping : Mapping {} /// Trait for mappings with a Fenchel conjugate /// /// The conjugate type has to implement [`ConvexMapping`], but a `Conjugable` mapping need /// not be convex. -pub trait Conjugable : Mapping { - type DualDomain : Space; - type Conjugate<'a> : ConvexMapping where Self : 'a; +pub trait Conjugable, F : Num = f64> : Mapping { + type Conjugate<'a> : ConvexMapping where Self : 'a; fn conjugate(&self) -> Self::Conjugate<'_>; } @@ -28,10 +29,13 @@ /// Trait for mappings with a Fenchel preconjugate /// /// In contrast to [`Conjugable`], the preconjugate need not implement [`ConvexMapping`], -/// but a `Preconjugable` mapping has be convex. -pub trait Preconjugable : ConvexMapping { - type PredualDomain : Space; - type Preconjugate<'a> : Mapping where Self : 'a; +/// but a `Preconjugable` mapping has to be convex. +pub trait Preconjugable : ConvexMapping +where + Domain : Space, + Predual : HasDual +{ + type Preconjugate<'a> : Mapping where Self : 'a; fn preconjugate(&self) -> Self::Preconjugate<'_>; } @@ -65,20 +69,13 @@ pub struct NormConjugate(NormMapping); -impl ConvexMapping for NormMapping +impl ConvexMapping for NormMapping where Domain : Space, E : NormExponent, F : Float, - Self : Mapping {} - - -impl ConvexMapping for NormConjugate -where - Domain : Space, - E : NormExponent, - F : Float, - Self : Mapping {} + Self : Mapping +{} impl Mapping for NormConjugate @@ -98,20 +95,121 @@ } } +impl ConvexMapping for NormConjugate +where + Domain : Space, + E : NormExponent, + F : Float, + Self : Mapping +{} + + +impl Conjugable for NormMapping +where + E : HasDualExponent, + F : Float, + Domain : HasDual + Norm + Space, + >::DualSpace : Norm +{ + type Conjugate<'a> = NormConjugate where Self : 'a; + + fn conjugate(&self) -> Self::Conjugate<'_> { + NormConjugate(self.exponent.dual_exponent().as_mapping()) + } +} -impl Conjugable for NormMapping -where - E : NormExponent + Clone, - F : Float, - Domain : Norm + Space, -{ +/// The zero mapping +pub struct Zero(PhantomData<(Domain, F)>); + +impl Zero { + pub fn new() -> Self { + Zero(PhantomData) + } +} + +impl Mapping for Zero { + type Codomain = F; - type DualDomain = Domain; - type Conjugate<'a> = NormConjugate where Self : 'a; + /// Compute the value of `self` at `x`. + fn apply>(&self, _x : I) -> Self::Codomain { + F::ZERO + } +} + +impl ConvexMapping for Zero { } + +impl, F : Float> Conjugable for Zero { + type Conjugate<'a> = ZeroIndicator where Self : 'a; + + #[inline] fn conjugate(&self) -> Self::Conjugate<'_> { - NormConjugate(self.clone()) + ZeroIndicator::new() } } +impl Preconjugable for Zero +where + Domain : Space, + Predual : HasDual +{ + type Preconjugate<'a> = ZeroIndicator where Self : 'a; + + #[inline] + fn preconjugate(&self) -> Self::Preconjugate<'_> { + ZeroIndicator::new() + } +} + +impl Prox for Zero { + type Prox<'a> = IdOp where Self : 'a; + + #[inline] + fn prox_mapping(&self, _τ : Self::Codomain) -> Self::Prox<'_> { + IdOp::new() + } +} + + +/// The zero indicator +pub struct ZeroIndicator(PhantomData<(Domain, F)>); + +impl ZeroIndicator { + pub fn new() -> Self { + ZeroIndicator(PhantomData) + } +} + +impl, F : Float> Mapping for ZeroIndicator { + type Codomain = F; + + /// Compute the value of `self` at `x`. + fn apply>(&self, x : I) -> Self::Codomain { + x.eval(|x̃| if x̃.is_zero() { F::ZERO } else { F::INFINITY }) + } +} + +impl, F : Float> ConvexMapping for ZeroIndicator { } + +impl, F : Float> Conjugable for ZeroIndicator { + type Conjugate<'a> = Zero where Self : 'a; + + #[inline] + fn conjugate(&self) -> Self::Conjugate<'_> { + Zero::new() + } +} + +impl Preconjugable for ZeroIndicator +where + Domain : Normed, + Predual : HasDual +{ + type Preconjugate<'a> = Zero where Self : 'a; + + #[inline] + fn preconjugate(&self) -> Self::Preconjugate<'_> { + Zero::new() + } +} diff -r 9226980e45a7 -r 848ecc05becf src/direct_product.rs --- a/src/direct_product.rs Tue Dec 31 08:30:02 2024 -0500 +++ b/src/direct_product.rs Tue Dec 31 09:02:55 2024 -0500 @@ -15,7 +15,7 @@ use crate::mapping::Space; use crate::linops::AXPY; use crate::loc::Loc; -use crate::norms::{Norm, PairNorm, NormExponent}; +use crate::norms::{Norm, PairNorm, NormExponent, Normed, HasDual, L2}; #[derive(Debug,Clone,Copy,PartialEq,Eq,Serialize,Deserialize)] pub struct Pair (pub A, pub B); @@ -468,3 +468,29 @@ } +impl Normed for Pair +where + A : Normed, + B : Normed, +{ + type NormExp = PairNorm; + + #[inline] + fn norm_exponent(&self) -> Self::NormExp { + PairNorm(self.0.norm_exponent(), self.1.norm_exponent(), L2) + } + + #[inline] + fn is_zero(&self) -> bool { + self.0.is_zero() && self.1.is_zero() + } +} + +impl HasDual for Pair +where + A : HasDual, + B : HasDual, + +{ + type DualSpace = Pair; +} diff -r 9226980e45a7 -r 848ecc05becf src/loc.rs --- a/src/loc.rs Tue Dec 31 08:30:02 2024 -0500 +++ b/src/loc.rs Tue Dec 31 09:02:55 2024 -0500 @@ -578,6 +578,31 @@ } } + +/// The default norm for `Loc` is [`L2`]. +impl Normed for Loc { + type NormExp = L2; + + #[inline] + fn norm_exponent(&self) -> Self::NormExp { + L2 + } + + // #[inline] + // fn similar_origin(&self) -> Self { + // [F::ZERO; N].into() + // } + + #[inline] + fn is_zero(&self) -> bool { + self.norm2_squared() == F::ZERO + } +} + +impl HasDual for Loc { + type DualSpace = Self; +} + impl Norm for Loc { #[inline] fn norm(&self, _ : L2) -> F { self.norm2() } @@ -588,6 +613,17 @@ fn dist(&self, other : &Self, _ : L2) -> F { self.dist2(other) } } +/* Implemented automatically as Euclidean. +impl Projection for Loc { + #[inline] + fn proj_ball_mut(&mut self, ρ : F, nrm : L2) { + let n = self.norm(nrm); + if n > ρ { + *self *= ρ/n; + } + } +}*/ + impl Norm for Loc { /// This implementation is not stabilised as it's meant to be used for very small vectors. /// Use [`nalgebra`] for larger vectors. diff -r 9226980e45a7 -r 848ecc05becf src/nalgebra_support.rs --- a/src/nalgebra_support.rs Tue Dec 31 08:30:02 2024 -0500 +++ b/src/nalgebra_support.rs Tue Dec 31 09:02:55 2024 -0500 @@ -101,6 +101,20 @@ } } +/* Implemented automatically as Euclidean. +impl Projection for Vector +where SM: StorageMut + Clone, + M : Dim, E : Scalar + Zero + One + Float + RealField, + DefaultAllocator : Allocator { + #[inline] + fn proj_ball_mut(&mut self, ρ : E, _ : L2) { + let n = self.norm(L2); + if n > ρ { + self.iter_mut().for_each(|v| *v *= ρ/n) + } + } +}*/ + impl Projection for Vector where SM: StorageMut + Clone, M : Dim, E : Scalar + Zero + One + Float + RealField, @@ -205,10 +219,41 @@ } } +/// The default norm for `Vector` is [`L2`]. +impl Normed +for Vector +where M : Dim, + S : Storage + Clone, + E : Float + Scalar + Zero + One + RealField, + DefaultAllocator : Allocator { + + type NormExp = L2; + + #[inline] + fn norm_exponent(&self) -> Self::NormExp { + L2 + } + + #[inline] + fn is_zero(&self) -> bool { + Vector::::norm_squared(self) == E::ZERO + } +} + +impl HasDual +for Vector +where M : Dim, + S : Storage + Clone, + E : Float + Scalar + Zero + One + RealField, + DefaultAllocator : Allocator { + // TODO: Doesn't work with different storage formats. + type DualSpace = Self; +} + impl Norm for Vector where M : Dim, - S : StorageMut, + S : Storage, E : Float + Scalar + Zero + One + RealField, DefaultAllocator : Allocator { @@ -221,7 +266,7 @@ impl Dist for Vector where M : Dim, - S : StorageMut, + S : Storage, E : Float + Scalar + Zero + One + RealField, DefaultAllocator : Allocator { #[inline] @@ -233,7 +278,7 @@ impl Norm for Vector where M : Dim, - S : StorageMut, + S : Storage, E : Float + Scalar + Zero + One + RealField, DefaultAllocator : Allocator { @@ -246,7 +291,7 @@ impl Dist for Vector where M : Dim, - S : StorageMut, + S : Storage, E : Float + Scalar + Zero + One + RealField, DefaultAllocator : Allocator { #[inline] @@ -258,7 +303,7 @@ impl Norm for Vector where M : Dim, - S : StorageMut, + S : Storage, E : Float + Scalar + Zero + One + RealField, DefaultAllocator : Allocator { @@ -271,7 +316,7 @@ impl Dist for Vector where M : Dim, - S : StorageMut, + S : Storage, E : Float + Scalar + Zero + One + RealField, DefaultAllocator : Allocator { #[inline] diff -r 9226980e45a7 -r 848ecc05becf src/norms.rs --- a/src/norms.rs Tue Dec 31 08:30:02 2024 -0500 +++ b/src/norms.rs Tue Dec 31 09:02:55 2024 -0500 @@ -119,7 +119,7 @@ /// /// println!("{:?}, {:?}", x.proj_ball(1.0, L2), x.proj_ball(0.5, Linfinity)); /// ``` -pub trait Projection : Norm + Euclidean +pub trait Projection : Norm + Sized where F : Float { /// Projection of `self` to the `q`-norm-ball of radius ρ. fn proj_ball(mut self, ρ : F, q : Exponent) -> Self { @@ -128,7 +128,7 @@ } /// In-place projection of `self` to the `q`-norm-ball of radius ρ. - fn proj_ball_mut(&mut self, ρ : F, _q : Exponent); + fn proj_ball_mut(&mut self, ρ : F, q : Exponent); } /*impl> Norm for E { @@ -202,3 +202,65 @@ } } +pub trait Normed : Space + Norm { + type NormExp : NormExponent; + + fn norm_exponent(&self) -> Self::NormExp; + + #[inline] + fn norm_(&self) -> F { + self.norm(self.norm_exponent()) + } + + // fn similar_origin(&self) -> Self; + + fn is_zero(&self) -> bool; +} + +pub trait HasDual : Normed { + type DualSpace : Normed; +} + +pub trait Reflexive : HasDual +where + Self::DualSpace : HasDual +{ } + + +pub trait HasDualExponent : NormExponent { + type DualExp : NormExponent; + + fn dual_exponent(&self) -> Self::DualExp; +} + +impl HasDualExponent for L2 { + type DualExp = L2; + + #[inline] + fn dual_exponent(&self) -> Self::DualExp { + L2 + } +} + +impl HasDualExponent for L1 { + type DualExp = Linfinity; + + #[inline] + fn dual_exponent(&self) -> Self::DualExp { + Linfinity + } +} + + +impl HasDualExponent for Linfinity { + type DualExp = L1; + + #[inline] + fn dual_exponent(&self) -> Self::DualExp { + L1 + } +} + + + +