source: NonGTP/Boost/boost/random/uniform_on_sphere.hpp @ 857

Revision 857, 2.4 KB checked in by igarcia, 18 years ago (diff)
Line 
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,v 1.11 2004/07/27 03:43:32 dgregor Exp $
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/normal_distribution.hpp>
23
24namespace boost {
25
26template<class RealType = double, class Cont = std::vector<RealType> >
27class uniform_on_sphere
28{
29public:
30  typedef RealType input_type;
31  typedef Cont result_type;
32
33  explicit uniform_on_sphere(int dim = 2) : _container(dim), _dim(dim) { }
34
35  // compiler-generated copy ctor and assignment operator are fine
36
37  void reset() { _normal.reset(); }
38
39  template<class Engine>
40  const result_type & operator()(Engine& eng)
41  {
42    RealType sqsum = 0;
43    for(typename Cont::iterator it = _container.begin();
44        it != _container.end();
45        ++it) {
46      RealType val = _normal(eng);
47      *it = val;
48      sqsum += val * val;
49    }
50#ifndef BOOST_NO_STDC_NAMESPACE
51    using std::sqrt;
52#endif
53    // for all i: result[i] /= sqrt(sqsum)
54    std::transform(_container.begin(), _container.end(), _container.begin(),
55                   std::bind2nd(std::divides<RealType>(), sqrt(sqsum)));
56    return _container;
57  }
58
59#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
60  template<class CharT, class Traits>
61  friend std::basic_ostream<CharT,Traits>&
62  operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_on_sphere& sd)
63  {
64    os << sd._dim;
65    return os;
66  }
67
68  template<class CharT, class Traits>
69  friend std::basic_istream<CharT,Traits>&
70  operator>>(std::basic_istream<CharT,Traits>& is, uniform_on_sphere& sd)
71  {
72    is >> std::ws >> sd._dim;
73    sd._container.resize(sd._dim);
74    return is;
75  }
76#endif
77
78private:
79  normal_distribution<RealType> _normal;
80  result_type _container;
81  int _dim;
82};
83
84} // namespace boost
85
86#endif // BOOST_RANDOM_UNIFORM_ON_SPHERE_HPP
Note: See TracBrowser for help on using the repository browser.