src/euclidean.rs

Tue, 31 Dec 2024 08:30:02 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Tue, 31 Dec 2024 08:30:02 -0500
branch
dev
changeset 59
9226980e45a7
parent 5
59dc4c5883f4
child 62
d8305c9b6fdf
permissions
-rw-r--r--

Significantly simplify Mapping / Apply through Instance

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

mercurial