max@0
|
1 // Copyright (C) 2009-2011 NICTA (www.nicta.com.au)
|
max@0
|
2 // Copyright (C) 2009-2011 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_prod
|
max@0
|
15 //! @{
|
max@0
|
16
|
max@0
|
17
|
max@0
|
18 //! \brief
|
max@0
|
19 //! Delayed product 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 (i.e. traverse across rows)
|
max@0
|
22 //! For dim = 1, find the sum of each row (i.e. traverse across columns)
|
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_prod>
|
max@0
|
29 prod(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_prod>(X.get_ref(), dim, 0);
|
max@0
|
34 }
|
max@0
|
35
|
max@0
|
36
|
max@0
|
37
|
max@0
|
38 //! \brief
|
max@0
|
39 //! Immediate 'product of all values' operation for a row vector
|
max@0
|
40 template<typename eT>
|
max@0
|
41 inline
|
max@0
|
42 arma_warn_unused
|
max@0
|
43 eT
|
max@0
|
44 prod(const Row<eT>& X)
|
max@0
|
45 {
|
max@0
|
46 arma_extra_debug_sigprint();
|
max@0
|
47
|
max@0
|
48 return arrayops::product(X.memptr(), X.n_elem);
|
max@0
|
49 }
|
max@0
|
50
|
max@0
|
51
|
max@0
|
52
|
max@0
|
53 //! \brief
|
max@0
|
54 //! Immediate 'product of all values' operation for a column vector
|
max@0
|
55 template<typename eT>
|
max@0
|
56 inline
|
max@0
|
57 arma_warn_unused
|
max@0
|
58 eT
|
max@0
|
59 prod(const Col<eT>& X)
|
max@0
|
60 {
|
max@0
|
61 arma_extra_debug_sigprint();
|
max@0
|
62
|
max@0
|
63 return arrayops::product(X.memptr(), X.n_elem);
|
max@0
|
64 }
|
max@0
|
65
|
max@0
|
66
|
max@0
|
67
|
max@0
|
68 //! \brief
|
max@0
|
69 //! Immediate 'product of all values' operation,
|
max@0
|
70 //! invoked, for example, by: prod(prod(A))
|
max@0
|
71
|
max@0
|
72 template<typename T1>
|
max@0
|
73 inline
|
max@0
|
74 typename T1::elem_type
|
max@0
|
75 prod(const Op<T1, op_prod>& in)
|
max@0
|
76 {
|
max@0
|
77 arma_extra_debug_sigprint();
|
max@0
|
78 arma_extra_debug_print("prod(): two consecutive prod() calls detected");
|
max@0
|
79
|
max@0
|
80 typedef typename T1::elem_type eT;
|
max@0
|
81
|
max@0
|
82 const unwrap<T1> tmp(in.m);
|
max@0
|
83 const Mat<eT>& X = tmp.M;
|
max@0
|
84
|
max@0
|
85 return arrayops::product( X.memptr(), X.n_elem );
|
max@0
|
86 }
|
max@0
|
87
|
max@0
|
88
|
max@0
|
89
|
max@0
|
90 template<typename T1>
|
max@0
|
91 inline
|
max@0
|
92 const Op<Op<T1, op_prod>, op_prod>
|
max@0
|
93 prod(const Op<T1, op_prod>& in, const uword dim)
|
max@0
|
94 {
|
max@0
|
95 arma_extra_debug_sigprint();
|
max@0
|
96
|
max@0
|
97 return Op<Op<T1, op_prod>, op_prod>(in, dim, 0);
|
max@0
|
98 }
|
max@0
|
99
|
max@0
|
100
|
max@0
|
101
|
max@0
|
102 //! product of all values of a subview_row
|
max@0
|
103 template<typename eT>
|
max@0
|
104 inline
|
max@0
|
105 arma_warn_unused
|
max@0
|
106 eT
|
max@0
|
107 prod(const subview_row<eT>& S)
|
max@0
|
108 {
|
max@0
|
109 arma_extra_debug_sigprint();
|
max@0
|
110
|
max@0
|
111 const Mat<eT>& X = S.m;
|
max@0
|
112
|
max@0
|
113 const uword n_elem = S.n_elem;
|
max@0
|
114 const uword row = S.aux_row1;
|
max@0
|
115 const uword start_col = S.aux_col1;
|
max@0
|
116 const uword end_col_plus_1 = start_col + S.n_cols;
|
max@0
|
117
|
max@0
|
118 eT val = eT(1);
|
max@0
|
119
|
max@0
|
120 if(n_elem > 0)
|
max@0
|
121 {
|
max@0
|
122 for(uword col=start_col; col<end_col_plus_1; ++col)
|
max@0
|
123 {
|
max@0
|
124 val *= X.at(row,col);
|
max@0
|
125 }
|
max@0
|
126 }
|
max@0
|
127
|
max@0
|
128 return val;
|
max@0
|
129 }
|
max@0
|
130
|
max@0
|
131
|
max@0
|
132
|
max@0
|
133 //! product of all values of a subview_col
|
max@0
|
134 template<typename eT>
|
max@0
|
135 inline
|
max@0
|
136 arma_warn_unused
|
max@0
|
137 eT
|
max@0
|
138 prod(const subview_col<eT>& S)
|
max@0
|
139 {
|
max@0
|
140 arma_extra_debug_sigprint();
|
max@0
|
141
|
max@0
|
142 return (S.n_elem > 0) ? arrayops::product( S.colptr(0), S.n_rows ) : eT(1);
|
max@0
|
143 }
|
max@0
|
144
|
max@0
|
145
|
max@0
|
146
|
max@0
|
147 //! product of all values of a diagview
|
max@0
|
148 template<typename eT>
|
max@0
|
149 arma_warn_unused
|
max@0
|
150 inline
|
max@0
|
151 eT
|
max@0
|
152 prod(const diagview<eT>& X)
|
max@0
|
153 {
|
max@0
|
154 arma_extra_debug_sigprint();
|
max@0
|
155
|
max@0
|
156 const uword X_n_elem = X.n_elem;
|
max@0
|
157
|
max@0
|
158 eT val = eT(1);
|
max@0
|
159
|
max@0
|
160 for(uword i=0; i<X_n_elem; ++i)
|
max@0
|
161 {
|
max@0
|
162 val *= X[i];
|
max@0
|
163 }
|
max@0
|
164
|
max@0
|
165 return val;
|
max@0
|
166 }
|
max@0
|
167
|
max@0
|
168
|
max@0
|
169
|
max@0
|
170 template<typename eT, typename T1>
|
max@0
|
171 inline
|
max@0
|
172 arma_warn_unused
|
max@0
|
173 eT
|
max@0
|
174 prod(const subview_elem1<eT,T1>& A)
|
max@0
|
175 {
|
max@0
|
176 arma_extra_debug_sigprint();
|
max@0
|
177
|
max@0
|
178 const Col<eT> X(A);
|
max@0
|
179
|
max@0
|
180 return prod(X);
|
max@0
|
181 }
|
max@0
|
182
|
max@0
|
183
|
max@0
|
184
|
max@0
|
185 //! @}
|