comparison armadillo-2.4.4/include/armadillo_bits/op_sum_meat.hpp @ 0:8b6102e2a9b0

Armadillo Library
author maxzanoni76 <max.zanoni@eecs.qmul.ac.uk>
date Wed, 11 Apr 2012 09:27:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:8b6102e2a9b0
1 // Copyright (C) 2008-2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 2008-2011 Conrad Sanderson
3 //
4 // This file is part of the Armadillo C++ library.
5 // It is provided without any warranty of fitness
6 // for any purpose. You can redistribute this file
7 // and/or modify it under the terms of the GNU
8 // Lesser General Public License (LGPL) as published
9 // by the Free Software Foundation, either version 3
10 // of the License or (at your option) any later version.
11 // (see http://www.opensource.org/licenses for more info)
12
13
14 //! \addtogroup op_sum
15 //! @{
16
17 //! \brief
18 //! Immediate sum of elements of a matrix along a specified dimension (either rows or columns).
19 //! The result is stored in a dense matrix that has either one column or one row.
20 //! See the sum() function for more details.
21 template<typename T1>
22 inline
23 void
24 op_sum::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_sum>& in)
25 {
26 arma_extra_debug_sigprint();
27
28 const uword dim = in.aux_uword_a;
29 arma_debug_check( (dim > 1), "sum(): incorrect usage. dim must be 0 or 1");
30
31 typedef typename T1::elem_type eT;
32
33 const unwrap_check<T1> tmp(in.m, out);
34 const Mat<eT>& X = tmp.M;
35
36 const uword X_n_rows = X.n_rows;
37 const uword X_n_cols = X.n_cols;
38
39 if(dim == 0) // traverse across rows (i.e. find the sum in each column)
40 {
41 out.set_size(1, X_n_cols);
42
43 eT* out_mem = out.memptr();
44
45 for(uword col=0; col<X_n_cols; ++col)
46 {
47 out_mem[col] = arrayops::accumulate(X.colptr(col), X_n_rows);
48 }
49 }
50 else // traverse across columns (i.e. find the sum in each row)
51 {
52 out.set_size(X_n_rows, 1);
53
54 eT* out_mem = out.memptr();
55
56 for(uword row=0; row<X_n_rows; ++row)
57 {
58 eT val = eT(0);
59
60 for(uword col=0; col<X_n_cols; ++col)
61 {
62 val += X.at(row,col);
63 }
64
65 out_mem[row] = val;
66 }
67 }
68 }
69
70
71
72 //! @}