Struct Matrix

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

Implementations§

Source§

impl Matrix

Source

pub fn new(rows: usize, cols: usize) -> Self

Returns a new Matrix given the rows and columns. The matrix is full of 0’s

§Arguments
  • rows - The amount of rows of the matrix
  • cols - The amount of columns of the matrix
§Examples
use tinymatrix::Matrix;
let matrix = Matrix::new(2, 2);
Source

pub fn from_vector(rows: usize, cols: usize, values: Vec<f64>) -> Self

Returns a new Matrix given the rows, columns and all the data inside the matrix.

§Arguments
  • rows - The amount of rows of the matrix
  • cols - The amount of columns of the matrix
  • values - The data that the Matrix is created from (needs to be an 1D Vector)
§Examples
use tinymatrix::Matrix;
let vector = vec![
    1.0, 2.0,
    3.0, 4.0
];
let matrix = Matrix::from_vector(2, 2, vector);
Source

pub fn get(&self, row: usize, col: usize) -> f64

Returns the data given the coordinates (row and column)

§Arguments
  • row - The row where the data is located
  • col - The column where the data is located
§Examples
use tinymatrix::Matrix;
let vector = vec![
    1.0, 2.0,
    3.0, 4.0
];
let matrix = Matrix::from_vector(2, 2, vector);
matrix.get(0, 0);
§Returns

The value on that coordinate

Source

pub fn set(&mut self, row: usize, col: usize, value: f64)

Sets a value given the coordinates (row and column) and the value

§Arguments
  • row - The row where the data is located
  • col - The column where the data is located
  • value - The value that is being set.
§Examples
use tinymatrix::Matrix;
let vector = vec![
    1.0, 2.0,
    3.0, 4.0
];
let mut matrix = Matrix::from_vector(2, 2, vector);
matrix.set(0, 0, 1.5);
Source

pub fn is_squared(&self) -> bool

Returns if the matrix is squared (e.g the amount of columns is equal to the amount of rows)

§Examples
use tinymatrix::Matrix;
let vector = vec![
    1.0, 2.0,
    3.0, 4.0
];
let matrix = Matrix::from_vector(2, 2, vector);
matrix.is_squared(); // Should return true
§Returns

A boolean based on the result.

Source

pub fn main_diagonal(&self) -> (Vec<f64>, Vec<f64>, Vec<f64>)

Returns the main diagonal, all items above and below the main diagonal of a Matrix (only if it’s squared)

§Examples
use tinymatrix::Matrix;
let vector = vec![
    1.0, 2.0,
    3.0, 4.0
];
let matrix = Matrix::from_vector(2, 2, vector);
let (main_diagonal, above_diagonal, below_diagonal) = matrix.main_diagonal();
§Returns

The main diagonal, all numbers above the main diagoan and all numbers below the main diagonal. All diagonals are Vec (Vectors)

Source

pub fn is_u_triangular(&self) -> bool

Returns if the matrix is upper triangular (e.g the matrix is squared and all numbers below the main diagonal are zero)

§Examples
use tinymatrix::Matrix;
let vector = vec![
    1.0, 2.0,
    0.0, 4.0
];
let matrix = Matrix::from_vector(2, 2, vector);
matrix.is_u_triangular(); // Should return true
§Returns

A boolean based on the result.

Source

pub fn is_l_triangular(&self) -> bool

Returns if the matrix is lower triangular (e.g the matrix is squared and all numbers above the main diagonal are zero)

§Examples
use tinymatrix::Matrix;
let vector = vec![
    1.0, 0.0,
    1.0, 4.0
];
let matrix = Matrix::from_vector(2, 2, vector);
matrix.is_l_triangular(); // Should return true
§Returns

A boolean based on the result.

Source

pub fn concat_rows(&self, other: &Self) -> Self

Returns a new Matrix when given two matrices and both matrices have the same number of rows, join all rows

§Arguments
  • other - The matrix that is concatenating all rows.
§Examples
use tinymatrix::Matrix;
let vector1 = vec![
    1.0, 2.0,
    3.0, 4.0
];
let vector2 = vec![
    5.0, 6.0,
    7.0, 8.0
];
let matrix1 = Matrix::from_vector(2, 2, vector1);
let matrix2 = Matrix::from_vector(2, 2, vector2);
let joined_rows = matrix1.concat_rows(&matrix2);
§Returns

A new matrix with all rows joint.

Source

pub fn concat_cols(&self, other: &Self) -> Self

Returns a new Matrix when given two matrices and they have the same amount of columns, join all columns

§Arguments
  • other - The matrix that is concatenating all columns.
§Examples
use tinymatrix::Matrix;
let vector1 = vec![
    1.0, 2.0,
    3.0, 4.0
];
let vector2 = vec![
    5.0, 6.0,
    7.0, 8.0
];
let matrix1 = Matrix::from_vector(2, 2, vector1);
let matrix2 = Matrix::from_vector(2, 2, vector2);
let joined_rows = matrix1.concat_cols(&matrix2);
§Returns

A new matrix with all columns joint.

Source

pub fn transpose(&self) -> Self

Source

pub fn identity(&self) -> Self

Returns the identity matrix of a matrix.

§Examples
use tinymatrix::Matrix;
let vector = vec![
    1.0, 2.0,
    3.0, 4.0
];
let matrix = Matrix::from_vector(2, 2, vector);
let identity = matrix.identity();
§Returns

A new matrix with all the main diagonal 1 and all the other elements zero.

Source

pub fn determinant(&self) -> Self

Source

pub fn lu_decomposition(&self) -> Self

Source

pub fn print_matrix(&self)

Prints the matrix in a nice way. Useful for debugging.

§Examples
use tinymatrix::Matrix;
let vector = vec![
    1.0, 2.0,
    3.0, 4.0
];
let matrix = Matrix::from_vector(2, 2, vector);
matrix.print_matrix();

Trait Implementations§

Source§

impl Add for Matrix

Source§

type Output = Matrix

The resulting type after applying the + operator.
Source§

fn add(self, other: Matrix) -> Self::Output

Performs the + operation. Read more
Source§

impl Clone for Matrix

Source§

fn clone(&self) -> Matrix

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 Debug for Matrix

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Div<i32> for Matrix

Source§

type Output = Matrix

The resulting type after applying the / operator.
Source§

fn div(self, other: i32) -> Self::Output

Performs the / operation. Read more
Source§

impl Mul<i32> for Matrix

Source§

type Output = Matrix

The resulting type after applying the * operator.
Source§

fn mul(self, other: i32) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul for Matrix

Source§

type Output = Matrix

The resulting type after applying the * operator.
Source§

fn mul(self, other: Matrix) -> Self::Output

Performs the * operation. Read more
Source§

impl Sub for Matrix

Source§

type Output = Matrix

The resulting type after applying the - operator.
Source§

fn sub(self, other: Matrix) -> Self::Output

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.