From c9be27abb5149065ea4b51d2ae0d050ca0cf7a04 Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Date: Mon, 9 Jul 2018 13:52:34 -0700 Subject: [PATCH] Added imax/imin() as variations of iamax/iamin() --- src/base/blas.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/base/blas.rs b/src/base/blas.rs index 982bbdbf..3c04b8e0 100644 --- a/src/base/blas.rs +++ b/src/base/blas.rs @@ -14,6 +14,27 @@ use base::storage::{Storage, StorageMut}; use base::{DefaultAllocator, Matrix, Scalar, SquareMatrix, Vector}; impl> Vector { + + /// 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. #[inline] pub fn iamax(&self) -> usize { @@ -34,6 +55,26 @@ impl> Vector 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. #[inline] pub fn iamin(&self) -> usize {