src/convex.rs

Mon, 23 Dec 2024 23:27:45 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 23 Dec 2024 23:27:45 -0500
branch
dev
changeset 80
f802ddbabcfc
parent 60
848ecc05becf
permissions
-rw-r--r--

Basic arithmetric opt-in hack attempt: not allowed by Rust.

58
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 /*!
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2 Some convex analysis basics
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 */
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
5 use std::marker::PhantomData;
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
6 use crate::types::*;
80
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
7 use crate::mapping::{Mapping, Space, ArithmeticTrue};
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
8 use crate::linops::IdOp;
80
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
9 use crate::instance::{Instance, InstanceMut, DecompositionMut,};
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
10 use crate::norms::*;
58
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 /// Trait for convex mappings. Has no features, just serves as a constraint
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 ///
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 /// TODO: should constrain `Mapping::Codomain` to implement a partial order,
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 /// but this makes everything complicated with little benefit.
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
16 pub trait ConvexMapping<Domain : Space, F : Num = f64> : Mapping<Domain, Codomain = F>
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
17 {}
58
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19 /// Trait for mappings with a Fenchel conjugate
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 ///
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 /// The conjugate type has to implement [`ConvexMapping`], but a `Conjugable` mapping need
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 /// not be convex.
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
23 pub trait Conjugable<Domain : HasDual<F>, F : Num = f64> : Mapping<Domain> {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
24 type Conjugate<'a> : ConvexMapping<Domain::DualSpace, F> where Self : 'a;
58
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 fn conjugate(&self) -> Self::Conjugate<'_>;
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 }
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 /// Trait for mappings with a Fenchel preconjugate
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 ///
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 /// 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
32 /// but a `Preconjugable` mapping has to be convex.
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
33 pub trait Preconjugable<Domain, Predual, F : Num = f64> : ConvexMapping<Domain, F>
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
34 where
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
35 Domain : Space,
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
36 Predual : HasDual<F>
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
37 {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
38 type Preconjugate<'a> : Mapping<Predual> where Self : 'a;
58
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 fn preconjugate(&self) -> Self::Preconjugate<'_>;
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 }
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 /// Trait for mappings with a proximap map
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 ///
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 /// The conjugate type has to implement [`ConvexMapping`], but a `Conjugable` mapping need
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 /// not be convex.
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
47 pub trait Prox<Domain : Space> : Mapping<Domain> {
58
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48 type Prox<'a> : Mapping<Domain, Codomain=Domain> where Self : 'a;
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 /// Returns a proximal mapping with weight τ
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 fn prox_mapping(&self, τ : Self::Codomain) -> Self::Prox<'_>;
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 /// Calculate the proximal mapping with weight τ
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
54 fn prox<I : Instance<Domain>>(&self, τ : Self::Codomain, z : I) -> Domain {
58
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55 self.prox_mapping(τ).apply(z)
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 }
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
57
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
58 /// Calculate the proximal mapping with weight τ in-place
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
59 fn prox_mut<'b>(&self, τ : Self::Codomain, y : &'b mut Domain)
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
60 where
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
61 &'b mut Domain : InstanceMut<Domain>,
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
62 Domain:: Decomp : DecompositionMut<Domain>,
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
63 for<'a> &'a Domain : Instance<Domain>,
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
64 {
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
65 *y = self.prox(τ, &*y);
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
66 }
58
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
67 }
1a38447a89fa Convex analysis basics
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
68
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
69
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
70 pub struct NormConjugate<F : Float, E : NormExponent>(NormMapping<F, E>);
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
71
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
72 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
73 where
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
74 Domain : Space,
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
75 E : NormExponent,
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
76 F : Float,
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
77 Self : Mapping<Domain, Codomain=F>
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
78 {}
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
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
81 impl<F, E, Domain> Mapping<Domain> for NormConjugate<F, E>
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
82 where
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
83 Domain : Space + Norm<F, E>,
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
84 F : Float,
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
85 E : NormExponent,
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
86 {
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
87 type Codomain = F;
80
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
88 type ArithmeticOptIn = ArithmeticTrue;
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
89
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
90 fn apply<I : Instance<Domain>>(&self, d : I) -> F {
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
91 if d.eval(|x| x.norm(self.0.exponent)) <= F::ONE {
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
92 F::ZERO
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
93 } else {
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
94 F::INFINITY
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
95 }
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
96 }
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
97 }
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
98
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
99 impl<Domain, E, F> ConvexMapping<Domain, F> for NormConjugate<F, E>
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
100 where
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
101 Domain : Space,
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
102 E : NormExponent,
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
103 F : Float,
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
104 Self : Mapping<Domain, Codomain=F>
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
105 {}
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
106
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
107
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
108 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
109 where
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
110 E : HasDualExponent,
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
111 F : Float,
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
112 Domain : HasDual<F> + Norm<F, E> + Space,
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
113 <Domain as HasDual<F>>::DualSpace : Norm<F, E::DualExp>
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
114 {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
115 type Conjugate<'a> = NormConjugate<F, E::DualExp> where Self : 'a;
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
116
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
117 fn conjugate(&self) -> Self::Conjugate<'_> {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
118 NormConjugate(self.exponent.dual_exponent().as_mapping())
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
119 }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
120 }
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
121
80
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
122 impl<Domain, E, F> Prox<Domain> for NormConjugate<F, E>
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
123 where
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
124 Domain : Space + Norm<F, E>,
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
125 E : NormExponent,
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
126 F : Float,
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
127 NormProjection<F, E> : Mapping<Domain, Codomain=Domain>,
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
128 {
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
129 type Prox<'a> = NormProjection<F, E> where Self : 'a;
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
130
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
131 #[inline]
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
132 fn prox_mapping(&self, τ : Self::Codomain) -> Self::Prox<'_> {
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
133 NormProjection{ α : τ, exponent : self.0.exponent }
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
134 }
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
135 }
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
136
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
137 pub struct NormProjection<F : Float, E : NormExponent> {
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
138 α : F,
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
139 exponent : E,
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
140 }
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
141
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
142 /*
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
143 impl<F, Domain> Mapping<Domain> for NormProjection<F, L2>
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
144 where
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
145 Domain : Space + Euclidean<F> + std::ops::MulAssign<F>,
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
146 F : Float,
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
147 {
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
148 type Codomain = Domain;
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
149
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
150 fn apply<I : Instance<Domain>>(&self, d : I) -> Domain {
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
151 d.own().proj_ball2(self.α)
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
152 }
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
153 }
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
154 */
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
155
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
156 impl<F, E, Domain> Mapping<Domain> for NormProjection<F, E>
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
157 where
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
158 Domain : Space + Projection<F, E> + std::ops::MulAssign<F>,
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
159 F : Float,
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
160 E : NormExponent,
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
161 {
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
162 type Codomain = Domain;
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
163 type ArithmeticOptIn = ArithmeticTrue;
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
164
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
165 fn apply<I : Instance<Domain>>(&self, d : I) -> Domain {
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
166 d.own().proj_ball(self.α, self.exponent)
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
167 }
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
168 }
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
169
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
170 /// The zero mapping
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
171 pub struct Zero<Domain : Space, F : Num>(PhantomData<(Domain, F)>);
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
172
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
173 impl<Domain : Space, F : Num> Zero<Domain, F> {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
174 pub fn new() -> Self {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
175 Zero(PhantomData)
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
176 }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
177 }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
178
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
179 impl<Domain : Space, F : Num> Mapping<Domain> for Zero<Domain, F> {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
180 type Codomain = F;
80
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
181 type ArithmeticOptIn = ArithmeticTrue;
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
182
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
183 /// Compute the value of `self` at `x`.
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
184 fn apply<I : Instance<Domain>>(&self, _x : I) -> Self::Codomain {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
185 F::ZERO
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
186 }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
187 }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
188
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
189 impl<Domain : Space, F : Num> ConvexMapping<Domain, F> for Zero<Domain, F> { }
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
190
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
191
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
192 impl<Domain : HasDual<F>, F : Float> Conjugable<Domain, F> for Zero<Domain, F> {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
193 type Conjugate<'a> = ZeroIndicator<Domain::DualSpace, F> where Self : 'a;
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
194
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
195 #[inline]
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
196 fn conjugate(&self) -> Self::Conjugate<'_> {
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
197 ZeroIndicator::new()
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
198 }
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
199 }
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 58
diff changeset
200
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
201 impl<Domain, Predual, F : Float> Preconjugable<Domain, Predual, F> for Zero<Domain, F>
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
202 where
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
203 Domain : Space,
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
204 Predual : HasDual<F>
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
205 {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
206 type Preconjugate<'a> = ZeroIndicator<Predual, F> where Self : 'a;
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
207
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
208 #[inline]
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
209 fn preconjugate(&self) -> Self::Preconjugate<'_> {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
210 ZeroIndicator::new()
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
211 }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
212 }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
213
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
214 impl<Domain : Space + Clone, F : Num> Prox<Domain> for Zero<Domain, F> {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
215 type Prox<'a> = IdOp<Domain> where Self : 'a;
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
216
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
217 #[inline]
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
218 fn prox_mapping(&self, _τ : Self::Codomain) -> Self::Prox<'_> {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
219 IdOp::new()
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 }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
222
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
223
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
224 /// The zero indicator
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
225 pub struct ZeroIndicator<Domain : Space, F : Num>(PhantomData<(Domain, F)>);
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
226
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
227 impl<Domain : Space, F : Num> ZeroIndicator<Domain, F> {
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 ZeroIndicator(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
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
233 impl<Domain : Normed<F>, F : Float> Mapping<Domain> for ZeroIndicator<Domain, F> {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
234 type Codomain = F;
80
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
235 type ArithmeticOptIn = ArithmeticTrue;
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
236
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
237 /// Compute the value of `self` at `x`.
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
238 fn apply<I : Instance<Domain>>(&self, x : I) -> Self::Codomain {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
239 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
240 }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
241 }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
242
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
243 impl<Domain : Normed<F>, F : Float> ConvexMapping<Domain, F> for ZeroIndicator<Domain, F> { }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
244
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
245 impl<Domain : HasDual<F>, F : Float> Conjugable<Domain, F> for ZeroIndicator<Domain, F> {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
246 type Conjugate<'a> = Zero<Domain::DualSpace, F> where Self : 'a;
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]
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
249 fn conjugate(&self) -> Self::Conjugate<'_> {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
250 Zero::new()
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
251 }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
252 }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
253
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
254 impl<Domain, Predual, F : Float> Preconjugable<Domain, Predual, F> for ZeroIndicator<Domain, F>
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
255 where
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
256 Domain : Normed<F>,
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
257 Predual : HasDual<F>
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
258 {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
259 type Preconjugate<'a> = Zero<Predual, F> where Self : 'a;
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
260
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
261 #[inline]
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
262 fn preconjugate(&self) -> Self::Preconjugate<'_> {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
263 Zero::new()
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 }

mercurial