1 /* boost random/uniform_on_sphere.hpp header file
\r
3 * Copyright Jens Maurer 2000-2001
\r
4 * Distributed under the Boost Software License, Version 1.0. (See
\r
5 * accompanying file LICENSE_1_0.txt or copy at
\r
6 * http://www.boost.org/LICENSE_1_0.txt)
\r
8 * See http://www.boost.org for most recent version including documentation.
\r
10 * $Id: uniform_on_sphere.hpp 52492 2009-04-19 14:55:57Z steven_watanabe $
\r
13 * 2001-02-18 moved to individual header files
\r
16 #ifndef BOOST_RANDOM_UNIFORM_ON_SPHERE_HPP
\r
17 #define BOOST_RANDOM_UNIFORM_ON_SPHERE_HPP
\r
20 #include <algorithm> // std::transform
\r
21 #include <functional> // std::bind2nd, std::divides
\r
22 #include <boost/random/detail/config.hpp>
\r
23 #include <boost/random/normal_distribution.hpp>
\r
27 template<class RealType = double, class Cont = std::vector<RealType> >
\r
28 class uniform_on_sphere
\r
31 typedef RealType input_type;
\r
32 typedef Cont result_type;
\r
34 explicit uniform_on_sphere(int dim = 2) : _container(dim), _dim(dim) { }
\r
36 // compiler-generated copy ctor and assignment operator are fine
\r
38 void reset() { _normal.reset(); }
\r
40 template<class Engine>
\r
41 const result_type & operator()(Engine& eng)
\r
44 for(typename Cont::iterator it = _container.begin();
\r
45 it != _container.end();
\r
47 RealType val = _normal(eng);
\r
51 #ifndef BOOST_NO_STDC_NAMESPACE
\r
54 // for all i: result[i] /= sqrt(sqsum)
\r
55 std::transform(_container.begin(), _container.end(), _container.begin(),
\r
56 std::bind2nd(std::divides<RealType>(), sqrt(sqsum)));
\r
60 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
\r
61 template<class CharT, class Traits>
\r
62 friend std::basic_ostream<CharT,Traits>&
\r
63 operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_on_sphere& sd)
\r
69 template<class CharT, class Traits>
\r
70 friend std::basic_istream<CharT,Traits>&
\r
71 operator>>(std::basic_istream<CharT,Traits>& is, uniform_on_sphere& sd)
\r
73 is >> std::ws >> sd._dim;
\r
74 sd._container.resize(sd._dim);
\r
80 normal_distribution<RealType> _normal;
\r
81 result_type _container;
\r
85 } // namespace boost
\r
87 #endif // BOOST_RANDOM_UNIFORM_ON_SPHERE_HPP
\r