Skip to main content

Matrix

Struct Matrix 

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

Implementations§

Source§

impl<T> Matrix<T>

Source

pub fn new<F>(width: usize, height: usize, address_value_converter: F) -> Self
where F: Fn(MatrixAddress) -> T,

Creates a new Matrix based on dimensions and a mapper function.

§Arguments
  • width: The width, or number of columns in the matrix
  • height: The height, or number of rows in the matrix
  • address_value_converter: Converts a matrix address to a value.

Returns: Matrix

§Examples
use rust_tensors::matrix::Matrix;
use rust_tensors::tensor::Tensor;

// Creates a 1000x1000 zero matrix
let (width, height) = (1000, 1000);
let mut matrix = Matrix::new(width, height, |_address| 0usize);
matrix.address_iter()
    .for_each(|address| assert_eq!(matrix[address], 0));

// Creates a 50x10 matrix where the value is the index of the array
let (width, height) = (1000, 1000);
let mut matrix = Matrix::new(width, height, |address| address.y * width as i32 + address.x);
matrix.address_iter()
    .for_each(|address| assert_eq!(matrix[address], address.y * width as i32 + address.x));
Source

pub fn to_display_string<T1: Display, F: Fn(&T) -> T1>( &self, display_func: F, row_delimiter: &str, column_delimiter: &str, ) -> String

Makes a string fit for displaying the contents of the matrix

§Arguments
  • display_func: Converts a value to a string
  • row_delimiter: Separates the rows in the matrix
  • column_delimiter: Separates the columns in the matrix

Returns: the formatted string

§Examples
use rust_tensors::matrix::Matrix;
let mut matrix =
Matrix::<i32>::parse_matrix("1 2 3|4 5 6|7 8 9", " ", "|", |s| s.parse().unwrap())
    .unwrap();
assert_eq!(
    matrix.to_display_string(|i| i.to_string(), "-", "|"),
    "1-2-3|4-5-6|7-8-9"
);
Source

pub fn parse_matrix<F>( data_str: &str, column_delimiter: &str, row_delimiter: &str, str_to_t_converter: F, ) -> Result<Matrix<T>, String>
where F: Fn(&str) -> T,

Parses a matrix from a string. Fallible, and will return an Err if the matrix cannot be parsed, or if the matrix does not have a uniform row length

§Arguments
  • data_str: The string to be parsed
  • column_delimiter: The string which separates the items in the columns
  • row_delimiter: The string which separates the rows
  • str_to_t_converter: The function which converts the item strings to a value

Returns: Result<Matrix, String>

§Examples
use rust_tensors::matrix::Matrix;

let mut matrix =
    Matrix::<i32>::parse_matrix("0 1 2|3 4 5|6 7 8", " ", "|", |s| s.parse().unwrap())
        .unwrap();

assert_eq!(
    matrix, Matrix::new(3, 3, |address| address.x + 3 * address.y)
);

Trait Implementations§

Source§

impl<T: Clone> Clone for Matrix<T>

Source§

fn clone(&self) -> Matrix<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<T: Debug> Debug for Matrix<T>

Source§

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

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

impl<T: Display> Display for Matrix<T>

Source§

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

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

impl<T: Eq> Eq for Matrix<T>

Source§

impl<T> Index<(i32, i32)> for Matrix<T>

Source§

type Output = T

The returned type after indexing.
Source§

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

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

impl<T> Index<MatrixAddress> for Matrix<T>

Source§

type Output = T

The returned type after indexing.
Source§

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

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

impl<T> IndexMut<(i32, i32)> for Matrix<T>

Source§

fn index_mut(&mut self, index: (i32, i32)) -> &mut Self::Output

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

impl<T> IndexMut<MatrixAddress> for Matrix<T>

Source§

fn index_mut(&mut self, index: MatrixAddress) -> &mut Self::Output

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

impl<T: PartialEq> PartialEq for Matrix<T>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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> StructuralPartialEq for Matrix<T>

Source§

impl<T> Tensor<T, i32, MatrixAddress, 2> for Matrix<T>

Source§

fn smallest_contained_address(&self) -> MatrixAddress

Source§

fn largest_contained_address(&self) -> MatrixAddress

Source§

fn get(&self, address: A) -> Result<&T, String>

Attempts to get a reference of the value at the given address. Will return Err if the address is not contained in the matrix. Read more
Source§

fn get_mut(&mut self, address: A) -> Result<&mut T, String>

Attempts to get a mutable reference of the value at the given address. Will return Err if the address is not contained in the matrix. Read more
Source§

fn contains_address(&self, address: A) -> bool

Evaluates whether an address is valid and has an associated value in the tensor. Read more
Source§

fn address_iter(&self) -> AddressIterator<V, A, DIMENSION>

Creates an iterator over the addresses within the bounds of the tensor. 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> UnsafeUnpin for Matrix<T>

§

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