fn_accu.hpp
Go to the documentation of this file.
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 
16 
17 
18 
20 template<typename T1>
22 inline
23 typename T1::elem_type
25  {
27 
28  typedef typename T1::elem_type eT;
29  typedef typename Proxy<T1>::ea_type ea_type;
30 
31  const Proxy<T1> A(X.get_ref());
32 
34  {
35  ea_type P = A.get_ea();
36  const uword n_elem = A.get_n_elem();
37 
38  eT val1 = eT(0);
39  eT val2 = eT(0);
40 
41  uword i,j;
42 
43  for(i=0, j=1; j<n_elem; i+=2, j+=2)
44  {
45  val1 += P[i];
46  val2 += P[j];
47  }
48 
49  if(i < n_elem)
50  {
51  val1 += P[i];
52  }
53 
54  return val1 + val2;
55  }
56  else
57  {
58  const uword n_rows = A.get_n_rows();
59  const uword n_cols = A.get_n_cols();
60 
61  eT val = eT(0);
62 
63  for(uword col=0; col<n_cols; ++col)
64  {
65  for(uword row=0; row<n_rows; ++row)
66  {
67  val += A.at(row,col);
68  }
69  }
70 
71  return val;
72  }
73  }
74 
75 
76 
78 template<typename T1>
81 uword
83  {
85 
86  typedef typename T1::elem_type eT;
87 
88  const Proxy<T1> A(X.m);
89 
90  const uword n_elem = A.get_n_elem();
91  const eT val = X.aux;
92 
93  uword n_nonzero = 0;
94  for(uword i=0; i<n_elem; ++i)
95  {
96  if(A[i] != val)
97  {
98  ++n_nonzero;
99  }
100  }
101 
102  return n_nonzero;
103  }
104 
105 
106 
108 template<typename T1>
109 arma_hot
111 inline
112 typename T1::elem_type
114  {
116 
117  typedef typename T1::elem_type eT;
118  typedef typename ProxyCube<T1>::ea_type ea_type;
119 
120  const ProxyCube<T1> A(X.get_ref());
121 
123  {
124 
125  ea_type P = A.get_ea();
126  const uword n_elem = A.get_n_elem();
127 
128  eT val1 = eT(0);
129  eT val2 = eT(0);
130 
131  uword i,j;
132 
133  for(i=0, j=1; j<n_elem; i+=2, j+=2)
134  {
135  val1 += P[i];
136  val2 += P[j];
137  }
138 
139  if(i < n_elem)
140  {
141  val1 += P[i];
142  }
143 
144  return val1 + val2;
145  }
146  else
147  {
148  const uword n_rows = A.get_n_rows();
149  const uword n_cols = A.get_n_cols();
150  const uword n_slices = A.get_n_slices();
151 
152  eT val = eT(0);
153 
154  for(uword slice=0; slice<n_slices; ++slice)
155  {
156  for(uword col=0; col<n_cols; ++col)
157  {
158  for(uword row=0; row<n_rows; ++row)
159  {
160  val += A.at(row,col,slice);
161  }
162  }
163  }
164 
165  return val;
166  }
167  }
168 
169 
170 
172 template<typename eT>
173 arma_pure
175 inline
176 eT
178  {
180 
181  const uword n_elem = X.n_elem;
182 
183  eT val = eT(0);
184 
185  for(uword i=0; i<n_elem; ++i)
186  {
187  val += X[i];
188  }
189 
190  return val;
191  }
192 
193 
194 
196 template<typename eT>
197 arma_pure
199 inline
200 eT
201 accu(const subview<eT>& S)
202  {
204 
205  const uword S_n_rows = S.n_rows;
206  const uword S_n_cols = S.n_cols;
207  const uword S_n_elem = S.n_elem;
208 
209  eT val = eT(0);
210 
211  if(S_n_elem > 0)
212  {
213  for(uword col=0; col<S_n_cols; ++col)
214  {
215  val += arrayops::accumulate( S.colptr(col), S_n_rows );
216  }
217  }
218 
219  return val;
220  }
221 
222 
223 
225 template<typename eT>
226 arma_pure
228 inline
229 eT
231  {
233 
234  const Mat<eT>& X = S.m;
235 
236  const uword n_elem = S.n_elem;
237  const uword row = S.aux_row1;
238  const uword start_col = S.aux_col1;
239  const uword end_col_p1 = start_col + S.n_cols;
240 
241  eT val = eT(0);
242 
243  if(n_elem > 0)
244  {
245  uword i,j;
246 
247  for(i=start_col, j=start_col+1; j < end_col_p1; i+=2, j+=2)
248  {
249  val += X.at(row,i);
250  val += X.at(row,j);
251  }
252 
253  if(i < end_col_p1)
254  {
255  val += X.at(row,i);
256  }
257  }
258 
259  return val;
260  }
261 
262 
263 
265 template<typename eT>
266 arma_pure
268 inline
269 eT
271  {
273 
274  return (S.n_elem > 0) ? arrayops::accumulate( S.colptr(0), S.n_rows ) : eT(0);
275  }
276 
277 
278 
const uword aux_col1
arma_aligned const T1 & m
storage of reference to the operand (eg. a matrix)
Definition: mtOp_bones.hpp:39
arma_inline const derived & get_ref() const
Definition: Base_meat.hpp:22
const uword n_cols
const uword n_rows
arma_aligned in_eT aux
storage of auxiliary data, using the element type as used by T1
Definition: mtOp_bones.hpp:40
arma_hot T1::elem_type accu(const Base< typename T1::elem_type, T1 > &X)
accumulate the elements of a matrix
Definition: fn_accu.hpp:24
const uword n_elem
u32 uword
Definition: typedef.hpp:85
const uword aux_row1
arma_inline arma_warn_unused eT & at(const uword i)
linear element accessor (treats the matrix as a vector); no bounds check.
Definition: Mat_meat.hpp:3692
arma_aligned const Mat< eT > & m
arma_inline eT * colptr(const uword in_col)
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
Analog of the Base class, intended for cubes.
#define arma_warn_unused
#define arma_pure
Dense matrix class.
#define arma_inline
const uword n_elem
arma_inline const derived & get_ref() const
Class for storing data required to extract and set the diagonals of a matrix.
arma_hot static arma_pure eT accumulate(const eT *src, const uword n_elem)
#define arma_hot


armadillo_matrix
Author(s):
autogenerated on Fri Apr 16 2021 02:31:57