Mon, 01 Sep 2025 13:51:03 -0500
fubar
| 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}; |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
7 | use crate::norms::{HasDual, Reflexive}; |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
8 | use crate::types::*; |
|
132
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
9 | use std::ops::{Add, AddAssign, Sub, SubAssign}; |
| 5 | 10 | |
| 146 | 11 | pub mod wrap; |
| 12 | ||
| 150 | 13 | // TODO: Euclidean & EuclideanMut |
| 14 | // | |
| 5 | 15 | /// Space (type) with Euclidean and vector space structure |
| 16 | /// | |
| 17 | /// The type should implement vector space operations (addition, subtraction, scalar | |
| 18 | /// 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
|
19 | /// as an inner product. |
|
132
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
20 | // TODO: remove F parameter, use AXPY::Field |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
21 | pub trait Euclidean<F: Float = f64>: |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
22 | HasDual<F, DualSpace = Self> |
| 150 | 23 | + VectorSpace<Field = F> |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
24 | + Reflexive<F> |
|
132
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
25 | // TODO: move the following to AXPY |
| 150 | 26 | + for<'b> Add<&'b Self, Output = <Self as VectorSpace>::Owned> |
| 27 | + for<'b> Sub<&'b Self, Output = <Self as VectorSpace>::Owned> | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
28 | + for<'b> AddAssign<&'b Self> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
29 | + for<'b> SubAssign<&'b Self> |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
30 | { |
|
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
31 | // Inner product |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
32 | 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
|
33 | |
| 5 | 34 | /// 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
|
35 | /// |
|
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
36 | /// 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
|
37 | /// `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
|
38 | fn norm2_squared(&self) -> F; |
| 5 | 39 | |
| 40 | /// Calculate the square of the 2-norm divided by 2, $\frac{1}{2}\\|x\\|_2^2$, | |
| 41 | /// where `self` is $x$. | |
| 42 | #[inline] | |
| 43 | fn norm2_squared_div2(&self) -> F { | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
44 | self.norm2_squared() / F::TWO |
| 5 | 45 | } |
| 46 | ||
| 47 | /// Calculate the 2-norm $‖x‖_2$, where `self` is $x$. | |
| 48 | #[inline] | |
| 49 | fn norm2(&self) -> F { | |
| 50 | self.norm2_squared().sqrt() | |
| 51 | } | |
| 52 | ||
| 53 | /// 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
|
54 | fn dist2_squared<I: Instance<Self>>(&self, y: I) -> F; |
| 5 | 55 | |
| 56 | /// Calculate the 2-distance $\\|x-y\\|_2$, where `self` is $x$. | |
| 57 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
58 | fn dist2<I: Instance<Self>>(&self, y: I) -> F { |
| 5 | 59 | self.dist2_squared(y).sqrt() |
| 60 | } | |
| 61 | ||
| 62 | /// Projection to the 2-ball. | |
| 63 | #[inline] | |
| 150 | 64 | fn proj_ball2(self, ρ: F) -> Self::Owned { |
| 65 | let r = self.norm2(); | |
| 66 | if r > ρ { | |
| 67 | self * (ρ / r) | |
| 68 | } else { | |
| 69 | self.into_owned() | |
| 70 | } | |
| 5 | 71 | } |
| 150 | 72 | } |
| 5 | 73 | |
| 150 | 74 | // TODO: remove F parameter, use AXPY::Field |
| 75 | pub trait EuclideanMut<F: Float = f64>: | |
| 76 | Euclidean<F> + AXPY<Field = F> + for<'b> AddAssign<&'b Self> + for<'b> SubAssign<&'b Self> | |
| 77 | { | |
| 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 | ||
| 150 | 88 | impl<X, F: Float> EuclideanMut<F> for X where |
| 89 | X: Euclidean<F> + AXPY<Field = F> + for<'b> AddAssign<&'b Self> + for<'b> SubAssign<&'b Self> | |
| 90 | { | |
| 91 | } | |
| 92 | ||
| 5 | 93 | /// 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
|
94 | pub trait StaticEuclidean<F: Float = f64>: Euclidean<F> { |
| 5 | 95 | /// Returns the origin |
| 150 | 96 | fn origin() -> <Self as VectorSpace>::Owned; |
| 5 | 97 | } |