operator_plus.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 operator_plus
00018 //! @{
00019 
00020 
00021 
00022 //! unary plus operation (does nothing, but is required for completeness)
00023 template<typename T1>
00024 arma_inline
00025 const Base<typename T1::elem_type,T1>&
00026 operator+
00027 (const Base<typename T1::elem_type,T1>& X)
00028   {
00029   arma_extra_debug_sigprint();
00030   
00031   return X;
00032   }
00033 
00034 
00035 
00036 //! Base + scalar
00037 template<typename T1>
00038 arma_inline
00039 const eOp<T1, eop_scalar_plus>
00040 operator+
00041 (const Base<typename T1::elem_type,T1>& X, const typename T1::elem_type k)
00042   {
00043   arma_extra_debug_sigprint();
00044   
00045   return eOp<T1, eop_scalar_plus>(X.get_ref(), k);
00046   }
00047 
00048 
00049 
00050 //! scalar + Base
00051 template<typename T1>
00052 arma_inline
00053 const eOp<T1, eop_scalar_plus>
00054 operator+
00055 (const typename T1::elem_type k, const Base<typename T1::elem_type,T1>& X)
00056   {
00057   arma_extra_debug_sigprint();
00058   
00059   return eOp<T1, eop_scalar_plus>(X.get_ref(), k);  // NOTE: order is swapped
00060   }
00061 
00062 
00063 
00064 //! non-complex Base + complex scalar (experimental)
00065 template<typename T1>
00066 arma_inline
00067 Mat<typename std::complex<typename T1::pod_type> >
00068 operator+
00069   (
00070   const Base<typename T1::pod_type, T1>&     X,
00071   const std::complex<typename T1::pod_type>& k
00072   )
00073   {
00074   arma_extra_debug_sigprint();
00075   
00076   typedef typename std::complex<typename T1::pod_type> eT;
00077   typedef typename T1::pod_type                         T;
00078   
00079   const Proxy<T1> A(X.get_ref());
00080   
00081   Mat<eT> out(A.n_rows, A.n_cols);
00082   
00083   const u32 n_elem  = A.n_elem;
00084         eT* out_mem = out.memptr();
00085   
00086   for(u32 i=0; i<n_elem; ++i)
00087     {
00088     out_mem[i] = A[i] + k;
00089     }
00090   
00091   return out;
00092   }
00093 
00094 
00095 
00096 //! complex scalar + non-complex Base (experimental)
00097 template<typename T1>
00098 arma_inline
00099 Mat<typename std::complex<typename T1::pod_type> >
00100 operator+
00101   (
00102   const std::complex<typename T1::pod_type>& k,
00103   const Base<typename T1::pod_type, T1>&     X
00104   )
00105   {
00106   arma_extra_debug_sigprint();
00107   
00108   typedef typename std::complex<typename T1::pod_type> eT;
00109   typedef typename T1::pod_type                         T;
00110   
00111   const Proxy<T1> A(X.get_ref());
00112   
00113   Mat<eT> out(A.n_rows, A.n_cols);
00114   
00115   const u32 n_elem  = A.n_elem;
00116         eT* out_mem = out.memptr();
00117   
00118   for(u32 i=0; i<n_elem; ++i)
00119     {
00120     out_mem[i] = k + A[i];
00121     }
00122   
00123   return out;
00124   }
00125 
00126 
00127 
00128 //! addition of Base objects with same element type
00129 template<typename T1, typename T2>
00130 arma_inline
00131 const eGlue<T1, T2, eglue_plus>
00132 operator+
00133   (
00134   const Base<typename T1::elem_type,T1>& X,
00135   const Base<typename T1::elem_type,T2>& Y
00136   )
00137   {
00138   arma_extra_debug_sigprint();
00139   
00140   return eGlue<T1, T2, eglue_plus>(X.get_ref(), Y.get_ref());
00141   }
00142 
00143 
00144 
00145 //! addition of Base objects with different element types
00146 template<typename T1, typename T2>
00147 arma_inline
00148 Mat<typename promote_type<typename T1::elem_type, typename T2::elem_type>::result>
00149 operator+
00150   (
00151   const Base< typename force_different_type<typename T1::elem_type, typename T2::elem_type>::T1_result, T1>& X,
00152   const Base< typename force_different_type<typename T1::elem_type, typename T2::elem_type>::T2_result, T2>& Y
00153   )
00154   {
00155   arma_extra_debug_sigprint();
00156   
00157   typedef typename T1::elem_type eT1;
00158   typedef typename T2::elem_type eT2;
00159   
00160   typedef typename promote_type<eT1,eT2>::result out_eT;
00161   
00162   promote_type<eT1,eT2>::check();
00163   
00164   const Proxy<T1> A(X.get_ref());
00165   const Proxy<T2> B(Y.get_ref());
00166   
00167   arma_debug_assert_same_size(A, B, "matrix addition");
00168   
00169   Mat<out_eT> out(A.n_rows, A.n_cols);
00170 
00171         out_eT* out_mem = out.memptr();
00172   const u32     n_elem  = out.n_elem;
00173   
00174   for(u32 i=0; i<n_elem; ++i)
00175     {
00176     out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) + upgrade_val<eT1,eT2>::apply(B[i]);
00177     }
00178   
00179   return out;
00180   }
00181 
00182 
00183 
00184 //! @}