IMP logo
IMP Reference Guide  2.16.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-2021 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 "internal/vector.h"
22 
23 #include <limits>
24 #include <cmath>
25 #include <boost/random/normal_distribution.hpp>
26 #include <boost/static_assert.hpp>
27 
28 #if IMP_HAS_CHECKS >= IMP_USAGE
29 #define IMP_ALGEBRA_VECTOR_CHECK check_vector()
30 #define IMP_ALGEBRA_VECTOR_CHECK_INDEX(i) check_index(i)
31 #define IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o) \
32  check_compatible_vector(o); \
33  o.check_vector()
34 #else
35 #define IMP_ALGEBRA_VECTOR_CHECK
36 #define IMP_ALGEBRA_VECTOR_CHECK_INDEX(i)
37 #define IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o)
38 #endif
39 
40 IMPALGEBRA_BEGIN_NAMESPACE
41 //! A Cartesian vector in D-dimensions.
42 /** Store a vector of Cartesian coordinates. It supports all expected
43  mathematical operators, including using * for the dot product.
44  \see Vector3D
45  \see Vector2D
46 
47  \geometry
48  */
49 template <int D>
50 class VectorBaseD : public GeometricPrimitiveD<D> {
51  void check_vector() const {
52  IMP_USAGE_CHECK(!data_.get_is_null(),
53  "Attempt to use uninitialized vector.");
54  }
55  template <int OD>
56  void check_compatible_vector(const VectorBaseD<OD> &o) const {
58  IMP_USAGE_CHECK(o.get_dimension() == get_dimension(),
59  "Dimensions don't match: " << get_dimension() << " vs "
60  << o.get_dimension());
61  }
62  void check_index(unsigned int i) const {
63 #if IMP_HAS_CHECKS < IMP_INTERNAL
64  IMP_UNUSED(i);
65 #endif
66  IMP_INTERNAL_CHECK(i < data_.get_dimension(),
67  "Invalid component of vector requested: "
68  << i << " of " << get_dimension());
69  }
70 
71  public:
72  /** Will accept a list of floats from Python. */
73  template <class Range>
74  explicit VectorBaseD(const Range &r) {
75  if (D != -1 && static_cast<int>(boost::distance(r)) != D) {
76  IMP_THROW("Expected " << D << " but got " << boost::distance(r),
78  }
80  IMP_FOREACH(double f, r) {
81  IMP_UNUSED(f);
82  IMP_USAGE_CHECK(!IMP::is_nan(f), "NaN passed to constructor");
83  }
84  }
85  data_.set_coordinates(boost::begin(r), boost::end(r));
86  }
87 
88 #ifndef SWIG
89  template <class R>
90  VectorBaseD<D> &operator=(const R &r) {
91  if (D != -1 && static_cast<int>(boost::distance(r)) != D) {
92  IMP_THROW("Expected " << D << " but got " << boost::distance(r),
94  }
96  IMP_FOREACH(double f, r) {
97  IMP_USAGE_CHECK(!IMP::is_nan(f), "NaN passed in equals");
98  }
99  }
100  data_.set_coordinates(boost::begin(r), boost::end(r));
101  }
102  //! Return the ith Cartesian coordinate.
103  /** In 3D use [0] to get the x coordinate etc. */
104  inline double operator[](unsigned int i) const {
105  IMP_ALGEBRA_VECTOR_CHECK_INDEX(i);
106  IMP_ALGEBRA_VECTOR_CHECK;
107  return data_.get_data()[i];
108  }
109  //! Return the ith Cartesian coordinate.
110  /** In 3D use [0] to get the x coordinate etc. */
111  inline double &operator[](unsigned int i) {
112  IMP_ALGEBRA_VECTOR_CHECK_INDEX(i);
113  return data_.get_data()[i];
114  }
115 
116 #endif
117  //! Default constructor
119 
120  double get_scalar_product(const VectorBaseD<D> &o) const {
121  IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
122  IMP_ALGEBRA_VECTOR_CHECK;
123  double ret = 0;
124  for (unsigned int i = 0; i < get_dimension(); ++i) {
125  ret += operator[](i) * o.operator[](i);
126  }
127  return ret;
128  }
129 
130  double get_squared_magnitude() const {
131  // Could be equivalently written as get_scalar_product(*this)
132  // This should be faster though since checks on 'o' can be skipped
133  IMP_ALGEBRA_VECTOR_CHECK;
134  double ret = 0;
135  const double *data = get_data();
136  for (unsigned int i = 0; i < get_dimension(); ++i) {
137  ret += data[i] * data[i];
138  }
139  return ret;
140  }
141 
142  double get_magnitude() const { return std::sqrt(get_squared_magnitude()); }
143 
144 #ifndef IMP_DOXYGEN
145  double operator*(const VectorBaseD<D> &o) const {
146  return get_scalar_product(o);
147  }
148 
149  VectorBaseD &operator+=(const VectorBaseD &o) {
150  IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
151  IMP_ALGEBRA_VECTOR_CHECK;
152  for (unsigned int i = 0; i < get_dimension(); ++i) {
153  operator[](i) += o[i];
154  }
155  return *this;
156  }
157 
158  VectorBaseD &operator-=(const VectorBaseD &o) {
159  IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
160  IMP_ALGEBRA_VECTOR_CHECK;
161  for (unsigned int i = 0; i < get_dimension(); ++i) {
162  operator[](i) -= o[i];
163  }
164  return *this;
165  }
166 
167  VectorBaseD &operator/=(double f) {
168  IMP_ALGEBRA_VECTOR_CHECK;
169  for (unsigned int i = 0; i < get_dimension(); ++i) {
170  operator[](i) /= f;
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  void show(std::ostream &out, std::string delim, bool parens = true) const {
184  IMP_ALGEBRA_VECTOR_CHECK;
185  if (parens) out << "(";
186  for (unsigned int i = 0; i < get_dimension(); ++i) {
187  out << operator[](i);
188  if (i != get_dimension() - 1) {
189  out << delim;
190  }
191  }
192  if (parens) out << ")";
193  }
194  IMP_SHOWABLE_INLINE(VectorBaseD, show(out, ", "););
195 #endif
196 
197 #ifndef SWIG
198  typedef double *iterator;
199  typedef const double *const_iterator;
200  iterator begin() { return data_.get_data(); }
201  iterator end() { return data_.get_data() + get_dimension(); }
202  const_iterator begin() const { return data_.get_data(); }
203  const_iterator end() const { return data_.get_data() + get_dimension(); }
204 
205  typedef double value_type;
206  typedef std::random_access_iterator_tag iterator_category;
207  typedef std::ptrdiff_t difference_type;
208  typedef double *pointer;
209  typedef double &reference;
210  typedef const double &const_reference;
211 
212  static const int DIMENSION = D;
213 #endif
214 
215 #ifndef SWIG
216  // For some reason, this method breaks IMP::atom::get_rmsd() in Python, so
217  // hide it from SWIG
218  Floats get_coordinates() const {
219  return Floats(begin(), end());
220  }
221 
222  //! Return a pointer to the data stored.
223  /** Useful for conversion to other types. */
224  const double *get_data() const { return data_.get_data(); }
225 #endif
226  unsigned int get_dimension() const { return data_.get_dimension(); }
227 
228  private:
229  internal::VectorData<double, D, false> data_;
230 };
231 
232 //! Returns a unit vector pointing at the same direction as this vector.
233 /**
234  @note If the magnitude of this vector is smaller than 1e-12
235  (an arbitrarily selected small number), returns a unit
236  vector pointing at a random direction.
237  */
238 template <class VT>
239 inline VT get_unit_vector(VT vt) {
240  static const double tiny_double =
241  256.0 * std::numeric_limits<double>::epsilon();
242  double mag = vt.get_magnitude();
243  if (mag > tiny_double) {
244  VT ret_value= vt/mag;
245  IMP_USAGE_CHECK(std::abs(ret_value.get_magnitude() - 1.0) < 256.0 * tiny_double,
246  "returned vector is not unit vector");
247  return ret_value;
248  } else {
249  // avoid division by zero - return random unit v
250  // NOTE: (1) avoids vector_generators / SphereD to prevent recursiveness
251  // (2) D might be -1, so use get_dimension()
252  static boost::variate_generator<RandomNumberGenerator,
253  boost::normal_distribution<> >
255  ::boost::normal_distribution<>(0, 1.0));
256  for (unsigned int i = 0; i < vt.get_dimension(); ++i) {
257  vt[i] = generator();
258  }
259  return get_unit_vector(vt);
260  }
261 }
262 
263 //! Returns the magnitude of vt and turns it to a unit vector in place.
264 /**
265  @note If the magnitude of this vector is smaller than 1e-12
266  (an arbitrarily selected small number), vt is turned into a unit
267  vector pointing at a random direction.
268  */
269 template <class VT>
271  const double tiny_double = 1e-12;
272  double mag = vt.get_magnitude();
273  if (mag > tiny_double) {
274  vt /= mag;
275  } else {
276  // avoid division by zero - return random unit v
277  // using get_unit_vector()
278  vt= get_unit_vector(vt);
279  }
280  return mag;
281 }
282 
283 IMPALGEBRA_END_NAMESPACE
284 
285 #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:118
IMP::Vector< Float > Floats
Standard way to pass a bunch of Float values.
Definition: types.h:47
VectorBaseD(const Range &r)
Definition: VectorBaseD.h:74
VT get_unit_vector(VT vt)
Returns a unit vector pointing at the same direction as this vector.
Definition: VectorBaseD.h:239
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:270
Base class for geometric types.
#define IMP_FOREACH(v, r)
#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:224
A Cartesian vector in D-dimensions.
Definition: VectorBaseD.h:50
An exception for an invalid value being passed to IMP.
Definition: exception.h:137
double operator[](unsigned int i) const
Return the ith Cartesian coordinate.
Definition: VectorBaseD.h:104
RandomNumberGenerator random_number_generator
A shared non-GPU random number generator.
double & operator[](unsigned int i)
Return the ith Cartesian coordinate.
Definition: VectorBaseD.h:111