Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the dependency on the ndarray crate optional #47

Merged
merged 2 commits into from
Aug 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ appveyor = { repository = "mgeisler/smawk" }
codecov = { repository = "mgeisler/smawk" }

[dependencies]
ndarray = "0.13"
ndarray = { version = "0.13", optional = true }
num-traits = "0.2"
rand = "0.7"

Expand Down
1 change: 1 addition & 0 deletions benches/comparison.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "ndarray")]
#![feature(test)]

extern crate test;
Expand Down
6 changes: 6 additions & 0 deletions src/brute_force.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
//! Brute-force algorithm for finding column minima.
//!
//! The functions here are mostly meant to be used for testing
//! correctness of the SMAWK implementation.
//!
//! **Note: this module is only available if you enable the `ndarray`
//! Cargo feature.**

use ndarray::{Array2, ArrayView1};

Expand Down
143 changes: 80 additions & 63 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,11 @@

#![doc(html_root_url = "https://docs.rs/smawk/0.2.0")]

#[cfg(feature = "ndarray")]
pub mod brute_force;
#[cfg(feature = "ndarray")]
pub mod monge;
#[cfg(feature = "ndarray")]
pub mod recursive;

/// Minimal matrix trait for two-dimensional arrays.
Expand All @@ -101,6 +104,9 @@ pub mod recursive;
/// elements. Modeled after
/// [`ndarray::Array2`](https://docs.rs/ndarray/latest/ndarray/type.Array2.html)
/// from the [ndarray crate ](https://crates.io/crates/ndarray).
///
/// Enable the `ndarray` Cargo feature if you want to use it with
/// `ndarray::Array2`.
pub trait Matrix<T: Copy> {
/// Return the number of rows.
fn nrows(&self) -> usize;
Expand Down Expand Up @@ -129,6 +135,10 @@ impl<T: Copy> Matrix<T> for Vec<Vec<T>> {
}

/// Adapting `ndarray::Array2` to the `Matrix` trait.
///
/// **Note: this implementation is only available if you enable the
/// `ndarray` Cargo feature.**
#[cfg(feature = "ndarray")]
impl<T: Copy> Matrix<T> for ndarray::Array2<T> {
#[inline]
fn nrows(&self) -> usize {
Expand Down Expand Up @@ -376,120 +386,127 @@ pub fn online_column_minima<T: Copy + Ord, M: Fn(&[(usize, T)], usize, usize) ->
#[cfg(test)]
mod tests {
use super::*;
use ndarray::arr2;

#[test]
fn smawk_1x1() {
let matrix = arr2(&[[2]]);
let minima = vec![0];
assert_eq!(smawk_row_minima(&matrix), minima);
assert_eq!(smawk_column_minima(&matrix.reversed_axes()), minima);
let matrix = vec![vec![2]];
assert_eq!(smawk_row_minima(&matrix), vec![0]);
assert_eq!(smawk_column_minima(&matrix), vec![0]);
}

#[test]
fn smawk_2x1() {
let matrix = arr2(&[[3], [2]]);
let minima = vec![0, 0];
assert_eq!(smawk_row_minima(&matrix), minima);
assert_eq!(smawk_column_minima(&matrix.reversed_axes()), minima);
let matrix = vec![
vec![3], //
vec![2],
];
assert_eq!(smawk_row_minima(&matrix), vec![0, 0]);
assert_eq!(smawk_column_minima(&matrix), vec![1]);
}

#[test]
fn smawk_1x2() {
let matrix = arr2(&[[2, 1]]);
let minima = vec![1];
assert_eq!(smawk_row_minima(&matrix), minima);
assert_eq!(smawk_column_minima(&matrix.reversed_axes()), minima);
let matrix = vec![vec![2, 1]];
assert_eq!(smawk_row_minima(&matrix), vec![1]);
assert_eq!(smawk_column_minima(&matrix), vec![0, 0]);
}

#[test]
fn smawk_2x2() {
let matrix = arr2(&[[3, 2], [2, 1]]);
let minima = vec![1, 1];
assert_eq!(smawk_row_minima(&matrix), minima);
assert_eq!(smawk_column_minima(&matrix.reversed_axes()), minima);
let matrix = vec![
vec![3, 2], //
vec![2, 1],
];
assert_eq!(smawk_row_minima(&matrix), vec![1, 1]);
assert_eq!(smawk_column_minima(&matrix), vec![1, 1]);
}

#[test]
fn smawk_3x3() {
let matrix = arr2(&[[3, 4, 4], [3, 4, 4], [2, 3, 3]]);
let minima = vec![0, 0, 0];
assert_eq!(smawk_row_minima(&matrix), minima);
assert_eq!(smawk_column_minima(&matrix.reversed_axes()), minima);
let matrix = vec![
vec![3, 4, 4], //
vec![3, 4, 4],
vec![2, 3, 3],
];
assert_eq!(smawk_row_minima(&matrix), vec![0, 0, 0]);
assert_eq!(smawk_column_minima(&matrix), vec![2, 2, 2]);
}

#[test]
fn smawk_4x4() {
let matrix = arr2(&[[4, 5, 5, 5], [2, 3, 3, 3], [2, 3, 3, 3], [2, 2, 2, 2]]);
let minima = vec![0, 0, 0, 0];
assert_eq!(smawk_row_minima(&matrix), minima);
assert_eq!(smawk_column_minima(&matrix.reversed_axes()), minima);
let matrix = vec![
vec![4, 5, 5, 5], //
vec![2, 3, 3, 3],
vec![2, 3, 3, 3],
vec![2, 2, 2, 2],
];
assert_eq!(smawk_row_minima(&matrix), vec![0, 0, 0, 0]);
assert_eq!(smawk_column_minima(&matrix), vec![1, 3, 3, 3]);
}

#[test]
fn smawk_5x5() {
let matrix = arr2(&[
[3, 2, 4, 5, 6],
[2, 1, 3, 3, 4],
[2, 1, 3, 3, 4],
[3, 2, 4, 3, 4],
[4, 3, 2, 1, 1],
]);
let minima = vec![1, 1, 1, 1, 3];
assert_eq!(smawk_row_minima(&matrix), minima);
assert_eq!(smawk_column_minima(&matrix.reversed_axes()), minima);
let matrix = vec![
vec![3, 2, 4, 5, 6],
vec![2, 1, 3, 3, 4],
vec![2, 1, 3, 3, 4],
vec![3, 2, 4, 3, 4],
vec![4, 3, 2, 1, 1],
];
assert_eq!(smawk_row_minima(&matrix), vec![1, 1, 1, 1, 3]);
assert_eq!(smawk_column_minima(&matrix), vec![1, 1, 4, 4, 4]);
}

#[test]
fn online_1x1() {
let matrix = arr2(&[[0]]);
let matrix = vec![vec![0]];
let minima = vec![(0, 0)];
assert_eq!(
online_column_minima(0, 1, |_, i, j| matrix[[i, j]],),
minima
);
assert_eq!(online_column_minima(0, 1, |_, i, j| matrix[i][j]), minima);
}

#[test]
fn online_2x2() {
let matrix = arr2(&[[0, 2], [0, 0]]);
let matrix = vec![
vec![0, 2], //
vec![0, 0],
];
let minima = vec![(0, 0), (0, 2)];
assert_eq!(
online_column_minima(0, 2, |_, i, j| matrix[[i, j]],),
minima
);
assert_eq!(online_column_minima(0, 2, |_, i, j| matrix[i][j]), minima);
}

#[test]
fn online_3x3() {
let matrix = arr2(&[[0, 4, 4], [0, 0, 4], [0, 0, 0]]);
let matrix = vec![
vec![0, 4, 4], //
vec![0, 0, 4],
vec![0, 0, 0],
];
let minima = vec![(0, 0), (0, 4), (0, 4)];
assert_eq!(
online_column_minima(0, 3, |_, i, j| matrix[[i, j]],),
minima
);
assert_eq!(online_column_minima(0, 3, |_, i, j| matrix[i][j]), minima);
}

#[test]
fn online_4x4() {
let matrix = arr2(&[[0, 5, 5, 5], [0, 0, 3, 3], [0, 0, 0, 3], [0, 0, 0, 0]]);
let matrix = vec![
vec![0, 5, 5, 5], //
vec![0, 0, 3, 3],
vec![0, 0, 0, 3],
vec![0, 0, 0, 0],
];
let minima = vec![(0, 0), (0, 5), (1, 3), (1, 3)];
assert_eq!(
online_column_minima(0, 4, |_, i, j| matrix[[i, j]],),
minima
);
assert_eq!(online_column_minima(0, 4, |_, i, j| matrix[i][j]), minima);
}

#[test]
fn online_5x5() {
let matrix = arr2(&[
[0, 2, 4, 6, 7],
[0, 0, 3, 4, 5],
[0, 0, 0, 3, 4],
[0, 0, 0, 0, 4],
[0, 0, 0, 0, 0],
]);
let matrix = vec![
vec![0, 2, 4, 6, 7],
vec![0, 0, 3, 4, 5],
vec![0, 0, 0, 3, 4],
vec![0, 0, 0, 0, 4],
vec![0, 0, 0, 0, 0],
];
let minima = vec![(0, 0), (0, 2), (1, 3), (2, 3), (2, 4)];
assert_eq!(online_column_minima(0, 5, |_, i, j| matrix[[i, j]]), minima);
assert_eq!(online_column_minima(0, 5, |_, i, j| matrix[i][j]), minima);
}
}
6 changes: 6 additions & 0 deletions src/monge.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
//! Functions for generating and checking Monge arrays.
//!
//! The functions here are mostly meant to be used for testing
//! correctness of the SMAWK implementation.
//!
//! **Note: this module is only available if you enable the `ndarray`
//! Cargo feature.**

use ndarray::{s, Array2};
use num_traits::{PrimInt, WrappingAdd};
Expand Down
6 changes: 6 additions & 0 deletions src/recursive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
//! Recursive algorithm for finding column minima.
//!
//! The functions here are mostly meant to be used for testing
//! correctness of the SMAWK implementation.
//!
//! **Note: this module is only available if you enable the `ndarray`
//! Cargo feature.**

use ndarray::{s, Array2, ArrayView2, Axis};

Expand Down
2 changes: 2 additions & 0 deletions tests/agreement.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "ndarray")]

use ndarray::{s, Array2};
use rand::SeedableRng;
use rand_chacha::ChaCha20Rng;
Expand Down
2 changes: 2 additions & 0 deletions tests/complexity.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "ndarray")]

use ndarray::{Array1, Array2};
use rand::SeedableRng;
use rand_chacha::ChaCha20Rng;
Expand Down