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>
impl<const N: usize, T> Vector<N, T>
Sourcepub fn from_fn(f: impl FnMut(usize) -> T) -> Self
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]);Sourcepub const fn as_array(&self) -> &[T; N]
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]);Sourcepub const fn as_slice(&self) -> &[T]
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]);Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
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);Sourcepub fn into_array(self) -> [T; N]
pub fn into_array(self) -> [T; N]
Consumes the vector, returning its components.
Source§impl<const N: usize, T: Copy> Vector<N, T>
impl<const N: usize, T: Copy> Vector<N, T>
Sourcepub fn try_from_slice(slice: &[T]) -> Option<Self>
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>
impl<const N: usize, T: Numeric> Vector<N, T>
Sourcepub fn zeros() -> Self
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]);Sourcepub fn scale(self, scalar: T) -> Self
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]));Sourcepub fn dot(self, rhs: Self) -> T
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);Sourcepub fn norm_squared(self) -> T
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§impl<T: Numeric> Vector<3, T>
impl<T: Numeric> Vector<3, T>
Sourcepub fn cross(self, rhs: Self) -> Self
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]));Sourcepub fn scalar_triple(self, b: Self, c: Self) -> T
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>
impl<T: Numeric> Vector<2, T>
Sourcepub fn cross(self, rhs: Self) -> T
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> AddAssign for Vector<N, T>
impl<const N: usize, T: Numeric> AddAssign for Vector<N, T>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read more