Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
util.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/util.hh
11 /// @brief small bundle of utilities for dealing with numbers.
12 /// @author James Thompson
13 
14 #ifndef INCLUDED_numeric_util_hh
15 #define INCLUDED_numeric_util_hh
16 
17 #include <numeric/types.hh>
19 #include <utility/vector1.hh>
20 
21 #include <limits>
22 #include <cmath>
23 #include <algorithm>
24 
25 namespace numeric {
26 
27 /// @brief Clamps <value> to the closed interval [lower_bound, upper_bound].
28 /// Templated type must implement operator<.
29 template<typename Number>
30 Number clamp(Number value, Number lower_bound, Number upper_bound) {
31  if ( value < lower_bound ) {
32  return lower_bound;
33  } else if ( upper_bound < value ) {
34  return upper_bound;
35  } else {
36  return value;
37  }
38 }
39 
40 /// @brief Computes log(x) in the given base
41 inline double log(double x, double base) {
42  return std::log10(x) / std::log10(base);
43 }
44 
45 /// @brief portable check to see if a value is NaN.
46 template < typename T >
47 inline bool isnan( T value ) {
48  return value != value;
49 }
50 
51 template < typename T >
52 inline bool isinf( T value ) {
53  return std::numeric_limits< T >::has_infinity &&
54  value == std::numeric_limits< T >::infinity();
55 }
56 
57 /// @brief are two Real values are equal up to some epsilon
58 ///
59 /// implemented only for Reals, to prevent unsigned hassle
60 /// (Barak 30/6/2009)
61 inline bool equal_by_epsilon(
62  numeric::Real value1,
63  numeric::Real value2,
64  numeric::Real epsilon
65 ) {
66  if ( epsilon < 0 ) {
67  epsilon = -epsilon;
68  }
69  return (
70  value1 <= value2 + epsilon &&
71  value1 >= value2 - epsilon
72  );
73 }
74 
75 
76 /// @brief Returns the median from a vector1 of Real values.
78 
80 
81 template < typename T >
82 T max( utility::vector1< T > const & values ) {
83  assert(values.size());
84  T retval = values[1];
85  for ( numeric::Size ii(2); ii <= values.size(); ++ii ) {
86  retval = max(retval, values[ii]);
87  }
88  return retval;
89 }
90 
91 template < typename T >
92 T min( utility::vector1< T > const & values ) {
93  assert(values.size());
94  T retval = values[1];
95  for ( numeric::Size ii(2); ii <= values.size(); ++ii ) {
96  retval = min(retval, values[ii]);
97  }
98  return retval;
99 }
100 
101 
102 /// @brief Calculates the acceptance probability of a given score-change at
103 /// the given temperature, generally used in simulated annealing algorithms.
104 /// Returns a value in the range (0-1).
105 inline
107  Real const score_before,
108  Real const score_after,
109  Real const temperature
110 ) {
111  Real const boltz_factor( ( score_before - score_after ) / temperature );
112  Real const probability (
113  std::exp( std::min( 40.0, std::max( -40.0, boltz_factor ) ) )
114  );
115  return probability;
116 }
117 
118 /// @brief recursive binary search that finds the value closest to key. Call find_nearest_value(input_list,value) instead. It's the driver
119 /// function for this function. This fails miserably (and silently!) on a non-sorted vector, so don't do that!.
120 template < typename T >
121 T find_nearest_value(typename utility::vector1<T> const & input_list, T key, platform::Size min_index, platform::Size max_index)
122 {
123 
124  if ( (max_index - min_index) == 1 ) {
125  //down to the last two items and we haven't found the key yet, one of these must be the closest.
126  T max_distance = std::abs(input_list[max_index] - key);
127  T min_distance = std::abs(input_list[min_index] - key);
128  if ( max_distance < min_distance ) {
129  return input_list[max_index];
130  } else {
131  return input_list[min_index];
132  }
133  }
134 
135  platform::Size mid_index = (min_index + max_index) /2;
136 
137  if ( input_list[mid_index] > key ) {
138  return find_nearest_value<T>(input_list,key,min_index,mid_index-1);
139  } else if ( input_list[mid_index] < key ) {
140  return find_nearest_value<T>(input_list,key,mid_index+1, max_index);
141  } else {
142  return input_list[mid_index];
143  }
144 }
145 
146 /// @brief given a vector and an input value, return the value in the vector that is closest to the input
147 /// This is a wrapper for find_nearest_value(input_list,key,min,max) and insures that you're sorted.
148 template < typename T >
149 T find_nearest_value(typename utility::vector1<T> const & input_list, T key)
150 {
151  utility::vector1<T> temp_list = input_list;
152  std::sort(temp_list.begin(),temp_list.end());
153 
154  platform::Size initial_minimum = 1;
155  platform::Size initial_maximum = temp_list.size();
156 
157  return find_nearest_value<T>(temp_list,key,initial_minimum,initial_maximum);
158 
159 }
160 
161 } // numeric
162 
163 #endif
static T min(T x, T y)
Definition: Svm.cc:16
bool isnan(T value)
portable check to see if a value is NaN.
Definition: util.hh:47
Real boltzmann_accept_probability(Real const score_before, Real const score_after, Real const temperature)
Calculates the acceptance probability of a given score-change at the given temperature, generally used in simulated annealing algorithms. Returns a value in the range (0-1).
Definition: util.hh:106
platform::Size Size
Definition: types.hh:42
def x
bool equal_by_epsilon(numeric::Real value1, numeric::Real value2, numeric::Real epsilon)
are two Real values are equal up to some epsilon
Definition: util.hh:61
numeric::Real median(utility::vector1< numeric::Real > const &values)
Returns the median from a vector1 of Real values.
Definition: util.cc:21
bool isinf(T value)
Definition: util.hh:52
double log(double x, double base)
Computes log(x) in the given base.
Definition: util.hh:41
numeric::Real mean(utility::vector1< numeric::Real > const &values)
Definition: util.cc:37
short int min(short int const a, short int const b)
min( short int, short int )
member1 value
Definition: Tag.cc:296
short int max(short int const a, short int const b)
max( short int, short int )
Number clamp(Number value, Number lower_bound, Number upper_bound)
Clamps to the closed interval [lower_bound, upper_bound]. Templated type must implement operator<...
Definition: util.hh:30
utility::keys::lookup::key< KeyType > const key
T abs(T const &x)
std::abs( x ) == | x |
Definition: Fmath.hh:295
std::vector with 1-based indexing
Definition: vector1.fwd.hh:44
rosetta project type declarations. Should be kept updated with core/types.hh. This exists because num...
double Real
Definition: types.hh:39
vector1: std::vector with 1-based indexing
Numeric functions.
T find_nearest_value(typename utility::vector1< T > const &input_list, T key, platform::Size min_index, platform::Size max_index)
recursive binary search that finds the value closest to key. Call find_nearest_value(input_list,value) instead. It's the driver function for this function. This fails miserably (and silently!) on a non-sorted vector, so don't do that!.
Definition: util.hh:121
std::size_t Size
Definition: types.hh:37
static T max(T x, T y)
Definition: Svm.cc:19