Added imax/imin() as variations of iamax/iamin()

This commit is contained in:
Bernardo Meurer 2018-07-09 13:52:34 -07:00 committed by Sébastien Crozet
parent 3eaa65c9cf
commit c9be27abb5

View File

@ -14,6 +14,27 @@ use base::storage::{Storage, StorageMut};
use base::{DefaultAllocator, Matrix, Scalar, SquareMatrix, Vector}; use base::{DefaultAllocator, Matrix, Scalar, SquareMatrix, Vector};
impl<N: Scalar + PartialOrd + Signed, D: Dim, S: Storage<N, D>> Vector<N, D, S> { impl<N: Scalar + PartialOrd + Signed, D: Dim, S: Storage<N, D>> Vector<N, D, S> {
/// Computes the index of the vector component with the largest value.
#[inline]
pub fn imax(&self) -> usize {
assert!(!self.is_empty(), "The input vector must not be empty.");
let mut the_max = unsafe { self.vget_unchecked(0) };
let mut the_i = 0;
for i in 1..self.nrows() {
let val = unsafe { self.vget_unchecked(i) };
if val > the_max {
the_max = val;
the_i = i;
}
}
the_i
}
/// Computes the index of the vector component with the largest absolute value. /// Computes the index of the vector component with the largest absolute value.
#[inline] #[inline]
pub fn iamax(&self) -> usize { pub fn iamax(&self) -> usize {
@ -34,6 +55,26 @@ impl<N: Scalar + PartialOrd + Signed, D: Dim, S: Storage<N, D>> Vector<N, D, S>
the_i the_i
} }
/// Computes the index of the vector component with the smallest value.
#[inline]
pub fn imin(&self) -> usize {
assert!(!self.is_empty(), "The input vector must not be empty.");
let mut the_max = unsafe { self.vget_unchecked(0) };
let mut the_i = 0;
for i in 1..self.nrows() {
let val = unsafe { self.vget_unchecked(i) };
if val < the_max {
the_max = val;
the_i = i;
}
}
the_i
}
/// Computes the index of the vector component with the smallest absolute value. /// Computes the index of the vector component with the smallest absolute value.
#[inline] #[inline]
pub fn iamin(&self) -> usize { pub fn iamin(&self) -> usize {