| 8 |
8 |
| 9 // |
9 // |
| 10 // Abstract norms |
10 // Abstract norms |
| 11 // |
11 // |
| 12 |
12 |
| |
13 /// An exponent for norms. |
| |
14 /// |
| |
15 // Just a collection of desirabl attributes for a marker type |
| |
16 pub trait NormExponent : Copy + Send + Sync + 'static {} |
| |
17 |
| |
18 |
| 13 /// Exponent type for the 1-[`Norm`]. |
19 /// Exponent type for the 1-[`Norm`]. |
| 14 #[derive(Copy,Debug,Clone,Serialize,Eq,PartialEq)] |
20 #[derive(Copy,Debug,Clone,Serialize,Eq,PartialEq)] |
| 15 pub struct L1; |
21 pub struct L1; |
| |
22 impl NormExponent for L1 {} |
| 16 |
23 |
| 17 /// Exponent type for the 2-[`Norm`]. |
24 /// Exponent type for the 2-[`Norm`]. |
| 18 #[derive(Copy,Debug,Clone,Serialize,Eq,PartialEq)] |
25 #[derive(Copy,Debug,Clone,Serialize,Eq,PartialEq)] |
| 19 pub struct L2; |
26 pub struct L2; |
| |
27 impl NormExponent for L2 {} |
| 20 |
28 |
| 21 /// Exponent type for the ∞-[`Norm`]. |
29 /// Exponent type for the ∞-[`Norm`]. |
| 22 #[derive(Copy,Debug,Clone,Serialize,Eq,PartialEq)] |
30 #[derive(Copy,Debug,Clone,Serialize,Eq,PartialEq)] |
| 23 pub struct Linfinity; |
31 pub struct Linfinity; |
| |
32 impl NormExponent for Linfinity {} |
| 24 |
33 |
| 25 /// Exponent type for 2,1-[`Norm`]. |
34 /// Exponent type for 2,1-[`Norm`]. |
| 26 /// (1-norm over a domain Ω, 2-norm of a vector at each point of the domain.) |
35 /// (1-norm over a domain Ω, 2-norm of a vector at each point of the domain.) |
| 27 #[derive(Copy,Debug,Clone,Serialize,Eq,PartialEq)] |
36 #[derive(Copy,Debug,Clone,Serialize,Eq,PartialEq)] |
| 28 pub struct L21; |
37 pub struct L21; |
| |
38 impl NormExponent for L21 {} |
| 29 |
39 |
| 30 /// A Huber/Moreau–Yosida smoothed [`L1`] norm. (Not a norm itself.) |
40 /// A Huber/Moreau–Yosida smoothed [`L1`] norm. (Not a norm itself.) |
| 31 /// |
41 /// |
| 32 /// The parameter γ of this type is the smoothing factor. Zero means no smoothing, and higher |
42 /// The parameter γ of this type is the smoothing factor. Zero means no smoothing, and higher |
| 33 /// values more smoothing. Behaviour with γ < 0 is undefined. |
43 /// values more smoothing. Behaviour with γ < 0 is undefined. |
| 34 #[derive(Copy,Debug,Clone,Serialize,Eq,PartialEq)] |
44 #[derive(Copy,Debug,Clone,Serialize,Eq,PartialEq)] |
| 35 pub struct HuberL1<F : Float>(pub F); |
45 pub struct HuberL1<F : Float>(pub F); |
| |
46 impl<F : Float> NormExponent for HuberL1<F> {} |
| 36 |
47 |
| 37 /// A Huber/Moreau–Yosida smoothed [`L21`] norm. (Not a norm itself.) |
48 /// A Huber/Moreau–Yosida smoothed [`L21`] norm. (Not a norm itself.) |
| 38 /// |
49 /// |
| 39 /// The parameter γ of this type is the smoothing factor. Zero means no smoothing, and higher |
50 /// The parameter γ of this type is the smoothing factor. Zero means no smoothing, and higher |
| 40 /// values more smoothing. Behaviour with γ < 0 is undefined. |
51 /// values more smoothing. Behaviour with γ < 0 is undefined. |
| 41 #[derive(Copy,Debug,Clone,Serialize,Eq,PartialEq)] |
52 #[derive(Copy,Debug,Clone,Serialize,Eq,PartialEq)] |
| 42 pub struct HuberL21<F : Float>(pub F); |
53 pub struct HuberL21<F : Float>(pub F); |
| |
54 impl<F : Float> NormExponent for HuberL21<F> {} |
| |
55 |
| 43 |
56 |
| 44 /// A normed space (type) with exponent or other type `Exponent` for the norm. |
57 /// A normed space (type) with exponent or other type `Exponent` for the norm. |
| 45 /// |
58 /// |
| 46 /// Use as |
59 /// Use as |
| 47 /// ``` |
60 /// ``` |
| 49 /// # use alg_tools::loc::Loc; |
62 /// # use alg_tools::loc::Loc; |
| 50 /// let x = Loc([1.0, 2.0, 3.0]); |
63 /// let x = Loc([1.0, 2.0, 3.0]); |
| 51 /// |
64 /// |
| 52 /// println!("{}, {} {}", x.norm(L1), x.norm(L2), x.norm(Linfinity)) |
65 /// println!("{}, {} {}", x.norm(L1), x.norm(L2), x.norm(Linfinity)) |
| 53 /// ``` |
66 /// ``` |
| 54 pub trait Norm<F, Exponent> { |
67 pub trait Norm<F : Num, Exponent : NormExponent> { |
| 55 /// Calculate the norm. |
68 /// Calculate the norm. |
| 56 fn norm(&self, _p : Exponent) -> F; |
69 fn norm(&self, _p : Exponent) -> F; |
| 57 } |
70 } |
| 58 |
71 |
| 59 /// Indicates that the `Self`-[`Norm`] is dominated by the `Exponent`-`Norm` on the space |
72 /// Indicates that the `Self`-[`Norm`] is dominated by the `Exponent`-`Norm` on the space |
| 60 /// `Elem` with the corresponding field `F`. |
73 /// `Elem` with the corresponding field `F`. |
| 61 pub trait Dominated<F : Num, Exponent, Elem> { |
74 pub trait Dominated<F : Num, Exponent : NormExponent, Elem> { |
| 62 /// Indicates the factor $c$ for the inequality $‖x‖ ≤ C ‖x‖_p$. |
75 /// Indicates the factor $c$ for the inequality $‖x‖ ≤ C ‖x‖_p$. |
| 63 fn norm_factor(&self, p : Exponent) -> F; |
76 fn norm_factor(&self, p : Exponent) -> F; |
| 64 /// Given a norm-value $‖x‖_p$, calculates $C‖x‖_p$ such that $‖x‖ ≤ C‖x‖_p$ |
77 /// Given a norm-value $‖x‖_p$, calculates $C‖x‖_p$ such that $‖x‖ ≤ C‖x‖_p$ |
| 65 #[inline] |
78 #[inline] |
| 66 fn from_norm(&self, p_norm : F, p : Exponent) -> F { |
79 fn from_norm(&self, p_norm : F, p : Exponent) -> F { |
| 67 p_norm * self.norm_factor(p) |
80 p_norm * self.norm_factor(p) |
| 68 } |
81 } |
| 69 } |
82 } |
| 70 |
83 |
| 71 /// Trait for distances with respect to a norm. |
84 /// Trait for distances with respect to a norm. |
| 72 pub trait Dist<F,Exponent> : Norm<F, Exponent> { |
85 pub trait Dist<F : Num, Exponent : NormExponent> : Norm<F, Exponent> { |
| 73 /// Calculate the distance |
86 /// Calculate the distance |
| 74 fn dist(&self, other : &Self, _p : Exponent) -> F; |
87 fn dist(&self, other : &Self, _p : Exponent) -> F; |
| 75 } |
88 } |
| 76 |
89 |
| 77 /// Trait for Euclidean projections to the `Exponent`-[`Norm`]-ball. |
90 /// Trait for Euclidean projections to the `Exponent`-[`Norm`]-ball. |
| 82 /// # use alg_tools::loc::Loc; |
95 /// # use alg_tools::loc::Loc; |
| 83 /// let x = Loc([1.0, 2.0, 3.0]); |
96 /// let x = Loc([1.0, 2.0, 3.0]); |
| 84 /// |
97 /// |
| 85 /// println!("{:?}, {:?}", x.proj_ball(1.0, L2), x.proj_ball(0.5, Linfinity)); |
98 /// println!("{:?}, {:?}", x.proj_ball(1.0, L2), x.proj_ball(0.5, Linfinity)); |
| 86 /// ``` |
99 /// ``` |
| 87 pub trait Projection<F, Exponent> : Norm<F, Exponent> + Euclidean<F> where F : Float { |
100 pub trait Projection<F : Num, Exponent : NormExponent> : Norm<F, Exponent> + Euclidean<F> |
| |
101 where F : Float { |
| 88 /// Projection of `self` to the `q`-norm-ball of radius ρ. |
102 /// Projection of `self` to the `q`-norm-ball of radius ρ. |
| 89 fn proj_ball(mut self, ρ : F, q : Exponent) -> Self { |
103 fn proj_ball(mut self, ρ : F, q : Exponent) -> Self { |
| 90 self.proj_ball_mut(ρ, q); |
104 self.proj_ball_mut(ρ, q); |
| 91 self |
105 self |
| 92 } |
106 } |