pub struct Vector<T, const N: usize> { /* private fields */ }
Expand description
A compile-time n-dimensional vector, how fancy!
Implementations§
Source§impl<T: Copy, const N: usize> Vector<T, N>
impl<T: Copy, const N: usize> Vector<T, N>
Sourcepub fn num_cast<K: Num + Copy + NumCast>(&self) -> Option<Vector<K, N>>where
T: ToPrimitive,
pub fn num_cast<K: Num + Copy + NumCast>(&self) -> Option<Vector<K, N>>where
T: ToPrimitive,
Allows numerically casting each component of the vector. Makes use of the num_traits::NumCast trait. If the cast fails, None is returned.
let a = vector!(1.0, 2.0, 3.0);
let b = vector!(1, 2, 3);
assert_eq!(a.num_cast::<i32>().unwrap(), b);
Sourcepub fn try_cast<K: Num + Copy + TryFrom<T>>(
&self,
) -> Result<Vector<K, N>, <K as TryFrom<T>>::Error>
pub fn try_cast<K: Num + Copy + TryFrom<T>>( &self, ) -> Result<Vector<K, N>, <K as TryFrom<T>>::Error>
Allows casting each component of the vector using the TryFrom trait. If the cast fails, an error is returned.
In many cases Vector::num_cast
is more versatile as it supports more conversions.
For example u32
to f32
is supported by Vector::num_cast
but not by Vector::try_cast
.
let a: Vector<u32, 3> = vector!(1, 2, 3);
let b: Vector<u8, 3> = vector!(1, 2, 3);
assert_eq!(a.try_cast().unwrap(), b);
Sourcepub fn cast<K: Num + Copy + From<T>>(&self) -> Vector<K, N>
pub fn cast<K: Num + Copy + From<T>>(&self) -> Vector<K, N>
Casts each component of the vector to the given type.
This is similar to Vector::try_cast
but only works if the cast is infallible.
Because of this, it should be preferred over Vector::try_cast
when casting from smaller to larger types.
Source§impl<T: Num + Copy, const N: usize> Vector<T, N>
impl<T: Num + Copy, const N: usize> Vector<T, N>
Sourcepub fn hadamard_product(&self, other: &Self) -> Self
pub fn hadamard_product(&self, other: &Self) -> Self
Computes the Hadamard product of two vectors (component-wise multiplication).
Source§impl<T: Num + Copy + Ord, const N: usize> Vector<T, N>
impl<T: Num + Copy + Ord, const N: usize> Vector<T, N>
Sourcepub fn min_component(&self) -> T
pub fn min_component(&self) -> T
Takes the minimum component of a vector.
Sourcepub fn max_component(&self) -> T
pub fn max_component(&self) -> T
Takes the maximum component of a vector.
Source§impl<T: Num + Copy + Signed, const N: usize> Vector<T, N>
impl<T: Num + Copy + Signed, const N: usize> Vector<T, N>
Sourcepub fn opposite(&self) -> Self
pub fn opposite(&self) -> Self
Calculates the opposite of a vector. This is the vector with all components negated.
Sourcepub fn signum(&self) -> Self
pub fn signum(&self) -> Self
Calculates the sign of each component of a vector. This is -1 if the component is negative, 0 if it is zero, and 1 if it is positive.
Sourcepub fn manhattan_distance(&self, other: &Self) -> T
pub fn manhattan_distance(&self, other: &Self) -> T
Calculates the Manhattan Distance of two vectors.
Source§impl<T: Num + Copy + Sum + Real, const N: usize> Vector<T, N>
impl<T: Num + Copy + Sum + Real, const N: usize> Vector<T, N>
Sourcepub fn magnitude(&self) -> T
pub fn magnitude(&self) -> T
Calculates the magnitude of a vector. This is the square root of the sum of all squared components.
Sourcepub fn normalize(&self) -> Self
pub fn normalize(&self) -> Self
Normalizes a vector. This is the vector divided by its magnitude.
Sourcepub fn distance(&self, other: &Self) -> T
pub fn distance(&self, other: &Self) -> T
Calculates the Euclidean Distance of two vectors.
Trait Implementations§
Source§impl<T: Num + Copy, const N: usize> AddAssign for Vector<T, N>
impl<T: Num + Copy, const N: usize> AddAssign for Vector<T, N>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+=
operation. Read moreSource§impl<T: Num + Copy, const N: usize> DivAssign for Vector<T, N>
impl<T: Num + Copy, const N: usize> DivAssign for Vector<T, N>
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
/=
operation. Read moreSource§impl<T: Num + Copy, const N: usize> FromIterator<T> for Vector<T, N>
impl<T: Num + Copy, const N: usize> FromIterator<T> for Vector<T, N>
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Create a new vector from an iterator. If the iterator has less than N items, the remaining components will be zeroed. If the iterator has more than N items, the remaining items will be ignored.
Source§impl<T: Num + Copy, const N: usize> RemAssign for Vector<T, N>
impl<T: Num + Copy, const N: usize> RemAssign for Vector<T, N>
Source§fn rem_assign(&mut self, rhs: Self)
fn rem_assign(&mut self, rhs: Self)
%=
operation. Read moreSource§impl<T: Num + Copy, const N: usize> SubAssign for Vector<T, N>
impl<T: Num + Copy, const N: usize> SubAssign for Vector<T, N>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-=
operation. Read more