From c351f5ebd0e8ea21b1a95aa7efe83fbab9eda516 Mon Sep 17 00:00:00 2001 From: conrad Date: Fri, 14 May 2021 12:26:42 +1000 Subject: [PATCH] add .clamp() --- include/armadillo_bits/diagview_bones.hpp | 2 ++ include/armadillo_bits/diagview_meat.hpp | 34 +++++++++++++++++++ .../armadillo_bits/subview_elem1_bones.hpp | 2 ++ include/armadillo_bits/subview_elem1_meat.hpp | 16 +++++++++ .../armadillo_bits/subview_elem2_bones.hpp | 2 ++ include/armadillo_bits/subview_elem2_meat.hpp | 16 +++++++++ 6 files changed, 72 insertions(+) diff --git a/include/armadillo_bits/diagview_bones.hpp b/include/armadillo_bits/diagview_bones.hpp index 49274a5a..689dbba2 100644 --- a/include/armadillo_bits/diagview_bones.hpp +++ b/include/armadillo_bits/diagview_bones.hpp @@ -93,6 +93,8 @@ class diagview : public Base< eT, diagview > inline void replace(const eT old_val, const eT new_val); + inline void clamp(const eT min_val, const eT max_val); + inline void fill(const eT val); inline void zeros(); inline void ones(); diff --git a/include/armadillo_bits/diagview_meat.hpp b/include/armadillo_bits/diagview_meat.hpp index bc89fb44..4439bd39 100644 --- a/include/armadillo_bits/diagview_meat.hpp +++ b/include/armadillo_bits/diagview_meat.hpp @@ -937,6 +937,40 @@ diagview::replace(const eT old_val, const eT new_val) +template +inline +void +diagview::clamp(const eT min_val, const eT max_val) + { + arma_extra_debug_sigprint(); + + if(is_cx::no) + { + arma_debug_check( (access::tmp_real(min_val) > access::tmp_real(max_val)), "diagview::clamp(): min_val must be less than max_val" ); + } + else + { + arma_debug_check( (access::tmp_real(min_val) > access::tmp_real(max_val)), "diagview::clamp(): real(min_val) must be less than real(max_val)" ); + arma_debug_check( (access::tmp_imag(min_val) > access::tmp_imag(max_val)), "diagview::clamp(): imag(min_val) must be less than imag(max_val)" ); + } + + Mat& x = const_cast< Mat& >(m); + + const uword local_n_elem = n_elem; + + podarray tmp(local_n_elem); + + eT* tmp_mem = tmp.memptr(); + + for(uword ii=0; ii < local_n_elem; ++ii) { tmp_mem[ii] = x.at(ii+row_offset, ii+col_offset); } + + arrayops::clamp( tmp_mem, local_n_elem, min_val, max_val ); + + for(uword ii=0; ii < local_n_elem; ++ii) { x.at(ii+row_offset, ii+col_offset) = tmp_mem[ii]; } + } + + + template inline void diff --git a/include/armadillo_bits/subview_elem1_bones.hpp b/include/armadillo_bits/subview_elem1_bones.hpp index 05dfcf3e..4cb3a84a 100644 --- a/include/armadillo_bits/subview_elem1_bones.hpp +++ b/include/armadillo_bits/subview_elem1_bones.hpp @@ -57,6 +57,8 @@ class subview_elem1 : public Base< eT, subview_elem1 > inline void replace(const eT old_val, const eT new_val); + inline void clamp(const eT min_val, const eT max_val); + inline void fill(const eT val); inline void zeros(); inline void ones(); diff --git a/include/armadillo_bits/subview_elem1_meat.hpp b/include/armadillo_bits/subview_elem1_meat.hpp index 5f6379a6..9c879c1c 100644 --- a/include/armadillo_bits/subview_elem1_meat.hpp +++ b/include/armadillo_bits/subview_elem1_meat.hpp @@ -395,6 +395,22 @@ subview_elem1::replace(const eT old_val, const eT new_val) +template +inline +void +subview_elem1::clamp(const eT min_val, const eT max_val) + { + arma_extra_debug_sigprint(); + + Mat tmp(*this); + + tmp.clamp(min_val, max_val); + + (*this).operator=(tmp); + } + + + template inline void diff --git a/include/armadillo_bits/subview_elem2_bones.hpp b/include/armadillo_bits/subview_elem2_bones.hpp index eef44996..ff89c63e 100644 --- a/include/armadillo_bits/subview_elem2_bones.hpp +++ b/include/armadillo_bits/subview_elem2_bones.hpp @@ -60,6 +60,8 @@ class subview_elem2 : public Base< eT, subview_elem2 > inline void clean(const pod_type threshold); + inline void clamp(const eT min_val, const eT max_val); + inline void fill(const eT val); inline void zeros(); inline void ones(); diff --git a/include/armadillo_bits/subview_elem2_meat.hpp b/include/armadillo_bits/subview_elem2_meat.hpp index 13dfdd28..89b74a44 100644 --- a/include/armadillo_bits/subview_elem2_meat.hpp +++ b/include/armadillo_bits/subview_elem2_meat.hpp @@ -334,6 +334,22 @@ subview_elem2::clean(const pod_type threshold) +template +inline +void +subview_elem2::clamp(const eT min_val, const eT max_val) + { + arma_extra_debug_sigprint(); + + Mat tmp(*this); + + tmp.clamp(min_val, max_val); + + (*this).operator=(tmp); + } + + + template inline void