IMP logo
IMP Reference Guide  2.22.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-2022 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/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 //! Constrain a value between two given limits
52 /*
53  * The limits act as the constraint to saturate the value. Examples:
54  *
55  * \code
56  * x = get_constrained(-8,-2,2); // x=-2;
57  * x = get_constrained(-2,-2,2); // x=-2;
58  * x = get_constrained(-1,-2,2); // x=-1;
59  * x = get_constrained(0,-2,2); // x=0;
60  * x = get_constrained(2,-2,2); // x=2;
61  * x = get_constrained(4,-2,2); // x=2;
62  * \endcode
63  */
64 template <typename T>
65 inline T get_constrained(const T x, const T x0, const T xF) {
66  if (x < x0) return x0;
67  if (x > xF) return xF;
68  return x;
69 }
70 
71 //! Closest power of 2 that can contain a number x
72 inline float get_next_larger_power_of_2(float x) {
73  float p = 1;
74  while (p < x) {
75  p *= 2;
76  }
77  return p;
78 }
79 
80 //! Closest power of 2 that can contain a number x
81 inline double get_next_larger_power_of_2(double x) {
82  double p = 1;
83  while (p < x) {
84  p *= 2;
85  }
86  return p;
87 }
88 
89 //! Closest power of 2 for a number, not necessarily higher
90 inline double get_closer_power_of_2(double x) {
91  double p = 1;
92  double q = 1;
93  while (p < x) {
94  q = p;
95  p *= 2;
96  }
97  if ((x - q) < (p - x)) {
98  return q;
99  }
100  return p;
101 }
102 
103 //! Compare two values for "almost equal" (intended for doubles)
104 /**
105  * epsilon is the tolerance allowed to consider the values as equal
106  */
107 inline bool get_are_almost_equal(const double a, const double b,
108  const double epsilon) {
109  return (std::abs(a - b) < epsilon);
110 }
111 
112 IMPALGEBRA_END_NAMESPACE
113 
114 #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)
Compare two values for "almost equal" (intended for doubles)
T get_constrained(const T x, const T x0, const T xF)
Constrain a value between two given limits.
double get_closer_power_of_2(double x)
Closest power of 2 for a number, not necessarily higher.