comparison armadillo-3.900.4/include/armadillo_bits/fn_accu.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-2012 NICTA (www.nicta.com.au)
2 // Copyright (C) 2008-2012 Conrad Sanderson
3 // Copyright (C) 2012 Ryan Curtin
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
9
10 //! \addtogroup fn_accu
11 //! @{
12
13
14
15 template<typename T1>
16 arma_hot
17 inline
18 typename T1::elem_type
19 accu_proxy_linear(const Proxy<T1>& P)
20 {
21 typedef typename T1::elem_type eT;
22 typedef typename Proxy<T1>::ea_type ea_type;
23
24 ea_type A = P.get_ea();
25 const uword n_elem = P.get_n_elem();
26
27 eT val1 = eT(0);
28 eT val2 = eT(0);
29
30 uword i,j;
31 for(i=0, j=1; j < n_elem; i+=2, j+=2)
32 {
33 val1 += A[i];
34 val2 += A[j];
35 }
36
37 if(i < n_elem)
38 {
39 val1 += A[i]; // equivalent to: val1 += A[n_elem-1];
40 }
41
42 return (val1 + val2);
43 }
44
45
46
47 template<typename T1>
48 arma_hot
49 inline
50 typename T1::elem_type
51 accu_proxy_at(const Proxy<T1>& P)
52 {
53 typedef typename T1::elem_type eT;
54
55 const uword n_rows = P.get_n_rows();
56 const uword n_cols = P.get_n_cols();
57
58 eT val = eT(0);
59
60 if(n_rows != 1)
61 {
62 for(uword col=0; col < n_cols; ++col)
63 for(uword row=0; row < n_rows; ++row)
64 {
65 val += P.at(row,col);
66 }
67 }
68 else
69 {
70 for(uword col=0; col < n_cols; ++col)
71 {
72 val += P.at(0,col);
73 }
74 }
75
76 return val;
77 }
78
79
80
81 //! accumulate the elements of a matrix
82 template<typename T1>
83 arma_hot
84 inline
85 typename enable_if2< is_arma_type<T1>::value, typename T1::elem_type >::result
86 accu(const T1& X)
87 {
88 arma_extra_debug_sigprint();
89
90 const Proxy<T1> P(X);
91
92 return (Proxy<T1>::prefer_at_accessor == false) ? accu_proxy_linear(P) : accu_proxy_at(P);
93 }
94
95
96
97 //! explicit handling of Hamming norm (also known as zero norm)
98 template<typename T1>
99 inline
100 arma_warn_unused
101 uword
102 accu(const mtOp<uword,T1,op_rel_noteq>& X)
103 {
104 arma_extra_debug_sigprint();
105
106 typedef typename T1::elem_type eT;
107
108 const eT val = X.aux;
109
110 const Proxy<T1> P(X.m);
111
112 uword n_nonzero = 0;
113
114 if(Proxy<T1>::prefer_at_accessor == false)
115 {
116 typedef typename Proxy<T1>::ea_type ea_type;
117
118 ea_type A = P.get_ea();
119 const uword n_elem = P.get_n_elem();
120
121 for(uword i=0; i<n_elem; ++i)
122 {
123 if(A[i] != val) { ++n_nonzero; }
124 }
125 }
126 else
127 {
128 const uword P_n_cols = P.get_n_cols();
129 const uword P_n_rows = P.get_n_rows();
130
131 if(P_n_rows == 1)
132 {
133 for(uword col=0; col < P_n_cols; ++col)
134 {
135 if(P.at(0,col) != val) { ++n_nonzero; }
136 }
137 }
138 else
139 {
140 for(uword col=0; col < P_n_cols; ++col)
141 for(uword row=0; row < P_n_rows; ++row)
142 {
143 if(P.at(row,col) != val) { ++n_nonzero; }
144 }
145 }
146 }
147
148 return n_nonzero;
149 }
150
151
152
153 //! accumulate the elements of a subview (submatrix)
154 template<typename eT>
155 arma_hot
156 arma_pure
157 arma_warn_unused
158 inline
159 eT
160 accu(const subview<eT>& X)
161 {
162 arma_extra_debug_sigprint();
163
164 const uword X_n_rows = X.n_rows;
165 const uword X_n_cols = X.n_cols;
166
167 eT val = eT(0);
168
169 if(X_n_rows == 1)
170 {
171 const Mat<eT>& A = X.m;
172
173 const uword start_row = X.aux_row1;
174 const uword start_col = X.aux_col1;
175
176 const uword end_col_p1 = start_col + X_n_cols;
177
178 uword i,j;
179 for(i=start_col, j=start_col+1; j < end_col_p1; i+=2, j+=2)
180 {
181 val += A.at(start_row, i);
182 val += A.at(start_row, j);
183 }
184
185 if(i < end_col_p1)
186 {
187 val += A.at(start_row, i);
188 }
189 }
190 else
191 if(X_n_cols == 1)
192 {
193 val = arrayops::accumulate( X.colptr(0), X_n_rows );
194 }
195 else
196 {
197 for(uword col=0; col < X_n_cols; ++col)
198 {
199 val += arrayops::accumulate( X.colptr(col), X_n_rows );
200 }
201 }
202
203 return val;
204 }
205
206
207
208 template<typename eT>
209 arma_hot
210 arma_pure
211 arma_warn_unused
212 inline
213 eT
214 accu(const subview_col<eT>& X)
215 {
216 arma_extra_debug_sigprint();
217
218 return arrayops::accumulate( X.colptr(0), X.n_rows );
219 }
220
221
222
223 //! accumulate the elements of a cube
224 template<typename T1>
225 arma_hot
226 arma_warn_unused
227 inline
228 typename T1::elem_type
229 accu(const BaseCube<typename T1::elem_type,T1>& X)
230 {
231 arma_extra_debug_sigprint();
232
233 typedef typename T1::elem_type eT;
234 typedef typename ProxyCube<T1>::ea_type ea_type;
235
236 const ProxyCube<T1> A(X.get_ref());
237
238 if(ProxyCube<T1>::prefer_at_accessor == false)
239 {
240 ea_type P = A.get_ea();
241 const uword n_elem = A.get_n_elem();
242
243 eT val1 = eT(0);
244 eT val2 = eT(0);
245
246 uword i,j;
247
248 for(i=0, j=1; j<n_elem; i+=2, j+=2)
249 {
250 val1 += P[i];
251 val2 += P[j];
252 }
253
254 if(i < n_elem)
255 {
256 val1 += P[i];
257 }
258
259 return val1 + val2;
260 }
261 else
262 {
263 const uword n_rows = A.get_n_rows();
264 const uword n_cols = A.get_n_cols();
265 const uword n_slices = A.get_n_slices();
266
267 eT val = eT(0);
268
269 for(uword slice=0; slice<n_slices; ++slice)
270 for(uword col=0; col<n_cols; ++col)
271 for(uword row=0; row<n_rows; ++row)
272 {
273 val += A.at(row,col,slice);
274 }
275
276 return val;
277 }
278 }
279
280
281
282 template<typename T>
283 arma_inline
284 arma_warn_unused
285 const typename arma_scalar_only<T>::result &
286 accu(const T& x)
287 {
288 return x;
289 }
290
291
292
293 //! accumulate values in a sparse object
294 template<typename T1>
295 arma_hot
296 inline
297 arma_warn_unused
298 typename enable_if2<is_arma_sparse_type<T1>::value, typename T1::elem_type>::result
299 accu(const T1& x)
300 {
301 arma_extra_debug_sigprint();
302
303 typedef typename T1::elem_type eT;
304
305 const SpProxy<T1> p(x);
306
307 if(SpProxy<T1>::must_use_iterator == false)
308 {
309 // direct counting
310 return arrayops::accumulate(p.get_values(), p.get_n_nonzero());
311 }
312 else
313 {
314 typename SpProxy<T1>::const_iterator_type it = p.begin();
315 typename SpProxy<T1>::const_iterator_type it_end = p.end();
316
317 eT result = eT(0);
318
319 while(it != it_end)
320 {
321 result += (*it);
322 ++it;
323 }
324
325 return result;
326 }
327 }
328
329
330
331 //! @}