Sun, 18 May 2025 23:15:50 -0500
Basic Pair pyo3 conversions
| 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; |
|
132
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
6 | use crate::linops::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 | |
| 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. |
|
132
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
16 | // 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
|
17 | pub trait Euclidean<F: Float = f64>: |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
18 | HasDual<F, DualSpace = Self> |
|
132
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
19 | + AXPY<Field = F> |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
20 | + Reflexive<F> |
|
132
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
21 | // TODO: move the following to AXPY |
|
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
22 | + for<'b> Add<&'b Self, Output = <Self as AXPY>::Owned> |
|
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
23 | + 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
|
24 | + for<'b> AddAssign<&'b Self> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
25 | + 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
|
26 | { |
|
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] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
60 | fn proj_ball2(mut self, ρ: F) -> Self { |
| 5 | 61 | self.proj_ball2_mut(ρ); |
| 62 | self | |
| 63 | } | |
| 64 | ||
| 65 | /// In-place projection to the 2-ball. | |
| 66 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
67 | fn proj_ball2_mut(&mut self, ρ: F) { |
| 5 | 68 | let r = self.norm2(); |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
69 | if r > ρ { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
70 | *self *= ρ / r |
| 5 | 71 | } |
| 72 | } | |
| 73 | } | |
| 74 | ||
| 75 | /// 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
|
76 | pub trait StaticEuclidean<F: Float = f64>: Euclidean<F> { |
| 5 | 77 | /// Returns the origin |
|
132
89371dc4d637
Make Euclidean depend on AXPY
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
78 | fn origin() -> <Self as AXPY>::Owned; |
| 5 | 79 | } |