From b687c63bb2cc366e5f0f3b4b655c47169ca08f75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crozet=20S=C3=A9bastien?= Date: Sat, 6 Mar 2021 19:16:22 +0100 Subject: [PATCH] Add a SIMD version of cap_magnitude: simd_cap_magnitude. --- src/base/norm.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/base/norm.rs b/src/base/norm.rs index 9717e031..3f0508f2 100644 --- a/src/base/norm.rs +++ b/src/base/norm.rs @@ -8,9 +8,9 @@ use crate::allocator::Allocator; use crate::base::{DefaultAllocator, Dim, DimName, Matrix, MatrixMN, Normed, VectorN}; use crate::constraint::{SameNumberOfColumns, SameNumberOfRows, ShapeConstraint}; use crate::storage::{Storage, StorageMut}; -use crate::{ComplexField, RealField, Scalar, SimdComplexField, Unit}; +use crate::{ComplexField, Scalar, SimdComplexField, Unit}; use simba::scalar::ClosedNeg; -use simba::simd::{SimdOption, SimdPartialOrd}; +use simba::simd::{SimdOption, SimdPartialOrd, SimdValue}; // TODO: this should be be a trait on alga? /// A trait for abstract matrix norms. @@ -343,7 +343,7 @@ impl> Matrix { #[inline] pub fn cap_magnitude(&self, max: N::RealField) -> MatrixMN where - N: RealField, + N: ComplexField, DefaultAllocator: Allocator, { let n = self.norm(); @@ -355,6 +355,20 @@ impl> Matrix { } } + /// Returns a new vector with the same magnitude as `self` clamped between `0.0` and `max`. + #[inline] + pub fn simd_cap_magnitude(&self, max: N::SimdRealField) -> MatrixMN + where + N: SimdComplexField, + N::Element: Scalar, + DefaultAllocator: Allocator + Allocator, + { + let n = self.norm(); + let scaled = self.scale(max / n); + let use_scaled = n.simd_gt(max); + scaled.select(use_scaled, self.clone_owned()) + } + /// Returns a normalized version of this matrix unless its norm as smaller or equal to `eps`. /// /// The components of this matrix cannot be SIMD types (see `simd_try_normalize`) instead.