op_stddev_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_stddev
00018 //! @{
00019 
00020 
00021 //! \brief
00022 //! For each row or for each column, find the standard deviation.
00023 //! The result is stored in a dense matrix that has either one column or one row.
00024 //! The dimension for which the standard deviations are found is set via the stddev() function.
00025 template<typename eT>
00026 inline
00027 void
00028 op_stddev::apply(Mat< typename get_pod_type<eT>::result >& out, const Mat<eT>& X, const u32 norm_type, const u32 dim)
00029   {
00030   arma_extra_debug_sigprint();
00031   
00032   arma_debug_check( (X.n_elem == 0), "stddev(): given matrix has no elements" );
00033   
00034   arma_debug_check( (norm_type > 1), "stddev(): incorrect usage. norm_type must be 0 or 1");
00035   arma_debug_check( (dim > 1),       "stddev(): incorrect usage. dim must be 0 or 1"      );
00036   
00037   if(dim == 0)
00038     {
00039     arma_extra_debug_print("op_stddev::apply(), dim = 0");
00040     
00041     out.set_size(1, X.n_cols);
00042     
00043     for(u32 col=0; col<X.n_cols; ++col)
00044       {
00045       out[col] = std::sqrt( op_var::direct_var( X.colptr(col), X.n_rows, norm_type ) );
00046       }
00047     }
00048   else
00049   if(dim == 1)
00050     {
00051     arma_extra_debug_print("op_stddev::apply(), dim = 1");
00052     
00053     const u32 n_rows = X.n_rows;
00054     const u32 n_cols = X.n_cols;
00055     
00056     out.set_size(n_rows, 1);
00057     
00058     podarray<eT> tmp(n_cols);
00059     
00060     eT* tmp_mem = tmp.memptr();
00061     
00062     for(u32 row=0; row<n_rows; ++row)
00063       {
00064       for(u32 col=0; col<n_cols; ++col)
00065         {
00066         tmp_mem[col] = X.at(row,col);
00067         }
00068       
00069       out[row] = std::sqrt( op_var::direct_var(tmp_mem, n_cols, norm_type) );
00070       }
00071     
00072     }
00073   
00074   }
00075 
00076 
00077 
00078 //! @}