src/euclidean.rs

Tue, 20 Feb 2024 12:33:16 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Tue, 20 Feb 2024 12:33:16 -0500
changeset 25
d14c877e14b7
parent 5
59dc4c5883f4
permissions
-rw-r--r--

Logarithmic logging base correction

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

mercurial