]> AND Private Git Repository - canny.git/blob - stc/exp/ml_stc_linux_make_v1.0/include/boost/random/uniform_on_sphere.hpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
80c8bce75e8f5b88a83780ee295d53b97160bece
[canny.git] / stc / exp / ml_stc_linux_make_v1.0 / include / boost / random / uniform_on_sphere.hpp
1 /* boost random/uniform_on_sphere.hpp header file\r
2  *\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
7  *\r
8  * See http://www.boost.org for most recent version including documentation.\r
9  *\r
10  * $Id: uniform_on_sphere.hpp 52492 2009-04-19 14:55:57Z steven_watanabe $\r
11  *\r
12  * Revision history\r
13  *  2001-02-18  moved to individual header files\r
14  */\r
15 \r
16 #ifndef BOOST_RANDOM_UNIFORM_ON_SPHERE_HPP\r
17 #define BOOST_RANDOM_UNIFORM_ON_SPHERE_HPP\r
18 \r
19 #include <vector>\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
24 \r
25 namespace boost {\r
26 \r
27 template<class RealType = double, class Cont = std::vector<RealType> >\r
28 class uniform_on_sphere\r
29 {\r
30 public:\r
31   typedef RealType input_type;\r
32   typedef Cont result_type;\r
33 \r
34   explicit uniform_on_sphere(int dim = 2) : _container(dim), _dim(dim) { }\r
35 \r
36   // compiler-generated copy ctor and assignment operator are fine\r
37 \r
38   void reset() { _normal.reset(); }\r
39 \r
40   template<class Engine>\r
41   const result_type & operator()(Engine& eng)\r
42   {\r
43     RealType sqsum = 0;\r
44     for(typename Cont::iterator it = _container.begin();\r
45         it != _container.end();\r
46         ++it) {\r
47       RealType val = _normal(eng);\r
48       *it = val;\r
49       sqsum += val * val;\r
50     }\r
51 #ifndef BOOST_NO_STDC_NAMESPACE\r
52     using std::sqrt;\r
53 #endif\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
57     return _container;\r
58   }\r
59 \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
64   {\r
65     os << sd._dim;\r
66     return os;\r
67   }\r
68 \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
72   {\r
73     is >> std::ws >> sd._dim;\r
74     sd._container.resize(sd._dim);\r
75     return is;\r
76   }\r
77 #endif\r
78 \r
79 private:\r
80   normal_distribution<RealType> _normal;\r
81   result_type _container;\r
82   int _dim;\r
83 };\r
84 \r
85 } // namespace boost\r
86 \r
87 #endif // BOOST_RANDOM_UNIFORM_ON_SPHERE_HPP\r