max@0: // Copyright (C) 2008-2011 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2008-2011 Conrad Sanderson max@0: // max@0: // This file is part of the Armadillo C++ library. max@0: // It is provided without any warranty of fitness max@0: // for any purpose. You can redistribute this file max@0: // and/or modify it under the terms of the GNU max@0: // Lesser General Public License (LGPL) as published max@0: // by the Free Software Foundation, either version 3 max@0: // of the License or (at your option) any later version. max@0: // (see http://www.opensource.org/licenses for more info) max@0: max@0: max@0: //! \addtogroup fn_norm max@0: //! @{ max@0: max@0: max@0: max@0: template max@0: arma_hot max@0: inline max@0: typename T1::pod_type max@0: arma_vec_norm_1(const Proxy& A) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename T1::pod_type T; max@0: max@0: T acc = T(0); max@0: max@0: if(Proxy::prefer_at_accessor == false) max@0: { max@0: typename Proxy::ea_type P = A.get_ea(); max@0: max@0: const uword N = A.get_n_elem(); max@0: max@0: uword i,j; max@0: max@0: for(i=0, j=1; j max@0: arma_hot max@0: inline max@0: typename T1::pod_type max@0: arma_vec_norm_2(const Proxy& A, const typename arma_not_cx::result* junk = 0) max@0: { max@0: arma_extra_debug_sigprint(); max@0: arma_ignore(junk); max@0: max@0: typedef typename T1::pod_type T; max@0: max@0: T acc = T(0); max@0: max@0: if(Proxy::prefer_at_accessor == false) max@0: { max@0: typename Proxy::ea_type P = A.get_ea(); max@0: max@0: const uword N = A.get_n_elem(); max@0: max@0: uword i,j; max@0: max@0: for(i=0, j=1; j max@0: arma_hot max@0: inline max@0: typename T1::pod_type max@0: arma_vec_norm_2(const Proxy& A, const typename arma_cx_only::result* junk = 0) max@0: { max@0: arma_extra_debug_sigprint(); max@0: arma_ignore(junk); max@0: max@0: typedef typename T1::pod_type T; max@0: max@0: T acc = T(0); max@0: max@0: if(Proxy::prefer_at_accessor == false) max@0: { max@0: typename Proxy::ea_type P = A.get_ea(); max@0: max@0: const uword N = A.get_n_elem(); max@0: max@0: for(uword i=0; i max@0: arma_hot max@0: inline max@0: typename T1::pod_type max@0: arma_vec_norm_k(const Proxy& A, const int k) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename T1::pod_type T; max@0: max@0: T acc = T(0); max@0: max@0: if(Proxy::prefer_at_accessor == false) max@0: { max@0: typename Proxy::ea_type P = A.get_ea(); max@0: max@0: const uword N = A.get_n_elem(); max@0: max@0: uword i,j; max@0: max@0: for(i=0, j=1; j max@0: arma_hot max@0: inline max@0: typename T1::pod_type max@0: arma_vec_norm_max(const Proxy& A) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename T1::pod_type T; max@0: typedef typename Proxy::ea_type ea_type; max@0: max@0: ea_type P = A.get_ea(); max@0: const uword N = A.get_n_elem(); max@0: max@0: T max_val = (N != 1) ? priv::most_neg() : std::abs(P[0]); max@0: max@0: uword i,j; max@0: max@0: for(i=0, j=1; j max@0: arma_hot max@0: inline max@0: typename T1::pod_type max@0: arma_vec_norm_min(const Proxy& A) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename T1::pod_type T; max@0: typedef typename Proxy::ea_type ea_type; max@0: max@0: ea_type P = A.get_ea(); max@0: const uword N = A.get_n_elem(); max@0: max@0: T min_val = (N != 1) ? priv::most_pos() : std::abs(P[0]); max@0: max@0: uword i,j; max@0: max@0: for(i=0, j=1; j tmp_i) { min_val = tmp_i; } max@0: if(min_val > tmp_j) { min_val = tmp_j; } max@0: } max@0: max@0: if(i < N) max@0: { max@0: const T tmp_i = std::abs(P[i]); max@0: max@0: if(min_val > tmp_i) { min_val = tmp_i; } max@0: } max@0: max@0: return min_val; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: typename T1::pod_type max@0: arma_mat_norm_1(const Proxy& A) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename T1::elem_type eT; max@0: typedef typename T1::pod_type T; max@0: max@0: const unwrap::stored_type> tmp(A.Q); max@0: const Mat& X = tmp.M; max@0: max@0: // TODO: this can be sped up with a dedicated implementation max@0: return as_scalar( max( sum(abs(X)), 1) ); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: typename T1::pod_type max@0: arma_mat_norm_2(const Proxy& A) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename T1::elem_type eT; max@0: typedef typename T1::pod_type T; max@0: max@0: const unwrap::stored_type> tmp(A.Q); max@0: const Mat& X = tmp.M; max@0: max@0: Col S; max@0: svd(S, X); max@0: max@0: return (S.n_elem > 0) ? max(S) : T(0); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: typename T1::pod_type max@0: arma_mat_norm_inf(const Proxy& A) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename T1::elem_type eT; max@0: typedef typename T1::pod_type T; max@0: max@0: const unwrap::stored_type> tmp(A.Q); max@0: const Mat& X = tmp.M; max@0: max@0: // TODO: this can be sped up with a dedicated implementation max@0: return as_scalar( max( sum(abs(X),1) ) ); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: arma_warn_unused max@0: typename T1::pod_type max@0: norm max@0: ( max@0: const Base& X, max@0: const uword k, max@0: const typename arma_float_or_cx_only::result* junk = 0 max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: arma_ignore(junk); max@0: max@0: typedef typename T1::elem_type eT; max@0: typedef typename T1::pod_type T; max@0: max@0: const Proxy A(X.get_ref()); max@0: max@0: if(A.get_n_elem() == 0) max@0: { max@0: return T(0); max@0: } max@0: max@0: const bool is_vec = (A.get_n_rows() == 1) || (A.get_n_cols() == 1); max@0: max@0: if(is_vec == true) max@0: { max@0: switch(k) max@0: { max@0: case 1: max@0: return arma_vec_norm_1(A); max@0: break; max@0: max@0: case 2: max@0: return arma_vec_norm_2(A); max@0: break; max@0: max@0: default: max@0: { max@0: arma_debug_check( (k == 0), "norm(): k must be greater than zero" ); max@0: return arma_vec_norm_k(A, int(k)); max@0: } max@0: } max@0: } max@0: else max@0: { max@0: switch(k) max@0: { max@0: case 1: max@0: return arma_mat_norm_1(A); max@0: break; max@0: max@0: case 2: max@0: return arma_mat_norm_2(A); max@0: break; max@0: max@0: default: max@0: arma_stop("norm(): unsupported matrix norm type"); max@0: return T(0); max@0: } max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: arma_warn_unused max@0: typename T1::pod_type max@0: norm max@0: ( max@0: const Base& X, max@0: const char* method, max@0: const typename arma_float_or_cx_only::result* junk = 0 max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: arma_ignore(junk); max@0: max@0: typedef typename T1::elem_type eT; max@0: typedef typename T1::pod_type T; max@0: max@0: const Proxy A(X.get_ref()); max@0: max@0: if(A.get_n_elem() == 0) max@0: { max@0: return T(0); max@0: } max@0: max@0: const char sig = method[0]; max@0: const bool is_vec = (A.get_n_rows() == 1) || (A.get_n_cols() == 1); max@0: max@0: if(is_vec == true) max@0: { max@0: if( (sig == 'i') || (sig == 'I') || (sig == '+') ) // max norm max@0: { max@0: return arma_vec_norm_max(A); max@0: } max@0: else max@0: if(sig == '-') // min norm max@0: { max@0: return arma_vec_norm_min(A); max@0: } max@0: else max@0: if( (sig == 'f') || (sig == 'F') ) max@0: { max@0: return arma_vec_norm_2(A); max@0: } max@0: else max@0: { max@0: arma_stop("norm(): unsupported vector norm type"); max@0: return T(0); max@0: } max@0: } max@0: else max@0: { max@0: if( (sig == 'i') || (sig == 'I') || (sig == '+') ) // inf norm max@0: { max@0: return arma_mat_norm_inf(A); max@0: } max@0: else max@0: if( (sig == 'f') || (sig == 'F') ) max@0: { max@0: return arma_vec_norm_2(A); max@0: } max@0: else max@0: { max@0: arma_stop("norm(): unsupported matrix norm type"); max@0: return T(0); max@0: } max@0: } max@0: } max@0: max@0: max@0: max@0: //! @}