From dc41b55e5ae876008aebea70163ba40b96789f8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Fri, 2 Feb 2018 12:26:22 +0100 Subject: [PATCH] Add iamin. --- CHANGELOG.md | 2 ++ src/core/blas.rs | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 758f884f..2fe62236 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). and to types of the [mint](https://crates.io/crates/mint) crate. * The `::repeat(...)` constructor that is an alternative name to `::from_element(...)`. + * The `.iamin()` methods that returns the index of the vector entry with + smallest absolute value. diff --git a/src/core/blas.rs b/src/core/blas.rs index 517e7793..65dbe7e8 100644 --- a/src/core/blas.rs +++ b/src/core/blas.rs @@ -31,6 +31,26 @@ impl> Vector the_i } + + /// Computes the index of the vector component with the smallest absolute value. + #[inline] + pub fn iamin(&self) -> usize { + assert!(!self.is_empty(), "The input vector must not be empty."); + + let mut the_max = unsafe { self.vget_unchecked(0).abs() }; + let mut the_i = 0; + + for i in 1 .. self.nrows() { + let val = unsafe { self.vget_unchecked(i).abs() }; + + if val < the_max { + the_max = val; + the_i = i; + } + } + + the_i + } } impl> Matrix {