Fri, 05 Sep 2025 00:16:08 -0500
strides alt
| 5 | 1 | /*! |
| 2 | Euclidean spaces. | |
| 3 | */ | |
| 4 | ||
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
5 | use crate::instance::Instance; |
| 150 | 6 | use crate::linops::{VectorSpace, AXPY}; |
| 151 | 7 | use crate::norms::Reflexive; |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
8 | use crate::types::*; |
| 5 | 9 | |
| 146 | 10 | pub mod wrap; |
| 11 | ||
| 150 | 12 | // TODO: Euclidean & EuclideanMut |
| 13 | // | |
| 5 | 14 | /// Space (type) with Euclidean and vector space structure |
| 15 | /// | |
| 16 | /// The type should implement vector space operations (addition, subtraction, scalar | |
| 17 | /// multiplication and scalar division) along with their assignment versions, as well | |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
18 | /// as an inner product. |
| 151 | 19 | // TODO: remove F parameter, use VectorSpace::Field |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
20 | pub trait Euclidean<F: Float = f64>: |
| 164 | 21 | VectorSpace<Field = F, PrincipalV = Self::PrincipalE> + Reflexive<F, DualSpace = Self::PrincipalE> |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
22 | { |
| 164 | 23 | /// Principal form of the space; always equal to [`Space::Principal`] and |
| 24 | /// [`VectorSpace::PrincipalV`], but with more traits guaranteed. | |
| 25 | type PrincipalE: ClosedEuclidean<F>; | |
| 151 | 26 | |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
27 | // Inner product |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
28 | fn dot<I: Instance<Self>>(&self, other: I) -> F; |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
29 | |
| 5 | 30 | /// Calculate the square of the 2-norm, $\frac{1}{2}\\|x\\|_2^2$, where `self` is $x$. |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
31 | /// |
|
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
32 | /// This is not automatically implemented to avoid imposing |
|
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
33 | /// `for <'a> &'a Self : Instance<Self>` trait bound bloat. |
|
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
34 | fn norm2_squared(&self) -> F; |
| 5 | 35 | |
| 36 | /// Calculate the square of the 2-norm divided by 2, $\frac{1}{2}\\|x\\|_2^2$, | |
| 37 | /// where `self` is $x$. | |
| 38 | #[inline] | |
| 39 | fn norm2_squared_div2(&self) -> F { | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
40 | self.norm2_squared() / F::TWO |
| 5 | 41 | } |
| 42 | ||
| 43 | /// Calculate the 2-norm $‖x‖_2$, where `self` is $x$. | |
| 44 | #[inline] | |
| 45 | fn norm2(&self) -> F { | |
| 46 | self.norm2_squared().sqrt() | |
| 47 | } | |
| 48 | ||
| 49 | /// Calculate the 2-distance squared $\\|x-y\\|_2^2$, where `self` is $x$. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
50 | fn dist2_squared<I: Instance<Self>>(&self, y: I) -> F; |
| 5 | 51 | |
| 52 | /// Calculate the 2-distance $\\|x-y\\|_2$, where `self` is $x$. | |
| 53 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
54 | fn dist2<I: Instance<Self>>(&self, y: I) -> F { |
| 5 | 55 | self.dist2_squared(y).sqrt() |
| 56 | } | |
| 57 | ||
| 58 | /// Projection to the 2-ball. | |
| 59 | #[inline] | |
| 164 | 60 | fn proj_ball2(self, ρ: F) -> Self::PrincipalV { |
| 150 | 61 | let r = self.norm2(); |
| 62 | if r > ρ { | |
| 63 | self * (ρ / r) | |
| 64 | } else { | |
| 65 | self.into_owned() | |
| 66 | } | |
| 5 | 67 | } |
| 150 | 68 | } |
| 5 | 69 | |
| 151 | 70 | pub trait ClosedEuclidean<F: Float = f64>: |
| 164 | 71 | Instance<Self> + Euclidean<F, PrincipalE = Self> |
| 151 | 72 | { |
| 73 | } | |
| 164 | 74 | impl<F: Float, X: Instance<X> + Euclidean<F, PrincipalE = Self>> ClosedEuclidean<F> for X {} |
| 151 | 75 | |
| 150 | 76 | // TODO: remove F parameter, use AXPY::Field |
| 151 | 77 | pub trait EuclideanMut<F: Float = f64>: Euclidean<F> + AXPY<Field = F> { |
| 5 | 78 | /// In-place projection to the 2-ball. |
| 79 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
80 | fn proj_ball2_mut(&mut self, ρ: F) { |
| 5 | 81 | let r = self.norm2(); |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
82 | if r > ρ { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
83 | *self *= ρ / r |
| 5 | 84 | } |
| 85 | } | |
| 86 | } | |
| 87 | ||
| 151 | 88 | impl<X, F: Float> EuclideanMut<F> for X where X: Euclidean<F> + AXPY<Field = F> {} |
| 150 | 89 | |
| 5 | 90 | /// Trait for [`Euclidean`] spaces with dimensions known at compile time. |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
91 | pub trait StaticEuclidean<F: Float = f64>: Euclidean<F> { |
| 5 | 92 | /// Returns the origin |
| 164 | 93 | fn origin() -> <Self as VectorSpace>::PrincipalV; |
| 5 | 94 | } |