1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use num;
use std::ops::{
Add, AddAssign, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Neg, Sub, SubAssign,
};
use general::{ClosedAdd, ClosedDiv, ClosedMul, Field, Module, Real};
pub trait VectorSpace: Module<Ring = <Self as VectorSpace>::Field>
{
type Field: Field;
}
pub trait NormedSpace: VectorSpace {
fn norm_squared(&self) -> Self::Field;
fn norm(&self) -> Self::Field;
fn normalize(&self) -> Self;
fn normalize_mut(&mut self) -> Self::Field;
fn try_normalize(&self, eps: Self::Field) -> Option<Self>;
fn try_normalize_mut(&mut self, eps: Self::Field) -> Option<Self::Field>;
}
pub trait InnerSpace: NormedSpace<Field = <Self as InnerSpace>::Real> {
type Real: Real;
fn inner_product(&self, other: &Self) -> Self::Real;
#[inline]
fn angle(&self, other: &Self) -> Self::Real {
let prod = self.inner_product(other);
let n1 = self.norm();
let n2 = other.norm();
if n1 == num::zero() || n2 == num::zero() {
num::zero()
} else {
let cang = prod / (n1 * n2);
if cang > num::one() {
num::zero()
} else if cang < -num::one::<Self::Real>() {
Self::Real::pi()
} else {
cang.acos()
}
}
}
}
pub trait FiniteDimVectorSpace:
VectorSpace
+ Index<usize, Output = <Self as VectorSpace>::Field>
+ IndexMut<usize, Output = <Self as VectorSpace>::Field>
{
fn dimension() -> usize;
fn canonical_basis<F: FnMut(&Self) -> bool>(mut f: F) {
for i in 0..Self::dimension() {
if !f(&Self::canonical_basis_element(i)) {
break;
}
}
}
fn canonical_basis_element(i: usize) -> Self;
fn dot(&self, other: &Self) -> Self::Field;
unsafe fn component_unchecked(&self, i: usize) -> &Self::Field;
unsafe fn component_unchecked_mut(&mut self, i: usize) -> &mut Self::Field;
}
pub trait FiniteDimInnerSpace:
InnerSpace + FiniteDimVectorSpace<Field = <Self as InnerSpace>::Real>
{
fn orthonormalize(vs: &mut [Self]) -> usize;
fn orthonormal_subspace_basis<F: FnMut(&Self) -> bool>(vs: &[Self], f: F);
}
pub trait AffineSpace:
Sized
+ Clone
+ PartialEq
+ Sub<Self, Output = <Self as AffineSpace>::Translation>
+ ClosedAdd<<Self as AffineSpace>::Translation>
{
type Translation: VectorSpace;
#[inline]
fn translate_by(&self, t: &Self::Translation) -> Self {
self.clone() + t.clone()
}
#[inline]
fn subtract(&self, right: &Self) -> Self::Translation {
self.clone() - right.clone()
}
}
pub trait EuclideanSpace: AffineSpace<Translation = <Self as EuclideanSpace>::Coordinates> +
ClosedMul<<Self as EuclideanSpace>::Real> +
ClosedDiv<<Self as EuclideanSpace>::Real> +
Neg<Output = Self> {
type Coordinates: FiniteDimInnerSpace<Real = Self::Real> +
Add<Self::Coordinates, Output = Self::Coordinates> +
AddAssign<Self::Coordinates> +
Sub<Self::Coordinates, Output = Self::Coordinates> +
SubAssign<Self::Coordinates> +
Mul<Self::Real, Output = Self::Coordinates> +
MulAssign<Self::Real> +
Div<Self::Real, Output = Self::Coordinates> +
DivAssign<Self::Real> +
Neg<Output = Self::Coordinates>;
type Real: Real;
fn origin() -> Self;
#[inline]
fn scale_by(&self, s: Self::Real) -> Self {
Self::from_coordinates(self.coordinates() * s)
}
#[inline]
fn coordinates(&self) -> Self::Coordinates {
self.subtract(&Self::origin())
}
#[inline]
fn from_coordinates(coords: Self::Coordinates) -> Self {
Self::origin().translate_by(&coords)
}
#[inline]
fn distance_squared(&self, b: &Self) -> Self::Real {
self.subtract(b).norm_squared()
}
#[inline]
fn distance(&self, b: &Self) -> Self::Real {
self.subtract(b).norm()
}
}