ivl 679
ivl/details/mfunc/rand.hpp
00001 /* This file is part of the ivl C++ library <http://image.ntua.gr/ivl>.
00002    A C++ template library extending syntax towards mathematical notation.
00003 
00004    Copyright (C) 2012 Yannis Avrithis <iavr@image.ntua.gr>
00005    Copyright (C) 2012 Kimon Kontosis <kimonas@image.ntua.gr>
00006 
00007    ivl is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU Lesser General Public License 
00009    version 3 as published by the Free Software Foundation.
00010 
00011    Alternatively, you can redistribute it and/or modify it under the terms 
00012    of the GNU General Public License version 2 as published by the Free 
00013    Software Foundation.
00014 
00015    ivl is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00018    See the GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License 
00021    and a copy of the GNU Lesser General Public License along 
00022    with ivl. If not, see <http://www.gnu.org/licenses/>. */
00023 
00024 #ifndef IVL_MFUNC_RAND
00025 #define IVL_MFUNC_RAND
00026 
00027 namespace ivl {
00028 
00029 namespace core_details {
00030 
00031 template <class J, class J1, class J2>
00032 struct rand_
00033 {
00034         typedef types::number<0> cost;
00035         static inline double elem_op(double min, double max)
00036         {
00037                 return ivl::math::rand(min, max);
00038         }
00039 };
00040 
00041 } // namespace core_details
00042 
00043 static __attribute__ ((unused)) struct rand_impl : public ivl_func<rand_impl>,
00044         public binary_elem_base<double, double, double, core_details::rand_>
00045 {
00046         typedef binary_elem_base<double, double, double, core_details::rand_> rand_base;
00047 
00048         using rand_base::operator[];
00049         //using randb::operator();
00050 
00051         
00053         array<double> operator()(size_t s)
00054         {
00055                 array<double> c(s);
00056                 for (size_t i = 0; i < c.length(); i++)
00057                         c[i] = std::rand() / (double)RAND_MAX;
00058                 return c;
00059         }
00060         
00062         template<class T>
00063         array<T> operator()(size_t s, const T& from, const T& to)
00064         {
00065                 CHECK(to >= from, ecomp); // TODO: different kind of exception here...
00066 
00067                 array<T> c(s);
00068                 for (size_t i = 0; i < c.length(); ++i)
00069                         c[i] = ivl::math::rand(from, to);
00070                 return c;
00071         }
00072 
00073         T operator()(const T& from, const T& to)
00074         {
00075                 return ivl::math::rand(from, to);
00076         }
00077 
00085         template <class T, class D>
00086         inline
00087         array_nd<T> type(const array<size_t, D>& sz)
00088         {
00089                 array_nd<T> a(sz);
00090                 for (size_t i = 0; i < a.length(); i++)
00091                         a[i] = std::rand();
00092                 return a;
00093         }
00094 
00095 
00103         template <class T, class D>
00104         inline
00105         array_nd<T> operator()(const array<size_t, D>& sz)
00106         {
00107                 array_nd<T> a(sz);
00108                 for (size_t i = 0; i < a.length(); i++)
00109                         a[i] = std::rand();
00110                 return a;
00111         }
00112 
00113 
00114 } rand, rnd, random;
00115 
00116 } /* namespace ivl */
00117 
00118 #endif // IVL_MFUNC_RAND
 All Classes Namespaces Files Functions Variables Typedefs Enumerations