Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
random.functions.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file numeric/random.functions.hh
11 /// @brief xyzVector and xyzMatrix functions
12 /// @author Justin R. Porter
13 
14 #ifndef INCLUDED_numeric_random_random_functions_hh
15 #define INCLUDED_numeric_random_random_functions_hh
16 
17 #include <numeric/random/random.hh>
18 #include <numeric/xyz.functions.hh>
19 #include <numeric/xyzVector.hh>
20 
21 namespace numeric {
22 namespace random {
23 
24 /// @detail generate uniformly distributed vector on the unit sphere
25 /// @author Zhe Zhang
26 /// @author Justin R. Porter
27 // template< typename T >
28 // inline
29 // xyzVector< T >
30 // random_point_on_unit_sphere( RandomGenerator& RG ){
31 // T phi = RG.uniform() * NumericTraits< T >::pi_2();
32 // T theta = std::acos( sin_cos_range( 1-2*RG.uniform() ) );
33 // return spherical_to_xyz( sphericalVector<T>( phi, theta, 1 ) );
34 // }
35 
36 /// @detail generate uniformly distributed unit vector
37 /// @detail a random normal distribution of coordinates gives a uniform distribution of directions.
38 /// @detail same functionality as the commented random_point_on_unit_sphere(), but simpler algorithm
39 /// @detail rename as random_uniform_distributed_direction()?
40 /// @author Zhe Zhang
41 template< typename T >
42 inline
45  xyzVector< T > vec( RG.gaussian(), RG.gaussian(), RG.gaussian() );
46  while ( vec.length() == 0 ) {
47  vec = xyzVector< T >( RG.gaussian(), RG.gaussian(), RG.gaussian() );
48  }
49  return vec.normalized();
50 }
51 
52 /// @detail generate axis and angle for axis-angle rotation for random rotation move in R/RT degrees of
53 /// freedom. rotation axis: uniformly distributed on unit sphere, rotation angle: chosen to mimic the
54 /// distribution of rotation angles obtained from gaussian distrbuted Euler angles (core/kinematics/Jump.cc),
55 /// which is a gamma-distribution-like distribution
56 /// @note gaussian distributed Euler angles do not give unbiased sampling in rotational space
57 /// by applying this angle to a uniformly chosen rotation axis unbiased rotational sampling is achieved
58 /// @brief gamma-distribution-like random angle generation, rot_mag makes exactly the same sense as in gaussian_move
59 /// @author Zhe Zhang
60 /// @auhtor Justin R. Porter
61 
62 template < typename T >
63 inline
64 T // angle in radians
66  T rot_mag,
68 )
69 {
70  xyzMatrix< T > const mat(
73  numeric::x_rotation_matrix_degrees( rot_mag * RG.gaussian() ) );
74  T theta;
75  rotation_axis( mat, theta );
76  return theta;
77 }
78 
79 /// @brief produce a random translation in xyz space
80 /// @author Zhe Zhang
81 /// @author Justin R. Porter
82 template < typename T >
83 inline
86  T trans_mag,
88 )
89 {
90  return xyzVector<T>(
91  trans_mag * RG.gaussian(),
92  trans_mag * RG.gaussian(),
93  trans_mag * RG.gaussian() );
94 }
95 
96 /// @brief Return the index in the CDF array such that it is smaller than or equal to the
97 /// uniform-random-number (urn) and that the next entry in the array is larger than urn.
98 /// The CDF ought to represent the exclusive cumulative sum of the probabilities of some
99 /// discrete set so that the width for entry i -- the gap between entry i and entry i+1
100 /// should be the probability of entry i.
101 template < typename T >
104  utility::vector1< T > const & cdf,
105  T urn
106 )
107 {
108  platform::Size lower( 1 ), upper( cdf.size() );
109  platform::Size guess( 0 );
110 
111  while ( true ) {
112 
113  guess = ( upper + lower ) / 2;
114  T guess_val = cdf[ guess ];
115  T next_val = guess < cdf.size() ? cdf[ guess+1 ] : 1.1;
116  if ( guess_val <= urn && urn < next_val ) {
117  // found it!
118  break;
119  }
120  if ( guess_val <= urn ) {
121  lower = guess+1;
122  } else {
123  upper = guess-1;
124  }
125  }
126 
127  return guess;
128 
129 }
130 
131 /// @brief Choose an element from an array of positions representing the
132 /// cumulative distribution function for some target distribution. This uses
133 /// binary search to quickly find the target index. If any position has the
134 /// same probability as the preceeding position, then its index won't be
135 /// returned.
136 template < typename T >
139  utility::vector1< T > const & cdf,
141 )
142 {
143  T const urn = RG.uniform();
144  return binary_search_cdf( cdf, urn );
145 }
146 
147 
148 }
149 }
150 
151 #endif
void normalized(xyzVector &a) const
Normalized.
Definition: xyzVector.hh:735
xyzVector< T > random_translation(T trans_mag, RandomGenerator &RG)
produce a random translation in xyz space
xyzMatrix< T > y_rotation_matrix_degrees(T const &theta)
Rotation matrix for rotation about the y axis by an angle in degrees.
platform::Size pick_random_index_from_cdf(utility::vector1< T > const &cdf, RandomGenerator &RG)
Choose an element from an array of positions representing the cumulative distribution function for so...
xyzVector< T > rotation_axis(xyzMatrix< T > const &R, T &theta)
Transformation from rotation matrix to helical axis of rotation.
platform::Size binary_search_cdf(utility::vector1< T > const &cdf, T urn)
Return the index in the CDF array such that it is smaller than or equal to the uniform-random-number ...
Random number generator system.
Definition: random.hh:73
T random_rotation_angle(T rot_mag, RandomGenerator &RG)
gamma-distribution-like random angle generation, rot_mag makes exactly the same sense as in gaussian_...
Random number generator system.
xyzMatrix< T > z_rotation_matrix_degrees(T const &theta)
Rotation matrix for rotation about the z axis by an angle in degrees.
double gaussian()
Get Gaussian distribution random number.
Definition: random.cc:99
xyzMatrix< T > x_rotation_matrix_degrees(T const &theta)
Rotation matrix for rotation about the x axis by an angle in degrees.
xyzVector and xyzMatrix functions
std::size_t Size
Definition: types.hh:37
Fast (x,y,z)-coordinate numeric vector.
xyzVector< T > random_point_on_unit_sphere(RandomGenerator &RG)