op_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 // - Conrad Sanderson (conradsand at ieee dot org)
00006 // - Dimitrios Bouzas (dimitris dot mpouzas at gmail dot com)
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 
00019 //! \addtogroup op_cov
00020 //! @{
00021 
00022 
00023 
00024 template<typename eT>
00025 inline
00026 void
00027 op_cov::direct_cov(Mat<eT>& out, const Mat<eT>& A, const u32 norm_type)
00028   {
00029   arma_extra_debug_sigprint();
00030   
00031   if(A.is_vec())
00032     {
00033     if(A.n_rows == 1)
00034       {
00035       out = var(trans(A), norm_type);      
00036       }
00037     else
00038       {
00039       out = var(A, norm_type);
00040       }
00041     }
00042   else
00043     {
00044     const u32 N = A.n_rows;
00045     const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
00046 
00047     const Row<eT> acc = sum(A);
00048 
00049     out = trans(A) * A;
00050     out -= (trans(acc) * acc)/eT(N);
00051     out /= norm_val;
00052     }
00053   }
00054 
00055 
00056 
00057 template<typename T>
00058 inline
00059 void
00060 op_cov::direct_cov(Mat< std::complex<T> >& out, const Mat< std::complex<T> >& A, const u32 norm_type)
00061   {
00062   arma_extra_debug_sigprint();
00063   
00064   typedef typename std::complex<T> eT;
00065   
00066   if(A.is_vec())
00067     {
00068     if(A.n_rows == 1)
00069       {
00070       const Mat<T> tmp_mat = var(trans(A), norm_type);
00071       out.set_size(1,1);
00072       out[0] = tmp_mat[0];
00073       }
00074     else
00075       {
00076       const Mat<T> tmp_mat = var(A, norm_type);
00077       out.set_size(1,1);
00078       out[0] = tmp_mat[0];
00079       }
00080     }
00081   else
00082     {
00083     const u32 N = A.n_rows;
00084     const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N);
00085 
00086     const Row<eT> acc = sum(A);
00087 
00088     out = trans(conj(A)) * A;
00089     out -= (trans(conj(acc)) * acc)/eT(N);
00090     out /= norm_val;
00091     }
00092   }
00093 
00094 
00095 
00096 template<typename T1>
00097 inline
00098 void
00099 op_cov::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_cov>& in)
00100   {
00101   arma_extra_debug_sigprint();
00102   
00103   typedef typename T1::elem_type eT;
00104   
00105   const unwrap_check<T1> tmp(in.m, out);
00106   const Mat<eT>& A     = tmp.M;
00107   
00108   const u32 norm_type = in.aux_u32_a;
00109   
00110   op_cov::direct_cov(out, A, norm_type);
00111   }
00112 
00113 
00114 
00115 //! @}