pub struct Cartesian<const N: usize> {
pub coordinates: [f64; N],
}Expand description
A Vector represented by N f64 coordinates.
Cartesian is the canonical implementation of InnerProduct.
§Constructing vectors
The default is the 0 vector:
use hoomd_vector::Cartesian;
let v = Cartesian::<3>::default();
assert_eq!(v, [0.0; 3].into())Create a vector with an array of coordinates:
use hoomd_vector::Cartesian;
let v = Cartesian::from([1.0, 2.0, 3.0, 4.0, 5.0]);2D and 3D vectors can also be initialized from tuples:
use hoomd_vector::Cartesian;
let a = Cartesian::from((1.0, 2.0, 3.0));
let b = Cartesian::from((4.0, 5.0));Construct a random vector in the [-1, 1] hypercube:
use hoomd_vector::Cartesian;
use rand::{RngExt, SeedableRng, rngs::StdRng};
let mut rng = StdRng::seed_from_u64(1);
let v: Cartesian<3> = rng.random();§Operating on vectors
Use vector math operations when you can:
use hoomd_vector::{Cartesian, InnerProduct};
let a = Cartesian::from([1.0, 2.0]);
let b = Cartesian::from([4.0, 8.0]);
let c = (a + b).dot(&a);Access the coordinates directly when needed:
use hoomd_vector::Cartesian;
let a = Cartesian::from((1.0, 2.0));
let b = Cartesian::from((a[1], 0.0));Compute the sum of an iterator over vectors:
use hoomd_vector::Cartesian;
let total: Cartesian<2> =
[Cartesian::from((1.0, 2.0)), Cartesian::from((3.0, 4.0))]
.into_iter()
.sum();Fields§
§coordinates: [f64; N]The vector’s coordinates.
Implementations§
Source§impl Cartesian<4>
impl Cartesian<4>
Sourcepub fn counary_cross(vectors: &[Self; 3]) -> Self
pub fn counary_cross(vectors: &[Self; 3]) -> Self
Compute the N-1-ary (co-unary) cross product of a four-dimensional vector.
This function is a generalization of the cross product to general dimension,
identifying a unique vector perpendicular to N-1 other vectors. In two
dimensions, this is Cartesian::<2>::perpendicular, and in three dimensions this is
simply Cross.
In geometric algebra terms, this is the Hodge dual of the exterior (Wedge) product. Geometrically, the dot product of the resulting vector with all inputs is zero and the magnitude of the vector is the hypervolume of the parallelepiped spanned by the inputs.
Source§impl Cartesian<2>
impl Cartesian<2>
Sourcepub fn perpendicular(self) -> Self
pub fn perpendicular(self) -> Self
Construct a 2-vector perpendicular to self.
Given a vector $(v_x, v_y)$ perpendicular returns the vector
rotated by $\pi/2$:
(-v_y, v_x)§Example
use hoomd_vector::Cartesian;
let v = Cartesian::from([1.0, -4.5]);
assert_eq!(v.perpendicular(), [4.5, 1.0].into());Source§impl<const N: usize> Cartesian<N>
impl<const N: usize> Cartesian<N>
Sourcepub fn to_row_matrix(self) -> Matrix<1, N>
pub fn to_row_matrix(self) -> Matrix<1, N>
Convert a Cartesian<N> into a row matrix Matrix<1, N>.
§Example
use hoomd_linear_algebra::matrix::Matrix;
use hoomd_vector::Cartesian;
let a = Cartesian::from([1.0, -2.0, 3.0]);
let b = a.to_row_matrix();
assert_eq!(b.rows, [[1.0, -2.0, 3.0]]);Sourcepub fn to_column_matrix(self) -> Matrix<N, 1>
pub fn to_column_matrix(self) -> Matrix<N, 1>
Convert a Cartesian<N> into a column matrix Matrix<N, 1>.
§Example
use hoomd_linear_algebra::matrix::Matrix;
use hoomd_vector::Cartesian;
let a = Cartesian::from([1.0, -2.0, 3.0]);
let b = a.to_column_matrix();
assert_eq!(b.rows, [[1.0], [-2.0], [3.0]]);Trait Implementations§
Source§impl<const N: usize> AbsDiffEq for Cartesian<N>
impl<const N: usize> AbsDiffEq for Cartesian<N>
Source§fn default_epsilon() -> Self::Epsilon
fn default_epsilon() -> Self::Epsilon
Source§fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool
§fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
AbsDiffEq::abs_diff_eq].Source§impl<const N: usize> AddAssign for Cartesian<N>
impl<const N: usize> AddAssign for Cartesian<N>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl<'de, const N: usize> Deserialize<'de> for Cartesian<N>
impl<'de, const N: usize> Deserialize<'de> for Cartesian<N>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<const N: usize> Distribution<Cartesian<N>> for Ball
impl<const N: usize> Distribution<Cartesian<N>> for Ball
Source§fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Cartesian<N>
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Cartesian<N>
T, using rng as the source of randomness.§fn sample_iter<R>(self, rng: R) -> Iter<Self, R, T>where
R: Rng,
Self: Sized,
fn sample_iter<R>(self, rng: R) -> Iter<Self, R, T>where
R: Rng,
Self: Sized,
T, using rng as
the source of randomness. Read moreSource§impl<const N: usize> Distribution<Cartesian<N>> for StandardUniform
impl<const N: usize> Distribution<Cartesian<N>> for StandardUniform
Source§fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Cartesian<N>
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Cartesian<N>
Sample a Cartesian vector from the uniform [-1, 1] hypercube.
Each coordinate in the vector is in the closed range [-1, 1].
§Example
use hoomd_vector::Cartesian;
use rand::{RngExt, SeedableRng, rngs::StdRng};
let mut rng = StdRng::seed_from_u64(1);
let v: Cartesian<3> = rng.random();§fn sample_iter<R>(self, rng: R) -> Iter<Self, R, T>where
R: Rng,
Self: Sized,
fn sample_iter<R>(self, rng: R) -> Iter<Self, R, T>where
R: Rng,
Self: Sized,
T, using rng as
the source of randomness. Read moreSource§impl<const N: usize> DivAssign<f64> for Cartesian<N>
impl<const N: usize> DivAssign<f64> for Cartesian<N>
Source§fn div_assign(&mut self, rhs: f64)
fn div_assign(&mut self, rhs: f64)
/= operation. Read moreSource§impl<const N: usize> InnerProduct for Cartesian<N>
impl<const N: usize> InnerProduct for Cartesian<N>
Source§fn default_unit() -> Unit<Self>
fn default_unit() -> Unit<Self>
Create a unit vector in the space.
The default unit vector in Cartesian space is [0.0, 0.0, ...., 1.0].
§Example
use hoomd_vector::{Cartesian, InnerProduct};
let u = Cartesian::<2>::default_unit();
assert_eq!(*u.get(), [0.0, 1.0].into());
let u = Cartesian::<3>::default_unit();
assert_eq!(*u.get(), [0.0, 0.0, 1.0].into());Source§fn dot(&self, other: &Self) -> f64
fn dot(&self, other: &Self) -> f64
Source§fn norm_squared(&self) -> f64
fn norm_squared(&self) -> f64
Source§fn to_unit(self) -> Result<(Unit<Self>, f64), Error>
fn to_unit(self) -> Result<(Unit<Self>, f64), Error>
Source§impl<const N: usize> IntoIterator for Cartesian<N>
impl<const N: usize> IntoIterator for Cartesian<N>
Source§impl<const N: usize> Metric for Cartesian<N>
impl<const N: usize> Metric for Cartesian<N>
Source§fn distance_squared(&self, other: &Self) -> f64
fn distance_squared(&self, other: &Self) -> f64
Computes the squared distance between two points in Euclidean space.
d^2(\vec{x},\vec{y}) = \sum_{i=1}^{N} (x_i - y_i)^2§Example
use hoomd_vector::{Cartesian, Metric};
let x = Cartesian::from([0.0, 1.0, 1.0]);
let y = Cartesian::from([1.0, 0.0, 0.0]);
assert_eq!(3.0, x.distance_squared(&y));Source§fn n_dimensions() -> usize
fn n_dimensions() -> usize
Return the number of dimensions in this Cartesian vector space.
§Example
use hoomd_vector::{Cartesian, Metric};
assert_eq!(2, Cartesian::<2>::n_dimensions());
assert_eq!(3, Cartesian::<3>::n_dimensions());Source§impl<const N: usize> MulAssign<f64> for Cartesian<N>
impl<const N: usize> MulAssign<f64> for Cartesian<N>
Source§fn mul_assign(&mut self, rhs: f64)
fn mul_assign(&mut self, rhs: f64)
*= operation. Read moreSource§impl<const N: usize> RelativeEq for Cartesian<N>
impl<const N: usize> RelativeEq for Cartesian<N>
Source§fn default_max_relative() -> Self::Epsilon
fn default_max_relative() -> Self::Epsilon
Source§fn relative_eq(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool
fn relative_eq( &self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool
§fn relative_ne(
&self,
other: &Rhs,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool
fn relative_ne( &self, other: &Rhs, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool
RelativeEq::relative_eq].Source§impl Rotate<Cartesian<2>> for Angle
impl Rotate<Cartesian<2>> for Angle
Source§fn rotate(&self, vector: &Cartesian<2>) -> Cartesian<2>
fn rotate(&self, vector: &Cartesian<2>) -> Cartesian<2>
Rotate a Cartesian<2> in the plane by an Angle
§Example
use approxim::assert_relative_eq;
use hoomd_vector::{Angle, Cartesian, Rotate, Rotation};
use std::f64::consts::PI;
let v = Cartesian::from([-1.0, 0.0]);
let a = Angle::from(PI / 2.0);
let rotated = a.rotate(&v);
assert_relative_eq!(rotated, [0.0, -1.0].into());Source§type Matrix = RotationMatrix<2>
type Matrix = RotationMatrix<2>
Source§impl Rotate<Cartesian<3>> for Versor
impl Rotate<Cartesian<3>> for Versor
Source§fn rotate(&self, vector: &Cartesian<3>) -> Cartesian<3>
fn rotate(&self, vector: &Cartesian<3>) -> Cartesian<3>
Rotate a Cartesian<3> by a Versor
\mathbf{q} \vec{a} \mathbf{q}^*§Example
use approxim::assert_relative_eq;
use hoomd_vector::{Cartesian, Rotate, Rotation, Versor};
use std::f64::consts::PI;
let a = Cartesian::from([-1.0, 0.0, 0.0]);
let v = Versor::from_axis_angle([0.0, 0.0, 1.0].try_into()?, PI / 2.0);
let b = v.rotate(&a);
assert_relative_eq!(b, [0.0, -1.0, 0.0].into());Source§type Matrix = RotationMatrix<3>
type Matrix = RotationMatrix<3>
Source§impl<const N: usize> Rotate<Cartesian<N>> for RotationMatrix<N>
impl<const N: usize> Rotate<Cartesian<N>> for RotationMatrix<N>
Source§fn rotate(&self, vector: &Cartesian<N>) -> Cartesian<N>
fn rotate(&self, vector: &Cartesian<N>) -> Cartesian<N>
Rotate a Cartesian<N> by a RotationMatrix
§Examples
use approxim::assert_relative_eq;
use hoomd_vector::{Angle, Cartesian, Rotate, RotationMatrix};
use std::f64::consts::PI;
let v = Cartesian::from([-1.0, 0.0]);
let a = Angle::from(PI / 2.0);
let matrix = RotationMatrix::from(a);
let rotated = matrix.rotate(&v);
assert_relative_eq!(rotated, [0.0, -1.0].into());use approxim::assert_relative_eq;
use hoomd_vector::{Cartesian, Rotate, RotationMatrix, Versor};
use std::f64::consts::PI;
let a = Cartesian::from([-1.0, 0.0, 0.0]);
let v = Versor::from_axis_angle([0.0, 0.0, 1.0].try_into()?, PI / 2.0);
let matrix = RotationMatrix::from(v);
let b = matrix.rotate(&a);
assert_relative_eq!(b, [0.0, -1.0, 0.0].into());Source§type Matrix = RotationMatrix<N>
type Matrix = RotationMatrix<N>
Source§impl<const N: usize> SubAssign for Cartesian<N>
impl<const N: usize> SubAssign for Cartesian<N>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read moreSource§impl<const N: usize> TryFrom<Range<usize>> for Cartesian<N>
impl<const N: usize> TryFrom<Range<usize>> for Cartesian<N>
Source§impl<const N: usize> TryFrom<Vec<f64>> for Cartesian<N>
impl<const N: usize> TryFrom<Vec<f64>> for Cartesian<N>
Source§fn try_from(value: Vec<f64>) -> Result<Self, Self::Error>
fn try_from(value: Vec<f64>) -> Result<Self, Self::Error>
Create a Cartesian vector with coordinates given by a Vec<f64>
§Example
use hoomd_vector::Cartesian;
let v = Cartesian::<3>::try_from(vec![3.0, 4.0, 5.0])?;
assert_eq!(v, [3.0, 4.0, 5.0].into());This method deallocates the Vec after copying it.
Use Cartesian::From<[f64; N]> in performance critical code.
Source§impl Wedge for Cartesian<2>
impl Wedge for Cartesian<2>
Source§impl Wedge for Cartesian<3>
impl Wedge for Cartesian<3>
Source§fn wedge(&self, other: &Self) -> Self::Bivector
fn wedge(&self, other: &Self) -> Self::Bivector
Compute the wedge product of two vectors.
\textbf{A}=\textbf{a}\wedge{\textbf{b}}§Example
use hoomd_vector::{Cartesian, Wedge};
let a = Cartesian::from([1.0, 0.0, 0.0]);
let b = Cartesian::from([0.0, 1.0, 0.0]);
assert_eq!(a.wedge(&b), [0.0, 0.0, 1.0].into());