src/euclidean.rs

Sat, 21 Dec 2024 23:32:20 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sat, 21 Dec 2024 23:32:20 -0500
branch
dev
changeset 62
d8305c9b6fdf
parent 59
9226980e45a7
child 63
f7b87d84864d
permissions
-rw-r--r--

Move origin stuff to AXPY form Euclidean

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 /// 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
35 #[inline]
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 fn norm2_squared(&self) -> F {
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 self.dot(self)
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 }
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 /// 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
41 /// where `self` is $x$.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 #[inline]
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 fn norm2_squared_div2(&self) -> F {
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 self.norm2_squared()/F::TWO
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 }
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 /// Calculate the 2-norm $‖x‖_2$, where `self` is $x$.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48 #[inline]
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 fn norm2(&self) -> F {
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 self.norm2_squared().sqrt()
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 }
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 /// Calculate the 2-distance squared $\\|x-y\\|_2^2$, where `self` is $x$.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 fn dist2_squared(&self, y : &Self) -> F;
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 $\\|x-y\\|_2$, where `self` is $x$.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 #[inline]
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58 fn dist2(&self, y : &Self) -> F {
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59 self.dist2_squared(y).sqrt()
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
60 }
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
61
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
62 /// Projection to the 2-ball.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
63 #[inline]
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
64 fn proj_ball2(mut self, ρ : F) -> Self {
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
65 self.proj_ball2_mut(ρ);
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
66 self
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
67 }
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
68
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
69 /// In-place projection to the 2-ball.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
70 #[inline]
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
71 fn proj_ball2_mut(&mut self, ρ : F) {
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
72 let r = self.norm2();
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
73 if r>ρ {
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
74 *self *= ρ/r
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
75 }
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
76 }
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 /// Trait for [`Euclidean`] spaces with dimensions known at compile time.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
80 pub trait StaticEuclidean<F : Float> : Euclidean<F> {
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
81 /// Returns the origin
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
82 fn origin() -> <Self as Euclidean<F>>::Output;
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
83 }

mercurial