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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use ndarray_rand::RandomExt;
use rand::distributions::Uniform;
use rand::prelude::*;
pub fn eigenvalues_2x2(mat: &ndarray::ArrayView2<f64>) -> (f64, f64) {
let a = mat[(0, 0)];
let b = mat[(0, 1)];
let c = mat[(1, 0)];
let d = mat[(1, 1)];
let tmp = ((-(a + d) / 2.0).powi(2) - a * d + b * c).sqrt();
let l1 = (a + d) / 2.0 + tmp;
let l2 = (a + d) / 2.0 - tmp;
if l1 > l2 {
(l1, l2)
} else {
(l2, l1)
}
}
pub fn swap_columns<T>(mat: &mut ndarray::Array2<T>, idx1: usize, idx2: usize)
where
ndarray::OwnedRepr<T>: ndarray::Data,
{
let s = mat.raw_dim();
for i in 0..s[0] {
mat.swap((i, idx1), (i, idx2));
}
}
pub fn swap_rows<T>(mat: &mut ndarray::Array2<T>, idx1: usize, idx2: usize)
where
ndarray::OwnedRepr<T>: ndarray::Data,
{
let s = mat.raw_dim();
for i in 0..s[1] {
mat.swap((idx1, i), (idx2, i));
}
}
pub fn index_of_largest<'a, T>(c: &ndarray::ArrayView1<T>) -> usize
where
<ndarray::ViewRepr<&'a T> as ndarray::RawData>::Elem:
std::cmp::PartialOrd + num::traits::Signed + Clone,
{
let mut max = c[0].clone();
let mut max_idx = 0;
c.iter()
.enumerate()
.map(|(i, ci)| {
let ci = ci.clone();
if ci > max {
max = ci;
max_idx = i
}
})
.count();
max_idx
}
pub fn index_of_largest_abs<'a, T>(c: &ndarray::ArrayView1<T>) -> usize
where
<ndarray::ViewRepr<&'a T> as ndarray::RawData>::Elem:
std::cmp::PartialOrd + num::traits::Signed + Clone,
{
let mut max = num::abs(c[0].clone());
let mut max_idx = 0;
c.iter()
.enumerate()
.map(|(i, ci)| {
let ci = num::abs(ci.clone());
if ci > max {
max = ci;
max_idx = i
}
})
.count();
max_idx
}
pub fn index_to_permutation_mat(idxs: &[usize]) -> ndarray::Array2<f64> {
let n = idxs.len();
let mut mat = ndarray::Array2::zeros((n, n));
for i in 0..n {
mat[(i, idxs[i])] = 1.0;
}
mat
}
pub fn diag_mat_from_arr<T>(arr: &[T]) -> ndarray::Array2<T>
where
T: Clone + num::traits::identities::Zero,
{
let n = arr.len();
let mut mat: ndarray::Array2<T> = ndarray::Array2::zeros((n, n));
let diag: ndarray::Array1<T> = ndarray::Array::from_iter(arr.iter().cloned());
mat.diag_mut().assign(&diag);
mat
}
pub fn random_householder(dim: usize, seed: u8) -> ndarray::Array2<f64> {
let mut rng = rand_xorshift::XorShiftRng::from_seed([seed; 16]);
let w = ndarray::Array::random_using((dim, 1), Uniform::new_inclusive(-1.0, 1.0), &mut rng);
let denom = w.fold(0.0, |acc, &x: &f64| acc + x.powi(2));
ndarray::Array::eye(dim) - 2.0 / denom * w.dot(&w.t())
}
pub fn random_diagonal(
dim: usize,
(eig_range_min, eig_range_max): (f64, f64),
min_neg_eigenvalues: usize,
seed: u8,
) -> ndarray::Array2<f64> {
let mut rng = rand_xorshift::XorShiftRng::from_seed([seed; 16]);
let mut w = ndarray::Array::random_using(
dim,
Uniform::new_inclusive(eig_range_min, eig_range_max),
&mut rng,
);
let mut idxs: Vec<usize> = (0..dim).collect();
for _ in 0..min_neg_eigenvalues {
let idxidx: usize = (rng.gen::<f64>() * (idxs.len() - 1) as f64).floor() as usize;
let idx = idxs.remove(idxidx);
w[idx] = rng.gen::<f64>() - 1.0;
}
let mut out = ndarray::Array::eye(dim);
out.diag_mut().assign(&w);
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_swap_columns() {
let mut a: ndarray::Array2<i64> = ndarray::arr2(&[[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
super::swap_columns(&mut a, 1, 2);
let c: ndarray::Array2<i64> = ndarray::arr2(&[[1, 3, 2], [4, 6, 5], [7, 9, 8]]);
a.iter()
.zip(c.iter())
.map(|(x, y)| assert_eq!(*x, *y))
.count();
}
#[test]
fn test_swap_rows() {
let mut a: ndarray::Array2<i64> = ndarray::arr2(&[[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
super::swap_rows(&mut a, 1, 2);
let c: ndarray::Array2<i64> = ndarray::arr2(&[[1, 2, 3], [7, 8, 9], [4, 5, 6]]);
a.iter()
.zip(c.iter())
.map(|(x, y)| assert_eq!(*x, *y))
.count();
}
#[test]
fn test_swap_rows_and_columns() {
let mut a: ndarray::Array2<i64> = ndarray::arr2(&[[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
super::swap_rows(&mut a, 1, 2);
super::swap_columns(&mut a, 1, 2);
let c: ndarray::Array2<i64> = ndarray::arr2(&[[1, 3, 2], [7, 9, 8], [4, 6, 5]]);
a.iter()
.zip(c.iter())
.map(|(x, y)| assert_eq!(*x, *y))
.count();
}
#[test]
fn test_biggest_index() {
use ndarray::s;
let j = 1;
let a: ndarray::Array2<i64> =
ndarray::arr2(&[[1, 2, 3, 0], [4, 2, 6, 0], [7, 8, 3, 0], [3, 4, 2, 8]]);
let idx = super::index_of_largest(&a.diag().slice(s![j..]));
assert_eq!(idx + j, 3);
}
#[test]
fn test_biggest_index_abs() {
use ndarray::s;
let j = 1;
let a: ndarray::Array2<f64> = ndarray::arr2(&[
[1.0, 2.0, 3.0, 0.0],
[4.0, 2.0, 6.0, 0.0],
[7.0, 8.0, 3.0, 0.0],
[3.0, 4.0, 2.0, -8.0],
]);
let idx = super::index_of_largest_abs(&a.diag().slice(s![j..]));
assert_eq!(idx + j, 3);
}
#[test]
fn test_rand_householder() {
let q = random_householder(2, 84);
let res: ndarray::Array2<f64> = ndarray::arr2(&[
[-0.0000034067079459632055, -0.9999999999941971],
[-0.9999999999941971, 0.0000034067079460742278],
]);
assert!(q.abs_diff_eq(&res, 1e-19));
}
#[test]
fn test_random_diagonal_all_pos() {
let d = random_diagonal(2, (-1.0, 1.0), 0, 128);
let res: ndarray::Array2<f64> =
ndarray::arr2(&[[0.003923416145884984, 0.0], [0.0, 0.0039215684018965025]]);
assert!(d.abs_diff_eq(&res, 1e-19));
}
#[test]
fn test_random_diagonal_one_neg() {
let d = random_diagonal(2, (-1.0, 1.0), 1, 128);
let res: ndarray::Array2<f64> =
ndarray::arr2(&[[-0.49803921207376156, 0.0], [0.0, 0.0039215684018965025]]);
assert!(d.abs_diff_eq(&res, 1e-19));
}
}