pub trait InnerProduct: Vector {
// Required methods
fn dot(&self, other: &Self) -> f64;
fn default_unit() -> Unit<Self>;
// Provided methods
fn norm_squared(&self) -> f64 { ... }
fn norm(&self) -> f64 { ... }
fn to_unit(self) -> Result<(Unit<Self>, f64), Error> { ... }
fn to_unit_unchecked(self) -> (Unit<Self>, f64) { ... }
fn project(&self, b: &Self) -> Self { ... }
}Expand description
Operate on elements of an inner product space.
The InnerProduct subtrait defines additional methods that can be performed on any vector
in an inner product space, specifically vector norms and inner products.
Required Methods§
Sourcefn dot(&self, other: &Self) -> f64
fn dot(&self, other: &Self) -> f64
Compute the vector dot product between two vectors.
c = \vec{a} \cdot \vec{b}§Example
use hoomd_vector::{Cartesian, InnerProduct};
let a = Cartesian::from([1.0, 2.0]);
let b = Cartesian::from([3.0, 4.0]);
let c = a.dot(&b);
assert_eq!(c, 11.0);Sourcefn default_unit() -> Unit<Self>
fn default_unit() -> Unit<Self>
Create a unit vector in the space.
Each vector space defines its own default unit vector.
§Example
use hoomd_vector::{Cartesian, InnerProduct};
let u = Cartesian::<2>::default_unit();
assert_eq!(*u.get(), [0.0, 1.0].into());Provided Methods§
Sourcefn norm_squared(&self) -> f64
fn norm_squared(&self) -> f64
Compute the squared norm of the vector.
\left| \vec{v} \right|^2§Example
use hoomd_vector::{Cartesian, InnerProduct};
let v = Cartesian::from([2.0, 4.0]);
let norm_squared = v.norm_squared();
assert_eq!(norm_squared, 20.0);Sourcefn norm(&self) -> f64
fn norm(&self) -> f64
Compute the norm of the vector.
\left| \vec{v} \right|Computing the norm calls sqrt. Prefer
norm_squared when possible.
§Example
use hoomd_vector::{Cartesian, InnerProduct};
let v = Cartesian::from([3.0, 4.0]);
let norm = v.norm();
assert_eq!(norm, 5.0);Sourcefn to_unit(self) -> Result<(Unit<Self>, f64), Error>
fn to_unit(self) -> Result<(Unit<Self>, f64), Error>
Create a vector of unit length pointing in the same direction as the given vector.
Returns a tuple containing unit vector along with the original vector’s norm:
\frac{\vec{v}}{|\vec{v}|}§Example
use hoomd_vector::{Cartesian, InnerProduct, Unit};
let a = Cartesian::from([3.0, 4.0]);
let (unit, norm) = a.to_unit()?;
assert_eq!(*unit.get(), [3.0 / 5.0, 4.0 / 5.0].into());
assert_eq!(norm, 5.0);§Errors
Error::InvalidVectorMagnitude when self is the 0 vector.
Sourcefn to_unit_unchecked(self) -> (Unit<Self>, f64)
fn to_unit_unchecked(self) -> (Unit<Self>, f64)
Create a vector of unit length pointing in the same direction as the given vector.
Returns a tuple containing unit vector along with the original vector’s norm:
\frac{\vec{v}}{|\vec{v}|}§Example
use hoomd_vector::{Cartesian, InnerProduct, Unit};
let a = Cartesian::from([3.0, 4.0]);
let (unit, norm) = a.to_unit_unchecked();
assert_eq!(*unit.get(), [3.0 / 5.0, 4.0 / 5.0].into());
assert_eq!(norm, 5.0);§Panics
Divide by 0 when self is the 0 vector.
Sourcefn project(&self, b: &Self) -> Self
fn project(&self, b: &Self) -> Self
Project one vector onto another.
\left(\frac{\vec{a} \cdot \vec{b}}{|\vec{b}|^2}\right) \vec{b}where self is $\vec{a}$.
§Example
use hoomd_vector::{Cartesian, InnerProduct, Vector};
let a = Cartesian::from([1.0, 2.0]);
let b = Cartesian::from([4.0, 0.0]);
let c = a.project(&b);
assert_eq!(c, [1.0, 0.0].into());Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.