IMP logo
IMP Reference Guide  2.18.0
The Integrative Modeling Platform
VectorBaseD.h
Go to the documentation of this file.
1 /**
2  * \file IMP/algebra/VectorBaseD.h \brief Simple D vector class.
3  *
4  * Copyright 2007-2022 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPALGEBRA_VECTOR_BASE_D_H
9 #define IMPALGEBRA_VECTOR_BASE_D_H
10 
11 #include <IMP/algebra/algebra_config.h>
12 #include "GeometricPrimitiveD.h"
13 #include <IMP/check_macros.h>
14 #include <IMP/exception.h>
15 #include <IMP/random.h>
16 #include <IMP/utility.h>
17 #include <IMP/types.h>
18 #include <boost/random/variate_generator.hpp>
19 #include <boost/random/normal_distribution.hpp>
20 #include <boost/range.hpp>
21 #include <boost/serialization/access.hpp>
22 #include "internal/vector.h"
23 
24 #include <limits>
25 #include <cmath>
26 #include <boost/random/normal_distribution.hpp>
27 #include <boost/static_assert.hpp>
28 
29 #if IMP_HAS_CHECKS >= IMP_USAGE
30 #define IMP_ALGEBRA_VECTOR_CHECK check_vector()
31 #define IMP_ALGEBRA_VECTOR_CHECK_INDEX(i) check_index(i)
32 #define IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o) \
33  check_compatible_vector(o); \
34  o.check_vector()
35 #else
36 #define IMP_ALGEBRA_VECTOR_CHECK
37 #define IMP_ALGEBRA_VECTOR_CHECK_INDEX(i)
38 #define IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o)
39 #endif
40 
41 IMPALGEBRA_BEGIN_NAMESPACE
42 //! A Cartesian vector in D-dimensions.
43 /** Store a vector of Cartesian coordinates. It supports all expected
44  mathematical operators, including using * for the dot product.
45  \see Vector3D
46  \see Vector2D
47 
48  \geometry
49  */
50 template <int D>
51 class VectorBaseD : public GeometricPrimitiveD<D> {
52  friend class boost::serialization::access;
53 
54  template<class Archive>
55  void serialize(Archive &ar, const unsigned int) {
56  ar & data_;
57  }
58 
59  void check_vector() const {
60  IMP_USAGE_CHECK(!data_.get_is_null(),
61  "Attempt to use uninitialized vector.");
62  }
63  template <int OD>
64  void check_compatible_vector(const VectorBaseD<OD> &o) const {
66  IMP_USAGE_CHECK(o.get_dimension() == get_dimension(),
67  "Dimensions don't match: " << get_dimension() << " vs "
68  << o.get_dimension());
69  }
70  void check_index(unsigned int i) const {
71 #if IMP_HAS_CHECKS < IMP_INTERNAL
72  IMP_UNUSED(i);
73 #endif
74  IMP_INTERNAL_CHECK(i < data_.get_dimension(),
75  "Invalid component of vector requested: "
76  << i << " of " << get_dimension());
77  }
78 
79  public:
80  /** Will accept a list of floats from Python. */
81  template <class Range>
82  explicit VectorBaseD(const Range &r) {
83  if (D != -1 && static_cast<int>(boost::distance(r)) != D) {
84  IMP_THROW("Expected " << D << " but got " << boost::distance(r),
86  }
88  for(double f : r) {
89  IMP_UNUSED(f);
90  IMP_USAGE_CHECK(!IMP::is_nan(f), "NaN passed to constructor");
91  }
92  }
93  data_.set_coordinates(boost::begin(r), boost::end(r));
94  }
95 
96 #ifndef SWIG
97  template <class R>
98  VectorBaseD<D> &operator=(const R &r) {
99  if (D != -1 && static_cast<int>(boost::distance(r)) != D) {
100  IMP_THROW("Expected " << D << " but got " << boost::distance(r),
102  }
104  for(double f : r) {
105  IMP_USAGE_CHECK(!IMP::is_nan(f), "NaN passed in equals");
106  }
107  }
108  data_.set_coordinates(boost::begin(r), boost::end(r));
109  }
110  //! Return the ith Cartesian coordinate.
111  /** In 3D use [0] to get the x coordinate etc. */
112  inline double operator[](unsigned int i) const {
113  IMP_ALGEBRA_VECTOR_CHECK_INDEX(i);
114  IMP_ALGEBRA_VECTOR_CHECK;
115  return data_.get_data()[i];
116  }
117  //! Return the ith Cartesian coordinate.
118  /** In 3D use [0] to get the x coordinate etc. */
119  inline double &operator[](unsigned int i) {
120  IMP_ALGEBRA_VECTOR_CHECK_INDEX(i);
121  return data_.get_data()[i];
122  }
123 
124 #endif
125  //! Default constructor
127 
128  double get_scalar_product(const VectorBaseD<D> &o) const {
129  IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
130  IMP_ALGEBRA_VECTOR_CHECK;
131  double ret = 0;
132  for (unsigned int i = 0; i < get_dimension(); ++i) {
133  ret += operator[](i) * o.operator[](i);
134  }
135  return ret;
136  }
137 
138  double get_squared_magnitude() const {
139  // Could be equivalently written as get_scalar_product(*this)
140  // This should be faster though since checks on 'o' can be skipped
141  IMP_ALGEBRA_VECTOR_CHECK;
142  double ret = 0;
143  const double *data = get_data();
144  for (unsigned int i = 0; i < get_dimension(); ++i) {
145  ret += data[i] * data[i];
146  }
147  return ret;
148  }
149 
150  double get_magnitude() const { return std::sqrt(get_squared_magnitude()); }
151 
152 #ifndef IMP_DOXYGEN
153  double operator*(const VectorBaseD<D> &o) const {
154  return get_scalar_product(o);
155  }
156 
157  VectorBaseD &operator+=(const VectorBaseD &o) {
158  IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
159  IMP_ALGEBRA_VECTOR_CHECK;
160  for (unsigned int i = 0; i < get_dimension(); ++i) {
161  operator[](i) += o[i];
162  }
163  return *this;
164  }
165 
166  VectorBaseD &operator-=(const VectorBaseD &o) {
167  IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
168  IMP_ALGEBRA_VECTOR_CHECK;
169  for (unsigned int i = 0; i < get_dimension(); ++i) {
170  operator[](i) -= o[i];
171  }
172  return *this;
173  }
174 
175  VectorBaseD &operator/=(double f) {
176  IMP_ALGEBRA_VECTOR_CHECK;
177  for (unsigned int i = 0; i < get_dimension(); ++i) {
178  operator[](i) /= f;
179  }
180  return *this;
181  }
182 
183  VectorBaseD &operator*=(double f) {
184  IMP_ALGEBRA_VECTOR_CHECK;
185  for (unsigned int i = 0; i < get_dimension(); ++i) {
186  operator[](i) *= f;
187  }
188  return *this;
189  }
190 
191  void show(std::ostream &out, std::string delim, bool parens = true) const {
192  IMP_ALGEBRA_VECTOR_CHECK;
193  if (parens) out << "(";
194  for (unsigned int i = 0; i < get_dimension(); ++i) {
195  out << operator[](i);
196  if (i != get_dimension() - 1) {
197  out << delim;
198  }
199  }
200  if (parens) out << ")";
201  }
202  IMP_SHOWABLE_INLINE(VectorBaseD, show(out, ", "););
203 #endif
204 
205 #ifndef SWIG
206  typedef double *iterator;
207  typedef const double *const_iterator;
208  iterator begin() { return data_.get_data(); }
209  iterator end() { return data_.get_data() + get_dimension(); }
210  const_iterator begin() const { return data_.get_data(); }
211  const_iterator end() const { return data_.get_data() + get_dimension(); }
212 
213  typedef double value_type;
214  typedef std::random_access_iterator_tag iterator_category;
215  typedef std::ptrdiff_t difference_type;
216  typedef double *pointer;
217  typedef double &reference;
218  typedef const double &const_reference;
219 
220  static const int DIMENSION = D;
221 #endif
222 
223 #ifndef SWIG
224  // For some reason, this method breaks IMP::atom::get_rmsd() in Python, so
225  // hide it from SWIG
226  Floats get_coordinates() const {
227  return Floats(begin(), end());
228  }
229 
230  //! Return a pointer to the data stored.
231  /** Useful for conversion to other types. */
232  const double *get_data() const { return data_.get_data(); }
233 #endif
234  unsigned int get_dimension() const { return data_.get_dimension(); }
235 
236  private:
237  internal::VectorData<double, D, false> data_;
238 };
239 
240 //! Returns a unit vector pointing at the same direction as this vector.
241 /**
242  @note If the magnitude of this vector is smaller than 1e-12
243  (an arbitrarily selected small number), returns a unit
244  vector pointing at a random direction.
245  */
246 template <class VT>
247 inline VT get_unit_vector(VT vt) {
248  static const double tiny_double =
249  256.0 * std::numeric_limits<double>::epsilon();
250  double mag = vt.get_magnitude();
251  if (mag > tiny_double) {
252  VT ret_value= vt/mag;
253  IMP_USAGE_CHECK(std::abs(ret_value.get_magnitude() - 1.0) < 256.0 * tiny_double,
254  "returned vector is not unit vector");
255  return ret_value;
256  } else {
257  // avoid division by zero - return random unit v
258  // NOTE: (1) avoids vector_generators / SphereD to prevent recursiveness
259  // (2) D might be -1, so use get_dimension()
260  static boost::variate_generator<RandomNumberGenerator,
261  boost::normal_distribution<> >
263  ::boost::normal_distribution<>(0, 1.0));
264  for (unsigned int i = 0; i < vt.get_dimension(); ++i) {
265  vt[i] = generator();
266  }
267  return get_unit_vector(vt);
268  }
269 }
270 
271 //! Returns the magnitude of vt and turns it to a unit vector in place.
272 /**
273  @note If the magnitude of this vector is smaller than 1e-12
274  (an arbitrarily selected small number), vt is turned into a unit
275  vector pointing at a random direction.
276  */
277 template <class VT>
279  const double tiny_double = 1e-12;
280  double mag = vt.get_magnitude();
281  if (mag > tiny_double) {
282  vt /= mag;
283  } else {
284  // avoid division by zero - return random unit v
285  // using get_unit_vector()
286  vt= get_unit_vector(vt);
287  }
288  return mag;
289 }
290 
291 IMPALGEBRA_END_NAMESPACE
292 
293 #endif /* IMPALGEBRA_VECTOR_BASE_D_H */
Base class for geometric types.
Basic types used by IMP.
#define IMP_USAGE_CHECK_VARIABLE(variable)
Definition: check_macros.h:190
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
#define IMP_IF_CHECK(level)
Execute the code block if a certain level checks are on.
Definition: check_macros.h:104
VectorBaseD()
Default constructor.
Definition: VectorBaseD.h:126
IMP::Vector< Float > Floats
Standard way to pass a bunch of Float values.
Definition: types.h:46
VectorBaseD(const Range &r)
Definition: VectorBaseD.h:82
VT get_unit_vector(VT vt)
Returns a unit vector pointing at the same direction as this vector.
Definition: VectorBaseD.h:247
Exception definitions and assertions.
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
Definition: check_macros.h:139
double get_magnitude_and_normalize_in_place(VT &vt)
Returns the magnitude of vt and turns it to a unit vector in place.
Definition: VectorBaseD.h:278
Base class for geometric types.
#define IMP_UNUSED(variable)
For backwards compatibility.
std::ostream & show(Hierarchy h, std::ostream &out=std::cout)
Print the hierarchy using a given decorator to display each node.
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
Definition: check_macros.h:50
Helper macros for throwing and handling exceptions.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
VectorD< D > operator*(double s, VectorD< D > o)
Definition: VectorD.h:193
Random number generators used by IMP.
const double * get_data() const
Return a pointer to the data stored.
Definition: VectorBaseD.h:232
A Cartesian vector in D-dimensions.
Definition: VectorBaseD.h:51
An exception for an invalid value being passed to IMP.
Definition: exception.h:136
double operator[](unsigned int i) const
Return the ith Cartesian coordinate.
Definition: VectorBaseD.h:112
RandomNumberGenerator random_number_generator
A shared non-GPU random number generator.
double & operator[](unsigned int i)
Return the ith Cartesian coordinate.
Definition: VectorBaseD.h:119