From 8e41215cdb7cc46db8e00e4eebf3ea79e311d750 Mon Sep 17 00:00:00 2001 From: conrad Date: Mon, 3 May 2021 23:40:03 +1000 Subject: [PATCH] simplifications --- include/armadillo_bits/fn_clamp.hpp | 4 - include/armadillo_bits/op_clamp_bones.hpp | 8 +- include/armadillo_bits/op_clamp_meat.hpp | 296 ++++++++++++---------- 3 files changed, 163 insertions(+), 145 deletions(-) diff --git a/include/armadillo_bits/fn_clamp.hpp b/include/armadillo_bits/fn_clamp.hpp index d7dd726e..94216387 100644 --- a/include/armadillo_bits/fn_clamp.hpp +++ b/include/armadillo_bits/fn_clamp.hpp @@ -32,8 +32,6 @@ clamp(const T1& X, const typename T1::elem_type min_val, const typename T1::elem { arma_extra_debug_sigprint(); - arma_debug_check( (min_val > max_val), "clamp(): min_val must be less than max_val" ); - return mtOp(mtOp_dual_aux_indicator(), X, min_val, max_val); } @@ -66,8 +64,6 @@ clamp(const BaseCube& X, const typename T1::elem_type arma_extra_debug_sigprint(); arma_ignore(junk); - arma_debug_check( (min_val > max_val), "clamp(): min_val must be less than max_val" ); - return mtOpCube(mtOpCube_dual_aux_indicator(), X.get_ref(), min_val, max_val); } diff --git a/include/armadillo_bits/op_clamp_bones.hpp b/include/armadillo_bits/op_clamp_bones.hpp index 466239c9..0a7bac46 100644 --- a/include/armadillo_bits/op_clamp_bones.hpp +++ b/include/armadillo_bits/op_clamp_bones.hpp @@ -29,17 +29,17 @@ class op_clamp template inline static void apply(Mat& out, const mtOp& in); - template inline static void apply_proxy_noalias(Mat& out, const Proxy& P, const typename T1::elem_type min_val, const typename T1::elem_type max_val); - template inline static void apply_direct(Mat& out, const Mat& X, const eT min_val, const eT max_val); + template inline static void apply_proxy_noalias(Mat& out, const Proxy& P, const typename T1::elem_type min_val, const typename T1::elem_type max_val); + // cubes template inline static void apply(Cube& out, const mtOpCube& in); - template inline static void apply_proxy_noalias(Cube& out, const ProxyCube& P, const typename T1::elem_type min_val, const typename T1::elem_type max_val); - template inline static void apply_direct(Cube& out, const Cube& X, const eT min_val, const eT max_val); + + template inline static void apply_proxy_noalias(Cube& out, const ProxyCube& P, const typename T1::elem_type min_val, const typename T1::elem_type max_val); }; diff --git a/include/armadillo_bits/op_clamp_meat.hpp b/include/armadillo_bits/op_clamp_meat.hpp index 79f8f2d5..f93a2189 100644 --- a/include/armadillo_bits/op_clamp_meat.hpp +++ b/include/armadillo_bits/op_clamp_meat.hpp @@ -27,17 +27,77 @@ op_clamp::apply(Mat& out, const mtOp P(in.m); + typedef typename T1::elem_type eT; - if(is_Mat::stored_type>::value || P.is_alias(out)) + const eT min_val = in.aux; + const eT max_val = in.aux_out_eT; + + arma_debug_check( (min_val > max_val), "clamp(): min_val must be less than max_val" ); + + if(is_Mat::value) { - const unwrap::stored_type> U(P.Q); + const unwrap U(in.m); - op_clamp::apply_direct(out, U.M, in.aux, in.aux_out_eT); + op_clamp::apply_direct(out, U.M, min_val, max_val); } else { - op_clamp::apply_proxy_noalias(out, P, in.aux, in.aux_out_eT); + const Proxy P(in.m); + + if(P.is_alias(out)) + { + Mat tmp; + + op_clamp::apply_proxy_noalias(tmp, P, min_val, max_val); + + out.steal_mem(tmp); + } + else + { + op_clamp::apply_proxy_noalias(out, P, min_val, max_val); + } + } + } + + + +template +inline +void +op_clamp::apply_direct(Mat& out, const Mat& X, const eT min_val, const eT max_val) + { + arma_extra_debug_sigprint(); + + if(&out != &X) + { + out.set_size(X.n_rows, X.n_cols); + + const uword N = out.n_elem; + + const eT* X_mem = X.memptr(); + eT* out_mem = out.memptr(); + + for(uword i=0; i max_val) ? max_val : val); + } + } + else + { + arma_extra_debug_print("op_clamp::apply_direct(): inplace operation"); + + const uword N = out.n_elem; + + eT* out_mem = out.memptr(); + + for(uword i=0; i max_val) ? max_val : out_val); + } } } @@ -65,72 +125,23 @@ op_clamp::apply_proxy_noalias(Mat& out, const Proxy& typename Proxy::ea_type A = P.get_ea(); - uword j; - for(j=1; j max_val) ? max_val : val_i); - val_j = (val_j < min_val) ? min_val : ((val_j > max_val) ? max_val : val_j); - - (*out_mem) = val_i; out_mem++; - (*out_mem) = val_j; out_mem++; - } - - const uword i = j-1; - - if(i < N) - { - eT val_i = A[i]; - - val_i = (val_i < min_val) ? min_val : ((val_i > max_val) ? max_val : val_i); - - (*out_mem) = val_i; - } - } - else - { - for(uword col=0; col max_val) ? max_val : val); - - (*out_mem) = val; out_mem++; - } - } - } - - - -template -inline -void -op_clamp::apply_direct(Mat& out, const Mat& X, const eT min_val, const eT max_val) - { - arma_extra_debug_sigprint(); - - if(&out != &X) - { - const Proxy< Mat > P(X); - - op_clamp::apply_proxy_noalias(out, P, min_val, max_val); - } - else - { - arma_extra_debug_print("inplace operation"); - - const uword N = out.n_elem; - - eT* out_mem = out.memptr(); - for(uword i=0; i max_val) ? max_val : out_val ); + out_mem[i] = (val < min_val) ? min_val : ((val > max_val) ? max_val : val); + } + } + else + { + for(uword col=0; col < n_cols; ++col) + for(uword row=0; row < n_rows; ++row) + { + const eT val = P.at(row,col); + + (*out_mem) = (val < min_val) ? min_val : ((val > max_val) ? max_val : val); + + out_mem++; } } } @@ -148,17 +159,77 @@ op_clamp::apply(Cube& out, const mtOpCube P(in.m); + typedef typename T1::elem_type eT; - if((is_Cube::stored_type>::value) || P.is_alias(out)) + const eT min_val = in.aux; + const eT max_val = in.aux_out_eT; + + arma_debug_check( (min_val > max_val), "clamp(): min_val must be less than max_val" ); + + if(is_Cube::value) { - const unwrap_cube::stored_type> U(P.Q); + const unwrap_cube U(in.m); - op_clamp::apply_direct(out, U.M, in.aux, in.aux_out_eT); + op_clamp::apply_direct(out, U.M, min_val, max_val); } else { - op_clamp::apply_proxy_noalias(out, P, in.aux, in.aux_out_eT); + const ProxyCube P(in.m); + + if(P.is_alias(out)) + { + Cube tmp; + + op_clamp::apply_proxy_noalias(tmp, P, min_val, max_val); + + out.steal_mem(tmp); + } + else + { + op_clamp::apply_proxy_noalias(out, P, min_val, max_val); + } + } + } + + + +template +inline +void +op_clamp::apply_direct(Cube& out, const Cube& X, const eT min_val, const eT max_val) + { + arma_extra_debug_sigprint(); + + if(&out != &X) + { + out.set_size(X.n_rows, X.n_cols, X.n_slices); + + const uword N = out.n_elem; + + const eT* X_mem = X.memptr(); + eT* out_mem = out.memptr(); + + for(uword i=0; i max_val) ? max_val : val); + } + } + else + { + arma_extra_debug_print("op_clamp::apply_direct(): inplace operation"); + + const uword N = out.n_elem; + + eT* out_mem = out.memptr(); + + for(uword i=0; i max_val) ? max_val : out_val ); + } } } @@ -187,73 +258,24 @@ op_clamp::apply_proxy_noalias(Cube& out, const ProxyCube typename ProxyCube::ea_type A = P.get_ea(); - uword j; - for(j=1; j max_val) ? max_val : val_i); - val_j = (val_j < min_val) ? min_val : ((val_j > max_val) ? max_val : val_j); - - (*out_mem) = val_i; out_mem++; - (*out_mem) = val_j; out_mem++; - } - - const uword i = j-1; - - if(i < N) - { - eT val_i = A[i]; - - val_i = (val_i < min_val) ? min_val : ((val_i > max_val) ? max_val : val_i); - - (*out_mem) = val_i; - } - } - else - { - for(uword k=0; k < n_slices; ++k) - for(uword j=0; j < n_cols; ++j) - for(uword i=0; i < n_rows; ++i) - { - eT val = P.at(i,j,k); - - val = (val < min_val) ? min_val : ((val > max_val) ? max_val : val); - - (*out_mem) = val; out_mem++; - } - } - } - - - -template -inline -void -op_clamp::apply_direct(Cube& out, const Cube& X, const eT min_val, const eT max_val) - { - arma_extra_debug_sigprint(); - - if(&out != &X) - { - const ProxyCube< Cube > P(X); - - op_clamp::apply_proxy_noalias(out, P, min_val, max_val); - } - else - { - arma_extra_debug_print("inplace operation"); - - const uword N = out.n_elem; - - eT* out_mem = out.memptr(); - for(uword i=0; i max_val) ? max_val : out_val ); + out_mem[i] = (val < min_val) ? min_val : ((val > max_val) ? max_val : val); + } + } + else + { + for(uword s=0; s < n_slices; ++s) + for(uword c=0; c < n_cols; ++c) + for(uword r=0; r < n_rows; ++r) + { + const eT val = P.at(r,c,s); + + (*out_mem) = (val < min_val) ? min_val : ((val > max_val) ? max_val : val); + + out_mem++; } } }