8 #ifndef IMPALGEBRA_VECTOR_BASE_D_H
9 #define IMPALGEBRA_VECTOR_BASE_D_H
11 #include <IMP/algebra/algebra_config.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"
26 #include <boost/random/normal_distribution.hpp>
27 #include <boost/static_assert.hpp>
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); \
36 #define IMP_ALGEBRA_VECTOR_CHECK
37 #define IMP_ALGEBRA_VECTOR_CHECK_INDEX(i)
38 #define IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o)
41 IMPALGEBRA_BEGIN_NAMESPACE
52 friend class boost::serialization::access;
54 template<
class Archive>
55 void serialize(Archive &ar,
const unsigned int) {
59 void check_vector()
const {
61 "Attempt to use uninitialized vector.");
67 "Dimensions don't match: " << get_dimension() <<
" vs "
68 << o.get_dimension());
70 void check_index(
unsigned int i)
const {
71 #if IMP_HAS_CHECKS < IMP_INTERNAL
75 "Invalid component of vector requested: "
76 << i <<
" of " << get_dimension());
81 template <
class Range>
83 if (D != -1 && static_cast<int>(boost::distance(r)) != D) {
84 IMP_THROW(
"Expected " << D <<
" but got " << boost::distance(r),
93 data_.set_coordinates(boost::begin(r), boost::end(r));
99 if (D != -1 && static_cast<int>(boost::distance(r)) != D) {
100 IMP_THROW(
"Expected " << D <<
" but got " << boost::distance(r),
108 data_.set_coordinates(boost::begin(r), boost::end(r));
113 IMP_ALGEBRA_VECTOR_CHECK_INDEX(i);
114 IMP_ALGEBRA_VECTOR_CHECK;
115 return data_.get_data()[i];
120 IMP_ALGEBRA_VECTOR_CHECK_INDEX(i);
121 return data_.get_data()[i];
129 IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
130 IMP_ALGEBRA_VECTOR_CHECK;
132 for (
unsigned int i = 0; i < get_dimension(); ++i) {
133 ret += operator[](i) * o.operator[](i);
138 double get_squared_magnitude()
const {
141 IMP_ALGEBRA_VECTOR_CHECK;
143 const double *data = get_data();
144 for (
unsigned int i = 0; i < get_dimension(); ++i) {
145 ret += data[i] * data[i];
150 double get_magnitude()
const {
return std::sqrt(get_squared_magnitude()); }
153 double operator*(
const VectorBaseD<D> &o)
const {
154 return get_scalar_product(o);
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];
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];
175 VectorBaseD &operator/=(
double f) {
176 IMP_ALGEBRA_VECTOR_CHECK;
177 for (
unsigned int i = 0; i < get_dimension(); ++i) {
183 VectorBaseD &operator*=(
double f) {
184 IMP_ALGEBRA_VECTOR_CHECK;
185 for (
unsigned int i = 0; i < get_dimension(); ++i) {
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) {
200 if (parens) out <<
")";
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(); }
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;
220 static const int DIMENSION = D;
226 Floats get_coordinates()
const {
227 return Floats(begin(), end());
232 const double *
get_data()
const {
return data_.get_data(); }
234 unsigned int get_dimension()
const {
return data_.get_dimension(); }
237 internal::VectorData<double, D, false> data_;
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");
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) {
279 const double tiny_double = 1e-12;
280 double mag = vt.get_magnitude();
281 if (mag > tiny_double) {
291 IMPALGEBRA_END_NAMESPACE
Base class for geometric types.
#define IMP_USAGE_CHECK_VARIABLE(variable)
#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.
VectorBaseD()
Default constructor.
IMP::Vector< Float > Floats
Standard way to pass a bunch of Float values.
VectorBaseD(const Range &r)
VT get_unit_vector(VT vt)
Returns a unit vector pointing at the same direction as this vector.
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.
double get_magnitude_and_normalize_in_place(VT &vt)
Returns the magnitude of vt and turns it to a unit vector in place.
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.
Helper macros for throwing and handling exceptions.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
VectorD< D > operator*(double s, VectorD< D > o)
Random number generators used by IMP.
const double * get_data() const
Return a pointer to the data stored.
A Cartesian vector in D-dimensions.
An exception for an invalid value being passed to IMP.
double operator[](unsigned int i) const
Return the ith Cartesian coordinate.
RandomNumberGenerator random_number_generator
A shared non-GPU random number generator.
double & operator[](unsigned int i)
Return the ith Cartesian coordinate.