Skip to main content

Cartesian

Struct Cartesian 

Source
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>

Source

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>

Source

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>

Source

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]]);
Source

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>

Source§

type Epsilon = <f64 as AbsDiffEq>::Epsilon

Used for specifying relative comparisons.
Source§

fn default_epsilon() -> Self::Epsilon

The default tolerance to use when testing values that are close together. Read more
Source§

fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool

A test for equality that uses the absolute difference to compute the approximimate equality of two numbers.
§

fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool

The inverse of [AbsDiffEq::abs_diff_eq].
Source§

impl<const N: usize> Add for Cartesian<N>

Source§

type Output = Cartesian<N>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
Source§

impl<const N: usize> AddAssign for Cartesian<N>

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl<const N: usize> Clone for Cartesian<N>

Source§

fn clone(&self) -> Cartesian<N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Cross for Cartesian<3>

Source§

fn cross(&self, other: &Self) -> Self

Compute the cross product (right-handed) of two vectors: Read more
Source§

impl<const N: usize> Debug for Cartesian<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> Default for Cartesian<N>

Source§

fn default() -> Self

Create a 0 vector.

§Example
use hoomd_vector::Cartesian;

let v = Cartesian::<3>::default();
assert_eq!(v, [0.0; 3].into())
Source§

impl<'de, const N: usize> Deserialize<'de> for Cartesian<N>

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<const N: usize> Display for Cartesian<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> Distribution<Cartesian<N>> for Ball

Source§

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Cartesian<N>

Generate a random value of T, using rng as the source of randomness.
§

fn sample_iter<R>(self, rng: R) -> Iter<Self, R, T>
where R: Rng, Self: Sized,

Create an iterator that generates random values of T, using rng as the source of randomness. Read more
§

fn map<F, S>(self, func: F) -> Map<Self, F, T, S>
where F: Fn(T) -> S, Self: Sized,

Map sampled values to type S Read more
Source§

impl<const N: usize> Distribution<Cartesian<N>> for StandardUniform

Source§

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,

Create an iterator that generates random values of T, using rng as the source of randomness. Read more
§

fn map<F, S>(self, func: F) -> Map<Self, F, T, S>
where F: Fn(T) -> S, Self: Sized,

Map sampled values to type S Read more
Source§

impl<const N: usize> Div<f64> for Cartesian<N>

Source§

type Output = Cartesian<N>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f64) -> Self

Performs the / operation. Read more
Source§

impl<const N: usize> DivAssign<f64> for Cartesian<N>

Source§

fn div_assign(&mut self, rhs: f64)

Performs the /= operation. Read more
Source§

impl<const N: usize> From<[f64; N]> for Cartesian<N>

Source§

fn from(coordinates: [f64; N]) -> Self

Create a Cartesian vector with the given coordinates.

§Example
use hoomd_vector::Cartesian;

let v = Cartesian::from([4.0, 3.0]);
Source§

impl From<(f64, f64)> for Cartesian<2>

Source§

fn from(coordinates: (f64, f64)) -> Self

Converts to this type from the input type.
Source§

impl From<(f64, f64, f64)> for Cartesian<3>

Source§

fn from(coordinates: (f64, f64, f64)) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> From<Matrix<1, N>> for Cartesian<N>

Source§

fn from(value: Matrix<1, N>) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize, T> Index<T> for Cartesian<N>
where T: Into<usize> + SliceIndex<[f64], Output = f64>,

Source§

fn index(&self, index: T) -> &Self::Output

Get the value of the vector at coordinate i.

§Example
use hoomd_vector::Cartesian;

let v = Cartesian::<3>::try_from(3..6)?;
assert_eq!((v[0], v[1], v[2]), (3.0, 4.0, 5.0));
Source§

type Output = f64

The returned type after indexing.
Source§

impl<const N: usize, T> IndexMut<T> for Cartesian<N>
where T: Into<usize> + SliceIndex<[f64], Output = f64>,

Source§

fn index_mut(&mut self, index: T) -> &mut Self::Output

Get a mutable reference to the value of the vector at coordinate i.

§Example
use hoomd_vector::Cartesian;

let mut v = Cartesian::<3>::try_from(3..6)?;
assert_eq!((v[0], v[1], v[2]), (3.0, 4.0, 5.0));
v[0] += 1.0;
assert_eq!(v[0], 4.0);
Source§

impl<const N: usize> InnerProduct for Cartesian<N>

Source§

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

Compute the vector dot product between two vectors. Read more
Source§

fn norm_squared(&self) -> f64

Compute the squared norm of the vector. Read more
Source§

fn norm(&self) -> f64

Compute the norm of the vector. Read more
Source§

fn to_unit(self) -> Result<(Unit<Self>, f64), Error>

Create a vector of unit length pointing in the same direction as the given vector. Read more
Source§

fn to_unit_unchecked(self) -> (Unit<Self>, f64)

Create a vector of unit length pointing in the same direction as the given vector. Read more
Source§

fn project(&self, b: &Self) -> Self

Project one vector onto another. Read more
Source§

impl<const N: usize> IntoIterator for Cartesian<N>

Source§

fn into_iter(self) -> Self::IntoIter

Iterate over the components of the vector.

§Example
use hoomd_vector::Cartesian;

let a = Cartesian::from([1.0, 2.0]);
let mut iter = a.into_iter();

assert_eq!(iter.next(), Some(1.0));
assert_eq!(iter.next(), Some(2.0));
assert_eq!(iter.next(), None);
Source§

type Item = f64

The type of the elements being iterated over.
Source§

type IntoIter = <[f64; N] as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?
Source§

impl<const N: usize> Metric for Cartesian<N>

Source§

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

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§

fn distance(&self, other: &Self) -> f64

Compute the distance between two vectors belonging to a metric space. Read more
Source§

impl<const N: usize> Mul<f64> for Cartesian<N>

Source§

type Output = Cartesian<N>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f64) -> Self

Performs the * operation. Read more
Source§

impl<const N: usize> MulAssign<f64> for Cartesian<N>

Source§

fn mul_assign(&mut self, rhs: f64)

Performs the *= operation. Read more
Source§

impl<const N: usize> Neg for Cartesian<N>

Source§

type Output = Cartesian<N>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<const N: usize> Outer for Cartesian<N>

Source§

type Tensor = Matrix<N, N>

Result type.
Source§

fn outer(&self, other: &Self) -> Self::Tensor

Compute the outer product of two vectors. Read more
Source§

impl<const N: usize> PartialEq for Cartesian<N>

Source§

fn eq(&self, other: &Cartesian<N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize> RelativeEq for Cartesian<N>

Source§

fn default_max_relative() -> Self::Epsilon

The default relative tolerance for testing values that are far-apart. Read more
Source§

fn relative_eq( &self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool

A test for equality that uses a relative comparison if the values are far apart.
§

fn relative_ne( &self, other: &Rhs, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool

The inverse of [RelativeEq::relative_eq].
Source§

impl Rotate<Cartesian<2>> for Angle

Source§

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 of the related rotation matrix
Source§

impl Rotate<Cartesian<3>> for Versor

Source§

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 of the related rotation matrix
Source§

impl<const N: usize> Rotate<Cartesian<N>> for RotationMatrix<N>

Source§

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 of the related rotation matrix
Source§

impl<const N: usize> Serialize for Cartesian<N>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<const N: usize> Sub for Cartesian<N>

Source§

type Output = Cartesian<N>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self

Performs the - operation. Read more
Source§

impl<const N: usize> SubAssign for Cartesian<N>

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl<const N: usize> Sum for Cartesian<N>

Source§

fn sum<I>(iter: I) -> Self
where I: Iterator<Item = Self>,

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl<const N: usize> TryFrom<Range<usize>> for Cartesian<N>

Source§

fn try_from(value: Range<usize>) -> Result<Self, Self::Error>

Create a Cartesian vector with coordinates given by a range.

§Example
use hoomd_vector::Cartesian;

let v = Cartesian::<3>::try_from(3..6)?;
assert_eq!(v, [3.0, 4.0, 5.0].into());
Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

impl<const N: usize> TryFrom<Vec<f64>> for Cartesian<N>

Source§

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§

type Error = Error

The type returned in the event of a conversion error.
Source§

impl Wedge for Cartesian<2>

Source§

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([2.0, 1.0]);
let b = Cartesian::from([3.0, 1.0]);

assert_eq!(a.wedge(&b), -1.0);
Source§

type Bivector = f64

Type of the bivector result.
Source§

impl Wedge for Cartesian<3>

Source§

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());
Source§

type Bivector = Cartesian<3>

Type of the bivector result.
Source§

impl<const N: usize> Copy for Cartesian<N>

Source§

impl<const N: usize> StructuralPartialEq for Cartesian<N>

Source§

impl<const N: usize> Vector for Cartesian<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for Cartesian<N>

§

impl<const N: usize> RefUnwindSafe for Cartesian<N>

§

impl<const N: usize> Send for Cartesian<N>

§

impl<const N: usize> Sync for Cartesian<N>

§

impl<const N: usize> Unpin for Cartesian<N>

§

impl<const N: usize> UnsafeUnpin for Cartesian<N>

§

impl<const N: usize> UnwindSafe for Cartesian<N>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,