fix doc example typo
[boost.git] / boost / random / uniform_on_sphere.hpp
blobad7334db84a9b91819a1fdb9c306f525cd20496b
1 /* boost random/uniform_on_sphere.hpp header file
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)
8 * See http://www.boost.org for most recent version including documentation.
10 * $Id$
12 * Revision history
13 * 2001-02-18 moved to individual header files
16 #ifndef BOOST_RANDOM_UNIFORM_ON_SPHERE_HPP
17 #define BOOST_RANDOM_UNIFORM_ON_SPHERE_HPP
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>
25 namespace boost {
27 template<class RealType = double, class Cont = std::vector<RealType> >
28 class uniform_on_sphere
30 public:
31 typedef RealType input_type;
32 typedef Cont result_type;
34 explicit uniform_on_sphere(int dim = 2) : _container(dim), _dim(dim) { }
36 // compiler-generated copy ctor and assignment operator are fine
38 void reset() { _normal.reset(); }
40 template<class Engine>
41 const result_type & operator()(Engine& eng)
43 RealType sqsum = 0;
44 for(typename Cont::iterator it = _container.begin();
45 it != _container.end();
46 ++it) {
47 RealType val = _normal(eng);
48 *it = val;
49 sqsum += val * val;
51 #ifndef BOOST_NO_STDC_NAMESPACE
52 using std::sqrt;
53 #endif
54 // for all i: result[i] /= sqrt(sqsum)
55 std::transform(_container.begin(), _container.end(), _container.begin(),
56 std::bind2nd(std::divides<RealType>(), sqrt(sqsum)));
57 return _container;
60 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
61 template<class CharT, class Traits>
62 friend std::basic_ostream<CharT,Traits>&
63 operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_on_sphere& sd)
65 os << sd._dim;
66 return os;
69 template<class CharT, class Traits>
70 friend std::basic_istream<CharT,Traits>&
71 operator>>(std::basic_istream<CharT,Traits>& is, uniform_on_sphere& sd)
73 is >> std::ws >> sd._dim;
74 sd._container.resize(sd._dim);
75 return is;
77 #endif
79 private:
80 normal_distribution<RealType> _normal;
81 result_type _container;
82 int _dim;
85 } // namespace boost
87 #endif // BOOST_RANDOM_UNIFORM_ON_SPHERE_HPP