Chris@49: // Copyright (C) 2009-2012 NICTA (www.nicta.com.au) Chris@49: // Copyright (C) 2009-2012 Conrad Sanderson Chris@49: // Chris@49: // This Source Code Form is subject to the terms of the Mozilla Public Chris@49: // License, v. 2.0. If a copy of the MPL was not distributed with this Chris@49: // file, You can obtain one at http://mozilla.org/MPL/2.0/. Chris@49: Chris@49: Chris@49: //! \addtogroup op_var Chris@49: //! @{ Chris@49: Chris@49: Chris@49: //! \brief Chris@49: //! For each row or for each column, find the variance. Chris@49: //! The result is stored in a dense matrix that has either one column or one row. Chris@49: //! The dimension, for which the variances are found, is set via the var() function. Chris@49: template Chris@49: inline Chris@49: void Chris@49: op_var::apply(Mat& out, const mtOp& in) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: typedef typename T1::elem_type in_eT; Chris@49: typedef typename T1::pod_type out_eT; Chris@49: Chris@49: const unwrap_check_mixed tmp(in.m, out); Chris@49: const Mat& X = tmp.M; Chris@49: Chris@49: const uword norm_type = in.aux_uword_a; Chris@49: const uword dim = in.aux_uword_b; Chris@49: Chris@49: arma_debug_check( (norm_type > 1), "var(): incorrect usage. norm_type must be 0 or 1"); Chris@49: arma_debug_check( (dim > 1), "var(): incorrect usage. dim must be 0 or 1" ); Chris@49: Chris@49: const uword X_n_rows = X.n_rows; Chris@49: const uword X_n_cols = X.n_cols; Chris@49: Chris@49: if(dim == 0) Chris@49: { Chris@49: arma_extra_debug_print("op_var::apply(), dim = 0"); Chris@49: Chris@49: arma_debug_check( (X_n_rows == 0), "var(): given object has zero rows" ); Chris@49: Chris@49: out.set_size(1, X_n_cols); Chris@49: Chris@49: out_eT* out_mem = out.memptr(); Chris@49: Chris@49: for(uword col=0; col dat(X_n_cols); Chris@49: Chris@49: in_eT* dat_mem = dat.memptr(); Chris@49: out_eT* out_mem = out.memptr(); Chris@49: Chris@49: for(uword row=0; row Chris@49: inline Chris@49: typename T1::pod_type Chris@49: op_var::var_vec(const Base& X, const uword norm_type) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: typedef typename T1::elem_type eT; Chris@49: Chris@49: arma_debug_check( (norm_type > 1), "var(): incorrect usage. norm_type must be 0 or 1"); Chris@49: Chris@49: const Proxy P(X.get_ref()); Chris@49: Chris@49: const podarray tmp(P); Chris@49: Chris@49: return op_var::direct_var(tmp.memptr(), tmp.n_elem, norm_type); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: typename get_pod_type::result Chris@49: op_var::var_vec(const subview_col& X, const uword norm_type) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check( (norm_type > 1), "var(): incorrect usage. norm_type must be 0 or 1"); Chris@49: Chris@49: return op_var::direct_var(X.colptr(0), X.n_rows, norm_type); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: typename get_pod_type::result Chris@49: op_var::var_vec(const subview_row& X, const uword norm_type) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check( (norm_type > 1), "var(): incorrect usage. norm_type must be 0 or 1"); Chris@49: Chris@49: const Mat& A = X.m; Chris@49: Chris@49: const uword start_row = X.aux_row1; Chris@49: const uword start_col = X.aux_col1; Chris@49: Chris@49: const uword end_col_p1 = start_col + X.n_cols; Chris@49: Chris@49: podarray tmp(X.n_elem); Chris@49: eT* tmp_mem = tmp.memptr(); Chris@49: Chris@49: for(uword i=0, col=start_col; col < end_col_p1; ++col, ++i) Chris@49: { Chris@49: tmp_mem[i] = A.at(start_row, col); Chris@49: } Chris@49: Chris@49: return op_var::direct_var(tmp.memptr(), tmp.n_elem, norm_type); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! find the variance of an array Chris@49: template Chris@49: inline Chris@49: eT Chris@49: op_var::direct_var(const eT* const X, const uword n_elem, const uword norm_type) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: if(n_elem >= 2) Chris@49: { Chris@49: const eT acc1 = op_mean::direct_mean(X, n_elem); Chris@49: Chris@49: eT acc2 = eT(0); Chris@49: eT acc3 = eT(0); Chris@49: Chris@49: uword i,j; Chris@49: Chris@49: for(i=0, j=1; j Chris@49: inline Chris@49: eT Chris@49: op_var::direct_var_robust(const eT* const X, const uword n_elem, const uword norm_type) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: if(n_elem > 1) Chris@49: { Chris@49: eT r_mean = X[0]; Chris@49: eT r_var = eT(0); Chris@49: Chris@49: for(uword i=1; i Chris@49: inline Chris@49: T Chris@49: op_var::direct_var(const std::complex* const X, const uword n_elem, const uword norm_type) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: typedef typename std::complex eT; Chris@49: Chris@49: if(n_elem >= 2) Chris@49: { Chris@49: const eT acc1 = op_mean::direct_mean(X, n_elem); Chris@49: Chris@49: T acc2 = T(0); Chris@49: eT acc3 = eT(0); Chris@49: Chris@49: for(uword i=0; i Chris@49: inline Chris@49: T Chris@49: op_var::direct_var_robust(const std::complex* const X, const uword n_elem, const uword norm_type) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: typedef typename std::complex eT; Chris@49: Chris@49: if(n_elem > 1) Chris@49: { Chris@49: eT r_mean = X[0]; Chris@49: T r_var = T(0); Chris@49: Chris@49: for(uword i=1; i