comparison armadillo-2.4.4/include/armadillo_bits/fn_prod.hpp @ 0:8b6102e2a9b0

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