]> git.donarmstrong.com Git - rsem.git/blob - boost/random/uniform_on_sphere.hpp
c5c7280696fb7c319e032a670b6100f906b74eff
[rsem.git] / boost / random / uniform_on_sphere.hpp
1 /* boost random/uniform_on_sphere.hpp header file
2  *
3  * Copyright Jens Maurer 2000-2001
4  * Distributed under the Boost Software License, Version 1.0. (See
5  * accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  * See http://www.boost.org for most recent version including documentation.
9  *
10  * $Id: uniform_on_sphere.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
11  *
12  * Revision history
13  *  2001-02-18  moved to individual header files
14  */
15
16 #ifndef BOOST_RANDOM_UNIFORM_ON_SPHERE_HPP
17 #define BOOST_RANDOM_UNIFORM_ON_SPHERE_HPP
18
19 #include <vector>
20 #include <algorithm>     // std::transform
21 #include <functional>    // std::bind2nd, std::divides
22 #include <boost/random/detail/config.hpp>
23 #include <boost/random/normal_distribution.hpp>
24
25 namespace boost {
26
27 /**
28  * Instantiations of class template uniform_on_sphere model a
29  * \random_distribution. Such a distribution produces random
30  * numbers uniformly distributed on the unit sphere of arbitrary
31  * dimension @c dim. The @c Cont template parameter must be a STL-like
32  * container type with begin and end operations returning non-const
33  * ForwardIterators of type @c Cont::iterator. Each invocation of the
34  * @c UniformRandomNumberGenerator shall result in a floating-point
35  * value in the range [0,1). 
36  */
37 template<class RealType = double, class Cont = std::vector<RealType> >
38 class uniform_on_sphere
39 {
40 public:
41   typedef RealType input_type;
42   typedef Cont result_type;
43
44   /**
45    * Constructs a @c uniform_on_sphere distribution.
46    * @c dim is the dimension of the sphere.
47    */
48   explicit uniform_on_sphere(int dim = 2) : _container(dim), _dim(dim) { }
49
50   // compiler-generated copy ctor and assignment operator are fine
51
52   void reset() { _normal.reset(); }
53
54   template<class Engine>
55   const result_type & operator()(Engine& eng)
56   {
57     RealType sqsum = 0;
58     for(typename Cont::iterator it = _container.begin();
59         it != _container.end();
60         ++it) {
61       RealType val = _normal(eng);
62       *it = val;
63       sqsum += val * val;
64     }
65 #ifndef BOOST_NO_STDC_NAMESPACE
66     using std::sqrt;
67 #endif
68     // for all i: result[i] /= sqrt(sqsum)
69     std::transform(_container.begin(), _container.end(), _container.begin(),
70                    std::bind2nd(std::divides<RealType>(), sqrt(sqsum)));
71     return _container;
72   }
73
74 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
75   template<class CharT, class Traits>
76   friend std::basic_ostream<CharT,Traits>&
77   operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_on_sphere& sd)
78   {
79     os << sd._dim;
80     return os;
81   }
82
83   template<class CharT, class Traits>
84   friend std::basic_istream<CharT,Traits>&
85   operator>>(std::basic_istream<CharT,Traits>& is, uniform_on_sphere& sd)
86   {
87     is >> std::ws >> sd._dim;
88     sd._container.resize(sd._dim);
89     return is;
90   }
91 #endif
92
93 private:
94   normal_distribution<RealType> _normal;
95   result_type _container;
96   int _dim;
97 };
98
99 } // namespace boost
100
101 #endif // BOOST_RANDOM_UNIFORM_ON_SPHERE_HPP