Rust numeric library with R Syntax.
Corresponds with 0.6.3
.
cargo.toml
toml
peroxide = "0.6"
rust
extern crate peroxide;
use peroxide::*;
mod
and re-export
rand
crateVec<f64>
```bash
├── macros │ ├── matlabmacro.rs │ ├── mod.rs │ └── rmacro.rs ├── ml │ ├── mod.rs │ └── reg.rs ├── numerical │ ├── interp.rs │ ├── mod.rs │ └── spline.rs ├── operation │ ├── extraops.rs │ └── mod.rs ├── statistics │ ├── mod.rs │ ├── rand.rs │ └── stat.rs ├── structure │ ├── dual.rs │ ├── matrix.rs │ ├── mod.rs │ ├── polynomial.rs │ └── vector.rs └── util ├── mod.rs ├── nonmacro.rs └── print.rs ```
```R
a = c(1,2,3,4) b = seq(1,5,2) # (=c(1,3,5)) ```
```rust // Peroxide extern crate peroxide; use peroxide::*;
fn main() { let a = c!(1,2,3,4); let b = seq!(1,5,2); // (=c!(1,3,5)) } ```
```R
a = matrix(1:4, 2, 2, T) ```
```rust // Peroxide (All belows are same) extern crate peroxide; use peroxide::*;
fn main() { // matrix function let a = matrix(vec![1,2,3,4], 2, 2, Row); let b = matrix(c!(1,2,3,4), 2, 2, Row); let c = matrix(seq!(1,4,1), 2, 2, Row);
// matrix macro (More convenient)
let d = matrix!(1;4;1, 2, 2, Row);
} ```
```R
a = matrix(1:4, 2, 2, T) print(a)
```
```rust // Peroxide extern crate peroxide; use peroxide::*;
fn main() { let a = matrix!(1;4;1, 2, 2, Row); println!("{}", a); // Or a.print(); } // c[0] c[1] // r[0] 1 2 // r[1] 3 4 ```
1. Vector + Vector => Vector ```R
a = c(1,2,3) b = c(4,5,6)
c = c(a, b) print(c) # c(1,2,3,4,5,6) ```
```rust // Peroxide extern crate peroxide; use peroxide::*;
fn main() { let a = c!(1,2,3); let b = c!(4,5,6); let c = c!(a; b); // Must use semi-colon btw vectors c.print(); } ```
2. Matrix + Matrix => Matrix ```R
a = matrix(1:4, 2, 2, F) b = matrix(c(5,6), 2, 1, F) c = cbind(a, b) print(c)
a = matrix(1:4, 2, 2, T) b = matrix(c(5,6), 1, 2, T) c = rbind(a,b) print(c)
```
```rust // Peroxide extern crate peroxide; use peroxide::*;
fn main() { // cbind let a = matrix!(1;4;1, 2, 2, Col); let b = matrix(c!(5,6), 2, 1, Col); let c = cbind!(a, b); c.print(); // c[0] c[1] c[2] // r[0] 1 3 5 // r[1] 2 4 6
// rbind
let d = matrix!(1;4;1, 2, 2, Row);
let e = matrix(c!(5,6),1, 2, Row);
let f = rbind!(a, b);
f.print();
// c[0] c[1]
// r[0] 1 2
// r[1] 3 4
// r[2] 5 6
} ```
clone
because Rust std::ops
consume value. ```R
a = matrix(1:4, 2, 2, T) b = matrix(1:4, 2, 2, F) print(a + b) print(a - b) print(a * b) print(a %*% b) ```
```rust // Peroxide extern crate peroxide; use peroxide::*;
fn main() { let a = matrix!(1;4;1, 2, 2, Row); let b = matrix!(1;4;1, 2, 2, Col); println!("{}", a.clone() + b.clone()); println!("{}", a.clone() - b.clone()); println!("{}", a.clone() * b.clone()); // Element-wise multiplication println!("{}", a % b); // Matrix multiplication // Consume -> You can't use a,b anymore. } ```
Option
```rust // Peroxide extern crate peroxide; use peroxide::*;
fn main() { let a = matrix(c!(1,2,3,4), 2, 2, Row); let pqlu = a.lu().unwrap(); // for singular matrix, returns None let (p,q,l,u) = (pqlu.p, pqlu.q, pqlu.l, pqlu.u); asserteq!(p, vec![(0,1)]); // swap 0 & 1 (Row) asserteq!(q, vec![(0,1)]); // swap 0 & 1 (Col) asserteq!(l, matrix(c!(1,0,0.5,1),2,2,Row)); asserteq!(u, matrix(c!(4,3,0,-0.5),2,2,Row)); } ```
```rust // Peroxide extern crate peroxide; use peroxide::*;
fn main() { let a = matrix(c!(1,2,3,4), 2, 2, Row); assert_eq!(a.det(), -2f64); } ```
Option<Matrix>
unwrap
or pattern matching```rust // Peroxide extern crate peroxide; use peroxide::*;
fn main() { // 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);
} ```
```R
a = matrix(1:4, 2, 2, T) print(a[,1]) print(a[,2]) print(a[1,]) print(a[2,]) ```
```rust //Peroxide extern crate peroxide; use peroxide::*;
fn main() { let a = matrix!(1;4;1, 2, 2, Row); a.col(0).print(); a.col(1).print(); a.row(0).print(); a.row(1).print(); } ```
```rust // Peroxide extern crate peroxide; use peroxide::*;
fn main() { let a = matrix!(1;4;1, 2, 2, Row); println!("{}", a.fmap(|x| x + 1.0)); println!("{}", a.fmap(|x| x - 1.0)); println!("{}", a.fmap(|x| x * 2.0)); }
// 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 ```
You can write matrix to csv by two ways.
```rust // Peroxide extern crate peroxide; use peroxide::*; use std::process; // for error handling
fn main() { // 1. Just write let a = matrix!(1;4;1, 2, 2, Row); a.write("test.csv"); // It will save a to test.csv
// 2. Error Handling
let b = matrix!(1;4;1, 2, 2, Row);
if let Err(err) = b.write("test.csv") {
println!("{}", err);
process::exit(1);
}
} ```
You can read matrix with error handling
```rust // Peroxide extern crate peroxide; use peroxide::*; use std::process;
fn main() { let m = read("test.csv", false); // no header // Error handling match m { Ok(mat) => println!("{}", mat), Err(err) => { println!("{}", err); process::exit(1); } }
// Just write
let n = read("test.csv", false).unwrap(); // no header
println!("{}", n);
} ```
mean
- Meanvar
- Variancesd
- Standard Deviationcov
- Covariancecor
- Pearson's Coefficient```r
a <- c(1,2,3) b <- c(3,2,1) print(mean(a)) print(var(a)) print(sd(a)) print(cov(a, b)) print(cor(a, b))
m <- matrix(c(1,2,3,3,2,1), 3, 2, F) print(cov(m)) ```
```rust // Peroxide extern crate peroxide; use peroxide::*;
fn main() { // Vector Stats let a = c!(1,2,3); let b = c!(3,2,1);
println!("{}",a.mean());
println!("{}",a.var());
println!("{}",a.sd());
println!("{}",cov(&a, &b)); // Should borrow! - Not consume value
println!("{}",cor(&a, &b));
// Matrix Stats
let m = matrix(c!(1,2,3,3,2,1), 3, 2, Col);
println!("{}",m.cov());
} ```
lm(x, y)
```r
a <- c(1,2,3,4,5) b <- a + rnorm(5) lm(b ~ a)
#
```
```rust //Peroxide extern crate peroxide; use peroxide::*;
fn main() { let a = c!(1,2,3,4,5).tomatrix(); let b = a.clone() + Normal::new(0,1).sample(5).tomatrix(); lm!(b ~ a).print();
// c[0]
// r[0] 0.7219
// r[1] 0.8058
}
```
Current available distribution
rand
crate)```rust // Peroxide extern crate peroxide; use peroxide::*;
fn main() { // Uniform unsigned integers let vu32 = Uniform::new(1u32, 11); vu32.sample(10).print();
// Uniform 64bit float numbers
let v_f64 = Uniform::new(1f64, 11f64);
v_f64.sample(10).print();
// Normal distribution with mean 0, sd 1
let v_n = Normal::new(0, 1);
println!("{}", v_n.sample(1000).mean()); // almost 0
println!("{}", v_n.sample(1000).sd()); // almost 1
} ```
```rust // Peroxide extern crate peroxide; use peroxide::*;
fn main() { // Declare polynomial let a = poly(c!(1,3,2)); a.print(); // x^2 + 3x + 2 a.eval(1); // Evaluate when x = 1 -> 6.0
let b = poly(c!(1,2,3,4)); // x^3 + 2x^2 + 3x + 4
(a.clone() + b.clone()).print(); // x^3 + 3x^2 + 6x + 6
(a.clone() - b.clone()).print(); // -x^3 - x^2 - 2
(a.clone() * b.clone()).print(); // x^5 + 5x^4 + 11x^3 + 17x^2 + 18x + 8
let c = poly(c!(1, -1));
c.print(); // x - 1
c.pow(2).print(); // x^2 - 2x + 1
} ```
```rust // Peroxide extern crate peroxide; use peroxide::*;
fn main() { let a = c!(-1, 0, 1); let b = c!(1, 0, 1);
let l = lagrange_polynomial(a, b);
l.print(); // x^2
} ```
```rust // Peroxide extern crate peroxide; use peroxide::*;
fn main() { let x = c!(0.9, 1.3, 1.9, 2.1); let y = c!(1.3, 1.5, 1.85, 2.1);
let s = cubic_spline(x, y);
for i in 0 .. s.len() {
s[i].print();
}
// -0.2347x^3 + 0.6338x^2 - 0.0329x + 0.9873
// 0.9096x^3 - 3.8292x^2 + 5.7691x - 1.5268
// -2.2594x^3 + 14.2342x^2 - 28.5513x + 20.2094
}
```
zeros
- zero vector or matrixeye
- identity matrixrand
- random matrix (range from 0 to 1)To see Release.md