Struct Matrix

Source
pub struct Matrix { /* private fields */ }

Implementations§

Source§

impl Matrix

A Matrix is represented here by 3 fields named: nrows: i16, ncols: i16, data: Vec<Vec>

Source

pub fn nrows(&self) -> i16

Returns the number of rows as i16.

§Examples

use nn::Matrix;

let matrix: Matrix = Matrix::create_random_matrix(6, 6);

println!(“The matrix has {} rows and {} cols”, matrix.nrows(), matrix.ncols());

Source

pub fn ncols(&self) -> i16

Returns the number of cols as i16.

§Examples

use nn::Matrix;

let matrix: Matrix = Matrix::create_random_matrix(6, 6);

println!(“The matrix has {} rows and {} cols”, matrix.nrows(), matrix.ncols());

Source

pub fn set(&mut self, x: i16, y: i16, v: f64)

Sets one value in the Vector.

§Arguments
  • x - A i16 Integer that holds the row, in which the variable is set.
  • y - A i16 Integer that holds the col, in which the variable is set.
  • v - A f64 Float that holds the new variable value.
§Examples

use nn::Matrix;

let matrix: Matrix = Matrix::create_random_matrix(6, 6);

matrix.print();

matrix.set(0, 0, 5.3);

matrix.print();

Source

pub fn get(&self, x: i16, y: i16) -> f64

Returns one value in the Vector by a given row and col as f64.

§Arguments
  • x - A i16 Integer that holds the row, in which the variable is set.
  • y - A i16 Integer that holds the col, in which the variable is set.
§Examples

use nn::Matrix;

let matrix: Matrix = Matrix::create_random_matrix(6, 6);

println!(“Value at row 0 and col 0 has the value {}”, matrix.get(0, 0));

Source

pub fn add(a: &Matrix, b: &Matrix) -> Matrix

Returns two matrices added together one by one as a Matrix.

§Arguments
  • a - A &Matrix.
  • b - A &Matrix.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let matrixb: Matrix = Matrix::create_random_matrix(6, 6);

let matrix_sum: Matrix = Matrix::add(&matrixa, &matrixb);

Source

pub fn sub(a: &Matrix, b: &Matrix) -> Matrix

Returns two matrices subtracted one by one as a Matrix.

§Arguments
  • a - A &Matrix.
  • b - A &Matrix.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let matrixb: Matrix = Matrix::create_random_matrix(6, 6);

let matrix_sub: Matrix = Matrix::sub(&matrixa, &matrixb);

Source

pub fn mul(a: &Matrix, b: &Matrix) -> Matrix

Returns two matrices multiplied one by one as a Matrix.

§Arguments
  • a - A &Matrix.
  • b - A &Matrix.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let matrixb: Matrix = Matrix::create_random_matrix(6, 6);

let matrix_mul: Matrix = Matrix::mul(&matrixa, &matrixb);

Source

pub fn div(a: &Matrix, b: &Matrix) -> Matrix

Returns two matrices divided one by one as a Matrix.

§Arguments
  • a - A &Matrix.
  • b - A &Matrix.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let matrixb: Matrix = Matrix::create_random_matrix(6, 6);

let matrix_div: Matrix = Matrix::div(&matrixa, &matrixb);

Source

pub fn dot(a: &Matrix, b: &Matrix) -> Matrix

Returns the dot product of two matrices as a Matrix.

§Arguments
  • a - A &Matrix.
  • b - A &Matrix.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(1, 2);

let matrixb: Matrix = Matrix::create_random_matrix(2, 1);

let matrix_dot: Matrix = Matrix::dot(&matrixa, &matrixb);

Source

pub fn transpose(a: &Matrix) -> Matrix

Returns the transposed Matrix as a Matrix.

§Arguments
  • a - A &Matrix.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let matrix_t: Matrix = Matrix::transpose(&matrixa);

Source

pub fn map(&self, f: fn(f64) -> f64) -> Matrix

Applies a given function fn(f64) -> f64 to every value in the Matrix and returns the new Matrix as a Matrix.

§Arguments
  • a - A &Matrix.
  • f - A fn(f64) -> f64.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6); let closure = |i: f64| -> f64 { i+10.0 }; let matrixb = matrixa.map(closure);

Source

pub fn scalar_add(a: &Matrix, v: f64) -> Matrix

Adds a single f64 float to every value in a given Matrix and returns it as a Matrix.

§Arguments
  • a - A &Matrix.
  • v - A f64.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let scalar_add: Matrix = Matrix::scalar_add(&matrixa, 10.0);

Source

pub fn scalar_sub(a: &Matrix, v: f64) -> Matrix

Subtracts a single f64 float from every value in a given Matrix and returns it as a Matrix.

§Arguments
  • a - A &Matrix.
  • v - A f64.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let scalar_sub: Matrix = Matrix::scalar_sub(&matrixa, 10.0);

Source

pub fn scalar_sub_first(a: &Matrix, v: f64) -> Matrix

Subtracts every value of the Matrix from a given float f64 and returns it as a Matrix.

§Arguments
  • a - A &Matrix.
  • v - A f64.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let scalar_sub: Matrix = Matrix::scalar_sub_first(&matrixa, 10.0);

Source

pub fn scalar_mult(a: &Matrix, v: f64) -> Matrix

Multiplies every value of the Matrix by a given float f64 and returns it as a Matrix.

§Arguments
  • a - A &Matrix.
  • v - A f64.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let scalar_sub: Matrix = Matrix::scalar_mult(&matrixa, 10.0);

Source

pub fn scalar_div(a: &Matrix, v: f64) -> Matrix

Divides every value of the Matrix by a given float f64 and returns it as a Matrix.

§Arguments
  • a - A &Matrix.
  • v - A f64.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let scalar_sub: Matrix = Matrix::scalar_div(&matrixa, 10.0);

Source

pub fn inverse(a: &Matrix) -> Matrix

Returns a Matrix where every value has the opposite sign as a Matrix.

§Arguments
  • a - A &Matrix.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let matrixa_inverse: Matrix = Matrix::inverse(&matrixa);

Source

pub fn sum_rows(a: &Matrix) -> Matrix

Sums every row together and returns a Matrix with the same number of rows and 1 column.

§Arguments
  • a - A &Matrix.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let matrixa_rows_sum: = Matrix::sum_rows(&matrixa);

Source

pub fn pow(a: &Matrix, v: f64) -> Matrix

Every value in the Matrix is raised to a given power and returned as a Matrix.

§Arguments
  • a - A &Matrix.
  • v - A f64.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let matrixa_pow_2 = Matrix::pow(&matrixa, 2.0);

Source

pub fn filter_by_function(a: &Matrix, f: fn(f64) -> bool, v: f64) -> Matrix

Filters every value in the Matrix by a given function fn(f64) -> bool. If the output of the function is true, the value stays the same. If the output of the function is false, the value is replaced by a given float f64. It returns a Matrix.

§Arguments
  • a - A &Matrix.
  • f - A fn(f64) -> bool.
  • v - A f64.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let closure = |v: f64| -> bool { v < 10.0 };

let matrixb = Matrix::filter_by_function(&matrixa, closure, 0.0);

Source

pub fn get_shape_string(&self) -> String

Returns the shape of a Matrix as a string formatted with Shape:({}, {}) as a String.

§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

println!(“Matrix has {}”, matrixa.get_shape_string());

Source

pub fn print(&self)

Pretty prints the Matrix to the console using print!().

§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

matrixa.print();

Source

pub fn create_matrix(nrows: i16, ncols: i16) -> Matrix

Creates a Matrix by a given number of rows as i16 and cols as i16 where every value is zero and returns it as a Matrix.

§Arguments
  • nrows - A i16.
  • ncols - A i16.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_matrix(6, 6);

Source

pub fn create_random_matrix(nrows: i16, ncols: i16) -> Matrix

Creates a Matrix by a given number of rows as i16 and cols as i16 where every value is random between 0 and 1 and returns it as a Matrix.

§Arguments
  • nrows - A i16.
  • ncols - A i16.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_matrix(6, 6);

Source

pub fn create_matrix_by_float(nrows: i16, ncols: i16, v: f64) -> Matrix

Creates a Matrix by a given number of rows as i16 and cols as i16 where every value is a given float and returns it as a Matrix.

§Arguments
  • nrows - A i16.
  • ncols - A i16.
  • v - A f64.
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_matrix_by_float(6, 6, 10.0);

Source

pub fn create_matrix_from_nested_float_array(v: &[&[f64]]) -> Matrix

Creates a Matrix by nested float array &[&f64] and returns it as a Matrix.

§Arguments
  • v - A &[&f64].
§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_matrix_from_nested_float_array(&[ &[10.0, 1.0, 1.2, 3.4], &[2.3, 4.3, 2.1, 5.4] ]);

matrixa.print();

Trait Implementations§

Source§

impl Add for Matrix

Returns two matrices added together one by one as a Matrix.

§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let matrixb: Matrix = Matrix::create_random_matrix(6, 6);

let matrix_sum: Matrix = matrixa.clone() + matrixb.clone();

Source§

type Output = Matrix

The resulting type after applying the + operator.
Source§

fn add(self, _rhs: Matrix) -> Matrix

Performs the + operation. Read more
Source§

impl AddAssign for Matrix

Adds a Matrix to a given Matrix one by one.

§Examples

use nn::Matrix;

let mut matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let matrixb: Matrix = Matrix::create_random_matrix(6, 6);

matrixa += matrixb;

Source§

fn add_assign(&mut self, _rhs: Matrix)

Performs the += operation. Read more
Source§

impl BitOr for Matrix

Returns the dot product of two matrices as a Matrix.

§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(1, 2);

let matrixb: Matrix = Matrix::create_random_matrix(2, 1);

let matrix_dot: Matrix = matrixa.clone() | matrixb.clone();

Source§

type Output = Matrix

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Matrix) -> Matrix

Performs the | operation. Read more
Source§

impl Clone for Matrix

Source§

fn clone(&self) -> Matrix

Returns a duplicate 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 Div for Matrix

Returns two matrices divided one by one as a Matrix.

§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let matrixb: Matrix = Matrix::create_random_matrix(6, 6);

let matrix_div: Matrix = matrixa.clone() / matrixb.clone();

Source§

type Output = Matrix

The resulting type after applying the / operator.
Source§

fn div(self, _rhs: Matrix) -> Matrix

Performs the / operation. Read more
Source§

impl DivAssign for Matrix

Divides a Matrix by a given Matrix one by one.

§Examples

use nn::Matrix;

let mut matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let matrixb: Matrix = Matrix::create_random_matrix(6, 6);

matrixa /= matrixb;

Source§

fn div_assign(&mut self, _rhs: Matrix)

Performs the /= operation. Read more
Source§

impl Index<Range<usize>> for Matrix

Creates a Matrix by nested float array &[&f64] and returns it as a Matrix.

§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(2, 2);

println!(“The value at Row 0 and Col 0 has the value {}”, matrixa[0..0]);

Source§

type Output = f64

The returned type after indexing.
Source§

fn index(&self, l: Range<usize>) -> &f64

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

impl Mul for Matrix

Returns two matrices multiplied one by one as a Matrix.

§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let matrixb: Matrix = Matrix::create_random_matrix(6, 6);

let matrix_mul: Matrix = matrixa.clone() * matrixb.clone();

Source§

type Output = Matrix

The resulting type after applying the * operator.
Source§

fn mul(self, _rhs: Matrix) -> Matrix

Performs the * operation. Read more
Source§

impl MulAssign for Matrix

Multiplies a Matrix with a given Matrix one by one.

§Examples

use nn::Matrix;

let mut matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let matrixb: Matrix = Matrix::create_random_matrix(6, 6);

matrixa *= matrixb;

Source§

fn mul_assign(&mut self, _rhs: Matrix)

Performs the *= operation. Read more
Source§

impl Not for Matrix

Returns a Matrix where every value has the opposite sign as a Matrix.

§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let matrixa_inverse: Matrix = Matrix::inverse(&matrixa);

Source§

type Output = Matrix

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl Sub for Matrix

Returns two matrices subtracted one by one as a Matrix.

§Examples

use nn::Matrix;

let matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let matrixb: Matrix = Matrix::create_random_matrix(6, 6);

let matrix_sub: Matrix = matrixa.clone() - matrixb.clone();

Source§

type Output = Matrix

The resulting type after applying the - operator.
Source§

fn sub(self, _rhs: Matrix) -> Matrix

Performs the - operation. Read more
Source§

impl SubAssign for Matrix

Subtracts a Matrix with a given Matrix one by one.

§Examples

use nn::Matrix;

let mut matrixa: Matrix = Matrix::create_random_matrix(6, 6);

let matrixb: Matrix = Matrix::create_random_matrix(6, 6);

matrixa -= matrixb;

Source§

fn sub_assign(&mut self, _rhs: Matrix)

Performs the -= operation. Read more

Auto Trait Implementations§

§

impl Freeze for Matrix

§

impl RefUnwindSafe for Matrix

§

impl Send for Matrix

§

impl Sync for Matrix

§

impl Unpin for Matrix

§

impl UnwindSafe for Matrix

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

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

Source§

fn vzip(self) -> V