| 1 |
#![cfg(feature = "ndarray")] |
| 2 |
|
| 3 |
use ndarray::{s, Array2}; |
| 4 |
use rand::SeedableRng; |
| 5 |
use rand_chacha::ChaCha20Rng; |
| 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 = vec![1, 2, 3, 4, 5, 10, 15, 20, 30]; |
| 17 |
let mut rng = ChaCha20Rng::seed_from_u64(0); |
| 18 |
for _ in 0..4 { |
| 19 |
for m in sizes.clone().iter() { |
| 20 |
for n in sizes.clone().iter() { |
| 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 = vec![1, 2, 3, 4, 5, 10, 15, 20, 30, 50]; |
| 63 |
let mut rng = ChaCha20Rng::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 |
} |