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

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: usize§col: usize§shape: Shape

Implementations§

Main matrix structure

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);

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

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));

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));

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));

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));

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");

Trait Implementations§

Element-wise addition of Matrix

Caution

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

The resulting type after applying the + operator.
Performs the + operation. Read more

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));
The resulting type after applying the + operator.
Performs the + operation. Read more

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));
The resulting type after applying the + operator.
Performs the + operation. Read more

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));
The resulting type after applying the + operator.
Performs the + operation. Read more

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));
The resulting type after applying the + operator.
Performs the + operation. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

Matrix generic constructor

You can use any numeric type vector e.g. u32, i32, i64, …

Examples
extern crate peroxide;
use peroxide::*;

let a = Matrix::new(
    vec![1,2,3,4], // Can use any Into<f64> type
    2,
    2,
    Row,
);
Formats the value using the given formatter. Read more

Pretty Print

Formats the value using the given formatter. Read more

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);
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more

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));
Performs the mutable indexing (container[index]) operation. Read more

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));

Determinant

Examples
extern crate peroxide;
use peroxide::*;

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

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));

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);

Just clone

Transpose

Examples
extern crate peroxide;
use peroxide::*;

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

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());

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]]
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more

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]]
The resulting type after applying the - operator.
Performs the unary - operation. Read more

PartialEq implements

This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

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));
The resulting type after applying the % operator.
Performs the % operation. Read more

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));
The resulting type after applying the % operator.
Performs the % operation. Read more

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));

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));

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));

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

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]]
The resulting type after applying the - operator.
Performs the - operation. Read more

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));
The resulting type after applying the - operator.
Performs the - operation. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.