op_rand_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 //! \addtogroup op_rand
00017 //! @{
00018 
00019 
00020 //! TODO: optionally use the Marsenne-Twister random number generator (see Boost)
00021 template<typename eT>
00022 inline
00023 void
00024 op_rand::direct_rand(eT* x, const u32 n_elem)
00025   {
00026   arma_extra_debug_sigprint();
00027   
00028   for(u32 i=0; i<n_elem; ++i)
00029     {
00030     x[i] = eT(std::rand()) / eT(RAND_MAX);
00031     }
00032   
00033   
00034   }
00035 
00036 
00037 
00038 template<typename T>
00039 inline
00040 void
00041 op_rand::direct_rand(std::complex<T>* x, const u32 n_elem)
00042   {
00043   arma_extra_debug_sigprint();
00044   
00045   for(u32 i=0; i<n_elem; ++i)
00046     {
00047     const T a = T(std::rand()) / T(RAND_MAX);
00048     const T b = T(std::rand()) / T(RAND_MAX);
00049 
00050     x[i] = std::complex<T>(a,b);
00051     }
00052   
00053   
00054   }
00055 
00056 
00057 
00058 template<typename eT>
00059 inline
00060 void
00061 op_rand::apply(Mat<eT>& out, const Op<Mat<eT>,op_rand>& in)
00062   {
00063   arma_extra_debug_sigprint();
00064   
00065   const u32 n_rows = in.aux_u32_a;
00066   const u32 n_cols = (in.aux_u32_b > 0) ? in.aux_u32_b : 1;
00067   
00068   out.set_size(n_rows, n_cols);
00069   
00070   op_rand::direct_rand(out.memptr(), out.n_elem);
00071   }
00072 
00073 
00074 
00075 template<typename eT>
00076 inline
00077 void
00078 op_rand::apply(Mat<eT>& out, const Op<Col<eT>,op_rand>& in)
00079   {
00080   arma_extra_debug_sigprint();
00081   
00082   out.set_size(in.aux_u32_a, 1);
00083   
00084   op_rand::direct_rand(out.memptr(), out.n_elem);
00085   }
00086 
00087 
00088 
00089 template<typename eT>
00090 inline
00091 void
00092 op_rand::apply(Mat<eT>& out, const Op<Row<eT>,op_rand>& in)
00093   {
00094   arma_extra_debug_sigprint();
00095   
00096   out.set_size(1, in.aux_u32_a);
00097   
00098   op_rand::direct_rand(out.memptr(), out.n_elem);
00099   }
00100 
00101 
00102 
00103 template<typename eT>
00104 inline
00105 void
00106 op_rand::apply(Col<eT>& out, const Op<Col<eT>,op_rand>& in)
00107   {
00108   arma_extra_debug_sigprint();
00109   
00110   out.set_size(in.aux_u32_a);
00111   
00112   op_rand::direct_rand(out.memptr(), out.n_elem);
00113   }
00114 
00115 
00116 
00117 template<typename eT>
00118 inline
00119 void
00120 op_rand::apply(Row<eT>& out, const Op<Row<eT>,op_rand>& in)
00121   {
00122   arma_extra_debug_sigprint();
00123   
00124   out.set_size(in.aux_u32_a);
00125   
00126   op_rand::direct_rand(out.memptr(), out.n_elem);
00127   }
00128 
00129 
00130 //! @}