glue_mixed_meat.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 glue_mixed
00018 //! @{
00019 
00020 
00021 
00022 //! matrix multiplication with different element types
00023 template<typename T1, typename T2>
00024 inline
00025 void
00026 glue_mixed_times::apply(Mat<typename eT_promoter<T1,T2>::eT>& out, const mtGlue<typename eT_promoter<T1,T2>::eT, T1, T2, glue_mixed_times>& X)
00027   {
00028   arma_extra_debug_sigprint();
00029   
00030   typedef typename T1::elem_type eT1;
00031   typedef typename T2::elem_type eT2;
00032   
00033   // TODO: extend the unwrap_check framework to handle mixed matrix types
00034   
00035   const unwrap<T1> tmp1(X.A);
00036   const unwrap<T2> tmp2(X.B);
00037   
00038   const Mat<eT1>& A = tmp1.M;
00039   const Mat<eT2>& B = tmp2.M;
00040   
00041   const bool A_is_alias = ( ((void *)&out) == ((void *)&A) );
00042   const bool B_is_alias = ( ((void *)&out) == ((void *)&B) );
00043   
00044   const Mat<eT1>* AA_ptr = A_is_alias ? new Mat<eT1>(A) : 0;
00045   const Mat<eT2>* BB_ptr = B_is_alias ? new Mat<eT2>(B) : 0;
00046   
00047   const Mat<eT1>& AA = A_is_alias ? *AA_ptr : A;
00048   const Mat<eT2>& BB = B_is_alias ? *BB_ptr : B;
00049   
00050   arma_debug_assert_mul_size(AA, BB, "matrix multiplication");
00051   
00052   out.set_size(AA.n_rows, BB.n_cols);
00053   
00054   gemm_mixed<>::apply(out, AA, BB);
00055   
00056   if(A_is_alias == true)
00057     {
00058     delete AA_ptr;
00059     }
00060   
00061   if(B_is_alias == true)
00062     {
00063     delete BB_ptr;
00064     }
00065   }
00066 
00067 
00068 
00069 //! matrix addition with different element types
00070 template<typename T1, typename T2>
00071 inline
00072 void
00073 glue_mixed_plus::apply(Mat<typename eT_promoter<T1,T2>::eT>& out, const mtGlue<typename eT_promoter<T1,T2>::eT, T1, T2, glue_mixed_plus>& X)
00074   {
00075   arma_extra_debug_sigprint();
00076   
00077   typedef typename T1::elem_type eT1;
00078   typedef typename T2::elem_type eT2;
00079   
00080   typedef typename promote_type<eT1,eT2>::result out_eT;
00081   
00082   promote_type<eT1,eT2>::check();
00083   
00084   const Proxy<T1> A(X.A);
00085   const Proxy<T2> B(X.B);
00086   
00087   arma_debug_assert_same_size(A, B, "matrix addition");
00088   
00089   out.set_size(A.n_rows, A.n_cols);
00090   
00091         out_eT* out_mem = out.memptr();
00092   const u32     n_elem  = out.n_elem;
00093   
00094   for(u32 i=0; i<n_elem; ++i)
00095     {
00096     out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) + upgrade_val<eT1,eT2>::apply(B[i]);
00097     }
00098   }
00099 
00100 
00101 
00102 //! matrix subtraction with different element types
00103 template<typename T1, typename T2>
00104 inline
00105 void
00106 glue_mixed_minus::apply(Mat<typename eT_promoter<T1,T2>::eT>& out, const mtGlue<typename eT_promoter<T1,T2>::eT, T1, T2, glue_mixed_minus>& X)
00107   {
00108   arma_extra_debug_sigprint();
00109   
00110   typedef typename T1::elem_type eT1;
00111   typedef typename T2::elem_type eT2;
00112   
00113   typedef typename promote_type<eT1,eT2>::result out_eT;
00114   
00115   promote_type<eT1,eT2>::check();
00116   
00117   const Proxy<T1> A(X.A);
00118   const Proxy<T2> B(X.B);
00119   
00120   arma_debug_assert_same_size(A, B, "matrix subtraction");
00121   
00122   out.set_size(A.n_rows, A.n_cols);
00123   
00124         out_eT* out_mem = out.memptr();
00125   const u32     n_elem  = out.n_elem;
00126   
00127   for(u32 i=0; i<n_elem; ++i)
00128     {
00129     out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) - upgrade_val<eT1,eT2>::apply(B[i]);
00130     }
00131   }
00132 
00133 
00134 
00135 //! element-wise matrix division with different element types
00136 template<typename T1, typename T2>
00137 inline
00138 void
00139 glue_mixed_div::apply(Mat<typename eT_promoter<T1,T2>::eT>& out, const mtGlue<typename eT_promoter<T1,T2>::eT, T1, T2, glue_mixed_div>& X)
00140   {
00141   arma_extra_debug_sigprint();
00142   
00143   typedef typename T1::elem_type eT1;
00144   typedef typename T2::elem_type eT2;
00145   
00146   typedef typename promote_type<eT1,eT2>::result out_eT;
00147   
00148   promote_type<eT1,eT2>::check();
00149   
00150   const Proxy<T1> A(X.A);
00151   const Proxy<T2> B(X.B);
00152   
00153   arma_debug_assert_same_size(A, B, "element-wise matrix division");
00154   
00155   out.set_size(A.n_rows, A.n_cols);
00156   
00157         out_eT* out_mem = out.memptr();
00158   const u32     n_elem  = out.n_elem;
00159   
00160   for(u32 i=0; i<n_elem; ++i)
00161     {
00162     out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) / upgrade_val<eT1,eT2>::apply(B[i]);
00163     }
00164   }
00165 
00166 
00167 
00168 //! element-wise matrix multiplication with different element types
00169 template<typename T1, typename T2>
00170 inline
00171 void
00172 glue_mixed_schur::apply(Mat<typename eT_promoter<T1,T2>::eT>& out, const mtGlue<typename eT_promoter<T1,T2>::eT, T1, T2, glue_mixed_schur>& X)
00173   {
00174   arma_extra_debug_sigprint();
00175   
00176   typedef typename T1::elem_type eT1;
00177   typedef typename T2::elem_type eT2;
00178   
00179   typedef typename promote_type<eT1,eT2>::result out_eT;
00180   
00181   promote_type<eT1,eT2>::check();
00182   
00183   const Proxy<T1> A(X.A);
00184   const Proxy<T2> B(X.B);
00185   
00186   arma_debug_assert_same_size(A, B, "element-wise matrix multiplication");
00187   
00188   out.set_size(A.n_rows, A.n_cols);
00189   
00190         out_eT* out_mem = out.memptr();
00191   const u32     n_elem  = out.n_elem;
00192   
00193   for(u32 i=0; i<n_elem; ++i)
00194     {
00195     out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) * upgrade_val<eT1,eT2>::apply(B[i]);
00196     }
00197   }
00198 
00199 
00200 
00201 //! @}