vector_space/
vector.rs

1use num_traits::{real::Real, Zero};
2use std::{
3    cmp::PartialOrd,
4    ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign},
5};
6
7/// This trait specifies some type to be a vector type.
8/// It specifies the scalar type and is required for other vector types.
9pub trait VectorSpace: Copy + Zero + PartialEq
10where
11    Self: Add<Output = Self>,
12    Self: Sub<Output = Self>,
13    Self: Mul<<Self as VectorSpace>::Scalar, Output = Self>,
14    Self: Div<<Self as VectorSpace>::Scalar, Output = Self>,
15    Self: Neg<Output = Self>,
16{
17    /// The scalar type of the vector space.
18    type Scalar: Real + PartialOrd;
19}
20
21impl VectorSpace for f32 {
22    type Scalar = f32;
23}
24impl VectorSpace for f64 {
25    type Scalar = f64;
26}
27
28/// This trait is automatically implemented for vector spaces, which also implement assignment operations.
29pub trait VectorSpaceAssign:
30    VectorSpace + AddAssign + SubAssign + MulAssign<Self::Scalar> + DivAssign<Self::Scalar>
31{
32}
33impl<T> VectorSpaceAssign for T where
34    T: VectorSpace + AddAssign + SubAssign + MulAssign<Self::Scalar> + DivAssign<Self::Scalar>
35{
36}