fn_prod.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2010 NICTA and the authors listed below
00002 // http://nicta.com.au
00003 // 
00004 // Authors:
00005 // - Conrad Sanderson (conradsand at ieee dot org)
00006 // 
00007 // This file is part of the Armadillo C++ library.
00008 // It is provided without any warranty of fitness
00009 // for any purpose. You can redistribute this file
00010 // and/or modify it under the terms of the GNU
00011 // Lesser General Public License (LGPL) as published
00012 // by the Free Software Foundation, either version 3
00013 // of the License or (at your option) any later version.
00014 // (see http://www.opensource.org/licenses for more info)
00015 
00016 
00017 //! \addtogroup fn_prod
00018 //! @{
00019 
00020 
00021 //! \brief
00022 //! Delayed product of elements of a matrix along a specified dimension (either rows or columns).
00023 //! The result is stored in a dense matrix that has either one column or one row.
00024 //! For dim = 0, find the sum of each column (i.e. traverse across rows)
00025 //! For dim = 1, find the sum of each row (i.e. traverse across columns)
00026 //! The default is dim = 0.
00027 //! NOTE: this function works differently than in Matlab/Octave.
00028 
00029 template<typename T1>
00030 arma_inline
00031 const Op<T1, op_prod>
00032 prod(const Base<typename T1::elem_type,T1>& X, const u32 dim = 0)
00033   {
00034   arma_extra_debug_sigprint();
00035   
00036   return Op<T1, op_prod>(X.get_ref(), dim, 0);
00037   }
00038 
00039 
00040 
00041 //! \brief
00042 //! Immediate 'product of all values' operation for a row vector
00043 template<typename eT>
00044 inline
00045 eT
00046 prod(const Row<eT>& X)
00047   {
00048   arma_extra_debug_sigprint();
00049   
00050   arma_debug_check( (X.n_elem < 1), "prod(): given object has no elements" );
00051   
00052   const u32 n_elem = X.n_elem;
00053   const eT* X_mem  = X.memptr();
00054   
00055   eT val = X_mem[0];
00056   
00057   for(u32 i=1; i<n_elem; ++i)
00058     {
00059     val *= X_mem[i];
00060     }
00061   
00062   return val;
00063   }
00064 
00065 
00066 
00067 //! \brief
00068 //! Immediate 'product of all values' operation for a column vector
00069 template<typename eT>
00070 inline
00071 eT
00072 prod(const Col<eT>& X)
00073   {
00074   arma_extra_debug_sigprint();
00075   
00076   arma_debug_check( (X.n_elem < 1), "prod(): given object has no elements" );
00077   
00078   const u32 n_elem = X.n_elem;
00079   const eT* X_mem  = X.memptr();
00080   
00081   eT val = X_mem[0];
00082   
00083   for(u32 i=1; i<n_elem; ++i)
00084     {
00085     val *= X_mem[i];
00086     }
00087   
00088   return val;
00089   }
00090 
00091 
00092 
00093 //! \brief
00094 //! Immediate 'product of all values' operation,
00095 //! invoked, for example, by: prod(prod(A))
00096 
00097 template<typename T1>
00098 inline
00099 typename T1::elem_type
00100 prod(const Op<T1, op_prod>& in)
00101   {
00102   arma_extra_debug_sigprint();
00103   arma_extra_debug_print("prod(): two consecutive prod() calls detected");
00104   
00105   typedef typename T1::elem_type eT;
00106   
00107   const unwrap<T1>   tmp(in.m);
00108   const Mat<eT>& X = tmp.M;
00109   
00110   arma_debug_check( (X.n_elem < 1), "prod(): given object has no elements" );
00111   
00112   const u32 n_elem = X.n_elem;
00113   const eT* X_mem  = X.memptr();
00114   
00115   eT val = X_mem[0];
00116   
00117   for(u32 i=1; i<n_elem; ++i)
00118     {
00119     val *= X_mem[i];
00120     }
00121   
00122   return val;
00123   }
00124 
00125 
00126 
00127 template<typename T1>
00128 inline
00129 const Op<Op<T1, op_prod>, op_prod>
00130 prod(const Op<T1, op_prod>& in, const u32 dim)
00131   {
00132   arma_extra_debug_sigprint();
00133   
00134   return Op<Op<T1, op_prod>, op_prod>(in, dim, 0);
00135   }
00136 
00137 
00138 
00139 //! product of all values of a subview_row
00140 template<typename eT>
00141 inline
00142 eT
00143 prod(const subview_row<eT>& S)
00144   {
00145   arma_extra_debug_sigprint();
00146   
00147   arma_debug_check( (S.n_elem < 1), "prod(): given object has no elements" );
00148   
00149   const Mat<eT>& X = S.m;
00150   
00151   const u32 row       = S.aux_row1;
00152   const u32 start_col = S.aux_col1;
00153   const u32 end_col   = S.aux_col2;
00154   
00155   eT val = X.at(row,start_col);
00156   
00157   for(u32 col=start_col+1; col<=end_col; ++col)
00158     {
00159     val *= X.at(row,col);
00160     }
00161   
00162   return val;
00163   }
00164 
00165 
00166 
00167 //! product of all values of a subview_col
00168 template<typename eT>
00169 inline
00170 eT
00171 prod(const subview_col<eT>& S)
00172   {
00173   arma_extra_debug_sigprint();
00174   
00175   arma_debug_check( (S.n_elem < 1), "prod(): given object has no elements" );
00176   
00177   const eT* S_colptr = S.colptr(0);
00178   const u32 n_rows   = S.n_rows;
00179   
00180   eT val = S_colptr[0];
00181   
00182   for(u32 row=1; row<n_rows; ++row)
00183     {
00184     val *= S_colptr[row];
00185     }
00186   
00187   return val;
00188   }
00189 
00190 
00191 
00192 //! product of all values of a diagview
00193 template<typename eT>
00194 inline
00195 eT
00196 prod(const diagview<eT>& X)
00197   {
00198   arma_extra_debug_sigprint();
00199   
00200   arma_debug_check( (X.n_elem < 1), "prod(): given object has no elements" );
00201   
00202   const u32 n_elem = X.n_elem;
00203   
00204   eT val = X[0];
00205   
00206   for(u32 i=1; i<n_elem; ++i)
00207     {
00208     val *= X[i];
00209     }
00210   
00211   return val;
00212   }
00213 
00214 
00215 
00216 //! @}