IMP  2.3.0
The Integrative Modeling Platform
algebra/utility.h
Go to the documentation of this file.
1 /**
2  * \file IMP/algebra/utility.h
3  * \brief Functions to deal with very common math operations
4  *
5  * Copyright 2007-2014 IMP Inventors. All rights reserved.
6 */
7 
8 #ifndef IMPALGEBRA_UTILITY_H
9 #define IMPALGEBRA_UTILITY_H
10 
11 #include <cmath> // abs
12 #include <IMP/algebra/algebra_config.h>
13 #include <IMP/base/exception.h>
14 
15 IMPALGEBRA_BEGIN_NAMESPACE
16 #ifndef IMP_DOXYGEN
17 //! xor operation between two values
18 inline bool xorT(bool x, bool y) { return (((x) && !(y)) || (!(x) && (y))); }
19 #endif
20 
21 inline double get_squared(double x) { return x * x; }
22 
23 //! Sign of a number. 1 if the number is higher or equal to 0 and -1 otherwise
24 template <typename T>
25 inline int get_sign(const T& x) {
26  if (x >= 0) return 1;
27  return -1;
28 }
29 
30 //! Rounds a number to next integer.
31 /**
32  * The result is of type integer but the argument can be of any type. Some
33  * examples:
34  *
35  * \code
36  * a = round(-0.7); // a = -1
37  * a = round(-0.2); // a = 0
38  * a = round(0.2); // a = 0
39  * a = round(0.7); // a = 1
40  * \endcode
41  */
42 template <typename T>
43 inline int get_rounded(const T& x) {
44  if (x > 0) {
45  return static_cast<int>((x) + 0.5);
46  } else {
47  return static_cast<int>((x) - 0.5);
48  }
49 }
50 
51 //! Constrains a value between two given limits
52 /*
53  * The limits act as the constrain to saturate the value. Examples:
54  * with x and constrain(x,-2,2):
55  *
56  * \code
57  * x = constrain(-8,-2,2); // x=-2;
58  * x = constrain(-2,-2,2); // x=-2;
59  * x = constrain(-1,-2,2); // x=-1;
60  * x = constrain(0,-2,2); // x=0;
61  * x = constrain(2,-2,2); // x=2;
62  * x = constrain(4,-2,2); // x=2;
63  * \endcode
64  */
65 template <typename T>
66 inline T get_constrained(const T x, const T x0, const T xF) {
67  if (x < x0) return x0;
68  if (x > xF) return xF;
69  return x;
70 }
71 
72 //! Closest power of 2 that can contain a number x
73 inline float get_next_larger_power_of_2(float x) {
74  float p = 1;
75  while (p < x) {
76  p *= 2;
77  }
78  return p;
79 }
80 
81 //! Closest power of 2 that can contain a number x
82 inline double get_next_larger_power_of_2(double x) {
83  double p = 1;
84  while (p < x) {
85  p *= 2;
86  }
87  return p;
88 }
89 
90 //! Closest power of 2 for a number, not necessarily higher
91 inline double get_closer_power_of_2(double x) {
92  double p = 1;
93  double q = 1;
94  while (p < x) {
95  q = p;
96  p *= 2;
97  }
98  if ((x - q) < (p - x)) {
99  return q;
100  }
101  return p;
102 }
103 
104 //! Compares two values (intended for doubles)
105 /**
106  * epsilon is the tolerance allowed to consider the values as equal
107  */
108 inline bool get_are_almost_equal(const double a, const double b,
109  const double epsilon) {
110  return (std::abs(a - b) < epsilon);
111 }
112 
113 IMPALGEBRA_END_NAMESPACE
114 
115 #endif /* IMPALGEBRA_UTILITY_H */
double get_next_larger_power_of_2(double x)
Closest power of 2 that can contain a number x.
Exception definitions and assertions.
int get_sign(const T &x)
Sign of a number. 1 if the number is higher or equal to 0 and -1 otherwise.
int get_rounded(const T &x)
Rounds a number to next integer.
bool get_are_almost_equal(const double a, const double b, const double epsilon)
Compares two values (intended for doubles)
T get_constrained(const T x, const T x0, const T xF)
Constrains a value between two given limits.
double get_closer_power_of_2(double x)
Closest power of 2 for a number, not necessarily higher.