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::fuga::*;

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

Implementations

Main matrix structure

Raw pointer for self.data

Raw mutable pointer for self.data

Slice of self.data

Examples
extern crate peroxide;
use peroxide::fuga::*;

let a = matrix(vec![1,2,3,4], 2, 2, Col);
let b = a.as_slice();
assert_eq!(b, &[1f64,2f64,3f64,4f64]);

Mutable slice of self.data

Examples
extern crate peroxide;
use peroxide::fuga::*;

let mut a = matrix(vec![1,2,3,4], 2, 2, Col);
let mut b = a.as_mut_slice();
b[0] = 5f64;
assert_eq!(b, &[5f64, 2f64, 3f64, 4f64]);
assert_eq!(a, matrix(vec![5,2,3,4], 2, 2, Col));

Change Bindings

Row -> Col or Col -> Row

Examples
extern crate peroxide;
use peroxide::fuga::*;

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

Change Bindings Mutably

Row -> Col or Col -> Row

Examples
extern crate peroxide;
use peroxide::fuga::*;

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::fuga::*;

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
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

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

Extract Row

Examples
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

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

Extract diagonal components

Examples
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

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

Transpose

Examples
extern crate peroxide;
use peroxide::fuga::*;

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

R-like transpose function

Examples
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

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

Should check shape

Substitute Col

Substitute Row

From index operations

Matrix to Vec<Vec<f64>>

To send Matrix to inline-python

Submatrix

Description

Return below elements of matrix to new matrix

$$ \begin{pmatrix} \ddots & & & & \\ & start & \cdots & end.1 & \\ & \vdots & \ddots & \vdots & \\ & end.0 & \cdots & end & \\ & & & & \ddots \end{pmatrix} $$

Examples
extern crate peroxide;
use peroxide::fuga::*;
 
fn main() {
    let a = ml_matrix("1 2 3;4 5 6;7 8 9");
    let b = ml_matrix("5 6;8 9");
    let c = a.submat((1, 1), (2, 2));
    assert_eq!(b, c);   
}

Substitute matrix to specific position

Description

Substitute below elements of matrix

$$ \begin{pmatrix} \ddots & & & & \\ & start & \cdots & end.1 & \\ & \vdots & \ddots & \vdots & \\ & end.0 & \cdots & end & \\ & & & & \ddots \end{pmatrix} $$

Examples
extern crate peroxide;
use peroxide::fuga::*;
 
fn main() {
    let mut a = ml_matrix("1 2 3;4 5 6;7 8 9");
    let b = ml_matrix("1 2;3 4");
    let c = ml_matrix("1 2 3;4 1 2;7 3 4");
    a.subs_mat((1,1), (2,2), &b);
    assert_eq!(a, c);       
}

Matrix from series

Example
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    let a = Series::new(c!(1,2,3,4));
    let m = Matrix::from_series(&a, 2, 2, Row);
     
    assert_eq!(m, matrix(c!(1,2,3,4), 2, 2, Row));
}

Trait Implementations

Element-wise addition between f64 & &Matrix

The resulting type after applying the + operator.

Performs the + operation. Read more

Element-wise addition between i32 & &Matrix

The resulting type after applying the + operator.

Performs the + operation. Read more

Element-wise addition between usize & &Matrix

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

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

#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    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 i32 & Matrix

Examples

#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    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 usize & matrix

Examples

#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    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

#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    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

Element-wise addition between &Matrix & f64

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

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Pretty Print

Formats the value using the given formatter. Read more

Element-wise division between Matrix vs f64

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

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

Column map

Example
use peroxide::fuga::*;

fn main() {
    let x = ml_matrix("1 2;3 4;5 6");
    let y = x.col_map(|c| c.fmap(|t| t - c.mean()));

    assert_eq!(y, ml_matrix("-2 -2;0 0;2 2"));
}

Row map

Example
use peroxide::fuga::*;

fn main() {
    let x = ml_matrix("1 2 3;4 5 6");
    let y = x.row_map(|r| r.fmap(|t| t - r.mean()));

    assert_eq!(y, ml_matrix("-1 0 1;-1 0 1"));
}

Index for Matrix

(usize, usize) -> f64

Examples

extern crate peroxide;
use peroxide::fuga::*;

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

#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    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

Frobenius inner product

&Matrix to &Vec

Converts this type into the (usually inferred) input type.

Vec to Matrix

Converts this type into the (usually inferred) input type.

Converts this type into the (usually inferred) input type.

Converts this type into the (usually inferred) input type.

Converts this type into the (usually inferred) input type.

Matrix to Vec

Converts this type into the (usually inferred) input type.

Backward Substitution for Upper Triangular

Forward substitution for Lower Triangular

LU Decomposition Implements (Complete Pivot)

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
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    let a = matrix(vec![1,2,3,4], 2, 2, Row);
    let pqlu = a.lu();
    let (p,q,l,u) = (pqlu.p, pqlu.q, pqlu.l, pqlu.u);
    assert_eq!(p, vec![1]); // swap 0 & 1 (Row)
    assert_eq!(q, vec![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));
}

QR Decomposition

Translation of RosettaCode#Python

Example
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    let a = ml_matrix("12 -51 4;6 167 -68; -4 24 -41");
    let qr = a.qr();
    let r = ml_matrix("-14 -21 14; 0 -175 70; 0 0 -35");
    #[cfg(feature="O3")]
    {
        assert_eq!(r, qr.r);
    }
    qr.r.print();
}

Singular Value Decomposition

Examples
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    let a = ml_matrix("3 2 2;2 3 -2");
    #[cfg(feature="O3")]
    {
        let svd = a.svd();
        assert!(eq_vec(&vec![5f64, 3f64], &svd.s, 1e-7));
    }
    a.print();
}

Cholesky Decomposition

Examples
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    let a = ml_matrix("1 2;2 5");
    #[cfg(feature = "O3")]
    {
        let u = a.cholesky(Upper);
        let l = a.cholesky(Lower);

        assert_eq!(u, ml_matrix("1 2;0 1"));
        assert_eq!(l, ml_matrix("1 0;2 1"));
    }
    a.print();
}

Reduced Row Echelon Form

Implementation of RosettaCode

Determinant

Examples
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

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

Block Partition

Examples
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    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
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

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

    // Singular
    //let b = matrix!(1;9;1, 3, 3, Row);
    //let c = b.inv(); // Can compile but..
    //let d = b.det();
    //assert_eq!(d, 0f64);
}

Moore-Penrose Pseudo inverse

Description

$X^\dagger = (X^T X)^{-1} X^T$

Examples
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

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

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

Solve with Vector

Solve options
  • LU: Gaussian elimination with Complete pivoting LU (GECP)
  • WAZ: Solve with WAZ decomposition
Reference
  • Biswa Nath Datta, Numerical Linear Algebra and Applications, Second Edition
  • Ke Chen, Matrix Preconditioning Techniques and Applications, Cambridge Monographs on Applied and Computational Mathematics

TODO: Transpose Matrix as Linear operator for Vector

Row pointer

Examples
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    let a = ml_matrix("1 2;3 4");
    unsafe {
        let b = a.row_ptr(1);
        let b_vec = ptr_to_vec(&b);
        assert_eq!(b_vec, vec![3f64, 4f64]);
    }
}

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

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

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

Matrix Multiplication

Examples

#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    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));
}

The resulting type after applying the * operator.

Performs the * operation. Read more

Matrix multiplication for Vec vs Matrix

Examples

#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

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

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

Element-wise multiplication between Matrix vs f64

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

Negation of Matrix

Examples

extern crate peroxide;
use peroxide::fuga::*;

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

Negation of &’a Matrix

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 !=.

Modify Matrix

Resize matrix

#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    let a = ml_matrix("1 2 3;4 5 6"); // ml_matrix has shape `Col`
    let b1 = a.resize((3, 2), Row);
    let b2 = a.resize((3, 2), Col);
    assert_eq!(b1, ml_matrix("1 2;3 4;5 6"));
    assert_eq!(b2, ml_matrix("1 4;2 5;3 6"));
}

Add row

#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    let a = ml_matrix("1 2 3;4 5 6");
    let b = c!(7,8,9);
    let c = a.add_row(&b);
    assert_eq!(c, ml_matrix("1 2 3;4 5 6;7 8 9"));
}

Add column

#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    let a = ml_matrix("1 2 3;4 5 6");
    let b = c!(7,8);
    let c = a.add_col(&b);
    assert_eq!(c, ml_matrix("1 2 3 7;4 5 6 8"));
}

Resize matrix (Mutable)

#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    let mut a = ml_matrix("1 2 3;4 5 6"); // ml_matrix has shape `Row`
    a.resize_mut((3, 2), Row);
    assert_eq!(a, ml_matrix("1 2;3 4;5 6"));
    a.resize_mut((3, 2), Col);
    assert_eq!(a, ml_matrix("1 4;2 5;3 6"));
}

Add row (Mutable)

#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    let mut a = ml_matrix("1 2 3;4 5 6");
    let b = c!(7,8,9);
    a.add_row_mut(&b);
    assert_eq!(a, ml_matrix("1 2 3;4 5 6;7 8 9"));
}

Add column (Mutable)

#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    let mut a = ml_matrix("1 2 3;4 5 6");
    let b = c!(7,8);
    a.add_col_mut(&b);
    assert_eq!(a, ml_matrix("1 2 3 7;4 5 6 8"));
}

Simple Frobenius norm

Column Mean

Examples
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

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

Column variance

Examples
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

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

Column Standard Deviation

Examples
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    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
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    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
}

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

Subtraction between Matrix

Examples

extern crate peroxide;
use peroxide::fuga::*;

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

#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    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

Subtraction between Matrix & f64

The resulting type after applying the - operator.

Performs the - operation. Read more

Subtraction between &Matrix & f64

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

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

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.