op_sum_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_sum
00017 //! @{
00018 
00019 //! \brief
00020 //! Immediate sum of elements of a matrix along a specified dimension (either rows or columns).
00021 //! The result is stored in a dense matrix that has either one column or one row.
00022 //! See the sum() function for more details.
00023 template<typename T1>
00024 inline
00025 void
00026 op_sum::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_sum>& in)
00027   {
00028   arma_extra_debug_sigprint();
00029   
00030   const u32 dim = in.aux_u32_a;
00031   arma_debug_check( (dim > 1), "op_sum::apply(): incorrect usage. dim must be 0 or 1");
00032   
00033   typedef typename T1::elem_type eT;
00034   
00035   const unwrap_check<T1> tmp(in.m, out);
00036   const Mat<eT>& X = tmp.M;
00037   
00038   if(dim == 0)  // column-wise sum
00039     {
00040     out.set_size(1, X.n_cols);
00041     
00042     for(u32 col=0; col<X.n_cols; ++col)
00043       {
00044       eT tmp_val = eT(0);
00045       for(u32 row=0; row<X.n_rows; ++row)
00046         {
00047         tmp_val += X.at(row,col);
00048         }
00049     
00050       out.at(0,col) = tmp_val;
00051       }
00052     }
00053   else
00054   if(dim == 1)  // row-wise sum
00055     {
00056     out.set_size(X.n_rows, 1);
00057     
00058     for(u32 row=0; row<X.n_rows; ++row)
00059       {
00060       eT tmp_val = eT(0);
00061       for(u32 col=0; col<X.n_cols; ++col)
00062         {
00063         tmp_val += X.at(row,col);
00064         }
00065     
00066       out.at(row,0) = tmp_val;
00067       }
00068     
00069     }
00070   
00071   }
00072 
00073 //! @}