[][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) -> Matrix
[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) -> Matrix
[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_with_header(
    &self,
    file_path: &str,
    header: Vec<&str>
) -> 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("test.csv");

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

Trait Implementations

impl LinearOps for Matrix
[src]

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

Just clone

fn transpose(&self) -> Matrix
[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) -> Matrix
[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 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) -> (Matrix, Matrix, Matrix, Matrix)
[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<Matrix>
[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<Matrix>
[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 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) -> Matrix
[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 Printable for Matrix
[src]

impl MATLAB for Matrix
[src]

impl PYTHON for Matrix
[src]

impl R for Matrix
[src]

impl PartialEq<Matrix> for Matrix
[src]

PartialEq implements

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl Clone for Matrix
[src]

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

Performs copy-assignment from source. Read more

impl Debug for Matrix
[src]

impl Display for Matrix
[src]

Pretty Print

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 = 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 = 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 Add<Matrix> for i32
[src]

Element-wise addition between f64 & 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 Add<Matrix> for usize
[src]

Element-wise addition between f64 & 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 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 = Matrix

The resulting type after applying the - operator.

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

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 Sub<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 Mul<Matrix> for Matrix
[src]

Element-wise matrix multiplication

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); // [[1,6],[6,16]]

type Output = Matrix

The resulting type after applying the * operator.

impl<T> Mul<T> for Matrix where
    T: Into<f64> + Copy
[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<T> Rem<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 = Matrix

The resulting type after applying the % operator.

impl Rem<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 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 = 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 Send for Matrix

impl Sync for Matrix

Blanket Implementations

impl<T> From for T
[src]

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

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

type Owned = T

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

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

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

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

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

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

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

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