| 1 |
#![cfg(feature = "ndarray")] |
| 2 |
|
| 3 |
use ndarray::{s, Array2}; |
| 4 |
use rand::rngs::StdRng; |
| 5 |
use rand::SeedableRng; |
| 6 |
use smawk::{brute_force, online_column_minima, recursive}; |
| 7 |
|
| 8 |
mod random_monge; |
| 9 |
use random_monge::random_monge_matrix; |
| 10 |
|
| 11 |
/// Check that the brute force, recursive, and SMAWK functions |
| 12 |
/// give identical results on a large number of randomly generated |
| 13 |
/// Monge matrices. |
| 14 |
#[test] |
| 15 |
fn column_minima_agree() { |
| 16 |
let sizes = [1, 2, 3, 4, 5, 10, 15, 20, 30]; |
| 17 |
let mut rng = StdRng::seed_from_u64(0); |
| 18 |
for _ in 0..4 { |
| 19 |
for &m in &sizes { |
| 20 |
for &n in &sizes { |
| 21 |
let matrix: Array2<i32> = random_monge_matrix(m, n, &mut rng); |
| 22 |
|
| 23 |
// Compute and test row minima. |
| 24 |
let brute_force = brute_force::row_minima(&matrix); |
| 25 |
let recursive = recursive::row_minima(&matrix); |
| 26 |
let smawk = smawk::row_minima(&matrix); |
| 27 |
assert_eq!( |
| 28 |
brute_force, recursive, |
| 29 |
"recursive and brute force differs on:\n{:?}", |
| 30 |
matrix |
| 31 |
); |
| 32 |
assert_eq!( |
| 33 |
brute_force, smawk, |
| 34 |
"SMAWK and brute force differs on:\n{:?}", |
| 35 |
matrix |
| 36 |
); |
| 37 |
|
| 38 |
// Do the same for the column minima. |
| 39 |
let brute_force = brute_force::column_minima(&matrix); |
| 40 |
let recursive = recursive::column_minima(&matrix); |
| 41 |
let smawk = smawk::column_minima(&matrix); |
| 42 |
assert_eq!( |
| 43 |
brute_force, recursive, |
| 44 |
"recursive and brute force differs on:\n{:?}", |
| 45 |
matrix |
| 46 |
); |
| 47 |
assert_eq!( |
| 48 |
brute_force, smawk, |
| 49 |
"SMAWK and brute force differs on:\n{:?}", |
| 50 |
matrix |
| 51 |
); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/// Check that the brute force and online SMAWK functions give |
| 58 |
/// identical results on a large number of randomly generated |
| 59 |
/// Monge matrices. |
| 60 |
#[test] |
| 61 |
fn online_agree() { |
| 62 |
let sizes = [1, 2, 3, 4, 5, 10, 15, 20, 30, 50]; |
| 63 |
let mut rng = StdRng::seed_from_u64(0); |
| 64 |
for _ in 0..5 { |
| 65 |
for &size in &sizes { |
| 66 |
// Random totally monotone square matrix of the |
| 67 |
// desired size. |
| 68 |
let mut matrix: Array2<i32> = random_monge_matrix(size, size, &mut rng); |
| 69 |
|
| 70 |
// Adjust matrix so the column minima are above the |
| 71 |
// diagonal. The brute_force::column_minima will still |
| 72 |
// work just fine on such a mangled Monge matrix. |
| 73 |
let max = *matrix.iter().max().unwrap_or(&0); |
| 74 |
for idx in 0..(size as isize) { |
| 75 |
// Using the maximum value of the matrix instead |
| 76 |
// of i32::max_value() makes for prettier matrices |
| 77 |
// in case we want to print them. |
| 78 |
matrix.slice_mut(s![idx..idx + 1, ..idx + 1]).fill(max); |
| 79 |
} |
| 80 |
|
| 81 |
// The online algorithm always returns the initial |
| 82 |
// value for the left-most column -- without |
| 83 |
// inspecting the column at all. So we fill the |
| 84 |
// left-most column with this value to have the brute |
| 85 |
// force algorithm do the same. |
| 86 |
let initial = 42; |
| 87 |
matrix.slice_mut(s![0.., ..1]).fill(initial); |
| 88 |
|
| 89 |
// Brute-force computation of column minima, returned |
| 90 |
// in the same form as online_column_minima. |
| 91 |
let brute_force = brute_force::column_minima(&matrix) |
| 92 |
.iter() |
| 93 |
.enumerate() |
| 94 |
.map(|(j, &i)| (i, matrix[[i, j]])) |
| 95 |
.collect::<Vec<_>>(); |
| 96 |
let online = online_column_minima(initial, size, |_, i, j| matrix[[i, j]]); |
| 97 |
assert_eq!( |
| 98 |
brute_force, online, |
| 99 |
"brute force and online differ on:\n{:3?}", |
| 100 |
matrix |
| 101 |
); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/// Check that the algorithms agree on extremely unbalanced |
| 107 |
/// rectangular matrices. |
| 108 |
#[test] |
| 109 |
fn unbalanced_matrices_agree() { |
| 110 |
let mut rng = StdRng::seed_from_u64(42); |
| 111 |
let cases = [(1, 100), (100, 1), (2, 50), (50, 2), (5, 80), (80, 5)]; |
| 112 |
for &(m, n) in &cases { |
| 113 |
let matrix: Array2<i32> = random_monge_matrix(m, n, &mut rng); |
| 114 |
|
| 115 |
// Compute and test row minima. |
| 116 |
let brute_force = brute_force::row_minima(&matrix); |
| 117 |
let recursive = recursive::row_minima(&matrix); |
| 118 |
let smawk = smawk::row_minima(&matrix); |
| 119 |
assert_eq!(brute_force, recursive); |
| 120 |
assert_eq!(brute_force, smawk); |
| 121 |
|
| 122 |
// Do the same for the column minima. |
| 123 |
let brute_force = brute_force::column_minima(&matrix); |
| 124 |
let recursive = recursive::column_minima(&matrix); |
| 125 |
let smawk = smawk::column_minima(&matrix); |
| 126 |
assert_eq!(brute_force, recursive); |
| 127 |
assert_eq!(brute_force, smawk); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/// Check that all three algorithms agree on handling empty 0x0 |
| 132 |
/// matrices correctly. |
| 133 |
#[test] |
| 134 |
fn empty_matrices_agree() { |
| 135 |
let matrix: Array2<i32> = Array2::from_elem((0, 0), 0); |
| 136 |
let brute_force_row = brute_force::row_minima(&matrix); |
| 137 |
let recursive_row = recursive::row_minima(&matrix); |
| 138 |
let smawk_row = smawk::row_minima(&matrix); |
| 139 |
assert_eq!(brute_force_row, recursive_row); |
| 140 |
assert_eq!(brute_force_row, smawk_row); |
| 141 |
assert_eq!(brute_force_row, vec![]); |
| 142 |
|
| 143 |
let brute_force_col = brute_force::column_minima(&matrix); |
| 144 |
let recursive_col = recursive::column_minima(&matrix); |
| 145 |
let smawk_col = smawk::column_minima(&matrix); |
| 146 |
assert_eq!(brute_force_col, recursive_col); |
| 147 |
assert_eq!(brute_force_col, smawk_col); |
| 148 |
assert_eq!(brute_force_col, vec![]); |
| 149 |
} |
| 150 |
|
| 151 |
/// Check that smawk::row_minima panics when ncols is 0. |
| 152 |
#[test] |
| 153 |
#[should_panic] |
| 154 |
fn smawk_row_minima_empty_cols_panics() { |
| 155 |
let matrix: Array2<i32> = Array2::from_elem((5, 0), 0); |
| 156 |
smawk::row_minima(&matrix); |
| 157 |
} |
| 158 |
|
| 159 |
/// Check that brute_force::row_minima panics when ncols is 0. |
| 160 |
#[test] |
| 161 |
#[should_panic] |
| 162 |
fn brute_force_row_minima_empty_cols_panics() { |
| 163 |
let matrix: Array2<i32> = Array2::from_elem((5, 0), 0); |
| 164 |
brute_force::row_minima(&matrix); |
| 165 |
} |
| 166 |
|
| 167 |
/// Check that recursive::row_minima panics when ncols is 0. |
| 168 |
#[test] |
| 169 |
#[should_panic] |
| 170 |
fn recursive_row_minima_empty_cols_panics() { |
| 171 |
let matrix: Array2<i32> = Array2::from_elem((5, 0), 0); |
| 172 |
recursive::row_minima(&matrix); |
| 173 |
} |
| 174 |
|
| 175 |
/// Check that smawk::column_minima panics when nrows is 0. |
| 176 |
#[test] |
| 177 |
#[should_panic] |
| 178 |
fn smawk_column_minima_empty_rows_panics() { |
| 179 |
let matrix: Array2<i32> = Array2::from_elem((0, 5), 0); |
| 180 |
smawk::column_minima(&matrix); |
| 181 |
} |
| 182 |
|
| 183 |
/// Check that brute_force::column_minima panics when nrows is 0. |
| 184 |
#[test] |
| 185 |
#[should_panic] |
| 186 |
fn brute_force_column_minima_empty_rows_panics() { |
| 187 |
let matrix: Array2<i32> = Array2::from_elem((0, 5), 0); |
| 188 |
brute_force::column_minima(&matrix); |
| 189 |
} |
| 190 |
|
| 191 |
/// Check that recursive::column_minima panics when nrows is 0. |
| 192 |
#[test] |
| 193 |
#[should_panic] |
| 194 |
fn recursive_column_minima_empty_rows_panics() { |
| 195 |
let matrix: Array2<i32> = Array2::from_elem((0, 5), 0); |
| 196 |
recursive::column_minima(&matrix); |
| 197 |
} |