comparison armadillo-3.900.4/include/armadillo_bits/op_sum_meat.hpp @ 49:1ec0e2823891

Switch to using subrepo copies of qm-dsp, nnls-chroma, vamp-plugin-sdk; update Armadillo version; assume build without external BLAS/LAPACK
author Chris Cannam
date Thu, 13 Jun 2013 10:25:24 +0100
parents
children
comparison
equal deleted inserted replaced
48:69251e11a913 49:1ec0e2823891
1 // Copyright (C) 2008-2013 NICTA (www.nicta.com.au)
2 // Copyright (C) 2008-2013 Conrad Sanderson
3 //
4 // This Source Code Form is subject to the terms of the Mozilla Public
5 // License, v. 2.0. If a copy of the MPL was not distributed with this
6 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8
9 //! \addtogroup op_sum
10 //! @{
11
12 //! \brief
13 //! Immediate sum of elements of a matrix along a specified dimension (either rows or columns).
14 //! The result is stored in a dense matrix that has either one column or one row.
15 //! See the sum() function for more details.
16 template<typename T1>
17 arma_hot
18 inline
19 void
20 op_sum::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_sum>& in)
21 {
22 arma_extra_debug_sigprint();
23
24 typedef typename T1::elem_type eT;
25
26 const uword dim = in.aux_uword_a;
27 arma_debug_check( (dim > 1), "sum(): incorrect usage. dim must be 0 or 1");
28
29 const Proxy<T1> P(in.m);
30
31 typedef typename Proxy<T1>::stored_type P_stored_type;
32
33 const bool is_alias = P.is_alias(out);
34
35 if( (is_Mat<P_stored_type>::value == true) || is_alias )
36 {
37 const unwrap_check<P_stored_type> tmp(P.Q, is_alias);
38
39 const typename unwrap_check<P_stored_type>::stored_type& X = tmp.M;
40
41 const uword X_n_rows = X.n_rows;
42 const uword X_n_cols = X.n_cols;
43
44 if(dim == 0) // traverse across rows (i.e. find the sum in each column)
45 {
46 out.set_size(1, X_n_cols);
47
48 eT* out_mem = out.memptr();
49
50 for(uword col=0; col < X_n_cols; ++col)
51 {
52 out_mem[col] = arrayops::accumulate( X.colptr(col), X_n_rows );
53 }
54 }
55 else // traverse across columns (i.e. find the sum in each row)
56 {
57 out.set_size(X_n_rows, 1);
58
59 eT* out_mem = out.memptr();
60
61 for(uword row=0; row < X_n_rows; ++row)
62 {
63 eT val = eT(0);
64
65 uword i,j;
66 for(i=0, j=1; j < X_n_cols; i+=2, j+=2)
67 {
68 val += X.at(row,i);
69 val += X.at(row,j);
70 }
71
72 if(i < X_n_cols)
73 {
74 val += X.at(row,i);
75 }
76
77 out_mem[row] = val;
78 }
79 }
80 }
81 else
82 {
83 const uword P_n_rows = P.get_n_rows();
84 const uword P_n_cols = P.get_n_cols();
85
86 if(dim == 0) // traverse across rows (i.e. find the sum in each column)
87 {
88 out.set_size(1, P_n_cols);
89
90 eT* out_mem = out.memptr();
91
92 for(uword col=0; col < P_n_cols; ++col)
93 {
94 eT val = eT(0);
95
96 uword i,j;
97 for(i=0, j=1; j < P_n_rows; i+=2, j+=2)
98 {
99 val += P.at(i,col);
100 val += P.at(j,col);
101 }
102
103 if(i < P_n_rows)
104 {
105 val += P.at(i,col);
106 }
107
108 out_mem[col] = val;
109 }
110 }
111 else // traverse across columns (i.e. find the sum in each row)
112 {
113 out.set_size(P_n_rows, 1);
114
115 eT* out_mem = out.memptr();
116
117 for(uword row=0; row < P_n_rows; ++row)
118 {
119 eT val = eT(0);
120
121 uword i,j;
122 for(i=0, j=1; j < P_n_cols; i+=2, j+=2)
123 {
124 val += P.at(row,i);
125 val += P.at(row,j);
126 }
127
128 if(i < P_n_cols)
129 {
130 val += P.at(row,i);
131 }
132
133 out_mem[row] = val;
134 }
135 }
136 }
137 }
138
139
140
141 //! @}