op_princomp_cov_meat.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2010 NICTA and the authors listed below
00002 // http://nicta.com.au
00003 // 
00004 // Authors:
00005 // - Dimitrios Bouzas (dimitris dot mpouzas at gmail dot com)
00006 // - Conrad Sanderson (conradsand at ieee dot org)
00007 // 
00008 // This file is part of the Armadillo C++ library.
00009 // It is provided without any warranty of fitness
00010 // for any purpose. You can redistribute this file
00011 // and/or modify it under the terms of the GNU
00012 // Lesser General Public License (LGPL) as published
00013 // by the Free Software Foundation, either version 3
00014 // of the License or (at your option) any later version.
00015 // (see http://www.opensource.org/licenses for more info)
00016 
00017 
00018 //! \addtogroup op_princomp_cov
00019 //! @{
00020 
00021 
00022 
00023 //! \brief
00024 //! principal component analysis of a covariance matrix -- 3 arguments version
00025 //! computation is done via singular value decomposition
00026 //! coeff_out     -> principal component coefficients
00027 //! latent_out    -> principal component variances
00028 //! explained_out -> percentage of the total variance explained by each principal component
00029 template<typename eT>
00030 inline
00031 void
00032 op_princomp_cov::direct_princomp_cov
00033   (
00034         Mat<eT>& coeff_out,
00035         Col<eT>& latent_out,
00036         Col<eT>& explained_out,
00037   const Mat<eT>& in
00038   )
00039   {
00040   arma_extra_debug_sigprint();
00041   
00042   // computation of the covariance matrix
00043   const Mat<eT> in_cov = cov(in);
00044   
00045   // singular value decomposition
00046   Mat<eT> U;
00047 
00048   const bool svd_ok = svd(U, latent_out, coeff_out, in_cov);
00049     
00050   if(svd_ok == false)
00051     {
00052     arma_print("princomp_cov(): singular value decomposition failed");
00053       
00054     coeff_out.reset();
00055     latent_out.reset();
00056     explained_out.reset();
00057       
00058     return;
00059     }
00060   
00061   explained_out =  (eT(100) * latent_out) / sum(latent_out);
00062   }
00063 
00064 
00065 
00066 //! \brief
00067 //! principal component analysis of a covariance matrix -- 2 arguments version
00068 //! computation is done via singular value decomposition
00069 //! coeff_out     -> principal component coefficients
00070 //! latent_out    -> principal component variances
00071 template<typename eT>
00072 inline
00073 void
00074 op_princomp_cov::direct_princomp_cov
00075   (
00076         Mat<eT>& coeff_out,
00077         Col<eT>& latent_out,
00078   const Mat<eT>& in
00079   )
00080   {
00081   arma_extra_debug_sigprint();
00082   
00083   // computation of the covariance matrix
00084   const Mat<eT> in_cov = cov(in);
00085   
00086   // singular value decomposition
00087   Mat<eT> U;
00088 
00089   const bool svd_ok = svd(U, latent_out, coeff_out, in_cov);
00090     
00091   if(svd_ok == false)
00092     {
00093     arma_print("princomp_cov(): singular value decomposition failed");
00094       
00095     coeff_out.reset();
00096     latent_out.reset();
00097       
00098     return;
00099     }
00100 
00101   }
00102 
00103 
00104 
00105 //! \brief
00106 //! principal component analysis of a covariance matrix -- 1 argument version
00107 //! computation is done via singular value decomposition
00108 //! coeff_out     -> principal component coefficients
00109 template<typename eT>
00110 inline
00111 void
00112 op_princomp_cov::direct_princomp_cov
00113   (
00114         Mat<eT>& coeff_out,
00115   const Mat<eT>& in
00116   )
00117   {
00118   arma_extra_debug_sigprint();
00119   
00120   // computation of the covariance matrix
00121   const Mat<eT> in_cov = cov(in);
00122   
00123   // singular value decomposition
00124   Mat<eT> U;
00125   Col<eT> s;
00126 
00127   const bool svd_ok = svd(U, s, coeff_out, in_cov);
00128     
00129   if(svd_ok == false)
00130     {
00131     arma_print("princomp_cov(): singular value decomposition failed");
00132       
00133     coeff_out.reset();
00134      
00135     return;
00136     }
00137   
00138   }
00139 
00140 
00141 
00142 //! \brief
00143 //! principal component analysis of a covariance matrix -- 3 arguments complex version
00144 //! computation is done via singular value decomposition
00145 //! coeff_out     -> principal component coefficients
00146 //! latent_out    -> principal component variances
00147 //! explained_out -> percentage of the total variance explained by each principal component
00148 template<typename T>
00149 inline
00150 void
00151 op_princomp_cov::direct_princomp_cov
00152   (
00153         Mat< std::complex<T> >& coeff_out,
00154                         Col<T>& latent_out,
00155                         Col<T>& explained_out,
00156   const Mat< std::complex<T> >& in
00157   )
00158   {
00159   arma_extra_debug_sigprint();
00160   
00161   typedef std::complex<T> eT;
00162   
00163   // computation of the covariance matrix
00164   const Mat<eT> in_cov = cov(in);
00165   
00166   // singular value decomposition
00167   Mat<eT> U;
00168 
00169   const bool svd_ok = svd(U, latent_out, coeff_out, in_cov);
00170     
00171   if(svd_ok == false)
00172     {
00173     arma_print("princomp_cov(): singular value decomposition failed");
00174       
00175     coeff_out.reset();
00176     latent_out.reset();
00177     explained_out.reset();
00178       
00179     return;
00180     }
00181   
00182   explained_out =  (T(100) * latent_out) / sum(latent_out);
00183   }
00184 
00185 
00186 
00187 //! \brief
00188 //! principal component analysis of a covariance matrix -- 2 arguments complex version
00189 //! computation is done via singular value decomposition
00190 //! coeff_out     -> principal component coefficients
00191 //! latent_out    -> principal component variances
00192 template<typename T>
00193 inline
00194 void
00195 op_princomp_cov::direct_princomp_cov
00196   (
00197         Mat< std::complex<T> >& coeff_out,
00198                         Col<T>& latent_out,
00199   const Mat< std::complex<T> >& in
00200   )
00201   {
00202   arma_extra_debug_sigprint();
00203   
00204   typedef std::complex<T> eT;
00205   
00206   // computation of the covariance matrix
00207   const Mat<eT> in_cov = cov(in);
00208   
00209   // singular value decomposition
00210   Mat<eT> U;
00211 
00212   const bool svd_ok = svd(U, latent_out, coeff_out, in_cov);
00213     
00214   if(svd_ok == false)
00215     {
00216     arma_print("princomp_cov(): singular value decomposition failed");
00217       
00218     coeff_out.reset();
00219     latent_out.reset();
00220       
00221     return;
00222     }
00223 
00224   }
00225 
00226 
00227 
00228 //! \brief
00229 //! principal component analysis of a covariance matrix -- 1 argument complex version
00230 //! computation is done via singular value decomposition
00231 //! coeff_out    -> principal component coefficients
00232 template<typename T>
00233 inline
00234 void
00235 op_princomp_cov::direct_princomp_cov
00236   (
00237         Mat< std::complex<T> >& coeff_out,
00238   const Mat< std::complex<T> >& in
00239   )
00240   {
00241   arma_extra_debug_sigprint();
00242   
00243   typedef std::complex<T> eT;
00244   
00245   // computation of the covariance matrix
00246   const Mat<eT> in_cov = cov(in);
00247   
00248   // singular value decomposition
00249   Mat<eT> U;
00250   Col<T> s;
00251 
00252   const bool svd_ok = svd(U, s, coeff_out, in_cov);
00253     
00254   if(svd_ok == false)
00255     {
00256     arma_print("princomp_cov(): singular value decomposition failed");
00257       
00258     coeff_out.reset();
00259       
00260     return;
00261     }
00262   
00263   }
00264 
00265 
00266 
00267 template<typename T1>
00268 inline
00269 void
00270 op_princomp_cov::apply
00271   (
00272         Mat<typename T1::elem_type>& out,
00273   const Op<T1,op_princomp_cov>&      in
00274   )
00275   {
00276   arma_extra_debug_sigprint();
00277   
00278   typedef typename T1::elem_type eT;
00279   
00280   const unwrap_check<T1> tmp(in.m, out);
00281   const Mat<eT>& A     = tmp.M;
00282   
00283   op_princomp_cov::direct_princomp_cov(out, A);  
00284   }
00285 
00286 
00287 
00288 //! @}