Skip to content

Implement argmin argmax #30

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

Merged
merged 8 commits into from
Mar 10, 2019
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
84 changes: 84 additions & 0 deletions src/quantile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,31 @@ where
S: Data<Elem = A>,
D: Dimension,
{
/// Finds the first index of the minimum value of the array.
///
/// Returns `None` if any of the pairwise orderings tested by the function
/// are undefined. (For example, this occurs if there are any
/// floating-point NaN values in the array.)
///
/// Returns `None` if the array is empty.
///
/// # Example
///
/// ```
/// extern crate ndarray;
/// extern crate ndarray_stats;
///
/// use ndarray::array;
/// use ndarray_stats::QuantileExt;
///
/// let a = array![[1., 3., 5.],
/// [2., 0., 6.]];
/// assert_eq!(a.argmin(), Some((1, 1)));
/// ```
fn argmin(&self) -> Option<D::Pattern>
where
A: PartialOrd;

/// Finds the elementwise minimum of the array.
///
/// Returns `None` if any of the pairwise orderings tested by the function
Expand All @@ -203,6 +228,31 @@ where
A: MaybeNan,
A::NotNan: Ord;

/// Finds the first index of the maximum value of the array.
///
/// Returns `None` if any of the pairwise orderings tested by the function
/// are undefined. (For example, this occurs if there are any
/// floating-point NaN values in the array.)
///
/// Returns `None` if the array is empty.
///
/// # Example
///
/// ```
/// extern crate ndarray;
/// extern crate ndarray_stats;
///
/// use ndarray::array;
/// use ndarray_stats::QuantileExt;
///
/// let a = array![[1., 3., 7.],
/// [2., 5., 6.]];
/// assert_eq!(a.argmax(), Some((0, 2)));
/// ```
fn argmax(&self) -> Option<D::Pattern>
where
A: PartialOrd;

/// Finds the elementwise maximum of the array.
///
/// Returns `None` if any of the pairwise orderings tested by the function
Expand Down Expand Up @@ -278,6 +328,23 @@ where
S: Data<Elem = A>,
D: Dimension,
{
fn argmin(&self) -> Option<D::Pattern>
where
A: PartialOrd,
{
let mut current_min = self.first()?;
let mut current_pattern_min = D::zeros(self.ndim()).into_pattern();

for (pattern, elem) in self.indexed_iter() {
if elem.partial_cmp(current_min)? == cmp::Ordering::Less {
current_pattern_min = pattern;
current_min = elem
}
}

Some(current_pattern_min)
}

fn min(&self) -> Option<&A>
where
A: PartialOrd,
Expand All @@ -303,6 +370,23 @@ where
}))
}

fn argmax(&self) -> Option<D::Pattern>
where
A: PartialOrd,
{
let mut current_max = self.first()?;
let mut current_pattern_max = D::zeros(self.ndim()).into_pattern();

for (pattern, elem) in self.indexed_iter() {
if elem.partial_cmp(current_max)? == cmp::Ordering::Greater {
current_pattern_max = pattern;
current_max = elem
}
}

Some(current_pattern_max)
}

fn max(&self) -> Option<&A>
where
A: PartialOrd,
Expand Down
36 changes: 36 additions & 0 deletions tests/quantile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ use ndarray_stats::{
Quantile1dExt, QuantileExt,
};

#[test]
fn test_argmin() {
let a = array![[1, 5, 3], [2, 0, 6]];
assert_eq!(a.argmin(), Some((1, 1)));

let a = array![[1., 5., 3.], [2., 0., 6.]];
assert_eq!(a.argmin(), Some((1, 1)));

let a = array![[1., 5., 3.], [2., ::std::f64::NAN, 6.]];
assert_eq!(a.argmin(), None);

let a = array![[1, 0, 3], [2, 0, 6]];
assert_eq!(a.argmin(), Some((0, 1)));

let a: Array2<i32> = array![[], []];
assert_eq!(a.argmin(), None);
}

#[test]
fn test_min() {
let a = array![[1, 5, 3], [2, 0, 6]];
Expand Down Expand Up @@ -35,6 +53,24 @@ fn test_min_skipnan_all_nan() {
assert!(a.min_skipnan().is_nan());
}

#[test]
fn test_argmax() {
let a = array![[1, 5, 3], [2, 0, 6]];
assert_eq!(a.argmax(), Some((1, 2)));

let a = array![[1., 5., 3.], [2., 0., 6.]];
assert_eq!(a.argmax(), Some((1, 2)));

let a = array![[1., 5., 3.], [2., ::std::f64::NAN, 6.]];
assert_eq!(a.argmax(), None);

let a = array![[1, 5, 6], [2, 0, 6]];
assert_eq!(a.argmax(), Some((0, 2)));

let a: Array2<i32> = array![[], []];
assert_eq!(a.argmax(), None);
}

#[test]
fn test_max() {
let a = array![[1, 5, 7], [2, 0, 6]];
Expand Down