-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice_matrix.rs
355 lines (320 loc) · 13.6 KB
/
slice_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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
use super::matrix::Matrix;
use crate::error::{PsetErr, PsetRes};
use std::{
fmt::{self, Debug, Display},
ops::{Add, AddAssign, Mul, Range, Sub},
};
/// A flexible window over data held in some other Matrix.
/// Rows and columns index into the parent matrix.
/// Use Display formatting for a more attractive printing
/// of the sliced matrix.
#[derive(Debug, Clone)]
pub struct SliceMatrix<'a, T> {
parent: &'a Matrix<T>,
rows: Range<usize>,
cols: Range<usize>,
}
impl<'a, T> SliceMatrix<'a, T> {
/// Returns the number of rows in this matrix WITH PADDING
pub fn num_rows(&self) -> usize {
debug_assert!(
self.rows.end <= self.parent.num_rows(),
"Slice matrix row range should always be in bounds"
);
self.rows.end - self.rows.start
}
/// Returns the number of columns in this matrix
pub fn num_cols(&self) -> usize {
debug_assert!(
self.cols.end <= self.parent.num_cols(),
"Slice matrix col range should always be in bounds"
);
self.cols.end - self.cols.start
}
}
impl<'a, T> SliceMatrix<'a, T>
where
T: AddAssign + Default + Copy + Debug + 'static,
for<'b> &'b T: Add<Output = T> + Sub<Output = T> + Mul<Output = T>,
{
/// Retrieves an entry from a row, col in the slice.
/// COORDS ARE RELATIVE TO THE SLICE.
/// Ex, the top left coord in the slice matrix is 0, 0 regardless
/// of where it falls on the parent matrix.
pub fn get(&self, req_row: usize, req_col: usize) -> Option<&T> {
let parent_row = self.rows.start + req_row;
let parent_col = self.cols.start + req_col;
if !self.rows.contains(&parent_row) || !self.cols.contains(&parent_col) {
return None;
}
self.parent
.inner
.get(parent_row)
.and_then(|r| r.get(parent_col))
}
/// Indexes into the requested row and column of the slice. Indices
/// are relative to the slice, not the parent matrix.
/// Panics if the indices are out of bounds of the slice dimensions.
pub fn index(&self, req_row: usize, req_col: usize) -> &T {
self.get(req_row, req_col)
.expect("Accessed out of bounds slice index")
}
/// Returns a matrix representing the operation left + right
pub fn add<'b>(left: &SliceMatrix<'b, T>, right: &SliceMatrix<'b, T>) -> PsetRes<Matrix<T>> {
Self::op_one_to_one(left, right, |l, r| l + r)
}
/// Returns a matrix representing the operation left - right
pub fn sub<'b>(left: &SliceMatrix<'b, T>, right: &SliceMatrix<'b, T>) -> PsetRes<Matrix<T>> {
Self::op_one_to_one(left, right, |l, r| l - r)
}
/// Performs O(n^3) iterative multiplication on the given matrices.
/// If the input matrices are padded, the output matrix is computed
/// without padding.
pub fn mul_iter<'b>(
left: &SliceMatrix<'b, T>,
right: &SliceMatrix<'b, T>,
) -> PsetRes<Matrix<T>> {
if left.num_cols() != right.num_rows() {
return Err(PsetErr::Static("Matrix dims don't support right multiply"));
}
let mut res: Vec<Vec<T>> = Vec::with_capacity(left.num_rows());
for left_row_off in 0..left.num_rows() {
let mut row: Vec<T> = Vec::with_capacity(right.num_rows());
for right_col_off in 0..right.num_cols() {
let mut sum = T::default();
for offset in 0..left.num_cols() {
// indexing is safe because the initial check guarantees dimensions, and it's
// invariant that slices do no exceed the bounds of the parent matrix.
sum += left.index(left_row_off, offset) * right.index(offset, right_col_off);
}
row.push(sum);
}
res.push(row);
}
Ok(Matrix { inner: res })
}
/// Performs O(n^3) recursive multiplication. Used as a stepping stone before
/// Strassen's algorithm.
pub fn mul_naive_rec<'b>(
left: &SliceMatrix<'b, T>,
right: &SliceMatrix<'b, T>,
base_sz: usize,
) -> PsetRes<Matrix<T>> {
if left.num_cols() <= base_sz || left.num_rows() <= base_sz {
return Self::mul_iter(left, right);
}
let left_quad = left.try_split_quad()?;
let right_quad = right.try_split_quad()?;
let mut tl1 = Self::mul_naive_rec(&left_quad.top_left, &right_quad.top_left, base_sz)?;
let tl2 = Self::mul_naive_rec(&left_quad.top_right, &right_quad.bottom_left, base_sz)?;
let mut bl1 = Self::mul_naive_rec(&left_quad.bottom_left, &right_quad.top_left, base_sz)?;
let bl2 = Self::mul_naive_rec(&left_quad.bottom_right, &right_quad.bottom_left, base_sz)?;
let mut tr1 = Self::mul_naive_rec(&left_quad.top_left, &right_quad.top_right, base_sz)?;
let tr2 = Self::mul_naive_rec(&left_quad.top_right, &right_quad.bottom_right, base_sz)?;
let mut br1 = Self::mul_naive_rec(&left_quad.bottom_left, &right_quad.top_right, base_sz)?;
let br2 = Self::mul_naive_rec(&left_quad.bottom_right, &right_quad.bottom_right, base_sz)?;
tl1.add_in_place(&tl2)?;
bl1.add_in_place(&bl2)?;
tr1.add_in_place(&tr2)?;
br1.add_in_place(&br2)?;
tl1.merge_neighbors(bl1, tr1, br1)?;
Ok(tl1)
}
/// Performs Strassen's algorithm on the given SliceMatrix instances, switching to iterative
/// multiplication when sub-matrices are smaller than or equal to base_sz.
/// Fails if left and right are not a power of 2. Apply padding before multiplication if
/// needed.
pub fn mul_strassen<'b>(
left: &SliceMatrix<'b, T>,
right: &SliceMatrix<'b, T>,
base_sz: usize,
) -> PsetRes<Matrix<T>> {
if left.num_cols() <= base_sz || left.num_rows() <= base_sz {
return Self::mul_iter(left, right);
}
let left_quad = left.try_split_quad()?;
let right_quad = right.try_split_quad()?;
// Using this algorithm:
// https://en.wikipedia.org/wiki/Strassen_algorithm
let mut m1 = {
let part_a = SliceMatrix::add(&left_quad.top_left, &left_quad.bottom_right)?;
let part_b = SliceMatrix::add(&right_quad.top_left, &right_quad.bottom_right)?;
Self::mul_strassen(&(&part_a).into(), &(&part_b).into(), base_sz)?
};
let m2 = {
let part_a = SliceMatrix::add(&left_quad.bottom_left, &left_quad.bottom_right)?;
Self::mul_strassen(&(&part_a).into(), &right_quad.top_left, base_sz)?
};
let mut m3 = {
let part_b = SliceMatrix::sub(&right_quad.top_right, &right_quad.bottom_right)?;
Self::mul_strassen(&left_quad.top_left, &(&part_b).into(), base_sz)?
};
let mut m4 = {
let part_b = SliceMatrix::sub(&right_quad.bottom_left, &right_quad.top_left)?;
Self::mul_strassen(&left_quad.bottom_right, &(&part_b).into(), base_sz)?
};
let m5 = {
let part_a = SliceMatrix::add(&left_quad.top_left, &left_quad.top_right)?;
Self::mul_strassen(&(&part_a).into(), &right_quad.bottom_right, base_sz)?
};
let m6 = {
let part_a = SliceMatrix::sub(&left_quad.bottom_left, &left_quad.top_left)?;
let part_b = SliceMatrix::add(&right_quad.top_left, &right_quad.top_right)?;
Self::mul_strassen(&(&part_a).into(), &(&part_b).into(), base_sz)?
};
let m7 = {
let part_a = SliceMatrix::sub(&left_quad.top_right, &left_quad.bottom_right)?;
let part_b = SliceMatrix::add(&right_quad.bottom_left, &right_quad.bottom_right)?;
Self::mul_strassen(&(&part_a).into(), &(&part_b).into(), base_sz)?
};
// done in order that minimizes the need for copying
let final_bottom_left = Self::add(&(&m2).into(), &(&m4).into())?;
let mut final_top_left = {
m4.add_in_place(&m1)?.sub_in_place(&m5)?.add_in_place(&m7)?;
m4
};
let final_bottom_right = {
m1.sub_in_place(&m2)?.add_in_place(&m3)?.add_in_place(&m6)?;
m1
};
let final_top_right = {
m3.add_in_place(&m5)?;
m3
};
final_top_left.merge_neighbors(final_bottom_left, final_top_right, final_bottom_right)?;
Ok(final_top_left)
}
/// If successful, returns the slice matrix partitioned into four equal
/// quadrants.
/// Fails unless the given matrix is an even-dimension square. The method
/// `pad_even_square` can be helpful getting there.
fn try_split_quad(&self) -> PsetRes<SplitQuad<T>> {
SplitQuad::build(self)
}
/// Attempts to build a new slice matrix from a range within
/// the existing one.
/// RANGES ARE IN RELATION TO THE PARENT SLICE, NOT THE PARENT MATRIX.
/// Ex. Requesting a range beginning at row 0 yields row 0 of the slice
/// no matter where that index occurs in the parent matrix.
fn slice_range(
&self,
req_rows: &Range<usize>,
req_cols: &Range<usize>,
) -> PsetRes<SliceMatrix<T>> {
let req_row_len = req_rows.end - req_rows.start;
let req_col_len = req_cols.end - req_cols.start;
if self.num_rows() < req_row_len || self.num_cols() < req_col_len {
return Err(PsetErr::Static(
"slice_range: Requested dims exceed parent slice",
));
}
let sl_row_start = self.rows.start + req_rows.start;
let sl_col_start = self.cols.start + req_cols.start;
if self.rows.end <= sl_row_start || self.cols.end <= sl_col_start {
return Err(PsetErr::Static(
"slice_range: Requested start exceeds parent end",
));
}
let sl_rows = sl_row_start..(sl_row_start + req_row_len);
let sl_cols = sl_col_start..(sl_col_start + req_col_len);
debug_assert!(sl_rows.end <= self.rows.end);
debug_assert!(sl_cols.end <= self.cols.end);
Ok(SliceMatrix {
parent: self.parent,
rows: sl_rows,
cols: sl_cols,
})
}
/// Performs an operation on parallel entries in the given matrices, writing the output
/// to a new matrix. Useful for addition and subtraction.
/// Materializes imaginary padding into the result as T::default() values.
fn op_one_to_one<'b>(
left: &SliceMatrix<'b, T>,
right: &SliceMatrix<'b, T>,
op: fn(&T, &T) -> T,
) -> PsetRes<Matrix<T>> {
if left.num_rows() != right.num_rows() || left.num_cols() != right.num_cols() {
return Err(PsetErr::Static(
"Cannot perform a one to one operation on matrices of different dimensions",
));
}
let mut res = Vec::with_capacity(left.num_rows());
for row_offset in 0..left.num_rows() {
let mut row = Vec::with_capacity(left.num_cols());
for col_offset in 0..left.num_cols() {
// Indexing is okay because the initial check
// guarantees that bounds are safe, and slice indices are
// always in-bounds of the parent matrix.
row.push(op(
left.index(row_offset, col_offset),
right.index(row_offset, col_offset),
));
}
res.push(row);
}
Ok(res.into())
}
}
/// Returns the successful partition of a single SliceMatrix into
/// four sub-slices.
#[derive(Debug)]
struct SplitQuad<'a, T> {
top_left: SliceMatrix<'a, T>,
bottom_left: SliceMatrix<'a, T>,
top_right: SliceMatrix<'a, T>,
bottom_right: SliceMatrix<'a, T>,
}
impl<'a, T> SplitQuad<'a, T>
where
T: AddAssign + Default + Copy + Debug + 'static,
for<'b> &'b T: Add<Output = T> + Sub<Output = T> + Mul<Output = T>,
{
/// Returns that matrix partitioned into four equal quadrants.
/// Fails if the given parent slice is not at least 2x2, square, and even.
fn build(mtx: &'a SliceMatrix<'a, T>) -> PsetRes<Self> {
if mtx.num_rows() < 2 || mtx.num_cols() < 2 {
return Err(PsetErr::Static(
"Matrix must be at least 2x2 to split as quad.",
));
}
if mtx.num_rows() != mtx.num_cols() || mtx.num_rows() % 2 != 0 {
return Err(PsetErr::Static("SplitQuad requires an even square matrix."));
}
let quad_rows = mtx.num_rows() / 2;
let quad_cols = mtx.num_cols() / 2;
let (top_rows, bottom_rows) = (0..quad_rows, quad_rows..(quad_rows * 2));
let (left_cols, right_cols) = (0..quad_cols, quad_cols..(quad_cols * 2));
let top_left = mtx.slice_range(&top_rows, &left_cols)?;
let bottom_left = mtx.slice_range(&bottom_rows, &left_cols)?;
let top_right = mtx.slice_range(&top_rows, &right_cols)?;
let bottom_right = mtx.slice_range(&bottom_rows, &right_cols)?;
Ok(SplitQuad {
top_left,
bottom_left,
top_right,
bottom_right,
})
}
}
impl<'a, T> From<&'a Matrix<T>> for SliceMatrix<'a, T> {
fn from(parent: &'a Matrix<T>) -> Self {
SliceMatrix {
parent,
rows: 0..parent.num_rows(),
cols: 0..parent.num_cols(),
}
}
}
impl<T> Display for SliceMatrix<'_, T>
where
T: AddAssign + Default + Copy + Debug + 'static,
for<'b> &'b T: Add<Output = T> + Sub<Output = T> + Mul<Output = T>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "rows: {:?}, cols: {:?}", self.rows, self.cols)?;
for row_ind in self.rows.clone() {
writeln!(f, "{:?}", &self.parent.inner[row_ind][self.cols.clone()])?;
}
Ok(())
}
}