max@0: // Copyright (C) 2009-2011 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2009-2011 Conrad Sanderson max@0: // max@0: // This file is part of the Armadillo C++ library. max@0: // It is provided without any warranty of fitness max@0: // for any purpose. You can redistribute this file max@0: // and/or modify it under the terms of the GNU max@0: // Lesser General Public License (LGPL) as published max@0: // by the Free Software Foundation, either version 3 max@0: // of the License or (at your option) any later version. max@0: // (see http://www.opensource.org/licenses for more info) max@0: max@0: max@0: //! \addtogroup fn_prod max@0: //! @{ max@0: max@0: max@0: //! \brief max@0: //! Delayed product of elements of a matrix along a specified dimension (either rows or columns). max@0: //! The result is stored in a dense matrix that has either one column or one row. max@0: //! For dim = 0, find the sum of each column (i.e. traverse across rows) max@0: //! For dim = 1, find the sum of each row (i.e. traverse across columns) max@0: //! The default is dim = 0. max@0: //! NOTE: this function works differently than in Matlab/Octave. max@0: max@0: template max@0: arma_inline max@0: const Op max@0: prod(const Base& X, const uword dim = 0) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: return Op(X.get_ref(), dim, 0); max@0: } max@0: max@0: max@0: max@0: //! \brief max@0: //! Immediate 'product of all values' operation for a row vector max@0: template max@0: inline max@0: arma_warn_unused max@0: eT max@0: prod(const Row& X) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: return arrayops::product(X.memptr(), X.n_elem); max@0: } max@0: max@0: max@0: max@0: //! \brief max@0: //! Immediate 'product of all values' operation for a column vector max@0: template max@0: inline max@0: arma_warn_unused max@0: eT max@0: prod(const Col& X) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: return arrayops::product(X.memptr(), X.n_elem); max@0: } max@0: max@0: max@0: max@0: //! \brief max@0: //! Immediate 'product of all values' operation, max@0: //! invoked, for example, by: prod(prod(A)) max@0: max@0: template max@0: inline max@0: typename T1::elem_type max@0: prod(const Op& in) max@0: { max@0: arma_extra_debug_sigprint(); max@0: arma_extra_debug_print("prod(): two consecutive prod() calls detected"); max@0: max@0: typedef typename T1::elem_type eT; max@0: max@0: const unwrap tmp(in.m); max@0: const Mat& X = tmp.M; max@0: max@0: return arrayops::product( X.memptr(), X.n_elem ); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: const Op, op_prod> max@0: prod(const Op& in, const uword dim) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: return Op, op_prod>(in, dim, 0); max@0: } max@0: max@0: max@0: max@0: //! product of all values of a subview_row max@0: template max@0: inline max@0: arma_warn_unused max@0: eT max@0: prod(const subview_row& S) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const Mat& X = S.m; max@0: max@0: const uword n_elem = S.n_elem; max@0: const uword row = S.aux_row1; max@0: const uword start_col = S.aux_col1; max@0: const uword end_col_plus_1 = start_col + S.n_cols; max@0: max@0: eT val = eT(1); max@0: max@0: if(n_elem > 0) max@0: { max@0: for(uword col=start_col; col max@0: inline max@0: arma_warn_unused max@0: eT max@0: prod(const subview_col& S) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: return (S.n_elem > 0) ? arrayops::product( S.colptr(0), S.n_rows ) : eT(1); max@0: } max@0: max@0: max@0: max@0: //! product of all values of a diagview max@0: template max@0: arma_warn_unused max@0: inline max@0: eT max@0: prod(const diagview& X) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const uword X_n_elem = X.n_elem; max@0: max@0: eT val = eT(1); max@0: max@0: for(uword i=0; i max@0: inline max@0: arma_warn_unused max@0: eT max@0: prod(const subview_elem1& A) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const Col X(A); max@0: max@0: return prod(X); max@0: } max@0: max@0: max@0: max@0: //! @}