max@0: // Copyright (C) 2010-2011 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2010-2011 Conrad Sanderson max@0: // Copyright (C) 2010 Dimitrios Bouzas max@0: // Copyright (C) 2011 Stanislav Funiak 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 op_princomp max@0: //! @{ max@0: max@0: max@0: max@0: //! \brief max@0: //! principal component analysis -- 4 arguments version max@0: //! computation is done via singular value decomposition max@0: //! coeff_out -> principal component coefficients max@0: //! score_out -> projected samples max@0: //! latent_out -> eigenvalues of principal vectors max@0: //! tsquared_out -> Hotelling's T^2 statistic max@0: template max@0: inline max@0: bool max@0: op_princomp::direct_princomp max@0: ( max@0: Mat& coeff_out, max@0: Mat& score_out, max@0: Col& latent_out, max@0: Col& tsquared_out, max@0: const Mat& in max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const uword n_rows = in.n_rows; max@0: const uword n_cols = in.n_cols; max@0: max@0: if(n_rows > 1) // more than one sample max@0: { max@0: // subtract the mean - use score_out as temporary matrix max@0: score_out = in - repmat(mean(in), n_rows, 1); max@0: max@0: // singular value decomposition max@0: Mat U; max@0: Col s; max@0: max@0: const bool svd_ok = svd(U,s,coeff_out,score_out); max@0: max@0: if(svd_ok == false) max@0: { max@0: return false; max@0: } max@0: max@0: max@0: //U.reset(); // TODO: do we need this ? U will get automatically deleted anyway max@0: max@0: // normalize the eigenvalues max@0: s /= std::sqrt( double(n_rows - 1) ); max@0: max@0: // project the samples to the principals max@0: score_out *= coeff_out; max@0: max@0: if(n_rows <= n_cols) // number of samples is less than their dimensionality max@0: { max@0: score_out.cols(n_rows-1,n_cols-1).zeros(); max@0: max@0: //Col s_tmp = zeros< Col >(n_cols); max@0: Col s_tmp(n_cols); max@0: s_tmp.zeros(); max@0: max@0: s_tmp.rows(0,n_rows-2) = s.rows(0,n_rows-2); max@0: s = s_tmp; max@0: max@0: // compute the Hotelling's T-squared max@0: s_tmp.rows(0,n_rows-2) = eT(1) / s_tmp.rows(0,n_rows-2); max@0: max@0: const Mat S = score_out * diagmat(Col(s_tmp)); max@0: tsquared_out = sum(S%S,1); max@0: } max@0: else max@0: { max@0: // compute the Hotelling's T-squared max@0: const Mat S = score_out * diagmat(Col( eT(1) / s)); max@0: tsquared_out = sum(S%S,1); max@0: } max@0: max@0: // compute the eigenvalues of the principal vectors max@0: latent_out = s%s; max@0: } max@0: else // 0 or 1 samples max@0: { max@0: coeff_out.eye(n_cols, n_cols); max@0: max@0: score_out.copy_size(in); max@0: score_out.zeros(); max@0: max@0: latent_out.set_size(n_cols); max@0: latent_out.zeros(); max@0: max@0: tsquared_out.set_size(n_rows); max@0: tsquared_out.zeros(); max@0: } max@0: max@0: return true; max@0: } max@0: max@0: max@0: max@0: //! \brief max@0: //! principal component analysis -- 3 arguments version max@0: //! computation is done via singular value decomposition max@0: //! coeff_out -> principal component coefficients max@0: //! score_out -> projected samples max@0: //! latent_out -> eigenvalues of principal vectors max@0: template max@0: inline max@0: bool max@0: op_princomp::direct_princomp max@0: ( max@0: Mat& coeff_out, max@0: Mat& score_out, max@0: Col& latent_out, max@0: const Mat& in max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const uword n_rows = in.n_rows; max@0: const uword n_cols = in.n_cols; max@0: max@0: if(n_rows > 1) // more than one sample max@0: { max@0: // subtract the mean - use score_out as temporary matrix max@0: score_out = in - repmat(mean(in), n_rows, 1); max@0: max@0: // singular value decomposition max@0: Mat U; max@0: Col s; max@0: max@0: const bool svd_ok = svd(U,s,coeff_out,score_out); max@0: max@0: if(svd_ok == false) max@0: { max@0: return false; max@0: } max@0: max@0: max@0: // U.reset(); max@0: max@0: // normalize the eigenvalues max@0: s /= std::sqrt( double(n_rows - 1) ); max@0: max@0: // project the samples to the principals max@0: score_out *= coeff_out; max@0: max@0: if(n_rows <= n_cols) // number of samples is less than their dimensionality max@0: { max@0: score_out.cols(n_rows-1,n_cols-1).zeros(); max@0: max@0: Col s_tmp = zeros< Col >(n_cols); max@0: s_tmp.rows(0,n_rows-2) = s.rows(0,n_rows-2); max@0: s = s_tmp; max@0: } max@0: max@0: // compute the eigenvalues of the principal vectors max@0: latent_out = s%s; max@0: max@0: } max@0: else // 0 or 1 samples max@0: { max@0: coeff_out.eye(n_cols, n_cols); max@0: max@0: score_out.copy_size(in); max@0: score_out.zeros(); max@0: max@0: latent_out.set_size(n_cols); max@0: latent_out.zeros(); max@0: } max@0: max@0: return true; max@0: } max@0: max@0: max@0: max@0: //! \brief max@0: //! principal component analysis -- 2 arguments version max@0: //! computation is done via singular value decomposition max@0: //! coeff_out -> principal component coefficients max@0: //! score_out -> projected samples max@0: template max@0: inline max@0: bool max@0: op_princomp::direct_princomp max@0: ( max@0: Mat& coeff_out, max@0: Mat& score_out, max@0: const Mat& in max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const uword n_rows = in.n_rows; max@0: const uword n_cols = in.n_cols; max@0: max@0: if(n_rows > 1) // more than one sample max@0: { max@0: // subtract the mean - use score_out as temporary matrix max@0: score_out = in - repmat(mean(in), n_rows, 1); max@0: max@0: // singular value decomposition max@0: Mat U; max@0: Col s; max@0: max@0: const bool svd_ok = svd(U,s,coeff_out,score_out); max@0: max@0: if(svd_ok == false) max@0: { max@0: return false; max@0: } max@0: max@0: // U.reset(); max@0: max@0: // normalize the eigenvalues max@0: s /= std::sqrt( double(n_rows - 1) ); max@0: max@0: // project the samples to the principals max@0: score_out *= coeff_out; max@0: max@0: if(n_rows <= n_cols) // number of samples is less than their dimensionality max@0: { max@0: score_out.cols(n_rows-1,n_cols-1).zeros(); max@0: max@0: Col s_tmp = zeros< Col >(n_cols); max@0: s_tmp.rows(0,n_rows-2) = s.rows(0,n_rows-2); max@0: s = s_tmp; max@0: } max@0: } max@0: else // 0 or 1 samples max@0: { max@0: coeff_out.eye(n_cols, n_cols); max@0: score_out.copy_size(in); max@0: score_out.zeros(); max@0: } max@0: max@0: return true; max@0: } max@0: max@0: max@0: max@0: //! \brief max@0: //! principal component analysis -- 1 argument version max@0: //! computation is done via singular value decomposition max@0: //! coeff_out -> principal component coefficients max@0: template max@0: inline max@0: bool max@0: op_princomp::direct_princomp max@0: ( max@0: Mat& coeff_out, max@0: const Mat& in max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: if(in.n_elem != 0) max@0: { max@0: // singular value decomposition max@0: Mat U; max@0: Col s; max@0: max@0: const Mat tmp = in - repmat(mean(in), in.n_rows, 1); max@0: max@0: const bool svd_ok = svd(U,s,coeff_out, tmp); max@0: max@0: if(svd_ok == false) max@0: { max@0: return false; max@0: } max@0: } max@0: else max@0: { max@0: coeff_out.eye(in.n_cols, in.n_cols); max@0: } max@0: max@0: return true; max@0: } max@0: max@0: max@0: max@0: //! \brief max@0: //! principal component analysis -- 4 arguments complex version max@0: //! computation is done via singular value decomposition max@0: //! coeff_out -> principal component coefficients max@0: //! score_out -> projected samples max@0: //! latent_out -> eigenvalues of principal vectors max@0: //! tsquared_out -> Hotelling's T^2 statistic max@0: template max@0: inline max@0: bool max@0: op_princomp::direct_princomp max@0: ( max@0: Mat< std::complex >& coeff_out, max@0: Mat< std::complex >& score_out, max@0: Col& latent_out, max@0: Col< std::complex >& tsquared_out, max@0: const Mat< std::complex >& in max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef std::complex eT; max@0: max@0: const uword n_rows = in.n_rows; max@0: const uword n_cols = in.n_cols; max@0: max@0: if(n_rows > 1) // more than one sample max@0: { max@0: // subtract the mean - use score_out as temporary matrix max@0: score_out = in - repmat(mean(in), n_rows, 1); max@0: max@0: // singular value decomposition max@0: Mat U; max@0: Col s; max@0: max@0: const bool svd_ok = svd(U,s,coeff_out,score_out); max@0: max@0: if(svd_ok == false) max@0: { max@0: return false; max@0: } max@0: max@0: max@0: //U.reset(); max@0: max@0: // normalize the eigenvalues max@0: s /= std::sqrt( double(n_rows - 1) ); max@0: max@0: // project the samples to the principals max@0: score_out *= coeff_out; max@0: max@0: if(n_rows <= n_cols) // number of samples is less than their dimensionality max@0: { max@0: score_out.cols(n_rows-1,n_cols-1).zeros(); max@0: max@0: Col s_tmp = zeros< Col >(n_cols); max@0: s_tmp.rows(0,n_rows-2) = s.rows(0,n_rows-2); max@0: s = s_tmp; max@0: max@0: // compute the Hotelling's T-squared max@0: s_tmp.rows(0,n_rows-2) = 1.0 / s_tmp.rows(0,n_rows-2); max@0: const Mat S = score_out * diagmat(Col(s_tmp)); max@0: tsquared_out = sum(S%S,1); max@0: } max@0: else max@0: { max@0: // compute the Hotelling's T-squared max@0: const Mat S = score_out * diagmat(Col(T(1) / s)); max@0: tsquared_out = sum(S%S,1); max@0: } max@0: max@0: // compute the eigenvalues of the principal vectors max@0: latent_out = s%s; max@0: max@0: } max@0: else // 0 or 1 samples max@0: { max@0: coeff_out.eye(n_cols, n_cols); max@0: max@0: score_out.copy_size(in); max@0: score_out.zeros(); max@0: max@0: latent_out.set_size(n_cols); max@0: latent_out.zeros(); max@0: max@0: tsquared_out.set_size(n_rows); max@0: tsquared_out.zeros(); max@0: } max@0: max@0: return true; max@0: } max@0: max@0: max@0: max@0: //! \brief max@0: //! principal component analysis -- 3 arguments complex version max@0: //! computation is done via singular value decomposition max@0: //! coeff_out -> principal component coefficients max@0: //! score_out -> projected samples max@0: //! latent_out -> eigenvalues of principal vectors max@0: template max@0: inline max@0: bool max@0: op_princomp::direct_princomp max@0: ( max@0: Mat< std::complex >& coeff_out, max@0: Mat< std::complex >& score_out, max@0: Col& latent_out, max@0: const Mat< std::complex >& in max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef std::complex eT; max@0: max@0: const uword n_rows = in.n_rows; max@0: const uword n_cols = in.n_cols; max@0: max@0: if(n_rows > 1) // more than one sample max@0: { max@0: // subtract the mean - use score_out as temporary matrix max@0: score_out = in - repmat(mean(in), n_rows, 1); max@0: max@0: // singular value decomposition max@0: Mat U; max@0: Col< T> s; max@0: max@0: const bool svd_ok = svd(U,s,coeff_out,score_out); max@0: max@0: if(svd_ok == false) max@0: { max@0: return false; max@0: } max@0: max@0: max@0: // U.reset(); max@0: max@0: // normalize the eigenvalues max@0: s /= std::sqrt( double(n_rows - 1) ); max@0: max@0: // project the samples to the principals max@0: score_out *= coeff_out; max@0: max@0: if(n_rows <= n_cols) // number of samples is less than their dimensionality max@0: { max@0: score_out.cols(n_rows-1,n_cols-1).zeros(); max@0: max@0: Col s_tmp = zeros< Col >(n_cols); max@0: s_tmp.rows(0,n_rows-2) = s.rows(0,n_rows-2); max@0: s = s_tmp; max@0: } max@0: max@0: // compute the eigenvalues of the principal vectors max@0: latent_out = s%s; max@0: max@0: } max@0: else // 0 or 1 samples max@0: { max@0: coeff_out.eye(n_cols, n_cols); max@0: max@0: score_out.copy_size(in); max@0: score_out.zeros(); max@0: max@0: latent_out.set_size(n_cols); max@0: latent_out.zeros(); max@0: } max@0: max@0: return true; max@0: } max@0: max@0: max@0: max@0: //! \brief max@0: //! principal component analysis -- 2 arguments complex version max@0: //! computation is done via singular value decomposition max@0: //! coeff_out -> principal component coefficients max@0: //! score_out -> projected samples max@0: template max@0: inline max@0: bool max@0: op_princomp::direct_princomp max@0: ( max@0: Mat< std::complex >& coeff_out, max@0: Mat< std::complex >& score_out, max@0: const Mat< std::complex >& in max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef std::complex eT; max@0: max@0: const uword n_rows = in.n_rows; max@0: const uword n_cols = in.n_cols; max@0: max@0: if(n_rows > 1) // more than one sample max@0: { max@0: // subtract the mean - use score_out as temporary matrix max@0: score_out = in - repmat(mean(in), n_rows, 1); max@0: max@0: // singular value decomposition max@0: Mat U; max@0: Col< T> s; max@0: max@0: const bool svd_ok = svd(U,s,coeff_out,score_out); max@0: max@0: if(svd_ok == false) max@0: { max@0: return false; max@0: } max@0: max@0: // U.reset(); max@0: max@0: // normalize the eigenvalues max@0: s /= std::sqrt( double(n_rows - 1) ); max@0: max@0: // project the samples to the principals max@0: score_out *= coeff_out; max@0: max@0: if(n_rows <= n_cols) // number of samples is less than their dimensionality max@0: { max@0: score_out.cols(n_rows-1,n_cols-1).zeros(); max@0: } max@0: max@0: } max@0: else // 0 or 1 samples max@0: { max@0: coeff_out.eye(n_cols, n_cols); max@0: max@0: score_out.copy_size(in); max@0: score_out.zeros(); max@0: } max@0: max@0: return true; max@0: } max@0: max@0: max@0: max@0: //! \brief max@0: //! principal component analysis -- 1 argument complex version max@0: //! computation is done via singular value decomposition max@0: //! coeff_out -> principal component coefficients max@0: template max@0: inline max@0: bool max@0: op_princomp::direct_princomp max@0: ( max@0: Mat< std::complex >& coeff_out, max@0: const Mat< std::complex >& in max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename std::complex eT; max@0: max@0: if(in.n_elem != 0) max@0: { max@0: // singular value decomposition max@0: Mat U; max@0: Col< T> s; max@0: max@0: const Mat tmp = in - repmat(mean(in), in.n_rows, 1); max@0: max@0: const bool svd_ok = svd(U,s,coeff_out, tmp); max@0: max@0: if(svd_ok == false) max@0: { max@0: return false; max@0: } max@0: } max@0: else max@0: { max@0: coeff_out.eye(in.n_cols, in.n_cols); max@0: } max@0: max@0: return true; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: op_princomp::apply max@0: ( max@0: Mat& out, max@0: const Op& in max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename T1::elem_type eT; max@0: max@0: const unwrap_check tmp(in.m, out); max@0: const Mat& A = tmp.M; max@0: max@0: const bool status = op_princomp::direct_princomp(out, A); max@0: max@0: if(status == false) max@0: { max@0: out.reset(); max@0: max@0: arma_bad("princomp(): failed to converge"); max@0: } max@0: } max@0: max@0: max@0: max@0: //! @}