diff --git a/include/armadillo_bits/fn_norm.hpp b/include/armadillo_bits/fn_norm.hpp index c7546958..c4b3d94b 100644 --- a/include/armadillo_bits/fn_norm.hpp +++ b/include/armadillo_bits/fn_norm.hpp @@ -43,42 +43,24 @@ norm if(is_vec) { - switch(k) - { - case 1: - return op_norm::vec_norm_1(P); - break; - - case 2: - return op_norm::vec_norm_2(P); - break; - - default: - { - arma_debug_check( (k == 0), "norm(): k must be greater than zero" ); - return op_norm::vec_norm_k(P, int(k)); - } - } + if(k == uword(1)) { return op_norm::vec_norm_1(P); } + if(k == uword(2)) { return op_norm::vec_norm_2(P); } + + arma_debug_check( (k == 0), "norm(): k must be greater than zero" ); + + return op_norm::vec_norm_k(P, int(k)); } else { - switch(k) - { - case 1: - return op_norm::mat_norm_1(P); - break; + const quasi_unwrap::stored_type> U(P.Q); + + if(k == uword(1)) { return op_norm::mat_norm_1(U.M); } + if(k == uword(2)) { return op_norm::mat_norm_2(U.M); } - case 2: - return op_norm::mat_norm_2(P); - break; - - default: - arma_stop_logic_error("norm(): unsupported matrix norm type"); - return T(0); - } + arma_stop_logic_error("norm(): unsupported matrix norm type"); } - return T(0); // prevent erroneous compiler warnings + return T(0); } @@ -108,45 +90,30 @@ norm if(is_vec) { - if( (sig == 'i') || (sig == 'I') || (sig == '+') ) // max norm - { - return op_norm::vec_norm_max(P); - } - else - if(sig == '-') // min norm - { - return op_norm::vec_norm_min(P); - } - else - if( (sig == 'f') || (sig == 'F') ) - { - return op_norm::vec_norm_2(P); - } - else - { - arma_stop_logic_error("norm(): unsupported vector norm type"); - return T(0); - } + if( (sig == 'i') || (sig == 'I') || (sig == '+') ) { return op_norm::vec_norm_max(P); } + if( (sig == '-') ) { return op_norm::vec_norm_min(P); } + if( (sig == 'f') || (sig == 'F') ) { return op_norm::vec_norm_2(P); } + + arma_stop_logic_error("norm(): unsupported vector norm type"); } else { if( (sig == 'i') || (sig == 'I') || (sig == '+') ) // inf norm { - return op_norm::mat_norm_inf(P); + const quasi_unwrap::stored_type> U(P.Q); + + return op_norm::mat_norm_inf(U.M); } else if( (sig == 'f') || (sig == 'F') ) { return op_norm::vec_norm_2(P); } - else - { - arma_stop_logic_error("norm(): unsupported matrix norm type"); - return T(0); - } + + arma_stop_logic_error("norm(): unsupported matrix norm type"); } - return T(0); // prevent erroneous compiler warnings + return T(0); } @@ -188,42 +155,22 @@ norm const Proxy< Col > P_fake_vector(fake_vector); - switch(k) - { - case 1: - return op_norm::vec_norm_1(P_fake_vector); - break; - - case 2: - return op_norm::vec_norm_2(P_fake_vector); - break; - - default: - { - arma_debug_check( (k == 0), "norm(): k must be greater than zero" ); - return op_norm::vec_norm_k(P_fake_vector, int(k)); - } - } + if(k == uword(1)) { return op_norm::vec_norm_1(P_fake_vector); } + if(k == uword(2)) { return op_norm::vec_norm_2(P_fake_vector); } + + arma_debug_check( (k == 0), "norm(): k must be greater than zero" ); + + return op_norm::vec_norm_k(P_fake_vector, int(k)); } else { - switch(k) - { - case 1: - return op_norm::mat_norm_1(P); - break; - - case 2: - return op_norm::mat_norm_2(P); - break; - - default: - arma_stop_logic_error("norm(): unsupported or unimplemented norm type for sparse matrices"); - return T(0); - } + if(k == uword(1)) { return op_norm::mat_norm_1(P); } + if(k == uword(2)) { return op_norm::mat_norm_2(P); } + + arma_stop_logic_error("norm(): unsupported or unimplemented norm type for sparse matrices"); } - return T(0); // prevent erroneous compiler warnings + return T(0); } @@ -273,25 +220,15 @@ norm { const T val = op_norm::vec_norm_min(P_fake_vector); - if( P.get_n_nonzero() < P.get_n_elem() ) - { - return (std::min)(T(0), val); - } - else - { - return val; - } + return (P.get_n_nonzero() < P.get_n_elem()) ? T((std::min)(T(0), val)) : T(val); } else if( (sig == 'f') || (sig == 'F') ) { return op_norm::vec_norm_2(P_fake_vector); } - else - { - arma_stop_logic_error("norm(): unsupported vector norm type"); - return T(0); - } + + arma_stop_logic_error("norm(): unsupported vector norm type"); } else { @@ -304,14 +241,11 @@ norm { return op_norm::vec_norm_2(P_fake_vector); } - else - { - arma_stop_logic_error("norm(): unsupported matrix norm type"); - return T(0); - } + + arma_stop_logic_error("norm(): unsupported matrix norm type"); } - return T(0); // prevent erroneous compiler warnings + return T(0); } diff --git a/include/armadillo_bits/op_norm_bones.hpp b/include/armadillo_bits/op_norm_bones.hpp index 1e8e901b..a131bdd4 100644 --- a/include/armadillo_bits/op_norm_bones.hpp +++ b/include/armadillo_bits/op_norm_bones.hpp @@ -41,10 +41,10 @@ class op_norm template arma_hot inline static typename T1::pod_type vec_norm_max(const Proxy& P); template arma_hot inline static typename T1::pod_type vec_norm_min(const Proxy& P); - template inline static typename T1::pod_type mat_norm_1(const Proxy& P); - template inline static typename T1::pod_type mat_norm_2(const Proxy& P); + template inline static typename get_pod_type::result mat_norm_1(const Mat& X); + template inline static typename get_pod_type::result mat_norm_2(const Mat& X); - template inline static typename T1::pod_type mat_norm_inf(const Proxy& P); + template inline static typename get_pod_type::result mat_norm_inf(const Mat& X); // norms for sparse matrices diff --git a/include/armadillo_bits/op_norm_meat.hpp b/include/armadillo_bits/op_norm_meat.hpp index 5b2bf482..2a15c782 100644 --- a/include/armadillo_bits/op_norm_meat.hpp +++ b/include/armadillo_bits/op_norm_meat.hpp @@ -867,49 +867,47 @@ op_norm::vec_norm_min(const Proxy& P) -template +template inline -typename T1::pod_type -op_norm::mat_norm_1(const Proxy& P) +typename get_pod_type::result +op_norm::mat_norm_1(const Mat& X) { arma_extra_debug_sigprint(); // TODO: this can be sped up with a dedicated implementation - return as_scalar( max( sum(abs(P.Q), 0), 1) ); + return as_scalar( max( sum(abs(X), 0), 1) ); } -template +template inline -typename T1::pod_type -op_norm::mat_norm_2(const Proxy& P) +typename get_pod_type::result +op_norm::mat_norm_2(const Mat& X) { arma_extra_debug_sigprint(); - typedef typename T1::pod_type T; + typedef typename get_pod_type::result T; - const quasi_unwrap::stored_type> U(P.Q); - - if(U.M.is_finite() == false) { arma_debug_warn("norm(): given matrix has non-finite elements"); } + if(X.is_finite() == false) { arma_debug_warn("norm(): given matrix has non-finite elements"); } Col S; - svd(S, U.M); + svd(S, X); return (S.n_elem > 0) ? S[0] : T(0); } -template +template inline -typename T1::pod_type -op_norm::mat_norm_inf(const Proxy& P) +typename get_pod_type::result +op_norm::mat_norm_inf(const Mat& X) { arma_extra_debug_sigprint(); // TODO: this can be sped up with a dedicated implementation - return as_scalar( max( sum(abs(P.Q), 1), 0) ); + return as_scalar( max( sum(abs(X), 1), 0) ); }