From 8037558eae9f62f3038078e71e9151ebeef2ffd3 Mon Sep 17 00:00:00 2001 From: conrad Date: Tue, 4 May 2021 00:06:20 +1000 Subject: [PATCH] add .clamp() --- include/armadillo_bits/Mat_bones.hpp | 2 + include/armadillo_bits/Mat_meat.hpp | 14 +++++++ include/armadillo_bits/arrayops_bones.hpp | 13 +++--- include/armadillo_bits/arrayops_meat.hpp | 48 +++++++++++++++++++++++ 4 files changed, 72 insertions(+), 5 deletions(-) diff --git a/include/armadillo_bits/Mat_bones.hpp b/include/armadillo_bits/Mat_bones.hpp index 226cbc30..d813ee45 100644 --- a/include/armadillo_bits/Mat_bones.hpp +++ b/include/armadillo_bits/Mat_bones.hpp @@ -467,6 +467,8 @@ class Mat : public Base< eT, Mat > inline const Mat& clean(const pod_type threshold); + inline const Mat& clamp(const eT min_val, const eT max_val); + inline const Mat& fill(const eT val); template diff --git a/include/armadillo_bits/Mat_meat.hpp b/include/armadillo_bits/Mat_meat.hpp index 90559720..52a6cb66 100644 --- a/include/armadillo_bits/Mat_meat.hpp +++ b/include/armadillo_bits/Mat_meat.hpp @@ -6860,6 +6860,20 @@ Mat::clean(const typename get_pod_type::result threshold) +template +inline +const Mat& +Mat::clamp(const eT min_val, const eT max_val) + { + arma_extra_debug_sigprint(); + + arrayops::clamp(memptr(), n_elem, min_val, max_val); + + return *this; + } + + + //! fill the matrix with the specified value template inline diff --git a/include/armadillo_bits/arrayops_bones.hpp b/include/armadillo_bits/arrayops_bones.hpp index 0391573a..f04f9dbf 100644 --- a/include/armadillo_bits/arrayops_bones.hpp +++ b/include/armadillo_bits/arrayops_bones.hpp @@ -26,31 +26,34 @@ class arrayops arma_inline static void copy(eT* dest, const eT* src, const uword n_elem); - template arma_cold inline static void copy_small(eT* dest, const eT* src, const uword n_elem); - template inline static void fill_zeros(eT* dest, const uword n_elem); - template arma_hot inline static void replace(eT* mem, const uword n_elem, const eT old_val, const eT new_val); - template arma_hot inline static void clean(eT* mem, const uword n_elem, const eT abs_limit, const typename arma_not_cx::result* junk = nullptr); - template arma_hot inline static void clean(std::complex* mem, const uword n_elem, const T abs_limit); + template + inline static void + clamp(eT* mem, const uword n_elem, const eT min_val, const eT max_val, const typename arma_not_cx::result* junk = nullptr); + + template + inline static void + clamp(std::complex* mem, const uword n_elem, const std::complex& min_val, const std::complex& max_val); + // // array = convert(array) diff --git a/include/armadillo_bits/arrayops_meat.hpp b/include/armadillo_bits/arrayops_meat.hpp index 35cf6964..280021a5 100644 --- a/include/armadillo_bits/arrayops_meat.hpp +++ b/include/armadillo_bits/arrayops_meat.hpp @@ -177,6 +177,54 @@ arrayops::clean(std::complex* mem, const uword n_elem, const T abs_limit) +template +arma_hot +inline +void +arrayops::clamp(eT* mem, const uword n_elem, const eT min_val, const eT max_val, const typename arma_not_cx::result* junk) + { + arma_ignore(junk); + + for(uword i=0; i max_val) ? max_val : val); + } + } + + + +template +arma_hot +inline +void +arrayops::clamp(std::complex* mem, const uword n_elem, const std::complex& min_val, const std::complex& max_val) + { + typedef typename std::complex eT; + + const T min_val_real = std::real(min_val); + const T min_val_imag = std::imag(min_val); + + const T max_val_real = std::real(max_val); + const T max_val_imag = std::imag(max_val); + + for(uword i=0; i max_val_real) ? max_val_real : val_real); + val_imag = (val_imag < min_val_imag) ? min_val_imag : ((val_imag > max_val_imag) ? max_val_imag : val_imag); + + val = std::complex(val_real,val_imag); + } + } + + + template arma_inline void