add .clamp()

This commit is contained in:
conrad
2021-05-04 00:06:20 +10:00
parent 8e41215cdb
commit 8037558eae
4 changed files with 72 additions and 5 deletions
+2
View File
@@ -467,6 +467,8 @@ class Mat : public Base< eT, Mat<eT> >
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<typename fill_type>
+14
View File
@@ -6860,6 +6860,20 @@ Mat<eT>::clean(const typename get_pod_type<eT>::result threshold)
template<typename eT>
inline
const Mat<eT>&
Mat<eT>::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<typename eT>
inline
+8 -5
View File
@@ -26,31 +26,34 @@ class arrayops
arma_inline static void
copy(eT* dest, const eT* src, const uword n_elem);
template<typename eT>
arma_cold inline static void
copy_small(eT* dest, const eT* src, const uword n_elem);
template<typename eT>
inline static void
fill_zeros(eT* dest, const uword n_elem);
template<typename eT>
arma_hot inline static void
replace(eT* mem, const uword n_elem, const eT old_val, const eT new_val);
template<typename eT>
arma_hot inline static void
clean(eT* mem, const uword n_elem, const eT abs_limit, const typename arma_not_cx<eT>::result* junk = nullptr);
template<typename T>
arma_hot inline static void
clean(std::complex<T>* mem, const uword n_elem, const T abs_limit);
template<typename eT>
inline static void
clamp(eT* mem, const uword n_elem, const eT min_val, const eT max_val, const typename arma_not_cx<eT>::result* junk = nullptr);
template<typename T>
inline static void
clamp(std::complex<T>* mem, const uword n_elem, const std::complex<T>& min_val, const std::complex<T>& max_val);
//
// array = convert(array)
+48
View File
@@ -177,6 +177,54 @@ arrayops::clean(std::complex<T>* mem, const uword n_elem, const T abs_limit)
template<typename eT>
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<eT>::result* junk)
{
arma_ignore(junk);
for(uword i=0; i<n_elem; ++i)
{
eT& val = mem[i];
val = (val < min_val) ? min_val : ((val > max_val) ? max_val : val);
}
}
template<typename T>
arma_hot
inline
void
arrayops::clamp(std::complex<T>* mem, const uword n_elem, const std::complex<T>& min_val, const std::complex<T>& max_val)
{
typedef typename std::complex<T> 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<n_elem; ++i)
{
eT& val = mem[i];
T val_real = std::real(val);
T val_imag = std::imag(val);
val_real = (val_real < min_val_real) ? min_val_real : ((val_real > 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<T>(val_real,val_imag);
}
}
template<typename out_eT, typename in_eT>
arma_inline
void