-
Notifications
You must be signed in to change notification settings - Fork 32
/
api.rs
76 lines (69 loc) · 1.76 KB
/
api.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! Choose api - MATLAB, R, Python
use crate::structure::matrix::*;
pub trait MATLAB {
fn new(s: &str) -> Self;
}
pub trait PYTHON {
fn new<T>(v: Vec<Vec<T>>) -> Self
where
T: Into<f64> + Copy;
}
pub trait R {
fn new<T>(v: Vec<T>, x: usize, y: usize, shape: Shape) -> Self
where
T: Into<f64>;
}
impl MATLAB for Matrix {
fn new(s: &str) -> Self where {
let str_rows_temp: Vec<&str> = s.split(';').collect();
let str_rows = str_rows_temp
.into_iter()
.filter(|&t| t != "")
.collect::<Vec<&str>>();
let r = str_rows.len();
let str_data = str_rows
.into_iter()
.map(|x| x.trim().split(' ').collect::<Vec<&str>>())
.collect::<Vec<Vec<&str>>>();
let c = str_data[0].len();
let data = str_data
.into_iter()
.flat_map(|t| {
t.into_iter()
.map(|x| x.parse::<f64>().unwrap())
.collect::<Vec<f64>>()
})
.collect::<Vec<f64>>();
matrix(data, r, c, Row)
}
}
impl PYTHON for Matrix {
fn new<T>(v: Vec<Vec<T>>) -> Self
where
T: Into<f64> + Copy,
{
let r = v.len();
let c = v[0].len();
let mut data = vec![0f64; r * c];
for i in 0..r {
for j in 0..c {
let idx = i * c + j;
data[idx] = v[i][j].into();
}
}
matrix(data, r, c, Row)
}
}
impl R for Matrix {
fn new<T>(v: Vec<T>, x: usize, y: usize, shape: Shape) -> Self
where
T: Into<f64>,
{
matrix(
v.into_iter().map(|t| t.into()).collect::<Vec<f64>>(),
x,
y,
shape,
)
}
}