pub struct Matrix<T> { /* private fields */ }
Expand description
A structure of values
Implementations§
Source§impl<T> Matrix<T>
impl<T> Matrix<T>
Sourcepub fn to_flattened(self) -> Vec<T>
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);
}
pub fn buffer(&self) -> &Vec<T>
Sourcepub fn get_indicies(&self, Vec2: Vec2) -> Vec<usize>
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);
}
Sourcepub fn get_indicies_stride(&self, Vec2: Vec2, stride: usize) -> Vec<usize>
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, ystride
- 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,
impl<T> Matrix<T>where
T: Copy,
Sourcepub fn get_submatrix(&self, Vec2: Vec2, Vec2: Vec2) -> Matrix<T>
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 cornerVec2
- 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§impl<'a, T> Matrix<T>
impl<'a, T> Matrix<T>
Sourcepub fn product(&self, rhs: &'a Matrix<T>) -> Matrix<T>
pub fn product(&self, rhs: &'a Matrix<T>) -> Matrix<T>
Hadamard Product of Matricies by reference
§Arguments
self
- this matrix referencerhs
- 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>
impl<'a, 'b, T> Add<&'b Matrix<T>> for &'a Matrix<T>
Source§fn add(self, rhs: &'b Matrix<T>) -> Matrix<T>
fn add(self, rhs: &'b Matrix<T>) -> Matrix<T>
Add Matricies by reference
§Arguments
self
- this matrix referencerhs
- 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§impl<'a, 'b, T> Add<&'b T> for &'a Matrix<T>
impl<'a, 'b, T> Add<&'b T> for &'a Matrix<T>
Source§fn add(self, rhs: &'b T) -> Matrix<T>
fn add(self, rhs: &'b T) -> Matrix<T>
Add Matrix and a constant
§Arguments
self
- this matrix referencerhs
- 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§impl<T> Add for Matrix<T>
impl<T> Add for Matrix<T>
Source§fn add(self, rhs: Matrix<T>) -> Matrix<T>
fn add(self, rhs: Matrix<T>) -> Matrix<T>
Add Matricies
§Arguments
self
- this matrixrhs
- 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§impl<'a, 'b, T> Mul<&'b Matrix<T>> for &'a Matrix<T>
impl<'a, 'b, T> Mul<&'b Matrix<T>> for &'a Matrix<T>
Source§fn mul(self, rhs: &'b Matrix<T>) -> Matrix<T>
fn mul(self, rhs: &'b Matrix<T>) -> Matrix<T>
Multiply Matricies by reference
§Arguments
self
- this matrix referencerhs
- 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§impl<'a, 'b, T> Mul<&'b T> for &'a Matrix<T>
impl<'a, 'b, T> Mul<&'b T> for &'a Matrix<T>
Source§fn mul(self, rhs: &'b T) -> Matrix<T>
fn mul(self, rhs: &'b T) -> Matrix<T>
Multiply Matrix and a constant
§Arguments
self
- this matrix referencerhs
- 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§impl<T> Mul for Matrix<T>
impl<T> Mul for Matrix<T>
Source§fn mul(self, rhs: Matrix<T>) -> Matrix<T>
fn mul(self, rhs: Matrix<T>) -> Matrix<T>
Multiply Matricies
§Arguments
self
- this matrixrhs
- 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);
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more