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