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 "internal/vector.h"
25 #include <boost/random/normal_distribution.hpp>
26 #include <boost/static_assert.hpp>
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); \
35 #define IMP_ALGEBRA_VECTOR_CHECK
36 #define IMP_ALGEBRA_VECTOR_CHECK_INDEX(i)
37 #define IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o)
40 IMPALGEBRA_BEGIN_NAMESPACE
51 void check_vector()
const {
53 "Attempt to use uninitialized vector.");
59 "Dimensions don't match: " << get_dimension() <<
" vs "
60 << o.get_dimension());
62 void check_index(
unsigned int i)
const {
63 #if IMP_HAS_CHECKS < IMP_INTERNAL
67 "Invalid component of vector requested: "
68 << i <<
" of " << get_dimension());
73 template <
class Range>
75 if (D != -1 && static_cast<int>(boost::distance(r)) != D) {
76 IMP_THROW(
"Expected " << D <<
" but got " << boost::distance(r),
85 data_.set_coordinates(boost::begin(r), boost::end(r));
91 if (D != -1 && static_cast<int>(boost::distance(r)) != D) {
92 IMP_THROW(
"Expected " << D <<
" but got " << boost::distance(r),
100 data_.set_coordinates(boost::begin(r), boost::end(r));
105 IMP_ALGEBRA_VECTOR_CHECK_INDEX(i);
106 IMP_ALGEBRA_VECTOR_CHECK;
107 return data_.get_data()[i];
112 IMP_ALGEBRA_VECTOR_CHECK_INDEX(i);
113 return data_.get_data()[i];
121 IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
122 IMP_ALGEBRA_VECTOR_CHECK;
124 for (
unsigned int i = 0; i < get_dimension(); ++i) {
125 ret += operator[](i) * o.operator[](i);
130 double get_squared_magnitude()
const {
133 IMP_ALGEBRA_VECTOR_CHECK;
135 const double *data = get_data();
136 for (
unsigned int i = 0; i < get_dimension(); ++i) {
137 ret += data[i] * data[i];
142 double get_magnitude()
const {
return std::sqrt(get_squared_magnitude()); }
145 double operator*(
const VectorBaseD<D> &o)
const {
146 return get_scalar_product(o);
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];
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];
167 VectorBaseD &operator/=(
double f) {
168 IMP_ALGEBRA_VECTOR_CHECK;
169 for (
unsigned int i = 0; i < get_dimension(); ++i) {
175 VectorBaseD &operator*=(
double f) {
176 IMP_ALGEBRA_VECTOR_CHECK;
177 for (
unsigned int i = 0; i < get_dimension(); ++i) {
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) {
192 if (parens) out <<
")";
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(); }
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;
212 static const int DIMENSION = D;
218 Floats get_coordinates()
const {
219 return Floats(begin(), end());
224 const double *
get_data()
const {
return data_.get_data(); }
226 unsigned int get_dimension()
const {
return data_.get_dimension(); }
229 internal::VectorData<double, D, false> data_;
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");
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) {
271 const double tiny_double = 1e-12;
272 double mag = vt.get_magnitude();
273 if (mag > tiny_double) {
283 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_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.
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.