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 <cereal/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_INTERNAL
30 #define IMP_ALGEBRA_VECTOR_CHECK check_vector()
32 #define IMP_ALGEBRA_VECTOR_CHECK
35 #if IMP_HAS_CHECKS >= IMP_USAGE
36 #define IMP_ALGEBRA_VECTOR_CHECK_INDEX(i) check_index(i)
37 #define IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o) \
38 check_compatible_vector(o); \
41 #define IMP_ALGEBRA_VECTOR_CHECK_INDEX(i)
42 #define IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o)
45 IMPALGEBRA_BEGIN_NAMESPACE
56 friend class cereal::access;
58 template<
class Archive>
59 void serialize(Archive &ar) {
63 void check_vector()
const {
65 "Attempt to use uninitialized vector.");
71 "Dimensions don't match: " << get_dimension() <<
" vs "
72 << o.get_dimension());
74 void check_index(
unsigned int i)
const {
75 #if IMP_HAS_CHECKS < IMP_INTERNAL
79 "Invalid component of vector requested: "
80 << i <<
" of " << get_dimension());
85 template <
class Range>
87 if (D != -1 && static_cast<int>(boost::distance(r)) != D) {
88 IMP_THROW(
"Expected " << D <<
" but got " << boost::distance(r),
97 data_.set_coordinates(boost::begin(r), boost::end(r));
103 if (D != -1 && static_cast<int>(boost::distance(r)) != D) {
104 IMP_THROW(
"Expected " << D <<
" but got " << boost::distance(r),
112 data_.set_coordinates(boost::begin(r), boost::end(r));
117 IMP_ALGEBRA_VECTOR_CHECK_INDEX(i);
118 IMP_ALGEBRA_VECTOR_CHECK;
119 return data_.get_data()[i];
124 IMP_ALGEBRA_VECTOR_CHECK_INDEX(i);
125 return data_.get_data()[i];
133 IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
134 IMP_ALGEBRA_VECTOR_CHECK;
136 for (
unsigned int i = 0; i < get_dimension(); ++i) {
137 ret += operator[](i) * o.operator[](i);
142 double get_squared_magnitude()
const {
145 IMP_ALGEBRA_VECTOR_CHECK;
147 const double *data = get_data();
148 for (
unsigned int i = 0; i < get_dimension(); ++i) {
149 ret += data[i] * data[i];
154 double get_magnitude()
const {
return std::sqrt(get_squared_magnitude()); }
157 double operator*(
const VectorBaseD<D> &o)
const {
158 return get_scalar_product(o);
161 VectorBaseD &operator+=(
const VectorBaseD &o) {
162 IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
163 IMP_ALGEBRA_VECTOR_CHECK;
164 for (
unsigned int i = 0; i < get_dimension(); ++i) {
165 operator[](i) += o[i];
170 VectorBaseD &operator-=(
const VectorBaseD &o) {
171 IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
172 IMP_ALGEBRA_VECTOR_CHECK;
173 for (
unsigned int i = 0; i < get_dimension(); ++i) {
174 operator[](i) -= o[i];
179 VectorBaseD &operator/=(
double f) {
180 IMP_ALGEBRA_VECTOR_CHECK;
181 for (
unsigned int i = 0; i < get_dimension(); ++i) {
187 VectorBaseD &operator*=(
double f) {
188 IMP_ALGEBRA_VECTOR_CHECK;
189 for (
unsigned int i = 0; i < get_dimension(); ++i) {
195 void show(std::ostream &out, std::string delim,
bool parens =
true)
const {
196 IMP_ALGEBRA_VECTOR_CHECK;
197 if (parens) out <<
"(";
198 for (
unsigned int i = 0; i < get_dimension(); ++i) {
199 out << operator[](i);
200 if (i != get_dimension() - 1) {
204 if (parens) out <<
")";
210 typedef double *iterator;
211 typedef const double *const_iterator;
212 iterator begin() {
return data_.get_data(); }
213 iterator end() {
return data_.get_data() + get_dimension(); }
214 const_iterator begin()
const {
return data_.get_data(); }
215 const_iterator end()
const {
return data_.get_data() + get_dimension(); }
217 typedef double value_type;
218 typedef std::random_access_iterator_tag iterator_category;
219 typedef std::ptrdiff_t difference_type;
220 typedef double *pointer;
221 typedef double &reference;
222 typedef const double &const_reference;
224 static const int DIMENSION = D;
230 Floats get_coordinates()
const {
231 return Floats(begin(), end());
236 const double *
get_data()
const {
return data_.get_data(); }
238 unsigned int get_dimension()
const {
return data_.get_dimension(); }
241 internal::VectorData<double, D, false> data_;
252 static const double tiny_double =
253 256.0 * std::numeric_limits<double>::epsilon();
254 double mag = vt.get_magnitude();
255 if (mag > tiny_double) {
256 VT ret_value= vt/mag;
257 IMP_USAGE_CHECK(std::abs(ret_value.get_magnitude() - 1.0) < 256.0 * tiny_double,
258 "returned vector is not unit vector");
264 static boost::variate_generator<RandomNumberGenerator,
265 boost::normal_distribution<> >
267 ::boost::normal_distribution<>(0, 1.0));
268 for (
unsigned int i = 0; i < vt.get_dimension(); ++i) {
283 const double tiny_double = 1e-12;
284 double mag = vt.get_magnitude();
285 if (mag > tiny_double) {
295 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.