IMP logo
IMP Reference Guide  2.6.2
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-2016 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 { return get_scalar_product(*this); }
131 
132  double get_magnitude() const { return std::sqrt(get_squared_magnitude()); }
133 
134 #ifndef IMP_DOXYGEN
135  double operator*(const VectorBaseD<D> &o) const {
136  return get_scalar_product(o);
137  }
138 
139  VectorBaseD &operator+=(const VectorBaseD &o) {
140  IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
141  IMP_ALGEBRA_VECTOR_CHECK;
142  for (unsigned int i = 0; i < get_dimension(); ++i) {
143  operator[](i) += o[i];
144  }
145  return *this;
146  }
147 
148  VectorBaseD &operator-=(const VectorBaseD &o) {
149  IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
150  IMP_ALGEBRA_VECTOR_CHECK;
151  for (unsigned int i = 0; i < get_dimension(); ++i) {
152  operator[](i) -= o[i];
153  }
154  return *this;
155  }
156 
157  VectorBaseD &operator/=(double f) {
158  IMP_ALGEBRA_VECTOR_CHECK;
159  for (unsigned int i = 0; i < get_dimension(); ++i) {
160  operator[](i) /= f;
161  }
162  return *this;
163  }
164 
165  VectorBaseD &operator*=(double f) {
166  IMP_ALGEBRA_VECTOR_CHECK;
167  for (unsigned int i = 0; i < get_dimension(); ++i) {
168  operator[](i) *= f;
169  }
170  return *this;
171  }
172 
173  void show(std::ostream &out, std::string delim, bool parens = true) const {
174  IMP_ALGEBRA_VECTOR_CHECK;
175  if (parens) out << "(";
176  for (unsigned int i = 0; i < get_dimension(); ++i) {
177  out << operator[](i);
178  if (i != get_dimension() - 1) {
179  out << delim;
180  }
181  }
182  if (parens) out << ")";
183  }
184  IMP_SHOWABLE_INLINE(VectorBaseD, show(out, ", "););
185 #endif
186 
187 #ifndef SWIG
188  typedef double *iterator;
189  typedef const double *const_iterator;
190  iterator begin() { return data_.get_data(); }
191  iterator end() { return data_.get_data() + get_dimension(); }
192  const_iterator begin() const { return data_.get_data(); }
193  const_iterator end() const { return data_.get_data() + get_dimension(); }
194 
195  typedef double value_type;
196  typedef std::random_access_iterator_tag iterator_category;
197  typedef std::ptrdiff_t difference_type;
198  typedef double *pointer;
199  typedef double &reference;
200  typedef const double &const_reference;
201 
202  static const int DIMENSION = D;
203 #endif
204 
205 #ifndef SWIG
206  // For some reason, this method breaks IMP::atom::get_rmsd() in Python, so
207  // hide it from SWIG
208  Floats get_coordinates() const {
209  return Floats(begin(), end());
210  }
211 
212  //! Return a pointer to the data stored.
213  /** Useful for conversion to other types. */
214  const double *get_data() const { return data_.get_data(); }
215 #endif
216  unsigned int get_dimension() const { return data_.get_dimension(); }
217 
218  private:
219  internal::VectorData<double, D, false> data_;
220 };
221 
222 //! Returns a unit vector pointing at the same direction as this vector.
223 /**
224  @note If the magnitude of this vector is smaller than 1e-12
225  (an arbitrarily selected small number), returns a unit
226  vector pointing at a random direction.
227  */
228 template <class VT>
229 inline VT get_unit_vector(VT vt) {
230  const double tiny_double = 1e-12;
231  double mag = vt.get_magnitude();
232  if (mag > tiny_double) {
233  return vt / mag;
234  } else {
235  // avoid division by zero - return random unit v
236  // NOTE: (1) avoids vector_generators / SphereD to prevent recursiveness
237  // (2) D might be -1, so use get_dimension()
238  boost::variate_generator<RandomNumberGenerator,
239  boost::normal_distribution<> >
241  ::boost::normal_distribution<>(0, 1.0));
242  for (unsigned int i = 0; i < vt.get_dimension(); ++i) {
243  vt[i] = generator();
244  }
245  return get_unit_vector(vt);
246  }
247 }
248 
249 IMPALGEBRA_END_NAMESPACE
250 
251 #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:229
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
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
Exception definitions and assertions.
#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:214
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