fn_sum.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 fn_sum
00018 //! @{
00019 
00020 
00021 //! \brief
00022 //! Delayed sum of elements of a matrix along a specified dimension (either rows or columns).
00023 //! The result is stored in a dense matrix that has either one column or one row.
00024 //! For dim = 0, find the sum of each column.
00025 //! For dim = 1, find the sum of each row.
00026 //! The default is dim = 0.
00027 //! NOTE: this function works differently than in Matlab/Octave.
00028 
00029 template<typename T1>
00030 arma_inline
00031 const Op<T1, op_sum>
00032 sum(const Base<typename T1::elem_type,T1>& X, const u32 dim = 0)
00033   {
00034   arma_extra_debug_sigprint();
00035   
00036   return Op<T1, op_sum>(X.get_ref(), dim, 0);
00037   }
00038 
00039 
00040 //! \brief
00041 //! Immediate 'sum all values' operation for a row vector
00042 template<typename eT>
00043 arma_inline
00044 arma_warn_unused
00045 eT
00046 sum(const Row<eT>& X)
00047   {
00048   arma_extra_debug_sigprint();
00049   
00050   return accu(X);
00051   }
00052 
00053 
00054 
00055 //! \brief
00056 //! Immediate 'sum all values' operation for a column vector
00057 template<typename eT>
00058 arma_inline
00059 arma_warn_unused
00060 eT
00061 sum(const Col<eT>& X)
00062   {
00063   arma_extra_debug_sigprint();
00064   
00065   return accu(X);
00066   }
00067 
00068 
00069 
00070 //! \brief
00071 //! Immediate 'sum all values' operation,
00072 //! invoked, for example, by: sum(sum(A))
00073 
00074 template<typename T1>
00075 arma_inline
00076 arma_warn_unused
00077 typename T1::elem_type
00078 sum(const Op<T1, op_sum>& in)
00079   {
00080   arma_extra_debug_sigprint();
00081   arma_extra_debug_print("sum(): two consecutive sum() calls detected");
00082   
00083   return accu(in.m);
00084   }
00085 
00086 
00087 
00088 template<typename T1>
00089 arma_inline
00090 const Op<Op<T1, op_sum>, op_sum>
00091 sum(const Op<T1, op_sum>& in, const u32 dim)
00092   {
00093   arma_extra_debug_sigprint();
00094   
00095   return Op<Op<T1, op_sum>, op_sum>(in, dim, 0);
00096   }
00097 
00098 
00099 
00100 //! sum all values of a subview_row
00101 template<typename eT>
00102 arma_inline
00103 arma_warn_unused
00104 eT
00105 sum(const subview_row<eT>& X)
00106   {
00107   arma_extra_debug_sigprint();
00108   
00109   return accu(X);
00110   }
00111 
00112 
00113 
00114 //! sum all values of a subview_col
00115 template<typename eT>
00116 arma_inline
00117 arma_warn_unused
00118 eT
00119 sum(const subview_col<eT>& X)
00120   {
00121   arma_extra_debug_sigprint();
00122   
00123   return accu(X);
00124   }
00125 
00126 
00127 
00128 //! sum all values of a diagview
00129 template<typename eT>
00130 arma_inline
00131 arma_warn_unused
00132 eT
00133 sum(const diagview<eT>& X)
00134   {
00135   arma_extra_debug_sigprint();
00136   
00137   return accu(X);
00138   }
00139 
00140 
00141 
00142 //! @}