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§
source§impl Matrix
impl Matrix
Main matrix structure
sourcepub fn change_shape(&self) -> Matrix
pub fn change_shape(&self) -> Matrix
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);
sourcepub fn spread(&self) -> String
pub fn spread(&self) -> String
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
sourcepub fn col(&self, index: usize) -> Vector
pub fn col(&self, index: usize) -> Vector
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));
sourcepub fn row(&self, index: usize) -> Vector
pub fn row(&self, index: usize) -> Vector
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));
sourcepub fn diag(&self) -> Vector
pub fn diag(&self) -> Vector
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));
Trait Implementations§
source§impl Add<Matrix> for Matrix
impl Add<Matrix> for Matrix
Element-wise addition of Matrix
Caution
You should remember ownership. If you use Matrix
a,b
then you can’t use them after.
source§impl Add<Matrix> for f64
impl Add<Matrix> for f64
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));
source§impl Add<Matrix> for i32
impl Add<Matrix> for i32
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));
source§impl Add<Matrix> for usize
impl Add<Matrix> for usize
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));
source§impl<T> Add<T> for Matrixwhere
T: Into<f64> + Copy,
impl<T> Add<T> for Matrixwhere
T: Into<f64> + Copy,
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));
source§impl Index<(usize, usize)> for Matrix
impl Index<(usize, usize)> for Matrix
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);
source§impl IndexMut<(usize, usize)> for Matrix
impl IndexMut<(usize, usize)> for Matrix
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));
source§impl LinearAlgebra for Matrix
impl LinearAlgebra for Matrix
source§fn lu(&self) -> Option<PQLU>
fn lu(&self) -> Option<PQLU>
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));
source§fn det(&self) -> f64
fn det(&self) -> f64
Determinant
Examples
extern crate peroxide;
use peroxide::*;
let a = matrix!(1;4;1, 2, 2, Row);
assert_eq!(a.det(), -2f64);
source§fn block(&self) -> (Matrix, Matrix, Matrix, Matrix)
fn block(&self) -> (Matrix, Matrix, Matrix, Matrix)
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));
source§fn inv(&self) -> Option<Matrix>
fn inv(&self) -> Option<Matrix>
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);
source§impl LinearOps for Matrix
impl LinearOps for Matrix
source§impl Mul<Matrix> for Matrix
impl Mul<Matrix> for Matrix
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]]
source§impl Neg for Matrix
impl Neg for Matrix
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]]
source§impl PartialEq<Matrix> for Matrix
impl PartialEq<Matrix> for Matrix
PartialEq implements
source§impl Rem<Matrix> for Vector
impl Rem<Matrix> for Vector
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));
source§impl<T> Rem<T> for Matrixwhere
T: LinearOps,
impl<T> Rem<T> for Matrixwhere
T: LinearOps,
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));
source§impl Statistics for Matrix
impl Statistics for Matrix
source§fn mean(&self) -> Vector
fn mean(&self) -> Vector
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));
source§fn var(&self) -> Vector
fn var(&self) -> Vector
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));
source§fn sd(&self) -> Vector
fn sd(&self) -> Vector
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));
source§fn cov(&self) -> Matrix
fn cov(&self) -> Matrix
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
type Array = Matrix
type Value = Vec<f64, Global>
fn cor(&self) -> Matrix
source§impl Sub<Matrix> for Matrix
impl Sub<Matrix> for Matrix
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]]
source§impl Sub<Matrix> for f64
impl Sub<Matrix> for f64
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));