Skip to main content

Vector

Struct Vector 

Source
pub struct Vector<const N: usize, T = f64> { /* private fields */ }
Expand description

A column vector of N components, stored inline on the stack.

use multicalc::linear_algebra::Vector;
let a = Vector::new([1.0, 2.0, 3.0]);
let b = Vector::from([4.0, 5.0, 6.0]);

assert_eq!(a[0], 1.0);
assert_eq!(a + b, Vector::new([5.0, 7.0, 9.0]));
assert_eq!(b - a, Vector::new([3.0, 3.0, 3.0]));
assert_eq!(-a, Vector::new([-1.0, -2.0, -3.0]));
assert_eq!(a * 2.0, Vector::new([2.0, 4.0, 6.0]));
assert_eq!(a.dot(b), 32.0);

Implementations§

Source§

impl<const N: usize, T> Vector<N, T>

Source

pub const fn new(data: [T; N]) -> Self

Wraps N components into a vector.

Source

pub fn from_fn(f: impl FnMut(usize) -> T) -> Self

Builds a vector by calling f with each index in 0..N.

use multicalc::linear_algebra::Vector;
let v = Vector::<4>::from_fn(|i| i as f64);
assert_eq!(v.into_array(), [0.0, 1.0, 2.0, 3.0]);
Source

pub const fn as_array(&self) -> &[T; N]

Borrows the components as an array.

use multicalc::linear_algebra::Vector;
assert_eq!(Vector::new([1.0, 2.0]).as_array(), &[1.0, 2.0]);
Source

pub const fn as_slice(&self) -> &[T]

Borrows the components as a slice.

use multicalc::linear_algebra::Vector;
assert_eq!(Vector::new([1.0, 2.0]).as_slice(), &[1.0, 2.0]);
Source

pub fn as_mut_slice(&mut self) -> &mut [T]

Borrows the components as a mutable slice.

use multicalc::linear_algebra::Vector;
let mut v = Vector::new([1.0, 2.0]);
v.as_mut_slice()[0] = 9.0;
assert_eq!(v[0], 9.0);
Source

pub fn into_array(self) -> [T; N]

Consumes the vector, returning its components.

Source§

impl<const N: usize, T: Copy> Vector<N, T>

Source

pub fn try_from_slice(slice: &[T]) -> Option<Self>

Builds a vector from a slice, or None if slice.len() is not N.

use multicalc::linear_algebra::Vector;
assert!(Vector::<3>::try_from_slice(&[1.0, 2.0, 3.0]).is_some());
assert!(Vector::<3>::try_from_slice(&[1.0, 2.0]).is_none());
Source§

impl<const N: usize, T: Numeric> Vector<N, T>

Source

pub fn zeros() -> Self

The zero vector.

use multicalc::linear_algebra::Vector;
let v: Vector<3> = Vector::zeros();
assert_eq!(v.into_array(), [0.0, 0.0, 0.0]);
Source

pub fn scale(self, scalar: T) -> Self

Multiplies every component by scalar.

use multicalc::linear_algebra::Vector;
assert_eq!(Vector::new([1.0, 2.0]).scale(3.0), Vector::new([3.0, 6.0]));
Source

pub fn dot(self, rhs: Self) -> T

The dot product Σ self[i] * rhs[i], summed left to right.

use multicalc::linear_algebra::Vector;
assert_eq!(Vector::new([1.0, 2.0, 3.0]).dot(Vector::new([4.0, 5.0, 6.0])), 32.0);
assert_eq!(Vector::new([1.0, 0.0]).dot(Vector::new([0.0, 1.0])), 0.0);
Source

pub fn norm_squared(self) -> T

The squared Euclidean norm self · self (no square root).

use multicalc::linear_algebra::Vector;
assert_eq!(Vector::new([3.0, 4.0]).norm_squared(), 25.0);
Source

pub fn norm(self) -> T

The Euclidean norm √(self · self).

use multicalc::linear_algebra::Vector;
assert_eq!(Vector::new([3.0, 4.0]).norm(), 5.0);
Source

pub fn is_finite(self) -> bool

Returns true when every component is neither infinite nor NaN.

use multicalc::linear_algebra::Vector;
assert!(Vector::new([1.0, -2.0]).is_finite());
assert!(!Vector::new([1.0, f64::NAN]).is_finite());
Source§

impl<T: Numeric> Vector<3, T>

Source

pub fn cross(self, rhs: Self) -> Self

The cross product self × rhs, available only for 3-D vectors.

use multicalc::linear_algebra::Vector;
let x = Vector::new([1.0, 0.0, 0.0]);
let y = Vector::new([0.0, 1.0, 0.0]);
assert_eq!(x.cross(y), Vector::new([0.0, 0.0, 1.0]));
Source

pub fn scalar_triple(self, b: Self, c: Self) -> T

The scalar triple product self · (b × c): the signed volume spanned by the three vectors.

use multicalc::linear_algebra::Vector;
let x = Vector::new([1.0, 0.0, 0.0]);
let y = Vector::new([0.0, 1.0, 0.0]);
let z = Vector::new([0.0, 0.0, 1.0]);
assert_eq!(x.scalar_triple(y, z), 1.0);
Source§

impl<T: Numeric> Vector<2, T>

Source

pub fn cross(self, rhs: Self) -> T

The 2-D cross product self[0] * rhs[1] - self[1] * rhs[0] — the scalar z-component of the 3-D cross, available only for 2-D vectors.

use multicalc::linear_algebra::Vector;
let x = Vector::new([1.0, 0.0]);
let y = Vector::new([0.0, 1.0]);
assert_eq!(x.cross(y), 1.0);
assert_eq!(y.cross(x), -1.0);

Trait Implementations§

Source§

impl<const N: usize, T: Numeric> Add for Vector<N, T>

Source§

type Output = Vector<N, T>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<const N: usize, T: Numeric> AddAssign for Vector<N, T>

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl<const N: usize, T: Clone> Clone for Vector<N, T>

Source§

fn clone(&self) -> Vector<N, T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<const N: usize, T: Copy> Copy for Vector<N, T>

Source§

impl<const N: usize, T: Debug> Debug for Vector<N, T>

Source§

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

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

impl<T: Numeric> From<Twist<T>> for Vector<6, T>

Source§

fn from(t: Twist<T>) -> Self

Flattens a twist into [v; ω].

Source§

impl<T: Numeric> From<Vector<6, T>> for Twist<T>

Source§

fn from(v: Vector<6, T>) -> Self

Reinterprets a flat [v; ω] Vector<6> as a twist.

Source§

impl<T: Numeric> From<Vector<6, T>> for Wrench<T>

Source§

fn from(v: Vector<6, T>) -> Self

Reinterprets a flat [f; τ] Vector<6> as a wrench.

Source§

impl<T: Numeric> From<Wrench<T>> for Vector<6, T>

Source§

fn from(w: Wrench<T>) -> Self

Flattens a wrench into [f; τ].

Source§

impl<const N: usize, T> From<[T; N]> for Vector<N, T>

Source§

fn from(data: [T; N]) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize, T> Index<usize> for Vector<N, T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &T

Performs the indexing (container[index]) operation. Read more
Source§

impl<const N: usize, T> IndexMut<usize> for Vector<N, T>

Source§

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

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<const N: usize, T: Numeric> Mul<T> for Vector<N, T>

Source§

type Output = Vector<N, T>

The resulting type after applying the * operator.
Source§

fn mul(self, scalar: T) -> Self

Performs the * operation. Read more
Source§

impl<const ROWS: usize, const COLS: usize, T: Numeric> Mul<Vector<COLS, T>> for Matrix<ROWS, COLS, T>

Source§

type Output = Vector<ROWS, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vector<COLS, T>) -> Vector<ROWS, T>

Performs the * operation. Read more
Source§

impl<const N: usize, T: Numeric> Neg for Vector<N, T>

Source§

type Output = Vector<N, T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl<const N: usize, T: PartialEq> PartialEq for Vector<N, T>

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<const N: usize, T: PartialEq> StructuralPartialEq for Vector<N, T>

Source§

impl<const N: usize, T: Numeric> Sub for Vector<N, T>

Source§

type Output = Vector<N, T>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<const N: usize, T: Numeric> SubAssign for Vector<N, T>

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more

Auto Trait Implementations§

§

impl<const N: usize, T> Freeze for Vector<N, T>
where T: Freeze,

§

impl<const N: usize, T> RefUnwindSafe for Vector<N, T>
where T: RefUnwindSafe,

§

impl<const N: usize, T> Send for Vector<N, T>
where T: Send,

§

impl<const N: usize, T> Sync for Vector<N, T>
where T: Sync,

§

impl<const N: usize, T> Unpin for Vector<N, T>
where T: Unpin,

§

impl<const N: usize, T> UnsafeUnpin for Vector<N, T>
where T: UnsafeUnpin,

§

impl<const N: usize, T> UnwindSafe for Vector<N, T>
where T: UnwindSafe,

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, 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.