Struct Matrix

Source
pub struct Matrix<T> { /* private fields */ }
Expand description

A representation of a standard Matrix of size r * c

§Examples

let mut m = Matrix::new(2, 2, vec![0; 4]);

assert_eq!(m.at(0, 0), 0);

*m.at_mut(1, 1) = 5;
assert_eq!(m.at(1, 1), 5);
let m = Matrix::from_vec(1, 2, &vec![9, 8]);

assert_eq!(&vec![9, 8], m.data());

Implementations§

Source§

impl<T> Matrix<T>
where T: Copy + Add<Output = T> + Div<Output = T>,

Source

pub fn new(r: usize, c: usize, v: Vec<T>) -> Matrix<T>

Constructs a new Matrix<T>, taking ownership of v and using it as the buffer.

Source

pub fn from_vec(r: usize, c: usize, v: &Vec<T>) -> Matrix<T>

Constructs a new Matrix<T>, using v as the buffer without taking ownership of it.

Source

pub fn rows(&self) -> usize

Returns the number of rows of the matrix.

Source

pub fn cols(&self) -> usize

Returns the number of columns of the matrix.

Source

pub fn at(&self, r: usize, c: usize) -> T

Returns the element at the r-th row and c-th column, provided those are within bounds.

Source

pub fn at_mut(&mut self, r: usize, c: usize) -> &mut T

Returns a mutable reference to the element at the r-th row and c-th column, provided those are within bounds.

Source

pub fn data(&self) -> &Vec<T>

Returns a reference to the matrix data vector

Source

pub fn map<U, F>(&self, f: F) -> Matrix<U>
where U: Copy + Add<Output = U> + Div<Output = U>, F: Fn(&T) -> U,

Maps over the elements of the matrix.

§Example
let m = Matrix::new(2, 2, vec![5; 4]);

let mapped = m.map(|&x| x * 8);
assert_eq!(mapped.data(), &vec![40; 4]);
Source

pub fn diagonal(&self) -> Vec<T>

Produces the diagonal of an ‘m x m’ matrix. Panics otherwise.

§Example
let m = Matrix::new(2, 2, vec![1, 2, 3, 4]);

let diag = m.diagonal();
assert_eq!(diag, vec![1, 4]);

Trait Implementations§

Source§

impl<'a, T> Add for &'a Matrix<T>
where T: Copy + Add<Output = T> + Div<Output = T>,

Source§

type Output = Matrix<T>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<'a, T> Mul for &'a Matrix<T>
where T: Copy + Add<Output = T> + AddAssign<T> + Mul<Output = T> + Div<Output = T> + Default,

Source§

type Output = Matrix<T>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Self::Output

Performs the * operation. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Matrix<T>

§

impl<T> RefUnwindSafe for Matrix<T>
where T: RefUnwindSafe,

§

impl<T> Send for Matrix<T>
where T: Send,

§

impl<T> Sync for Matrix<T>
where T: Sync,

§

impl<T> Unpin for Matrix<T>
where T: Unpin,

§

impl<T> UnwindSafe for Matrix<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> 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.