[][src]Struct ml_rust_cuda::math::linear::Matrix

pub struct Matrix { /* fields omitted */ }

A representation of a 32-bit float matrix.

A matrix's elements are stored as a flat, 1-dimensional vector, with an internal tuple which defines the dimensions of the matrix, i.e. how the matrix's elements are interpreted, in (rows, columns) format.

use ml_rust_cuda::math::linear::Matrix;

let elements =
  vec![vec![1_f32, 3_f32, 4_f32],
       vec![2_f32, 5_f32, 8_f32]];
let matrix = Matrix::new(elements);

// "Matrix { elements: [ 1.0, 3.0, 4.0, 2.0, 5.0, 8.0 ], dims: (2, 3) }"
println!("{:?}", matrix);

Implementations

impl Matrix[src]

pub fn new(elements: Vec<Vec<f32>>) -> Self[src]

Returns a matrix representation from a supplied 2-dimensional f32 vector.

Arguments

  • elements - A Vec of rows of f32s for the matrix.

Examples

use ml_rust_cuda::math::linear::Matrix;

let vec_of_vecs =
  vec![vec![1_f32, 3_f32, 4_f32],
       vec![2_f32, 5_f32, 8_f32],
       vec![9_f32, 5_f32, 8_f32],
       vec![3_f32, 4_f32, 0_f32]];
let matrix = Matrix::new(vec_of_vecs);

println!("{}", matrix);

Panics

Panics if all Vecs within elements are not of equal length.

pub fn from_flat(elements: Vec<f32>, dims: (usize, usize)) -> Self[src]

Returns a matrix representation from a flat 1-dimensional f32 vector of specified dimensions.

Arguments

  • elements - A flat vector of f32s stored in row-major form (i.e. elements in the same row are adjacent to each other in elements).
  • dims - The dimensions of the matrix in (rows, columns).

Examples

use ml_rust_cuda::math::linear::Matrix;

let flat_vec =
  vec![1_f32, 3_f32, 4_f32,
       2_f32, 5_f32, 8_f32,
       9_f32, 5_f32, 8_f32,
       3_f32, 4_f32, 0_f32];
let matrix = Matrix::from_flat(flat_vec, (4, 3));

println!("{}", matrix);

pub fn zero(dims: (usize, usize)) -> Self[src]

Returns a matrix of specified dimensions filled with zeros.

Arguments

  • dims - The dimensions of the matrix in (rows, columns).

Examples

use ml_rust_cuda::math::linear::Matrix;

let matrix = Matrix::zero((4, 3));

println!("{}", matrix);

pub fn get(&self, pos: (usize, usize)) -> Option<f32>[src]

Returns the element of the matrix at a given position, or None if the index is out of bounds of the matrix.

Arguments

  • pos - The target element's position in (row, column) form, zero-indexed.

Examples

use ml_rust_cuda::math::linear::Matrix;

let vec_of_vecs =
  vec![vec![1_f32, 3_f32, 4_f32],
       vec![2_f32, 5_f32, 8_f32]];
let matrix = Matrix::new(vec_of_vecs);

assert_eq!(matrix.get((1, 2)), Some(8_f32));
assert!(matrix.get((2, 0)).is_none());

pub fn set(&mut self, pos: (usize, usize), val: f32)[src]

Replaces the element of the matrix at a given position with a given value. Does nothing if the position is out of bounds of the matrix.

Arguments

  • pos - The target element's position in (row, column) form, zero-indexed.
  • val - The value with which to replace the target element.

Examples

use ml_rust_cuda::math::linear::Matrix;

let vec_of_vecs =
  vec![vec![1_f32, 3_f32, 4_f32],
       vec![2_f32, 5_f32, 8_f32]];
let mut matrix = Matrix::new(vec_of_vecs);

matrix.set((1, 2), 5_f32);

assert_eq!(matrix.get((1, 2)), Some(5_f32));

pub fn elements(&self) -> &Vec<f32>[src]

Returns a reference to the matrix's internal flat element vector.

pub fn dims(&self) -> &(usize, usize)[src]

Returns a reference to the matrix's dimension tuple in the form (rows, columns), zero-indexed.

pub fn zero_padded(&self, target: usize) -> Self[src]

Returns a copy of the matrix padded with zeros such that each dimension of the resulting matrix is a multiple of a specified value.

Arguments

  • target - The number by which the resulting matrix's dimensions should be divisible by.

Examples

use ml_rust_cuda::math::linear::Matrix;

let vec_of_vecs =
  vec![vec![1_f32, 3_f32],
       vec![2_f32, 5_f32],
       vec![0_f32, 7_f32],
       vec![8_f32, 4_f32]];
let matrix = Matrix::new(vec_of_vecs);

println!("{}", matrix.zero_padded(3));

pub fn truncated(&self, new_dims: (usize, usize)) -> Self[src]

Returns a copy of the matrix truncated such that the resulting matrix's dimensions match the provided dimensions.

Arguments

  • new_dims - The dimensions of the new matrix in (rows, columns).

Examples

use ml_rust_cuda::math::linear::Matrix;

let vec_of_vecs =
  vec![vec![1_f32, 3_f32, 0_f32, 0_f32],
       vec![2_f32, 5_f32, 0_f32, 0_f32],
       vec![0_f32, 7_f32, 0_f32, 0_f32],
       vec![8_f32, 4_f32, 0_f32, 0_f32],
       vec![0_f32, 0_f32, 0_f32, 0_f32],
       vec![0_f32, 0_f32, 0_f32, 0_f32]];
let matrix = Matrix::new(vec_of_vecs);

println!("{}", matrix.truncated((4, 2)));

pub fn transposed(&self) -> Self[src]

Returns a transposed copy of the matrix.

Uses a CUDA kernel under the hood.

Examples

use ml_rust_cuda::math::linear::Matrix;

let vec_of_vecs =
  vec![vec![1_f32, 3_f32],
       vec![2_f32, 5_f32],
       vec![0_f32, 7_f32],
       vec![8_f32, 4_f32]];
let matrix = Matrix::new(vec_of_vecs);

println!("{}", matrix.transposed());

Trait Implementations

impl<'_> Add<&'_ Matrix> for &'_ Matrix[src]

type Output = Matrix

The resulting type after applying the + operator.

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

Uses a CUDA kernel under the hood.

Panics

Panics if both operand matrices are not of the same dimension.

impl Clone for Matrix[src]

impl Debug for Matrix[src]

impl Display for Matrix[src]

impl<'_> Mul<&'_ Matrix> for f32[src]

type Output = Matrix

The resulting type after applying the * operator.

fn mul(self, other: &Matrix) -> Self::Output[src]

Uses a CUDA kernel under the hood.

impl<'_> Mul<&'_ Matrix> for &'_ Matrix[src]

type Output = Matrix

The resulting type after applying the * operator.

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

Uses a CUDA kernel under the hood.

Panics

Panics if the number of columns in the first operand is not equal to the number of rows in the second operand.

impl<'_, '_> Mul<&'_ Vector> for &'_ Matrix[src]

type Output = Vector

The resulting type after applying the * operator.

fn mul(self, other: &Vector) -> Self::Output[src]

Uses the same CUDA kernel as matrix multiplication.

Panics

Panics if the number of columns in the matrix is not equal to the dimension of the vector.

impl PartialEq<Matrix> for Matrix[src]

fn eq(&self, other: &Self) -> bool[src]

Uses a CUDA kernel under the hood.

impl<'_> Sub<&'_ Matrix> for &'_ Matrix[src]

type Output = Matrix

The resulting type after applying the - operator.

fn sub(self, other: Self) -> Self::Output[src]

Uses two CUDA kernels under the hood (one for scalar multiplication by -1, the other for addition).

Panics

Panics if both operand matrices are not of the same dimension.

Auto Trait Implementations

impl RefUnwindSafe for Matrix

impl Send for Matrix

impl Sync for Matrix

impl Unpin for Matrix

impl UnwindSafe for Matrix

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,