pub struct Vector<T, const N: usize> {
pub store: [T; N],
}
Expand description
Represents a vector of N
elements of type T
.
§Examples
use mini_matrix::Vector;
let v = Vector::from([1, 2, 3]);
assert_eq!(v[0], 1);
assert_eq!(v[1], 2);
assert_eq!(v[2], 3);
Fields§
§store: [T; N]
Implementations§
Source§impl<T, const N: usize> Vector<T, N>
impl<T, const N: usize> Vector<T, N>
Sourcepub fn from(data: [T; N]) -> Self
pub fn from(data: [T; N]) -> Self
Creates a new Vector
from an array of elements.
§Examples
use mini_matrix::Vector;
let v = Vector::from([1, 2, 3]);
Source§impl<T, const N: usize> Vector<T, N>
impl<T, const N: usize> Vector<T, N>
Sourcepub fn add(&mut self, rhs: &Self)
pub fn add(&mut self, rhs: &Self)
Adds another vector to this vector in-place.
§Examples
use mini_matrix::Vector;
let mut v1 = Vector::from([1, 2, 3]);
let v2 = Vector::from([4, 5, 6]);
v1.add(&v2);
assert_eq!(v1[0], 5);
assert_eq!(v1[1], 7);
assert_eq!(v1[2], 9);
Source§impl<T, const N: usize> Vector<T, N>
impl<T, const N: usize> Vector<T, N>
Sourcepub fn dot(&self, v: &Self) -> T
pub fn dot(&self, v: &Self) -> T
Computes the dot product of two vectors.
The dot product is the sum of the products of corresponding elements.
§Examples
use mini_matrix::Vector;
let v1 = Vector::from([1, 2, 3]);
let v2 = Vector::from([4, 5, 6]);
assert_eq!(v1.dot(&v2), 32); // 1*4 + 2*5 + 3*6 = 32
Source§impl<T, const N: usize> Vector<T, N>
impl<T, const N: usize> Vector<T, N>
Sourcepub fn norm(&self) -> T
pub fn norm(&self) -> T
Calculates the L2 norm (Euclidean norm) of the vector.
The L2 norm is the square root of the sum of the squared components.
§Returns
The L2 norm as a value of type T
.
§Examples
use mini_matrix::Vector;
let v = Vector::from([1.0, -2.0, 3.0]);
assert_eq!(v.norm(), [1.0 + 4.0 + 9.0].iter().sum::<f32>().sqrt());
Sourcepub fn norm_inf(&self) -> T
pub fn norm_inf(&self) -> T
Calculates the L-infinity norm (maximum norm) of the vector.
The L-infinity norm is the maximum of the absolute values of the vector’s components.
§Returns
The L-infinity norm as a value of type T
.
§Examples
use mini_matrix::Vector;
let v = Vector::from([1.0, -2.0, 3.0]);
assert_eq!(v.norm_inf(), 3.0);
Methods from Deref<Target = [T; N]>§
1.57.0 · Sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Returns a slice containing the entire array. Equivalent to &s[..]
.
1.57.0 · Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
Returns a mutable slice containing the entire array. Equivalent to
&mut s[..]
.
1.77.0 · Sourcepub fn each_ref(&self) -> [&T; N]
pub fn each_ref(&self) -> [&T; N]
Borrows each element and returns an array of references with the same
size as self
.
§Example
let floats = [3.1, 2.7, -1.0];
let float_refs: [&f64; 3] = floats.each_ref();
assert_eq!(float_refs, [&3.1, &2.7, &-1.0]);
This method is particularly useful if combined with other methods, like
map
. This way, you can avoid moving the original
array if its elements are not Copy
.
let strings = ["Ferris".to_string(), "♥".to_string(), "Rust".to_string()];
let is_ascii = strings.each_ref().map(|s| s.is_ascii());
assert_eq!(is_ascii, [true, false, true]);
// We can still access the original array: it has not been moved.
assert_eq!(strings.len(), 3);
1.77.0 · Sourcepub fn each_mut(&mut self) -> [&mut T; N]
pub fn each_mut(&mut self) -> [&mut T; N]
Borrows each element mutably and returns an array of mutable references
with the same size as self
.
§Example
let mut floats = [3.1, 2.7, -1.0];
let float_refs: [&mut f64; 3] = floats.each_mut();
*float_refs[0] = 0.0;
assert_eq!(float_refs, [&mut 0.0, &mut 2.7, &mut -1.0]);
assert_eq!(floats, [0.0, 2.7, -1.0]);
Sourcepub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])
🔬This is a nightly-only experimental API. (split_array
)
pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])
split_array
)Divides one array reference into two at an index.
The first will contain all indices from [0, M)
(excluding
the index M
itself) and the second will contain all
indices from [M, N)
(excluding the index N
itself).
§Panics
Panics if M > N
.
§Examples
#![feature(split_array)]
let v = [1, 2, 3, 4, 5, 6];
{
let (left, right) = v.split_array_ref::<0>();
assert_eq!(left, &[]);
assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}
{
let (left, right) = v.split_array_ref::<2>();
assert_eq!(left, &[1, 2]);
assert_eq!(right, &[3, 4, 5, 6]);
}
{
let (left, right) = v.split_array_ref::<6>();
assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
assert_eq!(right, &[]);
}
Sourcepub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T])
🔬This is a nightly-only experimental API. (split_array
)
pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T])
split_array
)Divides one mutable array reference into two at an index.
The first will contain all indices from [0, M)
(excluding
the index M
itself) and the second will contain all
indices from [M, N)
(excluding the index N
itself).
§Panics
Panics if M > N
.
§Examples
#![feature(split_array)]
let mut v = [1, 0, 3, 0, 5, 6];
let (left, right) = v.split_array_mut::<2>();
assert_eq!(left, &mut [1, 0][..]);
assert_eq!(right, &mut [3, 0, 5, 6]);
left[1] = 2;
right[1] = 4;
assert_eq!(v, [1, 2, 3, 4, 5, 6]);
Sourcepub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])
🔬This is a nightly-only experimental API. (split_array
)
pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])
split_array
)Divides one array reference into two at an index from the end.
The first will contain all indices from [0, N - M)
(excluding
the index N - M
itself) and the second will contain all
indices from [N - M, N)
(excluding the index N
itself).
§Panics
Panics if M > N
.
§Examples
#![feature(split_array)]
let v = [1, 2, 3, 4, 5, 6];
{
let (left, right) = v.rsplit_array_ref::<0>();
assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
assert_eq!(right, &[]);
}
{
let (left, right) = v.rsplit_array_ref::<2>();
assert_eq!(left, &[1, 2, 3, 4]);
assert_eq!(right, &[5, 6]);
}
{
let (left, right) = v.rsplit_array_ref::<6>();
assert_eq!(left, &[]);
assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}
Sourcepub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M])
🔬This is a nightly-only experimental API. (split_array
)
pub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M])
split_array
)Divides one mutable array reference into two at an index from the end.
The first will contain all indices from [0, N - M)
(excluding
the index N - M
itself) and the second will contain all
indices from [N - M, N)
(excluding the index N
itself).
§Panics
Panics if M > N
.
§Examples
#![feature(split_array)]
let mut v = [1, 0, 3, 0, 5, 6];
let (left, right) = v.rsplit_array_mut::<4>();
assert_eq!(left, &mut [1, 0]);
assert_eq!(right, &mut [3, 0, 5, 6][..]);
left[1] = 2;
right[1] = 4;
assert_eq!(v, [1, 2, 3, 4, 5, 6]);
Trait Implementations§
Source§impl<T, const N: usize> Add for Vector<T, N>
impl<T, const N: usize> Add for Vector<T, N>
Source§impl<T, const N: usize> AddAssign for Vector<T, N>
impl<T, const N: usize> AddAssign for Vector<T, N>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Adds another vector to this vector in-place.
§Examples
use mini_matrix::Vector;
let mut v1 = Vector::from([1, 2, 3]);
let v2 = Vector::from([4, 5, 6]);
v1 += v2;
assert_eq!(v1, Vector::from([5, 7, 9]));
Source§impl<T, const N: usize> Mul<T> for Vector<T, N>
impl<T, const N: usize> Mul<T> for Vector<T, N>
Source§impl<T, const M: usize, const N: usize> Mul<Vector<T, N>> for Matrix<T, M, N>
impl<T, const M: usize, const N: usize> Mul<Vector<T, N>> for Matrix<T, M, N>
Source§impl<T, const N: usize> Mul for Vector<T, N>
impl<T, const N: usize> Mul for Vector<T, N>
Source§fn mul(self, rhs: Self) -> Self::Output
fn mul(self, rhs: Self) -> Self::Output
Computes the dot product of two vectors.
This is an alternative way to compute the dot product using the * operator.
§Examples
use mini_matrix::Vector;
let v1 = Vector::from([1, 2, 3]);
let v2 = Vector::from([4, 5, 6]);
assert_eq!(v1 * v2, 32); // 1*4 + 2*5 + 3*6 = 32
Source§impl<T, const N: usize> Neg for Vector<T, N>
impl<T, const N: usize> Neg for Vector<T, N>
Source§impl<T, const N: usize> Sub for Vector<T, N>
impl<T, const N: usize> Sub for Vector<T, N>
Source§impl<T, const N: usize> SubAssign for Vector<T, N>
impl<T, const N: usize> SubAssign for Vector<T, N>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Subtracts another vector from this vector in-place.
§Examples
use mini_matrix::Vector;
let mut v1 = Vector::from([4, 5, 6]);
let v2 = Vector::from([1, 2, 3]);
v1 -= v2;
assert_eq!(v1, Vector::from([3, 3, 3]));