-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathmatrix.rs
More file actions
164 lines (144 loc) · 3.06 KB
/
matrix.rs
File metadata and controls
164 lines (144 loc) · 3.06 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#[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"));
}
#[test]
fn test_rref() {
let a = ml_matrix(
r#"
-3 2 -1 -1;
6 -6 7 -7;
3 -4 4 -6"#,
);
let b = a.rref();
assert_eq!(
b,
ml_matrix(
r#"
1 0 0 2;
0 1 0 2;
0 0 1 -1"#
)
);
}
#[test]
fn test_rref_unstable() {
let epsilon = 1e-10;
// this matrix used to become unstable during rref
let a = ml_matrix(
r#"
1 1 0 0 0 1 0 1 31;
1 1 1 1 0 0 1 1 185;
0 0 1 0 0 1 1 1 165;
1 0 1 0 1 1 0 1 32;
1 0 1 0 0 0 1 1 174;
0 0 1 0 1 1 1 1 171;
0 1 1 0 1 1 0 1 27;
1 0 0 1 0 1 0 0 20;
1 0 1 1 0 1 0 0 23"#,
);
let b = a.rref();
// creating a row like "0 0 0 0 0 0 0 0 1" will "prove" 0 == 1
// which is a tell of numeric instability
for row in 0..b.row {
let ends_in_1 = (b[(row, b.col - 1)] - 1.0).abs() < epsilon;
let rest_zeroes = (0..b.col - 1).all(|col| b[(row, col)].abs() < epsilon);
assert!(!(ends_in_1 && rest_zeroes));
}
}