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),
80 IMP_FOREACH(
double f, 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),
96 IMP_FOREACH(
double f, 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 {
return get_scalar_product(*
this); }
132 double get_magnitude()
const {
return std::sqrt(get_squared_magnitude()); }
135 double operator*(
const VectorBaseD<D> &o)
const {
136 return get_scalar_product(o);
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];
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];
157 VectorBaseD &operator/=(
double f) {
158 IMP_ALGEBRA_VECTOR_CHECK;
159 for (
unsigned int i = 0; i < get_dimension(); ++i) {
165 VectorBaseD &operator*=(
double f) {
166 IMP_ALGEBRA_VECTOR_CHECK;
167 for (
unsigned int i = 0; i < get_dimension(); ++i) {
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) {
182 if (parens) out <<
")";
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(); }
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;
202 static const int DIMENSION = D;
208 Floats get_coordinates()
const {
209 return Floats(begin(), end());
214 const double *
get_data()
const {
return data_.get_data(); }
216 unsigned int get_dimension()
const {
return data_.get_dimension(); }
219 internal::VectorData<double, D, false> data_;
230 const double tiny_double = 1e-12;
231 double mag = vt.get_magnitude();
232 if (mag > tiny_double) {
238 boost::variate_generator<base::RandomNumberGenerator,
239 boost::normal_distribution<> >
241 ::boost::normal_distribution<>(0, 1.0));
242 for (
unsigned int i = 0; i < vt.get_dimension(); ++i) {
249 IMPALGEBRA_END_NAMESPACE
#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.
RandomNumberGenerator random_number_generator
A shared random number generator.
VectorBaseD()
Default constructor.
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.
Base class for geometric types.
#define IMP_UNUSED(variable)
Various general useful functions for IMP.
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
Exception definitions and assertions.
void show(Hierarchy h, std::ostream &out=std::cout)
Print out a molecular hierarchy.
IMP::base::Vector< Float > Floats
Standard way to pass a bunch of Float values.
#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.
double operator[](unsigned int i) const
Return the ith Cartesian coordinate.
An exception for an invalid value being passed to IMP.
double & operator[](unsigned int i)
Return the ith Cartesian coordinate.