op_misc_meat.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2009 NICTA
00002 // 
00003 // Authors:
00004 // - Conrad Sanderson (conradsand at ieee dot org)
00005 // 
00006 // This file is part of the Armadillo C++ library.
00007 // It is provided without any warranty of fitness
00008 // for any purpose. You can redistribute this file
00009 // and/or modify it under the terms of the GNU
00010 // Lesser General Public License (LGPL) as published
00011 // by the Free Software Foundation, either version 3
00012 // of the License or (at your option) any later version.
00013 // (see http://www.opensource.org/licenses for more info)
00014 
00015 
00016 
00017 //! \addtogroup op_misc
00018 //! @{
00019 
00020 
00021 template<typename T1>
00022 inline
00023 void
00024 op_log::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_log>& in)
00025   {
00026   arma_extra_debug_sigprint();
00027   
00028   typedef typename T1::elem_type eT;
00029   
00030   const unwrap<T1> tmp(in.m);
00031   
00032   const Mat<eT>& A = tmp.M;
00033   const u32 n_elem       = A.n_elem;
00034   
00035   out.set_size(A.n_rows, A.n_cols);
00036   eT* out_ptr = out.memptr();
00037   
00038   for(u32 i=0; i<n_elem; ++i)
00039     {
00040     out_ptr[i] = std::log(A.mem[i]);
00041     }
00042   
00043   }
00044   
00045   
00046 
00047 template<typename T1>
00048 inline
00049 void
00050 op_trunc_log::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_trunc_log>& in)
00051   {
00052   arma_extra_debug_sigprint();
00053   
00054   typedef typename T1::elem_type eT;
00055   
00056   const unwrap<T1> tmp(in.m);
00057   
00058   const Mat<eT>& A = tmp.M;
00059   const u32 n_elem       = A.n_elem;
00060   
00061   out.set_size(A.n_rows, A.n_cols);
00062   eT* out_ptr = out.memptr();
00063   
00064   for(u32 i=0; i<n_elem; ++i)
00065     {
00066     out_ptr[i] = trunc_log(A.mem[i]);
00067     }
00068   
00069   }
00070   
00071   
00072 
00073 template<typename T1>
00074 inline
00075 void
00076 op_log10::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_log10>& in)
00077   {
00078   arma_extra_debug_sigprint();
00079   
00080   typedef typename T1::elem_type eT;
00081   
00082   const unwrap<T1> tmp(in.m);
00083   
00084   const Mat<eT>& A = tmp.M;
00085   const u32 n_elem       = A.n_elem;
00086   
00087   out.set_size(A.n_rows, A.n_cols);
00088   eT* out_ptr = out.memptr();
00089   
00090   for(u32 i=0; i<n_elem; ++i)
00091     {
00092     out_ptr[i] = std::log10(A.mem[i]);
00093     }
00094   
00095   }
00096   
00097   
00098 
00099 template<typename T1>
00100 inline
00101 void
00102 op_exp::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_exp>& in)
00103   {
00104   arma_extra_debug_sigprint();
00105   
00106   typedef typename T1::elem_type eT;
00107   
00108   const unwrap<T1> tmp(in.m);
00109   
00110   const Mat<eT>& A = tmp.M;
00111   const u32 n_elem       = A.n_elem;
00112   
00113   out.set_size(A.n_rows, A.n_cols);
00114   eT* out_ptr = out.memptr();
00115   
00116   for(u32 i=0; i<n_elem; ++i)
00117     {
00118     out_ptr[i] = std::exp(A.mem[i]);
00119     }
00120   
00121   }
00122   
00123   
00124 
00125 template<typename T1>
00126 inline
00127 void
00128 op_trunc_exp::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_trunc_exp>& in)
00129   {
00130   arma_extra_debug_sigprint();
00131   
00132   typedef typename T1::elem_type eT;
00133   
00134   const unwrap<T1> tmp(in.m);
00135   
00136   const Mat<eT>& A = tmp.M;
00137   const u32 n_elem       = A.n_elem;
00138   
00139   out.set_size(A.n_rows, A.n_cols);
00140   eT* out_ptr = out.memptr();
00141   
00142   for(u32 i=0; i<n_elem; ++i)
00143     {
00144     out_ptr[i] = trunc_exp(A.mem[i]);
00145     }
00146   
00147   }
00148 
00149 
00150 
00151 template<typename T1>
00152 inline
00153 void
00154 op_sqrt::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_sqrt>& in)
00155   {
00156   arma_extra_debug_sigprint();
00157   
00158   typedef typename T1::elem_type eT;
00159   
00160   const unwrap<T1> tmp(in.m);
00161   
00162   const Mat<eT>& A = tmp.M;
00163   const u32 n_elem       = A.n_elem;
00164   
00165   out.set_size(A.n_rows, A.n_cols);
00166   eT* out_ptr = out.memptr();
00167   
00168   for(u32 i=0; i<n_elem; ++i)
00169     {
00170     out_ptr[i] = std::sqrt(A.mem[i]);
00171     }
00172   
00173   }
00174 
00175 
00176 
00177 template<typename T1>
00178 inline
00179 void
00180 op_square::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_square>& in)
00181   {
00182   arma_extra_debug_sigprint();
00183   
00184   typedef typename T1::elem_type eT;
00185   
00186   const unwrap<T1> tmp(in.m);
00187   
00188   const Mat<eT>& A = tmp.M;
00189   const u32 n_elem = A.n_elem;
00190   
00191   out.set_size(A.n_rows, A.n_cols);
00192   eT* out_ptr = out.memptr();
00193   
00194   for(u32 i=0; i<n_elem; ++i)
00195     {
00196     const eT tmp_val = A.mem[i];
00197     out_ptr[i] = tmp_val*tmp_val;
00198     }
00199   
00200   }
00201   
00202   
00203 
00204 template<typename T1>
00205 inline
00206 void
00207 op_pow::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_pow>& in)
00208   {
00209   arma_extra_debug_sigprint();
00210   
00211   typedef typename T1::elem_type eT;
00212   
00213   const unwrap<T1> tmp(in.m);
00214   
00215   const Mat<eT>& A = tmp.M;
00216   const u32 n_elem       = A.n_elem;
00217   
00218   out.set_size(A.n_rows, A.n_cols);
00219   eT* out_ptr = out.memptr();
00220   
00221   for(u32 i=0; i<n_elem; ++i)
00222     {
00223     out_ptr[i] = std::pow(A.mem[i], in.aux);
00224     }
00225   
00226   }
00227 
00228 
00229 
00230 template<typename T, typename T1>
00231 inline
00232 void
00233 op_pow::apply(Mat< std::complex<T> >& out, const Op<T1,op_pow>& in)
00234   {
00235   arma_extra_debug_sigprint();
00236   
00237   typedef std::complex<T> eT;
00238   
00239   isnt_same_type<eT, typename T1::elem_type>::check();
00240   
00241   const unwrap<T1> tmp(in.m);
00242   
00243   const Mat<eT>& A = tmp.M;
00244   const u32 n_elem       = A.n_elem;
00245   
00246   out.set_size(A.n_rows, A.n_cols);
00247   eT* out_ptr = out.memptr();
00248   
00249   
00250   if(in.aux.imag() == T(0))
00251     {
00252     const T in_aux_real = in.aux.real();
00253     
00254     for(u32 i=0; i<n_elem; ++i)
00255       {
00256       out_ptr[i] = std::pow(A.mem[i], in_aux_real);
00257       }
00258     }
00259   else
00260     {
00261     for(u32 i=0; i<n_elem; ++i)
00262       {
00263       out_ptr[i] = std::pow(A.mem[i], in.aux);
00264       }
00265     }
00266   }
00267 
00268 
00269 
00270 template<typename T1>
00271 inline
00272 T1 
00273 op_pow_s32::internal_pow(const T1 base, const int exponent)
00274   {
00275   return std::pow(base, exponent);
00276   }
00277 
00278 
00279 
00280 inline
00281 char
00282 op_pow_s32::internal_pow(const char base, const int exponent)
00283   {
00284   typedef char out_type;
00285   return out_type( std::pow(double(base), exponent) );
00286   }
00287 
00288 
00289 
00290 inline
00291 unsigned char
00292 op_pow_s32::internal_pow(const unsigned char base, const int exponent)
00293   {
00294   typedef unsigned char out_type;
00295   return out_type( std::pow(double(base), exponent) );
00296   }
00297 
00298 
00299 
00300 inline
00301 int
00302 op_pow_s32::internal_pow(const int base, const int exponent)
00303   {
00304   typedef int out_type;
00305   return out_type( std::pow(double(base), exponent) );
00306   }
00307 
00308 
00309 
00310 inline
00311 unsigned int
00312 op_pow_s32::internal_pow(const unsigned int base, const int exponent)
00313   {
00314   typedef unsigned int out_type;
00315   return out_type( std::pow(double(base), exponent) );
00316   }
00317 
00318 
00319 
00320 template<typename T1>
00321 inline
00322 void
00323 op_pow_s32::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_pow_s32>& in)
00324   {
00325   arma_extra_debug_sigprint();
00326   
00327   typedef typename T1::elem_type eT;
00328   
00329   const unwrap<T1> tmp(in.m);
00330   
00331   const Mat<eT>& A = tmp.M;
00332   const u32 n_elem       = A.n_elem;
00333   
00334   out.set_size(A.n_rows, A.n_cols);
00335   eT* out_ptr = out.memptr();
00336   
00337   const int exponent = (in.aux_u32_b == 0) ? in.aux_u32_a : -in.aux_u32_a;
00338   
00339   for(u32 i=0; i<n_elem; ++i)
00340     {
00341     //out_ptr[i] = std::pow(A.mem[i], exponent);  // causes problems with gcc 4.1/4.2 for base that has an integer type
00342     out_ptr[i] = op_pow_s32::internal_pow(A.mem[i], exponent);
00343     }
00344   
00345   }
00346 
00347 
00348 
00349 template<typename T1>
00350 inline
00351 void
00352 op_conj::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_conj>& in)
00353   {
00354   arma_extra_debug_sigprint();
00355   
00356   typedef typename T1::elem_type eT;
00357 
00358   arma_type_check< is_complex<T1>::value == true >::apply();
00359   
00360   const unwrap<T1> tmp(in.m);
00361   
00362   const Mat<eT>& A = tmp.M;
00363   const u32 n_elem       = A.n_elem;
00364   
00365   out.set_size(A.n_rows, A.n_cols);
00366   eT* out_ptr = out.memptr();
00367   
00368   for(u32 i=0; i<n_elem; ++i)
00369     {
00370     out_ptr[i] = std::conj(A.mem[i]);
00371     }
00372   
00373   }
00374   
00375   
00376 
00377 //! @}