Mon, 12 May 2025 17:10:39 -0500
euclidean simplify fail
| 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; |
| 131 | 6 | use crate::linops::AXPY; |
| 7 | use crate::norms::{HasDual, NormExponent, Normed, Reflexive, L2}; | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
8 | use crate::types::*; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
9 | use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; |
| 5 | 10 | |
| 11 | /// Space (type) with Euclidean and vector space structure | |
| 12 | /// | |
| 13 | /// The type should implement vector space operations (addition, subtraction, scalar | |
| 14 | /// 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
|
15 | /// as an inner product. |
| 131 | 16 | pub trait Euclidean: |
| 17 | AXPY<Owned = Self::Owned_> | |
| 18 | + HasDual<Self::Field, DualSpace = Self> | |
| 19 | //+ Normed<Self::Field, NormExp = L2> | |
| 20 | + Reflexive<Self::Field> | |
| 21 | + Mul<Self::Field, Output = <Self as AXPY>::Owned> | |
| 22 | + MulAssign<Self::Field> | |
| 23 | + Div<Self::Field, Output = <Self as AXPY>::Owned> | |
| 24 | + DivAssign<Self::Field> | |
| 25 | + Add<Self, Output = <Self as AXPY>::Owned> | |
| 26 | + Sub<Self, Output = <Self as AXPY>::Owned> | |
| 27 | + for<'b> Add<&'b Self, Output = <Self as AXPY>::Owned> | |
| 28 | + for<'b> Sub<&'b Self, Output = <Self as AXPY>::Owned> | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
29 | + AddAssign<Self> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
30 | + for<'b> AddAssign<&'b Self> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
31 | + SubAssign<Self> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
32 | + for<'b> SubAssign<&'b Self> |
| 131 | 33 | + Neg<Output = <Self as AXPY>::Owned> |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
34 | { |
| 131 | 35 | type Owned_: Euclidean<Field = Self::Field>; |
| 5 | 36 | |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
37 | // Inner product |
| 131 | 38 | fn dot<I: Instance<Self>>(&self, other: I) -> Self::Field; |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
39 | |
| 5 | 40 | /// 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
|
41 | /// |
|
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
42 | /// 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
|
43 | /// `for <'a> &'a Self : Instance<Self>` trait bound bloat. |
| 131 | 44 | fn norm2_squared(&self) -> Self::Field; |
| 5 | 45 | |
| 46 | /// Calculate the square of the 2-norm divided by 2, $\frac{1}{2}\\|x\\|_2^2$, | |
| 47 | /// where `self` is $x$. | |
| 48 | #[inline] | |
| 131 | 49 | fn norm2_squared_div2(&self) -> Self::Field { |
| 50 | self.norm2_squared() / Self::Field::TWO | |
| 5 | 51 | } |
| 52 | ||
| 53 | /// Calculate the 2-norm $‖x‖_2$, where `self` is $x$. | |
| 54 | #[inline] | |
| 131 | 55 | fn norm2(&self) -> Self::Field { |
| 5 | 56 | self.norm2_squared().sqrt() |
| 57 | } | |
| 58 | ||
| 59 | /// Calculate the 2-distance squared $\\|x-y\\|_2^2$, where `self` is $x$. | |
| 131 | 60 | fn dist2_squared<I: Instance<Self>>(&self, y: I) -> Self::Field; |
| 5 | 61 | |
| 62 | /// Calculate the 2-distance $\\|x-y\\|_2$, where `self` is $x$. | |
| 63 | #[inline] | |
| 131 | 64 | fn dist2<I: Instance<Self>>(&self, y: I) -> Self::Field { |
| 5 | 65 | self.dist2_squared(y).sqrt() |
| 66 | } | |
| 67 | ||
| 68 | /// Projection to the 2-ball. | |
| 69 | #[inline] | |
| 131 | 70 | fn proj_ball2(mut self, ρ: Self::Field) -> Self { |
| 5 | 71 | self.proj_ball2_mut(ρ); |
| 72 | self | |
| 73 | } | |
| 74 | ||
| 75 | /// In-place projection to the 2-ball. | |
| 76 | #[inline] | |
| 131 | 77 | fn proj_ball2_mut(&mut self, ρ: Self::Field) { |
| 5 | 78 | let r = self.norm2(); |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
79 | if r > ρ { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
80 | *self *= ρ / r |
| 5 | 81 | } |
| 82 | } | |
| 83 | } | |
| 84 | ||
| 85 | /// Trait for [`Euclidean`] spaces with dimensions known at compile time. | |
| 131 | 86 | pub trait StaticEuclidean: Euclidean { |
| 5 | 87 | /// Returns the origin |
| 131 | 88 | fn origin() -> <Self as AXPY>::Owned; |
| 5 | 89 | } |