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
use crate::VectorSpace;
use num_traits::One;
pub trait Basis<const I: usize>: VectorSpace {
fn unit_basis() -> Self {
Self::basis_of(<Self as VectorSpace>::Scalar::one())
}
fn basis_of(magnitude: Self::Scalar) -> Self {
Self::unit_basis() * magnitude
}
fn basis(&self) -> Self::Scalar;
fn basis_mut(&mut self) -> &mut Self::Scalar;
fn with_basis(mut self, magnitude: Self::Scalar) -> Self {
*self.basis_mut() = magnitude;
self
}
}
pub trait Bases: VectorSpace {
#[inline]
fn unit_bases<const I: usize>() -> Self
where
Self: Basis<I>,
{
Basis::unit_basis()
}
#[inline]
fn bases_of<const I: usize>(magnitude: Self::Scalar) -> Self
where
Self: Basis<I>,
{
Basis::basis_of(magnitude)
}
#[inline]
fn bases<const I: usize>(&self) -> Self::Scalar
where
Self: Basis<I>,
{
Basis::basis(self)
}
#[inline]
fn bases_mut<const I: usize>(&mut self) -> &mut Self::Scalar
where
Self: Basis<I>,
{
Basis::basis_mut(self)
}
#[inline]
fn with_bases<const I: usize>(self, magnitude: Self::Scalar) -> Self
where
Self: Basis<I>,
{
Basis::with_basis(self, magnitude)
}
}
impl<T: VectorSpace> Bases for T {}