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 op_prod
|
max@0
|
15 //! @{
|
max@0
|
16
|
max@0
|
17 //! \brief
|
max@0
|
18 //! Immediate product of elements of a matrix along a specified dimension (either rows or columns).
|
max@0
|
19 //! The result is stored in a dense matrix that has either one column or one row.
|
max@0
|
20 //! See the prod() function for more details.
|
max@0
|
21 template<typename T1>
|
max@0
|
22 inline
|
max@0
|
23 void
|
max@0
|
24 op_prod::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_prod>& in)
|
max@0
|
25 {
|
max@0
|
26 arma_extra_debug_sigprint();
|
max@0
|
27
|
max@0
|
28 typedef typename T1::elem_type eT;
|
max@0
|
29
|
max@0
|
30 const uword dim = in.aux_uword_a;
|
max@0
|
31 arma_debug_check( (dim > 1), "prod(): incorrect usage. dim must be 0 or 1");
|
max@0
|
32
|
max@0
|
33 const unwrap_check<T1> tmp(in.m, out);
|
max@0
|
34 const Mat<eT>& X = tmp.M;
|
max@0
|
35
|
max@0
|
36 const uword X_n_rows = X.n_rows;
|
max@0
|
37 const uword X_n_cols = X.n_cols;
|
max@0
|
38
|
max@0
|
39 if(dim == 0) // traverse across rows (i.e. find the product in each column)
|
max@0
|
40 {
|
max@0
|
41 out.set_size(1, X_n_cols);
|
max@0
|
42
|
max@0
|
43 eT* out_mem = out.memptr();
|
max@0
|
44
|
max@0
|
45 for(uword col=0; col<X_n_cols; ++col)
|
max@0
|
46 {
|
max@0
|
47 out_mem[col] = arrayops::product(X.colptr(col), X_n_rows);
|
max@0
|
48 }
|
max@0
|
49 }
|
max@0
|
50 else // traverse across columns (i.e. find the product in each row)
|
max@0
|
51 {
|
max@0
|
52 out.set_size(X_n_rows, 1);
|
max@0
|
53
|
max@0
|
54 eT* out_mem = out.memptr();
|
max@0
|
55
|
max@0
|
56 for(uword row=0; row<X_n_rows; ++row)
|
max@0
|
57 {
|
max@0
|
58 eT val = eT(1);
|
max@0
|
59
|
max@0
|
60 for(uword col=0; col<X_n_cols; ++col)
|
max@0
|
61 {
|
max@0
|
62 val *= X.at(row,col);
|
max@0
|
63 }
|
max@0
|
64
|
max@0
|
65 out_mem[row] = val;
|
max@0
|
66 }
|
max@0
|
67 }
|
max@0
|
68 }
|
max@0
|
69
|
max@0
|
70
|
max@0
|
71
|
max@0
|
72 //! @}
|