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 T1>
00059 inline
00060 void
00061 op_rand::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_rand>& in)
00062   {
00063   arma_extra_debug_sigprint();
00064   
00065   out.set_size(in.aux_u32_a, in.aux_u32_b);
00066   
00067   op_rand::direct_rand(out.memptr(), out.n_elem);
00068   }
00069 
00070 
00071 
00072 template<typename eT>
00073 inline
00074 void
00075 op_rand::apply(Cube<eT>& out, const OpCube<Cube<eT>,op_rand>& in)
00076   {
00077   arma_extra_debug_sigprint();
00078   
00079   out.set_size(in.aux_u32_a, in.aux_u32_b, in.aux_u32_c);
00080   
00081   op_rand::direct_rand(out.memptr(), out.n_elem);
00082   }
00083 
00084 
00085 
00086 //! @}