-
Notifications
You must be signed in to change notification settings - Fork 32
/
matrix.rs
113 lines (100 loc) · 2.06 KB
/
matrix.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;
#[test]
fn test_seq() {
let v1 = c!(2, 4, 6, 8);
let v2 = seq(2, 8, 2);
assert_eq!(v1, v2);
}
#[test]
fn test_c() {
let v1: Vec<f64> = vec![1f64, 2f64, 3f64, 4f64];
let v2 = c!(1, 2, 3, 4);
assert_eq!(v1, v2);
}
#[test]
fn test_zeros() {
let v = zeros!(5);
assert_eq!(v, vec![0f64; 5]);
}
#[test]
fn test_accumulation() {
let v1 = c!(1, 2, 3, 4);
let v2 = seq(5, 8, 1);
assert_eq!(seq(1, 8, 1), c!(v1; v2));
}
#[test]
fn test_add_matrix() {
let a = matrix!(1;100;1, 10, 10, Row);
assert_eq!(&a + &a, 2f64 * a);
}
#[test]
fn test_add_f64() {
let a = matrix(c!(1, 2, 3, 4), 2, 2, Row);
let b = matrix(c!(2, 3, 4, 5), 2, 2, Row);
assert_eq!(a + 1.0, b);
}
#[test]
fn test_col() {
let a = matrix(seq(1, 4, 1), 2, 2, Row);
assert_eq!(a.col(0), c!(1, 3));
}
#[test]
fn test_row() {
let a = matrix!(1;4;1, 2, 2, Row);
assert_eq!(a.row(0), c!(1, 2));
}
#[test]
fn test_print() {
let op = Bernoulli(0);
op.print();
}
#[test]
fn test_row_map() {
let m = ml_matrix("1 2;3 4");
let n = m.row_map(|v| v.normalize(Norm::L2));
let o = matrix(
vec![
1f64 / 5f64.sqrt(),
2f64 / 5f64.sqrt(),
3f64 / 5f64,
4f64 / 5f64,
],
2,
2,
Row,
);
assert_eq!(n, o);
}
#[test]
fn test_col_map() {
let m = ml_matrix("1 3;2 4");
let n = m.col_map(|v| v.normalize(Norm::L2));
let o = matrix(
vec![
1f64 / 5f64.sqrt(),
2f64 / 5f64.sqrt(),
3f64 / 5f64,
4f64 / 5f64,
],
2,
2,
Col,
);
assert_eq!(n, o);
}
#[test]
fn test_outer() {
let a = c!(1, 2, 3);
let b = c!(4, 5, 6);
let c = a.outer(&b);
assert_eq!(c, ml_matrix("4 5 6;8 10 12;12 15 18"));
}
#[test]
fn test_kronecker() {
let a1 = ml_matrix("1 2;3 4");
let b1 = ml_matrix("0 5;6 7");
let c1 = a1.kronecker(&b1);
assert_eq!(c1, ml_matrix("0 5 0 10;6 7 12 14;0 15 0 20;18 21 24 28"));
}