Struct Matrix

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

A structure of values

Implementations§

Source§

impl<T> Matrix<T>

Source

pub fn new(dimensions: Vec2, buffer: Vec<T>) -> Matrix<T>

Returns a new Matrix

§Arguments
  • dimensions - dimensions of Matrix
  • buffer - Vec of values
§Example
let matrix = ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).collect());
Source

pub fn len(&self) -> usize

Returns the total number of items in the Matrix

Source

pub fn dim(&self) -> Vec2

Returns the dimensions of the Matrix

Source

pub fn to_flattened(self) -> Vec<T>

Gives ownership to the buffer

§Example
let vector = ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).collect()).to_flattened();
let result = vec![0, 1, 2, 3, 4, 5];
for (&i, &j) in vector.iter().zip(result.iter()) {
    assert_eq!(i, j);
}
Source

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

Source

pub fn get_indicies(&self, Vec2: Vec2) -> Vec<usize>

Returns Vec of indicies from (0, 0) to (x, y) (inclusive and exclusive)

§Arguments
  • Vec2 - bottom corner of indicies
§Example
let matrix = ktensor::math::Matrix::new(ktensor::math::Vec2(3, 3), (0..9).collect());
let indicies = matrix.get_indicies(ktensor::math::Vec2(2, 2));
let result = vec![0, 1, 3, 4];
for (&i, &j) in indicies.iter().zip(result.iter()) {
    assert_eq!(i, j);
}
Source

pub fn get_indicies_stride(&self, Vec2: Vec2, stride: usize) -> Vec<usize>

Returns Vec of indicies across the entire Matrix up to (x, y) with the specified stride length

§Arguments
  • Vec2 - number of strides in directions x, y
  • stride - length of stride
§Example
let matrix = ktensor::math::Matrix::new(ktensor::math::Vec2(6, 6), (0..36).collect());
let indicies = matrix.get_indicies_stride(ktensor::math::Vec2(2, 2), 2);
let matrix = matrix.to_flattened();
assert_eq!(indicies.len(), 4);
for &i in indicies.iter() {
    assert_eq!(i, matrix[i]);
}
Source§

impl<T> Matrix<T>
where T: Copy,

Source

pub fn get(&self, Vec2: Vec2) -> T

Returns value at Vec2

§Arguments
  • Vec2 - coordinates of the value in the Matrix
§Example
let matrix = ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).collect());
assert_eq!(matrix.get(ktensor::math::Vec2(1, 2)), 5);
assert_eq!(matrix.get(ktensor::math::Vec2(1, 2)), 5);
Source

pub fn get_submatrix(&self, Vec2: Vec2, Vec2: Vec2) -> Matrix<T>

Returns Matrix of values from [vec1 to vec2) (inclusive and exclusive)

§Arguments
  • Vec2 - coordinates of the first corner
  • Vec2 - coordinates of the second corner
§Example
let matrix = ktensor::math::Matrix::new(ktensor::math::Vec2(3, 3), (0..9).collect());
let slice = matrix.get_submatrix(ktensor::math::Vec2(1, 1), ktensor::math::Vec2(3, 3)).to_flattened();
let result = vec![4, 5, 7, 8];
for (&i, &j) in slice.iter().zip(result.iter()) {
    assert_eq!(i, j);
}
Source

pub fn transpose(&self) -> Matrix<T>

Consumes Matrix and returns transposed Matrix

§Example
let vector = ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).collect());
let vector = vector.transpose();
assert_eq!(vector.get(ktensor::math::Vec2(2, 0)), 2);
Source§

impl<'a, T> Matrix<T>
where T: Mul<Output = T> + Copy,

Source

pub fn product(&self, rhs: &'a Matrix<T>) -> Matrix<T>

Hadamard Product of Matricies by reference

§Arguments
  • self - this matrix reference
  • rhs - another matrix reference
§Example
let matrix1 = ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).collect());
let matrix2 = ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).rev().collect());
let matrix3 = &matrix1.product(&matrix2);
assert_eq!(matrix1.get(ktensor::math::Vec2(0, 2)), 2.0);
assert_eq!(matrix2.get(ktensor::math::Vec2(0, 2)), 3.0);
assert_eq!(matrix3.get(ktensor::math::Vec2(0, 2)), 6.0);

Trait Implementations§

Source§

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

Source§

fn add(self, rhs: &'b Matrix<T>) -> Matrix<T>

Add Matricies by reference

§Arguments
  • self - this matrix reference
  • rhs - another matrix reference
§Example
let matrix1 = ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).collect());
let matrix2 = ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).rev().collect());
let matrix3 = &matrix1 + &matrix2;
assert_eq!(matrix1.get(ktensor::math::Vec2(0, 0)), 0.0);
assert_eq!(matrix2.get(ktensor::math::Vec2(0, 0)), 5.0);
assert_eq!(matrix3.get(ktensor::math::Vec2(0, 0)), 5.0);
Source§

type Output = Matrix<T>

The resulting type after applying the + operator.
Source§

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

Source§

fn add(self, rhs: &'b T) -> Matrix<T>

Add Matrix and a constant

§Arguments
  • self - this matrix reference
  • rhs - a constant reference
§Example
let matrix1 = ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).collect());
let float = 1.0;
let matrix2 = &matrix1 + &float;
assert_eq!(matrix1.get(ktensor::math::Vec2(0, 0)), 0.0);
assert_eq!(matrix2.get(ktensor::math::Vec2(0, 0)), 1.0);
assert_eq!(float, 1.0);
Source§

type Output = Matrix<T>

The resulting type after applying the + operator.
Source§

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

Source§

fn add(self, rhs: T) -> Matrix<T>

Add Matrix and a constant

§Arguments
  • self - this matrix
  • rhs - a constant
§Example
let matrix1 = ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).collect());
let float = 1.0;
let matrix2 = matrix1 + float;
assert_eq!(matrix2.get(ktensor::math::Vec2(0, 0)), 1.0);
Source§

type Output = Matrix<T>

The resulting type after applying the + operator.
Source§

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

Source§

fn add(self, rhs: Matrix<T>) -> Matrix<T>

Add Matricies

§Arguments
  • self - this matrix
  • rhs - another matrix
§Example
let matrix1 = ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).collect());
let matrix2 = ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).rev().collect());
let matrix3 = matrix1 + matrix2;
assert_eq!(matrix3.get(ktensor::math::Vec2(0, 0)), 5.0);
Source§

type Output = Matrix<T>

The resulting type after applying the + operator.
Source§

impl<T> Clone for Matrix<T>
where T: Copy,

Source§

fn clone(&self) -> Matrix<T>

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<'a, 'b, T> Mul<&'b Matrix<T>> for &'a Matrix<T>
where T: Mul<Output = T> + Add<Output = T> + Copy,

Source§

fn mul(self, rhs: &'b Matrix<T>) -> Matrix<T>

Multiply Matricies by reference

§Arguments
  • self - this matrix reference
  • rhs - another matrix reference
§Example
let matrix1 = ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).collect());
let matrix2 = ktensor::math::Matrix::new(ktensor::math::Vec2(3, 2), (0..6).map(|i| i as f64).rev().collect());
let matrix3 = &matrix1 * &matrix2;
assert_eq!(matrix3.len(), 4);
assert_eq!(matrix1.get(ktensor::math::Vec2(0, 0)), 0.0);
assert_eq!(matrix2.get(ktensor::math::Vec2(0, 0)), 5.0);
assert_eq!(matrix3.get(ktensor::math::Vec2(0, 0)), 5.0);
Source§

type Output = Matrix<T>

The resulting type after applying the * operator.
Source§

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

Source§

fn mul(self, rhs: &'b T) -> Matrix<T>

Multiply Matrix and a constant

§Arguments
  • self - this matrix reference
  • rhs - a constant reference
§Example
let matrix1 = ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).collect());
let float = 2.0;
let matrix2 = &matrix1 * &float;
assert_eq!(matrix1.get(ktensor::math::Vec2(0, 2)), 2.0);
assert_eq!(matrix2.get(ktensor::math::Vec2(0, 2)), 4.0);
assert_eq!(float, 2.0);
Source§

type Output = Matrix<T>

The resulting type after applying the * operator.
Source§

impl<T> Mul<T> for Matrix<T>
where T: Mul<Output = T> + Copy,

Source§

fn mul(self, rhs: T) -> Matrix<T>

Multiply Matrix and a constant

§Arguments
  • self - this matrix
  • rhs - a constant
§Example
let matrix1 = ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).collect());
let float = 2.0;
let matrix2 = matrix1 * float;
assert_eq!(matrix2.get(ktensor::math::Vec2(0, 2)), 4.0);
Source§

type Output = Matrix<T>

The resulting type after applying the * operator.
Source§

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

Source§

fn mul(self, rhs: Matrix<T>) -> Matrix<T>

Multiply Matricies

§Arguments
  • self - this matrix
  • rhs - another matrix
§Example
let matrix1 = ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).collect());
let matrix2 = ktensor::math::Matrix::new(ktensor::math::Vec2(3, 2), (0..6).map(|i| i as f64).rev().collect());
let matrix3 = matrix1 * matrix2;
assert_eq!(matrix3.len(), 4);
assert_eq!(matrix3.get(ktensor::math::Vec2(0, 0)), 5.0);
Source§

type Output = Matrix<T>

The resulting type after applying the * operator.

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