Tue, 31 Dec 2024 09:12:43 -0500
Try to have Field as member type in Mappings etc.
5 | 1 | /*! |
2 | Euclidean spaces. | |
3 | */ | |
4 | ||
5 | use crate::types::*; | |
6 | use std::ops::{Mul, MulAssign, Div, DivAssign, Add, Sub, AddAssign, SubAssign, Neg}; | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
7 | pub use crate::types::{HasScalarField, HasRealField}; |
5 | 8 | |
9 | /// Space (type) with a defined dot product. | |
10 | /// | |
11 | /// `U` is the space of the multiplier, and `F` the space of scalars. | |
12 | /// Since `U` ≠ `Self`, this trait can also implement dual products. | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
13 | pub trait Dot<U> : HasScalarField { |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
14 | fn dot(&self, other : U) -> Self::Field; |
5 | 15 | } |
16 | ||
17 | /// Space (type) with Euclidean and vector space structure | |
18 | /// | |
19 | /// The type should implement vector space operations (addition, subtraction, scalar | |
20 | /// multiplication and scalar division) along with their assignment versions, as well | |
21 | /// as the [`Dot`] product with respect to `Self`. | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
22 | pub trait Euclidean : Sized |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
23 | + HasRealField |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
24 | + Dot<Self> + for<'a> Dot<&'a Self> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
25 | + Mul<Self::Field, Output=<Self as Euclidean>::Output> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
26 | + MulAssign<Self::Field> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
27 | + Div<Self::Field, Output=<Self as Euclidean>::Output> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
28 | + DivAssign<Self::Field> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
29 | + Add<Self, Output=<Self as Euclidean>::Output> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
30 | + Sub<Self, Output=<Self as Euclidean>::Output> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
31 | + for<'b> Add<&'b Self, Output=<Self as Euclidean>::Output> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
32 | + for<'b> Sub<&'b Self, Output=<Self as Euclidean>::Output> |
5 | 33 | + AddAssign<Self> + for<'b> AddAssign<&'b Self> |
34 | + SubAssign<Self> + for<'b> SubAssign<&'b Self> | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
35 | + Neg<Output=<Self as Euclidean>::Output> { |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
36 | |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
37 | type Output : Euclidean<Field = Self::Field>; |
5 | 38 | |
39 | /// Returns origin of same dimensions as `self`. | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
40 | fn similar_origin(&self) -> <Self as Euclidean>::Output; |
5 | 41 | |
42 | /// Calculate the square of the 2-norm, $\frac{1}{2}\\|x\\|_2^2$, where `self` is $x$. | |
43 | #[inline] | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
44 | fn norm2_squared(&self) -> Self::Field { |
5 | 45 | self.dot(self) |
46 | } | |
47 | ||
48 | /// Calculate the square of the 2-norm divided by 2, $\frac{1}{2}\\|x\\|_2^2$, | |
49 | /// where `self` is $x$. | |
50 | #[inline] | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
51 | fn norm2_squared_div2(&self) -> Self::Field { |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
52 | self.norm2_squared()/Self::Field::TWO |
5 | 53 | } |
54 | ||
55 | /// Calculate the 2-norm $‖x‖_2$, where `self` is $x$. | |
56 | #[inline] | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
57 | fn norm2(&self) -> Self::Field { |
5 | 58 | self.norm2_squared().sqrt() |
59 | } | |
60 | ||
61 | /// Calculate the 2-distance squared $\\|x-y\\|_2^2$, where `self` is $x$. | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
62 | fn dist2_squared(&self, y : &Self) -> Self::Field; |
5 | 63 | |
64 | /// Calculate the 2-distance $\\|x-y\\|_2$, where `self` is $x$. | |
65 | #[inline] | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
66 | fn dist2(&self, y : &Self) -> Self::Field { |
5 | 67 | self.dist2_squared(y).sqrt() |
68 | } | |
69 | ||
70 | /// Projection to the 2-ball. | |
71 | #[inline] | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
72 | fn proj_ball2(mut self, ρ : Self::Field) -> Self { |
5 | 73 | self.proj_ball2_mut(ρ); |
74 | self | |
75 | } | |
76 | ||
77 | /// In-place projection to the 2-ball. | |
78 | #[inline] | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
79 | fn proj_ball2_mut(&mut self, ρ : Self::Field) { |
5 | 80 | let r = self.norm2(); |
81 | if r>ρ { | |
82 | *self *= ρ/r | |
83 | } | |
84 | } | |
85 | } | |
86 | ||
87 | /// Trait for [`Euclidean`] spaces with dimensions known at compile time. | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
88 | pub trait StaticEuclidean : Euclidean { |
5 | 89 | /// Returns the origin |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
90 | fn origin() -> <Self as Euclidean>::Output; |
5 | 91 | } |