Struct ktensor::math::Matrix
[−]
[src]
pub struct Matrix<T> { /* fields omitted */ }
A structure of values
Methods
impl<T> Matrix<T>
[src]
fn new(dimensions: Vec2, buffer: Vec<T>) -> Matrix<T>
Returns a new Matrix
Arguments
dimensions
- dimensions ofMatrix
buffer
-Vec
of values
Example
let matrix = ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).collect());
fn len(&self) -> usize
Returns the total number of items in the Matrix
fn dim(&self) -> Vec2
Returns the dimensions of the Matrix
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); }
fn buffer(&self) -> &Vec<T>
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); }
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]); }
impl<T> Matrix<T> where
T: Copy,
[src]
T: Copy,
fn get(&self, Vec2: Vec2) -> T
Returns value at Vec2
Arguments
Vec2
- coordinates of the value in theMatrix
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);
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); }
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);
impl<'a, T> Matrix<T> where
T: Mul<Output = T> + Copy,
[src]
T: Mul<Output = T> + Copy,
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
impl<T> Clone for Matrix<T> where
T: Copy,
[src]
T: Copy,
fn clone(&self) -> Matrix<T>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<T> Add<Matrix<T>> for Matrix<T> where
T: Add<Output = T> + Copy,
[src]
T: Add<Output = T> + Copy,
type Output = Matrix<T>
The resulting type after applying the +
operator
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);
impl<'a, 'b, T> Add<&'b Matrix<T>> for &'a Matrix<T> where
T: Add<Output = T> + Copy,
[src]
T: Add<Output = T> + Copy,
type Output = Matrix<T>
The resulting type after applying the +
operator
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);
impl<T> Add<T> for Matrix<T> where
T: Add<Output = T> + Copy,
[src]
T: Add<Output = T> + Copy,
type Output = Matrix<T>
The resulting type after applying the +
operator
fn add(self, rhs: T) -> Matrix<T>
impl<'a, 'b, T> Add<&'b T> for &'a Matrix<T> where
T: Add<Output = T> + Copy,
[src]
T: Add<Output = T> + Copy,
type Output = Matrix<T>
The resulting type after applying the +
operator
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);
impl<T> Mul<Matrix<T>> for Matrix<T> where
T: Mul<Output = T> + Add<Output = T> + Copy,
[src]
T: Mul<Output = T> + Add<Output = T> + Copy,
type Output = Matrix<T>
The resulting type after applying the *
operator
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);
impl<'a, 'b, T> Mul<&'b Matrix<T>> for &'a Matrix<T> where
T: Mul<Output = T> + Add<Output = T> + Copy,
[src]
T: Mul<Output = T> + Add<Output = T> + Copy,
type Output = Matrix<T>
The resulting type after applying the *
operator
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);
impl<T> Mul<T> for Matrix<T> where
T: Mul<Output = T> + Copy,
[src]
T: Mul<Output = T> + Copy,
type Output = Matrix<T>
The resulting type after applying the *
operator
fn mul(self, rhs: T) -> Matrix<T>
impl<'a, 'b, T> Mul<&'b T> for &'a Matrix<T> where
T: Mul<Output = T> + Copy,
[src]
T: Mul<Output = T> + Copy,
type Output = Matrix<T>
The resulting type after applying the *
operator
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);