pub trait VectorMath: Sized {
// Required methods
fn dot(&self, other: &Self) -> f64;
fn add(&self, other: &Self) -> Self;
fn sub(&self, other: &Self) -> Self;
fn mul(&self, scalar: f64) -> Self;
fn mul_pt(&self, other: &Self) -> Self;
// Provided methods
fn norm(&self) -> f64 { ... }
fn normalized(&self) -> Self { ... }
}
Expand description
Treats object mathematically as if it were a vector.
Basically this is a way to perform math on objects like Vec<f64>
without
wrapping them in a struct.
§Examples
Basic addition:
let a = vec![1.0f64, 1.0, 1.0];
let b = vec![1.0, 2.0, 3.0];
let c = a.add(&b);
assert_eq!(c, vec![2.0, 3.0, 4.0]);
Dot products:
let a = vec![1.0, 1.0, 1.0];
let b = vec![1.0, 0.0, 2.0];
assert_eq!(a.dot(b), 4.0);
Required Methods§
Provided Methods§
Sourcefn normalized(&self) -> Self
fn normalized(&self) -> Self
Normalizes the vector to unit length, returning a new vector
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.