[][src]Struct peroxide::structure::matrix::Matrix

pub struct Matrix {
    pub data: Vec<f64>,
    pub row: usize,
    pub col: usize,
    pub shape: Shape,
}

R-like matrix structure

Examples

extern crate peroxide;
use peroxide::*;

let a = Matrix {
    data: vec![1f64,2f64,3f64,4f64],
    row: 2,
    col: 2,
    shape: Row,
}; // [[1,2],[3,4]]

Fields

data: Vec<f64>row: usizecol: usizeshape: Shape

Methods

impl Matrix[src]

Main matrix structure

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

Change Bindings

Row -> Col or Col -> Row

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix(vec![1,2,3,4],2,2,Row);
assert_eq!(a.shape, Row);
let b = a.change_shape();
assert_eq!(b.shape, Col);

pub fn spread(&self) -> String[src]

Spread data(1D vector) to 2D formatted String

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix(vec![1,2,3,4],2,2,Row);
println!("{}", a.spread()); // same as println!("{}", a);
// Result:
//       c[0] c[1]
// r[0]     1    3
// r[1]     2    4

pub fn col(&self, index: usize) -> Vector[src]

Extract Column

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix(c!(1,2,3,4), 2, 2, Row);
assert_eq!(a.col(0), c!(1,3));

pub fn row(&self, index: usize) -> Vector[src]

Extract Row

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix(c!(1,2,3,4), 2, 2, Row);
assert_eq!(a.row(0), c!(1,2));

pub fn diag(&self) -> Vector[src]

Extract diagonal components

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix!(1;4;1, 2, 2, Row);
assert_eq!(a.diag(), c!(1,4));

pub fn swap(&self, idx1: usize, idx2: usize, shape: Shape) -> Self[src]

Swap row or col

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix!(1;4;1, 2, 2, Row);
assert_eq!(a.swap(0,1,Row), matrix(c!(3,4,1,2),2,2,Row));
assert_eq!(a.swap(0,1,Col), matrix(c!(2,4,1,3),2,2,Col));

pub fn write(&self, file_path: &str) -> Result<(), Box<dyn Error>>[src]

Write to CSV

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix(c!(1,2,3,3,2,1), 3, 2, Col);
a.write("test.csv");

pub fn write_round(
    &self,
    file_path: &str,
    round: usize
) -> Result<(), Box<dyn Error>>
[src]

Write to CSV (with round option)

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix(c!(1,2,3,3,2,1), 3, 2, Col);
a.write_round("test.csv", 0);

pub fn write_with_header(
    &self,
    file_path: &str,
    header: Vec<&str>
) -> Result<(), Box<dyn Error>>
[src]

pub fn write_with_header_round(
    &self,
    file_path: &str,
    header: Vec<&str>,
    round: usize
) -> Result<(), Box<dyn Error>>
[src]

pub fn read(
    file_path: &str,
    header: bool,
    delimiter: char
) -> Result<Matrix, Box<dyn Error>>
[src]

Read from CSV

Examples

extern crate peroxide;
use peroxide::*;
use std::process;

let a = matrix(c!(1,2,3,3,2,1), 3, 2, Col);
a.write_round("test.csv", 0);

let b = Matrix::read("test.csv", false, ','); // header = false, delimiter = ','
match b {
    Ok(mat) => println!("{}", mat),
    Err(err) => {
        println!("{}", err);
        process::exit(1);
    }
}

pub fn subs_col(&mut self, idx: usize, v: Vec<f64>)[src]

Substitute Col

pub fn subs_row(&mut self, idx: usize, v: Vec<f64>)[src]

Substitute Row

pub fn from_index<F, G>(f: F, size: (usize, usize)) -> Matrix where
    F: Fn(usize, usize) -> G + Copy,
    G: Into<f64>, 
[src]

From index operations

pub fn to_vec(&self) -> Vec<Vec<f64>>[src]

Matrix to Vec<Vec<f64>>

To send Matrix to inline-python

Trait Implementations

impl Statistics for Matrix[src]

type Array = Matrix

type Value = Vector

fn mean(&self) -> Vector[src]

Column Mean

Examples

extern crate peroxide;
use peroxide::*;

let m = matrix(c!(1,3,3,1), 2, 2, Col);
assert_eq!(m.mean(), c!(2,2));

fn var(&self) -> Vector[src]

Column variance

Examples

extern crate peroxide;
use peroxide::*;

let m = matrix(c!(1,2,3,3,2,1), 3, 2, Col);
assert!(nearly_eq(m.var()[0], 1));

fn sd(&self) -> Vector[src]

Column Standard Deviation

Examples

extern crate peroxide;
use peroxide::*;

let m = matrix(c!(1,2,3,3,2,1), 3, 2, Col);
assert!(nearly_eq(m.sd()[0], 1));

fn cov(&self) -> Self[src]

Covariance Matrix (Column based)

Examples

extern crate peroxide;
use peroxide::*;

let m = matrix(c!(1,2,3,3,2,1), 3, 2, Col);
println!("{}", m.cov());

//         c[0]    c[1]
// r[0]  1.0000 -1.0000
// r[1] -1.0000  1.0000

impl LinearOps for Matrix[src]

fn to_matrix(&self) -> Self[src]

Just clone

fn transpose(&self) -> Self[src]

Transpose

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix(vec![1,2,3,4], 2, 2, Row);
println!("{}", a); // [[1,3],[2,4]]

fn t(&self) -> Self[src]

R-like transpose function

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix!(1;4;1, 2, 2, Row);
assert_eq!(a.transpose(), a.t());

impl FP for Matrix[src]

impl LinearAlgebra for Matrix[src]

fn norm(&self, norm: Norm) -> f64[src]

Matrix norm

Kinds

  • Frobenius : Frobenius norm
  • PQ(usize, usize) : L_pq norm
  • One : 1-norm
  • Infinity : Infinity norm

fn lu(&self) -> Option<PQLU>[src]

LU Decomposition Implements

Description

It use complete pivoting LU decomposition. You can get two permutations, and LU matrices.

Caution

It returns Option<PQLU> - You should unwrap to obtain real value. PQLU has four field - p, q, l, u. p, q are permutations. l, u are matrices.

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix(vec![1,2,3,4], 2, 2, Row);
let pqlu = a.lu().unwrap();
let (p,q,l,u) = (pqlu.p, pqlu.q, pqlu.l, pqlu.u);
assert_eq!(p, vec![(0,1)]); // swap 0 & 1 (Row)
assert_eq!(q, vec![(0,1)]); // swap 0 & 1 (Col)
assert_eq!(l, matrix(c!(1,0,0.5,1),2,2,Row));
assert_eq!(u, matrix(c!(4,3,0,-0.5),2,2,Row));

fn det(&self) -> f64[src]

Determinant

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix!(1;4;1, 2, 2, Row);
assert_eq!(a.det(), -2f64);

fn block(&self) -> (Self, Self, Self, Self)[src]

Block Partition

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix!(1;16;1, 4, 4, Row);
let (m1, m2, m3, m4) = a.block();
assert_eq!(m1, matrix(c!(1,2,5,6), 2, 2, Row));
assert_eq!(m2, matrix(c!(3,4,7,8), 2, 2, Row));
assert_eq!(m3, matrix(c!(9,10,13,14), 2, 2, Row));
assert_eq!(m4, matrix(c!(11,12,15,16), 2, 2, Row));

let b = matrix!(1;16;1, 4, 4, Col);
let (m1, m2, m3, m4) = b.block();
assert_eq!(m1, matrix(c!(1,2,5,6), 2, 2, Col));
assert_eq!(m3, matrix(c!(3,4,7,8), 2, 2, Col));
assert_eq!(m2, matrix(c!(9,10,13,14), 2, 2, Col));
assert_eq!(m4, matrix(c!(11,12,15,16), 2, 2, Col));

fn inv(&self) -> Option<Self>[src]

Inverse of Matrix

Caution

inv function returns Option<Matrix> Thus, you should use pattern matching or unwrap to obtain inverse.

Examples

extern crate peroxide;
use peroxide::*;

// Non-singular
let a = matrix!(1;4;1, 2, 2, Row);
assert_eq!(a.inv().unwrap(), matrix(c!(-2,1,1.5,-0.5),2,2,Row));

// Singular
let b = matrix!(1;9;1, 3, 3, Row);
assert_eq!(b.inv(), None);

fn pseudo_inv(&self) -> Option<Self>[src]

Moore-Penrose Pseudo inverse

Description

(X^T X)^{-1} X

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix!(1;4;1, 2, 2, Row);
let inv_a = a.inv().unwrap();
let pse_a = a.pseudo_inv().unwrap();

assert_eq!(inv_a, pse_a); // Nearly equal

impl MATLAB for Matrix[src]

impl PYTHON for Matrix[src]

impl R for Matrix[src]

impl Pickle for Matrix[src]

impl Printable for Matrix[src]

impl Clone for Matrix[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl PartialEq<Matrix> for Matrix[src]

PartialEq implements

#[must_use] fn ne(&self, other: &Rhs) -> bool1.0.0[src]

This method tests for !=.

impl Display for Matrix[src]

Pretty Print

impl Debug for Matrix[src]

impl Add<Matrix> for Matrix[src]

Element-wise addition of Matrix

Caution

You should remember ownership. If you use Matrix a,b then you can't use them after.

type Output = Self

The resulting type after applying the + operator.

impl<'a, 'b> Add<&'b Matrix> for &'a Matrix[src]

Element-wise addition of &Matrix

type Output = Matrix

The resulting type after applying the + operator.

impl<T> Add<T> for Matrix where
    T: Into<f64> + Copy
[src]

Element-wise addition between Matrix & f64

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix!(1;4;1, 2, 2, Row);
assert_eq!(a + 1, matrix!(2;5;1, 2, 2, Row));

type Output = Self

The resulting type after applying the + operator.

impl<'a, T> Add<T> for &'a Matrix where
    T: Into<f64> + Copy
[src]

Element-wise addition between &Matrix & f64

type Output = Matrix

The resulting type after applying the + operator.

impl Add<Matrix> for f64[src]

Element-wise addition between f64 & matrix

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix!(1;4;1, 2, 2, Row);
assert_eq!(1f64 + a, matrix!(2;5;1, 2, 2, Row));

type Output = Matrix

The resulting type after applying the + operator.

impl<'a> Add<&'a Matrix> for f64[src]

Element-wise addition between f64 & &Matrix

type Output = Matrix

The resulting type after applying the + operator.

impl Add<Matrix> for i32[src]

Element-wise addition between i32 & Matrix

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix!(1;4;1, 2, 2, Row);
assert_eq!(1 + a, matrix!(2;5;1, 2, 2, Row));

type Output = Matrix

The resulting type after applying the + operator.

impl<'a> Add<&'a Matrix> for i32[src]

Element-wise addition between i32 & &Matrix

type Output = Matrix

The resulting type after applying the + operator.

impl Add<Matrix> for usize[src]

Element-wise addition between usize & matrix

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix!(1;4;1, 2, 2, Row);
assert_eq!(1 as usize + a, matrix!(2;5;1, 2, 2, Row));

type Output = Matrix

The resulting type after applying the + operator.

impl<'a> Add<&'a Matrix> for usize[src]

Element-wise addition between usize & &Matrix

type Output = Matrix

The resulting type after applying the + operator.

impl Sub<Matrix> for Matrix[src]

Subtraction between Matrix

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix(vec![1,2,3,4],2,2,Row);
let b = matrix(vec![1,2,3,4],2,2,Col);
println!("{}", a - b); // [[0, -1], [1, 0]]

type Output = Self

The resulting type after applying the - operator.

impl<'a, 'b> Sub<&'b Matrix> for &'a Matrix[src]

Subtraction between &'a Matrix

type Output = Matrix

The resulting type after applying the - operator.

impl<T> Sub<T> for Matrix where
    T: Into<f64> + Copy
[src]

Subtraction between Matrix & f64

type Output = Self

The resulting type after applying the - operator.

impl<'a, T> Sub<T> for &'a Matrix where
    T: Into<f64> + Copy
[src]

Subtraction between &Matrix & f64

type Output = Matrix

The resulting type after applying the - operator.

impl Sub<Matrix> for f64[src]

Subtraction Matrix with f64

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix(vec![1,2,3,4],2,2,Row);
assert_eq!(a - 1f64, matrix!(0;3;1, 2, 2, Row));

type Output = Matrix

The resulting type after applying the - operator.

impl<'a> Sub<&'a Matrix> for f64[src]

type Output = Matrix

The resulting type after applying the - operator.

impl Sub<Matrix> for i32[src]

type Output = Matrix

The resulting type after applying the - operator.

impl<'a> Sub<&'a Matrix> for i32[src]

type Output = Matrix

The resulting type after applying the - operator.

impl Sub<Matrix> for usize[src]

type Output = Matrix

The resulting type after applying the - operator.

impl<'a> Sub<&'a Matrix> for usize[src]

type Output = Matrix

The resulting type after applying the - operator.

impl Mul<f64> for Matrix[src]

Element-wise multiplication between Matrix vs f64

type Output = Self

The resulting type after applying the * operator.

impl Mul<i64> for Matrix[src]

type Output = Self

The resulting type after applying the * operator.

impl Mul<i32> for Matrix[src]

type Output = Self

The resulting type after applying the * operator.

impl Mul<usize> for Matrix[src]

type Output = Self

The resulting type after applying the * operator.

impl<'a> Mul<f64> for &'a Matrix[src]

type Output = Matrix

The resulting type after applying the * operator.

impl<'a> Mul<i64> for &'a Matrix[src]

type Output = Matrix

The resulting type after applying the * operator.

impl<'a> Mul<i32> for &'a Matrix[src]

type Output = Matrix

The resulting type after applying the * operator.

impl<'a> Mul<usize> for &'a Matrix[src]

type Output = Matrix

The resulting type after applying the * operator.

impl Mul<Matrix> for f64[src]

type Output = Matrix

The resulting type after applying the * operator.

impl Mul<Matrix> for i64[src]

type Output = Matrix

The resulting type after applying the * operator.

impl Mul<Matrix> for i32[src]

type Output = Matrix

The resulting type after applying the * operator.

impl Mul<Matrix> for usize[src]

type Output = Matrix

The resulting type after applying the * operator.

impl<'a> Mul<&'a Matrix> for f64[src]

type Output = Matrix

The resulting type after applying the * operator.

impl<'a> Mul<&'a Matrix> for i64[src]

type Output = Matrix

The resulting type after applying the * operator.

impl<'a> Mul<&'a Matrix> for i32[src]

type Output = Matrix

The resulting type after applying the * operator.

impl<'a> Mul<&'a Matrix> for usize[src]

type Output = Matrix

The resulting type after applying the * operator.

impl<T> Mul<T> for Matrix where
    T: LinearOps
[src]

Matrix Multiplication

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix!(1;4;1, 2, 2, Row);
let b = matrix!(1;4;1, 2, 2, Col);
assert_eq!(a * b, matrix(c!(5, 11, 11, 25), 2, 2, Row));

let m = matrix!(1;4;1, 2, 2, Row);
let v = c!(1,2);
assert_eq!(m * v, matrix(c!(5,11),2,1,Col));

type Output = Self

The resulting type after applying the * operator.

impl<'a, 'b, T> Mul<&'b T> for &'a Matrix where
    T: LinearOps
[src]

type Output = Matrix

The resulting type after applying the * operator.

impl Mul<Matrix> for Vector[src]

Matrix multiplication for Vector vs Matrix

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix!(1;4;1, 2, 2, Row);
let v = c!(1,2);
assert_eq!(v * a, matrix(c!(7,10),1,2,Row));

type Output = Matrix

The resulting type after applying the * operator.

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

type Output = Matrix

The resulting type after applying the * operator.

impl Neg for Matrix[src]

Negation of Matrix

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix(vec![1,2,3,4],2,2,Row);
println!("{}", -a); // [[-1,-2],[-3,-4]]

type Output = Self

The resulting type after applying the - operator.

impl<'a> Neg for &'a Matrix[src]

Negation of &'a Matrix

type Output = Matrix

The resulting type after applying the - operator.

impl Index<(usize, usize)> for Matrix[src]

Index for Matrix

(usize, usize) -> f64

Examples

extern crate peroxide;
use peroxide::*;

let a = matrix(vec![1,2,3,4],2,2,Row);
assert_eq!(a[(0,1)], 2f64);

type Output = f64

The returned type after indexing.

impl IndexMut<(usize, usize)> for Matrix[src]

IndexMut for Matrix (Assign)

(usize, usize) -> f64

Examples

extern crate peroxide;
use peroxide::*;

let mut a = matrix!(1;4;1, 2, 2, Row);
a[(1,1)] = 10.0;
assert_eq!(a, matrix(c!(1,2,3,10), 2, 2, Row));

Auto Trait Implementations

impl Sync for Matrix

impl Send for Matrix

impl Unpin for Matrix

impl RefUnwindSafe for Matrix

impl UnwindSafe for Matrix

Blanket Implementations

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

type Owned = T

The resulting type after obtaining ownership.

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

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

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<T> BorrowMut<T> for T where
    T: ?Sized
[src]

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

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

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

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

impl<T, U> IntoPy<U> for T where
    U: FromPy<T>, 
[src]