src/euclidean.rs

branch
dev
changeset 151
402d717bb5c0
parent 150
c4e394a9c84c
child 164
fd9dba51afd3
equal deleted inserted replaced
150:c4e394a9c84c 151:402d717bb5c0
2 Euclidean spaces. 2 Euclidean spaces.
3 */ 3 */
4 4
5 use crate::instance::Instance; 5 use crate::instance::Instance;
6 use crate::linops::{VectorSpace, AXPY}; 6 use crate::linops::{VectorSpace, AXPY};
7 use crate::norms::{HasDual, Reflexive}; 7 use crate::norms::Reflexive;
8 use crate::types::*; 8 use crate::types::*;
9 use std::ops::{Add, AddAssign, Sub, SubAssign};
10 9
11 pub mod wrap; 10 pub mod wrap;
12 11
13 // TODO: Euclidean & EuclideanMut 12 // TODO: Euclidean & EuclideanMut
14 // 13 //
15 /// Space (type) with Euclidean and vector space structure 14 /// Space (type) with Euclidean and vector space structure
16 /// 15 ///
17 /// The type should implement vector space operations (addition, subtraction, scalar 16 /// The type should implement vector space operations (addition, subtraction, scalar
18 /// multiplication and scalar division) along with their assignment versions, as well 17 /// multiplication and scalar division) along with their assignment versions, as well
19 /// as an inner product. 18 /// as an inner product.
20 // TODO: remove F parameter, use AXPY::Field 19 // TODO: remove F parameter, use VectorSpace::Field
21 pub trait Euclidean<F: Float = f64>: 20 pub trait Euclidean<F: Float = f64>:
22 HasDual<F, DualSpace = Self> 21 VectorSpace<Field = F, Owned = Self::OwnedEuclidean>
23 + VectorSpace<Field = F> 22 + Reflexive<F, DualSpace = Self::OwnedEuclidean>
24 + Reflexive<F>
25 // TODO: move the following to AXPY
26 + for<'b> Add<&'b Self, Output = <Self as VectorSpace>::Owned>
27 + for<'b> Sub<&'b Self, Output = <Self as VectorSpace>::Owned>
28 + for<'b> AddAssign<&'b Self>
29 + for<'b> SubAssign<&'b Self>
30 { 23 {
24 type OwnedEuclidean: ClosedEuclidean<F>;
25
31 // Inner product 26 // Inner product
32 fn dot<I: Instance<Self>>(&self, other: I) -> F; 27 fn dot<I: Instance<Self>>(&self, other: I) -> F;
33 28
34 /// Calculate the square of the 2-norm, $\frac{1}{2}\\|x\\|_2^2$, where `self` is $x$. 29 /// Calculate the square of the 2-norm, $\frac{1}{2}\\|x\\|_2^2$, where `self` is $x$.
35 /// 30 ///
69 self.into_owned() 64 self.into_owned()
70 } 65 }
71 } 66 }
72 } 67 }
73 68
69 pub trait ClosedEuclidean<F: Float = f64>:
70 Instance<Self> + Euclidean<F, OwnedEuclidean = Self>
71 {
72 }
73 impl<F: Float, X: Instance<X> + Euclidean<F, OwnedEuclidean = Self>> ClosedEuclidean<F> for X {}
74
74 // TODO: remove F parameter, use AXPY::Field 75 // TODO: remove F parameter, use AXPY::Field
75 pub trait EuclideanMut<F: Float = f64>: 76 pub trait EuclideanMut<F: Float = f64>: Euclidean<F> + AXPY<Field = F> {
76 Euclidean<F> + AXPY<Field = F> + for<'b> AddAssign<&'b Self> + for<'b> SubAssign<&'b Self>
77 {
78 /// In-place projection to the 2-ball. 77 /// In-place projection to the 2-ball.
79 #[inline] 78 #[inline]
80 fn proj_ball2_mut(&mut self, ρ: F) { 79 fn proj_ball2_mut(&mut self, ρ: F) {
81 let r = self.norm2(); 80 let r = self.norm2();
82 if r > ρ { 81 if r > ρ {
83 *self *= ρ / r 82 *self *= ρ / r
84 } 83 }
85 } 84 }
86 } 85 }
87 86
88 impl<X, F: Float> EuclideanMut<F> for X where 87 impl<X, F: Float> EuclideanMut<F> for X where X: Euclidean<F> + AXPY<Field = F> {}
89 X: Euclidean<F> + AXPY<Field = F> + for<'b> AddAssign<&'b Self> + for<'b> SubAssign<&'b Self>
90 {
91 }
92 88
93 /// Trait for [`Euclidean`] spaces with dimensions known at compile time. 89 /// Trait for [`Euclidean`] spaces with dimensions known at compile time.
94 pub trait StaticEuclidean<F: Float = f64>: Euclidean<F> { 90 pub trait StaticEuclidean<F: Float = f64>: Euclidean<F> {
95 /// Returns the origin 91 /// Returns the origin
96 fn origin() -> <Self as VectorSpace>::Owned; 92 fn origin() -> <Self as VectorSpace>::Owned;

mercurial