Peroxide
Rust numeric library with R Syntax.
Latest README version
Corresponds with 0.4.5
.
Usage
Initial Import
extern crate peroxide;
use *;
Vec<f64> Declaration
# R
a =
b = # (=c(1,3,5))
// Peroxide
let a = c!;
let b = seq!; // (=c!(1,3,5))
Matrix Declaration
# R
a =
// Peroxide (All belows are same)
// matrix function
let a = matrix;
let b = matrix;
let c = matrix;
// matrix macro (More convenient)
let c = matrix!;
# R
a =
# [,1] [,2]
# [1,] 1 2
# [2,] 3 4
// Peroxide
let a = matrix!;
println!;
// c[0] c[1]
// r[0] 1 2
// r[1] 3 4
Concatenate
1. Vector + Vector => Vector
# R
a =
b =
c = # c(1,2,3,4,5,6)
// Peroxide
let a = c!;
let b = c!;
let c = c!; // Must use semi-colon btw vectors
2. Matrix + Matrix => Matrix
# R
# cbind
a =
b =
c =
# [,1] [,2] [,3]
#[1,] 1 3 5
#[2,] 2 4 6
# rbind
a =
b =
c =
# [,1] [,2]
#[1,] 1 2
#[2,] 3 4
#[3,] 5 6
// Peroxide
// cbind
let a = matrix!;
let b = matrix;
let c = cbind!;
// c[0] c[1] c[2]
// r[0] 1 3 5
// r[1] 2 4 6
// rbind
let a = matrix!;
let b = matrix;
let c = rbind!;
// c[0] c[1]
// r[0] 1 2
// r[1] 3 4
// r[2] 5 6
Matrix operation
- If you want to do multiple operations on same matrix, then you should use
clone
because Ruststd::ops
consume value.
# R
a =
b =
// Peroxide
let a = matrix!;
let b = matrix!;
println!;
println!;
println!; // Element-wise multiplication
println!; // Matrix multiplication
// Consume -> You can't use a,b anymore.
LU Decomposition
- Peroxide uses complete pivoting LU decomposition. - Very stable.
- Also there are lots of error handling for LU, so, you should use
Option
// Peroxide
let a = matrix;
let pqlu = a.lu.unwrap; // for singular matrix, returns None
let = ;
assert_eq!; // swap 0 & 1 (Row)
assert_eq!; // swap 0 & 1 (Col)
assert_eq!;
assert_eq!;
Determinant
- Determinant is implemented using by LU decomposition (O(n^3))
// Peroxide
let a = matrix;
assert_eq!;
Inverse
- Inverse is also implemented using by LU decomposition
- To handle singularity, output type is
Option<Matrix>
- To obtain inverse, you should use
unwrap
or pattern matching
- To obtain inverse, you should use
// Peroxide
// Non-singular
let a = matrix!;
assert_eq!;
// Singular
let b = matrix!;
assert_eq!;
Extract Column or Row
# R
a =
//Peroxide
let a = matrix!;
println!;
println!;
println!;
println!;
Functional Programming
// Peroxide
let a = matrix!;
println!;
println!;
println!;
// Results
//
// c[0] c[1]
// r[0] 2 3
// r[1] 4 5
//
// c[0] c[1]
// r[0] 0 1
// r[1] 2 3
//
// c[0] c[1]
// r[0] 2 4
// r[1] 6 8
Version Info
To see Release.md