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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//! Implementation of the two sided interpolative decomposition
//!
//! The two sided interpolative decomposition of a matrix $A\in\mathbb{C}&{m\times n}$ is
//! defined as
//! $$
//! A \approx CXR,
//! $$
//! where $C\in\mathbb{C}^{m\times k}$, $X\in\mathbb{C}^{k\times k}$, and $R\in\mathbb{C}^{k\times n}$.
//! The matrix $X$ contains a subset of the entries of $A$, such that A\[row_ind\[:\], col_ind\[:\]\] = X, where
//! row_ind and col_ind are index vectors.
use crateApply;
use ;
use crate;
/// Store a two sided interpolative decomposition
/// Traits defining a two sided interpolative decomposition
///
/// defined as
/// The two sided interpolative decomposition of a matrix $A\in\mathbb{C}&{m\times n} is
/// $$
/// A \approx CXR,
/// $$
/// where $C\in\mathbb{C}^{m\times k}$, $X\in\mathbb{C}^{k\times k}$, and $R\in\mathbb{C}^{k\times n}$.
/// The matrix $X$ contains a subset of the entries of $A$, such that A\[row_ind\[:\], col_ind\[:\]\] = X, where
/// row_ind and col_ind are index vectors.
impl_two_sided_id!;
impl_two_sided_id!;
impl_two_sided_id!;
impl_two_sided_id!;
// impl<A: ScalarType> TwoSidedIDResult<A> {
// pub fn nrows(&self) -> usize {
// self.c.nrows()
// }
// pub fn ncols(&self) -> usize {
// self.r.ncols()
// }
// pub fn rank(&self) -> usize {
// self.x.nrows()
// }
// pub fn to_mat(&self) -> Array2<A> {
// self.c.dot(&self.x.dot(&self.r))
// }
// pub fn apply_matrix<S: Data<Elem = A>>(
// &self,
// other: &ArrayBase<S, Ix2>,
// ) -> ArrayBase<OwnedRepr<A>, Ix2> {
// self.c.dot(&self.x.dot(&self.r.dot(other)))
// }
// pub fn apply_vector<S: Data<Elem = A>>(
// &self,
// other: &ArrayBase<S, Ix1>,
// ) -> ArrayBase<OwnedRepr<A>, Ix1> {
// self.c.dot(&self.x.dot(&self.r.dot(other)))
// }
// //}
// }
// impl<A: ScalarType> QRContainer<A> {
// pub fn two_sided_id(&self) -> Result<TwoSidedIDResult<A>> {
// let col_id = self.column_id()?;
// let row_id = col_id.c.pivoted_lq()?.row_id()?;
// Ok(TwoSidedIDResult {
// c: row_id.x,
// x: row_id.r,
// r: col_id.z,
// row_ind: row_id.row_ind,
// col_ind: col_id.col_ind,
// })
// }
// }
// #[cfg(test)]
// mod tests {
// use crate::prelude::ApplyPermutationToMatrix;
// use crate::prelude::CompressionType;
// use crate::prelude::MatrixPermutationMode;
// use crate::prelude::PivotedQR;
// use crate::prelude::RandomMatrix;
// use crate::prelude::RelDiff;
// use ndarray_linalg::Scalar;
// macro_rules! id_compression_tests {
// ($($name:ident: $scalar:ty, $dim:expr, $tol:expr,)*) => {
// $(
// #[test]
// fn $name() {
// let m = $dim.0;
// let n = $dim.1;
// let sigma_max = 1.0;
// let sigma_min = 1E-10;
// let mut rng = rand::thread_rng();
// let mat = <$scalar>::random_approximate_low_rank_matrix((m, n), sigma_max, sigma_min, &mut rng);
// let qr = mat.pivoted_qr().unwrap().compress(CompressionType::ADAPTIVE($tol)).unwrap();
// let rank = qr.rank();
// let two_sided_id = qr.two_sided_id().unwrap();
// // Compare with original matrix
// assert!(two_sided_id.to_mat().rel_diff(&mat) < 5.0 * $tol);
// // Now compare the individual columns to make sure that the id basis columns
// // agree with the corresponding matrix columns.
// let mat_permuted = mat.apply_permutation(two_sided_id.row_ind.view(), MatrixPermutationMode::ROW).
// apply_permutation(two_sided_id.col_ind.view(), MatrixPermutationMode::COL);
// // Assert that the x matrix in the two sided id is squared with correct dimension.
// assert!(two_sided_id.x.nrows() == two_sided_id.x.ncols());
// assert!(two_sided_id.x.nrows() == rank);
// // Now compare with the original matrix.
// for row_index in 0..rank {
// for col_index in 0..rank {
// let tmp = (two_sided_id.x[[row_index, col_index]] - mat_permuted[[row_index, col_index]]).abs() / mat_permuted[[row_index, col_index]].abs();
// println!("Rel Error {}", tmp);
// //if tmp >= 5.0 * $tol {
// //println!(" Rel Error {}", tmp);
// //}
// assert!((two_sided_id.x[[row_index, col_index]] - mat_permuted[[row_index, col_index]]).abs()
// < 10.0 * $tol * mat_permuted[[row_index, col_index]].abs())
// }
// }
// }
// )*
// }
// }
// id_compression_tests! {
// test_id_compression_by_tol_f32_thin: f32, (100, 50), 1E-4,
// test_id_compression_by_tol_c32_thin: ndarray_linalg::c32, (100, 50), 1E-4,
// test_id_compression_by_tol_f64_thin: f64, (100, 50), 1E-4,
// test_id_compression_by_tol_c64_thin: ndarray_linalg::c64, (100, 50), 1E-4,
// test_id_compression_by_tol_f32_thick: f32, (50, 100), 1E-4,
// test_id_compression_by_tol_c32_thick: ndarray_linalg::c32, (50, 100), 1E-4,
// test_id_compression_by_tol_f64_thick: f64, (50, 100), 1E-4,
// test_id_compression_by_tol_c64_thick: ndarray_linalg::c64, (50, 100), 1E-4,
// }
// }