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