| 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::Reflexive; |
7 use crate::norms::{HasDual, Norm, Normed, Reflexive, L2}; |
| 8 use crate::types::*; |
8 use crate::types::*; |
| 9 |
9 |
| 10 pub mod wrap; |
10 pub mod wrap; |
| 11 |
11 |
| 12 // TODO: Euclidean & EuclideanMut |
12 // TODO: Euclidean & EuclideanMut |
| 90 /// Trait for [`Euclidean`] spaces with dimensions known at compile time. |
90 /// Trait for [`Euclidean`] spaces with dimensions known at compile time. |
| 91 pub trait StaticEuclidean<F: Float = f64>: Euclidean<F> { |
91 pub trait StaticEuclidean<F: Float = f64>: Euclidean<F> { |
| 92 /// Returns the origin |
92 /// Returns the origin |
| 93 fn origin() -> <Self as Euclidean<F>>::PrincipalE; |
93 fn origin() -> <Self as Euclidean<F>>::PrincipalE; |
| 94 } |
94 } |
| |
95 |
| |
96 macro_rules! scalar_euclidean { |
| |
97 ($f:ident) => { |
| |
98 impl VectorSpace for $f { |
| |
99 type Field = $f; |
| |
100 type PrincipalV = $f; |
| |
101 |
| |
102 #[inline] |
| |
103 fn similar_origin(&self) -> Self::PrincipalV { |
| |
104 0.0 |
| |
105 } |
| |
106 } |
| |
107 impl AXPY for $f { |
| |
108 #[inline] |
| |
109 fn axpy<I: Instance<$f>>(&mut self, α: $f, x: I, β: $f) { |
| |
110 *self = β * *self + α * x.own() |
| |
111 } |
| |
112 |
| |
113 #[inline] |
| |
114 fn set_zero(&mut self) { |
| |
115 *self = 0.0 |
| |
116 } |
| |
117 } |
| |
118 |
| |
119 impl Norm<L2, $f> for $f { |
| |
120 fn norm(&self, _p: L2) -> $f { |
| |
121 self.abs() |
| |
122 } |
| |
123 } |
| |
124 |
| |
125 impl Normed<$f> for $f { |
| |
126 type NormExp = L2; |
| |
127 |
| |
128 fn norm_exponent(&self) -> Self::NormExp { |
| |
129 L2 |
| |
130 } |
| |
131 } |
| |
132 |
| |
133 impl HasDual<$f> for $f { |
| |
134 type DualSpace = $f; |
| |
135 |
| |
136 #[inline] |
| |
137 fn dual_origin(&self) -> $f { |
| |
138 0.0 |
| |
139 } |
| |
140 } |
| |
141 |
| |
142 impl Euclidean<$f> for $f { |
| |
143 type PrincipalE = $f; |
| |
144 |
| |
145 #[inline] |
| |
146 fn dot<I: Instance<Self>>(&self, other: I) -> $f { |
| |
147 *self * other.own() |
| |
148 } |
| |
149 |
| |
150 #[inline] |
| |
151 fn norm2_squared(&self) -> $f { |
| |
152 *self * *self |
| |
153 } |
| |
154 |
| |
155 #[inline] |
| |
156 fn dist2_squared<I: Instance<Self>>(&self, y: I) -> $f { |
| |
157 let d = *self - y.own(); |
| |
158 d * d |
| |
159 } |
| |
160 } |
| |
161 }; |
| |
162 } |
| |
163 |
| |
164 scalar_euclidean!(f64); |
| |
165 scalar_euclidean!(f32); |