max@0
|
1 // Copyright (C) 2008-2010 NICTA (www.nicta.com.au)
|
max@0
|
2 // Copyright (C) 2008-2010 Conrad Sanderson
|
max@0
|
3 //
|
max@0
|
4 // This file is part of the Armadillo C++ library.
|
max@0
|
5 // It is provided without any warranty of fitness
|
max@0
|
6 // for any purpose. You can redistribute this file
|
max@0
|
7 // and/or modify it under the terms of the GNU
|
max@0
|
8 // Lesser General Public License (LGPL) as published
|
max@0
|
9 // by the Free Software Foundation, either version 3
|
max@0
|
10 // of the License or (at your option) any later version.
|
max@0
|
11 // (see http://www.opensource.org/licenses for more info)
|
max@0
|
12
|
max@0
|
13
|
max@0
|
14 //! \addtogroup fn_sum
|
max@0
|
15 //! @{
|
max@0
|
16
|
max@0
|
17
|
max@0
|
18 //! \brief
|
max@0
|
19 //! Delayed sum of elements of a matrix along a specified dimension (either rows or columns).
|
max@0
|
20 //! The result is stored in a dense matrix that has either one column or one row.
|
max@0
|
21 //! For dim = 0, find the sum of each column.
|
max@0
|
22 //! For dim = 1, find the sum of each row.
|
max@0
|
23 //! The default is dim = 0.
|
max@0
|
24 //! NOTE: this function works differently than in Matlab/Octave.
|
max@0
|
25
|
max@0
|
26 template<typename T1>
|
max@0
|
27 arma_inline
|
max@0
|
28 const Op<T1, op_sum>
|
max@0
|
29 sum(const Base<typename T1::elem_type,T1>& X, const uword dim = 0)
|
max@0
|
30 {
|
max@0
|
31 arma_extra_debug_sigprint();
|
max@0
|
32
|
max@0
|
33 return Op<T1, op_sum>(X.get_ref(), dim, 0);
|
max@0
|
34 }
|
max@0
|
35
|
max@0
|
36
|
max@0
|
37 //! \brief
|
max@0
|
38 //! Immediate 'sum all values' operation for a row vector
|
max@0
|
39 template<typename eT>
|
max@0
|
40 inline
|
max@0
|
41 arma_warn_unused
|
max@0
|
42 eT
|
max@0
|
43 sum(const Row<eT>& X)
|
max@0
|
44 {
|
max@0
|
45 arma_extra_debug_sigprint();
|
max@0
|
46
|
max@0
|
47 return accu(X);
|
max@0
|
48 }
|
max@0
|
49
|
max@0
|
50
|
max@0
|
51
|
max@0
|
52 //! \brief
|
max@0
|
53 //! Immediate 'sum all values' operation for a column vector
|
max@0
|
54 template<typename eT>
|
max@0
|
55 inline
|
max@0
|
56 arma_warn_unused
|
max@0
|
57 eT
|
max@0
|
58 sum(const Col<eT>& X)
|
max@0
|
59 {
|
max@0
|
60 arma_extra_debug_sigprint();
|
max@0
|
61
|
max@0
|
62 return accu(X);
|
max@0
|
63 }
|
max@0
|
64
|
max@0
|
65
|
max@0
|
66
|
max@0
|
67 //! \brief
|
max@0
|
68 //! Immediate 'sum all values' operation,
|
max@0
|
69 //! invoked, for example, by: sum(sum(A))
|
max@0
|
70
|
max@0
|
71 template<typename T1>
|
max@0
|
72 inline
|
max@0
|
73 arma_warn_unused
|
max@0
|
74 typename T1::elem_type
|
max@0
|
75 sum(const Op<T1, op_sum>& in)
|
max@0
|
76 {
|
max@0
|
77 arma_extra_debug_sigprint();
|
max@0
|
78 arma_extra_debug_print("sum(): two consecutive sum() calls detected");
|
max@0
|
79
|
max@0
|
80 return accu(in.m);
|
max@0
|
81 }
|
max@0
|
82
|
max@0
|
83
|
max@0
|
84
|
max@0
|
85 template<typename T1>
|
max@0
|
86 arma_inline
|
max@0
|
87 const Op<Op<T1, op_sum>, op_sum>
|
max@0
|
88 sum(const Op<T1, op_sum>& in, const uword dim)
|
max@0
|
89 {
|
max@0
|
90 arma_extra_debug_sigprint();
|
max@0
|
91
|
max@0
|
92 return Op<Op<T1, op_sum>, op_sum>(in, dim, 0);
|
max@0
|
93 }
|
max@0
|
94
|
max@0
|
95
|
max@0
|
96
|
max@0
|
97 //! sum all values of a subview_row
|
max@0
|
98 template<typename eT>
|
max@0
|
99 inline
|
max@0
|
100 arma_warn_unused
|
max@0
|
101 eT
|
max@0
|
102 sum(const subview_row<eT>& X)
|
max@0
|
103 {
|
max@0
|
104 arma_extra_debug_sigprint();
|
max@0
|
105
|
max@0
|
106 return accu(X);
|
max@0
|
107 }
|
max@0
|
108
|
max@0
|
109
|
max@0
|
110
|
max@0
|
111 //! sum all values of a subview_col
|
max@0
|
112 template<typename eT>
|
max@0
|
113 inline
|
max@0
|
114 arma_warn_unused
|
max@0
|
115 eT
|
max@0
|
116 sum(const subview_col<eT>& X)
|
max@0
|
117 {
|
max@0
|
118 arma_extra_debug_sigprint();
|
max@0
|
119
|
max@0
|
120 return accu(X);
|
max@0
|
121 }
|
max@0
|
122
|
max@0
|
123
|
max@0
|
124
|
max@0
|
125 //! sum all values of a diagview
|
max@0
|
126 template<typename eT>
|
max@0
|
127 inline
|
max@0
|
128 arma_warn_unused
|
max@0
|
129 eT
|
max@0
|
130 sum(const diagview<eT>& X)
|
max@0
|
131 {
|
max@0
|
132 arma_extra_debug_sigprint();
|
max@0
|
133
|
max@0
|
134 return accu(X);
|
max@0
|
135 }
|
max@0
|
136
|
max@0
|
137
|
max@0
|
138
|
max@0
|
139 template<typename eT, typename T1>
|
max@0
|
140 inline
|
max@0
|
141 arma_warn_unused
|
max@0
|
142 eT
|
max@0
|
143 sum(const subview_elem1<eT,T1>& A)
|
max@0
|
144 {
|
max@0
|
145 arma_extra_debug_sigprint();
|
max@0
|
146
|
max@0
|
147 return accu(A);
|
max@0
|
148 }
|
max@0
|
149
|
max@0
|
150
|
max@0
|
151
|
max@0
|
152 //! @}
|