Trait VectorMath

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

Source

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

Computes dot product of two vectors.

Source

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

Adds two vectors, returning a new vector.

Source

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

Subtracts two vectors, returning a new vector.

Source

fn mul(&self, scalar: f64) -> Self

Multiplies a scalar and a vector, returning a new vector.

Source

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

Multiplies a vector and a vector pointwise, returning a new vector.

Provided Methods§

Source

fn norm(&self) -> f64

Computes the vector norm

Source

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.

Implementations on Foreign Types§

Source§

impl VectorMath for (f64, f64)

Source§

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

Source§

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

Source§

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

Source§

fn mul(&self, scalar: f64) -> Self

Source§

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

Source§

impl VectorMath for Vec<f64>

Source§

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

Source§

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

Source§

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

Source§

fn mul(&self, scalar: f64) -> Self

Source§

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

Implementors§