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 IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
137 return get_scalar_product(o);
140 VectorBaseD &operator+=(
const VectorBaseD &o) {
141 IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
142 IMP_ALGEBRA_VECTOR_CHECK;
143 for (
unsigned int i = 0; i < get_dimension(); ++i) {
144 operator[](i) += o[i];
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/=(
double f) {
159 IMP_ALGEBRA_VECTOR_CHECK;
160 for (
unsigned int i = 0; i < get_dimension(); ++i) {
166 VectorBaseD &operator*=(
double f) {
167 IMP_ALGEBRA_VECTOR_CHECK;
168 for (
unsigned int i = 0; i < get_dimension(); ++i) {
174 void show(std::ostream &out, std::string delim,
bool parens =
true)
const {
175 IMP_ALGEBRA_VECTOR_CHECK;
176 if (parens) out <<
"(";
177 for (
unsigned int i = 0; i < get_dimension(); ++i) {
178 out << operator[](i);
179 if (i != get_dimension() - 1) {
183 if (parens) out <<
")";
189 typedef double *iterator;
190 typedef const double *const_iterator;
192 IMPALGEBRA_DEPRECATED_FUNCTION_DECL(2.2)
193 iterator coordinates_begin() {
return data_.get_data(); }
194 iterator coordinates_end() {
return data_.get_data() + get_dimension(); }
196 IMPALGEBRA_DEPRECATED_FUNCTION_DECL(2.2)
197 const_iterator coordinates_begin()
const {
return data_.get_data(); }
198 const_iterator coordinates_end()
const {
199 return data_.get_data() + get_dimension();
201 iterator begin() {
return data_.get_data(); }
202 iterator end() {
return data_.get_data() + get_dimension(); }
203 const_iterator begin()
const {
return data_.get_data(); }
204 const_iterator end()
const {
return data_.get_data() + get_dimension(); }
206 typedef double value_type;
207 typedef std::random_access_iterator_tag iterator_category;
208 typedef std::ptrdiff_t difference_type;
209 typedef double *pointer;
210 typedef double &reference;
211 typedef const double &const_reference;
213 static const int DIMENSION = D;
219 Floats get_coordinates()
const {
220 return Floats(coordinates_begin(), coordinates_end());
226 const double *
get_data()
const {
return data_.get_data(); }
228 unsigned int get_dimension()
const {
return data_.get_dimension(); }
231 internal::VectorData<double, D, false> data_;
243 const double tiny_double = 1e-12;
244 double mag = vt.get_magnitude();
245 if (mag > tiny_double) {
251 boost::variate_generator<base::RandomNumberGenerator,
252 boost::normal_distribution<> >
254 ::boost::normal_distribution<>(0, 1.0));
255 for (
unsigned int i = 0; i < vt.get_dimension(); ++i) {
262 IMPALGEBRA_END_NAMESPACE
RandomNumberGenerator random_number_generator
A shared random number generator.
VectorBaseD()
Default constructor.
VectorBaseD(const Range &r)
#define IMP_UNUSED(variable)
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
VT get_unit_vector(VT vt)
Random number generators used by IMP.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
#define IMP_USAGE_CHECK_VARIABLE(variable)
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
Various general useful functions for IMP.
#define IMP_IF_CHECK(level)
Execute the code block if a certain level checks are on.
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_THROW(message, exception_name)
Throw an exception with a message.
VectorD< D > operator*(double s, VectorD< D > o)
Exception definitions and assertions.
const double * get_data() const
A Cartesian vector in D-dimensions.
double operator[](unsigned int i) const
An exception for an invalid value being passed to IMP.
double & operator[](unsigned int i)