From 576dcae341ffe4ea690063b1b73f8aa61bd7aea4 Mon Sep 17 00:00:00 2001 From: mandeep Date: Mon, 21 Oct 2019 17:21:35 -0400 Subject: [PATCH] Add component_min and component_max methods --- src/base/componentwise.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/base/componentwise.rs b/src/base/componentwise.rs index e5f4d7ec..f5ace2b8 100644 --- a/src/base/componentwise.rs +++ b/src/base/componentwise.rs @@ -1,6 +1,7 @@ // Non-conventional component-wise operators. use num::{Signed, Zero}; +use std::cmp::Ord; use std::ops::{Add, Mul}; use alga::general::{ClosedDiv, ClosedMul}; @@ -40,6 +41,44 @@ impl> Matrix { res } + /// Computes the component-wise minimum + /// + /// # Example + /// + /// ``` + /// use nalgebra::Vector3; + /// + /// let u = Vector3::new(-1.0, 3.0, 2.0); + /// let v = Vector3::new(1.0, 2.0, 3.0); + /// assert_eq!(u.component_min(v), Vector3::new(-1.0, 2.0, 3.0)) + /// ``` + pub fn component_min(&self, rhs: &Matrix) -> MatrixMN + where + N: Ord, + DefaultAllocator: Allocator, + { + self.zip_map(rhs, |a, b| a.min(b)) + } + + /// Computes the component-wise maximum + /// + /// # Example + /// + /// ``` + /// use nalgebra::Vector3; + /// + /// let u = Vector3::new(-1.0, 3.0, 2.0); + /// let v = Vector3::new(1.0, 2.0, 3.0); + /// assert_eq!(u.component_max(v), Vector3::new(1.0, 3.0, 3.0)) + /// ``` + pub fn component_max(&self, rhs: &Matrix) -> MatrixMN + where + N: Ord, + DefaultAllocator: Allocator, + { + self.zip_map(rhs, |a, b| a.min(b)) + } + // FIXME: add other operators like component_ln, component_pow, etc. ? }