vector_space/
vector.rs

1use core::{
2    cmp::PartialOrd,
3    ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign},
4};
5use num_traits::{Zero, real::Real};
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: Zero + PartialEq
10where
11    Self: Add<Output = Self>
12        + Sub<Output = Self>
13        + Mul<<Self as VectorSpace>::Scalar, Output = Self>
14        + Div<<Self as VectorSpace>::Scalar, Output = Self>
15        + 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 = Self;
23}
24impl VectorSpace for f64 {
25    type Scalar = Self;
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}