op_mean_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 // 
00007 // This file is part of the Armadillo C++ library.
00008 // It is provided without any warranty of fitness
00009 // for any purpose. You can redistribute this file
00010 // and/or modify it under the terms of the GNU
00011 // Lesser General Public License (LGPL) as published
00012 // by the Free Software Foundation, either version 3
00013 // of the License or (at your option) any later version.
00014 // (see http://www.opensource.org/licenses for more info)
00015 
00016 
00017 //! \addtogroup op_mean
00018 //! @{
00019 
00020 
00021 //! find the mean value of an array
00022 template<typename eT>
00023 inline 
00024 eT
00025 op_mean::direct_mean(const eT* const X, const u32 n_elem)
00026   {
00027   arma_extra_debug_sigprint();
00028   
00029   eT val = eT(0);
00030   
00031   for(u32 i=0; i<n_elem; ++i)
00032     {
00033     val += X[i];
00034     }
00035   
00036   return val / eT(n_elem);
00037   }
00038 
00039 
00040 
00041 //! find the mean value of a subview
00042 template<typename eT>
00043 inline 
00044 eT
00045 op_mean::direct_mean(const subview<eT>& X)
00046   {
00047   arma_extra_debug_sigprint();
00048   
00049   eT val = eT(0);
00050   
00051   for(u32 i=0; i<X.n_elem; ++i)
00052     {
00053     val += X[i];
00054     }
00055   
00056   return val / eT(X.n_elem);
00057   }
00058 
00059 
00060 
00061 //! find the mean value of a diagview
00062 template<typename eT>
00063 inline 
00064 eT
00065 op_mean::direct_mean(const diagview<eT>& X)
00066   {
00067   arma_extra_debug_sigprint();
00068   
00069   eT val = eT(0);
00070   
00071   for(u32 i=0; i<X.n_elem; ++i)
00072     {
00073     val += X[i];
00074     }
00075   
00076   return val / eT(X.n_elem);
00077   }
00078 
00079 
00080 
00081 //! \brief
00082 //! For each row or for each column, find the mean value.
00083 //! The result is stored in a dense matrix that has either one column or one row.
00084 //! The dimension, for which the means are found, is set via the mean() function.
00085 template<typename T1>
00086 inline
00087 void
00088 op_mean::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_mean>& in)
00089   {
00090   arma_extra_debug_sigprint();
00091   
00092   typedef typename T1::elem_type eT;
00093   
00094   const unwrap_check<T1> tmp(in.m, out);
00095   const Mat<eT>& X = tmp.M;
00096   
00097   arma_debug_check( (X.n_elem == 0), "mean(): given matrix has no elements" );
00098   
00099   const u32 dim = in.aux_u32_a;
00100   arma_debug_check( (dim > 1), "mean(): incorrect usage. dim must be 0 or 1");
00101   
00102   
00103   if(dim == 0)
00104     {
00105     arma_extra_debug_print("op_mean::apply(), dim = 0");
00106     
00107     out.set_size(1, X.n_cols);
00108     
00109     for(u32 col=0; col<X.n_cols; ++col)
00110       {
00111       out[col] = op_mean::direct_mean( X.colptr(col), X.n_rows );
00112       }
00113     }
00114   else
00115   if(dim == 1)
00116     {
00117     arma_extra_debug_print("op_mean::apply(), dim = 1");
00118     
00119     out.set_size(X.n_rows, 1);
00120     
00121     for(u32 row=0; row<X.n_rows; ++row)
00122       {
00123       eT val = eT(0);
00124       
00125       for(u32 col=0; col<X.n_cols; ++col)
00126         {
00127         val += X.at(row,col);
00128         }
00129       
00130       out[row] = val / eT(X.n_cols);
00131       
00132       }
00133     
00134     }
00135   
00136   }
00137 
00138 
00139 //! @}