simplifications

This commit is contained in:
conrad
2021-05-03 23:40:03 +10:00
parent 864383539e
commit 8e41215cdb
3 changed files with 163 additions and 145 deletions
-4
View File
@@ -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<typename T1::elem_type, T1, op_clamp>(mtOp_dual_aux_indicator(), X, min_val, max_val);
}
@@ -66,8 +64,6 @@ clamp(const BaseCube<typename T1::elem_type,T1>& 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<typename T1::elem_type, T1, op_clamp>(mtOpCube_dual_aux_indicator(), X.get_ref(), min_val, max_val);
}
+4 -4
View File
@@ -29,17 +29,17 @@ class op_clamp
template<typename T1> inline static void apply(Mat<typename T1::elem_type>& out, const mtOp<typename T1::elem_type, T1, op_clamp>& in);
template<typename T1> inline static void apply_proxy_noalias(Mat<typename T1::elem_type>& out, const Proxy<T1>& P, const typename T1::elem_type min_val, const typename T1::elem_type max_val);
template<typename eT> inline static void apply_direct(Mat<eT>& out, const Mat<eT>& X, const eT min_val, const eT max_val);
template<typename T1> inline static void apply_proxy_noalias(Mat<typename T1::elem_type>& out, const Proxy<T1>& P, const typename T1::elem_type min_val, const typename T1::elem_type max_val);
// cubes
template<typename T1> inline static void apply(Cube<typename T1::elem_type>& out, const mtOpCube<typename T1::elem_type, T1, op_clamp>& in);
template<typename T1> inline static void apply_proxy_noalias(Cube<typename T1::elem_type>& out, const ProxyCube<T1>& P, const typename T1::elem_type min_val, const typename T1::elem_type max_val);
template<typename eT> inline static void apply_direct(Cube<eT>& out, const Cube<eT>& X, const eT min_val, const eT max_val);
template<typename T1> inline static void apply_proxy_noalias(Cube<typename T1::elem_type>& out, const ProxyCube<T1>& P, const typename T1::elem_type min_val, const typename T1::elem_type max_val);
};
+159 -137
View File
@@ -27,17 +27,77 @@ op_clamp::apply(Mat<typename T1::elem_type>& out, const mtOp<typename T1::elem_t
{
arma_extra_debug_sigprint();
const Proxy<T1> P(in.m);
typedef typename T1::elem_type eT;
if(is_Mat<typename Proxy<T1>::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<T1>::value)
{
const unwrap<typename Proxy<T1>::stored_type> U(P.Q);
const unwrap<T1> 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<T1> P(in.m);
if(P.is_alias(out))
{
Mat<eT> 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<typename eT>
inline
void
op_clamp::apply_direct(Mat<eT>& out, const Mat<eT>& 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<N; ++i)
{
const eT val = X_mem[i];
out_mem[i] = (val < min_val) ? min_val : ((val > 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<N; ++i)
{
eT& out_val = out_mem[i];
out_val = (out_val < min_val) ? min_val : ((out_val > max_val) ? max_val : out_val);
}
}
}
@@ -65,72 +125,23 @@ op_clamp::apply_proxy_noalias(Mat<typename T1::elem_type>& out, const Proxy<T1>&
typename Proxy<T1>::ea_type A = P.get_ea();
uword j;
for(j=1; j<N; j+=2)
{
eT val_i = A[j-1];
eT val_j = A[j ];
val_i = (val_i < min_val) ? min_val : ((val_i > 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<n_cols; ++col)
for(uword row=0; row<n_rows; ++row)
{
eT val = P.at(row,col);
val = (val < min_val) ? min_val : ((val > max_val) ? max_val : val);
(*out_mem) = val; out_mem++;
}
}
}
template<typename eT>
inline
void
op_clamp::apply_direct(Mat<eT>& out, const Mat<eT>& X, const eT min_val, const eT max_val)
{
arma_extra_debug_sigprint();
if(&out != &X)
{
const Proxy< Mat<eT> > 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<N; ++i)
{
eT& out_val = out_mem[i];
const eT val = A[i];
out_val = (out_val < min_val) ? min_val : ( (out_val > 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<typename T1::elem_type>& out, const mtOpCube<typename T1::e
{
arma_extra_debug_sigprint();
const ProxyCube<T1> P(in.m);
typedef typename T1::elem_type eT;
if((is_Cube<typename ProxyCube<T1>::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<T1>::value)
{
const unwrap_cube<typename ProxyCube<T1>::stored_type> U(P.Q);
const unwrap_cube<T1> 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<T1> P(in.m);
if(P.is_alias(out))
{
Cube<eT> 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<typename eT>
inline
void
op_clamp::apply_direct(Cube<eT>& out, const Cube<eT>& 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<N; ++i)
{
const eT val = X_mem[i];
out_mem[i] = (val < min_val) ? min_val : ((val > 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<N; ++i)
{
eT& out_val = out_mem[i];
out_val = (out_val < min_val) ? min_val : ( (out_val > max_val) ? max_val : out_val );
}
}
}
@@ -187,73 +258,24 @@ op_clamp::apply_proxy_noalias(Cube<typename T1::elem_type>& out, const ProxyCube
typename ProxyCube<T1>::ea_type A = P.get_ea();
uword j;
for(j=1; j<N; j+=2)
{
eT val_i = A[j-1];
eT val_j = A[j ];
val_i = (val_i < min_val) ? min_val : ((val_i > 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<typename eT>
inline
void
op_clamp::apply_direct(Cube<eT>& out, const Cube<eT>& X, const eT min_val, const eT max_val)
{
arma_extra_debug_sigprint();
if(&out != &X)
{
const ProxyCube< Cube<eT> > 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<N; ++i)
{
eT& out_val = out_mem[i];
const eT val = A[i];
out_val = (out_val < min_val) ? min_val : ( (out_val > 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++;
}
}
}