op_mean_meat.hpp

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