Struct Vector

Source
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>
where T: Copy + Default,

Source

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

pub const fn size(&self) -> usize

Returns the number of elements in the Vector.

§Examples
use mini_matrix::Vector;

let v: Vector<i32, 3> = Vector::from([1, 2, 3]);
assert_eq!(v.size(), 3);
Source

pub fn zero() -> Self

Creates a new Vector with all elements set to the default value of type T.

§Examples
use mini_matrix::Vector;

let v: Vector<i32, 3> = Vector::zero();
assert_eq!(v[0], 0);
assert_eq!(v[1], 0);
assert_eq!(v[2], 0);
Source§

impl<T, const N: usize> Vector<T, N>
where T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Copy + Clone + Default,

Source

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

pub fn sub(&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.sub(&v2);
assert_eq!(v1[0], 3);
assert_eq!(v1[1], 3);
assert_eq!(v1[2], 3);
Source

pub fn scl(&mut self, scalar: T)

Multiplies this vector by a scalar value in-place.

§Examples
use mini_matrix::Vector;

let mut v = Vector::from([1, 2, 3]);
v.scl(2);
assert_eq!(v[0], 2);
assert_eq!(v[1], 4);
assert_eq!(v[2], 6);
Source§

impl<T, const N: usize> Vector<T, N>
where T: Num + Sum + Copy + Clone,

Source

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>
where T: Float + Sum<T>,

Source

pub fn norm_1(&self) -> T

Calculates the L1 norm (Manhattan norm) of the vector.

The L1 norm is the sum of the absolute values of the vector’s components.

§Returns

The L1 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(), 6.0);
Source

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());
Source

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

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

Returns a slice containing the entire array. Equivalent to &s[..].

1.57.0 · Source

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

Returns a mutable slice containing the entire array. Equivalent to &mut s[..].

1.77.0 · Source

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

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]);
Source

pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])

🔬This is a nightly-only experimental API. (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, &[]);
}
Source

pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T])

🔬This is a nightly-only experimental API. (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]);
Source

pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])

🔬This is a nightly-only experimental API. (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]);
}
Source

pub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M])

🔬This is a nightly-only experimental API. (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>
where T: Add<Output = T> + Default + Clone + Copy,

Source§

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

Adds two vectors element-wise.

§Examples
use mini_matrix::Vector;

let v1 = Vector::from([1, 2, 3]);
let v2 = Vector::from([4, 5, 6]);
let v3 = v1 + v2;
assert_eq!(v3, Vector::from([5, 7, 9]));
Source§

type Output = Vector<T, N>

The resulting type after applying the + operator.
Source§

impl<T, const N: usize> AddAssign for Vector<T, N>
where T: Num + Default + Clone + Copy,

Source§

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: Clone, const N: usize> Clone for Vector<T, N>

Source§

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

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

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

Source§

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

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

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

Source§

type Target = [T; N]

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

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

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

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

Source§

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

Formats the vector for display.

§Examples
use mini_matrix::Vector;

let v = Vector::from([1.0, 2.5, 3.7]);
println!("{}", v); // Outputs: //-> [1.0, 2.5, 3.7]
Source§

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

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

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

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

Source§

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

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

impl<T, const N: usize> Mul<T> for Vector<T, N>
where T: Mul + Num + Copy,

Source§

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

Multiplies a vector by a scalar.

§Examples
use mini_matrix::Vector;

let v1 = Vector::from([1.0, 2.0, 3.0]);
let v2 = v1 * 2.0;
assert_eq!(v2, Vector::from([2.0, 4.0, 6.0]));
Source§

type Output = Vector<T, N>

The resulting type after applying the * operator.
Source§

impl<T, const M: usize, const N: usize> Mul<Vector<T, N>> for Matrix<T, M, N>
where T: MulAssign + AddAssign + Copy + Num + Default,

Source§

fn mul(self, rhs: Vector<T, N>) -> Self::Output

Multiplies a matrix by a vector.

§Examples
use mini_matrix::{Matrix, Vector};

let a = Matrix::<i32, 2, 2>::from([[1, 2], [3, 4]]);
let v = Vector::<i32, 2>::from([5, 6]);
let result = a * v;
assert_eq!(result.store, [17, 39]);
Source§

type Output = Vector<T, M>

The resulting type after applying the * operator.
Source§

impl<T, const N: usize> Mul for Vector<T, N>
where T: Mul + Num + Sum + Copy,

Source§

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§

type Output = T

The resulting type after applying the * operator.
Source§

impl<T, const N: usize> Neg for Vector<T, N>
where T: Num + Copy + Signed,

Source§

fn neg(self) -> Self::Output

Negates a vector, inverting the sign of all its elements.

§Examples
use mini_matrix::Vector;

let v1 = Vector::from([1, -2, 3]);
let v2 = -v1;
assert_eq!(v2, Vector::from([-1, 2, -3]));
Source§

type Output = Vector<T, N>

The resulting type after applying the - operator.
Source§

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

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, const N: usize> Sub for Vector<T, N>
where T: Sub<Output = T> + Default + Clone + Copy,

Source§

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

Subtracts one vector from another element-wise.

§Examples
use mini_matrix::Vector;

let v1 = Vector::from([4, 5, 6]);
let v2 = Vector::from([1, 2, 3]);
let v3 = v1 - v2;
assert_eq!(v3, Vector::from([3, 3, 3]));
Source§

type Output = Vector<T, N>

The resulting type after applying the - operator.
Source§

impl<T, const N: usize> SubAssign for Vector<T, N>
where T: Num + Default + Clone + Copy,

Source§

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]));
Source§

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

Source§

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T, const N: usize> UnwindSafe for Vector<T, N>
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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.